Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp3197623rwd; Mon, 29 May 2023 07:06:03 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ7MLEELCADx8mrp9bHX2bCtelXtCYu/5uviu3P1eidENuovnPKDNBD31gub+S7j6BlUEd7u X-Received: by 2002:a05:6a20:914e:b0:10c:37ed:3e88 with SMTP id x14-20020a056a20914e00b0010c37ed3e88mr8485644pzc.23.1685369163376; Mon, 29 May 2023 07:06:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685369163; cv=none; d=google.com; s=arc-20160816; b=APYuoI1Gpi+je72cArr0K9xRnNSrs1DcuLwU03LoFDMGw8Mu6IddMW0yu45k+l1ZwF Vu9kxtUN/g8hlh1Pi5QrA9q89pW4eHWL5nuiNrAyf8TL55tJi9Ug2OHlXDDpvdeP7By8 UJjp7lYT6t/WEEJbtVoBNrzlLULB95vVjf9f5/xdrcgN2AAP4K2qCY6Qx2BDkUHVCgDC Ez3TXJK6bKVu8pj79KLnwZxTp1Xg5DVtaRNvqPV5Mvzu/jymeyCKsAnUaRIPsBgSXDuf VfUYD662N+rifixwZH+eldcfYsoR/jBp0Z89vymJD1nWbFNaElII2RHnDox6ggY7VRJI JgiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=embt6Ls9k2VANJwMs4RIikcIM5ejyhPH8j4QEL/1Chc=; b=mRIF/QNZ3nAnfLLASdK+69EjR2VE0E7zksf0Celitjjlk7FDK21IfLfdVyhN1O8kY5 XOzbtDSPruvKsOltWIRtzCTVX4K7R44d/iYCgOYLQEO7WprYf7J4rj2doB0ZwNcCwPht Mmkcexsb3PAiKeTvqMdHArLPJs53sKndnyb9czpOhtrtMU5NE9tPr23BMJrvyU++mleO w0L0tVa/5YuGAdqBqpic55OYc8bflJs/sEAIPmf2mcMcN9zfQsphv3+O7ZvfSBfFUbYa NatvFD/ufahWUEBjBt7wrTlYPqPSCSXr3LNk4fDwpRo7Upv7bztgs2+idTG6dTqgdfh/ XBaw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@oltmanns.dev header.s=MBO0001 header.b=pr9UbGDU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oltmanns.dev Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g7-20020a17090a640700b00256a7c340b1si133164pjj.3.2023.05.29.07.05.51; Mon, 29 May 2023 07:06:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@oltmanns.dev header.s=MBO0001 header.b=pr9UbGDU; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=oltmanns.dev Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229632AbjE2NfI (ORCPT + 99 others); Mon, 29 May 2023 09:35:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54560 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229601AbjE2NfG (ORCPT ); Mon, 29 May 2023 09:35:06 -0400 Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0CA1F90; Mon, 29 May 2023 06:35:03 -0700 (PDT) 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 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.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--; -- 2.40.1