Skip to contentSkip to conversation

iio: chemical: sgp40: Add secondary chip functionality

PATCH Superseded Jaakko Koivisto 5 participants 16 messages Sep 18 – Sep 25

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover iio: chemical: sgp40: Add secondary chip functionality

Add functions for getting Sensirion SGP40 VOC sensor serial number, running self-test, and turning the heating element off.

Tested with adafruit SGP40 and waveshare SGP40 breakout boards using Raspberry Pi 3 and Raspberry Pi 1.

Jaakko Koivisto (3):
iio: chemical: sgp40: Implement get_serial_number-command
iio: chemical: sgp40: Implement execute_self_test-command
iio: chemical: sgp40: Implement turn_heater_off-command

 drivers/iio/chemical/sgp40.c | 159 ++++++++++++++++++++++++++++++++++-
 1 file changed, 157 insertions(+), 2 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/3 iio: chemical: sgp40: Implement get_serial_number-command

-Retrieve the chip serial number.
-Present the serial number to userspace as device attribute. -Rename the tg_measure -struct now that is is used for multiple commands.

---
 drivers/iio/chemical/sgp40.c | 78 +++++++++++++++++++++++++++++++++++-
 1 file changed, 76 insertions(+), 2 deletions(-)
drivers/iio/chemical/sgp40.c +76 -2
index b2b5e32a9eb2..c1e2a992ec2a 100644
@@ -35,6 +35,7 @@
 #include <linux/mutex.h>
 #include <linux/i2c.h>
 #include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
 
 /*
  * floating point calculation of voc is done as integer
@@ -53,11 +54,12 @@ struct sgp40_data {
 	int			rht;
 	int			temp;
 	int			res_calibbias;
+	u64			serial_number;
 	/* Prevent concurrent access to rht, tmp, calibbias */
 	struct mutex		lock;
 };
 
-struct sgp40_tg_measure {
+struct sgp40_command {
 	u8	command[2];
 	__be16	rht_ticks;
 	u8	rht_crc;
@@ -70,6 +72,18 @@ struct sgp40_tg_result {
 	u8	res_crc;
 } __packed;
 
+/*
+ * Datasheet table 16. Serial number is given as 48-bit value 0xAAAABBBBCCCC.
+ */
+struct sgp40_serial_number_result {
+	__be16	A;
+	u8	A_crc;
+	__be16	B;
+	u8	B_crc;
+	__be16	C;
+	u8	C_crc;
+} __packed;
+
 static const struct iio_chan_spec sgp40_channels[] = {
 	{
 		.type = IIO_CONCENTRATION,
@@ -162,6 +176,40 @@ static int sgp40_calc_voc(struct sgp40_data *data, u16 resistance_raw, int *voc)
 	return 0;
 }
 
+static int sgp40_get_serial_number(struct sgp40_data *data)
+{
+	int ret;
+	struct i2c_client *client = data->client;
+	struct sgp40_command get_sn = {.command = {0x36, 0x82}};
+	struct sgp40_serial_number_result res;
+
+	ret = i2c_master_send(client, (char*)&get_sn, sizeof(get_sn.command));
+	if (ret != sizeof(get_sn.command)) {
+		dev_err(data->dev, "i2c_master_send ret: %d, expected %zu", ret, sizeof(get_sn.command));
+		return -EIO;
+	}
+	msleep(1);
+	ret = i2c_master_recv(client, (char*)&res, sizeof(res));
+	if (ret < 0)
+		return ret;
+	if (ret != sizeof(res)) {
+		dev_err(data->dev, "i2c_master_recv ret: %d, expected: %zu", ret, sizeof(res));
+		return -EIO;
+	}
+
+	if (crc8(sgp40_crc8_table, (u8*)&res.A, 2, SGP40_CRC8_INIT) != res.A_crc ||
+	    crc8(sgp40_crc8_table, (u8*)&res.B, 2, SGP40_CRC8_INIT) != res.B_crc ||
+	    crc8(sgp40_crc8_table, (u8*)&res.C, 2, SGP40_CRC8_INIT) != res.C_crc)
+	{
+		dev_warn(data->dev, "CRC error in get_serial_number");
+	}
+
+	data->serial_number = 0LL | ((u64)be16_to_cpu(res.A) << 32) | ((u64)be16_to_cpu(res.B) << 16) | (u64)be16_to_cpu(res.C);
+	dev_dbg(data->dev, "serial number: %llu", data->serial_number);
+
+	return 0;
+}
+
 static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance_raw)
 {
 	int ret;
@@ -169,7 +217,7 @@ static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance
 	u32 ticks;
 	u16 ticks16;
 	u8 crc;
-	struct sgp40_tg_measure tg = {.command = {0x26, 0x0F}};
+	struct sgp40_command tg = {.command = {0x26, 0x0F}};
 	struct sgp40_tg_result tgres;
 
 	mutex_lock(&data->lock);
@@ -311,9 +359,31 @@ static int sgp40_write_raw(struct iio_dev *indio_dev,
 	return -EINVAL;
 }
 
+static ssize_t serial_number_show(struct device *dev,
+				  struct device_attribute *attr,
+				  char *buf)
+{
+	struct sgp40_data *data = iio_priv(dev_to_iio_dev(dev));
+
+	return sysfs_emit_at(buf, 0, "%llu\n", data->serial_number);
+}
+
+static IIO_DEVICE_ATTR_RO(serial_number, 0);
+
+static struct attribute *sgp40_attributes[] = {
+	&iio_dev_attr_serial_number.dev_attr.attr,
+	NULL
+};
+
+static struct attribute_group sgp40_attribute_group = {
+	.attrs = sgp40_attributes,
+};
+
+
 static const struct iio_info sgp40_info = {
 	.read_raw	= sgp40_read_raw,
 	.write_raw	= sgp40_write_raw,
+	.attrs		= &sgp40_attribute_group,
 };
 
 static int sgp40_probe(struct i2c_client *client)
@@ -347,6 +417,10 @@ static int sgp40_probe(struct i2c_client *client)
 	indio_dev->channels = sgp40_channels;
 	indio_dev->num_channels = ARRAY_SIZE(sgp40_channels);
 
+	ret = sgp40_get_serial_number(data);
+	if (ret)
+		dev_warn(dev, "failed to retrieve device serial number\n");
+
 	ret = devm_iio_device_register(dev, indio_dev);
 	if (ret)
 		dev_err(dev, "failed to register iio device\n");
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH 1/3] iio: chemical: sgp40: Implement get_serial_number-command

Hi there Jaakko,

Firstly a (rather important) question I have is what's the point? I'm not sure that anyone will need the serial number on the fly (assuming a business would be using this, they will likely keep records of their parts). Though maybe I could be wrong (so feel free to prove me wrong).

Quoted 5 lines · CDT Jaakko Koivisto
(quoted diff) +0 -3
-Retrieve the chip serial number.
-Present the serial number to userspace as device attribute.
-Rename the tg_measure -struct now that is is used for multiple

commands.

Last one should be left out and put in a separate patch.

Also, commit message needs a bit of work, something like:
"Add support to the SGP40 driver to enable retrieval of the serial number from the chip and add new sysfs attribute to expose the serial number to userspace."

Quoted 32 lines
---
 drivers/iio/chemical/sgp40.c | 78 +++++++++++++++++++++++++++++++++++-
 1 file changed, 76 insertions(+), 2 deletions(-)
drivers/iio/chemical/sgp40.c +3 -1
index b2b5e32a9eb2..c1e2a992ec2a 100644
@@ -35,6 +35,7 @@
 #include <linux/mutex.h>
 #include <linux/i2c.h>
 #include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
 
 /*
  * floating point calculation of voc is done as integer
@@ -53,11 +54,12 @@ struct sgp40_data {
 	int			rht;
 	int			temp;
 	int			res_calibbias;
+	u64			serial_number;
 	/* Prevent concurrent access to rht, tmp, calibbias */
 	struct mutex		lock;
 };
 
-struct sgp40_tg_measure {
+struct sgp40_command {
 	u8	command[2];
 	__be16	rht_ticks;
 	u8	rht_crc;

drivers/iio/chemical/sgp40.c struct sgp40_tg_result { · line 74

Quoted 3 lines
drivers/iio/chemical/sgp40.c +0 -0
@@ -70,6 +72,18 @@ struct sgp40_tg_result {
 	u8	res_crc;
 } __packed;
 

Name change should be put in a different patch or just left out entirely.

drivers/iio/chemical/sgp40.c struct sgp40_tg_result { · line 86

Quoted 12 lines
drivers/iio/chemical/sgp40.c +12 -0
+/*
+ * Datasheet table 16. Serial number is given as 48-bit value 0xAAAABBBBCCCC.
+ */
+struct sgp40_serial_number_result {
+	__be16	A;
+	u8	A_crc;
+	__be16	B;
+	u8	B_crc;
+	__be16	C;
+	u8	C_crc;
+} __packed;
+

Perhaps this but maybe this could be implemented as an annonymous struct instead.

Quoted 7 lines
drivers/iio/chemical/sgp40.c +0 -0
 static const struct iio_chan_spec sgp40_channels[] = {
 	{
 		.type = IIO_CONCENTRATION,
@@ -162,6 +176,40 @@ static int sgp40_calc_voc(struct sgp40_data *data, u16 resistance_raw, int *voc)
 	return 0;
 }
 

drivers/iio/chemical/sgp40.c static int sgp40_calc_voc(struct sgp40_data *data, u16 resistance_raw, int *voc) · line 188

Quoted 10 lines
drivers/iio/chemical/sgp40.c +10 -0
+static int sgp40_get_serial_number(struct sgp40_data *data)
+{
+	int ret;
+	struct i2c_client *client = data->client;
+	struct sgp40_command get_sn = {.command = {0x36, 0x82}};
+	struct sgp40_serial_number_result res;
+
+	ret = i2c_master_send(client, (char*)&get_sn, sizeof(get_sn.command));
+	if (ret != sizeof(get_sn.command)) {
+		dev_err(data->dev, "i2c_master_send ret: %d, expected %zu", ret, sizeof(get_sn.command));

Missing '\n' here (sashiko).

Quoted 6 lines
drivers/iio/chemical/sgp40.c +6 -0
+		return -EIO;
+	}
+	msleep(1);
+	ret = i2c_master_recv(client, (char*)&res, sizeof(res));
+	if (ret < 0)
+		return ret;

drivers/iio/chemical/sgp40.c static int sgp40_calc_voc(struct sgp40_data *data, u16 resistance_raw, int *voc) · line 204

Quoted 10 lines
drivers/iio/chemical/sgp40.c +10 -0
+	if (ret != sizeof(res)) {
+		dev_err(data->dev, "i2c_master_recv ret: %d, expected: %zu", ret, sizeof(res));
+		return -EIO;
+	}
+
+	if (crc8(sgp40_crc8_table, (u8*)&res.A, 2, SGP40_CRC8_INIT) != res.A_crc ||
+	    crc8(sgp40_crc8_table, (u8*)&res.B, 2, SGP40_CRC8_INIT) != res.B_crc ||
+	    crc8(sgp40_crc8_table, (u8*)&res.C, 2, SGP40_CRC8_INIT) != res.C_crc)
+	{
+		dev_warn(data->dev, "CRC error in get_serial_number");

Probably should return either -EIO or -EREMOTEIO here (sashiko).

Quoted 63 lines
drivers/iio/chemical/sgp40.c +35 -1
+	}
+
+	data->serial_number = 0LL | ((u64)be16_to_cpu(res.A) << 32) | ((u64)be16_to_cpu(res.B) << 16) | (u64)be16_to_cpu(res.C);
+	dev_dbg(data->dev, "serial number: %llu", data->serial_number);
+
+	return 0;
+}
+
 static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance_raw)
 {
 	int ret;
@@ -169,7 +217,7 @@ static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance
 	u32 ticks;
 	u16 ticks16;
 	u8 crc;
-	struct sgp40_tg_measure tg = {.command = {0x26, 0x0F}};
+	struct sgp40_command tg = {.command = {0x26, 0x0F}};
 	struct sgp40_tg_result tgres;
 
 	mutex_lock(&data->lock);
@@ -311,9 +359,31 @@ static int sgp40_write_raw(struct iio_dev *indio_dev,
 	return -EINVAL;
 }
 
+static ssize_t serial_number_show(struct device *dev,
+				  struct device_attribute *attr,
+				  char *buf)
+{
+	struct sgp40_data *data = iio_priv(dev_to_iio_dev(dev));
+
+	return sysfs_emit_at(buf, 0, "%llu\n", data->serial_number);
+}
+
+static IIO_DEVICE_ATTR_RO(serial_number, 0);
+
+static struct attribute *sgp40_attributes[] = {
+	&iio_dev_attr_serial_number.dev_attr.attr,
+	NULL
+};
+
+static struct attribute_group sgp40_attribute_group = {
+	.attrs = sgp40_attributes,
+};
+
+
 static const struct iio_info sgp40_info = {
 	.read_raw	= sgp40_read_raw,
 	.write_raw	= sgp40_write_raw,
+	.attrs		= &sgp40_attribute_group,
 };
 
 static int sgp40_probe(struct i2c_client *client)
@@ -347,6 +417,10 @@ static int sgp40_probe(struct i2c_client *client)
 	indio_dev->channels = sgp40_channels;
 	indio_dev->num_channels = ARRAY_SIZE(sgp40_channels);
 
+	ret = sgp40_get_serial_number(data);
+	if (ret)
+		dev_warn(dev, "failed to retrieve device serial number\n");
+
 	ret = devm_iio_device_register(dev, indio_dev);
 	if (ret)
 		dev_err(dev, "failed to register iio device\n");
#3 lore raw

Re: [PATCH 1/3] iio: chemical: sgp40: Implement get_serial_number-command

Quoted 6 lines · Maxwell Doose

Hi there Jaakko,

Firstly a (rather important) question I have is what's the point? I'm not sure that anyone will need the serial number on the fly (assuming a business would be using this, they will likely keep records of their parts). Though maybe I could be wrong (so feel free to prove me wrong).

I agree that everybody should keep accurate records. Whether or not they do, and those records are fast and easy to access, is another question. Stuff like this gets lost in company mergers, or the records are buried somewhere in manufacturing department archives.
I don't see the downside in having fast and easy way to, for example, check that the records are in fact correct.

You are probably correct that it won't be called for often. Saving the serial number to driver data is probably overkill, and better to just read it when requested. Would remove one variable and less code in probe()-function.

Quoted 4 lines

On Fri Sep 18, 2026 at 8:40 AM CDT
Jaakko Koivisto <[email protected]> wrote:

Quoted 8 lines
Quoted 5 lines
(quoted diff) +0 -3
-Retrieve the chip serial number.
-Present the serial number to userspace as device attribute.
-Rename the tg_measure -struct now that is is used for multiple

commands.

Last one should be left out and put in a separate patch.

Probably best to leave it out, not strictly necessary.

Quoted 6 lines

Also, commit message needs a bit of work, something like:
"Add support to the SGP40 driver to enable retrieval of the serial number from the chip and add new sysfs attribute to expose the serial number to userspace."

Thanks, I'll improve it for v2.

Quoted 26 lines
Quoted 26 lines
---
 drivers/iio/chemical/sgp40.c | 78 +++++++++++++++++++++++++++++++++++-
 1 file changed, 76 insertions(+), 2 deletions(-)
drivers/iio/chemical/sgp40.c +2 -0
index b2b5e32a9eb2..c1e2a992ec2a 100644
@@ -35,6 +35,7 @@
 #include <linux/mutex.h>
 #include <linux/i2c.h>
 #include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
 
 /*
  * floating point calculation of voc is done as integer
@@ -53,11 +54,12 @@ struct sgp40_data {
 	int			rht;
 	int			temp;
 	int			res_calibbias;
+	u64			serial_number;
 	/* Prevent concurrent access to rht, tmp, calibbias */
 	struct mutex		lock;
 };
 
Quoted 13 lines

drivers/iio/chemical/sgp40.c struct sgp40_tg_result { · line 74

Quoted 9 lines
drivers/iio/chemical/sgp40.c +1 -1
-struct sgp40_tg_measure {
+struct sgp40_command {
 	u8	command[2];
 	__be16	rht_ticks;
 	u8	rht_crc;
@@ -70,6 +72,18 @@ struct sgp40_tg_result {
 	u8	res_crc;
 } __packed;
 

Name change should be put in a different patch or just left out entirely.

I'll leave it out.

Quoted 17 lines

drivers/iio/chemical/sgp40.c struct sgp40_tg_result { · line 86

Quoted 13 lines
drivers/iio/chemical/sgp40.c +12 -0
+/*
+ * Datasheet table 16. Serial number is given as 48-bit value 0xAAAABBBBCCCC.
+ */
+struct sgp40_serial_number_result {
+	__be16	A;
+	u8	A_crc;
+	__be16	B;
+	u8	B_crc;
+	__be16	C;
+	u8	C_crc;
+} __packed;
+

Perhaps this but maybe this could be implemented as an annonymous struct instead.

This I would like to leave as it is in order to have all the command and result structs follow the same pattern as the already existing sgp40_tg_result and sgp40_tg_measure.

Quoted 7 lines
Quoted 7 lines
drivers/iio/chemical/sgp40.c +0 -0
 static const struct iio_chan_spec sgp40_channels[] = {
 	{
 		.type = IIO_CONCENTRATION,
@@ -162,6 +176,40 @@ static int sgp40_calc_voc(struct sgp40_data *data, u16 resistance_raw, int *voc)
 	return 0;
 }
 
Quoted 12 lines

drivers/iio/chemical/sgp40.c static int sgp40_calc_voc(struct sgp40_data *data, u16 resistance_raw, int *voc) · line 188

Quoted 10 lines
drivers/iio/chemical/sgp40.c +10 -0
+static int sgp40_get_serial_number(struct sgp40_data *data)
+{
+	int ret;
+	struct i2c_client *client = data->client;
+	struct sgp40_command get_sn = {.command = {0x36, 0x82}};
+	struct sgp40_serial_number_result res;
+
+	ret = i2c_master_send(client, (char*)&get_sn, sizeof(get_sn.command));
+	if (ret != sizeof(get_sn.command)) {
+		dev_err(data->dev, "i2c_master_send ret: %d, expected %zu", ret, sizeof(get_sn.command));

Missing '\n' here (sashiko).

I'll add this and other missing '\n's.

Quoted 10 lines
Quoted 10 lines
drivers/iio/chemical/sgp40.c +10 -0
+		return -EIO;
+	}
+	msleep(1);
+	ret = i2c_master_recv(client, (char*)&res, sizeof(res));
+	if (ret < 0)
+		return ret;
+	if (ret != sizeof(res)) {
+		dev_err(data->dev, "i2c_master_recv ret: %d, expected: %zu", ret, sizeof(res));
+		return -EIO;
+	}
Quoted 9 lines

drivers/iio/chemical/sgp40.c static int sgp40_calc_voc(struct sgp40_data *data, u16 resistance_raw, int *voc) · line 204

Quoted 6 lines
drivers/iio/chemical/sgp40.c +6 -0
+
+	if (crc8(sgp40_crc8_table, (u8*)&res.A, 2, SGP40_CRC8_INIT) != res.A_crc ||
+	    crc8(sgp40_crc8_table, (u8*)&res.B, 2, SGP40_CRC8_INIT) != res.B_crc ||
+	    crc8(sgp40_crc8_table, (u8*)&res.C, 2, SGP40_CRC8_INIT) != res.C_crc)
+	{
+		dev_warn(data->dev, "CRC error in get_serial_number");

Probably should return either -EIO or -EREMOTEIO here (sashiko).

Will change for v2.

Quoted 63 lines
Quoted 63 lines
drivers/iio/chemical/sgp40.c +35 -1
+	}
+
+	data->serial_number = 0LL | ((u64)be16_to_cpu(res.A) << 32) | ((u64)be16_to_cpu(res.B) << 16) | (u64)be16_to_cpu(res.C);
+	dev_dbg(data->dev, "serial number: %llu", data->serial_number);
+
+	return 0;
+}
+
 static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance_raw)
 {
 	int ret;
@@ -169,7 +217,7 @@ static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance
 	u32 ticks;
 	u16 ticks16;
 	u8 crc;
-	struct sgp40_tg_measure tg = {.command = {0x26, 0x0F}};
+	struct sgp40_command tg = {.command = {0x26, 0x0F}};
 	struct sgp40_tg_result tgres;
 
 	mutex_lock(&data->lock);
@@ -311,9 +359,31 @@ static int sgp40_write_raw(struct iio_dev *indio_dev,
 	return -EINVAL;
 }
 
+static ssize_t serial_number_show(struct device *dev,
+				  struct device_attribute *attr,
+				  char *buf)
+{
+	struct sgp40_data *data = iio_priv(dev_to_iio_dev(dev));
+
+	return sysfs_emit_at(buf, 0, "%llu\n", data->serial_number);
+}
+
+static IIO_DEVICE_ATTR_RO(serial_number, 0);
+
+static struct attribute *sgp40_attributes[] = {
+	&iio_dev_attr_serial_number.dev_attr.attr,
+	NULL
+};
+
+static struct attribute_group sgp40_attribute_group = {
+	.attrs = sgp40_attributes,
+};
+
+
 static const struct iio_info sgp40_info = {
 	.read_raw	= sgp40_read_raw,
 	.write_raw	= sgp40_write_raw,
+	.attrs		= &sgp40_attribute_group,
 };
 
 static int sgp40_probe(struct i2c_client *client)
@@ -347,6 +417,10 @@ static int sgp40_probe(struct i2c_client *client)
 	indio_dev->channels = sgp40_channels;
 	indio_dev->num_channels = ARRAY_SIZE(sgp40_channels);
 
+	ret = sgp40_get_serial_number(data);
+	if (ret)
+		dev_warn(dev, "failed to retrieve device serial number\n");
+
 	ret = devm_iio_device_register(dev, indio_dev);
 	if (ret)
 		dev_err(dev, "failed to register iio device\n");
#4 lore raw

Re: [PATCH 1/3] iio: chemical: sgp40: Implement get_serial_number-command

In reply to Jaakko Koivisto
Quoted 4 lines · Jaakko Koivisto
(quoted diff) +0 -3
-Retrieve the chip serial number.
-Present the serial number to userspace as device attribute.
-Rename the tg_measure -struct now that is is used for multiple

commands.

...

drivers/iio/chemical/sgp40.c static int sgp40_calc_voc(struct sgp40_data *data, u16 resistance_raw, int *voc) · line 191

Quoted 6 lines
drivers/iio/chemical/sgp40.c +6 -0
+	ret = i2c_master_send(client, (char*)&get_sn, sizeof(get_sn.command));
+	if (ret != sizeof(get_sn.command)) {
+		dev_err(data->dev, "i2c_master_send ret: %d, expected %zu", ret, sizeof(get_sn.command));
+		return -EIO;
+	}
+	msleep(1);

Explain this sleep.

Quoted 6 lines
drivers/iio/chemical/sgp40.c +6 -0
+	ret = i2c_master_recv(client, (char*)&res, sizeof(res));
+	if (ret < 0)
+		return ret;
+	if (ret != sizeof(res)) {
+		dev_err(data->dev, "i2c_master_recv ret: %d, expected: %zu", ret, sizeof(res));
+		return -EIO;

drivers/iio/chemical/sgp40.c static int sgp40_calc_voc(struct sgp40_data *data, u16 resistance_raw, int *voc) · line 207

Quoted 10 lines
drivers/iio/chemical/sgp40.c +10 -0
+	}
+
+	if (crc8(sgp40_crc8_table, (u8*)&res.A, 2, SGP40_CRC8_INIT) != res.A_crc ||
+	    crc8(sgp40_crc8_table, (u8*)&res.B, 2, SGP40_CRC8_INIT) != res.B_crc ||
+	    crc8(sgp40_crc8_table, (u8*)&res.C, 2, SGP40_CRC8_INIT) != res.C_crc)
+	{
+		dev_warn(data->dev, "CRC error in get_serial_number");
+	}
+
+	data->serial_number = 0LL | ((u64)be16_to_cpu(res.A) << 32) | ((u64)be16_to_cpu(res.B) << 16) | (u64)be16_to_cpu(res.C);

0LL ?!

drivers/iio/chemical/sgp40.c static int sgp40_calc_voc(struct sgp40_data *data, u16 resistance_raw, int *voc) · line 208

Quoted 1 line
drivers/iio/chemical/sgp40.c +1 -0
+	dev_dbg(data->dev, "serial number: %llu", data->serial_number);

Why?!

...

drivers/iio/chemical/sgp40.c static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance · line 220

Quoted 2 lines
drivers/iio/chemical/sgp40.c +1 -1
-	struct sgp40_tg_measure tg = {.command = {0x26, 0x0F}};
+	struct sgp40_command tg = {.command = {0x26, 0x0F}};

While at it, add more spaces.

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+}
+

Unneeded blank line.

drivers/iio/chemical/sgp40.c static int sgp40_write_raw(struct iio_dev *indio_dev, · line 382

Quoted 12 lines
drivers/iio/chemical/sgp40.c +12 -0
+static IIO_DEVICE_ATTR_RO(serial_number, 0);
+
+static struct attribute *sgp40_attributes[] = {
+	&iio_dev_attr_serial_number.dev_attr.attr,
+	NULL
+};
+
+static struct attribute_group sgp40_attribute_group = {
+	.attrs = sgp40_attributes,
+};
+
+

Single blank line is enough and we have ATTRIBUTE_GROUPS().

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw

Re: [PATCH 1/3] iio: chemical: sgp40: Implement get_serial_number-command

Quoted 8 lines · Andy Shevchenko
Quoted 4 lines · Jaakko Koivisto
(quoted diff) +0 -3
-Retrieve the chip serial number.
-Present the serial number to userspace as device attribute.
-Rename the tg_measure -struct now that is is used for multiple

commands.

...

Quoted 8 lines

drivers/iio/chemical/sgp40.c static int sgp40_calc_voc(struct sgp40_data *data, u16 resistance_raw, int *voc) · line 191

Quoted 6 lines
drivers/iio/chemical/sgp40.c +6 -0
+	ret = i2c_master_send(client, (char*)&get_sn, sizeof(get_sn.command));
+	if (ret != sizeof(get_sn.command)) {
+		dev_err(data->dev, "i2c_master_send ret: %d, expected %zu", ret, sizeof(get_sn.command));
+		return -EIO;
+	}
+	msleep(1);

Explain this sleep.

It is the maximum time it takes for the chip to execute the command. I'll add a comment describing it.

In testing this command works if the sleep is omitted, but I would leave it here anyway to be sure we don't try to read the result before the chip is ready.

Quoted 10 lines
Quoted 10 lines
drivers/iio/chemical/sgp40.c +10 -0
+	ret = i2c_master_recv(client, (char*)&res, sizeof(res));
+	if (ret < 0)
+		return ret;
+	if (ret != sizeof(res)) {
+		dev_err(data->dev, "i2c_master_recv ret: %d, expected: %zu", ret, sizeof(res));
+		return -EIO;
+	}
+
+	if (crc8(sgp40_crc8_table, (u8*)&res.A, 2, SGP40_CRC8_INIT) != res.A_crc ||
+	    crc8(sgp40_crc8_table, (u8*)&res.B, 2, SGP40_CRC8_INIT) != res.B_crc ||
Quoted 9 lines

drivers/iio/chemical/sgp40.c static int sgp40_calc_voc(struct sgp40_data *data, u16 resistance_raw, int *voc) · line 207

Quoted 6 lines
drivers/iio/chemical/sgp40.c +6 -0
+	    crc8(sgp40_crc8_table, (u8*)&res.C, 2, SGP40_CRC8_INIT) != res.C_crc)
+	{
+		dev_warn(data->dev, "CRC error in get_serial_number");
+	}
+
+	data->serial_number = 0LL | ((u64)be16_to_cpu(res.A) << 32) | ((u64)be16_to_cpu(res.B) << 16) | (u64)be16_to_cpu(res.C);

0LL ?!

I have no excuse for this. Both unnecessary and for u64 should have been LLU. I will remove it.

Quoted 4 lines

drivers/iio/chemical/sgp40.c static int sgp40_calc_voc(struct sgp40_data *data, u16 resistance_raw, int *voc) · line 208

Quoted 1 line
drivers/iio/chemical/sgp40.c +1 -0
+	dev_dbg(data->dev, "serial number: %llu", data->serial_number);

Why?!

You are right, not really debug information. Will remove.

Quoted 8 lines

...

drivers/iio/chemical/sgp40.c static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance · line 220

Quoted 2 lines
drivers/iio/chemical/sgp40.c +1 -1
-	struct sgp40_tg_measure tg = {.command = {0x26, 0x0F}};
+	struct sgp40_command tg = {.command = {0x26, 0x0F}};

While at it, add more spaces.

Thanks, I'll fix this and other formatting mistakes.

Quoted 7 lines

...

Review

Quoted 2 lines
(quoted diff) +2 -0
+}
+

Unneeded blank line.

Quoted 15 lines

drivers/iio/chemical/sgp40.c static int sgp40_write_raw(struct iio_dev *indio_dev, · line 382

Quoted 12 lines
drivers/iio/chemical/sgp40.c +12 -0
+static IIO_DEVICE_ATTR_RO(serial_number, 0);
+
+static struct attribute *sgp40_attributes[] = {
+	&iio_dev_attr_serial_number.dev_attr.attr,
+	NULL
+};
+
+static struct attribute_group sgp40_attribute_group = {
+	.attrs = sgp40_attributes,
+};
+
+

Single blank line is enough and we have ATTRIBUTE_GROUPS().

Is using ATTRIBUTE_GROUPS() valid here? ATTRIBUTE_GROUPS(sgp40) will create both attribute_group sgp40_group and
attribute_group *sgp40_groups[], and we only need the first one for iio_info. The extra *sgp40_groups[] will create unused variable -warning.

#6 lore raw

PATCH 2/3 iio: chemical: sgp40: Implement execute_self_test-command

In reply to Jaakko Koivisto

-Run the chip self test routine testing heater and MOX -material. -Log error if the test fails.

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.

---
 drivers/iio/chemical/sgp40.c | 53 ++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
drivers/iio/chemical/sgp40.c +53 -0
index c1e2a992ec2a..28d5e737d1dc 100644
@@ -84,6 +84,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,
@@ -210,6 +223,42 @@ static int sgp40_get_serial_number(struct sgp40_data *data)
 	return 0;
 }
 
+static int sgp40_execute_self_test(struct sgp40_data *data)
+{
+	int ret;
+	struct i2c_client *client = data->client;
+	struct sgp40_command run_test = {.command = {0x28, 0x0E}};
+	struct sgp40_self_test_result res;
+
+	ret = i2c_master_send(client, (char*)&run_test, sizeof(run_test.command));
+	if (ret != sizeof(run_test.command)) {
+		dev_err(data->dev, "i2c_master_send ret: %d, expected %zu", ret, sizeof(run_test.command));
+		return -EIO;
+	}
+	msleep(320);
+
+	ret = i2c_master_recv(client, (char*)&res, sizeof(res));
+	if (ret < 0)
+		return ret;
+	if (ret != sizeof(res)) {
+		dev_err(data->dev, "i2c_master_recv ret: %d, expected: %zu", ret, sizeof(res));
+		return -EIO;
+	}
+
+	if (crc8(sgp40_crc8_table, (u8*)&res, 2, SGP40_CRC8_INIT) != res.crc) {
+		dev_warn(data->dev, "CRC error in execute_self_test");
+	}
+
+	dev_dbg(data->dev, "self test result: 0x%x", res.data);
+	switch (res.data) {
+		case SGP40_SELF_TEST_PASS:
+			return 0;
+		case SGP40_SELF_TEST_FAIL:
+		default:
+			return res.data;
+	}
+}
+
 static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance_raw)
 {
 	int ret;
@@ -421,6 +470,10 @@ static int sgp40_probe(struct i2c_client *client)
 	if (ret)
 		dev_warn(dev, "failed to retrieve device serial number\n");
 
+	ret = sgp40_execute_self_test(data);
+	if (ret)
+		dev_warn(dev, "device self test failed: ret 0x%x", ret);
+
 	ret = devm_iio_device_register(dev, indio_dev);
 	if (ret)
 		dev_err(dev, "failed to register iio device\n");
Signature · 1 line
2.55.0
maintainer
#7 lore raw

Re: [PATCH 2/3] iio: chemical: sgp40: Implement execute_self_test-command

Quoted 8 lines · On Fri, 18 Sep 2026 16:40:18 +0300 Jaakko Koivisto <[email protected]> wrote:
(quoted diff) +0 -2
-Run the chip self test routine testing heater and MOX -material.
-Log error if the test fails.

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.

Hi Jaakko,

A few things inline,

Thanks,

Jonathan

Quoted 33 lines
---
 drivers/iio/chemical/sgp40.c | 53 ++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
drivers/iio/chemical/sgp40.c +14 -0
index c1e2a992ec2a..28d5e737d1dc 100644
@@ -84,6 +84,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,
@@ -210,6 +223,42 @@ static int sgp40_get_serial_number(struct sgp40_data *data)
 	return 0;
 }
 
+static int sgp40_execute_self_test(struct sgp40_data *data)

drivers/iio/chemical/sgp40.c static int sgp40_get_serial_number(struct sgp40_data *data) · line 236

Quoted 10 lines
drivers/iio/chemical/sgp40.c +10 -0
+{
+	int ret;
+	struct i2c_client *client = data->client;
+	struct sgp40_command run_test = {.command = {0x28, 0x0E}};
+	struct sgp40_self_test_result res;
+
+	ret = i2c_master_send(client, (char*)&run_test, sizeof(run_test.command));
+	if (ret != sizeof(run_test.command)) {
+		dev_err(data->dev, "i2c_master_send ret: %d, expected %zu", ret, sizeof(run_test.command));
+		return -EIO;

Only called from problem so prefer use of

return dev_err_probe().

Also don't eat the error if it returns on (rather than a short access).

drivers/iio/chemical/sgp40.c static int sgp40_get_serial_number(struct sgp40_data *data) · line 238

Quoted 2 lines
drivers/iio/chemical/sgp40.c +2 -0
+	}
+	msleep(320);

Spec reference needed for any sleep times.

drivers/iio/chemical/sgp40.c static int sgp40_get_serial_number(struct sgp40_data *data) · line 245

Quoted 7 lines
drivers/iio/chemical/sgp40.c +7 -0
+
+	ret = i2c_master_recv(client, (char*)&res, sizeof(res));
+	if (ret < 0)
+		return ret;
+	if (ret != sizeof(res)) {
+		dev_err(data->dev, "i2c_master_recv ret: %d, expected: %zu", ret, sizeof(res));
+		return -EIO;

return dev_err_probe() and don't eat the error return if there is one.

drivers/iio/chemical/sgp40.c static int sgp40_get_serial_number(struct sgp40_data *data) · line 249

Quoted 4 lines
drivers/iio/chemical/sgp40.c +4 -0
+	}
+
+	if (crc8(sgp40_crc8_table, (u8*)&res, 2, SGP40_CRC8_INIT) != res.crc) {
+		dev_warn(data->dev, "CRC error in execute_self_test");

CRC fail indicates broken comms. I'd fail hard on this one.

Quoted 16 lines
drivers/iio/chemical/sgp40.c +12 -0
+	}
+
+	dev_dbg(data->dev, "self test result: 0x%x", res.data);
+	switch (res.data) {
+		case SGP40_SELF_TEST_PASS:
+			return 0;
+		case SGP40_SELF_TEST_FAIL:
+		default:
+			return res.data;
+	}
+}
+
 static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance_raw)
 {
 	int ret;

drivers/iio/chemical/sgp40.c static int sgp40_probe(struct i2c_client *client) · line 475

Quoted 6 lines
drivers/iio/chemical/sgp40.c +3 -0
@@ -421,6 +470,10 @@ static int sgp40_probe(struct i2c_client *client)
 	if (ret)
 		dev_warn(dev, "failed to retrieve device serial number\n");
 
+	ret = sgp40_execute_self_test(data);
+	if (ret)
+		dev_warn(dev, "device self test failed: ret 0x%x", ret);

There should be a distinction between the test failed to run and failed to pass. For failing to run (e.g. comms error) then fail the probe. Failing to pass the test is what you might choose to carry on from.

Quoted 4 lines
drivers/iio/chemical/sgp40.c +1 -0
+
 	ret = devm_iio_device_register(dev, indio_dev);
 	if (ret)
 		dev_err(dev, "failed to register iio device\n");
#8 lore raw

Re: [PATCH 2/3] iio: chemical: sgp40: Implement execute_self_test-command

Quoted 64 lines · Jonathan Cameron
Quoted 8 lines · On Fri, 18 Sep 2026 16:40:18 +0300 Jaakko Koivisto <[email protected]> wrote:
(quoted diff) +0 -2
-Run the chip self test routine testing heater and MOX -material.
-Log error if the test fails.

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.

Hi Jaakko,

A few things inline,

Thanks,

Jonathan

Quoted 33 lines
---
 drivers/iio/chemical/sgp40.c | 53 ++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)
drivers/iio/chemical/sgp40.c +14 -0
index c1e2a992ec2a..28d5e737d1dc 100644
@@ -84,6 +84,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,
@@ -210,6 +223,42 @@ static int sgp40_get_serial_number(struct sgp40_data *data)
 	return 0;
 }
 
+static int sgp40_execute_self_test(struct sgp40_data *data)

drivers/iio/chemical/sgp40.c static int sgp40_get_serial_number(struct sgp40_data *data) · line 236

Quoted 10 lines
drivers/iio/chemical/sgp40.c +10 -0
+{
+	int ret;
+	struct i2c_client *client = data->client;
+	struct sgp40_command run_test = {.command = {0x28, 0x0E}};
+	struct sgp40_self_test_result res;
+
+	ret = i2c_master_send(client, (char*)&run_test, sizeof(run_test.command));
+	if (ret != sizeof(run_test.command)) {
+		dev_err(data->dev, "i2c_master_send ret: %d, expected %zu", ret, sizeof(run_test.command));
+		return -EIO;

Only called from problem so prefer use of

Quoted 4 lines

return dev_err_probe().

Also don't eat the error if it returns on (rather than a short access).

I will change it, thanks.

Quoted 6 lines

drivers/iio/chemical/sgp40.c static int sgp40_get_serial_number(struct sgp40_data *data) · line 238

Quoted 3 lines
drivers/iio/chemical/sgp40.c +2 -0
+	}
+	msleep(320);

Spec reference needed for any sleep times.

It's the maximum time the chip takes to execute the self-test. All command times are found in datasheet table 8. I'll add a comment here.

Quoted 9 lines

drivers/iio/chemical/sgp40.c static int sgp40_get_serial_number(struct sgp40_data *data) · line 245

Quoted 7 lines
drivers/iio/chemical/sgp40.c +7 -0
+
+	ret = i2c_master_recv(client, (char*)&res, sizeof(res));
+	if (ret < 0)
+		return ret;
+	if (ret != sizeof(res)) {
+		dev_err(data->dev, "i2c_master_recv ret: %d, expected: %zu", ret, sizeof(res));
+		return -EIO;

return dev_err_probe() and don't eat the error return if there is one.

Will change.

Quoted 7 lines

drivers/iio/chemical/sgp40.c static int sgp40_get_serial_number(struct sgp40_data *data) · line 249

Quoted 4 lines
drivers/iio/chemical/sgp40.c +4 -0
+	}
+
+	if (crc8(sgp40_crc8_table, (u8*)&res, 2, SGP40_CRC8_INIT) != res.crc) {
+		dev_warn(data->dev, "CRC error in execute_self_test");

CRC fail indicates broken comms. I'd fail hard on this one.

Ditto.

Quoted 19 lines
Quoted 19 lines
drivers/iio/chemical/sgp40.c +12 -0
+	}
+
+	dev_dbg(data->dev, "self test result: 0x%x", res.data);
+	switch (res.data) {
+		case SGP40_SELF_TEST_PASS:
+			return 0;
+		case SGP40_SELF_TEST_FAIL:
+		default:
+			return res.data;
+	}
+}
+
 static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance_raw)
 {
 	int ret;
@@ -421,6 +470,10 @@ static int sgp40_probe(struct i2c_client *client)
 	if (ret)
 		dev_warn(dev, "failed to retrieve device serial number\n");
 
Quoted 7 lines

drivers/iio/chemical/sgp40.c static int sgp40_probe(struct i2c_client *client) · line 475

Quoted 3 lines
drivers/iio/chemical/sgp40.c +3 -0
+	ret = sgp40_execute_self_test(data);
+	if (ret)
+		dev_warn(dev, "device self test failed: ret 0x%x", ret);

There should be a distinction between the test failed to run and failed to pass. For failing to run (e.g. comms error) then fail the probe. Failing to pass the test is what you might choose to carry on from.

I'll change it to include failing to run the test.

Quoted 5 lines
Quoted 5 lines
drivers/iio/chemical/sgp40.c +1 -0
+
 	ret = devm_iio_device_register(dev, indio_dev);
 	if (ret)
 		dev_err(dev, "failed to register iio device\n");
#9 lore raw

PATCH 3/3 iio: chemical: sgp40: Implement turn_heater_off-command

In reply to Jaakko Koivisto

-Turn the heating element off and enter idle mode. -Present the functionality as device attribute,
'echo 1 > turn_heater_off'.

Saves approx. 2.5 mA compared to regular operation. The heating element is automatically turned back on when measurement is performed.

---
 drivers/iio/chemical/sgp40.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
drivers/iio/chemical/sgp40.c +28 -0
index 28d5e737d1dc..a4fc5c778303 100644
@@ -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
 };
 
Signature · 1 line
2.55.0
maintainer
#10 lore raw

Re: [PATCH 3/3] iio: chemical: sgp40: Implement turn_heater_off-command

Hi Jaakko,

Jaakko Koivisto <[email protected]> schrieb am Fr, 18. Sep 16:40:

Review

Quoted 3 lines
(quoted diff) +0 -2
-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

Quoted 68 lines

Saves approx. 2.5 mA compared to regular operation. The heating element is automatically turned back on when measurement is performed.

---
 drivers/iio/chemical/sgp40.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
drivers/iio/chemical/sgp40.c +28 -0
index 28d5e737d1dc..a4fc5c778303 100644
@@ -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
 };
 
Signature · 1 line
2.55.0
maintainer
#11 lore raw

Re: [PATCH 3/3] iio: chemical: sgp40: Implement turn_heater_off-command

Quoted 9 lines · On Sat, 19 Sep 2026 01:36:45 +0200 Andreas Klinger <[email protected]> wrote:

Hi Jaakko,

Jaakko Koivisto <[email protected]> schrieb am Fr, 18. Sep 16:40:

Review

Quoted 3 lines
(quoted diff) +0 -2
-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)?

May not apply in this case but normally the warm up time of these sorts of heaters are in the seconds. No one wants that latency when they want a measurement. As such normal runtime pm doesn't work - it needs to be a specific userspace opt in.

We have defined ABI for this though and this isn't it See Documentation/ABI/testing/sysfs-bus-iio (and more in -humidity)

Jonathan

Quoted 72 lines

Andreas

Quoted 68 lines

Saves approx. 2.5 mA compared to regular operation. The heating element is automatically turned back on when measurement is performed.

---
 drivers/iio/chemical/sgp40.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
drivers/iio/chemical/sgp40.c +28 -0
index 28d5e737d1dc..a4fc5c778303 100644
@@ -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
 };
 
Signature · 1 line
2.55.0
#12 lore raw

Re: [PATCH 3/3] iio: chemical: sgp40: Implement turn_heater_off-command

Quoted 13 lines · Jonathan Cameron
Quoted 9 lines · On Sat, 19 Sep 2026 01:36:45 +0200 Andreas Klinger <[email protected]> wrote:

Hi Jaakko,

Jaakko Koivisto <[email protected]> schrieb am Fr, 18. Sep 16:40:

Review

Quoted 3 lines
(quoted diff) +0 -2
-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)?

Quoted 4 lines

May not apply in this case but normally the warm up time of these sorts of heaters are in the seconds. No one wants that latency when they want a measurement. As such normal runtime pm doesn't work - it needs to be a specific userspace opt in.

For SGP40 the heater must be on for 60 seconds for reliable data, and up to 60 minutes to reach all datasheet specs. These are very long times, so I would not want to turn the heater off automatically.

Quoted 2 lines

We have defined ABI for this though and this isn't it See Documentation/ABI/testing/sysfs-bus-iio (and more in -humidity)

Thanks, missed that there was already ABI for heaters. I will change to use this instead.

While looking at this I noticed this driver is using mutex_lock() and mutex_unlock(). Recent commits, e.g. eb60a24b35bfb9e85a272e561379833e49a12a79, say using the newer guard() is the preferred way these days. Should this be updated as well?

Quoted 75 lines

Jonathan

Quoted 72 lines

Andreas

Quoted 68 lines

Saves approx. 2.5 mA compared to regular operation. The heating element is automatically turned back on when measurement is performed.

---
 drivers/iio/chemical/sgp40.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
drivers/iio/chemical/sgp40.c +28 -0
index 28d5e737d1dc..a4fc5c778303 100644
@@ -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
 };
 
Signature · 1 line
2.55.0
maintainer
#13 lore raw

Re: [PATCH 3/3] iio: chemical: sgp40: Implement turn_heater_off-command

Quoted 26 lines · On Thu, 24 Sep 2026 11:04:21 +0300 Jaakko Koivisto <[email protected]> wrote:
Quoted 13 lines · Jonathan Cameron
Quoted 9 lines · On Sat, 19 Sep 2026 01:36:45 +0200 Andreas Klinger <[email protected]> wrote:

Hi Jaakko,

Jaakko Koivisto <[email protected]> schrieb am Fr, 18. Sep 16:40:

Review

Quoted 3 lines
(quoted diff) +0 -2
-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)?

Quoted 4 lines

May not apply in this case but normally the warm up time of these sorts of heaters are in the seconds. No one wants that latency when they want a measurement. As such normal runtime pm doesn't work - it needs to be a specific userspace opt in.

For SGP40 the heater must be on for 60 seconds for reliable data, and up to 60 minutes to reach all datasheet specs. These are very long times, so I would not want to turn the heater off automatically.

Quoted 2 lines

We have defined ABI for this though and this isn't it See Documentation/ABI/testing/sysfs-bus-iio (and more in -humidity)

Quoted 8 lines

Thanks, missed that there was already ABI for heaters. I will change to use this instead.

While looking at this I noticed this driver is using mutex_lock() and mutex_unlock(). Recent commits, e.g. eb60a24b35bfb9e85a272e561379833e49a12a79, say using the newer guard() is the preferred way these days. Should this be updated as well?

There is no universal right answer to this. If guard() and related give cleaner, easier to understand code then it may well be useful to update to that whilst you are touching the driver.

Sometimes the code is such that they provide very little advantage and in those cases we should just leave things as they are!

Jonathan

Quoted 77 lines
Quoted 75 lines

Jonathan

Quoted 72 lines

Andreas

Quoted 68 lines

Saves approx. 2.5 mA compared to regular operation. The heating element is automatically turned back on when measurement is performed.

---
 drivers/iio/chemical/sgp40.c | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)
drivers/iio/chemical/sgp40.c +28 -0
index 28d5e737d1dc..a4fc5c778303 100644
@@ -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
 };
 
Signature · 1 line
2.55.0
maintainer
#14 lore raw
In reply to Jaakko Koivisto

On Fri, 18 Sep 2026 16:40:16 +0300
Jaakko Koivisto <[email protected]> wrote:
I'm not sure what 'secondary' means in the title. I'd just say what you are adding
Add serial number readback, self-test and heater control

Slightly long line but more useful than a vague title.

Quoted 14 lines

Add functions for getting Sensirion SGP40 VOC sensor serial number, running self-test, and turning the heating element off.

Tested with adafruit SGP40 and waveshare SGP40 breakout boards using Raspberry Pi 3 and Raspberry Pi 1.

Jaakko Koivisto (3):
iio: chemical: sgp40: Implement get_serial_number-command
iio: chemical: sgp40: Implement execute_self_test-command
iio: chemical: sgp40: Implement turn_heater_off-command

 drivers/iio/chemical/sgp40.c | 159 ++++++++++++++++++++++++++++++++++-
 1 file changed, 157 insertions(+), 2 deletions(-)
#15 lore raw
Quoted 7 lines · Jonathan Cameron

On Fri, 18 Sep 2026 16:40:16 +0300
Jaakko Koivisto <[email protected]> wrote:
I'm not sure what 'secondary' means in the title. I'd just say what you are adding
Add serial number readback, self-test and heater control

Slightly long line but more useful than a vague title.

The idea was that measuring is the primary function of the sensor, and all the other functions are secondary. Not as informative as I had hoped. Thanks for the suggestion, will change it for v2.

Quoted 15 lines
Quoted 15 lines

Add functions for getting Sensirion SGP40 VOC sensor serial number, running self-test, and turning the heating element off.

Tested with adafruit SGP40 and waveshare SGP40 breakout boards using Raspberry Pi 3 and Raspberry Pi 1.

Jaakko Koivisto (3):
iio: chemical: sgp40: Implement get_serial_number-command
iio: chemical: sgp40: Implement execute_self_test-command
iio: chemical: sgp40: Implement turn_heater_off-command

 drivers/iio/chemical/sgp40.c | 159 ++++++++++++++++++++++++++++++++++-
 1 file changed, 157 insertions(+), 2 deletions(-)