From: Jaakko Koivisto <jmatko@utu.fi>
To: "Andreas Klinger" <ak@it-klinger.de>,
"Jonathan Cameron" <jic23@kernel.org>,
"David Lechner" <dlechner@baylibre.com>,
"Nuno Sá" <nuno.sa@analog.com>,
"Andy Shevchenko" <andy@kernel.org>
Cc: <linux-iio@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
Jaakko Koivisto <jmatko@utu.fi>
Subject: [PATCH v2 5/5] iio: chemical: sgp40: Implement turn_heater_off-command
Date: Mon, 2 Nov 2026 11:56:02 +0200 [thread overview]
Message-ID: <20261102095602.333752-1-jmatko@utu.fi> (raw)
In-Reply-To: <20261102091258.247402-1-jmatko@utu.fi>
Add functionality for turning the SGP40 sensor heating element
on or off manually. After turning the heater off, the sensor enters
idle mode. Track the state of the heater in the driver, as the device
does not expose current heater state via any register.
Turning the heater off 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@utu.fi>
---
drivers/iio/chemical/sgp40.c | 62 +++++++++++++++++++++++++++++++++++-
1 file changed, 61 insertions(+), 1 deletion(-)
diff --git a/drivers/iio/chemical/sgp40.c b/drivers/iio/chemical/sgp40.c
index 278390bc6b04..87df8d79fe6e 100644
--- a/drivers/iio/chemical/sgp40.c
+++ b/drivers/iio/chemical/sgp40.c
@@ -54,7 +54,8 @@ struct sgp40_data {
int rht;
int temp;
int res_calibbias;
- /* Prevent concurrent access to rht, tmp, calibbias */
+ bool heater_enabled;
+ /* Prevent concurrent access to rht, tmp, calibbias, heater_enabled */
struct mutex lock;
};
@@ -263,6 +264,27 @@ 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_tg_measure turn_off = { .command = {0x36, 0x15} };
+
+ guard(mutex)(&data->lock);
+
+ ret = i2c_master_send(client, (char *)&turn_off, sizeof(turn_off.command));
+ if (ret < 0)
+ return ret;
+ if (ret != sizeof(turn_off.command))
+ return dev_err_probe(data->dev, -EIO,
+ "i2c_master_send ret: %d, expected %zu", ret, sizeof(turn_off.command));
+
+ fsleep(1000); /* Max duration for command turn_heater_off. Datasheet table 8. */
+ data->heater_enabled = false;
+
+ return 0;
+}
+
static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance_raw)
{
int ret;
@@ -293,6 +315,7 @@ static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance
return -EIO;
}
msleep(30);
+ data->heater_enabled = true;
ret = i2c_master_recv(client, (u8 *)&tgres, sizeof(tgres));
if (ret < 0)
@@ -423,10 +446,47 @@ static ssize_t serialnumber_show(struct device *dev,
return sysfs_emit_at(buf, 0, "%llu\n", serial_number);
}
+static ssize_t heater_enable_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t len)
+{
+ int ret;
+ u16 __ignore;
+ bool val;
+ struct sgp40_data *data = iio_priv(dev_to_iio_dev(dev));
+
+ ret = kstrtobool(buf, &val);
+ if (ret)
+ return ret;
+
+ if (val)
+ ret = sgp40_measure_resistance_raw(data, &__ignore);
+ else
+ ret = sgp40_turn_heater_off(data);
+
+ if (ret)
+ return ret;
+
+ return len;
+}
+
+static ssize_t heater_enable_show(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ struct sgp40_data *data = iio_priv(dev_to_iio_dev(dev));
+
+ guard(mutex)(&data->lock);
+
+ return sysfs_emit(buf, "%d\n", data->heater_enabled);
+}
+
static IIO_DEVICE_ATTR_RO(serialnumber, 0);
+static IIO_DEVICE_ATTR_RW(heater_enable, 0);
static struct attribute *sgp40_attributes[] = {
&iio_dev_attr_serialnumber.dev_attr.attr,
+ &iio_dev_attr_heater_enable.dev_attr.attr,
NULL
};
--
2.56.0
next prev parent reply other threads:[~2026-11-02 9:57 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-02 9:12 [PATCH v2 0/5] iio: chemical: sgp40: add funtions for serial number readback, self-test, and heater control Jaakko Koivisto
2026-11-02 9:12 ` [PATCH v2 1/5] iio: chemical: sgp40: Do not convert I2C errors to EIO Jaakko Koivisto
2026-11-02 9:54 ` Joshua Crofts
2026-11-02 9:12 ` [PATCH v2 2/5] iio: chemical: sgp40: Replace manual locking with guard() Jaakko Koivisto
2026-11-02 10:10 ` Joshua Crofts
2026-11-02 9:12 ` [PATCH v2 3/5] iio: chemical: sgp40: Implement get_serial_number-command Jaakko Koivisto
2026-11-02 10:20 ` Joshua Crofts
2026-11-02 9:12 ` [PATCH v2 4/5] iio: chemical: sgp40: Implement execute_self_test-command Jaakko Koivisto
2026-11-02 10:21 ` Joshua Crofts
2026-11-02 9:56 ` Jaakko Koivisto [this message]
2026-11-02 10:24 ` [PATCH v2 5/5] iio: chemical: sgp40: Implement turn_heater_off-command Joshua Crofts
2026-11-02 9:15 Jaakko Koivisto
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=20261102095602.333752-1-jmatko@utu.fi \
--to=jmatko@utu.fi \
--cc=ak@it-klinger.de \
--cc=andy@kernel.org \
--cc=dlechner@baylibre.com \
--cc=jic23@kernel.org \
--cc=linux-iio@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nuno.sa@analog.com \
/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
all inboxes | Powered by JetHome®