ucb1x00-ts.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /*
  2. * Touchscreen driver for UCB1x00-based touchscreens
  3. *
  4. * Copyright (C) 2001 Russell King, All Rights Reserved.
  5. * Copyright (C) 2005 Pavel Machek
  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. * 21-Jan-2002 <jco@ict.es> :
  12. *
  13. * Added support for synchronous A/D mode. This mode is useful to
  14. * avoid noise induced in the touchpanel by the LCD, provided that
  15. * the UCB1x00 has a valid LCD sync signal routed to its ADCSYNC pin.
  16. * It is important to note that the signal connected to the ADCSYNC
  17. * pin should provide pulses even when the LCD is blanked, otherwise
  18. * a pen touch needed to unblank the LCD will never be read.
  19. */
  20. #include <linux/config.h>
  21. #include <linux/module.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/init.h>
  24. #include <linux/smp.h>
  25. #include <linux/smp_lock.h>
  26. #include <linux/sched.h>
  27. #include <linux/completion.h>
  28. #include <linux/delay.h>
  29. #include <linux/string.h>
  30. #include <linux/input.h>
  31. #include <linux/device.h>
  32. #include <linux/suspend.h>
  33. #include <linux/slab.h>
  34. #include <linux/kthread.h>
  35. #include <asm/dma.h>
  36. #include <asm/semaphore.h>
  37. #include "ucb1x00.h"
  38. struct ucb1x00_ts {
  39. struct input_dev idev;
  40. struct ucb1x00 *ucb;
  41. wait_queue_head_t irq_wait;
  42. struct task_struct *rtask;
  43. u16 x_res;
  44. u16 y_res;
  45. int restart:1;
  46. int adcsync:1;
  47. };
  48. static int adcsync;
  49. static inline void ucb1x00_ts_evt_add(struct ucb1x00_ts *ts, u16 pressure, u16 x, u16 y)
  50. {
  51. input_report_abs(&ts->idev, ABS_X, x);
  52. input_report_abs(&ts->idev, ABS_Y, y);
  53. input_report_abs(&ts->idev, ABS_PRESSURE, pressure);
  54. input_sync(&ts->idev);
  55. }
  56. static inline void ucb1x00_ts_event_release(struct ucb1x00_ts *ts)
  57. {
  58. input_report_abs(&ts->idev, ABS_PRESSURE, 0);
  59. input_sync(&ts->idev);
  60. }
  61. /*
  62. * Switch to interrupt mode.
  63. */
  64. static inline void ucb1x00_ts_mode_int(struct ucb1x00_ts *ts)
  65. {
  66. ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
  67. UCB_TS_CR_TSMX_POW | UCB_TS_CR_TSPX_POW |
  68. UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_GND |
  69. UCB_TS_CR_MODE_INT);
  70. }
  71. /*
  72. * Switch to pressure mode, and read pressure. We don't need to wait
  73. * here, since both plates are being driven.
  74. */
  75. static inline unsigned int ucb1x00_ts_read_pressure(struct ucb1x00_ts *ts)
  76. {
  77. ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
  78. UCB_TS_CR_TSMX_POW | UCB_TS_CR_TSPX_POW |
  79. UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_GND |
  80. UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
  81. return ucb1x00_adc_read(ts->ucb, UCB_ADC_INP_TSPY, ts->adcsync);
  82. }
  83. /*
  84. * Switch to X position mode and measure Y plate. We switch the plate
  85. * configuration in pressure mode, then switch to position mode. This
  86. * gives a faster response time. Even so, we need to wait about 55us
  87. * for things to stabilise.
  88. */
  89. static inline unsigned int ucb1x00_ts_read_xpos(struct ucb1x00_ts *ts)
  90. {
  91. ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
  92. UCB_TS_CR_TSMX_GND | UCB_TS_CR_TSPX_POW |
  93. UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
  94. ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
  95. UCB_TS_CR_TSMX_GND | UCB_TS_CR_TSPX_POW |
  96. UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
  97. ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
  98. UCB_TS_CR_TSMX_GND | UCB_TS_CR_TSPX_POW |
  99. UCB_TS_CR_MODE_POS | UCB_TS_CR_BIAS_ENA);
  100. udelay(55);
  101. return ucb1x00_adc_read(ts->ucb, UCB_ADC_INP_TSPY, ts->adcsync);
  102. }
  103. /*
  104. * Switch to Y position mode and measure X plate. We switch the plate
  105. * configuration in pressure mode, then switch to position mode. This
  106. * gives a faster response time. Even so, we need to wait about 55us
  107. * for things to stabilise.
  108. */
  109. static inline unsigned int ucb1x00_ts_read_ypos(struct ucb1x00_ts *ts)
  110. {
  111. ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
  112. UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_POW |
  113. UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
  114. ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
  115. UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_POW |
  116. UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
  117. ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
  118. UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_POW |
  119. UCB_TS_CR_MODE_POS | UCB_TS_CR_BIAS_ENA);
  120. udelay(55);
  121. return ucb1x00_adc_read(ts->ucb, UCB_ADC_INP_TSPX, ts->adcsync);
  122. }
  123. /*
  124. * Switch to X plate resistance mode. Set MX to ground, PX to
  125. * supply. Measure current.
  126. */
  127. static inline unsigned int ucb1x00_ts_read_xres(struct ucb1x00_ts *ts)
  128. {
  129. ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
  130. UCB_TS_CR_TSMX_GND | UCB_TS_CR_TSPX_POW |
  131. UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
  132. return ucb1x00_adc_read(ts->ucb, 0, ts->adcsync);
  133. }
  134. /*
  135. * Switch to Y plate resistance mode. Set MY to ground, PY to
  136. * supply. Measure current.
  137. */
  138. static inline unsigned int ucb1x00_ts_read_yres(struct ucb1x00_ts *ts)
  139. {
  140. ucb1x00_reg_write(ts->ucb, UCB_TS_CR,
  141. UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_POW |
  142. UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
  143. return ucb1x00_adc_read(ts->ucb, 0, ts->adcsync);
  144. }
  145. /*
  146. * This is a RT kernel thread that handles the ADC accesses
  147. * (mainly so we can use semaphores in the UCB1200 core code
  148. * to serialise accesses to the ADC).
  149. */
  150. static int ucb1x00_thread(void *_ts)
  151. {
  152. struct ucb1x00_ts *ts = _ts;
  153. struct task_struct *tsk = current;
  154. DECLARE_WAITQUEUE(wait, tsk);
  155. int valid;
  156. /*
  157. * We could run as a real-time thread. However, thus far
  158. * this doesn't seem to be necessary.
  159. */
  160. // tsk->policy = SCHED_FIFO;
  161. // tsk->rt_priority = 1;
  162. valid = 0;
  163. add_wait_queue(&ts->irq_wait, &wait);
  164. while (!kthread_should_stop()) {
  165. unsigned int x, y, p, val;
  166. signed long timeout;
  167. ts->restart = 0;
  168. ucb1x00_adc_enable(ts->ucb);
  169. x = ucb1x00_ts_read_xpos(ts);
  170. y = ucb1x00_ts_read_ypos(ts);
  171. p = ucb1x00_ts_read_pressure(ts);
  172. /*
  173. * Switch back to interrupt mode.
  174. */
  175. ucb1x00_ts_mode_int(ts);
  176. ucb1x00_adc_disable(ts->ucb);
  177. msleep(10);
  178. ucb1x00_enable(ts->ucb);
  179. val = ucb1x00_reg_read(ts->ucb, UCB_TS_CR);
  180. if (val & (UCB_TS_CR_TSPX_LOW | UCB_TS_CR_TSMX_LOW)) {
  181. set_task_state(tsk, TASK_INTERRUPTIBLE);
  182. ucb1x00_enable_irq(ts->ucb, UCB_IRQ_TSPX, UCB_FALLING);
  183. ucb1x00_disable(ts->ucb);
  184. /*
  185. * If we spat out a valid sample set last time,
  186. * spit out a "pen off" sample here.
  187. */
  188. if (valid) {
  189. ucb1x00_ts_event_release(ts);
  190. valid = 0;
  191. }
  192. timeout = MAX_SCHEDULE_TIMEOUT;
  193. } else {
  194. ucb1x00_disable(ts->ucb);
  195. /*
  196. * Filtering is policy. Policy belongs in user
  197. * space. We therefore leave it to user space
  198. * to do any filtering they please.
  199. */
  200. if (!ts->restart) {
  201. ucb1x00_ts_evt_add(ts, p, x, y);
  202. valid = 1;
  203. }
  204. set_task_state(tsk, TASK_INTERRUPTIBLE);
  205. timeout = HZ / 100;
  206. }
  207. try_to_freeze();
  208. schedule_timeout(timeout);
  209. }
  210. remove_wait_queue(&ts->irq_wait, &wait);
  211. ts->rtask = NULL;
  212. return 0;
  213. }
  214. /*
  215. * We only detect touch screen _touches_ with this interrupt
  216. * handler, and even then we just schedule our task.
  217. */
  218. static void ucb1x00_ts_irq(int idx, void *id)
  219. {
  220. struct ucb1x00_ts *ts = id;
  221. ucb1x00_disable_irq(ts->ucb, UCB_IRQ_TSPX, UCB_FALLING);
  222. wake_up(&ts->irq_wait);
  223. }
  224. static int ucb1x00_ts_open(struct input_dev *idev)
  225. {
  226. struct ucb1x00_ts *ts = (struct ucb1x00_ts *)idev;
  227. int ret = 0;
  228. BUG_ON(ts->rtask);
  229. init_waitqueue_head(&ts->irq_wait);
  230. ret = ucb1x00_hook_irq(ts->ucb, UCB_IRQ_TSPX, ucb1x00_ts_irq, ts);
  231. if (ret < 0)
  232. goto out;
  233. /*
  234. * If we do this at all, we should allow the user to
  235. * measure and read the X and Y resistance at any time.
  236. */
  237. ucb1x00_adc_enable(ts->ucb);
  238. ts->x_res = ucb1x00_ts_read_xres(ts);
  239. ts->y_res = ucb1x00_ts_read_yres(ts);
  240. ucb1x00_adc_disable(ts->ucb);
  241. ts->rtask = kthread_run(ucb1x00_thread, ts, "ktsd");
  242. if (!IS_ERR(ts->rtask)) {
  243. ret = 0;
  244. } else {
  245. ucb1x00_free_irq(ts->ucb, UCB_IRQ_TSPX, ts);
  246. ts->rtask = NULL;
  247. ret = -EFAULT;
  248. }
  249. out:
  250. return ret;
  251. }
  252. /*
  253. * Release touchscreen resources. Disable IRQs.
  254. */
  255. static void ucb1x00_ts_close(struct input_dev *idev)
  256. {
  257. struct ucb1x00_ts *ts = (struct ucb1x00_ts *)idev;
  258. if (ts->rtask)
  259. kthread_stop(ts->rtask);
  260. ucb1x00_enable(ts->ucb);
  261. ucb1x00_free_irq(ts->ucb, UCB_IRQ_TSPX, ts);
  262. ucb1x00_reg_write(ts->ucb, UCB_TS_CR, 0);
  263. ucb1x00_disable(ts->ucb);
  264. }
  265. #ifdef CONFIG_PM
  266. static int ucb1x00_ts_resume(struct ucb1x00_dev *dev)
  267. {
  268. struct ucb1x00_ts *ts = dev->priv;
  269. if (ts->rtask != NULL) {
  270. /*
  271. * Restart the TS thread to ensure the
  272. * TS interrupt mode is set up again
  273. * after sleep.
  274. */
  275. ts->restart = 1;
  276. wake_up(&ts->irq_wait);
  277. }
  278. return 0;
  279. }
  280. #else
  281. #define ucb1x00_ts_resume NULL
  282. #endif
  283. /*
  284. * Initialisation.
  285. */
  286. static int ucb1x00_ts_add(struct ucb1x00_dev *dev)
  287. {
  288. struct ucb1x00_ts *ts;
  289. ts = kmalloc(sizeof(struct ucb1x00_ts), GFP_KERNEL);
  290. if (!ts)
  291. return -ENOMEM;
  292. memset(ts, 0, sizeof(struct ucb1x00_ts));
  293. ts->ucb = dev->ucb;
  294. ts->adcsync = adcsync ? UCB_SYNC : UCB_NOSYNC;
  295. ts->idev.name = "Touchscreen panel";
  296. ts->idev.id.product = ts->ucb->id;
  297. ts->idev.open = ucb1x00_ts_open;
  298. ts->idev.close = ucb1x00_ts_close;
  299. __set_bit(EV_ABS, ts->idev.evbit);
  300. __set_bit(ABS_X, ts->idev.absbit);
  301. __set_bit(ABS_Y, ts->idev.absbit);
  302. __set_bit(ABS_PRESSURE, ts->idev.absbit);
  303. input_register_device(&ts->idev);
  304. dev->priv = ts;
  305. return 0;
  306. }
  307. static void ucb1x00_ts_remove(struct ucb1x00_dev *dev)
  308. {
  309. struct ucb1x00_ts *ts = dev->priv;
  310. input_unregister_device(&ts->idev);
  311. kfree(ts);
  312. }
  313. static struct ucb1x00_driver ucb1x00_ts_driver = {
  314. .add = ucb1x00_ts_add,
  315. .remove = ucb1x00_ts_remove,
  316. .resume = ucb1x00_ts_resume,
  317. };
  318. static int __init ucb1x00_ts_init(void)
  319. {
  320. return ucb1x00_register_driver(&ucb1x00_ts_driver);
  321. }
  322. static void __exit ucb1x00_ts_exit(void)
  323. {
  324. ucb1x00_unregister_driver(&ucb1x00_ts_driver);
  325. }
  326. module_param(adcsync, int, 0444);
  327. module_init(ucb1x00_ts_init);
  328. module_exit(ucb1x00_ts_exit);
  329. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  330. MODULE_DESCRIPTION("UCB1x00 touchscreen driver");
  331. MODULE_LICENSE("GPL");