eeti_ts.c 7.2 KB

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