Skip to contentSkip to conversation

clk: add clk_hw_set_rate_range()

PATCH New Ahmad Fatoum 1 participant 2 messages Nov 25

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 clk: add clk_hw_set_rate_range()

From: Ahmad Fatoum <[email protected]>

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.

---
 drivers/clk/clk.c            | 53 ++++++++++++++++++++++++++++++++----
 include/linux/clk-provider.h |  2 ++
 include/linux/clk.h          |  4 +++
 3 files changed, 53 insertions(+), 6 deletions(-)
drivers/clk/clk.c +47 -6
index 60a875d27687..107903aa1449 100644
@@ -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) {
include/linux/clk-provider.h +2 -0
index 65e6489e91e0..cef24a1a5dbf 100644
@@ -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,
include/linux/clk.h +4 -0
index abcfdac0ebf8..6c58731ebd6a 100644
@@ -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;
 };
 
 /**
Signature · 1 line
2.47.3
#1 lore raw

PATCH 2/2 clk: scmi: limit clocks to the rate range reported by the firmware

From: Ahmad Fatoum <[email protected]>

Linux clk-scmi driver registers the lowest and highest rate of each clock as rate range and the clock core clamps all requests to it.

barebox didn't and so in the incoming Amlogic A9 support, TF-A would report:

NOTICE: fclk_div2 does not allow modification through scmi-clk

because determine_rate returned the requested rate as is and the clock framework assumed the clock can run at any rate.

Now that barebox supports rate ranges as well, do as Linux does.

---
 drivers/clk/clk-scmi.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/clk/clk-scmi.c +11 -1
index 79e2c65f847d..ddccd568b723 100644
@@ -179,6 +179,7 @@ static int scmi_clk_is_enabled(struct clk_hw *hw)
 static int scmi_clk_ops_init(struct device *dev, struct scmi_clk *sclk,
 			     const struct clk_ops *scmi_ops)
 {
+	unsigned long min_rate, max_rate;
 	int ret;
 
 	struct clk_init_data init = {
@@ -199,9 +200,18 @@ static int scmi_clk_ops_init(struct device *dev, struct scmi_clk *sclk,
 
 		if (num_rates <= 0)
 			return -EINVAL;
+
+		/* the rates are sorted in ascending order */
+		min_rate = sclk->info->list.rates[0];
+		max_rate = sclk->info->list.rates[num_rates - 1];
+	} else {
+		min_rate = sclk->info->range.min_rate;
+		max_rate = sclk->info->range.max_rate;
 	}
 
-	return ret;
+	clk_hw_set_rate_range(&sclk->hw, min_rate, max_rate);
+
+	return 0;
 }
 
 /**
Signature · 1 line
2.47.3