ti_am335x_tsc.c 14 KB

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