From nobody Mon Dec 1 22:05:47 2025 Received: from mail-ed1-f47.google.com (mail-ed1-f47.google.com [209.85.208.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 81249304BC6 for ; Mon, 1 Dec 2025 10:00:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.47 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764583247; cv=none; b=n6uGPJVI1nzL+XmLKmg/9+C/BmuGhW5UTeYsrZ1hHMVuBOcsOjSyUQbgP4W60EQ4SRoFaXAr7ezb9xJHR/T7zyux+eOBi/5E2nREj7CI2c84deO4z29gsOZTFpEHtwKbX8T5pjquABZfiJqe1OBHFgI8Pa++T6SukUTAxsXY71M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764583247; c=relaxed/simple; bh=I2wP8/v8+iTwp/IbXvF+LNCirEJ4rMBvnG2GqyV2YMI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=L/4T87tLafD8oH7jaIcQuh0PN0/WczR1XJxwjBBd0wBBu8WG4YQ6WMqM6Dw4lcMy7ABucs1t+1m/7iL8VnmmKGwtIAGK6AvjfIDKJ6VlcN2I8yZOsUu8LHqwk0SUWeN1xD6hnExiN35vcaWxcvvzBE+HJ9uL7dXqYfOW1aOEE20= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=0z9leyOb; arc=none smtp.client-ip=209.85.208.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="0z9leyOb" Received: by mail-ed1-f47.google.com with SMTP id 4fb4d7f45d1cf-6431b0a1948so323192a12.3 for ; Mon, 01 Dec 2025 02:00:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1764583244; x=1765188044; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=0JpMgD46UuIDmNrqgcRKLqCKR6U3UNIGWYWZqhE2Z4U=; b=0z9leyOb78LnpCpQD3Box0saKxyqF9bslEd0zZFjrQXYeyk9vYLHVWtmUQ609YZSaz MfO+F0qCbLTFQvLaSmQBL0O1uWh63tG0dIYnI/P1sp7r4PtfX2vOeCR+0k8p+ndngc4d Yw6QSbCPnnOcaH1CxjShpRSep9Qz+IbctGMYSqUk9t9Ztiyli6LbOFORQp8JpJvI/CyG M1N1UfkcNjtWkBRsVO/4JJYv3h9l/48qtNVQ/JV+s5WMSiH2lrfQtrDTmSJJBC9xflx3 /MEQlEwgQAVbrxnN2PPWc5iYcaC2042yA0i7+Xbe4p0WRi95XRsTu8XNHUQJlGWhmh6s CXaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764583244; x=1765188044; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=0JpMgD46UuIDmNrqgcRKLqCKR6U3UNIGWYWZqhE2Z4U=; b=s8gpXsxDlGPb++8nyYMBlI7tWKqWttb3oritPdE8NmUmJh6neXjjN+/i+4SEMX5DVm NufCTjKxVdZ/1XYbAaYzjMXycgeBWADSabMaLp7Gg1W5UWO500ghviGbnb3e68m3Poo5 i6FkDg12ckLkCOllVpPtbdls/3fwVm3/VzGPh+v4GHeWiz1wq3O2NmrWTDlGDm22ctWX LWAv3uIDLamTDdXHnlMFhCR4rLZdB/vATMLc9Omz8ZHB04E2b+Yo7SKKHS8BTECw4NOx SL/QpX+NS1RlNKWq+78ehN+DtJoKf7JxmeuChTWY4zJQJmyP3vqfYt1eW530sIqf8Ul2 Kruw== X-Forwarded-Encrypted: i=1; AJvYcCWuCJ+ivvy9JR8d5aNcfBIl1saEFJ+HeDtuOv2Xdz1N88b8reNB6Ex1kLcQAKa71ZNmiZ20wfKdZs/dz7U=@vger.kernel.org X-Gm-Message-State: AOJu0YxXZQvRa/OMeX10bYvmf0rFFCCEOARc0UfxSeKytrxJeIXAgK9W 93bgeOc/VOSakJ69iieaR9x9tHH+QCTpa7uXCXzBNf8w8ByU5o3JJ2Az8iw0SUjTX8w= X-Gm-Gg: ASbGnct6f3Cvplqkx2lcNeGDm60eSb9CHJHoWKJPRvP8T1B213z/+T8JXApYYh8d/FW e84TuVkLyZj253ybRIkS3O0ZlM4deca+JuY44sWr8ktn6/KpJraxYmSozhYasL1mJPEujRC0ZlB Bsb5bXwi6dIoUvcwaErTLT39l78wQbm6GIuLf599oArckYgqe0uZPY4aeIk38+Wv3ZsNsW3fzoG NphQC5wiIm4euLTETr8VoA55V3lHSPH+DvkqsqTyjp21KwrkNhWZ9rC0gYoDlI2ZZeR0bqoBDoZ 8fJZvvtwLEjorCnkoDZKWLUshAb/7VlNlQeZB1W1rssiJ7jWCOoFDljw2eKVW+d+wNviuThPXyF /xXmW3IsAaWR2RD9U0yxj5e+qymbWon4TjJN1z7ySQWtLrWrpwfBp8bfX X-Google-Smtp-Source: AGHT+IGGa8xdBt3SXYflxkPq8WEDGUrHlW065EwBJ0L3P1ifoX3XoJocf/urbzR9t6D8lAQEs5qFrA== X-Received: by 2002:a05:6402:5244:b0:645:e6b:4260 with SMTP id 4fb4d7f45d1cf-645eb2a86e4mr23027176a12.20.1764583243703; Mon, 01 Dec 2025 02:00:43 -0800 (PST) Received: from localhost ([151.35.151.28]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-64751035b75sm13049937a12.20.2025.12.01.02.00.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Dec 2025 02:00:43 -0800 (PST) From: Francesco Lavra To: Lorenzo Bianconi , Jonathan Cameron , David Lechner , =?UTF-8?q?Nuno=20S=C3=A1?= , Andy Shevchenko , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Andy Shevchenko Subject: [PATCH v4 6/9] iio: imu: st_lsm6dsx: make event management functions generic Date: Mon, 1 Dec 2025 11:00:15 +0100 Message-Id: <20251201100018.426749-7-flavra@baylibre.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20251201100018.426749-1-flavra@baylibre.com> References: <20251201100018.426749-1-flavra@baylibre.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=openpgp-sha256; l=11222; i=flavra@baylibre.com; h=from:subject; bh=I2wP8/v8+iTwp/IbXvF+LNCirEJ4rMBvnG2GqyV2YMI=; b=owEB7QES/pANAwAKAe3xO3POlDZfAcsmYgBpLWb6dbr+4Y1eg2Sf/ylUoDZfk6O/zYPXRoNug tM7uHmzKSKJAbMEAAEKAB0WIQSGV4VPlTvcox7DFObt8TtzzpQ2XwUCaS1m+gAKCRDt8TtzzpQ2 Xw/wC/9rNAn+KlJXCHUpC4iCxPmIdZvOVan1GorqjR2t6cJzjRkF3ECyPJv1RJgHNNtZOjDtpzN dU1OLYaCy9cEXTyIkzkV2YSKRCJanJ79GLOWN23yilu0Otko5Vgi+b8FL5rqB6m+uxlRWOIpoOK f8QcsIkKXghvkPMDbTVJ0GMldd8SP3Cw8YPCvqi1kRuA+jFu0WqrBekRwO8M3wQOs65F+qRMfUG PqPo7WdRdUUD9ox87lou9jHhhnTzR2sOvY4JFAMeidOZ8tqJLB+OoWBzEvNDEO6KvNEFIW/mwZF jWbLb/bs3pcMtGFDb4qoxhKtjuDi+VYaZzjqGL+ZvfC+ewlTtUFTieE6LfPqKSgPSo1IT+Nn/JL GQb9QLQMniPJqguMsirG9USRU+aWoOVEWPtCTUCw5xzP03JcNJufQzQYJC4Vq1L9vj8asvoR25p URdlE1MpmhPTLl2BFI7ow3ZevtfgoPh+00ZcplYHcvayQskyMLkhUr0Vwby9RRG1xBXRk= X-Developer-Key: i=flavra@baylibre.com; a=openpgp; fpr=8657854F953BDCA31EC314E6EDF13B73CE94365F Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In preparation for adding support for more event types, use an array indexed by event ID instead of a scalar value to store enabled events, and refactor the functions to configure and report events so that their implementation is not specific for wakeup events. Move the logic to update the global event interrupt enable flag from st_lsm6dsx_event_setup() to its calling function, so that it can take into account also event sources different from the source being configured. While changing the signature of the st_lsm6dsx_event_setup() function, opportunistically add the currently unused `axis` parameter, which will be used when adding support for enabling and disabling events on a per axis basis. Signed-off-by: Francesco Lavra Reviewed-by: Andy Shevchenko Acked-by: Lorenzo Bianconi --- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h | 2 +- drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c | 150 ++++++++++++++----- 2 files changed, 110 insertions(+), 42 deletions(-) diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h b/drivers/iio/imu/st_l= sm6dsx/st_lsm6dsx.h index b27a833d5107..2aae56b7db0b 100644 --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx.h @@ -445,7 +445,7 @@ struct st_lsm6dsx_hw { u8 sip; =20 u8 irq_routing; - u8 enable_event; + u8 enable_event[ST_LSM6DSX_EVENT_MAX]; =20 u8 *buff; =20 diff --git a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c b/drivers/iio/imu= /st_lsm6dsx/st_lsm6dsx_core.c index dbdf9bb9e258..6dc6cda54d05 100644 --- a/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c +++ b/drivers/iio/imu/st_lsm6dsx/st_lsm6dsx_core.c @@ -1759,11 +1759,16 @@ static int st_lsm6dsx_check_events(struct st_lsm6dsx_sensor *sensor) { struct st_lsm6dsx_hw *hw =3D sensor->hw; + int event; =20 if (sensor->id !=3D ST_LSM6DSX_ID_ACC) return 0; =20 - return hw->enable_event; + for (event =3D 0; event < ST_LSM6DSX_EVENT_MAX; event++) { + if (hw->enable_event[event]) + return true; + } + return false; } =20 int st_lsm6dsx_sensor_set_enable(struct st_lsm6dsx_sensor *sensor, @@ -1876,9 +1881,10 @@ static int st_lsm6dsx_write_raw(struct iio_dev *iio_= dev, return err; } =20 -static int st_lsm6dsx_event_setup(struct st_lsm6dsx_hw *hw, bool state) +static int st_lsm6dsx_event_setup(struct st_lsm6dsx_hw *hw, + enum st_lsm6dsx_event_id event, int axis, + bool state) { - const struct st_lsm6dsx_reg *reg; const struct st_lsm6dsx_event_src *src; unsigned int data; int err; @@ -1886,22 +1892,24 @@ static int st_lsm6dsx_event_setup(struct st_lsm6dsx= _hw *hw, bool state) if (!hw->irq_routing) return -ENOTSUPP; =20 - reg =3D &hw->settings->event_settings.enable_reg; - if (reg->addr) { - data =3D ST_LSM6DSX_SHIFT_VAL(state, reg->mask); - err =3D st_lsm6dsx_update_bits_locked(hw, reg->addr, - reg->mask, data); - if (err < 0) - return err; - } - - /* Enable wakeup interrupt */ - src =3D &hw->settings->event_settings.sources[ST_LSM6DSX_EVENT_WAKEUP]; + /* Enable/disable event interrupt */ + src =3D &hw->settings->event_settings.sources[event]; data =3D ST_LSM6DSX_SHIFT_VAL(state, src->enable_mask); return st_lsm6dsx_update_bits_locked(hw, hw->irq_routing, src->enable_mask, data); } =20 +static enum st_lsm6dsx_event_id +st_lsm6dsx_get_event_id(enum iio_event_type type) +{ + switch (type) { + case IIO_EV_TYPE_THRESH: + return ST_LSM6DSX_EVENT_WAKEUP; + default: + return ST_LSM6DSX_EVENT_MAX; + } +} + static int st_lsm6dsx_read_event(struct iio_dev *iio_dev, const struct iio_chan_spec *chan, enum iio_event_type type, @@ -1909,16 +1917,17 @@ static int st_lsm6dsx_read_event(struct iio_dev *ii= o_dev, enum iio_event_info info, int *val, int *val2) { + enum st_lsm6dsx_event_id event =3D st_lsm6dsx_get_event_id(type); struct st_lsm6dsx_sensor *sensor =3D iio_priv(iio_dev); struct st_lsm6dsx_hw *hw =3D sensor->hw; const struct st_lsm6dsx_reg *reg; u8 data; int err; =20 - if (type !=3D IIO_EV_TYPE_THRESH) + if (event =3D=3D ST_LSM6DSX_EVENT_MAX) return -EINVAL; =20 - reg =3D &hw->settings->event_settings.sources[ST_LSM6DSX_EVENT_WAKEUP].va= lue; + reg =3D &hw->settings->event_settings.sources[event].value; err =3D st_lsm6dsx_read_locked(hw, reg->addr, &data, sizeof(data)); if (err < 0) return err; @@ -1937,19 +1946,20 @@ st_lsm6dsx_write_event(struct iio_dev *iio_dev, enum iio_event_info info, int val, int val2) { + enum st_lsm6dsx_event_id event =3D st_lsm6dsx_get_event_id(type); struct st_lsm6dsx_sensor *sensor =3D iio_priv(iio_dev); struct st_lsm6dsx_hw *hw =3D sensor->hw; const struct st_lsm6dsx_reg *reg; unsigned int data; int err; =20 - if (type !=3D IIO_EV_TYPE_THRESH) + if (event =3D=3D ST_LSM6DSX_EVENT_MAX) return -EINVAL; =20 if (val < 0 || val > 31) return -EINVAL; =20 - reg =3D &hw->settings->event_settings.sources[ST_LSM6DSX_EVENT_WAKEUP].va= lue; + reg =3D &hw->settings->event_settings.sources[event].value; data =3D ST_LSM6DSX_SHIFT_VAL(val, reg->mask); err =3D st_lsm6dsx_update_bits_locked(hw, reg->addr, reg->mask, data); @@ -1965,13 +1975,56 @@ st_lsm6dsx_read_event_config(struct iio_dev *iio_de= v, enum iio_event_type type, enum iio_event_direction dir) { + enum st_lsm6dsx_event_id event =3D st_lsm6dsx_get_event_id(type); struct st_lsm6dsx_sensor *sensor =3D iio_priv(iio_dev); struct st_lsm6dsx_hw *hw =3D sensor->hw; =20 - if (type !=3D IIO_EV_TYPE_THRESH) + if (event =3D=3D ST_LSM6DSX_EVENT_MAX) return -EINVAL; =20 - return !!(hw->enable_event & BIT(chan->channel2)); + return !!(hw->enable_event[event] & BIT(chan->channel2)); +} + +/** + * st_lsm6dsx_check_other_events - Check for enabled sensor events. + * @hw: Sensor hardware instance. + * @curr: Current event type. + * + * Return: whether any events other than @curr are enabled. + */ +static bool st_lsm6dsx_check_other_events(struct st_lsm6dsx_hw *hw, + enum st_lsm6dsx_event_id curr) +{ + enum st_lsm6dsx_event_id other; + + for (other =3D 0; other < ST_LSM6DSX_EVENT_MAX; other++) { + if (other !=3D curr && hw->enable_event[other]) + return true; + } + + return false; +} + +static int st_lsm6dsx_events_enable(struct st_lsm6dsx_sensor *sensor, + bool state) +{ + struct st_lsm6dsx_hw *hw =3D sensor->hw; + const struct st_lsm6dsx_reg *reg; + + reg =3D &hw->settings->event_settings.enable_reg; + if (reg->addr) { + int err; + + err =3D regmap_update_bits(hw->regmap, reg->addr, reg->mask, + ST_LSM6DSX_SHIFT_VAL(state, reg->mask)); + if (err) + return err; + } + + if (state || !(hw->fifo_mask & BIT(sensor->id))) + return __st_lsm6dsx_sensor_set_enable(sensor, state); + + return 0; } =20 static int @@ -1980,22 +2033,23 @@ st_lsm6dsx_write_event_config(struct iio_dev *iio_d= ev, enum iio_event_type type, enum iio_event_direction dir, bool state) { + enum st_lsm6dsx_event_id event =3D st_lsm6dsx_get_event_id(type); struct st_lsm6dsx_sensor *sensor =3D iio_priv(iio_dev); struct st_lsm6dsx_hw *hw =3D sensor->hw; u8 enable_event; int err; =20 - if (type !=3D IIO_EV_TYPE_THRESH) + if (event =3D=3D ST_LSM6DSX_EVENT_MAX) return -EINVAL; =20 if (state) { - enable_event =3D hw->enable_event | BIT(chan->channel2); + enable_event =3D hw->enable_event[event] | BIT(chan->channel2); =20 /* do not enable events if they are already enabled */ - if (hw->enable_event) + if (hw->enable_event[event]) goto out; } else { - enable_event =3D hw->enable_event & ~BIT(chan->channel2); + enable_event =3D hw->enable_event[event] & ~BIT(chan->channel2); =20 /* only turn off sensor if no events is enabled */ if (enable_event) @@ -2003,22 +2057,24 @@ st_lsm6dsx_write_event_config(struct iio_dev *iio_d= ev, } =20 /* stop here if no changes have been made */ - if (hw->enable_event =3D=3D enable_event) + if (hw->enable_event[event] =3D=3D enable_event) return 0; =20 - err =3D st_lsm6dsx_event_setup(hw, state); + err =3D st_lsm6dsx_event_setup(hw, event, chan->channel2, state); if (err < 0) return err; =20 mutex_lock(&hw->conf_lock); - if (enable_event || !(hw->fifo_mask & BIT(sensor->id))) - err =3D __st_lsm6dsx_sensor_set_enable(sensor, state); + if (enable_event) + err =3D st_lsm6dsx_events_enable(sensor, true); + else if (!st_lsm6dsx_check_other_events(hw, event)) + err =3D st_lsm6dsx_events_enable(sensor, false); mutex_unlock(&hw->conf_lock); if (err < 0) return err; =20 out: - hw->enable_event =3D enable_event; + hw->enable_event[event] =3D enable_event; =20 return 0; } @@ -2429,18 +2485,19 @@ static struct iio_dev *st_lsm6dsx_alloc_iiodev(stru= ct st_lsm6dsx_hw *hw, } =20 static bool -st_lsm6dsx_report_motion_event(struct st_lsm6dsx_hw *hw) +st_lsm6dsx_report_events(struct st_lsm6dsx_hw *hw, enum st_lsm6dsx_event_i= d id, + enum iio_event_type type, enum iio_event_direction dir) { const struct st_lsm6dsx_event_settings *event_settings; const struct st_lsm6dsx_event_src *src; int err, data; s64 timestamp; =20 - if (!hw->enable_event) + if (!hw->enable_event[id]) return false; =20 event_settings =3D &hw->settings->event_settings; - src =3D &event_settings->sources[ST_LSM6DSX_EVENT_WAKEUP]; + src =3D &event_settings->sources[id]; err =3D st_lsm6dsx_read_locked(hw, src->status.addr, &data, sizeof(data)); if (err < 0) @@ -2448,38 +2505,49 @@ st_lsm6dsx_report_motion_event(struct st_lsm6dsx_hw= *hw) =20 timestamp =3D iio_get_time_ns(hw->iio_devs[ST_LSM6DSX_ID_ACC]); if ((data & src->status_z_mask) && - (hw->enable_event & BIT(IIO_MOD_Z))) + (hw->enable_event[id] & BIT(IIO_MOD_Z))) iio_push_event(hw->iio_devs[ST_LSM6DSX_ID_ACC], IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_Z, - IIO_EV_TYPE_THRESH, - IIO_EV_DIR_EITHER), + type, + dir), timestamp); =20 if ((data & src->status_y_mask) && - (hw->enable_event & BIT(IIO_MOD_Y))) + (hw->enable_event[id] & BIT(IIO_MOD_Y))) iio_push_event(hw->iio_devs[ST_LSM6DSX_ID_ACC], IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_Y, - IIO_EV_TYPE_THRESH, - IIO_EV_DIR_EITHER), + type, + dir), timestamp); =20 if ((data & src->status_x_mask) && - (hw->enable_event & BIT(IIO_MOD_X))) + (hw->enable_event[id] & BIT(IIO_MOD_X))) iio_push_event(hw->iio_devs[ST_LSM6DSX_ID_ACC], IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X, - IIO_EV_TYPE_THRESH, - IIO_EV_DIR_EITHER), + type, + dir), timestamp); =20 return data & src->status.mask; } =20 +static bool st_lsm6dsx_report_motion_event(struct st_lsm6dsx_hw *hw) +{ + bool events_found; + + events_found =3D st_lsm6dsx_report_events(hw, ST_LSM6DSX_EVENT_WAKEUP, + IIO_EV_TYPE_THRESH, + IIO_EV_DIR_EITHER); + + return events_found; +} + static irqreturn_t st_lsm6dsx_handler_thread(int irq, void *private) { struct st_lsm6dsx_hw *hw =3D private; --=20 2.39.5