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/interrupt.h>
  24. #include <linux/sched.h>
  25. #include <linux/spinlock.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/freezer.h>
  32. #include <linux/slab.h>
  33. #include <linux/kthread.h>
  34. #include <linux/mfd/ucb1x00.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. spinlock_t irq_lock;
  41. unsigned irq_disabled;
  42. wait_queue_head_t irq_wait;
  43. struct task_struct *rtask;
  44. u16 x_res;
  45. u16 y_res;
  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_report_key(idev, BTN_TOUCH, 1);
  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_report_key(idev, BTN_TOUCH, 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. DECLARE_WAITQUEUE(wait, current);
  183. bool frozen, ignore = false;
  184. int valid = 0;
  185. set_freezable();
  186. add_wait_queue(&ts->irq_wait, &wait);
  187. while (!kthread_freezable_should_stop(&frozen)) {
  188. unsigned int x, y, p;
  189. signed long timeout;
  190. if (frozen)
  191. ignore = true;
  192. ucb1x00_adc_enable(ts->ucb);
  193. x = ucb1x00_ts_read_xpos(ts);
  194. y = ucb1x00_ts_read_ypos(ts);
  195. p = ucb1x00_ts_read_pressure(ts);
  196. /*
  197. * Switch back to interrupt mode.
  198. */
  199. ucb1x00_ts_mode_int(ts);
  200. ucb1x00_adc_disable(ts->ucb);
  201. msleep(10);
  202. ucb1x00_enable(ts->ucb);
  203. if (ucb1x00_ts_pen_down(ts)) {
  204. set_current_state(TASK_INTERRUPTIBLE);
  205. spin_lock_irq(&ts->irq_lock);
  206. if (ts->irq_disabled) {
  207. ts->irq_disabled = 0;
  208. enable_irq(ts->ucb->irq_base + UCB_IRQ_TSPX);
  209. }
  210. spin_unlock_irq(&ts->irq_lock);
  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 (!ignore) {
  229. ucb1x00_ts_evt_add(ts, p, x, y);
  230. valid = 1;
  231. }
  232. set_current_state(TASK_INTERRUPTIBLE);
  233. timeout = HZ / 100;
  234. }
  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 irqreturn_t ucb1x00_ts_irq(int irq, void *id)
  246. {
  247. struct ucb1x00_ts *ts = id;
  248. spin_lock(&ts->irq_lock);
  249. ts->irq_disabled = 1;
  250. disable_irq_nosync(ts->ucb->irq_base + UCB_IRQ_TSPX);
  251. spin_unlock(&ts->irq_lock);
  252. wake_up(&ts->irq_wait);
  253. return IRQ_HANDLED;
  254. }
  255. static int ucb1x00_ts_open(struct input_dev *idev)
  256. {
  257. struct ucb1x00_ts *ts = input_get_drvdata(idev);
  258. unsigned long flags = 0;
  259. int ret = 0;
  260. BUG_ON(ts->rtask);
  261. if (machine_is_collie())
  262. flags = IRQF_TRIGGER_RISING;
  263. else
  264. flags = IRQF_TRIGGER_FALLING;
  265. ts->irq_disabled = 0;
  266. init_waitqueue_head(&ts->irq_wait);
  267. ret = request_irq(ts->ucb->irq_base + UCB_IRQ_TSPX, ucb1x00_ts_irq,
  268. flags, "ucb1x00-ts", ts);
  269. if (ret < 0)
  270. goto out;
  271. /*
  272. * If we do this at all, we should allow the user to
  273. * measure and read the X and Y resistance at any time.
  274. */
  275. ucb1x00_adc_enable(ts->ucb);
  276. ts->x_res = ucb1x00_ts_read_xres(ts);
  277. ts->y_res = ucb1x00_ts_read_yres(ts);
  278. ucb1x00_adc_disable(ts->ucb);
  279. ts->rtask = kthread_run(ucb1x00_thread, ts, "ktsd");
  280. if (!IS_ERR(ts->rtask)) {
  281. ret = 0;
  282. } else {
  283. free_irq(ts->ucb->irq_base + UCB_IRQ_TSPX, ts);
  284. ts->rtask = NULL;
  285. ret = -EFAULT;
  286. }
  287. out:
  288. return ret;
  289. }
  290. /*
  291. * Release touchscreen resources. Disable IRQs.
  292. */
  293. static void ucb1x00_ts_close(struct input_dev *idev)
  294. {
  295. struct ucb1x00_ts *ts = input_get_drvdata(idev);
  296. if (ts->rtask)
  297. kthread_stop(ts->rtask);
  298. ucb1x00_enable(ts->ucb);
  299. free_irq(ts->ucb->irq_base + UCB_IRQ_TSPX, ts);
  300. ucb1x00_reg_write(ts->ucb, UCB_TS_CR, 0);
  301. ucb1x00_disable(ts->ucb);
  302. }
  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. spin_lock_init(&ts->irq_lock);
  321. idev->name = "Touchscreen panel";
  322. idev->id.product = ts->ucb->id;
  323. idev->open = ucb1x00_ts_open;
  324. idev->close = ucb1x00_ts_close;
  325. idev->dev.parent = &ts->ucb->dev;
  326. idev->evbit[0] = BIT_MASK(EV_ABS) | BIT_MASK(EV_KEY);
  327. idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  328. input_set_drvdata(idev, ts);
  329. ucb1x00_adc_enable(ts->ucb);
  330. ts->x_res = ucb1x00_ts_read_xres(ts);
  331. ts->y_res = ucb1x00_ts_read_yres(ts);
  332. ucb1x00_adc_disable(ts->ucb);
  333. input_set_abs_params(idev, ABS_X, 0, ts->x_res, 0, 0);
  334. input_set_abs_params(idev, ABS_Y, 0, ts->y_res, 0, 0);
  335. input_set_abs_params(idev, ABS_PRESSURE, 0, 0, 0, 0);
  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. };
  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");