public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] regulator: Ensure voltages are within the allowed range
@ 2014-11-12 10:12 Thierry Reding
  2014-11-12 11:45 ` Mark Brown
  0 siblings, 1 reply; 4+ messages in thread
From: Thierry Reding @ 2014-11-12 10:12 UTC (permalink / raw)
  To: Mark Brown; +Cc: Allen Martin, linux-kernel

From: Thierry Reding <treding@nvidia.com>

Unless a regulator has a fixed output voltage the core will not attempt
to modify the output voltage. This can cause a situation where a driver
enables the regulator but the currently configured voltage is outside
the valid range.

Fix this by constraining the current voltage to the allowed range upon
regulator registration.

Signed-off-by: Thierry Reding <treding@nvidia.com>
---
 drivers/regulator/core.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c
index bbf93c9caca3..97851f39b4c0 100644
--- a/drivers/regulator/core.c
+++ b/drivers/regulator/core.c
@@ -876,6 +876,7 @@ static int machine_constraints_voltage(struct regulator_dev *rdev,
 		int	max_uV = INT_MIN;
 		int	cmin = constraints->min_uV;
 		int	cmax = constraints->max_uV;
+		int	value;
 
 		/* it's safe to autoconfigure fixed-voltage supplies
 		   and the constraints are used by list_voltage. */
@@ -898,8 +899,6 @@ static int machine_constraints_voltage(struct regulator_dev *rdev,
 
 		/* initial: [cmin..cmax] valid, [min_uV..max_uV] not */
 		for (i = 0; i < count; i++) {
-			int	value;
-
 			value = ops->list_voltage(rdev, i);
 			if (value <= 0)
 				continue;
@@ -930,6 +929,24 @@ static int machine_constraints_voltage(struct regulator_dev *rdev,
 				 constraints->max_uV, max_uV);
 			constraints->max_uV = max_uV;
 		}
+
+		/*
+		 * Now that the voltage range has been determined, make sure
+		 * that the regulator's current voltage is within that range.
+		 */
+		value = _regulator_get_voltage(rdev);
+
+		if (value < constraints->min_uV ||
+		    value > constraints->max_uV) {
+			ret = _regulator_do_set_voltage(rdev,
+							constraints->min_uV,
+							constraints->max_uV);
+			if (ret < 0) {
+				rdev_err(rdev, "failed to set voltage to within %d-%d\n",
+					 constraints->min_uV, constraints->max_uV);
+				return ret;
+			}
+		}
 	}
 
 	return 0;
-- 
2.1.3


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] regulator: Ensure voltages are within the allowed range
  2014-11-12 10:12 [PATCH] regulator: Ensure voltages are within the allowed range Thierry Reding
@ 2014-11-12 11:45 ` Mark Brown
  2014-11-12 12:02   ` Thierry Reding
  0 siblings, 1 reply; 4+ messages in thread
From: Mark Brown @ 2014-11-12 11:45 UTC (permalink / raw)
  To: Thierry Reding; +Cc: Allen Martin, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1084 bytes --]

On Wed, Nov 12, 2014 at 11:12:05AM +0100, Thierry Reding wrote:
> From: Thierry Reding <treding@nvidia.com>
> 
> Unless a regulator has a fixed output voltage the core will not attempt
> to modify the output voltage. This can cause a situation where a driver
> enables the regulator but the currently configured voltage is outside
> the valid range.
> 
> Fix this by constraining the current voltage to the allowed range upon
> regulator registration.

I've thought about doing this before but it's tricky as if there is a
voltage range it's possible something needs it at the high rather than
low end of the range and will get upset if the voltage gets lowered.
Setting the top voltage also has risks (though less bad when constraints
are sane) and requires working out what that is which we'd need to write
the machinery to do.

This is all especially likely to break given the depressing desire
everyone seems to have to put the maximum possible voltage range for
their regulators in constraints regardless of what's actually sensible
for their board design.

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] regulator: Ensure voltages are within the allowed range
  2014-11-12 11:45 ` Mark Brown
@ 2014-11-12 12:02   ` Thierry Reding
  2014-11-12 14:48     ` Mark Brown
  0 siblings, 1 reply; 4+ messages in thread
From: Thierry Reding @ 2014-11-12 12:02 UTC (permalink / raw)
  To: Mark Brown; +Cc: Allen Martin, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 1645 bytes --]

On Wed, Nov 12, 2014 at 11:45:05AM +0000, Mark Brown wrote:
> On Wed, Nov 12, 2014 at 11:12:05AM +0100, Thierry Reding wrote:
> > From: Thierry Reding <treding@nvidia.com>
> > 
> > Unless a regulator has a fixed output voltage the core will not attempt
> > to modify the output voltage. This can cause a situation where a driver
> > enables the regulator but the currently configured voltage is outside
> > the valid range.
> > 
> > Fix this by constraining the current voltage to the allowed range upon
> > regulator registration.
> 
> I've thought about doing this before but it's tricky as if there is a
> voltage range it's possible something needs it at the high rather than
> low end of the range and will get upset if the voltage gets lowered.
> Setting the top voltage also has risks (though less bad when constraints
> are sane) and requires working out what that is which we'd need to write
> the machinery to do.
> 
> This is all especially likely to break given the depressing desire
> everyone seems to have to put the maximum possible voltage range for
> their regulators in constraints regardless of what's actually sensible
> for their board design.

Perhaps rather than checking that the voltage is within the valid range
we could at least cover the case where voltage is zero, which is what
triggered me to write this patch.

As it is, regulators with a valid voltage range but for which a voltage
isn't explicitly set will remain off if their default voltage is 0.

Do you have any recommendations to work around this other than adjusting
the actual voltage to be within the range?

Thierry

[-- Attachment #2: Type: application/pgp-signature, Size: 819 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] regulator: Ensure voltages are within the allowed range
  2014-11-12 12:02   ` Thierry Reding
@ 2014-11-12 14:48     ` Mark Brown
  0 siblings, 0 replies; 4+ messages in thread
From: Mark Brown @ 2014-11-12 14:48 UTC (permalink / raw)
  To: Thierry Reding; +Cc: Allen Martin, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 638 bytes --]

On Wed, Nov 12, 2014 at 01:02:04PM +0100, Thierry Reding wrote:

> Perhaps rather than checking that the voltage is within the valid range
> we could at least cover the case where voltage is zero, which is what
> triggered me to write this patch.

Anything which has a voltage of zero is buggy anyway, zero volts is the
off state and should show up as such.  If we don't have a voltage then
the enable should fail.

> Do you have any recommendations to work around this other than adjusting
> the actual voltage to be within the range?

Such regulators need a way of providing a bootsrapping voltage if
they're off when we start I think.

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 473 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-11-12 14:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-11-12 10:12 [PATCH] regulator: Ensure voltages are within the allowed range Thierry Reding
2014-11-12 11:45 ` Mark Brown
2014-11-12 12:02   ` Thierry Reding
2014-11-12 14:48     ` Mark Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox