egalax_ts.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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/module.h>
  15. #include <linux/i2c.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/input.h>
  18. #include <linux/irq.h>
  19. #include <linux/gpio.h>
  20. #include <linux/delay.h>
  21. #include <linux/slab.h>
  22. #include <linux/bitops.h>
  23. #include <linux/input/mt.h>
  24. #include <linux/of_gpio.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 device_node *np = client->dev.of_node;
  101. int gpio;
  102. int ret;
  103. if (!np)
  104. return -ENODEV;
  105. gpio = of_get_named_gpio(np, "wakeup-gpios", 0);
  106. if (!gpio_is_valid(gpio))
  107. return -ENODEV;
  108. ret = gpio_request(gpio, "egalax_irq");
  109. if (ret < 0) {
  110. dev_err(&client->dev,
  111. "request gpio failed, cannot wake up controller: %d\n",
  112. ret);
  113. return ret;
  114. }
  115. /* wake up controller via an falling edge on IRQ gpio. */
  116. gpio_direction_output(gpio, 0);
  117. gpio_set_value(gpio, 1);
  118. /* controller should be waken up, return irq. */
  119. gpio_direction_input(gpio);
  120. gpio_free(gpio);
  121. return 0;
  122. }
  123. static int egalax_firmware_version(struct i2c_client *client)
  124. {
  125. static const u8 cmd[MAX_I2C_DATA_LEN] = { 0x03, 0x03, 0xa, 0x01, 0x41 };
  126. int ret;
  127. ret = i2c_master_send(client, cmd, MAX_I2C_DATA_LEN);
  128. if (ret < 0)
  129. return ret;
  130. return 0;
  131. }
  132. static int egalax_ts_probe(struct i2c_client *client,
  133. const struct i2c_device_id *id)
  134. {
  135. struct egalax_ts *ts;
  136. struct input_dev *input_dev;
  137. int error;
  138. ts = devm_kzalloc(&client->dev, sizeof(struct egalax_ts), GFP_KERNEL);
  139. if (!ts) {
  140. dev_err(&client->dev, "Failed to allocate memory\n");
  141. return -ENOMEM;
  142. }
  143. input_dev = devm_input_allocate_device(&client->dev);
  144. if (!input_dev) {
  145. dev_err(&client->dev, "Failed to allocate memory\n");
  146. return -ENOMEM;
  147. }
  148. ts->client = client;
  149. ts->input_dev = input_dev;
  150. /* controller may be in sleep, wake it up. */
  151. error = egalax_wake_up_device(client);
  152. if (error) {
  153. dev_err(&client->dev, "Failed to wake up the controller\n");
  154. return error;
  155. }
  156. error = egalax_firmware_version(client);
  157. if (error < 0) {
  158. dev_err(&client->dev, "Failed to read firmware version\n");
  159. return error;
  160. }
  161. input_dev->name = "EETI eGalax Touch Screen";
  162. input_dev->id.bustype = BUS_I2C;
  163. __set_bit(EV_ABS, input_dev->evbit);
  164. __set_bit(EV_KEY, input_dev->evbit);
  165. __set_bit(BTN_TOUCH, input_dev->keybit);
  166. input_set_abs_params(input_dev, ABS_X, 0, EGALAX_MAX_X, 0, 0);
  167. input_set_abs_params(input_dev, ABS_Y, 0, EGALAX_MAX_Y, 0, 0);
  168. input_set_abs_params(input_dev,
  169. ABS_MT_POSITION_X, 0, EGALAX_MAX_X, 0, 0);
  170. input_set_abs_params(input_dev,
  171. ABS_MT_POSITION_Y, 0, EGALAX_MAX_Y, 0, 0);
  172. input_mt_init_slots(input_dev, MAX_SUPPORT_POINTS, 0);
  173. error = devm_request_threaded_irq(&client->dev, client->irq, NULL,
  174. egalax_ts_interrupt,
  175. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  176. "egalax_ts", ts);
  177. if (error < 0) {
  178. dev_err(&client->dev, "Failed to register interrupt\n");
  179. return error;
  180. }
  181. error = input_register_device(ts->input_dev);
  182. if (error)
  183. return error;
  184. return 0;
  185. }
  186. static const struct i2c_device_id egalax_ts_id[] = {
  187. { "egalax_ts", 0 },
  188. { }
  189. };
  190. MODULE_DEVICE_TABLE(i2c, egalax_ts_id);
  191. static int __maybe_unused egalax_ts_suspend(struct device *dev)
  192. {
  193. static const u8 suspend_cmd[MAX_I2C_DATA_LEN] = {
  194. 0x3, 0x6, 0xa, 0x3, 0x36, 0x3f, 0x2, 0, 0, 0
  195. };
  196. struct i2c_client *client = to_i2c_client(dev);
  197. int ret;
  198. if (device_may_wakeup(dev))
  199. return enable_irq_wake(client->irq);
  200. ret = i2c_master_send(client, suspend_cmd, MAX_I2C_DATA_LEN);
  201. return ret > 0 ? 0 : ret;
  202. }
  203. static int __maybe_unused egalax_ts_resume(struct device *dev)
  204. {
  205. struct i2c_client *client = to_i2c_client(dev);
  206. if (device_may_wakeup(dev))
  207. return disable_irq_wake(client->irq);
  208. return egalax_wake_up_device(client);
  209. }
  210. static SIMPLE_DEV_PM_OPS(egalax_ts_pm_ops, egalax_ts_suspend, egalax_ts_resume);
  211. static const struct of_device_id egalax_ts_dt_ids[] = {
  212. { .compatible = "eeti,egalax_ts" },
  213. { /* sentinel */ }
  214. };
  215. MODULE_DEVICE_TABLE(of, egalax_ts_dt_ids);
  216. static struct i2c_driver egalax_ts_driver = {
  217. .driver = {
  218. .name = "egalax_ts",
  219. .pm = &egalax_ts_pm_ops,
  220. .of_match_table = egalax_ts_dt_ids,
  221. },
  222. .id_table = egalax_ts_id,
  223. .probe = egalax_ts_probe,
  224. };
  225. module_i2c_driver(egalax_ts_driver);
  226. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  227. MODULE_DESCRIPTION("Touchscreen driver for EETI eGalax touch controller");
  228. MODULE_LICENSE("GPL");