2 * wm9712.c -- Codec driver for Wolfson WM9712 AC97 Codecs.
4 * Copyright 2003, 2004, 2005, 2006, 2007 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood
6 * liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
7 * Parts Copyright : Ian Molton <spyro@f2s.com>
8 * Andrew Zabolotny <zap@homelink.ru>
9 * Russell King <rmk@arm.linux.org.uk>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
20 #include <linux/version.h>
21 #include <linux/kernel.h>
22 #include <linux/input.h>
23 #include <linux/delay.h>
24 #include <linux/bitops.h>
25 #include <linux/wm97xx.h>
27 #define TS_NAME "wm97xx"
28 #define WM9712_VERSION "1.00"
29 #define DEFAULT_PRESSURE 0xb0c0
36 * Set internal pull up for pen detect.
38 * Pull up is in the range 1.02k (least sensitive) to 64k (most sensitive)
39 * i.e. pull up resistance = 64k Ohms / rpu.
41 * Adjust this value if you are having problems with pen detect not
42 * detecting any down event.
45 module_param(rpu, int, 0);
46 MODULE_PARM_DESC(rpu, "Set internal pull up resitor for pen detect.");
49 * Set current used for pressure measurement.
51 * Set pil = 2 to use 400uA
52 * pil = 1 to use 200uA and
53 * pil = 0 to disable pressure measurement.
55 * This is used to increase the range of values returned by the adc
56 * when measureing touchpanel pressure.
59 module_param(pil, int, 0);
60 MODULE_PARM_DESC(pil, "Set current used for pressure measurement.");
63 * Set threshold for pressure measurement.
65 * Pen down pressure below threshold is ignored.
67 static int pressure = DEFAULT_PRESSURE & 0xfff;
68 module_param(pressure, int, 0);
69 MODULE_PARM_DESC(pressure, "Set threshold for pressure measurement.");
72 * Set adc sample delay.
74 * For accurate touchpanel measurements, some settling time may be
75 * required between the switch matrix applying a voltage across the
76 * touchpanel plate and the ADC sampling the signal.
78 * This delay can be set by setting delay = n, where n is the array
79 * position of the delay in the array delay_table below.
80 * Long delays > 1ms are supported for completeness, but are not
84 module_param(delay, int, 0);
85 MODULE_PARM_DESC(delay, "Set adc sample delay.");
88 * Set five_wire = 1 to use a 5 wire touchscreen.
90 * NOTE: Five wire mode does not allow for readback of pressure.
93 module_param(five_wire, int, 0);
94 MODULE_PARM_DESC(five_wire, "Set to '1' to use 5-wire touchscreen.");
97 * Set adc mask function.
99 * Sources of glitch noise, such as signals driving an LCD display, may feed
100 * through to the touch screen plates and affect measurement accuracy. In
101 * order to minimise this, a signal may be applied to the MASK pin to delay or
102 * synchronise the sampling.
104 * 0 = No delay or sync
105 * 1 = High on pin stops conversions
106 * 2 = Edge triggered, edge on pin delays conversion by delay param (above)
107 * 3 = Edge triggered, edge on pin starts conversion after delay param
110 module_param(mask, int, 0);
111 MODULE_PARM_DESC(mask, "Set adc mask function.");
114 * Coordinate Polling Enable.
116 * Set to 1 to enable coordinate polling. e.g. x,y[,p] is sampled together
120 module_param(coord, int, 0);
121 MODULE_PARM_DESC(coord, "Polling coordinate mode");
124 * ADC sample delay times in uS
126 static const int delay_table[] = {
127 21, /* 1 AC97 Link frames */
142 0 /* No delay, switch matrix always on */
146 * Delay after issuing a POLL command.
148 * The delay is 3 AC97 link frames + the touchpanel settling delay
150 static inline void poll_delay(int d)
152 udelay(3 * AC97_LINK_FRAME + delay_table[d]);
156 * set up the physical settings of the WM9712
158 static void wm9712_phy_init(struct wm97xx *wm)
161 u16 dig2 = WM97XX_RPR | WM9712_RPU(1);
166 dig2 |= WM9712_RPU(rpu);
167 dev_dbg(wm->dev, "setting pen detect pull-up to %d Ohms",
171 /* touchpanel pressure current*/
175 "setting pressure measurement current to 400uA.");
178 "setting pressure measurement current to 200uA.");
182 /* WM9712 five wire */
185 dev_dbg(wm->dev, "setting 5-wire touchscreen mode.");
188 /* polling mode sample settling delay */
189 if (delay < 0 || delay > 15) {
190 dev_dbg(wm->dev, "supplied delay out of range.");
194 dig1 |= WM97XX_DELAY(delay);
195 dev_dbg(wm->dev, "setting adc sample delay to %d u Secs.",
199 dig2 |= ((mask & 0x3) << 6);
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);
209 /* wait - coord mode */
213 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, dig1);
214 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, dig2);
217 static void wm9712_dig_enable(struct wm97xx *wm, int enable)
219 u16 dig2 = wm->dig[2];
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 */
226 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2,
227 dig2 & ~WM97XX_PRP_DET_DIG);
230 static void wm9712_aux_prepare(struct wm97xx *wm)
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);
237 static void wm9712_dig_restore(struct wm97xx *wm)
239 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, wm->dig_save[1]);
240 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, wm->dig_save[2]);
243 static inline int is_pden(struct wm97xx *wm)
245 return wm->dig[2] & WM9712_PDEN;
249 * Read a sample from the WM9712 adc in polling mode.
251 static int wm9712_poll_sample(struct wm97xx *wm, int adcsel, int *sample)
253 int timeout = 5 * delay;
255 if (!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 */
264 adcsel = ((adcsel & 0x7fff) + 3) << 12;
266 if (wm->mach_ops && wm->mach_ops->pre_sample)
267 wm->mach_ops->pre_sample(adcsel);
268 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1,
269 adcsel | WM97XX_POLL | WM97XX_DELAY(delay));
271 /* wait 3 AC97 time slots + delay for conversion */
274 /* wait for POLL to go low */
275 while ((wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER1) & WM97XX_POLL)
277 udelay(AC97_LINK_FRAME);
282 /* If PDEN is set, we can get a timeout when pen goes up */
284 wm->pen_probably_down = 0;
286 dev_dbg(wm->dev, "adc sample timeout");
290 *sample = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
291 if (wm->mach_ops && wm->mach_ops->post_sample)
292 wm->mach_ops->post_sample(adcsel);
294 /* check we have correct sample */
295 if ((*sample & WM97XX_ADCSEL_MASK) != adcsel) {
296 dev_dbg(wm->dev, "adc wrong sample, read %x got %x", adcsel,
297 *sample & WM97XX_ADCSEL_MASK);
301 if (!(*sample & WM97XX_PEN_DOWN)) {
302 wm->pen_probably_down = 0;
310 * Read a coord from the WM9712 adc in polling mode.
312 static int wm9712_poll_coord(struct wm97xx *wm, struct wm97xx_data *data)
314 int timeout = 5 * delay;
316 if (!wm->pen_probably_down) {
317 u16 data_rd = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
318 if (!(data_rd & WM97XX_PEN_DOWN))
320 wm->pen_probably_down = 1;
323 /* set up digitiser */
324 if (wm->mach_ops && wm->mach_ops->pre_sample)
325 wm->mach_ops->pre_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y);
327 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1,
328 WM97XX_COO | WM97XX_POLL | WM97XX_DELAY(delay));
330 /* wait 3 AC97 time slots + delay for conversion and read x */
332 data->x = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
333 /* wait for POLL to go low */
334 while ((wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER1) & WM97XX_POLL)
336 udelay(AC97_LINK_FRAME);
341 /* If PDEN is set, we can get a timeout when pen goes up */
343 wm->pen_probably_down = 0;
345 dev_dbg(wm->dev, "adc sample timeout");
349 /* read back y data */
350 data->y = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
352 data->p = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
354 data->p = DEFAULT_PRESSURE;
356 if (wm->mach_ops && wm->mach_ops->post_sample)
357 wm->mach_ops->post_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y);
359 /* check we have correct sample */
360 if (!(data->x & WM97XX_ADCSEL_X) || !(data->y & WM97XX_ADCSEL_Y))
362 if (pil && !(data->p & WM97XX_ADCSEL_PRES))
365 if (!(data->x & WM97XX_PEN_DOWN) || !(data->y & WM97XX_PEN_DOWN)) {
366 wm->pen_probably_down = 0;
375 * Sample the WM9712 touchscreen in polling mode
377 static int wm9712_poll_touch(struct wm97xx *wm, struct wm97xx_data *data)
382 rc = wm9712_poll_coord(wm, data);
386 rc = wm9712_poll_sample(wm, WM97XX_ADCSEL_X, &data->x);
390 rc = wm9712_poll_sample(wm, WM97XX_ADCSEL_Y, &data->y);
394 if (pil && !five_wire) {
395 rc = wm9712_poll_sample(wm, WM97XX_ADCSEL_PRES,
400 data->p = DEFAULT_PRESSURE;
406 * Enable WM9712 continuous mode, i.e. touch data is streamed across
409 static int wm9712_acc_enable(struct wm97xx *wm, int enable)
419 if (wm->mach_ops->acc_startup) {
420 ret = wm->mach_ops->acc_startup(wm);
424 dig1 &= ~(WM97XX_CM_RATE_MASK | WM97XX_ADCSEL_MASK |
425 WM97XX_DELAY_MASK | WM97XX_SLT_MASK);
426 dig1 |= WM97XX_CTC | WM97XX_COO | WM97XX_SLEN |
427 WM97XX_DELAY(delay) |
428 WM97XX_SLT(wm->acc_slot) |
429 WM97XX_RATE(wm->acc_rate);
431 dig1 |= WM97XX_ADCSEL_PRES;
434 dig1 &= ~(WM97XX_CTC | WM97XX_COO | WM97XX_SLEN);
435 dig2 &= ~WM9712_PDEN;
436 if (wm->mach_ops->acc_shutdown)
437 wm->mach_ops->acc_shutdown(wm);
440 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, dig1);
441 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, dig2);
446 struct wm97xx_codec_drv wm9712_codec = {
449 .poll_sample = wm9712_poll_sample,
450 .poll_touch = wm9712_poll_touch,
451 .acc_enable = wm9712_acc_enable,
452 .phy_init = wm9712_phy_init,
453 .dig_enable = wm9712_dig_enable,
454 .dig_restore = wm9712_dig_restore,
455 .aux_prepare = wm9712_aux_prepare,
457 EXPORT_SYMBOL_GPL(wm9712_codec);
459 /* Module information */
460 MODULE_AUTHOR("Liam Girdwood <liam.girdwood@wolfsonmicro.com>");
461 MODULE_DESCRIPTION("WM9712 Touch Screen Driver");
462 MODULE_LICENSE("GPL");