wm9713.c 12 KB

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