[PATCH v6 1/7] platform/chrome: cros_ec_sensorhub: Unwrap sample TS relative to FIFO TS

From: Satyam Gupta

Date: Thu Oct 29 2026 - 05:29:13 EST


The EC reports the sample timestamp (a) and the FIFO interrupt timestamp
(b) as 32-bit microsecond counters, which wrap every 71.6 minutes. They
are unwrapped by cros_ec_sensor_ring_fix_overflow() with separate states,
assuming that two consecutive values are less than half a period apart.

After a longer gap without events, for instance across suspend, that
assumption no longer holds, and as a and b are not taken at the same
time, one of them may be unwrapped to the next period while the other is
not. The AP timestamp of a sample is computed from b - a, so the sample
timestamps are then off by a whole period, about 71.6 minutes, until
another gap happens to realign them.

Derive a from the unwrapped b and the raw difference b - a instead, so
that a and b always fall in the same period. A sample can be taken long
before its FIFO interrupt, when it stays in the FIFO during a suspend,
but only slightly after it, when it reaches the FIFO before the FIFO is
read. So treat b - a as unsigned, offset by FIFO_TS_SLACK_US for the
latter case: a is then unwrapped correctly for samples up to almost a
whole period older than b.

Signed-off-by: Satyam Gupta <sgsatyam@xxxxxxxxxx>
---
.../platform/chrome/cros_ec_sensorhub_ring.c | 21 ++++++++++++++++---
.../linux/platform_data/cros_ec_sensorhub.h | 2 --
2 files changed, 18 insertions(+), 5 deletions(-)

diff --git a/drivers/platform/chrome/cros_ec_sensorhub_ring.c b/drivers/platform/chrome/cros_ec_sensorhub_ring.c
index d92b602..7eb8841 100644
--- a/drivers/platform/chrome/cros_ec_sensorhub_ring.c
+++ b/drivers/platform/chrome/cros_ec_sensorhub_ring.c
@@ -35,6 +35,13 @@
/* To measure by how much the filter is overshooting, if it happens. */
#define FUTURE_TS_ANALYTICS_COUNT_MAX 100

+/*
+ * A sample can reach the FIFO after the FIFO interrupt and before the FIFO
+ * is read, so its timestamp (a) can be slightly later than the interrupt
+ * one (b). This is how much later it is allowed to be.
+ */
+#define FIFO_TS_SLACK_US (1 * USEC_PER_SEC)
+
static inline int
cros_sensorhub_send_sample(struct cros_ec_sensorhub *sensorhub,
struct cros_ec_sensors_ring_sample *sample)
@@ -441,14 +448,22 @@ cros_ec_sensor_ring_process_event(struct cros_ec_sensorhub *sensorhub,
(MOTIONSENSE_SENSOR_FLAG_ODR | MOTIONSENSE_SENSOR_FLAG_FLUSH);

if (in->flags & MOTIONSENSE_SENSOR_FLAG_TIMESTAMP && !async_flags) {
- s64 a = in->timestamp;
s64 b = fifo_info->timestamp;
s64 c = fifo_timestamp;
+ s64 a;

- cros_ec_sensor_ring_fix_overflow(&a, 1LL << 32,
- &sensorhub->overflow_a);
cros_ec_sensor_ring_fix_overflow(&b, 1LL << 32,
&sensorhub->overflow_b);
+ /*
+ * Derive a from b rather than unwrapping it on its own, so
+ * that both always fall in the same overflow period. a is at
+ * most FIFO_TS_SLACK_US later than b, but can be up to almost
+ * a whole period earlier, when the sample stayed in the FIFO
+ * while the FIFO interrupt was disabled, e.g. in suspend.
+ */
+ a = b + FIFO_TS_SLACK_US -
+ (u32)(fifo_info->timestamp + FIFO_TS_SLACK_US -
+ in->timestamp);

if (sensorhub->tight_timestamps) {
cros_ec_sensor_ring_ts_filter_update(
diff --git a/include/linux/platform_data/cros_ec_sensorhub.h b/include/linux/platform_data/cros_ec_sensorhub.h
index 0ecce6a..96eaad2 100644
--- a/include/linux/platform_data/cros_ec_sensorhub.h
+++ b/include/linux/platform_data/cros_ec_sensorhub.h
@@ -127,7 +127,6 @@ struct cros_ec_sensors_ts_batch_state {
* @fifo_info: Copy of FIFO information coming from the EC.
* @fifo_size: Size of the ring.
* @batch_state: Per sensor information of the last batches received.
- * @overflow_a: For handling timestamp overflow for a time (sensor events)
* @overflow_b: For handling timestamp overflow for b time (ec interrupts)
* @filter: Medium fileter structure.
* @tight_timestamps: Set to truen when EC support tight timestamping:
@@ -164,7 +163,6 @@ struct cros_ec_sensorhub {

struct cros_ec_sensors_ts_batch_state *batch_state;

- struct cros_ec_sensors_ec_overflow_state overflow_a;
struct cros_ec_sensors_ec_overflow_state overflow_b;

struct cros_ec_sensors_ts_filter_state filter;
--
2.56.0.307.g5fe7bd680c-goog