eeti_ts.c 7.8 KB

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