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