eeti_ts.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 <daniel@zonque.org>
  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 <ujjwal@kenati.com>
  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 <linux/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. {
  132. struct device *dev = &client->dev;
  133. struct eeti_ts *eeti;
  134. struct input_dev *input;
  135. int error;
  136. /*
  137. * In contrast to what's described in the datasheet, there seems
  138. * to be no way of probing the presence of that device using I2C
  139. * commands. So we need to blindly believe it is there, and wait
  140. * for interrupts to occur.
  141. */
  142. eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL);
  143. if (!eeti) {
  144. dev_err(dev, "failed to allocate driver data\n");
  145. return -ENOMEM;
  146. }
  147. mutex_init(&eeti->mutex);
  148. input = devm_input_allocate_device(dev);
  149. if (!input) {
  150. dev_err(dev, "Failed to allocate input device.\n");
  151. return -ENOMEM;
  152. }
  153. input_set_capability(input, EV_KEY, BTN_TOUCH);
  154. input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
  155. input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
  156. input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
  157. touchscreen_parse_properties(input, false, &eeti->props);
  158. input->name = client->name;
  159. input->id.bustype = BUS_I2C;
  160. input->open = eeti_ts_open;
  161. input->close = eeti_ts_close;
  162. eeti->client = client;
  163. eeti->input = input;
  164. eeti->attn_gpio = devm_gpiod_get_optional(dev, "attn", GPIOD_IN);
  165. if (IS_ERR(eeti->attn_gpio))
  166. return PTR_ERR(eeti->attn_gpio);
  167. i2c_set_clientdata(client, eeti);
  168. input_set_drvdata(input, eeti);
  169. error = devm_request_threaded_irq(dev, client->irq,
  170. NULL, eeti_ts_isr,
  171. IRQF_ONESHOT,
  172. client->name, eeti);
  173. if (error) {
  174. dev_err(dev, "Unable to request touchscreen IRQ: %d\n",
  175. error);
  176. return error;
  177. }
  178. /*
  179. * Disable the device for now. It will be enabled once the
  180. * input device is opened.
  181. */
  182. eeti_ts_stop(eeti);
  183. error = input_register_device(input);
  184. if (error)
  185. return error;
  186. return 0;
  187. }
  188. static int eeti_ts_suspend(struct device *dev)
  189. {
  190. struct i2c_client *client = to_i2c_client(dev);
  191. struct eeti_ts *eeti = i2c_get_clientdata(client);
  192. struct input_dev *input_dev = eeti->input;
  193. mutex_lock(&input_dev->mutex);
  194. if (input_device_enabled(input_dev))
  195. eeti_ts_stop(eeti);
  196. mutex_unlock(&input_dev->mutex);
  197. if (device_may_wakeup(&client->dev))
  198. enable_irq_wake(client->irq);
  199. return 0;
  200. }
  201. static int eeti_ts_resume(struct device *dev)
  202. {
  203. struct i2c_client *client = to_i2c_client(dev);
  204. struct eeti_ts *eeti = i2c_get_clientdata(client);
  205. struct input_dev *input_dev = eeti->input;
  206. if (device_may_wakeup(&client->dev))
  207. disable_irq_wake(client->irq);
  208. mutex_lock(&input_dev->mutex);
  209. if (input_device_enabled(input_dev))
  210. eeti_ts_start(eeti);
  211. mutex_unlock(&input_dev->mutex);
  212. return 0;
  213. }
  214. static DEFINE_SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
  215. static const struct i2c_device_id eeti_ts_id[] = {
  216. { "eeti_ts" },
  217. { }
  218. };
  219. MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
  220. #ifdef CONFIG_OF
  221. static const struct of_device_id of_eeti_ts_match[] = {
  222. { .compatible = "eeti,exc3000-i2c", },
  223. { }
  224. };
  225. #endif
  226. static struct i2c_driver eeti_ts_driver = {
  227. .driver = {
  228. .name = "eeti_ts",
  229. .pm = pm_sleep_ptr(&eeti_ts_pm),
  230. .of_match_table = of_match_ptr(of_eeti_ts_match),
  231. },
  232. .probe = eeti_ts_probe,
  233. .id_table = eeti_ts_id,
  234. };
  235. module_i2c_driver(eeti_ts_driver);
  236. MODULE_DESCRIPTION("EETI Touchscreen driver");
  237. MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>");
  238. MODULE_LICENSE("GPL");