Linux Sound subsystem development
 help / color / mirror / Atom feed
* [PATCH v1] ASoC: tegra: Add support for mono channel to Tegra20/Tegra30 I2S DAIs
@ 2026-11-23 10:02 Svyatoslav Ryhel
  2026-11-23 10:23 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Svyatoslav Ryhel @ 2026-11-23 10:02 UTC (permalink / raw)
  To: Liam Girdwood, Mark Brown, Jaroslav Kysela, Takashi Iwai,
	Thierry Reding, Jonathan Hunter, Svyatoslav Ryhel
  Cc: linux-sound, linux-tegra, linux-kernel

According to the TRM, both Tegra20 and Tegra30 I2S should be able to
support a mono channel for non-DSP formats, while the number of channels
for the DSP (FSYNC) format should remain restricted to a minimum of two
for Tegra30+.

Implement support for mono audio by adjusting the I2S clock and divider
calculation logic for both Tegra20 and Tegra30+.

Signed-off-by: Svyatoslav Ryhel <clamor95@gmail.com>
---
I2S clock calculations were adjusted to match those found in downstream
sources for Tegra20 up to Tegra124. This change was tested on the LG
Optimus Vu (P895), and sound was consistent with both mono and dual
channels, with no distortion.
---
 sound/soc/tegra/tegra20_i2s.c | 21 ++++++++++++++++-----
 sound/soc/tegra/tegra30_i2s.c | 31 +++++++++++++++++++++----------
 2 files changed, 37 insertions(+), 15 deletions(-)

diff --git a/sound/soc/tegra/tegra20_i2s.c b/sound/soc/tegra/tegra20_i2s.c
index 2978c9d6f4839..285923e6aeffa 100644
--- a/sound/soc/tegra/tegra20_i2s.c
+++ b/sound/soc/tegra/tegra20_i2s.c
@@ -144,7 +144,12 @@ static int tegra20_i2s_hw_params(struct snd_pcm_substream *substream,
 	struct device *dev = dai->dev;
 	struct tegra20_i2s *i2s = snd_soc_dai_get_drvdata(dai);
 	unsigned int mask, val;
-	int ret, sample_size, srate, i2sclock, bitcnt;
+	int ret, sample_size, srate, i2sclock, bitcnt, i2sclk_div, is_dsp;
+
+	is_dsp = regmap_test_bits(i2s->regmap, TEGRA20_I2S_CTRL,
+				  TEGRA20_I2S_CTRL_BIT_FORMAT_DSP);
+	if (is_dsp < 0)
+		return is_dsp;
 
 	mask = TEGRA20_I2S_CTRL_BIT_SIZE_MASK;
 	switch (params_format(params)) {
@@ -174,18 +179,24 @@ static int tegra20_i2s_hw_params(struct snd_pcm_substream *substream,
 	/* Final "* 2" required by Tegra hardware */
 	i2sclock = srate * params_channels(params) * sample_size * 2;
 
+	/* Additional "* 2" is needed for DSP mode */
+	if (is_dsp)
+		i2sclock *= 2;
+
 	ret = clk_set_rate(i2s->clk_i2s, i2sclock);
 	if (ret) {
 		dev_err(dev, "Can't set I2S clock rate: %d\n", ret);
 		return ret;
 	}
 
-	bitcnt = (i2sclock / (2 * srate)) - 1;
+	i2sclk_div = is_dsp ? srate : params_channels(params) * srate;
+
+	bitcnt = (i2sclock / i2sclk_div) - 1;
 	if (bitcnt < 0 || bitcnt > TEGRA20_I2S_TIMING_CHANNEL_BIT_COUNT_MASK_US)
 		return -EINVAL;
 	val = bitcnt << TEGRA20_I2S_TIMING_CHANNEL_BIT_COUNT_SHIFT;
 
-	if (i2sclock % (2 * srate))
+	if (i2sclock % i2sclk_div)
 		val |= TEGRA20_I2S_TIMING_NON_SYM_ENABLE;
 
 	regmap_write(i2s->regmap, TEGRA20_I2S_TIMING, val);
@@ -330,14 +341,14 @@ static const struct snd_soc_dai_ops tegra20_i2s_dai_ops = {
 static const struct snd_soc_dai_driver tegra20_i2s_dai_template = {
 	.playback = {
 		.stream_name = "Playback",
-		.channels_min = 2,
+		.channels_min = 1,
 		.channels_max = 2,
 		.rates = SNDRV_PCM_RATE_8000_96000,
 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
 	},
 	.capture = {
 		.stream_name = "Capture",
-		.channels_min = 2,
+		.channels_min = 1,
 		.channels_max = 2,
 		.rates = SNDRV_PCM_RATE_8000_96000,
 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
diff --git a/sound/soc/tegra/tegra30_i2s.c b/sound/soc/tegra/tegra30_i2s.c
index e5e648d00621b..433a7eb873b43 100644
--- a/sound/soc/tegra/tegra30_i2s.c
+++ b/sound/soc/tegra/tegra30_i2s.c
@@ -137,10 +137,16 @@ static int tegra30_i2s_hw_params(struct snd_pcm_substream *substream,
 	struct device *dev = dai->dev;
 	struct tegra30_i2s *i2s = snd_soc_dai_get_drvdata(dai);
 	unsigned int mask, val, reg;
-	int ret, sample_size, srate, i2sclock, bitcnt;
+	int ret, sample_size, srate, i2sclock, bitcnt, i2sclk_div, is_dsp;
 	struct tegra30_ahub_cif_conf cif_conf;
 
-	if (params_channels(params) != 2)
+	is_dsp = regmap_test_bits(i2s->regmap, TEGRA30_I2S_CTRL,
+				  TEGRA30_I2S_CTRL_FRAME_FORMAT_FSYNC);
+	if (is_dsp < 0)
+		return is_dsp;
+
+	/* For DSP format min amount of channels is still two */
+	if (params_channels(params) != 2 && is_dsp)
 		return -EINVAL;
 
 	mask = TEGRA30_I2S_CTRL_BIT_SIZE_MASK;
@@ -157,10 +163,15 @@ static int tegra30_i2s_hw_params(struct snd_pcm_substream *substream,
 
 	srate = params_rate(params);
 
-	/* Final "* 2" required by Tegra hardware */
-	i2sclock = srate * params_channels(params) * sample_size * 2;
+	i2sclock = srate * params_channels(params) * sample_size;
+
+	/* Additional "* 4" is needed for DSP mode */
+	if (is_dsp)
+		i2sclock *= 4;
+
+	i2sclk_div = is_dsp ? srate : params_channels(params) * srate;
 
-	bitcnt = (i2sclock / (2 * srate)) - 1;
+	bitcnt = (i2sclock / i2sclk_div) - 1;
 	if (bitcnt < 0 || bitcnt > TEGRA30_I2S_TIMING_CHANNEL_BIT_COUNT_MASK_US)
 		return -EINVAL;
 
@@ -172,14 +183,14 @@ static int tegra30_i2s_hw_params(struct snd_pcm_substream *substream,
 
 	val = bitcnt << TEGRA30_I2S_TIMING_CHANNEL_BIT_COUNT_SHIFT;
 
-	if (i2sclock % (2 * srate))
+	if (i2sclock % i2sclk_div)
 		val |= TEGRA30_I2S_TIMING_NON_SYM_ENABLE;
 
 	regmap_write(i2s->regmap, TEGRA30_I2S_TIMING, val);
 
 	cif_conf.threshold = 0;
-	cif_conf.audio_channels = 2;
-	cif_conf.client_channels = 2;
+	cif_conf.audio_channels = params_channels(params);
+	cif_conf.client_channels = params_channels(params);
 	cif_conf.audio_bits = TEGRA30_AUDIOCIF_BITS_16;
 	cif_conf.client_bits = TEGRA30_AUDIOCIF_BITS_16;
 	cif_conf.expand = 0;
@@ -323,14 +334,14 @@ static const struct snd_soc_dai_ops tegra30_i2s_dai_ops = {
 static const struct snd_soc_dai_driver tegra30_i2s_dai_template = {
 	.playback = {
 		.stream_name = "Playback",
-		.channels_min = 2,
+		.channels_min = 1,
 		.channels_max = 2,
 		.rates = SNDRV_PCM_RATE_8000_96000,
 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
 	},
 	.capture = {
 		.stream_name = "Capture",
-		.channels_min = 2,
+		.channels_min = 1,
 		.channels_max = 2,
 		.rates = SNDRV_PCM_RATE_8000_96000,
 		.formats = SNDRV_PCM_FMTBIT_S16_LE,
-- 
2.53.0


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

* Re: [PATCH v1] ASoC: tegra: Add support for mono channel to Tegra20/Tegra30 I2S DAIs
  2026-11-23 10:02 [PATCH v1] ASoC: tegra: Add support for mono channel to Tegra20/Tegra30 I2S DAIs Svyatoslav Ryhel
@ 2026-11-23 10:23 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-23 10:23 UTC (permalink / raw)
  To: Svyatoslav Ryhel; +Cc: linux-sound

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] ASoC: tegra: mono LRCK divider halves frame rate in
  i2s_hw_params()
- [High] ASoC: tegra: I2S and APBIF CIF channel mismatch in
  tegra30_i2s_hw_params()
--

commit 7e2665c666f6776eab203c934282fc56f2dc5a17
Author: Svyatoslav Ryhel <clamor95@gmail.com>

ASoC: tegra: Add support for mono channel to Tegra20/Tegra30 I2S DAIs

This commit enables 1-channel audio on Tegra20 and Tegra30+ I2S DAIs for
non-DSP formats and updates the I2S clock, timing divider, and Tegra30 CIF
channel calculations.

> diff --git a/sound/soc/tegra/tegra20_i2s.c b/sound/soc/tegra/tegra20_i2s.c
> index 2978c9d6f4839..285923e6aeffa 100644
> --- a/sound/soc/tegra/tegra20_i2s.c
> +++ b/sound/soc/tegra/tegra20_i2s.c
> @@ -144,7 +144,12 @@ static int tegra20_i2s_hw_params(struct snd_pcm_substream *substream,
>  	struct device *dev = dai->dev;
>  	struct tegra20_i2s *i2s = snd_soc_dai_get_drvdata(dai);
>  	unsigned int mask, val;
> -	int ret, sample_size, srate, i2sclock, bitcnt;
> +	int ret, sample_size, srate, i2sclock, bitcnt, i2sclk_div, is_dsp;
> +
> +	is_dsp = regmap_test_bits(i2s->regmap, TEGRA20_I2S_CTRL,
> +				  TEGRA20_I2S_CTRL_BIT_FORMAT_DSP);
> +	if (is_dsp < 0)
> +		return is_dsp;
>  
>  	mask = TEGRA20_I2S_CTRL_BIT_SIZE_MASK;
>  	switch (params_format(params)) {
> @@ -174,18 +179,24 @@ static int tegra20_i2s_hw_params(struct snd_pcm_substream *substream,
>  	/* Final "* 2" required by Tegra hardware */
>  	i2sclock = srate * params_channels(params) * sample_size * 2;
>  
> +	/* Additional "* 2" is needed for DSP mode */
> +	if (is_dsp)
> +		i2sclock *= 2;
> +
>  	ret = clk_set_rate(i2s->clk_i2s, i2sclock);
>  	if (ret) {
>  		dev_err(dev, "Can't set I2S clock rate: %d\n", ret);
>  		return ret;
>  	}
>  
> -	bitcnt = (i2sclock / (2 * srate)) - 1;
> +	i2sclk_div = is_dsp ? srate : params_channels(params) * srate;
> +
> +	bitcnt = (i2sclock / i2sclk_div) - 1;
>  	if (bitcnt < 0 || bitcnt > TEGRA20_I2S_TIMING_CHANNEL_BIT_COUNT_MASK_US)
>  		return -EINVAL;
>  	val = bitcnt << TEGRA20_I2S_TIMING_CHANNEL_BIT_COUNT_SHIFT;
>  
> -	if (i2sclock % (2 * srate))
> +	if (i2sclock % i2sclk_div)
>  		val |= TEGRA20_I2S_TIMING_NON_SYM_ENABLE;

[Severity: High]
Does dividing i2sclock by params_channels(params) * srate in LRCK mode drive
LRCK at srate / 2 for 1-channel streams (here and in tegra30_i2s_hw_params()
below)?

In LRCK formats, each sample frame always has two LRCK half-periods (Left and
Right), and CHANNEL_BIT_COUNT (bitcnt) sets the number of clocks per half-
period minus 1 (see tegra210_i2s_set_timing_params(), which divides by
srate * 2 in I2S_CTRL_FRAME_FMT_LRCK_MODE).  When params_channels(params) is
1, scaling both i2sclock and i2sclk_div by params_channels(params) cancels
out in i2sclock / i2sclk_div, leaving bitcnt identical to 2-channel stereo
while clk_i2s is halved.  On Tegra20, TEGRA20_I2S_CTRL is also still set to
TEGRA20_I2S_CTRL_FIFO_FORMAT_PACKED with no mono configuration, so two
consecutive mono samples in each 32-bit FIFO word are unpacked and shifted
out as Left and Right stereo channels at half the requested sample rate.

[ ... ]

> diff --git a/sound/soc/tegra/tegra30_i2s.c b/sound/soc/tegra/tegra30_i2s.c
> index e5e648d00621b..433a7eb873b43 100644
> --- a/sound/soc/tegra/tegra30_i2s.c
> +++ b/sound/soc/tegra/tegra30_i2s.c

[ ... ]

> @@ -172,14 +183,14 @@ static int tegra30_i2s_hw_params(struct snd_pcm_substream *substream,
>  
>  	val = bitcnt << TEGRA30_I2S_TIMING_CHANNEL_BIT_COUNT_SHIFT;
>  
> -	if (i2sclock % (2 * srate))
> +	if (i2sclock % i2sclk_div)
>  		val |= TEGRA30_I2S_TIMING_NON_SYM_ENABLE;
>  
>  	regmap_write(i2s->regmap, TEGRA30_I2S_TIMING, val);
>  
>  	cif_conf.threshold = 0;
> -	cif_conf.audio_channels = 2;
> -	cif_conf.client_channels = 2;
> +	cif_conf.audio_channels = params_channels(params);
> +	cif_conf.client_channels = params_channels(params);
>  	cif_conf.audio_bits = TEGRA30_AUDIOCIF_BITS_16;
>  	cif_conf.client_bits = TEGRA30_AUDIOCIF_BITS_16;

[Severity: High]
Does setting audio_channels and client_channels to 1 on the I2S CIF mismatch
the paired AHUB APBIF FIFO CIF?

tegra30_ahub_allocate_tx_fifo() and tegra30_ahub_allocate_rx_fifo() configure
the APBIF CIF once at probe time with audio_channels = 2 and
client_channels = 2:

sound/soc/tegra/tegra30_ahub.c:tegra30_ahub_allocate_tx_fifo() {
    ...
	cif_conf.threshold = 0;
	cif_conf.audio_channels = 2;
	cif_conf.client_channels = 2;
	cif_conf.audio_bits = TEGRA30_AUDIOCIF_BITS_16;
	cif_conf.client_bits = TEGRA30_AUDIOCIF_BITS_16;
    ...
}

When params_channels(params) is 1, the APBIF side of the AHUB crossbar stays
configured for 2-channel frames while the I2S CIF is programmed for 1-channel
frames with mono_conv = 0 and stereo_conv = 0, misframing mono playback and
capture streams across the crossbar.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261123100228.77797-1-clamor95@gmail.com?part=1

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

end of thread, other threads:[~2026-11-23 10:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 10:02 [PATCH v1] ASoC: tegra: Add support for mono channel to Tegra20/Tegra30 I2S DAIs Svyatoslav Ryhel
2026-11-23 10:23 ` sashiko-bot

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