tnetv107x-ts.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * Texas Instruments TNETV107X Touchscreen Driver
  3. *
  4. * Copyright (C) 2010 Texas Instruments
  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/errno.h>
  17. #include <linux/input.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/slab.h>
  21. #include <linux/delay.h>
  22. #include <linux/ctype.h>
  23. #include <linux/io.h>
  24. #include <linux/clk.h>
  25. #include <mach/tnetv107x.h>
  26. #define TSC_PENUP_POLL (HZ / 5)
  27. #define IDLE_TIMEOUT 100 /* msec */
  28. /*
  29. * The first and last samples of a touch interval are usually garbage and need
  30. * to be filtered out with these devices. The following definitions control
  31. * the number of samples skipped.
  32. */
  33. #define TSC_HEAD_SKIP 1
  34. #define TSC_TAIL_SKIP 1
  35. #define TSC_SKIP (TSC_HEAD_SKIP + TSC_TAIL_SKIP + 1)
  36. #define TSC_SAMPLES (TSC_SKIP + 1)
  37. /* Register Offsets */
  38. struct tsc_regs {
  39. u32 rev;
  40. u32 tscm;
  41. u32 bwcm;
  42. u32 swc;
  43. u32 adcchnl;
  44. u32 adcdata;
  45. u32 chval[4];
  46. };
  47. /* TSC Mode Configuration Register (tscm) bits */
  48. #define WMODE BIT(0)
  49. #define TSKIND BIT(1)
  50. #define ZMEASURE_EN BIT(2)
  51. #define IDLE BIT(3)
  52. #define TSC_EN BIT(4)
  53. #define STOP BIT(5)
  54. #define ONE_SHOT BIT(6)
  55. #define SINGLE BIT(7)
  56. #define AVG BIT(8)
  57. #define AVGNUM(x) (((x) & 0x03) << 9)
  58. #define PVSTC(x) (((x) & 0x07) << 11)
  59. #define PON BIT(14)
  60. #define PONBG BIT(15)
  61. #define AFERST BIT(16)
  62. /* ADC DATA Capture Register bits */
  63. #define DATA_VALID BIT(16)
  64. /* Register Access Macros */
  65. #define tsc_read(ts, reg) __raw_readl(&(ts)->regs->reg)
  66. #define tsc_write(ts, reg, val) __raw_writel(val, &(ts)->regs->reg);
  67. #define tsc_set_bits(ts, reg, val) \
  68. tsc_write(ts, reg, tsc_read(ts, reg) | (val))
  69. #define tsc_clr_bits(ts, reg, val) \
  70. tsc_write(ts, reg, tsc_read(ts, reg) & ~(val))
  71. struct sample {
  72. int x, y, p;
  73. };
  74. struct tsc_data {
  75. struct input_dev *input_dev;
  76. struct resource *res;
  77. struct tsc_regs __iomem *regs;
  78. struct timer_list timer;
  79. spinlock_t lock;
  80. struct clk *clk;
  81. struct device *dev;
  82. int sample_count;
  83. struct sample samples[TSC_SAMPLES];
  84. int tsc_irq;
  85. };
  86. static int tsc_read_sample(struct tsc_data *ts, struct sample* sample)
  87. {
  88. int x, y, z1, z2, t, p = 0;
  89. u32 val;
  90. val = tsc_read(ts, chval[0]);
  91. if (val & DATA_VALID)
  92. x = val & 0xffff;
  93. else
  94. return -EINVAL;
  95. y = tsc_read(ts, chval[1]) & 0xffff;
  96. z1 = tsc_read(ts, chval[2]) & 0xffff;
  97. z2 = tsc_read(ts, chval[3]) & 0xffff;
  98. if (z1) {
  99. t = ((600 * x) * (z2 - z1));
  100. p = t / (u32) (z1 << 12);
  101. if (p < 0)
  102. p = 0;
  103. }
  104. sample->x = x;
  105. sample->y = y;
  106. sample->p = p;
  107. return 0;
  108. }
  109. static void tsc_poll(unsigned long data)
  110. {
  111. struct tsc_data *ts = (struct tsc_data *)data;
  112. unsigned long flags;
  113. int i, val, x, y, p;
  114. spin_lock_irqsave(&ts->lock, flags);
  115. if (ts->sample_count >= TSC_SKIP) {
  116. input_report_abs(ts->input_dev, ABS_PRESSURE, 0);
  117. input_report_key(ts->input_dev, BTN_TOUCH, 0);
  118. input_sync(ts->input_dev);
  119. } else if (ts->sample_count > 0) {
  120. /*
  121. * A touch event lasted less than our skip count. Salvage and
  122. * report anyway.
  123. */
  124. for (i = 0, val = 0; i < ts->sample_count; i++)
  125. val += ts->samples[i].x;
  126. x = val / ts->sample_count;
  127. for (i = 0, val = 0; i < ts->sample_count; i++)
  128. val += ts->samples[i].y;
  129. y = val / ts->sample_count;
  130. for (i = 0, val = 0; i < ts->sample_count; i++)
  131. val += ts->samples[i].p;
  132. p = val / ts->sample_count;
  133. input_report_abs(ts->input_dev, ABS_X, x);
  134. input_report_abs(ts->input_dev, ABS_Y, y);
  135. input_report_abs(ts->input_dev, ABS_PRESSURE, p);
  136. input_report_key(ts->input_dev, BTN_TOUCH, 1);
  137. input_sync(ts->input_dev);
  138. }
  139. ts->sample_count = 0;
  140. spin_unlock_irqrestore(&ts->lock, flags);
  141. }
  142. static irqreturn_t tsc_irq(int irq, void *dev_id)
  143. {
  144. struct tsc_data *ts = (struct tsc_data *)dev_id;
  145. struct sample *sample;
  146. int index;
  147. spin_lock(&ts->lock);
  148. index = ts->sample_count % TSC_SAMPLES;
  149. sample = &ts->samples[index];
  150. if (tsc_read_sample(ts, sample) < 0)
  151. goto out;
  152. if (++ts->sample_count >= TSC_SKIP) {
  153. index = (ts->sample_count - TSC_TAIL_SKIP - 1) % TSC_SAMPLES;
  154. sample = &ts->samples[index];
  155. input_report_abs(ts->input_dev, ABS_X, sample->x);
  156. input_report_abs(ts->input_dev, ABS_Y, sample->y);
  157. input_report_abs(ts->input_dev, ABS_PRESSURE, sample->p);
  158. if (ts->sample_count == TSC_SKIP)
  159. input_report_key(ts->input_dev, BTN_TOUCH, 1);
  160. input_sync(ts->input_dev);
  161. }
  162. mod_timer(&ts->timer, jiffies + TSC_PENUP_POLL);
  163. out:
  164. spin_unlock(&ts->lock);
  165. return IRQ_HANDLED;
  166. }
  167. static int tsc_start(struct input_dev *dev)
  168. {
  169. struct tsc_data *ts = input_get_drvdata(dev);
  170. unsigned long timeout = jiffies + msecs_to_jiffies(IDLE_TIMEOUT);
  171. u32 val;
  172. clk_enable(ts->clk);
  173. /* Go to idle mode, before any initialization */
  174. while (time_after(timeout, jiffies)) {
  175. if (tsc_read(ts, tscm) & IDLE)
  176. break;
  177. }
  178. if (time_before(timeout, jiffies)) {
  179. dev_warn(ts->dev, "timeout waiting for idle\n");
  180. clk_disable(ts->clk);
  181. return -EIO;
  182. }
  183. /* Configure TSC Control register*/
  184. val = (PONBG | PON | PVSTC(4) | ONE_SHOT | ZMEASURE_EN);
  185. tsc_write(ts, tscm, val);
  186. /* Bring TSC out of reset: Clear AFE reset bit */
  187. val &= ~(AFERST);
  188. tsc_write(ts, tscm, val);
  189. /* Configure all pins for hardware control*/
  190. tsc_write(ts, bwcm, 0);
  191. /* Finally enable the TSC */
  192. tsc_set_bits(ts, tscm, TSC_EN);
  193. return 0;
  194. }
  195. static void tsc_stop(struct input_dev *dev)
  196. {
  197. struct tsc_data *ts = input_get_drvdata(dev);
  198. tsc_clr_bits(ts, tscm, TSC_EN);
  199. synchronize_irq(ts->tsc_irq);
  200. del_timer_sync(&ts->timer);
  201. clk_disable(ts->clk);
  202. }
  203. static int __devinit tsc_probe(struct platform_device *pdev)
  204. {
  205. struct device *dev = &pdev->dev;
  206. struct tsc_data *ts;
  207. int error = 0;
  208. u32 rev = 0;
  209. ts = kzalloc(sizeof(struct tsc_data), GFP_KERNEL);
  210. if (!ts) {
  211. dev_err(dev, "cannot allocate device info\n");
  212. return -ENOMEM;
  213. }
  214. ts->dev = dev;
  215. spin_lock_init(&ts->lock);
  216. setup_timer(&ts->timer, tsc_poll, (unsigned long)ts);
  217. platform_set_drvdata(pdev, ts);
  218. ts->tsc_irq = platform_get_irq(pdev, 0);
  219. if (ts->tsc_irq < 0) {
  220. dev_err(dev, "cannot determine device interrupt\n");
  221. error = -ENODEV;
  222. goto error_res;
  223. }
  224. ts->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  225. if (!ts->res) {
  226. dev_err(dev, "cannot determine register area\n");
  227. error = -ENODEV;
  228. goto error_res;
  229. }
  230. if (!request_mem_region(ts->res->start, resource_size(ts->res),
  231. pdev->name)) {
  232. dev_err(dev, "cannot claim register memory\n");
  233. ts->res = NULL;
  234. error = -EINVAL;
  235. goto error_res;
  236. }
  237. ts->regs = ioremap(ts->res->start, resource_size(ts->res));
  238. if (!ts->regs) {
  239. dev_err(dev, "cannot map register memory\n");
  240. error = -ENOMEM;
  241. goto error_map;
  242. }
  243. ts->clk = clk_get(dev, NULL);
  244. if (!ts->clk) {
  245. dev_err(dev, "cannot claim device clock\n");
  246. error = -EINVAL;
  247. goto error_clk;
  248. }
  249. error = request_threaded_irq(ts->tsc_irq, NULL, tsc_irq, 0,
  250. dev_name(dev), ts);
  251. if (error < 0) {
  252. dev_err(ts->dev, "Could not allocate ts irq\n");
  253. goto error_irq;
  254. }
  255. ts->input_dev = input_allocate_device();
  256. if (!ts->input_dev) {
  257. dev_err(dev, "cannot allocate input device\n");
  258. error = -ENOMEM;
  259. goto error_input;
  260. }
  261. input_set_drvdata(ts->input_dev, ts);
  262. ts->input_dev->name = pdev->name;
  263. ts->input_dev->id.bustype = BUS_HOST;
  264. ts->input_dev->dev.parent = &pdev->dev;
  265. ts->input_dev->open = tsc_start;
  266. ts->input_dev->close = tsc_stop;
  267. clk_enable(ts->clk);
  268. rev = tsc_read(ts, rev);
  269. ts->input_dev->id.product = ((rev >> 8) & 0x07);
  270. ts->input_dev->id.version = ((rev >> 16) & 0xfff);
  271. clk_disable(ts->clk);
  272. __set_bit(EV_KEY, ts->input_dev->evbit);
  273. __set_bit(EV_ABS, ts->input_dev->evbit);
  274. __set_bit(BTN_TOUCH, ts->input_dev->keybit);
  275. input_set_abs_params(ts->input_dev, ABS_X, 0, 0xffff, 5, 0);
  276. input_set_abs_params(ts->input_dev, ABS_Y, 0, 0xffff, 5, 0);
  277. input_set_abs_params(ts->input_dev, ABS_PRESSURE, 0, 4095, 128, 0);
  278. error = input_register_device(ts->input_dev);
  279. if (error < 0) {
  280. dev_err(dev, "failed input device registration\n");
  281. goto error_reg;
  282. }
  283. return 0;
  284. error_reg:
  285. input_free_device(ts->input_dev);
  286. error_input:
  287. free_irq(ts->tsc_irq, ts);
  288. error_irq:
  289. clk_put(ts->clk);
  290. error_clk:
  291. iounmap(ts->regs);
  292. error_map:
  293. release_mem_region(ts->res->start, resource_size(ts->res));
  294. error_res:
  295. platform_set_drvdata(pdev, NULL);
  296. kfree(ts);
  297. return error;
  298. }
  299. static int __devexit tsc_remove(struct platform_device *pdev)
  300. {
  301. struct tsc_data *ts = platform_get_drvdata(pdev);
  302. input_unregister_device(ts->input_dev);
  303. free_irq(ts->tsc_irq, ts);
  304. clk_put(ts->clk);
  305. iounmap(ts->regs);
  306. release_mem_region(ts->res->start, resource_size(ts->res));
  307. platform_set_drvdata(pdev, NULL);
  308. kfree(ts);
  309. return 0;
  310. }
  311. static struct platform_driver tsc_driver = {
  312. .probe = tsc_probe,
  313. .remove = __devexit_p(tsc_remove),
  314. .driver.name = "tnetv107x-ts",
  315. .driver.owner = THIS_MODULE,
  316. };
  317. static int __init tsc_init(void)
  318. {
  319. return platform_driver_register(&tsc_driver);
  320. }
  321. static void __exit tsc_exit(void)
  322. {
  323. platform_driver_unregister(&tsc_driver);
  324. }
  325. module_init(tsc_init);
  326. module_exit(tsc_exit);
  327. MODULE_AUTHOR("Cyril Chemparathy");
  328. MODULE_DESCRIPTION("TNETV107X Touchscreen Driver");
  329. MODULE_ALIAS("platform: tnetv107x-ts");
  330. MODULE_LICENSE("GPL");