ucb1x00-ts.c 11 KB

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