From: Niranjan H Y <niranjan.hy@ti.com>
To: <alsa-devel@alsa-project.org>
Cc: <linux-sound@vger.kernel.org>, <lgirdwood@gmail.com>,
<broonie@kernel.org>, <perex@perex.cz>, <tiwai@suse.com>,
<yung-chuan.liao@linux.intel.com>, <cezary.rojewski@intel.com>,
<peter.ujfalusi@linux.intel.com>,
<ranjani.sridharan@linux.intel.com>,
<kai.vehmanen@linux.intel.com>, <pierre-louis.bossart@linux.dev>,
<navada@ti.com>, <shenghao-ding@ti.com>, <v-hampiholi@ti.com>,
<baojun.xu@ti.com>, Niranjan H Y <niranjan.hy@ti.com>
Subject: [PATCH v4 1/6] ASoC: ops: improve snd_soc_get_volsw
Date: Thu, 11 Sep 2025 21:26:58 +0530 [thread overview]
Message-ID: <20250911155704.2236-1-niranjan.hy@ti.com> (raw)
* add error handling in case register read fails
* add support for clamping the values if the register
value read is greater than max value
Signed-off-by: Niranjan H Y <niranjan.hy@ti.com>
---
sound/soc/soc-ops.c | 38 +++++++++++++++++++++++++++++---------
1 file changed, 29 insertions(+), 9 deletions(-)
diff --git a/sound/soc/soc-ops.c b/sound/soc/soc-ops.c
index a629e0eac..59e91741b 100644
--- a/sound/soc/soc-ops.c
+++ b/sound/soc/soc-ops.c
@@ -111,10 +111,14 @@ int snd_soc_put_enum_double(struct snd_kcontrol *kcontrol,
EXPORT_SYMBOL_GPL(snd_soc_put_enum_double);
static int soc_mixer_reg_to_ctl(struct soc_mixer_control *mc, unsigned int reg_val,
- unsigned int mask, unsigned int shift, int max)
+ unsigned int mask, unsigned int shift, int max,
+ bool clamp)
{
int val = (reg_val >> shift) & mask;
+ if (clamp)
+ val = clamp(val, 0, mc->max);
+
if (mc->sign_bit)
val = sign_extend32(val, mc->sign_bit);
@@ -245,29 +249,44 @@ static int soc_put_volsw(struct snd_kcontrol *kcontrol,
static int soc_get_volsw(struct snd_kcontrol *kcontrol,
struct snd_ctl_elem_value *ucontrol,
- struct soc_mixer_control *mc, int mask, int max)
+ struct soc_mixer_control *mc, int mask, int max,
+ bool clamp)
{
struct snd_soc_component *component = snd_kcontrol_chip(kcontrol);
unsigned int reg_val;
- int val;
+ int val, ret = 0;
reg_val = snd_soc_component_read(component, mc->reg);
- val = soc_mixer_reg_to_ctl(mc, reg_val, mask, mc->shift, max);
+ val = reg_val;
+ if (val < 0) {
+ ret = val;
+ goto get_volsw_done;
+ }
+
+ val = soc_mixer_reg_to_ctl(mc, reg_val, mask, mc->shift, max, clamp);
ucontrol->value.integer.value[0] = val;
if (snd_soc_volsw_is_stereo(mc)) {
if (mc->reg == mc->rreg) {
- val = soc_mixer_reg_to_ctl(mc, reg_val, mask, mc->rshift, max);
+ val = soc_mixer_reg_to_ctl(mc, reg_val, mask, mc->rshift,
+ max, clamp);
} else {
reg_val = snd_soc_component_read(component, mc->rreg);
- val = soc_mixer_reg_to_ctl(mc, reg_val, mask, mc->shift, max);
+ val = reg_val;
+ if (val < 0) {
+ ret = val;
+ goto get_volsw_done;
+ }
+ val = soc_mixer_reg_to_ctl(mc, reg_val, mask,
+ mc->shift, max, clamp);
}
ucontrol->value.integer.value[1] = val;
}
- return 0;
+get_volsw_done:
+ return ret;
}
/**
@@ -330,7 +349,8 @@ int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
(struct soc_mixer_control *)kcontrol->private_value;
unsigned int mask = soc_mixer_mask(mc);
- return soc_get_volsw(kcontrol, ucontrol, mc, mask, mc->max - mc->min);
+ return soc_get_volsw(kcontrol, ucontrol, mc, mask,
+ mc->max - mc->min, false);
}
EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
@@ -372,7 +392,7 @@ int snd_soc_get_volsw_sx(struct snd_kcontrol *kcontrol,
(struct soc_mixer_control *)kcontrol->private_value;
unsigned int mask = soc_mixer_sx_mask(mc);
- return soc_get_volsw(kcontrol, ucontrol, mc, mask, mc->max);
+ return soc_get_volsw(kcontrol, ucontrol, mc, mask, mc->max, false);
}
EXPORT_SYMBOL_GPL(snd_soc_get_volsw_sx);
--
2.45.2
next reply other threads:[~2025-09-11 15:57 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-11 15:56 Niranjan H Y [this message]
2025-09-11 15:56 ` [PATCH v4 2/6] ASoC: ops: add snd_soc_get_volsw_clamped Niranjan H Y
2025-09-11 16:04 ` Mark Brown
2025-09-11 17:21 ` [EXTERNAL] " Holalu Yogendra, Niranjan
2025-09-11 18:32 ` Mark Brown
2025-09-11 15:57 ` [PATCH v4 3/6] ASoc: tas2783A: Add soundwire based codec driver Niranjan H Y
2025-09-11 15:57 ` [PATCH v4 4/6] ASoc: tas2783A: machine driver amp utility for TI devices Niranjan H Y
2025-09-11 15:57 ` [PATCH v4 5/6] ASoc: tas2783A: add machine driver changes Niranjan H Y
2025-09-11 15:57 ` [PATCH v4 6/6] tas2783A: Add acpi match changes for Intel MTL Niranjan H Y
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250911155704.2236-1-niranjan.hy@ti.com \
--to=niranjan.hy@ti.com \
--cc=alsa-devel@alsa-project.org \
--cc=baojun.xu@ti.com \
--cc=broonie@kernel.org \
--cc=cezary.rojewski@intel.com \
--cc=kai.vehmanen@linux.intel.com \
--cc=lgirdwood@gmail.com \
--cc=linux-sound@vger.kernel.org \
--cc=navada@ti.com \
--cc=perex@perex.cz \
--cc=peter.ujfalusi@linux.intel.com \
--cc=pierre-louis.bossart@linux.dev \
--cc=ranjani.sridharan@linux.intel.com \
--cc=shenghao-ding@ti.com \
--cc=tiwai@suse.com \
--cc=v-hampiholi@ti.com \
--cc=yung-chuan.liao@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox