1/* SPDX-License-Identifier: GPL-2.0 */
2
3/*
4 * Register bits and API for Wolfson WM97xx series of codecs
5 */
6
7#ifndef _LINUX_WM97XX_H
8#define _LINUX_WM97XX_H
9
10#include <sound/core.h>
11#include <sound/pcm.h>
12#include <sound/ac97_codec.h>
13#include <sound/initval.h>
14#include <linux/types.h>
15#include <linux/list.h>
16#include <linux/input.h>	/* Input device layer */
17#include <linux/platform_device.h>
18
19/*
20 * WM97xx variants
21 */
22#define	WM97xx_GENERIC			0x0000
23#define	WM97xx_WM1613			0x1613
24
25/*
26 * WM97xx AC97 Touchscreen registers
27 */
28#define AC97_WM97XX_DIGITISER1		0x76
29#define AC97_WM97XX_DIGITISER2		0x78
30#define AC97_WM97XX_DIGITISER_RD 	0x7a
31#define AC97_WM9713_DIG1		0x74
32#define AC97_WM9713_DIG2		AC97_WM97XX_DIGITISER1
33#define AC97_WM9713_DIG3		AC97_WM97XX_DIGITISER2
34
35/*
36 * WM97xx register bits
37 */
38#define WM97XX_POLL		0x8000	/* initiate a polling measurement */
39#define WM97XX_ADCSEL_X		0x1000	/* x coord measurement */
40#define WM97XX_ADCSEL_Y		0x2000	/* y coord measurement */
41#define WM97XX_ADCSEL_PRES	0x3000	/* pressure measurement */
42#define WM97XX_AUX_ID1		0x4000
43#define WM97XX_AUX_ID2		0x5000
44#define WM97XX_AUX_ID3		0x6000
45#define WM97XX_AUX_ID4		0x7000
46#define WM97XX_ADCSEL_MASK	0x7000	/* ADC selection mask */
47#define WM97XX_COO		0x0800	/* enable coordinate mode */
48#define WM97XX_CTC		0x0400	/* enable continuous mode */
49#define WM97XX_CM_RATE_93	0x0000	/* 93.75Hz continuous rate */
50#define WM97XX_CM_RATE_187	0x0100	/* 187.5Hz continuous rate */
51#define WM97XX_CM_RATE_375	0x0200	/* 375Hz continuous rate */
52#define WM97XX_CM_RATE_750	0x0300	/* 750Hz continuous rate */
53#define WM97XX_CM_RATE_8K	0x00f0	/* 8kHz continuous rate */
54#define WM97XX_CM_RATE_12K	0x01f0	/* 12kHz continuous rate */
55#define WM97XX_CM_RATE_24K	0x02f0	/* 24kHz continuous rate */
56#define WM97XX_CM_RATE_48K	0x03f0	/* 48kHz continuous rate */
57#define WM97XX_CM_RATE_MASK	0x03f0
58#define WM97XX_RATE(i)		(((i & 3) << 8) | ((i & 4) ? 0xf0 : 0))
59#define WM97XX_DELAY(i)		((i << 4) & 0x00f0)	/* sample delay times */
60#define WM97XX_DELAY_MASK	0x00f0
61#define WM97XX_SLEN		0x0008	/* slot read back enable */
62#define WM97XX_SLT(i)		((i - 5) & 0x7)	/* panel slot (5-11) */
63#define WM97XX_SLT_MASK		0x0007
64#define WM97XX_PRP_DETW		0x4000	/* detect on, digitise off, wake */
65#define WM97XX_PRP_DET		0x8000	/* detect on, digitise off, no wake */
66#define WM97XX_PRP_DET_DIG	0xc000	/* setect on, digitise on */
67#define WM97XX_RPR		0x2000	/* wake up on pen down */
68#define WM97XX_PEN_DOWN		0x8000	/* pen is down */
69
70/* WM9712 Bits */
71#define WM9712_45W		0x1000	/* set for 5-wire touchscreen */
72#define WM9712_PDEN		0x0800	/* measure only when pen down */
73#define WM9712_WAIT		0x0200	/* wait until adc is read before next sample */
74#define WM9712_PIL		0x0100	/* current used for pressure measurement. set 400uA else 200uA */
75#define WM9712_MASK_HI		0x0040	/* hi on mask pin (47) stops conversions */
76#define WM9712_MASK_EDGE	0x0080	/* rising/falling edge on pin delays sample */
77#define	WM9712_MASK_SYNC	0x00c0	/* rising/falling edge on mask initiates sample */
78#define WM9712_RPU(i)		(i&0x3f)	/* internal pull up on pen detect (64k / rpu) */
79#define WM9712_PD(i)		(0x1 << i)	/* power management */
80
81/* WM9712 Registers */
82#define AC97_WM9712_POWER	0x24
83#define AC97_WM9712_REV		0x58
84
85/* WM9705 Bits */
86#define WM9705_PDEN		0x1000	/* measure only when pen is down */
87#define WM9705_PINV		0x0800	/* inverts sense of pen down output */
88#define WM9705_BSEN		0x0400	/* BUSY flag enable, pin47 is 1 when busy */
89#define WM9705_BINV		0x0200	/* invert BUSY (pin47) output */
90#define WM9705_WAIT		0x0100	/* wait until adc is read before next sample */
91#define WM9705_PIL		0x0080	/* current used for pressure measurement. set 400uA else 200uA */
92#define WM9705_PHIZ		0x0040	/* set PHONE and PCBEEP inputs to high impedance */
93#define WM9705_MASK_HI		0x0010	/* hi on mask stops conversions */
94#define WM9705_MASK_EDGE	0x0020	/* rising/falling edge on pin delays sample */
95#define	WM9705_MASK_SYNC	0x0030	/* rising/falling edge on mask initiates sample */
96#define WM9705_PDD(i)		(i & 0x000f)	/* pen detect comparator threshold */
97
98
99/* WM9713 Bits */
100#define WM9713_PDPOL		0x0400	/* Pen down polarity */
101#define WM9713_POLL		0x0200	/* initiate a polling measurement */
102#define WM9713_CTC		0x0100	/* enable continuous mode */
103#define WM9713_ADCSEL_X		0x0002	/* X measurement */
104#define WM9713_ADCSEL_Y		0x0004	/* Y measurement */
105#define WM9713_ADCSEL_PRES	0x0008	/* Pressure measurement */
106#define WM9713_COO		0x0001	/* enable coordinate mode */
107#define WM9713_45W		0x1000  /* set for 5 wire panel */
108#define WM9713_PDEN		0x0800	/* measure only when pen down */
109#define WM9713_ADCSEL_MASK	0x00fe	/* ADC selection mask */
110#define WM9713_WAIT		0x0200	/* coordinate wait */
111
112/* AUX ADC ID's */
113#define TS_COMP1		0x0
114#define TS_COMP2		0x1
115#define TS_BMON			0x2
116#define TS_WIPER		0x3
117
118/* ID numbers */
119#define WM97XX_ID1		0x574d
120#define WM9712_ID2		0x4c12
121#define WM9705_ID2		0x4c05
122#define WM9713_ID2		0x4c13
123
124/* Codec GPIO's */
125#define WM97XX_MAX_GPIO		16
126#define WM97XX_GPIO_1		(1 << 1)
127#define WM97XX_GPIO_2		(1 << 2)
128#define WM97XX_GPIO_3		(1 << 3)
129#define WM97XX_GPIO_4		(1 << 4)
130#define WM97XX_GPIO_5		(1 << 5)
131#define WM97XX_GPIO_6		(1 << 6)
132#define WM97XX_GPIO_7		(1 << 7)
133#define WM97XX_GPIO_8		(1 << 8)
134#define WM97XX_GPIO_9		(1 << 9)
135#define WM97XX_GPIO_10		(1 << 10)
136#define WM97XX_GPIO_11		(1 << 11)
137#define WM97XX_GPIO_12		(1 << 12)
138#define WM97XX_GPIO_13		(1 << 13)
139#define WM97XX_GPIO_14		(1 << 14)
140#define WM97XX_GPIO_15		(1 << 15)
141
142
143#define AC97_LINK_FRAME		21	/* time in uS for AC97 link frame */
144
145
146/*---------------- Return codes from sample reading functions ---------------*/
147
148/* More data is available; call the sample gathering function again */
149#define RC_AGAIN			0x00000001
150/* The returned sample is valid */
151#define RC_VALID			0x00000002
152/* The pen is up (the first RC_VALID without RC_PENUP means pen is down) */
153#define RC_PENUP			0x00000004
154/* The pen is down (RC_VALID implies RC_PENDOWN, but sometimes it is helpful
155   to tell the handler that the pen is down but we don't know yet his coords,
156   so the handler should not sleep or wait for pendown irq) */
157#define RC_PENDOWN			0x00000008
158
159/*
160 * The wm97xx driver provides a private API for writing platform-specific
161 * drivers.
162 */
163
164/* The structure used to return arch specific sampled data into */
165struct wm97xx_data {
166    int x;
167    int y;
168    int p;
169};
170
171/*
172 * Codec GPIO status
173 */
174enum wm97xx_gpio_status {
175    WM97XX_GPIO_HIGH,
176    WM97XX_GPIO_LOW
177};
178
179/*
180 * Codec GPIO direction
181 */
182enum wm97xx_gpio_dir {
183    WM97XX_GPIO_IN,
184    WM97XX_GPIO_OUT
185};
186
187/*
188 * Codec GPIO polarity
189 */
190enum wm97xx_gpio_pol {
191    WM97XX_GPIO_POL_HIGH,
192    WM97XX_GPIO_POL_LOW
193};
194
195/*
196 * Codec GPIO sticky
197 */
198enum wm97xx_gpio_sticky {
199    WM97XX_GPIO_STICKY,
200    WM97XX_GPIO_NOTSTICKY
201};
202
203/*
204 * Codec GPIO wake
205 */
206enum wm97xx_gpio_wake {
207    WM97XX_GPIO_WAKE,
208    WM97XX_GPIO_NOWAKE
209};
210
211/*
212 * Digitiser ioctl commands
213 */
214#define WM97XX_DIG_START	0x1
215#define WM97XX_DIG_STOP		0x2
216#define WM97XX_PHY_INIT		0x3
217#define WM97XX_AUX_PREPARE	0x4
218#define WM97XX_DIG_RESTORE	0x5
219
220struct wm97xx;
221
222extern struct wm97xx_codec_drv wm9705_codec;
223extern struct wm97xx_codec_drv wm9712_codec;
224extern struct wm97xx_codec_drv wm9713_codec;
225
226/*
227 * Codec driver interface - allows mapping to WM9705/12/13 and newer codecs
228 */
229struct wm97xx_codec_drv {
230	u16 id;
231	char *name;
232
233	/* read 1 sample */
234	int (*poll_sample) (struct wm97xx *, int adcsel, int *sample);
235
236	/* read X,Y,[P] in poll */
237	int (*poll_touch) (struct wm97xx *, struct wm97xx_data *);
238
239	int (*acc_enable) (struct wm97xx *, int enable);
240	void (*phy_init) (struct wm97xx *);
241	void (*dig_enable) (struct wm97xx *, int enable);
242	void (*dig_restore) (struct wm97xx *);
243	void (*aux_prepare) (struct wm97xx *);
244};
245
246
247/* Machine specific and accelerated touch operations */
248struct wm97xx_mach_ops {
249
250	/* accelerated touch readback - coords are transmited on AC97 link */
251	int acc_enabled;
252	void (*acc_pen_up) (struct wm97xx *);
253	int (*acc_pen_down) (struct wm97xx *);
254	int (*acc_startup) (struct wm97xx *);
255	void (*acc_shutdown) (struct wm97xx *);
256
257	/* GPIO pin used for accelerated operation */
258	int irq_gpio;
259
260	/* pre and post sample - can be used to minimise any analog noise */
261	void (*pre_sample) (int);  /* function to run before sampling */
262	void (*post_sample) (int);  /* function to run after sampling */
263};
264
265struct wm97xx {
266	u16 dig[3], id, gpio[6], misc;	/* Cached codec registers */
267	u16 dig_save[3];		/* saved during aux reading */
268	struct wm97xx_codec_drv *codec;	/* attached codec driver*/
269	struct input_dev *input_dev;	/* touchscreen input device */
270	struct snd_ac97 *ac97;		/* ALSA codec access */
271	struct device *dev;		/* ALSA device */
272	struct platform_device *battery_dev;
273	struct platform_device *touch_dev;
274	struct wm97xx_mach_ops *mach_ops;
275	struct mutex codec_mutex;
276	struct delayed_work ts_reader;  /* Used to poll touchscreen */
277	unsigned long ts_reader_interval; /* Current interval for timer */
278	unsigned long ts_reader_min_interval; /* Minimum interval */
279	unsigned int pen_irq;		/* Pen IRQ number in use */
280	struct workqueue_struct *ts_workq;
281	u16 acc_slot;			/* AC97 slot used for acc touch data */
282	u16 acc_rate;			/* acc touch data rate */
283	unsigned pen_is_down:1;		/* Pen is down */
284	unsigned aux_waiting:1;		/* aux measurement waiting */
285	unsigned pen_probably_down:1;	/* used in polling mode */
286	u16 variant;			/* WM97xx chip variant */
287	u16 suspend_mode;               /* PRP in suspend mode */
288};
289
290struct wm97xx_batt_pdata {
291	int	batt_aux;
292	int	temp_aux;
293	int	min_voltage;
294	int	max_voltage;
295	int	batt_div;
296	int	batt_mult;
297	int	temp_div;
298	int	temp_mult;
299	int	batt_tech;
300	char	*batt_name;
301};
302
303struct wm97xx_pdata {
304	struct wm97xx_batt_pdata	*batt_pdata;	/* battery data */
305};
306
307/*
308 * Codec GPIO access (not supported on WM9705)
309 * This can be used to set/get codec GPIO and Virtual GPIO status.
310 */
311enum wm97xx_gpio_status wm97xx_get_gpio(struct wm97xx *wm, u32 gpio);
312void wm97xx_set_gpio(struct wm97xx *wm, u32 gpio,
313			  enum wm97xx_gpio_status status);
314void wm97xx_config_gpio(struct wm97xx *wm, u32 gpio,
315				     enum wm97xx_gpio_dir dir,
316				     enum wm97xx_gpio_pol pol,
317				     enum wm97xx_gpio_sticky sticky,
318				     enum wm97xx_gpio_wake wake);
319
320void wm97xx_set_suspend_mode(struct wm97xx *wm, u16 mode);
321
322/* codec AC97 IO access */
323int wm97xx_reg_read(struct wm97xx *wm, u16 reg);
324void wm97xx_reg_write(struct wm97xx *wm, u16 reg, u16 val);
325
326/* aux adc readback */
327int wm97xx_read_aux_adc(struct wm97xx *wm, u16 adcsel);
328
329/* machine ops */
330int wm97xx_register_mach_ops(struct wm97xx *, struct wm97xx_mach_ops *);
331void wm97xx_unregister_mach_ops(struct wm97xx *);
332
333#endif
334