mainstone-wm97xx.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * mainstone-wm97xx.c -- Mainstone Continuous Touch screen driver for
  3. * Wolfson WM97xx AC97 Codecs.
  4. *
  5. * Copyright 2004, 2007 Wolfson Microelectronics PLC.
  6. * Author: Liam Girdwood <lrg@slimlogic.co.uk>
  7. * Parts Copyright : Ian Molton <spyro@f2s.com>
  8. * Andrew Zabolotny <zap@homelink.ru>
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2 of the License, or (at your
  13. * option) any later version.
  14. *
  15. * Notes:
  16. * This is a wm97xx extended touch driver to capture touch
  17. * data in a continuous manner on the Intel XScale architecture
  18. *
  19. * Features:
  20. * - codecs supported:- WM9705, WM9712, WM9713
  21. * - processors supported:- Intel XScale PXA25x, PXA26x, PXA27x
  22. *
  23. */
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/kernel.h>
  27. #include <linux/delay.h>
  28. #include <linux/irq.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/wm97xx.h>
  31. #include <linux/io.h>
  32. #include <linux/gpio.h>
  33. #include <mach/regs-ac97.h>
  34. #include <asm/mach-types.h>
  35. struct continuous {
  36. u16 id; /* codec id */
  37. u8 code; /* continuous code */
  38. u8 reads; /* number of coord reads per read cycle */
  39. u32 speed; /* number of coords per second */
  40. };
  41. #define WM_READS(sp) ((sp / HZ) + 1)
  42. static const struct continuous cinfo[] = {
  43. {WM9705_ID2, 0, WM_READS(94), 94},
  44. {WM9705_ID2, 1, WM_READS(188), 188},
  45. {WM9705_ID2, 2, WM_READS(375), 375},
  46. {WM9705_ID2, 3, WM_READS(750), 750},
  47. {WM9712_ID2, 0, WM_READS(94), 94},
  48. {WM9712_ID2, 1, WM_READS(188), 188},
  49. {WM9712_ID2, 2, WM_READS(375), 375},
  50. {WM9712_ID2, 3, WM_READS(750), 750},
  51. {WM9713_ID2, 0, WM_READS(94), 94},
  52. {WM9713_ID2, 1, WM_READS(120), 120},
  53. {WM9713_ID2, 2, WM_READS(154), 154},
  54. {WM9713_ID2, 3, WM_READS(188), 188},
  55. };
  56. /* continuous speed index */
  57. static int sp_idx;
  58. static u16 last, tries;
  59. static int irq;
  60. /*
  61. * Pen sampling frequency (Hz) in continuous mode.
  62. */
  63. static int cont_rate = 200;
  64. module_param(cont_rate, int, 0);
  65. MODULE_PARM_DESC(cont_rate, "Sampling rate in continuous mode (Hz)");
  66. /*
  67. * Pen down detection.
  68. *
  69. * This driver can either poll or use an interrupt to indicate a pen down
  70. * event. If the irq request fails then it will fall back to polling mode.
  71. */
  72. static int pen_int;
  73. module_param(pen_int, int, 0);
  74. MODULE_PARM_DESC(pen_int, "Pen down detection (1 = interrupt, 0 = polling)");
  75. /*
  76. * Pressure readback.
  77. *
  78. * Set to 1 to read back pen down pressure
  79. */
  80. static int pressure;
  81. module_param(pressure, int, 0);
  82. MODULE_PARM_DESC(pressure, "Pressure readback (1 = pressure, 0 = no pressure)");
  83. /*
  84. * AC97 touch data slot.
  85. *
  86. * Touch screen readback data ac97 slot
  87. */
  88. static int ac97_touch_slot = 5;
  89. module_param(ac97_touch_slot, int, 0);
  90. MODULE_PARM_DESC(ac97_touch_slot, "Touch screen data slot AC97 number");
  91. /* flush AC97 slot 5 FIFO on pxa machines */
  92. #ifdef CONFIG_PXA27x
  93. static void wm97xx_acc_pen_up(struct wm97xx *wm)
  94. {
  95. schedule_timeout_uninterruptible(1);
  96. while (MISR & (1 << 2))
  97. MODR;
  98. }
  99. #else
  100. static void wm97xx_acc_pen_up(struct wm97xx *wm)
  101. {
  102. unsigned int count;
  103. schedule_timeout_uninterruptible(1);
  104. for (count = 0; count < 16; count++)
  105. MODR;
  106. }
  107. #endif
  108. static int wm97xx_acc_pen_down(struct wm97xx *wm)
  109. {
  110. u16 x, y, p = 0x100 | WM97XX_ADCSEL_PRES;
  111. int reads = 0;
  112. /* When the AC97 queue has been drained we need to allow time
  113. * to buffer up samples otherwise we end up spinning polling
  114. * for samples. The controller can't have a suitably low
  115. * threshold set to use the notifications it gives.
  116. */
  117. schedule_timeout_uninterruptible(1);
  118. if (tries > 5) {
  119. tries = 0;
  120. return RC_PENUP;
  121. }
  122. x = MODR;
  123. if (x == last) {
  124. tries++;
  125. return RC_AGAIN;
  126. }
  127. last = x;
  128. do {
  129. if (reads)
  130. x = MODR;
  131. y = MODR;
  132. if (pressure)
  133. p = MODR;
  134. dev_dbg(wm->dev, "Raw coordinates: x=%x, y=%x, p=%x\n",
  135. x, y, p);
  136. /* are samples valid */
  137. if ((x & WM97XX_ADCSEL_MASK) != WM97XX_ADCSEL_X ||
  138. (y & WM97XX_ADCSEL_MASK) != WM97XX_ADCSEL_Y ||
  139. (p & WM97XX_ADCSEL_MASK) != WM97XX_ADCSEL_PRES)
  140. goto up;
  141. /* coordinate is good */
  142. tries = 0;
  143. input_report_abs(wm->input_dev, ABS_X, x & 0xfff);
  144. input_report_abs(wm->input_dev, ABS_Y, y & 0xfff);
  145. input_report_abs(wm->input_dev, ABS_PRESSURE, p & 0xfff);
  146. input_report_key(wm->input_dev, BTN_TOUCH, (p != 0));
  147. input_sync(wm->input_dev);
  148. reads++;
  149. } while (reads < cinfo[sp_idx].reads);
  150. up:
  151. return RC_PENDOWN | RC_AGAIN;
  152. }
  153. static int wm97xx_acc_startup(struct wm97xx *wm)
  154. {
  155. int idx = 0, ret = 0;
  156. /* check we have a codec */
  157. if (wm->ac97 == NULL)
  158. return -ENODEV;
  159. /* Go you big red fire engine */
  160. for (idx = 0; idx < ARRAY_SIZE(cinfo); idx++) {
  161. if (wm->id != cinfo[idx].id)
  162. continue;
  163. sp_idx = idx;
  164. if (cont_rate <= cinfo[idx].speed)
  165. break;
  166. }
  167. wm->acc_rate = cinfo[sp_idx].code;
  168. wm->acc_slot = ac97_touch_slot;
  169. dev_info(wm->dev,
  170. "mainstone accelerated touchscreen driver, %d samples/sec\n",
  171. cinfo[sp_idx].speed);
  172. /* IRQ driven touchscreen is used on Palm hardware */
  173. if (machine_is_palmt5() || machine_is_palmtx() || machine_is_palmld()) {
  174. pen_int = 1;
  175. irq = 27;
  176. /* There is some obscure mutant of WM9712 interbred with WM9713
  177. * used on Palm HW */
  178. wm->variant = WM97xx_WM1613;
  179. } else if (machine_is_mainstone() && pen_int)
  180. irq = 4;
  181. if (irq) {
  182. ret = gpio_request(irq, "Touchscreen IRQ");
  183. if (ret)
  184. goto out;
  185. ret = gpio_direction_input(irq);
  186. if (ret) {
  187. gpio_free(irq);
  188. goto out;
  189. }
  190. wm->pen_irq = gpio_to_irq(irq);
  191. irq_set_irq_type(wm->pen_irq, IRQ_TYPE_EDGE_BOTH);
  192. } else /* pen irq not supported */
  193. pen_int = 0;
  194. /* codec specific irq config */
  195. if (pen_int) {
  196. switch (wm->id) {
  197. case WM9705_ID2:
  198. break;
  199. case WM9712_ID2:
  200. case WM9713_ID2:
  201. /* use PEN_DOWN GPIO 13 to assert IRQ on GPIO line 2 */
  202. wm97xx_config_gpio(wm, WM97XX_GPIO_13, WM97XX_GPIO_IN,
  203. WM97XX_GPIO_POL_HIGH,
  204. WM97XX_GPIO_STICKY,
  205. WM97XX_GPIO_WAKE);
  206. wm97xx_config_gpio(wm, WM97XX_GPIO_2, WM97XX_GPIO_OUT,
  207. WM97XX_GPIO_POL_HIGH,
  208. WM97XX_GPIO_NOTSTICKY,
  209. WM97XX_GPIO_NOWAKE);
  210. break;
  211. default:
  212. dev_err(wm->dev,
  213. "pen down irq not supported on this device\n");
  214. pen_int = 0;
  215. break;
  216. }
  217. }
  218. out:
  219. return ret;
  220. }
  221. static void wm97xx_acc_shutdown(struct wm97xx *wm)
  222. {
  223. /* codec specific deconfig */
  224. if (pen_int) {
  225. if (irq)
  226. gpio_free(irq);
  227. wm->pen_irq = 0;
  228. }
  229. }
  230. static void wm97xx_irq_enable(struct wm97xx *wm, int enable)
  231. {
  232. if (enable)
  233. enable_irq(wm->pen_irq);
  234. else
  235. disable_irq_nosync(wm->pen_irq);
  236. }
  237. static struct wm97xx_mach_ops mainstone_mach_ops = {
  238. .acc_enabled = 1,
  239. .acc_pen_up = wm97xx_acc_pen_up,
  240. .acc_pen_down = wm97xx_acc_pen_down,
  241. .acc_startup = wm97xx_acc_startup,
  242. .acc_shutdown = wm97xx_acc_shutdown,
  243. .irq_enable = wm97xx_irq_enable,
  244. .irq_gpio = WM97XX_GPIO_2,
  245. };
  246. static int mainstone_wm97xx_probe(struct platform_device *pdev)
  247. {
  248. struct wm97xx *wm = platform_get_drvdata(pdev);
  249. return wm97xx_register_mach_ops(wm, &mainstone_mach_ops);
  250. }
  251. static int mainstone_wm97xx_remove(struct platform_device *pdev)
  252. {
  253. struct wm97xx *wm = platform_get_drvdata(pdev);
  254. wm97xx_unregister_mach_ops(wm);
  255. return 0;
  256. }
  257. static struct platform_driver mainstone_wm97xx_driver = {
  258. .probe = mainstone_wm97xx_probe,
  259. .remove = mainstone_wm97xx_remove,
  260. .driver = {
  261. .name = "wm97xx-touch",
  262. },
  263. };
  264. module_platform_driver(mainstone_wm97xx_driver);
  265. /* Module information */
  266. MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
  267. MODULE_DESCRIPTION("wm97xx continuous touch driver for mainstone");
  268. MODULE_LICENSE("GPL");