Merge commit '63cc8c75156462d4b42cbdd76c293b7eee7ddbfe':
[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/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 WM9713_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 = 4;
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 WM9713
156  */
157 static void wm9713_phy_init(struct wm97xx *wm)
158 {
159         u16 dig1 = 0, dig2, dig3;
160
161         /* default values */
162         dig2 = WM97XX_DELAY(4) | WM97XX_SLT(5);
163         dig3 = WM9712_RPU(1);
164
165         /* rpu */
166         if (rpu) {
167                 dig3 &= 0xffc0;
168                 dig3 |= WM9712_RPU(rpu);
169                 dev_info(wm->dev, "setting pen detect pull-up to %d Ohms\n",
170                          64000 / rpu);
171         }
172
173         /* Five wire panel? */
174         if (five_wire) {
175                 dig3 |= WM9713_45W;
176                 dev_info(wm->dev, "setting 5-wire touchscreen mode.");
177
178                 if (pil) {
179                         dev_warn(wm->dev,
180                                  "Pressure measurement not supported in 5 "
181                                  "wire mode, disabling\n");
182                         pil = 0;
183                 }
184         }
185
186         /* touchpanel pressure */
187         if (pil == 2) {
188                 dig3 |= WM9712_PIL;
189                 dev_info(wm->dev,
190                          "setting pressure measurement current to 400uA.");
191         } else if (pil)
192                 dev_info(wm->dev,
193                          "setting pressure measurement current to 200uA.");
194         if (!pil)
195                 pressure = 0;
196
197         /* sample settling delay */
198         if (delay < 0 || delay > 15) {
199                 dev_info(wm->dev, "supplied delay out of range.");
200                 delay = 4;
201                 dev_info(wm->dev, "setting adc sample delay to %d u Secs.",
202                          delay_table[delay]);
203         }
204         dig2 &= 0xff0f;
205         dig2 |= WM97XX_DELAY(delay);
206
207         /* mask */
208         dig3 |= ((mask & 0x3) << 4);
209         if (coord)
210                 dig3 |= WM9713_WAIT;
211
212         wm->misc = wm97xx_reg_read(wm, 0x5a);
213
214         wm97xx_reg_write(wm, AC97_WM9713_DIG1, dig1);
215         wm97xx_reg_write(wm, AC97_WM9713_DIG2, dig2);
216         wm97xx_reg_write(wm, AC97_WM9713_DIG3, dig3);
217         wm97xx_reg_write(wm, AC97_GPIO_STICKY, 0x0);
218 }
219
220 static void wm9713_dig_enable(struct wm97xx *wm, int enable)
221 {
222         u16 val;
223
224         if (enable) {
225                 val = wm97xx_reg_read(wm, AC97_EXTENDED_MID);
226                 wm97xx_reg_write(wm, AC97_EXTENDED_MID, val & 0x7fff);
227                 wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig[2] |
228                                  WM97XX_PRP_DET_DIG);
229                 wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD); /* dummy read */
230         } else {
231                 wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig[2] &
232                                         ~WM97XX_PRP_DET_DIG);
233                 val = wm97xx_reg_read(wm, AC97_EXTENDED_MID);
234                 wm97xx_reg_write(wm, AC97_EXTENDED_MID, val | 0x8000);
235         }
236 }
237
238 static void wm9713_dig_restore(struct wm97xx *wm)
239 {
240         wm97xx_reg_write(wm, AC97_WM9713_DIG1, wm->dig_save[0]);
241         wm97xx_reg_write(wm, AC97_WM9713_DIG2, wm->dig_save[1]);
242         wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig_save[2]);
243 }
244
245 static void wm9713_aux_prepare(struct wm97xx *wm)
246 {
247         memcpy(wm->dig_save, wm->dig, sizeof(wm->dig));
248         wm97xx_reg_write(wm, AC97_WM9713_DIG1, 0);
249         wm97xx_reg_write(wm, AC97_WM9713_DIG2, 0);
250         wm97xx_reg_write(wm, AC97_WM9713_DIG3, WM97XX_PRP_DET_DIG);
251 }
252
253 static inline int is_pden(struct wm97xx *wm)
254 {
255         return wm->dig[2] & WM9713_PDEN;
256 }
257
258 /*
259  * Read a sample from the WM9713 adc in polling mode.
260  */
261 static int wm9713_poll_sample(struct wm97xx *wm, int adcsel, int *sample)
262 {
263         u16 dig1;
264         int timeout = 5 * delay;
265
266         if (!wm->pen_probably_down) {
267                 u16 data = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
268                 if (!(data & WM97XX_PEN_DOWN))
269                         return RC_PENUP;
270                 wm->pen_probably_down = 1;
271         }
272
273         /* set up digitiser */
274         if (adcsel & 0x8000)
275                 adcsel = 1 << ((adcsel & 0x7fff) + 3);
276
277         dig1 = wm97xx_reg_read(wm, AC97_WM9713_DIG1);
278         dig1 &= ~WM9713_ADCSEL_MASK;
279
280         if (wm->mach_ops && wm->mach_ops->pre_sample)
281                 wm->mach_ops->pre_sample(adcsel);
282         wm97xx_reg_write(wm, AC97_WM9713_DIG1, dig1 | adcsel | WM9713_POLL);
283
284         /* wait 3 AC97 time slots + delay for conversion */
285         poll_delay(delay);
286
287         /* wait for POLL to go low */
288         while ((wm97xx_reg_read(wm, AC97_WM9713_DIG1) & WM9713_POLL) &&
289                 timeout) {
290                 udelay(AC97_LINK_FRAME);
291                 timeout--;
292         }
293
294         if (timeout <= 0) {
295                 /* If PDEN is set, we can get a timeout when pen goes up */
296                 if (is_pden(wm))
297                         wm->pen_probably_down = 0;
298                 else
299                         dev_dbg(wm->dev, "adc sample timeout");
300                 return RC_PENUP;
301         }
302
303         *sample = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
304         if (wm->mach_ops && wm->mach_ops->post_sample)
305                 wm->mach_ops->post_sample(adcsel);
306
307         /* check we have correct sample */
308         if ((*sample & WM97XX_ADCSRC_MASK) != ffs(adcsel >> 1) << 12) {
309                 dev_dbg(wm->dev, "adc wrong sample, read %x got %x", adcsel,
310                         *sample & WM97XX_ADCSRC_MASK);
311                 return RC_PENUP;
312         }
313
314         if (!(*sample & WM97XX_PEN_DOWN)) {
315                 wm->pen_probably_down = 0;
316                 return RC_PENUP;
317         }
318
319         return RC_VALID;
320 }
321
322 /*
323  * Read a coordinate from the WM9713 adc in polling mode.
324  */
325 static int wm9713_poll_coord(struct wm97xx *wm, struct wm97xx_data *data)
326 {
327         u16 dig1;
328         int timeout = 5 * delay;
329
330         if (!wm->pen_probably_down) {
331                 u16 val = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
332                 if (!(val & WM97XX_PEN_DOWN))
333                         return RC_PENUP;
334                 wm->pen_probably_down = 1;
335         }
336
337         /* set up digitiser */
338         dig1 = wm97xx_reg_read(wm, AC97_WM9713_DIG1);
339         dig1 &= ~WM9713_ADCSEL_MASK;
340         if (pil)
341                 dig1 |= WM9713_ADCSEL_PRES;
342
343         if (wm->mach_ops && wm->mach_ops->pre_sample)
344                 wm->mach_ops->pre_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y);
345         wm97xx_reg_write(wm, AC97_WM9713_DIG1,
346                          dig1 | WM9713_POLL | WM9713_COO);
347
348         /* wait 3 AC97 time slots + delay for conversion */
349         poll_delay(delay);
350         data->x = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
351         /* wait for POLL to go low */
352         while ((wm97xx_reg_read(wm, AC97_WM9713_DIG1) & WM9713_POLL)
353                && timeout) {
354                 udelay(AC97_LINK_FRAME);
355                 timeout--;
356         }
357
358         if (timeout <= 0) {
359                 /* If PDEN is set, we can get a timeout when pen goes up */
360                 if (is_pden(wm))
361                         wm->pen_probably_down = 0;
362                 else
363                         dev_dbg(wm->dev, "adc sample timeout");
364                 return RC_PENUP;
365         }
366
367         /* read back data */
368         data->y = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
369         if (pil)
370                 data->p = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD);
371         else
372                 data->p = DEFAULT_PRESSURE;
373
374         if (wm->mach_ops && wm->mach_ops->post_sample)
375                 wm->mach_ops->post_sample(WM97XX_ADCSEL_X | WM97XX_ADCSEL_Y);
376
377         /* check we have correct sample */
378         if (!(data->x & WM97XX_ADCSEL_X) || !(data->y & WM97XX_ADCSEL_Y))
379                 goto err;
380         if (pil && !(data->p & WM97XX_ADCSEL_PRES))
381                 goto err;
382
383         if (!(data->x & WM97XX_PEN_DOWN) || !(data->y & WM97XX_PEN_DOWN)) {
384                 wm->pen_probably_down = 0;
385                 return RC_PENUP;
386         }
387         return RC_VALID;
388 err:
389         return 0;
390 }
391
392 /*
393  * Sample the WM9713 touchscreen in polling mode
394  */
395 static int wm9713_poll_touch(struct wm97xx *wm, struct wm97xx_data *data)
396 {
397         int rc;
398
399         if (coord) {
400                 rc = wm9713_poll_coord(wm, data);
401                 if (rc != RC_VALID)
402                         return rc;
403         } else {
404                 rc = wm9713_poll_sample(wm, WM9713_ADCSEL_X, &data->x);
405                 if (rc != RC_VALID)
406                         return rc;
407                 rc = wm9713_poll_sample(wm, WM9713_ADCSEL_Y, &data->y);
408                 if (rc != RC_VALID)
409                         return rc;
410                 if (pil) {
411                         rc = wm9713_poll_sample(wm, WM9713_ADCSEL_PRES,
412                                                 &data->p);
413                         if (rc != RC_VALID)
414                                 return rc;
415                 } else
416                         data->p = DEFAULT_PRESSURE;
417         }
418         return RC_VALID;
419 }
420
421 /*
422  * Enable WM9713 continuous mode, i.e. touch data is streamed across
423  * an AC97 slot
424  */
425 static int wm9713_acc_enable(struct wm97xx *wm, int enable)
426 {
427         u16 dig1, dig2, dig3;
428         int ret = 0;
429
430         dig1 = wm->dig[0];
431         dig2 = wm->dig[1];
432         dig3 = wm->dig[2];
433
434         if (enable) {
435                 /* continous mode */
436                 if (wm->mach_ops->acc_startup &&
437                         (ret = wm->mach_ops->acc_startup(wm)) < 0)
438                         return ret;
439
440                 dig1 &= ~WM9713_ADCSEL_MASK;
441                 dig1 |= WM9713_CTC | WM9713_COO | WM9713_ADCSEL_X |
442                         WM9713_ADCSEL_Y;
443                 if (pil)
444                         dig1 |= WM9713_ADCSEL_PRES;
445                 dig2 &= ~(WM97XX_DELAY_MASK | WM97XX_SLT_MASK  |
446                         WM97XX_CM_RATE_MASK);
447                 dig2 |= WM97XX_SLEN | WM97XX_DELAY(delay) |
448                 WM97XX_SLT(wm->acc_slot) | WM97XX_RATE(wm->acc_rate);
449                 dig3 |= WM9713_PDEN;
450         } else {
451                 dig1 &= ~(WM9713_CTC | WM9713_COO);
452                 dig2 &= ~WM97XX_SLEN;
453                 dig3 &= ~WM9713_PDEN;
454                 if (wm->mach_ops->acc_shutdown)
455                         wm->mach_ops->acc_shutdown(wm);
456         }
457
458         wm97xx_reg_write(wm, AC97_WM9713_DIG1, dig1);
459         wm97xx_reg_write(wm, AC97_WM9713_DIG2, dig2);
460         wm97xx_reg_write(wm, AC97_WM9713_DIG3, dig3);
461
462         return ret;
463 }
464
465 struct wm97xx_codec_drv wm9713_codec = {
466         .id = WM9713_ID2,
467         .name = "wm9713",
468         .poll_sample = wm9713_poll_sample,
469         .poll_touch = wm9713_poll_touch,
470         .acc_enable = wm9713_acc_enable,
471         .phy_init = wm9713_phy_init,
472         .dig_enable = wm9713_dig_enable,
473         .dig_restore = wm9713_dig_restore,
474         .aux_prepare = wm9713_aux_prepare,
475 };
476 EXPORT_SYMBOL_GPL(wm9713_codec);
477
478 /* Module information */
479 MODULE_AUTHOR("Liam Girdwood <liam.girdwood@wolfsonmicro.com>");
480 MODULE_DESCRIPTION("WM9713 Touch Screen Driver");
481 MODULE_LICENSE("GPL");