[PATCH v5 3/6] platform/chrome: cros_ec_sensorhub: Clamp clock drift slope to MAX_DRIFT_PPM

From: Satyam Gupta

Date: Wed Oct 14 2026 - 03:44:11 EST


From: Scott Collyer <scollyer@xxxxxxxxxx>

Temporary interrupt jitter spikes during sensor batching can produce
extreme instantaneous drift slopes (exceeding 20,000 Parts Per Million
(PPM)) that corrupt the timestamp median filter history and cause Out Of
Order (OOO) sensor events.

Clamp the calculated drift slope m within +/- MAX_DRIFT_PPM (5000 PPM)
so the filter can track expected clock frequency drift across both
Integrated Sensor Hub (ISH) and standalone Embedded Controller (EC)
configurations (which can drift by ~2,500 PPM when running on an
independent crystal/PLL), while rejecting large interrupt jitter spikes.

Signed-off-by: Scott Collyer <scollyer@xxxxxxxxxx>
Signed-off-by: Satyam Gupta <sgsatyam@xxxxxxxxxx>
---
drivers/platform/chrome/cros_ec_sensorhub_ring.c | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/drivers/platform/chrome/cros_ec_sensorhub_ring.c b/drivers/platform/chrome/cros_ec_sensorhub_ring.c
index 6975592c6b05..ea70847c8792 100644
--- a/drivers/platform/chrome/cros_ec_sensorhub_ring.c
+++ b/drivers/platform/chrome/cros_ec_sensorhub_ring.c
@@ -22,6 +22,8 @@

/* Precision of fixed point for the m values from the filter */
#define M_PRECISION BIT(23)
+#define MAX_DRIFT_PPM 5000LL
+#define MAX_M ((s64)(MAX_DRIFT_PPM * M_PRECISION / 1000000))

/* Only activate the filter once we have at least this many elements. */
#define TS_HISTORY_THRESHOLD 8
@@ -271,6 +273,12 @@ cros_ec_sensor_ring_ts_filter_update(struct cros_ec_sensors_ts_filter_state
return; /* we already have this irq in the history */
dy = (state->y_history[0] + state->y_offset) - y;
m = div64_s64(dy * M_PRECISION, dx);
+ /*
+ * Clamp the calculated slope to a realistic range (+/- 5000 ppm).
+ * Temporary large jitter spikes can produce huge slopes (e.g. +/- 20000 ppm)
+ * that would otherwise corrupt the median filter history.
+ */
+ m = clamp(m, -MAX_M, MAX_M);

/* Empty filter if we haven't seen any action in a while. */
if (-dx > TS_HISTORY_BORED_US)
--
2.56.0.385.gd3acb90ef8-goog