migor_ts.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 <[email protected]>,
  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. const struct i2c_device_id *idp)
  101. {
  102. struct migor_ts_priv *priv;
  103. struct input_dev *input;
  104. int error;
  105. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  106. input = input_allocate_device();
  107. if (!priv || !input) {
  108. dev_err(&client->dev, "failed to allocate memory\n");
  109. error = -ENOMEM;
  110. goto err_free_mem;
  111. }
  112. priv->client = client;
  113. priv->input = input;
  114. priv->irq = client->irq;
  115. input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  116. __set_bit(BTN_TOUCH, input->keybit);
  117. input_set_abs_params(input, ABS_X, 95, 955, 0, 0);
  118. input_set_abs_params(input, ABS_Y, 85, 935, 0, 0);
  119. input->name = client->name;
  120. input->id.bustype = BUS_I2C;
  121. input->dev.parent = &client->dev;
  122. input->open = migor_ts_open;
  123. input->close = migor_ts_close;
  124. input_set_drvdata(input, priv);
  125. error = request_threaded_irq(priv->irq, NULL, migor_ts_isr,
  126. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  127. client->name, priv);
  128. if (error) {
  129. dev_err(&client->dev, "Unable to request touchscreen IRQ.\n");
  130. goto err_free_mem;
  131. }
  132. error = input_register_device(input);
  133. if (error)
  134. goto err_free_irq;
  135. i2c_set_clientdata(client, priv);
  136. device_init_wakeup(&client->dev, 1);
  137. return 0;
  138. err_free_irq:
  139. free_irq(priv->irq, priv);
  140. err_free_mem:
  141. input_free_device(input);
  142. kfree(priv);
  143. return error;
  144. }
  145. static void migor_ts_remove(struct i2c_client *client)
  146. {
  147. struct migor_ts_priv *priv = i2c_get_clientdata(client);
  148. free_irq(priv->irq, priv);
  149. input_unregister_device(priv->input);
  150. kfree(priv);
  151. dev_set_drvdata(&client->dev, NULL);
  152. }
  153. static int __maybe_unused migor_ts_suspend(struct device *dev)
  154. {
  155. struct i2c_client *client = to_i2c_client(dev);
  156. struct migor_ts_priv *priv = i2c_get_clientdata(client);
  157. if (device_may_wakeup(&client->dev))
  158. enable_irq_wake(priv->irq);
  159. return 0;
  160. }
  161. static int __maybe_unused migor_ts_resume(struct device *dev)
  162. {
  163. struct i2c_client *client = to_i2c_client(dev);
  164. struct migor_ts_priv *priv = i2c_get_clientdata(client);
  165. if (device_may_wakeup(&client->dev))
  166. disable_irq_wake(priv->irq);
  167. return 0;
  168. }
  169. static SIMPLE_DEV_PM_OPS(migor_ts_pm, migor_ts_suspend, migor_ts_resume);
  170. static const struct i2c_device_id migor_ts_id[] = {
  171. { "migor_ts", 0 },
  172. { }
  173. };
  174. MODULE_DEVICE_TABLE(i2c, migor_ts_id);
  175. static struct i2c_driver migor_ts_driver = {
  176. .driver = {
  177. .name = "migor_ts",
  178. .pm = &migor_ts_pm,
  179. },
  180. .probe = migor_ts_probe,
  181. .remove = migor_ts_remove,
  182. .id_table = migor_ts_id,
  183. };
  184. module_i2c_driver(migor_ts_driver);
  185. MODULE_DESCRIPTION("MigoR Touchscreen driver");
  186. MODULE_AUTHOR("Magnus Damm <[email protected]>");
  187. MODULE_LICENSE("GPL");