egalax_ts.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Driver for EETI eGalax Multiple Touch Controller
  4. *
  5. * Copyright (C) 2011 Freescale Semiconductor, Inc.
  6. *
  7. * based on max11801_ts.c
  8. */
  9. /* EETI eGalax serial touch screen controller is a I2C based multiple
  10. * touch screen controller, it supports 5 point multiple touch. */
  11. /* TODO:
  12. - auto idle mode support
  13. */
  14. #include <linux/err.h>
  15. #include <linux/module.h>
  16. #include <linux/i2c.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/input.h>
  19. #include <linux/irq.h>
  20. #include <linux/gpio/consumer.h>
  21. #include <linux/delay.h>
  22. #include <linux/slab.h>
  23. #include <linux/string_choices.h>
  24. #include <linux/bitops.h>
  25. #include <linux/input/mt.h>
  26. /*
  27. * Mouse Mode: some panel may configure the controller to mouse mode,
  28. * which can only report one point at a given time.
  29. * This driver will ignore events in this mode.
  30. */
  31. #define REPORT_MODE_MOUSE 0x1
  32. /*
  33. * Vendor Mode: this mode is used to transfer some vendor specific
  34. * messages.
  35. * This driver will ignore events in this mode.
  36. */
  37. #define REPORT_MODE_VENDOR 0x3
  38. /* Multiple Touch Mode */
  39. #define REPORT_MODE_MTTOUCH 0x4
  40. #define MAX_SUPPORT_POINTS 5
  41. #define EVENT_VALID_OFFSET 7
  42. #define EVENT_VALID_MASK (0x1 << EVENT_VALID_OFFSET)
  43. #define EVENT_ID_OFFSET 2
  44. #define EVENT_ID_MASK (0xf << EVENT_ID_OFFSET)
  45. #define EVENT_IN_RANGE (0x1 << 1)
  46. #define EVENT_DOWN_UP (0X1 << 0)
  47. #define MAX_I2C_DATA_LEN 10
  48. #define EGALAX_MAX_X 32760
  49. #define EGALAX_MAX_Y 32760
  50. #define EGALAX_MAX_TRIES 100
  51. struct egalax_ts {
  52. struct i2c_client *client;
  53. struct input_dev *input_dev;
  54. };
  55. static irqreturn_t egalax_ts_interrupt(int irq, void *dev_id)
  56. {
  57. struct egalax_ts *ts = dev_id;
  58. struct input_dev *input_dev = ts->input_dev;
  59. struct i2c_client *client = ts->client;
  60. u8 buf[MAX_I2C_DATA_LEN];
  61. int id, ret, x, y, z;
  62. int tries = 0;
  63. bool down, valid;
  64. u8 state;
  65. do {
  66. ret = i2c_master_recv(client, buf, MAX_I2C_DATA_LEN);
  67. } while (ret == -EAGAIN && tries++ < EGALAX_MAX_TRIES);
  68. if (ret < 0)
  69. return IRQ_HANDLED;
  70. if (buf[0] != REPORT_MODE_MTTOUCH) {
  71. /* ignore mouse events and vendor events */
  72. return IRQ_HANDLED;
  73. }
  74. state = buf[1];
  75. x = (buf[3] << 8) | buf[2];
  76. y = (buf[5] << 8) | buf[4];
  77. z = (buf[7] << 8) | buf[6];
  78. valid = state & EVENT_VALID_MASK;
  79. id = (state & EVENT_ID_MASK) >> EVENT_ID_OFFSET;
  80. down = state & EVENT_DOWN_UP;
  81. if (!valid || id > MAX_SUPPORT_POINTS) {
  82. dev_dbg(&client->dev, "point invalid\n");
  83. return IRQ_HANDLED;
  84. }
  85. input_mt_slot(input_dev, id);
  86. input_mt_report_slot_state(input_dev, MT_TOOL_FINGER, down);
  87. dev_dbg(&client->dev, "%s id:%d x:%d y:%d z:%d",
  88. str_down_up(down), id, x, y, z);
  89. if (down) {
  90. input_report_abs(input_dev, ABS_MT_POSITION_X, x);
  91. input_report_abs(input_dev, ABS_MT_POSITION_Y, y);
  92. input_report_abs(input_dev, ABS_MT_PRESSURE, z);
  93. }
  94. input_mt_report_pointer_emulation(input_dev, true);
  95. input_sync(input_dev);
  96. return IRQ_HANDLED;
  97. }
  98. /* wake up controller by an falling edge of interrupt gpio. */
  99. static int egalax_wake_up_device(struct i2c_client *client)
  100. {
  101. struct gpio_desc *gpio;
  102. int ret;
  103. /* wake up controller via an falling edge on IRQ gpio. */
  104. gpio = gpiod_get(&client->dev, "wakeup", GPIOD_OUT_HIGH);
  105. ret = PTR_ERR_OR_ZERO(gpio);
  106. if (ret) {
  107. if (ret != -EPROBE_DEFER)
  108. dev_err(&client->dev,
  109. "failed to request wakeup gpio, cannot wake up controller: %d\n",
  110. ret);
  111. return ret;
  112. }
  113. /* release the line */
  114. gpiod_set_value_cansleep(gpio, 0);
  115. /* controller should be woken up, return irq. */
  116. gpiod_direction_input(gpio);
  117. gpiod_put(gpio);
  118. return 0;
  119. }
  120. static int egalax_firmware_version(struct i2c_client *client)
  121. {
  122. static const u8 cmd[MAX_I2C_DATA_LEN] = { 0x03, 0x03, 0xa, 0x01, 0x41 };
  123. int ret;
  124. ret = i2c_master_send(client, cmd, MAX_I2C_DATA_LEN);
  125. if (ret < 0)
  126. return ret;
  127. return 0;
  128. }
  129. static int egalax_ts_probe(struct i2c_client *client)
  130. {
  131. struct egalax_ts *ts;
  132. struct input_dev *input_dev;
  133. int error;
  134. ts = devm_kzalloc(&client->dev, sizeof(struct egalax_ts), GFP_KERNEL);
  135. if (!ts) {
  136. dev_err(&client->dev, "Failed to allocate memory\n");
  137. return -ENOMEM;
  138. }
  139. input_dev = devm_input_allocate_device(&client->dev);
  140. if (!input_dev) {
  141. dev_err(&client->dev, "Failed to allocate memory\n");
  142. return -ENOMEM;
  143. }
  144. ts->client = client;
  145. ts->input_dev = input_dev;
  146. /* controller may be in sleep, wake it up. */
  147. error = egalax_wake_up_device(client);
  148. if (error)
  149. return error;
  150. error = egalax_firmware_version(client);
  151. if (error < 0) {
  152. dev_err(&client->dev, "Failed to read firmware version\n");
  153. return error;
  154. }
  155. input_dev->name = "EETI eGalax Touch Screen";
  156. input_dev->id.bustype = BUS_I2C;
  157. __set_bit(EV_ABS, input_dev->evbit);
  158. __set_bit(EV_KEY, input_dev->evbit);
  159. __set_bit(BTN_TOUCH, input_dev->keybit);
  160. input_set_abs_params(input_dev, ABS_X, 0, EGALAX_MAX_X, 0, 0);
  161. input_set_abs_params(input_dev, ABS_Y, 0, EGALAX_MAX_Y, 0, 0);
  162. input_set_abs_params(input_dev,
  163. ABS_MT_POSITION_X, 0, EGALAX_MAX_X, 0, 0);
  164. input_set_abs_params(input_dev,
  165. ABS_MT_POSITION_Y, 0, EGALAX_MAX_Y, 0, 0);
  166. input_mt_init_slots(input_dev, MAX_SUPPORT_POINTS, 0);
  167. error = devm_request_threaded_irq(&client->dev, client->irq,
  168. NULL, egalax_ts_interrupt,
  169. IRQF_ONESHOT, "egalax_ts", ts);
  170. if (error < 0) {
  171. dev_err(&client->dev, "Failed to register interrupt\n");
  172. return error;
  173. }
  174. error = input_register_device(ts->input_dev);
  175. if (error)
  176. return error;
  177. return 0;
  178. }
  179. static const struct i2c_device_id egalax_ts_id[] = {
  180. { "egalax_ts" },
  181. { }
  182. };
  183. MODULE_DEVICE_TABLE(i2c, egalax_ts_id);
  184. static int egalax_ts_suspend(struct device *dev)
  185. {
  186. static const u8 suspend_cmd[MAX_I2C_DATA_LEN] = {
  187. 0x3, 0x6, 0xa, 0x3, 0x36, 0x3f, 0x2, 0, 0, 0
  188. };
  189. struct i2c_client *client = to_i2c_client(dev);
  190. int ret;
  191. if (device_may_wakeup(dev))
  192. return enable_irq_wake(client->irq);
  193. ret = i2c_master_send(client, suspend_cmd, MAX_I2C_DATA_LEN);
  194. return ret > 0 ? 0 : ret;
  195. }
  196. static int egalax_ts_resume(struct device *dev)
  197. {
  198. struct i2c_client *client = to_i2c_client(dev);
  199. if (device_may_wakeup(dev))
  200. return disable_irq_wake(client->irq);
  201. return egalax_wake_up_device(client);
  202. }
  203. static DEFINE_SIMPLE_DEV_PM_OPS(egalax_ts_pm_ops,
  204. egalax_ts_suspend, egalax_ts_resume);
  205. static const struct of_device_id egalax_ts_dt_ids[] = {
  206. { .compatible = "eeti,egalax_ts" },
  207. { /* sentinel */ }
  208. };
  209. MODULE_DEVICE_TABLE(of, egalax_ts_dt_ids);
  210. static struct i2c_driver egalax_ts_driver = {
  211. .driver = {
  212. .name = "egalax_ts",
  213. .pm = pm_sleep_ptr(&egalax_ts_pm_ops),
  214. .of_match_table = egalax_ts_dt_ids,
  215. },
  216. .id_table = egalax_ts_id,
  217. .probe = egalax_ts_probe,
  218. };
  219. module_i2c_driver(egalax_ts_driver);
  220. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  221. MODULE_DESCRIPTION("Touchscreen driver for EETI eGalax touch controller");
  222. MODULE_LICENSE("GPL");