2 * wm8961.c -- WM8961 ALSA SoC Audio driver
4 * Copyright 2009-10 Wolfson Microelectronics, plc
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * Currently unimplemented features:
16 #include <linux/module.h>
17 #include <linux/moduleparam.h>
18 #include <linux/init.h>
19 #include <linux/delay.h>
21 #include <linux/i2c.h>
22 #include <linux/slab.h>
23 #include <sound/core.h>
24 #include <sound/pcm.h>
25 #include <sound/pcm_params.h>
26 #include <sound/soc.h>
27 #include <sound/initval.h>
28 #include <sound/tlv.h>
32 #define WM8961_MAX_REGISTER 0xFC
34 static u16 wm8961_reg_defaults
[] = {
35 0x009F, /* R0 - Left Input volume */
36 0x009F, /* R1 - Right Input volume */
37 0x0000, /* R2 - LOUT1 volume */
38 0x0000, /* R3 - ROUT1 volume */
39 0x0020, /* R4 - Clocking1 */
40 0x0008, /* R5 - ADC & DAC Control 1 */
41 0x0000, /* R6 - ADC & DAC Control 2 */
42 0x000A, /* R7 - Audio Interface 0 */
43 0x01F4, /* R8 - Clocking2 */
44 0x0000, /* R9 - Audio Interface 1 */
45 0x00FF, /* R10 - Left DAC volume */
46 0x00FF, /* R11 - Right DAC volume */
49 0x0040, /* R14 - Audio Interface 2 */
50 0x0000, /* R15 - Software Reset */
52 0x007B, /* R17 - ALC1 */
53 0x0000, /* R18 - ALC2 */
54 0x0032, /* R19 - ALC3 */
55 0x0000, /* R20 - Noise Gate */
56 0x00C0, /* R21 - Left ADC volume */
57 0x00C0, /* R22 - Right ADC volume */
58 0x0120, /* R23 - Additional control(1) */
59 0x0000, /* R24 - Additional control(2) */
60 0x0000, /* R25 - Pwr Mgmt (1) */
61 0x0000, /* R26 - Pwr Mgmt (2) */
62 0x0000, /* R27 - Additional Control (3) */
63 0x0000, /* R28 - Anti-pop */
65 0x005F, /* R30 - Clocking 3 */
67 0x0000, /* R32 - ADCL signal path */
68 0x0000, /* R33 - ADCR signal path */
75 0x0000, /* R40 - LOUT2 volume */
76 0x0000, /* R41 - ROUT2 volume */
82 0x0000, /* R47 - Pwr Mgmt (3) */
83 0x0023, /* R48 - Additional Control (4) */
84 0x0000, /* R49 - Class D Control 1 */
86 0x0003, /* R51 - Class D Control 2 */
91 0x0106, /* R56 - Clocking 4 */
92 0x0000, /* R57 - DSP Sidetone 0 */
93 0x0000, /* R58 - DSP Sidetone 1 */
95 0x0000, /* R60 - DC Servo 0 */
96 0x0000, /* R61 - DC Servo 1 */
98 0x015E, /* R63 - DC Servo 3 */
100 0x0010, /* R65 - DC Servo 5 */
103 0x0003, /* R68 - Analogue PGA Bias */
104 0x0000, /* R69 - Analogue HP 0 */
106 0x01FB, /* R71 - Analogue HP 2 */
107 0x0000, /* R72 - Charge Pump 1 */
117 0x0000, /* R82 - Charge Pump B */
122 0x0000, /* R87 - Write Sequencer 1 */
123 0x0000, /* R88 - Write Sequencer 2 */
124 0x0000, /* R89 - Write Sequencer 3 */
125 0x0000, /* R90 - Write Sequencer 4 */
126 0x0000, /* R91 - Write Sequencer 5 */
127 0x0000, /* R92 - Write Sequencer 6 */
128 0x0000, /* R93 - Write Sequencer 7 */
287 0x0001, /* R252 - General test 1 */
291 enum snd_soc_control_type control_type
;
295 static int wm8961_volatile_register(struct snd_soc_codec
*codec
, unsigned int reg
)
298 case WM8961_SOFTWARE_RESET
:
299 case WM8961_WRITE_SEQUENCER_7
:
300 case WM8961_DC_SERVO_1
:
308 static int wm8961_reset(struct snd_soc_codec
*codec
)
310 return snd_soc_write(codec
, WM8961_SOFTWARE_RESET
, 0);
314 * The headphone output supports special anti-pop sequences giving
315 * silent power up and power down.
317 static int wm8961_hp_event(struct snd_soc_dapm_widget
*w
,
318 struct snd_kcontrol
*kcontrol
, int event
)
320 struct snd_soc_codec
*codec
= w
->codec
;
321 u16 hp_reg
= snd_soc_read(codec
, WM8961_ANALOGUE_HP_0
);
322 u16 cp_reg
= snd_soc_read(codec
, WM8961_CHARGE_PUMP_1
);
323 u16 pwr_reg
= snd_soc_read(codec
, WM8961_PWR_MGMT_2
);
324 u16 dcs_reg
= snd_soc_read(codec
, WM8961_DC_SERVO_1
);
327 if (event
& SND_SOC_DAPM_POST_PMU
) {
328 /* Make sure the output is shorted */
329 hp_reg
&= ~(WM8961_HPR_RMV_SHORT
| WM8961_HPL_RMV_SHORT
);
330 snd_soc_write(codec
, WM8961_ANALOGUE_HP_0
, hp_reg
);
332 /* Enable the charge pump */
333 cp_reg
|= WM8961_CP_ENA
;
334 snd_soc_write(codec
, WM8961_CHARGE_PUMP_1
, cp_reg
);
338 pwr_reg
|= WM8961_LOUT1_PGA
| WM8961_ROUT1_PGA
;
339 snd_soc_write(codec
, WM8961_PWR_MGMT_2
, pwr_reg
);
341 /* Enable the amplifier */
342 hp_reg
|= WM8961_HPR_ENA
| WM8961_HPL_ENA
;
343 snd_soc_write(codec
, WM8961_ANALOGUE_HP_0
, hp_reg
);
345 /* Second stage enable */
346 hp_reg
|= WM8961_HPR_ENA_DLY
| WM8961_HPL_ENA_DLY
;
347 snd_soc_write(codec
, WM8961_ANALOGUE_HP_0
, hp_reg
);
349 /* Enable the DC servo & trigger startup */
351 WM8961_DCS_ENA_CHAN_HPR
| WM8961_DCS_TRIG_STARTUP_HPR
|
352 WM8961_DCS_ENA_CHAN_HPL
| WM8961_DCS_TRIG_STARTUP_HPL
;
353 dev_dbg(codec
->dev
, "Enabling DC servo\n");
355 snd_soc_write(codec
, WM8961_DC_SERVO_1
, dcs_reg
);
358 dcs_reg
= snd_soc_read(codec
, WM8961_DC_SERVO_1
);
359 } while (--timeout
&&
360 dcs_reg
& (WM8961_DCS_TRIG_STARTUP_HPR
|
361 WM8961_DCS_TRIG_STARTUP_HPL
));
362 if (dcs_reg
& (WM8961_DCS_TRIG_STARTUP_HPR
|
363 WM8961_DCS_TRIG_STARTUP_HPL
))
364 dev_err(codec
->dev
, "DC servo timed out\n");
366 dev_dbg(codec
->dev
, "DC servo startup complete\n");
368 /* Enable the output stage */
369 hp_reg
|= WM8961_HPR_ENA_OUTP
| WM8961_HPL_ENA_OUTP
;
370 snd_soc_write(codec
, WM8961_ANALOGUE_HP_0
, hp_reg
);
372 /* Remove the short on the output stage */
373 hp_reg
|= WM8961_HPR_RMV_SHORT
| WM8961_HPL_RMV_SHORT
;
374 snd_soc_write(codec
, WM8961_ANALOGUE_HP_0
, hp_reg
);
377 if (event
& SND_SOC_DAPM_PRE_PMD
) {
378 /* Short the output */
379 hp_reg
&= ~(WM8961_HPR_RMV_SHORT
| WM8961_HPL_RMV_SHORT
);
380 snd_soc_write(codec
, WM8961_ANALOGUE_HP_0
, hp_reg
);
382 /* Disable the output stage */
383 hp_reg
&= ~(WM8961_HPR_ENA_OUTP
| WM8961_HPL_ENA_OUTP
);
384 snd_soc_write(codec
, WM8961_ANALOGUE_HP_0
, hp_reg
);
386 /* Disable DC offset cancellation */
387 dcs_reg
&= ~(WM8961_DCS_ENA_CHAN_HPR
|
388 WM8961_DCS_ENA_CHAN_HPL
);
389 snd_soc_write(codec
, WM8961_DC_SERVO_1
, dcs_reg
);
392 hp_reg
&= ~(WM8961_HPR_ENA_DLY
| WM8961_HPR_ENA
|
393 WM8961_HPL_ENA_DLY
| WM8961_HPL_ENA
);
394 snd_soc_write(codec
, WM8961_ANALOGUE_HP_0
, hp_reg
);
396 /* Disable the PGA */
397 pwr_reg
&= ~(WM8961_LOUT1_PGA
| WM8961_ROUT1_PGA
);
398 snd_soc_write(codec
, WM8961_PWR_MGMT_2
, pwr_reg
);
400 /* Disable the charge pump */
401 dev_dbg(codec
->dev
, "Disabling charge pump\n");
402 snd_soc_write(codec
, WM8961_CHARGE_PUMP_1
,
403 cp_reg
& ~WM8961_CP_ENA
);
409 static int wm8961_spk_event(struct snd_soc_dapm_widget
*w
,
410 struct snd_kcontrol
*kcontrol
, int event
)
412 struct snd_soc_codec
*codec
= w
->codec
;
413 u16 pwr_reg
= snd_soc_read(codec
, WM8961_PWR_MGMT_2
);
414 u16 spk_reg
= snd_soc_read(codec
, WM8961_CLASS_D_CONTROL_1
);
416 if (event
& SND_SOC_DAPM_POST_PMU
) {
418 pwr_reg
|= WM8961_SPKL_PGA
| WM8961_SPKR_PGA
;
419 snd_soc_write(codec
, WM8961_PWR_MGMT_2
, pwr_reg
);
421 /* Enable the amplifier */
422 spk_reg
|= WM8961_SPKL_ENA
| WM8961_SPKR_ENA
;
423 snd_soc_write(codec
, WM8961_CLASS_D_CONTROL_1
, spk_reg
);
426 if (event
& SND_SOC_DAPM_PRE_PMD
) {
427 /* Disable the amplifier */
428 spk_reg
&= ~(WM8961_SPKL_ENA
| WM8961_SPKR_ENA
);
429 snd_soc_write(codec
, WM8961_CLASS_D_CONTROL_1
, spk_reg
);
431 /* Disable the PGA */
432 pwr_reg
&= ~(WM8961_SPKL_PGA
| WM8961_SPKR_PGA
);
433 snd_soc_write(codec
, WM8961_PWR_MGMT_2
, pwr_reg
);
439 static const char *adc_hpf_text
[] = {
440 "Hi-fi", "Voice 1", "Voice 2", "Voice 3",
443 static const struct soc_enum adc_hpf
=
444 SOC_ENUM_SINGLE(WM8961_ADC_DAC_CONTROL_2
, 7, 4, adc_hpf_text
);
446 static const char *dac_deemph_text
[] = {
447 "None", "32kHz", "44.1kHz", "48kHz",
450 static const struct soc_enum dac_deemph
=
451 SOC_ENUM_SINGLE(WM8961_ADC_DAC_CONTROL_1
, 1, 4, dac_deemph_text
);
453 static const DECLARE_TLV_DB_SCALE(out_tlv
, -12100, 100, 1);
454 static const DECLARE_TLV_DB_SCALE(hp_sec_tlv
, -700, 100, 0);
455 static const DECLARE_TLV_DB_SCALE(adc_tlv
, -7200, 75, 1);
456 static const DECLARE_TLV_DB_SCALE(sidetone_tlv
, -3600, 300, 0);
457 static unsigned int boost_tlv
[] = {
458 TLV_DB_RANGE_HEAD(4),
459 0, 0, TLV_DB_SCALE_ITEM(0, 0, 0),
460 1, 1, TLV_DB_SCALE_ITEM(13, 0, 0),
461 2, 2, TLV_DB_SCALE_ITEM(20, 0, 0),
462 3, 3, TLV_DB_SCALE_ITEM(29, 0, 0),
464 static const DECLARE_TLV_DB_SCALE(pga_tlv
, -2325, 75, 0);
466 static const struct snd_kcontrol_new wm8961_snd_controls
[] = {
467 SOC_DOUBLE_R_TLV("Headphone Volume", WM8961_LOUT1_VOLUME
, WM8961_ROUT1_VOLUME
,
469 SOC_DOUBLE_TLV("Headphone Secondary Volume", WM8961_ANALOGUE_HP_2
,
470 6, 3, 7, 0, hp_sec_tlv
),
471 SOC_DOUBLE_R("Headphone ZC Switch", WM8961_LOUT1_VOLUME
, WM8961_ROUT1_VOLUME
,
474 SOC_DOUBLE_R_TLV("Speaker Volume", WM8961_LOUT2_VOLUME
, WM8961_ROUT2_VOLUME
,
476 SOC_DOUBLE_R("Speaker ZC Switch", WM8961_LOUT2_VOLUME
, WM8961_ROUT2_VOLUME
,
478 SOC_SINGLE("Speaker AC Gain", WM8961_CLASS_D_CONTROL_2
, 0, 7, 0),
480 SOC_SINGLE("DAC x128 OSR Switch", WM8961_ADC_DAC_CONTROL_2
, 0, 1, 0),
481 SOC_ENUM("DAC Deemphasis", dac_deemph
),
482 SOC_SINGLE("DAC Soft Mute Switch", WM8961_ADC_DAC_CONTROL_2
, 3, 1, 0),
484 SOC_DOUBLE_R_TLV("Sidetone Volume", WM8961_DSP_SIDETONE_0
,
485 WM8961_DSP_SIDETONE_1
, 4, 12, 0, sidetone_tlv
),
487 SOC_SINGLE("ADC High Pass Filter Switch", WM8961_ADC_DAC_CONTROL_1
, 0, 1, 0),
488 SOC_ENUM("ADC High Pass Filter Mode", adc_hpf
),
490 SOC_DOUBLE_R_TLV("Capture Volume",
491 WM8961_LEFT_ADC_VOLUME
, WM8961_RIGHT_ADC_VOLUME
,
493 SOC_DOUBLE_R_TLV("Capture Boost Volume",
494 WM8961_ADCL_SIGNAL_PATH
, WM8961_ADCR_SIGNAL_PATH
,
496 SOC_DOUBLE_R_TLV("Capture PGA Volume",
497 WM8961_LEFT_INPUT_VOLUME
, WM8961_RIGHT_INPUT_VOLUME
,
499 SOC_DOUBLE_R("Capture PGA ZC Switch",
500 WM8961_LEFT_INPUT_VOLUME
, WM8961_RIGHT_INPUT_VOLUME
,
502 SOC_DOUBLE_R("Capture PGA Switch",
503 WM8961_LEFT_INPUT_VOLUME
, WM8961_RIGHT_INPUT_VOLUME
,
507 static const char *sidetone_text
[] = {
508 "None", "Left", "Right"
511 static const struct soc_enum dacl_sidetone
=
512 SOC_ENUM_SINGLE(WM8961_DSP_SIDETONE_0
, 2, 3, sidetone_text
);
514 static const struct soc_enum dacr_sidetone
=
515 SOC_ENUM_SINGLE(WM8961_DSP_SIDETONE_1
, 2, 3, sidetone_text
);
517 static const struct snd_kcontrol_new dacl_mux
=
518 SOC_DAPM_ENUM("DACL Sidetone", dacl_sidetone
);
520 static const struct snd_kcontrol_new dacr_mux
=
521 SOC_DAPM_ENUM("DACR Sidetone", dacr_sidetone
);
523 static const struct snd_soc_dapm_widget wm8961_dapm_widgets
[] = {
524 SND_SOC_DAPM_INPUT("LINPUT"),
525 SND_SOC_DAPM_INPUT("RINPUT"),
527 SND_SOC_DAPM_SUPPLY("CLK_DSP", WM8961_CLOCKING2
, 4, 0, NULL
, 0),
529 SND_SOC_DAPM_PGA("Left Input", WM8961_PWR_MGMT_1
, 5, 0, NULL
, 0),
530 SND_SOC_DAPM_PGA("Right Input", WM8961_PWR_MGMT_1
, 4, 0, NULL
, 0),
532 SND_SOC_DAPM_ADC("ADCL", "HiFi Capture", WM8961_PWR_MGMT_1
, 3, 0),
533 SND_SOC_DAPM_ADC("ADCR", "HiFi Capture", WM8961_PWR_MGMT_1
, 2, 0),
535 SND_SOC_DAPM_SUPPLY("MICBIAS", WM8961_PWR_MGMT_1
, 1, 0, NULL
, 0),
537 SND_SOC_DAPM_MUX("DACL Sidetone", SND_SOC_NOPM
, 0, 0, &dacl_mux
),
538 SND_SOC_DAPM_MUX("DACR Sidetone", SND_SOC_NOPM
, 0, 0, &dacr_mux
),
540 SND_SOC_DAPM_DAC("DACL", "HiFi Playback", WM8961_PWR_MGMT_2
, 8, 0),
541 SND_SOC_DAPM_DAC("DACR", "HiFi Playback", WM8961_PWR_MGMT_2
, 7, 0),
543 /* Handle as a mono path for DCS */
544 SND_SOC_DAPM_PGA_E("Headphone Output", SND_SOC_NOPM
,
545 4, 0, NULL
, 0, wm8961_hp_event
,
546 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
547 SND_SOC_DAPM_PGA_E("Speaker Output", SND_SOC_NOPM
,
548 4, 0, NULL
, 0, wm8961_spk_event
,
549 SND_SOC_DAPM_POST_PMU
| SND_SOC_DAPM_PRE_PMD
),
551 SND_SOC_DAPM_OUTPUT("HP_L"),
552 SND_SOC_DAPM_OUTPUT("HP_R"),
553 SND_SOC_DAPM_OUTPUT("SPK_LN"),
554 SND_SOC_DAPM_OUTPUT("SPK_LP"),
555 SND_SOC_DAPM_OUTPUT("SPK_RN"),
556 SND_SOC_DAPM_OUTPUT("SPK_RP"),
560 static const struct snd_soc_dapm_route audio_paths
[] = {
561 { "DACL", NULL
, "CLK_DSP" },
562 { "DACL", NULL
, "DACL Sidetone" },
563 { "DACR", NULL
, "CLK_DSP" },
564 { "DACR", NULL
, "DACR Sidetone" },
566 { "DACL Sidetone", "Left", "ADCL" },
567 { "DACL Sidetone", "Right", "ADCR" },
569 { "DACR Sidetone", "Left", "ADCL" },
570 { "DACR Sidetone", "Right", "ADCR" },
572 { "HP_L", NULL
, "Headphone Output" },
573 { "HP_R", NULL
, "Headphone Output" },
574 { "Headphone Output", NULL
, "DACL" },
575 { "Headphone Output", NULL
, "DACR" },
577 { "SPK_LN", NULL
, "Speaker Output" },
578 { "SPK_LP", NULL
, "Speaker Output" },
579 { "SPK_RN", NULL
, "Speaker Output" },
580 { "SPK_RP", NULL
, "Speaker Output" },
582 { "Speaker Output", NULL
, "DACL" },
583 { "Speaker Output", NULL
, "DACR" },
585 { "ADCL", NULL
, "Left Input" },
586 { "ADCL", NULL
, "CLK_DSP" },
587 { "ADCR", NULL
, "Right Input" },
588 { "ADCR", NULL
, "CLK_DSP" },
590 { "Left Input", NULL
, "LINPUT" },
591 { "Right Input", NULL
, "RINPUT" },
595 /* Values for CLK_SYS_RATE */
599 } wm8961_clk_sys_ratio
[] = {
612 /* Values for SAMPLE_RATE */
628 static int wm8961_hw_params(struct snd_pcm_substream
*substream
,
629 struct snd_pcm_hw_params
*params
,
630 struct snd_soc_dai
*dai
)
632 struct snd_soc_codec
*codec
= dai
->codec
;
633 struct wm8961_priv
*wm8961
= snd_soc_codec_get_drvdata(codec
);
634 int i
, best
, target
, fs
;
637 fs
= params_rate(params
);
639 if (!wm8961
->sysclk
) {
640 dev_err(codec
->dev
, "MCLK has not been specified\n");
644 /* Find the closest sample rate for the filters */
646 for (i
= 0; i
< ARRAY_SIZE(wm8961_srate
); i
++) {
647 if (abs(wm8961_srate
[i
].rate
- fs
) <
648 abs(wm8961_srate
[best
].rate
- fs
))
651 reg
= snd_soc_read(codec
, WM8961_ADDITIONAL_CONTROL_3
);
652 reg
&= ~WM8961_SAMPLE_RATE_MASK
;
653 reg
|= wm8961_srate
[best
].val
;
654 snd_soc_write(codec
, WM8961_ADDITIONAL_CONTROL_3
, reg
);
655 dev_dbg(codec
->dev
, "Selected SRATE %dHz for %dHz\n",
656 wm8961_srate
[best
].rate
, fs
);
658 /* Select a CLK_SYS/fs ratio equal to or higher than required */
659 target
= wm8961
->sysclk
/ fs
;
661 if (substream
->stream
== SNDRV_PCM_STREAM_PLAYBACK
&& target
< 64) {
663 "SYSCLK must be at least 64*fs for DAC\n");
666 if (substream
->stream
== SNDRV_PCM_STREAM_CAPTURE
&& target
< 256) {
668 "SYSCLK must be at least 256*fs for ADC\n");
672 for (i
= 0; i
< ARRAY_SIZE(wm8961_clk_sys_ratio
); i
++) {
673 if (wm8961_clk_sys_ratio
[i
].ratio
>= target
)
676 if (i
== ARRAY_SIZE(wm8961_clk_sys_ratio
)) {
677 dev_err(codec
->dev
, "Unable to generate CLK_SYS_RATE\n");
680 dev_dbg(codec
->dev
, "Selected CLK_SYS_RATE of %d for %d/%d=%d\n",
681 wm8961_clk_sys_ratio
[i
].ratio
, wm8961
->sysclk
, fs
,
682 wm8961
->sysclk
/ fs
);
684 reg
= snd_soc_read(codec
, WM8961_CLOCKING_4
);
685 reg
&= ~WM8961_CLK_SYS_RATE_MASK
;
686 reg
|= wm8961_clk_sys_ratio
[i
].val
<< WM8961_CLK_SYS_RATE_SHIFT
;
687 snd_soc_write(codec
, WM8961_CLOCKING_4
, reg
);
689 reg
= snd_soc_read(codec
, WM8961_AUDIO_INTERFACE_0
);
690 reg
&= ~WM8961_WL_MASK
;
691 switch (params_format(params
)) {
692 case SNDRV_PCM_FORMAT_S16_LE
:
694 case SNDRV_PCM_FORMAT_S20_3LE
:
695 reg
|= 1 << WM8961_WL_SHIFT
;
697 case SNDRV_PCM_FORMAT_S24_LE
:
698 reg
|= 2 << WM8961_WL_SHIFT
;
700 case SNDRV_PCM_FORMAT_S32_LE
:
701 reg
|= 3 << WM8961_WL_SHIFT
;
706 snd_soc_write(codec
, WM8961_AUDIO_INTERFACE_0
, reg
);
708 /* Sloping stop-band filter is recommended for <= 24kHz */
709 reg
= snd_soc_read(codec
, WM8961_ADC_DAC_CONTROL_2
);
711 reg
|= WM8961_DACSLOPE
;
713 reg
&= ~WM8961_DACSLOPE
;
714 snd_soc_write(codec
, WM8961_ADC_DAC_CONTROL_2
, reg
);
719 static int wm8961_set_sysclk(struct snd_soc_dai
*dai
, int clk_id
,
723 struct snd_soc_codec
*codec
= dai
->codec
;
724 struct wm8961_priv
*wm8961
= snd_soc_codec_get_drvdata(codec
);
725 u16 reg
= snd_soc_read(codec
, WM8961_CLOCKING1
);
727 if (freq
> 33000000) {
728 dev_err(codec
->dev
, "MCLK must be <33MHz\n");
732 if (freq
> 16500000) {
733 dev_dbg(codec
->dev
, "Using MCLK/2 for %dHz MCLK\n", freq
);
734 reg
|= WM8961_MCLKDIV
;
737 dev_dbg(codec
->dev
, "Using MCLK/1 for %dHz MCLK\n", freq
);
738 reg
&= ~WM8961_MCLKDIV
;
741 snd_soc_write(codec
, WM8961_CLOCKING1
, reg
);
743 wm8961
->sysclk
= freq
;
748 static int wm8961_set_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
750 struct snd_soc_codec
*codec
= dai
->codec
;
751 u16 aif
= snd_soc_read(codec
, WM8961_AUDIO_INTERFACE_0
);
753 aif
&= ~(WM8961_BCLKINV
| WM8961_LRP
|
754 WM8961_MS
| WM8961_FORMAT_MASK
);
756 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
757 case SND_SOC_DAIFMT_CBM_CFM
:
760 case SND_SOC_DAIFMT_CBS_CFS
:
766 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
767 case SND_SOC_DAIFMT_RIGHT_J
:
770 case SND_SOC_DAIFMT_LEFT_J
:
774 case SND_SOC_DAIFMT_I2S
:
778 case SND_SOC_DAIFMT_DSP_B
:
780 case SND_SOC_DAIFMT_DSP_A
:
782 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
783 case SND_SOC_DAIFMT_NB_NF
:
784 case SND_SOC_DAIFMT_IB_NF
:
795 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
796 case SND_SOC_DAIFMT_NB_NF
:
798 case SND_SOC_DAIFMT_NB_IF
:
801 case SND_SOC_DAIFMT_IB_NF
:
802 aif
|= WM8961_BCLKINV
;
804 case SND_SOC_DAIFMT_IB_IF
:
805 aif
|= WM8961_BCLKINV
| WM8961_LRP
;
811 return snd_soc_write(codec
, WM8961_AUDIO_INTERFACE_0
, aif
);
814 static int wm8961_set_tristate(struct snd_soc_dai
*dai
, int tristate
)
816 struct snd_soc_codec
*codec
= dai
->codec
;
817 u16 reg
= snd_soc_read(codec
, WM8961_ADDITIONAL_CONTROL_2
);
824 return snd_soc_write(codec
, WM8961_ADDITIONAL_CONTROL_2
, reg
);
827 static int wm8961_digital_mute(struct snd_soc_dai
*dai
, int mute
)
829 struct snd_soc_codec
*codec
= dai
->codec
;
830 u16 reg
= snd_soc_read(codec
, WM8961_ADC_DAC_CONTROL_1
);
835 reg
&= ~WM8961_DACMU
;
839 return snd_soc_write(codec
, WM8961_ADC_DAC_CONTROL_1
, reg
);
842 static int wm8961_set_clkdiv(struct snd_soc_dai
*dai
, int div_id
, int div
)
844 struct snd_soc_codec
*codec
= dai
->codec
;
849 reg
= snd_soc_read(codec
, WM8961_CLOCKING2
);
850 reg
&= ~WM8961_BCLKDIV_MASK
;
852 snd_soc_write(codec
, WM8961_CLOCKING2
, reg
);
856 reg
= snd_soc_read(codec
, WM8961_AUDIO_INTERFACE_2
);
857 reg
&= ~WM8961_LRCLK_RATE_MASK
;
859 snd_soc_write(codec
, WM8961_AUDIO_INTERFACE_2
, reg
);
869 static int wm8961_set_bias_level(struct snd_soc_codec
*codec
,
870 enum snd_soc_bias_level level
)
874 /* This is all slightly unusual since we have no bypass paths
875 * and the output amplifier structure means we can just slam
876 * the biases straight up rather than having to ramp them
880 case SND_SOC_BIAS_ON
:
883 case SND_SOC_BIAS_PREPARE
:
884 if (codec
->dapm
.bias_level
== SND_SOC_BIAS_STANDBY
) {
885 /* Enable bias generation */
886 reg
= snd_soc_read(codec
, WM8961_ANTI_POP
);
887 reg
|= WM8961_BUFIOEN
| WM8961_BUFDCOPEN
;
888 snd_soc_write(codec
, WM8961_ANTI_POP
, reg
);
890 /* VMID=2*50k, VREF */
891 reg
= snd_soc_read(codec
, WM8961_PWR_MGMT_1
);
892 reg
&= ~WM8961_VMIDSEL_MASK
;
893 reg
|= (1 << WM8961_VMIDSEL_SHIFT
) | WM8961_VREF
;
894 snd_soc_write(codec
, WM8961_PWR_MGMT_1
, reg
);
898 case SND_SOC_BIAS_STANDBY
:
899 if (codec
->dapm
.bias_level
== SND_SOC_BIAS_PREPARE
) {
901 reg
= snd_soc_read(codec
, WM8961_PWR_MGMT_1
);
903 snd_soc_write(codec
, WM8961_PWR_MGMT_1
, reg
);
905 /* Bias generation off */
906 reg
= snd_soc_read(codec
, WM8961_ANTI_POP
);
907 reg
&= ~(WM8961_BUFIOEN
| WM8961_BUFDCOPEN
);
908 snd_soc_write(codec
, WM8961_ANTI_POP
, reg
);
911 reg
= snd_soc_read(codec
, WM8961_PWR_MGMT_1
);
912 reg
&= ~WM8961_VMIDSEL_MASK
;
913 snd_soc_write(codec
, WM8961_PWR_MGMT_1
, reg
);
917 case SND_SOC_BIAS_OFF
:
921 codec
->dapm
.bias_level
= level
;
927 #define WM8961_RATES SNDRV_PCM_RATE_8000_48000
929 #define WM8961_FORMATS \
930 (SNDRV_PCM_FMTBIT_S16_LE | SNDRV_PCM_FMTBIT_S20_3LE | \
931 SNDRV_PCM_FMTBIT_S24_LE)
933 static const struct snd_soc_dai_ops wm8961_dai_ops
= {
934 .hw_params
= wm8961_hw_params
,
935 .set_sysclk
= wm8961_set_sysclk
,
936 .set_fmt
= wm8961_set_fmt
,
937 .digital_mute
= wm8961_digital_mute
,
938 .set_tristate
= wm8961_set_tristate
,
939 .set_clkdiv
= wm8961_set_clkdiv
,
942 static struct snd_soc_dai_driver wm8961_dai
= {
943 .name
= "wm8961-hifi",
945 .stream_name
= "HiFi Playback",
948 .rates
= WM8961_RATES
,
949 .formats
= WM8961_FORMATS
,},
951 .stream_name
= "HiFi Capture",
954 .rates
= WM8961_RATES
,
955 .formats
= WM8961_FORMATS
,},
956 .ops
= &wm8961_dai_ops
,
959 static int wm8961_probe(struct snd_soc_codec
*codec
)
961 struct snd_soc_dapm_context
*dapm
= &codec
->dapm
;
965 ret
= snd_soc_codec_set_cache_io(codec
, 8, 16, SND_SOC_I2C
);
967 dev_err(codec
->dev
, "Failed to set cache I/O: %d\n", ret
);
971 reg
= snd_soc_read(codec
, WM8961_SOFTWARE_RESET
);
973 dev_err(codec
->dev
, "Device is not a WM8961: ID=0x%x\n", reg
);
977 /* This isn't volatile - readback doesn't correspond to write */
978 codec
->cache_bypass
= 1;
979 reg
= snd_soc_read(codec
, WM8961_RIGHT_INPUT_VOLUME
);
980 codec
->cache_bypass
= 0;
981 dev_info(codec
->dev
, "WM8961 family %d revision %c\n",
982 (reg
& WM8961_DEVICE_ID_MASK
) >> WM8961_DEVICE_ID_SHIFT
,
983 ((reg
& WM8961_CHIP_REV_MASK
) >> WM8961_CHIP_REV_SHIFT
)
986 ret
= wm8961_reset(codec
);
988 dev_err(codec
->dev
, "Failed to issue reset\n");
993 reg
= snd_soc_read(codec
, WM8961_CHARGE_PUMP_B
);
994 reg
|= WM8961_CP_DYN_PWR_MASK
;
995 snd_soc_write(codec
, WM8961_CHARGE_PUMP_B
, reg
);
997 /* Latch volume update bits (right channel only, we always
998 * write both out) and default ZC on. */
999 reg
= snd_soc_read(codec
, WM8961_ROUT1_VOLUME
);
1000 snd_soc_write(codec
, WM8961_ROUT1_VOLUME
,
1001 reg
| WM8961_LO1ZC
| WM8961_OUT1VU
);
1002 snd_soc_write(codec
, WM8961_LOUT1_VOLUME
, reg
| WM8961_LO1ZC
);
1003 reg
= snd_soc_read(codec
, WM8961_ROUT2_VOLUME
);
1004 snd_soc_write(codec
, WM8961_ROUT2_VOLUME
,
1005 reg
| WM8961_SPKRZC
| WM8961_SPKVU
);
1006 snd_soc_write(codec
, WM8961_LOUT2_VOLUME
, reg
| WM8961_SPKLZC
);
1008 reg
= snd_soc_read(codec
, WM8961_RIGHT_ADC_VOLUME
);
1009 snd_soc_write(codec
, WM8961_RIGHT_ADC_VOLUME
, reg
| WM8961_ADCVU
);
1010 reg
= snd_soc_read(codec
, WM8961_RIGHT_INPUT_VOLUME
);
1011 snd_soc_write(codec
, WM8961_RIGHT_INPUT_VOLUME
, reg
| WM8961_IPVU
);
1013 /* Use soft mute by default */
1014 reg
= snd_soc_read(codec
, WM8961_ADC_DAC_CONTROL_2
);
1015 reg
|= WM8961_DACSMM
;
1016 snd_soc_write(codec
, WM8961_ADC_DAC_CONTROL_2
, reg
);
1018 /* Use automatic clocking mode by default; for now this is all
1021 reg
= snd_soc_read(codec
, WM8961_CLOCKING_3
);
1022 reg
&= ~WM8961_MANUAL_MODE
;
1023 snd_soc_write(codec
, WM8961_CLOCKING_3
, reg
);
1025 wm8961_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
1027 snd_soc_add_codec_controls(codec
, wm8961_snd_controls
,
1028 ARRAY_SIZE(wm8961_snd_controls
));
1029 snd_soc_dapm_new_controls(dapm
, wm8961_dapm_widgets
,
1030 ARRAY_SIZE(wm8961_dapm_widgets
));
1031 snd_soc_dapm_add_routes(dapm
, audio_paths
, ARRAY_SIZE(audio_paths
));
1036 static int wm8961_remove(struct snd_soc_codec
*codec
)
1038 wm8961_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
1043 static int wm8961_suspend(struct snd_soc_codec
*codec
)
1045 wm8961_set_bias_level(codec
, SND_SOC_BIAS_OFF
);
1050 static int wm8961_resume(struct snd_soc_codec
*codec
)
1052 snd_soc_cache_sync(codec
);
1054 wm8961_set_bias_level(codec
, SND_SOC_BIAS_STANDBY
);
1059 #define wm8961_suspend NULL
1060 #define wm8961_resume NULL
1063 static struct snd_soc_codec_driver soc_codec_dev_wm8961
= {
1064 .probe
= wm8961_probe
,
1065 .remove
= wm8961_remove
,
1066 .suspend
= wm8961_suspend
,
1067 .resume
= wm8961_resume
,
1068 .set_bias_level
= wm8961_set_bias_level
,
1069 .reg_cache_size
= ARRAY_SIZE(wm8961_reg_defaults
),
1070 .reg_word_size
= sizeof(u16
),
1071 .reg_cache_default
= wm8961_reg_defaults
,
1072 .volatile_register
= wm8961_volatile_register
,
1075 static __devinit
int wm8961_i2c_probe(struct i2c_client
*i2c
,
1076 const struct i2c_device_id
*id
)
1078 struct wm8961_priv
*wm8961
;
1081 wm8961
= devm_kzalloc(&i2c
->dev
, sizeof(struct wm8961_priv
),
1086 i2c_set_clientdata(i2c
, wm8961
);
1088 ret
= snd_soc_register_codec(&i2c
->dev
,
1089 &soc_codec_dev_wm8961
, &wm8961_dai
, 1);
1094 static __devexit
int wm8961_i2c_remove(struct i2c_client
*client
)
1096 snd_soc_unregister_codec(&client
->dev
);
1101 static const struct i2c_device_id wm8961_i2c_id
[] = {
1105 MODULE_DEVICE_TABLE(i2c
, wm8961_i2c_id
);
1107 static struct i2c_driver wm8961_i2c_driver
= {
1110 .owner
= THIS_MODULE
,
1112 .probe
= wm8961_i2c_probe
,
1113 .remove
= __devexit_p(wm8961_i2c_remove
),
1114 .id_table
= wm8961_i2c_id
,
1117 static int __init
wm8961_modinit(void)
1120 ret
= i2c_add_driver(&wm8961_i2c_driver
);
1122 printk(KERN_ERR
"Failed to register wm8961 I2C driver: %d\n",
1127 module_init(wm8961_modinit
);
1129 static void __exit
wm8961_exit(void)
1131 i2c_del_driver(&wm8961_i2c_driver
);
1133 module_exit(wm8961_exit
);
1135 MODULE_DESCRIPTION("ASoC WM8961 driver");
1136 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
1137 MODULE_LICENSE("GPL");