GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / input / touchscreen / wm9712.c
blob7490b05c356651cf8aa6e2d0e1ee1b1621096cd6
1 /*
2 * wm9712.c -- Codec driver for Wolfson WM9712 AC97 Codecs.
4 * Copyright 2003, 2004, 2005, 2006, 2007 Wolfson Microelectronics PLC.
5 * Author: Liam Girdwood <lrg@slimlogic.co.uk>
6 * Parts Copyright : Ian Molton <spyro@f2s.com>
7 * Andrew Zabolotny <zap@homelink.ru>
8 * Russell King <rmk@arm.linux.org.uk>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the
12 * Free Software Foundation; either version 2 of the License, or (at your
13 * option) any later version.
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19 #include <linux/kernel.h>
20 #include <linux/input.h>
21 #include <linux/delay.h>
22 #include <linux/bitops.h>
23 #include <linux/wm97xx.h>
25 #define TS_NAME "wm97xx"
26 #define WM9712_VERSION "1.00"
27 #define DEFAULT_PRESSURE 0xb0c0
30 * Module parameters
34 * Set internal pull up for pen detect.
36 * Pull up is in the range 1.02k (least sensitive) to 64k (most sensitive)
37 * i.e. pull up resistance = 64k Ohms / rpu.
39 * Adjust this value if you are having problems with pen detect not
40 * detecting any down event.
42 static int rpu = 8;
43 module_param(rpu, int, 0);
44 MODULE_PARM_DESC(rpu, "Set internal pull up resitor for pen detect.");
47 * Set current used for pressure measurement.
49 * Set pil = 2 to use 400uA
50 * pil = 1 to use 200uA and
51 * pil = 0 to disable pressure measurement.
53 * This is used to increase the range of values returned by the adc
54 * when measureing touchpanel pressure.
56 static int pil;
57 module_param(pil, int, 0);
58 MODULE_PARM_DESC(pil, "Set current used for pressure measurement.");
61 * Set threshold for pressure measurement.
63 * Pen down pressure below threshold is ignored.
65 static int pressure = DEFAULT_PRESSURE & 0xfff;
66 module_param(pressure, int, 0);
67 MODULE_PARM_DESC(pressure, "Set threshold for pressure measurement.");
70 * Set adc sample delay.
72 * For accurate touchpanel measurements, some settling time may be
73 * required between the switch matrix applying a voltage across the
74 * touchpanel plate and the ADC sampling the signal.
76 * This delay can be set by setting delay = n, where n is the array
77 * position of the delay in the array delay_table below.
78 * Long delays > 1ms are supported for completeness, but are not
79 * recommended.
81 static int delay = 3;
82 module_param(delay, int, 0);
83 MODULE_PARM_DESC(delay, "Set adc sample delay.");
86 * Set five_wire = 1 to use a 5 wire touchscreen.
88 * NOTE: Five wire mode does not allow for readback of pressure.
90 static int five_wire;
91 module_param(five_wire, int, 0);
92 MODULE_PARM_DESC(five_wire, "Set to '1' to use 5-wire touchscreen.");
95 * Set adc mask function.
97 * Sources of glitch noise, such as signals driving an LCD display, may feed
98 * through to the touch screen plates and affect measurement accuracy. In
99 * order to minimise this, a signal may be applied to the MASK pin to delay or
100 * synchronise the sampling.
102 * 0 = No delay or sync
103 * 1 = High on pin stops conversions
104 * 2 = Edge triggered, edge on pin delays conversion by delay param (above)
105 * 3 = Edge triggered, edge on pin starts conversion after delay param
107 static int mask;
108 module_param(mask, int, 0);
109 MODULE_PARM_DESC(mask, "Set adc mask function.");
112 * Coordinate Polling Enable.
114 * Set to 1 to enable coordinate polling. e.g. x,y[,p] is sampled together
115 * for every poll.
117 static int coord;
118 module_param(coord, int, 0);
119 MODULE_PARM_DESC(coord, "Polling coordinate mode");
122 * ADC sample delay times in uS
124 static const int delay_table[] = {
125 21, /* 1 AC97 Link frames */
126 42, /* 2 */
127 84, /* 4 */
128 167, /* 8 */
129 333, /* 16 */
130 667, /* 32 */
131 1000, /* 48 */
132 1333, /* 64 */
133 2000, /* 96 */
134 2667, /* 128 */
135 3333, /* 160 */
136 4000, /* 192 */
137 4667, /* 224 */
138 5333, /* 256 */
139 6000, /* 288 */
140 0 /* No delay, switch matrix always on */
144 * Delay after issuing a POLL command.
146 * The delay is 3 AC97 link frames + the touchpanel settling delay
148 static inline void poll_delay(int d)
150 udelay(3 * AC97_LINK_FRAME + delay_table[d]);
154 * set up the physical settings of the WM9712
156 static void wm9712_phy_init(struct wm97xx *wm)
158 u16 dig1 = 0;
159 u16 dig2 = WM97XX_RPR | WM9712_RPU(1);
161 /* WM9712 rpu */
162 if (rpu) {
163 dig2 &= 0xffc0;
164 dig2 |= WM9712_RPU(rpu);
165 dev_dbg(wm->dev, "setting pen detect pull-up to %d Ohms",
166 64000 / rpu);
169 /* WM9712 five wire */
170 if (five_wire) {
171 dig2 |= WM9712_45W;
172 dev_dbg(wm->dev, "setting 5-wire touchscreen mode.");
174 if (pil) {
175 dev_warn(wm->dev, "pressure measurement is not "
176 "supported in 5-wire mode\n");
177 pil = 0;
181 /* touchpanel pressure current*/
182 if (pil == 2) {
183 dig2 |= WM9712_PIL;
184 dev_dbg(wm->dev,
185 "setting pressure measurement current to 400uA.");
186 } else if (pil)
187 dev_dbg(wm->dev,
188 "setting pressure measurement current to 200uA.");
189 if (!pil)
190 pressure = 0;
192 /* polling mode sample settling delay */
193 if (delay < 0 || delay > 15) {
194 dev_dbg(wm->dev, "supplied delay out of range.");
195 delay = 4;
197 dig1 &= 0xff0f;
198 dig1 |= WM97XX_DELAY(delay);
199 dev_dbg(wm->dev, "setting adc sample delay to %d u Secs.",
200 delay_table[delay]);
202 /* mask */
203 dig2 |= ((mask & 0x3) << 6);
204 if (mask) {
205 u16 reg;
206 /* Set GPIO4 as Mask Pin*/
207 reg = wm97xx_reg_read(wm, AC97_MISC_AFE);
208 wm97xx_reg_write(wm, AC97_MISC_AFE, reg | WM97XX_GPIO_4);
209 reg = wm97xx_reg_read(wm, AC97_GPIO_CFG);
210 wm97xx_reg_write(wm, AC97_GPIO_CFG, reg | WM97XX_GPIO_4);
213 /* wait - coord mode */
214 if (coord)
215 dig2 |= WM9712_WAIT;
217 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, dig1);
218 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, dig2);
221 static void wm9712_dig_enable(struct wm97xx *wm, int enable)
223 u16 dig2 = wm->dig[2];
225 if (enable) {
226 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2,
227 dig2 | WM97XX_PRP_DET_DIG);
228 wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD); /* dummy read */
229 } else
230 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2,
231 dig2 & ~WM97XX_PRP_DET_DIG);
234 static void wm9712_aux_prepare(struct wm97xx *wm)
236 memcpy(wm->dig_save, wm->dig, sizeof(wm->dig));
237 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, 0);
238 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, WM97XX_PRP_DET_DIG);
241 static void wm9712_dig_restore(struct wm97xx *wm)
243 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, wm->dig_save[1]);
244 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, wm->dig_save[2]);
247 static inline int is_pden(struct wm97xx *wm)
249 return wm->dig[2] & WM9712_PDEN;
253 * Read a sample from the WM9712 adc in polling mode.
255 static int wm9712_poll_sample(struct wm97xx *wm, int adcsel, int *sample)
257 int timeout = 5 * delay;
259 if (!wm->pen_probably_down) {
260 u16 data = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
261 if (!(data & WM97XX_PEN_DOWN))
262 return RC_PENUP;
263 wm->pen_probably_down = 1;
266 /* set up digitiser */
267 if (adcsel & 0x8000)
268 adcsel = ((adcsel & 0x7fff) + 3) << 12;
270 if (wm->mach_ops && wm->mach_ops->pre_sample)
271 wm->mach_ops->pre_sample(adcsel);
272 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1,
273 adcsel | WM97XX_POLL | WM97XX_DELAY(delay));
275 /* wait 3 AC97 time slots + delay for conversion */
276 poll_delay(delay);
278 /* wait for POLL to go low */
279 while ((wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER1) & WM97XX_POLL)
280 && timeout) {
281 udelay(AC97_LINK_FRAME);
282 timeout--;
285 if (timeout <= 0) {
286 /* If PDEN is set, we can get a timeout when pen goes up */
287 if (is_pden(wm))
288 wm->pen_probably_down = 0;
289 else
290 dev_dbg(wm->dev, "adc sample timeout");
291 return RC_PENUP;
294 *sample = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
295 if (wm->mach_ops && wm->mach_ops->post_sample)
296 wm->mach_ops->post_sample(adcsel);
298 /* check we have correct sample */
299 if ((*sample & WM97XX_ADCSEL_MASK) != adcsel) {
300 dev_dbg(wm->dev, "adc wrong sample, read %x got %x", adcsel,
301 *sample & WM97XX_ADCSEL_MASK);
302 return RC_PENUP;
305 if (!(*sample & WM97XX_PEN_DOWN)) {
306 wm->pen_probably_down = 0;
307 return RC_PENUP;
310 return RC_VALID;
314 * Read a coord from the WM9712 adc in polling mode.
316 static int wm9712_poll_coord(struct wm97xx *wm, struct wm97xx_data *data)
318 int timeout = 5 * delay;
320 if (!wm->pen_probably_down) {
321 u16 data_rd = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
322 if (!(data_rd & WM97XX_PEN_DOWN))
323 return RC_PENUP;
324 wm->pen_probably_down = 1;
327 /* set up digitiser */
328 if (wm->mach_ops && wm->mach_ops->pre_sample)
329 wm->mach_ops->pre_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y);
331 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1,
332 WM97XX_COO | WM97XX_POLL | WM97XX_DELAY(delay));
334 /* wait 3 AC97 time slots + delay for conversion and read x */
335 poll_delay(delay);
336 data->x = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
337 /* wait for POLL to go low */
338 while ((wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER1) & WM97XX_POLL)
339 && timeout) {
340 udelay(AC97_LINK_FRAME);
341 timeout--;
344 if (timeout <= 0) {
345 /* If PDEN is set, we can get a timeout when pen goes up */
346 if (is_pden(wm))
347 wm->pen_probably_down = 0;
348 else
349 dev_dbg(wm->dev, "adc sample timeout");
350 return RC_PENUP;
353 /* read back y data */
354 data->y = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
355 if (pil)
356 data->p = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
357 else
358 data->p = DEFAULT_PRESSURE;
360 if (wm->mach_ops && wm->mach_ops->post_sample)
361 wm->mach_ops->post_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y);
363 /* check we have correct sample */
364 if (!(data->x & WM97XX_ADCSEL_X) || !(data->y & WM97XX_ADCSEL_Y))
365 goto err;
366 if (pil && !(data->p & WM97XX_ADCSEL_PRES))
367 goto err;
369 if (!(data->x & WM97XX_PEN_DOWN) || !(data->y & WM97XX_PEN_DOWN)) {
370 wm->pen_probably_down = 0;
371 return RC_PENUP;
373 return RC_VALID;
374 err:
375 return 0;
379 * Sample the WM9712 touchscreen in polling mode
381 static int wm9712_poll_touch(struct wm97xx *wm, struct wm97xx_data *data)
383 int rc;
385 if (coord) {
386 rc = wm9712_poll_coord(wm, data);
387 if (rc != RC_VALID)
388 return rc;
389 } else {
390 rc = wm9712_poll_sample(wm, WM97XX_ADCSEL_X, &data->x);
391 if (rc != RC_VALID)
392 return rc;
394 rc = wm9712_poll_sample(wm, WM97XX_ADCSEL_Y, &data->y);
395 if (rc != RC_VALID)
396 return rc;
398 if (pil && !five_wire) {
399 rc = wm9712_poll_sample(wm, WM97XX_ADCSEL_PRES,
400 &data->p);
401 if (rc != RC_VALID)
402 return rc;
403 } else
404 data->p = DEFAULT_PRESSURE;
406 return RC_VALID;
410 * Enable WM9712 continuous mode, i.e. touch data is streamed across
411 * an AC97 slot
413 static int wm9712_acc_enable(struct wm97xx *wm, int enable)
415 u16 dig1, dig2;
416 int ret = 0;
418 dig1 = wm->dig[1];
419 dig2 = wm->dig[2];
421 if (enable) {
422 /* continous mode */
423 if (wm->mach_ops->acc_startup) {
424 ret = wm->mach_ops->acc_startup(wm);
425 if (ret < 0)
426 return ret;
428 dig1 &= ~(WM97XX_CM_RATE_MASK | WM97XX_ADCSEL_MASK |
429 WM97XX_DELAY_MASK | WM97XX_SLT_MASK);
430 dig1 |= WM97XX_CTC | WM97XX_COO | WM97XX_SLEN |
431 WM97XX_DELAY(delay) |
432 WM97XX_SLT(wm->acc_slot) |
433 WM97XX_RATE(wm->acc_rate);
434 if (pil)
435 dig1 |= WM97XX_ADCSEL_PRES;
436 dig2 |= WM9712_PDEN;
437 } else {
438 dig1 &= ~(WM97XX_CTC | WM97XX_COO | WM97XX_SLEN);
439 dig2 &= ~WM9712_PDEN;
440 if (wm->mach_ops->acc_shutdown)
441 wm->mach_ops->acc_shutdown(wm);
444 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, dig1);
445 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, dig2);
447 return 0;
450 struct wm97xx_codec_drv wm9712_codec = {
451 .id = WM9712_ID2,
452 .name = "wm9712",
453 .poll_sample = wm9712_poll_sample,
454 .poll_touch = wm9712_poll_touch,
455 .acc_enable = wm9712_acc_enable,
456 .phy_init = wm9712_phy_init,
457 .dig_enable = wm9712_dig_enable,
458 .dig_restore = wm9712_dig_restore,
459 .aux_prepare = wm9712_aux_prepare,
461 EXPORT_SYMBOL_GPL(wm9712_codec);
463 /* Module information */
464 MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
465 MODULE_DESCRIPTION("WM9712 Touch Screen Driver");
466 MODULE_LICENSE("GPL");