2 * BMI160 - Bosch IMU (accel, gyro plus external magnetometer)
4 * Copyright (c) 2016, Intel Corporation.
6 * This file is subject to the terms and conditions of version 2 of
7 * the GNU General Public License. See the file COPYING in the main
8 * directory of this archive for more details.
10 * IIO core driver for BMI160, with support for I2C/SPI busses
12 * TODO: magnetometer, interrupts, hardware FIFO
14 #include <linux/module.h>
15 #include <linux/regmap.h>
16 #include <linux/acpi.h>
17 #include <linux/delay.h>
19 #include <linux/iio/iio.h>
20 #include <linux/iio/triggered_buffer.h>
21 #include <linux/iio/trigger_consumer.h>
22 #include <linux/iio/buffer.h>
26 #define BMI160_REG_CHIP_ID 0x00
27 #define BMI160_CHIP_ID_VAL 0xD1
29 #define BMI160_REG_PMU_STATUS 0x03
31 /* X axis data low byte address, the rest can be obtained using axis offset */
32 #define BMI160_REG_DATA_MAGN_XOUT_L 0x04
33 #define BMI160_REG_DATA_GYRO_XOUT_L 0x0C
34 #define BMI160_REG_DATA_ACCEL_XOUT_L 0x12
36 #define BMI160_REG_ACCEL_CONFIG 0x40
37 #define BMI160_ACCEL_CONFIG_ODR_MASK GENMASK(3, 0)
38 #define BMI160_ACCEL_CONFIG_BWP_MASK GENMASK(6, 4)
40 #define BMI160_REG_ACCEL_RANGE 0x41
41 #define BMI160_ACCEL_RANGE_2G 0x03
42 #define BMI160_ACCEL_RANGE_4G 0x05
43 #define BMI160_ACCEL_RANGE_8G 0x08
44 #define BMI160_ACCEL_RANGE_16G 0x0C
46 #define BMI160_REG_GYRO_CONFIG 0x42
47 #define BMI160_GYRO_CONFIG_ODR_MASK GENMASK(3, 0)
48 #define BMI160_GYRO_CONFIG_BWP_MASK GENMASK(5, 4)
50 #define BMI160_REG_GYRO_RANGE 0x43
51 #define BMI160_GYRO_RANGE_2000DPS 0x00
52 #define BMI160_GYRO_RANGE_1000DPS 0x01
53 #define BMI160_GYRO_RANGE_500DPS 0x02
54 #define BMI160_GYRO_RANGE_250DPS 0x03
55 #define BMI160_GYRO_RANGE_125DPS 0x04
57 #define BMI160_REG_CMD 0x7E
58 #define BMI160_CMD_ACCEL_PM_SUSPEND 0x10
59 #define BMI160_CMD_ACCEL_PM_NORMAL 0x11
60 #define BMI160_CMD_ACCEL_PM_LOW_POWER 0x12
61 #define BMI160_CMD_GYRO_PM_SUSPEND 0x14
62 #define BMI160_CMD_GYRO_PM_NORMAL 0x15
63 #define BMI160_CMD_GYRO_PM_FAST_STARTUP 0x17
64 #define BMI160_CMD_SOFTRESET 0xB6
66 #define BMI160_REG_DUMMY 0x7F
68 #define BMI160_ACCEL_PMU_MIN_USLEEP 3200
69 #define BMI160_ACCEL_PMU_MAX_USLEEP 3800
70 #define BMI160_GYRO_PMU_MIN_USLEEP 55000
71 #define BMI160_GYRO_PMU_MAX_USLEEP 80000
72 #define BMI160_SOFTRESET_USLEEP 1000
74 #define BMI160_CHANNEL(_type, _axis, _index) { \
77 .channel2 = IIO_MOD_##_axis, \
78 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
79 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
80 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
81 .scan_index = _index, \
86 .endianness = IIO_LE, \
90 /* scan indexes follow DATA register order */
91 enum bmi160_scan_axis
{
92 BMI160_SCAN_EXT_MAGN_X
= 0,
93 BMI160_SCAN_EXT_MAGN_Y
,
94 BMI160_SCAN_EXT_MAGN_Z
,
102 BMI160_SCAN_TIMESTAMP
,
105 enum bmi160_sensor_type
{
109 BMI160_NUM_SENSORS
/* must be last */
113 struct regmap
*regmap
;
116 const struct regmap_config bmi160_regmap_config
= {
120 EXPORT_SYMBOL(bmi160_regmap_config
);
123 u8 data
; /* LSB byte register for X-axis */
132 static struct bmi160_regs bmi160_regs
[] = {
134 .data
= BMI160_REG_DATA_ACCEL_XOUT_L
,
135 .config
= BMI160_REG_ACCEL_CONFIG
,
136 .config_odr_mask
= BMI160_ACCEL_CONFIG_ODR_MASK
,
137 .config_bwp_mask
= BMI160_ACCEL_CONFIG_BWP_MASK
,
138 .range
= BMI160_REG_ACCEL_RANGE
,
139 .pmu_cmd_normal
= BMI160_CMD_ACCEL_PM_NORMAL
,
140 .pmu_cmd_suspend
= BMI160_CMD_ACCEL_PM_SUSPEND
,
143 .data
= BMI160_REG_DATA_GYRO_XOUT_L
,
144 .config
= BMI160_REG_GYRO_CONFIG
,
145 .config_odr_mask
= BMI160_GYRO_CONFIG_ODR_MASK
,
146 .config_bwp_mask
= BMI160_GYRO_CONFIG_BWP_MASK
,
147 .range
= BMI160_REG_GYRO_RANGE
,
148 .pmu_cmd_normal
= BMI160_CMD_GYRO_PM_NORMAL
,
149 .pmu_cmd_suspend
= BMI160_CMD_GYRO_PM_SUSPEND
,
153 struct bmi160_pmu_time
{
158 static struct bmi160_pmu_time bmi160_pmu_time
[] = {
160 .min
= BMI160_ACCEL_PMU_MIN_USLEEP
,
161 .max
= BMI160_ACCEL_PMU_MAX_USLEEP
164 .min
= BMI160_GYRO_PMU_MIN_USLEEP
,
165 .max
= BMI160_GYRO_PMU_MIN_USLEEP
,
169 struct bmi160_scale
{
180 static const struct bmi160_scale bmi160_accel_scale
[] = {
181 { BMI160_ACCEL_RANGE_2G
, 598},
182 { BMI160_ACCEL_RANGE_4G
, 1197},
183 { BMI160_ACCEL_RANGE_8G
, 2394},
184 { BMI160_ACCEL_RANGE_16G
, 4788},
187 static const struct bmi160_scale bmi160_gyro_scale
[] = {
188 { BMI160_GYRO_RANGE_2000DPS
, 1065},
189 { BMI160_GYRO_RANGE_1000DPS
, 532},
190 { BMI160_GYRO_RANGE_500DPS
, 266},
191 { BMI160_GYRO_RANGE_250DPS
, 133},
192 { BMI160_GYRO_RANGE_125DPS
, 66},
195 struct bmi160_scale_item
{
196 const struct bmi160_scale
*tbl
;
200 static const struct bmi160_scale_item bmi160_scale_table
[] = {
202 .tbl
= bmi160_accel_scale
,
203 .num
= ARRAY_SIZE(bmi160_accel_scale
),
206 .tbl
= bmi160_gyro_scale
,
207 .num
= ARRAY_SIZE(bmi160_gyro_scale
),
211 static const struct bmi160_odr bmi160_accel_odr
[] = {
226 static const struct bmi160_odr bmi160_gyro_odr
[] = {
237 struct bmi160_odr_item
{
238 const struct bmi160_odr
*tbl
;
242 static const struct bmi160_odr_item bmi160_odr_table
[] = {
244 .tbl
= bmi160_accel_odr
,
245 .num
= ARRAY_SIZE(bmi160_accel_odr
),
248 .tbl
= bmi160_gyro_odr
,
249 .num
= ARRAY_SIZE(bmi160_gyro_odr
),
253 static const struct iio_chan_spec bmi160_channels
[] = {
254 BMI160_CHANNEL(IIO_ACCEL
, X
, BMI160_SCAN_ACCEL_X
),
255 BMI160_CHANNEL(IIO_ACCEL
, Y
, BMI160_SCAN_ACCEL_Y
),
256 BMI160_CHANNEL(IIO_ACCEL
, Z
, BMI160_SCAN_ACCEL_Z
),
257 BMI160_CHANNEL(IIO_ANGL_VEL
, X
, BMI160_SCAN_GYRO_X
),
258 BMI160_CHANNEL(IIO_ANGL_VEL
, Y
, BMI160_SCAN_GYRO_Y
),
259 BMI160_CHANNEL(IIO_ANGL_VEL
, Z
, BMI160_SCAN_GYRO_Z
),
260 IIO_CHAN_SOFT_TIMESTAMP(BMI160_SCAN_TIMESTAMP
),
263 static enum bmi160_sensor_type
bmi160_to_sensor(enum iio_chan_type iio_type
)
276 int bmi160_set_mode(struct bmi160_data
*data
, enum bmi160_sensor_type t
,
283 cmd
= bmi160_regs
[t
].pmu_cmd_normal
;
285 cmd
= bmi160_regs
[t
].pmu_cmd_suspend
;
287 ret
= regmap_write(data
->regmap
, BMI160_REG_CMD
, cmd
);
291 usleep_range(bmi160_pmu_time
[t
].min
, bmi160_pmu_time
[t
].max
);
297 int bmi160_set_scale(struct bmi160_data
*data
, enum bmi160_sensor_type t
,
302 for (i
= 0; i
< bmi160_scale_table
[t
].num
; i
++)
303 if (bmi160_scale_table
[t
].tbl
[i
].uscale
== uscale
)
306 if (i
== bmi160_scale_table
[t
].num
)
309 return regmap_write(data
->regmap
, bmi160_regs
[t
].range
,
310 bmi160_scale_table
[t
].tbl
[i
].bits
);
314 int bmi160_get_scale(struct bmi160_data
*data
, enum bmi160_sensor_type t
,
319 ret
= regmap_read(data
->regmap
, bmi160_regs
[t
].range
, &val
);
323 for (i
= 0; i
< bmi160_scale_table
[t
].num
; i
++)
324 if (bmi160_scale_table
[t
].tbl
[i
].bits
== val
) {
325 *uscale
= bmi160_scale_table
[t
].tbl
[i
].uscale
;
332 static int bmi160_get_data(struct bmi160_data
*data
, int chan_type
,
338 enum bmi160_sensor_type t
= bmi160_to_sensor(chan_type
);
340 reg
= bmi160_regs
[t
].data
+ (axis
- IIO_MOD_X
) * sizeof(__le16
);
342 ret
= regmap_bulk_read(data
->regmap
, reg
, &sample
, sizeof(__le16
));
346 *val
= sign_extend32(le16_to_cpu(sample
), 15);
352 int bmi160_set_odr(struct bmi160_data
*data
, enum bmi160_sensor_type t
,
357 for (i
= 0; i
< bmi160_odr_table
[t
].num
; i
++)
358 if (bmi160_odr_table
[t
].tbl
[i
].odr
== odr
&&
359 bmi160_odr_table
[t
].tbl
[i
].uodr
== uodr
)
362 if (i
>= bmi160_odr_table
[t
].num
)
365 return regmap_update_bits(data
->regmap
,
366 bmi160_regs
[t
].config
,
367 bmi160_regs
[t
].config_odr_mask
,
368 bmi160_odr_table
[t
].tbl
[i
].bits
);
371 static int bmi160_get_odr(struct bmi160_data
*data
, enum bmi160_sensor_type t
,
376 ret
= regmap_read(data
->regmap
, bmi160_regs
[t
].config
, &val
);
380 val
&= bmi160_regs
[t
].config_odr_mask
;
382 for (i
= 0; i
< bmi160_odr_table
[t
].num
; i
++)
383 if (val
== bmi160_odr_table
[t
].tbl
[i
].bits
)
386 if (i
>= bmi160_odr_table
[t
].num
)
389 *odr
= bmi160_odr_table
[t
].tbl
[i
].odr
;
390 *uodr
= bmi160_odr_table
[t
].tbl
[i
].uodr
;
395 static irqreturn_t
bmi160_trigger_handler(int irq
, void *p
)
397 struct iio_poll_func
*pf
= p
;
398 struct iio_dev
*indio_dev
= pf
->indio_dev
;
399 struct bmi160_data
*data
= iio_priv(indio_dev
);
400 s16 buf
[16]; /* 3 sens x 3 axis x s16 + 3 x s16 pad + 4 x s16 tstamp */
401 int i
, ret
, j
= 0, base
= BMI160_REG_DATA_MAGN_XOUT_L
;
404 for_each_set_bit(i
, indio_dev
->active_scan_mask
,
405 indio_dev
->masklength
) {
406 ret
= regmap_bulk_read(data
->regmap
, base
+ i
* sizeof(__le16
),
407 &sample
, sizeof(__le16
));
413 iio_push_to_buffers_with_timestamp(indio_dev
, buf
, iio_get_time_ns());
415 iio_trigger_notify_done(indio_dev
->trig
);
419 static int bmi160_read_raw(struct iio_dev
*indio_dev
,
420 struct iio_chan_spec
const *chan
,
421 int *val
, int *val2
, long mask
)
424 struct bmi160_data
*data
= iio_priv(indio_dev
);
427 case IIO_CHAN_INFO_RAW
:
428 ret
= bmi160_get_data(data
, chan
->type
, chan
->channel2
, val
);
432 case IIO_CHAN_INFO_SCALE
:
434 ret
= bmi160_get_scale(data
,
435 bmi160_to_sensor(chan
->type
), val2
);
436 return ret
< 0 ? ret
: IIO_VAL_INT_PLUS_MICRO
;
437 case IIO_CHAN_INFO_SAMP_FREQ
:
438 ret
= bmi160_get_odr(data
, bmi160_to_sensor(chan
->type
),
440 return ret
< 0 ? ret
: IIO_VAL_INT_PLUS_MICRO
;
448 static int bmi160_write_raw(struct iio_dev
*indio_dev
,
449 struct iio_chan_spec
const *chan
,
450 int val
, int val2
, long mask
)
452 struct bmi160_data
*data
= iio_priv(indio_dev
);
455 case IIO_CHAN_INFO_SCALE
:
456 return bmi160_set_scale(data
,
457 bmi160_to_sensor(chan
->type
), val2
);
459 case IIO_CHAN_INFO_SAMP_FREQ
:
460 return bmi160_set_odr(data
, bmi160_to_sensor(chan
->type
),
469 static const struct iio_info bmi160_info
= {
470 .driver_module
= THIS_MODULE
,
471 .read_raw
= bmi160_read_raw
,
472 .write_raw
= bmi160_write_raw
,
475 static const char *bmi160_match_acpi_device(struct device
*dev
)
477 const struct acpi_device_id
*id
;
479 id
= acpi_match_device(dev
->driver
->acpi_match_table
, dev
);
483 return dev_name(dev
);
486 static int bmi160_chip_init(struct bmi160_data
*data
, bool use_spi
)
490 struct device
*dev
= regmap_get_device(data
->regmap
);
492 ret
= regmap_write(data
->regmap
, BMI160_REG_CMD
, BMI160_CMD_SOFTRESET
);
496 usleep_range(BMI160_SOFTRESET_USLEEP
, BMI160_SOFTRESET_USLEEP
+ 1);
499 * CS rising edge is needed before starting SPI, so do a dummy read
500 * See Section 3.2.1, page 86 of the datasheet
503 ret
= regmap_read(data
->regmap
, BMI160_REG_DUMMY
, &val
);
508 ret
= regmap_read(data
->regmap
, BMI160_REG_CHIP_ID
, &val
);
510 dev_err(dev
, "Error reading chip id\n");
513 if (val
!= BMI160_CHIP_ID_VAL
) {
514 dev_err(dev
, "Wrong chip id, got %x expected %x\n",
515 val
, BMI160_CHIP_ID_VAL
);
519 ret
= bmi160_set_mode(data
, BMI160_ACCEL
, true);
523 ret
= bmi160_set_mode(data
, BMI160_GYRO
, true);
530 static void bmi160_chip_uninit(struct bmi160_data
*data
)
532 bmi160_set_mode(data
, BMI160_GYRO
, false);
533 bmi160_set_mode(data
, BMI160_ACCEL
, false);
536 int bmi160_core_probe(struct device
*dev
, struct regmap
*regmap
,
537 const char *name
, bool use_spi
)
539 struct iio_dev
*indio_dev
;
540 struct bmi160_data
*data
;
543 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
547 data
= iio_priv(indio_dev
);
548 dev_set_drvdata(dev
, indio_dev
);
549 data
->regmap
= regmap
;
551 ret
= bmi160_chip_init(data
, use_spi
);
555 if (!name
&& ACPI_HANDLE(dev
))
556 name
= bmi160_match_acpi_device(dev
);
558 indio_dev
->dev
.parent
= dev
;
559 indio_dev
->channels
= bmi160_channels
;
560 indio_dev
->num_channels
= ARRAY_SIZE(bmi160_channels
);
561 indio_dev
->name
= name
;
562 indio_dev
->modes
= INDIO_DIRECT_MODE
;
563 indio_dev
->info
= &bmi160_info
;
565 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
566 bmi160_trigger_handler
, NULL
);
570 ret
= iio_device_register(indio_dev
);
576 iio_triggered_buffer_cleanup(indio_dev
);
578 bmi160_chip_uninit(data
);
581 EXPORT_SYMBOL_GPL(bmi160_core_probe
);
583 void bmi160_core_remove(struct device
*dev
)
585 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
586 struct bmi160_data
*data
= iio_priv(indio_dev
);
588 iio_device_unregister(indio_dev
);
589 iio_triggered_buffer_cleanup(indio_dev
);
590 bmi160_chip_uninit(data
);
592 EXPORT_SYMBOL_GPL(bmi160_core_remove
);
594 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com");
595 MODULE_DESCRIPTION("Bosch BMI160 driver");
596 MODULE_LICENSE("GPL v2");