Skip to contentSkip to conversation

iio: chemical: sgp30: Support Sensirion SGP30/SGPC3 sensors

PATCH In review Andreas Brauchli 4 participants 9 messages Dec 13, 2018 – Mar 10, 2019 IIO

lore
Share & tools
lore
#0 lore raw

PATCH v3 2/3 iio: chemical: sgp30: Support Sensirion SGP30/SGPC3 sensors

Support Sensirion SGP30 and SGPC3 multi-pixel I2C gas sensors

Supported Features:

* Indoor Air Quality (IAQ) concentrations for
- tVOC (in_concentration_voc_input)
- CO2eq (in_concentration_co2_input) - SGP30 only

IAQ concentrations are periodically read out by a background thread to allow the sensor to maintain its internal baseline.

* Gas concentration signals
- Ethanol (in_concentration_ethanol_raw)
- H2 (in_concentration_h2_raw) - SGP30 only

https://www.sensirion.com/file/datasheet_sgp30
https://www.sensirion.com/file/datasheet_sgpc3

---
 drivers/iio/chemical/Kconfig  |  13 +
 drivers/iio/chemical/Makefile |   1 +
 drivers/iio/chemical/sgp30.c  | 603 ++++++++++++++++++++++++++++++++++
 3 files changed, 617 insertions(+)
 create mode 100644 drivers/iio/chemical/sgp30.c
drivers/iio/chemical/Kconfig +13 -0
index b8e005be4f87..b0c912be94a9 100644
@@ -61,6 +61,19 @@ config IAQCORE
 	  iAQ-Core Continuous/Pulsed VOC (Volatile Organic Compounds)
 	  sensors
 
+config SENSIRION_SGP30
+	tristate "Sensirion SGPxx gas sensors"
+	depends on I2C
+	select CRC8
+	help
+	  Say Y here to build I2C interface support for the following
+	  Sensirion SGP gas sensors:
+	    * SGP30 gas sensor
+	    * SGPC3 low power gas sensor
+
+	  To compile this driver as module, choose M here: the
+	  module will be called sgp30.
+
 config VZ89X
 	tristate "SGX Sensortech MiCS VZ89X VOC sensor"
 	depends on I2C
drivers/iio/chemical/Makefile +1 -0
index 2f4c4ba4d781..61e83944db1a 100644
@@ -9,4 +9,5 @@ obj-$(CONFIG_BME680_I2C) += bme680_i2c.o
 obj-$(CONFIG_BME680_SPI) += bme680_spi.o
 obj-$(CONFIG_CCS811)		+= ccs811.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
+obj-$(CONFIG_SENSIRION_SGP30)	+= sgp30.o
 obj-$(CONFIG_VZ89X)		+= vz89x.o
drivers/iio/chemical/sgp30.c new file +603 -0

603 lines not on this page · raw message

Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 11 lines · On Thu, 13 Dec 2018 15:43:23 +0100 Andreas Brauchli…

Support Sensirion SGP30 and SGPC3 multi-pixel I2C gas sensors

Supported Features:

* Indoor Air Quality (IAQ) concentrations for
- tVOC (in_concentration_voc_input)
- CO2eq (in_concentration_co2_input) - SGP30 only

IAQ concentrations are periodically read out by a background thread to allow the sensor to maintain its internal baseline.

Quoted 8 lines

* Gas concentration signals
- Ethanol (in_concentration_ethanol_raw)
- H2 (in_concentration_h2_raw) - SGP30 only

https://www.sensirion.com/file/datasheet_sgp30
https://www.sensirion.com/file/datasheet_sgpc3

Why do we provide endianness info given we don't provide the buffered interface? It's only used when we do.

Some over the top line wrapping as well. However as that's it I'll just clean up whilst applying. If I've missed something odd around the endian thing shout!

Jonathan

Quoted 192 lines
---
 drivers/iio/chemical/Kconfig  |  13 +
 drivers/iio/chemical/Makefile |   1 +
 drivers/iio/chemical/sgp30.c  | 603 ++++++++++++++++++++++++++++++++++
 3 files changed, 617 insertions(+)
 create mode 100644 drivers/iio/chemical/sgp30.c
drivers/iio/chemical/Kconfig +13 -0
index b8e005be4f87..b0c912be94a9 100644
@@ -61,6 +61,19 @@ config IAQCORE
 	  iAQ-Core Continuous/Pulsed VOC (Volatile Organic Compounds)
 	  sensors
 
+config SENSIRION_SGP30
+	tristate "Sensirion SGPxx gas sensors"
+	depends on I2C
+	select CRC8
+	help
+	  Say Y here to build I2C interface support for the following
+	  Sensirion SGP gas sensors:
+	    * SGP30 gas sensor
+	    * SGPC3 low power gas sensor
+
+	  To compile this driver as module, choose M here: the
+	  module will be called sgp30.
+
 config VZ89X
 	tristate "SGX Sensortech MiCS VZ89X VOC sensor"
 	depends on I2C
drivers/iio/chemical/Makefile +1 -0
index 2f4c4ba4d781..61e83944db1a 100644
@@ -9,4 +9,5 @@ obj-$(CONFIG_BME680_I2C) += bme680_i2c.o
 obj-$(CONFIG_BME680_SPI) += bme680_spi.o
 obj-$(CONFIG_CCS811)		+= ccs811.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
+obj-$(CONFIG_SENSIRION_SGP30)	+= sgp30.o
 obj-$(CONFIG_VZ89X)		+= vz89x.o
drivers/iio/chemical/sgp30.c new file +145 -0
new file mode 100644
index 000000000000..90eb4cbb18cf
@@ -0,0 +1,603 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * sgp30.c - Support for Sensirion SGP Gas Sensors
+ *
+ * Copyright (C) 2018 Andreas Brauchli <[email protected]>
+ *
+ * I2C slave address: 0x58
+ *
+ * Datasheets:
+ * https://www.sensirion.com/file/datasheet_sgp30
+ * https://www.sensirion.com/file/datasheet_sgpc3
+ *
+ * TODO:
+ * - baseline support
+ * - humidity compensation
+ * - power mode switching (SGPC3)
+ */
+
+#include <linux/crc8.h>
+#include <linux/delay.h>
+#include <linux/kthread.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/i2c.h>
+#include <linux/of_device.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+#define SGP_WORD_LEN				2
+#define SGP_CRC8_POLYNOMIAL			0x31
+#define SGP_CRC8_INIT				0xff
+#define SGP_CRC8_LEN				1
+#define SGP_CMD(cmd_word)			cpu_to_be16(cmd_word)
+#define SGP_CMD_DURATION_US			12000
+#define SGP_MEASUREMENT_DURATION_US		50000
+#define SGP_CMD_LEN				SGP_WORD_LEN
+#define SGP_CMD_MAX_BUF_SIZE			(SGP_CMD_LEN + 2 * SGP_WORD_LEN)
+#define SGP_MEASUREMENT_LEN			2
+#define SGP30_MEASURE_INTERVAL_HZ		1
+#define SGPC3_MEASURE_INTERVAL_HZ		2
+#define SGP_VERS_PRODUCT(data)	((((data)->feature_set) & 0xf000) >> 12)
+#define SGP_VERS_RESERVED(data)	((((data)->feature_set) & 0x0800) >> 11)
+#define SGP_VERS_GEN(data)	((((data)->feature_set) & 0x0600) >> 9)
+#define SGP_VERS_ENG_BIT(data)	((((data)->feature_set) & 0x0100) >> 8)
+#define SGP_VERS_MAJOR(data)	((((data)->feature_set) & 0x00e0) >> 5)
+#define SGP_VERS_MINOR(data)	(((data)->feature_set) & 0x001f)
+
+DECLARE_CRC8_TABLE(sgp_crc8_table);
+
+enum sgp_product_id {
+	SGP30 = 0,
+	SGPC3,
+};
+
+enum sgp30_channel_idx {
+	SGP30_IAQ_TVOC_IDX = 0,
+	SGP30_IAQ_CO2EQ_IDX,
+	SGP30_SIG_ETOH_IDX,
+	SGP30_SIG_H2_IDX,
+};
+
+enum sgpc3_channel_idx {
+	SGPC3_IAQ_TVOC_IDX = 10,
+	SGPC3_SIG_ETOH_IDX,
+};
+
+enum sgp_cmd {
+	SGP_CMD_IAQ_INIT			= SGP_CMD(0x2003),
+	SGP_CMD_IAQ_MEASURE			= SGP_CMD(0x2008),
+	SGP_CMD_GET_FEATURE_SET			= SGP_CMD(0x202f),
+	SGP_CMD_GET_SERIAL_ID			= SGP_CMD(0x3682),
+
+	SGP30_CMD_MEASURE_SIGNAL		= SGP_CMD(0x2050),
+
+	SGPC3_CMD_MEASURE_RAW			= SGP_CMD(0x2046),
+};
+
+struct sgp_version {
+	u8 major;
+	u8 minor;
+};
+
+struct sgp_crc_word {
+	__be16 value;
+	u8 crc8;
+} __attribute__((__packed__));
+
+union sgp_reading {
+	u8 start;
+	struct sgp_crc_word raw_words[4];
+};
+
+enum _iaq_buffer_state {
+	IAQ_BUFFER_EMPTY = 0,
+	IAQ_BUFFER_DEFAULT_VALS,
+	IAQ_BUFFER_VALID,
+};
+
+struct sgp_data {
+	struct i2c_client *client;
+	struct task_struct *iaq_thread;
+	struct mutex data_lock;
+	unsigned long iaq_init_start_jiffies;
+	unsigned long iaq_defval_skip_jiffies;
+	u16 product_id;
+	u16 feature_set;
+	unsigned long measure_interval_jiffies;
+	enum sgp_cmd iaq_init_cmd;
+	enum sgp_cmd measure_iaq_cmd;
+	enum sgp_cmd measure_gas_signals_cmd;
+	union sgp_reading buffer;
+	union sgp_reading iaq_buffer;
+	enum _iaq_buffer_state iaq_buffer_state;
+};
+
+struct sgp_device {
+	const struct iio_chan_spec *channels;
+	int num_channels;
+};
+
+static const struct sgp_version supported_versions_sgp30[] = {
+	{
+		.major = 1,
+		.minor = 0,
+	},
+};
+
+static const struct sgp_version supported_versions_sgpc3[] = {
+	{
+		.major = 0,
+		.minor = 4,
+	},
+};
+
+static const struct iio_chan_spec sgp30_channels[] = {
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_VOC,
+		.modified = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+		.address = SGP30_IAQ_TVOC_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_CO2,

drivers/iio/chemical/sgp30.c line 155

Quoted 10 lines
drivers/iio/chemical/sgp30.c new file +10 -0
+		.modified = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+		.address = SGP30_IAQ_CO2EQ_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_ETHANOL,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),

Definitely looks like it'll fit in under 80 chars.

drivers/iio/chemical/sgp30.c line 166

Quoted 11 lines
drivers/iio/chemical/sgp30.c new file +11 -0
+		.address = SGP30_SIG_ETOH_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_H2,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),

Same here on length.

Quoted 11 lines
drivers/iio/chemical/sgp30.c new file +11 -0
+		.address = SGP30_SIG_H2_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+};
+
+static const struct iio_chan_spec sgpc3_channels[] = {
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_VOC,

drivers/iio/chemical/sgp30.c line 187

Quoted 10 lines
drivers/iio/chemical/sgp30.c new file +10 -0
+		.modified = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+		.address = SGPC3_IAQ_TVOC_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_ETHANOL,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),

And another on length.

Quoted 416 lines
drivers/iio/chemical/sgp30.c new file +416 -0
+		.address = SGPC3_SIG_ETOH_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+};
+
+static const struct sgp_device sgp_devices[] = {
+	[SGP30] = {
+		.channels = sgp30_channels,
+		.num_channels = ARRAY_SIZE(sgp30_channels),
+	},
+	[SGPC3] = {
+		.channels = sgpc3_channels,
+		.num_channels = ARRAY_SIZE(sgpc3_channels),
+	},
+};
+
+/**
+ * sgp_verify_buffer() - verify the checksums of the data buffer words
+ *
+ * @data:       SGP data
+ * @buf:        Raw data buffer
+ * @word_count: Num data words stored in the buffer, excluding CRC bytes
+ *
+ * Return:      0 on success, negative error otherwise.
+ */
+static int sgp_verify_buffer(const struct sgp_data *data,
+			     union sgp_reading *buf, size_t word_count)
+{
+	size_t size = word_count * (SGP_WORD_LEN + SGP_CRC8_LEN);
+	int i;
+	u8 crc;
+	u8 *data_buf = &buf->start;
+
+	for (i = 0; i < size; i += SGP_WORD_LEN + SGP_CRC8_LEN) {
+		crc = crc8(sgp_crc8_table, &data_buf[i], SGP_WORD_LEN,
+			   SGP_CRC8_INIT);
+		if (crc != data_buf[i + SGP_WORD_LEN]) {
+			dev_err(&data->client->dev, "CRC error\n");
+			return -EIO;
+		}
+	}
+
+	return 0;
+}
+
+/**
+ * sgp_read_cmd() - reads data from sensor after issuing a command
+ * The caller must hold data->data_lock for the duration of the call.
+ * @data:        SGP data
+ * @cmd:         SGP Command to issue
+ * @buf:         Raw data buffer to use
+ * @word_count:  Num words to read, excluding CRC bytes
+ *
+ * Return:       0 on success, negative error otherwise.
+ */
+static int sgp_read_cmd(struct sgp_data *data, enum sgp_cmd cmd,
+			union sgp_reading *buf, size_t word_count,
+			unsigned long duration_us)
+{
+	int ret;
+	struct i2c_client *client = data->client;
+	size_t size = word_count * (SGP_WORD_LEN + SGP_CRC8_LEN);
+	u8 *data_buf;
+
+	ret = i2c_master_send(client, (const char *)&cmd, SGP_CMD_LEN);
+	if (ret != SGP_CMD_LEN)
+		return -EIO;
+	usleep_range(duration_us, duration_us + 1000);
+
+	if (word_count == 0)
+		return 0;
+
+	data_buf = &buf->start;
+	ret = i2c_master_recv(client, data_buf, size);
+	if (ret < 0)
+		return ret;
+	if (ret != size)
+		return -EIO;
+
+	return sgp_verify_buffer(data, buf, word_count);
+}
+
+/**
+ * sgp_measure_iaq() - measure and retrieve IAQ values from sensor
+ * The caller must hold data->data_lock for the duration of the call.
+ * @data:       SGP data
+ *
+ * Return:      0 on success, -EBUSY on default values, negative error
+ *              otherwise.
+ */
+
+static int sgp_measure_iaq(struct sgp_data *data)
+{
+	int ret;
+	/* data contains default values */
+	bool default_vals = !time_after(jiffies, data->iaq_init_start_jiffies +
+						 data->iaq_defval_skip_jiffies);
+
+	ret = sgp_read_cmd(data, data->measure_iaq_cmd, &data->iaq_buffer,
+			   SGP_MEASUREMENT_LEN, SGP_MEASUREMENT_DURATION_US);
+	if (ret < 0)
+		return ret;
+
+	data->iaq_buffer_state = IAQ_BUFFER_DEFAULT_VALS;
+
+	if (default_vals)
+		return -EBUSY;
+
+	data->iaq_buffer_state = IAQ_BUFFER_VALID;
+
+	return 0;
+}
+
+static void sgp_iaq_thread_sleep_until(const struct sgp_data *data,
+				       unsigned long sleep_jiffies)
+{
+	const long IAQ_POLL = 50000;
+
+	while (!time_after(jiffies, sleep_jiffies)) {
+		usleep_range(IAQ_POLL, IAQ_POLL + 10000);
+		if (kthread_should_stop() || data->iaq_init_start_jiffies == 0)
+			return;
+	}
+}
+
+static int sgp_iaq_threadfn(void *p)
+{
+	struct sgp_data *data = (struct sgp_data *)p;
+	unsigned long next_update_jiffies;
+	int ret;
+
+	while (!kthread_should_stop()) {
+		mutex_lock(&data->data_lock);
+		if (data->iaq_init_start_jiffies == 0) {
+			ret = sgp_read_cmd(data, data->iaq_init_cmd, NULL, 0,
+					   SGP_CMD_DURATION_US);
+			if (ret < 0)
+				goto unlock_sleep_continue;
+			data->iaq_init_start_jiffies = jiffies;
+		}
+
+		ret = sgp_measure_iaq(data);
+		if (ret && ret != -EBUSY) {
+			dev_warn(&data->client->dev,
+				 "IAQ measurement error [%d]\n", ret);
+		}
+unlock_sleep_continue:
+		next_update_jiffies = jiffies + data->measure_interval_jiffies;
+		mutex_unlock(&data->data_lock);
+		sgp_iaq_thread_sleep_until(data, next_update_jiffies);
+	}
+
+	return 0;
+}
+
+static int sgp_read_raw(struct iio_dev *indio_dev,
+			struct iio_chan_spec const *chan, int *val,
+			int *val2, long mask)
+{
+	struct sgp_data *data = iio_priv(indio_dev);
+	struct sgp_crc_word *words;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
+		mutex_lock(&data->data_lock);
+		if (data->iaq_buffer_state != IAQ_BUFFER_VALID) {
+			mutex_unlock(&data->data_lock);
+			return -EBUSY;
+		}
+		words = data->iaq_buffer.raw_words;
+		switch (chan->address) {
+		case SGP30_IAQ_TVOC_IDX:
+		case SGPC3_IAQ_TVOC_IDX:
+			*val = 0;
+			*val2 = be16_to_cpu(words[1].value);
+			ret = IIO_VAL_INT_PLUS_NANO;
+			break;
+		case SGP30_IAQ_CO2EQ_IDX:
+			*val = 0;
+			*val2 = be16_to_cpu(words[0].value);
+			ret = IIO_VAL_INT_PLUS_MICRO;
+			break;
+		default:
+			ret = -EINVAL;
+			break;
+		}
+		mutex_unlock(&data->data_lock);
+		break;
+	case IIO_CHAN_INFO_RAW:
+		mutex_lock(&data->data_lock);
+		if (chan->address == SGPC3_SIG_ETOH_IDX) {
+			if (data->iaq_buffer_state == IAQ_BUFFER_EMPTY)
+				ret = -EBUSY;
+			else
+				ret = 0;
+			words = data->iaq_buffer.raw_words;
+		} else {
+			ret = sgp_read_cmd(data, data->measure_gas_signals_cmd,
+					   &data->buffer, SGP_MEASUREMENT_LEN,
+					   SGP_MEASUREMENT_DURATION_US);
+			words = data->buffer.raw_words;
+		}
+		if (ret) {
+			mutex_unlock(&data->data_lock);
+			return ret;
+		}
+
+		switch (chan->address) {
+		case SGP30_SIG_ETOH_IDX:
+			*val = be16_to_cpu(words[1].value);
+			ret = IIO_VAL_INT;
+			break;
+		case SGPC3_SIG_ETOH_IDX:
+		case SGP30_SIG_H2_IDX:
+			*val = be16_to_cpu(words[0].value);
+			ret = IIO_VAL_INT;
+			break;
+		default:
+			ret = -EINVAL;
+			break;
+		}
+		mutex_unlock(&data->data_lock);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return ret;
+}
+
+static int sgp_check_compat(struct sgp_data *data,
+			    unsigned int product_id)
+{
+	const struct sgp_version *supported_versions;
+	u16 ix, num_fs;
+	u16 product, generation, major, minor;
+
+	/* driver does not match product */
+	generation = SGP_VERS_GEN(data);
+	if (generation != 0) {
+		dev_err(&data->client->dev,
+			"incompatible product generation %d != 0", generation);
+		return -ENODEV;
+	}
+
+	product = SGP_VERS_PRODUCT(data);
+	if (product != product_id) {
+		dev_err(&data->client->dev,
+			"sensor reports a different product: 0x%04hx\n",
+			product);
+		return -ENODEV;
+	}
+
+	if (SGP_VERS_RESERVED(data))
+		dev_warn(&data->client->dev, "reserved bit is set\n");
+
+	/* engineering samples are not supported: no interface guarantees */
+	if (SGP_VERS_ENG_BIT(data))
+		return -ENODEV;
+
+	switch (product) {
+	case SGP30:
+		supported_versions = supported_versions_sgp30;
+		num_fs = ARRAY_SIZE(supported_versions_sgp30);
+		break;
+	case SGPC3:
+		supported_versions = supported_versions_sgpc3;
+		num_fs = ARRAY_SIZE(supported_versions_sgpc3);
+		break;
+	default:
+		return -ENODEV;
+	}
+
+	major = SGP_VERS_MAJOR(data);
+	minor = SGP_VERS_MINOR(data);
+	for (ix = 0; ix < num_fs; ix++) {
+		if (major == supported_versions[ix].major &&
+		    minor >= supported_versions[ix].minor)
+			return 0;
+	}
+	dev_err(&data->client->dev, "unsupported sgp version: %d.%d\n",
+		major, minor);
+
+	return -ENODEV;
+}
+
+static void sgp_init(struct sgp_data *data)
+{
+	data->iaq_init_cmd = SGP_CMD_IAQ_INIT;
+	data->iaq_init_start_jiffies = 0;
+	data->iaq_buffer_state = IAQ_BUFFER_EMPTY;
+	switch (SGP_VERS_PRODUCT(data)) {
+	case SGP30:
+		data->measure_interval_jiffies = SGP30_MEASURE_INTERVAL_HZ * HZ;
+		data->measure_iaq_cmd = SGP_CMD_IAQ_MEASURE;
+		data->measure_gas_signals_cmd = SGP30_CMD_MEASURE_SIGNAL;
+		data->product_id = SGP30;
+		data->iaq_defval_skip_jiffies = 15 * HZ;
+		break;
+	case SGPC3:
+		data->measure_interval_jiffies = SGPC3_MEASURE_INTERVAL_HZ * HZ;
+		data->measure_iaq_cmd = SGPC3_CMD_MEASURE_RAW;
+		data->measure_gas_signals_cmd = SGPC3_CMD_MEASURE_RAW;
+		data->product_id = SGPC3;
+		data->iaq_defval_skip_jiffies =
+			43 * data->measure_interval_jiffies;
+		break;
+	};
+}
+
+static const struct iio_info sgp_info = {
+	.read_raw	= sgp_read_raw,
+};
+
+static const struct of_device_id sgp_dt_ids[] = {
+	{ .compatible = "sensirion,sgp30", .data = (void *)SGP30 },
+	{ .compatible = "sensirion,sgpc3", .data = (void *)SGPC3 },
+	{ }
+};
+
+static int sgp_probe(struct i2c_client *client,
+		     const struct i2c_device_id *id)
+{
+	struct iio_dev *indio_dev;
+	struct sgp_data *data;
+	const struct of_device_id *of_id;
+	unsigned long product_id;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	of_id = of_match_device(sgp_dt_ids, &client->dev);
+	if (of_id)
+		product_id = (unsigned long)of_id->data;
+	else
+		product_id = id->driver_data;
+
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	data->client = client;
+	crc8_populate_msb(sgp_crc8_table, SGP_CRC8_POLYNOMIAL);
+	mutex_init(&data->data_lock);
+
+	/* get feature set version and write it to client data */
+	ret = sgp_read_cmd(data, SGP_CMD_GET_FEATURE_SET, &data->buffer, 1,
+			   SGP_CMD_DURATION_US);
+	if (ret < 0)
+		return ret;
+
+	data->feature_set = be16_to_cpu(data->buffer.raw_words[0].value);
+
+	ret = sgp_check_compat(data, product_id);
+	if (ret)
+		return ret;
+
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->info = &sgp_info;
+	indio_dev->name = id->name;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = sgp_devices[product_id].channels;
+	indio_dev->num_channels = sgp_devices[product_id].num_channels;
+
+	sgp_init(data);
+
+	ret = devm_iio_device_register(&client->dev, indio_dev);
+	if (ret) {
+		dev_err(&client->dev, "failed to register iio device\n");
+		return ret;
+	}
+
+	data->iaq_thread = kthread_run(sgp_iaq_threadfn, data,
+				       "%s-iaq", data->client->name);
+
+	return 0;
+}
+
+static int sgp_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+	struct sgp_data *data = iio_priv(indio_dev);
+
+	if (data->iaq_thread)
+		kthread_stop(data->iaq_thread);
+
+	return 0;
+}
+
+static const struct i2c_device_id sgp_id[] = {
+	{ "sgp30", SGP30 },
+	{ "sgpc3", SGPC3 },
+	{ }
+};
+
+MODULE_DEVICE_TABLE(i2c, sgp_id);
+MODULE_DEVICE_TABLE(of, sgp_dt_ids);
+
+static struct i2c_driver sgp_driver = {
+	.driver = {
+		.name = "sgp30",
+		.of_match_table = of_match_ptr(sgp_dt_ids),
+	},
+	.probe = sgp_probe,
+	.remove = sgp_remove,
+	.id_table = sgp_id,
+};
+module_i2c_driver(sgp_driver);
+
+MODULE_AUTHOR("Andreas Brauchli <[email protected]>");
+MODULE_AUTHOR("Pascal Sachs <[email protected]>");
+MODULE_DESCRIPTION("Sensirion SGP gas sensors");
+MODULE_LICENSE("GPL v2");
#2 lore raw
Quoted 19 lines · Jonathan Cameron
Quoted 16 lines · On Thu, 13 Dec 2018 15:43:23 +0100 Andreas Brauchli…

Support Sensirion SGP30 and SGPC3 multi-pixel I2C gas sensors

Supported Features:

* Indoor Air Quality (IAQ) concentrations for
- tVOC (in_concentration_voc_input)
- CO2eq (in_concentration_co2_input) - SGP30 only

IAQ concentrations are periodically read out by a background
thread
to allow the sensor to maintain its internal baseline.

* Gas concentration signals
- Ethanol (in_concentration_ethanol_raw)
- H2 (in_concentration_h2_raw) - SGP30 only

Quoted 7 lines
Quoted 4 lines

https://www.sensirion.com/file/datasheet_sgp30
https://www.sensirion.com/file/datasheet_sgpc3

Why do we provide endianness info given we don't provide the buffered interface? It's only used when we do.

Makes sense but I wasn't aware of it. The initial submission came with buffering, so it probably wasn't spotted in v2.

Quoted 4 lines

Some over the top line wrapping as well. However as that's it I'll just clean up whilst applying. If I've missed something odd around the endian thing shout!

No that's it, thanks a lot.

Quoted 20 lines

Jonathan

Quoted 17 lines
---
 drivers/iio/chemical/Kconfig  |  13 +
 drivers/iio/chemical/Makefile |   1 +
 drivers/iio/chemical/sgp30.c  | 603
++++++++++++++++++++++++++++++++++
 3 files changed, 617 insertions(+)
 create mode 100644 drivers/iio/chemical/sgp30.c
drivers/iio/chemical/Kconfig +0 -0
index b8e005be4f87..b0c912be94a9 100644
@@ -61,6 +61,19 @@ config IAQCORE
 	  iAQ-Core Continuous/Pulsed VOC (Volatile Organic Compounds)
 	  sensors
 
Quoted 1 line

drivers/iio/chemical/Kconfig config IAQCORE · line 64

Quoted 1 line
drivers/iio/chemical/Kconfig +1 -0
+config SENSIRION_SGP30

I noticed that in Tomasz's SPS30 patch it's just SPS30, so for consistency's sake I'm fine with dropping the branding. Up to you.

Quoted 679 lines
Quoted 185 lines
drivers/iio/chemical/Kconfig +12 -0
+	tristate "Sensirion SGPxx gas sensors"
+	depends on I2C
+	select CRC8
+	help
+	  Say Y here to build I2C interface support for the following
+	  Sensirion SGP gas sensors:
+	    * SGP30 gas sensor
+	    * SGPC3 low power gas sensor
+
+	  To compile this driver as module, choose M here: the
+	  module will be called sgp30.
+
 config VZ89X
 	tristate "SGX Sensortech MiCS VZ89X VOC sensor"
 	depends on I2C
drivers/iio/chemical/Makefile +1 -0
index 2f4c4ba4d781..61e83944db1a 100644
@@ -9,4 +9,5 @@ obj-$(CONFIG_BME680_I2C) += bme680_i2c.o
 obj-$(CONFIG_BME680_SPI) += bme680_spi.o
 obj-$(CONFIG_CCS811)		+= ccs811.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
+obj-$(CONFIG_SENSIRION_SGP30)	+= sgp30.o
 obj-$(CONFIG_VZ89X)		+= vz89x.o
drivers/iio/chemical/sgp30.c new file +5 -0
new file mode 100644
index 000000000000..90eb4cbb18cf
@@ -0,0 +1,603 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * sgp30.c - Support for Sensirion SGP Gas Sensors
+ *
+ * Copyright (C) 2018 Andreas Brauchli <

[email protected]>
+ *
+ * I2C slave address: 0x58
+ *
+ * Datasheets:
+ * https://www.sensirion.com/file/datasheet_sgp30
+ * https://www.sensirion.com/file/datasheet_sgpc3
+ *
+ * TODO:
+ * - baseline support
+ * - humidity compensation
+ * - power mode switching (SGPC3)
+ */

drivers/iio/chemical/sgp30.c new file +20 -0
+
+#include <linux/crc8.h>
+#include <linux/delay.h>
+#include <linux/kthread.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/i2c.h>
+#include <linux/of_device.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+#define SGP_WORD_LEN				2
+#define SGP_CRC8_POLYNOMIAL			0x31
+#define SGP_CRC8_INIT				0xff
+#define SGP_CRC8_LEN				1
+#define SGP_CMD(cmd_word)			cpu_to_be16(cmd_word)
+#define SGP_CMD_DURATION_US			12000
+#define SGP_MEASUREMENT_DURATION_US		50000
+#define SGP_CMD_LEN				SGP_WORD_LEN
+#define SGP_CMD_MAX_BUF_SIZE			(SGP_CMD_LEN +

2 * SGP_WORD_LEN)

drivers/iio/chemical/sgp30.c new file +4 -0
+#define SGP_MEASUREMENT_LEN			2
+#define SGP30_MEASURE_INTERVAL_HZ		1
+#define SGPC3_MEASURE_INTERVAL_HZ		2
+#define SGP_VERS_PRODUCT(data)	((((data)->feature_set) &

0xf000) >> 12)
+#define SGP_VERS_RESERVED(data) ((((data)->feature_set) &
0x0800) >> 11)

drivers/iio/chemical/sgp30.c new file +2 -0
+#define SGP_VERS_GEN(data)	((((data)->feature_set) & 0x0600) >> 9)
+#define SGP_VERS_ENG_BIT(data)	((((data)->feature_set) &

0x0100) >> 8)
+#define SGP_VERS_MAJOR(data) ((((data)->feature_set) &
0x00e0) >> 5)
+#define SGP_VERS_MINOR(data) (((data)->feature_set) &
0x001f)

drivers/iio/chemical/sgp30.c new file +99 -0
+
+DECLARE_CRC8_TABLE(sgp_crc8_table);
+
+enum sgp_product_id {
+	SGP30 = 0,
+	SGPC3,
+};
+
+enum sgp30_channel_idx {
+	SGP30_IAQ_TVOC_IDX = 0,
+	SGP30_IAQ_CO2EQ_IDX,
+	SGP30_SIG_ETOH_IDX,
+	SGP30_SIG_H2_IDX,
+};
+
+enum sgpc3_channel_idx {
+	SGPC3_IAQ_TVOC_IDX = 10,
+	SGPC3_SIG_ETOH_IDX,
+};
+
+enum sgp_cmd {
+	SGP_CMD_IAQ_INIT			= SGP_CMD(0x2003),
+	SGP_CMD_IAQ_MEASURE			= SGP_CMD(0x2008),
+	SGP_CMD_GET_FEATURE_SET			= SGP_CMD(0x202f),
+	SGP_CMD_GET_SERIAL_ID			= SGP_CMD(0x3682),
+
+	SGP30_CMD_MEASURE_SIGNAL		= SGP_CMD(0x2050),
+
+	SGPC3_CMD_MEASURE_RAW			= SGP_CMD(0x2046),
+};
+
+struct sgp_version {
+	u8 major;
+	u8 minor;
+};
+
+struct sgp_crc_word {
+	__be16 value;
+	u8 crc8;
+} __attribute__((__packed__));
+
+union sgp_reading {
+	u8 start;
+	struct sgp_crc_word raw_words[4];
+};
+
+enum _iaq_buffer_state {
+	IAQ_BUFFER_EMPTY = 0,
+	IAQ_BUFFER_DEFAULT_VALS,
+	IAQ_BUFFER_VALID,
+};
+
+struct sgp_data {
+	struct i2c_client *client;
+	struct task_struct *iaq_thread;
+	struct mutex data_lock;
+	unsigned long iaq_init_start_jiffies;
+	unsigned long iaq_defval_skip_jiffies;
+	u16 product_id;
+	u16 feature_set;
+	unsigned long measure_interval_jiffies;
+	enum sgp_cmd iaq_init_cmd;
+	enum sgp_cmd measure_iaq_cmd;
+	enum sgp_cmd measure_gas_signals_cmd;
+	union sgp_reading buffer;
+	union sgp_reading iaq_buffer;
+	enum _iaq_buffer_state iaq_buffer_state;
+};
+
+struct sgp_device {
+	const struct iio_chan_spec *channels;
+	int num_channels;
+};
+
+static const struct sgp_version supported_versions_sgp30[] = {
+	{
+		.major = 1,
+		.minor = 0,
+	},
+};
+
+static const struct sgp_version supported_versions_sgpc3[] = {
+	{
+		.major = 0,
+		.minor = 4,
+	},
+};
+
+static const struct iio_chan_spec sgp30_channels[] = {
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_VOC,
+		.modified = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+		.address = SGP30_IAQ_TVOC_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_CO2,

drivers/iio/chemical/sgp30.c line 155

Quoted 10 lines
drivers/iio/chemical/sgp30.c new file +10 -0
+		.modified = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+		.address = SGP30_IAQ_CO2EQ_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_ETHANOL,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),

Definitely looks like it'll fit in under 80 chars.

drivers/iio/chemical/sgp30.c line 166

Quoted 11 lines
drivers/iio/chemical/sgp30.c new file +11 -0
+		.address = SGP30_SIG_ETOH_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_H2,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),

Same here on length.

Quoted 11 lines
drivers/iio/chemical/sgp30.c new file +11 -0
+		.address = SGP30_SIG_H2_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+};
+
+static const struct iio_chan_spec sgpc3_channels[] = {
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_VOC,

drivers/iio/chemical/sgp30.c line 187

Quoted 10 lines
drivers/iio/chemical/sgp30.c new file +10 -0
+		.modified = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+		.address = SGPC3_IAQ_TVOC_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_ETHANOL,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),

And another on length.

Quoted 442 lines
drivers/iio/chemical/sgp30.c new file +20 -0
+		.address = SGPC3_SIG_ETOH_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+};
+
+static const struct sgp_device sgp_devices[] = {
+	[SGP30] = {
+		.channels = sgp30_channels,
+		.num_channels = ARRAY_SIZE(sgp30_channels),
+	},
+	[SGPC3] = {
+		.channels = sgpc3_channels,
+		.num_channels = ARRAY_SIZE(sgpc3_channels),
+	},
+};
+
+/**
+ * sgp_verify_buffer() - verify the checksums of the data buffer

words
+ *
+ * @data: SGP data
+ * @buf: Raw data buffer
+ * @word_count: Num data words stored in the buffer, excluding CRC bytes
+ *
+ * Return: 0 on success, negative error otherwise.
+ */

drivers/iio/chemical/sgp30.c new file +23 -0
+static int sgp_verify_buffer(const struct sgp_data *data,
+			     union sgp_reading *buf, size_t word_count)
+{
+	size_t size = word_count * (SGP_WORD_LEN + SGP_CRC8_LEN);
+	int i;
+	u8 crc;
+	u8 *data_buf = &buf->start;
+
+	for (i = 0; i < size; i += SGP_WORD_LEN + SGP_CRC8_LEN) {
+		crc = crc8(sgp_crc8_table, &data_buf[i], SGP_WORD_LEN,
+			   SGP_CRC8_INIT);
+		if (crc != data_buf[i + SGP_WORD_LEN]) {
+			dev_err(&data->client->dev, "CRC error\n");
+			return -EIO;
+		}
+	}
+
+	return 0;
+}
+
+/**
+ * sgp_read_cmd() - reads data from sensor after issuing a command
+ * The caller must hold data->data_lock for the duration of the

call.

+ * @data:        SGP data
+ * @cmd:         SGP Command to issue
+ * @buf:         Raw data buffer to use
+ * @word_count:  Num words to read, excluding CRC bytes

+ *
+ * Return: 0 on success, negative error otherwise.
+ */

drivers/iio/chemical/sgp30.c new file +30 -0
+static int sgp_read_cmd(struct sgp_data *data, enum sgp_cmd cmd,
+			union sgp_reading *buf, size_t word_count,
+			unsigned long duration_us)
+{
+	int ret;
+	struct i2c_client *client = data->client;
+	size_t size = word_count * (SGP_WORD_LEN + SGP_CRC8_LEN);
+	u8 *data_buf;
+
+	ret = i2c_master_send(client, (const char *)&cmd, SGP_CMD_LEN);
+	if (ret != SGP_CMD_LEN)
+		return -EIO;
+	usleep_range(duration_us, duration_us + 1000);
+
+	if (word_count == 0)
+		return 0;
+
+	data_buf = &buf->start;
+	ret = i2c_master_recv(client, data_buf, size);
+	if (ret < 0)
+		return ret;
+	if (ret != size)
+		return -EIO;
+
+	return sgp_verify_buffer(data, buf, word_count);
+}
+
+/**
+ * sgp_measure_iaq() - measure and retrieve IAQ values from sensor
+ * The caller must hold data->data_lock for the duration of the

call.
+ * @data: SGP data
+ *
+ * Return: 0 on success, -EBUSY on default values, negative
error
+ * otherwise.
+ */

drivers/iio/chemical/sgp30.c new file +6 -0
+
+static int sgp_measure_iaq(struct sgp_data *data)
+{
+	int ret;
+	/* data contains default values */
+	bool default_vals = !time_after(jiffies, data-
Quoted 1 line

iaq_init_start_jiffies +

drivers/iio/chemical/sgp30.c new file +1 -0
+						 data-
Quoted 1 line

iaq_defval_skip_jiffies);

drivers/iio/chemical/sgp30.c new file +2 -0
+
+	ret = sgp_read_cmd(data, data->measure_iaq_cmd, &data-
Quoted 1 line

iaq_buffer,

drivers/iio/chemical/sgp30.c new file +1 -0
+			   SGP_MEASUREMENT_LEN,

SGP_MEASUREMENT_DURATION_US);

drivers/iio/chemical/sgp30.c new file +14 -0
+	if (ret < 0)
+		return ret;
+
+	data->iaq_buffer_state = IAQ_BUFFER_DEFAULT_VALS;
+
+	if (default_vals)
+		return -EBUSY;
+
+	data->iaq_buffer_state = IAQ_BUFFER_VALID;
+
+	return 0;
+}
+
+static void sgp_iaq_thread_sleep_until(const struct sgp_data

*data,

drivers/iio/chemical/sgp30.c new file +7 -0
+				       unsigned long sleep_jiffies)
+{
+	const long IAQ_POLL = 50000;
+
+	while (!time_after(jiffies, sleep_jiffies)) {
+		usleep_range(IAQ_POLL, IAQ_POLL + 10000);
+		if (kthread_should_stop() || data-
Quoted 1 line

iaq_init_start_jiffies == 0)

drivers/iio/chemical/sgp30.c new file +14 -0
+			return;
+	}
+}
+
+static int sgp_iaq_threadfn(void *p)
+{
+	struct sgp_data *data = (struct sgp_data *)p;
+	unsigned long next_update_jiffies;
+	int ret;
+
+	while (!kthread_should_stop()) {
+		mutex_lock(&data->data_lock);
+		if (data->iaq_init_start_jiffies == 0) {
+			ret = sgp_read_cmd(data, data->iaq_init_cmd,

NULL, 0,

drivers/iio/chemical/sgp30.c new file +13 -0
+					   SGP_CMD_DURATION_US);
+			if (ret < 0)
+				goto unlock_sleep_continue;
+			data->iaq_init_start_jiffies = jiffies;
+		}
+
+		ret = sgp_measure_iaq(data);
+		if (ret && ret != -EBUSY) {
+			dev_warn(&data->client->dev,
+				 "IAQ measurement error [%d]\n", ret);
+		}
+unlock_sleep_continue:
+		next_update_jiffies = jiffies + data-
Quoted 1 line

measure_interval_jiffies;

drivers/iio/chemical/sgp30.c new file +51 -0
+		mutex_unlock(&data->data_lock);
+		sgp_iaq_thread_sleep_until(data, next_update_jiffies);
+	}
+
+	return 0;
+}
+
+static int sgp_read_raw(struct iio_dev *indio_dev,
+			struct iio_chan_spec const *chan, int *val,
+			int *val2, long mask)
+{
+	struct sgp_data *data = iio_priv(indio_dev);
+	struct sgp_crc_word *words;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
+		mutex_lock(&data->data_lock);
+		if (data->iaq_buffer_state != IAQ_BUFFER_VALID) {
+			mutex_unlock(&data->data_lock);
+			return -EBUSY;
+		}
+		words = data->iaq_buffer.raw_words;
+		switch (chan->address) {
+		case SGP30_IAQ_TVOC_IDX:
+		case SGPC3_IAQ_TVOC_IDX:
+			*val = 0;
+			*val2 = be16_to_cpu(words[1].value);
+			ret = IIO_VAL_INT_PLUS_NANO;
+			break;
+		case SGP30_IAQ_CO2EQ_IDX:
+			*val = 0;
+			*val2 = be16_to_cpu(words[0].value);
+			ret = IIO_VAL_INT_PLUS_MICRO;
+			break;
+		default:
+			ret = -EINVAL;
+			break;
+		}
+		mutex_unlock(&data->data_lock);
+		break;
+	case IIO_CHAN_INFO_RAW:
+		mutex_lock(&data->data_lock);
+		if (chan->address == SGPC3_SIG_ETOH_IDX) {
+			if (data->iaq_buffer_state == IAQ_BUFFER_EMPTY)
+				ret = -EBUSY;
+			else
+				ret = 0;
+			words = data->iaq_buffer.raw_words;
+		} else {
+			ret = sgp_read_cmd(data, data-
Quoted 1 line

measure_gas_signals_cmd,

drivers/iio/chemical/sgp30.c new file +1 -0
+					   &data->buffer,

SGP_MEASUREMENT_LEN,

drivers/iio/chemical/sgp30.c new file +1 -0
+					   SGP_MEASUREMENT_DURATION_US)

;

drivers/iio/chemical/sgp30.c new file +42 -0
+			words = data->buffer.raw_words;
+		}
+		if (ret) {
+			mutex_unlock(&data->data_lock);
+			return ret;
+		}
+
+		switch (chan->address) {
+		case SGP30_SIG_ETOH_IDX:
+			*val = be16_to_cpu(words[1].value);
+			ret = IIO_VAL_INT;
+			break;
+		case SGPC3_SIG_ETOH_IDX:
+		case SGP30_SIG_H2_IDX:
+			*val = be16_to_cpu(words[0].value);
+			ret = IIO_VAL_INT;
+			break;
+		default:
+			ret = -EINVAL;
+			break;
+		}
+		mutex_unlock(&data->data_lock);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return ret;
+}
+
+static int sgp_check_compat(struct sgp_data *data,
+			    unsigned int product_id)
+{
+	const struct sgp_version *supported_versions;
+	u16 ix, num_fs;
+	u16 product, generation, major, minor;
+
+	/* driver does not match product */
+	generation = SGP_VERS_GEN(data);
+	if (generation != 0) {
+		dev_err(&data->client->dev,
+			"incompatible product generation %d != 0",

generation);

drivers/iio/chemical/sgp30.c new file +7 -0
+		return -ENODEV;
+	}
+
+	product = SGP_VERS_PRODUCT(data);
+	if (product != product_id) {
+		dev_err(&data->client->dev,
+			"sensor reports a different product:

0x%04hx\n",

drivers/iio/chemical/sgp30.c new file +8 -0
+			product);
+		return -ENODEV;
+	}
+
+	if (SGP_VERS_RESERVED(data))
+		dev_warn(&data->client->dev, "reserved bit is set\n");
+
+	/* engineering samples are not supported: no interface

guarantees */

drivers/iio/chemical/sgp30.c new file +37 -0
+	if (SGP_VERS_ENG_BIT(data))
+		return -ENODEV;
+
+	switch (product) {
+	case SGP30:
+		supported_versions = supported_versions_sgp30;
+		num_fs = ARRAY_SIZE(supported_versions_sgp30);
+		break;
+	case SGPC3:
+		supported_versions = supported_versions_sgpc3;
+		num_fs = ARRAY_SIZE(supported_versions_sgpc3);
+		break;
+	default:
+		return -ENODEV;
+	}
+
+	major = SGP_VERS_MAJOR(data);
+	minor = SGP_VERS_MINOR(data);
+	for (ix = 0; ix < num_fs; ix++) {
+		if (major == supported_versions[ix].major &&
+		    minor >= supported_versions[ix].minor)
+			return 0;
+	}
+	dev_err(&data->client->dev, "unsupported sgp version: %d.%d\n",
+		major, minor);
+
+	return -ENODEV;
+}
+
+static void sgp_init(struct sgp_data *data)
+{
+	data->iaq_init_cmd = SGP_CMD_IAQ_INIT;
+	data->iaq_init_start_jiffies = 0;
+	data->iaq_buffer_state = IAQ_BUFFER_EMPTY;
+	switch (SGP_VERS_PRODUCT(data)) {
+	case SGP30:
+		data->measure_interval_jiffies =

SGP30_MEASURE_INTERVAL_HZ * HZ;

drivers/iio/chemical/sgp30.c new file +2 -0
+		data->measure_iaq_cmd = SGP_CMD_IAQ_MEASURE;
+		data->measure_gas_signals_cmd =

SGP30_CMD_MEASURE_SIGNAL;

drivers/iio/chemical/sgp30.c new file +5 -0
+		data->product_id = SGP30;
+		data->iaq_defval_skip_jiffies = 15 * HZ;
+		break;
+	case SGPC3:
+		data->measure_interval_jiffies =

SGPC3_MEASURE_INTERVAL_HZ * HZ;

drivers/iio/chemical/sgp30.c new file +46 -0
+		data->measure_iaq_cmd = SGPC3_CMD_MEASURE_RAW;
+		data->measure_gas_signals_cmd = SGPC3_CMD_MEASURE_RAW;
+		data->product_id = SGPC3;
+		data->iaq_defval_skip_jiffies =
+			43 * data->measure_interval_jiffies;
+		break;
+	};
+}
+
+static const struct iio_info sgp_info = {
+	.read_raw	= sgp_read_raw,
+};
+
+static const struct of_device_id sgp_dt_ids[] = {
+	{ .compatible = "sensirion,sgp30", .data = (void *)SGP30 },
+	{ .compatible = "sensirion,sgpc3", .data = (void *)SGPC3 },
+	{ }
+};
+
+static int sgp_probe(struct i2c_client *client,
+		     const struct i2c_device_id *id)
+{
+	struct iio_dev *indio_dev;
+	struct sgp_data *data;
+	const struct of_device_id *of_id;
+	unsigned long product_id;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	of_id = of_match_device(sgp_dt_ids, &client->dev);
+	if (of_id)
+		product_id = (unsigned long)of_id->data;
+	else
+		product_id = id->driver_data;
+
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	data->client = client;
+	crc8_populate_msb(sgp_crc8_table, SGP_CRC8_POLYNOMIAL);
+	mutex_init(&data->data_lock);
+
+	/* get feature set version and write it to client data */
+	ret = sgp_read_cmd(data, SGP_CMD_GET_FEATURE_SET, &data-
Quoted 1 line

buffer, 1,

drivers/iio/chemical/sgp30.c new file +5 -0
+			   SGP_CMD_DURATION_US);
+	if (ret < 0)
+		return ret;
+
+	data->feature_set = be16_to_cpu(data-
Quoted 1 line

buffer.raw_words[0].value);

drivers/iio/chemical/sgp30.c new file +17 -0
+
+	ret = sgp_check_compat(data, product_id);
+	if (ret)
+		return ret;
+
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->info = &sgp_info;
+	indio_dev->name = id->name;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = sgp_devices[product_id].channels;
+	indio_dev->num_channels = sgp_devices[product_id].num_channels;
+
+	sgp_init(data);
+
+	ret = devm_iio_device_register(&client->dev, indio_dev);
+	if (ret) {
+		dev_err(&client->dev, "failed to register iio

device\n");

drivers/iio/chemical/sgp30.c new file +41 -0
+		return ret;
+	}
+
+	data->iaq_thread = kthread_run(sgp_iaq_threadfn, data,
+				       "%s-iaq", data->client->name);
+
+	return 0;
+}
+
+static int sgp_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+	struct sgp_data *data = iio_priv(indio_dev);
+
+	if (data->iaq_thread)
+		kthread_stop(data->iaq_thread);
+
+	return 0;
+}
+
+static const struct i2c_device_id sgp_id[] = {
+	{ "sgp30", SGP30 },
+	{ "sgpc3", SGPC3 },
+	{ }
+};
+
+MODULE_DEVICE_TABLE(i2c, sgp_id);
+MODULE_DEVICE_TABLE(of, sgp_dt_ids);
+
+static struct i2c_driver sgp_driver = {
+	.driver = {
+		.name = "sgp30",
+		.of_match_table = of_match_ptr(sgp_dt_ids),
+	},
+	.probe = sgp_probe,
+	.remove = sgp_remove,
+	.id_table = sgp_id,
+};
+module_i2c_driver(sgp_driver);
+
+MODULE_AUTHOR("Andreas Brauchli <[email protected]>")

;

drivers/iio/chemical/sgp30.c new file +3 -0
+MODULE_AUTHOR("Pascal Sachs <[email protected]>");
+MODULE_DESCRIPTION("Sensirion SGP gas sensors");
+MODULE_LICENSE("GPL v2");
#3 lore raw
Quoted 58 lines · On Sun, 16 Dec 2018 17:38:18 +0100 Andreas Brauchli…
Quoted 19 lines · Jonathan Cameron
Quoted 16 lines · On Thu, 13 Dec 2018 15:43:23 +0100 Andreas Brauchli…

Support Sensirion SGP30 and SGPC3 multi-pixel I2C gas sensors

Supported Features:

* Indoor Air Quality (IAQ) concentrations for
- tVOC (in_concentration_voc_input)
- CO2eq (in_concentration_co2_input) - SGP30 only

IAQ concentrations are periodically read out by a background
thread
to allow the sensor to maintain its internal baseline.

* Gas concentration signals
- Ethanol (in_concentration_ethanol_raw)
- H2 (in_concentration_h2_raw) - SGP30 only

Quoted 7 lines
Quoted 4 lines

https://www.sensirion.com/file/datasheet_sgp30
https://www.sensirion.com/file/datasheet_sgpc3

Why do we provide endianness info given we don't provide the buffered interface? It's only used when we do.

Makes sense but I wasn't aware of it. The initial submission came with buffering, so it probably wasn't spotted in v2.

Quoted 4 lines

Some over the top line wrapping as well. However as that's it I'll just clean up whilst applying. If I've missed something odd around the endian thing shout!

No that's it, thanks a lot.

Quoted 20 lines

Jonathan

Quoted 17 lines
---
 drivers/iio/chemical/Kconfig  |  13 +
 drivers/iio/chemical/Makefile |   1 +
 drivers/iio/chemical/sgp30.c  | 603
++++++++++++++++++++++++++++++++++
 3 files changed, 617 insertions(+)
 create mode 100644 drivers/iio/chemical/sgp30.c
drivers/iio/chemical/Kconfig +0 -0
index b8e005be4f87..b0c912be94a9 100644
@@ -61,6 +61,19 @@ config IAQCORE
 	  iAQ-Core Continuous/Pulsed VOC (Volatile Organic Compounds)
 	  sensors
 
Quoted 4 lines
Quoted 1 line

drivers/iio/chemical/Kconfig config IAQCORE · line 64

Quoted 1 line
drivers/iio/chemical/Kconfig +1 -0
+config SENSIRION_SGP30  

I noticed that in Tomasz's SPS30 patch it's just SPS30, so for consistency's sake I'm fine with dropping the branding. Up to you.

We've never had any hard rules on this, at least partly because a lot of drivers came from other subsystems in the early days.

We probably should have a rule though. SGP30 for example is generic enough that it might well be reused by someone else.

Anyone have any strong opinions on this? If not I think I'll start encouraging prefixing with manufacturer of first supported device.

Jonathan

Quoted 681 lines
Quoted 680 lines
Quoted 186 lines
drivers/iio/chemical/Kconfig +12 -0
+	tristate "Sensirion SGPxx gas sensors"
+	depends on I2C
+	select CRC8
+	help
+	  Say Y here to build I2C interface support for the following
+	  Sensirion SGP gas sensors:
+	    * SGP30 gas sensor
+	    * SGPC3 low power gas sensor
+
+	  To compile this driver as module, choose M here: the
+	  module will be called sgp30.
+
 config VZ89X
 	tristate "SGX Sensortech MiCS VZ89X VOC sensor"
 	depends on I2C
drivers/iio/chemical/Makefile +1 -0
index 2f4c4ba4d781..61e83944db1a 100644
@@ -9,4 +9,5 @@ obj-$(CONFIG_BME680_I2C) += bme680_i2c.o
 obj-$(CONFIG_BME680_SPI) += bme680_spi.o
 obj-$(CONFIG_CCS811)		+= ccs811.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
+obj-$(CONFIG_SENSIRION_SGP30)	+= sgp30.o
 obj-$(CONFIG_VZ89X)		+= vz89x.o
drivers/iio/chemical/sgp30.c new file +5 -0
new file mode 100644
index 000000000000..90eb4cbb18cf
@@ -0,0 +1,603 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * sgp30.c - Support for Sensirion SGP Gas Sensors
+ *
+ * Copyright (C) 2018 Andreas Brauchli <  

[email protected]>
+ *
+ * I2C slave address: 0x58
+ *
+ * Datasheets:
+ * https://www.sensirion.com/file/datasheet_sgp30
+ * https://www.sensirion.com/file/datasheet_sgpc3
+ *
+ * TODO:
+ * - baseline support
+ * - humidity compensation
+ * - power mode switching (SGPC3)
+ */

drivers/iio/chemical/sgp30.c new file +20 -0
+
+#include <linux/crc8.h>
+#include <linux/delay.h>
+#include <linux/kthread.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/i2c.h>
+#include <linux/of_device.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+#define SGP_WORD_LEN				2
+#define SGP_CRC8_POLYNOMIAL			0x31
+#define SGP_CRC8_INIT				0xff
+#define SGP_CRC8_LEN				1
+#define SGP_CMD(cmd_word)			cpu_to_be16(cmd_word)
+#define SGP_CMD_DURATION_US			12000
+#define SGP_MEASUREMENT_DURATION_US		50000
+#define SGP_CMD_LEN				SGP_WORD_LEN
+#define SGP_CMD_MAX_BUF_SIZE			(SGP_CMD_LEN +

2 * SGP_WORD_LEN)

drivers/iio/chemical/sgp30.c new file +4 -0
+#define SGP_MEASUREMENT_LEN			2
+#define SGP30_MEASURE_INTERVAL_HZ		1
+#define SGPC3_MEASURE_INTERVAL_HZ		2
+#define SGP_VERS_PRODUCT(data)	((((data)->feature_set) &

0xf000) >> 12)
+#define SGP_VERS_RESERVED(data) ((((data)->feature_set) &
0x0800) >> 11)

drivers/iio/chemical/sgp30.c new file +2 -0
+#define SGP_VERS_GEN(data)	((((data)->feature_set) & 0x0600) >> 9)
+#define SGP_VERS_ENG_BIT(data)	((((data)->feature_set) &

0x0100) >> 8)
+#define SGP_VERS_MAJOR(data) ((((data)->feature_set) &
0x00e0) >> 5)
+#define SGP_VERS_MINOR(data) (((data)->feature_set) &
0x001f)

drivers/iio/chemical/sgp30.c new file +99 -0
+
+DECLARE_CRC8_TABLE(sgp_crc8_table);
+
+enum sgp_product_id {
+	SGP30 = 0,
+	SGPC3,
+};
+
+enum sgp30_channel_idx {
+	SGP30_IAQ_TVOC_IDX = 0,
+	SGP30_IAQ_CO2EQ_IDX,
+	SGP30_SIG_ETOH_IDX,
+	SGP30_SIG_H2_IDX,
+};
+
+enum sgpc3_channel_idx {
+	SGPC3_IAQ_TVOC_IDX = 10,
+	SGPC3_SIG_ETOH_IDX,
+};
+
+enum sgp_cmd {
+	SGP_CMD_IAQ_INIT			= SGP_CMD(0x2003),
+	SGP_CMD_IAQ_MEASURE			= SGP_CMD(0x2008),
+	SGP_CMD_GET_FEATURE_SET			= SGP_CMD(0x202f),
+	SGP_CMD_GET_SERIAL_ID			= SGP_CMD(0x3682),
+
+	SGP30_CMD_MEASURE_SIGNAL		= SGP_CMD(0x2050),
+
+	SGPC3_CMD_MEASURE_RAW			= SGP_CMD(0x2046),
+};
+
+struct sgp_version {
+	u8 major;
+	u8 minor;
+};
+
+struct sgp_crc_word {
+	__be16 value;
+	u8 crc8;
+} __attribute__((__packed__));
+
+union sgp_reading {
+	u8 start;
+	struct sgp_crc_word raw_words[4];
+};
+
+enum _iaq_buffer_state {
+	IAQ_BUFFER_EMPTY = 0,
+	IAQ_BUFFER_DEFAULT_VALS,
+	IAQ_BUFFER_VALID,
+};
+
+struct sgp_data {
+	struct i2c_client *client;
+	struct task_struct *iaq_thread;
+	struct mutex data_lock;
+	unsigned long iaq_init_start_jiffies;
+	unsigned long iaq_defval_skip_jiffies;
+	u16 product_id;
+	u16 feature_set;
+	unsigned long measure_interval_jiffies;
+	enum sgp_cmd iaq_init_cmd;
+	enum sgp_cmd measure_iaq_cmd;
+	enum sgp_cmd measure_gas_signals_cmd;
+	union sgp_reading buffer;
+	union sgp_reading iaq_buffer;
+	enum _iaq_buffer_state iaq_buffer_state;
+};
+
+struct sgp_device {
+	const struct iio_chan_spec *channels;
+	int num_channels;
+};
+
+static const struct sgp_version supported_versions_sgp30[] = {
+	{
+		.major = 1,
+		.minor = 0,
+	},
+};
+
+static const struct sgp_version supported_versions_sgpc3[] = {
+	{
+		.major = 0,
+		.minor = 4,
+	},
+};
+
+static const struct iio_chan_spec sgp30_channels[] = {
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_VOC,
+		.modified = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+		.address = SGP30_IAQ_TVOC_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_CO2,

drivers/iio/chemical/sgp30.c line 155

Quoted 10 lines
drivers/iio/chemical/sgp30.c new file +10 -0
+		.modified = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+		.address = SGP30_IAQ_CO2EQ_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_ETHANOL,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),  

Definitely looks like it'll fit in under 80 chars.

drivers/iio/chemical/sgp30.c line 166

Quoted 11 lines
drivers/iio/chemical/sgp30.c new file +11 -0
+		.address = SGP30_SIG_ETOH_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_H2,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),  

Same here on length.

Quoted 11 lines
drivers/iio/chemical/sgp30.c new file +11 -0
+		.address = SGP30_SIG_H2_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+};
+
+static const struct iio_chan_spec sgpc3_channels[] = {
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_VOC,

drivers/iio/chemical/sgp30.c line 187

Quoted 10 lines
drivers/iio/chemical/sgp30.c new file +10 -0
+		.modified = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+		.address = SGPC3_IAQ_TVOC_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_ETHANOL,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),  

And another on length.

Quoted 442 lines
drivers/iio/chemical/sgp30.c new file +20 -0
+		.address = SGPC3_SIG_ETOH_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+};
+
+static const struct sgp_device sgp_devices[] = {
+	[SGP30] = {
+		.channels = sgp30_channels,
+		.num_channels = ARRAY_SIZE(sgp30_channels),
+	},
+	[SGPC3] = {
+		.channels = sgpc3_channels,
+		.num_channels = ARRAY_SIZE(sgpc3_channels),
+	},
+};
+
+/**
+ * sgp_verify_buffer() - verify the checksums of the data buffer

words
+ *
+ * @data: SGP data
+ * @buf: Raw data buffer
+ * @word_count: Num data words stored in the buffer, excluding CRC bytes
+ *
+ * Return: 0 on success, negative error otherwise.
+ */

drivers/iio/chemical/sgp30.c new file +23 -0
+static int sgp_verify_buffer(const struct sgp_data *data,
+			     union sgp_reading *buf, size_t word_count)
+{
+	size_t size = word_count * (SGP_WORD_LEN + SGP_CRC8_LEN);
+	int i;
+	u8 crc;
+	u8 *data_buf = &buf->start;
+
+	for (i = 0; i < size; i += SGP_WORD_LEN + SGP_CRC8_LEN) {
+		crc = crc8(sgp_crc8_table, &data_buf[i], SGP_WORD_LEN,
+			   SGP_CRC8_INIT);
+		if (crc != data_buf[i + SGP_WORD_LEN]) {
+			dev_err(&data->client->dev, "CRC error\n");
+			return -EIO;
+		}
+	}
+
+	return 0;
+}
+
+/**
+ * sgp_read_cmd() - reads data from sensor after issuing a command
+ * The caller must hold data->data_lock for the duration of the

call.

+ * @data:        SGP data
+ * @cmd:         SGP Command to issue
+ * @buf:         Raw data buffer to use
+ * @word_count:  Num words to read, excluding CRC bytes

+ *
+ * Return: 0 on success, negative error otherwise.
+ */

drivers/iio/chemical/sgp30.c new file +30 -0
+static int sgp_read_cmd(struct sgp_data *data, enum sgp_cmd cmd,
+			union sgp_reading *buf, size_t word_count,
+			unsigned long duration_us)
+{
+	int ret;
+	struct i2c_client *client = data->client;
+	size_t size = word_count * (SGP_WORD_LEN + SGP_CRC8_LEN);
+	u8 *data_buf;
+
+	ret = i2c_master_send(client, (const char *)&cmd, SGP_CMD_LEN);
+	if (ret != SGP_CMD_LEN)
+		return -EIO;
+	usleep_range(duration_us, duration_us + 1000);
+
+	if (word_count == 0)
+		return 0;
+
+	data_buf = &buf->start;
+	ret = i2c_master_recv(client, data_buf, size);
+	if (ret < 0)
+		return ret;
+	if (ret != size)
+		return -EIO;
+
+	return sgp_verify_buffer(data, buf, word_count);
+}
+
+/**
+ * sgp_measure_iaq() - measure and retrieve IAQ values from sensor
+ * The caller must hold data->data_lock for the duration of the

call.
+ * @data: SGP data
+ *
+ * Return: 0 on success, -EBUSY on default values, negative
error
+ * otherwise.
+ */

drivers/iio/chemical/sgp30.c new file +6 -0
+
+static int sgp_measure_iaq(struct sgp_data *data)
+{
+	int ret;
+	/* data contains default values */
+	bool default_vals = !time_after(jiffies, data-  
Quoted 1 line

iaq_init_start_jiffies +

drivers/iio/chemical/sgp30.c new file +1 -0
+						 data-  
Quoted 1 line

iaq_defval_skip_jiffies);

drivers/iio/chemical/sgp30.c new file +2 -0
+
+	ret = sgp_read_cmd(data, data->measure_iaq_cmd, &data-  
Quoted 1 line

iaq_buffer,

drivers/iio/chemical/sgp30.c new file +1 -0
+			   SGP_MEASUREMENT_LEN,

SGP_MEASUREMENT_DURATION_US);

drivers/iio/chemical/sgp30.c new file +14 -0
+	if (ret < 0)
+		return ret;
+
+	data->iaq_buffer_state = IAQ_BUFFER_DEFAULT_VALS;
+
+	if (default_vals)
+		return -EBUSY;
+
+	data->iaq_buffer_state = IAQ_BUFFER_VALID;
+
+	return 0;
+}
+
+static void sgp_iaq_thread_sleep_until(const struct sgp_data

*data,

drivers/iio/chemical/sgp30.c new file +7 -0
+				       unsigned long sleep_jiffies)
+{
+	const long IAQ_POLL = 50000;
+
+	while (!time_after(jiffies, sleep_jiffies)) {
+		usleep_range(IAQ_POLL, IAQ_POLL + 10000);
+		if (kthread_should_stop() || data-  
Quoted 1 line

iaq_init_start_jiffies == 0)

drivers/iio/chemical/sgp30.c new file +14 -0
+			return;
+	}
+}
+
+static int sgp_iaq_threadfn(void *p)
+{
+	struct sgp_data *data = (struct sgp_data *)p;
+	unsigned long next_update_jiffies;
+	int ret;
+
+	while (!kthread_should_stop()) {
+		mutex_lock(&data->data_lock);
+		if (data->iaq_init_start_jiffies == 0) {
+			ret = sgp_read_cmd(data, data->iaq_init_cmd,

NULL, 0,

drivers/iio/chemical/sgp30.c new file +13 -0
+					   SGP_CMD_DURATION_US);
+			if (ret < 0)
+				goto unlock_sleep_continue;
+			data->iaq_init_start_jiffies = jiffies;
+		}
+
+		ret = sgp_measure_iaq(data);
+		if (ret && ret != -EBUSY) {
+			dev_warn(&data->client->dev,
+				 "IAQ measurement error [%d]\n", ret);
+		}
+unlock_sleep_continue:
+		next_update_jiffies = jiffies + data-  
Quoted 1 line

measure_interval_jiffies;

drivers/iio/chemical/sgp30.c new file +51 -0
+		mutex_unlock(&data->data_lock);
+		sgp_iaq_thread_sleep_until(data, next_update_jiffies);
+	}
+
+	return 0;
+}
+
+static int sgp_read_raw(struct iio_dev *indio_dev,
+			struct iio_chan_spec const *chan, int *val,
+			int *val2, long mask)
+{
+	struct sgp_data *data = iio_priv(indio_dev);
+	struct sgp_crc_word *words;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
+		mutex_lock(&data->data_lock);
+		if (data->iaq_buffer_state != IAQ_BUFFER_VALID) {
+			mutex_unlock(&data->data_lock);
+			return -EBUSY;
+		}
+		words = data->iaq_buffer.raw_words;
+		switch (chan->address) {
+		case SGP30_IAQ_TVOC_IDX:
+		case SGPC3_IAQ_TVOC_IDX:
+			*val = 0;
+			*val2 = be16_to_cpu(words[1].value);
+			ret = IIO_VAL_INT_PLUS_NANO;
+			break;
+		case SGP30_IAQ_CO2EQ_IDX:
+			*val = 0;
+			*val2 = be16_to_cpu(words[0].value);
+			ret = IIO_VAL_INT_PLUS_MICRO;
+			break;
+		default:
+			ret = -EINVAL;
+			break;
+		}
+		mutex_unlock(&data->data_lock);
+		break;
+	case IIO_CHAN_INFO_RAW:
+		mutex_lock(&data->data_lock);
+		if (chan->address == SGPC3_SIG_ETOH_IDX) {
+			if (data->iaq_buffer_state == IAQ_BUFFER_EMPTY)
+				ret = -EBUSY;
+			else
+				ret = 0;
+			words = data->iaq_buffer.raw_words;
+		} else {
+			ret = sgp_read_cmd(data, data-  
Quoted 1 line

measure_gas_signals_cmd,

drivers/iio/chemical/sgp30.c new file +1 -0
+					   &data->buffer,

SGP_MEASUREMENT_LEN,

drivers/iio/chemical/sgp30.c new file +1 -0
+					   SGP_MEASUREMENT_DURATION_US)

;

drivers/iio/chemical/sgp30.c new file +42 -0
+			words = data->buffer.raw_words;
+		}
+		if (ret) {
+			mutex_unlock(&data->data_lock);
+			return ret;
+		}
+
+		switch (chan->address) {
+		case SGP30_SIG_ETOH_IDX:
+			*val = be16_to_cpu(words[1].value);
+			ret = IIO_VAL_INT;
+			break;
+		case SGPC3_SIG_ETOH_IDX:
+		case SGP30_SIG_H2_IDX:
+			*val = be16_to_cpu(words[0].value);
+			ret = IIO_VAL_INT;
+			break;
+		default:
+			ret = -EINVAL;
+			break;
+		}
+		mutex_unlock(&data->data_lock);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return ret;
+}
+
+static int sgp_check_compat(struct sgp_data *data,
+			    unsigned int product_id)
+{
+	const struct sgp_version *supported_versions;
+	u16 ix, num_fs;
+	u16 product, generation, major, minor;
+
+	/* driver does not match product */
+	generation = SGP_VERS_GEN(data);
+	if (generation != 0) {
+		dev_err(&data->client->dev,
+			"incompatible product generation %d != 0",

generation);

drivers/iio/chemical/sgp30.c new file +7 -0
+		return -ENODEV;
+	}
+
+	product = SGP_VERS_PRODUCT(data);
+	if (product != product_id) {
+		dev_err(&data->client->dev,
+			"sensor reports a different product:

0x%04hx\n",

drivers/iio/chemical/sgp30.c new file +8 -0
+			product);
+		return -ENODEV;
+	}
+
+	if (SGP_VERS_RESERVED(data))
+		dev_warn(&data->client->dev, "reserved bit is set\n");
+
+	/* engineering samples are not supported: no interface

guarantees */

drivers/iio/chemical/sgp30.c new file +37 -0
+	if (SGP_VERS_ENG_BIT(data))
+		return -ENODEV;
+
+	switch (product) {
+	case SGP30:
+		supported_versions = supported_versions_sgp30;
+		num_fs = ARRAY_SIZE(supported_versions_sgp30);
+		break;
+	case SGPC3:
+		supported_versions = supported_versions_sgpc3;
+		num_fs = ARRAY_SIZE(supported_versions_sgpc3);
+		break;
+	default:
+		return -ENODEV;
+	}
+
+	major = SGP_VERS_MAJOR(data);
+	minor = SGP_VERS_MINOR(data);
+	for (ix = 0; ix < num_fs; ix++) {
+		if (major == supported_versions[ix].major &&
+		    minor >= supported_versions[ix].minor)
+			return 0;
+	}
+	dev_err(&data->client->dev, "unsupported sgp version: %d.%d\n",
+		major, minor);
+
+	return -ENODEV;
+}
+
+static void sgp_init(struct sgp_data *data)
+{
+	data->iaq_init_cmd = SGP_CMD_IAQ_INIT;
+	data->iaq_init_start_jiffies = 0;
+	data->iaq_buffer_state = IAQ_BUFFER_EMPTY;
+	switch (SGP_VERS_PRODUCT(data)) {
+	case SGP30:
+		data->measure_interval_jiffies =

SGP30_MEASURE_INTERVAL_HZ * HZ;

drivers/iio/chemical/sgp30.c new file +2 -0
+		data->measure_iaq_cmd = SGP_CMD_IAQ_MEASURE;
+		data->measure_gas_signals_cmd =

SGP30_CMD_MEASURE_SIGNAL;

drivers/iio/chemical/sgp30.c new file +5 -0
+		data->product_id = SGP30;
+		data->iaq_defval_skip_jiffies = 15 * HZ;
+		break;
+	case SGPC3:
+		data->measure_interval_jiffies =

SGPC3_MEASURE_INTERVAL_HZ * HZ;

drivers/iio/chemical/sgp30.c new file +46 -0
+		data->measure_iaq_cmd = SGPC3_CMD_MEASURE_RAW;
+		data->measure_gas_signals_cmd = SGPC3_CMD_MEASURE_RAW;
+		data->product_id = SGPC3;
+		data->iaq_defval_skip_jiffies =
+			43 * data->measure_interval_jiffies;
+		break;
+	};
+}
+
+static const struct iio_info sgp_info = {
+	.read_raw	= sgp_read_raw,
+};
+
+static const struct of_device_id sgp_dt_ids[] = {
+	{ .compatible = "sensirion,sgp30", .data = (void *)SGP30 },
+	{ .compatible = "sensirion,sgpc3", .data = (void *)SGPC3 },
+	{ }
+};
+
+static int sgp_probe(struct i2c_client *client,
+		     const struct i2c_device_id *id)
+{
+	struct iio_dev *indio_dev;
+	struct sgp_data *data;
+	const struct of_device_id *of_id;
+	unsigned long product_id;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	of_id = of_match_device(sgp_dt_ids, &client->dev);
+	if (of_id)
+		product_id = (unsigned long)of_id->data;
+	else
+		product_id = id->driver_data;
+
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	data->client = client;
+	crc8_populate_msb(sgp_crc8_table, SGP_CRC8_POLYNOMIAL);
+	mutex_init(&data->data_lock);
+
+	/* get feature set version and write it to client data */
+	ret = sgp_read_cmd(data, SGP_CMD_GET_FEATURE_SET, &data-  
Quoted 1 line

buffer, 1,

drivers/iio/chemical/sgp30.c new file +5 -0
+			   SGP_CMD_DURATION_US);
+	if (ret < 0)
+		return ret;
+
+	data->feature_set = be16_to_cpu(data-  
Quoted 1 line

buffer.raw_words[0].value);

drivers/iio/chemical/sgp30.c new file +17 -0
+
+	ret = sgp_check_compat(data, product_id);
+	if (ret)
+		return ret;
+
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->info = &sgp_info;
+	indio_dev->name = id->name;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = sgp_devices[product_id].channels;
+	indio_dev->num_channels = sgp_devices[product_id].num_channels;
+
+	sgp_init(data);
+
+	ret = devm_iio_device_register(&client->dev, indio_dev);
+	if (ret) {
+		dev_err(&client->dev, "failed to register iio

device\n");

drivers/iio/chemical/sgp30.c new file +41 -0
+		return ret;
+	}
+
+	data->iaq_thread = kthread_run(sgp_iaq_threadfn, data,
+				       "%s-iaq", data->client->name);
+
+	return 0;
+}
+
+static int sgp_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+	struct sgp_data *data = iio_priv(indio_dev);
+
+	if (data->iaq_thread)
+		kthread_stop(data->iaq_thread);
+
+	return 0;
+}
+
+static const struct i2c_device_id sgp_id[] = {
+	{ "sgp30", SGP30 },
+	{ "sgpc3", SGPC3 },
+	{ }
+};
+
+MODULE_DEVICE_TABLE(i2c, sgp_id);
+MODULE_DEVICE_TABLE(of, sgp_dt_ids);
+
+static struct i2c_driver sgp_driver = {
+	.driver = {
+		.name = "sgp30",
+		.of_match_table = of_match_ptr(sgp_dt_ids),
+	},
+	.probe = sgp_probe,
+	.remove = sgp_remove,
+	.id_table = sgp_id,
+};
+module_i2c_driver(sgp_driver);
+
+MODULE_AUTHOR("Andreas Brauchli <[email protected]>")

;

drivers/iio/chemical/sgp30.c new file +3 -0
+MODULE_AUTHOR("Pascal Sachs <[email protected]>");
+MODULE_DESCRIPTION("Sensirion SGP gas sensors");
+MODULE_LICENSE("GPL v2");  
#4 lore raw
Quoted 66 lines · Jonathan Cameron
Quoted 58 lines · On Sun, 16 Dec 2018 17:38:18 +0100 Andreas Brauchli…
Quoted 19 lines · Jonathan Cameron
Quoted 16 lines · On Thu, 13 Dec 2018 15:43:23 +0100 Andreas Brauchli…

Support Sensirion SGP30 and SGPC3 multi-pixel I2C gas sensors

Supported Features:

* Indoor Air Quality (IAQ) concentrations for
- tVOC (in_concentration_voc_input)
- CO2eq (in_concentration_co2_input) - SGP30 only

IAQ concentrations are periodically read out by a background
thread
to allow the sensor to maintain its internal baseline.

* Gas concentration signals
- Ethanol (in_concentration_ethanol_raw)
- H2 (in_concentration_h2_raw) - SGP30 only

Quoted 7 lines
Quoted 4 lines

https://www.sensirion.com/file/datasheet_sgp30
https://www.sensirion.com/file/datasheet_sgpc3

Why do we provide endianness info given we don't provide the buffered interface? It's only used when we do.

Makes sense but I wasn't aware of it. The initial submission came with buffering, so it probably wasn't spotted in v2.

Quoted 4 lines

Some over the top line wrapping as well. However as that's it I'll just clean up whilst applying. If I've missed something odd around the endian thing shout!

No that's it, thanks a lot.

Quoted 20 lines

Jonathan

Quoted 17 lines
---
 drivers/iio/chemical/Kconfig  |  13 +
 drivers/iio/chemical/Makefile |   1 +
 drivers/iio/chemical/sgp30.c  | 603
++++++++++++++++++++++++++++++++++
 3 files changed, 617 insertions(+)
 create mode 100644 drivers/iio/chemical/sgp30.c
drivers/iio/chemical/Kconfig +0 -0
index b8e005be4f87..b0c912be94a9 100644
@@ -61,6 +61,19 @@ config IAQCORE
 	  iAQ-Core Continuous/Pulsed VOC (Volatile Organic Compounds)
 	  sensors
 
Quoted 4 lines
Quoted 1 line

drivers/iio/chemical/Kconfig config IAQCORE · line 64

Quoted 1 line
drivers/iio/chemical/Kconfig +1 -0
+config SENSIRION_SGP30

I noticed that in Tomasz's SPS30 patch it's just SPS30, so for consistency's sake I'm fine with dropping the branding. Up to you.

Quoted 6 lines

We've never had any hard rules on this, at least partly because a lot of drivers came from other subsystems in the early days.

We probably should have a rule though. SGP30 for example is generic enough that it might well be reused by someone else.

Wondering how likely is this? These days some popular sensors have their clones. Interestingly enough, they are sold as genuine i.e without vendor/sensor name changes. On the other hand there are very same ICs manufactured by different companies but still with different names.

Quoted 3 lines

Anyone have any strong opinions on this? If not I think I'll start encouraging prefixing with manufacturer of first supported device.

What about sticking to what we have now and adding prefix when we do have naming collision? Just my 2 cents.

Quoted 685 lines

Jonathan

Quoted 681 lines
Quoted 680 lines
Quoted 186 lines
drivers/iio/chemical/Kconfig +12 -0
+	tristate "Sensirion SGPxx gas sensors"
+	depends on I2C
+	select CRC8
+	help
+	  Say Y here to build I2C interface support for the following
+	  Sensirion SGP gas sensors:
+	    * SGP30 gas sensor
+	    * SGPC3 low power gas sensor
+
+	  To compile this driver as module, choose M here: the
+	  module will be called sgp30.
+
 config VZ89X
 	tristate "SGX Sensortech MiCS VZ89X VOC sensor"
 	depends on I2C
drivers/iio/chemical/Makefile +1 -0
index 2f4c4ba4d781..61e83944db1a 100644
@@ -9,4 +9,5 @@ obj-$(CONFIG_BME680_I2C) += bme680_i2c.o
 obj-$(CONFIG_BME680_SPI) += bme680_spi.o
 obj-$(CONFIG_CCS811)		+= ccs811.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
+obj-$(CONFIG_SENSIRION_SGP30)	+= sgp30.o
 obj-$(CONFIG_VZ89X)		+= vz89x.o
drivers/iio/chemical/sgp30.c new file +5 -0
new file mode 100644
index 000000000000..90eb4cbb18cf
@@ -0,0 +1,603 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * sgp30.c - Support for Sensirion SGP Gas Sensors
+ *
+ * Copyright (C) 2018 Andreas Brauchli <

[email protected]>
+ *
+ * I2C slave address: 0x58
+ *
+ * Datasheets:
+ * https://www.sensirion.com/file/datasheet_sgp30
+ * https://www.sensirion.com/file/datasheet_sgpc3
+ *
+ * TODO:
+ * - baseline support
+ * - humidity compensation
+ * - power mode switching (SGPC3)
+ */

drivers/iio/chemical/sgp30.c new file +20 -0
+
+#include <linux/crc8.h>
+#include <linux/delay.h>
+#include <linux/kthread.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/i2c.h>
+#include <linux/of_device.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+#define SGP_WORD_LEN				2
+#define SGP_CRC8_POLYNOMIAL			0x31
+#define SGP_CRC8_INIT				0xff
+#define SGP_CRC8_LEN				1
+#define SGP_CMD(cmd_word)			cpu_to_be16(cmd_word)
+#define SGP_CMD_DURATION_US			12000
+#define SGP_MEASUREMENT_DURATION_US		50000
+#define SGP_CMD_LEN				SGP_WORD_LEN
+#define SGP_CMD_MAX_BUF_SIZE			(SGP_CMD_LEN +

2 * SGP_WORD_LEN)

drivers/iio/chemical/sgp30.c new file +4 -0
+#define SGP_MEASUREMENT_LEN			2
+#define SGP30_MEASURE_INTERVAL_HZ		1
+#define SGPC3_MEASURE_INTERVAL_HZ		2
+#define SGP_VERS_PRODUCT(data)	((((data)->feature_set) &

0xf000) >> 12)
+#define SGP_VERS_RESERVED(data) ((((data)->feature_set) &
0x0800) >> 11)

drivers/iio/chemical/sgp30.c new file +2 -0
+#define SGP_VERS_GEN(data)	((((data)->feature_set) & 0x0600) >> 9)
+#define SGP_VERS_ENG_BIT(data)	((((data)->feature_set) &

0x0100) >> 8)
+#define SGP_VERS_MAJOR(data) ((((data)->feature_set) &
0x00e0) >> 5)
+#define SGP_VERS_MINOR(data) (((data)->feature_set) &
0x001f)

drivers/iio/chemical/sgp30.c new file +99 -0
+
+DECLARE_CRC8_TABLE(sgp_crc8_table);
+
+enum sgp_product_id {
+	SGP30 = 0,
+	SGPC3,
+};
+
+enum sgp30_channel_idx {
+	SGP30_IAQ_TVOC_IDX = 0,
+	SGP30_IAQ_CO2EQ_IDX,
+	SGP30_SIG_ETOH_IDX,
+	SGP30_SIG_H2_IDX,
+};
+
+enum sgpc3_channel_idx {
+	SGPC3_IAQ_TVOC_IDX = 10,
+	SGPC3_SIG_ETOH_IDX,
+};
+
+enum sgp_cmd {
+	SGP_CMD_IAQ_INIT			= SGP_CMD(0x2003),
+	SGP_CMD_IAQ_MEASURE			= SGP_CMD(0x2008),
+	SGP_CMD_GET_FEATURE_SET			= SGP_CMD(0x202f),
+	SGP_CMD_GET_SERIAL_ID			= SGP_CMD(0x3682),
+
+	SGP30_CMD_MEASURE_SIGNAL		= SGP_CMD(0x2050),
+
+	SGPC3_CMD_MEASURE_RAW			= SGP_CMD(0x2046),
+};
+
+struct sgp_version {
+	u8 major;
+	u8 minor;
+};
+
+struct sgp_crc_word {
+	__be16 value;
+	u8 crc8;
+} __attribute__((__packed__));
+
+union sgp_reading {
+	u8 start;
+	struct sgp_crc_word raw_words[4];
+};
+
+enum _iaq_buffer_state {
+	IAQ_BUFFER_EMPTY = 0,
+	IAQ_BUFFER_DEFAULT_VALS,
+	IAQ_BUFFER_VALID,
+};
+
+struct sgp_data {
+	struct i2c_client *client;
+	struct task_struct *iaq_thread;
+	struct mutex data_lock;
+	unsigned long iaq_init_start_jiffies;
+	unsigned long iaq_defval_skip_jiffies;
+	u16 product_id;
+	u16 feature_set;
+	unsigned long measure_interval_jiffies;
+	enum sgp_cmd iaq_init_cmd;
+	enum sgp_cmd measure_iaq_cmd;
+	enum sgp_cmd measure_gas_signals_cmd;
+	union sgp_reading buffer;
+	union sgp_reading iaq_buffer;
+	enum _iaq_buffer_state iaq_buffer_state;
+};
+
+struct sgp_device {
+	const struct iio_chan_spec *channels;
+	int num_channels;
+};
+
+static const struct sgp_version supported_versions_sgp30[] = {
+	{
+		.major = 1,
+		.minor = 0,
+	},
+};
+
+static const struct sgp_version supported_versions_sgpc3[] = {
+	{
+		.major = 0,
+		.minor = 4,
+	},
+};
+
+static const struct iio_chan_spec sgp30_channels[] = {
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_VOC,
+		.modified = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+		.address = SGP30_IAQ_TVOC_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_CO2,

drivers/iio/chemical/sgp30.c line 155

Quoted 10 lines
drivers/iio/chemical/sgp30.c new file +10 -0
+		.modified = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+		.address = SGP30_IAQ_CO2EQ_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_ETHANOL,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),

Definitely looks like it'll fit in under 80 chars.

drivers/iio/chemical/sgp30.c line 166

Quoted 11 lines
drivers/iio/chemical/sgp30.c new file +11 -0
+		.address = SGP30_SIG_ETOH_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_H2,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),

Same here on length.

Quoted 11 lines
drivers/iio/chemical/sgp30.c new file +11 -0
+		.address = SGP30_SIG_H2_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+};
+
+static const struct iio_chan_spec sgpc3_channels[] = {
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_VOC,

drivers/iio/chemical/sgp30.c line 187

Quoted 10 lines
drivers/iio/chemical/sgp30.c new file +10 -0
+		.modified = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+		.address = SGPC3_IAQ_TVOC_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_ETHANOL,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),

And another on length.

Quoted 442 lines
drivers/iio/chemical/sgp30.c new file +20 -0
+		.address = SGPC3_SIG_ETOH_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+};
+
+static const struct sgp_device sgp_devices[] = {
+	[SGP30] = {
+		.channels = sgp30_channels,
+		.num_channels = ARRAY_SIZE(sgp30_channels),
+	},
+	[SGPC3] = {
+		.channels = sgpc3_channels,
+		.num_channels = ARRAY_SIZE(sgpc3_channels),
+	},
+};
+
+/**
+ * sgp_verify_buffer() - verify the checksums of the data buffer

words
+ *
+ * @data: SGP data
+ * @buf: Raw data buffer
+ * @word_count: Num data words stored in the buffer, excluding CRC bytes
+ *
+ * Return: 0 on success, negative error otherwise.
+ */

drivers/iio/chemical/sgp30.c new file +23 -0
+static int sgp_verify_buffer(const struct sgp_data *data,
+			     union sgp_reading *buf, size_t word_count)
+{
+	size_t size = word_count * (SGP_WORD_LEN + SGP_CRC8_LEN);
+	int i;
+	u8 crc;
+	u8 *data_buf = &buf->start;
+
+	for (i = 0; i < size; i += SGP_WORD_LEN + SGP_CRC8_LEN) {
+		crc = crc8(sgp_crc8_table, &data_buf[i], SGP_WORD_LEN,
+			   SGP_CRC8_INIT);
+		if (crc != data_buf[i + SGP_WORD_LEN]) {
+			dev_err(&data->client->dev, "CRC error\n");
+			return -EIO;
+		}
+	}
+
+	return 0;
+}
+
+/**
+ * sgp_read_cmd() - reads data from sensor after issuing a command
+ * The caller must hold data->data_lock for the duration of the

call.

+ * @data:        SGP data
+ * @cmd:         SGP Command to issue
+ * @buf:         Raw data buffer to use
+ * @word_count:  Num words to read, excluding CRC bytes

+ *
+ * Return: 0 on success, negative error otherwise.
+ */

drivers/iio/chemical/sgp30.c new file +30 -0
+static int sgp_read_cmd(struct sgp_data *data, enum sgp_cmd cmd,
+			union sgp_reading *buf, size_t word_count,
+			unsigned long duration_us)
+{
+	int ret;
+	struct i2c_client *client = data->client;
+	size_t size = word_count * (SGP_WORD_LEN + SGP_CRC8_LEN);
+	u8 *data_buf;
+
+	ret = i2c_master_send(client, (const char *)&cmd, SGP_CMD_LEN);
+	if (ret != SGP_CMD_LEN)
+		return -EIO;
+	usleep_range(duration_us, duration_us + 1000);
+
+	if (word_count == 0)
+		return 0;
+
+	data_buf = &buf->start;
+	ret = i2c_master_recv(client, data_buf, size);
+	if (ret < 0)
+		return ret;
+	if (ret != size)
+		return -EIO;
+
+	return sgp_verify_buffer(data, buf, word_count);
+}
+
+/**
+ * sgp_measure_iaq() - measure and retrieve IAQ values from sensor
+ * The caller must hold data->data_lock for the duration of the

call.
+ * @data: SGP data
+ *
+ * Return: 0 on success, -EBUSY on default values, negative
error
+ * otherwise.
+ */

drivers/iio/chemical/sgp30.c new file +6 -0
+
+static int sgp_measure_iaq(struct sgp_data *data)
+{
+	int ret;
+	/* data contains default values */
+	bool default_vals = !time_after(jiffies, data-
Quoted 1 line

iaq_init_start_jiffies +

drivers/iio/chemical/sgp30.c new file +1 -0
+						 data-
Quoted 1 line

iaq_defval_skip_jiffies);

drivers/iio/chemical/sgp30.c new file +2 -0
+
+	ret = sgp_read_cmd(data, data->measure_iaq_cmd, &data-
Quoted 1 line

iaq_buffer,

drivers/iio/chemical/sgp30.c new file +1 -0
+			   SGP_MEASUREMENT_LEN,

SGP_MEASUREMENT_DURATION_US);

drivers/iio/chemical/sgp30.c new file +14 -0
+	if (ret < 0)
+		return ret;
+
+	data->iaq_buffer_state = IAQ_BUFFER_DEFAULT_VALS;
+
+	if (default_vals)
+		return -EBUSY;
+
+	data->iaq_buffer_state = IAQ_BUFFER_VALID;
+
+	return 0;
+}
+
+static void sgp_iaq_thread_sleep_until(const struct sgp_data

*data,

drivers/iio/chemical/sgp30.c new file +7 -0
+				       unsigned long sleep_jiffies)
+{
+	const long IAQ_POLL = 50000;
+
+	while (!time_after(jiffies, sleep_jiffies)) {
+		usleep_range(IAQ_POLL, IAQ_POLL + 10000);
+		if (kthread_should_stop() || data-
Quoted 1 line

iaq_init_start_jiffies == 0)

drivers/iio/chemical/sgp30.c new file +14 -0
+			return;
+	}
+}
+
+static int sgp_iaq_threadfn(void *p)
+{
+	struct sgp_data *data = (struct sgp_data *)p;
+	unsigned long next_update_jiffies;
+	int ret;
+
+	while (!kthread_should_stop()) {
+		mutex_lock(&data->data_lock);
+		if (data->iaq_init_start_jiffies == 0) {
+			ret = sgp_read_cmd(data, data->iaq_init_cmd,

NULL, 0,

drivers/iio/chemical/sgp30.c new file +13 -0
+					   SGP_CMD_DURATION_US);
+			if (ret < 0)
+				goto unlock_sleep_continue;
+			data->iaq_init_start_jiffies = jiffies;
+		}
+
+		ret = sgp_measure_iaq(data);
+		if (ret && ret != -EBUSY) {
+			dev_warn(&data->client->dev,
+				 "IAQ measurement error [%d]\n", ret);
+		}
+unlock_sleep_continue:
+		next_update_jiffies = jiffies + data-
Quoted 1 line

measure_interval_jiffies;

drivers/iio/chemical/sgp30.c new file +51 -0
+		mutex_unlock(&data->data_lock);
+		sgp_iaq_thread_sleep_until(data, next_update_jiffies);
+	}
+
+	return 0;
+}
+
+static int sgp_read_raw(struct iio_dev *indio_dev,
+			struct iio_chan_spec const *chan, int *val,
+			int *val2, long mask)
+{
+	struct sgp_data *data = iio_priv(indio_dev);
+	struct sgp_crc_word *words;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
+		mutex_lock(&data->data_lock);
+		if (data->iaq_buffer_state != IAQ_BUFFER_VALID) {
+			mutex_unlock(&data->data_lock);
+			return -EBUSY;
+		}
+		words = data->iaq_buffer.raw_words;
+		switch (chan->address) {
+		case SGP30_IAQ_TVOC_IDX:
+		case SGPC3_IAQ_TVOC_IDX:
+			*val = 0;
+			*val2 = be16_to_cpu(words[1].value);
+			ret = IIO_VAL_INT_PLUS_NANO;
+			break;
+		case SGP30_IAQ_CO2EQ_IDX:
+			*val = 0;
+			*val2 = be16_to_cpu(words[0].value);
+			ret = IIO_VAL_INT_PLUS_MICRO;
+			break;
+		default:
+			ret = -EINVAL;
+			break;
+		}
+		mutex_unlock(&data->data_lock);
+		break;
+	case IIO_CHAN_INFO_RAW:
+		mutex_lock(&data->data_lock);
+		if (chan->address == SGPC3_SIG_ETOH_IDX) {
+			if (data->iaq_buffer_state == IAQ_BUFFER_EMPTY)
+				ret = -EBUSY;
+			else
+				ret = 0;
+			words = data->iaq_buffer.raw_words;
+		} else {
+			ret = sgp_read_cmd(data, data-
Quoted 1 line

measure_gas_signals_cmd,

drivers/iio/chemical/sgp30.c new file +1 -0
+					   &data->buffer,

SGP_MEASUREMENT_LEN,

drivers/iio/chemical/sgp30.c new file +1 -0
+					   SGP_MEASUREMENT_DURATION_US)

;

drivers/iio/chemical/sgp30.c new file +42 -0
+			words = data->buffer.raw_words;
+		}
+		if (ret) {
+			mutex_unlock(&data->data_lock);
+			return ret;
+		}
+
+		switch (chan->address) {
+		case SGP30_SIG_ETOH_IDX:
+			*val = be16_to_cpu(words[1].value);
+			ret = IIO_VAL_INT;
+			break;
+		case SGPC3_SIG_ETOH_IDX:
+		case SGP30_SIG_H2_IDX:
+			*val = be16_to_cpu(words[0].value);
+			ret = IIO_VAL_INT;
+			break;
+		default:
+			ret = -EINVAL;
+			break;
+		}
+		mutex_unlock(&data->data_lock);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return ret;
+}
+
+static int sgp_check_compat(struct sgp_data *data,
+			    unsigned int product_id)
+{
+	const struct sgp_version *supported_versions;
+	u16 ix, num_fs;
+	u16 product, generation, major, minor;
+
+	/* driver does not match product */
+	generation = SGP_VERS_GEN(data);
+	if (generation != 0) {
+		dev_err(&data->client->dev,
+			"incompatible product generation %d != 0",

generation);

drivers/iio/chemical/sgp30.c new file +7 -0
+		return -ENODEV;
+	}
+
+	product = SGP_VERS_PRODUCT(data);
+	if (product != product_id) {
+		dev_err(&data->client->dev,
+			"sensor reports a different product:

0x%04hx\n",

drivers/iio/chemical/sgp30.c new file +8 -0
+			product);
+		return -ENODEV;
+	}
+
+	if (SGP_VERS_RESERVED(data))
+		dev_warn(&data->client->dev, "reserved bit is set\n");
+
+	/* engineering samples are not supported: no interface

guarantees */

drivers/iio/chemical/sgp30.c new file +37 -0
+	if (SGP_VERS_ENG_BIT(data))
+		return -ENODEV;
+
+	switch (product) {
+	case SGP30:
+		supported_versions = supported_versions_sgp30;
+		num_fs = ARRAY_SIZE(supported_versions_sgp30);
+		break;
+	case SGPC3:
+		supported_versions = supported_versions_sgpc3;
+		num_fs = ARRAY_SIZE(supported_versions_sgpc3);
+		break;
+	default:
+		return -ENODEV;
+	}
+
+	major = SGP_VERS_MAJOR(data);
+	minor = SGP_VERS_MINOR(data);
+	for (ix = 0; ix < num_fs; ix++) {
+		if (major == supported_versions[ix].major &&
+		    minor >= supported_versions[ix].minor)
+			return 0;
+	}
+	dev_err(&data->client->dev, "unsupported sgp version: %d.%d\n",
+		major, minor);
+
+	return -ENODEV;
+}
+
+static void sgp_init(struct sgp_data *data)
+{
+	data->iaq_init_cmd = SGP_CMD_IAQ_INIT;
+	data->iaq_init_start_jiffies = 0;
+	data->iaq_buffer_state = IAQ_BUFFER_EMPTY;
+	switch (SGP_VERS_PRODUCT(data)) {
+	case SGP30:
+		data->measure_interval_jiffies =

SGP30_MEASURE_INTERVAL_HZ * HZ;

drivers/iio/chemical/sgp30.c new file +2 -0
+		data->measure_iaq_cmd = SGP_CMD_IAQ_MEASURE;
+		data->measure_gas_signals_cmd =

SGP30_CMD_MEASURE_SIGNAL;

drivers/iio/chemical/sgp30.c new file +5 -0
+		data->product_id = SGP30;
+		data->iaq_defval_skip_jiffies = 15 * HZ;
+		break;
+	case SGPC3:
+		data->measure_interval_jiffies =

SGPC3_MEASURE_INTERVAL_HZ * HZ;

drivers/iio/chemical/sgp30.c new file +46 -0
+		data->measure_iaq_cmd = SGPC3_CMD_MEASURE_RAW;
+		data->measure_gas_signals_cmd = SGPC3_CMD_MEASURE_RAW;
+		data->product_id = SGPC3;
+		data->iaq_defval_skip_jiffies =
+			43 * data->measure_interval_jiffies;
+		break;
+	};
+}
+
+static const struct iio_info sgp_info = {
+	.read_raw	= sgp_read_raw,
+};
+
+static const struct of_device_id sgp_dt_ids[] = {
+	{ .compatible = "sensirion,sgp30", .data = (void *)SGP30 },
+	{ .compatible = "sensirion,sgpc3", .data = (void *)SGPC3 },
+	{ }
+};
+
+static int sgp_probe(struct i2c_client *client,
+		     const struct i2c_device_id *id)
+{
+	struct iio_dev *indio_dev;
+	struct sgp_data *data;
+	const struct of_device_id *of_id;
+	unsigned long product_id;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	of_id = of_match_device(sgp_dt_ids, &client->dev);
+	if (of_id)
+		product_id = (unsigned long)of_id->data;
+	else
+		product_id = id->driver_data;
+
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	data->client = client;
+	crc8_populate_msb(sgp_crc8_table, SGP_CRC8_POLYNOMIAL);
+	mutex_init(&data->data_lock);
+
+	/* get feature set version and write it to client data */
+	ret = sgp_read_cmd(data, SGP_CMD_GET_FEATURE_SET, &data-
Quoted 1 line

buffer, 1,

drivers/iio/chemical/sgp30.c new file +5 -0
+			   SGP_CMD_DURATION_US);
+	if (ret < 0)
+		return ret;
+
+	data->feature_set = be16_to_cpu(data-
Quoted 1 line

buffer.raw_words[0].value);

drivers/iio/chemical/sgp30.c new file +17 -0
+
+	ret = sgp_check_compat(data, product_id);
+	if (ret)
+		return ret;
+
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->info = &sgp_info;
+	indio_dev->name = id->name;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = sgp_devices[product_id].channels;
+	indio_dev->num_channels = sgp_devices[product_id].num_channels;
+
+	sgp_init(data);
+
+	ret = devm_iio_device_register(&client->dev, indio_dev);
+	if (ret) {
+		dev_err(&client->dev, "failed to register iio

device\n");

drivers/iio/chemical/sgp30.c new file +41 -0
+		return ret;
+	}
+
+	data->iaq_thread = kthread_run(sgp_iaq_threadfn, data,
+				       "%s-iaq", data->client->name);
+
+	return 0;
+}
+
+static int sgp_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+	struct sgp_data *data = iio_priv(indio_dev);
+
+	if (data->iaq_thread)
+		kthread_stop(data->iaq_thread);
+
+	return 0;
+}
+
+static const struct i2c_device_id sgp_id[] = {
+	{ "sgp30", SGP30 },
+	{ "sgpc3", SGPC3 },
+	{ }
+};
+
+MODULE_DEVICE_TABLE(i2c, sgp_id);
+MODULE_DEVICE_TABLE(of, sgp_dt_ids);
+
+static struct i2c_driver sgp_driver = {
+	.driver = {
+		.name = "sgp30",
+		.of_match_table = of_match_ptr(sgp_dt_ids),
+	},
+	.probe = sgp_probe,
+	.remove = sgp_remove,
+	.id_table = sgp_id,
+};
+module_i2c_driver(sgp_driver);
+
+MODULE_AUTHOR("Andreas Brauchli <[email protected]>")

;

drivers/iio/chemical/sgp30.c new file +3 -0
+MODULE_AUTHOR("Pascal Sachs <[email protected]>");
+MODULE_DESCRIPTION("Sensirion SGP gas sensors");
+MODULE_LICENSE("GPL v2");
maintainer
#5 lore raw
Quoted 80 lines · On Tue, 18 Dec 2018 18:44:40 +0100 Tomasz Duszynski <[email protected]> wrote:
Quoted 66 lines · Jonathan Cameron
Quoted 58 lines · On Sun, 16 Dec 2018 17:38:18 +0100 Andreas Brauchli…
Quoted 19 lines · Jonathan Cameron
Quoted 16 lines · On Thu, 13 Dec 2018 15:43:23 +0100 Andreas Brauchli…

Support Sensirion SGP30 and SGPC3 multi-pixel I2C gas sensors

Supported Features:

* Indoor Air Quality (IAQ) concentrations for
- tVOC (in_concentration_voc_input)
- CO2eq (in_concentration_co2_input) - SGP30 only

IAQ concentrations are periodically read out by a background
thread
to allow the sensor to maintain its internal baseline.

* Gas concentration signals
- Ethanol (in_concentration_ethanol_raw)
- H2 (in_concentration_h2_raw) - SGP30 only

Quoted 7 lines
Quoted 4 lines

https://www.sensirion.com/file/datasheet_sgp30
https://www.sensirion.com/file/datasheet_sgpc3

Why do we provide endianness info given we don't provide the buffered interface? It's only used when we do.

Makes sense but I wasn't aware of it. The initial submission came with buffering, so it probably wasn't spotted in v2.

Quoted 4 lines

Some over the top line wrapping as well. However as that's it I'll just clean up whilst applying. If I've missed something odd around the endian thing shout!

No that's it, thanks a lot.

Quoted 20 lines

Jonathan

Quoted 17 lines
---
 drivers/iio/chemical/Kconfig  |  13 +
 drivers/iio/chemical/Makefile |   1 +
 drivers/iio/chemical/sgp30.c  | 603
++++++++++++++++++++++++++++++++++
 3 files changed, 617 insertions(+)
 create mode 100644 drivers/iio/chemical/sgp30.c
drivers/iio/chemical/Kconfig +0 -0
index b8e005be4f87..b0c912be94a9 100644
@@ -61,6 +61,19 @@ config IAQCORE
 	  iAQ-Core Continuous/Pulsed VOC (Volatile Organic Compounds)
 	  sensors
 
Quoted 4 lines
Quoted 1 line

drivers/iio/chemical/Kconfig config IAQCORE · line 64

Quoted 1 line
drivers/iio/chemical/Kconfig +1 -0
+config SENSIRION_SGP30  

I noticed that in Tomasz's SPS30 patch it's just SPS30, so for consistency's sake I'm fine with dropping the branding. Up to you.

Quoted 6 lines

We've never had any hard rules on this, at least partly because a lot of drivers came from other subsystems in the early days.

We probably should have a rule though. SGP30 for example is generic enough that it might well be reused by someone else.

Wondering how likely is this? These days some popular sensors have their clones. Interestingly enough, they are sold as genuine i.e without vendor/sensor name changes. On the other hand there are very same ICs manufactured by different companies but still with different names.

Quoted 7 lines
Quoted 3 lines

Anyone have any strong opinions on this? If not I think I'll start encouraging prefixing with manufacturer of first supported device.

What about sticking to what we have now and adding prefix when we do have naming collision? Just my 2 cents.

Agreed in the sense that it's not worth changing existing drivers unless we have a reason to..

But if the question is raised for what is preferred, I think adding a vendor is good. There have been clashes with i2c devices in the past where the binding without vendor was insufficient to distinguish the part.

Not sure what it was, but I remember the discussion being one of the motivations to add vendor prefixes to the look up for i2c devices.

Jonathan

Quoted 685 lines
Quoted 685 lines

Jonathan

Quoted 681 lines
Quoted 680 lines
Quoted 186 lines
drivers/iio/chemical/Kconfig +12 -0
+	tristate "Sensirion SGPxx gas sensors"
+	depends on I2C
+	select CRC8
+	help
+	  Say Y here to build I2C interface support for the following
+	  Sensirion SGP gas sensors:
+	    * SGP30 gas sensor
+	    * SGPC3 low power gas sensor
+
+	  To compile this driver as module, choose M here: the
+	  module will be called sgp30.
+
 config VZ89X
 	tristate "SGX Sensortech MiCS VZ89X VOC sensor"
 	depends on I2C
drivers/iio/chemical/Makefile +1 -0
index 2f4c4ba4d781..61e83944db1a 100644
@@ -9,4 +9,5 @@ obj-$(CONFIG_BME680_I2C) += bme680_i2c.o
 obj-$(CONFIG_BME680_SPI) += bme680_spi.o
 obj-$(CONFIG_CCS811)		+= ccs811.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
+obj-$(CONFIG_SENSIRION_SGP30)	+= sgp30.o
 obj-$(CONFIG_VZ89X)		+= vz89x.o
drivers/iio/chemical/sgp30.c new file +5 -0
new file mode 100644
index 000000000000..90eb4cbb18cf
@@ -0,0 +1,603 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * sgp30.c - Support for Sensirion SGP Gas Sensors
+ *
+ * Copyright (C) 2018 Andreas Brauchli <  

[email protected]>
+ *
+ * I2C slave address: 0x58
+ *
+ * Datasheets:
+ * https://www.sensirion.com/file/datasheet_sgp30
+ * https://www.sensirion.com/file/datasheet_sgpc3
+ *
+ * TODO:
+ * - baseline support
+ * - humidity compensation
+ * - power mode switching (SGPC3)
+ */

drivers/iio/chemical/sgp30.c new file +20 -0
+
+#include <linux/crc8.h>
+#include <linux/delay.h>
+#include <linux/kthread.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/i2c.h>
+#include <linux/of_device.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+#define SGP_WORD_LEN				2
+#define SGP_CRC8_POLYNOMIAL			0x31
+#define SGP_CRC8_INIT				0xff
+#define SGP_CRC8_LEN				1
+#define SGP_CMD(cmd_word)			cpu_to_be16(cmd_word)
+#define SGP_CMD_DURATION_US			12000
+#define SGP_MEASUREMENT_DURATION_US		50000
+#define SGP_CMD_LEN				SGP_WORD_LEN
+#define SGP_CMD_MAX_BUF_SIZE			(SGP_CMD_LEN +

2 * SGP_WORD_LEN)

drivers/iio/chemical/sgp30.c new file +4 -0
+#define SGP_MEASUREMENT_LEN			2
+#define SGP30_MEASURE_INTERVAL_HZ		1
+#define SGPC3_MEASURE_INTERVAL_HZ		2
+#define SGP_VERS_PRODUCT(data)	((((data)->feature_set) &

0xf000) >> 12)
+#define SGP_VERS_RESERVED(data) ((((data)->feature_set) &
0x0800) >> 11)

drivers/iio/chemical/sgp30.c new file +2 -0
+#define SGP_VERS_GEN(data)	((((data)->feature_set) & 0x0600) >> 9)
+#define SGP_VERS_ENG_BIT(data)	((((data)->feature_set) &

0x0100) >> 8)
+#define SGP_VERS_MAJOR(data) ((((data)->feature_set) &
0x00e0) >> 5)
+#define SGP_VERS_MINOR(data) (((data)->feature_set) &
0x001f)

drivers/iio/chemical/sgp30.c new file +99 -0
+
+DECLARE_CRC8_TABLE(sgp_crc8_table);
+
+enum sgp_product_id {
+	SGP30 = 0,
+	SGPC3,
+};
+
+enum sgp30_channel_idx {
+	SGP30_IAQ_TVOC_IDX = 0,
+	SGP30_IAQ_CO2EQ_IDX,
+	SGP30_SIG_ETOH_IDX,
+	SGP30_SIG_H2_IDX,
+};
+
+enum sgpc3_channel_idx {
+	SGPC3_IAQ_TVOC_IDX = 10,
+	SGPC3_SIG_ETOH_IDX,
+};
+
+enum sgp_cmd {
+	SGP_CMD_IAQ_INIT			= SGP_CMD(0x2003),
+	SGP_CMD_IAQ_MEASURE			= SGP_CMD(0x2008),
+	SGP_CMD_GET_FEATURE_SET			= SGP_CMD(0x202f),
+	SGP_CMD_GET_SERIAL_ID			= SGP_CMD(0x3682),
+
+	SGP30_CMD_MEASURE_SIGNAL		= SGP_CMD(0x2050),
+
+	SGPC3_CMD_MEASURE_RAW			= SGP_CMD(0x2046),
+};
+
+struct sgp_version {
+	u8 major;
+	u8 minor;
+};
+
+struct sgp_crc_word {
+	__be16 value;
+	u8 crc8;
+} __attribute__((__packed__));
+
+union sgp_reading {
+	u8 start;
+	struct sgp_crc_word raw_words[4];
+};
+
+enum _iaq_buffer_state {
+	IAQ_BUFFER_EMPTY = 0,
+	IAQ_BUFFER_DEFAULT_VALS,
+	IAQ_BUFFER_VALID,
+};
+
+struct sgp_data {
+	struct i2c_client *client;
+	struct task_struct *iaq_thread;
+	struct mutex data_lock;
+	unsigned long iaq_init_start_jiffies;
+	unsigned long iaq_defval_skip_jiffies;
+	u16 product_id;
+	u16 feature_set;
+	unsigned long measure_interval_jiffies;
+	enum sgp_cmd iaq_init_cmd;
+	enum sgp_cmd measure_iaq_cmd;
+	enum sgp_cmd measure_gas_signals_cmd;
+	union sgp_reading buffer;
+	union sgp_reading iaq_buffer;
+	enum _iaq_buffer_state iaq_buffer_state;
+};
+
+struct sgp_device {
+	const struct iio_chan_spec *channels;
+	int num_channels;
+};
+
+static const struct sgp_version supported_versions_sgp30[] = {
+	{
+		.major = 1,
+		.minor = 0,
+	},
+};
+
+static const struct sgp_version supported_versions_sgpc3[] = {
+	{
+		.major = 0,
+		.minor = 4,
+	},
+};
+
+static const struct iio_chan_spec sgp30_channels[] = {
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_VOC,
+		.modified = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+		.address = SGP30_IAQ_TVOC_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_CO2,

drivers/iio/chemical/sgp30.c line 155

Quoted 10 lines
drivers/iio/chemical/sgp30.c new file +10 -0
+		.modified = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+		.address = SGP30_IAQ_CO2EQ_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_ETHANOL,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),  

Definitely looks like it'll fit in under 80 chars.

drivers/iio/chemical/sgp30.c line 166

Quoted 11 lines
drivers/iio/chemical/sgp30.c new file +11 -0
+		.address = SGP30_SIG_ETOH_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_H2,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),  

Same here on length.

Quoted 11 lines
drivers/iio/chemical/sgp30.c new file +11 -0
+		.address = SGP30_SIG_H2_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+};
+
+static const struct iio_chan_spec sgpc3_channels[] = {
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_VOC,

drivers/iio/chemical/sgp30.c line 187

Quoted 10 lines
drivers/iio/chemical/sgp30.c new file +10 -0
+		.modified = 1,
+		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
+		.address = SGPC3_IAQ_TVOC_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_ETHANOL,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),  

And another on length.

Quoted 442 lines
drivers/iio/chemical/sgp30.c new file +20 -0
+		.address = SGPC3_SIG_ETOH_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+};
+
+static const struct sgp_device sgp_devices[] = {
+	[SGP30] = {
+		.channels = sgp30_channels,
+		.num_channels = ARRAY_SIZE(sgp30_channels),
+	},
+	[SGPC3] = {
+		.channels = sgpc3_channels,
+		.num_channels = ARRAY_SIZE(sgpc3_channels),
+	},
+};
+
+/**
+ * sgp_verify_buffer() - verify the checksums of the data buffer

words
+ *
+ * @data: SGP data
+ * @buf: Raw data buffer
+ * @word_count: Num data words stored in the buffer, excluding CRC bytes
+ *
+ * Return: 0 on success, negative error otherwise.
+ */

drivers/iio/chemical/sgp30.c new file +23 -0
+static int sgp_verify_buffer(const struct sgp_data *data,
+			     union sgp_reading *buf, size_t word_count)
+{
+	size_t size = word_count * (SGP_WORD_LEN + SGP_CRC8_LEN);
+	int i;
+	u8 crc;
+	u8 *data_buf = &buf->start;
+
+	for (i = 0; i < size; i += SGP_WORD_LEN + SGP_CRC8_LEN) {
+		crc = crc8(sgp_crc8_table, &data_buf[i], SGP_WORD_LEN,
+			   SGP_CRC8_INIT);
+		if (crc != data_buf[i + SGP_WORD_LEN]) {
+			dev_err(&data->client->dev, "CRC error\n");
+			return -EIO;
+		}
+	}
+
+	return 0;
+}
+
+/**
+ * sgp_read_cmd() - reads data from sensor after issuing a command
+ * The caller must hold data->data_lock for the duration of the

call.

+ * @data:        SGP data
+ * @cmd:         SGP Command to issue
+ * @buf:         Raw data buffer to use
+ * @word_count:  Num words to read, excluding CRC bytes

+ *
+ * Return: 0 on success, negative error otherwise.
+ */

drivers/iio/chemical/sgp30.c new file +30 -0
+static int sgp_read_cmd(struct sgp_data *data, enum sgp_cmd cmd,
+			union sgp_reading *buf, size_t word_count,
+			unsigned long duration_us)
+{
+	int ret;
+	struct i2c_client *client = data->client;
+	size_t size = word_count * (SGP_WORD_LEN + SGP_CRC8_LEN);
+	u8 *data_buf;
+
+	ret = i2c_master_send(client, (const char *)&cmd, SGP_CMD_LEN);
+	if (ret != SGP_CMD_LEN)
+		return -EIO;
+	usleep_range(duration_us, duration_us + 1000);
+
+	if (word_count == 0)
+		return 0;
+
+	data_buf = &buf->start;
+	ret = i2c_master_recv(client, data_buf, size);
+	if (ret < 0)
+		return ret;
+	if (ret != size)
+		return -EIO;
+
+	return sgp_verify_buffer(data, buf, word_count);
+}
+
+/**
+ * sgp_measure_iaq() - measure and retrieve IAQ values from sensor
+ * The caller must hold data->data_lock for the duration of the

call.
+ * @data: SGP data
+ *
+ * Return: 0 on success, -EBUSY on default values, negative
error
+ * otherwise.
+ */

drivers/iio/chemical/sgp30.c new file +6 -0
+
+static int sgp_measure_iaq(struct sgp_data *data)
+{
+	int ret;
+	/* data contains default values */
+	bool default_vals = !time_after(jiffies, data-  
Quoted 1 line

iaq_init_start_jiffies +

drivers/iio/chemical/sgp30.c new file +1 -0
+						 data-  
Quoted 1 line

iaq_defval_skip_jiffies);

drivers/iio/chemical/sgp30.c new file +2 -0
+
+	ret = sgp_read_cmd(data, data->measure_iaq_cmd, &data-  
Quoted 1 line

iaq_buffer,

drivers/iio/chemical/sgp30.c new file +1 -0
+			   SGP_MEASUREMENT_LEN,

SGP_MEASUREMENT_DURATION_US);

drivers/iio/chemical/sgp30.c new file +14 -0
+	if (ret < 0)
+		return ret;
+
+	data->iaq_buffer_state = IAQ_BUFFER_DEFAULT_VALS;
+
+	if (default_vals)
+		return -EBUSY;
+
+	data->iaq_buffer_state = IAQ_BUFFER_VALID;
+
+	return 0;
+}
+
+static void sgp_iaq_thread_sleep_until(const struct sgp_data

*data,

drivers/iio/chemical/sgp30.c new file +7 -0
+				       unsigned long sleep_jiffies)
+{
+	const long IAQ_POLL = 50000;
+
+	while (!time_after(jiffies, sleep_jiffies)) {
+		usleep_range(IAQ_POLL, IAQ_POLL + 10000);
+		if (kthread_should_stop() || data-  
Quoted 1 line

iaq_init_start_jiffies == 0)

drivers/iio/chemical/sgp30.c new file +14 -0
+			return;
+	}
+}
+
+static int sgp_iaq_threadfn(void *p)
+{
+	struct sgp_data *data = (struct sgp_data *)p;
+	unsigned long next_update_jiffies;
+	int ret;
+
+	while (!kthread_should_stop()) {
+		mutex_lock(&data->data_lock);
+		if (data->iaq_init_start_jiffies == 0) {
+			ret = sgp_read_cmd(data, data->iaq_init_cmd,

NULL, 0,

drivers/iio/chemical/sgp30.c new file +13 -0
+					   SGP_CMD_DURATION_US);
+			if (ret < 0)
+				goto unlock_sleep_continue;
+			data->iaq_init_start_jiffies = jiffies;
+		}
+
+		ret = sgp_measure_iaq(data);
+		if (ret && ret != -EBUSY) {
+			dev_warn(&data->client->dev,
+				 "IAQ measurement error [%d]\n", ret);
+		}
+unlock_sleep_continue:
+		next_update_jiffies = jiffies + data-  
Quoted 1 line

measure_interval_jiffies;

drivers/iio/chemical/sgp30.c new file +51 -0
+		mutex_unlock(&data->data_lock);
+		sgp_iaq_thread_sleep_until(data, next_update_jiffies);
+	}
+
+	return 0;
+}
+
+static int sgp_read_raw(struct iio_dev *indio_dev,
+			struct iio_chan_spec const *chan, int *val,
+			int *val2, long mask)
+{
+	struct sgp_data *data = iio_priv(indio_dev);
+	struct sgp_crc_word *words;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
+		mutex_lock(&data->data_lock);
+		if (data->iaq_buffer_state != IAQ_BUFFER_VALID) {
+			mutex_unlock(&data->data_lock);
+			return -EBUSY;
+		}
+		words = data->iaq_buffer.raw_words;
+		switch (chan->address) {
+		case SGP30_IAQ_TVOC_IDX:
+		case SGPC3_IAQ_TVOC_IDX:
+			*val = 0;
+			*val2 = be16_to_cpu(words[1].value);
+			ret = IIO_VAL_INT_PLUS_NANO;
+			break;
+		case SGP30_IAQ_CO2EQ_IDX:
+			*val = 0;
+			*val2 = be16_to_cpu(words[0].value);
+			ret = IIO_VAL_INT_PLUS_MICRO;
+			break;
+		default:
+			ret = -EINVAL;
+			break;
+		}
+		mutex_unlock(&data->data_lock);
+		break;
+	case IIO_CHAN_INFO_RAW:
+		mutex_lock(&data->data_lock);
+		if (chan->address == SGPC3_SIG_ETOH_IDX) {
+			if (data->iaq_buffer_state == IAQ_BUFFER_EMPTY)
+				ret = -EBUSY;
+			else
+				ret = 0;
+			words = data->iaq_buffer.raw_words;
+		} else {
+			ret = sgp_read_cmd(data, data-  
Quoted 1 line

measure_gas_signals_cmd,

drivers/iio/chemical/sgp30.c new file +1 -0
+					   &data->buffer,

SGP_MEASUREMENT_LEN,

drivers/iio/chemical/sgp30.c new file +1 -0
+					   SGP_MEASUREMENT_DURATION_US)

;

drivers/iio/chemical/sgp30.c new file +42 -0
+			words = data->buffer.raw_words;
+		}
+		if (ret) {
+			mutex_unlock(&data->data_lock);
+			return ret;
+		}
+
+		switch (chan->address) {
+		case SGP30_SIG_ETOH_IDX:
+			*val = be16_to_cpu(words[1].value);
+			ret = IIO_VAL_INT;
+			break;
+		case SGPC3_SIG_ETOH_IDX:
+		case SGP30_SIG_H2_IDX:
+			*val = be16_to_cpu(words[0].value);
+			ret = IIO_VAL_INT;
+			break;
+		default:
+			ret = -EINVAL;
+			break;
+		}
+		mutex_unlock(&data->data_lock);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return ret;
+}
+
+static int sgp_check_compat(struct sgp_data *data,
+			    unsigned int product_id)
+{
+	const struct sgp_version *supported_versions;
+	u16 ix, num_fs;
+	u16 product, generation, major, minor;
+
+	/* driver does not match product */
+	generation = SGP_VERS_GEN(data);
+	if (generation != 0) {
+		dev_err(&data->client->dev,
+			"incompatible product generation %d != 0",

generation);

drivers/iio/chemical/sgp30.c new file +7 -0
+		return -ENODEV;
+	}
+
+	product = SGP_VERS_PRODUCT(data);
+	if (product != product_id) {
+		dev_err(&data->client->dev,
+			"sensor reports a different product:

0x%04hx\n",

drivers/iio/chemical/sgp30.c new file +8 -0
+			product);
+		return -ENODEV;
+	}
+
+	if (SGP_VERS_RESERVED(data))
+		dev_warn(&data->client->dev, "reserved bit is set\n");
+
+	/* engineering samples are not supported: no interface

guarantees */

drivers/iio/chemical/sgp30.c new file +37 -0
+	if (SGP_VERS_ENG_BIT(data))
+		return -ENODEV;
+
+	switch (product) {
+	case SGP30:
+		supported_versions = supported_versions_sgp30;
+		num_fs = ARRAY_SIZE(supported_versions_sgp30);
+		break;
+	case SGPC3:
+		supported_versions = supported_versions_sgpc3;
+		num_fs = ARRAY_SIZE(supported_versions_sgpc3);
+		break;
+	default:
+		return -ENODEV;
+	}
+
+	major = SGP_VERS_MAJOR(data);
+	minor = SGP_VERS_MINOR(data);
+	for (ix = 0; ix < num_fs; ix++) {
+		if (major == supported_versions[ix].major &&
+		    minor >= supported_versions[ix].minor)
+			return 0;
+	}
+	dev_err(&data->client->dev, "unsupported sgp version: %d.%d\n",
+		major, minor);
+
+	return -ENODEV;
+}
+
+static void sgp_init(struct sgp_data *data)
+{
+	data->iaq_init_cmd = SGP_CMD_IAQ_INIT;
+	data->iaq_init_start_jiffies = 0;
+	data->iaq_buffer_state = IAQ_BUFFER_EMPTY;
+	switch (SGP_VERS_PRODUCT(data)) {
+	case SGP30:
+		data->measure_interval_jiffies =

SGP30_MEASURE_INTERVAL_HZ * HZ;

drivers/iio/chemical/sgp30.c new file +2 -0
+		data->measure_iaq_cmd = SGP_CMD_IAQ_MEASURE;
+		data->measure_gas_signals_cmd =

SGP30_CMD_MEASURE_SIGNAL;

drivers/iio/chemical/sgp30.c new file +5 -0
+		data->product_id = SGP30;
+		data->iaq_defval_skip_jiffies = 15 * HZ;
+		break;
+	case SGPC3:
+		data->measure_interval_jiffies =

SGPC3_MEASURE_INTERVAL_HZ * HZ;

drivers/iio/chemical/sgp30.c new file +46 -0
+		data->measure_iaq_cmd = SGPC3_CMD_MEASURE_RAW;
+		data->measure_gas_signals_cmd = SGPC3_CMD_MEASURE_RAW;
+		data->product_id = SGPC3;
+		data->iaq_defval_skip_jiffies =
+			43 * data->measure_interval_jiffies;
+		break;
+	};
+}
+
+static const struct iio_info sgp_info = {
+	.read_raw	= sgp_read_raw,
+};
+
+static const struct of_device_id sgp_dt_ids[] = {
+	{ .compatible = "sensirion,sgp30", .data = (void *)SGP30 },
+	{ .compatible = "sensirion,sgpc3", .data = (void *)SGPC3 },
+	{ }
+};
+
+static int sgp_probe(struct i2c_client *client,
+		     const struct i2c_device_id *id)
+{
+	struct iio_dev *indio_dev;
+	struct sgp_data *data;
+	const struct of_device_id *of_id;
+	unsigned long product_id;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
+	if (!indio_dev)
+		return -ENOMEM;
+
+	of_id = of_match_device(sgp_dt_ids, &client->dev);
+	if (of_id)
+		product_id = (unsigned long)of_id->data;
+	else
+		product_id = id->driver_data;
+
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	data->client = client;
+	crc8_populate_msb(sgp_crc8_table, SGP_CRC8_POLYNOMIAL);
+	mutex_init(&data->data_lock);
+
+	/* get feature set version and write it to client data */
+	ret = sgp_read_cmd(data, SGP_CMD_GET_FEATURE_SET, &data-  
Quoted 1 line

buffer, 1,

drivers/iio/chemical/sgp30.c new file +5 -0
+			   SGP_CMD_DURATION_US);
+	if (ret < 0)
+		return ret;
+
+	data->feature_set = be16_to_cpu(data-  
Quoted 1 line

buffer.raw_words[0].value);

drivers/iio/chemical/sgp30.c new file +17 -0
+
+	ret = sgp_check_compat(data, product_id);
+	if (ret)
+		return ret;
+
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->info = &sgp_info;
+	indio_dev->name = id->name;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = sgp_devices[product_id].channels;
+	indio_dev->num_channels = sgp_devices[product_id].num_channels;
+
+	sgp_init(data);
+
+	ret = devm_iio_device_register(&client->dev, indio_dev);
+	if (ret) {
+		dev_err(&client->dev, "failed to register iio

device\n");

drivers/iio/chemical/sgp30.c new file +41 -0
+		return ret;
+	}
+
+	data->iaq_thread = kthread_run(sgp_iaq_threadfn, data,
+				       "%s-iaq", data->client->name);
+
+	return 0;
+}
+
+static int sgp_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+	struct sgp_data *data = iio_priv(indio_dev);
+
+	if (data->iaq_thread)
+		kthread_stop(data->iaq_thread);
+
+	return 0;
+}
+
+static const struct i2c_device_id sgp_id[] = {
+	{ "sgp30", SGP30 },
+	{ "sgpc3", SGPC3 },
+	{ }
+};
+
+MODULE_DEVICE_TABLE(i2c, sgp_id);
+MODULE_DEVICE_TABLE(of, sgp_dt_ids);
+
+static struct i2c_driver sgp_driver = {
+	.driver = {
+		.name = "sgp30",
+		.of_match_table = of_match_ptr(sgp_dt_ids),
+	},
+	.probe = sgp_probe,
+	.remove = sgp_remove,
+	.id_table = sgp_id,
+};
+module_i2c_driver(sgp_driver);
+
+MODULE_AUTHOR("Andreas Brauchli <[email protected]>")

;

drivers/iio/chemical/sgp30.c new file +3 -0
+MODULE_AUTHOR("Pascal Sachs <[email protected]>");
+MODULE_DESCRIPTION("Sensirion SGP gas sensors");
+MODULE_LICENSE("GPL v2");  
#6 lore raw
Quoted 50 lines · Jonathan Cameron
Quoted 47 lines · On Tue, 18 Dec 2018 18:44:40 +0100 Tomasz Duszynski <[email protected]> wrote:
Quoted 46 lines · Jonathan Cameron
Quoted 43 lines · On Sun, 16 Dec 2018 17:38:18 +0100 Andreas Brauchli…
Quoted 24 lines · Jonathan Cameron
Quoted 21 lines · On Thu, 13 Dec 2018 15:43:23 +0100 Andreas Brauchli…

Support Sensirion SGP30 and SGPC3 multi-pixel I2C gas sensors

Supported Features:

* Indoor Air Quality (IAQ) concentrations for
- tVOC (in_concentration_voc_input)
- CO2eq (in_concentration_co2_input) - SGP30 only

IAQ concentrations are periodically read out by a
background
thread
to allow the sensor to maintain its internal baseline.

* Gas concentration signals
- Ethanol (in_concentration_ethanol_raw)
- H2 (in_concentration_h2_raw) - SGP30 only

https://www.sensirion.com/file/datasheet_sgp30
https://www.sensirion.com/file/datasheet_sgpc3

Quoted 6 lines
Quoted 2 lines

Signed-off-by: Andreas Brauchli <
[email protected]>

Why do we provide endianness info given we don't provide the buffered
interface? It's only used when we do.

Makes sense but I wasn't aware of it. The initial submission came with
buffering, so it probably wasn't spotted in v2.

Quoted 6 lines

Some over the top line wrapping as well. However as that's it
I'll just clean up whilst applying. If I've missed something odd
around the endian thing shout!

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

No that's it, thanks a lot.

Quoted 5 lines

Jonathan

Quoted 2 lines
---
 drivers/iio/chemical/Kconfig  |  13 +

Apologies for noticing so late, but I fear that the Kconfig part might have been lost.

It's not in the upstream commit
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?h=v5.0&id=ce514124161ac2ceb13d10b6c40cbf05c8f0cc91
nor in the testing branch (togreg is already removed)
https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git/tree/drivers/iio/chemical/Kconfig?h=testing

Quoted 821 lines
Quoted 45 lines
Quoted 22 lines
Quoted 16 lines
Quoted 16 lines
Quoted 16 lines
 drivers/iio/chemical/Makefile |   1 +
 drivers/iio/chemical/sgp30.c  | 603
++++++++++++++++++++++++++++++++++
 3 files changed, 617 insertions(+)
 create mode 100644 drivers/iio/chemical/sgp30.c
drivers/iio/chemical/Kconfig +0 -0
index b8e005be4f87..b0c912be94a9 100644
@@ -61,6 +61,19 @@ config IAQCORE
 	  iAQ-Core Continuous/Pulsed VOC (Volatile Organic
 Compounds)
 	  sensors
 
Quoted 5 lines
Quoted 1 line

drivers/iio/chemical/Kconfig config IAQCORE · line 64

Quoted 1 line
drivers/iio/chemical/Kconfig +1 -0
+config SENSIRION_SGP30  

I noticed that in Tomasz's SPS30 patch it's just SPS30, so for consistency's sake I'm fine with dropping the branding. Up to you.

Quoted 8 lines

We've never had any hard rules on this, at least partly because a lot
of drivers came from other subsystems in the early days.

We probably should have a rule though. SGP30 for example is generic
enough that it might well be reused by someone else.

Wondering how likely is this? These days some popular sensors have their
clones. Interestingly enough, they are sold as genuine i.e without vendor/sensor name changes. On the other hand there are very
same ICs manufactured by different companies but still with different names.

Quoted 5 lines

Anyone have any strong opinions on this? If not I think I'll start
encouraging prefixing with manufacturer of first supported device.

Quoted 4 lines

What about sticking to what we have now and adding prefix when we do
have naming collision? Just my 2 cents.

Agreed in the sense that it's not worth changing existing drivers unless
we have a reason to..

But if the question is raised for what is preferred, I think adding a vendor
is good. There have been clashes with i2c devices in the past where the
binding without vendor was insufficient to distinguish the part.

Not sure what it was, but I remember the discussion being one of the motivations
to add vendor prefixes to the look up for i2c devices.

Jonathan

Quoted 753 lines
Quoted 753 lines

Jonathan

Quoted 749 lines
Quoted 747 lines
Quoted 205 lines
drivers/iio/chemical/Kconfig +5 -0
+	tristate "Sensirion SGPxx gas sensors"
+	depends on I2C
+	select CRC8
+	help
+	  Say Y here to build I2C interface support for the

following

drivers/iio/chemical/Kconfig +7 -0
+	  Sensirion SGP gas sensors:
+	    * SGP30 gas sensor
+	    * SGPC3 low power gas sensor
+
+	  To compile this driver as module, choose M here: the
+	  module will be called sgp30.
+
 config VZ89X
 	tristate "SGX Sensortech MiCS VZ89X VOC sensor"
 	depends on I2C
drivers/iio/chemical/Makefile +1 -0
index 2f4c4ba4d781..61e83944db1a 100644
@@ -9,4 +9,5 @@ obj-$(CONFIG_BME680_I2C) += bme680_i2c.o
 obj-$(CONFIG_BME680_SPI) += bme680_spi.o
 obj-$(CONFIG_CCS811)		+= ccs811.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
+obj-$(CONFIG_SENSIRION_SGP30)	+= sgp30.o
 obj-$(CONFIG_VZ89X)		+= vz89x.o
drivers/iio/chemical/sgp30.c new file +5 -0
new file mode 100644
index 000000000000..90eb4cbb18cf
@@ -0,0 +1,603 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * sgp30.c - Support for Sensirion SGP Gas Sensors
+ *
+ * Copyright (C) 2018 Andreas Brauchli <  

[email protected]>
+ *
+ * I2C slave address: 0x58
+ *
+ * Datasheets:
+ * https://www.sensirion.com/file/datasheet_sgp30
+ * https://www.sensirion.com/file/datasheet_sgpc3
+ *
+ * TODO:
+ * - baseline support
+ * - humidity compensation
+ * - power mode switching (SGPC3)
+ */

drivers/iio/chemical/sgp30.c new file +16 -0
+
+#include <linux/crc8.h>
+#include <linux/delay.h>
+#include <linux/kthread.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/i2c.h>
+#include <linux/of_device.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+#define SGP_WORD_LEN				2
+#define SGP_CRC8_POLYNOMIAL			0x31
+#define SGP_CRC8_INIT				0xff
+#define SGP_CRC8_LEN				1
+#define SGP_CMD(cmd_word)			cpu_to_be16(cmd

_word)

drivers/iio/chemical/sgp30.c new file +3 -0
+#define SGP_CMD_DURATION_US			12000
+#define SGP_MEASUREMENT_DURATION_US		50000
+#define SGP_CMD_LEN				SGP_WOR

D_LEN
+#define SGP_CMD_MAX_BUF_SIZE (SGP_CM
D_LEN +
2 * SGP_WORD_LEN)

drivers/iio/chemical/sgp30.c new file +4 -0
+#define SGP_MEASUREMENT_LEN			2
+#define SGP30_MEASURE_INTERVAL_HZ		1
+#define SGPC3_MEASURE_INTERVAL_HZ		2
+#define SGP_VERS_PRODUCT(data)	((((data)->feature_set) 

&
0xf000) >> 12)
+#define SGP_VERS_RESERVED(data) ((((data)->feature_set)
&
0x0800) >> 11)
+#define SGP_VERS_GEN(data) ((((data)->feature_set) &
0x0600) >> 9)
+#define SGP_VERS_ENG_BIT(data) ((((data)->feature_set)
&
0x0100) >> 8)
+#define SGP_VERS_MAJOR(data) ((((data)->feature_set)
&
0x00e0) >> 5)
+#define SGP_VERS_MINOR(data) (((data)->feature_set)
&
0x001f)

drivers/iio/chemical/sgp30.c new file +24 -0
+
+DECLARE_CRC8_TABLE(sgp_crc8_table);
+
+enum sgp_product_id {
+	SGP30 = 0,
+	SGPC3,
+};
+
+enum sgp30_channel_idx {
+	SGP30_IAQ_TVOC_IDX = 0,
+	SGP30_IAQ_CO2EQ_IDX,
+	SGP30_SIG_ETOH_IDX,
+	SGP30_SIG_H2_IDX,
+};
+
+enum sgpc3_channel_idx {
+	SGPC3_IAQ_TVOC_IDX = 10,
+	SGPC3_SIG_ETOH_IDX,
+};
+
+enum sgp_cmd {
+	SGP_CMD_IAQ_INIT			= SGP_CMD(0x2003),
+	SGP_CMD_IAQ_MEASURE			= SGP_CMD(0x2008),
+	SGP_CMD_GET_FEATURE_SET			=

SGP_CMD(0x202f),

drivers/iio/chemical/sgp30.c new file +1 -0
+	SGP_CMD_GET_SERIAL_ID			=

SGP_CMD(0x3682),

drivers/iio/chemical/sgp30.c new file +4 -0
+
+	SGP30_CMD_MEASURE_SIGNAL		= SGP_CMD(0x2050),
+
+	SGPC3_CMD_MEASURE_RAW			=

SGP_CMD(0x2046),

drivers/iio/chemical/sgp30.c new file +46 -0
+};
+
+struct sgp_version {
+	u8 major;
+	u8 minor;
+};
+
+struct sgp_crc_word {
+	__be16 value;
+	u8 crc8;
+} __attribute__((__packed__));
+
+union sgp_reading {
+	u8 start;
+	struct sgp_crc_word raw_words[4];
+};
+
+enum _iaq_buffer_state {
+	IAQ_BUFFER_EMPTY = 0,
+	IAQ_BUFFER_DEFAULT_VALS,
+	IAQ_BUFFER_VALID,
+};
+
+struct sgp_data {
+	struct i2c_client *client;
+	struct task_struct *iaq_thread;
+	struct mutex data_lock;
+	unsigned long iaq_init_start_jiffies;
+	unsigned long iaq_defval_skip_jiffies;
+	u16 product_id;
+	u16 feature_set;
+	unsigned long measure_interval_jiffies;
+	enum sgp_cmd iaq_init_cmd;
+	enum sgp_cmd measure_iaq_cmd;
+	enum sgp_cmd measure_gas_signals_cmd;
+	union sgp_reading buffer;
+	union sgp_reading iaq_buffer;
+	enum _iaq_buffer_state iaq_buffer_state;
+};
+
+struct sgp_device {
+	const struct iio_chan_spec *channels;
+	int num_channels;
+};
+
+static const struct sgp_version supported_versions_sgp30[]

= {

drivers/iio/chemical/sgp30.c new file +7 -0
+	{
+		.major = 1,
+		.minor = 0,
+	},
+};
+
+static const struct sgp_version supported_versions_sgpc3[]

= {

drivers/iio/chemical/sgp30.c new file +12 -0
+	{
+		.major = 0,
+		.minor = 4,
+	},
+};
+
+static const struct iio_chan_spec sgp30_channels[] = {
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_VOC,
+		.modified = 1,
+		.info_mask_separate =

BIT(IIO_CHAN_INFO_PROCESSED),

drivers/iio/chemical/sgp30.c new file +7 -0
+		.address = SGP30_IAQ_TVOC_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_CO2,
+		.modified = 1,
+		.info_mask_separate =

BIT(IIO_CHAN_INFO_PROCESSED),

drivers/iio/chemical/sgp30.c line 155

Quoted 8 lines
drivers/iio/chemical/sgp30.c new file +8 -0
+		.address = SGP30_IAQ_CO2EQ_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_ETHANOL,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),  

Definitely looks like it'll fit in under 80 chars.

drivers/iio/chemical/sgp30.c line 166

Quoted 11 lines
drivers/iio/chemical/sgp30.c new file +11 -0
+		.address = SGP30_SIG_ETOH_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_H2,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),  

Same here on length.

Quoted 14 lines
drivers/iio/chemical/sgp30.c new file +13 -0
+		.address = SGP30_SIG_H2_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+};
+
+static const struct iio_chan_spec sgpc3_channels[] = {
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_VOC,
+		.modified = 1,
+		.info_mask_separate =

BIT(IIO_CHAN_INFO_PROCESSED),

drivers/iio/chemical/sgp30.c line 187

Quoted 8 lines
drivers/iio/chemical/sgp30.c new file +8 -0
+		.address = SGPC3_IAQ_TVOC_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_ETHANOL,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),  

And another on length.

Quoted 491 lines
drivers/iio/chemical/sgp30.c new file +20 -0
+		.address = SGPC3_SIG_ETOH_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+};
+
+static const struct sgp_device sgp_devices[] = {
+	[SGP30] = {
+		.channels = sgp30_channels,
+		.num_channels = ARRAY_SIZE(sgp30_channels),
+	},
+	[SGPC3] = {
+		.channels = sgpc3_channels,
+		.num_channels = ARRAY_SIZE(sgpc3_channels),
+	},
+};
+
+/**
+ * sgp_verify_buffer() - verify the checksums of the data

buffer
words
+ *
+ * @data: SGP data
+ * @buf: Raw data buffer
+ * @word_count: Num data words stored in the buffer, excluding CRC
bytes
+ *
+ * Return: 0 on success, negative error otherwise.
+ */

drivers/iio/chemical/sgp30.c new file +2 -0
+static int sgp_verify_buffer(const struct sgp_data *data,
+			     union sgp_reading *buf, size_t

word_count)

drivers/iio/chemical/sgp30.c new file +2 -0
+{
+	size_t size = word_count * (SGP_WORD_LEN +

SGP_CRC8_LEN);

drivers/iio/chemical/sgp30.c new file +5 -0
+	int i;
+	u8 crc;
+	u8 *data_buf = &buf->start;
+
+	for (i = 0; i < size; i += SGP_WORD_LEN + SGP_CRC8_LEN)

{

drivers/iio/chemical/sgp30.c new file +1 -0
+		crc = crc8(sgp_crc8_table, &data_buf[i],

SGP_WORD_LEN,

drivers/iio/chemical/sgp30.c new file +3 -0
+			   SGP_CRC8_INIT);
+		if (crc != data_buf[i + SGP_WORD_LEN]) {
+			dev_err(&data->client->dev, "CRC

error\n");

drivers/iio/chemical/sgp30.c new file +9 -0
+			return -EIO;
+		}
+	}
+
+	return 0;
+}
+
+/**
+ * sgp_read_cmd() - reads data from sensor after issuing a

command
+ * The caller must hold data->data_lock for the duration of the
call.

+ * @data:        SGP data
+ * @cmd:         SGP Command to issue
+ * @buf:         Raw data buffer to use
+ * @word_count:  Num words to read, excluding CRC bytes

+ *
+ * Return: 0 on success, negative error otherwise.
+ */
+static int sgp_read_cmd(struct sgp_data *data, enum sgp_cmd cmd,

drivers/iio/chemical/sgp30.c new file +1 -0
+			union sgp_reading *buf, size_t

word_count,

drivers/iio/chemical/sgp30.c new file +5 -0
+			unsigned long duration_us)
+{
+	int ret;
+	struct i2c_client *client = data->client;
+	size_t size = word_count * (SGP_WORD_LEN +

SGP_CRC8_LEN);

drivers/iio/chemical/sgp30.c new file +3 -0
+	u8 *data_buf;
+
+	ret = i2c_master_send(client, (const char *)&cmd,

SGP_CMD_LEN);

drivers/iio/chemical/sgp30.c new file +19 -0
+	if (ret != SGP_CMD_LEN)
+		return -EIO;
+	usleep_range(duration_us, duration_us + 1000);
+
+	if (word_count == 0)
+		return 0;
+
+	data_buf = &buf->start;
+	ret = i2c_master_recv(client, data_buf, size);
+	if (ret < 0)
+		return ret;
+	if (ret != size)
+		return -EIO;
+
+	return sgp_verify_buffer(data, buf, word_count);
+}
+
+/**
+ * sgp_measure_iaq() - measure and retrieve IAQ values

from sensor
+ * The caller must hold data->data_lock for the duration of the
call.
+ * @data: SGP data
+ *
+ * Return: 0 on success, -EBUSY on default values,
negative
error
+ * otherwise.
+ */

drivers/iio/chemical/sgp30.c new file +6 -0
+
+static int sgp_measure_iaq(struct sgp_data *data)
+{
+	int ret;
+	/* data contains default values */
+	bool default_vals = !time_after(jiffies, data-  
Quoted 1 line

iaq_init_start_jiffies +

drivers/iio/chemical/sgp30.c new file +1 -0
+						 data-  
Quoted 1 line

iaq_defval_skip_jiffies);

drivers/iio/chemical/sgp30.c new file +2 -0
+
+	ret = sgp_read_cmd(data, data->measure_iaq_cmd,

&data-

Quoted 1 line

iaq_buffer,

drivers/iio/chemical/sgp30.c new file +1 -0
+			   SGP_MEASUREMENT_LEN,

SGP_MEASUREMENT_DURATION_US);

drivers/iio/chemical/sgp30.c new file +14 -0
+	if (ret < 0)
+		return ret;
+
+	data->iaq_buffer_state = IAQ_BUFFER_DEFAULT_VALS;
+
+	if (default_vals)
+		return -EBUSY;
+
+	data->iaq_buffer_state = IAQ_BUFFER_VALID;
+
+	return 0;
+}
+
+static void sgp_iaq_thread_sleep_until(const struct

sgp_data
*data,

drivers/iio/chemical/sgp30.c new file +1 -0
+				       unsigned long

sleep_jiffies)

drivers/iio/chemical/sgp30.c new file +6 -0
+{
+	const long IAQ_POLL = 50000;
+
+	while (!time_after(jiffies, sleep_jiffies)) {
+		usleep_range(IAQ_POLL, IAQ_POLL + 10000);
+		if (kthread_should_stop() || data-  
Quoted 1 line

iaq_init_start_jiffies == 0)

drivers/iio/chemical/sgp30.c new file +14 -0
+			return;
+	}
+}
+
+static int sgp_iaq_threadfn(void *p)
+{
+	struct sgp_data *data = (struct sgp_data *)p;
+	unsigned long next_update_jiffies;
+	int ret;
+
+	while (!kthread_should_stop()) {
+		mutex_lock(&data->data_lock);
+		if (data->iaq_init_start_jiffies == 0) {
+			ret = sgp_read_cmd(data, data-
Quoted 1 line

iaq_init_cmd,

NULL, 0,

drivers/iio/chemical/sgp30.c new file +1 -0
+					   SGP_CMD_DURATION_US)

;

drivers/iio/chemical/sgp30.c new file +9 -0
+			if (ret < 0)
+				goto unlock_sleep_continue;
+			data->iaq_init_start_jiffies = jiffies;
+		}
+
+		ret = sgp_measure_iaq(data);
+		if (ret && ret != -EBUSY) {
+			dev_warn(&data->client->dev,
+				 "IAQ measurement error

[%d]\n", ret);

drivers/iio/chemical/sgp30.c new file +3 -0
+		}
+unlock_sleep_continue:
+		next_update_jiffies = jiffies + data-  
Quoted 1 line

measure_interval_jiffies;

drivers/iio/chemical/sgp30.c new file +2 -0
+		mutex_unlock(&data->data_lock);
+		sgp_iaq_thread_sleep_until(data,

next_update_jiffies);

drivers/iio/chemical/sgp30.c new file +7 -0
+	}
+
+	return 0;
+}
+
+static int sgp_read_raw(struct iio_dev *indio_dev,
+			struct iio_chan_spec const *chan, int

*val,

drivers/iio/chemical/sgp30.c new file +10 -0
+			int *val2, long mask)
+{
+	struct sgp_data *data = iio_priv(indio_dev);
+	struct sgp_crc_word *words;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
+		mutex_lock(&data->data_lock);
+		if (data->iaq_buffer_state != IAQ_BUFFER_VALID)

{

drivers/iio/chemical/sgp30.c new file +26 -0
+			mutex_unlock(&data->data_lock);
+			return -EBUSY;
+		}
+		words = data->iaq_buffer.raw_words;
+		switch (chan->address) {
+		case SGP30_IAQ_TVOC_IDX:
+		case SGPC3_IAQ_TVOC_IDX:
+			*val = 0;
+			*val2 = be16_to_cpu(words[1].value);
+			ret = IIO_VAL_INT_PLUS_NANO;
+			break;
+		case SGP30_IAQ_CO2EQ_IDX:
+			*val = 0;
+			*val2 = be16_to_cpu(words[0].value);
+			ret = IIO_VAL_INT_PLUS_MICRO;
+			break;
+		default:
+			ret = -EINVAL;
+			break;
+		}
+		mutex_unlock(&data->data_lock);
+		break;
+	case IIO_CHAN_INFO_RAW:
+		mutex_lock(&data->data_lock);
+		if (chan->address == SGPC3_SIG_ETOH_IDX) {
+			if (data->iaq_buffer_state ==

IAQ_BUFFER_EMPTY)

drivers/iio/chemical/sgp30.c new file +6 -0
+				ret = -EBUSY;
+			else
+				ret = 0;
+			words = data->iaq_buffer.raw_words;
+		} else {
+			ret = sgp_read_cmd(data, data-  
Quoted 1 line

measure_gas_signals_cmd,

drivers/iio/chemical/sgp30.c new file +1 -0
+					   &data->buffer,

SGP_MEASUREMENT_LEN,

drivers/iio/chemical/sgp30.c new file +1 -0
+					   SGP_MEASUREMENT_DURA

TION_US)
;

drivers/iio/chemical/sgp30.c new file +42 -0
+			words = data->buffer.raw_words;
+		}
+		if (ret) {
+			mutex_unlock(&data->data_lock);
+			return ret;
+		}
+
+		switch (chan->address) {
+		case SGP30_SIG_ETOH_IDX:
+			*val = be16_to_cpu(words[1].value);
+			ret = IIO_VAL_INT;
+			break;
+		case SGPC3_SIG_ETOH_IDX:
+		case SGP30_SIG_H2_IDX:
+			*val = be16_to_cpu(words[0].value);
+			ret = IIO_VAL_INT;
+			break;
+		default:
+			ret = -EINVAL;
+			break;
+		}
+		mutex_unlock(&data->data_lock);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return ret;
+}
+
+static int sgp_check_compat(struct sgp_data *data,
+			    unsigned int product_id)
+{
+	const struct sgp_version *supported_versions;
+	u16 ix, num_fs;
+	u16 product, generation, major, minor;
+
+	/* driver does not match product */
+	generation = SGP_VERS_GEN(data);
+	if (generation != 0) {
+		dev_err(&data->client->dev,
+			"incompatible product generation %d !=

0",
generation);

drivers/iio/chemical/sgp30.c new file +7 -0
+		return -ENODEV;
+	}
+
+	product = SGP_VERS_PRODUCT(data);
+	if (product != product_id) {
+		dev_err(&data->client->dev,
+			"sensor reports a different product:

0x%04hx\n",

drivers/iio/chemical/sgp30.c new file +6 -0
+			product);
+		return -ENODEV;
+	}
+
+	if (SGP_VERS_RESERVED(data))
+		dev_warn(&data->client->dev, "reserved bit is

set\n");

drivers/iio/chemical/sgp30.c new file +2 -0
+
+	/* engineering samples are not supported: no interface

guarantees */

drivers/iio/chemical/sgp30.c new file +24 -0
+	if (SGP_VERS_ENG_BIT(data))
+		return -ENODEV;
+
+	switch (product) {
+	case SGP30:
+		supported_versions = supported_versions_sgp30;
+		num_fs = ARRAY_SIZE(supported_versions_sgp30);
+		break;
+	case SGPC3:
+		supported_versions = supported_versions_sgpc3;
+		num_fs = ARRAY_SIZE(supported_versions_sgpc3);
+		break;
+	default:
+		return -ENODEV;
+	}
+
+	major = SGP_VERS_MAJOR(data);
+	minor = SGP_VERS_MINOR(data);
+	for (ix = 0; ix < num_fs; ix++) {
+		if (major == supported_versions[ix].major &&
+		    minor >= supported_versions[ix].minor)
+			return 0;
+	}
+	dev_err(&data->client->dev, "unsupported sgp version:

%d.%d\n",

drivers/iio/chemical/sgp30.c new file +13 -0
+		major, minor);
+
+	return -ENODEV;
+}
+
+static void sgp_init(struct sgp_data *data)
+{
+	data->iaq_init_cmd = SGP_CMD_IAQ_INIT;
+	data->iaq_init_start_jiffies = 0;
+	data->iaq_buffer_state = IAQ_BUFFER_EMPTY;
+	switch (SGP_VERS_PRODUCT(data)) {
+	case SGP30:
+		data->measure_interval_jiffies =

SGP30_MEASURE_INTERVAL_HZ * HZ;

drivers/iio/chemical/sgp30.c new file +2 -0
+		data->measure_iaq_cmd = SGP_CMD_IAQ_MEASURE;
+		data->measure_gas_signals_cmd =

SGP30_CMD_MEASURE_SIGNAL;

drivers/iio/chemical/sgp30.c new file +5 -0
+		data->product_id = SGP30;
+		data->iaq_defval_skip_jiffies = 15 * HZ;
+		break;
+	case SGPC3:
+		data->measure_interval_jiffies =

SGPC3_MEASURE_INTERVAL_HZ * HZ;

drivers/iio/chemical/sgp30.c new file +2 -0
+		data->measure_iaq_cmd = SGPC3_CMD_MEASURE_RAW;
+		data->measure_gas_signals_cmd =

SGPC3_CMD_MEASURE_RAW;

drivers/iio/chemical/sgp30.c new file +13 -0
+		data->product_id = SGPC3;
+		data->iaq_defval_skip_jiffies =
+			43 * data->measure_interval_jiffies;
+		break;
+	};
+}
+
+static const struct iio_info sgp_info = {
+	.read_raw	= sgp_read_raw,
+};
+
+static const struct of_device_id sgp_dt_ids[] = {
+	{ .compatible = "sensirion,sgp30", .data = (void

*)SGP30 },

drivers/iio/chemical/sgp30.c new file +1 -0
+	{ .compatible = "sensirion,sgpc3", .data = (void

*)SGPC3 },

drivers/iio/chemical/sgp30.c new file +13 -0
+	{ }
+};
+
+static int sgp_probe(struct i2c_client *client,
+		     const struct i2c_device_id *id)
+{
+	struct iio_dev *indio_dev;
+	struct sgp_data *data;
+	const struct of_device_id *of_id;
+	unsigned long product_id;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&client->dev,

sizeof(*data));

drivers/iio/chemical/sgp30.c new file +16 -0
+	if (!indio_dev)
+		return -ENOMEM;
+
+	of_id = of_match_device(sgp_dt_ids, &client->dev);
+	if (of_id)
+		product_id = (unsigned long)of_id->data;
+	else
+		product_id = id->driver_data;
+
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	data->client = client;
+	crc8_populate_msb(sgp_crc8_table, SGP_CRC8_POLYNOMIAL);
+	mutex_init(&data->data_lock);
+
+	/* get feature set version and write it to client data

*/

drivers/iio/chemical/sgp30.c new file +1 -0
+	ret = sgp_read_cmd(data, SGP_CMD_GET_FEATURE_SET,

&data-

Quoted 1 line

buffer, 1,

drivers/iio/chemical/sgp30.c new file +5 -0
+			   SGP_CMD_DURATION_US);
+	if (ret < 0)
+		return ret;
+
+	data->feature_set = be16_to_cpu(data-  
Quoted 1 line

buffer.raw_words[0].value);

drivers/iio/chemical/sgp30.c new file +11 -0
+
+	ret = sgp_check_compat(data, product_id);
+	if (ret)
+		return ret;
+
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->info = &sgp_info;
+	indio_dev->name = id->name;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = sgp_devices[product_id].channels;
+	indio_dev->num_channels =

sgp_devices[product_id].num_channels;

drivers/iio/chemical/sgp30.c new file +4 -0
+
+	sgp_init(data);
+
+	ret = devm_iio_device_register(&client->dev,

indio_dev);

drivers/iio/chemical/sgp30.c new file +2 -0
+	if (ret) {
+		dev_err(&client->dev, "failed to register iio

device\n");

drivers/iio/chemical/sgp30.c new file +5 -0
+		return ret;
+	}
+
+	data->iaq_thread = kthread_run(sgp_iaq_threadfn, data,
+				       "%s-iaq", data->client-
Quoted 1 line

name);

drivers/iio/chemical/sgp30.c new file +36 -0
+
+	return 0;
+}
+
+static int sgp_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+	struct sgp_data *data = iio_priv(indio_dev);
+
+	if (data->iaq_thread)
+		kthread_stop(data->iaq_thread);
+
+	return 0;
+}
+
+static const struct i2c_device_id sgp_id[] = {
+	{ "sgp30", SGP30 },
+	{ "sgpc3", SGPC3 },
+	{ }
+};
+
+MODULE_DEVICE_TABLE(i2c, sgp_id);
+MODULE_DEVICE_TABLE(of, sgp_dt_ids);
+
+static struct i2c_driver sgp_driver = {
+	.driver = {
+		.name = "sgp30",
+		.of_match_table = of_match_ptr(sgp_dt_ids),
+	},
+	.probe = sgp_probe,
+	.remove = sgp_remove,
+	.id_table = sgp_id,
+};
+module_i2c_driver(sgp_driver);
+
+MODULE_AUTHOR("Andreas Brauchli <

[email protected]>")
;
+MODULE_AUTHOR("Pascal Sachs <[email protected]>") ;

drivers/iio/chemical/sgp30.c new file +2 -0
+MODULE_DESCRIPTION("Sensirion SGP gas sensors");
+MODULE_LICENSE("GPL v2");  
maintainer
#7 lore raw
Quoted 59 lines · On Thu, 07 Mar 2019 21:34:12 +0100 Andreas Brauchli…
Quoted 50 lines · Jonathan Cameron
Quoted 47 lines · On Tue, 18 Dec 2018 18:44:40 +0100 Tomasz Duszynski <[email protected]> wrote:
Quoted 46 lines · Jonathan Cameron
Quoted 43 lines · On Sun, 16 Dec 2018 17:38:18 +0100 Andreas Brauchli…
Quoted 24 lines · Jonathan Cameron
Quoted 21 lines · On Thu, 13 Dec 2018 15:43:23 +0100 Andreas Brauchli…

Support Sensirion SGP30 and SGPC3 multi-pixel I2C gas sensors

Supported Features:

* Indoor Air Quality (IAQ) concentrations for
- tVOC (in_concentration_voc_input)
- CO2eq (in_concentration_co2_input) - SGP30 only

IAQ concentrations are periodically read out by a
background
thread
to allow the sensor to maintain its internal baseline.

* Gas concentration signals
- Ethanol (in_concentration_ethanol_raw)
- H2 (in_concentration_h2_raw) - SGP30 only

https://www.sensirion.com/file/datasheet_sgp30
https://www.sensirion.com/file/datasheet_sgpc3

Quoted 6 lines
Quoted 2 lines

Signed-off-by: Andreas Brauchli <
[email protected]>

Why do we provide endianness info given we don't provide the buffered
interface? It's only used when we do.

Makes sense but I wasn't aware of it. The initial submission came with
buffering, so it probably wasn't spotted in v2.

Quoted 6 lines

Some over the top line wrapping as well. However as that's it
I'll just clean up whilst applying. If I've missed something odd
around the endian thing shout!

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

No that's it, thanks a lot.

Quoted 5 lines

Jonathan

Quoted 2 lines
---
 drivers/iio/chemical/Kconfig  |  13 +  
Quoted 7 lines

Apologies for noticing so late, but I fear that the Kconfig part might have been lost.

It's not in the upstream commit
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?h=v5.0&id=ce514124161ac2ceb13d10b6c40cbf05c8f0cc91
nor in the testing branch (togreg is already removed)
https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git/tree/drivers/iio/chemical/Kconfig?h=testing

Sorry about this. I'll put together a patch to fix it up as it's a simple case of adding the missing Kconfig block, I've applied it directly to the fixes-togreg branch of iio.git with your reported-by added. If you could sanity check that it would be great. I'll push out as soon as my local build tests finish.

This will have to wait for the merge window to close, but that should be in about a week so not to bad.

Sorry again, clearly messed this one up!

Jonathan

Quoted 823 lines
Quoted 822 lines
Quoted 46 lines
Quoted 23 lines
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
 drivers/iio/chemical/Makefile |   1 +
 drivers/iio/chemical/sgp30.c  | 603
++++++++++++++++++++++++++++++++++
 3 files changed, 617 insertions(+)
 create mode 100644 drivers/iio/chemical/sgp30.c
drivers/iio/chemical/Kconfig +0 -0
index b8e005be4f87..b0c912be94a9 100644
@@ -61,6 +61,19 @@ config IAQCORE
 	  iAQ-Core Continuous/Pulsed VOC (Volatile Organic
 Compounds)
 	  sensors
 
Quoted 5 lines
Quoted 1 line

drivers/iio/chemical/Kconfig config IAQCORE · line 65

Quoted 1 line
drivers/iio/chemical/Kconfig +1 -0
+config SENSIRION_SGP30    

I noticed that in Tomasz's SPS30 patch it's just SPS30, so for consistency's sake I'm fine with dropping the branding. Up to you.

Quoted 8 lines

We've never had any hard rules on this, at least partly because a lot
of drivers came from other subsystems in the early days.

We probably should have a rule though. SGP30 for example is generic
enough that it might well be reused by someone else.

Wondering how likely is this? These days some popular sensors have their
clones. Interestingly enough, they are sold as genuine i.e without vendor/sensor name changes. On the other hand there are very
same ICs manufactured by different companies but still with different names.

Quoted 5 lines

Anyone have any strong opinions on this? If not I think I'll start
encouraging prefixing with manufacturer of first supported device.

Quoted 4 lines

What about sticking to what we have now and adding prefix when we do
have naming collision? Just my 2 cents.

Agreed in the sense that it's not worth changing existing drivers unless
we have a reason to..

But if the question is raised for what is preferred, I think adding a vendor
is good. There have been clashes with i2c devices in the past where the
binding without vendor was insufficient to distinguish the part.

Not sure what it was, but I remember the discussion being one of the motivations
to add vendor prefixes to the look up for i2c devices.

Jonathan

Quoted 753 lines
Quoted 753 lines

Jonathan

Quoted 749 lines
Quoted 747 lines
Quoted 205 lines
drivers/iio/chemical/Kconfig +5 -0
+	tristate "Sensirion SGPxx gas sensors"
+	depends on I2C
+	select CRC8
+	help
+	  Say Y here to build I2C interface support for the

following

drivers/iio/chemical/Kconfig +7 -0
+	  Sensirion SGP gas sensors:
+	    * SGP30 gas sensor
+	    * SGPC3 low power gas sensor
+
+	  To compile this driver as module, choose M here: the
+	  module will be called sgp30.
+
 config VZ89X
 	tristate "SGX Sensortech MiCS VZ89X VOC sensor"
 	depends on I2C
drivers/iio/chemical/Makefile +1 -0
index 2f4c4ba4d781..61e83944db1a 100644
@@ -9,4 +9,5 @@ obj-$(CONFIG_BME680_I2C) += bme680_i2c.o
 obj-$(CONFIG_BME680_SPI) += bme680_spi.o
 obj-$(CONFIG_CCS811)		+= ccs811.o
 obj-$(CONFIG_IAQCORE)		+= ams-iaq-core.o
+obj-$(CONFIG_SENSIRION_SGP30)	+= sgp30.o
 obj-$(CONFIG_VZ89X)		+= vz89x.o
drivers/iio/chemical/sgp30.c new file +5 -0
new file mode 100644
index 000000000000..90eb4cbb18cf
@@ -0,0 +1,603 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * sgp30.c - Support for Sensirion SGP Gas Sensors
+ *
+ * Copyright (C) 2018 Andreas Brauchli <    

[email protected]>
+ *
+ * I2C slave address: 0x58
+ *
+ * Datasheets:
+ * https://www.sensirion.com/file/datasheet_sgp30
+ * https://www.sensirion.com/file/datasheet_sgpc3
+ *
+ * TODO:
+ * - baseline support
+ * - humidity compensation
+ * - power mode switching (SGPC3)
+ */

drivers/iio/chemical/sgp30.c new file +16 -0
+
+#include <linux/crc8.h>
+#include <linux/delay.h>
+#include <linux/kthread.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/i2c.h>
+#include <linux/of_device.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+#define SGP_WORD_LEN				2
+#define SGP_CRC8_POLYNOMIAL			0x31
+#define SGP_CRC8_INIT				0xff
+#define SGP_CRC8_LEN				1
+#define SGP_CMD(cmd_word)			cpu_to_be16(cmd

_word)

drivers/iio/chemical/sgp30.c new file +3 -0
+#define SGP_CMD_DURATION_US			12000
+#define SGP_MEASUREMENT_DURATION_US		50000
+#define SGP_CMD_LEN				SGP_WOR

D_LEN
+#define SGP_CMD_MAX_BUF_SIZE (SGP_CM
D_LEN +
2 * SGP_WORD_LEN)

drivers/iio/chemical/sgp30.c new file +4 -0
+#define SGP_MEASUREMENT_LEN			2
+#define SGP30_MEASURE_INTERVAL_HZ		1
+#define SGPC3_MEASURE_INTERVAL_HZ		2
+#define SGP_VERS_PRODUCT(data)	((((data)->feature_set) 

&
0xf000) >> 12)
+#define SGP_VERS_RESERVED(data) ((((data)->feature_set)
&
0x0800) >> 11)
+#define SGP_VERS_GEN(data) ((((data)->feature_set) &
0x0600) >> 9)
+#define SGP_VERS_ENG_BIT(data) ((((data)->feature_set)
&
0x0100) >> 8)
+#define SGP_VERS_MAJOR(data) ((((data)->feature_set)
&
0x00e0) >> 5)
+#define SGP_VERS_MINOR(data) (((data)->feature_set)
&
0x001f)

drivers/iio/chemical/sgp30.c new file +24 -0
+
+DECLARE_CRC8_TABLE(sgp_crc8_table);
+
+enum sgp_product_id {
+	SGP30 = 0,
+	SGPC3,
+};
+
+enum sgp30_channel_idx {
+	SGP30_IAQ_TVOC_IDX = 0,
+	SGP30_IAQ_CO2EQ_IDX,
+	SGP30_SIG_ETOH_IDX,
+	SGP30_SIG_H2_IDX,
+};
+
+enum sgpc3_channel_idx {
+	SGPC3_IAQ_TVOC_IDX = 10,
+	SGPC3_SIG_ETOH_IDX,
+};
+
+enum sgp_cmd {
+	SGP_CMD_IAQ_INIT			= SGP_CMD(0x2003),
+	SGP_CMD_IAQ_MEASURE			= SGP_CMD(0x2008),
+	SGP_CMD_GET_FEATURE_SET			=

SGP_CMD(0x202f),

drivers/iio/chemical/sgp30.c new file +1 -0
+	SGP_CMD_GET_SERIAL_ID			=

SGP_CMD(0x3682),

drivers/iio/chemical/sgp30.c new file +4 -0
+
+	SGP30_CMD_MEASURE_SIGNAL		= SGP_CMD(0x2050),
+
+	SGPC3_CMD_MEASURE_RAW			=

SGP_CMD(0x2046),

drivers/iio/chemical/sgp30.c new file +46 -0
+};
+
+struct sgp_version {
+	u8 major;
+	u8 minor;
+};
+
+struct sgp_crc_word {
+	__be16 value;
+	u8 crc8;
+} __attribute__((__packed__));
+
+union sgp_reading {
+	u8 start;
+	struct sgp_crc_word raw_words[4];
+};
+
+enum _iaq_buffer_state {
+	IAQ_BUFFER_EMPTY = 0,
+	IAQ_BUFFER_DEFAULT_VALS,
+	IAQ_BUFFER_VALID,
+};
+
+struct sgp_data {
+	struct i2c_client *client;
+	struct task_struct *iaq_thread;
+	struct mutex data_lock;
+	unsigned long iaq_init_start_jiffies;
+	unsigned long iaq_defval_skip_jiffies;
+	u16 product_id;
+	u16 feature_set;
+	unsigned long measure_interval_jiffies;
+	enum sgp_cmd iaq_init_cmd;
+	enum sgp_cmd measure_iaq_cmd;
+	enum sgp_cmd measure_gas_signals_cmd;
+	union sgp_reading buffer;
+	union sgp_reading iaq_buffer;
+	enum _iaq_buffer_state iaq_buffer_state;
+};
+
+struct sgp_device {
+	const struct iio_chan_spec *channels;
+	int num_channels;
+};
+
+static const struct sgp_version supported_versions_sgp30[]

= {

drivers/iio/chemical/sgp30.c new file +7 -0
+	{
+		.major = 1,
+		.minor = 0,
+	},
+};
+
+static const struct sgp_version supported_versions_sgpc3[]

= {

drivers/iio/chemical/sgp30.c new file +12 -0
+	{
+		.major = 0,
+		.minor = 4,
+	},
+};
+
+static const struct iio_chan_spec sgp30_channels[] = {
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_VOC,
+		.modified = 1,
+		.info_mask_separate =

BIT(IIO_CHAN_INFO_PROCESSED),

drivers/iio/chemical/sgp30.c new file +7 -0
+		.address = SGP30_IAQ_TVOC_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_CO2,
+		.modified = 1,
+		.info_mask_separate =

BIT(IIO_CHAN_INFO_PROCESSED),

drivers/iio/chemical/sgp30.c

Quoted 8 lines
drivers/iio/chemical/sgp30.c new file +8 -0
+		.address = SGP30_IAQ_CO2EQ_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_ETHANOL,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),    

Definitely looks like it'll fit in under 80 chars.

drivers/iio/chemical/sgp30.c

Quoted 11 lines
drivers/iio/chemical/sgp30.c new file +11 -0
+		.address = SGP30_SIG_ETOH_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_H2,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),    

Same here on length.

Quoted 14 lines
drivers/iio/chemical/sgp30.c new file +13 -0
+		.address = SGP30_SIG_H2_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+};
+
+static const struct iio_chan_spec sgpc3_channels[] = {
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_VOC,
+		.modified = 1,
+		.info_mask_separate =

BIT(IIO_CHAN_INFO_PROCESSED),

drivers/iio/chemical/sgp30.c

Quoted 8 lines
drivers/iio/chemical/sgp30.c new file +8 -0
+		.address = SGPC3_IAQ_TVOC_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_ETHANOL,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),    

And another on length.

Quoted 491 lines
drivers/iio/chemical/sgp30.c new file +20 -0
+		.address = SGPC3_SIG_ETOH_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+};
+
+static const struct sgp_device sgp_devices[] = {
+	[SGP30] = {
+		.channels = sgp30_channels,
+		.num_channels = ARRAY_SIZE(sgp30_channels),
+	},
+	[SGPC3] = {
+		.channels = sgpc3_channels,
+		.num_channels = ARRAY_SIZE(sgpc3_channels),
+	},
+};
+
+/**
+ * sgp_verify_buffer() - verify the checksums of the data

buffer
words
+ *
+ * @data: SGP data
+ * @buf: Raw data buffer
+ * @word_count: Num data words stored in the buffer, excluding CRC
bytes
+ *
+ * Return: 0 on success, negative error otherwise.
+ */

drivers/iio/chemical/sgp30.c new file +2 -0
+static int sgp_verify_buffer(const struct sgp_data *data,
+			     union sgp_reading *buf, size_t

word_count)

drivers/iio/chemical/sgp30.c new file +2 -0
+{
+	size_t size = word_count * (SGP_WORD_LEN +

SGP_CRC8_LEN);

drivers/iio/chemical/sgp30.c new file +5 -0
+	int i;
+	u8 crc;
+	u8 *data_buf = &buf->start;
+
+	for (i = 0; i < size; i += SGP_WORD_LEN + SGP_CRC8_LEN)

{

drivers/iio/chemical/sgp30.c new file +1 -0
+		crc = crc8(sgp_crc8_table, &data_buf[i],

SGP_WORD_LEN,

drivers/iio/chemical/sgp30.c new file +3 -0
+			   SGP_CRC8_INIT);
+		if (crc != data_buf[i + SGP_WORD_LEN]) {
+			dev_err(&data->client->dev, "CRC

error\n");

drivers/iio/chemical/sgp30.c new file +9 -0
+			return -EIO;
+		}
+	}
+
+	return 0;
+}
+
+/**
+ * sgp_read_cmd() - reads data from sensor after issuing a

command
+ * The caller must hold data->data_lock for the duration of the
call.

+ * @data:        SGP data
+ * @cmd:         SGP Command to issue
+ * @buf:         Raw data buffer to use
+ * @word_count:  Num words to read, excluding CRC bytes

+ *
+ * Return: 0 on success, negative error otherwise.
+ */
+static int sgp_read_cmd(struct sgp_data *data, enum sgp_cmd cmd,

drivers/iio/chemical/sgp30.c new file +1 -0
+			union sgp_reading *buf, size_t

word_count,

drivers/iio/chemical/sgp30.c new file +5 -0
+			unsigned long duration_us)
+{
+	int ret;
+	struct i2c_client *client = data->client;
+	size_t size = word_count * (SGP_WORD_LEN +

SGP_CRC8_LEN);

drivers/iio/chemical/sgp30.c new file +3 -0
+	u8 *data_buf;
+
+	ret = i2c_master_send(client, (const char *)&cmd,

SGP_CMD_LEN);

drivers/iio/chemical/sgp30.c new file +19 -0
+	if (ret != SGP_CMD_LEN)
+		return -EIO;
+	usleep_range(duration_us, duration_us + 1000);
+
+	if (word_count == 0)
+		return 0;
+
+	data_buf = &buf->start;
+	ret = i2c_master_recv(client, data_buf, size);
+	if (ret < 0)
+		return ret;
+	if (ret != size)
+		return -EIO;
+
+	return sgp_verify_buffer(data, buf, word_count);
+}
+
+/**
+ * sgp_measure_iaq() - measure and retrieve IAQ values

from sensor
+ * The caller must hold data->data_lock for the duration of the
call.
+ * @data: SGP data
+ *
+ * Return: 0 on success, -EBUSY on default values,
negative
error
+ * otherwise.
+ */

drivers/iio/chemical/sgp30.c new file +6 -0
+
+static int sgp_measure_iaq(struct sgp_data *data)
+{
+	int ret;
+	/* data contains default values */
+	bool default_vals = !time_after(jiffies, data-    
Quoted 1 line

iaq_init_start_jiffies +

drivers/iio/chemical/sgp30.c new file +1 -0
+						 data-    
Quoted 1 line

iaq_defval_skip_jiffies);

drivers/iio/chemical/sgp30.c new file +2 -0
+
+	ret = sgp_read_cmd(data, data->measure_iaq_cmd,

&data-

Quoted 1 line

iaq_buffer,

drivers/iio/chemical/sgp30.c new file +1 -0
+			   SGP_MEASUREMENT_LEN,

SGP_MEASUREMENT_DURATION_US);

drivers/iio/chemical/sgp30.c new file +14 -0
+	if (ret < 0)
+		return ret;
+
+	data->iaq_buffer_state = IAQ_BUFFER_DEFAULT_VALS;
+
+	if (default_vals)
+		return -EBUSY;
+
+	data->iaq_buffer_state = IAQ_BUFFER_VALID;
+
+	return 0;
+}
+
+static void sgp_iaq_thread_sleep_until(const struct

sgp_data
*data,

drivers/iio/chemical/sgp30.c new file +1 -0
+				       unsigned long

sleep_jiffies)

drivers/iio/chemical/sgp30.c new file +6 -0
+{
+	const long IAQ_POLL = 50000;
+
+	while (!time_after(jiffies, sleep_jiffies)) {
+		usleep_range(IAQ_POLL, IAQ_POLL + 10000);
+		if (kthread_should_stop() || data-    
Quoted 1 line

iaq_init_start_jiffies == 0)

drivers/iio/chemical/sgp30.c new file +14 -0
+			return;
+	}
+}
+
+static int sgp_iaq_threadfn(void *p)
+{
+	struct sgp_data *data = (struct sgp_data *)p;
+	unsigned long next_update_jiffies;
+	int ret;
+
+	while (!kthread_should_stop()) {
+		mutex_lock(&data->data_lock);
+		if (data->iaq_init_start_jiffies == 0) {
+			ret = sgp_read_cmd(data, data-  
Quoted 1 line

iaq_init_cmd,

NULL, 0,

drivers/iio/chemical/sgp30.c new file +1 -0
+					   SGP_CMD_DURATION_US)

;

drivers/iio/chemical/sgp30.c new file +9 -0
+			if (ret < 0)
+				goto unlock_sleep_continue;
+			data->iaq_init_start_jiffies = jiffies;
+		}
+
+		ret = sgp_measure_iaq(data);
+		if (ret && ret != -EBUSY) {
+			dev_warn(&data->client->dev,
+				 "IAQ measurement error

[%d]\n", ret);

drivers/iio/chemical/sgp30.c new file +3 -0
+		}
+unlock_sleep_continue:
+		next_update_jiffies = jiffies + data-    
Quoted 1 line

measure_interval_jiffies;

drivers/iio/chemical/sgp30.c new file +2 -0
+		mutex_unlock(&data->data_lock);
+		sgp_iaq_thread_sleep_until(data,

next_update_jiffies);

drivers/iio/chemical/sgp30.c new file +7 -0
+	}
+
+	return 0;
+}
+
+static int sgp_read_raw(struct iio_dev *indio_dev,
+			struct iio_chan_spec const *chan, int

*val,

drivers/iio/chemical/sgp30.c new file +10 -0
+			int *val2, long mask)
+{
+	struct sgp_data *data = iio_priv(indio_dev);
+	struct sgp_crc_word *words;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
+		mutex_lock(&data->data_lock);
+		if (data->iaq_buffer_state != IAQ_BUFFER_VALID)

{

drivers/iio/chemical/sgp30.c new file +26 -0
+			mutex_unlock(&data->data_lock);
+			return -EBUSY;
+		}
+		words = data->iaq_buffer.raw_words;
+		switch (chan->address) {
+		case SGP30_IAQ_TVOC_IDX:
+		case SGPC3_IAQ_TVOC_IDX:
+			*val = 0;
+			*val2 = be16_to_cpu(words[1].value);
+			ret = IIO_VAL_INT_PLUS_NANO;
+			break;
+		case SGP30_IAQ_CO2EQ_IDX:
+			*val = 0;
+			*val2 = be16_to_cpu(words[0].value);
+			ret = IIO_VAL_INT_PLUS_MICRO;
+			break;
+		default:
+			ret = -EINVAL;
+			break;
+		}
+		mutex_unlock(&data->data_lock);
+		break;
+	case IIO_CHAN_INFO_RAW:
+		mutex_lock(&data->data_lock);
+		if (chan->address == SGPC3_SIG_ETOH_IDX) {
+			if (data->iaq_buffer_state ==

IAQ_BUFFER_EMPTY)

drivers/iio/chemical/sgp30.c new file +6 -0
+				ret = -EBUSY;
+			else
+				ret = 0;
+			words = data->iaq_buffer.raw_words;
+		} else {
+			ret = sgp_read_cmd(data, data-    
Quoted 1 line

measure_gas_signals_cmd,

drivers/iio/chemical/sgp30.c new file +1 -0
+					   &data->buffer,

SGP_MEASUREMENT_LEN,

drivers/iio/chemical/sgp30.c new file +1 -0
+					   SGP_MEASUREMENT_DURA

TION_US)
;

drivers/iio/chemical/sgp30.c new file +42 -0
+			words = data->buffer.raw_words;
+		}
+		if (ret) {
+			mutex_unlock(&data->data_lock);
+			return ret;
+		}
+
+		switch (chan->address) {
+		case SGP30_SIG_ETOH_IDX:
+			*val = be16_to_cpu(words[1].value);
+			ret = IIO_VAL_INT;
+			break;
+		case SGPC3_SIG_ETOH_IDX:
+		case SGP30_SIG_H2_IDX:
+			*val = be16_to_cpu(words[0].value);
+			ret = IIO_VAL_INT;
+			break;
+		default:
+			ret = -EINVAL;
+			break;
+		}
+		mutex_unlock(&data->data_lock);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return ret;
+}
+
+static int sgp_check_compat(struct sgp_data *data,
+			    unsigned int product_id)
+{
+	const struct sgp_version *supported_versions;
+	u16 ix, num_fs;
+	u16 product, generation, major, minor;
+
+	/* driver does not match product */
+	generation = SGP_VERS_GEN(data);
+	if (generation != 0) {
+		dev_err(&data->client->dev,
+			"incompatible product generation %d !=

0",
generation);

drivers/iio/chemical/sgp30.c new file +7 -0
+		return -ENODEV;
+	}
+
+	product = SGP_VERS_PRODUCT(data);
+	if (product != product_id) {
+		dev_err(&data->client->dev,
+			"sensor reports a different product:

0x%04hx\n",

drivers/iio/chemical/sgp30.c new file +6 -0
+			product);
+		return -ENODEV;
+	}
+
+	if (SGP_VERS_RESERVED(data))
+		dev_warn(&data->client->dev, "reserved bit is

set\n");

drivers/iio/chemical/sgp30.c new file +2 -0
+
+	/* engineering samples are not supported: no interface

guarantees */

drivers/iio/chemical/sgp30.c new file +24 -0
+	if (SGP_VERS_ENG_BIT(data))
+		return -ENODEV;
+
+	switch (product) {
+	case SGP30:
+		supported_versions = supported_versions_sgp30;
+		num_fs = ARRAY_SIZE(supported_versions_sgp30);
+		break;
+	case SGPC3:
+		supported_versions = supported_versions_sgpc3;
+		num_fs = ARRAY_SIZE(supported_versions_sgpc3);
+		break;
+	default:
+		return -ENODEV;
+	}
+
+	major = SGP_VERS_MAJOR(data);
+	minor = SGP_VERS_MINOR(data);
+	for (ix = 0; ix < num_fs; ix++) {
+		if (major == supported_versions[ix].major &&
+		    minor >= supported_versions[ix].minor)
+			return 0;
+	}
+	dev_err(&data->client->dev, "unsupported sgp version:

%d.%d\n",

drivers/iio/chemical/sgp30.c new file +13 -0
+		major, minor);
+
+	return -ENODEV;
+}
+
+static void sgp_init(struct sgp_data *data)
+{
+	data->iaq_init_cmd = SGP_CMD_IAQ_INIT;
+	data->iaq_init_start_jiffies = 0;
+	data->iaq_buffer_state = IAQ_BUFFER_EMPTY;
+	switch (SGP_VERS_PRODUCT(data)) {
+	case SGP30:
+		data->measure_interval_jiffies =

SGP30_MEASURE_INTERVAL_HZ * HZ;

drivers/iio/chemical/sgp30.c new file +2 -0
+		data->measure_iaq_cmd = SGP_CMD_IAQ_MEASURE;
+		data->measure_gas_signals_cmd =

SGP30_CMD_MEASURE_SIGNAL;

drivers/iio/chemical/sgp30.c new file +5 -0
+		data->product_id = SGP30;
+		data->iaq_defval_skip_jiffies = 15 * HZ;
+		break;
+	case SGPC3:
+		data->measure_interval_jiffies =

SGPC3_MEASURE_INTERVAL_HZ * HZ;

drivers/iio/chemical/sgp30.c new file +2 -0
+		data->measure_iaq_cmd = SGPC3_CMD_MEASURE_RAW;
+		data->measure_gas_signals_cmd =

SGPC3_CMD_MEASURE_RAW;

drivers/iio/chemical/sgp30.c new file +13 -0
+		data->product_id = SGPC3;
+		data->iaq_defval_skip_jiffies =
+			43 * data->measure_interval_jiffies;
+		break;
+	};
+}
+
+static const struct iio_info sgp_info = {
+	.read_raw	= sgp_read_raw,
+};
+
+static const struct of_device_id sgp_dt_ids[] = {
+	{ .compatible = "sensirion,sgp30", .data = (void

*)SGP30 },

drivers/iio/chemical/sgp30.c new file +1 -0
+	{ .compatible = "sensirion,sgpc3", .data = (void

*)SGPC3 },

drivers/iio/chemical/sgp30.c new file +13 -0
+	{ }
+};
+
+static int sgp_probe(struct i2c_client *client,
+		     const struct i2c_device_id *id)
+{
+	struct iio_dev *indio_dev;
+	struct sgp_data *data;
+	const struct of_device_id *of_id;
+	unsigned long product_id;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&client->dev,

sizeof(*data));

drivers/iio/chemical/sgp30.c new file +16 -0
+	if (!indio_dev)
+		return -ENOMEM;
+
+	of_id = of_match_device(sgp_dt_ids, &client->dev);
+	if (of_id)
+		product_id = (unsigned long)of_id->data;
+	else
+		product_id = id->driver_data;
+
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	data->client = client;
+	crc8_populate_msb(sgp_crc8_table, SGP_CRC8_POLYNOMIAL);
+	mutex_init(&data->data_lock);
+
+	/* get feature set version and write it to client data

*/

drivers/iio/chemical/sgp30.c new file +1 -0
+	ret = sgp_read_cmd(data, SGP_CMD_GET_FEATURE_SET,

&data-

Quoted 1 line

buffer, 1,

drivers/iio/chemical/sgp30.c new file +5 -0
+			   SGP_CMD_DURATION_US);
+	if (ret < 0)
+		return ret;
+
+	data->feature_set = be16_to_cpu(data-    
Quoted 1 line

buffer.raw_words[0].value);

drivers/iio/chemical/sgp30.c new file +11 -0
+
+	ret = sgp_check_compat(data, product_id);
+	if (ret)
+		return ret;
+
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->info = &sgp_info;
+	indio_dev->name = id->name;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels = sgp_devices[product_id].channels;
+	indio_dev->num_channels =

sgp_devices[product_id].num_channels;

drivers/iio/chemical/sgp30.c new file +4 -0
+
+	sgp_init(data);
+
+	ret = devm_iio_device_register(&client->dev,

indio_dev);

drivers/iio/chemical/sgp30.c new file +2 -0
+	if (ret) {
+		dev_err(&client->dev, "failed to register iio

device\n");

drivers/iio/chemical/sgp30.c new file +5 -0
+		return ret;
+	}
+
+	data->iaq_thread = kthread_run(sgp_iaq_threadfn, data,
+				       "%s-iaq", data->client-  
Quoted 1 line

name);

drivers/iio/chemical/sgp30.c new file +36 -0
+
+	return 0;
+}
+
+static int sgp_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev = i2c_get_clientdata(client);
+	struct sgp_data *data = iio_priv(indio_dev);
+
+	if (data->iaq_thread)
+		kthread_stop(data->iaq_thread);
+
+	return 0;
+}
+
+static const struct i2c_device_id sgp_id[] = {
+	{ "sgp30", SGP30 },
+	{ "sgpc3", SGPC3 },
+	{ }
+};
+
+MODULE_DEVICE_TABLE(i2c, sgp_id);
+MODULE_DEVICE_TABLE(of, sgp_dt_ids);
+
+static struct i2c_driver sgp_driver = {
+	.driver = {
+		.name = "sgp30",
+		.of_match_table = of_match_ptr(sgp_dt_ids),
+	},
+	.probe = sgp_probe,
+	.remove = sgp_remove,
+	.id_table = sgp_id,
+};
+module_i2c_driver(sgp_driver);
+
+MODULE_AUTHOR("Andreas Brauchli <  

[email protected]>")
;
+MODULE_AUTHOR("Pascal Sachs <[email protected]>") ;

drivers/iio/chemical/sgp30.c new file +2 -0
+MODULE_DESCRIPTION("Sensirion SGP gas sensors");
+MODULE_LICENSE("GPL v2");    
author Reviewed-by
#8 lore raw
Quoted 69 lines · Jonathan Cameron
Quoted 66 lines · On Thu, 07 Mar 2019 21:34:12 +0100 Andreas Brauchli…
Quoted 64 lines · Jonathan Cameron
Quoted 61 lines · On Tue, 18 Dec 2018 18:44:40 +0100 Tomasz Duszynski <[email protected]> wrote:
Quoted 59 lines · Jonathan Cameron
Quoted 56 lines · On Sun, 16 Dec 2018 17:38:18 +0100 Andreas Brauchli…
Quoted 25 lines · Jonathan Cameron
Quoted 22 lines · On Thu, 13 Dec 2018 15:43:23 +0100 Andreas Brauchli…

Support Sensirion SGP30 and SGPC3 multi-pixel I2C gas sensors

Supported Features:

* Indoor Air Quality (IAQ) concentrations for
- tVOC (in_concentration_voc_input)
- CO2eq (in_concentration_co2_input) - SGP30 only

IAQ concentrations are periodically read out by a
background
thread
to allow the sensor to maintain its internal
baseline.

* Gas concentration signals
- Ethanol (in_concentration_ethanol_raw)
- H2 (in_concentration_h2_raw) - SGP30 only

https://www.sensirion.com/file/datasheet_sgp30
https://www.sensirion.com/file/datasheet_sgpc3

Quoted 7 lines
Quoted 2 lines

Signed-off-by: Andreas Brauchli <
[email protected]>

Why do we provide endianness info given we don't provide the
buffered
interface? It's only used when we do.

Makes sense but I wasn't aware of it. The initial
submission
came with
buffering, so it probably wasn't spotted in v2.

Quoted 8 lines

Some over the top line wrapping as well. However as that's
it
I'll just clean up whilst applying. If I've missed something
odd
around the endian thing shout!

No that's it, thanks a lot.

Quoted 5 lines

Jonathan

Quoted 2 lines
---
 drivers/iio/chemical/Kconfig  |  13 +  
Quoted 6 lines
Quoted 6 lines

Apologies for noticing so late, but I fear that the Kconfig part might
have been lost.

It's not in the upstream commit

https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?h=v5.0&id=ce514124161ac2ceb13d10b6c40cbf05c8f0cc91

Quoted 2 lines
Quoted 2 lines

nor in the testing branch (togreg is already removed)

https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git/tree/drivers/iio/chemical/Kconfig?h=testing

Quoted 6 lines

Sorry about this. I'll put together a patch to fix it up as it's a simple case of adding the missing Kconfig block, I've applied it directly to the fixes-togreg branch of iio.git with your reported-by added. If you could sanity check that it would be great. I'll push out as soon as my local build tests finish.

No worries, all good. Thanks for the quick fix.

https://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio.git/commit/drivers/iio/chemical?h=fixes-togreg&id=831d2fefdfce757fcea86742220f8cbe7ca51ddd

Quoted 925 lines

This will have to wait for the merge window to close, but that should be in about a week so not to bad.

Sorry again, clearly messed this one up!

Jonathan

Quoted 915 lines
Quoted 915 lines
Quoted 51 lines
Quoted 26 lines
Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
 drivers/iio/chemical/Makefile |   1 +
 drivers/iio/chemical/sgp30.c  | 603
++++++++++++++++++++++++++++++++++
 3 files changed, 617 insertions(+)
 create mode 100644 drivers/iio/chemical/sgp30.c
drivers/iio/chemical/Kconfig +0 -0
index b8e005be4f87..b0c912be94a9 100644
@@ -61,6 +61,19 @@ config IAQCORE
 	  iAQ-Core Continuous/Pulsed VOC (Volatile

Organic
Compounds)
sensors

Quoted 7 lines
Quoted 1 line
Quoted 1 line

+config SENSIRION_SGP30

I noticed that in Tomasz's SPS30 patch it's just SPS30, so for
consistency's sake I'm fine with dropping the branding. Up to
you.

Quoted 9 lines

We've never had any hard rules on this, at least partly because a
lot
of drivers came from other subsystems in the early days.

We probably should have a rule though. SGP30 for example is generic
enough that it might well be reused by someone else.

Wondering how likely is this? These days some popular sensors have
their
clones. Interestingly enough, they are sold as genuine i.e without vendor/sensor name changes. On the other hand there are very
same ICs manufactured by different companies but still with different names.

Quoted 5 lines

Anyone have any strong opinions on this? If not I think I'll start
encouraging prefixing with manufacturer of first supported device.

Quoted 5 lines

What about sticking to what we have now and adding prefix when we
do
have naming collision? Just my 2 cents.

Agreed in the sense that it's not worth changing existing drivers unless
we have a reason to..

But if the question is raised for what is preferred, I think adding a
vendor
is good. There have been clashes with i2c devices in the past where
the
binding without vendor was insufficient to distinguish the part.

Not sure what it was, but I remember the discussion being one of the
motivations
to add vendor prefixes to the look up for i2c devices.

Jonathan

Quoted 837 lines
Quoted 837 lines

Jonathan

Quoted 833 lines
Quoted 831 lines
Quoted 219 lines
drivers/iio/chemical/Kconfig +5 -0
+	tristate "Sensirion SGPxx gas sensors"
+	depends on I2C
+	select CRC8
+	help
+	  Say Y here to build I2C interface support for

the
following

drivers/iio/chemical/Kconfig +5 -0
+	  Sensirion SGP gas sensors:
+	    * SGP30 gas sensor
+	    * SGPC3 low power gas sensor
+
+	  To compile this driver as module, choose M

here: the

drivers/iio/chemical/Kconfig +2 -0
+	  module will be called sgp30.
+
 config VZ89X
 	tristate "SGX Sensortech MiCS VZ89X VOC sensor"
 	depends on I2C
drivers/iio/chemical/Makefile +0 -0
index 2f4c4ba4d781..61e83944db1a 100644
@@ -9,4 +9,5 @@ obj-$(CONFIG_BME680_I2C) +=

bme680_i2c.o
obj-$(CONFIG_BME680_SPI) += bme680_spi.o
obj-$(CONFIG_CCS811) += ccs811.o
obj-$(CONFIG_IAQCORE) += ams-iaq-core.o

drivers/iio/chemical/Makefile +1 -0
+obj-$(CONFIG_SENSIRION_SGP30)	+= sgp30.o
 obj-$(CONFIG_VZ89X)		+= vz89x.o
drivers/iio/chemical/sgp30.c new file +5 -0
new file mode 100644
index 000000000000..90eb4cbb18cf
@@ -0,0 +1,603 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * sgp30.c - Support for Sensirion SGP Gas Sensors
+ *
+ * Copyright (C) 2018 Andreas Brauchli <    

[email protected]>
+ *
+ * I2C slave address: 0x58
+ *
+ * Datasheets:
+ * https://www.sensirion.com/file/datasheet_sgp30
+ * https://www.sensirion.com/file/datasheet_sgpc3
+ *
+ * TODO:
+ * - baseline support
+ * - humidity compensation
+ * - power mode switching (SGPC3)
+ */

drivers/iio/chemical/sgp30.c new file +16 -0
+
+#include <linux/crc8.h>
+#include <linux/delay.h>
+#include <linux/kthread.h>
+#include <linux/module.h>
+#include <linux/mutex.h>
+#include <linux/i2c.h>
+#include <linux/of_device.h>
+#include <linux/iio/iio.h>
+#include <linux/iio/sysfs.h>
+
+#define SGP_WORD_LEN				2
+#define SGP_CRC8_POLYNOMIAL			0x31
+#define SGP_CRC8_INIT				0xff
+#define SGP_CRC8_LEN				1
+#define SGP_CMD(cmd_word)			cpu_to_

be16(cmd
_word)

drivers/iio/chemical/sgp30.c new file +3 -0
+#define SGP_CMD_DURATION_US			12000
+#define SGP_MEASUREMENT_DURATION_US		50000
+#define SGP_CMD_LEN				SGP_WOR

D_LEN
+#define SGP_CMD_MAX_BUF_SIZE (SGP_CM
D_LEN +
2 * SGP_WORD_LEN)

drivers/iio/chemical/sgp30.c new file +4 -0
+#define SGP_MEASUREMENT_LEN			2
+#define SGP30_MEASURE_INTERVAL_HZ		1
+#define SGPC3_MEASURE_INTERVAL_HZ		2
+#define SGP_VERS_PRODUCT(data)	((((data)-
Quoted 1 line

feature_set)

&
0xf000) >> 12)
+#define SGP_VERS_RESERVED(data) ((((data)-

Quoted 1 line

feature_set)

&
0x0800) >> 11)
+#define SGP_VERS_GEN(data) ((((data)->feature_set)
&
0x0600) >> 9)
+#define SGP_VERS_ENG_BIT(data) ((((data)-

Quoted 1 line

feature_set)

&
0x0100) >> 8)
+#define SGP_VERS_MAJOR(data) ((((data)-

Quoted 1 line

feature_set)

&
0x00e0) >> 5)
+#define SGP_VERS_MINOR(data) (((data)->feature_set)
&
0x001f)

drivers/iio/chemical/sgp30.c new file +22 -0
+
+DECLARE_CRC8_TABLE(sgp_crc8_table);
+
+enum sgp_product_id {
+	SGP30 = 0,
+	SGPC3,
+};
+
+enum sgp30_channel_idx {
+	SGP30_IAQ_TVOC_IDX = 0,
+	SGP30_IAQ_CO2EQ_IDX,
+	SGP30_SIG_ETOH_IDX,
+	SGP30_SIG_H2_IDX,
+};
+
+enum sgpc3_channel_idx {
+	SGPC3_IAQ_TVOC_IDX = 10,
+	SGPC3_SIG_ETOH_IDX,
+};
+
+enum sgp_cmd {
+	SGP_CMD_IAQ_INIT			=

SGP_CMD(0x2003),

drivers/iio/chemical/sgp30.c new file +1 -0
+	SGP_CMD_IAQ_MEASURE			=

SGP_CMD(0x2008),

drivers/iio/chemical/sgp30.c new file +1 -0
+	SGP_CMD_GET_FEATURE_SET			=

SGP_CMD(0x202f),

drivers/iio/chemical/sgp30.c new file +1 -0
+	SGP_CMD_GET_SERIAL_ID			=

SGP_CMD(0x3682),

drivers/iio/chemical/sgp30.c new file +2 -0
+
+	SGP30_CMD_MEASURE_SIGNAL		=

SGP_CMD(0x2050),

drivers/iio/chemical/sgp30.c new file +2 -0
+
+	SGPC3_CMD_MEASURE_RAW			=

SGP_CMD(0x2046),

drivers/iio/chemical/sgp30.c new file +46 -0
+};
+
+struct sgp_version {
+	u8 major;
+	u8 minor;
+};
+
+struct sgp_crc_word {
+	__be16 value;
+	u8 crc8;
+} __attribute__((__packed__));
+
+union sgp_reading {
+	u8 start;
+	struct sgp_crc_word raw_words[4];
+};
+
+enum _iaq_buffer_state {
+	IAQ_BUFFER_EMPTY = 0,
+	IAQ_BUFFER_DEFAULT_VALS,
+	IAQ_BUFFER_VALID,
+};
+
+struct sgp_data {
+	struct i2c_client *client;
+	struct task_struct *iaq_thread;
+	struct mutex data_lock;
+	unsigned long iaq_init_start_jiffies;
+	unsigned long iaq_defval_skip_jiffies;
+	u16 product_id;
+	u16 feature_set;
+	unsigned long measure_interval_jiffies;
+	enum sgp_cmd iaq_init_cmd;
+	enum sgp_cmd measure_iaq_cmd;
+	enum sgp_cmd measure_gas_signals_cmd;
+	union sgp_reading buffer;
+	union sgp_reading iaq_buffer;
+	enum _iaq_buffer_state iaq_buffer_state;
+};
+
+struct sgp_device {
+	const struct iio_chan_spec *channels;
+	int num_channels;
+};
+
+static const struct sgp_version

supported_versions_sgp30[]
= {

drivers/iio/chemical/sgp30.c new file +7 -0
+	{
+		.major = 1,
+		.minor = 0,
+	},
+};
+
+static const struct sgp_version

supported_versions_sgpc3[]
= {

drivers/iio/chemical/sgp30.c new file +12 -0
+	{
+		.major = 0,
+		.minor = 4,
+	},
+};
+
+static const struct iio_chan_spec sgp30_channels[] = {
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_VOC,
+		.modified = 1,
+		.info_mask_separate =

BIT(IIO_CHAN_INFO_PROCESSED),

drivers/iio/chemical/sgp30.c new file +7 -0
+		.address = SGP30_IAQ_TVOC_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_CO2,
+		.modified = 1,
+		.info_mask_separate =

BIT(IIO_CHAN_INFO_PROCESSED),

drivers/iio/chemical/sgp30.c

Quoted 8 lines
drivers/iio/chemical/sgp30.c new file +8 -0
+		.address = SGP30_IAQ_CO2EQ_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_ETHANOL,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),    

Definitely looks like it'll fit in under 80 chars.

drivers/iio/chemical/sgp30.c

Quoted 11 lines
drivers/iio/chemical/sgp30.c new file +11 -0
+		.address = SGP30_SIG_ETOH_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_H2,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),    

Same here on length.

Quoted 14 lines
drivers/iio/chemical/sgp30.c new file +13 -0
+		.address = SGP30_SIG_H2_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+};
+
+static const struct iio_chan_spec sgpc3_channels[] = {
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_VOC,
+		.modified = 1,
+		.info_mask_separate =

BIT(IIO_CHAN_INFO_PROCESSED),

drivers/iio/chemical/sgp30.c

Quoted 8 lines
drivers/iio/chemical/sgp30.c new file +8 -0
+		.address = SGPC3_IAQ_TVOC_IDX,
+	},
+	{
+		.type = IIO_CONCENTRATION,
+		.channel2 = IIO_MOD_ETHANOL,
+		.modified = 1,
+		.info_mask_separate =
+			BIT(IIO_CHAN_INFO_RAW),    

And another on length.

Quoted 561 lines
drivers/iio/chemical/sgp30.c new file +11 -0
+		.address = SGPC3_SIG_ETOH_IDX,
+		.scan_type = {
+			.endianness = IIO_BE,
+		},
+	},
+};
+
+static const struct sgp_device sgp_devices[] = {
+	[SGP30] = {
+		.channels = sgp30_channels,
+		.num_channels =

ARRAY_SIZE(sgp30_channels),

drivers/iio/chemical/sgp30.c new file +4 -0
+	},
+	[SGPC3] = {
+		.channels = sgpc3_channels,
+		.num_channels =

ARRAY_SIZE(sgpc3_channels),

drivers/iio/chemical/sgp30.c new file +5 -0
+	},
+};
+
+/**
+ * sgp_verify_buffer() - verify the checksums of the

data
buffer
words
+ *
+ * @data: SGP data
+ * @buf: Raw data buffer
+ * @word_count: Num data words stored in the buffer, excluding CRC
bytes
+ *
+ * Return: 0 on success, negative error
otherwise.
+ */
+static int sgp_verify_buffer(const struct sgp_data *data,

drivers/iio/chemical/sgp30.c new file +1 -0
+			     union sgp_reading *buf,

size_t
word_count)

drivers/iio/chemical/sgp30.c new file +2 -0
+{
+	size_t size = word_count * (SGP_WORD_LEN +

SGP_CRC8_LEN);

drivers/iio/chemical/sgp30.c new file +5 -0
+	int i;
+	u8 crc;
+	u8 *data_buf = &buf->start;
+
+	for (i = 0; i < size; i += SGP_WORD_LEN +

SGP_CRC8_LEN)
{

drivers/iio/chemical/sgp30.c new file +1 -0
+		crc = crc8(sgp_crc8_table,

&data_buf[i],
SGP_WORD_LEN,

drivers/iio/chemical/sgp30.c new file +2 -0
+			   SGP_CRC8_INIT);
+		if (crc != data_buf[i + SGP_WORD_LEN])

{

drivers/iio/chemical/sgp30.c new file +1 -0
+			dev_err(&data->client->dev,

"CRC
error\n");

drivers/iio/chemical/sgp30.c new file +9 -0
+			return -EIO;
+		}
+	}
+
+	return 0;
+}
+
+/**
+ * sgp_read_cmd() - reads data from sensor after

issuing a
command
+ * The caller must hold data->data_lock for the
duration
of the
call.

+ * @data:        SGP data
+ * @cmd:         SGP Command to issue
+ * @buf:         Raw data buffer to use
+ * @word_count:  Num words to read, excluding CRC

bytes
+ *
+ * Return: 0 on success, negative error
otherwise.
+ */
+static int sgp_read_cmd(struct sgp_data *data, enum sgp_cmd cmd,

drivers/iio/chemical/sgp30.c new file +1 -0
+			union sgp_reading *buf, size_t

word_count,

drivers/iio/chemical/sgp30.c new file +5 -0
+			unsigned long duration_us)
+{
+	int ret;
+	struct i2c_client *client = data->client;
+	size_t size = word_count * (SGP_WORD_LEN +

SGP_CRC8_LEN);

drivers/iio/chemical/sgp30.c new file +3 -0
+	u8 *data_buf;
+
+	ret = i2c_master_send(client, (const char

*)&cmd,
SGP_CMD_LEN);

drivers/iio/chemical/sgp30.c new file +15 -0
+	if (ret != SGP_CMD_LEN)
+		return -EIO;
+	usleep_range(duration_us, duration_us + 1000);
+
+	if (word_count == 0)
+		return 0;
+
+	data_buf = &buf->start;
+	ret = i2c_master_recv(client, data_buf, size);
+	if (ret < 0)
+		return ret;
+	if (ret != size)
+		return -EIO;
+
+	return sgp_verify_buffer(data, buf,

word_count);

drivers/iio/chemical/sgp30.c new file +4 -0
+}
+
+/**
+ * sgp_measure_iaq() - measure and retrieve IAQ values

from sensor
+ * The caller must hold data->data_lock for the
duration
of the
call.
+ * @data: SGP data
+ *
+ * Return: 0 on success, -EBUSY on default
values,
negative
error
+ * otherwise.
+ */

drivers/iio/chemical/sgp30.c new file +6 -0
+
+static int sgp_measure_iaq(struct sgp_data *data)
+{
+	int ret;
+	/* data contains default values */
+	bool default_vals = !time_after(jiffies,

data-

Quoted 1 line

iaq_init_start_jiffies +

drivers/iio/chemical/sgp30.c new file +1 -0
+						 data- 
Quoted 2 lines

iaq_defval_skip_jiffies);

drivers/iio/chemical/sgp30.c new file +2 -0
+
+	ret = sgp_read_cmd(data, data->measure_iaq_cmd,

&data-

Quoted 1 line

iaq_buffer,

drivers/iio/chemical/sgp30.c new file +1 -0
+			   SGP_MEASUREMENT_LEN,

SGP_MEASUREMENT_DURATION_US);

drivers/iio/chemical/sgp30.c new file +4 -0
+	if (ret < 0)
+		return ret;
+
+	data->iaq_buffer_state =

IAQ_BUFFER_DEFAULT_VALS;

drivers/iio/chemical/sgp30.c new file +10 -0
+
+	if (default_vals)
+		return -EBUSY;
+
+	data->iaq_buffer_state = IAQ_BUFFER_VALID;
+
+	return 0;
+}
+
+static void sgp_iaq_thread_sleep_until(const struct

sgp_data
*data,

drivers/iio/chemical/sgp30.c new file +1 -0
+				       unsigned long

sleep_jiffies)

drivers/iio/chemical/sgp30.c new file +5 -0
+{
+	const long IAQ_POLL = 50000;
+
+	while (!time_after(jiffies, sleep_jiffies)) {
+		usleep_range(IAQ_POLL, IAQ_POLL +

10000);

drivers/iio/chemical/sgp30.c new file +1 -0
+		if (kthread_should_stop() || data-    
Quoted 1 line

iaq_init_start_jiffies == 0)

drivers/iio/chemical/sgp30.c new file +13 -0
+			return;
+	}
+}
+
+static int sgp_iaq_threadfn(void *p)
+{
+	struct sgp_data *data = (struct sgp_data *)p;
+	unsigned long next_update_jiffies;
+	int ret;
+
+	while (!kthread_should_stop()) {
+		mutex_lock(&data->data_lock);
+		if (data->iaq_init_start_jiffies == 0)

{

drivers/iio/chemical/sgp30.c new file +1 -0
+			ret = sgp_read_cmd(data,

data-

Quoted 1 line

iaq_init_cmd,

NULL, 0,

drivers/iio/chemical/sgp30.c new file +1 -0
+					   SGP_CMD_DURA

TION_US)
;

drivers/iio/chemical/sgp30.c new file +2 -0
+			if (ret < 0)
+				goto

unlock_sleep_continue;

drivers/iio/chemical/sgp30.c new file +1 -0
+			data->iaq_init_start_jiffies =

jiffies;

drivers/iio/chemical/sgp30.c new file +6 -0
+		}
+
+		ret = sgp_measure_iaq(data);
+		if (ret && ret != -EBUSY) {
+			dev_warn(&data->client->dev,
+				 "IAQ measurement error

[%d]\n", ret);

drivers/iio/chemical/sgp30.c new file +3 -0
+		}
+unlock_sleep_continue:
+		next_update_jiffies = jiffies +

data-

Quoted 1 line

measure_interval_jiffies;

drivers/iio/chemical/sgp30.c new file +2 -0
+		mutex_unlock(&data->data_lock);
+		sgp_iaq_thread_sleep_until(data,

next_update_jiffies);

drivers/iio/chemical/sgp30.c new file +7 -0
+	}
+
+	return 0;
+}
+
+static int sgp_read_raw(struct iio_dev *indio_dev,
+			struct iio_chan_spec const

*chan, int
*val,

drivers/iio/chemical/sgp30.c new file +10 -0
+			int *val2, long mask)
+{
+	struct sgp_data *data = iio_priv(indio_dev);
+	struct sgp_crc_word *words;
+	int ret;
+
+	switch (mask) {
+	case IIO_CHAN_INFO_PROCESSED:
+		mutex_lock(&data->data_lock);
+		if (data->iaq_buffer_state !=

IAQ_BUFFER_VALID)
{

drivers/iio/chemical/sgp30.c new file +9 -0
+			mutex_unlock(&data->data_lock);
+			return -EBUSY;
+		}
+		words = data->iaq_buffer.raw_words;
+		switch (chan->address) {
+		case SGP30_IAQ_TVOC_IDX:
+		case SGPC3_IAQ_TVOC_IDX:
+			*val = 0;
+			*val2 =

be16_to_cpu(words[1].value);

drivers/iio/chemical/sgp30.c new file +5 -0
+			ret = IIO_VAL_INT_PLUS_NANO;
+			break;
+		case SGP30_IAQ_CO2EQ_IDX:
+			*val = 0;
+			*val2 =

be16_to_cpu(words[0].value);

drivers/iio/chemical/sgp30.c new file +11 -0
+			ret = IIO_VAL_INT_PLUS_MICRO;
+			break;
+		default:
+			ret = -EINVAL;
+			break;
+		}
+		mutex_unlock(&data->data_lock);
+		break;
+	case IIO_CHAN_INFO_RAW:
+		mutex_lock(&data->data_lock);
+		if (chan->address ==

SGPC3_SIG_ETOH_IDX) {

drivers/iio/chemical/sgp30.c new file +1 -0
+			if (data->iaq_buffer_state ==

IAQ_BUFFER_EMPTY)

drivers/iio/chemical/sgp30.c new file +4 -0
+				ret = -EBUSY;
+			else
+				ret = 0;
+			words = data-
Quoted 1 line

iaq_buffer.raw_words;

drivers/iio/chemical/sgp30.c new file +2 -0
+		} else {
+			ret = sgp_read_cmd(data,

data-

Quoted 1 line

measure_gas_signals_cmd,

drivers/iio/chemical/sgp30.c new file +1 -0
+					   &data-
Quoted 1 line

buffer,

SGP_MEASUREMENT_LEN,

drivers/iio/chemical/sgp30.c new file +1 -0
+					   SGP_MEASUREM

ENT_DURA
TION_US)
;

drivers/iio/chemical/sgp30.c new file +10 -0
+			words = data->buffer.raw_words;
+		}
+		if (ret) {
+			mutex_unlock(&data->data_lock);
+			return ret;
+		}
+
+		switch (chan->address) {
+		case SGP30_SIG_ETOH_IDX:
+			*val =

be16_to_cpu(words[1].value);

drivers/iio/chemical/sgp30.c new file +5 -0
+			ret = IIO_VAL_INT;
+			break;
+		case SGPC3_SIG_ETOH_IDX:
+		case SGP30_SIG_H2_IDX:
+			*val =

be16_to_cpu(words[0].value);

drivers/iio/chemical/sgp30.c new file +27 -0
+			ret = IIO_VAL_INT;
+			break;
+		default:
+			ret = -EINVAL;
+			break;
+		}
+		mutex_unlock(&data->data_lock);
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return ret;
+}
+
+static int sgp_check_compat(struct sgp_data *data,
+			    unsigned int product_id)
+{
+	const struct sgp_version *supported_versions;
+	u16 ix, num_fs;
+	u16 product, generation, major, minor;
+
+	/* driver does not match product */
+	generation = SGP_VERS_GEN(data);
+	if (generation != 0) {
+		dev_err(&data->client->dev,
+			"incompatible product

generation %d !=
0",
generation);

drivers/iio/chemical/sgp30.c new file +7 -0
+		return -ENODEV;
+	}
+
+	product = SGP_VERS_PRODUCT(data);
+	if (product != product_id) {
+		dev_err(&data->client->dev,
+			"sensor reports a different

product:
0x%04hx\n",

drivers/iio/chemical/sgp30.c new file +6 -0
+			product);
+		return -ENODEV;
+	}
+
+	if (SGP_VERS_RESERVED(data))
+		dev_warn(&data->client->dev, "reserved

bit is
set\n");

drivers/iio/chemical/sgp30.c new file +2 -0
+
+	/* engineering samples are not supported: no

interface
guarantees */

drivers/iio/chemical/sgp30.c new file +6 -0
+	if (SGP_VERS_ENG_BIT(data))
+		return -ENODEV;
+
+	switch (product) {
+	case SGP30:
+		supported_versions =

supported_versions_sgp30;

drivers/iio/chemical/sgp30.c new file +1 -0
+		num_fs =

ARRAY_SIZE(supported_versions_sgp30);

drivers/iio/chemical/sgp30.c new file +3 -0
+		break;
+	case SGPC3:
+		supported_versions =

supported_versions_sgpc3;

drivers/iio/chemical/sgp30.c new file +1 -0
+		num_fs =

ARRAY_SIZE(supported_versions_sgpc3);

drivers/iio/chemical/sgp30.c new file +9 -0
+		break;
+	default:
+		return -ENODEV;
+	}
+
+	major = SGP_VERS_MAJOR(data);
+	minor = SGP_VERS_MINOR(data);
+	for (ix = 0; ix < num_fs; ix++) {
+		if (major ==

supported_versions[ix].major &&

drivers/iio/chemical/sgp30.c new file +1 -0
+		    minor >=

supported_versions[ix].minor)

drivers/iio/chemical/sgp30.c new file +3 -0
+			return 0;
+	}
+	dev_err(&data->client->dev, "unsupported sgp

version:
%d.%d\n",

drivers/iio/chemical/sgp30.c new file +13 -0
+		major, minor);
+
+	return -ENODEV;
+}
+
+static void sgp_init(struct sgp_data *data)
+{
+	data->iaq_init_cmd = SGP_CMD_IAQ_INIT;
+	data->iaq_init_start_jiffies = 0;
+	data->iaq_buffer_state = IAQ_BUFFER_EMPTY;
+	switch (SGP_VERS_PRODUCT(data)) {
+	case SGP30:
+		data->measure_interval_jiffies =

SGP30_MEASURE_INTERVAL_HZ * HZ;

drivers/iio/chemical/sgp30.c new file +1 -0
+		data->measure_iaq_cmd =

SGP_CMD_IAQ_MEASURE;

drivers/iio/chemical/sgp30.c new file +1 -0
+		data->measure_gas_signals_cmd =

SGP30_CMD_MEASURE_SIGNAL;

drivers/iio/chemical/sgp30.c new file +2 -0
+		data->product_id = SGP30;
+		data->iaq_defval_skip_jiffies = 15 *

HZ;

drivers/iio/chemical/sgp30.c new file +3 -0
+		break;
+	case SGPC3:
+		data->measure_interval_jiffies =

SGPC3_MEASURE_INTERVAL_HZ * HZ;

drivers/iio/chemical/sgp30.c new file +1 -0
+		data->measure_iaq_cmd =

SGPC3_CMD_MEASURE_RAW;

drivers/iio/chemical/sgp30.c new file +1 -0
+		data->measure_gas_signals_cmd =

SGPC3_CMD_MEASURE_RAW;

drivers/iio/chemical/sgp30.c new file +3 -0
+		data->product_id = SGPC3;
+		data->iaq_defval_skip_jiffies =
+			43 * data-
Quoted 1 line

measure_interval_jiffies;

drivers/iio/chemical/sgp30.c new file +10 -0
+		break;
+	};
+}
+
+static const struct iio_info sgp_info = {
+	.read_raw	= sgp_read_raw,
+};
+
+static const struct of_device_id sgp_dt_ids[] = {
+	{ .compatible = "sensirion,sgp30", .data =

(void
*)SGP30 },

drivers/iio/chemical/sgp30.c new file +1 -0
+	{ .compatible = "sensirion,sgpc3", .data =

(void
*)SGPC3 },

drivers/iio/chemical/sgp30.c new file +13 -0
+	{ }
+};
+
+static int sgp_probe(struct i2c_client *client,
+		     const struct i2c_device_id *id)
+{
+	struct iio_dev *indio_dev;
+	struct sgp_data *data;
+	const struct of_device_id *of_id;
+	unsigned long product_id;
+	int ret;
+
+	indio_dev = devm_iio_device_alloc(&client->dev,

sizeof(*data));

drivers/iio/chemical/sgp30.c new file +4 -0
+	if (!indio_dev)
+		return -ENOMEM;
+
+	of_id = of_match_device(sgp_dt_ids, &client-
Quoted 1 line

dev);

drivers/iio/chemical/sgp30.c new file +2 -0
+	if (of_id)
+		product_id = (unsigned long)of_id-
Quoted 1 line

data;

drivers/iio/chemical/sgp30.c new file +7 -0
+	else
+		product_id = id->driver_data;
+
+	data = iio_priv(indio_dev);
+	i2c_set_clientdata(client, indio_dev);
+	data->client = client;
+	crc8_populate_msb(sgp_crc8_table,

SGP_CRC8_POLYNOMIAL);

drivers/iio/chemical/sgp30.c new file +3 -0
+	mutex_init(&data->data_lock);
+
+	/* get feature set version and write it to

client data
*/

drivers/iio/chemical/sgp30.c new file +1 -0
+	ret = sgp_read_cmd(data,

SGP_CMD_GET_FEATURE_SET,
&data-

Quoted 1 line

buffer, 1,

drivers/iio/chemical/sgp30.c new file +5 -0
+			   SGP_CMD_DURATION_US);
+	if (ret < 0)
+		return ret;
+
+	data->feature_set = be16_to_cpu(data-    
Quoted 1 line

buffer.raw_words[0].value);

drivers/iio/chemical/sgp30.c new file +10 -0
+
+	ret = sgp_check_compat(data, product_id);
+	if (ret)
+		return ret;
+
+	indio_dev->dev.parent = &client->dev;
+	indio_dev->info = &sgp_info;
+	indio_dev->name = id->name;
+	indio_dev->modes = INDIO_DIRECT_MODE;
+	indio_dev->channels =

sgp_devices[product_id].channels;

drivers/iio/chemical/sgp30.c new file +1 -0
+	indio_dev->num_channels =

sgp_devices[product_id].num_channels;

drivers/iio/chemical/sgp30.c new file +4 -0
+
+	sgp_init(data);
+
+	ret = devm_iio_device_register(&client->dev,

indio_dev);

drivers/iio/chemical/sgp30.c new file +2 -0
+	if (ret) {
+		dev_err(&client->dev, "failed to

register iio
device\n");

drivers/iio/chemical/sgp30.c new file +4 -0
+		return ret;
+	}
+
+	data->iaq_thread =

kthread_run(sgp_iaq_threadfn, data,

drivers/iio/chemical/sgp30.c new file +1 -0
+				       "%s-iaq", data-
Quoted 2 lines

client-
name);

drivers/iio/chemical/sgp30.c new file +7 -0
+
+	return 0;
+}
+
+static int sgp_remove(struct i2c_client *client)
+{
+	struct iio_dev *indio_dev =

i2c_get_clientdata(client);

drivers/iio/chemical/sgp30.c new file +21 -0
+	struct sgp_data *data = iio_priv(indio_dev);
+
+	if (data->iaq_thread)
+		kthread_stop(data->iaq_thread);
+
+	return 0;
+}
+
+static const struct i2c_device_id sgp_id[] = {
+	{ "sgp30", SGP30 },
+	{ "sgpc3", SGPC3 },
+	{ }
+};
+
+MODULE_DEVICE_TABLE(i2c, sgp_id);
+MODULE_DEVICE_TABLE(of, sgp_dt_ids);
+
+static struct i2c_driver sgp_driver = {
+	.driver = {
+		.name = "sgp30",
+		.of_match_table =

of_match_ptr(sgp_dt_ids),

drivers/iio/chemical/sgp30.c new file +8 -0
+	},
+	.probe = sgp_probe,
+	.remove = sgp_remove,
+	.id_table = sgp_id,
+};
+module_i2c_driver(sgp_driver);
+
+MODULE_AUTHOR("Andreas Brauchli <  

[email protected]>")
;
+MODULE_AUTHOR("Pascal Sachs <
[email protected]>")
;

drivers/iio/chemical/sgp30.c new file +2 -0
+MODULE_DESCRIPTION("Sensirion SGP gas sensors");
+MODULE_LICENSE("GPL v2");