mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 5/5] iio: chemical: sgp40: Implement turn_heater_off-command
@ 2026-11-02  9:15 Jaakko Koivisto
  2026-11-02 10:00 ` VS: " Jaakko Koivisto
  0 siblings, 1 reply; 4+ messages in thread
From: Jaakko Koivisto @ 2026-11-02  9:15 UTC (permalink / raw)
  To: Andreas Klinger, Jonathan Cameron, David Lechner, Nuno Sá,
	Andy Shevchenko
  Cc: linux-iio, linux-kernel, Jaakko Koivisto

Add functionality for turning the SGP40 sensor heating element
on or off manually. After turning the heater off, the sensor enters
idle mode. Track the state of the heater in the driver, as the device
does not expose current heater state via any register.

Turning the heater off saves approx. 2.5 mA compared to regular
operation. The heating element is automatically turned back on
when measurement is performed.

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

diff --git a/drivers/iio/chemical/sgp40.c b/drivers/iio/chemical/sgp40.c
index 278390bc6b04..87df8d79fe6e 100644
--- a/drivers/iio/chemical/sgp40.c
+++ b/drivers/iio/chemical/sgp40.c
@@ -54,7 +54,8 @@ struct sgp40_data {
 	int			rht;
 	int			temp;
 	int			res_calibbias;
-	/* Prevent concurrent access to rht, tmp, calibbias */
+	bool			heater_enabled;
+	/* Prevent concurrent access to rht, tmp, calibbias, heater_enabled */
 	struct mutex		lock;
 };
 
@@ -263,6 +264,27 @@ static int sgp40_execute_self_test(struct sgp40_data *data)
 	}
 }
 
+static int sgp40_turn_heater_off(struct sgp40_data *data)
+{
+	int ret;
+	struct i2c_client *client = data->client;
+	struct sgp40_tg_measure turn_off = { .command = {0x36, 0x15} };
+
+	guard(mutex)(&data->lock);
+
+	ret = i2c_master_send(client, (char *)&turn_off, sizeof(turn_off.command));
+	if (ret < 0)
+		return ret;
+	if (ret != sizeof(turn_off.command))
+		return dev_err_probe(data->dev, -EIO,
+		       "i2c_master_send ret: %d, expected %zu", ret, sizeof(turn_off.command));
+
+	fsleep(1000); /* Max duration for command turn_heater_off. Datasheet table 8. */
+	data->heater_enabled = false;
+
+	return 0;
+}
+
 static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance_raw)
 {
 	int ret;
@@ -293,6 +315,7 @@ static int sgp40_measure_resistance_raw(struct sgp40_data *data, u16 *resistance
 		return -EIO;
 	}
 	msleep(30);
+	data->heater_enabled = true;
 
 	ret = i2c_master_recv(client, (u8 *)&tgres, sizeof(tgres));
 	if (ret < 0)
@@ -423,10 +446,47 @@ static ssize_t serialnumber_show(struct device *dev,
 	return sysfs_emit_at(buf, 0, "%llu\n", serial_number);
 }
 
+static ssize_t heater_enable_store(struct device *dev,
+				   struct device_attribute *attr,
+				   const char *buf, size_t len)
+{
+	int ret;
+	u16 __ignore;
+	bool val;
+	struct sgp40_data *data = iio_priv(dev_to_iio_dev(dev));
+
+	ret = kstrtobool(buf, &val);
+	if (ret)
+		return ret;
+
+	if (val)
+		ret = sgp40_measure_resistance_raw(data, &__ignore);
+	else
+		ret = sgp40_turn_heater_off(data);
+
+	if (ret)
+		return ret;
+
+	return len;
+}
+
+static ssize_t heater_enable_show(struct device *dev,
+				  struct device_attribute *attr,
+				  char *buf)
+{
+	struct sgp40_data *data = iio_priv(dev_to_iio_dev(dev));
+
+	guard(mutex)(&data->lock);
+
+	return sysfs_emit(buf, "%d\n", data->heater_enabled);
+}
+
 static IIO_DEVICE_ATTR_RO(serialnumber, 0);
+static IIO_DEVICE_ATTR_RW(heater_enable, 0);
 
 static struct attribute *sgp40_attributes[] = {
 	&iio_dev_attr_serialnumber.dev_attr.attr,
+	&iio_dev_attr_heater_enable.dev_attr.attr,
 	NULL
 };
 
-- 
2.56.0


^ permalink raw reply	[flat|nested] 4+ messages in thread
* [PATCH v2 0/5] iio: chemical: sgp40: add funtions for serial number readback, self-test, and heater control.
@ 2026-11-02  9:12 Jaakko Koivisto
  2026-11-02  9:56 ` [PATCH v2 5/5] iio: chemical: sgp40: Implement turn_heater_off-command Jaakko Koivisto
  0 siblings, 1 reply; 4+ messages in thread
From: Jaakko Koivisto @ 2026-11-02  9:12 UTC (permalink / raw)
  To: Andreas Klinger, Jonathan Cameron, David Lechner, Nuno Sá,
	Andy Shevchenko
  Cc: linux-iio, linux-kernel, Jaakko Koivisto

Add commands for getting the Sensirion SGP40 VOC-sensor
serial number, controlling the device's internal heater,
and running the device self-test.

Replace the manual mutex_lock()/mutex_unlock() with guard(mutex).
Simplifies the locking patterns, making it easier to add new mutex-guarded
variable 'heater_enabled' in patch 5.
Extends the lock to cover i2c communications with the device.

Fixes issue with converting all i2c errors to EIO in
sgp40_measure_resistance_raw(). Included in the patchset as it was found
due to reviewer flagging the same issue in the v1 patches.

Changes in v2:
- Added patch for replacing mutex_lock()/mutex_unlock() with guard().

- Added guard against concurrent access to the device in
  sgp40_get_serial_number(), sgp40_execute_self_test(),
  and sgp40_turn_heater_off().

- Return EIO if CRC check fails in sgp40_get_serial_number(),
  sgp40_execute_self_test(), sgp40_and turn_heater_off().

- Fixed formatting issues, missing newlines, unnecessary debug-print and
  unnecessary 0LL constant, added comments for sleep times.

- Changed 1 ms sleeps in sgp40_get_serial_number() and
  sgp40_turn_heater_off() to use fsleep instead of msleep
  (checkpatch.pl warning against using short msleep)

- Removed storing serial-number in driver-data, instead retrieve it when
  requested.

- Use the existing serialnumber ABI for presenting the SN to userspace.

- Fixed error paths in sgp40_turn_heater_off(). In v1 positive
  numbers were returned for errors. Fail the driver probe if the self-test
  fails to run, or the response is not PASS or FAIL.

- Use the existing heater_enable ABI for heater control.

- Return any negative errors from i2c_master_send() without
  converting them to -EIO in get_serial_number, execute_self_test,
  and turn_heater_off.

- Added patch for fixing the i2c error conversion issue in
  sgp40_measure_resistance_raw().

- Log errors with dev_err_probe() instead of dev_err().

- Link to v1: https://lore.kernel.org/linux-iio/20260918134019.1101308-1-jmatko@utu.fi/

Jaakko Koivisto (5):
  iio: chemical: sgp40: Do not convert I2C errors to EIO
  iio: chemical: sgp40: Replace manual locking with guard()
  iio: chemical: sgp40: Implement get_serial_number-command
  iio: chemical: sgp40: Implement execute_self_test-command
  iio: chemical: sgp40: Implement turn_heater_off-command

 drivers/iio/chemical/sgp40.c | 231 +++++++++++++++++++++++++++++++----
 1 file changed, 210 insertions(+), 21 deletions(-)

-- 
2.56.0


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

end of thread, other threads:[~2026-11-02 10:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-02  9:15 [PATCH v2 5/5] iio: chemical: sgp40: Implement turn_heater_off-command Jaakko Koivisto
2026-11-02 10:00 ` VS: " Jaakko Koivisto
  -- strict thread matches above, loose matches on Subject: below --
2026-11-02  9:12 [PATCH v2 0/5] iio: chemical: sgp40: add funtions for serial number readback, self-test, and heater control Jaakko Koivisto
2026-11-02  9:56 ` [PATCH v2 5/5] iio: chemical: sgp40: Implement turn_heater_off-command Jaakko Koivisto
2026-11-02 10:24   ` Joshua Crofts

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®