ucb1x00-ts.c 11 KB

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