From nobody Sat Jul 25 05:30:16 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D8E5B3998B1; Fri, 17 Jul 2026 12:39:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784291994; cv=none; b=HB+1D1smz21c+7TTkpU4sF1YLTblynY9mrL88sqkpBIqrsla8mu28Xu45PpA54lW7T2ymKtZWta7twTOLTwXaHfUt8fkQK+bE+0FhpYeUP7mPFV/tNhXVthvM6ZX9FxDFTOw/J9Oj6M4zp+4rhKS+lEgqxydaQOg42F2gBH1hgA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784291994; c=relaxed/simple; bh=QBDKufXgajqn4DtRASDavNy7maqfFljBFPBJkbiaTxI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=TbNQJ85n5zZWq6NVYGRGyWrQt8avnrqtA4p0ArOiLRhUy+14+mB/OF8Qs2EFv4fJ34VKR40QUsBLF/xiDbvdUkxBKWq1fUhE3q7qeEUXQFwbtgdr/AJrJT+EBcnk60A0vGSm4neletDBFCiC37ZP1cXpZp2Ex4dVeiNxtqqTWWI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=chREGJh8; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="chREGJh8" Received: by smtp.kernel.org (Postfix) with ESMTPS id 9FAB6C2BCB9; Fri, 17 Jul 2026 12:39:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1784291994; bh=QBDKufXgajqn4DtRASDavNy7maqfFljBFPBJkbiaTxI=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=chREGJh8YBCBRvApjUMA1YZa/PZDveX5pph97lLXEwoZT8nf8dBOe8vUkYaODE91M EGNAEpe6FrkRJgYG56fZrI8+gKijvy6Jeyaj8GOTcj4Tudv6IBSF0vTvBsxa/yrhgn WZabl9hzKAa4KMGEJIv9ErcMnoN/iy/LiUDCy7QpipQjaTK7/xCfihHCMQsJXFo2T8 H6FoXLkCRVHn3uREayX5X+AGJh9BV7Cq/XVxW2V69dzcnJfd47E0G6FvfATUr9eXiM Jg61V7kWtPvFH/WQo89aTLjwEb3h4UptrlOKW5R4L9cM8upSQihuR7rH+tvzw+VTnq 9blS1K+EU/y+g== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7C68EC4451C; Fri, 17 Jul 2026 12:39:54 +0000 (UTC) From: Jean-Baptiste Maneyrol via B4 Relay Date: Fri, 17 Jul 2026 14:39:40 +0200 Subject: [PATCH 1/2] iio: invensense: better timestamp alignment when using watermark Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260717-iio-common-inv-sensors-timestamp-rework-v1-1-d1afee2805cd@tdk.com> References: <20260717-iio-common-inv-sensors-timestamp-rework-v1-0-d1afee2805cd@tdk.com> In-Reply-To: <20260717-iio-common-inv-sensors-timestamp-rework-v1-0-d1afee2805cd@tdk.com> To: Jonathan Cameron , David Lechner , =?utf-8?q?Nuno_S=C3=A1?= , Andy Shevchenko Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Jean-Baptiste Maneyrol X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1784291993; l=4319; i=jean-baptiste.maneyrol@tdk.com; s=20240923; h=from:subject:message-id; bh=CuzodI1iMOle4XQ64WKBjQvz2G3+BooQH4bpQ3wpLYY=; b=/zIaFrKHQx02nZE4pfeN8YFFlpvMXtgarZijbCKBcAVviuDEV488UHT2RTDIcmP/K7m5Dm7OT og81yPFMwmMCb1hm7c0wjsnrOs3VVDj48Pp1O40cllGlhww3LkLvMOB X-Developer-Key: i=jean-baptiste.maneyrol@tdk.com; a=ed25519; pk=bRqF1WYk0hR3qrnAithOLXSD0LvSu8DUd+quKLxCicI= X-Endpoint-Received: by B4 Relay for jean-baptiste.maneyrol@tdk.com/20240923 with auth_id=218 X-Original-From: Jean-Baptiste Maneyrol Reply-To: jean-baptiste.maneyrol@tdk.com From: Jean-Baptiste Maneyrol Current interrupt timestamp alignment only change the next coming timestamp. For watermark where we have a batch of samples per interrupt, it doesn't manage to align timestamp since modification is limited because of jitter. Implement a better version that instead modify the period to align to interrupt timestamp. Period is now computed by align timestamp if interrupt interval is valid, or we use the computed estimation. Signed-off-by: Jean-Baptiste Maneyrol --- .../iio/common/inv_sensors/inv_sensors_timestamp.c | 55 ++++++++----------= ---- 1 file changed, 20 insertions(+), 35 deletions(-) diff --git a/drivers/iio/common/inv_sensors/inv_sensors_timestamp.c b/drive= rs/iio/common/inv_sensors/inv_sensors_timestamp.c index e0b10366ed2b..2c89e0edc87d 100644 --- a/drivers/iio/common/inv_sensors/inv_sensors_timestamp.c +++ b/drivers/iio/common/inv_sensors/inv_sensors_timestamp.c @@ -10,9 +10,7 @@ =20 #include =20 -/* compute jitter, min and max following jitter in per mille */ -#define INV_SENSORS_TIMESTAMP_JITTER(_val, _jitter) \ - (div_s64((_val) * (_jitter), 1000)) +/* compute min and max following jitter in per mille */ #define INV_SENSORS_TIMESTAMP_MIN(_val, _jitter) \ (((_val) * (1000 - (_jitter))) / 1000) #define INV_SENSORS_TIMESTAMP_MAX(_val, _jitter) \ @@ -102,34 +100,22 @@ static bool inv_update_chip_period(struct inv_sensors= _timestamp *ts, /* update chip internal period estimation */ new_chip_period =3D period / ts->mult; inv_update_acc(&ts->chip_period, new_chip_period); - ts->period =3D ts->mult * ts->chip_period.val; =20 return true; } =20 -static void inv_align_timestamp_it(struct inv_sensors_timestamp *ts) +static uint32_t inv_align_timestamp_it(struct inv_sensors_timestamp *ts, + unsigned int sample_nb) { - const int64_t period_min =3D (int64_t)ts->min_period * ts->mult; - const int64_t period_max =3D (int64_t)ts->max_period * ts->mult; - int64_t add_max, sub_max; - int64_t delta, jitter; - int64_t adjust; - - /* delta time between last sample and last interrupt */ - delta =3D ts->it.lo - ts->timestamp; - - /* adjust timestamp while respecting jitter */ - add_max =3D period_max - (int64_t)ts->period; - sub_max =3D period_min - (int64_t)ts->period; - jitter =3D INV_SENSORS_TIMESTAMP_JITTER((int64_t)ts->period, ts->chip.jit= ter); - if (delta > jitter) - adjust =3D add_max; - else if (delta < -jitter) - adjust =3D sub_max; - else - adjust =3D 0; + const uint64_t period_min =3D (uint64_t)ts->min_period * ts->mult; + const uint64_t period_max =3D (uint64_t)ts->max_period * ts->mult; + uint32_t new_period; =20 - ts->timestamp +=3D adjust; + /* compute new period aligning last timestamp with interrupt timestamp */ + new_period =3D div_s64(ts->it.up - ts->timestamp, sample_nb); + + /* ensure that period never overflows the jitter */ + return clamp(new_period, period_min, period_max); } =20 void inv_sensors_timestamp_interrupt(struct inv_sensors_timestamp *ts, @@ -143,6 +129,13 @@ void inv_sensors_timestamp_interrupt(struct inv_sensor= s_timestamp *ts, if (sample_nb =3D=3D 0) return; =20 + /* no previous data, compute theoretical value from interrupt */ + if (ts->timestamp =3D=3D 0) { + /* elapsed time: sensor period * sensor samples number */ + interval =3D (int64_t)ts->period * (int64_t)sample_nb; + ts->timestamp =3D timestamp - interval; + } + /* update interrupt timestamp and compute chip and sensor periods */ it =3D &ts->it; it->lo =3D it->up; @@ -154,17 +147,9 @@ void inv_sensors_timestamp_interrupt(struct inv_sensor= s_timestamp *ts, valid =3D inv_update_chip_period(ts, period); } =20 - /* no previous data, compute theoretical value from interrupt */ - if (ts->timestamp =3D=3D 0) { - /* elapsed time: sensor period * sensor samples number */ - interval =3D (int64_t)ts->period * (int64_t)sample_nb; - ts->timestamp =3D it->up - interval; - return; - } - /* if interrupt interval is valid, sync with interrupt timestamp */ - if (valid) - inv_align_timestamp_it(ts); + ts->period =3D valid ? inv_align_timestamp_it(ts, sample_nb) : + ts->mult * ts->chip_period.val; } EXPORT_SYMBOL_NS_GPL(inv_sensors_timestamp_interrupt, "IIO_INV_SENSORS_TIM= ESTAMP"); =20 --=20 2.54.0 From nobody Sat Jul 25 05:30:16 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DD44539B4BC; Fri, 17 Jul 2026 12:39:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784291994; cv=none; b=nGdMyq7nkV6O7olAVwNRmJ7a4EWREjT6hv7iYM3uFDNYeXJMqkEaJ9sirBUSQEF1S099zIf60um+hWnbahpkMZL9wEo/Dp0WuAGgFFf+C8/GG5d8esh2dUQLHGL1M0OY7xNi9hoIz3EXFShgHO9JJChYQr958Jzhm7PbRLB/ygo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784291994; c=relaxed/simple; bh=sgNDHJb3RKgnT1vrDMA6ETnGqUQSBLnSbjiP8j2PMj0=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=pFb0nhVnpBlfXZaddqfN64V+0CaDX1tU8WKb9RrOOM9ihJTUVUSjim87DFmb44QPngaKcMRalQwEm2ioA5D8x1VSapGfTIjEKoyTksz7kA2xbtG8Gf2Lklo+TRLw5T4JWx0YYeBf9jqf5+NwyE2hfsk4tbNOPBqX9sUhCdzaz1U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=E37kNoqN; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="E37kNoqN" Received: by smtp.kernel.org (Postfix) with ESMTPS id B240CC2BCF6; Fri, 17 Jul 2026 12:39:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1784291994; bh=sgNDHJb3RKgnT1vrDMA6ETnGqUQSBLnSbjiP8j2PMj0=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=E37kNoqNBnL9MxC4gniH+1NrHMROWgLd2JLanN89DyStR/C+QhpM2SB3mDqeRUmSk 1ueH+lClVGB7X4yi/TQnYRJQuHdWezgsKHotSd8V3PAuTy0n4ztuS6C68IgRclIlwB Ciyisdi4Iry/cRslIsX+hkersSrumq0zQ9ejhNCXQimMq4nGFCwpEqzMClgZ2S6GbM 9N75EWLdpwwzHGSnHm2vXBVt7RYjHJfVR74MwGNBfJuOcYXWEEwpfuGZavbgSFOYmS xs07Xriy6Ilpy00/31KShvNbSR164AKjnECGTKotPMR9JF77wwiJ25KxFVygSwkQXw Yo3cIK8iF6wBg== Received: from aws-us-west-2-korg-lkml-1.web.codeaurora.org (localhost.localdomain [127.0.0.1]) by smtp.lore.kernel.org (Postfix) with ESMTP id 96B6AC44520; Fri, 17 Jul 2026 12:39:54 +0000 (UTC) From: Jean-Baptiste Maneyrol via B4 Relay Date: Fri, 17 Jul 2026 14:39:41 +0200 Subject: [PATCH 2/2] iio: invensense: improve period measurement by using a longer delay Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260717-iio-common-inv-sensors-timestamp-rework-v1-2-d1afee2805cd@tdk.com> References: <20260717-iio-common-inv-sensors-timestamp-rework-v1-0-d1afee2805cd@tdk.com> In-Reply-To: <20260717-iio-common-inv-sensors-timestamp-rework-v1-0-d1afee2805cd@tdk.com> To: Jonathan Cameron , David Lechner , =?utf-8?q?Nuno_S=C3=A1?= , Andy Shevchenko Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Jean-Baptiste Maneyrol X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1784291993; l=4521; i=jean-baptiste.maneyrol@tdk.com; s=20240923; h=from:subject:message-id; bh=QaB1sqyTIlaV5WHBIAd4mGNxiNij+cw0Vv5qDbn6Sr4=; b=aiCdjaLn9gJqnWHa6UH0pXxfZt/qHxBg0e73Luv8nALJmZXVHxaZxUYOMVB9Jwj49buy98D9y IvO6fFf82+iAo4P5SpQXeLHspXm8Wpv0fPtHbFagN2uzhY3C6PP0mo+ X-Developer-Key: i=jean-baptiste.maneyrol@tdk.com; a=ed25519; pk=bRqF1WYk0hR3qrnAithOLXSD0LvSu8DUd+quKLxCicI= X-Endpoint-Received: by B4 Relay for jean-baptiste.maneyrol@tdk.com/20240923 with auth_id=218 X-Original-From: Jean-Baptiste Maneyrol Reply-To: jean-baptiste.maneyrol@tdk.com From: Jean-Baptiste Maneyrol Period measurement can be difficult when using high sampling frequency where the jitter criteria is hard to meet because of the system jitter. This new version is using the delta time between 2 distant interrupts to measure an interval of at least 20ms. 20ms is a good compromise between the mitigation of system jitter and the delay to update period. This way we decorrelate the period measurement from the interrupt timestamps syncing using only the 2 last interrupts. Signed-off-by: Jean-Baptiste Maneyrol --- .../iio/common/inv_sensors/inv_sensors_timestamp.c | 31 +++++++++++++++++-= ---- include/linux/iio/common/inv_sensors_timestamp.h | 6 +++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/drivers/iio/common/inv_sensors/inv_sensors_timestamp.c b/drive= rs/iio/common/inv_sensors/inv_sensors_timestamp.c index 2c89e0edc87d..63c1d8a2e19a 100644 --- a/drivers/iio/common/inv_sensors/inv_sensors_timestamp.c +++ b/drivers/iio/common/inv_sensors/inv_sensors_timestamp.c @@ -16,6 +16,9 @@ #define INV_SENSORS_TIMESTAMP_MAX(_val, _jitter) \ (((_val) * (1000 + (_jitter))) / 1000) =20 +/* minimum timestamp delta between 2 interrupts for measuring period (20ms= ) */ +#define INV_SENSORS_MIN_IT_DELTA (20 * NSEC_PER_MSEC) + /* Add a new value inside an accumulator and update the estimate value */ static void inv_update_acc(struct inv_sensors_timestamp_acc *acc, uint32_t= val) { @@ -121,10 +124,13 @@ static uint32_t inv_align_timestamp_it(struct inv_sen= sors_timestamp *ts, void inv_sensors_timestamp_interrupt(struct inv_sensors_timestamp *ts, size_t sample_nb, int64_t timestamp) { + const int64_t delta_threshold =3D + INV_SENSORS_TIMESTAMP_MIN(INV_SENSORS_MIN_IT_DELTA, + ts->chip.jitter); struct inv_sensors_timestamp_interval *it; int64_t delta, interval; uint32_t period; - bool valid =3D false; + bool valid; =20 if (sample_nb =3D=3D 0) return; @@ -136,16 +142,29 @@ void inv_sensors_timestamp_interrupt(struct inv_senso= rs_timestamp *ts, ts->timestamp =3D timestamp - interval; } =20 + /* update delta timestamps and estimated period */ + it =3D &ts->delta; + ts->delta_counter +=3D sample_nb; + delta =3D timestamp - it->up; + if (delta >=3D delta_threshold) { + it->lo =3D it->up; + it->up =3D timestamp; + if (it->lo !=3D 0) { + /* compute period: delta time divided by number of samples */ + delta =3D it->up - it->lo; + period =3D div_s64(delta, sample_nb); + inv_update_chip_period(ts, period); + } + ts->delta_counter =3D 0; + } + /* update interrupt timestamp and compute chip and sensor periods */ it =3D &ts->it; it->lo =3D it->up; it->up =3D timestamp; delta =3D it->up - it->lo; - if (it->lo !=3D 0) { - /* compute period: delta time divided by number of samples */ - period =3D div_s64(delta, sample_nb); - valid =3D inv_update_chip_period(ts, period); - } + period =3D div_s64(delta, sample_nb); + valid =3D inv_validate_period(ts, period); =20 /* if interrupt interval is valid, sync with interrupt timestamp */ ts->period =3D valid ? inv_align_timestamp_it(ts, sample_nb) : diff --git a/include/linux/iio/common/inv_sensors_timestamp.h b/include/lin= ux/iio/common/inv_sensors_timestamp.h index 8d506f1e9df2..bc04831f54c4 100644 --- a/include/linux/iio/common/inv_sensors_timestamp.h +++ b/include/linux/iio/common/inv_sensors_timestamp.h @@ -46,6 +46,8 @@ struct inv_sensors_timestamp_acc { * @min_period: minimal acceptable clock period * @max_period: maximal acceptable clock period * @it: interrupts interval timestamps + * @delta: interval timestamps between several interrupts + * @delta_counter: number of data samples in the delta interval * @timestamp: store last timestamp for computing next data timestamp * @mult: current internal period multiplier * @new_mult: new set internal period multiplier (not yet effective) @@ -57,6 +59,8 @@ struct inv_sensors_timestamp { uint32_t min_period; uint32_t max_period; struct inv_sensors_timestamp_interval it; + struct inv_sensors_timestamp_interval delta; + uint32_t delta_counter; int64_t timestamp; uint32_t mult; uint32_t new_mult; @@ -88,6 +92,8 @@ static inline void inv_sensors_timestamp_reset(struct inv= _sensors_timestamp *ts) const struct inv_sensors_timestamp_interval interval_init =3D {0LL, 0LL}; =20 ts->it =3D interval_init; + ts->delta =3D interval_init; + ts->delta_counter =3D 0; ts->timestamp =3D 0; } =20 --=20 2.54.0