eeti_ts.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 void eeti_ts_start(struct eeti_ts_priv *priv)
  104. {
  105. enable_irq(priv->irq);
  106. /* Read the events once to arm the IRQ */
  107. eeti_ts_read(&priv->work);
  108. }
  109. static void eeti_ts_stop(struct eeti_ts_priv *priv)
  110. {
  111. disable_irq(priv->irq);
  112. cancel_work_sync(&priv->work);
  113. }
  114. static int eeti_ts_open(struct input_dev *dev)
  115. {
  116. struct eeti_ts_priv *priv = input_get_drvdata(dev);
  117. eeti_ts_start(priv);
  118. return 0;
  119. }
  120. static void eeti_ts_close(struct input_dev *dev)
  121. {
  122. struct eeti_ts_priv *priv = input_get_drvdata(dev);
  123. eeti_ts_stop(priv);
  124. }
  125. static int __devinit eeti_ts_probe(struct i2c_client *client,
  126. const struct i2c_device_id *idp)
  127. {
  128. struct eeti_ts_platform_data *pdata;
  129. struct eeti_ts_priv *priv;
  130. struct input_dev *input;
  131. unsigned int irq_flags;
  132. int err = -ENOMEM;
  133. /*
  134. * In contrast to what's described in the datasheet, there seems
  135. * to be no way of probing the presence of that device using I2C
  136. * commands. So we need to blindly believe it is there, and wait
  137. * for interrupts to occur.
  138. */
  139. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  140. if (!priv) {
  141. dev_err(&client->dev, "failed to allocate driver data\n");
  142. goto err0;
  143. }
  144. mutex_init(&priv->mutex);
  145. input = input_allocate_device();
  146. if (!input) {
  147. dev_err(&client->dev, "Failed to allocate input device.\n");
  148. goto err1;
  149. }
  150. input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  151. input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  152. input_set_abs_params(input, ABS_X, 0, EETI_MAXVAL, 0, 0);
  153. input_set_abs_params(input, ABS_Y, 0, EETI_MAXVAL, 0, 0);
  154. input_set_abs_params(input, ABS_PRESSURE, 0, 0xff, 0, 0);
  155. input->name = client->name;
  156. input->id.bustype = BUS_I2C;
  157. input->dev.parent = &client->dev;
  158. input->open = eeti_ts_open;
  159. input->close = eeti_ts_close;
  160. priv->client = client;
  161. priv->input = input;
  162. priv->irq = client->irq;
  163. pdata = client->dev.platform_data;
  164. if (pdata)
  165. priv->irq_active_high = pdata->irq_active_high;
  166. irq_flags = priv->irq_active_high ?
  167. IRQF_TRIGGER_RISING : IRQF_TRIGGER_FALLING;
  168. INIT_WORK(&priv->work, eeti_ts_read);
  169. i2c_set_clientdata(client, priv);
  170. input_set_drvdata(input, priv);
  171. err = input_register_device(input);
  172. if (err)
  173. goto err1;
  174. err = request_irq(priv->irq, eeti_ts_isr, irq_flags,
  175. client->name, priv);
  176. if (err) {
  177. dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
  178. goto err2;
  179. }
  180. /*
  181. * Disable the device for now. It will be enabled once the
  182. * input device is opened.
  183. */
  184. eeti_ts_stop(priv);
  185. device_init_wakeup(&client->dev, 0);
  186. return 0;
  187. err2:
  188. input_unregister_device(input);
  189. input = NULL; /* so we dont try to free it below */
  190. err1:
  191. input_free_device(input);
  192. i2c_set_clientdata(client, NULL);
  193. kfree(priv);
  194. err0:
  195. return err;
  196. }
  197. static int __devexit eeti_ts_remove(struct i2c_client *client)
  198. {
  199. struct eeti_ts_priv *priv = i2c_get_clientdata(client);
  200. free_irq(priv->irq, priv);
  201. /*
  202. * eeti_ts_stop() leaves IRQ disabled. We need to re-enable it
  203. * so that device still works if we reload the driver.
  204. */
  205. enable_irq(priv->irq);
  206. input_unregister_device(priv->input);
  207. i2c_set_clientdata(client, NULL);
  208. kfree(priv);
  209. return 0;
  210. }
  211. #ifdef CONFIG_PM
  212. static int eeti_ts_suspend(struct i2c_client *client, pm_message_t mesg)
  213. {
  214. struct eeti_ts_priv *priv = i2c_get_clientdata(client);
  215. struct input_dev *input_dev = priv->input;
  216. mutex_lock(&input_dev->mutex);
  217. if (input_dev->users)
  218. eeti_ts_stop(priv);
  219. mutex_unlock(&input_dev->mutex);
  220. if (device_may_wakeup(&client->dev))
  221. enable_irq_wake(priv->irq);
  222. return 0;
  223. }
  224. static int eeti_ts_resume(struct i2c_client *client)
  225. {
  226. struct eeti_ts_priv *priv = i2c_get_clientdata(client);
  227. struct input_dev *input_dev = priv->input;
  228. if (device_may_wakeup(&client->dev))
  229. disable_irq_wake(priv->irq);
  230. mutex_lock(&input_dev->mutex);
  231. if (input_dev->users)
  232. eeti_ts_start(priv);
  233. mutex_unlock(&input_dev->mutex);
  234. return 0;
  235. }
  236. #else
  237. #define eeti_ts_suspend NULL
  238. #define eeti_ts_resume NULL
  239. #endif
  240. static const struct i2c_device_id eeti_ts_id[] = {
  241. { "eeti_ts", 0 },
  242. { }
  243. };
  244. MODULE_DEVICE_TABLE(i2c, eeti_ts_id);
  245. static struct i2c_driver eeti_ts_driver = {
  246. .driver = {
  247. .name = "eeti_ts",
  248. },
  249. .probe = eeti_ts_probe,
  250. .remove = __devexit_p(eeti_ts_remove),
  251. .suspend = eeti_ts_suspend,
  252. .resume = eeti_ts_resume,
  253. .id_table = eeti_ts_id,
  254. };
  255. static int __init eeti_ts_init(void)
  256. {
  257. return i2c_add_driver(&eeti_ts_driver);
  258. }
  259. static void __exit eeti_ts_exit(void)
  260. {
  261. i2c_del_driver(&eeti_ts_driver);
  262. }
  263. MODULE_DESCRIPTION("EETI Touchscreen driver");
  264. MODULE_AUTHOR("Daniel Mack <daniel@caiaq.de>");
  265. MODULE_LICENSE("GPL");
  266. module_init(eeti_ts_init);
  267. module_exit(eeti_ts_exit);