migor_ts.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Touch Screen driver for Renesas MIGO-R Platform
  4. *
  5. * Copyright (c) 2008 Magnus Damm
  6. * Copyright (c) 2007 Ujjwal Pande <ujjwal@kenati.com>,
  7. * Kenati Technologies Pvt Ltd.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/input.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/pm.h>
  14. #include <linux/slab.h>
  15. #include <asm/io.h>
  16. #include <linux/i2c.h>
  17. #include <linux/timer.h>
  18. #define EVENT_PENDOWN 1
  19. #define EVENT_REPEAT 2
  20. #define EVENT_PENUP 3
  21. struct migor_ts_priv {
  22. struct i2c_client *client;
  23. struct input_dev *input;
  24. int irq;
  25. };
  26. static const u_int8_t migor_ts_ena_seq[17] = { 0x33, 0x22, 0x11,
  27. 0x01, 0x06, 0x07, };
  28. static const u_int8_t migor_ts_dis_seq[17] = { };
  29. static irqreturn_t migor_ts_isr(int irq, void *dev_id)
  30. {
  31. struct migor_ts_priv *priv = dev_id;
  32. unsigned short xpos, ypos;
  33. unsigned char event;
  34. u_int8_t buf[16];
  35. /*
  36. * The touch screen controller chip is hooked up to the CPU
  37. * using I2C and a single interrupt line. The interrupt line
  38. * is pulled low whenever someone taps the screen. To deassert
  39. * the interrupt line we need to acknowledge the interrupt by
  40. * communicating with the controller over the slow i2c bus.
  41. *
  42. * Since I2C bus controller may sleep we are using threaded
  43. * IRQ here.
  44. */
  45. memset(buf, 0, sizeof(buf));
  46. /* Set Index 0 */
  47. buf[0] = 0;
  48. if (i2c_master_send(priv->client, buf, 1) != 1) {
  49. dev_err(&priv->client->dev, "Unable to write i2c index\n");
  50. goto out;
  51. }
  52. /* Now do Page Read */
  53. if (i2c_master_recv(priv->client, buf, sizeof(buf)) != sizeof(buf)) {
  54. dev_err(&priv->client->dev, "Unable to read i2c page\n");
  55. goto out;
  56. }
  57. ypos = ((buf[9] & 0x03) << 8 | buf[8]);
  58. xpos = ((buf[11] & 0x03) << 8 | buf[10]);
  59. event = buf[12];
  60. switch (event) {
  61. case EVENT_PENDOWN:
  62. case EVENT_REPEAT:
  63. input_report_key(priv->input, BTN_TOUCH, 1);
  64. input_report_abs(priv->input, ABS_X, ypos); /*X-Y swap*/
  65. input_report_abs(priv->input, ABS_Y, xpos);
  66. input_sync(priv->input);
  67. break;
  68. case EVENT_PENUP:
  69. input_report_key(priv->input, BTN_TOUCH, 0);
  70. input_sync(priv->input);
  71. break;
  72. }
  73. out:
  74. return IRQ_HANDLED;
  75. }
  76. static int migor_ts_open(struct input_dev *dev)
  77. {
  78. struct migor_ts_priv *priv = input_get_drvdata(dev);
  79. struct i2c_client *client = priv->client;
  80. int count;
  81. /* enable controller */
  82. count = i2c_master_send(client, migor_ts_ena_seq,
  83. sizeof(migor_ts_ena_seq));
  84. if (count != sizeof(migor_ts_ena_seq)) {
  85. dev_err(&client->dev, "Unable to enable touchscreen.\n");
  86. return -ENXIO;
  87. }
  88. return 0;
  89. }
  90. static void migor_ts_close(struct input_dev *dev)
  91. {
  92. struct migor_ts_priv *priv = input_get_drvdata(dev);
  93. struct i2c_client *client = priv->client;
  94. disable_irq(priv->irq);
  95. /* disable controller */
  96. i2c_master_send(client, migor_ts_dis_seq, sizeof(migor_ts_dis_seq));
  97. enable_irq(priv->irq);
  98. }
  99. static int migor_ts_probe(struct i2c_client *client)
  100. {
  101. struct migor_ts_priv *priv;
  102. struct input_dev *input;
  103. int error;
  104. priv = kzalloc_obj(*priv);
  105. input = input_allocate_device();
  106. if (!priv || !input) {
  107. dev_err(&client->dev, "failed to allocate memory\n");
  108. error = -ENOMEM;
  109. goto err_free_mem;
  110. }
  111. priv->client = client;
  112. priv->input = input;
  113. priv->irq = client->irq;
  114. input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  115. __set_bit(BTN_TOUCH, input->keybit);
  116. input_set_abs_params(input, ABS_X, 95, 955, 0, 0);
  117. input_set_abs_params(input, ABS_Y, 85, 935, 0, 0);
  118. input->name = client->name;
  119. input->id.bustype = BUS_I2C;
  120. input->dev.parent = &client->dev;
  121. input->open = migor_ts_open;
  122. input->close = migor_ts_close;
  123. input_set_drvdata(input, priv);
  124. error = request_threaded_irq(priv->irq, NULL, migor_ts_isr,
  125. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  126. client->name, priv);
  127. if (error) {
  128. dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
  129. goto err_free_mem;
  130. }
  131. error = input_register_device(input);
  132. if (error)
  133. goto err_free_irq;
  134. i2c_set_clientdata(client, priv);
  135. device_init_wakeup(&client->dev, 1);
  136. return 0;
  137. err_free_irq:
  138. free_irq(priv->irq, priv);
  139. err_free_mem:
  140. input_free_device(input);
  141. kfree(priv);
  142. return error;
  143. }
  144. static void migor_ts_remove(struct i2c_client *client)
  145. {
  146. struct migor_ts_priv *priv = i2c_get_clientdata(client);
  147. free_irq(priv->irq, priv);
  148. input_unregister_device(priv->input);
  149. kfree(priv);
  150. dev_set_drvdata(&client->dev, NULL);
  151. }
  152. static int migor_ts_suspend(struct device *dev)
  153. {
  154. struct i2c_client *client = to_i2c_client(dev);
  155. struct migor_ts_priv *priv = i2c_get_clientdata(client);
  156. if (device_may_wakeup(&client->dev))
  157. enable_irq_wake(priv->irq);
  158. return 0;
  159. }
  160. static int migor_ts_resume(struct device *dev)
  161. {
  162. struct i2c_client *client = to_i2c_client(dev);
  163. struct migor_ts_priv *priv = i2c_get_clientdata(client);
  164. if (device_may_wakeup(&client->dev))
  165. disable_irq_wake(priv->irq);
  166. return 0;
  167. }
  168. static DEFINE_SIMPLE_DEV_PM_OPS(migor_ts_pm, migor_ts_suspend, migor_ts_resume);
  169. static const struct i2c_device_id migor_ts_id[] = {
  170. { "migor_ts" },
  171. { }
  172. };
  173. MODULE_DEVICE_TABLE(i2c, migor_ts_id);
  174. static struct i2c_driver migor_ts_driver = {
  175. .driver = {
  176. .name = "migor_ts",
  177. .pm = pm_sleep_ptr(&migor_ts_pm),
  178. },
  179. .probe = migor_ts_probe,
  180. .remove = migor_ts_remove,
  181. .id_table = migor_ts_id,
  182. };
  183. module_i2c_driver(migor_ts_driver);
  184. MODULE_DESCRIPTION("MigoR Touchscreen driver");
  185. MODULE_AUTHOR("Magnus Damm <damm@opensource.se>");
  186. MODULE_LICENSE("GPL");