Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tiwai/sound-2.6
[linux-2.6] / drivers / input / touchscreen / wm9705.c
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 <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>
9  *
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.
14  *
15  */
16
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>
24
25 #define TS_NAME                 "wm97xx"
26 #define WM9705_VERSION          "1.00"
27 #define DEFAULT_PRESSURE        0xb0c0
28
29 /*
30  * Module parameters
31  */
32
33 /*
34  * Set current used for pressure measurement.
35  *
36  * Set pil = 2 to use 400uA
37  *     pil = 1 to use 200uA and
38  *     pil = 0 to disable pressure measurement.
39  *
40  * This is used to increase the range of values returned by the adc
41  * when measureing touchpanel pressure.
42  */
43 static int pil;
44 module_param(pil, int, 0);
45 MODULE_PARM_DESC(pil, "Set current used for pressure measurement.");
46
47 /*
48  * Set threshold for pressure measurement.
49  *
50  * Pen down pressure below threshold is ignored.
51  */
52 static int pressure = DEFAULT_PRESSURE & 0xfff;
53 module_param(pressure, int, 0);
54 MODULE_PARM_DESC(pressure, "Set threshold for pressure measurement.");
55
56 /*
57  * Set adc sample delay.
58  *
59  * For accurate touchpanel measurements, some settling time may be
60  * required between the switch matrix applying a voltage across the
61  * touchpanel plate and the ADC sampling the signal.
62  *
63  * This delay can be set by setting delay = n, where n is the array
64  * position of the delay in the array delay_table below.
65  * Long delays > 1ms are supported for completeness, but are not
66  * recommended.
67  */
68 static int delay = 4;
69 module_param(delay, int, 0);
70 MODULE_PARM_DESC(delay, "Set adc sample delay.");
71
72 /*
73  * Pen detect comparator threshold.
74  *
75  * 0 to Vmid in 15 steps, 0 = use zero power comparator with Vmid threshold
76  * i.e. 1 =  Vmid/15 threshold
77  *      15 =  Vmid/1 threshold
78  *
79  * Adjust this value if you are having problems with pen detect not
80  * detecting any down events.
81  */
82 static int pdd = 8;
83 module_param(pdd, int, 0);
84 MODULE_PARM_DESC(pdd, "Set pen detect comparator threshold");
85
86 /*
87  * Set adc mask function.
88  *
89  * Sources of glitch noise, such as signals driving an LCD display, may feed
90  * through to the touch screen plates and affect measurement accuracy. In
91  * order to minimise this, a signal may be applied to the MASK pin to delay or
92  * synchronise the sampling.
93  *
94  * 0 = No delay or sync
95  * 1 = High on pin stops conversions
96  * 2 = Edge triggered, edge on pin delays conversion by delay param (above)
97  * 3 = Edge triggered, edge on pin starts conversion after delay param
98  */
99 static int mask;
100 module_param(mask, int, 0);
101 MODULE_PARM_DESC(mask, "Set adc mask function.");
102
103 /*
104  * ADC sample delay times in uS
105  */
106 static const int delay_table[] = {
107         21,    /* 1 AC97 Link frames */
108         42,    /* 2                  */
109         84,    /* 4                  */
110         167,   /* 8                  */
111         333,   /* 16                 */
112         667,   /* 32                 */
113         1000,  /* 48                 */
114         1333,  /* 64                 */
115         2000,  /* 96                 */
116         2667,  /* 128                */
117         3333,  /* 160                */
118         4000,  /* 192                */
119         4667,  /* 224                */
120         5333,  /* 256                */
121         6000,  /* 288                */
122         0      /* No delay, switch matrix always on */
123 };
124
125 /*
126  * Delay after issuing a POLL command.
127  *
128  * The delay is 3 AC97 link frames + the touchpanel settling delay
129  */
130 static inline void poll_delay(int d)
131 {
132         udelay(3 * AC97_LINK_FRAME + delay_table[d]);
133 }
134
135 /*
136  * set up the physical settings of the WM9705
137  */
138 static void wm9705_phy_init(struct wm97xx *wm)
139 {
140         u16 dig1 = 0, dig2 = WM97XX_RPR;
141
142         /*
143         * mute VIDEO and AUX as they share X and Y touchscreen
144         * inputs on the WM9705
145         */
146         wm97xx_reg_write(wm, AC97_AUX, 0x8000);
147         wm97xx_reg_write(wm, AC97_VIDEO, 0x8000);
148
149         /* touchpanel pressure current*/
150         if (pil == 2) {
151                 dig2 |= WM9705_PIL;
152                 dev_dbg(wm->dev,
153                         "setting pressure measurement current to 400uA.");
154         } else if (pil)
155                 dev_dbg(wm->dev,
156                         "setting pressure measurement current to 200uA.");
157         if (!pil)
158                 pressure = 0;
159
160         /* polling mode sample settling delay */
161         if (delay != 4) {
162                 if (delay < 0 || delay > 15) {
163                         dev_dbg(wm->dev, "supplied delay out of range.");
164                         delay = 4;
165                 }
166         }
167         dig1 &= 0xff0f;
168         dig1 |= WM97XX_DELAY(delay);
169         dev_dbg(wm->dev, "setting adc sample delay to %d u Secs.",
170                 delay_table[delay]);
171
172         /* WM9705 pdd */
173         dig2 |= (pdd & 0x000f);
174         dev_dbg(wm->dev, "setting pdd to Vmid/%d", 1 - (pdd & 0x000f));
175
176         /* mask */
177         dig2 |= ((mask & 0x3) << 4);
178
179         wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, dig1);
180         wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, dig2);
181 }
182
183 static void wm9705_dig_enable(struct wm97xx *wm, int enable)
184 {
185         if (enable) {
186                 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2,
187                                  wm->dig[2] | WM97XX_PRP_DET_DIG);
188                 wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD); /* dummy read */
189         } else
190                 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2,
191                                  wm->dig[2] & ~WM97XX_PRP_DET_DIG);
192 }
193
194 static void wm9705_aux_prepare(struct wm97xx *wm)
195 {
196         memcpy(wm->dig_save, wm->dig, sizeof(wm->dig));
197         wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, 0);
198         wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, WM97XX_PRP_DET_DIG);
199 }
200
201 static void wm9705_dig_restore(struct wm97xx *wm)
202 {
203         wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, wm->dig_save[1]);
204         wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, wm->dig_save[2]);
205 }
206
207 static inline int is_pden(struct wm97xx *wm)
208 {
209         return wm->dig[2] & WM9705_PDEN;
210 }
211
212 /*
213  * Read a sample from the WM9705 adc in polling mode.
214  */
215 static int wm9705_poll_sample(struct wm97xx *wm, int adcsel, int *sample)
216 {
217         int timeout = 5 * delay;
218
219         if (!wm->pen_probably_down) {
220                 u16 data = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
221                 if (!(data & WM97XX_PEN_DOWN))
222                         return RC_PENUP;
223                 wm->pen_probably_down = 1;
224         }
225
226         /* set up digitiser */
227         if (adcsel & 0x8000)
228                 adcsel = ((adcsel & 0x7fff) + 3) << 12;
229
230         if (wm->mach_ops && wm->mach_ops->pre_sample)
231                 wm->mach_ops->pre_sample(adcsel);
232         wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1,
233                          adcsel | WM97XX_POLL | WM97XX_DELAY(delay));
234
235         /* wait 3 AC97 time slots + delay for conversion */
236         poll_delay(delay);
237
238         /* wait for POLL to go low */
239         while ((wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER1) & WM97XX_POLL)
240                && timeout) {
241                 udelay(AC97_LINK_FRAME);
242                 timeout--;
243         }
244
245         if (timeout == 0) {
246                 /* If PDEN is set, we can get a timeout when pen goes up */
247                 if (is_pden(wm))
248                         wm->pen_probably_down = 0;
249                 else
250                         dev_dbg(wm->dev, "adc sample timeout");
251                 return RC_PENUP;
252         }
253
254         *sample = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
255         if (wm->mach_ops && wm->mach_ops->post_sample)
256                 wm->mach_ops->post_sample(adcsel);
257
258         /* check we have correct sample */
259         if ((*sample & WM97XX_ADCSEL_MASK) != adcsel) {
260                 dev_dbg(wm->dev, "adc wrong sample, read %x got %x", adcsel,
261                 *sample & WM97XX_ADCSEL_MASK);
262                 return RC_PENUP;
263         }
264
265         if (!(*sample & WM97XX_PEN_DOWN)) {
266                 wm->pen_probably_down = 0;
267                 return RC_PENUP;
268         }
269
270         return RC_VALID;
271 }
272
273 /*
274  * Sample the WM9705 touchscreen in polling mode
275  */
276 static int wm9705_poll_touch(struct wm97xx *wm, struct wm97xx_data *data)
277 {
278         int rc;
279
280         rc = wm9705_poll_sample(wm, WM97XX_ADCSEL_X, &data->x);
281         if (rc != RC_VALID)
282                 return rc;
283         rc = wm9705_poll_sample(wm, WM97XX_ADCSEL_Y, &data->y);
284         if (rc != RC_VALID)
285                 return rc;
286         if (pil) {
287                 rc = wm9705_poll_sample(wm, WM97XX_ADCSEL_PRES, &data->p);
288                 if (rc != RC_VALID)
289                         return rc;
290         } else
291                 data->p = DEFAULT_PRESSURE;
292
293         return RC_VALID;
294 }
295
296 /*
297  * Enable WM9705 continuous mode, i.e. touch data is streamed across
298  * an AC97 slot
299  */
300 static int wm9705_acc_enable(struct wm97xx *wm, int enable)
301 {
302         u16 dig1, dig2;
303         int ret = 0;
304
305         dig1 = wm->dig[1];
306         dig2 = wm->dig[2];
307
308         if (enable) {
309                 /* continous mode */
310                 if (wm->mach_ops->acc_startup &&
311                     (ret = wm->mach_ops->acc_startup(wm)) < 0)
312                         return ret;
313                 dig1 &= ~(WM97XX_CM_RATE_MASK | WM97XX_ADCSEL_MASK |
314                           WM97XX_DELAY_MASK | WM97XX_SLT_MASK);
315                 dig1 |= WM97XX_CTC | WM97XX_COO | WM97XX_SLEN |
316                         WM97XX_DELAY(delay) |
317                         WM97XX_SLT(wm->acc_slot) |
318                         WM97XX_RATE(wm->acc_rate);
319                 if (pil)
320                         dig1 |= WM97XX_ADCSEL_PRES;
321                 dig2 |= WM9705_PDEN;
322         } else {
323                 dig1 &= ~(WM97XX_CTC | WM97XX_COO | WM97XX_SLEN);
324                 dig2 &= ~WM9705_PDEN;
325                 if (wm->mach_ops->acc_shutdown)
326                         wm->mach_ops->acc_shutdown(wm);
327         }
328
329         wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, dig1);
330         wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, dig2);
331
332         return ret;
333 }
334
335 struct wm97xx_codec_drv wm9705_codec = {
336         .id = WM9705_ID2,
337         .name = "wm9705",
338         .poll_sample = wm9705_poll_sample,
339         .poll_touch = wm9705_poll_touch,
340         .acc_enable = wm9705_acc_enable,
341         .phy_init = wm9705_phy_init,
342         .dig_enable = wm9705_dig_enable,
343         .dig_restore = wm9705_dig_restore,
344         .aux_prepare = wm9705_aux_prepare,
345 };
346 EXPORT_SYMBOL_GPL(wm9705_codec);
347
348 /* Module information */
349 MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
350 MODULE_DESCRIPTION("WM9705 Touch Screen Driver");
351 MODULE_LICENSE("GPL");