Skip to contentSkip to conversation

regulator: gpio-regulator: Set the smallest voltage/current in the specified range

PATCH Applied Axel Lin 4 participants 4 messages Mar 22, 2012 – Jun 1, 2012 Voltage and current regulator framework

lore
Share & tools
lore

Axel Lin

author
#0 lore raw

PATCH 1/1 regulator: gpio-regulator: Set the smallest voltage/current in the specified range

Do not assume the gpio regulator states map is sorted in any order. This patch ensures we always set the smallest voltage/current that falls within the specified range.

---
 drivers/regulator/gpio-regulator.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
drivers/regulator/gpio-regulator.c +4 -4
index 42e1cb1..b728b36 100644
@@ -105,15 +105,15 @@ static int gpio_regulator_set_value(struct regulator_dev *dev,
 					int min, int max)
 {
 	struct gpio_regulator_data *data = rdev_get_drvdata(dev);
-	int ptr, target, state;
+	int ptr, target, state, best_val = INT_MAX;
 
-	target = -1;
 	for (ptr = 0; ptr < data->nr_states; ptr++)
-		if (data->states[ptr].value >= min &&
+		if (data->states[ptr].value < best_val &&
+		    data->states[ptr].value >= min &&
 		    data->states[ptr].value <= max)
 			target = data->states[ptr].gpios;
 
-	if (target < 0)
+	if (best_val == INT_MAX)
 		return -EINVAL;
 
 	for (ptr = 0; ptr < data->nr_gpios; ptr++) {
Signature · 1 line
1.7.5.4
Acked-by on v1 1/1
#1 lore raw

Am Donnerstag, 22. März 2012, 07:08:04 schrieb Axel Lin:

Quoted 5 lines

Do not assume the gpio regulator states map is sorted in any order. This patch ensures we always set the smallest voltage/current that falls within the specified range.

nice addition, thanks

Quoted 28 lines
---
 drivers/regulator/gpio-regulator.c |    8 ++++----
 1 files changed, 4 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/regulator/gpio-regulator.c index 42e1cb1..b728b36 100644

drivers/regulator/gpio-regulator.c +4 -4
@@ -105,15 +105,15 @@ static int gpio_regulator_set_value(struct
 regulator_dev *dev, int min, int max)
 {
 	struct gpio_regulator_data *data = rdev_get_drvdata(dev);
-	int ptr, target, state;
+	int ptr, target, state, best_val = INT_MAX;
 
-	target = -1;
 	for (ptr = 0; ptr < data->nr_states; ptr++)
-		if (data->states[ptr].value >= min &&
+		if (data->states[ptr].value < best_val &&
+		    data->states[ptr].value >= min &&
 		    data->states[ptr].value <= max)
 			target = data->states[ptr].gpios;
 
-	if (target < 0)
+	if (best_val == INT_MAX)
 		return -EINVAL;
 
 	for (ptr = 0; ptr < data->nr_gpios; ptr++) {
Applied
#2 lore raw
In reply to Axel Lin
Quoted 3 lines · Axel Lin

Do not assume the gpio regulator states map is sorted in any order. This patch ensures we always set the smallest voltage/current that falls within the specified range.

Applied, thanks.

#3 lore raw
Quoted 4 lines · Mark Brown <broonie <at> opensource.wolfsonmicro.com> writes:
Quoted 2 lines · Axel Lin

Do not assume the gpio regulator states map is sorted in any order. This patch ensures we always set the smallest voltage/current that falls

within

Quoted 4 lines
Quoted 1 line

the specified range.

Applied, thanks.

struct gpio_regulator_data *data = rdev_get_drvdata(dev);
- int ptr, target, state;
+ int ptr, target, state, best_val = INT_MAX;

- target = -1;
for (ptr = 0; ptr < data->nr_states; ptr++)
- if (data->states[ptr].value >= min &&
+ if (data->states[ptr].value < best_val &&
+ data->states[ptr].value >= min &&
data->states[ptr].value <= max)
target = data->states[ptr].gpios;

- if (target < 0)
+ if (best_val == INT_MAX)

Here seems something wrong, for best_val is always INT_MAX. From the discussion above, maybe best_val is should be reassigned in the for loop.
It may be like as the following:

- for (ptr = 0; ptr < data->nr_states; ptr++)

+       for (ptr = 0; ptr < data->nr_states; ptr++) {
                if (data->states[ptr].value < best_val &&
                    data->states[ptr].value >= min &&
-                   data->states[ptr].value <= max)
+                   data->states[ptr].value <= max) {
                        target = data->states[ptr].gpios;

+ best_val = target;
+ }

What do you think? thanks;