Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH 0/2] ASoC: ti: davinci-mcasp: McASP code cleanup and clk div fixes
@ 2026-03-05 19:58 Sen Wang
  2026-03-05 19:58 ` [PATCH 1/2] ASoC: ti: davinci-mcasp: extract mcasp_is_auxclk_enabled() helper Sen Wang
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Sen Wang @ 2026-03-05 19:58 UTC (permalink / raw)
  To: peter.ujfalusi, broonie, lgirdwood, perex, tiwai, linux-sound,
	linux-kernel
  Cc: devarsht, r-donadkar, s-jain1, m-shah, Sen Wang

Just two minor patches that aim to tidy up the code a little bit,
as well as fix the aux_div selection in davinci_mcasp_calc_clk_div()
for mid-range dividers (33 <= div <= 4096).

Sen Wang (2):
  ASoC: ti: davinci-mcasp: extract mcasp_is_auxclk_enabled() helper
  ASoC: ti: davinci-mcasp: improve aux_div selection for mid-range dividers

 sound/soc/ti/davinci-mcasp.c | 45 +++++++++++++++++++++++++-----------
 1 file changed, 31 insertions(+), 14 deletions(-)

-- 
2.43.0


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

* [PATCH 1/2] ASoC: ti: davinci-mcasp: extract mcasp_is_auxclk_enabled() helper
  2026-03-05 19:58 [PATCH 0/2] ASoC: ti: davinci-mcasp: McASP code cleanup and clk div fixes Sen Wang
@ 2026-03-05 19:58 ` Sen Wang
  2026-03-05 19:58 ` [PATCH 2/2] ASoC: ti: davinci-mcasp: improve aux_div selection for mid-range dividers Sen Wang
  2026-03-10 15:53 ` [PATCH 0/2] ASoC: ti: davinci-mcasp: McASP code cleanup and clk div fixes Mark Brown
  2 siblings, 0 replies; 4+ messages in thread
From: Sen Wang @ 2026-03-05 19:58 UTC (permalink / raw)
  To: peter.ujfalusi, broonie, lgirdwood, perex, tiwai, linux-sound,
	linux-kernel
  Cc: devarsht, r-donadkar, s-jain1, m-shah, Sen Wang

Move the AUXCLK-enabled check out of davinci_mcasp_calc_clk_div() into
a reusable helper.  No functional change.

Signed-off-by: Sen Wang <sen@ti.com>
---
 sound/soc/ti/davinci-mcasp.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/sound/soc/ti/davinci-mcasp.c b/sound/soc/ti/davinci-mcasp.c
index 2d260fbc9b83..6d0310f09b12 100644
--- a/sound/soc/ti/davinci-mcasp.c
+++ b/sound/soc/ti/davinci-mcasp.c
@@ -274,6 +274,14 @@ static inline unsigned int mcasp_get_auxclk_fs_ratio(struct davinci_mcasp *mcasp
 	       mcasp->auxclk_fs_ratio_tx : mcasp->auxclk_fs_ratio_rx;
 }
 
+static inline bool mcasp_is_auxclk_enabled(struct davinci_mcasp *mcasp, int stream)
+{
+	if (mcasp->async_mode && stream == SNDRV_PCM_STREAM_CAPTURE)
+		return mcasp_get_reg(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG) & AHCLKRE;
+
+	return mcasp_get_reg(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG) & AHCLKXE;
+}
+
 static void mcasp_start_rx(struct davinci_mcasp *mcasp)
 {
 	if (mcasp->rxnumevt) {	/* enable FIFO */
@@ -1337,16 +1345,15 @@ static int davinci_mcasp_calc_clk_div(struct davinci_mcasp *mcasp,
 	int bclk_div_id, auxclk_div_id;
 	bool auxclk_enabled;
 
+	auxclk_enabled = mcasp_is_auxclk_enabled(mcasp, stream);
+
 	if (mcasp->async_mode && stream == SNDRV_PCM_STREAM_CAPTURE) {
-		auxclk_enabled = mcasp_get_reg(mcasp, DAVINCI_MCASP_AHCLKRCTL_REG) & AHCLKRE;
 		bclk_div_id = MCASP_CLKDIV_BCLK_RXONLY;
 		auxclk_div_id = MCASP_CLKDIV_AUXCLK_RXONLY;
 	} else if (mcasp->async_mode && stream == SNDRV_PCM_STREAM_PLAYBACK) {
-		auxclk_enabled = mcasp_get_reg(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG) & AHCLKXE;
 		bclk_div_id = MCASP_CLKDIV_BCLK_TXONLY;
 		auxclk_div_id = MCASP_CLKDIV_AUXCLK_TXONLY;
 	} else {
-		auxclk_enabled = mcasp_get_reg(mcasp, DAVINCI_MCASP_AHCLKXCTL_REG) & AHCLKXE;
 		bclk_div_id = MCASP_CLKDIV_BCLK;
 		auxclk_div_id = MCASP_CLKDIV_AUXCLK;
 	}
-- 
2.43.0


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

* [PATCH 2/2] ASoC: ti: davinci-mcasp: improve aux_div selection for mid-range dividers
  2026-03-05 19:58 [PATCH 0/2] ASoC: ti: davinci-mcasp: McASP code cleanup and clk div fixes Sen Wang
  2026-03-05 19:58 ` [PATCH 1/2] ASoC: ti: davinci-mcasp: extract mcasp_is_auxclk_enabled() helper Sen Wang
@ 2026-03-05 19:58 ` Sen Wang
  2026-03-10 15:53 ` [PATCH 0/2] ASoC: ti: davinci-mcasp: McASP code cleanup and clk div fixes Mark Brown
  2 siblings, 0 replies; 4+ messages in thread
From: Sen Wang @ 2026-03-05 19:58 UTC (permalink / raw)
  To: peter.ujfalusi, broonie, lgirdwood, perex, tiwai, linux-sound,
	linux-kernel
  Cc: devarsht, r-donadkar, s-jain1, m-shah, Sen Wang

When the ideal total divider (sysclk/bclk) is between 33 and 4096 and
AUXCLK is enabled, the driver computes aux_div as ceil(div/32) and then
recomputes bclk_div from the truncated sysclk.

This two-step integer division loses precision due to truncation and can
sometimes produce PPM errors large enough for ALSA's hw_rule_format to
reject otherwise valid sample formats.

For example, on AM62D-EVM (auxclk-fs-ratio=2177, tdm-slots=2, fck=96 MHz),
playing S16_LE at 44100 Hz gives BCLK = 1,411,200 Hz and an ideal total
divider of 68.  The old code picks aux_div = ceil(68/32) = 3,
then bclk_div = (96005700/3) / 1411200 = 22, for a total of 3 x 22 =
66 -- two steps from ideal.  The resulting error exceeds the PPM threshold
and causes S16_LE, S24_LE to be rejected.

Therefore when the total divider fits in the AHCLKXDIV register alone
(<=4096), use it directly as aux_div with bclk_div=1, and compare floor
and ceil to pick the closer match, to ensure the best ideal total dividers.

Dividers at or below 32 never enter this path, and dividers above 4096
still fall through to the existing DIV_ROUND_UP path, so previously
working configurations remains unaffected.

Signed-off-by: Sen Wang <sen@ti.com>
---
 sound/soc/ti/davinci-mcasp.c | 34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)

diff --git a/sound/soc/ti/davinci-mcasp.c b/sound/soc/ti/davinci-mcasp.c
index 6d0310f09b12..800c383ed2e1 100644
--- a/sound/soc/ti/davinci-mcasp.c
+++ b/sound/soc/ti/davinci-mcasp.c
@@ -1358,19 +1358,29 @@ static int davinci_mcasp_calc_clk_div(struct davinci_mcasp *mcasp,
 		auxclk_div_id = MCASP_CLKDIV_AUXCLK;
 	}
 
-	if (div > (ACLKXDIV_MASK + 1)) {
-		if (auxclk_enabled) {
-			aux_div = div / (ACLKXDIV_MASK + 1);
-			if (div % (ACLKXDIV_MASK + 1))
-				aux_div++;
-
-			sysclk_freq /= aux_div;
-			div = sysclk_freq / bclk_freq;
-			rem = sysclk_freq % bclk_freq;
-		} else if (set) {
-			dev_warn(mcasp->dev, "Too fast reference clock (%u)\n",
-				 sysclk_freq);
+	if (div > (ACLKXDIV_MASK + 1) && auxclk_enabled) {
+		if (div <= (AHCLKXDIV_MASK + 1)) {
+			/* aux_div absorbs entire division; bclk_div = 1 */
+			aux_div = div;
+			if ((div + 1) <= (AHCLKXDIV_MASK + 1)) {
+				unsigned int err_lo = sysclk_freq / div -
+						      bclk_freq;
+				unsigned int err_hi = bclk_freq -
+						      sysclk_freq / (div + 1);
+
+				if (err_hi < err_lo)
+					aux_div = div + 1;
+			}
+		} else {
+			aux_div = DIV_ROUND_UP(div, ACLKXDIV_MASK + 1);
 		}
+
+		sysclk_freq /= aux_div;
+		div = sysclk_freq / bclk_freq;
+		rem = sysclk_freq % bclk_freq;
+	} else if (div > (ACLKXDIV_MASK + 1) && set) {
+		dev_warn(mcasp->dev, "Too fast reference clock (%u)\n",
+			 sysclk_freq);
 	}
 
 	if (rem != 0) {
-- 
2.43.0


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

* Re: [PATCH 0/2] ASoC: ti: davinci-mcasp: McASP code cleanup and clk div fixes
  2026-03-05 19:58 [PATCH 0/2] ASoC: ti: davinci-mcasp: McASP code cleanup and clk div fixes Sen Wang
  2026-03-05 19:58 ` [PATCH 1/2] ASoC: ti: davinci-mcasp: extract mcasp_is_auxclk_enabled() helper Sen Wang
  2026-03-05 19:58 ` [PATCH 2/2] ASoC: ti: davinci-mcasp: improve aux_div selection for mid-range dividers Sen Wang
@ 2026-03-10 15:53 ` Mark Brown
  2 siblings, 0 replies; 4+ messages in thread
From: Mark Brown @ 2026-03-10 15:53 UTC (permalink / raw)
  To: peter.ujfalusi, lgirdwood, perex, tiwai, linux-sound,
	linux-kernel, Sen Wang
  Cc: devarsht, r-donadkar, s-jain1, m-shah

On Thu, 05 Mar 2026 13:58:23 -0600, Sen Wang wrote:
> Just two minor patches that aim to tidy up the code a little bit,
> as well as fix the aux_div selection in davinci_mcasp_calc_clk_div()
> for mid-range dividers (33 <= div <= 4096).
> 
> Sen Wang (2):
>   ASoC: ti: davinci-mcasp: extract mcasp_is_auxclk_enabled() helper
>   ASoC: ti: davinci-mcasp: improve aux_div selection for mid-range dividers
> 
> [...]

Applied to

   https://git.kernel.org/pub/scm/linux/kernel/git/broonie/sound.git for-next

Thanks!

[1/2] ASoC: ti: davinci-mcasp: extract mcasp_is_auxclk_enabled() helper
      commit: 5bebbfd64b879d1a7220233767be3274e7d442b8
[2/2] ASoC: ti: davinci-mcasp: improve aux_div selection for mid-range dividers
      commit: a8075ada4a341ce58ebf8bef0188cefe6c2f6487

All being well this means that it will be integrated into the linux-next
tree (usually sometime in the next 24 hours) and sent to Linus during
the next merge window (or sooner if it is a bug fix), however if
problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing
and review of the tree, please engage with people reporting problems and
send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they
should be sent as incremental updates against current git, existing
patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying
to this mail.

Thanks,
Mark


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

end of thread, other threads:[~2026-03-10 15:53 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-05 19:58 [PATCH 0/2] ASoC: ti: davinci-mcasp: McASP code cleanup and clk div fixes Sen Wang
2026-03-05 19:58 ` [PATCH 1/2] ASoC: ti: davinci-mcasp: extract mcasp_is_auxclk_enabled() helper Sen Wang
2026-03-05 19:58 ` [PATCH 2/2] ASoC: ti: davinci-mcasp: improve aux_div selection for mid-range dividers Sen Wang
2026-03-10 15:53 ` [PATCH 0/2] ASoC: ti: davinci-mcasp: McASP code cleanup and clk div fixes Mark Brown

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