mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH v2 1/5] regulator: implement dev_of_regulator_get
Date: Mon, 27 Oct 2025 20:28:16 +0100	[thread overview]
Message-ID: <20251027192938.2586566-1-a.fatoum@barebox.org> (raw)

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@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




             reply	other threads:[~2025-10-27 19:30 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-27 19:28 Ahmad Fatoum [this message]
2025-10-27 19:28 ` [PATCH v2 2/5] pmdomain: look up pmdomain even if not have_genpd_providers Ahmad Fatoum
2025-10-27 19:28 ` [PATCH v2 3/5] pmdomain: warn if deep probe enabled and driver missing Ahmad Fatoum
2025-10-27 19:28 ` [PATCH v2 4/5] pmdomain: allow callback for when devices are attached Ahmad Fatoum
2025-10-27 19:28 ` [PATCH v2 5/5] pmdomain: add Rockchip power domain support Ahmad Fatoum
2025-10-28  8:57   ` Sascha Hauer
2025-10-28  9:04     ` Ahmad Fatoum
2025-10-28  9:48       ` Sascha Hauer
2025-10-28 10:41         ` Ahmad Fatoum
2025-10-28 10:46           ` Sascha Hauer
2025-10-28 11:01             ` 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=20251027192938.2586566-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