Skip to contentSkip to conversation

wm8904: adapt driver for use with audio-graph-card

PATCH Superseded Michał Mirosław 3 participants 8 messages Jul 22, 2019 – Jul 23, 2019

Subsystems (6)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover wm8904: adapt driver for use with audio-graph-card

This series allows to use WM8904 codec as audio-graph-card component. It is a rewrite of an earlier approach from
https://patchwork.kernel.org/patch/10738097/

Series based on tiwai/sound/for-next tree. You can also pull from:
https://rere.qmqm.pl/git/linux
branch:
wm8904

Example DTS:

/ {
        sound { 
                compatible = "audio-graph-card"; 

                widgets = 
                        "Speaker", "Loudspeaker", 
                        "Headphone", "Headphone Jack", 
                        "Microphone", "Internal Mic", 

                routing = 
                        "Headphone Jack", "HPOUTL", 
                        "Headphone Jack", "HPOUTR", 
                        "Amplifier", "LINEOUTL",
                        "Amplifier", "LINEOUTR",
                        "Loudspeaker", "Amplifier",
                        "IN1L", "MICBIAS",
                        "IN1L", "Internal Mic";

                dais = <&ssc0_port>;
        };
};
&i2c1 {
        status = "okay";

        wm8904: acodec@1a {
                compatible = "wlf,wm8904";
                reg = <0x1a>;
                clocks = <&rtc_32k>;
                clock-names = "mclk";
                interrupts-extended = <&pioA PIN_PB3 IRQ_TYPE_LEVEL_HIGH>;

                #sound-dai-cells = <0>;

                codec_port: port {         
                        codec_ep: endpoint {
                                remote-endpoint = <&ssc0_ep>;
                                dai-format = "left_j";
                                bitclock-master;
                                frame-master;
                                mclk-fs = <256>;
                        };
                };
        };
};
&ssc0 {
        pinctrl-names = "default";
        pinctrl-0 = <&pinctrl_ssc0_default>;
        status = "okay";

        atmel,shared-fs-pin;
        #sound-dai-cells = <0>;

        ssc0_port: port {
                ssc0_ep: endpoint {
                        remote-endpoint = <&codec_ep>;
                };
        };
};

Michał Mirosław (2):
ASoC: wm8904: rework FLL handling
ASoC: wm8904: automatically choose clock source

 .../devicetree/bindings/sound/wm8904.txt      |   4 +
 sound/soc/atmel/atmel_wm8904.c                |  11 +-
 sound/soc/codecs/Kconfig                      |   4 +
 sound/soc/codecs/Makefile                     |   2 +
 sound/soc/codecs/wm8904.c                     | 494 ++++++------------
 sound/soc/codecs/wm8904.h                     |   5 -
 sound/soc/codecs/wm_fll.c                     | 339 ++++++++++++
 sound/soc/codecs/wm_fll.h                     |  26 +
 8 files changed, 531 insertions(+), 354 deletions(-)
 create mode 100644 sound/soc/codecs/wm_fll.c
 create mode 100644 sound/soc/codecs/wm_fll.h
Signature · 6 lines
2.20.1

_______________________________________________
Alsa-devel mailing list
[email protected]
https://mailman.alsa-project.org/mailman/listinfo/alsa-devel
#1 lore raw

PATCH 1/2 ASoC: wm8904: rework FLL handling

Extract and rework FLL handling. This makes it possible to reuse the code for other Wolfson codecs and makes codec adapt SYSCLK to exactly match frequency required for used sampling rate.

---
 .../devicetree/bindings/sound/wm8904.txt      |   4 +
 sound/soc/atmel/atmel_wm8904.c                |  11 +-
 sound/soc/codecs/Kconfig                      |   4 +
 sound/soc/codecs/Makefile                     |   2 +
 sound/soc/codecs/wm8904.c                     | 454 +++++-------------
 sound/soc/codecs/wm8904.h                     |   5 -
 sound/soc/codecs/wm_fll.c                     | 339 +++++++++++++
 sound/soc/codecs/wm_fll.h                     |  26 +
 8 files changed, 492 insertions(+), 353 deletions(-)
Documentation/devicetree/bindings/sound/wm8904.txt +4 -0
index 66bf261423b9..6b51d87f5987 100644
@@ -9,6 +9,10 @@ Required properties:
   - clocks: reference to
     <Documentation/devicetree/bindings/clock/clock-bindings.txt>
 
+Optional properties:
+  - wlf,fll-input: FLL input signal: "mclk", "bclk", "lrclk" or "osc"
+	("mclk" by default)
+
 Pins on the device (for linking into audio routes):
 
   * IN1L
sound/soc/atmel/atmel_wm8904.c +1 -10
index 776b27d3686e..b77ea2495efe 100644
@@ -30,20 +30,11 @@ static int atmel_asoc_wm8904_hw_params(struct snd_pcm_substream *substream,
 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
 	int ret;
 
-	ret = snd_soc_dai_set_pll(codec_dai, WM8904_FLL_MCLK, WM8904_FLL_MCLK,
-		32768, params_rate(params) * 256);
-	if (ret < 0) {
-		pr_err("%s - failed to set wm8904 codec PLL.", __func__);
-		return ret;
-	}
-
 	/*
 	 * As here wm8904 use FLL output as its system clock
-	 * so calling set_sysclk won't care freq parameter
-	 * then we pass 0
 	 */
 	ret = snd_soc_dai_set_sysclk(codec_dai, WM8904_CLK_FLL,
-			0, SND_SOC_CLOCK_IN);
+			params_rate(params) * 256, SND_SOC_CLOCK_IN);
 	if (ret < 0) {
 		pr_err("%s -failed to set wm8904 SYSCLK\n", __func__);
 		return ret;
sound/soc/codecs/Kconfig +4 -0
index 9f89a5346299..15a39faf33ab 100644
@@ -281,6 +281,9 @@ config SND_SOC_ARIZONA
 	default m if SND_SOC_WM8997=m
 	default m if SND_SOC_WM8998=m
 
+config SND_SOC_WM_FLL
+	tristate
+
 config SND_SOC_WM_HUBS
 	tristate
 	default y if SND_SOC_WM8993=y || SND_SOC_WM8994=y
@@ -1325,6 +1328,7 @@ config SND_SOC_WM8903
 config SND_SOC_WM8904
 	tristate "Wolfson Microelectronics WM8904 CODEC"
 	depends on I2C
+	select SND_SOC_WM_FLL
 
 config SND_SOC_WM8940
         tristate
sound/soc/codecs/Makefile +2 -0
index 5b4bb8cf4325..22704fbb7497 100644
@@ -269,6 +269,7 @@ snd-soc-wm9090-objs := wm9090.o
 snd-soc-wm9705-objs := wm9705.o
 snd-soc-wm9712-objs := wm9712.o
 snd-soc-wm9713-objs := wm9713.o
+snd-soc-wm-fll-objs := wm_fll.o
 snd-soc-wm-hubs-objs := wm_hubs.o
 snd-soc-zx-aud96p22-objs := zx_aud96p22.o
 # Amp
@@ -549,6 +550,7 @@ obj-$(CONFIG_SND_SOC_WM9705)	+= snd-soc-wm9705.o
 obj-$(CONFIG_SND_SOC_WM9712)	+= snd-soc-wm9712.o
 obj-$(CONFIG_SND_SOC_WM9713)	+= snd-soc-wm9713.o
 obj-$(CONFIG_SND_SOC_WM_ADSP)	+= snd-soc-wm-adsp.o
+obj-$(CONFIG_SND_SOC_WM_FLL)	+= snd-soc-wm-fll.o
 obj-$(CONFIG_SND_SOC_WM_HUBS)	+= snd-soc-wm-hubs.o
 obj-$(CONFIG_SND_SOC_ZX_AUD96P22) += snd-soc-zx-aud96p22.o
 
sound/soc/codecs/wm8904.c +116 -338

569 lines not on this page · raw message

sound/soc/codecs/wm8904.h +0 -5
index c1bca52f9927..60af09e0bb15 100644
@@ -13,11 +13,6 @@
 #define WM8904_CLK_MCLK 1
 #define WM8904_CLK_FLL  2
 
-#define WM8904_FLL_MCLK          1
-#define WM8904_FLL_BCLK          2
-#define WM8904_FLL_LRCLK         3
-#define WM8904_FLL_FREE_RUNNING  4
-
 /*
  * Register values.
  */
sound/soc/codecs/wm_fll.c new file +339 -0

339 lines not on this page · raw message

sound/soc/codecs/wm_fll.h new file +26 -0
new file mode 100644
index 000000000000..2518a0910179
@@ -0,0 +1,26 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * wm_fll.h  --  FLL support for Wolfson codecs
+ *
+ * Copyright 2019 Michał Mirosław
+ */
+
+#ifndef _WM_FLL_H
+#define _WM_FLL_H
+
+#include <linux/clk.h>
+#include <linux/device.h>
+#include <linux/regmap.h>
+
+struct wm_fll_data
+{
+	struct regmap			*regmap;
+	struct clk			*mclk;
+};
+
+int wm_fll_init(struct wm_fll_data *hw, struct device *dev);
+int wm_fll_enable(struct wm_fll_data *hw);
+int wm_fll_disable(struct wm_fll_data *hw);
+int wm_fll_set_rate(struct wm_fll_data *hw, unsigned long rate);
+
+#endif /* _WM_FLL_H */
Signature · 6 lines
2.20.1

_______________________________________________
Alsa-devel mailing list
[email protected]
https://mailman.alsa-project.org/mailman/listinfo/alsa-devel
#2 lore raw

Re: [PATCH 1/2] ASoC: wm8904: rework FLL handling

Quoted 4 lines · Michał Mirosław

Extract and rework FLL handling. This makes it possible to reuse the code for other Wolfson codecs and makes codec adapt SYSCLK to exactly match frequency required for used sampling rate.

Do you have thoughts on which CODECs you would be including in this? These older parts often have small differences between the configuration that might make this challenging so if you have plans here would be good to have a look from this end.

Quoted 17 lines
---
 .../devicetree/bindings/sound/wm8904.txt      |   4 +
 sound/soc/atmel/atmel_wm8904.c                |  11 +-
 sound/soc/codecs/Kconfig                      |   4 +
 sound/soc/codecs/Makefile                     |   2 +
 sound/soc/codecs/wm8904.c                     | 454 +++++-------------
 sound/soc/codecs/wm8904.h                     |   5 -
 sound/soc/codecs/wm_fll.c                     | 339 +++++++++++++
 sound/soc/codecs/wm_fll.h                     |  26 +
 8 files changed, 492 insertions(+), 353 deletions(-)
Documentation/devicetree/bindings/sound/wm8904.txt +0 -0
index 66bf261423b9..6b51d87f5987 100644

Documentation/devicetree/bindings/sound/wm8904.txt Required properties: · line 15

Quoted 7 lines
Documentation/devicetree/bindings/sound/wm8904.txt +4 -0
@@ -9,6 +9,10 @@ Required properties:
   - clocks: reference to
     <Documentation/devicetree/bindings/clock/clock-bindings.txt>
 
+Optional properties:
+  - wlf,fll-input: FLL input signal: "mclk", "bclk", "lrclk" or "osc"
+	("mclk" by default)
+

Would be keen to see some other peoples thoughts on this one, but this feels a little off to me. Selecting the FLL input is generally something that would be done on a use-case by use-case basis rather than being a property of the hardware that would belong in device tree.

Thanks,
Charles
_______________________________________________
Alsa-devel mailing list
[email protected]
https://mailman.alsa-project.org/mailman/listinfo/alsa-devel

maintainer
#3 lore raw

Re: [PATCH 1/2] ASoC: wm8904: rework FLL handling

Quoted 9 lines · Charles Keepax
Quoted 3 lines · Michał Mirosław

Extract and rework FLL handling. This makes it possible to reuse the code for other Wolfson codecs and makes codec adapt SYSCLK to exactly match frequency required for used sampling rate.

Do you have thoughts on which CODECs you would be including in this? These older parts often have small differences between the configuration that might make this challenging so if you have plans here would be good to have a look from this end.

Right, it's not like it's the same IP being dropped into multiple chips in an identical fashion. There's a lot of high level similarities in the register interfaces but also many small per device tweaks, and it's not clear what benefit we get from refactoring at this point.

#4 lore raw

Re: [PATCH 1/2] ASoC: wm8904: rework FLL handling

Quoted 11 lines · Mark Brown
Quoted 9 lines · Charles Keepax
Quoted 3 lines · Michał Mirosław

Extract and rework FLL handling. This makes it possible to reuse the code for other Wolfson codecs and makes codec adapt SYSCLK to exactly match frequency required for used sampling rate.

Do you have thoughts on which CODECs you would be including in this? These older parts often have small differences between the configuration that might make this challenging so if you have plans here would be good to have a look from this end.

Quoted 4 lines

Right, it's not like it's the same IP being dropped into multiple chips in an identical fashion. There's a lot of high level similarities in the register interfaces but also many small per device tweaks, and it's not clear what benefit we get from refactoring at this point.

This would be mainly code separation, so it's easier to understand and has a potential for direct reuse. I can see that clock selection needs to be changed, but the idea is to have it configurable via device-tree.

I picked at random WM9081. It's FLL implementation looks very similar - major diffferences being in FLL_OUTDIV selection (direct divider vs 2^N) and register block offset.

Another random pick - WM8900. The general FLL idea seems the same, but this one has a bit more complicated register layout, so I wouldn't consider it at first.

WM8994 - it has two FLL's but otherwise has identical register layout for them as WM8904. The only difference is in clock source selection.

Best Regards,
Michał Mirosław

maintainer
#5 lore raw

Re: [PATCH 1/2] ASoC: wm8904: rework FLL handling

Quoted 10 lines · Michał Mirosław
Quoted 4 lines · Mark Brown

Right, it's not like it's the same IP being dropped into multiple chips in an identical fashion. There's a lot of high level similarities in the register interfaces but also many small per device tweaks, and it's not clear what benefit we get from refactoring at this point.

This would be mainly code separation, so it's easier to understand and has a potential for direct reuse. I can see that clock selection needs to be changed, but the idea is to have it configurable via device-tree.

Not all the world is DT...

Quoted 7 lines

I picked at random WM9081. It's FLL implementation looks very similar - major diffferences being in FLL_OUTDIV selection (direct divider vs 2^N) and register block offset.

Another random pick - WM8900. The general FLL idea seems the same, but this one has a bit more complicated register layout, so I wouldn't consider it at first.

Yeah, there's a lot that on the surface looks similar but there's a lot of variations in the detail - different numbers getting plugged in, register layouts getting tweaked, different sets of sources and so on. I get that we can potentially combine the implementations which in theory is code reuse but what is the end goal for that code reuse given that all the refactoring is going to warrant some testing over a bunch of different parts.

#6 lore raw

Re: [PATCH 1/2] ASoC: wm8904: rework FLL handling

In reply to Michał Mirosław
Quoted 25 lines · Michał Mirosław
Quoted 11 lines · Mark Brown
Quoted 9 lines · Charles Keepax
Quoted 3 lines · Michał Mirosław

Extract and rework FLL handling. This makes it possible to reuse the code for other Wolfson codecs and makes codec adapt SYSCLK to exactly match frequency required for used sampling rate.

Do you have thoughts on which CODECs you would be including in this? These older parts often have small differences between the configuration that might make this challenging so if you have plans here would be good to have a look from this end.

Quoted 4 lines

Right, it's not like it's the same IP being dropped into multiple chips in an identical fashion. There's a lot of high level similarities in the register interfaces but also many small per device tweaks, and it's not clear what benefit we get from refactoring at this point.

This would be mainly code separation, so it's easier to understand and has a potential for direct reuse. I can see that clock selection needs to be changed, but the idea is to have it configurable via device-tree.

I picked at random WM9081. It's FLL implementation looks very similar - major diffferences being in FLL_OUTDIV selection (direct divider vs 2^N) and register block offset.

Quoted 7 lines

Another random pick - WM8900. The general FLL idea seems the same, but this one has a bit more complicated register layout, so I wouldn't consider it at first.

WM8994 - it has two FLL's but otherwise has identical register layout for them as WM8904. The only difference is in clock source selection.

The register layouts do look similar but the code controlling the FLLs in these cases is quite different. I am somewhat nervous there are subtle factors at play here which are going to cause problems and its very hard to seperate divergence and actually required sequencing here.

At the very least if you are really sure you want to proceed in this direction I think we should look at splitting the patch into two parts one that factors out the functionality and a separate patch that adds any new functionality. It makes things much easier to review that way.

Thanks,
Charles
_______________________________________________
Alsa-devel mailing list
[email protected]
https://mailman.alsa-project.org/mailman/listinfo/alsa-devel

#7 lore raw

PATCH 2/2 ASoC: wm8904: automatically choose clock source

In reply to Michał Mirosław

Choose clock source automatically if not provided. This will be the case with eg. audio-graph-card.

---
 sound/soc/codecs/wm8904.c | 42 +++++++++++++++++++++++++++++++++++++--
 1 file changed, 40 insertions(+), 2 deletions(-)
sound/soc/codecs/wm8904.c +40 -2
index 033a38e408b5..2d8cb61a2d97 100644
@@ -367,15 +367,34 @@ static int wm8904_enable_sysclk(struct wm8904_priv *priv)
 	return err;
 }
 
+static int wm8904_bump_fll_sysclk(unsigned int *rate);
+
 static int wm8904_set_sysclk(struct snd_soc_dai *dai, int clk_id,
 			     unsigned int rate, int dir)
 {
 	struct snd_soc_component *component = dai->component;
 	struct wm8904_priv *wm8904 = snd_soc_component_get_drvdata(component);
 	unsigned int clock0, clock2;
-	int err;
+	int err, do_div = false;
 
 	switch (clk_id) {
+	case 0:
+		if (rate == clk_round_rate(wm8904->mclk, rate)) {
+			clk_id = WM8904_CLK_MCLK;
+		} else if (rate * 2 == clk_round_rate(wm8904->mclk, rate * 2)) {
+			rate *= 2;
+			clk_id = WM8904_CLK_MCLK;
+			do_div = true;
+		} else {
+			clk_id = WM8904_CLK_FLL;
+			err = wm8904_bump_fll_sysclk(&rate);
+			if (err) {
+				dev_dbg(component->dev, "Can't match %u over FLL 1406250 Hz minimum\n", rate);
+				return err;
+			}
+		}
+		break;
+
 	case WM8904_CLK_MCLK:
 	case WM8904_CLK_FLL:
 		break;
@@ -419,7 +438,9 @@ static int wm8904_set_sysclk(struct snd_soc_dai *dai, int clk_id,
 	}
 
 	/* SYSCLK shouldn't be over 13.5MHz */
-	if (rate > 13500000) {
+	if (rate > 13500000)
+		do_div = true;
+	if (do_div) {
 		clock0 = WM8904_MCLK_DIV;
 		wm8904->sysclk_rate = rate / 2;
 	} else {
@@ -1334,6 +1355,23 @@ static struct {
 	{ 480, 20 },
 };
 
+static int wm8904_bump_fll_sysclk(unsigned int *rate)
+{
+	int i;
+
+	/* bump SYSCLK rate if below minimal FLL output */
+
+	for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
+		if (*rate * bclk_divs[i].div >= 1406250 * 10)
+			break;
+	}
+
+	if (i == ARRAY_SIZE(bclk_divs))
+		return -ERANGE;
+
+	*rate = (*rate * bclk_divs[i].div) / 10;
+	return 0;
+}
 
 static int wm8904_hw_params(struct snd_pcm_substream *substream,
 			    struct snd_pcm_hw_params *params,
Signature · 6 lines
2.20.1

_______________________________________________
Alsa-devel mailing list
[email protected]
https://mailman.alsa-project.org/mailman/listinfo/alsa-devel