public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ASoC: tegra: Add Mixer Fade controls
@ 2026-04-22 10:29 Sheetal
  2026-04-22 11:26 ` Mark Brown
  0 siblings, 1 reply; 2+ messages in thread
From: Sheetal @ 2026-04-22 10:29 UTC (permalink / raw)
  To: Liam Girdwood, Mark Brown
  Cc: Jaroslav Kysela, Takashi Iwai, Thierry Reding, Jonathan Hunter,
	Sameer Pujar, Mohan Kumar, Kuninori Morimoto, linux-sound,
	linux-tegra, linux-kernel, Sheetal

Add fade controls for the Tegra mixer to allow the user to configure
the gain and duration for each mixer stream. The mixer supports up to
10 input streams (RX1 through RX10), each with independently
configurable target gain and fade duration. The duration is specified
in terms of the total the number of samples and this is configured via
the N3 parameter in the mixer. The total duration (N3 parameter) used
in gain curve calculations of the form n/N3 or (N3-n)/N3, where n is
the current sample position.

The fade control accepts a comma-separated list of triplets:
  amixer -c <card> cset name="MIXER1 Fade" \
    "<id1>,<gain1>,<duration1>[,<id2>,<gain2>,<duration2>,...]"
  id       - mixer input RX stream index (1 to 10)
  gain     - target gain level
  duration - fade duration in samples (N3 parameter)

 Example: fade 2 streams simultaneously:
   amixer -c <card> cset name="MIXER1 Fade" "1,12000,1024,2,15000,2048"
   Stream 1: fade to gain 12000 over 1024 samples
   Stream 2: fade to gain 15000 over 2048 samples

The fade status reports per-stream state for all 10 RX inputs:
  amixer -c <card> cget name="MIXER1 Fade Status"
  Output: "0,1,-1,-1,-1,-1,-1,-1,-1,-1"

  -1 - inactive (no fade configured)
   0 - active (fade in progress)
   1 - complete (target gain reached)

 Once fading completes, the status resets to inactive for that stream.

Signed-off-by: Sheetal <sheetal@nvidia.com>
---
 sound/soc/tegra/tegra210_mixer.c | 179 ++++++++++++++++++++++++++++++-
 sound/soc/tegra/tegra210_mixer.h |  16 ++-
 2 files changed, 190 insertions(+), 5 deletions(-)

diff --git a/sound/soc/tegra/tegra210_mixer.c b/sound/soc/tegra/tegra210_mixer.c
index ce44117a0b9c..7990038f005f 100644
--- a/sound/soc/tegra/tegra210_mixer.c
+++ b/sound/soc/tegra/tegra210_mixer.c
@@ -151,10 +151,17 @@ static int tegra210_mixer_configure_gain(struct snd_soc_component *cmpnt,
 	for (i = 0; i < NUM_DURATION_PARMS; i++) {
 		int val;
 
-		if (instant_gain)
+		if (instant_gain) {
 			val = 1;
-		else
-			val = gain_params.duration[i];
+		} else {
+			if (i == DURATION_N3_ID)
+				val = mixer->duration[id];
+			else if (i == DURATION_INV_N3_ID)
+				val = (BIT(31) * BIT(TEGRA210_MIXER_PRESCALAR)) /
+					mixer->duration[id];
+			else
+				val = gain_params.duration[i];
+		}
 
 		err = tegra210_mixer_write_ram(mixer,
 					       REG_DURATION_PARAM(reg, i),
@@ -173,6 +180,124 @@ static int tegra210_mixer_configure_gain(struct snd_soc_component *cmpnt,
 	return err;
 }
 
+static int tegra210_mixer_put_fade(struct snd_kcontrol *kcontrol,
+				   struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_component *cmpnt = snd_kcontrol_chip(kcontrol);
+	struct tegra210_mixer *mixer = snd_soc_component_get_drvdata(cmpnt);
+	u32 rx_id, rx_gain, rx_duration;
+	int id, base, err = 0;
+
+	/* Process array of RX stream parameters (id, gain, duration) */
+	for (id = 0; id < TEGRA210_MIXER_RX_MAX; id++) {
+		base = 3 * id;
+		rx_id = ucontrol->value.integer.value[base];
+		rx_gain = ucontrol->value.integer.value[base + 1];
+		rx_duration = ucontrol->value.integer.value[base + 2];
+
+		if (rx_id > TEGRA210_MIXER_RX_MAX) {
+			dev_err(cmpnt->dev, "Invalid mixer rx index %d\n", rx_id);
+			return -EINVAL;
+		}
+
+		/* Array ends here, user isn't required to pass all RX configurations */
+		if (rx_id == 0 && rx_gain == 0 && rx_duration == 0)
+			break;
+
+		/* Convert 1-based rx_id to 0-based array index */
+		rx_id = rx_id - 1;
+
+		/* Only update if values are different */
+		if (mixer->duration[rx_id] != rx_duration ||
+		    mixer->gain_value[rx_id] != rx_gain) {
+			err = tegra210_mixer_configure_gain(cmpnt, rx_id, false);
+			if (err) {
+				dev_err(cmpnt->dev,
+					"Failed to configure fade for channel %d\n",
+					rx_id);
+				return err;
+			}
+			err = 1;
+			mixer->duration[rx_id] = rx_duration;
+			mixer->gain_value[rx_id] = rx_gain;
+		}
+
+		if (!mixer->in_fade[rx_id]) {
+			pm_runtime_get_sync(cmpnt->dev);
+			err = regmap_update_bits(mixer->regmap,
+						 MIXER_REG(TEGRA210_MIXER_RX1_CTRL, rx_id),
+						 TEGRA210_MIXER_SAMPLE_COUNT_ENABLE,
+						 TEGRA210_MIXER_SAMPLE_COUNT_ENABLE);
+			pm_runtime_put(cmpnt->dev);
+			if (err) {
+				dev_err(cmpnt->dev,
+					"Failed to enable sample count for channel %d\n", rx_id);
+				return err;
+			}
+			err = 1;
+			mixer->in_fade[rx_id] = true;
+		}
+	}
+
+	return err;
+}
+
+static int tegra210_mixer_get_fade(struct snd_kcontrol *kcontrol,
+				   struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_component *cmpnt = snd_kcontrol_chip(kcontrol);
+	struct tegra210_mixer *mixer = snd_soc_component_get_drvdata(cmpnt);
+	u32 id, base, index = 0;
+
+	for (id = 0; id < TEGRA210_MIXER_RX_MAX; id++) {
+		if (mixer->in_fade[id]) {
+			base = 3 * index;
+			ucontrol->value.integer.value[base] = id + 1;
+			ucontrol->value.integer.value[base + 1] = mixer->gain_value[id];
+			ucontrol->value.integer.value[base + 2] = mixer->duration[id];
+			index++;
+		}
+	}
+
+	return 0;
+}
+
+static int tegra210_mixer_get_fade_status(struct snd_kcontrol *kcontrol,
+					  struct snd_ctl_elem_value *ucontrol)
+{
+	struct snd_soc_component *cmpnt = snd_kcontrol_chip(kcontrol);
+	struct tegra210_mixer *mixer = snd_soc_component_get_drvdata(cmpnt);
+	u32 count;
+	int id;
+
+	pm_runtime_get_sync(cmpnt->dev);
+
+	for (id = 0; id < TEGRA210_MIXER_RX_MAX; id++) {
+		if (!mixer->in_fade[id]) {
+			ucontrol->value.integer.value[id] = TEGRA210_MIXER_FADE_IDLE;
+			continue;
+		}
+
+		regmap_read(mixer->regmap,
+			    MIXER_REG(TEGRA210_MIXER_RX1_SAMPLE_COUNT, id),
+			    &count);
+
+		if (count >= mixer->duration[id]) {
+			ucontrol->value.integer.value[id] = TEGRA210_MIXER_FADE_COMPLETE;
+			regmap_update_bits(mixer->regmap,
+					   MIXER_REG(TEGRA210_MIXER_RX1_CTRL, id),
+					   TEGRA210_MIXER_SAMPLE_COUNT_ENABLE, 0);
+			mixer->in_fade[id] = false;
+		} else {
+			ucontrol->value.integer.value[id] = TEGRA210_MIXER_FADE_ACTIVE;
+		}
+	}
+
+	pm_runtime_put(cmpnt->dev);
+
+	return 0;
+}
+
 static int tegra210_mixer_get_gain(struct snd_kcontrol *kcontrol,
 				   struct snd_ctl_elem_value *ucontrol)
 {
@@ -396,6 +521,44 @@ ADDER_CTRL_DECL(adder3, TEGRA210_MIXER_TX3_ADDER_CONFIG);
 ADDER_CTRL_DECL(adder4, TEGRA210_MIXER_TX4_ADDER_CONFIG);
 ADDER_CTRL_DECL(adder5, TEGRA210_MIXER_TX5_ADDER_CONFIG);
 
+static int tegra210_mixer_param_info(struct snd_kcontrol *kcontrol,
+				     struct snd_ctl_elem_info *uinfo)
+{
+	struct soc_bytes *params = (void *)kcontrol->private_value;
+
+	if (strstr(kcontrol->id.name, "Status")) {
+		params->num_regs = 10;
+		uinfo->value.integer.min = TEGRA210_MIXER_FADE_IDLE;
+		uinfo->value.integer.max = TEGRA210_MIXER_FADE_COMPLETE;
+	} else {
+		params->num_regs = 30;
+		uinfo->value.integer.min = 0;
+		uinfo->value.integer.max = U32_MAX;
+	}
+
+	/* Set control info */
+	uinfo->type = params->mask;
+	uinfo->count = params->num_regs;
+
+	return 0;
+}
+
+#define MIXER_FADE_CTRL(xname, xbase, xnum_regs, xget, xput, xaccess, xmask) \
+{ \
+	.iface = SNDRV_CTL_ELEM_IFACE_MIXER, \
+	.info =  tegra210_mixer_param_info, \
+	.name = xname, \
+	.access = xaccess, \
+	.get = xget, \
+	.put = xput, \
+	.private_value = (unsigned long)&(struct soc_bytes)	\
+	{ \
+		.base = xbase, \
+		.num_regs = xnum_regs, \
+		.mask = xmask \
+	} \
+},
+
 #define GAIN_CTRL(id)	\
 	SOC_SINGLE_EXT("RX" #id " Gain Volume",			\
 		       MIXER_GAIN_CFG_RAM_ADDR((id) - 1), 0,	\
@@ -418,6 +581,11 @@ static const struct snd_kcontrol_new tegra210_mixer_gain_ctls[] = {
 	GAIN_CTRL(8)
 	GAIN_CTRL(9)
 	GAIN_CTRL(10)
+
+MIXER_FADE_CTRL("Fade", 0x0, 30, tegra210_mixer_get_fade, tegra210_mixer_put_fade,
+		SNDRV_CTL_ELEM_ACCESS_READWRITE, SNDRV_CTL_ELEM_TYPE_INTEGER)
+MIXER_FADE_CTRL("Fade Status", 0x0, 10, tegra210_mixer_get_fade_status, NULL,
+		SNDRV_CTL_ELEM_ACCESS_READ, SNDRV_CTL_ELEM_TYPE_INTEGER)
 };
 
 static const struct snd_soc_dapm_widget tegra210_mixer_widgets[] = {
@@ -579,6 +747,7 @@ static bool tegra210_mixer_volatile_reg(struct device *dev,
 	case TEGRA210_MIXER_GAIN_CFG_RAM_DATA:
 	case TEGRA210_MIXER_PEAKM_RAM_CTRL:
 	case TEGRA210_MIXER_PEAKM_RAM_DATA:
+	case TEGRA210_MIXER_RX1_SAMPLE_COUNT:
 		return true;
 	default:
 		return false;
@@ -632,8 +801,10 @@ static int tegra210_mixer_platform_probe(struct platform_device *pdev)
 	dev_set_drvdata(dev, mixer);
 
 	/* Use default gain value for all MIXER inputs */
-	for (i = 0; i < TEGRA210_MIXER_RX_MAX; i++)
+	for (i = 0; i < TEGRA210_MIXER_RX_MAX; i++) {
 		mixer->gain_value[i] = gain_params.gain_value;
+		mixer->duration[i] = gain_params.duration[DURATION_N3_ID];
+	}
 
 	regs = devm_platform_ioremap_resource(pdev, 0);
 	if (IS_ERR(regs))
diff --git a/sound/soc/tegra/tegra210_mixer.h b/sound/soc/tegra/tegra210_mixer.h
index a330530fbc61..da16f0002bfd 100644
--- a/sound/soc/tegra/tegra210_mixer.h
+++ b/sound/soc/tegra/tegra210_mixer.h
@@ -79,12 +79,24 @@
 #define TEGRA210_MIXER_RX_LIMIT		(TEGRA210_MIXER_RX_MAX * TEGRA210_MIXER_REG_STRIDE)
 #define TEGRA210_MIXER_TX_MAX		5
 #define TEGRA210_MIXER_TX_LIMIT		(TEGRA210_MIXER_RX_LIMIT + (TEGRA210_MIXER_TX_MAX * TEGRA210_MIXER_REG_STRIDE))
+#define TEGRA210_MIXER_SAMPLE_COUNT_SHIFT	24
+#define TEGRA210_MIXER_SAMPLE_COUNT_ENABLE	BIT(TEGRA210_MIXER_SAMPLE_COUNT_SHIFT)
 
 #define REG_CFG_DONE_TRIGGER	0xf
 #define VAL_CFG_DONE_TRIGGER	0x1
 
 #define NUM_GAIN_POLY_COEFFS 9
-#define NUM_DURATION_PARMS 4
+
+#define TEGRA210_MIXER_PRESCALAR	    6
+#define TEGRA210_MIXER_FADE_IDLE	    (-1)
+#define TEGRA210_MIXER_FADE_ACTIVE	  0
+#define TEGRA210_MIXER_FADE_COMPLETE	1
+
+enum {
+	DURATION_N3_ID = 2,
+	DURATION_INV_N3_ID,
+	NUM_DURATION_PARMS,
+};
 
 struct tegra210_mixer_gain_params {
 	int poly_coeff[NUM_GAIN_POLY_COEFFS];
@@ -94,6 +106,8 @@ struct tegra210_mixer_gain_params {
 
 struct tegra210_mixer {
 	int gain_value[TEGRA210_MIXER_RX_MAX];
+	u32 duration[TEGRA210_MIXER_RX_MAX];
+	bool in_fade[TEGRA210_MIXER_RX_MAX];
 	struct regmap *regmap;
 };
 
-- 
2.17.1


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

* Re: [PATCH] ASoC: tegra: Add Mixer Fade controls
  2026-04-22 10:29 [PATCH] ASoC: tegra: Add Mixer Fade controls Sheetal
@ 2026-04-22 11:26 ` Mark Brown
  0 siblings, 0 replies; 2+ messages in thread
From: Mark Brown @ 2026-04-22 11:26 UTC (permalink / raw)
  To: Sheetal
  Cc: Liam Girdwood, Jaroslav Kysela, Takashi Iwai, Thierry Reding,
	Jonathan Hunter, Sameer Pujar, Mohan Kumar, Kuninori Morimoto,
	linux-sound, linux-tegra, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 2112 bytes --]

On Wed, Apr 22, 2026 at 10:29:57AM +0000, Sheetal wrote:
> Add fade controls for the Tegra mixer to allow the user to configure
> the gain and duration for each mixer stream. The mixer supports up to
> 10 input streams (RX1 through RX10), each with independently

>   amixer -c <card> cset name="MIXER1 Fade" \
>     "<id1>,<gain1>,<duration1>[,<id2>,<gain2>,<duration2>,...]"
>   id       - mixer input RX stream index (1 to 10)
>   gain     - target gain level
>   duration - fade duration in samples (N3 parameter)

This would be more idomatically represented as 10 pairs of controls, one
with the gain and one with the duration.  Possibly also with an
additional strobe control which actually makes the changes take effect
if there's a need to do a block of them at once.

> +static int tegra210_mixer_put_fade(struct snd_kcontrol *kcontrol,
> +				   struct snd_ctl_elem_value *ucontrol)
> +{

> +	struct snd_soc_component *cmpnt = snd_kcontrol_chip(kcontrol);
> +	struct tegra210_mixer *mixer = snd_soc_component_get_drvdata(cmpnt);
> +	u32 rx_id, rx_gain, rx_duration;
> +	int id, base, err = 0;
> +
> +	/* Process array of RX stream parameters (id, gain, duration) */
> +	for (id = 0; id < TEGRA210_MIXER_RX_MAX; id++) {
> +		base = 3 * id;
> +		rx_id = ucontrol->value.integer.value[base];
> +		rx_gain = ucontrol->value.integer.value[base + 1];
> +		rx_duration = ucontrol->value.integer.value[base + 2];

I'm not seeing much validation of these, for example it looks like you
can set a duration of 0 quite happily which seems to be a problem as we
divide by the duration when configuring.

> +               /* Only update if values are different */
> +               if (mixer->duration[rx_id] != rx_duration ||
 +                   mixer->gain_value[rx_id] != rx_gain) {
> +                       err = tegra210_mixer_configure_gain(cmpnt, rx_id, false);

...

> +                       mixer->duration[rx_id] = rx_duration;
> +                       mixer->gain_value[rx_id] = rx_gain;

Doesn't this mean that the values that get configured are the ones that
were previously set, not the new ones?

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

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

end of thread, other threads:[~2026-04-22 11:26 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-04-22 10:29 [PATCH] ASoC: tegra: Add Mixer Fade controls Sheetal
2026-04-22 11:26 ` Mark Brown

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