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