1 /* Bosch BMI160 inertial measurement unit driver
2  *
3  * Copyright (c) 2016 Intel Corporation
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  *
7  * Datasheet:
8  * http://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMI160-DS000-07.pdf
9  */
10 
11 #include <zephyr/init.h>
12 #include <zephyr/drivers/i2c.h>
13 #include <zephyr/drivers/sensor.h>
14 #include <zephyr/sys/byteorder.h>
15 #include <zephyr/kernel.h>
16 #include <zephyr/sys/__assert.h>
17 #include <zephyr/pm/pm.h>
18 #include <zephyr/pm/device.h>
19 #include <zephyr/pm/device_runtime.h>
20 #include <zephyr/logging/log.h>
21 
22 #include "bmi160.h"
23 
24 LOG_MODULE_REGISTER(BMI160, CONFIG_SENSOR_LOG_LEVEL);
25 
26 #if DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 0
27 #warning "BMI160 driver enabled without any devices"
28 #endif
29 
30 #if BMI160_BUS_SPI
bmi160_transceive(const struct device * dev,uint8_t reg,bool write,void * buf,size_t length)31 static int bmi160_transceive(const struct device *dev, uint8_t reg,
32 			     bool write, void *buf, size_t length)
33 {
34 	const struct bmi160_cfg *cfg = dev->config;
35 	const struct spi_buf tx_buf[2] = {
36 		{
37 			.buf = &reg,
38 			.len = 1
39 		},
40 		{
41 			.buf = buf,
42 			.len = length
43 		}
44 	};
45 	const struct spi_buf_set tx = {
46 		.buffers = tx_buf,
47 		.count = buf ? 2 : 1
48 	};
49 
50 	if (!write) {
51 		const struct spi_buf_set rx = {
52 			.buffers = tx_buf,
53 			.count = 2
54 		};
55 
56 		return spi_transceive_dt(&cfg->bus.spi, &tx, &rx);
57 	}
58 
59 	return spi_write_dt(&cfg->bus.spi, &tx);
60 }
61 
bmi160_bus_ready_spi(const struct device * dev)62 bool bmi160_bus_ready_spi(const struct device *dev)
63 {
64 	const struct bmi160_cfg *cfg = dev->config;
65 
66 	return spi_is_ready_dt(&cfg->bus.spi);
67 }
68 
bmi160_read_spi(const struct device * dev,uint8_t reg_addr,void * buf,uint8_t len)69 int bmi160_read_spi(const struct device *dev,
70 		    uint8_t reg_addr, void *buf, uint8_t len)
71 {
72 	return bmi160_transceive(dev, reg_addr | BMI160_REG_READ, false,
73 				 buf, len);
74 }
75 
bmi160_write_spi(const struct device * dev,uint8_t reg_addr,void * buf,uint8_t len)76 int bmi160_write_spi(const struct device *dev,
77 		     uint8_t reg_addr, void *buf, uint8_t len)
78 {
79 	return bmi160_transceive(dev, reg_addr & BMI160_REG_MASK, true,
80 				 buf, len);
81 }
82 
83 static const struct bmi160_bus_io bmi160_bus_io_spi = {
84 	.ready = bmi160_bus_ready_spi,
85 	.read = bmi160_read_spi,
86 	.write = bmi160_write_spi,
87 };
88 #endif /* BMI160_BUS_SPI */
89 
90 #if BMI160_BUS_I2C
91 
bmi160_bus_ready_i2c(const struct device * dev)92 bool bmi160_bus_ready_i2c(const struct device *dev)
93 {
94 	const struct bmi160_cfg *cfg = dev->config;
95 
96 	return device_is_ready(cfg->bus.i2c.bus);
97 }
98 
bmi160_read_i2c(const struct device * dev,uint8_t reg_addr,void * buf,uint8_t len)99 int bmi160_read_i2c(const struct device *dev,
100 		    uint8_t reg_addr, void *buf, uint8_t len)
101 {
102 	const struct bmi160_cfg *cfg = dev->config;
103 
104 	return i2c_burst_read_dt(&cfg->bus.i2c, reg_addr, buf, len);
105 }
106 
bmi160_write_i2c(const struct device * dev,uint8_t reg_addr,void * buf,uint8_t len)107 int bmi160_write_i2c(const struct device *dev,
108 		     uint8_t reg_addr, void *buf, uint8_t len)
109 {
110 	const struct bmi160_cfg *cfg = dev->config;
111 
112 	return i2c_burst_write_dt(&cfg->bus.i2c, reg_addr, buf, len);
113 }
114 
115 static const struct bmi160_bus_io bmi160_bus_io_i2c = {
116 	.ready = bmi160_bus_ready_i2c,
117 	.read = bmi160_read_i2c,
118 	.write = bmi160_write_i2c,
119 };
120 #endif
121 
bmi160_read(const struct device * dev,uint8_t reg_addr,void * buf,uint8_t len)122 int bmi160_read(const struct device *dev, uint8_t reg_addr, void *buf,
123 		uint8_t len)
124 {
125 	const struct bmi160_cfg *cfg = dev->config;
126 
127 	return cfg->bus_io->read(dev, reg_addr, buf, len);
128 }
129 
bmi160_byte_read(const struct device * dev,uint8_t reg_addr,uint8_t * byte)130 int bmi160_byte_read(const struct device *dev, uint8_t reg_addr, uint8_t *byte)
131 {
132 	return bmi160_read(dev, reg_addr, byte, 1);
133 }
134 
bmi160_word_read(const struct device * dev,uint8_t reg_addr,uint16_t * word)135 static int bmi160_word_read(const struct device *dev, uint8_t reg_addr,
136 			    uint16_t *word)
137 {
138 	int rc;
139 
140 	rc = bmi160_read(dev, reg_addr, word, 2);
141 	if (rc != 0) {
142 		return rc;
143 	}
144 
145 	*word = sys_le16_to_cpu(*word);
146 
147 	return 0;
148 }
149 
bmi160_write(const struct device * dev,uint8_t reg_addr,void * buf,uint8_t len)150 int bmi160_write(const struct device *dev, uint8_t reg_addr, void *buf,
151 		 uint8_t len)
152 {
153 	const struct bmi160_cfg *cfg = dev->config;
154 
155 	return cfg->bus_io->write(dev, reg_addr, buf, len);
156 }
157 
bmi160_byte_write(const struct device * dev,uint8_t reg_addr,uint8_t byte)158 int bmi160_byte_write(const struct device *dev, uint8_t reg_addr,
159 		      uint8_t byte)
160 {
161 	return bmi160_write(dev, reg_addr & BMI160_REG_MASK, &byte, 1);
162 }
163 
bmi160_word_write(const struct device * dev,uint8_t reg_addr,uint16_t word)164 int bmi160_word_write(const struct device *dev, uint8_t reg_addr,
165 		      uint16_t word)
166 {
167 	uint8_t tx_word[2] = {
168 		(uint8_t)(word & 0xff),
169 		(uint8_t)(word >> 8)
170 	};
171 
172 	return bmi160_write(dev, reg_addr & BMI160_REG_MASK, tx_word, 2);
173 }
174 
bmi160_reg_field_update(const struct device * dev,uint8_t reg_addr,uint8_t pos,uint8_t mask,uint8_t val)175 int bmi160_reg_field_update(const struct device *dev, uint8_t reg_addr,
176 			    uint8_t pos, uint8_t mask, uint8_t val)
177 {
178 	uint8_t old_val;
179 
180 	if (bmi160_byte_read(dev, reg_addr, &old_val) < 0) {
181 		return -EIO;
182 	}
183 
184 	return bmi160_byte_write(dev, reg_addr,
185 				 (old_val & ~mask) | ((val << pos) & mask));
186 }
187 
bmi160_pmu_set(const struct device * dev,union bmi160_pmu_status * pmu_sts)188 static int bmi160_pmu_set(const struct device *dev,
189 			  union bmi160_pmu_status *pmu_sts)
190 {
191 	struct {
192 		uint8_t cmd;
193 		uint16_t delay_us; /* values taken from page 82 */
194 	} cmds[] = {
195 		{BMI160_CMD_PMU_MAG | pmu_sts->mag, 350},
196 		{BMI160_CMD_PMU_ACC | pmu_sts->acc, 3200},
197 		{BMI160_CMD_PMU_GYR | pmu_sts->gyr, 55000}
198 	};
199 	size_t i;
200 
201 	for (i = 0; i < ARRAY_SIZE(cmds); i++) {
202 		union bmi160_pmu_status sts;
203 		bool pmu_set = false;
204 
205 		if (bmi160_byte_write(dev, BMI160_REG_CMD, cmds[i].cmd) < 0) {
206 			return -EIO;
207 		}
208 
209 		/*
210 		 * Cannot use a timer here since this is called from the
211 		 * init function and the timeouts were not initialized yet.
212 		 */
213 		k_busy_wait(cmds[i].delay_us);
214 
215 		/* make sure the PMU_STATUS was set, though */
216 		do {
217 			if (bmi160_byte_read(dev, BMI160_REG_PMU_STATUS,
218 					     &sts.raw) < 0) {
219 				return -EIO;
220 			}
221 
222 			if (i == 0) {
223 				pmu_set = (pmu_sts->mag == sts.mag);
224 			} else if (i == 1) {
225 				pmu_set = (pmu_sts->acc == sts.acc);
226 			} else {
227 				pmu_set = (pmu_sts->gyr == sts.gyr);
228 			}
229 
230 		} while (!pmu_set);
231 	}
232 
233 	/* set the undersampling flag for accelerometer */
234 	return bmi160_reg_field_update(dev, BMI160_REG_ACC_CONF,
235 				       BMI160_ACC_CONF_US_POS, BMI160_ACC_CONF_US_MASK,
236 				       pmu_sts->acc != BMI160_PMU_NORMAL);
237 }
238 
239 #if defined(CONFIG_BMI160_GYRO_ODR_RUNTIME) ||\
240 	defined(CONFIG_BMI160_ACCEL_ODR_RUNTIME)
241 /*
242  * Output data rate map with allowed frequencies:
243  * freq = freq_int + freq_milli / 1000
244  *
245  * Since we don't need a finer frequency resolution than milliHz, use uint16_t
246  * to save some flash.
247  */
248 struct {
249 	uint16_t freq_int;
250 	uint16_t freq_milli; /* User should convert to uHz before setting the
251 			      * SENSOR_ATTR_SAMPLING_FREQUENCY attribute.
252 			      */
253 } bmi160_odr_map[] = {
254 	{0,    0  }, {0,     781}, {1,     562}, {3,    125}, {6,   250},
255 	{12,   500}, {25,    0  }, {50,    0  }, {100,  0  }, {200, 0  },
256 	{400,  0  }, {800,   0  }, {1600,  0  }, {3200, 0  },
257 };
258 
bmi160_freq_to_odr_val(uint16_t freq_int,uint16_t freq_milli)259 static int bmi160_freq_to_odr_val(uint16_t freq_int, uint16_t freq_milli)
260 {
261 	size_t i;
262 
263 	/* An ODR of 0 Hz is not allowed */
264 	if (freq_int == 0U && freq_milli == 0U) {
265 		return -EINVAL;
266 	}
267 
268 	for (i = 0; i < ARRAY_SIZE(bmi160_odr_map); i++) {
269 		if (freq_int < bmi160_odr_map[i].freq_int ||
270 		    (freq_int == bmi160_odr_map[i].freq_int &&
271 		     freq_milli <= bmi160_odr_map[i].freq_milli)) {
272 			return i;
273 		}
274 	}
275 
276 	return -EINVAL;
277 }
278 #endif
279 
280 #if defined(CONFIG_BMI160_ACCEL_ODR_RUNTIME)
bmi160_acc_odr_set(const struct device * dev,uint16_t freq_int,uint16_t freq_milli)281 static int bmi160_acc_odr_set(const struct device *dev, uint16_t freq_int,
282 			      uint16_t freq_milli)
283 {
284 	int odr = bmi160_freq_to_odr_val(freq_int, freq_milli);
285 
286 	if (odr < 0) {
287 		return odr;
288 	}
289 
290 	return bmi160_reg_field_update(dev, BMI160_REG_ACC_CONF,
291 				       BMI160_ACC_CONF_ODR_POS,
292 				       BMI160_ACC_CONF_ODR_MASK,
293 				       (uint8_t) odr);
294 }
295 #endif
296 
297 static const struct bmi160_range bmi160_acc_range_map[] = {
298 	{2,	BMI160_ACC_RANGE_2G},
299 	{4,	BMI160_ACC_RANGE_4G},
300 	{8,	BMI160_ACC_RANGE_8G},
301 	{16,	BMI160_ACC_RANGE_16G},
302 };
303 #define BMI160_ACC_RANGE_MAP_SIZE	ARRAY_SIZE(bmi160_acc_range_map)
304 
305 static const struct bmi160_range bmi160_gyr_range_map[] = {
306 	{125,	BMI160_GYR_RANGE_125DPS},
307 	{250,	BMI160_GYR_RANGE_250DPS},
308 	{500,	BMI160_GYR_RANGE_500DPS},
309 	{1000,	BMI160_GYR_RANGE_1000DPS},
310 	{2000,	BMI160_GYR_RANGE_2000DPS},
311 };
312 #define BMI160_GYR_RANGE_MAP_SIZE	ARRAY_SIZE(bmi160_gyr_range_map)
313 
314 #if defined(CONFIG_BMI160_ACCEL_RANGE_RUNTIME) ||\
315 	defined(CONFIG_BMI160_GYRO_RANGE_RUNTIME)
bmi160_range_to_reg_val(uint16_t range,const struct bmi160_range * range_map,uint16_t range_map_size)316 static int32_t bmi160_range_to_reg_val(uint16_t range,
317 				       const struct bmi160_range *range_map,
318 				       uint16_t range_map_size)
319 {
320 	int i;
321 
322 	for (i = 0; i < range_map_size; i++) {
323 		if (range <= range_map[i].range) {
324 			return range_map[i].reg_val;
325 		}
326 	}
327 
328 	return -EINVAL;
329 }
330 #endif
331 
bmi160_reg_val_to_range(uint8_t reg_val,const struct bmi160_range * range_map,uint16_t range_map_size)332 static int32_t bmi160_reg_val_to_range(uint8_t reg_val,
333 				       const struct bmi160_range *range_map,
334 				       uint16_t range_map_size)
335 {
336 	int i;
337 
338 	for (i = 0; i < range_map_size; i++) {
339 		if (reg_val == range_map[i].reg_val) {
340 			return range_map[i].range;
341 		}
342 	}
343 
344 	return -EINVAL;
345 }
346 
bmi160_acc_reg_val_to_range(uint8_t reg_val)347 int32_t bmi160_acc_reg_val_to_range(uint8_t reg_val)
348 {
349 	return bmi160_reg_val_to_range(reg_val, bmi160_acc_range_map,
350 				       BMI160_ACC_RANGE_MAP_SIZE);
351 }
352 
bmi160_gyr_reg_val_to_range(uint8_t reg_val)353 int32_t bmi160_gyr_reg_val_to_range(uint8_t reg_val)
354 {
355 	return bmi160_reg_val_to_range(reg_val, bmi160_gyr_range_map,
356 				       BMI160_GYR_RANGE_MAP_SIZE);
357 }
358 
bmi160_do_calibration(const struct device * dev,uint8_t foc_conf)359 static int bmi160_do_calibration(const struct device *dev, uint8_t foc_conf)
360 {
361 	if (bmi160_byte_write(dev, BMI160_REG_FOC_CONF, foc_conf) < 0) {
362 		return -EIO;
363 	}
364 
365 	if (bmi160_byte_write(dev, BMI160_REG_CMD, BMI160_CMD_START_FOC) < 0) {
366 		return -EIO;
367 	}
368 
369 	k_busy_wait(250000); /* calibration takes a maximum of 250ms */
370 
371 	return 0;
372 }
373 
374 #if defined(CONFIG_BMI160_ACCEL_RANGE_RUNTIME)
bmi160_acc_range_set(const struct device * dev,const struct sensor_value * val)375 static int bmi160_acc_range_set(const struct device *dev, const struct sensor_value *val)
376 {
377 	int32_t range_g = sensor_ms2_to_g(val);
378 	struct bmi160_data *data = dev->data;
379 	int32_t reg_val = bmi160_range_to_reg_val(range_g,
380 						  bmi160_acc_range_map,
381 						  BMI160_ACC_RANGE_MAP_SIZE);
382 
383 	if (reg_val < 0) {
384 		return reg_val;
385 	}
386 
387 	switch (reg_val & 0xff) {
388 	case BMI160_ACC_RANGE_2G:
389 		range_g = 2;
390 		break;
391 	case BMI160_ACC_RANGE_4G:
392 		range_g = 4;
393 		break;
394 	case BMI160_ACC_RANGE_8G:
395 		range_g = 8;
396 		break;
397 	case BMI160_ACC_RANGE_16G:
398 		range_g = 16;
399 		break;
400 	}
401 
402 	if (bmi160_byte_write(dev, BMI160_REG_ACC_RANGE, reg_val & 0xff) < 0) {
403 		return -EIO;
404 	}
405 
406 	data->scale.acc_numerator = BMI160_ACC_SCALE_NUMERATOR(range_g);
407 	return 0;
408 }
409 #endif
410 
411 #if !defined(CONFIG_BMI160_ACCEL_PMU_SUSPEND)
412 /*
413  * Accelerometer offset scale, taken from pg. 79, converted to micro m/s^2:
414  *	3.9 * 9.80665 * 1000
415  */
416 #define BMI160_ACC_OFS_LSB		38246
bmi160_acc_ofs_set(const struct device * dev,enum sensor_channel chan,const struct sensor_value * ofs)417 static int bmi160_acc_ofs_set(const struct device *dev,
418 			      enum sensor_channel chan,
419 			      const struct sensor_value *ofs)
420 {
421 	uint8_t reg_addr[] = {
422 		BMI160_REG_OFFSET_ACC_X,
423 		BMI160_REG_OFFSET_ACC_Y,
424 		BMI160_REG_OFFSET_ACC_Z
425 	};
426 	int i;
427 	int32_t reg_val;
428 
429 	/* we need the offsets for all axis */
430 	if (chan != SENSOR_CHAN_ACCEL_XYZ) {
431 		return -ENOTSUP;
432 	}
433 
434 	for (i = 0; i < BMI160_AXES; i++, ofs++) {
435 		/* convert offset to micro m/s^2 */
436 		reg_val =
437 			CLAMP(sensor_value_to_micro(ofs) / BMI160_ACC_OFS_LSB, INT8_MIN, INT8_MAX);
438 
439 		if (bmi160_byte_write(dev, reg_addr[i], reg_val) < 0) {
440 			return -EIO;
441 		}
442 	}
443 
444 	/* activate accel HW compensation */
445 	return bmi160_reg_field_update(dev, BMI160_REG_OFFSET_EN,
446 				       BMI160_ACC_OFS_EN_POS,
447 				       BIT(BMI160_ACC_OFS_EN_POS), 1);
448 }
449 
bmi160_acc_calibrate(const struct device * dev,enum sensor_channel chan,const struct sensor_value * xyz_calib_value)450 static int  bmi160_acc_calibrate(const struct device *dev,
451 				 enum sensor_channel chan,
452 				 const struct sensor_value *xyz_calib_value)
453 {
454 	struct bmi160_data *data = dev->data;
455 	uint8_t foc_pos[] = {
456 		BMI160_FOC_ACC_X_POS,
457 		BMI160_FOC_ACC_Y_POS,
458 		BMI160_FOC_ACC_Z_POS,
459 	};
460 	int i;
461 	uint8_t reg_val = 0U;
462 
463 	/* Calibration has to be done in normal mode. */
464 	if (data->pmu_sts.acc != BMI160_PMU_NORMAL) {
465 		return -ENOTSUP;
466 	}
467 
468 	/*
469 	 * Hardware calibration is done knowing the expected values on all axis.
470 	 */
471 	if (chan != SENSOR_CHAN_ACCEL_XYZ) {
472 		return -ENOTSUP;
473 	}
474 
475 	for (i = 0; i < BMI160_AXES; i++, xyz_calib_value++) {
476 		int32_t accel_g;
477 		uint8_t accel_val;
478 
479 		accel_g = sensor_ms2_to_g(xyz_calib_value);
480 		if (accel_g == 0) {
481 			accel_val = 3U;
482 		} else if (accel_g == 1) {
483 			accel_val = 1U;
484 		} else if (accel_g == -1) {
485 			accel_val = 2U;
486 		} else {
487 			accel_val = 0U;
488 		}
489 		reg_val |= (accel_val << foc_pos[i]);
490 	}
491 
492 	if (bmi160_do_calibration(dev, reg_val) < 0) {
493 		return -EIO;
494 	}
495 
496 	/* activate accel HW compensation */
497 	return bmi160_reg_field_update(dev, BMI160_REG_OFFSET_EN,
498 				       BMI160_ACC_OFS_EN_POS,
499 				       BIT(BMI160_ACC_OFS_EN_POS), 1);
500 }
501 
bmi160_acc_config(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)502 static int bmi160_acc_config(const struct device *dev,
503 			     enum sensor_channel chan,
504 			     enum sensor_attribute attr,
505 			     const struct sensor_value *val)
506 {
507 	switch (attr) {
508 #if defined(CONFIG_BMI160_ACCEL_RANGE_RUNTIME)
509 	case SENSOR_ATTR_FULL_SCALE:
510 		return bmi160_acc_range_set(dev, val);
511 #endif
512 #if defined(CONFIG_BMI160_ACCEL_ODR_RUNTIME)
513 	case SENSOR_ATTR_SAMPLING_FREQUENCY:
514 		return bmi160_acc_odr_set(dev, val->val1, val->val2 / 1000);
515 #endif
516 	case SENSOR_ATTR_OFFSET:
517 		return bmi160_acc_ofs_set(dev, chan, val);
518 	case SENSOR_ATTR_CALIB_TARGET:
519 		return bmi160_acc_calibrate(dev, chan, val);
520 #if defined(CONFIG_BMI160_TRIGGER)
521 	case SENSOR_ATTR_SLOPE_TH:
522 	case SENSOR_ATTR_SLOPE_DUR:
523 		return bmi160_acc_slope_config(dev, attr, val);
524 #endif
525 	default:
526 		LOG_DBG("Accel attribute not supported.");
527 		return -ENOTSUP;
528 	}
529 
530 	return 0;
531 }
532 #endif /* !defined(CONFIG_BMI160_ACCEL_PMU_SUSPEND) */
533 
534 #if defined(CONFIG_BMI160_GYRO_ODR_RUNTIME)
bmi160_gyr_odr_set(const struct device * dev,uint16_t freq_int,uint16_t freq_milli)535 static int bmi160_gyr_odr_set(const struct device *dev, uint16_t freq_int,
536 			      uint16_t freq_milli)
537 {
538 	int odr = bmi160_freq_to_odr_val(freq_int, freq_milli);
539 
540 	if (odr < 0) {
541 		return odr;
542 	}
543 
544 	if (odr < BMI160_ODR_25 || odr > BMI160_ODR_3200) {
545 		return -ENOTSUP;
546 	}
547 
548 	return bmi160_reg_field_update(dev, BMI160_REG_GYR_CONF,
549 				       BMI160_GYR_CONF_ODR_POS,
550 				       BMI160_GYR_CONF_ODR_MASK,
551 				       (uint8_t) odr);
552 }
553 #endif
554 
555 #if defined(CONFIG_BMI160_GYRO_RANGE_RUNTIME)
bmi160_gyr_range_set(const struct device * dev,const struct sensor_value * val)556 static int bmi160_gyr_range_set(const struct device *dev, const struct sensor_value *val)
557 {
558 	uint16_t range = sensor_rad_to_degrees(val);
559 	struct bmi160_data *data = dev->data;
560 	int32_t reg_val = bmi160_range_to_reg_val(range,
561 						bmi160_gyr_range_map,
562 						BMI160_GYR_RANGE_MAP_SIZE);
563 
564 	if (reg_val < 0) {
565 		return reg_val;
566 	}
567 	switch (reg_val) {
568 	case BMI160_GYR_RANGE_125DPS:
569 		range = 125;
570 		break;
571 	case BMI160_GYR_RANGE_250DPS:
572 		range = 250;
573 		break;
574 	case BMI160_GYR_RANGE_500DPS:
575 		range = 500;
576 		break;
577 	case BMI160_GYR_RANGE_1000DPS:
578 		range = 1000;
579 		break;
580 	case BMI160_GYR_RANGE_2000DPS:
581 		range = 2000;
582 		break;
583 	}
584 
585 	if (bmi160_byte_write(dev, BMI160_REG_GYR_RANGE, reg_val) < 0) {
586 		return -EIO;
587 	}
588 
589 	data->scale.gyr_numerator = BMI160_GYR_SCALE_NUMERATOR(range);
590 
591 	return 0;
592 }
593 #endif
594 
595 #if !defined(CONFIG_BMI160_GYRO_PMU_SUSPEND)
596 /*
597  * Gyro offset scale, taken from pg. 79, converted to micro rad/s:
598  *		0.061 * (pi / 180) * 1000000, where pi = 3.141592
599  */
600 #define BMI160_GYR_OFS_LSB		1065
bmi160_gyr_ofs_set(const struct device * dev,enum sensor_channel chan,const struct sensor_value * ofs)601 static int bmi160_gyr_ofs_set(const struct device *dev,
602 			      enum sensor_channel chan,
603 			      const struct sensor_value *ofs)
604 {
605 	struct {
606 		uint8_t lsb_addr;
607 		uint8_t msb_pos;
608 	} ofs_desc[] = {
609 		{BMI160_REG_OFFSET_GYR_X, BMI160_GYR_MSB_OFS_X_POS},
610 		{BMI160_REG_OFFSET_GYR_Y, BMI160_GYR_MSB_OFS_Y_POS},
611 		{BMI160_REG_OFFSET_GYR_Z, BMI160_GYR_MSB_OFS_Z_POS},
612 	};
613 	int i;
614 	int32_t ofs_u;
615 	int16_t val;
616 
617 	/* we need the offsets for all axis */
618 	if (chan != SENSOR_CHAN_GYRO_XYZ) {
619 		return -ENOTSUP;
620 	}
621 
622 	for (i = 0; i < BMI160_AXES; i++, ofs++) {
623 		/* convert offset to micro rad/s */
624 		ofs_u = ofs->val1 * 1000000ULL + ofs->val2;
625 
626 		val = CLAMP(ofs_u / BMI160_GYR_OFS_LSB, -512, 511);
627 
628 		/* write the LSB */
629 		if (bmi160_byte_write(dev, ofs_desc[i].lsb_addr,
630 				      val & 0xff) < 0) {
631 			return -EIO;
632 		}
633 
634 		/* write the MSB */
635 		if (bmi160_reg_field_update(dev, BMI160_REG_OFFSET_EN,
636 					    ofs_desc[i].msb_pos,
637 					    0x3 << ofs_desc[i].msb_pos,
638 					    (val >> 8) & 0x3) < 0) {
639 			return -EIO;
640 		}
641 	}
642 
643 	/* activate gyro HW compensation */
644 	return bmi160_reg_field_update(dev, BMI160_REG_OFFSET_EN,
645 				       BMI160_GYR_OFS_EN_POS,
646 				       BIT(BMI160_GYR_OFS_EN_POS), 1);
647 }
648 
bmi160_gyr_calibrate(const struct device * dev,enum sensor_channel chan)649 static int bmi160_gyr_calibrate(const struct device *dev,
650 				enum sensor_channel chan)
651 {
652 	struct bmi160_data *data = dev->data;
653 
654 	ARG_UNUSED(chan);
655 
656 	/* Calibration has to be done in normal mode. */
657 	if (data->pmu_sts.gyr != BMI160_PMU_NORMAL) {
658 		return -ENOTSUP;
659 	}
660 
661 	if (bmi160_do_calibration(dev, BIT(BMI160_FOC_GYR_EN_POS)) < 0) {
662 		return -EIO;
663 	}
664 
665 	/* activate gyro HW compensation */
666 	return bmi160_reg_field_update(dev, BMI160_REG_OFFSET_EN,
667 				       BMI160_GYR_OFS_EN_POS,
668 				       BIT(BMI160_GYR_OFS_EN_POS), 1);
669 }
670 
bmi160_gyr_config(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)671 static int bmi160_gyr_config(const struct device *dev,
672 			     enum sensor_channel chan,
673 			     enum sensor_attribute attr,
674 			     const struct sensor_value *val)
675 {
676 	switch (attr) {
677 #if defined(CONFIG_BMI160_GYRO_RANGE_RUNTIME)
678 	case SENSOR_ATTR_FULL_SCALE:
679 		return bmi160_gyr_range_set(dev, val);
680 #endif
681 #if defined(CONFIG_BMI160_GYRO_ODR_RUNTIME)
682 	case SENSOR_ATTR_SAMPLING_FREQUENCY:
683 		return bmi160_gyr_odr_set(dev, val->val1, val->val2 / 1000);
684 #endif
685 	case SENSOR_ATTR_OFFSET:
686 		return bmi160_gyr_ofs_set(dev, chan, val);
687 
688 	case SENSOR_ATTR_CALIB_TARGET:
689 		return bmi160_gyr_calibrate(dev, chan);
690 
691 	default:
692 		LOG_DBG("Gyro attribute not supported.");
693 		return -ENOTSUP;
694 	}
695 
696 	return 0;
697 }
698 #endif /* !defined(CONFIG_BMI160_GYRO_PMU_SUSPEND) */
699 
bmi160_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)700 static int bmi160_attr_set(const struct device *dev, enum sensor_channel chan,
701 			   enum sensor_attribute attr,
702 			   const struct sensor_value *val)
703 {
704 	switch (chan) {
705 #if !defined(CONFIG_BMI160_GYRO_PMU_SUSPEND)
706 	case SENSOR_CHAN_GYRO_X:
707 	case SENSOR_CHAN_GYRO_Y:
708 	case SENSOR_CHAN_GYRO_Z:
709 	case SENSOR_CHAN_GYRO_XYZ:
710 		return bmi160_gyr_config(dev, chan, attr, val);
711 #endif
712 #if !defined(CONFIG_BMI160_ACCEL_PMU_SUSPEND)
713 	case SENSOR_CHAN_ACCEL_X:
714 	case SENSOR_CHAN_ACCEL_Y:
715 	case SENSOR_CHAN_ACCEL_Z:
716 	case SENSOR_CHAN_ACCEL_XYZ:
717 		return bmi160_acc_config(dev, chan, attr, val);
718 #endif
719 	default:
720 		LOG_DBG("attr_set() not supported on this channel.");
721 		return -ENOTSUP;
722 	}
723 
724 	return 0;
725 }
726 
bmi160_attr_get(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,struct sensor_value * val)727 static int bmi160_attr_get(const struct device *dev, enum sensor_channel chan,
728 			   enum sensor_attribute attr, struct sensor_value *val)
729 {
730 	int rc;
731 
732 	if (attr == SENSOR_ATTR_OFFSET) {
733 		if (chan != SENSOR_CHAN_ACCEL_XYZ && chan != SENSOR_CHAN_GYRO_XYZ) {
734 			return -EINVAL;
735 		}
736 
737 		int8_t data[7];
738 
739 		rc = bmi160_read(dev, BMI160_REG_OFFSET_ACC_X, data, 7);
740 		if (rc != 0) {
741 			return rc;
742 		}
743 
744 		if ((chan == SENSOR_CHAN_ACCEL_XYZ &&
745 		     FIELD_GET(BIT(BMI160_ACC_OFS_EN_POS), data[6]) == 0) ||
746 		    (chan == SENSOR_CHAN_GYRO_XYZ &&
747 		     FIELD_GET(BIT(BMI160_GYR_OFS_EN_POS), data[6]) == 0)) {
748 			for (int i = 0; i < 3; ++i) {
749 				val[i].val1 = 0;
750 				val[i].val2 = 0;
751 			}
752 		} else {
753 			for (int i = 0; i < 3; ++i) {
754 				if (chan == SENSOR_CHAN_ACCEL_XYZ) {
755 					int32_t ug = data[i] * INT32_C(3900);
756 
757 					sensor_ug_to_ms2(ug, &val[i]);
758 				} else {
759 					int32_t udeg =
760 						(FIELD_GET(GENMASK((2 * i) + 1, 2 * i), data[6])
761 						 << 8) |
762 						data[3 + i];
763 
764 					udeg |= 0 - (udeg & 0x200);
765 					udeg *= 61000;
766 					sensor_10udegrees_to_rad(udeg / 10, &val[i]);
767 				}
768 			}
769 		}
770 		return 0;
771 	}
772 	if (attr == SENSOR_ATTR_SAMPLING_FREQUENCY) {
773 		if (chan == SENSOR_CHAN_ACCEL_XYZ) {
774 			int64_t rate_uhz;
775 			uint8_t acc_odr;
776 
777 			if (IS_ENABLED(CONFIG_BMI160_ACCEL_ODR_RUNTIME)) {
778 				/* Read the register */
779 				rc = bmi160_byte_read(dev, BMI160_REG_ACC_CONF, &acc_odr);
780 				if (rc != 0) {
781 					return rc;
782 				}
783 				acc_odr = FIELD_GET(BMI160_ACC_CONF_ODR_MASK, acc_odr);
784 			} else {
785 				acc_odr = BMI160_DEFAULT_ODR_ACC;
786 			}
787 
788 			rate_uhz = INT64_C(100000000) * BIT(acc_odr) / 256;
789 			val->val1 = rate_uhz / 1000000;
790 			val->val2 = rate_uhz - val->val1 * 1000000;
791 			return 0;
792 		} else if (chan == SENSOR_CHAN_GYRO_XYZ) {
793 			int64_t rate_uhz;
794 			uint8_t gyr_ord;
795 
796 			if (IS_ENABLED(CONFIG_BMI160_GYRO_ODR_RUNTIME)) {
797 				/* Read the register */
798 				rc = bmi160_byte_read(dev, BMI160_REG_GYR_CONF, &gyr_ord);
799 				if (rc != 0) {
800 					return rc;
801 				}
802 				gyr_ord = FIELD_GET(BMI160_GYR_CONF_ODR_MASK, gyr_ord);
803 			} else {
804 				gyr_ord = BMI160_DEFAULT_ODR_GYR;
805 			}
806 
807 			rate_uhz = INT64_C(100000000) * BIT(gyr_ord) / 256;
808 			val->val1 = rate_uhz / 1000000;
809 			val->val2 = rate_uhz - val->val1 * 1000000;
810 			return 0;
811 
812 		}
813 		return -EINVAL;
814 
815 	}
816 	if (attr == SENSOR_ATTR_FULL_SCALE) {
817 		if (chan == SENSOR_CHAN_ACCEL_XYZ) {
818 			uint8_t acc_range;
819 
820 			if (IS_ENABLED(CONFIG_BMI160_ACCEL_RANGE_RUNTIME)) {
821 				rc = bmi160_byte_read(dev, BMI160_REG_ACC_RANGE, &acc_range);
822 				if (rc != 0) {
823 					return rc;
824 				}
825 			} else {
826 				acc_range = BMI160_DEFAULT_RANGE_ACC;
827 			}
828 			sensor_g_to_ms2(bmi160_acc_reg_val_to_range(acc_range), val);
829 			return 0;
830 		} else if (chan == SENSOR_CHAN_GYRO_XYZ) {
831 			uint8_t gyr_range;
832 
833 			if (IS_ENABLED(CONFIG_BMI160_GYRO_RANGE_RUNTIME)) {
834 				rc = bmi160_byte_read(dev, BMI160_REG_GYR_RANGE, &gyr_range);
835 				if (rc != 0) {
836 					return rc;
837 				}
838 			} else {
839 				gyr_range = BMI160_DEFAULT_RANGE_GYR;
840 			}
841 			sensor_degrees_to_rad(bmi160_gyr_reg_val_to_range(gyr_range), val);
842 			return 0;
843 		}
844 		return -EINVAL;
845 	}
846 	return -EINVAL;
847 }
848 
bmi160_sample_fetch(const struct device * dev,enum sensor_channel chan)849 static int bmi160_sample_fetch(const struct device *dev,
850 			       enum sensor_channel chan)
851 {
852 	struct bmi160_data *data = dev->data;
853 	uint8_t status;
854 	size_t i;
855 	int ret = 0;
856 	enum pm_device_state pm_state;
857 
858 	(void)pm_device_state_get(dev, &pm_state);
859 	if (pm_state != PM_DEVICE_STATE_ACTIVE) {
860 		LOG_DBG("Device is suspended, fetch is unavailable");
861 		ret = -EIO;
862 		goto out;
863 	}
864 
865 	if (chan == SENSOR_CHAN_DIE_TEMP) {
866 		/* Die temperature is only valid when at least one measurement is active */
867 		if (data->pmu_sts.raw == 0U) {
868 			return -EINVAL;
869 		}
870 		return bmi160_word_read(dev, BMI160_REG_TEMPERATURE0, &data->sample.temperature);
871 	}
872 
873 	__ASSERT_NO_MSG(chan == SENSOR_CHAN_ALL);
874 
875 	status = 0;
876 	while ((status & BMI160_DATA_READY_BIT_MASK) == 0) {
877 
878 		if (bmi160_byte_read(dev, BMI160_REG_STATUS, &status) < 0) {
879 			ret = -EIO;
880 			goto out;
881 		}
882 	}
883 
884 	if (bmi160_read(dev, BMI160_SAMPLE_BURST_READ_ADDR, data->sample.raw,
885 			BMI160_BUF_SIZE) < 0) {
886 		ret = -EIO;
887 		goto out;
888 	}
889 
890 	/* convert samples to cpu endianness */
891 	for (i = 0; i < BMI160_SAMPLE_SIZE; i += 2) {
892 		uint16_t *sample =
893 			(uint16_t *) &data->sample.raw[i];
894 
895 		*sample = sys_le16_to_cpu(*sample);
896 	}
897 
898 out:
899 	return ret;
900 }
901 
bmi160_to_fixed_point(int16_t raw_val,int64_t scale_numerator,uint32_t scale_denominator,struct sensor_value * val)902 static void bmi160_to_fixed_point(int16_t raw_val, int64_t scale_numerator,
903 				  uint32_t scale_denominator, struct sensor_value *val)
904 {
905 	int64_t converted_val = (int64_t)raw_val * scale_numerator / scale_denominator;
906 
907 	val->val1 = converted_val / 1000000;
908 	val->val2 = converted_val % 1000000;
909 }
910 
bmi160_channel_convert(enum sensor_channel chan,int64_t scale_numerator,uint32_t scale_denominator,uint16_t * raw_xyz,struct sensor_value * val)911 static void bmi160_channel_convert(enum sensor_channel chan,
912 				   int64_t scale_numerator,
913 				   uint32_t scale_denominator,
914 				   uint16_t *raw_xyz,
915 				   struct sensor_value *val)
916 {
917 	int i;
918 	uint8_t ofs_start, ofs_stop;
919 
920 	switch (chan) {
921 	case SENSOR_CHAN_ACCEL_X:
922 	case SENSOR_CHAN_GYRO_X:
923 		ofs_start = ofs_stop = 0U;
924 		break;
925 	case SENSOR_CHAN_ACCEL_Y:
926 	case SENSOR_CHAN_GYRO_Y:
927 		ofs_start = ofs_stop = 1U;
928 		break;
929 	case SENSOR_CHAN_ACCEL_Z:
930 	case SENSOR_CHAN_GYRO_Z:
931 		ofs_start = ofs_stop = 2U;
932 		break;
933 	default:
934 		ofs_start = 0U; ofs_stop = 2U;
935 		break;
936 	}
937 
938 	for (i = ofs_start; i <= ofs_stop ; i++, val++) {
939 		bmi160_to_fixed_point(raw_xyz[i], scale_numerator, scale_denominator, val);
940 	}
941 }
942 
943 #if !defined(CONFIG_BMI160_GYRO_PMU_SUSPEND)
bmi160_gyr_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)944 static inline void bmi160_gyr_channel_get(const struct device *dev,
945 					  enum sensor_channel chan,
946 					  struct sensor_value *val)
947 {
948 	struct bmi160_data *data = dev->data;
949 
950 	bmi160_channel_convert(chan, data->scale.gyr_numerator, BMI160_GYR_SCALE_DENOMINATOR,
951 			       data->sample.gyr, val);
952 }
953 #endif
954 
955 #if !defined(CONFIG_BMI160_ACCEL_PMU_SUSPEND)
bmi160_acc_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)956 static inline void bmi160_acc_channel_get(const struct device *dev,
957 					  enum sensor_channel chan,
958 					  struct sensor_value *val)
959 {
960 	struct bmi160_data *data = dev->data;
961 
962 	bmi160_channel_convert(chan, data->scale.acc_numerator, BMI160_ACC_SCALE_DENOMINATOR,
963 			       data->sample.acc, val);
964 }
965 #endif
966 
bmi160_temp_channel_get(const struct device * dev,struct sensor_value * val)967 static int bmi160_temp_channel_get(const struct device *dev,
968 				   struct sensor_value *val)
969 {
970 	struct bmi160_data *data = dev->data;
971 
972 	/* the scale is 1/2^9/LSB = 1953 micro degrees */
973 	int32_t temp_micro = BMI160_TEMP_OFFSET * 1000000ULL + data->sample.temperature * 1953ULL;
974 
975 	val->val1 = temp_micro / 1000000ULL;
976 	val->val2 = temp_micro % 1000000ULL;
977 
978 	return 0;
979 }
980 
bmi160_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)981 static int bmi160_channel_get(const struct device *dev,
982 			      enum sensor_channel chan,
983 			      struct sensor_value *val)
984 {
985 	switch (chan) {
986 #if !defined(CONFIG_BMI160_GYRO_PMU_SUSPEND)
987 	case SENSOR_CHAN_GYRO_X:
988 	case SENSOR_CHAN_GYRO_Y:
989 	case SENSOR_CHAN_GYRO_Z:
990 	case SENSOR_CHAN_GYRO_XYZ:
991 		bmi160_gyr_channel_get(dev, chan, val);
992 		return 0;
993 #endif
994 #if !defined(CONFIG_BMI160_ACCEL_PMU_SUSPEND)
995 	case SENSOR_CHAN_ACCEL_X:
996 	case SENSOR_CHAN_ACCEL_Y:
997 	case SENSOR_CHAN_ACCEL_Z:
998 	case SENSOR_CHAN_ACCEL_XYZ:
999 		bmi160_acc_channel_get(dev, chan, val);
1000 		return 0;
1001 #endif
1002 	case SENSOR_CHAN_DIE_TEMP:
1003 		return bmi160_temp_channel_get(dev, val);
1004 	default:
1005 		LOG_DBG("Channel not supported.");
1006 		return -ENOTSUP;
1007 	}
1008 
1009 	return 0;
1010 }
1011 
1012 static const struct sensor_driver_api bmi160_api = {
1013 	.attr_set = bmi160_attr_set,
1014 	.attr_get = bmi160_attr_get,
1015 #ifdef CONFIG_BMI160_TRIGGER
1016 	.trigger_set = bmi160_trigger_set,
1017 #endif
1018 	.sample_fetch = bmi160_sample_fetch,
1019 	.channel_get = bmi160_channel_get,
1020 };
1021 
1022 
bmi160_resume(const struct device * dev)1023 static inline int bmi160_resume(const struct device *dev)
1024 {
1025 	struct bmi160_data *data = dev->data;
1026 
1027 	return bmi160_pmu_set(dev, &data->pmu_sts);
1028 }
1029 
bmi160_suspend(const struct device * dev)1030 static inline int bmi160_suspend(const struct device *dev)
1031 {
1032 	struct bmi160_data *data = dev->data;
1033 
1034 	/* Suspend everything */
1035 	union bmi160_pmu_status st = {
1036 		.acc = BMI160_PMU_SUSPEND,
1037 		.gyr = BMI160_PMU_SUSPEND,
1038 		.mag = BMI160_PMU_SUSPEND,
1039 	};
1040 
1041 	int ret = bmi160_pmu_set(dev, &st);
1042 
1043 	if (ret == 0) {
1044 		memset(data->sample.raw, 0, sizeof(data->sample.raw));
1045 	}
1046 	return ret;
1047 }
1048 
bmi160_init(const struct device * dev)1049 int bmi160_init(const struct device *dev)
1050 {
1051 	const struct bmi160_cfg *cfg = dev->config;
1052 	struct bmi160_data *data = dev->data;
1053 	uint8_t val = 0U;
1054 	int32_t acc_range, gyr_range;
1055 
1056 	if (!cfg->bus_io->ready(dev)) {
1057 		LOG_ERR("Bus not ready");
1058 		return -EINVAL;
1059 	}
1060 
1061 	/* reboot the chip */
1062 	if (bmi160_byte_write(dev, BMI160_REG_CMD, BMI160_CMD_SOFT_RESET) < 0) {
1063 		LOG_DBG("Cannot reboot chip.");
1064 		return -EIO;
1065 	}
1066 
1067 	k_busy_wait(1000);
1068 
1069 	/* do a dummy read from 0x7F to activate SPI */
1070 	if (bmi160_byte_read(dev, BMI160_SPI_START, &val) < 0) {
1071 		LOG_DBG("Cannot read from 0x7F..");
1072 		return -EIO;
1073 	}
1074 
1075 	k_busy_wait(150);
1076 
1077 	if (bmi160_byte_read(dev, BMI160_REG_CHIPID, &val) < 0) {
1078 		LOG_DBG("Failed to read chip id.");
1079 		return -EIO;
1080 	}
1081 
1082 	if (val != BMI160_CHIP_ID) {
1083 		LOG_DBG("Unsupported chip detected (0x%x)!", val);
1084 		return -ENODEV;
1085 	}
1086 
1087 	/* set default PMU for gyro, accelerometer */
1088 	data->pmu_sts.gyr = BMI160_DEFAULT_PMU_GYR;
1089 	data->pmu_sts.acc = BMI160_DEFAULT_PMU_ACC;
1090 
1091 	/* compass not supported, yet */
1092 	data->pmu_sts.mag = BMI160_PMU_SUSPEND;
1093 
1094 	/* Start in a suspended state (never turning on the mems sensors) if
1095 	 * PM_DEVICE_RUNTIME is enabled.
1096 	 */
1097 #ifdef CONFIG_PM_DEVICE_RUNTIME
1098 	pm_device_init_suspended(dev);
1099 
1100 	int ret = pm_device_runtime_enable(dev);
1101 
1102 	if (ret < 0 && ret != -ENOSYS) {
1103 		LOG_ERR("Failed to enabled runtime power management");
1104 		return -EIO;
1105 	}
1106 #else
1107 
1108 	/*
1109 	 * The next command will take around 100ms (contains some necessary busy
1110 	 * waits), but we cannot do it in a separate thread since we need to
1111 	 * guarantee the BMI is up and running, before the app's main() is
1112 	 * called.
1113 	 */
1114 	if (bmi160_pmu_set(dev, &data->pmu_sts) < 0) {
1115 		LOG_DBG("Failed to set power mode.");
1116 		return -EIO;
1117 	}
1118 #endif
1119 
1120 	/* set accelerometer default range */
1121 	if (bmi160_byte_write(dev, BMI160_REG_ACC_RANGE,
1122 				BMI160_DEFAULT_RANGE_ACC) < 0) {
1123 		LOG_DBG("Cannot set default range for accelerometer.");
1124 		return -EIO;
1125 	}
1126 
1127 	acc_range = bmi160_acc_reg_val_to_range(BMI160_DEFAULT_RANGE_ACC);
1128 
1129 	data->scale.acc_numerator = BMI160_ACC_SCALE_NUMERATOR(acc_range);
1130 
1131 	/* set gyro default range */
1132 	if (bmi160_byte_write(dev, BMI160_REG_GYR_RANGE,
1133 			      BMI160_DEFAULT_RANGE_GYR) < 0) {
1134 		LOG_DBG("Cannot set default range for gyroscope.");
1135 		return -EIO;
1136 	}
1137 
1138 	gyr_range = bmi160_gyr_reg_val_to_range(BMI160_DEFAULT_RANGE_GYR);
1139 
1140 	data->scale.gyr_numerator = BMI160_GYR_SCALE_NUMERATOR(gyr_range);
1141 
1142 	if (bmi160_reg_field_update(dev, BMI160_REG_ACC_CONF,
1143 				    BMI160_ACC_CONF_ODR_POS,
1144 				    BMI160_ACC_CONF_ODR_MASK,
1145 				    BMI160_DEFAULT_ODR_ACC) < 0) {
1146 		LOG_DBG("Failed to set accel's default ODR.");
1147 		return -EIO;
1148 	}
1149 
1150 	if (bmi160_reg_field_update(dev, BMI160_REG_GYR_CONF,
1151 				    BMI160_GYR_CONF_ODR_POS,
1152 				    BMI160_GYR_CONF_ODR_MASK,
1153 				    BMI160_DEFAULT_ODR_GYR) < 0) {
1154 		LOG_DBG("Failed to set gyro's default ODR.");
1155 		return -EIO;
1156 	}
1157 
1158 #ifdef CONFIG_BMI160_TRIGGER
1159 	if (bmi160_trigger_mode_init(dev) < 0) {
1160 		LOG_DBG("Cannot set up trigger mode.");
1161 		return -EINVAL;
1162 	}
1163 #endif
1164 
1165 	return 0;
1166 }
1167 
bmi160_pm(const struct device * dev,enum pm_device_action action)1168 int bmi160_pm(const struct device *dev, enum pm_device_action action)
1169 {
1170 	int ret = 0;
1171 
1172 	switch (action) {
1173 	case PM_DEVICE_ACTION_RESUME:
1174 		bmi160_resume(dev);
1175 		break;
1176 	case PM_DEVICE_ACTION_SUSPEND:
1177 		bmi160_suspend(dev);
1178 		break;
1179 	default:
1180 		ret = -ENOTSUP;
1181 	}
1182 
1183 	return ret;
1184 }
1185 
1186 #if defined(CONFIG_BMI160_TRIGGER)
1187 #define BMI160_TRIGGER_CFG(inst) \
1188 	.interrupt = GPIO_DT_SPEC_INST_GET(inst, int_gpios),
1189 #else
1190 #define BMI160_TRIGGER_CFG(inst)
1191 #endif
1192 
1193 #define BMI160_DEVICE_INIT(inst)								\
1194 	IF_ENABLED(CONFIG_PM_DEVICE_RUNTIME, (PM_DEVICE_DT_INST_DEFINE(inst, bmi160_pm)));	\
1195 	SENSOR_DEVICE_DT_INST_DEFINE(inst, bmi160_init,						\
1196 		COND_CODE_1(CONFIG_PM_DEVICE_RUNTIME, (PM_DEVICE_DT_INST_GET(inst)), (NULL)),	\
1197 		&bmi160_data_##inst, &bmi160_cfg_##inst,					\
1198 		POST_KERNEL, CONFIG_SENSOR_INIT_PRIORITY,					\
1199 		&bmi160_api);
1200 
1201 /* Instantiation macros used when a device is on a SPI bus */
1202 #define BMI160_DEFINE_SPI(inst)						   \
1203 	static struct bmi160_data bmi160_data_##inst;			   \
1204 	static const struct bmi160_cfg bmi160_cfg_##inst = {		   \
1205 		.bus.spi = SPI_DT_SPEC_INST_GET(inst, SPI_WORD_SET(8), 0), \
1206 		.bus_io = &bmi160_bus_io_spi,				   \
1207 		BMI160_TRIGGER_CFG(inst)				   \
1208 	};								   \
1209 	BMI160_DEVICE_INIT(inst)
1210 
1211 /* Instantiation macros used when a device is on an I2C bus */
1212 #define BMI160_CONFIG_I2C(inst)			       \
1213 	{					       \
1214 		.bus.i2c = I2C_DT_SPEC_INST_GET(inst), \
1215 		.bus_io = &bmi160_bus_io_i2c,	       \
1216 		BMI160_TRIGGER_CFG(inst)	       \
1217 	}
1218 
1219 #define BMI160_DEFINE_I2C(inst)							    \
1220 	static struct bmi160_data bmi160_data_##inst;				    \
1221 	static const struct bmi160_cfg bmi160_cfg_##inst = BMI160_CONFIG_I2C(inst); \
1222 	BMI160_DEVICE_INIT(inst)
1223 
1224 /*
1225  * Main instantiation macro. Use of COND_CODE_1() selects the right
1226  * bus-specific macro at preprocessor time.
1227  */
1228 #define BMI160_DEFINE(inst)						\
1229 	COND_CODE_1(DT_INST_ON_BUS(inst, spi),				\
1230 		    (BMI160_DEFINE_SPI(inst)),				\
1231 		    (BMI160_DEFINE_I2C(inst)))
1232 
1233 DT_INST_FOREACH_STATUS_OKAY(BMI160_DEFINE)
1234