mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Akshat Chandra <notmissinge@gmail.com>
Cc: linux-iio@vger.kernel.org, jic23@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: Mon, 16 Nov 2026 14:20:50 +0200	[thread overview]
Message-ID: <avr1IgExD8Rmt4eP@ashevche-desk.local> (raw)
In-Reply-To: <20261115055941.362185-3-notmissinge@gmail.com>

On Sun, Nov 15, 2026 at 11:29:40AM +0530, Akshat Chandra 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.

...

> +config SENSIRION_SGP41
> +	tristate "Sensirion SGP41 gas sensor"

Make sure this text is the same or quite similar to what is used in the top
comment and MODULE_DESCRIPTION().

> +	depends on I2C
> +	select CRC8
> +	help
> +	  Say Y here to build I2C interface to support Sensirion SGP41 gas
> +	  sensor
> +
> +	  To compile this driver as module, choose M here: the
> +	  module will be called sgp41.

...

> +/*
> + * sgp41 - Support for Sensirion SGP41 Gas Sensor

Avoid mentioning name file in the file.

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

Wrong indentation. Also note that you are supposed to follow IWYU principle,
for instance bits.h is absent.

> +#include <linux/crc8.h>
> +#include <linux/delay.h>
> +#include <linux/i2c.h>
> +#include <linux/iio/iio.h>
> +#include <linux/module.h>
> +#include <linux/mutex.h>
> +#include <linux/regulator/consumer.h>
> +#include <linux/types.h>

...

> +struct sgp41_data {

> +	struct device *dev;
> +	struct i2c_client *client;

One of them is redundant.

> +	int rht;
> +	int temp;
> +
> +	int voc_calibbias;
> +
> +	struct mutex lock; /* Protects sensor state and I2C transaction.*/
> +	bool conditioned;
> +	u8 condition_count; /*Counter for conditioning_cmd sent.*/

All over the file you have an issues with indentation.

> +};

...

> +struct sgp41_tg_measure {
> +	u8 command[2];

Is it really 2 independent bytes?

> +	__be16 rht_ticks;
> +	u8 rht_crc;
> +	__be16 temp_ticks;
> +	u8 temp_crc;

This...

> +} __packed;
> +
> +struct sgp41_tg_result {
> +	__be16 voc_res_ticks;
> +	u8 voc_res_crc;
> +	__be16 nox_res_ticks;
> +	u8 nox_res_crc;
> +} __packed;

...and this are the same. Why not to abstract?

// find better names
struct sgp41_result {
	__be16 a_ticks;
	u8 a_crc;
	__be16 b_ticks;
	u8 b_crc;
} __packed;

struct sgp41_tg_measure {
	u8 command[2];
	struct sgp41_result tg;
} __packed;

struct sgp41_tg_result {
	struct sgp41_result tg;
} __packed;

(note, I don't know which approach is better, just a question)?

...

> +static u32 sgp41_exp(int exp, u32 power, u32 rounds)
> +{
> +	u32 x, y, xp;
> +	u32 factorial, divider, xmax;
> +	int sign = 1;
> +	u32 i;
> +
> +	if (exp == 0) {
> +		return 1 << power;
> +	} else if (exp < 0) {
> +		sign = -1;
> +		exp *= -1;
> +	}
> +
> +	xmax = 0x7FFFFFFF / exp;
> +	x = exp;
> +	xp = 1;
> +	factorial = 1;
> +	y = 1 << power;
> +	divider = 0;
> +
> +	for (i = 1; i <= rounds; i++) {
> +		xp *= x;
> +		factorial *= i;
> +		y += (xp >> divider) / factorial;
> +		divider += power;
> +		/* divide when next multiplication would overflow */
> +		if (xp >= xmax) {
> +			xp >>= power;
> +			divider -= power;
> +		}
> +	}
> +
> +	if (sign == -1)
> +		return (1 << (power * 2)) / y;
> +	else
> +		return y;
> +}

This really requires to get some comments to explain the math behind. Also we
have intlog10(), rational_best_approximation(), and other nice math-related
helpers. Can you check if anything may help you with this?

...

> +static void temp_rht_payload(struct sgp41_data *data,
> +			     struct sgp41_tg_measure *tg)
> +{
> +	s64 ticks;
> +	u16 ticks16;
> +
> +	ticks = div_s64((s64)data->rht * 65535, 100000);
> +	ticks16 = (u16)clamp_t(s64, ticks, 0, 65535);
> +	tg->rht_ticks = cpu_to_be16(ticks16);
> +	tg->rht_crc = crc8(sgp41_crc8_table, (u8 *)&tg->rht_ticks, 2,
> +			   SGP41_CRC8_INIT);
> +
> +	ticks = div_s64((s64)(data->temp + 45000) * 65535, 175000);
> +	ticks16 = (u16)clamp_t(s64, ticks, 0, 65535);

No _t variant. And why do you need a casting after that?

> +	tg->temp_ticks = cpu_to_be16(ticks16);
> +	tg->temp_crc = crc8(sgp41_crc8_table, (u8 *)&tg->temp_ticks, 2,
> +			    SGP41_CRC8_INIT);
> +}

...

> +static int sgp41_calc_voc(struct sgp41_data *data, u16 voc_raw, int *voc)
> +{
> +	int x;
> +	u32 exp = 0;
> +	int voc_calibbias;
> +
> +	/* we calculate as a multiple of 16384 (2^14) */
> +	scoped_guard(mutex, &data->lock)
> +		voc_calibbias = data->voc_calibbias;
> +
> +	x = ((int)voc_raw - voc_calibbias) * 106;

Any type (signed or unsigned) shorter than int will be promoted to *signed*
int, no need to have this casting.

> +		/* voc = 500 / (1 + e^x) */
> +	exp = sgp41_exp(x, SGP41_CALC_POWER, 18);

Also look at nexp_u32() and sgp40 driver, perhaps we need to extract something
nice to math library?

> +	*voc = 500 * ((1 << (SGP41_CALC_POWER * 2)) / ((1 << SGP41_CALC_POWER) + exp));
> +
> +	dev_dbg(data->dev, "voc_raw: %d voc_calibbias: %d x: %d exp: %u voc: %d\n",
> +		voc_raw, data->voc_calibbias, x, exp, *voc);
> +
> +	return 0;
> +}

...

> +static int sgp41_run_conditioning(struct sgp41_data *data)
> +{
> +	struct sgp41_tg_measure tg = {
> +		.command = { 0x26, 0x12 },
> +	};
> +	int i;

Why signed?

> +	int ret;

> +	for (i = 0; i < SGP41_COND_ITERATIONS; i++) {

i is not used outside, hence

	for (unsigned int i = 0; i < SGP41_COND_ITERATIONS; i++) {

> +		if (data->condition_count!= i)
> +			i = data->condition_count;
> +
> +		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;
> +		}

Unnest the conditionals.

> +		data->condition_count++;
> +		msleep(50);

> +		if (i != SGP41_COND_ITERATIONS - 1)
> +			msleep(950);

This is invariant to the loop, also, why so long? Each sleep (especially this
long) must be explained.

> +	}
> +
> +	data->conditioned = true;
> +
> +	return 0;
> +}

...

> +		switch (chan->channel2) {
> +		case IIO_MOD_VOC:
> +			ret = sgp41_measure_raw(data, &voc_raw, &nox_raw);
> +			if (ret)
> +				return ret;
> +
> +			ret = sgp41_calc_voc(data, voc_raw, &voc);
> +			if (ret)
> +				return ret;

> +			*val = voc / (1 << SGP41_CALC_POWER);

			*val = voc >> SGP41_CALC_POWER;

> +		/*
> +		 * calculation should fit into integer, where:
> +		 * voc <= (500 * 2^SGP41_CALC_POWER) = 8192000
> +		 * (with SGP41_CALC_POWER = 14)
> +		 */
> +		*val2 = ((voc % (1 << SGP41_CALC_POWER)) * 244) /
> +			(1 << (SGP41_CALC_POWER - 12));
> +		dev_dbg(data->dev, "voc: %d val: %d.%06d\n",
> +			voc, *val, *val2);
> +		return IIO_VAL_INT_PLUS_MICRO;
> +		default:
> +			return -EINVAL;
> +		}

How many AI agents wrote this all crap?

...

I stop it here, this driver needs much more work. Please, don't rush, take your
time to make it shiny first.

-- 
With Best Regards,
Andy Shevchenko



      parent reply	other threads:[~2026-11-16 12:20 UTC|newest]

Thread overview: 5+ 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 [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=avr1IgExD8Rmt4eP@ashevche-desk.local \
    --to=andriy.shevchenko@intel.com \
    --cc=andy@kernel.org \
    --cc=dlechner@baylibre.com \
    --cc=jic23@kernel.org \
    --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®