mainstone-wm97xx.c 7.1 KB

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