ti_am335x_tsc.c 14 KB

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