1 /*
2 * Copyright (c) 2021 Bosch Sensortec GmbH
3 * Copyright (c) 2022 Nordic Semiconductor ASA
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 #define DT_DRV_COMPAT bosch_bmi270
9
10 #include <zephyr/drivers/sensor.h>
11 #include <zephyr/init.h>
12 #include <zephyr/kernel.h>
13 #include <zephyr/sys/__assert.h>
14 #include <zephyr/sys/byteorder.h>
15 #include <zephyr/logging/log.h>
16
17 #include "bmi270.h"
18 #include "bmi270_config_file.h"
19
20 LOG_MODULE_REGISTER(bmi270, CONFIG_SENSOR_LOG_LEVEL);
21
22 #define BMI270_WR_LEN 256
23 #define BMI270_CONFIG_FILE_RETRIES 15
24 #define BMI270_CONFIG_FILE_POLL_PERIOD_US 10000
25 #define BMI270_INTER_WRITE_DELAY_US 1000
26
bmi270_bus_check(const struct device * dev)27 static inline int bmi270_bus_check(const struct device *dev)
28 {
29 const struct bmi270_config *cfg = dev->config;
30
31 return cfg->bus_io->check(&cfg->bus);
32 }
33
bmi270_bus_init(const struct device * dev)34 static inline int bmi270_bus_init(const struct device *dev)
35 {
36 const struct bmi270_config *cfg = dev->config;
37
38 return cfg->bus_io->init(&cfg->bus);
39 }
40
bmi270_reg_read(const struct device * dev,uint8_t reg,uint8_t * data,uint16_t length)41 int bmi270_reg_read(const struct device *dev, uint8_t reg, uint8_t *data, uint16_t length)
42 {
43 const struct bmi270_config *cfg = dev->config;
44
45 return cfg->bus_io->read(&cfg->bus, reg, data, length);
46 }
47
bmi270_reg_write(const struct device * dev,uint8_t reg,const uint8_t * data,uint16_t length)48 int bmi270_reg_write(const struct device *dev, uint8_t reg,
49 const uint8_t *data, uint16_t length)
50 {
51 const struct bmi270_config *cfg = dev->config;
52
53 return cfg->bus_io->write(&cfg->bus, reg, data, length);
54 }
55
bmi270_reg_write_with_delay(const struct device * dev,uint8_t reg,const uint8_t * data,uint16_t length,uint32_t delay_us)56 int bmi270_reg_write_with_delay(const struct device *dev,
57 uint8_t reg,
58 const uint8_t *data,
59 uint16_t length,
60 uint32_t delay_us)
61 {
62 int ret = 0;
63
64 ret = bmi270_reg_write(dev, reg, data, length);
65 if (ret == 0) {
66 k_usleep(delay_us);
67 }
68 return ret;
69 }
70
channel_accel_convert(struct sensor_value * val,int64_t raw_val,uint8_t range)71 static void channel_accel_convert(struct sensor_value *val, int64_t raw_val,
72 uint8_t range)
73 {
74 /* 16 bit accelerometer. 2^15 bits represent the range in G */
75 /* Converting from G to m/s^2 */
76 raw_val = (raw_val * SENSOR_G * (int64_t) range) / INT16_MAX;
77
78 val->val1 = raw_val / 1000000LL;
79 val->val2 = raw_val % 1000000LL;
80 }
81
channel_gyro_convert(struct sensor_value * val,int64_t raw_val,uint16_t range)82 static void channel_gyro_convert(struct sensor_value *val, int64_t raw_val,
83 uint16_t range)
84 {
85 /* 16 bit gyroscope. 2^15 bits represent the range in degrees/s */
86 /* Converting from degrees/s to radians/s */
87
88 val->val1 = ((raw_val * (int64_t) range * SENSOR_PI)
89 / (180LL * INT16_MAX)) / 1000000LL;
90 val->val2 = ((raw_val * (int64_t) range * SENSOR_PI)
91 / (180LL * INT16_MAX)) % 1000000LL;
92 }
93
acc_odr_to_reg(const struct sensor_value * val)94 static uint8_t acc_odr_to_reg(const struct sensor_value *val)
95 {
96 double odr = sensor_value_to_double((struct sensor_value *) val);
97 uint8_t reg = 0;
98
99 if ((odr >= 0.78125) && (odr < 1.5625)) {
100 reg = BMI270_ACC_ODR_25D32_HZ;
101 } else if ((odr >= 1.5625) && (odr < 3.125)) {
102 reg = BMI270_ACC_ODR_25D16_HZ;
103 } else if ((odr >= 3.125) && (odr < 6.25)) {
104 reg = BMI270_ACC_ODR_25D8_HZ;
105 } else if ((odr >= 6.25) && (odr < 12.5)) {
106 reg = BMI270_ACC_ODR_25D4_HZ;
107 } else if ((odr >= 12.5) && (odr < 25.0)) {
108 reg = BMI270_ACC_ODR_25D2_HZ;
109 } else if ((odr >= 25.0) && (odr < 50.0)) {
110 reg = BMI270_ACC_ODR_25_HZ;
111 } else if ((odr >= 50.0) && (odr < 100.0)) {
112 reg = BMI270_ACC_ODR_50_HZ;
113 } else if ((odr >= 100.0) && (odr < 200.0)) {
114 reg = BMI270_ACC_ODR_100_HZ;
115 } else if ((odr >= 200.0) && (odr < 400.0)) {
116 reg = BMI270_ACC_ODR_200_HZ;
117 } else if ((odr >= 400.0) && (odr < 800.0)) {
118 reg = BMI270_ACC_ODR_400_HZ;
119 } else if ((odr >= 800.0) && (odr < 1600.0)) {
120 reg = BMI270_ACC_ODR_800_HZ;
121 } else if (odr >= 1600.0) {
122 reg = BMI270_ACC_ODR_1600_HZ;
123 }
124 return reg;
125 }
126
set_accel_odr_osr(const struct device * dev,const struct sensor_value * odr,const struct sensor_value * osr)127 static int set_accel_odr_osr(const struct device *dev, const struct sensor_value *odr,
128 const struct sensor_value *osr)
129 {
130 struct bmi270_data *data = dev->data;
131 uint8_t acc_conf, odr_bits, pwr_ctrl, osr_bits;
132 int ret = 0;
133
134 if (odr || osr) {
135 ret = bmi270_reg_read(dev, BMI270_REG_ACC_CONF, &acc_conf, 1);
136 if (ret != 0) {
137 return ret;
138 }
139
140 ret = bmi270_reg_read(dev, BMI270_REG_PWR_CTRL, &pwr_ctrl, 1);
141 if (ret != 0) {
142 return ret;
143 }
144 }
145
146 if (odr) {
147 odr_bits = acc_odr_to_reg(odr);
148 acc_conf = BMI270_SET_BITS_POS_0(acc_conf, BMI270_ACC_ODR,
149 odr_bits);
150
151 /* If odr_bits is 0, implies that the sampling frequency is 0Hz
152 * or invalid too.
153 */
154 if (odr_bits) {
155 pwr_ctrl |= BMI270_PWR_CTRL_ACC_EN;
156 } else {
157 pwr_ctrl &= ~BMI270_PWR_CTRL_ACC_EN;
158 }
159
160 /* If the Sampling frequency (odr) >= 100Hz, enter performance
161 * mode else, power optimized. This also has a consequence
162 * for the OSR
163 */
164 if (odr_bits >= BMI270_ACC_ODR_100_HZ) {
165 acc_conf = BMI270_SET_BITS(acc_conf, BMI270_ACC_FILT,
166 BMI270_ACC_FILT_PERF_OPT);
167 } else {
168 acc_conf = BMI270_SET_BITS(acc_conf, BMI270_ACC_FILT,
169 BMI270_ACC_FILT_PWR_OPT);
170 }
171
172 data->acc_odr = odr_bits;
173 }
174
175 if (osr) {
176 if (data->acc_odr >= BMI270_ACC_ODR_100_HZ) {
177 /* Performance mode */
178 /* osr->val2 should be unused */
179 switch (osr->val1) {
180 case 4:
181 osr_bits = BMI270_ACC_BWP_OSR4_AVG1;
182 break;
183 case 2:
184 osr_bits = BMI270_ACC_BWP_OSR2_AVG2;
185 break;
186 case 1:
187 osr_bits = BMI270_ACC_BWP_NORM_AVG4;
188 break;
189 default:
190 osr_bits = BMI270_ACC_BWP_CIC_AVG8;
191 break;
192 }
193 } else {
194 /* Power optimized mode */
195 /* osr->val2 should be unused */
196 switch (osr->val1) {
197 case 1:
198 osr_bits = BMI270_ACC_BWP_OSR4_AVG1;
199 break;
200 case 2:
201 osr_bits = BMI270_ACC_BWP_OSR2_AVG2;
202 break;
203 case 4:
204 osr_bits = BMI270_ACC_BWP_NORM_AVG4;
205 break;
206 case 8:
207 osr_bits = BMI270_ACC_BWP_CIC_AVG8;
208 break;
209 case 16:
210 osr_bits = BMI270_ACC_BWP_RES_AVG16;
211 break;
212 case 32:
213 osr_bits = BMI270_ACC_BWP_RES_AVG32;
214 break;
215 case 64:
216 osr_bits = BMI270_ACC_BWP_RES_AVG64;
217 break;
218 case 128:
219 osr_bits = BMI270_ACC_BWP_RES_AVG128;
220 break;
221 default:
222 return -ENOTSUP;
223 }
224 }
225
226 acc_conf = BMI270_SET_BITS(acc_conf, BMI270_ACC_BWP,
227 osr_bits);
228 }
229
230 if (odr || osr) {
231 ret = bmi270_reg_write(dev, BMI270_REG_ACC_CONF, &acc_conf, 1);
232 if (ret != 0) {
233 return ret;
234 }
235
236 /* Assuming we have advance power save enabled */
237 k_usleep(BMI270_TRANSC_DELAY_SUSPEND);
238
239 pwr_ctrl &= BMI270_PWR_CTRL_MSK;
240 ret = bmi270_reg_write_with_delay(dev, BMI270_REG_PWR_CTRL,
241 &pwr_ctrl, 1,
242 BMI270_INTER_WRITE_DELAY_US);
243 }
244
245 return ret;
246 }
247
set_accel_range(const struct device * dev,const struct sensor_value * range)248 static int set_accel_range(const struct device *dev, const struct sensor_value *range)
249 {
250 struct bmi270_data *data = dev->data;
251 int ret = 0;
252 uint8_t acc_range, reg;
253
254 ret = bmi270_reg_read(dev, BMI270_REG_ACC_RANGE, &acc_range, 1);
255 if (ret != 0) {
256 return ret;
257 }
258
259 /* range->val2 is unused */
260 switch (range->val1) {
261 case 2:
262 reg = BMI270_ACC_RANGE_2G;
263 data->acc_range = 2;
264 break;
265 case 4:
266 reg = BMI270_ACC_RANGE_4G;
267 data->acc_range = 4;
268 break;
269 case 8:
270 reg = BMI270_ACC_RANGE_8G;
271 data->acc_range = 8;
272 break;
273 case 16:
274 reg = BMI270_ACC_RANGE_16G;
275 data->acc_range = 16;
276 break;
277 default:
278 return -ENOTSUP;
279 }
280
281 acc_range = BMI270_SET_BITS_POS_0(acc_range, BMI270_ACC_RANGE,
282 reg);
283 ret = bmi270_reg_write_with_delay(dev, BMI270_REG_ACC_RANGE, &acc_range,
284 1, BMI270_INTER_WRITE_DELAY_US);
285
286 return ret;
287 }
288
gyr_odr_to_reg(const struct sensor_value * val)289 static uint8_t gyr_odr_to_reg(const struct sensor_value *val)
290 {
291 double odr = sensor_value_to_double((struct sensor_value *) val);
292 uint8_t reg = 0;
293
294 if ((odr >= 25.0) && (odr < 50.0)) {
295 reg = BMI270_GYR_ODR_25_HZ;
296 } else if ((odr >= 50.0) && (odr < 100.0)) {
297 reg = BMI270_GYR_ODR_50_HZ;
298 } else if ((odr >= 100.0) && (odr < 200.0)) {
299 reg = BMI270_GYR_ODR_100_HZ;
300 } else if ((odr >= 200.0) && (odr < 400.0)) {
301 reg = BMI270_GYR_ODR_200_HZ;
302 } else if ((odr >= 400.0) && (odr < 800.0)) {
303 reg = BMI270_GYR_ODR_400_HZ;
304 } else if ((odr >= 800.0) && (odr < 1600.0)) {
305 reg = BMI270_GYR_ODR_800_HZ;
306 } else if ((odr >= 1600.0) && (odr < 3200.0)) {
307 reg = BMI270_GYR_ODR_1600_HZ;
308 } else if (odr >= 3200.0) {
309 reg = BMI270_GYR_ODR_3200_HZ;
310 }
311
312 return reg;
313 }
314
set_gyro_odr_osr(const struct device * dev,const struct sensor_value * odr,const struct sensor_value * osr)315 static int set_gyro_odr_osr(const struct device *dev, const struct sensor_value *odr,
316 const struct sensor_value *osr)
317 {
318 struct bmi270_data *data = dev->data;
319 uint8_t gyr_conf, odr_bits, pwr_ctrl, osr_bits;
320 int ret = 0;
321
322 if (odr || osr) {
323 ret = bmi270_reg_read(dev, BMI270_REG_GYR_CONF, &gyr_conf, 1);
324 if (ret != 0) {
325 return ret;
326 }
327
328 ret = bmi270_reg_read(dev, BMI270_REG_PWR_CTRL, &pwr_ctrl, 1);
329 if (ret != 0) {
330 return ret;
331 }
332 }
333
334 if (odr) {
335 odr_bits = gyr_odr_to_reg(odr);
336 gyr_conf = BMI270_SET_BITS_POS_0(gyr_conf, BMI270_GYR_ODR,
337 odr_bits);
338
339 /* If odr_bits is 0, implies that the sampling frequency is
340 * 0Hz or invalid too.
341 */
342 if (odr_bits) {
343 pwr_ctrl |= BMI270_PWR_CTRL_GYR_EN;
344 } else {
345 pwr_ctrl &= ~BMI270_PWR_CTRL_GYR_EN;
346 }
347
348 /* If the Sampling frequency (odr) >= 100Hz, enter performance
349 * mode else, power optimized. This also has a consequence for
350 * the OSR
351 */
352 if (odr_bits >= BMI270_GYR_ODR_100_HZ) {
353 gyr_conf = BMI270_SET_BITS(gyr_conf,
354 BMI270_GYR_FILT,
355 BMI270_GYR_FILT_PERF_OPT);
356 gyr_conf = BMI270_SET_BITS(gyr_conf,
357 BMI270_GYR_FILT_NOISE,
358 BMI270_GYR_FILT_NOISE_PERF);
359 } else {
360 gyr_conf = BMI270_SET_BITS(gyr_conf,
361 BMI270_GYR_FILT,
362 BMI270_GYR_FILT_PWR_OPT);
363 gyr_conf = BMI270_SET_BITS(gyr_conf,
364 BMI270_GYR_FILT_NOISE,
365 BMI270_GYR_FILT_NOISE_PWR);
366 }
367
368 data->gyr_odr = odr_bits;
369 }
370
371 if (osr) {
372 /* osr->val2 should be unused */
373 switch (osr->val1) {
374 case 4:
375 osr_bits = BMI270_GYR_BWP_OSR4;
376 break;
377 case 2:
378 osr_bits = BMI270_GYR_BWP_OSR2;
379 break;
380 default:
381 osr_bits = BMI270_GYR_BWP_NORM;
382 break;
383 }
384
385 gyr_conf = BMI270_SET_BITS(gyr_conf, BMI270_GYR_BWP,
386 osr_bits);
387 }
388
389 if (odr || osr) {
390 ret = bmi270_reg_write(dev, BMI270_REG_GYR_CONF, &gyr_conf, 1);
391 if (ret != 0) {
392 return ret;
393 }
394
395 /* Assuming we have advance power save enabled */
396 k_usleep(BMI270_TRANSC_DELAY_SUSPEND);
397
398 pwr_ctrl &= BMI270_PWR_CTRL_MSK;
399 ret = bmi270_reg_write_with_delay(dev, BMI270_REG_PWR_CTRL,
400 &pwr_ctrl, 1,
401 BMI270_INTER_WRITE_DELAY_US);
402 }
403
404 return ret;
405 }
406
set_gyro_range(const struct device * dev,const struct sensor_value * range)407 static int set_gyro_range(const struct device *dev, const struct sensor_value *range)
408 {
409 struct bmi270_data *data = dev->data;
410 int ret = 0;
411 uint8_t gyr_range, reg;
412
413 ret = bmi270_reg_read(dev, BMI270_REG_GYR_RANGE, &gyr_range, 1);
414 if (ret != 0) {
415 return ret;
416 }
417
418 /* range->val2 is unused */
419 switch (range->val1) {
420 case 125:
421 reg = BMI270_GYR_RANGE_125DPS;
422 data->gyr_range = 125;
423 break;
424 case 250:
425 reg = BMI270_GYR_RANGE_250DPS;
426 data->gyr_range = 250;
427 break;
428 case 500:
429 reg = BMI270_GYR_RANGE_500DPS;
430 data->gyr_range = 500;
431 break;
432 case 1000:
433 reg = BMI270_GYR_RANGE_1000DPS;
434 data->gyr_range = 1000;
435 break;
436 case 2000:
437 reg = BMI270_GYR_RANGE_2000DPS;
438 data->gyr_range = 2000;
439 break;
440 default:
441 return -ENOTSUP;
442 }
443
444 gyr_range = BMI270_SET_BITS_POS_0(gyr_range, BMI270_GYR_RANGE, reg);
445 ret = bmi270_reg_write_with_delay(dev, BMI270_REG_GYR_RANGE, &gyr_range,
446 1, BMI270_INTER_WRITE_DELAY_US);
447
448 return ret;
449 }
450
write_config_file(const struct device * dev)451 static int8_t write_config_file(const struct device *dev)
452 {
453 const struct bmi270_config *cfg = dev->config;
454 int8_t ret = 0;
455 uint16_t index = 0;
456 uint8_t addr_array[2] = { 0 };
457
458 LOG_DBG("writing config file %s", cfg->feature->name);
459
460 /* Disable loading of the configuration */
461 for (index = 0; index < cfg->feature->config_file_len;
462 index += BMI270_WR_LEN) {
463 /* Store 0 to 3 bits of address in first byte */
464 addr_array[0] = (uint8_t)((index / 2) & 0x0F);
465
466 /* Store 4 to 11 bits of address in the second byte */
467 addr_array[1] = (uint8_t)((index / 2) >> 4);
468
469 ret = bmi270_reg_write_with_delay(dev, BMI270_REG_INIT_ADDR_0,
470 addr_array, 2,
471 BMI270_INTER_WRITE_DELAY_US);
472
473 if (ret == 0) {
474 ret = bmi270_reg_write_with_delay(dev,
475 BMI270_REG_INIT_DATA,
476 &cfg->feature->config_file[index],
477 BMI270_WR_LEN,
478 BMI270_INTER_WRITE_DELAY_US);
479 }
480 }
481
482 return ret;
483 }
484
bmi270_sample_fetch(const struct device * dev,enum sensor_channel chan)485 static int bmi270_sample_fetch(const struct device *dev, enum sensor_channel chan)
486 {
487 struct bmi270_data *data = dev->data;
488 uint8_t buf[12];
489 int ret;
490
491 if (chan != SENSOR_CHAN_ALL) {
492 return -ENOTSUP;
493 }
494
495 ret = bmi270_reg_read(dev, BMI270_REG_ACC_X_LSB, buf, 12);
496 if (ret == 0) {
497 data->ax = (int16_t)sys_get_le16(&buf[0]);
498 data->ay = (int16_t)sys_get_le16(&buf[2]);
499 data->az = (int16_t)sys_get_le16(&buf[4]);
500 data->gx = (int16_t)sys_get_le16(&buf[6]);
501 data->gy = (int16_t)sys_get_le16(&buf[8]);
502 data->gz = (int16_t)sys_get_le16(&buf[10]);
503 } else {
504 data->ax = 0;
505 data->ay = 0;
506 data->az = 0;
507 data->gx = 0;
508 data->gy = 0;
509 data->gz = 0;
510 }
511
512 return ret;
513 }
514
bmi270_channel_get(const struct device * dev,enum sensor_channel chan,struct sensor_value * val)515 static int bmi270_channel_get(const struct device *dev, enum sensor_channel chan,
516 struct sensor_value *val)
517 {
518 struct bmi270_data *data = dev->data;
519
520 if (chan == SENSOR_CHAN_ACCEL_X) {
521 channel_accel_convert(val, data->ax, data->acc_range);
522 } else if (chan == SENSOR_CHAN_ACCEL_Y) {
523 channel_accel_convert(val, data->ay, data->acc_range);
524 } else if (chan == SENSOR_CHAN_ACCEL_Z) {
525 channel_accel_convert(val, data->az, data->acc_range);
526 } else if (chan == SENSOR_CHAN_ACCEL_XYZ) {
527 channel_accel_convert(&val[0], data->ax,
528 data->acc_range);
529 channel_accel_convert(&val[1], data->ay,
530 data->acc_range);
531 channel_accel_convert(&val[2], data->az,
532 data->acc_range);
533 } else if (chan == SENSOR_CHAN_GYRO_X) {
534 channel_gyro_convert(val, data->gx, data->gyr_range);
535 } else if (chan == SENSOR_CHAN_GYRO_Y) {
536 channel_gyro_convert(val, data->gy, data->gyr_range);
537 } else if (chan == SENSOR_CHAN_GYRO_Z) {
538 channel_gyro_convert(val, data->gz, data->gyr_range);
539 } else if (chan == SENSOR_CHAN_GYRO_XYZ) {
540 channel_gyro_convert(&val[0], data->gx,
541 data->gyr_range);
542 channel_gyro_convert(&val[1], data->gy,
543 data->gyr_range);
544 channel_gyro_convert(&val[2], data->gz,
545 data->gyr_range);
546 } else {
547 return -ENOTSUP;
548 }
549
550 return 0;
551 }
552
553 #if defined(CONFIG_BMI270_TRIGGER)
554
555 /* ANYMO_1.duration conversion is 20 ms / LSB */
556 #define ANYMO_1_DURATION_MSEC_TO_LSB(_ms) \
557 BMI270_ANYMO_1_DURATION(_ms / 20)
558
bmi270_write_anymo_threshold(const struct device * dev,struct sensor_value val)559 static int bmi270_write_anymo_threshold(const struct device *dev,
560 struct sensor_value val)
561 {
562 struct bmi270_data *data = dev->data;
563
564 /* this takes configuration in g. */
565 if (val.val1 > 0) {
566 LOG_DBG("anymo_threshold set to max");
567 val.val2 = 1e6;
568 }
569
570 /* max = BIT_MASK(10) = 1g => 0.49 mg/LSB */
571 uint16_t lsbs = (val.val2 * BMI270_ANYMO_2_THRESHOLD_MASK) / 1e6;
572
573 if (!lsbs) {
574 LOG_ERR("Threshold too low!");
575 return -EINVAL;
576 }
577
578 uint16_t anymo_2 = BMI270_ANYMO_2_THRESHOLD(lsbs)
579 | BMI270_ANYMO_2_OUT_CONF_BIT_6;
580
581 data->anymo_2 = anymo_2;
582 return 0;
583 }
584
bmi270_write_anymo_duration(const struct device * dev,uint32_t ms)585 static int bmi270_write_anymo_duration(const struct device *dev, uint32_t ms)
586 {
587 struct bmi270_data *data = dev->data;
588 uint16_t val = ANYMO_1_DURATION_MSEC_TO_LSB(ms)
589 | BMI270_ANYMO_1_SELECT_XYZ;
590
591 data->anymo_1 = val;
592 return 0;
593 }
594 #endif /* CONFIG_BMI270_TRIGGER */
595
bmi270_attr_set(const struct device * dev,enum sensor_channel chan,enum sensor_attribute attr,const struct sensor_value * val)596 static int bmi270_attr_set(const struct device *dev, enum sensor_channel chan,
597 enum sensor_attribute attr, const struct sensor_value *val)
598 {
599 int ret = -ENOTSUP;
600
601 if ((chan == SENSOR_CHAN_ACCEL_X) || (chan == SENSOR_CHAN_ACCEL_Y)
602 || (chan == SENSOR_CHAN_ACCEL_Z)
603 || (chan == SENSOR_CHAN_ACCEL_XYZ)) {
604 switch (attr) {
605 case SENSOR_ATTR_SAMPLING_FREQUENCY:
606 ret = set_accel_odr_osr(dev, val, NULL);
607 break;
608 case SENSOR_ATTR_OVERSAMPLING:
609 ret = set_accel_odr_osr(dev, NULL, val);
610 break;
611 case SENSOR_ATTR_FULL_SCALE:
612 ret = set_accel_range(dev, val);
613 break;
614 #if defined(CONFIG_BMI270_TRIGGER)
615 case SENSOR_ATTR_SLOPE_DUR:
616 return bmi270_write_anymo_duration(dev, val->val1);
617 case SENSOR_ATTR_SLOPE_TH:
618 return bmi270_write_anymo_threshold(dev, *val);
619 #endif
620 default:
621 ret = -ENOTSUP;
622 }
623 } else if ((chan == SENSOR_CHAN_GYRO_X) || (chan == SENSOR_CHAN_GYRO_Y)
624 || (chan == SENSOR_CHAN_GYRO_Z)
625 || (chan == SENSOR_CHAN_GYRO_XYZ)) {
626 switch (attr) {
627 case SENSOR_ATTR_SAMPLING_FREQUENCY:
628 ret = set_gyro_odr_osr(dev, val, NULL);
629 break;
630 case SENSOR_ATTR_OVERSAMPLING:
631 ret = set_gyro_odr_osr(dev, NULL, val);
632 break;
633 case SENSOR_ATTR_FULL_SCALE:
634 ret = set_gyro_range(dev, val);
635 break;
636 default:
637 ret = -ENOTSUP;
638 }
639 }
640
641 return ret;
642 }
643
bmi270_init(const struct device * dev)644 static int bmi270_init(const struct device *dev)
645 {
646 int ret;
647 struct bmi270_data *data = dev->data;
648 uint8_t chip_id;
649 uint8_t soft_reset_cmd;
650 uint8_t init_ctrl;
651 uint8_t msg;
652 uint8_t tries;
653 uint8_t adv_pwr_save;
654
655 ret = bmi270_bus_check(dev);
656 if (ret < 0) {
657 LOG_ERR("Could not initialize bus");
658 return ret;
659 }
660
661 #if CONFIG_BMI270_TRIGGER
662 data->dev = dev;
663 k_mutex_init(&data->trigger_mutex);
664 #endif
665
666 data->acc_odr = BMI270_ACC_ODR_100_HZ;
667 data->acc_range = 8;
668 data->gyr_odr = BMI270_GYR_ODR_200_HZ;
669 data->gyr_range = 2000;
670
671 k_usleep(BMI270_POWER_ON_TIME);
672
673 ret = bmi270_bus_init(dev);
674 if (ret != 0) {
675 LOG_ERR("Could not initiate bus communication");
676 return ret;
677 }
678
679 ret = bmi270_reg_read(dev, BMI270_REG_CHIP_ID, &chip_id, 1);
680 if (ret != 0) {
681 return ret;
682 }
683
684 if (chip_id != BMI270_CHIP_ID) {
685 LOG_ERR("Unexpected chip id (%x). Expected (%x)",
686 chip_id, BMI270_CHIP_ID);
687 return -EIO;
688 }
689
690 soft_reset_cmd = BMI270_CMD_SOFT_RESET;
691 ret = bmi270_reg_write(dev, BMI270_REG_CMD, &soft_reset_cmd, 1);
692 if (ret != 0) {
693 return ret;
694 }
695
696 k_usleep(BMI270_SOFT_RESET_TIME);
697
698 ret = bmi270_reg_read(dev, BMI270_REG_PWR_CONF, &adv_pwr_save, 1);
699 if (ret != 0) {
700 return ret;
701 }
702
703 adv_pwr_save = BMI270_SET_BITS_POS_0(adv_pwr_save,
704 BMI270_PWR_CONF_ADV_PWR_SAVE,
705 BMI270_PWR_CONF_ADV_PWR_SAVE_DIS);
706 ret = bmi270_reg_write_with_delay(dev, BMI270_REG_PWR_CONF,
707 &adv_pwr_save, 1,
708 BMI270_INTER_WRITE_DELAY_US);
709 if (ret != 0) {
710 return ret;
711 }
712
713 init_ctrl = BMI270_PREPARE_CONFIG_LOAD;
714 ret = bmi270_reg_write(dev, BMI270_REG_INIT_CTRL, &init_ctrl, 1);
715 if (ret != 0) {
716 return ret;
717 }
718
719 ret = write_config_file(dev);
720
721 if (ret != 0) {
722 return ret;
723 }
724
725 init_ctrl = BMI270_COMPLETE_CONFIG_LOAD;
726 ret = bmi270_reg_write(dev, BMI270_REG_INIT_CTRL, &init_ctrl, 1);
727 if (ret != 0) {
728 return ret;
729 }
730
731 /* Timeout after BMI270_CONFIG_FILE_RETRIES x
732 * BMI270_CONFIG_FILE_POLL_PERIOD_US microseconds.
733 * If tries is BMI270_CONFIG_FILE_RETRIES by the end of the loop,
734 * report an error
735 */
736 for (tries = 0; tries <= BMI270_CONFIG_FILE_RETRIES; tries++) {
737 ret = bmi270_reg_read(dev, BMI270_REG_INTERNAL_STATUS, &msg, 1);
738 if (ret != 0) {
739 return ret;
740 }
741
742 msg &= BMI270_INST_MESSAGE_MSK;
743 if (msg == BMI270_INST_MESSAGE_INIT_OK) {
744 break;
745 }
746
747 k_usleep(BMI270_CONFIG_FILE_POLL_PERIOD_US);
748 }
749
750 if (tries == BMI270_CONFIG_FILE_RETRIES) {
751 return -EIO;
752 }
753
754 #if CONFIG_BMI270_TRIGGER
755 ret = bmi270_init_interrupts(dev);
756 if (ret) {
757 LOG_ERR("bmi270_init_interrupts returned %d", ret);
758 return ret;
759 }
760 #endif
761
762 adv_pwr_save = BMI270_SET_BITS_POS_0(adv_pwr_save,
763 BMI270_PWR_CONF_ADV_PWR_SAVE,
764 BMI270_PWR_CONF_ADV_PWR_SAVE_EN);
765 ret = bmi270_reg_write_with_delay(dev, BMI270_REG_PWR_CONF,
766 &adv_pwr_save, 1,
767 BMI270_INTER_WRITE_DELAY_US);
768
769 return ret;
770 }
771
772 static const struct sensor_driver_api bmi270_driver_api = {
773 .sample_fetch = bmi270_sample_fetch,
774 .channel_get = bmi270_channel_get,
775 .attr_set = bmi270_attr_set,
776 #if defined(CONFIG_BMI270_TRIGGER)
777 .trigger_set = bmi270_trigger_set,
778 #endif
779 };
780
781 static const struct bmi270_feature_config bmi270_feature_max_fifo = {
782 .name = "max_fifo",
783 .config_file = bmi270_config_file_max_fifo,
784 .config_file_len = sizeof(bmi270_config_file_max_fifo),
785 };
786
787 static const struct bmi270_feature_config bmi270_feature_base = {
788 .name = "base",
789 .config_file = bmi270_config_file_base,
790 .config_file_len = sizeof(bmi270_config_file_base),
791 .anymo_1 = &(struct bmi270_feature_reg){ .page = 1, .addr = 0x3C },
792 .anymo_2 = &(struct bmi270_feature_reg){ .page = 1, .addr = 0x3E },
793 };
794
795 #define BMI270_FEATURE(inst) ( \
796 DT_NODE_HAS_COMPAT(DT_DRV_INST(inst), bosch_bmi270_base) ? \
797 &bmi270_feature_base : \
798 &bmi270_feature_max_fifo)
799
800 #if CONFIG_BMI270_TRIGGER
801 #define BMI270_CONFIG_INT(inst) \
802 .int1 = GPIO_DT_SPEC_INST_GET_BY_IDX_OR(inst, irq_gpios, 0, {}),\
803 .int2 = GPIO_DT_SPEC_INST_GET_BY_IDX_OR(inst, irq_gpios, 1, {}),
804 #else
805 #define BMI270_CONFIG_INT(inst)
806 #endif
807
808 /* Initializes a struct bmi270_config for an instance on a SPI bus. */
809 #define BMI270_CONFIG_SPI(inst) \
810 .bus.spi = SPI_DT_SPEC_INST_GET( \
811 inst, BMI270_SPI_OPERATION, 0), \
812 .bus_io = &bmi270_bus_io_spi,
813
814 /* Initializes a struct bmi270_config for an instance on an I2C bus. */
815 #define BMI270_CONFIG_I2C(inst) \
816 .bus.i2c = I2C_DT_SPEC_INST_GET(inst), \
817 .bus_io = &bmi270_bus_io_i2c,
818
819 #define BMI270_CREATE_INST(inst) \
820 \
821 static struct bmi270_data bmi270_drv_##inst; \
822 \
823 static const struct bmi270_config bmi270_config_##inst = { \
824 COND_CODE_1(DT_INST_ON_BUS(inst, spi), \
825 (BMI270_CONFIG_SPI(inst)), \
826 (BMI270_CONFIG_I2C(inst))) \
827 .feature = BMI270_FEATURE(inst), \
828 BMI270_CONFIG_INT(inst) \
829 }; \
830 \
831 SENSOR_DEVICE_DT_INST_DEFINE(inst, \
832 bmi270_init, \
833 NULL, \
834 &bmi270_drv_##inst, \
835 &bmi270_config_##inst, \
836 POST_KERNEL, \
837 CONFIG_SENSOR_INIT_PRIORITY, \
838 &bmi270_driver_api);
839
840 DT_INST_FOREACH_STATUS_OKAY(BMI270_CREATE_INST);
841