ASoC: intel/sdw_utils: Add volume limit to cs35l56 speakers
authorStefan Binding <sbinding@opensource.cirrus.com>
Wed, 30 Apr 2025 10:31:20 +0000 (11:31 +0100)
committerMark Brown <broonie@kernel.org>
Wed, 30 Apr 2025 23:22:06 +0000 (08:22 +0900)
The volume control for cs35l56 speakers has a maximum gain of +12 dB.
However, for many use cases, this can cause distorted audio, depending
various factors, such as other signal-processing elements in the chain,
for example if the audio passes through a gain control before reaching
the amp or the signal path has been tuned for a particular maximum
gain in the amp.

In the case of systems which use the soc_sdw_* driver, audio will
likely be distorted in all cases above 0 dB, therefore add a volume
limit of 400, which is 0 dB maximum volume inside this driver.

The volume limit should be applied to both soundwire and soundwire
bridge configurations.

Signed-off-by: Stefan Binding <sbinding@opensource.cirrus.com>
Link: https://patch.msgid.link/20250430103134.24579-3-sbinding@opensource.cirrus.com
Signed-off-by: Mark Brown <broonie@kernel.org>
include/sound/soc_sdw_utils.h
sound/soc/sdw_utils/soc_sdw_bridge_cs35l56.c
sound/soc/sdw_utils/soc_sdw_cs_amp.c

index 36a4a1e..d8bd5d3 100644 (file)
@@ -226,6 +226,7 @@ int asoc_sdw_cs_amp_init(struct snd_soc_card *card,
                         bool playback);
 int asoc_sdw_cs_spk_feedback_rtd_init(struct snd_soc_pcm_runtime *rtd,
                                      struct snd_soc_dai *dai);
+int asoc_sdw_cs35l56_volume_limit(struct snd_soc_card *card, const char *name_prefix);
 
 /* MAXIM codec support */
 int asoc_sdw_maxim_init(struct snd_soc_card *card,
index 246e5c2..c7e55f4 100644 (file)
@@ -60,6 +60,10 @@ static int asoc_sdw_bridge_cs35l56_asp_init(struct snd_soc_pcm_runtime *rtd)
 
        /* 4 x 16-bit sample slots and FSYNC=48000, BCLK=3.072 MHz */
        for_each_rtd_codec_dais(rtd, i, codec_dai) {
+               ret = asoc_sdw_cs35l56_volume_limit(card, codec_dai->component->name_prefix);
+               if (ret)
+                       return ret;
+
                ret = snd_soc_dai_set_tdm_slot(codec_dai, tx_mask, rx_mask, 4, 16);
                if (ret < 0)
                        return ret;
index 4b6181c..35b550b 100644 (file)
 
 #define CODEC_NAME_SIZE        8
 #define CS_AMP_CHANNELS_PER_AMP        4
+#define CS35L56_SPK_VOLUME_0DB 400 /* 0dB Max */
+
+int asoc_sdw_cs35l56_volume_limit(struct snd_soc_card *card, const char *name_prefix)
+{
+       char *volume_ctl_name;
+       int ret;
+
+       volume_ctl_name = kasprintf(GFP_KERNEL, "%s Speaker Volume", name_prefix);
+       if (!volume_ctl_name)
+               return -ENOMEM;
+
+       ret = snd_soc_limit_volume(card, volume_ctl_name, CS35L56_SPK_VOLUME_0DB);
+       if (ret)
+               dev_err(card->dev, "%s limit set failed: %d\n", volume_ctl_name, ret);
+
+       kfree(volume_ctl_name);
+       return ret;
+}
+EXPORT_SYMBOL_NS(asoc_sdw_cs35l56_volume_limit, "SND_SOC_SDW_UTILS");
 
 int asoc_sdw_cs_spk_rtd_init(struct snd_soc_pcm_runtime *rtd, struct snd_soc_dai *dai)
 {
@@ -40,6 +59,11 @@ int asoc_sdw_cs_spk_rtd_init(struct snd_soc_pcm_runtime *rtd, struct snd_soc_dai
 
                snprintf(widget_name, sizeof(widget_name), "%s SPK",
                         codec_dai->component->name_prefix);
+
+               ret = asoc_sdw_cs35l56_volume_limit(card, codec_dai->component->name_prefix);
+               if (ret)
+                       return ret;
+
                ret = snd_soc_dapm_add_routes(&card->dapm, &route, 1);
                if (ret)
                        return ret;