eeti_ts.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.h>
  34. static int flip_x;
  35. module_param(flip_x, bool, 0644);
  36. MODULE_PARM_DESC(flip_x, "flip x coordinate");
  37. static int flip_y;
  38. module_param(flip_y, bool, 0644);
  39. MODULE_PARM_DESC(flip_y, "flip y coordinate");
  40. struct eeti_ts_priv {
  41. struct i2c_client *client;
  42. struct input_dev *input;
  43. struct work_struct work;
  44. struct mutex mutex;
  45. int irq;
  46. };
  47. #define EETI_TS_BITDEPTH (11)
  48. #define EETI_MAXVAL ((1 << (EETI_TS_BITDEPTH + 1)) - 1)
  49. #define REPORT_BIT_PRESSED (1 << 0)
  50. #define REPORT_BIT_AD0 (1 << 1)
  51. #define REPORT_BIT_AD1 (1 << 2)
  52. #define REPORT_BIT_HAS_PRESSURE (1 << 6)
  53. #define REPORT_RES_BITS(v) (((v) >> 1) + EETI_TS_BITDEPTH)
  54. static void eeti_ts_read(struct work_struct *work)
  55. {
  56. char buf[6];
  57. unsigned int x, y, res, pressed, to = 100;
  58. struct eeti_ts_priv *priv =
  59. container_of(work, struct eeti_ts_priv, work);
  60. mutex_lock(&priv->mutex);
  61. while (!gpio_get_value(irq_to_gpio(priv->irq)) && --to)
  62. i2c_master_recv(priv->client, buf, sizeof(buf));
  63. if (!to) {
  64. dev_err(&priv->client->dev,
  65. "unable to clear IRQ - line stuck?\n");
  66. goto out;
  67. }
  68. /* drop non-report packets */
  69. if (!(buf[0] & 0x80))
  70. goto out;
  71. pressed = buf[0] & REPORT_BIT_PRESSED;
  72. res = REPORT_RES_BITS(buf[0] & (REPORT_BIT_AD0 | REPORT_BIT_AD1));
  73. x = buf[2] | (buf[1] << 8);
  74. y = buf[4] | (buf[3] << 8);
  75. /* fix the range to 11 bits */
  76. x >>= res - EETI_TS_BITDEPTH;
  77. y >>= res - EETI_TS_BITDEPTH;
  78. if (flip_x)
  79. x = EETI_MAXVAL - x;
  80. if (flip_y)
  81. y = EETI_MAXVAL - y;
  82. if (buf[0] & REPORT_BIT_HAS_PRESSURE)
  83. input_report_abs(priv->input, ABS_PRESSURE, buf[5]);
  84. input_report_abs(priv->input, ABS_X, x);
  85. input_report_abs(priv->input, ABS_Y, y);
  86. input_report_key(priv->input, BTN_TOUCH, !!pressed);
  87. input_sync(priv->input);
  88. out:
  89. mutex_unlock(&priv->mutex);
  90. }
  91. static irqreturn_t eeti_ts_isr(int irq, void *dev_id)
  92. {
  93. struct eeti_ts_priv *priv = dev_id;
  94. /* postpone I2C transactions as we are atomic */
  95. schedule_work(&priv->work);
  96. return IRQ_HANDLED;
  97. }
  98. static int eeti_ts_open(struct input_dev *dev)
  99. {
  100. struct eeti_ts_priv *priv = input_get_drvdata(dev);
  101. enable_irq(priv->irq);
  102. /* Read the events once to arm the IRQ */
  103. eeti_ts_read(&priv->work);
  104. return 0;
  105. }
  106. static void eeti_ts_close(struct input_dev *dev)
  107. {
  108. struct eeti_ts_priv *priv = input_get_drvdata(dev);
  109. disable_irq(priv->irq);
  110. cancel_work_sync(&priv->work);
  111. }
  112. static int __devinit eeti_ts_probe(struct i2c_client *client,
  113. const struct i2c_device_id *idp)
  114. {
  115. struct eeti_ts_priv *priv;
  116. struct input_dev *input;
  117. int err = -ENOMEM;
  118. /* In contrast to what's described in the datasheet, there seems
  119. * to be no way of probing the presence of that device using I2C
  120. * commands. So we need to blindly believe it is there, and wait
  121. * for interrupts to occur. */
  122. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  123. if (!priv) {
  124. dev_err(&client->dev, "failed to allocate driver data\n");
  125. goto err0;
  126. }
  127. mutex_init(&priv->mutex);
  128. input = input_allocate_device();
  129. if (!input) {
  130. dev_err(&client->dev, "Failed to allocate input device.\n");
  131. goto err1;
  132. }
  133. input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  134. input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  135. input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
  136. input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
  137. input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
  138. input->name = client->name;
  139. input->id.bustype = BUS_I2C;
  140. input->dev.parent = &client->dev;
  141. input->open = eeti_ts_open;
  142. input->close = eeti_ts_close;
  143. priv->client = client;
  144. priv->input = input;
  145. priv->irq = client->irq;
  146. INIT_WORK(&priv->work, eeti_ts_read);
  147. i2c_set_clientdata(client, priv);
  148. input_set_drvdata(input, priv);
  149. err = input_register_device(input);
  150. if (err)
  151. goto err1;
  152. err = request_irq(priv->irq, eeti_ts_isr, IRQF_TRIGGER_FALLING,
  153. client->name, priv);
  154. if (err) {
  155. dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
  156. goto err2;
  157. }
  158. /* Disable the irq for now. It will be enabled once the input device
  159. * is opened. */
  160. disable_irq(priv->irq);
  161. device_init_wakeup(&client->dev, 0);
  162. return 0;
  163. err2:
  164. input_unregister_device(input);
  165. input = NULL; /* so we dont try to free it below */
  166. err1:
  167. input_free_device(input);
  168. i2c_set_clientdata(client, NULL);
  169. kfree(priv);
  170. err0:
  171. return err;
  172. }
  173. static int __devexit eeti_ts_remove(struct i2c_client *client)
  174. {
  175. struct eeti_ts_priv *priv = i2c_get_clientdata(client);
  176. free_irq(priv->irq, priv);
  177. input_unregister_device(priv->input);
  178. i2c_set_clientdata(client, NULL);
  179. kfree(priv);
  180. return 0;
  181. }
  182. #ifdef CONFIG_PM
  183. static int eeti_ts_suspend(struct i2c_client *client, pm_message_t mesg)
  184. {
  185. struct eeti_ts_priv *priv = i2c_get_clientdata(client);
  186. if (device_may_wakeup(&client->dev))
  187. enable_irq_wake(priv->irq);
  188. return 0;
  189. }
  190. static int eeti_ts_resume(struct i2c_client *client)
  191. {
  192. struct eeti_ts_priv *priv = i2c_get_clientdata(client);
  193. if (device_may_wakeup(&client->dev))
  194. disable_irq_wake(priv->irq);
  195. return 0;
  196. }
  197. #else
  198. #define eeti_ts_suspend NULL
  199. #define eeti_ts_resume NULL
  200. #endif
  201. static const struct i2c_device_id eeti_ts_id[] = {
  202. { "eeti_ts", 0 },
  203. { }
  204. };
  205. MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
  206. static struct i2c_driver eeti_ts_driver = {
  207. .driver = {
  208. .name = "eeti_ts",
  209. },
  210. .probe = eeti_ts_probe,
  211. .remove = __devexit_p(eeti_ts_remove),
  212. .suspend = eeti_ts_suspend,
  213. .resume = eeti_ts_resume,
  214. .id_table = eeti_ts_id,
  215. };
  216. static int __init eeti_ts_init(void)
  217. {
  218. return i2c_add_driver(&eeti_ts_driver);
  219. }
  220. static void __exit eeti_ts_exit(void)
  221. {
  222. i2c_del_driver(&eeti_ts_driver);
  223. }
  224. MODULE_DESCRIPTION("EETI Touchscreen driver");
  225. MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
  226. MODULE_LICENSE("GPL");
  227. module_init(eeti_ts_init);
  228. module_exit(eeti_ts_exit);