Linux Sound subsystem development
 help / color / mirror / Atom feed
* Re: [PATCH v4 2/6] ASoC: ops: add snd_soc_get_volsw_clamped
@ 2025-09-12  8:35 Holalu Yogendra, Niranjan
  0 siblings, 0 replies; 3+ messages in thread
From: Holalu Yogendra, Niranjan @ 2025-09-12  8:35 UTC (permalink / raw)
  To: Mark Brown
  Cc: alsa-devel@alsa-project.org, linux-sound@vger.kernel.org,
	lgirdwood@gmail.com, 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 Kanyana, Mukund, Ding, Shenghao, Hampiholi, Vallabha,
	Xu, Baojun

> From: Mark Brown <broonie@kernel.org>
> Sent: Friday, September 12, 2025 12:02 AM
> Subject: Re: [EXTERNAL] Re: [PATCH v4 2/6] ASoC: ops: add
> snd_soc_get_volsw_clamped
> 
> On Thu, Sep 11, 2025 at 05:21:14PM +0000, Holalu Yogendra, Niranjan wrote:
> > > From: Mark Brown <broonie@kernel.org>
> 
> >   Since few of the drivers as still using the original API, and was not sure if
> > everyone wanted to clamp the register value, did not want to disturb the
> > original API snd_soc_get_volsw. Per my analysis, if register read fails due to
> In what situation would anything want to report an invalid value?
I am not sure if I fully follow. But in my case, for one of the cases,
valid amp volume is 0x0 to 0x14 with mask 0x1f - so if the register value read is
0x15 to 0x1f, it is invalid. But in the next patch, I will add the logic to clamp the value.

> > Are you thinking that this patch could be dropped and clamp the value to max in  
> > 'soc_get_volsw' (as in 1st patch) and use the same API ? Please suggest.
> Yes, everything should report valid values.  I/O errors should be
> propagated as errors.
I will update the next patch accordingly. 

Thanks 
Niranjan H Y

^ permalink raw reply	[flat|nested] 3+ messages in thread
* [PATCH v4 1/6] ASoC: ops: improve snd_soc_get_volsw
@ 2025-09-11 15:56 Niranjan H Y
  2025-09-11 15:56 ` [PATCH v4 2/6] ASoC: ops: add snd_soc_get_volsw_clamped Niranjan H Y
  0 siblings, 1 reply; 3+ messages in thread
From: Niranjan H Y @ 2025-09-11 15:56 UTC (permalink / raw)
  To: alsa-devel
  Cc: linux-sound, lgirdwood, broonie, perex, tiwai, yung-chuan.liao,
	cezary.rojewski, peter.ujfalusi, ranjani.sridharan, kai.vehmanen,
	pierre-louis.bossart, navada, shenghao-ding, v-hampiholi,
	baojun.xu, Niranjan H Y

* 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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-09-12  8:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-09-12  8:35 [PATCH v4 2/6] ASoC: ops: add snd_soc_get_volsw_clamped Holalu Yogendra, Niranjan
  -- strict thread matches above, loose matches on Subject: below --
2025-09-11 15:56 [PATCH v4 1/6] ASoC: ops: improve snd_soc_get_volsw Niranjan H Y
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox