mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/3] iio: chemical: sgp40: Add secondary chip functionality
@ 2026-09-18 13:40 Jaakko Koivisto
  2026-09-18 13:40 ` [PATCH 1/3] iio: chemical: sgp40: Implement get_serial_number-command Jaakko Koivisto
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Jaakko Koivisto @ 2026-09-18 13:40 UTC (permalink / raw)
  To: Andreas Klinger, Jonathan Cameron, David Lechner, Nuno Sá,
	Andy Shevchenko
  Cc: linux-iio, linux-kernel, Jaakko Koivisto

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(-)

-- 
2.55.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/3] iio: chemical: sgp40: Implement get_serial_number-command
  2026-09-18 13:40 [PATCH 0/3] iio: chemical: sgp40: Add secondary chip functionality Jaakko Koivisto
@ 2026-09-18 13:40 ` Jaakko Koivisto
  2026-09-18 14:26   ` Maxwell Doose
  2026-09-18 13:40 ` [PATCH 2/3] iio: chemical: sgp40: Implement execute_self_test-command Jaakko Koivisto
  2026-09-18 13:40 ` [PATCH 3/3] iio: chemical: sgp40: Implement turn_heater_off-command Jaakko Koivisto
  2 siblings, 1 reply; 5+ messages in thread
From: Jaakko Koivisto @ 2026-09-18 13:40 UTC (permalink / raw)
  To: Andreas Klinger, Jonathan Cameron, David Lechner, Nuno Sá,
	Andy Shevchenko
  Cc: linux-iio, linux-kernel, Jaakko Koivisto

-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.

Signed-off-by: Jaakko Koivisto <jmatko@utu.fi>
---
 drivers/iio/chemical/sgp40.c | 78 +++++++++++++++++++++++++++++++++++-
 1 file changed, 76 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/chemical/sgp40.c b/drivers/iio/chemical/sgp40.c
index b2b5e32a9eb2..c1e2a992ec2a 100644
--- a/drivers/iio/chemical/sgp40.c
+++ b/drivers/iio/chemical/sgp40.c
@@ -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");
-- 
2.55.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/3] iio: chemical: sgp40: Implement execute_self_test-command
  2026-09-18 13:40 [PATCH 0/3] iio: chemical: sgp40: Add secondary chip functionality Jaakko Koivisto
  2026-09-18 13:40 ` [PATCH 1/3] iio: chemical: sgp40: Implement get_serial_number-command Jaakko Koivisto
@ 2026-09-18 13:40 ` Jaakko Koivisto
  2026-09-18 13:40 ` [PATCH 3/3] iio: chemical: sgp40: Implement turn_heater_off-command Jaakko Koivisto
  2 siblings, 0 replies; 5+ messages in thread
From: Jaakko Koivisto @ 2026-09-18 13:40 UTC (permalink / raw)
  To: Andreas Klinger, Jonathan Cameron, David Lechner, Nuno Sá,
	Andy Shevchenko
  Cc: linux-iio, linux-kernel, 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.

Signed-off-by: Jaakko Koivisto <jmatko@utu.fi>
---
 drivers/iio/chemical/sgp40.c | 53 ++++++++++++++++++++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/drivers/iio/chemical/sgp40.c b/drivers/iio/chemical/sgp40.c
index c1e2a992ec2a..28d5e737d1dc 100644
--- a/drivers/iio/chemical/sgp40.c
+++ b/drivers/iio/chemical/sgp40.c
@@ -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");
-- 
2.55.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 3/3] iio: chemical: sgp40: Implement turn_heater_off-command
  2026-09-18 13:40 [PATCH 0/3] iio: chemical: sgp40: Add secondary chip functionality Jaakko Koivisto
  2026-09-18 13:40 ` [PATCH 1/3] iio: chemical: sgp40: Implement get_serial_number-command Jaakko Koivisto
  2026-09-18 13:40 ` [PATCH 2/3] iio: chemical: sgp40: Implement execute_self_test-command Jaakko Koivisto
@ 2026-09-18 13:40 ` Jaakko Koivisto
  2 siblings, 0 replies; 5+ messages in thread
From: Jaakko Koivisto @ 2026-09-18 13:40 UTC (permalink / raw)
  To: Andreas Klinger, Jonathan Cameron, David Lechner, Nuno Sá,
	Andy Shevchenko
  Cc: linux-iio, linux-kernel, 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.

Signed-off-by: Jaakko Koivisto <jmatko@utu.fi>
---
 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


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/3] iio: chemical: sgp40: Implement get_serial_number-command
  2026-09-18 13:40 ` [PATCH 1/3] iio: chemical: sgp40: Implement get_serial_number-command Jaakko Koivisto
@ 2026-09-18 14:26   ` Maxwell Doose
  0 siblings, 0 replies; 5+ messages in thread
From: Maxwell Doose @ 2026-09-18 14:26 UTC (permalink / raw)
  To: Jaakko Koivisto, Andreas Klinger, Jonathan Cameron,
	David Lechner, Nuno Sá,
	Andy Shevchenko
  Cc: linux-iio, linux-kernel

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).

On Fri Sep 18, 2026 at 8:40 AM CDT
Jaakko Koivisto <jmatko@utu.fi> wrote:

> -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."

> Signed-off-by: Jaakko Koivisto <jmatko@utu.fi>
> ---
>  drivers/iio/chemical/sgp40.c | 78 +++++++++++++++++++++++++++++++++++-
>  1 file changed, 76 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/iio/chemical/sgp40.c b/drivers/iio/chemical/sgp40.c
> index b2b5e32a9eb2..c1e2a992ec2a 100644
> --- a/drivers/iio/chemical/sgp40.c
> +++ b/drivers/iio/chemical/sgp40.c
> @@ -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;
>  

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

> +/*
> + * 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.

>  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));

Missing '\n' here (sashiko).

> +		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");

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

> +	}
> +
> +	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");


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-09-18 14:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-18 13:40 [PATCH 0/3] iio: chemical: sgp40: Add secondary chip functionality Jaakko Koivisto
2026-09-18 13:40 ` [PATCH 1/3] iio: chemical: sgp40: Implement get_serial_number-command Jaakko Koivisto
2026-09-18 14:26   ` Maxwell Doose
2026-09-18 13:40 ` [PATCH 2/3] iio: chemical: sgp40: Implement execute_self_test-command Jaakko Koivisto
2026-09-18 13:40 ` [PATCH 3/3] iio: chemical: sgp40: Implement turn_heater_off-command Jaakko Koivisto

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®