ASoC: soc-ops: Fix clamp range for SOC_DOUBLE_SX_TLV controls
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
@@ -148,7 +148,7 @@ static int soc_mixer_reg_to_ctl(struct soc_mixer_control *mc, unsigned int reg_vif (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
Quoted 28 lines · Cole Leavitt
For
SOC_DOUBLE_SX_TLVcontrols,mc->minrepresents the hardware register offset (e.g., 283) andmc->maxrepresents 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 value0x1FF(511) should read as user value 228
- Butclamp(511, 283, 229)returns 229, then 229 - 283 = -54
- The masked result (-54 &0x1FF) = 458, which is wrongFix by clamping to [min, min + max] which correctly handles both regular controls (where min=0) and SX controls (where min is an offset).
Signed-off-by Cole Leavitt <[email protected]>--- sound/soc/soc-ops.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)sound/soc/soc-ops.c +0
-0
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_vif (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