From nobody Sat Jul 25 02:47:36 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 914293D810D; Mon, 20 Jul 2026 10:38:58 +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=1784543938; cv=none; b=ke7zCHkGmu0+enqYGC6sudf4Khx7uF2/uSYkkdaNv1qxJ9+IFmYDtq2rJ9hv/c/1jy7WGw8hMvigFXEegrZuJjDK1gyzRSzpIeDTaVUHkUzxT8NHUIt4llUa9zNv/tAXLje+ag/Bdqm7F2ErenHHCsNcwqMNPVAztiJfRMDzKbg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784543938; c=relaxed/simple; bh=924OMq8kYlXr9KheiW/qgB0/Gvywmx/QBwEVOAd2OAw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=ZewGqFwqrW3LBk2HSiPh0Khk/yPF0o3J1DMOzVThFDedATTcFbsfiLgDOsq0hbc8ge8krzOC1P4ps1bxBZ0jlv2mhpA4oqz9eCxdmuwi2eWNWRdbM3hSgKBW3i9dpN2DaAF5TucEtwhQKeN8xui6SqZjY6j3sSyc6EWhDW3q29o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AOSRRO7q; 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="AOSRRO7q" Received: by smtp.kernel.org (Postfix) with ESMTPS id 49F90C2BCFA; Mon, 20 Jul 2026 10:38:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1784543938; bh=924OMq8kYlXr9KheiW/qgB0/Gvywmx/QBwEVOAd2OAw=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=AOSRRO7q0p40z2iUAmhK4wKeYovFLlCJFKUUKq55g/XlZ2eTkpae56cOfkknaP7/o CescOXc4f2Naqtz+0AVDlH+s1s93DzUvYtBOwCREKBJIAbC1fTQejfz/zOiOwK2pSY 2s+lLJcc+gLArjrm9ozSS6dnv/rXw/jeqnowtaZz0e2T5u+Y15HIRiQnSsNSPfmAX8 72AGpJA5PJo4vY1wEo1UJAP7sgFeT4ogF3WIg/5VachmE9n81wbB8fHwsiS/+p+Sp/ 5QPvT5j5gt8eWOy+aMCFnUkbyKL0T8Kmbb7HtsUB6pAjrJ/xJMx7sE5VDyREI/obcJ jELzk0gf8hwvg== 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 2A647C44515; Mon, 20 Jul 2026 10:38:58 +0000 (UTC) From: Jean-Baptiste Maneyrol via B4 Relay Date: Mon, 20 Jul 2026 12:38:32 +0200 Subject: [PATCH v2 1/3] iio: inv_sensors: convert to kernel types 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: <20260720-iio-common-inv-sensors-timestamp-rework-v2-1-aec31446809c@tdk.com> References: <20260720-iio-common-inv-sensors-timestamp-rework-v2-0-aec31446809c@tdk.com> In-Reply-To: <20260720-iio-common-inv-sensors-timestamp-rework-v2-0-aec31446809c@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=1784543936; l=7732; i=jean-baptiste.maneyrol@tdk.com; s=20240923; h=from:subject:message-id; bh=49yZuvcafugGR1ZfjYa5lzm+gj3Ua3pQf2fJ6haRP4k=; b=l/kAI2mzm47RR9ZxD9Hf26mxop0Xi0ZM9e2LSBNJTzdd1m2N73ENU3J1kjQElt1sU5EiKzLfD U1R5sLLDKQTB5Hfg4lCCeGy6hvwYx+J/p0RFhhK5SYgItPs9Rhx6xVQ 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 Convert standard types (u)intXX_t to kernel type u/sXX. Signed-off-by: Jean-Baptiste Maneyrol Reviewed-by: Andy Shevchenko Suggested-by: Andy Shevchenko --- .../iio/common/inv_sensors/inv_sensors_timestamp.c | 48 +++++++++++-------= ---- include/linux/iio/common/inv_sensors_timestamp.h | 34 +++++++-------- 2 files changed, 41 insertions(+), 41 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..ecfd54ffe7e9 100644 --- a/drivers/iio/common/inv_sensors/inv_sensors_timestamp.c +++ b/drivers/iio/common/inv_sensors/inv_sensors_timestamp.c @@ -19,9 +19,9 @@ (((_val) * (1000 + (_jitter))) / 1000) =20 /* 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) +static void inv_update_acc(struct inv_sensors_timestamp_acc *acc, u32 val) { - uint64_t sum =3D 0; + u64 sum =3D 0; size_t i; =20 acc->values[acc->idx++] =3D val; @@ -58,9 +58,9 @@ void inv_sensors_timestamp_init(struct inv_sensors_timest= amp *ts, EXPORT_SYMBOL_NS_GPL(inv_sensors_timestamp_init, "IIO_INV_SENSORS_TIMESTAM= P"); =20 int inv_sensors_timestamp_update_odr(struct inv_sensors_timestamp *ts, - uint32_t period, bool fifo) + u32 period, bool fifo) { - uint32_t mult; + u32 mult; =20 /* when FIFO is on, prevent odr change if one is already pending */ if (fifo && ts->new_mult !=3D 0) @@ -78,9 +78,9 @@ int inv_sensors_timestamp_update_odr(struct inv_sensors_t= imestamp *ts, } EXPORT_SYMBOL_NS_GPL(inv_sensors_timestamp_update_odr, "IIO_INV_SENSORS_TI= MESTAMP"); =20 -static bool inv_validate_period(struct inv_sensors_timestamp *ts, uint32_t= period) +static bool inv_validate_period(struct inv_sensors_timestamp *ts, u32 peri= od) { - uint32_t period_min, period_max; + u32 period_min, period_max; =20 /* check that period is acceptable */ period_min =3D ts->min_period * ts->mult; @@ -92,9 +92,9 @@ static bool inv_validate_period(struct inv_sensors_timest= amp *ts, uint32_t perio } =20 static bool inv_update_chip_period(struct inv_sensors_timestamp *ts, - uint32_t period) + u32 period) { - uint32_t new_chip_period; + u32 new_chip_period; =20 if (!inv_validate_period(ts, period)) return false; @@ -109,19 +109,19 @@ static bool inv_update_chip_period(struct inv_sensors= _timestamp *ts, =20 static void inv_align_timestamp_it(struct inv_sensors_timestamp *ts) { - 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; + const s64 period_min =3D (s64)ts->min_period * ts->mult; + const s64 period_max =3D (s64)ts->max_period * ts->mult; + s64 add_max, sub_max; + s64 delta, jitter; + s64 adjust; =20 /* delta time between last sample and last interrupt */ delta =3D ts->it.lo - ts->timestamp; =20 /* 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); + add_max =3D period_max - (s64)ts->period; + sub_max =3D period_min - (s64)ts->period; + jitter =3D INV_SENSORS_TIMESTAMP_JITTER((s64)ts->period, ts->chip.jitter); if (delta > jitter) adjust =3D add_max; else if (delta < -jitter) @@ -133,11 +133,11 @@ static void inv_align_timestamp_it(struct inv_sensors= _timestamp *ts) } =20 void inv_sensors_timestamp_interrupt(struct inv_sensors_timestamp *ts, - size_t sample_nb, int64_t timestamp) + size_t sample_nb, s64 timestamp) { struct inv_sensors_timestamp_interval *it; - int64_t delta, interval; - uint32_t period; + s64 delta, interval; + u32 period; bool valid =3D false; =20 if (sample_nb =3D=3D 0) @@ -157,7 +157,7 @@ void inv_sensors_timestamp_interrupt(struct inv_sensors= _timestamp *ts, /* 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; + interval =3D (s64)ts->period * (s64)sample_nb; ts->timestamp =3D it->up - interval; return; } @@ -169,11 +169,11 @@ void inv_sensors_timestamp_interrupt(struct inv_senso= rs_timestamp *ts, EXPORT_SYMBOL_NS_GPL(inv_sensors_timestamp_interrupt, "IIO_INV_SENSORS_TIM= ESTAMP"); =20 void inv_sensors_timestamp_apply_odr(struct inv_sensors_timestamp *ts, - uint32_t fifo_period, size_t fifo_nb, + u32 fifo_period, size_t fifo_nb, unsigned int fifo_no) { - int64_t interval; - uint32_t fifo_mult; + s64 interval; + u32 fifo_mult; =20 if (ts->new_mult =3D=3D 0) return; @@ -194,7 +194,7 @@ void inv_sensors_timestamp_apply_odr(struct inv_sensors= _timestamp *ts, fifo_mult =3D fifo_period / ts->chip.clock_period; fifo_period =3D fifo_mult * ts->chip_period.val; /* computes time interval between interrupt and this sample */ - interval =3D (int64_t)(fifo_nb - fifo_no) * (int64_t)fifo_period; + interval =3D (s64)(fifo_nb - fifo_no) * (s64)fifo_period; ts->timestamp =3D ts->it.up - interval; } } diff --git a/include/linux/iio/common/inv_sensors_timestamp.h b/include/lin= ux/iio/common/inv_sensors_timestamp.h index 8d506f1e9df2..e4e720e6f4b7 100644 --- a/include/linux/iio/common/inv_sensors_timestamp.h +++ b/include/linux/iio/common/inv_sensors_timestamp.h @@ -13,9 +13,9 @@ * @init_period: chip initial period at reset in ns */ struct inv_sensors_timestamp_chip { - uint32_t clock_period; - uint32_t jitter; - uint32_t init_period; + u32 clock_period; + u32 jitter; + u32 init_period; }; =20 /** @@ -24,8 +24,8 @@ struct inv_sensors_timestamp_chip { * @up: interval upper bound */ struct inv_sensors_timestamp_interval { - int64_t lo; - int64_t up; + s64 lo; + s64 up; }; =20 /** @@ -35,9 +35,9 @@ struct inv_sensors_timestamp_interval { * @values: table of all measured values, use for computing the mean */ struct inv_sensors_timestamp_acc { - uint32_t val; + u32 val; size_t idx; - uint32_t values[32]; + u32 values[32]; }; =20 /** @@ -54,13 +54,13 @@ struct inv_sensors_timestamp_acc { */ struct inv_sensors_timestamp { struct inv_sensors_timestamp_chip chip; - uint32_t min_period; - uint32_t max_period; + u32 min_period; + u32 max_period; struct inv_sensors_timestamp_interval it; - int64_t timestamp; - uint32_t mult; - uint32_t new_mult; - uint32_t period; + s64 timestamp; + u32 mult; + u32 new_mult; + u32 period; struct inv_sensors_timestamp_acc chip_period; }; =20 @@ -68,19 +68,19 @@ void inv_sensors_timestamp_init(struct inv_sensors_time= stamp *ts, const struct inv_sensors_timestamp_chip *chip); =20 int inv_sensors_timestamp_update_odr(struct inv_sensors_timestamp *ts, - uint32_t period, bool fifo); + u32 period, bool fifo); =20 void inv_sensors_timestamp_interrupt(struct inv_sensors_timestamp *ts, - size_t sample_nb, int64_t timestamp); + size_t sample_nb, s64 timestamp); =20 -static inline int64_t inv_sensors_timestamp_pop(struct inv_sensors_timesta= mp *ts) +static inline s64 inv_sensors_timestamp_pop(struct inv_sensors_timestamp *= ts) { ts->timestamp +=3D ts->period; return ts->timestamp; } =20 void inv_sensors_timestamp_apply_odr(struct inv_sensors_timestamp *ts, - uint32_t fifo_period, size_t fifo_nb, + u32 fifo_period, size_t fifo_nb, unsigned int fifo_no); =20 static inline void inv_sensors_timestamp_reset(struct inv_sensors_timestam= p *ts) --=20 2.54.0 From nobody Sat Jul 25 02:47:36 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 912403D75AD; Mon, 20 Jul 2026 10:38:58 +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=1784543938; cv=none; b=E9gSqDYI2t41F/po0Zjm6Ng977Zg+jEvkbdhHCcYLlvg1Uaea51NfVWCDQfYoCyWfKBL9lE5NpEZdgeq8BrdyD1Y//73mpaYjV3KEaq19OAspKycanma4PTRx/PVOd6TjcgdvLsCbdapoR3H+kDWkakhn7u+fqSYYh26Zlu4Q+Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784543938; c=relaxed/simple; bh=jX3mT5ogBRPc8ytC7Ugls8/3fSIOYLzRplELLtwtuQU=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=NKTQzyVFqISEqibyd5d13zdmacmIqEQomVkASEoK2HO3Ckp31FVYjM9a2pt0KFJWEQfakOiLutFgaR/QPahoSdCX8IF0lkrIpEvCBFyGaZC4sKEk8OeUkQroHG5sJYQ9+VwFMcx2GxR1F4V9cNdbDuExc8lqz6WEVY3aEeuF3Ao= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=XJhC3sCY; 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="XJhC3sCY" Received: by smtp.kernel.org (Postfix) with ESMTPS id 52045C4AF09; Mon, 20 Jul 2026 10:38:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1784543938; bh=jX3mT5ogBRPc8ytC7Ugls8/3fSIOYLzRplELLtwtuQU=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=XJhC3sCYzmOwmXEUzB28FTHHOOorHJjUI06Udk7N3ydw0VteKFkqRJO547oMhJOma VZZLehDLJq9ZspQQZqPce3ANXyG7uq91ozfSYuu+o8e5YCcHg5Ek8g509yR82HXwi5 R1s2K/JYoNuMsnWPLlQU/MgGGmUXYKbBunKk4jeqaVGfoczMyta3oi1PmniynXAMnr 7PNkJROMlLyO2OERqM863TQMvuZwZSPZZHLAjbMU/QMGnivWLRS3J/Cul/FnfqlhIe OFUw8kjX+rhh/r63oDtmoC3Sze88++ln5uDOflNg5KxeUN5MddKC2yXNQioq3E2ekU KsyELzlKF61FQ== 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 396E2C44527; Mon, 20 Jul 2026 10:38:58 +0000 (UTC) From: Jean-Baptiste Maneyrol via B4 Relay Date: Mon, 20 Jul 2026 12:38:33 +0200 Subject: [PATCH v2 2/3] iio: inv_sensors: 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: <20260720-iio-common-inv-sensors-timestamp-rework-v2-2-aec31446809c@tdk.com> References: <20260720-iio-common-inv-sensors-timestamp-rework-v2-0-aec31446809c@tdk.com> In-Reply-To: <20260720-iio-common-inv-sensors-timestamp-rework-v2-0-aec31446809c@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=1784543936; l=4065; i=jean-baptiste.maneyrol@tdk.com; s=20240923; h=from:subject:message-id; bh=5Y3d1x9lJj9xiAU5X4QM0jQxvh5zbPbgxHv2JS+TVXc=; b=HUoSLqp/O1HoKzpVGswc35/6WB9q8Ym55ZYuzOu48jcf6aM2SUx1QlPsedljAV5USwMcVlU5E pbMZgv3+h5SDYEBtTxGVLxr/+UU1kNorwUJNkJc06kcsC8YPumCFTEJ 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 changes the final timestamp. When the watermark is in use, we have a batch of samples for each interrupt. The current code doesn't manage to align the timestamp because the jitter is too high. Instead modify the estimated inter interrupt period and use that to adjust the timestamp alignment over the batch in a linear fashion. Signed-off-by: Jean-Baptiste Maneyrol --- .../iio/common/inv_sensors/inv_sensors_timestamp.c | 51 ++++++++----------= ---- 1 file changed, 19 insertions(+), 32 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 ecfd54ffe7e9..2aaaa8df6d03 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 u32 inv_align_timestamp_it(struct inv_sensors_timestamp *ts, + unsigned int sample_nb) { const s64 period_min =3D (s64)ts->min_period * ts->mult; const s64 period_max =3D (s64)ts->max_period * ts->mult; - s64 add_max, sub_max; - s64 delta, jitter; - s64 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 - (s64)ts->period; - sub_max =3D period_min - (s64)ts->period; - jitter =3D INV_SENSORS_TIMESTAMP_JITTER((s64)ts->period, ts->chip.jitter); - if (delta > jitter) - adjust =3D add_max; - else if (delta < -jitter) - adjust =3D sub_max; - else - adjust =3D 0; + s64 new_period; + + /* compute new period aligning last timestamp with interrupt timestamp */ + new_period =3D div_s64(ts->it.up - ts->timestamp, sample_nb); =20 - ts->timestamp +=3D adjust; + /* 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 (s64)ts->period * (s64)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,11 @@ void inv_sensors_timestamp_interrupt(struct inv_senso= rs_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 (s64)ts->period * (s64)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 inv_align_timestamp_it(ts, sample_nb); + else + ts->period =3D 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 02:47:36 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 9138A3D7D61; Mon, 20 Jul 2026 10:38:58 +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=1784543938; cv=none; b=Cag16CuWoKRf5eh2JwsdXrnxD0yvHpuEnZiDrl2ylIh8fgkODd8wDUVN2sX7HWiKXCxvOjKkjM6ZnCSpCVIOpKwp5CeRWUO9pBRbQBcn3wzmI7a2cSHbMcrS63H5b1i8GO68ibHcLNoee8yQ7CFhMc34ztFOonoPbl5o7BhNQiw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1784543938; c=relaxed/simple; bh=EFcWiaBHxwv7Q9BfJkEhkZi4ZG+Q2Uy//bO7IpjXn9s=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Swqs35hABNmkn8Gc8qiDDvR2o3S21AuSfI9surjx8joolnUaysqVxDuuCLR6htly6AR3ia7KNIIb6NbQfVAaL9kXuCPxTFHfO7IKVsAOp0BCKESvxmHVlmgBym6+kLIWptCMMb145G1HtXdePKj/sw2+xJnMR5UAd4EFDBCYwIU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=frwOvqFe; 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="frwOvqFe" Received: by smtp.kernel.org (Postfix) with ESMTPS id 5E8C5C2BCF7; Mon, 20 Jul 2026 10:38:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1784543938; bh=EFcWiaBHxwv7Q9BfJkEhkZi4ZG+Q2Uy//bO7IpjXn9s=; h=From:Date:Subject:References:In-Reply-To:To:Cc:Reply-To:From; b=frwOvqFeCAD2rLu76z5xVOjJOMZWWnast2OjfV5NbKu6gKsfjsBNtFN1jMSMGXLMv JgEENtntaUP3dmoWNgO5wYAsbC/WGYFcIhFwGf3XjKDyenTLeQC3Y/t4dIW4W4kPc1 oRD8zO2v2oTwqKzqe3Rf85AuE42RQ/ZLFD7SvaT2ZQSdL1um37kgLCNl1kAGngXiaM f43/aQX9VQg1VS6Mc7xgeFsi+0I9+DCRJ/TbC/FUw6gF6C9qbW5/CAZie+rn79MS4A HPiMt1esF8wCHrqC/A92DbLcMqquOG0n0k1fzqR5WYyl36duGpd4QiPKU/RKAN6Upb zL0zReENenvWA== 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 469F8C4452B; Mon, 20 Jul 2026 10:38:58 +0000 (UTC) From: Jean-Baptiste Maneyrol via B4 Relay Date: Mon, 20 Jul 2026 12:38:34 +0200 Subject: [PATCH v2 3/3] iio: inv_sensors: 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: <20260720-iio-common-inv-sensors-timestamp-rework-v2-3-aec31446809c@tdk.com> References: <20260720-iio-common-inv-sensors-timestamp-rework-v2-0-aec31446809c@tdk.com> In-Reply-To: <20260720-iio-common-inv-sensors-timestamp-rework-v2-0-aec31446809c@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=1784543936; l=4309; i=jean-baptiste.maneyrol@tdk.com; s=20240923; h=from:subject:message-id; bh=XgtZLh2KI5FSAM9NrJBryiRhF0RxEixZYqvMUesVYz4=; b=yqal+NPcGcWDQLPsp6oAzWkTPN6HOImaAvl+0jLlAWKsGG9+Vk4itLV34VeazKABngTi1znw3 eBzbLcB94UiCK22mTb2pt1pac7S01QaI8gF+uG0h9mYGak2eEQETXr9 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 | 26 ++++++++++++++++++= +--- include/linux/iio/common/inv_sensors_timestamp.h | 6 +++++ 2 files changed, 29 insertions(+), 3 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 2aaaa8df6d03..88a82d1370c5 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, u32 val) { @@ -122,7 +125,7 @@ void inv_sensors_timestamp_interrupt(struct inv_sensors= _timestamp *ts, size_t sample_nb, s64 timestamp) { struct inv_sensors_timestamp_interval *it; - s64 delta, interval; + s64 delta, delta_threshold, interval; u32 period; bool valid =3D false; =20 @@ -136,15 +139,32 @@ 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; + delta_threshold =3D INV_SENSORS_TIMESTAMP_MIN(INV_SENSORS_MIN_IT_DELTA, t= s->chip.jitter); + 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, ts->delta_counter); + 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 */ + /* compute period and check validity */ period =3D div_s64(delta, sample_nb); - valid =3D inv_update_chip_period(ts, period); + valid =3D inv_validate_period(ts, period); } =20 /* if interrupt interval is valid, sync with interrupt timestamp */ diff --git a/include/linux/iio/common/inv_sensors_timestamp.h b/include/lin= ux/iio/common/inv_sensors_timestamp.h index e4e720e6f4b7..4f08204ede3b 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 { u32 min_period; u32 max_period; struct inv_sensors_timestamp_interval it; + struct inv_sensors_timestamp_interval delta; + u32 delta_counter; s64 timestamp; u32 mult; u32 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