1 // SPDX-License-Identifier: GPL-2.0
3 * IIO core driver for Bosch BMI323 6-Axis IMU.
5 * Copyright (C) 2023, Jagath Jog J <jagathjog1996@gmail.com>
7 * Datasheet: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmi323-ds000.pdf
10 #include <linux/bitfield.h>
11 #include <linux/cleanup.h>
12 #include <linux/device.h>
13 #include <linux/interrupt.h>
14 #include <linux/minmax.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/property.h>
18 #include <linux/regmap.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/units.h>
22 #include <linux/unaligned.h>
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/events.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/sysfs.h>
28 #include <linux/iio/trigger.h>
29 #include <linux/iio/trigger_consumer.h>
30 #include <linux/iio/triggered_buffer.h>
34 enum bmi323_sensor_type
{
40 enum bmi323_opr_mode
{
41 ACC_GYRO_MODE_DISABLE
= 0x00,
42 GYRO_DRIVE_MODE_ENABLED
= 0x01,
43 ACC_GYRO_MODE_DUTYCYCLE
= 0x03,
44 ACC_GYRO_MODE_CONTINOUS
= 0x04,
45 ACC_GYRO_MODE_HIGH_PERF
= 0x07,
50 BMI323_BUFFER_DRDY_TRIGGERED
,
78 const int (*scale_table
)[2];
83 * The accelerometer supports +-2G/4G/8G/16G ranges, and the resolution of
84 * each sample is 16 bits, signed.
85 * At +-8G the scale can calculated by
86 * ((8 + 8) * 9.80665 / (2^16 - 1)) * 10^6 = 2394.23819 scale in micro
89 static const int bmi323_accel_scale
[][2] = {
96 static const int bmi323_gyro_scale
[][2] = {
104 static const int bmi323_accel_gyro_avrg
[] = {0, 2, 4, 8, 16, 32, 64};
106 static const struct bmi323_hw bmi323_hw
[2] = {
108 .data
= BMI323_ACCEL_X_REG
,
109 .config
= BMI323_ACC_CONF_REG
,
110 .scale_table
= bmi323_accel_scale
,
111 .scale_table_len
= ARRAY_SIZE(bmi323_accel_scale
),
114 .data
= BMI323_GYRO_X_REG
,
115 .config
= BMI323_GYRO_CONF_REG
,
116 .scale_table
= bmi323_gyro_scale
,
117 .scale_table_len
= ARRAY_SIZE(bmi323_gyro_scale
),
121 static const unsigned int bmi323_reg_savestate
[] = {
124 BMI323_IO_INT_CTR_REG
,
125 BMI323_IO_INT_CONF_REG
,
127 BMI323_GYRO_CONF_REG
,
129 BMI323_FIFO_WTRMRK_REG
,
133 static const unsigned int bmi323_ext_reg_savestate
[] = {
138 BMI323_FEAT_IO0_S_TAP_MSK
,
142 BMI323_ANYMO1_REG
+ BMI323_MO2_OFFSET
,
143 BMI323_NOMO1_REG
+ BMI323_MO2_OFFSET
,
144 BMI323_ANYMO1_REG
+ BMI323_MO3_OFFSET
,
145 BMI323_NOMO1_REG
+ BMI323_MO3_OFFSET
148 struct bmi323_regs_runtime_pm
{
149 unsigned int reg_settings
[ARRAY_SIZE(bmi323_reg_savestate
)];
150 unsigned int ext_reg_settings
[ARRAY_SIZE(bmi323_ext_reg_savestate
)];
155 struct regmap
*regmap
;
156 struct iio_mount_matrix orientation
;
157 enum bmi323_irq_pin irq_pin
;
158 struct iio_trigger
*trig
;
159 bool drdy_trigger_enabled
;
160 enum bmi323_state state
;
161 s64 fifo_tstamp
, old_fifo_tstamp
;
162 u32 odrns
[BMI323_SENSORS_CNT
];
163 u32 odrhz
[BMI323_SENSORS_CNT
];
164 unsigned int feature_events
;
165 struct bmi323_regs_runtime_pm runtime_pm_status
;
168 * Lock to protect the members of device's private data from concurrent
169 * access and also to serialize the access of extended registers.
170 * See bmi323_write_ext_reg(..) for more info.
174 __le16 fifo_buff
[BMI323_FIFO_FULL_IN_WORDS
] __aligned(IIO_DMA_MINALIGN
);
176 __le16 channels
[BMI323_CHAN_MAX
];
179 __le16 steps_count
[BMI323_STEP_LEN
];
182 static const struct iio_mount_matrix
*
183 bmi323_get_mount_matrix(const struct iio_dev
*idev
,
184 const struct iio_chan_spec
*chan
)
186 struct bmi323_data
*data
= iio_priv(idev
);
188 return &data
->orientation
;
191 static const struct iio_chan_spec_ext_info bmi323_ext_info
[] = {
192 IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE
, bmi323_get_mount_matrix
),
196 static const struct iio_event_spec bmi323_step_wtrmrk_event
= {
197 .type
= IIO_EV_TYPE_CHANGE
,
198 .dir
= IIO_EV_DIR_NONE
,
199 .mask_shared_by_type
= BIT(IIO_EV_INFO_ENABLE
) |
200 BIT(IIO_EV_INFO_VALUE
),
203 static const struct iio_event_spec bmi323_accel_event
[] = {
205 .type
= IIO_EV_TYPE_MAG
,
206 .dir
= IIO_EV_DIR_FALLING
,
207 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
) |
208 BIT(IIO_EV_INFO_PERIOD
) |
209 BIT(IIO_EV_INFO_HYSTERESIS
) |
210 BIT(IIO_EV_INFO_ENABLE
),
213 .type
= IIO_EV_TYPE_MAG
,
214 .dir
= IIO_EV_DIR_RISING
,
215 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
) |
216 BIT(IIO_EV_INFO_PERIOD
) |
217 BIT(IIO_EV_INFO_HYSTERESIS
) |
218 BIT(IIO_EV_INFO_ENABLE
),
221 .type
= IIO_EV_TYPE_GESTURE
,
222 .dir
= IIO_EV_DIR_SINGLETAP
,
223 .mask_shared_by_type
= BIT(IIO_EV_INFO_ENABLE
) |
224 BIT(IIO_EV_INFO_VALUE
) |
225 BIT(IIO_EV_INFO_RESET_TIMEOUT
),
228 .type
= IIO_EV_TYPE_GESTURE
,
229 .dir
= IIO_EV_DIR_DOUBLETAP
,
230 .mask_shared_by_type
= BIT(IIO_EV_INFO_ENABLE
) |
231 BIT(IIO_EV_INFO_VALUE
) |
232 BIT(IIO_EV_INFO_RESET_TIMEOUT
) |
233 BIT(IIO_EV_INFO_TAP2_MIN_DELAY
),
237 #define BMI323_ACCEL_CHANNEL(_type, _axis, _index) { \
240 .channel2 = IIO_MOD_##_axis, \
241 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
242 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
243 BIT(IIO_CHAN_INFO_SCALE) | \
244 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
245 .info_mask_shared_by_type_available = \
246 BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
247 BIT(IIO_CHAN_INFO_SCALE) | \
248 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
249 .scan_index = _index, \
254 .endianness = IIO_LE, \
256 .ext_info = bmi323_ext_info, \
257 .event_spec = bmi323_accel_event, \
258 .num_event_specs = ARRAY_SIZE(bmi323_accel_event), \
261 #define BMI323_GYRO_CHANNEL(_type, _axis, _index) { \
264 .channel2 = IIO_MOD_##_axis, \
265 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
266 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
267 BIT(IIO_CHAN_INFO_SCALE) | \
268 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
269 .info_mask_shared_by_type_available = \
270 BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
271 BIT(IIO_CHAN_INFO_SCALE) | \
272 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
273 .scan_index = _index, \
278 .endianness = IIO_LE, \
280 .ext_info = bmi323_ext_info, \
283 static const struct iio_chan_spec bmi323_channels
[] = {
284 BMI323_ACCEL_CHANNEL(IIO_ACCEL
, X
, BMI323_ACCEL_X
),
285 BMI323_ACCEL_CHANNEL(IIO_ACCEL
, Y
, BMI323_ACCEL_Y
),
286 BMI323_ACCEL_CHANNEL(IIO_ACCEL
, Z
, BMI323_ACCEL_Z
),
287 BMI323_GYRO_CHANNEL(IIO_ANGL_VEL
, X
, BMI323_GYRO_X
),
288 BMI323_GYRO_CHANNEL(IIO_ANGL_VEL
, Y
, BMI323_GYRO_Y
),
289 BMI323_GYRO_CHANNEL(IIO_ANGL_VEL
, Z
, BMI323_GYRO_Z
),
292 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
293 BIT(IIO_CHAN_INFO_OFFSET
) |
294 BIT(IIO_CHAN_INFO_SCALE
),
299 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
300 BIT(IIO_CHAN_INFO_ENABLE
),
302 .event_spec
= &bmi323_step_wtrmrk_event
,
303 .num_event_specs
= 1,
306 IIO_CHAN_SOFT_TIMESTAMP(BMI323_CHAN_MAX
),
309 static const int bmi323_acc_gyro_odr
[][2] = {
323 static const int bmi323_acc_gyro_odrns
[] = {
337 static enum bmi323_sensor_type
bmi323_iio_to_sensor(enum iio_chan_type iio_type
)
349 static int bmi323_set_mode(struct bmi323_data
*data
,
350 enum bmi323_sensor_type sensor
,
351 enum bmi323_opr_mode mode
)
353 guard(mutex
)(&data
->mutex
);
354 return regmap_update_bits(data
->regmap
, bmi323_hw
[sensor
].config
,
355 BMI323_ACC_GYRO_CONF_MODE_MSK
,
356 FIELD_PREP(BMI323_ACC_GYRO_CONF_MODE_MSK
,
361 * When writing data to extended register there must be no communication to
362 * any other register before write transaction is complete.
363 * See datasheet section 6.2 Extended Register Map Description.
365 static int bmi323_write_ext_reg(struct bmi323_data
*data
, unsigned int ext_addr
,
366 unsigned int ext_data
)
368 int ret
, feature_status
;
370 ret
= regmap_read(data
->regmap
, BMI323_FEAT_DATA_STATUS
,
375 if (!FIELD_GET(BMI323_FEAT_DATA_TX_RDY_MSK
, feature_status
))
378 ret
= regmap_write(data
->regmap
, BMI323_FEAT_DATA_ADDR
, ext_addr
);
382 return regmap_write(data
->regmap
, BMI323_FEAT_DATA_TX
, ext_data
);
386 * When reading data from extended register there must be no communication to
387 * any other register before read transaction is complete.
388 * See datasheet section 6.2 Extended Register Map Description.
390 static int bmi323_read_ext_reg(struct bmi323_data
*data
, unsigned int ext_addr
,
391 unsigned int *ext_data
)
393 int ret
, feature_status
;
395 ret
= regmap_read(data
->regmap
, BMI323_FEAT_DATA_STATUS
,
400 if (!FIELD_GET(BMI323_FEAT_DATA_TX_RDY_MSK
, feature_status
))
403 ret
= regmap_write(data
->regmap
, BMI323_FEAT_DATA_ADDR
, ext_addr
);
407 return regmap_read(data
->regmap
, BMI323_FEAT_DATA_TX
, ext_data
);
410 static int bmi323_update_ext_reg(struct bmi323_data
*data
,
411 unsigned int ext_addr
,
412 unsigned int mask
, unsigned int ext_data
)
417 ret
= bmi323_read_ext_reg(data
, ext_addr
, &value
);
421 set_mask_bits(&value
, mask
, ext_data
);
423 return bmi323_write_ext_reg(data
, ext_addr
, value
);
426 static int bmi323_get_error_status(struct bmi323_data
*data
)
430 guard(mutex
)(&data
->mutex
);
431 ret
= regmap_read(data
->regmap
, BMI323_ERR_REG
, &error
);
436 dev_err(data
->dev
, "Sensor error 0x%x\n", error
);
441 static int bmi323_feature_engine_events(struct bmi323_data
*data
,
442 const unsigned int event_mask
,
448 ret
= regmap_read(data
->regmap
, BMI323_FEAT_IO0_REG
, &value
);
452 /* Register must be cleared before changing an active config */
453 ret
= regmap_write(data
->regmap
, BMI323_FEAT_IO0_REG
, 0);
460 value
&= ~event_mask
;
462 ret
= regmap_write(data
->regmap
, BMI323_FEAT_IO0_REG
, value
);
466 return regmap_write(data
->regmap
, BMI323_FEAT_IO_STATUS_REG
,
467 BMI323_FEAT_IO_STATUS_MSK
);
470 static int bmi323_step_wtrmrk_en(struct bmi323_data
*data
, bool state
)
472 enum bmi323_irq_pin step_irq
;
475 guard(mutex
)(&data
->mutex
);
476 if (!FIELD_GET(BMI323_FEAT_IO0_STP_CNT_MSK
, data
->feature_events
))
480 step_irq
= data
->irq_pin
;
482 step_irq
= BMI323_IRQ_DISABLED
;
484 ret
= bmi323_update_ext_reg(data
, BMI323_STEP_SC1_REG
,
485 BMI323_STEP_SC1_WTRMRK_MSK
,
486 FIELD_PREP(BMI323_STEP_SC1_WTRMRK_MSK
,
491 return regmap_update_bits(data
->regmap
, BMI323_INT_MAP1_REG
,
493 FIELD_PREP(BMI323_STEP_CNT_MSK
, step_irq
));
496 static int bmi323_motion_config_reg(enum iio_event_direction dir
)
499 case IIO_EV_DIR_RISING
:
500 return BMI323_ANYMO1_REG
;
501 case IIO_EV_DIR_FALLING
:
502 return BMI323_NOMO1_REG
;
508 static int bmi323_motion_event_en(struct bmi323_data
*data
,
509 enum iio_event_direction dir
, bool state
)
511 unsigned int state_value
= state
? BMI323_FEAT_XYZ_MSK
: 0;
512 int config
, ret
, msk
, raw
, field_value
;
513 enum bmi323_irq_pin motion_irq
;
514 int irq_msk
, irq_field_val
;
517 motion_irq
= data
->irq_pin
;
519 motion_irq
= BMI323_IRQ_DISABLED
;
522 case IIO_EV_DIR_RISING
:
523 msk
= BMI323_FEAT_IO0_XYZ_MOTION_MSK
;
525 config
= BMI323_ANYMO1_REG
;
526 irq_msk
= BMI323_MOTION_MSK
;
527 irq_field_val
= FIELD_PREP(BMI323_MOTION_MSK
, motion_irq
);
528 field_value
= FIELD_PREP(BMI323_FEAT_IO0_XYZ_MOTION_MSK
,
531 case IIO_EV_DIR_FALLING
:
532 msk
= BMI323_FEAT_IO0_XYZ_NOMOTION_MSK
;
534 config
= BMI323_NOMO1_REG
;
535 irq_msk
= BMI323_NOMOTION_MSK
;
536 irq_field_val
= FIELD_PREP(BMI323_NOMOTION_MSK
, motion_irq
);
537 field_value
= FIELD_PREP(BMI323_FEAT_IO0_XYZ_NOMOTION_MSK
,
544 guard(mutex
)(&data
->mutex
);
545 ret
= bmi323_feature_engine_events(data
, msk
, state
);
549 ret
= bmi323_update_ext_reg(data
, config
,
550 BMI323_MO1_REF_UP_MSK
,
551 FIELD_PREP(BMI323_MO1_REF_UP_MSK
, 0));
555 /* Set initial value to avoid interrupts while enabling*/
556 ret
= bmi323_update_ext_reg(data
, config
,
557 BMI323_MO1_SLOPE_TH_MSK
,
558 FIELD_PREP(BMI323_MO1_SLOPE_TH_MSK
, raw
));
562 ret
= regmap_update_bits(data
->regmap
, BMI323_INT_MAP1_REG
, irq_msk
,
567 set_mask_bits(&data
->feature_events
, msk
, field_value
);
572 static int bmi323_tap_event_en(struct bmi323_data
*data
,
573 enum iio_event_direction dir
, bool state
)
575 enum bmi323_irq_pin tap_irq
;
576 int ret
, tap_enabled
;
578 guard(mutex
)(&data
->mutex
);
580 if (data
->odrhz
[BMI323_ACCEL
] < 200) {
581 dev_err(data
->dev
, "Invalid accelerometer parameter\n");
586 case IIO_EV_DIR_SINGLETAP
:
587 ret
= bmi323_feature_engine_events(data
,
588 BMI323_FEAT_IO0_S_TAP_MSK
,
593 set_mask_bits(&data
->feature_events
, BMI323_FEAT_IO0_S_TAP_MSK
,
594 FIELD_PREP(BMI323_FEAT_IO0_S_TAP_MSK
, state
));
596 case IIO_EV_DIR_DOUBLETAP
:
597 ret
= bmi323_feature_engine_events(data
,
598 BMI323_FEAT_IO0_D_TAP_MSK
,
603 set_mask_bits(&data
->feature_events
, BMI323_FEAT_IO0_D_TAP_MSK
,
604 FIELD_PREP(BMI323_FEAT_IO0_D_TAP_MSK
, state
));
610 tap_enabled
= FIELD_GET(BMI323_FEAT_IO0_S_TAP_MSK
|
611 BMI323_FEAT_IO0_D_TAP_MSK
,
612 data
->feature_events
);
615 tap_irq
= data
->irq_pin
;
617 tap_irq
= BMI323_IRQ_DISABLED
;
619 ret
= regmap_update_bits(data
->regmap
, BMI323_INT_MAP2_REG
,
621 FIELD_PREP(BMI323_TAP_MSK
, tap_irq
));
628 ret
= bmi323_update_ext_reg(data
, BMI323_TAP1_REG
,
629 BMI323_TAP1_MAX_PEAKS_MSK
,
630 FIELD_PREP(BMI323_TAP1_MAX_PEAKS_MSK
,
635 ret
= bmi323_update_ext_reg(data
, BMI323_TAP1_REG
,
636 BMI323_TAP1_AXIS_SEL_MSK
,
637 FIELD_PREP(BMI323_TAP1_AXIS_SEL_MSK
,
638 BMI323_AXIS_XYZ_MSK
));
642 return bmi323_update_ext_reg(data
, BMI323_TAP1_REG
,
643 BMI323_TAP1_TIMOUT_MSK
,
644 FIELD_PREP(BMI323_TAP1_TIMOUT_MSK
,
648 static ssize_t
in_accel_gesture_tap_wait_dur_show(struct device
*dev
,
649 struct device_attribute
*attr
,
652 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
653 struct bmi323_data
*data
= iio_priv(indio_dev
);
654 unsigned int reg_value
, raw
;
657 scoped_guard(mutex
, &data
->mutex
) {
658 ret
= bmi323_read_ext_reg(data
, BMI323_TAP2_REG
, ®_value
);
663 raw
= FIELD_GET(BMI323_TAP2_MAX_DUR_MSK
, reg_value
);
664 val
[0] = raw
/ BMI323_MAX_GES_DUR_SCALE
;
665 val
[1] = BMI323_RAW_TO_MICRO(raw
, BMI323_MAX_GES_DUR_SCALE
);
667 return iio_format_value(buf
, IIO_VAL_INT_PLUS_MICRO
, ARRAY_SIZE(val
),
671 static ssize_t
in_accel_gesture_tap_wait_dur_store(struct device
*dev
,
672 struct device_attribute
*attr
,
673 const char *buf
, size_t len
)
675 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
676 struct bmi323_data
*data
= iio_priv(indio_dev
);
677 int ret
, val_int
, val_fract
, raw
;
679 ret
= iio_str_to_fixpoint(buf
, 100000, &val_int
, &val_fract
);
683 raw
= BMI323_INT_MICRO_TO_RAW(val_int
, val_fract
,
684 BMI323_MAX_GES_DUR_SCALE
);
685 if (!in_range(raw
, 0, 64))
688 guard(mutex
)(&data
->mutex
);
689 ret
= bmi323_update_ext_reg(data
, BMI323_TAP2_REG
,
690 BMI323_TAP2_MAX_DUR_MSK
,
691 FIELD_PREP(BMI323_TAP2_MAX_DUR_MSK
, raw
));
699 * Maximum duration from first tap within the second tap is expected to happen.
700 * This timeout is applicable only if gesture_tap_wait_timeout is enabled.
702 static IIO_DEVICE_ATTR_RW(in_accel_gesture_tap_wait_dur
, 0);
704 static ssize_t
in_accel_gesture_tap_wait_timeout_show(struct device
*dev
,
705 struct device_attribute
*attr
,
708 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
709 struct bmi323_data
*data
= iio_priv(indio_dev
);
710 unsigned int reg_value
, raw
;
713 scoped_guard(mutex
, &data
->mutex
) {
714 ret
= bmi323_read_ext_reg(data
, BMI323_TAP1_REG
, ®_value
);
719 raw
= FIELD_GET(BMI323_TAP1_TIMOUT_MSK
, reg_value
);
721 return iio_format_value(buf
, IIO_VAL_INT
, 1, &raw
);
724 static ssize_t
in_accel_gesture_tap_wait_timeout_store(struct device
*dev
,
725 struct device_attribute
*attr
,
729 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
730 struct bmi323_data
*data
= iio_priv(indio_dev
);
734 ret
= kstrtobool(buf
, &val
);
738 guard(mutex
)(&data
->mutex
);
739 ret
= bmi323_update_ext_reg(data
, BMI323_TAP1_REG
,
740 BMI323_TAP1_TIMOUT_MSK
,
741 FIELD_PREP(BMI323_TAP1_TIMOUT_MSK
, val
));
748 /* Enable/disable gesture confirmation with wait time */
749 static IIO_DEVICE_ATTR_RW(in_accel_gesture_tap_wait_timeout
, 0);
751 static IIO_CONST_ATTR(in_accel_gesture_tap_wait_dur_available
,
754 static IIO_CONST_ATTR(in_accel_gesture_doubletap_tap2_min_delay_available
,
755 "[0.005 0.005 0.075]");
757 static IIO_CONST_ATTR(in_accel_gesture_tap_reset_timeout_available
,
760 static IIO_CONST_ATTR(in_accel_gesture_tap_value_available
, "[0.0 0.002 1.99]");
762 static IIO_CONST_ATTR(in_accel_mag_value_available
, "[0.0 0.002 7.99]");
764 static IIO_CONST_ATTR(in_accel_mag_period_available
, "[0.0 0.02 162.0]");
766 static IIO_CONST_ATTR(in_accel_mag_hysteresis_available
, "[0.0 0.002 1.99]");
768 static struct attribute
*bmi323_event_attributes
[] = {
769 &iio_const_attr_in_accel_gesture_tap_value_available
.dev_attr
.attr
,
770 &iio_const_attr_in_accel_gesture_tap_reset_timeout_available
.dev_attr
.attr
,
771 &iio_const_attr_in_accel_gesture_doubletap_tap2_min_delay_available
.dev_attr
.attr
,
772 &iio_const_attr_in_accel_gesture_tap_wait_dur_available
.dev_attr
.attr
,
773 &iio_dev_attr_in_accel_gesture_tap_wait_timeout
.dev_attr
.attr
,
774 &iio_dev_attr_in_accel_gesture_tap_wait_dur
.dev_attr
.attr
,
775 &iio_const_attr_in_accel_mag_value_available
.dev_attr
.attr
,
776 &iio_const_attr_in_accel_mag_period_available
.dev_attr
.attr
,
777 &iio_const_attr_in_accel_mag_hysteresis_available
.dev_attr
.attr
,
781 static const struct attribute_group bmi323_event_attribute_group
= {
782 .attrs
= bmi323_event_attributes
,
785 static int bmi323_write_event_config(struct iio_dev
*indio_dev
,
786 const struct iio_chan_spec
*chan
,
787 enum iio_event_type type
,
788 enum iio_event_direction dir
, bool state
)
790 struct bmi323_data
*data
= iio_priv(indio_dev
);
793 case IIO_EV_TYPE_MAG
:
794 return bmi323_motion_event_en(data
, dir
, state
);
795 case IIO_EV_TYPE_GESTURE
:
796 return bmi323_tap_event_en(data
, dir
, state
);
797 case IIO_EV_TYPE_CHANGE
:
798 return bmi323_step_wtrmrk_en(data
, state
);
804 static int bmi323_read_event_config(struct iio_dev
*indio_dev
,
805 const struct iio_chan_spec
*chan
,
806 enum iio_event_type type
,
807 enum iio_event_direction dir
)
809 struct bmi323_data
*data
= iio_priv(indio_dev
);
810 int ret
, value
, reg_val
;
812 guard(mutex
)(&data
->mutex
);
814 switch (chan
->type
) {
817 case IIO_EV_DIR_SINGLETAP
:
818 ret
= FIELD_GET(BMI323_FEAT_IO0_S_TAP_MSK
,
819 data
->feature_events
);
821 case IIO_EV_DIR_DOUBLETAP
:
822 ret
= FIELD_GET(BMI323_FEAT_IO0_D_TAP_MSK
,
823 data
->feature_events
);
825 case IIO_EV_DIR_RISING
:
826 value
= FIELD_GET(BMI323_FEAT_IO0_XYZ_MOTION_MSK
,
827 data
->feature_events
);
830 case IIO_EV_DIR_FALLING
:
831 value
= FIELD_GET(BMI323_FEAT_IO0_XYZ_NOMOTION_MSK
,
832 data
->feature_events
);
841 ret
= regmap_read(data
->regmap
, BMI323_INT_MAP1_REG
, ®_val
);
845 return FIELD_GET(BMI323_STEP_CNT_MSK
, reg_val
) ? 1 : 0;
851 static int bmi323_write_event_value(struct iio_dev
*indio_dev
,
852 const struct iio_chan_spec
*chan
,
853 enum iio_event_type type
,
854 enum iio_event_direction dir
,
855 enum iio_event_info info
,
858 struct bmi323_data
*data
= iio_priv(indio_dev
);
862 guard(mutex
)(&data
->mutex
);
865 case IIO_EV_TYPE_GESTURE
:
867 case IIO_EV_INFO_VALUE
:
868 if (!in_range(val
, 0, 2))
871 raw
= BMI323_INT_MICRO_TO_RAW(val
, val2
,
872 BMI323_TAP_THRES_SCALE
);
874 return bmi323_update_ext_reg(data
, BMI323_TAP2_REG
,
875 BMI323_TAP2_THRES_MSK
,
876 FIELD_PREP(BMI323_TAP2_THRES_MSK
,
878 case IIO_EV_INFO_RESET_TIMEOUT
:
879 if (val
|| !in_range(val2
, 40000, 560001))
882 raw
= BMI323_INT_MICRO_TO_RAW(val
, val2
,
883 BMI323_QUITE_TIM_GES_SCALE
);
885 return bmi323_update_ext_reg(data
, BMI323_TAP3_REG
,
886 BMI323_TAP3_QT_AFT_GES_MSK
,
887 FIELD_PREP(BMI323_TAP3_QT_AFT_GES_MSK
,
889 case IIO_EV_INFO_TAP2_MIN_DELAY
:
890 if (val
|| !in_range(val2
, 5000, 70001))
893 raw
= BMI323_INT_MICRO_TO_RAW(val
, val2
,
894 BMI323_DUR_BW_TAP_SCALE
);
896 return bmi323_update_ext_reg(data
, BMI323_TAP3_REG
,
897 BMI323_TAP3_QT_BW_TAP_MSK
,
898 FIELD_PREP(BMI323_TAP3_QT_BW_TAP_MSK
,
903 case IIO_EV_TYPE_MAG
:
904 reg
= bmi323_motion_config_reg(dir
);
909 case IIO_EV_INFO_VALUE
:
910 if (!in_range(val
, 0, 8))
913 raw
= BMI323_INT_MICRO_TO_RAW(val
, val2
,
914 BMI323_MOTION_THRES_SCALE
);
916 return bmi323_update_ext_reg(data
, reg
,
917 BMI323_MO1_SLOPE_TH_MSK
,
918 FIELD_PREP(BMI323_MO1_SLOPE_TH_MSK
,
920 case IIO_EV_INFO_PERIOD
:
921 if (!in_range(val
, 0, 163))
924 raw
= BMI323_INT_MICRO_TO_RAW(val
, val2
,
925 BMI323_MOTION_DURAT_SCALE
);
927 return bmi323_update_ext_reg(data
,
928 reg
+ BMI323_MO3_OFFSET
,
930 FIELD_PREP(BMI323_MO3_DURA_MSK
,
932 case IIO_EV_INFO_HYSTERESIS
:
933 if (!in_range(val
, 0, 2))
936 raw
= BMI323_INT_MICRO_TO_RAW(val
, val2
,
937 BMI323_MOTION_HYSTR_SCALE
);
939 return bmi323_update_ext_reg(data
,
940 reg
+ BMI323_MO2_OFFSET
,
941 BMI323_MO2_HYSTR_MSK
,
942 FIELD_PREP(BMI323_MO2_HYSTR_MSK
,
947 case IIO_EV_TYPE_CHANGE
:
948 if (!in_range(val
, 0, 20461))
952 return bmi323_update_ext_reg(data
, BMI323_STEP_SC1_REG
,
953 BMI323_STEP_SC1_WTRMRK_MSK
,
954 FIELD_PREP(BMI323_STEP_SC1_WTRMRK_MSK
,
961 static int bmi323_read_event_value(struct iio_dev
*indio_dev
,
962 const struct iio_chan_spec
*chan
,
963 enum iio_event_type type
,
964 enum iio_event_direction dir
,
965 enum iio_event_info info
,
968 struct bmi323_data
*data
= iio_priv(indio_dev
);
969 unsigned int raw
, reg_value
;
972 guard(mutex
)(&data
->mutex
);
975 case IIO_EV_TYPE_GESTURE
:
977 case IIO_EV_INFO_VALUE
:
978 ret
= bmi323_read_ext_reg(data
, BMI323_TAP2_REG
,
983 raw
= FIELD_GET(BMI323_TAP2_THRES_MSK
, reg_value
);
984 *val
= raw
/ BMI323_TAP_THRES_SCALE
;
985 *val2
= BMI323_RAW_TO_MICRO(raw
, BMI323_TAP_THRES_SCALE
);
986 return IIO_VAL_INT_PLUS_MICRO
;
987 case IIO_EV_INFO_RESET_TIMEOUT
:
988 ret
= bmi323_read_ext_reg(data
, BMI323_TAP3_REG
,
993 raw
= FIELD_GET(BMI323_TAP3_QT_AFT_GES_MSK
, reg_value
);
995 *val2
= BMI323_RAW_TO_MICRO(raw
,
996 BMI323_QUITE_TIM_GES_SCALE
);
997 return IIO_VAL_INT_PLUS_MICRO
;
998 case IIO_EV_INFO_TAP2_MIN_DELAY
:
999 ret
= bmi323_read_ext_reg(data
, BMI323_TAP3_REG
,
1004 raw
= FIELD_GET(BMI323_TAP3_QT_BW_TAP_MSK
, reg_value
);
1006 *val2
= BMI323_RAW_TO_MICRO(raw
,
1007 BMI323_DUR_BW_TAP_SCALE
);
1008 return IIO_VAL_INT_PLUS_MICRO
;
1012 case IIO_EV_TYPE_MAG
:
1013 reg
= bmi323_motion_config_reg(dir
);
1018 case IIO_EV_INFO_VALUE
:
1019 ret
= bmi323_read_ext_reg(data
, reg
, ®_value
);
1023 raw
= FIELD_GET(BMI323_MO1_SLOPE_TH_MSK
, reg_value
);
1024 *val
= raw
/ BMI323_MOTION_THRES_SCALE
;
1025 *val2
= BMI323_RAW_TO_MICRO(raw
,
1026 BMI323_MOTION_THRES_SCALE
);
1027 return IIO_VAL_INT_PLUS_MICRO
;
1028 case IIO_EV_INFO_PERIOD
:
1029 ret
= bmi323_read_ext_reg(data
,
1030 reg
+ BMI323_MO3_OFFSET
,
1035 raw
= FIELD_GET(BMI323_MO3_DURA_MSK
, reg_value
);
1036 *val
= raw
/ BMI323_MOTION_DURAT_SCALE
;
1037 *val2
= BMI323_RAW_TO_MICRO(raw
,
1038 BMI323_MOTION_DURAT_SCALE
);
1039 return IIO_VAL_INT_PLUS_MICRO
;
1040 case IIO_EV_INFO_HYSTERESIS
:
1041 ret
= bmi323_read_ext_reg(data
,
1042 reg
+ BMI323_MO2_OFFSET
,
1047 raw
= FIELD_GET(BMI323_MO2_HYSTR_MSK
, reg_value
);
1048 *val
= raw
/ BMI323_MOTION_HYSTR_SCALE
;
1049 *val2
= BMI323_RAW_TO_MICRO(raw
,
1050 BMI323_MOTION_HYSTR_SCALE
);
1051 return IIO_VAL_INT_PLUS_MICRO
;
1055 case IIO_EV_TYPE_CHANGE
:
1056 ret
= bmi323_read_ext_reg(data
, BMI323_STEP_SC1_REG
,
1061 raw
= FIELD_GET(BMI323_STEP_SC1_WTRMRK_MSK
, reg_value
);
1069 static int __bmi323_fifo_flush(struct iio_dev
*indio_dev
)
1071 struct bmi323_data
*data
= iio_priv(indio_dev
);
1072 int i
, ret
, fifo_lvl
, frame_count
, bit
, index
;
1073 __le16
*frame
, *pchannels
;
1077 guard(mutex
)(&data
->mutex
);
1078 ret
= regmap_read(data
->regmap
, BMI323_FIFO_FILL_LEVEL_REG
, &fifo_lvl
);
1082 fifo_lvl
= min(fifo_lvl
, BMI323_FIFO_FULL_IN_WORDS
);
1084 frame_count
= fifo_lvl
/ BMI323_FIFO_FRAME_LENGTH
;
1088 if (fifo_lvl
% BMI323_FIFO_FRAME_LENGTH
)
1089 dev_warn(data
->dev
, "Bad FIFO alignment\n");
1092 * Approximate timestamps for each of the sample based on the sampling
1093 * frequency, timestamp for last sample and number of samples.
1095 if (data
->old_fifo_tstamp
) {
1096 sample_period
= data
->fifo_tstamp
- data
->old_fifo_tstamp
;
1097 do_div(sample_period
, frame_count
);
1099 sample_period
= data
->odrns
[BMI323_ACCEL
];
1102 tstamp
= data
->fifo_tstamp
- (frame_count
- 1) * sample_period
;
1104 ret
= regmap_noinc_read(data
->regmap
, BMI323_FIFO_DATA_REG
,
1105 &data
->fifo_buff
[0],
1106 fifo_lvl
* BMI323_BYTES_PER_SAMPLE
);
1110 for (i
= 0; i
< frame_count
; i
++) {
1111 frame
= &data
->fifo_buff
[i
* BMI323_FIFO_FRAME_LENGTH
];
1112 pchannels
= &data
->buffer
.channels
[0];
1115 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
1117 pchannels
[index
++] = frame
[bit
];
1119 iio_push_to_buffers_with_timestamp(indio_dev
, &data
->buffer
,
1122 tstamp
+= sample_period
;
1128 static int bmi323_set_watermark(struct iio_dev
*indio_dev
, unsigned int val
)
1130 struct bmi323_data
*data
= iio_priv(indio_dev
);
1132 val
= min(val
, (u32
)BMI323_FIFO_FULL_IN_FRAMES
);
1134 guard(mutex
)(&data
->mutex
);
1135 data
->watermark
= val
;
1140 static int bmi323_fifo_disable(struct bmi323_data
*data
)
1144 guard(mutex
)(&data
->mutex
);
1145 ret
= regmap_write(data
->regmap
, BMI323_FIFO_CONF_REG
, 0);
1149 ret
= regmap_update_bits(data
->regmap
, BMI323_INT_MAP2_REG
,
1150 BMI323_FIFO_WTRMRK_MSK
,
1151 FIELD_PREP(BMI323_FIFO_WTRMRK_MSK
, 0));
1155 data
->fifo_tstamp
= 0;
1156 data
->state
= BMI323_IDLE
;
1161 static int bmi323_buffer_predisable(struct iio_dev
*indio_dev
)
1163 struct bmi323_data
*data
= iio_priv(indio_dev
);
1165 if (iio_device_get_current_mode(indio_dev
) == INDIO_BUFFER_TRIGGERED
)
1168 return bmi323_fifo_disable(data
);
1171 static int bmi323_update_watermark(struct bmi323_data
*data
)
1175 wtrmrk
= data
->watermark
* BMI323_FIFO_FRAME_LENGTH
;
1177 return regmap_write(data
->regmap
, BMI323_FIFO_WTRMRK_REG
, wtrmrk
);
1180 static int bmi323_fifo_enable(struct bmi323_data
*data
)
1184 guard(mutex
)(&data
->mutex
);
1185 ret
= regmap_update_bits(data
->regmap
, BMI323_FIFO_CONF_REG
,
1186 BMI323_FIFO_CONF_ACC_GYR_EN_MSK
,
1187 FIELD_PREP(BMI323_FIFO_CONF_ACC_GYR_EN_MSK
,
1188 BMI323_FIFO_ACC_GYR_MSK
));
1192 ret
= regmap_update_bits(data
->regmap
, BMI323_INT_MAP2_REG
,
1193 BMI323_FIFO_WTRMRK_MSK
,
1194 FIELD_PREP(BMI323_FIFO_WTRMRK_MSK
,
1199 ret
= bmi323_update_watermark(data
);
1203 ret
= regmap_write(data
->regmap
, BMI323_FIFO_CTRL_REG
,
1204 BMI323_FIFO_FLUSH_MSK
);
1208 data
->state
= BMI323_BUFFER_FIFO
;
1213 static int bmi323_buffer_preenable(struct iio_dev
*indio_dev
)
1215 struct bmi323_data
*data
= iio_priv(indio_dev
);
1217 guard(mutex
)(&data
->mutex
);
1219 * When the ODR of the accelerometer and gyroscope do not match, the
1220 * maximum ODR value between the accelerometer and gyroscope is used
1221 * for FIFO and the signal with lower ODR will insert dummy frame.
1222 * So allow buffer read only when ODR's of accelero and gyro are equal.
1223 * See datasheet section 5.7 "FIFO Data Buffering".
1225 if (data
->odrns
[BMI323_ACCEL
] != data
->odrns
[BMI323_GYRO
]) {
1226 dev_err(data
->dev
, "Accelero and Gyro ODR doesn't match\n");
1233 static int bmi323_buffer_postenable(struct iio_dev
*indio_dev
)
1235 struct bmi323_data
*data
= iio_priv(indio_dev
);
1237 if (iio_device_get_current_mode(indio_dev
) == INDIO_BUFFER_TRIGGERED
)
1240 return bmi323_fifo_enable(data
);
1243 static ssize_t
hwfifo_watermark_show(struct device
*dev
,
1244 struct device_attribute
*attr
, char *buf
)
1246 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1247 struct bmi323_data
*data
= iio_priv(indio_dev
);
1250 scoped_guard(mutex
, &data
->mutex
)
1251 wm
= data
->watermark
;
1253 return sysfs_emit(buf
, "%d\n", wm
);
1255 static IIO_DEVICE_ATTR_RO(hwfifo_watermark
, 0);
1257 static ssize_t
hwfifo_enabled_show(struct device
*dev
,
1258 struct device_attribute
*attr
,
1261 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1262 struct bmi323_data
*data
= iio_priv(indio_dev
);
1265 scoped_guard(mutex
, &data
->mutex
)
1266 state
= data
->state
== BMI323_BUFFER_FIFO
;
1268 return sysfs_emit(buf
, "%d\n", state
);
1270 static IIO_DEVICE_ATTR_RO(hwfifo_enabled
, 0);
1272 static const struct iio_dev_attr
*bmi323_fifo_attributes
[] = {
1273 &iio_dev_attr_hwfifo_watermark
,
1274 &iio_dev_attr_hwfifo_enabled
,
1278 static const struct iio_buffer_setup_ops bmi323_buffer_ops
= {
1279 .preenable
= bmi323_buffer_preenable
,
1280 .postenable
= bmi323_buffer_postenable
,
1281 .predisable
= bmi323_buffer_predisable
,
1284 static irqreturn_t
bmi323_irq_thread_handler(int irq
, void *private)
1286 struct iio_dev
*indio_dev
= private;
1287 struct bmi323_data
*data
= iio_priv(indio_dev
);
1288 unsigned int status_addr
, status
, feature_event
;
1289 s64 timestamp
= iio_get_time_ns(indio_dev
);
1292 if (data
->irq_pin
== BMI323_IRQ_INT1
)
1293 status_addr
= BMI323_STATUS_INT1_REG
;
1295 status_addr
= BMI323_STATUS_INT2_REG
;
1297 scoped_guard(mutex
, &data
->mutex
) {
1298 ret
= regmap_read(data
->regmap
, status_addr
, &status
);
1303 if (!status
|| FIELD_GET(BMI323_STATUS_ERROR_MSK
, status
))
1306 if (FIELD_GET(BMI323_STATUS_FIFO_WTRMRK_MSK
, status
)) {
1307 data
->old_fifo_tstamp
= data
->fifo_tstamp
;
1308 data
->fifo_tstamp
= iio_get_time_ns(indio_dev
);
1309 ret
= __bmi323_fifo_flush(indio_dev
);
1314 if (FIELD_GET(BMI323_STATUS_ACC_GYR_DRDY_MSK
, status
))
1315 iio_trigger_poll_nested(data
->trig
);
1317 if (FIELD_GET(BMI323_STATUS_MOTION_MSK
, status
))
1318 iio_push_event(indio_dev
, IIO_MOD_EVENT_CODE(IIO_ACCEL
, 0,
1319 IIO_MOD_X_OR_Y_OR_Z
,
1324 if (FIELD_GET(BMI323_STATUS_NOMOTION_MSK
, status
))
1325 iio_push_event(indio_dev
, IIO_MOD_EVENT_CODE(IIO_ACCEL
, 0,
1326 IIO_MOD_X_OR_Y_OR_Z
,
1328 IIO_EV_DIR_FALLING
),
1331 if (FIELD_GET(BMI323_STATUS_STP_WTR_MSK
, status
))
1332 iio_push_event(indio_dev
, IIO_MOD_EVENT_CODE(IIO_STEPS
, 0,
1338 if (FIELD_GET(BMI323_STATUS_TAP_MSK
, status
)) {
1339 scoped_guard(mutex
, &data
->mutex
) {
1340 ret
= regmap_read(data
->regmap
,
1341 BMI323_FEAT_EVNT_EXT_REG
,
1347 if (FIELD_GET(BMI323_FEAT_EVNT_EXT_S_MSK
, feature_event
)) {
1348 iio_push_event(indio_dev
,
1349 IIO_MOD_EVENT_CODE(IIO_ACCEL
, 0,
1350 IIO_MOD_X_OR_Y_OR_Z
,
1351 IIO_EV_TYPE_GESTURE
,
1352 IIO_EV_DIR_SINGLETAP
),
1356 if (FIELD_GET(BMI323_FEAT_EVNT_EXT_D_MSK
, feature_event
))
1357 iio_push_event(indio_dev
,
1358 IIO_MOD_EVENT_CODE(IIO_ACCEL
, 0,
1359 IIO_MOD_X_OR_Y_OR_Z
,
1360 IIO_EV_TYPE_GESTURE
,
1361 IIO_EV_DIR_DOUBLETAP
),
1368 static int bmi323_set_drdy_irq(struct bmi323_data
*data
,
1369 enum bmi323_irq_pin irq_pin
)
1373 ret
= regmap_update_bits(data
->regmap
, BMI323_INT_MAP2_REG
,
1374 BMI323_GYR_DRDY_MSK
,
1375 FIELD_PREP(BMI323_GYR_DRDY_MSK
, irq_pin
));
1379 return regmap_update_bits(data
->regmap
, BMI323_INT_MAP2_REG
,
1380 BMI323_ACC_DRDY_MSK
,
1381 FIELD_PREP(BMI323_ACC_DRDY_MSK
, irq_pin
));
1384 static int bmi323_data_rdy_trigger_set_state(struct iio_trigger
*trig
,
1387 struct bmi323_data
*data
= iio_trigger_get_drvdata(trig
);
1388 enum bmi323_irq_pin irq_pin
;
1390 guard(mutex
)(&data
->mutex
);
1392 if (data
->state
== BMI323_BUFFER_FIFO
) {
1393 dev_warn(data
->dev
, "Can't set trigger when FIFO enabled\n");
1398 data
->state
= BMI323_BUFFER_DRDY_TRIGGERED
;
1399 irq_pin
= data
->irq_pin
;
1401 data
->state
= BMI323_IDLE
;
1402 irq_pin
= BMI323_IRQ_DISABLED
;
1405 return bmi323_set_drdy_irq(data
, irq_pin
);
1408 static const struct iio_trigger_ops bmi323_trigger_ops
= {
1409 .set_trigger_state
= &bmi323_data_rdy_trigger_set_state
,
1412 static irqreturn_t
bmi323_trigger_handler(int irq
, void *p
)
1414 struct iio_poll_func
*pf
= p
;
1415 struct iio_dev
*indio_dev
= pf
->indio_dev
;
1416 struct bmi323_data
*data
= iio_priv(indio_dev
);
1417 int ret
, bit
, index
= 0;
1419 /* Lock to protect the data->buffer */
1420 guard(mutex
)(&data
->mutex
);
1422 if (*indio_dev
->active_scan_mask
== BMI323_ALL_CHAN_MSK
) {
1423 ret
= regmap_bulk_read(data
->regmap
, BMI323_ACCEL_X_REG
,
1424 &data
->buffer
.channels
,
1425 ARRAY_SIZE(data
->buffer
.channels
));
1429 for_each_set_bit(bit
, indio_dev
->active_scan_mask
,
1431 ret
= regmap_raw_read(data
->regmap
,
1432 BMI323_ACCEL_X_REG
+ bit
,
1433 &data
->buffer
.channels
[index
++],
1434 BMI323_BYTES_PER_SAMPLE
);
1440 iio_push_to_buffers_with_timestamp(indio_dev
, &data
->buffer
,
1441 iio_get_time_ns(indio_dev
));
1444 iio_trigger_notify_done(indio_dev
->trig
);
1449 static int bmi323_set_average(struct bmi323_data
*data
,
1450 enum bmi323_sensor_type sensor
, int avg
)
1452 int raw
= ARRAY_SIZE(bmi323_accel_gyro_avrg
);
1455 if (avg
== bmi323_accel_gyro_avrg
[raw
])
1460 guard(mutex
)(&data
->mutex
);
1461 return regmap_update_bits(data
->regmap
, bmi323_hw
[sensor
].config
,
1462 BMI323_ACC_GYRO_CONF_AVG_MSK
,
1463 FIELD_PREP(BMI323_ACC_GYRO_CONF_AVG_MSK
,
1467 static int bmi323_get_average(struct bmi323_data
*data
,
1468 enum bmi323_sensor_type sensor
, int *avg
)
1470 int ret
, value
, raw
;
1472 scoped_guard(mutex
, &data
->mutex
) {
1473 ret
= regmap_read(data
->regmap
, bmi323_hw
[sensor
].config
, &value
);
1478 raw
= FIELD_GET(BMI323_ACC_GYRO_CONF_AVG_MSK
, value
);
1479 *avg
= bmi323_accel_gyro_avrg
[raw
];
1484 static int bmi323_enable_steps(struct bmi323_data
*data
, int val
)
1488 guard(mutex
)(&data
->mutex
);
1489 if (data
->odrhz
[BMI323_ACCEL
] < 200) {
1490 dev_err(data
->dev
, "Invalid accelerometer parameter\n");
1494 ret
= bmi323_feature_engine_events(data
, BMI323_FEAT_IO0_STP_CNT_MSK
,
1499 set_mask_bits(&data
->feature_events
, BMI323_FEAT_IO0_STP_CNT_MSK
,
1500 FIELD_PREP(BMI323_FEAT_IO0_STP_CNT_MSK
, val
? 1 : 0));
1505 static int bmi323_read_steps(struct bmi323_data
*data
, int *val
)
1509 guard(mutex
)(&data
->mutex
);
1510 if (!FIELD_GET(BMI323_FEAT_IO0_STP_CNT_MSK
, data
->feature_events
))
1513 ret
= regmap_bulk_read(data
->regmap
, BMI323_FEAT_IO2_REG
,
1515 ARRAY_SIZE(data
->steps_count
));
1519 *val
= get_unaligned_le32(data
->steps_count
);
1524 static int bmi323_read_axis(struct bmi323_data
*data
,
1525 struct iio_chan_spec
const *chan
, int *val
)
1527 enum bmi323_sensor_type sensor
;
1532 ret
= bmi323_get_error_status(data
);
1536 sensor
= bmi323_iio_to_sensor(chan
->type
);
1537 addr
= bmi323_hw
[sensor
].data
+ (chan
->channel2
- IIO_MOD_X
);
1539 scoped_guard(mutex
, &data
->mutex
) {
1540 ret
= regmap_read(data
->regmap
, addr
, &value
);
1545 *val
= sign_extend32(value
, chan
->scan_type
.realbits
- 1);
1550 static int bmi323_get_temp_data(struct bmi323_data
*data
, int *val
)
1555 ret
= bmi323_get_error_status(data
);
1559 scoped_guard(mutex
, &data
->mutex
) {
1560 ret
= regmap_read(data
->regmap
, BMI323_TEMP_REG
, &value
);
1565 *val
= sign_extend32(value
, 15);
1570 static int bmi323_get_odr(struct bmi323_data
*data
,
1571 enum bmi323_sensor_type sensor
, int *odr
, int *uodr
)
1573 int ret
, value
, odr_raw
;
1575 scoped_guard(mutex
, &data
->mutex
) {
1576 ret
= regmap_read(data
->regmap
, bmi323_hw
[sensor
].config
, &value
);
1581 odr_raw
= FIELD_GET(BMI323_ACC_GYRO_CONF_ODR_MSK
, value
);
1582 *odr
= bmi323_acc_gyro_odr
[odr_raw
- 1][0];
1583 *uodr
= bmi323_acc_gyro_odr
[odr_raw
- 1][1];
1585 return IIO_VAL_INT_PLUS_MICRO
;
1588 static int bmi323_configure_power_mode(struct bmi323_data
*data
,
1589 enum bmi323_sensor_type sensor
,
1592 enum bmi323_opr_mode mode
;
1594 if (bmi323_acc_gyro_odr
[odr_index
][0] > 25)
1595 mode
= ACC_GYRO_MODE_CONTINOUS
;
1597 mode
= ACC_GYRO_MODE_DUTYCYCLE
;
1599 return bmi323_set_mode(data
, sensor
, mode
);
1602 static int bmi323_set_odr(struct bmi323_data
*data
,
1603 enum bmi323_sensor_type sensor
, int odr
, int uodr
)
1607 odr_raw
= ARRAY_SIZE(bmi323_acc_gyro_odr
);
1610 if (odr
== bmi323_acc_gyro_odr
[odr_raw
][0] &&
1611 uodr
== bmi323_acc_gyro_odr
[odr_raw
][1])
1616 ret
= bmi323_configure_power_mode(data
, sensor
, odr_raw
);
1620 guard(mutex
)(&data
->mutex
);
1621 data
->odrhz
[sensor
] = bmi323_acc_gyro_odr
[odr_raw
][0];
1622 data
->odrns
[sensor
] = bmi323_acc_gyro_odrns
[odr_raw
];
1626 return regmap_update_bits(data
->regmap
, bmi323_hw
[sensor
].config
,
1627 BMI323_ACC_GYRO_CONF_ODR_MSK
,
1628 FIELD_PREP(BMI323_ACC_GYRO_CONF_ODR_MSK
,
1632 static int bmi323_get_scale(struct bmi323_data
*data
,
1633 enum bmi323_sensor_type sensor
, int *val2
)
1635 int ret
, value
, scale_raw
;
1637 scoped_guard(mutex
, &data
->mutex
) {
1638 ret
= regmap_read(data
->regmap
, bmi323_hw
[sensor
].config
,
1644 scale_raw
= FIELD_GET(BMI323_ACC_GYRO_CONF_SCL_MSK
, value
);
1645 *val2
= bmi323_hw
[sensor
].scale_table
[scale_raw
][1];
1647 return IIO_VAL_INT_PLUS_MICRO
;
1650 static int bmi323_set_scale(struct bmi323_data
*data
,
1651 enum bmi323_sensor_type sensor
, int val
, int val2
)
1655 scale_raw
= bmi323_hw
[sensor
].scale_table_len
;
1658 if (val
== bmi323_hw
[sensor
].scale_table
[scale_raw
][0] &&
1659 val2
== bmi323_hw
[sensor
].scale_table
[scale_raw
][1])
1664 guard(mutex
)(&data
->mutex
);
1665 return regmap_update_bits(data
->regmap
, bmi323_hw
[sensor
].config
,
1666 BMI323_ACC_GYRO_CONF_SCL_MSK
,
1667 FIELD_PREP(BMI323_ACC_GYRO_CONF_SCL_MSK
,
1671 static int bmi323_read_avail(struct iio_dev
*indio_dev
,
1672 struct iio_chan_spec
const *chan
,
1673 const int **vals
, int *type
, int *length
,
1676 enum bmi323_sensor_type sensor
;
1679 case IIO_CHAN_INFO_SAMP_FREQ
:
1680 *type
= IIO_VAL_INT_PLUS_MICRO
;
1681 *vals
= (const int *)bmi323_acc_gyro_odr
;
1682 *length
= ARRAY_SIZE(bmi323_acc_gyro_odr
) * 2;
1683 return IIO_AVAIL_LIST
;
1684 case IIO_CHAN_INFO_SCALE
:
1685 sensor
= bmi323_iio_to_sensor(chan
->type
);
1686 *type
= IIO_VAL_INT_PLUS_MICRO
;
1687 *vals
= (const int *)bmi323_hw
[sensor
].scale_table
;
1688 *length
= bmi323_hw
[sensor
].scale_table_len
* 2;
1689 return IIO_AVAIL_LIST
;
1690 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
1691 *type
= IIO_VAL_INT
;
1692 *vals
= (const int *)bmi323_accel_gyro_avrg
;
1693 *length
= ARRAY_SIZE(bmi323_accel_gyro_avrg
);
1694 return IIO_AVAIL_LIST
;
1700 static int bmi323_write_raw(struct iio_dev
*indio_dev
,
1701 struct iio_chan_spec
const *chan
, int val
,
1702 int val2
, long mask
)
1704 struct bmi323_data
*data
= iio_priv(indio_dev
);
1707 case IIO_CHAN_INFO_SAMP_FREQ
:
1708 iio_device_claim_direct_scoped(return -EBUSY
, indio_dev
)
1709 return bmi323_set_odr(data
,
1710 bmi323_iio_to_sensor(chan
->type
),
1713 case IIO_CHAN_INFO_SCALE
:
1714 iio_device_claim_direct_scoped(return -EBUSY
, indio_dev
)
1715 return bmi323_set_scale(data
,
1716 bmi323_iio_to_sensor(chan
->type
),
1719 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
1720 iio_device_claim_direct_scoped(return -EBUSY
, indio_dev
)
1721 return bmi323_set_average(data
,
1722 bmi323_iio_to_sensor(chan
->type
),
1725 case IIO_CHAN_INFO_ENABLE
:
1726 return bmi323_enable_steps(data
, val
);
1727 case IIO_CHAN_INFO_PROCESSED
: {
1728 guard(mutex
)(&data
->mutex
);
1730 if (val
|| !FIELD_GET(BMI323_FEAT_IO0_STP_CNT_MSK
,
1731 data
->feature_events
))
1734 /* Clear step counter value */
1735 return bmi323_update_ext_reg(data
, BMI323_STEP_SC1_REG
,
1736 BMI323_STEP_SC1_RST_CNT_MSK
,
1737 FIELD_PREP(BMI323_STEP_SC1_RST_CNT_MSK
,
1745 static int bmi323_read_raw(struct iio_dev
*indio_dev
,
1746 struct iio_chan_spec
const *chan
, int *val
,
1747 int *val2
, long mask
)
1749 struct bmi323_data
*data
= iio_priv(indio_dev
);
1752 case IIO_CHAN_INFO_PROCESSED
:
1753 return bmi323_read_steps(data
, val
);
1754 case IIO_CHAN_INFO_RAW
:
1755 switch (chan
->type
) {
1758 iio_device_claim_direct_scoped(return -EBUSY
,
1760 return bmi323_read_axis(data
, chan
, val
);
1763 return bmi323_get_temp_data(data
, val
);
1767 case IIO_CHAN_INFO_SAMP_FREQ
:
1768 return bmi323_get_odr(data
, bmi323_iio_to_sensor(chan
->type
),
1770 case IIO_CHAN_INFO_SCALE
:
1771 switch (chan
->type
) {
1775 return bmi323_get_scale(data
,
1776 bmi323_iio_to_sensor(chan
->type
),
1779 *val
= BMI323_TEMP_SCALE
/ MEGA
;
1780 *val2
= BMI323_TEMP_SCALE
% MEGA
;
1781 return IIO_VAL_INT_PLUS_MICRO
;
1785 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
1786 return bmi323_get_average(data
,
1787 bmi323_iio_to_sensor(chan
->type
),
1789 case IIO_CHAN_INFO_OFFSET
:
1790 switch (chan
->type
) {
1792 *val
= BMI323_TEMP_OFFSET
;
1797 case IIO_CHAN_INFO_ENABLE
:
1798 scoped_guard(mutex
, &data
->mutex
)
1799 *val
= FIELD_GET(BMI323_FEAT_IO0_STP_CNT_MSK
,
1800 data
->feature_events
);
1807 static const struct iio_info bmi323_info
= {
1808 .read_raw
= bmi323_read_raw
,
1809 .write_raw
= bmi323_write_raw
,
1810 .read_avail
= bmi323_read_avail
,
1811 .hwfifo_set_watermark
= bmi323_set_watermark
,
1812 .write_event_config
= bmi323_write_event_config
,
1813 .read_event_config
= bmi323_read_event_config
,
1814 .write_event_value
= bmi323_write_event_value
,
1815 .read_event_value
= bmi323_read_event_value
,
1816 .event_attrs
= &bmi323_event_attribute_group
,
1819 #define BMI323_SCAN_MASK_ACCEL_3AXIS \
1820 (BIT(BMI323_ACCEL_X) | BIT(BMI323_ACCEL_Y) | BIT(BMI323_ACCEL_Z))
1822 #define BMI323_SCAN_MASK_GYRO_3AXIS \
1823 (BIT(BMI323_GYRO_X) | BIT(BMI323_GYRO_Y) | BIT(BMI323_GYRO_Z))
1825 static const unsigned long bmi323_avail_scan_masks
[] = {
1827 BMI323_SCAN_MASK_ACCEL_3AXIS
,
1829 BMI323_SCAN_MASK_GYRO_3AXIS
,
1830 /* 3-axis accel + 3-axis gyro */
1831 BMI323_SCAN_MASK_ACCEL_3AXIS
| BMI323_SCAN_MASK_GYRO_3AXIS
,
1835 static int bmi323_int_pin_config(struct bmi323_data
*data
,
1836 enum bmi323_irq_pin irq_pin
,
1837 bool active_high
, bool open_drain
, bool latch
)
1839 unsigned int mask
, field_value
;
1842 ret
= regmap_update_bits(data
->regmap
, BMI323_IO_INT_CONF_REG
,
1843 BMI323_IO_INT_LTCH_MSK
,
1844 FIELD_PREP(BMI323_IO_INT_LTCH_MSK
, latch
));
1848 ret
= bmi323_update_ext_reg(data
, BMI323_GEN_SET1_REG
,
1849 BMI323_GEN_HOLD_DUR_MSK
,
1850 FIELD_PREP(BMI323_GEN_HOLD_DUR_MSK
, 0));
1855 case BMI323_IRQ_INT1
:
1856 mask
= BMI323_IO_INT1_LVL_OD_OP_MSK
;
1858 field_value
= FIELD_PREP(BMI323_IO_INT1_LVL_MSK
, active_high
) |
1859 FIELD_PREP(BMI323_IO_INT1_OD_MSK
, open_drain
) |
1860 FIELD_PREP(BMI323_IO_INT1_OP_EN_MSK
, 1);
1862 case BMI323_IRQ_INT2
:
1863 mask
= BMI323_IO_INT2_LVL_OD_OP_MSK
;
1865 field_value
= FIELD_PREP(BMI323_IO_INT2_LVL_MSK
, active_high
) |
1866 FIELD_PREP(BMI323_IO_INT2_OD_MSK
, open_drain
) |
1867 FIELD_PREP(BMI323_IO_INT2_OP_EN_MSK
, 1);
1873 return regmap_update_bits(data
->regmap
, BMI323_IO_INT_CTR_REG
, mask
,
1877 static int bmi323_trigger_probe(struct bmi323_data
*data
,
1878 struct iio_dev
*indio_dev
)
1880 bool open_drain
, active_high
, latch
;
1881 struct fwnode_handle
*fwnode
;
1882 enum bmi323_irq_pin irq_pin
;
1883 int ret
, irq
, irq_type
;
1885 fwnode
= dev_fwnode(data
->dev
);
1889 irq
= fwnode_irq_get_byname(fwnode
, "INT1");
1891 irq_pin
= BMI323_IRQ_INT1
;
1893 irq
= fwnode_irq_get_byname(fwnode
, "INT2");
1897 irq_pin
= BMI323_IRQ_INT2
;
1900 irq_type
= irq_get_trigger_type(irq
);
1902 case IRQF_TRIGGER_RISING
:
1906 case IRQF_TRIGGER_HIGH
:
1910 case IRQF_TRIGGER_FALLING
:
1912 active_high
= false;
1914 case IRQF_TRIGGER_LOW
:
1916 active_high
= false;
1919 return dev_err_probe(data
->dev
, -EINVAL
,
1920 "Invalid interrupt type 0x%x specified\n",
1924 open_drain
= fwnode_property_read_bool(fwnode
, "drive-open-drain");
1926 ret
= bmi323_int_pin_config(data
, irq_pin
, active_high
, open_drain
,
1929 return dev_err_probe(data
->dev
, ret
,
1930 "Failed to configure irq line\n");
1932 data
->trig
= devm_iio_trigger_alloc(data
->dev
, "%s-trig-%d",
1933 indio_dev
->name
, irq_pin
);
1937 data
->trig
->ops
= &bmi323_trigger_ops
;
1938 iio_trigger_set_drvdata(data
->trig
, data
);
1940 ret
= devm_request_threaded_irq(data
->dev
, irq
, NULL
,
1941 bmi323_irq_thread_handler
,
1942 IRQF_ONESHOT
, "bmi323-int", indio_dev
);
1944 return dev_err_probe(data
->dev
, ret
, "Failed to request IRQ\n");
1946 ret
= devm_iio_trigger_register(data
->dev
, data
->trig
);
1948 return dev_err_probe(data
->dev
, ret
,
1949 "Trigger registration failed\n");
1951 data
->irq_pin
= irq_pin
;
1956 static int bmi323_feature_engine_enable(struct bmi323_data
*data
, bool en
)
1958 unsigned int feature_status
;
1962 return regmap_write(data
->regmap
, BMI323_FEAT_CTRL_REG
, 0);
1964 ret
= regmap_write(data
->regmap
, BMI323_FEAT_IO2_REG
, 0x012c);
1968 ret
= regmap_write(data
->regmap
, BMI323_FEAT_IO_STATUS_REG
,
1969 BMI323_FEAT_IO_STATUS_MSK
);
1973 ret
= regmap_write(data
->regmap
, BMI323_FEAT_CTRL_REG
,
1974 BMI323_FEAT_ENG_EN_MSK
);
1979 * It takes around 4 msec to enable the Feature engine, so check
1980 * the status of the feature engine every 2 msec for a maximum
1983 ret
= regmap_read_poll_timeout(data
->regmap
, BMI323_FEAT_IO1_REG
,
1985 FIELD_GET(BMI323_FEAT_IO1_ERR_MSK
,
1986 feature_status
) == 1,
1987 BMI323_FEAT_ENG_POLL
,
1988 BMI323_FEAT_ENG_TIMEOUT
);
1990 return dev_err_probe(data
->dev
, -EINVAL
,
1991 "Failed to enable feature engine\n");
1996 static void bmi323_disable(void *data_ptr
)
1998 struct bmi323_data
*data
= data_ptr
;
2000 bmi323_set_mode(data
, BMI323_ACCEL
, ACC_GYRO_MODE_DISABLE
);
2001 bmi323_set_mode(data
, BMI323_GYRO
, ACC_GYRO_MODE_DISABLE
);
2004 * Place the peripheral in its lowest power consuming state.
2006 regmap_write(data
->regmap
, BMI323_CMD_REG
, BMI323_RST_VAL
);
2009 static int bmi323_set_bw(struct bmi323_data
*data
,
2010 enum bmi323_sensor_type sensor
, enum bmi323_3db_bw bw
)
2012 return regmap_update_bits(data
->regmap
, bmi323_hw
[sensor
].config
,
2013 BMI323_ACC_GYRO_CONF_BW_MSK
,
2014 FIELD_PREP(BMI323_ACC_GYRO_CONF_BW_MSK
, bw
));
2017 static int bmi323_init(struct bmi323_data
*data
)
2022 * Perform soft reset to make sure the device is in a known state after
2023 * start up. A delay of 1.5 ms is required after reset.
2024 * See datasheet section 5.17 "Soft Reset".
2026 ret
= regmap_write(data
->regmap
, BMI323_CMD_REG
, BMI323_RST_VAL
);
2030 usleep_range(1500, 2000);
2033 * Dummy read is required to enable SPI interface after reset.
2034 * See datasheet section 7.2.1 "Protocol Selection".
2036 regmap_read(data
->regmap
, BMI323_CHIP_ID_REG
, &val
);
2038 ret
= regmap_read(data
->regmap
, BMI323_STATUS_REG
, &val
);
2042 if (!FIELD_GET(BMI323_STATUS_POR_MSK
, val
))
2043 return dev_err_probe(data
->dev
, -EINVAL
,
2044 "Sensor initialization error\n");
2046 ret
= regmap_read(data
->regmap
, BMI323_CHIP_ID_REG
, &val
);
2050 if (FIELD_GET(BMI323_CHIP_ID_MSK
, val
) != BMI323_CHIP_ID_VAL
)
2051 return dev_err_probe(data
->dev
, -EINVAL
, "Chip ID mismatch\n");
2053 ret
= bmi323_feature_engine_enable(data
, true);
2057 ret
= regmap_read(data
->regmap
, BMI323_ERR_REG
, &val
);
2062 return dev_err_probe(data
->dev
, -EINVAL
,
2063 "Sensor power error = 0x%x\n", val
);
2068 static int bmi323_init_reset(struct bmi323_data
*data
)
2073 * Set the Bandwidth coefficient which defines the 3 dB cutoff
2074 * frequency in relation to the ODR.
2076 ret
= bmi323_set_bw(data
, BMI323_ACCEL
, BMI323_BW_ODR_BY_2
);
2080 ret
= bmi323_set_bw(data
, BMI323_GYRO
, BMI323_BW_ODR_BY_2
);
2084 ret
= bmi323_set_odr(data
, BMI323_ACCEL
, 25, 0);
2088 ret
= bmi323_set_odr(data
, BMI323_GYRO
, 25, 0);
2092 return devm_add_action_or_reset(data
->dev
, bmi323_disable
, data
);
2095 int bmi323_core_probe(struct device
*dev
)
2097 static const char * const regulator_names
[] = { "vdd", "vddio" };
2098 struct iio_dev
*indio_dev
;
2099 struct bmi323_data
*data
;
2100 struct regmap
*regmap
;
2103 regmap
= dev_get_regmap(dev
, NULL
);
2105 return dev_err_probe(dev
, -ENODEV
, "Failed to get regmap\n");
2107 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
2109 return dev_err_probe(dev
, -ENOMEM
,
2110 "Failed to allocate device\n");
2112 ret
= devm_regulator_bulk_get_enable(dev
, ARRAY_SIZE(regulator_names
),
2115 return dev_err_probe(dev
, ret
, "Failed to enable regulators\n");
2117 data
= iio_priv(indio_dev
);
2119 data
->regmap
= regmap
;
2120 data
->irq_pin
= BMI323_IRQ_DISABLED
;
2121 data
->state
= BMI323_IDLE
;
2122 mutex_init(&data
->mutex
);
2124 ret
= bmi323_init(data
);
2128 ret
= bmi323_init_reset(data
);
2132 if (!iio_read_acpi_mount_matrix(dev
, &data
->orientation
, "ROTM")) {
2133 ret
= iio_read_mount_matrix(dev
, &data
->orientation
);
2138 indio_dev
->name
= "bmi323-imu";
2139 indio_dev
->info
= &bmi323_info
;
2140 indio_dev
->channels
= bmi323_channels
;
2141 indio_dev
->num_channels
= ARRAY_SIZE(bmi323_channels
);
2142 indio_dev
->available_scan_masks
= bmi323_avail_scan_masks
;
2143 indio_dev
->modes
= INDIO_DIRECT_MODE
| INDIO_BUFFER_SOFTWARE
;
2144 dev_set_drvdata(data
->dev
, indio_dev
);
2146 ret
= bmi323_trigger_probe(data
, indio_dev
);
2150 ret
= devm_iio_triggered_buffer_setup_ext(data
->dev
, indio_dev
,
2151 &iio_pollfunc_store_time
,
2152 bmi323_trigger_handler
,
2153 IIO_BUFFER_DIRECTION_IN
,
2155 bmi323_fifo_attributes
);
2157 return dev_err_probe(data
->dev
, ret
,
2158 "Failed to setup trigger buffer\n");
2160 ret
= devm_iio_device_register(data
->dev
, indio_dev
);
2162 return dev_err_probe(data
->dev
, ret
,
2163 "Unable to register iio device\n");
2165 return bmi323_fifo_disable(data
);
2167 EXPORT_SYMBOL_NS_GPL(bmi323_core_probe
, "IIO_BMI323");
2169 static int bmi323_core_runtime_suspend(struct device
*dev
)
2171 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
2172 struct bmi323_data
*data
= iio_priv(indio_dev
);
2173 struct bmi323_regs_runtime_pm
*savestate
= &data
->runtime_pm_status
;
2176 guard(mutex
)(&data
->mutex
);
2178 ret
= iio_device_suspend_triggering(indio_dev
);
2182 /* Save registers meant to be restored by resume pm callback. */
2183 for (unsigned int i
= 0; i
< ARRAY_SIZE(bmi323_reg_savestate
); i
++) {
2184 ret
= regmap_read(data
->regmap
, bmi323_reg_savestate
[i
],
2185 &savestate
->reg_settings
[i
]);
2188 "Error reading bmi323 reg 0x%x: %d\n",
2189 bmi323_reg_savestate
[i
], ret
);
2194 for (unsigned int i
= 0; i
< ARRAY_SIZE(bmi323_ext_reg_savestate
); i
++) {
2195 ret
= bmi323_read_ext_reg(data
, bmi323_ext_reg_savestate
[i
],
2196 &savestate
->ext_reg_settings
[i
]);
2199 "Error reading bmi323 external reg 0x%x: %d\n",
2200 bmi323_ext_reg_savestate
[i
], ret
);
2205 /* Perform soft reset to place the device in its lowest power state. */
2206 ret
= regmap_write(data
->regmap
, BMI323_CMD_REG
, BMI323_RST_VAL
);
2213 static int bmi323_core_runtime_resume(struct device
*dev
)
2215 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
2216 struct bmi323_data
*data
= iio_priv(indio_dev
);
2217 struct bmi323_regs_runtime_pm
*savestate
= &data
->runtime_pm_status
;
2221 guard(mutex
)(&data
->mutex
);
2224 * Perform the device power-on and initial setup once again
2225 * after being reset in the lower power state by runtime-pm.
2227 ret
= bmi323_init(data
);
2229 dev_err(data
->dev
, "Device power-on and init failed: %d", ret
);
2233 /* Register must be cleared before changing an active config */
2234 ret
= regmap_write(data
->regmap
, BMI323_FEAT_IO0_REG
, 0);
2236 dev_err(data
->dev
, "Error stopping feature engine\n");
2240 for (unsigned int i
= 0; i
< ARRAY_SIZE(bmi323_ext_reg_savestate
); i
++) {
2241 ret
= bmi323_write_ext_reg(data
, bmi323_ext_reg_savestate
[i
],
2242 savestate
->ext_reg_settings
[i
]);
2245 "Error writing bmi323 external reg 0x%x: %d\n",
2246 bmi323_ext_reg_savestate
[i
], ret
);
2251 for (unsigned int i
= 0; i
< ARRAY_SIZE(bmi323_reg_savestate
); i
++) {
2252 ret
= regmap_write(data
->regmap
, bmi323_reg_savestate
[i
],
2253 savestate
->reg_settings
[i
]);
2256 "Error writing bmi323 reg 0x%x: %d\n",
2257 bmi323_reg_savestate
[i
], ret
);
2263 * Clear old FIFO samples that might be generated before suspend
2264 * or generated from a peripheral state not equal to the saved one.
2266 if (data
->state
== BMI323_BUFFER_FIFO
) {
2267 ret
= regmap_write(data
->regmap
, BMI323_FIFO_CTRL_REG
,
2268 BMI323_FIFO_FLUSH_MSK
);
2270 dev_err(data
->dev
, "Error flushing FIFO buffer: %d\n", ret
);
2275 ret
= regmap_read(data
->regmap
, BMI323_ERR_REG
, &val
);
2278 "Error reading bmi323 error register: %d\n", ret
);
2284 "Sensor power error in PM = 0x%x\n", val
);
2288 return iio_device_resume_triggering(indio_dev
);
2291 const struct dev_pm_ops bmi323_core_pm_ops
= {
2292 RUNTIME_PM_OPS(bmi323_core_runtime_suspend
,
2293 bmi323_core_runtime_resume
, NULL
)
2295 EXPORT_SYMBOL_NS_GPL(bmi323_core_pm_ops
, "IIO_BMI323");
2297 MODULE_DESCRIPTION("Bosch BMI323 IMU driver");
2298 MODULE_AUTHOR("Jagath Jog J <jagathjog1996@gmail.com>");
2299 MODULE_LICENSE("GPL");