wm9712.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. /* WM9712 five wire */
  157. if (five_wire) {
  158. dig2 |= WM9712_45W;
  159. dev_dbg(wm->dev, "setting 5-wire touchscreen mode.");
  160. if (pil) {
  161. dev_warn(wm->dev, "pressure measurement is not "
  162. "supported in 5-wire mode\n");
  163. pil = 0;
  164. }
  165. }
  166. /* touchpanel pressure current*/
  167. if (pil == 2) {
  168. dig2 |= WM9712_PIL;
  169. dev_dbg(wm->dev,
  170. "setting pressure measurement current to 400uA.");
  171. } else if (pil)
  172. dev_dbg(wm->dev,
  173. "setting pressure measurement current to 200uA.");
  174. if (!pil)
  175. pressure = 0;
  176. /* polling mode sample settling delay */
  177. if (delay < 0 || delay > 15) {
  178. dev_dbg(wm->dev, "supplied delay out of range.");
  179. delay = 4;
  180. }
  181. dig1 &= 0xff0f;
  182. dig1 |= WM97XX_DELAY(delay);
  183. dev_dbg(wm->dev, "setting adc sample delay to %d u Secs.",
  184. delay_table[delay]);
  185. /* mask */
  186. dig2 |= ((mask & 0x3) << 6);
  187. if (mask) {
  188. u16 reg;
  189. /* Set GPIO4 as Mask Pin*/
  190. reg = wm97xx_reg_read(wm, AC97_MISC_AFE);
  191. wm97xx_reg_write(wm, AC97_MISC_AFE, reg | WM97XX_GPIO_4);
  192. reg = wm97xx_reg_read(wm, AC97_GPIO_CFG);
  193. wm97xx_reg_write(wm, AC97_GPIO_CFG, reg | WM97XX_GPIO_4);
  194. }
  195. /* wait - coord mode */
  196. if (coord)
  197. dig2 |= WM9712_WAIT;
  198. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, dig1);
  199. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, dig2);
  200. }
  201. static void wm9712_dig_enable(struct wm97xx *wm, int enable)
  202. {
  203. u16 dig2 = wm->dig[2];
  204. if (enable) {
  205. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2,
  206. dig2 | WM97XX_PRP_DET_DIG);
  207. wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD); /* dummy read */
  208. } else
  209. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2,
  210. dig2 & ~WM97XX_PRP_DET_DIG);
  211. }
  212. static void wm9712_aux_prepare(struct wm97xx *wm)
  213. {
  214. memcpy(wm->dig_save, wm->dig, sizeof(wm->dig));
  215. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, 0);
  216. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, WM97XX_PRP_DET_DIG);
  217. }
  218. static void wm9712_dig_restore(struct wm97xx *wm)
  219. {
  220. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, wm->dig_save[1]);
  221. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, wm->dig_save[2]);
  222. }
  223. static inline int is_pden(struct wm97xx *wm)
  224. {
  225. return wm->dig[2] & WM9712_PDEN;
  226. }
  227. /*
  228. * Read a sample from the WM9712 adc in polling mode.
  229. */
  230. static int wm9712_poll_sample(struct wm97xx *wm, int adcsel, int *sample)
  231. {
  232. int timeout = 5 * delay;
  233. if (!wm->pen_probably_down) {
  234. u16 data = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
  235. if (!(data & WM97XX_PEN_DOWN))
  236. return RC_PENUP;
  237. wm->pen_probably_down = 1;
  238. }
  239. /* set up digitiser */
  240. if (adcsel & 0x8000)
  241. adcsel = ((adcsel & 0x7fff) + 3) << 12;
  242. if (wm->mach_ops && wm->mach_ops->pre_sample)
  243. wm->mach_ops->pre_sample(adcsel);
  244. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1,
  245. adcsel | WM97XX_POLL | WM97XX_DELAY(delay));
  246. /* wait 3 AC97 time slots + delay for conversion */
  247. poll_delay(delay);
  248. /* wait for POLL to go low */
  249. while ((wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER1) & WM97XX_POLL)
  250. && timeout) {
  251. udelay(AC97_LINK_FRAME);
  252. timeout--;
  253. }
  254. if (timeout <= 0) {
  255. /* If PDEN is set, we can get a timeout when pen goes up */
  256. if (is_pden(wm))
  257. wm->pen_probably_down = 0;
  258. else
  259. dev_dbg(wm->dev, "adc sample timeout");
  260. return RC_PENUP;
  261. }
  262. *sample = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
  263. if (wm->mach_ops && wm->mach_ops->post_sample)
  264. wm->mach_ops->post_sample(adcsel);
  265. /* check we have correct sample */
  266. if ((*sample & WM97XX_ADCSEL_MASK) != adcsel) {
  267. dev_dbg(wm->dev, "adc wrong sample, read %x got %x", adcsel,
  268. *sample & WM97XX_ADCSEL_MASK);
  269. return RC_PENUP;
  270. }
  271. if (!(*sample & WM97XX_PEN_DOWN)) {
  272. wm->pen_probably_down = 0;
  273. return RC_PENUP;
  274. }
  275. return RC_VALID;
  276. }
  277. /*
  278. * Read a coord from the WM9712 adc in polling mode.
  279. */
  280. static int wm9712_poll_coord(struct wm97xx *wm, struct wm97xx_data *data)
  281. {
  282. int timeout = 5 * delay;
  283. if (!wm->pen_probably_down) {
  284. u16 data_rd = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
  285. if (!(data_rd & WM97XX_PEN_DOWN))
  286. return RC_PENUP;
  287. wm->pen_probably_down = 1;
  288. }
  289. /* set up digitiser */
  290. if (wm->mach_ops && wm->mach_ops->pre_sample)
  291. wm->mach_ops->pre_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y);
  292. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1,
  293. WM97XX_COO | WM97XX_POLL | WM97XX_DELAY(delay));
  294. /* wait 3 AC97 time slots + delay for conversion and read x */
  295. poll_delay(delay);
  296. data->x = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
  297. /* wait for POLL to go low */
  298. while ((wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER1) & WM97XX_POLL)
  299. && timeout) {
  300. udelay(AC97_LINK_FRAME);
  301. timeout--;
  302. }
  303. if (timeout <= 0) {
  304. /* If PDEN is set, we can get a timeout when pen goes up */
  305. if (is_pden(wm))
  306. wm->pen_probably_down = 0;
  307. else
  308. dev_dbg(wm->dev, "adc sample timeout");
  309. return RC_PENUP;
  310. }
  311. /* read back y data */
  312. data->y = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
  313. if (pil)
  314. data->p = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
  315. else
  316. data->p = DEFAULT_PRESSURE;
  317. if (wm->mach_ops && wm->mach_ops->post_sample)
  318. wm->mach_ops->post_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y);
  319. /* check we have correct sample */
  320. if (!(data->x & WM97XX_ADCSEL_X) || !(data->y & WM97XX_ADCSEL_Y))
  321. goto err;
  322. if (pil && !(data->p & WM97XX_ADCSEL_PRES))
  323. goto err;
  324. if (!(data->x & WM97XX_PEN_DOWN) || !(data->y & WM97XX_PEN_DOWN)) {
  325. wm->pen_probably_down = 0;
  326. return RC_PENUP;
  327. }
  328. return RC_VALID;
  329. err:
  330. return 0;
  331. }
  332. /*
  333. * Sample the WM9712 touchscreen in polling mode
  334. */
  335. static int wm9712_poll_touch(struct wm97xx *wm, struct wm97xx_data *data)
  336. {
  337. int rc;
  338. if (coord) {
  339. rc = wm9712_poll_coord(wm, data);
  340. if (rc != RC_VALID)
  341. return rc;
  342. } else {
  343. rc = wm9712_poll_sample(wm, WM97XX_ADCSEL_X, &data->x);
  344. if (rc != RC_VALID)
  345. return rc;
  346. rc = wm9712_poll_sample(wm, WM97XX_ADCSEL_Y, &data->y);
  347. if (rc != RC_VALID)
  348. return rc;
  349. if (pil && !five_wire) {
  350. rc = wm9712_poll_sample(wm, WM97XX_ADCSEL_PRES,
  351. &data->p);
  352. if (rc != RC_VALID)
  353. return rc;
  354. } else
  355. data->p = DEFAULT_PRESSURE;
  356. }
  357. return RC_VALID;
  358. }
  359. /*
  360. * Enable WM9712 continuous mode, i.e. touch data is streamed across
  361. * an AC97 slot
  362. */
  363. static int wm9712_acc_enable(struct wm97xx *wm, int enable)
  364. {
  365. u16 dig1, dig2;
  366. int ret = 0;
  367. dig1 = wm->dig[1];
  368. dig2 = wm->dig[2];
  369. if (enable) {
  370. /* continous mode */
  371. if (wm->mach_ops->acc_startup) {
  372. ret = wm->mach_ops->acc_startup(wm);
  373. if (ret < 0)
  374. return ret;
  375. }
  376. dig1 &= ~(WM97XX_CM_RATE_MASK | WM97XX_ADCSEL_MASK |
  377. WM97XX_DELAY_MASK | WM97XX_SLT_MASK);
  378. dig1 |= WM97XX_CTC | WM97XX_COO | WM97XX_SLEN |
  379. WM97XX_DELAY(delay) |
  380. WM97XX_SLT(wm->acc_slot) |
  381. WM97XX_RATE(wm->acc_rate);
  382. if (pil)
  383. dig1 |= WM97XX_ADCSEL_PRES;
  384. dig2 |= WM9712_PDEN;
  385. } else {
  386. dig1 &= ~(WM97XX_CTC | WM97XX_COO | WM97XX_SLEN);
  387. dig2 &= ~WM9712_PDEN;
  388. if (wm->mach_ops->acc_shutdown)
  389. wm->mach_ops->acc_shutdown(wm);
  390. }
  391. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, dig1);
  392. wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, dig2);
  393. return 0;
  394. }
  395. struct wm97xx_codec_drv wm9712_codec = {
  396. .id = WM9712_ID2,
  397. .name = "wm9712",
  398. .poll_sample = wm9712_poll_sample,
  399. .poll_touch = wm9712_poll_touch,
  400. .acc_enable = wm9712_acc_enable,
  401. .phy_init = wm9712_phy_init,
  402. .dig_enable = wm9712_dig_enable,
  403. .dig_restore = wm9712_dig_restore,
  404. .aux_prepare = wm9712_aux_prepare,
  405. };
  406. EXPORT_SYMBOL_GPL(wm9712_codec);
  407. /* Module information */
  408. MODULE_AUTHOR("Liam Girdwood <liam.girdwood@wolfsonmicro.com>");
  409. MODULE_DESCRIPTION("WM9712 Touch Screen Driver");
  410. MODULE_LICENSE("GPL");