wm9705.c 9.2 KB

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