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