mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Stefano Radaelli <stefano.radaelli21@gmail.com>
To: Mark Brown <broonie@kernel.org>,
	Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>,
	Liam Girdwood <lgirdwood@gmail.com>,
	Jaroslav Kysela <perex@perex.cz>, Takashi Iwai <tiwai@suse.com>,
	linux-sound@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: alexander.h@variscite.com, pierluigi.p@variscite.com,
	Stefano Radaelli <stefano.r@variscite.com>
Subject: [PATCH v1] ASoC: simple-card-utils: set CPU sysclk before codec for mclk-fs
Date: Fri,  6 Feb 2026 14:40:14 +0100	[thread overview]
Message-ID: <20260206134014.143057-1-stefano.r@variscite.com> (raw)

From: Stefano Radaelli <stefano.r@variscite.com>

When simple-audio-card uses simple-audio-card,mclk-fs,
simple_util_hw_params() programs sysclk for codec DAIs and CPU DAIs.

Some CPU DAIs may apply the final MCLK rate as part of their .set_sysclk()
callback (e.g. by changing the underlying clock rate via clk_set_rate()).
If codec sysclk is configured before the CPU DAI applies the final
MCLK rate, the codec may configure its internal clocking based on a
non-final MCLK setting, leading to an incorrect clocking state on the
first playback after boot.

This behaviour was observed on i.MX95 systems using fsl_sai with
downstream kernels, which made the issue visible.
However, the problem is generic and can affect any setup where the
CPU DAI applies the final MCLK rate in set_sysclk() while
simple-audio-card uses mclk-fs.

A reproducible symptom is that the first playback runs at the wrong speed
("slow" audio), while subsequent playbacks work correctly.

Example setup:
  - CPU DAI: fsl_sai (i.MX95)
  - Codec: wm8904
  - simple-audio-card,mclk-fs enabled

Reproducer:
  aplay -D hw:wm8904audio /home/Front_Center_8k.wav

Debug evidence:
ftrace showed snd_soc_dai_set_sysclk() called from simple_util_hw_params
for both codec and CPU DAIs, and confirmed wm8904_set_sysclk() ran before
the CPU DAI sysclk call, and the CPU DAI then applied the final
MCLK settings.

Fix this by setting the CPU DAI sysclk before the codec sysclk
when mclk-fs is used, so that the codec always configures its internal
clocking based on the final MCLK rate.

Signed-off-by: Stefano Radaelli <stefano.r@variscite.com>
---
 sound/soc/generic/simple-card-utils.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/sound/soc/generic/simple-card-utils.c b/sound/soc/generic/simple-card-utils.c
index bdc02e85b089..50f60e4996a5 100644
--- a/sound/soc/generic/simple-card-utils.c
+++ b/sound/soc/generic/simple-card-utils.c
@@ -501,15 +501,21 @@ int simple_util_hw_params(struct snd_pcm_substream *substream,
 				goto end;
 		}
 
-		for_each_rtd_codec_dais(rtd, i, sdai) {
-			pdai = simple_props_to_dai_codec(props, i);
+		/*
+		 * When mclk-fs is used, some CPU DAIs may change the
+		 * actual MCLK rate in set_sysclk() (e.g. via clk_set_rate()).
+		 * Configure CPU DAI first so codec clocking/FLL sees
+		 * the final MCLK rate.
+		 */
+		for_each_rtd_cpu_dais(rtd, i, sdai) {
+			pdai = simple_props_to_dai_cpu(props, i);
 			ret = snd_soc_dai_set_sysclk(sdai, 0, mclk, pdai->clk_direction);
 			if (ret && ret != -ENOTSUPP)
 				goto end;
 		}
 
-		for_each_rtd_cpu_dais(rtd, i, sdai) {
-			pdai = simple_props_to_dai_cpu(props, i);
+		for_each_rtd_codec_dais(rtd, i, sdai) {
+			pdai = simple_props_to_dai_codec(props, i);
 			ret = snd_soc_dai_set_sysclk(sdai, 0, mclk, pdai->clk_direction);
 			if (ret && ret != -ENOTSUPP)
 				goto end;

base-commit: b7ff7151e653aa296ab6c5495b2c1ab7c21eb250
-- 
2.47.3


             reply	other threads:[~2026-02-06 13:41 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-06 13:40 Stefano Radaelli [this message]
2026-02-10  0:51 ` Kuninori Morimoto
2026-02-10  9:32   ` Stefano Radaelli

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=20260206134014.143057-1-stefano.r@variscite.com \
    --to=stefano.radaelli21@gmail.com \
    --cc=alexander.h@variscite.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 \
    --cc=perex@perex.cz \
    --cc=pierluigi.p@variscite.com \
    --cc=stefano.r@variscite.com \
    --cc=tiwai@suse.com \
    /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

all inboxes | Powered by JetHome®