ucb1400_ts.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. * Philips UCB1400 touchscreen driver
  3. *
  4. * Author: Nicolas Pitre
  5. * Created: September 25, 2006
  6. * Copyright: MontaVista Software, Inc.
  7. *
  8. * Spliting done by: Marek Vasut <marek.vasut@gmail.com>
  9. * If something doesn't work and it worked before spliting, e-mail me,
  10. * dont bother Nicolas please ;-)
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2 as
  14. * published by the Free Software Foundation.
  15. *
  16. * This code is heavily based on ucb1x00-*.c copyrighted by Russell King
  17. * covering the UCB1100, UCB1200 and UCB1300.. Support for the UCB1400 has
  18. * been made separate from ucb1x00-core/ucb1x00-ts on Russell's request.
  19. */
  20. #include <linux/module.h>
  21. #include <linux/delay.h>
  22. #include <linux/sched.h>
  23. #include <linux/wait.h>
  24. #include <linux/input.h>
  25. #include <linux/device.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/ucb1400.h>
  28. #define UCB1400_TS_POLL_PERIOD 10 /* ms */
  29. static bool adcsync;
  30. static int ts_delay = 55; /* us */
  31. static int ts_delay_pressure; /* us */
  32. /* Switch to interrupt mode. */
  33. static void ucb1400_ts_mode_int(struct ucb1400_ts *ucb)
  34. {
  35. ucb1400_reg_write(ucb->ac97, UCB_TS_CR,
  36. UCB_TS_CR_TSMX_POW | UCB_TS_CR_TSPX_POW |
  37. UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_GND |
  38. UCB_TS_CR_MODE_INT);
  39. }
  40. /*
  41. * Switch to pressure mode, and read pressure. We don't need to wait
  42. * here, since both plates are being driven.
  43. */
  44. static unsigned int ucb1400_ts_read_pressure(struct ucb1400_ts *ucb)
  45. {
  46. ucb1400_reg_write(ucb->ac97, UCB_TS_CR,
  47. UCB_TS_CR_TSMX_POW | UCB_TS_CR_TSPX_POW |
  48. UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_GND |
  49. UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
  50. udelay(ts_delay_pressure);
  51. return ucb1400_adc_read(ucb->ac97, UCB_ADC_INP_TSPY, adcsync);
  52. }
  53. /*
  54. * Switch to X position mode and measure Y plate. We switch the plate
  55. * configuration in pressure mode, then switch to position mode. This
  56. * gives a faster response time. Even so, we need to wait about 55us
  57. * for things to stabilise.
  58. */
  59. static unsigned int ucb1400_ts_read_xpos(struct ucb1400_ts *ucb)
  60. {
  61. ucb1400_reg_write(ucb->ac97, UCB_TS_CR,
  62. UCB_TS_CR_TSMX_GND | UCB_TS_CR_TSPX_POW |
  63. UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
  64. ucb1400_reg_write(ucb->ac97, UCB_TS_CR,
  65. UCB_TS_CR_TSMX_GND | UCB_TS_CR_TSPX_POW |
  66. UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
  67. ucb1400_reg_write(ucb->ac97, UCB_TS_CR,
  68. UCB_TS_CR_TSMX_GND | UCB_TS_CR_TSPX_POW |
  69. UCB_TS_CR_MODE_POS | UCB_TS_CR_BIAS_ENA);
  70. udelay(ts_delay);
  71. return ucb1400_adc_read(ucb->ac97, UCB_ADC_INP_TSPY, adcsync);
  72. }
  73. /*
  74. * Switch to Y position mode and measure X plate. We switch the plate
  75. * configuration in pressure mode, then switch to position mode. This
  76. * gives a faster response time. Even so, we need to wait about 55us
  77. * for things to stabilise.
  78. */
  79. static int ucb1400_ts_read_ypos(struct ucb1400_ts *ucb)
  80. {
  81. ucb1400_reg_write(ucb->ac97, UCB_TS_CR,
  82. UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_POW |
  83. UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
  84. ucb1400_reg_write(ucb->ac97, UCB_TS_CR,
  85. UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_POW |
  86. UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
  87. ucb1400_reg_write(ucb->ac97, UCB_TS_CR,
  88. UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_POW |
  89. UCB_TS_CR_MODE_POS | UCB_TS_CR_BIAS_ENA);
  90. udelay(ts_delay);
  91. return ucb1400_adc_read(ucb->ac97, UCB_ADC_INP_TSPX, adcsync);
  92. }
  93. /*
  94. * Switch to X plate resistance mode. Set MX to ground, PX to
  95. * supply. Measure current.
  96. */
  97. static unsigned int ucb1400_ts_read_xres(struct ucb1400_ts *ucb)
  98. {
  99. ucb1400_reg_write(ucb->ac97, UCB_TS_CR,
  100. UCB_TS_CR_TSMX_GND | UCB_TS_CR_TSPX_POW |
  101. UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
  102. return ucb1400_adc_read(ucb->ac97, 0, adcsync);
  103. }
  104. /*
  105. * Switch to Y plate resistance mode. Set MY to ground, PY to
  106. * supply. Measure current.
  107. */
  108. static unsigned int ucb1400_ts_read_yres(struct ucb1400_ts *ucb)
  109. {
  110. ucb1400_reg_write(ucb->ac97, UCB_TS_CR,
  111. UCB_TS_CR_TSMY_GND | UCB_TS_CR_TSPY_POW |
  112. UCB_TS_CR_MODE_PRES | UCB_TS_CR_BIAS_ENA);
  113. return ucb1400_adc_read(ucb->ac97, 0, adcsync);
  114. }
  115. static int ucb1400_ts_pen_up(struct ucb1400_ts *ucb)
  116. {
  117. unsigned short val = ucb1400_reg_read(ucb->ac97, UCB_TS_CR);
  118. return val & (UCB_TS_CR_TSPX_LOW | UCB_TS_CR_TSMX_LOW);
  119. }
  120. static void ucb1400_ts_irq_enable(struct ucb1400_ts *ucb)
  121. {
  122. ucb1400_reg_write(ucb->ac97, UCB_IE_CLEAR, UCB_IE_TSPX);
  123. ucb1400_reg_write(ucb->ac97, UCB_IE_CLEAR, 0);
  124. ucb1400_reg_write(ucb->ac97, UCB_IE_FAL, UCB_IE_TSPX);
  125. }
  126. static void ucb1400_ts_irq_disable(struct ucb1400_ts *ucb)
  127. {
  128. ucb1400_reg_write(ucb->ac97, UCB_IE_FAL, 0);
  129. }
  130. static void ucb1400_ts_report_event(struct input_dev *idev, u16 pressure, u16 x, u16 y)
  131. {
  132. input_report_abs(idev, ABS_X, x);
  133. input_report_abs(idev, ABS_Y, y);
  134. input_report_abs(idev, ABS_PRESSURE, pressure);
  135. input_report_key(idev, BTN_TOUCH, 1);
  136. input_sync(idev);
  137. }
  138. static void ucb1400_ts_event_release(struct input_dev *idev)
  139. {
  140. input_report_abs(idev, ABS_PRESSURE, 0);
  141. input_report_key(idev, BTN_TOUCH, 0);
  142. input_sync(idev);
  143. }
  144. static void ucb1400_clear_pending_irq(struct ucb1400_ts *ucb)
  145. {
  146. unsigned int isr;
  147. isr = ucb1400_reg_read(ucb->ac97, UCB_IE_STATUS);
  148. ucb1400_reg_write(ucb->ac97, UCB_IE_CLEAR, isr);
  149. ucb1400_reg_write(ucb->ac97, UCB_IE_CLEAR, 0);
  150. if (isr & UCB_IE_TSPX)
  151. ucb1400_ts_irq_disable(ucb);
  152. else
  153. dev_dbg(&ucb->ts_idev->dev,
  154. "ucb1400: unexpected IE_STATUS = %#x\n", isr);
  155. }
  156. /*
  157. * A restriction with interrupts exists when using the ucb1400, as
  158. * the codec read/write routines may sleep while waiting for codec
  159. * access completion and uses semaphores for access control to the
  160. * AC97 bus. Therefore the driver is forced to use threaded interrupt
  161. * handler.
  162. */
  163. static irqreturn_t ucb1400_irq(int irqnr, void *devid)
  164. {
  165. struct ucb1400_ts *ucb = devid;
  166. unsigned int x, y, p;
  167. bool penup;
  168. if (unlikely(irqnr != ucb->irq))
  169. return IRQ_NONE;
  170. ucb1400_clear_pending_irq(ucb);
  171. /* Start with a small delay before checking pendown state */
  172. msleep(UCB1400_TS_POLL_PERIOD);
  173. while (!ucb->stopped && !(penup = ucb1400_ts_pen_up(ucb))) {
  174. ucb1400_adc_enable(ucb->ac97);
  175. x = ucb1400_ts_read_xpos(ucb);
  176. y = ucb1400_ts_read_ypos(ucb);
  177. p = ucb1400_ts_read_pressure(ucb);
  178. ucb1400_adc_disable(ucb->ac97);
  179. ucb1400_ts_report_event(ucb->ts_idev, p, x, y);
  180. wait_event_timeout(ucb->ts_wait, ucb->stopped,
  181. msecs_to_jiffies(UCB1400_TS_POLL_PERIOD));
  182. }
  183. ucb1400_ts_event_release(ucb->ts_idev);
  184. if (!ucb->stopped) {
  185. /* Switch back to interrupt mode. */
  186. ucb1400_ts_mode_int(ucb);
  187. ucb1400_ts_irq_enable(ucb);
  188. }
  189. return IRQ_HANDLED;
  190. }
  191. static void ucb1400_ts_stop(struct ucb1400_ts *ucb)
  192. {
  193. /* Signal IRQ thread to stop polling and disable the handler. */
  194. ucb->stopped = true;
  195. mb();
  196. wake_up(&ucb->ts_wait);
  197. disable_irq(ucb->irq);
  198. ucb1400_ts_irq_disable(ucb);
  199. ucb1400_reg_write(ucb->ac97, UCB_TS_CR, 0);
  200. }
  201. /* Must be called with ts->lock held */
  202. static void ucb1400_ts_start(struct ucb1400_ts *ucb)
  203. {
  204. /* Tell IRQ thread that it may poll the device. */
  205. ucb->stopped = false;
  206. mb();
  207. ucb1400_ts_mode_int(ucb);
  208. ucb1400_ts_irq_enable(ucb);
  209. enable_irq(ucb->irq);
  210. }
  211. static int ucb1400_ts_open(struct input_dev *idev)
  212. {
  213. struct ucb1400_ts *ucb = input_get_drvdata(idev);
  214. ucb1400_ts_start(ucb);
  215. return 0;
  216. }
  217. static void ucb1400_ts_close(struct input_dev *idev)
  218. {
  219. struct ucb1400_ts *ucb = input_get_drvdata(idev);
  220. ucb1400_ts_stop(ucb);
  221. }
  222. #ifndef NO_IRQ
  223. #define NO_IRQ 0
  224. #endif
  225. /*
  226. * Try to probe our interrupt, rather than relying on lots of
  227. * hard-coded machine dependencies.
  228. */
  229. static int ucb1400_ts_detect_irq(struct ucb1400_ts *ucb,
  230. struct platform_device *pdev)
  231. {
  232. unsigned long mask, timeout;
  233. mask = probe_irq_on();
  234. /* Enable the ADC interrupt. */
  235. ucb1400_reg_write(ucb->ac97, UCB_IE_RIS, UCB_IE_ADC);
  236. ucb1400_reg_write(ucb->ac97, UCB_IE_FAL, UCB_IE_ADC);
  237. ucb1400_reg_write(ucb->ac97, UCB_IE_CLEAR, 0xffff);
  238. ucb1400_reg_write(ucb->ac97, UCB_IE_CLEAR, 0);
  239. /* Cause an ADC interrupt. */
  240. ucb1400_reg_write(ucb->ac97, UCB_ADC_CR, UCB_ADC_ENA);
  241. ucb1400_reg_write(ucb->ac97, UCB_ADC_CR, UCB_ADC_ENA | UCB_ADC_START);
  242. /* Wait for the conversion to complete. */
  243. timeout = jiffies + HZ/2;
  244. while (!(ucb1400_reg_read(ucb->ac97, UCB_ADC_DATA) &
  245. UCB_ADC_DAT_VALID)) {
  246. cpu_relax();
  247. if (time_after(jiffies, timeout)) {
  248. dev_err(&pdev->dev, "timed out in IRQ probe\n");
  249. probe_irq_off(mask);
  250. return -ENODEV;
  251. }
  252. }
  253. ucb1400_reg_write(ucb->ac97, UCB_ADC_CR, 0);
  254. /* Disable and clear interrupt. */
  255. ucb1400_reg_write(ucb->ac97, UCB_IE_RIS, 0);
  256. ucb1400_reg_write(ucb->ac97, UCB_IE_FAL, 0);
  257. ucb1400_reg_write(ucb->ac97, UCB_IE_CLEAR, 0xffff);
  258. ucb1400_reg_write(ucb->ac97, UCB_IE_CLEAR, 0);
  259. /* Read triggered interrupt. */
  260. ucb->irq = probe_irq_off(mask);
  261. if (ucb->irq < 0 || ucb->irq == NO_IRQ)
  262. return -ENODEV;
  263. return 0;
  264. }
  265. static int ucb1400_ts_probe(struct platform_device *pdev)
  266. {
  267. struct ucb1400_ts *ucb = dev_get_platdata(&pdev->dev);
  268. int error, x_res, y_res;
  269. u16 fcsr;
  270. ucb->ts_idev = input_allocate_device();
  271. if (!ucb->ts_idev) {
  272. error = -ENOMEM;
  273. goto err;
  274. }
  275. /* Only in case the IRQ line wasn't supplied, try detecting it */
  276. if (ucb->irq < 0) {
  277. error = ucb1400_ts_detect_irq(ucb, pdev);
  278. if (error) {
  279. dev_err(&pdev->dev, "IRQ probe failed\n");
  280. goto err_free_devs;
  281. }
  282. }
  283. dev_dbg(&pdev->dev, "found IRQ %d\n", ucb->irq);
  284. init_waitqueue_head(&ucb->ts_wait);
  285. input_set_drvdata(ucb->ts_idev, ucb);
  286. ucb->ts_idev->dev.parent = &pdev->dev;
  287. ucb->ts_idev->name = "UCB1400 touchscreen interface";
  288. ucb->ts_idev->id.vendor = ucb1400_reg_read(ucb->ac97,
  289. AC97_VENDOR_ID1);
  290. ucb->ts_idev->id.product = ucb->id;
  291. ucb->ts_idev->open = ucb1400_ts_open;
  292. ucb->ts_idev->close = ucb1400_ts_close;
  293. ucb->ts_idev->evbit[0] = BIT_MASK(EV_ABS) | BIT_MASK(EV_KEY);
  294. ucb->ts_idev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  295. /*
  296. * Enable ADC filter to prevent horrible jitter on Colibri.
  297. * This also further reduces jitter on boards where ADCSYNC
  298. * pin is connected.
  299. */
  300. fcsr = ucb1400_reg_read(ucb->ac97, UCB_FCSR);
  301. ucb1400_reg_write(ucb->ac97, UCB_FCSR, fcsr | UCB_FCSR_AVE);
  302. ucb1400_adc_enable(ucb->ac97);
  303. x_res = ucb1400_ts_read_xres(ucb);
  304. y_res = ucb1400_ts_read_yres(ucb);
  305. ucb1400_adc_disable(ucb->ac97);
  306. dev_dbg(&pdev->dev, "x/y = %d/%d\n", x_res, y_res);
  307. input_set_abs_params(ucb->ts_idev, ABS_X, 0, x_res, 0, 0);
  308. input_set_abs_params(ucb->ts_idev, ABS_Y, 0, y_res, 0, 0);
  309. input_set_abs_params(ucb->ts_idev, ABS_PRESSURE, 0, 0, 0, 0);
  310. ucb1400_ts_stop(ucb);
  311. error = request_threaded_irq(ucb->irq, NULL, ucb1400_irq,
  312. IRQF_TRIGGER_RISING | IRQF_ONESHOT,
  313. "UCB1400", ucb);
  314. if (error) {
  315. dev_err(&pdev->dev,
  316. "unable to grab irq%d: %d\n", ucb->irq, error);
  317. goto err_free_devs;
  318. }
  319. error = input_register_device(ucb->ts_idev);
  320. if (error)
  321. goto err_free_irq;
  322. return 0;
  323. err_free_irq:
  324. free_irq(ucb->irq, ucb);
  325. err_free_devs:
  326. input_free_device(ucb->ts_idev);
  327. err:
  328. return error;
  329. }
  330. static int ucb1400_ts_remove(struct platform_device *pdev)
  331. {
  332. struct ucb1400_ts *ucb = dev_get_platdata(&pdev->dev);
  333. free_irq(ucb->irq, ucb);
  334. input_unregister_device(ucb->ts_idev);
  335. return 0;
  336. }
  337. static int __maybe_unused ucb1400_ts_suspend(struct device *dev)
  338. {
  339. struct ucb1400_ts *ucb = dev_get_platdata(dev);
  340. struct input_dev *idev = ucb->ts_idev;
  341. mutex_lock(&idev->mutex);
  342. if (idev->users)
  343. ucb1400_ts_start(ucb);
  344. mutex_unlock(&idev->mutex);
  345. return 0;
  346. }
  347. static int __maybe_unused ucb1400_ts_resume(struct device *dev)
  348. {
  349. struct ucb1400_ts *ucb = dev_get_platdata(dev);
  350. struct input_dev *idev = ucb->ts_idev;
  351. mutex_lock(&idev->mutex);
  352. if (idev->users)
  353. ucb1400_ts_stop(ucb);
  354. mutex_unlock(&idev->mutex);
  355. return 0;
  356. }
  357. static SIMPLE_DEV_PM_OPS(ucb1400_ts_pm_ops,
  358. ucb1400_ts_suspend, ucb1400_ts_resume);
  359. static struct platform_driver ucb1400_ts_driver = {
  360. .probe = ucb1400_ts_probe,
  361. .remove = ucb1400_ts_remove,
  362. .driver = {
  363. .name = "ucb1400_ts",
  364. .pm = &ucb1400_ts_pm_ops,
  365. },
  366. };
  367. module_platform_driver(ucb1400_ts_driver);
  368. module_param(adcsync, bool, 0444);
  369. MODULE_PARM_DESC(adcsync, "Synchronize touch readings with ADCSYNC pin.");
  370. module_param(ts_delay, int, 0444);
  371. MODULE_PARM_DESC(ts_delay, "Delay between panel setup and"
  372. " position read. Default = 55us.");
  373. module_param(ts_delay_pressure, int, 0444);
  374. MODULE_PARM_DESC(ts_delay_pressure,
  375. "delay between panel setup and pressure read."
  376. " Default = 0us.");
  377. MODULE_DESCRIPTION("Philips UCB1400 touchscreen driver");
  378. MODULE_LICENSE("GPL");