From patchwork Fri Dec 16 09:46:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lukasz Majewski X-Patchwork-Id: 13074851 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from alsa0.perex.cz (alsa0.perex.cz [77.48.224.243]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 46F18C4332F for ; Fri, 16 Dec 2022 09:48:26 +0000 (UTC) Received: from alsa1.perex.cz (alsa1.perex.cz [207.180.221.201]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by alsa0.perex.cz (Postfix) with ESMTPS id 115891712; Fri, 16 Dec 2022 10:47:34 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa0.perex.cz 115891712 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alsa-project.org; s=default; t=1671184104; bh=wWbWvJmUI3/8mzdXlByCB60ArMZ4m6Fj/Ah9zNUB1qI=; h=From:To:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: Cc:From; b=Mg4wddAaJt5FSEGLP4v+B7RDKPbUdM86/YWyGihipIbhHt29LWVV5SM/CyqUjQKIu yrFam3bSUqpVQicBRSicqcSJ0jiUTPQBhH4Ff2SWn7DQidPTUEN5L6dsWnOqgZcXFZ RQQhjxLmghyQC1WPSadgpjnTe+uZY3iaVhrXyWl0= Received: from alsa1.perex.cz (localhost.localdomain [127.0.0.1]) by alsa1.perex.cz (Postfix) with ESMTP id B1C46F80519; Fri, 16 Dec 2022 10:47:13 +0100 (CET) Received: by alsa1.perex.cz (Postfix, from userid 50401) id BBF40F804E7; Fri, 16 Dec 2022 10:47:11 +0100 (CET) Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by alsa1.perex.cz (Postfix) with ESMTPS id 143EAF804E7 for ; Fri, 16 Dec 2022 10:47:09 +0100 (CET) DKIM-Filter: OpenDKIM Filter v2.11.0 alsa1.perex.cz 143EAF804E7 Authentication-Results: alsa1.perex.cz; dkim=pass (2048-bit key, unprotected) header.d=denx.de header.i=@denx.de header.a=rsa-sha256 header.s=phobos-20191101 header.b=OUc7HgVE Received: from localhost.localdomain (85-222-111-42.dynamic.chello.pl [85.222.111.42]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: lukma@denx.de) by phobos.denx.de (Postfix) with ESMTPSA id CAF8C85203; Fri, 16 Dec 2022 10:46:38 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=denx.de; s=phobos-20191101; t=1671183999; bh=QjrOuOyv1lBx156ygBTuGYeWBUOwe+V7ssNzofrG5HE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OUc7HgVEIh0X/Hwk/JsXi+qLVmWbUWnmJ80icQijdwTHh8r0OW9IMy/hyNk/ZpCSa EDFnJ7j3Fq71IVxM3WGKQ480avojWIK4AVht0ivyw6GDWfPoyutg4jQfXTSTFnqa+m Zm8DxHOlhGm8XD6cviXYcGxRSuXwj6aztV9Ked0MG2Z221pWxE8z5AEDYO8jWK/UjL ytWLzrkJZ7gisLHwIw1+cl+FqHugjC/6gS23LjgCCsg+4bmkKVEiCtbLppr3CG4YFu rjYZS6yQ5DTpoowherl1eD7+AtT4LkhWwxaRSELeJqZdaWFrl1ZO2Wq/5wFl2r+Fpl S3oIB2fzoX9hA== From: Lukasz Majewski To: Liam Girdwood , Mark Brown , Charles Keepax Subject: [PATCH v3 2/3] ASoC: wm8940: Rewrite code to set proper clocks Date: Fri, 16 Dec 2022 10:46:23 +0100 Message-Id: <20221216094624.3849278-2-lukma@denx.de> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221216094624.3849278-1-lukma@denx.de> References: <20221216094624.3849278-1-lukma@denx.de> MIME-Version: 1.0 X-Virus-Scanned: clamav-milter 0.103.6 at phobos.denx.de X-Virus-Status: Clean X-BeenThere: alsa-devel@alsa-project.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Alsa-devel mailing list for ALSA developers - http://www.alsa-project.org" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: alsa-devel@alsa-project.org, Stephen Kitt , patches@opensource.cirrus.com, Takashi Iwai , linux-kernel@vger.kernel.org, Lukasz Majewski Errors-To: alsa-devel-bounces@alsa-project.org Sender: "Alsa-devel" This patch enables support for internal wm8940's PLL and proper divider to set proper value for 256x fs clock. This approach is more flexible and replaces hardcoded clock values and makes the codec work with the simple-card driver. Card drivers calling set_pll() and set_clkdiv() directly are unaffected. For the reference - code in this commit is based on: 51b2bb3f2568e6d9d81a001d38b8d70c2ba4af99 ("ASoC: wm8974: configure pll and mclk divider automatically") Signed-off-by: Lukasz Majewski Acked-by: Charles Keepax --- Changes for v2: - Remove not required line add/remove - Rewrite the commit message to describe more precisely the problem Changes for v3: - Rewrite commit message with proper refernce format for patch - Add Acked-by tag --- sound/soc/codecs/wm8940.c | 101 +++++++++++++++++++++++++++++++------- 1 file changed, 82 insertions(+), 19 deletions(-) diff --git a/sound/soc/codecs/wm8940.c b/sound/soc/codecs/wm8940.c index 0b59020d747f..1b02d5e8a007 100644 --- a/sound/soc/codecs/wm8940.c +++ b/sound/soc/codecs/wm8940.c @@ -37,7 +37,9 @@ #include "wm8940.h" struct wm8940_priv { - unsigned int sysclk; + unsigned int mclk; + unsigned int fs; + struct regmap *regmap; }; @@ -387,17 +389,24 @@ static int wm8940_set_dai_fmt(struct snd_soc_dai *codec_dai, return 0; } +static int wm8940_update_clocks(struct snd_soc_dai *dai); static int wm8940_i2s_hw_params(struct snd_pcm_substream *substream, struct snd_pcm_hw_params *params, struct snd_soc_dai *dai) { struct snd_soc_component *component = dai->component; + struct wm8940_priv *priv = snd_soc_component_get_drvdata(component); u16 iface = snd_soc_component_read(component, WM8940_IFACE) & 0xFD9F; u16 addcntrl = snd_soc_component_read(component, WM8940_ADDCNTRL) & 0xFFF1; u16 companding = snd_soc_component_read(component, WM8940_COMPANDINGCTL) & 0xFFDF; int ret; + priv->fs = params_rate(params); + ret = wm8940_update_clocks(dai); + if (ret) + return ret; + /* LoutR control */ if (substream->stream == SNDRV_PCM_STREAM_CAPTURE && params_channels(params) == 2) @@ -611,24 +620,6 @@ static int wm8940_set_dai_pll(struct snd_soc_dai *codec_dai, int pll_id, return 0; } -static int wm8940_set_dai_sysclk(struct snd_soc_dai *codec_dai, - int clk_id, unsigned int freq, int dir) -{ - struct snd_soc_component *component = codec_dai->component; - struct wm8940_priv *wm8940 = snd_soc_component_get_drvdata(component); - - switch (freq) { - case 11289600: - case 12000000: - case 12288000: - case 16934400: - case 18432000: - wm8940->sysclk = freq; - return 0; - } - return -EINVAL; -} - static int wm8940_set_dai_clkdiv(struct snd_soc_dai *codec_dai, int div_id, int div) { @@ -653,6 +644,78 @@ static int wm8940_set_dai_clkdiv(struct snd_soc_dai *codec_dai, return ret; } +static unsigned int wm8940_get_mclkdiv(unsigned int f_in, unsigned int f_out, + int *mclkdiv) +{ + unsigned int ratio = 2 * f_in / f_out; + + if (ratio <= 2) { + *mclkdiv = WM8940_MCLKDIV_1; + ratio = 2; + } else if (ratio == 3) { + *mclkdiv = WM8940_MCLKDIV_1_5; + } else if (ratio == 4) { + *mclkdiv = WM8940_MCLKDIV_2; + } else if (ratio <= 6) { + *mclkdiv = WM8940_MCLKDIV_3; + ratio = 6; + } else if (ratio <= 8) { + *mclkdiv = WM8940_MCLKDIV_4; + ratio = 8; + } else if (ratio <= 12) { + *mclkdiv = WM8940_MCLKDIV_6; + ratio = 12; + } else if (ratio <= 16) { + *mclkdiv = WM8940_MCLKDIV_8; + ratio = 16; + } else { + *mclkdiv = WM8940_MCLKDIV_12; + ratio = 24; + } + + return f_out * ratio / 2; +} + +static int wm8940_update_clocks(struct snd_soc_dai *dai) +{ + struct snd_soc_component *codec = dai->component; + struct wm8940_priv *priv = snd_soc_component_get_drvdata(codec); + unsigned int fs256; + unsigned int fpll = 0; + unsigned int f; + int mclkdiv; + + if (!priv->mclk || !priv->fs) + return 0; + + fs256 = 256 * priv->fs; + + f = wm8940_get_mclkdiv(priv->mclk, fs256, &mclkdiv); + if (f != priv->mclk) { + /* The PLL performs best around 90MHz */ + fpll = wm8940_get_mclkdiv(22500000, fs256, &mclkdiv); + } + + wm8940_set_dai_pll(dai, 0, 0, priv->mclk, fpll); + wm8940_set_dai_clkdiv(dai, WM8940_MCLKDIV, mclkdiv); + + return 0; +} + +static int wm8940_set_dai_sysclk(struct snd_soc_dai *dai, int clk_id, + unsigned int freq, int dir) +{ + struct snd_soc_component *codec = dai->component; + struct wm8940_priv *priv = snd_soc_component_get_drvdata(codec); + + if (dir != SND_SOC_CLOCK_IN) + return -EINVAL; + + priv->mclk = freq; + + return wm8940_update_clocks(dai); +} + #define WM8940_RATES SNDRV_PCM_RATE_8000_48000 #define WM8940_FORMATS (SNDRV_PCM_FMTBIT_S8 | \