Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / input / touchscreen / wm9712.c
blob6947714dfefa1817e107b03df66d4228723610b6
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * wm9712.c -- Codec driver for Wolfson WM9712 AC97 Codecs.
5 * Copyright 2003, 2004, 2005, 2006, 2007 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>
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>
20 #define TS_NAME "wm97xx"
21 #define WM9712_VERSION "1.00"
22 #define DEFAULT_PRESSURE 0xb0c0
25 * Module parameters
29 * Set internal pull up for pen detect.
31 * Pull up is in the range 1.02k (least sensitive) to 64k (most sensitive)
32 * i.e. pull up resistance = 64k Ohms / rpu.
34 * Adjust this value if you are having problems with pen detect not
35 * detecting any down event.
37 static int rpu = 8;
38 module_param(rpu, int, 0);
39 MODULE_PARM_DESC(rpu, "Set internal pull up resistor for pen detect.");
42 * Set current used for pressure measurement.
44 * Set pil = 2 to use 400uA
45 * pil = 1 to use 200uA and
46 * pil = 0 to disable pressure measurement.
48 * This is used to increase the range of values returned by the adc
49 * when measureing touchpanel pressure.
51 static int pil;
52 module_param(pil, int, 0);
53 MODULE_PARM_DESC(pil, "Set current used for pressure measurement.");
56 * Set threshold for pressure measurement.
58 * Pen down pressure below threshold is ignored.
60 static int pressure = DEFAULT_PRESSURE & 0xfff;
61 module_param(pressure, int, 0);
62 MODULE_PARM_DESC(pressure, "Set threshold for pressure measurement.");
65 * Set adc sample delay.
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.
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.
76 static int delay = 3;
77 module_param(delay, int, 0);
78 MODULE_PARM_DESC(delay, "Set adc sample delay.");
81 * Set five_wire = 1 to use a 5 wire touchscreen.
83 * NOTE: Five wire mode does not allow for readback of pressure.
85 static int five_wire;
86 module_param(five_wire, int, 0);
87 MODULE_PARM_DESC(five_wire, "Set to '1' to use 5-wire touchscreen.");
90 * Set adc mask function.
92 * Sources of glitch noise, such as signals driving an LCD display, may feed
93 * through to the touch screen plates and affect measurement accuracy. In
94 * order to minimise this, a signal may be applied to the MASK pin to delay or
95 * synchronise the sampling.
97 * 0 = No delay or sync
98 * 1 = High on pin stops conversions
99 * 2 = Edge triggered, edge on pin delays conversion by delay param (above)
100 * 3 = Edge triggered, edge on pin starts conversion after delay param
102 static int mask;
103 module_param(mask, int, 0);
104 MODULE_PARM_DESC(mask, "Set adc mask function.");
107 * Coordinate Polling Enable.
109 * Set to 1 to enable coordinate polling. e.g. x,y[,p] is sampled together
110 * for every poll.
112 static int coord;
113 module_param(coord, int, 0);
114 MODULE_PARM_DESC(coord, "Polling coordinate mode");
117 * ADC sample delay times in uS
119 static const int delay_table[] = {
120 21, /* 1 AC97 Link frames */
121 42, /* 2 */
122 84, /* 4 */
123 167, /* 8 */
124 333, /* 16 */
125 667, /* 32 */
126 1000, /* 48 */
127 1333, /* 64 */
128 2000, /* 96 */
129 2667, /* 128 */
130 3333, /* 160 */
131 4000, /* 192 */
132 4667, /* 224 */
133 5333, /* 256 */
134 6000, /* 288 */
135 0 /* No delay, switch matrix always on */
139 * Delay after issuing a POLL command.
141 * The delay is 3 AC97 link frames + the touchpanel settling delay
143 static inline void poll_delay(int d)
145 udelay(3 * AC97_LINK_FRAME + delay_table[d]);
149 * set up the physical settings of the WM9712
151 static void wm9712_phy_init(struct wm97xx *wm)
153 u16 dig1 = 0;
154 u16 dig2 = WM97XX_RPR | WM9712_RPU(1);
156 /* WM9712 rpu */
157 if (rpu) {
158 dig2 &= 0xffc0;
159 dig2 |= WM9712_RPU(rpu);
160 dev_dbg(wm->dev, "setting pen detect pull-up to %d Ohms\n",
161 64000 / rpu);
164 /* WM9712 five wire */
165 if (five_wire) {
166 dig2 |= WM9712_45W;
167 dev_dbg(wm->dev, "setting 5-wire touchscreen mode.\n");
169 if (pil) {
170 dev_warn(wm->dev, "pressure measurement is not "
171 "supported in 5-wire mode\n");
172 pil = 0;
176 /* touchpanel pressure current*/
177 if (pil == 2) {
178 dig2 |= WM9712_PIL;
179 dev_dbg(wm->dev,
180 "setting pressure measurement current to 400uA.\n");
181 } else if (pil)
182 dev_dbg(wm->dev,
183 "setting pressure measurement current to 200uA.\n");
184 if (!pil)
185 pressure = 0;
187 /* polling mode sample settling delay */
188 if (delay < 0 || delay > 15) {
189 dev_dbg(wm->dev, "supplied delay out of range.\n");
190 delay = 4;
192 dig1 &= 0xff0f;
193 dig1 |= WM97XX_DELAY(delay);
194 dev_dbg(wm->dev, "setting adc sample delay to %d u Secs.\n",
195 delay_table[delay]);
197 /* mask */
198 dig2 |= ((mask & 0x3) << 6);
199 if (mask) {
200 u16 reg;
201 /* Set GPIO4 as Mask Pin*/
202 reg = wm97xx_reg_read(wm, AC97_MISC_AFE);
203 wm97xx_reg_write(wm, AC97_MISC_AFE, reg | WM97XX_GPIO_4);
204 reg = wm97xx_reg_read(wm, AC97_GPIO_CFG);
205 wm97xx_reg_write(wm, AC97_GPIO_CFG, reg | WM97XX_GPIO_4);
208 /* wait - coord mode */
209 if (coord)
210 dig2 |= WM9712_WAIT;
212 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, dig1);
213 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, dig2);
216 static void wm9712_dig_enable(struct wm97xx *wm, int enable)
218 u16 dig2 = wm->dig[2];
220 if (enable) {
221 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2,
222 dig2 | WM97XX_PRP_DET_DIG);
223 wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD); /* dummy read */
224 } else
225 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2,
226 dig2 & ~WM97XX_PRP_DET_DIG);
229 static void wm9712_aux_prepare(struct wm97xx *wm)
231 memcpy(wm->dig_save, wm->dig, sizeof(wm->dig));
232 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, 0);
233 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, WM97XX_PRP_DET_DIG);
236 static void wm9712_dig_restore(struct wm97xx *wm)
238 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, wm->dig_save[1]);
239 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, wm->dig_save[2]);
242 static inline int is_pden(struct wm97xx *wm)
244 return wm->dig[2] & WM9712_PDEN;
248 * Read a sample from the WM9712 adc in polling mode.
250 static int wm9712_poll_sample(struct wm97xx *wm, int adcsel, int *sample)
252 int timeout = 5 * delay;
253 bool wants_pen = adcsel & WM97XX_PEN_DOWN;
255 if (wants_pen && !wm->pen_probably_down) {
256 u16 data = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
257 if (!(data & WM97XX_PEN_DOWN))
258 return RC_PENUP;
259 wm->pen_probably_down = 1;
262 /* set up digitiser */
263 if (wm->mach_ops && wm->mach_ops->pre_sample)
264 wm->mach_ops->pre_sample(adcsel);
265 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, (adcsel & WM97XX_ADCSEL_MASK)
266 | WM97XX_POLL | WM97XX_DELAY(delay));
268 /* wait 3 AC97 time slots + delay for conversion */
269 poll_delay(delay);
271 /* wait for POLL to go low */
272 while ((wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER1) & WM97XX_POLL)
273 && timeout) {
274 udelay(AC97_LINK_FRAME);
275 timeout--;
278 if (timeout <= 0) {
279 /* If PDEN is set, we can get a timeout when pen goes up */
280 if (is_pden(wm))
281 wm->pen_probably_down = 0;
282 else
283 dev_dbg(wm->dev, "adc sample timeout\n");
284 return RC_PENUP;
287 *sample = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
288 if (wm->mach_ops && wm->mach_ops->post_sample)
289 wm->mach_ops->post_sample(adcsel);
291 /* check we have correct sample */
292 if ((*sample ^ adcsel) & WM97XX_ADCSEL_MASK) {
293 dev_dbg(wm->dev, "adc wrong sample, wanted %x got %x\n",
294 adcsel & WM97XX_ADCSEL_MASK,
295 *sample & WM97XX_ADCSEL_MASK);
296 return RC_AGAIN;
299 if (wants_pen && !(*sample & WM97XX_PEN_DOWN)) {
300 /* Sometimes it reads a wrong value the first time. */
301 *sample = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
302 if (!(*sample & WM97XX_PEN_DOWN)) {
303 wm->pen_probably_down = 0;
304 return RC_PENUP;
308 return RC_VALID;
312 * Read a coord from the WM9712 adc in polling mode.
314 static int wm9712_poll_coord(struct wm97xx *wm, struct wm97xx_data *data)
316 int timeout = 5 * delay;
318 if (!wm->pen_probably_down) {
319 u16 data_rd = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
320 if (!(data_rd & WM97XX_PEN_DOWN))
321 return RC_PENUP;
322 wm->pen_probably_down = 1;
325 /* set up digitiser */
326 if (wm->mach_ops && wm->mach_ops->pre_sample)
327 wm->mach_ops->pre_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y);
329 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1,
330 WM97XX_COO | WM97XX_POLL | WM97XX_DELAY(delay));
332 /* wait 3 AC97 time slots + delay for conversion and read x */
333 poll_delay(delay);
334 data->x = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
335 /* wait for POLL to go low */
336 while ((wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER1) & WM97XX_POLL)
337 && timeout) {
338 udelay(AC97_LINK_FRAME);
339 timeout--;
342 if (timeout <= 0) {
343 /* If PDEN is set, we can get a timeout when pen goes up */
344 if (is_pden(wm))
345 wm->pen_probably_down = 0;
346 else
347 dev_dbg(wm->dev, "adc sample timeout\n");
348 return RC_PENUP;
351 /* read back y data */
352 data->y = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
353 if (pil)
354 data->p = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
355 else
356 data->p = DEFAULT_PRESSURE;
358 if (wm->mach_ops && wm->mach_ops->post_sample)
359 wm->mach_ops->post_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y);
361 /* check we have correct sample */
362 if (!(data->x & WM97XX_ADCSEL_X) || !(data->y & WM97XX_ADCSEL_Y))
363 goto err;
364 if (pil && !(data->p & WM97XX_ADCSEL_PRES))
365 goto err;
367 if (!(data->x & WM97XX_PEN_DOWN) || !(data->y & WM97XX_PEN_DOWN)) {
368 wm->pen_probably_down = 0;
369 return RC_PENUP;
371 return RC_VALID;
372 err:
373 return 0;
377 * Sample the WM9712 touchscreen in polling mode
379 static int wm9712_poll_touch(struct wm97xx *wm, struct wm97xx_data *data)
381 int rc;
383 if (coord) {
384 rc = wm9712_poll_coord(wm, data);
385 if (rc != RC_VALID)
386 return rc;
387 } else {
388 rc = wm9712_poll_sample(wm, WM97XX_ADCSEL_X | WM97XX_PEN_DOWN,
389 &data->x);
390 if (rc != RC_VALID)
391 return rc;
393 rc = wm9712_poll_sample(wm, WM97XX_ADCSEL_Y | WM97XX_PEN_DOWN,
394 &data->y);
395 if (rc != RC_VALID)
396 return rc;
398 if (pil && !five_wire) {
399 rc = wm9712_poll_sample(wm, WM97XX_ADCSEL_PRES | WM97XX_PEN_DOWN,
400 &data->p);
401 if (rc != RC_VALID)
402 return rc;
403 } else
404 data->p = DEFAULT_PRESSURE;
406 return RC_VALID;
410 * Enable WM9712 continuous mode, i.e. touch data is streamed across
411 * an AC97 slot
413 static int wm9712_acc_enable(struct wm97xx *wm, int enable)
415 u16 dig1, dig2;
416 int ret = 0;
418 dig1 = wm->dig[1];
419 dig2 = wm->dig[2];
421 if (enable) {
422 /* continuous mode */
423 if (wm->mach_ops->acc_startup) {
424 ret = wm->mach_ops->acc_startup(wm);
425 if (ret < 0)
426 return ret;
428 dig1 &= ~(WM97XX_CM_RATE_MASK | WM97XX_ADCSEL_MASK |
429 WM97XX_DELAY_MASK | WM97XX_SLT_MASK);
430 dig1 |= WM97XX_CTC | WM97XX_COO | WM97XX_SLEN |
431 WM97XX_DELAY(delay) |
432 WM97XX_SLT(wm->acc_slot) |
433 WM97XX_RATE(wm->acc_rate);
434 if (pil)
435 dig1 |= WM97XX_ADCSEL_PRES;
436 dig2 |= WM9712_PDEN;
437 } else {
438 dig1 &= ~(WM97XX_CTC | WM97XX_COO | WM97XX_SLEN);
439 dig2 &= ~WM9712_PDEN;
440 if (wm->mach_ops->acc_shutdown)
441 wm->mach_ops->acc_shutdown(wm);
444 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, dig1);
445 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, dig2);
447 return 0;
450 struct wm97xx_codec_drv wm9712_codec = {
451 .id = WM9712_ID2,
452 .name = "wm9712",
453 .poll_sample = wm9712_poll_sample,
454 .poll_touch = wm9712_poll_touch,
455 .acc_enable = wm9712_acc_enable,
456 .phy_init = wm9712_phy_init,
457 .dig_enable = wm9712_dig_enable,
458 .dig_restore = wm9712_dig_restore,
459 .aux_prepare = wm9712_aux_prepare,
461 EXPORT_SYMBOL_GPL(wm9712_codec);
463 /* Module information */
464 MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
465 MODULE_DESCRIPTION("WM9712 Touch Screen Driver");
466 MODULE_LICENSE("GPL");