wacom_i2c.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Wacom Penabled Driver for I2C
  4. *
  5. * Copyright (c) 2011 - 2013 Tatsunosuke Tobita, Wacom.
  6. * <[email protected]>
  7. */
  8. #include <linux/bits.h>
  9. #include <linux/module.h>
  10. #include <linux/input.h>
  11. #include <linux/i2c.h>
  12. #include <linux/slab.h>
  13. #include <linux/irq.h>
  14. #include <linux/interrupt.h>
  15. #include <asm/unaligned.h>
  16. /* Bitmasks (for data[3]) */
  17. #define WACOM_TIP_SWITCH BIT(0)
  18. #define WACOM_BARREL_SWITCH BIT(1)
  19. #define WACOM_ERASER BIT(2)
  20. #define WACOM_INVERT BIT(3)
  21. #define WACOM_BARREL_SWITCH_2 BIT(4)
  22. #define WACOM_IN_PROXIMITY BIT(5)
  23. /* Registers */
  24. #define WACOM_COMMAND_LSB 0x04
  25. #define WACOM_COMMAND_MSB 0x00
  26. #define WACOM_DATA_LSB 0x05
  27. #define WACOM_DATA_MSB 0x00
  28. /* Report types */
  29. #define REPORT_FEATURE 0x30
  30. /* Requests / operations */
  31. #define OPCODE_GET_REPORT 0x02
  32. #define WACOM_QUERY_REPORT 3
  33. #define WACOM_QUERY_SIZE 19
  34. struct wacom_features {
  35. int x_max;
  36. int y_max;
  37. int pressure_max;
  38. char fw_version;
  39. };
  40. struct wacom_i2c {
  41. struct i2c_client *client;
  42. struct input_dev *input;
  43. u8 data[WACOM_QUERY_SIZE];
  44. bool prox;
  45. int tool;
  46. };
  47. static int wacom_query_device(struct i2c_client *client,
  48. struct wacom_features *features)
  49. {
  50. u8 get_query_data_cmd[] = {
  51. WACOM_COMMAND_LSB,
  52. WACOM_COMMAND_MSB,
  53. REPORT_FEATURE | WACOM_QUERY_REPORT,
  54. OPCODE_GET_REPORT,
  55. WACOM_DATA_LSB,
  56. WACOM_DATA_MSB,
  57. };
  58. u8 data[WACOM_QUERY_SIZE];
  59. int ret;
  60. struct i2c_msg msgs[] = {
  61. /* Request reading of feature ReportID: 3 (Pen Query Data) */
  62. {
  63. .addr = client->addr,
  64. .flags = 0,
  65. .len = sizeof(get_query_data_cmd),
  66. .buf = get_query_data_cmd,
  67. },
  68. {
  69. .addr = client->addr,
  70. .flags = I2C_M_RD,
  71. .len = sizeof(data),
  72. .buf = data,
  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. features->x_max = get_unaligned_le16(&data[3]);
  81. features->y_max = get_unaligned_le16(&data[5]);
  82. features->pressure_max = get_unaligned_le16(&data[11]);
  83. features->fw_version = get_unaligned_le16(&data[13]);
  84. dev_dbg(&client->dev,
  85. "x_max:%d, y_max:%d, pressure:%d, fw:%d\n",
  86. features->x_max, features->y_max,
  87. features->pressure_max, features->fw_version);
  88. return 0;
  89. }
  90. static irqreturn_t wacom_i2c_irq(int irq, void *dev_id)
  91. {
  92. struct wacom_i2c *wac_i2c = dev_id;
  93. struct input_dev *input = wac_i2c->input;
  94. u8 *data = wac_i2c->data;
  95. unsigned int x, y, pressure;
  96. unsigned char tsw, f1, f2, ers;
  97. int error;
  98. error = i2c_master_recv(wac_i2c->client,
  99. wac_i2c->data, sizeof(wac_i2c->data));
  100. if (error < 0)
  101. goto out;
  102. tsw = data[3] & WACOM_TIP_SWITCH;
  103. ers = data[3] & WACOM_ERASER;
  104. f1 = data[3] & WACOM_BARREL_SWITCH;
  105. f2 = data[3] & WACOM_BARREL_SWITCH_2;
  106. x = le16_to_cpup((__le16 *)&data[4]);
  107. y = le16_to_cpup((__le16 *)&data[6]);
  108. pressure = le16_to_cpup((__le16 *)&data[8]);
  109. if (!wac_i2c->prox)
  110. wac_i2c->tool = (data[3] & (WACOM_ERASER | WACOM_INVERT)) ?
  111. BTN_TOOL_RUBBER : BTN_TOOL_PEN;
  112. wac_i2c->prox = data[3] & WACOM_IN_PROXIMITY;
  113. input_report_key(input, BTN_TOUCH, tsw || ers);
  114. input_report_key(input, wac_i2c->tool, wac_i2c->prox);
  115. input_report_key(input, BTN_STYLUS, f1);
  116. input_report_key(input, BTN_STYLUS2, f2);
  117. input_report_abs(input, ABS_X, x);
  118. input_report_abs(input, ABS_Y, y);
  119. input_report_abs(input, ABS_PRESSURE, pressure);
  120. input_sync(input);
  121. out:
  122. return IRQ_HANDLED;
  123. }
  124. static int wacom_i2c_open(struct input_dev *dev)
  125. {
  126. struct wacom_i2c *wac_i2c = input_get_drvdata(dev);
  127. struct i2c_client *client = wac_i2c->client;
  128. enable_irq(client->irq);
  129. return 0;
  130. }
  131. static void wacom_i2c_close(struct input_dev *dev)
  132. {
  133. struct wacom_i2c *wac_i2c = input_get_drvdata(dev);
  134. struct i2c_client *client = wac_i2c->client;
  135. disable_irq(client->irq);
  136. }
  137. static int wacom_i2c_probe(struct i2c_client *client,
  138. const struct i2c_device_id *id)
  139. {
  140. struct device *dev = &client->dev;
  141. struct wacom_i2c *wac_i2c;
  142. struct input_dev *input;
  143. struct wacom_features features = { 0 };
  144. int error;
  145. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
  146. dev_err(dev, "i2c_check_functionality error\n");
  147. return -EIO;
  148. }
  149. error = wacom_query_device(client, &features);
  150. if (error)
  151. return error;
  152. wac_i2c = devm_kzalloc(dev, sizeof(*wac_i2c), GFP_KERNEL);
  153. if (!wac_i2c)
  154. return -ENOMEM;
  155. wac_i2c->client = client;
  156. input = devm_input_allocate_device(dev);
  157. if (!input)
  158. return -ENOMEM;
  159. wac_i2c->input = input;
  160. input->name = "Wacom I2C Digitizer";
  161. input->id.bustype = BUS_I2C;
  162. input->id.vendor = 0x56a;
  163. input->id.version = features.fw_version;
  164. input->open = wacom_i2c_open;
  165. input->close = wacom_i2c_close;
  166. input->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  167. __set_bit(BTN_TOOL_PEN, input->keybit);
  168. __set_bit(BTN_TOOL_RUBBER, input->keybit);
  169. __set_bit(BTN_STYLUS, input->keybit);
  170. __set_bit(BTN_STYLUS2, input->keybit);
  171. __set_bit(BTN_TOUCH, input->keybit);
  172. input_set_abs_params(input, ABS_X, 0, features.x_max, 0, 0);
  173. input_set_abs_params(input, ABS_Y, 0, features.y_max, 0, 0);
  174. input_set_abs_params(input, ABS_PRESSURE,
  175. 0, features.pressure_max, 0, 0);
  176. input_set_drvdata(input, wac_i2c);
  177. error = devm_request_threaded_irq(dev, client->irq, NULL, wacom_i2c_irq,
  178. IRQF_ONESHOT, "wacom_i2c", wac_i2c);
  179. if (error) {
  180. dev_err(dev, "Failed to request IRQ: %d\n", error);
  181. return error;
  182. }
  183. /* Disable the IRQ, we'll enable it in wac_i2c_open() */
  184. disable_irq(client->irq);
  185. error = input_register_device(wac_i2c->input);
  186. if (error) {
  187. dev_err(dev, "Failed to register input device: %d\n", error);
  188. return error;
  189. }
  190. return 0;
  191. }
  192. static int __maybe_unused wacom_i2c_suspend(struct device *dev)
  193. {
  194. struct i2c_client *client = to_i2c_client(dev);
  195. disable_irq(client->irq);
  196. return 0;
  197. }
  198. static int __maybe_unused wacom_i2c_resume(struct device *dev)
  199. {
  200. struct i2c_client *client = to_i2c_client(dev);
  201. enable_irq(client->irq);
  202. return 0;
  203. }
  204. static SIMPLE_DEV_PM_OPS(wacom_i2c_pm, wacom_i2c_suspend, wacom_i2c_resume);
  205. static const struct i2c_device_id wacom_i2c_id[] = {
  206. { "WAC_I2C_EMR", 0 },
  207. { },
  208. };
  209. MODULE_DEVICE_TABLE(i2c, wacom_i2c_id);
  210. static struct i2c_driver wacom_i2c_driver = {
  211. .driver = {
  212. .name = "wacom_i2c",
  213. .pm = &wacom_i2c_pm,
  214. },
  215. .probe = wacom_i2c_probe,
  216. .id_table = wacom_i2c_id,
  217. };
  218. module_i2c_driver(wacom_i2c_driver);
  219. MODULE_AUTHOR("Tatsunosuke Tobita <[email protected]>");
  220. MODULE_DESCRIPTION("WACOM EMR I2C Driver");
  221. MODULE_LICENSE("GPL");