Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp605046pxb; Thu, 25 Feb 2021 10:13:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJwsUZjiOOUZIg2AlyOYReDJe9UrsPc2AWSJEUrm4FG9Lc7tmt9HJMjCiTlStlU9VUPRBMlh X-Received: by 2002:a50:fb03:: with SMTP id d3mr4276748edq.262.1614276829745; Thu, 25 Feb 2021 10:13:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614276829; cv=none; d=google.com; s=arc-20160816; b=gmYpoh1+/vu/II7Hs+gffxCrjNjGIExjNMfUfigvQZo1PNJG6BRq6NScHz8UocYSd2 Xz14AczxBbv3XBJvybY6HVQu85ZNJ72dkEhr8s0c+HHGXuTUu6Sgszly/WivBMw+7Qrq TvKuuuMEyQ5uoYh/Fe9LFcldR18RCGa2FQ3PsDxonK5bzhiqHiejnbz5h/J/wQYPxlAO DeukPuw6PofDyobKAguP1nbP4L0pF/dbgCPNkID5Xb/VCg8OkWzAhnRcXsQensX9PM8p ifT1ab2LzzLZ94KiZwK79yyMwIyU5IPYCwp7Rjee5TegmX6mBQ1ZEIz7HyO+D5+Y+nA2 USng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:dkim-signature:mime-version:references :in-reply-to:message-id:date:subject:cc:to:from; bh=XsuTkaQjpknAbqXsocoOXCmvAkmPnsCYijQXi5ZJhAI=; b=zXDG+losvlQuPMvPoO4n54QwU0eoaZhTBGvwjlKK61xH8khPJTYYuUWFCJW9X8nYv+ uO2+0h8QwaG6xNIXPB4XpLaMAlzUgZ/6lsTBrEk5tGg9BspcFd+kB4QM8OCL3g/CXsUL k+QDnsEvfUSpyuOpaHRCH8g+bC1qhZ/IcsVvrc2AHkhdiHo2rCyePjhzS3HEm+lQEp3+ DKx99V5DxzNZFE/uFvRiX+e1GZXglYpfa9EsREN8Et3C2HeGSyhf28wB9Bwz7QbsZw7H sfuGIYHFneQL043P+GUyIanSGNd4tFDO/HNsiVlQ4/ziZCw6uqxW5ovE8+01ujxfuBmp cCWA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@nvidia.com header.s=n1 header.b=eIPcptow; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nvidia.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e15si2391036edy.373.2021.02.25.10.13.26; Thu, 25 Feb 2021 10:13:49 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@nvidia.com header.s=n1 header.b=eIPcptow; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=nvidia.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233257AbhBYSLz (ORCPT + 99 others); Thu, 25 Feb 2021 13:11:55 -0500 Received: from hqnvemgate24.nvidia.com ([216.228.121.143]:19353 "EHLO hqnvemgate24.nvidia.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232631AbhBYSJQ (ORCPT ); Thu, 25 Feb 2021 13:09:16 -0500 Received: from hqmail.nvidia.com (Not Verified[216.228.121.13]) by hqnvemgate24.nvidia.com (using TLS: TLSv1.2, AES256-SHA) id ; Thu, 25 Feb 2021 10:06:50 -0800 Received: from HQMAIL107.nvidia.com (172.20.187.13) by HQMAIL109.nvidia.com (172.20.187.15) with Microsoft SMTP Server (TLS) id 15.0.1497.2; Thu, 25 Feb 2021 18:06:49 +0000 Received: from audio.nvidia.com (172.20.145.6) by mail.nvidia.com (172.20.187.13) with Microsoft SMTP Server id 15.0.1497.2 via Frontend Transport; Thu, 25 Feb 2021 18:06:47 +0000 From: Sameer Pujar To: , CC: , , , , , Sameer Pujar Subject: [RFC PATCH 5/5] ASoC: simple-card-utils: Support pll configuration Date: Thu, 25 Feb 2021 23:36:04 +0530 Message-ID: <1614276364-13655-6-git-send-email-spujar@nvidia.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1614276364-13655-1-git-send-email-spujar@nvidia.com> References: <1614276364-13655-1-git-send-email-spujar@nvidia.com> MIME-Version: 1.0 Content-Type: text/plain DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=nvidia.com; s=n1; t=1614276410; bh=XsuTkaQjpknAbqXsocoOXCmvAkmPnsCYijQXi5ZJhAI=; h=From:To:CC:Subject:Date:Message-ID:X-Mailer:In-Reply-To: References:MIME-Version:Content-Type; b=eIPcptowc7wdxHaMJXOyE5rvKtk5H4cdQCuLTxJhHu0zx6JhuPMQoUkLRTQYQsa6i NtMgHS1CCcugb4mCEz2fmDEl87MmB2YhYZnWELHmjNJBGW2wdsvRLvnqX8l8fynXK4 MCyYN7FG1MkXM3aUmaNrGNvSAn7HCFVkPCh/HnaLzEc6NlQiWfs7WYs7BOxHgEhJTm 2UPZl+3IWd78mVlY40Yq3nvLPXlHe+ou6s4RO5oWmAmaRe8MgLonEVyjxxeX3epFqY H1Q/oYZH6s+v5l7v3oDnmLwV60meB5iSQMvG/yqfnvYZ+vUaMstOdMGjkPevNq4aNo nH9DHcqc7WI6g== Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 = ; }; 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 = ; pll-source = ; /* * 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 Signed-off-by: Sameer Pujar --- 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 #include #include +#include #include 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