tnetv107x-ts.c 9.4 KB

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