egalax_ts.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. /*
  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. int gpio = irq_to_gpio(client->irq);
  104. int ret;
  105. ret = gpio_request(gpio, "egalax_irq");
  106. if (ret < 0) {
  107. dev_err(&client->dev,
  108. "request gpio failed, cannot wake up controller: %d\n",
  109. ret);
  110. return ret;
  111. }
  112. /* wake up controller via an falling edge on IRQ gpio. */
  113. gpio_direction_output(gpio, 0);
  114. gpio_set_value(gpio, 1);
  115. /* controller should be waken up, return irq. */
  116. gpio_direction_input(gpio);
  117. gpio_free(gpio);
  118. return 0;
  119. }
  120. static int __devinit egalax_firmware_version(struct i2c_client *client)
  121. {
  122. static const u8 cmd[MAX_I2C_DATA_LEN] = { 0x03, 0x03, 0xa, 0x01, 0x41 };
  123. int ret;
  124. ret = i2c_master_send(client, cmd, MAX_I2C_DATA_LEN);
  125. if (ret < 0)
  126. return ret;
  127. return 0;
  128. }
  129. static int __devinit egalax_ts_probe(struct i2c_client *client,
  130. const struct i2c_device_id *id)
  131. {
  132. struct egalax_ts *ts;
  133. struct input_dev *input_dev;
  134. int ret;
  135. int error;
  136. ts = kzalloc(sizeof(struct egalax_ts), GFP_KERNEL);
  137. if (!ts) {
  138. dev_err(&client->dev, "Failed to allocate memory\n");
  139. return -ENOMEM;
  140. }
  141. input_dev = input_allocate_device();
  142. if (!input_dev) {
  143. dev_err(&client->dev, "Failed to allocate memory\n");
  144. error = -ENOMEM;
  145. goto err_free_ts;
  146. }
  147. ts->client = client;
  148. ts->input_dev = input_dev;
  149. /* controller may be in sleep, wake it up. */
  150. egalax_wake_up_device(client);
  151. ret = egalax_firmware_version(client);
  152. if (ret < 0) {
  153. dev_err(&client->dev, "Failed to read firmware version\n");
  154. error = -EIO;
  155. goto err_free_dev;
  156. }
  157. input_dev->name = "EETI eGalax Touch Screen";
  158. input_dev->id.bustype = BUS_I2C;
  159. input_dev->dev.parent = &client->dev;
  160. __set_bit(EV_ABS, input_dev->evbit);
  161. __set_bit(EV_KEY, input_dev->evbit);
  162. __set_bit(BTN_TOUCH, input_dev->keybit);
  163. input_set_abs_params(input_dev, ABS_X, 0, EGALAX_MAX_X, 0, 0);
  164. input_set_abs_params(input_dev, ABS_Y, 0, EGALAX_MAX_Y, 0, 0);
  165. input_set_abs_params(input_dev,
  166. ABS_MT_POSITION_X, 0, EGALAX_MAX_X, 0, 0);
  167. input_set_abs_params(input_dev,
  168. ABS_MT_POSITION_X, 0, EGALAX_MAX_Y, 0, 0);
  169. input_mt_init_slots(input_dev, MAX_SUPPORT_POINTS);
  170. input_set_drvdata(input_dev, ts);
  171. error = request_threaded_irq(client->irq, NULL, egalax_ts_interrupt,
  172. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  173. "egalax_ts", ts);
  174. if (error < 0) {
  175. dev_err(&client->dev, "Failed to register interrupt\n");
  176. goto err_free_dev;
  177. }
  178. error = input_register_device(ts->input_dev);
  179. if (error)
  180. goto err_free_irq;
  181. i2c_set_clientdata(client, ts);
  182. return 0;
  183. err_free_irq:
  184. free_irq(client->irq, ts);
  185. err_free_dev:
  186. input_free_device(input_dev);
  187. err_free_ts:
  188. kfree(ts);
  189. return error;
  190. }
  191. static __devexit int egalax_ts_remove(struct i2c_client *client)
  192. {
  193. struct egalax_ts *ts = i2c_get_clientdata(client);
  194. free_irq(client->irq, ts);
  195. input_unregister_device(ts->input_dev);
  196. kfree(ts);
  197. return 0;
  198. }
  199. static const struct i2c_device_id egalax_ts_id[] = {
  200. { "egalax_ts", 0 },
  201. { }
  202. };
  203. MODULE_DEVICE_TABLE(i2c, egalax_ts_id);
  204. #ifdef CONFIG_PM_SLEEP
  205. static int egalax_ts_suspend(struct device *dev)
  206. {
  207. static const u8 suspend_cmd[MAX_I2C_DATA_LEN] = {
  208. 0x3, 0x6, 0xa, 0x3, 0x36, 0x3f, 0x2, 0, 0, 0
  209. };
  210. struct i2c_client *client = to_i2c_client(dev);
  211. int ret;
  212. ret = i2c_master_send(client, suspend_cmd, MAX_I2C_DATA_LEN);
  213. return ret > 0 ? 0 : ret;
  214. }
  215. static int egalax_ts_resume(struct device *dev)
  216. {
  217. struct i2c_client *client = to_i2c_client(dev);
  218. return egalax_wake_up_device(client);
  219. }
  220. #endif
  221. static SIMPLE_DEV_PM_OPS(egalax_ts_pm_ops, egalax_ts_suspend, egalax_ts_resume);
  222. static struct i2c_driver egalax_ts_driver = {
  223. .driver = {
  224. .name = "egalax_ts",
  225. .owner = THIS_MODULE,
  226. .pm = &egalax_ts_pm_ops,
  227. },
  228. .id_table = egalax_ts_id,
  229. .probe = egalax_ts_probe,
  230. .remove = __devexit_p(egalax_ts_remove),
  231. };
  232. module_i2c_driver(egalax_ts_driver);
  233. MODULE_AUTHOR("Freescale Semiconductor, Inc.");
  234. MODULE_DESCRIPTION("Touchscreen driver for EETI eGalax touch controller");
  235. MODULE_LICENSE("GPL");