mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 09/11] regulator: Set initial voltage
Date: Wed, 20 Sep 2023 12:33:14 +0200	[thread overview]
Message-ID: <20230920103316.2758383-10-s.hauer@pengutronix.de> (raw)
In-Reply-To: <20230920103316.2758383-1-s.hauer@pengutronix.de>

When voltage constraints are given in the device tree then we should
set the regulator to a valid voltage before enabling it. Without it
we can end up with invalid voltages.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/regulator/core.c | 60 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 60 insertions(+)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index d08df1dc68..00a2aefce7 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -187,6 +187,62 @@ static int regulator_resolve_supply(struct regulator_dev *rdev)
 	return 0;
 }
 
+static int regulator_init_voltage(struct regulator_dev *rdev)
+{
+	int target_min, target_max, current_uV, ret;
+
+	if (!rdev->min_uv || !rdev->max_uv)
+		return 0;
+
+	current_uV = regulator_get_voltage_internal(rdev);
+	if (current_uV < 0) {
+		/* This regulator can't be read and must be initialized */
+		rdev_info(rdev, "Setting %d-%duV\n", rdev->min_uv, rdev->max_uv);
+		regulator_set_voltage_internal(rdev, rdev->min_uv, rdev->max_uv);
+		current_uV = regulator_get_voltage_internal(rdev);
+	}
+
+	if (current_uV < 0) {
+		if (current_uV != -EPROBE_DEFER)
+			rdev_err(rdev,
+				 "failed to get the current voltage: %pe\n",
+				 ERR_PTR(current_uV));
+		return current_uV;
+	}
+
+	/*
+	 * If we're below the minimum voltage move up to the
+	 * minimum voltage, if we're above the maximum voltage
+	 * then move down to the maximum.
+	 */
+	target_min = current_uV;
+	target_max = current_uV;
+
+	if (current_uV < rdev->min_uv) {
+		target_min = rdev->min_uv;
+		target_max = rdev->min_uv;
+	}
+
+	if (current_uV > rdev->max_uv) {
+		target_min = rdev->max_uv;
+		target_max = rdev->max_uv;
+	}
+
+	if (target_min != current_uV || target_max != current_uV) {
+		rdev_info(rdev, "Bringing %duV into %d-%duV\n",
+			  current_uV, target_min, target_max);
+		ret = regulator_set_voltage_internal(rdev, target_min, target_max);
+		if (ret < 0) {
+			rdev_err(rdev,
+				"failed to apply %d-%duV constraint: %pe\n",
+				target_min, target_max, ERR_PTR(ret));
+			return ret;
+		}
+	}
+
+	return 0;
+}
+
 static int __regulator_register(struct regulator_dev *rdev, const char *name)
 {
 	int ret;
@@ -198,6 +254,10 @@ static int __regulator_register(struct regulator_dev *rdev, const char *name)
 	if (name)
 		rdev->name = xstrdup(name);
 
+	ret = regulator_init_voltage(rdev);
+	if (ret)
+		goto err;
+
 	if (rdev->boot_on || rdev->always_on) {
 		ret = regulator_resolve_supply(rdev);
 		if (ret < 0)
-- 
2.39.2




  parent reply	other threads:[~2023-09-20 10:34 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-20 10:33 [PATCH 00/11] regulator updates Sascha Hauer
2023-09-20 10:33 ` [PATCH 01/11] regulator: rename variable rd to rdev Sascha Hauer
2023-09-20 11:15   ` Marco Felsch
2023-09-20 10:33 ` [PATCH 02/11] regulator: merge struct regulator_internal fields into struct regulator_dev Sascha Hauer
2023-09-20 10:52   ` Marco Felsch
2023-09-20 11:07     ` Sascha Hauer
2023-09-20 11:13       ` Marco Felsch
2023-09-20 11:20   ` Marco Felsch
2023-09-20 10:33 ` [PATCH 03/11] regulator: introduce regulator logging functions Sascha Hauer
2023-09-20 11:22   ` Marco Felsch
2023-09-20 10:33 ` [PATCH 04/11] regulator: add regulator_get_voltage_internal() Sascha Hauer
2023-09-20 11:25   ` Marco Felsch
2023-09-20 11:45     ` Sascha Hauer
2023-09-20 10:33 ` [PATCH 05/11] regulator: Add missing cases in regulator_map_voltage() Sascha Hauer
2023-09-20 11:28   ` Marco Felsch
2023-09-20 10:33 ` [PATCH 06/11] regulator: stpmic1: add .get_voltage_sel Sascha Hauer
2023-09-20 11:29   ` Marco Felsch
2023-09-20 10:33 ` [PATCH 07/11] regulator: stpmic1: add .supply_name Sascha Hauer
2023-09-20 11:36   ` Marco Felsch
2023-09-20 10:33 ` [PATCH 08/11] regulator: register regulator as last step in of_regulator_register() Sascha Hauer
2023-09-20 11:39   ` Marco Felsch
2023-09-20 10:33 ` Sascha Hauer [this message]
2023-09-20 11:51   ` [PATCH 09/11] regulator: Set initial voltage Marco Felsch
2023-09-20 10:33 ` [PATCH 10/11] regulator: drop struct regulator_dev::supply_name Sascha Hauer
2023-09-20 11:51   ` Marco Felsch
2023-09-20 10:33 ` [PATCH 11/11] regulator: print regulator tree Sascha Hauer

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=20230920103316.2758383-10-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --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