From patchwork Mon Aug 7 12:43:35 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frank Oltmanns X-Patchwork-Id: 13344121 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 1906BC001DF for ; Mon, 7 Aug 2023 12:44:37 +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:Cc:To:In-Reply-To:References:Message-Id :MIME-Version:Subject:Date:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=QPCdDcr0xMxQQQBbUfp1qiApqeilLzG+XHIvigY/cIc=; b=yQtEpQ+xtQYiz6 zHQU5/xMlCdaSC4MkzTvfNfOKOjO6F1vt4vAvcMjKFQHNWCeiCEqzem6kCTcOfaRK4I0nBh/qgaDD aM39btYyr60yFLvZMNPIsI4E0kRkkZSyimXYPkF0U2Jv46hLRtPXXtrLQrESAGdqFIKkYUS8qrlUS +J2gKUZinkeZLC7GMk1AGDcMpNDtw7zyzXwpPUSYPVqvyLCgyONDjvC81GkqJ93UhWkrUeEi2Qf6S rpfGCKHo40RTn5VdVIplQqPrD64sCEeJKuqKL3M9h3ZkYtRVCN+OgexCunj+kKJsnanOLBcqhbBIp o+28KNLzrZC6zGYicBFQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1qSzal-00HFiV-2I; Mon, 07 Aug 2023 12:44:11 +0000 Received: from mout-p-101.mailbox.org ([80.241.56.151]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1qSzai-00HFh9-1J for linux-arm-kernel@lists.infradead.org; Mon, 07 Aug 2023 12:44:10 +0000 Received: from smtp1.mailbox.org (smtp1.mailbox.org [IPv6:2001:67c:2050:b231:465::1]) (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-101.mailbox.org (Postfix) with ESMTPS id 4RKGGY36Knz9snc; Mon, 7 Aug 2023 14:44:05 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oltmanns.dev; s=MBO0001; t=1691412245; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Y8ktluX8ISdNot5DVuNsCYEDtddUgFalgfYdbHRzW38=; b=v9QFVAcdrssP/cI8ELuc6Z9v/BvV7PtjZd6Ez6Xoe2FsJp1HgM5kqJ+BNOk9pawmzlkZfx xyz/nSkybAeOCu5kqRCsbNiO7imkOn5x0TnhYvWJEv7uuzLAsx4IJG2o3Rr9qRHMmcrKdx qfeXivqU5SiLf/ar2bdZkeFGxtoILavkp1N06z6eFAHJbvIl0LrcgECAW4umz4a+LRhqQQ JNfZKVxTWKs84n7jr3PppwO/nTRau6p7rjxUej9UL/nrX9rw+9Ybc4RJ1Q5SLXwgvODGsl leH83vQ4fupPbmCeDSLIDG7Pf+H6dNmBTGfZsXifmAXxbx1pBBga4CqncEKdeA== From: Frank Oltmanns Date: Mon, 07 Aug 2023 14:43:35 +0200 Subject: [PATCH v6 02/11] clk: sunxi-ng: nkm: consider alternative parent rates when determining rate MIME-Version: 1.0 Message-Id: <20230807-pll-mipi_set_rate_parent-v6-2-f173239a4b59@oltmanns.dev> References: <20230807-pll-mipi_set_rate_parent-v6-0-f173239a4b59@oltmanns.dev> In-Reply-To: <20230807-pll-mipi_set_rate_parent-v6-0-f173239a4b59@oltmanns.dev> To: Maxime Ripard , Michael Turquette , Stephen Boyd , Chen-Yu Tsai , Jernej Skrabec , Samuel Holland , Andre Przywara , Roman Beranek Cc: linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org, Frank Oltmanns X-Developer-Signature: v=1; a=openpgp-sha256; l=2705; i=frank@oltmanns.dev; h=from:subject:message-id; bh=DhVDNfWw7/1aAXsxGZ2ImXUGfXIvkdSMT1LB/wIRtvw=; b=owEB7QES/pANAwAIAZppogiUStPHAcsmYgBk0OcKB1QCOVuSnP9f40U1kYo4xgG5KUuRdnVW5 mC6MFJDoRSJAbMEAAEIAB0WIQQC/SV7f5DmuaVET5aaaaIIlErTxwUCZNDnCgAKCRCaaaIIlErT xxYtC/92g6+CgrTGASkH/Ott8VxrMH8bycREhYf/t2+xQMjCk5E5e00rCo1JGKBcXlvQZC3UNDF Ppu1Divft8lTU+QR+x4rs4ycACV4EQ2SiKLi4AJlGDLBSk276R8Z6hUyqG1h+51+oV1Bv5rRg3V MsLmLpePWNmAFuNtPNkzv44Mnjsja7mFRcFLb9l1PD79McUTmXbmretcnW+aEh9HYuuXGpf+yhV h4zWqNcuwQUNwYMdYCkSxZ7ZpEUfCYe1bXpVGVEwqcJf+Xh5Y/yx4070guw7W4KIf++comIoVdj qp8toYdJ2vF05VZGqq62A1TQvJzoIAYxtCrBD8ow6RbsY7CxdX2A2Y5XBxa2BK35Jrpn1ZdLGHm 2VfpeVuqghgdngI4kYk/1OCvU1+m9Cp4lumlgWYMPqwcVchCcj1eAmUNS8WjWnxPj+ikHVK8lYf e54ZbpL8ulSGHiHtgAZMVHAZ+oFukEWp9lycZoyzkryjnDWNUK9/8WOPOkTDTSoCQMUIY= X-Developer-Key: i=frank@oltmanns.dev; a=openpgp; fpr=02FD257B7F90E6B9A5444F969A69A208944AD3C7 X-Rspamd-Queue-Id: 4RKGGY36Knz9snc X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230807_054408_647255_919E1CA4 X-CRM114-Status: GOOD ( 14.83 ) 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 In case the CLK_SET_RATE_PARENT flag is set, consider using a different parent rate when determining a new rate. To find the best match for the requested rate, perform the following steps for each NKM combination: - calculate the optimal parent rate, - find the best parent rate that the parent clock actually supports - use that parent rate to calculate the effective rate. In case the clk does not support setting the parent rate, use the same algorithm as before. Acked-by: Maxime Ripard Signed-off-by: Frank Oltmanns Reviewed-by: Chen-Yu Tsai --- drivers/clk/sunxi-ng/ccu_nkm.c | 44 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/drivers/clk/sunxi-ng/ccu_nkm.c b/drivers/clk/sunxi-ng/ccu_nkm.c index f267142e58b3..2f60f3c33e30 100644 --- a/drivers/clk/sunxi-ng/ccu_nkm.c +++ b/drivers/clk/sunxi-ng/ccu_nkm.c @@ -16,6 +16,45 @@ struct _ccu_nkm { unsigned long m, min_m, max_m; }; +static unsigned long ccu_nkm_find_best_with_parent_adj(struct clk_hw *parent_hw, + unsigned long *parent, unsigned long rate, + struct _ccu_nkm *nkm) +{ + unsigned long best_rate = 0, best_parent_rate = *parent, tmp_parent = *parent; + unsigned long best_n = 0, best_k = 0, best_m = 0; + unsigned long _n, _k, _m; + + for (_k = nkm->min_k; _k <= nkm->max_k; _k++) { + for (_n = nkm->min_n; _n <= nkm->max_n; _n++) { + for (_m = nkm->min_m; _m <= nkm->max_m; _m++) { + unsigned long tmp_rate; + + tmp_parent = clk_hw_round_rate(parent_hw, rate * _m / (_n * _k)); + + tmp_rate = tmp_parent * _n * _k / _m; + if (tmp_rate > rate) + continue; + + if ((rate - tmp_rate) < (rate - best_rate)) { + best_rate = tmp_rate; + best_parent_rate = tmp_parent; + best_n = _n; + best_k = _k; + best_m = _m; + } + } + } + } + + nkm->n = best_n; + nkm->k = best_k; + nkm->m = best_m; + + *parent = best_parent_rate; + + return best_rate; +} + static unsigned long ccu_nkm_find_best(unsigned long parent, unsigned long rate, struct _ccu_nkm *nkm) { @@ -124,7 +163,10 @@ static unsigned long ccu_nkm_round_rate(struct ccu_mux_internal *mux, if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV) rate *= nkm->fixed_post_div; - rate = ccu_nkm_find_best(*parent_rate, rate, &_nkm); + if (!clk_hw_can_set_rate_parent(&nkm->common.hw)) + rate = ccu_nkm_find_best(*parent_rate, rate, &_nkm); + else + rate = ccu_nkm_find_best_with_parent_adj(parent_hw, parent_rate, rate, &_nkm); if (nkm->common.features & CCU_FEATURE_FIXED_POSTDIV) rate /= nkm->fixed_post_div;