Input: wm97xx - add BTN_TOUCH event to wm97xx to use it with Android
[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 <lrg@slimlogic.co.uk>
7  * Parts Copyright : Ian Molton <spyro@f2s.com>
8  *                   Andrew Zabolotny <zap@homelink.ru>
9  *                   Russell King <rmk@arm.linux.org.uk>
10  *
11  *  This program is free software; you can redistribute  it and/or modify it
12  *  under  the terms of  the GNU General  Public License as published by the
13  *  Free Software Foundation;  either version 2 of the  License, or (at your
14  *  option) any later version.
15  *
16  * Notes:
17  *
18  *  Features:
19  *       - supports WM9705, WM9712, WM9713
20  *       - polling mode
21  *       - continuous mode (arch-dependent)
22  *       - adjustable rpu/dpp settings
23  *       - adjustable pressure current
24  *       - adjustable sample settle delay
25  *       - 4 and 5 wire touchscreens (5 wire is WM9712 only)
26  *       - pen down detection
27  *       - battery monitor
28  *       - sample AUX adcs
29  *       - power management
30  *       - codec GPIO
31  *       - codec event notification
32  * Todo
33  *       - Support for async sampling control for noisy LCDs.
34  *
35  */
36
37 #include <linux/module.h>
38 #include <linux/moduleparam.h>
39 #include <linux/kernel.h>
40 #include <linux/init.h>
41 #include <linux/delay.h>
42 #include <linux/string.h>
43 #include <linux/proc_fs.h>
44 #include <linux/pm.h>
45 #include <linux/interrupt.h>
46 #include <linux/bitops.h>
47 #include <linux/workqueue.h>
48 #include <linux/wm97xx.h>
49 #include <linux/uaccess.h>
50 #include <linux/io.h>
51
52 #define TS_NAME                 "wm97xx"
53 #define WM_CORE_VERSION         "1.00"
54 #define DEFAULT_PRESSURE        0xb0c0
55
56
57 /*
58  * Touchscreen absolute values
59  *
60  * These parameters are used to help the input layer discard out of
61  * range readings and reduce jitter etc.
62  *
63  *   o min, max:- indicate the min and max values your touch screen returns
64  *   o fuzz:- use a higher number to reduce jitter
65  *
66  * The default values correspond to Mainstone II in QVGA mode
67  *
68  * Please read
69  * Documentation/input/input-programming.txt for more details.
70  */
71
72 static int abs_x[3] = {350, 3900, 5};
73 module_param_array(abs_x, int, NULL, 0);
74 MODULE_PARM_DESC(abs_x, "Touchscreen absolute X min, max, fuzz");
75
76 static int abs_y[3] = {320, 3750, 40};
77 module_param_array(abs_y, int, NULL, 0);
78 MODULE_PARM_DESC(abs_y, "Touchscreen absolute Y min, max, fuzz");
79
80 static int abs_p[3] = {0, 150, 4};
81 module_param_array(abs_p, int, NULL, 0);
82 MODULE_PARM_DESC(abs_p, "Touchscreen absolute Pressure min, max, fuzz");
83
84 /*
85  * wm97xx IO access, all IO locking done by AC97 layer
86  */
87 int wm97xx_reg_read(struct wm97xx *wm, u16 reg)
88 {
89         if (wm->ac97)
90                 return wm->ac97->bus->ops->read(wm->ac97, reg);
91         else
92                 return -1;
93 }
94 EXPORT_SYMBOL_GPL(wm97xx_reg_read);
95
96 void wm97xx_reg_write(struct wm97xx *wm, u16 reg, u16 val)
97 {
98         /* cache digitiser registers */
99         if (reg >= AC97_WM9713_DIG1 && reg <= AC97_WM9713_DIG3)
100                 wm->dig[(reg - AC97_WM9713_DIG1) >> 1] = val;
101
102         /* cache gpio regs */
103         if (reg >= AC97_GPIO_CFG && reg <= AC97_MISC_AFE)
104                 wm->gpio[(reg - AC97_GPIO_CFG) >> 1] = val;
105
106         /* wm9713 irq reg */
107         if (reg == 0x5a)
108                 wm->misc = val;
109
110         if (wm->ac97)
111                 wm->ac97->bus->ops->write(wm->ac97, reg, val);
112 }
113 EXPORT_SYMBOL_GPL(wm97xx_reg_write);
114
115 /**
116  * wm97xx_read_aux_adc - Read the aux adc.
117  * @wm: wm97xx device.
118  * @adcsel: codec ADC to be read
119  *
120  * Reads the selected AUX ADC.
121  */
122
123 int wm97xx_read_aux_adc(struct wm97xx *wm, u16 adcsel)
124 {
125         int power_adc = 0, auxval;
126         u16 power = 0;
127
128         /* get codec */
129         mutex_lock(&wm->codec_mutex);
130
131         /* When the touchscreen is not in use, we may have to power up
132          * the AUX ADC before we can use sample the AUX inputs->
133          */
134         if (wm->id == WM9713_ID2 &&
135             (power = wm97xx_reg_read(wm, AC97_EXTENDED_MID)) & 0x8000) {
136                 power_adc = 1;
137                 wm97xx_reg_write(wm, AC97_EXTENDED_MID, power & 0x7fff);
138         }
139
140         /* Prepare the codec for AUX reading */
141         wm->codec->aux_prepare(wm);
142
143         /* Turn polling mode on to read AUX ADC */
144         wm->pen_probably_down = 1;
145         wm->codec->poll_sample(wm, adcsel, &auxval);
146
147         if (power_adc)
148                 wm97xx_reg_write(wm, AC97_EXTENDED_MID, power | 0x8000);
149
150         wm->codec->dig_restore(wm);
151
152         wm->pen_probably_down = 0;
153
154         mutex_unlock(&wm->codec_mutex);
155         return auxval & 0xfff;
156 }
157 EXPORT_SYMBOL_GPL(wm97xx_read_aux_adc);
158
159 /**
160  * wm97xx_get_gpio - Get the status of a codec GPIO.
161  * @wm: wm97xx device.
162  * @gpio: gpio
163  *
164  * Get the status of a codec GPIO pin
165  */
166
167 enum wm97xx_gpio_status wm97xx_get_gpio(struct wm97xx *wm, u32 gpio)
168 {
169         u16 status;
170         enum wm97xx_gpio_status ret;
171
172         mutex_lock(&wm->codec_mutex);
173         status = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
174
175         if (status & gpio)
176                 ret = WM97XX_GPIO_HIGH;
177         else
178                 ret = WM97XX_GPIO_LOW;
179
180         mutex_unlock(&wm->codec_mutex);
181         return ret;
182 }
183 EXPORT_SYMBOL_GPL(wm97xx_get_gpio);
184
185 /**
186  * wm97xx_set_gpio - Set the status of a codec GPIO.
187  * @wm: wm97xx device.
188  * @gpio: gpio
189  *
190  *
191  * Set the status of a codec GPIO pin
192  */
193
194 void wm97xx_set_gpio(struct wm97xx *wm, u32 gpio,
195                                 enum wm97xx_gpio_status status)
196 {
197         u16 reg;
198
199         mutex_lock(&wm->codec_mutex);
200         reg = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
201
202         if (status & WM97XX_GPIO_HIGH)
203                 reg |= gpio;
204         else
205                 reg &= ~gpio;
206
207         if (wm->id == WM9712_ID2)
208                 wm97xx_reg_write(wm, AC97_GPIO_STATUS, reg << 1);
209         else
210                 wm97xx_reg_write(wm, AC97_GPIO_STATUS, reg);
211         mutex_unlock(&wm->codec_mutex);
212 }
213 EXPORT_SYMBOL_GPL(wm97xx_set_gpio);
214
215 /*
216  * Codec GPIO pin configuration, this sets pin direction, polarity,
217  * stickyness and wake up.
218  */
219 void wm97xx_config_gpio(struct wm97xx *wm, u32 gpio, enum wm97xx_gpio_dir dir,
220                    enum wm97xx_gpio_pol pol, enum wm97xx_gpio_sticky sticky,
221                    enum wm97xx_gpio_wake wake)
222 {
223         u16 reg;
224
225         mutex_lock(&wm->codec_mutex);
226         reg = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
227
228         if (pol == WM97XX_GPIO_POL_HIGH)
229                 reg |= gpio;
230         else
231                 reg &= ~gpio;
232
233         wm97xx_reg_write(wm, AC97_GPIO_POLARITY, reg);
234         reg = wm97xx_reg_read(wm, AC97_GPIO_STICKY);
235
236         if (sticky == WM97XX_GPIO_STICKY)
237                 reg |= gpio;
238         else
239                 reg &= ~gpio;
240
241         wm97xx_reg_write(wm, AC97_GPIO_STICKY, reg);
242         reg = wm97xx_reg_read(wm, AC97_GPIO_WAKEUP);
243
244         if (wake == WM97XX_GPIO_WAKE)
245                 reg |= gpio;
246         else
247                 reg &= ~gpio;
248
249         wm97xx_reg_write(wm, AC97_GPIO_WAKEUP, reg);
250         reg = wm97xx_reg_read(wm, AC97_GPIO_CFG);
251
252         if (dir == WM97XX_GPIO_IN)
253                 reg |= gpio;
254         else
255                 reg &= ~gpio;
256
257         wm97xx_reg_write(wm, AC97_GPIO_CFG, reg);
258         mutex_unlock(&wm->codec_mutex);
259 }
260 EXPORT_SYMBOL_GPL(wm97xx_config_gpio);
261
262 /*
263  * Configure the WM97XX_PRP value to use while system is suspended.
264  * If a value other than 0 is set then WM97xx pen detection will be
265  * left enabled in the configured mode while the system is in suspend,
266  * the device has users and suspend has not been disabled via the
267  * wakeup sysfs entries.
268  *
269  * @wm:   WM97xx device to configure
270  * @mode: WM97XX_PRP value to configure while suspended
271  */
272 void wm97xx_set_suspend_mode(struct wm97xx *wm, u16 mode)
273 {
274         wm->suspend_mode = mode;
275         device_init_wakeup(&wm->input_dev->dev, mode != 0);
276 }
277 EXPORT_SYMBOL_GPL(wm97xx_set_suspend_mode);
278
279 /*
280  * Handle a pen down interrupt.
281  */
282 static void wm97xx_pen_irq_worker(struct work_struct *work)
283 {
284         struct wm97xx *wm = container_of(work, struct wm97xx, pen_event_work);
285         int pen_was_down = wm->pen_is_down;
286
287         /* do we need to enable the touch panel reader */
288         if (wm->id == WM9705_ID2) {
289                 if (wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD) &
290                                         WM97XX_PEN_DOWN)
291                         wm->pen_is_down = 1;
292                 else
293                         wm->pen_is_down = 0;
294         } else {
295                 u16 status, pol;
296                 mutex_lock(&wm->codec_mutex);
297                 status = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
298                 pol = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
299
300                 if (WM97XX_GPIO_13 & pol & status) {
301                         wm->pen_is_down = 1;
302                         wm97xx_reg_write(wm, AC97_GPIO_POLARITY, pol &
303                                                 ~WM97XX_GPIO_13);
304                 } else {
305                         wm->pen_is_down = 0;
306                         wm97xx_reg_write(wm, AC97_GPIO_POLARITY, pol |
307                                          WM97XX_GPIO_13);
308                 }
309
310                 if (wm->id == WM9712_ID2)
311                         wm97xx_reg_write(wm, AC97_GPIO_STATUS, (status &
312                                                 ~WM97XX_GPIO_13) << 1);
313                 else
314                         wm97xx_reg_write(wm, AC97_GPIO_STATUS, status &
315                                                 ~WM97XX_GPIO_13);
316                 mutex_unlock(&wm->codec_mutex);
317         }
318
319         /* If the system is not using continuous mode or it provides a
320          * pen down operation then we need to schedule polls while the
321          * pen is down.  Otherwise the machine driver is responsible
322          * for scheduling reads.
323          */
324         if (!wm->mach_ops->acc_enabled || wm->mach_ops->acc_pen_down) {
325                 if (wm->pen_is_down && !pen_was_down) {
326                         /* Data is not availiable immediately on pen down */
327                         queue_delayed_work(wm->ts_workq, &wm->ts_reader, 1);
328                 }
329
330                 /* Let ts_reader report the pen up for debounce. */
331                 if (!wm->pen_is_down && pen_was_down)
332                         wm->pen_is_down = 1;
333         }
334
335         if (!wm->pen_is_down && wm->mach_ops->acc_enabled)
336                 wm->mach_ops->acc_pen_up(wm);
337
338         wm->mach_ops->irq_enable(wm, 1);
339 }
340
341 /*
342  * Codec PENDOWN irq handler
343  *
344  * We have to disable the codec interrupt in the handler because it
345  * can take upto 1ms to clear the interrupt source. We schedule a task
346  * in a work queue to do the actual interaction with the chip.  The
347  * interrupt is then enabled again in the slow handler when the source
348  * has been cleared.
349  */
350 static irqreturn_t wm97xx_pen_interrupt(int irq, void *dev_id)
351 {
352         struct wm97xx *wm = dev_id;
353
354         if (!work_pending(&wm->pen_event_work)) {
355                 wm->mach_ops->irq_enable(wm, 0);
356                 queue_work(wm->ts_workq, &wm->pen_event_work);
357         }
358
359         return IRQ_HANDLED;
360 }
361
362 /*
363  * initialise pen IRQ handler and workqueue
364  */
365 static int wm97xx_init_pen_irq(struct wm97xx *wm)
366 {
367         u16 reg;
368
369         /* If an interrupt is supplied an IRQ enable operation must also be
370          * provided. */
371         BUG_ON(!wm->mach_ops->irq_enable);
372
373         if (request_irq(wm->pen_irq, wm97xx_pen_interrupt,
374                         IRQF_SHARED | IRQF_SAMPLE_RANDOM,
375                         "wm97xx-pen", wm)) {
376                 dev_err(wm->dev,
377                         "Failed to register pen down interrupt, polling");
378                 wm->pen_irq = 0;
379                 return -EINVAL;
380         }
381
382         /* Configure GPIO as interrupt source on WM971x */
383         if (wm->id != WM9705_ID2) {
384                 BUG_ON(!wm->mach_ops->irq_gpio);
385                 reg = wm97xx_reg_read(wm, AC97_MISC_AFE);
386                 wm97xx_reg_write(wm, AC97_MISC_AFE,
387                                 reg & ~(wm->mach_ops->irq_gpio));
388                 reg = wm97xx_reg_read(wm, 0x5a);
389                 wm97xx_reg_write(wm, 0x5a, reg & ~0x0001);
390         }
391
392         return 0;
393 }
394
395 static int wm97xx_read_samples(struct wm97xx *wm)
396 {
397         struct wm97xx_data data;
398         int rc;
399
400         mutex_lock(&wm->codec_mutex);
401
402         if (wm->mach_ops && wm->mach_ops->acc_enabled)
403                 rc = wm->mach_ops->acc_pen_down(wm);
404         else
405                 rc = wm->codec->poll_touch(wm, &data);
406
407         if (rc & RC_PENUP) {
408                 if (wm->pen_is_down) {
409                         wm->pen_is_down = 0;
410                         dev_dbg(wm->dev, "pen up\n");
411                         input_report_abs(wm->input_dev, ABS_PRESSURE, 0);
412                         input_report_key(wm->input_dev, BTN_TOUCH, 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_report_key(wm->input_dev, BTN_TOUCH, 1);
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
634         __set_bit(EV_ABS, wm->input_dev->evbit);
635         __set_bit(EV_KEY, wm->input_dev->evbit);
636         __set_bit(BTN_TOUCH, wm->input_dev->keybit);
637
638         input_set_abs_params(wm->input_dev, ABS_X, abs_x[0], abs_x[1],
639                              abs_x[2], 0);
640         input_set_abs_params(wm->input_dev, ABS_Y, abs_y[0], abs_y[1],
641                              abs_y[2], 0);
642         input_set_abs_params(wm->input_dev, ABS_PRESSURE, abs_p[0], abs_p[1],
643                              abs_p[2], 0);
644
645         input_set_drvdata(wm->input_dev, wm);
646         wm->input_dev->dev.parent = dev;
647
648         ret = input_register_device(wm->input_dev);
649         if (ret < 0)
650                 goto dev_alloc_err;
651
652         /* register our battery device */
653         wm->battery_dev = platform_device_alloc("wm97xx-battery", -1);
654         if (!wm->battery_dev) {
655                 ret = -ENOMEM;
656                 goto batt_err;
657         }
658         platform_set_drvdata(wm->battery_dev, wm);
659         wm->battery_dev->dev.parent = dev;
660         ret = platform_device_add(wm->battery_dev);
661         if (ret < 0)
662                 goto batt_reg_err;
663
664         /* register our extended touch device (for machine specific
665          * extensions) */
666         wm->touch_dev = platform_device_alloc("wm97xx-touch", -1);
667         if (!wm->touch_dev) {
668                 ret = -ENOMEM;
669                 goto touch_err;
670         }
671         platform_set_drvdata(wm->touch_dev, wm);
672         wm->touch_dev->dev.parent = dev;
673         ret = platform_device_add(wm->touch_dev);
674         if (ret < 0)
675                 goto touch_reg_err;
676
677         return ret;
678
679  touch_reg_err:
680         platform_device_put(wm->touch_dev);
681  touch_err:
682         platform_device_unregister(wm->battery_dev);
683         wm->battery_dev = NULL;
684  batt_reg_err:
685         platform_device_put(wm->battery_dev);
686  batt_err:
687         input_unregister_device(wm->input_dev);
688         wm->input_dev = NULL;
689  dev_alloc_err:
690         input_free_device(wm->input_dev);
691  alloc_err:
692         kfree(wm);
693
694         return ret;
695 }
696
697 static int wm97xx_remove(struct device *dev)
698 {
699         struct wm97xx *wm = dev_get_drvdata(dev);
700
701         platform_device_unregister(wm->battery_dev);
702         platform_device_unregister(wm->touch_dev);
703         input_unregister_device(wm->input_dev);
704         kfree(wm);
705
706         return 0;
707 }
708
709 #ifdef CONFIG_PM
710 static int wm97xx_suspend(struct device *dev, pm_message_t state)
711 {
712         struct wm97xx *wm = dev_get_drvdata(dev);
713         u16 reg;
714         int suspend_mode;
715
716         if (device_may_wakeup(&wm->input_dev->dev))
717                 suspend_mode = wm->suspend_mode;
718         else
719                 suspend_mode = 0;
720
721         if (wm->input_dev->users)
722                 cancel_delayed_work_sync(&wm->ts_reader);
723
724         /* Power down the digitiser (bypassing the cache for resume) */
725         reg = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER2);
726         reg &= ~WM97XX_PRP_DET_DIG;
727         if (wm->input_dev->users)
728                 reg |= suspend_mode;
729         wm->ac97->bus->ops->write(wm->ac97, AC97_WM97XX_DIGITISER2, reg);
730
731         /* WM9713 has an additional power bit - turn it off if there
732          * are no users or if suspend mode is zero. */
733         if (wm->id == WM9713_ID2 &&
734             (!wm->input_dev->users || !suspend_mode)) {
735                 reg = wm97xx_reg_read(wm, AC97_EXTENDED_MID) | 0x8000;
736                 wm97xx_reg_write(wm, AC97_EXTENDED_MID, reg);
737         }
738
739         return 0;
740 }
741
742 static int wm97xx_resume(struct device *dev)
743 {
744         struct wm97xx *wm = dev_get_drvdata(dev);
745
746         /* restore digitiser and gpios */
747         if (wm->id == WM9713_ID2) {
748                 wm97xx_reg_write(wm, AC97_WM9713_DIG1, wm->dig[0]);
749                 wm97xx_reg_write(wm, 0x5a, wm->misc);
750                 if (wm->input_dev->users) {
751                         u16 reg;
752                         reg = wm97xx_reg_read(wm, AC97_EXTENDED_MID) & 0x7fff;
753                         wm97xx_reg_write(wm, AC97_EXTENDED_MID, reg);
754                 }
755         }
756
757         wm97xx_reg_write(wm, AC97_WM9713_DIG2, wm->dig[1]);
758         wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig[2]);
759
760         wm97xx_reg_write(wm, AC97_GPIO_CFG, wm->gpio[0]);
761         wm97xx_reg_write(wm, AC97_GPIO_POLARITY, wm->gpio[1]);
762         wm97xx_reg_write(wm, AC97_GPIO_STICKY, wm->gpio[2]);
763         wm97xx_reg_write(wm, AC97_GPIO_WAKEUP, wm->gpio[3]);
764         wm97xx_reg_write(wm, AC97_GPIO_STATUS, wm->gpio[4]);
765         wm97xx_reg_write(wm, AC97_MISC_AFE, wm->gpio[5]);
766
767         if (wm->input_dev->users && !wm->pen_irq) {
768                 wm->ts_reader_interval = wm->ts_reader_min_interval;
769                 queue_delayed_work(wm->ts_workq, &wm->ts_reader,
770                                    wm->ts_reader_interval);
771         }
772
773         return 0;
774 }
775
776 #else
777 #define wm97xx_suspend          NULL
778 #define wm97xx_resume           NULL
779 #endif
780
781 /*
782  * Machine specific operations
783  */
784 int wm97xx_register_mach_ops(struct wm97xx *wm,
785                              struct wm97xx_mach_ops *mach_ops)
786 {
787         mutex_lock(&wm->codec_mutex);
788         if (wm->mach_ops) {
789                 mutex_unlock(&wm->codec_mutex);
790                 return -EINVAL;
791         }
792         wm->mach_ops = mach_ops;
793         mutex_unlock(&wm->codec_mutex);
794
795         return 0;
796 }
797 EXPORT_SYMBOL_GPL(wm97xx_register_mach_ops);
798
799 void wm97xx_unregister_mach_ops(struct wm97xx *wm)
800 {
801         mutex_lock(&wm->codec_mutex);
802         wm->mach_ops = NULL;
803         mutex_unlock(&wm->codec_mutex);
804 }
805 EXPORT_SYMBOL_GPL(wm97xx_unregister_mach_ops);
806
807 static struct device_driver wm97xx_driver = {
808         .name =         "wm97xx-ts",
809         .bus =          &ac97_bus_type,
810         .owner =        THIS_MODULE,
811         .probe =        wm97xx_probe,
812         .remove =       wm97xx_remove,
813         .suspend =      wm97xx_suspend,
814         .resume =       wm97xx_resume,
815 };
816
817 static int __init wm97xx_init(void)
818 {
819         return driver_register(&wm97xx_driver);
820 }
821
822 static void __exit wm97xx_exit(void)
823 {
824         driver_unregister(&wm97xx_driver);
825 }
826
827 module_init(wm97xx_init);
828 module_exit(wm97xx_exit);
829
830 /* Module information */
831 MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
832 MODULE_DESCRIPTION("WM97xx Core - Touch Screen / AUX ADC / GPIO Driver");
833 MODULE_LICENSE("GPL");