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/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 <linux/mfd/ucb1x00.h>
  34. #include <mach/dma.h>
  35. #include <mach/collie.h>
  36. #include <asm/mach-types.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_report_key(idev, BTN_TOUCH, 1);
  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_report_key(idev, BTN_TOUCH, 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. DECLARE_WAITQUEUE(wait, current);
  182. int valid = 0;
  183. set_freezable();
  184. add_wait_queue(&ts->irq_wait, &wait);
  185. while (!kthread_should_stop()) {
  186. unsigned int x, y, p;
  187. signed long timeout;
  188. ts->restart = 0;
  189. ucb1x00_adc_enable(ts->ucb);
  190. x = ucb1x00_ts_read_xpos(ts);
  191. y = ucb1x00_ts_read_ypos(ts);
  192. p = ucb1x00_ts_read_pressure(ts);
  193. /*
  194. * Switch back to interrupt mode.
  195. */
  196. ucb1x00_ts_mode_int(ts);
  197. ucb1x00_adc_disable(ts->ucb);
  198. msleep(10);
  199. ucb1x00_enable(ts->ucb);
  200. if (ucb1x00_ts_pen_down(ts)) {
  201. set_current_state(TASK_INTERRUPTIBLE);
  202. ucb1x00_enable_irq(ts->ucb, UCB_IRQ_TSPX, machine_is_collie() ? UCB_RISING : UCB_FALLING);
  203. ucb1x00_disable(ts->ucb);
  204. /*
  205. * If we spat out a valid sample set last time,
  206. * spit out a "pen off" sample here.
  207. */
  208. if (valid) {
  209. ucb1x00_ts_event_release(ts);
  210. valid = 0;
  211. }
  212. timeout = MAX_SCHEDULE_TIMEOUT;
  213. } else {
  214. ucb1x00_disable(ts->ucb);
  215. /*
  216. * Filtering is policy. Policy belongs in user
  217. * space. We therefore leave it to user space
  218. * to do any filtering they please.
  219. */
  220. if (!ts->restart) {
  221. ucb1x00_ts_evt_add(ts, p, x, y);
  222. valid = 1;
  223. }
  224. set_current_state(TASK_INTERRUPTIBLE);
  225. timeout = HZ / 100;
  226. }
  227. try_to_freeze();
  228. schedule_timeout(timeout);
  229. }
  230. remove_wait_queue(&ts->irq_wait, &wait);
  231. ts->rtask = NULL;
  232. return 0;
  233. }
  234. /*
  235. * We only detect touch screen _touches_ with this interrupt
  236. * handler, and even then we just schedule our task.
  237. */
  238. static void ucb1x00_ts_irq(int idx, void *id)
  239. {
  240. struct ucb1x00_ts *ts = id;
  241. ucb1x00_disable_irq(ts->ucb, UCB_IRQ_TSPX, UCB_FALLING);
  242. wake_up(&ts->irq_wait);
  243. }
  244. static int ucb1x00_ts_open(struct input_dev *idev)
  245. {
  246. struct ucb1x00_ts *ts = input_get_drvdata(idev);
  247. int ret = 0;
  248. BUG_ON(ts->rtask);
  249. init_waitqueue_head(&ts->irq_wait);
  250. ret = ucb1x00_hook_irq(ts->ucb, UCB_IRQ_TSPX, ucb1x00_ts_irq, ts);
  251. if (ret < 0)
  252. goto out;
  253. /*
  254. * If we do this at all, we should allow the user to
  255. * measure and read the X and Y resistance at any time.
  256. */
  257. ucb1x00_adc_enable(ts->ucb);
  258. ts->x_res = ucb1x00_ts_read_xres(ts);
  259. ts->y_res = ucb1x00_ts_read_yres(ts);
  260. ucb1x00_adc_disable(ts->ucb);
  261. ts->rtask = kthread_run(ucb1x00_thread, ts, "ktsd");
  262. if (!IS_ERR(ts->rtask)) {
  263. ret = 0;
  264. } else {
  265. ucb1x00_free_irq(ts->ucb, UCB_IRQ_TSPX, ts);
  266. ts->rtask = NULL;
  267. ret = -EFAULT;
  268. }
  269. out:
  270. return ret;
  271. }
  272. /*
  273. * Release touchscreen resources. Disable IRQs.
  274. */
  275. static void ucb1x00_ts_close(struct input_dev *idev)
  276. {
  277. struct ucb1x00_ts *ts = input_get_drvdata(idev);
  278. if (ts->rtask)
  279. kthread_stop(ts->rtask);
  280. ucb1x00_enable(ts->ucb);
  281. ucb1x00_free_irq(ts->ucb, UCB_IRQ_TSPX, ts);
  282. ucb1x00_reg_write(ts->ucb, UCB_TS_CR, 0);
  283. ucb1x00_disable(ts->ucb);
  284. }
  285. #ifdef CONFIG_PM
  286. static int ucb1x00_ts_resume(struct ucb1x00_dev *dev)
  287. {
  288. struct ucb1x00_ts *ts = dev->priv;
  289. if (ts->rtask != NULL) {
  290. /*
  291. * Restart the TS thread to ensure the
  292. * TS interrupt mode is set up again
  293. * after sleep.
  294. */
  295. ts->restart = 1;
  296. wake_up(&ts->irq_wait);
  297. }
  298. return 0;
  299. }
  300. #else
  301. #define ucb1x00_ts_resume NULL
  302. #endif
  303. /*
  304. * Initialisation.
  305. */
  306. static int ucb1x00_ts_add(struct ucb1x00_dev *dev)
  307. {
  308. struct ucb1x00_ts *ts;
  309. struct input_dev *idev;
  310. int err;
  311. ts = kzalloc(sizeof(struct ucb1x00_ts), GFP_KERNEL);
  312. idev = input_allocate_device();
  313. if (!ts || !idev) {
  314. err = -ENOMEM;
  315. goto fail;
  316. }
  317. ts->ucb = dev->ucb;
  318. ts->idev = idev;
  319. ts->adcsync = adcsync ? UCB_SYNC : UCB_NOSYNC;
  320. idev->name = "Touchscreen panel";
  321. idev->id.product = ts->ucb->id;
  322. idev->open = ucb1x00_ts_open;
  323. idev->close = ucb1x00_ts_close;
  324. idev->evbit[0] = BIT_MASK(EV_ABS) | BIT_MASK(EV_KEY);
  325. idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  326. input_set_drvdata(idev, ts);
  327. ucb1x00_adc_enable(ts->ucb);
  328. ts->x_res = ucb1x00_ts_read_xres(ts);
  329. ts->y_res = ucb1x00_ts_read_yres(ts);
  330. ucb1x00_adc_disable(ts->ucb);
  331. input_set_abs_params(idev, ABS_X, 0, ts->x_res, 0, 0);
  332. input_set_abs_params(idev, ABS_Y, 0, ts->y_res, 0, 0);
  333. input_set_abs_params(idev, ABS_PRESSURE, 0, 0, 0, 0);
  334. err = input_register_device(idev);
  335. if (err)
  336. goto fail;
  337. dev->priv = ts;
  338. return 0;
  339. fail:
  340. input_free_device(idev);
  341. kfree(ts);
  342. return err;
  343. }
  344. static void ucb1x00_ts_remove(struct ucb1x00_dev *dev)
  345. {
  346. struct ucb1x00_ts *ts = dev->priv;
  347. input_unregister_device(ts->idev);
  348. kfree(ts);
  349. }
  350. static struct ucb1x00_driver ucb1x00_ts_driver = {
  351. .add = ucb1x00_ts_add,
  352. .remove = ucb1x00_ts_remove,
  353. .resume = ucb1x00_ts_resume,
  354. };
  355. static int __init ucb1x00_ts_init(void)
  356. {
  357. return ucb1x00_register_driver(&ucb1x00_ts_driver);
  358. }
  359. static void __exit ucb1x00_ts_exit(void)
  360. {
  361. ucb1x00_unregister_driver(&ucb1x00_ts_driver);
  362. }
  363. module_param(adcsync, int, 0444);
  364. module_init(ucb1x00_ts_init);
  365. module_exit(ucb1x00_ts_exit);
  366. MODULE_AUTHOR("Russell King <rmk@arm.linux.org.uk>");
  367. MODULE_DESCRIPTION("UCB1x00 touchscreen driver");
  368. MODULE_LICENSE("GPL");