Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp707933pxb; Tue, 5 Apr 2022 19:41:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw/2ht/XaD0FRaqqrOviK+UD36fwfRr12mkh7+RnsgIHfF4FtOGgLIlMg1pNblNUbDqvFr7 X-Received: by 2002:a17:907:1ca7:b0:6e0:346:ca6 with SMTP id nb39-20020a1709071ca700b006e003460ca6mr6046348ejc.311.1649212890293; Tue, 05 Apr 2022 19:41:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649212890; cv=none; d=google.com; s=arc-20160816; b=pOyb2mpoPphTD/8vz/PfWo+ldSr7c9YUcPEmG5YAYkmQ6DU6kuc/LeHGMjN492vZZo IX4Q16/4WN+P5Sf6gONga5vNNKlLPYNwgDByrnoyDgvwAGR2VWOgkZT2gLBBg/OzGMRh 7wB4gNoBKfl7M419b4merfoZQkoWU8jWe2kkb3xpr6elgIhrwXl4CdXFzVgPCMQHDhMQ QiJ+86UM1EwiE9LtZ85+VQ7djWTjCwQmhoNrvbJfMsHThXGQ2XKHf9C70/7NOAl8Jyuj ZqPt6fWeYkxxgOkMsklB6q2bsVlfBpqmmCIDn8rfEMRqAeWkBX0Z/rSxn/Cj4g1SSDHK dahQ== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=EMZhOyHDvp1wDBMG4F7hx19IQ33hwsa7a7TmfFJJvwY=; b=wvNdDjeC0ChywlCXzgSiYhz6bjoHS4b5kUbSXrL5ujast2Vh7A982U14AxGStDcJMB H9Q+xyGTt1E2BK7NIRHhvn89Qi4CYWNwk4HJ9sIAGfrjg1SNXsgRsJj+QiBWSzNluKaR ZbI8X+2poodcPQjorIvzyfPUd4ZAZ+7HEsGu7NSc2PJVk/TcY5P5JgNSJ0UZ7c/d9SDB e9VkS9/sV/mKyDnjgnW1VtVhl/TcYqJYopYFMBX0GxeHPH28FSQhY27p88hjEbSZ+TwZ BNQuF39QfJVPapNauc9A4/V7kOcY/ETdj+mb2klo5ZIGJVsGUoQCDhkAyAmoeMAJ3Mqe K8Bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=h7hhs7L7; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id nd29-20020a170907629d00b006df76385bb5si12790831ejc.85.2022.04.05.19.41.00; Tue, 05 Apr 2022 19:41:30 -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=@linuxfoundation.org header.s=korg header.b=h7hhs7L7; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242670AbiDEJhv (ORCPT + 99 others); Tue, 5 Apr 2022 05:37:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34720 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239439AbiDEIUF (ORCPT ); Tue, 5 Apr 2022 04:20:05 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C68AA939A7; Tue, 5 Apr 2022 01:13:19 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 5E102B81BAC; Tue, 5 Apr 2022 08:13:18 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A2428C385A1; Tue, 5 Apr 2022 08:13:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649146397; bh=k2E3ZEIeZsl/3Wg7OEWzS+F8ELAOmyjo1IPyJCH3edU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=h7hhs7L7j3C5RRUf8zwQe3p7TyDnbHXOIHX/a0SJvkgBNC2tnPhfvyNnfYznQ9d56 KAPRvBwfuda4bCsIj5HELKh9PI28FjTQYhWGTBZIMmmHUtY6YEXyRMHrBZ1NeMEhia w59Jt76bVAcFKgGMXNxe5GtOi37dB8u45HKz4c5Q= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Emil Renner Berthing , Stephen Boyd , Sasha Levin Subject: [PATCH 5.17 0748/1126] clk: starfive: jh7100: Dont round divisor up twice Date: Tue, 5 Apr 2022 09:24:55 +0200 Message-Id: <20220405070429.538925033@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070407.513532867@linuxfoundation.org> References: <20220405070407.513532867@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 From: Emil Renner Berthing [ Upstream commit 40dda3532f903107a063cd6ec1f15e10dd0eccc5 ] The problem is best illustrated by an example. Suppose a consumer wants a 4MHz clock rate from a divider with a 10MHz parent. It would then call clk_round_rate(clk, 4000000) which would call into our determine_rate() callback that correctly rounds up and finds that a divisor of 3 gives the highest possible frequency below the requested 4MHz and returns 10000000 / 3 = 3333333Hz. However the consumer would then call clk_set_rate(clk, 3333333) but since 3333333 doesn't divide 10000000 evenly our set_rate() callback would again round the divisor up and set it to 4 which results in an unnecessarily low rate of 2.5MHz. Fix it by using DIV_ROUND_CLOSEST in the set_rate() callback. Fixes: 4210be668a09 ("clk: starfive: Add JH7100 clock generator driver") Signed-off-by: Emil Renner Berthing Link: https://lore.kernel.org/r/20220126173953.1016706-2-kernel@esmil.dk Signed-off-by: Stephen Boyd Signed-off-by: Sasha Levin --- drivers/clk/starfive/clk-starfive-jh7100.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/drivers/clk/starfive/clk-starfive-jh7100.c b/drivers/clk/starfive/clk-starfive-jh7100.c index 25d31afa0f87..db6a4dc203af 100644 --- a/drivers/clk/starfive/clk-starfive-jh7100.c +++ b/drivers/clk/starfive/clk-starfive-jh7100.c @@ -399,22 +399,13 @@ static unsigned long jh7100_clk_recalc_rate(struct clk_hw *hw, return div ? parent_rate / div : 0; } -static unsigned long jh7100_clk_bestdiv(struct jh7100_clk *clk, - unsigned long rate, unsigned long parent) -{ - unsigned long max = clk->max_div; - unsigned long div = DIV_ROUND_UP(parent, rate); - - return min(div, max); -} - static int jh7100_clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req) { struct jh7100_clk *clk = jh7100_clk_from(hw); unsigned long parent = req->best_parent_rate; unsigned long rate = clamp(req->rate, req->min_rate, req->max_rate); - unsigned long div = jh7100_clk_bestdiv(clk, rate, parent); + unsigned long div = min_t(unsigned long, DIV_ROUND_UP(parent, rate), clk->max_div); unsigned long result = parent / div; /* @@ -442,7 +433,8 @@ static int jh7100_clk_set_rate(struct clk_hw *hw, unsigned long parent_rate) { struct jh7100_clk *clk = jh7100_clk_from(hw); - unsigned long div = jh7100_clk_bestdiv(clk, rate, parent_rate); + unsigned long div = clamp(DIV_ROUND_CLOSEST(parent_rate, rate), + 1UL, (unsigned long)clk->max_div); jh7100_clk_reg_rmw(clk, JH7100_CLK_DIV_MASK, div); return 0; -- 2.34.1