Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757895AbdCUPD6 (ORCPT ); Tue, 21 Mar 2017 11:03:58 -0400 Received: from mail-ve1eur01on0065.outbound.protection.outlook.com ([104.47.1.65]:33696 "EHLO EUR01-VE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1756711AbdCUPDo (ORCPT ); Tue, 21 Mar 2017 11:03:44 -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: [PATCH v3 1/2] ASoC: codec: wm8960: Refactor sysclk freq search Date: Tue, 21 Mar 2017 17:03:24 +0200 Message-ID: <1490108605-20538-2-git-send-email-daniel.baluta@nxp.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1490108605-20538-1-git-send-email-daniel.baluta@nxp.com> References: <1490108605-20538-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: AM5PR0201CA0015.eurprd02.prod.outlook.com (10.169.248.25) To DBXPR04MB509.eurprd04.prod.outlook.com (10.141.233.12) X-MS-Office365-Filtering-Correlation-Id: ea03dc9a-d74e-4386-2983-08d4706b745c X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(48565401081);SRVR:DBXPR04MB509; X-Microsoft-Exchange-Diagnostics: 1;DBXPR04MB509;3:e5driQXtkfpuE9dbrQVb+5IcpfJABzTTGlE/5YUUEyz7I/DRlhybnFEn2rqMEzvHt+iXR+XPnf+jzwsdO97/kPS8TsZVZvc80THEERIuQ5ok/qeOQxs2/EfNqnmkwYMtm+84XNO9/oBHIc3gTscw7zmj/GGMAyotqJixhYishMpLTZoXwsjxk+M/RSsZ4GWE7ZWfpRah9kOIZeqjJJJ50h/CX5y7LA5HKsx1fsdwz2t2ubn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Kv0VaqlHI7pt5Raq9OweE8qf3QA8G8CS4GzFtFU5/bVo57Fdek/yZ5EtFWhR7zgnYEE+8aGdsSGW5D6ChWfbq/z8XZBzrERl5QIQHBNRupOHivM0elvfMJHRPLzSLCidL4pr7U5SaoSVo4gRaNY5B9/11beAq8QbboYaDs3mykHppob3EDnvUnn0tTwmLJxT1eHhbA3RlEs5ktUpYZOve40VScLMP3rQvI7StPtXMhey0Tx6p26CVEIP7ZeC4dduyXtEUF2SdcNG 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)(20161123558025)(20161123564025)(20161123555025)(20161123560025)(20161123562025)(6072148);SRVR:DBXPR04MB509;BCL:0;PCL:0;RULEID:;SRVR:DBXPR04MB509; X-Microsoft-Exchange-Diagnostics: 1;DBXPR04MB509;4:RGhPWam2Iy08JnMryQML7+8JWaLKOuq59P8uQIxRyQTCvcZQcH/ieMjMRgseDfybnhv6JOj3DIk2skIEcZA86Fpm8qFwyAehZms1UV+/RXjWY99RS809LhpKWKLVZ5OogSuA28QL5xxKv3F/HSY3duaGNPIfYtrUpSJEwcYEoV6D0fWXHRcgiQVMcT4LWTxkMBxk+sZPvX4mwTAWKURAvkkRO52dtuYw09Y9byXk88LIZEoc85hIDGr3q0UOhtQzMCTEe/nAERA9mVGncbY4RhCnxUEnpMPwjZbgYJchjijvk7T5at8lqtFAh3hN7oB7R3XwC5u+rZdVAFsI3Z6GNFKjgvcvGw9HcUZarGTp/QDuIAWxC3gUKcXtSf0pkpGu/hy2Z/FNln5Z7Vm85YjFtGQQA9HWzd/JsvYsvKT0eHrBhEtK4KuA5QBVwLbm332ZWJu+v7ESdG9krKLSFW53pME5Htb922i4EAkbAPBXjnk17a2eYoxVRCQFPLCjnFgQ2zJn1ugkxtZEn8lpw2nUggczxlj5yzytmlC5m4CJe0aoUKMwz8sVYiv2I4sT40DXCI7hi2elUOsiDKOY6vaVWk/DjYRsz+PL3BG8fsCxZ/t3fbZ8rVHg+EcVctC/bIzAanx6HEyXMyTrn1Feju4oIERMGESHMCTgTSz+ZHm/UDg= X-Forefront-PRVS: 02530BD3AA X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(6009001)(39840400002)(39860400002)(39410400002)(39850400002)(39450400003)(53936002)(189998001)(81166006)(38730400002)(33646002)(305945005)(8676002)(25786008)(48376002)(6486002)(6506006)(7736002)(42186005)(76176999)(50986999)(6512007)(54906002)(50466002)(5660300001)(36756003)(47776003)(66066001)(3846002)(2950100002)(2906002)(86362001)(50226002)(2201001)(6116002)(5003940100001)(6666003)(4326008);DIR:OUT;SFP:1101;SCL:1;SRVR:DBXPR04MB509;H:fsr-ub1664-118.ea.freescale.net;FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;DBXPR04MB509;23:IhzUsy3e1EOiAxoe5iKxUcyghyfqFkUGPiC/xlCaER?= =?us-ascii?Q?CXxX6BX9fuayrbzEYK0AWWVjqRmK8kS7gkXa8MHIs7wQSbE/q93SjtlZ/Ely?= =?us-ascii?Q?GXbahBNFs1o+Tfs4/Bb+j73cVJm0TridLOmRbkRASEIMRwOjGvchAQS22lkR?= =?us-ascii?Q?zqEOOPswMoL8ISzYWXnCX8Vl1IzWbXubOuunNhi6lkTz6oNUamUSPANw78cy?= =?us-ascii?Q?p7Y5i6NrAu9c6sooPp1Eb7dsZWzufuvsRhmzQlIj8z0uwVSa0wOmdYNsv3a3?= =?us-ascii?Q?OsM9eHYmPxyqSG+YwnRNDcVWkoGduoVW/+KgsvJHsuCswXjLgTGM7vBELfxB?= =?us-ascii?Q?3OGzAv8g0DwfaxSm1GWw0pCz2iUdSlLCQmEwygE+mYEp4AKErLINyPQvAn6u?= =?us-ascii?Q?LCAc/MFyz2Gf686zQlrRFT4RQWlQUVIZ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JD945BBkLW0s9WEsegkWCV3c8NHqXE2s4kyJ9aYfM3BaCdusnRyGhWX1DzdxVYC+emD39MYlKp+xcqSOkZiAfDjpmqk3UPQWUsBzvIQeoU66sTVTvT4o=;5:tRGxbeKp5xZ1jjW4I1YdbA20/q68O3wJr/7prCCIG0hbZh6dmvBlEqpeNtT48Bl0DSiZ0qNgvkjH9jsu9U/xRb9bO+1Qu64MqTfsWVgXBZw8xG4fOanNoKKvky+V91CWUgGm58A1a60pq6IbPPR3pA==;24:Xft1zoXHuL6A6Ue+GOFNNYpE2rW3gz8y7H1/bbwD4/1kmr4HMSkCad2SUzz+bdx892yr2B9Hth8ViZy+NzpCCD8HkZ7/hd/jMNRPG/uR46c= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;DBXPR04MB509;7:QeFeVbJwZZY2tEV3dwFwQ2h6xfmRsm4chKuWucJ02b2fQMEzMBNpsQF6sznhZoy/WB7AqDRm/ySWiGce4C1ekPCbG52ezX6D6wJhG8Hs8+eLgv0dw7h6ewB+ZUjPqsApErJWf4DOhmngbeNvGTEfOGTSY4FekNKRfxGKlpqRQTCqzPhWnTSTkff/icovcRVV0VhI0+sxu7BSUny0FK8YajM20/pR4QgjAyDI1/E9XQoGxbWzq5RHMZK8d6j+PIsDAOXwCXeG25vjIqxP4CQnkWaRwhF8pcZezTSluOHNPbN/0+eRXHJNLtWB9+Uo2amY2suW3pIGgeo0r4lfbHtxzA== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Mar 2017 15:03:37.3644 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DBXPR04MB509 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3473 Lines: 119 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 Acked-by: Charles Keepax --- Changes since v2: * none sound/soc/codecs/wm8960.c | 80 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 61 insertions(+), 19 deletions(-) diff --git a/sound/soc/codecs/wm8960.c b/sound/soc/codecs/wm8960.c index 3bf081a..25a4a11 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 + * @sysclk_idx: sysclk_divs index for found sysclk + * @dac_idx: dac_divs index for found lrclk + * @bclk_idx: bclk_divs index for found bclk + * + * Returns: + * -1, in case no sysclk frequency available found + * 0, in case an exact (@sysclk_idx, @dac_idx, @bclk_idx) match is found + */ +static +int wm8960_configure_sysclk(struct wm8960_priv *wm8960, int mclk, + int *sysclk_idx, int *dac_idx, int *bclk_idx) +{ + 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) { + *sysclk_idx = i; + *dac_idx = j; + *bclk_idx = 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,25 +702,8 @@ 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) { dev_err(codec->dev, "failed to configure clock\n"); -- 2.7.4