/drivers/input/touchscreen/mainstone-wm97xx.c

http://github.com/mirrors/linux · C · 304 lines · 208 code · 41 blank · 55 comment · 34 complexity · f9f82b63832de21f664697f0167d555b MD5 · raw file

  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 <lrg@slimlogic.co.uk>
  8. * Parts Copyright : Ian Molton <spyro@f2s.com>
  9. * Andrew Zabolotny <zap@homelink.ru>
  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/irq.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/wm97xx.h>
  26. #include <linux/io.h>
  27. #include <linux/gpio.h>
  28. #include <mach/regs-ac97.h>
  29. #include <asm/mach-types.h>
  30. struct continuous {
  31. u16 id; /* codec id */
  32. u8 code; /* continuous code */
  33. u8 reads; /* number of coord reads per read cycle */
  34. u32 speed; /* number of coords per second */
  35. };
  36. #define WM_READS(sp) ((sp / HZ) + 1)
  37. static const struct continuous cinfo[] = {
  38. {WM9705_ID2, 0, WM_READS(94), 94},
  39. {WM9705_ID2, 1, WM_READS(188), 188},
  40. {WM9705_ID2, 2, WM_READS(375), 375},
  41. {WM9705_ID2, 3, WM_READS(750), 750},
  42. {WM9712_ID2, 0, WM_READS(94), 94},
  43. {WM9712_ID2, 1, WM_READS(188), 188},
  44. {WM9712_ID2, 2, WM_READS(375), 375},
  45. {WM9712_ID2, 3, WM_READS(750), 750},
  46. {WM9713_ID2, 0, WM_READS(94), 94},
  47. {WM9713_ID2, 1, WM_READS(120), 120},
  48. {WM9713_ID2, 2, WM_READS(154), 154},
  49. {WM9713_ID2, 3, WM_READS(188), 188},
  50. };
  51. /* continuous speed index */
  52. static int sp_idx;
  53. static u16 last, tries;
  54. static int 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. #ifdef CONFIG_PXA27x
  88. static void wm97xx_acc_pen_up(struct wm97xx *wm)
  89. {
  90. schedule_timeout_uninterruptible(1);
  91. while (MISR & (1 << 2))
  92. MODR;
  93. }
  94. #else
  95. static void wm97xx_acc_pen_up(struct wm97xx *wm)
  96. {
  97. unsigned int count;
  98. schedule_timeout_uninterruptible(1);
  99. for (count = 0; count < 16; count++)
  100. MODR;
  101. }
  102. #endif
  103. static int wm97xx_acc_pen_down(struct wm97xx *wm)
  104. {
  105. u16 x, y, p = 0x100 | WM97XX_ADCSEL_PRES;
  106. int reads = 0;
  107. /* When the AC97 queue has been drained we need to allow time
  108. * to buffer up samples otherwise we end up spinning polling
  109. * for samples. The controller can't have a suitably low
  110. * threshold set to use the notifications it gives.
  111. */
  112. schedule_timeout_uninterruptible(1);
  113. if (tries > 5) {
  114. tries = 0;
  115. return RC_PENUP;
  116. }
  117. x = MODR;
  118. if (x == last) {
  119. tries++;
  120. return RC_AGAIN;
  121. }
  122. last = x;
  123. do {
  124. if (reads)
  125. x = MODR;
  126. y = MODR;
  127. if (pressure)
  128. p = MODR;
  129. dev_dbg(wm->dev, "Raw coordinates: x=%x, y=%x, p=%x\n",
  130. x, y, p);
  131. /* are samples valid */
  132. if ((x & WM97XX_ADCSEL_MASK) != WM97XX_ADCSEL_X ||
  133. (y & WM97XX_ADCSEL_MASK) != WM97XX_ADCSEL_Y ||
  134. (p & WM97XX_ADCSEL_MASK) != WM97XX_ADCSEL_PRES)
  135. goto up;
  136. /* coordinate is good */
  137. tries = 0;
  138. input_report_abs(wm->input_dev, ABS_X, x & 0xfff);
  139. input_report_abs(wm->input_dev, ABS_Y, y & 0xfff);
  140. input_report_abs(wm->input_dev, ABS_PRESSURE, p & 0xfff);
  141. input_report_key(wm->input_dev, BTN_TOUCH, (p != 0));
  142. input_sync(wm->input_dev);
  143. reads++;
  144. } while (reads < cinfo[sp_idx].reads);
  145. up:
  146. return RC_PENDOWN | RC_AGAIN;
  147. }
  148. static int wm97xx_acc_startup(struct wm97xx *wm)
  149. {
  150. int idx = 0, ret = 0;
  151. /* check we have a codec */
  152. if (wm->ac97 == NULL)
  153. return -ENODEV;
  154. /* Go you big red fire engine */
  155. for (idx = 0; idx < ARRAY_SIZE(cinfo); idx++) {
  156. if (wm->id != cinfo[idx].id)
  157. continue;
  158. sp_idx = idx;
  159. if (cont_rate <= cinfo[idx].speed)
  160. break;
  161. }
  162. wm->acc_rate = cinfo[sp_idx].code;
  163. wm->acc_slot = ac97_touch_slot;
  164. dev_info(wm->dev,
  165. "mainstone accelerated touchscreen driver, %d samples/sec\n",
  166. cinfo[sp_idx].speed);
  167. /* IRQ driven touchscreen is used on Palm hardware */
  168. if (machine_is_palmt5() || machine_is_palmtx() || machine_is_palmld()) {
  169. pen_int = 1;
  170. irq = 27;
  171. /* There is some obscure mutant of WM9712 interbred with WM9713
  172. * used on Palm HW */
  173. wm->variant = WM97xx_WM1613;
  174. } else if (machine_is_mainstone() && pen_int)
  175. irq = 4;
  176. if (irq) {
  177. ret = gpio_request(irq, "Touchscreen IRQ");
  178. if (ret)
  179. goto out;
  180. ret = gpio_direction_input(irq);
  181. if (ret) {
  182. gpio_free(irq);
  183. goto out;
  184. }
  185. wm->pen_irq = gpio_to_irq(irq);
  186. irq_set_irq_type(wm->pen_irq, IRQ_TYPE_EDGE_BOTH);
  187. } else /* pen irq not supported */
  188. pen_int = 0;
  189. /* codec specific irq config */
  190. if (pen_int) {
  191. switch (wm->id) {
  192. case WM9705_ID2:
  193. break;
  194. case WM9712_ID2:
  195. case WM9713_ID2:
  196. /* use PEN_DOWN GPIO 13 to assert IRQ on GPIO line 2 */
  197. wm97xx_config_gpio(wm, WM97XX_GPIO_13, WM97XX_GPIO_IN,
  198. WM97XX_GPIO_POL_HIGH,
  199. WM97XX_GPIO_STICKY,
  200. WM97XX_GPIO_WAKE);
  201. wm97xx_config_gpio(wm, WM97XX_GPIO_2, WM97XX_GPIO_OUT,
  202. WM97XX_GPIO_POL_HIGH,
  203. WM97XX_GPIO_NOTSTICKY,
  204. WM97XX_GPIO_NOWAKE);
  205. break;
  206. default:
  207. dev_err(wm->dev,
  208. "pen down irq not supported on this device\n");
  209. pen_int = 0;
  210. break;
  211. }
  212. }
  213. out:
  214. return ret;
  215. }
  216. static void wm97xx_acc_shutdown(struct wm97xx *wm)
  217. {
  218. /* codec specific deconfig */
  219. if (pen_int) {
  220. if (irq)
  221. gpio_free(irq);
  222. wm->pen_irq = 0;
  223. }
  224. }
  225. static void wm97xx_irq_enable(struct wm97xx *wm, int enable)
  226. {
  227. if (enable)
  228. enable_irq(wm->pen_irq);
  229. else
  230. disable_irq_nosync(wm->pen_irq);
  231. }
  232. static struct wm97xx_mach_ops mainstone_mach_ops = {
  233. .acc_enabled = 1,
  234. .acc_pen_up = wm97xx_acc_pen_up,
  235. .acc_pen_down = wm97xx_acc_pen_down,
  236. .acc_startup = wm97xx_acc_startup,
  237. .acc_shutdown = wm97xx_acc_shutdown,
  238. .irq_enable = wm97xx_irq_enable,
  239. .irq_gpio = WM97XX_GPIO_2,
  240. };
  241. static int mainstone_wm97xx_probe(struct platform_device *pdev)
  242. {
  243. struct wm97xx *wm = platform_get_drvdata(pdev);
  244. return wm97xx_register_mach_ops(wm, &mainstone_mach_ops);
  245. }
  246. static int mainstone_wm97xx_remove(struct platform_device *pdev)
  247. {
  248. struct wm97xx *wm = platform_get_drvdata(pdev);
  249. wm97xx_unregister_mach_ops(wm);
  250. return 0;
  251. }
  252. static struct platform_driver mainstone_wm97xx_driver = {
  253. .probe = mainstone_wm97xx_probe,
  254. .remove = mainstone_wm97xx_remove,
  255. .driver = {
  256. .name = "wm97xx-touch",
  257. },
  258. };
  259. module_platform_driver(mainstone_wm97xx_driver);
  260. /* Module information */
  261. MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
  262. MODULE_DESCRIPTION("wm97xx continuous touch driver for mainstone");
  263. MODULE_LICENSE("GPL");