diff --git a/drivers/sensor/silabs/si7006/Kconfig b/drivers/sensor/silabs/si7006/Kconfig index c45914c4364..37872744bdd 100644 --- a/drivers/sensor/silabs/si7006/Kconfig +++ b/drivers/sensor/silabs/si7006/Kconfig @@ -2,9 +2,11 @@ # SPDX-License-Identifier: Apache-2.0 config SI7006 - bool "Si7006 Temperature and Humidity Sensor" + bool "SHT21, Si7006, and HTU21D Humidity and Temperature Sensors" default y - depends on DT_HAS_SILABS_SI7006_ENABLED + depends on DT_HAS_SILABS_SI7006_ENABLED || DT_HAS_SENSIRION_SHT21_ENABLED select I2C help - Enable I2C-based driver for Si7006 Temperature and Humidity Sensor. + Enable I2C-based driver for several humidity and temperature sensors + compatible with the Sensirion SHT21, such as the Silicon Labs + Si7006/13/20/21 and Measurement Specialties HTU21D diff --git a/drivers/sensor/silabs/si7006/si7006.c b/drivers/sensor/silabs/si7006/si7006.c index df0095e78fd..d2f320b0927 100644 --- a/drivers/sensor/silabs/si7006/si7006.c +++ b/drivers/sensor/silabs/si7006/si7006.c @@ -1,11 +1,10 @@ /* * Copyright (c) 2019 Electronut Labs + * Copyright (c) 2023 Trent Piepho * * SPDX-License-Identifier: Apache-2.0 */ -#define DT_DRV_COMPAT silabs_si7006 - #include #include #include @@ -28,6 +27,8 @@ struct si7006_data { struct si7006_config { struct i2c_dt_spec i2c; + /** Use "read temp" vs "read old temp" command, the latter only with SiLabs sensors. */ + uint8_t read_temp_cmd; }; /** @@ -58,20 +59,20 @@ static int si7006_get_humidity(const struct device *dev) /** * @brief function to get temperature * - * Note that si7006_get_humidity must be called before calling - * si7006_get_old_temperature. + * Note that for Si7006 type sensors, si7006_get_humidity must be called before + * calling si7006_get_temperature, as the get old temperature command is used. * * @return int 0 on success */ -static int si7006_get_old_temperature(const struct device *dev) +static int si7006_get_temperature(const struct device *dev) { struct si7006_data *si_data = dev->data; const struct si7006_config *config = dev->config; uint8_t temp[2]; int retval; - retval = i2c_burst_read_dt(&config->i2c, SI7006_READ_OLD_TEMP, temp, + retval = i2c_burst_read_dt(&config->i2c, config->read_temp_cmd, temp, sizeof(temp)); if (retval == 0) { @@ -95,7 +96,7 @@ static int si7006_sample_fetch(const struct device *dev, retval = si7006_get_humidity(dev); if (retval == 0) { - retval = si7006_get_old_temperature(dev); + retval = si7006_get_temperature(dev); } return retval; @@ -164,15 +165,17 @@ static int si7006_init(const struct device *dev) return 0; } -#define SI7006_DEFINE(inst) \ +#define SI7006_DEFINE(inst, temp_cmd) \ static struct si7006_data si7006_data_##inst; \ \ static const struct si7006_config si7006_config_##inst = { \ - .i2c = I2C_DT_SPEC_INST_GET(inst), \ + .i2c = I2C_DT_SPEC_GET(inst), \ + .read_temp_cmd = temp_cmd, \ }; \ \ - SENSOR_DEVICE_DT_INST_DEFINE(inst, si7006_init, NULL, \ - &si7006_data_##inst, &si7006_config_##inst, \ - POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &si7006_api); \ + SENSOR_DEVICE_DT_DEFINE(inst, si7006_init, NULL, \ + &si7006_data_##inst, &si7006_config_##inst, \ + POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY, &si7006_api); \ -DT_INST_FOREACH_STATUS_OKAY(SI7006_DEFINE) +DT_FOREACH_STATUS_OKAY_VARGS(silabs_si7006, SI7006_DEFINE, SI7006_READ_OLD_TEMP); +DT_FOREACH_STATUS_OKAY_VARGS(sensirion_sht21, SI7006_DEFINE, SI7006_MEAS_TEMP_MASTER_MODE); diff --git a/dts/bindings/sensor/sensirion,sht21.yaml b/dts/bindings/sensor/sensirion,sht21.yaml new file mode 100644 index 00000000000..29cc2518cfd --- /dev/null +++ b/dts/bindings/sensor/sensirion,sht21.yaml @@ -0,0 +1,14 @@ +# Copyright (c) 2023, Trent Piepho +# SPDX-License-Identifier: Apache-2.0 + +description: | + Sensirion SHT21 Humidity and Temperature Sensor + + This is compatible with the Measurement Specialties HUT21D, "meas,htu21d". + + For the Silicon Labs Si7006/13/20/21 series, use the "silabs,si7006" + compatible for slightly altered operation. + +compatible: "sensirion,sht21" + +include: [sensor-device.yaml, i2c-device.yaml] diff --git a/dts/bindings/sensor/silabs,si7006.yaml b/dts/bindings/sensor/silabs,si7006.yaml index 95032a0d610..d4a68ca9078 100644 --- a/dts/bindings/sensor/silabs,si7006.yaml +++ b/dts/bindings/sensor/silabs,si7006.yaml @@ -1,8 +1,14 @@ # Copyright (c) 2019, Electronut Labs +# Copyright (c) 2023, Trent Piepho # SPDX-License-Identifier: Apache-2.0 -description: Si7006 temperature and humidity sensor +description: | + Silicon Labs Si7006 Humidity and Temperature Sensor + + This is compatible with a range of Silicon Labs sensors, such as the Si7013, + Si7020, and Si7021. This binding will use a slightly different (better) + command to read temperature from the "sensirion,sht21" binding. compatible: "silabs,si7006" -include: [sensor-device.yaml, i2c-device.yaml] +include: sensirion,sht21.yaml diff --git a/tests/drivers/build_all/sensor/i2c.dtsi b/tests/drivers/build_all/sensor/i2c.dtsi index 78b38275d1a..992fe301362 100644 --- a/tests/drivers/build_all/sensor/i2c.dtsi +++ b/tests/drivers/build_all/sensor/i2c.dtsi @@ -1062,3 +1062,8 @@ test_i2c_lm95234: lm95234@8f { reg = <0x8f>; status = "okay"; }; + +test_i2c_sht21@90 { + compatible = "sensirion,sht21"; + reg = <0x90>; +};