tnetv107x-ts.c 9.2 KB

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