ti_am335x_tsc.c 13 KB

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