eeti_ts.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Touch Screen driver for EETI's I2C connected touch screen panels
  3. * Copyright (c) 2009,2018 Daniel Mack <daniel@zonque.org>
  4. *
  5. * See EETI's software guide for the protocol specification:
  6. * http://home.eeti.com.tw/documentation.html
  7. *
  8. * Based on migor_ts.c
  9. * Copyright (c) 2008 Magnus Damm
  10. * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>
  11. *
  12. * This file is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2 of the License, or (at your option) any later version.
  16. *
  17. * This file is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public
  23. * License along with this library; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #include <linux/module.h>
  27. #include <linux/kernel.h>
  28. #include <linux/input.h>
  29. #include <linux/input/touchscreen.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/i2c.h>
  32. #include <linux/timer.h>
  33. #include <linux/gpio/consumer.h>
  34. #include <linux/of.h>
  35. #include <linux/slab.h>
  36. #include <asm/unaligned.h>
  37. struct eeti_ts {
  38. struct i2c_client *client;
  39. struct input_dev *input;
  40. struct gpio_desc *attn_gpio;
  41. struct touchscreen_properties props;
  42. bool running;
  43. };
  44. #define EETI_TS_BITDEPTH (11)
  45. #define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1)
  46. #define REPORT_BIT_PRESSED BIT(0)
  47. #define REPORT_BIT_AD0 BIT(1)
  48. #define REPORT_BIT_AD1 BIT(2)
  49. #define REPORT_BIT_HAS_PRESSURE BIT(6)
  50. #define REPORT_RES_BITS(v) (((v) >> 1) + EETI_TS_BITDEPTH)
  51. static void eeti_ts_report_event(struct eeti_ts *eeti, u8 *buf)
  52. {
  53. unsigned int res;
  54. u16 x, y;
  55. res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
  56. x = get_unaligned_be16(&buf[1]);
  57. y = get_unaligned_be16(&buf[3]);
  58. /* fix the range to 11 bits */
  59. x >>= res - EETI_TS_BITDEPTH;
  60. y >>= res - EETI_TS_BITDEPTH;
  61. if (buf[0] & REPORT_BIT_HAS_PRESSURE)
  62. input_report_abs(eeti->input, ABS_PRESSURE, buf[5]);
  63. touchscreen_report_pos(eeti->input, &eeti->props, x, y, false);
  64. input_report_key(eeti->input, BTN_TOUCH, buf[0] & REPORT_BIT_PRESSED);
  65. input_sync(eeti->input);
  66. }
  67. static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
  68. {
  69. struct eeti_ts *eeti = dev_id;
  70. int len;
  71. int error;
  72. char buf[6];
  73. do {
  74. len = i2c_master_recv(eeti->client, buf, sizeof(buf));
  75. if (len != sizeof(buf)) {
  76. error = len < 0 ? len : -EIO;
  77. dev_err(&eeti->client->dev,
  78. "failed to read touchscreen data: %d\n",
  79. error);
  80. break;
  81. }
  82. if (buf[0] & 0x80) {
  83. /* Motion packet */
  84. eeti_ts_report_event(eeti, buf);
  85. }
  86. } while (eeti->running &&
  87. eeti->attn_gpio && gpiod_get_value_cansleep(eeti->attn_gpio));
  88. return IRQ_HANDLED;
  89. }
  90. static void eeti_ts_start(struct eeti_ts *eeti)
  91. {
  92. eeti->running = true;
  93. wmb();
  94. enable_irq(eeti->client->irq);
  95. }
  96. static void eeti_ts_stop(struct eeti_ts *eeti)
  97. {
  98. eeti->running = false;
  99. wmb();
  100. disable_irq(eeti->client->irq);
  101. }
  102. static int eeti_ts_open(struct input_dev *dev)
  103. {
  104. struct eeti_ts *eeti = input_get_drvdata(dev);
  105. eeti_ts_start(eeti);
  106. return 0;
  107. }
  108. static void eeti_ts_close(struct input_dev *dev)
  109. {
  110. struct eeti_ts *eeti = input_get_drvdata(dev);
  111. eeti_ts_stop(eeti);
  112. }
  113. static int eeti_ts_probe(struct i2c_client *client,
  114. const struct i2c_device_id *idp)
  115. {
  116. struct device *dev = &client->dev;
  117. struct eeti_ts *eeti;
  118. struct input_dev *input;
  119. int error;
  120. /*
  121. * In contrast to what's described in the datasheet, there seems
  122. * to be no way of probing the presence of that device using I2C
  123. * commands. So we need to blindly believe it is there, and wait
  124. * for interrupts to occur.
  125. */
  126. eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL);
  127. if (!eeti) {
  128. dev_err(dev, "failed to allocate driver data\n");
  129. return -ENOMEM;
  130. }
  131. input = devm_input_allocate_device(dev);
  132. if (!input) {
  133. dev_err(dev, "Failed to allocate input device.\n");
  134. return -ENOMEM;
  135. }
  136. input_set_capability(input, EV_KEY, BTN_TOUCH);
  137. input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
  138. input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
  139. input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
  140. touchscreen_parse_properties(input, false, &eeti->props);
  141. input->name = client->name;
  142. input->id.bustype = BUS_I2C;
  143. input->open = eeti_ts_open;
  144. input->close = eeti_ts_close;
  145. eeti->client = client;
  146. eeti->input = input;
  147. eeti->attn_gpio = devm_gpiod_get_optional(dev, "attn", GPIOD_IN);
  148. if (IS_ERR(eeti->attn_gpio))
  149. return PTR_ERR(eeti->attn_gpio);
  150. i2c_set_clientdata(client, eeti);
  151. input_set_drvdata(input, eeti);
  152. error = devm_request_threaded_irq(dev, client->irq,
  153. NULL, eeti_ts_isr,
  154. IRQF_ONESHOT,
  155. client->name, eeti);
  156. if (error) {
  157. dev_err(dev, "Unable to request touchscreen IRQ: %d\n",
  158. error);
  159. return error;
  160. }
  161. /*
  162. * Disable the device for now. It will be enabled once the
  163. * input device is opened.
  164. */
  165. eeti_ts_stop(eeti);
  166. error = input_register_device(input);
  167. if (error)
  168. return error;
  169. return 0;
  170. }
  171. static int __maybe_unused eeti_ts_suspend(struct device *dev)
  172. {
  173. struct i2c_client *client = to_i2c_client(dev);
  174. struct eeti_ts *eeti = i2c_get_clientdata(client);
  175. struct input_dev *input_dev = eeti->input;
  176. mutex_lock(&input_dev->mutex);
  177. if (input_dev->users)
  178. eeti_ts_stop(eeti);
  179. mutex_unlock(&input_dev->mutex);
  180. if (device_may_wakeup(&client->dev))
  181. enable_irq_wake(client->irq);
  182. return 0;
  183. }
  184. static int __maybe_unused eeti_ts_resume(struct device *dev)
  185. {
  186. struct i2c_client *client = to_i2c_client(dev);
  187. struct eeti_ts *eeti = i2c_get_clientdata(client);
  188. struct input_dev *input_dev = eeti->input;
  189. if (device_may_wakeup(&client->dev))
  190. disable_irq_wake(client->irq);
  191. mutex_lock(&input_dev->mutex);
  192. if (input_dev->users)
  193. eeti_ts_start(eeti);
  194. mutex_unlock(&input_dev->mutex);
  195. return 0;
  196. }
  197. static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
  198. static const struct i2c_device_id eeti_ts_id[] = {
  199. { "eeti_ts", 0 },
  200. { }
  201. };
  202. MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
  203. #ifdef CONFIG_OF
  204. static const struct of_device_id of_eeti_ts_match[] = {
  205. { .compatible = "eeti,exc3000-i2c", },
  206. { }
  207. };
  208. #endif
  209. static struct i2c_driver eeti_ts_driver = {
  210. .driver = {
  211. .name = "eeti_ts",
  212. .pm = &eeti_ts_pm,
  213. .of_match_table = of_match_ptr(of_eeti_ts_match),
  214. },
  215. .probe = eeti_ts_probe,
  216. .id_table = eeti_ts_id,
  217. };
  218. module_i2c_driver(eeti_ts_driver);
  219. MODULE_DESCRIPTION("EETI Touchscreen driver");
  220. MODULE_AUTHOR("Daniel Mack <daniel@zonque.org>");
  221. MODULE_LICENSE("GPL");