public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] sound/soc/codecs/wm8978: add missing BCLK divider setup
@ 2025-10-03  9:13 Brian Sune
  2025-10-07 10:30 ` Charles Keepax
  2025-10-07 11:33 ` [PATCH v2] ASoC: wm8978: " Brian Sune
  0 siblings, 2 replies; 22+ messages in thread
From: Brian Sune @ 2025-10-03  9:13 UTC (permalink / raw)
  To: Liam Girdwood, Mark Brown, Kuninori Morimoto; +Cc: linux-sound, linux-kernel

The original WM8978 codec driver did not set the BCLK (bit clock)
divider, which can cause audio clocks to be incorrect or unstable
depending on the sample rate and word length.

This patch adds proper calculation and configuration of the BCLK
divider based on the sample rate and word width, ensuring the
codec generates the correct bit clock for all supported rates.

Signed-off-by: Brian Sune <briansune@gmail.com>
---
 sound/soc/codecs/wm8978.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/sound/soc/codecs/wm8978.c b/sound/soc/codecs/wm8978.c
index 8c45ba6fc4c3..2109c84f33df 100644
--- a/sound/soc/codecs/wm8978.c
+++ b/sound/soc/codecs/wm8978.c
@@ -717,6 +717,11 @@ static int wm8978_hw_params(struct snd_pcm_substream *substream,
 			    struct snd_pcm_hw_params *params,
 			    struct snd_soc_dai *dai)
 {
+	unsigned int bclk, bclkdiv = 0, min_diff = UINT_MAX;
+	unsigned int target_bclk = params_rate(params) * params_width(params) * 2;
+	/* WM8978 supports divisors */
+	static const int bclk_divs[] = {1, 2, 4, 8, 16, 32};
+
 	struct snd_soc_component *component = dai->component;
 	struct wm8978_priv *wm8978 = snd_soc_component_get_drvdata(component);
 	/* Word length mask = 0x60 */
@@ -820,6 +825,21 @@ static int wm8978_hw_params(struct snd_pcm_substream *substream,
 	/* MCLK divisor mask = 0xe0 */
 	snd_soc_component_update_bits(component, WM8978_CLOCKING, 0xe0, best << 5);
 
+	for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
+		bclk = wm8978->f_256fs / bclk_divs[i];
+		if (abs(bclk - target_bclk) < min_diff) {
+			min_diff = abs(bclk - target_bclk);
+			bclkdiv = i;
+		}
+	}
+
+	dev_dbg(component->dev, "%s: fs=%u width=%u -> target BCLK=%u, using div #%u\n",
+		__func__, params_rate(params), params_width(params), target_bclk,
+		bclk_divs[bclkdiv]);
+
+	/* BCLKDIV divisor mask = 0x1c */
+	snd_soc_component_update_bits(component, WM8978_CLOCKING, 0x1c, bclkdiv << 2);
+
 	snd_soc_component_write(component, WM8978_AUDIO_INTERFACE, iface_ctl);
 	snd_soc_component_write(component, WM8978_ADDITIONAL_CONTROL, add_ctl);
 
-- 
2.47.1.windows.1


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

end of thread, other threads:[~2025-10-07 13:54 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-03  9:13 [PATCH] sound/soc/codecs/wm8978: add missing BCLK divider setup Brian Sune
2025-10-07 10:30 ` Charles Keepax
2025-10-07 11:22   ` Sune Brian
2025-10-07 12:11     ` Charles Keepax
2025-10-07 12:18       ` Mark Brown
2025-10-07 12:26         ` Charles Keepax
2025-10-07 12:30           ` Mark Brown
2025-10-07 12:37             ` Sune Brian
2025-10-07 12:48       ` Sune Brian
2025-10-07 13:15         ` Charles Keepax
2025-10-07 13:39           ` Sune Brian
2025-10-07 13:48             ` Mark Brown
2025-10-07 13:54               ` Sune Brian
2025-10-07 11:33 ` [PATCH v2] ASoC: wm8978: " Brian Sune
2025-10-07 11:44   ` Mark Brown
2025-10-07 11:48     ` Sune Brian
2025-10-07 11:52       ` Mark Brown
2025-10-07 11:56         ` Sune Brian
2025-10-07 12:10           ` Mark Brown
2025-10-07 13:03             ` Sune Brian
2025-10-07 13:15               ` Mark Brown
2025-10-07 13:48                 ` Sune Brian

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