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_tids
8
9 #include <stdlib.h>
10
11 #include <zephyr/sys/__assert.h>
12 #include <zephyr/sys/byteorder.h>
13 #include <zephyr/logging/log.h>
14
15 #include "wsen_tids.h"
16
17 LOG_MODULE_REGISTER(WSEN_TIDS, CONFIG_SENSOR_LOG_LEVEL);
18
19 /*
20 * List of supported output data rates. Index into this list is used as
21 * argument for TIDS_setOutputDataRate()
22 */
23 static const int32_t tids_odr_list[] = {
24 25,
25 50,
26 100,
27 200,
28 };
29
tids_sample_fetch(const struct device * dev,enum sensor_channel chan)30 static int tids_sample_fetch(const struct device *dev, enum sensor_channel chan)
31 {
32 struct tids_data *data = dev->data;
33 int16_t raw_temperature;
34
35 if ((chan != SENSOR_CHAN_ALL) && (chan != SENSOR_CHAN_AMBIENT_TEMP)) {
36 LOG_ERR("Fetching is not supported on channel %d.", chan);
37 return -EINVAL;
38 }
39
40 if (TIDS_getRawTemperature(&data->sensor_interface, &raw_temperature) != WE_SUCCESS) {
41 LOG_ERR("Failed to fetch data sample");
42 return -EIO;
43 }
44
45 data->temperature = raw_temperature;
46
47 return 0;
48 }
49
tids_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)50 static int tids_channel_get(const struct device *dev, enum sensor_channel chan,
51 struct sensor_value *val)
52 {
53 struct tids_data *data = dev->data;
54
55 if (chan == SENSOR_CHAN_AMBIENT_TEMP) {
56 /* Convert temperature from 0.01 degrees Celsius to degrees Celsius */
57 val->val1 = data->temperature / 100;
58 val->val2 = ((int32_t)data->temperature % 100) * (1000000 / 100);
59 } else {
60 return -ENOTSUP;
61 }
62
63 return 0;
64 }
65
66 /* Set output data rate. See tids_odr_list for allowed values. */
tids_odr_set(const struct device * dev,const struct sensor_value * odr)67 static int tids_odr_set(const struct device *dev, const struct sensor_value *odr)
68 {
69 struct tids_data *data = dev->data;
70 int odr_index;
71
72 for (odr_index = 0; odr_index < ARRAY_SIZE(tids_odr_list); odr_index++) {
73 if (odr->val1 == tids_odr_list[odr_index] && odr->val2 == 0) {
74 break;
75 }
76 }
77
78 if (odr_index == ARRAY_SIZE(tids_odr_list)) {
79 /* ODR not allowed (was not found in tids_odr_list) */
80 LOG_ERR("Bad sampling frequency %d.%d", odr->val1, abs(odr->val2));
81 return -EINVAL;
82 }
83
84 if (TIDS_setOutputDataRate(&data->sensor_interface, (TIDS_outputDataRate_t)odr_index) !=
85 WE_SUCCESS) {
86 LOG_ERR("Failed to set output data rate");
87 return -EIO;
88 }
89
90 return 0;
91 }
92
tids_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)93 static int tids_attr_set(const struct device *dev, enum sensor_channel chan,
94 enum sensor_attribute attr, const struct sensor_value *val)
95 {
96 if (chan != SENSOR_CHAN_ALL) {
97 LOG_WRN("attr_set() is not supported on channel %d.", chan);
98 return -ENOTSUP;
99 }
100
101 switch (attr) {
102 case SENSOR_ATTR_SAMPLING_FREQUENCY:
103 return tids_odr_set(dev, val);
104
105 #ifdef CONFIG_WSEN_TIDS_TRIGGER
106 case SENSOR_ATTR_LOWER_THRESH:
107 return tids_threshold_set(dev, val, false);
108
109 case SENSOR_ATTR_UPPER_THRESH:
110 return tids_threshold_set(dev, val, true);
111 #endif /* CONFIG_WSEN_TIDS_TRIGGER */
112
113 default:
114 LOG_ERR("Operation not supported.");
115 return -ENOTSUP;
116 }
117 }
118
119 static const struct sensor_driver_api tids_driver_api = {
120 .attr_set = tids_attr_set,
121 #if CONFIG_WSEN_TIDS_TRIGGER
122 .trigger_set = tids_trigger_set,
123 #endif
124 .sample_fetch = tids_sample_fetch,
125 .channel_get = tids_channel_get,
126 };
127
tids_init(const struct device * dev)128 static int tids_init(const struct device *dev)
129 {
130 const struct tids_config *const config = dev->config;
131 struct tids_data *data = dev->data;
132 int status;
133 uint8_t device_id;
134 struct sensor_value odr;
135
136 /* Initialize WE sensor interface */
137 TIDS_getDefaultInterface(&data->sensor_interface);
138 data->sensor_interface.interfaceType = WE_i2c;
139 data->sensor_interface.handle = (void *)&config->bus_cfg.i2c;
140
141 /* First communication test - check device ID */
142 if (TIDS_getDeviceID(&data->sensor_interface, &device_id) != WE_SUCCESS) {
143 LOG_ERR("Failed to read device ID.");
144 return -EIO;
145 }
146
147 if (device_id != TIDS_DEVICE_ID_VALUE) {
148 LOG_ERR("Invalid device ID 0x%x.", device_id);
149 return -EIO;
150 }
151
152 /* Reset the sensor with an arbitrary off time of 5 us */
153 TIDS_softReset(&data->sensor_interface, TIDS_enable);
154 k_sleep(K_USEC(5));
155 TIDS_softReset(&data->sensor_interface, TIDS_disable);
156
157 odr.val1 = tids_odr_list[config->odr];
158 odr.val2 = 0;
159 status = tids_odr_set(dev, &odr);
160 if (status < 0) {
161 LOG_ERR("Failed to set output data rate.");
162 return status;
163 }
164
165 if (TIDS_enableBlockDataUpdate(&data->sensor_interface, TIDS_enable) != WE_SUCCESS) {
166 LOG_ERR("Failed to enable block data update.");
167 return -EIO;
168 }
169
170 if (TIDS_enableContinuousMode(&data->sensor_interface, TIDS_enable) != WE_SUCCESS) {
171 LOG_ERR("Failed to enable continuous mode.");
172 return -EIO;
173 }
174
175 #ifdef CONFIG_WSEN_TIDS_TRIGGER
176 status = tids_init_interrupt(dev);
177 if (status < 0) {
178 LOG_ERR("Failed to initialize threshold interrupt.");
179 return status;
180 }
181 #endif
182
183 return 0;
184 }
185
186 #if DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 0
187 #warning "TIDS driver enabled without any devices"
188 #endif
189
190 /*
191 * Device creation macros
192 */
193
194 #define TIDS_DEVICE_INIT(inst) \
195 SENSOR_DEVICE_DT_INST_DEFINE(inst, \
196 tids_init, \
197 NULL, \
198 &tids_data_##inst, \
199 &tids_config_##inst, \
200 POST_KERNEL, \
201 CONFIG_SENSOR_INIT_PRIORITY, \
202 &tids_driver_api);
203
204 #ifdef CONFIG_WSEN_TIDS_TRIGGER
205 #define TIDS_CFG_IRQ(inst) \
206 .gpio_threshold = GPIO_DT_SPEC_INST_GET(inst, int_gpios), \
207 .high_threshold = DT_INST_PROP(inst, temp_high_threshold), \
208 .low_threshold = DT_INST_PROP(inst, temp_low_threshold)
209 #else
210 #define TIDS_CFG_IRQ(inst)
211 #endif /* CONFIG_WSEN_TIDS_TRIGGER */
212
213 /*
214 * Main instantiation macro.
215 */
216 #define TIDS_DEFINE(inst) \
217 static struct tids_data tids_data_##inst; \
218 static const struct tids_config tids_config_##inst = \
219 { \
220 .bus_cfg = { \
221 .i2c = I2C_DT_SPEC_INST_GET(inst), \
222 }, \
223 .odr = (TIDS_outputDataRate_t)(DT_INST_ENUM_IDX(inst, odr)), \
224 COND_CODE_1(DT_INST_NODE_HAS_PROP(inst, int_gpios), \
225 (TIDS_CFG_IRQ(inst)), ()) \
226 }; \
227 TIDS_DEVICE_INIT(inst)
228
229 DT_INST_FOREACH_STATUS_OKAY(TIDS_DEFINE)
230