linux-sunxi.lists.linux.dev archive mirror
 help / color / mirror / Atom feed
From: Frank Oltmanns <frank@oltmanns.dev>
To: linux-arm-kernel@lists.infradead.org, linux-clk@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-sunxi@lists.linux.dev
Cc: Frank Oltmanns <frank@oltmanns.dev>,
	Andre Przywara <andre.przywara@arm.com>,
	Chen-Yu Tsai <wens@csie.org>, Icenowy Zheng <icenowy@aosc.io>,
	Jernej Skrabec <jernej.skrabec@gmail.com>,
	Maxime Ripard <mripard@kernel.org>,
	Michael Turquette <mturquette@baylibre.com>,
	Rob Herring <robh@kernel.org>,
	Samuel Holland <samuel@sholland.org>,
	Stephen Boyd <sboyd@kernel.org>
Subject: [RFC PATCH 1/3] clk: sunxi-ng: nkm: Minimize difference when finding rate
Date: Sat, 27 May 2023 15:27:45 +0200	[thread overview]
Message-ID: <20230527132747.83196-2-frank@oltmanns.dev> (raw)
In-Reply-To: <20230527132747.83196-1-frank@oltmanns.dev>

Instead of selecting the first rate that is less than the requested
rate, select the rate that minimizes the absolute difference with the
requested rate. This ensures a more accurate rate selection, when the
closest available rate is higher than the requested rate.

Also stop searching once an exact match is found.

Adjust mux to this change in rate selection.

Signed-off-by: Frank Oltmanns <frank@oltmanns.dev>
---
 drivers/clk/sunxi-ng/ccu_mux.c |  2 +-
 drivers/clk/sunxi-ng/ccu_nkm.c | 13 +++++++++----
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/drivers/clk/sunxi-ng/ccu_mux.c b/drivers/clk/sunxi-ng/ccu_mux.c
index 9303e1f02ffd..6fe0a8d098f1 100644
--- a/drivers/clk/sunxi-ng/ccu_mux.c
+++ b/drivers/clk/sunxi-ng/ccu_mux.c
@@ -145,7 +145,7 @@ int ccu_mux_helper_determine_rate(struct ccu_common *common,
 			goto out;
 		}
 
-		if ((req->rate - tmp_rate) < (req->rate - best_rate)) {
+		if (abs(req->rate - tmp_rate) < abs(req->rate - best_rate)) {
 			best_rate = tmp_rate;
 			best_parent_rate = parent_rate;
 			best_parent = parent;
diff --git a/drivers/clk/sunxi-ng/ccu_nkm.c b/drivers/clk/sunxi-ng/ccu_nkm.c
index a0978a50edae..94d2a83992b2 100644
--- a/drivers/clk/sunxi-ng/ccu_nkm.c
+++ b/drivers/clk/sunxi-ng/ccu_nkm.c
@@ -19,7 +19,7 @@ struct _ccu_nkm {
 static unsigned long ccu_nkm_find_best(unsigned long parent, unsigned long rate,
 				       struct _ccu_nkm *nkm)
 {
-	unsigned long best_rate = 0;
+	unsigned long best_rate = 0, best_diff = ULONG_MAX;
 	unsigned long best_n = 0, best_k = 0, best_m = 0;
 	unsigned long _n, _k, _m;
 
@@ -27,21 +27,26 @@ static unsigned long ccu_nkm_find_best(unsigned long parent, unsigned long rate,
 		for (_n = nkm->min_n; _n <= nkm->max_n; _n++) {
 			for (_m = nkm->min_m; _m <= nkm->max_m; _m++) {
 				unsigned long tmp_rate;
+				unsigned long tmp_diff;
 
 				tmp_rate = parent * _n * _k / _m;
 
-				if (tmp_rate > rate)
-					continue;
-				if ((rate - tmp_rate) < (rate - best_rate)) {
+				tmp_diff = abs(rate - tmp_rate);
+
+				if (tmp_diff < best_diff) {
 					best_rate = tmp_rate;
+					best_diff = tmp_diff;
 					best_n = _n;
 					best_k = _k;
 					best_m = _m;
+					if (best_diff == 0)
+						goto out;
 				}
 			}
 		}
 	}
 
+out:
 	nkm->n = best_n;
 	nkm->k = best_k;
 	nkm->m = best_m;
-- 
2.40.1


  reply	other threads:[~2023-05-27 13:36 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-27 13:27 [RFC PATCH 0/3] clk: sunxi-ng: Optimize rate selection for NKM clocks Frank Oltmanns
2023-05-27 13:27 ` Frank Oltmanns [this message]
2023-05-27 13:27 ` [RFC PATCH 2/3] clk: sunxi-ng: Implement precalculated NKM rate selection Frank Oltmanns
2023-05-27 23:19   ` Julian Calaby
2023-05-28  9:12     ` Frank Oltmanns
2023-05-28 15:32       ` Julian Calaby
2023-05-28 17:12         ` Frank Oltmanns
2023-05-28 14:11   ` Frank Oltmanns
2023-05-27 13:27 ` [RFC PATCH 3/3] clk: sunxi-ng: sun50i-a64: Precalculate NKM combinations for pll-mipi Frank Oltmanns
2023-05-31 13:48 ` [RFC PATCH 0/3] clk: sunxi-ng: Optimize rate selection for NKM clocks Maxime Ripard
2023-06-01  5:16   ` Frank Oltmanns
2023-06-01 19:41     ` Jernej Škrabec
2023-06-02  7:34       ` Maxime Ripard
2023-06-05 11:41         ` Frank Oltmanns
2023-06-02  7:31     ` Maxime Ripard
2023-06-05 10:31       ` Frank Oltmanns
2023-06-06 14:02         ` Maxime Ripard
2023-06-06 20:40           ` Frank Oltmanns
2023-06-07 11:42             ` Maxime Ripard

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=20230527132747.83196-2-frank@oltmanns.dev \
    --to=frank@oltmanns.dev \
    --cc=andre.przywara@arm.com \
    --cc=icenowy@aosc.io \
    --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=mripard@kernel.org \
    --cc=mturquette@baylibre.com \
    --cc=robh@kernel.org \
    --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;
as well as URLs for NNTP newsgroup(s).