Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754028AbdCOPeK (ORCPT ); Wed, 15 Mar 2017 11:34:10 -0400 Received: from mail-eopbgr10045.outbound.protection.outlook.com ([40.107.1.45]:55018 "EHLO EUR02-HE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1753312AbdCOPdV (ORCPT ); Wed, 15 Mar 2017 11:33:21 -0400 Authentication-Results: gmail.com; dkim=none (message not signed) header.d=none;gmail.com; dmarc=none action=none header.from=nxp.com; From: Daniel Baluta To: , , CC: , , , , , , Subject: [RFC PATCH 1/2] ASoC: codec: wm8960: Refactor sysclk freq search Date: Wed, 15 Mar 2017 17:33:05 +0200 Message-ID: <1489591986-6302-2-git-send-email-daniel.baluta@nxp.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1489591986-6302-1-git-send-email-daniel.baluta@nxp.com> References: <1489591986-6302-1-git-send-email-daniel.baluta@nxp.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [192.88.146.1] X-ClientProxiedBy: HE1PR0701CA0064.eurprd07.prod.outlook.com (10.168.191.32) To DB4PR04MB507.eurprd04.prod.outlook.com (10.141.239.149) X-MS-Office365-Filtering-Correlation-Id: 271713a1-92bd-45e0-2461-08d46bb89aab X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(48565401081);SRVR:DB4PR04MB507; X-Microsoft-Exchange-Diagnostics: 1;DB4PR04MB507;3:0UXYt/w+adq+rtGy6vxm+tTTROOiecqQoInciHi+7uOMoWG8ngBElOX1XM7MPY/UUYRecc8r559jRCQmsEdIgfyAEKeDX7SdMenFqZPqjq1TO4CO1ooN8NTvzjznRmDwvog6m3j5dGCDFdDaukCKww8P8PCnw7sWw6SK4oUFUsgUJjSGUXknjjWz3GAaCzzgU+33fY6CHYdkEbYlh0mpOs6HITVVfxXFAxwF01+9/iGSIEUAmaTPrLf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HAkwlnvXv3jMyWoZ8SbCfko3VdA+M0k9a039IELDV5ZtF18cmDjoy14n7N5Th3QbF96k/7pXCiboier0Q2pYk9dwxJM9iuy1aYIku5hdZCOxkRO+1xDhzLO/suWTIRa5p9NUyuoOT+qt6wECw91gki5b2Faa+oFl3vU410UImW1WwWGBzKEvmVvmHZu7fL/g6l5MTLYBAsNVr5qnXnBj/1JVzckT2DNkCiKHeAXxFUXai62HN8PgUX X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(185117386973197); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040375)(601004)(2401047)(8121501046)(5005006)(3002001)(10201501046)(6055026)(6041248)(20161123555025)(20161123560025)(20161123564025)(20161123558025)(20161123562025)(6072148);SRVR:DB4PR04MB507;BCL:0;PCL:0;RULEID:;SRVR:DB4PR04MB507; X-Microsoft-Exchange-Diagnostics: 1;DB4PR04MB507;4:2HNCl1d+72Q1lA0Ct0jv2BHrtmXxT7sb0qQHpYnG3Z1FwPxqHor1cO0oDWLAUxFH8c1WJEgO1UvRZNJ9iCu0y/PJtfUGHUnstoWAShxIyqWjb9Uk26O4vsQfPQduVyjaNOkhzI1xvvFlrWtd2BgKu9XZ6wVYSbAoh7ivc7bmWrrDG30O/mA97BrGfm5dFmeB6Ey2sujDT9E0KGlpXk9t8kawaBkQQwljmVyzDeaAiXLI07zAql1c7kJ0H0DCg9pTevm04XJKRPPAH1McNRZKMrNs2ZK6dTR+3kshx2O/tb7F8vLiRxWHR6njvNqm1glLic8p8/bqjjKryXPjnWUe3ZCqwxff9gb7A3Ln+nbGDnzOInXTnS3q4Spo+GNboG1HdI3lCvYlm/Gv05eZFlmkgPW3DFHPOHBWkn4Au19+g9U3583dCuv0xDwnFqFG3DXmI/E3qpF7RI/5axmDmYE10JxGEH4Od4/VnmJi8KbFG4gc2J9qk0n5xk4piUmyj/r+EHQofWK+42JhM4QbLRl0Q9OZKPZS2eHunLVc9BoznNFgZoJzHmlG1mSo60w6A/VfBwWlLWq9SvyIsUB81mo+XUZ3kOGIr7EUO8oD62uVz/BJTHzLRglNLh/f7RSMl/v4yeRHe07L2+Ceesc4pWa7wTjgOAMf+WY0ZnW2s6/f/ds= X-Forefront-PRVS: 02475B2A01 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(6009001)(39860400002)(39410400002)(39450400003)(39840400002)(39850400002)(189998001)(53936002)(50226002)(66066001)(6506006)(48376002)(86362001)(50466002)(54906002)(81166006)(6486002)(5003940100001)(25786008)(8676002)(4326008)(5660300001)(33646002)(42186005)(6666003)(36756003)(50986999)(76176999)(47776003)(6116002)(2950100002)(38730400002)(7736002)(6512007)(2201001)(3846002)(2906002)(305945005)(217873001);DIR:OUT;SFP:1101;SCL:1;SRVR:DB4PR04MB507;H:fsr-ub1664-118.ea.freescale.net;FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;DB4PR04MB507;23:v90UGU2zMn8qKWSMe0jieLHyzJOuaPgBvly5sJlc71?= =?us-ascii?Q?k/yRgclReke/Xe5OHsm7Kghn3Dt78ZX/hZQiaZOxykvKPOraOv/XsA3bPUKT?= =?us-ascii?Q?QbqyyJkF+JUC8oZFcR7iIh/y1T/f5cIb/kU/6FB+65JkbS7PVvxUaHk4uHqb?= =?us-ascii?Q?q11XE8/uCw41HMJYn2fbS4GgQ00FcAVZdQhtr0Yreen8RKiomQ+hH1elSSec?= =?us-ascii?Q?g2XKyK2cQLfgA3LdPCR7o8wgn0k1aASDnVTYMMAIiA63wgU7JJoTuPQ5Xwyg?= =?us-ascii?Q?6Citg8XZI+HYgyUY7HTy6okUwYN+GrqEbuh7pV6JQ9IxO1jsypoUB/FPLHem?= =?us-ascii?Q?ouhlOFqIVhrl54TEc4Agio5JH/pHGxy7AQ5UmcE/AJrFR6pGs8E+UPB6pHfP?= =?us-ascii?Q?OVLFTy+tStb+XfGH8XYYGCzQf2t3vElSFRZ2xQ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QGsWauvBfDeH4XNEB3bC3lzeMhmg684enGESaHBcLnkFv7bcfvYpy3YySn2Vn/+CvhkoQHz+hTIu6Z2o8b7duxfdlL7Dxi6DpajoAsVMs7Wm3Mym4ZB/CcPnasnxLrS3QLFnx6TTaOWS2Qk=;5:0eaVuduWteQE7u2nPUclJLvy2v+RwQtHals1Z83t6bnni5oTVTK7GEIS/kCQ0ucubbzd6mkToIpmeZfjFlPbXXg+K9yHu8RLCq20NyXXDXnUuUjBmoVZ7GeihjdHL9WxdJjaCeqzvAr0cx3a6z7DUQ==;24:1rAIj96w1d47vqdORR/jyer2nOy/Glx8Yp+WD4PGNiCj5OSk/V/qW7v0pys7Ws6nTqm5xpdXZeaYsGQXYimqK6OGhxvvclwD9ibgvROv47Y= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;DB4PR04MB507;7:B8PVmkBE7FZzX36vhxkEToBt36tBRq2Y9NOPPEFzptBqnP3XbwuT7aCk6i1zVtonF2s4RjZx9etGF5PuoiAwWDcRk9ZK5qOGnHxHCZs046TrbIBOAAQygM96rX2BxUlazg2NasqsJRxz6+lJ5bItpUTEzWxQDLrf4SEk+dLvBG+7MDj4QrnS7WFaRRzi0Y79UJkCk+iBZjAOC50AF3VpahpuizUYXMOukIG1i+WoO3PPAm5h81n2QSHxZQr96A6DIO9cJfHKVpDEom1lRN6Ce8eKkRij7RJaLNbaZYC1sDR83DuboGFlB5j+XMWXhmLbMfq3H2jyndtJoMnWKFf8zw== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 15 Mar 2017 15:33:16.4612 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB4PR04MB507 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3383 Lines: 118 Add a separate function for finding (sysclk, lrclk, bclk) when the clock is auto or mclk. This makes code easier to read and reduces the indentation level in wm8960_configure_clocking. Signed-off-by: Daniel Baluta --- sound/soc/codecs/wm8960.c | 82 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 20 deletions(-) diff --git a/sound/soc/codecs/wm8960.c b/sound/soc/codecs/wm8960.c index 3bf081a..cb2ff2d 100644 --- a/sound/soc/codecs/wm8960.c +++ b/sound/soc/codecs/wm8960.c @@ -604,12 +604,71 @@ static const int bclk_divs[] = { 120, 160, 220, 240, 320, 320, 320 }; +/** + * wm8960_configure_sysclk - checks if there is a sysclk frequency available + * The sysclk must be chosen such that: + * - sysclk = MCLK / sysclk_divs + * - lrclk = sysclk / dac_divs + * - 10 * bclk = sysclk / bclk_divs + * + * @wm8960_priv: wm8960 codec private data + * @mclk: MCLK used to derive sysclk + * @_i: sysclk_divs index for found sysclk + * @_j: dac_divs index for found lrclk + * @_k: bclk_divs index for found bclk + * + * Returns: + * -1, in case no sysclk frequency available found + * 0, in case an exact match is found. See @_i, @_j, @_k + * + */ +int wm8960_configure_sysclk(struct wm8960_priv *wm8960, int mclk, + int *_i, int *_j, int *_k) +{ + int sysclk, bclk, lrclk; + int i, j, k; + int diff; + + bclk = wm8960->bclk; + lrclk = wm8960->lrclk; + + /* check if the sysclk frequency is available. */ + for (i = 0; i < ARRAY_SIZE(sysclk_divs); ++i) { + if (sysclk_divs[i] == -1) + continue; + sysclk = mclk / sysclk_divs[i]; + for (j = 0; j < ARRAY_SIZE(dac_divs); ++j) { + if (sysclk != dac_divs[j] * lrclk) + continue; + for (k = 0; k < ARRAY_SIZE(bclk_divs); ++k) { + diff = sysclk - bclk * bclk_divs[k] / 10; + if (diff == 0) { + *_i = i; + *_j = j; + *_k = k; + break; + } + } + if (k != ARRAY_SIZE(bclk_divs)) + break; + } + if (j != ARRAY_SIZE(dac_divs)) + break; + } + + if (i != ARRAY_SIZE(sysclk_divs)) + return 0; + + return -1; +} + static int wm8960_configure_clocking(struct snd_soc_codec *codec) { struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec); int sysclk, bclk, lrclk, freq_out, freq_in; u16 iface1 = snd_soc_read(codec, WM8960_IFACE1); int i, j, k; + int ret; if (!(iface1 & (1<<6))) { dev_dbg(codec->dev, @@ -643,27 +702,10 @@ static int wm8960_configure_clocking(struct snd_soc_codec *codec) } if (wm8960->clk_id != WM8960_SYSCLK_PLL) { - /* check if the sysclk frequency is available. */ - for (i = 0; i < ARRAY_SIZE(sysclk_divs); ++i) { - if (sysclk_divs[i] == -1) - continue; - sysclk = freq_out / sysclk_divs[i]; - for (j = 0; j < ARRAY_SIZE(dac_divs); ++j) { - if (sysclk != dac_divs[j] * lrclk) - continue; - for (k = 0; k < ARRAY_SIZE(bclk_divs); ++k) - if (sysclk == bclk * bclk_divs[k] / 10) - break; - if (k != ARRAY_SIZE(bclk_divs)) - break; - } - if (j != ARRAY_SIZE(dac_divs)) - break; - } - - if (i != ARRAY_SIZE(sysclk_divs)) { + ret = wm8960_configure_sysclk(wm8960, freq_out, &i, &j, &k); + if (ret == 0) goto configure_clock; - } else if (wm8960->clk_id != WM8960_SYSCLK_AUTO) { + else if (wm8960->clk_id != WM8960_SYSCLK_AUTO) { dev_err(codec->dev, "failed to configure clock\n"); return -EINVAL; } -- 2.7.4