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