Skip to contentSkip to conversation

iio: mlx90614: fix missing GPIO direction return value checks

PATCH In review Salah Triki 4 participants 8 messages Apr 27 – May 5

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: mlx90614: fix missing GPIO direction return value checks

The functions gpiod_direction_output() and gpiod_direction_input() can fail, but their return values were previously ignored.

If an error occurs during the GPIO configuration, the function should abort the wake-up sequence and return the error code. More importantly, failing to check these values could lead to the I2C bus remaining locked if an error occurs after i2c_lock_bus() is called.

Add return value checks and ensure the I2C bus is properly unlocked via a goto label in case of failure.

Fixes eb4b07dae4d4 ("iio: mlx90614: Add power management")
---
 drivers/iio/temperature/mlx90614.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/iio/temperature/mlx90614.c +15 -2
index 1ad21b73e1b4..ce12b44c84e0 100644
@@ -489,6 +489,7 @@ static int mlx90614_sleep(struct mlx90614_data *data)
 static int mlx90614_wakeup(struct mlx90614_data *data)
 {
 	const struct mlx_chip_info *chip_info = data->chip_info;
+	int ret;
 
 	if (!data->wakeup_gpio) {
 		dev_dbg(&data->client->dev, "Wake-up disabled");
@@ -498,9 +499,17 @@ static int mlx90614_wakeup(struct mlx90614_data *data)
 	dev_dbg(&data->client->dev, "Requesting wake-up");
 
 	i2c_lock_bus(data->client->adapter, I2C_LOCK_ROOT_ADAPTER);
-	gpiod_direction_output(data->wakeup_gpio, 0);
+
+	ret = gpiod_direction_output(data->wakeup_gpio, 0);
+	if (ret)
+		goto out_unlock;
+
 	msleep(chip_info->wakeup_delay_ms);
-	gpiod_direction_input(data->wakeup_gpio);
+
+	ret = gpiod_direction_input(data->wakeup_gpio);
+	if (ret)
+		goto out_unlock;
+
 	i2c_unlock_bus(data->client->adapter, I2C_LOCK_ROOT_ADAPTER);
 
 	data->ready_timestamp = jiffies +
@@ -515,6 +524,10 @@ static int mlx90614_wakeup(struct mlx90614_data *data)
 	i2c_smbus_read_word_data(data->client, chip_info->op_eeprom_config1);
 
 	return 0;
+
+out_unlock:
+	i2c_unlock_bus(data->client->adapter, I2C_LOCK_ROOT_ADAPTER);
+	return ret;
 }
 
 /* Return wake-up GPIO or NULL if sleep functionality should be disabled. */
Signature · 1 line
2.43.0
#1 lore raw
Quoted 10 lines · Salah Triki

The functions gpiod_direction_output() and gpiod_direction_input() can fail, but their return values were previously ignored.

If an error occurs during the GPIO configuration, the function should abort the wake-up sequence and return the error code. More importantly, failing to check these values could lead to the I2C bus remaining locked if an error occurs after i2c_lock_bus() is called.

Add return value checks and ensure the I2C bus is properly unlocked via a goto label in case of failure.

...

drivers/iio/temperature/mlx90614.c static int mlx90614_wakeup(struct mlx90614_data *data) · line 511

Quoted 12 lines
drivers/iio/temperature/mlx90614.c +9 -2
-	gpiod_direction_output(data->wakeup_gpio, 0);
+
+	ret = gpiod_direction_output(data->wakeup_gpio, 0);
+	if (ret)
+		goto out_unlock;
+
 	msleep(chip_info->wakeup_delay_ms);
-	gpiod_direction_input(data->wakeup_gpio);
+
+	ret = gpiod_direction_input(data->wakeup_gpio);
+	if (ret)
+		goto out_unlock;

While technically it sounds correct, the potential problem here is that you may fail this in case CONFIG_GPIOLIB=n. Is this GPIO optional?
What may happen if GPIO is not optional, but for some reason setting it fails?

TL;DR:
I am not sure about this patch. At least I'm not comfortable to take it without testing on real HW.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 31 lines · Andy Shevchenko
Quoted 10 lines · Salah Triki

The functions gpiod_direction_output() and gpiod_direction_input() can fail, but their return values were previously ignored.

If an error occurs during the GPIO configuration, the function should abort the wake-up sequence and return the error code. More importantly, failing to check these values could lead to the I2C bus remaining locked if an error occurs after i2c_lock_bus() is called.

Add return value checks and ensure the I2C bus is properly unlocked via a goto label in case of failure.

...

drivers/iio/temperature/mlx90614.c static int mlx90614_wakeup(struct mlx90614_data *data) · line 511

Quoted 12 lines
drivers/iio/temperature/mlx90614.c +9 -2
-	gpiod_direction_output(data->wakeup_gpio, 0);
+
+	ret = gpiod_direction_output(data->wakeup_gpio, 0);
+	if (ret)
+		goto out_unlock;
+
 	msleep(chip_info->wakeup_delay_ms);
-	gpiod_direction_input(data->wakeup_gpio);
+
+	ret = gpiod_direction_input(data->wakeup_gpio);
+	if (ret)
+		goto out_unlock;

While technically it sounds correct, the potential problem here is that you may fail this in case CONFIG_GPIOLIB=n. Is this GPIO optional?
What may happen if GPIO is not optional, but for some reason setting it fails?

Quoted 9 lines

TL;DR:
I am not sure about this patch. At least I'm not comfortable to take it without testing on real HW.

Signature · 2 lines
With Best Regards,
Andy Shevchenko

Thank you for your feedback. Since I don't have the physical hardware to perform the necessary tests and ensure there are no regressions, I agree that it is safer to drop this patch. I don't want to risk breaking the driver for a minor cleanup.

Best regards,
--
Salah Triki

maintainer
#3 lore raw
Quoted 42 lines · On Tue, 28 Apr 2026 09:38:47 +0100 Salah Triki <[email protected]> wrote:
Quoted 31 lines · Andy Shevchenko
Quoted 10 lines · Salah Triki

The functions gpiod_direction_output() and gpiod_direction_input() can fail, but their return values were previously ignored.

If an error occurs during the GPIO configuration, the function should abort the wake-up sequence and return the error code. More importantly, failing to check these values could lead to the I2C bus remaining locked if an error occurs after i2c_lock_bus() is called.

Add return value checks and ensure the I2C bus is properly unlocked via a goto label in case of failure.

...

drivers/iio/temperature/mlx90614.c static int mlx90614_wakeup(struct mlx90614_data *data) · line 511

Quoted 12 lines
drivers/iio/temperature/mlx90614.c +9 -2
-	gpiod_direction_output(data->wakeup_gpio, 0);
+
+	ret = gpiod_direction_output(data->wakeup_gpio, 0);
+	if (ret)
+		goto out_unlock;
+
 	msleep(chip_info->wakeup_delay_ms);
-	gpiod_direction_input(data->wakeup_gpio);
+
+	ret = gpiod_direction_input(data->wakeup_gpio);
+	if (ret)
+		goto out_unlock;  

While technically it sounds correct, the potential problem here is that you may fail this in case CONFIG_GPIOLIB=n. Is this GPIO optional?
What may happen if GPIO is not optional, but for some reason setting it fails?

Quoted 9 lines

TL;DR:
I am not sure about this patch. At least I'm not comfortable to take it without testing on real HW.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Quoted 5 lines

Thank you for your feedback. Since I don't have the physical hardware to perform the necessary tests and ensure there are no regressions, I agree that it is safer to drop this patch. I don't want to risk breaking the driver for a minor cleanup.

Maybe Crt has the hardware to test. For now I'll assume this is not going anywhere wrt to tracking in patchwork.

Quoted 3 lines

Best regards,
--
Salah Triki

maintainer
#4 lore raw
Quoted 46 lines · Jonathan Cameron
Quoted 42 lines · On Tue, 28 Apr 2026 09:38:47 +0100 Salah Triki <[email protected]> wrote:
Quoted 40 lines · Andy Shevchenko
Quoted 10 lines · Salah Triki

The functions gpiod_direction_output() and gpiod_direction_input() can fail, but their return values were previously ignored.

If an error occurs during the GPIO configuration, the function should abort the wake-up sequence and return the error code. More importantly, failing to check these values could lead to the I2C bus remaining locked if an error occurs after i2c_lock_bus() is called.

Add return value checks and ensure the I2C bus is properly unlocked via a goto label in case of failure.

...

drivers/iio/temperature/mlx90614.c static int mlx90614_wakeup(struct mlx90614_data *data) · line 511

Quoted 12 lines

- gpiod_direction_output(data->wakeup_gpio, 0);

drivers/iio/temperature/mlx90614.c +9 -1
+
+ ret = gpiod_direction_output(data->wakeup_gpio, 0);
+ if (ret)
+         goto out_unlock;
+
  msleep(chip_info->wakeup_delay_ms);
- gpiod_direction_input(data->wakeup_gpio);
+
+ ret = gpiod_direction_input(data->wakeup_gpio);
+ if (ret)
+         goto out_unlock;

While technically it sounds correct, the potential problem here is that you may fail this in case CONFIG_GPIOLIB=n. Is this GPIO optional?
What may happen if GPIO is not optional, but for some reason setting it fails?

TL;DR:
I am not sure about this patch. At least I'm not comfortable to take it without testing on real HW.

--
With Best Regards,
Andy Shevchenko

Quoted 8 lines
Quoted 5 lines

Thank you for your feedback. Since I don't have the physical hardware to perform the necessary tests and ensure there are no regressions, I agree that it is safer to drop this patch. I don't want to risk breaking the driver for a minor cleanup.

Maybe Crt has the hardware to test. For now I'll assume this is not going anywhere wrt to tracking in patchwork.

I will dig the hardware to test as it was off my desk for some time now. It seems like backwards compatible, but I am wondering why would we want to be informed of this failing? The problem is that sensor uses wakeup line (same as for i2c) to receive wakeup pulse, so in theory this is already a workaround. @Salah did you see any issues in the wild?

Quoted 4 lines
Quoted 3 lines

Best regards,
--
Salah Triki

#5 lore raw
Quoted 55 lines · Crt Mori
Quoted 46 lines · Jonathan Cameron
Quoted 42 lines · On Tue, 28 Apr 2026 09:38:47 +0100 Salah Triki <[email protected]> wrote:
Quoted 40 lines · Andy Shevchenko
Quoted 10 lines · Salah Triki

The functions gpiod_direction_output() and gpiod_direction_input() can fail, but their return values were previously ignored.

If an error occurs during the GPIO configuration, the function should abort the wake-up sequence and return the error code. More importantly, failing to check these values could lead to the I2C bus remaining locked if an error occurs after i2c_lock_bus() is called.

Add return value checks and ensure the I2C bus is properly unlocked via a goto label in case of failure.

...

drivers/iio/temperature/mlx90614.c static int mlx90614_wakeup(struct mlx90614_data *data) · line 511

Quoted 12 lines

- gpiod_direction_output(data->wakeup_gpio, 0);

drivers/iio/temperature/mlx90614.c +9 -1
+
+ ret = gpiod_direction_output(data->wakeup_gpio, 0);
+ if (ret)
+         goto out_unlock;
+
  msleep(chip_info->wakeup_delay_ms);
- gpiod_direction_input(data->wakeup_gpio);
+
+ ret = gpiod_direction_input(data->wakeup_gpio);
+ if (ret)
+         goto out_unlock;

While technically it sounds correct, the potential problem here is that you may fail this in case CONFIG_GPIOLIB=n. Is this GPIO optional?
What may happen if GPIO is not optional, but for some reason setting it fails?

TL;DR:
I am not sure about this patch. At least I'm not comfortable to take it without testing on real HW.

--
With Best Regards,
Andy Shevchenko

Quoted 8 lines
Quoted 5 lines

Thank you for your feedback. Since I don't have the physical hardware to perform the necessary tests and ensure there are no regressions, I agree that it is safer to drop this patch. I don't want to risk breaking the driver for a minor cleanup.

Maybe Crt has the hardware to test. For now I'll assume this is not going anywhere wrt to tracking in patchwork.

Quoted 7 lines

I will dig the hardware to test as it was off my desk for some time now. It seems like backwards compatible, but I am wondering why would we want to be informed of this failing? The problem is that sensor uses wakeup line (same as for i2c) to receive wakeup pulse, so in theory this is already a workaround. @Salah did you see any issues in the wild?

Thanks for digging out the hardware to test this!

To answer your question: I haven't observed this failing in the wild on this specific sensor. The patch was initially motivated by a static analysis and general robustness cleanup, ensuring that we don't ignore errors from gpiod_* functions (especially in the context of keeping the I2C bus locked).

However, as Andy pointed out, turning this into a fatal error might not be desirable if the GPIO is optional or if the failure is transient on some platforms. The goal was simply to improve visibility if the GPIO configuration fails.

Best Regards,
--
Salah Triki

maintainer
#6 lore raw
Quoted 73 lines · Salah Triki
Quoted 55 lines · Crt Mori
Quoted 46 lines · Jonathan Cameron
Quoted 42 lines · On Tue, 28 Apr 2026 09:38:47 +0100 Salah Triki <[email protected]> wrote:
Quoted 40 lines · Andy Shevchenko
Quoted 10 lines · Salah Triki

The functions gpiod_direction_output() and gpiod_direction_input() can fail, but their return values were previously ignored.

If an error occurs during the GPIO configuration, the function should abort the wake-up sequence and return the error code. More importantly, failing to check these values could lead to the I2C bus remaining locked if an error occurs after i2c_lock_bus() is called.

Add return value checks and ensure the I2C bus is properly unlocked via a goto label in case of failure.

...

drivers/iio/temperature/mlx90614.c static int mlx90614_wakeup(struct mlx90614_data *data) · line 511

Quoted 12 lines

- gpiod_direction_output(data->wakeup_gpio, 0);

drivers/iio/temperature/mlx90614.c +9 -1
+
+ ret = gpiod_direction_output(data->wakeup_gpio, 0);
+ if (ret)
+         goto out_unlock;
+
  msleep(chip_info->wakeup_delay_ms);
- gpiod_direction_input(data->wakeup_gpio);
+
+ ret = gpiod_direction_input(data->wakeup_gpio);
+ if (ret)
+         goto out_unlock;

While technically it sounds correct, the potential problem here is that you may fail this in case CONFIG_GPIOLIB=n. Is this GPIO optional?
What may happen if GPIO is not optional, but for some reason setting it fails?

TL;DR:
I am not sure about this patch. At least I'm not comfortable to take it without testing on real HW.

--
With Best Regards,
Andy Shevchenko

Quoted 8 lines
Quoted 5 lines

Thank you for your feedback. Since I don't have the physical hardware to perform the necessary tests and ensure there are no regressions, I agree that it is safer to drop this patch. I don't want to risk breaking the driver for a minor cleanup.

Maybe Crt has the hardware to test. For now I'll assume this is not going anywhere wrt to tracking in patchwork.

Quoted 7 lines

I will dig the hardware to test as it was off my desk for some time now. It seems like backwards compatible, but I am wondering why would we want to be informed of this failing? The problem is that sensor uses wakeup line (same as for i2c) to receive wakeup pulse, so in theory this is already a workaround. @Salah did you see any issues in the wild?

Thanks for digging out the hardware to test this!

To answer your question: I haven't observed this failing in the wild on this specific sensor. The patch was initially motivated by a static analysis and general robustness cleanup, ensuring that we don't ignore errors from gpiod_* functions (especially in the context of keeping the I2C bus locked).

Quoted 4 lines

However, as Andy pointed out, turning this into a fatal error might not be desirable if the GPIO is optional or if the failure is transient on some platforms. The goal was simply to improve visibility if the GPIO configuration fails.

This GPIO in this case is basically used for generating wakeup pulse (so when sensor is sleeping) and I doubt there is usefulness of the GPIO error failure for this case from user perspective - there is a use case when debugging.

At the moment I am fairly busy. I have found the sensor, but I need to dig out the some board to port the latest kernel to. I hope I get a reminder from Jonathan as the window gets closed, but I also have trouble

Quoted 4 lines

Best Regards,
--
Salah Triki

maintainer
#7 lore raw
In reply to Salah Triki
Quoted 31 lines · On Mon, 27 Apr 2026 22:58:00 +0100 Salah Triki <[email protected]> wrote:

The functions gpiod_direction_output() and gpiod_direction_input() can fail, but their return values were previously ignored.

If an error occurs during the GPIO configuration, the function should abort the wake-up sequence and return the error code. More importantly, failing to check these values could lead to the I2C bus remaining locked if an error occurs after i2c_lock_bus() is called.

Add return value checks and ensure the I2C bus is properly unlocked via a goto label in case of failure.

Fixes eb4b07dae4d4 ("iio: mlx90614: Add power management")
---
 drivers/iio/temperature/mlx90614.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)
drivers/iio/temperature/mlx90614.c +1 -0
index 1ad21b73e1b4..ce12b44c84e0 100644
@@ -489,6 +489,7 @@ static int mlx90614_sleep(struct mlx90614_data *data)
 static int mlx90614_wakeup(struct mlx90614_data *data)
 {
 	const struct mlx_chip_info *chip_info = data->chip_info;
+	int ret;
 
 	if (!data->wakeup_gpio) {
 		dev_dbg(&data->client->dev, "Wake-up disabled");
@@ -498,9 +499,17 @@ static int mlx90614_wakeup(struct mlx90614_data *data)
 	dev_dbg(&data->client->dev, "Requesting wake-up");

drivers/iio/temperature/mlx90614.c static int mlx90614_wakeup(struct mlx90614_data *data) · line 503

Quoted 10 lines
drivers/iio/temperature/mlx90614.c +5 -2
 
 	i2c_lock_bus(data->client->adapter, I2C_LOCK_ROOT_ADAPTER);
-	gpiod_direction_output(data->wakeup_gpio, 0);
+
+	ret = gpiod_direction_output(data->wakeup_gpio, 0);
+	if (ret)
+		goto out_unlock;
+
 	msleep(chip_info->wakeup_delay_ms);
-	gpiod_direction_input(data->wakeup_gpio);

If this did go forwards, I'd prefer the section under the lock moved into a helper function so that we can do

	ret = helper();
	i2c_unlock_bus();
	if (ret)
		return ret;

Rather than introducing a goto for a section of code in the middle of the function that isn't appropriate if we were to later introduce some error path after this.

Quoted 19 lines
drivers/iio/temperature/mlx90614.c +9 -0
+
+	ret = gpiod_direction_input(data->wakeup_gpio);
+	if (ret)
+		goto out_unlock;
+
 	i2c_unlock_bus(data->client->adapter, I2C_LOCK_ROOT_ADAPTER);
 
 	data->ready_timestamp = jiffies +
@@ -515,6 +524,10 @@ static int mlx90614_wakeup(struct mlx90614_data *data)
 	i2c_smbus_read_word_data(data->client, chip_info->op_eeprom_config1);
 
 	return 0;
+
+out_unlock:
+	i2c_unlock_bus(data->client->adapter, I2C_LOCK_ROOT_ADAPTER);
+	return ret;
 }
 
 /* Return wake-up GPIO or NULL if sleep functionality should be disabled. */