Skip to content

Commit ce8bcdb

Browse files
Sylwester Nawrockibroonie
authored andcommitted
ASoC: samsung: i2s: Protect more registers with a spinlock
Ensure the I2SMOD, I2SPSR registers, which are also exposed through clk API are only accessed with the i2s->spinlock spinlock held. Signed-off-by: Sylwester Nawrocki <[email protected]> Signed-off-by: Mark Brown <[email protected]>
1 parent f367053 commit ce8bcdb

File tree

1 file changed

+51
-30
lines changed

1 file changed

+51
-30
lines changed

sound/soc/samsung/i2s.c

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -472,17 +472,22 @@ static int i2s_set_sysclk(struct snd_soc_dai *dai,
472472
{
473473
struct i2s_dai *i2s = to_info(dai);
474474
struct i2s_dai *other = get_other_dai(i2s);
475-
u32 mod = readl(i2s->addr + I2SMOD);
476475
const struct samsung_i2s_variant_regs *i2s_regs = i2s->variant_regs;
477476
unsigned int cdcon_mask = 1 << i2s_regs->cdclkcon_off;
478477
unsigned int rsrc_mask = 1 << i2s_regs->rclksrc_off;
478+
u32 mod, mask, val = 0;
479+
480+
spin_lock(i2s->lock);
481+
mod = readl(i2s->addr + I2SMOD);
482+
spin_unlock(i2s->lock);
479483

480484
switch (clk_id) {
481485
case SAMSUNG_I2S_OPCLK:
482-
mod &= ~MOD_OPCLK_MASK;
483-
mod |= dir;
486+
mask = MOD_OPCLK_MASK;
487+
val = dir;
484488
break;
485489
case SAMSUNG_I2S_CDCLK:
490+
mask = 1 << i2s_regs->cdclkcon_off;
486491
/* Shouldn't matter in GATING(CLOCK_IN) mode */
487492
if (dir == SND_SOC_CLOCK_IN)
488493
rfs = 0;
@@ -499,15 +504,15 @@ static int i2s_set_sysclk(struct snd_soc_dai *dai,
499504
}
500505

501506
if (dir == SND_SOC_CLOCK_IN)
502-
mod |= 1 << i2s_regs->cdclkcon_off;
503-
else
504-
mod &= ~(1 << i2s_regs->cdclkcon_off);
507+
val = 1 << i2s_regs->cdclkcon_off;
505508

506509
i2s->rfs = rfs;
507510
break;
508511

509512
case SAMSUNG_I2S_RCLKSRC_0: /* clock corrsponding to IISMOD[10] := 0 */
510513
case SAMSUNG_I2S_RCLKSRC_1: /* clock corrsponding to IISMOD[10] := 1 */
514+
mask = 1 << i2s_regs->rclksrc_off;
515+
511516
if ((i2s->quirks & QUIRK_NO_MUXPSR)
512517
|| (clk_id == SAMSUNG_I2S_RCLKSRC_0))
513518
clk_id = 0;
@@ -557,18 +562,19 @@ static int i2s_set_sysclk(struct snd_soc_dai *dai,
557562
return 0;
558563
}
559564

560-
if (clk_id == 0)
561-
mod &= ~(1 << i2s_regs->rclksrc_off);
562-
else
563-
mod |= 1 << i2s_regs->rclksrc_off;
564-
565+
if (clk_id == 1)
566+
val = 1 << i2s_regs->rclksrc_off;
565567
break;
566568
default:
567569
dev_err(&i2s->pdev->dev, "We don't serve that!\n");
568570
return -EINVAL;
569571
}
570572

573+
spin_lock(i2s->lock);
574+
mod = readl(i2s->addr + I2SMOD);
575+
mod = (mod & ~mask) | val;
571576
writel(mod, i2s->addr + I2SMOD);
577+
spin_unlock(i2s->lock);
572578

573579
return 0;
574580
}
@@ -577,9 +583,8 @@ static int i2s_set_fmt(struct snd_soc_dai *dai,
577583
unsigned int fmt)
578584
{
579585
struct i2s_dai *i2s = to_info(dai);
580-
u32 mod = readl(i2s->addr + I2SMOD);
581586
int lrp_shift, sdf_shift, sdf_mask, lrp_rlow, mod_slave;
582-
u32 tmp = 0;
587+
u32 mod, tmp = 0;
583588

584589
lrp_shift = i2s->variant_regs->lrp_off;
585590
sdf_shift = i2s->variant_regs->sdf_off;
@@ -639,12 +644,15 @@ static int i2s_set_fmt(struct snd_soc_dai *dai,
639644
return -EINVAL;
640645
}
641646

647+
spin_lock(i2s->lock);
648+
mod = readl(i2s->addr + I2SMOD);
642649
/*
643650
* Don't change the I2S mode if any controller is active on this
644651
* channel.
645652
*/
646653
if (any_active(i2s) &&
647654
((mod & (sdf_mask | lrp_rlow | mod_slave)) != tmp)) {
655+
spin_unlock(i2s->lock);
648656
dev_err(&i2s->pdev->dev,
649657
"%s:%d Other DAI busy\n", __func__, __LINE__);
650658
return -EAGAIN;
@@ -653,6 +661,7 @@ static int i2s_set_fmt(struct snd_soc_dai *dai,
653661
mod &= ~(sdf_mask | lrp_rlow | mod_slave);
654662
mod |= tmp;
655663
writel(mod, i2s->addr + I2SMOD);
664+
spin_unlock(i2s->lock);
656665

657666
return 0;
658667
}
@@ -661,16 +670,16 @@ static int i2s_hw_params(struct snd_pcm_substream *substream,
661670
struct snd_pcm_hw_params *params, struct snd_soc_dai *dai)
662671
{
663672
struct i2s_dai *i2s = to_info(dai);
664-
u32 mod = readl(i2s->addr + I2SMOD);
673+
u32 mod, mask = 0, val = 0;
665674

666675
if (!is_secondary(i2s))
667-
mod &= ~(MOD_DC2_EN | MOD_DC1_EN);
676+
mask |= (MOD_DC2_EN | MOD_DC1_EN);
668677

669678
switch (params_channels(params)) {
670679
case 6:
671-
mod |= MOD_DC2_EN;
680+
val |= MOD_DC2_EN;
672681
case 4:
673-
mod |= MOD_DC1_EN;
682+
val |= MOD_DC1_EN;
674683
break;
675684
case 2:
676685
if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
@@ -692,44 +701,49 @@ static int i2s_hw_params(struct snd_pcm_substream *substream,
692701
}
693702

694703
if (is_secondary(i2s))
695-
mod &= ~MOD_BLCS_MASK;
704+
mask |= MOD_BLCS_MASK;
696705
else
697-
mod &= ~MOD_BLCP_MASK;
706+
mask |= MOD_BLCP_MASK;
698707

699708
if (is_manager(i2s))
700-
mod &= ~MOD_BLC_MASK;
709+
mask |= MOD_BLC_MASK;
701710

702711
switch (params_width(params)) {
703712
case 8:
704713
if (is_secondary(i2s))
705-
mod |= MOD_BLCS_8BIT;
714+
val |= MOD_BLCS_8BIT;
706715
else
707-
mod |= MOD_BLCP_8BIT;
716+
val |= MOD_BLCP_8BIT;
708717
if (is_manager(i2s))
709-
mod |= MOD_BLC_8BIT;
718+
val |= MOD_BLC_8BIT;
710719
break;
711720
case 16:
712721
if (is_secondary(i2s))
713-
mod |= MOD_BLCS_16BIT;
722+
val |= MOD_BLCS_16BIT;
714723
else
715-
mod |= MOD_BLCP_16BIT;
724+
val |= MOD_BLCP_16BIT;
716725
if (is_manager(i2s))
717-
mod |= MOD_BLC_16BIT;
726+
val |= MOD_BLC_16BIT;
718727
break;
719728
case 24:
720729
if (is_secondary(i2s))
721-
mod |= MOD_BLCS_24BIT;
730+
val |= MOD_BLCS_24BIT;
722731
else
723-
mod |= MOD_BLCP_24BIT;
732+
val |= MOD_BLCP_24BIT;
724733
if (is_manager(i2s))
725-
mod |= MOD_BLC_24BIT;
734+
val |= MOD_BLC_24BIT;
726735
break;
727736
default:
728737
dev_err(&i2s->pdev->dev, "Format(%d) not supported\n",
729738
params_format(params));
730739
return -EINVAL;
731740
}
741+
742+
spin_lock(i2s->lock);
743+
mod = readl(i2s->addr + I2SMOD);
744+
mod = (mod & ~mask) | val;
732745
writel(mod, i2s->addr + I2SMOD);
746+
spin_unlock(i2s->lock);
733747

734748
samsung_asoc_init_dma_data(dai, &i2s->dma_playback, &i2s->dma_capture);
735749

@@ -979,6 +993,7 @@ static int samsung_i2s_dai_probe(struct snd_soc_dai *dai)
979993
{
980994
struct i2s_dai *i2s = to_info(dai);
981995
struct i2s_dai *other = get_other_dai(i2s);
996+
unsigned long flags;
982997

983998
if (is_secondary(i2s)) { /* If this is probe on the secondary DAI */
984999
samsung_asoc_init_dma_data(dai, &other->sec_dai->dma_playback,
@@ -999,11 +1014,14 @@ static int samsung_i2s_dai_probe(struct snd_soc_dai *dai)
9991014
i2s->rfs = 0;
10001015
i2s->bfs = 0;
10011016
i2s->rclk_srcrate = 0;
1017+
1018+
spin_lock_irqsave(i2s->lock, flags);
10021019
i2s_txctrl(i2s, 0);
10031020
i2s_rxctrl(i2s, 0);
10041021
i2s_fifo(i2s, FIC_TXFLUSH);
10051022
i2s_fifo(other, FIC_TXFLUSH);
10061023
i2s_fifo(i2s, FIC_RXFLUSH);
1024+
spin_unlock_irqrestore(i2s->lock, flags);
10071025

10081026
/* Gate CDCLK by default */
10091027
if (!is_opened(other))
@@ -1018,8 +1036,11 @@ static int samsung_i2s_dai_remove(struct snd_soc_dai *dai)
10181036
struct i2s_dai *i2s = snd_soc_dai_get_drvdata(dai);
10191037

10201038
if (!is_secondary(i2s)) {
1021-
if (i2s->quirks & QUIRK_NEED_RSTCLR)
1039+
if (i2s->quirks & QUIRK_NEED_RSTCLR) {
1040+
spin_lock(i2s->lock);
10221041
writel(0, i2s->addr + I2SCON);
1042+
spin_unlock(i2s->lock);
1043+
}
10231044
}
10241045

10251046
return 0;

0 commit comments

Comments
 (0)