ti_am335x_tsc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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. titsc_writel(ts_dev, REG_IRQWAKEUP, 0x00);
  244. titsc_writel(ts_dev, REG_IRQCLR, IRQENB_HW_PEN);
  245. irqclr |= IRQENB_HW_PEN;
  246. }
  247. if (status & IRQENB_PENUP) {
  248. fsm = titsc_readl(ts_dev, REG_ADCFSM);
  249. if (fsm == ADCFSM_STEPID) {
  250. ts_dev->pen_down = false;
  251. input_report_key(input_dev, BTN_TOUCH, 0);
  252. input_report_abs(input_dev, ABS_PRESSURE, 0);
  253. input_sync(input_dev);
  254. } else {
  255. ts_dev->pen_down = true;
  256. }
  257. irqclr |= IRQENB_PENUP;
  258. }
  259. if (status & IRQENB_EOS)
  260. irqclr |= IRQENB_EOS;
  261. /*
  262. * ADC and touchscreen share the IRQ line.
  263. * FIFO1 interrupts are used by ADC. Handle FIFO0 IRQs here only
  264. */
  265. if (status & IRQENB_FIFO0THRES) {
  266. titsc_read_coordinates(ts_dev, &x, &y, &z1, &z2);
  267. if (ts_dev->pen_down && z1 != 0 && z2 != 0) {
  268. /*
  269. * Calculate pressure using formula
  270. * Resistance(touch) = x plate resistance *
  271. * x postion/4096 * ((z2 / z1) - 1)
  272. */
  273. z = z1 - z2;
  274. z *= x;
  275. z *= ts_dev->x_plate_resistance;
  276. z /= z2;
  277. z = (z + 2047) >> 12;
  278. if (z <= MAX_12BIT) {
  279. input_report_abs(input_dev, ABS_X, x);
  280. input_report_abs(input_dev, ABS_Y, y);
  281. input_report_abs(input_dev, ABS_PRESSURE, z);
  282. input_report_key(input_dev, BTN_TOUCH, 1);
  283. input_sync(input_dev);
  284. }
  285. }
  286. irqclr |= IRQENB_FIFO0THRES;
  287. }
  288. if (irqclr) {
  289. titsc_writel(ts_dev, REG_IRQSTATUS, irqclr);
  290. if (status & IRQENB_EOS)
  291. am335x_tsc_se_set_cache(ts_dev->mfd_tscadc,
  292. ts_dev->step_mask);
  293. return IRQ_HANDLED;
  294. }
  295. return IRQ_NONE;
  296. }
  297. static int titsc_parse_dt(struct platform_device *pdev,
  298. struct titsc *ts_dev)
  299. {
  300. struct device_node *node = pdev->dev.of_node;
  301. int err;
  302. if (!node)
  303. return -EINVAL;
  304. err = of_property_read_u32(node, "ti,wires", &ts_dev->wires);
  305. if (err < 0)
  306. return err;
  307. switch (ts_dev->wires) {
  308. case 4:
  309. case 5:
  310. case 8:
  311. break;
  312. default:
  313. return -EINVAL;
  314. }
  315. err = of_property_read_u32(node, "ti,x-plate-resistance",
  316. &ts_dev->x_plate_resistance);
  317. if (err < 0)
  318. return err;
  319. /*
  320. * Try with the new binding first. If it fails, try again with
  321. * bogus, miss-spelled version.
  322. */
  323. err = of_property_read_u32(node, "ti,coordinate-readouts",
  324. &ts_dev->coordinate_readouts);
  325. if (err < 0) {
  326. dev_warn(&pdev->dev, "please use 'ti,coordinate-readouts' instead\n");
  327. err = of_property_read_u32(node, "ti,coordiante-readouts",
  328. &ts_dev->coordinate_readouts);
  329. }
  330. if (err < 0)
  331. return err;
  332. if (ts_dev->coordinate_readouts <= 0) {
  333. dev_warn(&pdev->dev,
  334. "invalid co-ordinate readouts, resetting it to 5\n");
  335. ts_dev->coordinate_readouts = 5;
  336. }
  337. err = of_property_read_u32(node, "ti,charge-delay",
  338. &ts_dev->charge_delay);
  339. /*
  340. * If ti,charge-delay value is not specified, then use
  341. * CHARGEDLY_OPENDLY as the default value.
  342. */
  343. if (err < 0) {
  344. ts_dev->charge_delay = CHARGEDLY_OPENDLY;
  345. dev_warn(&pdev->dev, "ti,charge-delay not specified\n");
  346. }
  347. return of_property_read_u32_array(node, "ti,wire-config",
  348. ts_dev->config_inp, ARRAY_SIZE(ts_dev->config_inp));
  349. }
  350. /*
  351. * The functions for inserting/removing driver as a module.
  352. */
  353. static int titsc_probe(struct platform_device *pdev)
  354. {
  355. struct titsc *ts_dev;
  356. struct input_dev *input_dev;
  357. struct ti_tscadc_dev *tscadc_dev = ti_tscadc_dev_get(pdev);
  358. int err;
  359. /* Allocate memory for device */
  360. ts_dev = kzalloc(sizeof(struct titsc), GFP_KERNEL);
  361. input_dev = input_allocate_device();
  362. if (!ts_dev || !input_dev) {
  363. dev_err(&pdev->dev, "failed to allocate memory.\n");
  364. err = -ENOMEM;
  365. goto err_free_mem;
  366. }
  367. tscadc_dev->tsc = ts_dev;
  368. ts_dev->mfd_tscadc = tscadc_dev;
  369. ts_dev->input = input_dev;
  370. ts_dev->irq = tscadc_dev->irq;
  371. err = titsc_parse_dt(pdev, ts_dev);
  372. if (err) {
  373. dev_err(&pdev->dev, "Could not find valid DT data.\n");
  374. goto err_free_mem;
  375. }
  376. err = request_irq(ts_dev->irq, titsc_irq,
  377. IRQF_SHARED, pdev->dev.driver->name, ts_dev);
  378. if (err) {
  379. dev_err(&pdev->dev, "failed to allocate irq.\n");
  380. goto err_free_mem;
  381. }
  382. titsc_writel(ts_dev, REG_IRQENABLE, IRQENB_FIFO0THRES);
  383. titsc_writel(ts_dev, REG_IRQENABLE, IRQENB_EOS);
  384. err = titsc_config_wires(ts_dev);
  385. if (err) {
  386. dev_err(&pdev->dev, "wrong i/p wire configuration\n");
  387. goto err_free_irq;
  388. }
  389. titsc_step_config(ts_dev);
  390. titsc_writel(ts_dev, REG_FIFO0THR,
  391. ts_dev->coordinate_readouts * 2 + 2 - 1);
  392. input_dev->name = "ti-tsc";
  393. input_dev->dev.parent = &pdev->dev;
  394. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  395. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  396. input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, 0, 0);
  397. input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
  398. input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT, 0, 0);
  399. /* register to the input system */
  400. err = input_register_device(input_dev);
  401. if (err)
  402. goto err_free_irq;
  403. platform_set_drvdata(pdev, ts_dev);
  404. return 0;
  405. err_free_irq:
  406. free_irq(ts_dev->irq, ts_dev);
  407. err_free_mem:
  408. input_free_device(input_dev);
  409. kfree(ts_dev);
  410. return err;
  411. }
  412. static int titsc_remove(struct platform_device *pdev)
  413. {
  414. struct titsc *ts_dev = platform_get_drvdata(pdev);
  415. u32 steps;
  416. free_irq(ts_dev->irq, ts_dev);
  417. /* total steps followed by the enable mask */
  418. steps = 2 * ts_dev->coordinate_readouts + 2;
  419. steps = (1 << steps) - 1;
  420. am335x_tsc_se_clr(ts_dev->mfd_tscadc, steps);
  421. input_unregister_device(ts_dev->input);
  422. kfree(ts_dev);
  423. return 0;
  424. }
  425. #ifdef CONFIG_PM
  426. static int titsc_suspend(struct device *dev)
  427. {
  428. struct titsc *ts_dev = dev_get_drvdata(dev);
  429. struct ti_tscadc_dev *tscadc_dev;
  430. unsigned int idle;
  431. tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
  432. if (device_may_wakeup(tscadc_dev->dev)) {
  433. idle = titsc_readl(ts_dev, REG_IRQENABLE);
  434. titsc_writel(ts_dev, REG_IRQENABLE,
  435. (idle | IRQENB_HW_PEN));
  436. titsc_writel(ts_dev, REG_IRQWAKEUP, IRQWKUP_ENB);
  437. }
  438. return 0;
  439. }
  440. static int titsc_resume(struct device *dev)
  441. {
  442. struct titsc *ts_dev = dev_get_drvdata(dev);
  443. struct ti_tscadc_dev *tscadc_dev;
  444. tscadc_dev = ti_tscadc_dev_get(to_platform_device(dev));
  445. if (device_may_wakeup(tscadc_dev->dev)) {
  446. titsc_writel(ts_dev, REG_IRQWAKEUP,
  447. 0x00);
  448. titsc_writel(ts_dev, REG_IRQCLR, IRQENB_HW_PEN);
  449. }
  450. titsc_step_config(ts_dev);
  451. titsc_writel(ts_dev, REG_FIFO0THR,
  452. ts_dev->coordinate_readouts * 2 + 2 - 1);
  453. return 0;
  454. }
  455. static const struct dev_pm_ops titsc_pm_ops = {
  456. .suspend = titsc_suspend,
  457. .resume = titsc_resume,
  458. };
  459. #define TITSC_PM_OPS (&titsc_pm_ops)
  460. #else
  461. #define TITSC_PM_OPS NULL
  462. #endif
  463. static const struct of_device_id ti_tsc_dt_ids[] = {
  464. { .compatible = "ti,am3359-tsc", },
  465. { }
  466. };
  467. MODULE_DEVICE_TABLE(of, ti_tsc_dt_ids);
  468. static struct platform_driver ti_tsc_driver = {
  469. .probe = titsc_probe,
  470. .remove = titsc_remove,
  471. .driver = {
  472. .name = "TI-am335x-tsc",
  473. .pm = TITSC_PM_OPS,
  474. .of_match_table = ti_tsc_dt_ids,
  475. },
  476. };
  477. module_platform_driver(ti_tsc_driver);
  478. MODULE_DESCRIPTION("TI touchscreen controller driver");
  479. MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
  480. MODULE_LICENSE("GPL");