From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Subject: [PATCH 1/2] clk: add clk_hw_set_rate_range()
Date: Wed, 25 Nov 2026 18:33:44 +0100 [thread overview]
Message-ID: <20261125173348.1514894-1-a.fatoum@barebox.org> (raw)
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
The barebox clock core has no notion of a clock rate range.
clk_hw_init_rate_request() always initializes the request with
[0, ULONG_MAX] and nothing ever acts on it.
In Linux, it's possible to restrict rate per consumer handle and from
provider side. As we do not have this separation in barebox, let's for
now implement only the provider side of the rate range support:
Ranges default to [0, ULONG_MAX], so no functional change just yet.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
drivers/clk/clk.c | 53 ++++++++++++++++++++++++++++++++----
include/linux/clk-provider.h | 2 ++
include/linux/clk.h | 4 +++
3 files changed, 53 insertions(+), 6 deletions(-)
diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index 60a875d27687..107903aa1449 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -172,8 +172,8 @@ void clk_hw_init_rate_request(const struct clk_hw *hw,
req->hw = (struct clk_hw *)hw;
req->rate = rate;
- req->min_rate = 0;
- req->max_rate = ULONG_MAX;
+ req->min_rate = hw->clk.min_rate;
+ req->max_rate = hw->clk.max_rate;
}
EXPORT_SYMBOL_GPL(clk_hw_init_rate_request);
@@ -233,8 +233,8 @@ void clk_hw_forward_rate_request(const struct clk_hw *hw,
req->hw = (struct clk_hw *)parent;
req->rate = parent_rate;
- req->min_rate = old_req->min_rate;
- req->max_rate = old_req->max_rate;
+ req->min_rate = max(parent->clk.min_rate, old_req->min_rate);
+ req->max_rate = min(parent->clk.max_rate, old_req->max_rate);
}
static int clk_core_round_rate(struct clk *clk, struct clk_rate_request *req);
@@ -365,6 +365,22 @@ int __clk_mux_determine_rate_closest(struct clk_hw *hw,
}
EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
+static int clk_core_determine_round(struct clk *clk,
+ struct clk_rate_request *req)
+{
+ /*
+ * A request that wasn't initialized with clk_hw_init_rate_request()
+ * or clk_hw_forward_rate_request() would be clamped to 0
+ */
+ if (!req->min_rate && !req->max_rate)
+ pr_warn("%s: %s: clk_rate_request has no min and max rate\n",
+ __func__, clk->name);
+ else
+ req->rate = clamp(req->rate, req->min_rate, req->max_rate);
+
+ return clk->ops->determine_rate(clk_to_clk_hw(clk), req);
+}
+
/*
* Determine the rate @clk would settle on for the request @req, without
* touching the hardware. Mirrors Linux' clk_core_round_rate_nolock().
@@ -375,7 +391,7 @@ static int clk_core_round_rate(struct clk *clk, struct clk_rate_request *req)
struct clk *parent;
if (clk->ops->determine_rate)
- return clk->ops->determine_rate(hw, req);
+ return clk_core_determine_round(clk, req);
parent = clk_get_parent(clk);
@@ -466,16 +482,21 @@ int clk_set_rate(struct clk *clk, unsigned long rate)
current_rate = clk_get_rate(clk);
+ rate = clamp(rate, clk->min_rate, clk->max_rate);
+
if (clk->ops->determine_rate) {
struct clk_rate_request req;
struct clk_hw *best_parent_hw;
hw = clk_to_clk_hw(clk);
clk_hw_init_rate_request(hw, &req, rate);
- ret = clk->ops->determine_rate(hw, &req);
+ ret = clk_core_determine_round(clk, &req);
if (ret)
return ret;
+ if (req.rate < clk->min_rate || req.rate > clk->max_rate)
+ return -EINVAL;
+
if (req.rate == current_rate)
return 0;
@@ -563,6 +584,23 @@ int clk_hw_set_rate(struct clk_hw *hw, unsigned long rate)
return clk_set_rate(&hw->clk, rate);
}
+/**
+ * clk_hw_set_rate_range - set the rate range a clock can run at
+ * @hw: clock to set the range of, must be registered already
+ * @min_rate: minimum rate in Hz, inclusive
+ * @max_rate: maximum rate in Hz, inclusive
+ *
+ * Rate requests for the clock, including those its children forward to it,
+ * are clamped to the range.
+ */
+void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
+ unsigned long max_rate)
+{
+ hw->clk.min_rate = min_rate;
+ hw->clk.max_rate = max_rate;
+}
+EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
+
static int clk_fetch_parent_index(struct clk *clk,
struct clk *parent)
{
@@ -806,6 +844,9 @@ static int __bclk_register(struct clk *clk)
}
}
+ clk->min_rate = 0;
+ clk->max_rate = ULONG_MAX;
+
list_add_tail(&clk->list, &clks);
if (clk->ops->init) {
diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h
index 65e6489e91e0..cef24a1a5dbf 100644
--- a/include/linux/clk-provider.h
+++ b/include/linux/clk-provider.h
@@ -66,6 +66,8 @@ void clk_hw_forward_rate_request(const struct clk_hw *hw,
unsigned long parent_rate);
int __clk_determine_rate(struct clk_hw *hw, struct clk_rate_request *req);
+void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
+ unsigned long max_rate);
int clk_hw_determine_rate_no_reparent(struct clk_hw *hw,
struct clk_rate_request *req);
int clk_mux_determine_rate_flags(struct clk_hw *hw,
diff --git a/include/linux/clk.h b/include/linux/clk.h
index abcfdac0ebf8..6c58731ebd6a 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -397,6 +397,10 @@ struct clk {
struct clk **parents;
unsigned long flags;
+
+ /* set by the provider with clk_hw_set_rate_range() */
+ unsigned long min_rate;
+ unsigned long max_rate;
};
/**
--
2.47.3
next reply other threads:[~2026-11-25 17:35 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 17:33 Ahmad Fatoum [this message]
2026-11-25 17:33 ` [PATCH 2/2] clk: scmi: limit clocks to the rate range reported by the firmware Ahmad Fatoum
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=20261125173348.1514894-1-a.fatoum@barebox.org \
--to=a.fatoum@barebox.org \
--cc=barebox@lists.infradead.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