pcap_ts.c 6.5 KB

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