mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: Akshat Chandra <notmissinge@gmail.com>
Cc: linux-iio@vger.kernel.org, andy@kernel.org,
	"Lukasz Kulga" <lukasz.kulga@gmail.com>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 2/2] iio: chemical: Add support for Sensirion SGP41
Date: Sun, 22 Nov 2026 20:31:01 +0000	[thread overview]
Message-ID: <20261122203101.453545fd@jic23-hlaptop> (raw)
In-Reply-To: <20261115055941.362185-3-notmissinge@gmail.com>

On Sun, 15 Nov 2026 11:29:40 +0530
Akshat Chandra <notmissinge@gmail.com> wrote:

> Add a driver for the Sensirion SGP41 VOC and NOx gas sensor,
> connected over I2C.
> 
> Expose raw VOC and NOx signals as resistance channels and
> allow userspace to write temperature and relative humidity
> compensation values.
> 
> Run sensor conditioning lazily before the first raw measurement.
> Calculate an estimated VOC index and leave NOx-index calculation
> to userspace.
> 
> Signed-off-by: Akshat Chandra <notmissinge@gmail.com>
Having read Andy's reply I considered skipping this entirely
as indeed it has a lot of simple stuff wrong.

Anyhow, this is at most a quick glance to highlight a few things
that jumped out at me.

>
> diff --git a/drivers/iio/chemical/sgp41.c b/drivers/iio/chemical/sgp41.c
> new file mode 100644
> index 000000000000..f094ba22b7aa
> --- /dev/null
> +++ b/drivers/iio/chemical/sgp41.c
> @@ -0,0 +1,542 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * sgp41 - Support for Sensirion SGP41 Gas Sensor
> + *
> + * Copyright (C) 2026 Akshat Chandra <notmissinge@gmail.com>
> + *
> + * I2C slave address: 0x59
> + *
> + * Datasheet can be found here:
> + * https://www.sensirion.com/file/datasheet_sgp41
> + *
> + * There are two functionalities supported:
> + *
> + * 1) read raw logarithmic resistance value from sensor
> + *    --> useful to pass it to the algorithm of the sensor vendor for
> + *    measuring deteriorations and improvements of air quality.
> + *    The values can be read from the in_resistance_voc_raw and
> + *    in_resistance_nox_raw.
> + *
> + * 2) calculate an estimated VOC index (0 - 500) and expose it through
> + *    the in_concentration_voc_input attribute.
> + *
> + *    The VOC index is calculated using the raw VOC resistance and a
> + *    configurable calibration bias, exposed through
> + *    in_resistance_voc_calibbias (default 30000).
> + *
> + * The driver does not calculate a NOx index.

Generally don't bother saying what is not done.  Those sorts
of comments tend to become wrong fast!

> + *
> + * Conditioning runs before the first raw measurement, once per second
> + * for a total of 10 seconds.
> + *
> + * Compensation values for relative humidity and temperature can be
> + * configured through the corresponding output attributes.
> + *
> + * The sensor heater is turned off when the device is removed.
> + */
> +
> +#include<linux/cleanup.h>
This sort of formatting bug shows that you need to be a lot more
careful to run standard tests and read your own code before submitting.

Oddly this rather convinces me this isn't AI generated as the
LLMs tend not to make that sort of formatting error.


> +
> +static int sgp41_run_conditioning(struct sgp41_data *data)
> +{
> +	struct sgp41_tg_measure tg = {
> +		.command = { 0x26, 0x12 },
> +	};
> +	int i;
> +	int ret;
> +
> +	for (i = 0; i < SGP41_COND_ITERATIONS; i++) {
	for (unsigned int i = 0;
> +		if (data->condition_count!= i)

Fix formatting to conform to kernel style.

> +			i = data->condition_count;

This is unusual enough that it needs a comment on why.

> +
> +		temp_rht_payload(data, &tg);
> +		ret = i2c_master_send(data->client, (const char *)&tg,
> +				      sizeof(tg));
> +		if (ret != sizeof(tg)) {
> +			dev_warn(data->dev,
> +				 "i2c_master_send ret: %d sizeof: %zu\n",
> +				 ret, sizeof(tg));
> +			return ret < 0 ? ret : -EIO;
> +		}
> +		data->condition_count++;
> +		msleep(50);
> +
> +		if (i != SGP41_COND_ITERATIONS - 1)
> +			msleep(950);
> +	}
> +
> +	data->conditioned = true;
> +
> +	return 0;
> +}

> +
> +static int sgp41_measure_raw(struct sgp41_data *data,
> +			     u16 *voc_raw, u16 *nox_raw)
> +{
> +	struct sgp41_tg_measure tg = {
> +		.command = { 0x26, 0x19 },

What are these magic numbers? If they are command codes I'd normally
expect to see some defines for all the ones that exist at the top
of the file.  Hopefully they have names that make it obvious what
is going on here.

> +	};
> +	struct sgp41_tg_result tgres;
> +	u8 crc;
> +	int ret;

If nothing else sets order, reverse xmas tree preferred.

> +
> +	guard(mutex)(&data->lock);
> +
> +	if (!data->conditioned) {
> +		ret = sgp41_run_conditioning(data);
> +		if (ret) {
> +			sgp41_heater_off(data);

There is no code in this function turning the heater on.
If it's in run_conditioning() then I'd expect that to be side
effect free on returning an error. I.e. that function gets to do
the heater_off.

> +			return ret;
> +		}
> +	}

> +
> +static int sgp41_write_raw(struct iio_dev *indio_dev,
> +			   struct iio_chan_spec const *chan, int val,
> +			   int val2, long mask)
> +{
> +	struct sgp41_data *data = iio_priv(indio_dev);
> +
> +	switch (mask) {
> +	case IIO_CHAN_INFO_RAW:
> +		switch (chan->type) {
> +		case IIO_TEMP: {
> +			if (val < -45000 || val > 130000)
> +				return -EINVAL;
> +
> +			guard(mutex)(&data->lock);
> +			data->temp = val;
> +			return 0;
> +		}
> +		case IIO_HUMIDITYRELATIVE: {
> +			if (val < 0 || val > 100000)
> +				return -EINVAL;
> +
> +			guard(mutex)(&data->lock);
> +			data->rht = val;

This is unusual enough probably needs a specific documentation
file for the driver. I guess calibration requires rht and temp?

Doing that via output channels is fine but still not that common.

> +			return 0;
> +		}
> +		default:
> +			return -EINVAL;
> +		}
> +
> +	case IIO_CHAN_INFO_CALIBBIAS:
> +		switch (chan->channel) {
> +		/*For VOC Calibration,*/

See kernel comment style. It's not quite this.

> +		case SRAW_VOC: {
> +			if (val < 20000 || val > 52768)
> +				return -EINVAL;
> +
> +			guard(mutex)(&data->lock);
> +			data->voc_calibbias = val;
> +			return 0;
> +		}
> +		default:
> +			return -EINVAL;
> +		}
> +
> +	default:
> +		return -EINVAL;
> +	}
> +}
> +
> +
> +static void sgp41_remove(struct i2c_client *client)
> +{
> +	int ret;
> +	struct iio_dev *indio_dev = i2c_get_clientdata(client);
> +	struct sgp41_data *data = iio_priv(indio_dev);
> +
> +	iio_device_unregister(indio_dev);
> +
> +	guard(mutex)(&data->lock);
> +	ret = sgp41_heater_off(data);
> +	if (ret)
> +	dev_warn(data->dev, "Failed to turn off heater: %d\n", ret);
run almost anything against this code and that should give you a warning.
In general look at how other drivers down powering off of elements on
remove.  devm_add_action_or_reset() called in the appropriate place
will do it for you.

We almost never need an remove() function in a modern IIO driver.

> +}
> +
> +static const struct iio_info sgp41_info = {
> +	.read_raw = sgp41_read_raw,
> +	.write_raw = sgp41_write_raw,
> +};
> +
> +static int sgp41_probe(struct i2c_client *client)
> +{
> +	const struct i2c_device_id *id = i2c_client_get_device_id(client);
> +	struct device *dev = &client->dev;
> +	struct iio_dev *indio_dev;
> +	struct sgp41_data *data;
> +	int ret;
> +
> +	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
> +	if (!indio_dev)
> +		return -ENOMEM;
> +
> +	i2c_set_clientdata(client, indio_dev);
Given no power management (which is fine in a new driver) and
the lack of a need for remove, I'd expect this to go away in v2.
> +
> +	data = iio_priv(indio_dev);
> +	data->client = client;
> +	data->dev = dev;
Why do you need that pointer rather that using client->dev
where appropriate.

> +	data->conditioned = false;
> +	data->condition_count = 0;
data is zero initialized so as long as those are obvious defaults
this probably brings nothing.
> +
> +	crc8_populate_msb(sgp41_crc8_table, SGP41_CRC8_POLYNOMIAL);

This needs to happen only once how ever many copies of this driver
existing. There is infrastructure to doing things once in the kernel
and we have started using that in IIO for crc tables recently.

> +
> +	mutex_init(&data->lock);
	ret = devm_mutex_init();
	if (ret)
		return ret;

Take a look at what this does. It's a minor gain to have the debug logic
available but it is worth it for one line more code!
> +
> +	/*default values.*/
> +	data->rht = 50000;
> +	data->temp = 25000;
> +	data->voc_calibbias = 30000; /*resistance raw value for voc index of 250*/
> +
> +	ret = devm_regulator_get_enable(dev, "vdd");
> +	if (ret)
> +		return dev_err_probe(dev, ret, "failed to get/enable vdd supply\n");
> +
> +	indio_dev->info = &sgp41_info;
> +
> +	if (id)
> +		indio_dev->name = id->name;

Prefer that we don't use id at all.  Just hard code the name
until we get additional supported parts.

> +	else
> +		indio_dev->name = "sgp41";

i.e. Just this option.

> +
> +	indio_dev->modes = INDIO_DIRECT_MODE;
> +	indio_dev->channels = sgp41_channels;
> +	indio_dev->num_channels = ARRAY_SIZE(sgp41_channels);
> +
> +	ret = iio_device_register(indio_dev);
> +	if (ret)
> +		dev_err(dev, "failed to register iio device\n");
> +
> +	return ret;
> +}



      parent reply	other threads:[~2026-11-22 20:31 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20261115055941.362185-1-notmissinge@gmail.com>
2026-11-15  5:59 ` [PATCH v2 1/2] dt-bindings: iio: chemical: Add Sensirion SGP41 binding Akshat Chandra
2026-11-16 12:28   ` Krzysztof Kozlowski
2026-11-15  5:59 ` [PATCH v2 2/2] iio: chemical: Add support for Sensirion SGP41 Akshat Chandra
2026-11-15 21:19   ` kernel test robot
2026-11-16 12:20   ` Andy Shevchenko
2026-11-22 20:31   ` Jonathan Cameron [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261122203101.453545fd@jic23-hlaptop \
    --to=jic23@kernel.org \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=lukasz.kulga@gmail.com \
    --cc=notmissinge@gmail.com \
    --cc=nuno.sa@analog.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®