[PATCH v2 1/5] regulator: implement dev_of_regulator_get

Ahmad Fatoum a.fatoum at barebox.org
Mon Oct 27 12:28:16 PDT 2025


Regulators for a device need not be listed in the main node associated
with a device. Export dev_of_regulator_get for these use cases to
simplify porting Linux drivers.

Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
v1 -> v2:
  - fix wrong stub breaking non-regulator enabled platforms...
---
 drivers/regulator/core.c | 20 ++++++++++++--------
 include/regulator.h      | 27 ++++++++++++++++++++++-----
 2 files changed, 34 insertions(+), 13 deletions(-)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index f18622cba80c..cd0795589403 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -329,7 +329,8 @@ int of_regulator_register(struct regulator_dev *rdev, struct device_node *node)
 }
 
 static struct regulator_dev *of_regulator_get(struct device *dev,
-						   const char *supply)
+					      struct device_node *np,
+					      const char *supply)
 {
 	char *propname;
 	struct regulator_dev *rdev;
@@ -339,7 +340,7 @@ static struct regulator_dev *of_regulator_get(struct device *dev,
 	/*
 	 * If the device does have a device node return the dummy regulator.
 	 */
-	if (!dev->of_node)
+	if (!np)
 		return NULL;
 
 	propname = basprintf("%s-supply", supply);
@@ -348,7 +349,7 @@ static struct regulator_dev *of_regulator_get(struct device *dev,
 	 * If the device node does not contain a supply property, this device doesn't
 	 * need a regulator. Return the dummy regulator in this case.
 	 */
-	if (!of_get_property(dev->of_node, propname, NULL)) {
+	if (!of_get_property(np, propname, NULL)) {
 		dev_dbg(dev, "No %s-supply node found, using dummy regulator\n",
 				supply);
 		rdev = NULL;
@@ -359,7 +360,7 @@ static struct regulator_dev *of_regulator_get(struct device *dev,
 	 * The device node specifies a supply, so it's mandatory. Return an error when
 	 * something goes wrong below.
 	 */
-	node = of_parse_phandle(dev->of_node, propname, 0);
+	node = of_parse_phandle(np, propname, 0);
 	if (!node) {
 		dev_dbg(dev, "No %s node found\n", propname);
 		rdev = ERR_PTR(-EINVAL);
@@ -408,7 +409,8 @@ static struct regulator_dev *of_regulator_get(struct device *dev,
 }
 #else
 static struct regulator_dev *of_regulator_get(struct device *dev,
-						   const char *supply)
+					      struct device_node *np,
+					      const char *supply)
 {
 	return NULL;
 }
@@ -505,14 +507,16 @@ static struct regulator_dev *dev_regulator_get(struct device *dev,
  *
  * Return: a regulator object or an error pointer
  */
-struct regulator *regulator_get(struct device *dev, const char *supply)
+struct regulator *dev_of_regulator_get(struct device *dev,
+				       struct device_node *np,
+				       const char *supply)
 {
 	struct regulator_dev *rdev = NULL;
 	struct regulator *r;
 	int ret;
 
-	if (dev->of_node && supply) {
-		rdev = of_regulator_get(dev, supply);
+	if (np && supply) {
+		rdev = of_regulator_get(dev, np, supply);
 		if (IS_ERR(rdev))
 			return ERR_CAST(rdev);
 	}
diff --git a/include/regulator.h b/include/regulator.h
index cb542b05c696..bca2d37d1a0f 100644
--- a/include/regulator.h
+++ b/include/regulator.h
@@ -4,8 +4,7 @@
 
 #include <linux/bitops.h>
 #include <linux/err.h>
-
-struct device;
+#include <device.h>
 
 /* struct regulator is an opaque object for consumers */
 struct regulator;
@@ -212,7 +211,9 @@ const char *rdev_get_name(struct regulator_dev *rdev);
 
 #ifdef CONFIG_REGULATOR
 
-struct regulator *regulator_get(struct device *, const char *);
+struct regulator *dev_of_regulator_get(struct device *dev,
+				       struct device_node *np,
+				       const char *supply);
 void regulator_put(struct regulator *r);
 struct regulator *regulator_get_name(const char *name);
 int regulator_enable(struct regulator *);
@@ -265,8 +266,9 @@ int regulator_list_voltage_table(struct regulator_dev *rdev,
 				  unsigned int selector);
 #else
 
-static inline struct regulator *regulator_get(struct device *dev,
-					      const char *id)
+static inline struct regulator *dev_of_regulator_get(struct device *dev,
+						     struct device_node *np,
+						     const char *supply)
 {
 	return NULL;
 }
@@ -327,6 +329,21 @@ static inline int regulator_get_voltage(struct regulator *regulator)
 
 #endif
 
+/*
+ * regulator_get - get the supply for a device.
+ * @dev:	the device a supply is requested for
+ * @supply:     the supply name
+ *
+ * This returns a supply for a device. Check the result with IS_ERR().
+ * NULL is a valid regulator, the dummy regulator.
+ *
+ * Return: a regulator object or an error pointer
+ */
+static inline struct regulator *regulator_get(struct device *dev, const char *id)
+{
+	return dev_of_regulator_get(dev, dev_of_node(dev), id);
+}
+
 static inline struct regulator *regulator_get_optional(struct device *dev,
 						       const char *id)
 {
-- 
2.47.3




More information about the barebox mailing list