Commit 410ab742 authored by Bartosz Golaszewski's avatar Bartosz Golaszewski Committed by Greg Kroah-Hartman

gpiolib: don't clear FLAG_IS_OUT when emulating open-drain/open-source

[ Upstream commit e735244e ]

When emulating open-drain/open-source by not actively driving the output
lines - we're simply changing their mode to input. This is wrong as it
will then make it impossible to change the value of such line - it's now
considered to actually be in input mode. If we want to still use the
direction_input() callback for simplicity then we need to set FLAG_IS_OUT
manually in gpiod_direction_output() and not clear it in
gpio_set_open_drain_value_commit() and
gpio_set_open_source_value_commit().

Fixes: c663e5f5 ("gpio: support native single-ended hardware drivers")
Cc: stable@vger.kernel.org
Reported-by: default avatarKent Gibson <warthog618@gmail.com>
Signed-off-by: default avatarBartosz Golaszewski <bgolaszewski@baylibre.com>
[Bartosz: backported to v5.3, v4.19]
Signed-off-by: default avatarBartosz Golaszewski <bgolaszewski@baylibre.com>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
parent b41013b5
...@@ -2649,8 +2649,10 @@ int gpiod_direction_output(struct gpio_desc *desc, int value) ...@@ -2649,8 +2649,10 @@ int gpiod_direction_output(struct gpio_desc *desc, int value)
if (!ret) if (!ret)
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 (value) {
return gpiod_direction_input(desc); ret = gpiod_direction_input(desc);
goto set_output_flag;
}
} }
else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) { else if (test_bit(FLAG_OPEN_SOURCE, &desc->flags)) {
ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc), ret = gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
...@@ -2658,8 +2660,10 @@ int gpiod_direction_output(struct gpio_desc *desc, int value) ...@@ -2658,8 +2660,10 @@ int gpiod_direction_output(struct gpio_desc *desc, int value)
if (!ret) if (!ret)
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 (!value) {
return gpiod_direction_input(desc); ret = gpiod_direction_input(desc);
goto set_output_flag;
}
} else { } else {
gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc), gpio_set_drive_single_ended(gc, gpio_chip_hwgpio(desc),
PIN_CONFIG_DRIVE_PUSH_PULL); PIN_CONFIG_DRIVE_PUSH_PULL);
...@@ -2667,6 +2671,17 @@ int gpiod_direction_output(struct gpio_desc *desc, int value) ...@@ -2667,6 +2671,17 @@ int gpiod_direction_output(struct gpio_desc *desc, int value)
set_output_value: set_output_value:
return gpiod_direction_output_raw_commit(desc, value); return gpiod_direction_output_raw_commit(desc, value);
set_output_flag:
/*
* When emulating open-source or open-drain functionalities by not
* actively driving the line (setting mode to input) we still need to
* set the IS_OUT flag or otherwise we won't be able to set the line
* value anymore.
*/
if (ret == 0)
set_bit(FLAG_IS_OUT, &desc->flags);
return ret;
} }
EXPORT_SYMBOL_GPL(gpiod_direction_output); EXPORT_SYMBOL_GPL(gpiod_direction_output);
...@@ -2980,8 +2995,6 @@ static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value) ...@@ -2980,8 +2995,6 @@ static void gpio_set_open_drain_value_commit(struct gpio_desc *desc, bool value)
if (value) { if (value) {
err = chip->direction_input(chip, offset); err = chip->direction_input(chip, offset);
if (!err)
clear_bit(FLAG_IS_OUT, &desc->flags);
} else { } else {
err = chip->direction_output(chip, offset, 0); err = chip->direction_output(chip, offset, 0);
if (!err) if (!err)
...@@ -3011,8 +3024,6 @@ static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value ...@@ -3011,8 +3024,6 @@ static void gpio_set_open_source_value_commit(struct gpio_desc *desc, bool value
set_bit(FLAG_IS_OUT, &desc->flags); set_bit(FLAG_IS_OUT, &desc->flags);
} else { } else {
err = chip->direction_input(chip, offset); err = chip->direction_input(chip, offset);
if (!err)
clear_bit(FLAG_IS_OUT, &desc->flags);
} }
trace_gpio_direction(desc_to_gpio(desc), !value, err); trace_gpio_direction(desc_to_gpio(desc), !value, err);
if (err < 0) if (err < 0)
......
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