1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * wm9712.c -- Codec driver for Wolfson WM9712 AC97 Codecs.
4 *
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>
10 */
11
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
20#define TS_NAME "wm97xx"
21#define WM9712_VERSION "1.00"
22#define DEFAULT_PRESSURE 0xb0c0
23
24/*
25 * Module parameters
26 */
27
28/*
29 * Set internal pull up for pen detect.
30 *
31 * Pull up is in the range 1.02k (least sensitive) to 64k (most sensitive)
32 * i.e. pull up resistance = 64k Ohms / rpu.
33 *
34 * Adjust this value if you are having problems with pen detect not
35 * detecting any down event.
36 */
37static int rpu = 8;
38module_param(rpu, int, 0);
39MODULE_PARM_DESC(rpu, "Set internal pull up resistor for pen detect.");
40
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 */
51static int pil;
52module_param(pil, int, 0);
53MODULE_PARM_DESC(pil, "Set current used for pressure measurement.");
54
55/*
56 * Set threshold for pressure measurement.
57 *
58 * Pen down pressure below threshold is ignored.
59 */
60static int pressure = DEFAULT_PRESSURE & 0xfff;
61module_param(pressure, int, 0);
62MODULE_PARM_DESC(pressure, "Set threshold for pressure measurement.");
63
64/*
65 * Set adc sample delay.
66 *
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.
70 *
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.
75 */
76static int delay = 3;
77module_param(delay, int, 0);
78MODULE_PARM_DESC(delay, "Set adc sample delay.");
79
80/*
81 * Set five_wire = 1 to use a 5 wire touchscreen.
82 *
83 * NOTE: Five wire mode does not allow for readback of pressure.
84 */
85static int five_wire;
86module_param(five_wire, int, 0);
87MODULE_PARM_DESC(five_wire, "Set to '1' to use 5-wire touchscreen.");
88
89/*
90 * Set adc mask function.
91 *
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.
96 *
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
101 */
102static int mask;
103module_param(mask, int, 0);
104MODULE_PARM_DESC(mask, "Set adc mask function.");
105
106/*
107 * Coordinate Polling Enable.
108 *
109 * Set to 1 to enable coordinate polling. e.g. x,y[,p] is sampled together
110 * for every poll.
111 */
112static int coord;
113module_param(coord, int, 0);
114MODULE_PARM_DESC(coord, "Polling coordinate mode");
115
116/*
117 * ADC sample delay times in uS
118 */
119static 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 */
136};
137
138/*
139 * Delay after issuing a POLL command.
140 *
141 * The delay is 3 AC97 link frames + the touchpanel settling delay
142 */
143static inline void poll_delay(int d)
144{
145 udelay(3 * AC97_LINK_FRAME + delay_table[d]);
146}
147
148/*
149 * set up the physical settings of the WM9712
150 */
151static void wm9712_phy_init(struct wm97xx *wm)
152{
153 u16 dig1 = 0;
154 u16 dig2 = WM97XX_RPR | WM9712_RPU(1);
155
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);
162 }
163
164 /* WM9712 five wire */
165 if (five_wire) {
166 dig2 |= WM9712_45W;
167 dev_dbg(wm->dev, "setting 5-wire touchscreen mode.\n");
168
169 if (pil) {
170 dev_warn(wm->dev, "pressure measurement is not "
171 "supported in 5-wire mode\n");
172 pil = 0;
173 }
174 }
175
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;
186
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;
191 }
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]);
196
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, val: reg | WM97XX_GPIO_4);
204 reg = wm97xx_reg_read(wm, AC97_GPIO_CFG);
205 wm97xx_reg_write(wm, AC97_GPIO_CFG, val: reg | WM97XX_GPIO_4);
206 }
207
208 /* wait - coord mode */
209 if (coord)
210 dig2 |= WM9712_WAIT;
211
212 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, val: dig1);
213 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, val: dig2);
214}
215
216static void wm9712_dig_enable(struct wm97xx *wm, int enable)
217{
218 u16 dig2 = wm->dig[2];
219
220 if (enable) {
221 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2,
222 val: 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 val: dig2 & ~WM97XX_PRP_DET_DIG);
227}
228
229static void wm9712_aux_prepare(struct wm97xx *wm)
230{
231 memcpy(wm->dig_save, wm->dig, sizeof(wm->dig));
232 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, val: 0);
233 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, WM97XX_PRP_DET_DIG);
234}
235
236static void wm9712_dig_restore(struct wm97xx *wm)
237{
238 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, val: wm->dig_save[1]);
239 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, val: wm->dig_save[2]);
240}
241
242static inline int is_pden(struct wm97xx *wm)
243{
244 return wm->dig[2] & WM9712_PDEN;
245}
246
247/*
248 * Read a sample from the WM9712 adc in polling mode.
249 */
250static int wm9712_poll_sample(struct wm97xx *wm, int adcsel, int *sample)
251{
252 int timeout = 5 * delay;
253 bool wants_pen = adcsel & WM97XX_PEN_DOWN;
254
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;
260 }
261
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, val: (adcsel & WM97XX_ADCSEL_MASK)
266 | WM97XX_POLL | WM97XX_DELAY(delay));
267
268 /* wait 3 AC97 time slots + delay for conversion */
269 poll_delay(d: delay);
270
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--;
276 }
277
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;
285 }
286
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);
290
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;
297 }
298
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;
305 }
306 }
307
308 return RC_VALID;
309}
310
311/*
312 * Read a coord from the WM9712 adc in polling mode.
313 */
314static int wm9712_poll_coord(struct wm97xx *wm, struct wm97xx_data *data)
315{
316 int timeout = 5 * delay;
317
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;
323 }
324
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);
328
329 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1,
330 WM97XX_COO | WM97XX_POLL | WM97XX_DELAY(delay));
331
332 /* wait 3 AC97 time slots + delay for conversion and read x */
333 poll_delay(d: 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--;
340 }
341
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;
349 }
350
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;
357
358 if (wm->mach_ops && wm->mach_ops->post_sample)
359 wm->mach_ops->post_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y);
360
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;
366
367 if (!(data->x & WM97XX_PEN_DOWN) || !(data->y & WM97XX_PEN_DOWN)) {
368 wm->pen_probably_down = 0;
369 return RC_PENUP;
370 }
371 return RC_VALID;
372err:
373 return 0;
374}
375
376/*
377 * Sample the WM9712 touchscreen in polling mode
378 */
379static int wm9712_poll_touch(struct wm97xx *wm, struct wm97xx_data *data)
380{
381 int rc;
382
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 sample: &data->x);
390 if (rc != RC_VALID)
391 return rc;
392
393 rc = wm9712_poll_sample(wm, WM97XX_ADCSEL_Y | WM97XX_PEN_DOWN,
394 sample: &data->y);
395 if (rc != RC_VALID)
396 return rc;
397
398 if (pil && !five_wire) {
399 rc = wm9712_poll_sample(wm, WM97XX_ADCSEL_PRES | WM97XX_PEN_DOWN,
400 sample: &data->p);
401 if (rc != RC_VALID)
402 return rc;
403 } else
404 data->p = DEFAULT_PRESSURE;
405 }
406 return RC_VALID;
407}
408
409/*
410 * Enable WM9712 continuous mode, i.e. touch data is streamed across
411 * an AC97 slot
412 */
413static int wm9712_acc_enable(struct wm97xx *wm, int enable)
414{
415 u16 dig1, dig2;
416 int ret = 0;
417
418 dig1 = wm->dig[1];
419 dig2 = wm->dig[2];
420
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;
427 }
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);
442 }
443
444 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, val: dig1);
445 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, val: dig2);
446
447 return 0;
448}
449
450struct 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,
460};
461EXPORT_SYMBOL_GPL(wm9712_codec);
462
463/* Module information */
464MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
465MODULE_DESCRIPTION("WM9712 Touch Screen Driver");
466MODULE_LICENSE("GPL");
467

source code of linux/drivers/input/touchscreen/wm9712.c