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 * 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 <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 WM9713_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 = 4;
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 WM9713
151 */
152static void wm9713_phy_init(struct wm97xx *wm)
153{
154 u16 dig1 = 0, dig2, dig3;
155
156 /* default values */
157 dig2 = WM97XX_DELAY(4) | WM97XX_SLT(5);
158 dig3 = WM9712_RPU(1);
159
160 /* rpu */
161 if (rpu) {
162 dig3 &= 0xffc0;
163 dig3 |= WM9712_RPU(rpu);
164 dev_info(wm->dev, "setting pen detect pull-up to %d Ohms\n",
165 64000 / rpu);
166 }
167
168 /* Five wire panel? */
169 if (five_wire) {
170 dig3 |= WM9713_45W;
171 dev_info(wm->dev, "setting 5-wire touchscreen mode.");
172
173 if (pil) {
174 dev_warn(wm->dev,
175 "Pressure measurement not supported in 5 "
176 "wire mode, disabling\n");
177 pil = 0;
178 }
179 }
180
181 /* touchpanel pressure */
182 if (pil == 2) {
183 dig3 |= WM9712_PIL;
184 dev_info(wm->dev,
185 "setting pressure measurement current to 400uA.");
186 } else if (pil)
187 dev_info(wm->dev,
188 "setting pressure measurement current to 200uA.");
189 if (!pil)
190 pressure = 0;
191
192 /* sample settling delay */
193 if (delay < 0 || delay > 15) {
194 dev_info(wm->dev, "supplied delay out of range.");
195 delay = 4;
196 dev_info(wm->dev, "setting adc sample delay to %d u Secs.",
197 delay_table[delay]);
198 }
199 dig2 &= 0xff0f;
200 dig2 |= WM97XX_DELAY(delay);
201
202 /* mask */
203 dig3 |= ((mask & 0x3) << 4);
204 if (coord)
205 dig3 |= WM9713_WAIT;
206
207 wm->misc = wm97xx_reg_read(wm, 0x5a);
208
209 wm97xx_reg_write(wm, AC97_WM9713_DIG1, dig1);
210 wm97xx_reg_write(wm, AC97_WM9713_DIG2, dig2);
211 wm97xx_reg_write(wm, AC97_WM9713_DIG3, dig3);
212 wm97xx_reg_write(wm, AC97_GPIO_STICKY, 0x0);
213}
214
215static void wm9713_dig_enable(struct wm97xx *wm, int enable)
216{
217 u16 val;
218
219 if (enable) {
220 val = wm97xx_reg_read(wm, AC97_EXTENDED_MID);
221 wm97xx_reg_write(wm, AC97_EXTENDED_MID, val & 0x7fff);
222 wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig[2] |
223 WM97XX_PRP_DET_DIG);
224 wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD); /* dummy read */
225 } else {
226 wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig[2] &
227 ~WM97XX_PRP_DET_DIG);
228 val = wm97xx_reg_read(wm, AC97_EXTENDED_MID);
229 wm97xx_reg_write(wm, AC97_EXTENDED_MID, val | 0x8000);
230 }
231}
232
233static void wm9713_dig_restore(struct wm97xx *wm)
234{
235 wm97xx_reg_write(wm, AC97_WM9713_DIG1, wm->dig_save[0]);
236 wm97xx_reg_write(wm, AC97_WM9713_DIG2, wm->dig_save[1]);
237 wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig_save[2]);
238}
239
240static void wm9713_aux_prepare(struct wm97xx *wm)
241{
242 memcpy(wm->dig_save, wm->dig, sizeof(wm->dig));
243 wm97xx_reg_write(wm, AC97_WM9713_DIG1, 0);
244 wm97xx_reg_write(wm, AC97_WM9713_DIG2, 0);
245 wm97xx_reg_write(wm, AC97_WM9713_DIG3, WM97XX_PRP_DET_DIG);
246}
247
248static inline int is_pden(struct wm97xx *wm)
249{
250 return wm->dig[2] & WM9713_PDEN;
251}
252
253/*
254 * Read a sample from the WM9713 adc in polling mode.
255 */
256static int wm9713_poll_sample(struct wm97xx *wm, int adcsel, int *sample)
257{
258 u16 dig1;
259 int timeout = 5 * delay;
260 bool wants_pen = adcsel & WM97XX_PEN_DOWN;
261
262 if (wants_pen && !wm->pen_probably_down) {
263 u16 data = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
264 if (!(data & WM97XX_PEN_DOWN))
265 return RC_PENUP;
266 wm->pen_probably_down = 1;
267 }
268
269 /* set up digitiser */
270 dig1 = wm97xx_reg_read(wm, AC97_WM9713_DIG1);
271 dig1 &= ~WM9713_ADCSEL_MASK;
272 /* WM97XX_ADCSEL_* channels need to be converted to WM9713 format */
273 dig1 |= 1 << ((adcsel & WM97XX_ADCSEL_MASK) >> 12);
274
275 if (wm->mach_ops && wm->mach_ops->pre_sample)
276 wm->mach_ops->pre_sample(adcsel);
277 wm97xx_reg_write(wm, AC97_WM9713_DIG1, dig1 | WM9713_POLL);
278
279 /* wait 3 AC97 time slots + delay for conversion */
280 poll_delay(delay);
281
282 /* wait for POLL to go low */
283 while ((wm97xx_reg_read(wm, AC97_WM9713_DIG1) & WM9713_POLL) &&
284 timeout) {
285 udelay(AC97_LINK_FRAME);
286 timeout--;
287 }
288
289 if (timeout <= 0) {
290 /* If PDEN is set, we can get a timeout when pen goes up */
291 if (is_pden(wm))
292 wm->pen_probably_down = 0;
293 else
294 dev_dbg(wm->dev, "adc sample timeout");
295 return RC_PENUP;
296 }
297
298 *sample = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
299 if (wm->mach_ops && wm->mach_ops->post_sample)
300 wm->mach_ops->post_sample(adcsel);
301
302 /* check we have correct sample */
303 if ((*sample ^ adcsel) & WM97XX_ADCSEL_MASK) {
304 dev_dbg(wm->dev, "adc wrong sample, wanted %x got %x",
305 adcsel & WM97XX_ADCSEL_MASK,
306 *sample & WM97XX_ADCSEL_MASK);
307 return RC_PENUP;
308 }
309
310 if (wants_pen && !(*sample & WM97XX_PEN_DOWN)) {
311 wm->pen_probably_down = 0;
312 return RC_PENUP;
313 }
314
315 return RC_VALID;
316}
317
318/*
319 * Read a coordinate from the WM9713 adc in polling mode.
320 */
321static int wm9713_poll_coord(struct wm97xx *wm, struct wm97xx_data *data)
322{
323 u16 dig1;
324 int timeout = 5 * delay;
325
326 if (!wm->pen_probably_down) {
327 u16 val = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
328 if (!(val & WM97XX_PEN_DOWN))
329 return RC_PENUP;
330 wm->pen_probably_down = 1;
331 }
332
333 /* set up digitiser */
334 dig1 = wm97xx_reg_read(wm, AC97_WM9713_DIG1);
335 dig1 &= ~WM9713_ADCSEL_MASK;
336 if (pil)
337 dig1 |= WM9713_ADCSEL_PRES;
338
339 if (wm->mach_ops && wm->mach_ops->pre_sample)
340 wm->mach_ops->pre_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y);
341 wm97xx_reg_write(wm, AC97_WM9713_DIG1,
342 dig1 | WM9713_POLL | WM9713_COO);
343
344 /* wait 3 AC97 time slots + delay for conversion */
345 poll_delay(delay);
346 data->x = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
347 /* wait for POLL to go low */
348 while ((wm97xx_reg_read(wm, AC97_WM9713_DIG1) & WM9713_POLL)
349 && timeout) {
350 udelay(AC97_LINK_FRAME);
351 timeout--;
352 }
353
354 if (timeout <= 0) {
355 /* If PDEN is set, we can get a timeout when pen goes up */
356 if (is_pden(wm))
357 wm->pen_probably_down = 0;
358 else
359 dev_dbg(wm->dev, "adc sample timeout");
360 return RC_PENUP;
361 }
362
363 /* read back data */
364 data->y = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
365 if (pil)
366 data->p = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
367 else
368 data->p = DEFAULT_PRESSURE;
369
370 if (wm->mach_ops && wm->mach_ops->post_sample)
371 wm->mach_ops->post_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y);
372
373 /* check we have correct sample */
374 if (!(data->x & WM97XX_ADCSEL_X) || !(data->y & WM97XX_ADCSEL_Y))
375 goto err;
376 if (pil && !(data->p & WM97XX_ADCSEL_PRES))
377 goto err;
378
379 if (!(data->x & WM97XX_PEN_DOWN) || !(data->y & WM97XX_PEN_DOWN)) {
380 wm->pen_probably_down = 0;
381 return RC_PENUP;
382 }
383 return RC_VALID;
384err:
385 return 0;
386}
387
388/*
389 * Sample the WM9713 touchscreen in polling mode
390 */
391static int wm9713_poll_touch(struct wm97xx *wm, struct wm97xx_data *data)
392{
393 int rc;
394
395 if (coord) {
396 rc = wm9713_poll_coord(wm, data);
397 if (rc != RC_VALID)
398 return rc;
399 } else {
400 rc = wm9713_poll_sample(wm, WM97XX_ADCSEL_X | WM97XX_PEN_DOWN, &data->x);
401 if (rc != RC_VALID)
402 return rc;
403 rc = wm9713_poll_sample(wm, WM97XX_ADCSEL_Y | WM97XX_PEN_DOWN, &data->y);
404 if (rc != RC_VALID)
405 return rc;
406 if (pil) {
407 rc = wm9713_poll_sample(wm, WM97XX_ADCSEL_PRES | WM97XX_PEN_DOWN,
408 &data->p);
409 if (rc != RC_VALID)
410 return rc;
411 } else
412 data->p = DEFAULT_PRESSURE;
413 }
414 return RC_VALID;
415}
416
417/*
418 * Enable WM9713 continuous mode, i.e. touch data is streamed across
419 * an AC97 slot
420 */
421static int wm9713_acc_enable(struct wm97xx *wm, int enable)
422{
423 u16 dig1, dig2, dig3;
424 int ret = 0;
425
426 dig1 = wm->dig[0];
427 dig2 = wm->dig[1];
428 dig3 = wm->dig[2];
429
430 if (enable) {
431 /* continuous mode */
432 if (wm->mach_ops->acc_startup &&
433 (ret = wm->mach_ops->acc_startup(wm)) < 0)
434 return ret;
435
436 dig1 &= ~WM9713_ADCSEL_MASK;
437 dig1 |= WM9713_CTC | WM9713_COO | WM9713_ADCSEL_X |
438 WM9713_ADCSEL_Y;
439 if (pil)
440 dig1 |= WM9713_ADCSEL_PRES;
441 dig2 &= ~(WM97XX_DELAY_MASK | WM97XX_SLT_MASK |
442 WM97XX_CM_RATE_MASK);
443 dig2 |= WM97XX_SLEN | WM97XX_DELAY(delay) |
444 WM97XX_SLT(wm->acc_slot) | WM97XX_RATE(wm->acc_rate);
445 dig3 |= WM9713_PDEN;
446 } else {
447 dig1 &= ~(WM9713_CTC | WM9713_COO);
448 dig2 &= ~WM97XX_SLEN;
449 dig3 &= ~WM9713_PDEN;
450 if (wm->mach_ops->acc_shutdown)
451 wm->mach_ops->acc_shutdown(wm);
452 }
453
454 wm97xx_reg_write(wm, AC97_WM9713_DIG1, dig1);
455 wm97xx_reg_write(wm, AC97_WM9713_DIG2, dig2);
456 wm97xx_reg_write(wm, AC97_WM9713_DIG3, dig3);
457
458 return ret;
459}
460
461struct wm97xx_codec_drv wm9713_codec = {
462 .id = WM9713_ID2,
463 .name = "wm9713",
464 .poll_sample = wm9713_poll_sample,
465 .poll_touch = wm9713_poll_touch,
466 .acc_enable = wm9713_acc_enable,
467 .phy_init = wm9713_phy_init,
468 .dig_enable = wm9713_dig_enable,
469 .dig_restore = wm9713_dig_restore,
470 .aux_prepare = wm9713_aux_prepare,
471};
472EXPORT_SYMBOL_GPL(wm9713_codec);
473
474/* Module information */
475MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
476MODULE_DESCRIPTION("WM9713 Touch Screen Driver");
477MODULE_LICENSE("GPL");