1 // SPDX-License-Identifier: GPL-2.0
3 * BMI160 - Bosch IMU (accel, gyro plus external magnetometer)
5 * Copyright (c) 2016, Intel Corporation.
6 * Copyright (c) 2019, Martin Kelly.
8 * IIO core driver for BMI160, with support for I2C/SPI busses
10 * TODO: magnetometer, hardware FIFO
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 #include <linux/acpi.h>
15 #include <linux/delay.h>
16 #include <linux/irq.h>
17 #include <linux/of_irq.h>
18 #include <linux/regulator/consumer.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/triggered_buffer.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/sysfs.h>
25 #include <linux/iio/trigger.h>
29 #define BMI160_REG_CHIP_ID 0x00
30 #define BMI160_CHIP_ID_VAL 0xD1
32 #define BMI160_REG_PMU_STATUS 0x03
34 /* X axis data low byte address, the rest can be obtained using axis offset */
35 #define BMI160_REG_DATA_MAGN_XOUT_L 0x04
36 #define BMI160_REG_DATA_GYRO_XOUT_L 0x0C
37 #define BMI160_REG_DATA_ACCEL_XOUT_L 0x12
39 #define BMI160_REG_ACCEL_CONFIG 0x40
40 #define BMI160_ACCEL_CONFIG_ODR_MASK GENMASK(3, 0)
41 #define BMI160_ACCEL_CONFIG_BWP_MASK GENMASK(6, 4)
43 #define BMI160_REG_ACCEL_RANGE 0x41
44 #define BMI160_ACCEL_RANGE_2G 0x03
45 #define BMI160_ACCEL_RANGE_4G 0x05
46 #define BMI160_ACCEL_RANGE_8G 0x08
47 #define BMI160_ACCEL_RANGE_16G 0x0C
49 #define BMI160_REG_GYRO_CONFIG 0x42
50 #define BMI160_GYRO_CONFIG_ODR_MASK GENMASK(3, 0)
51 #define BMI160_GYRO_CONFIG_BWP_MASK GENMASK(5, 4)
53 #define BMI160_REG_GYRO_RANGE 0x43
54 #define BMI160_GYRO_RANGE_2000DPS 0x00
55 #define BMI160_GYRO_RANGE_1000DPS 0x01
56 #define BMI160_GYRO_RANGE_500DPS 0x02
57 #define BMI160_GYRO_RANGE_250DPS 0x03
58 #define BMI160_GYRO_RANGE_125DPS 0x04
60 #define BMI160_REG_CMD 0x7E
61 #define BMI160_CMD_ACCEL_PM_SUSPEND 0x10
62 #define BMI160_CMD_ACCEL_PM_NORMAL 0x11
63 #define BMI160_CMD_ACCEL_PM_LOW_POWER 0x12
64 #define BMI160_CMD_GYRO_PM_SUSPEND 0x14
65 #define BMI160_CMD_GYRO_PM_NORMAL 0x15
66 #define BMI160_CMD_GYRO_PM_FAST_STARTUP 0x17
67 #define BMI160_CMD_SOFTRESET 0xB6
69 #define BMI160_REG_INT_EN 0x51
70 #define BMI160_DRDY_INT_EN BIT(4)
72 #define BMI160_REG_INT_OUT_CTRL 0x53
73 #define BMI160_INT_OUT_CTRL_MASK 0x0f
74 #define BMI160_INT1_OUT_CTRL_SHIFT 0
75 #define BMI160_INT2_OUT_CTRL_SHIFT 4
76 #define BMI160_EDGE_TRIGGERED BIT(0)
77 #define BMI160_ACTIVE_HIGH BIT(1)
78 #define BMI160_OPEN_DRAIN BIT(2)
79 #define BMI160_OUTPUT_EN BIT(3)
81 #define BMI160_REG_INT_LATCH 0x54
82 #define BMI160_INT1_LATCH_MASK BIT(4)
83 #define BMI160_INT2_LATCH_MASK BIT(5)
85 /* INT1 and INT2 are in the opposite order as in INT_OUT_CTRL! */
86 #define BMI160_REG_INT_MAP 0x56
87 #define BMI160_INT1_MAP_DRDY_EN 0x80
88 #define BMI160_INT2_MAP_DRDY_EN 0x08
90 #define BMI160_REG_DUMMY 0x7F
92 #define BMI160_NORMAL_WRITE_USLEEP 2
93 #define BMI160_SUSPENDED_WRITE_USLEEP 450
95 #define BMI160_ACCEL_PMU_MIN_USLEEP 3800
96 #define BMI160_GYRO_PMU_MIN_USLEEP 80000
97 #define BMI160_SOFTRESET_USLEEP 1000
99 #define BMI160_CHANNEL(_type, _axis, _index) { \
102 .channel2 = IIO_MOD_##_axis, \
103 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
104 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
105 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
106 .scan_index = _index, \
111 .endianness = IIO_LE, \
113 .ext_info = bmi160_ext_info, \
116 /* scan indexes follow DATA register order */
117 enum bmi160_scan_axis {
118 BMI160_SCAN_EXT_MAGN_X = 0,
119 BMI160_SCAN_EXT_MAGN_Y,
120 BMI160_SCAN_EXT_MAGN_Z,
128 BMI160_SCAN_TIMESTAMP,
131 enum bmi160_sensor_type {
135 BMI160_NUM_SENSORS /* must be last */
138 enum bmi160_int_pin {
143 const struct regmap_config bmi160_regmap_config = {
147 EXPORT_SYMBOL(bmi160_regmap_config);
150 u8 data; /* LSB byte register for X-axis */
159 static struct bmi160_regs bmi160_regs[] = {
161 .data = BMI160_REG_DATA_ACCEL_XOUT_L,
162 .config = BMI160_REG_ACCEL_CONFIG,
163 .config_odr_mask = BMI160_ACCEL_CONFIG_ODR_MASK,
164 .config_bwp_mask = BMI160_ACCEL_CONFIG_BWP_MASK,
165 .range = BMI160_REG_ACCEL_RANGE,
166 .pmu_cmd_normal = BMI160_CMD_ACCEL_PM_NORMAL,
167 .pmu_cmd_suspend = BMI160_CMD_ACCEL_PM_SUSPEND,
170 .data = BMI160_REG_DATA_GYRO_XOUT_L,
171 .config = BMI160_REG_GYRO_CONFIG,
172 .config_odr_mask = BMI160_GYRO_CONFIG_ODR_MASK,
173 .config_bwp_mask = BMI160_GYRO_CONFIG_BWP_MASK,
174 .range = BMI160_REG_GYRO_RANGE,
175 .pmu_cmd_normal = BMI160_CMD_GYRO_PM_NORMAL,
176 .pmu_cmd_suspend = BMI160_CMD_GYRO_PM_SUSPEND,
180 static unsigned long bmi160_pmu_time[] = {
181 [BMI160_ACCEL] = BMI160_ACCEL_PMU_MIN_USLEEP,
182 [BMI160_GYRO] = BMI160_GYRO_PMU_MIN_USLEEP,
185 struct bmi160_scale {
196 static const struct bmi160_scale bmi160_accel_scale[] = {
197 { BMI160_ACCEL_RANGE_2G, 598},
198 { BMI160_ACCEL_RANGE_4G, 1197},
199 { BMI160_ACCEL_RANGE_8G, 2394},
200 { BMI160_ACCEL_RANGE_16G, 4788},
203 static const struct bmi160_scale bmi160_gyro_scale[] = {
204 { BMI160_GYRO_RANGE_2000DPS, 1065},
205 { BMI160_GYRO_RANGE_1000DPS, 532},
206 { BMI160_GYRO_RANGE_500DPS, 266},
207 { BMI160_GYRO_RANGE_250DPS, 133},
208 { BMI160_GYRO_RANGE_125DPS, 66},
211 struct bmi160_scale_item {
212 const struct bmi160_scale *tbl;
216 static const struct bmi160_scale_item bmi160_scale_table[] = {
218 .tbl = bmi160_accel_scale,
219 .num = ARRAY_SIZE(bmi160_accel_scale),
222 .tbl = bmi160_gyro_scale,
223 .num = ARRAY_SIZE(bmi160_gyro_scale),
227 static const struct bmi160_odr bmi160_accel_odr[] = {
242 static const struct bmi160_odr bmi160_gyro_odr[] = {
253 struct bmi160_odr_item {
254 const struct bmi160_odr *tbl;
258 static const struct bmi160_odr_item bmi160_odr_table[] = {
260 .tbl = bmi160_accel_odr,
261 .num = ARRAY_SIZE(bmi160_accel_odr),
264 .tbl = bmi160_gyro_odr,
265 .num = ARRAY_SIZE(bmi160_gyro_odr),
269 static const struct iio_mount_matrix *
270 bmi160_get_mount_matrix(const struct iio_dev *indio_dev,
271 const struct iio_chan_spec *chan)
273 struct bmi160_data *data = iio_priv(indio_dev);
275 return &data->orientation;
278 static const struct iio_chan_spec_ext_info bmi160_ext_info[] = {
279 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, bmi160_get_mount_matrix),
283 static const struct iio_chan_spec bmi160_channels[] = {
284 BMI160_CHANNEL(IIO_ACCEL, X, BMI160_SCAN_ACCEL_X),
285 BMI160_CHANNEL(IIO_ACCEL, Y, BMI160_SCAN_ACCEL_Y),
286 BMI160_CHANNEL(IIO_ACCEL, Z, BMI160_SCAN_ACCEL_Z),
287 BMI160_CHANNEL(IIO_ANGL_VEL, X, BMI160_SCAN_GYRO_X),
288 BMI160_CHANNEL(IIO_ANGL_VEL, Y, BMI160_SCAN_GYRO_Y),
289 BMI160_CHANNEL(IIO_ANGL_VEL, Z, BMI160_SCAN_GYRO_Z),
290 IIO_CHAN_SOFT_TIMESTAMP(BMI160_SCAN_TIMESTAMP),
293 static enum bmi160_sensor_type bmi160_to_sensor(enum iio_chan_type iio_type)
306 int bmi160_set_mode(struct bmi160_data *data, enum bmi160_sensor_type t,
313 cmd = bmi160_regs[t].pmu_cmd_normal;
315 cmd = bmi160_regs[t].pmu_cmd_suspend;
317 ret = regmap_write(data->regmap, BMI160_REG_CMD, cmd);
321 usleep_range(bmi160_pmu_time[t], bmi160_pmu_time[t] + 1000);
327 int bmi160_set_scale(struct bmi160_data *data, enum bmi160_sensor_type t,
332 for (i = 0; i < bmi160_scale_table[t].num; i++)
333 if (bmi160_scale_table[t].tbl[i].uscale == uscale)
336 if (i == bmi160_scale_table[t].num)
339 return regmap_write(data->regmap, bmi160_regs[t].range,
340 bmi160_scale_table[t].tbl[i].bits);
344 int bmi160_get_scale(struct bmi160_data *data, enum bmi160_sensor_type t,
349 ret = regmap_read(data->regmap, bmi160_regs[t].range, &val);
353 for (i = 0; i < bmi160_scale_table[t].num; i++)
354 if (bmi160_scale_table[t].tbl[i].bits == val) {
355 *uscale = bmi160_scale_table[t].tbl[i].uscale;
362 static int bmi160_get_data(struct bmi160_data *data, int chan_type,
368 enum bmi160_sensor_type t = bmi160_to_sensor(chan_type);
370 reg = bmi160_regs[t].data + (axis - IIO_MOD_X) * sizeof(sample);
372 ret = regmap_bulk_read(data->regmap, reg, &sample, sizeof(sample));
376 *val = sign_extend32(le16_to_cpu(sample), 15);
382 int bmi160_set_odr(struct bmi160_data *data, enum bmi160_sensor_type t,
387 for (i = 0; i < bmi160_odr_table[t].num; i++)
388 if (bmi160_odr_table[t].tbl[i].odr == odr &&
389 bmi160_odr_table[t].tbl[i].uodr == uodr)
392 if (i >= bmi160_odr_table[t].num)
395 return regmap_update_bits(data->regmap,
396 bmi160_regs[t].config,
397 bmi160_regs[t].config_odr_mask,
398 bmi160_odr_table[t].tbl[i].bits);
401 static int bmi160_get_odr(struct bmi160_data *data, enum bmi160_sensor_type t,
406 ret = regmap_read(data->regmap, bmi160_regs[t].config, &val);
410 val &= bmi160_regs[t].config_odr_mask;
412 for (i = 0; i < bmi160_odr_table[t].num; i++)
413 if (val == bmi160_odr_table[t].tbl[i].bits)
416 if (i >= bmi160_odr_table[t].num)
419 *odr = bmi160_odr_table[t].tbl[i].odr;
420 *uodr = bmi160_odr_table[t].tbl[i].uodr;
425 static irqreturn_t bmi160_trigger_handler(int irq, void *p)
427 struct iio_poll_func *pf = p;
428 struct iio_dev *indio_dev = pf->indio_dev;
429 struct bmi160_data *data = iio_priv(indio_dev);
430 int i, ret, j = 0, base = BMI160_REG_DATA_MAGN_XOUT_L;
433 for_each_set_bit(i, indio_dev->active_scan_mask,
434 indio_dev->masklength) {
435 ret = regmap_bulk_read(data->regmap, base + i * sizeof(sample),
436 &sample, sizeof(sample));
439 data->buf[j++] = sample;
442 iio_push_to_buffers_with_timestamp(indio_dev, data->buf, pf->timestamp);
444 iio_trigger_notify_done(indio_dev->trig);
448 static int bmi160_read_raw(struct iio_dev *indio_dev,
449 struct iio_chan_spec const *chan,
450 int *val, int *val2, long mask)
453 struct bmi160_data *data = iio_priv(indio_dev);
456 case IIO_CHAN_INFO_RAW:
457 ret = bmi160_get_data(data, chan->type, chan->channel2, val);
461 case IIO_CHAN_INFO_SCALE:
463 ret = bmi160_get_scale(data,
464 bmi160_to_sensor(chan->type), val2);
465 return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
466 case IIO_CHAN_INFO_SAMP_FREQ:
467 ret = bmi160_get_odr(data, bmi160_to_sensor(chan->type),
469 return ret ? ret : IIO_VAL_INT_PLUS_MICRO;
477 static int bmi160_write_raw(struct iio_dev *indio_dev,
478 struct iio_chan_spec const *chan,
479 int val, int val2, long mask)
481 struct bmi160_data *data = iio_priv(indio_dev);
484 case IIO_CHAN_INFO_SCALE:
485 return bmi160_set_scale(data,
486 bmi160_to_sensor(chan->type), val2);
488 case IIO_CHAN_INFO_SAMP_FREQ:
489 return bmi160_set_odr(data, bmi160_to_sensor(chan->type),
499 IIO_CONST_ATTR(in_accel_sampling_frequency_available,
500 "0.78125 1.5625 3.125 6.25 12.5 25 50 100 200 400 800 1600");
502 IIO_CONST_ATTR(in_anglvel_sampling_frequency_available,
503 "25 50 100 200 400 800 1600 3200");
505 IIO_CONST_ATTR(in_accel_scale_available,
506 "0.000598 0.001197 0.002394 0.004788");
508 IIO_CONST_ATTR(in_anglvel_scale_available,
509 "0.001065 0.000532 0.000266 0.000133 0.000066");
511 static struct attribute *bmi160_attrs[] = {
512 &iio_const_attr_in_accel_sampling_frequency_available.dev_attr.attr,
513 &iio_const_attr_in_anglvel_sampling_frequency_available.dev_attr.attr,
514 &iio_const_attr_in_accel_scale_available.dev_attr.attr,
515 &iio_const_attr_in_anglvel_scale_available.dev_attr.attr,
519 static const struct attribute_group bmi160_attrs_group = {
520 .attrs = bmi160_attrs,
523 static const struct iio_info bmi160_info = {
524 .read_raw = bmi160_read_raw,
525 .write_raw = bmi160_write_raw,
526 .attrs = &bmi160_attrs_group,
529 static const char *bmi160_match_acpi_device(struct device *dev)
531 const struct acpi_device_id *id;
533 id = acpi_match_device(dev->driver->acpi_match_table, dev);
537 return dev_name(dev);
540 static int bmi160_write_conf_reg(struct regmap *regmap, unsigned int reg,
541 unsigned int mask, unsigned int bits,
542 unsigned int write_usleep)
547 ret = regmap_read(regmap, reg, &val);
551 val = (val & ~mask) | bits;
553 ret = regmap_write(regmap, reg, val);
558 * We need to wait after writing before we can write again. See the
559 * datasheet, page 93.
561 usleep_range(write_usleep, write_usleep + 1000);
566 static int bmi160_config_pin(struct regmap *regmap, enum bmi160_int_pin pin,
567 bool open_drain, u8 irq_mask,
568 unsigned long write_usleep)
571 struct device *dev = regmap_get_device(regmap);
572 u8 int_out_ctrl_shift;
575 u8 int_out_ctrl_mask;
576 u8 int_out_ctrl_bits;
577 const char *pin_name;
580 case BMI160_PIN_INT1:
581 int_out_ctrl_shift = BMI160_INT1_OUT_CTRL_SHIFT;
582 int_latch_mask = BMI160_INT1_LATCH_MASK;
583 int_map_mask = BMI160_INT1_MAP_DRDY_EN;
585 case BMI160_PIN_INT2:
586 int_out_ctrl_shift = BMI160_INT2_OUT_CTRL_SHIFT;
587 int_latch_mask = BMI160_INT2_LATCH_MASK;
588 int_map_mask = BMI160_INT2_MAP_DRDY_EN;
591 int_out_ctrl_mask = BMI160_INT_OUT_CTRL_MASK << int_out_ctrl_shift;
594 * Enable the requested pin with the right settings:
595 * - Push-pull/open-drain
597 * - Edge/level triggered
599 int_out_ctrl_bits = BMI160_OUTPUT_EN;
601 /* Default is push-pull. */
602 int_out_ctrl_bits |= BMI160_OPEN_DRAIN;
603 int_out_ctrl_bits |= irq_mask;
604 int_out_ctrl_bits <<= int_out_ctrl_shift;
606 ret = bmi160_write_conf_reg(regmap, BMI160_REG_INT_OUT_CTRL,
607 int_out_ctrl_mask, int_out_ctrl_bits,
612 /* Set the pin to input mode with no latching. */
613 ret = bmi160_write_conf_reg(regmap, BMI160_REG_INT_LATCH,
614 int_latch_mask, int_latch_mask,
619 /* Map interrupts to the requested pin. */
620 ret = bmi160_write_conf_reg(regmap, BMI160_REG_INT_MAP,
621 int_map_mask, int_map_mask,
625 case BMI160_PIN_INT1:
628 case BMI160_PIN_INT2:
632 dev_err(dev, "Failed to configure %s IRQ pin", pin_name);
638 int bmi160_enable_irq(struct regmap *regmap, bool enable)
640 unsigned int enable_bit = 0;
643 enable_bit = BMI160_DRDY_INT_EN;
645 return bmi160_write_conf_reg(regmap, BMI160_REG_INT_EN,
646 BMI160_DRDY_INT_EN, enable_bit,
647 BMI160_NORMAL_WRITE_USLEEP);
649 EXPORT_SYMBOL(bmi160_enable_irq);
651 static int bmi160_get_irq(struct device_node *of_node, enum bmi160_int_pin *pin)
655 /* Use INT1 if possible, otherwise fall back to INT2. */
656 irq = of_irq_get_byname(of_node, "INT1");
658 *pin = BMI160_PIN_INT1;
662 irq = of_irq_get_byname(of_node, "INT2");
664 *pin = BMI160_PIN_INT2;
669 static int bmi160_config_device_irq(struct iio_dev *indio_dev, int irq_type,
670 enum bmi160_int_pin pin)
674 struct bmi160_data *data = iio_priv(indio_dev);
675 struct device *dev = regmap_get_device(data->regmap);
677 /* Level-triggered, active-low is the default if we set all zeroes. */
678 if (irq_type == IRQF_TRIGGER_RISING)
679 irq_mask = BMI160_ACTIVE_HIGH | BMI160_EDGE_TRIGGERED;
680 else if (irq_type == IRQF_TRIGGER_FALLING)
681 irq_mask = BMI160_EDGE_TRIGGERED;
682 else if (irq_type == IRQF_TRIGGER_HIGH)
683 irq_mask = BMI160_ACTIVE_HIGH;
684 else if (irq_type == IRQF_TRIGGER_LOW)
687 dev_err(&indio_dev->dev,
688 "Invalid interrupt type 0x%x specified\n", irq_type);
692 open_drain = of_property_read_bool(dev->of_node, "drive-open-drain");
694 return bmi160_config_pin(data->regmap, pin, open_drain, irq_mask,
695 BMI160_NORMAL_WRITE_USLEEP);
698 static int bmi160_setup_irq(struct iio_dev *indio_dev, int irq,
699 enum bmi160_int_pin pin)
701 struct irq_data *desc;
705 desc = irq_get_irq_data(irq);
707 dev_err(&indio_dev->dev, "Could not find IRQ %d\n", irq);
711 irq_type = irqd_get_trigger_type(desc);
713 ret = bmi160_config_device_irq(indio_dev, irq_type, pin);
717 return bmi160_probe_trigger(indio_dev, irq, irq_type);
720 static int bmi160_chip_init(struct bmi160_data *data, bool use_spi)
724 struct device *dev = regmap_get_device(data->regmap);
726 ret = regulator_bulk_enable(ARRAY_SIZE(data->supplies), data->supplies);
728 dev_err(dev, "Failed to enable regulators: %d\n", ret);
732 ret = regmap_write(data->regmap, BMI160_REG_CMD, BMI160_CMD_SOFTRESET);
734 goto disable_regulator;
736 usleep_range(BMI160_SOFTRESET_USLEEP, BMI160_SOFTRESET_USLEEP + 1);
739 * CS rising edge is needed before starting SPI, so do a dummy read
740 * See Section 3.2.1, page 86 of the datasheet
743 ret = regmap_read(data->regmap, BMI160_REG_DUMMY, &val);
745 goto disable_regulator;
748 ret = regmap_read(data->regmap, BMI160_REG_CHIP_ID, &val);
750 dev_err(dev, "Error reading chip id\n");
751 goto disable_regulator;
753 if (val != BMI160_CHIP_ID_VAL) {
754 dev_err(dev, "Wrong chip id, got %x expected %x\n",
755 val, BMI160_CHIP_ID_VAL);
757 goto disable_regulator;
760 ret = bmi160_set_mode(data, BMI160_ACCEL, true);
762 goto disable_regulator;
764 ret = bmi160_set_mode(data, BMI160_GYRO, true);
771 bmi160_set_mode(data, BMI160_ACCEL, false);
774 regulator_bulk_disable(ARRAY_SIZE(data->supplies), data->supplies);
778 static int bmi160_data_rdy_trigger_set_state(struct iio_trigger *trig,
781 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
782 struct bmi160_data *data = iio_priv(indio_dev);
784 return bmi160_enable_irq(data->regmap, enable);
787 static const struct iio_trigger_ops bmi160_trigger_ops = {
788 .set_trigger_state = &bmi160_data_rdy_trigger_set_state,
791 int bmi160_probe_trigger(struct iio_dev *indio_dev, int irq, u32 irq_type)
793 struct bmi160_data *data = iio_priv(indio_dev);
796 data->trig = devm_iio_trigger_alloc(&indio_dev->dev, "%s-dev%d",
797 indio_dev->name, indio_dev->id);
799 if (data->trig == NULL)
802 ret = devm_request_irq(&indio_dev->dev, irq,
803 &iio_trigger_generic_data_rdy_poll,
804 irq_type, "bmi160", data->trig);
808 data->trig->dev.parent = regmap_get_device(data->regmap);
809 data->trig->ops = &bmi160_trigger_ops;
810 iio_trigger_set_drvdata(data->trig, indio_dev);
812 ret = devm_iio_trigger_register(&indio_dev->dev, data->trig);
816 indio_dev->trig = iio_trigger_get(data->trig);
821 static void bmi160_chip_uninit(void *data)
823 struct bmi160_data *bmi_data = data;
824 struct device *dev = regmap_get_device(bmi_data->regmap);
827 bmi160_set_mode(bmi_data, BMI160_GYRO, false);
828 bmi160_set_mode(bmi_data, BMI160_ACCEL, false);
830 ret = regulator_bulk_disable(ARRAY_SIZE(bmi_data->supplies),
833 dev_err(dev, "Failed to disable regulators: %d\n", ret);
836 int bmi160_core_probe(struct device *dev, struct regmap *regmap,
837 const char *name, bool use_spi)
839 struct iio_dev *indio_dev;
840 struct bmi160_data *data;
842 enum bmi160_int_pin int_pin;
845 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
849 data = iio_priv(indio_dev);
850 dev_set_drvdata(dev, indio_dev);
851 data->regmap = regmap;
853 data->supplies[0].supply = "vdd";
854 data->supplies[1].supply = "vddio";
855 ret = devm_regulator_bulk_get(dev,
856 ARRAY_SIZE(data->supplies),
859 dev_err(dev, "Failed to get regulators: %d\n", ret);
863 ret = iio_read_mount_matrix(dev, "mount-matrix",
868 ret = bmi160_chip_init(data, use_spi);
872 ret = devm_add_action_or_reset(dev, bmi160_chip_uninit, data);
876 if (!name && ACPI_HANDLE(dev))
877 name = bmi160_match_acpi_device(dev);
879 indio_dev->channels = bmi160_channels;
880 indio_dev->num_channels = ARRAY_SIZE(bmi160_channels);
881 indio_dev->name = name;
882 indio_dev->modes = INDIO_DIRECT_MODE;
883 indio_dev->info = &bmi160_info;
885 ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
886 iio_pollfunc_store_time,
887 bmi160_trigger_handler, NULL);
891 irq = bmi160_get_irq(dev->of_node, &int_pin);
893 ret = bmi160_setup_irq(indio_dev, irq, int_pin);
895 dev_err(&indio_dev->dev, "Failed to setup IRQ %d\n",
898 dev_info(&indio_dev->dev, "Not setting up IRQ trigger\n");
901 return devm_iio_device_register(dev, indio_dev);
903 EXPORT_SYMBOL_GPL(bmi160_core_probe);
905 MODULE_AUTHOR("Daniel Baluta <daniel.baluta@intel.com>");
906 MODULE_DESCRIPTION("Bosch BMI160 driver");
907 MODULE_LICENSE("GPL v2");