ti_am335x_tsc.c 13 KB

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