1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
3 #include <linux/bitfield.h>
4 #include <linux/firmware.h>
6 #include <linux/module.h>
7 #include <linux/regmap.h>
9 #include <linux/iio/iio.h>
10 #include <linux/iio/sysfs.h>
11 #include <linux/iio/triggered_buffer.h>
12 #include <linux/iio/trigger_consumer.h>
16 #define BMI270_CHIP_ID_REG 0x00
18 /* Checked to prevent sending incompatible firmware to BMI160 devices */
19 #define BMI160_CHIP_ID_VAL 0xD1
21 #define BMI260_CHIP_ID_VAL 0x27
22 #define BMI270_CHIP_ID_VAL 0x24
23 #define BMI270_CHIP_ID_MSK GENMASK(7, 0)
25 #define BMI270_ACCEL_X_REG 0x0c
26 #define BMI270_ANG_VEL_X_REG 0x12
28 #define BMI270_INTERNAL_STATUS_REG 0x21
29 #define BMI270_INTERNAL_STATUS_MSG_MSK GENMASK(3, 0)
30 #define BMI270_INTERNAL_STATUS_MSG_INIT_OK 0x01
32 #define BMI270_INTERNAL_STATUS_AXES_REMAP_ERR_MSK BIT(5)
33 #define BMI270_INTERNAL_STATUS_ODR_50HZ_ERR_MSK BIT(6)
35 #define BMI270_ACC_CONF_REG 0x40
36 #define BMI270_ACC_CONF_ODR_MSK GENMASK(3, 0)
37 #define BMI270_ACC_CONF_ODR_100HZ 0x08
38 #define BMI270_ACC_CONF_BWP_MSK GENMASK(6, 4)
39 #define BMI270_ACC_CONF_BWP_NORMAL_MODE 0x02
40 #define BMI270_ACC_CONF_FILTER_PERF_MSK BIT(7)
42 #define BMI270_ACC_CONF_RANGE_REG 0x41
43 #define BMI270_ACC_CONF_RANGE_MSK GENMASK(1, 0)
45 #define BMI270_GYR_CONF_REG 0x42
46 #define BMI270_GYR_CONF_ODR_MSK GENMASK(3, 0)
47 #define BMI270_GYR_CONF_ODR_200HZ 0x09
48 #define BMI270_GYR_CONF_BWP_MSK GENMASK(5, 4)
49 #define BMI270_GYR_CONF_BWP_NORMAL_MODE 0x02
50 #define BMI270_GYR_CONF_NOISE_PERF_MSK BIT(6)
51 #define BMI270_GYR_CONF_FILTER_PERF_MSK BIT(7)
53 #define BMI270_GYR_CONF_RANGE_REG 0x43
54 #define BMI270_GYR_CONF_RANGE_MSK GENMASK(2, 0)
56 #define BMI270_INIT_CTRL_REG 0x59
57 #define BMI270_INIT_CTRL_LOAD_DONE_MSK BIT(0)
59 #define BMI270_INIT_DATA_REG 0x5e
61 #define BMI270_PWR_CONF_REG 0x7c
62 #define BMI270_PWR_CONF_ADV_PWR_SAVE_MSK BIT(0)
63 #define BMI270_PWR_CONF_FIFO_WKUP_MSK BIT(1)
64 #define BMI270_PWR_CONF_FUP_EN_MSK BIT(2)
66 #define BMI270_PWR_CTRL_REG 0x7d
67 #define BMI270_PWR_CTRL_AUX_EN_MSK BIT(0)
68 #define BMI270_PWR_CTRL_GYR_EN_MSK BIT(1)
69 #define BMI270_PWR_CTRL_ACCEL_EN_MSK BIT(2)
70 #define BMI270_PWR_CTRL_TEMP_EN_MSK BIT(3)
72 #define BMI260_INIT_DATA_FILE "bmi260-init-data.fw"
73 #define BMI270_INIT_DATA_FILE "bmi270-init-data.fw"
82 BMI270_SCAN_TIMESTAMP
,
85 static const unsigned long bmi270_avail_scan_masks
[] = {
86 (BIT(BMI270_SCAN_ACCEL_X
) |
87 BIT(BMI270_SCAN_ACCEL_Y
) |
88 BIT(BMI270_SCAN_ACCEL_Z
) |
89 BIT(BMI270_SCAN_GYRO_X
) |
90 BIT(BMI270_SCAN_GYRO_Y
) |
91 BIT(BMI270_SCAN_GYRO_Z
)),
95 const struct bmi270_chip_info bmi260_chip_info
= {
97 .chip_id
= BMI260_CHIP_ID_VAL
,
98 .fw_name
= BMI260_INIT_DATA_FILE
,
100 EXPORT_SYMBOL_NS_GPL(bmi260_chip_info
, "IIO_BMI270");
102 const struct bmi270_chip_info bmi270_chip_info
= {
104 .chip_id
= BMI270_CHIP_ID_VAL
,
105 .fw_name
= BMI270_INIT_DATA_FILE
,
107 EXPORT_SYMBOL_NS_GPL(bmi270_chip_info
, "IIO_BMI270");
109 enum bmi270_sensor_type
{
114 struct bmi270_scale
{
124 static const struct bmi270_scale bmi270_accel_scale
[] = {
131 static const struct bmi270_scale bmi270_gyro_scale
[] = {
139 struct bmi270_scale_item
{
140 const struct bmi270_scale
*tbl
;
144 static const struct bmi270_scale_item bmi270_scale_table
[] = {
146 .tbl
= bmi270_accel_scale
,
147 .num
= ARRAY_SIZE(bmi270_accel_scale
),
150 .tbl
= bmi270_gyro_scale
,
151 .num
= ARRAY_SIZE(bmi270_gyro_scale
),
155 static const struct bmi270_odr bmi270_accel_odr
[] = {
170 static const u8 bmi270_accel_odr_vals
[] = {
185 static const struct bmi270_odr bmi270_gyro_odr
[] = {
196 static const u8 bmi270_gyro_odr_vals
[] = {
207 struct bmi270_odr_item
{
208 const struct bmi270_odr
*tbl
;
213 static const struct bmi270_odr_item bmi270_odr_table
[] = {
215 .tbl
= bmi270_accel_odr
,
216 .vals
= bmi270_accel_odr_vals
,
217 .num
= ARRAY_SIZE(bmi270_accel_odr
),
220 .tbl
= bmi270_gyro_odr
,
221 .vals
= bmi270_gyro_odr_vals
,
222 .num
= ARRAY_SIZE(bmi270_gyro_odr
),
226 static int bmi270_set_scale(struct bmi270_data
*data
, int chan_type
, int uscale
)
230 struct bmi270_scale_item bmi270_scale_item
;
234 reg
= BMI270_ACC_CONF_RANGE_REG
;
235 mask
= BMI270_ACC_CONF_RANGE_MSK
;
236 bmi270_scale_item
= bmi270_scale_table
[BMI270_ACCEL
];
239 reg
= BMI270_GYR_CONF_RANGE_REG
;
240 mask
= BMI270_GYR_CONF_RANGE_MSK
;
241 bmi270_scale_item
= bmi270_scale_table
[BMI270_GYRO
];
247 for (i
= 0; i
< bmi270_scale_item
.num
; i
++) {
248 if (bmi270_scale_item
.tbl
[i
].uscale
!= uscale
)
251 return regmap_update_bits(data
->regmap
, reg
, mask
, i
);
257 static int bmi270_get_scale(struct bmi270_data
*bmi270_device
, int chan_type
,
262 struct bmi270_scale_item bmi270_scale_item
;
266 ret
= regmap_read(bmi270_device
->regmap
,
267 BMI270_ACC_CONF_RANGE_REG
, &val
);
271 val
= FIELD_GET(BMI270_ACC_CONF_RANGE_MSK
, val
);
272 bmi270_scale_item
= bmi270_scale_table
[BMI270_ACCEL
];
275 ret
= regmap_read(bmi270_device
->regmap
,
276 BMI270_GYR_CONF_RANGE_REG
, &val
);
280 val
= FIELD_GET(BMI270_GYR_CONF_RANGE_MSK
, val
);
281 bmi270_scale_item
= bmi270_scale_table
[BMI270_GYRO
];
287 if (val
>= bmi270_scale_item
.num
)
290 *uscale
= bmi270_scale_item
.tbl
[val
].uscale
;
294 static int bmi270_set_odr(struct bmi270_data
*data
, int chan_type
, int odr
,
299 struct bmi270_odr_item bmi270_odr_item
;
303 reg
= BMI270_ACC_CONF_REG
;
304 mask
= BMI270_ACC_CONF_ODR_MSK
;
305 bmi270_odr_item
= bmi270_odr_table
[BMI270_ACCEL
];
308 reg
= BMI270_GYR_CONF_REG
;
309 mask
= BMI270_GYR_CONF_ODR_MSK
;
310 bmi270_odr_item
= bmi270_odr_table
[BMI270_GYRO
];
316 for (i
= 0; i
< bmi270_odr_item
.num
; i
++) {
317 if (bmi270_odr_item
.tbl
[i
].odr
!= odr
||
318 bmi270_odr_item
.tbl
[i
].uodr
!= uodr
)
321 return regmap_update_bits(data
->regmap
, reg
, mask
,
322 bmi270_odr_item
.vals
[i
]);
328 static int bmi270_get_odr(struct bmi270_data
*data
, int chan_type
, int *odr
,
332 struct bmi270_odr_item bmi270_odr_item
;
336 ret
= regmap_read(data
->regmap
, BMI270_ACC_CONF_REG
, &val
);
340 val
= FIELD_GET(BMI270_ACC_CONF_ODR_MSK
, val
);
341 bmi270_odr_item
= bmi270_odr_table
[BMI270_ACCEL
];
344 ret
= regmap_read(data
->regmap
, BMI270_GYR_CONF_REG
, &val
);
348 val
= FIELD_GET(BMI270_GYR_CONF_ODR_MSK
, val
);
349 bmi270_odr_item
= bmi270_odr_table
[BMI270_GYRO
];
355 for (i
= 0; i
< bmi270_odr_item
.num
; i
++) {
356 if (val
!= bmi270_odr_item
.vals
[i
])
359 *odr
= bmi270_odr_item
.tbl
[i
].odr
;
360 *uodr
= bmi270_odr_item
.tbl
[i
].uodr
;
367 static irqreturn_t
bmi270_trigger_handler(int irq
, void *p
)
369 struct iio_poll_func
*pf
= p
;
370 struct iio_dev
*indio_dev
= pf
->indio_dev
;
371 struct bmi270_data
*bmi270_device
= iio_priv(indio_dev
);
374 ret
= regmap_bulk_read(bmi270_device
->regmap
, BMI270_ACCEL_X_REG
,
375 &bmi270_device
->data
.channels
,
376 sizeof(bmi270_device
->data
.channels
));
381 iio_push_to_buffers_with_timestamp(indio_dev
, &bmi270_device
->data
,
384 iio_trigger_notify_done(indio_dev
->trig
);
388 static int bmi270_get_data(struct bmi270_data
*bmi270_device
,
389 int chan_type
, int axis
, int *val
)
397 reg
= BMI270_ACCEL_X_REG
+ (axis
- IIO_MOD_X
) * 2;
400 reg
= BMI270_ANG_VEL_X_REG
+ (axis
- IIO_MOD_X
) * 2;
406 ret
= regmap_bulk_read(bmi270_device
->regmap
, reg
, &sample
, sizeof(sample
));
410 *val
= sign_extend32(le16_to_cpu(sample
), 15);
415 static int bmi270_read_raw(struct iio_dev
*indio_dev
,
416 struct iio_chan_spec
const *chan
,
417 int *val
, int *val2
, long mask
)
420 struct bmi270_data
*bmi270_device
= iio_priv(indio_dev
);
423 case IIO_CHAN_INFO_RAW
:
424 ret
= bmi270_get_data(bmi270_device
, chan
->type
, chan
->channel2
, val
);
429 case IIO_CHAN_INFO_SCALE
:
431 ret
= bmi270_get_scale(bmi270_device
, chan
->type
, val2
);
432 return ret
? ret
: IIO_VAL_INT_PLUS_MICRO
;
433 case IIO_CHAN_INFO_SAMP_FREQ
:
434 ret
= bmi270_get_odr(bmi270_device
, chan
->type
, val
, val2
);
435 return ret
? ret
: IIO_VAL_INT_PLUS_MICRO
;
441 static int bmi270_write_raw(struct iio_dev
*indio_dev
,
442 struct iio_chan_spec
const *chan
,
443 int val
, int val2
, long mask
)
445 struct bmi270_data
*data
= iio_priv(indio_dev
);
448 case IIO_CHAN_INFO_SCALE
:
449 return bmi270_set_scale(data
, chan
->type
, val2
);
450 case IIO_CHAN_INFO_SAMP_FREQ
:
451 return bmi270_set_odr(data
, chan
->type
, val
, val2
);
457 static int bmi270_read_avail(struct iio_dev
*indio_dev
,
458 struct iio_chan_spec
const *chan
,
459 const int **vals
, int *type
, int *length
,
463 case IIO_CHAN_INFO_SCALE
:
464 *type
= IIO_VAL_INT_PLUS_MICRO
;
465 switch (chan
->type
) {
467 *vals
= (const int *)bmi270_gyro_scale
;
468 *length
= ARRAY_SIZE(bmi270_gyro_scale
) * 2;
469 return IIO_AVAIL_LIST
;
471 *vals
= (const int *)bmi270_accel_scale
;
472 *length
= ARRAY_SIZE(bmi270_accel_scale
) * 2;
473 return IIO_AVAIL_LIST
;
477 case IIO_CHAN_INFO_SAMP_FREQ
:
478 *type
= IIO_VAL_INT_PLUS_MICRO
;
479 switch (chan
->type
) {
481 *vals
= (const int *)bmi270_gyro_odr
;
482 *length
= ARRAY_SIZE(bmi270_gyro_odr
) * 2;
483 return IIO_AVAIL_LIST
;
485 *vals
= (const int *)bmi270_accel_odr
;
486 *length
= ARRAY_SIZE(bmi270_accel_odr
) * 2;
487 return IIO_AVAIL_LIST
;
496 static const struct iio_info bmi270_info
= {
497 .read_raw
= bmi270_read_raw
,
498 .write_raw
= bmi270_write_raw
,
499 .read_avail
= bmi270_read_avail
,
502 #define BMI270_ACCEL_CHANNEL(_axis) { \
505 .channel2 = IIO_MOD_##_axis, \
506 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
507 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
508 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
509 .info_mask_shared_by_type_available = \
510 BIT(IIO_CHAN_INFO_SCALE) | \
511 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
512 .scan_index = BMI270_SCAN_ACCEL_##_axis, \
517 .endianness = IIO_LE, \
521 #define BMI270_ANG_VEL_CHANNEL(_axis) { \
522 .type = IIO_ANGL_VEL, \
524 .channel2 = IIO_MOD_##_axis, \
525 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
526 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
527 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
528 .info_mask_shared_by_type_available = \
529 BIT(IIO_CHAN_INFO_SCALE) | \
530 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
531 .scan_index = BMI270_SCAN_GYRO_##_axis, \
536 .endianness = IIO_LE, \
540 static const struct iio_chan_spec bmi270_channels
[] = {
541 BMI270_ACCEL_CHANNEL(X
),
542 BMI270_ACCEL_CHANNEL(Y
),
543 BMI270_ACCEL_CHANNEL(Z
),
544 BMI270_ANG_VEL_CHANNEL(X
),
545 BMI270_ANG_VEL_CHANNEL(Y
),
546 BMI270_ANG_VEL_CHANNEL(Z
),
547 IIO_CHAN_SOFT_TIMESTAMP(BMI270_SCAN_TIMESTAMP
),
550 static int bmi270_validate_chip_id(struct bmi270_data
*bmi270_device
)
554 struct device
*dev
= bmi270_device
->dev
;
555 struct regmap
*regmap
= bmi270_device
->regmap
;
557 ret
= regmap_read(regmap
, BMI270_CHIP_ID_REG
, &chip_id
);
559 return dev_err_probe(dev
, ret
, "Failed to read chip id");
562 * Some manufacturers use "BMI0160" for both the BMI160 and
563 * BMI260. If the device is actually a BMI160, the bmi160
564 * driver should handle it and this driver should not.
566 if (chip_id
== BMI160_CHIP_ID_VAL
)
569 if (chip_id
!= bmi270_device
->chip_info
->chip_id
)
570 dev_info(dev
, "Unexpected chip id 0x%x", chip_id
);
572 if (chip_id
== bmi260_chip_info
.chip_id
)
573 bmi270_device
->chip_info
= &bmi260_chip_info
;
574 else if (chip_id
== bmi270_chip_info
.chip_id
)
575 bmi270_device
->chip_info
= &bmi270_chip_info
;
580 static int bmi270_write_calibration_data(struct bmi270_data
*bmi270_device
)
584 const struct firmware
*init_data
;
585 struct device
*dev
= bmi270_device
->dev
;
586 struct regmap
*regmap
= bmi270_device
->regmap
;
588 ret
= regmap_clear_bits(regmap
, BMI270_PWR_CONF_REG
,
589 BMI270_PWR_CONF_ADV_PWR_SAVE_MSK
);
591 return dev_err_probe(dev
, ret
,
592 "Failed to write power configuration");
595 * After disabling advanced power save, all registers are accessible
596 * after a 450us delay. This delay is specified in table A of the
599 usleep_range(450, 1000);
601 ret
= regmap_clear_bits(regmap
, BMI270_INIT_CTRL_REG
,
602 BMI270_INIT_CTRL_LOAD_DONE_MSK
);
604 return dev_err_probe(dev
, ret
,
605 "Failed to prepare device to load init data");
607 ret
= request_firmware(&init_data
,
608 bmi270_device
->chip_info
->fw_name
, dev
);
610 return dev_err_probe(dev
, ret
, "Failed to load init data file");
612 ret
= regmap_bulk_write(regmap
, BMI270_INIT_DATA_REG
,
613 init_data
->data
, init_data
->size
);
614 release_firmware(init_data
);
616 return dev_err_probe(dev
, ret
, "Failed to write init data");
618 ret
= regmap_set_bits(regmap
, BMI270_INIT_CTRL_REG
,
619 BMI270_INIT_CTRL_LOAD_DONE_MSK
);
621 return dev_err_probe(dev
, ret
,
622 "Failed to stop device initialization");
625 * Wait at least 140ms for the device to complete configuration.
626 * This delay is specified in table C of the datasheet.
628 usleep_range(140000, 160000);
630 ret
= regmap_read(regmap
, BMI270_INTERNAL_STATUS_REG
, &status
);
632 return dev_err_probe(dev
, ret
, "Failed to read internal status");
634 if (status
!= BMI270_INTERNAL_STATUS_MSG_INIT_OK
)
635 return dev_err_probe(dev
, -ENODEV
, "Device failed to initialize");
640 static int bmi270_configure_imu(struct bmi270_data
*bmi270_device
)
643 struct device
*dev
= bmi270_device
->dev
;
644 struct regmap
*regmap
= bmi270_device
->regmap
;
646 ret
= regmap_set_bits(regmap
, BMI270_PWR_CTRL_REG
,
647 BMI270_PWR_CTRL_AUX_EN_MSK
|
648 BMI270_PWR_CTRL_GYR_EN_MSK
|
649 BMI270_PWR_CTRL_ACCEL_EN_MSK
);
651 return dev_err_probe(dev
, ret
, "Failed to enable accelerometer and gyroscope");
653 ret
= regmap_set_bits(regmap
, BMI270_ACC_CONF_REG
,
654 FIELD_PREP(BMI270_ACC_CONF_ODR_MSK
,
655 BMI270_ACC_CONF_ODR_100HZ
) |
656 FIELD_PREP(BMI270_ACC_CONF_BWP_MSK
,
657 BMI270_ACC_CONF_BWP_NORMAL_MODE
) |
658 BMI270_PWR_CONF_ADV_PWR_SAVE_MSK
);
660 return dev_err_probe(dev
, ret
, "Failed to configure accelerometer");
662 ret
= regmap_set_bits(regmap
, BMI270_GYR_CONF_REG
,
663 FIELD_PREP(BMI270_GYR_CONF_ODR_MSK
,
664 BMI270_GYR_CONF_ODR_200HZ
) |
665 FIELD_PREP(BMI270_GYR_CONF_BWP_MSK
,
666 BMI270_GYR_CONF_BWP_NORMAL_MODE
) |
667 BMI270_PWR_CONF_ADV_PWR_SAVE_MSK
);
669 return dev_err_probe(dev
, ret
, "Failed to configure gyroscope");
671 /* Enable FIFO_WKUP, Disable ADV_PWR_SAVE and FUP_EN */
672 ret
= regmap_write(regmap
, BMI270_PWR_CONF_REG
,
673 BMI270_PWR_CONF_FIFO_WKUP_MSK
);
675 return dev_err_probe(dev
, ret
, "Failed to set power configuration");
680 static int bmi270_chip_init(struct bmi270_data
*bmi270_device
)
684 ret
= bmi270_validate_chip_id(bmi270_device
);
688 ret
= bmi270_write_calibration_data(bmi270_device
);
692 return bmi270_configure_imu(bmi270_device
);
695 int bmi270_core_probe(struct device
*dev
, struct regmap
*regmap
,
696 const struct bmi270_chip_info
*chip_info
)
699 struct bmi270_data
*bmi270_device
;
700 struct iio_dev
*indio_dev
;
702 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*bmi270_device
));
706 bmi270_device
= iio_priv(indio_dev
);
707 bmi270_device
->dev
= dev
;
708 bmi270_device
->regmap
= regmap
;
709 bmi270_device
->chip_info
= chip_info
;
711 ret
= bmi270_chip_init(bmi270_device
);
715 indio_dev
->channels
= bmi270_channels
;
716 indio_dev
->num_channels
= ARRAY_SIZE(bmi270_channels
);
717 indio_dev
->name
= chip_info
->name
;
718 indio_dev
->available_scan_masks
= bmi270_avail_scan_masks
;
719 indio_dev
->modes
= INDIO_DIRECT_MODE
;
720 indio_dev
->info
= &bmi270_info
;
722 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
,
723 iio_pollfunc_store_time
,
724 bmi270_trigger_handler
, NULL
);
728 return devm_iio_device_register(dev
, indio_dev
);
730 EXPORT_SYMBOL_NS_GPL(bmi270_core_probe
, "IIO_BMI270");
732 MODULE_AUTHOR("Alex Lanzano");
733 MODULE_DESCRIPTION("BMI270 driver");
734 MODULE_LICENSE("GPL");