1 /*
2  * Copyright (c) 2023 Trackunit Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "bmi323.h"
8 #include "bmi323_spi.h"
9 
10 #include <zephyr/kernel.h>
11 #include <zephyr/device.h>
12 #include <zephyr/drivers/gpio.h>
13 #include <zephyr/drivers/sensor.h>
14 #include <zephyr/pm/device.h>
15 #include <zephyr/pm/device_runtime.h>
16 
17 #include <zephyr/logging/log.h>
18 LOG_MODULE_REGISTER(bosch_bmi323);
19 
20 #define DT_DRV_COMPAT bosch_bmi323
21 
22 /* Value taken from BMI323 Datasheet section 5.8.1 */
23 #define IMU_BOSCH_FEATURE_ENGINE_STARTUP_CONFIG (0x012C)
24 
25 #define IMU_BOSCH_DIE_TEMP_OFFSET_MICRO_DEG_CELCIUS (23000000LL)
26 #define IMU_BOSCH_DIE_TEMP_MICRO_DEG_CELCIUS_LSB    (1953L)
27 
28 typedef void (*bosch_bmi323_gpio_callback_ptr)(const struct device *dev, struct gpio_callback *cb,
29 					       uint32_t pins);
30 
31 struct bosch_bmi323_config {
32 	const struct bosch_bmi323_bus *bus;
33 	const struct gpio_dt_spec int_gpio;
34 
35 	const bosch_bmi323_gpio_callback_ptr int_gpio_callback;
36 };
37 
38 struct bosch_bmi323_data {
39 	struct k_mutex lock;
40 
41 	struct sensor_value acc_samples[3];
42 	struct sensor_value gyro_samples[3];
43 	struct sensor_value temperature;
44 
45 	bool acc_samples_valid;
46 	bool gyro_samples_valid;
47 	bool temperature_valid;
48 
49 	uint32_t acc_full_scale;
50 	uint32_t gyro_full_scale;
51 
52 	struct gpio_callback gpio_callback;
53 	const struct sensor_trigger *trigger;
54 	sensor_trigger_handler_t trigger_handler;
55 	struct k_work callback_work;
56 	const struct device *dev;
57 };
58 
bosch_bmi323_bus_init(const struct device * dev)59 static int bosch_bmi323_bus_init(const struct device *dev)
60 {
61 	const struct bosch_bmi323_config *config = (const struct bosch_bmi323_config *)dev->config;
62 
63 	const struct bosch_bmi323_bus *bus = config->bus;
64 
65 	return bus->api->init(bus->context);
66 }
67 
bosch_bmi323_bus_read_words(const struct device * dev,uint8_t offset,uint16_t * words,uint16_t words_count)68 static int bosch_bmi323_bus_read_words(const struct device *dev, uint8_t offset, uint16_t *words,
69 				       uint16_t words_count)
70 {
71 	const struct bosch_bmi323_config *config = (const struct bosch_bmi323_config *)dev->config;
72 
73 	const struct bosch_bmi323_bus *bus = config->bus;
74 
75 	return bus->api->read_words(bus->context, offset, words, words_count);
76 }
77 
bosch_bmi323_bus_write_words(const struct device * dev,uint8_t offset,uint16_t * words,uint16_t words_count)78 static int bosch_bmi323_bus_write_words(const struct device *dev, uint8_t offset, uint16_t *words,
79 					uint16_t words_count)
80 {
81 	const struct bosch_bmi323_config *config = (const struct bosch_bmi323_config *)dev->config;
82 
83 	const struct bosch_bmi323_bus *bus = config->bus;
84 
85 	return bus->api->write_words(bus->context, offset, words, words_count);
86 }
87 
bosch_bmi323_lsb_from_fullscale(int64_t fullscale)88 static int32_t bosch_bmi323_lsb_from_fullscale(int64_t fullscale)
89 {
90 	return (fullscale * 1000) / INT16_MAX;
91 }
92 
93 /* lsb is the value of one 1/1000000 LSB */
bosch_bmi323_value_to_micro(int16_t value,int32_t lsb)94 static int64_t bosch_bmi323_value_to_micro(int16_t value, int32_t lsb)
95 {
96 	return ((int64_t)value) * lsb;
97 }
98 
99 /* lsb is the value of one 1/1000000 LSB */
bosch_bmi323_value_to_sensor_value(struct sensor_value * result,int16_t value,int32_t lsb)100 static void bosch_bmi323_value_to_sensor_value(struct sensor_value *result, int16_t value,
101 					       int32_t lsb)
102 {
103 	int64_t ll_value = (int64_t)value * lsb;
104 	int32_t int_part = (int32_t)(ll_value / 1000000);
105 	int32_t frac_part = (int32_t)(ll_value % 1000000);
106 
107 	result->val1 = int_part;
108 	result->val2 = frac_part;
109 }
110 
bosch_bmi323_sensor_value_from_micro(struct sensor_value * result,int64_t micro)111 static void bosch_bmi323_sensor_value_from_micro(struct sensor_value *result, int64_t micro)
112 {
113 	int32_t int_part = (int32_t)(micro / 1000000);
114 	int32_t frac_part = (int32_t)(micro % 1000000);
115 
116 	result->val1 = int_part;
117 	result->val2 = frac_part;
118 }
119 
bosch_bmi323_value_is_valid(int16_t value)120 static bool bosch_bmi323_value_is_valid(int16_t value)
121 {
122 	return ((uint16_t)value == 0x8000) ? false : true;
123 }
124 
bosch_bmi323_validate_chip_id(const struct device * dev)125 static int bosch_bmi323_validate_chip_id(const struct device *dev)
126 {
127 	uint16_t sensor_id;
128 	int ret;
129 
130 	ret = bosch_bmi323_bus_read_words(dev, 0, &sensor_id, 1);
131 
132 	if (ret < 0) {
133 		return ret;
134 	}
135 
136 	if ((sensor_id & 0xFF) != 0x43) {
137 		return -ENODEV;
138 	}
139 
140 	return 0;
141 }
142 
bosch_bmi323_soft_reset(const struct device * dev)143 static int bosch_bmi323_soft_reset(const struct device *dev)
144 {
145 	uint16_t cmd;
146 	int ret;
147 
148 	cmd = IMU_BOSCH_BMI323_REG_VALUE(CMD, CMD, SOFT_RESET);
149 
150 	ret = bosch_bmi323_bus_write_words(dev, IMU_BOSCH_BMI323_REG_CMD, &cmd, 1);
151 
152 	if (ret < 0) {
153 		return ret;
154 	}
155 
156 	k_usleep(1500);
157 
158 	return 0;
159 }
160 
bosch_bmi323_enable_feature_engine(const struct device * dev)161 static int bosch_bmi323_enable_feature_engine(const struct device *dev)
162 {
163 	uint16_t buf;
164 	int ret;
165 
166 	buf = IMU_BOSCH_FEATURE_ENGINE_STARTUP_CONFIG;
167 
168 	ret = bosch_bmi323_bus_write_words(dev, IMU_BOSCH_BMI323_REG_FEATURE_IO2, &buf, 1);
169 
170 	if (ret < 0) {
171 		return ret;
172 	}
173 
174 	buf = IMU_BOSCH_BMI323_REG_VALUE(FEATURE_IO_STATUS, STATUS, SET);
175 
176 	ret = bosch_bmi323_bus_write_words(dev, IMU_BOSCH_BMI323_REG_FEATURE_IO_STATUS, &buf, 1);
177 
178 	if (ret < 0) {
179 		return ret;
180 	}
181 
182 	buf = IMU_BOSCH_BMI323_REG_VALUE(FEATURE_CTRL, ENABLE, EN);
183 
184 	return bosch_bmi323_bus_write_words(dev, IMU_BOSCH_BMI323_REG_FEATURE_CTRL, &buf, 1);
185 }
186 
bosch_bmi323_driver_api_set_acc_odr(const struct device * dev,const struct sensor_value * val)187 static int bosch_bmi323_driver_api_set_acc_odr(const struct device *dev,
188 					       const struct sensor_value *val)
189 {
190 	int ret;
191 	uint16_t acc_conf;
192 	int64_t odr = sensor_value_to_milli(val);
193 
194 	ret = bosch_bmi323_bus_read_words(dev, IMU_BOSCH_BMI323_REG_ACC_CONF, &acc_conf, 1);
195 
196 	if (ret < 0) {
197 		return ret;
198 	}
199 
200 	acc_conf &= ~IMU_BOSCH_BMI323_REG_MASK(ACC_CONF, ODR);
201 
202 	if (odr <= 782) {
203 		acc_conf |= IMU_BOSCH_BMI323_REG_VALUE(ACC_CONF, ODR, HZ0P78125);
204 	} else if (odr <= 1563) {
205 		acc_conf |= IMU_BOSCH_BMI323_REG_VALUE(ACC_CONF, ODR, HZ1P5625);
206 	} else if (odr <= 3125) {
207 		acc_conf |= IMU_BOSCH_BMI323_REG_VALUE(ACC_CONF, ODR, HZ3P125);
208 	} else if (odr <= 6250) {
209 		acc_conf |= IMU_BOSCH_BMI323_REG_VALUE(ACC_CONF, ODR, HZ6P25);
210 	} else if (odr <= 12500) {
211 		acc_conf |= IMU_BOSCH_BMI323_REG_VALUE(ACC_CONF, ODR, HZ12P5);
212 	} else if (odr <= 25000) {
213 		acc_conf |= IMU_BOSCH_BMI323_REG_VALUE(ACC_CONF, ODR, HZ25);
214 	} else if (odr <= 50000) {
215 		acc_conf |= IMU_BOSCH_BMI323_REG_VALUE(ACC_CONF, ODR, HZ50);
216 	} else if (odr <= 100000) {
217 		acc_conf |= IMU_BOSCH_BMI323_REG_VALUE(ACC_CONF, ODR, HZ100);
218 	} else if (odr <= 200000) {
219 		acc_conf |= IMU_BOSCH_BMI323_REG_VALUE(ACC_CONF, ODR, HZ200);
220 	} else if (odr <= 400000) {
221 		acc_conf |= IMU_BOSCH_BMI323_REG_VALUE(ACC_CONF, ODR, HZ400);
222 	} else if (odr <= 800000) {
223 		acc_conf |= IMU_BOSCH_BMI323_REG_VALUE(ACC_CONF, ODR, HZ800);
224 	} else if (odr <= 1600000) {
225 		acc_conf |= IMU_BOSCH_BMI323_REG_VALUE(ACC_CONF, ODR, HZ1600);
226 	} else if (odr <= 3200000) {
227 		acc_conf |= IMU_BOSCH_BMI323_REG_VALUE(ACC_CONF, ODR, HZ3200);
228 	} else {
229 		acc_conf |= IMU_BOSCH_BMI323_REG_VALUE(ACC_CONF, ODR, HZ6400);
230 	}
231 
232 	return bosch_bmi323_bus_write_words(dev, IMU_BOSCH_BMI323_REG_ACC_CONF, &acc_conf, 1);
233 }
234 
bosch_bmi323_driver_api_set_acc_full_scale(const struct device * dev,const struct sensor_value * val)235 static int bosch_bmi323_driver_api_set_acc_full_scale(const struct device *dev,
236 						      const struct sensor_value *val)
237 {
238 	struct bosch_bmi323_data *data = (struct bosch_bmi323_data *)dev->data;
239 	int ret;
240 	uint16_t acc_conf;
241 	int64_t fullscale = sensor_value_to_milli(val);
242 
243 	ret = bosch_bmi323_bus_read_words(dev, IMU_BOSCH_BMI323_REG_ACC_CONF, &acc_conf, 1);
244 
245 	if (ret < 0) {
246 		return ret;
247 	}
248 
249 	acc_conf &= ~IMU_BOSCH_BMI323_REG_MASK(ACC_CONF, RANGE);
250 
251 	if (fullscale <= 2000) {
252 		acc_conf |= IMU_BOSCH_BMI323_REG_VALUE(ACC_CONF, RANGE, G2);
253 	} else if (fullscale <= 4000) {
254 		acc_conf |= IMU_BOSCH_BMI323_REG_VALUE(ACC_CONF, RANGE, G4);
255 	} else if (fullscale <= 8000) {
256 		acc_conf |= IMU_BOSCH_BMI323_REG_VALUE(ACC_CONF, RANGE, G8);
257 	} else {
258 		acc_conf |= IMU_BOSCH_BMI323_REG_VALUE(ACC_CONF, RANGE, G16);
259 	}
260 
261 	data->acc_full_scale = 0;
262 
263 	return bosch_bmi323_bus_write_words(dev, IMU_BOSCH_BMI323_REG_ACC_CONF, &acc_conf, 1);
264 }
265 
bosch_bmi323_driver_api_set_acc_feature_mask(const struct device * dev,const struct sensor_value * val)266 static int bosch_bmi323_driver_api_set_acc_feature_mask(const struct device *dev,
267 							const struct sensor_value *val)
268 {
269 	int ret;
270 	uint16_t acc_conf;
271 
272 	ret = bosch_bmi323_bus_read_words(dev, IMU_BOSCH_BMI323_REG_ACC_CONF, &acc_conf, 1);
273 
274 	if (ret < 0) {
275 		return ret;
276 	}
277 
278 	acc_conf &= ~IMU_BOSCH_BMI323_REG_MASK(ACC_CONF, MODE);
279 
280 	if (val->val1) {
281 		acc_conf |= IMU_BOSCH_BMI323_REG_VALUE(ACC_CONF, MODE, HPWR);
282 	} else {
283 		acc_conf |= IMU_BOSCH_BMI323_REG_VALUE(ACC_CONF, MODE, DIS);
284 	}
285 
286 	return bosch_bmi323_bus_write_words(dev, IMU_BOSCH_BMI323_REG_ACC_CONF, &acc_conf, 1);
287 }
288 
bosch_bmi323_driver_api_set_gyro_odr(const struct device * dev,const struct sensor_value * val)289 static int bosch_bmi323_driver_api_set_gyro_odr(const struct device *dev,
290 						const struct sensor_value *val)
291 {
292 	int ret;
293 	uint16_t gyro_conf;
294 	int64_t odr = sensor_value_to_milli(val);
295 
296 	ret = bosch_bmi323_bus_read_words(dev, IMU_BOSCH_BMI323_REG_GYRO_CONF, &gyro_conf, 1);
297 
298 	if (ret < 0) {
299 		return ret;
300 	}
301 
302 	gyro_conf &= ~IMU_BOSCH_BMI323_REG_MASK(GYRO_CONF, ODR);
303 
304 	if (odr <= 782) {
305 		gyro_conf |= IMU_BOSCH_BMI323_REG_VALUE(GYRO_CONF, ODR, HZ0P78125);
306 	} else if (odr <= 1563) {
307 		gyro_conf |= IMU_BOSCH_BMI323_REG_VALUE(GYRO_CONF, ODR, HZ1P5625);
308 	} else if (odr <= 3125) {
309 		gyro_conf |= IMU_BOSCH_BMI323_REG_VALUE(GYRO_CONF, ODR, HZ3P125);
310 	} else if (odr <= 6250) {
311 		gyro_conf |= IMU_BOSCH_BMI323_REG_VALUE(GYRO_CONF, ODR, HZ6P25);
312 	} else if (odr <= 12500) {
313 		gyro_conf |= IMU_BOSCH_BMI323_REG_VALUE(GYRO_CONF, ODR, HZ12P5);
314 	} else if (odr <= 25000) {
315 		gyro_conf |= IMU_BOSCH_BMI323_REG_VALUE(GYRO_CONF, ODR, HZ25);
316 	} else if (odr <= 50000) {
317 		gyro_conf |= IMU_BOSCH_BMI323_REG_VALUE(GYRO_CONF, ODR, HZ50);
318 	} else if (odr <= 100000) {
319 		gyro_conf |= IMU_BOSCH_BMI323_REG_VALUE(GYRO_CONF, ODR, HZ100);
320 	} else if (odr <= 200000) {
321 		gyro_conf |= IMU_BOSCH_BMI323_REG_VALUE(GYRO_CONF, ODR, HZ200);
322 	} else if (odr <= 400000) {
323 		gyro_conf |= IMU_BOSCH_BMI323_REG_VALUE(GYRO_CONF, ODR, HZ400);
324 	} else if (odr <= 800000) {
325 		gyro_conf |= IMU_BOSCH_BMI323_REG_VALUE(GYRO_CONF, ODR, HZ800);
326 	} else if (odr <= 1600000) {
327 		gyro_conf |= IMU_BOSCH_BMI323_REG_VALUE(GYRO_CONF, ODR, HZ1600);
328 	} else if (odr <= 3200000) {
329 		gyro_conf |= IMU_BOSCH_BMI323_REG_VALUE(GYRO_CONF, ODR, HZ3200);
330 	} else {
331 		gyro_conf |= IMU_BOSCH_BMI323_REG_VALUE(GYRO_CONF, ODR, HZ6400);
332 	}
333 
334 	return bosch_bmi323_bus_write_words(dev, IMU_BOSCH_BMI323_REG_GYRO_CONF, &gyro_conf, 1);
335 }
336 
bosch_bmi323_driver_api_set_gyro_full_scale(const struct device * dev,const struct sensor_value * val)337 static int bosch_bmi323_driver_api_set_gyro_full_scale(const struct device *dev,
338 						       const struct sensor_value *val)
339 {
340 	struct bosch_bmi323_data *data = (struct bosch_bmi323_data *)dev->data;
341 	int ret;
342 	uint16_t gyro_conf;
343 	int32_t fullscale = sensor_value_to_milli(val);
344 
345 	ret = bosch_bmi323_bus_read_words(dev, IMU_BOSCH_BMI323_REG_GYRO_CONF, &gyro_conf, 1);
346 
347 	if (ret < 0) {
348 		return ret;
349 	}
350 
351 	gyro_conf &= ~IMU_BOSCH_BMI323_REG_MASK(GYRO_CONF, RANGE);
352 
353 	if (fullscale <= 125000) {
354 		gyro_conf |= IMU_BOSCH_BMI323_REG_VALUE(GYRO_CONF, RANGE, DPS125);
355 	} else if (fullscale <= 250000) {
356 		gyro_conf |= IMU_BOSCH_BMI323_REG_VALUE(GYRO_CONF, RANGE, DPS250);
357 	} else if (fullscale <= 500000) {
358 		gyro_conf |= IMU_BOSCH_BMI323_REG_VALUE(GYRO_CONF, RANGE, DPS500);
359 	} else if (fullscale <= 1000000) {
360 		gyro_conf |= IMU_BOSCH_BMI323_REG_VALUE(GYRO_CONF, RANGE, DPS1000);
361 	} else {
362 		gyro_conf |= IMU_BOSCH_BMI323_REG_VALUE(GYRO_CONF, RANGE, DPS2000);
363 	}
364 
365 	data->gyro_full_scale = 0;
366 
367 	return bosch_bmi323_bus_write_words(dev, IMU_BOSCH_BMI323_REG_GYRO_CONF, &gyro_conf, 1);
368 }
369 
bosch_bmi323_driver_api_set_gyro_feature_mask(const struct device * dev,const struct sensor_value * val)370 static int bosch_bmi323_driver_api_set_gyro_feature_mask(const struct device *dev,
371 							 const struct sensor_value *val)
372 {
373 	int ret;
374 	uint16_t gyro_conf;
375 
376 	ret = bosch_bmi323_bus_read_words(dev, IMU_BOSCH_BMI323_REG_GYRO_CONF, &gyro_conf, 1);
377 
378 	if (ret < 0) {
379 		return ret;
380 	}
381 
382 	gyro_conf &= ~IMU_BOSCH_BMI323_REG_MASK(GYRO_CONF, MODE);
383 
384 	if (val->val1) {
385 		gyro_conf |= IMU_BOSCH_BMI323_REG_VALUE(GYRO_CONF, MODE, HPWR);
386 	} else {
387 		gyro_conf |= IMU_BOSCH_BMI323_REG_VALUE(GYRO_CONF, MODE, DIS);
388 	}
389 
390 	return bosch_bmi323_bus_write_words(dev, IMU_BOSCH_BMI323_REG_GYRO_CONF, &gyro_conf, 1);
391 }
392 
bosch_bmi323_driver_api_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)393 static int bosch_bmi323_driver_api_attr_set(const struct device *dev, enum sensor_channel chan,
394 					    enum sensor_attribute attr,
395 					    const struct sensor_value *val)
396 {
397 	struct bosch_bmi323_data *data = (struct bosch_bmi323_data *)dev->data;
398 	int ret;
399 
400 	k_mutex_lock(&data->lock, K_FOREVER);
401 
402 	switch (chan) {
403 	case SENSOR_CHAN_ACCEL_XYZ:
404 		switch (attr) {
405 		case SENSOR_ATTR_SAMPLING_FREQUENCY:
406 			ret = bosch_bmi323_driver_api_set_acc_odr(dev, val);
407 
408 			break;
409 
410 		case SENSOR_ATTR_FULL_SCALE:
411 			ret = bosch_bmi323_driver_api_set_acc_full_scale(dev, val);
412 
413 			break;
414 
415 		case SENSOR_ATTR_FEATURE_MASK:
416 			ret = bosch_bmi323_driver_api_set_acc_feature_mask(dev, val);
417 
418 			break;
419 
420 		default:
421 			ret = -ENODEV;
422 
423 			break;
424 		}
425 
426 		break;
427 
428 	case SENSOR_CHAN_GYRO_XYZ:
429 		switch (attr) {
430 		case SENSOR_ATTR_SAMPLING_FREQUENCY:
431 			ret = bosch_bmi323_driver_api_set_gyro_odr(dev, val);
432 
433 			break;
434 
435 		case SENSOR_ATTR_FULL_SCALE:
436 			ret = bosch_bmi323_driver_api_set_gyro_full_scale(dev, val);
437 
438 			break;
439 
440 		case SENSOR_ATTR_FEATURE_MASK:
441 			ret = bosch_bmi323_driver_api_set_gyro_feature_mask(dev, val);
442 
443 			break;
444 
445 		default:
446 			ret = -ENODEV;
447 
448 			break;
449 		}
450 
451 		break;
452 
453 	default:
454 		ret = -ENODEV;
455 
456 		break;
457 	}
458 
459 	k_mutex_unlock(&data->lock);
460 
461 	return ret;
462 }
463 
bosch_bmi323_driver_api_get_acc_odr(const struct device * dev,struct sensor_value * val)464 static int bosch_bmi323_driver_api_get_acc_odr(const struct device *dev, struct sensor_value *val)
465 {
466 	uint16_t acc_conf;
467 	int ret;
468 
469 	ret = bosch_bmi323_bus_read_words(dev, IMU_BOSCH_BMI323_REG_ACC_CONF, &acc_conf, 1);
470 
471 	if (ret < 0) {
472 		return ret;
473 	}
474 
475 	switch (IMU_BOSCH_BMI323_REG_VALUE_GET_FIELD(acc_conf, ACC_CONF, ODR)) {
476 	case IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ0P78125:
477 		val->val1 = 0;
478 		val->val2 = 781250;
479 		break;
480 	case IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ1P5625:
481 		val->val1 = 1;
482 		val->val2 = 562500;
483 		break;
484 	case IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ3P125:
485 		val->val1 = 3;
486 		val->val2 = 125000;
487 		break;
488 	case IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ6P25:
489 		val->val1 = 6;
490 		val->val2 = 250000;
491 		break;
492 	case IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ12P5:
493 		val->val1 = 12;
494 		val->val2 = 500000;
495 		break;
496 	case IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ25:
497 		val->val1 = 25;
498 		val->val2 = 0;
499 		break;
500 	case IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ50:
501 		val->val1 = 50;
502 		val->val2 = 0;
503 		break;
504 	case IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ100:
505 		val->val1 = 100;
506 		val->val2 = 0;
507 		break;
508 	case IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ200:
509 		val->val1 = 200;
510 		val->val2 = 0;
511 		break;
512 	case IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ400:
513 		val->val1 = 400;
514 		val->val2 = 0;
515 		break;
516 	case IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ800:
517 		val->val1 = 800;
518 		val->val2 = 0;
519 		break;
520 	case IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ1600:
521 		val->val1 = 1600;
522 		val->val2 = 0;
523 		break;
524 	case IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ3200:
525 		val->val1 = 3200;
526 		val->val2 = 0;
527 		break;
528 	case IMU_BOSCH_BMI323_REG_ACC_CONF_ODR_VAL_HZ6400:
529 		val->val1 = 6400;
530 		val->val2 = 0;
531 		break;
532 	default:
533 		return -EINVAL;
534 	}
535 
536 	return 0;
537 }
538 
bosch_bmi323_driver_api_get_acc_full_scale(const struct device * dev,struct sensor_value * val)539 static int bosch_bmi323_driver_api_get_acc_full_scale(const struct device *dev,
540 						      struct sensor_value *val)
541 {
542 	uint16_t acc_conf;
543 	int ret;
544 
545 	ret = bosch_bmi323_bus_read_words(dev, IMU_BOSCH_BMI323_REG_ACC_CONF, &acc_conf, 1);
546 
547 	if (ret < 0) {
548 		return ret;
549 	}
550 
551 	switch (IMU_BOSCH_BMI323_REG_VALUE_GET_FIELD(acc_conf, ACC_CONF, RANGE)) {
552 	case IMU_BOSCH_BMI323_REG_ACC_CONF_RANGE_VAL_G2:
553 		val->val1 = 2;
554 		val->val2 = 0;
555 		break;
556 	case IMU_BOSCH_BMI323_REG_ACC_CONF_RANGE_VAL_G4:
557 		val->val1 = 4;
558 		val->val2 = 0;
559 		break;
560 	case IMU_BOSCH_BMI323_REG_ACC_CONF_RANGE_VAL_G8:
561 		val->val1 = 8;
562 		val->val2 = 0;
563 		break;
564 	case IMU_BOSCH_BMI323_REG_ACC_CONF_RANGE_VAL_G16:
565 		val->val1 = 16;
566 		val->val2 = 0;
567 		break;
568 	default:
569 		return -EINVAL;
570 	}
571 
572 	return 0;
573 }
574 
bosch_bmi323_driver_api_get_acc_feature_mask(const struct device * dev,struct sensor_value * val)575 static int bosch_bmi323_driver_api_get_acc_feature_mask(const struct device *dev,
576 							struct sensor_value *val)
577 {
578 	uint16_t acc_conf;
579 	int ret;
580 
581 	ret = bosch_bmi323_bus_read_words(dev, IMU_BOSCH_BMI323_REG_ACC_CONF, &acc_conf, 1);
582 
583 	if (ret < 0) {
584 		return ret;
585 	}
586 
587 	if (IMU_BOSCH_BMI323_REG_VALUE_GET_FIELD(acc_conf, ACC_CONF, MODE)) {
588 		val->val1 = 1;
589 		val->val2 = 0;
590 	} else {
591 		val->val1 = 0;
592 		val->val2 = 0;
593 	}
594 
595 	return 0;
596 }
597 
bosch_bmi323_driver_api_get_gyro_odr(const struct device * dev,struct sensor_value * val)598 static int bosch_bmi323_driver_api_get_gyro_odr(const struct device *dev, struct sensor_value *val)
599 {
600 	uint16_t gyro_conf;
601 	int ret;
602 
603 	ret = bosch_bmi323_bus_read_words(dev, IMU_BOSCH_BMI323_REG_GYRO_CONF, &gyro_conf, 1);
604 
605 	if (ret < 0) {
606 		return ret;
607 	}
608 
609 	switch (IMU_BOSCH_BMI323_REG_VALUE_GET_FIELD(gyro_conf, GYRO_CONF, ODR)) {
610 	case IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ0P78125:
611 		val->val1 = 0;
612 		val->val2 = 781250;
613 		break;
614 	case IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ1P5625:
615 		val->val1 = 1;
616 		val->val2 = 562500;
617 		break;
618 	case IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ3P125:
619 		val->val1 = 3;
620 		val->val2 = 125000;
621 		break;
622 	case IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ6P25:
623 		val->val1 = 6;
624 		val->val2 = 250000;
625 		break;
626 	case IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ12P5:
627 		val->val1 = 12;
628 		val->val2 = 500000;
629 		break;
630 	case IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ25:
631 		val->val1 = 25;
632 		val->val2 = 0;
633 		break;
634 	case IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ50:
635 		val->val1 = 50;
636 		val->val2 = 0;
637 		break;
638 	case IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ100:
639 		val->val1 = 100;
640 		val->val2 = 0;
641 		break;
642 	case IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ200:
643 		val->val1 = 200;
644 		val->val2 = 0;
645 		break;
646 	case IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ400:
647 		val->val1 = 400;
648 		val->val2 = 0;
649 		break;
650 	case IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ800:
651 		val->val1 = 800;
652 		val->val2 = 0;
653 		break;
654 	case IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ1600:
655 		val->val1 = 1600;
656 		val->val2 = 0;
657 		break;
658 	case IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ3200:
659 		val->val1 = 3200;
660 		val->val2 = 0;
661 		break;
662 	case IMU_BOSCH_BMI323_REG_GYRO_CONF_ODR_VAL_HZ6400:
663 		val->val1 = 6400;
664 		val->val2 = 0;
665 		break;
666 	default:
667 		return -EINVAL;
668 	}
669 
670 	return 0;
671 }
672 
bosch_bmi323_driver_api_get_gyro_full_scale(const struct device * dev,struct sensor_value * val)673 static int bosch_bmi323_driver_api_get_gyro_full_scale(const struct device *dev,
674 						       struct sensor_value *val)
675 {
676 	uint16_t gyro_conf;
677 	int ret;
678 
679 	ret = bosch_bmi323_bus_read_words(dev, IMU_BOSCH_BMI323_REG_GYRO_CONF, &gyro_conf, 1);
680 
681 	if (ret < 0) {
682 		return ret;
683 	}
684 
685 	switch (IMU_BOSCH_BMI323_REG_VALUE_GET_FIELD(gyro_conf, GYRO_CONF, RANGE)) {
686 	case IMU_BOSCH_BMI323_REG_GYRO_CONF_RANGE_VAL_DPS125:
687 		val->val1 = 125;
688 		val->val2 = 0;
689 		break;
690 	case IMU_BOSCH_BMI323_REG_GYRO_CONF_RANGE_VAL_DPS250:
691 		val->val1 = 250;
692 		val->val2 = 0;
693 		break;
694 	case IMU_BOSCH_BMI323_REG_GYRO_CONF_RANGE_VAL_DPS500:
695 		val->val1 = 500;
696 		val->val2 = 0;
697 		break;
698 	case IMU_BOSCH_BMI323_REG_GYRO_CONF_RANGE_VAL_DPS1000:
699 		val->val1 = 1000;
700 		val->val2 = 0;
701 		break;
702 	case IMU_BOSCH_BMI323_REG_GYRO_CONF_RANGE_VAL_DPS2000:
703 		val->val1 = 2000;
704 		val->val2 = 0;
705 		break;
706 	default:
707 		return -EINVAL;
708 	}
709 
710 	return 0;
711 }
712 
bosch_bmi323_driver_api_get_gyro_feature_mask(const struct device * dev,struct sensor_value * val)713 static int bosch_bmi323_driver_api_get_gyro_feature_mask(const struct device *dev,
714 							 struct sensor_value *val)
715 {
716 	uint16_t gyro_conf;
717 	int ret;
718 
719 	ret = bosch_bmi323_bus_read_words(dev, IMU_BOSCH_BMI323_REG_GYRO_CONF, &gyro_conf, 1);
720 
721 	if (ret < 0) {
722 		return ret;
723 	}
724 
725 	if (IMU_BOSCH_BMI323_REG_VALUE_GET_FIELD(gyro_conf, GYRO_CONF, MODE)) {
726 		val->val1 = 1;
727 		val->val2 = 0;
728 	} else {
729 		val->val1 = 0;
730 		val->val2 = 0;
731 	}
732 
733 	return 0;
734 }
735 
bosch_bmi323_driver_api_attr_get(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,struct sensor_value * val)736 static int bosch_bmi323_driver_api_attr_get(const struct device *dev, enum sensor_channel chan,
737 					    enum sensor_attribute attr, struct sensor_value *val)
738 {
739 	struct bosch_bmi323_data *data = (struct bosch_bmi323_data *)dev->data;
740 	int ret;
741 
742 	k_mutex_lock(&data->lock, K_FOREVER);
743 
744 	switch (chan) {
745 	case SENSOR_CHAN_ACCEL_XYZ:
746 		switch (attr) {
747 		case SENSOR_ATTR_SAMPLING_FREQUENCY:
748 			ret = bosch_bmi323_driver_api_get_acc_odr(dev, val);
749 
750 			break;
751 
752 		case SENSOR_ATTR_FULL_SCALE:
753 			ret = bosch_bmi323_driver_api_get_acc_full_scale(dev, val);
754 
755 			break;
756 
757 		case SENSOR_ATTR_FEATURE_MASK:
758 			ret = bosch_bmi323_driver_api_get_acc_feature_mask(dev, val);
759 
760 			break;
761 
762 		default:
763 			ret = -ENODEV;
764 
765 			break;
766 		}
767 
768 		break;
769 
770 	case SENSOR_CHAN_GYRO_XYZ:
771 		switch (attr) {
772 		case SENSOR_ATTR_SAMPLING_FREQUENCY:
773 			ret = bosch_bmi323_driver_api_get_gyro_odr(dev, val);
774 
775 			break;
776 
777 		case SENSOR_ATTR_FULL_SCALE:
778 			ret = bosch_bmi323_driver_api_get_gyro_full_scale(dev, val);
779 
780 			break;
781 
782 		case SENSOR_ATTR_FEATURE_MASK:
783 			ret = bosch_bmi323_driver_api_get_gyro_feature_mask(dev, val);
784 
785 			break;
786 
787 		default:
788 			ret = -ENODEV;
789 
790 			break;
791 		}
792 
793 		break;
794 
795 	default:
796 		ret = -ENODEV;
797 		break;
798 	}
799 
800 	k_mutex_unlock(&data->lock);
801 
802 	return ret;
803 }
804 
bosch_bmi323_driver_api_trigger_set_acc_drdy(const struct device * dev)805 static int bosch_bmi323_driver_api_trigger_set_acc_drdy(const struct device *dev)
806 {
807 	uint16_t buf[2];
808 
809 	buf[0] = 0;
810 	buf[1] = IMU_BOSCH_BMI323_REG_VALUE(INT_MAP2, ACC_DRDY_INT, INT1);
811 
812 	return bosch_bmi323_bus_write_words(dev, IMU_BOSCH_BMI323_REG_INT_MAP1, buf, 2);
813 }
814 
bosch_bmi323_driver_api_trigger_set_acc_motion(const struct device * dev)815 static int bosch_bmi323_driver_api_trigger_set_acc_motion(const struct device *dev)
816 {
817 	uint16_t buf[2];
818 	int ret;
819 
820 	buf[0] = IMU_BOSCH_BMI323_REG_VALUE(INT_MAP1, MOTION_OUT, INT1);
821 	buf[1] = 0;
822 
823 	ret = bosch_bmi323_bus_write_words(dev, IMU_BOSCH_BMI323_REG_INT_MAP1, buf, 2);
824 
825 	if (ret < 0) {
826 		return ret;
827 	}
828 
829 	buf[0] = 0;
830 
831 	ret = bosch_bmi323_bus_write_words(dev, IMU_BOSCH_BMI323_REG_FEATURE_IO0, buf, 1);
832 
833 	if (ret < 0) {
834 		return ret;
835 	}
836 
837 	buf[0] = IMU_BOSCH_BMI323_REG_VALUE(FEATURE_IO0, MOTION_X_EN, EN) |
838 		 IMU_BOSCH_BMI323_REG_VALUE(FEATURE_IO0, MOTION_Y_EN, EN) |
839 		 IMU_BOSCH_BMI323_REG_VALUE(FEATURE_IO0, MOTION_Z_EN, EN);
840 
841 	ret = bosch_bmi323_bus_write_words(dev, IMU_BOSCH_BMI323_REG_FEATURE_IO0, buf, 1);
842 
843 	if (ret < 0) {
844 		return ret;
845 	}
846 
847 	buf[0] = 1;
848 
849 	return bosch_bmi323_bus_write_words(dev, IMU_BOSCH_BMI323_REG_FEATURE_IO_STATUS, buf, 1);
850 }
851 
bosch_bmi323_driver_api_trigger_set(const struct device * dev,const struct sensor_trigger * trig,sensor_trigger_handler_t handler)852 static int bosch_bmi323_driver_api_trigger_set(const struct device *dev,
853 					       const struct sensor_trigger *trig,
854 					       sensor_trigger_handler_t handler)
855 {
856 	struct bosch_bmi323_data *data = (struct bosch_bmi323_data *)dev->data;
857 	int ret = -ENODEV;
858 
859 	k_mutex_lock(&data->lock, K_FOREVER);
860 
861 	data->trigger = trig;
862 	data->trigger_handler = handler;
863 
864 	switch (trig->chan) {
865 	case SENSOR_CHAN_ACCEL_XYZ:
866 		switch (trig->type) {
867 		case SENSOR_TRIG_DATA_READY:
868 			ret = bosch_bmi323_driver_api_trigger_set_acc_drdy(dev);
869 
870 			break;
871 
872 		case SENSOR_TRIG_MOTION:
873 			ret = bosch_bmi323_driver_api_trigger_set_acc_motion(dev);
874 
875 			break;
876 
877 		default:
878 			break;
879 		}
880 
881 		break;
882 
883 	default:
884 		break;
885 	}
886 
887 	k_mutex_unlock(&data->lock);
888 
889 	return ret;
890 }
891 
bosch_bmi323_driver_api_fetch_acc_samples(const struct device * dev)892 static int bosch_bmi323_driver_api_fetch_acc_samples(const struct device *dev)
893 {
894 	struct bosch_bmi323_data *data = (struct bosch_bmi323_data *)dev->data;
895 	struct sensor_value full_scale;
896 	int16_t *buf = (int16_t *)data->acc_samples;
897 	int ret;
898 	int32_t lsb;
899 
900 	if (data->acc_full_scale == 0) {
901 		ret = bosch_bmi323_driver_api_get_acc_full_scale(dev, &full_scale);
902 
903 		if (ret < 0) {
904 			return ret;
905 		}
906 
907 		data->acc_full_scale = sensor_value_to_milli(&full_scale);
908 	}
909 
910 	ret = bosch_bmi323_bus_read_words(dev, IMU_BOSCH_BMI323_REG_ACC_DATA_X, (uint16_t *)buf, 3);
911 
912 	if (ret < 0) {
913 		return ret;
914 	}
915 
916 	if ((bosch_bmi323_value_is_valid(buf[0]) == false) ||
917 	    (bosch_bmi323_value_is_valid(buf[1]) == false) ||
918 	    (bosch_bmi323_value_is_valid(buf[2]) == false)) {
919 		return -ENODATA;
920 	}
921 
922 	lsb = bosch_bmi323_lsb_from_fullscale(data->acc_full_scale);
923 
924 	/* Reuse vector backwards to avoid overwriting the raw values */
925 	bosch_bmi323_value_to_sensor_value(&data->acc_samples[2], buf[2], lsb);
926 	bosch_bmi323_value_to_sensor_value(&data->acc_samples[1], buf[1], lsb);
927 	bosch_bmi323_value_to_sensor_value(&data->acc_samples[0], buf[0], lsb);
928 
929 	data->acc_samples_valid = true;
930 
931 	return 0;
932 }
933 
bosch_bmi323_driver_api_fetch_gyro_samples(const struct device * dev)934 static int bosch_bmi323_driver_api_fetch_gyro_samples(const struct device *dev)
935 {
936 	struct bosch_bmi323_data *data = (struct bosch_bmi323_data *)dev->data;
937 	struct sensor_value full_scale;
938 	int16_t *buf = (int16_t *)data->gyro_samples;
939 	int ret;
940 	int32_t lsb;
941 
942 	if (data->gyro_full_scale == 0) {
943 		ret = bosch_bmi323_driver_api_get_gyro_full_scale(dev, &full_scale);
944 
945 		if (ret < 0) {
946 			return ret;
947 		}
948 
949 		data->gyro_full_scale = sensor_value_to_milli(&full_scale);
950 	}
951 
952 	ret = bosch_bmi323_bus_read_words(dev, IMU_BOSCH_BMI323_REG_GYRO_DATA_X, (uint16_t *)buf,
953 					  3);
954 
955 	if (ret < 0) {
956 		return ret;
957 	}
958 
959 	if ((bosch_bmi323_value_is_valid(buf[0]) == false) ||
960 	    (bosch_bmi323_value_is_valid(buf[1]) == false) ||
961 	    (bosch_bmi323_value_is_valid(buf[2]) == false)) {
962 		return -ENODATA;
963 	}
964 
965 	lsb = bosch_bmi323_lsb_from_fullscale(data->gyro_full_scale);
966 
967 	/* Reuse vector backwards to avoid overwriting the raw values */
968 	bosch_bmi323_value_to_sensor_value(&data->gyro_samples[2], buf[2], lsb);
969 	bosch_bmi323_value_to_sensor_value(&data->gyro_samples[1], buf[1], lsb);
970 	bosch_bmi323_value_to_sensor_value(&data->gyro_samples[0], buf[0], lsb);
971 
972 	data->gyro_samples_valid = true;
973 
974 	return 0;
975 }
976 
bosch_bmi323_driver_api_fetch_temperature(const struct device * dev)977 static int bosch_bmi323_driver_api_fetch_temperature(const struct device *dev)
978 {
979 	struct bosch_bmi323_data *data = (struct bosch_bmi323_data *)dev->data;
980 	int16_t buf;
981 	int64_t micro;
982 	int ret;
983 
984 	ret = bosch_bmi323_bus_read_words(dev, IMU_BOSCH_BMI323_REG_TEMP_DATA, &buf, 1);
985 
986 	if (ret < 0) {
987 		return ret;
988 	}
989 
990 	if (bosch_bmi323_value_is_valid(buf) == false) {
991 		return -ENODATA;
992 	}
993 
994 	micro = bosch_bmi323_value_to_micro(buf, IMU_BOSCH_DIE_TEMP_MICRO_DEG_CELCIUS_LSB);
995 
996 	micro += IMU_BOSCH_DIE_TEMP_OFFSET_MICRO_DEG_CELCIUS;
997 
998 	bosch_bmi323_sensor_value_from_micro(&data->temperature, micro);
999 
1000 	data->temperature_valid = true;
1001 
1002 	return 0;
1003 }
1004 
bosch_bmi323_driver_api_sample_fetch(const struct device * dev,enum sensor_channel chan)1005 static int bosch_bmi323_driver_api_sample_fetch(const struct device *dev, enum sensor_channel chan)
1006 {
1007 	struct bosch_bmi323_data *data = (struct bosch_bmi323_data *)dev->data;
1008 	int ret;
1009 
1010 	k_mutex_lock(&data->lock, K_FOREVER);
1011 
1012 	switch (chan) {
1013 	case SENSOR_CHAN_ACCEL_XYZ:
1014 		ret = bosch_bmi323_driver_api_fetch_acc_samples(dev);
1015 
1016 		break;
1017 
1018 	case SENSOR_CHAN_GYRO_XYZ:
1019 		ret = bosch_bmi323_driver_api_fetch_gyro_samples(dev);
1020 
1021 		break;
1022 
1023 	case SENSOR_CHAN_DIE_TEMP:
1024 		ret = bosch_bmi323_driver_api_fetch_temperature(dev);
1025 
1026 		break;
1027 
1028 	case SENSOR_CHAN_ALL:
1029 		ret = bosch_bmi323_driver_api_fetch_acc_samples(dev);
1030 
1031 		if (ret < 0) {
1032 			break;
1033 		}
1034 
1035 		ret = bosch_bmi323_driver_api_fetch_gyro_samples(dev);
1036 
1037 		if (ret < 0) {
1038 			break;
1039 		}
1040 
1041 		ret = bosch_bmi323_driver_api_fetch_temperature(dev);
1042 
1043 		break;
1044 
1045 	default:
1046 		ret = -ENODEV;
1047 
1048 		break;
1049 	}
1050 
1051 	k_mutex_unlock(&data->lock);
1052 
1053 	return ret;
1054 }
1055 
bosch_bmi323_driver_api_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)1056 static int bosch_bmi323_driver_api_channel_get(const struct device *dev, enum sensor_channel chan,
1057 					       struct sensor_value *val)
1058 {
1059 	struct bosch_bmi323_data *data = (struct bosch_bmi323_data *)dev->data;
1060 	int ret = 0;
1061 
1062 	k_mutex_lock(&data->lock, K_FOREVER);
1063 
1064 	switch (chan) {
1065 	case SENSOR_CHAN_ACCEL_XYZ:
1066 		if (data->acc_samples_valid == false) {
1067 			ret = -ENODATA;
1068 
1069 			break;
1070 		}
1071 
1072 		memcpy(val, data->acc_samples, sizeof(data->acc_samples));
1073 
1074 		break;
1075 
1076 	case SENSOR_CHAN_GYRO_XYZ:
1077 		if (data->gyro_samples_valid == false) {
1078 			ret = -ENODATA;
1079 
1080 			break;
1081 		}
1082 
1083 		memcpy(val, data->gyro_samples, sizeof(data->gyro_samples));
1084 
1085 		break;
1086 
1087 	case SENSOR_CHAN_DIE_TEMP:
1088 		if (data->temperature_valid == false) {
1089 			ret = -ENODATA;
1090 
1091 			break;
1092 		}
1093 
1094 		(*val) = data->temperature;
1095 
1096 		break;
1097 
1098 	default:
1099 		ret = -ENOTSUP;
1100 
1101 		break;
1102 	}
1103 
1104 	k_mutex_unlock(&data->lock);
1105 
1106 	return ret;
1107 }
1108 
1109 static DEVICE_API(sensor, bosch_bmi323_api) = {
1110 	.attr_set = bosch_bmi323_driver_api_attr_set,
1111 	.attr_get = bosch_bmi323_driver_api_attr_get,
1112 	.trigger_set = bosch_bmi323_driver_api_trigger_set,
1113 	.sample_fetch = bosch_bmi323_driver_api_sample_fetch,
1114 	.channel_get = bosch_bmi323_driver_api_channel_get,
1115 };
1116 
bosch_bmi323_irq_callback(const struct device * dev)1117 static void bosch_bmi323_irq_callback(const struct device *dev)
1118 {
1119 	struct bosch_bmi323_data *data = (struct bosch_bmi323_data *)dev->data;
1120 
1121 	k_work_submit(&data->callback_work);
1122 }
1123 
bosch_bmi323_init_irq(const struct device * dev)1124 static int bosch_bmi323_init_irq(const struct device *dev)
1125 {
1126 	struct bosch_bmi323_data *data = (struct bosch_bmi323_data *)dev->data;
1127 	struct bosch_bmi323_config *config = (struct bosch_bmi323_config *)dev->config;
1128 	int ret;
1129 
1130 	ret = gpio_pin_configure_dt(&config->int_gpio, GPIO_INPUT);
1131 
1132 	if (ret < 0) {
1133 		return ret;
1134 	}
1135 
1136 	gpio_init_callback(&data->gpio_callback, config->int_gpio_callback,
1137 			   BIT(config->int_gpio.pin));
1138 
1139 	ret = gpio_add_callback(config->int_gpio.port, &data->gpio_callback);
1140 
1141 	if (ret < 0) {
1142 		return ret;
1143 	}
1144 
1145 	return gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_DISABLE);
1146 }
1147 
bosch_bmi323_init_int1(const struct device * dev)1148 static int bosch_bmi323_init_int1(const struct device *dev)
1149 {
1150 	uint16_t buf;
1151 
1152 	buf = IMU_BOSCH_BMI323_REG_VALUE(IO_INT_CTRL, INT1_LVL, ACT_HIGH) |
1153 	      IMU_BOSCH_BMI323_REG_VALUE(IO_INT_CTRL, INT1_OD, PUSH_PULL) |
1154 	      IMU_BOSCH_BMI323_REG_VALUE(IO_INT_CTRL, INT1_OUTPUT_EN, EN);
1155 
1156 	return bosch_bmi323_bus_write_words(dev, IMU_BOSCH_BMI323_REG_IO_INT_CTRL, &buf, 1);
1157 }
1158 
bosch_bmi323_irq_callback_handler(struct k_work * item)1159 static void bosch_bmi323_irq_callback_handler(struct k_work *item)
1160 {
1161 	struct bosch_bmi323_data *data =
1162 		CONTAINER_OF(item, struct bosch_bmi323_data, callback_work);
1163 
1164 	k_mutex_lock(&data->lock, K_FOREVER);
1165 
1166 	if (data->trigger_handler != NULL) {
1167 		data->trigger_handler(data->dev, data->trigger);
1168 	}
1169 
1170 	k_mutex_unlock(&data->lock);
1171 }
1172 
bosch_bmi323_pm_resume(const struct device * dev)1173 static int bosch_bmi323_pm_resume(const struct device *dev)
1174 {
1175 	const struct bosch_bmi323_config *config = (const struct bosch_bmi323_config *)dev->config;
1176 	int ret;
1177 
1178 	ret = bosch_bmi323_bus_init(dev);
1179 
1180 	if (ret < 0) {
1181 		LOG_WRN("Failed to init bus");
1182 
1183 		return ret;
1184 	}
1185 
1186 	ret = bosch_bmi323_validate_chip_id(dev);
1187 
1188 	if (ret < 0) {
1189 		LOG_WRN("Failed to validate chip id");
1190 
1191 		return ret;
1192 	}
1193 
1194 	ret = bosch_bmi323_soft_reset(dev);
1195 
1196 	if (ret < 0) {
1197 		LOG_WRN("Failed to soft reset chip");
1198 
1199 		return ret;
1200 	}
1201 
1202 	ret = bosch_bmi323_bus_init(dev);
1203 
1204 	if (ret < 0) {
1205 		LOG_WRN("Failed to re-init bus");
1206 
1207 		return ret;
1208 	}
1209 
1210 	ret = bosch_bmi323_enable_feature_engine(dev);
1211 
1212 	if (ret < 0) {
1213 		LOG_WRN("Failed to enable feature engine");
1214 
1215 		return ret;
1216 	}
1217 
1218 	ret = bosch_bmi323_init_int1(dev);
1219 
1220 	if (ret < 0) {
1221 		LOG_WRN("Failed to enable INT1");
1222 		return ret;
1223 	}
1224 
1225 	ret = gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_EDGE_TO_ACTIVE);
1226 	if (ret < 0) {
1227 		LOG_WRN("Failed to configure int");
1228 	}
1229 
1230 	return ret;
1231 }
1232 
1233 #ifdef CONFIG_PM_DEVICE
bosch_bmi323_pm_suspend(const struct device * dev)1234 static int bosch_bmi323_pm_suspend(const struct device *dev)
1235 {
1236 	const struct bosch_bmi323_config *config = (const struct bosch_bmi323_config *)dev->config;
1237 	int ret;
1238 
1239 	ret = gpio_pin_interrupt_configure_dt(&config->int_gpio, GPIO_INT_DISABLE);
1240 	if (ret < 0) {
1241 		LOG_WRN("Failed to disable int");
1242 	}
1243 
1244 	/* Soft reset device to put it into suspend */
1245 	return bosch_bmi323_soft_reset(dev);
1246 }
1247 #endif /* CONFIG_PM_DEVICE */
1248 
1249 #ifdef CONFIG_PM_DEVICE
bosch_bmi323_pm_action(const struct device * dev,enum pm_device_action action)1250 static int bosch_bmi323_pm_action(const struct device *dev, enum pm_device_action action)
1251 {
1252 	struct bosch_bmi323_data *data = (struct bosch_bmi323_data *)dev->data;
1253 	int ret;
1254 
1255 	k_mutex_lock(&data->lock, K_FOREVER);
1256 
1257 	switch (action) {
1258 	case PM_DEVICE_ACTION_RESUME:
1259 		ret = bosch_bmi323_pm_resume(dev);
1260 
1261 		break;
1262 
1263 	case PM_DEVICE_ACTION_SUSPEND:
1264 		ret = bosch_bmi323_pm_suspend(dev);
1265 
1266 		break;
1267 
1268 	default:
1269 		ret = -ENOTSUP;
1270 
1271 		break;
1272 	}
1273 
1274 	k_mutex_unlock(&data->lock);
1275 
1276 	return ret;
1277 }
1278 #endif /* CONFIG_PM_DEVICE */
1279 
bosch_bmi323_init(const struct device * dev)1280 static int bosch_bmi323_init(const struct device *dev)
1281 {
1282 	struct bosch_bmi323_data *data = (struct bosch_bmi323_data *)dev->data;
1283 	int ret;
1284 
1285 	k_mutex_init(&data->lock);
1286 
1287 	k_work_init(&data->callback_work, bosch_bmi323_irq_callback_handler);
1288 
1289 	data->dev = dev;
1290 
1291 	ret = bosch_bmi323_init_irq(dev);
1292 
1293 	if (ret < 0) {
1294 		LOG_WRN("Failed to init irq");
1295 
1296 		return ret;
1297 	}
1298 
1299 #ifndef CONFIG_PM_DEVICE_RUNTIME
1300 	ret = bosch_bmi323_pm_resume(dev);
1301 
1302 	if (ret < 0) {
1303 		LOG_WRN("Failed to initialize device");
1304 	}
1305 #else
1306 	pm_device_init_suspended(dev);
1307 
1308 	ret = pm_device_runtime_enable(dev);
1309 
1310 	if (ret < 0) {
1311 		LOG_WRN("Failed to enable device pm runtime");
1312 	}
1313 #endif /* CONFIG_PM_DEVICE_RUNTIME */
1314 
1315 	return ret;
1316 }
1317 
1318 /*
1319  * Currently only support for the SPI bus is implemented. This shall be updated to
1320  * select the appropriate bus once I2C is implemented.
1321  */
1322 #define BMI323_DEVICE_BUS(inst)                                                                    \
1323 	BUILD_ASSERT(DT_INST_ON_BUS(inst, spi), "Unimplemented bus");                              \
1324 	BMI323_DEVICE_SPI_BUS(inst)
1325 
1326 #define BMI323_DEVICE(inst)                                                                        \
1327 	static struct bosch_bmi323_data bosch_bmi323_data_##inst;                                  \
1328                                                                                                    \
1329 	BMI323_DEVICE_BUS(inst);                                                                   \
1330                                                                                                    \
1331 	static void bosch_bmi323_irq_callback##inst(const struct device *dev,                      \
1332 						    struct gpio_callback *cb, uint32_t pins)       \
1333 	{                                                                                          \
1334 		bosch_bmi323_irq_callback(DEVICE_DT_INST_GET(inst));                               \
1335 	}                                                                                          \
1336                                                                                                    \
1337 	static const struct bosch_bmi323_config bosch_bmi323_config_##inst = {                     \
1338 		.bus = &bosch_bmi323_bus_api##inst,                                                \
1339 		.int_gpio = GPIO_DT_SPEC_INST_GET(inst, int_gpios),                                \
1340 		.int_gpio_callback = bosch_bmi323_irq_callback##inst,                              \
1341 	};                                                                                         \
1342                                                                                                    \
1343 	PM_DEVICE_DT_INST_DEFINE(inst, bosch_bmi323_pm_action);                                    \
1344                                                                                                    \
1345 	SENSOR_DEVICE_DT_INST_DEFINE(inst, bosch_bmi323_init, PM_DEVICE_DT_INST_GET(inst),         \
1346 				     &bosch_bmi323_data_##inst, &bosch_bmi323_config_##inst,       \
1347 				     POST_KERNEL, 99, &bosch_bmi323_api);
1348 
1349 DT_INST_FOREACH_STATUS_OKAY(BMI323_DEVICE)
1350