eeti_ts.c 7.3 KB

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