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