ti_am335x_tsc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * TI Touch Screen driver
  3. *
  4. * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation version 2.
  9. *
  10. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  11. * kind, whether express or implied; without even the implied warranty
  12. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/err.h>
  18. #include <linux/module.h>
  19. #include <linux/input.h>
  20. #include <linux/slab.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/clk.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/io.h>
  25. #include <linux/input/ti_am335x_tsc.h>
  26. #include <linux/delay.h>
  27. #include <linux/mfd/ti_am335x_tscadc.h>
  28. #define ADCFSM_STEPID 0x10
  29. #define SEQ_SETTLE 275
  30. #define MAX_12BIT ((1 << 12) - 1)
  31. struct titsc {
  32. struct input_dev *input;
  33. struct ti_tscadc_dev *mfd_tscadc;
  34. unsigned int irq;
  35. unsigned int wires;
  36. unsigned int x_plate_resistance;
  37. bool pen_down;
  38. int steps_to_configure;
  39. };
  40. static unsigned int titsc_readl(struct titsc *ts, unsigned int reg)
  41. {
  42. return readl(ts->mfd_tscadc->tscadc_base + reg);
  43. }
  44. static void titsc_writel(struct titsc *tsc, unsigned int reg,
  45. unsigned int val)
  46. {
  47. writel(val, tsc->mfd_tscadc->tscadc_base + reg);
  48. }
  49. static void titsc_step_config(struct titsc *ts_dev)
  50. {
  51. unsigned int config;
  52. int i, total_steps;
  53. /* Configure the Step registers */
  54. total_steps = 2 * ts_dev->steps_to_configure;
  55. config = STEPCONFIG_MODE_HWSYNC |
  56. STEPCONFIG_AVG_16 | STEPCONFIG_XPP;
  57. switch (ts_dev->wires) {
  58. case 4:
  59. config |= STEPCONFIG_INP_AN2 | STEPCONFIG_XNN;
  60. break;
  61. case 5:
  62. config |= STEPCONFIG_YNN |
  63. STEPCONFIG_INP_AN4 | STEPCONFIG_XNN |
  64. STEPCONFIG_YPP;
  65. break;
  66. case 8:
  67. config |= STEPCONFIG_INP_AN2 | STEPCONFIG_XNN;
  68. break;
  69. }
  70. for (i = 1; i <= ts_dev->steps_to_configure; i++) {
  71. titsc_writel(ts_dev, REG_STEPCONFIG(i), config);
  72. titsc_writel(ts_dev, REG_STEPDELAY(i), STEPCONFIG_OPENDLY);
  73. }
  74. config = 0;
  75. config = STEPCONFIG_MODE_HWSYNC |
  76. STEPCONFIG_AVG_16 | STEPCONFIG_YNN |
  77. STEPCONFIG_INM_ADCREFM | STEPCONFIG_FIFO1;
  78. switch (ts_dev->wires) {
  79. case 4:
  80. config |= STEPCONFIG_YPP;
  81. break;
  82. case 5:
  83. config |= STEPCONFIG_XPP | STEPCONFIG_INP_AN4 |
  84. STEPCONFIG_XNP | STEPCONFIG_YPN;
  85. break;
  86. case 8:
  87. config |= STEPCONFIG_YPP;
  88. break;
  89. }
  90. for (i = (ts_dev->steps_to_configure + 1); i <= total_steps; i++) {
  91. titsc_writel(ts_dev, REG_STEPCONFIG(i), config);
  92. titsc_writel(ts_dev, REG_STEPDELAY(i), STEPCONFIG_OPENDLY);
  93. }
  94. config = 0;
  95. /* Charge step configuration */
  96. config = STEPCONFIG_XPP | STEPCONFIG_YNN |
  97. STEPCHARGE_RFP_XPUL | STEPCHARGE_RFM_XNUR |
  98. STEPCHARGE_INM_AN1 | STEPCHARGE_INP_AN1;
  99. titsc_writel(ts_dev, REG_CHARGECONFIG, config);
  100. titsc_writel(ts_dev, REG_CHARGEDELAY, CHARGEDLY_OPENDLY);
  101. config = 0;
  102. /* Configure to calculate pressure */
  103. config = STEPCONFIG_MODE_HWSYNC |
  104. STEPCONFIG_AVG_16 | STEPCONFIG_YPP |
  105. STEPCONFIG_XNN | STEPCONFIG_INM_ADCREFM;
  106. titsc_writel(ts_dev, REG_STEPCONFIG(total_steps + 1), config);
  107. titsc_writel(ts_dev, REG_STEPDELAY(total_steps + 1),
  108. STEPCONFIG_OPENDLY);
  109. config |= STEPCONFIG_INP_AN3 | STEPCONFIG_FIFO1;
  110. titsc_writel(ts_dev, REG_STEPCONFIG(total_steps + 2), config);
  111. titsc_writel(ts_dev, REG_STEPDELAY(total_steps + 2),
  112. STEPCONFIG_OPENDLY);
  113. titsc_writel(ts_dev, REG_SE, STPENB_STEPENB_TC);
  114. }
  115. static void titsc_read_coordinates(struct titsc *ts_dev,
  116. unsigned int *x, unsigned int *y)
  117. {
  118. unsigned int fifocount = titsc_readl(ts_dev, REG_FIFO0CNT);
  119. unsigned int prev_val_x = ~0, prev_val_y = ~0;
  120. unsigned int prev_diff_x = ~0, prev_diff_y = ~0;
  121. unsigned int read, diff;
  122. unsigned int i, channel;
  123. /*
  124. * Delta filter is used to remove large variations in sampled
  125. * values from ADC. The filter tries to predict where the next
  126. * coordinate could be. This is done by taking a previous
  127. * coordinate and subtracting it form current one. Further the
  128. * algorithm compares the difference with that of a present value,
  129. * if true the value is reported to the sub system.
  130. */
  131. for (i = 0; i < fifocount - 1; i++) {
  132. read = titsc_readl(ts_dev, REG_FIFO0);
  133. channel = read & 0xf0000;
  134. channel = channel >> 0x10;
  135. if ((channel >= 0) && (channel < ts_dev->steps_to_configure)) {
  136. read &= 0xfff;
  137. diff = abs(read - prev_val_x);
  138. if (diff < prev_diff_x) {
  139. prev_diff_x = diff;
  140. *x = read;
  141. }
  142. prev_val_x = read;
  143. }
  144. read = titsc_readl(ts_dev, REG_FIFO1);
  145. channel = read & 0xf0000;
  146. channel = channel >> 0x10;
  147. if ((channel >= ts_dev->steps_to_configure) &&
  148. (channel < (2 * ts_dev->steps_to_configure - 1))) {
  149. read &= 0xfff;
  150. diff = abs(read - prev_val_y);
  151. if (diff < prev_diff_y) {
  152. prev_diff_y = diff;
  153. *y = read;
  154. }
  155. prev_val_y = read;
  156. }
  157. }
  158. }
  159. static irqreturn_t titsc_irq(int irq, void *dev)
  160. {
  161. struct titsc *ts_dev = dev;
  162. struct input_dev *input_dev = ts_dev->input;
  163. unsigned int status, irqclr = 0;
  164. unsigned int x = 0, y = 0;
  165. unsigned int z1, z2, z;
  166. unsigned int fsm;
  167. unsigned int fifo1count, fifo0count;
  168. int i;
  169. status = titsc_readl(ts_dev, REG_IRQSTATUS);
  170. if (status & IRQENB_FIFO0THRES) {
  171. titsc_read_coordinates(ts_dev, &x, &y);
  172. z1 = titsc_readl(ts_dev, REG_FIFO0) & 0xfff;
  173. z2 = titsc_readl(ts_dev, REG_FIFO1) & 0xfff;
  174. fifo1count = titsc_readl(ts_dev, REG_FIFO1CNT);
  175. for (i = 0; i < fifo1count; i++)
  176. titsc_readl(ts_dev, REG_FIFO1);
  177. fifo0count = titsc_readl(ts_dev, REG_FIFO0CNT);
  178. for (i = 0; i < fifo0count; i++)
  179. titsc_readl(ts_dev, REG_FIFO0);
  180. if (ts_dev->pen_down && z1 != 0 && z2 != 0) {
  181. /*
  182. * Calculate pressure using formula
  183. * Resistance(touch) = x plate resistance *
  184. * x postion/4096 * ((z2 / z1) - 1)
  185. */
  186. z = z2 - z1;
  187. z *= x;
  188. z *= ts_dev->x_plate_resistance;
  189. z /= z1;
  190. z = (z + 2047) >> 12;
  191. if (z <= MAX_12BIT) {
  192. input_report_abs(input_dev, ABS_X, x);
  193. input_report_abs(input_dev, ABS_Y, y);
  194. input_report_abs(input_dev, ABS_PRESSURE, z);
  195. input_report_key(input_dev, BTN_TOUCH, 1);
  196. input_sync(input_dev);
  197. }
  198. }
  199. irqclr |= IRQENB_FIFO0THRES;
  200. }
  201. /*
  202. * Time for sequencer to settle, to read
  203. * correct state of the sequencer.
  204. */
  205. udelay(SEQ_SETTLE);
  206. status = titsc_readl(ts_dev, REG_RAWIRQSTATUS);
  207. if (status & IRQENB_PENUP) {
  208. /* Pen up event */
  209. fsm = titsc_readl(ts_dev, REG_ADCFSM);
  210. if (fsm == ADCFSM_STEPID) {
  211. ts_dev->pen_down = false;
  212. input_report_key(input_dev, BTN_TOUCH, 0);
  213. input_report_abs(input_dev, ABS_PRESSURE, 0);
  214. input_sync(input_dev);
  215. } else {
  216. ts_dev->pen_down = true;
  217. }
  218. irqclr |= IRQENB_PENUP;
  219. }
  220. titsc_writel(ts_dev, REG_IRQSTATUS, irqclr);
  221. titsc_writel(ts_dev, REG_SE, STPENB_STEPENB_TC);
  222. return IRQ_HANDLED;
  223. }
  224. /*
  225. * The functions for inserting/removing driver as a module.
  226. */
  227. static int titsc_probe(struct platform_device *pdev)
  228. {
  229. struct titsc *ts_dev;
  230. struct input_dev *input_dev;
  231. struct ti_tscadc_dev *tscadc_dev = pdev->dev.platform_data;
  232. struct mfd_tscadc_board *pdata;
  233. int err;
  234. pdata = tscadc_dev->dev->platform_data;
  235. if (!pdata) {
  236. dev_err(&pdev->dev, "Could not find platform data\n");
  237. return -EINVAL;
  238. }
  239. /* Allocate memory for device */
  240. ts_dev = kzalloc(sizeof(struct titsc), GFP_KERNEL);
  241. input_dev = input_allocate_device();
  242. if (!ts_dev || !input_dev) {
  243. dev_err(&pdev->dev, "failed to allocate memory.\n");
  244. err = -ENOMEM;
  245. goto err_free_mem;
  246. }
  247. tscadc_dev->tsc = ts_dev;
  248. ts_dev->mfd_tscadc = tscadc_dev;
  249. ts_dev->input = input_dev;
  250. ts_dev->irq = tscadc_dev->irq;
  251. ts_dev->wires = pdata->tsc_init->wires;
  252. ts_dev->x_plate_resistance = pdata->tsc_init->x_plate_resistance;
  253. ts_dev->steps_to_configure = pdata->tsc_init->steps_to_configure;
  254. err = request_irq(ts_dev->irq, titsc_irq,
  255. 0, pdev->dev.driver->name, ts_dev);
  256. if (err) {
  257. dev_err(&pdev->dev, "failed to allocate irq.\n");
  258. goto err_free_mem;
  259. }
  260. titsc_writel(ts_dev, REG_IRQENABLE, IRQENB_FIFO0THRES);
  261. titsc_step_config(ts_dev);
  262. titsc_writel(ts_dev, REG_FIFO0THR, ts_dev->steps_to_configure);
  263. input_dev->name = "ti-tsc";
  264. input_dev->dev.parent = &pdev->dev;
  265. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  266. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  267. input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, 0, 0);
  268. input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
  269. input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT, 0, 0);
  270. /* register to the input system */
  271. err = input_register_device(input_dev);
  272. if (err)
  273. goto err_free_irq;
  274. platform_set_drvdata(pdev, ts_dev);
  275. return 0;
  276. err_free_irq:
  277. free_irq(ts_dev->irq, ts_dev);
  278. err_free_mem:
  279. input_free_device(input_dev);
  280. kfree(ts_dev);
  281. return err;
  282. }
  283. static int titsc_remove(struct platform_device *pdev)
  284. {
  285. struct ti_tscadc_dev *tscadc_dev = pdev->dev.platform_data;
  286. struct titsc *ts_dev = tscadc_dev->tsc;
  287. free_irq(ts_dev->irq, ts_dev);
  288. input_unregister_device(ts_dev->input);
  289. platform_set_drvdata(pdev, NULL);
  290. kfree(ts_dev);
  291. return 0;
  292. }
  293. #ifdef CONFIG_PM
  294. static int titsc_suspend(struct device *dev)
  295. {
  296. struct ti_tscadc_dev *tscadc_dev = dev->platform_data;
  297. struct titsc *ts_dev = tscadc_dev->tsc;
  298. unsigned int idle;
  299. if (device_may_wakeup(tscadc_dev->dev)) {
  300. idle = titsc_readl(ts_dev, REG_IRQENABLE);
  301. titsc_writel(ts_dev, REG_IRQENABLE,
  302. (idle | IRQENB_HW_PEN));
  303. titsc_writel(ts_dev, REG_IRQWAKEUP, IRQWKUP_ENB);
  304. }
  305. return 0;
  306. }
  307. static int titsc_resume(struct device *dev)
  308. {
  309. struct ti_tscadc_dev *tscadc_dev = dev->platform_data;
  310. struct titsc *ts_dev = tscadc_dev->tsc;
  311. if (device_may_wakeup(tscadc_dev->dev)) {
  312. titsc_writel(ts_dev, REG_IRQWAKEUP,
  313. 0x00);
  314. titsc_writel(ts_dev, REG_IRQCLR, IRQENB_HW_PEN);
  315. }
  316. titsc_step_config(ts_dev);
  317. titsc_writel(ts_dev, REG_FIFO0THR,
  318. ts_dev->steps_to_configure);
  319. return 0;
  320. }
  321. static const struct dev_pm_ops titsc_pm_ops = {
  322. .suspend = titsc_suspend,
  323. .resume = titsc_resume,
  324. };
  325. #define TITSC_PM_OPS (&titsc_pm_ops)
  326. #else
  327. #define TITSC_PM_OPS NULL
  328. #endif
  329. static struct platform_driver ti_tsc_driver = {
  330. .probe = titsc_probe,
  331. .remove = titsc_remove,
  332. .driver = {
  333. .name = "tsc",
  334. .owner = THIS_MODULE,
  335. .pm = TITSC_PM_OPS,
  336. },
  337. };
  338. module_platform_driver(ti_tsc_driver);
  339. MODULE_DESCRIPTION("TI touchscreen controller driver");
  340. MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
  341. MODULE_LICENSE("GPL");