Skip to contentSkip to conversation

hwmon: (sht4x): add heater support

PATCH Superseded Antoni Pokusinski 2 participants 4 messages Aug 26, 2024 – Aug 28, 2024

Subsystems (2)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 hwmon: (sht4x): add heater support

Add support for manipulating the internal heater of sht4x devices. Enabling the heater removes condensed water from the sensor surface which disturbs the relative humidity measurements.

The heater can operate at three heating levels (20, 110 or 200 milliwatts). Also, two heating durations may be selected (0.1 or 1s). Once the heating time elapses the heater is automatically switched off.

---
Hello Guenter,

Thanks for the review.
I'm not sure if this short patch description above is now ok or shall I be even more detailed.
The problem that I was facing was that when I was using the sensor for relative humidity measurements the results were always ~100% even though the correct value should be ~60%. Enabling the heater in periodic pulses to remove the condensation fixed the issue and the measurements were correct after that.
Thus, I'm not sure if I should include that detailed arguments in the patch description or leave it as it is right now.

Regards,
Antoni Pokusinski

--
Changes since v2:
* heater_enable_store: remove unnecessary if
* Documentation: remove incorrect info about turning off the heater
* be more specific in the patch description

Changes since v1:
* explain the use case of the new attributes set
* heater_enable attr: make it write-only
* heater_enable_store: define cmd as u8 instead of u8*
* heater_enable_store: remove unreachable data path
* heater_enable_store: remove unnecessary lock
* heater_enable_store: call i2c_master_send only if status==true
* define attributes as DEVICE_ATTR_* instead of SENSOR_DEVICE_ATTR_*

---
 Documentation/hwmon/sht4x.rst |  10 +++
 drivers/hwmon/sht4x.c         | 126 +++++++++++++++++++++++++++++++++-
 2 files changed, 135 insertions(+), 1 deletion(-)
Documentation/hwmon/sht4x.rst +10 -0
index daf21e763425..0b17f89fa1ab 100644
@@ -42,4 +42,14 @@ humidity1_input Measured humidity in %H
 update_interval The minimum interval for polling the sensor,
                 in milliseconds. Writable. Must be at least
                 2000.
+heater_power	The requested heater power, in milliwatts.
+		Available values: 20, 110, 200 (default: 200).
+heater_time	The requested operating time of the heater,
+		in milliseconds.
+		Available values: 100, 1000 (default 1000).
+heater_enable	Enable the heater with the selected power
+		and for the selected time in order to remove
+		condensed water from the sensor surface. Write-only.
+
+			- 1: turn on
 =============== ============================================
drivers/hwmon/sht4x.c +125 -1

168 lines not on this page · raw message

Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 135 lines · Antoni Pokusinski

Add support for manipulating the internal heater of sht4x devices. Enabling the heater removes condensed water from the sensor surface which disturbs the relative humidity measurements.

The heater can operate at three heating levels (20, 110 or 200 milliwatts). Also, two heating durations may be selected (0.1 or 1s). Once the heating time elapses the heater is automatically switched off.

---
Hello Guenter,

Thanks for the review.
I'm not sure if this short patch description above is now ok or shall I be even more detailed.
The problem that I was facing was that when I was using the sensor for relative humidity measurements the results were always ~100% even though the correct value should be ~60%. Enabling the heater in periodic pulses to remove the condensation fixed the issue and the measurements were correct after that.
Thus, I'm not sure if I should include that detailed arguments in the patch description or leave it as it is right now.

Regards,
Antoni Pokusinski

--
Changes since v2:
* heater_enable_store: remove unnecessary if
* Documentation: remove incorrect info about turning off the heater
* be more specific in the patch description

Changes since v1:
* explain the use case of the new attributes set
* heater_enable attr: make it write-only
* heater_enable_store: define cmd as u8 instead of u8*
* heater_enable_store: remove unreachable data path
* heater_enable_store: remove unnecessary lock
* heater_enable_store: call i2c_master_send only if status==true
* define attributes as DEVICE_ATTR_* instead of SENSOR_DEVICE_ATTR_*

---
  Documentation/hwmon/sht4x.rst |  10 +++
  drivers/hwmon/sht4x.c         | 126 +++++++++++++++++++++++++++++++++-
  2 files changed, 135 insertions(+), 1 deletion(-)
Documentation/hwmon/sht4x.rst +10 -0
index daf21e763425..0b17f89fa1ab 100644
@@ -42,4 +42,14 @@ humidity1_input Measured humidity in %H
  update_interval The minimum interval for polling the sensor,
                  in milliseconds. Writable. Must be at least
                  2000.
+heater_power	The requested heater power, in milliwatts.
+		Available values: 20, 110, 200 (default: 200).
+heater_time	The requested operating time of the heater,
+		in milliseconds.
+		Available values: 100, 1000 (default 1000).
+heater_enable	Enable the heater with the selected power
+		and for the selected time in order to remove
+		condensed water from the sensor surface. Write-only.
+
+			- 1: turn on
  =============== ============================================
drivers/hwmon/sht4x.c +35 -0
index b8916d2735b5..20b50f7feac0 100644
@@ -11,6 +11,7 @@
  #include <linux/crc8.h>
  #include <linux/delay.h>
  #include <linux/hwmon.h>
+#include <linux/hwmon-sysfs.h>
  #include <linux/i2c.h>
  #include <linux/jiffies.h>
  #include <linux/module.h>
@@ -31,6 +32,12 @@
   */
  #define SHT4X_CMD_MEASURE_HPM	0b11111101
  #define SHT4X_CMD_RESET		0b10010100
+#define SHT4X_CMD_HEATER_20_1	0b00011110
+#define SHT4X_CMD_HEATER_20_01	0b00010101
+#define SHT4X_CMD_HEATER_110_1	0b00101111
+#define SHT4X_CMD_HEATER_110_01	0b00100100
+#define SHT4X_CMD_HEATER_200_1	0b00111001
+#define SHT4X_CMD_HEATER_200_01 0b00110010
 
  #define SHT4X_CMD_LEN		1
  #define SHT4X_CRC8_LEN		1
@@ -54,6 +61,8 @@ DECLARE_CRC8_TABLE(sht4x_crc8_table);
   * @last_updated: the previous time that the SHT4X was polled
   * @temperature: the latest temperature value received from the SHT4X
   * @humidity: the latest humidity value received from the SHT4X
+ * @heater_power: the power at which the heater will be started
+ * @heater_time: the time for which the heater will remain turned on
   */
  struct sht4x_data {
  	struct i2c_client	*client;
@@ -63,6 +72,8 @@ struct sht4x_data {
  	long			last_updated;	/* in jiffies */
  	s32			temperature;
  	s32			humidity;
+	u32			heater_power;	/* in milli-watts */
+	u32			heater_time;	/* in milli-seconds */
  };
 
  /**
@@ -215,6 +226,117 @@ static int sht4x_hwmon_write(struct device *dev, enum hwmon_sensor_types type,
  	}
  }
 
+static ssize_t heater_enable_store(struct device *dev,
+				   struct device_attribute *attr,
+				   const char *buf,
+				   size_t count)
+{
+	struct sht4x_data *data = dev_get_drvdata(dev);
+	bool status;
+	ssize_t ret;
+	u8 cmd;
+
+	ret = kstrtobool(buf, &status);
+	if (ret)
+		return ret;
+
+	if (status) {
+		if (data->heater_power == 20) {
+			if (data->heater_time == 100)
+				cmd = SHT4X_CMD_HEATER_20_01;
+			else /* data->heater_time == 1000 */
+				cmd = SHT4X_CMD_HEATER_20_1;
+		} else if (data->heater_power == 110) {
+			if (data->heater_time == 100)
+				cmd = SHT4X_CMD_HEATER_110_01;
+			else /* data->heater_time == 1000 */

drivers/hwmon/sht4x.c static int sht4x_hwmon_write(struct device *dev, enum hwmon_sensor_types type, · line 262

Quoted 10 lines
drivers/hwmon/sht4x.c +10 -0
+				cmd = SHT4X_CMD_HEATER_110_1;
+		} else { /* data->heater_power == 200 */
+			if (data->heater_time == 100)
+				cmd = SHT4X_CMD_HEATER_200_01;
+			else /* data->heater_time == 1000 */
+				cmd = SHT4X_CMD_HEATER_200_1;
+		}
+
+		ret = i2c_master_send(data->client, &cmd, SHT4X_CMD_LEN);
+	}

This still accepts (and ignores) writing "0" or "false".

On top of that, unless I misunderstand the chip documentation, attempts to read the humidity before the "turn on heater" command completes will result in a NACK from the chip. This is not currently handled by the driver.

Also, the datasheet states that a high precision measurement is done before the heater is turned off. That measurement is currently ignored.

I am not entirely sure how to handle all this, but based on the information in the datasheet I can not accept your current solution. At the very least, you'll need to make sure that an attempt to read the humidity immediately after turning on the heater does not result in an error message.

Thanks,
Guenter

#2 lore raw

Hello,

I've been thinking on how to approach the problem of NACKs received from the sensor while the heater is on but I haven't found a perfectly satisfying solution either. This is due to the fact that the device does not provide any way to check if the heater is on/off.

1. I guess that the simplest possible approach would involve sleeping in heater_enable_store():

  ssize_t heater_enable_store() {
    ...
    mutex_lock(data->lock);
    ret = i2c_master_send(data->client, &cmd, SHT4X_CMD_LEN);
    msleep(...) /* for >100 or >1000 ms */
    mutex_unlock(data->lock);
    ...
  }

This way, the user would have to wait for the heating to complete in order to read RH or temperature measurement. However, I find this solution unacceptable since it's completely unnecessary for the user to wait for the heating to complete.

2. A better solution could be possibly to use a wait queue in order to defer the job of enabling the heater:

  struct sht4x_data {
    ...
    struct work_struct* heater_work; /* This would be initialized
                                        with the handler described
                                        below */
  }

The task of sending the "heater_enable" command and sleeping would be performed by the worker function:

  static void heater_enable_handler(struct work_struct *work) {
    ...
    mutex_lock(data->lock);
    ret = i2c_master_send(data->client, &cmd, SHT4X_CMD_LEN);
    msleep(...) /* for >100 or >1000 ms */
    mutex_unlock(data->lock);
    ...
  }

And that above mentioned work would be scheduled
in heater_enable_store():

  ssize_t heater_enable_store() {
    ...
    schedule_work(data->heater_work);
    ...
  }

I think that this approach with work queue is better since the user doesn't have to wait for the heating to complete and the RH or temperature measurements can also be retrieved without the NACK error (even though the user still may have to wait for the heater to be off), since the data->lock mutex is used to guard both measurement reads from the sensor and the heating in heater_enable_handler.

I'm worried though about the situation where the user writes 1 to "heater_enable" while it's already enabled. Since the work_struct is already on the queue, the heater_enable_store would return an error and I see no easy solution to this for now.

Regards,
Antoni Pokusinski

maintainer
#3 lore raw
Quoted 62 lines · Antoni Pokusinski

Hello,

I've been thinking on how to approach the problem of NACKs received from the sensor while the heater is on but I haven't found a perfectly satisfying solution either. This is due to the fact that the device does not provide any way to check if the heater is on/off.

1. I guess that the simplest possible approach would involve sleeping in heater_enable_store():

   ssize_t heater_enable_store() {
     ...
     mutex_lock(data->lock);
     ret = i2c_master_send(data->client, &cmd, SHT4X_CMD_LEN);
     msleep(...) /* for >100 or >1000 ms */
     mutex_unlock(data->lock);
     ...
   }

This way, the user would have to wait for the heating to complete in order to read RH or temperature measurement. However, I find this solution unacceptable since it's completely unnecessary for the user to wait for the heating to complete.

2. A better solution could be possibly to use a wait queue in order to defer the job of enabling the heater:

   struct sht4x_data {
     ...
     struct work_struct* heater_work; /* This would be initialized
                                         with the handler described
                                         below */
   }

The task of sending the "heater_enable" command and sleeping would be performed by the worker function:

   static void heater_enable_handler(struct work_struct *work) {
     ...
     mutex_lock(data->lock);
     ret = i2c_master_send(data->client, &cmd, SHT4X_CMD_LEN);
     msleep(...) /* for >100 or >1000 ms */
     mutex_unlock(data->lock);
     ...
   }

And that above mentioned work would be scheduled
in heater_enable_store():

   ssize_t heater_enable_store() {
     ...
     schedule_work(data->heater_work);
     ...
   }

I think that this approach with work queue is better since the user doesn't have to wait for the heating to complete and the RH or temperature measurements can also be retrieved without the NACK error (even though the user still may have to wait for the heater to be off), since the data->lock mutex is used to guard both measurement reads from the sensor and the heating in heater_enable_handler.

Quoted 5 lines

I'm worried though about the situation where the user writes 1 to "heater_enable" while it's already enabled. Since the work_struct is already on the queue, the heater_enable_store would return an error and I see no easy solution to this for now.

Introducing a worker seems to be overly complicated to me. You could store the time when heating is expected to be complete, and use that time in the read function to determine if it needs to wait and/or if it should trigger another RH read request instead of just reading the RH measurement from the heater command. Something like
if (still heating)
wait for heater to be turned off and measurement completion
if (heater was active within [select period of time])
read RH directly without additional request clear heater status
else
if RH was requested recently
report previous RH
else
request RH measurement
read RH

This would also help with the heater_enable problem: It would just do nothing if the heater is still enabled from the previous command (or maybe return -EBUSY). If there is some additional requirement by the chip, such as that the heater must not be enabled for more than X% of the time, that information could also be used to determine if it can be enabled again.

Thanks,
Guenter