Skip to contentSkip to conversation

ASoC: rt5631: Support an MCLK clock from the SoC

PATCH In review Romain TISSERAND 2 participants 4 messages Nov 1 – Nov 2

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

v2 cover ASoC: rt5631: Support an MCLK clock from the SoC

The RT5631 driver doesn't take a master clock, so when MCLK comes from a SoC clock output, that clock is turned off at boot as unused. This series adds an optional "mclk" clock to the binding and enables it in the driver while the codec is active, as rt5616 does.

The board that needs it is the GPD XD (RK3288), whose device tree is not upstream yet. Its MCLK is i2s0_clkout: handed to simple-audio-card, the mux picked the 12 MHz oscillator for 12.288 MHz (wrong pitch); held by the codec, it follows the I2S clock, as on the RK3288 boards with ES8388 and MAX98090 codecs.

Changes in v2:
- Patch 2: get the clock in the I2C probe, as rt5616 does since
b5c9df40ca56: from the component probe a deferral was lost and a rebind leaked the clock (reported by Sashiko).
- Patch 1: unchanged; picked up Krzysztof's Reviewed-by.
- v1: https://lore.kernel.org/all/[email protected]/

Testing: dt_binding_check for realtek,rt5631.yaml; W=1 builds for arm (multi_v7) and arm64 (defconfig) with SND_SOC_RT5631=y and =m, no warnings. Tested live on the GPD XD (RT5631 on simple-audio-card, mclk-fs 256) on 6.18.54 with this change backported (the bias level read uses snd_soc_component_get_bias_level() there): i2s0_clkout stays on the I2S clock at 11.2896 MHz for 44.1 kHz and 12.288 MHz for 48 kHz, and both play at the right pitch; v2 tested the same way, with the clock taken in the I2C probe. Boards without an "mclk" clock are not affected (clk_prepare_enable() on a NULL clock does nothing); not tested on other boards.

Tool disclosure: this series was prepared with Claude Opus 5.5 (Anthropic) as a coding assistant, in an interactive session where I asked it to fix the pitch problem without the RK3288 clock change that was refused in 2014. It traced the clock ordering in simple-audio-card, proposed taking the clock in the codec driver following rt5616, and drafted the patches and the messages; v2 follows the Sashiko review. I reviewed and understand them, and tested them on the GPD XD as described above.

Romain TISSERAND (2):
ASoC: dt-bindings: realtek,rt5631: Add the optional MCLK
ASoC: codecs: rt5631: Enable the optional MCLK

 .../bindings/sound/realtek,rt5631.yaml         |  8 ++++++++
 sound/soc/codecs/rt5631.c                      | 18 +++++++++++++++++-
 2 files changed, 25 insertions(+), 1 deletion(-)

base-commit: cc3fc99f1bce93c3ae3771ef98cb91a566da857e

Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/2 ASoC: dt-bindings: realtek,rt5631: Add the optional MCLK

The RT5631 runs from a master clock on its MCLK pin. On boards where that clock comes from a SoC clock output, the codec node needs to name it so the driver can keep it running while the codec is in use.

Document an optional "mclk" clock, as the realtek,rt5616 binding does.

---
 .../devicetree/bindings/sound/realtek,rt5631.yaml         | 8 ++++++++
 1 file changed, 8 insertions(+)
Documentation/devicetree/bindings/sound/realtek,rt5631.yaml +8 -0
index 747a731c44..03ee25e1eb 100644
@@ -45,6 +45,14 @@ properties:
   reg:
     maxItems: 1
 
+  clocks:
+    items:
+      - description: Master clock to the CODEC
+
+  clock-names:
+    items:
+      - const: mclk
+
   port:
     $ref: audio-graph-port.yaml#
     unevaluatedProperties: false
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 2/2 ASoC: codecs: rt5631: Enable the optional MCLK

In reply to Romain TISSERAND

When the RT5631 master clock comes from a SoC clock output, nothing in the codec driver keeps it running, and the clock framework turns it off at boot as an unused clock.

Get an optional "mclk" clock in the I2C probe and enable it while the codec is active, from the PREPARE bias level, as rt5616 does. The rate is left to the CPU DAI.

On the GPD XD (RK3288) MCLK is i2s0_clkout, a mux between the I2S clock and a 12 MHz oscillator. Handed to simple-audio-card instead, it is set to the stream rate before the CPU DAI sets the I2S clock, so for 12.288 MHz the mux picks the oscillator and audio plays at the wrong pitch. Held by the codec, it follows the I2S clock.

---
 sound/soc/codecs/rt5631.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)
sound/soc/codecs/rt5631.c +17 -1
index f97fd24e42..d523d6d3a2 100644
@@ -11,6 +11,7 @@
 #include <linux/module.h>
 #include <linux/moduleparam.h>
 #include <linux/init.h>
+#include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/pm.h>
 #include <linux/i2c.h>
@@ -27,6 +28,7 @@
 
 struct rt5631_priv {
 	struct regmap *regmap;
+	struct clk *mclk;
 	int codec_version;
 	int master;
 	int sysclk;
@@ -1536,10 +1538,19 @@ static int rt5631_set_bias_level(struct snd_soc_component *component,
 {
 	struct rt5631_priv *rt5631 = snd_soc_component_get_drvdata(component);
 	struct snd_soc_dapm_context *dapm = snd_soc_component_to_dapm(component);
+	int ret;
 
 	switch (level) {
-	case SND_SOC_BIAS_ON:
 	case SND_SOC_BIAS_PREPARE:
+		if (snd_soc_dapm_get_bias_level(dapm) == SND_SOC_BIAS_ON) {
+			clk_disable_unprepare(rt5631->mclk);
+		} else {
+			ret = clk_prepare_enable(rt5631->mclk);
+			if (ret)
+				return ret;
+		}
+		fallthrough;
+	case SND_SOC_BIAS_ON:
 		snd_soc_component_update_bits(component, RT5631_PWR_MANAG_ADD2,
 			RT5631_PWR_MICBIAS1_VOL | RT5631_PWR_MICBIAS2_VOL,
 			RT5631_PWR_MICBIAS1_VOL | RT5631_PWR_MICBIAS2_VOL);
@@ -1726,6 +1737,11 @@ static int rt5631_i2c_probe(struct i2c_client *i2c)
 	if (IS_ERR(rt5631->regmap))
 		return PTR_ERR(rt5631->regmap);
 
+	rt5631->mclk = devm_clk_get_optional(&i2c->dev, "mclk");
+	if (IS_ERR(rt5631->mclk))
+		return dev_err_probe(&i2c->dev, PTR_ERR(rt5631->mclk),
+				     "failed to get the mclk clock\n");
+
 	ret = devm_snd_soc_register_component(&i2c->dev,
 			&soc_component_dev_rt5631,
 			rt5631_dai, ARRAY_SIZE(rt5631_dai));
Signature · 1 line
2.43.0
maintainer
#3 lore raw
In reply to Romain TISSERAND
Quoted 8 lines · Romain TISSERAND

ASoC: rt5631: Support an MCLK clock from the SoC

The RT5631 driver doesn't take a master clock, so when MCLK comes from a SoC clock output, that clock is turned off at boot as unused. This series adds an optional "mclk" clock to the binding and enables it in the driver while the codec is active, as rt5616 does.

[...]

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-7.5

Thanks!

[1/2] ASoC: dt-bindings: realtek,rt5631: Add the optional MCLK

      https://git.kernel.org/broonie/sound/c/4ff0de1f4a82
[2/2] ASoC: codecs: rt5631: Enable the optional MCLK
      https://git.kernel.org/broonie/sound/c/595aa5f68e6e

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark