migor_ts.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. 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 irqreturn_t migor_ts_isr(int irq, void *dev_id)
  43. {
  44. struct migor_ts_priv *priv = dev_id;
  45. unsigned short xpos, ypos;
  46. unsigned char event;
  47. u_int8_t buf[16];
  48. /*
  49. * The touch screen controller chip is hooked up to the CPU
  50. * using I2C and a single interrupt line. The interrupt line
  51. * is pulled low whenever someone taps the screen. To deassert
  52. * the interrupt line we need to acknowledge the interrupt by
  53. * communicating with the controller over the slow i2c bus.
  54. *
  55. * Since I2C bus controller may sleep we are using threaded
  56. * IRQ here.
  57. */
  58. memset(buf, 0, sizeof(buf));
  59. /* Set Index 0 */
  60. buf[0] = 0;
  61. if (i2c_master_send(priv->client, buf, 1) != 1) {
  62. dev_err(&priv->client->dev, "Unable to write i2c index\n");
  63. goto out;
  64. }
  65. /* Now do Page Read */
  66. if (i2c_master_recv(priv->client, buf, sizeof(buf)) != sizeof(buf)) {
  67. dev_err(&priv->client->dev, "Unable to read i2c page\n");
  68. goto out;
  69. }
  70. ypos = ((buf[9] & 0x03) << 8 | buf[8]);
  71. xpos = ((buf[11] & 0x03) << 8 | buf[10]);
  72. event = buf[12];
  73. switch (event) {
  74. case EVENT_PENDOWN:
  75. case EVENT_REPEAT:
  76. input_report_key(priv->input, BTN_TOUCH, 1);
  77. input_report_abs(priv->input, ABS_X, ypos); /*X-Y swap*/
  78. input_report_abs(priv->input, ABS_Y, xpos);
  79. input_sync(priv->input);
  80. break;
  81. case EVENT_PENUP:
  82. input_report_key(priv->input, BTN_TOUCH, 0);
  83. input_sync(priv->input);
  84. break;
  85. }
  86. out:
  87. return IRQ_HANDLED;
  88. }
  89. static int migor_ts_open(struct input_dev *dev)
  90. {
  91. struct migor_ts_priv *priv = input_get_drvdata(dev);
  92. struct i2c_client *client = priv->client;
  93. int count;
  94. /* enable controller */
  95. count = i2c_master_send(client, migor_ts_ena_seq,
  96. sizeof(migor_ts_ena_seq));
  97. if (count != sizeof(migor_ts_ena_seq)) {
  98. dev_err(&client->dev, "Unable to enable touchscreen.\n");
  99. return -ENXIO;
  100. }
  101. return 0;
  102. }
  103. static void migor_ts_close(struct input_dev *dev)
  104. {
  105. struct migor_ts_priv *priv = input_get_drvdata(dev);
  106. struct i2c_client *client = priv->client;
  107. disable_irq(priv->irq);
  108. /* disable controller */
  109. i2c_master_send(client, migor_ts_dis_seq, sizeof(migor_ts_dis_seq));
  110. enable_irq(priv->irq);
  111. }
  112. static int migor_ts_probe(struct i2c_client *client,
  113. const struct i2c_device_id *idp)
  114. {
  115. struct migor_ts_priv *priv;
  116. struct input_dev *input;
  117. int error;
  118. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  119. input = input_allocate_device();
  120. if (!priv || !input) {
  121. dev_err(&client->dev, "failed to allocate memory\n");
  122. error = -ENOMEM;
  123. goto err_free_mem;
  124. }
  125. priv->client = client;
  126. priv->input = input;
  127. priv->irq = client->irq;
  128. input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  129. __set_bit(BTN_TOUCH, input->keybit);
  130. input_set_abs_params(input, ABS_X, 95, 955, 0, 0);
  131. input_set_abs_params(input, ABS_Y, 85, 935, 0, 0);
  132. input->name = client->name;
  133. input->id.bustype = BUS_I2C;
  134. input->dev.parent = &client->dev;
  135. input->open = migor_ts_open;
  136. input->close = migor_ts_close;
  137. input_set_drvdata(input, priv);
  138. error = request_threaded_irq(priv->irq, NULL, migor_ts_isr,
  139. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  140. client->name, priv);
  141. if (error) {
  142. dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
  143. goto err_free_mem;
  144. }
  145. error = input_register_device(input);
  146. if (error)
  147. goto err_free_irq;
  148. i2c_set_clientdata(client, priv);
  149. device_init_wakeup(&client->dev, 1);
  150. return 0;
  151. err_free_irq:
  152. free_irq(priv->irq, priv);
  153. err_free_mem:
  154. input_free_device(input);
  155. kfree(priv);
  156. return error;
  157. }
  158. static int migor_ts_remove(struct i2c_client *client)
  159. {
  160. struct migor_ts_priv *priv = i2c_get_clientdata(client);
  161. free_irq(priv->irq, priv);
  162. input_unregister_device(priv->input);
  163. kfree(priv);
  164. dev_set_drvdata(&client->dev, NULL);
  165. return 0;
  166. }
  167. static int migor_ts_suspend(struct device *dev)
  168. {
  169. struct i2c_client *client = to_i2c_client(dev);
  170. struct migor_ts_priv *priv = i2c_get_clientdata(client);
  171. if (device_may_wakeup(&client->dev))
  172. enable_irq_wake(priv->irq);
  173. return 0;
  174. }
  175. static int migor_ts_resume(struct device *dev)
  176. {
  177. struct i2c_client *client = to_i2c_client(dev);
  178. struct migor_ts_priv *priv = i2c_get_clientdata(client);
  179. if (device_may_wakeup(&client->dev))
  180. disable_irq_wake(priv->irq);
  181. return 0;
  182. }
  183. static SIMPLE_DEV_PM_OPS(migor_ts_pm, migor_ts_suspend, migor_ts_resume);
  184. static const struct i2c_device_id migor_ts_id[] = {
  185. { "migor_ts", 0 },
  186. { }
  187. };
  188. MODULE_DEVICE_TABLE(i2c, migor_ts);
  189. static struct i2c_driver migor_ts_driver = {
  190. .driver = {
  191. .name = "migor_ts",
  192. .pm = &migor_ts_pm,
  193. },
  194. .probe = migor_ts_probe,
  195. .remove = migor_ts_remove,
  196. .id_table = migor_ts_id,
  197. };
  198. module_i2c_driver(migor_ts_driver);
  199. MODULE_DESCRIPTION("MigoR Touchscreen driver");
  200. MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
  201. MODULE_LICENSE("GPL");