lpc32xx_ts.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * LPC32xx built-in touchscreen driver
  3. *
  4. * Copyright (C) 2010 NXP Semiconductors
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/platform_device.h>
  17. #include <linux/input.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/module.h>
  20. #include <linux/clk.h>
  21. #include <linux/io.h>
  22. #include <linux/slab.h>
  23. #include <linux/of.h>
  24. /*
  25. * Touchscreen controller register offsets
  26. */
  27. #define LPC32XX_TSC_STAT 0x00
  28. #define LPC32XX_TSC_SEL 0x04
  29. #define LPC32XX_TSC_CON 0x08
  30. #define LPC32XX_TSC_FIFO 0x0C
  31. #define LPC32XX_TSC_DTR 0x10
  32. #define LPC32XX_TSC_RTR 0x14
  33. #define LPC32XX_TSC_UTR 0x18
  34. #define LPC32XX_TSC_TTR 0x1C
  35. #define LPC32XX_TSC_DXP 0x20
  36. #define LPC32XX_TSC_MIN_X 0x24
  37. #define LPC32XX_TSC_MAX_X 0x28
  38. #define LPC32XX_TSC_MIN_Y 0x2C
  39. #define LPC32XX_TSC_MAX_Y 0x30
  40. #define LPC32XX_TSC_AUX_UTR 0x34
  41. #define LPC32XX_TSC_AUX_MIN 0x38
  42. #define LPC32XX_TSC_AUX_MAX 0x3C
  43. #define LPC32XX_TSC_STAT_FIFO_OVRRN (1 << 8)
  44. #define LPC32XX_TSC_STAT_FIFO_EMPTY (1 << 7)
  45. #define LPC32XX_TSC_SEL_DEFVAL 0x0284
  46. #define LPC32XX_TSC_ADCCON_IRQ_TO_FIFO_4 (0x1 << 11)
  47. #define LPC32XX_TSC_ADCCON_X_SAMPLE_SIZE(s) ((10 - (s)) << 7)
  48. #define LPC32XX_TSC_ADCCON_Y_SAMPLE_SIZE(s) ((10 - (s)) << 4)
  49. #define LPC32XX_TSC_ADCCON_POWER_UP (1 << 2)
  50. #define LPC32XX_TSC_ADCCON_AUTO_EN (1 << 0)
  51. #define LPC32XX_TSC_FIFO_TS_P_LEVEL (1 << 31)
  52. #define LPC32XX_TSC_FIFO_NORMALIZE_X_VAL(x) (((x) & 0x03FF0000) >> 16)
  53. #define LPC32XX_TSC_FIFO_NORMALIZE_Y_VAL(y) ((y) & 0x000003FF)
  54. #define LPC32XX_TSC_ADCDAT_VALUE_MASK 0x000003FF
  55. #define LPC32XX_TSC_MIN_XY_VAL 0x0
  56. #define LPC32XX_TSC_MAX_XY_VAL 0x3FF
  57. #define MOD_NAME "ts-lpc32xx"
  58. #define tsc_readl(dev, reg) \
  59. __raw_readl((dev)->tsc_base + (reg))
  60. #define tsc_writel(dev, reg, val) \
  61. __raw_writel((val), (dev)->tsc_base + (reg))
  62. struct lpc32xx_tsc {
  63. struct input_dev *dev;
  64. void __iomem *tsc_base;
  65. int irq;
  66. struct clk *clk;
  67. };
  68. static void lpc32xx_fifo_clear(struct lpc32xx_tsc *tsc)
  69. {
  70. while (!(tsc_readl(tsc, LPC32XX_TSC_STAT) &
  71. LPC32XX_TSC_STAT_FIFO_EMPTY))
  72. tsc_readl(tsc, LPC32XX_TSC_FIFO);
  73. }
  74. static irqreturn_t lpc32xx_ts_interrupt(int irq, void *dev_id)
  75. {
  76. u32 tmp, rv[4], xs[4], ys[4];
  77. int idx;
  78. struct lpc32xx_tsc *tsc = dev_id;
  79. struct input_dev *input = tsc->dev;
  80. tmp = tsc_readl(tsc, LPC32XX_TSC_STAT);
  81. if (tmp & LPC32XX_TSC_STAT_FIFO_OVRRN) {
  82. /* FIFO overflow - throw away samples */
  83. lpc32xx_fifo_clear(tsc);
  84. return IRQ_HANDLED;
  85. }
  86. /*
  87. * Gather and normalize 4 samples. Pen-up events may have less
  88. * than 4 samples, but its ok to pop 4 and let the last sample
  89. * pen status check drop the samples.
  90. */
  91. idx = 0;
  92. while (idx < 4 &&
  93. !(tsc_readl(tsc, LPC32XX_TSC_STAT) &
  94. LPC32XX_TSC_STAT_FIFO_EMPTY)) {
  95. tmp = tsc_readl(tsc, LPC32XX_TSC_FIFO);
  96. xs[idx] = LPC32XX_TSC_ADCDAT_VALUE_MASK -
  97. LPC32XX_TSC_FIFO_NORMALIZE_X_VAL(tmp);
  98. ys[idx] = LPC32XX_TSC_ADCDAT_VALUE_MASK -
  99. LPC32XX_TSC_FIFO_NORMALIZE_Y_VAL(tmp);
  100. rv[idx] = tmp;
  101. idx++;
  102. }
  103. /* Data is only valid if pen is still down in last sample */
  104. if (!(rv[3] & LPC32XX_TSC_FIFO_TS_P_LEVEL) && idx == 4) {
  105. /* Use average of 2nd and 3rd sample for position */
  106. input_report_abs(input, ABS_X, (xs[1] + xs[2]) / 2);
  107. input_report_abs(input, ABS_Y, (ys[1] + ys[2]) / 2);
  108. input_report_key(input, BTN_TOUCH, 1);
  109. } else {
  110. input_report_key(input, BTN_TOUCH, 0);
  111. }
  112. input_sync(input);
  113. return IRQ_HANDLED;
  114. }
  115. static void lpc32xx_stop_tsc(struct lpc32xx_tsc *tsc)
  116. {
  117. /* Disable auto mode */
  118. tsc_writel(tsc, LPC32XX_TSC_CON,
  119. tsc_readl(tsc, LPC32XX_TSC_CON) &
  120. ~LPC32XX_TSC_ADCCON_AUTO_EN);
  121. clk_disable_unprepare(tsc->clk);
  122. }
  123. static int lpc32xx_setup_tsc(struct lpc32xx_tsc *tsc)
  124. {
  125. u32 tmp;
  126. int err;
  127. err = clk_prepare_enable(tsc->clk);
  128. if (err)
  129. return err;
  130. tmp = tsc_readl(tsc, LPC32XX_TSC_CON) & ~LPC32XX_TSC_ADCCON_POWER_UP;
  131. /* Set the TSC FIFO depth to 4 samples @ 10-bits per sample (max) */
  132. tmp = LPC32XX_TSC_ADCCON_IRQ_TO_FIFO_4 |
  133. LPC32XX_TSC_ADCCON_X_SAMPLE_SIZE(10) |
  134. LPC32XX_TSC_ADCCON_Y_SAMPLE_SIZE(10);
  135. tsc_writel(tsc, LPC32XX_TSC_CON, tmp);
  136. /* These values are all preset */
  137. tsc_writel(tsc, LPC32XX_TSC_SEL, LPC32XX_TSC_SEL_DEFVAL);
  138. tsc_writel(tsc, LPC32XX_TSC_MIN_X, LPC32XX_TSC_MIN_XY_VAL);
  139. tsc_writel(tsc, LPC32XX_TSC_MAX_X, LPC32XX_TSC_MAX_XY_VAL);
  140. tsc_writel(tsc, LPC32XX_TSC_MIN_Y, LPC32XX_TSC_MIN_XY_VAL);
  141. tsc_writel(tsc, LPC32XX_TSC_MAX_Y, LPC32XX_TSC_MAX_XY_VAL);
  142. /* Aux support is not used */
  143. tsc_writel(tsc, LPC32XX_TSC_AUX_UTR, 0);
  144. tsc_writel(tsc, LPC32XX_TSC_AUX_MIN, 0);
  145. tsc_writel(tsc, LPC32XX_TSC_AUX_MAX, 0);
  146. /*
  147. * Set sample rate to about 240Hz per X/Y pair. A single measurement
  148. * consists of 4 pairs which gives about a 60Hz sample rate based on
  149. * a stable 32768Hz clock source. Values are in clocks.
  150. * Rate is (32768 / (RTR + XCONV + RTR + YCONV + DXP + TTR + UTR) / 4
  151. */
  152. tsc_writel(tsc, LPC32XX_TSC_RTR, 0x2);
  153. tsc_writel(tsc, LPC32XX_TSC_DTR, 0x2);
  154. tsc_writel(tsc, LPC32XX_TSC_TTR, 0x10);
  155. tsc_writel(tsc, LPC32XX_TSC_DXP, 0x4);
  156. tsc_writel(tsc, LPC32XX_TSC_UTR, 88);
  157. lpc32xx_fifo_clear(tsc);
  158. /* Enable automatic ts event capture */
  159. tsc_writel(tsc, LPC32XX_TSC_CON, tmp | LPC32XX_TSC_ADCCON_AUTO_EN);
  160. return 0;
  161. }
  162. static int lpc32xx_ts_open(struct input_dev *dev)
  163. {
  164. struct lpc32xx_tsc *tsc = input_get_drvdata(dev);
  165. return lpc32xx_setup_tsc(tsc);
  166. }
  167. static void lpc32xx_ts_close(struct input_dev *dev)
  168. {
  169. struct lpc32xx_tsc *tsc = input_get_drvdata(dev);
  170. lpc32xx_stop_tsc(tsc);
  171. }
  172. static int lpc32xx_ts_probe(struct platform_device *pdev)
  173. {
  174. struct lpc32xx_tsc *tsc;
  175. struct input_dev *input;
  176. struct resource *res;
  177. resource_size_t size;
  178. int irq;
  179. int error;
  180. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  181. if (!res) {
  182. dev_err(&pdev->dev, "Can't get memory resource\n");
  183. return -ENOENT;
  184. }
  185. irq = platform_get_irq(pdev, 0);
  186. if (irq < 0) {
  187. dev_err(&pdev->dev, "Can't get interrupt resource\n");
  188. return irq;
  189. }
  190. tsc = kzalloc(sizeof(*tsc), GFP_KERNEL);
  191. input = input_allocate_device();
  192. if (!tsc || !input) {
  193. dev_err(&pdev->dev, "failed allocating memory\n");
  194. error = -ENOMEM;
  195. goto err_free_mem;
  196. }
  197. tsc->dev = input;
  198. tsc->irq = irq;
  199. size = resource_size(res);
  200. if (!request_mem_region(res->start, size, pdev->name)) {
  201. dev_err(&pdev->dev, "TSC registers are not free\n");
  202. error = -EBUSY;
  203. goto err_free_mem;
  204. }
  205. tsc->tsc_base = ioremap(res->start, size);
  206. if (!tsc->tsc_base) {
  207. dev_err(&pdev->dev, "Can't map memory\n");
  208. error = -ENOMEM;
  209. goto err_release_mem;
  210. }
  211. tsc->clk = clk_get(&pdev->dev, NULL);
  212. if (IS_ERR(tsc->clk)) {
  213. dev_err(&pdev->dev, "failed getting clock\n");
  214. error = PTR_ERR(tsc->clk);
  215. goto err_unmap;
  216. }
  217. input->name = MOD_NAME;
  218. input->phys = "lpc32xx/input0";
  219. input->id.bustype = BUS_HOST;
  220. input->id.vendor = 0x0001;
  221. input->id.product = 0x0002;
  222. input->id.version = 0x0100;
  223. input->dev.parent = &pdev->dev;
  224. input->open = lpc32xx_ts_open;
  225. input->close = lpc32xx_ts_close;
  226. input->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  227. input->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  228. input_set_abs_params(input, ABS_X, LPC32XX_TSC_MIN_XY_VAL,
  229. LPC32XX_TSC_MAX_XY_VAL, 0, 0);
  230. input_set_abs_params(input, ABS_Y, LPC32XX_TSC_MIN_XY_VAL,
  231. LPC32XX_TSC_MAX_XY_VAL, 0, 0);
  232. input_set_drvdata(input, tsc);
  233. error = request_irq(tsc->irq, lpc32xx_ts_interrupt,
  234. 0, pdev->name, tsc);
  235. if (error) {
  236. dev_err(&pdev->dev, "failed requesting interrupt\n");
  237. goto err_put_clock;
  238. }
  239. error = input_register_device(input);
  240. if (error) {
  241. dev_err(&pdev->dev, "failed registering input device\n");
  242. goto err_free_irq;
  243. }
  244. platform_set_drvdata(pdev, tsc);
  245. device_init_wakeup(&pdev->dev, 1);
  246. return 0;
  247. err_free_irq:
  248. free_irq(tsc->irq, tsc);
  249. err_put_clock:
  250. clk_put(tsc->clk);
  251. err_unmap:
  252. iounmap(tsc->tsc_base);
  253. err_release_mem:
  254. release_mem_region(res->start, size);
  255. err_free_mem:
  256. input_free_device(input);
  257. kfree(tsc);
  258. return error;
  259. }
  260. static int lpc32xx_ts_remove(struct platform_device *pdev)
  261. {
  262. struct lpc32xx_tsc *tsc = platform_get_drvdata(pdev);
  263. struct resource *res;
  264. free_irq(tsc->irq, tsc);
  265. input_unregister_device(tsc->dev);
  266. clk_put(tsc->clk);
  267. iounmap(tsc->tsc_base);
  268. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  269. release_mem_region(res->start, resource_size(res));
  270. kfree(tsc);
  271. return 0;
  272. }
  273. #ifdef CONFIG_PM
  274. static int lpc32xx_ts_suspend(struct device *dev)
  275. {
  276. struct lpc32xx_tsc *tsc = dev_get_drvdata(dev);
  277. struct input_dev *input = tsc->dev;
  278. /*
  279. * Suspend and resume can be called when the device hasn't been
  280. * enabled. If there are no users that have the device open, then
  281. * avoid calling the TSC stop and start functions as the TSC
  282. * isn't yet clocked.
  283. */
  284. mutex_lock(&input->mutex);
  285. if (input->users) {
  286. if (device_may_wakeup(dev))
  287. enable_irq_wake(tsc->irq);
  288. else
  289. lpc32xx_stop_tsc(tsc);
  290. }
  291. mutex_unlock(&input->mutex);
  292. return 0;
  293. }
  294. static int lpc32xx_ts_resume(struct device *dev)
  295. {
  296. struct lpc32xx_tsc *tsc = dev_get_drvdata(dev);
  297. struct input_dev *input = tsc->dev;
  298. mutex_lock(&input->mutex);
  299. if (input->users) {
  300. if (device_may_wakeup(dev))
  301. disable_irq_wake(tsc->irq);
  302. else
  303. lpc32xx_setup_tsc(tsc);
  304. }
  305. mutex_unlock(&input->mutex);
  306. return 0;
  307. }
  308. static const struct dev_pm_ops lpc32xx_ts_pm_ops = {
  309. .suspend = lpc32xx_ts_suspend,
  310. .resume = lpc32xx_ts_resume,
  311. };
  312. #define LPC32XX_TS_PM_OPS (&lpc32xx_ts_pm_ops)
  313. #else
  314. #define LPC32XX_TS_PM_OPS NULL
  315. #endif
  316. #ifdef CONFIG_OF
  317. static const struct of_device_id lpc32xx_tsc_of_match[] = {
  318. { .compatible = "nxp,lpc3220-tsc", },
  319. { },
  320. };
  321. MODULE_DEVICE_TABLE(of, lpc32xx_tsc_of_match);
  322. #endif
  323. static struct platform_driver lpc32xx_ts_driver = {
  324. .probe = lpc32xx_ts_probe,
  325. .remove = lpc32xx_ts_remove,
  326. .driver = {
  327. .name = MOD_NAME,
  328. .pm = LPC32XX_TS_PM_OPS,
  329. .of_match_table = of_match_ptr(lpc32xx_tsc_of_match),
  330. },
  331. };
  332. module_platform_driver(lpc32xx_ts_driver);
  333. MODULE_AUTHOR("Kevin Wells <kevin.wells@nxp.com");
  334. MODULE_DESCRIPTION("LPC32XX TSC Driver");
  335. MODULE_LICENSE("GPL");
  336. MODULE_ALIAS("platform:lpc32xx_ts");