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 <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 WM9713_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 = 4;
  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 WM9713
  142. */
  143. static void wm9713_phy_init(struct wm97xx *wm)
  144. {
  145. u16 dig1 = 0, dig2, dig3;
  146. /* default values */
  147. dig2 = WM97XX_DELAY(4) | WM97XX_SLT(5);
  148. dig3 = WM9712_RPU(1);
  149. /* rpu */
  150. if (rpu) {
  151. dig3 &= 0xffc0;
  152. dig3 |= WM9712_RPU(rpu);
  153. dev_info(wm->dev, "setting pen detect pull-up to %d Ohms\n",
  154. 64000 / rpu);
  155. }
  156. /* Five wire panel? */
  157. if (five_wire) {
  158. dig3 |= WM9713_45W;
  159. dev_info(wm->dev, "setting 5-wire touchscreen mode.");
  160. if (pil) {
  161. dev_warn(wm->dev,
  162. "Pressure measurement not supported in 5 "
  163. "wire mode, disabling\n");
  164. pil = 0;
  165. }
  166. }
  167. /* touchpanel pressure */
  168. if (pil == 2) {
  169. dig3 |= WM9712_PIL;
  170. dev_info(wm->dev,
  171. "setting pressure measurement current to 400uA.");
  172. } else if (pil)
  173. dev_info(wm->dev,
  174. "setting pressure measurement current to 200uA.");
  175. if (!pil)
  176. pressure = 0;
  177. /* sample settling delay */
  178. if (delay < 0 || delay > 15) {
  179. dev_info(wm->dev, "supplied delay out of range.");
  180. delay = 4;
  181. dev_info(wm->dev, "setting adc sample delay to %d u Secs.",
  182. delay_table[delay]);
  183. }
  184. dig2 &= 0xff0f;
  185. dig2 |= WM97XX_DELAY(delay);
  186. /* mask */
  187. dig3 |= ((mask & 0x3) << 4);
  188. if (coord)
  189. dig3 |= WM9713_WAIT;
  190. wm->misc = wm97xx_reg_read(wm, 0x5a);
  191. wm97xx_reg_write(wm, AC97_WM9713_DIG1, dig1);
  192. wm97xx_reg_write(wm, AC97_WM9713_DIG2, dig2);
  193. wm97xx_reg_write(wm, AC97_WM9713_DIG3, dig3);
  194. wm97xx_reg_write(wm, AC97_GPIO_STICKY, 0x0);
  195. }
  196. static void wm9713_dig_enable(struct wm97xx *wm, int enable)
  197. {
  198. u16 val;
  199. if (enable) {
  200. val = wm97xx_reg_read(wm, AC97_EXTENDED_MID);
  201. wm97xx_reg_write(wm, AC97_EXTENDED_MID, val & 0x7fff);
  202. wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig[2] |
  203. WM97XX_PRP_DET_DIG);
  204. wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD); /* dummy read */
  205. } else {
  206. wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig[2] &
  207. ~WM97XX_PRP_DET_DIG);
  208. val = wm97xx_reg_read(wm, AC97_EXTENDED_MID);
  209. wm97xx_reg_write(wm, AC97_EXTENDED_MID, val | 0x8000);
  210. }
  211. }
  212. static void wm9713_dig_restore(struct wm97xx *wm)
  213. {
  214. wm97xx_reg_write(wm, AC97_WM9713_DIG1, wm->dig_save[0]);
  215. wm97xx_reg_write(wm, AC97_WM9713_DIG2, wm->dig_save[1]);
  216. wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig_save[2]);
  217. }
  218. static void wm9713_aux_prepare(struct wm97xx *wm)
  219. {
  220. memcpy(wm->dig_save, wm->dig, sizeof(wm->dig));
  221. wm97xx_reg_write(wm, AC97_WM9713_DIG1, 0);
  222. wm97xx_reg_write(wm, AC97_WM9713_DIG2, 0);
  223. wm97xx_reg_write(wm, AC97_WM9713_DIG3, WM97XX_PRP_DET_DIG);
  224. }
  225. static inline int is_pden(struct wm97xx *wm)
  226. {
  227. return wm->dig[2] & WM9713_PDEN;
  228. }
  229. /*
  230. * Read a sample from the WM9713 adc in polling mode.
  231. */
  232. static int wm9713_poll_sample(struct wm97xx *wm, int adcsel, int *sample)
  233. {
  234. u16 dig1;
  235. int timeout = 5 * delay;
  236. bool wants_pen = adcsel & WM97XX_PEN_DOWN;
  237. if (wants_pen && !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. dig1 = wm97xx_reg_read(wm, AC97_WM9713_DIG1);
  245. dig1 &= ~WM9713_ADCSEL_MASK;
  246. /* WM97XX_ADCSEL_* channels need to be converted to WM9713 format */
  247. dig1 |= 1 << ((adcsel & WM97XX_ADCSEL_MASK) >> 12);
  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 | 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 ^ adcsel) & WM97XX_ADCSEL_MASK) {
  272. dev_dbg(wm->dev, "adc wrong sample, wanted %x got %x",
  273. adcsel & WM97XX_ADCSEL_MASK,
  274. *sample & WM97XX_ADCSEL_MASK);
  275. return RC_PENUP;
  276. }
  277. if (wants_pen && !(*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, WM97XX_ADCSEL_X | WM97XX_PEN_DOWN, &data->x);
  355. if (rc != RC_VALID)
  356. return rc;
  357. rc = wm9713_poll_sample(wm, WM97XX_ADCSEL_Y | WM97XX_PEN_DOWN, &data->y);
  358. if (rc != RC_VALID)
  359. return rc;
  360. if (pil) {
  361. rc = wm9713_poll_sample(wm, WM97XX_ADCSEL_PRES | WM97XX_PEN_DOWN,
  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. /* continuous 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 <lrg@slimlogic.co.uk>");
  422. MODULE_DESCRIPTION("WM9713 Touch Screen Driver");
  423. MODULE_LICENSE("GPL");