2 * wm9705.c -- Codec driver for Wolfson WM9705 AC97 Codec.
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 WM9705_VERSION "1.00"
29 #define DEFAULT_PRESSURE 0xb0c0
36 * Set current used for pressure measurement.
38 * Set pil = 2 to use 400uA
39 * pil = 1 to use 200uA and
40 * pil = 0 to disable pressure measurement.
42 * This is used to increase the range of values returned by the adc
43 * when measureing touchpanel pressure.
46 module_param(pil, int, 0);
47 MODULE_PARM_DESC(pil, "Set current used for pressure measurement.");
50 * Set threshold for pressure measurement.
52 * Pen down pressure below threshold is ignored.
54 static int pressure = DEFAULT_PRESSURE & 0xfff;
55 module_param(pressure, int, 0);
56 MODULE_PARM_DESC(pressure, "Set threshold for pressure measurement.");
59 * Set adc sample delay.
61 * For accurate touchpanel measurements, some settling time may be
62 * required between the switch matrix applying a voltage across the
63 * touchpanel plate and the ADC sampling the signal.
65 * This delay can be set by setting delay = n, where n is the array
66 * position of the delay in the array delay_table below.
67 * Long delays > 1ms are supported for completeness, but are not
71 module_param(delay, int, 0);
72 MODULE_PARM_DESC(delay, "Set adc sample delay.");
75 * Pen detect comparator threshold.
77 * 0 to Vmid in 15 steps, 0 = use zero power comparator with Vmid threshold
78 * i.e. 1 = Vmid/15 threshold
79 * 15 = Vmid/1 threshold
81 * Adjust this value if you are having problems with pen detect not
82 * detecting any down events.
85 module_param(pdd, int, 0);
86 MODULE_PARM_DESC(pdd, "Set pen detect comparator threshold");
89 * Set adc mask function.
91 * Sources of glitch noise, such as signals driving an LCD display, may feed
92 * through to the touch screen plates and affect measurement accuracy. In
93 * order to minimise this, a signal may be applied to the MASK pin to delay or
94 * synchronise the sampling.
96 * 0 = No delay or sync
97 * 1 = High on pin stops conversions
98 * 2 = Edge triggered, edge on pin delays conversion by delay param (above)
99 * 3 = Edge triggered, edge on pin starts conversion after delay param
102 module_param(mask, int, 0);
103 MODULE_PARM_DESC(mask, "Set adc mask function.");
106 * ADC sample delay times in uS
108 static const int delay_table[] = {
109 21, /* 1 AC97 Link frames */
124 0 /* No delay, switch matrix always on */
128 * Delay after issuing a POLL command.
130 * The delay is 3 AC97 link frames + the touchpanel settling delay
132 static inline void poll_delay(int d)
134 udelay(3 * AC97_LINK_FRAME + delay_table[d]);
138 * set up the physical settings of the WM9705
140 static void wm9705_phy_init(struct wm97xx *wm)
142 u16 dig1 = 0, dig2 = WM97XX_RPR;
145 * mute VIDEO and AUX as they share X and Y touchscreen
146 * inputs on the WM9705
148 wm97xx_reg_write(wm, AC97_AUX, 0x8000);
149 wm97xx_reg_write(wm, AC97_VIDEO, 0x8000);
151 /* touchpanel pressure current*/
155 "setting pressure measurement current to 400uA.");
158 "setting pressure measurement current to 200uA.");
162 /* polling mode sample settling delay */
164 if (delay < 0 || delay > 15) {
165 dev_dbg(wm->dev, "supplied delay out of range.");
170 dig1 |= WM97XX_DELAY(delay);
171 dev_dbg(wm->dev, "setting adc sample delay to %d u Secs.",
175 dig2 |= (pdd & 0x000f);
176 dev_dbg(wm->dev, "setting pdd to Vmid/%d", 1 - (pdd & 0x000f));
179 dig2 |= ((mask & 0x3) << 4);
181 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, dig1);
182 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, dig2);
185 static void wm9705_dig_enable(struct wm97xx *wm, int enable)
188 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2,
189 wm->dig[2] | WM97XX_PRP_DET_DIG);
190 wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD); /* dummy read */
192 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2,
193 wm->dig[2] & ~WM97XX_PRP_DET_DIG);
196 static void wm9705_aux_prepare(struct wm97xx *wm)
198 memcpy(wm->dig_save, wm->dig, sizeof(wm->dig));
199 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, 0);
200 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, WM97XX_PRP_DET_DIG);
203 static void wm9705_dig_restore(struct wm97xx *wm)
205 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, wm->dig_save[1]);
206 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, wm->dig_save[2]);
209 static inline int is_pden(struct wm97xx *wm)
211 return wm->dig[2] & WM9705_PDEN;
215 * Read a sample from the WM9705 adc in polling mode.
217 static int wm9705_poll_sample(struct wm97xx *wm, int adcsel, int *sample)
219 int timeout = 5 * delay;
221 if (!wm->pen_probably_down) {
222 u16 data = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
223 if (!(data & WM97XX_PEN_DOWN))
225 wm->pen_probably_down = 1;
228 /* set up digitiser */
230 adcsel = ((adcsel & 0x7fff) + 3) << 12;
232 if (wm->mach_ops && wm->mach_ops->pre_sample)
233 wm->mach_ops->pre_sample(adcsel);
234 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1,
235 adcsel | WM97XX_POLL | WM97XX_DELAY(delay));
237 /* wait 3 AC97 time slots + delay for conversion */
240 /* wait for POLL to go low */
241 while ((wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER1) & WM97XX_POLL)
243 udelay(AC97_LINK_FRAME);
248 /* If PDEN is set, we can get a timeout when pen goes up */
250 wm->pen_probably_down = 0;
252 dev_dbg(wm->dev, "adc sample timeout");
256 *sample = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
257 if (wm->mach_ops && wm->mach_ops->post_sample)
258 wm->mach_ops->post_sample(adcsel);
260 /* check we have correct sample */
261 if ((*sample & WM97XX_ADCSEL_MASK) != adcsel) {
262 dev_dbg(wm->dev, "adc wrong sample, read %x got %x", adcsel,
263 *sample & WM97XX_ADCSEL_MASK);
267 if (!(*sample & WM97XX_PEN_DOWN)) {
268 wm->pen_probably_down = 0;
276 * Sample the WM9705 touchscreen in polling mode
278 static int wm9705_poll_touch(struct wm97xx *wm, struct wm97xx_data *data)
282 rc = wm9705_poll_sample(wm, WM97XX_ADCSEL_X, &data->x);
285 rc = wm9705_poll_sample(wm, WM97XX_ADCSEL_Y, &data->y);
289 rc = wm9705_poll_sample(wm, WM97XX_ADCSEL_PRES, &data->p);
293 data->p = DEFAULT_PRESSURE;
299 * Enable WM9705 continuous mode, i.e. touch data is streamed across
302 static int wm9705_acc_enable(struct wm97xx *wm, int enable)
312 if (wm->mach_ops->acc_startup &&
313 (ret = wm->mach_ops->acc_startup(wm)) < 0)
315 dig1 &= ~(WM97XX_CM_RATE_MASK | WM97XX_ADCSEL_MASK |
316 WM97XX_DELAY_MASK | WM97XX_SLT_MASK);
317 dig1 |= WM97XX_CTC | WM97XX_COO | WM97XX_SLEN |
318 WM97XX_DELAY(delay) |
319 WM97XX_SLT(wm->acc_slot) |
320 WM97XX_RATE(wm->acc_rate);
322 dig1 |= WM97XX_ADCSEL_PRES;
325 dig1 &= ~(WM97XX_CTC | WM97XX_COO | WM97XX_SLEN);
326 dig2 &= ~WM9705_PDEN;
327 if (wm->mach_ops->acc_shutdown)
328 wm->mach_ops->acc_shutdown(wm);
331 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, dig1);
332 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, dig2);
337 struct wm97xx_codec_drv wm9705_codec = {
340 .poll_sample = wm9705_poll_sample,
341 .poll_touch = wm9705_poll_touch,
342 .acc_enable = wm9705_acc_enable,
343 .phy_init = wm9705_phy_init,
344 .dig_enable = wm9705_dig_enable,
345 .dig_restore = wm9705_dig_restore,
346 .aux_prepare = wm9705_aux_prepare,
348 EXPORT_SYMBOL_GPL(wm9705_codec);
350 /* Module information */
351 MODULE_AUTHOR("Liam Girdwood <liam.girdwood@wolfsonmicro.com>");
352 MODULE_DESCRIPTION("WM9705 Touch Screen Driver");
353 MODULE_LICENSE("GPL");