From: Satyam Gupta <sgsatyam@google.com>
To: Tzung-Bi Shih <tzungbi@kernel.org>
Cc: Benson Leung <bleung@chromium.org>,
Guenter Roeck <groeck@chromium.org>,
Gwendal Grignou <gwendal@chromium.org>,
Scott Collyer <scollyer@google.com>,
chrome-platform@lists.linux.dev, linux-kernel@vger.kernel.org,
Satyam Gupta <sgsatyam@google.com>
Subject: [PATCH v6 1/7] platform/chrome: cros_ec_sensorhub: Unwrap sample TS relative to FIFO TS
Date: Thu, 29 Oct 2026 09:28:40 +0000 [thread overview]
Message-ID: <20261029092846.1567475-2-sgsatyam@google.com> (raw)
In-Reply-To: <20261029092846.1567475-1-sgsatyam@google.com>
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@google.com>
---
.../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
next prev parent reply other threads:[~2026-10-29 9:28 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-29 9:28 [PATCH v6 0/7] platform/chrome: cros_ec_sensorhub: Timestamp wrap and drift filter fixes Satyam Gupta
2026-10-29 9:28 ` Satyam Gupta [this message]
2026-10-29 9:28 ` [PATCH v6 2/7] platform/chrome: cros_ec_sensorhub: Reset TS filter on EC/AP time mismatch Satyam Gupta
2026-10-29 9:28 ` [PATCH v6 3/7] platform/chrome: cros_ec_sensorhub: Fix TS filter history shift index Satyam Gupta
2026-10-29 9:28 ` [PATCH v6 4/7] platform/chrome: cros_ec_sensorhub: Clamp clock drift slope to MAX_DRIFT_PPM Satyam Gupta
2026-10-29 9:28 ` [PATCH v6 5/7] platform/chrome: cros_ec_sensorhub: Reset TS filter on ODR or mode changes Satyam Gupta
2026-10-29 9:28 ` [PATCH v6 6/7] platform/chrome: cros_ec_sensorhub: Increase TS_HISTORY_BORED_US to 2s Satyam Gupta
2026-10-29 9:28 ` [PATCH v6 7/7] platform/chrome: cros_ec_sensorhub: Drop sensor samples with past timestamps Satyam Gupta
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261029092846.1567475-2-sgsatyam@google.com \
--to=sgsatyam@google.com \
--cc=bleung@chromium.org \
--cc=chrome-platform@lists.linux.dev \
--cc=groeck@chromium.org \
--cc=gwendal@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=scollyer@google.com \
--cc=tzungbi@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®