Skip to content

Commit ea39f4c

Browse files
ggrsjic23
authored andcommitted
iio: imu: bmi270: add step counter watermark event
Add support for generating events when the step counter reaches the configurable watermark. Reviewed-by: Andy Shevchenko <andy@kernel.org> Signed-off-by: Gustavo Silva <gustavograzs@gmail.com> Link: https://patch.msgid.link/20250616-bmi270-events-v3-2-16e37588604f@gmail.com Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
1 parent c65ce2c commit ea39f4c

File tree

1 file changed

+166
-3
lines changed

1 file changed

+166
-3
lines changed

drivers/iio/imu/bmi270/bmi270_core.c

Lines changed: 166 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <linux/regmap.h>
99
#include <linux/units.h>
1010

11+
#include <linux/iio/events.h>
1112
#include <linux/iio/iio.h>
1213
#include <linux/iio/sysfs.h>
1314
#include <linux/iio/trigger.h>
@@ -28,6 +29,9 @@
2829
#define BMI270_ACCEL_X_REG 0x0c
2930
#define BMI270_ANG_VEL_X_REG 0x12
3031

32+
#define BMI270_INT_STATUS_0_REG 0x1c
33+
#define BMI270_INT_STATUS_0_STEP_CNT_MSK BIT(1)
34+
3135
#define BMI270_INT_STATUS_1_REG 0x1d
3236
#define BMI270_INT_STATUS_1_ACC_GYR_DRDY_MSK GENMASK(7, 6)
3337

@@ -74,6 +78,10 @@
7478
#define BMI270_INT_LATCH_REG 0x55
7579
#define BMI270_INT_LATCH_REG_MSK BIT(0)
7680

81+
#define BMI270_INT1_MAP_FEAT_REG 0x56
82+
#define BMI270_INT2_MAP_FEAT_REG 0x57
83+
#define BMI270_INT_MAP_FEAT_STEP_CNT_WTRMRK_MSK BIT(1)
84+
7785
#define BMI270_INT_MAP_DATA_REG 0x58
7886
#define BMI270_INT_MAP_DATA_DRDY_INT1_MSK BIT(2)
7987
#define BMI270_INT_MAP_DATA_DRDY_INT2_MSK BIT(6)
@@ -94,13 +102,18 @@
94102
#define BMI270_PWR_CTRL_ACCEL_EN_MSK BIT(2)
95103
#define BMI270_PWR_CTRL_TEMP_EN_MSK BIT(3)
96104

105+
#define BMI270_STEP_SC26_WTRMRK_MSK GENMASK(9, 0)
97106
#define BMI270_STEP_SC26_RST_CNT_MSK BIT(10)
98107
#define BMI270_STEP_SC26_EN_CNT_MSK BIT(12)
99108

100109
/* See datasheet section 4.6.14, Temperature Sensor */
101110
#define BMI270_TEMP_OFFSET 11776
102111
#define BMI270_TEMP_SCALE 1953125
103112

113+
/* See page 90 of datasheet. The step counter "holds implicitly a 20x factor" */
114+
#define BMI270_STEP_COUNTER_FACTOR 20
115+
#define BMI270_STEP_COUNTER_MAX 20460
116+
104117
#define BMI260_INIT_DATA_FILE "bmi260-init-data.fw"
105118
#define BMI270_INIT_DATA_FILE "bmi270-init-data.fw"
106119

@@ -396,6 +409,36 @@ static int bmi270_read_steps(struct bmi270_data *data, int *val)
396409
return IIO_VAL_INT;
397410
}
398411

412+
static int bmi270_int_map_reg(enum bmi270_irq_pin pin)
413+
{
414+
switch (pin) {
415+
case BMI270_IRQ_INT1:
416+
return BMI270_INT1_MAP_FEAT_REG;
417+
case BMI270_IRQ_INT2:
418+
return BMI270_INT2_MAP_FEAT_REG;
419+
default:
420+
return -EINVAL;
421+
}
422+
}
423+
424+
static int bmi270_step_wtrmrk_en(struct bmi270_data *data, bool state)
425+
{
426+
int reg;
427+
428+
guard(mutex)(&data->mutex);
429+
if (!data->steps_enabled)
430+
return -EINVAL;
431+
432+
reg = bmi270_int_map_reg(data->irq_pin);
433+
if (reg < 0)
434+
return reg;
435+
436+
return regmap_update_bits(data->regmap, reg,
437+
BMI270_INT_MAP_FEAT_STEP_CNT_WTRMRK_MSK,
438+
FIELD_PREP(BMI270_INT_MAP_FEAT_STEP_CNT_WTRMRK_MSK,
439+
state));
440+
}
441+
399442
static int bmi270_set_scale(struct bmi270_data *data, int chan_type, int uscale)
400443
{
401444
int i;
@@ -552,19 +595,31 @@ static irqreturn_t bmi270_irq_thread_handler(int irq, void *private)
552595
{
553596
struct iio_dev *indio_dev = private;
554597
struct bmi270_data *data = iio_priv(indio_dev);
555-
unsigned int status;
598+
unsigned int status0, status1;
599+
s64 timestamp = iio_get_time_ns(indio_dev);
556600
int ret;
557601

558602
scoped_guard(mutex, &data->mutex) {
603+
ret = regmap_read(data->regmap, BMI270_INT_STATUS_0_REG,
604+
&status0);
605+
if (ret)
606+
return IRQ_NONE;
607+
559608
ret = regmap_read(data->regmap, BMI270_INT_STATUS_1_REG,
560-
&status);
609+
&status1);
561610
if (ret)
562611
return IRQ_NONE;
563612
}
564613

565-
if (FIELD_GET(BMI270_INT_STATUS_1_ACC_GYR_DRDY_MSK, status))
614+
if (FIELD_GET(BMI270_INT_STATUS_1_ACC_GYR_DRDY_MSK, status1))
566615
iio_trigger_poll_nested(data->trig);
567616

617+
if (FIELD_GET(BMI270_INT_STATUS_0_STEP_CNT_MSK, status0))
618+
iio_push_event(indio_dev, IIO_UNMOD_EVENT_CODE(IIO_STEPS, 0,
619+
IIO_EV_TYPE_CHANGE,
620+
IIO_EV_DIR_NONE),
621+
timestamp);
622+
568623
return IRQ_HANDLED;
569624
}
570625

@@ -772,10 +827,116 @@ static int bmi270_read_avail(struct iio_dev *indio_dev,
772827
}
773828
}
774829

830+
static int bmi270_write_event_config(struct iio_dev *indio_dev,
831+
const struct iio_chan_spec *chan,
832+
enum iio_event_type type,
833+
enum iio_event_direction dir, bool state)
834+
{
835+
struct bmi270_data *data = iio_priv(indio_dev);
836+
837+
switch (type) {
838+
case IIO_EV_TYPE_CHANGE:
839+
return bmi270_step_wtrmrk_en(data, state);
840+
default:
841+
return -EINVAL;
842+
}
843+
}
844+
845+
static int bmi270_read_event_config(struct iio_dev *indio_dev,
846+
const struct iio_chan_spec *chan,
847+
enum iio_event_type type,
848+
enum iio_event_direction dir)
849+
{
850+
struct bmi270_data *data = iio_priv(indio_dev);
851+
int ret, reg, regval;
852+
853+
guard(mutex)(&data->mutex);
854+
855+
switch (chan->type) {
856+
case IIO_STEPS:
857+
reg = bmi270_int_map_reg(data->irq_pin);
858+
if (reg)
859+
return reg;
860+
861+
ret = regmap_read(data->regmap, reg, &regval);
862+
if (ret)
863+
return ret;
864+
return FIELD_GET(BMI270_INT_MAP_FEAT_STEP_CNT_WTRMRK_MSK,
865+
regval) ? 1 : 0;
866+
default:
867+
return -EINVAL;
868+
}
869+
}
870+
871+
static int bmi270_write_event_value(struct iio_dev *indio_dev,
872+
const struct iio_chan_spec *chan,
873+
enum iio_event_type type,
874+
enum iio_event_direction dir,
875+
enum iio_event_info info,
876+
int val, int val2)
877+
{
878+
struct bmi270_data *data = iio_priv(indio_dev);
879+
unsigned int raw;
880+
881+
guard(mutex)(&data->mutex);
882+
883+
switch (type) {
884+
case IIO_EV_TYPE_CHANGE:
885+
if (!in_range(val, 0, BMI270_STEP_COUNTER_MAX + 1))
886+
return -EINVAL;
887+
888+
raw = val / BMI270_STEP_COUNTER_FACTOR;
889+
return bmi270_update_feature_reg(data, BMI270_SC_26_REG,
890+
BMI270_STEP_SC26_WTRMRK_MSK,
891+
FIELD_PREP(BMI270_STEP_SC26_WTRMRK_MSK,
892+
raw));
893+
default:
894+
return -EINVAL;
895+
}
896+
}
897+
898+
static int bmi270_read_event_value(struct iio_dev *indio_dev,
899+
const struct iio_chan_spec *chan,
900+
enum iio_event_type type,
901+
enum iio_event_direction dir,
902+
enum iio_event_info info,
903+
int *val, int *val2)
904+
{
905+
struct bmi270_data *data = iio_priv(indio_dev);
906+
unsigned int raw;
907+
u16 regval;
908+
int ret;
909+
910+
guard(mutex)(&data->mutex);
911+
912+
switch (type) {
913+
case IIO_EV_TYPE_CHANGE:
914+
ret = bmi270_read_feature_reg(data, BMI270_SC_26_REG, &regval);
915+
if (ret)
916+
return ret;
917+
918+
raw = FIELD_GET(BMI270_STEP_SC26_WTRMRK_MSK, regval);
919+
*val = raw * BMI270_STEP_COUNTER_FACTOR;
920+
return IIO_VAL_INT;
921+
default:
922+
return -EINVAL;
923+
}
924+
}
925+
926+
static const struct iio_event_spec bmi270_step_wtrmrk_event = {
927+
.type = IIO_EV_TYPE_CHANGE,
928+
.dir = IIO_EV_DIR_NONE,
929+
.mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE) | BIT(IIO_EV_INFO_VALUE),
930+
};
931+
775932
static const struct iio_info bmi270_info = {
776933
.read_raw = bmi270_read_raw,
777934
.write_raw = bmi270_write_raw,
778935
.read_avail = bmi270_read_avail,
936+
.write_event_config = bmi270_write_event_config,
937+
.read_event_config = bmi270_read_event_config,
938+
.write_event_value = bmi270_write_event_value,
939+
.read_event_value = bmi270_read_event_value,
779940
};
780941

781942
#define BMI270_ACCEL_CHANNEL(_axis) { \
@@ -835,6 +996,8 @@ static const struct iio_chan_spec bmi270_channels[] = {
835996
.info_mask_separate = BIT(IIO_CHAN_INFO_ENABLE) |
836997
BIT(IIO_CHAN_INFO_PROCESSED),
837998
.scan_index = -1, /* No buffer support */
999+
.event_spec = &bmi270_step_wtrmrk_event,
1000+
.num_event_specs = 1,
8381001
},
8391002
IIO_CHAN_SOFT_TIMESTAMP(BMI270_SCAN_TIMESTAMP),
8401003
};

0 commit comments

Comments
 (0)