Received: by 2002:a25:4158:0:0:0:0:0 with SMTP id o85csp801098yba; Wed, 3 Apr 2019 20:52:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqzy9QLNAzJwkNcagMhxuG+X45IF0s2PII27oIdkpW5eC70pkJD2liZJqLGrcv3EnnhL3Glh X-Received: by 2002:a62:1249:: with SMTP id a70mr3407399pfj.160.1554349945647; Wed, 03 Apr 2019 20:52:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1554349945; cv=none; d=google.com; s=arc-20160816; b=FozTchIiMOd5fvYGNwZ6w9Mokb71Oe61nOnFBWQEZXzAJWw5328WIlKdsWhy5Gu25c SQxjgXJzc+YiYd1iuitMnSASfIkVz0OD67onEaGei3CqqdDCeoKIcCq4/+V8hnxg7D6j wtAAXiGUZdrNvcTn4mPm/MnETfdiubtggm9mc3hse3UW6f7dZ/ie+kLg1Y8ehBuAYUT3 f0dIkEFAcqEdIcQwCBVWXy6WRKcEuhjXfPEoaDdlO1DAtOLbgTxGhQHPLPKYElLMiKWw 7qyodb6yThjTdvOZn1Gal5d35M33Fz4HoDsSW/4Eo5uWHovyACmsHcJ6yyhX4lHUBmio OhPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=FEzTYRST0ntB1RF7uagt9Hi8cAfi/9galLlLTzLrigE=; b=TJC1tQ8Sxq83up4ScBEdAKJ+RySkYpTeNlmk16LAEiE6kVJOnBwapFQWa/DJKLECqV oAQLNF9u7MzsCMb+w5P0YkV71Y+BWwPi+qkRW6LxyEpHf4PxDyQtnEHi5tbHJqnYdiU+ JUD5q7ykQNW553oonlSlSLWIthqZIClypD/ndzfN5gM1X6IPP4Tq7EqH8/2d2Q88ZsWg XB/FgVmIjpEwiUWwmpHigEWMF6+1Dh35ivWXJ7dnv+IYqzXQRwwjbglNeCNKnJt9hrLC 5AbrrqUaa2gMfYQ+Jc/yc0kJDbzpMi303Dv6K9vsqoNi0ciHDxsbP3rZYMUopU9Mav7P 2bOA== 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e5si15194869pls.29.2019.04.03.20.52.10; Wed, 03 Apr 2019 20:52:25 -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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728735AbfDDDv2 (ORCPT + 99 others); Wed, 3 Apr 2019 23:51:28 -0400 Received: from regular1.263xmail.com ([211.150.70.203]:48596 "EHLO regular1.263xmail.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728723AbfDDDv2 (ORCPT ); Wed, 3 Apr 2019 23:51:28 -0400 Received: from sugar.zhang?rock-chips.com (unknown [192.168.167.228]) by regular1.263xmail.com (Postfix) with ESMTP id 47DA43E9; Thu, 4 Apr 2019 11:51:25 +0800 (CST) X-263anti-spam: KSV:0;BIG:0; X-MAIL-GRAY: 0 X-MAIL-DELIVERY: 1 X-KSVirus-check: 0 X-ADDR-CHECKED4: 1 X-ABS-CHECKED: 1 X-SKE-CHECKED: 1 X-ANTISPAM-LEVEL: 2 Received: from localhost.localdomain (unknown [58.22.7.114]) by smtp.263.net (postfix) whith ESMTP id P10068T139956624271104S1554349882632874_; Thu, 04 Apr 2019 11:51:24 +0800 (CST) X-IP-DOMAINF: 1 X-UNIQUE-TAG: <884880fc7e9e10ed45848acbefede9f0> X-RL-SENDER: sugar.zhang@rock-chips.com X-SENDER: zxg@rock-chips.com X-LOGIN-NAME: sugar.zhang@rock-chips.com X-FST-TO: heiko@sntech.de X-SENDER-IP: 58.22.7.114 X-ATTACHMENT-NUM: 0 X-DNS-TYPE: 0 From: Sugar Zhang To: heiko@sntech.de, broonie@kernel.org Cc: Liam Girdwood , Jaroslav Kysela , Takashi Iwai , alsa-devel@alsa-project.org, linux-arm-kernel@lists.infradead.org, linux-rockchip@lists.infradead.org, linux-kernel@vger.kernel.org, Sugar Zhang Subject: [PATCH v1 06/12] ASoC: rockchip: pdm: fixup pdm fractional div Date: Thu, 4 Apr 2019 11:51:09 +0800 Message-Id: <1554349869-23623-1-git-send-email-sugar.zhang@rock-chips.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1554298856-59028-1-git-send-email-sugar.zhang@rock-chips.com> References: <1554298856-59028-1-git-send-email-sugar.zhang@rock-chips.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This patch adds support fractional div for rk3308. Signed-off-by: Sugar Zhang --- sound/soc/rockchip/rockchip_pdm.c | 172 ++++++++++++++++++++++++++++---------- sound/soc/rockchip/rockchip_pdm.h | 9 ++ 2 files changed, 139 insertions(+), 42 deletions(-) diff --git a/sound/soc/rockchip/rockchip_pdm.c b/sound/soc/rockchip/rockchip_pdm.c index c50494b..4f93a74 100644 --- a/sound/soc/rockchip/rockchip_pdm.c +++ b/sound/soc/rockchip/rockchip_pdm.c @@ -17,14 +17,23 @@ #include #include #include +#include #include +#include #include +#include #include #include #include "rockchip_pdm.h" #define PDM_DMA_BURST_SIZE (8) /* size * width: 8*4 = 32 bytes */ +#define PDM_SIGNOFF_CLK_RATE (100000000) + +enum rk_pdm_version { + RK_PDM_RK3229, + RK_PDM_RK3308, +}; struct rk_pdm_dev { struct device *dev; @@ -32,22 +41,51 @@ struct rk_pdm_dev { struct clk *hclk; struct regmap *regmap; struct snd_dmaengine_dai_dma_data capture_dma_data; + struct reset_control *reset; + enum rk_pdm_version version; }; struct rk_pdm_clkref { unsigned int sr; unsigned int clk; + unsigned int clk_out; +}; + +struct rk_pdm_ds_ratio { + unsigned int ratio; + unsigned int sr; }; static struct rk_pdm_clkref clkref[] = { - { 8000, 40960000 }, - { 11025, 56448000 }, - { 12000, 61440000 }, + { 8000, 40960000, 2048000 }, + { 11025, 56448000, 2822400 }, + { 12000, 61440000, 3072000 }, + { 8000, 98304000, 2048000 }, + { 12000, 98304000, 3072000 }, +}; + +static struct rk_pdm_ds_ratio ds_ratio[] = { + { 0, 192000 }, + { 0, 176400 }, + { 0, 128000 }, + { 1, 96000 }, + { 1, 88200 }, + { 1, 64000 }, + { 2, 48000 }, + { 2, 44100 }, + { 2, 32000 }, + { 3, 24000 }, + { 3, 22050 }, + { 3, 16000 }, + { 4, 12000 }, + { 4, 11025 }, + { 4, 8000 }, }; -static unsigned int get_pdm_clk(unsigned int sr) +static unsigned int get_pdm_clk(struct rk_pdm_dev *pdm, unsigned int sr, + unsigned int *clk_src, unsigned int *clk_out) { - unsigned int i, count, clk, div; + unsigned int i, count, clk, div, rate; clk = 0; if (!sr) @@ -59,14 +97,39 @@ static unsigned int get_pdm_clk(unsigned int sr) continue; div = sr / clkref[i].sr; if ((div & (div - 1)) == 0) { + *clk_out = clkref[i].clk_out; + rate = clk_round_rate(pdm->clk, clkref[i].clk); + if (rate != clkref[i].clk) + continue; clk = clkref[i].clk; + *clk_src = clkref[i].clk; break; } } + if (!clk) { + clk = clk_round_rate(pdm->clk, PDM_SIGNOFF_CLK_RATE); + *clk_src = clk; + } return clk; } +static unsigned int get_pdm_ds_ratio(unsigned int sr) +{ + unsigned int i, count, ratio; + + ratio = 0; + if (!sr) + return ratio; + + count = ARRAY_SIZE(ds_ratio); + for (i = 0; i < count; i++) { + if (sr == ds_ratio[i].sr) + ratio = ds_ratio[i].ratio; + } + return ratio; +} + static inline struct rk_pdm_dev *to_info(struct snd_soc_dai *dai) { return snd_soc_dai_get_drvdata(dai); @@ -95,40 +158,52 @@ static int rockchip_pdm_hw_params(struct snd_pcm_substream *substream, struct rk_pdm_dev *pdm = to_info(dai); unsigned int val = 0; unsigned int clk_rate, clk_div, samplerate; + unsigned int clk_src, clk_out; + unsigned long m, n; + bool change; int ret; + if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) + return 0; + samplerate = params_rate(params); - clk_rate = get_pdm_clk(samplerate); + clk_rate = get_pdm_clk(pdm, samplerate, &clk_src, &clk_out); if (!clk_rate) return -EINVAL; - ret = clk_set_rate(pdm->clk, clk_rate); + ret = clk_set_rate(pdm->clk, clk_src); if (ret) return -EINVAL; - clk_div = DIV_ROUND_CLOSEST(clk_rate, samplerate); - - switch (clk_div) { - case 320: - val = PDM_CLK_320FS; - break; - case 640: - val = PDM_CLK_640FS; - break; - case 1280: - val = PDM_CLK_1280FS; - break; - case 2560: - val = PDM_CLK_2560FS; - break; - case 5120: - val = PDM_CLK_5120FS; - break; - default: - dev_err(pdm->dev, "unsupported div: %d\n", clk_div); - return -EINVAL; + if (pdm->version == RK_PDM_RK3308) { + rational_best_approximation(clk_out, clk_src, + GENMASK(16 - 1, 0), + GENMASK(16 - 1, 0), + &m, &n); + + val = (m << PDM_FD_NUMERATOR_SFT) | + (n << PDM_FD_DENOMINATOR_SFT); + regmap_update_bits_check(pdm->regmap, PDM_CTRL1, + PDM_FD_NUMERATOR_MSK | + PDM_FD_DENOMINATOR_MSK, + val, &change); + if (change) { + reset_control_assert(pdm->reset); + reset_control_deassert(pdm->reset); + rockchip_pdm_rxctrl(pdm, 0); + } + clk_div = n / m; + if (clk_div >= 40) + val = PDM_CLK_FD_RATIO_40; + else if (clk_div <= 35) + val = PDM_CLK_FD_RATIO_35; + else + return -EINVAL; + regmap_update_bits(pdm->regmap, PDM_CLK_CTRL, + PDM_CLK_FD_RATIO_MSK, + val); } - + val = get_pdm_ds_ratio(samplerate); regmap_update_bits(pdm->regmap, PDM_CLK_CTRL, PDM_DS_RATIO_MSK, val); regmap_update_bits(pdm->regmap, PDM_HPF_CTRL, PDM_HPF_CF_MSK, PDM_HPF_60HZ); @@ -177,13 +252,11 @@ static int rockchip_pdm_hw_params(struct snd_pcm_substream *substream, return -EINVAL; } - if (substream->stream == SNDRV_PCM_STREAM_CAPTURE) { - regmap_update_bits(pdm->regmap, PDM_CTRL0, - PDM_PATH_MSK | PDM_VDW_MSK, - val); - regmap_update_bits(pdm->regmap, PDM_DMA_CTRL, PDM_DMA_RDL_MSK, - PDM_DMA_RDL(16)); - } + regmap_update_bits(pdm->regmap, PDM_CTRL0, + PDM_PATH_MSK | PDM_VDW_MSK, + val); + regmap_update_bits(pdm->regmap, PDM_DMA_CTRL, PDM_DMA_RDL_MSK, + PDM_DMA_RDL(16)); return 0; } @@ -380,8 +453,19 @@ static const struct regmap_config rockchip_pdm_regmap_config = { .cache_type = REGCACHE_FLAT, }; +static const struct of_device_id rockchip_pdm_match[] = { + { .compatible = "rockchip,pdm", }, + { .compatible = "rockchip,px30-pdm", + .data = (void *)RK_PDM_RK3308 }, + { .compatible = "rockchip,rk3308-pdm", + .data = (void *)RK_PDM_RK3308 }, + {}, +}; +MODULE_DEVICE_TABLE(of, rockchip_pdm_match); + static int rockchip_pdm_probe(struct platform_device *pdev) { + const struct of_device_id *match; struct rk_pdm_dev *pdm; struct resource *res; void __iomem *regs; @@ -391,6 +475,16 @@ static int rockchip_pdm_probe(struct platform_device *pdev) if (!pdm) return -ENOMEM; + match = of_match_device(rockchip_pdm_match, &pdev->dev); + if (match) + pdm->version = (enum rk_pdm_version)match->data; + + if (pdm->version == RK_PDM_RK3308) { + pdm->reset = devm_reset_control_get(&pdev->dev, "pdm-m"); + if (IS_ERR(pdm->reset)) + return PTR_ERR(pdm->reset); + } + res = platform_get_resource(pdev, IORESOURCE_MEM, 0); regs = devm_ioremap_resource(&pdev->dev, res); if (IS_ERR(regs)) @@ -503,12 +597,6 @@ static const struct dev_pm_ops rockchip_pdm_pm_ops = { SET_SYSTEM_SLEEP_PM_OPS(rockchip_pdm_suspend, rockchip_pdm_resume) }; -static const struct of_device_id rockchip_pdm_match[] = { - { .compatible = "rockchip,pdm", }, - {}, -}; -MODULE_DEVICE_TABLE(of, rockchip_pdm_match); - static struct platform_driver rockchip_pdm_driver = { .probe = rockchip_pdm_probe, .remove = rockchip_pdm_remove, diff --git a/sound/soc/rockchip/rockchip_pdm.h b/sound/soc/rockchip/rockchip_pdm.h index 00a8fa1..ae88644 100644 --- a/sound/soc/rockchip/rockchip_pdm.h +++ b/sound/soc/rockchip/rockchip_pdm.h @@ -53,7 +53,16 @@ #define PDM_VDW_MSK (0x1f << 0) #define PDM_VDW(X) ((X - 1) << 0) +/* PDM CTRL1 */ +#define PDM_FD_NUMERATOR_SFT 16 +#define PDM_FD_NUMERATOR_MSK GENMASK(31, 16) +#define PDM_FD_DENOMINATOR_SFT 0 +#define PDM_FD_DENOMINATOR_MSK GENMASK(15, 0) + /* PDM CLK CTRL */ +#define PDM_CLK_FD_RATIO_MSK BIT(6) +#define PDM_CLK_FD_RATIO_40 (0X0 << 6) +#define PDM_CLK_FD_RATIO_35 BIT(6) #define PDM_CLK_MSK BIT(5) #define PDM_CLK_EN BIT(5) #define PDM_CLK_DIS (0x0 << 5) -- 2.7.4