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