egalax_ts.c 6.9 KB

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