Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932315AbdDDQpd (ORCPT ); Tue, 4 Apr 2017 12:45:33 -0400 Received: from mail-ve1eur01on0080.outbound.protection.outlook.com ([104.47.1.80]:20992 "EHLO EUR01-VE1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S932213AbdDDQp2 (ORCPT ); Tue, 4 Apr 2017 12:45:28 -0400 Authentication-Results: alsa-project.org; dkim=none (message not signed) header.d=none;alsa-project.org; dmarc=none action=none header.from=nxp.com; From: Daniel Baluta To: CC: , , , , , , , , , Subject: [PATCH 1/2] ASoC: codec: wm9860: Refactor PLL out freq search Date: Tue, 4 Apr 2017 19:45:13 +0300 Message-ID: <1491324314-27067-2-git-send-email-daniel.baluta@nxp.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1491324314-27067-1-git-send-email-daniel.baluta@nxp.com> References: <1491324314-27067-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: VI1PR09CA0066.eurprd09.prod.outlook.com (2603:10a6:802:28::34) To DB4PR04MB506.eurprd04.prod.outlook.com (2a01:111:e400:983a::14) X-MS-Office365-Filtering-Correlation-Id: 249d3c49-8ab3-48a9-3b63-08d47b79fdc9 X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(48565401081)(201703131423075)(201703031133081);SRVR:DB4PR04MB506; X-Microsoft-Exchange-Diagnostics: 1;DB4PR04MB506;3:VPtpfu8xH0AaMqVppFCVoF05QQAoUB7WbI/wSMyWSPuHPsUTTgsuKhmlQgxQKF1oc1dtvPtQkN9viJoHBzaj2PzFbz17bWjFN+faLTkudPxlqj42JKobAGChqq3M7+PWhedPsLMr/6VZIEfs9dB7isRZ0B1nvkc8LAllgeO401TJpuMIODvW1r8XwVF8zRu3Ym1/eRv/faSrRjFCvG24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MWcjYmkgxmwfyf/+3D46Ig+Ww96zT1n1hGs/Rr+y5OvsxaukN2lNiLJFoMe6EUVjSbzcX/r+lXymcAO5FpqE04ToqSUPJmPc9OPZYKSkIELTqlZnPPQ1EY9+DC9vUSx2rcyfQNGD1BX68Ipb4aBL8g0PcpuwSLNReQbu7Xuj8EnNNmILI6b3zfaDjQgifTJVfIiIbhuP6F9wUf/3230kI9Of5dJq85Io1i5Frop1Kr9GJMr/U3GuxM0ulM1TQSfzDDNLRRBE+JME4NkWSBJrr5nYTOZ/m/s+Jdj6nyL5sSF5TPQ5u9D37rY+b2Ki9CgEYzHzSAa433OpX20/IMoyswl8XSzvCNO6H1l0lKlRMQ/cz8FN7lE X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(185117386973197); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(6040450)(601004)(2401047)(5005006)(8121501046)(93006095)(93001095)(10201501046)(3002001)(6055026)(6041248)(20161123562025)(20161123555025)(201703131423075)(201702281528075)(201703061421075)(20161123560025)(20161123564025)(6072148);SRVR:DB4PR04MB506;BCL:0;PCL:0;RULEID:;SRVR:DB4PR04MB506; X-Microsoft-Exchange-Diagnostics: 1;DB4PR04MB506;4:bPr3c/UJoUnZoZMP0P7xBL/4uGFXjeTrOuG44NMMKk0gFIVRZV/YZmm++Jv7tiYlq+DrpDdKpXq5O80C7jtK1R+t+qlHyzCBbsHmJ7p9nuNFMnur7BCOcggDjSmmnZDDztyP2xcTkDNMmY/sAkOSzTlkQrEz6eO/SY9/I70wWoIYe1Jd8sXPCDJyVuUoC48ZTxwVYj26QDJeqx8aQI1XrNZoe9ryJ8qTwBfXhheny4ClGnCmaRap/+BlYIVQtbZAQ3kXMiVTNfefq9UNSRl9pHEKxo0MCNRiD+s2oZ/qSYm+nb3dz4vnWx6LSXUw5moYqITiH4lS7YLHlihPGWfSdHjCQ8fjC7l7tbSsMz68Cm2E8pBmIVZwHK2tLaGo7es0W9cxElgzAm1YLKsHkZxrQ3zSsbG4/sNUuJDW4gfcgojLE5inidBs5qM2UjKZ9hBYDu3kUEIe3wzzC4Tw+NMMuTTF6jlTmJ6D3FXXNnv3MrH7gReJaPaZVllSWbiofjou/4p5Zc444hieq0gC5ND4/2D3XXGhWO7KqVDF8BcSqBXNQeVHWbfamGScYwvi8bA+cKOQXMVidF6TZeqps9/ocA33vAGfsIudYdTNmxBjdELEmmiZLCSDYn/2v1yc06iImsSKYhD8cEmET+7Qy4UmreTVbwLaEsIIhy/1KXebmyNeogok3m2FtHzEK//aZw0LKZ2bWNIOmf8iv/bN5k/5e4ymxdxrgnIckriyPUm0ZmuIiekhP2qmUNIjTqWYwQKhpw1NK1ZF/ufPMTeiXwqFRQ== X-Forefront-PRVS: 0267E514F9 X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(6009001)(39410400002)(39850400002)(39400400002)(39840400002)(39860400002)(39450400003)(5660300001)(66066001)(50226002)(6486002)(2906002)(6506006)(6116002)(54906002)(53936002)(36756003)(25786009)(305945005)(47776003)(6512007)(50986999)(76176999)(50466002)(5003940100001)(48376002)(86362001)(3846002)(38730400002)(81166006)(2351001)(8676002)(4326008)(42186005)(6916009)(2950100002)(110136004)(33646002)(189998001)(7736002)(6666003);DIR:OUT;SFP:1101;SCL:1;SRVR:DB4PR04MB506;H:fsr-ub1664-118.ea.freescale.net;FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1;DB4PR04MB506;23:XARqZRZpHSLrxU5gM0Y0JMroH7ucJhqOFMvP4pLCW/?= =?us-ascii?Q?F84BVtEEkXI3l9hnTYiVBn+zCfG/S1p6JRp6My99LNLlNBNF6c7rDj2g0xoj?= =?us-ascii?Q?GmM5CoA1RKZ8zwMRQGA9GyJierGE3azy7V684PQdUKY1LXyIyjI/jaeirxe2?= =?us-ascii?Q?tAyH3UGHpN0gc6hhml6JeNdmKvtVQohH6zK982QuEW7qrn9VTGr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Microsoft-Exchange-Diagnostics: 1;DB4PR04MB506;6:SA+/gFtctl3L2olMzkmv3uZznihnc1KOdsjWVoXYHg4Aqaxdlx88biViErnSPq82Qys/J8IBwWRgV7PF4MENUNcCEeM0RtAtBGXA9zn++VXefvGY0CnnHL5IWX86TN5KeJQySk+PeeTw5CucK8FlHlYcSHDo68jbGhF1XQlbl+u1C3Ck80syE9rqZTWDR9ZonxZtMdOmN8eS2PP2wjCelzxnYBFNPhi0dwe1ryq83RSBRMKGlDSBJIUyYVUNp2C9a/TPGbnrfthOiOHrllJ37viNIX0jdLVHke+3pDMUxuiaG70hxi1FRpy9AORGH70n19lQmHGxgtFi0nDNR/i5q+Q43VT/ikzY6p8smdqUgBrtTp7A+RrmHPlaak62Doo8MxxyyDGo+de3qvZziF4oj93XPcokTPtaiDKqkY98pvI=;5:5auLKxUwEnfi1lJs0nTRcQHuM8Ut2sfCe2+Zoj3RHr+KDNJoA2Bb7Kkk6kjUs2N/9cEEgpthThFjrzydCJHf4t1Y8hq18hkupcMTfUdgPLA/FcY7qOzfPhtznyICU9tnHKDonCyYZoLuPqf51Wt5vw==;24:aaaGVMLP1o+pQv857AidRUiopBDCclyMtjYNtJ67wGgqDmww2qgx6dmZfr6MXaUALIaAzg1mPzYUjJZiOQm8zN1ZErEAd+YsmCZMu2k+gXM= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-Microsoft-Exchange-Diagnostics: 1;DB4PR04MB506;7:Xt4KSfGCX3dZGuaRL9nD3/E/vKsaAPQTLdE5WY+lg2N1vAh4VsmFn/pnzab07U1Sw6OdyQ2CvxlWgF4ho2gdujv7k0ogBQwPR4Vz0vCptuodI872SwzxgMcqauxRC6M2Kwjy2G3ZMq4tIMQn3ZdBajvwTiUx0mQPHx8/f92D2peBh1gY7YF0c1oZlTUNd06660JQMXTJl4PVHe561y9ryCEVMvuprkkudpyJaFbcQoUJeWF00G5QcY/4jz8pM1sKLOOF9vq8zTl/ft3UxiTH1R8PJzvvOHe/y/8RsQOdfrhg5I+F4uUFiVFDLTLoZdY9ojK6Gu2W/UYgF6FP1aE3CQ== X-OriginatorOrg: nxp.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 04 Apr 2017 16:45:23.3295 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB4PR04MB506 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3851 Lines: 132 Add a separate function for deriving (sysclk, lrclk, bclk) when the clock is auto or pll. Signed-off-by: Daniel Baluta --- sound/soc/codecs/wm8960.c | 93 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 64 insertions(+), 29 deletions(-) diff --git a/sound/soc/codecs/wm8960.c b/sound/soc/codecs/wm8960.c index ce159f1..36c8454 100644 --- a/sound/soc/codecs/wm8960.c +++ b/sound/soc/codecs/wm8960.c @@ -672,10 +672,70 @@ int wm8960_configure_sysclk(struct wm8960_priv *wm8960, int mclk, return *bclk_idx; } +/** + * wm8960_configure_pll - checks if there is a PLL out frequency available + * The PLL out frequency must be chosen such that: + * - sysclk = lrclk * dac_divs + * - freq_out = sysclk * sysclk_divs + * - 10 * sysclk = bclk * bclk_divs + * + * @codec: codec structure + * @freq_in: input frequency used to derive freq out via PLL + * @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 PLL frequency out available was found + * >=0, in case we could derive bclk, lrclk, sysclk from PLL out using + * (@sysclk_idx, @dac_idx, @bclk_idx) dividers + */ +static +int wm8960_configure_pll(struct snd_soc_codec *codec, int freq_in, + int *sysclk_idx, int *dac_idx, int *bclk_idx) +{ + struct wm8960_priv *wm8960 = snd_soc_codec_get_drvdata(codec); + int sysclk, bclk, lrclk, freq_out; + int diff, best_freq_out; + int i, j, k; + + bclk = wm8960->bclk; + lrclk = wm8960->lrclk; + + *bclk_idx = -1; + + for (i = 0; i < ARRAY_SIZE(sysclk_divs); ++i) { + if (sysclk_divs[i] == -1) + continue; + for (j = 0; j < ARRAY_SIZE(dac_divs); ++j) { + sysclk = lrclk * dac_divs[j]; + freq_out = sysclk * sysclk_divs[i]; + + for (k = 0; k < ARRAY_SIZE(bclk_divs); ++k) { + if (!is_pll_freq_available(freq_in, freq_out)) + continue; + + diff = sysclk - bclk * bclk_divs[k] / 10; + if (diff == 0) { + *sysclk_idx = i; + *dac_idx = j; + *bclk_idx = k; + best_freq_out = freq_out; + break; + } + } + } + } + + if (*bclk_idx != -1) + wm8960_set_pll(codec, freq_in, best_freq_out); + + return *bclk_idx; +} 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; + int freq_out, freq_in; u16 iface1 = snd_soc_read(codec, WM8960_IFACE1); int i, j, k; int ret; @@ -692,8 +752,6 @@ static int wm8960_configure_clocking(struct snd_soc_codec *codec) } freq_in = wm8960->freq_in; - bclk = wm8960->bclk; - lrclk = wm8960->lrclk; /* * If it's sysclk auto mode, check if the MCLK can provide sysclk or * not. If MCLK can provide sysclk, using MCLK to provide sysclk @@ -720,33 +778,10 @@ static int wm8960_configure_clocking(struct snd_soc_codec *codec) return -EINVAL; } } - /* get a available pll out frequency and set pll */ - for (i = 0; i < ARRAY_SIZE(sysclk_divs); ++i) { - if (sysclk_divs[i] == -1) - continue; - for (j = 0; j < ARRAY_SIZE(dac_divs); ++j) { - sysclk = lrclk * dac_divs[j]; - freq_out = sysclk * sysclk_divs[i]; - - for (k = 0; k < ARRAY_SIZE(bclk_divs); ++k) { - if (sysclk == bclk * bclk_divs[k] / 10 && - is_pll_freq_available(freq_in, freq_out)) { - wm8960_set_pll(codec, - freq_in, freq_out); - break; - } else { - continue; - } - } - if (k != ARRAY_SIZE(bclk_divs)) - break; - } - if (j != ARRAY_SIZE(dac_divs)) - break; - } - if (i == ARRAY_SIZE(sysclk_divs)) { - dev_err(codec->dev, "failed to configure clock\n"); + ret = wm8960_configure_pll(codec, freq_in, &i, &j, &k); + if (ret < 0) { + dev_err(codec->dev, "failed to configure clock via PLL\n"); return -EINVAL; } -- 2.7.4