1 /*
2 * Copyright (c) 2022 Würth Elektronik eiSos GmbH & Co. KG
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT we_wsen_hids
8
9 #include <string.h>
10
11 #include <zephyr/sys/__assert.h>
12 #include <zephyr/sys/byteorder.h>
13 #include <zephyr/logging/log.h>
14
15 #include "wsen_hids.h"
16
17 LOG_MODULE_REGISTER(WSEN_HIDS, CONFIG_SENSOR_LOG_LEVEL);
18
19 /*
20 * List of supported output data rates (sensor_value struct, input to
21 * sensor_attr_set()). Index into this list is used as argument for
22 * HIDS_setOutputDataRate().
23 */
24 static const struct sensor_value hids_odr_list[] = {
25 {.val1 = 0, .val2 = 0},
26 {.val1 = 1, .val2 = 0},
27 {.val1 = 7, .val2 = 0},
28 {.val1 = 12, .val2 = 5 * 100000},
29 };
30
hids_sample_fetch(const struct device * dev,enum sensor_channel channel)31 static int hids_sample_fetch(const struct device *dev, enum sensor_channel channel)
32 {
33 struct hids_data *data = dev->data;
34 int16_t raw_humidity;
35 int16_t raw_temp;
36
37 __ASSERT_NO_MSG(channel == SENSOR_CHAN_ALL);
38
39 if (HIDS_getRawValues(&data->sensor_interface, &raw_humidity, &raw_temp) != WE_SUCCESS) {
40 LOG_ERR("Failed to %s sample.", "fetch data");
41 return -EIO;
42 }
43
44 if (HIDS_convertHumidity_uint16(&data->sensor_interface, raw_humidity, &data->humidity) !=
45 WE_SUCCESS) {
46 LOG_ERR("Failed to %s sample.", "convert humidity");
47 return -EIO;
48 }
49
50 if (HIDS_convertTemperature_int16(&data->sensor_interface, raw_temp, &data->temperature) !=
51 WE_SUCCESS) {
52 LOG_ERR("Failed to %s sample.", "convert temperature");
53 return -EIO;
54 }
55
56 return 0;
57 }
58
hids_channel_get(const struct device * dev,enum sensor_channel channel,struct sensor_value * value)59 static int hids_channel_get(const struct device *dev, enum sensor_channel channel,
60 struct sensor_value *value)
61 {
62 struct hids_data *data = dev->data;
63 int32_t value_converted;
64
65 if (channel == SENSOR_CHAN_AMBIENT_TEMP) {
66 value_converted = (int32_t)data->temperature;
67
68 /* Convert temperature from 0.01 degrees Celsius to degrees Celsius */
69 value->val1 = value_converted / 100;
70 value->val2 = (value_converted % 100) * (1000000 / 100);
71 } else if (channel == SENSOR_CHAN_HUMIDITY) {
72 value_converted = (int32_t)data->humidity;
73
74 /* Convert humidity from 0.01 percent to percent */
75 value->val1 = value_converted / 100;
76 value->val2 = (value_converted % 100) * (1000000 / 100);
77 } else {
78 return -ENOTSUP;
79 }
80
81 return 0;
82 }
83
84 /* Set output data rate. See hids_odr_list for allowed values. */
hids_odr_set(const struct device * dev,const struct sensor_value * odr)85 static int hids_odr_set(const struct device *dev, const struct sensor_value *odr)
86 {
87 struct hids_data *data = dev->data;
88 int odr_index;
89
90 for (odr_index = 0; odr_index < ARRAY_SIZE(hids_odr_list); odr_index++) {
91 if (odr->val1 == hids_odr_list[odr_index].val1 &&
92 odr->val2 == hids_odr_list[odr_index].val2) {
93 break;
94 }
95 }
96
97 if (odr_index == ARRAY_SIZE(hids_odr_list)) {
98 /* ODR not allowed (was not found in hids_odr_list) */
99 LOG_ERR("Bad sampling frequency %d.%d", odr->val1, odr->val2);
100 return -EINVAL;
101 }
102
103 if (HIDS_setOutputDataRate(&data->sensor_interface, (HIDS_outputDataRate_t)odr_index) !=
104 WE_SUCCESS) {
105 LOG_ERR("Failed to set output data rate");
106 return -EIO;
107 }
108
109 return 0;
110 }
111
hids_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)112 static int hids_attr_set(const struct device *dev, enum sensor_channel chan,
113 enum sensor_attribute attr, const struct sensor_value *val)
114 {
115 if (chan != SENSOR_CHAN_ALL) {
116 LOG_WRN("attr_set() is not supported on channel %d.", chan);
117 return -ENOTSUP;
118 }
119
120 if (attr == SENSOR_ATTR_SAMPLING_FREQUENCY) {
121 return hids_odr_set(dev, val);
122 } else {
123 return -ENOTSUP;
124 }
125 }
126
127 static const struct sensor_driver_api hids_driver_api = {
128 .attr_set = hids_attr_set,
129 #if CONFIG_WSEN_HIDS_TRIGGER
130 .trigger_set = hids_trigger_set,
131 #endif
132 .sample_fetch = hids_sample_fetch,
133 .channel_get = hids_channel_get,
134 };
135
hids_init(const struct device * dev)136 static int hids_init(const struct device *dev)
137 {
138 const struct hids_config *config = dev->config;
139 struct hids_data *data = dev->data;
140 uint8_t device_id;
141
142 /* Initialize WE sensor interface */
143 WE_sensorInterfaceType_t interface_type = data->sensor_interface.interfaceType;
144
145 HIDS_getDefaultInterface(&data->sensor_interface);
146 data->sensor_interface.interfaceType = interface_type;
147
148 switch (data->sensor_interface.interfaceType) {
149 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(i2c)
150 case WE_i2c:
151 data->sensor_interface.handle = (void *)&config->bus_cfg.i2c;
152 break;
153 #endif
154 #if DT_ANY_INST_ON_BUS_STATUS_OKAY(spi)
155 case WE_spi:
156 data->sensor_interface.handle = (void *)&config->bus_cfg.spi;
157 break;
158 #endif
159 default:
160 LOG_ERR("Invalid interface type");
161 return -EINVAL;
162 }
163
164 /* First communication test - check device ID */
165 if (HIDS_getDeviceID(&data->sensor_interface, &device_id) != WE_SUCCESS) {
166 LOG_ERR("Failed to read device ID.");
167 return -EIO;
168 }
169
170 if (device_id != HIDS_DEVICE_ID_VALUE) {
171 LOG_ERR("Invalid device ID 0x%x.", device_id);
172 return -EINVAL;
173 }
174
175 if (HIDS_setOutputDataRate(&data->sensor_interface, config->odr) != WE_SUCCESS) {
176 LOG_ERR("Failed to set output data rate.");
177 return -EIO;
178 }
179
180 if (HIDS_enableBlockDataUpdate(&data->sensor_interface, HIDS_enable) != WE_SUCCESS) {
181 LOG_ERR("Failed to enable block data update.");
182 return -EIO;
183 }
184
185 if (HIDS_setPowerMode(&data->sensor_interface, HIDS_activeMode) != WE_SUCCESS) {
186 LOG_ERR("Failed to set power mode.");
187 return -EIO;
188 }
189
190 if (HIDS_readCalibrationData(&data->sensor_interface) != WE_SUCCESS) {
191 LOG_ERR("Failed to read calibration data.");
192 return -EIO;
193 }
194
195 #if CONFIG_WSEN_HIDS_TRIGGER
196 int status = hids_init_interrupt(dev);
197
198 if (status < 0) {
199 LOG_ERR("Failed to initialize data-ready interrupt.");
200 return status;
201 }
202 #endif
203
204 return 0;
205 }
206
207 #if DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 0
208 #warning "HIDS driver enabled without any devices"
209 #endif
210
211 /*
212 * Device creation macros
213 */
214
215 #define HIDS_DEVICE_INIT(inst) \
216 SENSOR_DEVICE_DT_INST_DEFINE(inst, \
217 hids_init, \
218 NULL, \
219 &hids_data_##inst, \
220 &hids_config_##inst, \
221 POST_KERNEL, \
222 CONFIG_SENSOR_INIT_PRIORITY, \
223 &hids_driver_api);
224
225 #ifdef CONFIG_WSEN_HIDS_TRIGGER
226 #define HIDS_CFG_IRQ(inst) .gpio_drdy = GPIO_DT_SPEC_INST_GET(inst, drdy_gpios)
227 #else
228 #define HIDS_CFG_IRQ(inst)
229 #endif /* CONFIG_WSEN_HIDS_TRIGGER */
230
231 #define HIDS_CONFIG_COMMON(inst) \
232 .odr = (HIDS_outputDataRate_t)(DT_INST_ENUM_IDX(inst, odr) + 1), \
233 COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, drdy_gpios), \
234 (HIDS_CFG_IRQ(inst)), ())
235
236 /*
237 * Instantiation macros used when device is on SPI bus.
238 */
239
240 #define HIDS_SPI_OPERATION (SPI_WORD_SET(8) | SPI_OP_MODE_MASTER | SPI_MODE_CPOL | SPI_MODE_CPHA)
241
242 #define HIDS_CONFIG_SPI(inst) \
243 { \
244 .bus_cfg = { \
245 .spi = SPI_DT_SPEC_INST_GET(inst, \
246 HIDS_SPI_OPERATION, \
247 0), \
248 }, \
249 HIDS_CONFIG_COMMON(inst) \
250 }
251
252 /*
253 * Instantiation macros used when device is on I2C bus.
254 */
255
256 #define HIDS_CONFIG_I2C(inst) \
257 { \
258 .bus_cfg = { \
259 .i2c = I2C_DT_SPEC_INST_GET(inst), \
260 }, \
261 HIDS_CONFIG_COMMON(inst) \
262 }
263
264 /*
265 * Main instantiation macro. Use of COND_CODE_1() selects the right
266 * bus-specific macro at preprocessor time.
267 */
268 #define HIDS_DEFINE(inst) \
269 static struct hids_data hids_data_##inst = \
270 COND_CODE_1(DT_INST_ON_BUS(inst, i2c), \
271 ({ .sensor_interface = { .interfaceType = WE_i2c } }), ()) \
272 COND_CODE_1(DT_INST_ON_BUS(inst, spi), \
273 ({ .sensor_interface = { .interfaceType = WE_spi } }), ());\
274 static const struct hids_config hids_config_##inst = \
275 COND_CODE_1(DT_INST_ON_BUS(inst, i2c), (HIDS_CONFIG_I2C(inst)), ()) \
276 COND_CODE_1(DT_INST_ON_BUS(inst, spi), (HIDS_CONFIG_SPI(inst)), ()); \
277 HIDS_DEVICE_INIT(inst)
278
279 DT_INST_FOREACH_STATUS_OKAY(HIDS_DEFINE)
280