migor_ts.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Touch Screen driver for Renesas MIGO-R Platform
  3. *
  4. * Copyright (c) 2008 Magnus Damm
  5. * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>,
  6. * Kenati Technologies Pvt Ltd.
  7. *
  8. * This file is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This file is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/input.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/pm.h>
  27. #include <linux/slab.h>
  28. #include <asm/io.h>
  29. #include <linux/i2c.h>
  30. #include <linux/timer.h>
  31. #define EVENT_PENDOWN 1
  32. #define EVENT_REPEAT 2
  33. #define EVENT_PENUP 3
  34. struct migor_ts_priv {
  35. struct i2c_client *client;
  36. struct input_dev *input;
  37. struct delayed_work work;
  38. int irq;
  39. };
  40. static const u_int8_t migor_ts_ena_seq[17] = { 0x33, 0x22, 0x11,
  41. 0x01, 0x06, 0x07, };
  42. static const u_int8_t migor_ts_dis_seq[17] = { };
  43. static void migor_ts_poscheck(struct work_struct *work)
  44. {
  45. struct migor_ts_priv *priv = container_of(work,
  46. struct migor_ts_priv,
  47. work.work);
  48. unsigned short xpos, ypos;
  49. unsigned char event;
  50. u_int8_t buf[16];
  51. memset(buf, 0, sizeof(buf));
  52. /* Set Index 0 */
  53. buf[0] = 0;
  54. if (i2c_master_send(priv->client, buf, 1) != 1) {
  55. dev_err(&priv->client->dev, "Unable to write i2c index\n");
  56. goto out;
  57. }
  58. /* Now do Page Read */
  59. if (i2c_master_recv(priv->client, buf, sizeof(buf)) != sizeof(buf)) {
  60. dev_err(&priv->client->dev, "Unable to read i2c page\n");
  61. goto out;
  62. }
  63. ypos = ((buf[9] & 0x03) << 8 | buf[8]);
  64. xpos = ((buf[11] & 0x03) << 8 | buf[10]);
  65. event = buf[12];
  66. if (event == EVENT_PENDOWN || event == EVENT_REPEAT) {
  67. input_report_key(priv->input, BTN_TOUCH, 1);
  68. input_report_abs(priv->input, ABS_X, ypos); /*X-Y swap*/
  69. input_report_abs(priv->input, ABS_Y, xpos);
  70. input_sync(priv->input);
  71. } else if (event == EVENT_PENUP) {
  72. input_report_key(priv->input, BTN_TOUCH, 0);
  73. input_sync(priv->input);
  74. }
  75. out:
  76. enable_irq(priv->irq);
  77. }
  78. static irqreturn_t migor_ts_isr(int irq, void *dev_id)
  79. {
  80. struct migor_ts_priv *priv = dev_id;
  81. /* the touch screen controller chip is hooked up to the cpu
  82. * using i2c and a single interrupt line. the interrupt line
  83. * is pulled low whenever someone taps the screen. to deassert
  84. * the interrupt line we need to acknowledge the interrupt by
  85. * communicating with the controller over the slow i2c bus.
  86. *
  87. * we can't acknowledge from interrupt context since the i2c
  88. * bus controller may sleep, so we just disable the interrupt
  89. * here and handle the acknowledge using delayed work.
  90. */
  91. disable_irq_nosync(irq);
  92. schedule_delayed_work(&priv->work, HZ / 20);
  93. return IRQ_HANDLED;
  94. }
  95. static int migor_ts_open(struct input_dev *dev)
  96. {
  97. struct migor_ts_priv *priv = input_get_drvdata(dev);
  98. struct i2c_client *client = priv->client;
  99. int count;
  100. /* enable controller */
  101. count = i2c_master_send(client, migor_ts_ena_seq,
  102. sizeof(migor_ts_ena_seq));
  103. if (count != sizeof(migor_ts_ena_seq)) {
  104. dev_err(&client->dev, "Unable to enable touchscreen.\n");
  105. return -ENXIO;
  106. }
  107. return 0;
  108. }
  109. static void migor_ts_close(struct input_dev *dev)
  110. {
  111. struct migor_ts_priv *priv = input_get_drvdata(dev);
  112. struct i2c_client *client = priv->client;
  113. disable_irq(priv->irq);
  114. /* cancel pending work and wait for migor_ts_poscheck() to finish */
  115. if (cancel_delayed_work_sync(&priv->work)) {
  116. /*
  117. * if migor_ts_poscheck was canceled we need to enable IRQ
  118. * here to balance disable done in migor_ts_isr.
  119. */
  120. enable_irq(priv->irq);
  121. }
  122. /* disable controller */
  123. i2c_master_send(client, migor_ts_dis_seq, sizeof(migor_ts_dis_seq));
  124. enable_irq(priv->irq);
  125. }
  126. static int migor_ts_probe(struct i2c_client *client,
  127. const struct i2c_device_id *idp)
  128. {
  129. struct migor_ts_priv *priv;
  130. struct input_dev *input;
  131. int error;
  132. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  133. if (!priv) {
  134. dev_err(&client->dev, "failed to allocate driver data\n");
  135. error = -ENOMEM;
  136. goto err0;
  137. }
  138. dev_set_drvdata(&client->dev, priv);
  139. input = input_allocate_device();
  140. if (!input) {
  141. dev_err(&client->dev, "Failed to allocate input device.\n");
  142. error = -ENOMEM;
  143. goto err1;
  144. }
  145. input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  146. input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  147. input_set_abs_params(input, ABS_X, 95, 955, 0, 0);
  148. input_set_abs_params(input, ABS_Y, 85, 935, 0, 0);
  149. input->name = client->name;
  150. input->id.bustype = BUS_I2C;
  151. input->dev.parent = &client->dev;
  152. input->open = migor_ts_open;
  153. input->close = migor_ts_close;
  154. input_set_drvdata(input, priv);
  155. priv->client = client;
  156. priv->input = input;
  157. INIT_DELAYED_WORK(&priv->work, migor_ts_poscheck);
  158. priv->irq = client->irq;
  159. error = input_register_device(input);
  160. if (error)
  161. goto err1;
  162. error = request_irq(priv->irq, migor_ts_isr, IRQF_TRIGGER_LOW,
  163. client->name, priv);
  164. if (error) {
  165. dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
  166. goto err2;
  167. }
  168. device_init_wakeup(&client->dev, 1);
  169. return 0;
  170. err2:
  171. input_unregister_device(input);
  172. input = NULL; /* so we dont try to free it below */
  173. err1:
  174. input_free_device(input);
  175. kfree(priv);
  176. err0:
  177. dev_set_drvdata(&client->dev, NULL);
  178. return error;
  179. }
  180. static int migor_ts_remove(struct i2c_client *client)
  181. {
  182. struct migor_ts_priv *priv = dev_get_drvdata(&client->dev);
  183. free_irq(priv->irq, priv);
  184. input_unregister_device(priv->input);
  185. kfree(priv);
  186. dev_set_drvdata(&client->dev, NULL);
  187. return 0;
  188. }
  189. static int migor_ts_suspend(struct device *dev)
  190. {
  191. struct i2c_client *client = to_i2c_client(dev);
  192. struct migor_ts_priv *priv = dev_get_drvdata(&client->dev);
  193. if (device_may_wakeup(&client->dev))
  194. enable_irq_wake(priv->irq);
  195. return 0;
  196. }
  197. static int migor_ts_resume(struct device *dev)
  198. {
  199. struct i2c_client *client = to_i2c_client(dev);
  200. struct migor_ts_priv *priv = dev_get_drvdata(&client->dev);
  201. if (device_may_wakeup(&client->dev))
  202. disable_irq_wake(priv->irq);
  203. return 0;
  204. }
  205. static SIMPLE_DEV_PM_OPS(migor_ts_pm, migor_ts_suspend, migor_ts_resume);
  206. static const struct i2c_device_id migor_ts_id[] = {
  207. { "migor_ts", 0 },
  208. { }
  209. };
  210. MODULE_DEVICE_TABLE(i2c, migor_ts);
  211. static struct i2c_driver migor_ts_driver = {
  212. .driver = {
  213. .name = "migor_ts",
  214. .pm = &migor_ts_pm,
  215. },
  216. .probe = migor_ts_probe,
  217. .remove = migor_ts_remove,
  218. .id_table = migor_ts_id,
  219. };
  220. static int __init migor_ts_init(void)
  221. {
  222. return i2c_add_driver(&migor_ts_driver);
  223. }
  224. static void __exit migor_ts_exit(void)
  225. {
  226. i2c_del_driver(&migor_ts_driver);
  227. }
  228. MODULE_DESCRIPTION("MigoR Touchscreen driver");
  229. MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
  230. MODULE_LICENSE("GPL");
  231. module_init(migor_ts_init);
  232. module_exit(migor_ts_exit);