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