Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/davej/cpufreq
[linux-2.6] / drivers / input / touchscreen / wm97xx-core.c
1 /*
2  * wm97xx-core.c  --  Touch screen driver core for Wolfson WM9705, WM9712
3  *                    and WM9713 AC97 Codecs.
4  *
5  * Copyright 2003, 2004, 2005, 2006, 2007, 2008 Wolfson Microelectronics PLC.
6  * Author: Liam Girdwood
7  *         liam.girdwood@wolfsonmicro.com or linux@wolfsonmicro.com
8  * Parts Copyright : Ian Molton <spyro@f2s.com>
9  *                   Andrew Zabolotny <zap@homelink.ru>
10  *                   Russell King <rmk@arm.linux.org.uk>
11  *
12  *  This program is free software; you can redistribute  it and/or modify it
13  *  under  the terms of  the GNU General  Public License as published by the
14  *  Free Software Foundation;  either version 2 of the  License, or (at your
15  *  option) any later version.
16  *
17  * Notes:
18  *
19  *  Features:
20  *       - supports WM9705, WM9712, WM9713
21  *       - polling mode
22  *       - continuous mode (arch-dependent)
23  *       - adjustable rpu/dpp settings
24  *       - adjustable pressure current
25  *       - adjustable sample settle delay
26  *       - 4 and 5 wire touchscreens (5 wire is WM9712 only)
27  *       - pen down detection
28  *       - battery monitor
29  *       - sample AUX adcs
30  *       - power management
31  *       - codec GPIO
32  *       - codec event notification
33  * Todo
34  *       - Support for async sampling control for noisy LCDs.
35  *
36  */
37
38 #include <linux/module.h>
39 #include <linux/moduleparam.h>
40 #include <linux/version.h>
41 #include <linux/kernel.h>
42 #include <linux/init.h>
43 #include <linux/delay.h>
44 #include <linux/string.h>
45 #include <linux/proc_fs.h>
46 #include <linux/pm.h>
47 #include <linux/interrupt.h>
48 #include <linux/bitops.h>
49 #include <linux/workqueue.h>
50 #include <linux/wm97xx.h>
51 #include <linux/uaccess.h>
52 #include <linux/io.h>
53
54 #define TS_NAME                 "wm97xx"
55 #define WM_CORE_VERSION         "1.00"
56 #define DEFAULT_PRESSURE        0xb0c0
57
58
59 /*
60  * Touchscreen absolute values
61  *
62  * These parameters are used to help the input layer discard out of
63  * range readings and reduce jitter etc.
64  *
65  *   o min, max:- indicate the min and max values your touch screen returns
66  *   o fuzz:- use a higher number to reduce jitter
67  *
68  * The default values correspond to Mainstone II in QVGA mode
69  *
70  * Please read
71  * Documentation/input/input-programming.txt for more details.
72  */
73
74 static int abs_x[3] = {350, 3900, 5};
75 module_param_array(abs_x, int, NULL, 0);
76 MODULE_PARM_DESC(abs_x, "Touchscreen absolute X min, max, fuzz");
77
78 static int abs_y[3] = {320, 3750, 40};
79 module_param_array(abs_y, int, NULL, 0);
80 MODULE_PARM_DESC(abs_y, "Touchscreen absolute Y min, max, fuzz");
81
82 static int abs_p[3] = {0, 150, 4};
83 module_param_array(abs_p, int, NULL, 0);
84 MODULE_PARM_DESC(abs_p, "Touchscreen absolute Pressure min, max, fuzz");
85
86 /*
87  * wm97xx IO access, all IO locking done by AC97 layer
88  */
89 int wm97xx_reg_read(struct wm97xx *wm, u16 reg)
90 {
91         if (wm->ac97)
92                 return wm->ac97->bus->ops->read(wm->ac97, reg);
93         else
94                 return -1;
95 }
96 EXPORT_SYMBOL_GPL(wm97xx_reg_read);
97
98 void wm97xx_reg_write(struct wm97xx *wm, u16 reg, u16 val)
99 {
100         /* cache digitiser registers */
101         if (reg >= AC97_WM9713_DIG1 && reg <= AC97_WM9713_DIG3)
102                 wm->dig[(reg - AC97_WM9713_DIG1) >> 1] = val;
103
104         /* cache gpio regs */
105         if (reg >= AC97_GPIO_CFG && reg <= AC97_MISC_AFE)
106                 wm->gpio[(reg - AC97_GPIO_CFG) >> 1] = val;
107
108         /* wm9713 irq reg */
109         if (reg == 0x5a)
110                 wm->misc = val;
111
112         if (wm->ac97)
113                 wm->ac97->bus->ops->write(wm->ac97, reg, val);
114 }
115 EXPORT_SYMBOL_GPL(wm97xx_reg_write);
116
117 /**
118  * wm97xx_read_aux_adc - Read the aux adc.
119  * @wm: wm97xx device.
120  * @adcsel: codec ADC to be read
121  *
122  * Reads the selected AUX ADC.
123  */
124
125 int wm97xx_read_aux_adc(struct wm97xx *wm, u16 adcsel)
126 {
127         int power_adc = 0, auxval;
128         u16 power = 0;
129
130         /* get codec */
131         mutex_lock(&wm->codec_mutex);
132
133         /* When the touchscreen is not in use, we may have to power up
134          * the AUX ADC before we can use sample the AUX inputs->
135          */
136         if (wm->id == WM9713_ID2 &&
137             (power = wm97xx_reg_read(wm, AC97_EXTENDED_MID)) & 0x8000) {
138                 power_adc = 1;
139                 wm97xx_reg_write(wm, AC97_EXTENDED_MID, power & 0x7fff);
140         }
141
142         /* Prepare the codec for AUX reading */
143         wm->codec->aux_prepare(wm);
144
145         /* Turn polling mode on to read AUX ADC */
146         wm->pen_probably_down = 1;
147         wm->codec->poll_sample(wm, adcsel, &auxval);
148
149         if (power_adc)
150                 wm97xx_reg_write(wm, AC97_EXTENDED_MID, power | 0x8000);
151
152         wm->codec->dig_restore(wm);
153
154         wm->pen_probably_down = 0;
155
156         mutex_unlock(&wm->codec_mutex);
157         return auxval & 0xfff;
158 }
159 EXPORT_SYMBOL_GPL(wm97xx_read_aux_adc);
160
161 /**
162  * wm97xx_get_gpio - Get the status of a codec GPIO.
163  * @wm: wm97xx device.
164  * @gpio: gpio
165  *
166  * Get the status of a codec GPIO pin
167  */
168
169 enum wm97xx_gpio_status wm97xx_get_gpio(struct wm97xx *wm, u32 gpio)
170 {
171         u16 status;
172         enum wm97xx_gpio_status ret;
173
174         mutex_lock(&wm->codec_mutex);
175         status = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
176
177         if (status & gpio)
178                 ret = WM97XX_GPIO_HIGH;
179         else
180                 ret = WM97XX_GPIO_LOW;
181
182         mutex_unlock(&wm->codec_mutex);
183         return ret;
184 }
185 EXPORT_SYMBOL_GPL(wm97xx_get_gpio);
186
187 /**
188  * wm97xx_set_gpio - Set the status of a codec GPIO.
189  * @wm: wm97xx device.
190  * @gpio: gpio
191  *
192  *
193  * Set the status of a codec GPIO pin
194  */
195
196 void wm97xx_set_gpio(struct wm97xx *wm, u32 gpio,
197                                 enum wm97xx_gpio_status status)
198 {
199         u16 reg;
200
201         mutex_lock(&wm->codec_mutex);
202         reg = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
203
204         if (status & WM97XX_GPIO_HIGH)
205                 reg |= gpio;
206         else
207                 reg &= ~gpio;
208
209         if (wm->id == WM9712_ID2)
210                 wm97xx_reg_write(wm, AC97_GPIO_STATUS, reg << 1);
211         else
212                 wm97xx_reg_write(wm, AC97_GPIO_STATUS, reg);
213         mutex_unlock(&wm->codec_mutex);
214 }
215 EXPORT_SYMBOL_GPL(wm97xx_set_gpio);
216
217 /*
218  * Codec GPIO pin configuration, this sets pin direction, polarity,
219  * stickyness and wake up.
220  */
221 void wm97xx_config_gpio(struct wm97xx *wm, u32 gpio, enum wm97xx_gpio_dir dir,
222                    enum wm97xx_gpio_pol pol, enum wm97xx_gpio_sticky sticky,
223                    enum wm97xx_gpio_wake wake)
224 {
225         u16 reg;
226
227         mutex_lock(&wm->codec_mutex);
228         reg = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
229
230         if (pol == WM97XX_GPIO_POL_HIGH)
231                 reg |= gpio;
232         else
233                 reg &= ~gpio;
234
235         wm97xx_reg_write(wm, AC97_GPIO_POLARITY, reg);
236         reg = wm97xx_reg_read(wm, AC97_GPIO_STICKY);
237
238         if (sticky == WM97XX_GPIO_STICKY)
239                 reg |= gpio;
240         else
241                 reg &= ~gpio;
242
243         wm97xx_reg_write(wm, AC97_GPIO_STICKY, reg);
244         reg = wm97xx_reg_read(wm, AC97_GPIO_WAKEUP);
245
246         if (wake == WM97XX_GPIO_WAKE)
247                 reg |= gpio;
248         else
249                 reg &= ~gpio;
250
251         wm97xx_reg_write(wm, AC97_GPIO_WAKEUP, reg);
252         reg = wm97xx_reg_read(wm, AC97_GPIO_CFG);
253
254         if (dir == WM97XX_GPIO_IN)
255                 reg |= gpio;
256         else
257                 reg &= ~gpio;
258
259         wm97xx_reg_write(wm, AC97_GPIO_CFG, reg);
260         mutex_unlock(&wm->codec_mutex);
261 }
262 EXPORT_SYMBOL_GPL(wm97xx_config_gpio);
263
264 /*
265  * Configure the WM97XX_PRP value to use while system is suspended.
266  * If a value other than 0 is set then WM97xx pen detection will be
267  * left enabled in the configured mode while the system is in suspend,
268  * the device has users and suspend has not been disabled via the
269  * wakeup sysfs entries.
270  *
271  * @wm:   WM97xx device to configure
272  * @mode: WM97XX_PRP value to configure while suspended
273  */
274 void wm97xx_set_suspend_mode(struct wm97xx *wm, u16 mode)
275 {
276         wm->suspend_mode = mode;
277         device_init_wakeup(&wm->input_dev->dev, mode != 0);
278 }
279 EXPORT_SYMBOL_GPL(wm97xx_set_suspend_mode);
280
281 /*
282  * Handle a pen down interrupt.
283  */
284 static void wm97xx_pen_irq_worker(struct work_struct *work)
285 {
286         struct wm97xx *wm = container_of(work, struct wm97xx, pen_event_work);
287         int pen_was_down = wm->pen_is_down;
288
289         /* do we need to enable the touch panel reader */
290         if (wm->id == WM9705_ID2) {
291                 if (wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD) &
292                                         WM97XX_PEN_DOWN)
293                         wm->pen_is_down = 1;
294                 else
295                         wm->pen_is_down = 0;
296         } else {
297                 u16 status, pol;
298                 mutex_lock(&wm->codec_mutex);
299                 status = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
300                 pol = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
301
302                 if (WM97XX_GPIO_13 & pol & status) {
303                         wm->pen_is_down = 1;
304                         wm97xx_reg_write(wm, AC97_GPIO_POLARITY, pol &
305                                                 ~WM97XX_GPIO_13);
306                 } else {
307                         wm->pen_is_down = 0;
308                         wm97xx_reg_write(wm, AC97_GPIO_POLARITY, pol |
309                                          WM97XX_GPIO_13);
310                 }
311
312                 if (wm->id == WM9712_ID2)
313                         wm97xx_reg_write(wm, AC97_GPIO_STATUS, (status &
314                                                 ~WM97XX_GPIO_13) << 1);
315                 else
316                         wm97xx_reg_write(wm, AC97_GPIO_STATUS, status &
317                                                 ~WM97XX_GPIO_13);
318                 mutex_unlock(&wm->codec_mutex);
319         }
320
321         /* If the system is not using continuous mode or it provides a
322          * pen down operation then we need to schedule polls while the
323          * pen is down.  Otherwise the machine driver is responsible
324          * for scheduling reads.
325          */
326         if (!wm->mach_ops->acc_enabled || wm->mach_ops->acc_pen_down) {
327                 if (wm->pen_is_down && !pen_was_down) {
328                         /* Data is not availiable immediately on pen down */
329                         queue_delayed_work(wm->ts_workq, &wm->ts_reader, 1);
330                 }
331
332                 /* Let ts_reader report the pen up for debounce. */
333                 if (!wm->pen_is_down && pen_was_down)
334                         wm->pen_is_down = 1;
335         }
336
337         if (!wm->pen_is_down && wm->mach_ops->acc_enabled)
338                 wm->mach_ops->acc_pen_up(wm);
339
340         wm->mach_ops->irq_enable(wm, 1);
341 }
342
343 /*
344  * Codec PENDOWN irq handler
345  *
346  * We have to disable the codec interrupt in the handler because it
347  * can take upto 1ms to clear the interrupt source. We schedule a task
348  * in a work queue to do the actual interaction with the chip.  The
349  * interrupt is then enabled again in the slow handler when the source
350  * has been cleared.
351  */
352 static irqreturn_t wm97xx_pen_interrupt(int irq, void *dev_id)
353 {
354         struct wm97xx *wm = dev_id;
355
356         if (!work_pending(&wm->pen_event_work)) {
357                 wm->mach_ops->irq_enable(wm, 0);
358                 queue_work(wm->ts_workq, &wm->pen_event_work);
359         }
360
361         return IRQ_HANDLED;
362 }
363
364 /*
365  * initialise pen IRQ handler and workqueue
366  */
367 static int wm97xx_init_pen_irq(struct wm97xx *wm)
368 {
369         u16 reg;
370
371         /* If an interrupt is supplied an IRQ enable operation must also be
372          * provided. */
373         BUG_ON(!wm->mach_ops->irq_enable);
374
375         if (request_irq(wm->pen_irq, wm97xx_pen_interrupt,
376                         IRQF_SHARED | IRQF_SAMPLE_RANDOM,
377                         "wm97xx-pen", wm)) {
378                 dev_err(wm->dev,
379                         "Failed to register pen down interrupt, polling");
380                 wm->pen_irq = 0;
381                 return -EINVAL;
382         }
383
384         /* Configure GPIO as interrupt source on WM971x */
385         if (wm->id != WM9705_ID2) {
386                 BUG_ON(!wm->mach_ops->irq_gpio);
387                 reg = wm97xx_reg_read(wm, AC97_MISC_AFE);
388                 wm97xx_reg_write(wm, AC97_MISC_AFE,
389                                 reg & ~(wm->mach_ops->irq_gpio));
390                 reg = wm97xx_reg_read(wm, 0x5a);
391                 wm97xx_reg_write(wm, 0x5a, reg & ~0x0001);
392         }
393
394         return 0;
395 }
396
397 static int wm97xx_read_samples(struct wm97xx *wm)
398 {
399         struct wm97xx_data data;
400         int rc;
401
402         mutex_lock(&wm->codec_mutex);
403
404         if (wm->mach_ops && wm->mach_ops->acc_enabled)
405                 rc = wm->mach_ops->acc_pen_down(wm);
406         else
407                 rc = wm->codec->poll_touch(wm, &data);
408
409         if (rc & RC_PENUP) {
410                 if (wm->pen_is_down) {
411                         wm->pen_is_down = 0;
412                         dev_dbg(wm->dev, "pen up\n");
413                         input_report_abs(wm->input_dev, ABS_PRESSURE, 0);
414                         input_sync(wm->input_dev);
415                 } else if (!(rc & RC_AGAIN)) {
416                         /* We need high frequency updates only while
417                         * pen is down, the user never will be able to
418                         * touch screen faster than a few times per
419                         * second... On the other hand, when the user
420                         * is actively working with the touchscreen we
421                         * don't want to lose the quick response. So we
422                         * will slowly increase sleep time after the
423                         * pen is up and quicky restore it to ~one task
424                         * switch when pen is down again.
425                         */
426                         if (wm->ts_reader_interval < HZ / 10)
427                                 wm->ts_reader_interval++;
428                 }
429
430         } else if (rc & RC_VALID) {
431                 dev_dbg(wm->dev,
432                         "pen down: x=%x:%d, y=%x:%d, pressure=%x:%d\n",
433                         data.x >> 12, data.x & 0xfff, data.y >> 12,
434                         data.y & 0xfff, data.p >> 12, data.p & 0xfff);
435                 input_report_abs(wm->input_dev, ABS_X, data.x & 0xfff);
436                 input_report_abs(wm->input_dev, ABS_Y, data.y & 0xfff);
437                 input_report_abs(wm->input_dev, ABS_PRESSURE, data.p & 0xfff);
438                 input_sync(wm->input_dev);
439                 wm->pen_is_down = 1;
440                 wm->ts_reader_interval = wm->ts_reader_min_interval;
441         } else if (rc & RC_PENDOWN) {
442                 dev_dbg(wm->dev, "pen down\n");
443                 wm->pen_is_down = 1;
444                 wm->ts_reader_interval = wm->ts_reader_min_interval;
445         }
446
447         mutex_unlock(&wm->codec_mutex);
448         return rc;
449 }
450
451 /*
452 * The touchscreen sample reader.
453 */
454 static void wm97xx_ts_reader(struct work_struct *work)
455 {
456         int rc;
457         struct wm97xx *wm = container_of(work, struct wm97xx, ts_reader.work);
458
459         BUG_ON(!wm->codec);
460
461         do {
462                 rc = wm97xx_read_samples(wm);
463         } while (rc & RC_AGAIN);
464
465         if (wm->pen_is_down || !wm->pen_irq)
466                 queue_delayed_work(wm->ts_workq, &wm->ts_reader,
467                                    wm->ts_reader_interval);
468 }
469
470 /**
471  * wm97xx_ts_input_open - Open the touch screen input device.
472  * @idev:       Input device to be opened.
473  *
474  * Called by the input sub system to open a wm97xx touchscreen device.
475  * Starts the touchscreen thread and touch digitiser.
476  */
477 static int wm97xx_ts_input_open(struct input_dev *idev)
478 {
479         struct wm97xx *wm = input_get_drvdata(idev);
480
481         wm->ts_workq = create_singlethread_workqueue("kwm97xx");
482         if (wm->ts_workq == NULL) {
483                 dev_err(wm->dev,
484                         "Failed to create workqueue\n");
485                 return -EINVAL;
486         }
487
488         /* start digitiser */
489         if (wm->mach_ops && wm->mach_ops->acc_enabled)
490                 wm->codec->acc_enable(wm, 1);
491         wm->codec->dig_enable(wm, 1);
492
493         INIT_DELAYED_WORK(&wm->ts_reader, wm97xx_ts_reader);
494         INIT_WORK(&wm->pen_event_work, wm97xx_pen_irq_worker);
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
522  * device.  Kills the touchscreen thread and stops the touch
523  * digitiser.
524  */
525
526 static void wm97xx_ts_input_close(struct input_dev *idev)
527 {
528         struct wm97xx *wm = input_get_drvdata(idev);
529         u16 reg;
530
531         if (wm->pen_irq) {
532                 /* Return the interrupt to GPIO usage (disabling it) */
533                 if (wm->id != WM9705_ID2) {
534                         BUG_ON(!wm->mach_ops->irq_gpio);
535                         reg = wm97xx_reg_read(wm, AC97_MISC_AFE);
536                         wm97xx_reg_write(wm, AC97_MISC_AFE,
537                                          reg | wm->mach_ops->irq_gpio);
538                 }
539
540                 free_irq(wm->pen_irq, wm);
541         }
542
543         wm->pen_is_down = 0;
544
545         /* Balance out interrupt disables/enables */
546         if (cancel_work_sync(&wm->pen_event_work))
547                 wm->mach_ops->irq_enable(wm, 1);
548
549         /* ts_reader rearms itself so we need to explicitly stop it
550          * before we destroy the workqueue.
551          */
552         cancel_delayed_work_sync(&wm->ts_reader);
553
554         destroy_workqueue(wm->ts_workq);
555
556         /* stop digitiser */
557         wm->codec->dig_enable(wm, 0);
558         if (wm->mach_ops && wm->mach_ops->acc_enabled)
559                 wm->codec->acc_enable(wm, 0);
560 }
561
562 static int wm97xx_probe(struct device *dev)
563 {
564         struct wm97xx *wm;
565         int ret = 0, id = 0;
566
567         wm = kzalloc(sizeof(struct wm97xx), GFP_KERNEL);
568         if (!wm)
569                 return -ENOMEM;
570         mutex_init(&wm->codec_mutex);
571
572         wm->dev = dev;
573         dev->driver_data = wm;
574         wm->ac97 = to_ac97_t(dev);
575
576         /* check that we have a supported codec */
577         id = wm97xx_reg_read(wm, AC97_VENDOR_ID1);
578         if (id != WM97XX_ID1) {
579                 dev_err(dev, "Device with vendor %04x is not a wm97xx\n", id);
580                 ret = -ENODEV;
581                 goto alloc_err;
582         }
583
584         wm->id = wm97xx_reg_read(wm, AC97_VENDOR_ID2);
585
586         dev_info(wm->dev, "detected a wm97%02x codec\n", wm->id & 0xff);
587
588         switch (wm->id & 0xff) {
589 #ifdef CONFIG_TOUCHSCREEN_WM9705
590         case 0x05:
591                 wm->codec = &wm9705_codec;
592                 break;
593 #endif
594 #ifdef CONFIG_TOUCHSCREEN_WM9712
595         case 0x12:
596                 wm->codec = &wm9712_codec;
597                 break;
598 #endif
599 #ifdef CONFIG_TOUCHSCREEN_WM9713
600         case 0x13:
601                 wm->codec = &wm9713_codec;
602                 break;
603 #endif
604         default:
605                 dev_err(wm->dev, "Support for wm97%02x not compiled in.\n",
606                         wm->id & 0xff);
607                 ret = -ENODEV;
608                 goto alloc_err;
609         }
610
611         /* set up physical characteristics */
612         wm->codec->phy_init(wm);
613
614         /* load gpio cache */
615         wm->gpio[0] = wm97xx_reg_read(wm, AC97_GPIO_CFG);
616         wm->gpio[1] = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
617         wm->gpio[2] = wm97xx_reg_read(wm, AC97_GPIO_STICKY);
618         wm->gpio[3] = wm97xx_reg_read(wm, AC97_GPIO_WAKEUP);
619         wm->gpio[4] = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
620         wm->gpio[5] = wm97xx_reg_read(wm, AC97_MISC_AFE);
621
622         wm->input_dev = input_allocate_device();
623         if (wm->input_dev == NULL) {
624                 ret = -ENOMEM;
625                 goto alloc_err;
626         }
627
628         /* set up touch configuration */
629         wm->input_dev->name = "wm97xx touchscreen";
630         wm->input_dev->phys = "wm97xx";
631         wm->input_dev->open = wm97xx_ts_input_open;
632         wm->input_dev->close = wm97xx_ts_input_close;
633         set_bit(EV_ABS, wm->input_dev->evbit);
634         set_bit(ABS_X, wm->input_dev->absbit);
635         set_bit(ABS_Y, wm->input_dev->absbit);
636         set_bit(ABS_PRESSURE, wm->input_dev->absbit);
637         input_set_abs_params(wm->input_dev, ABS_X, abs_x[0], abs_x[1],
638                              abs_x[2], 0);
639         input_set_abs_params(wm->input_dev, ABS_Y, abs_y[0], abs_y[1],
640                              abs_y[2], 0);
641         input_set_abs_params(wm->input_dev, ABS_PRESSURE, abs_p[0], abs_p[1],
642                              abs_p[2], 0);
643         input_set_drvdata(wm->input_dev, wm);
644         wm->input_dev->dev.parent = dev;
645         ret = input_register_device(wm->input_dev);
646         if (ret < 0)
647                 goto dev_alloc_err;
648
649         /* register our battery device */
650         wm->battery_dev = platform_device_alloc("wm97xx-battery", -1);
651         if (!wm->battery_dev) {
652                 ret = -ENOMEM;
653                 goto batt_err;
654         }
655         platform_set_drvdata(wm->battery_dev, wm);
656         wm->battery_dev->dev.parent = dev;
657         ret = platform_device_add(wm->battery_dev);
658         if (ret < 0)
659                 goto batt_reg_err;
660
661         /* register our extended touch device (for machine specific
662          * extensions) */
663         wm->touch_dev = platform_device_alloc("wm97xx-touch", -1);
664         if (!wm->touch_dev) {
665                 ret = -ENOMEM;
666                 goto touch_err;
667         }
668         platform_set_drvdata(wm->touch_dev, wm);
669         wm->touch_dev->dev.parent = dev;
670         ret = platform_device_add(wm->touch_dev);
671         if (ret < 0)
672                 goto touch_reg_err;
673
674         return ret;
675
676  touch_reg_err:
677         platform_device_put(wm->touch_dev);
678  touch_err:
679         platform_device_unregister(wm->battery_dev);
680         wm->battery_dev = NULL;
681  batt_reg_err:
682         platform_device_put(wm->battery_dev);
683  batt_err:
684         input_unregister_device(wm->input_dev);
685         wm->input_dev = NULL;
686  dev_alloc_err:
687         input_free_device(wm->input_dev);
688  alloc_err:
689         kfree(wm);
690
691         return ret;
692 }
693
694 static int wm97xx_remove(struct device *dev)
695 {
696         struct wm97xx *wm = dev_get_drvdata(dev);
697
698         platform_device_unregister(wm->battery_dev);
699         platform_device_unregister(wm->touch_dev);
700         input_unregister_device(wm->input_dev);
701         kfree(wm);
702
703         return 0;
704 }
705
706 #ifdef CONFIG_PM
707 static int wm97xx_suspend(struct device *dev, pm_message_t state)
708 {
709         struct wm97xx *wm = dev_get_drvdata(dev);
710         u16 reg;
711         int suspend_mode;
712
713         if (device_may_wakeup(&wm->input_dev->dev))
714                 suspend_mode = wm->suspend_mode;
715         else
716                 suspend_mode = 0;
717
718         if (wm->input_dev->users)
719                 cancel_delayed_work_sync(&wm->ts_reader);
720
721         /* Power down the digitiser (bypassing the cache for resume) */
722         reg = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER2);
723         reg &= ~WM97XX_PRP_DET_DIG;
724         if (wm->input_dev->users)
725                 reg |= suspend_mode;
726         wm->ac97->bus->ops->write(wm->ac97, AC97_WM97XX_DIGITISER2, reg);
727
728         /* WM9713 has an additional power bit - turn it off if there
729          * are no users or if suspend mode is zero. */
730         if (wm->id == WM9713_ID2 &&
731             (!wm->input_dev->users || !suspend_mode)) {
732                 reg = wm97xx_reg_read(wm, AC97_EXTENDED_MID) | 0x8000;
733                 wm97xx_reg_write(wm, AC97_EXTENDED_MID, reg);
734         }
735
736         return 0;
737 }
738
739 static int wm97xx_resume(struct device *dev)
740 {
741         struct wm97xx *wm = dev_get_drvdata(dev);
742
743         /* restore digitiser and gpios */
744         if (wm->id == WM9713_ID2) {
745                 wm97xx_reg_write(wm, AC97_WM9713_DIG1, wm->dig[0]);
746                 wm97xx_reg_write(wm, 0x5a, wm->misc);
747                 if (wm->input_dev->users) {
748                         u16 reg;
749                         reg = wm97xx_reg_read(wm, AC97_EXTENDED_MID) & 0x7fff;
750                         wm97xx_reg_write(wm, AC97_EXTENDED_MID, reg);
751                 }
752         }
753
754         wm97xx_reg_write(wm, AC97_WM9713_DIG2, wm->dig[1]);
755         wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig[2]);
756
757         wm97xx_reg_write(wm, AC97_GPIO_CFG, wm->gpio[0]);
758         wm97xx_reg_write(wm, AC97_GPIO_POLARITY, wm->gpio[1]);
759         wm97xx_reg_write(wm, AC97_GPIO_STICKY, wm->gpio[2]);
760         wm97xx_reg_write(wm, AC97_GPIO_WAKEUP, wm->gpio[3]);
761         wm97xx_reg_write(wm, AC97_GPIO_STATUS, wm->gpio[4]);
762         wm97xx_reg_write(wm, AC97_MISC_AFE, wm->gpio[5]);
763
764         if (wm->input_dev->users && !wm->pen_irq) {
765                 wm->ts_reader_interval = wm->ts_reader_min_interval;
766                 queue_delayed_work(wm->ts_workq, &wm->ts_reader,
767                                    wm->ts_reader_interval);
768         }
769
770         return 0;
771 }
772
773 #else
774 #define wm97xx_suspend          NULL
775 #define wm97xx_resume           NULL
776 #endif
777
778 /*
779  * Machine specific operations
780  */
781 int wm97xx_register_mach_ops(struct wm97xx *wm,
782                              struct wm97xx_mach_ops *mach_ops)
783 {
784         mutex_lock(&wm->codec_mutex);
785         if (wm->mach_ops) {
786                 mutex_unlock(&wm->codec_mutex);
787                 return -EINVAL;
788         }
789         wm->mach_ops = mach_ops;
790         mutex_unlock(&wm->codec_mutex);
791
792         return 0;
793 }
794 EXPORT_SYMBOL_GPL(wm97xx_register_mach_ops);
795
796 void wm97xx_unregister_mach_ops(struct wm97xx *wm)
797 {
798         mutex_lock(&wm->codec_mutex);
799         wm->mach_ops = NULL;
800         mutex_unlock(&wm->codec_mutex);
801 }
802 EXPORT_SYMBOL_GPL(wm97xx_unregister_mach_ops);
803
804 static struct device_driver wm97xx_driver = {
805         .name =         "wm97xx-ts",
806         .bus =          &ac97_bus_type,
807         .owner =        THIS_MODULE,
808         .probe =        wm97xx_probe,
809         .remove =       wm97xx_remove,
810         .suspend =      wm97xx_suspend,
811         .resume =       wm97xx_resume,
812 };
813
814 static int __init wm97xx_init(void)
815 {
816         return driver_register(&wm97xx_driver);
817 }
818
819 static void __exit wm97xx_exit(void)
820 {
821         driver_unregister(&wm97xx_driver);
822 }
823
824 module_init(wm97xx_init);
825 module_exit(wm97xx_exit);
826
827 /* Module information */
828 MODULE_AUTHOR("Liam Girdwood <liam.girdwood@wolfsonmicro.com>");
829 MODULE_DESCRIPTION("WM97xx Core - Touch Screen / AUX ADC / GPIO Driver");
830 MODULE_LICENSE("GPL");