PageRenderTime 1550ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/input/touchscreen/ucb1400_ts.c

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