mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Frank Wunderlich <frank-w@public-files.de>,
	Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 3/4] regulator: recursively enable/disable regulator dependency tree
Date: Fri, 22 Jul 2022 14:28:17 +0200	[thread overview]
Message-ID: <20220722122818.3658884-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20220722122818.3658884-1-a.fatoum@pengutronix.de>

Regulators may themself have other regulators supplying them.
The regulators need to be enabled recursively for proper operation.

Linux handles this by allows drivers to provide
struct regulator_desc::supply_name, which will be requested when the
regulator itself is requested and enabled/disabled as necessary.

As no driver yet uses this new member, this should introduce no
functional change.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/regulator/core.c | 46 +++++++++++++++++++++++++++++++++++-----
 include/regulator.h      |  5 +++++
 2 files changed, 46 insertions(+), 5 deletions(-)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index 29a335f50278..acbb3c11d6ea 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -42,6 +42,7 @@ static int regulator_map_voltage(struct regulator_dev *rdev, int min_uV,
 
 static int regulator_enable_internal(struct regulator_internal *ri)
 {
+	struct regulator_dev *rdev = ri->rdev;
 	int ret;
 
 	if (ri->enable_count) {
@@ -49,13 +50,20 @@ static int regulator_enable_internal(struct regulator_internal *ri)
 		return 0;
 	}
 
-	if (!ri->rdev->desc->ops->enable)
+	if (!rdev->desc->ops->enable)
 		return -ENOSYS;
 
-	ret = ri->rdev->desc->ops->enable(ri->rdev);
+	/* turn on parent regulator */
+	ret = regulator_enable(rdev->supply);
 	if (ret)
 		return ret;
 
+	ret = rdev->desc->ops->enable(ri->rdev);
+	if (ret) {
+		regulator_disable(rdev->supply);
+		return ret;
+	}
+
 	if (ri->enable_time_us)
 		udelay(ri->enable_time_us);
 
@@ -66,6 +74,7 @@ static int regulator_enable_internal(struct regulator_internal *ri)
 
 static int regulator_disable_internal(struct regulator_internal *ri)
 {
+	struct regulator_dev *rdev = ri->rdev;
 	int ret;
 
 	if (!ri->enable_count)
@@ -76,16 +85,16 @@ static int regulator_disable_internal(struct regulator_internal *ri)
 		return 0;
 	}
 
-	if (!ri->rdev->desc->ops->disable)
+	if (!rdev->desc->ops->disable)
 		return -ENOSYS;
 
-	ret = ri->rdev->desc->ops->disable(ri->rdev);
+	ret = rdev->desc->ops->disable(rdev);
 	if (ret)
 		return ret;
 
 	ri->enable_count--;
 
-	return 0;
+	return regulator_disable(rdev->supply);
 }
 
 static int regulator_set_voltage_internal(struct regulator_internal *ri,
@@ -319,6 +328,26 @@ static struct regulator_internal *dev_regulator_get(struct device_d *dev, const
 	return ret;
 }
 
+static int regulator_resolve_supply(struct regulator_dev *rdev)
+{
+	struct regulator *supply;
+	const char *supply_name;
+
+	if (!rdev || rdev->supply)
+		return 0;
+
+	supply_name = rdev->desc->supply_name;
+	if (!supply_name)
+		return 0;
+
+	supply = regulator_get(rdev->dev, supply_name);
+	if (IS_ERR(supply))
+		return PTR_ERR(supply);
+
+	rdev->supply = supply;
+	return 0;
+}
+
 /*
  * regulator_get - get the supply for a device.
  * @dev:	the device a supply is requested for
@@ -333,6 +362,7 @@ struct regulator *regulator_get(struct device_d *dev, const char *supply)
 {
 	struct regulator_internal *ri = NULL;
 	struct regulator *r;
+	int ret;
 
 	if (dev->device_node) {
 		ri = of_regulator_get(dev, supply);
@@ -349,6 +379,10 @@ struct regulator *regulator_get(struct device_d *dev, const char *supply)
 	if (!ri)
 		return NULL;
 
+	ret = regulator_resolve_supply(ri->rdev);
+	if (ret < 0)
+		return ERR_PTR(ret);
+
 	r = xzalloc(sizeof(*r));
 	r->ri = ri;
 	r->dev = dev;
@@ -588,6 +622,8 @@ int regulator_get_voltage(struct regulator *regulator)
 		ret = rdev->desc->ops->list_voltage(rdev, 0);
 	} else if (rdev->desc->fixed_uV && (rdev->desc->n_voltages == 1)) {
 		ret = rdev->desc->fixed_uV;
+	} else if (rdev->supply) {
+		ret = regulator_get_voltage(rdev->supply);
 	} else {
 		return -EINVAL;
 	}
diff --git a/include/regulator.h b/include/regulator.h
index dfdfbf033262..1ae31ad7cf23 100644
--- a/include/regulator.h
+++ b/include/regulator.h
@@ -32,6 +32,8 @@ struct regulator_bulk_data {
  * structure contains the non-varying parts of the regulator
  * description.
  *
+ * @supply_name: Identifying the supply of this regulator
+ *
  * @n_voltages: Number of selectors available for ops.list_voltage().
  * @ops: Regulator operations table.
  *
@@ -57,6 +59,7 @@ struct regulator_bulk_data {
  */
 
 struct regulator_desc {
+	const char *supply_name;
 	unsigned n_voltages;
 	const struct regulator_ops *ops;
 
@@ -88,6 +91,8 @@ struct regulator_dev {
 	bool always_on;
 	/* the device this regulator device belongs to */
 	struct device_d *dev;
+	/* The regulator powering this device */
+	struct regulator *supply;
 };
 
 struct regulator_ops {
-- 
2.30.2




  parent reply	other threads:[~2022-07-22 12:30 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-22 12:28 [PATCH 1/4] mfd: implement mfd_add_devices Ahmad Fatoum
2022-07-22 12:28 ` [PATCH 2/4] regmap: implement regmap_init_i2c_smbus Ahmad Fatoum
2022-07-22 12:28 ` Ahmad Fatoum [this message]
2022-07-22 12:28 ` [PATCH 4/4] regulator: add Rockchip rk808 support 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=20220722122818.3658884-3-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=frank-w@public-files.de \
    /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