pcap_ts.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for Motorola PCAP2 touchscreen as found in the EZX phone platform.
  4. *
  5. * Copyright (C) 2006 Harald Welte <[email protected]>
  6. * Copyright (C) 2009 Daniel Ribeiro <[email protected]>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/fs.h>
  10. #include <linux/string.h>
  11. #include <linux/slab.h>
  12. #include <linux/pm.h>
  13. #include <linux/timer.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/input.h>
  17. #include <linux/mfd/ezx-pcap.h>
  18. struct pcap_ts {
  19. struct pcap_chip *pcap;
  20. struct input_dev *input;
  21. struct delayed_work work;
  22. u16 x, y;
  23. u16 pressure;
  24. u8 read_state;
  25. };
  26. #define SAMPLE_DELAY 20 /* msecs */
  27. #define X_AXIS_MIN 0
  28. #define X_AXIS_MAX 1023
  29. #define Y_AXIS_MAX X_AXIS_MAX
  30. #define Y_AXIS_MIN X_AXIS_MIN
  31. #define PRESSURE_MAX X_AXIS_MAX
  32. #define PRESSURE_MIN X_AXIS_MIN
  33. static void pcap_ts_read_xy(void *data, u16 res[2])
  34. {
  35. struct pcap_ts *pcap_ts = data;
  36. switch (pcap_ts->read_state) {
  37. case PCAP_ADC_TS_M_PRESSURE:
  38. /* pressure reading is unreliable */
  39. if (res[0] > PRESSURE_MIN && res[0] < PRESSURE_MAX)
  40. pcap_ts->pressure = res[0];
  41. pcap_ts->read_state = PCAP_ADC_TS_M_XY;
  42. schedule_delayed_work(&pcap_ts->work, 0);
  43. break;
  44. case PCAP_ADC_TS_M_XY:
  45. pcap_ts->y = res[0];
  46. pcap_ts->x = res[1];
  47. if (pcap_ts->x <= X_AXIS_MIN || pcap_ts->x >= X_AXIS_MAX ||
  48. pcap_ts->y <= Y_AXIS_MIN || pcap_ts->y >= Y_AXIS_MAX) {
  49. /* pen has been released */
  50. input_report_abs(pcap_ts->input, ABS_PRESSURE, 0);
  51. input_report_key(pcap_ts->input, BTN_TOUCH, 0);
  52. pcap_ts->read_state = PCAP_ADC_TS_M_STANDBY;
  53. schedule_delayed_work(&pcap_ts->work, 0);
  54. } else {
  55. /* pen is touching the screen */
  56. input_report_abs(pcap_ts->input, ABS_X, pcap_ts->x);
  57. input_report_abs(pcap_ts->input, ABS_Y, pcap_ts->y);
  58. input_report_key(pcap_ts->input, BTN_TOUCH, 1);
  59. input_report_abs(pcap_ts->input, ABS_PRESSURE,
  60. pcap_ts->pressure);
  61. /* switch back to pressure read mode */
  62. pcap_ts->read_state = PCAP_ADC_TS_M_PRESSURE;
  63. schedule_delayed_work(&pcap_ts->work,
  64. msecs_to_jiffies(SAMPLE_DELAY));
  65. }
  66. input_sync(pcap_ts->input);
  67. break;
  68. default:
  69. dev_warn(&pcap_ts->input->dev,
  70. "pcap_ts: Warning, unhandled read_state %d\n",
  71. pcap_ts->read_state);
  72. break;
  73. }
  74. }
  75. static void pcap_ts_work(struct work_struct *work)
  76. {
  77. struct delayed_work *dw = to_delayed_work(work);
  78. struct pcap_ts *pcap_ts = container_of(dw, struct pcap_ts, work);
  79. u8 ch[2];
  80. pcap_set_ts_bits(pcap_ts->pcap,
  81. pcap_ts->read_state << PCAP_ADC_TS_M_SHIFT);
  82. if (pcap_ts->read_state == PCAP_ADC_TS_M_STANDBY)
  83. return;
  84. /* start adc conversion */
  85. ch[0] = PCAP_ADC_CH_TS_X1;
  86. ch[1] = PCAP_ADC_CH_TS_Y1;
  87. pcap_adc_async(pcap_ts->pcap, PCAP_ADC_BANK_1, 0, ch,
  88. pcap_ts_read_xy, pcap_ts);
  89. }
  90. static irqreturn_t pcap_ts_event_touch(int pirq, void *data)
  91. {
  92. struct pcap_ts *pcap_ts = data;
  93. if (pcap_ts->read_state == PCAP_ADC_TS_M_STANDBY) {
  94. pcap_ts->read_state = PCAP_ADC_TS_M_PRESSURE;
  95. schedule_delayed_work(&pcap_ts->work, 0);
  96. }
  97. return IRQ_HANDLED;
  98. }
  99. static int pcap_ts_open(struct input_dev *dev)
  100. {
  101. struct pcap_ts *pcap_ts = input_get_drvdata(dev);
  102. pcap_ts->read_state = PCAP_ADC_TS_M_STANDBY;
  103. schedule_delayed_work(&pcap_ts->work, 0);
  104. return 0;
  105. }
  106. static void pcap_ts_close(struct input_dev *dev)
  107. {
  108. struct pcap_ts *pcap_ts = input_get_drvdata(dev);
  109. cancel_delayed_work_sync(&pcap_ts->work);
  110. pcap_ts->read_state = PCAP_ADC_TS_M_NONTS;
  111. pcap_set_ts_bits(pcap_ts->pcap,
  112. pcap_ts->read_state << PCAP_ADC_TS_M_SHIFT);
  113. }
  114. static int pcap_ts_probe(struct platform_device *pdev)
  115. {
  116. struct input_dev *input_dev;
  117. struct pcap_ts *pcap_ts;
  118. int err = -ENOMEM;
  119. pcap_ts = kzalloc(sizeof(*pcap_ts), GFP_KERNEL);
  120. if (!pcap_ts)
  121. return err;
  122. pcap_ts->pcap = dev_get_drvdata(pdev->dev.parent);
  123. platform_set_drvdata(pdev, pcap_ts);
  124. input_dev = input_allocate_device();
  125. if (!input_dev)
  126. goto fail;
  127. INIT_DELAYED_WORK(&pcap_ts->work, pcap_ts_work);
  128. pcap_ts->read_state = PCAP_ADC_TS_M_NONTS;
  129. pcap_set_ts_bits(pcap_ts->pcap,
  130. pcap_ts->read_state << PCAP_ADC_TS_M_SHIFT);
  131. pcap_ts->input = input_dev;
  132. input_set_drvdata(input_dev, pcap_ts);
  133. input_dev->name = "pcap-touchscreen";
  134. input_dev->phys = "pcap_ts/input0";
  135. input_dev->id.bustype = BUS_HOST;
  136. input_dev->id.vendor = 0x0001;
  137. input_dev->id.product = 0x0002;
  138. input_dev->id.version = 0x0100;
  139. input_dev->dev.parent = &pdev->dev;
  140. input_dev->open = pcap_ts_open;
  141. input_dev->close = pcap_ts_close;
  142. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  143. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  144. input_set_abs_params(input_dev, ABS_X, X_AXIS_MIN, X_AXIS_MAX, 0, 0);
  145. input_set_abs_params(input_dev, ABS_Y, Y_AXIS_MIN, Y_AXIS_MAX, 0, 0);
  146. input_set_abs_params(input_dev, ABS_PRESSURE, PRESSURE_MIN,
  147. PRESSURE_MAX, 0, 0);
  148. err = input_register_device(pcap_ts->input);
  149. if (err)
  150. goto fail_allocate;
  151. err = request_irq(pcap_to_irq(pcap_ts->pcap, PCAP_IRQ_TS),
  152. pcap_ts_event_touch, 0, "Touch Screen", pcap_ts);
  153. if (err)
  154. goto fail_register;
  155. return 0;
  156. fail_register:
  157. input_unregister_device(input_dev);
  158. goto fail;
  159. fail_allocate:
  160. input_free_device(input_dev);
  161. fail:
  162. kfree(pcap_ts);
  163. return err;
  164. }
  165. static int pcap_ts_remove(struct platform_device *pdev)
  166. {
  167. struct pcap_ts *pcap_ts = platform_get_drvdata(pdev);
  168. free_irq(pcap_to_irq(pcap_ts->pcap, PCAP_IRQ_TS), pcap_ts);
  169. cancel_delayed_work_sync(&pcap_ts->work);
  170. input_unregister_device(pcap_ts->input);
  171. kfree(pcap_ts);
  172. return 0;
  173. }
  174. #ifdef CONFIG_PM
  175. static int pcap_ts_suspend(struct device *dev)
  176. {
  177. struct pcap_ts *pcap_ts = dev_get_drvdata(dev);
  178. pcap_set_ts_bits(pcap_ts->pcap, PCAP_ADC_TS_REF_LOWPWR);
  179. return 0;
  180. }
  181. static int pcap_ts_resume(struct device *dev)
  182. {
  183. struct pcap_ts *pcap_ts = dev_get_drvdata(dev);
  184. pcap_set_ts_bits(pcap_ts->pcap,
  185. pcap_ts->read_state << PCAP_ADC_TS_M_SHIFT);
  186. return 0;
  187. }
  188. static const struct dev_pm_ops pcap_ts_pm_ops = {
  189. .suspend = pcap_ts_suspend,
  190. .resume = pcap_ts_resume,
  191. };
  192. #define PCAP_TS_PM_OPS (&pcap_ts_pm_ops)
  193. #else
  194. #define PCAP_TS_PM_OPS NULL
  195. #endif
  196. static struct platform_driver pcap_ts_driver = {
  197. .probe = pcap_ts_probe,
  198. .remove = pcap_ts_remove,
  199. .driver = {
  200. .name = "pcap-ts",
  201. .pm = PCAP_TS_PM_OPS,
  202. },
  203. };
  204. module_platform_driver(pcap_ts_driver);
  205. MODULE_DESCRIPTION("Motorola PCAP2 touchscreen driver");
  206. MODULE_AUTHOR("Daniel Ribeiro / Harald Welte");
  207. MODULE_LICENSE("GPL");
  208. MODULE_ALIAS("platform:pcap_ts");