mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andreas Kemnade <akemnade@kernel.org>
To: Liam Girdwood <lgirdwood@gmail.com>, Mark Brown <broonie@kernel.org>
Cc: linux-kernel@vger.kernel.org, Andreas Kemnade <akemnade@kernel.org>
Subject: [PATCH RFC] regulator: core: fix constraints handling if current state out of range
Date: Mon, 03 Nov 2025 20:32:41 +0100	[thread overview]
Message-ID: <20251103-regu-fix-v1-1-c8a46581c850@kernel.org> (raw)

Given a regulator set to an initially out of constraints value,
which cannot be set to the exact voltage given in the min or max constraints,
current code tries to apply a fixed value which obviously fails.

To fix that, allow a range as a constraint in out of bound cases.

A practical use case for this scenario is if there is a quotient
in uV_step which needs to get rounded.
The devicetree should describe the hardware and not depend on the
idea of a specific driver implementation how to round things,
so a small range need to be specified.
Instead of rounding uV_step another user of the devicetree
might be specifying uV_max and uV_min and therefore only needs
to round the end result leading to slightly different results.

Stumbled upon it while creating a regulator with uV_step = 5000000 / 255

Signed-off-by: Andreas Kemnade <akemnade@kernel.org>
Fixes: fa93fd4ecc9c ("regulator: core: Ensure we are at least in bounds for our constraints")
---
not tested yet to avoid magic smoke in case of errors. I rather prefer
having a second pair of eyes on it in this sensitive area.
---
 drivers/regulator/core.c | 8 ++------
 1 file changed, 2 insertions(+), 6 deletions(-)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index dd7b10e768c0..6b491c21ec5b 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -1238,13 +1238,9 @@ static int machine_constraints_voltage(struct regulator_dev *rdev,
 		target_min = current_uV;
 		target_max = current_uV;
 
-		if (current_uV < rdev->constraints->min_uV) {
+		if ((current_uV < rdev->constraints->min_uV) ||
+		    (current_uV > rdev->constraints->max_uV)) {
 			target_min = rdev->constraints->min_uV;
-			target_max = rdev->constraints->min_uV;
-		}
-
-		if (current_uV > rdev->constraints->max_uV) {
-			target_min = rdev->constraints->max_uV;
 			target_max = rdev->constraints->max_uV;
 		}
 

---
base-commit: dcb6fa37fd7bc9c3d2b066329b0d27dedf8becaa
change-id: 20251103-regu-fix-d650274afa89

Best regards,
--  
Andreas Kemnade <akemnade@kernel.org>


             reply	other threads:[~2025-11-03 19:32 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-03 19:32 Andreas Kemnade [this message]
2025-11-04 13:52 ` Mark Brown
2025-11-05  9:08   ` Andreas Kemnade
2025-11-05 11:48     ` Mark Brown

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=20251103-regu-fix-v1-1-c8a46581c850@kernel.org \
    --to=akemnade@kernel.org \
    --cc=broonie@kernel.org \
    --cc=lgirdwood@gmail.com \
    --cc=linux-kernel@vger.kernel.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

all inboxes | Powered by JetHome®