ti_am335x_tsc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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/delay.h>
  26. #include <linux/of.h>
  27. #include <linux/of_device.h>
  28. #include <linux/mfd/ti_am335x_tscadc.h>
  29. #define ADCFSM_STEPID 0x10
  30. #define SEQ_SETTLE 275
  31. #define MAX_12BIT ((1 << 12) - 1)
  32. static const int config_pins[] = {
  33. STEPCONFIG_XPP,
  34. STEPCONFIG_XNN,
  35. STEPCONFIG_YPP,
  36. STEPCONFIG_YNN,
  37. };
  38. struct titsc {
  39. struct input_dev *input;
  40. struct ti_tscadc_dev *mfd_tscadc;
  41. unsigned int irq;
  42. unsigned int wires;
  43. unsigned int x_plate_resistance;
  44. bool pen_down;
  45. int coordinate_readouts;
  46. u32 config_inp[4];
  47. u32 bit_xp, bit_xn, bit_yp, bit_yn;
  48. u32 inp_xp, inp_xn, inp_yp, inp_yn;
  49. u32 step_mask;
  50. };
  51. static unsigned int titsc_readl(struct titsc *ts, unsigned int reg)
  52. {
  53. return readl(ts->mfd_tscadc->tscadc_base + reg);
  54. }
  55. static void titsc_writel(struct titsc *tsc, unsigned int reg,
  56. unsigned int val)
  57. {
  58. writel(val, tsc->mfd_tscadc->tscadc_base + reg);
  59. }
  60. static int titsc_config_wires(struct titsc *ts_dev)
  61. {
  62. u32 analog_line[4];
  63. u32 wire_order[4];
  64. int i, bit_cfg;
  65. for (i = 0; i < 4; i++) {
  66. /*
  67. * Get the order in which TSC wires are attached
  68. * w.r.t. each of the analog input lines on the EVM.
  69. */
  70. analog_line[i] = (ts_dev->config_inp[i] & 0xF0) >> 4;
  71. wire_order[i] = ts_dev->config_inp[i] & 0x0F;
  72. if (WARN_ON(analog_line[i] > 7))
  73. return -EINVAL;
  74. if (WARN_ON(wire_order[i] > ARRAY_SIZE(config_pins)))
  75. return -EINVAL;
  76. }
  77. for (i = 0; i < 4; i++) {
  78. int an_line;
  79. int wi_order;
  80. an_line = analog_line[i];
  81. wi_order = wire_order[i];
  82. bit_cfg = config_pins[wi_order];
  83. if (bit_cfg == 0)
  84. return -EINVAL;
  85. switch (wi_order) {
  86. case 0:
  87. ts_dev->bit_xp = bit_cfg;
  88. ts_dev->inp_xp = an_line;
  89. break;
  90. case 1:
  91. ts_dev->bit_xn = bit_cfg;
  92. ts_dev->inp_xn = an_line;
  93. break;
  94. case 2:
  95. ts_dev->bit_yp = bit_cfg;
  96. ts_dev->inp_yp = an_line;
  97. break;
  98. case 3:
  99. ts_dev->bit_yn = bit_cfg;
  100. ts_dev->inp_yn = an_line;
  101. break;
  102. }
  103. }
  104. return 0;
  105. }
  106. static void titsc_step_config(struct titsc *ts_dev)
  107. {
  108. unsigned int config;
  109. int i;
  110. int end_step;
  111. u32 stepenable;
  112. config = STEPCONFIG_MODE_HWSYNC |
  113. STEPCONFIG_AVG_16 | ts_dev->bit_xp;
  114. switch (ts_dev->wires) {
  115. case 4:
  116. config |= STEPCONFIG_INP(ts_dev->inp_yp) | ts_dev->bit_xn;
  117. break;
  118. case 5:
  119. config |= ts_dev->bit_yn |
  120. STEPCONFIG_INP_AN4 | ts_dev->bit_xn |
  121. ts_dev->bit_yp;
  122. break;
  123. case 8:
  124. config |= STEPCONFIG_INP(ts_dev->inp_yp) | ts_dev->bit_xn;
  125. break;
  126. }
  127. /* 1 … coordinate_readouts is for X */
  128. end_step = ts_dev->coordinate_readouts;
  129. for (i = 0; i < end_step; i++) {
  130. titsc_writel(ts_dev, REG_STEPCONFIG(i), config);
  131. titsc_writel(ts_dev, REG_STEPDELAY(i), STEPCONFIG_OPENDLY);
  132. }
  133. config = 0;
  134. config = STEPCONFIG_MODE_HWSYNC |
  135. STEPCONFIG_AVG_16 | ts_dev->bit_yn |
  136. STEPCONFIG_INM_ADCREFM;
  137. switch (ts_dev->wires) {
  138. case 4:
  139. config |= ts_dev->bit_yp | STEPCONFIG_INP(ts_dev->inp_xp);
  140. break;
  141. case 5:
  142. config |= ts_dev->bit_xp | STEPCONFIG_INP_AN4 |
  143. ts_dev->bit_xn | ts_dev->bit_yp;
  144. break;
  145. case 8:
  146. config |= ts_dev->bit_yp | STEPCONFIG_INP(ts_dev->inp_xp);
  147. break;
  148. }
  149. /* coordinate_readouts … coordinate_readouts * 2 is for Y */
  150. end_step = ts_dev->coordinate_readouts * 2;
  151. for (i = ts_dev->coordinate_readouts; i < end_step; i++) {
  152. titsc_writel(ts_dev, REG_STEPCONFIG(i), config);
  153. titsc_writel(ts_dev, REG_STEPDELAY(i), STEPCONFIG_OPENDLY);
  154. }
  155. /* Charge step configuration */
  156. config = ts_dev->bit_xp | ts_dev->bit_yn |
  157. STEPCHARGE_RFP_XPUL | STEPCHARGE_RFM_XNUR |
  158. STEPCHARGE_INM_AN1 | STEPCHARGE_INP(ts_dev->inp_yp);
  159. titsc_writel(ts_dev, REG_CHARGECONFIG, config);
  160. titsc_writel(ts_dev, REG_CHARGEDELAY, CHARGEDLY_OPENDLY);
  161. /* coordinate_readouts * 2 … coordinate_readouts * 2 + 2 is for Z */
  162. config = STEPCONFIG_MODE_HWSYNC |
  163. STEPCONFIG_AVG_16 | ts_dev->bit_yp |
  164. ts_dev->bit_xn | STEPCONFIG_INM_ADCREFM |
  165. STEPCONFIG_INP(ts_dev->inp_xp);
  166. titsc_writel(ts_dev, REG_STEPCONFIG(end_step), config);
  167. titsc_writel(ts_dev, REG_STEPDELAY(end_step),
  168. STEPCONFIG_OPENDLY);
  169. end_step++;
  170. config |= STEPCONFIG_INP(ts_dev->inp_yn);
  171. titsc_writel(ts_dev, REG_STEPCONFIG(end_step), config);
  172. titsc_writel(ts_dev, REG_STEPDELAY(end_step),
  173. STEPCONFIG_OPENDLY);
  174. /* The steps1 … end and bit 0 for TS_Charge */
  175. stepenable = (1 << (end_step + 2)) - 1;
  176. ts_dev->step_mask = stepenable;
  177. am335x_tsc_se_set(ts_dev->mfd_tscadc, ts_dev->step_mask);
  178. }
  179. static void titsc_read_coordinates(struct titsc *ts_dev,
  180. u32 *x, u32 *y, u32 *z1, u32 *z2)
  181. {
  182. unsigned int fifocount = titsc_readl(ts_dev, REG_FIFO0CNT);
  183. unsigned int prev_val_x = ~0, prev_val_y = ~0;
  184. unsigned int prev_diff_x = ~0, prev_diff_y = ~0;
  185. unsigned int read, diff;
  186. unsigned int i, channel;
  187. unsigned int creads = ts_dev->coordinate_readouts;
  188. *z1 = *z2 = 0;
  189. if (fifocount % (creads * 2 + 2))
  190. fifocount -= fifocount % (creads * 2 + 2);
  191. /*
  192. * Delta filter is used to remove large variations in sampled
  193. * values from ADC. The filter tries to predict where the next
  194. * coordinate could be. This is done by taking a previous
  195. * coordinate and subtracting it form current one. Further the
  196. * algorithm compares the difference with that of a present value,
  197. * if true the value is reported to the sub system.
  198. */
  199. for (i = 0; i < fifocount; i++) {
  200. read = titsc_readl(ts_dev, REG_FIFO0);
  201. channel = (read & 0xf0000) >> 16;
  202. read &= 0xfff;
  203. if (channel < creads) {
  204. diff = abs(read - prev_val_x);
  205. if (diff < prev_diff_x) {
  206. prev_diff_x = diff;
  207. *x = read;
  208. }
  209. prev_val_x = read;
  210. } else if (channel < creads * 2) {
  211. diff = abs(read - prev_val_y);
  212. if (diff < prev_diff_y) {
  213. prev_diff_y = diff;
  214. *y = read;
  215. }
  216. prev_val_y = read;
  217. } else if (channel < creads * 2 + 1) {
  218. *z1 = read;
  219. } else if (channel < creads * 2 + 2) {
  220. *z2 = read;
  221. }
  222. }
  223. }
  224. static irqreturn_t titsc_irq(int irq, void *dev)
  225. {
  226. struct titsc *ts_dev = dev;
  227. struct input_dev *input_dev = ts_dev->input;
  228. unsigned int status, irqclr = 0;
  229. unsigned int x = 0, y = 0;
  230. unsigned int z1, z2, z;
  231. unsigned int fsm;
  232. status = titsc_readl(ts_dev, REG_IRQSTATUS);
  233. /*
  234. * ADC and touchscreen share the IRQ line.
  235. * FIFO1 interrupts are used by ADC. Handle FIFO0 IRQs here only
  236. */
  237. if (status & IRQENB_FIFO0THRES) {
  238. titsc_read_coordinates(ts_dev, &x, &y, &z1, &z2);
  239. if (ts_dev->pen_down && z1 != 0 && z2 != 0) {
  240. /*
  241. * Calculate pressure using formula
  242. * Resistance(touch) = x plate resistance *
  243. * x postion/4096 * ((z2 / z1) - 1)
  244. */
  245. z = z1 - z2;
  246. z *= x;
  247. z *= ts_dev->x_plate_resistance;
  248. z /= z2;
  249. z = (z + 2047) >> 12;
  250. if (z <= MAX_12BIT) {
  251. input_report_abs(input_dev, ABS_X, x);
  252. input_report_abs(input_dev, ABS_Y, y);
  253. input_report_abs(input_dev, ABS_PRESSURE, z);
  254. input_report_key(input_dev, BTN_TOUCH, 1);
  255. input_sync(input_dev);
  256. }
  257. }
  258. irqclr |= IRQENB_FIFO0THRES;
  259. }
  260. /*
  261. * Time for sequencer to settle, to read
  262. * correct state of the sequencer.
  263. */
  264. udelay(SEQ_SETTLE);
  265. status = titsc_readl(ts_dev, REG_RAWIRQSTATUS);
  266. if (status & IRQENB_PENUP) {
  267. /* Pen up event */
  268. fsm = titsc_readl(ts_dev, REG_ADCFSM);
  269. if (fsm == ADCFSM_STEPID) {
  270. ts_dev->pen_down = false;
  271. input_report_key(input_dev, BTN_TOUCH, 0);
  272. input_report_abs(input_dev, ABS_PRESSURE, 0);
  273. input_sync(input_dev);
  274. } else {
  275. ts_dev->pen_down = true;
  276. }
  277. irqclr |= IRQENB_PENUP;
  278. }
  279. if (status & IRQENB_HW_PEN) {
  280. titsc_writel(ts_dev, REG_IRQWAKEUP, 0x00);
  281. titsc_writel(ts_dev, REG_IRQCLR, IRQENB_HW_PEN);
  282. }
  283. if (irqclr) {
  284. titsc_writel(ts_dev, REG_IRQSTATUS, irqclr);
  285. am335x_tsc_se_set(ts_dev->mfd_tscadc, ts_dev->step_mask);
  286. return IRQ_HANDLED;
  287. }
  288. return IRQ_NONE;
  289. }
  290. static int titsc_parse_dt(struct platform_device *pdev,
  291. struct titsc *ts_dev)
  292. {
  293. struct device_node *node = pdev->dev.of_node;
  294. int err;
  295. if (!node)
  296. return -EINVAL;
  297. err = of_property_read_u32(node, "ti,wires", &ts_dev->wires);
  298. if (err < 0)
  299. return err;
  300. switch (ts_dev->wires) {
  301. case 4:
  302. case 5:
  303. case 8:
  304. break;
  305. default:
  306. return -EINVAL;
  307. }
  308. err = of_property_read_u32(node, "ti,x-plate-resistance",
  309. &ts_dev->x_plate_resistance);
  310. if (err < 0)
  311. return err;
  312. err = of_property_read_u32(node, "ti,coordiante-readouts",
  313. &ts_dev->coordinate_readouts);
  314. if (err < 0)
  315. return err;
  316. return of_property_read_u32_array(node, "ti,wire-config",
  317. ts_dev->config_inp, ARRAY_SIZE(ts_dev->config_inp));
  318. }
  319. /*
  320. * The functions for inserting/removing driver as a module.
  321. */
  322. static int titsc_probe(struct platform_device *pdev)
  323. {
  324. struct titsc *ts_dev;
  325. struct input_dev *input_dev;
  326. struct ti_tscadc_dev *tscadc_dev = ti_tscadc_dev_get(pdev);
  327. int err;
  328. /* Allocate memory for device */
  329. ts_dev = kzalloc(sizeof(struct titsc), GFP_KERNEL);
  330. input_dev = input_allocate_device();
  331. if (!ts_dev || !input_dev) {
  332. dev_err(&pdev->dev, "failed to allocate memory.\n");
  333. err = -ENOMEM;
  334. goto err_free_mem;
  335. }
  336. tscadc_dev->tsc = ts_dev;
  337. ts_dev->mfd_tscadc = tscadc_dev;
  338. ts_dev->input = input_dev;
  339. ts_dev->irq = tscadc_dev->irq;
  340. err = titsc_parse_dt(pdev, ts_dev);
  341. if (err) {
  342. dev_err(&pdev->dev, "Could not find valid DT data.\n");
  343. goto err_free_mem;
  344. }
  345. err = request_irq(ts_dev->irq, titsc_irq,
  346. IRQF_SHARED, pdev->dev.driver->name, ts_dev);
  347. if (err) {
  348. dev_err(&pdev->dev, "failed to allocate irq.\n");
  349. goto err_free_mem;
  350. }
  351. titsc_writel(ts_dev, REG_IRQENABLE, IRQENB_FIFO0THRES);
  352. err = titsc_config_wires(ts_dev);
  353. if (err) {
  354. dev_err(&pdev->dev, "wrong i/p wire configuration\n");
  355. goto err_free_irq;
  356. }
  357. titsc_step_config(ts_dev);
  358. titsc_writel(ts_dev, REG_FIFO0THR,
  359. ts_dev->coordinate_readouts * 2 + 2 - 1);
  360. input_dev->name = "ti-tsc";
  361. input_dev->dev.parent = &pdev->dev;
  362. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  363. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  364. input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, 0, 0);
  365. input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
  366. input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT, 0, 0);
  367. /* register to the input system */
  368. err = input_register_device(input_dev);
  369. if (err)
  370. goto err_free_irq;
  371. platform_set_drvdata(pdev, ts_dev);
  372. return 0;
  373. err_free_irq:
  374. free_irq(ts_dev->irq, ts_dev);
  375. err_free_mem:
  376. input_free_device(input_dev);
  377. kfree(ts_dev);
  378. return err;
  379. }
  380. static int titsc_remove(struct platform_device *pdev)
  381. {
  382. struct titsc *ts_dev = platform_get_drvdata(pdev);
  383. u32 steps;
  384. free_irq(ts_dev->irq, ts_dev);
  385. /* total steps followed by the enable mask */
  386. steps = 2 * ts_dev->coordinate_readouts + 2;
  387. steps = (1 << steps) - 1;
  388. am335x_tsc_se_clr(ts_dev->mfd_tscadc, steps);
  389. input_unregister_device(ts_dev->input);
  390. kfree(ts_dev);
  391. return 0;
  392. }
  393. #ifdef CONFIG_PM
  394. static int titsc_suspend(struct device *dev)
  395. {
  396. struct titsc *ts_dev = dev_get_drvdata(dev);
  397. struct ti_tscadc_dev *tscadc_dev;
  398. unsigned int idle;
  399. tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
  400. if (device_may_wakeup(tscadc_dev->dev)) {
  401. idle = titsc_readl(ts_dev, REG_IRQENABLE);
  402. titsc_writel(ts_dev, REG_IRQENABLE,
  403. (idle | IRQENB_HW_PEN));
  404. titsc_writel(ts_dev, REG_IRQWAKEUP, IRQWKUP_ENB);
  405. }
  406. return 0;
  407. }
  408. static int titsc_resume(struct device *dev)
  409. {
  410. struct titsc *ts_dev = dev_get_drvdata(dev);
  411. struct ti_tscadc_dev *tscadc_dev;
  412. tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
  413. if (device_may_wakeup(tscadc_dev->dev)) {
  414. titsc_writel(ts_dev, REG_IRQWAKEUP,
  415. 0x00);
  416. titsc_writel(ts_dev, REG_IRQCLR, IRQENB_HW_PEN);
  417. }
  418. titsc_step_config(ts_dev);
  419. titsc_writel(ts_dev, REG_FIFO0THR,
  420. ts_dev->coordinate_readouts * 2 + 2 - 1);
  421. return 0;
  422. }
  423. static const struct dev_pm_ops titsc_pm_ops = {
  424. .suspend = titsc_suspend,
  425. .resume = titsc_resume,
  426. };
  427. #define TITSC_PM_OPS (&titsc_pm_ops)
  428. #else
  429. #define TITSC_PM_OPS NULL
  430. #endif
  431. static const struct of_device_id ti_tsc_dt_ids[] = {
  432. { .compatible = "ti,am3359-tsc", },
  433. { }
  434. };
  435. MODULE_DEVICE_TABLE(of, ti_tsc_dt_ids);
  436. static struct platform_driver ti_tsc_driver = {
  437. .probe = titsc_probe,
  438. .remove = titsc_remove,
  439. .driver = {
  440. .name = "TI-am335x-tsc",
  441. .owner = THIS_MODULE,
  442. .pm = TITSC_PM_OPS,
  443. .of_match_table = of_match_ptr(ti_tsc_dt_ids),
  444. },
  445. };
  446. module_platform_driver(ti_tsc_driver);
  447. MODULE_DESCRIPTION("TI touchscreen controller driver");
  448. MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
  449. MODULE_LICENSE("GPL");