[PATCH 02/13] clk: factor out clk_hw_get_parent_index

Ahmad Fatoum a.fatoum at pengutronix.de
Sun Nov 26 22:49:36 PST 2023


clk_ops::set_parent passes the parent as an index, so clk_set_parent
needs to determine the index. Linux provides this information via
clk_hw_get_parent_index and some drivers depend on this, so factor out
the code in question in barebox as well.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 drivers/clk/clk.c   | 55 ++++++++++++++++++++++++++++++++++++---------
 include/linux/clk.h |  6 +++++
 2 files changed, 50 insertions(+), 11 deletions(-)

diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index 5b85c375cfed..25d32e9e12f9 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -211,6 +211,47 @@ int clk_hw_set_rate(struct clk_hw *hw, unsigned long rate)
 	return clk_set_rate(&hw->clk, rate);
 }
 
+static int clk_fetch_parent_index(struct clk *clk,
+				  struct clk *parent)
+{
+	int i;
+
+	if (!parent)
+		return -EINVAL;
+
+	for (i = 0; i < clk->num_parents; i++) {
+		if (IS_ERR_OR_NULL(clk->parents[i]))
+			clk->parents[i] = clk_lookup(clk->parent_names[i]);
+
+		if (!IS_ERR_OR_NULL(clk->parents[i]))
+			if (clk->parents[i] == parent)
+				break;
+	}
+
+	if (i == clk->num_parents)
+		return -EINVAL;
+
+	return i;
+}
+
+/**
+ * clk_hw_get_parent_index - return the index of the parent clock
+ * @hw: clk_hw associated with the clk being consumed
+ *
+ * Fetches and returns the index of parent clock. Returns -EINVAL if the given
+ * clock does not have a current parent.
+ */
+int clk_hw_get_parent_index(struct clk_hw *hw)
+{
+	struct clk_hw *parent = clk_hw_get_parent(hw);
+
+	if (WARN_ON(parent == NULL))
+		return -EINVAL;
+
+	return clk_fetch_parent_index(clk_hw_to_clk(hw), clk_hw_to_clk(parent));
+}
+EXPORT_SYMBOL_GPL(clk_hw_get_parent_index);
+
 struct clk *clk_lookup(const char *name)
 {
 	struct clk *c;
@@ -245,17 +286,9 @@ int clk_set_parent(struct clk *clk, struct clk *newparent)
 	if (!clk->ops->set_parent)
 		return -EINVAL;
 
-	for (i = 0; i < clk->num_parents; i++) {
-		if (IS_ERR_OR_NULL(clk->parents[i]))
-			clk->parents[i] = clk_lookup(clk->parent_names[i]);
-
-		if (!IS_ERR_OR_NULL(clk->parents[i]))
-			if (clk->parents[i] == newparent)
-				break;
-	}
-
-	if (i == clk->num_parents)
-		return -EINVAL;
+	i = clk_fetch_parent_index(clk, newparent);
+	if (i < 0)
+		return i;
 
 	if (clk->enable_count)
 		clk_enable(newparent);
diff --git a/include/linux/clk.h b/include/linux/clk.h
index 80921136438d..9975c1a601c4 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -139,6 +139,7 @@ int clk_hw_set_parent(struct clk_hw *hw, struct clk_hw *hwp);
  */
 struct clk *clk_get_parent(struct clk *clk);
 struct clk_hw *clk_hw_get_parent(struct clk_hw *hw);
+int clk_hw_get_parent_index(struct clk_hw *hw);
 
 int clk_set_phase(struct clk *clk, int degrees);
 int clk_get_phase(struct clk *clk);
@@ -185,6 +186,11 @@ static inline struct clk *clk_get_parent(struct clk *clk)
 	return NULL;
 }
 
+static inline int clk_hw_get_parent_index(struct clk_hw *hw)
+{
+	return -EINVAL;
+}
+
 static inline int clk_enable(struct clk *clk)
 {
 	return 0;
-- 
2.39.2




More information about the barebox mailing list