1 /*
2  * wm9705.c  --  Codec driver for Wolfson WM9705 AC97 Codec.
3  *
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>
10  *
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.
15  *
16  */
17 
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>
26 
27 #define TS_NAME			"wm97xx"
28 #define WM9705_VERSION		"1.00"
29 #define DEFAULT_PRESSURE	0xb0c0
30 
31 /*
32  * Module parameters
33  */
34 
35 /*
36  * Set current used for pressure measurement.
37  *
38  * Set pil = 2 to use 400uA
39  *     pil = 1 to use 200uA and
40  *     pil = 0 to disable pressure measurement.
41  *
42  * This is used to increase the range of values returned by the adc
43  * when measureing touchpanel pressure.
44  */
45 static int pil;
46 module_param(pil, int, 0);
47 MODULE_PARM_DESC(pil, "Set current used for pressure measurement.");
48 
49 /*
50  * Set threshold for pressure measurement.
51  *
52  * Pen down pressure below threshold is ignored.
53  */
54 static int pressure = DEFAULT_PRESSURE & 0xfff;
55 module_param(pressure, int, 0);
56 MODULE_PARM_DESC(pressure, "Set threshold for pressure measurement.");
57 
58 /*
59  * Set adc sample delay.
60  *
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.
64  *
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
68  * recommended.
69  */
70 static int delay = 4;
71 module_param(delay, int, 0);
72 MODULE_PARM_DESC(delay, "Set adc sample delay.");
73 
74 /*
75  * Pen detect comparator threshold.
76  *
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
80  *
81  * Adjust this value if you are having problems with pen detect not
82  * detecting any down events.
83  */
84 static int pdd = 8;
85 module_param(pdd, int, 0);
86 MODULE_PARM_DESC(pdd, "Set pen detect comparator threshold");
87 
88 /*
89  * Set adc mask function.
90  *
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.
95  *
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
100  */
101 static int mask;
102 module_param(mask, int, 0);
103 MODULE_PARM_DESC(mask, "Set adc mask function.");
104 
105 /*
106  * ADC sample delay times in uS
107  */
108 static const int delay_table[] = {
109 	21,    /* 1 AC97 Link frames */
110 	42,    /* 2                  */
111 	84,    /* 4                  */
112 	167,   /* 8                  */
113 	333,   /* 16                 */
114 	667,   /* 32                 */
115 	1000,  /* 48                 */
116 	1333,  /* 64                 */
117 	2000,  /* 96                 */
118 	2667,  /* 128                */
119 	3333,  /* 160                */
120 	4000,  /* 192                */
121 	4667,  /* 224                */
122 	5333,  /* 256                */
123 	6000,  /* 288                */
124 	0      /* No delay, switch matrix always on */
125 };
126 
127 /*
128  * Delay after issuing a POLL command.
129  *
130  * The delay is 3 AC97 link frames + the touchpanel settling delay
131  */
132 static inline void poll_delay(int d)
133 {
134 	udelay(3 * AC97_LINK_FRAME + delay_table[d]);
135 }
136 
137 /*
138  * set up the physical settings of the WM9705
139  */
140 static void wm9705_phy_init(struct wm97xx *wm)
141 {
142 	u16 dig1 = 0, dig2 = WM97XX_RPR;
143 
144 	/*
145 	* mute VIDEO and AUX as they share X and Y touchscreen
146 	* inputs on the WM9705
147 	*/
148 	wm97xx_reg_write(wm, AC97_AUX, 0x8000);
149 	wm97xx_reg_write(wm, AC97_VIDEO, 0x8000);
150 
151 	/* touchpanel pressure current*/
152 	if (pil == 2) {
153 		dig2 |= WM9705_PIL;
154 		dev_dbg(wm->dev,
155 			"setting pressure measurement current to 400uA.");
156 	} else if (pil)
157 		dev_dbg(wm->dev,
158 			"setting pressure measurement current to 200uA.");
159 	if (!pil)
160 		pressure = 0;
161 
162 	/* polling mode sample settling delay */
163 	if (delay != 4) {
164 		if (delay < 0 || delay > 15) {
165 			dev_dbg(wm->dev, "supplied delay out of range.");
166 			delay = 4;
167 		}
168 	}
169 	dig1 &= 0xff0f;
170 	dig1 |= WM97XX_DELAY(delay);
171 	dev_dbg(wm->dev, "setting adc sample delay to %d u Secs.",
172 		delay_table[delay]);
173 
174 	/* WM9705 pdd */
175 	dig2 |= (pdd & 0x000f);
176 	dev_dbg(wm->dev, "setting pdd to Vmid/%d", 1 - (pdd & 0x000f));
177 
178 	/* mask */
179 	dig2 |= ((mask & 0x3) << 4);
180 
181 	wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, dig1);
182 	wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, dig2);
183 }
184 
185 static void wm9705_dig_enable(struct wm97xx *wm, int enable)
186 {
187 	if (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 */
191 	} else
192 		wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2,
193 				 wm->dig[2] & ~WM97XX_PRP_DET_DIG);
194 }
195 
196 static void wm9705_aux_prepare(struct wm97xx *wm)
197 {
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);
201 }
202 
203 static void wm9705_dig_restore(struct wm97xx *wm)
204 {
205 	wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, wm->dig_save[1]);
206 	wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, wm->dig_save[2]);
207 }
208 
209 static inline int is_pden(struct wm97xx *wm)
210 {
211 	return wm->dig[2] & WM9705_PDEN;
212 }
213 
214 /*
215  * Read a sample from the WM9705 adc in polling mode.
216  */
217 static int wm9705_poll_sample(struct wm97xx *wm, int adcsel, int *sample)
218 {
219 	int timeout = 5 * delay;
220 
221 	if (!wm->pen_probably_down) {
222 		u16 data = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
223 		if (!(data & WM97XX_PEN_DOWN))
224 			return RC_PENUP;
225 		wm->pen_probably_down = 1;
226 	}
227 
228 	/* set up digitiser */
229 	if (adcsel & 0x8000)
230 		adcsel = ((adcsel & 0x7fff) + 3) << 12;
231 
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));
236 
237 	/* wait 3 AC97 time slots + delay for conversion */
238 	poll_delay(delay);
239 
240 	/* wait for POLL to go low */
241 	while ((wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER1) & WM97XX_POLL)
242 	       && timeout) {
243 		udelay(AC97_LINK_FRAME);
244 		timeout--;
245 	}
246 
247 	if (timeout == 0) {
248 		/* If PDEN is set, we can get a timeout when pen goes up */
249 		if (is_pden(wm))
250 			wm->pen_probably_down = 0;
251 		else
252 			dev_dbg(wm->dev, "adc sample timeout");
253 		return RC_PENUP;
254 	}
255 
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);
259 
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);
264 		return RC_PENUP;
265 	}
266 
267 	if (!(*sample & WM97XX_PEN_DOWN)) {
268 		wm->pen_probably_down = 0;
269 		return RC_PENUP;
270 	}
271 
272 	return RC_VALID;
273 }
274 
275 /*
276  * Sample the WM9705 touchscreen in polling mode
277  */
278 static int wm9705_poll_touch(struct wm97xx *wm, struct wm97xx_data *data)
279 {
280 	int rc;
281 
282 	rc = wm9705_poll_sample(wm, WM97XX_ADCSEL_X, &data->x);
283 	if (rc != RC_VALID)
284 		return rc;
285 	rc = wm9705_poll_sample(wm, WM97XX_ADCSEL_Y, &data->y);
286 	if (rc != RC_VALID)
287 		return rc;
288 	if (pil) {
289 		rc = wm9705_poll_sample(wm, WM97XX_ADCSEL_PRES, &data->p);
290 		if (rc != RC_VALID)
291 			return rc;
292 	} else
293 		data->p = DEFAULT_PRESSURE;
294 
295 	return RC_VALID;
296 }
297 
298 /*
299  * Enable WM9705 continuous mode, i.e. touch data is streamed across
300  * an AC97 slot
301  */
302 static int wm9705_acc_enable(struct wm97xx *wm, int enable)
303 {
304 	u16 dig1, dig2;
305 	int ret = 0;
306 
307 	dig1 = wm->dig[1];
308 	dig2 = wm->dig[2];
309 
310 	if (enable) {
311 		/* continous mode */
312 		if (wm->mach_ops->acc_startup &&
313 		    (ret = wm->mach_ops->acc_startup(wm)) < 0)
314 			return ret;
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);
321 		if (pil)
322 			dig1 |= WM97XX_ADCSEL_PRES;
323 		dig2 |= WM9705_PDEN;
324 	} else {
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);
329 	}
330 
331 	wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, dig1);
332 	wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, dig2);
333 
334 	return ret;
335 }
336 
337 struct wm97xx_codec_drv wm9705_codec = {
338 	.id = WM9705_ID2,
339 	.name = "wm9705",
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,
347 };
348 EXPORT_SYMBOL_GPL(wm9705_codec);
349 
350 /* Module information */
351 MODULE_AUTHOR("Liam Girdwood <liam.girdwood@wolfsonmicro.com>");
352 MODULE_DESCRIPTION("WM9705 Touch Screen Driver");
353 MODULE_LICENSE("GPL");
354