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