[PATCH 2/2] ASoC: ti: davinci-mcasp: improve aux_div selection for mid-range dividers
From: Sen Wang
Date: Thu Mar 05 2026 - 15:04:17 EST
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@xxxxxx>
---
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