wacom_i2c.c 6.5 KB

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