[PATCH] iio: pressure: bmp280: Use IS_ERR_OR_NULL() in bmp280_common_probe()

Salah Triki posted 1 patch 1 month, 4 weeks ago
drivers/iio/pressure/bmp280-core.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
[PATCH] iio: pressure: bmp280: Use IS_ERR_OR_NULL() in bmp280_common_probe()
Posted by Salah Triki 1 month, 4 weeks ago
`devm_gpiod_get_optional()` may return non-NULL error pointer on failure.
Check its return value using `IS_ERR_OR_NULL()` and propagate the error if
necessary.

Signed-off-by: Salah Triki <salah.triki@gmail.com>
---
 drivers/iio/pressure/bmp280-core.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c
index 74505c9ec1a0..2ac0188d2857 100644
--- a/drivers/iio/pressure/bmp280-core.c
+++ b/drivers/iio/pressure/bmp280-core.c
@@ -3213,11 +3213,13 @@ int bmp280_common_probe(struct device *dev,
 
 	/* Bring chip out of reset if there is an assigned GPIO line */
 	gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
+
+	if (IS_ERR_OR_NULL(gpiod))
+		return dev_err_probe(dev, PTR_ERR(gpiod), "failed to get GPIO\n");
+
 	/* Deassert the signal */
-	if (gpiod) {
-		dev_info(dev, "release reset\n");
-		gpiod_set_value(gpiod, 0);
-	}
+	dev_info(dev, "release reset\n");
+	gpiod_set_value(gpiod, 0);
 
 	data->regmap = regmap;
 
-- 
2.43.0
Re: [PATCH] iio: pressure: bmp280: Use IS_ERR_OR_NULL() in bmp280_common_probe()
Posted by David Lechner 1 month, 4 weeks ago
On 8/6/25 7:52 PM, Salah Triki wrote:
> `devm_gpiod_get_optional()` may return non-NULL error pointer on failure.
> Check its return value using `IS_ERR_OR_NULL()` and propagate the error if
> necessary.
> 
> Signed-off-by: Salah Triki <salah.triki@gmail.com>
> ---
>  drivers/iio/pressure/bmp280-core.c | 10 ++++++----
>  1 file changed, 6 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/iio/pressure/bmp280-core.c b/drivers/iio/pressure/bmp280-core.c
> index 74505c9ec1a0..2ac0188d2857 100644
> --- a/drivers/iio/pressure/bmp280-core.c
> +++ b/drivers/iio/pressure/bmp280-core.c
> @@ -3213,11 +3213,13 @@ int bmp280_common_probe(struct device *dev,
>  
>  	/* Bring chip out of reset if there is an assigned GPIO line */
>  	gpiod = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIGH);
> +

No blank line here.

> +	if (IS_ERR_OR_NULL(gpiod))

This needs to be IS_ERR(). NULL is not an error and we
cant return early here because there is more to do in
the probe function.

> +		return dev_err_probe(dev, PTR_ERR(gpiod), "failed to get GPIO\n");
> +
>  	/* Deassert the signal */
> -	if (gpiod) {
> -		dev_info(dev, "release reset\n");
> -		gpiod_set_value(gpiod, 0);
> -	}
> +	dev_info(dev, "release reset\n");
> +	gpiod_set_value(gpiod, 0);

If we drop the `if` here, we should also drop the
dev_info(). gpiod_set_value() handles NULL gpiod value
so that is fine, but the message is just noise.

Also, gpiod_set_value_cansleep() would be more
appropriate. This is not an atomic context.

>  
>  	data->regmap = regmap;
>
Re: [PATCH] iio: pressure: bmp280: Use IS_ERR_OR_NULL() in bmp280_common_probe()
Posted by Andy Shevchenko 1 month, 4 weeks ago
On Thu, Aug 7, 2025 at 3:24 AM David Lechner <dlechner@baylibre.com> wrote:
> On 8/6/25 7:52 PM, Salah Triki wrote:

...

All what David said I agree with.

> Also, gpiod_set_value_cansleep() would be more
> appropriate. This is not an atomic context.

Just note that the original code doesn't have it, so it may be done in
a separate patch.

-- 
With Best Regards,
Andy Shevchenko