wm9713.c 12 KB

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