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