1 // SPDX-License-Identifier: GPL-2.0
3 * IIO driver for Bosch BNO055 IMU
5 * Copyright (C) 2021-2022 Istituto Italiano di Tecnologia
6 * Electronic Design Laboratory
7 * Written by Andrea Merello <andrea.merello@iit.it>
9 * Portions of this driver are taken from the BNO055 driver patch
10 * from Vlad Dogaru which is Copyright (c) 2016, Intel Corporation.
12 * This driver is also based on BMI160 driver, which is:
13 * Copyright (c) 2016, Intel Corporation.
14 * Copyright (c) 2019, Martin Kelly.
17 #include <linux/bitfield.h>
18 #include <linux/bitmap.h>
19 #include <linux/clk.h>
20 #include <linux/debugfs.h>
21 #include <linux/device.h>
22 #include <linux/firmware.h>
23 #include <linux/gpio/consumer.h>
24 #include <linux/module.h>
25 #include <linux/mutex.h>
26 #include <linux/regmap.h>
27 #include <linux/util_macros.h>
29 #include <linux/iio/buffer.h>
30 #include <linux/iio/iio.h>
31 #include <linux/iio/sysfs.h>
32 #include <linux/iio/trigger_consumer.h>
33 #include <linux/iio/triggered_buffer.h>
37 #define BNO055_FW_UID_FMT "bno055-caldata-%*phN.dat"
38 #define BNO055_FW_GENERIC_NAME "bno055-caldata.dat"
40 /* common registers */
41 #define BNO055_PAGESEL_REG 0x7
43 /* page 0 registers */
44 #define BNO055_CHIP_ID_REG 0x0
45 #define BNO055_CHIP_ID_MAGIC 0xA0
46 #define BNO055_SW_REV_LSB_REG 0x4
47 #define BNO055_SW_REV_MSB_REG 0x5
48 #define BNO055_ACC_DATA_X_LSB_REG 0x8
49 #define BNO055_ACC_DATA_Y_LSB_REG 0xA
50 #define BNO055_ACC_DATA_Z_LSB_REG 0xC
51 #define BNO055_MAG_DATA_X_LSB_REG 0xE
52 #define BNO055_MAG_DATA_Y_LSB_REG 0x10
53 #define BNO055_MAG_DATA_Z_LSB_REG 0x12
54 #define BNO055_GYR_DATA_X_LSB_REG 0x14
55 #define BNO055_GYR_DATA_Y_LSB_REG 0x16
56 #define BNO055_GYR_DATA_Z_LSB_REG 0x18
57 #define BNO055_EUL_DATA_X_LSB_REG 0x1A
58 #define BNO055_EUL_DATA_Y_LSB_REG 0x1C
59 #define BNO055_EUL_DATA_Z_LSB_REG 0x1E
60 #define BNO055_QUAT_DATA_W_LSB_REG 0x20
61 #define BNO055_LIA_DATA_X_LSB_REG 0x28
62 #define BNO055_LIA_DATA_Y_LSB_REG 0x2A
63 #define BNO055_LIA_DATA_Z_LSB_REG 0x2C
64 #define BNO055_GRAVITY_DATA_X_LSB_REG 0x2E
65 #define BNO055_GRAVITY_DATA_Y_LSB_REG 0x30
66 #define BNO055_GRAVITY_DATA_Z_LSB_REG 0x32
67 #define BNO055_SCAN_CH_COUNT ((BNO055_GRAVITY_DATA_Z_LSB_REG - BNO055_ACC_DATA_X_LSB_REG) / 2)
68 #define BNO055_TEMP_REG 0x34
69 #define BNO055_CALIB_STAT_REG 0x35
70 #define BNO055_CALIB_STAT_MAGN_SHIFT 0
71 #define BNO055_CALIB_STAT_ACCEL_SHIFT 2
72 #define BNO055_CALIB_STAT_GYRO_SHIFT 4
73 #define BNO055_CALIB_STAT_SYS_SHIFT 6
74 #define BNO055_SYS_ERR_REG 0x3A
75 #define BNO055_POWER_MODE_REG 0x3E
76 #define BNO055_POWER_MODE_NORMAL 0
77 #define BNO055_SYS_TRIGGER_REG 0x3F
78 #define BNO055_SYS_TRIGGER_RST_SYS BIT(5)
79 #define BNO055_SYS_TRIGGER_CLK_SEL BIT(7)
80 #define BNO055_OPR_MODE_REG 0x3D
81 #define BNO055_OPR_MODE_CONFIG 0x0
82 #define BNO055_OPR_MODE_AMG 0x7
83 #define BNO055_OPR_MODE_FUSION_FMC_OFF 0xB
84 #define BNO055_OPR_MODE_FUSION 0xC
85 #define BNO055_UNIT_SEL_REG 0x3B
86 /* Android orientation mode means: pitch value decreases turning clockwise */
87 #define BNO055_UNIT_SEL_ANDROID BIT(7)
88 #define BNO055_UNIT_SEL_GYR_RPS BIT(1)
89 #define BNO055_CALDATA_START 0x55
90 #define BNO055_CALDATA_END 0x6A
91 #define BNO055_CALDATA_LEN 22
94 * The difference in address between the register that contains the
95 * value and the register that contains the offset. This applies for
96 * accel, gyro and magn channels.
98 #define BNO055_REG_OFFSET_ADDR 0x4D
100 /* page 1 registers */
101 #define BNO055_PG1(x) ((x) | 0x80)
102 #define BNO055_ACC_CONFIG_REG BNO055_PG1(0x8)
103 #define BNO055_ACC_CONFIG_LPF_MASK GENMASK(4, 2)
104 #define BNO055_ACC_CONFIG_RANGE_MASK GENMASK(1, 0)
105 #define BNO055_MAG_CONFIG_REG BNO055_PG1(0x9)
106 #define BNO055_MAG_CONFIG_HIGHACCURACY 0x18
107 #define BNO055_MAG_CONFIG_ODR_MASK GENMASK(2, 0)
108 #define BNO055_GYR_CONFIG_REG BNO055_PG1(0xA)
109 #define BNO055_GYR_CONFIG_RANGE_MASK GENMASK(2, 0)
110 #define BNO055_GYR_CONFIG_LPF_MASK GENMASK(5, 3)
111 #define BNO055_GYR_AM_SET_REG BNO055_PG1(0x1F)
112 #define BNO055_UID_LOWER_REG BNO055_PG1(0x50)
113 #define BNO055_UID_HIGHER_REG BNO055_PG1(0x5F)
114 #define BNO055_UID_LEN 16
116 struct bno055_sysfs_attr
{
124 static int bno055_acc_lpf_vals
[] = {
125 7, 810000, 15, 630000, 31, 250000, 62, 500000,
126 125, 0, 250, 0, 500, 0, 1000, 0,
129 static struct bno055_sysfs_attr bno055_acc_lpf
= {
130 .vals
= bno055_acc_lpf_vals
,
131 .len
= ARRAY_SIZE(bno055_acc_lpf_vals
),
132 .fusion_vals
= (int[]){62, 500000},
133 .type
= IIO_VAL_INT_PLUS_MICRO
,
136 static int bno055_acc_range_vals
[] = {
138 1962, 3924, 7848, 15696
141 static struct bno055_sysfs_attr bno055_acc_range
= {
142 .vals
= bno055_acc_range_vals
,
143 .len
= ARRAY_SIZE(bno055_acc_range_vals
),
144 .fusion_vals
= (int[]){3924}, /* 4G */
149 * Theoretically the IMU should return data in a given (i.e. fixed) unit
150 * regardless of the range setting. This happens for the accelerometer, but not
151 * for the gyroscope; the gyroscope range setting affects the scale.
152 * This is probably due to this[0] bug.
153 * For this reason we map the internal range setting onto the standard IIO scale
154 * attribute for gyro.
155 * Since the bug[0] may be fixed in future, we check for the IMU FW version and
156 * eventually warn the user.
157 * Currently we just don't care about "range" attributes for gyro.
159 * [0] https://community.bosch-sensortec.com/t5/MEMS-sensors-forum/BNO055-Wrong-sensitivity-resolution-in-datasheet/td-p/10266
163 * dps = hwval * (dps_range/2^15)
164 * rps = hwval * (rps_range/2^15)
165 * = hwval * (dps_range/(2^15 * k))
166 * where k is rad-to-deg factor
168 static int bno055_gyr_scale_vals
[] = {
169 125, 1877467, 250, 1877467, 500, 1877467,
170 1000, 1877467, 2000, 1877467,
173 static struct bno055_sysfs_attr bno055_gyr_scale
= {
174 .vals
= bno055_gyr_scale_vals
,
175 .len
= ARRAY_SIZE(bno055_gyr_scale_vals
),
176 .fusion_vals
= (int[]){1, 900},
177 .hw_xlate
= (int[]){4, 3, 2, 1, 0},
178 .type
= IIO_VAL_FRACTIONAL
,
181 static int bno055_gyr_lpf_vals
[] = {12, 23, 32, 47, 64, 116, 230, 523};
182 static struct bno055_sysfs_attr bno055_gyr_lpf
= {
183 .vals
= bno055_gyr_lpf_vals
,
184 .len
= ARRAY_SIZE(bno055_gyr_lpf_vals
),
185 .fusion_vals
= (int[]){32},
186 .hw_xlate
= (int[]){5, 4, 7, 3, 6, 2, 1, 0},
190 static int bno055_mag_odr_vals
[] = {2, 6, 8, 10, 15, 20, 25, 30};
191 static struct bno055_sysfs_attr bno055_mag_odr
= {
192 .vals
= bno055_mag_odr_vals
,
193 .len
= ARRAY_SIZE(bno055_mag_odr_vals
),
194 .fusion_vals
= (int[]){20},
199 struct regmap
*regmap
;
203 int xfer_burst_break_thr
;
205 u8 uid
[BNO055_UID_LEN
];
206 struct gpio_desc
*reset_gpio
;
209 __le16 chans
[BNO055_SCAN_CH_COUNT
];
210 s64 timestamp
__aligned(8);
212 struct dentry
*debugfs
;
215 static bool bno055_regmap_volatile(struct device
*dev
, unsigned int reg
)
217 /* data and status registers */
218 if (reg
>= BNO055_ACC_DATA_X_LSB_REG
&& reg
<= BNO055_SYS_ERR_REG
)
221 /* when in fusion mode, config is updated by chip */
222 if (reg
== BNO055_MAG_CONFIG_REG
||
223 reg
== BNO055_ACC_CONFIG_REG
||
224 reg
== BNO055_GYR_CONFIG_REG
)
227 /* calibration data may be updated by the IMU */
228 if (reg
>= BNO055_CALDATA_START
&& reg
<= BNO055_CALDATA_END
)
234 static bool bno055_regmap_readable(struct device
*dev
, unsigned int reg
)
236 /* unnamed PG0 reserved areas */
237 if ((reg
< BNO055_PG1(0) && reg
> BNO055_CALDATA_END
) ||
241 /* unnamed PG1 reserved areas */
242 if (reg
> BNO055_PG1(BNO055_UID_HIGHER_REG
) ||
243 (reg
< BNO055_PG1(BNO055_UID_LOWER_REG
) && reg
> BNO055_PG1(BNO055_GYR_AM_SET_REG
)) ||
244 reg
== BNO055_PG1(0xE) ||
245 (reg
< BNO055_PG1(BNO055_PAGESEL_REG
) && reg
>= BNO055_PG1(0x0)))
250 static bool bno055_regmap_writeable(struct device
*dev
, unsigned int reg
)
253 * Unreadable registers are indeed reserved; there are no WO regs
254 * (except for a single bit in SYS_TRIGGER register)
256 if (!bno055_regmap_readable(dev
, reg
))
259 /* data and status registers */
260 if (reg
>= BNO055_ACC_DATA_X_LSB_REG
&& reg
<= BNO055_SYS_ERR_REG
)
264 if (reg
< BNO055_PAGESEL_REG
||
265 (reg
<= BNO055_UID_HIGHER_REG
&& reg
>= BNO055_UID_LOWER_REG
))
271 static const struct regmap_range_cfg bno055_regmap_ranges
[] = {
274 .range_max
= 0x7f * 2,
275 .selector_reg
= BNO055_PAGESEL_REG
,
276 .selector_mask
= GENMASK(7, 0),
283 const struct regmap_config bno055_regmap_config
= {
287 .ranges
= bno055_regmap_ranges
,
289 .volatile_reg
= bno055_regmap_volatile
,
290 .max_register
= 0x80 * 2,
291 .writeable_reg
= bno055_regmap_writeable
,
292 .readable_reg
= bno055_regmap_readable
,
293 .cache_type
= REGCACHE_RBTREE
,
295 EXPORT_SYMBOL_NS_GPL(bno055_regmap_config
, "IIO_BNO055");
297 /* must be called in configuration mode */
298 static int bno055_calibration_load(struct bno055_priv
*priv
, const u8
*data
, int len
)
300 if (len
!= BNO055_CALDATA_LEN
) {
301 dev_dbg(priv
->dev
, "Invalid calibration file size %d (expected %d)",
302 len
, BNO055_CALDATA_LEN
);
306 dev_dbg(priv
->dev
, "loading cal data: %*ph", BNO055_CALDATA_LEN
, data
);
307 return regmap_bulk_write(priv
->regmap
, BNO055_CALDATA_START
,
308 data
, BNO055_CALDATA_LEN
);
311 static int bno055_operation_mode_do_set(struct bno055_priv
*priv
,
316 ret
= regmap_write(priv
->regmap
, BNO055_OPR_MODE_REG
,
321 /* Following datasheet specifications: sensor takes 7mS up to 19 mS to switch mode */
327 static int bno055_system_reset(struct bno055_priv
*priv
)
331 if (priv
->reset_gpio
) {
332 gpiod_set_value_cansleep(priv
->reset_gpio
, 0);
333 usleep_range(5000, 10000);
334 gpiod_set_value_cansleep(priv
->reset_gpio
, 1);
335 } else if (priv
->sw_reset
) {
336 ret
= regmap_write(priv
->regmap
, BNO055_SYS_TRIGGER_REG
,
337 BNO055_SYS_TRIGGER_RST_SYS
);
344 regcache_drop_region(priv
->regmap
, 0x0, 0xff);
345 usleep_range(650000, 700000);
350 static int bno055_init(struct bno055_priv
*priv
, const u8
*caldata
, int len
)
354 ret
= bno055_operation_mode_do_set(priv
, BNO055_OPR_MODE_CONFIG
);
358 ret
= regmap_write(priv
->regmap
, BNO055_POWER_MODE_REG
,
359 BNO055_POWER_MODE_NORMAL
);
363 ret
= regmap_write(priv
->regmap
, BNO055_SYS_TRIGGER_REG
,
364 priv
->clk
? BNO055_SYS_TRIGGER_CLK_SEL
: 0);
368 /* use standard SI units */
369 ret
= regmap_write(priv
->regmap
, BNO055_UNIT_SEL_REG
,
370 BNO055_UNIT_SEL_ANDROID
| BNO055_UNIT_SEL_GYR_RPS
);
375 ret
= bno055_calibration_load(priv
, caldata
, len
);
377 dev_warn(priv
->dev
, "failed to load calibration data with error %d\n",
384 static ssize_t
bno055_operation_mode_set(struct bno055_priv
*priv
,
387 u8 caldata
[BNO055_CALDATA_LEN
];
390 mutex_lock(&priv
->lock
);
392 ret
= bno055_operation_mode_do_set(priv
, BNO055_OPR_MODE_CONFIG
);
396 if (operation_mode
== BNO055_OPR_MODE_FUSION
||
397 operation_mode
== BNO055_OPR_MODE_FUSION_FMC_OFF
) {
398 /* for entering fusion mode, reset the chip to clear the algo state */
399 ret
= regmap_bulk_read(priv
->regmap
, BNO055_CALDATA_START
, caldata
,
404 ret
= bno055_system_reset(priv
);
408 ret
= bno055_init(priv
, caldata
, BNO055_CALDATA_LEN
);
413 ret
= bno055_operation_mode_do_set(priv
, operation_mode
);
417 priv
->operation_mode
= operation_mode
;
420 mutex_unlock(&priv
->lock
);
424 static void bno055_uninit(void *arg
)
426 struct bno055_priv
*priv
= arg
;
429 bno055_operation_mode_do_set(priv
, BNO055_OPR_MODE_CONFIG
);
432 #define BNO055_CHANNEL(_type, _axis, _index, _address, _sep, _sh, _avail) { \
433 .address = _address, \
436 .channel2 = IIO_MOD_##_axis, \
437 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | (_sep), \
438 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | (_sh), \
439 .info_mask_shared_by_type_available = _avail, \
440 .scan_index = _index, \
445 .endianness = IIO_LE, \
446 .repeat = IIO_MOD_##_axis == IIO_MOD_QUATERNION ? 4 : 0, \
450 /* scan indexes follow DATA register order */
451 enum bno055_scan_axis
{
464 BNO055_SCAN_QUATERNION
,
468 BNO055_SCAN_GRAVITY_X
,
469 BNO055_SCAN_GRAVITY_Y
,
470 BNO055_SCAN_GRAVITY_Z
,
471 BNO055_SCAN_TIMESTAMP
,
475 static const struct iio_chan_spec bno055_channels
[] = {
477 BNO055_CHANNEL(IIO_ACCEL
, X
, BNO055_SCAN_ACCEL_X
,
478 BNO055_ACC_DATA_X_LSB_REG
, BIT(IIO_CHAN_INFO_OFFSET
),
479 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
),
480 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
)),
481 BNO055_CHANNEL(IIO_ACCEL
, Y
, BNO055_SCAN_ACCEL_Y
,
482 BNO055_ACC_DATA_Y_LSB_REG
, BIT(IIO_CHAN_INFO_OFFSET
),
483 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
),
484 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
)),
485 BNO055_CHANNEL(IIO_ACCEL
, Z
, BNO055_SCAN_ACCEL_Z
,
486 BNO055_ACC_DATA_Z_LSB_REG
, BIT(IIO_CHAN_INFO_OFFSET
),
487 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
),
488 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
)),
490 BNO055_CHANNEL(IIO_ANGL_VEL
, X
, BNO055_SCAN_GYRO_X
,
491 BNO055_GYR_DATA_X_LSB_REG
, BIT(IIO_CHAN_INFO_OFFSET
),
492 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
),
493 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
) |
494 BIT(IIO_CHAN_INFO_SCALE
)),
495 BNO055_CHANNEL(IIO_ANGL_VEL
, Y
, BNO055_SCAN_GYRO_Y
,
496 BNO055_GYR_DATA_Y_LSB_REG
, BIT(IIO_CHAN_INFO_OFFSET
),
497 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
),
498 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
) |
499 BIT(IIO_CHAN_INFO_SCALE
)),
500 BNO055_CHANNEL(IIO_ANGL_VEL
, Z
, BNO055_SCAN_GYRO_Z
,
501 BNO055_GYR_DATA_Z_LSB_REG
, BIT(IIO_CHAN_INFO_OFFSET
),
502 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
),
503 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
) |
504 BIT(IIO_CHAN_INFO_SCALE
)),
506 BNO055_CHANNEL(IIO_MAGN
, X
, BNO055_SCAN_MAGN_X
,
507 BNO055_MAG_DATA_X_LSB_REG
, BIT(IIO_CHAN_INFO_OFFSET
),
508 BIT(IIO_CHAN_INFO_SAMP_FREQ
), BIT(IIO_CHAN_INFO_SAMP_FREQ
)),
509 BNO055_CHANNEL(IIO_MAGN
, Y
, BNO055_SCAN_MAGN_Y
,
510 BNO055_MAG_DATA_Y_LSB_REG
, BIT(IIO_CHAN_INFO_OFFSET
),
511 BIT(IIO_CHAN_INFO_SAMP_FREQ
), BIT(IIO_CHAN_INFO_SAMP_FREQ
)),
512 BNO055_CHANNEL(IIO_MAGN
, Z
, BNO055_SCAN_MAGN_Z
,
513 BNO055_MAG_DATA_Z_LSB_REG
, BIT(IIO_CHAN_INFO_OFFSET
),
514 BIT(IIO_CHAN_INFO_SAMP_FREQ
), BIT(IIO_CHAN_INFO_SAMP_FREQ
)),
516 BNO055_CHANNEL(IIO_ROT
, YAW
, BNO055_SCAN_YAW
,
517 BNO055_EUL_DATA_X_LSB_REG
, 0, 0, 0),
518 BNO055_CHANNEL(IIO_ROT
, ROLL
, BNO055_SCAN_ROLL
,
519 BNO055_EUL_DATA_Y_LSB_REG
, 0, 0, 0),
520 BNO055_CHANNEL(IIO_ROT
, PITCH
, BNO055_SCAN_PITCH
,
521 BNO055_EUL_DATA_Z_LSB_REG
, 0, 0, 0),
523 BNO055_CHANNEL(IIO_ROT
, QUATERNION
, BNO055_SCAN_QUATERNION
,
524 BNO055_QUAT_DATA_W_LSB_REG
, 0, 0, 0),
526 /* linear acceleration */
527 BNO055_CHANNEL(IIO_ACCEL
, LINEAR_X
, BNO055_SCAN_LIA_X
,
528 BNO055_LIA_DATA_X_LSB_REG
, 0, 0, 0),
529 BNO055_CHANNEL(IIO_ACCEL
, LINEAR_Y
, BNO055_SCAN_LIA_Y
,
530 BNO055_LIA_DATA_Y_LSB_REG
, 0, 0, 0),
531 BNO055_CHANNEL(IIO_ACCEL
, LINEAR_Z
, BNO055_SCAN_LIA_Z
,
532 BNO055_LIA_DATA_Z_LSB_REG
, 0, 0, 0),
535 BNO055_CHANNEL(IIO_GRAVITY
, X
, BNO055_SCAN_GRAVITY_X
,
536 BNO055_GRAVITY_DATA_X_LSB_REG
, 0, 0, 0),
537 BNO055_CHANNEL(IIO_GRAVITY
, Y
, BNO055_SCAN_GRAVITY_Y
,
538 BNO055_GRAVITY_DATA_Y_LSB_REG
, 0, 0, 0),
539 BNO055_CHANNEL(IIO_GRAVITY
, Z
, BNO055_SCAN_GRAVITY_Z
,
540 BNO055_GRAVITY_DATA_Z_LSB_REG
, 0, 0, 0),
544 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
547 IIO_CHAN_SOFT_TIMESTAMP(BNO055_SCAN_TIMESTAMP
),
550 static int bno055_get_regmask(struct bno055_priv
*priv
, int *val
, int *val2
,
551 int reg
, int mask
, struct bno055_sysfs_attr
*attr
)
553 const int shift
= __ffs(mask
);
558 ret
= regmap_read(priv
->regmap
, reg
, &hwval
);
562 idx
= (hwval
& mask
) >> shift
;
564 for (i
= 0; i
< attr
->len
; i
++)
565 if (attr
->hw_xlate
[i
] == idx
) {
569 if (attr
->type
== IIO_VAL_INT
) {
570 *val
= attr
->vals
[idx
];
571 } else { /* IIO_VAL_INT_PLUS_MICRO or IIO_VAL_FRACTIONAL */
572 *val
= attr
->vals
[idx
* 2];
573 *val2
= attr
->vals
[idx
* 2 + 1];
579 static int bno055_set_regmask(struct bno055_priv
*priv
, int val
, int val2
,
580 int reg
, int mask
, struct bno055_sysfs_attr
*attr
)
582 const int shift
= __ffs(mask
);
594 * The closest value the HW supports is only one in fusion mode,
595 * and it is autoselected, so don't do anything, just return OK,
596 * as the closest possible value has been (virtually) selected
598 if (priv
->operation_mode
!= BNO055_OPR_MODE_AMG
)
604 * We always get a request in INT_PLUS_MICRO, but we
605 * take care of the micro part only when we really have
606 * non-integer tables. This prevents 32-bit overflow with
607 * larger integers contained in integer tables.
610 if (attr
->type
!= IIO_VAL_INT
) {
612 req_val
= min(val
, 2147) * 1000000 + val2
;
616 for (i
= 0; i
< len
; i
++) {
617 switch (attr
->type
) {
619 tbl_val
= attr
->vals
[i
];
621 case IIO_VAL_INT_PLUS_MICRO
:
622 WARN_ON(attr
->vals
[i
* 2] > 2147);
623 tbl_val
= attr
->vals
[i
* 2] * 1000000 +
624 attr
->vals
[i
* 2 + 1];
626 case IIO_VAL_FRACTIONAL
:
627 WARN_ON(attr
->vals
[i
* 2] > 4294);
628 tbl_val
= attr
->vals
[i
* 2] * 1000000 /
629 attr
->vals
[i
* 2 + 1];
634 delta
= abs(tbl_val
- req_val
);
635 if (first
|| delta
< best_delta
) {
643 hwval
= attr
->hw_xlate
[hwval
];
645 ret
= bno055_operation_mode_do_set(priv
, BNO055_OPR_MODE_CONFIG
);
649 ret
= regmap_update_bits(priv
->regmap
, reg
, mask
, hwval
<< shift
);
653 return bno055_operation_mode_do_set(priv
, BNO055_OPR_MODE_AMG
);
656 static int bno055_read_simple_chan(struct iio_dev
*indio_dev
,
657 struct iio_chan_spec
const *chan
,
658 int *val
, int *val2
, long mask
)
660 struct bno055_priv
*priv
= iio_priv(indio_dev
);
665 case IIO_CHAN_INFO_RAW
:
666 ret
= regmap_bulk_read(priv
->regmap
, chan
->address
,
667 &raw_val
, sizeof(raw_val
));
670 *val
= sign_extend32(le16_to_cpu(raw_val
), 15);
672 case IIO_CHAN_INFO_OFFSET
:
673 if (priv
->operation_mode
!= BNO055_OPR_MODE_AMG
) {
676 ret
= regmap_bulk_read(priv
->regmap
,
678 BNO055_REG_OFFSET_ADDR
,
679 &raw_val
, sizeof(raw_val
));
683 * IMU reports sensor offsets; IIO wants correction
684 * offsets, thus we need the 'minus' here.
686 *val
= -sign_extend32(le16_to_cpu(raw_val
), 15);
689 case IIO_CHAN_INFO_SCALE
:
691 switch (chan
->type
) {
693 /* Table 3-35: 1 m/s^2 = 100 LSB */
695 /* Table 3-17: 1 m/s^2 = 100 LSB */
700 * Table 3-19: 1 uT = 16 LSB. But we need
701 * Gauss: 1G = 0.1 uT.
707 * Table 3-22: 1 Rps = 900 LSB
708 * .. but this is not exactly true. See comment at the
709 * beginning of this file.
711 if (priv
->operation_mode
!= BNO055_OPR_MODE_AMG
) {
712 *val
= bno055_gyr_scale
.fusion_vals
[0];
713 *val2
= bno055_gyr_scale
.fusion_vals
[1];
714 return IIO_VAL_FRACTIONAL
;
717 return bno055_get_regmask(priv
, val
, val2
,
718 BNO055_GYR_CONFIG_REG
,
719 BNO055_GYR_CONFIG_RANGE_MASK
,
723 /* Table 3-28: 1 degree = 16 LSB */
729 return IIO_VAL_FRACTIONAL
;
731 case IIO_CHAN_INFO_SAMP_FREQ
:
732 if (chan
->type
!= IIO_MAGN
)
735 return bno055_get_regmask(priv
, val
, val2
,
736 BNO055_MAG_CONFIG_REG
,
737 BNO055_MAG_CONFIG_ODR_MASK
,
740 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
741 switch (chan
->type
) {
743 return bno055_get_regmask(priv
, val
, val2
,
744 BNO055_GYR_CONFIG_REG
,
745 BNO055_GYR_CONFIG_LPF_MASK
,
748 return bno055_get_regmask(priv
, val
, val2
,
749 BNO055_ACC_CONFIG_REG
,
750 BNO055_ACC_CONFIG_LPF_MASK
,
761 static int bno055_sysfs_attr_avail(struct bno055_priv
*priv
, struct bno055_sysfs_attr
*attr
,
762 const int **vals
, int *length
)
764 if (priv
->operation_mode
!= BNO055_OPR_MODE_AMG
) {
765 /* locked when fusion enabled */
766 *vals
= attr
->fusion_vals
;
767 if (attr
->type
== IIO_VAL_INT
)
770 *length
= 2; /* IIO_VAL_INT_PLUS_MICRO or IIO_VAL_FRACTIONAL*/
779 static int bno055_read_avail(struct iio_dev
*indio_dev
,
780 struct iio_chan_spec
const *chan
,
781 const int **vals
, int *type
, int *length
,
784 struct bno055_priv
*priv
= iio_priv(indio_dev
);
787 case IIO_CHAN_INFO_SCALE
:
788 switch (chan
->type
) {
790 *type
= bno055_sysfs_attr_avail(priv
, &bno055_gyr_scale
,
792 return IIO_AVAIL_LIST
;
796 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
797 switch (chan
->type
) {
799 *type
= bno055_sysfs_attr_avail(priv
, &bno055_gyr_lpf
,
801 return IIO_AVAIL_LIST
;
803 *type
= bno055_sysfs_attr_avail(priv
, &bno055_acc_lpf
,
805 return IIO_AVAIL_LIST
;
811 case IIO_CHAN_INFO_SAMP_FREQ
:
812 switch (chan
->type
) {
814 *type
= bno055_sysfs_attr_avail(priv
, &bno055_mag_odr
,
816 return IIO_AVAIL_LIST
;
825 static int bno055_read_temp_chan(struct iio_dev
*indio_dev
, int *val
)
827 struct bno055_priv
*priv
= iio_priv(indio_dev
);
828 unsigned int raw_val
;
831 ret
= regmap_read(priv
->regmap
, BNO055_TEMP_REG
, &raw_val
);
836 * Tables 3-36 and 3-37: one byte of priv, signed, 1 LSB = 1C.
839 *val
= raw_val
* 1000;
844 static int bno055_read_quaternion(struct iio_dev
*indio_dev
,
845 struct iio_chan_spec
const *chan
,
846 int size
, int *vals
, int *val_len
,
849 struct bno055_priv
*priv
= iio_priv(indio_dev
);
854 case IIO_CHAN_INFO_RAW
:
857 ret
= regmap_bulk_read(priv
->regmap
,
858 BNO055_QUAT_DATA_W_LSB_REG
,
859 raw_vals
, sizeof(raw_vals
));
862 for (i
= 0; i
< 4; i
++)
863 vals
[i
] = sign_extend32(le16_to_cpu(raw_vals
[i
]), 15);
865 return IIO_VAL_INT_MULTIPLE
;
866 case IIO_CHAN_INFO_SCALE
:
867 /* Table 3-31: 1 quaternion = 2^14 LSB */
872 return IIO_VAL_FRACTIONAL_LOG2
;
878 static bool bno055_is_chan_readable(struct iio_dev
*indio_dev
,
879 struct iio_chan_spec
const *chan
)
881 struct bno055_priv
*priv
= iio_priv(indio_dev
);
883 if (priv
->operation_mode
!= BNO055_OPR_MODE_AMG
)
886 switch (chan
->type
) {
891 if (chan
->channel2
== IIO_MOD_LINEAR_X
||
892 chan
->channel2
== IIO_MOD_LINEAR_Y
||
893 chan
->channel2
== IIO_MOD_LINEAR_Z
)
901 static int _bno055_read_raw_multi(struct iio_dev
*indio_dev
,
902 struct iio_chan_spec
const *chan
,
903 int size
, int *vals
, int *val_len
,
906 if (!bno055_is_chan_readable(indio_dev
, chan
))
909 switch (chan
->type
) {
917 return bno055_read_simple_chan(indio_dev
, chan
,
922 return bno055_read_temp_chan(indio_dev
, &vals
[0]);
925 * Rotation is exposed as either a quaternion or three
928 if (chan
->channel2
== IIO_MOD_QUATERNION
)
929 return bno055_read_quaternion(indio_dev
, chan
,
935 return bno055_read_simple_chan(indio_dev
, chan
,
943 static int bno055_read_raw_multi(struct iio_dev
*indio_dev
,
944 struct iio_chan_spec
const *chan
,
945 int size
, int *vals
, int *val_len
,
948 struct bno055_priv
*priv
= iio_priv(indio_dev
);
951 mutex_lock(&priv
->lock
);
952 ret
= _bno055_read_raw_multi(indio_dev
, chan
, size
,
953 vals
, val_len
, mask
);
954 mutex_unlock(&priv
->lock
);
958 static int _bno055_write_raw(struct iio_dev
*iio_dev
,
959 struct iio_chan_spec
const *chan
,
960 int val
, int val2
, long mask
)
962 struct bno055_priv
*priv
= iio_priv(iio_dev
);
964 switch (chan
->type
) {
967 case IIO_CHAN_INFO_SAMP_FREQ
:
968 return bno055_set_regmask(priv
, val
, val2
,
969 BNO055_MAG_CONFIG_REG
,
970 BNO055_MAG_CONFIG_ODR_MASK
,
977 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
978 return bno055_set_regmask(priv
, val
, val2
,
979 BNO055_ACC_CONFIG_REG
,
980 BNO055_ACC_CONFIG_LPF_MASK
,
988 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
989 return bno055_set_regmask(priv
, val
, val2
,
990 BNO055_GYR_CONFIG_REG
,
991 BNO055_GYR_CONFIG_LPF_MASK
,
993 case IIO_CHAN_INFO_SCALE
:
994 return bno055_set_regmask(priv
, val
, val2
,
995 BNO055_GYR_CONFIG_REG
,
996 BNO055_GYR_CONFIG_RANGE_MASK
,
1006 static int bno055_write_raw(struct iio_dev
*iio_dev
,
1007 struct iio_chan_spec
const *chan
,
1008 int val
, int val2
, long mask
)
1010 struct bno055_priv
*priv
= iio_priv(iio_dev
);
1013 mutex_lock(&priv
->lock
);
1014 ret
= _bno055_write_raw(iio_dev
, chan
, val
, val2
, mask
);
1015 mutex_unlock(&priv
->lock
);
1020 static ssize_t
in_accel_range_raw_available_show(struct device
*dev
,
1021 struct device_attribute
*attr
,
1024 struct bno055_priv
*priv
= iio_priv(dev_to_iio_dev(dev
));
1028 if (priv
->operation_mode
!= BNO055_OPR_MODE_AMG
)
1029 return sysfs_emit(buf
, "%d\n", bno055_acc_range
.fusion_vals
[0]);
1031 for (i
= 0; i
< bno055_acc_range
.len
; i
++)
1032 len
+= sysfs_emit_at(buf
, len
, "%d ", bno055_acc_range
.vals
[i
]);
1033 buf
[len
- 1] = '\n';
1038 static ssize_t
fusion_enable_show(struct device
*dev
,
1039 struct device_attribute
*attr
,
1042 struct bno055_priv
*priv
= iio_priv(dev_to_iio_dev(dev
));
1044 return sysfs_emit(buf
, "%d\n",
1045 priv
->operation_mode
!= BNO055_OPR_MODE_AMG
);
1048 static ssize_t
fusion_enable_store(struct device
*dev
,
1049 struct device_attribute
*attr
,
1050 const char *buf
, size_t len
)
1052 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1053 struct bno055_priv
*priv
= iio_priv(indio_dev
);
1057 if (indio_dev
->active_scan_mask
&&
1058 !bitmap_empty(indio_dev
->active_scan_mask
, _BNO055_SCAN_MAX
))
1061 ret
= kstrtobool(buf
, &en
);
1066 return bno055_operation_mode_set(priv
, BNO055_OPR_MODE_AMG
) ?: len
;
1069 * Coming from AMG means the FMC was off, just switch to fusion but
1070 * don't change anything that doesn't belong to us (i.e let FMC stay off).
1071 * Coming from any other fusion mode means we don't need to do anything.
1073 if (priv
->operation_mode
== BNO055_OPR_MODE_AMG
)
1074 return bno055_operation_mode_set(priv
, BNO055_OPR_MODE_FUSION_FMC_OFF
) ?: len
;
1079 static ssize_t
in_magn_calibration_fast_enable_show(struct device
*dev
,
1080 struct device_attribute
*attr
,
1083 struct bno055_priv
*priv
= iio_priv(dev_to_iio_dev(dev
));
1085 return sysfs_emit(buf
, "%d\n",
1086 priv
->operation_mode
== BNO055_OPR_MODE_FUSION
);
1089 static ssize_t
in_magn_calibration_fast_enable_store(struct device
*dev
,
1090 struct device_attribute
*attr
,
1091 const char *buf
, size_t len
)
1093 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1094 struct bno055_priv
*priv
= iio_priv(indio_dev
);
1097 if (indio_dev
->active_scan_mask
&&
1098 !bitmap_empty(indio_dev
->active_scan_mask
, _BNO055_SCAN_MAX
))
1101 if (sysfs_streq(buf
, "0")) {
1102 if (priv
->operation_mode
== BNO055_OPR_MODE_FUSION
) {
1103 ret
= bno055_operation_mode_set(priv
, BNO055_OPR_MODE_FUSION_FMC_OFF
);
1108 if (priv
->operation_mode
== BNO055_OPR_MODE_AMG
)
1111 if (priv
->operation_mode
!= BNO055_OPR_MODE_FUSION
) {
1112 ret
= bno055_operation_mode_set(priv
, BNO055_OPR_MODE_FUSION
);
1121 static ssize_t
in_accel_range_raw_show(struct device
*dev
,
1122 struct device_attribute
*attr
,
1125 struct bno055_priv
*priv
= iio_priv(dev_to_iio_dev(dev
));
1129 ret
= bno055_get_regmask(priv
, &val
, NULL
,
1130 BNO055_ACC_CONFIG_REG
,
1131 BNO055_ACC_CONFIG_RANGE_MASK
,
1136 return sysfs_emit(buf
, "%d\n", val
);
1139 static ssize_t
in_accel_range_raw_store(struct device
*dev
,
1140 struct device_attribute
*attr
,
1141 const char *buf
, size_t len
)
1143 struct bno055_priv
*priv
= iio_priv(dev_to_iio_dev(dev
));
1147 ret
= kstrtoul(buf
, 10, &val
);
1151 mutex_lock(&priv
->lock
);
1152 ret
= bno055_set_regmask(priv
, val
, 0,
1153 BNO055_ACC_CONFIG_REG
,
1154 BNO055_ACC_CONFIG_RANGE_MASK
,
1156 mutex_unlock(&priv
->lock
);
1161 static ssize_t
bno055_get_calib_status(struct device
*dev
, char *buf
, int which
)
1163 struct bno055_priv
*priv
= iio_priv(dev_to_iio_dev(dev
));
1168 if (priv
->operation_mode
== BNO055_OPR_MODE_AMG
||
1169 (priv
->operation_mode
== BNO055_OPR_MODE_FUSION_FMC_OFF
&&
1170 which
== BNO055_CALIB_STAT_MAGN_SHIFT
)) {
1173 mutex_lock(&priv
->lock
);
1174 ret
= regmap_read(priv
->regmap
, BNO055_CALIB_STAT_REG
, &val
);
1175 mutex_unlock(&priv
->lock
);
1180 calib
= ((val
>> which
) & GENMASK(1, 0)) + 1;
1183 return sysfs_emit(buf
, "%d\n", calib
);
1186 static ssize_t
serialnumber_show(struct device
*dev
,
1187 struct device_attribute
*attr
,
1190 struct bno055_priv
*priv
= iio_priv(dev_to_iio_dev(dev
));
1192 return sysfs_emit(buf
, "%*ph\n", BNO055_UID_LEN
, priv
->uid
);
1195 static ssize_t
calibration_data_read(struct file
*filp
, struct kobject
*kobj
,
1196 struct bin_attribute
*bin_attr
, char *buf
,
1197 loff_t pos
, size_t count
)
1199 struct bno055_priv
*priv
= iio_priv(dev_to_iio_dev(kobj_to_dev(kobj
)));
1200 u8 data
[BNO055_CALDATA_LEN
];
1204 * Calibration data is volatile; reading it in chunks will possibly
1205 * results in inconsistent data. We require the user to read the whole
1206 * blob in a single chunk
1208 if (count
< BNO055_CALDATA_LEN
|| pos
)
1211 mutex_lock(&priv
->lock
);
1212 ret
= bno055_operation_mode_do_set(priv
, BNO055_OPR_MODE_CONFIG
);
1216 ret
= regmap_bulk_read(priv
->regmap
, BNO055_CALDATA_START
, data
,
1217 BNO055_CALDATA_LEN
);
1221 ret
= bno055_operation_mode_do_set(priv
, priv
->operation_mode
);
1225 memcpy(buf
, data
, BNO055_CALDATA_LEN
);
1227 ret
= BNO055_CALDATA_LEN
;
1229 mutex_unlock(&priv
->lock
);
1233 static ssize_t
sys_calibration_auto_status_show(struct device
*dev
,
1234 struct device_attribute
*a
,
1237 return bno055_get_calib_status(dev
, buf
, BNO055_CALIB_STAT_SYS_SHIFT
);
1240 static ssize_t
in_accel_calibration_auto_status_show(struct device
*dev
,
1241 struct device_attribute
*a
,
1244 return bno055_get_calib_status(dev
, buf
, BNO055_CALIB_STAT_ACCEL_SHIFT
);
1247 static ssize_t
in_gyro_calibration_auto_status_show(struct device
*dev
,
1248 struct device_attribute
*a
,
1251 return bno055_get_calib_status(dev
, buf
, BNO055_CALIB_STAT_GYRO_SHIFT
);
1254 static ssize_t
in_magn_calibration_auto_status_show(struct device
*dev
,
1255 struct device_attribute
*a
,
1258 return bno055_get_calib_status(dev
, buf
, BNO055_CALIB_STAT_MAGN_SHIFT
);
1261 static int bno055_debugfs_reg_access(struct iio_dev
*iio_dev
, unsigned int reg
,
1262 unsigned int writeval
, unsigned int *readval
)
1264 struct bno055_priv
*priv
= iio_priv(iio_dev
);
1267 return regmap_read(priv
->regmap
, reg
, readval
);
1269 return regmap_write(priv
->regmap
, reg
, writeval
);
1272 static ssize_t
bno055_show_fw_version(struct file
*file
, char __user
*userbuf
,
1273 size_t count
, loff_t
*ppos
)
1275 struct bno055_priv
*priv
= file
->private_data
;
1280 ret
= regmap_read(priv
->regmap
, BNO055_SW_REV_LSB_REG
, &rev
);
1284 ret
= regmap_read(priv
->regmap
, BNO055_SW_REV_MSB_REG
, &ver
);
1288 buf
= kasprintf(GFP_KERNEL
, "ver: 0x%x, rev: 0x%x\n", ver
, rev
);
1292 ret
= simple_read_from_buffer(userbuf
, count
, ppos
, buf
, strlen(buf
));
1298 static const struct file_operations bno055_fw_version_ops
= {
1299 .open
= simple_open
,
1300 .read
= bno055_show_fw_version
,
1301 .llseek
= default_llseek
,
1302 .owner
= THIS_MODULE
,
1305 static void bno055_debugfs_remove(void *_priv
)
1307 struct bno055_priv
*priv
= _priv
;
1309 debugfs_remove(priv
->debugfs
);
1310 priv
->debugfs
= NULL
;
1313 static void bno055_debugfs_init(struct iio_dev
*iio_dev
)
1315 struct bno055_priv
*priv
= iio_priv(iio_dev
);
1317 priv
->debugfs
= debugfs_create_file("firmware_version", 0400,
1318 iio_get_debugfs_dentry(iio_dev
),
1319 priv
, &bno055_fw_version_ops
);
1320 if (!IS_ERR(priv
->debugfs
))
1321 devm_add_action_or_reset(priv
->dev
, bno055_debugfs_remove
,
1323 if (IS_ERR_OR_NULL(priv
->debugfs
))
1324 dev_warn(priv
->dev
, "failed to setup debugfs");
1327 static IIO_DEVICE_ATTR_RW(fusion_enable
, 0);
1328 static IIO_DEVICE_ATTR_RW(in_magn_calibration_fast_enable
, 0);
1329 static IIO_DEVICE_ATTR_RW(in_accel_range_raw
, 0);
1331 static IIO_DEVICE_ATTR_RO(in_accel_range_raw_available
, 0);
1332 static IIO_DEVICE_ATTR_RO(sys_calibration_auto_status
, 0);
1333 static IIO_DEVICE_ATTR_RO(in_accel_calibration_auto_status
, 0);
1334 static IIO_DEVICE_ATTR_RO(in_gyro_calibration_auto_status
, 0);
1335 static IIO_DEVICE_ATTR_RO(in_magn_calibration_auto_status
, 0);
1336 static IIO_DEVICE_ATTR_RO(serialnumber
, 0);
1338 static struct attribute
*bno055_attrs
[] = {
1339 &iio_dev_attr_in_accel_range_raw_available
.dev_attr
.attr
,
1340 &iio_dev_attr_in_accel_range_raw
.dev_attr
.attr
,
1341 &iio_dev_attr_fusion_enable
.dev_attr
.attr
,
1342 &iio_dev_attr_in_magn_calibration_fast_enable
.dev_attr
.attr
,
1343 &iio_dev_attr_sys_calibration_auto_status
.dev_attr
.attr
,
1344 &iio_dev_attr_in_accel_calibration_auto_status
.dev_attr
.attr
,
1345 &iio_dev_attr_in_gyro_calibration_auto_status
.dev_attr
.attr
,
1346 &iio_dev_attr_in_magn_calibration_auto_status
.dev_attr
.attr
,
1347 &iio_dev_attr_serialnumber
.dev_attr
.attr
,
1351 static BIN_ATTR_RO(calibration_data
, BNO055_CALDATA_LEN
);
1353 static struct bin_attribute
*bno055_bin_attrs
[] = {
1354 &bin_attr_calibration_data
,
1358 static const struct attribute_group bno055_attrs_group
= {
1359 .attrs
= bno055_attrs
,
1360 .bin_attrs
= bno055_bin_attrs
,
1363 static const struct iio_info bno055_info
= {
1364 .read_raw_multi
= bno055_read_raw_multi
,
1365 .read_avail
= bno055_read_avail
,
1366 .write_raw
= bno055_write_raw
,
1367 .attrs
= &bno055_attrs_group
,
1368 .debugfs_reg_access
= bno055_debugfs_reg_access
,
1372 * Reads len samples from the HW, stores them in buf starting from buf_idx,
1373 * and applies mask to cull (skip) unneeded samples.
1374 * Updates buf_idx incrementing with the number of stored samples.
1375 * Samples from HW are transferred into buf, then in-place copy on buf is
1376 * performed in order to cull samples that need to be skipped.
1377 * This avoids copies of the first samples until we hit the 1st sample to skip,
1378 * and also avoids having an extra bounce buffer.
1379 * buf must be able to contain len elements in spite of how many samples we are
1382 static int bno055_scan_xfer(struct bno055_priv
*priv
,
1383 int start_ch
, int len
, unsigned long mask
,
1384 __le16
*buf
, int *buf_idx
)
1386 const int base
= BNO055_ACC_DATA_X_LSB_REG
;
1387 bool quat_in_read
= false;
1388 int buf_base
= *buf_idx
;
1399 * All channels are made up 1 16-bit sample, except for quaternion that
1400 * is made up 4 16-bit values.
1401 * For us the quaternion CH is just like 4 regular CHs.
1402 * If our read starts past the quaternion make sure to adjust the
1403 * starting offset; if the quaternion is contained in our scan then make
1404 * sure to adjust the read len.
1406 if (start_ch
> BNO055_SCAN_QUATERNION
) {
1408 } else if ((start_ch
<= BNO055_SCAN_QUATERNION
) &&
1409 ((start_ch
+ len
) > BNO055_SCAN_QUATERNION
)) {
1410 quat_in_read
= true;
1414 ret
= regmap_bulk_read(priv
->regmap
,
1415 base
+ start_ch
* sizeof(__le16
),
1417 xfer_len
* sizeof(__le16
));
1421 for_each_set_bit(i
, &mask
, len
) {
1422 if (quat_in_read
&& ((start_ch
+ i
) > BNO055_SCAN_QUATERNION
))
1425 dst
= buf
+ *buf_idx
;
1426 src
= buf
+ buf_base
+ offs_fixup
+ i
;
1428 n
= (start_ch
+ i
== BNO055_SCAN_QUATERNION
) ? 4 : 1;
1431 memcpy(dst
, src
, n
* sizeof(__le16
));
1438 static irqreturn_t
bno055_trigger_handler(int irq
, void *p
)
1440 struct iio_poll_func
*pf
= p
;
1441 struct iio_dev
*iio_dev
= pf
->indio_dev
;
1442 struct bno055_priv
*priv
= iio_priv(iio_dev
);
1443 int xfer_start
, start
, end
, prev_end
;
1451 mutex_lock(&priv
->lock
);
1454 * Walk the bitmap and eventually perform several transfers.
1455 * Bitmap ones-fields that are separated by gaps <= xfer_burst_break_thr
1456 * will be included in same transfer.
1457 * Every time the bitmap contains a gap wider than xfer_burst_break_thr
1458 * then we split the transfer, skipping the gap.
1460 for_each_set_bitrange(start
, end
, iio_dev
->active_scan_mask
,
1461 iio_get_masklength(iio_dev
)) {
1463 * First transfer will start from the beginning of the first
1464 * ones-field in the bitmap
1470 * We found the next ones-field; check whether to
1471 * include it in * the current transfer or not (i.e.
1472 * let's perform the current * transfer and prepare for
1477 * In case the zeros-gap contains the quaternion bit,
1478 * then its length is actually 4 words instead of 1
1479 * (i.e. +3 wrt other channels).
1481 quat_extra_len
= ((start
> BNO055_SCAN_QUATERNION
) &&
1482 (prev_end
<= BNO055_SCAN_QUATERNION
)) ? 3 : 0;
1484 /* If the gap is wider than xfer_burst_break_thr then.. */
1485 thr_hit
= (start
- prev_end
+ quat_extra_len
) >
1486 priv
->xfer_burst_break_thr
;
1489 * .. transfer all the data up to the gap. Then set the
1490 * next transfer start index at right after the gap
1491 * (i.e. at the start of this ones-field).
1494 mask
= *iio_dev
->active_scan_mask
>> xfer_start
;
1495 ret
= bno055_scan_xfer(priv
, xfer_start
,
1496 prev_end
- xfer_start
,
1497 mask
, priv
->buf
.chans
, &buf_idx
);
1508 * We finished walking the bitmap; no more gaps to check for. Just
1509 * perform the current transfer.
1511 mask
= *iio_dev
->active_scan_mask
>> xfer_start
;
1512 ret
= bno055_scan_xfer(priv
, xfer_start
,
1513 prev_end
- xfer_start
,
1514 mask
, priv
->buf
.chans
, &buf_idx
);
1517 iio_push_to_buffers_with_timestamp(iio_dev
,
1518 &priv
->buf
, pf
->timestamp
);
1520 mutex_unlock(&priv
->lock
);
1521 iio_trigger_notify_done(iio_dev
->trig
);
1525 static int bno055_buffer_preenable(struct iio_dev
*indio_dev
)
1527 struct bno055_priv
*priv
= iio_priv(indio_dev
);
1528 const unsigned long fusion_mask
=
1529 BIT(BNO055_SCAN_YAW
) |
1530 BIT(BNO055_SCAN_ROLL
) |
1531 BIT(BNO055_SCAN_PITCH
) |
1532 BIT(BNO055_SCAN_QUATERNION
) |
1533 BIT(BNO055_SCAN_LIA_X
) |
1534 BIT(BNO055_SCAN_LIA_Y
) |
1535 BIT(BNO055_SCAN_LIA_Z
) |
1536 BIT(BNO055_SCAN_GRAVITY_X
) |
1537 BIT(BNO055_SCAN_GRAVITY_Y
) |
1538 BIT(BNO055_SCAN_GRAVITY_Z
);
1540 if (priv
->operation_mode
== BNO055_OPR_MODE_AMG
&&
1541 bitmap_intersects(indio_dev
->active_scan_mask
, &fusion_mask
,
1547 static const struct iio_buffer_setup_ops bno055_buffer_setup_ops
= {
1548 .preenable
= bno055_buffer_preenable
,
1551 int bno055_probe(struct device
*dev
, struct regmap
*regmap
,
1552 int xfer_burst_break_thr
, bool sw_reset
)
1554 const struct firmware
*caldata
= NULL
;
1555 struct bno055_priv
*priv
;
1556 struct iio_dev
*iio_dev
;
1562 iio_dev
= devm_iio_device_alloc(dev
, sizeof(*priv
));
1566 iio_dev
->name
= "bno055";
1567 priv
= iio_priv(iio_dev
);
1568 mutex_init(&priv
->lock
);
1569 priv
->regmap
= regmap
;
1571 priv
->xfer_burst_break_thr
= xfer_burst_break_thr
;
1572 priv
->sw_reset
= sw_reset
;
1574 priv
->reset_gpio
= devm_gpiod_get_optional(dev
, "reset", GPIOD_OUT_LOW
);
1575 if (IS_ERR(priv
->reset_gpio
))
1576 return dev_err_probe(dev
, PTR_ERR(priv
->reset_gpio
), "Failed to get reset GPIO\n");
1578 priv
->clk
= devm_clk_get_optional_enabled(dev
, "clk");
1579 if (IS_ERR(priv
->clk
))
1580 return dev_err_probe(dev
, PTR_ERR(priv
->clk
), "Failed to get CLK\n");
1582 if (priv
->reset_gpio
) {
1583 usleep_range(5000, 10000);
1584 gpiod_set_value_cansleep(priv
->reset_gpio
, 1);
1585 usleep_range(650000, 750000);
1586 } else if (!sw_reset
) {
1587 dev_warn(dev
, "No usable reset method; IMU may be unreliable\n");
1590 ret
= regmap_read(priv
->regmap
, BNO055_CHIP_ID_REG
, &val
);
1594 if (val
!= BNO055_CHIP_ID_MAGIC
)
1595 dev_warn(dev
, "Unrecognized chip ID 0x%x\n", val
);
1598 * In case we haven't a HW reset pin, we can still reset the chip via
1599 * register write. This is probably nonsense in case we can't even
1600 * communicate with the chip or the chip isn't the one we expect (i.e.
1601 * we don't write to unknown chips), so we perform SW reset only after
1602 * chip magic ID check
1604 if (!priv
->reset_gpio
) {
1605 ret
= bno055_system_reset(priv
);
1610 ret
= regmap_read(priv
->regmap
, BNO055_SW_REV_LSB_REG
, &rev
);
1614 ret
= regmap_read(priv
->regmap
, BNO055_SW_REV_MSB_REG
, &ver
);
1619 * The stock FW version contains a bug (see comment at the beginning of
1620 * this file) that causes the anglvel scale to be changed depending on
1621 * the chip range setting. We workaround this, but we don't know what
1622 * other FW versions might do.
1624 if (ver
!= 0x3 || rev
!= 0x11)
1625 dev_warn(dev
, "Untested firmware version. Anglvel scale may not work as expected\n");
1627 ret
= regmap_bulk_read(priv
->regmap
, BNO055_UID_LOWER_REG
,
1628 priv
->uid
, BNO055_UID_LEN
);
1632 /* Sensor calibration data */
1633 fw_name_buf
= kasprintf(GFP_KERNEL
, BNO055_FW_UID_FMT
,
1634 BNO055_UID_LEN
, priv
->uid
);
1638 ret
= request_firmware(&caldata
, fw_name_buf
, dev
);
1641 ret
= request_firmware(&caldata
, BNO055_FW_GENERIC_NAME
, dev
);
1643 dev_notice(dev
, "Calibration file load failed. See instruction in kernel Documentation/iio/bno055.rst\n");
1644 ret
= bno055_init(priv
, NULL
, 0);
1646 ret
= bno055_init(priv
, caldata
->data
, caldata
->size
);
1647 release_firmware(caldata
);
1652 priv
->operation_mode
= BNO055_OPR_MODE_FUSION
;
1653 ret
= bno055_operation_mode_do_set(priv
, priv
->operation_mode
);
1657 ret
= devm_add_action_or_reset(dev
, bno055_uninit
, priv
);
1661 iio_dev
->channels
= bno055_channels
;
1662 iio_dev
->num_channels
= ARRAY_SIZE(bno055_channels
);
1663 iio_dev
->info
= &bno055_info
;
1664 iio_dev
->modes
= INDIO_DIRECT_MODE
;
1666 ret
= devm_iio_triggered_buffer_setup(dev
, iio_dev
,
1667 iio_pollfunc_store_time
,
1668 bno055_trigger_handler
,
1669 &bno055_buffer_setup_ops
);
1673 ret
= devm_iio_device_register(dev
, iio_dev
);
1677 bno055_debugfs_init(iio_dev
);
1681 EXPORT_SYMBOL_NS_GPL(bno055_probe
, "IIO_BNO055");
1683 MODULE_AUTHOR("Andrea Merello <andrea.merello@iit.it>");
1684 MODULE_DESCRIPTION("Bosch BNO055 driver");
1685 MODULE_LICENSE("GPL");