eeti_ts.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Touch Screen driver for EETI's I2C connected touch screen panels
  3. * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
  4. *
  5. * See EETI's software guide for the protocol specification:
  6. * http://home.eeti.com.tw/web20/eg/guide.htm
  7. *
  8. * Based on migor_ts.c
  9. * Copyright (c) 2008 Magnus Damm
  10. * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>
  11. *
  12. * This file is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2 of the License, or (at your option) any later version.
  16. *
  17. * This file is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public
  23. * License along with this library; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. */
  26. #include <linux/module.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/kernel.h>
  29. #include <linux/input.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/i2c.h>
  32. #include <linux/timer.h>
  33. #include <linux/gpio/consumer.h>
  34. #include <linux/slab.h>
  35. #include <asm/unaligned.h>
  36. static bool flip_x;
  37. module_param(flip_x, bool, 0644);
  38. MODULE_PARM_DESC(flip_x, "flip x coordinate");
  39. static bool flip_y;
  40. module_param(flip_y, bool, 0644);
  41. MODULE_PARM_DESC(flip_y, "flip y coordinate");
  42. struct eeti_ts {
  43. struct i2c_client *client;
  44. struct input_dev *input;
  45. struct gpio_desc *attn_gpio;
  46. bool running;
  47. };
  48. #define EETI_TS_BITDEPTH (11)
  49. #define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1)
  50. #define REPORT_BIT_PRESSED BIT(0)
  51. #define REPORT_BIT_AD0 BIT(1)
  52. #define REPORT_BIT_AD1 BIT(2)
  53. #define REPORT_BIT_HAS_PRESSURE BIT(6)
  54. #define REPORT_RES_BITS(v) (((v) >> 1) + EETI_TS_BITDEPTH)
  55. static void eeti_ts_report_event(struct eeti_ts *eeti, u8 *buf)
  56. {
  57. unsigned int res;
  58. u16 x, y;
  59. res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
  60. x = get_unaligned_be16(&buf[1]);
  61. y = get_unaligned_be16(&buf[3]);
  62. /* fix the range to 11 bits */
  63. x >>= res - EETI_TS_BITDEPTH;
  64. y >>= res - EETI_TS_BITDEPTH;
  65. if (flip_x)
  66. x = EETI_MAXVAL - x;
  67. if (flip_y)
  68. y = EETI_MAXVAL - y;
  69. if (buf[0] & REPORT_BIT_HAS_PRESSURE)
  70. input_report_abs(eeti->input, ABS_PRESSURE, buf[5]);
  71. input_report_abs(eeti->input, ABS_X, x);
  72. input_report_abs(eeti->input, ABS_Y, y);
  73. input_report_key(eeti->input, BTN_TOUCH, buf[0] & REPORT_BIT_PRESSED);
  74. input_sync(eeti->input);
  75. }
  76. static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
  77. {
  78. struct eeti_ts *eeti = dev_id;
  79. int len;
  80. int error;
  81. char buf[6];
  82. do {
  83. len = i2c_master_recv(eeti->client, buf, sizeof(buf));
  84. if (len != sizeof(buf)) {
  85. error = len < 0 ? len : -EIO;
  86. dev_err(&eeti->client->dev,
  87. "failed to read touchscreen data: %d\n",
  88. error);
  89. break;
  90. }
  91. if (buf[0] & 0x80) {
  92. /* Motion packet */
  93. eeti_ts_report_event(eeti, buf);
  94. }
  95. } while (eeti->running &&
  96. eeti->attn_gpio && gpiod_get_value_cansleep(eeti->attn_gpio));
  97. return IRQ_HANDLED;
  98. }
  99. static void eeti_ts_start(struct eeti_ts *eeti)
  100. {
  101. eeti->running = true;
  102. wmb();
  103. enable_irq(eeti->client->irq);
  104. }
  105. static void eeti_ts_stop(struct eeti_ts *eeti)
  106. {
  107. eeti->running = false;
  108. wmb();
  109. disable_irq(eeti->client->irq);
  110. }
  111. static int eeti_ts_open(struct input_dev *dev)
  112. {
  113. struct eeti_ts *eeti = input_get_drvdata(dev);
  114. eeti_ts_start(eeti);
  115. return 0;
  116. }
  117. static void eeti_ts_close(struct input_dev *dev)
  118. {
  119. struct eeti_ts *eeti = input_get_drvdata(dev);
  120. eeti_ts_stop(eeti);
  121. }
  122. static int eeti_ts_probe(struct i2c_client *client,
  123. const struct i2c_device_id *idp)
  124. {
  125. struct device *dev = &client->dev;
  126. struct eeti_ts *eeti;
  127. struct input_dev *input;
  128. int error;
  129. /*
  130. * In contrast to what's described in the datasheet, there seems
  131. * to be no way of probing the presence of that device using I2C
  132. * commands. So we need to blindly believe it is there, and wait
  133. * for interrupts to occur.
  134. */
  135. eeti = devm_kzalloc(dev, sizeof(*eeti), GFP_KERNEL);
  136. if (!eeti) {
  137. dev_err(dev, "failed to allocate driver data\n");
  138. return -ENOMEM;
  139. }
  140. input = devm_input_allocate_device(dev);
  141. if (!input) {
  142. dev_err(dev, "Failed to allocate input device.\n");
  143. return -ENOMEM;
  144. }
  145. input_set_capability(input, EV_KEY, BTN_TOUCH);
  146. input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
  147. input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
  148. input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
  149. input->name = client->name;
  150. input->id.bustype = BUS_I2C;
  151. input->open = eeti_ts_open;
  152. input->close = eeti_ts_close;
  153. eeti->client = client;
  154. eeti->input = input;
  155. eeti->attn_gpio = devm_gpiod_get_optional(dev, "attn", GPIOD_IN);
  156. if (IS_ERR(eeti->attn_gpio))
  157. return PTR_ERR(eeti->attn_gpio);
  158. i2c_set_clientdata(client, eeti);
  159. input_set_drvdata(input, eeti);
  160. error = devm_request_threaded_irq(dev, client->irq,
  161. NULL, eeti_ts_isr,
  162. IRQF_ONESHOT,
  163. client->name, eeti);
  164. if (error) {
  165. dev_err(dev, "Unable to request touchscreen IRQ: %d\n",
  166. error);
  167. return error;
  168. }
  169. /*
  170. * Disable the device for now. It will be enabled once the
  171. * input device is opened.
  172. */
  173. eeti_ts_stop(eeti);
  174. error = input_register_device(input);
  175. if (error)
  176. return error;
  177. return 0;
  178. }
  179. static int __maybe_unused eeti_ts_suspend(struct device *dev)
  180. {
  181. struct i2c_client *client = to_i2c_client(dev);
  182. struct eeti_ts *eeti = i2c_get_clientdata(client);
  183. struct input_dev *input_dev = eeti->input;
  184. mutex_lock(&input_dev->mutex);
  185. if (input_dev->users)
  186. eeti_ts_stop(eeti);
  187. mutex_unlock(&input_dev->mutex);
  188. if (device_may_wakeup(&client->dev))
  189. enable_irq_wake(client->irq);
  190. return 0;
  191. }
  192. static int __maybe_unused eeti_ts_resume(struct device *dev)
  193. {
  194. struct i2c_client *client = to_i2c_client(dev);
  195. struct eeti_ts *eeti = i2c_get_clientdata(client);
  196. struct input_dev *input_dev = eeti->input;
  197. if (device_may_wakeup(&client->dev))
  198. disable_irq_wake(client->irq);
  199. mutex_lock(&input_dev->mutex);
  200. if (input_dev->users)
  201. eeti_ts_start(eeti);
  202. mutex_unlock(&input_dev->mutex);
  203. return 0;
  204. }
  205. static SIMPLE_DEV_PM_OPS(eeti_ts_pm, eeti_ts_suspend, eeti_ts_resume);
  206. static const struct i2c_device_id eeti_ts_id[] = {
  207. { "eeti_ts", 0 },
  208. { }
  209. };
  210. MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
  211. static struct i2c_driver eeti_ts_driver = {
  212. .driver = {
  213. .name = "eeti_ts",
  214. .pm = &eeti_ts_pm,
  215. },
  216. .probe = eeti_ts_probe,
  217. .id_table = eeti_ts_id,
  218. };
  219. module_i2c_driver(eeti_ts_driver);
  220. MODULE_DESCRIPTION("EETI Touchscreen driver");
  221. MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
  222. MODULE_LICENSE("GPL");