mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jaakko Koivisto <jmatko@utu.fi>
To: Jonathan Cameron <jic23@kernel.org>, Jaakko Koivisto <jmatko@utu.fi>
Cc: "Andreas Klinger" <ak@it-klinger.de>,
	"David Lechner" <dlechner@baylibre.com>,
	"Nuno Sá" <nuno.sa@analog.com>,
	"Andy Shevchenko" <andy@kernel.org>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 2/3] iio: chemical: sgp40: Implement execute_self_test-command
Date: Thu, 24 Sep 2026 11:16:09 +0300	[thread overview]
Message-ID: <DLNEBPFBUOM0.3GKQ8FDOU8YM4@utu.fi> (raw)
In-Reply-To: <20260920184837.2b2ca5dd@jic23-hlaptop>

On Sun Sep 20, 2026 at 8:48 PM EEST, Jonathan Cameron wrote:
> On Fri, 18 Sep 2026 16:40:18 +0300
> Jaakko Koivisto <jmatko@utu.fi> wrote:
>
>> -Run the chip self test routine testing heater and MOX -material.
>> -Log error if the test fails.
>> 
>> Datasheet does not clearly state if the chip is completely unusable if
>> the test fails. Because of this the driver will try to use the chip
>> normally even if the self-test has failed.
>> 
>> Signed-off-by: Jaakko Koivisto <jmatko@utu.fi>
> Hi Jaakko,
>
> A few things inline,
>
> Thanks,
>
> Jonathan
>
>> ---
>>  drivers/iio/chemical/sgp40.c | 53 ++++++++++++++++++++++++++++++++++++
>>  1 file changed, 53 insertions(+)
>> 
>> diff --git a/drivers/iio/chemical/sgp40.c b/drivers/iio/chemical/sgp40.c
>> index c1e2a992ec2a..28d5e737d1dc 100644
>> --- a/drivers/iio/chemical/sgp40.c
>> +++ b/drivers/iio/chemical/sgp40.c
>> @@ -84,6 +84,19 @@ struct sgp40_serial_number_result {
>>  	u8	C_crc;
>>  } __packed;
>>  
>> +/*
>> + * Datasheet table 13.
>> + */
>> +#define SGP40_SELF_TEST_PASS 0xD4
>> +#define SGP40_SELF_TEST_FAIL 0x4B
>> +
>> +struct sgp40_self_test_result {
>> +	u8	data;
>> +	u8	_ignore;
>> +	u8	crc;
>> +} __packed;
>> +
>> +
>>  static const struct iio_chan_spec sgp40_channels[] = {
>>  	{
>>  		.type = IIO_CONCENTRATION,
>> @@ -210,6 +223,42 @@ static int sgp40_get_serial_number(struct sgp40_data *data)
>>  	return 0;
>>  }
>>  
>> +static int sgp40_execute_self_test(struct sgp40_data *data)
>> +{
>> +	int ret;
>> +	struct i2c_client *client = data->client;
>> +	struct sgp40_command run_test = {.command = {0x28, 0x0E}};
>> +	struct sgp40_self_test_result res;
>> +
>> +	ret = i2c_master_send(client, (char*)&run_test, sizeof(run_test.command));
>> +	if (ret != sizeof(run_test.command)) {
>> +		dev_err(data->dev, "i2c_master_send ret: %d, expected %zu", ret, sizeof(run_test.command));
>> +		return -EIO;
> Only called from problem so prefer use of
>
> 		return dev_err_probe().
>
> Also don't eat the error if it returns on (rather than a short access).
>

I will change it, thanks.

>
>> +	}
>> +	msleep(320);
>
> Spec reference needed for any sleep times.
>

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

>> +
>> +	ret = i2c_master_recv(client, (char*)&res, sizeof(res));
>> +	if (ret < 0)
>> +		return ret;
>> +	if (ret != sizeof(res)) {
>> +		dev_err(data->dev, "i2c_master_recv ret: %d, expected: %zu", ret, sizeof(res));
>> +		return -EIO;
>
> return dev_err_probe() and don't eat the error return if there is one.

Will change.

>> +	}
>> +
>> +	if (crc8(sgp40_crc8_table, (u8*)&res, 2, SGP40_CRC8_INIT) != res.crc) {
>> +		dev_warn(data->dev, "CRC error in execute_self_test");
>
> CRC fail indicates broken comms. I'd fail hard on this one.
>
Ditto.

>> +	}
>> +
>> +	dev_dbg(data->dev, "self test result: 0x%x", res.data);
>> +	switch (res.data) {
>> +		case SGP40_SELF_TEST_PASS:
>> +			return 0;
>> +		case SGP40_SELF_TEST_FAIL:
>> +		default:
>> +			return res.data;
>> +	}
>> +}
>> +
>>  static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance_raw)
>>  {
>>  	int ret;
>> @@ -421,6 +470,10 @@ static int sgp40_probe(struct i2c_client *client)
>>  	if (ret)
>>  		dev_warn(dev, "failed to retrieve device serial number\n");
>>  
>> +	ret = sgp40_execute_self_test(data);
>> +	if (ret)
>> +		dev_warn(dev, "device self test failed: ret 0x%x", ret);
>
> There should be a distinction between the test failed to run and failed to pass.
> For failing to run (e.g. comms error) then fail the probe.  Failing to pass
> the test is what you might choose to carry on from.

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

>
>> +
>>  	ret = devm_iio_device_register(dev, indio_dev);
>>  	if (ret)
>>  		dev_err(dev, "failed to register iio device\n");



  reply	other threads:[~2026-09-24  8:16 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-18 13:40 [PATCH 0/3] iio: chemical: sgp40: Add secondary chip functionality Jaakko Koivisto
2026-09-18 13:40 ` [PATCH 1/3] iio: chemical: sgp40: Implement get_serial_number-command Jaakko Koivisto
2026-09-18 14:26   ` Maxwell Doose
2026-09-24  8:58     ` Jaakko Koivisto
2026-09-19 14:09   ` Andy Shevchenko
2026-09-24  8:31     ` Jaakko Koivisto
2026-09-18 13:40 ` [PATCH 2/3] iio: chemical: sgp40: Implement execute_self_test-command Jaakko Koivisto
2026-09-20 17:48   ` Jonathan Cameron
2026-09-24  8:16     ` Jaakko Koivisto [this message]
2026-09-18 13:40 ` [PATCH 3/3] iio: chemical: sgp40: Implement turn_heater_off-command Jaakko Koivisto
2026-09-18 23:36   ` Andreas Klinger
2026-09-20 17:52     ` Jonathan Cameron
2026-09-24  8:04       ` Jaakko Koivisto
2026-09-20 17:53 ` [PATCH 0/3] iio: chemical: sgp40: Add secondary chip functionality Jonathan Cameron
2026-09-24  7:54   ` Jaakko Koivisto

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=DLNEBPFBUOM0.3GKQ8FDOU8YM4@utu.fi \
    --to=jmatko@utu.fi \
    --cc=ak@it-klinger.de \
    --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=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®