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