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