mainstone-wm97xx.c 7.3 KB

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