ti_am335x_tsc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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/kernel.h>
  16. #include <linux/err.h>
  17. #include <linux/module.h>
  18. #include <linux/input.h>
  19. #include <linux/slab.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/clk.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/io.h>
  24. #include <linux/delay.h>
  25. #include <linux/of.h>
  26. #include <linux/sort.h>
  27. #include <linux/pm_wakeirq.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. #define TSC_IRQENB_MASK (IRQENB_FIFO0THRES | IRQENB_EOS | IRQENB_HW_PEN)
  33. static const int config_pins[] = {
  34. STEPCONFIG_XPP,
  35. STEPCONFIG_XNN,
  36. STEPCONFIG_YPP,
  37. STEPCONFIG_YNN,
  38. };
  39. struct titsc {
  40. struct input_dev *input;
  41. struct ti_tscadc_dev *mfd_tscadc;
  42. struct device *dev;
  43. unsigned int irq;
  44. unsigned int wires;
  45. unsigned int x_plate_resistance;
  46. bool pen_down;
  47. int coordinate_readouts;
  48. u32 config_inp[4];
  49. u32 bit_xp, bit_xn, bit_yp, bit_yn;
  50. u32 inp_xp, inp_xn, inp_yp, inp_yn;
  51. u32 step_mask;
  52. u32 charge_delay;
  53. };
  54. static unsigned int titsc_readl(struct titsc *ts, unsigned int reg)
  55. {
  56. return readl(ts->mfd_tscadc->tscadc_base + reg);
  57. }
  58. static void titsc_writel(struct titsc *tsc, unsigned int reg,
  59. unsigned int val)
  60. {
  61. writel(val, tsc->mfd_tscadc->tscadc_base + reg);
  62. }
  63. static int titsc_config_wires(struct titsc *ts_dev)
  64. {
  65. u32 analog_line[4];
  66. u32 wire_order[4];
  67. int i, bit_cfg;
  68. for (i = 0; i < 4; i++) {
  69. /*
  70. * Get the order in which TSC wires are attached
  71. * w.r.t. each of the analog input lines on the EVM.
  72. */
  73. analog_line[i] = (ts_dev->config_inp[i] & 0xF0) >> 4;
  74. wire_order[i] = ts_dev->config_inp[i] & 0x0F;
  75. if (WARN_ON(analog_line[i] > 7))
  76. return -EINVAL;
  77. if (WARN_ON(wire_order[i] >= ARRAY_SIZE(config_pins)))
  78. return -EINVAL;
  79. }
  80. for (i = 0; i < 4; i++) {
  81. int an_line;
  82. int wi_order;
  83. an_line = analog_line[i];
  84. wi_order = wire_order[i];
  85. bit_cfg = config_pins[wi_order];
  86. if (bit_cfg == 0)
  87. return -EINVAL;
  88. switch (wi_order) {
  89. case 0:
  90. ts_dev->bit_xp = bit_cfg;
  91. ts_dev->inp_xp = an_line;
  92. break;
  93. case 1:
  94. ts_dev->bit_xn = bit_cfg;
  95. ts_dev->inp_xn = an_line;
  96. break;
  97. case 2:
  98. ts_dev->bit_yp = bit_cfg;
  99. ts_dev->inp_yp = an_line;
  100. break;
  101. case 3:
  102. ts_dev->bit_yn = bit_cfg;
  103. ts_dev->inp_yn = an_line;
  104. break;
  105. }
  106. }
  107. return 0;
  108. }
  109. static void titsc_step_config(struct titsc *ts_dev)
  110. {
  111. unsigned int config;
  112. int i, n;
  113. int end_step, first_step, tsc_steps;
  114. u32 stepenable;
  115. config = STEPCONFIG_MODE_HWSYNC |
  116. STEPCONFIG_AVG_16 | ts_dev->bit_xp |
  117. STEPCONFIG_INM_ADCREFM;
  118. switch (ts_dev->wires) {
  119. case 4:
  120. config |= STEPCONFIG_INP(ts_dev->inp_yp) | ts_dev->bit_xn;
  121. break;
  122. case 5:
  123. config |= ts_dev->bit_yn |
  124. STEPCONFIG_INP_AN4 | ts_dev->bit_xn |
  125. ts_dev->bit_yp;
  126. break;
  127. case 8:
  128. config |= STEPCONFIG_INP(ts_dev->inp_yp) | ts_dev->bit_xn;
  129. break;
  130. }
  131. tsc_steps = ts_dev->coordinate_readouts * 2 + 2;
  132. first_step = TOTAL_STEPS - tsc_steps;
  133. /* Steps 16 to 16-coordinate_readouts is for X */
  134. end_step = first_step + tsc_steps;
  135. n = 0;
  136. for (i = end_step - ts_dev->coordinate_readouts; i < end_step; i++) {
  137. titsc_writel(ts_dev, REG_STEPCONFIG(i), config);
  138. titsc_writel(ts_dev, REG_STEPDELAY(i),
  139. n++ == 0 ? STEPCONFIG_OPENDLY : 0);
  140. }
  141. config = STEPCONFIG_MODE_HWSYNC |
  142. STEPCONFIG_AVG_16 | ts_dev->bit_yn |
  143. STEPCONFIG_INM_ADCREFM;
  144. switch (ts_dev->wires) {
  145. case 4:
  146. config |= ts_dev->bit_yp | STEPCONFIG_INP(ts_dev->inp_xp);
  147. break;
  148. case 5:
  149. config |= ts_dev->bit_xp | STEPCONFIG_INP_AN4 |
  150. STEPCONFIG_XNP | STEPCONFIG_YPN;
  151. break;
  152. case 8:
  153. config |= ts_dev->bit_yp | STEPCONFIG_INP(ts_dev->inp_xp);
  154. break;
  155. }
  156. /* 1 ... coordinate_readouts is for Y */
  157. end_step = first_step + ts_dev->coordinate_readouts;
  158. n = 0;
  159. for (i = first_step; i < end_step; i++) {
  160. titsc_writel(ts_dev, REG_STEPCONFIG(i), config);
  161. titsc_writel(ts_dev, REG_STEPDELAY(i),
  162. n++ == 0 ? STEPCONFIG_OPENDLY : 0);
  163. }
  164. /* Make CHARGECONFIG same as IDLECONFIG */
  165. config = titsc_readl(ts_dev, REG_IDLECONFIG);
  166. titsc_writel(ts_dev, REG_CHARGECONFIG, config);
  167. titsc_writel(ts_dev, REG_CHARGEDELAY, ts_dev->charge_delay);
  168. /* coordinate_readouts + 1 ... coordinate_readouts + 2 is for Z */
  169. config = STEPCONFIG_MODE_HWSYNC |
  170. STEPCONFIG_AVG_16 | ts_dev->bit_yp |
  171. ts_dev->bit_xn | STEPCONFIG_INM_ADCREFM |
  172. STEPCONFIG_INP(ts_dev->inp_xp);
  173. titsc_writel(ts_dev, REG_STEPCONFIG(end_step), config);
  174. titsc_writel(ts_dev, REG_STEPDELAY(end_step),
  175. STEPCONFIG_OPENDLY);
  176. end_step++;
  177. config = STEPCONFIG_MODE_HWSYNC |
  178. STEPCONFIG_AVG_16 | ts_dev->bit_yp |
  179. ts_dev->bit_xn | STEPCONFIG_INM_ADCREFM |
  180. STEPCONFIG_INP(ts_dev->inp_yn);
  181. titsc_writel(ts_dev, REG_STEPCONFIG(end_step), config);
  182. titsc_writel(ts_dev, REG_STEPDELAY(end_step),
  183. STEPCONFIG_OPENDLY);
  184. /* The steps end ... end - readouts * 2 + 2 and bit 0 for TS_Charge */
  185. stepenable = 1;
  186. for (i = 0; i < tsc_steps; i++)
  187. stepenable |= 1 << (first_step + i + 1);
  188. ts_dev->step_mask = stepenable;
  189. am335x_tsc_se_set_cache(ts_dev->mfd_tscadc, ts_dev->step_mask);
  190. }
  191. static int titsc_cmp_coord(const void *a, const void *b)
  192. {
  193. return *(int *)a - *(int *)b;
  194. }
  195. static void titsc_read_coordinates(struct titsc *ts_dev,
  196. u32 *x, u32 *y, u32 *z1, u32 *z2)
  197. {
  198. unsigned int yvals[7], xvals[7];
  199. unsigned int i, xsum = 0, ysum = 0;
  200. unsigned int creads = ts_dev->coordinate_readouts;
  201. for (i = 0; i < creads; i++) {
  202. yvals[i] = titsc_readl(ts_dev, REG_FIFO0);
  203. yvals[i] &= 0xfff;
  204. }
  205. *z1 = titsc_readl(ts_dev, REG_FIFO0);
  206. *z1 &= 0xfff;
  207. *z2 = titsc_readl(ts_dev, REG_FIFO0);
  208. *z2 &= 0xfff;
  209. for (i = 0; i < creads; i++) {
  210. xvals[i] = titsc_readl(ts_dev, REG_FIFO0);
  211. xvals[i] &= 0xfff;
  212. }
  213. /*
  214. * If co-ordinates readouts is less than 4 then
  215. * report the average. In case of 4 or more
  216. * readouts, sort the co-ordinate samples, drop
  217. * min and max values and report the average of
  218. * remaining values.
  219. */
  220. if (creads <= 3) {
  221. for (i = 0; i < creads; i++) {
  222. ysum += yvals[i];
  223. xsum += xvals[i];
  224. }
  225. ysum /= creads;
  226. xsum /= creads;
  227. } else {
  228. sort(yvals, creads, sizeof(unsigned int),
  229. titsc_cmp_coord, NULL);
  230. sort(xvals, creads, sizeof(unsigned int),
  231. titsc_cmp_coord, NULL);
  232. for (i = 1; i < creads - 1; i++) {
  233. ysum += yvals[i];
  234. xsum += xvals[i];
  235. }
  236. ysum /= creads - 2;
  237. xsum /= creads - 2;
  238. }
  239. *y = ysum;
  240. *x = xsum;
  241. }
  242. static irqreturn_t titsc_irq(int irq, void *dev)
  243. {
  244. struct titsc *ts_dev = dev;
  245. struct input_dev *input_dev = ts_dev->input;
  246. unsigned int fsm, status, irqclr = 0;
  247. unsigned int x = 0, y = 0;
  248. unsigned int z1, z2, z;
  249. status = titsc_readl(ts_dev, REG_RAWIRQSTATUS);
  250. if (status & IRQENB_HW_PEN) {
  251. ts_dev->pen_down = true;
  252. irqclr |= IRQENB_HW_PEN;
  253. pm_stay_awake(ts_dev->dev);
  254. }
  255. if (status & IRQENB_PENUP) {
  256. fsm = titsc_readl(ts_dev, REG_ADCFSM);
  257. if (fsm == ADCFSM_STEPID) {
  258. ts_dev->pen_down = false;
  259. input_report_key(input_dev, BTN_TOUCH, 0);
  260. input_report_abs(input_dev, ABS_PRESSURE, 0);
  261. input_sync(input_dev);
  262. pm_relax(ts_dev->dev);
  263. } else {
  264. ts_dev->pen_down = true;
  265. }
  266. irqclr |= IRQENB_PENUP;
  267. }
  268. if (status & IRQENB_EOS)
  269. irqclr |= IRQENB_EOS;
  270. /*
  271. * ADC and touchscreen share the IRQ line.
  272. * FIFO1 interrupts are used by ADC. Handle FIFO0 IRQs here only
  273. */
  274. if (status & IRQENB_FIFO0THRES) {
  275. titsc_read_coordinates(ts_dev, &x, &y, &z1, &z2);
  276. if (ts_dev->pen_down && z1 != 0 && z2 != 0) {
  277. /*
  278. * Calculate pressure using formula
  279. * Resistance(touch) = x plate resistance *
  280. * x position/4096 * ((z2 / z1) - 1)
  281. */
  282. z = z1 - z2;
  283. z *= x;
  284. z *= ts_dev->x_plate_resistance;
  285. z /= z2;
  286. z = (z + 2047) >> 12;
  287. if (z <= MAX_12BIT) {
  288. input_report_abs(input_dev, ABS_X, x);
  289. input_report_abs(input_dev, ABS_Y, y);
  290. input_report_abs(input_dev, ABS_PRESSURE, z);
  291. input_report_key(input_dev, BTN_TOUCH, 1);
  292. input_sync(input_dev);
  293. }
  294. }
  295. irqclr |= IRQENB_FIFO0THRES;
  296. }
  297. if (irqclr) {
  298. titsc_writel(ts_dev, REG_IRQSTATUS, irqclr);
  299. if (status & IRQENB_EOS)
  300. am335x_tsc_se_set_cache(ts_dev->mfd_tscadc,
  301. ts_dev->step_mask);
  302. return IRQ_HANDLED;
  303. }
  304. return IRQ_NONE;
  305. }
  306. static int titsc_parse_dt(struct platform_device *pdev,
  307. struct titsc *ts_dev)
  308. {
  309. struct device_node *node = pdev->dev.of_node;
  310. int err;
  311. if (!node)
  312. return -EINVAL;
  313. err = of_property_read_u32(node, "ti,wires", &ts_dev->wires);
  314. if (err < 0)
  315. return err;
  316. switch (ts_dev->wires) {
  317. case 4:
  318. case 5:
  319. case 8:
  320. break;
  321. default:
  322. return -EINVAL;
  323. }
  324. err = of_property_read_u32(node, "ti,x-plate-resistance",
  325. &ts_dev->x_plate_resistance);
  326. if (err < 0)
  327. return err;
  328. /*
  329. * Try with the new binding first. If it fails, try again with
  330. * bogus, miss-spelled version.
  331. */
  332. err = of_property_read_u32(node, "ti,coordinate-readouts",
  333. &ts_dev->coordinate_readouts);
  334. if (err < 0) {
  335. dev_warn(&pdev->dev, "please use 'ti,coordinate-readouts' instead\n");
  336. err = of_property_read_u32(node, "ti,coordiante-readouts",
  337. &ts_dev->coordinate_readouts);
  338. }
  339. if (err < 0)
  340. return err;
  341. if (ts_dev->coordinate_readouts <= 0) {
  342. dev_warn(&pdev->dev,
  343. "invalid co-ordinate readouts, resetting it to 5\n");
  344. ts_dev->coordinate_readouts = 5;
  345. } else if (ts_dev->coordinate_readouts > 6) {
  346. dev_warn(&pdev->dev,
  347. "co-ordinate readouts too large, limiting to 6\n");
  348. ts_dev->coordinate_readouts = 6;
  349. }
  350. err = of_property_read_u32(node, "ti,charge-delay",
  351. &ts_dev->charge_delay);
  352. /*
  353. * If ti,charge-delay value is not specified, then use
  354. * CHARGEDLY_OPENDLY as the default value.
  355. */
  356. if (err < 0) {
  357. ts_dev->charge_delay = CHARGEDLY_OPENDLY;
  358. dev_warn(&pdev->dev, "ti,charge-delay not specified\n");
  359. }
  360. return of_property_read_u32_array(node, "ti,wire-config",
  361. ts_dev->config_inp, ARRAY_SIZE(ts_dev->config_inp));
  362. }
  363. /*
  364. * The functions for inserting/removing driver as a module.
  365. */
  366. static int titsc_probe(struct platform_device *pdev)
  367. {
  368. struct titsc *ts_dev;
  369. struct input_dev *input_dev;
  370. struct ti_tscadc_dev *tscadc_dev = ti_tscadc_dev_get(pdev);
  371. int err;
  372. /* Allocate memory for device */
  373. ts_dev = kzalloc_obj(*ts_dev);
  374. input_dev = input_allocate_device();
  375. if (!ts_dev || !input_dev) {
  376. dev_err(&pdev->dev, "failed to allocate memory.\n");
  377. err = -ENOMEM;
  378. goto err_free_mem;
  379. }
  380. tscadc_dev->tsc = ts_dev;
  381. ts_dev->mfd_tscadc = tscadc_dev;
  382. ts_dev->input = input_dev;
  383. ts_dev->irq = tscadc_dev->irq;
  384. ts_dev->dev = &pdev->dev;
  385. err = titsc_parse_dt(pdev, ts_dev);
  386. if (err) {
  387. dev_err(&pdev->dev, "Could not find valid DT data.\n");
  388. goto err_free_mem;
  389. }
  390. err = request_irq(ts_dev->irq, titsc_irq,
  391. IRQF_SHARED, pdev->dev.driver->name, ts_dev);
  392. if (err) {
  393. dev_err(&pdev->dev, "failed to allocate irq.\n");
  394. goto err_free_mem;
  395. }
  396. device_init_wakeup(&pdev->dev, true);
  397. err = dev_pm_set_wake_irq(&pdev->dev, ts_dev->irq);
  398. if (err)
  399. dev_err(&pdev->dev, "irq wake enable failed.\n");
  400. titsc_writel(ts_dev, REG_IRQSTATUS, TSC_IRQENB_MASK);
  401. titsc_writel(ts_dev, REG_IRQENABLE, IRQENB_FIFO0THRES);
  402. titsc_writel(ts_dev, REG_IRQENABLE, IRQENB_EOS);
  403. err = titsc_config_wires(ts_dev);
  404. if (err) {
  405. dev_err(&pdev->dev, "wrong i/p wire configuration\n");
  406. goto err_free_irq;
  407. }
  408. titsc_step_config(ts_dev);
  409. titsc_writel(ts_dev, REG_FIFO0THR,
  410. ts_dev->coordinate_readouts * 2 + 2 - 1);
  411. input_dev->name = "ti-tsc";
  412. input_dev->dev.parent = &pdev->dev;
  413. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  414. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  415. input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, 0, 0);
  416. input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
  417. input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT, 0, 0);
  418. /* register to the input system */
  419. err = input_register_device(input_dev);
  420. if (err)
  421. goto err_free_irq;
  422. platform_set_drvdata(pdev, ts_dev);
  423. return 0;
  424. err_free_irq:
  425. dev_pm_clear_wake_irq(&pdev->dev);
  426. device_init_wakeup(&pdev->dev, false);
  427. free_irq(ts_dev->irq, ts_dev);
  428. err_free_mem:
  429. input_free_device(input_dev);
  430. kfree(ts_dev);
  431. return err;
  432. }
  433. static void titsc_remove(struct platform_device *pdev)
  434. {
  435. struct titsc *ts_dev = platform_get_drvdata(pdev);
  436. u32 steps;
  437. dev_pm_clear_wake_irq(&pdev->dev);
  438. device_init_wakeup(&pdev->dev, false);
  439. free_irq(ts_dev->irq, ts_dev);
  440. /* total steps followed by the enable mask */
  441. steps = 2 * ts_dev->coordinate_readouts + 2;
  442. steps = (1 << steps) - 1;
  443. am335x_tsc_se_clr(ts_dev->mfd_tscadc, steps);
  444. input_unregister_device(ts_dev->input);
  445. kfree(ts_dev);
  446. }
  447. static int titsc_suspend(struct device *dev)
  448. {
  449. struct titsc *ts_dev = dev_get_drvdata(dev);
  450. unsigned int idle;
  451. if (device_may_wakeup(dev)) {
  452. titsc_writel(ts_dev, REG_IRQSTATUS, TSC_IRQENB_MASK);
  453. idle = titsc_readl(ts_dev, REG_IRQENABLE);
  454. titsc_writel(ts_dev, REG_IRQENABLE,
  455. (idle | IRQENB_HW_PEN));
  456. titsc_writel(ts_dev, REG_IRQWAKEUP, IRQWKUP_ENB);
  457. }
  458. return 0;
  459. }
  460. static int titsc_resume(struct device *dev)
  461. {
  462. struct titsc *ts_dev = dev_get_drvdata(dev);
  463. if (device_may_wakeup(dev)) {
  464. titsc_writel(ts_dev, REG_IRQWAKEUP,
  465. 0x00);
  466. titsc_writel(ts_dev, REG_IRQCLR, IRQENB_HW_PEN);
  467. pm_relax(dev);
  468. }
  469. titsc_step_config(ts_dev);
  470. titsc_writel(ts_dev, REG_FIFO0THR,
  471. ts_dev->coordinate_readouts * 2 + 2 - 1);
  472. return 0;
  473. }
  474. static DEFINE_SIMPLE_DEV_PM_OPS(titsc_pm_ops, titsc_suspend, titsc_resume);
  475. static const struct of_device_id ti_tsc_dt_ids[] = {
  476. { .compatible = "ti,am3359-tsc", },
  477. { }
  478. };
  479. MODULE_DEVICE_TABLE(of, ti_tsc_dt_ids);
  480. static struct platform_driver ti_tsc_driver = {
  481. .probe = titsc_probe,
  482. .remove = titsc_remove,
  483. .driver = {
  484. .name = "TI-am335x-tsc",
  485. .pm = pm_sleep_ptr(&titsc_pm_ops),
  486. .of_match_table = ti_tsc_dt_ids,
  487. },
  488. };
  489. module_platform_driver(ti_tsc_driver);
  490. MODULE_DESCRIPTION("TI touchscreen controller driver");
  491. MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
  492. MODULE_LICENSE("GPL");