1 /*
2 * Wacom Penabled Driver for I2C
3 *
4 * Copyright (c) 2011 - 2013 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 <linux/of_gpio.h>
22 #include <linux/delay.h>
23 #include <asm/unaligned.h>
24 #include <linux/regulator/driver.h>
25 #include <linux/regulator/consumer.h>
26 #include <linux/notifier.h>
27 #include "tp_suspend.h"
28
29 //#define ORIGIN_COORD
30
31 static int exchange_x_y_flag = 0;
32 static int revert_x_flag = 0;
33 static int revert_y_flag = 0;
34
35 static int screen_max_x = 20280;
36 static int screen_max_y = 13942;
37
38 #define WACOM_CMD_QUERY0 0x04
39 #define WACOM_CMD_QUERY1 0x00
40 #define WACOM_CMD_QUERY2 0x33
41 #define WACOM_CMD_QUERY3 0x02
42 #define WACOM_CMD_THROW0 0x05
43 #define WACOM_CMD_THROW1 0x00
44 #define WACOM_QUERY_SIZE 19
45
46 struct wacom_features {
47 int x_max;
48 int y_max;
49 int pressure_max;
50 char fw_version;
51 };
52
53 /*HID specific register*/
54 #define HID_DESC_REGISTER 1
55 #define COMM_REG 0x04
56 #define DATA_REG 0x05
57
58 typedef struct hid_descriptor {
59 u16 wHIDDescLength;
60 u16 bcdVersion;
61 u16 wReportDescLength;
62 u16 wReportDescRegister;
63 u16 wInputRegister;
64 u16 wMaxInputLength;
65 u16 wOutputRegister;
66 u16 wMaxOutputLength;
67 u16 wCommandRegister;
68 u16 wDataRegister;
69 u16 wVendorID;
70 u16 wProductID;
71 u16 wVersion;
72 u16 RESERVED_HIGH;
73 u16 RESERVED_LOW;
74 } HID_DESC;
75
76 struct wacom_i2c {
77 struct wacom_features *features;
78 struct i2c_client *client;
79 struct input_dev *input;
80 u8 data[WACOM_QUERY_SIZE];
81 bool prox;
82 int tool;
83 struct tp_device tp;
84 struct regulator *supply;
85 int irq_gpio;
86 int pen_detect_gpio;
87 int reset_gpio;
88 };
89
get_hid_desc(struct i2c_client * client,struct hid_descriptor * hid_desc)90 static int get_hid_desc(struct i2c_client *client,
91 struct hid_descriptor *hid_desc)
92 {
93 int ret = -1;
94 char cmd[] = {HID_DESC_REGISTER, 0x00};
95 struct i2c_msg msgs[] = {
96 {
97 .addr = client->addr,
98 .flags = 0,
99 .len = sizeof(cmd),
100 .buf = cmd,
101 },
102 {
103 .addr = client->addr,
104 .flags = I2C_M_RD,
105 .len = sizeof(HID_DESC),
106 .buf = (char *)hid_desc,
107 },
108 };
109
110 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
111 if (ret < 0)
112 return ret;
113 if (ret != ARRAY_SIZE(msgs))
114 return -EIO;
115
116 printk("******************************\n");
117 printk("wacom firmware vesrsion:0x%x\n", hid_desc->wVersion);
118 printk("******************************\n");
119
120 ret = 0;
121 //out:
122 return ret;
123 }
124
125
wacom_query_device(struct i2c_client * client,struct wacom_features * features)126 static int wacom_query_device(struct i2c_client *client,
127 struct wacom_features *features)
128 {
129 int ret;
130 u8 cmd1[] = { WACOM_CMD_QUERY0, WACOM_CMD_QUERY1,
131 WACOM_CMD_QUERY2, WACOM_CMD_QUERY3 };
132 u8 cmd2[] = { WACOM_CMD_THROW0, WACOM_CMD_THROW1 };
133 u8 data[WACOM_QUERY_SIZE];
134 struct i2c_msg msgs[] = {
135 {
136 .addr = client->addr,
137 .flags = 0,
138 .len = sizeof(cmd1),
139 .buf = cmd1,
140 },
141 {
142 .addr = client->addr,
143 .flags = 0,
144 .len = sizeof(cmd2),
145 .buf = cmd2,
146 },
147 {
148 .addr = client->addr,
149 .flags = I2C_M_RD,
150 .len = sizeof(data),
151 .buf = data,
152 },
153 };
154
155 ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
156 if (ret < 0)
157 return ret;
158 if (ret != ARRAY_SIZE(msgs))
159 return -EIO;
160
161 features->x_max = get_unaligned_le16(&data[3]);
162 features->y_max = get_unaligned_le16(&data[5]);
163 features->pressure_max = get_unaligned_le16(&data[11]);
164 features->fw_version = get_unaligned_le16(&data[13]);
165 printk("Wacom source screen x_max:%d, y_max:%d, pressure:%d, fw:%d\n",
166 features->x_max, features->y_max,
167 features->pressure_max, features->fw_version);
168
169 if (1 == exchange_x_y_flag) {
170 swap(features->x_max, features->y_max);
171 }
172 screen_max_x = features->x_max;
173 screen_max_y = features->y_max;
174 printk("Wacom desc screen x_max:%d, y_max:%d\n", features->x_max, features->y_max);
175
176 return 0;
177 }
178
wacom_i2c_irq(int irq,void * dev_id)179 static irqreturn_t wacom_i2c_irq(int irq, void *dev_id)
180 {
181 struct wacom_i2c *wac_i2c = dev_id;
182 struct input_dev *input = wac_i2c->input;
183 //struct wacom_features *features = wac_i2c->features;
184 u8 *data = wac_i2c->data;
185 unsigned int x, y, pressure;
186 unsigned char tsw, f1, f2, ers;
187 int error;
188
189 if (device_can_wakeup(&wac_i2c->client->dev))
190 pm_stay_awake(&wac_i2c->client->dev);
191 error = i2c_master_recv(wac_i2c->client,
192 wac_i2c->data, sizeof(wac_i2c->data));
193 if (error < 0)
194 goto out;
195
196 tsw = data[3] & 0x01;
197 ers = data[3] & 0x04;
198 f1 = data[3] & 0x02;
199 f2 = data[3] & 0x10;
200 x = le16_to_cpup((__le16 *)&data[4]);
201 y = le16_to_cpup((__le16 *)&data[6]);
202 pressure = le16_to_cpup((__le16 *)&data[8]);
203
204 if (!wac_i2c->prox)
205 wac_i2c->tool = (data[3] & 0x0c) ?
206 BTN_TOOL_RUBBER : BTN_TOOL_PEN;
207
208 wac_i2c->prox = data[3] & 0x20;
209
210 if (1 == exchange_x_y_flag) {
211 swap(x, y);
212 }
213 if (1 == revert_x_flag) {
214 x = screen_max_x - x;
215 }
216 if (1 == revert_y_flag) {
217 y = screen_max_y - y;
218 }
219
220 input_report_key(input, BTN_TOUCH, tsw || ers);
221 input_report_key(input, wac_i2c->tool, wac_i2c->prox);
222 input_report_key(input, BTN_STYLUS, f1);
223 input_report_key(input, BTN_STYLUS2, f2);
224 input_report_abs(input, ABS_X, x);
225 input_report_abs(input, ABS_Y, y);
226 input_report_abs(input, ABS_PRESSURE, pressure);
227 input_sync(input);
228
229 out:
230 if (device_can_wakeup(&wac_i2c->client->dev))
231 pm_relax(&wac_i2c->client->dev);
232
233 return IRQ_HANDLED;
234 }
235
wacom_i2c_open(struct input_dev * dev)236 static int wacom_i2c_open(struct input_dev *dev)
237 {
238 struct wacom_i2c *wac_i2c = input_get_drvdata(dev);
239 struct i2c_client *client = wac_i2c->client;
240
241 enable_irq(client->irq);
242
243 return 0;
244 }
245
wacom_i2c_close(struct input_dev * dev)246 static void wacom_i2c_close(struct input_dev *dev)
247 {
248 struct wacom_i2c *wac_i2c = input_get_drvdata(dev);
249 struct i2c_client *client = wac_i2c->client;
250
251 disable_irq(client->irq);
252 }
253
wacom_i2c_suspend(struct tp_device * tp_d)254 static int __maybe_unused wacom_i2c_suspend(struct tp_device *tp_d)
255 {
256 struct wacom_i2c *wac_i2c = container_of(tp_d, struct wacom_i2c, tp);
257
258 dev_dbg(&wac_i2c->client->dev, "%s\n", __func__);
259 disable_irq(wac_i2c->client->irq);
260 if (wac_i2c->supply) {
261 gpio_direction_output(wac_i2c->irq_gpio, 0);
262 gpio_direction_output(wac_i2c->pen_detect_gpio, 0);
263 gpio_direction_output(wac_i2c->reset_gpio, 0);
264 regulator_disable(wac_i2c->supply);
265 }
266 return 0;
267 }
268
wacom_i2c_resume(struct tp_device * tp_d)269 static int __maybe_unused wacom_i2c_resume(struct tp_device *tp_d)
270 {
271 struct wacom_i2c *wac_i2c = container_of(tp_d, struct wacom_i2c, tp);
272 int ret;
273
274 dev_dbg(&wac_i2c->client->dev, "%s\n", __func__);
275 if (wac_i2c->supply) {
276 gpio_direction_input(wac_i2c->irq_gpio);
277 gpio_direction_input(wac_i2c->pen_detect_gpio);
278 gpio_direction_output(wac_i2c->reset_gpio, 1);
279 ret = regulator_enable(wac_i2c->supply);
280 if (ret < 0)
281 dev_err(&wac_i2c->client->dev, "failed to enable wacom power supply\n");
282 }
283 enable_irq(wac_i2c->client->irq);
284
285 return 0;
286 }
287
wacom_i2c_probe(struct i2c_client * client,const struct i2c_device_id * id)288 static int wacom_i2c_probe(struct i2c_client *client,
289 const struct i2c_device_id *id)
290 {
291 struct wacom_i2c *wac_i2c;
292 struct input_dev *input;
293 struct wacom_features features = { 0 };
294 HID_DESC hid_desc = { 0 };
295 struct device_node *wac_np;
296 int error;
297 struct regulator *power_supply;
298
299 wac_np = client->dev.of_node;
300 if (!wac_np) {
301 dev_err(&client->dev, "get device node error\n");
302 return -ENODEV;
303 }
304
305 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
306 dev_err(&client->dev, "i2c_check_functionality error\n");
307 return -EIO;
308 }
309 of_property_read_u32(wac_np, "revert_x", &revert_x_flag);
310 of_property_read_u32(wac_np, "revert_y", &revert_y_flag);
311 of_property_read_u32(wac_np, "xy_exchange", &exchange_x_y_flag);
312
313 power_supply = devm_regulator_get(&client->dev, "pwr");
314 if (power_supply) {
315 dev_info(&client->dev, "wacom power supply = %dmv\n", regulator_get_voltage(power_supply));
316 error = regulator_enable(power_supply);
317 if (error < 0)
318 dev_err(&client->dev, "failed to enable wacom power supply\n");
319 }
320
321 error = wacom_query_device(client, &features);
322 if (error)
323 return error;
324
325 error = get_hid_desc(client, &hid_desc);
326 if (error)
327 return error;
328
329 wac_i2c = kzalloc(sizeof(*wac_i2c), GFP_KERNEL);
330 input = input_allocate_device();
331 if (!wac_i2c || !input) {
332 error = -ENOMEM;
333 goto err_free_mem;
334 }
335
336 if (power_supply)
337 wac_i2c->supply = power_supply;
338
339 wac_i2c->reset_gpio = of_get_named_gpio(wac_np, "gpio_rst", 0);
340 if (!gpio_is_valid(wac_i2c->reset_gpio)) {
341 dev_err(&client->dev, "no gpio_rst pin available\n");
342 goto err_free_mem;
343 }
344
345 error = devm_gpio_request_one(&client->dev, wac_i2c->reset_gpio, GPIOF_OUT_INIT_LOW, "gpio-rst");
346 if (error < 0) {
347 goto err_free_mem;
348 }
349 gpio_direction_output(wac_i2c->reset_gpio, 0);
350 msleep(100);
351 gpio_direction_output(wac_i2c->reset_gpio, 1);
352
353 wac_i2c->pen_detect_gpio = of_get_named_gpio(wac_np, "gpio_detect", 0);
354 if (!gpio_is_valid(wac_i2c->pen_detect_gpio)) {
355 dev_err(&client->dev, "no pen_detect_gpio pin available\n");
356 goto err_free_reset_gpio;
357 }
358 error = devm_gpio_request_one(&client->dev, wac_i2c->pen_detect_gpio, GPIOF_IN, "gpio_detect");
359 if (error < 0) {
360 goto err_free_reset_gpio;
361 }
362
363 wac_i2c->irq_gpio = of_get_named_gpio(wac_np, "gpio_intr", 0);
364 if (!gpio_is_valid(wac_i2c->irq_gpio)) {
365 dev_err(&client->dev, "no gpio_intr pin available\n");
366 goto err_free_pen_detect_gpio;
367 }
368
369 error = devm_gpio_request_one(&client->dev, wac_i2c->irq_gpio, GPIOF_IN, "gpio_intr");
370 if (error < 0) {
371 goto err_free_pen_detect_gpio;
372 }
373
374 client->irq = gpio_to_irq(wac_i2c->irq_gpio);
375 if (client->irq < 0) {
376 dev_err(&client->dev, "Unable to get irq number for GPIO %d, error %d\n", wac_i2c->irq_gpio, client->irq);
377 goto err_free_irq_gpio;
378 }
379
380 wac_i2c->features = &features;
381 wac_i2c->client = client;
382 wac_i2c->input = input;
383
384 input->name = "Wacom I2C Digitizer";
385 input->id.bustype = BUS_I2C;
386 input->id.vendor = 0x56a;
387 //input->id.version = features.fw_version;
388 input->id.version = hid_desc.wVersion;
389
390 input->dev.parent = &client->dev;
391 input->open = wacom_i2c_open;
392 input->close = wacom_i2c_close;
393
394 input->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
395
396 __set_bit(BTN_TOOL_PEN, input->keybit);
397 __set_bit(BTN_TOOL_RUBBER, input->keybit);
398 __set_bit(BTN_STYLUS, input->keybit);
399 __set_bit(BTN_STYLUS2, input->keybit);
400 __set_bit(BTN_TOUCH, input->keybit);
401 __set_bit(INPUT_PROP_DIRECT, input->propbit);
402
403 input_set_abs_params(input, ABS_X, 0, features.x_max, 0, 0);
404 input_set_abs_params(input, ABS_Y, 0, features.y_max, 0, 0);
405 input_set_abs_params(input, ABS_PRESSURE, 0, features.pressure_max, 0, 0);
406
407 input_set_drvdata(input, wac_i2c);
408
409 error = request_threaded_irq(client->irq, NULL, wacom_i2c_irq,
410 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
411 "wacom", wac_i2c);
412 if (error) {
413 dev_err(&client->dev,
414 "Failed to enable IRQ, error: %d\n", error);
415 goto err_free_mem;
416 }
417
418 /* Disable the IRQ, we'll enable it in wac_i2c_open() */
419 disable_irq(client->irq);
420
421 error = input_register_device(wac_i2c->input);
422 if (error) {
423 dev_err(&client->dev,
424 "Failed to register input device, error: %d\n", error);
425 goto err_free_irq;
426 }
427
428 device_init_wakeup(&client->dev, 1);
429 enable_irq_wake(client->irq);
430
431 wac_i2c->tp.tp_resume = wacom_i2c_resume;
432 wac_i2c->tp.tp_suspend = wacom_i2c_suspend;
433 tp_register_fb(&wac_i2c->tp);
434 i2c_set_clientdata(client, wac_i2c);
435
436 return 0;
437
438 err_free_irq:
439 free_irq(client->irq, wac_i2c);
440 err_free_reset_gpio:
441 err_free_pen_detect_gpio:
442 err_free_irq_gpio:
443 err_free_mem:
444 input_free_device(input);
445 kfree(wac_i2c);
446
447 return error;
448 }
449
wacom_i2c_remove(struct i2c_client * client)450 static int wacom_i2c_remove(struct i2c_client *client)
451 {
452 struct wacom_i2c *wac_i2c = i2c_get_clientdata(client);
453
454 free_irq(client->irq, wac_i2c);
455 input_unregister_device(wac_i2c->input);
456 kfree(wac_i2c);
457
458 return 0;
459 }
460
461 static const struct i2c_device_id wacom_i2c_id[] = {
462 { "wacom", 0 },
463 { },
464 };
465 MODULE_DEVICE_TABLE(i2c, wacom_i2c_id);
466
467 static const struct of_device_id wacom_dt_ids[] = {
468 {
469 .compatible = "wacom,w9013",
470 .data = (void *) &wacom_i2c_id[0],
471 }, {
472 /* sentinel */
473 }
474 };
475 MODULE_DEVICE_TABLE(of, wacom_dt_ids);
476
477 static struct i2c_driver wacom_i2c_driver = {
478 .driver = {
479 .name = "wacom",
480 .owner = THIS_MODULE,
481 .of_match_table = wacom_dt_ids,
482 },
483
484 .probe = wacom_i2c_probe,
485 .remove = wacom_i2c_remove,
486 .id_table = wacom_i2c_id,
487 };
488
wacom_init(void)489 static int __init wacom_init(void)
490 {
491 return i2c_add_driver(&wacom_i2c_driver);
492 }
493
wacom_exit(void)494 static void __exit wacom_exit(void)
495 {
496 i2c_del_driver(&wacom_i2c_driver);
497 }
498
499 /*
500 * Module entry points
501 */
502 subsys_initcall(wacom_init);
503 //late_initcall(wacom_init);
504 module_exit(wacom_exit);
505
506 //module_i2c_driver(wacom_i2c_driver);
507
508 MODULE_AUTHOR("Tatsunosuke Tobita <tobita.tatsunosuke@wacom.co.jp>");
509 MODULE_DESCRIPTION("WACOM EMR I2C Driver");
510 MODULE_LICENSE("GPL");
511