egalax_ts.c 6.6 KB

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