wm9713.c 12 KB

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