wm9705.c 9.2 KB

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