wm9705.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * wm9705.c -- Codec driver for Wolfson WM9705 AC97 Codec.
  4. *
  5. * Copyright 2003, 2004, 2005, 2006, 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. * Russell King <rmk@arm.linux.org.uk>
  10. */
  11. #include <linux/export.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/kernel.h>
  15. #include <linux/input.h>
  16. #include <linux/delay.h>
  17. #include <linux/bitops.h>
  18. #include <linux/wm97xx.h>
  19. #define TS_NAME "wm97xx"
  20. #define WM9705_VERSION "1.00"
  21. #define DEFAULT_PRESSURE 0xb0c0
  22. /*
  23. * Module parameters
  24. */
  25. /*
  26. * Set current used for pressure measurement.
  27. *
  28. * Set pil = 2 to use 400uA
  29. * pil = 1 to use 200uA and
  30. * pil = 0 to disable pressure measurement.
  31. *
  32. * This is used to increase the range of values returned by the adc
  33. * when measureing touchpanel pressure.
  34. */
  35. static int pil;
  36. module_param(pil, int, 0);
  37. MODULE_PARM_DESC(pil, "Set current used for pressure measurement.");
  38. /*
  39. * Set threshold for pressure measurement.
  40. *
  41. * Pen down pressure below threshold is ignored.
  42. */
  43. static int pressure = DEFAULT_PRESSURE & 0xfff;
  44. module_param(pressure, int, 0);
  45. MODULE_PARM_DESC(pressure, "Set threshold for pressure measurement.");
  46. /*
  47. * Set adc sample delay.
  48. *
  49. * For accurate touchpanel measurements, some settling time may be
  50. * required between the switch matrix applying a voltage across the
  51. * touchpanel plate and the ADC sampling the signal.
  52. *
  53. * This delay can be set by setting delay = n, where n is the array
  54. * position of the delay in the array delay_table below.
  55. * Long delays > 1ms are supported for completeness, but are not
  56. * recommended.
  57. */
  58. static int delay = 4;
  59. module_param(delay, int, 0);
  60. MODULE_PARM_DESC(delay, "Set adc sample delay.");
  61. /*
  62. * Pen detect comparator threshold.
  63. *
  64. * 0 to Vmid in 15 steps, 0 = use zero power comparator with Vmid threshold
  65. * i.e. 1 = Vmid/15 threshold
  66. * 15 = Vmid/1 threshold
  67. *
  68. * Adjust this value if you are having problems with pen detect not
  69. * detecting any down events.
  70. */
  71. static int pdd = 8;
  72. module_param(pdd, int, 0);
  73. MODULE_PARM_DESC(pdd, "Set pen detect comparator threshold");
  74. /*
  75. * Set adc mask function.
  76. *
  77. * Sources of glitch noise, such as signals driving an LCD display, may feed
  78. * through to the touch screen plates and affect measurement accuracy. In
  79. * order to minimise this, a signal may be applied to the MASK pin to delay or
  80. * synchronise the sampling.
  81. *
  82. * 0 = No delay or sync
  83. * 1 = High on pin stops conversions
  84. * 2 = Edge triggered, edge on pin delays conversion by delay param (above)
  85. * 3 = Edge triggered, edge on pin starts conversion after delay param
  86. */
  87. static int mask;
  88. module_param(mask, int, 0);
  89. MODULE_PARM_DESC(mask, "Set adc mask function.");
  90. /*
  91. * ADC sample delay times in uS
  92. */
  93. static const int delay_table[] = {
  94. 21, /* 1 AC97 Link frames */
  95. 42, /* 2 */
  96. 84, /* 4 */
  97. 167, /* 8 */
  98. 333, /* 16 */
  99. 667, /* 32 */
  100. 1000, /* 48 */
  101. 1333, /* 64 */
  102. 2000, /* 96 */
  103. 2667, /* 128 */
  104. 3333, /* 160 */
  105. 4000, /* 192 */
  106. 4667, /* 224 */
  107. 5333, /* 256 */
  108. 6000, /* 288 */
  109. 0 /* No delay, switch matrix always on */
  110. };
  111. /*
  112. * Delay after issuing a POLL command.
  113. *
  114. * The delay is 3 AC97 link frames + the touchpanel settling delay
  115. */
  116. static inline void poll_delay(int d)
  117. {
  118. udelay(3 * AC97_LINK_FRAME + delay_table[d]);
  119. }
  120. /*
  121. * set up the physical settings of the WM9705
  122. */
  123. static void wm9705_phy_init(struct wm97xx *wm)
  124. {
  125. u16 dig1 = 0, dig2 = WM97XX_RPR;
  126. /*
  127. * mute VIDEO and AUX as they share X and Y touchscreen
  128. * inputs on the WM9705
  129. */
  130. wm97xx_reg_write(wm, AC97_AUX, 0x8000);
  131. wm97xx_reg_write(wm, AC97_VIDEO, 0x8000);
  132. /* touchpanel pressure current*/
  133. if (pil == 2) {
  134. dig2 |= WM9705_PIL;
  135. dev_dbg(wm->dev,
  136. "setting pressure measurement current to 400uA.");
  137. } else if (pil)
  138. dev_dbg(wm->dev,
  139. "setting pressure measurement current to 200uA.");
  140. if (!pil)
  141. pressure = 0;
  142. /* polling mode sample settling delay */
  143. if (delay != 4) {
  144. if (delay < 0 || delay > 15) {
  145. dev_dbg(wm->dev, "supplied delay out of range.");
  146. delay = 4;
  147. }
  148. }
  149. dig1 &= 0xff0f;
  150. dig1 |= WM97XX_DELAY(delay);
  151. dev_dbg(wm->dev, "setting adc sample delay to %d u Secs.",
  152. delay_table[delay]);
  153. /* WM9705 pdd */
  154. dig2 |= (pdd & 0x000f);
  155. dev_dbg(wm->dev, "setting pdd to Vmid/%d", 1 - (pdd & 0x000f));
  156. /* mask */
  157. dig2 |= ((mask & 0x3) << 4);
  158. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, dig1);
  159. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, dig2);
  160. }
  161. static void wm9705_dig_enable(struct wm97xx *wm, int enable)
  162. {
  163. if (enable) {
  164. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2,
  165. wm->dig[2] | WM97XX_PRP_DET_DIG);
  166. wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD); /* dummy read */
  167. } else
  168. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2,
  169. wm->dig[2] & ~WM97XX_PRP_DET_DIG);
  170. }
  171. static void wm9705_aux_prepare(struct wm97xx *wm)
  172. {
  173. memcpy(wm->dig_save, wm->dig, sizeof(wm->dig));
  174. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, 0);
  175. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, WM97XX_PRP_DET_DIG);
  176. }
  177. static void wm9705_dig_restore(struct wm97xx *wm)
  178. {
  179. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, wm->dig_save[1]);
  180. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, wm->dig_save[2]);
  181. }
  182. static inline int is_pden(struct wm97xx *wm)
  183. {
  184. return wm->dig[2] & WM9705_PDEN;
  185. }
  186. /*
  187. * Read a sample from the WM9705 adc in polling mode.
  188. */
  189. static int wm9705_poll_sample(struct wm97xx *wm, int adcsel, int *sample)
  190. {
  191. int timeout = 5 * delay;
  192. bool wants_pen = adcsel & WM97XX_PEN_DOWN;
  193. if (wants_pen && !wm->pen_probably_down) {
  194. u16 data = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
  195. if (!(data & WM97XX_PEN_DOWN))
  196. return RC_PENUP;
  197. wm->pen_probably_down = 1;
  198. }
  199. /* set up digitiser */
  200. if (wm->mach_ops && wm->mach_ops->pre_sample)
  201. wm->mach_ops->pre_sample(adcsel);
  202. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, (adcsel & WM97XX_ADCSEL_MASK)
  203. | WM97XX_POLL | WM97XX_DELAY(delay));
  204. /* wait 3 AC97 time slots + delay for conversion */
  205. poll_delay(delay);
  206. /* wait for POLL to go low */
  207. while ((wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER1) & WM97XX_POLL)
  208. && timeout) {
  209. udelay(AC97_LINK_FRAME);
  210. timeout--;
  211. }
  212. if (timeout == 0) {
  213. /* If PDEN is set, we can get a timeout when pen goes up */
  214. if (is_pden(wm))
  215. wm->pen_probably_down = 0;
  216. else
  217. dev_dbg(wm->dev, "adc sample timeout");
  218. return RC_PENUP;
  219. }
  220. *sample = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
  221. if (wm->mach_ops && wm->mach_ops->post_sample)
  222. wm->mach_ops->post_sample(adcsel);
  223. /* check we have correct sample */
  224. if ((*sample ^ adcsel) & WM97XX_ADCSEL_MASK) {
  225. dev_dbg(wm->dev, "adc wrong sample, wanted %x got %x",
  226. adcsel & WM97XX_ADCSEL_MASK,
  227. *sample & WM97XX_ADCSEL_MASK);
  228. return RC_PENUP;
  229. }
  230. if (wants_pen && !(*sample & WM97XX_PEN_DOWN)) {
  231. wm->pen_probably_down = 0;
  232. return RC_PENUP;
  233. }
  234. return RC_VALID;
  235. }
  236. /*
  237. * Sample the WM9705 touchscreen in polling mode
  238. */
  239. static int wm9705_poll_touch(struct wm97xx *wm, struct wm97xx_data *data)
  240. {
  241. int rc;
  242. rc = wm9705_poll_sample(wm, WM97XX_ADCSEL_X | WM97XX_PEN_DOWN, &data->x);
  243. if (rc != RC_VALID)
  244. return rc;
  245. rc = wm9705_poll_sample(wm, WM97XX_ADCSEL_Y | WM97XX_PEN_DOWN, &data->y);
  246. if (rc != RC_VALID)
  247. return rc;
  248. if (pil) {
  249. rc = wm9705_poll_sample(wm, WM97XX_ADCSEL_PRES | WM97XX_PEN_DOWN, &data->p);
  250. if (rc != RC_VALID)
  251. return rc;
  252. } else
  253. data->p = DEFAULT_PRESSURE;
  254. return RC_VALID;
  255. }
  256. /*
  257. * Enable WM9705 continuous mode, i.e. touch data is streamed across
  258. * an AC97 slot
  259. */
  260. static int wm9705_acc_enable(struct wm97xx *wm, int enable)
  261. {
  262. u16 dig1, dig2;
  263. int ret = 0;
  264. dig1 = wm->dig[1];
  265. dig2 = wm->dig[2];
  266. if (enable) {
  267. /* continuous mode */
  268. if (wm->mach_ops->acc_startup &&
  269. (ret = wm->mach_ops->acc_startup(wm)) < 0)
  270. return ret;
  271. dig1 &= ~(WM97XX_CM_RATE_MASK | WM97XX_ADCSEL_MASK |
  272. WM97XX_DELAY_MASK | WM97XX_SLT_MASK);
  273. dig1 |= WM97XX_CTC | WM97XX_COO | WM97XX_SLEN |
  274. WM97XX_DELAY(delay) |
  275. WM97XX_SLT(wm->acc_slot) |
  276. WM97XX_RATE(wm->acc_rate);
  277. if (pil)
  278. dig1 |= WM97XX_ADCSEL_PRES;
  279. dig2 |= WM9705_PDEN;
  280. } else {
  281. dig1 &= ~(WM97XX_CTC | WM97XX_COO | WM97XX_SLEN);
  282. dig2 &= ~WM9705_PDEN;
  283. if (wm->mach_ops->acc_shutdown)
  284. wm->mach_ops->acc_shutdown(wm);
  285. }
  286. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, dig1);
  287. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, dig2);
  288. return ret;
  289. }
  290. struct wm97xx_codec_drv wm9705_codec = {
  291. .id = WM9705_ID2,
  292. .name = "wm9705",
  293. .poll_sample = wm9705_poll_sample,
  294. .poll_touch = wm9705_poll_touch,
  295. .acc_enable = wm9705_acc_enable,
  296. .phy_init = wm9705_phy_init,
  297. .dig_enable = wm9705_dig_enable,
  298. .dig_restore = wm9705_dig_restore,
  299. .aux_prepare = wm9705_aux_prepare,
  300. };
  301. EXPORT_SYMBOL_GPL(wm9705_codec);
  302. /* Module information */
  303. MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
  304. MODULE_DESCRIPTION("WM9705 Touch Screen Driver");
  305. MODULE_LICENSE("GPL");