[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-Id: <20251014-bma220_events-v1-3-153424d7ea08@subdimension.ro>
Date: Tue, 14 Oct 2025 19:42:59 +0300
From: Petre Rodan <petre.rodan@...dimension.ro>
To: Jonathan Cameron <jic23@...nel.org>,
David Lechner <dlechner@...libre.com>,
Nuno Sá <nuno.sa@...log.com>,
Andy Shevchenko <andy@...nel.org>
Cc: linux-iio@...r.kernel.org, linux-kernel@...r.kernel.org
Subject: [PATCH 3/6] iio: accel: bma220: add tap detection
Add support for tap events.
Signed-off-by: Petre Rodan <petre.rodan@...dimension.ro>
---
Please advise on where should I should be using the mutex guard()
(single regmap reads vs single writes vs more complex read/write combo)
---
drivers/iio/accel/bma220_core.c | 296 +++++++++++++++++++++++++++++++++++++++-
1 file changed, 295 insertions(+), 1 deletion(-)
diff --git a/drivers/iio/accel/bma220_core.c b/drivers/iio/accel/bma220_core.c
index 871342d21456..c4bebf3e5548 100644
--- a/drivers/iio/accel/bma220_core.c
+++ b/drivers/iio/accel/bma220_core.c
@@ -21,6 +21,7 @@
#include <linux/types.h>
#include <linux/iio/buffer.h>
+#include <linux/iio/events.h>
#include <linux/iio/iio.h>
#include <linux/iio/sysfs.h>
#include <linux/iio/trigger.h>
@@ -101,6 +102,25 @@
#define BMA220_COF_64Hz 0x4
#define BMA220_COF_32Hz 0x5
+#define BMA220_TAP_TYPE_DOUBLE 0x0
+#define BMA220_TAP_TYPE_SINGLE 0x1
+
+static const struct iio_event_spec bma220_events[] = {
+ {
+ .type = IIO_EV_TYPE_GESTURE,
+ .dir = IIO_EV_DIR_SINGLETAP,
+ .mask_separate = BIT(IIO_EV_INFO_ENABLE),
+ .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE)
+ },
+ {
+ .type = IIO_EV_TYPE_GESTURE,
+ .dir = IIO_EV_DIR_DOUBLETAP,
+ .mask_separate = BIT(IIO_EV_INFO_ENABLE),
+ .mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
+ BIT(IIO_EV_INFO_PERIOD),
+ },
+};
+
#define BMA220_ACCEL_CHANNEL(index, reg, axis) { \
.type = IIO_ACCEL, \
.address = reg, \
@@ -112,6 +132,8 @@
.info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE) |\
BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
.scan_index = index, \
+ .event_spec = bma220_events, \
+ .num_event_specs = ARRAY_SIZE(bma220_events), \
.scan_type = { \
.sign = 's', \
.realbits = 6, \
@@ -136,6 +158,7 @@ struct bma220_data {
struct mutex lock;
u8 lpf_3dB_freq_idx;
u8 range_idx;
+ u8 tap_type;
struct iio_trigger *trig;
struct {
s8 chans[3];
@@ -231,6 +254,13 @@ static const struct iio_trigger_ops bma220_trigger_ops = {
.validate_device = &iio_trigger_validate_own_device,
};
+static int bma220_reset_int(struct bma220_data *data)
+{
+ return regmap_update_bits(data->regmap, BMA220_REG_IE1,
+ BMA220_INT_RST_MSK,
+ FIELD_PREP(BMA220_INT_RST_MSK, 1));
+}
+
static irqreturn_t bma220_trigger_handler(int irq, void *p)
{
int ret;
@@ -376,6 +406,241 @@ static int bma220_read_avail(struct iio_dev *indio_dev,
}
}
+static int bma220_is_tap_en(struct bma220_data *data,
+ enum iio_modifier axis,
+ int type,
+ bool *en)
+{
+ unsigned int reg_val, val;
+ int ret;
+
+ ret = regmap_read(data->regmap, BMA220_REG_IE0, ®_val);
+ if (ret)
+ return ret;
+
+ switch (axis) {
+ case IIO_MOD_X:
+ *en = FIELD_GET(BMA220_INT_EN_TAP_X_MSK, reg_val);
+ break;
+ case IIO_MOD_Y:
+ *en = FIELD_GET(BMA220_INT_EN_TAP_Y_MSK, reg_val);
+ break;
+ case IIO_MOD_Z:
+ *en = FIELD_GET(BMA220_INT_EN_TAP_Z_MSK, reg_val);
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ if (*en) {
+ ret = regmap_read(data->regmap, BMA220_REG_CONF5, ®_val);
+ if (ret)
+ return ret;
+ val = FIELD_GET(BMA220_TIP_EN_MSK, reg_val);
+ data->tap_type = val;
+/*
+ * the tip_en reg_flag - if '1' it enables SingleTap, '0' DoubleTap
+ * truth table for the logic below, *en has to be switched to false in two cases:
+ * ST DT reg_flag *en
+ * 1 0 0 (DT) false
+ * 1 0 1 (ST) true
+ * 0 1 0 (DT) true
+ * 0 1 1 (ST) false
+ */
+ if ((type == IIO_EV_DIR_DOUBLETAP) && val)
+ *en = false;
+ else if ((type == IIO_EV_DIR_SINGLETAP) && !val)
+ *en = false;
+ }
+
+ return 0;
+}
+
+static int bma220_set_tap_en(struct bma220_data *data,
+ enum iio_modifier axis,
+ int type,
+ bool en)
+{
+ unsigned int flags = BMA220_TAP_TYPE_DOUBLE;
+ int ret;
+
+ switch (axis) {
+ case IIO_MOD_X:
+ ret = regmap_update_bits(data->regmap, BMA220_REG_IE0,
+ BMA220_INT_EN_TAP_X_MSK,
+ FIELD_PREP(BMA220_INT_EN_TAP_X_MSK, en));
+ break;
+ case IIO_MOD_Y:
+ ret = regmap_update_bits(data->regmap, BMA220_REG_IE0,
+ BMA220_INT_EN_TAP_Y_MSK,
+ FIELD_PREP(BMA220_INT_EN_TAP_Y_MSK, en));
+ break;
+ case IIO_MOD_Z:
+ ret = regmap_update_bits(data->regmap, BMA220_REG_IE0,
+ BMA220_INT_EN_TAP_Z_MSK,
+ FIELD_PREP(BMA220_INT_EN_TAP_Z_MSK, en));
+ break;
+ default:
+ return -EINVAL;
+ }
+ if (ret)
+ return ret;
+
+ /* tip_en must be 1 to select singletap detection */
+ if (type == IIO_EV_DIR_SINGLETAP)
+ flags = BMA220_TAP_TYPE_SINGLE;
+
+ ret = regmap_update_bits(data->regmap, BMA220_REG_CONF5,
+ BMA220_TIP_EN_MSK,
+ FIELD_PREP(BMA220_TIP_EN_MSK, flags));
+ if (ret)
+ return ret;
+
+ data->tap_type = flags;
+
+ return 0;
+}
+
+static int bma220_read_event_config(struct iio_dev *indio_dev,
+ const struct iio_chan_spec *chan,
+ enum iio_event_type type,
+ enum iio_event_direction dir)
+{
+ struct bma220_data *data = iio_priv(indio_dev);
+ bool int_en;
+ int ret;
+
+ guard(mutex)(&data->lock);
+
+ switch (type) {
+ case IIO_EV_TYPE_GESTURE:
+ switch (dir) {
+ case IIO_EV_DIR_SINGLETAP:
+ ret = bma220_is_tap_en(data, chan->channel2,
+ IIO_EV_DIR_SINGLETAP, &int_en);
+ if (ret)
+ return ret;
+ return int_en;
+ case IIO_EV_DIR_DOUBLETAP:
+ ret = bma220_is_tap_en(data, chan->channel2,
+ IIO_EV_DIR_DOUBLETAP, &int_en);
+ if (ret)
+ return ret;
+ return int_en;
+ default:
+ return -EINVAL;
+ }
+ default:
+ return -EINVAL;
+ }
+}
+
+static int bma220_write_event_config(struct iio_dev *indio_dev,
+ const struct iio_chan_spec *chan,
+ enum iio_event_type type,
+ enum iio_event_direction dir,
+ bool state)
+{
+ struct bma220_data *data = iio_priv(indio_dev);
+ int ret = 0;
+
+ guard(mutex)(&data->lock);
+
+ switch (type) {
+ case IIO_EV_TYPE_GESTURE:
+ switch (dir) {
+ case IIO_EV_DIR_SINGLETAP:
+ ret = bma220_set_tap_en(data, chan->channel2,
+ IIO_EV_DIR_SINGLETAP, state);
+ break;
+ case IIO_EV_DIR_DOUBLETAP:
+ ret = bma220_set_tap_en(data, chan->channel2,
+ IIO_EV_DIR_DOUBLETAP, state);
+ break;
+ default:
+ return -EINVAL;
+ }
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ if (ret)
+ return ret;
+
+ return bma220_reset_int(data);
+}
+
+static int bma220_read_event_value(struct iio_dev *indio_dev,
+ const struct iio_chan_spec *chan,
+ enum iio_event_type type,
+ enum iio_event_direction dir,
+ enum iio_event_info info,
+ int *val, int *val2)
+{
+ struct bma220_data *data = iio_priv(indio_dev);
+ unsigned int reg_val;
+ int ret;
+
+ guard(mutex)(&data->lock);
+
+ switch (type) {
+ case IIO_EV_TYPE_GESTURE:
+ switch (info) {
+ case IIO_EV_INFO_VALUE:
+ ret = regmap_read(data->regmap, BMA220_REG_CONF3, ®_val);
+ if (ret)
+ return ret;
+ *val = FIELD_GET(BMA220_TT_TH_MSK, reg_val);
+ return IIO_VAL_INT;
+ case IIO_EV_INFO_PERIOD:
+ ret = regmap_read(data->regmap, BMA220_REG_CONF3, ®_val);
+ if (ret)
+ return ret;
+ *val = FIELD_GET(BMA220_TT_DUR_MSK, reg_val);
+ return IIO_VAL_INT;
+ default:
+ return -EINVAL;
+ }
+ default:
+ return -EINVAL;
+ }
+}
+
+static int bma220_write_event_value(struct iio_dev *indio_dev,
+ const struct iio_chan_spec *chan,
+ enum iio_event_type type,
+ enum iio_event_direction dir,
+ enum iio_event_info info,
+ int val, int val2)
+{
+ struct bma220_data *data = iio_priv(indio_dev);
+
+ guard(mutex)(&data->lock);
+
+ switch (type) {
+ case IIO_EV_TYPE_GESTURE:
+ switch (info) {
+ case IIO_EV_INFO_VALUE:
+ if (!FIELD_FIT(BMA220_TT_TH_MSK, val))
+ return -EINVAL;
+ return regmap_update_bits(data->regmap, BMA220_REG_CONF3,
+ BMA220_TT_TH_MSK,
+ FIELD_PREP(BMA220_TT_TH_MSK, val));
+ case IIO_EV_INFO_PERIOD:
+ if (!FIELD_FIT(BMA220_TT_DUR_MSK, val))
+ return -EINVAL;
+ return regmap_update_bits(data->regmap, BMA220_REG_CONF3,
+ BMA220_TT_DUR_MSK,
+ FIELD_PREP(BMA220_TT_DUR_MSK, val));
+ default:
+ return -EINVAL;
+ }
+ default:
+ return -EINVAL;
+ }
+}
+
static int bma220_reg_access(struct iio_dev *indio_dev, unsigned int reg,
unsigned int writeval, unsigned int *readval)
{
@@ -390,6 +655,10 @@ static const struct iio_info bma220_info = {
.read_raw = bma220_read_raw,
.write_raw = bma220_write_raw,
.read_avail = bma220_read_avail,
+ .read_event_config = bma220_read_event_config,
+ .write_event_config = bma220_write_event_config,
+ .read_event_value = bma220_read_event_value,
+ .write_event_value = bma220_write_event_value,
.debugfs_reg_access = &bma220_reg_access,
};
@@ -484,6 +753,8 @@ static int bma220_init(struct device *dev, struct bma220_data *data)
"Failed to set i2c watchdog\n");
}
+ data->tap_type = BMA220_TAP_TYPE_DOUBLE;
+
return 0;
}
@@ -506,13 +777,36 @@ static irqreturn_t bma220_irq_handler(int irq, void *private)
struct bma220_data *data = iio_priv(indio_dev);
int ret;
unsigned int bma220_reg_if1;
+ s64 timestamp = iio_get_time_ns(indio_dev);
+
+ guard(mutex)(&data->lock);
ret = regmap_read(data->regmap, BMA220_REG_IF1, &bma220_reg_if1);
if (ret)
return IRQ_NONE;
- if (FIELD_GET(BMA220_IF_DRDY, bma220_reg_if1))
+ if (FIELD_GET(BMA220_IF_DRDY, bma220_reg_if1)) {
iio_trigger_poll_nested(data->trig);
+ return IRQ_HANDLED;
+ }
+
+ if (FIELD_GET(BMA220_IF_TT, bma220_reg_if1)) {
+
+ if (data->tap_type == BMA220_TAP_TYPE_SINGLE)
+ iio_push_event(indio_dev,
+ IIO_MOD_EVENT_CODE(IIO_ACCEL, 0,
+ IIO_MOD_X_OR_Y_OR_Z,
+ IIO_EV_TYPE_GESTURE,
+ IIO_EV_DIR_SINGLETAP),
+ timestamp);
+ else
+ iio_push_event(indio_dev,
+ IIO_MOD_EVENT_CODE(IIO_ACCEL, 0,
+ IIO_MOD_X_OR_Y_OR_Z,
+ IIO_EV_TYPE_GESTURE,
+ IIO_EV_DIR_DOUBLETAP),
+ timestamp);
+ }
return IRQ_HANDLED;
}
--
2.49.1
Powered by blists - more mailing lists