[PATCH 3/6] media: qcom: camss: csid-340: Report the hardware SOF timestamp
From: Loic Poulain
Date: Mon Nov 23 2026 - 12:17:11 EST
The CSID latches the global timestamp counter at start-of-frame into a
per-interface register pair. That counter is the ARM architected
counter, so correlating it with CLOCK_MONOTONIC through
ktime_get_snapshot_id() turns the latched value into the time the frame
actually started, rather than the time the interrupt handler ran.
Measured on QCM2290 that removes around 36 us of delay, with ~8 us of
jitter, from the timestamp userspace sees on a 60 fps stream.
TIMESTAMP_EN is not at the same bit in the PIX and the RDI layouts of
CSID_CFG0, so replace the single define with one for each.
Signed-off-by: Loic Poulain <loic.poulain@xxxxxxxxxxxxxxxx>
---
drivers/media/platform/qcom/camss/camss-csid-340.c | 37 +++++++++++++--
drivers/media/platform/qcom/camss/camss-csid.c | 54 ++++++++++++++++++++++
drivers/media/platform/qcom/camss/camss-csid.h | 11 +++++
3 files changed, 97 insertions(+), 5 deletions(-)
diff --git a/drivers/media/platform/qcom/camss/camss-csid-340.c b/drivers/media/platform/qcom/camss/camss-csid-340.c
index 5eef5f8846fbfec5afa1152f73a098fecd717fdf..ff21cd10a7e4373952938ab2e00af64e3014b509 100644
--- a/drivers/media/platform/qcom/camss/camss-csid-340.c
+++ b/drivers/media/platform/qcom/camss/camss-csid-340.c
@@ -49,7 +49,8 @@
#define CSID_CFG0(iface) (0x200 + 0x100 * (iface))
#define CSID_CFG0_BYTE_CNTR_EN BIT(0)
-#define CSID_CFG0_TIMESTAMP_EN BIT(1)
+#define CSID_CFG0_PIX_TIMESTAMP_EN BIT(1)
+#define CSID_CFG0_RDI_TIMESTAMP_EN BIT(2)
#define CSID_CFG0_DECODE_FORMAT_MASK GENMASK(15, 12)
#define CSID_CFG0_DECODE_FORMAT_NOP CSID_CFG0_DECODE_FORMAT_MASK
#define CSID_CFG0_DT_MASK GENMASK(21, 16)
@@ -61,6 +62,13 @@
#define CSID_CTRL_HALT_AT_FRAME_BOUNDARY 0
#define CSID_CTRL_RESUME_AT_FRAME_BOUNDARY 1
+/*
+ * SOF timestamp of the most recent frame, a pair of registers holding a 56-bit
+ * counter value, the low 32 bits in the first and the high 24 in the second.
+ */
+#define CSID_TIMESTAMP_CURR0_SOF(iface) (0x290 + 0x100 * (iface))
+#define CSID_TIMESTAMP_CURR1_SOF(iface) (0x294 + 0x100 * (iface))
+
enum csid_iface {
CSID_IFACE_PIX,
CSID_IFACE_RDI0,
@@ -108,6 +116,7 @@ static void __csid_configure_stream(struct csid_device *csid, u8 enable, u8 port
input_format->code);
enum csid_iface iface = csid_port_iface_map[port];
+ u32 timestamp_en;
u8 dt_id;
u32 val;
@@ -125,17 +134,21 @@ static void __csid_configure_stream(struct csid_device *csid, u8 enable, u8 port
*/
dt_id = port & 0x03;
- if (iface == CSID_IFACE_PIX)
+ if (iface == CSID_IFACE_PIX) {
val = FIELD_PREP(CSID_CFG0_DECODE_FORMAT_MASK, format->decode_format);
- else /* RDI is raw, no decoding */
+ timestamp_en = CSID_CFG0_PIX_TIMESTAMP_EN;
+ } else {
+ /* RDI is raw, no decoding */
val = CSID_CFG0_DECODE_FORMAT_NOP;
+ timestamp_en = CSID_CFG0_RDI_TIMESTAMP_EN;
+ }
val |= FIELD_PREP(CSID_CFG0_DT_MASK, format->data_type);
val |= FIELD_PREP(CSID_CFG0_VC_MASK, vc);
val |= FIELD_PREP(CSID_CFG0_DTID_MASK, dt_id);
if (enable)
- val |= CSID_CFG0_ENABLE;
+ val |= CSID_CFG0_ENABLE | timestamp_en;
dev_dbg(csid->camss->dev, "CSID%u: Stream %s (dt:0x%x df=0x%x port=%u vc=%u)\n",
csid->id, enable ? "enable" : "disable", format->data_type,
@@ -184,6 +197,20 @@ static int csid_reset(struct csid_device *csid)
return 0;
}
+static u64 __csid_sof_timestamp(struct csid_device *csid, enum csid_iface iface)
+{
+ u32 lo, hi;
+
+ /*
+ * The pair is latched on SOF rather than counting, so it can be read
+ * one word at a time without tearing.
+ */
+ lo = readl_relaxed(csid->base + CSID_TIMESTAMP_CURR0_SOF(iface));
+ hi = readl_relaxed(csid->base + CSID_TIMESTAMP_CURR1_SOF(iface));
+
+ return csid_timestamp_to_monotonic(csid, ((u64)hi << 32) | lo);
+}
+
static irqreturn_t csid_isr(int irq, void *dev)
{
struct csid_device *csid = dev;
@@ -207,7 +234,7 @@ static irqreturn_t csid_isr(int irq, void *dev)
iface_val |= status;
if (status & CSID_IFACE_IRQ_INFO_INPUT_SOF)
- csid_sof_event(csid, i, 0);
+ csid_sof_event(csid, i, __csid_sof_timestamp(csid, iface));
}
writel_relaxed(CSID_IRQ_CMD_CLEAR, csid->base + CSID_IRQ_CMD);
diff --git a/drivers/media/platform/qcom/camss/camss-csid.c b/drivers/media/platform/qcom/camss/camss-csid.c
index bb89fe26ec2342487c3ba6357d37b6f188476d1e..c77fafdb9fe450aa3bd245144de353cd0edb44c6 100644
--- a/drivers/media/platform/qcom/camss/camss-csid.c
+++ b/drivers/media/platform/qcom/camss/camss-csid.c
@@ -8,14 +8,17 @@
* Copyright (C) 2015-2018 Linaro Ltd.
*/
#include <linux/clk.h>
+#include <linux/clocksource_ids.h>
#include <linux/completion.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/kernel.h>
+#include <linux/math64.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/regulator/consumer.h>
+#include <linux/timekeeping.h>
#include <media/media-entity.h>
#include <media/mipi-csi2.h>
#include <media/v4l2-device.h>
@@ -37,6 +40,16 @@
#define LANE_CFG_BITWIDTH 4
+/*
+ * The CSID latches the global timestamp counter on SOF and EOF. That counter is
+ * the ARM architected counter, 56 bit wide and ticking at 19.2 MHz.
+ */
+#define CSID_TIMESTAMP_WIDTH 56
+#define CSID_TIMESTAMP_MASK GENMASK_ULL(CSID_TIMESTAMP_WIDTH - 1, 0)
+#define CSID_TIMESTAMP_NS_MUL 10000 /* NSEC_PER_SEC / 19.2 MHz, reduced */
+#define CSID_TIMESTAMP_NS_DIV 192
+#define CSID_TIMESTAMP_MAX_AGE_NS (2 * NSEC_PER_MSEC)
+
#define MSM_CSID_NAME "msm_csid"
const char * const csid_testgen_modes[] = {
@@ -1370,6 +1383,47 @@ atomic_t *csid_get_sof_seq(struct media_pad *pad)
return &csid->sof_seq[pad->index - MSM_CSID_PAD_FIRST_SRC];
}
+static u64 csid_timestamp_to_ns(u64 ticks)
+{
+ return mul_u64_u32_div(ticks, CSID_TIMESTAMP_NS_MUL, CSID_TIMESTAMP_NS_DIV);
+}
+
+u64 csid_timestamp_to_monotonic(struct csid_device *csid, u64 ticks)
+{
+ struct system_time_snapshot snap;
+ u64 delta, delta_ns, now;
+
+ ktime_get_snapshot_id(CLOCK_MONOTONIC, &snap);
+
+ /*
+ * Translating requires reading the very counter the CSID latched from.
+ * If timekeeping is driven by something else there is nothing to
+ * correlate against, so let the caller fall back to IRQ time.
+ */
+ if (!snap.valid || snap.cs_id != CSID_ARM_ARCH_COUNTER)
+ return 0;
+
+ /* How long ago, in counter ticks, the timestamp was latched */
+ delta = (snap.cycles - ticks) & CSID_TIMESTAMP_MASK;
+ delta_ns = csid_timestamp_to_ns(delta);
+ now = ktime_to_ns(snap.systime);
+
+ /* Fall back rather than reporting a nonsensical timestamp. */
+ if (delta_ns > now) {
+ dev_warn_once(csid->camss->dev,
+ "CSID%u: SOF timestamp predates CLOCK_MONOTONIC, ignoring\n",
+ csid->id);
+ return 0;
+ }
+
+ if (delta_ns > CSID_TIMESTAMP_MAX_AGE_NS)
+ dev_warn_ratelimited(csid->camss->dev,
+ "CSID%u: Too slow! SOF timestamp is %llu us old\n",
+ csid->id, delta_ns / NSEC_PER_USEC);
+
+ return now - delta_ns;
+}
+
void csid_sof_event(struct csid_device *csid, unsigned int id, u64 ts)
{
struct timespec64 sof = ns_to_timespec64(ts);
diff --git a/drivers/media/platform/qcom/camss/camss-csid.h b/drivers/media/platform/qcom/camss/camss-csid.h
index 1fde67d16867a24be2a4f8940ed2d2a60f523ee6..4f55c72a44ca3a6cbd29861aa5c90cbab9ed5932 100644
--- a/drivers/media/platform/qcom/camss/camss-csid.h
+++ b/drivers/media/platform/qcom/camss/camss-csid.h
@@ -261,6 +261,17 @@ u32 csid_hw_version(struct csid_device *csid);
u32 csid_src_pad_code(struct csid_device *csid, u32 sink_code,
unsigned int match_format_idx, u32 match_code);
+/*
+ * csid_timestamp_to_monotonic - Put a CSID timestamp on the CLOCK_MONOTONIC
+ * timebase
+ * @csid: CSID device
+ * @ticks: Counter value latched by the CSID
+ *
+ * Return the CLOCK_MONOTONIC time, in nanoseconds, at which @ticks was latched,
+ * or 0 if the counter cannot be correlated with CLOCK_MONOTONIC.
+ */
+u64 csid_timestamp_to_monotonic(struct csid_device *csid, u64 ticks);
+
/*
* csid_sof_event - Track SOF generate V4L2_EVENT_FRAME_SYNC
* @csid: CSID device
--
2.34.1