2 * wm8961.c -- WM8961 ALSA SoC Audio driver
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Currently unimplemented features:
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/slab.h>
21 #include <sound/core.h>
22 #include <sound/pcm.h>
23 #include <sound/pcm_params.h>
24 #include <sound/soc.h>
25 #include <sound/initval.h>
26 #include <sound/tlv.h>
30 #define WM8961_MAX_REGISTER 0xFC
32 static u16 wm8961_reg_defaults[] = {
33 0x009F, /* R0 - Left Input volume */
34 0x009F, /* R1 - Right Input volume */
35 0x0000, /* R2 - LOUT1 volume */
36 0x0000, /* R3 - ROUT1 volume */
37 0x0020, /* R4 - Clocking1 */
38 0x0008, /* R5 - ADC & DAC Control 1 */
39 0x0000, /* R6 - ADC & DAC Control 2 */
40 0x000A, /* R7 - Audio Interface 0 */
41 0x01F4, /* R8 - Clocking2 */
42 0x0000, /* R9 - Audio Interface 1 */
43 0x00FF, /* R10 - Left DAC volume */
44 0x00FF, /* R11 - Right DAC volume */
47 0x0040, /* R14 - Audio Interface 2 */
48 0x0000, /* R15 - Software Reset */
50 0x007B, /* R17 - ALC1 */
51 0x0000, /* R18 - ALC2 */
52 0x0032, /* R19 - ALC3 */
53 0x0000, /* R20 - Noise Gate */
54 0x00C0, /* R21 - Left ADC volume */
55 0x00C0, /* R22 - Right ADC volume */
56 0x0120, /* R23 - Additional control(1) */
57 0x0000, /* R24 - Additional control(2) */
58 0x0000, /* R25 - Pwr Mgmt (1) */
59 0x0000, /* R26 - Pwr Mgmt (2) */
60 0x0000, /* R27 - Additional Control (3) */
61 0x0000, /* R28 - Anti-pop */
63 0x005F, /* R30 - Clocking 3 */
65 0x0000, /* R32 - ADCL signal path */
66 0x0000, /* R33 - ADCR signal path */
73 0x0000, /* R40 - LOUT2 volume */
74 0x0000, /* R41 - ROUT2 volume */
80 0x0000, /* R47 - Pwr Mgmt (3) */
81 0x0023, /* R48 - Additional Control (4) */
82 0x0000, /* R49 - Class D Control 1 */
84 0x0003, /* R51 - Class D Control 2 */
89 0x0106, /* R56 - Clocking 4 */
90 0x0000, /* R57 - DSP Sidetone 0 */
91 0x0000, /* R58 - DSP Sidetone 1 */
93 0x0000, /* R60 - DC Servo 0 */
94 0x0000, /* R61 - DC Servo 1 */
96 0x015E, /* R63 - DC Servo 3 */
98 0x0010, /* R65 - DC Servo 5 */
101 0x0003, /* R68 - Analogue PGA Bias */
102 0x0000, /* R69 - Analogue HP 0 */
104 0x01FB, /* R71 - Analogue HP 2 */
105 0x0000, /* R72 - Charge Pump 1 */
115 0x0000, /* R82 - Charge Pump B */
120 0x0000, /* R87 - Write Sequencer 1 */
121 0x0000, /* R88 - Write Sequencer 2 */
122 0x0000, /* R89 - Write Sequencer 3 */
123 0x0000, /* R90 - Write Sequencer 4 */
124 0x0000, /* R91 - Write Sequencer 5 */
125 0x0000, /* R92 - Write Sequencer 6 */
126 0x0000, /* R93 - Write Sequencer 7 */
285 0x0001, /* R252 - General test 1 */
289 enum snd_soc_control_type control_type;
293 static int wm8961_volatile_register(struct snd_soc_codec *codec, unsigned int reg)
296 case WM8961_SOFTWARE_RESET:
297 case WM8961_WRITE_SEQUENCER_7:
298 case WM8961_DC_SERVO_1:
306 static int wm8961_reset(struct snd_soc_codec *codec)
308 return snd_soc_write(codec, WM8961_SOFTWARE_RESET, 0);
312 * The headphone output supports special anti-pop sequences giving
313 * silent power up and power down.
315 static int wm8961_hp_event(struct snd_soc_dapm_widget *w,
316 struct snd_kcontrol *kcontrol, int event)
318 struct snd_soc_codec *codec = w->codec;
319 u16 hp_reg = snd_soc_read(codec, WM8961_ANALOGUE_HP_0);
320 u16 cp_reg = snd_soc_read(codec, WM8961_CHARGE_PUMP_1);
321 u16 pwr_reg = snd_soc_read(codec, WM8961_PWR_MGMT_2);
322 u16 dcs_reg = snd_soc_read(codec, WM8961_DC_SERVO_1);
325 if (event & SND_SOC_DAPM_POST_PMU) {
326 /* Make sure the output is shorted */
327 hp_reg &= ~(WM8961_HPR_RMV_SHORT | WM8961_HPL_RMV_SHORT);
328 snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
330 /* Enable the charge pump */
331 cp_reg |= WM8961_CP_ENA;
332 snd_soc_write(codec, WM8961_CHARGE_PUMP_1, cp_reg);
336 pwr_reg |= WM8961_LOUT1_PGA | WM8961_ROUT1_PGA;
337 snd_soc_write(codec, WM8961_PWR_MGMT_2, pwr_reg);
339 /* Enable the amplifier */
340 hp_reg |= WM8961_HPR_ENA | WM8961_HPL_ENA;
341 snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
343 /* Second stage enable */
344 hp_reg |= WM8961_HPR_ENA_DLY | WM8961_HPL_ENA_DLY;
345 snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
347 /* Enable the DC servo & trigger startup */
349 WM8961_DCS_ENA_CHAN_HPR | WM8961_DCS_TRIG_STARTUP_HPR |
350 WM8961_DCS_ENA_CHAN_HPL | WM8961_DCS_TRIG_STARTUP_HPL;
351 dev_dbg(codec->dev, "Enabling DC servo\n");
353 snd_soc_write(codec, WM8961_DC_SERVO_1, dcs_reg);
356 dcs_reg = snd_soc_read(codec, WM8961_DC_SERVO_1);
357 } while (--timeout &&
358 dcs_reg & (WM8961_DCS_TRIG_STARTUP_HPR |
359 WM8961_DCS_TRIG_STARTUP_HPL));
360 if (dcs_reg & (WM8961_DCS_TRIG_STARTUP_HPR |
361 WM8961_DCS_TRIG_STARTUP_HPL))
362 dev_err(codec->dev, "DC servo timed out\n");
364 dev_dbg(codec->dev, "DC servo startup complete\n");
366 /* Enable the output stage */
367 hp_reg |= WM8961_HPR_ENA_OUTP | WM8961_HPL_ENA_OUTP;
368 snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
370 /* Remove the short on the output stage */
371 hp_reg |= WM8961_HPR_RMV_SHORT | WM8961_HPL_RMV_SHORT;
372 snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
375 if (event & SND_SOC_DAPM_PRE_PMD) {
376 /* Short the output */
377 hp_reg &= ~(WM8961_HPR_RMV_SHORT | WM8961_HPL_RMV_SHORT);
378 snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
380 /* Disable the output stage */
381 hp_reg &= ~(WM8961_HPR_ENA_OUTP | WM8961_HPL_ENA_OUTP);
382 snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
384 /* Disable DC offset cancellation */
385 dcs_reg &= ~(WM8961_DCS_ENA_CHAN_HPR |
386 WM8961_DCS_ENA_CHAN_HPL);
387 snd_soc_write(codec, WM8961_DC_SERVO_1, dcs_reg);
390 hp_reg &= ~(WM8961_HPR_ENA_DLY | WM8961_HPR_ENA |
391 WM8961_HPL_ENA_DLY | WM8961_HPL_ENA);
392 snd_soc_write(codec, WM8961_ANALOGUE_HP_0, hp_reg);
394 /* Disable the PGA */
395 pwr_reg &= ~(WM8961_LOUT1_PGA | WM8961_ROUT1_PGA);
396 snd_soc_write(codec, WM8961_PWR_MGMT_2, pwr_reg);
398 /* Disable the charge pump */
399 dev_dbg(codec->dev, "Disabling charge pump\n");
400 snd_soc_write(codec, WM8961_CHARGE_PUMP_1,
401 cp_reg & ~WM8961_CP_ENA);
407 static int wm8961_spk_event(struct snd_soc_dapm_widget *w,
408 struct snd_kcontrol *kcontrol, int event)
410 struct snd_soc_codec *codec = w->codec;
411 u16 pwr_reg = snd_soc_read(codec, WM8961_PWR_MGMT_2);
412 u16 spk_reg = snd_soc_read(codec, WM8961_CLASS_D_CONTROL_1);
414 if (event & SND_SOC_DAPM_POST_PMU) {
416 pwr_reg |= WM8961_SPKL_PGA | WM8961_SPKR_PGA;
417 snd_soc_write(codec, WM8961_PWR_MGMT_2, pwr_reg);
419 /* Enable the amplifier */
420 spk_reg |= WM8961_SPKL_ENA | WM8961_SPKR_ENA;
421 snd_soc_write(codec, WM8961_CLASS_D_CONTROL_1, spk_reg);
424 if (event & SND_SOC_DAPM_PRE_PMD) {
425 /* Disable the amplifier */
426 spk_reg &= ~(WM8961_SPKL_ENA | WM8961_SPKR_ENA);
427 snd_soc_write(codec, WM8961_CLASS_D_CONTROL_1, spk_reg);
429 /* Disable the PGA */
430 pwr_reg &= ~(WM8961_SPKL_PGA | WM8961_SPKR_PGA);
431 snd_soc_write(codec, WM8961_PWR_MGMT_2, pwr_reg);
437 static const char *adc_hpf_text[] = {
438 "Hi-fi", "Voice 1", "Voice 2", "Voice 3",
441 static const struct soc_enum adc_hpf =
442 SOC_ENUM_SINGLE(WM8961_ADC_DAC_CONTROL_2, 7, 4, adc_hpf_text);
444 static const char *dac_deemph_text[] = {
445 "None", "32kHz", "44.1kHz", "48kHz",
448 static const struct soc_enum dac_deemph =
449 SOC_ENUM_SINGLE(WM8961_ADC_DAC_CONTROL_1, 1, 4, dac_deemph_text);
451 static const DECLARE_TLV_DB_SCALE(out_tlv, -12100, 100, 1);
452 static const DECLARE_TLV_DB_SCALE(hp_sec_tlv, -700, 100, 0);
453 static const DECLARE_TLV_DB_SCALE(adc_tlv, -7200, 75, 1);
454 static const DECLARE_TLV_DB_SCALE(sidetone_tlv, -3600, 300, 0);
455 static unsigned int boost_tlv[] = {
456 TLV_DB_RANGE_HEAD(4),
457 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
458 1, 1, TLV_DB_SCALE_ITEM(13, 0, 0),
459 2, 2, TLV_DB_SCALE_ITEM(20, 0, 0),
460 3, 3, TLV_DB_SCALE_ITEM(29, 0, 0),
462 static const DECLARE_TLV_DB_SCALE(pga_tlv, -2325, 75, 0);
464 static const struct snd_kcontrol_new wm8961_snd_controls[] = {
465 SOC_DOUBLE_R_TLV("Headphone Volume", WM8961_LOUT1_VOLUME, WM8961_ROUT1_VOLUME,
467 SOC_DOUBLE_TLV("Headphone Secondary Volume", WM8961_ANALOGUE_HP_2,
468 6, 3, 7, 0, hp_sec_tlv),
469 SOC_DOUBLE_R("Headphone ZC Switch", WM8961_LOUT1_VOLUME, WM8961_ROUT1_VOLUME,
472 SOC_DOUBLE_R_TLV("Speaker Volume", WM8961_LOUT2_VOLUME, WM8961_ROUT2_VOLUME,
474 SOC_DOUBLE_R("Speaker ZC Switch", WM8961_LOUT2_VOLUME, WM8961_ROUT2_VOLUME,
476 SOC_SINGLE("Speaker AC Gain", WM8961_CLASS_D_CONTROL_2, 0, 7, 0),
478 SOC_SINGLE("DAC x128 OSR Switch", WM8961_ADC_DAC_CONTROL_2, 0, 1, 0),
479 SOC_ENUM("DAC Deemphasis", dac_deemph),
480 SOC_SINGLE("DAC Soft Mute Switch", WM8961_ADC_DAC_CONTROL_2, 3, 1, 0),
482 SOC_DOUBLE_R_TLV("Sidetone Volume", WM8961_DSP_SIDETONE_0,
483 WM8961_DSP_SIDETONE_1, 4, 12, 0, sidetone_tlv),
485 SOC_SINGLE("ADC High Pass Filter Switch", WM8961_ADC_DAC_CONTROL_1, 0, 1, 0),
486 SOC_ENUM("ADC High Pass Filter Mode", adc_hpf),
488 SOC_DOUBLE_R_TLV("Capture Volume",
489 WM8961_LEFT_ADC_VOLUME, WM8961_RIGHT_ADC_VOLUME,
491 SOC_DOUBLE_R_TLV("Capture Boost Volume",
492 WM8961_ADCL_SIGNAL_PATH, WM8961_ADCR_SIGNAL_PATH,
494 SOC_DOUBLE_R_TLV("Capture PGA Volume",
495 WM8961_LEFT_INPUT_VOLUME, WM8961_RIGHT_INPUT_VOLUME,
497 SOC_DOUBLE_R("Capture PGA ZC Switch",
498 WM8961_LEFT_INPUT_VOLUME, WM8961_RIGHT_INPUT_VOLUME,
500 SOC_DOUBLE_R("Capture PGA Switch",
501 WM8961_LEFT_INPUT_VOLUME, WM8961_RIGHT_INPUT_VOLUME,
505 static const char *sidetone_text[] = {
506 "None", "Left", "Right"
509 static const struct soc_enum dacl_sidetone =
510 SOC_ENUM_SINGLE(WM8961_DSP_SIDETONE_0, 2, 3, sidetone_text);
512 static const struct soc_enum dacr_sidetone =
513 SOC_ENUM_SINGLE(WM8961_DSP_SIDETONE_1, 2, 3, sidetone_text);
515 static const struct snd_kcontrol_new dacl_mux =
516 SOC_DAPM_ENUM("DACL Sidetone", dacl_sidetone);
518 static const struct snd_kcontrol_new dacr_mux =
519 SOC_DAPM_ENUM("DACR Sidetone", dacr_sidetone);
521 static const struct snd_soc_dapm_widget wm8961_dapm_widgets[] = {
522 SND_SOC_DAPM_INPUT("LINPUT"),
523 SND_SOC_DAPM_INPUT("RINPUT"),
525 SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8961_CLOCKING2, 4, 0, NULL, 0),
527 SND_SOC_DAPM_PGA("Left Input", WM8961_PWR_MGMT_1, 5, 0, NULL, 0),
528 SND_SOC_DAPM_PGA("Right Input", WM8961_PWR_MGMT_1, 4, 0, NULL, 0),
530 SND_SOC_DAPM_ADC("ADCL", "HiFi Capture", WM8961_PWR_MGMT_1, 3, 0),
531 SND_SOC_DAPM_ADC("ADCR", "HiFi Capture", WM8961_PWR_MGMT_1, 2, 0),
533 SND_SOC_DAPM_SUPPLY("MICBIAS", WM8961_PWR_MGMT_1, 1, 0, NULL, 0),
535 SND_SOC_DAPM_MUX("DACL Sidetone", SND_SOC_NOPM, 0, 0, &dacl_mux),
536 SND_SOC_DAPM_MUX("DACR Sidetone", SND_SOC_NOPM, 0, 0, &dacr_mux),
538 SND_SOC_DAPM_DAC("DACL", "HiFi Playback", WM8961_PWR_MGMT_2, 8, 0),
539 SND_SOC_DAPM_DAC("DACR", "HiFi Playback", WM8961_PWR_MGMT_2, 7, 0),
541 /* Handle as a mono path for DCS */
542 SND_SOC_DAPM_PGA_E("Headphone Output", SND_SOC_NOPM,
543 4, 0, NULL, 0, wm8961_hp_event,
544 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
545 SND_SOC_DAPM_PGA_E("Speaker Output", SND_SOC_NOPM,
546 4, 0, NULL, 0, wm8961_spk_event,
547 SND_SOC_DAPM_POST_PMU | SND_SOC_DAPM_PRE_PMD),
549 SND_SOC_DAPM_OUTPUT("HP_L"),
550 SND_SOC_DAPM_OUTPUT("HP_R"),
551 SND_SOC_DAPM_OUTPUT("SPK_LN"),
552 SND_SOC_DAPM_OUTPUT("SPK_LP"),
553 SND_SOC_DAPM_OUTPUT("SPK_RN"),
554 SND_SOC_DAPM_OUTPUT("SPK_RP"),
558 static const struct snd_soc_dapm_route audio_paths[] = {
559 { "DACL", NULL, "CLK_DSP" },
560 { "DACL", NULL, "DACL Sidetone" },
561 { "DACR", NULL, "CLK_DSP" },
562 { "DACR", NULL, "DACR Sidetone" },
564 { "DACL Sidetone", "Left", "ADCL" },
565 { "DACL Sidetone", "Right", "ADCR" },
567 { "DACR Sidetone", "Left", "ADCL" },
568 { "DACR Sidetone", "Right", "ADCR" },
570 { "HP_L", NULL, "Headphone Output" },
571 { "HP_R", NULL, "Headphone Output" },
572 { "Headphone Output", NULL, "DACL" },
573 { "Headphone Output", NULL, "DACR" },
575 { "SPK_LN", NULL, "Speaker Output" },
576 { "SPK_LP", NULL, "Speaker Output" },
577 { "SPK_RN", NULL, "Speaker Output" },
578 { "SPK_RP", NULL, "Speaker Output" },
580 { "Speaker Output", NULL, "DACL" },
581 { "Speaker Output", NULL, "DACR" },
583 { "ADCL", NULL, "Left Input" },
584 { "ADCL", NULL, "CLK_DSP" },
585 { "ADCR", NULL, "Right Input" },
586 { "ADCR", NULL, "CLK_DSP" },
588 { "Left Input", NULL, "LINPUT" },
589 { "Right Input", NULL, "RINPUT" },
593 /* Values for CLK_SYS_RATE */
597 } wm8961_clk_sys_ratio[] = {
610 /* Values for SAMPLE_RATE */
626 static int wm8961_hw_params(struct snd_pcm_substream *substream,
627 struct snd_pcm_hw_params *params,
628 struct snd_soc_dai *dai)
630 struct snd_soc_codec *codec = dai->codec;
631 struct wm8961_priv *wm8961 = snd_soc_codec_get_drvdata(codec);
632 int i, best, target, fs;
635 fs = params_rate(params);
637 if (!wm8961->sysclk) {
638 dev_err(codec->dev, "MCLK has not been specified\n");
642 /* Find the closest sample rate for the filters */
644 for (i = 0; i < ARRAY_SIZE(wm8961_srate); i++) {
645 if (abs(wm8961_srate[i].rate - fs) <
646 abs(wm8961_srate[best].rate - fs))
649 reg = snd_soc_read(codec, WM8961_ADDITIONAL_CONTROL_3);
650 reg &= ~WM8961_SAMPLE_RATE_MASK;
651 reg |= wm8961_srate[best].val;
652 snd_soc_write(codec, WM8961_ADDITIONAL_CONTROL_3, reg);
653 dev_dbg(codec->dev, "Selected SRATE %dHz for %dHz\n",
654 wm8961_srate[best].rate, fs);
656 /* Select a CLK_SYS/fs ratio equal to or higher than required */
657 target = wm8961->sysclk / fs;
659 if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK && target < 64) {
661 "SYSCLK must be at least 64*fs for DAC\n");
664 if (substream->stream == SNDRV_PCM_STREAM_CAPTURE && target < 256) {
666 "SYSCLK must be at least 256*fs for ADC\n");
670 for (i = 0; i < ARRAY_SIZE(wm8961_clk_sys_ratio); i++) {
671 if (wm8961_clk_sys_ratio[i].ratio >= target)
674 if (i == ARRAY_SIZE(wm8961_clk_sys_ratio)) {
675 dev_err(codec->dev, "Unable to generate CLK_SYS_RATE\n");
678 dev_dbg(codec->dev, "Selected CLK_SYS_RATE of %d for %d/%d=%d\n",
679 wm8961_clk_sys_ratio[i].ratio, wm8961->sysclk, fs,
680 wm8961->sysclk / fs);
682 reg = snd_soc_read(codec, WM8961_CLOCKING_4);
683 reg &= ~WM8961_CLK_SYS_RATE_MASK;
684 reg |= wm8961_clk_sys_ratio[i].val << WM8961_CLK_SYS_RATE_SHIFT;
685 snd_soc_write(codec, WM8961_CLOCKING_4, reg);
687 reg = snd_soc_read(codec, WM8961_AUDIO_INTERFACE_0);
688 reg &= ~WM8961_WL_MASK;
689 switch (params_format(params)) {
690 case SNDRV_PCM_FORMAT_S16_LE:
692 case SNDRV_PCM_FORMAT_S20_3LE:
693 reg |= 1 << WM8961_WL_SHIFT;
695 case SNDRV_PCM_FORMAT_S24_LE:
696 reg |= 2 << WM8961_WL_SHIFT;
698 case SNDRV_PCM_FORMAT_S32_LE:
699 reg |= 3 << WM8961_WL_SHIFT;
704 snd_soc_write(codec, WM8961_AUDIO_INTERFACE_0, reg);
706 /* Sloping stop-band filter is recommended for <= 24kHz */
707 reg = snd_soc_read(codec, WM8961_ADC_DAC_CONTROL_2);
709 reg |= WM8961_DACSLOPE;
711 reg &= ~WM8961_DACSLOPE;
712 snd_soc_write(codec, WM8961_ADC_DAC_CONTROL_2, reg);
717 static int wm8961_set_sysclk(struct snd_soc_dai *dai, int clk_id,
721 struct snd_soc_codec *codec = dai->codec;
722 struct wm8961_priv *wm8961 = snd_soc_codec_get_drvdata(codec);
723 u16 reg = snd_soc_read(codec, WM8961_CLOCKING1);
725 if (freq > 33000000) {
726 dev_err(codec->dev, "MCLK must be <33MHz\n");
730 if (freq > 16500000) {
731 dev_dbg(codec->dev, "Using MCLK/2 for %dHz MCLK\n", freq);
732 reg |= WM8961_MCLKDIV;
735 dev_dbg(codec->dev, "Using MCLK/1 for %dHz MCLK\n", freq);
736 reg &= ~WM8961_MCLKDIV;
739 snd_soc_write(codec, WM8961_CLOCKING1, reg);
741 wm8961->sysclk = freq;
746 static int wm8961_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
748 struct snd_soc_codec *codec = dai->codec;
749 u16 aif = snd_soc_read(codec, WM8961_AUDIO_INTERFACE_0);
751 aif &= ~(WM8961_BCLKINV | WM8961_LRP |
752 WM8961_MS | WM8961_FORMAT_MASK);
754 switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
755 case SND_SOC_DAIFMT_CBM_CFM:
758 case SND_SOC_DAIFMT_CBS_CFS:
764 switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
765 case SND_SOC_DAIFMT_RIGHT_J:
768 case SND_SOC_DAIFMT_LEFT_J:
772 case SND_SOC_DAIFMT_I2S:
776 case SND_SOC_DAIFMT_DSP_B:
778 case SND_SOC_DAIFMT_DSP_A:
780 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
781 case SND_SOC_DAIFMT_NB_NF:
782 case SND_SOC_DAIFMT_IB_NF:
793 switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
794 case SND_SOC_DAIFMT_NB_NF:
796 case SND_SOC_DAIFMT_NB_IF:
799 case SND_SOC_DAIFMT_IB_NF:
800 aif |= WM8961_BCLKINV;
802 case SND_SOC_DAIFMT_IB_IF:
803 aif |= WM8961_BCLKINV | WM8961_LRP;
809 return snd_soc_write(codec, WM8961_AUDIO_INTERFACE_0, aif);
812 static int wm8961_set_tristate(struct snd_soc_dai *dai, int tristate)
814 struct snd_soc_codec *codec = dai->codec;
815 u16 reg = snd_soc_read(codec, WM8961_ADDITIONAL_CONTROL_2);
822 return snd_soc_write(codec, WM8961_ADDITIONAL_CONTROL_2, reg);
825 static int wm8961_digital_mute(struct snd_soc_dai *dai, int mute)
827 struct snd_soc_codec *codec = dai->codec;
828 u16 reg = snd_soc_read(codec, WM8961_ADC_DAC_CONTROL_1);
833 reg &= ~WM8961_DACMU;
837 return snd_soc_write(codec, WM8961_ADC_DAC_CONTROL_1, reg);
840 static int wm8961_set_clkdiv(struct snd_soc_dai *dai, int div_id, int div)
842 struct snd_soc_codec *codec = dai->codec;
847 reg = snd_soc_read(codec, WM8961_CLOCKING2);
848 reg &= ~WM8961_BCLKDIV_MASK;
850 snd_soc_write(codec, WM8961_CLOCKING2, reg);
854 reg = snd_soc_read(codec, WM8961_AUDIO_INTERFACE_2);
855 reg &= ~WM8961_LRCLK_RATE_MASK;
857 snd_soc_write(codec, WM8961_AUDIO_INTERFACE_2, reg);
867 static int wm8961_set_bias_level(struct snd_soc_codec *codec,
868 enum snd_soc_bias_level level)
872 /* This is all slightly unusual since we have no bypass paths
873 * and the output amplifier structure means we can just slam
874 * the biases straight up rather than having to ramp them
878 case SND_SOC_BIAS_ON:
881 case SND_SOC_BIAS_PREPARE:
882 if (codec->dapm.bias_level == SND_SOC_BIAS_STANDBY) {
883 /* Enable bias generation */
884 reg = snd_soc_read(codec, WM8961_ANTI_POP);
885 reg |= WM8961_BUFIOEN | WM8961_BUFDCOPEN;
886 snd_soc_write(codec, WM8961_ANTI_POP, reg);
888 /* VMID=2*50k, VREF */
889 reg = snd_soc_read(codec, WM8961_PWR_MGMT_1);
890 reg &= ~WM8961_VMIDSEL_MASK;
891 reg |= (1 << WM8961_VMIDSEL_SHIFT) | WM8961_VREF;
892 snd_soc_write(codec, WM8961_PWR_MGMT_1, reg);
896 case SND_SOC_BIAS_STANDBY:
897 if (codec->dapm.bias_level == SND_SOC_BIAS_PREPARE) {
899 reg = snd_soc_read(codec, WM8961_PWR_MGMT_1);
901 snd_soc_write(codec, WM8961_PWR_MGMT_1, reg);
903 /* Bias generation off */
904 reg = snd_soc_read(codec, WM8961_ANTI_POP);
905 reg &= ~(WM8961_BUFIOEN | WM8961_BUFDCOPEN);
906 snd_soc_write(codec, WM8961_ANTI_POP, reg);
909 reg = snd_soc_read(codec, WM8961_PWR_MGMT_1);
910 reg &= ~WM8961_VMIDSEL_MASK;
911 snd_soc_write(codec, WM8961_PWR_MGMT_1, reg);
915 case SND_SOC_BIAS_OFF:
919 codec->dapm.bias_level = level;
925 #define WM8961_RATES SNDRV_PCM_RATE_8000_48000
927 #define WM8961_FORMATS \
928 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
929 SNDRV_PCM_FMTBIT_S24_LE)
931 static const struct snd_soc_dai_ops wm8961_dai_ops = {
932 .hw_params = wm8961_hw_params,
933 .set_sysclk = wm8961_set_sysclk,
934 .set_fmt = wm8961_set_fmt,
935 .digital_mute = wm8961_digital_mute,
936 .set_tristate = wm8961_set_tristate,
937 .set_clkdiv = wm8961_set_clkdiv,
940 static struct snd_soc_dai_driver wm8961_dai = {
941 .name = "wm8961-hifi",
943 .stream_name = "HiFi Playback",
946 .rates = WM8961_RATES,
947 .formats = WM8961_FORMATS,},
949 .stream_name = "HiFi Capture",
952 .rates = WM8961_RATES,
953 .formats = WM8961_FORMATS,},
954 .ops = &wm8961_dai_ops,
957 static int wm8961_probe(struct snd_soc_codec *codec)
959 struct snd_soc_dapm_context *dapm = &codec->dapm;
963 ret = snd_soc_codec_set_cache_io(codec, 8, 16, SND_SOC_I2C);
965 dev_err(codec->dev, "Failed to set cache I/O: %d\n", ret);
969 reg = snd_soc_read(codec, WM8961_SOFTWARE_RESET);
971 dev_err(codec->dev, "Device is not a WM8961: ID=0x%x\n", reg);
975 /* This isn't volatile - readback doesn't correspond to write */
976 codec->cache_bypass = 1;
977 reg = snd_soc_read(codec, WM8961_RIGHT_INPUT_VOLUME);
978 codec->cache_bypass = 0;
979 dev_info(codec->dev, "WM8961 family %d revision %c\n",
980 (reg & WM8961_DEVICE_ID_MASK) >> WM8961_DEVICE_ID_SHIFT,
981 ((reg & WM8961_CHIP_REV_MASK) >> WM8961_CHIP_REV_SHIFT)
984 ret = wm8961_reset(codec);
986 dev_err(codec->dev, "Failed to issue reset\n");
991 reg = snd_soc_read(codec, WM8961_CHARGE_PUMP_B);
992 reg |= WM8961_CP_DYN_PWR_MASK;
993 snd_soc_write(codec, WM8961_CHARGE_PUMP_B, reg);
995 /* Latch volume update bits (right channel only, we always
996 * write both out) and default ZC on. */
997 reg = snd_soc_read(codec, WM8961_ROUT1_VOLUME);
998 snd_soc_write(codec, WM8961_ROUT1_VOLUME,
999 reg | WM8961_LO1ZC | WM8961_OUT1VU);
1000 snd_soc_write(codec, WM8961_LOUT1_VOLUME, reg | WM8961_LO1ZC);
1001 reg = snd_soc_read(codec, WM8961_ROUT2_VOLUME);
1002 snd_soc_write(codec, WM8961_ROUT2_VOLUME,
1003 reg | WM8961_SPKRZC | WM8961_SPKVU);
1004 snd_soc_write(codec, WM8961_LOUT2_VOLUME, reg | WM8961_SPKLZC);
1006 reg = snd_soc_read(codec, WM8961_RIGHT_ADC_VOLUME);
1007 snd_soc_write(codec, WM8961_RIGHT_ADC_VOLUME, reg | WM8961_ADCVU);
1008 reg = snd_soc_read(codec, WM8961_RIGHT_INPUT_VOLUME);
1009 snd_soc_write(codec, WM8961_RIGHT_INPUT_VOLUME, reg | WM8961_IPVU);
1011 /* Use soft mute by default */
1012 reg = snd_soc_read(codec, WM8961_ADC_DAC_CONTROL_2);
1013 reg |= WM8961_DACSMM;
1014 snd_soc_write(codec, WM8961_ADC_DAC_CONTROL_2, reg);
1016 /* Use automatic clocking mode by default; for now this is all
1019 reg = snd_soc_read(codec, WM8961_CLOCKING_3);
1020 reg &= ~WM8961_MANUAL_MODE;
1021 snd_soc_write(codec, WM8961_CLOCKING_3, reg);
1023 wm8961_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1025 snd_soc_add_codec_controls(codec, wm8961_snd_controls,
1026 ARRAY_SIZE(wm8961_snd_controls));
1027 snd_soc_dapm_new_controls(dapm, wm8961_dapm_widgets,
1028 ARRAY_SIZE(wm8961_dapm_widgets));
1029 snd_soc_dapm_add_routes(dapm, audio_paths, ARRAY_SIZE(audio_paths));
1034 static int wm8961_remove(struct snd_soc_codec *codec)
1036 wm8961_set_bias_level(codec, SND_SOC_BIAS_OFF);
1041 static int wm8961_suspend(struct snd_soc_codec *codec)
1043 wm8961_set_bias_level(codec, SND_SOC_BIAS_OFF);
1048 static int wm8961_resume(struct snd_soc_codec *codec)
1050 snd_soc_cache_sync(codec);
1052 wm8961_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
1057 #define wm8961_suspend NULL
1058 #define wm8961_resume NULL
1061 static struct snd_soc_codec_driver soc_codec_dev_wm8961 = {
1062 .probe = wm8961_probe,
1063 .remove = wm8961_remove,
1064 .suspend = wm8961_suspend,
1065 .resume = wm8961_resume,
1066 .set_bias_level = wm8961_set_bias_level,
1067 .reg_cache_size = ARRAY_SIZE(wm8961_reg_defaults),
1068 .reg_word_size = sizeof(u16),
1069 .reg_cache_default = wm8961_reg_defaults,
1070 .volatile_register = wm8961_volatile_register,
1073 static __devinit int wm8961_i2c_probe(struct i2c_client *i2c,
1074 const struct i2c_device_id *id)
1076 struct wm8961_priv *wm8961;
1079 wm8961 = devm_kzalloc(&i2c->dev, sizeof(struct wm8961_priv),
1084 i2c_set_clientdata(i2c, wm8961);
1086 ret = snd_soc_register_codec(&i2c->dev,
1087 &soc_codec_dev_wm8961, &wm8961_dai, 1);
1092 static __devexit int wm8961_i2c_remove(struct i2c_client *client)
1094 snd_soc_unregister_codec(&client->dev);
1099 static const struct i2c_device_id wm8961_i2c_id[] = {
1103 MODULE_DEVICE_TABLE(i2c, wm8961_i2c_id);
1105 static struct i2c_driver wm8961_i2c_driver = {
1108 .owner = THIS_MODULE,
1110 .probe = wm8961_i2c_probe,
1111 .remove = __devexit_p(wm8961_i2c_remove),
1112 .id_table = wm8961_i2c_id,
1115 static int __init wm8961_modinit(void)
1118 ret = i2c_add_driver(&wm8961_i2c_driver);
1120 printk(KERN_ERR "Failed to register wm8961 I2C driver: %d\n",
1125 module_init(wm8961_modinit);
1127 static void __exit wm8961_exit(void)
1129 i2c_del_driver(&wm8961_i2c_driver);
1131 module_exit(wm8961_exit);
1133 MODULE_DESCRIPTION("ASoC WM8961 driver");
1134 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1135 MODULE_LICENSE("GPL");