Skip to contentSkip to conversation

Fix several issues in the DesignWare I2S driver

PATCH Superseded [email protected] 4 participants 16 messages Nov 6 – Nov 10

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Fix several issues in the DesignWare I2S driver

From: bui duc phuc <[email protected]>

Hi all,

I've continued reviewing the DesignWare I2S driver and referred to the DesignWare APB I2S databook when looking into its clock configuration, channel limits, and FIFO handling.

I couldn't access the document through the manufacturer's website due to regional restrictions, so I've found an externally hosted copy for convenience. The Synopsys product page is also included for reference.

Databook:
https://www.scribd.com/document/891940768/Designware-apb-i2s
Manufacturer's website:
https://www.synopsys.com/dw/ipdir.php?c=DW_apb_i2s

Compile-tested only.

Best regards,
Phuc

bui duc phuc (5):
ASoC: dwc: Fix bit clock rate in TDM mode
ASoC: dwc: Fix error handling in dw_i2s_set_fmt()
ASoC: dwc: Fix channels_max derived from COMP_PARAM_1
ASoC: dwc: Fix DMA fifo_size calculation
ASoC: dwc: Remove redundant error messages

 sound/soc/dwc/dwc-i2s.c | 53 ++++++++++++++++-------------------------
 1 file changed, 21 insertions(+), 32 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/5 ASoC: dwc: Fix bit clock rate in TDM mode

From: bui duc phuc <[email protected]>

The bit clock is computed as rate * data_width * 2, which only fits a two-slot I2S frame. Use rate * data_width * tdm_slots in TDM mode.

Fixes 221acc16aee1 ("ASoC: dwc: Add TDM mode support")
---
 sound/soc/dwc/dwc-i2s.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
sound/soc/dwc/dwc-i2s.c +4 -1
index dbda943cfaf3..9007ba1157ac 100644
@@ -331,7 +331,10 @@ static int dw_i2s_hw_params(struct snd_pcm_substream *substream,
 			}
 		} else {
 			u32 bitclk = config->sample_rate *
-					config->data_width * 2;
+				     config->data_width * 2;
+			if (dev->tdm_slots)
+				bitclk = config->sample_rate *
+					 config->data_width * dev->tdm_slots;
 
 			ret = clk_set_rate(dev->clk, bitclk);
 			if (ret) {
Signature · 1 line
2.43.0
#4 lore raw

PATCH 2/5 ASoC: dwc: Fix error handling in dw_i2s_set_fmt()

In reply to [email protected]

From: bui duc phuc <[email protected]>

Return early when an unsupported clock provider format is encountered, preventing the function from updating frame_offset before returning an error.

Fixes 221acc16aee1 ("ASoC: dwc: Add TDM mode support")
---
 sound/soc/dwc/dwc-i2s.c | 24 +++++++-----------------
 1 file changed, 7 insertions(+), 17 deletions(-)
sound/soc/dwc/dwc-i2s.c +7 -17
index 9007ba1157ac..6dc84c55db0f 100644
@@ -390,29 +390,19 @@ static int dw_i2s_trigger(struct snd_pcm_substream *substream,
 static int dw_i2s_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
 {
 	struct dw_i2s_dev *dev = snd_soc_dai_get_drvdata(cpu_dai);
-	int ret = 0;
 
 	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
 	case SND_SOC_DAIFMT_BC_FC:
-		if (dev->capability & DW_I2S_SLAVE)
-			ret = 0;
-		else
-			ret = -EINVAL;
+		if (!(dev->capability & DW_I2S_SLAVE))
+			return -EINVAL;
 		break;
 	case SND_SOC_DAIFMT_BP_FP:
-		if (dev->capability & DW_I2S_MASTER)
-			ret = 0;
-		else
-			ret = -EINVAL;
-		break;
-	case SND_SOC_DAIFMT_BC_FP:
-	case SND_SOC_DAIFMT_BP_FC:
-		ret = -EINVAL;
+		if (!(dev->capability & DW_I2S_MASTER))
+			return -EINVAL;
 		break;
 	default:
 		dev_dbg(dev->dev, "dwc : Invalid clock provider format\n");
-		ret = -EINVAL;
-		break;
+		return -EINVAL;
 	}
 
 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
@@ -427,11 +417,11 @@ static int dw_i2s_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
 		dev->frame_offset = 0;
 		break;
 	default:
-		dev_err(dev->dev, "DAI format unsupported");
+		dev_err(dev->dev, "DAI format unsupported\n");
 		return -EINVAL;
 	}
 
-	return ret;
+	return 0;
 }
 
 static int dw_i2s_set_tdm_slot(struct snd_soc_dai *cpu_dai,	unsigned int tx_mask,
Signature · 1 line
2.43.0
#7 lore raw

PATCH 3/5 ASoC: dwc: Fix channels_max derived from COMP_PARAM_1

In reply to [email protected]

From: bui duc phuc <[email protected]>

The I2S_TX_CHANNELS (bits 10:9) and I2S_RX_CHANNELS (bits 8:7) fields of I2S_COMP_PARAM_1 encode the number of hardware channels minus one, i.e. values 0..3 mean 1..4 channels. In I2S mode each hardware channel is one serial data line carrying a left/right pair, so the maximum number of audio channels is 2 * (N + 1).

The current code uses 1 << (N + 1), which only produces the correct result for N < 2. For N >= 2, it overestimates the maximum number of audio channels.

Use 2 * (N + 1) for both playback and capture.

Fixes b226efe5818b ("ASoC: dwc: Read I2S block configuration from registers")
---
 sound/soc/dwc/dwc-i2s.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
sound/soc/dwc/dwc-i2s.c +2 -2
index 6dc84c55db0f..0de7bcb2d0c5 100644
@@ -605,7 +605,7 @@ static int dw_configure_dai(struct dw_i2s_dev *dev,
 			idx = 1;
 		dw_i2s_dai->playback.channels_min = MIN_CHANNEL_NUM;
 		dw_i2s_dai->playback.channels_max =
-				1 << (COMP1_TX_CHANNELS(comp1) + 1);
+				2 * (COMP1_TX_CHANNELS(comp1) + 1);
 		dw_i2s_dai->playback.formats = formats[idx];
 		dw_i2s_dai->playback.rates = rates;
 	}
@@ -619,7 +619,7 @@ static int dw_configure_dai(struct dw_i2s_dev *dev,
 			idx = 1;
 		dw_i2s_dai->capture.channels_min = MIN_CHANNEL_NUM;
 		dw_i2s_dai->capture.channels_max =
-				1 << (COMP1_RX_CHANNELS(comp1) + 1);
+				2 * (COMP1_RX_CHANNELS(comp1) + 1);
 		dw_i2s_dai->capture.formats = formats[idx];
 		dw_i2s_dai->capture.rates = rates;
 	}
Signature · 1 line
2.43.0
#9 lore raw

PATCH 4/5 ASoC: dwc: Fix DMA fifo_size calculation

In reply to [email protected]

From: bui duc phuc <[email protected]>

fifo_size should be the FIFO size in bytes (depth * width / 8), but the code shifts by 8 instead of 3. For capture the parentheses also make it always 0.

Use depth * width / 8 for both directions.

Fixes 52ea7c0543f8 ("ASoC: dwc: i2s: Add StarFive JH7110 SoC support")
---
 sound/soc/dwc/dwc-i2s.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
sound/soc/dwc/dwc-i2s.c +4 -4
index 0de7bcb2d0c5..1ff5b6f8864d 100644
@@ -664,14 +664,14 @@ static int dw_configure_dai_by_pd(struct dw_i2s_dev *dev,
 			idx2 = COMP1_TX_WORDSIZE_0(comp1);
 			dev->play_dma_data.dt.addr = res->start + I2S_TXDMA;
 			dev->play_dma_data.dt.fifo_size = dev->fifo_th * 2 *
-				(fifo_width[idx2]) >> 8;
+							  fifo_width[idx2] / 8;
 			dev->play_dma_data.dt.maxburst = 16;
 		}
 		if (COMP1_RX_ENABLED(comp1)) {
 			idx2 = COMP2_RX_WORDSIZE_0(comp2);
 			dev->capture_dma_data.dt.addr = res->start + I2S_RXDMA;
 			dev->capture_dma_data.dt.fifo_size = dev->fifo_th * 2 *
-				(fifo_width[idx2] >> 8);
+							     fifo_width[idx2] / 8;
 			dev->capture_dma_data.dt.maxburst = 16;
 		}
 	} else {
@@ -711,7 +711,7 @@ static int dw_configure_dai_by_dt(struct dw_i2s_dev *dev,
 		dev->capability |= DWC_I2S_PLAY;
 		dev->play_dma_data.dt.addr = res->start + I2S_TXDMA;
 		dev->play_dma_data.dt.fifo_size = fifo_depth *
-			(fifo_width[idx2]) >> 8;
+						  fifo_width[idx2] / 8;
 		dev->play_dma_data.dt.maxburst = 16;
 	}
 	if (COMP1_RX_ENABLED(comp1)) {
@@ -720,7 +720,7 @@ static int dw_configure_dai_by_dt(struct dw_i2s_dev *dev,
 		dev->capability |= DWC_I2S_RECORD;
 		dev->capture_dma_data.dt.addr = res->start + I2S_RXDMA;
 		dev->capture_dma_data.dt.fifo_size = fifo_depth *
-			(fifo_width[idx2] >> 8);
+						     fifo_width[idx2] / 8;
 		dev->capture_dma_data.dt.maxburst = 16;
 	}
 
Signature · 1 line
2.43.0
#11 lore raw

PATCH 5/5 ASoC: dwc: Remove redundant error messages

In reply to [email protected]

From: bui duc phuc <[email protected]>

The underlying functions already report these errors deeper in the call chain, making the additional dev_err_probe() and dev_err() calls unnecessary.

---
 sound/soc/dwc/dwc-i2s.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
sound/soc/dwc/dwc-i2s.c +4 -8
index 1ff5b6f8864d..87b47661e469 100644
@@ -752,7 +752,7 @@ static int jh7110_i2s_crg_master_init(struct dw_i2s_dev *dev)
 
 	ret = clk_bulk_get(dev->dev, ARRAY_SIZE(clks), clks);
 	if (ret)
-		return dev_err_probe(dev->dev, ret, "failed to get i2s clocks\n");
+		return ret;
 
 	mclk = clks[0].clk;
 	mclk_ext = clks[1].clk;
@@ -830,7 +830,7 @@ static int jh7110_i2s_crg_slave_init(struct dw_i2s_dev *dev)
 
 	ret = clk_bulk_get(dev->dev, ARRAY_SIZE(clks), clks);
 	if (ret)
-		return dev_err_probe(dev->dev, ret, "failed to get i2s clocks\n");
+		return ret;
 
 	mclk = clks[0].clk;
 	mclk_ext = clks[1].clk;
@@ -966,10 +966,8 @@ static int dw_i2s_probe(struct platform_device *pdev)
 	if (irq > 0) {
 		ret = devm_request_irq(&pdev->dev, irq, i2s_irq_handler, 0,
 				pdev->name, dev);
-		if (ret < 0) {
-			dev_err(&pdev->dev, "failed to request irq\n");
+		if (ret < 0)
 			goto err_assert_reset;
-		}
 	}
 
 	dev->i2s_reg_comp1 = I2S_COMP_PARAM_1;
@@ -1010,10 +1008,8 @@ static int dw_i2s_probe(struct platform_device *pdev)
 	dev_set_drvdata(&pdev->dev, dev);
 	ret = devm_snd_soc_register_component(&pdev->dev, &dw_i2s_component,
 					 dw_i2s_dai, 1);
-	if (ret != 0) {
-		dev_err(&pdev->dev, "not able to register dai\n");
+	if (ret)
 		goto err_assert_reset;
-	}
 
 	if (!pdata || dev->is_jh7110) {
 		if (irq >= 0) {
Signature · 1 line
2.43.0
#10 lore raw

Re: [PATCH 4/5] ASoC: dwc: Fix DMA fifo_size calculation

In reply to [email protected]
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] ASoC: dwc: missing Fixes trailer for dw_configure_dai_by_dt() fifo_size bug --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] ASoC: dwc: missing Fixes trailer for dw_configure_dai_by_dt() fifo_size bug
--

commit 8358dacf329eafc6bd36da9f372ae4682d156a1c
Author: bui duc phuc <[email protected]>

Quoted 10 lines

ASoC: dwc: Fix DMA fifo_size calculation

fifo_size should be the FIFO size in bytes (depth * width / 8), but the code shifts by 8 instead of 3. For capture the parentheses also make it always 0.

Use depth * width / 8 for both directions.

Fixes 52ea7c0543f8 ("ASoC: dwc: i2s: Add StarFive JH7110 SoC support")

[Severity: Low]
52ea7c0543f8 ("ASoC: dwc: i2s: Add StarFive JH7110 SoC support") only copied the broken fifo_size calculation into dw_configure_dai_by_pd(), whereas the original bug in dw_configure_dai_by_dt() dates back earlier. Should this also include:

Fixes 0d274544bfee ("ASoC: dwc: Add devicetree support for Designware I2S")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#12 lore raw

Re: [PATCH 5/5] ASoC: dwc: Remove redundant error messages

In reply to [email protected]
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] ASoC: dwc: use-after-free in…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] ASoC: dwc: use-after-free in jh7110_i2s_crg_master_init()
[1]
--

commit 5c9147e66967380d8d14ec0323b003b6d572821e
Author: bui duc phuc <[email protected]>

ASoC: dwc: Remove redundant error messages

This commit removes dev_err_probe() and dev_err() calls in the DesignWare I2S driver where the underlying helper functions already log errors on failure.

Quoted 14 lines
sound/soc/dwc/dwc-i2s.c +1 -1
index 1ff5b6f8864d6..87b47661e4698 100644
@@ -752,7 +752,7 @@ static int jh7110_i2s_crg_master_init(struct dw_i2s_dev *dev)
 
 	ret = clk_bulk_get(dev->dev, ARRAY_SIZE(clks), clks);
 	if (ret)
-		return dev_err_probe(dev->dev, ret, "failed to get i2s clocks\n");
+		return ret;
 
 	mclk = clks[0].clk;
 	mclk_ext = clks[1].clk;

sound/soc/dwc/dwc-i2s.c static int jh7110_i2s_crg_slave_init(struct dw_i2s_dev *dev) · line 836

Quoted 8 lines
sound/soc/dwc/dwc-i2s.c +1 -1
@@ -830,7 +830,7 @@ static int jh7110_i2s_crg_slave_init(struct dw_i2s_dev *dev)
 
 	ret = clk_bulk_get(dev->dev, ARRAY_SIZE(clks), clks);
 	if (ret)
-		return dev_err_probe(dev->dev, ret, "failed to get i2s clocks\n");
+		return ret;
 
 	mclk = clks[0].clk;
 	mclk_ext = clks[1].clk;

[Severity: Critical]
This problem wasn't introduced by this patch, but can concurrent probes clobber the static clks[] arrays [1] in jh7110_i2s_crg_master_init() and jh7110_i2s_crg_slave_init()?

Both functions declare their struct clk_bulk_data clks[] array as static, so clk_bulk_get() writes per-device struct clk pointers into shared state. If two I2S instances probe concurrently, one overwrites the other's clock pointers in clks[], causing subsequent clk_bulk_put() calls to leak the overwritten references and release the remaining ones multiple times, leading to a use-after-free.

[1] https://sashiko.dev/#/bug/linux-89ddb741-fe78-4a6e-bfd0-66e1730f7ed2

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
maintainer
#14 lore raw

Re: [PATCH 5/5] ASoC: dwc: Remove redundant error messages

In reply to [email protected]

sound/soc/dwc/dwc-i2s.c static int dw_i2s_probe(struct platform_device *pdev) · line 970

Quoted 10 lines

The underlying functions already report these errors deeper in the call chain, making the additional dev_err_probe() and dev_err() calls unnecessary.

sound/soc/dwc/dwc-i2s.c +1 -2
 		ret = devm_request_irq(&pdev->dev, irq, i2s_irq_handler, 0,
 				pdev->name, dev);
-		if (ret < 0) {
-			dev_err(&pdev->dev, "failed to request irq\n");
+		if (ret < 0)
 			goto err_assert_reset;

request_irq() logs some failures, but not those where the interrupt is just missing.

Signature · 1 line
Mark Brown <[email protected]>
#15 lore raw

Re: [PATCH 5/5] ASoC: dwc: Remove redundant error messages

Hi Mark,

Thank you for your review.

Quoted 12 lines

sound/soc/dwc/dwc-i2s.c static int dw_i2s_probe(struct platform_device *pdev) · line 970

Quoted 11 lines

The underlying functions already report these errors deeper in the call chain, making the additional dev_err_probe() and dev_err() calls unnecessary.

              ret = devm_request_irq(&pdev->dev, irq, i2s_irq_handler, 0,
                              pdev->name, dev);
sound/soc/dwc/dwc-i2s.c +1 -2
-             if (ret < 0) {
-                     dev_err(&pdev->dev, "failed to request irq\n");
+             if (ret < 0)
                      goto err_assert_reset;
Quoted 2 lines

request_irq() logs some failures, but not those where the interrupt is just missing.

I understand.
With platform_get_irq_optional(), a missing IRQ is not considered an error, so no error message is expected in that case.

To handle other IRQ retrieval errors explicitly, I'll update the code as follows:

------------------------------------------------------------------------------------------
irq = platform_get_irq_optional(pdev, 0);
if (irq < 0 && irq != -ENXIO)

        return dev_err_probe(&pdev->dev, irq, "failed to get IRQ\n");
if (irq > 0) {
        ret = devm_request_irq(&pdev->dev, irq, i2s_irq_handler, 0,
                                               pdev->name, dev);
        if (ret)
                goto err_assert_reset;
}

-------------------------------------------------------------------------------------------

Best regards,
Phuc

#13 lore raw

Re: [PATCH 5/5] ASoC: dwc: Remove redundant error messages

In reply to [email protected]
Quoted 7 lines

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] ASoC: dwc: use-after-free in jh7110_i2s_crg_master_init()
[1]
--

This is a pre-existing issue unrelated to the current patch, so I'll keep the patch scope unchanged.

Reviewed-by on v1 1/5
#2 lore raw

Re: [PATCH 1/5] ASoC: dwc: Fix bit clock rate in TDM mode

In reply to [email protected]
Quoted 16 lines · 06.11.2026 14:29, [email protected] wrote:

From: bui duc phuc <[email protected]>

The bit clock is computed as rate * data_width * 2, which only fits a two-slot I2S frame. Use rate * data_width * tdm_slots in TDM mode.

Fixes 221acc16aee1 ("ASoC: dwc: Add TDM mode support")
---
  sound/soc/dwc/dwc-i2s.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
sound/soc/dwc/dwc-i2s.c +0 -0
index dbda943cfaf3..9007ba1157ac 100644

sound/soc/dwc/dwc-i2s.c static int dw_i2s_hw_params(struct snd_pcm_substream *substream, · line 334

Quoted 5 lines
sound/soc/dwc/dwc-i2s.c +1 -1
@@ -331,7 +331,10 @@ static int dw_i2s_hw_params(struct snd_pcm_substream *substream,
  			}
  		} else {
  			u32 bitclk = config->sample_rate *
-					config->data_width * 2;
+				     config->data_width * 2;

Should the identation be fixed in a separate commit? It is not related to TDM clock rate.

sound/soc/dwc/dwc-i2s.c static int dw_i2s_hw_params(struct snd_pcm_substream *substream, · line 337

Quoted 3 lines
sound/soc/dwc/dwc-i2s.c +3 -0
+			if (dev->tdm_slots)
+				bitclk = config->sample_rate *
+					 config->data_width * dev->tdm_slots;
Reviewed-by on v1 2/5
#5 lore raw

Re: [PATCH 2/5] ASoC: dwc: Fix error handling in dw_i2s_set_fmt()

In reply to [email protected]
Quoted 8 lines · 06.11.2026 14:29, [email protected] wrote:

From: bui duc phuc <[email protected]>

Return early when an unsupported clock provider format is encountered, preventing the function from updating frame_offset before returning an error.

Fixes 221acc16aee1 ("ASoC: dwc: Add TDM mode support")

sound/soc/dwc/dwc-i2s.c static int dw_i2s_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt) · line 420

Quoted 7 lines
  	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
sound/soc/dwc/dwc-i2s.c +1 -1
@@ -427,11 +417,11 @@ static int dw_i2s_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt)
  		dev->frame_offset = 0;
  		break;
  	default:
-		dev_err(dev->dev, "DAI format unsupported");
+		dev_err(dev->dev, "DAI format unsupported\n");

Should the typo be fixed in a separate commit?

Reviewed-by on v1 3/5
#8 lore raw

Re: [PATCH 3/5] ASoC: dwc: Fix channels_max derived from COMP_PARAM_1

In reply to [email protected]
Quoted 8 lines · 06.11.2026 14:29, [email protected] wrote:

From: bui duc phuc <[email protected]>

The I2S_TX_CHANNELS (bits 10:9) and I2S_RX_CHANNELS (bits 8:7) fields of I2S_COMP_PARAM_1 encode the number of hardware channels minus one, i.e. values 0..3 mean 1..4 channels. In I2S mode each hardware channel is one serial data line carrying a left/right pair, so the maximum number of audio channels is 2 * (N + 1).

Quoted 8 lines

The current code uses 1 << (N + 1), which only produces the correct result for N < 2. For N >= 2, it overestimates the maximum number of audio channels.

Use 2 * (N + 1) for both playback and capture.

Fixes b226efe5818b ("ASoC: dwc: Read I2S block configuration from registers")
#3 lore raw

Re: [PATCH 1/5] ASoC: dwc: Fix bit clock rate in TDM mode

In reply to Maxim Kochetkov

Hi Maxim,

Quoted 7 lines

sound/soc/dwc/dwc-i2s.c static int dw_i2s_hw_params(struct snd_pcm_substream *substream, · line 334

Quoted 3 lines
                      u32 bitclk = config->sample_rate *
sound/soc/dwc/dwc-i2s.c +1 -1
-                                     config->data_width * 2;
+                                  config->data_width * 2;

Should the identation be fixed in a separate commit? It is not related to TDM clock rate.

Thanks for the review.
I'll move the indentation fix into a separate commit.

Best regards,
Phuc

#6 lore raw

Re: [PATCH 2/5] ASoC: dwc: Fix error handling in dw_i2s_set_fmt()

In reply to Maxim Kochetkov

Hi Maxim,

Thank you for your review.

Quoted 6 lines

sound/soc/dwc/dwc-i2s.c static int dw_i2s_set_fmt(struct snd_soc_dai *cpu_dai, unsigned int fmt) · line 420

Quoted 2 lines
sound/soc/dwc/dwc-i2s.c +1 -1
-             dev_err(dev->dev, "DAI format unsupported");
+             dev_err(dev->dev, "DAI format unsupported\n");

Should the typo be fixed in a separate commit?

OK, I'll move the typo fix into a separate commit as well.

Best regards,
Phuc