pcap_ts.c 6.8 KB

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