[PATCH] ASoC: ak4458: Handle errors when setting TDM slots

From: Haowen Tu

Date: Tue Dec 01 2026 - 21:51:48 EST


ak4458_set_tdm_slot() ignores errors when updating the TDM mode
register and records the new slot settings even if the update fails.
The caller can therefore proceed using a frame layout the codec has
not been configured for.

Return the register error and update the stored slot settings only
after a successful update. Use regmap_write_bits() so that retrying
the same settings still writes the register if an earlier bus error
left the new mode in the register cache.

Fixes: 08660086eff9 ("ASoC: ak4458: Add support for AK4458 DAC driver")
Signed-off-by: Haowen Tu <tuhaowen@xxxxxxxxxxxxx>
---
sound/soc/codecs/ak4458.c | 22 ++++++++++++----------
1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/sound/soc/codecs/ak4458.c b/sound/soc/codecs/ak4458.c
index 11b5e4cc11f8..03f96fdb297f 100644
--- a/sound/soc/codecs/ak4458.c
+++ b/sound/soc/codecs/ak4458.c
@@ -306,9 +306,9 @@ static const struct snd_soc_dapm_route ak4497_intercon[] = {

};

-static int ak4458_get_tdm_mode(struct ak4458_priv *ak4458)
+static int ak4458_get_tdm_mode(int slots, int slot_width)
{
- switch (ak4458->slots * ak4458->slot_width) {
+ switch (slots * slot_width) {
case 128:
return 1;
case 256:
@@ -440,7 +440,7 @@ static int ak4458_hw_params(struct snd_pcm_substream *substream,
* Enable/disable Daisy Chain if in TDM mode and the number of played
* channels is bigger than the maximum supported number of channels
*/
- dchn = ak4458_get_tdm_mode(ak4458) &&
+ dchn = ak4458_get_tdm_mode(ak4458->slots, ak4458->slot_width) &&
(ak4458->fmt == SND_SOC_DAIFMT_DSP_B) &&
(channels > channels_max) ? AK4458_DCHAIN_MASK : 0;

@@ -549,16 +549,18 @@ static int ak4458_set_tdm_slot(struct snd_soc_dai *dai, unsigned int tx_mask,
{
struct snd_soc_component *component = dai->component;
struct ak4458_priv *ak4458 = snd_soc_component_get_drvdata(component);
- int mode;
+ int mode, ret;

- ak4458->slots = slots;
- ak4458->slot_width = slot_width;
+ mode = ak4458_get_tdm_mode(slots, slot_width) << AK4458_MODE_SHIFT;

- mode = ak4458_get_tdm_mode(ak4458) << AK4458_MODE_SHIFT;
+ /* A failed write may already have updated the register cache. */
+ ret = regmap_write_bits(ak4458->regmap, AK4458_0A_CONTROL6,
+ AK4458_MODE_MASK, mode);
+ if (ret)
+ return ret;

- snd_soc_component_update_bits(component, AK4458_0A_CONTROL6,
- AK4458_MODE_MASK,
- mode);
+ ak4458->slots = slots;
+ ak4458->slot_width = slot_width;

return 0;
}
--
2.50.1