Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756830AbdCUKKR (ORCPT ); Tue, 21 Mar 2017 06:10:17 -0400 Received: from mail-he1eur01on0081.outbound.protection.outlook.com ([104.47.0.81]:48002 "EHLO EUR01-HE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1756536AbdCUKJx (ORCPT ); Tue, 21 Mar 2017 06:09:53 -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 v2 1/2] ASoC: codec: wm8960: Refactor sysclk freq search Date: Tue, 21 Mar 2017 12:09:35 +0200 Message-ID: <1490090976-25877-2-git-send-email-daniel.baluta@nxp.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1490090976-25877-1-git-send-email-daniel.baluta@nxp.com> References: <1490090976-25877-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: DB6P193CA0014.EURP193.PROD.OUTLOOK.COM (10.175.237.24) To AMSPR04MB502.eurprd04.prod.outlook.com (10.242.107.147) X-MS-Office365-Filtering-Correlation-Id: 9b3611ff-2ea3-4e88-70ad-08d470426706 X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(48565401081);SRVR:AMSPR04MB502; X-Microsoft-Exchange-Diagnostics: 1;AMSPR04MB502;3:cZYAJhj0EoOIA+NJOp+Vw5oHMz7QK819MbnPSo7ERjizfr/yayn3UGoHdo3Gnm05P5jVWg1APCymUZopB9FrD8EBYFIjdMoFXlhIXYRTRr8NGlbZoF476gRo7Yc016snmcE1lEiF47KamVUFg1FPYX1XbMfeqH3b3lm3xY4cWGzdJ/lZOxC4QLdqXUjsiYuSDNvWvv3jMhH1oA8vJ0Bkv4hNL2k5fvd+ulVDtSDFA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XILqKl5wW+mzw4eMxsn6PSKJtmV/iqHsfXdlKnv266++IoGxYJKsc0MisxMetugIVMmbPI5vdLXJuK1Jee32cvdk9f/sFAsx8t0iw7/z8+/uHgVe1anPKJ4X9b2E8f2vCBWWx4ofcWbzwKZFcTe39aQAVyo+aYeQjTjUqj2SeV9xkEggXnFkQtBV4ycAv92qSBTu3D66Iy0sCeqELQPW24IfDbUUQW2ZqoSab/B4ZpwQwKwoC32jMbD4Rq3pZNaoeEqj6dqrvYVr/zJh0uiyAFOI697eKD2Rnj5/reDgNWi20 X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(185117386973197)(17755550239193); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040375)(601004)(2401047)(8121501046)(5005006)(10201501046)(3002001)(6055026)(6041248)(20161123562025)(20161123564025)(20161123558025)(20161123555025)(20161123560025)(6072148);SRVR:AMSPR04MB502;BCL:0;PCL:0;RULEID:;SRVR:AMSPR04MB502; X-Microsoft-Exchange-Diagnostics: 1;AMSPR04MB502;4:ap9o1OWxTy05y1JUYU5KDWzINKnoLHw1HJsnYKg9OGrN/hMTjTVWCJ0ENMZMc/Yuckc9wH/eEBns9hfRLTaZzzZ0byW7ybdAWT9ptHYYmFFhCrs2Rx06Jpc6+IZgkM7gBlpdg77YYnFk5jCj7/lb6TLDF0OlhYKCrYpsZVWBCKZ+2oK/hSz3L5B0BZmVfGcdfAltatupZSvh8xx/D5YEm7whX5Ep2VZahPxhqnFpHWvlKJR+/xagaMP8JKkz1+hffFlojwMN4Wmk7RxzwRW1iWzEUlsIU6X78kz0ONOmOKp8KH804hrR+XJJ8uV33+l9IOrznZ/BdTI94zFp+QuQX+aUAN/pYrat8VhJL74hqDMgPIId9gIDMHDfa8601slLTZkPjoS0v1bOHSExWpAd98I4RBtlZpcoig7QR09bAVUalXqHfb+jiU9cQSNnxa3g5ZjuhZIGv7SVOF/xDTInKseybss1w6nWvDmaG7f05YzYcnJFdPoE+KkdyveXu6sP6Cs16MlQy7b3NSvHR0ph4YwYoEJRc/6QamCWA3zsQHDmpp8rhWduIXARWxRY+p/h4BwECBVcNYOcikdycG+p/UuUzzY1Q8HWsFNlQ/jDfTBT9vFF6sBTWmoVHU2SuJK+0dKmVUBpZpXxrQQXp1NnF7qSsmp/Vm1NahJYkfs0PnnkFlC5bw+Zzz1R2fvt711j/zQx4RDw2q6B4pQ4/ZuYJg== X-Forefront-PRVS: 02530BD3AA X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(6009001)(39860400002)(39840400002)(39450400003)(39850400002)(54906002)(6512007)(6486002)(5003940100001)(6506006)(53936002)(33646002)(48376002)(50466002)(7736002)(2950100002)(6666003)(6116002)(42186005)(2906002)(305945005)(3846002)(25786008)(66066001)(47776003)(86362001)(36756003)(50226002)(8676002)(81166006)(2201001)(189998001)(4326008)(5660300001)(50986999)(76176999)(38730400002);DIR:OUT;SFP:1101;SCL:1;SRVR:AMSPR04MB502;H:fsr-ub1664-118.ea.freescale.net;FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;AMSPR04MB502;23:uS19n/Ir5h8XRx4opwIq15QAFQ+1LxSEYYknvgh3aP?= =?us-ascii?Q?v7fw7lACuq4dBSxPoGLMWrvWvN843K8CtiqSGYs6Ds7D4IB5DVJUrhWlQrFZ?= =?us-ascii?Q?favAbfe7I8SbLTbPp4lAxdwdezbtIJG32pOSKsNhF/r7DzBnzwalMPLU0XIn?= =?us-ascii?Q?UQWDO3td3m1HkK4N6LcaVfQ7+r0ehd8h1P2iplyEXMC2qRQ2oUwXs8nZ/Z46?= =?us-ascii?Q?QRpSri31ytP9dXG21Ihb5y6qou/ftxwYaM+0QsDJzT5h3mxUx7TmfsxSxfrb?= =?us-ascii?Q?JG1fWDZSG4afFS72ksk7dYJyGX0fpfQd+D0nbdy5wtJqs3KZ8/nXmIcTWCDo?= =?us-ascii?Q?4hELjGc25BFLo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FeQE9RxHuUAk2IcihIWIKRuXtQOw4A4TSQsHtyWJnAj3TSNCca2+/KK3ipQG7zvb+G+3QdGdREM36Ipzm1LNmfZZBXuEHxT214vcHegRdf+pO2L/fcbaEJnAGPHHNLw0+eUSJFFUsxVE440CKUAjbMrMpm4Sw=;5:kfxvNwGHhDMR1+oFQmQvO+KhDlK/lE1iIW/vunXIR+h3MjY7iVkd7xz86sIafCW9D/EF28jKwdMyJcdTcDZhldcZg7qNr511/WnZb1y+KIf9SFjdjjvQuwsoEcTSG5vP4DtZez4sa6/L/FwgvUV/QA==;24:LfhUQDjNvKRufPrUsfyzoDHl7G4n8u7B0/GkL77Q5QGokE7DtJ86Y/5ZHwSSLojL8bSx85S93QurPFqjFfGZy4mzyooFgVId4SvkVfY24rQ= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;AMSPR04MB502;7:aQoqerX4iq2WWPXMA5JOFrCJj5WmQ5IQekiVv1eutvsI6xEXK8qpzW+YUn3a6uV3rpn/MFsG6XABfCkOws+8NFdhihztF3cWqUPGUeFYAghMaZCcuSlEeaqcYRCKE/60c30A1cOWG1f+vAYKAMp36H0++9Apb3Ozn2L/81Wy5qy7M+XWTYwDX2EuYWVvY/JOH2NPDDiDBvelJQvQChzqd9KzPhabk29lyUHulENCIvm+78wy209qgdRnIkr2wRnTjFjyZfxWuAdz1HbLm7WN2aNzW75jUYwlQJIPflbXYuHbagSRdB7I2xP5TiknpAUDgzqvfwiEK6hdk+7DFb0uWg== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 21 Mar 2017 10:09:45.4051 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: AMSPR04MB502 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3655 Lines: 123 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 --- Changes since v1: * made wm8960_configure_sysclk static * renamed i, j, with sysclk_idx, dac_idx, bclk_idx * added brackets to if() branch * kept the update of (sysclk_idx, ...) inside "for" loop because next patch requires (sysclk_idx, ..) to be saved inside loop 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