wm9713.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * wm9713.c -- Codec touch driver for Wolfson WM9713 AC97 Codec.
  3. *
  4. * Copyright 2003, 2004, 2005, 2006, 2007, 2008 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 WM9713_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 = 4;
  77. module_param(delay, int, 0);
  78. MODULE_PARM_DESC(delay, "Set adc sample delay.");
  79. /*
  80. * Set adc mask function.
  81. *
  82. * Sources of glitch noise, such as signals driving an LCD display, may feed
  83. * through to the touch screen plates and affect measurement accuracy. In
  84. * order to minimise this, a signal may be applied to the MASK pin to delay or
  85. * synchronise the sampling.
  86. *
  87. * 0 = No delay or sync
  88. * 1 = High on pin stops conversions
  89. * 2 = Edge triggered, edge on pin delays conversion by delay param (above)
  90. * 3 = Edge triggered, edge on pin starts conversion after delay param
  91. */
  92. static int mask;
  93. module_param(mask, int, 0);
  94. MODULE_PARM_DESC(mask, "Set adc mask function.");
  95. /*
  96. * Coordinate Polling Enable.
  97. *
  98. * Set to 1 to enable coordinate polling. e.g. x,y[,p] is sampled together
  99. * for every poll.
  100. */
  101. static int coord;
  102. module_param(coord, int, 0);
  103. MODULE_PARM_DESC(coord, "Polling coordinate mode");
  104. /*
  105. * ADC sample delay times in uS
  106. */
  107. static const int delay_table[] = {
  108. 21, /* 1 AC97 Link frames */
  109. 42, /* 2 */
  110. 84, /* 4 */
  111. 167, /* 8 */
  112. 333, /* 16 */
  113. 667, /* 32 */
  114. 1000, /* 48 */
  115. 1333, /* 64 */
  116. 2000, /* 96 */
  117. 2667, /* 128 */
  118. 3333, /* 160 */
  119. 4000, /* 192 */
  120. 4667, /* 224 */
  121. 5333, /* 256 */
  122. 6000, /* 288 */
  123. 0 /* No delay, switch matrix always on */
  124. };
  125. /*
  126. * Delay after issuing a POLL command.
  127. *
  128. * The delay is 3 AC97 link frames + the touchpanel settling delay
  129. */
  130. static inline void poll_delay(int d)
  131. {
  132. udelay(3 * AC97_LINK_FRAME + delay_table[d]);
  133. }
  134. /*
  135. * set up the physical settings of the WM9713
  136. */
  137. static void wm9713_phy_init(struct wm97xx *wm)
  138. {
  139. u16 dig1 = 0, dig2, dig3;
  140. /* default values */
  141. dig2 = WM97XX_DELAY(4) | WM97XX_SLT(5);
  142. dig3 = WM9712_RPU(1);
  143. /* rpu */
  144. if (rpu) {
  145. dig3 &= 0xffc0;
  146. dig3 |= WM9712_RPU(rpu);
  147. dev_info(wm->dev, "setting pen detect pull-up to %d Ohms\n",
  148. 64000 / rpu);
  149. }
  150. /* touchpanel pressure */
  151. if (pil == 2) {
  152. dig3 |= WM9712_PIL;
  153. dev_info(wm->dev,
  154. "setting pressure measurement current to 400uA.");
  155. } else if (pil)
  156. dev_info(wm->dev,
  157. "setting pressure measurement current to 200uA.");
  158. if (!pil)
  159. pressure = 0;
  160. /* sample settling delay */
  161. if (delay < 0 || delay > 15) {
  162. dev_info(wm->dev, "supplied delay out of range.");
  163. delay = 4;
  164. dev_info(wm->dev, "setting adc sample delay to %d u Secs.",
  165. delay_table[delay]);
  166. }
  167. dig2 &= 0xff0f;
  168. dig2 |= WM97XX_DELAY(delay);
  169. /* mask */
  170. dig3 |= ((mask & 0x3) << 4);
  171. if (coord)
  172. dig3 |= WM9713_WAIT;
  173. wm->misc = wm97xx_reg_read(wm, 0x5a);
  174. wm97xx_reg_write(wm, AC97_WM9713_DIG1, dig1);
  175. wm97xx_reg_write(wm, AC97_WM9713_DIG2, dig2);
  176. wm97xx_reg_write(wm, AC97_WM9713_DIG3, dig3);
  177. wm97xx_reg_write(wm, AC97_GPIO_STICKY, 0x0);
  178. }
  179. static void wm9713_dig_enable(struct wm97xx *wm, int enable)
  180. {
  181. u16 val;
  182. if (enable) {
  183. val = wm97xx_reg_read(wm, AC97_EXTENDED_MID);
  184. wm97xx_reg_write(wm, AC97_EXTENDED_MID, val & 0x7fff);
  185. wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig[2] |
  186. WM97XX_PRP_DET_DIG);
  187. wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD); /* dummy read */
  188. } else {
  189. wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig[2] &
  190. ~WM97XX_PRP_DET_DIG);
  191. val = wm97xx_reg_read(wm, AC97_EXTENDED_MID);
  192. wm97xx_reg_write(wm, AC97_EXTENDED_MID, val | 0x8000);
  193. }
  194. }
  195. static void wm9713_dig_restore(struct wm97xx *wm)
  196. {
  197. wm97xx_reg_write(wm, AC97_WM9713_DIG1, wm->dig_save[0]);
  198. wm97xx_reg_write(wm, AC97_WM9713_DIG2, wm->dig_save[1]);
  199. wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig_save[2]);
  200. }
  201. static void wm9713_aux_prepare(struct wm97xx *wm)
  202. {
  203. memcpy(wm->dig_save, wm->dig, sizeof(wm->dig));
  204. wm97xx_reg_write(wm, AC97_WM9713_DIG1, 0);
  205. wm97xx_reg_write(wm, AC97_WM9713_DIG2, 0);
  206. wm97xx_reg_write(wm, AC97_WM9713_DIG3, WM97XX_PRP_DET_DIG);
  207. }
  208. static inline int is_pden(struct wm97xx *wm)
  209. {
  210. return wm->dig[2] & WM9713_PDEN;
  211. }
  212. /*
  213. * Read a sample from the WM9713 adc in polling mode.
  214. */
  215. static int wm9713_poll_sample(struct wm97xx *wm, int adcsel, int *sample)
  216. {
  217. u16 dig1;
  218. int timeout = 5 * delay;
  219. if (!wm->pen_probably_down) {
  220. u16 data = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
  221. if (!(data & WM97XX_PEN_DOWN))
  222. return RC_PENUP;
  223. wm->pen_probably_down = 1;
  224. }
  225. /* set up digitiser */
  226. if (adcsel & 0x8000)
  227. adcsel = 1 << ((adcsel & 0x7fff) + 3);
  228. dig1 = wm97xx_reg_read(wm, AC97_WM9713_DIG1);
  229. dig1 &= ~WM9713_ADCSEL_MASK;
  230. if (wm->mach_ops && wm->mach_ops->pre_sample)
  231. wm->mach_ops->pre_sample(adcsel);
  232. wm97xx_reg_write(wm, AC97_WM9713_DIG1, dig1 | adcsel | WM9713_POLL);
  233. /* wait 3 AC97 time slots + delay for conversion */
  234. poll_delay(delay);
  235. /* wait for POLL to go low */
  236. while ((wm97xx_reg_read(wm, AC97_WM9713_DIG1) & WM9713_POLL) &&
  237. timeout) {
  238. udelay(AC97_LINK_FRAME);
  239. timeout--;
  240. }
  241. if (timeout <= 0) {
  242. /* If PDEN is set, we can get a timeout when pen goes up */
  243. if (is_pden(wm))
  244. wm->pen_probably_down = 0;
  245. else
  246. dev_dbg(wm->dev, "adc sample timeout");
  247. return RC_PENUP;
  248. }
  249. *sample = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
  250. if (wm->mach_ops && wm->mach_ops->post_sample)
  251. wm->mach_ops->post_sample(adcsel);
  252. /* check we have correct sample */
  253. if ((*sample & WM97XX_ADCSRC_MASK) != ffs(adcsel >> 1) << 12) {
  254. dev_dbg(wm->dev, "adc wrong sample, read %x got %x", adcsel,
  255. *sample & WM97XX_ADCSRC_MASK);
  256. return RC_PENUP;
  257. }
  258. if (!(*sample & WM97XX_PEN_DOWN)) {
  259. wm->pen_probably_down = 0;
  260. return RC_PENUP;
  261. }
  262. return RC_VALID;
  263. }
  264. /*
  265. * Read a coordinate from the WM9713 adc in polling mode.
  266. */
  267. static int wm9713_poll_coord(struct wm97xx *wm, struct wm97xx_data *data)
  268. {
  269. u16 dig1;
  270. int timeout = 5 * delay;
  271. if (!wm->pen_probably_down) {
  272. u16 val = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
  273. if (!(val & WM97XX_PEN_DOWN))
  274. return RC_PENUP;
  275. wm->pen_probably_down = 1;
  276. }
  277. /* set up digitiser */
  278. dig1 = wm97xx_reg_read(wm, AC97_WM9713_DIG1);
  279. dig1 &= ~WM9713_ADCSEL_MASK;
  280. if (pil)
  281. dig1 |= WM9713_ADCSEL_PRES;
  282. if (wm->mach_ops && wm->mach_ops->pre_sample)
  283. wm->mach_ops->pre_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y);
  284. wm97xx_reg_write(wm, AC97_WM9713_DIG1,
  285. dig1 | WM9713_POLL | WM9713_COO);
  286. /* wait 3 AC97 time slots + delay for conversion */
  287. poll_delay(delay);
  288. data->x = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
  289. /* wait for POLL to go low */
  290. while ((wm97xx_reg_read(wm, AC97_WM9713_DIG1) & WM9713_POLL)
  291. && timeout) {
  292. udelay(AC97_LINK_FRAME);
  293. timeout--;
  294. }
  295. if (timeout <= 0) {
  296. /* If PDEN is set, we can get a timeout when pen goes up */
  297. if (is_pden(wm))
  298. wm->pen_probably_down = 0;
  299. else
  300. dev_dbg(wm->dev, "adc sample timeout");
  301. return RC_PENUP;
  302. }
  303. /* read back data */
  304. data->y = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
  305. if (pil)
  306. data->p = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
  307. else
  308. data->p = DEFAULT_PRESSURE;
  309. if (wm->mach_ops && wm->mach_ops->post_sample)
  310. wm->mach_ops->post_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y);
  311. /* check we have correct sample */
  312. if (!(data->x & WM97XX_ADCSEL_X) || !(data->y & WM97XX_ADCSEL_Y))
  313. goto err;
  314. if (pil && !(data->p & WM97XX_ADCSEL_PRES))
  315. goto err;
  316. if (!(data->x & WM97XX_PEN_DOWN) || !(data->y & WM97XX_PEN_DOWN)) {
  317. wm->pen_probably_down = 0;
  318. return RC_PENUP;
  319. }
  320. return RC_VALID;
  321. err:
  322. return 0;
  323. }
  324. /*
  325. * Sample the WM9713 touchscreen in polling mode
  326. */
  327. static int wm9713_poll_touch(struct wm97xx *wm, struct wm97xx_data *data)
  328. {
  329. int rc;
  330. if (coord) {
  331. rc = wm9713_poll_coord(wm, data);
  332. if (rc != RC_VALID)
  333. return rc;
  334. } else {
  335. rc = wm9713_poll_sample(wm, WM9713_ADCSEL_X, &data->x);
  336. if (rc != RC_VALID)
  337. return rc;
  338. rc = wm9713_poll_sample(wm, WM9713_ADCSEL_Y, &data->y);
  339. if (rc != RC_VALID)
  340. return rc;
  341. if (pil) {
  342. rc = wm9713_poll_sample(wm, WM9713_ADCSEL_PRES,
  343. &data->p);
  344. if (rc != RC_VALID)
  345. return rc;
  346. } else
  347. data->p = DEFAULT_PRESSURE;
  348. }
  349. return RC_VALID;
  350. }
  351. /*
  352. * Enable WM9713 continuous mode, i.e. touch data is streamed across
  353. * an AC97 slot
  354. */
  355. static int wm9713_acc_enable(struct wm97xx *wm, int enable)
  356. {
  357. u16 dig1, dig2, dig3;
  358. int ret = 0;
  359. dig1 = wm->dig[0];
  360. dig2 = wm->dig[1];
  361. dig3 = wm->dig[2];
  362. if (enable) {
  363. /* continous mode */
  364. if (wm->mach_ops->acc_startup &&
  365. (ret = wm->mach_ops->acc_startup(wm)) < 0)
  366. return ret;
  367. dig1 &= ~WM9713_ADCSEL_MASK;
  368. dig1 |= WM9713_CTC | WM9713_COO | WM9713_ADCSEL_X |
  369. WM9713_ADCSEL_Y;
  370. if (pil)
  371. dig1 |= WM9713_ADCSEL_PRES;
  372. dig2 &= ~(WM97XX_DELAY_MASK | WM97XX_SLT_MASK |
  373. WM97XX_CM_RATE_MASK);
  374. dig2 |= WM97XX_SLEN | WM97XX_DELAY(delay) |
  375. WM97XX_SLT(wm->acc_slot) | WM97XX_RATE(wm->acc_rate);
  376. dig3 |= WM9713_PDEN;
  377. } else {
  378. dig1 &= ~(WM9713_CTC | WM9713_COO);
  379. dig2 &= ~WM97XX_SLEN;
  380. dig3 &= ~WM9713_PDEN;
  381. if (wm->mach_ops->acc_shutdown)
  382. wm->mach_ops->acc_shutdown(wm);
  383. }
  384. wm97xx_reg_write(wm, AC97_WM9713_DIG1, dig1);
  385. wm97xx_reg_write(wm, AC97_WM9713_DIG2, dig2);
  386. wm97xx_reg_write(wm, AC97_WM9713_DIG3, dig3);
  387. return ret;
  388. }
  389. struct wm97xx_codec_drv wm9713_codec = {
  390. .id = WM9713_ID2,
  391. .name = "wm9713",
  392. .poll_sample = wm9713_poll_sample,
  393. .poll_touch = wm9713_poll_touch,
  394. .acc_enable = wm9713_acc_enable,
  395. .phy_init = wm9713_phy_init,
  396. .dig_enable = wm9713_dig_enable,
  397. .dig_restore = wm9713_dig_restore,
  398. .aux_prepare = wm9713_aux_prepare,
  399. };
  400. EXPORT_SYMBOL_GPL(wm9713_codec);
  401. /* Module information */
  402. MODULE_AUTHOR("Liam Girdwood <liam.girdwood@wolfsonmicro.com>");
  403. MODULE_DESCRIPTION("WM9713 Touch Screen Driver");
  404. MODULE_LICENSE("GPL");