Re: [PATCH 3/3] iio: chemical: sgp40: Implement turn_heater_off-command
From: Andreas Klinger
Date: Fri Sep 18 2026 - 20:10:13 EST
Hi Jaakko,
Jaakko Koivisto <jmatko@xxxxxx> schrieb am Fr, 18. Sep 16:40:
> -Turn the heating element off and enter idle mode.
> -Present the functionality as device attribute,
> 'echo 1 > turn_heater_off'.
Instead of introducing a device specific attribute couldn't this be implemented
as standard power management operations (RUNTIME_PM_OPS)?
Andreas
> Saves approx. 2.5 mA compared to regular operation. The heating element
> is automatically turned back on when measurement is performed.
>
> Signed-off-by: Jaakko Koivisto <jmatko@xxxxxx>
> ---
> drivers/iio/chemical/sgp40.c | 28 ++++++++++++++++++++++++++++
> 1 file changed, 28 insertions(+)
>
> diff --git a/drivers/iio/chemical/sgp40.c b/drivers/iio/chemical/sgp40.c
> index 28d5e737d1dc..a4fc5c778303 100644
> --- a/drivers/iio/chemical/sgp40.c
> +++ b/drivers/iio/chemical/sgp40.c
> @@ -29,6 +29,7 @@
> * by writing to the out values of temp and humidityrelative.
> */
>
> +#include "linux/device.h"
> #include <linux/delay.h>
> #include <linux/crc8.h>
> #include <linux/module.h>
> @@ -259,6 +260,21 @@ static int sgp40_execute_self_test(struct sgp40_data *data)
> }
> }
>
> +static int sgp40_turn_heater_off(struct sgp40_data *data)
> +{
> + int ret;
> + struct i2c_client *client = data->client;
> + struct sgp40_command turn_off = {.command = {0x36, 0x15}};
> +
> + ret = i2c_master_send(client, (char*)&turn_off, sizeof(turn_off.command));
> + if (ret != sizeof(turn_off.command)) {
> + dev_err(data->dev, "i2c_master_send ret: %d, expected %zu", ret, sizeof(turn_off.command));
> + return -EIO;
> + }
> + msleep(1);
> + return 0;
> +}
> +
> static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance_raw)
> {
> int ret;
> @@ -417,10 +433,22 @@ static ssize_t serial_number_show(struct device *dev,
> return sysfs_emit_at(buf, 0, "%llu\n", data->serial_number);
> }
>
> +static ssize_t turn_heater_off_store(struct device *dev,
> + struct device_attribute *attr,
> + const char *buf, size_t len)
> +{
> + struct sgp40_data *data = iio_priv(dev_to_iio_dev(dev));
> + sgp40_turn_heater_off(data);
> +
> + return len;
> +}
> +
> static IIO_DEVICE_ATTR_RO(serial_number, 0);
> +static IIO_DEVICE_ATTR_WO(turn_heater_off, 0);
>
> static struct attribute *sgp40_attributes[] = {
> &iio_dev_attr_serial_number.dev_attr.attr,
> + &iio_dev_attr_turn_heater_off.dev_attr.attr,
> NULL
> };
>
> --
> 2.55.0
>