Skip to content

Commit 9d084a3

Browse files
Martonidtor
authored andcommitted
Input: add Austria Microsystem AS5011 joystick driver
This is driver for EasyPoint AS5011 2 axis joystick chip. This chip is plugged on an I2C bus. Tested on ARM processor (i.MX27). Signed-off-by: Fabien Marteau <fabien.marteau@armadeus.com> Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
1 parent 048fc01 commit 9d084a3

4 files changed

Lines changed: 398 additions & 0 deletions

File tree

‎drivers/input/joystick/Kconfig‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,16 @@ config JOYSTICK_AMIGA
255255
To compile this driver as a module, choose M here: the
256256
module will be called amijoy.
257257

258+
config JOYSTICK_AS5011
259+
tristate "Austria Microsystem AS5011 joystick"
260+
depends on I2C
261+
help
262+
Say Y here if you have an AS5011 digital joystick connected to your
263+
system.
264+
265+
To compile this driver as a module, choose M here: the
266+
module will be called as5011.
267+
258268
config JOYSTICK_JOYDUMP
259269
tristate "Gameport data dumper"
260270
select GAMEPORT

‎drivers/input/joystick/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
obj-$(CONFIG_JOYSTICK_A3D) += a3d.o
88
obj-$(CONFIG_JOYSTICK_ADI) += adi.o
99
obj-$(CONFIG_JOYSTICK_AMIGA) += amijoy.o
10+
obj-$(CONFIG_JOYSTICK_AS5011) += as5011.o
1011
obj-$(CONFIG_JOYSTICK_ANALOG) += analog.o
1112
obj-$(CONFIG_JOYSTICK_COBRA) += cobra.o
1213
obj-$(CONFIG_JOYSTICK_DB9) += db9.o

‎drivers/input/joystick/as5011.c‎

Lines changed: 367 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,367 @@
1+
/*
2+
* Copyright (c) 2010, 2011 Fabien Marteau <fabien.marteau@armadeus.com>
3+
* Sponsored by ARMadeus Systems
4+
*
5+
* This program is free software; you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation; either version 2 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18+
*
19+
* Driver for Austria Microsystems joysticks AS5011
20+
*
21+
* TODO:
22+
* - Power on the chip when open() and power down when close()
23+
* - Manage power mode
24+
*/
25+
26+
#include <linux/i2c.h>
27+
#include <linux/interrupt.h>
28+
#include <linux/input.h>
29+
#include <linux/gpio.h>
30+
#include <linux/delay.h>
31+
#include <linux/input/as5011.h>
32+
#include <linux/slab.h>
33+
34+
#define DRIVER_DESC "Driver for Austria Microsystems AS5011 joystick"
35+
#define MODULE_DEVICE_ALIAS "as5011"
36+
37+
MODULE_AUTHOR("Fabien Marteau <fabien.marteau@armadeus.com>");
38+
MODULE_DESCRIPTION(DRIVER_DESC);
39+
MODULE_LICENSE("GPL");
40+
41+
/* registers */
42+
#define AS5011_CTRL1 0x76
43+
#define AS5011_CTRL2 0x75
44+
#define AS5011_XP 0x43
45+
#define AS5011_XN 0x44
46+
#define AS5011_YP 0x53
47+
#define AS5011_YN 0x54
48+
#define AS5011_X_REG 0x41
49+
#define AS5011_Y_REG 0x42
50+
#define AS5011_X_RES_INT 0x51
51+
#define AS5011_Y_RES_INT 0x52
52+
53+
/* CTRL1 bits */
54+
#define AS5011_CTRL1_LP_PULSED 0x80
55+
#define AS5011_CTRL1_LP_ACTIVE 0x40
56+
#define AS5011_CTRL1_LP_CONTINUE 0x20
57+
#define AS5011_CTRL1_INT_WUP_EN 0x10
58+
#define AS5011_CTRL1_INT_ACT_EN 0x08
59+
#define AS5011_CTRL1_EXT_CLK_EN 0x04
60+
#define AS5011_CTRL1_SOFT_RST 0x02
61+
#define AS5011_CTRL1_DATA_VALID 0x01
62+
63+
/* CTRL2 bits */
64+
#define AS5011_CTRL2_EXT_SAMPLE_EN 0x08
65+
#define AS5011_CTRL2_RC_BIAS_ON 0x04
66+
#define AS5011_CTRL2_INV_SPINNING 0x02
67+
68+
#define AS5011_MAX_AXIS 80
69+
#define AS5011_MIN_AXIS (-80)
70+
#define AS5011_FUZZ 8
71+
#define AS5011_FLAT 40
72+
73+
struct as5011_device {
74+
struct input_dev *input_dev;
75+
struct i2c_client *i2c_client;
76+
unsigned int button_gpio;
77+
unsigned int button_irq;
78+
unsigned int axis_irq;
79+
};
80+
81+
static int as5011_i2c_write(struct i2c_client *client,
82+
uint8_t aregaddr,
83+
uint8_t avalue)
84+
{
85+
uint8_t data[2] = { aregaddr, avalue };
86+
struct i2c_msg msg = {
87+
client->addr, I2C_M_IGNORE_NAK, 2, (uint8_t *)data
88+
};
89+
int error;
90+
91+
error = i2c_transfer(client->adapter, &msg, 1);
92+
return error < 0 ? error : 0;
93+
}
94+
95+
static int as5011_i2c_read(struct i2c_client *client,
96+
uint8_t aregaddr, signed char *value)
97+
{
98+
uint8_t data[2] = { aregaddr };
99+
struct i2c_msg msg_set[2] = {
100+
{ client->addr, I2C_M_REV_DIR_ADDR, 1, (uint8_t *)data },
101+
{ client->addr, I2C_M_RD | I2C_M_NOSTART, 1, (uint8_t *)data }
102+
};
103+
int error;
104+
105+
error = i2c_transfer(client->adapter, msg_set, 2);
106+
if (error < 0)
107+
return error;
108+
109+
*value = data[0] & 0x80 ? -1 * (1 + ~data[0]) : data[0];
110+
return 0;
111+
}
112+
113+
static irqreturn_t as5011_button_interrupt(int irq, void *dev_id)
114+
{
115+
struct as5011_device *as5011 = dev_id;
116+
int val = gpio_get_value_cansleep(as5011->button_gpio);
117+
118+
input_report_key(as5011->input_dev, BTN_JOYSTICK, !val);
119+
input_sync(as5011->input_dev);
120+
121+
return IRQ_HANDLED;
122+
}
123+
124+
static irqreturn_t as5011_axis_interrupt(int irq, void *dev_id)
125+
{
126+
struct as5011_device *as5011 = dev_id;
127+
int error;
128+
signed char x, y;
129+
130+
error = as5011_i2c_read(as5011->i2c_client, AS5011_X_RES_INT, &x);
131+
if (error < 0)
132+
goto out;
133+
134+
error = as5011_i2c_read(as5011->i2c_client, AS5011_Y_RES_INT, &y);
135+
if (error < 0)
136+
goto out;
137+
138+
input_report_abs(as5011->input_dev, ABS_X, x);
139+
input_report_abs(as5011->input_dev, ABS_Y, y);
140+
input_sync(as5011->input_dev);
141+
142+
out:
143+
return IRQ_HANDLED;
144+
}
145+
146+
static int __devinit as5011_configure_chip(struct as5011_device *as5011,
147+
const struct as5011_platform_data *plat_dat)
148+
{
149+
struct i2c_client *client = as5011->i2c_client;
150+
int error;
151+
signed char value;
152+
153+
/* chip soft reset */
154+
error = as5011_i2c_write(client, AS5011_CTRL1,
155+
AS5011_CTRL1_SOFT_RST);
156+
if (error < 0) {
157+
dev_err(&client->dev, "Soft reset failed\n");
158+
return error;
159+
}
160+
161+
mdelay(10);
162+
163+
error = as5011_i2c_write(client, AS5011_CTRL1,
164+
AS5011_CTRL1_LP_PULSED |
165+
AS5011_CTRL1_LP_ACTIVE |
166+
AS5011_CTRL1_INT_ACT_EN);
167+
if (error < 0) {
168+
dev_err(&client->dev, "Power config failed\n");
169+
return error;
170+
}
171+
172+
error = as5011_i2c_write(client, AS5011_CTRL2,
173+
AS5011_CTRL2_INV_SPINNING);
174+
if (error < 0) {
175+
dev_err(&client->dev, "Can't invert spinning\n");
176+
return error;
177+
}
178+
179+
/* write threshold */
180+
error = as5011_i2c_write(client, AS5011_XP, plat_dat->xp);
181+
if (error < 0) {
182+
dev_err(&client->dev, "Can't write threshold\n");
183+
return error;
184+
}
185+
186+
error = as5011_i2c_write(client, AS5011_XN, plat_dat->xn);
187+
if (error < 0) {
188+
dev_err(&client->dev, "Can't write threshold\n");
189+
return error;
190+
}
191+
192+
error = as5011_i2c_write(client, AS5011_YP, plat_dat->yp);
193+
if (error < 0) {
194+
dev_err(&client->dev, "Can't write threshold\n");
195+
return error;
196+
}
197+
198+
error = as5011_i2c_write(client, AS5011_YN, plat_dat->yn);
199+
if (error < 0) {
200+
dev_err(&client->dev, "Can't write threshold\n");
201+
return error;
202+
}
203+
204+
/* to free irq gpio in chip */
205+
error = as5011_i2c_read(client, AS5011_X_RES_INT, &value);
206+
if (error < 0) {
207+
dev_err(&client->dev, "Can't read i2c X resolution value\n");
208+
return error;
209+
}
210+
211+
return 0;
212+
}
213+
214+
static int __devinit as5011_probe(struct i2c_client *client,
215+
const struct i2c_device_id *id)
216+
{
217+
const struct as5011_platform_data *plat_data;
218+
struct as5011_device *as5011;
219+
struct input_dev *input_dev;
220+
int irq;
221+
int error;
222+
223+
plat_data = client->dev.platform_data;
224+
if (!plat_data)
225+
return -EINVAL;
226+
227+
if (!plat_data->axis_irq) {
228+
dev_err(&client->dev, "No axis IRQ?\n");
229+
return -EINVAL;
230+
}
231+
232+
if (!i2c_check_functionality(client->adapter,
233+
I2C_FUNC_PROTOCOL_MANGLING)) {
234+
dev_err(&client->dev,
235+
"need i2c bus that supports protocol mangling\n");
236+
return -ENODEV;
237+
}
238+
239+
as5011 = kmalloc(sizeof(struct as5011_device), GFP_KERNEL);
240+
input_dev = input_allocate_device();
241+
if (!as5011 || !input_dev) {
242+
dev_err(&client->dev,
243+
"Can't allocate memory for device structure\n");
244+
error = -ENOMEM;
245+
goto err_free_mem;
246+
}
247+
248+
as5011->i2c_client = client;
249+
as5011->input_dev = input_dev;
250+
as5011->button_gpio = plat_data->button_gpio;
251+
as5011->axis_irq = plat_data->axis_irq;
252+
253+
input_dev->name = "Austria Microsystem as5011 joystick";
254+
input_dev->id.bustype = BUS_I2C;
255+
input_dev->dev.parent = &client->dev;
256+
257+
__set_bit(EV_KEY, input_dev->evbit);
258+
__set_bit(EV_ABS, input_dev->evbit);
259+
__set_bit(BTN_JOYSTICK, input_dev->keybit);
260+
261+
input_set_abs_params(input_dev, ABS_X,
262+
AS5011_MIN_AXIS, AS5011_MAX_AXIS, AS5011_FUZZ, AS5011_FLAT);
263+
input_set_abs_params(as5011->input_dev, ABS_Y,
264+
AS5011_MIN_AXIS, AS5011_MAX_AXIS, AS5011_FUZZ, AS5011_FLAT);
265+
266+
error = gpio_request(as5011->button_gpio, "AS5011 button");
267+
if (error < 0) {
268+
dev_err(&client->dev, "Failed to request button gpio\n");
269+
goto err_free_mem;
270+
}
271+
272+
irq = gpio_to_irq(as5011->button_gpio);
273+
if (irq < 0) {
274+
dev_err(&client->dev,
275+
"Failed to get irq number for button gpio\n");
276+
goto err_free_button_gpio;
277+
}
278+
279+
as5011->button_irq = irq;
280+
281+
error = request_threaded_irq(as5011->button_irq,
282+
NULL, as5011_button_interrupt,
283+
IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
284+
"as5011_button", as5011);
285+
if (error < 0) {
286+
dev_err(&client->dev,
287+
"Can't allocate button irq %d\n", as5011->button_irq);
288+
goto err_free_button_gpio;
289+
}
290+
291+
error = as5011_configure_chip(as5011, plat_data);
292+
if (error)
293+
goto err_free_button_irq;
294+
295+
error = request_threaded_irq(as5011->axis_irq, NULL,
296+
as5011_axis_interrupt,
297+
plat_data->axis_irqflags,
298+
"as5011_joystick", as5011);
299+
if (error) {
300+
dev_err(&client->dev,
301+
"Can't allocate axis irq %d\n", plat_data->axis_irq);
302+
goto err_free_button_irq;
303+
}
304+
305+
error = input_register_device(as5011->input_dev);
306+
if (error) {
307+
dev_err(&client->dev, "Failed to register input device\n");
308+
goto err_free_axis_irq;
309+
}
310+
311+
i2c_set_clientdata(client, as5011);
312+
313+
return 0;
314+
315+
err_free_axis_irq:
316+
free_irq(as5011->axis_irq, as5011);
317+
err_free_button_irq:
318+
free_irq(as5011->button_irq, as5011);
319+
err_free_button_gpio:
320+
gpio_free(as5011->button_gpio);
321+
err_free_mem:
322+
input_free_device(input_dev);
323+
kfree(as5011);
324+
325+
return error;
326+
}
327+
328+
static int __devexit as5011_remove(struct i2c_client *client)
329+
{
330+
struct as5011_device *as5011 = i2c_get_clientdata(client);
331+
332+
free_irq(as5011->axis_irq, as5011);
333+
free_irq(as5011->button_irq, as5011);
334+
gpio_free(as5011->button_gpio);
335+
336+
input_unregister_device(as5011->input_dev);
337+
kfree(as5011);
338+
339+
return 0;
340+
}
341+
342+
static const struct i2c_device_id as5011_id[] = {
343+
{ MODULE_DEVICE_ALIAS, 0 },
344+
{ }
345+
};
346+
MODULE_DEVICE_TABLE(i2c, as5011_id);
347+
348+
static struct i2c_driver as5011_driver = {
349+
.driver = {
350+
.name = "as5011",
351+
},
352+
.probe = as5011_probe,
353+
.remove = __devexit_p(as5011_remove),
354+
.id_table = as5011_id,
355+
};
356+
357+
static int __init as5011_init(void)
358+
{
359+
return i2c_add_driver(&as5011_driver);
360+
}
361+
module_init(as5011_init);
362+
363+
static void __exit as5011_exit(void)
364+
{
365+
i2c_del_driver(&as5011_driver);
366+
}
367+
module_exit(as5011_exit);

0 commit comments

Comments
 (0)