ti_tscadc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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/init.h>
  16. #include <linux/kernel.h>
  17. #include <linux/err.h>
  18. #include <linux/module.h>
  19. #include <linux/input.h>
  20. #include <linux/slab.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/clk.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/io.h>
  25. #include <linux/input/ti_tscadc.h>
  26. #include <linux/delay.h>
  27. #define REG_IRQEOI 0x020
  28. #define REG_RAWIRQSTATUS 0x024
  29. #define REG_IRQSTATUS 0x028
  30. #define REG_IRQENABLE 0x02C
  31. #define REG_IRQWAKEUP 0x034
  32. #define REG_CTRL 0x040
  33. #define REG_ADCFSM 0x044
  34. #define REG_CLKDIV 0x04C
  35. #define REG_SE 0x054
  36. #define REG_IDLECONFIG 0x058
  37. #define REG_CHARGECONFIG 0x05C
  38. #define REG_CHARGEDELAY 0x060
  39. #define REG_STEPCONFIG(n) (0x64 + ((n - 1) * 8))
  40. #define REG_STEPDELAY(n) (0x68 + ((n - 1) * 8))
  41. #define REG_STEPCONFIG13 0x0C4
  42. #define REG_STEPDELAY13 0x0C8
  43. #define REG_STEPCONFIG14 0x0CC
  44. #define REG_STEPDELAY14 0x0D0
  45. #define REG_FIFO0CNT 0xE4
  46. #define REG_FIFO1THR 0xF4
  47. #define REG_FIFO0 0x100
  48. #define REG_FIFO1 0x200
  49. /* Register Bitfields */
  50. #define IRQWKUP_ENB BIT(0)
  51. #define STPENB_STEPENB 0x7FFF
  52. #define IRQENB_FIFO1THRES BIT(5)
  53. #define IRQENB_PENUP BIT(9)
  54. #define STEPCONFIG_MODE_HWSYNC 0x2
  55. #define STEPCONFIG_SAMPLES_AVG (1 << 4)
  56. #define STEPCONFIG_XPP (1 << 5)
  57. #define STEPCONFIG_XNN (1 << 6)
  58. #define STEPCONFIG_YPP (1 << 7)
  59. #define STEPCONFIG_YNN (1 << 8)
  60. #define STEPCONFIG_XNP (1 << 9)
  61. #define STEPCONFIG_YPN (1 << 10)
  62. #define STEPCONFIG_INM (1 << 18)
  63. #define STEPCONFIG_INP (1 << 20)
  64. #define STEPCONFIG_INP_5 (1 << 21)
  65. #define STEPCONFIG_FIFO1 (1 << 26)
  66. #define STEPCONFIG_OPENDLY 0xff
  67. #define STEPCONFIG_Z1 (3 << 19)
  68. #define STEPIDLE_INP (1 << 22)
  69. #define STEPCHARGE_RFP (1 << 12)
  70. #define STEPCHARGE_INM (1 << 15)
  71. #define STEPCHARGE_INP (1 << 19)
  72. #define STEPCHARGE_RFM (1 << 23)
  73. #define STEPCHARGE_DELAY 0x1
  74. #define CNTRLREG_TSCSSENB (1 << 0)
  75. #define CNTRLREG_STEPID (1 << 1)
  76. #define CNTRLREG_STEPCONFIGWRT (1 << 2)
  77. #define CNTRLREG_4WIRE (1 << 5)
  78. #define CNTRLREG_5WIRE (1 << 6)
  79. #define CNTRLREG_8WIRE (3 << 5)
  80. #define CNTRLREG_TSCENB (1 << 7)
  81. #define ADCFSM_STEPID 0x10
  82. #define SEQ_SETTLE 275
  83. #define ADC_CLK 3000000
  84. #define MAX_12BIT ((1 << 12) - 1)
  85. #define TSCADC_DELTA_X 15
  86. #define TSCADC_DELTA_Y 15
  87. struct tscadc {
  88. struct input_dev *input;
  89. struct clk *tsc_ick;
  90. void __iomem *tsc_base;
  91. unsigned int irq;
  92. unsigned int wires;
  93. unsigned int x_plate_resistance;
  94. bool pen_down;
  95. };
  96. static unsigned int tscadc_readl(struct tscadc *ts, unsigned int reg)
  97. {
  98. return readl(ts->tsc_base + reg);
  99. }
  100. static void tscadc_writel(struct tscadc *tsc, unsigned int reg,
  101. unsigned int val)
  102. {
  103. writel(val, tsc->tsc_base + reg);
  104. }
  105. static void tscadc_step_config(struct tscadc *ts_dev)
  106. {
  107. unsigned int config;
  108. int i;
  109. /* Configure the Step registers */
  110. config = STEPCONFIG_MODE_HWSYNC |
  111. STEPCONFIG_SAMPLES_AVG | STEPCONFIG_XPP;
  112. switch (ts_dev->wires) {
  113. case 4:
  114. config |= STEPCONFIG_INP | STEPCONFIG_XNN;
  115. break;
  116. case 5:
  117. config |= STEPCONFIG_YNN |
  118. STEPCONFIG_INP_5 | STEPCONFIG_XNN |
  119. STEPCONFIG_YPP;
  120. break;
  121. case 8:
  122. config |= STEPCONFIG_INP | STEPCONFIG_XNN;
  123. break;
  124. }
  125. for (i = 1; i < 7; i++) {
  126. tscadc_writel(ts_dev, REG_STEPCONFIG(i), config);
  127. tscadc_writel(ts_dev, REG_STEPDELAY(i), STEPCONFIG_OPENDLY);
  128. }
  129. config = 0;
  130. config = STEPCONFIG_MODE_HWSYNC |
  131. STEPCONFIG_SAMPLES_AVG | STEPCONFIG_YNN |
  132. STEPCONFIG_INM | STEPCONFIG_FIFO1;
  133. switch (ts_dev->wires) {
  134. case 4:
  135. config |= STEPCONFIG_YPP;
  136. break;
  137. case 5:
  138. config |= STEPCONFIG_XPP | STEPCONFIG_INP_5 |
  139. STEPCONFIG_XNP | STEPCONFIG_YPN;
  140. break;
  141. case 8:
  142. config |= STEPCONFIG_YPP;
  143. break;
  144. }
  145. for (i = 7; i < 13; i++) {
  146. tscadc_writel(ts_dev, REG_STEPCONFIG(i), config);
  147. tscadc_writel(ts_dev, REG_STEPDELAY(i), STEPCONFIG_OPENDLY);
  148. }
  149. config = 0;
  150. /* Charge step configuration */
  151. config = STEPCONFIG_XPP | STEPCONFIG_YNN |
  152. STEPCHARGE_RFP | STEPCHARGE_RFM |
  153. STEPCHARGE_INM | STEPCHARGE_INP;
  154. tscadc_writel(ts_dev, REG_CHARGECONFIG, config);
  155. tscadc_writel(ts_dev, REG_CHARGEDELAY, STEPCHARGE_DELAY);
  156. config = 0;
  157. /* Configure to calculate pressure */
  158. config = STEPCONFIG_MODE_HWSYNC |
  159. STEPCONFIG_SAMPLES_AVG | STEPCONFIG_YPP |
  160. STEPCONFIG_XNN | STEPCONFIG_INM;
  161. tscadc_writel(ts_dev, REG_STEPCONFIG13, config);
  162. tscadc_writel(ts_dev, REG_STEPDELAY13, STEPCONFIG_OPENDLY);
  163. config |= STEPCONFIG_Z1 | STEPCONFIG_FIFO1;
  164. tscadc_writel(ts_dev, REG_STEPCONFIG14, config);
  165. tscadc_writel(ts_dev, REG_STEPDELAY14, STEPCONFIG_OPENDLY);
  166. tscadc_writel(ts_dev, REG_SE, STPENB_STEPENB);
  167. }
  168. static void tscadc_idle_config(struct tscadc *ts_config)
  169. {
  170. unsigned int idleconfig;
  171. idleconfig = STEPCONFIG_YNN |
  172. STEPCONFIG_INM |
  173. STEPCONFIG_YPN | STEPIDLE_INP;
  174. tscadc_writel(ts_config, REG_IDLECONFIG, idleconfig);
  175. }
  176. static void tscadc_read_coordinates(struct tscadc *ts_dev,
  177. unsigned int *x, unsigned int *y)
  178. {
  179. unsigned int fifocount = tscadc_readl(ts_dev, REG_FIFO0CNT);
  180. unsigned int prev_val_x = ~0, prev_val_y = ~0;
  181. unsigned int prev_diff_x = ~0, prev_diff_y = ~0;
  182. unsigned int read, diff;
  183. unsigned int i;
  184. /*
  185. * Delta filter is used to remove large variations in sampled
  186. * values from ADC. The filter tries to predict where the next
  187. * coordinate could be. This is done by taking a previous
  188. * coordinate and subtracting it form current one. Further the
  189. * algorithm compares the difference with that of a present value,
  190. * if true the value is reported to the sub system.
  191. */
  192. for (i = 0; i < fifocount - 1; i++) {
  193. read = tscadc_readl(ts_dev, REG_FIFO0) & 0xfff;
  194. diff = abs(read - prev_val_x);
  195. if (diff < prev_diff_x) {
  196. prev_diff_x = diff;
  197. *x = read;
  198. }
  199. prev_val_x = read;
  200. read = tscadc_readl(ts_dev, REG_FIFO1) & 0xfff;
  201. diff = abs(read - prev_val_y);
  202. if (diff < prev_diff_y) {
  203. prev_diff_y = diff;
  204. *y = read;
  205. }
  206. prev_val_y = read;
  207. }
  208. }
  209. static irqreturn_t tscadc_irq(int irq, void *dev)
  210. {
  211. struct tscadc *ts_dev = dev;
  212. struct input_dev *input_dev = ts_dev->input;
  213. unsigned int status, irqclr = 0;
  214. unsigned int x = 0, y = 0;
  215. unsigned int z1, z2, z;
  216. unsigned int fsm;
  217. status = tscadc_readl(ts_dev, REG_IRQSTATUS);
  218. if (status & IRQENB_FIFO1THRES) {
  219. tscadc_read_coordinates(ts_dev, &x, &y);
  220. z1 = tscadc_readl(ts_dev, REG_FIFO0) & 0xfff;
  221. z2 = tscadc_readl(ts_dev, REG_FIFO1) & 0xfff;
  222. if (ts_dev->pen_down && z1 != 0 && z2 != 0) {
  223. /*
  224. * Calculate pressure using formula
  225. * Resistance(touch) = x plate resistance *
  226. * x postion/4096 * ((z2 / z1) - 1)
  227. */
  228. z = z2 - z1;
  229. z *= x;
  230. z *= ts_dev->x_plate_resistance;
  231. z /= z1;
  232. z = (z + 2047) >> 12;
  233. if (z <= MAX_12BIT) {
  234. input_report_abs(input_dev, ABS_X, x);
  235. input_report_abs(input_dev, ABS_Y, y);
  236. input_report_abs(input_dev, ABS_PRESSURE, z);
  237. input_report_key(input_dev, BTN_TOUCH, 1);
  238. input_sync(input_dev);
  239. }
  240. }
  241. irqclr |= IRQENB_FIFO1THRES;
  242. }
  243. /*
  244. * Time for sequencer to settle, to read
  245. * correct state of the sequencer.
  246. */
  247. udelay(SEQ_SETTLE);
  248. status = tscadc_readl(ts_dev, REG_RAWIRQSTATUS);
  249. if (status & IRQENB_PENUP) {
  250. /* Pen up event */
  251. fsm = tscadc_readl(ts_dev, REG_ADCFSM);
  252. if (fsm == ADCFSM_STEPID) {
  253. ts_dev->pen_down = false;
  254. input_report_key(input_dev, BTN_TOUCH, 0);
  255. input_report_abs(input_dev, ABS_PRESSURE, 0);
  256. input_sync(input_dev);
  257. } else {
  258. ts_dev->pen_down = true;
  259. }
  260. irqclr |= IRQENB_PENUP;
  261. }
  262. tscadc_writel(ts_dev, REG_IRQSTATUS, irqclr);
  263. /* check pending interrupts */
  264. tscadc_writel(ts_dev, REG_IRQEOI, 0x0);
  265. tscadc_writel(ts_dev, REG_SE, STPENB_STEPENB);
  266. return IRQ_HANDLED;
  267. }
  268. /*
  269. * The functions for inserting/removing driver as a module.
  270. */
  271. static int __devinit tscadc_probe(struct platform_device *pdev)
  272. {
  273. const struct tsc_data *pdata = pdev->dev.platform_data;
  274. struct resource *res;
  275. struct tscadc *ts_dev;
  276. struct input_dev *input_dev;
  277. struct clk *clk;
  278. int err;
  279. int clk_value, ctrl, irq;
  280. if (!pdata) {
  281. dev_err(&pdev->dev, "missing platform data.\n");
  282. return -EINVAL;
  283. }
  284. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  285. if (!res) {
  286. dev_err(&pdev->dev, "no memory resource defined.\n");
  287. return -EINVAL;
  288. }
  289. irq = platform_get_irq(pdev, 0);
  290. if (irq < 0) {
  291. dev_err(&pdev->dev, "no irq ID is specified.\n");
  292. return -EINVAL;
  293. }
  294. /* Allocate memory for device */
  295. ts_dev = kzalloc(sizeof(struct tscadc), GFP_KERNEL);
  296. input_dev = input_allocate_device();
  297. if (!ts_dev || !input_dev) {
  298. dev_err(&pdev->dev, "failed to allocate memory.\n");
  299. err = -ENOMEM;
  300. goto err_free_mem;
  301. }
  302. ts_dev->input = input_dev;
  303. ts_dev->irq = irq;
  304. ts_dev->wires = pdata->wires;
  305. ts_dev->x_plate_resistance = pdata->x_plate_resistance;
  306. res = request_mem_region(res->start, resource_size(res), pdev->name);
  307. if (!res) {
  308. dev_err(&pdev->dev, "failed to reserve registers.\n");
  309. err = -EBUSY;
  310. goto err_free_mem;
  311. }
  312. ts_dev->tsc_base = ioremap(res->start, resource_size(res));
  313. if (!ts_dev->tsc_base) {
  314. dev_err(&pdev->dev, "failed to map registers.\n");
  315. err = -ENOMEM;
  316. goto err_release_mem_region;
  317. }
  318. err = request_irq(ts_dev->irq, tscadc_irq,
  319. 0, pdev->dev.driver->name, ts_dev);
  320. if (err) {
  321. dev_err(&pdev->dev, "failed to allocate irq.\n");
  322. goto err_unmap_regs;
  323. }
  324. ts_dev->tsc_ick = clk_get(&pdev->dev, "adc_tsc_ick");
  325. if (IS_ERR(ts_dev->tsc_ick)) {
  326. dev_err(&pdev->dev, "failed to get TSC ick\n");
  327. goto err_free_irq;
  328. }
  329. clk_enable(ts_dev->tsc_ick);
  330. clk = clk_get(&pdev->dev, "adc_tsc_fck");
  331. if (IS_ERR(clk)) {
  332. dev_err(&pdev->dev, "failed to get TSC fck\n");
  333. err = PTR_ERR(clk);
  334. goto err_disable_clk;
  335. }
  336. clk_value = clk_get_rate(clk) / ADC_CLK;
  337. clk_put(clk);
  338. if (clk_value < 7) {
  339. dev_err(&pdev->dev, "clock input less than min clock requirement\n");
  340. goto err_disable_clk;
  341. }
  342. /* CLKDIV needs to be configured to the value minus 1 */
  343. tscadc_writel(ts_dev, REG_CLKDIV, clk_value - 1);
  344. /* Enable wake-up of the SoC using touchscreen */
  345. tscadc_writel(ts_dev, REG_IRQWAKEUP, IRQWKUP_ENB);
  346. ctrl = CNTRLREG_STEPCONFIGWRT |
  347. CNTRLREG_TSCENB |
  348. CNTRLREG_STEPID;
  349. switch (ts_dev->wires) {
  350. case 4:
  351. ctrl |= CNTRLREG_4WIRE;
  352. break;
  353. case 5:
  354. ctrl |= CNTRLREG_5WIRE;
  355. break;
  356. case 8:
  357. ctrl |= CNTRLREG_8WIRE;
  358. break;
  359. }
  360. tscadc_writel(ts_dev, REG_CTRL, ctrl);
  361. tscadc_idle_config(ts_dev);
  362. tscadc_writel(ts_dev, REG_IRQENABLE, IRQENB_FIFO1THRES);
  363. tscadc_step_config(ts_dev);
  364. tscadc_writel(ts_dev, REG_FIFO1THR, 6);
  365. ctrl |= CNTRLREG_TSCSSENB;
  366. tscadc_writel(ts_dev, REG_CTRL, ctrl);
  367. input_dev->name = "ti-tsc-adc";
  368. input_dev->dev.parent = &pdev->dev;
  369. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  370. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  371. input_set_abs_params(input_dev, ABS_X, 0, MAX_12BIT, 0, 0);
  372. input_set_abs_params(input_dev, ABS_Y, 0, MAX_12BIT, 0, 0);
  373. input_set_abs_params(input_dev, ABS_PRESSURE, 0, MAX_12BIT, 0, 0);
  374. /* register to the input system */
  375. err = input_register_device(input_dev);
  376. if (err)
  377. goto err_disable_clk;
  378. platform_set_drvdata(pdev, ts_dev);
  379. return 0;
  380. err_disable_clk:
  381. clk_disable(ts_dev->tsc_ick);
  382. clk_put(ts_dev->tsc_ick);
  383. err_free_irq:
  384. free_irq(ts_dev->irq, ts_dev);
  385. err_unmap_regs:
  386. iounmap(ts_dev->tsc_base);
  387. err_release_mem_region:
  388. release_mem_region(res->start, resource_size(res));
  389. err_free_mem:
  390. input_free_device(input_dev);
  391. kfree(ts_dev);
  392. return err;
  393. }
  394. static int __devexit tscadc_remove(struct platform_device *pdev)
  395. {
  396. struct tscadc *ts_dev = platform_get_drvdata(pdev);
  397. struct resource *res;
  398. free_irq(ts_dev->irq, ts_dev);
  399. input_unregister_device(ts_dev->input);
  400. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  401. iounmap(ts_dev->tsc_base);
  402. release_mem_region(res->start, resource_size(res));
  403. clk_disable(ts_dev->tsc_ick);
  404. clk_put(ts_dev->tsc_ick);
  405. kfree(ts_dev);
  406. platform_set_drvdata(pdev, NULL);
  407. return 0;
  408. }
  409. static struct platform_driver ti_tsc_driver = {
  410. .probe = tscadc_probe,
  411. .remove = __devexit_p(tscadc_remove),
  412. .driver = {
  413. .name = "tsc",
  414. .owner = THIS_MODULE,
  415. },
  416. };
  417. module_platform_driver(ti_tsc_driver);
  418. MODULE_DESCRIPTION("TI touchscreen controller driver");
  419. MODULE_AUTHOR("Rachna Patil <rachna@ti.com>");
  420. MODULE_LICENSE("GPL");