ucb1x00-ts.c 11 KB

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