diff options
author | Tatsunosuke Tobita <tobita.tatsunosuke@wacom.co.jp> | 2012-03-25 20:23:19 -0400 |
---|---|---|
committer | Dmitry Torokhov <dmitry.torokhov@gmail.com> | 2012-04-04 12:24:23 -0400 |
commit | 5a966261c0dfb836f54444ff3893638802183cac (patch) | |
tree | 335a2475bec51b2eea1b3a503d4838c539c2ee97 /drivers/input/touchscreen | |
parent | 271002ca7956e5ef140c02af1bd8e93063924ec6 (diff) |
Input: add support for Wacom Stylus device with I2C interface
This adds support for Wacom Stylus device with I2C interface.
[Dan Carpenter <dan.carpenter@oracle.com>: fix NULL-pointer dereference
in error handling path.]
Signed-off-by: Tatsunosuke Tobita <tobita.tatsunosuke@wacom.co.jp>
Signed-off-by: Dmitry Torokhov <dtor@mail.ru>
Diffstat (limited to 'drivers/input/touchscreen')
-rw-r--r-- | drivers/input/touchscreen/Kconfig | 12 | ||||
-rw-r--r-- | drivers/input/touchscreen/Makefile | 1 | ||||
-rw-r--r-- | drivers/input/touchscreen/wacom_i2c.c | 315 |
3 files changed, 328 insertions, 0 deletions
diff --git a/drivers/input/touchscreen/Kconfig b/drivers/input/touchscreen/Kconfig index 97b31a0e0525..c5eb2b925be6 100644 --- a/drivers/input/touchscreen/Kconfig +++ b/drivers/input/touchscreen/Kconfig | |||
@@ -306,6 +306,18 @@ config TOUCHSCREEN_WACOM_W8001 | |||
306 | To compile this driver as a module, choose M here: the | 306 | To compile this driver as a module, choose M here: the |
307 | module will be called wacom_w8001. | 307 | module will be called wacom_w8001. |
308 | 308 | ||
309 | config TOUCHSCREEN_WACOM_I2C | ||
310 | tristate "Wacom Tablet support (I2C)" | ||
311 | depends on I2C | ||
312 | help | ||
313 | Say Y here if you want to use the I2C version of the Wacom | ||
314 | Pen Tablet. | ||
315 | |||
316 | If unsure, say N. | ||
317 | |||
318 | To compile this driver as a module, choose M here: the module | ||
319 | will be called wacom_i2c. | ||
320 | |||
309 | config TOUCHSCREEN_LPC32XX | 321 | config TOUCHSCREEN_LPC32XX |
310 | tristate "LPC32XX touchscreen controller" | 322 | tristate "LPC32XX touchscreen controller" |
311 | depends on ARCH_LPC32XX | 323 | depends on ARCH_LPC32XX |
diff --git a/drivers/input/touchscreen/Makefile b/drivers/input/touchscreen/Makefile index 3d5cf8cbf89c..7cca7ddb4643 100644 --- a/drivers/input/touchscreen/Makefile +++ b/drivers/input/touchscreen/Makefile | |||
@@ -59,6 +59,7 @@ obj-$(CONFIG_TOUCHSCREEN_TSC2005) += tsc2005.o | |||
59 | obj-$(CONFIG_TOUCHSCREEN_TSC2007) += tsc2007.o | 59 | obj-$(CONFIG_TOUCHSCREEN_TSC2007) += tsc2007.o |
60 | obj-$(CONFIG_TOUCHSCREEN_UCB1400) += ucb1400_ts.o | 60 | obj-$(CONFIG_TOUCHSCREEN_UCB1400) += ucb1400_ts.o |
61 | obj-$(CONFIG_TOUCHSCREEN_WACOM_W8001) += wacom_w8001.o | 61 | obj-$(CONFIG_TOUCHSCREEN_WACOM_W8001) += wacom_w8001.o |
62 | obj-$(CONFIG_TOUCHSCREEN_WACOM_I2C) += wacom_i2c.o | ||
62 | obj-$(CONFIG_TOUCHSCREEN_WM831X) += wm831x-ts.o | 63 | obj-$(CONFIG_TOUCHSCREEN_WM831X) += wm831x-ts.o |
63 | obj-$(CONFIG_TOUCHSCREEN_WM97XX) += wm97xx-ts.o | 64 | obj-$(CONFIG_TOUCHSCREEN_WM97XX) += wm97xx-ts.o |
64 | wm97xx-ts-$(CONFIG_TOUCHSCREEN_WM9705) += wm9705.o | 65 | wm97xx-ts-$(CONFIG_TOUCHSCREEN_WM9705) += wm9705.o |
diff --git a/drivers/input/touchscreen/wacom_i2c.c b/drivers/input/touchscreen/wacom_i2c.c new file mode 100644 index 000000000000..b8ca4a6bc91a --- /dev/null +++ b/drivers/input/touchscreen/wacom_i2c.c | |||
@@ -0,0 +1,315 @@ | |||
1 | /* | ||
2 | * Wacom Penabled Driver for I2C | ||
3 | * | ||
4 | * Copyright (c) 2011 Tatsunosuke Tobita, Wacom. | ||
5 | * <tobita.tatsunosuke@wacom.co.jp> | ||
6 | * | ||
7 | * This program is free software; you can redistribute it | ||
8 | * and/or modify it under the terms of the GNU General | ||
9 | * Public License as published by the Free Software | ||
10 | * Foundation; either version of 2 of the License, | ||
11 | * or (at your option) any later version. | ||
12 | */ | ||
13 | |||
14 | #include <linux/module.h> | ||
15 | #include <linux/input.h> | ||
16 | #include <linux/i2c.h> | ||
17 | #include <linux/slab.h> | ||
18 | #include <linux/irq.h> | ||
19 | #include <linux/interrupt.h> | ||
20 | #include <linux/gpio.h> | ||
21 | #include <asm/unaligned.h> | ||
22 | |||
23 | #define WACOM_CMD_QUERY0 0x04 | ||
24 | #define WACOM_CMD_QUERY1 0x00 | ||
25 | #define WACOM_CMD_QUERY2 0x33 | ||
26 | #define WACOM_CMD_QUERY3 0x02 | ||
27 | #define WACOM_CMD_THROW0 0x05 | ||
28 | #define WACOM_CMD_THROW1 0x00 | ||
29 | #define WACOM_QUERY_SIZE 19 | ||
30 | #define WACOM_RETRY_CNT 100 | ||
31 | |||
32 | struct wacom_features { | ||
33 | int x_max; | ||
34 | int y_max; | ||
35 | int pressure_max; | ||
36 | char fw_version; | ||
37 | }; | ||
38 | |||
39 | struct wacom_i2c { | ||
40 | struct i2c_client *client; | ||
41 | struct input_dev *input; | ||
42 | unsigned int gpio; | ||
43 | u8 data[WACOM_QUERY_SIZE]; | ||
44 | }; | ||
45 | |||
46 | static int wacom_query_device(struct i2c_client *client, | ||
47 | struct wacom_features *features) | ||
48 | { | ||
49 | int ret; | ||
50 | u8 cmd1[] = { WACOM_CMD_QUERY0, WACOM_CMD_QUERY1, | ||
51 | WACOM_CMD_QUERY2, WACOM_CMD_QUERY3 }; | ||
52 | u8 cmd2[] = { WACOM_CMD_THROW0, WACOM_CMD_THROW1 }; | ||
53 | u8 data[WACOM_QUERY_SIZE]; | ||
54 | struct i2c_msg msgs[] = { | ||
55 | { | ||
56 | .addr = client->addr, | ||
57 | .flags = 0, | ||
58 | .len = sizeof(cmd1), | ||
59 | .buf = cmd1, | ||
60 | }, | ||
61 | { | ||
62 | .addr = client->addr, | ||
63 | .flags = 0, | ||
64 | .len = sizeof(cmd2), | ||
65 | .buf = cmd2, | ||
66 | }, | ||
67 | { | ||
68 | .addr = client->addr, | ||
69 | .flags = I2C_M_RD, | ||
70 | .len = sizeof(data), | ||
71 | .buf = data, | ||
72 | }, | ||
73 | }; | ||
74 | |||
75 | ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs)); | ||
76 | if (ret < 0) | ||
77 | return ret; | ||
78 | if (ret != ARRAY_SIZE(msgs)) | ||
79 | return -EIO; | ||
80 | |||
81 | features->x_max = get_unaligned_le16(&data[3]); | ||
82 | features->y_max = get_unaligned_le16(&data[5]); | ||
83 | features->pressure_max = get_unaligned_le16(&data[11]); | ||
84 | features->fw_version = get_unaligned_le16(&data[13]); | ||
85 | |||
86 | dev_dbg(&client->dev, | ||
87 | "x_max:%d, y_max:%d, pressure:%d, fw:%d\n", | ||
88 | features->x_max, features->y_max, | ||
89 | features->pressure_max, features->fw_version); | ||
90 | |||
91 | return 0; | ||
92 | } | ||
93 | |||
94 | static int wacom_i2c_fetch_data(struct wacom_i2c *wac_i2c) | ||
95 | { | ||
96 | int retries = 0; | ||
97 | int ret; | ||
98 | |||
99 | do { | ||
100 | ret = i2c_master_recv(wac_i2c->client, | ||
101 | wac_i2c->data, sizeof(wac_i2c->data)); | ||
102 | } while (gpio_get_value(wac_i2c->gpio) == 0 && | ||
103 | retries++ < WACOM_RETRY_CNT); | ||
104 | |||
105 | if (retries >= WACOM_RETRY_CNT) | ||
106 | ret = -EIO; | ||
107 | |||
108 | return ret < 0 ? ret : 0; | ||
109 | } | ||
110 | |||
111 | static irqreturn_t wacom_i2c_irq(int irq, void *dev_id) | ||
112 | { | ||
113 | struct wacom_i2c *wac_i2c = dev_id; | ||
114 | struct input_dev *input = wac_i2c->input; | ||
115 | u8 *data = wac_i2c->data; | ||
116 | unsigned int x, y, pressure; | ||
117 | unsigned char tsw, f1, f2, ers; | ||
118 | int error; | ||
119 | |||
120 | error = wacom_i2c_fetch_data(wac_i2c); | ||
121 | if (error) | ||
122 | goto out; | ||
123 | |||
124 | tsw = data[3] & 0x01; | ||
125 | ers = data[3] & 0x04; | ||
126 | f1 = data[3] & 0x02; | ||
127 | f2 = data[3] & 0x10; | ||
128 | x = le16_to_cpup((__le16 *)&data[4]); | ||
129 | y = le16_to_cpup((__le16 *)&data[6]); | ||
130 | pressure = le16_to_cpup((__le16 *)&data[8]); | ||
131 | |||
132 | input_report_key(input, BTN_TOUCH, tsw || ers); | ||
133 | input_report_key(input, BTN_TOOL_PEN, tsw); | ||
134 | input_report_key(input, BTN_TOOL_RUBBER, ers); | ||
135 | input_report_key(input, BTN_STYLUS, f1); | ||
136 | input_report_key(input, BTN_STYLUS2, f2); | ||
137 | input_report_abs(input, ABS_X, x); | ||
138 | input_report_abs(input, ABS_Y, y); | ||
139 | input_report_abs(input, ABS_PRESSURE, pressure); | ||
140 | input_sync(input); | ||
141 | |||
142 | out: | ||
143 | return IRQ_HANDLED; | ||
144 | } | ||
145 | |||
146 | static int wacom_i2c_open(struct input_dev *dev) | ||
147 | { | ||
148 | struct wacom_i2c *wac_i2c = input_get_drvdata(dev); | ||
149 | struct i2c_client *client = wac_i2c->client; | ||
150 | int error; | ||
151 | |||
152 | /* Clear the device buffer */ | ||
153 | error = wacom_i2c_fetch_data(wac_i2c); | ||
154 | if (error) | ||
155 | return error; | ||
156 | |||
157 | enable_irq(client->irq); | ||
158 | |||
159 | return 0; | ||
160 | } | ||
161 | |||
162 | static void wacom_i2c_close(struct input_dev *dev) | ||
163 | { | ||
164 | struct wacom_i2c *wac_i2c = input_get_drvdata(dev); | ||
165 | struct i2c_client *client = wac_i2c->client; | ||
166 | |||
167 | disable_irq(client->irq); | ||
168 | } | ||
169 | |||
170 | static int __devinit wacom_i2c_probe(struct i2c_client *client, | ||
171 | const struct i2c_device_id *id) | ||
172 | { | ||
173 | struct wacom_i2c *wac_i2c; | ||
174 | struct input_dev *input; | ||
175 | struct wacom_features features; | ||
176 | int gpio; | ||
177 | int error; | ||
178 | |||
179 | if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) { | ||
180 | dev_err(&client->dev, "i2c_check_functionality error\n"); | ||
181 | return -EIO; | ||
182 | } | ||
183 | |||
184 | gpio = irq_to_gpio(client->irq); | ||
185 | if (gpio < 0) { | ||
186 | error = gpio; | ||
187 | dev_err(&client->dev, | ||
188 | "irq_to_gpio() failed, error: %d\n", error); | ||
189 | return error; | ||
190 | } | ||
191 | |||
192 | error = wacom_query_device(client, &features); | ||
193 | if (error) | ||
194 | return error; | ||
195 | |||
196 | wac_i2c = kzalloc(sizeof(*wac_i2c), GFP_KERNEL); | ||
197 | input = input_allocate_device(); | ||
198 | if (!wac_i2c || !input) { | ||
199 | error = -ENOMEM; | ||
200 | goto err_free_mem; | ||
201 | } | ||
202 | |||
203 | wac_i2c->client = client; | ||
204 | wac_i2c->input = input; | ||
205 | wac_i2c->gpio = gpio; | ||
206 | |||
207 | input->name = "Wacom I2C Digitizer"; | ||
208 | input->id.bustype = BUS_I2C; | ||
209 | input->id.vendor = 0x56a; | ||
210 | input->id.version = features.fw_version; | ||
211 | input->dev.parent = &client->dev; | ||
212 | input->open = wacom_i2c_open; | ||
213 | input->close = wacom_i2c_close; | ||
214 | |||
215 | input->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); | ||
216 | |||
217 | __set_bit(BTN_TOOL_PEN, input->keybit); | ||
218 | __set_bit(BTN_TOOL_RUBBER, input->keybit); | ||
219 | __set_bit(BTN_STYLUS, input->keybit); | ||
220 | __set_bit(BTN_STYLUS2, input->keybit); | ||
221 | __set_bit(BTN_TOUCH, input->keybit); | ||
222 | |||
223 | input_set_abs_params(input, ABS_X, 0, features.x_max, 0, 0); | ||
224 | input_set_abs_params(input, ABS_Y, 0, features.y_max, 0, 0); | ||
225 | input_set_abs_params(input, ABS_PRESSURE, | ||
226 | 0, features.pressure_max, 0, 0); | ||
227 | |||
228 | input_set_drvdata(input, wac_i2c); | ||
229 | |||
230 | error = request_threaded_irq(client->irq, NULL, wacom_i2c_irq, | ||
231 | IRQF_TRIGGER_FALLING, | ||
232 | "wacom_i2c", wac_i2c); | ||
233 | if (error) { | ||
234 | dev_err(&client->dev, | ||
235 | "Failed to enable IRQ, error: %d\n", error); | ||
236 | goto err_free_mem; | ||
237 | } | ||
238 | |||
239 | /* Disable the IRQ, we'll enable it in wac_i2c_open() */ | ||
240 | disable_irq(client->irq); | ||
241 | |||
242 | error = input_register_device(wac_i2c->input); | ||
243 | if (error) { | ||
244 | dev_err(&client->dev, | ||
245 | "Failed to register input device, error: %d\n", error); | ||
246 | goto err_free_irq; | ||
247 | } | ||
248 | |||
249 | i2c_set_clientdata(client, wac_i2c); | ||
250 | return 0; | ||
251 | |||
252 | err_free_irq: | ||
253 | free_irq(client->irq, wac_i2c); | ||
254 | err_free_mem: | ||
255 | input_free_device(input); | ||
256 | kfree(wac_i2c); | ||
257 | |||
258 | return error; | ||
259 | } | ||
260 | |||
261 | static int __devexit wacom_i2c_remove(struct i2c_client *client) | ||
262 | { | ||
263 | struct wacom_i2c *wac_i2c = i2c_get_clientdata(client); | ||
264 | |||
265 | free_irq(client->irq, wac_i2c); | ||
266 | input_unregister_device(wac_i2c->input); | ||
267 | kfree(wac_i2c); | ||
268 | |||
269 | return 0; | ||
270 | } | ||
271 | |||
272 | #ifdef CONFIG_PM_SLEEP | ||
273 | static int wacom_i2c_suspend(struct device *dev) | ||
274 | { | ||
275 | struct i2c_client *client = to_i2c_client(dev); | ||
276 | |||
277 | disable_irq(client->irq); | ||
278 | |||
279 | return 0; | ||
280 | } | ||
281 | |||
282 | static int wacom_i2c_resume(struct device *dev) | ||
283 | { | ||
284 | struct i2c_client *client = to_i2c_client(dev); | ||
285 | |||
286 | enable_irq(client->irq); | ||
287 | |||
288 | return 0; | ||
289 | } | ||
290 | #endif | ||
291 | |||
292 | static SIMPLE_DEV_PM_OPS(wacom_i2c_pm, wacom_i2c_suspend, wacom_i2c_resume); | ||
293 | |||
294 | static const struct i2c_device_id wacom_i2c_id[] = { | ||
295 | { "WAC_I2C_EMR", 0 }, | ||
296 | { }, | ||
297 | }; | ||
298 | MODULE_DEVICE_TABLE(i2c, wacom_i2c_id); | ||
299 | |||
300 | static struct i2c_driver wacom_i2c_driver = { | ||
301 | .driver = { | ||
302 | .name = "wacom_i2c", | ||
303 | .owner = THIS_MODULE, | ||
304 | .pm = &wacom_i2c_pm, | ||
305 | }, | ||
306 | |||
307 | .probe = wacom_i2c_probe, | ||
308 | .remove = __devexit_p(wacom_i2c_remove), | ||
309 | .id_table = wacom_i2c_id, | ||
310 | }; | ||
311 | module_i2c_driver(wacom_i2c_driver); | ||
312 | |||
313 | MODULE_AUTHOR("Tatsunosuke Tobita <tobita.tatsunosuke@wacom.co.jp>"); | ||
314 | MODULE_DESCRIPTION("WACOM EMR I2C Driver"); | ||
315 | MODULE_LICENSE("GPL"); | ||