ti_am335x_tsc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. err = of_property_read_u32(node, "ti,coordiante-readouts",
  307. &ts_dev->coordinate_readouts);
  308. if (err < 0)
  309. return err;
  310. return of_property_read_u32_array(node, "ti,wire-config",
  311. ts_dev->config_inp, ARRAY_SIZE(ts_dev->config_inp));
  312. }
  313. /*
  314. * The functions for inserting/removing driver as a module.
  315. */
  316. static int titsc_probe(struct platform_device *pdev)
  317. {
  318. struct titsc *ts_dev;
  319. struct input_dev *input_dev;
  320. struct ti_tscadc_dev *tscadc_dev = ti_tscadc_dev_get(pdev);
  321. int err;
  322. /* Allocate memory for device */
  323. ts_dev = kzalloc(sizeof(struct titsc), GFP_KERNEL);
  324. input_dev = input_allocate_device();
  325. if (!ts_dev || !input_dev) {
  326. dev_err(&pdev->dev, "failed to allocate memory.\n");
  327. err = -ENOMEM;
  328. goto err_free_mem;
  329. }
  330. tscadc_dev->tsc = ts_dev;
  331. ts_dev->mfd_tscadc = tscadc_dev;
  332. ts_dev->input = input_dev;
  333. ts_dev->irq = tscadc_dev->irq;
  334. err = titsc_parse_dt(pdev, ts_dev);
  335. if (err) {
  336. dev_err(&pdev->dev, "Could not find valid DT data.\n");
  337. goto err_free_mem;
  338. }
  339. err = request_irq(ts_dev->irq, titsc_irq,
  340. 0, pdev->dev.driver->name, ts_dev);
  341. if (err) {
  342. dev_err(&pdev->dev, "failed to allocate irq.\n");
  343. goto err_free_mem;
  344. }
  345. titsc_writel(ts_dev, REG_IRQENABLE, IRQENB_FIFO0THRES);
  346. err = titsc_config_wires(ts_dev);
  347. if (err) {
  348. dev_err(&pdev->dev, "wrong i/p wire configuration\n");
  349. goto err_free_irq;
  350. }
  351. titsc_step_config(ts_dev);
  352. titsc_writel(ts_dev, REG_FIFO0THR,
  353. ts_dev->coordinate_readouts * 2 + 2 - 1);
  354. input_dev->name = "ti-tsc";
  355. input_dev->dev.parent = &pdev->dev;
  356. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  357. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  358. input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, 0, 0);
  359. input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
  360. input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT, 0, 0);
  361. /* register to the input system */
  362. err = input_register_device(input_dev);
  363. if (err)
  364. goto err_free_irq;
  365. platform_set_drvdata(pdev, ts_dev);
  366. return 0;
  367. err_free_irq:
  368. free_irq(ts_dev->irq, ts_dev);
  369. err_free_mem:
  370. input_free_device(input_dev);
  371. kfree(ts_dev);
  372. return err;
  373. }
  374. static int titsc_remove(struct platform_device *pdev)
  375. {
  376. struct titsc *ts_dev = platform_get_drvdata(pdev);
  377. u32 steps;
  378. free_irq(ts_dev->irq, ts_dev);
  379. /* total steps followed by the enable mask */
  380. steps = 2 * ts_dev->coordinate_readouts + 2;
  381. steps = (1 << steps) - 1;
  382. am335x_tsc_se_clr(ts_dev->mfd_tscadc, steps);
  383. input_unregister_device(ts_dev->input);
  384. kfree(ts_dev);
  385. return 0;
  386. }
  387. #ifdef CONFIG_PM
  388. static int titsc_suspend(struct device *dev)
  389. {
  390. struct titsc *ts_dev = dev_get_drvdata(dev);
  391. struct ti_tscadc_dev *tscadc_dev;
  392. unsigned int idle;
  393. tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
  394. if (device_may_wakeup(tscadc_dev->dev)) {
  395. idle = titsc_readl(ts_dev, REG_IRQENABLE);
  396. titsc_writel(ts_dev, REG_IRQENABLE,
  397. (idle | IRQENB_HW_PEN));
  398. titsc_writel(ts_dev, REG_IRQWAKEUP, IRQWKUP_ENB);
  399. }
  400. return 0;
  401. }
  402. static int titsc_resume(struct device *dev)
  403. {
  404. struct titsc *ts_dev = dev_get_drvdata(dev);
  405. struct ti_tscadc_dev *tscadc_dev;
  406. tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
  407. if (device_may_wakeup(tscadc_dev->dev)) {
  408. titsc_writel(ts_dev, REG_IRQWAKEUP,
  409. 0x00);
  410. titsc_writel(ts_dev, REG_IRQCLR, IRQENB_HW_PEN);
  411. }
  412. titsc_step_config(ts_dev);
  413. titsc_writel(ts_dev, REG_FIFO0THR,
  414. ts_dev->coordinate_readouts * 2 + 2 - 1);
  415. return 0;
  416. }
  417. static const struct dev_pm_ops titsc_pm_ops = {
  418. .suspend = titsc_suspend,
  419. .resume = titsc_resume,
  420. };
  421. #define TITSC_PM_OPS (&titsc_pm_ops)
  422. #else
  423. #define TITSC_PM_OPS NULL
  424. #endif
  425. static const struct of_device_id ti_tsc_dt_ids[] = {
  426. { .compatible = "ti,am3359-tsc", },
  427. { }
  428. };
  429. MODULE_DEVICE_TABLE(of, ti_tsc_dt_ids);
  430. static struct platform_driver ti_tsc_driver = {
  431. .probe = titsc_probe,
  432. .remove = titsc_remove,
  433. .driver = {
  434. .name = "TI-am335x-tsc",
  435. .owner = THIS_MODULE,
  436. .pm = TITSC_PM_OPS,
  437. .of_match_table = of_match_ptr(ti_tsc_dt_ids),
  438. },
  439. };
  440. module_platform_driver(ti_tsc_driver);
  441. MODULE_DESCRIPTION("TI touchscreen controller driver");
  442. MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
  443. MODULE_LICENSE("GPL");