migor_ts.c 6.4 KB

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