wm9713.c 12 KB

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