wm9712.c 12 KB

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