eeti_ts.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Touch Screen driver for EETI's I2C connected touch screen panels
  4. * Copyright (c) 2009,2018 Daniel Mack <[email protected]>
  5. *
  6. * See EETI's software guide for the protocol specification:
  7. * http://home.eeti.com.tw/documentation.html
  8. *
  9. * Based on migor_ts.c
  10. * Copyright (c) 2008 Magnus Damm
  11. * Copyright (c) 2007 Ujjwal Pande <[email protected]>
  12. */
  13. #include <linux/module.h>
  14. #include <linux/kernel.h>
  15. #include <linux/input.h>
  16. #include <linux/input/touchscreen.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/i2c.h>
  19. #include <linux/timer.h>
  20. #include <linux/gpio/consumer.h>
  21. #include <linux/of.h>
  22. #include <linux/slab.h>
  23. #include <asm/unaligned.h>
  24. struct eeti_ts {
  25. struct i2c_client *client;
  26. struct input_dev *input;
  27. struct gpio_desc *attn_gpio;
  28. struct touchscreen_properties props;
  29. struct mutex mutex;
  30. bool running;
  31. };
  32. #define EETI_TS_BITDEPTH (11)
  33. #define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1)
  34. #define REPORT_BIT_PRESSED BIT(0)
  35. #define REPORT_BIT_AD0 BIT(1)
  36. #define REPORT_BIT_AD1 BIT(2)
  37. #define REPORT_BIT_HAS_PRESSURE BIT(6)
  38. #define REPORT_RES_BITS(v) (((v) >> 1) + EETI_TS_BITDEPTH)
  39. static void eeti_ts_report_event(struct eeti_ts *eeti, u8 *buf)
  40. {
  41. unsigned int res;
  42. u16 x, y;
  43. res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
  44. x = get_unaligned_be16(&buf[1]);
  45. y = get_unaligned_be16(&buf[3]);
  46. /* fix the range to 11 bits */
  47. x >>= res - EETI_TS_BITDEPTH;
  48. y >>= res - EETI_TS_BITDEPTH;
  49. if (buf[0] & REPORT_BIT_HAS_PRESSURE)
  50. input_report_abs(eeti->input, ABS_PRESSURE, buf[5]);
  51. touchscreen_report_pos(eeti->input, &eeti->props, x, y, false);
  52. input_report_key(eeti->input, BTN_TOUCH, buf[0] & REPORT_BIT_PRESSED);
  53. input_sync(eeti->input);
  54. }
  55. static int eeti_ts_read(struct eeti_ts *eeti)
  56. {
  57. int len, error;
  58. char buf[6];
  59. len = i2c_master_recv(eeti->client, buf, sizeof(buf));
  60. if (len != sizeof(buf)) {
  61. error = len < 0 ? len : -EIO;
  62. dev_err(&eeti->client->dev,
  63. "failed to read touchscreen data: %d\n",
  64. error);
  65. return error;
  66. }
  67. /* Motion packet */
  68. if (buf[0] & 0x80)
  69. eeti_ts_report_event(eeti, buf);
  70. return 0;
  71. }
  72. static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
  73. {
  74. struct eeti_ts *eeti = dev_id;
  75. int error;
  76. mutex_lock(&eeti->mutex);
  77. do {
  78. /*
  79. * If we have attention GPIO, trust it. Otherwise we'll read
  80. * once and exit. We assume that in this case we are using
  81. * level triggered interrupt and it will get raised again
  82. * if/when there is more data.
  83. */
  84. if (eeti->attn_gpio &&
  85. !gpiod_get_value_cansleep(eeti->attn_gpio)) {
  86. break;
  87. }
  88. error = eeti_ts_read(eeti);
  89. if (error)
  90. break;
  91. } while (eeti->running && eeti->attn_gpio);
  92. mutex_unlock(&eeti->mutex);
  93. return IRQ_HANDLED;
  94. }
  95. static void eeti_ts_start(struct eeti_ts *eeti)
  96. {
  97. mutex_lock(&eeti->mutex);
  98. eeti->running = true;
  99. enable_irq(eeti->client->irq);
  100. /*
  101. * Kick the controller in case we are using edge interrupt and
  102. * we missed our edge while interrupt was disabled. We expect
  103. * the attention GPIO to be wired in this case.
  104. */
  105. if (eeti->attn_gpio && gpiod_get_value_cansleep(eeti->attn_gpio))
  106. eeti_ts_read(eeti);
  107. mutex_unlock(&eeti->mutex);
  108. }
  109. static void eeti_ts_stop(struct eeti_ts *eeti)
  110. {
  111. /*
  112. * Not locking here, just setting a flag and expect that the
  113. * interrupt thread will notice the flag eventually.
  114. */
  115. eeti->running = false;
  116. wmb();
  117. disable_irq(eeti->client->irq);
  118. }
  119. static int eeti_ts_open(struct input_dev *dev)
  120. {
  121. struct eeti_ts *eeti = input_get_drvdata(dev);
  122. eeti_ts_start(eeti);
  123. return 0;
  124. }
  125. static void eeti_ts_close(struct input_dev *dev)
  126. {
  127. struct eeti_ts *eeti = input_get_drvdata(dev);
  128. eeti_ts_stop(eeti);
  129. }
  130. static int eeti_ts_probe(struct i2c_client *client,
  131. const struct i2c_device_id *idp)
  132. {
  133. struct device *dev = &client->dev;
  134. struct eeti_ts *eeti;
  135. struct input_dev *input;
  136. int error;
  137. /*
  138. * In contrast to what's described in the datasheet, there seems
  139. * to be no way of probing the presence of that device using I2C
  140. * commands. So we need to blindly believe it is there, and wait
  141. * for interrupts to occur.
  142. */
  143. eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL);
  144. if (!eeti) {
  145. dev_err(dev, "failed to allocate driver data\n");
  146. return -ENOMEM;
  147. }
  148. mutex_init(&eeti->mutex);
  149. input = devm_input_allocate_device(dev);
  150. if (!input) {
  151. dev_err(dev, "Failed to allocate input device.\n");
  152. return -ENOMEM;
  153. }
  154. input_set_capability(input, EV_KEY, BTN_TOUCH);
  155. input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
  156. input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
  157. input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
  158. touchscreen_parse_properties(input, false, &eeti->props);
  159. input->name = client->name;
  160. input->id.bustype = BUS_I2C;
  161. input->open = eeti_ts_open;
  162. input->close = eeti_ts_close;
  163. eeti->client = client;
  164. eeti->input = input;
  165. eeti->attn_gpio = devm_gpiod_get_optional(dev, "attn", GPIOD_IN);
  166. if (IS_ERR(eeti->attn_gpio))
  167. return PTR_ERR(eeti->attn_gpio);
  168. i2c_set_clientdata(client, eeti);
  169. input_set_drvdata(input, eeti);
  170. error = devm_request_threaded_irq(dev, client->irq,
  171. NULL, eeti_ts_isr,
  172. IRQF_ONESHOT,
  173. client->name, eeti);
  174. if (error) {
  175. dev_err(dev, "Unable to request touchscreen IRQ: %d\n",
  176. error);
  177. return error;
  178. }
  179. /*
  180. * Disable the device for now. It will be enabled once the
  181. * input device is opened.
  182. */
  183. eeti_ts_stop(eeti);
  184. error = input_register_device(input);
  185. if (error)
  186. return error;
  187. return 0;
  188. }
  189. static int __maybe_unused eeti_ts_suspend(struct device *dev)
  190. {
  191. struct i2c_client *client = to_i2c_client(dev);
  192. struct eeti_ts *eeti = i2c_get_clientdata(client);
  193. struct input_dev *input_dev = eeti->input;
  194. mutex_lock(&input_dev->mutex);
  195. if (input_device_enabled(input_dev))
  196. eeti_ts_stop(eeti);
  197. mutex_unlock(&input_dev->mutex);
  198. if (device_may_wakeup(&client->dev))
  199. enable_irq_wake(client->irq);
  200. return 0;
  201. }
  202. static int __maybe_unused eeti_ts_resume(struct device *dev)
  203. {
  204. struct i2c_client *client = to_i2c_client(dev);
  205. struct eeti_ts *eeti = i2c_get_clientdata(client);
  206. struct input_dev *input_dev = eeti->input;
  207. if (device_may_wakeup(&client->dev))
  208. disable_irq_wake(client->irq);
  209. mutex_lock(&input_dev->mutex);
  210. if (input_device_enabled(input_dev))
  211. eeti_ts_start(eeti);
  212. mutex_unlock(&input_dev->mutex);
  213. return 0;
  214. }
  215. static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
  216. static const struct i2c_device_id eeti_ts_id[] = {
  217. { "eeti_ts", 0 },
  218. { }
  219. };
  220. MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
  221. #ifdef CONFIG_OF
  222. static const struct of_device_id of_eeti_ts_match[] = {
  223. { .compatible = "eeti,exc3000-i2c", },
  224. { }
  225. };
  226. #endif
  227. static struct i2c_driver eeti_ts_driver = {
  228. .driver = {
  229. .name = "eeti_ts",
  230. .pm = &eeti_ts_pm,
  231. .of_match_table = of_match_ptr(of_eeti_ts_match),
  232. },
  233. .probe = eeti_ts_probe,
  234. .id_table = eeti_ts_id,
  235. };
  236. module_i2c_driver(eeti_ts_driver);
  237. MODULE_DESCRIPTION("EETI Touchscreen driver");
  238. MODULE_AUTHOR("Daniel Mack <[email protected]>");
  239. MODULE_LICENSE("GPL");