From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>,
linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Linus Walleij <linus.walleij@linaro.org>,
Bartosz Golaszewski <brgl@bgdev.pl>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Subject: [PATCH v1 1/1] gpio: Use traditional pattern when checking error codes
Date: Mon, 28 Oct 2024 15:43:39 +0200 [thread overview]
Message-ID: <20241028134454.1156852-1-andriy.shevchenko@linux.intel.com> (raw)
Instead of 'if (ret == 0)' switch to "check for the error first" rule.
Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
While it gives a "+" (plus) statistics it makes the code easier to read
and maintain (when, e.g., want to add somethning in between touched lines).
drivers/gpio/gpiolib.c | 104 ++++++++++++++++++++++-------------------
1 file changed, 56 insertions(+), 48 deletions(-)
diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 5666c462248c..a9a3e032ed5b 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -2674,10 +2674,11 @@ int gpio_set_debounce_timeout(struct gpio_desc *desc, unsigned int debounce)
ret = gpio_set_config_with_argument_optional(desc,
PIN_CONFIG_INPUT_DEBOUNCE,
debounce);
- if (!ret)
- gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
+ if (ret)
+ return ret;
- return ret;
+ gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
+ return 0;
}
/**
@@ -2697,16 +2698,17 @@ int gpiod_direction_input(struct gpio_desc *desc)
VALIDATE_DESC(desc);
ret = gpiod_direction_input_nonotify(desc);
- if (ret == 0)
- gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
+ if (ret)
+ return ret;
- return ret;
+ gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
+ return 0;
}
EXPORT_SYMBOL_GPL(gpiod_direction_input);
int gpiod_direction_input_nonotify(struct gpio_desc *desc)
{
- int ret = 0;
+ int ret;
CLASS(gpio_chip_guard, guard)(desc);
if (!guard.gc)
@@ -2733,6 +2735,8 @@ int gpiod_direction_input_nonotify(struct gpio_desc *desc)
if (guard.gc->direction_input) {
ret = guard.gc->direction_input(guard.gc,
gpio_chip_hwgpio(desc));
+ if (ret)
+ goto out_trace_direction;
} else if (guard.gc->get_direction &&
(guard.gc->get_direction(guard.gc,
gpio_chip_hwgpio(desc)) != 1)) {
@@ -2741,11 +2745,11 @@ int gpiod_direction_input_nonotify(struct gpio_desc *desc)
__func__);
return -EIO;
}
- if (ret == 0) {
- clear_bit(FLAG_IS_OUT, &desc->flags);
- ret = gpio_set_bias(desc);
- }
+ clear_bit(FLAG_IS_OUT, &desc->flags);
+ ret = gpio_set_bias(desc);
+
+out_trace_direction:
trace_gpio_direction(desc_to_gpio(desc), 1, ret);
return ret;
@@ -2774,6 +2778,8 @@ static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
if (guard.gc->direction_output) {
ret = guard.gc->direction_output(guard.gc,
gpio_chip_hwgpio(desc), val);
+ if (ret)
+ goto out_trace_value_and_direction;
} else {
/* Check that we are in output mode if we can */
if (guard.gc->get_direction &&
@@ -2790,8 +2796,9 @@ static int gpiod_direction_output_raw_commit(struct gpio_desc *desc, int value)
guard.gc->set(guard.gc, gpio_chip_hwgpio(desc), val);
}
- if (!ret)
- set_bit(FLAG_IS_OUT, &desc->flags);
+ set_bit(FLAG_IS_OUT, &desc->flags);
+
+out_trace_value_and_direction:
trace_gpio_value(desc_to_gpio(desc), 0, val);
trace_gpio_direction(desc_to_gpio(desc), 0, ret);
return ret;
@@ -2816,10 +2823,11 @@ int gpiod_direction_output_raw(struct gpio_desc *desc, int value)
VALIDATE_DESC(desc);
ret = gpiod_direction_output_raw_commit(desc, value);
- if (ret == 0)
- gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
+ if (ret)
+ return ret;
- return ret;
+ gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
+ return 0;
}
EXPORT_SYMBOL_GPL(gpiod_direction_output_raw);
@@ -2843,10 +2851,11 @@ int gpiod_direction_output(struct gpio_desc *desc, int value)
VALIDATE_DESC(desc);
ret = gpiod_direction_output_nonotify(desc, value);
- if (ret == 0)
- gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
+ if (ret)
+ return ret;
- return ret;
+ gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
+ return 0;
}
EXPORT_SYMBOL_GPL(gpiod_direction_output);
@@ -2877,19 +2886,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 +2906,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;
}
/**
@@ -2994,25 +3001,25 @@ int gpiod_set_config(struct gpio_desc *desc, unsigned long config)
VALIDATE_DESC(desc);
ret = gpio_do_set_config(desc, config);
- if (!ret) {
- /* These are the only options we notify the userspace about. */
- switch (pinconf_to_config_param(config)) {
- case PIN_CONFIG_BIAS_DISABLE:
- case PIN_CONFIG_BIAS_PULL_DOWN:
- case PIN_CONFIG_BIAS_PULL_UP:
- case PIN_CONFIG_DRIVE_OPEN_DRAIN:
- case PIN_CONFIG_DRIVE_OPEN_SOURCE:
- case PIN_CONFIG_DRIVE_PUSH_PULL:
- case PIN_CONFIG_INPUT_DEBOUNCE:
- gpiod_line_state_notify(desc,
- GPIO_V2_LINE_CHANGED_CONFIG);
- break;
- default:
- break;
- }
+ if (ret)
+ return ret;
+
+ /* These are the only options we notify the userspace about */
+ switch (pinconf_to_config_param(config)) {
+ case PIN_CONFIG_BIAS_DISABLE:
+ case PIN_CONFIG_BIAS_PULL_DOWN:
+ case PIN_CONFIG_BIAS_PULL_UP:
+ case PIN_CONFIG_DRIVE_OPEN_DRAIN:
+ case PIN_CONFIG_DRIVE_OPEN_SOURCE:
+ case PIN_CONFIG_DRIVE_PUSH_PULL:
+ case PIN_CONFIG_INPUT_DEBOUNCE:
+ gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
+ break;
+ default:
+ break;
}
- return ret;
+ return 0;
}
EXPORT_SYMBOL_GPL(gpiod_set_config);
@@ -3730,10 +3737,11 @@ int gpiod_set_consumer_name(struct gpio_desc *desc, const char *name)
VALIDATE_DESC(desc);
ret = desc_set_label(desc, name);
- if (ret == 0)
- gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
+ if (ret)
+ return ret;
- return ret;
+ gpiod_line_state_notify(desc, GPIO_V2_LINE_CHANGED_CONFIG);
+ return 0;
}
EXPORT_SYMBOL_GPL(gpiod_set_consumer_name);
--
2.43.0.rc1.1336.g36b5255a03ac
next reply other threads:[~2024-10-28 13:44 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-28 13:43 Andy Shevchenko [this message]
2024-10-28 22:10 ` [PATCH v1 1/1] gpio: Use traditional pattern when checking error codes Linus Walleij
2024-10-30 20:20 ` Bartosz Golaszewski
2024-10-31 9:15 ` Andy Shevchenko
2024-10-31 18:23 ` Bartosz Golaszewski
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20241028134454.1156852-1-andriy.shevchenko@linux.intel.com \
--to=andriy.shevchenko@linux.intel.com \
--cc=bartosz.golaszewski@linaro.org \
--cc=brgl@bgdev.pl \
--cc=linus.walleij@linaro.org \
--cc=linux-gpio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).