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