ti_am335x_tsc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. unsigned int stepenable = 0;
  53. int i, total_steps;
  54. /* Configure the Step registers */
  55. total_steps = 2 * ts_dev->steps_to_configure;
  56. config = STEPCONFIG_MODE_HWSYNC |
  57. STEPCONFIG_AVG_16 | STEPCONFIG_XPP;
  58. switch (ts_dev->wires) {
  59. case 4:
  60. config |= STEPCONFIG_INP_AN2 | STEPCONFIG_XNN;
  61. break;
  62. case 5:
  63. config |= STEPCONFIG_YNN |
  64. STEPCONFIG_INP_AN4 | STEPCONFIG_XNN |
  65. STEPCONFIG_YPP;
  66. break;
  67. case 8:
  68. config |= STEPCONFIG_INP_AN2 | STEPCONFIG_XNN;
  69. break;
  70. }
  71. for (i = 1; i <= ts_dev->steps_to_configure; i++) {
  72. titsc_writel(ts_dev, REG_STEPCONFIG(i), config);
  73. titsc_writel(ts_dev, REG_STEPDELAY(i), STEPCONFIG_OPENDLY);
  74. }
  75. config = 0;
  76. config = STEPCONFIG_MODE_HWSYNC |
  77. STEPCONFIG_AVG_16 | STEPCONFIG_YNN |
  78. STEPCONFIG_INM_ADCREFM | STEPCONFIG_FIFO1;
  79. switch (ts_dev->wires) {
  80. case 4:
  81. config |= STEPCONFIG_YPP;
  82. break;
  83. case 5:
  84. config |= STEPCONFIG_XPP | STEPCONFIG_INP_AN4 |
  85. STEPCONFIG_XNP | STEPCONFIG_YPN;
  86. break;
  87. case 8:
  88. config |= STEPCONFIG_YPP;
  89. break;
  90. }
  91. for (i = (ts_dev->steps_to_configure + 1); i <= total_steps; i++) {
  92. titsc_writel(ts_dev, REG_STEPCONFIG(i), config);
  93. titsc_writel(ts_dev, REG_STEPDELAY(i), STEPCONFIG_OPENDLY);
  94. }
  95. config = 0;
  96. /* Charge step configuration */
  97. config = STEPCONFIG_XPP | STEPCONFIG_YNN |
  98. STEPCHARGE_RFP_XPUL | STEPCHARGE_RFM_XNUR |
  99. STEPCHARGE_INM_AN1 | STEPCHARGE_INP_AN1;
  100. titsc_writel(ts_dev, REG_CHARGECONFIG, config);
  101. titsc_writel(ts_dev, REG_CHARGEDELAY, CHARGEDLY_OPENDLY);
  102. config = 0;
  103. /* Configure to calculate pressure */
  104. config = STEPCONFIG_MODE_HWSYNC |
  105. STEPCONFIG_AVG_16 | STEPCONFIG_YPP |
  106. STEPCONFIG_XNN | STEPCONFIG_INM_ADCREFM;
  107. titsc_writel(ts_dev, REG_STEPCONFIG(total_steps + 1), config);
  108. titsc_writel(ts_dev, REG_STEPDELAY(total_steps + 1),
  109. STEPCONFIG_OPENDLY);
  110. config |= STEPCONFIG_INP_AN3 | STEPCONFIG_FIFO1;
  111. titsc_writel(ts_dev, REG_STEPCONFIG(total_steps + 2), config);
  112. titsc_writel(ts_dev, REG_STEPDELAY(total_steps + 2),
  113. STEPCONFIG_OPENDLY);
  114. /* The steps1 … end and bit 0 for TS_Charge */
  115. stepenable = (1 << (total_steps + 2)) - 1;
  116. am335x_tsc_se_set(ts_dev->mfd_tscadc, stepenable);
  117. }
  118. static void titsc_read_coordinates(struct titsc *ts_dev,
  119. unsigned int *x, unsigned int *y)
  120. {
  121. unsigned int fifocount = titsc_readl(ts_dev, REG_FIFO0CNT);
  122. unsigned int prev_val_x = ~0, prev_val_y = ~0;
  123. unsigned int prev_diff_x = ~0, prev_diff_y = ~0;
  124. unsigned int read, diff;
  125. unsigned int i, channel;
  126. /*
  127. * Delta filter is used to remove large variations in sampled
  128. * values from ADC. The filter tries to predict where the next
  129. * coordinate could be. This is done by taking a previous
  130. * coordinate and subtracting it form current one. Further the
  131. * algorithm compares the difference with that of a present value,
  132. * if true the value is reported to the sub system.
  133. */
  134. for (i = 0; i < fifocount - 1; i++) {
  135. read = titsc_readl(ts_dev, REG_FIFO0);
  136. channel = read & 0xf0000;
  137. channel = channel >> 0x10;
  138. if ((channel >= 0) && (channel < ts_dev->steps_to_configure)) {
  139. read &= 0xfff;
  140. diff = abs(read - prev_val_x);
  141. if (diff < prev_diff_x) {
  142. prev_diff_x = diff;
  143. *x = read;
  144. }
  145. prev_val_x = read;
  146. }
  147. read = titsc_readl(ts_dev, REG_FIFO1);
  148. channel = read & 0xf0000;
  149. channel = channel >> 0x10;
  150. if ((channel >= ts_dev->steps_to_configure) &&
  151. (channel < (2 * ts_dev->steps_to_configure - 1))) {
  152. read &= 0xfff;
  153. diff = abs(read - prev_val_y);
  154. if (diff < prev_diff_y) {
  155. prev_diff_y = diff;
  156. *y = read;
  157. }
  158. prev_val_y = read;
  159. }
  160. }
  161. }
  162. static irqreturn_t titsc_irq(int irq, void *dev)
  163. {
  164. struct titsc *ts_dev = dev;
  165. struct input_dev *input_dev = ts_dev->input;
  166. unsigned int status, irqclr = 0;
  167. unsigned int x = 0, y = 0;
  168. unsigned int z1, z2, z;
  169. unsigned int fsm;
  170. unsigned int fifo1count, fifo0count;
  171. int i;
  172. status = titsc_readl(ts_dev, REG_IRQSTATUS);
  173. if (status & IRQENB_FIFO0THRES) {
  174. titsc_read_coordinates(ts_dev, &x, &y);
  175. z1 = titsc_readl(ts_dev, REG_FIFO0) & 0xfff;
  176. z2 = titsc_readl(ts_dev, REG_FIFO1) & 0xfff;
  177. fifo1count = titsc_readl(ts_dev, REG_FIFO1CNT);
  178. for (i = 0; i < fifo1count; i++)
  179. titsc_readl(ts_dev, REG_FIFO1);
  180. fifo0count = titsc_readl(ts_dev, REG_FIFO0CNT);
  181. for (i = 0; i < fifo0count; i++)
  182. titsc_readl(ts_dev, REG_FIFO0);
  183. if (ts_dev->pen_down && z1 != 0 && z2 != 0) {
  184. /*
  185. * Calculate pressure using formula
  186. * Resistance(touch) = x plate resistance *
  187. * x postion/4096 * ((z2 / z1) - 1)
  188. */
  189. z = z2 - z1;
  190. z *= x;
  191. z *= ts_dev->x_plate_resistance;
  192. z /= z1;
  193. z = (z + 2047) >> 12;
  194. if (z <= MAX_12BIT) {
  195. input_report_abs(input_dev, ABS_X, x);
  196. input_report_abs(input_dev, ABS_Y, y);
  197. input_report_abs(input_dev, ABS_PRESSURE, z);
  198. input_report_key(input_dev, BTN_TOUCH, 1);
  199. input_sync(input_dev);
  200. }
  201. }
  202. irqclr |= IRQENB_FIFO0THRES;
  203. }
  204. /*
  205. * Time for sequencer to settle, to read
  206. * correct state of the sequencer.
  207. */
  208. udelay(SEQ_SETTLE);
  209. status = titsc_readl(ts_dev, REG_RAWIRQSTATUS);
  210. if (status & IRQENB_PENUP) {
  211. /* Pen up event */
  212. fsm = titsc_readl(ts_dev, REG_ADCFSM);
  213. if (fsm == ADCFSM_STEPID) {
  214. ts_dev->pen_down = false;
  215. input_report_key(input_dev, BTN_TOUCH, 0);
  216. input_report_abs(input_dev, ABS_PRESSURE, 0);
  217. input_sync(input_dev);
  218. } else {
  219. ts_dev->pen_down = true;
  220. }
  221. irqclr |= IRQENB_PENUP;
  222. }
  223. titsc_writel(ts_dev, REG_IRQSTATUS, irqclr);
  224. am335x_tsc_se_update(ts_dev->mfd_tscadc);
  225. return IRQ_HANDLED;
  226. }
  227. /*
  228. * The functions for inserting/removing driver as a module.
  229. */
  230. static int titsc_probe(struct platform_device *pdev)
  231. {
  232. struct titsc *ts_dev;
  233. struct input_dev *input_dev;
  234. struct ti_tscadc_dev *tscadc_dev = ti_tscadc_dev_get(pdev);
  235. struct mfd_tscadc_board *pdata;
  236. int err;
  237. pdata = tscadc_dev->dev->platform_data;
  238. if (!pdata) {
  239. dev_err(&pdev->dev, "Could not find platform data\n");
  240. return -EINVAL;
  241. }
  242. /* Allocate memory for device */
  243. ts_dev = kzalloc(sizeof(struct titsc), GFP_KERNEL);
  244. input_dev = input_allocate_device();
  245. if (!ts_dev || !input_dev) {
  246. dev_err(&pdev->dev, "failed to allocate memory.\n");
  247. err = -ENOMEM;
  248. goto err_free_mem;
  249. }
  250. tscadc_dev->tsc = ts_dev;
  251. ts_dev->mfd_tscadc = tscadc_dev;
  252. ts_dev->input = input_dev;
  253. ts_dev->irq = tscadc_dev->irq;
  254. ts_dev->wires = pdata->tsc_init->wires;
  255. ts_dev->x_plate_resistance = pdata->tsc_init->x_plate_resistance;
  256. ts_dev->steps_to_configure = pdata->tsc_init->steps_to_configure;
  257. err = request_irq(ts_dev->irq, titsc_irq,
  258. 0, pdev->dev.driver->name, ts_dev);
  259. if (err) {
  260. dev_err(&pdev->dev, "failed to allocate irq.\n");
  261. goto err_free_mem;
  262. }
  263. titsc_writel(ts_dev, REG_IRQENABLE, IRQENB_FIFO0THRES);
  264. titsc_step_config(ts_dev);
  265. titsc_writel(ts_dev, REG_FIFO0THR, ts_dev->steps_to_configure);
  266. input_dev->name = "ti-tsc";
  267. input_dev->dev.parent = &pdev->dev;
  268. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  269. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  270. input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, 0, 0);
  271. input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
  272. input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT, 0, 0);
  273. /* register to the input system */
  274. err = input_register_device(input_dev);
  275. if (err)
  276. goto err_free_irq;
  277. platform_set_drvdata(pdev, ts_dev);
  278. return 0;
  279. err_free_irq:
  280. free_irq(ts_dev->irq, ts_dev);
  281. err_free_mem:
  282. input_free_device(input_dev);
  283. kfree(ts_dev);
  284. return err;
  285. }
  286. static int titsc_remove(struct platform_device *pdev)
  287. {
  288. struct titsc *ts_dev = platform_get_drvdata(pdev);
  289. u32 steps;
  290. free_irq(ts_dev->irq, ts_dev);
  291. /* total steps followed by the enable mask */
  292. steps = 2 * ts_dev->steps_to_configure + 2;
  293. steps = (1 << steps) - 1;
  294. am335x_tsc_se_clr(ts_dev->mfd_tscadc, steps);
  295. input_unregister_device(ts_dev->input);
  296. platform_set_drvdata(pdev, NULL);
  297. kfree(ts_dev);
  298. return 0;
  299. }
  300. #ifdef CONFIG_PM
  301. static int titsc_suspend(struct device *dev)
  302. {
  303. struct titsc *ts_dev = dev_get_drvdata(dev);
  304. struct ti_tscadc_dev *tscadc_dev;
  305. unsigned int idle;
  306. tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
  307. if (device_may_wakeup(tscadc_dev->dev)) {
  308. idle = titsc_readl(ts_dev, REG_IRQENABLE);
  309. titsc_writel(ts_dev, REG_IRQENABLE,
  310. (idle | IRQENB_HW_PEN));
  311. titsc_writel(ts_dev, REG_IRQWAKEUP, IRQWKUP_ENB);
  312. }
  313. return 0;
  314. }
  315. static int titsc_resume(struct device *dev)
  316. {
  317. struct titsc *ts_dev = dev_get_drvdata(dev);
  318. struct ti_tscadc_dev *tscadc_dev;
  319. tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
  320. if (device_may_wakeup(tscadc_dev->dev)) {
  321. titsc_writel(ts_dev, REG_IRQWAKEUP,
  322. 0x00);
  323. titsc_writel(ts_dev, REG_IRQCLR, IRQENB_HW_PEN);
  324. }
  325. titsc_step_config(ts_dev);
  326. titsc_writel(ts_dev, REG_FIFO0THR,
  327. ts_dev->steps_to_configure);
  328. return 0;
  329. }
  330. static const struct dev_pm_ops titsc_pm_ops = {
  331. .suspend = titsc_suspend,
  332. .resume = titsc_resume,
  333. };
  334. #define TITSC_PM_OPS (&titsc_pm_ops)
  335. #else
  336. #define TITSC_PM_OPS NULL
  337. #endif
  338. static struct platform_driver ti_tsc_driver = {
  339. .probe = titsc_probe,
  340. .remove = titsc_remove,
  341. .driver = {
  342. .name = "tsc",
  343. .owner = THIS_MODULE,
  344. .pm = TITSC_PM_OPS,
  345. },
  346. };
  347. module_platform_driver(ti_tsc_driver);
  348. MODULE_DESCRIPTION("TI touchscreen controller driver");
  349. MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
  350. MODULE_LICENSE("GPL");