2 * wm8988.c -- WM8988 ALSA SoC audio driver
4 * Copyright 2009 Wolfson Microelectronics plc
5 * Copyright 2005 Openedhand Ltd.
7 * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
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.
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
19 #include <linux/i2c.h>
20 #include <linux/spi/spi.h>
21 #include <linux/slab.h>
22 #include <sound/core.h>
23 #include <sound/pcm.h>
24 #include <sound/pcm_params.h>
25 #include <sound/tlv.h>
26 #include <sound/soc.h>
27 #include <sound/initval.h>
32 * wm8988 register cache
33 * We can't read the WM8988 register space when we
34 * are using 2 wire for device control, so we cache them instead.
36 static const u16 wm8988_reg
[] = {
37 0x0097, 0x0097, 0x0079, 0x0079, /* 0 */
38 0x0000, 0x0008, 0x0000, 0x000a, /* 4 */
39 0x0000, 0x0000, 0x00ff, 0x00ff, /* 8 */
40 0x000f, 0x000f, 0x0000, 0x0000, /* 12 */
41 0x0000, 0x007b, 0x0000, 0x0032, /* 16 */
42 0x0000, 0x00c3, 0x00c3, 0x00c0, /* 20 */
43 0x0000, 0x0000, 0x0000, 0x0000, /* 24 */
44 0x0000, 0x0000, 0x0000, 0x0000, /* 28 */
45 0x0000, 0x0000, 0x0050, 0x0050, /* 32 */
46 0x0050, 0x0050, 0x0050, 0x0050, /* 36 */
47 0x0079, 0x0079, 0x0079, /* 40 */
50 /* codec private data */
53 enum snd_soc_control_type control_type
;
54 struct snd_pcm_hw_constraint_list
*sysclk_constraints
;
57 #define wm8988_reset(c) snd_soc_write(c, WM8988_RESET, 0)
63 static const char *bass_boost_txt
[] = {"Linear Control", "Adaptive Boost"};
64 static const struct soc_enum bass_boost
=
65 SOC_ENUM_SINGLE(WM8988_BASS
, 7, 2, bass_boost_txt
);
67 static const char *bass_filter_txt
[] = { "130Hz @ 48kHz", "200Hz @ 48kHz" };
68 static const struct soc_enum bass_filter
=
69 SOC_ENUM_SINGLE(WM8988_BASS
, 6, 2, bass_filter_txt
);
71 static const char *treble_txt
[] = {"8kHz", "4kHz"};
72 static const struct soc_enum treble
=
73 SOC_ENUM_SINGLE(WM8988_TREBLE
, 6, 2, treble_txt
);
75 static const char *stereo_3d_lc_txt
[] = {"200Hz", "500Hz"};
76 static const struct soc_enum stereo_3d_lc
=
77 SOC_ENUM_SINGLE(WM8988_3D
, 5, 2, stereo_3d_lc_txt
);
79 static const char *stereo_3d_uc_txt
[] = {"2.2kHz", "1.5kHz"};
80 static const struct soc_enum stereo_3d_uc
=
81 SOC_ENUM_SINGLE(WM8988_3D
, 6, 2, stereo_3d_uc_txt
);
83 static const char *stereo_3d_func_txt
[] = {"Capture", "Playback"};
84 static const struct soc_enum stereo_3d_func
=
85 SOC_ENUM_SINGLE(WM8988_3D
, 7, 2, stereo_3d_func_txt
);
87 static const char *alc_func_txt
[] = {"Off", "Right", "Left", "Stereo"};
88 static const struct soc_enum alc_func
=
89 SOC_ENUM_SINGLE(WM8988_ALC1
, 7, 4, alc_func_txt
);
91 static const char *ng_type_txt
[] = {"Constant PGA Gain",
93 static const struct soc_enum ng_type
=
94 SOC_ENUM_SINGLE(WM8988_NGATE
, 1, 2, ng_type_txt
);
96 static const char *deemph_txt
[] = {"None", "32Khz", "44.1Khz", "48Khz"};
97 static const struct soc_enum deemph
=
98 SOC_ENUM_SINGLE(WM8988_ADCDAC
, 1, 4, deemph_txt
);
100 static const char *adcpol_txt
[] = {"Normal", "L Invert", "R Invert",
102 static const struct soc_enum adcpol
=
103 SOC_ENUM_SINGLE(WM8988_ADCDAC
, 5, 4, adcpol_txt
);
105 static const DECLARE_TLV_DB_SCALE(pga_tlv
, -1725, 75, 0);
106 static const DECLARE_TLV_DB_SCALE(adc_tlv
, -9750, 50, 1);
107 static const DECLARE_TLV_DB_SCALE(dac_tlv
, -12750, 50, 1);
108 static const DECLARE_TLV_DB_SCALE(out_tlv
, -12100, 100, 1);
109 static const DECLARE_TLV_DB_SCALE(bypass_tlv
, -1500, 300, 0);
111 static const struct snd_kcontrol_new wm8988_snd_controls
[] = {
113 SOC_ENUM("Bass Boost", bass_boost
),
114 SOC_ENUM("Bass Filter", bass_filter
),
115 SOC_SINGLE("Bass Volume", WM8988_BASS
, 0, 15, 1),
117 SOC_SINGLE("Treble Volume", WM8988_TREBLE
, 0, 15, 0),
118 SOC_ENUM("Treble Cut-off", treble
),
120 SOC_SINGLE("3D Switch", WM8988_3D
, 0, 1, 0),
121 SOC_SINGLE("3D Volume", WM8988_3D
, 1, 15, 0),
122 SOC_ENUM("3D Lower Cut-off", stereo_3d_lc
),
123 SOC_ENUM("3D Upper Cut-off", stereo_3d_uc
),
124 SOC_ENUM("3D Mode", stereo_3d_func
),
126 SOC_SINGLE("ALC Capture Target Volume", WM8988_ALC1
, 0, 7, 0),
127 SOC_SINGLE("ALC Capture Max Volume", WM8988_ALC1
, 4, 7, 0),
128 SOC_ENUM("ALC Capture Function", alc_func
),
129 SOC_SINGLE("ALC Capture ZC Switch", WM8988_ALC2
, 7, 1, 0),
130 SOC_SINGLE("ALC Capture Hold Time", WM8988_ALC2
, 0, 15, 0),
131 SOC_SINGLE("ALC Capture Decay Time", WM8988_ALC3
, 4, 15, 0),
132 SOC_SINGLE("ALC Capture Attack Time", WM8988_ALC3
, 0, 15, 0),
133 SOC_SINGLE("ALC Capture NG Threshold", WM8988_NGATE
, 3, 31, 0),
134 SOC_ENUM("ALC Capture NG Type", ng_type
),
135 SOC_SINGLE("ALC Capture NG Switch", WM8988_NGATE
, 0, 1, 0),
137 SOC_SINGLE("ZC Timeout Switch", WM8988_ADCTL1
, 0, 1, 0),
139 SOC_DOUBLE_R_TLV("Capture Digital Volume", WM8988_LADC
, WM8988_RADC
,
141 SOC_DOUBLE_R_TLV("Capture Volume", WM8988_LINVOL
, WM8988_RINVOL
,
143 SOC_DOUBLE_R("Capture ZC Switch", WM8988_LINVOL
, WM8988_RINVOL
, 6, 1, 0),
144 SOC_DOUBLE_R("Capture Switch", WM8988_LINVOL
, WM8988_RINVOL
, 7, 1, 1),
146 SOC_ENUM("Playback De-emphasis", deemph
),
148 SOC_ENUM("Capture Polarity", adcpol
),
149 SOC_SINGLE("Playback 6dB Attenuate", WM8988_ADCDAC
, 7, 1, 0),
150 SOC_SINGLE("Capture 6dB Attenuate", WM8988_ADCDAC
, 8, 1, 0),
152 SOC_DOUBLE_R_TLV("PCM Volume", WM8988_LDAC
, WM8988_RDAC
, 0, 255, 0, dac_tlv
),
154 SOC_SINGLE_TLV("Left Mixer Left Bypass Volume", WM8988_LOUTM1
, 4, 7, 1,
156 SOC_SINGLE_TLV("Left Mixer Right Bypass Volume", WM8988_LOUTM2
, 4, 7, 1,
158 SOC_SINGLE_TLV("Right Mixer Left Bypass Volume", WM8988_ROUTM1
, 4, 7, 1,
160 SOC_SINGLE_TLV("Right Mixer Right Bypass Volume", WM8988_ROUTM2
, 4, 7, 1,
163 SOC_DOUBLE_R("Output 1 Playback ZC Switch", WM8988_LOUT1V
,
164 WM8988_ROUT1V
, 7, 1, 0),
165 SOC_DOUBLE_R_TLV("Output 1 Playback Volume", WM8988_LOUT1V
, WM8988_ROUT1V
,
168 SOC_DOUBLE_R("Output 2 Playback ZC Switch", WM8988_LOUT2V
,
169 WM8988_ROUT2V
, 7, 1, 0),
170 SOC_DOUBLE_R_TLV("Output 2 Playback Volume", WM8988_LOUT2V
, WM8988_ROUT2V
,
179 static int wm8988_lrc_control(struct snd_soc_dapm_widget
*w
,
180 struct snd_kcontrol
*kcontrol
, int event
)
182 struct snd_soc_codec
*codec
= w
->codec
;
183 u16 adctl2
= snd_soc_read(codec
, WM8988_ADCTL2
);
185 /* Use the DAC to gate LRC if active, otherwise use ADC */
186 if (snd_soc_read(codec
, WM8988_PWR2
) & 0x180)
191 return snd_soc_write(codec
, WM8988_ADCTL2
, adctl2
);
194 static const char *wm8988_line_texts
[] = {
195 "Line 1", "Line 2", "PGA", "Differential"};
197 static const unsigned int wm8988_line_values
[] = {
200 static const struct soc_enum wm8988_lline_enum
=
201 SOC_VALUE_ENUM_SINGLE(WM8988_LOUTM1
, 0, 7,
202 ARRAY_SIZE(wm8988_line_texts
),
205 static const struct snd_kcontrol_new wm8988_left_line_controls
=
206 SOC_DAPM_VALUE_ENUM("Route", wm8988_lline_enum
);
208 static const struct soc_enum wm8988_rline_enum
=
209 SOC_VALUE_ENUM_SINGLE(WM8988_ROUTM1
, 0, 7,
210 ARRAY_SIZE(wm8988_line_texts
),
213 static const struct snd_kcontrol_new wm8988_right_line_controls
=
214 SOC_DAPM_VALUE_ENUM("Route", wm8988_lline_enum
);
217 static const struct snd_kcontrol_new wm8988_left_mixer_controls
[] = {
218 SOC_DAPM_SINGLE("Playback Switch", WM8988_LOUTM1
, 8, 1, 0),
219 SOC_DAPM_SINGLE("Left Bypass Switch", WM8988_LOUTM1
, 7, 1, 0),
220 SOC_DAPM_SINGLE("Right Playback Switch", WM8988_LOUTM2
, 8, 1, 0),
221 SOC_DAPM_SINGLE("Right Bypass Switch", WM8988_LOUTM2
, 7, 1, 0),
225 static const struct snd_kcontrol_new wm8988_right_mixer_controls
[] = {
226 SOC_DAPM_SINGLE("Left Playback Switch", WM8988_ROUTM1
, 8, 1, 0),
227 SOC_DAPM_SINGLE("Left Bypass Switch", WM8988_ROUTM1
, 7, 1, 0),
228 SOC_DAPM_SINGLE("Playback Switch", WM8988_ROUTM2
, 8, 1, 0),
229 SOC_DAPM_SINGLE("Right Bypass Switch", WM8988_ROUTM2
, 7, 1, 0),
232 static const char *wm8988_pga_sel
[] = {"Line 1", "Line 2", "Differential"};
233 static const unsigned int wm8988_pga_val
[] = { 0, 1, 3 };
236 static const struct soc_enum wm8988_lpga_enum
=
237 SOC_VALUE_ENUM_SINGLE(WM8988_LADCIN
, 6, 3,
238 ARRAY_SIZE(wm8988_pga_sel
),
241 static const struct snd_kcontrol_new wm8988_left_pga_controls
=
242 SOC_DAPM_VALUE_ENUM("Route", wm8988_lpga_enum
);
245 static const struct soc_enum wm8988_rpga_enum
=
246 SOC_VALUE_ENUM_SINGLE(WM8988_RADCIN
, 6, 3,
247 ARRAY_SIZE(wm8988_pga_sel
),
250 static const struct snd_kcontrol_new wm8988_right_pga_controls
=
251 SOC_DAPM_VALUE_ENUM("Route", wm8988_rpga_enum
);
253 /* Differential Mux */
254 static const char *wm8988_diff_sel
[] = {"Line 1", "Line 2"};
255 static const struct soc_enum diffmux
=
256 SOC_ENUM_SINGLE(WM8988_ADCIN
, 8, 2, wm8988_diff_sel
);
257 static const struct snd_kcontrol_new wm8988_diffmux_controls
=
258 SOC_DAPM_ENUM("Route", diffmux
);
261 static const char *wm8988_mono_mux
[] = {"Stereo", "Mono (Left)",
262 "Mono (Right)", "Digital Mono"};
263 static const struct soc_enum monomux
=
264 SOC_ENUM_SINGLE(WM8988_ADCIN
, 6, 4, wm8988_mono_mux
);
265 static const struct snd_kcontrol_new wm8988_monomux_controls
=
266 SOC_DAPM_ENUM("Route", monomux
);
268 static const struct snd_soc_dapm_widget wm8988_dapm_widgets
[] = {
269 SND_SOC_DAPM_SUPPLY("Mic Bias", WM8988_PWR1
, 1, 0, NULL
, 0),
271 SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM
, 0, 0,
272 &wm8988_diffmux_controls
),
273 SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM
, 0, 0,
274 &wm8988_monomux_controls
),
275 SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM
, 0, 0,
276 &wm8988_monomux_controls
),
278 SND_SOC_DAPM_MUX("Left PGA Mux", WM8988_PWR1
, 5, 0,
279 &wm8988_left_pga_controls
),
280 SND_SOC_DAPM_MUX("Right PGA Mux", WM8988_PWR1
, 4, 0,
281 &wm8988_right_pga_controls
),
283 SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM
, 0, 0,
284 &wm8988_left_line_controls
),
285 SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM
, 0, 0,
286 &wm8988_right_line_controls
),
288 SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8988_PWR1
, 2, 0),
289 SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8988_PWR1
, 3, 0),
291 SND_SOC_DAPM_DAC("Right DAC", "Right Playback", WM8988_PWR2
, 7, 0),
292 SND_SOC_DAPM_DAC("Left DAC", "Left Playback", WM8988_PWR2
, 8, 0),
294 SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM
, 0, 0,
295 &wm8988_left_mixer_controls
[0],
296 ARRAY_SIZE(wm8988_left_mixer_controls
)),
297 SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM
, 0, 0,
298 &wm8988_right_mixer_controls
[0],
299 ARRAY_SIZE(wm8988_right_mixer_controls
)),
301 SND_SOC_DAPM_PGA("Right Out 2", WM8988_PWR2
, 3, 0, NULL
, 0),
302 SND_SOC_DAPM_PGA("Left Out 2", WM8988_PWR2
, 4, 0, NULL
, 0),
303 SND_SOC_DAPM_PGA("Right Out 1", WM8988_PWR2
, 5, 0, NULL
, 0),
304 SND_SOC_DAPM_PGA("Left Out 1", WM8988_PWR2
, 6, 0, NULL
, 0),
306 SND_SOC_DAPM_POST("LRC control", wm8988_lrc_control
),
308 SND_SOC_DAPM_OUTPUT("LOUT1"),
309 SND_SOC_DAPM_OUTPUT("ROUT1"),
310 SND_SOC_DAPM_OUTPUT("LOUT2"),
311 SND_SOC_DAPM_OUTPUT("ROUT2"),
312 SND_SOC_DAPM_OUTPUT("VREF"),
314 SND_SOC_DAPM_INPUT("LINPUT1"),
315 SND_SOC_DAPM_INPUT("LINPUT2"),
316 SND_SOC_DAPM_INPUT("RINPUT1"),
317 SND_SOC_DAPM_INPUT("RINPUT2"),
320 static const struct snd_soc_dapm_route audio_map
[] = {
322 { "Left Line Mux", "Line 1", "LINPUT1" },
323 { "Left Line Mux", "Line 2", "LINPUT2" },
324 { "Left Line Mux", "PGA", "Left PGA Mux" },
325 { "Left Line Mux", "Differential", "Differential Mux" },
327 { "Right Line Mux", "Line 1", "RINPUT1" },
328 { "Right Line Mux", "Line 2", "RINPUT2" },
329 { "Right Line Mux", "PGA", "Right PGA Mux" },
330 { "Right Line Mux", "Differential", "Differential Mux" },
332 { "Left PGA Mux", "Line 1", "LINPUT1" },
333 { "Left PGA Mux", "Line 2", "LINPUT2" },
334 { "Left PGA Mux", "Differential", "Differential Mux" },
336 { "Right PGA Mux", "Line 1", "RINPUT1" },
337 { "Right PGA Mux", "Line 2", "RINPUT2" },
338 { "Right PGA Mux", "Differential", "Differential Mux" },
340 { "Differential Mux", "Line 1", "LINPUT1" },
341 { "Differential Mux", "Line 1", "RINPUT1" },
342 { "Differential Mux", "Line 2", "LINPUT2" },
343 { "Differential Mux", "Line 2", "RINPUT2" },
345 { "Left ADC Mux", "Stereo", "Left PGA Mux" },
346 { "Left ADC Mux", "Mono (Left)", "Left PGA Mux" },
347 { "Left ADC Mux", "Digital Mono", "Left PGA Mux" },
349 { "Right ADC Mux", "Stereo", "Right PGA Mux" },
350 { "Right ADC Mux", "Mono (Right)", "Right PGA Mux" },
351 { "Right ADC Mux", "Digital Mono", "Right PGA Mux" },
353 { "Left ADC", NULL
, "Left ADC Mux" },
354 { "Right ADC", NULL
, "Right ADC Mux" },
356 { "Left Line Mux", "Line 1", "LINPUT1" },
357 { "Left Line Mux", "Line 2", "LINPUT2" },
358 { "Left Line Mux", "PGA", "Left PGA Mux" },
359 { "Left Line Mux", "Differential", "Differential Mux" },
361 { "Right Line Mux", "Line 1", "RINPUT1" },
362 { "Right Line Mux", "Line 2", "RINPUT2" },
363 { "Right Line Mux", "PGA", "Right PGA Mux" },
364 { "Right Line Mux", "Differential", "Differential Mux" },
366 { "Left Mixer", "Playback Switch", "Left DAC" },
367 { "Left Mixer", "Left Bypass Switch", "Left Line Mux" },
368 { "Left Mixer", "Right Playback Switch", "Right DAC" },
369 { "Left Mixer", "Right Bypass Switch", "Right Line Mux" },
371 { "Right Mixer", "Left Playback Switch", "Left DAC" },
372 { "Right Mixer", "Left Bypass Switch", "Left Line Mux" },
373 { "Right Mixer", "Playback Switch", "Right DAC" },
374 { "Right Mixer", "Right Bypass Switch", "Right Line Mux" },
376 { "Left Out 1", NULL
, "Left Mixer" },
377 { "LOUT1", NULL
, "Left Out 1" },
378 { "Right Out 1", NULL
, "Right Mixer" },
379 { "ROUT1", NULL
, "Right Out 1" },
381 { "Left Out 2", NULL
, "Left Mixer" },
382 { "LOUT2", NULL
, "Left Out 2" },
383 { "Right Out 2", NULL
, "Right Mixer" },
384 { "ROUT2", NULL
, "Right Out 2" },
395 /* codec hifi mclk clock divider coefficients */
396 static const struct _coeff_div coeff_div
[] = {
398 {12288000, 8000, 1536, 0x6, 0x0},
399 {11289600, 8000, 1408, 0x16, 0x0},
400 {18432000, 8000, 2304, 0x7, 0x0},
401 {16934400, 8000, 2112, 0x17, 0x0},
402 {12000000, 8000, 1500, 0x6, 0x1},
405 {11289600, 11025, 1024, 0x18, 0x0},
406 {16934400, 11025, 1536, 0x19, 0x0},
407 {12000000, 11025, 1088, 0x19, 0x1},
410 {12288000, 16000, 768, 0xa, 0x0},
411 {18432000, 16000, 1152, 0xb, 0x0},
412 {12000000, 16000, 750, 0xa, 0x1},
415 {11289600, 22050, 512, 0x1a, 0x0},
416 {16934400, 22050, 768, 0x1b, 0x0},
417 {12000000, 22050, 544, 0x1b, 0x1},
420 {12288000, 32000, 384, 0xc, 0x0},
421 {18432000, 32000, 576, 0xd, 0x0},
422 {12000000, 32000, 375, 0xa, 0x1},
425 {11289600, 44100, 256, 0x10, 0x0},
426 {16934400, 44100, 384, 0x11, 0x0},
427 {12000000, 44100, 272, 0x11, 0x1},
430 {12288000, 48000, 256, 0x0, 0x0},
431 {18432000, 48000, 384, 0x1, 0x0},
432 {12000000, 48000, 250, 0x0, 0x1},
435 {11289600, 88200, 128, 0x1e, 0x0},
436 {16934400, 88200, 192, 0x1f, 0x0},
437 {12000000, 88200, 136, 0x1f, 0x1},
440 {12288000, 96000, 128, 0xe, 0x0},
441 {18432000, 96000, 192, 0xf, 0x0},
442 {12000000, 96000, 125, 0xe, 0x1},
445 static inline int get_coeff(int mclk
, int rate
)
449 for (i
= 0; i
< ARRAY_SIZE(coeff_div
); i
++) {
450 if (coeff_div
[i
].rate
== rate
&& coeff_div
[i
].mclk
== mclk
)
457 /* The set of rates we can generate from the above for each SYSCLK */
459 static unsigned int rates_12288
[] = {
460 8000, 12000, 16000, 24000, 24000, 32000, 48000, 96000,
463 static struct snd_pcm_hw_constraint_list constraints_12288
= {
464 .count
= ARRAY_SIZE(rates_12288
),
468 static unsigned int rates_112896
[] = {
469 8000, 11025, 22050, 44100,
472 static struct snd_pcm_hw_constraint_list constraints_112896
= {
473 .count
= ARRAY_SIZE(rates_112896
),
474 .list
= rates_112896
,
477 static unsigned int rates_12
[] = {
478 8000, 11025, 12000, 16000, 22050, 2400, 32000, 41100, 48000,
482 static struct snd_pcm_hw_constraint_list constraints_12
= {
483 .count
= ARRAY_SIZE(rates_12
),
488 * Note that this should be called from init rather than from hw_params.
490 static int wm8988_set_dai_sysclk(struct snd_soc_dai
*codec_dai
,
491 int clk_id
, unsigned int freq
, int dir
)
493 struct snd_soc_codec
*codec
= codec_dai
->codec
;
494 struct wm8988_priv
*wm8988
= snd_soc_codec_get_drvdata(codec
);
501 wm8988
->sysclk_constraints
= &constraints_112896
;
502 wm8988
->sysclk
= freq
;
509 wm8988
->sysclk_constraints
= &constraints_12288
;
510 wm8988
->sysclk
= freq
;
515 wm8988
->sysclk_constraints
= &constraints_12
;
516 wm8988
->sysclk
= freq
;
522 static int wm8988_set_dai_fmt(struct snd_soc_dai
*codec_dai
,
525 struct snd_soc_codec
*codec
= codec_dai
->codec
;
528 /* set master/slave audio interface */
529 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
530 case SND_SOC_DAIFMT_CBM_CFM
:
533 case SND_SOC_DAIFMT_CBS_CFS
:
539 /* interface format */
540 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
541 case SND_SOC_DAIFMT_I2S
:
544 case SND_SOC_DAIFMT_RIGHT_J
:
546 case SND_SOC_DAIFMT_LEFT_J
:
549 case SND_SOC_DAIFMT_DSP_A
:
552 case SND_SOC_DAIFMT_DSP_B
:
559 /* clock inversion */
560 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
561 case SND_SOC_DAIFMT_NB_NF
:
563 case SND_SOC_DAIFMT_IB_IF
:
566 case SND_SOC_DAIFMT_IB_NF
:
569 case SND_SOC_DAIFMT_NB_IF
:
576 snd_soc_write(codec
, WM8988_IFACE
, iface
);
580 static int wm8988_pcm_startup(struct snd_pcm_substream
*substream
,
581 struct snd_soc_dai
*dai
)
583 struct snd_soc_codec
*codec
= dai
->codec
;
584 struct wm8988_priv
*wm8988
= snd_soc_codec_get_drvdata(codec
);
586 /* The set of sample rates that can be supported depends on the
587 * MCLK supplied to the CODEC - enforce this.
589 if (!wm8988
->sysclk
) {
591 "No MCLK configured, call set_sysclk() on init\n");
595 snd_pcm_hw_constraint_list(substream
->runtime
, 0,
596 SNDRV_PCM_HW_PARAM_RATE
,
597 wm8988
->sysclk_constraints
);
602 static int wm8988_pcm_hw_params(struct snd_pcm_substream
*substream
,
603 struct snd_pcm_hw_params
*params
,
604 struct snd_soc_dai
*dai
)
606 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
607 struct snd_soc_codec
*codec
= rtd
->codec
;
608 struct wm8988_priv
*wm8988
= snd_soc_codec_get_drvdata(codec
);
609 u16 iface
= snd_soc_read(codec
, WM8988_IFACE
) & 0x1f3;
610 u16 srate
= snd_soc_read(codec
, WM8988_SRATE
) & 0x180;
613 coeff
= get_coeff(wm8988
->sysclk
, params_rate(params
));
615 coeff
= get_coeff(wm8988
->sysclk
/ 2, params_rate(params
));
620 "Unable to configure sample rate %dHz with %dHz MCLK\n",
621 params_rate(params
), wm8988
->sysclk
);
626 switch (params_format(params
)) {
627 case SNDRV_PCM_FORMAT_S16_LE
:
629 case SNDRV_PCM_FORMAT_S20_3LE
:
632 case SNDRV_PCM_FORMAT_S24_LE
:
635 case SNDRV_PCM_FORMAT_S32_LE
:
640 /* set iface & srate */
641 snd_soc_write(codec
, WM8988_IFACE
, iface
);
643 snd_soc_write(codec
, WM8988_SRATE
, srate
|
644 (coeff_div
[coeff
].sr
<< 1) | coeff_div
[coeff
].usb
);
649 static int wm8988_mute(struct snd_soc_dai
*dai
, int mute
)
651 struct snd_soc_codec
*codec
= dai
->codec
;
652 u16 mute_reg
= snd_soc_read(codec
, WM8988_ADCDAC
) & 0xfff7;
655 snd_soc_write(codec
, WM8988_ADCDAC
, mute_reg
| 0x8);
657 snd_soc_write(codec
, WM8988_ADCDAC
, mute_reg
);
661 static int wm8988_set_bias_level(struct snd_soc_codec
*codec
,
662 enum snd_soc_bias_level level
)
664 u16 pwr_reg
= snd_soc_read(codec
, WM8988_PWR1
) & ~0x1c1;
667 case SND_SOC_BIAS_ON
:
670 case SND_SOC_BIAS_PREPARE
:
671 /* VREF, VMID=2x50k, digital enabled */
672 snd_soc_write(codec
, WM8988_PWR1
, pwr_reg
| 0x00c0);
675 case SND_SOC_BIAS_STANDBY
:
676 if (codec
->dapm
.bias_level
== SND_SOC_BIAS_OFF
) {
677 snd_soc_cache_sync(codec
);
679 /* VREF, VMID=2x5k */
680 snd_soc_write(codec
, WM8988_PWR1
, pwr_reg
| 0x1c1);
686 /* VREF, VMID=2*500k, digital stopped */
687 snd_soc_write(codec
, WM8988_PWR1
, pwr_reg
| 0x0141);
690 case SND_SOC_BIAS_OFF
:
691 snd_soc_write(codec
, WM8988_PWR1
, 0x0000);
694 codec
->dapm
.bias_level
= level
;
698 #define WM8988_RATES SNDRV_PCM_RATE_8000_96000
700 #define WM8988_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
701 SNDRV_PCM_FMTBIT_S24_LE)
703 static const struct snd_soc_dai_ops wm8988_ops
= {
704 .startup
= wm8988_pcm_startup
,
705 .hw_params
= wm8988_pcm_hw_params
,
706 .set_fmt
= wm8988_set_dai_fmt
,
707 .set_sysclk
= wm8988_set_dai_sysclk
,
708 .digital_mute
= wm8988_mute
,
711 static struct snd_soc_dai_driver wm8988_dai
= {
712 .name
= "wm8988-hifi",
714 .stream_name
= "Playback",
717 .rates
= WM8988_RATES
,
718 .formats
= WM8988_FORMATS
,
721 .stream_name
= "Capture",
724 .rates
= WM8988_RATES
,
725 .formats
= WM8988_FORMATS
,
728 .symmetric_rates
= 1,
731 static int wm8988_suspend(struct snd_soc_codec
*codec
)
733 wm8988_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
737 static int wm8988_resume(struct snd_soc_codec
*codec
)
739 wm8988_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
743 static int wm8988_probe(struct snd_soc_codec
*codec
)
745 struct wm8988_priv
*wm8988
= snd_soc_codec_get_drvdata(codec
);
746 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
749 ret
= snd_soc_codec_set_cache_io(codec
, 7, 9, wm8988
->control_type
);
751 dev_err(codec
->dev
, "Failed to set cache I/O: %d\n", ret
);
755 ret
= wm8988_reset(codec
);
757 dev_err(codec
->dev
, "Failed to issue reset\n");
761 /* set the update bits (we always update left then right) */
762 snd_soc_update_bits(codec
, WM8988_RADC
, 0x0100, 0x0100);
763 snd_soc_update_bits(codec
, WM8988_RDAC
, 0x0100, 0x0100);
764 snd_soc_update_bits(codec
, WM8988_ROUT1V
, 0x0100, 0x0100);
765 snd_soc_update_bits(codec
, WM8988_ROUT2V
, 0x0100, 0x0100);
766 snd_soc_update_bits(codec
, WM8988_RINVOL
, 0x0100, 0x0100);
768 wm8988_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
770 snd_soc_add_controls(codec
, wm8988_snd_controls
,
771 ARRAY_SIZE(wm8988_snd_controls
));
772 snd_soc_dapm_new_controls(dapm
, wm8988_dapm_widgets
,
773 ARRAY_SIZE(wm8988_dapm_widgets
));
774 snd_soc_dapm_add_routes(dapm
, audio_map
, ARRAY_SIZE(audio_map
));
779 static int wm8988_remove(struct snd_soc_codec
*codec
)
781 wm8988_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
785 static struct snd_soc_codec_driver soc_codec_dev_wm8988
= {
786 .probe
= wm8988_probe
,
787 .remove
= wm8988_remove
,
788 .suspend
= wm8988_suspend
,
789 .resume
= wm8988_resume
,
790 .set_bias_level
= wm8988_set_bias_level
,
791 .reg_cache_size
= ARRAY_SIZE(wm8988_reg
),
792 .reg_word_size
= sizeof(u16
),
793 .reg_cache_default
= wm8988_reg
,
796 #if defined(CONFIG_SPI_MASTER)
797 static int __devinit
wm8988_spi_probe(struct spi_device
*spi
)
799 struct wm8988_priv
*wm8988
;
802 wm8988
= kzalloc(sizeof(struct wm8988_priv
), GFP_KERNEL
);
806 wm8988
->control_type
= SND_SOC_SPI
;
807 spi_set_drvdata(spi
, wm8988
);
809 ret
= snd_soc_register_codec(&spi
->dev
,
810 &soc_codec_dev_wm8988
, &wm8988_dai
, 1);
816 static int __devexit
wm8988_spi_remove(struct spi_device
*spi
)
818 snd_soc_unregister_codec(&spi
->dev
);
819 kfree(spi_get_drvdata(spi
));
823 static struct spi_driver wm8988_spi_driver
= {
826 .owner
= THIS_MODULE
,
828 .probe
= wm8988_spi_probe
,
829 .remove
= __devexit_p(wm8988_spi_remove
),
831 #endif /* CONFIG_SPI_MASTER */
833 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
834 static __devinit
int wm8988_i2c_probe(struct i2c_client
*i2c
,
835 const struct i2c_device_id
*id
)
837 struct wm8988_priv
*wm8988
;
840 wm8988
= kzalloc(sizeof(struct wm8988_priv
), GFP_KERNEL
);
844 i2c_set_clientdata(i2c
, wm8988
);
845 wm8988
->control_type
= SND_SOC_I2C
;
847 ret
= snd_soc_register_codec(&i2c
->dev
,
848 &soc_codec_dev_wm8988
, &wm8988_dai
, 1);
854 static __devexit
int wm8988_i2c_remove(struct i2c_client
*client
)
856 snd_soc_unregister_codec(&client
->dev
);
857 kfree(i2c_get_clientdata(client
));
861 static const struct i2c_device_id wm8988_i2c_id
[] = {
865 MODULE_DEVICE_TABLE(i2c
, wm8988_i2c_id
);
867 static struct i2c_driver wm8988_i2c_driver
= {
869 .name
= "wm8988-codec",
870 .owner
= THIS_MODULE
,
872 .probe
= wm8988_i2c_probe
,
873 .remove
= __devexit_p(wm8988_i2c_remove
),
874 .id_table
= wm8988_i2c_id
,
878 static int __init
wm8988_modinit(void)
881 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
882 ret
= i2c_add_driver(&wm8988_i2c_driver
);
884 printk(KERN_ERR
"Failed to register WM8988 I2C driver: %d\n",
888 #if defined(CONFIG_SPI_MASTER)
889 ret
= spi_register_driver(&wm8988_spi_driver
);
891 printk(KERN_ERR
"Failed to register WM8988 SPI driver: %d\n",
897 module_init(wm8988_modinit
);
899 static void __exit
wm8988_exit(void)
901 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
902 i2c_del_driver(&wm8988_i2c_driver
);
904 #if defined(CONFIG_SPI_MASTER)
905 spi_unregister_driver(&wm8988_spi_driver
);
908 module_exit(wm8988_exit
);
911 MODULE_DESCRIPTION("ASoC WM8988 driver");
912 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
913 MODULE_LICENSE("GPL");