hwmon: Add sht4x Temperature and Humidity Sensor Driver
Subsystems (2)
PATCH v6 1/1 hwmon: Add sht4x Temperature and Humidity Sensor Driver
This patch adds a hwmon driver for the SHT4x Temperature and Humidity sensor.
Changes in v2:
* Removed unused macro SHT4X_MIN_POLL_INTERVAL
* Replaced time_after instead of ktime_after
* Used goto statements for error handling
* Hardcoded the interval_time instead of clamp_val().
Changes in v3:
* Accept the poll interval if it is greater than SHT4X_MIN_POLL_INTERVAL and return -EINVAL for negative values & less than SHT4X_MIN_POLL_INTERVAL
* Changed the data type of update_interval and last_updated to long.
Changes in v4:
* "update_interval" is long but msecs_to_jiffies() accepts only unsigned int. clamp_val() api is used to assign the update_interval stays within UINT_MAX.
Changes in v5:
* Added error handling when master unable to send the data.
Changes in v6:
* clamp_val() alone is used to set the update interval. since the update interval is a continuous setting.
--- Documentation/hwmon/index.rst | 1 + Documentation/hwmon/sht4x.rst | 45 +++++ drivers/hwmon/Kconfig | 11 ++ drivers/hwmon/Makefile | 1 + drivers/hwmon/sht4x.c | 305 ++++++++++++++++++++++++++++++++++ 5 files changed, 363 insertions(+) create mode 100644 Documentation/hwmon/sht4x.rst create mode 100644 drivers/hwmon/sht4x.c
Documentation/hwmon/index.rst +1 -0
@@ -164,6 +164,7 @@ Hardware Monitoring Kernel Drivers sht15 sht21 sht3x + sht4x shtc1 sis5595 sl28cpld
Documentation/hwmon/sht4x.rst new file +45 -0
45 lines not on this page · raw message
drivers/hwmon/Kconfig +11 -0
@@ -1583,6 +1583,17 @@ config SENSORS_SHT3x This driver can also be built as a module. If so, the module will be called sht3x. +config SENSORS_SHT4x + tristate "Sensiron humidity and temperature sensors. SHT4x and compat." + depends on I2C + select CRC8 + help + If you say yes here you get support for the Sensiron SHT40, SHT41 and + SHT45 humidity and temperature sensors. + + This driver can also be built as a module. If so, the module + will be called sht4x. + config SENSORS_SHTC1 tristate "Sensiron humidity and temperature sensors. SHTC1 and compat." depends on I2C
drivers/hwmon/Makefile +1 -0
@@ -171,6 +171,7 @@ obj-$(CONFIG_SENSORS_SL28CPLD) += sl28cpld-hwmon.o obj-$(CONFIG_SENSORS_SHT15) += sht15.o obj-$(CONFIG_SENSORS_SHT21) += sht21.o obj-$(CONFIG_SENSORS_SHT3x) += sht3x.o +obj-$(CONFIG_SENSORS_SHT4x) += sht4x.o obj-$(CONFIG_SENSORS_SHTC1) += shtc1.o obj-$(CONFIG_SENSORS_SIS5595) += sis5595.o obj-$(CONFIG_SENSORS_SMM665) += smm665.o
drivers/hwmon/sht4x.c new file +305 -0
305 lines not on this page · raw message
Signature · 1 line
2.31.1
kernel test robot
build botThank you for the patch! Perhaps something to improve:
Hi Navin,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on hwmon/hwmon-next]
[also build test WARNING on v5.13-rc2 next-20210521]
[If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]
url: https://github.com/0day-ci/linux/commits/Navin-Sankar-Velliangiri/hwmon-Add-sht4x-Temperature-and-Humidity-Sensor-Driver/20210522-232201 base: https://git.kernel.org/pub/scm/linux/kernel/git/groeck/linux-staging.git hwmon-next config: x86_64-randconfig-r033-20210523 (attached as .config) compiler: clang version 13.0.0 (https://github.com/llvm/llvm-project e84a9b9bb3051c35dea993cdad7b3d2575638f85)
reproduce (this is a W=1 build):
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
chmod +x ~/bin/make.cross
# install x86_64 cross compiling tool for clang build
# apt-get install binutils-x86-64-linux-gnu
# https://github.com/0day-ci/linux/commit/a122a0f7b0de4be7208671bb4f68ee1e14a3f28e
git remote add linux-review https://github.com/0day-ci/linux
git fetch --no-tags linux-review Navin-Sankar-Velliangiri/hwmon-Add-sht4x-Temperature-and-Humidity-Sensor-Driver/20210522-232201 git checkout a122a0f7b0de4be7208671bb4f68ee1e14a3f28e
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=x86_64
If you fix the issue, kindly add following tag as appropriate
All warnings (new ones prefixed by >>):
>> drivers/hwmon/sht4x.c:84:2: warning: variable 'ret' is used uninitialized whenever 'if' condition is false [-Wsometimes-uninitialized]
if (!data->valid || time_after(jiffies, next_update)) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/compiler.h:56:28: note: expanded from macro 'if'
#define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/compiler.h:58:30: note: expanded from macro '__trace_if_var'
#define __trace_if_var(cond) (__builtin_constant_p(cond) ? (cond) : __trace_if_value(cond))
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/hwmon/sht4x.c:125:9: note: uninitialized use occurs here
return ret;
^~~
drivers/hwmon/sht4x.c:84:2: note: remove the 'if' if its condition is always true
if (!data->valid || time_after(jiffies, next_update)) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
include/linux/compiler.h:56:23: note: expanded from macro 'if'
#define if(cond, ...) if ( __trace_if_var( !!(cond , ## __VA_ARGS__) ) )
^
drivers/hwmon/sht4x.c:74:9: note: initialize the variable 'ret' to silence this warning
int ret;
^
= 01 warning generated.
vim +84 drivers/hwmon/sht4x.c
66
67 /**
68 * sht4x_read_values() - read and parse the raw data from the SHT4X
69 * @sht4x_data: the struct sht4x_data to use for the lock
70 * Return: 0 if succesfull, 1 if not
71 */
72 static int sht4x_read_values(struct sht4x_data *data)
73 {
74 int ret;
75 u16 t_ticks, rh_ticks;
76 unsigned long next_update;
77 struct i2c_client *client = data->client;
78 u8 crc, raw_data[SHT4X_RESPONSE_LENGTH],
79 cmd[] = {SHT4X_CMD_MEASURE_HPM};
80
81 mutex_lock(&data->lock);
82 next_update = data->last_updated +
83 msecs_to_jiffies(data->update_interval);
> 84 if (!data->valid || time_after(jiffies, next_update)) {
85 ret = i2c_master_send(client, cmd, SHT4X_CMD_LEN);
86 if (ret < 0)
87 goto unlock;
88
89 usleep_range(SHT4X_MEAS_DELAY,
90 SHT4X_MEAS_DELAY + SHT4X_DELAY_EXTRA);
91
92 ret = i2c_master_recv(client, raw_data, SHT4X_RESPONSE_LENGTH);
93 if (ret != SHT4X_RESPONSE_LENGTH) {
94 if (ret >= 0)
95 ret = -ENODATA;
96
97 goto unlock;
98 }
99 100 t_ticks = raw_data[0] << 8 | raw_data[1];
101 rh_ticks = raw_data[3] << 8 | raw_data[4];
102
103 crc = crc8(sht4x_crc8_table, &raw_data[0], SHT4X_WORD_LEN, CRC8_INIT_VALUE);
104 if (crc != raw_data[2]) {
105 dev_err(&client->dev, "data integrity check failed\n");
106 ret = -EIO;
107 goto unlock;
108 }
109
110 crc = crc8(sht4x_crc8_table, &raw_data[3], SHT4X_WORD_LEN, CRC8_INIT_VALUE);
111 if (crc != raw_data[5]) {
112 dev_err(&client->dev, "data integrity check failed\n");
113 ret = -EIO;
114 goto unlock;
115 }
116
117 data->temperature = ((21875 * (int32_t)t_ticks) >> 13) - 45000;
118 data->humidity = ((15625 * (int32_t)rh_ticks) >> 13) - 6000;
119 data->last_updated = jiffies;
120 data->valid = true;
121 }
122
123 unlock:
124 mutex_unlock(&data->lock);
125 return ret;
126 }
127
---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org
Guenter Roeck
maintainerQuoted 208 lines · Navin Sankar Velliangiri
This patch adds a hwmon driver for the SHT4x Temperature and Humidity sensor.
Signed-off-by Navin Sankar Velliangiri <[email protected]>Changes in v2:
* Removed unused macro
SHT4X_MIN_POLL_INTERVAL
* Replacedtime_afterinstead ofktime_after
* Used goto statements for error handling
* Hardcoded theinterval_timeinstead ofclamp_val().Changes in v3:
* Accept the poll interval if it is greater than
SHT4X_MIN_POLL_INTERVALand return-EINVALfor negative values & less thanSHT4X_MIN_POLL_INTERVAL
* Changed the data type ofupdate_intervalandlast_updatedto long.Changes in v4:
* "
update_interval" is long butmsecs_to_jiffies()accepts only unsigned int.clamp_val()api is used to assign theupdate_intervalstays withinUINT_MAX.Changes in v5:
* Added error handling when master unable to send the data.
Changes in v6:
*
clamp_val()alone is used to set the update interval. since the update interval is a continuous setting.--- Documentation/hwmon/index.rst | 1 + Documentation/hwmon/sht4x.rst | 45 +++++ drivers/hwmon/Kconfig | 11 ++ drivers/hwmon/Makefile | 1 + drivers/hwmon/sht4x.c | 305 ++++++++++++++++++++++++++++++++++ 5 files changed, 363 insertions(+)create mode 100644
Documentation/hwmon/sht4x.rstcreate mode 100644drivers/hwmon/sht4x.cDocumentation/hwmon/index.rst +1
-0@@ -164,6 +164,7 @@ Hardware Monitoring Kernel Drivers sht15 sht21 sht3x + sht4x shtc1 sis5595 sl28cpldDocumentation/hwmon/sht4x.rst new file +45
-0@@ -0,0 +1,45 @@ +.. SPDX-License-Identifier: GPL-2.0 + +Kernel driver sht4x +=================== + +Supported Chips: + + * Sensirion SHT4X + + Prefix: 'sht4x' + + Addresses scanned: None + + Datasheet: + + English: https://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/2_Humidity_Sensors/Datasheets/Sensirion_Humidity_Sensors_SHT4x_Datasheet.pdf + +Author: Navin Sankar Velliangiri <[email protected]> + + +Description +----------- + +This driver implements support for the Sensirion SHT4x chip, a humidity +and temperature sensor. Temperature is measured in degree celsius, relative +humidity is expressed as a percentage. In sysfs interface, all values are +scaled by 1000, i.e. the value for 31.5 degrees celsius is 31500. + +Usage Notes +----------- + +The device communicates with the I2C protocol. Sensors can have the I2C +address 0x44. See Documentation/i2c/instantiating-devices.rst for methods +to instantiate the device. + +Sysfs entries +------------- + +=============== ============================================ +temp1_input Measured temperature in millidegrees Celcius +humidity1_input Measured humidity in %H +update_interval The minimum interval for polling the sensor, + in milliseconds. Writable. Must be at least + 2000. +============== =============================================drivers/hwmon/Kconfig +11
-0@@ -1583,6 +1583,17 @@ config SENSORS_SHT3x This driver can also be built as a module. If so, the module will be called sht3x. +config SENSORS_SHT4x + tristate "Sensiron humidity and temperature sensors. SHT4x and compat." + depends on I2C + select CRC8 + help + If you say yes here you get support for the Sensiron SHT40, SHT41 and + SHT45 humidity and temperature sensors. + + This driver can also be built as a module. If so, the module + will be called sht4x. + config SENSORS_SHTC1 tristate "Sensiron humidity and temperature sensors. SHTC1 and compat." depends on I2Cdrivers/hwmon/Makefile +1
-0@@ -171,6 +171,7 @@ obj-$(CONFIG_SENSORS_SL28CPLD) += sl28cpld-hwmon.o obj-$(CONFIG_SENSORS_SHT15) += sht15.o obj-$(CONFIG_SENSORS_SHT21) += sht21.o obj-$(CONFIG_SENSORS_SHT3x) += sht3x.o +obj-$(CONFIG_SENSORS_SHT4x) += sht4x.o obj-$(CONFIG_SENSORS_SHTC1) += shtc1.o obj-$(CONFIG_SENSORS_SIS5595) += sis5595.o obj-$(CONFIG_SENSORS_SMM665) += smm665.odrivers/hwmon/sht4x.c new file +64
-0@@ -0,0 +1,305 @@ +// SPDX-License-Identifier: GPL-2.0-only + +/* +* Copyright (c) Linumiz 2021 +* +* sht4x.c - Linux hwmon driver for SHT4x Temperature and Humidity sensor +* +* Author: Navin Sankar Velliangiri <[email protected]> +*/ + +#include <linux/crc8.h> +#include <linux/delay.h> +#include <linux/hwmon.h> +#include <linux/i2c.h> +#include <linux/jiffies.h> +#include <linux/module.h> + +/* +* Poll intervals (in milliseconds) +*/ +#define SHT4X_MIN_POLL_INTERVAL 2000 + +/* +* I2C command delays (in microseconds) +*/ +#define SHT4X_MEAS_DELAY 1000 +#define SHT4X_DELAY_EXTRA 10000 + +/* +* Command Bytes +*/ +#define SHT4X_CMD_MEASURE_HPM 0b11111101 +#define SHT4X_CMD_RESET 0b10010100 + +#define SHT4X_CMD_LEN 1 +#define SHT4X_CRC8_LEN 1 +#define SHT4X_WORD_LEN 2 +#define SHT4X_RESPONSE_LENGTH 6 +#define SHT4X_CRC8_POLYNOMIAL 0x31 +#define SHT4X_CRC8_INIT 0xff +#define SHT4X_MIN_TEMPERATURE -45000 +#define SHT4X_MAX_TEMPERATURE 125000 +#define SHT4X_MIN_HUMIDITY 0 +#define SHT4X_MAX_HUMIDITY 100000 + +DECLARE_CRC8_TABLE (sht4x_crc8_table); + +/** +* struct sht4x_data - All the data required to operate an SHT4X chip +* @client: the i2c client associated with the SHT4X +* @lock: a mutex that is used to prevent parallel access to the i2c client +* @update_interval: the minimum poll interval +* @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 +*/ +struct sht4x_data { +struct i2c_client *client; +struct mutex lock;/* atomic read data updates */ +bool valid;/* validity of fields below */ +long update_interval;/* in milli-seconds */ +long last_updated;/* in jiffies */ +s32 temperature; +s32 humidity;
drivers/hwmon/sht4x.c line 74
Quoted 10 lines
drivers/hwmon/sht4x.c new file +10
-0+}; + +/** +* sht4x_read_values() - read and parse the raw data from the SHT4X +* @sht4x_data: the struct sht4x_data to use for the lock +* Return: 0 if succesfull, 1 if not +*/ +static int sht4x_read_values (struct sht4x_data *data) +{ +int ret;
Ah yes, the value of ret will be uninitialized, and the function will return a random value if data->valid is false or if two measurements follow each other.
Please fix and resubmit. The driver looks good otherwise.
Guenter