wm9712.c 12 KB

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