merge of '7393275c6ccce67cadeb49d4afb3459e56edf8a9'
[vuplus_openembedded] / packages / linux / linux-rp-2.6.24 / tosa / 0030-Core-driver-for-WM97xx-touchscreens.patch
1 From d3e044e0e10e6c6b75716cb927e92b4ec284132f Mon Sep 17 00:00:00 2001
2 From: Mark Brown <broonie@opensource.wolfsonmicro.com>
3 Date: Sat, 26 Jan 2008 21:14:20 +0300
4 Subject: [PATCH 30/64] Core driver for WM97xx touchscreens
5
6 This patch series adds support for the touchscreen controllers provided
7 by Wolfson Microelectronics WM97xx series chips in both polled and
8 streaming modes.
9
10 These drivers have been maintained out of tree since 2003.  During that
11 time the driver the primary maintainer was Liam Girdwood and a number of
12 people have made contributions including Stanley Cai, Rodolfo Giometti,
13 Russell King, Marc Kleine-Budde, Ian Molton, Vincent Sanders, Andrew
14 Zabolotny, Graeme Gregory, Mike Arthur and myself.  Apologies to anyone
15 I have omitted.
16
17 Signed-off-by: Liam Girdwood <liam.girdwood@wolfsonmicro.com>
18 Signed-off-by: Graeme Gregory <gg@opensource.wolfsonmicro.com>
19 Signed-off-by: Mike Arthur <mike.arthur@wolfsonmicro.com>
20 Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com>
21 Cc: Dmitry Baryshkov <dbaryshkov@gmail.com>
22 Cc: Stanley Cai <stanley.cai@intel.com>
23 Cc: Rodolfo Giometti <giometti@enneenne.com>
24 Cc: Russell King <rmk@arm.linux.org.uk>
25 Cc: Marc Kleine-Budde <mkl@pengutronix.de>
26 Cc: Ian Molton <spyro@f2s.com>
27 Cc: Vincent Sanders <vince@kyllikki.org>
28 Cc: Andrew Zabolotny <zap@homelink.ru>
29 ---
30  drivers/input/touchscreen/wm97xx-core.c |  724 +++++++++++++++++++++++++++++++
31  include/linux/wm97xx.h                  |  309 +++++++++++++
32  2 files changed, 1033 insertions(+), 0 deletions(-)
33  create mode 100644 drivers/input/touchscreen/wm97xx-core.c
34  create mode 100644 include/linux/wm97xx.h
35
36 diff --git a/drivers/input/touchscreen/wm97xx-core.c b/drivers/input/touchscreen/wm97xx-core.c
37 new file mode 100644
38 index 0000000..27a0a99
39 --- /dev/null
40 +++ b/drivers/input/touchscreen/wm97xx-core.c
41 @@ -0,0 +1,724 @@
42 +/*
43 + * wm97xx-core.c  --  Touch screen driver core for Wolfson WM9705, WM9712
44 + *                    and WM9713 AC97 Codecs.
45 + *
46 + * Copyright 2003, 2004, 2005, 2006, 2007, 2008 Wolfson Microelectronics PLC.
47 + * Author: Liam Girdwood
48 + *         liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
49 + * Parts Copyright : Ian Molton <spyro@f2s.com>
50 + *                   Andrew Zabolotny <zap@homelink.ru>
51 + *                   Russell King <rmk@arm.linux.org.uk>
52 + *
53 + *  This program is free software; you can redistribute  it and/or modify it
54 + *  under  the terms of  the GNU General  Public License as published by the
55 + *  Free Software Foundation;  either version 2 of the  License, or (at your
56 + *  option) any later version.
57 + *
58 + * Notes:
59 + *
60 + *  Features:
61 + *       - supports WM9705, WM9712, WM9713
62 + *       - polling mode
63 + *       - continuous mode (arch-dependent)
64 + *       - adjustable rpu/dpp settings
65 + *       - adjustable pressure current
66 + *       - adjustable sample settle delay
67 + *       - 4 and 5 wire touchscreens (5 wire is WM9712 only)
68 + *       - pen down detection
69 + *       - battery monitor
70 + *       - sample AUX adcs
71 + *       - power management
72 + *       - codec GPIO
73 + *       - codec event notification
74 + * Todo
75 + *       - Support for async sampling control for noisy LCDs.
76 + *
77 + */
78 +
79 +#include <linux/module.h>
80 +#include <linux/moduleparam.h>
81 +#include <linux/version.h>
82 +#include <linux/kernel.h>
83 +#include <linux/init.h>
84 +#include <linux/delay.h>
85 +#include <linux/string.h>
86 +#include <linux/proc_fs.h>
87 +#include <linux/pm.h>
88 +#include <linux/interrupt.h>
89 +#include <linux/bitops.h>
90 +#include <linux/workqueue.h>
91 +#include <linux/wm97xx.h>
92 +#include <linux/uaccess.h>
93 +#include <linux/io.h>
94 +
95 +#define TS_NAME                        "wm97xx"
96 +#define WM_CORE_VERSION                "0.65"
97 +#define DEFAULT_PRESSURE       0xb0c0
98 +
99 +
100 +/*
101 + * Touchscreen absolute values
102 + *
103 + * These parameters are used to help the input layer discard out of
104 + * range readings and reduce jitter etc.
105 + *
106 + *   o min, max:- indicate the min and max values your touch screen returns
107 + *   o fuzz:- use a higher number to reduce jitter
108 + *
109 + * The default values correspond to Mainstone II in QVGA mode
110 + *
111 + * Please read
112 + * Documentation/input/input-programming.txt for more details.
113 + */
114 +
115 +static int abs_x[3] = {350, 3900, 5};
116 +module_param_array(abs_x, int, NULL, 0);
117 +MODULE_PARM_DESC(abs_x, "Touchscreen absolute X min, max, fuzz");
118 +
119 +static int abs_y[3] = {320, 3750, 40};
120 +module_param_array(abs_y, int, NULL, 0);
121 +MODULE_PARM_DESC(abs_y, "Touchscreen absolute Y min, max, fuzz");
122 +
123 +static int abs_p[3] = {0, 150, 4};
124 +module_param_array(abs_p, int, NULL, 0);
125 +MODULE_PARM_DESC(abs_p, "Touchscreen absolute Pressure min, max, fuzz");
126 +
127 +/*
128 + * wm97xx IO access, all IO locking done by AC97 layer
129 + */
130 +int wm97xx_reg_read(struct wm97xx *wm, u16 reg)
131 +{
132 +       if (wm->ac97)
133 +               return wm->ac97->bus->ops->read(wm->ac97, reg);
134 +       else
135 +               return -1;
136 +}
137 +EXPORT_SYMBOL_GPL(wm97xx_reg_read);
138 +
139 +void wm97xx_reg_write(struct wm97xx *wm, u16 reg, u16 val)
140 +{
141 +       /* cache digitiser registers */
142 +       if (reg >= AC97_WM9713_DIG1 && reg <= AC97_WM9713_DIG3)
143 +               wm->dig[(reg - AC97_WM9713_DIG1) >> 1] = val;
144 +
145 +       /* cache gpio regs */
146 +       if (reg >= AC97_GPIO_CFG && reg <= AC97_MISC_AFE)
147 +               wm->gpio[(reg - AC97_GPIO_CFG) >> 1] = val;
148 +
149 +       /* wm9713 irq reg */
150 +       if (reg == 0x5a)
151 +               wm->misc = val;
152 +
153 +       if (wm->ac97)
154 +               wm->ac97->bus->ops->write(wm->ac97, reg, val);
155 +}
156 +EXPORT_SYMBOL_GPL(wm97xx_reg_write);
157 +
158 +/**
159 + *     wm97xx_read_aux_adc - Read the aux adc.
160 + *     @wm: wm97xx device.
161 + *  @adcsel: codec ADC to be read
162 + *
163 + *     Reads the selected AUX ADC.
164 + */
165 +
166 +int wm97xx_read_aux_adc(struct wm97xx *wm, u16 adcsel)
167 +{
168 +       int power_adc = 0, auxval;
169 +       u16 power = 0;
170 +
171 +       /* get codec */
172 +       mutex_lock(&wm->codec_mutex);
173 +
174 +       /* When the touchscreen is not in use, we may have to power up
175 +        * the AUX ADC before we can use sample the AUX inputs->
176 +        */
177 +       if (wm->id == WM9713_ID2 &&
178 +           (power = wm97xx_reg_read(wm, AC97_EXTENDED_MID)) & 0x8000) {
179 +               power_adc = 1;
180 +               wm97xx_reg_write(wm, AC97_EXTENDED_MID, power & 0x7fff);
181 +       }
182 +
183 +       /* Prepare the codec for AUX reading */
184 +       wm->codec->aux_prepare(wm);
185 +
186 +       /* Turn polling mode on to read AUX ADC */
187 +       wm->pen_probably_down = 1;
188 +       wm->codec->poll_sample(wm, adcsel, &auxval);
189 +
190 +       if (power_adc)
191 +               wm97xx_reg_write(wm, AC97_EXTENDED_MID, power | 0x8000);
192 +
193 +       wm->codec->dig_restore(wm);
194 +
195 +       wm->pen_probably_down = 0;
196 +
197 +       mutex_unlock(&wm->codec_mutex);
198 +       return auxval & 0xfff;
199 +}
200 +EXPORT_SYMBOL_GPL(wm97xx_read_aux_adc);
201 +
202 +/**
203 + *     wm97xx_get_gpio - Get the status of a codec GPIO.
204 + *     @wm: wm97xx device.
205 + *      @gpio: gpio
206 + *
207 + *     Get the status of a codec GPIO pin
208 + */
209 +
210 +enum wm97xx_gpio_status wm97xx_get_gpio(struct wm97xx *wm, u32 gpio)
211 +{
212 +       u16 status;
213 +       enum wm97xx_gpio_status ret;
214 +
215 +       mutex_lock(&wm->codec_mutex);
216 +       status = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
217 +
218 +       if (status & gpio)
219 +               ret = WM97XX_GPIO_HIGH;
220 +       else
221 +               ret = WM97XX_GPIO_LOW;
222 +
223 +       mutex_unlock(&wm->codec_mutex);
224 +       return ret;
225 +}
226 +EXPORT_SYMBOL_GPL(wm97xx_get_gpio);
227 +
228 +/**
229 + *     wm97xx_set_gpio - Set the status of a codec GPIO.
230 + *     @wm: wm97xx device.
231 + *  @gpio: gpio
232 + *
233 + *
234 + *     Set the status of a codec GPIO pin
235 + */
236 +
237 +void wm97xx_set_gpio(struct wm97xx *wm, u32 gpio,
238 +                               enum wm97xx_gpio_status status)
239 +{
240 +       u16 reg;
241 +
242 +       mutex_lock(&wm->codec_mutex);
243 +       reg = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
244 +
245 +       if (status & WM97XX_GPIO_HIGH)
246 +               reg |= gpio;
247 +       else
248 +               reg &= ~gpio;
249 +
250 +       if (wm->id == WM9712_ID2)
251 +               wm97xx_reg_write(wm, AC97_GPIO_STATUS, reg << 1);
252 +       else
253 +               wm97xx_reg_write(wm, AC97_GPIO_STATUS, reg);
254 +       mutex_unlock(&wm->codec_mutex);
255 +}
256 +EXPORT_SYMBOL_GPL(wm97xx_set_gpio);
257 +
258 +/*
259 + * Codec GPIO pin configuration, this sets pin direction, polarity,
260 + * stickyness and wake up.
261 + */
262 +void wm97xx_config_gpio(struct wm97xx *wm, u32 gpio, enum wm97xx_gpio_dir dir,
263 +                  enum wm97xx_gpio_pol pol, enum wm97xx_gpio_sticky sticky,
264 +                  enum wm97xx_gpio_wake wake)
265 +{
266 +       u16 reg;
267 +
268 +       mutex_lock(&wm->codec_mutex);
269 +       reg = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
270 +
271 +       if (pol == WM97XX_GPIO_POL_HIGH)
272 +               reg |= gpio;
273 +       else
274 +               reg &= ~gpio;
275 +
276 +       wm97xx_reg_write(wm, AC97_GPIO_POLARITY, reg);
277 +       reg = wm97xx_reg_read(wm, AC97_GPIO_STICKY);
278 +
279 +       if (sticky == WM97XX_GPIO_STICKY)
280 +               reg |= gpio;
281 +       else
282 +               reg &= ~gpio;
283 +
284 +       wm97xx_reg_write(wm, AC97_GPIO_STICKY, reg);
285 +       reg = wm97xx_reg_read(wm, AC97_GPIO_WAKEUP);
286 +
287 +       if (wake == WM97XX_GPIO_WAKE)
288 +               reg |= gpio;
289 +       else
290 +               reg &= ~gpio;
291 +
292 +       wm97xx_reg_write(wm, AC97_GPIO_WAKEUP, reg);
293 +       reg = wm97xx_reg_read(wm, AC97_GPIO_CFG);
294 +
295 +       if (dir == WM97XX_GPIO_IN)
296 +               reg |= gpio;
297 +       else
298 +               reg &= ~gpio;
299 +
300 +       wm97xx_reg_write(wm, AC97_GPIO_CFG, reg);
301 +       mutex_unlock(&wm->codec_mutex);
302 +}
303 +EXPORT_SYMBOL_GPL(wm97xx_config_gpio);
304 +
305 +/*
306 + * Handle a pen down interrupt.
307 + */
308 +static void wm97xx_pen_irq_worker(struct work_struct *work)
309 +{
310 +       struct wm97xx *wm = container_of(work, struct wm97xx, pen_event_work);
311 +
312 +       /* do we need to enable the touch panel reader */
313 +       if (wm->id == WM9705_ID2) {
314 +               if (wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD) &
315 +                                       WM97XX_PEN_DOWN)
316 +                       wm->pen_is_down = 1;
317 +               else
318 +                       wm->pen_is_down = 0;
319 +       } else {
320 +               u16 status, pol;
321 +               mutex_lock(&wm->codec_mutex);
322 +               status = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
323 +               pol = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
324 +
325 +               if (WM97XX_GPIO_13 & pol & status) {
326 +                       wm->pen_is_down = 1;
327 +                       wm97xx_reg_write(wm, AC97_GPIO_POLARITY, pol &
328 +                                               ~WM97XX_GPIO_13);
329 +               } else {
330 +                       wm->pen_is_down = 0;
331 +                       wm97xx_reg_write(wm, AC97_GPIO_POLARITY, pol |
332 +                                        WM97XX_GPIO_13);
333 +               }
334 +
335 +               if (wm->id == WM9712_ID2)
336 +                       wm97xx_reg_write(wm, AC97_GPIO_STATUS, (status &
337 +                                               ~WM97XX_GPIO_13) << 1);
338 +               else
339 +                       wm97xx_reg_write(wm, AC97_GPIO_STATUS, status &
340 +                                               ~WM97XX_GPIO_13);
341 +               mutex_unlock(&wm->codec_mutex);
342 +       }
343 +
344 +       queue_delayed_work(wm->ts_workq, &wm->ts_reader, 0);
345 +
346 +       if (!wm->pen_is_down && wm->mach_ops && wm->mach_ops->acc_enabled)
347 +               wm->mach_ops->acc_pen_up(wm);
348 +       wm->mach_ops->irq_enable(wm, 1);
349 +}
350 +
351 +/*
352 + * Codec PENDOWN irq handler
353 + *
354 + * We have to disable the codec interrupt in the handler because it can
355 + * take upto 1ms to clear the interrupt source. The interrupt is then enabled
356 + * again in the slow handler when the source has been cleared.
357 + */
358 +static irqreturn_t wm97xx_pen_interrupt(int irq, void *dev_id)
359 +{
360 +       struct wm97xx *wm = dev_id;
361 +       wm->mach_ops->irq_enable(wm, 0);
362 +       queue_work(wm->ts_workq, &wm->pen_event_work);
363 +       return IRQ_HANDLED;
364 +}
365 +
366 +/*
367 + * initialise pen IRQ handler and workqueue
368 + */
369 +static int wm97xx_init_pen_irq(struct wm97xx *wm)
370 +{
371 +       u16 reg;
372 +
373 +       /* If an interrupt is supplied an IRQ enable operation must also be
374 +        * provided. */
375 +       BUG_ON(!wm->mach_ops->irq_enable);
376 +
377 +       INIT_WORK(&wm->pen_event_work, wm97xx_pen_irq_worker);
378 +
379 +       if (request_irq(wm->pen_irq, wm97xx_pen_interrupt, IRQF_SHARED,
380 +                       "wm97xx-pen", wm)) {
381 +               dev_err(wm->dev,
382 +                       "Failed to register pen down interrupt, polling");
383 +               wm->pen_irq = 0;
384 +               return -EINVAL;
385 +       }
386 +
387 +       /* enable PEN down on wm9712/13 */
388 +       if (wm->id != WM9705_ID2) {
389 +               reg = wm97xx_reg_read(wm, AC97_MISC_AFE);
390 +               wm97xx_reg_write(wm, AC97_MISC_AFE, reg & 0xfffb);
391 +               reg = wm97xx_reg_read(wm, 0x5a);
392 +               wm97xx_reg_write(wm, 0x5a, reg & ~0x0001);
393 +       }
394 +
395 +       return 0;
396 +}
397 +
398 +static int wm97xx_read_samples(struct wm97xx *wm)
399 +{
400 +       struct wm97xx_data data;
401 +       int rc;
402 +
403 +       mutex_lock(&wm->codec_mutex);
404 +
405 +       if (wm->mach_ops && wm->mach_ops->acc_enabled)
406 +               rc = wm->mach_ops->acc_pen_down(wm);
407 +       else
408 +               rc = wm->codec->poll_touch(wm, &data);
409 +
410 +       if (rc & RC_PENUP) {
411 +               if (wm->pen_is_down) {
412 +                       wm->pen_is_down = 0;
413 +                       dev_dbg(wm->dev, "pen up\n");
414 +                       input_report_abs(wm->input_dev, ABS_PRESSURE, 0);
415 +                       input_sync(wm->input_dev);
416 +               } else if (!(rc & RC_AGAIN)) {
417 +                       /* We need high frequency updates only while
418 +                       * pen is down, the user never will be able to
419 +                       * touch screen faster than a few times per
420 +                       * second... On the other hand, when the user
421 +                       * is actively working with the touchscreen we
422 +                       * don't want to lose the quick response. So we
423 +                       * will slowly increase sleep time after the
424 +                       * pen is up and quicky restore it to ~one task
425 +                       * switch when pen is down again.
426 +                       */
427 +                       if (wm->ts_reader_interval < HZ / 10)
428 +                               wm->ts_reader_interval++;
429 +               }
430 +
431 +       } else if (rc & RC_VALID) {
432 +               dev_dbg(wm->dev,
433 +                       "pen down: x=%x:%d, y=%x:%d, pressure=%x:%d\n",
434 +                       data.x >> 12, data.x & 0xfff, data.y >> 12,
435 +                       data.y & 0xfff, data.p >> 12, data.p & 0xfff);
436 +               input_report_abs(wm->input_dev, ABS_X, data.x & 0xfff);
437 +               input_report_abs(wm->input_dev, ABS_Y, data.y & 0xfff);
438 +               input_report_abs(wm->input_dev, ABS_PRESSURE, data.p & 0xfff);
439 +               input_sync(wm->input_dev);
440 +               wm->pen_is_down = 1;
441 +               wm->ts_reader_interval = wm->ts_reader_min_interval;
442 +       } else if (rc & RC_PENDOWN) {
443 +               dev_dbg(wm->dev, "pen down");
444 +               wm->pen_is_down = 1;
445 +               wm->ts_reader_interval = wm->ts_reader_min_interval;
446 +       }
447 +
448 +       mutex_unlock(&wm->codec_mutex);
449 +       return rc;
450 +}
451 +
452 +/*
453 +* The touchscreen sample reader.
454 +*/
455 +static void wm97xx_ts_reader(struct work_struct *work)
456 +{
457 +       int rc;
458 +       struct wm97xx *wm = container_of(work, struct wm97xx, ts_reader.work);
459 +
460 +       BUG_ON(!wm->codec);
461 +
462 +       do {
463 +               rc = wm97xx_read_samples(wm);
464 +       } while (rc & RC_AGAIN);
465 +
466 +       if (wm->pen_is_down || !wm->pen_irq)
467 +               queue_delayed_work(wm->ts_workq, &wm->ts_reader,
468 +                                  wm->ts_reader_interval);
469 +}
470 +
471 +/**
472 + *     wm97xx_ts_input_open - Open the touch screen input device.
473 + *     @idev:  Input device to be opened.
474 + *
475 + *     Called by the input sub system to open a wm97xx touchscreen device.
476 + *  Starts the touchscreen thread and touch digitiser.
477 + */
478 +static int wm97xx_ts_input_open(struct input_dev *idev)
479 +{
480 +       struct wm97xx *wm = input_get_drvdata(idev);
481 +
482 +       wm->ts_workq = create_singlethread_workqueue("kwm97xx");
483 +       if (wm->ts_workq == NULL) {
484 +               dev_err(wm->dev,
485 +                       "Failed to create workqueue\n");
486 +               return -EINVAL;
487 +       }
488 +
489 +       /* start digitiser */
490 +       if (wm->mach_ops && wm->mach_ops->acc_enabled)
491 +               wm->codec->acc_enable(wm, 1);
492 +       wm->codec->dig_enable(wm, 1);
493 +
494 +       INIT_DELAYED_WORK(&wm->ts_reader, wm97xx_ts_reader);
495 +
496 +       wm->ts_reader_min_interval = HZ >= 100 ? HZ / 100 : 1;
497 +       if (wm->ts_reader_min_interval < 1)
498 +               wm->ts_reader_min_interval = 1;
499 +       wm->ts_reader_interval = wm->ts_reader_min_interval;
500 +
501 +       wm->pen_is_down = 0;
502 +       if (wm->pen_irq)
503 +               wm97xx_init_pen_irq(wm);
504 +       else
505 +               dev_err(wm->dev, "No IRQ specified\n");
506 +
507 +       /* If we either don't have an interrupt for pen down events or
508 +        * failed to acquire it then we need to poll.
509 +        */
510 +       if (wm->pen_irq == 0)
511 +               queue_delayed_work(wm->ts_workq, &wm->ts_reader,
512 +                                  wm->ts_reader_interval);
513 +
514 +       return 0;
515 +}
516 +
517 +/**
518 + *     wm97xx_ts_input_close - Close the touch screen input device.
519 + *     @idev:  Input device to be closed.
520 + *
521 + *     Called by the input sub system to close a wm97xx touchscreen device.
522 + *  Kills the touchscreen thread and stops the touch digitiser.
523 + */
524 +
525 +static void wm97xx_ts_input_close(struct input_dev *idev)
526 +{
527 +       struct wm97xx *wm = input_get_drvdata(idev);
528 +
529 +       if (wm->pen_irq)
530 +               free_irq(wm->pen_irq, wm);
531 +
532 +       wm->pen_is_down = 0;
533 +
534 +       /* ts_reader rearms itself so we need to explicitly stop it
535 +        * before we destroy the workqueue.
536 +        */
537 +       cancel_delayed_work_sync(&wm->ts_reader);
538 +       destroy_workqueue(wm->ts_workq);
539 +
540 +       /* stop digitiser */
541 +       wm->codec->dig_enable(wm, 0);
542 +       if (wm->mach_ops && wm->mach_ops->acc_enabled)
543 +               wm->codec->acc_enable(wm, 0);
544 +}
545 +
546 +static int wm97xx_probe(struct device *dev)
547 +{
548 +       struct wm97xx *wm;
549 +       int ret = 0, id = 0;
550 +
551 +       wm = kzalloc(sizeof(struct wm97xx), GFP_KERNEL);
552 +       if (!wm)
553 +               return -ENOMEM;
554 +       mutex_init(&wm->codec_mutex);
555 +
556 +       wm->dev = dev;
557 +       dev->driver_data = wm;
558 +       wm->ac97 = to_ac97_t(dev);
559 +
560 +       /* check that we have a supported codec */
561 +       id = wm97xx_reg_read(wm, AC97_VENDOR_ID1);
562 +       if (id != WM97XX_ID1) {
563 +               dev_err(dev, "Device with vendor %04x is not a wm97xx\n", id);
564 +               kfree(wm);
565 +               return -ENODEV;
566 +       }
567 +
568 +       wm->id = wm97xx_reg_read(wm, AC97_VENDOR_ID2);
569 +
570 +       dev_info(wm->dev, "detected a wm97%02x codec", wm->id & 0xff);
571 +
572 +       switch (wm->id & 0xff) {
573 +#ifdef CONFIG_TOUCHSCREEN_WM9705
574 +       case 0x05:
575 +               wm->codec = &wm9705_codec;
576 +               break;
577 +#endif
578 +#ifdef CONFIG_TOUCHSCREEN_WM9712
579 +       case 0x12:
580 +               wm->codec = &wm9712_codec;
581 +               break;
582 +#endif
583 +#ifdef CONFIG_TOUCHSCREEN_WM9713
584 +       case 0x13:
585 +               wm->codec = &wm9713_codec;
586 +               break;
587 +#endif
588 +       default:
589 +               dev_err(wm->dev, "Support for wm97%02x not compiled in.\n",
590 +                       wm->id & 0xff);
591 +               kfree(wm);
592 +               return -ENODEV;
593 +       }
594 +
595 +       wm->input_dev = input_allocate_device();
596 +       if (wm->input_dev == NULL) {
597 +               kfree(wm);
598 +               return -ENOMEM;
599 +       }
600 +
601 +       /* set up touch configuration */
602 +       wm->input_dev->name = "wm97xx touchscreen";
603 +       wm->input_dev->open = wm97xx_ts_input_open;
604 +       wm->input_dev->close = wm97xx_ts_input_close;
605 +       set_bit(EV_ABS, wm->input_dev->evbit);
606 +       set_bit(ABS_X, wm->input_dev->absbit);
607 +       set_bit(ABS_Y, wm->input_dev->absbit);
608 +       set_bit(ABS_PRESSURE, wm->input_dev->absbit);
609 +       input_set_abs_params(wm->input_dev, ABS_X, abs_x[0], abs_x[1],
610 +                            abs_x[2], 0);
611 +       input_set_abs_params(wm->input_dev, ABS_Y, abs_y[0], abs_y[1],
612 +                            abs_y[2], 0);
613 +       input_set_abs_params(wm->input_dev, ABS_PRESSURE, abs_p[0], abs_p[1],
614 +                            abs_p[2], 0);
615 +       input_set_drvdata(wm->input_dev, wm);
616 +       wm->input_dev->dev.parent = dev;
617 +       ret = input_register_device(wm->input_dev);
618 +       if (ret < 0) {
619 +               input_free_device(wm->input_dev);
620 +               kfree(wm);
621 +               return -ENOMEM;
622 +       }
623 +
624 +       /* set up physical characteristics */
625 +       wm->codec->phy_init(wm);
626 +
627 +       /* load gpio cache */
628 +       wm->gpio[0] = wm97xx_reg_read(wm, AC97_GPIO_CFG);
629 +       wm->gpio[1] = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
630 +       wm->gpio[2] = wm97xx_reg_read(wm, AC97_GPIO_STICKY);
631 +       wm->gpio[3] = wm97xx_reg_read(wm, AC97_GPIO_WAKEUP);
632 +       wm->gpio[4] = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
633 +       wm->gpio[5] = wm97xx_reg_read(wm, AC97_MISC_AFE);
634 +
635 +       /* register our battery device */
636 +       wm->battery_dev = platform_device_alloc("wm97xx-battery", 0);
637 +       if (!wm->battery_dev)
638 +               goto batt_err;
639 +       platform_set_drvdata(wm->battery_dev, wm);
640 +       wm->battery_dev->dev.parent = dev;
641 +       ret = platform_device_register(wm->battery_dev);
642 +       if (ret < 0)
643 +               goto batt_reg_err;
644 +
645 +       /* register our extended touch device (for machine specific
646 +        * extensions) */
647 +       wm->touch_dev = platform_device_alloc("wm97xx-touch", 0);
648 +       if (!wm->touch_dev)
649 +               goto touch_err;
650 +       platform_set_drvdata(wm->touch_dev, wm);
651 +       wm->touch_dev->dev.parent = dev;
652 +       ret = platform_device_register(wm->touch_dev);
653 +       if (ret < 0)
654 +               goto touch_reg_err;
655 +
656 +       return ret;
657 +
658 + touch_reg_err:
659 +       platform_device_put(wm->touch_dev);
660 + touch_err:
661 +       platform_device_unregister(wm->battery_dev);
662 + batt_reg_err:
663 +       platform_device_put(wm->battery_dev);
664 + batt_err:
665 +       input_unregister_device(wm->input_dev);
666 +       kfree(wm);
667 +       return ret;
668 +}
669 +
670 +static int wm97xx_remove(struct device *dev)
671 +{
672 +       struct wm97xx *wm = dev_get_drvdata(dev);
673 +
674 +       platform_device_unregister(wm->battery_dev);
675 +       platform_device_unregister(wm->touch_dev);
676 +       input_unregister_device(wm->input_dev);
677 +
678 +       kfree(wm);
679 +       return 0;
680 +}
681 +
682 +#ifdef CONFIG_PM
683 +static int wm97xx_resume(struct device *dev)
684 +{
685 +       struct wm97xx *wm = dev_get_drvdata(dev);
686 +
687 +       /* restore digitiser and gpios */
688 +       if (wm->id == WM9713_ID2) {
689 +               wm97xx_reg_write(wm, AC97_WM9713_DIG1, wm->dig[0]);
690 +               wm97xx_reg_write(wm, 0x5a, wm->misc);
691 +               if (wm->input_dev->users) {
692 +                       u16 reg;
693 +                       reg = wm97xx_reg_read(wm, AC97_EXTENDED_MID) & 0x7fff;
694 +                       wm97xx_reg_write(wm, AC97_EXTENDED_MID, reg);
695 +               }
696 +       }
697 +
698 +       wm97xx_reg_write(wm, AC97_WM9713_DIG2, wm->dig[1]);
699 +       wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig[2]);
700 +
701 +       wm97xx_reg_write(wm, AC97_GPIO_CFG, wm->gpio[0]);
702 +       wm97xx_reg_write(wm, AC97_GPIO_POLARITY, wm->gpio[1]);
703 +       wm97xx_reg_write(wm, AC97_GPIO_STICKY, wm->gpio[2]);
704 +       wm97xx_reg_write(wm, AC97_GPIO_WAKEUP, wm->gpio[3]);
705 +       wm97xx_reg_write(wm, AC97_GPIO_STATUS, wm->gpio[4]);
706 +       wm97xx_reg_write(wm, AC97_MISC_AFE, wm->gpio[5]);
707 +
708 +       return 0;
709 +}
710 +
711 +#else
712 +#define wm97xx_resume          NULL
713 +#endif
714 +
715 +/*
716 + * Machine specific operations
717 + */
718 +int wm97xx_register_mach_ops(struct wm97xx *wm,
719 +                            struct wm97xx_mach_ops *mach_ops)
720 +{
721 +       mutex_lock(&wm->codec_mutex);
722 +       if (wm->mach_ops) {
723 +               mutex_unlock(&wm->codec_mutex);
724 +               return -EINVAL;
725 +       }
726 +       wm->mach_ops = mach_ops;
727 +       mutex_unlock(&wm->codec_mutex);
728 +       return 0;
729 +}
730 +EXPORT_SYMBOL_GPL(wm97xx_register_mach_ops);
731 +
732 +void wm97xx_unregister_mach_ops(struct wm97xx *wm)
733 +{
734 +       mutex_lock(&wm->codec_mutex);
735 +       wm->mach_ops = NULL;
736 +       mutex_unlock(&wm->codec_mutex);
737 +}
738 +EXPORT_SYMBOL_GPL(wm97xx_unregister_mach_ops);
739 +
740 +static struct device_driver wm97xx_driver = {
741 +       .name =         "ac97",
742 +       .bus =          &ac97_bus_type,
743 +       .owner =        THIS_MODULE,
744 +       .probe =        wm97xx_probe,
745 +       .remove =       wm97xx_remove,
746 +       .resume =       wm97xx_resume,
747 +};
748 +
749 +static int __init wm97xx_init(void)
750 +{
751 +       return driver_register(&wm97xx_driver);
752 +}
753 +
754 +static void __exit wm97xx_exit(void)
755 +{
756 +       driver_unregister(&wm97xx_driver);
757 +}
758 +
759 +module_init(wm97xx_init);
760 +module_exit(wm97xx_exit);
761 +
762 +/* Module information */
763 +MODULE_AUTHOR("Liam Girdwood <liam.girdwood@wolfsonmicro.com>");
764 +MODULE_DESCRIPTION("WM97xx Core - Touch Screen / AUX ADC / GPIO Driver");
765 +MODULE_LICENSE("GPL");
766 diff --git a/include/linux/wm97xx.h b/include/linux/wm97xx.h
767 new file mode 100644
768 index 0000000..fc6e0b3
769 --- /dev/null
770 +++ b/include/linux/wm97xx.h
771 @@ -0,0 +1,309 @@
772 +
773 +/*
774 + * Register bits and API for Wolfson WM97xx series of codecs
775 + */
776 +
777 +#ifndef _LINUX_WM97XX_H
778 +#define _LINUX_WM97XX_H
779 +
780 +#include <sound/driver.h>
781 +#include <sound/core.h>
782 +#include <sound/pcm.h>
783 +#include <sound/ac97_codec.h>
784 +#include <sound/initval.h>
785 +#include <linux/types.h>
786 +#include <linux/list.h>
787 +#include <linux/input.h>       /* Input device layer */
788 +#include <linux/platform_device.h>
789 +
790 +/*
791 + * WM97xx AC97 Touchscreen registers
792 + */
793 +#define AC97_WM97XX_DIGITISER1         0x76
794 +#define AC97_WM97XX_DIGITISER2         0x78
795 +#define AC97_WM97XX_DIGITISER_RD       0x7a
796 +#define AC97_WM9713_DIG1               0x74
797 +#define AC97_WM9713_DIG2               AC97_WM97XX_DIGITISER1
798 +#define AC97_WM9713_DIG3               AC97_WM97XX_DIGITISER2
799 +
800 +/*
801 + * WM97xx register bits
802 + */
803 +#define WM97XX_POLL            0x8000  /* initiate a polling measurement */
804 +#define WM97XX_ADCSEL_X                0x1000  /* x coord measurement */
805 +#define WM97XX_ADCSEL_Y                0x2000  /* y coord measurement */
806 +#define WM97XX_ADCSEL_PRES     0x3000  /* pressure measurement */
807 +#define WM97XX_ADCSEL_MASK     0x7000
808 +#define WM97XX_COO             0x0800  /* enable coordinate mode */
809 +#define WM97XX_CTC             0x0400  /* enable continuous mode */
810 +#define WM97XX_CM_RATE_93      0x0000  /* 93.75Hz continuous rate */
811 +#define WM97XX_CM_RATE_187     0x0100  /* 187.5Hz continuous rate */
812 +#define WM97XX_CM_RATE_375     0x0200  /* 375Hz continuous rate */
813 +#define WM97XX_CM_RATE_750     0x0300  /* 750Hz continuous rate */
814 +#define WM97XX_CM_RATE_8K      0x00f0  /* 8kHz continuous rate */
815 +#define WM97XX_CM_RATE_12K     0x01f0  /* 12kHz continuous rate */
816 +#define WM97XX_CM_RATE_24K     0x02f0  /* 24kHz continuous rate */
817 +#define WM97XX_CM_RATE_48K     0x03f0  /* 48kHz continuous rate */
818 +#define WM97XX_CM_RATE_MASK    0x03f0
819 +#define WM97XX_RATE(i)         (((i & 3) << 8) | ((i & 4) ? 0xf0 : 0))
820 +#define WM97XX_DELAY(i)                ((i << 4) & 0x00f0)     /* sample delay times */
821 +#define WM97XX_DELAY_MASK      0x00f0
822 +#define WM97XX_SLEN            0x0008  /* slot read back enable */
823 +#define WM97XX_SLT(i)          ((i - 5) & 0x7) /* panel slot (5-11) */
824 +#define WM97XX_SLT_MASK                0x0007
825 +#define WM97XX_PRP_DETW                0x4000  /* detect on, digitise off, wake */
826 +#define WM97XX_PRP_DET         0x8000  /* detect on, digitise off, no wake */
827 +#define WM97XX_PRP_DET_DIG     0xc000  /* setect on, digitise on */
828 +#define WM97XX_RPR             0x2000  /* wake up on pen down */
829 +#define WM97XX_PEN_DOWN                0x8000  /* pen is down */
830 +#define WM97XX_ADCSRC_MASK     0x7000  /* ADC source mask */
831 +
832 +#define WM97XX_AUX_ID1         0x8001
833 +#define WM97XX_AUX_ID2         0x8002
834 +#define WM97XX_AUX_ID3         0x8003
835 +#define WM97XX_AUX_ID4         0x8004
836 +
837 +
838 +/* WM9712 Bits */
839 +#define WM9712_45W             0x1000  /* set for 5-wire touchscreen */
840 +#define WM9712_PDEN            0x0800  /* measure only when pen down */
841 +#define WM9712_WAIT            0x0200  /* wait until adc is read before next sample */
842 +#define WM9712_PIL             0x0100  /* current used for pressure measurement. set 400uA else 200uA */
843 +#define WM9712_MASK_HI         0x0040  /* hi on mask pin (47) stops conversions */
844 +#define WM9712_MASK_EDGE       0x0080  /* rising/falling edge on pin delays sample */
845 +#define        WM9712_MASK_SYNC        0x00c0  /* rising/falling edge on mask initiates sample */
846 +#define WM9712_RPU(i)          (i&0x3f)        /* internal pull up on pen detect (64k / rpu) */
847 +#define WM9712_PD(i)           (0x1 << i)      /* power management */
848 +
849 +/* WM9712 Registers */
850 +#define AC97_WM9712_POWER      0x24
851 +#define AC97_WM9712_REV                0x58
852 +
853 +/* WM9705 Bits */
854 +#define WM9705_PDEN            0x1000  /* measure only when pen is down */
855 +#define WM9705_PINV            0x0800  /* inverts sense of pen down output */
856 +#define WM9705_BSEN            0x0400  /* BUSY flag enable, pin47 is 1 when busy */
857 +#define WM9705_BINV            0x0200  /* invert BUSY (pin47) output */
858 +#define WM9705_WAIT            0x0100  /* wait until adc is read before next sample */
859 +#define WM9705_PIL             0x0080  /* current used for pressure measurement. set 400uA else 200uA */
860 +#define WM9705_PHIZ            0x0040  /* set PHONE and PCBEEP inputs to high impedance */
861 +#define WM9705_MASK_HI         0x0010  /* hi on mask stops conversions */
862 +#define WM9705_MASK_EDGE       0x0020  /* rising/falling edge on pin delays sample */
863 +#define        WM9705_MASK_SYNC        0x0030  /* rising/falling edge on mask initiates sample */
864 +#define WM9705_PDD(i)          (i & 0x000f)    /* pen detect comparator threshold */
865 +
866 +
867 +/* WM9713 Bits */
868 +#define WM9713_PDPOL           0x0400  /* Pen down polarity */
869 +#define WM9713_POLL            0x0200  /* initiate a polling measurement */
870 +#define WM9713_CTC             0x0100  /* enable continuous mode */
871 +#define WM9713_ADCSEL_X                0x0002  /* X measurement */
872 +#define WM9713_ADCSEL_Y                0x0004  /* Y measurement */
873 +#define WM9713_ADCSEL_PRES     0x0008  /* Pressure measurement */
874 +#define WM9713_COO             0x0001  /* enable coordinate mode */
875 +#define WM9713_PDEN            0x0800  /* measure only when pen down */
876 +#define WM9713_ADCSEL_MASK     0x00fe  /* ADC selection mask */
877 +#define WM9713_WAIT            0x0200  /* coordinate wait */
878 +
879 +/* AUX ADC ID's */
880 +#define TS_COMP1               0x0
881 +#define TS_COMP2               0x1
882 +#define TS_BMON                        0x2
883 +#define TS_WIPER               0x3
884 +
885 +/* ID numbers */
886 +#define WM97XX_ID1             0x574d
887 +#define WM9712_ID2             0x4c12
888 +#define WM9705_ID2             0x4c05
889 +#define WM9713_ID2             0x4c13
890 +
891 +/* Codec GPIO's */
892 +#define WM97XX_MAX_GPIO                16
893 +#define WM97XX_GPIO_1          (1 << 1)
894 +#define WM97XX_GPIO_2          (1 << 2)
895 +#define WM97XX_GPIO_3          (1 << 3)
896 +#define WM97XX_GPIO_4          (1 << 4)
897 +#define WM97XX_GPIO_5          (1 << 5)
898 +#define WM97XX_GPIO_6          (1 << 6)
899 +#define WM97XX_GPIO_7          (1 << 7)
900 +#define WM97XX_GPIO_8          (1 << 8)
901 +#define WM97XX_GPIO_9          (1 << 9)
902 +#define WM97XX_GPIO_10         (1 << 10)
903 +#define WM97XX_GPIO_11         (1 << 11)
904 +#define WM97XX_GPIO_12         (1 << 12)
905 +#define WM97XX_GPIO_13         (1 << 13)
906 +#define WM97XX_GPIO_14         (1 << 14)
907 +#define WM97XX_GPIO_15         (1 << 15)
908 +
909 +
910 +#define AC97_LINK_FRAME                21      /* time in uS for AC97 link frame */
911 +
912 +
913 +/*---------------- Return codes from sample reading functions ---------------*/
914 +
915 +/* More data is available; call the sample gathering function again */
916 +#define RC_AGAIN                       0x00000001
917 +/* The returned sample is valid */
918 +#define RC_VALID                       0x00000002
919 +/* The pen is up (the first RC_VALID without RC_PENUP means pen is down) */
920 +#define RC_PENUP                       0x00000004
921 +/* The pen is down (RC_VALID implies RC_PENDOWN, but sometimes it is helpful
922 +   to tell the handler that the pen is down but we don't know yet his coords,
923 +   so the handler should not sleep or wait for pendown irq) */
924 +#define RC_PENDOWN                     0x00000008
925 +
926 +/*
927 + * The wm97xx driver provides a private API for writing platform-specific
928 + * drivers.
929 + */
930 +
931 +/* The structure used to return arch specific sampled data into */
932 +struct wm97xx_data {
933 +    int x;
934 +    int y;
935 +    int p;
936 +};
937 +
938 +/*
939 + * Codec GPIO status
940 + */
941 +enum wm97xx_gpio_status {
942 +    WM97XX_GPIO_HIGH,
943 +    WM97XX_GPIO_LOW
944 +};
945 +
946 +/*
947 + * Codec GPIO direction
948 + */
949 +enum wm97xx_gpio_dir {
950 +    WM97XX_GPIO_IN,
951 +    WM97XX_GPIO_OUT
952 +};
953 +
954 +/*
955 + * Codec GPIO polarity
956 + */
957 +enum wm97xx_gpio_pol {
958 +    WM97XX_GPIO_POL_HIGH,
959 +    WM97XX_GPIO_POL_LOW
960 +};
961 +
962 +/*
963 + * Codec GPIO sticky
964 + */
965 +enum wm97xx_gpio_sticky {
966 +    WM97XX_GPIO_STICKY,
967 +    WM97XX_GPIO_NOTSTICKY
968 +};
969 +
970 +/*
971 + * Codec GPIO wake
972 + */
973 +enum wm97xx_gpio_wake {
974 +    WM97XX_GPIO_WAKE,
975 +    WM97XX_GPIO_NOWAKE
976 +};
977 +
978 +/*
979 + * Digitiser ioctl commands
980 + */
981 +#define WM97XX_DIG_START       0x1
982 +#define WM97XX_DIG_STOP                0x2
983 +#define WM97XX_PHY_INIT                0x3
984 +#define WM97XX_AUX_PREPARE     0x4
985 +#define WM97XX_DIG_RESTORE     0x5
986 +
987 +struct wm97xx;
988 +
989 +extern struct wm97xx_codec_drv wm9705_codec;
990 +extern struct wm97xx_codec_drv wm9712_codec;
991 +extern struct wm97xx_codec_drv wm9713_codec;
992 +
993 +/*
994 + * Codec driver interface - allows mapping to WM9705/12/13 and newer codecs
995 + */
996 +struct wm97xx_codec_drv {
997 +       u16 id;
998 +       char *name;
999 +
1000 +       /* read 1 sample */
1001 +       int (*poll_sample) (struct wm97xx *, int adcsel, int *sample);
1002 +
1003 +       /* read X,Y,[P] in poll */
1004 +       int (*poll_touch) (struct wm97xx *, struct wm97xx_data *);
1005 +
1006 +       int (*acc_enable) (struct wm97xx *, int enable);
1007 +       void (*phy_init) (struct wm97xx *);
1008 +       void (*dig_enable) (struct wm97xx *, int enable);
1009 +       void (*dig_restore) (struct wm97xx *);
1010 +       void (*aux_prepare) (struct wm97xx *);
1011 +};
1012 +
1013 +
1014 +/* Machine specific and accelerated touch operations */
1015 +struct wm97xx_mach_ops {
1016 +
1017 +       /* accelerated touch readback - coords are transmited on AC97 link */
1018 +       int acc_enabled;
1019 +       void (*acc_pen_up) (struct wm97xx *);
1020 +       int (*acc_pen_down) (struct wm97xx *);
1021 +       int (*acc_startup) (struct wm97xx *);
1022 +       void (*acc_shutdown) (struct wm97xx *);
1023 +
1024 +       /* interrupt mask control - required for accelerated operation */
1025 +       void (*irq_enable) (struct wm97xx *, int enable);
1026 +
1027 +       /* pre and post sample - can be used to minimise any analog noise */
1028 +       void (*pre_sample) (int);  /* function to run before sampling */
1029 +       void (*post_sample) (int);  /* function to run after sampling */
1030 +};
1031 +
1032 +struct wm97xx {
1033 +       u16 dig[3], id, gpio[6], misc;  /* Cached codec registers */
1034 +       u16 dig_save[3];                /* saved during aux reading */
1035 +       struct wm97xx_codec_drv *codec; /* attached codec driver*/
1036 +       struct input_dev *input_dev;    /* touchscreen input device */
1037 +       struct snd_ac97 *ac97;          /* ALSA codec access */
1038 +       struct device *dev;             /* ALSA device */
1039 +       struct platform_device *battery_dev;
1040 +       struct platform_device *touch_dev;
1041 +       struct wm97xx_mach_ops *mach_ops;
1042 +       struct mutex codec_mutex;
1043 +       struct delayed_work ts_reader;  /* Used to poll touchscreen */
1044 +       unsigned long ts_reader_interval; /* Current interval for timer */
1045 +       unsigned long ts_reader_min_interval; /* Minimum interval */
1046 +       unsigned int pen_irq;           /* Pen IRQ number in use */
1047 +       struct workqueue_struct *ts_workq;
1048 +       struct work_struct pen_event_work;
1049 +       u16 acc_slot;                   /* AC97 slot used for acc touch data */
1050 +       u16 acc_rate;                   /* acc touch data rate */
1051 +       unsigned pen_is_down:1;         /* Pen is down */
1052 +       unsigned aux_waiting:1;         /* aux measurement waiting */
1053 +       unsigned pen_probably_down:1;   /* used in polling mode */
1054 +};
1055 +
1056 +/*
1057 + * Codec GPIO access (not supported on WM9705)
1058 + * This can be used to set/get codec GPIO and Virtual GPIO status.
1059 + */
1060 +enum wm97xx_gpio_status wm97xx_get_gpio(struct wm97xx *wm, u32 gpio);
1061 +void wm97xx_set_gpio(struct wm97xx *wm, u32 gpio,
1062 +                         enum wm97xx_gpio_status status);
1063 +void wm97xx_config_gpio(struct wm97xx *wm, u32 gpio,
1064 +                                    enum wm97xx_gpio_dir dir,
1065 +                                    enum wm97xx_gpio_pol pol,
1066 +                                    enum wm97xx_gpio_sticky sticky,
1067 +                                    enum wm97xx_gpio_wake wake);
1068 +
1069 +/* codec AC97 IO access */
1070 +int wm97xx_reg_read(struct wm97xx *wm, u16 reg);
1071 +void wm97xx_reg_write(struct wm97xx *wm, u16 reg, u16 val);
1072 +
1073 +/* aux adc readback */
1074 +int wm97xx_read_aux_adc(struct wm97xx *wm, u16 adcsel);
1075 +
1076 +/* machine ops */
1077 +int wm97xx_register_mach_ops(struct wm97xx *, struct wm97xx_mach_ops *);
1078 +void wm97xx_unregister_mach_ops(struct wm97xx *);
1079 +
1080 +#endif
1081 -- 
1082 1.5.3.8
1083