xref: /openbmc/linux/sound/soc/codecs/wm8904.c (revision c133421800d9d1dfec0c98de6c9da0a7a99e0573)
1a91eb199SMark Brown /*
2a91eb199SMark Brown  * wm8904.c  --  WM8904 ALSA SoC Audio driver
3a91eb199SMark Brown  *
4a91eb199SMark Brown  * Copyright 2009 Wolfson Microelectronics plc
5a91eb199SMark Brown  *
6a91eb199SMark Brown  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
7a91eb199SMark Brown  *
8a91eb199SMark Brown  *
9a91eb199SMark Brown  * This program is free software; you can redistribute it and/or modify
10a91eb199SMark Brown  * it under the terms of the GNU General Public License version 2 as
11a91eb199SMark Brown  * published by the Free Software Foundation.
12a91eb199SMark Brown  */
13a91eb199SMark Brown 
14a91eb199SMark Brown #include <linux/module.h>
15a91eb199SMark Brown #include <linux/moduleparam.h>
16a91eb199SMark Brown #include <linux/init.h>
17a91eb199SMark Brown #include <linux/delay.h>
18a91eb199SMark Brown #include <linux/pm.h>
19a91eb199SMark Brown #include <linux/i2c.h>
20a91eb199SMark Brown #include <linux/platform_device.h>
21a91eb199SMark Brown #include <linux/regulator/consumer.h>
22a91eb199SMark Brown #include <sound/core.h>
23a91eb199SMark Brown #include <sound/pcm.h>
24a91eb199SMark Brown #include <sound/pcm_params.h>
25a91eb199SMark Brown #include <sound/soc.h>
26a91eb199SMark Brown #include <sound/soc-dapm.h>
27a91eb199SMark Brown #include <sound/initval.h>
28a91eb199SMark Brown #include <sound/tlv.h>
29a91eb199SMark Brown #include <sound/wm8904.h>
30a91eb199SMark Brown 
31a91eb199SMark Brown #include "wm8904.h"
32a91eb199SMark Brown 
33a91eb199SMark Brown static struct snd_soc_codec *wm8904_codec;
34a91eb199SMark Brown struct snd_soc_codec_device soc_codec_dev_wm8904;
35a91eb199SMark Brown 
36a91eb199SMark Brown #define WM8904_NUM_DCS_CHANNELS 4
37a91eb199SMark Brown 
38a91eb199SMark Brown #define WM8904_NUM_SUPPLIES 5
39a91eb199SMark Brown static const char *wm8904_supply_names[WM8904_NUM_SUPPLIES] = {
40a91eb199SMark Brown 	"DCVDD",
41a91eb199SMark Brown 	"DBVDD",
42a91eb199SMark Brown 	"AVDD",
43a91eb199SMark Brown 	"CPVDD",
44a91eb199SMark Brown 	"MICVDD",
45a91eb199SMark Brown };
46a91eb199SMark Brown 
47a91eb199SMark Brown /* codec private data */
48a91eb199SMark Brown struct wm8904_priv {
49a91eb199SMark Brown 	struct snd_soc_codec codec;
50a91eb199SMark Brown 	u16 reg_cache[WM8904_MAX_REGISTER + 1];
51a91eb199SMark Brown 
52a91eb199SMark Brown 	struct regulator_bulk_data supplies[WM8904_NUM_SUPPLIES];
53a91eb199SMark Brown 
54a91eb199SMark Brown 	struct wm8904_pdata *pdata;
55a91eb199SMark Brown 
56a91eb199SMark Brown 	int deemph;
57a91eb199SMark Brown 
58a91eb199SMark Brown 	/* Platform provided DRC configuration */
59a91eb199SMark Brown 	const char **drc_texts;
60a91eb199SMark Brown 	int drc_cfg;
61a91eb199SMark Brown 	struct soc_enum drc_enum;
62a91eb199SMark Brown 
63a91eb199SMark Brown 	/* Platform provided ReTune mobile configuration */
64a91eb199SMark Brown 	int num_retune_mobile_texts;
65a91eb199SMark Brown 	const char **retune_mobile_texts;
66a91eb199SMark Brown 	int retune_mobile_cfg;
67a91eb199SMark Brown 	struct soc_enum retune_mobile_enum;
68a91eb199SMark Brown 
69a91eb199SMark Brown 	/* FLL setup */
70a91eb199SMark Brown 	int fll_src;
71a91eb199SMark Brown 	int fll_fref;
72a91eb199SMark Brown 	int fll_fout;
73a91eb199SMark Brown 
74a91eb199SMark Brown 	/* Clocking configuration */
75a91eb199SMark Brown 	unsigned int mclk_rate;
76a91eb199SMark Brown 	int sysclk_src;
77a91eb199SMark Brown 	unsigned int sysclk_rate;
78a91eb199SMark Brown 
79a91eb199SMark Brown 	int tdm_width;
80a91eb199SMark Brown 	int tdm_slots;
81a91eb199SMark Brown 	int bclk;
82a91eb199SMark Brown 	int fs;
83a91eb199SMark Brown 
84a91eb199SMark Brown 	/* DC servo configuration - cached offset values */
85a91eb199SMark Brown 	int dcs_state[WM8904_NUM_DCS_CHANNELS];
86a91eb199SMark Brown };
87a91eb199SMark Brown 
88a91eb199SMark Brown static const u16 wm8904_reg[WM8904_MAX_REGISTER + 1] = {
89a91eb199SMark Brown 	0x8904,     /* R0   - SW Reset and ID */
90a91eb199SMark Brown 	0x0000,     /* R1   - Revision */
91a91eb199SMark Brown 	0x0000,     /* R2 */
92a91eb199SMark Brown 	0x0000,     /* R3 */
93a91eb199SMark Brown 	0x0018,     /* R4   - Bias Control 0 */
94a91eb199SMark Brown 	0x0000,     /* R5   - VMID Control 0 */
95a91eb199SMark Brown 	0x0000,     /* R6   - Mic Bias Control 0 */
96a91eb199SMark Brown 	0x0000,     /* R7   - Mic Bias Control 1 */
97a91eb199SMark Brown 	0x0001,     /* R8   - Analogue DAC 0 */
98a91eb199SMark Brown 	0x9696,     /* R9   - mic Filter Control */
99a91eb199SMark Brown 	0x0001,     /* R10  - Analogue ADC 0 */
100a91eb199SMark Brown 	0x0000,     /* R11 */
101a91eb199SMark Brown 	0x0000,     /* R12  - Power Management 0 */
102a91eb199SMark Brown 	0x0000,     /* R13 */
103a91eb199SMark Brown 	0x0000,     /* R14  - Power Management 2 */
104a91eb199SMark Brown 	0x0000,     /* R15  - Power Management 3 */
105a91eb199SMark Brown 	0x0000,     /* R16 */
106a91eb199SMark Brown 	0x0000,     /* R17 */
107a91eb199SMark Brown 	0x0000,     /* R18  - Power Management 6 */
108a91eb199SMark Brown 	0x0000,     /* R19 */
109a91eb199SMark Brown 	0x945E,     /* R20  - Clock Rates 0 */
110a91eb199SMark Brown 	0x0C05,     /* R21  - Clock Rates 1 */
111a91eb199SMark Brown 	0x0006,     /* R22  - Clock Rates 2 */
112a91eb199SMark Brown 	0x0000,     /* R23 */
113a91eb199SMark Brown 	0x0050,     /* R24  - Audio Interface 0 */
114a91eb199SMark Brown 	0x000A,     /* R25  - Audio Interface 1 */
115a91eb199SMark Brown 	0x00E4,     /* R26  - Audio Interface 2 */
116a91eb199SMark Brown 	0x0040,     /* R27  - Audio Interface 3 */
117a91eb199SMark Brown 	0x0000,     /* R28 */
118a91eb199SMark Brown 	0x0000,     /* R29 */
119a91eb199SMark Brown 	0x00C0,     /* R30  - DAC Digital Volume Left */
120a91eb199SMark Brown 	0x00C0,     /* R31  - DAC Digital Volume Right */
121a91eb199SMark Brown 	0x0000,     /* R32  - DAC Digital 0 */
122a91eb199SMark Brown 	0x0008,     /* R33  - DAC Digital 1 */
123a91eb199SMark Brown 	0x0000,     /* R34 */
124a91eb199SMark Brown 	0x0000,     /* R35 */
125a91eb199SMark Brown 	0x00C0,     /* R36  - ADC Digital Volume Left */
126a91eb199SMark Brown 	0x00C0,     /* R37  - ADC Digital Volume Right */
127a91eb199SMark Brown 	0x0010,     /* R38  - ADC Digital 0 */
128a91eb199SMark Brown 	0x0000,     /* R39  - Digital Microphone 0 */
129a91eb199SMark Brown 	0x01AF,     /* R40  - DRC 0 */
130a91eb199SMark Brown 	0x3248,     /* R41  - DRC 1 */
131a91eb199SMark Brown 	0x0000,     /* R42  - DRC 2 */
132a91eb199SMark Brown 	0x0000,     /* R43  - DRC 3 */
133a91eb199SMark Brown 	0x0085,     /* R44  - Analogue Left Input 0 */
134a91eb199SMark Brown 	0x0085,     /* R45  - Analogue Right Input 0 */
135a91eb199SMark Brown 	0x0044,     /* R46  - Analogue Left Input 1 */
136a91eb199SMark Brown 	0x0044,     /* R47  - Analogue Right Input 1 */
137a91eb199SMark Brown 	0x0000,     /* R48 */
138a91eb199SMark Brown 	0x0000,     /* R49 */
139a91eb199SMark Brown 	0x0000,     /* R50 */
140a91eb199SMark Brown 	0x0000,     /* R51 */
141a91eb199SMark Brown 	0x0000,     /* R52 */
142a91eb199SMark Brown 	0x0000,     /* R53 */
143a91eb199SMark Brown 	0x0000,     /* R54 */
144a91eb199SMark Brown 	0x0000,     /* R55 */
145a91eb199SMark Brown 	0x0000,     /* R56 */
146a91eb199SMark Brown 	0x002D,     /* R57  - Analogue OUT1 Left */
147a91eb199SMark Brown 	0x002D,     /* R58  - Analogue OUT1 Right */
148a91eb199SMark Brown 	0x0039,     /* R59  - Analogue OUT2 Left */
149a91eb199SMark Brown 	0x0039,     /* R60  - Analogue OUT2 Right */
150a91eb199SMark Brown 	0x0000,     /* R61  - Analogue OUT12 ZC */
151a91eb199SMark Brown 	0x0000,     /* R62 */
152a91eb199SMark Brown 	0x0000,     /* R63 */
153a91eb199SMark Brown 	0x0000,     /* R64 */
154a91eb199SMark Brown 	0x0000,     /* R65 */
155a91eb199SMark Brown 	0x0000,     /* R66 */
156a91eb199SMark Brown 	0x0000,     /* R67  - DC Servo 0 */
157a91eb199SMark Brown 	0x0000,     /* R68  - DC Servo 1 */
158a91eb199SMark Brown 	0xAAAA,     /* R69  - DC Servo 2 */
159a91eb199SMark Brown 	0x0000,     /* R70 */
160a91eb199SMark Brown 	0xAAAA,     /* R71  - DC Servo 4 */
161a91eb199SMark Brown 	0xAAAA,     /* R72  - DC Servo 5 */
162a91eb199SMark Brown 	0x0000,     /* R73  - DC Servo 6 */
163a91eb199SMark Brown 	0x0000,     /* R74  - DC Servo 7 */
164a91eb199SMark Brown 	0x0000,     /* R75  - DC Servo 8 */
165a91eb199SMark Brown 	0x0000,     /* R76  - DC Servo 9 */
166a91eb199SMark Brown 	0x0000,     /* R77  - DC Servo Readback 0 */
167a91eb199SMark Brown 	0x0000,     /* R78 */
168a91eb199SMark Brown 	0x0000,     /* R79 */
169a91eb199SMark Brown 	0x0000,     /* R80 */
170a91eb199SMark Brown 	0x0000,     /* R81 */
171a91eb199SMark Brown 	0x0000,     /* R82 */
172a91eb199SMark Brown 	0x0000,     /* R83 */
173a91eb199SMark Brown 	0x0000,     /* R84 */
174a91eb199SMark Brown 	0x0000,     /* R85 */
175a91eb199SMark Brown 	0x0000,     /* R86 */
176a91eb199SMark Brown 	0x0000,     /* R87 */
177a91eb199SMark Brown 	0x0000,     /* R88 */
178a91eb199SMark Brown 	0x0000,     /* R89 */
179a91eb199SMark Brown 	0x0000,     /* R90  - Analogue HP 0 */
180a91eb199SMark Brown 	0x0000,     /* R91 */
181a91eb199SMark Brown 	0x0000,     /* R92 */
182a91eb199SMark Brown 	0x0000,     /* R93 */
183a91eb199SMark Brown 	0x0000,     /* R94  - Analogue Lineout 0 */
184a91eb199SMark Brown 	0x0000,     /* R95 */
185a91eb199SMark Brown 	0x0000,     /* R96 */
186a91eb199SMark Brown 	0x0000,     /* R97 */
187a91eb199SMark Brown 	0x0000,     /* R98  - Charge Pump 0 */
188a91eb199SMark Brown 	0x0000,     /* R99 */
189a91eb199SMark Brown 	0x0000,     /* R100 */
190a91eb199SMark Brown 	0x0000,     /* R101 */
191a91eb199SMark Brown 	0x0000,     /* R102 */
192a91eb199SMark Brown 	0x0000,     /* R103 */
193a91eb199SMark Brown 	0x0004,     /* R104 - Class W 0 */
194a91eb199SMark Brown 	0x0000,     /* R105 */
195a91eb199SMark Brown 	0x0000,     /* R106 */
196a91eb199SMark Brown 	0x0000,     /* R107 */
197a91eb199SMark Brown 	0x0000,     /* R108 - Write Sequencer 0 */
198a91eb199SMark Brown 	0x0000,     /* R109 - Write Sequencer 1 */
199a91eb199SMark Brown 	0x0000,     /* R110 - Write Sequencer 2 */
200a91eb199SMark Brown 	0x0000,     /* R111 - Write Sequencer 3 */
201a91eb199SMark Brown 	0x0000,     /* R112 - Write Sequencer 4 */
202a91eb199SMark Brown 	0x0000,     /* R113 */
203a91eb199SMark Brown 	0x0000,     /* R114 */
204a91eb199SMark Brown 	0x0000,     /* R115 */
205a91eb199SMark Brown 	0x0000,     /* R116 - FLL Control 1 */
206a91eb199SMark Brown 	0x0007,     /* R117 - FLL Control 2 */
207a91eb199SMark Brown 	0x0000,     /* R118 - FLL Control 3 */
208a91eb199SMark Brown 	0x2EE0,     /* R119 - FLL Control 4 */
209a91eb199SMark Brown 	0x0004,     /* R120 - FLL Control 5 */
210a91eb199SMark Brown 	0x0014,     /* R121 - GPIO Control 1 */
211a91eb199SMark Brown 	0x0010,     /* R122 - GPIO Control 2 */
212a91eb199SMark Brown 	0x0010,     /* R123 - GPIO Control 3 */
213a91eb199SMark Brown 	0x0000,     /* R124 - GPIO Control 4 */
214a91eb199SMark Brown 	0x0000,     /* R125 */
215a91eb199SMark Brown 	0x0000,     /* R126 - Digital Pulls */
216a91eb199SMark Brown 	0x0000,     /* R127 - Interrupt Status */
217a91eb199SMark Brown 	0xFFFF,     /* R128 - Interrupt Status Mask */
218a91eb199SMark Brown 	0x0000,     /* R129 - Interrupt Polarity */
219a91eb199SMark Brown 	0x0000,     /* R130 - Interrupt Debounce */
220a91eb199SMark Brown 	0x0000,     /* R131 */
221a91eb199SMark Brown 	0x0000,     /* R132 */
222a91eb199SMark Brown 	0x0000,     /* R133 */
223a91eb199SMark Brown 	0x0000,     /* R134 - EQ1 */
224a91eb199SMark Brown 	0x000C,     /* R135 - EQ2 */
225a91eb199SMark Brown 	0x000C,     /* R136 - EQ3 */
226a91eb199SMark Brown 	0x000C,     /* R137 - EQ4 */
227a91eb199SMark Brown 	0x000C,     /* R138 - EQ5 */
228a91eb199SMark Brown 	0x000C,     /* R139 - EQ6 */
229a91eb199SMark Brown 	0x0FCA,     /* R140 - EQ7 */
230a91eb199SMark Brown 	0x0400,     /* R141 - EQ8 */
231a91eb199SMark Brown 	0x00D8,     /* R142 - EQ9 */
232a91eb199SMark Brown 	0x1EB5,     /* R143 - EQ10 */
233a91eb199SMark Brown 	0xF145,     /* R144 - EQ11 */
234a91eb199SMark Brown 	0x0B75,     /* R145 - EQ12 */
235a91eb199SMark Brown 	0x01C5,     /* R146 - EQ13 */
236a91eb199SMark Brown 	0x1C58,     /* R147 - EQ14 */
237a91eb199SMark Brown 	0xF373,     /* R148 - EQ15 */
238a91eb199SMark Brown 	0x0A54,     /* R149 - EQ16 */
239a91eb199SMark Brown 	0x0558,     /* R150 - EQ17 */
240a91eb199SMark Brown 	0x168E,     /* R151 - EQ18 */
241a91eb199SMark Brown 	0xF829,     /* R152 - EQ19 */
242a91eb199SMark Brown 	0x07AD,     /* R153 - EQ20 */
243a91eb199SMark Brown 	0x1103,     /* R154 - EQ21 */
244a91eb199SMark Brown 	0x0564,     /* R155 - EQ22 */
245a91eb199SMark Brown 	0x0559,     /* R156 - EQ23 */
246a91eb199SMark Brown 	0x4000,     /* R157 - EQ24 */
247a91eb199SMark Brown 	0x0000,     /* R158 */
248a91eb199SMark Brown 	0x0000,     /* R159 */
249a91eb199SMark Brown 	0x0000,     /* R160 */
250a91eb199SMark Brown 	0x0000,     /* R161 - Control Interface Test 1 */
251a91eb199SMark Brown 	0x0000,     /* R162 */
252a91eb199SMark Brown 	0x0000,     /* R163 */
253a91eb199SMark Brown 	0x0000,     /* R164 */
254a91eb199SMark Brown 	0x0000,     /* R165 */
255a91eb199SMark Brown 	0x0000,     /* R166 */
256a91eb199SMark Brown 	0x0000,     /* R167 */
257a91eb199SMark Brown 	0x0000,     /* R168 */
258a91eb199SMark Brown 	0x0000,     /* R169 */
259a91eb199SMark Brown 	0x0000,     /* R170 */
260a91eb199SMark Brown 	0x0000,     /* R171 */
261a91eb199SMark Brown 	0x0000,     /* R172 */
262a91eb199SMark Brown 	0x0000,     /* R173 */
263a91eb199SMark Brown 	0x0000,     /* R174 */
264a91eb199SMark Brown 	0x0000,     /* R175 */
265a91eb199SMark Brown 	0x0000,     /* R176 */
266a91eb199SMark Brown 	0x0000,     /* R177 */
267a91eb199SMark Brown 	0x0000,     /* R178 */
268a91eb199SMark Brown 	0x0000,     /* R179 */
269a91eb199SMark Brown 	0x0000,     /* R180 */
270a91eb199SMark Brown 	0x0000,     /* R181 */
271a91eb199SMark Brown 	0x0000,     /* R182 */
272a91eb199SMark Brown 	0x0000,     /* R183 */
273a91eb199SMark Brown 	0x0000,     /* R184 */
274a91eb199SMark Brown 	0x0000,     /* R185 */
275a91eb199SMark Brown 	0x0000,     /* R186 */
276a91eb199SMark Brown 	0x0000,     /* R187 */
277a91eb199SMark Brown 	0x0000,     /* R188 */
278a91eb199SMark Brown 	0x0000,     /* R189 */
279a91eb199SMark Brown 	0x0000,     /* R190 */
280a91eb199SMark Brown 	0x0000,     /* R191 */
281a91eb199SMark Brown 	0x0000,     /* R192 */
282a91eb199SMark Brown 	0x0000,     /* R193 */
283a91eb199SMark Brown 	0x0000,     /* R194 */
284a91eb199SMark Brown 	0x0000,     /* R195 */
285a91eb199SMark Brown 	0x0000,     /* R196 */
286a91eb199SMark Brown 	0x0000,     /* R197 */
287a91eb199SMark Brown 	0x0000,     /* R198 */
288a91eb199SMark Brown 	0x0000,     /* R199 */
289a91eb199SMark Brown 	0x0000,     /* R200 */
290a91eb199SMark Brown 	0x0000,     /* R201 */
291a91eb199SMark Brown 	0x0000,     /* R202 */
292a91eb199SMark Brown 	0x0000,     /* R203 */
293a91eb199SMark Brown 	0x0000,     /* R204 - Analogue Output Bias 0 */
294a91eb199SMark Brown 	0x0000,     /* R205 */
295a91eb199SMark Brown 	0x0000,     /* R206 */
296a91eb199SMark Brown 	0x0000,     /* R207 */
297a91eb199SMark Brown 	0x0000,     /* R208 */
298a91eb199SMark Brown 	0x0000,     /* R209 */
299a91eb199SMark Brown 	0x0000,     /* R210 */
300a91eb199SMark Brown 	0x0000,     /* R211 */
301a91eb199SMark Brown 	0x0000,     /* R212 */
302a91eb199SMark Brown 	0x0000,     /* R213 */
303a91eb199SMark Brown 	0x0000,     /* R214 */
304a91eb199SMark Brown 	0x0000,     /* R215 */
305a91eb199SMark Brown 	0x0000,     /* R216 */
306a91eb199SMark Brown 	0x0000,     /* R217 */
307a91eb199SMark Brown 	0x0000,     /* R218 */
308a91eb199SMark Brown 	0x0000,     /* R219 */
309a91eb199SMark Brown 	0x0000,     /* R220 */
310a91eb199SMark Brown 	0x0000,     /* R221 */
311a91eb199SMark Brown 	0x0000,     /* R222 */
312a91eb199SMark Brown 	0x0000,     /* R223 */
313a91eb199SMark Brown 	0x0000,     /* R224 */
314a91eb199SMark Brown 	0x0000,     /* R225 */
315a91eb199SMark Brown 	0x0000,     /* R226 */
316a91eb199SMark Brown 	0x0000,     /* R227 */
317a91eb199SMark Brown 	0x0000,     /* R228 */
318a91eb199SMark Brown 	0x0000,     /* R229 */
319a91eb199SMark Brown 	0x0000,     /* R230 */
320a91eb199SMark Brown 	0x0000,     /* R231 */
321a91eb199SMark Brown 	0x0000,     /* R232 */
322a91eb199SMark Brown 	0x0000,     /* R233 */
323a91eb199SMark Brown 	0x0000,     /* R234 */
324a91eb199SMark Brown 	0x0000,     /* R235 */
325a91eb199SMark Brown 	0x0000,     /* R236 */
326a91eb199SMark Brown 	0x0000,     /* R237 */
327a91eb199SMark Brown 	0x0000,     /* R238 */
328a91eb199SMark Brown 	0x0000,     /* R239 */
329a91eb199SMark Brown 	0x0000,     /* R240 */
330a91eb199SMark Brown 	0x0000,     /* R241 */
331a91eb199SMark Brown 	0x0000,     /* R242 */
332a91eb199SMark Brown 	0x0000,     /* R243 */
333a91eb199SMark Brown 	0x0000,     /* R244 */
334a91eb199SMark Brown 	0x0000,     /* R245 */
335a91eb199SMark Brown 	0x0000,     /* R246 */
336a91eb199SMark Brown 	0x0000,     /* R247 - FLL NCO Test 0 */
337a91eb199SMark Brown 	0x0019,     /* R248 - FLL NCO Test 1 */
338a91eb199SMark Brown };
339a91eb199SMark Brown 
340a91eb199SMark Brown static struct {
341a91eb199SMark Brown 	int readable;
342a91eb199SMark Brown 	int writable;
343a91eb199SMark Brown 	int vol;
344a91eb199SMark Brown } wm8904_access[] = {
345a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 1 }, /* R0   - SW Reset and ID */
346a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R1   - Revision */
347a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R2 */
348a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R3 */
349a91eb199SMark Brown 	{ 0x001F, 0x001F, 0 }, /* R4   - Bias Control 0 */
350a91eb199SMark Brown 	{ 0x0047, 0x0047, 0 }, /* R5   - VMID Control 0 */
351a91eb199SMark Brown 	{ 0x007F, 0x007F, 0 }, /* R6   - Mic Bias Control 0 */
352a91eb199SMark Brown 	{ 0xC007, 0xC007, 0 }, /* R7   - Mic Bias Control 1 */
353a91eb199SMark Brown 	{ 0x001E, 0x001E, 0 }, /* R8   - Analogue DAC 0 */
354a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 0 }, /* R9   - mic Filter Control */
355a91eb199SMark Brown 	{ 0x0001, 0x0001, 0 }, /* R10  - Analogue ADC 0 */
356a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R11 */
357a91eb199SMark Brown 	{ 0x0003, 0x0003, 0 }, /* R12  - Power Management 0 */
358a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R13 */
359a91eb199SMark Brown 	{ 0x0003, 0x0003, 0 }, /* R14  - Power Management 2 */
360a91eb199SMark Brown 	{ 0x0003, 0x0003, 0 }, /* R15  - Power Management 3 */
361a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R16 */
362a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R17 */
363a91eb199SMark Brown 	{ 0x000F, 0x000F, 0 }, /* R18  - Power Management 6 */
364a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R19 */
365a91eb199SMark Brown 	{ 0x7001, 0x7001, 0 }, /* R20  - Clock Rates 0 */
366a91eb199SMark Brown 	{ 0x3C07, 0x3C07, 0 }, /* R21  - Clock Rates 1 */
367a91eb199SMark Brown 	{ 0xD00F, 0xD00F, 0 }, /* R22  - Clock Rates 2 */
368a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R23 */
369a91eb199SMark Brown 	{ 0x1FFF, 0x1FFF, 0 }, /* R24  - Audio Interface 0 */
370a91eb199SMark Brown 	{ 0x3DDF, 0x3DDF, 0 }, /* R25  - Audio Interface 1 */
371a91eb199SMark Brown 	{ 0x0F1F, 0x0F1F, 0 }, /* R26  - Audio Interface 2 */
372a91eb199SMark Brown 	{ 0x0FFF, 0x0FFF, 0 }, /* R27  - Audio Interface 3 */
373a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R28 */
374a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R29 */
375a91eb199SMark Brown 	{ 0x00FF, 0x01FF, 0 }, /* R30  - DAC Digital Volume Left */
376a91eb199SMark Brown 	{ 0x00FF, 0x01FF, 0 }, /* R31  - DAC Digital Volume Right */
377a91eb199SMark Brown 	{ 0x0FFF, 0x0FFF, 0 }, /* R32  - DAC Digital 0 */
378a91eb199SMark Brown 	{ 0x1E4E, 0x1E4E, 0 }, /* R33  - DAC Digital 1 */
379a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R34 */
380a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R35 */
381a91eb199SMark Brown 	{ 0x00FF, 0x01FF, 0 }, /* R36  - ADC Digital Volume Left */
382a91eb199SMark Brown 	{ 0x00FF, 0x01FF, 0 }, /* R37  - ADC Digital Volume Right */
383a91eb199SMark Brown 	{ 0x0073, 0x0073, 0 }, /* R38  - ADC Digital 0 */
384a91eb199SMark Brown 	{ 0x1800, 0x1800, 0 }, /* R39  - Digital Microphone 0 */
385a91eb199SMark Brown 	{ 0xDFEF, 0xDFEF, 0 }, /* R40  - DRC 0 */
386a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 0 }, /* R41  - DRC 1 */
387a91eb199SMark Brown 	{ 0x003F, 0x003F, 0 }, /* R42  - DRC 2 */
388a91eb199SMark Brown 	{ 0x07FF, 0x07FF, 0 }, /* R43  - DRC 3 */
389a91eb199SMark Brown 	{ 0x009F, 0x009F, 0 }, /* R44  - Analogue Left Input 0 */
390a91eb199SMark Brown 	{ 0x009F, 0x009F, 0 }, /* R45  - Analogue Right Input 0 */
391a91eb199SMark Brown 	{ 0x007F, 0x007F, 0 }, /* R46  - Analogue Left Input 1 */
392a91eb199SMark Brown 	{ 0x007F, 0x007F, 0 }, /* R47  - Analogue Right Input 1 */
393a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R48 */
394a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R49 */
395a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R50 */
396a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R51 */
397a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R52 */
398a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R53 */
399a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R54 */
400a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R55 */
401a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R56 */
402a91eb199SMark Brown 	{ 0x017F, 0x01FF, 0 }, /* R57  - Analogue OUT1 Left */
403a91eb199SMark Brown 	{ 0x017F, 0x01FF, 0 }, /* R58  - Analogue OUT1 Right */
404a91eb199SMark Brown 	{ 0x017F, 0x01FF, 0 }, /* R59  - Analogue OUT2 Left */
405a91eb199SMark Brown 	{ 0x017F, 0x01FF, 0 }, /* R60  - Analogue OUT2 Right */
406a91eb199SMark Brown 	{ 0x000F, 0x000F, 0 }, /* R61  - Analogue OUT12 ZC */
407a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R62 */
408a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R63 */
409a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R64 */
410a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R65 */
411a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R66 */
412a91eb199SMark Brown 	{ 0x000F, 0x000F, 0 }, /* R67  - DC Servo 0 */
413a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 1 }, /* R68  - DC Servo 1 */
414a91eb199SMark Brown 	{ 0x0F0F, 0x0F0F, 0 }, /* R69  - DC Servo 2 */
415a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R70 */
416a91eb199SMark Brown 	{ 0x007F, 0x007F, 0 }, /* R71  - DC Servo 4 */
417a91eb199SMark Brown 	{ 0x007F, 0x007F, 0 }, /* R72  - DC Servo 5 */
418a91eb199SMark Brown 	{ 0x00FF, 0x00FF, 1 }, /* R73  - DC Servo 6 */
419a91eb199SMark Brown 	{ 0x00FF, 0x00FF, 1 }, /* R74  - DC Servo 7 */
420a91eb199SMark Brown 	{ 0x00FF, 0x00FF, 1 }, /* R75  - DC Servo 8 */
421a91eb199SMark Brown 	{ 0x00FF, 0x00FF, 1 }, /* R76  - DC Servo 9 */
422a91eb199SMark Brown 	{ 0x0FFF, 0x0000, 1 }, /* R77  - DC Servo Readback 0 */
423a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R78 */
424a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R79 */
425a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R80 */
426a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R81 */
427a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R82 */
428a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R83 */
429a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R84 */
430a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R85 */
431a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R86 */
432a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R87 */
433a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R88 */
434a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R89 */
435a91eb199SMark Brown 	{ 0x00FF, 0x00FF, 0 }, /* R90  - Analogue HP 0 */
436a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R91 */
437a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R92 */
438a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R93 */
439a91eb199SMark Brown 	{ 0x00FF, 0x00FF, 0 }, /* R94  - Analogue Lineout 0 */
440a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R95 */
441a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R96 */
442a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R97 */
443a91eb199SMark Brown 	{ 0x0001, 0x0001, 0 }, /* R98  - Charge Pump 0 */
444a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R99 */
445a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R100 */
446a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R101 */
447a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R102 */
448a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R103 */
449a91eb199SMark Brown 	{ 0x0001, 0x0001, 0 }, /* R104 - Class W 0 */
450a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R105 */
451a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R106 */
452a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R107 */
453a91eb199SMark Brown 	{ 0x011F, 0x011F, 0 }, /* R108 - Write Sequencer 0 */
454a91eb199SMark Brown 	{ 0x7FFF, 0x7FFF, 0 }, /* R109 - Write Sequencer 1 */
455a91eb199SMark Brown 	{ 0x4FFF, 0x4FFF, 0 }, /* R110 - Write Sequencer 2 */
456a91eb199SMark Brown 	{ 0x003F, 0x033F, 0 }, /* R111 - Write Sequencer 3 */
457a91eb199SMark Brown 	{ 0x03F1, 0x0000, 0 }, /* R112 - Write Sequencer 4 */
458a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R113 */
459a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R114 */
460a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R115 */
461a91eb199SMark Brown 	{ 0x0007, 0x0007, 0 }, /* R116 - FLL Control 1 */
462a91eb199SMark Brown 	{ 0x3F77, 0x3F77, 0 }, /* R117 - FLL Control 2 */
463a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 0 }, /* R118 - FLL Control 3 */
464a91eb199SMark Brown 	{ 0x7FEF, 0x7FEF, 0 }, /* R119 - FLL Control 4 */
465a91eb199SMark Brown 	{ 0x001B, 0x001B, 0 }, /* R120 - FLL Control 5 */
466a91eb199SMark Brown 	{ 0x003F, 0x003F, 0 }, /* R121 - GPIO Control 1 */
467a91eb199SMark Brown 	{ 0x003F, 0x003F, 0 }, /* R122 - GPIO Control 2 */
468a91eb199SMark Brown 	{ 0x003F, 0x003F, 0 }, /* R123 - GPIO Control 3 */
469a91eb199SMark Brown 	{ 0x038F, 0x038F, 0 }, /* R124 - GPIO Control 4 */
470a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R125 */
471a91eb199SMark Brown 	{ 0x00FF, 0x00FF, 0 }, /* R126 - Digital Pulls */
472a91eb199SMark Brown 	{ 0x07FF, 0x03FF, 1 }, /* R127 - Interrupt Status */
473a91eb199SMark Brown 	{ 0x03FF, 0x03FF, 0 }, /* R128 - Interrupt Status Mask */
474a91eb199SMark Brown 	{ 0x03FF, 0x03FF, 0 }, /* R129 - Interrupt Polarity */
475a91eb199SMark Brown 	{ 0x03FF, 0x03FF, 0 }, /* R130 - Interrupt Debounce */
476a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R131 */
477a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R132 */
478a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R133 */
479a91eb199SMark Brown 	{ 0x0001, 0x0001, 0 }, /* R134 - EQ1 */
480a91eb199SMark Brown 	{ 0x001F, 0x001F, 0 }, /* R135 - EQ2 */
481a91eb199SMark Brown 	{ 0x001F, 0x001F, 0 }, /* R136 - EQ3 */
482a91eb199SMark Brown 	{ 0x001F, 0x001F, 0 }, /* R137 - EQ4 */
483a91eb199SMark Brown 	{ 0x001F, 0x001F, 0 }, /* R138 - EQ5 */
484a91eb199SMark Brown 	{ 0x001F, 0x001F, 0 }, /* R139 - EQ6 */
485a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 0 }, /* R140 - EQ7 */
486a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 0 }, /* R141 - EQ8 */
487a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 0 }, /* R142 - EQ9 */
488a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 0 }, /* R143 - EQ10 */
489a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 0 }, /* R144 - EQ11 */
490a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 0 }, /* R145 - EQ12 */
491a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 0 }, /* R146 - EQ13 */
492a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 0 }, /* R147 - EQ14 */
493a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 0 }, /* R148 - EQ15 */
494a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 0 }, /* R149 - EQ16 */
495a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 0 }, /* R150 - EQ17 */
496a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 0 }, /* R151wm8523_dai - EQ18 */
497a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 0 }, /* R152 - EQ19 */
498a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 0 }, /* R153 - EQ20 */
499a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 0 }, /* R154 - EQ21 */
500a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 0 }, /* R155 - EQ22 */
501a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 0 }, /* R156 - EQ23 */
502a91eb199SMark Brown 	{ 0xFFFF, 0xFFFF, 0 }, /* R157 - EQ24 */
503a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R158 */
504a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R159 */
505a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R160 */
506a91eb199SMark Brown 	{ 0x0002, 0x0002, 0 }, /* R161 - Control Interface Test 1 */
507a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R162 */
508a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R163 */
509a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R164 */
510a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R165 */
511a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R166 */
512a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R167 */
513a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R168 */
514a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R169 */
515a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R170 */
516a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R171 */
517a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R172 */
518a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R173 */
519a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R174 */
520a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R175 */
521a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R176 */
522a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R177 */
523a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R178 */
524a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R179 */
525a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R180 */
526a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R181 */
527a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R182 */
528a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R183 */
529a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R184 */
530a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R185 */
531a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R186 */
532a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R187 */
533a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R188 */
534a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R189 */
535a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R190 */
536a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R191 */
537a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R192 */
538a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R193 */
539a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R194 */
540a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R195 */
541a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R196 */
542a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R197 */
543a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R198 */
544a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R199 */
545a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R200 */
546a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R201 */
547a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R202 */
548a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R203 */
549a91eb199SMark Brown 	{ 0x0070, 0x0070, 0 }, /* R204 - Analogue Output Bias 0 */
550a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R205 */
551a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R206 */
552a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R207 */
553a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R208 */
554a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R209 */
555a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R210 */
556a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R211 */
557a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R212 */
558a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R213 */
559a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R214 */
560a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R215 */
561a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R216 */
562a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R217 */
563a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R218 */
564a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R219 */
565a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R220 */
566a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R221 */
567a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R222 */
568a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R223 */
569a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R224 */
570a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R225 */
571a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R226 */
572a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R227 */
573a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R228 */
574a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R229 */
575a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R230 */
576a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R231 */
577a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R232 */
578a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R233 */
579a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R234 */
580a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R235 */
581a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R236 */
582a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R237 */
583a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R238 */
584a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R239 */
585a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R240 */
586a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R241 */
587a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R242 */
588a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R243 */
589a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R244 */
590a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R245 */
591a91eb199SMark Brown 	{ 0x0000, 0x0000, 0 }, /* R246 */
592a91eb199SMark Brown 	{ 0x0001, 0x0001, 0 }, /* R247 - FLL NCO Test 0 */
593a91eb199SMark Brown 	{ 0x003F, 0x003F, 0 }, /* R248 - FLL NCO Test 1 */
594a91eb199SMark Brown };
595a91eb199SMark Brown 
596a91eb199SMark Brown static int wm8904_volatile_register(unsigned int reg)
597a91eb199SMark Brown {
598a91eb199SMark Brown 	return wm8904_access[reg].vol;
599a91eb199SMark Brown }
600a91eb199SMark Brown 
601a91eb199SMark Brown static int wm8904_reset(struct snd_soc_codec *codec)
602a91eb199SMark Brown {
603a91eb199SMark Brown 	return snd_soc_write(codec, WM8904_SW_RESET_AND_ID, 0);
604a91eb199SMark Brown }
605a91eb199SMark Brown 
606a91eb199SMark Brown static int wm8904_configure_clocking(struct snd_soc_codec *codec)
607a91eb199SMark Brown {
608a91eb199SMark Brown 	struct wm8904_priv *wm8904 = codec->private_data;
609a91eb199SMark Brown 	unsigned int clock0, clock2, rate;
610a91eb199SMark Brown 
611a91eb199SMark Brown 	/* Gate the clock while we're updating to avoid misclocking */
612a91eb199SMark Brown 	clock2 = snd_soc_read(codec, WM8904_CLOCK_RATES_2);
613a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
614a91eb199SMark Brown 			    WM8904_SYSCLK_SRC, 0);
615a91eb199SMark Brown 
616a91eb199SMark Brown 	/* This should be done on init() for bypass paths */
617a91eb199SMark Brown 	switch (wm8904->sysclk_src) {
618a91eb199SMark Brown 	case WM8904_CLK_MCLK:
619a91eb199SMark Brown 		dev_dbg(codec->dev, "Using %dHz MCLK\n", wm8904->mclk_rate);
620a91eb199SMark Brown 
621a91eb199SMark Brown 		clock2 &= ~WM8904_SYSCLK_SRC;
622a91eb199SMark Brown 		rate = wm8904->mclk_rate;
623a91eb199SMark Brown 
624a91eb199SMark Brown 		/* Ensure the FLL is stopped */
625a91eb199SMark Brown 		snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
626a91eb199SMark Brown 				    WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
627a91eb199SMark Brown 		break;
628a91eb199SMark Brown 
629a91eb199SMark Brown 	case WM8904_CLK_FLL:
630a91eb199SMark Brown 		dev_dbg(codec->dev, "Using %dHz FLL clock\n",
631a91eb199SMark Brown 			wm8904->fll_fout);
632a91eb199SMark Brown 
633a91eb199SMark Brown 		clock2 |= WM8904_SYSCLK_SRC;
634a91eb199SMark Brown 		rate = wm8904->fll_fout;
635a91eb199SMark Brown 		break;
636a91eb199SMark Brown 
637a91eb199SMark Brown 	default:
638a91eb199SMark Brown 		dev_err(codec->dev, "System clock not configured\n");
639a91eb199SMark Brown 		return -EINVAL;
640a91eb199SMark Brown 	}
641a91eb199SMark Brown 
642a91eb199SMark Brown 	/* SYSCLK shouldn't be over 13.5MHz */
643a91eb199SMark Brown 	if (rate > 13500000) {
644a91eb199SMark Brown 		clock0 = WM8904_MCLK_DIV;
645a91eb199SMark Brown 		wm8904->sysclk_rate = rate / 2;
646a91eb199SMark Brown 	} else {
647a91eb199SMark Brown 		clock0 = 0;
648a91eb199SMark Brown 		wm8904->sysclk_rate = rate;
649a91eb199SMark Brown 	}
650a91eb199SMark Brown 
651a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_CLOCK_RATES_0, WM8904_MCLK_DIV,
652a91eb199SMark Brown 			    clock0);
653a91eb199SMark Brown 
654a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
655a91eb199SMark Brown 			    WM8904_CLK_SYS_ENA | WM8904_SYSCLK_SRC, clock2);
656a91eb199SMark Brown 
657a91eb199SMark Brown 	dev_dbg(codec->dev, "CLK_SYS is %dHz\n", wm8904->sysclk_rate);
658a91eb199SMark Brown 
659a91eb199SMark Brown 	return 0;
660a91eb199SMark Brown }
661a91eb199SMark Brown 
662a91eb199SMark Brown static void wm8904_set_drc(struct snd_soc_codec *codec)
663a91eb199SMark Brown {
664a91eb199SMark Brown 	struct wm8904_priv *wm8904 = codec->private_data;
665a91eb199SMark Brown 	struct wm8904_pdata *pdata = wm8904->pdata;
666a91eb199SMark Brown 	int save, i;
667a91eb199SMark Brown 
668a91eb199SMark Brown 	/* Save any enables; the configuration should clear them. */
669a91eb199SMark Brown 	save = snd_soc_read(codec, WM8904_DRC_0);
670a91eb199SMark Brown 
671a91eb199SMark Brown 	for (i = 0; i < WM8904_DRC_REGS; i++)
672a91eb199SMark Brown 		snd_soc_update_bits(codec, WM8904_DRC_0 + i, 0xffff,
673a91eb199SMark Brown 				    pdata->drc_cfgs[wm8904->drc_cfg].regs[i]);
674a91eb199SMark Brown 
675a91eb199SMark Brown 	/* Reenable the DRC */
676a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_DRC_0,
677a91eb199SMark Brown 			    WM8904_DRC_ENA | WM8904_DRC_DAC_PATH, save);
678a91eb199SMark Brown }
679a91eb199SMark Brown 
680a91eb199SMark Brown static int wm8904_put_drc_enum(struct snd_kcontrol *kcontrol,
681a91eb199SMark Brown 			       struct snd_ctl_elem_value *ucontrol)
682a91eb199SMark Brown {
683a91eb199SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
684a91eb199SMark Brown 	struct wm8904_priv *wm8904 = codec->private_data;
685a91eb199SMark Brown 	struct wm8904_pdata *pdata = wm8904->pdata;
686a91eb199SMark Brown 	int value = ucontrol->value.integer.value[0];
687a91eb199SMark Brown 
688a91eb199SMark Brown 	if (value >= pdata->num_drc_cfgs)
689a91eb199SMark Brown 		return -EINVAL;
690a91eb199SMark Brown 
691a91eb199SMark Brown 	wm8904->drc_cfg = value;
692a91eb199SMark Brown 
693a91eb199SMark Brown 	wm8904_set_drc(codec);
694a91eb199SMark Brown 
695a91eb199SMark Brown 	return 0;
696a91eb199SMark Brown }
697a91eb199SMark Brown 
698a91eb199SMark Brown static int wm8904_get_drc_enum(struct snd_kcontrol *kcontrol,
699a91eb199SMark Brown 			       struct snd_ctl_elem_value *ucontrol)
700a91eb199SMark Brown {
701a91eb199SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
702a91eb199SMark Brown 	struct wm8904_priv *wm8904 = codec->private_data;
703a91eb199SMark Brown 
704a91eb199SMark Brown 	ucontrol->value.enumerated.item[0] = wm8904->drc_cfg;
705a91eb199SMark Brown 
706a91eb199SMark Brown 	return 0;
707a91eb199SMark Brown }
708a91eb199SMark Brown 
709a91eb199SMark Brown static void wm8904_set_retune_mobile(struct snd_soc_codec *codec)
710a91eb199SMark Brown {
711a91eb199SMark Brown 	struct wm8904_priv *wm8904 = codec->private_data;
712a91eb199SMark Brown 	struct wm8904_pdata *pdata = wm8904->pdata;
713a91eb199SMark Brown 	int best, best_val, save, i, cfg;
714a91eb199SMark Brown 
715a91eb199SMark Brown 	if (!pdata || !wm8904->num_retune_mobile_texts)
716a91eb199SMark Brown 		return;
717a91eb199SMark Brown 
718a91eb199SMark Brown 	/* Find the version of the currently selected configuration
719a91eb199SMark Brown 	 * with the nearest sample rate. */
720a91eb199SMark Brown 	cfg = wm8904->retune_mobile_cfg;
721a91eb199SMark Brown 	best = 0;
722a91eb199SMark Brown 	best_val = INT_MAX;
723a91eb199SMark Brown 	for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
724a91eb199SMark Brown 		if (strcmp(pdata->retune_mobile_cfgs[i].name,
725a91eb199SMark Brown 			   wm8904->retune_mobile_texts[cfg]) == 0 &&
726a91eb199SMark Brown 		    abs(pdata->retune_mobile_cfgs[i].rate
727a91eb199SMark Brown 			- wm8904->fs) < best_val) {
728a91eb199SMark Brown 			best = i;
729a91eb199SMark Brown 			best_val = abs(pdata->retune_mobile_cfgs[i].rate
730a91eb199SMark Brown 				       - wm8904->fs);
731a91eb199SMark Brown 		}
732a91eb199SMark Brown 	}
733a91eb199SMark Brown 
734a91eb199SMark Brown 	dev_dbg(codec->dev, "ReTune Mobile %s/%dHz for %dHz sample rate\n",
735a91eb199SMark Brown 		pdata->retune_mobile_cfgs[best].name,
736a91eb199SMark Brown 		pdata->retune_mobile_cfgs[best].rate,
737a91eb199SMark Brown 		wm8904->fs);
738a91eb199SMark Brown 
739a91eb199SMark Brown 	/* The EQ will be disabled while reconfiguring it, remember the
740a91eb199SMark Brown 	 * current configuration.
741a91eb199SMark Brown 	 */
742a91eb199SMark Brown 	save = snd_soc_read(codec, WM8904_EQ1);
743a91eb199SMark Brown 
744a91eb199SMark Brown 	for (i = 0; i < WM8904_EQ_REGS; i++)
745a91eb199SMark Brown 		snd_soc_update_bits(codec, WM8904_EQ1 + i, 0xffff,
746a91eb199SMark Brown 				pdata->retune_mobile_cfgs[best].regs[i]);
747a91eb199SMark Brown 
748a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_EQ1, WM8904_EQ_ENA, save);
749a91eb199SMark Brown }
750a91eb199SMark Brown 
751a91eb199SMark Brown static int wm8904_put_retune_mobile_enum(struct snd_kcontrol *kcontrol,
752a91eb199SMark Brown 					 struct snd_ctl_elem_value *ucontrol)
753a91eb199SMark Brown {
754a91eb199SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
755a91eb199SMark Brown 	struct wm8904_priv *wm8904 = codec->private_data;
756a91eb199SMark Brown 	struct wm8904_pdata *pdata = wm8904->pdata;
757a91eb199SMark Brown 	int value = ucontrol->value.integer.value[0];
758a91eb199SMark Brown 
759a91eb199SMark Brown 	if (value >= pdata->num_retune_mobile_cfgs)
760a91eb199SMark Brown 		return -EINVAL;
761a91eb199SMark Brown 
762a91eb199SMark Brown 	wm8904->retune_mobile_cfg = value;
763a91eb199SMark Brown 
764a91eb199SMark Brown 	wm8904_set_retune_mobile(codec);
765a91eb199SMark Brown 
766a91eb199SMark Brown 	return 0;
767a91eb199SMark Brown }
768a91eb199SMark Brown 
769a91eb199SMark Brown static int wm8904_get_retune_mobile_enum(struct snd_kcontrol *kcontrol,
770a91eb199SMark Brown 					 struct snd_ctl_elem_value *ucontrol)
771a91eb199SMark Brown {
772a91eb199SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
773a91eb199SMark Brown 	struct wm8904_priv *wm8904 = codec->private_data;
774a91eb199SMark Brown 
775a91eb199SMark Brown 	ucontrol->value.enumerated.item[0] = wm8904->retune_mobile_cfg;
776a91eb199SMark Brown 
777a91eb199SMark Brown 	return 0;
778a91eb199SMark Brown }
779a91eb199SMark Brown 
780a91eb199SMark Brown static int deemph_settings[] = { 0, 32000, 44100, 48000 };
781a91eb199SMark Brown 
782a91eb199SMark Brown static int wm8904_set_deemph(struct snd_soc_codec *codec)
783a91eb199SMark Brown {
784a91eb199SMark Brown 	struct wm8904_priv *wm8904 = codec->private_data;
785a91eb199SMark Brown 	int val, i, best;
786a91eb199SMark Brown 
787a91eb199SMark Brown 	/* If we're using deemphasis select the nearest available sample
788a91eb199SMark Brown 	 * rate.
789a91eb199SMark Brown 	 */
790a91eb199SMark Brown 	if (wm8904->deemph) {
791a91eb199SMark Brown 		best = 1;
792a91eb199SMark Brown 		for (i = 2; i < ARRAY_SIZE(deemph_settings); i++) {
793a91eb199SMark Brown 			if (abs(deemph_settings[i] - wm8904->fs) <
794a91eb199SMark Brown 			    abs(deemph_settings[best] - wm8904->fs))
795a91eb199SMark Brown 				best = i;
796a91eb199SMark Brown 		}
797a91eb199SMark Brown 
798a91eb199SMark Brown 		val = best << WM8904_DEEMPH_SHIFT;
799a91eb199SMark Brown 	} else {
800a91eb199SMark Brown 		val = 0;
801a91eb199SMark Brown 	}
802a91eb199SMark Brown 
803a91eb199SMark Brown 	dev_dbg(codec->dev, "Set deemphasis %d\n", val);
804a91eb199SMark Brown 
805a91eb199SMark Brown 	return snd_soc_update_bits(codec, WM8904_DAC_DIGITAL_1,
806a91eb199SMark Brown 				   WM8904_DEEMPH_MASK, val);
807a91eb199SMark Brown }
808a91eb199SMark Brown 
809a91eb199SMark Brown static int wm8904_get_deemph(struct snd_kcontrol *kcontrol,
810a91eb199SMark Brown 			     struct snd_ctl_elem_value *ucontrol)
811a91eb199SMark Brown {
812a91eb199SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
813a91eb199SMark Brown 	struct wm8904_priv *wm8904 = codec->private_data;
814a91eb199SMark Brown 
815a91eb199SMark Brown 	return wm8904->deemph;
816a91eb199SMark Brown }
817a91eb199SMark Brown 
818a91eb199SMark Brown static int wm8904_put_deemph(struct snd_kcontrol *kcontrol,
819a91eb199SMark Brown 			      struct snd_ctl_elem_value *ucontrol)
820a91eb199SMark Brown {
821a91eb199SMark Brown 	struct snd_soc_codec *codec = snd_kcontrol_chip(kcontrol);
822a91eb199SMark Brown 	struct wm8904_priv *wm8904 = codec->private_data;
823a91eb199SMark Brown 	int deemph = ucontrol->value.enumerated.item[0];
824a91eb199SMark Brown 
825a91eb199SMark Brown 	if (deemph > 1)
826a91eb199SMark Brown 		return -EINVAL;
827a91eb199SMark Brown 
828a91eb199SMark Brown 	wm8904->deemph = deemph;
829a91eb199SMark Brown 
830a91eb199SMark Brown 	return wm8904_set_deemph(codec);
831a91eb199SMark Brown }
832a91eb199SMark Brown 
833a91eb199SMark Brown static const DECLARE_TLV_DB_SCALE(dac_boost_tlv, 0, 600, 0);
834a91eb199SMark Brown static const DECLARE_TLV_DB_SCALE(digital_tlv, -7200, 75, 1);
835a91eb199SMark Brown static const DECLARE_TLV_DB_SCALE(out_tlv, -5700, 100, 0);
836a91eb199SMark Brown static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -3600, 300, 0);
837a91eb199SMark Brown static const DECLARE_TLV_DB_SCALE(eq_tlv, -1200, 100, 0);
838a91eb199SMark Brown 
839a91eb199SMark Brown static const char *input_mode_text[] = {
840a91eb199SMark Brown 	"Single-Ended", "Differential Line", "Differential Mic"
841a91eb199SMark Brown };
842a91eb199SMark Brown 
843a91eb199SMark Brown static const struct soc_enum lin_mode =
844a91eb199SMark Brown 	SOC_ENUM_SINGLE(WM8904_ANALOGUE_LEFT_INPUT_1, 0, 3, input_mode_text);
845a91eb199SMark Brown 
846a91eb199SMark Brown static const struct soc_enum rin_mode =
847a91eb199SMark Brown 	SOC_ENUM_SINGLE(WM8904_ANALOGUE_RIGHT_INPUT_1, 0, 3, input_mode_text);
848a91eb199SMark Brown 
849a91eb199SMark Brown static const char *hpf_mode_text[] = {
850a91eb199SMark Brown 	"Hi-fi", "Voice 1", "Voice 2", "Voice 3"
851a91eb199SMark Brown };
852a91eb199SMark Brown 
853a91eb199SMark Brown static const struct soc_enum hpf_mode =
854a91eb199SMark Brown 	SOC_ENUM_SINGLE(WM8904_ADC_DIGITAL_0, 5, 4, hpf_mode_text);
855a91eb199SMark Brown 
856a91eb199SMark Brown static const struct snd_kcontrol_new wm8904_adc_snd_controls[] = {
857a91eb199SMark Brown SOC_DOUBLE_R_TLV("Digital Capture Volume", WM8904_ADC_DIGITAL_VOLUME_LEFT,
858a91eb199SMark Brown 		 WM8904_ADC_DIGITAL_VOLUME_RIGHT, 1, 119, 0, digital_tlv),
859a91eb199SMark Brown 
860a91eb199SMark Brown SOC_ENUM("Left Caputure Mode", lin_mode),
861a91eb199SMark Brown SOC_ENUM("Right Capture Mode", rin_mode),
862a91eb199SMark Brown 
863a91eb199SMark Brown /* No TLV since it depends on mode */
864a91eb199SMark Brown SOC_DOUBLE_R("Capture Volume", WM8904_ANALOGUE_LEFT_INPUT_0,
865a91eb199SMark Brown 	     WM8904_ANALOGUE_RIGHT_INPUT_0, 0, 31, 0),
866a91eb199SMark Brown SOC_DOUBLE_R("Capture Switch", WM8904_ANALOGUE_LEFT_INPUT_0,
867a91eb199SMark Brown 	     WM8904_ANALOGUE_RIGHT_INPUT_0, 7, 1, 0),
868a91eb199SMark Brown 
869a91eb199SMark Brown SOC_SINGLE("High Pass Filter Switch", WM8904_ADC_DIGITAL_0, 4, 1, 0),
870a91eb199SMark Brown SOC_ENUM("High Pass Filter Mode", hpf_mode),
871a91eb199SMark Brown 
872a91eb199SMark Brown SOC_SINGLE("ADC 128x OSR Switch", WM8904_ANALOGUE_ADC_0, 0, 1, 0),
873a91eb199SMark Brown };
874a91eb199SMark Brown 
875a91eb199SMark Brown static const char *drc_path_text[] = {
876a91eb199SMark Brown 	"ADC", "DAC"
877a91eb199SMark Brown };
878a91eb199SMark Brown 
879a91eb199SMark Brown static const struct soc_enum drc_path =
880a91eb199SMark Brown 	SOC_ENUM_SINGLE(WM8904_DRC_0, 14, 2, drc_path_text);
881a91eb199SMark Brown 
882a91eb199SMark Brown static const struct snd_kcontrol_new wm8904_dac_snd_controls[] = {
883a91eb199SMark Brown SOC_SINGLE_TLV("Digital Playback Boost Volume",
884a91eb199SMark Brown 	       WM8904_AUDIO_INTERFACE_0, 9, 3, 0, dac_boost_tlv),
885a91eb199SMark Brown SOC_DOUBLE_R_TLV("Digital Playback Volume", WM8904_DAC_DIGITAL_VOLUME_LEFT,
886a91eb199SMark Brown 		 WM8904_DAC_DIGITAL_VOLUME_RIGHT, 1, 96, 0, digital_tlv),
887a91eb199SMark Brown 
888a91eb199SMark Brown SOC_DOUBLE_R_TLV("Headphone Volume", WM8904_ANALOGUE_OUT1_LEFT,
889a91eb199SMark Brown 		 WM8904_ANALOGUE_OUT1_RIGHT, 0, 63, 0, out_tlv),
890a91eb199SMark Brown SOC_DOUBLE_R("Headphone Switch", WM8904_ANALOGUE_OUT1_LEFT,
891a91eb199SMark Brown 	     WM8904_ANALOGUE_OUT1_RIGHT, 8, 1, 1),
892a91eb199SMark Brown SOC_DOUBLE_R("Headphone ZC Switch", WM8904_ANALOGUE_OUT1_LEFT,
893a91eb199SMark Brown 	     WM8904_ANALOGUE_OUT1_RIGHT, 6, 1, 0),
894a91eb199SMark Brown 
895a91eb199SMark Brown SOC_DOUBLE_R_TLV("Line Output Volume", WM8904_ANALOGUE_OUT2_LEFT,
896a91eb199SMark Brown 		 WM8904_ANALOGUE_OUT2_RIGHT, 0, 63, 0, out_tlv),
897a91eb199SMark Brown SOC_DOUBLE_R("Line Output Switch", WM8904_ANALOGUE_OUT2_LEFT,
898a91eb199SMark Brown 	     WM8904_ANALOGUE_OUT2_RIGHT, 8, 1, 1),
899a91eb199SMark Brown SOC_DOUBLE_R("Line Output ZC Switch", WM8904_ANALOGUE_OUT2_LEFT,
900a91eb199SMark Brown 	     WM8904_ANALOGUE_OUT2_RIGHT, 6, 1, 0),
901a91eb199SMark Brown 
902a91eb199SMark Brown SOC_SINGLE("EQ Switch", WM8904_EQ1, 0, 1, 0),
903a91eb199SMark Brown SOC_SINGLE("DRC Switch", WM8904_DRC_0, 15, 1, 0),
904a91eb199SMark Brown SOC_ENUM("DRC Path", drc_path),
905a91eb199SMark Brown SOC_SINGLE("DAC OSRx2 Switch", WM8904_DAC_DIGITAL_1, 6, 1, 0),
906a91eb199SMark Brown SOC_SINGLE_BOOL_EXT("DAC Deemphasis Switch", 0,
907a91eb199SMark Brown 		    wm8904_get_deemph, wm8904_put_deemph),
908a91eb199SMark Brown };
909a91eb199SMark Brown 
910a91eb199SMark Brown static const struct snd_kcontrol_new wm8904_snd_controls[] = {
911a91eb199SMark Brown SOC_DOUBLE_TLV("Digital Sidetone Volume", WM8904_DAC_DIGITAL_0, 4, 8, 15, 0,
912a91eb199SMark Brown 	       sidetone_tlv),
913a91eb199SMark Brown };
914a91eb199SMark Brown 
915a91eb199SMark Brown static const struct snd_kcontrol_new wm8904_eq_controls[] = {
916a91eb199SMark Brown SOC_SINGLE_TLV("EQ1 Volume", WM8904_EQ2, 0, 24, 0, eq_tlv),
917a91eb199SMark Brown SOC_SINGLE_TLV("EQ2 Volume", WM8904_EQ3, 0, 24, 0, eq_tlv),
918a91eb199SMark Brown SOC_SINGLE_TLV("EQ3 Volume", WM8904_EQ4, 0, 24, 0, eq_tlv),
919a91eb199SMark Brown SOC_SINGLE_TLV("EQ4 Volume", WM8904_EQ5, 0, 24, 0, eq_tlv),
920a91eb199SMark Brown SOC_SINGLE_TLV("EQ5 Volume", WM8904_EQ6, 0, 24, 0, eq_tlv),
921a91eb199SMark Brown };
922a91eb199SMark Brown 
923a91eb199SMark Brown static int cp_event(struct snd_soc_dapm_widget *w,
924a91eb199SMark Brown 		    struct snd_kcontrol *kcontrol, int event)
925a91eb199SMark Brown {
926a91eb199SMark Brown 	BUG_ON(event != SND_SOC_DAPM_POST_PMU);
927a91eb199SMark Brown 
928a91eb199SMark Brown 	/* Maximum startup time */
929a91eb199SMark Brown 	udelay(500);
930a91eb199SMark Brown 
931a91eb199SMark Brown 	return 0;
932a91eb199SMark Brown }
933a91eb199SMark Brown 
934a91eb199SMark Brown static int sysclk_event(struct snd_soc_dapm_widget *w,
935a91eb199SMark Brown 			 struct snd_kcontrol *kcontrol, int event)
936a91eb199SMark Brown {
937a91eb199SMark Brown 	struct snd_soc_codec *codec = w->codec;
938a91eb199SMark Brown 	struct wm8904_priv *wm8904 = codec->private_data;
939a91eb199SMark Brown 
940a91eb199SMark Brown 	switch (event) {
941a91eb199SMark Brown 	case SND_SOC_DAPM_PRE_PMU:
942a91eb199SMark Brown 		/* If we're using the FLL then we only start it when
943a91eb199SMark Brown 		 * required; we assume that the configuration has been
944a91eb199SMark Brown 		 * done previously and all we need to do is kick it
945a91eb199SMark Brown 		 * off.
946a91eb199SMark Brown 		 */
947a91eb199SMark Brown 		switch (wm8904->sysclk_src) {
948a91eb199SMark Brown 		case WM8904_CLK_FLL:
949a91eb199SMark Brown 			snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
950a91eb199SMark Brown 					    WM8904_FLL_OSC_ENA,
951a91eb199SMark Brown 					    WM8904_FLL_OSC_ENA);
952a91eb199SMark Brown 
953a91eb199SMark Brown 			snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
954a91eb199SMark Brown 					    WM8904_FLL_ENA,
955a91eb199SMark Brown 					    WM8904_FLL_ENA);
956a91eb199SMark Brown 			break;
957a91eb199SMark Brown 
958a91eb199SMark Brown 		default:
959a91eb199SMark Brown 			break;
960a91eb199SMark Brown 		}
961a91eb199SMark Brown 		break;
962a91eb199SMark Brown 
963a91eb199SMark Brown 	case SND_SOC_DAPM_POST_PMD:
964a91eb199SMark Brown 		snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
965a91eb199SMark Brown 				    WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
966a91eb199SMark Brown 		break;
967a91eb199SMark Brown 	}
968a91eb199SMark Brown 
969a91eb199SMark Brown 	return 0;
970a91eb199SMark Brown }
971a91eb199SMark Brown 
972a91eb199SMark Brown static int out_pga_event(struct snd_soc_dapm_widget *w,
973a91eb199SMark Brown 			 struct snd_kcontrol *kcontrol, int event)
974a91eb199SMark Brown {
975a91eb199SMark Brown 	struct snd_soc_codec *codec = w->codec;
976a91eb199SMark Brown 	struct wm8904_priv *wm8904 = codec->private_data;
977a91eb199SMark Brown 	int reg, val;
978a91eb199SMark Brown 	int dcs_mask;
979a91eb199SMark Brown 	int dcs_l, dcs_r;
980a91eb199SMark Brown 	int dcs_l_reg, dcs_r_reg;
981a91eb199SMark Brown 	int timeout;
982a91eb199SMark Brown 
983a91eb199SMark Brown 	/* This code is shared between HP and LINEOUT; we do all our
984a91eb199SMark Brown 	 * power management in stereo pairs to avoid latency issues so
985a91eb199SMark Brown 	 * we reuse shift to identify which rather than strcmp() the
986a91eb199SMark Brown 	 * name. */
987a91eb199SMark Brown 	reg = w->shift;
988a91eb199SMark Brown 
989a91eb199SMark Brown 	switch (reg) {
990a91eb199SMark Brown 	case WM8904_ANALOGUE_HP_0:
991a91eb199SMark Brown 		dcs_mask = WM8904_DCS_ENA_CHAN_0 | WM8904_DCS_ENA_CHAN_1;
992a91eb199SMark Brown 		dcs_r_reg = WM8904_DC_SERVO_8;
993a91eb199SMark Brown 		dcs_l_reg = WM8904_DC_SERVO_9;
994a91eb199SMark Brown 		dcs_l = 0;
995a91eb199SMark Brown 		dcs_r = 1;
996a91eb199SMark Brown 		break;
997a91eb199SMark Brown 	case WM8904_ANALOGUE_LINEOUT_0:
998a91eb199SMark Brown 		dcs_mask = WM8904_DCS_ENA_CHAN_2 | WM8904_DCS_ENA_CHAN_3;
999a91eb199SMark Brown 		dcs_r_reg = WM8904_DC_SERVO_6;
1000a91eb199SMark Brown 		dcs_l_reg = WM8904_DC_SERVO_7;
1001a91eb199SMark Brown 		dcs_l = 2;
1002a91eb199SMark Brown 		dcs_r = 3;
1003a91eb199SMark Brown 		break;
1004a91eb199SMark Brown 	default:
1005a91eb199SMark Brown 		BUG();
1006a91eb199SMark Brown 		return -EINVAL;
1007a91eb199SMark Brown 	}
1008a91eb199SMark Brown 
1009a91eb199SMark Brown 	switch (event) {
1010a91eb199SMark Brown 	case SND_SOC_DAPM_POST_PMU:
1011a91eb199SMark Brown 		/* Power on the amplifier */
1012a91eb199SMark Brown 		snd_soc_update_bits(codec, reg,
1013a91eb199SMark Brown 				    WM8904_HPL_ENA | WM8904_HPR_ENA,
1014a91eb199SMark Brown 				    WM8904_HPL_ENA | WM8904_HPR_ENA);
1015a91eb199SMark Brown 
1016a91eb199SMark Brown 		/* Enable the first stage */
1017a91eb199SMark Brown 		snd_soc_update_bits(codec, reg,
1018a91eb199SMark Brown 				    WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY,
1019a91eb199SMark Brown 				    WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY);
1020a91eb199SMark Brown 
1021a91eb199SMark Brown 		/* Power up the DC servo */
1022a91eb199SMark Brown 		snd_soc_update_bits(codec, WM8904_DC_SERVO_0,
1023a91eb199SMark Brown 				    dcs_mask, dcs_mask);
1024a91eb199SMark Brown 
1025a91eb199SMark Brown 		/* Either calibrate the DC servo or restore cached state
1026a91eb199SMark Brown 		 * if we have that.
1027a91eb199SMark Brown 		 */
1028a91eb199SMark Brown 		if (wm8904->dcs_state[dcs_l] || wm8904->dcs_state[dcs_r]) {
1029a91eb199SMark Brown 			dev_dbg(codec->dev, "Restoring DC servo state\n");
1030a91eb199SMark Brown 
1031a91eb199SMark Brown 			snd_soc_write(codec, dcs_l_reg,
1032a91eb199SMark Brown 				      wm8904->dcs_state[dcs_l]);
1033a91eb199SMark Brown 			snd_soc_write(codec, dcs_r_reg,
1034a91eb199SMark Brown 				      wm8904->dcs_state[dcs_r]);
1035a91eb199SMark Brown 
1036a91eb199SMark Brown 			snd_soc_write(codec, WM8904_DC_SERVO_1, dcs_mask);
1037a91eb199SMark Brown 
1038a91eb199SMark Brown 			timeout = 20;
1039a91eb199SMark Brown 		} else {
1040a91eb199SMark Brown 			dev_dbg(codec->dev, "Calibrating DC servo\n");
1041a91eb199SMark Brown 
1042a91eb199SMark Brown 			snd_soc_write(codec, WM8904_DC_SERVO_1,
1043a91eb199SMark Brown 				dcs_mask << WM8904_DCS_TRIG_STARTUP_0_SHIFT);
1044a91eb199SMark Brown 
1045a91eb199SMark Brown 			timeout = 500;
1046a91eb199SMark Brown 		}
1047a91eb199SMark Brown 
1048a91eb199SMark Brown 		/* Wait for DC servo to complete */
1049a91eb199SMark Brown 		dcs_mask <<= WM8904_DCS_CAL_COMPLETE_SHIFT;
1050a91eb199SMark Brown 		do {
1051a91eb199SMark Brown 			val = snd_soc_read(codec, WM8904_DC_SERVO_READBACK_0);
1052a91eb199SMark Brown 			if ((val & dcs_mask) == dcs_mask)
1053a91eb199SMark Brown 				break;
1054a91eb199SMark Brown 
1055a91eb199SMark Brown 			msleep(1);
1056a91eb199SMark Brown 		} while (--timeout);
1057a91eb199SMark Brown 
1058a91eb199SMark Brown 		if ((val & dcs_mask) != dcs_mask)
1059a91eb199SMark Brown 			dev_warn(codec->dev, "DC servo timed out\n");
1060a91eb199SMark Brown 		else
1061a91eb199SMark Brown 			dev_dbg(codec->dev, "DC servo ready\n");
1062a91eb199SMark Brown 
1063a91eb199SMark Brown 		/* Enable the output stage */
1064a91eb199SMark Brown 		snd_soc_update_bits(codec, reg,
1065a91eb199SMark Brown 				    WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP,
1066a91eb199SMark Brown 				    WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP);
1067a91eb199SMark Brown 
1068a91eb199SMark Brown 		/* Unshort the output itself */
1069a91eb199SMark Brown 		snd_soc_update_bits(codec, reg,
1070a91eb199SMark Brown 				    WM8904_HPL_RMV_SHORT |
1071a91eb199SMark Brown 				    WM8904_HPR_RMV_SHORT,
1072a91eb199SMark Brown 				    WM8904_HPL_RMV_SHORT |
1073a91eb199SMark Brown 				    WM8904_HPR_RMV_SHORT);
1074a91eb199SMark Brown 
1075a91eb199SMark Brown 		break;
1076a91eb199SMark Brown 
1077a91eb199SMark Brown 	case SND_SOC_DAPM_PRE_PMD:
1078a91eb199SMark Brown 		/* Short the output */
1079a91eb199SMark Brown 		snd_soc_update_bits(codec, reg,
1080a91eb199SMark Brown 				    WM8904_HPL_RMV_SHORT |
1081a91eb199SMark Brown 				    WM8904_HPR_RMV_SHORT, 0);
1082a91eb199SMark Brown 
1083a91eb199SMark Brown 		/* Cache the DC servo configuration; this will be
1084a91eb199SMark Brown 		 * invalidated if we change the configuration. */
1085a91eb199SMark Brown 		wm8904->dcs_state[dcs_l] = snd_soc_read(codec, dcs_l_reg);
1086a91eb199SMark Brown 		wm8904->dcs_state[dcs_r] = snd_soc_read(codec, dcs_r_reg);
1087a91eb199SMark Brown 
1088a91eb199SMark Brown 		snd_soc_update_bits(codec, WM8904_DC_SERVO_0,
1089a91eb199SMark Brown 				    dcs_mask, 0);
1090a91eb199SMark Brown 
1091a91eb199SMark Brown 		/* Disable the amplifier input and output stages */
1092a91eb199SMark Brown 		snd_soc_update_bits(codec, reg,
1093a91eb199SMark Brown 				    WM8904_HPL_ENA | WM8904_HPR_ENA |
1094a91eb199SMark Brown 				    WM8904_HPL_ENA_DLY | WM8904_HPR_ENA_DLY |
1095a91eb199SMark Brown 				    WM8904_HPL_ENA_OUTP | WM8904_HPR_ENA_OUTP,
1096a91eb199SMark Brown 				    0);
1097a91eb199SMark Brown 		break;
1098a91eb199SMark Brown 	}
1099a91eb199SMark Brown 
1100a91eb199SMark Brown 	return 0;
1101a91eb199SMark Brown }
1102a91eb199SMark Brown 
1103a91eb199SMark Brown static const char *lin_text[] = {
1104a91eb199SMark Brown 	"IN1L", "IN2L", "IN3L"
1105a91eb199SMark Brown };
1106a91eb199SMark Brown 
1107a91eb199SMark Brown static const struct soc_enum lin_enum =
1108a91eb199SMark Brown 	SOC_ENUM_SINGLE(WM8904_ANALOGUE_LEFT_INPUT_1, 2, 3, lin_text);
1109a91eb199SMark Brown 
1110a91eb199SMark Brown static const struct snd_kcontrol_new lin_mux =
1111a91eb199SMark Brown 	SOC_DAPM_ENUM("Left Capture Mux", lin_enum);
1112a91eb199SMark Brown 
1113a91eb199SMark Brown static const struct soc_enum lin_inv_enum =
1114a91eb199SMark Brown 	SOC_ENUM_SINGLE(WM8904_ANALOGUE_LEFT_INPUT_1, 4, 3, lin_text);
1115a91eb199SMark Brown 
1116a91eb199SMark Brown static const struct snd_kcontrol_new lin_inv_mux =
1117a91eb199SMark Brown 	SOC_DAPM_ENUM("Left Capture Inveting Mux", lin_inv_enum);
1118a91eb199SMark Brown 
1119a91eb199SMark Brown static const char *rin_text[] = {
1120a91eb199SMark Brown 	"IN1R", "IN2R", "IN3R"
1121a91eb199SMark Brown };
1122a91eb199SMark Brown 
1123a91eb199SMark Brown static const struct soc_enum rin_enum =
1124a91eb199SMark Brown 	SOC_ENUM_SINGLE(WM8904_ANALOGUE_RIGHT_INPUT_1, 2, 3, rin_text);
1125a91eb199SMark Brown 
1126a91eb199SMark Brown static const struct snd_kcontrol_new rin_mux =
1127a91eb199SMark Brown 	SOC_DAPM_ENUM("Right Capture Mux", rin_enum);
1128a91eb199SMark Brown 
1129a91eb199SMark Brown static const struct soc_enum rin_inv_enum =
1130a91eb199SMark Brown 	SOC_ENUM_SINGLE(WM8904_ANALOGUE_RIGHT_INPUT_1, 4, 3, rin_text);
1131a91eb199SMark Brown 
1132a91eb199SMark Brown static const struct snd_kcontrol_new rin_inv_mux =
1133a91eb199SMark Brown 	SOC_DAPM_ENUM("Right Capture Inveting Mux", rin_inv_enum);
1134a91eb199SMark Brown 
1135a91eb199SMark Brown static const char *aif_text[] = {
1136a91eb199SMark Brown 	"Left", "Right"
1137a91eb199SMark Brown };
1138a91eb199SMark Brown 
1139a91eb199SMark Brown static const struct soc_enum aifoutl_enum =
1140a91eb199SMark Brown 	SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 7, 2, aif_text);
1141a91eb199SMark Brown 
1142a91eb199SMark Brown static const struct snd_kcontrol_new aifoutl_mux =
1143a91eb199SMark Brown 	SOC_DAPM_ENUM("AIFOUTL Mux", aifoutl_enum);
1144a91eb199SMark Brown 
1145a91eb199SMark Brown static const struct soc_enum aifoutr_enum =
1146a91eb199SMark Brown 	SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 6, 2, aif_text);
1147a91eb199SMark Brown 
1148a91eb199SMark Brown static const struct snd_kcontrol_new aifoutr_mux =
1149a91eb199SMark Brown 	SOC_DAPM_ENUM("AIFOUTR Mux", aifoutr_enum);
1150a91eb199SMark Brown 
1151a91eb199SMark Brown static const struct soc_enum aifinl_enum =
1152a91eb199SMark Brown 	SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 5, 2, aif_text);
1153a91eb199SMark Brown 
1154a91eb199SMark Brown static const struct snd_kcontrol_new aifinl_mux =
1155a91eb199SMark Brown 	SOC_DAPM_ENUM("AIFINL Mux", aifinl_enum);
1156a91eb199SMark Brown 
1157a91eb199SMark Brown static const struct soc_enum aifinr_enum =
1158a91eb199SMark Brown 	SOC_ENUM_SINGLE(WM8904_AUDIO_INTERFACE_0, 4, 2, aif_text);
1159a91eb199SMark Brown 
1160a91eb199SMark Brown static const struct snd_kcontrol_new aifinr_mux =
1161a91eb199SMark Brown 	SOC_DAPM_ENUM("AIFINR Mux", aifinr_enum);
1162a91eb199SMark Brown 
1163a91eb199SMark Brown static const struct snd_soc_dapm_widget wm8904_core_dapm_widgets[] = {
1164a91eb199SMark Brown SND_SOC_DAPM_SUPPLY("SYSCLK", WM8904_CLOCK_RATES_2, 2, 0, sysclk_event,
1165a91eb199SMark Brown 		    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
1166a91eb199SMark Brown SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8904_CLOCK_RATES_2, 1, 0, NULL, 0),
1167a91eb199SMark Brown SND_SOC_DAPM_SUPPLY("TOCLK", WM8904_CLOCK_RATES_2, 0, 0, NULL, 0),
1168a91eb199SMark Brown };
1169a91eb199SMark Brown 
1170a91eb199SMark Brown static const struct snd_soc_dapm_widget wm8904_adc_dapm_widgets[] = {
1171a91eb199SMark Brown SND_SOC_DAPM_INPUT("IN1L"),
1172a91eb199SMark Brown SND_SOC_DAPM_INPUT("IN1R"),
1173a91eb199SMark Brown SND_SOC_DAPM_INPUT("IN2L"),
1174a91eb199SMark Brown SND_SOC_DAPM_INPUT("IN2R"),
1175a91eb199SMark Brown SND_SOC_DAPM_INPUT("IN3L"),
1176a91eb199SMark Brown SND_SOC_DAPM_INPUT("IN3R"),
1177a91eb199SMark Brown 
1178a91eb199SMark Brown SND_SOC_DAPM_MICBIAS("MICBIAS", WM8904_MIC_BIAS_CONTROL_0, 0, 0),
1179a91eb199SMark Brown 
1180a91eb199SMark Brown SND_SOC_DAPM_MUX("Left Capture Mux", SND_SOC_NOPM, 0, 0, &lin_mux),
1181a91eb199SMark Brown SND_SOC_DAPM_MUX("Left Capture Inverting Mux", SND_SOC_NOPM, 0, 0,
1182a91eb199SMark Brown 		 &lin_inv_mux),
1183a91eb199SMark Brown SND_SOC_DAPM_MUX("Right Capture Mux", SND_SOC_NOPM, 0, 0, &rin_mux),
1184a91eb199SMark Brown SND_SOC_DAPM_MUX("Right Capture Inverting Mux", SND_SOC_NOPM, 0, 0,
1185a91eb199SMark Brown 		 &rin_inv_mux),
1186a91eb199SMark Brown 
1187a91eb199SMark Brown SND_SOC_DAPM_PGA("Left Capture PGA", WM8904_POWER_MANAGEMENT_0, 1, 0,
1188a91eb199SMark Brown 		 NULL, 0),
1189a91eb199SMark Brown SND_SOC_DAPM_PGA("Right Capture PGA", WM8904_POWER_MANAGEMENT_0, 0, 0,
1190a91eb199SMark Brown 		 NULL, 0),
1191a91eb199SMark Brown 
1192a91eb199SMark Brown SND_SOC_DAPM_ADC("ADCL", NULL, WM8904_POWER_MANAGEMENT_6, 1, 0),
1193a91eb199SMark Brown SND_SOC_DAPM_ADC("ADCR", NULL, WM8904_POWER_MANAGEMENT_6, 0, 0),
1194a91eb199SMark Brown 
1195a91eb199SMark Brown SND_SOC_DAPM_MUX("AIFOUTL Mux", SND_SOC_NOPM, 0, 0, &aifoutl_mux),
1196a91eb199SMark Brown SND_SOC_DAPM_MUX("AIFOUTR Mux", SND_SOC_NOPM, 0, 0, &aifoutr_mux),
1197a91eb199SMark Brown 
1198a91eb199SMark Brown SND_SOC_DAPM_AIF_OUT("AIFOUTL", "Capture", 0, SND_SOC_NOPM, 0, 0),
1199a91eb199SMark Brown SND_SOC_DAPM_AIF_OUT("AIFOUTR", "Capture", 1, SND_SOC_NOPM, 0, 0),
1200a91eb199SMark Brown };
1201a91eb199SMark Brown 
1202a91eb199SMark Brown static const struct snd_soc_dapm_widget wm8904_dac_dapm_widgets[] = {
1203a91eb199SMark Brown SND_SOC_DAPM_AIF_IN("AIFINL", "Playback", 0, SND_SOC_NOPM, 0, 0),
1204a91eb199SMark Brown SND_SOC_DAPM_AIF_IN("AIFINR", "Playback", 1, SND_SOC_NOPM, 0, 0),
1205a91eb199SMark Brown 
1206a91eb199SMark Brown SND_SOC_DAPM_MUX("DACL Mux", SND_SOC_NOPM, 0, 0, &aifinl_mux),
1207a91eb199SMark Brown SND_SOC_DAPM_MUX("DACR Mux", SND_SOC_NOPM, 0, 0, &aifinr_mux),
1208a91eb199SMark Brown 
1209a91eb199SMark Brown SND_SOC_DAPM_DAC("DACL", NULL, WM8904_POWER_MANAGEMENT_6, 3, 0),
1210a91eb199SMark Brown SND_SOC_DAPM_DAC("DACR", NULL, WM8904_POWER_MANAGEMENT_6, 2, 0),
1211a91eb199SMark Brown 
1212a91eb199SMark Brown SND_SOC_DAPM_SUPPLY("Charge pump", WM8904_CHARGE_PUMP_0, 0, 0, cp_event,
1213a91eb199SMark Brown 		    SND_SOC_DAPM_POST_PMU),
1214a91eb199SMark Brown 
1215a91eb199SMark Brown SND_SOC_DAPM_PGA("HPL PGA", WM8904_POWER_MANAGEMENT_2, 1, 0, NULL, 0),
1216a91eb199SMark Brown SND_SOC_DAPM_PGA("HPR PGA", WM8904_POWER_MANAGEMENT_2, 0, 0, NULL, 0),
1217a91eb199SMark Brown 
1218a91eb199SMark Brown SND_SOC_DAPM_PGA("LINEL PGA", WM8904_POWER_MANAGEMENT_3, 1, 0, NULL, 0),
1219a91eb199SMark Brown SND_SOC_DAPM_PGA("LINER PGA", WM8904_POWER_MANAGEMENT_3, 0, 0, NULL, 0),
1220a91eb199SMark Brown 
1221a91eb199SMark Brown SND_SOC_DAPM_PGA_E("Headphone Output", SND_SOC_NOPM, WM8904_ANALOGUE_HP_0,
1222a91eb199SMark Brown 		   0, NULL, 0, out_pga_event,
1223a91eb199SMark Brown 		   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1224a91eb199SMark Brown SND_SOC_DAPM_PGA_E("Line Output", SND_SOC_NOPM, WM8904_ANALOGUE_LINEOUT_0,
1225a91eb199SMark Brown 		   0, NULL, 0, out_pga_event,
1226a91eb199SMark Brown 		   SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
1227a91eb199SMark Brown 
1228a91eb199SMark Brown SND_SOC_DAPM_OUTPUT("HPOUTL"),
1229a91eb199SMark Brown SND_SOC_DAPM_OUTPUT("HPOUTR"),
1230a91eb199SMark Brown SND_SOC_DAPM_OUTPUT("LINEOUTL"),
1231a91eb199SMark Brown SND_SOC_DAPM_OUTPUT("LINEOUTR"),
1232a91eb199SMark Brown };
1233a91eb199SMark Brown 
1234a91eb199SMark Brown static const char *out_mux_text[] = {
1235a91eb199SMark Brown 	"DAC", "Bypass"
1236a91eb199SMark Brown };
1237a91eb199SMark Brown 
1238a91eb199SMark Brown static const struct soc_enum hpl_enum =
1239a91eb199SMark Brown 	SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 3, 2, out_mux_text);
1240a91eb199SMark Brown 
1241a91eb199SMark Brown static const struct snd_kcontrol_new hpl_mux =
1242a91eb199SMark Brown 	SOC_DAPM_ENUM("HPL Mux", hpl_enum);
1243a91eb199SMark Brown 
1244a91eb199SMark Brown static const struct soc_enum hpr_enum =
1245a91eb199SMark Brown 	SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 2, 2, out_mux_text);
1246a91eb199SMark Brown 
1247a91eb199SMark Brown static const struct snd_kcontrol_new hpr_mux =
1248a91eb199SMark Brown 	SOC_DAPM_ENUM("HPR Mux", hpr_enum);
1249a91eb199SMark Brown 
1250a91eb199SMark Brown static const struct soc_enum linel_enum =
1251a91eb199SMark Brown 	SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 1, 2, out_mux_text);
1252a91eb199SMark Brown 
1253a91eb199SMark Brown static const struct snd_kcontrol_new linel_mux =
1254a91eb199SMark Brown 	SOC_DAPM_ENUM("LINEL Mux", linel_enum);
1255a91eb199SMark Brown 
1256a91eb199SMark Brown static const struct soc_enum liner_enum =
1257a91eb199SMark Brown 	SOC_ENUM_SINGLE(WM8904_ANALOGUE_OUT12_ZC, 0, 2, out_mux_text);
1258a91eb199SMark Brown 
1259a91eb199SMark Brown static const struct snd_kcontrol_new liner_mux =
1260a91eb199SMark Brown 	SOC_DAPM_ENUM("LINEL Mux", liner_enum);
1261a91eb199SMark Brown 
1262a91eb199SMark Brown static const char *sidetone_text[] = {
1263a91eb199SMark Brown 	"None", "Left", "Right"
1264a91eb199SMark Brown };
1265a91eb199SMark Brown 
1266a91eb199SMark Brown static const struct soc_enum dacl_sidetone_enum =
1267a91eb199SMark Brown 	SOC_ENUM_SINGLE(WM8904_DAC_DIGITAL_0, 2, 3, sidetone_text);
1268a91eb199SMark Brown 
1269a91eb199SMark Brown static const struct snd_kcontrol_new dacl_sidetone_mux =
1270a91eb199SMark Brown 	SOC_DAPM_ENUM("Left Sidetone Mux", dacl_sidetone_enum);
1271a91eb199SMark Brown 
1272a91eb199SMark Brown static const struct soc_enum dacr_sidetone_enum =
1273a91eb199SMark Brown 	SOC_ENUM_SINGLE(WM8904_DAC_DIGITAL_0, 0, 3, sidetone_text);
1274a91eb199SMark Brown 
1275a91eb199SMark Brown static const struct snd_kcontrol_new dacr_sidetone_mux =
1276a91eb199SMark Brown 	SOC_DAPM_ENUM("Right Sidetone Mux", dacr_sidetone_enum);
1277a91eb199SMark Brown 
1278a91eb199SMark Brown static const struct snd_soc_dapm_widget wm8904_dapm_widgets[] = {
1279a91eb199SMark Brown SND_SOC_DAPM_SUPPLY("Class G", WM8904_CLASS_W_0, 0, 1, NULL, 0),
1280a91eb199SMark Brown SND_SOC_DAPM_PGA("Left Bypass", SND_SOC_NOPM, 0, 0, NULL, 0),
1281a91eb199SMark Brown SND_SOC_DAPM_PGA("Right Bypass", SND_SOC_NOPM, 0, 0, NULL, 0),
1282a91eb199SMark Brown 
1283a91eb199SMark Brown SND_SOC_DAPM_MUX("Left Sidetone", SND_SOC_NOPM, 0, 0, &dacl_sidetone_mux),
1284a91eb199SMark Brown SND_SOC_DAPM_MUX("Right Sidetone", SND_SOC_NOPM, 0, 0, &dacr_sidetone_mux),
1285a91eb199SMark Brown 
1286a91eb199SMark Brown SND_SOC_DAPM_MUX("HPL Mux", SND_SOC_NOPM, 0, 0, &hpl_mux),
1287a91eb199SMark Brown SND_SOC_DAPM_MUX("HPR Mux", SND_SOC_NOPM, 0, 0, &hpr_mux),
1288a91eb199SMark Brown SND_SOC_DAPM_MUX("LINEL Mux", SND_SOC_NOPM, 0, 0, &linel_mux),
1289a91eb199SMark Brown SND_SOC_DAPM_MUX("LINER Mux", SND_SOC_NOPM, 0, 0, &liner_mux),
1290a91eb199SMark Brown };
1291a91eb199SMark Brown 
1292a91eb199SMark Brown static const struct snd_soc_dapm_route core_intercon[] = {
1293a91eb199SMark Brown 	{ "CLK_DSP", NULL, "SYSCLK" },
1294a91eb199SMark Brown 	{ "TOCLK", NULL, "SYSCLK" },
1295a91eb199SMark Brown };
1296a91eb199SMark Brown 
1297a91eb199SMark Brown static const struct snd_soc_dapm_route adc_intercon[] = {
1298a91eb199SMark Brown 	{ "Left Capture Mux", "IN1L", "IN1L" },
1299a91eb199SMark Brown 	{ "Left Capture Mux", "IN2L", "IN2L" },
1300a91eb199SMark Brown 	{ "Left Capture Mux", "IN3L", "IN3L" },
1301a91eb199SMark Brown 
1302a91eb199SMark Brown 	{ "Left Capture Inverting Mux", "IN1L", "IN1L" },
1303a91eb199SMark Brown 	{ "Left Capture Inverting Mux", "IN2L", "IN2L" },
1304a91eb199SMark Brown 	{ "Left Capture Inverting Mux", "IN3L", "IN3L" },
1305a91eb199SMark Brown 
1306a91eb199SMark Brown 	{ "Right Capture Mux", "IN1R", "IN1R" },
1307a91eb199SMark Brown 	{ "Right Capture Mux", "IN2R", "IN2R" },
1308a91eb199SMark Brown 	{ "Right Capture Mux", "IN3R", "IN3R" },
1309a91eb199SMark Brown 
1310a91eb199SMark Brown 	{ "Right Capture Inverting Mux", "IN1R", "IN1R" },
1311a91eb199SMark Brown 	{ "Right Capture Inverting Mux", "IN2R", "IN2R" },
1312a91eb199SMark Brown 	{ "Right Capture Inverting Mux", "IN3R", "IN3R" },
1313a91eb199SMark Brown 
1314a91eb199SMark Brown 	{ "Left Capture PGA", NULL, "Left Capture Mux" },
1315a91eb199SMark Brown 	{ "Left Capture PGA", NULL, "Left Capture Inverting Mux" },
1316a91eb199SMark Brown 
1317a91eb199SMark Brown 	{ "Right Capture PGA", NULL, "Right Capture Mux" },
1318a91eb199SMark Brown 	{ "Right Capture PGA", NULL, "Right Capture Inverting Mux" },
1319a91eb199SMark Brown 
1320a91eb199SMark Brown 	{ "AIFOUTL", "Left",  "ADCL" },
1321a91eb199SMark Brown 	{ "AIFOUTL", "Right", "ADCR" },
1322a91eb199SMark Brown 	{ "AIFOUTR", "Left",  "ADCL" },
1323a91eb199SMark Brown 	{ "AIFOUTR", "Right", "ADCR" },
1324a91eb199SMark Brown 
1325a91eb199SMark Brown 	{ "ADCL", NULL, "CLK_DSP" },
1326a91eb199SMark Brown 	{ "ADCL", NULL, "Left Capture PGA" },
1327a91eb199SMark Brown 
1328a91eb199SMark Brown 	{ "ADCR", NULL, "CLK_DSP" },
1329a91eb199SMark Brown 	{ "ADCR", NULL, "Right Capture PGA" },
1330a91eb199SMark Brown };
1331a91eb199SMark Brown 
1332a91eb199SMark Brown static const struct snd_soc_dapm_route dac_intercon[] = {
1333a91eb199SMark Brown 	{ "DACL", "Right", "AIFINR" },
1334a91eb199SMark Brown 	{ "DACL", "Left",  "AIFINL" },
1335a91eb199SMark Brown 	{ "DACL", NULL, "CLK_DSP" },
1336a91eb199SMark Brown 
1337a91eb199SMark Brown 	{ "DACR", "Right", "AIFINR" },
1338a91eb199SMark Brown 	{ "DACR", "Left",  "AIFINL" },
1339a91eb199SMark Brown 	{ "DACR", NULL, "CLK_DSP" },
1340a91eb199SMark Brown 
1341a91eb199SMark Brown 	{ "Charge pump", NULL, "SYSCLK" },
1342a91eb199SMark Brown 
1343a91eb199SMark Brown 	{ "Headphone Output", NULL, "HPL PGA" },
1344a91eb199SMark Brown 	{ "Headphone Output", NULL, "HPR PGA" },
1345a91eb199SMark Brown 	{ "Headphone Output", NULL, "Charge pump" },
1346a91eb199SMark Brown 	{ "Headphone Output", NULL, "TOCLK" },
1347a91eb199SMark Brown 
1348a91eb199SMark Brown 	{ "Line Output", NULL, "LINEL PGA" },
1349a91eb199SMark Brown 	{ "Line Output", NULL, "LINER PGA" },
1350a91eb199SMark Brown 	{ "Line Output", NULL, "Charge pump" },
1351a91eb199SMark Brown 	{ "Line Output", NULL, "TOCLK" },
1352a91eb199SMark Brown 
1353a91eb199SMark Brown 	{ "HPOUTL", NULL, "Headphone Output" },
1354a91eb199SMark Brown 	{ "HPOUTR", NULL, "Headphone Output" },
1355a91eb199SMark Brown 
1356a91eb199SMark Brown 	{ "LINEOUTL", NULL, "Line Output" },
1357a91eb199SMark Brown 	{ "LINEOUTR", NULL, "Line Output" },
1358a91eb199SMark Brown };
1359a91eb199SMark Brown 
1360a91eb199SMark Brown static const struct snd_soc_dapm_route wm8904_intercon[] = {
1361a91eb199SMark Brown 	{ "Left Sidetone", "Left", "ADCL" },
1362a91eb199SMark Brown 	{ "Left Sidetone", "Right", "ADCR" },
1363a91eb199SMark Brown 	{ "DACL", NULL, "Left Sidetone" },
1364a91eb199SMark Brown 
1365a91eb199SMark Brown 	{ "Right Sidetone", "Left", "ADCL" },
1366a91eb199SMark Brown 	{ "Right Sidetone", "Right", "ADCR" },
1367a91eb199SMark Brown 	{ "DACR", NULL, "Right Sidetone" },
1368a91eb199SMark Brown 
1369a91eb199SMark Brown 	{ "Left Bypass", NULL, "Class G" },
1370a91eb199SMark Brown 	{ "Left Bypass", NULL, "Left Capture PGA" },
1371a91eb199SMark Brown 
1372a91eb199SMark Brown 	{ "Right Bypass", NULL, "Class G" },
1373a91eb199SMark Brown 	{ "Right Bypass", NULL, "Right Capture PGA" },
1374a91eb199SMark Brown 
1375a91eb199SMark Brown 	{ "HPL Mux", "DAC", "DACL" },
1376a91eb199SMark Brown 	{ "HPL Mux", "Bypass", "Left Bypass" },
1377a91eb199SMark Brown 
1378a91eb199SMark Brown 	{ "HPR Mux", "DAC", "DACR" },
1379a91eb199SMark Brown 	{ "HPR Mux", "Bypass", "Right Bypass" },
1380a91eb199SMark Brown 
1381a91eb199SMark Brown 	{ "LINEL Mux", "DAC", "DACL" },
1382a91eb199SMark Brown 	{ "LINEL Mux", "Bypass", "Left Bypass" },
1383a91eb199SMark Brown 
1384a91eb199SMark Brown 	{ "LINER Mux", "DAC", "DACR" },
1385a91eb199SMark Brown 	{ "LINER Mux", "Bypass", "Right Bypass" },
1386a91eb199SMark Brown 
1387a91eb199SMark Brown 	{ "HPL PGA", NULL, "HPL Mux" },
1388a91eb199SMark Brown 	{ "HPR PGA", NULL, "HPR Mux" },
1389a91eb199SMark Brown 
1390a91eb199SMark Brown 	{ "LINEL PGA", NULL, "LINEL Mux" },
1391a91eb199SMark Brown 	{ "LINER PGA", NULL, "LINER Mux" },
1392a91eb199SMark Brown };
1393a91eb199SMark Brown 
1394a91eb199SMark Brown static int wm8904_add_widgets(struct snd_soc_codec *codec)
1395a91eb199SMark Brown {
1396a91eb199SMark Brown 	snd_soc_add_controls(codec, wm8904_adc_snd_controls,
1397a91eb199SMark Brown 			     ARRAY_SIZE(wm8904_adc_snd_controls));
1398a91eb199SMark Brown 	snd_soc_add_controls(codec, wm8904_dac_snd_controls,
1399a91eb199SMark Brown 			     ARRAY_SIZE(wm8904_dac_snd_controls));
1400a91eb199SMark Brown 	snd_soc_add_controls(codec, wm8904_snd_controls,
1401a91eb199SMark Brown 			     ARRAY_SIZE(wm8904_snd_controls));
1402a91eb199SMark Brown 
1403a91eb199SMark Brown 	snd_soc_dapm_new_controls(codec, wm8904_core_dapm_widgets,
1404a91eb199SMark Brown 				  ARRAY_SIZE(wm8904_core_dapm_widgets));
1405a91eb199SMark Brown 	snd_soc_dapm_new_controls(codec, wm8904_adc_dapm_widgets,
1406a91eb199SMark Brown 				  ARRAY_SIZE(wm8904_adc_dapm_widgets));
1407a91eb199SMark Brown 	snd_soc_dapm_new_controls(codec, wm8904_dac_dapm_widgets,
1408a91eb199SMark Brown 				  ARRAY_SIZE(wm8904_dac_dapm_widgets));
1409a91eb199SMark Brown 	snd_soc_dapm_new_controls(codec, wm8904_dapm_widgets,
1410a91eb199SMark Brown 				  ARRAY_SIZE(wm8904_dapm_widgets));
1411a91eb199SMark Brown 
1412a91eb199SMark Brown 	snd_soc_dapm_add_routes(codec, core_intercon,
1413a91eb199SMark Brown 				ARRAY_SIZE(core_intercon));
1414a91eb199SMark Brown 	snd_soc_dapm_add_routes(codec, adc_intercon, ARRAY_SIZE(adc_intercon));
1415a91eb199SMark Brown 	snd_soc_dapm_add_routes(codec, dac_intercon, ARRAY_SIZE(dac_intercon));
1416a91eb199SMark Brown 	snd_soc_dapm_add_routes(codec, wm8904_intercon,
1417a91eb199SMark Brown 				ARRAY_SIZE(wm8904_intercon));
1418a91eb199SMark Brown 
1419a91eb199SMark Brown 	snd_soc_dapm_new_widgets(codec);
1420a91eb199SMark Brown 	return 0;
1421a91eb199SMark Brown }
1422a91eb199SMark Brown 
1423a91eb199SMark Brown static struct {
1424a91eb199SMark Brown 	int ratio;
1425a91eb199SMark Brown 	unsigned int clk_sys_rate;
1426a91eb199SMark Brown } clk_sys_rates[] = {
1427a91eb199SMark Brown 	{   64,  0 },
1428a91eb199SMark Brown 	{  128,  1 },
1429a91eb199SMark Brown 	{  192,  2 },
1430a91eb199SMark Brown 	{  256,  3 },
1431a91eb199SMark Brown 	{  384,  4 },
1432a91eb199SMark Brown 	{  512,  5 },
1433a91eb199SMark Brown 	{  786,  6 },
1434a91eb199SMark Brown 	{ 1024,  7 },
1435a91eb199SMark Brown 	{ 1408,  8 },
1436a91eb199SMark Brown 	{ 1536,  9 },
1437a91eb199SMark Brown };
1438a91eb199SMark Brown 
1439a91eb199SMark Brown static struct {
1440a91eb199SMark Brown 	int rate;
1441a91eb199SMark Brown 	int sample_rate;
1442a91eb199SMark Brown } sample_rates[] = {
1443a91eb199SMark Brown 	{ 8000,  0  },
1444a91eb199SMark Brown 	{ 11025, 1  },
1445a91eb199SMark Brown 	{ 12000, 1  },
1446a91eb199SMark Brown 	{ 16000, 2  },
1447a91eb199SMark Brown 	{ 22050, 3  },
1448a91eb199SMark Brown 	{ 24000, 3  },
1449a91eb199SMark Brown 	{ 32000, 4  },
1450a91eb199SMark Brown 	{ 44100, 5  },
1451a91eb199SMark Brown 	{ 48000, 5  },
1452a91eb199SMark Brown };
1453a91eb199SMark Brown 
1454a91eb199SMark Brown static struct {
1455a91eb199SMark Brown 	int div; /* *10 due to .5s */
1456a91eb199SMark Brown 	int bclk_div;
1457a91eb199SMark Brown } bclk_divs[] = {
1458a91eb199SMark Brown 	{ 10,  0  },
1459a91eb199SMark Brown 	{ 15,  1  },
1460a91eb199SMark Brown 	{ 20,  2  },
1461a91eb199SMark Brown 	{ 30,  3  },
1462a91eb199SMark Brown 	{ 40,  4  },
1463a91eb199SMark Brown 	{ 50,  5  },
1464a91eb199SMark Brown 	{ 55,  6  },
1465a91eb199SMark Brown 	{ 60,  7  },
1466a91eb199SMark Brown 	{ 80,  8  },
1467a91eb199SMark Brown 	{ 100, 9  },
1468a91eb199SMark Brown 	{ 110, 10 },
1469a91eb199SMark Brown 	{ 120, 11 },
1470a91eb199SMark Brown 	{ 160, 12 },
1471a91eb199SMark Brown 	{ 200, 13 },
1472a91eb199SMark Brown 	{ 220, 14 },
1473a91eb199SMark Brown 	{ 240, 16 },
1474a91eb199SMark Brown 	{ 200, 17 },
1475a91eb199SMark Brown 	{ 320, 18 },
1476a91eb199SMark Brown 	{ 440, 19 },
1477a91eb199SMark Brown 	{ 480, 20 },
1478a91eb199SMark Brown };
1479a91eb199SMark Brown 
1480a91eb199SMark Brown 
1481a91eb199SMark Brown static int wm8904_hw_params(struct snd_pcm_substream *substream,
1482a91eb199SMark Brown 			    struct snd_pcm_hw_params *params,
1483a91eb199SMark Brown 			    struct snd_soc_dai *dai)
1484a91eb199SMark Brown {
1485a91eb199SMark Brown 	struct snd_soc_codec *codec = dai->codec;
1486a91eb199SMark Brown 	struct wm8904_priv *wm8904 = codec->private_data;
1487a91eb199SMark Brown 	int ret, i, best, best_val, cur_val;
1488a91eb199SMark Brown 	unsigned int aif1 = 0;
1489a91eb199SMark Brown 	unsigned int aif2 = 0;
1490a91eb199SMark Brown 	unsigned int aif3 = 0;
1491a91eb199SMark Brown 	unsigned int clock1 = 0;
1492a91eb199SMark Brown 	unsigned int dac_digital1 = 0;
1493a91eb199SMark Brown 
1494a91eb199SMark Brown 	/* What BCLK do we need? */
1495a91eb199SMark Brown 	wm8904->fs = params_rate(params);
1496a91eb199SMark Brown 	if (wm8904->tdm_slots) {
1497a91eb199SMark Brown 		dev_dbg(codec->dev, "Configuring for %d %d bit TDM slots\n",
1498a91eb199SMark Brown 			wm8904->tdm_slots, wm8904->tdm_width);
1499a91eb199SMark Brown 		wm8904->bclk = snd_soc_calc_bclk(wm8904->fs,
1500a91eb199SMark Brown 						 wm8904->tdm_width, 2,
1501a91eb199SMark Brown 						 wm8904->tdm_slots);
1502a91eb199SMark Brown 	} else {
1503a91eb199SMark Brown 		wm8904->bclk = snd_soc_params_to_bclk(params);
1504a91eb199SMark Brown 	}
1505a91eb199SMark Brown 
150656927eb0SMark Brown 	switch (params_format(params)) {
150756927eb0SMark Brown 	case SNDRV_PCM_FORMAT_S16_LE:
150856927eb0SMark Brown 		break;
150956927eb0SMark Brown 	case SNDRV_PCM_FORMAT_S20_3LE:
151056927eb0SMark Brown 		aif1 |= 0x40;
151156927eb0SMark Brown 		break;
151256927eb0SMark Brown 	case SNDRV_PCM_FORMAT_S24_LE:
151356927eb0SMark Brown 		aif1 |= 0x80;
151456927eb0SMark Brown 		break;
151556927eb0SMark Brown 	case SNDRV_PCM_FORMAT_S32_LE:
151656927eb0SMark Brown 		aif1 |= 0xc0;
151756927eb0SMark Brown 		break;
151856927eb0SMark Brown 	default:
151956927eb0SMark Brown 		return -EINVAL;
152056927eb0SMark Brown 	}
152156927eb0SMark Brown 
152256927eb0SMark Brown 
1523a91eb199SMark Brown 	dev_dbg(codec->dev, "Target BCLK is %dHz\n", wm8904->bclk);
1524a91eb199SMark Brown 
1525a91eb199SMark Brown 	ret = wm8904_configure_clocking(codec);
1526a91eb199SMark Brown 	if (ret != 0)
1527a91eb199SMark Brown 		return ret;
1528a91eb199SMark Brown 
1529a91eb199SMark Brown 	/* Select nearest CLK_SYS_RATE */
1530a91eb199SMark Brown 	best = 0;
1531a91eb199SMark Brown 	best_val = abs((wm8904->sysclk_rate / clk_sys_rates[0].ratio)
1532a91eb199SMark Brown 		       - wm8904->fs);
1533a91eb199SMark Brown 	for (i = 1; i < ARRAY_SIZE(clk_sys_rates); i++) {
1534a91eb199SMark Brown 		cur_val = abs((wm8904->sysclk_rate /
1535a91eb199SMark Brown 			       clk_sys_rates[i].ratio) - wm8904->fs);;
1536a91eb199SMark Brown 		if (cur_val < best_val) {
1537a91eb199SMark Brown 			best = i;
1538a91eb199SMark Brown 			best_val = cur_val;
1539a91eb199SMark Brown 		}
1540a91eb199SMark Brown 	}
1541a91eb199SMark Brown 	dev_dbg(codec->dev, "Selected CLK_SYS_RATIO of %d\n",
1542a91eb199SMark Brown 		clk_sys_rates[best].ratio);
1543a91eb199SMark Brown 	clock1 |= (clk_sys_rates[best].clk_sys_rate
1544a91eb199SMark Brown 		   << WM8904_CLK_SYS_RATE_SHIFT);
1545a91eb199SMark Brown 
1546a91eb199SMark Brown 	/* SAMPLE_RATE */
1547a91eb199SMark Brown 	best = 0;
1548a91eb199SMark Brown 	best_val = abs(wm8904->fs - sample_rates[0].rate);
1549a91eb199SMark Brown 	for (i = 1; i < ARRAY_SIZE(sample_rates); i++) {
1550a91eb199SMark Brown 		/* Closest match */
1551a91eb199SMark Brown 		cur_val = abs(wm8904->fs - sample_rates[i].rate);
1552a91eb199SMark Brown 		if (cur_val < best_val) {
1553a91eb199SMark Brown 			best = i;
1554a91eb199SMark Brown 			best_val = cur_val;
1555a91eb199SMark Brown 		}
1556a91eb199SMark Brown 	}
1557a91eb199SMark Brown 	dev_dbg(codec->dev, "Selected SAMPLE_RATE of %dHz\n",
1558a91eb199SMark Brown 		sample_rates[best].rate);
1559a91eb199SMark Brown 	clock1 |= (sample_rates[best].sample_rate
1560a91eb199SMark Brown 		   << WM8904_SAMPLE_RATE_SHIFT);
1561a91eb199SMark Brown 
1562a91eb199SMark Brown 	/* Enable sloping stopband filter for low sample rates */
1563a91eb199SMark Brown 	if (wm8904->fs <= 24000)
1564a91eb199SMark Brown 		dac_digital1 |= WM8904_DAC_SB_FILT;
1565a91eb199SMark Brown 
1566a91eb199SMark Brown 	/* BCLK_DIV */
1567a91eb199SMark Brown 	best = 0;
1568a91eb199SMark Brown 	best_val = INT_MAX;
1569a91eb199SMark Brown 	for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
1570a91eb199SMark Brown 		cur_val = ((wm8904->sysclk_rate * 10) / bclk_divs[i].div)
1571a91eb199SMark Brown 			- wm8904->bclk;
1572a91eb199SMark Brown 		if (cur_val < 0) /* Table is sorted */
1573a91eb199SMark Brown 			break;
1574a91eb199SMark Brown 		if (cur_val < best_val) {
1575a91eb199SMark Brown 			best = i;
1576a91eb199SMark Brown 			best_val = cur_val;
1577a91eb199SMark Brown 		}
1578a91eb199SMark Brown 	}
1579a91eb199SMark Brown 	wm8904->bclk = (wm8904->sysclk_rate * 10) / bclk_divs[best].div;
1580a91eb199SMark Brown 	dev_dbg(codec->dev, "Selected BCLK_DIV of %d for %dHz BCLK\n",
1581a91eb199SMark Brown 		bclk_divs[best].div, wm8904->bclk);
1582a91eb199SMark Brown 	aif2 |= bclk_divs[best].bclk_div;
1583a91eb199SMark Brown 
1584a91eb199SMark Brown 	/* LRCLK is a simple fraction of BCLK */
1585a91eb199SMark Brown 	dev_dbg(codec->dev, "LRCLK_RATE is %d\n", wm8904->bclk / wm8904->fs);
1586a91eb199SMark Brown 	aif3 |= wm8904->bclk / wm8904->fs;
1587a91eb199SMark Brown 
1588a91eb199SMark Brown 	/* Apply the settings */
1589a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_DAC_DIGITAL_1,
1590a91eb199SMark Brown 			    WM8904_DAC_SB_FILT, dac_digital1);
1591a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_1,
1592a91eb199SMark Brown 			    WM8904_AIF_WL_MASK, aif1);
1593a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_2,
1594a91eb199SMark Brown 			    WM8904_BCLK_DIV_MASK, aif2);
1595a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_3,
1596a91eb199SMark Brown 			    WM8904_LRCLK_RATE_MASK, aif3);
1597a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_CLOCK_RATES_1,
1598a91eb199SMark Brown 			    WM8904_SAMPLE_RATE_MASK |
1599a91eb199SMark Brown 			    WM8904_CLK_SYS_RATE_MASK, clock1);
1600a91eb199SMark Brown 
1601a91eb199SMark Brown 	/* Update filters for the new settings */
1602a91eb199SMark Brown 	wm8904_set_retune_mobile(codec);
1603a91eb199SMark Brown 	wm8904_set_deemph(codec);
1604a91eb199SMark Brown 
1605a91eb199SMark Brown 	return 0;
1606a91eb199SMark Brown }
1607a91eb199SMark Brown 
1608a91eb199SMark Brown 
1609a91eb199SMark Brown static int wm8904_set_sysclk(struct snd_soc_dai *dai, int clk_id,
1610a91eb199SMark Brown 			     unsigned int freq, int dir)
1611a91eb199SMark Brown {
1612a91eb199SMark Brown 	struct snd_soc_codec *codec = dai->codec;
1613a91eb199SMark Brown 	struct wm8904_priv *priv = codec->private_data;
1614a91eb199SMark Brown 
1615a91eb199SMark Brown 	switch (clk_id) {
1616a91eb199SMark Brown 	case WM8904_CLK_MCLK:
1617a91eb199SMark Brown 		priv->sysclk_src = clk_id;
1618a91eb199SMark Brown 		priv->mclk_rate = freq;
1619a91eb199SMark Brown 		break;
1620a91eb199SMark Brown 
1621a91eb199SMark Brown 	case WM8904_CLK_FLL:
1622a91eb199SMark Brown 		priv->sysclk_src = clk_id;
1623a91eb199SMark Brown 		break;
1624a91eb199SMark Brown 
1625a91eb199SMark Brown 	default:
1626a91eb199SMark Brown 		return -EINVAL;
1627a91eb199SMark Brown 	}
1628a91eb199SMark Brown 
1629a91eb199SMark Brown 	dev_dbg(dai->dev, "Clock source is %d at %uHz\n", clk_id, freq);
1630a91eb199SMark Brown 
1631a91eb199SMark Brown 	wm8904_configure_clocking(codec);
1632a91eb199SMark Brown 
1633a91eb199SMark Brown 	return 0;
1634a91eb199SMark Brown }
1635a91eb199SMark Brown 
1636a91eb199SMark Brown static int wm8904_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
1637a91eb199SMark Brown {
1638a91eb199SMark Brown 	struct snd_soc_codec *codec = dai->codec;
1639a91eb199SMark Brown 	unsigned int aif1 = 0;
1640a91eb199SMark Brown 	unsigned int aif3 = 0;
1641a91eb199SMark Brown 
1642a91eb199SMark Brown 	switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
1643a91eb199SMark Brown 	case SND_SOC_DAIFMT_CBS_CFS:
1644a91eb199SMark Brown 		break;
1645a91eb199SMark Brown 	case SND_SOC_DAIFMT_CBS_CFM:
1646a91eb199SMark Brown 		aif3 |= WM8904_LRCLK_DIR;
1647a91eb199SMark Brown 		break;
1648a91eb199SMark Brown 	case SND_SOC_DAIFMT_CBM_CFS:
1649a91eb199SMark Brown 		aif1 |= WM8904_BCLK_DIR;
1650a91eb199SMark Brown 		break;
1651a91eb199SMark Brown 	case SND_SOC_DAIFMT_CBM_CFM:
1652a91eb199SMark Brown 		aif1 |= WM8904_BCLK_DIR;
1653a91eb199SMark Brown 		aif3 |= WM8904_LRCLK_DIR;
1654a91eb199SMark Brown 		break;
1655a91eb199SMark Brown 	default:
1656a91eb199SMark Brown 		return -EINVAL;
1657a91eb199SMark Brown 	}
1658a91eb199SMark Brown 
1659a91eb199SMark Brown 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1660a91eb199SMark Brown 	case SND_SOC_DAIFMT_DSP_B:
1661a91eb199SMark Brown 		aif1 |= WM8904_AIF_LRCLK_INV;
1662a91eb199SMark Brown 	case SND_SOC_DAIFMT_DSP_A:
1663a91eb199SMark Brown 		aif1 |= 0x3;
1664a91eb199SMark Brown 		break;
1665a91eb199SMark Brown 	case SND_SOC_DAIFMT_I2S:
1666a91eb199SMark Brown 		aif1 |= 0x2;
1667a91eb199SMark Brown 		break;
1668a91eb199SMark Brown 	case SND_SOC_DAIFMT_RIGHT_J:
1669a91eb199SMark Brown 		break;
1670a91eb199SMark Brown 	case SND_SOC_DAIFMT_LEFT_J:
1671a91eb199SMark Brown 		aif1 |= 0x1;
1672a91eb199SMark Brown 		break;
1673a91eb199SMark Brown 	default:
1674a91eb199SMark Brown 		return -EINVAL;
1675a91eb199SMark Brown 	}
1676a91eb199SMark Brown 
1677a91eb199SMark Brown 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
1678a91eb199SMark Brown 	case SND_SOC_DAIFMT_DSP_A:
1679a91eb199SMark Brown 	case SND_SOC_DAIFMT_DSP_B:
1680a91eb199SMark Brown 		/* frame inversion not valid for DSP modes */
1681a91eb199SMark Brown 		switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1682a91eb199SMark Brown 		case SND_SOC_DAIFMT_NB_NF:
1683a91eb199SMark Brown 			break;
1684a91eb199SMark Brown 		case SND_SOC_DAIFMT_IB_NF:
1685a91eb199SMark Brown 			aif1 |= WM8904_AIF_BCLK_INV;
1686a91eb199SMark Brown 			break;
1687a91eb199SMark Brown 		default:
1688a91eb199SMark Brown 			return -EINVAL;
1689a91eb199SMark Brown 		}
1690a91eb199SMark Brown 		break;
1691a91eb199SMark Brown 
1692a91eb199SMark Brown 	case SND_SOC_DAIFMT_I2S:
1693a91eb199SMark Brown 	case SND_SOC_DAIFMT_RIGHT_J:
1694a91eb199SMark Brown 	case SND_SOC_DAIFMT_LEFT_J:
1695a91eb199SMark Brown 		switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
1696a91eb199SMark Brown 		case SND_SOC_DAIFMT_NB_NF:
1697a91eb199SMark Brown 			break;
1698a91eb199SMark Brown 		case SND_SOC_DAIFMT_IB_IF:
1699a91eb199SMark Brown 			aif1 |= WM8904_AIF_BCLK_INV | WM8904_AIF_LRCLK_INV;
1700a91eb199SMark Brown 			break;
1701a91eb199SMark Brown 		case SND_SOC_DAIFMT_IB_NF:
1702a91eb199SMark Brown 			aif1 |= WM8904_AIF_BCLK_INV;
1703a91eb199SMark Brown 			break;
1704a91eb199SMark Brown 		case SND_SOC_DAIFMT_NB_IF:
1705a91eb199SMark Brown 			aif1 |= WM8904_AIF_LRCLK_INV;
1706a91eb199SMark Brown 			break;
1707a91eb199SMark Brown 		default:
1708a91eb199SMark Brown 			return -EINVAL;
1709a91eb199SMark Brown 		}
1710a91eb199SMark Brown 		break;
1711a91eb199SMark Brown 	default:
1712a91eb199SMark Brown 		return -EINVAL;
1713a91eb199SMark Brown 	}
1714a91eb199SMark Brown 
1715a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_1,
1716a91eb199SMark Brown 			    WM8904_AIF_BCLK_INV | WM8904_AIF_LRCLK_INV |
1717a91eb199SMark Brown 			    WM8904_AIF_FMT_MASK | WM8904_BCLK_DIR, aif1);
1718a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_3,
1719a91eb199SMark Brown 			    WM8904_LRCLK_DIR, aif3);
1720a91eb199SMark Brown 
1721a91eb199SMark Brown 	return 0;
1722a91eb199SMark Brown }
1723a91eb199SMark Brown 
1724a91eb199SMark Brown 
1725a91eb199SMark Brown static int wm8904_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
1726a91eb199SMark Brown 			       unsigned int rx_mask, int slots, int slot_width)
1727a91eb199SMark Brown {
1728a91eb199SMark Brown 	struct snd_soc_codec *codec = dai->codec;
1729a91eb199SMark Brown 	struct wm8904_priv *wm8904 = codec->private_data;
1730a91eb199SMark Brown 	int aif1 = 0;
1731a91eb199SMark Brown 
1732a91eb199SMark Brown 	/* Don't need to validate anything if we're turning off TDM */
1733a91eb199SMark Brown 	if (slots == 0)
1734a91eb199SMark Brown 		goto out;
1735a91eb199SMark Brown 
1736a91eb199SMark Brown 	/* Note that we allow configurations we can't handle ourselves -
1737a91eb199SMark Brown 	 * for example, we can generate clocks for slots 2 and up even if
1738a91eb199SMark Brown 	 * we can't use those slots ourselves.
1739a91eb199SMark Brown 	 */
1740a91eb199SMark Brown 	aif1 |= WM8904_AIFADC_TDM | WM8904_AIFDAC_TDM;
1741a91eb199SMark Brown 
1742a91eb199SMark Brown 	switch (rx_mask) {
1743a91eb199SMark Brown 	case 3:
1744a91eb199SMark Brown 		break;
1745a91eb199SMark Brown 	case 0xc:
1746a91eb199SMark Brown 		aif1 |= WM8904_AIFADC_TDM_CHAN;
1747a91eb199SMark Brown 		break;
1748a91eb199SMark Brown 	default:
1749a91eb199SMark Brown 		return -EINVAL;
1750a91eb199SMark Brown 	}
1751a91eb199SMark Brown 
1752a91eb199SMark Brown 
1753a91eb199SMark Brown 	switch (tx_mask) {
1754a91eb199SMark Brown 	case 3:
1755a91eb199SMark Brown 		break;
1756a91eb199SMark Brown 	case 0xc:
1757a91eb199SMark Brown 		aif1 |= WM8904_AIFDAC_TDM_CHAN;
1758a91eb199SMark Brown 		break;
1759a91eb199SMark Brown 	default:
1760a91eb199SMark Brown 		return -EINVAL;
1761a91eb199SMark Brown 	}
1762a91eb199SMark Brown 
1763a91eb199SMark Brown out:
1764a91eb199SMark Brown 	wm8904->tdm_width = slot_width;
1765a91eb199SMark Brown 	wm8904->tdm_slots = slots / 2;
1766a91eb199SMark Brown 
1767a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_AUDIO_INTERFACE_1,
1768a91eb199SMark Brown 			    WM8904_AIFADC_TDM | WM8904_AIFADC_TDM_CHAN |
1769a91eb199SMark Brown 			    WM8904_AIFDAC_TDM | WM8904_AIFDAC_TDM_CHAN, aif1);
1770a91eb199SMark Brown 
1771a91eb199SMark Brown 	return 0;
1772a91eb199SMark Brown }
1773a91eb199SMark Brown 
1774a91eb199SMark Brown struct _fll_div {
1775a91eb199SMark Brown 	u16 fll_fratio;
1776a91eb199SMark Brown 	u16 fll_outdiv;
1777a91eb199SMark Brown 	u16 fll_clk_ref_div;
1778a91eb199SMark Brown 	u16 n;
1779a91eb199SMark Brown 	u16 k;
1780a91eb199SMark Brown };
1781a91eb199SMark Brown 
1782a91eb199SMark Brown /* The size in bits of the FLL divide multiplied by 10
1783a91eb199SMark Brown  * to allow rounding later */
1784a91eb199SMark Brown #define FIXED_FLL_SIZE ((1 << 16) * 10)
1785a91eb199SMark Brown 
1786a91eb199SMark Brown static struct {
1787a91eb199SMark Brown 	unsigned int min;
1788a91eb199SMark Brown 	unsigned int max;
1789a91eb199SMark Brown 	u16 fll_fratio;
1790a91eb199SMark Brown 	int ratio;
1791a91eb199SMark Brown } fll_fratios[] = {
1792a91eb199SMark Brown 	{       0,    64000, 4, 16 },
1793a91eb199SMark Brown 	{   64000,   128000, 3,  8 },
1794a91eb199SMark Brown 	{  128000,   256000, 2,  4 },
1795a91eb199SMark Brown 	{  256000,  1000000, 1,  2 },
1796a91eb199SMark Brown 	{ 1000000, 13500000, 0,  1 },
1797a91eb199SMark Brown };
1798a91eb199SMark Brown 
1799a91eb199SMark Brown static int fll_factors(struct _fll_div *fll_div, unsigned int Fref,
1800a91eb199SMark Brown 		       unsigned int Fout)
1801a91eb199SMark Brown {
1802a91eb199SMark Brown 	u64 Kpart;
1803a91eb199SMark Brown 	unsigned int K, Ndiv, Nmod, target;
1804a91eb199SMark Brown 	unsigned int div;
1805a91eb199SMark Brown 	int i;
1806a91eb199SMark Brown 
1807a91eb199SMark Brown 	/* Fref must be <=13.5MHz */
1808a91eb199SMark Brown 	div = 1;
1809a91eb199SMark Brown 	fll_div->fll_clk_ref_div = 0;
1810a91eb199SMark Brown 	while ((Fref / div) > 13500000) {
1811a91eb199SMark Brown 		div *= 2;
1812a91eb199SMark Brown 		fll_div->fll_clk_ref_div++;
1813a91eb199SMark Brown 
1814a91eb199SMark Brown 		if (div > 8) {
1815a91eb199SMark Brown 			pr_err("Can't scale %dMHz input down to <=13.5MHz\n",
1816a91eb199SMark Brown 			       Fref);
1817a91eb199SMark Brown 			return -EINVAL;
1818a91eb199SMark Brown 		}
1819a91eb199SMark Brown 	}
1820a91eb199SMark Brown 
1821a91eb199SMark Brown 	pr_debug("Fref=%u Fout=%u\n", Fref, Fout);
1822a91eb199SMark Brown 
1823a91eb199SMark Brown 	/* Apply the division for our remaining calculations */
1824a91eb199SMark Brown 	Fref /= div;
1825a91eb199SMark Brown 
1826a91eb199SMark Brown 	/* Fvco should be 90-100MHz; don't check the upper bound */
1827a91eb199SMark Brown 	div = 4;
1828a91eb199SMark Brown 	while (Fout * div < 90000000) {
1829a91eb199SMark Brown 		div++;
1830a91eb199SMark Brown 		if (div > 64) {
1831a91eb199SMark Brown 			pr_err("Unable to find FLL_OUTDIV for Fout=%uHz\n",
1832a91eb199SMark Brown 			       Fout);
1833a91eb199SMark Brown 			return -EINVAL;
1834a91eb199SMark Brown 		}
1835a91eb199SMark Brown 	}
1836a91eb199SMark Brown 	target = Fout * div;
1837a91eb199SMark Brown 	fll_div->fll_outdiv = div - 1;
1838a91eb199SMark Brown 
1839a91eb199SMark Brown 	pr_debug("Fvco=%dHz\n", target);
1840a91eb199SMark Brown 
1841a91eb199SMark Brown 	/* Find an appropraite FLL_FRATIO and factor it out of the target */
1842a91eb199SMark Brown 	for (i = 0; i < ARRAY_SIZE(fll_fratios); i++) {
1843a91eb199SMark Brown 		if (fll_fratios[i].min <= Fref && Fref <= fll_fratios[i].max) {
1844a91eb199SMark Brown 			fll_div->fll_fratio = fll_fratios[i].fll_fratio;
1845a91eb199SMark Brown 			target /= fll_fratios[i].ratio;
1846a91eb199SMark Brown 			break;
1847a91eb199SMark Brown 		}
1848a91eb199SMark Brown 	}
1849a91eb199SMark Brown 	if (i == ARRAY_SIZE(fll_fratios)) {
1850a91eb199SMark Brown 		pr_err("Unable to find FLL_FRATIO for Fref=%uHz\n", Fref);
1851a91eb199SMark Brown 		return -EINVAL;
1852a91eb199SMark Brown 	}
1853a91eb199SMark Brown 
1854a91eb199SMark Brown 	/* Now, calculate N.K */
1855a91eb199SMark Brown 	Ndiv = target / Fref;
1856a91eb199SMark Brown 
1857a91eb199SMark Brown 	fll_div->n = Ndiv;
1858a91eb199SMark Brown 	Nmod = target % Fref;
1859a91eb199SMark Brown 	pr_debug("Nmod=%d\n", Nmod);
1860a91eb199SMark Brown 
1861a91eb199SMark Brown 	/* Calculate fractional part - scale up so we can round. */
1862a91eb199SMark Brown 	Kpart = FIXED_FLL_SIZE * (long long)Nmod;
1863a91eb199SMark Brown 
1864a91eb199SMark Brown 	do_div(Kpart, Fref);
1865a91eb199SMark Brown 
1866a91eb199SMark Brown 	K = Kpart & 0xFFFFFFFF;
1867a91eb199SMark Brown 
1868a91eb199SMark Brown 	if ((K % 10) >= 5)
1869a91eb199SMark Brown 		K += 5;
1870a91eb199SMark Brown 
1871a91eb199SMark Brown 	/* Move down to proper range now rounding is done */
1872a91eb199SMark Brown 	fll_div->k = K / 10;
1873a91eb199SMark Brown 
1874a91eb199SMark Brown 	pr_debug("N=%x K=%x FLL_FRATIO=%x FLL_OUTDIV=%x FLL_CLK_REF_DIV=%x\n",
1875a91eb199SMark Brown 		 fll_div->n, fll_div->k,
1876a91eb199SMark Brown 		 fll_div->fll_fratio, fll_div->fll_outdiv,
1877a91eb199SMark Brown 		 fll_div->fll_clk_ref_div);
1878a91eb199SMark Brown 
1879a91eb199SMark Brown 	return 0;
1880a91eb199SMark Brown }
1881a91eb199SMark Brown 
1882a91eb199SMark Brown static int wm8904_set_fll(struct snd_soc_dai *dai, int fll_id, int source,
1883a91eb199SMark Brown 			  unsigned int Fref, unsigned int Fout)
1884a91eb199SMark Brown {
1885a91eb199SMark Brown 	struct snd_soc_codec *codec = dai->codec;
1886a91eb199SMark Brown 	struct wm8904_priv *wm8904 = codec->private_data;
1887a91eb199SMark Brown 	struct _fll_div fll_div;
1888a91eb199SMark Brown 	int ret, val;
1889a91eb199SMark Brown 	int clock2, fll1;
1890a91eb199SMark Brown 
1891a91eb199SMark Brown 	/* Any change? */
1892a91eb199SMark Brown 	if (source == wm8904->fll_src && Fref == wm8904->fll_fref &&
1893a91eb199SMark Brown 	    Fout == wm8904->fll_fout)
1894a91eb199SMark Brown 		return 0;
1895a91eb199SMark Brown 
189618240b67SMark Brown 	clock2 = snd_soc_read(codec, WM8904_CLOCK_RATES_2);
189718240b67SMark Brown 
1898a91eb199SMark Brown 	if (Fout == 0) {
1899a91eb199SMark Brown 		dev_dbg(codec->dev, "FLL disabled\n");
1900a91eb199SMark Brown 
1901a91eb199SMark Brown 		wm8904->fll_fref = 0;
1902a91eb199SMark Brown 		wm8904->fll_fout = 0;
1903a91eb199SMark Brown 
1904a91eb199SMark Brown 		/* Gate SYSCLK to avoid glitches */
1905a91eb199SMark Brown 		snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
1906a91eb199SMark Brown 				    WM8904_CLK_SYS_ENA, 0);
1907a91eb199SMark Brown 
1908a91eb199SMark Brown 		snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
1909a91eb199SMark Brown 				    WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
1910a91eb199SMark Brown 
1911a91eb199SMark Brown 		goto out;
1912a91eb199SMark Brown 	}
1913a91eb199SMark Brown 
1914a91eb199SMark Brown 	/* Validate the FLL ID */
1915a91eb199SMark Brown 	switch (source) {
1916a91eb199SMark Brown 	case WM8904_FLL_MCLK:
1917a91eb199SMark Brown 	case WM8904_FLL_LRCLK:
1918a91eb199SMark Brown 	case WM8904_FLL_BCLK:
1919a91eb199SMark Brown 		ret = fll_factors(&fll_div, Fref, Fout);
1920a91eb199SMark Brown 		if (ret != 0)
1921a91eb199SMark Brown 			return ret;
1922a91eb199SMark Brown 		break;
1923a91eb199SMark Brown 
1924a91eb199SMark Brown 	case WM8904_FLL_FREE_RUNNING:
1925a91eb199SMark Brown 		dev_dbg(codec->dev, "Using free running FLL\n");
1926a91eb199SMark Brown 		/* Force 12MHz and output/4 for now */
1927a91eb199SMark Brown 		Fout = 12000000;
1928a91eb199SMark Brown 		Fref = 12000000;
1929a91eb199SMark Brown 
1930a91eb199SMark Brown 		memset(&fll_div, 0, sizeof(fll_div));
1931a91eb199SMark Brown 		fll_div.fll_outdiv = 3;
1932a91eb199SMark Brown 		break;
1933a91eb199SMark Brown 
1934a91eb199SMark Brown 	default:
1935a91eb199SMark Brown 		dev_err(codec->dev, "Unknown FLL ID %d\n", fll_id);
1936a91eb199SMark Brown 		return -EINVAL;
1937a91eb199SMark Brown 	}
1938a91eb199SMark Brown 
1939a91eb199SMark Brown 	/* Save current state then disable the FLL and SYSCLK to avoid
1940a91eb199SMark Brown 	 * misclocking */
1941a91eb199SMark Brown 	fll1 = snd_soc_read(codec, WM8904_FLL_CONTROL_1);
1942a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
1943a91eb199SMark Brown 			    WM8904_CLK_SYS_ENA, 0);
1944a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
1945a91eb199SMark Brown 			    WM8904_FLL_OSC_ENA | WM8904_FLL_ENA, 0);
1946a91eb199SMark Brown 
1947a91eb199SMark Brown 	/* Unlock forced oscilator control to switch it on/off */
1948a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_CONTROL_INTERFACE_TEST_1,
1949a91eb199SMark Brown 			    WM8904_USER_KEY, WM8904_USER_KEY);
1950a91eb199SMark Brown 
1951a91eb199SMark Brown 	if (fll_id == WM8904_FLL_FREE_RUNNING) {
1952a91eb199SMark Brown 		val = WM8904_FLL_FRC_NCO;
1953a91eb199SMark Brown 	} else {
1954a91eb199SMark Brown 		val = 0;
1955a91eb199SMark Brown 	}
1956a91eb199SMark Brown 
1957a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_FLL_NCO_TEST_1, WM8904_FLL_FRC_NCO,
1958a91eb199SMark Brown 			    val);
1959a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_CONTROL_INTERFACE_TEST_1,
1960a91eb199SMark Brown 			    WM8904_USER_KEY, 0);
1961a91eb199SMark Brown 
1962a91eb199SMark Brown 	switch (fll_id) {
1963a91eb199SMark Brown 	case WM8904_FLL_MCLK:
1964a91eb199SMark Brown 		snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
1965a91eb199SMark Brown 				    WM8904_FLL_CLK_REF_SRC_MASK, 0);
1966a91eb199SMark Brown 		break;
1967a91eb199SMark Brown 
1968a91eb199SMark Brown 	case WM8904_FLL_LRCLK:
1969a91eb199SMark Brown 		snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
1970a91eb199SMark Brown 				    WM8904_FLL_CLK_REF_SRC_MASK, 1);
1971a91eb199SMark Brown 		break;
1972a91eb199SMark Brown 
1973a91eb199SMark Brown 	case WM8904_FLL_BCLK:
1974a91eb199SMark Brown 		snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
1975a91eb199SMark Brown 				    WM8904_FLL_CLK_REF_SRC_MASK, 2);
1976a91eb199SMark Brown 		break;
1977a91eb199SMark Brown 	}
1978a91eb199SMark Brown 
1979a91eb199SMark Brown 	if (fll_div.k)
1980a91eb199SMark Brown 		val = WM8904_FLL_FRACN_ENA;
1981a91eb199SMark Brown 	else
1982a91eb199SMark Brown 		val = 0;
1983a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
1984a91eb199SMark Brown 			    WM8904_FLL_FRACN_ENA, val);
1985a91eb199SMark Brown 
1986a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_FLL_CONTROL_2,
1987a91eb199SMark Brown 			    WM8904_FLL_OUTDIV_MASK | WM8904_FLL_FRATIO_MASK,
1988a91eb199SMark Brown 			    (fll_div.fll_outdiv << WM8904_FLL_OUTDIV_SHIFT) |
1989a91eb199SMark Brown 			    (fll_div.fll_fratio << WM8904_FLL_FRATIO_SHIFT));
1990a91eb199SMark Brown 
1991a91eb199SMark Brown 	snd_soc_write(codec, WM8904_FLL_CONTROL_3, fll_div.k);
1992a91eb199SMark Brown 
1993a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_FLL_CONTROL_4, WM8904_FLL_N_MASK,
1994a91eb199SMark Brown 			    fll_div.n << WM8904_FLL_N_SHIFT);
1995a91eb199SMark Brown 
1996a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_FLL_CONTROL_5,
1997a91eb199SMark Brown 			    WM8904_FLL_CLK_REF_DIV_MASK,
1998a91eb199SMark Brown 			    fll_div.fll_clk_ref_div
1999a91eb199SMark Brown 			    << WM8904_FLL_CLK_REF_DIV_SHIFT);
2000a91eb199SMark Brown 
2001a91eb199SMark Brown 	dev_dbg(codec->dev, "FLL configured for %dHz->%dHz\n", Fref, Fout);
2002a91eb199SMark Brown 
2003a91eb199SMark Brown 	wm8904->fll_fref = Fref;
2004a91eb199SMark Brown 	wm8904->fll_fout = Fout;
2005a91eb199SMark Brown 	wm8904->fll_src = source;
2006a91eb199SMark Brown 
2007a91eb199SMark Brown 	/* Enable the FLL if it was previously active */
2008a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
2009a91eb199SMark Brown 			    WM8904_FLL_OSC_ENA, fll1);
2010a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_FLL_CONTROL_1,
2011a91eb199SMark Brown 			    WM8904_FLL_ENA, fll1);
2012a91eb199SMark Brown 
2013a91eb199SMark Brown out:
2014a91eb199SMark Brown 	/* Reenable SYSCLK if it was previously active */
2015a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_CLOCK_RATES_2,
2016a91eb199SMark Brown 			    WM8904_CLK_SYS_ENA, clock2);
2017a91eb199SMark Brown 
2018a91eb199SMark Brown 	return 0;
2019a91eb199SMark Brown }
2020a91eb199SMark Brown 
2021a91eb199SMark Brown static int wm8904_digital_mute(struct snd_soc_dai *codec_dai, int mute)
2022a91eb199SMark Brown {
2023a91eb199SMark Brown 	struct snd_soc_codec *codec = codec_dai->codec;
2024a91eb199SMark Brown 	int val;
2025a91eb199SMark Brown 
2026a91eb199SMark Brown 	if (mute)
2027a91eb199SMark Brown 		val = WM8904_DAC_MUTE;
2028a91eb199SMark Brown 	else
2029a91eb199SMark Brown 		val = 0;
2030a91eb199SMark Brown 
2031a91eb199SMark Brown 	snd_soc_update_bits(codec, WM8904_DAC_DIGITAL_1, WM8904_DAC_MUTE, val);
2032a91eb199SMark Brown 
2033a91eb199SMark Brown 	return 0;
2034a91eb199SMark Brown }
2035a91eb199SMark Brown 
2036*c1334218SMark Brown static void wm8904_sync_cache(struct snd_soc_codec *codec)
2037*c1334218SMark Brown {
2038*c1334218SMark Brown 	struct wm8904_priv *wm8904 = codec->private_data;
2039*c1334218SMark Brown 	int i;
2040*c1334218SMark Brown 
2041*c1334218SMark Brown 	if (!codec->cache_sync)
2042*c1334218SMark Brown 		return;
2043*c1334218SMark Brown 
2044*c1334218SMark Brown 	codec->cache_only = 0;
2045*c1334218SMark Brown 
2046*c1334218SMark Brown 	/* Sync back cached values if they're different from the
2047*c1334218SMark Brown 	 * hardware default.
2048*c1334218SMark Brown 	 */
2049*c1334218SMark Brown 	for (i = 1; i < ARRAY_SIZE(wm8904->reg_cache); i++) {
2050*c1334218SMark Brown 		if (!wm8904_access[i].writable)
2051*c1334218SMark Brown 			continue;
2052*c1334218SMark Brown 
2053*c1334218SMark Brown 		if (wm8904->reg_cache[i] == wm8904_reg[i])
2054*c1334218SMark Brown 			continue;
2055*c1334218SMark Brown 
2056*c1334218SMark Brown 		snd_soc_write(codec, i, wm8904->reg_cache[i]);
2057*c1334218SMark Brown 	}
2058*c1334218SMark Brown 
2059*c1334218SMark Brown 	codec->cache_sync = 0;
2060*c1334218SMark Brown }
2061*c1334218SMark Brown 
2062a91eb199SMark Brown static int wm8904_set_bias_level(struct snd_soc_codec *codec,
2063a91eb199SMark Brown 				 enum snd_soc_bias_level level)
2064a91eb199SMark Brown {
2065a91eb199SMark Brown 	struct wm8904_priv *wm8904 = codec->private_data;
2066*c1334218SMark Brown 	int ret;
2067a91eb199SMark Brown 
2068a91eb199SMark Brown 	switch (level) {
2069a91eb199SMark Brown 	case SND_SOC_BIAS_ON:
2070a91eb199SMark Brown 		break;
2071a91eb199SMark Brown 
2072a91eb199SMark Brown 	case SND_SOC_BIAS_PREPARE:
2073a91eb199SMark Brown 		/* VMID resistance 2*50k */
2074a91eb199SMark Brown 		snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
2075a91eb199SMark Brown 				    WM8904_VMID_RES_MASK,
2076a91eb199SMark Brown 				    0x1 << WM8904_VMID_RES_SHIFT);
2077a91eb199SMark Brown 
2078a91eb199SMark Brown 		/* Normal bias current */
2079a91eb199SMark Brown 		snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
2080a91eb199SMark Brown 				    WM8904_ISEL_MASK, 2 << WM8904_ISEL_SHIFT);
2081a91eb199SMark Brown 		break;
2082a91eb199SMark Brown 
2083a91eb199SMark Brown 	case SND_SOC_BIAS_STANDBY:
2084a91eb199SMark Brown 		if (codec->bias_level == SND_SOC_BIAS_OFF) {
2085a91eb199SMark Brown 			ret = regulator_bulk_enable(ARRAY_SIZE(wm8904->supplies),
2086a91eb199SMark Brown 						    wm8904->supplies);
2087a91eb199SMark Brown 			if (ret != 0) {
2088a91eb199SMark Brown 				dev_err(codec->dev,
2089a91eb199SMark Brown 					"Failed to enable supplies: %d\n",
2090a91eb199SMark Brown 					ret);
2091a91eb199SMark Brown 				return ret;
2092a91eb199SMark Brown 			}
2093a91eb199SMark Brown 
2094*c1334218SMark Brown 			wm8904_sync_cache(codec);
2095a91eb199SMark Brown 
2096a91eb199SMark Brown 			/* Enable bias */
2097a91eb199SMark Brown 			snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
2098a91eb199SMark Brown 					    WM8904_BIAS_ENA, WM8904_BIAS_ENA);
2099a91eb199SMark Brown 
2100a91eb199SMark Brown 			/* Enable VMID, VMID buffering, 2*5k resistance */
2101a91eb199SMark Brown 			snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
2102a91eb199SMark Brown 					    WM8904_VMID_ENA |
2103a91eb199SMark Brown 					    WM8904_VMID_RES_MASK,
2104a91eb199SMark Brown 					    WM8904_VMID_ENA |
2105a91eb199SMark Brown 					    0x3 << WM8904_VMID_RES_SHIFT);
2106a91eb199SMark Brown 
2107a91eb199SMark Brown 			/* Let VMID ramp */
2108a91eb199SMark Brown 			msleep(1);
2109a91eb199SMark Brown 		}
2110a91eb199SMark Brown 
2111a91eb199SMark Brown 		/* Maintain VMID with 2*250k */
2112a91eb199SMark Brown 		snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
2113a91eb199SMark Brown 				    WM8904_VMID_RES_MASK,
2114a91eb199SMark Brown 				    0x2 << WM8904_VMID_RES_SHIFT);
2115a91eb199SMark Brown 
2116a91eb199SMark Brown 		/* Bias current *0.5 */
2117a91eb199SMark Brown 		snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
2118a91eb199SMark Brown 				    WM8904_ISEL_MASK, 0);
2119a91eb199SMark Brown 		break;
2120a91eb199SMark Brown 
2121a91eb199SMark Brown 	case SND_SOC_BIAS_OFF:
2122a91eb199SMark Brown 		/* Turn off VMID */
2123a91eb199SMark Brown 		snd_soc_update_bits(codec, WM8904_VMID_CONTROL_0,
2124a91eb199SMark Brown 				    WM8904_VMID_RES_MASK | WM8904_VMID_ENA, 0);
2125a91eb199SMark Brown 
2126a91eb199SMark Brown 		/* Stop bias generation */
2127a91eb199SMark Brown 		snd_soc_update_bits(codec, WM8904_BIAS_CONTROL_0,
2128a91eb199SMark Brown 				    WM8904_BIAS_ENA, 0);
2129a91eb199SMark Brown 
2130*c1334218SMark Brown #ifdef CONFIG_REGULATOR
2131*c1334218SMark Brown 		/* Post 2.6.34 we will be able to get a callback when
2132*c1334218SMark Brown 		 * the regulators are disabled which we can use but
2133*c1334218SMark Brown 		 * for now just assume that the power will be cut if
2134*c1334218SMark Brown 		 * the regulator API is in use.
2135*c1334218SMark Brown 		 */
2136*c1334218SMark Brown 		codec->cache_sync = 1;
2137*c1334218SMark Brown #endif
2138*c1334218SMark Brown 
2139a91eb199SMark Brown 		regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies),
2140a91eb199SMark Brown 				       wm8904->supplies);
2141a91eb199SMark Brown 		break;
2142a91eb199SMark Brown 	}
2143a91eb199SMark Brown 	codec->bias_level = level;
2144a91eb199SMark Brown 	return 0;
2145a91eb199SMark Brown }
2146a91eb199SMark Brown 
2147a91eb199SMark Brown #define WM8904_RATES SNDRV_PCM_RATE_8000_96000
2148a91eb199SMark Brown 
2149a91eb199SMark Brown #define WM8904_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
2150a91eb199SMark Brown 			SNDRV_PCM_FMTBIT_S24_LE | SNDRV_PCM_FMTBIT_S32_LE)
2151a91eb199SMark Brown 
2152a91eb199SMark Brown static struct snd_soc_dai_ops wm8904_dai_ops = {
2153a91eb199SMark Brown 	.set_sysclk = wm8904_set_sysclk,
2154a91eb199SMark Brown 	.set_fmt = wm8904_set_fmt,
2155a91eb199SMark Brown 	.set_tdm_slot = wm8904_set_tdm_slot,
2156a91eb199SMark Brown 	.set_pll = wm8904_set_fll,
2157a91eb199SMark Brown 	.hw_params = wm8904_hw_params,
2158a91eb199SMark Brown 	.digital_mute = wm8904_digital_mute,
2159a91eb199SMark Brown };
2160a91eb199SMark Brown 
2161a91eb199SMark Brown struct snd_soc_dai wm8904_dai = {
2162a91eb199SMark Brown 	.name = "WM8904",
2163a91eb199SMark Brown 	.playback = {
2164a91eb199SMark Brown 		.stream_name = "Playback",
2165a91eb199SMark Brown 		.channels_min = 2,
2166a91eb199SMark Brown 		.channels_max = 2,
2167a91eb199SMark Brown 		.rates = WM8904_RATES,
2168a91eb199SMark Brown 		.formats = WM8904_FORMATS,
2169a91eb199SMark Brown 	},
2170a91eb199SMark Brown 	.capture = {
2171a91eb199SMark Brown 		.stream_name = "Capture",
2172a91eb199SMark Brown 		.channels_min = 2,
2173a91eb199SMark Brown 		.channels_max = 2,
2174a91eb199SMark Brown 		.rates = WM8904_RATES,
2175a91eb199SMark Brown 		.formats = WM8904_FORMATS,
2176a91eb199SMark Brown 	},
2177a91eb199SMark Brown 	.ops = &wm8904_dai_ops,
2178a91eb199SMark Brown 	.symmetric_rates = 1,
2179a91eb199SMark Brown };
2180a91eb199SMark Brown EXPORT_SYMBOL_GPL(wm8904_dai);
2181a91eb199SMark Brown 
2182a91eb199SMark Brown #ifdef CONFIG_PM
2183a91eb199SMark Brown static int wm8904_suspend(struct platform_device *pdev, pm_message_t state)
2184a91eb199SMark Brown {
2185a91eb199SMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
2186a91eb199SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
2187a91eb199SMark Brown 
2188a91eb199SMark Brown 	wm8904_set_bias_level(codec, SND_SOC_BIAS_OFF);
2189a91eb199SMark Brown 
2190a91eb199SMark Brown 	return 0;
2191a91eb199SMark Brown }
2192a91eb199SMark Brown 
2193a91eb199SMark Brown static int wm8904_resume(struct platform_device *pdev)
2194a91eb199SMark Brown {
2195a91eb199SMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
2196a91eb199SMark Brown 	struct snd_soc_codec *codec = socdev->card->codec;
2197a91eb199SMark Brown 
2198a91eb199SMark Brown 	wm8904_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
2199a91eb199SMark Brown 
2200a91eb199SMark Brown 	return 0;
2201a91eb199SMark Brown }
2202a91eb199SMark Brown #else
2203a91eb199SMark Brown #define wm8904_suspend NULL
2204a91eb199SMark Brown #define wm8904_resume NULL
2205a91eb199SMark Brown #endif
2206a91eb199SMark Brown 
2207a91eb199SMark Brown static void wm8904_handle_retune_mobile_pdata(struct wm8904_priv *wm8904)
2208a91eb199SMark Brown {
2209a91eb199SMark Brown 	struct snd_soc_codec *codec = &wm8904->codec;
2210a91eb199SMark Brown 	struct wm8904_pdata *pdata = wm8904->pdata;
2211a91eb199SMark Brown 	struct snd_kcontrol_new control =
2212a91eb199SMark Brown 		SOC_ENUM_EXT("EQ Mode",
2213a91eb199SMark Brown 			     wm8904->retune_mobile_enum,
2214a91eb199SMark Brown 			     wm8904_get_retune_mobile_enum,
2215a91eb199SMark Brown 			     wm8904_put_retune_mobile_enum);
2216a91eb199SMark Brown 	int ret, i, j;
2217a91eb199SMark Brown 	const char **t;
2218a91eb199SMark Brown 
2219a91eb199SMark Brown 	/* We need an array of texts for the enum API but the number
2220a91eb199SMark Brown 	 * of texts is likely to be less than the number of
2221a91eb199SMark Brown 	 * configurations due to the sample rate dependency of the
2222a91eb199SMark Brown 	 * configurations. */
2223a91eb199SMark Brown 	wm8904->num_retune_mobile_texts = 0;
2224a91eb199SMark Brown 	wm8904->retune_mobile_texts = NULL;
2225a91eb199SMark Brown 	for (i = 0; i < pdata->num_retune_mobile_cfgs; i++) {
2226a91eb199SMark Brown 		for (j = 0; j < wm8904->num_retune_mobile_texts; j++) {
2227a91eb199SMark Brown 			if (strcmp(pdata->retune_mobile_cfgs[i].name,
2228a91eb199SMark Brown 				   wm8904->retune_mobile_texts[j]) == 0)
2229a91eb199SMark Brown 				break;
2230a91eb199SMark Brown 		}
2231a91eb199SMark Brown 
2232a91eb199SMark Brown 		if (j != wm8904->num_retune_mobile_texts)
2233a91eb199SMark Brown 			continue;
2234a91eb199SMark Brown 
2235a91eb199SMark Brown 		/* Expand the array... */
2236a91eb199SMark Brown 		t = krealloc(wm8904->retune_mobile_texts,
2237a91eb199SMark Brown 			     sizeof(char *) *
2238a91eb199SMark Brown 			     (wm8904->num_retune_mobile_texts + 1),
2239a91eb199SMark Brown 			     GFP_KERNEL);
2240a91eb199SMark Brown 		if (t == NULL)
2241a91eb199SMark Brown 			continue;
2242a91eb199SMark Brown 
2243a91eb199SMark Brown 		/* ...store the new entry... */
2244a91eb199SMark Brown 		t[wm8904->num_retune_mobile_texts] =
2245a91eb199SMark Brown 			pdata->retune_mobile_cfgs[i].name;
2246a91eb199SMark Brown 
2247a91eb199SMark Brown 		/* ...and remember the new version. */
2248a91eb199SMark Brown 		wm8904->num_retune_mobile_texts++;
2249a91eb199SMark Brown 		wm8904->retune_mobile_texts = t;
2250a91eb199SMark Brown 	}
2251a91eb199SMark Brown 
2252a91eb199SMark Brown 	dev_dbg(codec->dev, "Allocated %d unique ReTune Mobile names\n",
2253a91eb199SMark Brown 		wm8904->num_retune_mobile_texts);
2254a91eb199SMark Brown 
2255a91eb199SMark Brown 	wm8904->retune_mobile_enum.max = wm8904->num_retune_mobile_texts;
2256a91eb199SMark Brown 	wm8904->retune_mobile_enum.texts = wm8904->retune_mobile_texts;
2257a91eb199SMark Brown 
2258a91eb199SMark Brown 	ret = snd_soc_add_controls(&wm8904->codec, &control, 1);
2259a91eb199SMark Brown 	if (ret != 0)
2260a91eb199SMark Brown 		dev_err(wm8904->codec.dev,
2261a91eb199SMark Brown 			"Failed to add ReTune Mobile control: %d\n", ret);
2262a91eb199SMark Brown }
2263a91eb199SMark Brown 
2264a91eb199SMark Brown static void wm8904_handle_pdata(struct wm8904_priv *wm8904)
2265a91eb199SMark Brown {
2266a91eb199SMark Brown 	struct snd_soc_codec *codec = &wm8904->codec;
2267a91eb199SMark Brown 	struct wm8904_pdata *pdata = wm8904->pdata;
2268a91eb199SMark Brown 	int ret, i;
2269a91eb199SMark Brown 
2270a91eb199SMark Brown 	if (!pdata) {
2271a91eb199SMark Brown 		snd_soc_add_controls(&wm8904->codec, wm8904_eq_controls,
2272a91eb199SMark Brown 				     ARRAY_SIZE(wm8904_eq_controls));
2273a91eb199SMark Brown 		return;
2274a91eb199SMark Brown 	}
2275a91eb199SMark Brown 
2276a91eb199SMark Brown 	dev_dbg(codec->dev, "%d DRC configurations\n", pdata->num_drc_cfgs);
2277a91eb199SMark Brown 
2278a91eb199SMark Brown 	if (pdata->num_drc_cfgs) {
2279a91eb199SMark Brown 		struct snd_kcontrol_new control =
2280a91eb199SMark Brown 			SOC_ENUM_EXT("DRC Mode", wm8904->drc_enum,
2281a91eb199SMark Brown 				     wm8904_get_drc_enum, wm8904_put_drc_enum);
2282a91eb199SMark Brown 
2283a91eb199SMark Brown 		/* We need an array of texts for the enum API */
2284a91eb199SMark Brown 		wm8904->drc_texts = kmalloc(sizeof(char *)
2285a91eb199SMark Brown 					    * pdata->num_drc_cfgs, GFP_KERNEL);
2286a91eb199SMark Brown 		if (!wm8904->drc_texts) {
2287a91eb199SMark Brown 			dev_err(wm8904->codec.dev,
2288a91eb199SMark Brown 				"Failed to allocate %d DRC config texts\n",
2289a91eb199SMark Brown 				pdata->num_drc_cfgs);
2290a91eb199SMark Brown 			return;
2291a91eb199SMark Brown 		}
2292a91eb199SMark Brown 
2293a91eb199SMark Brown 		for (i = 0; i < pdata->num_drc_cfgs; i++)
2294a91eb199SMark Brown 			wm8904->drc_texts[i] = pdata->drc_cfgs[i].name;
2295a91eb199SMark Brown 
2296a91eb199SMark Brown 		wm8904->drc_enum.max = pdata->num_drc_cfgs;
2297a91eb199SMark Brown 		wm8904->drc_enum.texts = wm8904->drc_texts;
2298a91eb199SMark Brown 
2299a91eb199SMark Brown 		ret = snd_soc_add_controls(&wm8904->codec, &control, 1);
2300a91eb199SMark Brown 		if (ret != 0)
2301a91eb199SMark Brown 			dev_err(wm8904->codec.dev,
2302a91eb199SMark Brown 				"Failed to add DRC mode control: %d\n", ret);
2303a91eb199SMark Brown 
2304a91eb199SMark Brown 		wm8904_set_drc(codec);
2305a91eb199SMark Brown 	}
2306a91eb199SMark Brown 
2307a91eb199SMark Brown 	dev_dbg(codec->dev, "%d ReTune Mobile configurations\n",
2308a91eb199SMark Brown 		pdata->num_retune_mobile_cfgs);
2309a91eb199SMark Brown 
2310a91eb199SMark Brown 	if (pdata->num_retune_mobile_cfgs)
2311a91eb199SMark Brown 		wm8904_handle_retune_mobile_pdata(wm8904);
2312a91eb199SMark Brown 	else
2313a91eb199SMark Brown 		snd_soc_add_controls(&wm8904->codec, wm8904_eq_controls,
2314a91eb199SMark Brown 				     ARRAY_SIZE(wm8904_eq_controls));
2315a91eb199SMark Brown }
2316a91eb199SMark Brown 
2317a91eb199SMark Brown static int wm8904_probe(struct platform_device *pdev)
2318a91eb199SMark Brown {
2319a91eb199SMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
2320a91eb199SMark Brown 	struct snd_soc_codec *codec;
2321a91eb199SMark Brown 	int ret = 0;
2322a91eb199SMark Brown 
2323a91eb199SMark Brown 	if (wm8904_codec == NULL) {
2324a91eb199SMark Brown 		dev_err(&pdev->dev, "Codec device not registered\n");
2325a91eb199SMark Brown 		return -ENODEV;
2326a91eb199SMark Brown 	}
2327a91eb199SMark Brown 
2328a91eb199SMark Brown 	socdev->card->codec = wm8904_codec;
2329a91eb199SMark Brown 	codec = wm8904_codec;
2330a91eb199SMark Brown 
2331a91eb199SMark Brown 	/* register pcms */
2332a91eb199SMark Brown 	ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
2333a91eb199SMark Brown 	if (ret < 0) {
2334a91eb199SMark Brown 		dev_err(codec->dev, "failed to create pcms: %d\n", ret);
2335a91eb199SMark Brown 		goto pcm_err;
2336a91eb199SMark Brown 	}
2337a91eb199SMark Brown 
2338a91eb199SMark Brown 	wm8904_handle_pdata(codec->private_data);
2339a91eb199SMark Brown 
2340a91eb199SMark Brown 	wm8904_add_widgets(codec);
2341a91eb199SMark Brown 
2342a91eb199SMark Brown 	return ret;
2343a91eb199SMark Brown 
2344a91eb199SMark Brown pcm_err:
2345a91eb199SMark Brown 	return ret;
2346a91eb199SMark Brown }
2347a91eb199SMark Brown 
2348a91eb199SMark Brown static int wm8904_remove(struct platform_device *pdev)
2349a91eb199SMark Brown {
2350a91eb199SMark Brown 	struct snd_soc_device *socdev = platform_get_drvdata(pdev);
2351a91eb199SMark Brown 
2352a91eb199SMark Brown 	snd_soc_free_pcms(socdev);
2353a91eb199SMark Brown 	snd_soc_dapm_free(socdev);
2354a91eb199SMark Brown 
2355a91eb199SMark Brown 	return 0;
2356a91eb199SMark Brown }
2357a91eb199SMark Brown 
2358a91eb199SMark Brown struct snd_soc_codec_device soc_codec_dev_wm8904 = {
2359a91eb199SMark Brown 	.probe = 	wm8904_probe,
2360a91eb199SMark Brown 	.remove = 	wm8904_remove,
2361a91eb199SMark Brown 	.suspend = 	wm8904_suspend,
2362a91eb199SMark Brown 	.resume =	wm8904_resume,
2363a91eb199SMark Brown };
2364a91eb199SMark Brown EXPORT_SYMBOL_GPL(soc_codec_dev_wm8904);
2365a91eb199SMark Brown 
2366a91eb199SMark Brown static int wm8904_register(struct wm8904_priv *wm8904,
2367a91eb199SMark Brown 			   enum snd_soc_control_type control)
2368a91eb199SMark Brown {
2369a91eb199SMark Brown 	int ret;
2370a91eb199SMark Brown 	struct snd_soc_codec *codec = &wm8904->codec;
2371a91eb199SMark Brown 	int i;
2372a91eb199SMark Brown 
2373a91eb199SMark Brown 	if (wm8904_codec) {
2374a91eb199SMark Brown 		dev_err(codec->dev, "Another WM8904 is registered\n");
2375a91eb199SMark Brown 		return -EINVAL;
2376a91eb199SMark Brown 	}
2377a91eb199SMark Brown 
2378a91eb199SMark Brown 	mutex_init(&codec->mutex);
2379a91eb199SMark Brown 	INIT_LIST_HEAD(&codec->dapm_widgets);
2380a91eb199SMark Brown 	INIT_LIST_HEAD(&codec->dapm_paths);
2381a91eb199SMark Brown 
2382a91eb199SMark Brown 	codec->private_data = wm8904;
2383a91eb199SMark Brown 	codec->name = "WM8904";
2384a91eb199SMark Brown 	codec->owner = THIS_MODULE;
2385a91eb199SMark Brown 	codec->bias_level = SND_SOC_BIAS_OFF;
2386a91eb199SMark Brown 	codec->set_bias_level = wm8904_set_bias_level;
2387a91eb199SMark Brown 	codec->dai = &wm8904_dai;
2388a91eb199SMark Brown 	codec->num_dai = 1;
2389a91eb199SMark Brown 	codec->reg_cache_size = WM8904_MAX_REGISTER;
2390a91eb199SMark Brown 	codec->reg_cache = &wm8904->reg_cache;
2391a91eb199SMark Brown 	codec->volatile_register = wm8904_volatile_register;
2392*c1334218SMark Brown 	codec->cache_sync = 1;
2393*c1334218SMark Brown 	codec->idle_bias_off = 1;
2394a91eb199SMark Brown 
2395a91eb199SMark Brown 	memcpy(codec->reg_cache, wm8904_reg, sizeof(wm8904_reg));
2396a91eb199SMark Brown 
2397a91eb199SMark Brown 	ret = snd_soc_codec_set_cache_io(codec, 8, 16, control);
2398a91eb199SMark Brown 	if (ret != 0) {
2399a91eb199SMark Brown 		dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
2400a91eb199SMark Brown 		goto err;
2401a91eb199SMark Brown 	}
2402a91eb199SMark Brown 
2403a91eb199SMark Brown 	for (i = 0; i < ARRAY_SIZE(wm8904->supplies); i++)
2404a91eb199SMark Brown 		wm8904->supplies[i].supply = wm8904_supply_names[i];
2405a91eb199SMark Brown 
2406a91eb199SMark Brown 	ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8904->supplies),
2407a91eb199SMark Brown 				 wm8904->supplies);
2408a91eb199SMark Brown 	if (ret != 0) {
2409a91eb199SMark Brown 		dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
2410a91eb199SMark Brown 		goto err;
2411a91eb199SMark Brown 	}
2412a91eb199SMark Brown 
2413a91eb199SMark Brown 	ret = regulator_bulk_enable(ARRAY_SIZE(wm8904->supplies),
2414a91eb199SMark Brown 				    wm8904->supplies);
2415a91eb199SMark Brown 	if (ret != 0) {
2416a91eb199SMark Brown 		dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
2417a91eb199SMark Brown 		goto err_get;
2418a91eb199SMark Brown 	}
2419a91eb199SMark Brown 
2420a91eb199SMark Brown 	ret = snd_soc_read(codec, WM8904_SW_RESET_AND_ID);
2421a91eb199SMark Brown 	if (ret < 0) {
2422a91eb199SMark Brown 		dev_err(codec->dev, "Failed to read ID register\n");
2423a91eb199SMark Brown 		goto err_enable;
2424a91eb199SMark Brown 	}
2425a91eb199SMark Brown 	if (ret != wm8904_reg[WM8904_SW_RESET_AND_ID]) {
2426a91eb199SMark Brown 		dev_err(codec->dev, "Device is not a WM8904, ID is %x\n", ret);
2427a91eb199SMark Brown 		ret = -EINVAL;
2428a91eb199SMark Brown 		goto err_enable;
2429a91eb199SMark Brown 	}
2430a91eb199SMark Brown 
2431a91eb199SMark Brown 	ret = snd_soc_read(codec, WM8904_REVISION);
2432a91eb199SMark Brown 	if (ret < 0) {
2433a91eb199SMark Brown 		dev_err(codec->dev, "Failed to read device revision: %d\n",
2434a91eb199SMark Brown 			ret);
2435a91eb199SMark Brown 		goto err_enable;
2436a91eb199SMark Brown 	}
2437a91eb199SMark Brown 	dev_info(codec->dev, "revision %c\n", ret + 'A');
2438a91eb199SMark Brown 
2439a91eb199SMark Brown 	ret = wm8904_reset(codec);
2440a91eb199SMark Brown 	if (ret < 0) {
2441a91eb199SMark Brown 		dev_err(codec->dev, "Failed to issue reset\n");
2442a91eb199SMark Brown 		goto err_enable;
2443a91eb199SMark Brown 	}
2444a91eb199SMark Brown 
2445a91eb199SMark Brown 	wm8904_dai.dev = codec->dev;
2446a91eb199SMark Brown 
2447a91eb199SMark Brown 	/* Change some default settings - latch VU and enable ZC */
2448a91eb199SMark Brown 	wm8904->reg_cache[WM8904_ADC_DIGITAL_VOLUME_LEFT] |= WM8904_ADC_VU;
2449a91eb199SMark Brown 	wm8904->reg_cache[WM8904_ADC_DIGITAL_VOLUME_RIGHT] |= WM8904_ADC_VU;
2450a91eb199SMark Brown 	wm8904->reg_cache[WM8904_DAC_DIGITAL_VOLUME_LEFT] |= WM8904_DAC_VU;
2451a91eb199SMark Brown 	wm8904->reg_cache[WM8904_DAC_DIGITAL_VOLUME_RIGHT] |= WM8904_DAC_VU;
2452a91eb199SMark Brown 	wm8904->reg_cache[WM8904_ANALOGUE_OUT1_LEFT] |= WM8904_HPOUT_VU |
2453a91eb199SMark Brown 		WM8904_HPOUTLZC;
2454a91eb199SMark Brown 	wm8904->reg_cache[WM8904_ANALOGUE_OUT1_RIGHT] |= WM8904_HPOUT_VU |
2455a91eb199SMark Brown 		WM8904_HPOUTRZC;
2456a91eb199SMark Brown 	wm8904->reg_cache[WM8904_ANALOGUE_OUT2_LEFT] |= WM8904_LINEOUT_VU |
2457a91eb199SMark Brown 		WM8904_LINEOUTLZC;
2458a91eb199SMark Brown 	wm8904->reg_cache[WM8904_ANALOGUE_OUT2_RIGHT] |= WM8904_LINEOUT_VU |
2459a91eb199SMark Brown 		WM8904_LINEOUTRZC;
2460a91eb199SMark Brown 	wm8904->reg_cache[WM8904_CLOCK_RATES_0] &= ~WM8904_SR_MODE;
2461a91eb199SMark Brown 
2462a91eb199SMark Brown 	/* Set Class W by default - this will be managed by the Class
2463a91eb199SMark Brown 	 * G widget at runtime where bypass paths are available.
2464a91eb199SMark Brown 	 */
2465a91eb199SMark Brown 	wm8904->reg_cache[WM8904_CLASS_W_0] |= WM8904_CP_DYN_PWR;
2466a91eb199SMark Brown 
2467a91eb199SMark Brown 	/* Use normal bias source */
2468a91eb199SMark Brown 	wm8904->reg_cache[WM8904_BIAS_CONTROL_0] &= ~WM8904_POBCTRL;
2469a91eb199SMark Brown 
2470a91eb199SMark Brown 	wm8904_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
2471a91eb199SMark Brown 
2472a91eb199SMark Brown 	/* Bias level configuration will have done an extra enable */
2473a91eb199SMark Brown 	regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
2474a91eb199SMark Brown 
2475a91eb199SMark Brown 	wm8904_codec = codec;
2476a91eb199SMark Brown 
2477a91eb199SMark Brown 	ret = snd_soc_register_codec(codec);
2478a91eb199SMark Brown 	if (ret != 0) {
2479a91eb199SMark Brown 		dev_err(codec->dev, "Failed to register codec: %d\n", ret);
2480a91eb199SMark Brown 		return ret;
2481a91eb199SMark Brown 	}
2482a91eb199SMark Brown 
2483a91eb199SMark Brown 	ret = snd_soc_register_dai(&wm8904_dai);
2484a91eb199SMark Brown 	if (ret != 0) {
2485a91eb199SMark Brown 		dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
2486a91eb199SMark Brown 		snd_soc_unregister_codec(codec);
2487a91eb199SMark Brown 		return ret;
2488a91eb199SMark Brown 	}
2489a91eb199SMark Brown 
2490a91eb199SMark Brown 	return 0;
2491a91eb199SMark Brown 
2492a91eb199SMark Brown err_enable:
2493a91eb199SMark Brown 	regulator_bulk_disable(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
2494a91eb199SMark Brown err_get:
2495a91eb199SMark Brown 	regulator_bulk_free(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
2496a91eb199SMark Brown err:
2497a91eb199SMark Brown 	kfree(wm8904);
2498a91eb199SMark Brown 	return ret;
2499a91eb199SMark Brown }
2500a91eb199SMark Brown 
2501a91eb199SMark Brown static void wm8904_unregister(struct wm8904_priv *wm8904)
2502a91eb199SMark Brown {
2503a91eb199SMark Brown 	wm8904_set_bias_level(&wm8904->codec, SND_SOC_BIAS_OFF);
2504a91eb199SMark Brown 	regulator_bulk_free(ARRAY_SIZE(wm8904->supplies), wm8904->supplies);
2505a91eb199SMark Brown 	snd_soc_unregister_dai(&wm8904_dai);
2506a91eb199SMark Brown 	snd_soc_unregister_codec(&wm8904->codec);
2507a91eb199SMark Brown 	kfree(wm8904);
2508a91eb199SMark Brown 	wm8904_codec = NULL;
2509a91eb199SMark Brown }
2510a91eb199SMark Brown 
2511a91eb199SMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
2512a91eb199SMark Brown static __devinit int wm8904_i2c_probe(struct i2c_client *i2c,
2513a91eb199SMark Brown 				      const struct i2c_device_id *id)
2514a91eb199SMark Brown {
2515a91eb199SMark Brown 	struct wm8904_priv *wm8904;
2516a91eb199SMark Brown 	struct snd_soc_codec *codec;
2517a91eb199SMark Brown 
2518a91eb199SMark Brown 	wm8904 = kzalloc(sizeof(struct wm8904_priv), GFP_KERNEL);
2519a91eb199SMark Brown 	if (wm8904 == NULL)
2520a91eb199SMark Brown 		return -ENOMEM;
2521a91eb199SMark Brown 
2522a91eb199SMark Brown 	codec = &wm8904->codec;
2523a91eb199SMark Brown 	codec->hw_write = (hw_write_t)i2c_master_send;
2524a91eb199SMark Brown 
2525a91eb199SMark Brown 	i2c_set_clientdata(i2c, wm8904);
2526a91eb199SMark Brown 	codec->control_data = i2c;
2527a91eb199SMark Brown 	wm8904->pdata = i2c->dev.platform_data;
2528a91eb199SMark Brown 
2529a91eb199SMark Brown 	codec->dev = &i2c->dev;
2530a91eb199SMark Brown 
2531a91eb199SMark Brown 	return wm8904_register(wm8904, SND_SOC_I2C);
2532a91eb199SMark Brown }
2533a91eb199SMark Brown 
2534a91eb199SMark Brown static __devexit int wm8904_i2c_remove(struct i2c_client *client)
2535a91eb199SMark Brown {
2536a91eb199SMark Brown 	struct wm8904_priv *wm8904 = i2c_get_clientdata(client);
2537a91eb199SMark Brown 	wm8904_unregister(wm8904);
2538a91eb199SMark Brown 	return 0;
2539a91eb199SMark Brown }
2540a91eb199SMark Brown 
2541a91eb199SMark Brown static const struct i2c_device_id wm8904_i2c_id[] = {
2542a91eb199SMark Brown 	{ "wm8904", 0 },
2543a91eb199SMark Brown 	{ }
2544a91eb199SMark Brown };
2545a91eb199SMark Brown MODULE_DEVICE_TABLE(i2c, wm8904_i2c_id);
2546a91eb199SMark Brown 
2547a91eb199SMark Brown static struct i2c_driver wm8904_i2c_driver = {
2548a91eb199SMark Brown 	.driver = {
2549a91eb199SMark Brown 		.name = "WM8904",
2550a91eb199SMark Brown 		.owner = THIS_MODULE,
2551a91eb199SMark Brown 	},
2552a91eb199SMark Brown 	.probe =    wm8904_i2c_probe,
2553a91eb199SMark Brown 	.remove =   __devexit_p(wm8904_i2c_remove),
2554a91eb199SMark Brown 	.id_table = wm8904_i2c_id,
2555a91eb199SMark Brown };
2556a91eb199SMark Brown #endif
2557a91eb199SMark Brown 
2558a91eb199SMark Brown static int __init wm8904_modinit(void)
2559a91eb199SMark Brown {
2560a91eb199SMark Brown 	int ret;
2561a91eb199SMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
2562a91eb199SMark Brown 	ret = i2c_add_driver(&wm8904_i2c_driver);
2563a91eb199SMark Brown 	if (ret != 0) {
2564a91eb199SMark Brown 		printk(KERN_ERR "Failed to register WM8904 I2C driver: %d\n",
2565a91eb199SMark Brown 		       ret);
2566a91eb199SMark Brown 	}
2567a91eb199SMark Brown #endif
2568a91eb199SMark Brown 	return 0;
2569a91eb199SMark Brown }
2570a91eb199SMark Brown module_init(wm8904_modinit);
2571a91eb199SMark Brown 
2572a91eb199SMark Brown static void __exit wm8904_exit(void)
2573a91eb199SMark Brown {
2574a91eb199SMark Brown #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
2575a91eb199SMark Brown 	i2c_del_driver(&wm8904_i2c_driver);
2576a91eb199SMark Brown #endif
2577a91eb199SMark Brown }
2578a91eb199SMark Brown module_exit(wm8904_exit);
2579a91eb199SMark Brown 
2580a91eb199SMark Brown MODULE_DESCRIPTION("ASoC WM8904 driver");
2581a91eb199SMark Brown MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
2582a91eb199SMark Brown MODULE_LICENSE("GPL");
2583