ucb1x00-ts.c 11 KB

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