Skip to contentSkip to conversation

ASoC: ops: improve snd_soc_get_volsw

PATCH Superseded Niranjan H Y 2 participants 9 messages Sep 11, 2025

Subsystems (4)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/6 ASoC: ops: improve snd_soc_get_volsw

* add error handling in case register read fails
* add support for clamping the values if the register value read is greater than max value

---
 sound/soc/soc-ops.c | 38 +++++++++++++++++++++++++++++---------
 1 file changed, 29 insertions(+), 9 deletions(-)
sound/soc/soc-ops.c +29 -9
index a629e0eac..59e91741b 100644
@@ -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);
 
Signature · 1 line
2.45.2
#1 lore raw

PATCH v4 2/6 ASoC: ops: add snd_soc_get_volsw_clamped

Add API snd_soc_get_volsw_clamped to read
the volume from device. This similar to
existing API snd_soc_get_volsw. But has
additional step to clamp the value to max value
if the register value is larger than max value.

---
 include/sound/soc.h |  2 ++
 sound/soc/soc-ops.c | 22 ++++++++++++++++++++++
 2 files changed, 24 insertions(+)
include/sound/soc.h +2 -0
index 1fffef311..34a490ece 100644
@@ -553,6 +553,8 @@ int snd_soc_info_volsw_sx(struct snd_kcontrol *kcontrol,
 #define snd_soc_info_bool_ext		snd_ctl_boolean_mono_info
 int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
 	struct snd_ctl_elem_value *ucontrol);
+int snd_soc_get_volsw_clamped(struct snd_kcontrol *kcontrol,
+	struct snd_ctl_elem_value *ucontrol);
 int snd_soc_put_volsw(struct snd_kcontrol *kcontrol,
 	struct snd_ctl_elem_value *ucontrol);
 #define snd_soc_get_volsw_2r snd_soc_get_volsw
sound/soc/soc-ops.c +22 -0
index 59e91741b..4d1ee5cd2 100644
@@ -354,6 +354,28 @@ int snd_soc_get_volsw(struct snd_kcontrol *kcontrol,
 }
 EXPORT_SYMBOL_GPL(snd_soc_get_volsw);
 
+/**
+ * snd_soc_get_volsw_clamped - single mixer get callback with range
+ * @kcontrol: mixer control
+ * @ucontrol: control element information
+ *
+ * Similar to snd_soc_get_volsw, with additional operation to
+ * clamp the register value to its maximum.
+ *
+ * Returns 0 for success.
+ */
+int snd_soc_get_volsw_clamped(struct snd_kcontrol *kcontrol,
+			      struct snd_ctl_elem_value *ucontrol)
+{
+	struct soc_mixer_control *mc =
+		(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, true);
+}
+EXPORT_SYMBOL_GPL(snd_soc_get_volsw_clamped);
+
 /**
  * snd_soc_put_volsw - single mixer put callback with range
  * @kcontrol: mixer control
Signature · 1 line
2.45.2
maintainer
#2 lore raw

Re: [PATCH v4 2/6] ASoC: ops: add snd_soc_get_volsw_clamped

Quoted 5 lines · Niranjan H Y

Add API snd_soc_get_volsw_clamped to read
the volume from device. This similar to
existing API snd_soc_get_volsw. But has
additional step to clamp the value to max value
if the register value is larger than max value.

Why is this a separate API, what is the situation where we would want to report an invalid value from a control? I was thinking of just adding this to the existing operations rather than adding separate ones that need to be explicitly set.

#3 lore raw

RE: [EXTERNAL] Re: [PATCH v4 2/6] ASoC: ops: add snd_soc_get_volsw_clamped

Quoted 13 lines

From: Mark Brown <[email protected]>
Sent: Thursday, September 11, 2025 9:34 PM
To: Holalu Yogendra, Niranjan <[email protected]>
Subject: Re: [PATCH v4 2/6] ASoC: ops: add snd_soc_get_volsw_clamped

Quoted 5 lines · Niranjan H Y

Add API snd_soc_get_volsw_clamped to read
the volume from device. This similar to
existing API snd_soc_get_volsw. But has
additional step to clamp the value to max value
if the register value is larger than max value.

Quoted 4 lines

Why is this a separate API, what is the situation where we would want to report an invalid value from a control? I was thinking of just adding this to the existing operations rather than adding separate ones that need to be explicitly set.

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 some issue (like IO) and, for some reason if register value was modified to have the value greater than mc->max value, the volume is report erroneously.
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.

Thanks,
Niranjan H Y

maintainer
#4 lore raw

Re: [EXTERNAL] Re: [PATCH v4 2/6] ASoC: ops: add snd_soc_get_volsw_clamped

Quoted 10 lines · Niranjan
Quoted 6 lines

From: Mark Brown <[email protected]>

Why is this a separate API, what is the situation where we would want to report an invalid value from a control? I was thinking of just adding this to the existing operations rather than adding separate ones that need to be explicitly set.

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?

Quoted 4 lines

some issue (like IO) and, for some reason if register value was modified to have the value greater than mc->max value, the volume is report erroneously.
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.

#5 lore raw

PATCH v4 3/6 ASoc: tas2783A: Add soundwire based codec driver

In reply to Niranjan H Y

TAS2783 is mono digital input class-D Smart Amplifier based on MIPI Alliance Soundwire interface. The driver supports loading the algorithm coefficients for one or more tas2783 chips.

---
v4:
- use snd_soc_put_volsw and snd_soc_get_volsw for volume handling instead of manual handling.
- move the stub implementation to header file.

v3:
- fix compilation issue for ARCH=hexagon clang compiler
- Kconfig - use depends on for SOUNDWIRE and EFI instead of select
- add REGMAP_SOUNDWIRE_MBQ

v2:
- append Volume to amp gain mixer control
- remove unwanted debug log

---
 sound/soc/codecs/Kconfig       |   14 +
 sound/soc/codecs/Makefile      |    2 +
 sound/soc/codecs/tas2783-sdw.c | 1331 ++++++++++++++++++++++++++++++++
 sound/soc/codecs/tas2783.h     |  110 +++
 4 files changed, 1457 insertions(+)
 create mode 100644 sound/soc/codecs/tas2783-sdw.c
 create mode 100644 sound/soc/codecs/tas2783.h
sound/soc/codecs/Kconfig +14 -0
index b8d58d2fe..a7d25ab7a 100644
@@ -266,6 +266,7 @@ config SND_SOC_ALL_CODECS
 	imply SND_SOC_TAS2770
 	imply SND_SOC_TAS2780
 	imply SND_SOC_TAS2781_I2C
+	imply SND_SOC_TAS2783_SDW
 	imply SND_SOC_TAS5086
 	imply SND_SOC_TAS571X
 	imply SND_SOC_TAS5720
@@ -2074,6 +2075,19 @@ config SND_SOC_TAS2781_I2C
 	  algo coefficient setting, for one, two or even multiple TAS2781
 	  chips.
 
+config SND_SOC_TAS2783_SDW
+	tristate "Texas Instruments TAS2783 speaker amplifier (sdw)"
+	depends on SOUNDWIRE
+	depends on EFI
+	select REGMAP_SOUNDWIRE
+	select REGMAP_SOUNDWIRE_MBQ
+	select CRC32
+	help
+	  Enable support for Texas Instruments TAS2783A Digital input
+	  mono Class-D and DSP-inside audio power amplifiers. TAS2783
+	  driver implements a flexible and configurable algorithm
+	  cofficient setting, for one, two or multiple TAS2783 chips.
+
 config SND_SOC_TAS5086
 	tristate "Texas Instruments TAS5086 speaker amplifier"
 	depends on I2C
sound/soc/codecs/Makefile +2 -0
index a476d6c45..1920fa17b 100644
@@ -316,6 +316,7 @@ snd-soc-tas2781-comlib-y := tas2781-comlib.o
 snd-soc-tas2781-comlib-i2c-y := tas2781-comlib-i2c.o
 snd-soc-tas2781-fmwlib-y := tas2781-fmwlib.o
 snd-soc-tas2781-i2c-y := tas2781-i2c.o
+snd-soc-tas2783-sdw-y := tas2783-sdw.o
 snd-soc-tfa9879-y := tfa9879.o
 snd-soc-tfa989x-y := tfa989x.o
 snd-soc-tlv320adc3xxx-y := tlv320adc3xxx.o
@@ -732,6 +733,7 @@ obj-$(CONFIG_SND_SOC_TAS2781_COMLIB)	+= snd-soc-tas2781-comlib.o
 obj-$(CONFIG_SND_SOC_TAS2781_COMLIB_I2C)	+= snd-soc-tas2781-comlib-i2c.o
 obj-$(CONFIG_SND_SOC_TAS2781_FMWLIB)	+= snd-soc-tas2781-fmwlib.o
 obj-$(CONFIG_SND_SOC_TAS2781_I2C)	+= snd-soc-tas2781-i2c.o
+obj-$(CONFIG_SND_SOC_TAS2783_SDW)	+= snd-soc-tas2783-sdw.o
 obj-$(CONFIG_SND_SOC_TAS5086)	+= snd-soc-tas5086.o
 obj-$(CONFIG_SND_SOC_TAS571X)	+= snd-soc-tas571x.o
 obj-$(CONFIG_SND_SOC_TAS5720)	+= snd-soc-tas5720.o
sound/soc/codecs/tas2783-sdw.c new file +1331 -0

1,331 lines not on this page · raw message

sound/soc/codecs/tas2783.h new file +110 -0

110 lines not on this page · raw message

Signature · 1 line
2.45.2
#6 lore raw

PATCH v4 4/6 ASoc: tas2783A: machine driver amp utility for TI devices

In reply to Niranjan H Y

Machine driver amp utility file to initialize and support
multiple tas2783a devices are added.

---
 include/sound/soc_sdw_utils.h        |  8 +++
 sound/soc/sdw_utils/Makefile         |  3 +-
 sound/soc/sdw_utils/soc_sdw_ti_amp.c | 93 ++++++++++++++++++++++++++++
 3 files changed, 103 insertions(+), 1 deletion(-)
 create mode 100644 sound/soc/sdw_utils/soc_sdw_ti_amp.c
include/sound/soc_sdw_utils.h +8 -0
index 6049a5d0c..3c5e9b2af 100644
@@ -248,5 +248,13 @@ int asoc_sdw_cs42l43_spk_rtd_init(struct snd_soc_pcm_runtime *rtd, struct snd_so
 int asoc_sdw_cs42l43_dmic_rtd_init(struct snd_soc_pcm_runtime *rtd, struct snd_soc_dai *dai);
 int asoc_sdw_cs_spk_rtd_init(struct snd_soc_pcm_runtime *rtd, struct snd_soc_dai *dai);
 int asoc_sdw_maxim_spk_rtd_init(struct snd_soc_pcm_runtime *rtd, struct snd_soc_dai *dai);
+/* TI */
+int asoc_sdw_ti_amp_init(struct snd_soc_card *card,
+			 struct snd_soc_dai_link *dai_links,
+			 struct asoc_sdw_codec_info *info,
+			 bool playback);
+int asoc_sdw_ti_spk_rtd_init(struct snd_soc_pcm_runtime *rtd, struct snd_soc_dai *dai);
+int asoc_sdw_ti_amp_initial_settings(struct snd_soc_card *card,
+				     const char *name_prefix);
 
 #endif
sound/soc/sdw_utils/Makefile +2 -1
index daf019113..a87c53e1a 100644
@@ -6,5 +6,6 @@ snd-soc-sdw-utils-y := soc_sdw_utils.o soc_sdw_dmic.o soc_sdw_rt_dmic.o \
 		       soc_sdw_bridge_cs35l56.o 			\
 		       soc_sdw_cs42l42.o soc_sdw_cs42l43.o 		\
 		       soc_sdw_cs_amp.o					\
-		       soc_sdw_maxim.o
+		       soc_sdw_maxim.o \
+		       soc_sdw_ti_amp.o
 obj-$(CONFIG_SND_SOC_SDW_UTILS) += snd-soc-sdw-utils.o
sound/soc/sdw_utils/soc_sdw_ti_amp.c new file +93 -0

93 lines not on this page · raw message

Signature · 1 line
2.45.2
#7 lore raw

PATCH v4 5/6 ASoc: tas2783A: add machine driver changes

In reply to Niranjan H Y

Add tas2783-codec for codec_info

---
 sound/soc/sdw_utils/soc_sdw_utils.c | 38 +++++++++++++++++++++--------
 1 file changed, 28 insertions(+), 10 deletions(-)
sound/soc/sdw_utils/soc_sdw_utils.c +28 -10
index 1580331cd..56c72ef27 100644
@@ -35,12 +35,12 @@ static const struct snd_kcontrol_new generic_spk_controls[] = {
 	SOC_DAPM_PIN_SWITCH("Speaker"),
 };
 
-static const struct snd_soc_dapm_widget maxim_widgets[] = {
+static const struct snd_soc_dapm_widget lr_spk_widgets[] = {
 	SND_SOC_DAPM_SPK("Left Spk", NULL),
 	SND_SOC_DAPM_SPK("Right Spk", NULL),
 };
 
-static const struct snd_kcontrol_new maxim_controls[] = {
+static const struct snd_kcontrol_new lr_spk_controls[] = {
 	SOC_DAPM_PIN_SWITCH("Left Spk"),
 	SOC_DAPM_PIN_SWITCH("Right Spk"),
 };
@@ -58,6 +58,24 @@ static const struct snd_kcontrol_new rt700_controls[] = {
 };
 
 struct asoc_sdw_codec_info codec_info_list[] = {
+	{
+		.part_id = 0x0000, /* TAS2783A */
+		.dais = {
+			{
+				.direction = {true, true},
+				.dai_name = "tas2783-codec",
+				.dai_type = SOC_SDW_DAI_TYPE_AMP,
+				.dailink = {SOC_SDW_AMP_OUT_DAI_ID, SOC_SDW_AMP_IN_DAI_ID},
+				.init = asoc_sdw_ti_amp_init,
+				.rtd_init = asoc_sdw_ti_spk_rtd_init,
+				.controls = lr_spk_controls,
+				.num_controls = ARRAY_SIZE(lr_spk_controls),
+				.widgets = lr_spk_widgets,
+				.num_widgets = ARRAY_SIZE(lr_spk_widgets),
+			},
+		},
+		.dai_num = 1,
+	},
 	{
 		.part_id = 0x700,
 		.dais = {
@@ -450,10 +468,10 @@ struct asoc_sdw_codec_info codec_info_list[] = {
 				.dailink = {SOC_SDW_AMP_OUT_DAI_ID, SOC_SDW_AMP_IN_DAI_ID},
 				.init = asoc_sdw_maxim_init,
 				.rtd_init = asoc_sdw_maxim_spk_rtd_init,
-				.controls = maxim_controls,
-				.num_controls = ARRAY_SIZE(maxim_controls),
-				.widgets = maxim_widgets,
-				.num_widgets = ARRAY_SIZE(maxim_widgets),
+				.controls = lr_spk_controls,
+				.num_controls = ARRAY_SIZE(lr_spk_controls),
+				.widgets = lr_spk_widgets,
+				.num_widgets = ARRAY_SIZE(lr_spk_widgets),
 			},
 		},
 		.dai_num = 1,
@@ -469,10 +487,10 @@ struct asoc_sdw_codec_info codec_info_list[] = {
 				.dailink = {SOC_SDW_AMP_OUT_DAI_ID, SOC_SDW_UNUSED_DAI_ID},
 				.init = asoc_sdw_maxim_init,
 				.rtd_init = asoc_sdw_maxim_spk_rtd_init,
-				.controls = maxim_controls,
-				.num_controls = ARRAY_SIZE(maxim_controls),
-				.widgets = maxim_widgets,
-				.num_widgets = ARRAY_SIZE(maxim_widgets),
+				.controls = lr_spk_controls,
+				.num_controls = ARRAY_SIZE(lr_spk_controls),
+				.widgets = lr_spk_widgets,
+				.num_widgets = ARRAY_SIZE(lr_spk_widgets),
 			},
 		},
 		.dai_num = 1,
Signature · 1 line
2.45.2
#8 lore raw

PATCH v4 6/6 tas2783A: Add acpi match changes for Intel MTL

In reply to Niranjan H Y

acpi match changes to support tas2783a on mtl
on link 0 for 2 devices

---
 .../intel/common/soc-acpi-intel-mtl-match.c   | 30 +++++++++++++++++++
 1 file changed, 30 insertions(+)
sound/soc/intel/common/soc-acpi-intel-mtl-match.c +30 -0
index 75dc8935a..ec9fd8486 100644
@@ -948,6 +948,30 @@ static const struct snd_soc_acpi_adr_device cs42l42_0_adr[] = {
 	}
 };
 
+static const struct snd_soc_acpi_adr_device tas2783_0_adr[] = {
+	{
+		.adr = 0x0000380102000001ull,
+		.num_endpoints = 1,
+		.endpoints = &spk_l_endpoint,
+		.name_prefix = "tas2783-1"
+	},
+	{
+		.adr = 0x0000390102000001ull,
+		.num_endpoints = 1,
+		.endpoints = &spk_r_endpoint,
+		.name_prefix = "tas2783-2"
+	}
+};
+
+static const struct snd_soc_acpi_link_adr tas2783_link0[] = {
+	{
+		.mask = BIT(0),
+		.num_adr = ARRAY_SIZE(tas2783_0_adr),
+		.adr_d = tas2783_0_adr,
+	},
+	{}
+};
+
 static const struct snd_soc_acpi_link_adr cs42l42_link0_max98363_link2[] = {
 	/* Expected order: jack -> amp */
 	{
@@ -1080,6 +1104,12 @@ struct snd_soc_acpi_mach snd_soc_acpi_intel_mtl_sdw_machines[] = {
 		.drv_name = "sof_sdw",
 		.sof_tplg_filename = "sof-mtl-rt715-rt711-rt1308-mono.tplg",
 	},
+	{
+		.link_mask = BIT(0),
+		.links = tas2783_link0,
+		.drv_name = "sof_sdw",
+		.sof_tplg_filename = "sof-mtl-tas2783.tplg",
+	},
 	{
 		.link_mask = GENMASK(3, 0),
 		.links = mtl_rt713_l0_rt1316_l12_rt1713_l3,
Signature · 1 line
2.45.2