lpc32xx_ts.c 8.7 KB

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