ASoC: stm: Use guard() for mutex & spin locks
Subsystems (4)
cover ASoC: stm: Use guard() for mutex & spin locks
From: bui duc phuc <[email protected]>
Hi all,
This series converts mutex and spinlock handling in the STM drivers to use guard() helpers.
The changes are code cleanup only and should have no functional impact.
Best regards,
Phuc
bui duc phuc (4):
ASoC: stm: stm32_adfsdm: Use guard() for mutex locks
ASoC: stm: stm32_i2s: Use guard() for spin locks
ASoC: stm: stm32_sai_sub: Use guard() for mutex & spin locks
ASoC: stm: stm32_spdifrx: Use guard() for spin locks
sound/soc/stm/stm32_adfsdm.c | 10 ++---- sound/soc/stm/stm32_i2s.c | 67 ++++++++++++++++------------------- sound/soc/stm/stm32_sai_sub.c | 29 ++++++--------- sound/soc/stm/stm32_spdifrx.c | 44 +++++++++-------------- 4 files changed, 60 insertions(+), 90 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/4 ASoC: stm: stm32_adfsdm: Use guard() for mutex locks
From: bui duc phuc <[email protected]>
Clean up the code using guard() for mutex locks.
Merely code refactoring, and no behavior change.
--- sound/soc/stm/stm32_adfsdm.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)
sound/soc/stm/stm32_adfsdm.c +3 -7
@@ -62,12 +62,11 @@ static void stm32_adfsdm_shutdown(struct snd_pcm_substream *substream, {struct stm32_adfsdm_priv *priv =snd_soc_dai_get_drvdata (dai); -mutex_lock (&priv->lock); +guard (mutex)(&priv->lock);if (priv->iio_active) {iio_channel_stop_all_cb (priv->iio_cb); priv->iio_active = false; } -mutex_unlock (&priv->lock); } static int stm32_adfsdm_dai_prepare(struct snd_pcm_substream *substream,
@@ -76,7 +75,7 @@ static int stm32_adfsdm_dai_prepare(struct snd_pcm_substream *substream,struct stm32_adfsdm_priv *priv =snd_soc_dai_get_drvdata (dai);int ret; -mutex_lock (&priv->lock); +guard (mutex)(&priv->lock);if (priv->iio_active) {iio_channel_stop_all_cb (priv->iio_cb); priv->iio_active = false;
@@ -88,7 +87,7 @@ static int stm32_adfsdm_dai_prepare(struct snd_pcm_substream *substream,if (ret <0 ) {dev_err (dai->dev,"%s: Failed to set %d sampling rate\n" , __func__, substream->runtime->rate); - goto out; +return ret; }if (!priv->iio_active) {
@@ -100,9 +99,6 @@ static int stm32_adfsdm_dai_prepare(struct snd_pcm_substream *substream, __func__, ret); } -out: -mutex_unlock (&priv->lock); -return ret; }
Signature · 1 line
2.43.0
PATCH 2/4 ASoC: stm: stm32_i2s: Use guard() for spin locks
In reply to [email protected]From: bui duc phuc <[email protected]>
Clean up the code using guard() for spin locks.
Merely code refactoring, and no behavior change.
--- sound/soc/stm/stm32_i2s.c | 67 ++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 37 deletions(-)
sound/soc/stm/stm32_i2s.c +30 -37
@@ -615,10 +615,10 @@ static irqreturn_t stm32_i2s_isr(int irq, void *devid)if (flags &I2S_SR_TIFRE )dev_dbg (&pdev->dev,"Frame error\n" ); -spin_lock (&i2s->irq_lock); -if (err && i2s->substream) -snd_pcm_stop_xrun (i2s->substream); -spin_unlock (&i2s->irq_lock); + scoped_guard(spinlock, &i2s->irq_lock) { +if (err && i2s->substream) +snd_pcm_stop_xrun (i2s->substream); + }return IRQ_HANDLED ; }
@@ -905,12 +905,10 @@ static int stm32_i2s_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *cpu_dai) {struct stm32_i2s_data *i2s =snd_soc_dai_get_drvdata (cpu_dai); -unsigned long flags;int ret; -spin_lock_irqsave (&i2s->irq_lock, flags); - i2s->substream = substream; -spin_unlock_irqrestore (&i2s->irq_lock, flags); +scoped_guard (spinlock_irqsave, &i2s->irq_lock) + i2s->substream = substream;if ((i2s->fmt &SND_SOC_DAIFMT_FORMAT_MASK ) !=SND_SOC_DAIFMT_DSP_A ) snd_pcm_hw_constraint_single(substream->runtime,
@@ -982,19 +980,19 @@ static int stm32_i2s_trigger(struct snd_pcm_substream *substream, int cmd,regmap_write_bits (i2s->regmap,STM32_I2S_IFCR_REG ,I2S_IFCR_MASK ,I2S_IFCR_MASK ); -spin_lock (&i2s->lock_fd); - i2s->refcount++; -if (playback_flg) { - ier =I2S_IER_UDRIE ; - }else { - ier =I2S_IER_OVRIE ; - -if (STM32_I2S_IS_MASTER (i2s) && i2s->refcount ==1 ) -/* dummy write to gate bus clocks */ -regmap_write (i2s->regmap, -STM32_I2S_TXDR_REG ,0 ); + scoped_guard(spinlock, &i2s->lock_fd) { + i2s->refcount++; +if (playback_flg) { + ier =I2S_IER_UDRIE ; + }else { + ier =I2S_IER_OVRIE ; + +if (STM32_I2S_IS_MASTER (i2s) && i2s->refcount ==1 ) +/* dummy write to gate bus clocks */ +regmap_write (i2s->regmap, +STM32_I2S_TXDR_REG ,0 ); + } } -spin_unlock (&i2s->lock_fd);if (STM32_I2S_IS_SLAVE (i2s)) ier |=I2S_IER_TIFREIE ;
@@ -1016,21 +1014,18 @@ static int stm32_i2s_trigger(struct snd_pcm_substream *substream, int cmd,I2S_IER_OVRIE , (unsigned int)~I2S_IER_OVRIE); -spin_lock (&i2s->lock_fd); - i2s->refcount--; -if (i2s->refcount) { -spin_unlock (&i2s->lock_fd); -break ; - } + scoped_guard(spinlock, &i2s->lock_fd) { + i2s->refcount--; +if (i2s->refcount) +break ; - ret =regmap_update_bits (i2s->regmap,STM32_I2S_CR1_REG , -I2S_CR1_SPE ,0 ); -if (ret <0 ) { -dev_err (cpu_dai->dev,"Error %d disabling I2S\n" , ret); -spin_unlock (&i2s->lock_fd); -return ret; + ret =regmap_update_bits (i2s->regmap,STM32_I2S_CR1_REG , +I2S_CR1_SPE ,0 ); +if (ret <0 ) { +dev_err (cpu_dai->dev,"Error %d disabling I2S\n" , ret); +return ret; + } } -spin_unlock (&i2s->lock_fd); cfg1_mask =I2S_CFG1_RXDMAEN |I2S_CFG1_TXDMAEN ; regmap_update_bits(i2s->regmap, STM32_I2S_CFG1_REG,
@@ -1047,7 +1042,6 @@ static void stm32_i2s_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *cpu_dai) {struct stm32_i2s_data *i2s =snd_soc_dai_get_drvdata (cpu_dai); -unsigned long flags;clk_disable_unprepare (i2s->i2sclk);
@@ -1059,9 +1053,8 @@ static void stm32_i2s_shutdown(struct snd_pcm_substream *substream,if (!i2s->i2smclk && i2s->put_i2s_clk_rate) i2s->put_i2s_clk_rate (i2s); -spin_lock_irqsave (&i2s->irq_lock, flags); - i2s->substream =NULL ; -spin_unlock_irqrestore (&i2s->irq_lock, flags); +scoped_guard (spinlock_irqsave, &i2s->irq_lock) + i2s->substream = NULL; }static int stm32_i2s_dai_probe (struct snd_soc_dai *cpu_dai)
Signature · 1 line
2.43.0
PATCH 3/4 ASoC: stm: stm32_sai_sub: Use guard() for mutex & spin locks
In reply to [email protected]From: bui duc phuc <[email protected]>
Clean up the code using guard() for mutex & spin locks. Merely code refactoring, and no behavior change.
--- sound/soc/stm/stm32_sai_sub.c | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-)
sound/soc/stm/stm32_sai_sub.c +11 -18
@@ -280,9 +280,8 @@ static int snd_pcm_iec958_get(struct snd_kcontrol *kcontrol, {struct stm32_sai_sub_data *sai =snd_kcontrol_chip (kcontrol); -mutex_lock (&sai->ctrl_lock); +guard (mutex)(&sai->ctrl_lock);memcpy (uctl->value.iec958.status, sai->iec958.status,4 ); -mutex_unlock (&sai->ctrl_lock);return 0 ; }
@@ -292,9 +291,8 @@ static int snd_pcm_iec958_put(struct snd_kcontrol *kcontrol, {struct stm32_sai_sub_data *sai =snd_kcontrol_chip (kcontrol); -mutex_lock (&sai->ctrl_lock); +guard (mutex)(&sai->ctrl_lock);memcpy (sai->iec958.status, uctl->value.iec958.status,4 ); -mutex_unlock (&sai->ctrl_lock);return 0 ; }
@@ -658,10 +656,10 @@ static irqreturn_t stm32_sai_isr(int irq, void *devid) status =SNDRV_PCM_STATE_XRUN ; } -spin_lock (&sai->irq_lock); -if (status !=SNDRV_PCM_STATE_RUNNING && sai->substream) -snd_pcm_stop_xrun (sai->substream); -spin_unlock (&sai->irq_lock); + scoped_guard(spinlock, &sai->irq_lock) { +if (status !=SNDRV_PCM_STATE_RUNNING && sai->substream) +snd_pcm_stop_xrun (sai->substream); + }return IRQ_HANDLED ; }
@@ -894,11 +892,9 @@ static int stm32_sai_startup(struct snd_pcm_substream *substream, {struct stm32_sai_sub_data *sai =snd_soc_dai_get_drvdata (cpu_dai);int imr, cr2, ret; -unsigned long flags; -spin_lock_irqsave (&sai->irq_lock, flags); - sai->substream = substream; -spin_unlock_irqrestore (&sai->irq_lock, flags); +scoped_guard (spinlock_irqsave, &sai->irq_lock) + sai->substream = substream;if (STM_SAI_PROTOCOL_IS_SPDIF (sai)) { snd_pcm_hw_constraint_mask64(substream->runtime,
@@ -1083,7 +1079,7 @@ static void stm32_sai_set_iec958_status(struct stm32_sai_sub_data *sai,return ;/* Force the sample rate according to runtime rate */ -mutex_lock (&sai->ctrl_lock); +guard (mutex)(&sai->ctrl_lock);switch (runtime->rate) {case 22050 : sai->iec958.status[3 ] =IEC958_AES3_CON_FS_22050 ;
@@ -1116,7 +1112,6 @@ static void stm32_sai_set_iec958_status(struct stm32_sai_sub_data *sai, sai->iec958.status[3 ] =IEC958_AES3_CON_FS_NOTID ;break ; } -mutex_unlock (&sai->ctrl_lock); } static int stm32_sai_configure_clock(struct snd_soc_dai *cpu_dai,
@@ -1284,7 +1279,6 @@ static void stm32_sai_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *cpu_dai) {struct stm32_sai_sub_data *sai =snd_soc_dai_get_drvdata (cpu_dai); -unsigned long flags;stm32_sai_sub_reg_up (sai,STM_SAI_IMR_REGX ,SAI_XIMR_MASK ,0 );
@@ -1298,9 +1292,8 @@ static void stm32_sai_shutdown(struct snd_pcm_substream *substream,if (!sai->sai_mclk && sai->put_sai_ck_rate) sai->put_sai_ck_rate (sai); -spin_lock_irqsave (&sai->irq_lock, flags); - sai->substream =NULL ; -spin_unlock_irqrestore (&sai->irq_lock, flags); +scoped_guard (spinlock_irqsave, &sai->irq_lock) + sai->substream = NULL; } static int stm32_sai_pcm_new(struct snd_soc_pcm_runtime *rtd,
Signature · 1 line
2.43.0
PATCH 4/4 ASoC: stm: stm32_spdifrx: Use guard() for spin locks
In reply to [email protected]From: bui duc phuc <[email protected]>
Clean up the code using guard() for spin locks.
Merely code refactoring, and no behavior change.
--- sound/soc/stm/stm32_spdifrx.c | 44 +++++++++++++---------------------- 1 file changed, 16 insertions(+), 28 deletions(-)
sound/soc/stm/stm32_spdifrx.c +16 -28
@@ -322,7 +322,6 @@ static void stm32_spdifrx_dma_ctrl_stop(struct stm32_spdifrx_data *spdifrx)static int stm32_spdifrx_start_sync (struct stm32_spdifrx_data *spdifrx) {int cr, cr_mask, imr, ret; -unsigned long flags;/* Enable IRQs */ imr =SPDIFRX_IMR_IFEIE |SPDIFRX_IMR_SYNCDIE |SPDIFRX_IMR_PERRIE ;
@@ -330,7 +329,7 @@ static int stm32_spdifrx_start_sync(struct stm32_spdifrx_data *spdifrx)if (ret)return ret; -spin_lock_irqsave (&spdifrx->lock, flags); +guard (spinlock_irqsave)(&spdifrx->lock); spdifrx->refcount++;
@@ -365,22 +364,17 @@ static int stm32_spdifrx_start_sync(struct stm32_spdifrx_data *spdifrx) "Failed to start synchronization\n"); } -spin_unlock_irqrestore (&spdifrx->lock, flags); -return ret; }static void stm32_spdifrx_stop (struct stm32_spdifrx_data *spdifrx) {int cr, cr_mask, reg; -unsigned long flags; -spin_lock_irqsave (&spdifrx->lock, flags); +guard (spinlock_irqsave)(&spdifrx->lock); -if (--spdifrx->refcount) { -spin_unlock_irqrestore (&spdifrx->lock, flags); +if (--spdifrx->refcount)return ; - } cr =SPDIFRX_CR_SPDIFENSET (SPDIFRX_SPDIFEN_DISABLE ); cr_mask =SPDIFRX_CR_SPDIFEN_MASK |SPDIFRX_CR_RXDMAEN ;
@@ -396,8 +390,6 @@ static void stm32_spdifrx_stop(struct stm32_spdifrx_data *spdifrx)/* dummy read to clear CSRNE and RXNE in status register */ regmap_read (spdifrx->regmap,STM32_SPDIFRX_DR , ®);regmap_read (spdifrx->regmap,STM32_SPDIFRX_CSR , ®); - -spin_unlock_irqrestore (&spdifrx->lock, flags); } static int stm32_spdifrx_dma_ctrl_register(struct device *dev,
@@ -744,19 +736,19 @@ static irqreturn_t stm32_spdifrx_isr(int irq, void *devid)return IRQ_HANDLED ; } -spin_lock (&spdifrx->irq_lock); -if (spdifrx->substream) -snd_pcm_stop (spdifrx->substream, -SNDRV_PCM_STATE_DISCONNECTED ); -spin_unlock (&spdifrx->irq_lock); + scoped_guard(spinlock, &spdifrx->irq_lock) { +if (spdifrx->substream) +snd_pcm_stop (spdifrx->substream, +SNDRV_PCM_STATE_DISCONNECTED ); + }return IRQ_HANDLED ; } -spin_lock (&spdifrx->irq_lock); -if (err_xrun && spdifrx->substream) -snd_pcm_stop_xrun (spdifrx->substream); -spin_unlock (&spdifrx->irq_lock); + scoped_guard(spinlock, &spdifrx->irq_lock) { +if (err_xrun && spdifrx->substream) +snd_pcm_stop_xrun (spdifrx->substream); + }return IRQ_HANDLED ; }
@@ -765,12 +757,10 @@ static int stm32_spdifrx_startup(struct snd_pcm_substream *substream, struct snd_soc_dai *cpu_dai) {struct stm32_spdifrx_data *spdifrx =snd_soc_dai_get_drvdata (cpu_dai); -unsigned long flags;int ret; -spin_lock_irqsave (&spdifrx->irq_lock, flags); - spdifrx->substream = substream; -spin_unlock_irqrestore (&spdifrx->irq_lock, flags); +scoped_guard (spinlock_irqsave, &spdifrx->irq_lock) + spdifrx->substream = substream; ret =clk_prepare_enable (spdifrx->kclk);if (ret)
@@ -846,11 +836,9 @@ static void stm32_spdifrx_shutdown(struct snd_pcm_substream *substream, struct snd_soc_dai *cpu_dai) {struct stm32_spdifrx_data *spdifrx =snd_soc_dai_get_drvdata (cpu_dai); -unsigned long flags; -spin_lock_irqsave (&spdifrx->irq_lock, flags); - spdifrx->substream =NULL ; -spin_unlock_irqrestore (&spdifrx->irq_lock, flags); +scoped_guard (spinlock_irqsave, &spdifrx->irq_lock) + spdifrx->substream = NULL;clk_disable_unprepare (spdifrx->kclk); }
Signature · 1 line
2.43.0
Mark Brown
maintainerRe: [PATCH 2/4] ASoC: stm: stm32_i2s: Use guard() for spin locks
In reply to [email protected]Quoted 4 lines · On Wed, May 13, 2026 at 05:43:27PM +0700, [email protected] wrote:
sound/soc/stm/stm32_i2s.c +0
-0@@ -1016,21 +1014,18 @@ static int stm32_i2s_trigger(struct snd_pcm_substream *substream, int cmd,I2S_IER_OVRIE , (unsigned int)~I2S_IER_OVRIE);
sound/soc/stm/stm32_i2s.c static int stm32_i2s_trigger(struct snd_pcm_substream *substream, int cmd, · line 1020
Quoted 10 lines
sound/soc/stm/stm32_i2s.c +4
-6-spin_lock (&i2s->lock_fd); - i2s->refcount--; -if (i2s->refcount) { -spin_unlock (&i2s->lock_fd); -break ; - } + scoped_guard(spinlock, &i2s->lock_fd) { + i2s->refcount--; +if (i2s->refcount) +break ;
How does scoped_guard interact with break statements - does this still apply to the switch? I've not looked at how they're implemented...
Re: [PATCH 2/4] ASoC: stm: stm32_i2s: Use guard() for spin locks
Hi Mark,
Quoted 2 lines · Mark Brown
How does
scoped_guardinteract with break statements - does this still apply to the switch? I've not looked at how they're implemented...
I checked the scoped_guard macro implementation...
and You're right the break statement inside scoped_guard only exits the guard's implicit loop, which is not what I intended.
Since there are no further statements after the switch block in this function, I will replace the break with return 0 to correctly exit the function.
I'll send a v2 of the whole series with this fix.
Would that work for you?
Best regard,
Phuc
Mark Brown
maintainerRe: [PATCH 2/4] ASoC: stm: stm32_i2s: Use guard() for spin locks
Quoted 2 lines · Bui Duc Phuc
I'll send a v2 of the whole series with this fix.
Would that work for you?
Yes.
Re: [PATCH 2/4] ASoC: stm: stm32_i2s: Use guard() for spin locks
Hi,
Quoted 4 lines
Quoted 2 lines
I'll send a v2 of the whole series with this fix.
Would that work for you?Yes.
Thank you for the confirmation.
By the way, I have an architectural question that came up during my cleanup...
I noticed that the STM drivers currently manage clk_prepare_enable()/ clk_disable_unprepare() directly from the dai_startup()/shutdown() paths.
After looking through various vendor audio drivers, I noticed that PM/clock handling styles still vary quite a lot between implementations.
For example:
some Intel drivers enable clocks during dai_link init.
Samsung separates bus/interface clocks and operational clocks, enabling them in different paths such as probe() and set_sysclk()
UX500 and STM enable clocks during DAI startup()/shutdown()
Tegra, Sunxi, and Rockchip often manage clocks through runtime PM callbacks
From a maintainer perspective, is there generally interest in gradually converging these drivers toward more modern/common PM patterns, or is preserving existing hardware-specific sequencing usually preferred unless there is a concrete issue to solve?
Best regard,
Phuc
Mark Brown
maintainerRe: [PATCH 2/4] ASoC: stm: stm32_i2s: Use guard() for spin locks
Quoted 4 lines · Bui Duc Phuc
From a maintainer perspective, is there generally interest in gradually converging these drivers toward more modern/common PM patterns, or is preserving existing hardware-specific sequencing usually preferred unless there is a concrete issue to solve?
There are likely to be different considerations for different drivers, on some systems the power savings from managing the clocks may not be meaingful or we may need the clocks for register access. In general it's nicer to actively manage the clocks but it's not super urgent to do so from a framework point of view, it's more a how much work the people working on the individual drivers want to do and if there's a use case for specific hardware.
Re: [PATCH 2/4] ASoC: stm: stm32_i2s: Use guard() for spin locks
Hi Mark,
Quoted 7 lines
There are likely to be different considerations for different drivers, on some systems the power savings from managing the clocks may not be meaingful or we may need the clocks for register access. In general it's nicer to actively manage the clocks but it's not super urgent to do so from a framework point of view, it's more a how much work the people working on the individual drivers want to do and if there's a use case for specific hardware.
Understood, and thank you for the clarification.
That gives me a better understanding of the considerations around PM/clock handling in these drivers.
Best Regard,
Phuc