1 /*
2  * Copyright (c) 2019 Electronut Labs
3  * Copyright (c) 2023 Trent Piepho <tpiepho@gmail.com>
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #include <zephyr/device.h>
9 #include <zephyr/drivers/i2c.h>
10 #include <zephyr/drivers/regulator.h>
11 #include <zephyr/drivers/sensor.h>
12 #include <zephyr/kernel.h>
13 #include <zephyr/sys/byteorder.h>
14 #include <zephyr/sys/__assert.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <zephyr/sys/util.h>
18 #include "si7006.h"
19 #include <zephyr/logging/log.h>
20 
21 LOG_MODULE_REGISTER(si7006, CONFIG_SENSOR_LOG_LEVEL);
22 
23 struct si7006_data {
24 	uint16_t temperature;
25 	uint16_t humidity;
26 };
27 
28 struct si7006_config {
29 	struct i2c_dt_spec i2c;
30 	const struct device *vin_supply;
31 	/** Use "read temp" vs "read old temp" command, the latter only with SiLabs sensors. */
32 	uint8_t read_temp_cmd;
33 };
34 
35 /**
36  * @brief function to get relative humidity
37  *
38  * @return int 0 on success
39  */
si7006_get_humidity(const struct device * dev)40 static int si7006_get_humidity(const struct device *dev)
41 {
42 	struct si7006_data *si_data = dev->data;
43 	const struct si7006_config *config = dev->config;
44 	int retval;
45 	uint16_t hum;
46 
47 	retval = i2c_burst_read_dt(&config->i2c, SI7006_MEAS_REL_HUMIDITY_MASTER_MODE,
48 				   (uint8_t *)&hum, sizeof(hum));
49 
50 	if (retval == 0) {
51 		si_data->humidity = sys_be16_to_cpu(hum) & ~3;
52 	} else {
53 		LOG_ERR("read register err: %d", retval);
54 	}
55 
56 	return retval;
57 }
58 
59 /**
60  * @brief function to get temperature
61  *
62  * Note that for Si7006 type sensors, si7006_get_humidity must be called before
63  * calling si7006_get_temperature, as the get old temperature command is used.
64  *
65  * @return int 0 on success
66  */
67 
si7006_get_temperature(const struct device * dev)68 static int si7006_get_temperature(const struct device *dev)
69 {
70 	struct si7006_data *si_data = dev->data;
71 	const struct si7006_config *config = dev->config;
72 	uint16_t temp;
73 	int retval;
74 
75 	retval = i2c_burst_read_dt(&config->i2c, config->read_temp_cmd,
76 				   (uint8_t *)&temp, sizeof(temp));
77 
78 	if (retval == 0) {
79 		si_data->temperature = sys_be16_to_cpu(temp) & ~3;
80 	} else {
81 		LOG_ERR("read register err: %d", retval);
82 	}
83 
84 	return retval;
85 }
86 
87 /**
88  * @brief fetch a sample from the sensor
89  *
90  * @return 0
91  */
si7006_sample_fetch(const struct device * dev,enum sensor_channel chan)92 static int si7006_sample_fetch(const struct device *dev,
93 			       enum sensor_channel chan)
94 {
95 	int retval;
96 
97 	retval = si7006_get_humidity(dev);
98 	if (retval == 0) {
99 		retval = si7006_get_temperature(dev);
100 	}
101 
102 	return retval;
103 }
104 
105 /**
106  * @brief sensor value get
107  *
108  * @return -ENOTSUP for unsupported channels
109  */
si7006_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)110 static int si7006_channel_get(const struct device *dev,
111 			      enum sensor_channel chan,
112 			      struct sensor_value *val)
113 {
114 	struct si7006_data *si_data = dev->data;
115 
116 	if (chan == SENSOR_CHAN_AMBIENT_TEMP) {
117 		/* Raw formula: (temp * 175.72) / 65536 - 46.85
118 		 * To use integer math, scale the 175.72 factor by 128 and move the offset to
119 		 * inside the division.  This gives us:
120 		 *
121 		 * (temp * 175.72 * 128 - 46.86 * 128 * 65536) / (65536 * 128)
122 		 * The constants can be calculated now:
123 		 * (temp * 22492 - 393006285) / 2^23
124 		 *
125 		 * There is a very small amount of round-off error in the factor of 22492.  To
126 		 * compenstate, a constant of 5246 is used to center the error about 0, thus
127 		 * reducing the overall MSE.
128 		 */
129 
130 		/* Temperature value times two to the 23rd power, i.e. temp_23 = temp << 23 */
131 		const int32_t temp_23 = si_data->temperature * 22492 - (393006285 - 5246);
132 		/* Integer component of temperature */
133 		int32_t temp_int = temp_23 >> 23;
134 		/* Fractional component of temperature */
135 		int32_t temp_frac = temp_23 & BIT_MASK(23);
136 
137 		/* Deal with the split twos-complement / BCD format oddness with negatives */
138 		if (temp_23 < 0) {
139 			temp_int += 1;
140 			temp_frac -= BIT(23);
141 		}
142 		val->val1 = temp_int;
143 		/* Remove a constant factor of 64 from (temp_frac * 1000000) >> 23 */
144 		val->val2 = (temp_frac * 15625ULL) >> 17;
145 
146 		LOG_DBG("temperature %u = val1:%d, val2:%d", si_data->temperature,
147 			val->val1, val->val2);
148 
149 		return 0;
150 	} else if (chan == SENSOR_CHAN_HUMIDITY) {
151 		/* Humidity times two to the 16th power.  Offset of -6 not applied yet. */
152 		const uint32_t rh_16 = si_data->humidity * 125U;
153 		/* Integer component of humidity */
154 		const int16_t rh_int = rh_16 >> 16;
155 		/* Fraction component of humidity */
156 		const uint16_t rh_frac = rh_16 & BIT_MASK(16);
157 
158 		val->val1 = rh_int - 6; /* Apply offset now */
159 		/* Remove a constant factor of 64 from (rh_frac * 1000000) >> 16 */
160 		val->val2 = (rh_frac * 15625) >> 10;
161 
162 		/* Deal with the split twos-complement / BCD format oddness with negatives */
163 		if (val->val1 < 0) {
164 			val->val1 += 1;
165 			val->val2 -= 1000000;
166 		}
167 
168 		LOG_DBG("humidity %u = val1:%d, val2:%d", si_data->humidity, val->val1, val->val2);
169 
170 		return 0;
171 	} else {
172 		return -ENOTSUP;
173 	}
174 }
175 
176 static DEVICE_API(sensor, si7006_api) = {
177 	.sample_fetch = &si7006_sample_fetch,
178 	.channel_get = &si7006_channel_get,
179 };
180 
181 /**
182  * @brief initialize the sensor
183  *
184  * @return 0 for success
185  */
186 
si7006_init(const struct device * dev)187 static int si7006_init(const struct device *dev)
188 {
189 	const struct si7006_config *config = dev->config;
190 
191 	if (!device_is_ready(config->i2c.bus)) {
192 		LOG_ERR("Bus device is not ready");
193 		return -ENODEV;
194 	}
195 
196 	if (IS_ENABLED(CONFIG_REGULATOR) && config->vin_supply) {
197 		regulator_enable(config->vin_supply);
198 
199 		/* As stated by the Si7006 spec - Maximum powerup time is 80ms */
200 		k_msleep(80);
201 	}
202 
203 	LOG_DBG("si7006 init ok");
204 
205 	return 0;
206 }
207 
208 #define SI7006_DEFINE(inst, name, temp_cmd)						\
209 	static struct si7006_data si7006_data_##name##_##inst;				\
210 											\
211 	static const struct si7006_config si7006_config_##name##_##inst = {		\
212 		.i2c = I2C_DT_SPEC_INST_GET(inst),					\
213 		.vin_supply = DEVICE_DT_GET_OR_NULL(DT_INST_PHANDLE(inst, vin_supply)),	\
214 		.read_temp_cmd = temp_cmd,						\
215 	};										\
216 											\
217 	SENSOR_DEVICE_DT_INST_DEFINE(inst, si7006_init, NULL,				\
218 				     &si7006_data_##name##_##inst,			\
219 				     &si7006_config_##name##_##inst,			\
220 				     POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,		\
221 				     &si7006_api);
222 
223 #define DT_DRV_COMPAT silabs_si7006
224 DT_INST_FOREACH_STATUS_OKAY_VARGS(SI7006_DEFINE, DT_DRV_COMPAT, SI7006_READ_OLD_TEMP);
225 
226 #undef DT_DRV_COMPAT
227 #define DT_DRV_COMPAT sensirion_sht21
228 DT_INST_FOREACH_STATUS_OKAY_VARGS(SI7006_DEFINE, DT_DRV_COMPAT, SI7006_MEAS_TEMP_MASTER_MODE);
229