1 // SPDX-License-Identifier: GPL-2.0-or-later
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
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.
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.
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
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.
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
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
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 */
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
)
154 u16 dig2
= WM97XX_RPR
| WM9712_RPU(1);
159 dig2
|= WM9712_RPU(rpu
);
160 dev_dbg(wm
->dev
, "setting pen detect pull-up to %d Ohms\n",
164 /* WM9712 five wire */
167 dev_dbg(wm
->dev
, "setting 5-wire touchscreen mode.\n");
170 dev_warn(wm
->dev
, "pressure measurement is not "
171 "supported in 5-wire mode\n");
176 /* touchpanel pressure current*/
180 "setting pressure measurement current to 400uA.\n");
183 "setting pressure measurement current to 200uA.\n");
187 /* polling mode sample settling delay */
188 if (delay
< 0 || delay
> 15) {
189 dev_dbg(wm
->dev
, "supplied delay out of range.\n");
193 dig1
|= WM97XX_DELAY(delay
);
194 dev_dbg(wm
->dev
, "setting adc sample delay to %d u Secs.\n",
198 dig2
|= ((mask
& 0x3) << 6);
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 */
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];
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 */
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
))
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 */
271 /* wait for POLL to go low */
272 while ((wm97xx_reg_read(wm
, AC97_WM97XX_DIGITISER1
) & WM97XX_POLL
)
274 udelay(AC97_LINK_FRAME
);
279 /* If PDEN is set, we can get a timeout when pen goes up */
281 wm
->pen_probably_down
= 0;
283 dev_dbg(wm
->dev
, "adc sample timeout\n");
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
);
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;
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
))
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 */
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
)
338 udelay(AC97_LINK_FRAME
);
343 /* If PDEN is set, we can get a timeout when pen goes up */
345 wm
->pen_probably_down
= 0;
347 dev_dbg(wm
->dev
, "adc sample timeout\n");
351 /* read back y data */
352 data
->y
= wm97xx_reg_read(wm
, AC97_WM97XX_DIGITISER_RD
);
354 data
->p
= wm97xx_reg_read(wm
, AC97_WM97XX_DIGITISER_RD
);
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
))
364 if (pil
&& !(data
->p
& WM97XX_ADCSEL_PRES
))
367 if (!(data
->x
& WM97XX_PEN_DOWN
) || !(data
->y
& WM97XX_PEN_DOWN
)) {
368 wm
->pen_probably_down
= 0;
377 * Sample the WM9712 touchscreen in polling mode
379 static int wm9712_poll_touch(struct wm97xx
*wm
, struct wm97xx_data
*data
)
384 rc
= wm9712_poll_coord(wm
, data
);
388 rc
= wm9712_poll_sample(wm
, WM97XX_ADCSEL_X
| WM97XX_PEN_DOWN
,
393 rc
= wm9712_poll_sample(wm
, WM97XX_ADCSEL_Y
| WM97XX_PEN_DOWN
,
398 if (pil
&& !five_wire
) {
399 rc
= wm9712_poll_sample(wm
, WM97XX_ADCSEL_PRES
| WM97XX_PEN_DOWN
,
404 data
->p
= DEFAULT_PRESSURE
;
410 * Enable WM9712 continuous mode, i.e. touch data is streamed across
413 static int wm9712_acc_enable(struct wm97xx
*wm
, int enable
)
422 /* continuous mode */
423 if (wm
->mach_ops
->acc_startup
) {
424 ret
= wm
->mach_ops
->acc_startup(wm
);
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
);
435 dig1
|= WM97XX_ADCSEL_PRES
;
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
);
450 struct wm97xx_codec_drv wm9712_codec
= {
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");