egalax_ts.c 6.9 KB

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