egalax_ts.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 ret;
  142. int error;
  143. ts = kzalloc(sizeof(struct egalax_ts), GFP_KERNEL);
  144. if (!ts) {
  145. dev_err(&client->dev, "Failed to allocate memory\n");
  146. return -ENOMEM;
  147. }
  148. input_dev = input_allocate_device();
  149. if (!input_dev) {
  150. dev_err(&client->dev, "Failed to allocate memory\n");
  151. error = -ENOMEM;
  152. goto err_free_ts;
  153. }
  154. ts->client = client;
  155. ts->input_dev = input_dev;
  156. /* controller may be in sleep, wake it up. */
  157. error = egalax_wake_up_device(client);
  158. if (error) {
  159. dev_err(&client->dev, "Failed to wake up the controller\n");
  160. goto err_free_dev;
  161. }
  162. ret = egalax_firmware_version(client);
  163. if (ret < 0) {
  164. dev_err(&client->dev, "Failed to read firmware version\n");
  165. error = -EIO;
  166. goto err_free_dev;
  167. }
  168. input_dev->name = "EETI eGalax Touch Screen";
  169. input_dev->id.bustype = BUS_I2C;
  170. input_dev->dev.parent = &client->dev;
  171. __set_bit(EV_ABS, input_dev->evbit);
  172. __set_bit(EV_KEY, input_dev->evbit);
  173. __set_bit(BTN_TOUCH, input_dev->keybit);
  174. input_set_abs_params(input_dev, ABS_X, 0, EGALAX_MAX_X, 0, 0);
  175. input_set_abs_params(input_dev, ABS_Y, 0, EGALAX_MAX_Y, 0, 0);
  176. input_set_abs_params(input_dev,
  177. ABS_MT_POSITION_X, 0, EGALAX_MAX_X, 0, 0);
  178. input_set_abs_params(input_dev,
  179. ABS_MT_POSITION_X, 0, EGALAX_MAX_Y, 0, 0);
  180. input_mt_init_slots(input_dev, MAX_SUPPORT_POINTS, 0);
  181. input_set_drvdata(input_dev, ts);
  182. error = request_threaded_irq(client->irq, NULL, egalax_ts_interrupt,
  183. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  184. "egalax_ts", ts);
  185. if (error < 0) {
  186. dev_err(&client->dev, "Failed to register interrupt\n");
  187. goto err_free_dev;
  188. }
  189. error = input_register_device(ts->input_dev);
  190. if (error)
  191. goto err_free_irq;
  192. i2c_set_clientdata(client, ts);
  193. return 0;
  194. err_free_irq:
  195. free_irq(client->irq, ts);
  196. err_free_dev:
  197. input_free_device(input_dev);
  198. err_free_ts:
  199. kfree(ts);
  200. return error;
  201. }
  202. static int egalax_ts_remove(struct i2c_client *client)
  203. {
  204. struct egalax_ts *ts = i2c_get_clientdata(client);
  205. free_irq(client->irq, ts);
  206. input_unregister_device(ts->input_dev);
  207. kfree(ts);
  208. return 0;
  209. }
  210. static const struct i2c_device_id egalax_ts_id[] = {
  211. { "egalax_ts", 0 },
  212. { }
  213. };
  214. MODULE_DEVICE_TABLE(i2c, egalax_ts_id);
  215. #ifdef CONFIG_PM_SLEEP
  216. static int egalax_ts_suspend(struct device *dev)
  217. {
  218. static const u8 suspend_cmd[MAX_I2C_DATA_LEN] = {
  219. 0x3, 0x6, 0xa, 0x3, 0x36, 0x3f, 0x2, 0, 0, 0
  220. };
  221. struct i2c_client *client = to_i2c_client(dev);
  222. int ret;
  223. ret = i2c_master_send(client, suspend_cmd, MAX_I2C_DATA_LEN);
  224. return ret > 0 ? 0 : ret;
  225. }
  226. static int egalax_ts_resume(struct device *dev)
  227. {
  228. struct i2c_client *client = to_i2c_client(dev);
  229. return egalax_wake_up_device(client);
  230. }
  231. #endif
  232. static SIMPLE_DEV_PM_OPS(egalax_ts_pm_ops, egalax_ts_suspend, egalax_ts_resume);
  233. static struct of_device_id egalax_ts_dt_ids[] = {
  234. { .compatible = "eeti,egalax_ts" },
  235. { /* sentinel */ }
  236. };
  237. static struct i2c_driver egalax_ts_driver = {
  238. .driver = {
  239. .name = "egalax_ts",
  240. .owner = THIS_MODULE,
  241. .pm = &egalax_ts_pm_ops,
  242. .of_match_table = of_match_ptr(egalax_ts_dt_ids),
  243. },
  244. .id_table = egalax_ts_id,
  245. .probe = egalax_ts_probe,
  246. .remove = egalax_ts_remove,
  247. };
  248. module_i2c_driver(egalax_ts_driver);
  249. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  250. MODULE_DESCRIPTION("Touchscreen driver for EETI eGalax touch controller");
  251. MODULE_LICENSE("GPL");