ti_am335x_tsc.c 12 KB

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