Skip to contentSkip to conversation

gpiolib: Deduplicate gpiod_direction_input_nonotify() call

PATCH In review Andy Shevchenko 2 participants 4 messages Oct 31, 2024 – Nov 1, 2024 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpiolib: Deduplicate gpiod_direction_input_nonotify() call

Deduplicate gpiod_direction_input_nonotify() call in gpiod_direction_output_nonotify() when emulating open-drain or open-source behaviour. It also aligns the error check approaches in set_output_value and set_output_flag labels.

---
 drivers/gpio/gpiolib.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/gpio/gpiolib.c +7 -9
index 5666c462248c..9376dd270344 100644
@@ -2877,19 +2877,15 @@ int gpiod_direction_output_nonotify(struct gpio_desc *desc, int value)
 		if (!ret)
 			goto set_output_value;
 		/* Emulate open drain by not actively driving the line high */
-		if (value) {
-			ret = gpiod_direction_input_nonotify(desc);
+		if (value)
 			goto set_output_flag;
-		}
 	} else if (test_bit(FLAG_OPEN_SOURCE, &flags)) {
 		ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
 		if (!ret)
 			goto set_output_value;
 		/* Emulate open source by not actively driving the line low */
-		if (!value) {
-			ret = gpiod_direction_input_nonotify(desc);
+		if (!value)
 			goto set_output_flag;
-		}
 	} else {
 		gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
 	}
@@ -2901,15 +2897,17 @@ int gpiod_direction_output_nonotify(struct gpio_desc *desc, int value)
 	return gpiod_direction_output_raw_commit(desc, value);
 
 set_output_flag:
+	ret = gpiod_direction_input_nonotify(desc);
+	if (ret)
+		return ret;
 	/*
 	 * 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;
+	set_bit(FLAG_IS_OUT, &desc->flags);
+	return 0;
 }
 
 /**
Signature · 1 line
2.43.0.rc1.1336.g36b5255a03ac
#1 lore raw
Quoted 38 lines · Andy Shevchenko

Deduplicate gpiod_direction_input_nonotify() call in gpiod_direction_output_nonotify() when emulating open-drain or open-source behaviour. It also aligns the error check approaches in set_output_value and set_output_flag labels.

---
 drivers/gpio/gpiolib.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/gpio/gpiolib.c +2 -6
index 5666c462248c..9376dd270344 100644
@@ -2877,19 +2877,15 @@ int gpiod_direction_output_nonotify(struct gpio_desc *desc, int value)
                if (!ret)
                        goto set_output_value;
                /* Emulate open drain by not actively driving the line high */
-               if (value) {
-                       ret = gpiod_direction_input_nonotify(desc);
+               if (value)
                        goto set_output_flag;
-               }
        } else if (test_bit(FLAG_OPEN_SOURCE, &flags)) {
                ret = gpio_set_config(desc, PIN_CONFIG_DRIVE_OPEN_SOURCE);
                if (!ret)
                        goto set_output_value;
                /* Emulate open source by not actively driving the line low */
-               if (!value) {
-                       ret = gpiod_direction_input_nonotify(desc);
+               if (!value)
                        goto set_output_flag;
-               }
        } else {
                gpio_set_config(desc, PIN_CONFIG_DRIVE_PUSH_PULL);
        }

drivers/gpio/gpiolib.c int gpiod_direction_output_nonotify(struct gpio_desc *desc, int value) · line 2899

Quoted 3 lines
drivers/gpio/gpiolib.c +0 -0
@@ -2901,15 +2897,17 @@ int gpiod_direction_output_nonotify(struct gpio_desc *desc, int value)
        return gpiod_direction_output_raw_commit(desc, value);
 
 set_output_flag:

Ok, I'll take it but please change this label to "emulate_output" or something similar that's more indicative on what the goal here is.

Bart

Quoted 20 lines
drivers/gpio/gpiolib.c +5 -4
+       ret = gpiod_direction_input_nonotify(desc);
+       if (ret)
+               return ret;
        /*
         * 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;
+       set_bit(FLAG_IS_OUT, &desc->flags);
+       return 0;
 }
 
 /**
--

2.43.0.rc1.1336.g36b5255a03ac

#2 lore raw
Quoted 2 lines · Bartosz Golaszewski

On Thu, Oct 31, 2024 at 10:22 AM Andy Shevchenko
<[email protected]> wrote:

...

Quoted 4 lines
Quoted 1 line

set_output_flag:

Ok, I'll take it but please change this label to "emulate_output" or something similar that's more indicative on what the goal here is.

It's out of scope of this patch.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
Quoted 5 lines · Andy Shevchenko
Quoted 2 lines · Bartosz Golaszewski

On Thu, Oct 31, 2024 at 10:22 AM Andy Shevchenko
<[email protected]> wrote:

Quoted 9 lines

...

Quoted 4 lines
Quoted 1 line

set_output_flag:

Ok, I'll take it but please change this label to "emulate_output" or something similar that's more indicative on what the goal here is.

It's out of scope of this patch.

It is not though. You are already touching this code, make it even better.

Just say in the commit message: "while at it, rename the label to ..."

Bartosz