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/platform_device.h>
22 #include <linux/slab.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/tlv.h>
27 #include <sound/soc.h>
28 #include <sound/soc-dapm.h>
29 #include <sound/initval.h>
31 #include <mach/iomux.h>
32 #include <mach/gpio.h>
38 #define DBG(x...) printk(KERN_INFO x)
40 #define DBG(x...) do { } while (0)
44 * wm8988 register cache
45 * We can't read the WM8988 register space when we
46 * are using 2 wire for device control, so we cache them instead.
48 static const u16 wm8988_reg[] = {
49 0x0097, 0x0097, 0x0079, 0x0079, /* 0 */
50 0x0000, 0x0008, 0x0000, 0x000a, /* 4 */
51 0x0000, 0x0000, 0x00ff, 0x00ff, /* 8 */
52 0x000f, 0x000f, 0x0000, 0x0000, /* 12 */
53 0x0000, 0x007b, 0x0000, 0x0032, /* 16 */
54 0x0000, 0x00c3, 0x00c3, 0x00c0, /* 20 */
55 0x0000, 0x0000, 0x0000, 0x0000, /* 24 */
56 0x0000, 0x0000, 0x0000, 0x0000, /* 28 */
57 0x0000, 0x0000, 0x0050, 0x0050, /* 32 */
58 0x0050, 0x0050, 0x0050, 0x0050, /* 36 */
59 0x0079, 0x0079, 0x0079, /* 40 */
62 /* codec private data */
65 struct snd_soc_codec codec;
66 struct snd_pcm_hw_constraint_list *sysclk_constraints;
67 u16 reg_cache[WM8988_NUM_REG];
71 #define wm8988_reset(c) snd_soc_write(c, WM8988_RESET, 0)
77 static const char *bass_boost_txt[] = {"Linear Control", "Adaptive Boost"};
78 static const struct soc_enum bass_boost =
79 SOC_ENUM_SINGLE(WM8988_BASS, 7, 2, bass_boost_txt);
81 static const char *bass_filter_txt[] = { "130Hz @ 48kHz", "200Hz @ 48kHz" };
82 static const struct soc_enum bass_filter =
83 SOC_ENUM_SINGLE(WM8988_BASS, 6, 2, bass_filter_txt);
85 static const char *treble_txt[] = {"8kHz", "4kHz"};
86 static const struct soc_enum treble =
87 SOC_ENUM_SINGLE(WM8988_TREBLE, 6, 2, treble_txt);
89 static const char *stereo_3d_lc_txt[] = {"200Hz", "500Hz"};
90 static const struct soc_enum stereo_3d_lc =
91 SOC_ENUM_SINGLE(WM8988_3D, 5, 2, stereo_3d_lc_txt);
93 static const char *stereo_3d_uc_txt[] = {"2.2kHz", "1.5kHz"};
94 static const struct soc_enum stereo_3d_uc =
95 SOC_ENUM_SINGLE(WM8988_3D, 6, 2, stereo_3d_uc_txt);
97 static const char *stereo_3d_func_txt[] = {"Capture", "Playback"};
98 static const struct soc_enum stereo_3d_func =
99 SOC_ENUM_SINGLE(WM8988_3D, 7, 2, stereo_3d_func_txt);
101 static const char *alc_func_txt[] = {"Off", "Right", "Left", "Stereo"};
102 static const struct soc_enum alc_func =
103 SOC_ENUM_SINGLE(WM8988_ALC1, 7, 4, alc_func_txt);
105 static const char *ng_type_txt[] = {"Constant PGA Gain",
107 static const struct soc_enum ng_type =
108 SOC_ENUM_SINGLE(WM8988_NGATE, 1, 2, ng_type_txt);
110 static const char *deemph_txt[] = {"None", "32Khz", "44.1Khz", "48Khz"};
111 static const struct soc_enum deemph =
112 SOC_ENUM_SINGLE(WM8988_ADCDAC, 1, 4, deemph_txt);
114 static const char *adcpol_txt[] = {"Normal", "L Invert", "R Invert",
116 static const struct soc_enum adcpol =
117 SOC_ENUM_SINGLE(WM8988_ADCDAC, 5, 4, adcpol_txt);
119 static const DECLARE_TLV_DB_SCALE(pga_tlv, -1725, 75, 0);
120 static const DECLARE_TLV_DB_SCALE(adc_tlv, -9750, 50, 1);
121 static const DECLARE_TLV_DB_SCALE(dac_tlv, -12750, 50, 1);
122 static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
123 static const DECLARE_TLV_DB_SCALE(bypass_tlv, -1500, 300, 0);
125 static const struct snd_kcontrol_new wm8988_snd_controls[] = {
127 SOC_ENUM("Bass Boost", bass_boost),
128 SOC_ENUM("Bass Filter", bass_filter),
129 SOC_SINGLE("Bass Volume", WM8988_BASS, 0, 15, 1),
131 SOC_SINGLE("Treble Volume", WM8988_TREBLE, 0, 15, 0),
132 SOC_ENUM("Treble Cut-off", treble),
134 SOC_SINGLE("3D Switch", WM8988_3D, 0, 1, 0),
135 SOC_SINGLE("3D Volume", WM8988_3D, 1, 15, 0),
136 SOC_ENUM("3D Lower Cut-off", stereo_3d_lc),
137 SOC_ENUM("3D Upper Cut-off", stereo_3d_uc),
138 SOC_ENUM("3D Mode", stereo_3d_func),
140 SOC_SINGLE("ALC Capture Target Volume", WM8988_ALC1, 0, 7, 0),
141 SOC_SINGLE("ALC Capture Max Volume", WM8988_ALC1, 4, 7, 0),
142 SOC_ENUM("ALC Capture Function", alc_func),
143 SOC_SINGLE("ALC Capture ZC Switch", WM8988_ALC2, 7, 1, 0),
144 SOC_SINGLE("ALC Capture Hold Time", WM8988_ALC2, 0, 15, 0),
145 SOC_SINGLE("ALC Capture Decay Time", WM8988_ALC3, 4, 15, 0),
146 SOC_SINGLE("ALC Capture Attack Time", WM8988_ALC3, 0, 15, 0),
147 SOC_SINGLE("ALC Capture NG Threshold", WM8988_NGATE, 3, 31, 0),
148 SOC_ENUM("ALC Capture NG Type", ng_type),
149 SOC_SINGLE("ALC Capture NG Switch", WM8988_NGATE, 0, 1, 0),
151 SOC_SINGLE("ZC Timeout Switch", WM8988_ADCTL1, 0, 1, 0),
153 SOC_DOUBLE_R_TLV("Capture Digital Volume", WM8988_LADC, WM8988_RADC,
155 SOC_DOUBLE_R_TLV("Capture Volume", WM8988_LINVOL, WM8988_RINVOL,
157 SOC_DOUBLE_R("Capture ZC Switch", WM8988_LINVOL, WM8988_RINVOL, 6, 1, 0),
158 SOC_DOUBLE_R("Capture Switch", WM8988_LINVOL, WM8988_RINVOL, 7, 1, 1),
160 SOC_ENUM("Playback De-emphasis", deemph),
162 SOC_ENUM("Capture Polarity", adcpol),
163 SOC_SINGLE("Playback 6dB Attenuate", WM8988_ADCDAC, 7, 1, 0),
164 SOC_SINGLE("Capture 6dB Attenuate", WM8988_ADCDAC, 8, 1, 0),
166 SOC_DOUBLE_R_TLV("PCM Volume", WM8988_LDAC, WM8988_RDAC, 0, 255, 0, dac_tlv),
168 SOC_SINGLE_TLV("Left Mixer Left Bypass Volume", WM8988_LOUTM1, 4, 7, 1,
170 SOC_SINGLE_TLV("Left Mixer Right Bypass Volume", WM8988_LOUTM2, 4, 7, 1,
172 SOC_SINGLE_TLV("Right Mixer Left Bypass Volume", WM8988_ROUTM1, 4, 7, 1,
174 SOC_SINGLE_TLV("Right Mixer Right Bypass Volume", WM8988_ROUTM2, 4, 7, 1,
177 SOC_DOUBLE_R("Output 1 Playback ZC Switch", WM8988_LOUT1V,
178 WM8988_ROUT1V, 7, 1, 0),
179 SOC_DOUBLE_R_TLV("Output 1 Playback Volume", WM8988_LOUT1V, WM8988_ROUT1V,
182 SOC_DOUBLE_R("Output 2 Playback ZC Switch", WM8988_LOUT2V,
183 WM8988_ROUT2V, 7, 1, 0),
184 SOC_DOUBLE_R_TLV("Output 2 Playback Volume", WM8988_LOUT2V, WM8988_ROUT2V,
193 static int wm8988_lrc_control(struct snd_soc_dapm_widget *w,
194 struct snd_kcontrol *kcontrol, int event)
196 struct snd_soc_codec *codec = w->codec;
197 u16 adctl2 = snd_soc_read(codec, WM8988_ADCTL2);
199 /* Use the DAC to gate LRC if active, otherwise use ADC */
200 if (snd_soc_read(codec, WM8988_PWR2) & 0x180)
205 DBG("Enter::%s----%d, adctl2 = %x\n",__FUNCTION__,__LINE__,adctl2);
207 return snd_soc_write(codec, WM8988_ADCTL2, adctl2);
210 static const char *wm8988_line_texts[] = {
211 "Line 1", "Line 2", "PGA", "Differential"};
213 static const unsigned int wm8988_line_values[] = {
216 static const struct soc_enum wm8988_lline_enum =
217 SOC_VALUE_ENUM_SINGLE(WM8988_LOUTM1, 0, 7,
218 ARRAY_SIZE(wm8988_line_texts),
221 static const struct snd_kcontrol_new wm8988_left_line_controls =
222 SOC_DAPM_VALUE_ENUM("Route", wm8988_lline_enum);
224 static const struct soc_enum wm8988_rline_enum =
225 SOC_VALUE_ENUM_SINGLE(WM8988_ROUTM1, 0, 7,
226 ARRAY_SIZE(wm8988_line_texts),
229 static const struct snd_kcontrol_new wm8988_right_line_controls =
230 SOC_DAPM_VALUE_ENUM("Route", wm8988_lline_enum);
233 static const struct snd_kcontrol_new wm8988_left_mixer_controls[] = {
234 SOC_DAPM_SINGLE("Playback Switch", WM8988_LOUTM1, 8, 1, 0),
235 SOC_DAPM_SINGLE("Left Bypass Switch", WM8988_LOUTM1, 7, 1, 0),
236 SOC_DAPM_SINGLE("Right Playback Switch", WM8988_LOUTM2, 8, 1, 0),
237 SOC_DAPM_SINGLE("Right Bypass Switch", WM8988_LOUTM2, 7, 1, 0),
241 static const struct snd_kcontrol_new wm8988_right_mixer_controls[] = {
242 SOC_DAPM_SINGLE("Left Playback Switch", WM8988_ROUTM1, 8, 1, 0),
243 SOC_DAPM_SINGLE("Left Bypass Switch", WM8988_ROUTM1, 7, 1, 0),
244 SOC_DAPM_SINGLE("Playback Switch", WM8988_ROUTM2, 8, 1, 0),
245 SOC_DAPM_SINGLE("Right Bypass Switch", WM8988_ROUTM2, 7, 1, 0),
248 static const char *wm8988_pga_sel[] = {"Line 1", "Line 2", "Differential"};
249 static const unsigned int wm8988_pga_val[] = { 0, 1, 3 };
252 static const struct soc_enum wm8988_lpga_enum =
253 SOC_VALUE_ENUM_SINGLE(WM8988_LADCIN, 6, 3,
254 ARRAY_SIZE(wm8988_pga_sel),
257 static const struct snd_kcontrol_new wm8988_left_pga_controls =
258 SOC_DAPM_VALUE_ENUM("Route", wm8988_lpga_enum);
261 static const struct soc_enum wm8988_rpga_enum =
262 SOC_VALUE_ENUM_SINGLE(WM8988_RADCIN, 6, 3,
263 ARRAY_SIZE(wm8988_pga_sel),
266 static const struct snd_kcontrol_new wm8988_right_pga_controls =
267 SOC_DAPM_VALUE_ENUM("Route", wm8988_rpga_enum);
269 /* Differential Mux */
270 static const char *wm8988_diff_sel[] = {"Line 1", "Line 2"};
271 static const struct soc_enum diffmux =
272 SOC_ENUM_SINGLE(WM8988_ADCIN, 8, 2, wm8988_diff_sel);
273 static const struct snd_kcontrol_new wm8988_diffmux_controls =
274 SOC_DAPM_ENUM("Route", diffmux);
277 static const char *wm8988_mono_mux[] = {"Stereo", "Mono (Left)",
278 "Mono (Right)", "Digital Mono"};
279 static const struct soc_enum monomux =
280 SOC_ENUM_SINGLE(WM8988_ADCIN, 6, 4, wm8988_mono_mux);
281 static const struct snd_kcontrol_new wm8988_monomux_controls =
282 SOC_DAPM_ENUM("Route", monomux);
284 static const struct snd_soc_dapm_widget wm8988_dapm_widgets[] = {
285 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8988_PWR1, 1, 0),
287 SND_SOC_DAPM_MUX("Differential Mux", SND_SOC_NOPM, 0, 0,
288 &wm8988_diffmux_controls),
289 SND_SOC_DAPM_MUX("Left ADC Mux", SND_SOC_NOPM, 0, 0,
290 &wm8988_monomux_controls),
291 SND_SOC_DAPM_MUX("Right ADC Mux", SND_SOC_NOPM, 0, 0,
292 &wm8988_monomux_controls),
294 SND_SOC_DAPM_MUX("Left PGA Mux", WM8988_PWR1, 5, 0,
295 &wm8988_left_pga_controls),
296 SND_SOC_DAPM_MUX("Right PGA Mux", WM8988_PWR1, 4, 0,
297 &wm8988_right_pga_controls),
299 SND_SOC_DAPM_MUX("Left Line Mux", SND_SOC_NOPM, 0, 0,
300 &wm8988_left_line_controls),
301 SND_SOC_DAPM_MUX("Right Line Mux", SND_SOC_NOPM, 0, 0,
302 &wm8988_right_line_controls),
304 SND_SOC_DAPM_ADC("Right ADC", "Right Capture", WM8988_PWR1, 2, 0),
305 SND_SOC_DAPM_ADC("Left ADC", "Left Capture", WM8988_PWR1, 3, 0),
307 SND_SOC_DAPM_DAC("Right DAC", "Right Playback", WM8988_PWR2, 7, 0),
308 SND_SOC_DAPM_DAC("Left DAC", "Left Playback", WM8988_PWR2, 8, 0),
310 SND_SOC_DAPM_MIXER("Left Mixer", SND_SOC_NOPM, 0, 0,
311 &wm8988_left_mixer_controls[0],
312 ARRAY_SIZE(wm8988_left_mixer_controls)),
313 SND_SOC_DAPM_MIXER("Right Mixer", SND_SOC_NOPM, 0, 0,
314 &wm8988_right_mixer_controls[0],
315 ARRAY_SIZE(wm8988_right_mixer_controls)),
317 SND_SOC_DAPM_PGA("Right Out 2", WM8988_PWR2, 3, 0, NULL, 0),
318 SND_SOC_DAPM_PGA("Left Out 2", WM8988_PWR2, 4, 0, NULL, 0),
319 SND_SOC_DAPM_PGA("Right Out 1", WM8988_PWR2, 5, 0, NULL, 0),
320 SND_SOC_DAPM_PGA("Left Out 1", WM8988_PWR2, 6, 0, NULL, 0),
322 SND_SOC_DAPM_POST("LRC control", wm8988_lrc_control),
324 SND_SOC_DAPM_OUTPUT("LOUT1"),
325 SND_SOC_DAPM_OUTPUT("ROUT1"),
326 SND_SOC_DAPM_OUTPUT("LOUT2"),
327 SND_SOC_DAPM_OUTPUT("ROUT2"),
328 SND_SOC_DAPM_OUTPUT("VREF"),
330 SND_SOC_DAPM_INPUT("LINPUT1"),
331 SND_SOC_DAPM_INPUT("LINPUT2"),
332 SND_SOC_DAPM_INPUT("RINPUT1"),
333 SND_SOC_DAPM_INPUT("RINPUT2"),
336 static const struct snd_soc_dapm_route audio_map[] = {
338 { "Left Line Mux", "Line 1", "LINPUT1" },
339 { "Left Line Mux", "Line 2", "LINPUT2" },
340 { "Left Line Mux", "PGA", "Left PGA Mux" },
341 { "Left Line Mux", "Differential", "Differential Mux" },
343 { "Right Line Mux", "Line 1", "RINPUT1" },
344 { "Right Line Mux", "Line 2", "RINPUT2" },
345 { "Right Line Mux", "PGA", "Right PGA Mux" },
346 { "Right Line Mux", "Differential", "Differential Mux" },
348 { "Left PGA Mux", "Line 1", "LINPUT1" },
349 { "Left PGA Mux", "Line 2", "LINPUT2" },
350 { "Left PGA Mux", "Differential", "Differential Mux" },
352 { "Right PGA Mux", "Line 1", "RINPUT1" },
353 { "Right PGA Mux", "Line 2", "RINPUT2" },
354 { "Right PGA Mux", "Differential", "Differential Mux" },
356 { "Differential Mux", "Line 1", "LINPUT1" },
357 { "Differential Mux", "Line 1", "RINPUT1" },
358 { "Differential Mux", "Line 2", "LINPUT2" },
359 { "Differential Mux", "Line 2", "RINPUT2" },
361 { "Left ADC Mux", "Stereo", "Left PGA Mux" },
362 { "Left ADC Mux", "Mono (Left)", "Left PGA Mux" },
363 { "Left ADC Mux", "Digital Mono", "Left PGA Mux" },
365 { "Right ADC Mux", "Stereo", "Right PGA Mux" },
366 { "Right ADC Mux", "Mono (Right)", "Right PGA Mux" },
367 { "Right ADC Mux", "Digital Mono", "Right PGA Mux" },
369 { "Left ADC", NULL, "Left ADC Mux" },
370 { "Right ADC", NULL, "Right ADC Mux" },
372 { "Left Line Mux", "Line 1", "LINPUT1" },
373 { "Left Line Mux", "Line 2", "LINPUT2" },
374 { "Left Line Mux", "PGA", "Left PGA Mux" },
375 { "Left Line Mux", "Differential", "Differential Mux" },
377 { "Right Line Mux", "Line 1", "RINPUT1" },
378 { "Right Line Mux", "Line 2", "RINPUT2" },
379 { "Right Line Mux", "PGA", "Right PGA Mux" },
380 { "Right Line Mux", "Differential", "Differential Mux" },
382 { "Left Mixer", "Playback Switch", "Left DAC" },
383 { "Left Mixer", "Left Bypass Switch", "Left Line Mux" },
384 { "Left Mixer", "Right Playback Switch", "Right DAC" },
385 { "Left Mixer", "Right Bypass Switch", "Right Line Mux" },
387 { "Right Mixer", "Left Playback Switch", "Left DAC" },
388 { "Right Mixer", "Left Bypass Switch", "Left Line Mux" },
389 { "Right Mixer", "Playback Switch", "Right DAC" },
390 { "Right Mixer", "Right Bypass Switch", "Right Line Mux" },
392 { "Left Out 1", NULL, "Left Mixer" },
393 { "LOUT1", NULL, "Left Out 1" },
394 { "Right Out 1", NULL, "Right Mixer" },
395 { "ROUT1", NULL, "Right Out 1" },
397 { "Left Out 2", NULL, "Left Mixer" },
398 { "LOUT2", NULL, "Left Out 2" },
399 { "Right Out 2", NULL, "Right Mixer" },
400 { "ROUT2", NULL, "Right Out 2" },
411 /* codec hifi mclk clock divider coefficients */
412 static const struct _coeff_div coeff_div[] = {
414 {12288000, 8000, 1536, 0x6, 0x0},
415 {11289600, 8000, 1408, 0x16, 0x0},
416 {18432000, 8000, 2304, 0x7, 0x0},
417 {16934400, 8000, 2112, 0x17, 0x0},
418 {12000000, 8000, 1500, 0x6, 0x1},
421 {11289600, 11025, 1024, 0x18, 0x0},
422 {16934400, 11025, 1536, 0x19, 0x0},
423 {12000000, 11025, 1088, 0x19, 0x1},
426 {12288000, 16000, 768, 0xa, 0x0},
427 {18432000, 16000, 1152, 0xb, 0x0},
428 {12000000, 16000, 750, 0xa, 0x1},
431 {11289600, 22050, 512, 0x1a, 0x0},
432 {16934400, 22050, 768, 0x1b, 0x0},
433 {12000000, 22050, 544, 0x1b, 0x1},
436 {12288000, 32000, 384, 0xc, 0x0},
437 {18432000, 32000, 576, 0xd, 0x0},
438 {12000000, 32000, 375, 0xa, 0x1},
441 {11289600, 44100, 256, 0x10, 0x0},
442 {16934400, 44100, 384, 0x11, 0x0},
443 {12000000, 44100, 272, 0x11, 0x1},
446 {12288000, 48000, 256, 0x0, 0x0},
447 {18432000, 48000, 384, 0x1, 0x0},
448 {12000000, 48000, 250, 0x0, 0x1},
451 {11289600, 88200, 128, 0x1e, 0x0},
452 {16934400, 88200, 192, 0x1f, 0x0},
453 {12000000, 88200, 136, 0x1f, 0x1},
456 {12288000, 96000, 128, 0xe, 0x0},
457 {18432000, 96000, 192, 0xf, 0x0},
458 {12000000, 96000, 125, 0xe, 0x1},
461 static inline int get_coeff(int mclk, int rate)
465 for (i = 0; i < ARRAY_SIZE(coeff_div); i++) {
466 if (coeff_div[i].rate == rate && coeff_div[i].mclk == mclk)
473 /* The set of rates we can generate from the above for each SYSCLK */
475 static unsigned int rates_12288[] = {
476 8000, 12000, 16000, 24000, 24000, 32000, 48000, 96000,
479 static struct snd_pcm_hw_constraint_list constraints_12288 = {
480 .count = ARRAY_SIZE(rates_12288),
484 static unsigned int rates_112896[] = {
485 8000, 11025, 22050, 44100,
488 static struct snd_pcm_hw_constraint_list constraints_112896 = {
489 .count = ARRAY_SIZE(rates_112896),
490 .list = rates_112896,
493 static unsigned int rates_12[] = {
494 8000, 11025, 12000, 16000, 22050, 2400, 32000, 41100, 48000,
498 static struct snd_pcm_hw_constraint_list constraints_12 = {
499 .count = ARRAY_SIZE(rates_12),
504 * Note that this should be called from init rather than from hw_params.
506 static int wm8988_set_dai_sysclk(struct snd_soc_dai *codec_dai,
507 int clk_id, unsigned int freq, int dir)
509 struct snd_soc_codec *codec = codec_dai->codec;
510 struct wm8988_priv *wm8988 = snd_soc_codec_get_drvdata(codec);
517 wm8988->sysclk_constraints = &constraints_112896;
518 wm8988->sysclk = freq;
525 wm8988->sysclk_constraints = &constraints_12288;
526 wm8988->sysclk = freq;
531 wm8988->sysclk_constraints = &constraints_12;
532 wm8988->sysclk = freq;
538 static int wm8988_set_dai_fmt(struct snd_soc_dai *codec_dai,
541 struct snd_soc_codec *codec = codec_dai->codec;
544 /* set master/slave audio interface */
545 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
546 case SND_SOC_DAIFMT_CBM_CFM:
549 case SND_SOC_DAIFMT_CBS_CFS:
555 /* interface format */
556 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
557 case SND_SOC_DAIFMT_I2S:
560 case SND_SOC_DAIFMT_RIGHT_J:
562 case SND_SOC_DAIFMT_LEFT_J:
565 case SND_SOC_DAIFMT_DSP_A:
568 case SND_SOC_DAIFMT_DSP_B:
575 /* clock inversion */
576 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
577 case SND_SOC_DAIFMT_NB_NF:
579 case SND_SOC_DAIFMT_IB_IF:
582 case SND_SOC_DAIFMT_IB_NF:
585 case SND_SOC_DAIFMT_NB_IF:
592 DBG("Enter::%s----%d iface=%x\n",__FUNCTION__,__LINE__,iface);
593 snd_soc_write(codec, WM8988_IFACE, iface);
597 static int wm8988_pcm_startup(struct snd_pcm_substream *substream,
598 struct snd_soc_dai *dai)
600 struct snd_soc_codec *codec = dai->codec;
601 struct wm8988_priv *wm8988 = snd_soc_codec_get_drvdata(codec);
603 /* The set of sample rates that can be supported depends on the
604 * MCLK supplied to the CODEC - enforce this.
606 DBG("Enter::%s----%d wm8988->sysclk=%d\n",__FUNCTION__,__LINE__,wm8988->sysclk);
607 if (!wm8988->sysclk) {
609 "No MCLK configured, call set_sysclk() on init\n");
613 snd_pcm_hw_constraint_list(substream->runtime, 0,
614 SNDRV_PCM_HW_PARAM_RATE,
615 wm8988->sysclk_constraints);
620 static int wm8988_pcm_hw_params(struct snd_pcm_substream *substream,
621 struct snd_pcm_hw_params *params,
622 struct snd_soc_dai *dai)
624 struct snd_soc_pcm_runtime *rtd = substream->private_data;
625 struct snd_soc_device *socdev = rtd->socdev;
626 struct snd_soc_codec *codec = socdev->card->codec;
627 struct wm8988_priv *wm8988 = snd_soc_codec_get_drvdata(codec);
628 u16 iface = snd_soc_read(codec, WM8988_IFACE) & 0x1f3;
629 u16 srate = snd_soc_read(codec, WM8988_SRATE) & 0x180;
632 coeff = get_coeff(wm8988->sysclk, params_rate(params));
634 coeff = get_coeff(wm8988->sysclk / 2, params_rate(params));
639 "Unable to configure sample rate %dHz with %dHz MCLK\n",
640 params_rate(params), wm8988->sysclk);
645 switch (params_format(params)) {
646 case SNDRV_PCM_FORMAT_S16_LE:
648 case SNDRV_PCM_FORMAT_S20_3LE:
651 case SNDRV_PCM_FORMAT_S24_LE:
654 case SNDRV_PCM_FORMAT_S32_LE:
658 DBG("Enter::%s----%d iface=%x srate =%x rate=%d\n",__FUNCTION__,__LINE__,iface,srate,params_rate(params));
660 /* set iface & srate */
661 snd_soc_write(codec, WM8988_IFACE, iface);
663 snd_soc_write(codec, WM8988_SRATE, srate |
664 (coeff_div[coeff].sr << 1) | coeff_div[coeff].usb);
669 static int wm8988_mute(struct snd_soc_dai *dai, int mute)
671 struct snd_soc_codec *codec = dai->codec;
672 u16 mute_reg = snd_soc_read(codec, WM8988_ADCDAC) & 0xfff7;
673 DBG("Enter::%s----%d--mute=%d\n",__FUNCTION__,__LINE__,mute);
675 snd_soc_write(codec, WM8988_ADCDAC, mute_reg | 0x8);
677 snd_soc_write(codec, WM8988_ADCDAC, mute_reg);
681 static int wm8988_set_bias_level(struct snd_soc_codec *codec,
682 enum snd_soc_bias_level level)
684 u16 pwr_reg = snd_soc_read(codec, WM8988_PWR1) & ~0x1c1;
685 DBG("Enter::%s----%d level =%d\n",__FUNCTION__,__LINE__,level);
687 case SND_SOC_BIAS_ON:
690 case SND_SOC_BIAS_PREPARE:
691 /* VREF, VMID=2x50k, digital enabled */
692 snd_soc_write(codec, WM8988_PWR1, pwr_reg | 0x00c0);
695 case SND_SOC_BIAS_STANDBY:
696 if (codec->bias_level == SND_SOC_BIAS_OFF) {
697 /* VREF, VMID=2x5k */
698 snd_soc_write(codec, WM8988_PWR1, pwr_reg | 0x1c1);
704 /* VREF, VMID=2*500k, digital stopped */
705 snd_soc_write(codec, WM8988_PWR1, pwr_reg | 0x0141);
708 case SND_SOC_BIAS_OFF:
709 snd_soc_write(codec, WM8988_PWR1, 0x0000);
712 codec->bias_level = level;
716 #define WM8988_RATES SNDRV_PCM_RATE_8000_96000
718 #define WM8988_FORMATS (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE |\
719 SNDRV_PCM_FMTBIT_S24_LE)
721 static struct snd_soc_dai_ops wm8988_ops = {
722 .startup = wm8988_pcm_startup,
723 .hw_params = wm8988_pcm_hw_params,
724 .set_fmt = wm8988_set_dai_fmt,
725 .set_sysclk = wm8988_set_dai_sysclk,
726 .digital_mute = wm8988_mute,
729 struct snd_soc_dai wm8988_dai = {
732 .stream_name = "Playback",
735 .rates = WM8988_RATES,
736 .formats = WM8988_FORMATS,
739 .stream_name = "Capture",
742 .rates = WM8988_RATES,
743 .formats = WM8988_FORMATS,
746 .symmetric_rates = 1,
748 EXPORT_SYMBOL_GPL(wm8988_dai);
750 static int wm8988_suspend(struct platform_device *pdev, pm_message_t state)
752 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
753 struct snd_soc_codec *codec = socdev->card->codec;
754 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
755 wm8988_set_bias_level(codec, SND_SOC_BIAS_OFF);
759 static int wm8988_resume(struct platform_device *pdev)
761 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
762 struct snd_soc_codec *codec = socdev->card->codec;
765 u16 *cache = codec->reg_cache;
766 DBG("Enter::%s----%d\n",__FUNCTION__,__LINE__);
767 /* Sync reg_cache with the hardware */
768 for (i = 0; i < WM8988_NUM_REG; i++) {
769 if (i == WM8988_RESET)
771 data[0] = (i << 1) | ((cache[i] >> 8) & 0x0001);
772 data[1] = cache[i] & 0x00ff;
773 codec->hw_write(codec->control_data, data, 2);
776 wm8988_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
781 static struct snd_soc_codec *wm8988_codec;
783 static int wm8988_probe(struct platform_device *pdev)
785 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
786 struct snd_soc_codec *codec;
789 if (wm8988_codec == NULL) {
790 dev_err(&pdev->dev, "Codec device not registered\n");
794 socdev->card->codec = wm8988_codec;
795 codec = wm8988_codec;
798 ret = snd_soc_new_pcms(socdev, SNDRV_DEFAULT_IDX1, SNDRV_DEFAULT_STR1);
800 dev_err(codec->dev, "failed to create pcms: %d\n", ret);
804 snd_soc_add_controls(codec, wm8988_snd_controls,
805 ARRAY_SIZE(wm8988_snd_controls));
806 snd_soc_dapm_new_controls(codec, wm8988_dapm_widgets,
807 ARRAY_SIZE(wm8988_dapm_widgets));
808 snd_soc_dapm_add_routes(codec, audio_map, ARRAY_SIZE(audio_map));
816 static int wm8988_remove(struct platform_device *pdev)
818 struct snd_soc_device *socdev = platform_get_drvdata(pdev);
820 snd_soc_free_pcms(socdev);
821 snd_soc_dapm_free(socdev);
826 struct snd_soc_codec_device soc_codec_dev_wm8988 = {
827 .probe = wm8988_probe,
828 .remove = wm8988_remove,
829 .suspend = wm8988_suspend,
830 .resume = wm8988_resume,
832 EXPORT_SYMBOL_GPL(soc_codec_dev_wm8988);
834 static int wm8988_register(struct wm8988_priv *wm8988,
835 enum snd_soc_control_type control)
837 struct snd_soc_codec *codec = &wm8988->codec;
842 dev_err(codec->dev, "Another WM8988 is registered\n");
847 mutex_init(&codec->mutex);
848 INIT_LIST_HEAD(&codec->dapm_widgets);
849 INIT_LIST_HEAD(&codec->dapm_paths);
851 snd_soc_codec_set_drvdata(codec, wm8988);
852 codec->name = "WM8988";
853 codec->owner = THIS_MODULE;
854 codec->dai = &wm8988_dai;
856 codec->reg_cache_size = ARRAY_SIZE(wm8988->reg_cache);
857 codec->reg_cache = &wm8988->reg_cache;
858 codec->bias_level = SND_SOC_BIAS_OFF;
859 codec->set_bias_level = wm8988_set_bias_level;
861 memcpy(codec->reg_cache, wm8988_reg,
864 ret = snd_soc_codec_set_cache_io(codec, 7, 9, control);
866 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
870 ret = wm8988_reset(codec);
872 dev_err(codec->dev, "Failed to issue reset\n");
877 gpio_request(RK2818_PIN_PF7, "WM8988");
878 rk2818_mux_api_set(GPIOE_SPI1_FLASH_SEL_NAME, IOMUXA_GPIO1_A3B7);
879 gpio_direction_output(RK2818_PIN_PF7,GPIO_HIGH);
882 /* set the update bits (we always update left then right) */
883 reg = snd_soc_read(codec, WM8988_RADC);
884 snd_soc_write(codec, WM8988_RADC, reg | 0x100);
885 reg = snd_soc_read(codec, WM8988_RDAC);
886 snd_soc_write(codec, WM8988_RDAC, reg | 0x0100);
887 reg = snd_soc_read(codec, WM8988_ROUT1V);
888 snd_soc_write(codec, WM8988_ROUT1V, reg | 0x0100);
889 reg = snd_soc_read(codec, WM8988_ROUT2V);
890 snd_soc_write(codec, WM8988_ROUT2V, reg | 0x0100);
891 reg = snd_soc_read(codec, WM8988_RINVOL);
892 snd_soc_write(codec, WM8988_RINVOL, reg | 0x0100);
894 snd_soc_write(codec, WM8988_LOUTM1, 0x120);
895 snd_soc_write(codec, WM8988_ROUTM2, 0x120);
896 snd_soc_write(codec, WM8988_LOUTM2, 0x0070);
897 snd_soc_write(codec, WM8988_ROUTM1, 0x0070);
899 snd_soc_write(codec, WM8988_LOUT1V, 0x017f);
900 snd_soc_write(codec, WM8988_ROUT1V, 0x017f);
901 snd_soc_write(codec, WM8988_LDAC, 0xff);
902 snd_soc_write(codec, WM8988_RDAC, 0x1ff);//vol set
904 snd_soc_write(codec, WM8988_SRATE,0x100); ///SET MCLK/8
905 snd_soc_write(codec, WM8988_PWR1, 0x1cc); ///(0x80|0x40|0x20|0x08|0x04|0x10|0x02));
906 snd_soc_write(codec, WM8988_PWR2, 0x1e0); //power r l out1
909 wm8988_set_bias_level(&wm8988->codec, SND_SOC_BIAS_STANDBY);
911 wm8988_dai.dev = codec->dev;
913 wm8988_codec = codec;
915 ret = snd_soc_register_codec(codec);
917 dev_err(codec->dev, "Failed to register codec: %d\n", ret);
921 ret = snd_soc_register_dai(&wm8988_dai);
923 dev_err(codec->dev, "Failed to register DAI: %d\n", ret);
930 snd_soc_unregister_codec(codec);
936 static void wm8988_unregister(struct wm8988_priv *wm8988)
938 wm8988_set_bias_level(&wm8988->codec, SND_SOC_BIAS_OFF);
939 snd_soc_unregister_dai(&wm8988_dai);
940 snd_soc_unregister_codec(&wm8988->codec);
945 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
946 static int wm8988_i2c_probe(struct i2c_client *i2c,
947 const struct i2c_device_id *id)
949 struct wm8988_priv *wm8988;
950 struct snd_soc_codec *codec;
952 wm8988 = kzalloc(sizeof(struct wm8988_priv), GFP_KERNEL);
956 codec = &wm8988->codec;
958 i2c_set_clientdata(i2c, wm8988);
959 codec->control_data = i2c;
961 codec->dev = &i2c->dev;
963 return wm8988_register(wm8988, SND_SOC_I2C);
966 static int wm8988_i2c_remove(struct i2c_client *client)
968 struct wm8988_priv *wm8988 = i2c_get_clientdata(client);
969 wm8988_unregister(wm8988);
973 static const struct i2c_device_id wm8988_i2c_id[] = {
977 MODULE_DEVICE_TABLE(i2c, wm8988_i2c_id);
979 static struct i2c_driver wm8988_i2c_driver = {
982 .owner = THIS_MODULE,
984 .probe = wm8988_i2c_probe,
985 .remove = wm8988_i2c_remove,
986 .id_table = wm8988_i2c_id,
990 #if defined(CONFIG_SPI_MASTER)
991 static int __devinit wm8988_spi_probe(struct spi_device *spi)
993 struct wm8988_priv *wm8988;
994 struct snd_soc_codec *codec;
996 wm8988 = kzalloc(sizeof(struct wm8988_priv), GFP_KERNEL);
1000 codec = &wm8988->codec;
1001 codec->control_data = spi;
1002 codec->dev = &spi->dev;
1004 dev_set_drvdata(&spi->dev, wm8988);
1006 return wm8988_register(wm8988, SND_SOC_SPI);
1009 static int __devexit wm8988_spi_remove(struct spi_device *spi)
1011 struct wm8988_priv *wm8988 = dev_get_drvdata(&spi->dev);
1013 wm8988_unregister(wm8988);
1018 static struct spi_driver wm8988_spi_driver = {
1021 .bus = &spi_bus_type,
1022 .owner = THIS_MODULE,
1024 .probe = wm8988_spi_probe,
1025 .remove = __devexit_p(wm8988_spi_remove),
1029 static int __init wm8988_modinit(void)
1033 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1034 ret = i2c_add_driver(&wm8988_i2c_driver);
1036 pr_err("WM8988: Unable to register I2C driver: %d\n", ret);
1038 #if defined(CONFIG_SPI_MASTER)
1039 ret = spi_register_driver(&wm8988_spi_driver);
1041 pr_err("WM8988: Unable to register SPI driver: %d\n", ret);
1045 module_init(wm8988_modinit);
1047 static void __exit wm8988_exit(void)
1049 #if defined(CONFIG_I2C) || defined(CONFIG_I2C_MODULE)
1050 i2c_del_driver(&wm8988_i2c_driver);
1052 #if defined(CONFIG_SPI_MASTER)
1053 spi_unregister_driver(&wm8988_spi_driver);
1056 module_exit(wm8988_exit);
1059 MODULE_DESCRIPTION("ASoC WM8988 driver");
1060 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1061 MODULE_LICENSE("GPL");