migor_ts.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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/slab.h>
  27. #include <asm/io.h>
  28. #include <linux/i2c.h>
  29. #include <linux/timer.h>
  30. #define EVENT_PENDOWN 1
  31. #define EVENT_REPEAT 2
  32. #define EVENT_PENUP 3
  33. struct migor_ts_priv {
  34. struct i2c_client *client;
  35. struct input_dev *input;
  36. struct delayed_work work;
  37. int irq;
  38. };
  39. static const u_int8_t migor_ts_ena_seq[17] = { 0x33, 0x22, 0x11,
  40. 0x01, 0x06, 0x07, };
  41. static const u_int8_t migor_ts_dis_seq[17] = { };
  42. static void migor_ts_poscheck(struct work_struct *work)
  43. {
  44. struct migor_ts_priv *priv = container_of(work,
  45. struct migor_ts_priv,
  46. work.work);
  47. unsigned short xpos, ypos;
  48. unsigned char event;
  49. u_int8_t buf[16];
  50. memset(buf, 0, sizeof(buf));
  51. /* Set Index 0 */
  52. buf[0] = 0;
  53. if (i2c_master_send(priv->client, buf, 1) != 1) {
  54. dev_err(&priv->client->dev, "Unable to write i2c index\n");
  55. goto out;
  56. }
  57. /* Now do Page Read */
  58. if (i2c_master_recv(priv->client, buf, sizeof(buf)) != sizeof(buf)) {
  59. dev_err(&priv->client->dev, "Unable to read i2c page\n");
  60. goto out;
  61. }
  62. ypos = ((buf[9] & 0x03) << 8 | buf[8]);
  63. xpos = ((buf[11] & 0x03) << 8 | buf[10]);
  64. event = buf[12];
  65. if (event == EVENT_PENDOWN || event == EVENT_REPEAT) {
  66. input_report_key(priv->input, BTN_TOUCH, 1);
  67. input_report_abs(priv->input, ABS_X, ypos); /*X-Y swap*/
  68. input_report_abs(priv->input, ABS_Y, xpos);
  69. input_sync(priv->input);
  70. } else if (event == EVENT_PENUP) {
  71. input_report_key(priv->input, BTN_TOUCH, 0);
  72. input_sync(priv->input);
  73. }
  74. out:
  75. enable_irq(priv->irq);
  76. }
  77. static irqreturn_t migor_ts_isr(int irq, void *dev_id)
  78. {
  79. struct migor_ts_priv *priv = dev_id;
  80. /* the touch screen controller chip is hooked up to the cpu
  81. * using i2c and a single interrupt line. the interrupt line
  82. * is pulled low whenever someone taps the screen. to deassert
  83. * the interrupt line we need to acknowledge the interrupt by
  84. * communicating with the controller over the slow i2c bus.
  85. *
  86. * we can't acknowledge from interrupt context since the i2c
  87. * bus controller may sleep, so we just disable the interrupt
  88. * here and handle the acknowledge using delayed work.
  89. */
  90. disable_irq_nosync(irq);
  91. schedule_delayed_work(&priv->work, HZ / 20);
  92. return IRQ_HANDLED;
  93. }
  94. static int migor_ts_open(struct input_dev *dev)
  95. {
  96. struct migor_ts_priv *priv = input_get_drvdata(dev);
  97. struct i2c_client *client = priv->client;
  98. int count;
  99. /* enable controller */
  100. count = i2c_master_send(client, migor_ts_ena_seq,
  101. sizeof(migor_ts_ena_seq));
  102. if (count != sizeof(migor_ts_ena_seq)) {
  103. dev_err(&client->dev, "Unable to enable touchscreen.\n");
  104. return -ENXIO;
  105. }
  106. return 0;
  107. }
  108. static void migor_ts_close(struct input_dev *dev)
  109. {
  110. struct migor_ts_priv *priv = input_get_drvdata(dev);
  111. struct i2c_client *client = priv->client;
  112. disable_irq(priv->irq);
  113. /* cancel pending work and wait for migor_ts_poscheck() to finish */
  114. if (cancel_delayed_work_sync(&priv->work)) {
  115. /*
  116. * if migor_ts_poscheck was canceled we need to enable IRQ
  117. * here to balance disable done in migor_ts_isr.
  118. */
  119. enable_irq(priv->irq);
  120. }
  121. /* disable controller */
  122. i2c_master_send(client, migor_ts_dis_seq, sizeof(migor_ts_dis_seq));
  123. enable_irq(priv->irq);
  124. }
  125. static int migor_ts_probe(struct i2c_client *client,
  126. const struct i2c_device_id *idp)
  127. {
  128. struct migor_ts_priv *priv;
  129. struct input_dev *input;
  130. int error;
  131. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  132. if (!priv) {
  133. dev_err(&client->dev, "failed to allocate driver data\n");
  134. error = -ENOMEM;
  135. goto err0;
  136. }
  137. dev_set_drvdata(&client->dev, priv);
  138. input = input_allocate_device();
  139. if (!input) {
  140. dev_err(&client->dev, "Failed to allocate input device.\n");
  141. error = -ENOMEM;
  142. goto err1;
  143. }
  144. input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  145. input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  146. input_set_abs_params(input, ABS_X, 95, 955, 0, 0);
  147. input_set_abs_params(input, ABS_Y, 85, 935, 0, 0);
  148. input->name = client->name;
  149. input->id.bustype = BUS_I2C;
  150. input->dev.parent = &client->dev;
  151. input->open = migor_ts_open;
  152. input->close = migor_ts_close;
  153. input_set_drvdata(input, priv);
  154. priv->client = client;
  155. priv->input = input;
  156. INIT_DELAYED_WORK(&priv->work, migor_ts_poscheck);
  157. priv->irq = client->irq;
  158. error = input_register_device(input);
  159. if (error)
  160. goto err1;
  161. error = request_irq(priv->irq, migor_ts_isr, IRQF_TRIGGER_LOW,
  162. client->name, priv);
  163. if (error) {
  164. dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
  165. goto err2;
  166. }
  167. device_init_wakeup(&client->dev, 1);
  168. return 0;
  169. err2:
  170. input_unregister_device(input);
  171. input = NULL; /* so we dont try to free it below */
  172. err1:
  173. input_free_device(input);
  174. kfree(priv);
  175. err0:
  176. dev_set_drvdata(&client->dev, NULL);
  177. return error;
  178. }
  179. static int migor_ts_remove(struct i2c_client *client)
  180. {
  181. struct migor_ts_priv *priv = dev_get_drvdata(&client->dev);
  182. free_irq(priv->irq, priv);
  183. input_unregister_device(priv->input);
  184. kfree(priv);
  185. dev_set_drvdata(&client->dev, NULL);
  186. return 0;
  187. }
  188. static int migor_ts_suspend(struct i2c_client *client, pm_message_t mesg)
  189. {
  190. struct migor_ts_priv *priv = dev_get_drvdata(&client->dev);
  191. if (device_may_wakeup(&client->dev))
  192. enable_irq_wake(priv->irq);
  193. return 0;
  194. }
  195. static int migor_ts_resume(struct i2c_client *client)
  196. {
  197. struct migor_ts_priv *priv = dev_get_drvdata(&client->dev);
  198. if (device_may_wakeup(&client->dev))
  199. disable_irq_wake(priv->irq);
  200. return 0;
  201. }
  202. static const struct i2c_device_id migor_ts_id[] = {
  203. { "migor_ts", 0 },
  204. { }
  205. };
  206. MODULE_DEVICE_TABLE(i2c, migor_ts);
  207. static struct i2c_driver migor_ts_driver = {
  208. .driver = {
  209. .name = "migor_ts",
  210. },
  211. .probe = migor_ts_probe,
  212. .remove = migor_ts_remove,
  213. .suspend = migor_ts_suspend,
  214. .resume = migor_ts_resume,
  215. .id_table = migor_ts_id,
  216. };
  217. static int __init migor_ts_init(void)
  218. {
  219. return i2c_add_driver(&migor_ts_driver);
  220. }
  221. static void __exit migor_ts_exit(void)
  222. {
  223. i2c_del_driver(&migor_ts_driver);
  224. }
  225. MODULE_DESCRIPTION("MigoR Touchscreen driver");
  226. MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
  227. MODULE_LICENSE("GPL");
  228. module_init(migor_ts_init);
  229. module_exit(migor_ts_exit);