Commit ad17731d authored by Linus Walleij's avatar Linus Walleij

gpio: clamp values on gpio[d]_direction_output()

I saw weird values != [0,1] being passed down to drivers
in their .set_direction_output() callbacks. Go over the
gpiolib and make sure to hammer it to [0,1] before hitting
the driver to avoid undesired side effects.
Signed-off-by: default avatarLinus Walleij <linus.walleij@linaro.org>
parent 24b35ed9
...@@ -2150,6 +2150,7 @@ EXPORT_SYMBOL_GPL(gpiod_direction_input); ...@@ -2150,6 +2150,7 @@ EXPORT_SYMBOL_GPL(gpiod_direction_input);
static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value) static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
{ {
struct gpio_chip *gc = desc->gdev->chip; struct gpio_chip *gc = desc->gdev->chip;
int val = !!value;
int ret; int ret;
/* GPIOs used for IRQs shall not be set as output */ /* GPIOs used for IRQs shall not be set as output */
...@@ -2169,7 +2170,7 @@ static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value) ...@@ -2169,7 +2170,7 @@ static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
goto set_output_value; goto set_output_value;
} }
/* Emulate open drain by not actively driving the line high */ /* Emulate open drain by not actively driving the line high */
if (value) if (val)
return gpiod_direction_input(desc); return gpiod_direction_input(desc);
} }
else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
...@@ -2180,7 +2181,7 @@ static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value) ...@@ -2180,7 +2181,7 @@ static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
goto set_output_value; goto set_output_value;
} }
/* Emulate open source by not actively driving the line low */ /* Emulate open source by not actively driving the line low */
if (!value) if (!val)
return gpiod_direction_input(desc); return gpiod_direction_input(desc);
} else { } else {
/* Make sure to disable open drain/source hardware, if any */ /* Make sure to disable open drain/source hardware, if any */
...@@ -2198,10 +2199,10 @@ static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value) ...@@ -2198,10 +2199,10 @@ static int _gpiod_direction_output_raw(struct gpio_desc *desc, int value)
return -EIO; return -EIO;
} }
ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), value); ret = gc->direction_output(gc, gpio_chip_hwgpio(desc), val);
if (!ret) if (!ret)
set_bit(FLAG_IS_OUT, &desc->flags); set_bit(FLAG_IS_OUT, &desc->flags);
trace_gpio_value(desc_to_gpio(desc), 0, value); trace_gpio_value(desc_to_gpio(desc), 0, val);
trace_gpio_direction(desc_to_gpio(desc), 0, ret); trace_gpio_direction(desc_to_gpio(desc), 0, ret);
return ret; return ret;
} }
...@@ -2241,6 +2242,8 @@ int gpiod_direction_output(struct gpio_desc *desc, int value) ...@@ -2241,6 +2242,8 @@ int gpiod_direction_output(struct gpio_desc *desc, int value)
VALIDATE_DESC(desc); VALIDATE_DESC(desc);
if (test_bit(FLAG_ACTIVE_LOW, &desc->flags)) if (test_bit(FLAG_ACTIVE_LOW, &desc->flags))
value = !value; value = !value;
else
value = !!value;
return _gpiod_direction_output_raw(desc, value); return _gpiod_direction_output_raw(desc, value);
} }
EXPORT_SYMBOL_GPL(gpiod_direction_output); EXPORT_SYMBOL_GPL(gpiod_direction_output);
...@@ -3094,7 +3097,7 @@ static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id, ...@@ -3094,7 +3097,7 @@ static int gpiod_configure_flags(struct gpio_desc *desc, const char *con_id,
/* Process flags */ /* Process flags */
if (dflags & GPIOD_FLAGS_BIT_DIR_OUT) if (dflags & GPIOD_FLAGS_BIT_DIR_OUT)
status = gpiod_direction_output(desc, status = gpiod_direction_output(desc,
dflags & GPIOD_FLAGS_BIT_DIR_VAL); !!(dflags & GPIOD_FLAGS_BIT_DIR_VAL));
else else
status = gpiod_direction_input(desc); status = gpiod_direction_input(desc);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment