mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Frank Oltmanns <frank@oltmanns.dev>
To: Maxime Ripard <mripard@kernel.org>,
	Michael Turquette <mturquette@baylibre.com>,
	Stephen Boyd <sboyd@kernel.org>, Chen-Yu Tsai <wens@csie.org>,
	Jernej Skrabec <jernej.skrabec@gmail.com>,
	Samuel Holland <samuel@sholland.org>,
	Andre Przywara <andre.przywara@arm.com>,
	Roman Beranek <me@crly.cz>
Cc: linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org,
	Frank Oltmanns <frank@oltmanns.dev>
Subject: [PATCH v5 02/11] clk: sunxi-ng: nkm: consider alternative parent rates when determining rate
Date: Sun, 06 Aug 2023 15:06:47 +0200	[thread overview]
Message-ID: <20230806-pll-mipi_set_rate_parent-v5-2-db4f5ca33fc3@oltmanns.dev> (raw)
In-Reply-To: <20230806-pll-mipi_set_rate_parent-v5-0-db4f5ca33fc3@oltmanns.dev>

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 <mripard@kernel.org>
Signed-off-by: Frank Oltmanns <frank@oltmanns.dev>
---
 drivers/clk/sunxi-ng/ccu_nkm.c | 45 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)

diff --git a/drivers/clk/sunxi-ng/ccu_nkm.c b/drivers/clk/sunxi-ng/ccu_nkm.c
index f267142e58b3..ea1b77e9b57f 100644
--- a/drivers/clk/sunxi-ng/ccu_nkm.c
+++ b/drivers/clk/sunxi-ng/ccu_nkm.c
@@ -6,6 +6,7 @@
 
 #include <linux/clk-provider.h>
 #include <linux/io.h>
+#include <linux/math.h>
 
 #include "ccu_gate.h"
 #include "ccu_nkm.h"
@@ -16,6 +17,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 +164,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;

-- 
2.41.0


  parent reply	other threads:[~2023-08-06 13:07 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-06 13:06 [PATCH v5 00/11] clk: sunxi-ng: Consider alternative parent rates when determining NKM clock rate Frank Oltmanns
2023-08-06 13:06 ` [PATCH v5 01/11] clk: sunxi-ng: nkm: Use correct parameter name for parent HW Frank Oltmanns
2023-08-06 13:31   ` Jernej Škrabec
2023-08-06 13:06 ` Frank Oltmanns [this message]
2023-08-06 13:32   ` [PATCH v5 02/11] clk: sunxi-ng: nkm: consider alternative parent rates when determining rate Jernej Škrabec
2023-08-06 13:57     ` Frank Oltmanns
2023-08-06 13:06 ` [PATCH v5 03/11] clk: sunxi-ng: a64: allow pll-mipi to set parent's rate Frank Oltmanns
2023-08-06 13:32   ` Jernej Škrabec
2023-08-06 13:06 ` [PATCH v5 04/11] clk: sunxi-ng: Add feature to find closest rate Frank Oltmanns
2023-08-06 13:33   ` Jernej Škrabec
2023-08-06 13:06 ` [PATCH v5 05/11] clk: sunxi-ng: Add helper function " Frank Oltmanns
2023-08-06 13:41   ` Jernej Škrabec
2023-08-06 13:06 ` [PATCH v5 06/11] clk: sunxi-ng: nm: Support finding " Frank Oltmanns
2023-08-06 13:42   ` Jernej Škrabec
2023-08-06 13:06 ` [PATCH v5 07/11] clk: sunxi-ng: nkm: " Frank Oltmanns
2023-08-06 13:42   ` Jernej Škrabec
2023-08-06 13:06 ` [PATCH v5 08/11] clk: sunxi-ng: mux: " Frank Oltmanns
2023-08-06 13:42   ` Jernej Škrabec
2023-08-06 13:06 ` [PATCH v5 09/11] clk: sunxi-ng: div: " Frank Oltmanns
2023-08-06 13:42   ` Jernej Škrabec
2023-08-06 13:06 ` [PATCH v5 10/11] clk: sunxi-ng: a64: select closest rate for pll-video0 Frank Oltmanns
2023-08-06 13:48   ` Jernej Škrabec
2023-08-06 13:06 ` [PATCH v5 11/11] clk: sunxi-ng: nkm: Prefer current parent rate Frank Oltmanns
2023-08-06 13:43   ` Jernej Škrabec

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230806-pll-mipi_set_rate_parent-v5-2-db4f5ca33fc3@oltmanns.dev \
    --to=frank@oltmanns.dev \
    --cc=andre.przywara@arm.com \
    --cc=jernej.skrabec@gmail.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sunxi@lists.linux.dev \
    --cc=me@crly.cz \
    --cc=mripard@kernel.org \
    --cc=mturquette@baylibre.com \
    --cc=samuel@sholland.org \
    --cc=sboyd@kernel.org \
    --cc=wens@csie.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®