OMAPDSS: VENC: fix NULL pointer dereference in DSS2 VENC sysfs debug attr on OMAP4
[zen-stable.git] / drivers / input / joystick / as5011.c
blob6d6e7418dc21d206f54f3c1ebd0770ebaf9388d4
1 /*
2 * Copyright (c) 2010, 2011 Fabien Marteau <fabien.marteau@armadeus.com>
3 * Sponsored by ARMadeus Systems
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.
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.
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
19 * Driver for Austria Microsystems joysticks AS5011
21 * TODO:
22 * - Power on the chip when open() and power down when close()
23 * - Manage power mode
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 #include <linux/module.h>
35 #define DRIVER_DESC "Driver for Austria Microsystems AS5011 joystick"
36 #define MODULE_DEVICE_ALIAS "as5011"
38 MODULE_AUTHOR("Fabien Marteau <fabien.marteau@armadeus.com>");
39 MODULE_DESCRIPTION(DRIVER_DESC);
40 MODULE_LICENSE("GPL");
42 /* registers */
43 #define AS5011_CTRL1 0x76
44 #define AS5011_CTRL2 0x75
45 #define AS5011_XP 0x43
46 #define AS5011_XN 0x44
47 #define AS5011_YP 0x53
48 #define AS5011_YN 0x54
49 #define AS5011_X_REG 0x41
50 #define AS5011_Y_REG 0x42
51 #define AS5011_X_RES_INT 0x51
52 #define AS5011_Y_RES_INT 0x52
54 /* CTRL1 bits */
55 #define AS5011_CTRL1_LP_PULSED 0x80
56 #define AS5011_CTRL1_LP_ACTIVE 0x40
57 #define AS5011_CTRL1_LP_CONTINUE 0x20
58 #define AS5011_CTRL1_INT_WUP_EN 0x10
59 #define AS5011_CTRL1_INT_ACT_EN 0x08
60 #define AS5011_CTRL1_EXT_CLK_EN 0x04
61 #define AS5011_CTRL1_SOFT_RST 0x02
62 #define AS5011_CTRL1_DATA_VALID 0x01
64 /* CTRL2 bits */
65 #define AS5011_CTRL2_EXT_SAMPLE_EN 0x08
66 #define AS5011_CTRL2_RC_BIAS_ON 0x04
67 #define AS5011_CTRL2_INV_SPINNING 0x02
69 #define AS5011_MAX_AXIS 80
70 #define AS5011_MIN_AXIS (-80)
71 #define AS5011_FUZZ 8
72 #define AS5011_FLAT 40
74 struct as5011_device {
75 struct input_dev *input_dev;
76 struct i2c_client *i2c_client;
77 unsigned int button_gpio;
78 unsigned int button_irq;
79 unsigned int axis_irq;
82 static int as5011_i2c_write(struct i2c_client *client,
83 uint8_t aregaddr,
84 uint8_t avalue)
86 uint8_t data[2] = { aregaddr, avalue };
87 struct i2c_msg msg = {
88 client->addr, I2C_M_IGNORE_NAK, 2, (uint8_t *)data
90 int error;
92 error = i2c_transfer(client->adapter, &msg, 1);
93 return error < 0 ? error : 0;
96 static int as5011_i2c_read(struct i2c_client *client,
97 uint8_t aregaddr, signed char *value)
99 uint8_t data[2] = { aregaddr };
100 struct i2c_msg msg_set[2] = {
101 { client->addr, I2C_M_REV_DIR_ADDR, 1, (uint8_t *)data },
102 { client->addr, I2C_M_RD | I2C_M_NOSTART, 1, (uint8_t *)data }
104 int error;
106 error = i2c_transfer(client->adapter, msg_set, 2);
107 if (error < 0)
108 return error;
110 *value = data[0] & 0x80 ? -1 * (1 + ~data[0]) : data[0];
111 return 0;
114 static irqreturn_t as5011_button_interrupt(int irq, void *dev_id)
116 struct as5011_device *as5011 = dev_id;
117 int val = gpio_get_value_cansleep(as5011->button_gpio);
119 input_report_key(as5011->input_dev, BTN_JOYSTICK, !val);
120 input_sync(as5011->input_dev);
122 return IRQ_HANDLED;
125 static irqreturn_t as5011_axis_interrupt(int irq, void *dev_id)
127 struct as5011_device *as5011 = dev_id;
128 int error;
129 signed char x, y;
131 error = as5011_i2c_read(as5011->i2c_client, AS5011_X_RES_INT, &x);
132 if (error < 0)
133 goto out;
135 error = as5011_i2c_read(as5011->i2c_client, AS5011_Y_RES_INT, &y);
136 if (error < 0)
137 goto out;
139 input_report_abs(as5011->input_dev, ABS_X, x);
140 input_report_abs(as5011->input_dev, ABS_Y, y);
141 input_sync(as5011->input_dev);
143 out:
144 return IRQ_HANDLED;
147 static int __devinit as5011_configure_chip(struct as5011_device *as5011,
148 const struct as5011_platform_data *plat_dat)
150 struct i2c_client *client = as5011->i2c_client;
151 int error;
152 signed char value;
154 /* chip soft reset */
155 error = as5011_i2c_write(client, AS5011_CTRL1,
156 AS5011_CTRL1_SOFT_RST);
157 if (error < 0) {
158 dev_err(&client->dev, "Soft reset failed\n");
159 return error;
162 mdelay(10);
164 error = as5011_i2c_write(client, AS5011_CTRL1,
165 AS5011_CTRL1_LP_PULSED |
166 AS5011_CTRL1_LP_ACTIVE |
167 AS5011_CTRL1_INT_ACT_EN);
168 if (error < 0) {
169 dev_err(&client->dev, "Power config failed\n");
170 return error;
173 error = as5011_i2c_write(client, AS5011_CTRL2,
174 AS5011_CTRL2_INV_SPINNING);
175 if (error < 0) {
176 dev_err(&client->dev, "Can't invert spinning\n");
177 return error;
180 /* write threshold */
181 error = as5011_i2c_write(client, AS5011_XP, plat_dat->xp);
182 if (error < 0) {
183 dev_err(&client->dev, "Can't write threshold\n");
184 return error;
187 error = as5011_i2c_write(client, AS5011_XN, plat_dat->xn);
188 if (error < 0) {
189 dev_err(&client->dev, "Can't write threshold\n");
190 return error;
193 error = as5011_i2c_write(client, AS5011_YP, plat_dat->yp);
194 if (error < 0) {
195 dev_err(&client->dev, "Can't write threshold\n");
196 return error;
199 error = as5011_i2c_write(client, AS5011_YN, plat_dat->yn);
200 if (error < 0) {
201 dev_err(&client->dev, "Can't write threshold\n");
202 return error;
205 /* to free irq gpio in chip */
206 error = as5011_i2c_read(client, AS5011_X_RES_INT, &value);
207 if (error < 0) {
208 dev_err(&client->dev, "Can't read i2c X resolution value\n");
209 return error;
212 return 0;
215 static int __devinit as5011_probe(struct i2c_client *client,
216 const struct i2c_device_id *id)
218 const struct as5011_platform_data *plat_data;
219 struct as5011_device *as5011;
220 struct input_dev *input_dev;
221 int irq;
222 int error;
224 plat_data = client->dev.platform_data;
225 if (!plat_data)
226 return -EINVAL;
228 if (!plat_data->axis_irq) {
229 dev_err(&client->dev, "No axis IRQ?\n");
230 return -EINVAL;
233 if (!i2c_check_functionality(client->adapter,
234 I2C_FUNC_PROTOCOL_MANGLING)) {
235 dev_err(&client->dev,
236 "need i2c bus that supports protocol mangling\n");
237 return -ENODEV;
240 as5011 = kmalloc(sizeof(struct as5011_device), GFP_KERNEL);
241 input_dev = input_allocate_device();
242 if (!as5011 || !input_dev) {
243 dev_err(&client->dev,
244 "Can't allocate memory for device structure\n");
245 error = -ENOMEM;
246 goto err_free_mem;
249 as5011->i2c_client = client;
250 as5011->input_dev = input_dev;
251 as5011->button_gpio = plat_data->button_gpio;
252 as5011->axis_irq = plat_data->axis_irq;
254 input_dev->name = "Austria Microsystem as5011 joystick";
255 input_dev->id.bustype = BUS_I2C;
256 input_dev->dev.parent = &client->dev;
258 __set_bit(EV_KEY, input_dev->evbit);
259 __set_bit(EV_ABS, input_dev->evbit);
260 __set_bit(BTN_JOYSTICK, input_dev->keybit);
262 input_set_abs_params(input_dev, ABS_X,
263 AS5011_MIN_AXIS, AS5011_MAX_AXIS, AS5011_FUZZ, AS5011_FLAT);
264 input_set_abs_params(as5011->input_dev, ABS_Y,
265 AS5011_MIN_AXIS, AS5011_MAX_AXIS, AS5011_FUZZ, AS5011_FLAT);
267 error = gpio_request(as5011->button_gpio, "AS5011 button");
268 if (error < 0) {
269 dev_err(&client->dev, "Failed to request button gpio\n");
270 goto err_free_mem;
273 irq = gpio_to_irq(as5011->button_gpio);
274 if (irq < 0) {
275 dev_err(&client->dev,
276 "Failed to get irq number for button gpio\n");
277 goto err_free_button_gpio;
280 as5011->button_irq = irq;
282 error = request_threaded_irq(as5011->button_irq,
283 NULL, as5011_button_interrupt,
284 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
285 "as5011_button", as5011);
286 if (error < 0) {
287 dev_err(&client->dev,
288 "Can't allocate button irq %d\n", as5011->button_irq);
289 goto err_free_button_gpio;
292 error = as5011_configure_chip(as5011, plat_data);
293 if (error)
294 goto err_free_button_irq;
296 error = request_threaded_irq(as5011->axis_irq, NULL,
297 as5011_axis_interrupt,
298 plat_data->axis_irqflags,
299 "as5011_joystick", as5011);
300 if (error) {
301 dev_err(&client->dev,
302 "Can't allocate axis irq %d\n", plat_data->axis_irq);
303 goto err_free_button_irq;
306 error = input_register_device(as5011->input_dev);
307 if (error) {
308 dev_err(&client->dev, "Failed to register input device\n");
309 goto err_free_axis_irq;
312 i2c_set_clientdata(client, as5011);
314 return 0;
316 err_free_axis_irq:
317 free_irq(as5011->axis_irq, as5011);
318 err_free_button_irq:
319 free_irq(as5011->button_irq, as5011);
320 err_free_button_gpio:
321 gpio_free(as5011->button_gpio);
322 err_free_mem:
323 input_free_device(input_dev);
324 kfree(as5011);
326 return error;
329 static int __devexit as5011_remove(struct i2c_client *client)
331 struct as5011_device *as5011 = i2c_get_clientdata(client);
333 free_irq(as5011->axis_irq, as5011);
334 free_irq(as5011->button_irq, as5011);
335 gpio_free(as5011->button_gpio);
337 input_unregister_device(as5011->input_dev);
338 kfree(as5011);
340 return 0;
343 static const struct i2c_device_id as5011_id[] = {
344 { MODULE_DEVICE_ALIAS, 0 },
347 MODULE_DEVICE_TABLE(i2c, as5011_id);
349 static struct i2c_driver as5011_driver = {
350 .driver = {
351 .name = "as5011",
353 .probe = as5011_probe,
354 .remove = __devexit_p(as5011_remove),
355 .id_table = as5011_id,
358 static int __init as5011_init(void)
360 return i2c_add_driver(&as5011_driver);
362 module_init(as5011_init);
364 static void __exit as5011_exit(void)
366 i2c_del_driver(&as5011_driver);
368 module_exit(as5011_exit);