Skip to contentSkip to conversation

ASoC: nau8810: add MCLK support

PATCH In review Mehmet Fide 2 participants 12 messages Dec 3

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

cover ASoC: nau8810: add MCLK support

From: Mehmet Fide <[email protected]>

The NAU8810 needs an internal master clock of 256 * fs, which the driver can only divide from MCLK. With 25 MHz from the AM62 AUDIO_EXT_REFCLK1, 48 kHz and 44.1 kHz both play at 48.8 kHz.

This series lets the driver take MCLK and make the system clock with its PLL, along the lines of the nau8822 changes, with two differences. PLL_EN stays with the PLL supply widget, so the problem fixed for nau8822 by commit 390cdd61a947 ("ASoC: nau8822: reprogram PLL after it has been disabled") cannot occur. And the PLL is not switched off while it is programmed, as in commit aeca8a329502 ("ASoC: nau8822: Add operation for internal PLL off and on"): the NAU8810 datasheet does not ask for it.

Tested on a Verdin AM62 with simple-audio-card and mclk-fs = <256>, on 6.12.120 with the series backported, also with lockdep: playback from 8 to 48 kHz and capture at 8, 44.1 and 48 kHz run within 0.2 % of the rate, and a board without mclk-fs keeps the old behaviour.

Mehmet Fide (7):
ASoC: dt-bindings: nau8810: Add MCLK clock
ASoC: nau8810: move nau8810_set_sysclk()
ASoC: nau8810: set NAU8810_REFIMP_80K only once
ASoC: nau8810: Lower debug print priority
ASoC: nau8810: Do not configure the PLL if freq_out is zero
ASoC: nau8810: Don't reconfigure PLL to the same values
ASoC: nau8810: add MCLK support

 .../bindings/sound/nuvoton,nau8810.yaml       | 51 +++++++++++
 .../bindings/sound/trivial-codec.yaml         |  3 -
 sound/soc/codecs/nau8810.c                    | 87 +++++++++++++++----
 sound/soc/codecs/nau8810.h                    |  3 +
 4 files changed, 126 insertions(+), 18 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/sound/nuvoton,nau8810.yaml

base-commit: b5b604242c1cc418d202d29d73fe8ec2db833a03

Signature · 1 line
2.56.0
#1 lore raw

PATCH 1/7 ASoC: dt-bindings: nau8810: Add MCLK clock

From: Mehmet Fide <[email protected]>

Add the master clock input. trivial-codec.yaml has no clocks, so move the nau8810, nau8812 and nau8814 compatibles into a schema of their own.

---
 .../bindings/sound/nuvoton,nau8810.yaml       | 51 +++++++++++++++++++
 .../bindings/sound/trivial-codec.yaml         |  3 --
 2 files changed, 51 insertions(+), 3 deletions(-)
 create mode 100644 Documentation/devicetree/bindings/sound/nuvoton,nau8810.yaml
Documentation/devicetree/bindings/sound/nuvoton,nau8810.yaml new file +51 -0
new file mode 100644
index 000000000000..285dc08ab289
@@ -0,0 +1,51 @@
+# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/sound/nuvoton,nau8810.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: NAU8810/NAU8812/NAU8814 audio CODEC
+
+maintainers:
+  - David Lin <[email protected]>
+
+allOf:
+  - $ref: dai-common.yaml#
+
+properties:
+  compatible:
+    enum:
+      - nuvoton,nau8810
+      - nuvoton,nau8812
+      - nuvoton,nau8814
+
+  reg:
+    maxItems: 1
+
+  '#sound-dai-cells':
+    const: 0
+
+  clocks:
+    maxItems: 1
+
+  clock-names:
+    const: mclk
+
+required:
+  - compatible
+  - reg
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        codec@1a {
+            #sound-dai-cells = <0>;
+            compatible = "nuvoton,nau8810";
+            reg = <0x1a>;
+        };
+    };
Documentation/devicetree/bindings/sound/trivial-codec.yaml +0 -3
index 9a35dfb17349..9fd2802bf391 100644
@@ -27,9 +27,6 @@ properties:
       - dlg,da9055-codec
       # Nuvoton Technology Corporation NAU85L40 Audio CODEC
       - nuvoton,nau8540
-      - nuvoton,nau8810
-      - nuvoton,nau8812
-      - nuvoton,nau8814
       # NXP TFA9879 class-D audio amplifier
       - nxp,tfa9879
       - nxp,uda1342
Signature · 1 line
2.56.0
#2 lore raw

Re: [PATCH 1/7] ASoC: dt-bindings: nau8810: Add MCLK clock

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] dt-bindings: sound: nau8810 example lists #sound-dai-cells before compatible --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] dt-bindings: sound: nau8810 example lists #sound-dai-cells before compatible
--

commit bdcd7057136ec88d8022ec8581545b606b3ea3ac
Author: Mehmet Fide <[email protected]>

ASoC: dt-bindings: nau8810: Add MCLK clock

This commit moves the nuvoton,nau8810, nuvoton,nau8812, and nuvoton,nau8814 compatibles out of trivial-codec.yaml into a dedicated nuvoton,nau8810.yaml binding and adds the master clock properties.

Quoted 5 lines
Documentation/devicetree/bindings/sound/nuvoton,nau8810.yaml new file +0 -0
new file mode 100644
index 0000000000000..285dc08ab2891

[ ... ]

Documentation/devicetree/bindings/sound/nuvoton,nau8810.yaml line 51

Quoted 12 lines
Documentation/devicetree/bindings/sound/nuvoton,nau8810.yaml new file +12 -0
+examples:
+  - |
+    i2c {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        codec@1a {
+            #sound-dai-cells = <0>;
+            compatible = "nuvoton,nau8810";
+            reg = <0x1a>;
+        };
+    };

[Severity: Low]
Should compatible and reg come before #sound-dai-cells in this example node? Documentation/devicetree/bindings/dts-coding-style.rst specifies listing compatible and reg before standard properties like #sound-dai-cells, which also matches the order used in this schema's properties section.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

Re: [PATCH 1/7] ASoC: dt-bindings: nau8810: Add MCLK clock

Quoted 2 lines

Should compatible and reg come before #sound-dai-cells in this example node?

Yes, v2 lists compatible and reg first.

Mehmet

#4 lore raw

PATCH 2/7 ASoC: nau8810: move nau8810_set_sysclk()

In reply to Mehmet Fide

From: Mehmet Fide <[email protected]>

The next change calls nau8810_set_pll() from nau8810_set_sysclk(). Move nau8810_set_sysclk() below nau8810_set_pll() to avoid a forward declaration.

---
 sound/soc/codecs/nau8810.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
sound/soc/codecs/nau8810.c +14 -14
index e18439c2b0e1..3ebef741f07a 100644
@@ -512,20 +512,6 @@ static const struct snd_soc_dapm_route nau8810_dapm_routes[] = {
 	{"DAC", NULL, "Digital Loopback"},
 };
 
-static int nau8810_set_sysclk(struct snd_soc_dai *dai,
-				 int clk_id, unsigned int freq, int dir)
-{
-	struct snd_soc_component *component = dai->component;
-	struct nau8810 *nau8810 = snd_soc_component_get_drvdata(component);
-
-	nau8810->clk_id = clk_id;
-	nau8810->sysclk = freq;
-	dev_dbg(nau8810->dev, "master sysclk %dHz, source %s\n",
-		freq, clk_id == NAU8810_SCLK_PLL ? "PLL" : "MCLK");
-
-	return 0;
-}
-
 static int nau8810_calc_pll(unsigned int pll_in,
 	unsigned int fs, struct nau8810_pll *pll_param)
 {
@@ -605,6 +591,20 @@ static int nau8810_set_pll(struct snd_soc_dai *codec_dai, int pll_id,
 	return 0;
 }
 
+static int nau8810_set_sysclk(struct snd_soc_dai *dai,
+				 int clk_id, unsigned int freq, int dir)
+{
+	struct snd_soc_component *component = dai->component;
+	struct nau8810 *nau8810 = snd_soc_component_get_drvdata(component);
+
+	nau8810->clk_id = clk_id;
+	nau8810->sysclk = freq;
+	dev_dbg(nau8810->dev, "master sysclk %dHz, source %s\n",
+		freq, clk_id == NAU8810_SCLK_PLL ? "PLL" : "MCLK");
+
+	return 0;
+}
+
 static int nau8810_set_dai_fmt(struct snd_soc_dai *codec_dai,
 		unsigned int fmt)
 {
Signature · 1 line
2.56.0
#5 lore raw

PATCH 3/7 ASoC: nau8810: set NAU8810_REFIMP_80K only once

In reply to Mehmet Fide

From: Mehmet Fide <[email protected]>

The bias level goes from STANDBY to PREPARE and then to ON before a stream, and both PREPARE and ON set NAU8810_REFIMP_80K. Set it only in PREPARE.

---
 sound/soc/codecs/nau8810.c | 2 ++
 1 file changed, 2 insertions(+)
sound/soc/codecs/nau8810.c +2 -0
index 3ebef741f07a..de268eb68e53 100644
@@ -781,6 +781,8 @@ static int nau8810_set_bias_level(struct snd_soc_component *component,
 
 	switch (level) {
 	case SND_SOC_BIAS_ON:
+		break;
+
 	case SND_SOC_BIAS_PREPARE:
 		regmap_update_bits(map, NAU8810_REG_POWER1,
 			NAU8810_REFIMP_MASK, NAU8810_REFIMP_80K);
Signature · 1 line
2.56.0
#6 lore raw

PATCH 4/7 ASoC: nau8810: Lower debug print priority

In reply to Mehmet Fide

From: Mehmet Fide <[email protected]>

The PLL parameters are of no interest to the general user. The message is for debugging, print it with dev_dbg().

---
 sound/soc/codecs/nau8810.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/soc/codecs/nau8810.c +1 -1
index de268eb68e53..b5b18994ebaa 100644
@@ -567,7 +567,7 @@ static int nau8810_set_pll(struct snd_soc_dai *codec_dai, int pll_id,
 		dev_err(nau8810->dev, "Unsupported input clock %d\n", freq_in);
 		return ret;
 	}
-	dev_info(nau8810->dev, "pll_int=%x pll_frac=%x mclk_scaler=%x pre_factor=%x\n",
+	dev_dbg(nau8810->dev, "pll_int=%x pll_frac=%x mclk_scaler=%x pre_factor=%x\n",
 		pll_param->pll_int, pll_param->pll_frac, pll_param->mclk_scaler,
 		pll_param->pre_factor);
 
Signature · 1 line
2.56.0
#7 lore raw

PATCH 5/7 ASoC: nau8810: Do not configure the PLL if freq_out is zero

In reply to Mehmet Fide

From: Mehmet Fide <[email protected]>

When a stream ends, the PLL or, with a later change, the system clock may be set to zero. nau8810_set_pll() then fails with "Unsupported input clock". Return early, as nau8822 does, but leave the PLL power to its supply widget, which already powers the PLL down.

---
 sound/soc/codecs/nau8810.c | 5 +++++
 1 file changed, 5 insertions(+)
sound/soc/codecs/nau8810.c +5 -0
index b5b18994ebaa..1a309d5c7454 100644
@@ -561,6 +561,11 @@ static int nau8810_set_pll(struct snd_soc_dai *codec_dai, int pll_id,
 	struct nau8810_pll *pll_param = &nau8810->pll;
 	int ret, fs;
 
+	if (freq_out == 0) {
+		dev_dbg(nau8810->dev, "PLL disabled\n");
+		return 0;
+	}
+
 	fs = freq_out / 256;
 	ret = nau8810_calc_pll(freq_in, fs, pll_param);
 	if (ret < 0) {
Signature · 1 line
2.56.0
#8 lore raw

PATCH 6/7 ASoC: nau8810: Don't reconfigure PLL to the same values

In reply to Mehmet Fide

From: Mehmet Fide <[email protected]>

Record the input and output frequency of the PLL and skip programming it again when asked for the same values, as nau8822 does. As the PLL is then not programmed on every stream, hw_params() selects the PLL and its prescaler itself.

---
 sound/soc/codecs/nau8810.c | 14 ++++++++++++++
 sound/soc/codecs/nau8810.h |  2 ++
 2 files changed, 16 insertions(+)
sound/soc/codecs/nau8810.c +14 -0
index 1a309d5c7454..a27fa63a2887 100644
@@ -561,6 +561,10 @@ static int nau8810_set_pll(struct snd_soc_dai *codec_dai, int pll_id,
 	struct nau8810_pll *pll_param = &nau8810->pll;
 	int ret, fs;
 
+	if (freq_in == pll_param->freq_in &&
+	    freq_out == pll_param->freq_out)
+		return 0;
+
 	if (freq_out == 0) {
 		dev_dbg(nau8810->dev, "PLL disabled\n");
 		return 0;
@@ -593,6 +597,9 @@ static int nau8810_set_pll(struct snd_soc_dai *codec_dai, int pll_id,
 	regmap_update_bits(map, NAU8810_REG_CLOCK,
 		NAU8810_CLKM_MASK, NAU8810_CLKM_PLL);
 
+	pll_param->freq_in = freq_in;
+	pll_param->freq_out = freq_out;
+
 	return 0;
 }
 
@@ -772,6 +779,13 @@ static int nau8810_pcm_hw_params(struct snd_pcm_substream *substream,
 		ret = nau8810_mclk_clkdiv(nau8810, params_rate(params));
 		if (ret < 0)
 			dev_err(nau8810->dev, "MCLK div configuration fail\n");
+	} else {
+		/* master clock from the PLL, through its prescaler */
+		int sel = nau8810->pll.mclk_scaler << NAU8810_MCLKSEL_SFT;
+
+		regmap_update_bits(nau8810->regmap, NAU8810_REG_CLOCK,
+				   NAU8810_MCLKSEL_MASK | NAU8810_CLKM_MASK,
+				   sel | NAU8810_CLKM_PLL);
 	}
 
 	return ret;
sound/soc/codecs/nau8810.h +2 -0
index 6a7cacbe044a..33cf0d599c17 100644
@@ -273,6 +273,8 @@ struct nau8810_pll {
 	int mclk_scaler;
 	int pll_frac;
 	int pll_int;
+	int freq_in;
+	int freq_out;
 };
 
 struct nau8810 {
Signature · 1 line
2.56.0
#9 lore raw

PATCH 7/7 ASoC: nau8810: add MCLK support

In reply to Mehmet Fide

From: Mehmet Fide <[email protected]>

The codec needs an internal clock of 256 * fs, which the prescalers can only divide from MCLK. From 25 MHz, 48 kHz and 44.1 kHz both play at 48.8 kHz.

Handle MCLK in the driver as nau8822 does: when the system clock asked for with mclk-fs differs from MCLK, make it with the PLL. MCLK is enabled while the codec is out of standby.

---
 sound/soc/codecs/nau8810.c | 38 +++++++++++++++++++++++++++++++++++++-
 sound/soc/codecs/nau8810.h |  1 +
 2 files changed, 38 insertions(+), 1 deletion(-)
sound/soc/codecs/nau8810.c +37 -1
index a27fa63a2887..311dfa8eea74 100644
@@ -13,6 +13,7 @@
 #include <linux/moduleparam.h>
 #include <linux/kernel.h>
 #include <linux/init.h>
+#include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/pm.h>
 #include <linux/i2c.h>
@@ -608,11 +609,26 @@ static int nau8810_set_sysclk(struct snd_soc_dai *dai,
 {
 	struct snd_soc_component *component = dai->component;
 	struct nau8810 *nau8810 = snd_soc_component_get_drvdata(component);
+	unsigned long mclk_freq;
 
 	nau8810->clk_id = clk_id;
 	nau8810->sysclk = freq;
+
+	if (nau8810->mclk) {
+		mclk_freq = clk_get_rate(nau8810->mclk);
+		if (mclk_freq != freq) {
+			int ret = nau8810_set_pll(dai, NAU8810_SCLK_MCLK,
+				NAU8810_SCLK_MCLK, mclk_freq, freq);
+			if (ret) {
+				dev_err(component->dev, "Failed to set PLL\n");
+				return ret;
+			}
+			nau8810->clk_id = NAU8810_SCLK_PLL;
+		}
+	}
+
 	dev_dbg(nau8810->dev, "master sysclk %dHz, source %s\n",
-		freq, clk_id == NAU8810_SCLK_PLL ? "PLL" : "MCLK");
+		freq, nau8810->clk_id == NAU8810_SCLK_PLL ? "PLL" : "MCLK");
 
 	return 0;
 }
@@ -803,11 +819,26 @@ static int nau8810_set_bias_level(struct snd_soc_component *component,
 		break;
 
 	case SND_SOC_BIAS_PREPARE:
+		if (nau8810->mclk &&
+		    snd_soc_dapm_get_bias_level(dapm) != SND_SOC_BIAS_ON) {
+			int ret = clk_prepare_enable(nau8810->mclk);
+
+			if (ret) {
+				dev_err(nau8810->dev,
+					"Failed to enable MCLK: %d\n", ret);
+				return ret;
+			}
+		}
+
 		regmap_update_bits(map, NAU8810_REG_POWER1,
 			NAU8810_REFIMP_MASK, NAU8810_REFIMP_80K);
 		break;
 
 	case SND_SOC_BIAS_STANDBY:
+		if (nau8810->mclk &&
+		    snd_soc_dapm_get_bias_level(dapm) != SND_SOC_BIAS_OFF)
+			clk_disable_unprepare(nau8810->mclk);
+
 		regmap_update_bits(map, NAU8810_REG_POWER1,
 			NAU8810_IOBUF_EN | NAU8810_ABIAS_EN,
 			NAU8810_IOBUF_EN | NAU8810_ABIAS_EN);
@@ -922,6 +953,11 @@ static int nau8810_i2c_probe(struct i2c_client *i2c)
 		return PTR_ERR(nau8810->regmap);
 	nau8810->dev = dev;
 
+	nau8810->mclk = devm_clk_get_optional(dev, "mclk");
+	if (IS_ERR(nau8810->mclk))
+		return dev_err_probe(dev, PTR_ERR(nau8810->mclk),
+				     "Error getting MCLK\n");
+
 	regmap_write(nau8810->regmap, NAU8810_REG_RESET, 0x00);
 
 	return devm_snd_soc_register_component(dev,
sound/soc/codecs/nau8810.h +1 -0
index 33cf0d599c17..c0f81402f293 100644
@@ -280,6 +280,7 @@ struct nau8810_pll {
 struct nau8810 {
 	struct device *dev;
 	struct regmap *regmap;
+	struct clk *mclk;
 	struct nau8810_pll pll;
 	int sysclk;
 	int clk_id;
Signature · 1 line
2.56.0
#10 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] ASoC: nau8810: MCLK-to-PLL switch leaves PLL off during pmdown_time - [Medium] ASoC: nau8810…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] ASoC: nau8810: MCLK-to-PLL switch leaves PLL off during pmdown_time
- [Medium] ASoC: nau8810: nau8810_set_pll() reprograms PLL while PLL_EN is active
--

commit 6d8fbbb4bed0d01ffd7425872f563b6b8efcc0d3
Author: Mehmet Fide <[email protected]>

ASoC: nau8810: add MCLK support

This commit adds optional MCLK clock handling to the nau8810 driver, enabling MCLK when the codec leaves standby and configuring the PLL in nau8810_set_sysclk() whenever the requested sysclk differs from the MCLK rate.

Quoted 4 lines
sound/soc/codecs/nau8810.c +0 -0
index a27fa63a2887e..311dfa8eea74d 100644

[ ... ]

Quoted 18 lines
sound/soc/codecs/nau8810.c +11 -0
@@ -608,11 +609,26 @@ static int nau8810_set_sysclk(struct snd_soc_dai *dai,
 {
 	struct snd_soc_component *component = dai->component;
 	struct nau8810 *nau8810 = snd_soc_component_get_drvdata(component);
+	unsigned long mclk_freq;
 
 	nau8810->clk_id = clk_id;
 	nau8810->sysclk = freq;
+
+	if (nau8810->mclk) {
+		mclk_freq = clk_get_rate(nau8810->mclk);
+		if (mclk_freq != freq) {
+			int ret = nau8810_set_pll(dai, NAU8810_SCLK_MCLK,
+				NAU8810_SCLK_MCLK, mclk_freq, freq);
+			if (ret) {
+				dev_err(component->dev, "Failed to set PLL\n");
+				return ret;
+			}

sound/soc/codecs/nau8810.c static int nau8810_set_sysclk(struct snd_soc_dai *dai, · line 634

Quoted 10 lines
sound/soc/codecs/nau8810.c +5 -1
+			nau8810->clk_id = NAU8810_SCLK_PLL;
+		}
+	}
+
 	dev_dbg(nau8810->dev, "master sysclk %dHz, source %s\n",
-		freq, clk_id == NAU8810_SCLK_PLL ? "PLL" : "MCLK");
+		freq, nau8810->clk_id == NAU8810_SCLK_PLL ? "PLL" : "MCLK");
 
 	return 0;
 }

[Severity: High]
Can switching from an MCLK-matched rate to a PLL-derived rate leave the PLL powered off during pmdown_time?

Unlike nau8822_set_pll(), nau8810_set_pll() selects NAU8810_CLKM_PLL in NAU8810_REG_CLOCK without setting NAU8810_PLL_EN_SFT in NAU8810_REG_POWER1. Instead, NAU8810_PLL_EN_SFT is only driven by the "PLL" DAPM supply widget, gated on check_mclk_select_pll().

Because nau8810_component_driver sets .use_pmdown_time = 1, closing an MCLK-matched stream leaves Playback and DAC powered on with PLL off. If a PLL-rate stream starts within pmdown_time, dapm_power_one_widget() sees Playback already active and skips check_mclk_select_pll().

The codec master clock is then routed from a powered-off PLL, breaking playback with silent output or PCM write timeouts when the codec is clock provider. In the reverse direction (PLL to MCLK), the PLL remains powered on until pmdown_time expires.

[Severity: Medium]
Can nau8810_set_pll() reprogram the PLL registers while NAU8810_PLL_EN_SFT is still active?

When back-to-back streams run at different PLL-derived rates within pmdown_time (such as 48 kHz then 44.1 kHz from a 25 MHz MCLK), the DAPM PLL widget keeps NAU8810_PLL_EN_SFT set. nau8810_set_pll() then writes NAU8810_REG_PLLN and NAU8810_REG_PLLK1..3 while the PLL is enabled.

On the sibling NAU8822 with the same fractional-N PLL registers, commit aeca8a329502 ("ASoC: nau8822: Add operation for internal PLL off and on") found that the PLL fails to lock unless PLL_EN is cleared before programming the fractional parameters and re-enabled afterward.

Should nau8810_set_pll() clear NAU8810_PLL_EN_SFT before updating the PLL registers and restore it afterward if it was enabled?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#11 lore raw
Quoted 2 lines

Can switching from an MCLK-matched rate to a PLL-derived rate leave the PLL powered off during pmdown_time?

Yes. v2 handles PLL_EN as nau8822 does: the PLL is turned off and its frequencies are cleared when the system clock is set to 0 at the end of a stream, as in commit 390cdd61a947, and nau8810_set_pll() turns it on after programming it.

Quoted 2 lines

Should nau8810_set_pll() clear NAU8810_PLL_EN_SFT before updating the PLL registers and restore it afterward if it was enabled?

v2 turns the PLL off while the registers are written and on afterwards, as nau8822 does since commit aeca8a329502.

Mehmet