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/bitfield.h>
15 #include <linux/bitops.h>
16 #include <linux/cleanup.h>
17 #include <linux/device.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>
23 #include <linux/slab.h>
25 #include <linux/unaligned.h>
27 #include <linux/iio/iio.h>
28 #include <linux/iio/buffer.h>
29 #include <linux/iio/events.h>
30 #include <linux/iio/sysfs.h>
31 #include <linux/iio/trigger.h>
32 #include <linux/iio/trigger_consumer.h>
33 #include <linux/iio/triggered_buffer.h>
38 * The G-range selection may be one of 2g, 4g, 8, or 16g. The scale may
39 * be selected with the acc_range bits of the ACC_CONFIG1 register.
40 * NB: This buffer is populated in the device init.
42 static int bma400_scales
[8];
45 * See the ACC_CONFIG1 section of the datasheet.
46 * NB: This buffer is populated in the device init.
48 static int bma400_sample_freqs
[14];
50 static const int bma400_osr_range
[] = { 0, 1, 3 };
52 static int tap_reset_timeout
[BMA400_TAP_TIM_LIST_LEN
] = {
59 static int tap_max2min_time
[BMA400_TAP_TIM_LIST_LEN
] = {
66 static int double_tap2_min_delay
[BMA400_TAP_TIM_LIST_LEN
] = {
73 /* See the ACC_CONFIG0 section of the datasheet */
74 enum bma400_power_mode
{
75 POWER_MODE_SLEEP
= 0x00,
76 POWER_MODE_LOW
= 0x01,
77 POWER_MODE_NORMAL
= 0x02,
78 POWER_MODE_INVALID
= 0x03,
88 struct bma400_sample_freq
{
93 enum bma400_activity
{
101 struct regmap
*regmap
;
102 struct mutex mutex
; /* data register lock */
103 struct iio_mount_matrix orientation
;
104 enum bma400_power_mode power_mode
;
105 struct bma400_sample_freq sample_freq
;
106 int oversampling_ratio
;
108 struct iio_trigger
*trig
;
111 bool activity_event_en
;
112 unsigned int generic_event_en
;
113 unsigned int tap_event_en_bitmask
;
114 /* Correct time stamp alignment */
119 } buffer
__aligned(IIO_DMA_MINALIGN
);
124 static bool bma400_is_writable_reg(struct device
*dev
, unsigned int reg
)
127 case BMA400_CHIP_ID_REG
:
129 case BMA400_STATUS_REG
:
130 case BMA400_X_AXIS_LSB_REG
:
131 case BMA400_X_AXIS_MSB_REG
:
132 case BMA400_Y_AXIS_LSB_REG
:
133 case BMA400_Y_AXIS_MSB_REG
:
134 case BMA400_Z_AXIS_LSB_REG
:
135 case BMA400_Z_AXIS_MSB_REG
:
136 case BMA400_SENSOR_TIME0
:
137 case BMA400_SENSOR_TIME1
:
138 case BMA400_SENSOR_TIME2
:
139 case BMA400_EVENT_REG
:
140 case BMA400_INT_STAT0_REG
:
141 case BMA400_INT_STAT1_REG
:
142 case BMA400_INT_STAT2_REG
:
143 case BMA400_TEMP_DATA_REG
:
144 case BMA400_FIFO_LENGTH0_REG
:
145 case BMA400_FIFO_LENGTH1_REG
:
146 case BMA400_FIFO_DATA_REG
:
147 case BMA400_STEP_CNT0_REG
:
148 case BMA400_STEP_CNT1_REG
:
149 case BMA400_STEP_CNT3_REG
:
150 case BMA400_STEP_STAT_REG
:
157 static bool bma400_is_volatile_reg(struct device
*dev
, unsigned int reg
)
161 case BMA400_STATUS_REG
:
162 case BMA400_X_AXIS_LSB_REG
:
163 case BMA400_X_AXIS_MSB_REG
:
164 case BMA400_Y_AXIS_LSB_REG
:
165 case BMA400_Y_AXIS_MSB_REG
:
166 case BMA400_Z_AXIS_LSB_REG
:
167 case BMA400_Z_AXIS_MSB_REG
:
168 case BMA400_SENSOR_TIME0
:
169 case BMA400_SENSOR_TIME1
:
170 case BMA400_SENSOR_TIME2
:
171 case BMA400_EVENT_REG
:
172 case BMA400_INT_STAT0_REG
:
173 case BMA400_INT_STAT1_REG
:
174 case BMA400_INT_STAT2_REG
:
175 case BMA400_TEMP_DATA_REG
:
176 case BMA400_FIFO_LENGTH0_REG
:
177 case BMA400_FIFO_LENGTH1_REG
:
178 case BMA400_FIFO_DATA_REG
:
179 case BMA400_STEP_CNT0_REG
:
180 case BMA400_STEP_CNT1_REG
:
181 case BMA400_STEP_CNT3_REG
:
182 case BMA400_STEP_STAT_REG
:
189 const struct regmap_config bma400_regmap_config
= {
192 .max_register
= BMA400_CMD_REG
,
193 .cache_type
= REGCACHE_RBTREE
,
194 .writeable_reg
= bma400_is_writable_reg
,
195 .volatile_reg
= bma400_is_volatile_reg
,
197 EXPORT_SYMBOL_NS(bma400_regmap_config
, "IIO_BMA400");
199 static const struct iio_mount_matrix
*
200 bma400_accel_get_mount_matrix(const struct iio_dev
*indio_dev
,
201 const struct iio_chan_spec
*chan
)
203 struct bma400_data
*data
= iio_priv(indio_dev
);
205 return &data
->orientation
;
208 static const struct iio_chan_spec_ext_info bma400_ext_info
[] = {
209 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR
, bma400_accel_get_mount_matrix
),
213 static const struct iio_event_spec bma400_step_detect_event
= {
214 .type
= IIO_EV_TYPE_CHANGE
,
215 .dir
= IIO_EV_DIR_NONE
,
216 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
),
219 static const struct iio_event_spec bma400_activity_event
= {
220 .type
= IIO_EV_TYPE_CHANGE
,
221 .dir
= IIO_EV_DIR_NONE
,
222 .mask_shared_by_type
= BIT(IIO_EV_INFO_ENABLE
),
225 static const struct iio_event_spec bma400_accel_event
[] = {
227 .type
= IIO_EV_TYPE_MAG
,
228 .dir
= IIO_EV_DIR_FALLING
,
229 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
) |
230 BIT(IIO_EV_INFO_PERIOD
) |
231 BIT(IIO_EV_INFO_HYSTERESIS
) |
232 BIT(IIO_EV_INFO_ENABLE
),
235 .type
= IIO_EV_TYPE_MAG
,
236 .dir
= IIO_EV_DIR_RISING
,
237 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
) |
238 BIT(IIO_EV_INFO_PERIOD
) |
239 BIT(IIO_EV_INFO_HYSTERESIS
) |
240 BIT(IIO_EV_INFO_ENABLE
),
243 .type
= IIO_EV_TYPE_GESTURE
,
244 .dir
= IIO_EV_DIR_SINGLETAP
,
245 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
) |
246 BIT(IIO_EV_INFO_ENABLE
) |
247 BIT(IIO_EV_INFO_RESET_TIMEOUT
),
250 .type
= IIO_EV_TYPE_GESTURE
,
251 .dir
= IIO_EV_DIR_DOUBLETAP
,
252 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
) |
253 BIT(IIO_EV_INFO_ENABLE
) |
254 BIT(IIO_EV_INFO_RESET_TIMEOUT
) |
255 BIT(IIO_EV_INFO_TAP2_MIN_DELAY
),
259 static int usec_to_tapreg_raw(int usec
, const int *time_list
)
263 for (index
= 0; index
< BMA400_TAP_TIM_LIST_LEN
; index
++) {
264 if (usec
== time_list
[index
])
270 static ssize_t
in_accel_gesture_tap_maxtomin_time_show(struct device
*dev
,
271 struct device_attribute
*attr
,
274 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
275 struct bma400_data
*data
= iio_priv(indio_dev
);
276 int ret
, reg_val
, raw
, vals
[2];
278 ret
= regmap_read(data
->regmap
, BMA400_TAP_CONFIG1
, ®_val
);
282 raw
= FIELD_GET(BMA400_TAP_TICSTH_MSK
, reg_val
);
284 vals
[1] = tap_max2min_time
[raw
];
286 return iio_format_value(buf
, IIO_VAL_INT_PLUS_MICRO
, 2, vals
);
289 static ssize_t
in_accel_gesture_tap_maxtomin_time_store(struct device
*dev
,
290 struct device_attribute
*attr
,
291 const char *buf
, size_t len
)
293 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
294 struct bma400_data
*data
= iio_priv(indio_dev
);
295 int ret
, val_int
, val_fract
, raw
;
297 ret
= iio_str_to_fixpoint(buf
, 100000, &val_int
, &val_fract
);
301 raw
= usec_to_tapreg_raw(val_fract
, tap_max2min_time
);
305 ret
= regmap_update_bits(data
->regmap
, BMA400_TAP_CONFIG1
,
306 BMA400_TAP_TICSTH_MSK
,
307 FIELD_PREP(BMA400_TAP_TICSTH_MSK
, raw
));
314 static IIO_DEVICE_ATTR_RW(in_accel_gesture_tap_maxtomin_time
, 0);
317 * Tap interrupts works with 200 Hz input data rate and the time based tap
318 * controls are in the terms of data samples so the below calculation is
319 * used to convert the configuration values into seconds.
321 * 60 data samples * 0.005 ms = 0.3 seconds.
322 * 80 data samples * 0.005 ms = 0.4 seconds.
325 /* quiet configuration values in seconds */
326 static IIO_CONST_ATTR(in_accel_gesture_tap_reset_timeout_available
,
329 /* tics_th configuration values in seconds */
330 static IIO_CONST_ATTR(in_accel_gesture_tap_maxtomin_time_available
,
331 "0.03 0.045 0.06 0.09");
333 /* quiet_dt configuration values in seconds */
334 static IIO_CONST_ATTR(in_accel_gesture_doubletap_tap2_min_delay_available
,
335 "0.02 0.04 0.06 0.08");
337 /* List of sensitivity values available to configure tap interrupts */
338 static IIO_CONST_ATTR(in_accel_gesture_tap_value_available
, "0 1 2 3 4 5 6 7");
340 static struct attribute
*bma400_event_attributes
[] = {
341 &iio_const_attr_in_accel_gesture_tap_value_available
.dev_attr
.attr
,
342 &iio_const_attr_in_accel_gesture_tap_reset_timeout_available
.dev_attr
.attr
,
343 &iio_const_attr_in_accel_gesture_tap_maxtomin_time_available
.dev_attr
.attr
,
344 &iio_const_attr_in_accel_gesture_doubletap_tap2_min_delay_available
.dev_attr
.attr
,
345 &iio_dev_attr_in_accel_gesture_tap_maxtomin_time
.dev_attr
.attr
,
349 static const struct attribute_group bma400_event_attribute_group
= {
350 .attrs
= bma400_event_attributes
,
353 #define BMA400_ACC_CHANNEL(_index, _axis) { \
356 .channel2 = IIO_MOD_##_axis, \
357 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
358 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
359 BIT(IIO_CHAN_INFO_SCALE) | \
360 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
361 .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
362 BIT(IIO_CHAN_INFO_SCALE) | \
363 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
364 .ext_info = bma400_ext_info, \
365 .scan_index = _index, \
370 .endianness = IIO_LE, \
372 .event_spec = bma400_accel_event, \
373 .num_event_specs = ARRAY_SIZE(bma400_accel_event) \
376 #define BMA400_ACTIVITY_CHANNEL(_chan2) { \
377 .type = IIO_ACTIVITY, \
379 .channel2 = _chan2, \
380 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
381 .scan_index = -1, /* No buffer support */ \
382 .event_spec = &bma400_activity_event, \
383 .num_event_specs = 1, \
386 static const struct iio_chan_spec bma400_channels
[] = {
387 BMA400_ACC_CHANNEL(0, X
),
388 BMA400_ACC_CHANNEL(1, Y
),
389 BMA400_ACC_CHANNEL(2, Z
),
392 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
393 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
399 .endianness
= IIO_LE
,
404 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
405 BIT(IIO_CHAN_INFO_ENABLE
),
406 .scan_index
= -1, /* No buffer support */
407 .event_spec
= &bma400_step_detect_event
,
408 .num_event_specs
= 1,
410 BMA400_ACTIVITY_CHANNEL(IIO_MOD_STILL
),
411 BMA400_ACTIVITY_CHANNEL(IIO_MOD_WALKING
),
412 BMA400_ACTIVITY_CHANNEL(IIO_MOD_RUNNING
),
413 IIO_CHAN_SOFT_TIMESTAMP(4),
416 static int bma400_get_temp_reg(struct bma400_data
*data
, int *val
, int *val2
)
418 unsigned int raw_temp
;
422 if (data
->power_mode
== POWER_MODE_SLEEP
)
425 ret
= regmap_read(data
->regmap
, BMA400_TEMP_DATA_REG
, &raw_temp
);
429 host_temp
= sign_extend32(raw_temp
, 7);
431 * The formula for the TEMP_DATA register in the datasheet
434 *val
= (host_temp
>> 1) + 23;
435 *val2
= (host_temp
& 0x1) * 500000;
436 return IIO_VAL_INT_PLUS_MICRO
;
439 static int bma400_get_accel_reg(struct bma400_data
*data
,
440 const struct iio_chan_spec
*chan
,
447 if (data
->power_mode
== POWER_MODE_SLEEP
)
450 switch (chan
->channel2
) {
452 lsb_reg
= BMA400_X_AXIS_LSB_REG
;
455 lsb_reg
= BMA400_Y_AXIS_LSB_REG
;
458 lsb_reg
= BMA400_Z_AXIS_LSB_REG
;
461 dev_err(data
->dev
, "invalid axis channel modifier\n");
465 /* bulk read two registers, with the base being the LSB register */
466 ret
= regmap_bulk_read(data
->regmap
, lsb_reg
, &raw_accel
,
471 *val
= sign_extend32(le16_to_cpu(raw_accel
), 11);
475 static void bma400_output_data_rate_from_raw(int raw
, unsigned int *val
,
478 *val
= BMA400_ACC_ODR_MAX_HZ
>> (BMA400_ACC_ODR_MAX_RAW
- raw
);
479 if (raw
> BMA400_ACC_ODR_MIN_RAW
)
485 static int bma400_get_accel_output_data_rate(struct bma400_data
*data
)
491 switch (data
->power_mode
) {
494 * Runs at a fixed rate in low-power mode. See section 4.3
497 bma400_output_data_rate_from_raw(BMA400_ACC_ODR_LP_RAW
,
498 &data
->sample_freq
.hz
,
499 &data
->sample_freq
.uhz
);
501 case POWER_MODE_NORMAL
:
503 * In normal mode the ODR can be found in the ACC_CONFIG1
506 ret
= regmap_read(data
->regmap
, BMA400_ACC_CONFIG1_REG
, &val
);
510 odr
= val
& BMA400_ACC_ODR_MASK
;
511 if (odr
< BMA400_ACC_ODR_MIN_RAW
||
512 odr
> BMA400_ACC_ODR_MAX_RAW
) {
517 bma400_output_data_rate_from_raw(odr
, &data
->sample_freq
.hz
,
518 &data
->sample_freq
.uhz
);
520 case POWER_MODE_SLEEP
:
521 data
->sample_freq
.hz
= 0;
522 data
->sample_freq
.uhz
= 0;
529 data
->sample_freq
.hz
= -1;
530 data
->sample_freq
.uhz
= -1;
534 static int bma400_set_accel_output_data_rate(struct bma400_data
*data
,
542 if (hz
>= BMA400_ACC_ODR_MIN_WHOLE_HZ
) {
543 if (uhz
|| hz
> BMA400_ACC_ODR_MAX_HZ
)
546 /* Note this works because MIN_WHOLE_HZ is odd */
549 if (hz
>> idx
!= BMA400_ACC_ODR_MIN_WHOLE_HZ
)
552 idx
+= BMA400_ACC_ODR_MIN_RAW
+ 1;
553 } else if (hz
== BMA400_ACC_ODR_MIN_HZ
&& uhz
== 500000) {
554 idx
= BMA400_ACC_ODR_MIN_RAW
;
559 ret
= regmap_read(data
->regmap
, BMA400_ACC_CONFIG1_REG
, &val
);
563 /* preserve the range and normal mode osr */
564 odr
= (~BMA400_ACC_ODR_MASK
& val
) | idx
;
566 ret
= regmap_write(data
->regmap
, BMA400_ACC_CONFIG1_REG
, odr
);
570 bma400_output_data_rate_from_raw(idx
, &data
->sample_freq
.hz
,
571 &data
->sample_freq
.uhz
);
575 static int bma400_get_accel_oversampling_ratio(struct bma400_data
*data
)
582 * The oversampling ratio is stored in a different register
583 * based on the power-mode. In normal mode the OSR is stored
584 * in ACC_CONFIG1. In low-power mode it is stored in
587 switch (data
->power_mode
) {
589 ret
= regmap_read(data
->regmap
, BMA400_ACC_CONFIG0_REG
, &val
);
591 data
->oversampling_ratio
= -1;
595 osr
= (val
& BMA400_LP_OSR_MASK
) >> BMA400_LP_OSR_SHIFT
;
597 data
->oversampling_ratio
= osr
;
599 case POWER_MODE_NORMAL
:
600 ret
= regmap_read(data
->regmap
, BMA400_ACC_CONFIG1_REG
, &val
);
602 data
->oversampling_ratio
= -1;
606 osr
= (val
& BMA400_NP_OSR_MASK
) >> BMA400_NP_OSR_SHIFT
;
608 data
->oversampling_ratio
= osr
;
610 case POWER_MODE_SLEEP
:
611 data
->oversampling_ratio
= 0;
614 data
->oversampling_ratio
= -1;
619 static int bma400_set_accel_oversampling_ratio(struct bma400_data
*data
,
622 unsigned int acc_config
;
625 if (val
& ~BMA400_TWO_BITS_MASK
)
629 * The oversampling ratio is stored in a different register
630 * based on the power-mode.
632 switch (data
->power_mode
) {
634 ret
= regmap_read(data
->regmap
, BMA400_ACC_CONFIG0_REG
,
639 ret
= regmap_write(data
->regmap
, BMA400_ACC_CONFIG0_REG
,
640 (acc_config
& ~BMA400_LP_OSR_MASK
) |
641 (val
<< BMA400_LP_OSR_SHIFT
));
643 dev_err(data
->dev
, "Failed to write out OSR\n");
647 data
->oversampling_ratio
= val
;
649 case POWER_MODE_NORMAL
:
650 ret
= regmap_read(data
->regmap
, BMA400_ACC_CONFIG1_REG
,
655 ret
= regmap_write(data
->regmap
, BMA400_ACC_CONFIG1_REG
,
656 (acc_config
& ~BMA400_NP_OSR_MASK
) |
657 (val
<< BMA400_NP_OSR_SHIFT
));
659 dev_err(data
->dev
, "Failed to write out OSR\n");
663 data
->oversampling_ratio
= val
;
671 static int bma400_accel_scale_to_raw(struct bma400_data
*data
,
679 /* Note this works because BMA400_SCALE_MIN is odd */
682 if (val
>> raw
!= BMA400_SCALE_MIN
)
688 static int bma400_get_accel_scale(struct bma400_data
*data
)
690 unsigned int raw_scale
;
694 ret
= regmap_read(data
->regmap
, BMA400_ACC_CONFIG1_REG
, &val
);
698 raw_scale
= (val
& BMA400_ACC_SCALE_MASK
) >> BMA400_SCALE_SHIFT
;
699 if (raw_scale
> BMA400_TWO_BITS_MASK
)
702 data
->scale
= BMA400_SCALE_MIN
<< raw_scale
;
707 static int bma400_set_accel_scale(struct bma400_data
*data
, unsigned int val
)
709 unsigned int acc_config
;
713 ret
= regmap_read(data
->regmap
, BMA400_ACC_CONFIG1_REG
, &acc_config
);
717 raw
= bma400_accel_scale_to_raw(data
, val
);
721 ret
= regmap_write(data
->regmap
, BMA400_ACC_CONFIG1_REG
,
722 (acc_config
& ~BMA400_ACC_SCALE_MASK
) |
723 (raw
<< BMA400_SCALE_SHIFT
));
731 static int bma400_get_power_mode(struct bma400_data
*data
)
736 ret
= regmap_read(data
->regmap
, BMA400_STATUS_REG
, &val
);
738 dev_err(data
->dev
, "Failed to read status register\n");
742 data
->power_mode
= (val
>> 1) & BMA400_TWO_BITS_MASK
;
746 static int bma400_set_power_mode(struct bma400_data
*data
,
747 enum bma400_power_mode mode
)
752 ret
= regmap_read(data
->regmap
, BMA400_ACC_CONFIG0_REG
, &val
);
756 if (data
->power_mode
== mode
)
759 if (mode
== POWER_MODE_INVALID
)
762 /* Preserve the low-power oversample ratio etc */
763 ret
= regmap_write(data
->regmap
, BMA400_ACC_CONFIG0_REG
,
764 mode
| (val
& ~BMA400_TWO_BITS_MASK
));
766 dev_err(data
->dev
, "Failed to write to power-mode\n");
770 data
->power_mode
= mode
;
773 * Update our cached osr and odr based on the new
776 bma400_get_accel_output_data_rate(data
);
777 bma400_get_accel_oversampling_ratio(data
);
781 static int bma400_enable_steps(struct bma400_data
*data
, int val
)
785 if (data
->steps_enabled
== val
)
788 ret
= regmap_update_bits(data
->regmap
, BMA400_INT_CONFIG1_REG
,
790 FIELD_PREP(BMA400_STEP_INT_MSK
, val
? 1 : 0));
793 data
->steps_enabled
= val
;
797 static int bma400_get_steps_reg(struct bma400_data
*data
, int *val
)
801 u8
*steps_raw
__free(kfree
) = kmalloc(BMA400_STEP_RAW_LEN
, GFP_KERNEL
);
805 ret
= regmap_bulk_read(data
->regmap
, BMA400_STEP_CNT0_REG
,
806 steps_raw
, BMA400_STEP_RAW_LEN
);
810 *val
= get_unaligned_le24(steps_raw
);
815 static void bma400_init_tables(void)
820 for (i
= 0; i
+ 1 < ARRAY_SIZE(bma400_sample_freqs
); i
+= 2) {
822 bma400_output_data_rate_from_raw(raw
, &bma400_sample_freqs
[i
],
823 &bma400_sample_freqs
[i
+ 1]);
826 for (i
= 0; i
+ 1 < ARRAY_SIZE(bma400_scales
); i
+= 2) {
828 bma400_scales
[i
] = 0;
829 bma400_scales
[i
+ 1] = BMA400_SCALE_MIN
<< raw
;
833 static void bma400_power_disable(void *data_ptr
)
835 struct bma400_data
*data
= data_ptr
;
838 mutex_lock(&data
->mutex
);
839 ret
= bma400_set_power_mode(data
, POWER_MODE_SLEEP
);
840 mutex_unlock(&data
->mutex
);
842 dev_warn(data
->dev
, "Failed to put device into sleep mode (%pe)\n",
846 static enum iio_modifier
bma400_act_to_mod(enum bma400_activity activity
)
850 return IIO_MOD_STILL
;
852 return IIO_MOD_WALKING
;
854 return IIO_MOD_RUNNING
;
860 static int bma400_init(struct bma400_data
*data
)
862 static const char * const regulator_names
[] = { "vdd", "vddio" };
866 ret
= devm_regulator_bulk_get_enable(data
->dev
,
867 ARRAY_SIZE(regulator_names
),
870 return dev_err_probe(data
->dev
, ret
, "Failed to get regulators\n");
872 /* Try to read chip_id register. It must return 0x90. */
873 ret
= regmap_read(data
->regmap
, BMA400_CHIP_ID_REG
, &val
);
875 dev_err(data
->dev
, "Failed to read chip id register\n");
879 if (val
!= BMA400_ID_REG_VAL
) {
880 dev_err(data
->dev
, "Chip ID mismatch\n");
884 ret
= bma400_get_power_mode(data
);
886 dev_err(data
->dev
, "Failed to get the initial power-mode\n");
890 if (data
->power_mode
!= POWER_MODE_NORMAL
) {
891 ret
= bma400_set_power_mode(data
, POWER_MODE_NORMAL
);
893 dev_err(data
->dev
, "Failed to wake up the device\n");
897 * TODO: The datasheet waits 1500us here in the example, but
898 * lists 2/ODR as the wakeup time.
900 usleep_range(1500, 2000);
903 ret
= devm_add_action_or_reset(data
->dev
, bma400_power_disable
, data
);
907 bma400_init_tables();
909 ret
= bma400_get_accel_output_data_rate(data
);
913 ret
= bma400_get_accel_oversampling_ratio(data
);
917 ret
= bma400_get_accel_scale(data
);
921 /* Configure INT1 pin to open drain */
922 ret
= regmap_write(data
->regmap
, BMA400_INT_IO_CTRL_REG
, 0x06);
926 * Once the interrupt engine is supported we might use the
927 * data_src_reg, but for now ensure this is set to the
928 * variable ODR filter selectable by the sample frequency
931 return regmap_write(data
->regmap
, BMA400_ACC_CONFIG2_REG
, 0x00);
934 static int bma400_read_raw(struct iio_dev
*indio_dev
,
935 struct iio_chan_spec
const *chan
, int *val
,
936 int *val2
, long mask
)
938 struct bma400_data
*data
= iio_priv(indio_dev
);
939 unsigned int activity
;
943 case IIO_CHAN_INFO_PROCESSED
:
944 switch (chan
->type
) {
946 mutex_lock(&data
->mutex
);
947 ret
= bma400_get_temp_reg(data
, val
, val2
);
948 mutex_unlock(&data
->mutex
);
951 return bma400_get_steps_reg(data
, val
);
953 ret
= regmap_read(data
->regmap
, BMA400_STEP_STAT_REG
,
958 * The device does not support confidence value levels,
959 * so we will always have 100% for current activity and
962 if (chan
->channel2
== bma400_act_to_mod(activity
))
970 case IIO_CHAN_INFO_RAW
:
971 mutex_lock(&data
->mutex
);
972 ret
= bma400_get_accel_reg(data
, chan
, val
);
973 mutex_unlock(&data
->mutex
);
975 case IIO_CHAN_INFO_SAMP_FREQ
:
976 switch (chan
->type
) {
978 if (data
->sample_freq
.hz
< 0)
981 *val
= data
->sample_freq
.hz
;
982 *val2
= data
->sample_freq
.uhz
;
983 return IIO_VAL_INT_PLUS_MICRO
;
986 * Runs at a fixed sampling frequency. See Section 4.4
991 return IIO_VAL_INT_PLUS_MICRO
;
995 case IIO_CHAN_INFO_SCALE
:
998 return IIO_VAL_INT_PLUS_MICRO
;
999 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
1001 * TODO: We could avoid this logic and returning -EINVAL here if
1002 * we set both the low-power and normal mode OSR registers when
1003 * we configure the device.
1005 if (data
->oversampling_ratio
< 0)
1008 *val
= data
->oversampling_ratio
;
1010 case IIO_CHAN_INFO_ENABLE
:
1011 *val
= data
->steps_enabled
;
1018 static int bma400_read_avail(struct iio_dev
*indio_dev
,
1019 struct iio_chan_spec
const *chan
,
1020 const int **vals
, int *type
, int *length
,
1024 case IIO_CHAN_INFO_SCALE
:
1025 *type
= IIO_VAL_INT_PLUS_MICRO
;
1026 *vals
= bma400_scales
;
1027 *length
= ARRAY_SIZE(bma400_scales
);
1028 return IIO_AVAIL_LIST
;
1029 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
1030 *type
= IIO_VAL_INT
;
1031 *vals
= bma400_osr_range
;
1032 *length
= ARRAY_SIZE(bma400_osr_range
);
1033 return IIO_AVAIL_RANGE
;
1034 case IIO_CHAN_INFO_SAMP_FREQ
:
1035 *type
= IIO_VAL_INT_PLUS_MICRO
;
1036 *vals
= bma400_sample_freqs
;
1037 *length
= ARRAY_SIZE(bma400_sample_freqs
);
1038 return IIO_AVAIL_LIST
;
1044 static int bma400_write_raw(struct iio_dev
*indio_dev
,
1045 struct iio_chan_spec
const *chan
, int val
, int val2
,
1048 struct bma400_data
*data
= iio_priv(indio_dev
);
1052 case IIO_CHAN_INFO_SAMP_FREQ
:
1054 * The sample frequency is readonly for the temperature
1055 * register and a fixed value in low-power mode.
1057 if (chan
->type
!= IIO_ACCEL
)
1060 mutex_lock(&data
->mutex
);
1061 ret
= bma400_set_accel_output_data_rate(data
, val
, val2
);
1062 mutex_unlock(&data
->mutex
);
1064 case IIO_CHAN_INFO_SCALE
:
1066 val2
< BMA400_SCALE_MIN
|| val2
> BMA400_SCALE_MAX
)
1069 mutex_lock(&data
->mutex
);
1070 ret
= bma400_set_accel_scale(data
, val2
);
1071 mutex_unlock(&data
->mutex
);
1073 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
1074 mutex_lock(&data
->mutex
);
1075 ret
= bma400_set_accel_oversampling_ratio(data
, val
);
1076 mutex_unlock(&data
->mutex
);
1078 case IIO_CHAN_INFO_ENABLE
:
1079 mutex_lock(&data
->mutex
);
1080 ret
= bma400_enable_steps(data
, val
);
1081 mutex_unlock(&data
->mutex
);
1088 static int bma400_write_raw_get_fmt(struct iio_dev
*indio_dev
,
1089 struct iio_chan_spec
const *chan
,
1093 case IIO_CHAN_INFO_SAMP_FREQ
:
1094 return IIO_VAL_INT_PLUS_MICRO
;
1095 case IIO_CHAN_INFO_SCALE
:
1096 return IIO_VAL_INT_PLUS_MICRO
;
1097 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
1099 case IIO_CHAN_INFO_ENABLE
:
1106 static int bma400_read_event_config(struct iio_dev
*indio_dev
,
1107 const struct iio_chan_spec
*chan
,
1108 enum iio_event_type type
,
1109 enum iio_event_direction dir
)
1111 struct bma400_data
*data
= iio_priv(indio_dev
);
1113 switch (chan
->type
) {
1116 case IIO_EV_DIR_RISING
:
1117 return FIELD_GET(BMA400_INT_GEN1_MSK
,
1118 data
->generic_event_en
);
1119 case IIO_EV_DIR_FALLING
:
1120 return FIELD_GET(BMA400_INT_GEN2_MSK
,
1121 data
->generic_event_en
);
1122 case IIO_EV_DIR_SINGLETAP
:
1123 return FIELD_GET(BMA400_S_TAP_MSK
,
1124 data
->tap_event_en_bitmask
);
1125 case IIO_EV_DIR_DOUBLETAP
:
1126 return FIELD_GET(BMA400_D_TAP_MSK
,
1127 data
->tap_event_en_bitmask
);
1132 return data
->step_event_en
;
1134 return data
->activity_event_en
;
1140 static int bma400_steps_event_enable(struct bma400_data
*data
, int state
)
1144 ret
= bma400_enable_steps(data
, 1);
1148 ret
= regmap_update_bits(data
->regmap
, BMA400_INT12_MAP_REG
,
1149 BMA400_STEP_INT_MSK
,
1150 FIELD_PREP(BMA400_STEP_INT_MSK
,
1154 data
->step_event_en
= state
;
1158 static int bma400_activity_event_en(struct bma400_data
*data
,
1159 enum iio_event_direction dir
,
1162 int ret
, reg
, msk
, value
;
1163 int field_value
= 0;
1166 case IIO_EV_DIR_RISING
:
1167 reg
= BMA400_GEN1INT_CONFIG0
;
1168 msk
= BMA400_INT_GEN1_MSK
;
1170 set_mask_bits(&field_value
, BMA400_INT_GEN1_MSK
,
1171 FIELD_PREP(BMA400_INT_GEN1_MSK
, state
));
1173 case IIO_EV_DIR_FALLING
:
1174 reg
= BMA400_GEN2INT_CONFIG0
;
1175 msk
= BMA400_INT_GEN2_MSK
;
1177 set_mask_bits(&field_value
, BMA400_INT_GEN2_MSK
,
1178 FIELD_PREP(BMA400_INT_GEN2_MSK
, state
));
1184 /* Enabling all axis for interrupt evaluation */
1185 ret
= regmap_write(data
->regmap
, reg
, 0xF8);
1189 /* OR combination of all axis for interrupt evaluation */
1190 ret
= regmap_write(data
->regmap
, reg
+ BMA400_GEN_CONFIG1_OFF
, value
);
1194 /* Initial value to avoid interrupts while enabling*/
1195 ret
= regmap_write(data
->regmap
, reg
+ BMA400_GEN_CONFIG2_OFF
, 0x0A);
1199 /* Initial duration value to avoid interrupts while enabling*/
1200 ret
= regmap_write(data
->regmap
, reg
+ BMA400_GEN_CONFIG31_OFF
, 0x0F);
1204 ret
= regmap_update_bits(data
->regmap
, BMA400_INT1_MAP_REG
, msk
,
1209 ret
= regmap_update_bits(data
->regmap
, BMA400_INT_CONFIG0_REG
, msk
,
1214 set_mask_bits(&data
->generic_event_en
, msk
, field_value
);
1218 static int bma400_tap_event_en(struct bma400_data
*data
,
1219 enum iio_event_direction dir
, int state
)
1222 unsigned int field_value
= 0;
1226 * Tap interrupts can be configured only in normal mode.
1227 * See table in section 4.3 "Power modes - performance modes" of
1230 if (data
->power_mode
!= POWER_MODE_NORMAL
)
1234 * Tap interrupts are operating with a data rate of 200Hz.
1235 * See section 4.7 "Tap sensing interrupt" in datasheet v1.2.
1237 if (data
->sample_freq
.hz
!= 200 && state
) {
1238 dev_err(data
->dev
, "Invalid data rate for tap interrupts.\n");
1242 ret
= regmap_update_bits(data
->regmap
, BMA400_INT12_MAP_REG
,
1244 FIELD_PREP(BMA400_S_TAP_MSK
, state
));
1249 case IIO_EV_DIR_SINGLETAP
:
1250 mask
= BMA400_S_TAP_MSK
;
1251 set_mask_bits(&field_value
, BMA400_S_TAP_MSK
,
1252 FIELD_PREP(BMA400_S_TAP_MSK
, state
));
1254 case IIO_EV_DIR_DOUBLETAP
:
1255 mask
= BMA400_D_TAP_MSK
;
1256 set_mask_bits(&field_value
, BMA400_D_TAP_MSK
,
1257 FIELD_PREP(BMA400_D_TAP_MSK
, state
));
1263 ret
= regmap_update_bits(data
->regmap
, BMA400_INT_CONFIG1_REG
, mask
,
1268 set_mask_bits(&data
->tap_event_en_bitmask
, mask
, field_value
);
1273 static int bma400_disable_adv_interrupt(struct bma400_data
*data
)
1277 ret
= regmap_write(data
->regmap
, BMA400_INT_CONFIG0_REG
, 0);
1281 ret
= regmap_write(data
->regmap
, BMA400_INT_CONFIG1_REG
, 0);
1285 data
->tap_event_en_bitmask
= 0;
1286 data
->generic_event_en
= 0;
1287 data
->step_event_en
= false;
1288 data
->activity_event_en
= false;
1293 static int bma400_write_event_config(struct iio_dev
*indio_dev
,
1294 const struct iio_chan_spec
*chan
,
1295 enum iio_event_type type
,
1296 enum iio_event_direction dir
, bool state
)
1298 struct bma400_data
*data
= iio_priv(indio_dev
);
1301 switch (chan
->type
) {
1304 case IIO_EV_TYPE_MAG
:
1305 mutex_lock(&data
->mutex
);
1306 ret
= bma400_activity_event_en(data
, dir
, state
);
1307 mutex_unlock(&data
->mutex
);
1309 case IIO_EV_TYPE_GESTURE
:
1310 mutex_lock(&data
->mutex
);
1311 ret
= bma400_tap_event_en(data
, dir
, state
);
1312 mutex_unlock(&data
->mutex
);
1318 mutex_lock(&data
->mutex
);
1319 ret
= bma400_steps_event_enable(data
, state
);
1320 mutex_unlock(&data
->mutex
);
1323 mutex_lock(&data
->mutex
);
1324 if (!data
->step_event_en
) {
1325 ret
= bma400_steps_event_enable(data
, true);
1327 mutex_unlock(&data
->mutex
);
1331 data
->activity_event_en
= state
;
1332 mutex_unlock(&data
->mutex
);
1339 static int get_gen_config_reg(enum iio_event_direction dir
)
1342 case IIO_EV_DIR_FALLING
:
1343 return BMA400_GEN2INT_CONFIG0
;
1344 case IIO_EV_DIR_RISING
:
1345 return BMA400_GEN1INT_CONFIG0
;
1351 static int bma400_read_event_value(struct iio_dev
*indio_dev
,
1352 const struct iio_chan_spec
*chan
,
1353 enum iio_event_type type
,
1354 enum iio_event_direction dir
,
1355 enum iio_event_info info
,
1356 int *val
, int *val2
)
1358 struct bma400_data
*data
= iio_priv(indio_dev
);
1359 int ret
, reg
, reg_val
, raw
;
1361 if (chan
->type
!= IIO_ACCEL
)
1365 case IIO_EV_TYPE_MAG
:
1366 reg
= get_gen_config_reg(dir
);
1372 case IIO_EV_INFO_VALUE
:
1373 ret
= regmap_read(data
->regmap
,
1374 reg
+ BMA400_GEN_CONFIG2_OFF
,
1379 case IIO_EV_INFO_PERIOD
:
1380 mutex_lock(&data
->mutex
);
1381 ret
= regmap_bulk_read(data
->regmap
,
1382 reg
+ BMA400_GEN_CONFIG3_OFF
,
1384 sizeof(data
->duration
));
1386 mutex_unlock(&data
->mutex
);
1389 *val
= be16_to_cpu(data
->duration
);
1390 mutex_unlock(&data
->mutex
);
1392 case IIO_EV_INFO_HYSTERESIS
:
1393 ret
= regmap_read(data
->regmap
, reg
, val
);
1396 *val
= FIELD_GET(BMA400_GEN_HYST_MSK
, *val
);
1401 case IIO_EV_TYPE_GESTURE
:
1403 case IIO_EV_INFO_VALUE
:
1404 ret
= regmap_read(data
->regmap
, BMA400_TAP_CONFIG
,
1409 *val
= FIELD_GET(BMA400_TAP_SEN_MSK
, reg_val
);
1411 case IIO_EV_INFO_RESET_TIMEOUT
:
1412 ret
= regmap_read(data
->regmap
, BMA400_TAP_CONFIG1
,
1417 raw
= FIELD_GET(BMA400_TAP_QUIET_MSK
, reg_val
);
1419 *val2
= tap_reset_timeout
[raw
];
1420 return IIO_VAL_INT_PLUS_MICRO
;
1421 case IIO_EV_INFO_TAP2_MIN_DELAY
:
1422 ret
= regmap_read(data
->regmap
, BMA400_TAP_CONFIG1
,
1427 raw
= FIELD_GET(BMA400_TAP_QUIETDT_MSK
, reg_val
);
1429 *val2
= double_tap2_min_delay
[raw
];
1430 return IIO_VAL_INT_PLUS_MICRO
;
1439 static int bma400_write_event_value(struct iio_dev
*indio_dev
,
1440 const struct iio_chan_spec
*chan
,
1441 enum iio_event_type type
,
1442 enum iio_event_direction dir
,
1443 enum iio_event_info info
,
1446 struct bma400_data
*data
= iio_priv(indio_dev
);
1449 if (chan
->type
!= IIO_ACCEL
)
1453 case IIO_EV_TYPE_MAG
:
1454 reg
= get_gen_config_reg(dir
);
1459 case IIO_EV_INFO_VALUE
:
1460 if (val
< 1 || val
> 255)
1463 return regmap_write(data
->regmap
,
1464 reg
+ BMA400_GEN_CONFIG2_OFF
,
1466 case IIO_EV_INFO_PERIOD
:
1467 if (val
< 1 || val
> 65535)
1470 mutex_lock(&data
->mutex
);
1471 put_unaligned_be16(val
, &data
->duration
);
1472 ret
= regmap_bulk_write(data
->regmap
,
1473 reg
+ BMA400_GEN_CONFIG3_OFF
,
1475 sizeof(data
->duration
));
1476 mutex_unlock(&data
->mutex
);
1478 case IIO_EV_INFO_HYSTERESIS
:
1479 if (val
< 0 || val
> 3)
1482 return regmap_update_bits(data
->regmap
, reg
,
1483 BMA400_GEN_HYST_MSK
,
1484 FIELD_PREP(BMA400_GEN_HYST_MSK
,
1489 case IIO_EV_TYPE_GESTURE
:
1491 case IIO_EV_INFO_VALUE
:
1492 if (val
< 0 || val
> 7)
1495 return regmap_update_bits(data
->regmap
,
1498 FIELD_PREP(BMA400_TAP_SEN_MSK
,
1500 case IIO_EV_INFO_RESET_TIMEOUT
:
1501 raw
= usec_to_tapreg_raw(val2
, tap_reset_timeout
);
1505 return regmap_update_bits(data
->regmap
,
1507 BMA400_TAP_QUIET_MSK
,
1508 FIELD_PREP(BMA400_TAP_QUIET_MSK
,
1510 case IIO_EV_INFO_TAP2_MIN_DELAY
:
1511 raw
= usec_to_tapreg_raw(val2
, double_tap2_min_delay
);
1515 return regmap_update_bits(data
->regmap
,
1517 BMA400_TAP_QUIETDT_MSK
,
1518 FIELD_PREP(BMA400_TAP_QUIETDT_MSK
,
1528 static int bma400_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
1531 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
1532 struct bma400_data
*data
= iio_priv(indio_dev
);
1535 ret
= regmap_update_bits(data
->regmap
, BMA400_INT_CONFIG0_REG
,
1536 BMA400_INT_DRDY_MSK
,
1537 FIELD_PREP(BMA400_INT_DRDY_MSK
, state
));
1541 return regmap_update_bits(data
->regmap
, BMA400_INT1_MAP_REG
,
1542 BMA400_INT_DRDY_MSK
,
1543 FIELD_PREP(BMA400_INT_DRDY_MSK
, state
));
1546 static const unsigned long bma400_avail_scan_masks
[] = {
1547 BIT(BMA400_ACCL_X
) | BIT(BMA400_ACCL_Y
) | BIT(BMA400_ACCL_Z
),
1548 BIT(BMA400_ACCL_X
) | BIT(BMA400_ACCL_Y
) | BIT(BMA400_ACCL_Z
)
1553 static const struct iio_info bma400_info
= {
1554 .read_raw
= bma400_read_raw
,
1555 .read_avail
= bma400_read_avail
,
1556 .write_raw
= bma400_write_raw
,
1557 .write_raw_get_fmt
= bma400_write_raw_get_fmt
,
1558 .read_event_config
= bma400_read_event_config
,
1559 .write_event_config
= bma400_write_event_config
,
1560 .write_event_value
= bma400_write_event_value
,
1561 .read_event_value
= bma400_read_event_value
,
1562 .event_attrs
= &bma400_event_attribute_group
,
1565 static const struct iio_trigger_ops bma400_trigger_ops
= {
1566 .set_trigger_state
= &bma400_data_rdy_trigger_set_state
,
1567 .validate_device
= &iio_trigger_validate_own_device
,
1570 static irqreturn_t
bma400_trigger_handler(int irq
, void *p
)
1572 struct iio_poll_func
*pf
= p
;
1573 struct iio_dev
*indio_dev
= pf
->indio_dev
;
1574 struct bma400_data
*data
= iio_priv(indio_dev
);
1577 /* Lock to protect the data->buffer */
1578 mutex_lock(&data
->mutex
);
1580 /* bulk read six registers, with the base being the LSB register */
1581 ret
= regmap_bulk_read(data
->regmap
, BMA400_X_AXIS_LSB_REG
,
1582 &data
->buffer
.buff
, sizeof(data
->buffer
.buff
));
1586 if (test_bit(BMA400_TEMP
, indio_dev
->active_scan_mask
)) {
1587 ret
= regmap_read(data
->regmap
, BMA400_TEMP_DATA_REG
, &temp
);
1591 data
->buffer
.temperature
= temp
;
1594 iio_push_to_buffers_with_timestamp(indio_dev
, &data
->buffer
,
1595 iio_get_time_ns(indio_dev
));
1597 mutex_unlock(&data
->mutex
);
1598 iio_trigger_notify_done(indio_dev
->trig
);
1602 mutex_unlock(&data
->mutex
);
1606 static irqreturn_t
bma400_interrupt(int irq
, void *private)
1608 struct iio_dev
*indio_dev
= private;
1609 struct bma400_data
*data
= iio_priv(indio_dev
);
1610 s64 timestamp
= iio_get_time_ns(indio_dev
);
1611 unsigned int act
, ev_dir
= IIO_EV_DIR_NONE
;
1614 /* Lock to protect the data->status */
1615 mutex_lock(&data
->mutex
);
1616 ret
= regmap_bulk_read(data
->regmap
, BMA400_INT_STAT0_REG
,
1618 sizeof(data
->status
));
1620 * if none of the bit is set in the status register then it is
1621 * spurious interrupt.
1623 if (ret
|| !data
->status
)
1627 * Disable all advance interrupts if interrupt engine overrun occurs.
1628 * See section 4.7 "Interrupt engine overrun" in datasheet v1.2.
1630 if (FIELD_GET(BMA400_INT_ENG_OVRUN_MSK
, le16_to_cpu(data
->status
))) {
1631 bma400_disable_adv_interrupt(data
);
1632 dev_err(data
->dev
, "Interrupt engine overrun\n");
1636 if (FIELD_GET(BMA400_INT_S_TAP_MSK
, le16_to_cpu(data
->status
)))
1637 iio_push_event(indio_dev
,
1638 IIO_MOD_EVENT_CODE(IIO_ACCEL
, 0,
1639 IIO_MOD_X_OR_Y_OR_Z
,
1640 IIO_EV_TYPE_GESTURE
,
1641 IIO_EV_DIR_SINGLETAP
),
1644 if (FIELD_GET(BMA400_INT_D_TAP_MSK
, le16_to_cpu(data
->status
)))
1645 iio_push_event(indio_dev
,
1646 IIO_MOD_EVENT_CODE(IIO_ACCEL
, 0,
1647 IIO_MOD_X_OR_Y_OR_Z
,
1648 IIO_EV_TYPE_GESTURE
,
1649 IIO_EV_DIR_DOUBLETAP
),
1652 if (FIELD_GET(BMA400_INT_GEN1_MSK
, le16_to_cpu(data
->status
)))
1653 ev_dir
= IIO_EV_DIR_RISING
;
1655 if (FIELD_GET(BMA400_INT_GEN2_MSK
, le16_to_cpu(data
->status
)))
1656 ev_dir
= IIO_EV_DIR_FALLING
;
1658 if (ev_dir
!= IIO_EV_DIR_NONE
) {
1659 iio_push_event(indio_dev
,
1660 IIO_MOD_EVENT_CODE(IIO_ACCEL
, 0,
1661 IIO_MOD_X_OR_Y_OR_Z
,
1662 IIO_EV_TYPE_MAG
, ev_dir
),
1666 if (FIELD_GET(BMA400_STEP_STAT_MASK
, le16_to_cpu(data
->status
))) {
1667 iio_push_event(indio_dev
,
1668 IIO_MOD_EVENT_CODE(IIO_STEPS
, 0, IIO_NO_MOD
,
1673 if (data
->activity_event_en
) {
1674 ret
= regmap_read(data
->regmap
, BMA400_STEP_STAT_REG
,
1679 iio_push_event(indio_dev
,
1680 IIO_MOD_EVENT_CODE(IIO_ACTIVITY
, 0,
1681 bma400_act_to_mod(act
),
1688 if (FIELD_GET(BMA400_INT_DRDY_MSK
, le16_to_cpu(data
->status
))) {
1689 mutex_unlock(&data
->mutex
);
1690 iio_trigger_poll_nested(data
->trig
);
1694 mutex_unlock(&data
->mutex
);
1698 mutex_unlock(&data
->mutex
);
1702 int bma400_probe(struct device
*dev
, struct regmap
*regmap
, int irq
,
1705 struct iio_dev
*indio_dev
;
1706 struct bma400_data
*data
;
1709 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
1713 data
= iio_priv(indio_dev
);
1714 data
->regmap
= regmap
;
1717 ret
= bma400_init(data
);
1721 ret
= iio_read_mount_matrix(dev
, &data
->orientation
);
1725 mutex_init(&data
->mutex
);
1726 indio_dev
->name
= name
;
1727 indio_dev
->info
= &bma400_info
;
1728 indio_dev
->channels
= bma400_channels
;
1729 indio_dev
->num_channels
= ARRAY_SIZE(bma400_channels
);
1730 indio_dev
->available_scan_masks
= bma400_avail_scan_masks
;
1731 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1734 data
->trig
= devm_iio_trigger_alloc(dev
, "%s-dev%d",
1736 iio_device_id(indio_dev
));
1740 data
->trig
->ops
= &bma400_trigger_ops
;
1741 iio_trigger_set_drvdata(data
->trig
, indio_dev
);
1743 ret
= devm_iio_trigger_register(data
->dev
, data
->trig
);
1745 return dev_err_probe(data
->dev
, ret
,
1746 "iio trigger register fail\n");
1748 indio_dev
->trig
= iio_trigger_get(data
->trig
);
1749 ret
= devm_request_threaded_irq(dev
, irq
, NULL
,
1751 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
1752 indio_dev
->name
, indio_dev
);
1754 return dev_err_probe(data
->dev
, ret
,
1755 "request irq %d failed\n", irq
);
1758 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
, NULL
,
1759 &bma400_trigger_handler
, NULL
);
1761 return dev_err_probe(data
->dev
, ret
,
1762 "iio triggered buffer setup failed\n");
1764 return devm_iio_device_register(dev
, indio_dev
);
1766 EXPORT_SYMBOL_NS(bma400_probe
, "IIO_BMA400");
1768 MODULE_AUTHOR("Dan Robertson <dan@dlrobertson.com>");
1769 MODULE_AUTHOR("Jagath Jog J <jagathjog1996@gmail.com>");
1770 MODULE_DESCRIPTION("Bosch BMA400 triaxial acceleration sensor core");
1771 MODULE_LICENSE("GPL");