* [PATCH 0/2] iio: common: improve InvenSense sample timestamping
@ 2026-07-17 12:39 Jean-Baptiste Maneyrol via B4 Relay
2026-07-17 12:39 ` [PATCH 1/2] iio: invensense: better timestamp alignment when using watermark Jean-Baptiste Maneyrol via B4 Relay
2026-07-17 12:39 ` [PATCH 2/2] iio: invensense: improve period measurement by using a longer delay Jean-Baptiste Maneyrol via B4 Relay
0 siblings, 2 replies; 7+ messages in thread
From: Jean-Baptiste Maneyrol via B4 Relay @ 2026-07-17 12:39 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko
Cc: linux-iio, linux-kernel, Jean-Baptiste Maneyrol
This series improves the InvenSense timestamping mechanism for 2 cases:
when using watermark and for high frequency sampling.
Data arriving by batch because of FIFO watermark will now be all aligned
correctly with the interrupt timestamp and the chip period measurement
will use a minimal period of 20ms to ensure that system latency is
not preventing measurement when using high frequency sampling.
This was tested using inv_icm42600 driver when using maximum FIFO
watermark and when using 1kHz sampling frequency.
Signed-off-by: Jean-Baptiste Maneyrol <jean-baptiste.maneyrol@tdk.com>
---
Jean-Baptiste Maneyrol (2):
iio: invensense: better timestamp alignment when using watermark
iio: invensense: improve period measurement by using a longer delay
.../iio/common/inv_sensors/inv_sensors_timestamp.c | 86 +++++++++++-----------
include/linux/iio/common/inv_sensors_timestamp.h | 6 ++
2 files changed, 51 insertions(+), 41 deletions(-)
---
base-commit: aa58ecc73466d0cb8c418de98e2225490bf600e3
change-id: 20260715-iio-common-inv-sensors-timestamp-rework-acf1a0aa00f4
Best regards,
--
Jean-Baptiste Maneyrol <jean-baptiste.maneyrol@tdk.com>
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 1/2] iio: invensense: better timestamp alignment when using watermark
2026-07-17 12:39 [PATCH 0/2] iio: common: improve InvenSense sample timestamping Jean-Baptiste Maneyrol via B4 Relay
@ 2026-07-17 12:39 ` Jean-Baptiste Maneyrol via B4 Relay
2026-07-17 18:06 ` Andy Shevchenko
2026-07-18 1:14 ` Jonathan Cameron
2026-07-17 12:39 ` [PATCH 2/2] iio: invensense: improve period measurement by using a longer delay Jean-Baptiste Maneyrol via B4 Relay
1 sibling, 2 replies; 7+ messages in thread
From: Jean-Baptiste Maneyrol via B4 Relay @ 2026-07-17 12:39 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko
Cc: linux-iio, linux-kernel, Jean-Baptiste Maneyrol
From: Jean-Baptiste Maneyrol <jean-baptiste.maneyrol@tdk.com>
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 <jean-baptiste.maneyrol@tdk.com>
---
.../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/drivers/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 @@
#include <linux/iio/common/inv_sensors_timestamp.h>
-/* 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 = period / ts->mult;
inv_update_acc(&ts->chip_period, new_chip_period);
- ts->period = ts->mult * ts->chip_period.val;
return true;
}
-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 = (int64_t)ts->min_period * ts->mult;
- const int64_t period_max = (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 = ts->it.lo - ts->timestamp;
-
- /* adjust timestamp while respecting jitter */
- add_max = period_max - (int64_t)ts->period;
- sub_max = period_min - (int64_t)ts->period;
- jitter = INV_SENSORS_TIMESTAMP_JITTER((int64_t)ts->period, ts->chip.jitter);
- if (delta > jitter)
- adjust = add_max;
- else if (delta < -jitter)
- adjust = sub_max;
- else
- adjust = 0;
+ const uint64_t period_min = (uint64_t)ts->min_period * ts->mult;
+ const uint64_t period_max = (uint64_t)ts->max_period * ts->mult;
+ uint32_t new_period;
- ts->timestamp += adjust;
+ /* compute new period aligning last timestamp with interrupt timestamp */
+ new_period = div_s64(ts->it.up - ts->timestamp, sample_nb);
+
+ /* ensure that period never overflows the jitter */
+ return clamp(new_period, period_min, period_max);
}
void inv_sensors_timestamp_interrupt(struct inv_sensors_timestamp *ts,
@@ -143,6 +129,13 @@ void inv_sensors_timestamp_interrupt(struct inv_sensors_timestamp *ts,
if (sample_nb == 0)
return;
+ /* no previous data, compute theoretical value from interrupt */
+ if (ts->timestamp == 0) {
+ /* elapsed time: sensor period * sensor samples number */
+ interval = (int64_t)ts->period * (int64_t)sample_nb;
+ ts->timestamp = timestamp - interval;
+ }
+
/* update interrupt timestamp and compute chip and sensor periods */
it = &ts->it;
it->lo = it->up;
@@ -154,17 +147,9 @@ void inv_sensors_timestamp_interrupt(struct inv_sensors_timestamp *ts,
valid = inv_update_chip_period(ts, period);
}
- /* no previous data, compute theoretical value from interrupt */
- if (ts->timestamp == 0) {
- /* elapsed time: sensor period * sensor samples number */
- interval = (int64_t)ts->period * (int64_t)sample_nb;
- ts->timestamp = it->up - interval;
- return;
- }
-
/* if interrupt interval is valid, sync with interrupt timestamp */
- if (valid)
- inv_align_timestamp_it(ts);
+ ts->period = 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_TIMESTAMP");
--
2.54.0
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH 2/2] iio: invensense: improve period measurement by using a longer delay
2026-07-17 12:39 [PATCH 0/2] iio: common: improve InvenSense sample timestamping Jean-Baptiste Maneyrol via B4 Relay
2026-07-17 12:39 ` [PATCH 1/2] iio: invensense: better timestamp alignment when using watermark Jean-Baptiste Maneyrol via B4 Relay
@ 2026-07-17 12:39 ` Jean-Baptiste Maneyrol via B4 Relay
2026-07-17 18:09 ` Andy Shevchenko
2026-07-18 1:25 ` Jonathan Cameron
1 sibling, 2 replies; 7+ messages in thread
From: Jean-Baptiste Maneyrol via B4 Relay @ 2026-07-17 12:39 UTC (permalink / raw)
To: Jonathan Cameron, David Lechner, Nuno Sá, Andy Shevchenko
Cc: linux-iio, linux-kernel, Jean-Baptiste Maneyrol
From: Jean-Baptiste Maneyrol <jean-baptiste.maneyrol@tdk.com>
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 <jean-baptiste.maneyrol@tdk.com>
---
.../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/drivers/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)
+/* 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_sensors_timestamp *ts,
void inv_sensors_timestamp_interrupt(struct inv_sensors_timestamp *ts,
size_t sample_nb, int64_t timestamp)
{
+ const int64_t delta_threshold =
+ 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 = false;
+ bool valid;
if (sample_nb == 0)
return;
@@ -136,16 +142,29 @@ void inv_sensors_timestamp_interrupt(struct inv_sensors_timestamp *ts,
ts->timestamp = timestamp - interval;
}
+ /* update delta timestamps and estimated period */
+ it = &ts->delta;
+ ts->delta_counter += sample_nb;
+ delta = timestamp - it->up;
+ if (delta >= delta_threshold) {
+ it->lo = it->up;
+ it->up = timestamp;
+ if (it->lo != 0) {
+ /* compute period: delta time divided by number of samples */
+ delta = it->up - it->lo;
+ period = div_s64(delta, sample_nb);
+ inv_update_chip_period(ts, period);
+ }
+ ts->delta_counter = 0;
+ }
+
/* update interrupt timestamp and compute chip and sensor periods */
it = &ts->it;
it->lo = it->up;
it->up = timestamp;
delta = it->up - it->lo;
- if (it->lo != 0) {
- /* compute period: delta time divided by number of samples */
- period = div_s64(delta, sample_nb);
- valid = inv_update_chip_period(ts, period);
- }
+ period = div_s64(delta, sample_nb);
+ valid = inv_validate_period(ts, period);
/* if interrupt interval is valid, sync with interrupt timestamp */
ts->period = valid ? inv_align_timestamp_it(ts, sample_nb) :
diff --git a/include/linux/iio/common/inv_sensors_timestamp.h b/include/linux/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 = {0LL, 0LL};
ts->it = interval_init;
+ ts->delta = interval_init;
+ ts->delta_counter = 0;
ts->timestamp = 0;
}
--
2.54.0
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] iio: invensense: better timestamp alignment when using watermark
2026-07-17 12:39 ` [PATCH 1/2] iio: invensense: better timestamp alignment when using watermark Jean-Baptiste Maneyrol via B4 Relay
@ 2026-07-17 18:06 ` Andy Shevchenko
2026-07-18 1:14 ` Jonathan Cameron
1 sibling, 0 replies; 7+ messages in thread
From: Andy Shevchenko @ 2026-07-17 18:06 UTC (permalink / raw)
To: jean-baptiste.maneyrol
Cc: Jonathan Cameron, David Lechner, Nuno Sá,
Andy Shevchenko, linux-iio, linux-kernel
On Fri, Jul 17, 2026 at 02:39:40PM +0200, Jean-Baptiste Maneyrol via B4 Relay wrote:
> 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.
...
> -/* 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)
Side note: Just noticed "per mille" for which we now have a constant defined
in units.h :-)
...
> -static void inv_align_timestamp_it(struct inv_sensors_timestamp *ts)
> +static uint32_t inv_align_timestamp_it(struct inv_sensors_timestamp *ts,
Please, use kernel types. u32 here. I think we used to have such a discussion
a year or couple of years ago and become to an agreement that at some point
the types should be changed in the driver before accepting any new changes
to it.
> + unsigned int sample_nb)
...
> /* if interrupt interval is valid, sync with interrupt timestamp */
> - if (valid)
Leave this line as is, just add assignment and 'else' branch.
> - inv_align_timestamp_it(ts);
> + ts->period = valid ? inv_align_timestamp_it(ts, sample_nb) :
> + ts->mult * ts->chip_period.val;
> }
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2/2] iio: invensense: improve period measurement by using a longer delay
2026-07-17 12:39 ` [PATCH 2/2] iio: invensense: improve period measurement by using a longer delay Jean-Baptiste Maneyrol via B4 Relay
@ 2026-07-17 18:09 ` Andy Shevchenko
2026-07-18 1:25 ` Jonathan Cameron
1 sibling, 0 replies; 7+ messages in thread
From: Andy Shevchenko @ 2026-07-17 18:09 UTC (permalink / raw)
To: jean-baptiste.maneyrol
Cc: Jonathan Cameron, David Lechner, Nuno Sá,
Andy Shevchenko, linux-iio, linux-kernel
On Fri, Jul 17, 2026 at 02:39:41PM +0200, Jean-Baptiste Maneyrol via B4 Relay wrote:
> 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.
...
> void inv_sensors_timestamp_interrupt(struct inv_sensors_timestamp *ts,
> size_t sample_nb, int64_t timestamp)
> {
> + const int64_t delta_threshold =
> + INV_SENSORS_TIMESTAMP_MIN(INV_SENSORS_MIN_IT_DELTA,
> + ts->chip.jitter);
This is less readable than the split version.
const s64 delta_threshold;
Also, what const gives us here?
> struct inv_sensors_timestamp_interval *it;
> int64_t delta, interval;
> uint32_t period;
> - bool valid = false;
> + bool valid;
delta_threshold =
INV_SENSORS_TIMESTAMP_MIN(INV_SENSORS_MIN_IT_DELTA, ts->chip.jitter);
(yes, I would go for a bit longer line).
> if (sample_nb == 0)
> return;
And why to assign it if we have to return, for example here?
Move the assignment closer to its first user.
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 1/2] iio: invensense: better timestamp alignment when using watermark
2026-07-17 12:39 ` [PATCH 1/2] iio: invensense: better timestamp alignment when using watermark Jean-Baptiste Maneyrol via B4 Relay
2026-07-17 18:06 ` Andy Shevchenko
@ 2026-07-18 1:14 ` Jonathan Cameron
1 sibling, 0 replies; 7+ messages in thread
From: Jonathan Cameron @ 2026-07-18 1:14 UTC (permalink / raw)
To: Jean-Baptiste Maneyrol via B4 Relay
Cc: jean-baptiste.maneyrol, David Lechner, Nuno Sá,
Andy Shevchenko, linux-iio, linux-kernel
On Fri, 17 Jul 2026 14:39:40 +0200
Jean-Baptiste Maneyrol via B4 Relay <devnull+jean-baptiste.maneyrol.tdk.com@kernel.org> wrote:
> From: Jean-Baptiste Maneyrol <jean-baptiste.maneyrol@tdk.com>
>
> 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.
I've read this a few times and I'm not entirely following the meaning.
Please rewrite. Intent might be something close to:
"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.
"
I'm not entirely sure however!
>
> 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 <jean-baptiste.maneyrol@tdk.com>
> ---
> .../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/drivers/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 @@
>
> #include <linux/iio/common/inv_sensors_timestamp.h>
>
> -/* 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 = period / ts->mult;
> inv_update_acc(&ts->chip_period, new_chip_period);
> - ts->period = ts->mult * ts->chip_period.val;
>
> return true;
> }
>
> -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 = (int64_t)ts->min_period * ts->mult;
> - const int64_t period_max = (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 = ts->it.lo - ts->timestamp;
> -
> - /* adjust timestamp while respecting jitter */
> - add_max = period_max - (int64_t)ts->period;
> - sub_max = period_min - (int64_t)ts->period;
> - jitter = INV_SENSORS_TIMESTAMP_JITTER((int64_t)ts->period, ts->chip.jitter);
> - if (delta > jitter)
> - adjust = add_max;
> - else if (delta < -jitter)
> - adjust = sub_max;
> - else
> - adjust = 0;
> + const uint64_t period_min = (uint64_t)ts->min_period * ts->mult;
> + const uint64_t period_max = (uint64_t)ts->max_period * ts->mult;
> + uint32_t new_period;
>
> - ts->timestamp += adjust;
> + /* compute new period aligning last timestamp with interrupt timestamp */
> + new_period = div_s64(ts->it.up - ts->timestamp, sample_nb);
> +
> + /* ensure that period never overflows the jitter */
> + return clamp(new_period, period_min, period_max);
> }
>
> void inv_sensors_timestamp_interrupt(struct inv_sensors_timestamp *ts,
> @@ -143,6 +129,13 @@ void inv_sensors_timestamp_interrupt(struct inv_sensors_timestamp *ts,
> if (sample_nb == 0)
> return;
>
> + /* no previous data, compute theoretical value from interrupt */
> + if (ts->timestamp == 0) {
> + /* elapsed time: sensor period * sensor samples number */
> + interval = (int64_t)ts->period * (int64_t)sample_nb;
> + ts->timestamp = timestamp - interval;
> + }
> +
> /* update interrupt timestamp and compute chip and sensor periods */
> it = &ts->it;
> it->lo = it->up;
> @@ -154,17 +147,9 @@ void inv_sensors_timestamp_interrupt(struct inv_sensors_timestamp *ts,
> valid = inv_update_chip_period(ts, period);
> }
>
> - /* no previous data, compute theoretical value from interrupt */
> - if (ts->timestamp == 0) {
> - /* elapsed time: sensor period * sensor samples number */
> - interval = (int64_t)ts->period * (int64_t)sample_nb;
> - ts->timestamp = it->up - interval;
> - return;
> - }
> -
> /* if interrupt interval is valid, sync with interrupt timestamp */
> - if (valid)
> - inv_align_timestamp_it(ts);
> + ts->period = 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_TIMESTAMP");
>
>
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH 2/2] iio: invensense: improve period measurement by using a longer delay
2026-07-17 12:39 ` [PATCH 2/2] iio: invensense: improve period measurement by using a longer delay Jean-Baptiste Maneyrol via B4 Relay
2026-07-17 18:09 ` Andy Shevchenko
@ 2026-07-18 1:25 ` Jonathan Cameron
1 sibling, 0 replies; 7+ messages in thread
From: Jonathan Cameron @ 2026-07-18 1:25 UTC (permalink / raw)
To: Jean-Baptiste Maneyrol via B4 Relay
Cc: jean-baptiste.maneyrol, David Lechner, Nuno Sá,
Andy Shevchenko, linux-iio, linux-kernel
On Fri, 17 Jul 2026 14:39:41 +0200
Jean-Baptiste Maneyrol via B4 Relay <devnull+jean-baptiste.maneyrol.tdk.com@kernel.org> wrote:
> From: Jean-Baptiste Maneyrol <jean-baptiste.maneyrol@tdk.com>
>
> 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 <jean-baptiste.maneyrol@tdk.com>
https://sashiko.dev/#/patchset/20260717-iio-common-inv-sensors-timestamp-rework-v1-0-d1afee2805cd%40tdk.com
Some comments. To me looks like a mixed bag. Please take a look.
> ---
> .../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/drivers/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)
>
> +/* 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_sensors_timestamp *ts,
> void inv_sensors_timestamp_interrupt(struct inv_sensors_timestamp *ts,
> size_t sample_nb, int64_t timestamp)
> {
> + const int64_t delta_threshold =
> + 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 = false;
> + bool valid;
>
> if (sample_nb == 0)
> return;
> @@ -136,16 +142,29 @@ void inv_sensors_timestamp_interrupt(struct inv_sensors_timestamp *ts,
> ts->timestamp = timestamp - interval;
> }
>
> + /* update delta timestamps and estimated period */
> + it = &ts->delta;
> + ts->delta_counter += sample_nb;
> + delta = timestamp - it->up;
> + if (delta >= delta_threshold) {
> + it->lo = it->up;
> + it->up = timestamp;
> + if (it->lo != 0) {
> + /* compute period: delta time divided by number of samples */
> + delta = it->up - it->lo;
> + period = div_s64(delta, sample_nb);
Sashiko asks if that should be delta_counter on the bottom. Seems plausible
to me.
> + inv_update_chip_period(ts, period);
> + }
> + ts->delta_counter = 0;
> + }
> +
> /* update interrupt timestamp and compute chip and sensor periods */
> it = &ts->it;
> it->lo = it->up;
> it->up = timestamp;
> delta = it->up - it->lo;
> - if (it->lo != 0) {
> - /* compute period: delta time divided by number of samples */
> - period = div_s64(delta, sample_nb);
> - valid = inv_update_chip_period(ts, period);
> - }
> + period = div_s64(delta, sample_nb);
> + valid = inv_validate_period(ts, period);
>
> /* if interrupt interval is valid, sync with interrupt timestamp */
> ts->period = valid ? inv_align_timestamp_it(ts, sample_nb) :
> diff --git a/include/linux/iio/common/inv_sensors_timestamp.h b/include/linux/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 = {0LL, 0LL};
>
> ts->it = interval_init;
> + ts->delta = interval_init;
> + ts->delta_counter = 0;
> ts->timestamp = 0;
> }
>
>
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2026-07-18 1:25 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-17 12:39 [PATCH 0/2] iio: common: improve InvenSense sample timestamping Jean-Baptiste Maneyrol via B4 Relay
2026-07-17 12:39 ` [PATCH 1/2] iio: invensense: better timestamp alignment when using watermark Jean-Baptiste Maneyrol via B4 Relay
2026-07-17 18:06 ` Andy Shevchenko
2026-07-18 1:14 ` Jonathan Cameron
2026-07-17 12:39 ` [PATCH 2/2] iio: invensense: improve period measurement by using a longer delay Jean-Baptiste Maneyrol via B4 Relay
2026-07-17 18:09 ` Andy Shevchenko
2026-07-18 1:25 ` Jonathan Cameron
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox