zylonite-wm97xx.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * zylonite-wm97xx.c -- Zylonite Continuous Touch screen driver
  4. *
  5. * Copyright 2004, 2007, 2008 Wolfson Microelectronics PLC.
  6. * Author: Mark Brown <[email protected]>
  7. * Parts Copyright : Ian Molton <[email protected]>
  8. * Andrew Zabolotny <[email protected]>
  9. *
  10. * Notes:
  11. * This is a wm97xx extended touch driver supporting interrupt driven
  12. * and continuous operation on Marvell Zylonite development systems
  13. * (which have a WM9713 on board).
  14. */
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/kernel.h>
  18. #include <linux/delay.h>
  19. #include <linux/gpio/consumer.h>
  20. #include <linux/irq.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/io.h>
  23. #include <linux/soc/pxa/cpu.h>
  24. #include <linux/wm97xx.h>
  25. #include <sound/pxa2xx-lib.h>
  26. struct continuous {
  27. u16 id; /* codec id */
  28. u8 code; /* continuous code */
  29. u8 reads; /* number of coord reads per read cycle */
  30. u32 speed; /* number of coords per second */
  31. };
  32. #define WM_READS(sp) ((sp / HZ) + 1)
  33. static const struct continuous cinfo[] = {
  34. { WM9713_ID2, 0, WM_READS(94), 94 },
  35. { WM9713_ID2, 1, WM_READS(120), 120 },
  36. { WM9713_ID2, 2, WM_READS(154), 154 },
  37. { WM9713_ID2, 3, WM_READS(188), 188 },
  38. };
  39. /* continuous speed index */
  40. static int sp_idx;
  41. /*
  42. * Pen sampling frequency (Hz) in continuous mode.
  43. */
  44. static int cont_rate = 200;
  45. module_param(cont_rate, int, 0);
  46. MODULE_PARM_DESC(cont_rate, "Sampling rate in continuous mode (Hz)");
  47. /*
  48. * Pressure readback.
  49. *
  50. * Set to 1 to read back pen down pressure
  51. */
  52. static int pressure;
  53. module_param(pressure, int, 0);
  54. MODULE_PARM_DESC(pressure, "Pressure readback (1 = pressure, 0 = no pressure)");
  55. /*
  56. * AC97 touch data slot.
  57. *
  58. * Touch screen readback data ac97 slot
  59. */
  60. static int ac97_touch_slot = 5;
  61. module_param(ac97_touch_slot, int, 0);
  62. MODULE_PARM_DESC(ac97_touch_slot, "Touch screen data slot AC97 number");
  63. /* flush AC97 slot 5 FIFO machines */
  64. static void wm97xx_acc_pen_up(struct wm97xx *wm)
  65. {
  66. int i;
  67. msleep(1);
  68. for (i = 0; i < 16; i++)
  69. pxa2xx_ac97_read_modr();
  70. }
  71. static int wm97xx_acc_pen_down(struct wm97xx *wm)
  72. {
  73. u16 x, y, p = 0x100 | WM97XX_ADCSEL_PRES;
  74. int reads = 0;
  75. static u16 last, tries;
  76. /* When the AC97 queue has been drained we need to allow time
  77. * to buffer up samples otherwise we end up spinning polling
  78. * for samples. The controller can't have a suitably low
  79. * threshold set to use the notifications it gives.
  80. */
  81. msleep(1);
  82. if (tries > 5) {
  83. tries = 0;
  84. return RC_PENUP;
  85. }
  86. x = pxa2xx_ac97_read_modr();
  87. if (x == last) {
  88. tries++;
  89. return RC_AGAIN;
  90. }
  91. last = x;
  92. do {
  93. if (reads)
  94. x = pxa2xx_ac97_read_modr();
  95. y = pxa2xx_ac97_read_modr();
  96. if (pressure)
  97. p = pxa2xx_ac97_read_modr();
  98. dev_dbg(wm->dev, "Raw coordinates: x=%x, y=%x, p=%x\n",
  99. x, y, p);
  100. /* are samples valid */
  101. if ((x & WM97XX_ADCSEL_MASK) != WM97XX_ADCSEL_X ||
  102. (y & WM97XX_ADCSEL_MASK) != WM97XX_ADCSEL_Y ||
  103. (p & WM97XX_ADCSEL_MASK) != WM97XX_ADCSEL_PRES)
  104. goto up;
  105. /* coordinate is good */
  106. tries = 0;
  107. input_report_abs(wm->input_dev, ABS_X, x & 0xfff);
  108. input_report_abs(wm->input_dev, ABS_Y, y & 0xfff);
  109. input_report_abs(wm->input_dev, ABS_PRESSURE, p & 0xfff);
  110. input_report_key(wm->input_dev, BTN_TOUCH, (p != 0));
  111. input_sync(wm->input_dev);
  112. reads++;
  113. } while (reads < cinfo[sp_idx].reads);
  114. up:
  115. return RC_PENDOWN | RC_AGAIN;
  116. }
  117. static int wm97xx_acc_startup(struct wm97xx *wm)
  118. {
  119. int idx;
  120. /* check we have a codec */
  121. if (wm->ac97 == NULL)
  122. return -ENODEV;
  123. /* Go you big red fire engine */
  124. for (idx = 0; idx < ARRAY_SIZE(cinfo); idx++) {
  125. if (wm->id != cinfo[idx].id)
  126. continue;
  127. sp_idx = idx;
  128. if (cont_rate <= cinfo[idx].speed)
  129. break;
  130. }
  131. wm->acc_rate = cinfo[sp_idx].code;
  132. wm->acc_slot = ac97_touch_slot;
  133. dev_info(wm->dev,
  134. "zylonite accelerated touchscreen driver, %d samples/sec\n",
  135. cinfo[sp_idx].speed);
  136. return 0;
  137. }
  138. static struct wm97xx_mach_ops zylonite_mach_ops = {
  139. .acc_enabled = 1,
  140. .acc_pen_up = wm97xx_acc_pen_up,
  141. .acc_pen_down = wm97xx_acc_pen_down,
  142. .acc_startup = wm97xx_acc_startup,
  143. .irq_gpio = WM97XX_GPIO_2,
  144. };
  145. static int zylonite_wm97xx_probe(struct platform_device *pdev)
  146. {
  147. struct wm97xx *wm = platform_get_drvdata(pdev);
  148. struct gpio_desc *gpio_touch_irq;
  149. int err;
  150. gpio_touch_irq = devm_gpiod_get(&pdev->dev, "touch", GPIOD_IN);
  151. err = PTR_ERR_OR_ZERO(gpio_touch_irq);
  152. if (err) {
  153. dev_err(&pdev->dev, "Cannot get irq gpio: %d\n", err);
  154. return err;
  155. }
  156. wm->pen_irq = gpiod_to_irq(gpio_touch_irq);
  157. irq_set_irq_type(wm->pen_irq, IRQ_TYPE_EDGE_BOTH);
  158. wm97xx_config_gpio(wm, WM97XX_GPIO_13, WM97XX_GPIO_IN,
  159. WM97XX_GPIO_POL_HIGH,
  160. WM97XX_GPIO_STICKY,
  161. WM97XX_GPIO_WAKE);
  162. wm97xx_config_gpio(wm, WM97XX_GPIO_2, WM97XX_GPIO_OUT,
  163. WM97XX_GPIO_POL_HIGH,
  164. WM97XX_GPIO_NOTSTICKY,
  165. WM97XX_GPIO_NOWAKE);
  166. return wm97xx_register_mach_ops(wm, &zylonite_mach_ops);
  167. }
  168. static int zylonite_wm97xx_remove(struct platform_device *pdev)
  169. {
  170. struct wm97xx *wm = platform_get_drvdata(pdev);
  171. wm97xx_unregister_mach_ops(wm);
  172. return 0;
  173. }
  174. static struct platform_driver zylonite_wm97xx_driver = {
  175. .probe = zylonite_wm97xx_probe,
  176. .remove = zylonite_wm97xx_remove,
  177. .driver = {
  178. .name = "wm97xx-touch",
  179. },
  180. };
  181. module_platform_driver(zylonite_wm97xx_driver);
  182. /* Module information */
  183. MODULE_AUTHOR("Mark Brown <[email protected]>");
  184. MODULE_DESCRIPTION("wm97xx continuous touch driver for Zylonite");
  185. MODULE_LICENSE("GPL");