From: Charles Keepax <ckeepax@opensource.cirrus.com>
To: Brian Sune <briansune@gmail.com>
Cc: Liam Girdwood <lgirdwood@gmail.com>,
Mark Brown <broonie@kernel.org>,
Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>,
linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3] ASoC: wm8978: add missing BCLK divider setup
Date: Tue, 7 Oct 2025 16:21:15 +0100 [thread overview]
Message-ID: <aOUv69rMkyYFiHae@opensource.cirrus.com> (raw)
In-Reply-To: <20251007145028.1845-1-briansune@gmail.com>
On Tue, Oct 07, 2025 at 10:50:28PM +0800, Brian Sune wrote:
> In previous WM8978 codec driver versions, wm8978_set_dai_clkdiv
> might not have been called for BCLK, leaving the bit clock
> divider unconfigured. This could cause incorrect or unstable audio
> clocks depending on sample rate and word length.
>
> This patch adds a check in wm8978_hw_params: if the BCLK divider
> has not been set via wm8978_set_dai_clkdiv, it is dynamically
> calculated and configured at runtime.
>
> This ensures that BCLK is always correctly set, whether the
> machine driver configures it explicitly or not.
>
> Signed-off-by: Brian Sune <briansune@gmail.com>
> ---
> sound/soc/codecs/wm8978.c | 24 ++++++++++++++++++++++++
> 1 file changed, 24 insertions(+)
>
> diff --git a/sound/soc/codecs/wm8978.c b/sound/soc/codecs/wm8978.c
> index 8c45ba6fc4c3..8dfce6ede8cd 100644
> --- a/sound/soc/codecs/wm8978.c
> +++ b/sound/soc/codecs/wm8978.c
> @@ -99,6 +99,7 @@ struct wm8978_priv {
> unsigned int f_mclk;
> unsigned int f_256fs;
> unsigned int f_opclk;
> + bool bclk_set;
> int mclk_idx;
> enum wm8978_sysclk_src sysclk;
> };
> @@ -590,6 +591,7 @@ static int wm8978_set_dai_clkdiv(struct snd_soc_dai *codec_dai,
> case WM8978_BCLKDIV:
> if (div & ~0x1c)
> return -EINVAL;
> + wm8978->bclk_set = true;
> snd_soc_component_update_bits(component, WM8978_CLOCKING, 0x1c, div);
> break;
> default:
> @@ -717,6 +719,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 +827,23 @@ 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);
>
> + if (!wm8978->bclk_set) {
Yeah that looks good.
> + 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;
> + }
> + }
Apologies but just realised there is still one small problem here.
You want to match the closest BCLK that is over your target rate,
if the BCLK is too slow the system won't work. As your bclk_divs
array is sorted I think you can do something like:
for (i = 0; i < ARRAY_SIZE(bclk_divs); i++) {
bclk = wm8978->f_256fs / bclk_divs[i];
if (bclk < target_bclk)
break;
bclkdiv = i;
}
Thanks,
Charles
next prev parent reply other threads:[~2025-10-07 15:21 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-07 14:50 [PATCH v3] ASoC: wm8978: add missing BCLK divider setup Brian Sune
2025-10-07 15:21 ` Charles Keepax [this message]
2025-10-07 15:55 ` Sune Brian
2025-10-08 12:12 ` Mark Brown
2025-10-08 13:26 ` Sune Brian
2025-10-08 13:40 ` Mark Brown
2025-10-08 13:46 ` Sune Brian
2025-10-08 14:20 ` Mark Brown
2025-10-08 14:37 ` Sune Brian
2025-10-08 17:13 ` Mark Brown
2025-10-08 14:18 ` Sune Brian
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aOUv69rMkyYFiHae@opensource.cirrus.com \
--to=ckeepax@opensource.cirrus.com \
--cc=briansune@gmail.com \
--cc=broonie@kernel.org \
--cc=kuninori.morimoto.gx@renesas.com \
--cc=lgirdwood@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sound@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox