1 // SPDX-License-Identifier: GPL-2.0-only
3 * Core IIO driver for Bosch BMA400 triaxial acceleration sensor.
5 * Copyright 2019 Dan Robertson <dan@dlrobertson.com>
8 * - Support for power management
9 * - Support events and interrupts
10 * - Create channel for step count
11 * - Create channel for sensor time
14 #include <linux/bitops.h>
15 #include <linux/device.h>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/sysfs.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/regmap.h>
22 #include <linux/regulator/consumer.h>
27 * The G-range selection may be one of 2g, 4g, 8, or 16g. The scale may
28 * be selected with the acc_range bits of the ACC_CONFIG1 register.
29 * NB: This buffer is populated in the device init.
31 static int bma400_scales
[8];
34 * See the ACC_CONFIG1 section of the datasheet.
35 * NB: This buffer is populated in the device init.
37 static int bma400_sample_freqs
[14];
39 static const int bma400_osr_range
[] = { 0, 1, 3 };
41 /* See the ACC_CONFIG0 section of the datasheet */
42 enum bma400_power_mode
{
43 POWER_MODE_SLEEP
= 0x00,
44 POWER_MODE_LOW
= 0x01,
45 POWER_MODE_NORMAL
= 0x02,
46 POWER_MODE_INVALID
= 0x03,
49 struct bma400_sample_freq
{
56 struct regmap
*regmap
;
57 struct regulator_bulk_data regulators
[BMA400_NUM_REGULATORS
];
58 struct mutex mutex
; /* data register lock */
59 struct iio_mount_matrix orientation
;
60 enum bma400_power_mode power_mode
;
61 struct bma400_sample_freq sample_freq
;
62 int oversampling_ratio
;
66 static bool bma400_is_writable_reg(struct device
*dev
, unsigned int reg
)
69 case BMA400_CHIP_ID_REG
:
71 case BMA400_STATUS_REG
:
72 case BMA400_X_AXIS_LSB_REG
:
73 case BMA400_X_AXIS_MSB_REG
:
74 case BMA400_Y_AXIS_LSB_REG
:
75 case BMA400_Y_AXIS_MSB_REG
:
76 case BMA400_Z_AXIS_LSB_REG
:
77 case BMA400_Z_AXIS_MSB_REG
:
78 case BMA400_SENSOR_TIME0
:
79 case BMA400_SENSOR_TIME1
:
80 case BMA400_SENSOR_TIME2
:
81 case BMA400_EVENT_REG
:
82 case BMA400_INT_STAT0_REG
:
83 case BMA400_INT_STAT1_REG
:
84 case BMA400_INT_STAT2_REG
:
85 case BMA400_TEMP_DATA_REG
:
86 case BMA400_FIFO_LENGTH0_REG
:
87 case BMA400_FIFO_LENGTH1_REG
:
88 case BMA400_FIFO_DATA_REG
:
89 case BMA400_STEP_CNT0_REG
:
90 case BMA400_STEP_CNT1_REG
:
91 case BMA400_STEP_CNT3_REG
:
92 case BMA400_STEP_STAT_REG
:
99 static bool bma400_is_volatile_reg(struct device
*dev
, unsigned int reg
)
103 case BMA400_STATUS_REG
:
104 case BMA400_X_AXIS_LSB_REG
:
105 case BMA400_X_AXIS_MSB_REG
:
106 case BMA400_Y_AXIS_LSB_REG
:
107 case BMA400_Y_AXIS_MSB_REG
:
108 case BMA400_Z_AXIS_LSB_REG
:
109 case BMA400_Z_AXIS_MSB_REG
:
110 case BMA400_SENSOR_TIME0
:
111 case BMA400_SENSOR_TIME1
:
112 case BMA400_SENSOR_TIME2
:
113 case BMA400_EVENT_REG
:
114 case BMA400_INT_STAT0_REG
:
115 case BMA400_INT_STAT1_REG
:
116 case BMA400_INT_STAT2_REG
:
117 case BMA400_TEMP_DATA_REG
:
118 case BMA400_FIFO_LENGTH0_REG
:
119 case BMA400_FIFO_LENGTH1_REG
:
120 case BMA400_FIFO_DATA_REG
:
121 case BMA400_STEP_CNT0_REG
:
122 case BMA400_STEP_CNT1_REG
:
123 case BMA400_STEP_CNT3_REG
:
124 case BMA400_STEP_STAT_REG
:
131 const struct regmap_config bma400_regmap_config
= {
134 .max_register
= BMA400_CMD_REG
,
135 .cache_type
= REGCACHE_RBTREE
,
136 .writeable_reg
= bma400_is_writable_reg
,
137 .volatile_reg
= bma400_is_volatile_reg
,
139 EXPORT_SYMBOL(bma400_regmap_config
);
141 static const struct iio_mount_matrix
*
142 bma400_accel_get_mount_matrix(const struct iio_dev
*indio_dev
,
143 const struct iio_chan_spec
*chan
)
145 struct bma400_data
*data
= iio_priv(indio_dev
);
147 return &data
->orientation
;
150 static const struct iio_chan_spec_ext_info bma400_ext_info
[] = {
151 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR
, bma400_accel_get_mount_matrix
),
155 #define BMA400_ACC_CHANNEL(_axis) { \
158 .channel2 = IIO_MOD_##_axis, \
159 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
160 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
161 BIT(IIO_CHAN_INFO_SCALE) | \
162 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
163 .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
164 BIT(IIO_CHAN_INFO_SCALE) | \
165 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
166 .ext_info = bma400_ext_info, \
169 static const struct iio_chan_spec bma400_channels
[] = {
170 BMA400_ACC_CHANNEL(X
),
171 BMA400_ACC_CHANNEL(Y
),
172 BMA400_ACC_CHANNEL(Z
),
175 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
176 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
180 static int bma400_get_temp_reg(struct bma400_data
*data
, int *val
, int *val2
)
182 unsigned int raw_temp
;
186 if (data
->power_mode
== POWER_MODE_SLEEP
)
189 ret
= regmap_read(data
->regmap
, BMA400_TEMP_DATA_REG
, &raw_temp
);
193 host_temp
= sign_extend32(raw_temp
, 7);
195 * The formula for the TEMP_DATA register in the datasheet
198 *val
= (host_temp
>> 1) + 23;
199 *val2
= (host_temp
& 0x1) * 500000;
200 return IIO_VAL_INT_PLUS_MICRO
;
203 static int bma400_get_accel_reg(struct bma400_data
*data
,
204 const struct iio_chan_spec
*chan
,
211 if (data
->power_mode
== POWER_MODE_SLEEP
)
214 switch (chan
->channel2
) {
216 lsb_reg
= BMA400_X_AXIS_LSB_REG
;
219 lsb_reg
= BMA400_Y_AXIS_LSB_REG
;
222 lsb_reg
= BMA400_Z_AXIS_LSB_REG
;
225 dev_err(data
->dev
, "invalid axis channel modifier\n");
229 /* bulk read two registers, with the base being the LSB register */
230 ret
= regmap_bulk_read(data
->regmap
, lsb_reg
, &raw_accel
,
235 *val
= sign_extend32(le16_to_cpu(raw_accel
), 11);
239 static void bma400_output_data_rate_from_raw(int raw
, unsigned int *val
,
242 *val
= BMA400_ACC_ODR_MAX_HZ
>> (BMA400_ACC_ODR_MAX_RAW
- raw
);
243 if (raw
> BMA400_ACC_ODR_MIN_RAW
)
249 static int bma400_get_accel_output_data_rate(struct bma400_data
*data
)
255 switch (data
->power_mode
) {
258 * Runs at a fixed rate in low-power mode. See section 4.3
261 bma400_output_data_rate_from_raw(BMA400_ACC_ODR_LP_RAW
,
262 &data
->sample_freq
.hz
,
263 &data
->sample_freq
.uhz
);
265 case POWER_MODE_NORMAL
:
267 * In normal mode the ODR can be found in the ACC_CONFIG1
270 ret
= regmap_read(data
->regmap
, BMA400_ACC_CONFIG1_REG
, &val
);
274 odr
= val
& BMA400_ACC_ODR_MASK
;
275 if (odr
< BMA400_ACC_ODR_MIN_RAW
||
276 odr
> BMA400_ACC_ODR_MAX_RAW
) {
281 bma400_output_data_rate_from_raw(odr
, &data
->sample_freq
.hz
,
282 &data
->sample_freq
.uhz
);
284 case POWER_MODE_SLEEP
:
285 data
->sample_freq
.hz
= 0;
286 data
->sample_freq
.uhz
= 0;
293 data
->sample_freq
.hz
= -1;
294 data
->sample_freq
.uhz
= -1;
298 static int bma400_set_accel_output_data_rate(struct bma400_data
*data
,
306 if (hz
>= BMA400_ACC_ODR_MIN_WHOLE_HZ
) {
307 if (uhz
|| hz
> BMA400_ACC_ODR_MAX_HZ
)
310 /* Note this works because MIN_WHOLE_HZ is odd */
313 if (hz
>> idx
!= BMA400_ACC_ODR_MIN_WHOLE_HZ
)
316 idx
+= BMA400_ACC_ODR_MIN_RAW
+ 1;
317 } else if (hz
== BMA400_ACC_ODR_MIN_HZ
&& uhz
== 500000) {
318 idx
= BMA400_ACC_ODR_MIN_RAW
;
323 ret
= regmap_read(data
->regmap
, BMA400_ACC_CONFIG1_REG
, &val
);
327 /* preserve the range and normal mode osr */
328 odr
= (~BMA400_ACC_ODR_MASK
& val
) | idx
;
330 ret
= regmap_write(data
->regmap
, BMA400_ACC_CONFIG1_REG
, odr
);
334 bma400_output_data_rate_from_raw(idx
, &data
->sample_freq
.hz
,
335 &data
->sample_freq
.uhz
);
339 static int bma400_get_accel_oversampling_ratio(struct bma400_data
*data
)
346 * The oversampling ratio is stored in a different register
347 * based on the power-mode. In normal mode the OSR is stored
348 * in ACC_CONFIG1. In low-power mode it is stored in
351 switch (data
->power_mode
) {
353 ret
= regmap_read(data
->regmap
, BMA400_ACC_CONFIG0_REG
, &val
);
355 data
->oversampling_ratio
= -1;
359 osr
= (val
& BMA400_LP_OSR_MASK
) >> BMA400_LP_OSR_SHIFT
;
361 data
->oversampling_ratio
= osr
;
363 case POWER_MODE_NORMAL
:
364 ret
= regmap_read(data
->regmap
, BMA400_ACC_CONFIG1_REG
, &val
);
366 data
->oversampling_ratio
= -1;
370 osr
= (val
& BMA400_NP_OSR_MASK
) >> BMA400_NP_OSR_SHIFT
;
372 data
->oversampling_ratio
= osr
;
374 case POWER_MODE_SLEEP
:
375 data
->oversampling_ratio
= 0;
378 data
->oversampling_ratio
= -1;
383 static int bma400_set_accel_oversampling_ratio(struct bma400_data
*data
,
386 unsigned int acc_config
;
389 if (val
& ~BMA400_TWO_BITS_MASK
)
393 * The oversampling ratio is stored in a different register
394 * based on the power-mode.
396 switch (data
->power_mode
) {
398 ret
= regmap_read(data
->regmap
, BMA400_ACC_CONFIG0_REG
,
403 ret
= regmap_write(data
->regmap
, BMA400_ACC_CONFIG0_REG
,
404 (acc_config
& ~BMA400_LP_OSR_MASK
) |
405 (val
<< BMA400_LP_OSR_SHIFT
));
407 dev_err(data
->dev
, "Failed to write out OSR\n");
411 data
->oversampling_ratio
= val
;
413 case POWER_MODE_NORMAL
:
414 ret
= regmap_read(data
->regmap
, BMA400_ACC_CONFIG1_REG
,
419 ret
= regmap_write(data
->regmap
, BMA400_ACC_CONFIG1_REG
,
420 (acc_config
& ~BMA400_NP_OSR_MASK
) |
421 (val
<< BMA400_NP_OSR_SHIFT
));
423 dev_err(data
->dev
, "Failed to write out OSR\n");
427 data
->oversampling_ratio
= val
;
435 static int bma400_accel_scale_to_raw(struct bma400_data
*data
,
443 /* Note this works because BMA400_SCALE_MIN is odd */
446 if (val
>> raw
!= BMA400_SCALE_MIN
)
452 static int bma400_get_accel_scale(struct bma400_data
*data
)
454 unsigned int raw_scale
;
458 ret
= regmap_read(data
->regmap
, BMA400_ACC_CONFIG1_REG
, &val
);
462 raw_scale
= (val
& BMA400_ACC_SCALE_MASK
) >> BMA400_SCALE_SHIFT
;
463 if (raw_scale
> BMA400_TWO_BITS_MASK
)
466 data
->scale
= BMA400_SCALE_MIN
<< raw_scale
;
471 static int bma400_set_accel_scale(struct bma400_data
*data
, unsigned int val
)
473 unsigned int acc_config
;
477 ret
= regmap_read(data
->regmap
, BMA400_ACC_CONFIG1_REG
, &acc_config
);
481 raw
= bma400_accel_scale_to_raw(data
, val
);
485 ret
= regmap_write(data
->regmap
, BMA400_ACC_CONFIG1_REG
,
486 (acc_config
& ~BMA400_ACC_SCALE_MASK
) |
487 (raw
<< BMA400_SCALE_SHIFT
));
495 static int bma400_get_power_mode(struct bma400_data
*data
)
500 ret
= regmap_read(data
->regmap
, BMA400_STATUS_REG
, &val
);
502 dev_err(data
->dev
, "Failed to read status register\n");
506 data
->power_mode
= (val
>> 1) & BMA400_TWO_BITS_MASK
;
510 static int bma400_set_power_mode(struct bma400_data
*data
,
511 enum bma400_power_mode mode
)
516 ret
= regmap_read(data
->regmap
, BMA400_ACC_CONFIG0_REG
, &val
);
520 if (data
->power_mode
== mode
)
523 if (mode
== POWER_MODE_INVALID
)
526 /* Preserve the low-power oversample ratio etc */
527 ret
= regmap_write(data
->regmap
, BMA400_ACC_CONFIG0_REG
,
528 mode
| (val
& ~BMA400_TWO_BITS_MASK
));
530 dev_err(data
->dev
, "Failed to write to power-mode\n");
534 data
->power_mode
= mode
;
537 * Update our cached osr and odr based on the new
540 bma400_get_accel_output_data_rate(data
);
541 bma400_get_accel_oversampling_ratio(data
);
545 static void bma400_init_tables(void)
550 for (i
= 0; i
+ 1 < ARRAY_SIZE(bma400_sample_freqs
); i
+= 2) {
552 bma400_output_data_rate_from_raw(raw
, &bma400_sample_freqs
[i
],
553 &bma400_sample_freqs
[i
+ 1]);
556 for (i
= 0; i
+ 1 < ARRAY_SIZE(bma400_scales
); i
+= 2) {
558 bma400_scales
[i
] = 0;
559 bma400_scales
[i
+ 1] = BMA400_SCALE_MIN
<< raw
;
563 static int bma400_init(struct bma400_data
*data
)
568 /* Try to read chip_id register. It must return 0x90. */
569 ret
= regmap_read(data
->regmap
, BMA400_CHIP_ID_REG
, &val
);
571 dev_err(data
->dev
, "Failed to read chip id register\n");
575 if (val
!= BMA400_ID_REG_VAL
) {
576 dev_err(data
->dev
, "Chip ID mismatch\n");
581 data
->regulators
[BMA400_VDD_REGULATOR
].supply
= "vdd";
582 data
->regulators
[BMA400_VDDIO_REGULATOR
].supply
= "vddio";
583 ret
= devm_regulator_bulk_get(data
->dev
,
584 ARRAY_SIZE(data
->regulators
),
587 if (ret
!= -EPROBE_DEFER
)
589 "Failed to get regulators: %d\n",
594 ret
= regulator_bulk_enable(ARRAY_SIZE(data
->regulators
),
597 dev_err(data
->dev
, "Failed to enable regulators: %d\n",
602 ret
= bma400_get_power_mode(data
);
604 dev_err(data
->dev
, "Failed to get the initial power-mode\n");
605 goto err_reg_disable
;
608 if (data
->power_mode
!= POWER_MODE_NORMAL
) {
609 ret
= bma400_set_power_mode(data
, POWER_MODE_NORMAL
);
611 dev_err(data
->dev
, "Failed to wake up the device\n");
612 goto err_reg_disable
;
615 * TODO: The datasheet waits 1500us here in the example, but
616 * lists 2/ODR as the wakeup time.
618 usleep_range(1500, 2000);
621 bma400_init_tables();
623 ret
= bma400_get_accel_output_data_rate(data
);
625 goto err_reg_disable
;
627 ret
= bma400_get_accel_oversampling_ratio(data
);
629 goto err_reg_disable
;
631 ret
= bma400_get_accel_scale(data
);
633 goto err_reg_disable
;
636 * Once the interrupt engine is supported we might use the
637 * data_src_reg, but for now ensure this is set to the
638 * variable ODR filter selectable by the sample frequency
641 return regmap_write(data
->regmap
, BMA400_ACC_CONFIG2_REG
, 0x00);
644 regulator_bulk_disable(ARRAY_SIZE(data
->regulators
),
650 static int bma400_read_raw(struct iio_dev
*indio_dev
,
651 struct iio_chan_spec
const *chan
, int *val
,
652 int *val2
, long mask
)
654 struct bma400_data
*data
= iio_priv(indio_dev
);
658 case IIO_CHAN_INFO_PROCESSED
:
659 mutex_lock(&data
->mutex
);
660 ret
= bma400_get_temp_reg(data
, val
, val2
);
661 mutex_unlock(&data
->mutex
);
663 case IIO_CHAN_INFO_RAW
:
664 mutex_lock(&data
->mutex
);
665 ret
= bma400_get_accel_reg(data
, chan
, val
);
666 mutex_unlock(&data
->mutex
);
668 case IIO_CHAN_INFO_SAMP_FREQ
:
669 switch (chan
->type
) {
671 if (data
->sample_freq
.hz
< 0)
674 *val
= data
->sample_freq
.hz
;
675 *val2
= data
->sample_freq
.uhz
;
676 return IIO_VAL_INT_PLUS_MICRO
;
679 * Runs at a fixed sampling frequency. See Section 4.4
684 return IIO_VAL_INT_PLUS_MICRO
;
688 case IIO_CHAN_INFO_SCALE
:
691 return IIO_VAL_INT_PLUS_MICRO
;
692 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
694 * TODO: We could avoid this logic and returning -EINVAL here if
695 * we set both the low-power and normal mode OSR registers when
696 * we configure the device.
698 if (data
->oversampling_ratio
< 0)
701 *val
= data
->oversampling_ratio
;
708 static int bma400_read_avail(struct iio_dev
*indio_dev
,
709 struct iio_chan_spec
const *chan
,
710 const int **vals
, int *type
, int *length
,
714 case IIO_CHAN_INFO_SCALE
:
715 *type
= IIO_VAL_INT_PLUS_MICRO
;
716 *vals
= bma400_scales
;
717 *length
= ARRAY_SIZE(bma400_scales
);
718 return IIO_AVAIL_LIST
;
719 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
721 *vals
= bma400_osr_range
;
722 *length
= ARRAY_SIZE(bma400_osr_range
);
723 return IIO_AVAIL_RANGE
;
724 case IIO_CHAN_INFO_SAMP_FREQ
:
725 *type
= IIO_VAL_INT_PLUS_MICRO
;
726 *vals
= bma400_sample_freqs
;
727 *length
= ARRAY_SIZE(bma400_sample_freqs
);
728 return IIO_AVAIL_LIST
;
734 static int bma400_write_raw(struct iio_dev
*indio_dev
,
735 struct iio_chan_spec
const *chan
, int val
, int val2
,
738 struct bma400_data
*data
= iio_priv(indio_dev
);
742 case IIO_CHAN_INFO_SAMP_FREQ
:
744 * The sample frequency is readonly for the temperature
745 * register and a fixed value in low-power mode.
747 if (chan
->type
!= IIO_ACCEL
)
750 mutex_lock(&data
->mutex
);
751 ret
= bma400_set_accel_output_data_rate(data
, val
, val2
);
752 mutex_unlock(&data
->mutex
);
754 case IIO_CHAN_INFO_SCALE
:
756 val2
< BMA400_SCALE_MIN
|| val2
> BMA400_SCALE_MAX
)
759 mutex_lock(&data
->mutex
);
760 ret
= bma400_set_accel_scale(data
, val2
);
761 mutex_unlock(&data
->mutex
);
763 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
764 mutex_lock(&data
->mutex
);
765 ret
= bma400_set_accel_oversampling_ratio(data
, val
);
766 mutex_unlock(&data
->mutex
);
773 static int bma400_write_raw_get_fmt(struct iio_dev
*indio_dev
,
774 struct iio_chan_spec
const *chan
,
778 case IIO_CHAN_INFO_SAMP_FREQ
:
779 return IIO_VAL_INT_PLUS_MICRO
;
780 case IIO_CHAN_INFO_SCALE
:
781 return IIO_VAL_INT_PLUS_MICRO
;
782 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
789 static const struct iio_info bma400_info
= {
790 .read_raw
= bma400_read_raw
,
791 .read_avail
= bma400_read_avail
,
792 .write_raw
= bma400_write_raw
,
793 .write_raw_get_fmt
= bma400_write_raw_get_fmt
,
796 int bma400_probe(struct device
*dev
, struct regmap
*regmap
, const char *name
)
798 struct iio_dev
*indio_dev
;
799 struct bma400_data
*data
;
802 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
806 data
= iio_priv(indio_dev
);
807 data
->regmap
= regmap
;
810 ret
= bma400_init(data
);
814 ret
= iio_read_mount_matrix(dev
, "mount-matrix", &data
->orientation
);
818 mutex_init(&data
->mutex
);
819 indio_dev
->dev
.parent
= dev
;
820 indio_dev
->name
= name
;
821 indio_dev
->info
= &bma400_info
;
822 indio_dev
->channels
= bma400_channels
;
823 indio_dev
->num_channels
= ARRAY_SIZE(bma400_channels
);
824 indio_dev
->modes
= INDIO_DIRECT_MODE
;
826 dev_set_drvdata(dev
, indio_dev
);
828 return iio_device_register(indio_dev
);
830 EXPORT_SYMBOL(bma400_probe
);
832 int bma400_remove(struct device
*dev
)
834 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
835 struct bma400_data
*data
= iio_priv(indio_dev
);
838 mutex_lock(&data
->mutex
);
839 ret
= bma400_set_power_mode(data
, POWER_MODE_SLEEP
);
840 mutex_unlock(&data
->mutex
);
842 regulator_bulk_disable(ARRAY_SIZE(data
->regulators
),
845 iio_device_unregister(indio_dev
);
849 EXPORT_SYMBOL(bma400_remove
);
851 MODULE_AUTHOR("Dan Robertson <dan@dlrobertson.com>");
852 MODULE_DESCRIPTION("Bosch BMA400 triaxial acceleration sensor core");
853 MODULE_LICENSE("GPL");