Skip to content

Commit 1522aac

Browse files
charleskeepaxbroonie
authored andcommitted
ASoC: ops: Replace snd_soc_read_signed() with new helper
The current snd_soc_read_signed() helper is only used from snd_soc_get_volsw() and can be implemented more simply with sign_extend. Remove snd_soc_read_signed() and add a new soc_mixer_reg_to_ctl() helper. This new helper does not include the reading of the register, but does include min and max handling. This allows the helper to replace more of the duplicated code and makes it easier to process the differences between single, double register and double shift style controls. It is worth noting this adds support for sign_bit into the _range and sx callbacks and the invert option to sx callbacks. Signed-off-by: Charles Keepax <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Mark Brown <[email protected]>
1 parent eeb76cb commit 1522aac

File tree

1 file changed

+41
-93
lines changed

1 file changed

+41
-93
lines changed

sound/soc/soc-ops.c

Lines changed: 41 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -110,52 +110,20 @@ int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
110110
}
111111
EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
112112

113-
/**
114-
* snd_soc_read_signed - Read a codec register and interpret as signed value
115-
* @component: component
116-
* @reg: Register to read
117-
* @mask: Mask to use after shifting the register value
118-
* @shift: Right shift of register value
119-
* @sign_bit: Bit that describes if a number is negative or not.
120-
* @signed_val: Pointer to where the read value should be stored
121-
*
122-
* This functions reads a codec register. The register value is shifted right
123-
* by 'shift' bits and masked with the given 'mask'. Afterwards it translates
124-
* the given registervalue into a signed integer if sign_bit is non-zero.
125-
*/
126-
static void snd_soc_read_signed(struct snd_soc_component *component,
127-
unsigned int reg, unsigned int mask,
128-
unsigned int shift, unsigned int sign_bit,
129-
int *signed_val)
113+
static int soc_mixer_reg_to_ctl(struct soc_mixer_control *mc, unsigned int reg_val,
114+
unsigned int mask, unsigned int shift, int max)
130115
{
131-
int ret;
132-
unsigned int val;
133-
134-
val = snd_soc_component_read(component, reg);
135-
val = (val >> shift) & mask;
116+
int val = (reg_val >> shift) & mask;
136117

137-
if (!sign_bit) {
138-
*signed_val = val;
139-
return;
140-
}
141-
142-
/* non-negative number */
143-
if (!(val & BIT(sign_bit))) {
144-
*signed_val = val;
145-
return;
146-
}
118+
if (mc->sign_bit)
119+
val = sign_extend32(val, mc->sign_bit);
147120

148-
ret = val;
121+
val -= mc->min;
149122

150-
/*
151-
* The register most probably does not contain a full-sized int.
152-
* Instead we have an arbitrary number of bits in a signed
153-
* representation which has to be translated into a full-sized int.
154-
* This is done by filling up all bits above the sign-bit.
155-
*/
156-
ret |= ~((int)(BIT(sign_bit) - 1));
123+
if (mc->invert)
124+
val = max - val;
157125

158-
*signed_val = ret;
126+
return val & mask;
159127
}
160128

161129
static int soc_mixer_valid_ctl(struct soc_mixer_control *mc, long val, int max)
@@ -281,34 +249,25 @@ int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
281249
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
282250
struct soc_mixer_control *mc =
283251
(struct soc_mixer_control *)kcontrol->private_value;
284-
unsigned int reg = mc->reg;
285-
unsigned int reg2 = mc->rreg;
286-
unsigned int shift = mc->shift;
287-
unsigned int rshift = mc->rshift;
288-
int max = mc->max;
289-
int min = mc->min;
290-
int sign_bit = mc->sign_bit;
252+
int max = mc->max - mc->min;
291253
unsigned int mask = soc_mixer_mask(mc);
292-
unsigned int invert = mc->invert;
254+
unsigned int reg_val;
293255
int val;
294256

295-
snd_soc_read_signed(component, reg, mask, shift, sign_bit, &val);
257+
reg_val = snd_soc_component_read(component, mc->reg);
258+
val = soc_mixer_reg_to_ctl(mc, reg_val, mask, mc->shift, max);
296259

297-
ucontrol->value.integer.value[0] = val - min;
298-
if (invert)
299-
ucontrol->value.integer.value[0] =
300-
max - ucontrol->value.integer.value[0];
260+
ucontrol->value.integer.value[0] = val;
301261

302262
if (snd_soc_volsw_is_stereo(mc)) {
303-
if (reg == reg2)
304-
snd_soc_read_signed(component, reg, mask, rshift, sign_bit, &val);
305-
else
306-
snd_soc_read_signed(component, reg2, mask, shift, sign_bit, &val);
263+
if (mc->reg == mc->rreg) {
264+
val = soc_mixer_reg_to_ctl(mc, reg_val, mask, mc->rshift, max);
265+
} else {
266+
reg_val = snd_soc_component_read(component, mc->rreg);
267+
val = soc_mixer_reg_to_ctl(mc, reg_val, mask, mc->shift, max);
268+
}
307269

308-
ucontrol->value.integer.value[1] = val - min;
309-
if (invert)
310-
ucontrol->value.integer.value[1] =
311-
max - ucontrol->value.integer.value[1];
270+
ucontrol->value.integer.value[1] = val;
312271
}
313272

314273
return 0;
@@ -408,18 +367,19 @@ int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
408367
(struct soc_mixer_control *)kcontrol->private_value;
409368
unsigned int reg = mc->reg;
410369
unsigned int reg2 = mc->rreg;
411-
unsigned int shift = mc->shift;
412-
unsigned int rshift = mc->rshift;
413-
int min = mc->min;
414370
unsigned int mask = soc_mixer_sx_mask(mc);
415-
unsigned int val;
371+
unsigned int reg_val;
372+
int val;
416373

417-
val = snd_soc_component_read(component, reg);
418-
ucontrol->value.integer.value[0] = ((val >> shift) - min) & mask;
374+
reg_val = snd_soc_component_read(component, reg);
375+
val = soc_mixer_reg_to_ctl(mc, reg_val, mask, mc->shift, mc->max);
376+
377+
ucontrol->value.integer.value[0] = val;
419378

420379
if (snd_soc_volsw_is_stereo(mc)) {
421-
val = snd_soc_component_read(component, reg2);
422-
val = ((val >> rshift) - min) & mask;
380+
reg_val = snd_soc_component_read(component, reg2);
381+
val = soc_mixer_reg_to_ctl(mc, reg_val, mask, mc->rshift, mc->max);
382+
423383
ucontrol->value.integer.value[1] = val;
424384
}
425385

@@ -602,33 +562,21 @@ int snd_soc_get_volsw_range(struct snd_kcontrol *kcontrol,
602562
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
603563
struct soc_mixer_control *mc =
604564
(struct soc_mixer_control *)kcontrol->private_value;
605-
unsigned int reg = mc->reg;
606-
unsigned int rreg = mc->rreg;
607-
unsigned int shift = mc->shift;
608-
int min = mc->min;
609-
int max = mc->max;
565+
int max = mc->max - mc->min;
610566
unsigned int mask = soc_mixer_mask(mc);
611-
unsigned int invert = mc->invert;
612-
unsigned int val;
567+
unsigned int reg_val;
568+
int val;
613569

614-
val = snd_soc_component_read(component, reg);
615-
ucontrol->value.integer.value[0] = (val >> shift) & mask;
616-
if (invert)
617-
ucontrol->value.integer.value[0] =
618-
max - ucontrol->value.integer.value[0];
619-
else
620-
ucontrol->value.integer.value[0] =
621-
ucontrol->value.integer.value[0] - min;
570+
reg_val = snd_soc_component_read(component, mc->reg);
571+
val = soc_mixer_reg_to_ctl(mc, reg_val, mask, mc->shift, max);
572+
573+
ucontrol->value.integer.value[0] = val;
622574

623575
if (snd_soc_volsw_is_stereo(mc)) {
624-
val = snd_soc_component_read(component, rreg);
625-
ucontrol->value.integer.value[1] = (val >> shift) & mask;
626-
if (invert)
627-
ucontrol->value.integer.value[1] =
628-
max - ucontrol->value.integer.value[1];
629-
else
630-
ucontrol->value.integer.value[1] =
631-
ucontrol->value.integer.value[1] - min;
576+
reg_val = snd_soc_component_read(component, mc->rreg);
577+
val = soc_mixer_reg_to_ctl(mc, reg_val, mask, mc->shift, max);
578+
579+
ucontrol->value.integer.value[1] = val;
632580
}
633581

634582
return 0;

0 commit comments

Comments
 (0)