ASoC: tegra20: i2s: Filter out unsupported rates
authorDmitry Osipenko <digetx@gmail.com>
Sat, 4 Dec 2021 14:37:16 +0000 (17:37 +0300)
committerMark Brown <broonie@kernel.org>
Fri, 17 Dec 2021 11:14:01 +0000 (11:14 +0000)
Support new nvidia,fixed-parent-rate device-tree property which instructs
I2S that board wants parent clock rate to stay at a fixed rate. This allows
to play audio over S/PDIF and I2S simultaneously. The root of the problem
is that audio components on Tegra share the same audio PLL, and thus, only
a subset of rates can be supported if we want to play audio simultaneously.
Filter out audio rates that don't match parent clock rate if device-tree
has the nvidia,fixed-parent-rate property.

Signed-off-by: Dmitry Osipenko <digetx@gmail.com>
Acked-by: Thierry Reding <treding@nvidia.com>
Link: https://lore.kernel.org/r/20211204143725.31646-14-digetx@gmail.com
Signed-off-by: Mark Brown <broonie@kernel.org>
sound/soc/tegra/tegra20_i2s.c

index 266d2ca..27365a8 100644 (file)
@@ -262,10 +262,59 @@ static int tegra20_i2s_probe(struct snd_soc_dai *dai)
        return 0;
 }
 
+static const unsigned int tegra20_i2s_rates[] = {
+       8000, 11025, 16000, 22050, 32000, 44100, 48000, 64000, 88200, 96000
+};
+
+static int tegra20_i2s_filter_rates(struct snd_pcm_hw_params *params,
+                                   struct snd_pcm_hw_rule *rule)
+{
+       struct snd_interval *r = hw_param_interval(params, rule->var);
+       struct snd_soc_dai *dai = rule->private;
+       struct tegra20_i2s *i2s = dev_get_drvdata(dai->dev);
+       struct clk *parent = clk_get_parent(i2s->clk_i2s);
+       long i, parent_rate, valid_rates = 0;
+
+       parent_rate = clk_get_rate(parent);
+       if (parent_rate <= 0) {
+               dev_err(dai->dev, "Can't get parent clock rate: %ld\n",
+                       parent_rate);
+               return parent_rate ?: -EINVAL;
+       }
+
+       for (i = 0; i < ARRAY_SIZE(tegra20_i2s_rates); i++) {
+               if (parent_rate % (tegra20_i2s_rates[i] * 128) == 0)
+                       valid_rates |= BIT(i);
+       }
+
+       /*
+        * At least one rate must be valid, otherwise the parent clock isn't
+        * audio PLL. Nothing should be filtered in this case.
+        */
+       if (!valid_rates)
+               valid_rates = BIT(ARRAY_SIZE(tegra20_i2s_rates)) - 1;
+
+       return snd_interval_list(r, ARRAY_SIZE(tegra20_i2s_rates),
+                                tegra20_i2s_rates, valid_rates);
+}
+
+static int tegra20_i2s_startup(struct snd_pcm_substream *substream,
+                              struct snd_soc_dai *dai)
+{
+       if (!device_property_read_bool(dai->dev, "nvidia,fixed-parent-rate"))
+               return 0;
+
+       return snd_pcm_hw_rule_add(substream->runtime, 0,
+                                  SNDRV_PCM_HW_PARAM_RATE,
+                                  tegra20_i2s_filter_rates, dai,
+                                  SNDRV_PCM_HW_PARAM_RATE, -1);
+}
+
 static const struct snd_soc_dai_ops tegra20_i2s_dai_ops = {
        .set_fmt        = tegra20_i2s_set_fmt,
        .hw_params      = tegra20_i2s_hw_params,
        .trigger        = tegra20_i2s_trigger,
+       .startup        = tegra20_i2s_startup,
 };
 
 static const struct snd_soc_dai_driver tegra20_i2s_dai_template = {