From afba5111aed03a05aa7fd46d3d9911319fa87a29 Mon Sep 17 00:00:00 2001
From: Christian Marangi <ansuelsmth@gmail.com>
Date: Thu, 30 Jan 2025 16:07:14 +0100
Subject: [PATCH 1/3] PM: runtime: add of_pm_clk_add_clk_index OP variant

Add of_pm_clk_add_clk_index OP variant of of_pm_clk_add_clk to take as
argument the clock index in DT instead of the name. This is to handle
case where clock-names property is not used by the node but clocks are
referenced with a dt-binding header or internally in the driver.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
 drivers/base/power/clock_ops.c | 31 +++++++++++++++++++++++++++++++
 include/linux/pm_clock.h       |  1 +
 2 files changed, 32 insertions(+)

--- a/drivers/base/power/clock_ops.c
+++ b/drivers/base/power/clock_ops.c
@@ -259,6 +259,37 @@ int pm_clk_add_clk(struct device *dev, s
 }
 EXPORT_SYMBOL_GPL(pm_clk_add_clk);
 
+/**
+ * of_pm_clk_add_clk_index - Start using a device clock for power management.
+ * @dev: Device whose clock is going to be used for power management.
+ * @index: Index of clock that is going to be used for power management.
+ *
+ * Add the clock described in the 'clocks' device-tree node at the index
+ * provided, to the list of clocks used for the power management of @dev.
+ * On success, returns 0. Returns a negative error code if the clock is not
+ * found or cannot be added.
+ */
+int of_pm_clk_add_clk_index(struct device *dev, int index)
+{
+	struct clk *clk;
+	int ret;
+
+	if (!dev || !dev->of_node || index < 0)
+		return -EINVAL;
+
+	clk = of_clk_get(dev->of_node, index);
+	if (IS_ERR(clk))
+		return PTR_ERR(clk);
+
+	ret = pm_clk_add_clk(dev, clk);
+	if (ret) {
+		clk_put(clk);
+		return ret;
+	}
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(of_pm_clk_add_clk_index);
 
 /**
  * of_pm_clk_add_clk - Start using a device clock for power management.
--- a/include/linux/pm_clock.h
+++ b/include/linux/pm_clock.h
@@ -41,6 +41,7 @@ extern int pm_clk_create(struct device *
 extern void pm_clk_destroy(struct device *dev);
 extern int pm_clk_add(struct device *dev, const char *con_id);
 extern int pm_clk_add_clk(struct device *dev, struct clk *clk);
+extern int of_pm_clk_add_clk_index(struct device *dev, int index);
 extern int of_pm_clk_add_clk(struct device *dev, const char *name);
 extern int of_pm_clk_add_clks(struct device *dev);
 extern void pm_clk_remove(struct device *dev, const char *con_id);
