wacom_i2c.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. #include <linux/module.h>
  14. #include <linux/input.h>
  15. #include <linux/i2c.h>
  16. #include <linux/slab.h>
  17. #include <linux/irq.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/gpio.h>
  20. #include <asm/unaligned.h>
  21. #define WACOM_CMD_QUERY0 0x04
  22. #define WACOM_CMD_QUERY1 0x00
  23. #define WACOM_CMD_QUERY2 0x33
  24. #define WACOM_CMD_QUERY3 0x02
  25. #define WACOM_CMD_THROW0 0x05
  26. #define WACOM_CMD_THROW1 0x00
  27. #define WACOM_QUERY_SIZE 19
  28. #define WACOM_RETRY_CNT 100
  29. struct wacom_features {
  30. int x_max;
  31. int y_max;
  32. int pressure_max;
  33. char fw_version;
  34. };
  35. struct wacom_i2c {
  36. struct i2c_client *client;
  37. struct input_dev *input;
  38. unsigned int gpio;
  39. u8 data[WACOM_QUERY_SIZE];
  40. };
  41. static int wacom_query_device(struct i2c_client *client,
  42. struct wacom_features *features)
  43. {
  44. int ret;
  45. u8 cmd1[] = { WACOM_CMD_QUERY0, WACOM_CMD_QUERY1,
  46. WACOM_CMD_QUERY2, WACOM_CMD_QUERY3 };
  47. u8 cmd2[] = { WACOM_CMD_THROW0, WACOM_CMD_THROW1 };
  48. u8 data[WACOM_QUERY_SIZE];
  49. struct i2c_msg msgs[] = {
  50. {
  51. .addr = client->addr,
  52. .flags = 0,
  53. .len = sizeof(cmd1),
  54. .buf = cmd1,
  55. },
  56. {
  57. .addr = client->addr,
  58. .flags = 0,
  59. .len = sizeof(cmd2),
  60. .buf = cmd2,
  61. },
  62. {
  63. .addr = client->addr,
  64. .flags = I2C_M_RD,
  65. .len = sizeof(data),
  66. .buf = data,
  67. },
  68. };
  69. ret = i2c_transfer(client->adapter, msgs, ARRAY_SIZE(msgs));
  70. if (ret < 0)
  71. return ret;
  72. if (ret != ARRAY_SIZE(msgs))
  73. return -EIO;
  74. features->x_max = get_unaligned_le16(&data[3]);
  75. features->y_max = get_unaligned_le16(&data[5]);
  76. features->pressure_max = get_unaligned_le16(&data[11]);
  77. features->fw_version = get_unaligned_le16(&data[13]);
  78. dev_dbg(&client->dev,
  79. "x_max:%d, y_max:%d, pressure:%d, fw:%d\n",
  80. features->x_max, features->y_max,
  81. features->pressure_max, features->fw_version);
  82. return 0;
  83. }
  84. static int wacom_i2c_fetch_data(struct wacom_i2c *wac_i2c)
  85. {
  86. int retries = 0;
  87. int ret;
  88. do {
  89. ret = i2c_master_recv(wac_i2c->client,
  90. wac_i2c->data, sizeof(wac_i2c->data));
  91. } while (gpio_get_value(wac_i2c->gpio) == 0 &&
  92. retries++ < WACOM_RETRY_CNT);
  93. if (retries >= WACOM_RETRY_CNT)
  94. ret = -EIO;
  95. return ret < 0 ? ret : 0;
  96. }
  97. static irqreturn_t wacom_i2c_irq(int irq, void *dev_id)
  98. {
  99. struct wacom_i2c *wac_i2c = dev_id;
  100. struct input_dev *input = wac_i2c->input;
  101. u8 *data = wac_i2c->data;
  102. unsigned int x, y, pressure;
  103. unsigned char tsw, f1, f2, ers;
  104. int error;
  105. error = wacom_i2c_fetch_data(wac_i2c);
  106. if (error)
  107. goto out;
  108. tsw = data[3] & 0x01;
  109. ers = data[3] & 0x04;
  110. f1 = data[3] & 0x02;
  111. f2 = data[3] & 0x10;
  112. x = le16_to_cpup((__le16 *)&data[4]);
  113. y = le16_to_cpup((__le16 *)&data[6]);
  114. pressure = le16_to_cpup((__le16 *)&data[8]);
  115. input_report_key(input, BTN_TOUCH, tsw || ers);
  116. input_report_key(input, BTN_TOOL_PEN, tsw);
  117. input_report_key(input, BTN_TOOL_RUBBER, ers);
  118. input_report_key(input, BTN_STYLUS, f1);
  119. input_report_key(input, BTN_STYLUS2, f2);
  120. input_report_abs(input, ABS_X, x);
  121. input_report_abs(input, ABS_Y, y);
  122. input_report_abs(input, ABS_PRESSURE, pressure);
  123. input_sync(input);
  124. out:
  125. return IRQ_HANDLED;
  126. }
  127. static int wacom_i2c_open(struct input_dev *dev)
  128. {
  129. struct wacom_i2c *wac_i2c = input_get_drvdata(dev);
  130. struct i2c_client *client = wac_i2c->client;
  131. int error;
  132. /* Clear the device buffer */
  133. error = wacom_i2c_fetch_data(wac_i2c);
  134. if (error)
  135. return error;
  136. enable_irq(client->irq);
  137. return 0;
  138. }
  139. static void wacom_i2c_close(struct input_dev *dev)
  140. {
  141. struct wacom_i2c *wac_i2c = input_get_drvdata(dev);
  142. struct i2c_client *client = wac_i2c->client;
  143. disable_irq(client->irq);
  144. }
  145. static int __devinit wacom_i2c_probe(struct i2c_client *client,
  146. const struct i2c_device_id *id)
  147. {
  148. struct wacom_i2c *wac_i2c;
  149. struct input_dev *input;
  150. struct wacom_features features;
  151. int gpio;
  152. int error;
  153. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  154. dev_err(&client->dev, "i2c_check_functionality error\n");
  155. return -EIO;
  156. }
  157. gpio = irq_to_gpio(client->irq);
  158. if (gpio < 0) {
  159. error = gpio;
  160. dev_err(&client->dev,
  161. "irq_to_gpio() failed, error: %d\n", error);
  162. return error;
  163. }
  164. error = wacom_query_device(client, &features);
  165. if (error)
  166. return error;
  167. wac_i2c = kzalloc(sizeof(*wac_i2c), GFP_KERNEL);
  168. input = input_allocate_device();
  169. if (!wac_i2c || !input) {
  170. error = -ENOMEM;
  171. goto err_free_mem;
  172. }
  173. wac_i2c->client = client;
  174. wac_i2c->input = input;
  175. wac_i2c->gpio = gpio;
  176. input->name = "Wacom I2C Digitizer";
  177. input->id.bustype = BUS_I2C;
  178. input->id.vendor = 0x56a;
  179. input->id.version = features.fw_version;
  180. input->dev.parent = &client->dev;
  181. input->open = wacom_i2c_open;
  182. input->close = wacom_i2c_close;
  183. input->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  184. __set_bit(BTN_TOOL_PEN, input->keybit);
  185. __set_bit(BTN_TOOL_RUBBER, input->keybit);
  186. __set_bit(BTN_STYLUS, input->keybit);
  187. __set_bit(BTN_STYLUS2, input->keybit);
  188. __set_bit(BTN_TOUCH, input->keybit);
  189. input_set_abs_params(input, ABS_X, 0, features.x_max, 0, 0);
  190. input_set_abs_params(input, ABS_Y, 0, features.y_max, 0, 0);
  191. input_set_abs_params(input, ABS_PRESSURE,
  192. 0, features.pressure_max, 0, 0);
  193. input_set_drvdata(input, wac_i2c);
  194. error = request_threaded_irq(client->irq, NULL, wacom_i2c_irq,
  195. IRQF_TRIGGER_FALLING,
  196. "wacom_i2c", wac_i2c);
  197. if (error) {
  198. dev_err(&client->dev,
  199. "Failed to enable IRQ, error: %d\n", error);
  200. goto err_free_mem;
  201. }
  202. /* Disable the IRQ, we'll enable it in wac_i2c_open() */
  203. disable_irq(client->irq);
  204. error = input_register_device(wac_i2c->input);
  205. if (error) {
  206. dev_err(&client->dev,
  207. "Failed to register input device, error: %d\n", error);
  208. goto err_free_irq;
  209. }
  210. i2c_set_clientdata(client, wac_i2c);
  211. return 0;
  212. err_free_irq:
  213. free_irq(client->irq, wac_i2c);
  214. err_free_mem:
  215. input_free_device(input);
  216. kfree(wac_i2c);
  217. return error;
  218. }
  219. static int __devexit wacom_i2c_remove(struct i2c_client *client)
  220. {
  221. struct wacom_i2c *wac_i2c = i2c_get_clientdata(client);
  222. free_irq(client->irq, wac_i2c);
  223. input_unregister_device(wac_i2c->input);
  224. kfree(wac_i2c);
  225. return 0;
  226. }
  227. #ifdef CONFIG_PM_SLEEP
  228. static int wacom_i2c_suspend(struct device *dev)
  229. {
  230. struct i2c_client *client = to_i2c_client(dev);
  231. disable_irq(client->irq);
  232. return 0;
  233. }
  234. static int wacom_i2c_resume(struct device *dev)
  235. {
  236. struct i2c_client *client = to_i2c_client(dev);
  237. enable_irq(client->irq);
  238. return 0;
  239. }
  240. #endif
  241. static SIMPLE_DEV_PM_OPS(wacom_i2c_pm, wacom_i2c_suspend, wacom_i2c_resume);
  242. static const struct i2c_device_id wacom_i2c_id[] = {
  243. { "WAC_I2C_EMR", 0 },
  244. { },
  245. };
  246. MODULE_DEVICE_TABLE(i2c, wacom_i2c_id);
  247. static struct i2c_driver wacom_i2c_driver = {
  248. .driver = {
  249. .name = "wacom_i2c",
  250. .owner = THIS_MODULE,
  251. .pm = &wacom_i2c_pm,
  252. },
  253. .probe = wacom_i2c_probe,
  254. .remove = __devexit_p(wacom_i2c_remove),
  255. .id_table = wacom_i2c_id,
  256. };
  257. module_i2c_driver(wacom_i2c_driver);
  258. MODULE_AUTHOR("Tatsunosuke Tobita <tobita.tatsunosuke@wacom.co.jp>");
  259. MODULE_DESCRIPTION("WACOM EMR I2C Driver");
  260. MODULE_LICENSE("GPL");