c8bb1e7335fcf8f9450c0be194cafc9f014d03fb
[pandora-kernel.git] / drivers / input / touchscreen / wm9712.c
1 /*
2  * wm9712.c  --  Codec driver for Wolfson WM9712 AC97 Codecs.
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 WM9712_VERSION          "1.00"
28 #define DEFAULT_PRESSURE        0xb0c0
29
30 /*
31  * Module parameters
32  */
33
34 /*
35  * Set internal pull up for pen detect.
36  *
37  * Pull up is in the range 1.02k (least sensitive) to 64k (most sensitive)
38  * i.e. pull up resistance = 64k Ohms / rpu.
39  *
40  * Adjust this value if you are having problems with pen detect not
41  * detecting any down event.
42  */
43 static int rpu = 8;
44 module_param(rpu, int, 0);
45 MODULE_PARM_DESC(rpu, "Set internal pull up resitor for pen detect.");
46
47 /*
48  * Set current used for pressure measurement.
49  *
50  * Set pil = 2 to use 400uA
51  *     pil = 1 to use 200uA and
52  *     pil = 0 to disable pressure measurement.
53  *
54  * This is used to increase the range of values returned by the adc
55  * when measureing touchpanel pressure.
56  */
57 static int pil;
58 module_param(pil, int, 0);
59 MODULE_PARM_DESC(pil, "Set current used for pressure measurement.");
60
61 /*
62  * Set threshold for pressure measurement.
63  *
64  * Pen down pressure below threshold is ignored.
65  */
66 static int pressure = DEFAULT_PRESSURE & 0xfff;
67 module_param(pressure, int, 0);
68 MODULE_PARM_DESC(pressure, "Set threshold for pressure measurement.");
69
70 /*
71  * Set adc sample delay.
72  *
73  * For accurate touchpanel measurements, some settling time may be
74  * required between the switch matrix applying a voltage across the
75  * touchpanel plate and the ADC sampling the signal.
76  *
77  * This delay can be set by setting delay = n, where n is the array
78  * position of the delay in the array delay_table below.
79  * Long delays > 1ms are supported for completeness, but are not
80  * recommended.
81  */
82 static int delay = 3;
83 module_param(delay, int, 0);
84 MODULE_PARM_DESC(delay, "Set adc sample delay.");
85
86 /*
87  * Set five_wire = 1 to use a 5 wire touchscreen.
88  *
89  * NOTE: Five wire mode does not allow for readback of pressure.
90  */
91 static int five_wire;
92 module_param(five_wire, int, 0);
93 MODULE_PARM_DESC(five_wire, "Set to '1' to use 5-wire touchscreen.");
94
95 /*
96  * Set adc mask function.
97  *
98  * Sources of glitch noise, such as signals driving an LCD display, may feed
99  * through to the touch screen plates and affect measurement accuracy. In
100  * order to minimise this, a signal may be applied to the MASK pin to delay or
101  * synchronise the sampling.
102  *
103  * 0 = No delay or sync
104  * 1 = High on pin stops conversions
105  * 2 = Edge triggered, edge on pin delays conversion by delay param (above)
106  * 3 = Edge triggered, edge on pin starts conversion after delay param
107  */
108 static int mask;
109 module_param(mask, int, 0);
110 MODULE_PARM_DESC(mask, "Set adc mask function.");
111
112 /*
113  * Coordinate Polling Enable.
114  *
115  * Set to 1 to enable coordinate polling. e.g. x,y[,p] is sampled together
116  * for every poll.
117  */
118 static int coord;
119 module_param(coord, int, 0);
120 MODULE_PARM_DESC(coord, "Polling coordinate mode");
121
122 /*
123  * ADC sample delay times in uS
124  */
125 static const int delay_table[] = {
126         21,    /* 1 AC97 Link frames */
127         42,    /* 2 */
128         84,    /* 4 */
129         167,   /* 8 */
130         333,   /* 16 */
131         667,   /* 32 */
132         1000,  /* 48 */
133         1333,  /* 64 */
134         2000,  /* 96 */
135         2667,  /* 128 */
136         3333,  /* 160 */
137         4000,  /* 192 */
138         4667,  /* 224 */
139         5333,  /* 256 */
140         6000,  /* 288 */
141         0      /* No delay, switch matrix always on */
142 };
143
144 /*
145  * Delay after issuing a POLL command.
146  *
147  * The delay is 3 AC97 link frames + the touchpanel settling delay
148  */
149 static inline void poll_delay(int d)
150 {
151         udelay(3 * AC97_LINK_FRAME + delay_table[d]);
152 }
153
154 /*
155  * set up the physical settings of the WM9712
156  */
157 static void wm9712_phy_init(struct wm97xx *wm)
158 {
159         u16 dig1 = 0;
160         u16 dig2 = WM97XX_RPR | WM9712_RPU(1);
161
162         /* WM9712 rpu */
163         if (rpu) {
164                 dig2 &= 0xffc0;
165                 dig2 |= WM9712_RPU(rpu);
166                 dev_dbg(wm->dev, "setting pen detect pull-up to %d Ohms",
167                         64000 / rpu);
168         }
169
170         /* WM9712 five wire */
171         if (five_wire) {
172                 dig2 |= WM9712_45W;
173                 dev_dbg(wm->dev, "setting 5-wire touchscreen mode.");
174
175                 if (pil) {
176                         dev_warn(wm->dev, "pressure measurement is not "
177                                  "supported in 5-wire mode\n");
178                         pil = 0;
179                 }
180         }
181
182         /* touchpanel pressure current*/
183         if (pil == 2) {
184                 dig2 |= WM9712_PIL;
185                 dev_dbg(wm->dev,
186                         "setting pressure measurement current to 400uA.");
187         } else if (pil)
188                 dev_dbg(wm->dev,
189                         "setting pressure measurement current to 200uA.");
190         if (!pil)
191                 pressure = 0;
192
193         /* polling mode sample settling delay */
194         if (delay < 0 || delay > 15) {
195                 dev_dbg(wm->dev, "supplied delay out of range.");
196                 delay = 4;
197         }
198         dig1 &= 0xff0f;
199         dig1 |= WM97XX_DELAY(delay);
200         dev_dbg(wm->dev, "setting adc sample delay to %d u Secs.",
201                 delay_table[delay]);
202
203         /* mask */
204         dig2 |= ((mask & 0x3) << 6);
205         if (mask) {
206                 u16 reg;
207                 /* Set GPIO4 as Mask Pin*/
208                 reg = wm97xx_reg_read(wm, AC97_MISC_AFE);
209                 wm97xx_reg_write(wm, AC97_MISC_AFE, reg | WM97XX_GPIO_4);
210                 reg = wm97xx_reg_read(wm, AC97_GPIO_CFG);
211                 wm97xx_reg_write(wm, AC97_GPIO_CFG, reg | WM97XX_GPIO_4);
212         }
213
214         /* wait - coord mode */
215         if (coord)
216                 dig2 |= WM9712_WAIT;
217
218         wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, dig1);
219         wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, dig2);
220 }
221
222 static void wm9712_dig_enable(struct wm97xx *wm, int enable)
223 {
224         u16 dig2 = wm->dig[2];
225
226         if (enable) {
227                 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2,
228                                  dig2 | WM97XX_PRP_DET_DIG);
229                 wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD); /* dummy read */
230         } else
231                 wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2,
232                                  dig2 & ~WM97XX_PRP_DET_DIG);
233 }
234
235 static void wm9712_aux_prepare(struct wm97xx *wm)
236 {
237         memcpy(wm->dig_save, wm->dig, sizeof(wm->dig));
238         wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, 0);
239         wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, WM97XX_PRP_DET_DIG);
240 }
241
242 static void wm9712_dig_restore(struct wm97xx *wm)
243 {
244         wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, wm->dig_save[1]);
245         wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, wm->dig_save[2]);
246 }
247
248 static inline int is_pden(struct wm97xx *wm)
249 {
250         return wm->dig[2] & WM9712_PDEN;
251 }
252
253 /*
254  * Read a sample from the WM9712 adc in polling mode.
255  */
256 static int wm9712_poll_sample(struct wm97xx *wm, int adcsel, int *sample)
257 {
258         int timeout = 5 * delay;
259
260         if (!wm->pen_probably_down) {
261                 u16 data = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
262                 if (!(data & WM97XX_PEN_DOWN))
263                         return RC_PENUP;
264                 wm->pen_probably_down = 1;
265         }
266
267         /* set up digitiser */
268         if (adcsel & 0x8000)
269                 adcsel = ((adcsel & 0x7fff) + 3) << 12;
270
271         if (wm->mach_ops && wm->mach_ops->pre_sample)
272                 wm->mach_ops->pre_sample(adcsel);
273         wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1,
274                          adcsel | WM97XX_POLL | WM97XX_DELAY(delay));
275
276         /* wait 3 AC97 time slots + delay for conversion */
277         poll_delay(delay);
278
279         /* wait for POLL to go low */
280         while ((wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER1) & WM97XX_POLL)
281                && timeout) {
282                 udelay(AC97_LINK_FRAME);
283                 timeout--;
284         }
285
286         if (timeout <= 0) {
287                 /* If PDEN is set, we can get a timeout when pen goes up */
288                 if (is_pden(wm))
289                         wm->pen_probably_down = 0;
290                 else
291                         dev_dbg(wm->dev, "adc sample timeout");
292                 return RC_PENUP;
293         }
294
295         *sample = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
296         if (wm->mach_ops && wm->mach_ops->post_sample)
297                 wm->mach_ops->post_sample(adcsel);
298
299         /* check we have correct sample */
300         if ((*sample & WM97XX_ADCSEL_MASK) != adcsel) {
301                 dev_dbg(wm->dev, "adc wrong sample, read %x got %x", adcsel,
302                 *sample & WM97XX_ADCSEL_MASK);
303                 return RC_PENUP;
304         }
305
306         if (!(*sample & WM97XX_PEN_DOWN)) {
307                 wm->pen_probably_down = 0;
308                 return RC_PENUP;
309         }
310
311         return RC_VALID;
312 }
313
314 /*
315  * Read a coord from the WM9712 adc in polling mode.
316  */
317 static int wm9712_poll_coord(struct wm97xx *wm, struct wm97xx_data *data)
318 {
319         int timeout = 5 * delay;
320
321         if (!wm->pen_probably_down) {
322                 u16 data_rd = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
323                 if (!(data_rd & WM97XX_PEN_DOWN))
324                         return RC_PENUP;
325                 wm->pen_probably_down = 1;
326         }
327
328         /* set up digitiser */
329         if (wm->mach_ops && wm->mach_ops->pre_sample)
330                 wm->mach_ops->pre_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y);
331
332         wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1,
333                 WM97XX_COO | WM97XX_POLL | WM97XX_DELAY(delay));
334
335         /* wait 3 AC97 time slots + delay for conversion and read x */
336         poll_delay(delay);
337         data->x = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
338         /* wait for POLL to go low */
339         while ((wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER1) & WM97XX_POLL)
340                && timeout) {
341                 udelay(AC97_LINK_FRAME);
342                 timeout--;
343         }
344
345         if (timeout <= 0) {
346                 /* If PDEN is set, we can get a timeout when pen goes up */
347                 if (is_pden(wm))
348                         wm->pen_probably_down = 0;
349                 else
350                         dev_dbg(wm->dev, "adc sample timeout");
351                 return RC_PENUP;
352         }
353
354         /* read back y data */
355         data->y = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
356         if (pil)
357                 data->p = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
358         else
359                 data->p = DEFAULT_PRESSURE;
360
361         if (wm->mach_ops && wm->mach_ops->post_sample)
362                 wm->mach_ops->post_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y);
363
364         /* check we have correct sample */
365         if (!(data->x & WM97XX_ADCSEL_X) || !(data->y & WM97XX_ADCSEL_Y))
366                 goto err;
367         if (pil && !(data->p & WM97XX_ADCSEL_PRES))
368                 goto err;
369
370         if (!(data->x & WM97XX_PEN_DOWN) || !(data->y & WM97XX_PEN_DOWN)) {
371                 wm->pen_probably_down = 0;
372                 return RC_PENUP;
373         }
374         return RC_VALID;
375 err:
376         return 0;
377 }
378
379 /*
380  * Sample the WM9712 touchscreen in polling mode
381  */
382 static int wm9712_poll_touch(struct wm97xx *wm, struct wm97xx_data *data)
383 {
384         int rc;
385
386         if (coord) {
387                 rc = wm9712_poll_coord(wm, data);
388                 if (rc != RC_VALID)
389                         return rc;
390         } else {
391                 rc = wm9712_poll_sample(wm, WM97XX_ADCSEL_X, &data->x);
392                 if (rc != RC_VALID)
393                         return rc;
394
395                 rc = wm9712_poll_sample(wm, WM97XX_ADCSEL_Y, &data->y);
396                 if (rc != RC_VALID)
397                         return rc;
398
399                 if (pil && !five_wire) {
400                         rc = wm9712_poll_sample(wm, WM97XX_ADCSEL_PRES,
401                                                 &data->p);
402                         if (rc != RC_VALID)
403                                 return rc;
404                 } else
405                         data->p = DEFAULT_PRESSURE;
406         }
407         return RC_VALID;
408 }
409
410 /*
411  * Enable WM9712 continuous mode, i.e. touch data is streamed across
412  * an AC97 slot
413  */
414 static int wm9712_acc_enable(struct wm97xx *wm, int enable)
415 {
416         u16 dig1, dig2;
417         int ret = 0;
418
419         dig1 = wm->dig[1];
420         dig2 = wm->dig[2];
421
422         if (enable) {
423                 /* continous mode */
424                 if (wm->mach_ops->acc_startup) {
425                         ret = wm->mach_ops->acc_startup(wm);
426                         if (ret < 0)
427                                 return ret;
428                 }
429                 dig1 &= ~(WM97XX_CM_RATE_MASK | WM97XX_ADCSEL_MASK |
430                         WM97XX_DELAY_MASK | WM97XX_SLT_MASK);
431                 dig1 |= WM97XX_CTC | WM97XX_COO | WM97XX_SLEN |
432                         WM97XX_DELAY(delay) |
433                         WM97XX_SLT(wm->acc_slot) |
434                         WM97XX_RATE(wm->acc_rate);
435                 if (pil)
436                         dig1 |= WM97XX_ADCSEL_PRES;
437                 dig2 |= WM9712_PDEN;
438         } else {
439                 dig1 &= ~(WM97XX_CTC | WM97XX_COO | WM97XX_SLEN);
440                 dig2 &= ~WM9712_PDEN;
441                 if (wm->mach_ops->acc_shutdown)
442                         wm->mach_ops->acc_shutdown(wm);
443         }
444
445         wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER1, dig1);
446         wm97xx_reg_write(wm, AC97_WM97XX_DIGITISER2, dig2);
447
448         return 0;
449 }
450
451 struct wm97xx_codec_drv wm9712_codec = {
452         .id = WM9712_ID2,
453         .name = "wm9712",
454         .poll_sample = wm9712_poll_sample,
455         .poll_touch = wm9712_poll_touch,
456         .acc_enable = wm9712_acc_enable,
457         .phy_init = wm9712_phy_init,
458         .dig_enable = wm9712_dig_enable,
459         .dig_restore = wm9712_dig_restore,
460         .aux_prepare = wm9712_aux_prepare,
461 };
462 EXPORT_SYMBOL_GPL(wm9712_codec);
463
464 /* Module information */
465 MODULE_AUTHOR("Liam Girdwood <liam.girdwood@wolfsonmicro.com>");
466 MODULE_DESCRIPTION("WM9712 Touch Screen Driver");
467 MODULE_LICENSE("GPL");