Skip to contentSkip to conversation

ASoC: soc-ops: Fix clamp range for SOC_DOUBLE_SX_TLV controls

PATCH In review Cole Leavitt 2 participants 2 messages Jan 12 – Jan 21

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 ASoC: soc-ops: Fix clamp range for SOC_DOUBLE_SX_TLV controls

For SOC_DOUBLE_SX_TLV controls, mc->min represents the hardware register offset (e.g., 283) and mc->max represents the number of user-visible steps (e.g., 229). The valid hardware register range is therefore [min, min + max], not [min, max].

When min > max (which is valid for SX controls), the clamp() call misbehaves because it expects min <= max. This causes the control read function to return incorrect values.

For example, with the CS42L43 headphone volume control:
- min = 0x11B (283), max = 229
- Register value 0x1FF (511) should read as user value 228
- But clamp(511, 283, 229) returns 229, then 229 - 283 = -54
- The masked result (-54 & 0x1FF) = 458, which is wrong

Fix by clamping to [min, min + max] which correctly handles both regular controls (where min=0) and SX controls (where min is an offset).

---
 sound/soc/soc-ops.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/soc/soc-ops.c +1 -1
index ce86978c1..b28fdf238 100644
@@ -148,7 +148,7 @@ static int soc_mixer_reg_to_ctl(struct soc_mixer_control *mc, unsigned int reg_v
 	if (mc->sign_bit)
 		val = sign_extend32(val, mc->sign_bit);
 
-	val = clamp(val, mc->min, mc->max);
+	val = clamp(val, mc->min, mc->min + mc->max);
 	val -= mc->min;
 
 	if (mc->invert)
Signature · 1 line
2.52.0
#1 lore raw
Quoted 28 lines · Cole Leavitt

For SOC_DOUBLE_SX_TLV controls, mc->min represents the hardware register offset (e.g., 283) and mc->max represents the number of user-visible steps (e.g., 229). The valid hardware register range is therefore [min, min + max], not [min, max].

When min > max (which is valid for SX controls), the clamp() call misbehaves because it expects min <= max. This causes the control read function to return incorrect values.

For example, with the CS42L43 headphone volume control:
- min = 0x11B (283), max = 229
- Register value 0x1FF (511) should read as user value 228
- But clamp(511, 283, 229) returns 229, then 229 - 283 = -54
- The masked result (-54 & 0x1FF) = 458, which is wrong

Fix by clamping to [min, min + max] which correctly handles both regular controls (where min=0) and SX controls (where min is an offset).

---
 sound/soc/soc-ops.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
sound/soc/soc-ops.c +0 -0
index ce86978c1..b28fdf238 100644

sound/soc/soc-ops.c static int soc_mixer_reg_to_ctl(struct soc_mixer_control *mc, unsigned int reg_v · line 151

Quoted 5 lines
sound/soc/soc-ops.c +1 -1
@@ -148,7 +148,7 @@ static int soc_mixer_reg_to_ctl(struct soc_mixer_control *mc, unsigned int reg_v
 	if (mc->sign_bit)
 		val = sign_extend32(val, mc->sign_bit);
 
-	val = clamp(val, mc->min, mc->max);
+	val = clamp(val, mc->min, mc->min + mc->max);

No, this was discussed here:

https://lore.kernel.org/linux-sound/[email protected]/

And the actual fix was merged here:

https://lore.kernel.org/all/[email protected]/

Thanks,
Charles