From patchwork Mon May 29 13:34:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Oltmanns X-Patchwork-Id: 13258597 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 A9368C77B7A for ; Mon, 29 May 2023 13:35:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:Cc :To:From:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=9jc2itQIQu5wCyV5/Bgvm5+fRxxEfFTBuE4xe709SmQ=; b=VrXBWF44tONCyD GGQtcGDwEoykNfz9MJjm4yfGYRzL9+MR77UWk50iNUxMCjF5gk8juVipqILPrnpgY/ZjQZ/YRBwUN j3QcpqAlurhJbcysZCHjYmFBXNYNjvV5glld7Ivg2dJvsLwzrvuVicdmMlODqfrBvyue2bWdarVED 06Fu3HCtOQ9oyhEY8lgT1ZhyW216MR/z0zYnTbG2UTKIGu/38dfLm6R6XkNuilTSmSkdwF0AOKnPt VmbHZaw+m/D3AHCbO4slf4BYDr1bUnbis6Kp+qU3GgfMXJ6JNuaq2ri3Ox54FHkcqH/GHM0QIT54W 0jtJmlwZspvUohYyLrdA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1q3d1g-00AcBq-01; Mon, 29 May 2023 13:35:08 +0000 Received: from mout-p-102.mailbox.org ([2001:67c:2050:0:465::102]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1q3d1d-00AcB0-2R for linux-arm-kernel@lists.infradead.org; Mon, 29 May 2023 13:35:07 +0000 Received: from smtp2.mailbox.org (smtp2.mailbox.org [10.196.197.2]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4QVGjW6BrQz9slB; Mon, 29 May 2023 15:34:55 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oltmanns.dev; s=MBO0001; t=1685367295; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=embt6Ls9k2VANJwMs4RIikcIM5ejyhPH8j4QEL/1Chc=; b=pr9UbGDU54G0Xsls/ofB+w5J0fzZb/9S0Um4muqDszOKa1qBqPlPGQiWADhuDz+D0NVbxZ qY9udn/s6sy1N5b/rZN6OQXtu/S5ME2jEhO3SlPx9+lZ3ikiXPCs/eE9m4IY+BTLBTGZzt ziVsW2w3LV2crmzS21t+/JZYcYQH9hVCWSAyGzAGrJ12maXwajgNo4AJKmIqz33+TEw40L wSbuFn4/P6DJeN50rJmPgja+mVnq82y/NLzLnPkbjhe9A1wcUrUoP5oUuY1+Vi3WlCPWSN /0DS+bz2w7Q0C2KNKkHCQy2vcUlwec/yfVsBeBJD7MIWE8DpAwEIqqjhXMJo2A== From: Frank Oltmanns To: Michael Turquette , Stephen Boyd Cc: Frank Oltmanns , Abel Vesa , Peng Fan , Shawn Guo , Sascha Hauer , Pengutronix Kernel Team , Fabio Estevam , NXP Linux Team , Jacky Bai , Anson Huang , linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [PATCH] clk: fractional-divider: Improve approximation when zero based Date: Mon, 29 May 2023 15:34:33 +0200 Message-Id: <20230529133433.56215-1-frank@oltmanns.dev> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230529_063505_996732_1433328E X-CRM114-Status: GOOD ( 11.40 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Consider the CLK_FRAC_DIVIDER_ZERO_BASED flag when finding the best approximation for m and n. By doing so, increase the range of valid values for the numerator and denominator by 1. Cc: A.s. Dong --- I stumpled upon this, when familiarizing myself with clk drivers. Unfortunately, I have no boards to test this patch. It seems the only user of this flag in mainline is drivers/clk/imx/clk-composite-7ulp.c, therefore I'm cc-ing get_maintainers.pl --git-blame -f drivers/clk/imx/clk-composite-7ulp.c in the hopes of a wider audience. Thank you for considering this contribution, Frank drivers/clk/clk-fractional-divider.c | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/drivers/clk/clk-fractional-divider.c b/drivers/clk/clk-fractional-divider.c index 479297763e70..7da21cd2bdb1 100644 --- a/drivers/clk/clk-fractional-divider.c +++ b/drivers/clk/clk-fractional-divider.c @@ -123,6 +123,7 @@ void clk_fractional_divider_general_approximation(struct clk_hw *hw, unsigned long *m, unsigned long *n) { struct clk_fractional_divider *fd = to_clk_fd(hw); + unsigned long max_m, max_n; /* * Get rate closer to *parent_rate to guarantee there is no overflow @@ -138,9 +139,15 @@ void clk_fractional_divider_general_approximation(struct clk_hw *hw, rate <<= scale - fd->nwidth; } - rational_best_approximation(rate, *parent_rate, - GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0), - m, n); + if (fd->flags & CLK_FRAC_DIVIDER_ZERO_BASED) { + max_m = 1 << fd->mwidth; + max_n = 1 << fd->nwidth; + } else { + max_m = GENMASK(fd->mwidth - 1, 0); + max_n = GENMASK(fd->nwidth - 1, 0); + } + + rational_best_approximation(rate, *parent_rate, max_m, max_n, m, n); } static long clk_fd_round_rate(struct clk_hw *hw, unsigned long rate, @@ -169,13 +176,18 @@ static int clk_fd_set_rate(struct clk_hw *hw, unsigned long rate, { struct clk_fractional_divider *fd = to_clk_fd(hw); unsigned long flags = 0; - unsigned long m, n; + unsigned long m, n, max_m, max_n; u32 mmask, nmask; u32 val; - rational_best_approximation(rate, parent_rate, - GENMASK(fd->mwidth - 1, 0), GENMASK(fd->nwidth - 1, 0), - &m, &n); + if (fd->flags & CLK_FRAC_DIVIDER_ZERO_BASED) { + max_m = 1 << fd->mwidth; + max_n = 1 << fd->nwidth; + } else { + max_m = GENMASK(fd->mwidth - 1, 0); + max_n = GENMASK(fd->nwidth - 1, 0); + } + rational_best_approximation(rate, parent_rate, max_m, max_n, &m, &n); if (fd->flags & CLK_FRAC_DIVIDER_ZERO_BASED) { m--;