wacom_i2c.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. #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. struct wacom_features {
  29. int x_max;
  30. int y_max;
  31. int pressure_max;
  32. char fw_version;
  33. };
  34. struct wacom_i2c {
  35. struct i2c_client *client;
  36. struct input_dev *input;
  37. u8 data[WACOM_QUERY_SIZE];
  38. bool prox;
  39. int tool;
  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 irqreturn_t wacom_i2c_irq(int irq, void *dev_id)
  85. {
  86. struct wacom_i2c *wac_i2c = dev_id;
  87. struct input_dev *input = wac_i2c->input;
  88. u8 *data = wac_i2c->data;
  89. unsigned int x, y, pressure;
  90. unsigned char tsw, f1, f2, ers;
  91. int error;
  92. error = i2c_master_recv(wac_i2c->client,
  93. wac_i2c->data, sizeof(wac_i2c->data));
  94. if (error < 0)
  95. goto out;
  96. tsw = data[3] & 0x01;
  97. ers = data[3] & 0x04;
  98. f1 = data[3] & 0x02;
  99. f2 = data[3] & 0x10;
  100. x = le16_to_cpup((__le16 *)&data[4]);
  101. y = le16_to_cpup((__le16 *)&data[6]);
  102. pressure = le16_to_cpup((__le16 *)&data[8]);
  103. if (!wac_i2c->prox)
  104. wac_i2c->tool = (data[3] & 0x0c) ?
  105. BTN_TOOL_RUBBER : BTN_TOOL_PEN;
  106. wac_i2c->prox = data[3] & 0x20;
  107. input_report_key(input, BTN_TOUCH, tsw || ers);
  108. input_report_key(input, wac_i2c->tool, wac_i2c->prox);
  109. input_report_key(input, BTN_STYLUS, f1);
  110. input_report_key(input, BTN_STYLUS2, f2);
  111. input_report_abs(input, ABS_X, x);
  112. input_report_abs(input, ABS_Y, y);
  113. input_report_abs(input, ABS_PRESSURE, pressure);
  114. input_sync(input);
  115. out:
  116. return IRQ_HANDLED;
  117. }
  118. static int wacom_i2c_open(struct input_dev *dev)
  119. {
  120. struct wacom_i2c *wac_i2c = input_get_drvdata(dev);
  121. struct i2c_client *client = wac_i2c->client;
  122. enable_irq(client->irq);
  123. return 0;
  124. }
  125. static void wacom_i2c_close(struct input_dev *dev)
  126. {
  127. struct wacom_i2c *wac_i2c = input_get_drvdata(dev);
  128. struct i2c_client *client = wac_i2c->client;
  129. disable_irq(client->irq);
  130. }
  131. static int wacom_i2c_probe(struct i2c_client *client,
  132. const struct i2c_device_id *id)
  133. {
  134. struct wacom_i2c *wac_i2c;
  135. struct input_dev *input;
  136. struct wacom_features features = { 0 };
  137. int error;
  138. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  139. dev_err(&client->dev, "i2c_check_functionality error\n");
  140. return -EIO;
  141. }
  142. error = wacom_query_device(client, &features);
  143. if (error)
  144. return error;
  145. wac_i2c = kzalloc(sizeof(*wac_i2c), GFP_KERNEL);
  146. input = input_allocate_device();
  147. if (!wac_i2c || !input) {
  148. error = -ENOMEM;
  149. goto err_free_mem;
  150. }
  151. wac_i2c->client = client;
  152. wac_i2c->input = input;
  153. input->name = "Wacom I2C Digitizer";
  154. input->id.bustype = BUS_I2C;
  155. input->id.vendor = 0x56a;
  156. input->id.version = features.fw_version;
  157. input->dev.parent = &client->dev;
  158. input->open = wacom_i2c_open;
  159. input->close = wacom_i2c_close;
  160. input->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  161. __set_bit(BTN_TOOL_PEN, input->keybit);
  162. __set_bit(BTN_TOOL_RUBBER, input->keybit);
  163. __set_bit(BTN_STYLUS, input->keybit);
  164. __set_bit(BTN_STYLUS2, input->keybit);
  165. __set_bit(BTN_TOUCH, input->keybit);
  166. input_set_abs_params(input, ABS_X, 0, features.x_max, 0, 0);
  167. input_set_abs_params(input, ABS_Y, 0, features.y_max, 0, 0);
  168. input_set_abs_params(input, ABS_PRESSURE,
  169. 0, features.pressure_max, 0, 0);
  170. input_set_drvdata(input, wac_i2c);
  171. error = request_threaded_irq(client->irq, NULL, wacom_i2c_irq,
  172. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  173. "wacom_i2c", wac_i2c);
  174. if (error) {
  175. dev_err(&client->dev,
  176. "Failed to enable IRQ, error: %d\n", error);
  177. goto err_free_mem;
  178. }
  179. /* Disable the IRQ, we'll enable it in wac_i2c_open() */
  180. disable_irq(client->irq);
  181. error = input_register_device(wac_i2c->input);
  182. if (error) {
  183. dev_err(&client->dev,
  184. "Failed to register input device, error: %d\n", error);
  185. goto err_free_irq;
  186. }
  187. i2c_set_clientdata(client, wac_i2c);
  188. return 0;
  189. err_free_irq:
  190. free_irq(client->irq, wac_i2c);
  191. err_free_mem:
  192. input_free_device(input);
  193. kfree(wac_i2c);
  194. return error;
  195. }
  196. static int wacom_i2c_remove(struct i2c_client *client)
  197. {
  198. struct wacom_i2c *wac_i2c = i2c_get_clientdata(client);
  199. free_irq(client->irq, wac_i2c);
  200. input_unregister_device(wac_i2c->input);
  201. kfree(wac_i2c);
  202. return 0;
  203. }
  204. static int __maybe_unused wacom_i2c_suspend(struct device *dev)
  205. {
  206. struct i2c_client *client = to_i2c_client(dev);
  207. disable_irq(client->irq);
  208. return 0;
  209. }
  210. static int __maybe_unused wacom_i2c_resume(struct device *dev)
  211. {
  212. struct i2c_client *client = to_i2c_client(dev);
  213. enable_irq(client->irq);
  214. return 0;
  215. }
  216. static SIMPLE_DEV_PM_OPS(wacom_i2c_pm, wacom_i2c_suspend, wacom_i2c_resume);
  217. static const struct i2c_device_id wacom_i2c_id[] = {
  218. { "WAC_I2C_EMR", 0 },
  219. { },
  220. };
  221. MODULE_DEVICE_TABLE(i2c, wacom_i2c_id);
  222. static struct i2c_driver wacom_i2c_driver = {
  223. .driver = {
  224. .name = "wacom_i2c",
  225. .pm = &wacom_i2c_pm,
  226. },
  227. .probe = wacom_i2c_probe,
  228. .remove = wacom_i2c_remove,
  229. .id_table = wacom_i2c_id,
  230. };
  231. module_i2c_driver(wacom_i2c_driver);
  232. MODULE_AUTHOR("Tatsunosuke Tobita <tobita.tatsunosuke@wacom.co.jp>");
  233. MODULE_DESCRIPTION("WACOM EMR I2C Driver");
  234. MODULE_LICENSE("GPL");