w90p910_ts.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Copyright (c) 2008 Nuvoton technology corporation.
  3. *
  4. * Wan ZongShun <mcuos.com@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation;version 2 of the License.
  9. *
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/io.h>
  15. #include <linux/clk.h>
  16. #include <linux/input.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/slab.h>
  19. /* ADC controller bit defines */
  20. #define ADC_DELAY 0xf00
  21. #define ADC_DOWN 0x01
  22. #define ADC_TSC_Y (0x01 << 8)
  23. #define ADC_TSC_X (0x00 << 8)
  24. #define TSC_FOURWIRE (~(0x03 << 1))
  25. #define ADC_CLK_EN (0x01 << 28) /* ADC clock enable */
  26. #define ADC_READ_CON (0x01 << 12)
  27. #define ADC_CONV (0x01 << 13)
  28. #define ADC_SEMIAUTO (0x01 << 14)
  29. #define ADC_WAITTRIG (0x03 << 14)
  30. #define ADC_RST1 (0x01 << 16)
  31. #define ADC_RST0 (0x00 << 16)
  32. #define ADC_EN (0x01 << 17)
  33. #define ADC_INT (0x01 << 18)
  34. #define WT_INT (0x01 << 20)
  35. #define ADC_INT_EN (0x01 << 21)
  36. #define LVD_INT_EN (0x01 << 22)
  37. #define WT_INT_EN (0x01 << 23)
  38. #define ADC_DIV (0x04 << 1) /* div = 6 */
  39. enum ts_state {
  40. TS_WAIT_NEW_PACKET, /* We are waiting next touch report */
  41. TS_WAIT_X_COORD, /* We are waiting for ADC to report X coord */
  42. TS_WAIT_Y_COORD, /* We are waiting for ADC to report Y coord */
  43. TS_IDLE, /* Input device is closed, don't do anything */
  44. };
  45. struct w90p910_ts {
  46. struct input_dev *input;
  47. struct timer_list timer;
  48. struct clk *clk;
  49. int irq_num;
  50. void __iomem *ts_reg;
  51. spinlock_t lock;
  52. enum ts_state state;
  53. };
  54. static void w90p910_report_event(struct w90p910_ts *w90p910_ts, bool down)
  55. {
  56. struct input_dev *dev = w90p910_ts->input;
  57. if (down) {
  58. input_report_abs(dev, ABS_X,
  59. __raw_readl(w90p910_ts->ts_reg + 0x0c));
  60. input_report_abs(dev, ABS_Y,
  61. __raw_readl(w90p910_ts->ts_reg + 0x10));
  62. }
  63. input_report_key(dev, BTN_TOUCH, down);
  64. input_sync(dev);
  65. }
  66. static void w90p910_prepare_x_reading(struct w90p910_ts *w90p910_ts)
  67. {
  68. unsigned long ctlreg;
  69. __raw_writel(ADC_TSC_X, w90p910_ts->ts_reg + 0x04);
  70. ctlreg = __raw_readl(w90p910_ts->ts_reg);
  71. ctlreg &= ~(ADC_WAITTRIG | WT_INT | WT_INT_EN);
  72. ctlreg |= ADC_SEMIAUTO | ADC_INT_EN | ADC_CONV;
  73. __raw_writel(ctlreg, w90p910_ts->ts_reg);
  74. w90p910_ts->state = TS_WAIT_X_COORD;
  75. }
  76. static void w90p910_prepare_y_reading(struct w90p910_ts *w90p910_ts)
  77. {
  78. unsigned long ctlreg;
  79. __raw_writel(ADC_TSC_Y, w90p910_ts->ts_reg + 0x04);
  80. ctlreg = __raw_readl(w90p910_ts->ts_reg);
  81. ctlreg &= ~(ADC_WAITTRIG | ADC_INT | WT_INT_EN);
  82. ctlreg |= ADC_SEMIAUTO | ADC_INT_EN | ADC_CONV;
  83. __raw_writel(ctlreg, w90p910_ts->ts_reg);
  84. w90p910_ts->state = TS_WAIT_Y_COORD;
  85. }
  86. static void w90p910_prepare_next_packet(struct w90p910_ts *w90p910_ts)
  87. {
  88. unsigned long ctlreg;
  89. ctlreg = __raw_readl(w90p910_ts->ts_reg);
  90. ctlreg &= ~(ADC_INT | ADC_INT_EN | ADC_SEMIAUTO | ADC_CONV);
  91. ctlreg |= ADC_WAITTRIG | WT_INT_EN;
  92. __raw_writel(ctlreg, w90p910_ts->ts_reg);
  93. w90p910_ts->state = TS_WAIT_NEW_PACKET;
  94. }
  95. static irqreturn_t w90p910_ts_interrupt(int irq, void *dev_id)
  96. {
  97. struct w90p910_ts *w90p910_ts = dev_id;
  98. unsigned long flags;
  99. spin_lock_irqsave(&w90p910_ts->lock, flags);
  100. switch (w90p910_ts->state) {
  101. case TS_WAIT_NEW_PACKET:
  102. /*
  103. * The controller only generates interrupts when pen
  104. * is down.
  105. */
  106. del_timer(&w90p910_ts->timer);
  107. w90p910_prepare_x_reading(w90p910_ts);
  108. break;
  109. case TS_WAIT_X_COORD:
  110. w90p910_prepare_y_reading(w90p910_ts);
  111. break;
  112. case TS_WAIT_Y_COORD:
  113. w90p910_report_event(w90p910_ts, true);
  114. w90p910_prepare_next_packet(w90p910_ts);
  115. mod_timer(&w90p910_ts->timer, jiffies + msecs_to_jiffies(100));
  116. break;
  117. case TS_IDLE:
  118. break;
  119. }
  120. spin_unlock_irqrestore(&w90p910_ts->lock, flags);
  121. return IRQ_HANDLED;
  122. }
  123. static void w90p910_check_pen_up(unsigned long data)
  124. {
  125. struct w90p910_ts *w90p910_ts = (struct w90p910_ts *) data;
  126. unsigned long flags;
  127. spin_lock_irqsave(&w90p910_ts->lock, flags);
  128. if (w90p910_ts->state == TS_WAIT_NEW_PACKET &&
  129. !(__raw_readl(w90p910_ts->ts_reg + 0x04) & ADC_DOWN)) {
  130. w90p910_report_event(w90p910_ts, false);
  131. }
  132. spin_unlock_irqrestore(&w90p910_ts->lock, flags);
  133. }
  134. static int w90p910_open(struct input_dev *dev)
  135. {
  136. struct w90p910_ts *w90p910_ts = input_get_drvdata(dev);
  137. unsigned long val;
  138. /* enable the ADC clock */
  139. clk_enable(w90p910_ts->clk);
  140. __raw_writel(ADC_RST1, w90p910_ts->ts_reg);
  141. msleep(1);
  142. __raw_writel(ADC_RST0, w90p910_ts->ts_reg);
  143. msleep(1);
  144. /* set delay and screen type */
  145. val = __raw_readl(w90p910_ts->ts_reg + 0x04);
  146. __raw_writel(val & TSC_FOURWIRE, w90p910_ts->ts_reg + 0x04);
  147. __raw_writel(ADC_DELAY, w90p910_ts->ts_reg + 0x08);
  148. w90p910_ts->state = TS_WAIT_NEW_PACKET;
  149. wmb();
  150. /* set trigger mode */
  151. val = __raw_readl(w90p910_ts->ts_reg);
  152. val |= ADC_WAITTRIG | ADC_DIV | ADC_EN | WT_INT_EN;
  153. __raw_writel(val, w90p910_ts->ts_reg);
  154. return 0;
  155. }
  156. static void w90p910_close(struct input_dev *dev)
  157. {
  158. struct w90p910_ts *w90p910_ts = input_get_drvdata(dev);
  159. unsigned long val;
  160. /* disable trigger mode */
  161. spin_lock_irq(&w90p910_ts->lock);
  162. w90p910_ts->state = TS_IDLE;
  163. val = __raw_readl(w90p910_ts->ts_reg);
  164. val &= ~(ADC_WAITTRIG | ADC_DIV | ADC_EN | WT_INT_EN | ADC_INT_EN);
  165. __raw_writel(val, w90p910_ts->ts_reg);
  166. spin_unlock_irq(&w90p910_ts->lock);
  167. /* Now that interrupts are shut off we can safely delete timer */
  168. del_timer_sync(&w90p910_ts->timer);
  169. /* stop the ADC clock */
  170. clk_disable(w90p910_ts->clk);
  171. }
  172. static int w90x900ts_probe(struct platform_device *pdev)
  173. {
  174. struct w90p910_ts *w90p910_ts;
  175. struct input_dev *input_dev;
  176. struct resource *res;
  177. int err;
  178. w90p910_ts = kzalloc(sizeof(struct w90p910_ts), GFP_KERNEL);
  179. input_dev = input_allocate_device();
  180. if (!w90p910_ts || !input_dev) {
  181. err = -ENOMEM;
  182. goto fail1;
  183. }
  184. w90p910_ts->input = input_dev;
  185. w90p910_ts->state = TS_IDLE;
  186. spin_lock_init(&w90p910_ts->lock);
  187. setup_timer(&w90p910_ts->timer, w90p910_check_pen_up,
  188. (unsigned long)w90p910_ts);
  189. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  190. if (!res) {
  191. err = -ENXIO;
  192. goto fail1;
  193. }
  194. if (!request_mem_region(res->start, resource_size(res),
  195. pdev->name)) {
  196. err = -EBUSY;
  197. goto fail1;
  198. }
  199. w90p910_ts->ts_reg = ioremap(res->start, resource_size(res));
  200. if (!w90p910_ts->ts_reg) {
  201. err = -ENOMEM;
  202. goto fail2;
  203. }
  204. w90p910_ts->clk = clk_get(&pdev->dev, NULL);
  205. if (IS_ERR(w90p910_ts->clk)) {
  206. err = PTR_ERR(w90p910_ts->clk);
  207. goto fail3;
  208. }
  209. input_dev->name = "W90P910 TouchScreen";
  210. input_dev->phys = "w90p910ts/event0";
  211. input_dev->id.bustype = BUS_HOST;
  212. input_dev->id.vendor = 0x0005;
  213. input_dev->id.product = 0x0001;
  214. input_dev->id.version = 0x0100;
  215. input_dev->dev.parent = &pdev->dev;
  216. input_dev->open = w90p910_open;
  217. input_dev->close = w90p910_close;
  218. input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
  219. input_dev->keybit[BIT_WORD(BTN_TOUCH)] = BIT_MASK(BTN_TOUCH);
  220. input_set_abs_params(input_dev, ABS_X, 0, 0x400, 0, 0);
  221. input_set_abs_params(input_dev, ABS_Y, 0, 0x400, 0, 0);
  222. input_set_drvdata(input_dev, w90p910_ts);
  223. w90p910_ts->irq_num = platform_get_irq(pdev, 0);
  224. if (request_irq(w90p910_ts->irq_num, w90p910_ts_interrupt,
  225. 0, "w90p910ts", w90p910_ts)) {
  226. err = -EBUSY;
  227. goto fail4;
  228. }
  229. err = input_register_device(w90p910_ts->input);
  230. if (err)
  231. goto fail5;
  232. platform_set_drvdata(pdev, w90p910_ts);
  233. return 0;
  234. fail5: free_irq(w90p910_ts->irq_num, w90p910_ts);
  235. fail4: clk_put(w90p910_ts->clk);
  236. fail3: iounmap(w90p910_ts->ts_reg);
  237. fail2: release_mem_region(res->start, resource_size(res));
  238. fail1: input_free_device(input_dev);
  239. kfree(w90p910_ts);
  240. return err;
  241. }
  242. static int w90x900ts_remove(struct platform_device *pdev)
  243. {
  244. struct w90p910_ts *w90p910_ts = platform_get_drvdata(pdev);
  245. struct resource *res;
  246. free_irq(w90p910_ts->irq_num, w90p910_ts);
  247. del_timer_sync(&w90p910_ts->timer);
  248. iounmap(w90p910_ts->ts_reg);
  249. clk_put(w90p910_ts->clk);
  250. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  251. release_mem_region(res->start, resource_size(res));
  252. input_unregister_device(w90p910_ts->input);
  253. kfree(w90p910_ts);
  254. return 0;
  255. }
  256. static struct platform_driver w90x900ts_driver = {
  257. .probe = w90x900ts_probe,
  258. .remove = w90x900ts_remove,
  259. .driver = {
  260. .name = "nuc900-ts",
  261. },
  262. };
  263. module_platform_driver(w90x900ts_driver);
  264. MODULE_AUTHOR("Wan ZongShun <mcuos.com@gmail.com>");
  265. MODULE_DESCRIPTION("w90p910 touch screen driver!");
  266. MODULE_LICENSE("GPL");
  267. MODULE_ALIAS("platform:nuc900-ts");