ti_am335x_tsc.c 12 KB

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