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