From patchwork Thu Oct 17 06:21:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Shengjiu Wang X-Patchwork-Id: 176549 Delivered-To: patch@linaro.org Received: by 2002:a92:7e96:0:0:0:0:0 with SMTP id q22csp475389ill; Wed, 16 Oct 2019 23:24:00 -0700 (PDT) X-Google-Smtp-Source: APXvYqzkQYe1MUq2HDy81aJiWASYr4jIyJJf2EPjZnc/2oQtiqmi5lREWi3nnDSYARfA3suaNeY/ X-Received: by 2002:a50:c2c2:: with SMTP id u2mr2034964edf.133.1571293440359; Wed, 16 Oct 2019 23:24:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1571293440; cv=none; d=google.com; s=arc-20160816; b=cBSgO6RsHsKqaz2gr3/h/WNgcYD9nYOrcPzE4UqNf9RC87ziZnTX9JQIMHgQwEX5a6 lbI102tWTPf2RzxprAz+DK6SXGHQe7Toc0HgGLcds+DhY52b8g4gmiXIikMmb0aAW2TK Kz6IkNbGDqWHpKPUk+WaIlxZIEUWD1Cjpk5Qw1AI/S0yv8hr3LQtdqiv1H8VbNTs5YI3 abv3iq5NCIjd6szxSnOTOybdd+JaT5vZ+vWnDaga7VJB1XGI4LtLnEbifszaIwK+cENg 4iTH42XVwzYuUPPWJrZaMXJ3z6BeorZ7NIAZu0ORFiySOnNZL2X4cYahY74I7xyHojQM Ni6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=/q/HXA+32ELtuJZ4zaooJ95rzbCoPlLFlraWcQ5TsZc=; b=MhJu+6yWBCeLNNLSuBaovpQjr33laJI8BWsEmIGFHZPvevJzHRvuR2AbRPaHT02B6N ro5kITzUFr2gRxo5Px6aw6KC0uutpcYaC5HYoINoPGZgN17B/68qU5gVvVHjnu8qJC95 gMZG2x7KzmgBVLohfRVliZSujKcZpzQUOIS/mWMVX3/HvXRHg+hvNqRUz+R88V00eew1 GXNkW234OnYITU/qcRTt46TyiSpeZ5KILXQD6Ur4Mevi86IRxk9zJUmV6E2IYCHOys/z aNzAE6S1kvdmC4G/WkVLekp+194mvHp/jt8zX7/oh1y6M2665fwqN+gXRFk4de+sy8hZ 7StA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e22si946507eda.300.2019.10.16.23.24.00; Wed, 16 Oct 2019 23:24:00 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2392595AbfJQGX5 (ORCPT + 26 others); Thu, 17 Oct 2019 02:23:57 -0400 Received: from inva021.nxp.com ([92.121.34.21]:54160 "EHLO inva021.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727653AbfJQGX5 (ORCPT ); Thu, 17 Oct 2019 02:23:57 -0400 Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id CF242200031; Thu, 17 Oct 2019 08:23:54 +0200 (CEST) Received: from invc005.ap-rdc01.nxp.com (invc005.ap-rdc01.nxp.com [165.114.16.14]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 8322220004C; Thu, 17 Oct 2019 08:23:49 +0200 (CEST) Received: from localhost.localdomain (shlinux2.ap.freescale.net [10.192.224.44]) by invc005.ap-rdc01.nxp.com (Postfix) with ESMTP id 11186402B4; Thu, 17 Oct 2019 14:23:42 +0800 (SGT) From: Shengjiu Wang To: timur@kernel.org, nicoleotsuka@gmail.com, Xiubo.Lee@gmail.com, festevam@gmail.com, broonie@kernel.org, alsa-devel@alsa-project.org, lgirdwood@gmail.com, perex@perex.cz, tiwai@suse.com Cc: linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org Subject: [PATCH] ASoC: fsl_asrc: refine the setting of internal clock divider Date: Thu, 17 Oct 2019 14:21:08 +0800 Message-Id: <1571293268-5146-1-git-send-email-shengjiu.wang@nxp.com> X-Mailer: git-send-email 2.7.4 X-Virus-Scanned: ClamAV using ClamSMTP Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org For P2P output, the output divider should align with the output sample rate, if use ideal sample rate, there will be a lot of overload, which would cause underrun. The maximum divider of asrc clock is 1024, but there is no judgement for this limitaion in driver, which may cause the divider setting not correct. For non-ideal ratio mode, the clock rate should divide the sample rate with no remainder, and the quotient should be less than 1024. Signed-off-by: Shengjiu Wang --- sound/soc/fsl/fsl_asrc.c | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) -- 2.21.0 diff --git a/sound/soc/fsl/fsl_asrc.c b/sound/soc/fsl/fsl_asrc.c index 0bf91a6f54b9..44d05ec28bd3 100644 --- a/sound/soc/fsl/fsl_asrc.c +++ b/sound/soc/fsl/fsl_asrc.c @@ -260,7 +260,7 @@ static int fsl_asrc_set_ideal_ratio(struct fsl_asrc_pair *pair, * of struct asrc_config which includes in/output sample rate, width, channel * and clock settings. */ -static int fsl_asrc_config_pair(struct fsl_asrc_pair *pair) +static int fsl_asrc_config_pair(struct fsl_asrc_pair *pair, bool p2p) { struct asrc_config *config = pair->config; struct fsl_asrc *asrc_priv = pair->asrc_priv; @@ -268,7 +268,8 @@ static int fsl_asrc_config_pair(struct fsl_asrc_pair *pair) enum asrc_word_width input_word_width; enum asrc_word_width output_word_width; u32 inrate, outrate, indiv, outdiv; - u32 clk_index[2], div[2]; + u32 clk_index[2], div[2], rem[2]; + u64 clk_rate; int in, out, channels; int pre_proc, post_proc; struct clk *clk; @@ -351,7 +352,9 @@ static int fsl_asrc_config_pair(struct fsl_asrc_pair *pair) /* We only have output clock for ideal ratio mode */ clk = asrc_priv->asrck_clk[clk_index[ideal ? OUT : IN]]; - div[IN] = clk_get_rate(clk) / inrate; + clk_rate = clk_get_rate(clk); + rem[IN] = do_div(clk_rate, inrate); + div[IN] = (u32)clk_rate; if (div[IN] == 0) { pair_err("failed to support input sample rate %dHz by asrck_%x\n", inrate, clk_index[ideal ? OUT : IN]); @@ -360,11 +363,20 @@ static int fsl_asrc_config_pair(struct fsl_asrc_pair *pair) clk = asrc_priv->asrck_clk[clk_index[OUT]]; - /* Use fixed output rate for Ideal Ratio mode (INCLK_NONE) */ - if (ideal) - div[OUT] = clk_get_rate(clk) / IDEAL_RATIO_RATE; - else - div[OUT] = clk_get_rate(clk) / outrate; + /* + * When P2P mode, output rate should align with the out samplerate. + * if set too high output rate, there will be lots of Overload. + * When M2M mode, output rate should also need to align with the out + * samplerate, but M2M must use less time to achieve good performance. + */ + clk_rate = clk_get_rate(clk); + if (p2p || !ideal) { + rem[OUT] = do_div(clk_rate, outrate); + div[OUT] = clk_rate; + } else { + rem[OUT] = do_div(clk_rate, IDEAL_RATIO_RATE); + div[OUT] = clk_rate; + } if (div[OUT] == 0) { pair_err("failed to support output sample rate %dHz by asrck_%x\n", @@ -372,6 +384,16 @@ static int fsl_asrc_config_pair(struct fsl_asrc_pair *pair) return -EINVAL; } + if (!ideal && (div[IN] > 1024 || div[OUT] > 1024 || + rem[IN] != 0 || rem[OUT] != 0)) { + pair_err("The divider can't be used for non ideal mode\n"); + return -EINVAL; + } + + /* Divider range is [1, 1024] */ + div[IN] = min_t(u32, 1024, div[IN]); + div[OUT] = min_t(u32, 1024, div[OUT]); + /* Set the channel number */ channels = config->channel_num; @@ -560,7 +582,7 @@ static int fsl_asrc_dai_hw_params(struct snd_pcm_substream *substream, config.output_sample_rate = rate; } - ret = fsl_asrc_config_pair(pair); + ret = fsl_asrc_config_pair(pair, true); if (ret) { dev_err(dai->dev, "fail to config asrc pair\n"); return ret;