[PATCH v2 4/5] iio: chemical: sgp40: Implement execute_self_test-command

From: Jaakko Koivisto

Date: Mon Nov 02 2026 - 04:16:37 EST


Add functionality for running the SGP40 sensor self-test, testing
the heater and MOX-material.

Datasheet does not clearly state if the chip is completely unusable if
the test fails. Because of this the driver will try to use the chip
normally even if the self-test has failed.

If the test fails to run, the driver probe is aborted.

Signed-off-by: Jaakko Koivisto <jmatko@xxxxxx>
---
Is it ok to fail the probe if the self-test fails? Currently the driver
will probe succesfully even if the device is powered down, or does
not exist at all, so failing the probe is rather significant change
from current behaviour.

Case where self-test reports FAIL was tested using a MCU
mimicking SGP40 instead of real device, due to problems finding any
failed SPG40s.

drivers/iio/chemical/sgp40.c | 58 ++++++++++++++++++++++++++++++++++++
1 file changed, 58 insertions(+)

diff --git a/drivers/iio/chemical/sgp40.c b/drivers/iio/chemical/sgp40.c
index e0a300720fce..278390bc6b04 100644
--- a/drivers/iio/chemical/sgp40.c
+++ b/drivers/iio/chemical/sgp40.c
@@ -83,6 +83,19 @@ struct sgp40_serial_number_result {
u8 C_crc;
} __packed;

+/*
+ * Datasheet table 13.
+ */
+#define SGP40_SELF_TEST_PASS 0xD4
+#define SGP40_SELF_TEST_FAIL 0x4B
+
+struct sgp40_self_test_result {
+ u8 data;
+ u8 _ignore;
+ u8 crc;
+} __packed;
+
+
static const struct iio_chan_spec sgp40_channels[] = {
{
.type = IIO_CONCENTRATION,
@@ -209,6 +222,47 @@ static int sgp40_get_serial_number(struct sgp40_data *data, u64 *serial_number)
return 0;
}

+static int sgp40_execute_self_test(struct sgp40_data *data)
+{
+ int ret;
+ struct i2c_client *client = data->client;
+ struct sgp40_tg_measure run_test = { .command = {0x28, 0x0E} };
+ struct sgp40_self_test_result res;
+
+ guard(mutex)(&data->lock);
+
+ ret = i2c_master_send(client, (char *)&run_test, sizeof(run_test.command));
+ if (ret < 0)
+ return ret;
+ if (ret != sizeof(run_test.command))
+ return dev_err_probe(data->dev, -EIO,
+ "i2c_master_send ret: %d, expected %zu\n", ret, sizeof(run_test.command));
+
+ msleep(320); /* Max duration for self-test command. Datasheet table 8. */
+
+ ret = i2c_master_recv(client, (char *)&res, sizeof(res));
+ if (ret < 0)
+ return ret;
+ if (ret != sizeof(res))
+ return dev_err_probe(data->dev, -EIO,
+ "i2c_master_recv ret: %d, expected: %zu\n", ret, sizeof(res));
+
+ if (crc8(sgp40_crc8_table, (u8 *)&res, 2, SGP40_CRC8_INIT) != res.crc)
+ return dev_err_probe(data->dev, -EIO, "CRC error in execute_self_test\n");
+
+ dev_dbg(data->dev, "self test result: 0x%x\n", res.data);
+ switch (res.data) {
+ case SGP40_SELF_TEST_PASS:
+ return 0;
+ case SGP40_SELF_TEST_FAIL:
+ dev_warn(data->dev, "Device self-test failed, check data validity\n");
+ return 0;
+ default:
+ dev_err(data->dev, "Invalid self-test response from device\n");
+ return -EIO;
+ }
+}
+
static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance_raw)
{
int ret;
@@ -418,6 +472,10 @@ static int sgp40_probe(struct i2c_client *client)
indio_dev->channels = sgp40_channels;
indio_dev->num_channels = ARRAY_SIZE(sgp40_channels);

+ ret = sgp40_execute_self_test(data);
+ if (ret)
+ return dev_err_probe(dev, ret, "Failed to run device self-test\n");
+
ret = devm_iio_device_register(dev, indio_dev);
if (ret)
dev_err(dev, "failed to register iio device\n");
--
2.56.0