wm9705.c 9.0 KB

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