wm9712.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. * wm9712.c -- Codec driver for Wolfson WM9712 AC97 Codecs.
  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 WM9712_VERSION "1.00"
  27. #define DEFAULT_PRESSURE 0xb0c0
  28. /*
  29. * Module parameters
  30. */
  31. /*
  32. * Set internal pull up for pen detect.
  33. *
  34. * Pull up is in the range 1.02k (least sensitive) to 64k (most sensitive)
  35. * i.e. pull up resistance = 64k Ohms / rpu.
  36. *
  37. * Adjust this value if you are having problems with pen detect not
  38. * detecting any down event.
  39. */
  40. static int rpu = 8;
  41. module_param(rpu, int, 0);
  42. MODULE_PARM_DESC(rpu, "Set internal pull up resitor for pen detect.");
  43. /*
  44. * Set current used for pressure measurement.
  45. *
  46. * Set pil = 2 to use 400uA
  47. * pil = 1 to use 200uA and
  48. * pil = 0 to disable pressure measurement.
  49. *
  50. * This is used to increase the range of values returned by the adc
  51. * when measureing touchpanel pressure.
  52. */
  53. static int pil;
  54. module_param(pil, int, 0);
  55. MODULE_PARM_DESC(pil, "Set current used for pressure measurement.");
  56. /*
  57. * Set threshold for pressure measurement.
  58. *
  59. * Pen down pressure below threshold is ignored.
  60. */
  61. static int pressure = DEFAULT_PRESSURE & 0xfff;
  62. module_param(pressure, int, 0);
  63. MODULE_PARM_DESC(pressure, "Set threshold for pressure measurement.");
  64. /*
  65. * Set adc sample delay.
  66. *
  67. * For accurate touchpanel measurements, some settling time may be
  68. * required between the switch matrix applying a voltage across the
  69. * touchpanel plate and the ADC sampling the signal.
  70. *
  71. * This delay can be set by setting delay = n, where n is the array
  72. * position of the delay in the array delay_table below.
  73. * Long delays > 1ms are supported for completeness, but are not
  74. * recommended.
  75. */
  76. static int delay = 3;
  77. module_param(delay, int, 0);
  78. MODULE_PARM_DESC(delay, "Set adc sample delay.");
  79. /*
  80. * Set five_wire = 1 to use a 5 wire touchscreen.
  81. *
  82. * NOTE: Five wire mode does not allow for readback of pressure.
  83. */
  84. static int five_wire;
  85. module_param(five_wire, int, 0);
  86. MODULE_PARM_DESC(five_wire, "Set to '1' to use 5-wire touchscreen.");
  87. /*
  88. * Set adc mask function.
  89. *
  90. * Sources of glitch noise, such as signals driving an LCD display, may feed
  91. * through to the touch screen plates and affect measurement accuracy. In
  92. * order to minimise this, a signal may be applied to the MASK pin to delay or
  93. * synchronise the sampling.
  94. *
  95. * 0 = No delay or sync
  96. * 1 = High on pin stops conversions
  97. * 2 = Edge triggered, edge on pin delays conversion by delay param (above)
  98. * 3 = Edge triggered, edge on pin starts conversion after delay param
  99. */
  100. static int mask;
  101. module_param(mask, int, 0);
  102. MODULE_PARM_DESC(mask, "Set adc mask function.");
  103. /*
  104. * Coordinate Polling Enable.
  105. *
  106. * Set to 1 to enable coordinate polling. e.g. x,y[,p] is sampled together
  107. * for every poll.
  108. */
  109. static int coord;
  110. module_param(coord, int, 0);
  111. MODULE_PARM_DESC(coord, "Polling coordinate mode");
  112. /*
  113. * ADC sample delay times in uS
  114. */
  115. static const int delay_table[] = {
  116. 21, /* 1 AC97 Link frames */
  117. 42, /* 2 */
  118. 84, /* 4 */
  119. 167, /* 8 */
  120. 333, /* 16 */
  121. 667, /* 32 */
  122. 1000, /* 48 */
  123. 1333, /* 64 */
  124. 2000, /* 96 */
  125. 2667, /* 128 */
  126. 3333, /* 160 */
  127. 4000, /* 192 */
  128. 4667, /* 224 */
  129. 5333, /* 256 */
  130. 6000, /* 288 */
  131. 0 /* No delay, switch matrix always on */
  132. };
  133. /*
  134. * Delay after issuing a POLL command.
  135. *
  136. * The delay is 3 AC97 link frames + the touchpanel settling delay
  137. */
  138. static inline void poll_delay(int d)
  139. {
  140. udelay(3 * AC97_LINK_FRAME + delay_table[d]);
  141. }
  142. /*
  143. * set up the physical settings of the WM9712
  144. */
  145. static void wm9712_phy_init(struct wm97xx *wm)
  146. {
  147. u16 dig1 = 0;
  148. u16 dig2 = WM97XX_RPR | WM9712_RPU(1);
  149. /* WM9712 rpu */
  150. if (rpu) {
  151. dig2 &= 0xffc0;
  152. dig2 |= WM9712_RPU(rpu);
  153. dev_dbg(wm->dev, "setting pen detect pull-up to %d Ohms",
  154. 64000 / rpu);
  155. }
  156. /* touchpanel pressure current*/
  157. if (pil == 2) {
  158. dig2 |= WM9712_PIL;
  159. dev_dbg(wm->dev,
  160. "setting pressure measurement current to 400uA.");
  161. } else if (pil)
  162. dev_dbg(wm->dev,
  163. "setting pressure measurement current to 200uA.");
  164. if (!pil)
  165. pressure = 0;
  166. /* WM9712 five wire */
  167. if (five_wire) {
  168. dig2 |= WM9712_45W;
  169. dev_dbg(wm->dev, "setting 5-wire touchscreen mode.");
  170. }
  171. /* polling mode sample settling delay */
  172. if (delay < 0 || delay > 15) {
  173. dev_dbg(wm->dev, "supplied delay out of range.");
  174. delay = 4;
  175. }
  176. dig1 &= 0xff0f;
  177. dig1 |= WM97XX_DELAY(delay);
  178. dev_dbg(wm->dev, "setting adc sample delay to %d u Secs.",
  179. delay_table[delay]);
  180. /* mask */
  181. dig2 |= ((mask & 0x3) << 6);
  182. if (mask) {
  183. u16 reg;
  184. /* Set GPIO4 as Mask Pin*/
  185. reg = wm97xx_reg_read(wm, AC97_MISC_AFE);
  186. wm97xx_reg_write(wm, AC97_MISC_AFE, reg | WM97XX_GPIO_4);
  187. reg = wm97xx_reg_read(wm, AC97_GPIO_CFG);
  188. wm97xx_reg_write(wm, AC97_GPIO_CFG, reg | WM97XX_GPIO_4);
  189. }
  190. /* wait - coord mode */
  191. if (coord)
  192. dig2 |= WM9712_WAIT;
  193. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, dig1);
  194. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, dig2);
  195. }
  196. static void wm9712_dig_enable(struct wm97xx *wm, int enable)
  197. {
  198. u16 dig2 = wm->dig[2];
  199. if (enable) {
  200. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2,
  201. dig2 | WM97XX_PRP_DET_DIG);
  202. wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD); /* dummy read */
  203. } else
  204. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2,
  205. dig2 & ~WM97XX_PRP_DET_DIG);
  206. }
  207. static void wm9712_aux_prepare(struct wm97xx *wm)
  208. {
  209. memcpy(wm->dig_save, wm->dig, sizeof(wm->dig));
  210. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, 0);
  211. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, WM97XX_PRP_DET_DIG);
  212. }
  213. static void wm9712_dig_restore(struct wm97xx *wm)
  214. {
  215. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, wm->dig_save[1]);
  216. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, wm->dig_save[2]);
  217. }
  218. static inline int is_pden(struct wm97xx *wm)
  219. {
  220. return wm->dig[2] & WM9712_PDEN;
  221. }
  222. /*
  223. * Read a sample from the WM9712 adc in polling mode.
  224. */
  225. static int wm9712_poll_sample(struct wm97xx *wm, int adcsel, int *sample)
  226. {
  227. int timeout = 5 * delay;
  228. if (!wm->pen_probably_down) {
  229. u16 data = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
  230. if (!(data & WM97XX_PEN_DOWN))
  231. return RC_PENUP;
  232. wm->pen_probably_down = 1;
  233. }
  234. /* set up digitiser */
  235. if (adcsel & 0x8000)
  236. adcsel = ((adcsel & 0x7fff) + 3) << 12;
  237. if (wm->mach_ops && wm->mach_ops->pre_sample)
  238. wm->mach_ops->pre_sample(adcsel);
  239. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1,
  240. adcsel | WM97XX_POLL | WM97XX_DELAY(delay));
  241. /* wait 3 AC97 time slots + delay for conversion */
  242. poll_delay(delay);
  243. /* wait for POLL to go low */
  244. while ((wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER1) & WM97XX_POLL)
  245. && timeout) {
  246. udelay(AC97_LINK_FRAME);
  247. timeout--;
  248. }
  249. if (timeout <= 0) {
  250. /* If PDEN is set, we can get a timeout when pen goes up */
  251. if (is_pden(wm))
  252. wm->pen_probably_down = 0;
  253. else
  254. dev_dbg(wm->dev, "adc sample timeout");
  255. return RC_PENUP;
  256. }
  257. *sample = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
  258. if (wm->mach_ops && wm->mach_ops->post_sample)
  259. wm->mach_ops->post_sample(adcsel);
  260. /* check we have correct sample */
  261. if ((*sample & WM97XX_ADCSEL_MASK) != adcsel) {
  262. dev_dbg(wm->dev, "adc wrong sample, read %x got %x", adcsel,
  263. *sample & WM97XX_ADCSEL_MASK);
  264. return RC_PENUP;
  265. }
  266. if (!(*sample & WM97XX_PEN_DOWN)) {
  267. wm->pen_probably_down = 0;
  268. return RC_PENUP;
  269. }
  270. return RC_VALID;
  271. }
  272. /*
  273. * Read a coord from the WM9712 adc in polling mode.
  274. */
  275. static int wm9712_poll_coord(struct wm97xx *wm, struct wm97xx_data *data)
  276. {
  277. int timeout = 5 * delay;
  278. if (!wm->pen_probably_down) {
  279. u16 data_rd = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
  280. if (!(data_rd & WM97XX_PEN_DOWN))
  281. return RC_PENUP;
  282. wm->pen_probably_down = 1;
  283. }
  284. /* set up digitiser */
  285. if (wm->mach_ops && wm->mach_ops->pre_sample)
  286. wm->mach_ops->pre_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y);
  287. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1,
  288. WM97XX_COO | WM97XX_POLL | WM97XX_DELAY(delay));
  289. /* wait 3 AC97 time slots + delay for conversion and read x */
  290. poll_delay(delay);
  291. data->x = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
  292. /* wait for POLL to go low */
  293. while ((wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER1) & WM97XX_POLL)
  294. && timeout) {
  295. udelay(AC97_LINK_FRAME);
  296. timeout--;
  297. }
  298. if (timeout <= 0) {
  299. /* If PDEN is set, we can get a timeout when pen goes up */
  300. if (is_pden(wm))
  301. wm->pen_probably_down = 0;
  302. else
  303. dev_dbg(wm->dev, "adc sample timeout");
  304. return RC_PENUP;
  305. }
  306. /* read back y data */
  307. data->y = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
  308. if (pil)
  309. data->p = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
  310. else
  311. data->p = DEFAULT_PRESSURE;
  312. if (wm->mach_ops && wm->mach_ops->post_sample)
  313. wm->mach_ops->post_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y);
  314. /* check we have correct sample */
  315. if (!(data->x & WM97XX_ADCSEL_X) || !(data->y & WM97XX_ADCSEL_Y))
  316. goto err;
  317. if (pil && !(data->p & WM97XX_ADCSEL_PRES))
  318. goto err;
  319. if (!(data->x & WM97XX_PEN_DOWN) || !(data->y & WM97XX_PEN_DOWN)) {
  320. wm->pen_probably_down = 0;
  321. return RC_PENUP;
  322. }
  323. return RC_VALID;
  324. err:
  325. return 0;
  326. }
  327. /*
  328. * Sample the WM9712 touchscreen in polling mode
  329. */
  330. static int wm9712_poll_touch(struct wm97xx *wm, struct wm97xx_data *data)
  331. {
  332. int rc;
  333. if (coord) {
  334. rc = wm9712_poll_coord(wm, data);
  335. if (rc != RC_VALID)
  336. return rc;
  337. } else {
  338. rc = wm9712_poll_sample(wm, WM97XX_ADCSEL_X, &data->x);
  339. if (rc != RC_VALID)
  340. return rc;
  341. rc = wm9712_poll_sample(wm, WM97XX_ADCSEL_Y, &data->y);
  342. if (rc != RC_VALID)
  343. return rc;
  344. if (pil && !five_wire) {
  345. rc = wm9712_poll_sample(wm, WM97XX_ADCSEL_PRES,
  346. &data->p);
  347. if (rc != RC_VALID)
  348. return rc;
  349. } else
  350. data->p = DEFAULT_PRESSURE;
  351. }
  352. return RC_VALID;
  353. }
  354. /*
  355. * Enable WM9712 continuous mode, i.e. touch data is streamed across
  356. * an AC97 slot
  357. */
  358. static int wm9712_acc_enable(struct wm97xx *wm, int enable)
  359. {
  360. u16 dig1, dig2;
  361. int ret = 0;
  362. dig1 = wm->dig[1];
  363. dig2 = wm->dig[2];
  364. if (enable) {
  365. /* continous mode */
  366. if (wm->mach_ops->acc_startup) {
  367. ret = wm->mach_ops->acc_startup(wm);
  368. if (ret < 0)
  369. return ret;
  370. }
  371. dig1 &= ~(WM97XX_CM_RATE_MASK | WM97XX_ADCSEL_MASK |
  372. WM97XX_DELAY_MASK | WM97XX_SLT_MASK);
  373. dig1 |= WM97XX_CTC | WM97XX_COO | WM97XX_SLEN |
  374. WM97XX_DELAY(delay) |
  375. WM97XX_SLT(wm->acc_slot) |
  376. WM97XX_RATE(wm->acc_rate);
  377. if (pil)
  378. dig1 |= WM97XX_ADCSEL_PRES;
  379. dig2 |= WM9712_PDEN;
  380. } else {
  381. dig1 &= ~(WM97XX_CTC | WM97XX_COO | WM97XX_SLEN);
  382. dig2 &= ~WM9712_PDEN;
  383. if (wm->mach_ops->acc_shutdown)
  384. wm->mach_ops->acc_shutdown(wm);
  385. }
  386. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, dig1);
  387. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, dig2);
  388. return 0;
  389. }
  390. struct wm97xx_codec_drv wm9712_codec = {
  391. .id = WM9712_ID2,
  392. .name = "wm9712",
  393. .poll_sample = wm9712_poll_sample,
  394. .poll_touch = wm9712_poll_touch,
  395. .acc_enable = wm9712_acc_enable,
  396. .phy_init = wm9712_phy_init,
  397. .dig_enable = wm9712_dig_enable,
  398. .dig_restore = wm9712_dig_restore,
  399. .aux_prepare = wm9712_aux_prepare,
  400. };
  401. EXPORT_SYMBOL_GPL(wm9712_codec);
  402. /* Module information */
  403. MODULE_AUTHOR("Liam Girdwood <liam.girdwood@wolfsonmicro.com>");
  404. MODULE_DESCRIPTION("WM9712 Touch Screen Driver");
  405. MODULE_LICENSE("GPL");