Commit 5ca2ab45 authored by Robert Hancock's avatar Robert Hancock Committed by Mark Brown

ASoC: simple-card-utils: Add new system-clock-fixed flag

Add a new system-clock-fixed flag, which can be used to specify that the
driver cannot or should not allow the clock frequency of the mapped clock
to be modified. This behavior is also implied if the system-clock-frequency
parameter is set explicitly - the flag is meant for cases where a clock is
mapped to the DAI but which is, or should be treated as, fixed.

When mclk-fs is also specified, this causes a PCM constraint to be added
which enforces that only the corresponding valid sample rate can be used.
Signed-off-by: default avatarRobert Hancock <robert.hancock@calian.com>
Link: https://lore.kernel.org/r/20220120195832.1742271-7-robert.hancock@calian.comSigned-off-by: default avatarMark Brown <broonie@kernel.org>
parent e9fed03a
...@@ -25,6 +25,7 @@ struct asoc_simple_dai { ...@@ -25,6 +25,7 @@ struct asoc_simple_dai {
unsigned int tx_slot_mask; unsigned int tx_slot_mask;
unsigned int rx_slot_mask; unsigned int rx_slot_mask;
struct clk *clk; struct clk *clk;
bool clk_fixed;
}; };
struct asoc_simple_data { struct asoc_simple_data {
......
...@@ -165,12 +165,15 @@ int asoc_simple_parse_clk(struct device *dev, ...@@ -165,12 +165,15 @@ int asoc_simple_parse_clk(struct device *dev,
* or device's module clock. * or device's module clock.
*/ */
clk = devm_get_clk_from_child(dev, node, NULL); clk = devm_get_clk_from_child(dev, node, NULL);
simple_dai->clk_fixed = of_property_read_bool(
node, "system-clock-fixed");
if (!IS_ERR(clk)) { if (!IS_ERR(clk)) {
simple_dai->sysclk = clk_get_rate(clk); simple_dai->sysclk = clk_get_rate(clk);
simple_dai->clk = clk; simple_dai->clk = clk;
} else if (!of_property_read_u32(node, "system-clock-frequency", &val)) { } else if (!of_property_read_u32(node, "system-clock-frequency", &val)) {
simple_dai->sysclk = val; simple_dai->sysclk = val;
simple_dai->clk_fixed = true;
} else { } else {
clk = devm_get_clk_from_child(dev, dlc->of_node, NULL); clk = devm_get_clk_from_child(dev, dlc->of_node, NULL);
if (!IS_ERR(clk)) if (!IS_ERR(clk))
...@@ -184,12 +187,29 @@ int asoc_simple_parse_clk(struct device *dev, ...@@ -184,12 +187,29 @@ int asoc_simple_parse_clk(struct device *dev,
} }
EXPORT_SYMBOL_GPL(asoc_simple_parse_clk); EXPORT_SYMBOL_GPL(asoc_simple_parse_clk);
static int asoc_simple_check_fixed_sysclk(struct device *dev,
struct asoc_simple_dai *dai,
unsigned int *fixed_sysclk)
{
if (dai->clk_fixed) {
if (*fixed_sysclk && *fixed_sysclk != dai->sysclk) {
dev_err(dev, "inconsistent fixed sysclk rates (%u vs %u)\n",
*fixed_sysclk, dai->sysclk);
return -EINVAL;
}
*fixed_sysclk = dai->sysclk;
}
return 0;
}
int asoc_simple_startup(struct snd_pcm_substream *substream) int asoc_simple_startup(struct snd_pcm_substream *substream)
{ {
struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card); struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card);
struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num); struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num);
struct asoc_simple_dai *dai; struct asoc_simple_dai *dai;
unsigned int fixed_sysclk = 0;
int i1, i2, i; int i1, i2, i;
int ret; int ret;
...@@ -197,12 +217,32 @@ int asoc_simple_startup(struct snd_pcm_substream *substream) ...@@ -197,12 +217,32 @@ int asoc_simple_startup(struct snd_pcm_substream *substream)
ret = asoc_simple_clk_enable(dai); ret = asoc_simple_clk_enable(dai);
if (ret) if (ret)
goto cpu_err; goto cpu_err;
ret = asoc_simple_check_fixed_sysclk(rtd->dev, dai, &fixed_sysclk);
if (ret)
goto cpu_err;
} }
for_each_prop_dai_codec(props, i2, dai) { for_each_prop_dai_codec(props, i2, dai) {
ret = asoc_simple_clk_enable(dai); ret = asoc_simple_clk_enable(dai);
if (ret) if (ret)
goto codec_err; goto codec_err;
ret = asoc_simple_check_fixed_sysclk(rtd->dev, dai, &fixed_sysclk);
if (ret)
goto codec_err;
}
if (fixed_sysclk && props->mclk_fs) {
unsigned int fixed_rate = fixed_sysclk / props->mclk_fs;
if (fixed_sysclk % props->mclk_fs) {
dev_err(rtd->dev, "fixed sysclk %u not divisible by mclk_fs %u\n",
fixed_sysclk, props->mclk_fs);
return -EINVAL;
}
ret = snd_pcm_hw_constraint_minmax(substream->runtime, SNDRV_PCM_HW_PARAM_RATE,
fixed_rate, fixed_rate);
if (ret)
goto codec_err;
} }
return 0; return 0;
...@@ -226,31 +266,40 @@ EXPORT_SYMBOL_GPL(asoc_simple_startup); ...@@ -226,31 +266,40 @@ EXPORT_SYMBOL_GPL(asoc_simple_startup);
void asoc_simple_shutdown(struct snd_pcm_substream *substream) void asoc_simple_shutdown(struct snd_pcm_substream *substream)
{ {
struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card); struct asoc_simple_priv *priv = snd_soc_card_get_drvdata(rtd->card);
struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num); struct simple_dai_props *props = simple_priv_to_props(priv, rtd->num);
struct asoc_simple_dai *dai; struct asoc_simple_dai *dai;
int i; int i;
if (props->mclk_fs) { for_each_prop_dai_cpu(props, i, dai) {
snd_soc_dai_set_sysclk(codec_dai, 0, 0, SND_SOC_CLOCK_IN); if (props->mclk_fs && !dai->clk_fixed)
snd_soc_dai_set_sysclk(cpu_dai, 0, 0, SND_SOC_CLOCK_OUT); snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, i),
} 0, 0, SND_SOC_CLOCK_IN);
for_each_prop_dai_cpu(props, i, dai)
asoc_simple_clk_disable(dai); asoc_simple_clk_disable(dai);
for_each_prop_dai_codec(props, i, dai) }
for_each_prop_dai_codec(props, i, dai) {
if (props->mclk_fs && !dai->clk_fixed)
snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd, i),
0, 0, SND_SOC_CLOCK_IN);
asoc_simple_clk_disable(dai); asoc_simple_clk_disable(dai);
}
} }
EXPORT_SYMBOL_GPL(asoc_simple_shutdown); EXPORT_SYMBOL_GPL(asoc_simple_shutdown);
static int asoc_simple_set_clk_rate(struct asoc_simple_dai *simple_dai, static int asoc_simple_set_clk_rate(struct device *dev,
struct asoc_simple_dai *simple_dai,
unsigned long rate) unsigned long rate)
{ {
if (!simple_dai) if (!simple_dai)
return 0; return 0;
if (simple_dai->clk_fixed && rate != simple_dai->sysclk) {
dev_err(dev, "dai %s invalid clock rate %lu\n", simple_dai->name, rate);
return -EINVAL;
}
if (!simple_dai->clk) if (!simple_dai->clk)
return 0; return 0;
...@@ -279,13 +328,13 @@ int asoc_simple_hw_params(struct snd_pcm_substream *substream, ...@@ -279,13 +328,13 @@ int asoc_simple_hw_params(struct snd_pcm_substream *substream,
mclk = params_rate(params) * mclk_fs; mclk = params_rate(params) * mclk_fs;
for_each_prop_dai_codec(props, i, pdai) { for_each_prop_dai_codec(props, i, pdai) {
ret = asoc_simple_set_clk_rate(pdai, mclk); ret = asoc_simple_set_clk_rate(rtd->dev, pdai, mclk);
if (ret < 0) if (ret < 0)
return ret; return ret;
} }
for_each_prop_dai_cpu(props, i, pdai) { for_each_prop_dai_cpu(props, i, pdai) {
ret = asoc_simple_set_clk_rate(pdai, mclk); ret = asoc_simple_set_clk_rate(rtd->dev, pdai, mclk);
if (ret < 0) if (ret < 0)
return ret; return ret;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment