From: Sameer Pujar <spujar@nvidia.com> To: <broonie@kernel.org>, <robh@kernel.org> Cc: <jonathanh@nvidia.com>, <kuninori.morimoto.gx@renesas.com>, <alsa-devel@alsa-project.org>, <devicetree@vger.kernel.org>, <linux-kernel@vger.kernel.org>, Sameer Pujar <spujar@nvidia.com> Subject: [RFC PATCH 5/5] ASoC: simple-card-utils: Support pll configuration Date: Thu, 25 Feb 2021 23:36:04 +0530 [thread overview] Message-ID: <1614276364-13655-6-git-send-email-spujar@nvidia.com> (raw) In-Reply-To: <1614276364-13655-1-git-send-email-spujar@nvidia.com> Some CPU/Codec DAI controllers may have an internal pll which can be used to source its clocking requirements. The ASoC core provides a DAI or component callback snd_soc_dai_set_pll() to configure this. But currently simple-card or audio-graph card drivers are not using this. To configure pll on DAI controller side, following optional DT properties can be used. These can be specified in CPU/Codec DAI or endpoint subnode. - pll-id : DAI specific pll id. - pll-source : DAI specific pll source. CPU/Codec DAI can specify the source that is used for pll reference. - pll-input-reference : Depending on the given value, input frequency can be calculated at runtime. For example if I2S bit clock is used as reference, rate depends on the actual PCM parameters (rate, channels and bits) during an audio session. If a fixed rate is used, then "pll-input-frequency" can be used instead. - pll-output-reference : Depending on the given value, output frequency can be calculated at runtime. If output clock is used for MCLK purpose, then it may depend on "mclk-fs" scale factor. In such cases it needs to be calculated at runtime. If a fixed output frequency is needed, then "pll-output-frequency" can be used instead. - pll-input-frequency : Specify fixed input frequency in Hz. This is checked only when "pll-input-reference" is not specified. - pll-output-frequency : Specify fixed output frequency in Hz. This is checked only when "pll-output-reference" is not specified. Usage examples with RT5658 Codec: 1. Use MCLK for Codec sysclk codec-subnode { mclk-fs = <256>; system-clock-source = <RT5659_SCLK_S_MCLK>; }; In above case, the sysclk source is set to MCLK and the sysclk rate is set to 256 * fs, where fs is sampling rate of the stream. 2. Use Codec internal pll to derive Codec sysclk. The pll can use SoC I2S bit clock (BCLK) as reference. codec-subnode { mclk-fs = <256>; system-clock-source = <RT5659_SCLK_S_PLL1>; pll-source = <RT5659_PLL1_S_BCLK1>; /* * SoC I2S BCLK as reference. Based on this pll input rate * (sample rate * channels * bits) is calculated at runtime. */ pll-input-reference = <2>; /* * pll will generate output rate based on MCLK, which is * calculated at runtime as (mclk_fs * sample rate). */ pll-output-reference = <1>; /* MCLK as output rate */ }; Cc: Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> Signed-off-by: Sameer Pujar <spujar@nvidia.com> --- include/sound/simple_card_utils.h | 33 ++++++++++ sound/soc/generic/simple-card-utils.c | 114 ++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) diff --git a/include/sound/simple_card_utils.h b/include/sound/simple_card_utils.h index e026bf2..0f937eb 100644 --- a/include/sound/simple_card_utils.h +++ b/include/sound/simple_card_utils.h @@ -16,11 +16,44 @@ #define asoc_simple_init_mic(card, sjack, prefix) \ asoc_simple_init_jack(card, sjack, 0, prefix, NULL) +/* + * External audio Codecs for their system clock (sysclk) may use one + * of the following: + * + * - MCLK provided by SoC (already "system-clock-frequency" or + * "mclk-fs" bindings exist) + * + * - Codec internal PLL which can take reference from SoC audio + * interface bit clock (BCLK) and generate required sysclk for + * the Codec. + * + * - Codec internal PLL taking reference from onboard crystal and + * generate required sysclk for the Codec. + * + * BCLK depends on PCM parameters (rate, channels and bits) and has + * to be calculated at runtime. So is MCLK, sometimes it depends on + * sample rate and a scaling factor. So whenever PLL is used for + * sysclk, input and output references/rates need to be provided. + * + * Following macros can be used to setup input/output frequency + * requirements for the PLL. + */ + +#define SND_SOC_DAI_PLL_REF_FIXED 0 +#define SND_SOC_DAI_PLL_REF_MCLK (1 << 0) +#define SND_SOC_DAI_PLL_REF_BCLK (1 << 1) + struct asoc_simple_dai { const char *name; unsigned int sysclk; int sysclk_id; int sysclk_source; + unsigned int pll_id; + unsigned int pll_source; + unsigned int pll_in_freq; + unsigned int pll_out_freq; + unsigned int pll_in_ref; + unsigned int pll_out_ref; int clk_direction; int slots; int slot_width; diff --git a/sound/soc/generic/simple-card-utils.c b/sound/soc/generic/simple-card-utils.c index bbbf891..6bfde08 100644 --- a/sound/soc/generic/simple-card-utils.c +++ b/sound/soc/generic/simple-card-utils.c @@ -12,6 +12,7 @@ #include <linux/of_gpio.h> #include <linux/of_graph.h> #include <sound/jack.h> +#include <sound/pcm_params.h> #include <sound/simple_card_utils.h> void asoc_simple_convert_fixup(struct asoc_simple_data *data, @@ -190,6 +191,30 @@ int asoc_simple_parse_clk(struct device *dev, of_property_read_u32(node, "system-clock-source", &simple_dai->sysclk_source); + of_property_read_u32(node, "pll-id", &simple_dai->pll_id); + + of_property_read_u32(node, "pll-source", &simple_dai->pll_source); + + if (of_property_read_u32(node, "pll-input-reference", + &simple_dai->pll_in_ref)) { + /* + * Look for "pll-input-frequency" if no explicit reference + * is provided. + */ + of_property_read_u32(node, "pll-input-frequency", + &simple_dai->pll_in_freq); + } + + if (of_property_read_u32(node, "pll-output-reference", + &simple_dai->pll_out_ref)) { + /* + * Look for "pll-output-frequency" if no explicit reference + * is provided. + */ + of_property_read_u32(node, "pll-output-frequency", + &simple_dai->pll_out_freq); + } + return 0; } EXPORT_SYMBOL_GPL(asoc_simple_parse_clk); @@ -217,6 +242,82 @@ static int asoc_simple_set_sysclk(struct snd_soc_dai *dai, return 0; } +static int asoc_simple_enable_pll(struct snd_soc_dai *dai, + struct asoc_simple_dai *simple_dai, + struct snd_pcm_hw_params *params, + unsigned int mclk_fs) +{ + int ret; + + if (!simple_dai) + return 0; + + switch (simple_dai->pll_in_ref) { + case SND_SOC_DAI_PLL_REF_FIXED: + break; + case SND_SOC_DAI_PLL_REF_MCLK: + simple_dai->pll_in_freq = params_rate(params) * mclk_fs; + break; + case SND_SOC_DAI_PLL_REF_BCLK: + simple_dai->pll_in_freq = params_rate(params) * + params_channels(params) * + params_width(params); + break; + default: + dev_err(dai->dev, "pll input reference is invalid!\n"); + return -EINVAL; + } + + switch (simple_dai->pll_out_ref) { + case SND_SOC_DAI_PLL_REF_FIXED: + break; + case SND_SOC_DAI_PLL_REF_MCLK: + simple_dai->pll_out_freq = params_rate(params) * mclk_fs; + break; + case SND_SOC_DAI_PLL_REF_BCLK: + simple_dai->pll_out_freq = params_rate(params) * + params_channels(params) * + params_width(params); + break; + default: + dev_err(dai->dev, "pll output reference is invalid!\n"); + return -EINVAL; + } + + if (!simple_dai->pll_in_freq && !simple_dai->pll_out_freq) { + /* There is nothing to be configured */ + return 0; + } else if ((!simple_dai->pll_in_freq && simple_dai->pll_out_freq) || + (simple_dai->pll_in_freq && !simple_dai->pll_out_freq)) { + dev_err(dai->dev, "Invalid pll input/output rates!\n"); + return -EINVAL; + } + + dev_dbg(dai->dev, + "pll config: id = %d, source = %d, in_freq = %u, out_freq = %u\n", + simple_dai->pll_id, simple_dai->pll_source, + simple_dai->pll_in_freq, simple_dai->pll_out_freq); + + ret = snd_soc_dai_set_pll(dai, simple_dai->pll_id, + simple_dai->pll_source, + simple_dai->pll_in_freq, + simple_dai->pll_out_freq); + + if (ret && ret != -ENOTSUPP) + return ret; + + return 0; +} + +static int asoc_simple_disable_pll(struct snd_soc_dai *dai, + struct asoc_simple_dai *simple_dai) +{ + if (!simple_dai) + return 0; + + return snd_soc_dai_set_pll(dai, simple_dai->pll_id, 0, 0, 0); +} + int asoc_simple_startup(struct snd_pcm_substream *substream) { struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); @@ -252,6 +353,9 @@ void asoc_simple_shutdown(struct snd_pcm_substream *substream) SND_SOC_CLOCK_OUT); } + asoc_simple_disable_pll(codec_dai, dai_props->codec_dai); + asoc_simple_disable_pll(cpu_dai, dai_props->cpu_dai); + asoc_simple_clk_disable(dai_props->cpu_dai); asoc_simple_clk_disable(dai_props->codec_dai); @@ -310,6 +414,16 @@ int asoc_simple_hw_params(struct snd_pcm_substream *substream, return ret; } + ret = asoc_simple_enable_pll(codec_dai, dai_props->codec_dai, params, + mclk_fs); + if (ret < 0) + return ret; + + ret = asoc_simple_enable_pll(cpu_dai, dai_props->cpu_dai, params, + mclk_fs); + if (ret < 0) + return ret; + return 0; } EXPORT_SYMBOL_GPL(asoc_simple_hw_params); -- 2.7.4
WARNING: multiple messages have this Message-ID (diff)
From: Sameer Pujar <spujar@nvidia.com> To: <broonie@kernel.org>, <robh@kernel.org> Cc: devicetree@vger.kernel.org, alsa-devel@alsa-project.org, kuninori.morimoto.gx@renesas.com, Sameer Pujar <spujar@nvidia.com>, linux-kernel@vger.kernel.org, jonathanh@nvidia.com Subject: [RFC PATCH 5/5] ASoC: simple-card-utils: Support pll configuration Date: Thu, 25 Feb 2021 23:36:04 +0530 [thread overview] Message-ID: <1614276364-13655-6-git-send-email-spujar@nvidia.com> (raw) In-Reply-To: <1614276364-13655-1-git-send-email-spujar@nvidia.com> Some CPU/Codec DAI controllers may have an internal pll which can be used to source its clocking requirements. The ASoC core provides a DAI or component callback snd_soc_dai_set_pll() to configure this. But currently simple-card or audio-graph card drivers are not using this. To configure pll on DAI controller side, following optional DT properties can be used. These can be specified in CPU/Codec DAI or endpoint subnode. - pll-id : DAI specific pll id. - pll-source : DAI specific pll source. CPU/Codec DAI can specify the source that is used for pll reference. - pll-input-reference : Depending on the given value, input frequency can be calculated at runtime. For example if I2S bit clock is used as reference, rate depends on the actual PCM parameters (rate, channels and bits) during an audio session. If a fixed rate is used, then "pll-input-frequency" can be used instead. - pll-output-reference : Depending on the given value, output frequency can be calculated at runtime. If output clock is used for MCLK purpose, then it may depend on "mclk-fs" scale factor. In such cases it needs to be calculated at runtime. If a fixed output frequency is needed, then "pll-output-frequency" can be used instead. - pll-input-frequency : Specify fixed input frequency in Hz. This is checked only when "pll-input-reference" is not specified. - pll-output-frequency : Specify fixed output frequency in Hz. This is checked only when "pll-output-reference" is not specified. Usage examples with RT5658 Codec: 1. Use MCLK for Codec sysclk codec-subnode { mclk-fs = <256>; system-clock-source = <RT5659_SCLK_S_MCLK>; }; In above case, the sysclk source is set to MCLK and the sysclk rate is set to 256 * fs, where fs is sampling rate of the stream. 2. Use Codec internal pll to derive Codec sysclk. The pll can use SoC I2S bit clock (BCLK) as reference. codec-subnode { mclk-fs = <256>; system-clock-source = <RT5659_SCLK_S_PLL1>; pll-source = <RT5659_PLL1_S_BCLK1>; /* * SoC I2S BCLK as reference. Based on this pll input rate * (sample rate * channels * bits) is calculated at runtime. */ pll-input-reference = <2>; /* * pll will generate output rate based on MCLK, which is * calculated at runtime as (mclk_fs * sample rate). */ pll-output-reference = <1>; /* MCLK as output rate */ }; Cc: Kuninori Morimoto <kuninori.morimoto.gx@renesas.com> Signed-off-by: Sameer Pujar <spujar@nvidia.com> --- include/sound/simple_card_utils.h | 33 ++++++++++ sound/soc/generic/simple-card-utils.c | 114 ++++++++++++++++++++++++++++++++++ 2 files changed, 147 insertions(+) diff --git a/include/sound/simple_card_utils.h b/include/sound/simple_card_utils.h index e026bf2..0f937eb 100644 --- a/include/sound/simple_card_utils.h +++ b/include/sound/simple_card_utils.h @@ -16,11 +16,44 @@ #define asoc_simple_init_mic(card, sjack, prefix) \ asoc_simple_init_jack(card, sjack, 0, prefix, NULL) +/* + * External audio Codecs for their system clock (sysclk) may use one + * of the following: + * + * - MCLK provided by SoC (already "system-clock-frequency" or + * "mclk-fs" bindings exist) + * + * - Codec internal PLL which can take reference from SoC audio + * interface bit clock (BCLK) and generate required sysclk for + * the Codec. + * + * - Codec internal PLL taking reference from onboard crystal and + * generate required sysclk for the Codec. + * + * BCLK depends on PCM parameters (rate, channels and bits) and has + * to be calculated at runtime. So is MCLK, sometimes it depends on + * sample rate and a scaling factor. So whenever PLL is used for + * sysclk, input and output references/rates need to be provided. + * + * Following macros can be used to setup input/output frequency + * requirements for the PLL. + */ + +#define SND_SOC_DAI_PLL_REF_FIXED 0 +#define SND_SOC_DAI_PLL_REF_MCLK (1 << 0) +#define SND_SOC_DAI_PLL_REF_BCLK (1 << 1) + struct asoc_simple_dai { const char *name; unsigned int sysclk; int sysclk_id; int sysclk_source; + unsigned int pll_id; + unsigned int pll_source; + unsigned int pll_in_freq; + unsigned int pll_out_freq; + unsigned int pll_in_ref; + unsigned int pll_out_ref; int clk_direction; int slots; int slot_width; diff --git a/sound/soc/generic/simple-card-utils.c b/sound/soc/generic/simple-card-utils.c index bbbf891..6bfde08 100644 --- a/sound/soc/generic/simple-card-utils.c +++ b/sound/soc/generic/simple-card-utils.c @@ -12,6 +12,7 @@ #include <linux/of_gpio.h> #include <linux/of_graph.h> #include <sound/jack.h> +#include <sound/pcm_params.h> #include <sound/simple_card_utils.h> void asoc_simple_convert_fixup(struct asoc_simple_data *data, @@ -190,6 +191,30 @@ int asoc_simple_parse_clk(struct device *dev, of_property_read_u32(node, "system-clock-source", &simple_dai->sysclk_source); + of_property_read_u32(node, "pll-id", &simple_dai->pll_id); + + of_property_read_u32(node, "pll-source", &simple_dai->pll_source); + + if (of_property_read_u32(node, "pll-input-reference", + &simple_dai->pll_in_ref)) { + /* + * Look for "pll-input-frequency" if no explicit reference + * is provided. + */ + of_property_read_u32(node, "pll-input-frequency", + &simple_dai->pll_in_freq); + } + + if (of_property_read_u32(node, "pll-output-reference", + &simple_dai->pll_out_ref)) { + /* + * Look for "pll-output-frequency" if no explicit reference + * is provided. + */ + of_property_read_u32(node, "pll-output-frequency", + &simple_dai->pll_out_freq); + } + return 0; } EXPORT_SYMBOL_GPL(asoc_simple_parse_clk); @@ -217,6 +242,82 @@ static int asoc_simple_set_sysclk(struct snd_soc_dai *dai, return 0; } +static int asoc_simple_enable_pll(struct snd_soc_dai *dai, + struct asoc_simple_dai *simple_dai, + struct snd_pcm_hw_params *params, + unsigned int mclk_fs) +{ + int ret; + + if (!simple_dai) + return 0; + + switch (simple_dai->pll_in_ref) { + case SND_SOC_DAI_PLL_REF_FIXED: + break; + case SND_SOC_DAI_PLL_REF_MCLK: + simple_dai->pll_in_freq = params_rate(params) * mclk_fs; + break; + case SND_SOC_DAI_PLL_REF_BCLK: + simple_dai->pll_in_freq = params_rate(params) * + params_channels(params) * + params_width(params); + break; + default: + dev_err(dai->dev, "pll input reference is invalid!\n"); + return -EINVAL; + } + + switch (simple_dai->pll_out_ref) { + case SND_SOC_DAI_PLL_REF_FIXED: + break; + case SND_SOC_DAI_PLL_REF_MCLK: + simple_dai->pll_out_freq = params_rate(params) * mclk_fs; + break; + case SND_SOC_DAI_PLL_REF_BCLK: + simple_dai->pll_out_freq = params_rate(params) * + params_channels(params) * + params_width(params); + break; + default: + dev_err(dai->dev, "pll output reference is invalid!\n"); + return -EINVAL; + } + + if (!simple_dai->pll_in_freq && !simple_dai->pll_out_freq) { + /* There is nothing to be configured */ + return 0; + } else if ((!simple_dai->pll_in_freq && simple_dai->pll_out_freq) || + (simple_dai->pll_in_freq && !simple_dai->pll_out_freq)) { + dev_err(dai->dev, "Invalid pll input/output rates!\n"); + return -EINVAL; + } + + dev_dbg(dai->dev, + "pll config: id = %d, source = %d, in_freq = %u, out_freq = %u\n", + simple_dai->pll_id, simple_dai->pll_source, + simple_dai->pll_in_freq, simple_dai->pll_out_freq); + + ret = snd_soc_dai_set_pll(dai, simple_dai->pll_id, + simple_dai->pll_source, + simple_dai->pll_in_freq, + simple_dai->pll_out_freq); + + if (ret && ret != -ENOTSUPP) + return ret; + + return 0; +} + +static int asoc_simple_disable_pll(struct snd_soc_dai *dai, + struct asoc_simple_dai *simple_dai) +{ + if (!simple_dai) + return 0; + + return snd_soc_dai_set_pll(dai, simple_dai->pll_id, 0, 0, 0); +} + int asoc_simple_startup(struct snd_pcm_substream *substream) { struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream); @@ -252,6 +353,9 @@ void asoc_simple_shutdown(struct snd_pcm_substream *substream) SND_SOC_CLOCK_OUT); } + asoc_simple_disable_pll(codec_dai, dai_props->codec_dai); + asoc_simple_disable_pll(cpu_dai, dai_props->cpu_dai); + asoc_simple_clk_disable(dai_props->cpu_dai); asoc_simple_clk_disable(dai_props->codec_dai); @@ -310,6 +414,16 @@ int asoc_simple_hw_params(struct snd_pcm_substream *substream, return ret; } + ret = asoc_simple_enable_pll(codec_dai, dai_props->codec_dai, params, + mclk_fs); + if (ret < 0) + return ret; + + ret = asoc_simple_enable_pll(cpu_dai, dai_props->cpu_dai, params, + mclk_fs); + if (ret < 0) + return ret; + return 0; } EXPORT_SYMBOL_GPL(asoc_simple_hw_params); -- 2.7.4
next prev parent reply other threads:[~2021-02-25 18:12 UTC|newest] Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-02-25 18:05 [RFC PATCH 0/5] Flexible sysclk/pll configuration Sameer Pujar 2021-02-25 18:05 ` Sameer Pujar 2021-02-25 18:06 ` [RFC PATCH 1/5] ASoC: soc-component: Fix return value of snd_soc_component_set_pll() Sameer Pujar 2021-02-25 18:06 ` Sameer Pujar 2021-02-25 18:06 ` [RFC PATCH 2/5] ASoC: soc-dai: Add sysclk source to snd_soc_dai_set_sysclk() Sameer Pujar 2021-02-25 18:06 ` Sameer Pujar 2021-02-25 20:48 ` kernel test robot 2021-02-25 18:06 ` [RFC PATCH 3/5] ASoC: audio-graph-card: Add bindings for sysclk and pll Sameer Pujar 2021-02-25 18:06 ` Sameer Pujar 2021-03-02 2:10 ` Rob Herring 2021-03-02 2:10 ` Rob Herring 2021-03-02 7:03 ` Sameer Pujar 2021-03-02 7:03 ` Sameer Pujar 2021-03-02 16:12 ` Mark Brown 2021-03-02 16:12 ` Mark Brown 2021-02-25 18:06 ` [RFC PATCH 4/5] ASoC: simple-card-utils: Parse sysclk id and source Sameer Pujar 2021-02-25 18:06 ` Sameer Pujar 2021-02-25 18:06 ` Sameer Pujar [this message] 2021-02-25 18:06 ` [RFC PATCH 5/5] ASoC: simple-card-utils: Support pll configuration Sameer Pujar 2021-02-26 5:24 ` [RFC PATCH 0/5] Flexible sysclk/pll configuration Sameer Pujar 2021-02-26 5:24 ` Sameer Pujar
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=1614276364-13655-6-git-send-email-spujar@nvidia.com \ --to=spujar@nvidia.com \ --cc=alsa-devel@alsa-project.org \ --cc=broonie@kernel.org \ --cc=devicetree@vger.kernel.org \ --cc=jonathanh@nvidia.com \ --cc=kuninori.morimoto.gx@renesas.com \ --cc=linux-kernel@vger.kernel.org \ --cc=robh@kernel.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.