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/mfd/wm97xx.h>
48 #include <linux/workqueue.h>
49 #include <linux/wm97xx.h>
50 #include <linux/uaccess.h>
51 #include <linux/io.h>
52 #include <linux/slab.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] = {150, 4000, 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] = {200, 4000, 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 	int rc = 0;
130 	int timeout = 0;
131 
132 	/* get codec */
133 	mutex_lock(&wm->codec_mutex);
134 
135 	/* When the touchscreen is not in use, we may have to power up
136 	 * the AUX ADC before we can use sample the AUX inputs->
137 	 */
138 	if (wm->id == WM9713_ID2 &&
139 	    (power = wm97xx_reg_read(wm, AC97_EXTENDED_MID)) & 0x8000) {
140 		power_adc = 1;
141 		wm97xx_reg_write(wm, AC97_EXTENDED_MID, power & 0x7fff);
142 	}
143 
144 	/* Prepare the codec for AUX reading */
145 	wm->codec->aux_prepare(wm);
146 
147 	/* Turn polling mode on to read AUX ADC */
148 	wm->pen_probably_down = 1;
149 
150 	while (rc != RC_VALID && timeout++ < 5)
151 		rc = wm->codec->poll_sample(wm, adcsel, &auxval);
152 
153 	if (power_adc)
154 		wm97xx_reg_write(wm, AC97_EXTENDED_MID, power | 0x8000);
155 
156 	wm->codec->dig_restore(wm);
157 
158 	wm->pen_probably_down = 0;
159 
160 	if (timeout >= 5) {
161 		dev_err(wm->dev,
162 			"timeout reading auxadc %d, disabling digitiser\n",
163 			adcsel);
164 		wm->codec->dig_enable(wm, false);
165 	}
166 
167 	mutex_unlock(&wm->codec_mutex);
168 	return (rc == RC_VALID ? auxval & 0xfff : -EBUSY);
169 }
170 EXPORT_SYMBOL_GPL(wm97xx_read_aux_adc);
171 
172 /**
173  * wm97xx_get_gpio - Get the status of a codec GPIO.
174  * @wm: wm97xx device.
175  * @gpio: gpio
176  *
177  * Get the status of a codec GPIO pin
178  */
179 
180 enum wm97xx_gpio_status wm97xx_get_gpio(struct wm97xx *wm, u32 gpio)
181 {
182 	u16 status;
183 	enum wm97xx_gpio_status ret;
184 
185 	mutex_lock(&wm->codec_mutex);
186 	status = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
187 
188 	if (status & gpio)
189 		ret = WM97XX_GPIO_HIGH;
190 	else
191 		ret = WM97XX_GPIO_LOW;
192 
193 	mutex_unlock(&wm->codec_mutex);
194 	return ret;
195 }
196 EXPORT_SYMBOL_GPL(wm97xx_get_gpio);
197 
198 /**
199  * wm97xx_set_gpio - Set the status of a codec GPIO.
200  * @wm: wm97xx device.
201  * @gpio: gpio
202  *
203  *
204  * Set the status of a codec GPIO pin
205  */
206 
207 void wm97xx_set_gpio(struct wm97xx *wm, u32 gpio,
208 				enum wm97xx_gpio_status status)
209 {
210 	u16 reg;
211 
212 	mutex_lock(&wm->codec_mutex);
213 	reg = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
214 
215 	if (status == WM97XX_GPIO_HIGH)
216 		reg |= gpio;
217 	else
218 		reg &= ~gpio;
219 
220 	if (wm->id == WM9712_ID2 && wm->variant != WM97xx_WM1613)
221 		wm97xx_reg_write(wm, AC97_GPIO_STATUS, reg << 1);
222 	else
223 		wm97xx_reg_write(wm, AC97_GPIO_STATUS, reg);
224 	mutex_unlock(&wm->codec_mutex);
225 }
226 EXPORT_SYMBOL_GPL(wm97xx_set_gpio);
227 
228 /*
229  * Codec GPIO pin configuration, this sets pin direction, polarity,
230  * stickyness and wake up.
231  */
232 void wm97xx_config_gpio(struct wm97xx *wm, u32 gpio, enum wm97xx_gpio_dir dir,
233 		   enum wm97xx_gpio_pol pol, enum wm97xx_gpio_sticky sticky,
234 		   enum wm97xx_gpio_wake wake)
235 {
236 	u16 reg;
237 
238 	mutex_lock(&wm->codec_mutex);
239 	reg = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
240 
241 	if (pol == WM97XX_GPIO_POL_HIGH)
242 		reg |= gpio;
243 	else
244 		reg &= ~gpio;
245 
246 	wm97xx_reg_write(wm, AC97_GPIO_POLARITY, reg);
247 	reg = wm97xx_reg_read(wm, AC97_GPIO_STICKY);
248 
249 	if (sticky == WM97XX_GPIO_STICKY)
250 		reg |= gpio;
251 	else
252 		reg &= ~gpio;
253 
254 	wm97xx_reg_write(wm, AC97_GPIO_STICKY, reg);
255 	reg = wm97xx_reg_read(wm, AC97_GPIO_WAKEUP);
256 
257 	if (wake == WM97XX_GPIO_WAKE)
258 		reg |= gpio;
259 	else
260 		reg &= ~gpio;
261 
262 	wm97xx_reg_write(wm, AC97_GPIO_WAKEUP, reg);
263 	reg = wm97xx_reg_read(wm, AC97_GPIO_CFG);
264 
265 	if (dir == WM97XX_GPIO_IN)
266 		reg |= gpio;
267 	else
268 		reg &= ~gpio;
269 
270 	wm97xx_reg_write(wm, AC97_GPIO_CFG, reg);
271 	mutex_unlock(&wm->codec_mutex);
272 }
273 EXPORT_SYMBOL_GPL(wm97xx_config_gpio);
274 
275 /*
276  * Configure the WM97XX_PRP value to use while system is suspended.
277  * If a value other than 0 is set then WM97xx pen detection will be
278  * left enabled in the configured mode while the system is in suspend,
279  * the device has users and suspend has not been disabled via the
280  * wakeup sysfs entries.
281  *
282  * @wm:   WM97xx device to configure
283  * @mode: WM97XX_PRP value to configure while suspended
284  */
285 void wm97xx_set_suspend_mode(struct wm97xx *wm, u16 mode)
286 {
287 	wm->suspend_mode = mode;
288 	device_init_wakeup(&wm->input_dev->dev, mode != 0);
289 }
290 EXPORT_SYMBOL_GPL(wm97xx_set_suspend_mode);
291 
292 /*
293  * Handle a pen down interrupt.
294  */
295 static void wm97xx_pen_irq_worker(struct work_struct *work)
296 {
297 	struct wm97xx *wm = container_of(work, struct wm97xx, pen_event_work);
298 	int pen_was_down = wm->pen_is_down;
299 
300 	/* do we need to enable the touch panel reader */
301 	if (wm->id == WM9705_ID2) {
302 		if (wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER_RD) &
303 					WM97XX_PEN_DOWN)
304 			wm->pen_is_down = 1;
305 		else
306 			wm->pen_is_down = 0;
307 	} else {
308 		u16 status, pol;
309 		mutex_lock(&wm->codec_mutex);
310 		status = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
311 		pol = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
312 
313 		if (WM97XX_GPIO_13 & pol & status) {
314 			wm->pen_is_down = 1;
315 			wm97xx_reg_write(wm, AC97_GPIO_POLARITY, pol &
316 						~WM97XX_GPIO_13);
317 		} else {
318 			wm->pen_is_down = 0;
319 			wm97xx_reg_write(wm, AC97_GPIO_POLARITY, pol |
320 					 WM97XX_GPIO_13);
321 		}
322 
323 		if (wm->id == WM9712_ID2 && wm->variant != WM97xx_WM1613)
324 			wm97xx_reg_write(wm, AC97_GPIO_STATUS, (status &
325 						~WM97XX_GPIO_13) << 1);
326 		else
327 			wm97xx_reg_write(wm, AC97_GPIO_STATUS, status &
328 						~WM97XX_GPIO_13);
329 		mutex_unlock(&wm->codec_mutex);
330 	}
331 
332 	/* If the system is not using continuous mode or it provides a
333 	 * pen down operation then we need to schedule polls while the
334 	 * pen is down.  Otherwise the machine driver is responsible
335 	 * for scheduling reads.
336 	 */
337 	if (!wm->mach_ops->acc_enabled || wm->mach_ops->acc_pen_down) {
338 		if (wm->pen_is_down && !pen_was_down) {
339 			/* Data is not available immediately on pen down */
340 			queue_delayed_work(wm->ts_workq, &wm->ts_reader, 1);
341 		}
342 
343 		/* Let ts_reader report the pen up for debounce. */
344 		if (!wm->pen_is_down && pen_was_down)
345 			wm->pen_is_down = 1;
346 	}
347 
348 	if (!wm->pen_is_down && wm->mach_ops->acc_enabled)
349 		wm->mach_ops->acc_pen_up(wm);
350 
351 	wm->mach_ops->irq_enable(wm, 1);
352 }
353 
354 /*
355  * Codec PENDOWN irq handler
356  *
357  * We have to disable the codec interrupt in the handler because it
358  * can take up to 1ms to clear the interrupt source. We schedule a task
359  * in a work queue to do the actual interaction with the chip.  The
360  * interrupt is then enabled again in the slow handler when the source
361  * has been cleared.
362  */
363 static irqreturn_t wm97xx_pen_interrupt(int irq, void *dev_id)
364 {
365 	struct wm97xx *wm = dev_id;
366 
367 	if (!work_pending(&wm->pen_event_work)) {
368 		wm->mach_ops->irq_enable(wm, 0);
369 		queue_work(wm->ts_workq, &wm->pen_event_work);
370 	}
371 
372 	return IRQ_HANDLED;
373 }
374 
375 /*
376  * initialise pen IRQ handler and workqueue
377  */
378 static int wm97xx_init_pen_irq(struct wm97xx *wm)
379 {
380 	u16 reg;
381 
382 	/* If an interrupt is supplied an IRQ enable operation must also be
383 	 * provided. */
384 	BUG_ON(!wm->mach_ops->irq_enable);
385 
386 	if (request_irq(wm->pen_irq, wm97xx_pen_interrupt, IRQF_SHARED,
387 			"wm97xx-pen", wm)) {
388 		dev_err(wm->dev,
389 			"Failed to register pen down interrupt, polling");
390 		wm->pen_irq = 0;
391 		return -EINVAL;
392 	}
393 
394 	/* Configure GPIO as interrupt source on WM971x */
395 	if (wm->id != WM9705_ID2) {
396 		BUG_ON(!wm->mach_ops->irq_gpio);
397 		reg = wm97xx_reg_read(wm, AC97_MISC_AFE);
398 		wm97xx_reg_write(wm, AC97_MISC_AFE,
399 				reg & ~(wm->mach_ops->irq_gpio));
400 		reg = wm97xx_reg_read(wm, 0x5a);
401 		wm97xx_reg_write(wm, 0x5a, reg & ~0x0001);
402 	}
403 
404 	return 0;
405 }
406 
407 static int wm97xx_read_samples(struct wm97xx *wm)
408 {
409 	struct wm97xx_data data;
410 	int rc;
411 
412 	mutex_lock(&wm->codec_mutex);
413 
414 	if (wm->mach_ops && wm->mach_ops->acc_enabled)
415 		rc = wm->mach_ops->acc_pen_down(wm);
416 	else
417 		rc = wm->codec->poll_touch(wm, &data);
418 
419 	if (rc & RC_PENUP) {
420 		if (wm->pen_is_down) {
421 			wm->pen_is_down = 0;
422 			dev_dbg(wm->dev, "pen up\n");
423 			input_report_abs(wm->input_dev, ABS_PRESSURE, 0);
424 			input_report_key(wm->input_dev, BTN_TOUCH, 0);
425 			input_sync(wm->input_dev);
426 		} else if (!(rc & RC_AGAIN)) {
427 			/* We need high frequency updates only while
428 			* pen is down, the user never will be able to
429 			* touch screen faster than a few times per
430 			* second... On the other hand, when the user
431 			* is actively working with the touchscreen we
432 			* don't want to lose the quick response. So we
433 			* will slowly increase sleep time after the
434 			* pen is up and quicky restore it to ~one task
435 			* switch when pen is down again.
436 			*/
437 			if (wm->ts_reader_interval < HZ / 10)
438 				wm->ts_reader_interval++;
439 		}
440 
441 	} else if (rc & RC_VALID) {
442 		dev_dbg(wm->dev,
443 			"pen down: x=%x:%d, y=%x:%d, pressure=%x:%d\n",
444 			data.x >> 12, data.x & 0xfff, data.y >> 12,
445 			data.y & 0xfff, data.p >> 12, data.p & 0xfff);
446 
447 		if (abs_x[0] > (data.x & 0xfff) ||
448 		    abs_x[1] < (data.x & 0xfff) ||
449 		    abs_y[0] > (data.y & 0xfff) ||
450 		    abs_y[1] < (data.y & 0xfff)) {
451 			dev_dbg(wm->dev, "Measurement out of range, dropping it\n");
452 			rc = RC_AGAIN;
453 			goto out;
454 		}
455 
456 		input_report_abs(wm->input_dev, ABS_X, data.x & 0xfff);
457 		input_report_abs(wm->input_dev, ABS_Y, data.y & 0xfff);
458 		input_report_abs(wm->input_dev, ABS_PRESSURE, data.p & 0xfff);
459 		input_report_key(wm->input_dev, BTN_TOUCH, 1);
460 		input_sync(wm->input_dev);
461 		wm->pen_is_down = 1;
462 		wm->ts_reader_interval = wm->ts_reader_min_interval;
463 	} else if (rc & RC_PENDOWN) {
464 		dev_dbg(wm->dev, "pen down\n");
465 		wm->pen_is_down = 1;
466 		wm->ts_reader_interval = wm->ts_reader_min_interval;
467 	}
468 
469 out:
470 	mutex_unlock(&wm->codec_mutex);
471 	return rc;
472 }
473 
474 /*
475 * The touchscreen sample reader.
476 */
477 static void wm97xx_ts_reader(struct work_struct *work)
478 {
479 	int rc;
480 	struct wm97xx *wm = container_of(work, struct wm97xx, ts_reader.work);
481 
482 	BUG_ON(!wm->codec);
483 
484 	do {
485 		rc = wm97xx_read_samples(wm);
486 	} while (rc & RC_AGAIN);
487 
488 	if (wm->pen_is_down || !wm->pen_irq)
489 		queue_delayed_work(wm->ts_workq, &wm->ts_reader,
490 				   wm->ts_reader_interval);
491 }
492 
493 /**
494  * wm97xx_ts_input_open - Open the touch screen input device.
495  * @idev:	Input device to be opened.
496  *
497  * Called by the input sub system to open a wm97xx touchscreen device.
498  * Starts the touchscreen thread and touch digitiser.
499  */
500 static int wm97xx_ts_input_open(struct input_dev *idev)
501 {
502 	struct wm97xx *wm = input_get_drvdata(idev);
503 
504 	wm->ts_workq = alloc_ordered_workqueue("kwm97xx", 0);
505 	if (wm->ts_workq == NULL) {
506 		dev_err(wm->dev,
507 			"Failed to create workqueue\n");
508 		return -EINVAL;
509 	}
510 
511 	/* start digitiser */
512 	if (wm->mach_ops && wm->mach_ops->acc_enabled)
513 		wm->codec->acc_enable(wm, 1);
514 	wm->codec->dig_enable(wm, 1);
515 
516 	INIT_DELAYED_WORK(&wm->ts_reader, wm97xx_ts_reader);
517 	INIT_WORK(&wm->pen_event_work, wm97xx_pen_irq_worker);
518 
519 	wm->ts_reader_min_interval = HZ >= 100 ? HZ / 100 : 1;
520 	if (wm->ts_reader_min_interval < 1)
521 		wm->ts_reader_min_interval = 1;
522 	wm->ts_reader_interval = wm->ts_reader_min_interval;
523 
524 	wm->pen_is_down = 0;
525 	if (wm->pen_irq)
526 		wm97xx_init_pen_irq(wm);
527 	else
528 		dev_err(wm->dev, "No IRQ specified\n");
529 
530 	/* If we either don't have an interrupt for pen down events or
531 	 * failed to acquire it then we need to poll.
532 	 */
533 	if (wm->pen_irq == 0)
534 		queue_delayed_work(wm->ts_workq, &wm->ts_reader,
535 				   wm->ts_reader_interval);
536 
537 	return 0;
538 }
539 
540 /**
541  * wm97xx_ts_input_close - Close the touch screen input device.
542  * @idev:	Input device to be closed.
543  *
544  * Called by the input sub system to close a wm97xx touchscreen
545  * device.  Kills the touchscreen thread and stops the touch
546  * digitiser.
547  */
548 
549 static void wm97xx_ts_input_close(struct input_dev *idev)
550 {
551 	struct wm97xx *wm = input_get_drvdata(idev);
552 	u16 reg;
553 
554 	if (wm->pen_irq) {
555 		/* Return the interrupt to GPIO usage (disabling it) */
556 		if (wm->id != WM9705_ID2) {
557 			BUG_ON(!wm->mach_ops->irq_gpio);
558 			reg = wm97xx_reg_read(wm, AC97_MISC_AFE);
559 			wm97xx_reg_write(wm, AC97_MISC_AFE,
560 					 reg | wm->mach_ops->irq_gpio);
561 		}
562 
563 		free_irq(wm->pen_irq, wm);
564 	}
565 
566 	wm->pen_is_down = 0;
567 
568 	/* Balance out interrupt disables/enables */
569 	if (cancel_work_sync(&wm->pen_event_work))
570 		wm->mach_ops->irq_enable(wm, 1);
571 
572 	/* ts_reader rearms itself so we need to explicitly stop it
573 	 * before we destroy the workqueue.
574 	 */
575 	cancel_delayed_work_sync(&wm->ts_reader);
576 
577 	destroy_workqueue(wm->ts_workq);
578 
579 	/* stop digitiser */
580 	wm->codec->dig_enable(wm, 0);
581 	if (wm->mach_ops && wm->mach_ops->acc_enabled)
582 		wm->codec->acc_enable(wm, 0);
583 }
584 
585 static int wm97xx_register_touch(struct wm97xx *wm)
586 {
587 	struct wm97xx_pdata *pdata = dev_get_platdata(wm->dev);
588 	int ret;
589 
590 	wm->input_dev = devm_input_allocate_device(wm->dev);
591 	if (wm->input_dev == NULL)
592 		return -ENOMEM;
593 
594 	/* set up touch configuration */
595 	wm->input_dev->name = "wm97xx touchscreen";
596 	wm->input_dev->phys = "wm97xx";
597 	wm->input_dev->open = wm97xx_ts_input_open;
598 	wm->input_dev->close = wm97xx_ts_input_close;
599 
600 	__set_bit(EV_ABS, wm->input_dev->evbit);
601 	__set_bit(EV_KEY, wm->input_dev->evbit);
602 	__set_bit(BTN_TOUCH, wm->input_dev->keybit);
603 
604 	input_set_abs_params(wm->input_dev, ABS_X, abs_x[0], abs_x[1],
605 			     abs_x[2], 0);
606 	input_set_abs_params(wm->input_dev, ABS_Y, abs_y[0], abs_y[1],
607 			     abs_y[2], 0);
608 	input_set_abs_params(wm->input_dev, ABS_PRESSURE, abs_p[0], abs_p[1],
609 			     abs_p[2], 0);
610 
611 	input_set_drvdata(wm->input_dev, wm);
612 	wm->input_dev->dev.parent = wm->dev;
613 
614 	ret = input_register_device(wm->input_dev);
615 	if (ret)
616 		return ret;
617 
618 	/*
619 	 * register our extended touch device (for machine specific
620 	 * extensions)
621 	 */
622 	wm->touch_dev = platform_device_alloc("wm97xx-touch", -1);
623 	if (!wm->touch_dev) {
624 		ret = -ENOMEM;
625 		goto touch_err;
626 	}
627 	platform_set_drvdata(wm->touch_dev, wm);
628 	wm->touch_dev->dev.parent = wm->dev;
629 	wm->touch_dev->dev.platform_data = pdata;
630 	ret = platform_device_add(wm->touch_dev);
631 	if (ret < 0)
632 		goto touch_reg_err;
633 
634 	return 0;
635 touch_reg_err:
636 	platform_device_put(wm->touch_dev);
637 touch_err:
638 	input_unregister_device(wm->input_dev);
639 	wm->input_dev = NULL;
640 
641 	return ret;
642 }
643 
644 static void wm97xx_unregister_touch(struct wm97xx *wm)
645 {
646 	platform_device_unregister(wm->touch_dev);
647 	input_unregister_device(wm->input_dev);
648 	wm->input_dev = NULL;
649 }
650 
651 static int _wm97xx_probe(struct wm97xx *wm)
652 {
653 	int id = 0;
654 
655 	mutex_init(&wm->codec_mutex);
656 	dev_set_drvdata(wm->dev, wm);
657 
658 	/* check that we have a supported codec */
659 	id = wm97xx_reg_read(wm, AC97_VENDOR_ID1);
660 	if (id != WM97XX_ID1) {
661 		dev_err(wm->dev,
662 			"Device with vendor %04x is not a wm97xx\n", id);
663 		return -ENODEV;
664 	}
665 
666 	wm->id = wm97xx_reg_read(wm, AC97_VENDOR_ID2);
667 
668 	wm->variant = WM97xx_GENERIC;
669 
670 	dev_info(wm->dev, "detected a wm97%02x codec\n", wm->id & 0xff);
671 
672 	switch (wm->id & 0xff) {
673 #ifdef CONFIG_TOUCHSCREEN_WM9705
674 	case 0x05:
675 		wm->codec = &wm9705_codec;
676 		break;
677 #endif
678 #ifdef CONFIG_TOUCHSCREEN_WM9712
679 	case 0x12:
680 		wm->codec = &wm9712_codec;
681 		break;
682 #endif
683 #ifdef CONFIG_TOUCHSCREEN_WM9713
684 	case 0x13:
685 		wm->codec = &wm9713_codec;
686 		break;
687 #endif
688 	default:
689 		dev_err(wm->dev, "Support for wm97%02x not compiled in.\n",
690 			wm->id & 0xff);
691 		return -ENODEV;
692 	}
693 
694 	/* set up physical characteristics */
695 	wm->codec->phy_init(wm);
696 
697 	/* load gpio cache */
698 	wm->gpio[0] = wm97xx_reg_read(wm, AC97_GPIO_CFG);
699 	wm->gpio[1] = wm97xx_reg_read(wm, AC97_GPIO_POLARITY);
700 	wm->gpio[2] = wm97xx_reg_read(wm, AC97_GPIO_STICKY);
701 	wm->gpio[3] = wm97xx_reg_read(wm, AC97_GPIO_WAKEUP);
702 	wm->gpio[4] = wm97xx_reg_read(wm, AC97_GPIO_STATUS);
703 	wm->gpio[5] = wm97xx_reg_read(wm, AC97_MISC_AFE);
704 
705 	return wm97xx_register_touch(wm);
706 }
707 
708 static void wm97xx_remove_battery(struct wm97xx *wm)
709 {
710 	platform_device_unregister(wm->battery_dev);
711 }
712 
713 static int wm97xx_add_battery(struct wm97xx *wm,
714 			      struct wm97xx_batt_pdata *pdata)
715 {
716 	int ret;
717 
718 	wm->battery_dev = platform_device_alloc("wm97xx-battery", -1);
719 	if (!wm->battery_dev)
720 		return -ENOMEM;
721 
722 	platform_set_drvdata(wm->battery_dev, wm);
723 	wm->battery_dev->dev.parent = wm->dev;
724 	wm->battery_dev->dev.platform_data = pdata;
725 	ret = platform_device_add(wm->battery_dev);
726 	if (ret)
727 		platform_device_put(wm->battery_dev);
728 
729 	return ret;
730 }
731 
732 static int wm97xx_probe(struct device *dev)
733 {
734 	struct wm97xx *wm;
735 	int ret;
736 	struct wm97xx_pdata *pdata = dev_get_platdata(dev);
737 
738 	wm = devm_kzalloc(dev, sizeof(struct wm97xx), GFP_KERNEL);
739 	if (!wm)
740 		return -ENOMEM;
741 
742 	wm->dev = dev;
743 	wm->ac97 = to_ac97_t(dev);
744 
745 	ret =  _wm97xx_probe(wm);
746 	if (ret)
747 		return ret;
748 
749 	ret = wm97xx_add_battery(wm, pdata ? pdata->batt_pdata : NULL);
750 	if (ret < 0)
751 		goto batt_err;
752 
753 	return ret;
754 
755 batt_err:
756 	wm97xx_unregister_touch(wm);
757 	return ret;
758 }
759 
760 static int wm97xx_remove(struct device *dev)
761 {
762 	struct wm97xx *wm = dev_get_drvdata(dev);
763 
764 	wm97xx_remove_battery(wm);
765 	wm97xx_unregister_touch(wm);
766 
767 	return 0;
768 }
769 
770 static int wm97xx_mfd_probe(struct platform_device *pdev)
771 {
772 	struct wm97xx *wm;
773 	struct wm97xx_platform_data *mfd_pdata = dev_get_platdata(&pdev->dev);
774 	int ret;
775 
776 	wm = devm_kzalloc(&pdev->dev, sizeof(struct wm97xx), GFP_KERNEL);
777 	if (!wm)
778 		return -ENOMEM;
779 
780 	wm->dev = &pdev->dev;
781 	wm->ac97 = mfd_pdata->ac97;
782 
783 	ret =  _wm97xx_probe(wm);
784 	if (ret)
785 		return ret;
786 
787 	ret = wm97xx_add_battery(wm, mfd_pdata->batt_pdata);
788 	if (ret < 0)
789 		goto batt_err;
790 
791 	return ret;
792 
793 batt_err:
794 	wm97xx_unregister_touch(wm);
795 	return ret;
796 }
797 
798 static int wm97xx_mfd_remove(struct platform_device *pdev)
799 {
800 	return wm97xx_remove(&pdev->dev);
801 }
802 
803 static int __maybe_unused wm97xx_suspend(struct device *dev)
804 {
805 	struct wm97xx *wm = dev_get_drvdata(dev);
806 	u16 reg;
807 	int suspend_mode;
808 
809 	if (device_may_wakeup(&wm->input_dev->dev))
810 		suspend_mode = wm->suspend_mode;
811 	else
812 		suspend_mode = 0;
813 
814 	if (wm->input_dev->users)
815 		cancel_delayed_work_sync(&wm->ts_reader);
816 
817 	/* Power down the digitiser (bypassing the cache for resume) */
818 	reg = wm97xx_reg_read(wm, AC97_WM97XX_DIGITISER2);
819 	reg &= ~WM97XX_PRP_DET_DIG;
820 	if (wm->input_dev->users)
821 		reg |= suspend_mode;
822 	wm->ac97->bus->ops->write(wm->ac97, AC97_WM97XX_DIGITISER2, reg);
823 
824 	/* WM9713 has an additional power bit - turn it off if there
825 	 * are no users or if suspend mode is zero. */
826 	if (wm->id == WM9713_ID2 &&
827 	    (!wm->input_dev->users || !suspend_mode)) {
828 		reg = wm97xx_reg_read(wm, AC97_EXTENDED_MID) | 0x8000;
829 		wm97xx_reg_write(wm, AC97_EXTENDED_MID, reg);
830 	}
831 
832 	return 0;
833 }
834 
835 static int __maybe_unused wm97xx_resume(struct device *dev)
836 {
837 	struct wm97xx *wm = dev_get_drvdata(dev);
838 
839 	/* restore digitiser and gpios */
840 	if (wm->id == WM9713_ID2) {
841 		wm97xx_reg_write(wm, AC97_WM9713_DIG1, wm->dig[0]);
842 		wm97xx_reg_write(wm, 0x5a, wm->misc);
843 		if (wm->input_dev->users) {
844 			u16 reg;
845 			reg = wm97xx_reg_read(wm, AC97_EXTENDED_MID) & 0x7fff;
846 			wm97xx_reg_write(wm, AC97_EXTENDED_MID, reg);
847 		}
848 	}
849 
850 	wm97xx_reg_write(wm, AC97_WM9713_DIG2, wm->dig[1]);
851 	wm97xx_reg_write(wm, AC97_WM9713_DIG3, wm->dig[2]);
852 
853 	wm97xx_reg_write(wm, AC97_GPIO_CFG, wm->gpio[0]);
854 	wm97xx_reg_write(wm, AC97_GPIO_POLARITY, wm->gpio[1]);
855 	wm97xx_reg_write(wm, AC97_GPIO_STICKY, wm->gpio[2]);
856 	wm97xx_reg_write(wm, AC97_GPIO_WAKEUP, wm->gpio[3]);
857 	wm97xx_reg_write(wm, AC97_GPIO_STATUS, wm->gpio[4]);
858 	wm97xx_reg_write(wm, AC97_MISC_AFE, wm->gpio[5]);
859 
860 	if (wm->input_dev->users && !wm->pen_irq) {
861 		wm->ts_reader_interval = wm->ts_reader_min_interval;
862 		queue_delayed_work(wm->ts_workq, &wm->ts_reader,
863 				   wm->ts_reader_interval);
864 	}
865 
866 	return 0;
867 }
868 
869 static SIMPLE_DEV_PM_OPS(wm97xx_pm_ops, wm97xx_suspend, wm97xx_resume);
870 
871 /*
872  * Machine specific operations
873  */
874 int wm97xx_register_mach_ops(struct wm97xx *wm,
875 			     struct wm97xx_mach_ops *mach_ops)
876 {
877 	mutex_lock(&wm->codec_mutex);
878 	if (wm->mach_ops) {
879 		mutex_unlock(&wm->codec_mutex);
880 		return -EINVAL;
881 	}
882 	wm->mach_ops = mach_ops;
883 	mutex_unlock(&wm->codec_mutex);
884 
885 	return 0;
886 }
887 EXPORT_SYMBOL_GPL(wm97xx_register_mach_ops);
888 
889 void wm97xx_unregister_mach_ops(struct wm97xx *wm)
890 {
891 	mutex_lock(&wm->codec_mutex);
892 	wm->mach_ops = NULL;
893 	mutex_unlock(&wm->codec_mutex);
894 }
895 EXPORT_SYMBOL_GPL(wm97xx_unregister_mach_ops);
896 
897 static struct device_driver wm97xx_driver = {
898 	.name =		"wm97xx-ts",
899 #ifdef CONFIG_AC97_BUS
900 	.bus =		&ac97_bus_type,
901 #endif
902 	.owner =	THIS_MODULE,
903 	.probe =	wm97xx_probe,
904 	.remove =	wm97xx_remove,
905 	.pm =		&wm97xx_pm_ops,
906 };
907 
908 static struct platform_driver wm97xx_mfd_driver = {
909 	.driver = {
910 		.name =		"wm97xx-ts",
911 		.pm =		&wm97xx_pm_ops,
912 	},
913 	.probe =	wm97xx_mfd_probe,
914 	.remove =	wm97xx_mfd_remove,
915 };
916 
917 static int __init wm97xx_init(void)
918 {
919 	int ret;
920 
921 	ret = platform_driver_register(&wm97xx_mfd_driver);
922 	if (ret)
923 		return ret;
924 
925 	if (IS_BUILTIN(CONFIG_AC97_BUS))
926 		ret =  driver_register(&wm97xx_driver);
927 	return ret;
928 }
929 
930 static void __exit wm97xx_exit(void)
931 {
932 	if (IS_BUILTIN(CONFIG_AC97_BUS))
933 		driver_unregister(&wm97xx_driver);
934 	platform_driver_unregister(&wm97xx_mfd_driver);
935 }
936 
937 module_init(wm97xx_init);
938 module_exit(wm97xx_exit);
939 
940 /* Module information */
941 MODULE_AUTHOR("Liam Girdwood <lrg@slimlogic.co.uk>");
942 MODULE_DESCRIPTION("WM97xx Core - Touch Screen / AUX ADC / GPIO Driver");
943 MODULE_LICENSE("GPL");
944