Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH v5] ASoC: wm8731: activate the interface after the inputs, before the outputs
@ 2026-12-04 20:54 Rafael Diniz
  2026-12-04 21:11 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Rafael Diniz @ 2026-12-04 20:54 UTC (permalink / raw)
  To: Mark Brown, Liam Girdwood
  Cc: Rafael Diniz, Charles Keepax, Jaroslav Kysela, Takashi Iwai,
	patches, linux-sound

The datasheet's power-up sequence powers everything but the outputs,
sets the other registers, activates the digital audio interface and
clears OUTPD last.  The driver has ACTIVE as a supply widget of its own,
which DAPM powers ahead of the ADC and DAC, so every stream start powers
a converter up under an active interface.

With the codec as clock provider at 96 kHz from a 12 MHz crystal (USB
mode), as on the AudioInjector stereo card for the Raspberry Pi, about
one capture start in five then leaves the ADC delivering its DC offset
and noise at their usual levels but no signal, until the interface is
deactivated and activated again.  Both orders reproduce it on a running
codec over I2C: the driver's start order (interface active, then the ADC
powered up) went deaf in 1 of 8 trials, and powering the ADC down and up
under an active interface in 2 of 8.  Activating the interface with the
ADC already powered stayed healthy in 42 of 42.

Power the ADC and DAC from one supply with the interface inactive, and
set ACTIVE at the last step before the outputs.  DAPM powers supplies
first, then the microphone bias, the output mixer, the line input and
the ADC, so OUTPD moves from the output mixer to an output driver widget
of its own, which DAPM powers after all of them.  For capture the ADC's
POST_PMU sets ACTIVE, after the microphone and line inputs; for playback
the output driver's PRE_PMU sets it, before OUTPD is cleared, unless the
converters are down (the line bypass alone).  When the outputs are
already up for the line bypass, the DAC's POST_PMU sets it.  The supply
clears ACTIVE in PRE_PMD, once everything else is down, before powering
the converters down.  The events run on any power change, a mixer switch
as well as a stream start, and a second stream finds the converters
powered and the interface left as it is.  A subsequence orders the
supply after the oscillator going up and before it going down.

The register writes traced on that card follow the datasheet's order:
oscillator, ADC and DAC, ACTIVE for capture, with MICPD cleared before
ACTIVE when the microphone input is selected; oscillator, ADC and DAC,
ACTIVE, then OUTPD for playback; OUTPD, ACTIVE, ADC and DAC, oscillator
going down.  Starting playback under the line bypass, with the outputs
already up, sets ACTIVE after the converters from the DAC's event, and
the line bypass alone powers only the outputs.  The card's machine
driver keeps the line input powered; with its route corrected in a test
build, capture from the line input traces as oscillator, ADC and DAC,
LINEINPD, ACTIVE, and going down as LINEINPD, ACTIVE, ADC and DAC,
oscillator.

Tested on a Raspberry Pi 4 with that card, 6.18.60 (Raspberry Pi kernel,
where the driver is the same as here): after each of 30 daemon restarts,
each a fresh capture and playback start with nothing in user space
cycling the interface, the capture decoded a test transmission from a
second station; before this patch about one start in five came up deaf.
Built with W=1 on mainline.

The analysis, the I2C reproducers, the patch and this changelog were
written with Claude Code (Claude Opus 5.5) from the reported fault, the
kernel sources and the reviews of v2, v3 and v4, and the tool ran the
hardware tests on the submitter's test setup at the submitter's
direction.

Fixes: 8a27bd9a3318 ("ASoC: Manage WM8731 ACTIVE bit as a supply widget")
Link: https://github.com/raspberrypi/linux/issues/7776
Signed-off-by: Rafael Diniz <rafael@riseup.net>
---
Changes in v5:
- v4 2/2 is in for-7.5 already (bc34e9913b7e), so this is v4 1/2 alone,
  on top of it.
- No code change.  The changelog adds the line input's place in the
  order, traced with the card's line-in route corrected (Charles).

v4: https://lore.kernel.org/linux-sound/20261130201710.3737693-1-rafael@riseup.net/

 sound/soc/codecs/wm8731.c | 108 ++++++++++++++++++++++++++++++++++----
 sound/soc/codecs/wm8731.h |   8 +++
 2 files changed, 106 insertions(+), 10 deletions(-)

diff --git a/sound/soc/codecs/wm8731.c b/sound/soc/codecs/wm8731.c
index f22f57b05..b19f7b106 100644
--- a/sound/soc/codecs/wm8731.c
+++ b/sound/soc/codecs/wm8731.c
@@ -167,18 +167,105 @@ SOC_DAPM_SINGLE("HiFi Playback Switch", WM8731_APANA, 4, 1, 0),
 static const struct snd_kcontrol_new wm8731_input_mux_controls =
 SOC_DAPM_ENUM("Input Select", wm8731_insel_enum);
 
+/*
+ * The datasheet's power-up sequence powers everything but the outputs, sets
+ * the other registers, activates the digital audio interface and powers the
+ * outputs last.  With the codec as clock provider at 96 kHz (12 MHz crystal,
+ * USB mode), powering the ADC up under an active interface left it, about
+ * one time in five, delivering its DC offset and noise but no signal, until
+ * the interface was deactivated and activated again.
+ *
+ * So a supply powers the ADC and DAC with the interface inactive, and the
+ * interface is activated at the last step before the outputs: after the ADC
+ * for capture, which DAPM powers after the microphone and line inputs, and
+ * before the output drivers (OUTPD) for playback.  When the outputs are
+ * already up, for the line bypass, the DAC activates it.  The supply clears
+ * ACTIVE once everything else is down, before powering the converters down.
+ * A second stream finds the converters powered and the interface left as it
+ * is.  The subsequence puts the supply after the oscillator going up and
+ * before it going down.
+ */
+#define WM8731_PWR_CONVERTERS	(WM8731_PWR_ADCPD | WM8731_PWR_DACPD)
+
+static void wm8731_activate(struct snd_soc_component *component)
+{
+	snd_soc_component_update_bits(component, WM8731_ACTIVE,
+				      WM8731_ACTIVE_ACTIVE,
+				      WM8731_ACTIVE_ACTIVE);
+}
+
+static int wm8731_converters_event(struct snd_soc_dapm_widget *w,
+				   struct snd_kcontrol *kcontrol, int event)
+{
+	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
+
+	switch (event) {
+	case SND_SOC_DAPM_PRE_PMU:
+		snd_soc_component_update_bits(component, WM8731_PWR,
+					      WM8731_PWR_CONVERTERS, 0);
+		break;
+	case SND_SOC_DAPM_PRE_PMD:
+		snd_soc_component_update_bits(component, WM8731_ACTIVE,
+					      WM8731_ACTIVE_ACTIVE, 0);
+		break;
+	case SND_SOC_DAPM_POST_PMD:
+		snd_soc_component_update_bits(component, WM8731_PWR,
+					      WM8731_PWR_CONVERTERS,
+					      WM8731_PWR_CONVERTERS);
+		break;
+	}
+	return 0;
+}
+
+static int wm8731_adc_event(struct snd_soc_dapm_widget *w,
+			    struct snd_kcontrol *kcontrol, int event)
+{
+	wm8731_activate(snd_soc_dapm_to_component(w->dapm));
+	return 0;
+}
+
+static int wm8731_dac_event(struct snd_soc_dapm_widget *w,
+			    struct snd_kcontrol *kcontrol, int event)
+{
+	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
+
+	/* Otherwise the outputs' own event activates it */
+	if (!(snd_soc_component_read(component, WM8731_PWR) & WM8731_PWR_OUTPD))
+		wm8731_activate(component);
+	return 0;
+}
+
+static int wm8731_outputs_event(struct snd_soc_dapm_widget *w,
+				struct snd_kcontrol *kcontrol, int event)
+{
+	struct snd_soc_component *component = snd_soc_dapm_to_component(w->dapm);
+
+	/* Not for the line bypass alone, with the converters down */
+	if (!(snd_soc_component_read(component, WM8731_PWR) &
+	      WM8731_PWR_CONVERTERS))
+		wm8731_activate(component);
+	return 0;
+}
+
 static const struct snd_soc_dapm_widget wm8731_dapm_widgets[] = {
-SND_SOC_DAPM_SUPPLY("ACTIVE",WM8731_ACTIVE, 0, 0, NULL, 0),
 SND_SOC_DAPM_SUPPLY("OSC", WM8731_PWR, 5, 1, NULL, 0),
-SND_SOC_DAPM_MIXER("Output Mixer", WM8731_PWR, 4, 1,
+SND_SOC_DAPM_SUPPLY_S("Converters", 1, SND_SOC_NOPM, 0, 0,
+		      wm8731_converters_event,
+		      SND_SOC_DAPM_PRE_PMU |
+		      SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMD),
+SND_SOC_DAPM_MIXER("Output Mixer", SND_SOC_NOPM, 0, 0,
 	&wm8731_output_mixer_controls[0],
 	ARRAY_SIZE(wm8731_output_mixer_controls)),
-SND_SOC_DAPM_DAC("DAC", "HiFi Playback", WM8731_PWR, 3, 1),
+SND_SOC_DAPM_OUT_DRV_E("Outputs", WM8731_PWR, 4, 1, NULL, 0,
+		       wm8731_outputs_event, SND_SOC_DAPM_PRE_PMU),
+SND_SOC_DAPM_DAC_E("DAC", "HiFi Playback", SND_SOC_NOPM, 0, 0,
+		   wm8731_dac_event, SND_SOC_DAPM_POST_PMU),
 SND_SOC_DAPM_OUTPUT("LOUT"),
 SND_SOC_DAPM_OUTPUT("LHPOUT"),
 SND_SOC_DAPM_OUTPUT("ROUT"),
 SND_SOC_DAPM_OUTPUT("RHPOUT"),
-SND_SOC_DAPM_ADC("ADC", "HiFi Capture", WM8731_PWR, 2, 1),
+SND_SOC_DAPM_ADC_E("ADC", "HiFi Capture", SND_SOC_NOPM, 0, 0,
+		   wm8731_adc_event, SND_SOC_DAPM_POST_PMU),
 SND_SOC_DAPM_MUX("Input Mux", SND_SOC_NOPM, 0, 0, &wm8731_input_mux_controls),
 SND_SOC_DAPM_PGA("Line Input", WM8731_PWR, 0, 1, NULL, 0),
 SND_SOC_DAPM_MICBIAS("Mic Bias", WM8731_PWR, 1, 1),
@@ -199,8 +286,8 @@ static int wm8731_check_osc(struct snd_soc_dapm_widget *source,
 static const struct snd_soc_dapm_route wm8731_intercon[] = {
 	{"DAC", NULL, "OSC", wm8731_check_osc},
 	{"ADC", NULL, "OSC", wm8731_check_osc},
-	{"DAC", NULL, "ACTIVE"},
-	{"ADC", NULL, "ACTIVE"},
+	{"DAC", NULL, "Converters"},
+	{"ADC", NULL, "Converters"},
 
 	/* output mixer */
 	{"Output Mixer", "Line Bypass Switch", "Line Input"},
@@ -208,10 +295,11 @@ static const struct snd_soc_dapm_route wm8731_intercon[] = {
 	{"Output Mixer", "Mic Sidetone Switch", "Mic Bias"},
 
 	/* outputs */
-	{"RHPOUT", NULL, "Output Mixer"},
-	{"ROUT", NULL, "Output Mixer"},
-	{"LHPOUT", NULL, "Output Mixer"},
-	{"LOUT", NULL, "Output Mixer"},
+	{"Outputs", NULL, "Output Mixer"},
+	{"RHPOUT", NULL, "Outputs"},
+	{"ROUT", NULL, "Outputs"},
+	{"LHPOUT", NULL, "Outputs"},
+	{"LOUT", NULL, "Outputs"},
 
 	/* input mux */
 	{"Input Mux", "Line In", "Line Input"},
diff --git a/sound/soc/codecs/wm8731.h b/sound/soc/codecs/wm8731.h
index 8d5a7a9ca..bc75dac04 100644
--- a/sound/soc/codecs/wm8731.h
+++ b/sound/soc/codecs/wm8731.h
@@ -33,6 +33,14 @@ struct snd_pcm_hw_constraint_list;
 #define WM8731_ACTIVE   0x09
 #define WM8731_RESET	0x0f
 
+/* WM8731_PWR */
+#define WM8731_PWR_ADCPD	0x0004
+#define WM8731_PWR_DACPD	0x0008
+#define WM8731_PWR_OUTPD	0x0010
+
+/* WM8731_ACTIVE */
+#define WM8731_ACTIVE_ACTIVE	0x0001
+
 #define WM8731_CACHEREGNUM 	10
 
 #define WM8731_SYSCLK_MCLK 0

base-commit: 441d6a9067353d1473fd2f22e0cc1bf9c40a5953
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-04 21:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 20:54 [PATCH v5] ASoC: wm8731: activate the interface after the inputs, before the outputs Rafael Diniz
2026-12-04 21:11 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox