[PATCH 3/4] ASoC: stm: stm32_sai_sub: Use guard() for mutex & spin locks

From: phucduc . bui

Date: Wed May 13 2026 - 06:45:58 EST


From: bui duc phuc <phucduc.bui@xxxxxxxxx>

Clean up the code using guard() for mutex & spin locks.
Merely code refactoring, and no behavior change.

Signed-off-by: bui duc phuc <phucduc.bui@xxxxxxxxx>
---
sound/soc/stm/stm32_sai_sub.c | 29 +++++++++++------------------
1 file changed, 11 insertions(+), 18 deletions(-)

diff --git a/sound/soc/stm/stm32_sai_sub.c b/sound/soc/stm/stm32_sai_sub.c
index 3e82fa90e719..ea9e8bddd63f 100644
--- a/sound/soc/stm/stm32_sai_sub.c
+++ b/sound/soc/stm/stm32_sai_sub.c
@@ -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,
--
2.43.0