wm9705.c 9.2 KB

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