mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/6] media: qcom: camss: Report the hardware SOF timestamp
@ 2026-11-23 17:16 Loic Poulain
  2026-11-23 17:16 ` [PATCH 1/6] media: v4l2-event: Allow drivers to provide the event timestamp Loic Poulain
                   ` (6 more replies)
  0 siblings, 7 replies; 16+ messages in thread
From: Loic Poulain @ 2026-11-23 17:16 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Bryan O'Donoghue, Vladimir Zapolskiy
  Cc: linux-media, linux-kernel, linux-arm-msm, Loic Poulain

The CAMSS CSID latches the ARM architected counter on every start of
frame. This series exposes that time to userspace, both through the
V4L2_EVENT_FRAME_SYNC event and as the buffer timestamp.

Until now both were taken when the interrupt was handled, or worse, when
the frame had finished being written out to memory, which is about a
frame period late. The hardware value is free of interrupt latency and
is directly comparable with CLOCK_MONOTONIC, so it can be correlated
with other timestamped data such as IMU samples.

Patch 1 lets a driver fill in the timestamp of a V4L2 event instead of
having the core take it. Patch 2 passes the SOF time down to the common
CSID event helper and adds the counter to CLOCK_MONOTONIC conversion.
Patches 3 to 5 read the registers on csid-340 and csid-gen2. Patch 6
uses the same time to stamp the captured buffers.

Patches 4 and 5 are picked from the timestamp support Bryan shared
earlier, with patch 5 adapted to use the conversion helper added in
patch 2 rather than its own.

Tested on QCM2290 (Arduino UNO Q) with an IMX219 sensor.

Signed-off-by: Loic Poulain <loic.poulain@oss.qualcomm.com>
---
Bryan O'Donoghue (2):
      media: camss: Define timestamp registers for csid-gen2
      media: camss: csid-gen2: Report the hardware SOF timestamp

Loic Poulain (4):
      media: v4l2-event: Allow drivers to provide the event timestamp
      media: qcom: camss: csid: Pass the SOF time to csid_sof_event()
      media: qcom: camss: csid-340: Report the hardware SOF timestamp
      media: qcom: camss: Timestamp buffers at start of frame

 drivers/media/platform/qcom/camss/camss-csid-340.c | 37 ++++++++--
 drivers/media/platform/qcom/camss/camss-csid-680.c |  2 +-
 .../media/platform/qcom/camss/camss-csid-gen2.c    | 32 ++++++++-
 .../media/platform/qcom/camss/camss-csid-gen3.c    |  2 +-
 drivers/media/platform/qcom/camss/camss-csid.c     | 81 +++++++++++++++++++++-
 drivers/media/platform/qcom/camss/camss-csid.h     | 25 ++++++-
 drivers/media/platform/qcom/camss/camss-vfe-17x.c  |  7 +-
 drivers/media/platform/qcom/camss/camss-vfe.c      |  8 ++-
 drivers/media/platform/qcom/camss/camss-vfe.h      |  1 +
 drivers/media/v4l2-core/v4l2-event.c               | 20 +++++-
 include/media/v4l2-event.h                         |  9 ++-
 11 files changed, 205 insertions(+), 19 deletions(-)
---
base-commit: 2ad58ca589da80990b7afbcff018777851577f89
change-id: 20261123-camss-hw-ts-e0297adb69d9

Best regards,
-- 
Loic Poulain <loic.poulain@oss.qualcomm.com>


^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH 1/6] media: v4l2-event: Allow drivers to provide the event timestamp
  2026-11-23 17:16 [PATCH 0/6] media: qcom: camss: Report the hardware SOF timestamp Loic Poulain
@ 2026-11-23 17:16 ` Loic Poulain
  2026-11-23 17:16 ` [PATCH 2/6] media: qcom: camss: csid: Pass the SOF time to csid_sof_event() Loic Poulain
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 16+ messages in thread
From: Loic Poulain @ 2026-11-23 17:16 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Bryan O'Donoghue, Vladimir Zapolskiy
  Cc: linux-media, linux-kernel, linux-arm-msm, Loic Poulain

The timestamp reported to userspace for a V4L2 event is taken when the
event is queued, which for an event signalling a hardware occurrence is
only as good as the latency between the two. A CSI decoder able to latch
the start-of-frame against the system counter knows that time to within a
few microseconds, whereas the interrupt handler that gets to queue the
event can be hundreds of microseconds late and is not consistent about
it.

Take the timestamp from the event when the driver filled it in, falling
back to the current time when the field is cleared. Drivers have had no
reason to set it so far, and every in-tree caller either uses a
designated initialiser or memsets the event, so the field reads as zero
everywhere today.

Signed-off-by: Loic Poulain <loic.poulain@oss.qualcomm.com>
---
 drivers/media/v4l2-core/v4l2-event.c | 20 ++++++++++++++++++--
 include/media/v4l2-event.h           |  9 ++++++++-
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/drivers/media/v4l2-core/v4l2-event.c b/drivers/media/v4l2-core/v4l2-event.c
index 9dd2aaa95a67406bda3a257325442888895ed088..a35646c3a5c89febeb072d691ffd7853d9af714a 100644
--- a/drivers/media/v4l2-core/v4l2-event.c
+++ b/drivers/media/v4l2-core/v4l2-event.c
@@ -150,6 +150,22 @@ static void __v4l2_event_queue_fh(struct v4l2_fh *fh,
 	wake_up_all(&fh->wait);
 }
 
+/*
+ * Hardware able to timestamp the event itself reports that time in the event,
+ * which is more accurate than the moment the driver gets to queue it. Fall
+ * back to the current time for the drivers that leave the field cleared.
+ */
+static u64 v4l2_event_ts(const struct v4l2_event *ev)
+{
+	struct timespec64 ts = {
+		.tv_sec = ev->timestamp.tv_sec,
+		.tv_nsec = ev->timestamp.tv_nsec,
+	};
+	u64 ns = timespec64_to_ns(&ts);
+
+	return ns ? ns : ktime_get_ns();
+}
+
 void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev)
 {
 	struct v4l2_fh *fh;
@@ -159,7 +175,7 @@ void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev)
 	if (vdev == NULL)
 		return;
 
-	ts = ktime_get_ns();
+	ts = v4l2_event_ts(ev);
 
 	spin_lock_irqsave(&vdev->fh_lock, flags);
 
@@ -173,7 +189,7 @@ EXPORT_SYMBOL_GPL(v4l2_event_queue);
 void v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev)
 {
 	unsigned long flags;
-	u64 ts = ktime_get_ns();
+	u64 ts = v4l2_event_ts(ev);
 
 	spin_lock_irqsave(&fh->vdev->fh_lock, flags);
 	__v4l2_event_queue_fh(fh, ev, ts);
diff --git a/include/media/v4l2-event.h b/include/media/v4l2-event.h
index 3a0e2588361cf451b3dbfc63dfab99550d13cf6a..076f788e0469ae4666c60344e5ea73a698352134 100644
--- a/include/media/v4l2-event.h
+++ b/include/media/v4l2-event.h
@@ -102,6 +102,11 @@ int v4l2_event_dequeue(struct v4l2_fh *fh, struct v4l2_event *event,
  * .. note::
  *    The driver's only responsibility is to fill in the type and the data
  *    fields. The other fields will be filled in by V4L2.
+ *
+ *    Drivers whose hardware timestamps the event more accurately than the
+ *    moment the driver gets to handle it may also fill in the timestamp
+ *    field, as a CLOCK_MONOTONIC time. It is left alone if non-zero, so such
+ *    drivers must zero-initialize the whole &struct v4l2_event.
  */
 void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev);
 
@@ -116,7 +121,9 @@ void v4l2_event_queue(struct video_device *vdev, const struct v4l2_event *ev);
  *
  * .. note::
  *    The driver's only responsibility is to fill in the type and the data
- *    fields. The other fields will be filled in by V4L2.
+ *    fields. The other fields will be filled in by V4L2. As for
+ *    v4l2_event_queue(), a non-zero timestamp field is taken as the
+ *    CLOCK_MONOTONIC time of the event.
  */
 void v4l2_event_queue_fh(struct v4l2_fh *fh, const struct v4l2_event *ev);
 

-- 
2.34.1


^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH 2/6] media: qcom: camss: csid: Pass the SOF time to csid_sof_event()
  2026-11-23 17:16 [PATCH 0/6] media: qcom: camss: Report the hardware SOF timestamp Loic Poulain
  2026-11-23 17:16 ` [PATCH 1/6] media: v4l2-event: Allow drivers to provide the event timestamp Loic Poulain
@ 2026-11-23 17:16 ` Loic Poulain
  2026-11-24  9:56   ` Bryan O'Donoghue
  2026-11-23 17:16 ` [PATCH 3/6] media: qcom: camss: csid-340: Report the hardware SOF timestamp Loic Poulain
                   ` (4 subsequent siblings)
  6 siblings, 1 reply; 16+ messages in thread
From: Loic Poulain @ 2026-11-23 17:16 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Bryan O'Donoghue, Vladimir Zapolskiy
  Cc: linux-media, linux-kernel, linux-arm-msm, Loic Poulain

csid_sof_event() queues the frame sync event with no notion of when the
start-of-frame it reports actually happened, leaving the V4L2 core to
timestamp it at the moment the event is queued. Some CSID revisions latch
that time in hardware and can do considerably better, so take it as a
parameter and let it reach the event.

Every caller passes 0 for now, which keeps the existing behaviour of
timestamping at queue time.

Signed-off-by: Loic Poulain <loic.poulain@oss.qualcomm.com>
---
 drivers/media/platform/qcom/camss/camss-csid-340.c  | 2 +-
 drivers/media/platform/qcom/camss/camss-csid-680.c  | 2 +-
 drivers/media/platform/qcom/camss/camss-csid-gen2.c | 2 +-
 drivers/media/platform/qcom/camss/camss-csid-gen3.c | 2 +-
 drivers/media/platform/qcom/camss/camss-csid.c      | 5 ++++-
 drivers/media/platform/qcom/camss/camss-csid.h      | 4 +++-
 6 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/drivers/media/platform/qcom/camss/camss-csid-340.c b/drivers/media/platform/qcom/camss/camss-csid-340.c
index 42ff629ce410d33f21930ee56d714f41911cc077..5eef5f8846fbfec5afa1152f73a098fecd717fdf 100644
--- a/drivers/media/platform/qcom/camss/camss-csid-340.c
+++ b/drivers/media/platform/qcom/camss/camss-csid-340.c
@@ -207,7 +207,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);
+			csid_sof_event(csid, i, 0);
 	}
 
 	writel_relaxed(CSID_IRQ_CMD_CLEAR, csid->base + CSID_IRQ_CMD);
diff --git a/drivers/media/platform/qcom/camss/camss-csid-680.c b/drivers/media/platform/qcom/camss/camss-csid-680.c
index 452640e72f6d5f5e2271945d97764c62b630da6c..8ac83d469dc39d45a37c9be815d41bbab12909b0 100644
--- a/drivers/media/platform/qcom/camss/camss-csid-680.c
+++ b/drivers/media/platform/qcom/camss/camss-csid-680.c
@@ -411,7 +411,7 @@ static irqreturn_t csid_isr(int irq, void *dev)
 			writel(val, csid->base + CSID_CSI2_RDIN_IRQ_CLEAR(i));
 
 		if (val & CSID_CSI2_RDIN_INFO_INPUT_SOF)
-			csid_sof_event(csid, i);
+			csid_sof_event(csid, i, 0);
 
 		if (val & CSID_CSI2_RDIN_RUP_DONE)
 			csid_rup_complete(csid, i);
diff --git a/drivers/media/platform/qcom/camss/camss-csid-gen2.c b/drivers/media/platform/qcom/camss/camss-csid-gen2.c
index a351ac4b91728b981fe80e030fcb5ebfdab138b2..72f329c2bb1ddeec96f33c21198104157c14a73c 100644
--- a/drivers/media/platform/qcom/camss/camss-csid-gen2.c
+++ b/drivers/media/platform/qcom/camss/camss-csid-gen2.c
@@ -387,7 +387,7 @@ static irqreturn_t csid_isr(int irq, void *dev)
 			val = readl_relaxed(csid->base + CSID_CSI2_RDIN_IRQ_STATUS(i));
 			writel_relaxed(val, csid->base + CSID_CSI2_RDIN_IRQ_CLEAR(i));
 			if (val & RDIN_IRQ_INFO_INPUT_SOF)
-				csid_sof_event(csid, i);
+				csid_sof_event(csid, i, 0);
 		}
 
 	val = 1 << IRQ_CMD_CLEAR;
diff --git a/drivers/media/platform/qcom/camss/camss-csid-gen3.c b/drivers/media/platform/qcom/camss/camss-csid-gen3.c
index 6a538d825f78be5cf64691c351149b0a8fa68c3d..fa9af117f3e68237f1dd27a48bfc339152c63f94 100644
--- a/drivers/media/platform/qcom/camss/camss-csid-gen3.c
+++ b/drivers/media/platform/qcom/camss/camss-csid-gen3.c
@@ -304,7 +304,7 @@ static irqreturn_t csid_isr(int irq, void *dev)
 			writel(val, csid->base + CSID_CSI2_RDIN_IRQ_CLEAR(i));
 
 			if (val & INFO_INPUT_SOF_IRQ)
-				csid_sof_event(csid, i);
+				csid_sof_event(csid, i, 0);
 
 			if (val & RUP_DONE_IRQ_STATUS)
 				/* clear the reg update bit */
diff --git a/drivers/media/platform/qcom/camss/camss-csid.c b/drivers/media/platform/qcom/camss/camss-csid.c
index 588122beec854ae5b78de9997b68c7a81d4207e1..bb89fe26ec2342487c3ba6357d37b6f188476d1e 100644
--- a/drivers/media/platform/qcom/camss/camss-csid.c
+++ b/drivers/media/platform/qcom/camss/camss-csid.c
@@ -1370,12 +1370,15 @@ atomic_t *csid_get_sof_seq(struct media_pad *pad)
 	return &csid->sof_seq[pad->index - MSM_CSID_PAD_FIRST_SRC];
 }
 
-void csid_sof_event(struct csid_device *csid, unsigned int id)
+void csid_sof_event(struct csid_device *csid, unsigned int id, u64 ts)
 {
+	struct timespec64 sof = ns_to_timespec64(ts);
 	struct v4l2_event event = {
 		.type = V4L2_EVENT_FRAME_SYNC,
 		.u.frame_sync.frame_sequence =
 			atomic_inc_return(&csid->sof_seq[id]),
+		.timestamp.tv_sec = sof.tv_sec,
+		.timestamp.tv_nsec = sof.tv_nsec,
 	};
 
 	if (atomic_read(&csid->frame_sync_users))
diff --git a/drivers/media/platform/qcom/camss/camss-csid.h b/drivers/media/platform/qcom/camss/camss-csid.h
index ccafdbf3a9fe0c282da68ccdd04661b3fab81ff5..1fde67d16867a24be2a4f8940ed2d2a60f523ee6 100644
--- a/drivers/media/platform/qcom/camss/camss-csid.h
+++ b/drivers/media/platform/qcom/camss/camss-csid.h
@@ -265,10 +265,12 @@ u32 csid_src_pad_code(struct csid_device *csid, u32 sink_code,
  * csid_sof_event - Track SOF generate V4L2_EVENT_FRAME_SYNC
  * @csid: CSID device
  * @id: CSID src-stream ID
+ * @ts: CLOCK_MONOTONIC time of the SOF in nanoseconds, 0 if unknown, in which
+ *	case the time the event is queued is used instead
  *
  *  Called from the CSID interrupt handler on SOF of the path driving the event.
  */
-void csid_sof_event(struct csid_device *csid, unsigned int id);
+void csid_sof_event(struct csid_device *csid, unsigned int id, u64 ts);
 
 /*
  * csid_get_sof_seq - Get a pointer to the SOF frame sequence variable.

-- 
2.34.1


^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH 3/6] media: qcom: camss: csid-340: Report the hardware SOF timestamp
  2026-11-23 17:16 [PATCH 0/6] media: qcom: camss: Report the hardware SOF timestamp Loic Poulain
  2026-11-23 17:16 ` [PATCH 1/6] media: v4l2-event: Allow drivers to provide the event timestamp Loic Poulain
  2026-11-23 17:16 ` [PATCH 2/6] media: qcom: camss: csid: Pass the SOF time to csid_sof_event() Loic Poulain
@ 2026-11-23 17:16 ` Loic Poulain
  2026-11-24  9:57   ` Bryan O'Donoghue
  2026-11-23 17:16 ` [PATCH 4/6] media: camss: Define timestamp registers for csid-gen2 Loic Poulain
                   ` (3 subsequent siblings)
  6 siblings, 1 reply; 16+ messages in thread
From: Loic Poulain @ 2026-11-23 17:16 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Bryan O'Donoghue, Vladimir Zapolskiy
  Cc: linux-media, linux-kernel, linux-arm-msm, Loic Poulain

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@oss.qualcomm.com>
---
 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


^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH 4/6] media: camss: Define timestamp registers for csid-gen2
  2026-11-23 17:16 [PATCH 0/6] media: qcom: camss: Report the hardware SOF timestamp Loic Poulain
                   ` (2 preceding siblings ...)
  2026-11-23 17:16 ` [PATCH 3/6] media: qcom: camss: csid-340: Report the hardware SOF timestamp Loic Poulain
@ 2026-11-23 17:16 ` Loic Poulain
  2026-11-23 17:16 ` [PATCH 5/6] media: camss: csid-gen2: Report the hardware SOF timestamp Loic Poulain
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 16+ messages in thread
From: Loic Poulain @ 2026-11-23 17:16 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Bryan O'Donoghue, Vladimir Zapolskiy
  Cc: linux-media, linux-kernel, linux-arm-msm, Loic Poulain

From: Bryan O'Donoghue <bryan.odonoghue@linaro.org>

Timestamp registers inside of the CSID block are available at different
offsets from the CSID base depending on whether the CSID is lite or not.

Signed-off-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>
Signed-off-by: Loic Poulain <loic.poulain@oss.qualcomm.com>
---
 drivers/media/platform/qcom/camss/camss-csid-gen2.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/drivers/media/platform/qcom/camss/camss-csid-gen2.c b/drivers/media/platform/qcom/camss/camss-csid-gen2.c
index 72f329c2bb1ddeec96f33c21198104157c14a73c..ac085ede704653a532aa1ab77b2845471d1e6900 100644
--- a/drivers/media/platform/qcom/camss/camss-csid-gen2.c
+++ b/drivers/media/platform/qcom/camss/camss-csid-gen2.c
@@ -121,6 +121,22 @@
 							+ 0x100 * (rdi))
 #define CSID_RDI_RPP_LINE_DROP_PERIOD(rdi)		((csid_is_lite(csid) ? 0x230 : 0x330)\
 							+ 0x100 * (rdi))
+#define CSID_RDI_TIMESTAMP_CURR0_SOF(rdi)		((csid_is_lite(csid) ? 0x290 : 0x390)\
+							+ 0x100 * (rdi))
+#define CSID_RDI_TIMESTAMP_CURR1_SOF(rdi)		((csid_is_lite(csid) ? 0x294 : 0x394)\
+							+ 0x100 * (rdi))
+#define CSID_RDI_TIMESTAMP_PREV0_SOF(rdi)		((csid_is_lite(csid) ? 0x298 : 0x398)\
+							+ 0x100 * (rdi))
+#define CSID_RDI_TIMESTAMP_PREV1_SOF(rdi)		((csid_is_lite(csid) ? 0x29C : 0x39C)\
+							+ 0x100 * (rdi))
+#define CSID_RDI_TIMESTAMP_CURR0_EOF(rdi)		((csid_is_lite(csid) ? 0x2A0 : 0x3A0)\
+							+ 0x100 * (rdi))
+#define CSID_RDI_TIMESTAMP_CURR1_EOF(rdi)		((csid_is_lite(csid) ? 0x2A4 : 0x3A4)\
+							+ 0x100 * (rdi))
+#define CSID_RDI_TIMESTAMP_PREV0_EOF(rdi)		((csid_is_lite(csid) ? 0x2A8 : 0x3A8)\
+							+ 0x100 * (rdi))
+#define CSID_RDI_TIMESTAMP_PREV1_EOF(rdi)		((csid_is_lite(csid) ? 0x2AC : 0x3AC)\
+							+ 0x100 * (rdi))
 
 #define CSID_TPG_CTRL		0x600
 #define		TPG_CTRL_TEST_EN		0

-- 
2.34.1


^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH 5/6] media: camss: csid-gen2: Report the hardware SOF timestamp
  2026-11-23 17:16 [PATCH 0/6] media: qcom: camss: Report the hardware SOF timestamp Loic Poulain
                   ` (3 preceding siblings ...)
  2026-11-23 17:16 ` [PATCH 4/6] media: camss: Define timestamp registers for csid-gen2 Loic Poulain
@ 2026-11-23 17:16 ` Loic Poulain
  2026-11-23 17:16 ` [PATCH 6/6] media: qcom: camss: Timestamp buffers at start of frame Loic Poulain
  2026-11-24  9:59 ` [PATCH 0/6] media: qcom: camss: Report the hardware SOF timestamp Bryan O'Donoghue
  6 siblings, 0 replies; 16+ messages in thread
From: Loic Poulain @ 2026-11-23 17:16 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Bryan O'Donoghue, Vladimir Zapolskiy
  Cc: linux-media, linux-kernel, linux-arm-msm, Loic Poulain

From: Bryan O'Donoghue <bryan.odonoghue@linaro.org>

The CSID latches a free-running timer value for every RDI at
start-of-frame, a timer that keeps running across system suspend. The
driver already enables that latching, it just never reads the result,
leaving the frame sync event to be timestamped when the interrupt
handler gets around to queueing it.

Read the CURRENT_SOF pair instead and hand it to
csid_timestamp_to_monotonic(), which puts it on the CLOCK_MONOTONIC
timebase the event carries. CURRENT_EOF and the PREVIOUS_* pairs are
left alone, following the reference Qualcomm Camx code which only
interacts with CURRENT_SOF.

Upstream CAMSS uses a fixed mapping of RDI to virtual channel, hence
VC0 => RDI0 and VC1 => RDI1, so the event id indexes the register block
directly.

Signed-off-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>
Signed-off-by: Loic Poulain <loic.poulain@oss.qualcomm.com>
---
 drivers/media/platform/qcom/camss/camss-csid-gen2.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/drivers/media/platform/qcom/camss/camss-csid-gen2.c b/drivers/media/platform/qcom/camss/camss-csid-gen2.c
index ac085ede704653a532aa1ab77b2845471d1e6900..a2da70d5533314816fdacfb8b07248860d243b58 100644
--- a/drivers/media/platform/qcom/camss/camss-csid-gen2.c
+++ b/drivers/media/platform/qcom/camss/camss-csid-gen2.c
@@ -376,6 +376,20 @@ static int csid_configure_testgen_pattern(struct csid_device *csid, s32 val)
 	return 0;
 }
 
+static u64 __csid_sof_timestamp(struct csid_device *csid, u8 rdi)
+{
+	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_RDI_TIMESTAMP_CURR0_SOF(rdi));
+	hi = readl_relaxed(csid->base + CSID_RDI_TIMESTAMP_CURR1_SOF(rdi));
+
+	return csid_timestamp_to_monotonic(csid, ((u64)hi << 32) | lo);
+}
+
 /*
  * csid_isr - CSID module interrupt service routine
  * @irq: Interrupt line
@@ -403,7 +417,7 @@ static irqreturn_t csid_isr(int irq, void *dev)
 			val = readl_relaxed(csid->base + CSID_CSI2_RDIN_IRQ_STATUS(i));
 			writel_relaxed(val, csid->base + CSID_CSI2_RDIN_IRQ_CLEAR(i));
 			if (val & RDIN_IRQ_INFO_INPUT_SOF)
-				csid_sof_event(csid, i, 0);
+				csid_sof_event(csid, i, __csid_sof_timestamp(csid, i));
 		}
 
 	val = 1 << IRQ_CMD_CLEAR;

-- 
2.34.1


^ permalink raw reply	[flat|nested] 16+ messages in thread

* [PATCH 6/6] media: qcom: camss: Timestamp buffers at start of frame
  2026-11-23 17:16 [PATCH 0/6] media: qcom: camss: Report the hardware SOF timestamp Loic Poulain
                   ` (4 preceding siblings ...)
  2026-11-23 17:16 ` [PATCH 5/6] media: camss: csid-gen2: Report the hardware SOF timestamp Loic Poulain
@ 2026-11-23 17:16 ` Loic Poulain
  2026-11-24  9:59 ` [PATCH 0/6] media: qcom: camss: Report the hardware SOF timestamp Bryan O'Donoghue
  6 siblings, 0 replies; 16+ messages in thread
From: Loic Poulain @ 2026-11-23 17:16 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Bryan O'Donoghue, Vladimir Zapolskiy
  Cc: linux-media, linux-kernel, linux-arm-msm, Loic Poulain

Buffers are currently stamped with ktime_get_ns() from the write master
done interrupt, i.e. once the frame has been written out to memory. V4L2
defines vb2_buf.timestamp as the time the first data byte was captured,
so the reported time is late by about a frame period, plus whatever
interrupt latency the system adds.

The CSID already computes the SOF time, from hardware where that is
available, so hand it to the VFE the same way the SOF sequence number is
handed over: the CSID stores it in a per-src-stream atomic64_t, the VFE
picks up a pointer to it when it enables the output and reads it when a
buffer completes. Hardware without SOF tracking keeps using the buf done
time.

The frame to SOF association is best effort, as it is for the sequence
number: buf done reads the most recent SOF, so if write-out of frame N
completes after SOF N+1 has been signalled, the timestamp belongs to
N+1. That is still closer to the capture time than the buf done time is.

Signed-off-by: Loic Poulain <loic.poulain@oss.qualcomm.com>
---
 drivers/media/platform/qcom/camss/camss-csid.c    | 22 ++++++++++++++++++++--
 drivers/media/platform/qcom/camss/camss-csid.h    | 10 ++++++++++
 drivers/media/platform/qcom/camss/camss-vfe-17x.c |  7 +++++--
 drivers/media/platform/qcom/camss/camss-vfe.c     |  8 ++++++--
 drivers/media/platform/qcom/camss/camss-vfe.h     |  1 +
 5 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/drivers/media/platform/qcom/camss/camss-csid.c b/drivers/media/platform/qcom/camss/camss-csid.c
index c77fafdb9fe450aa3bd245144de353cd0edb44c6..fceb5055bc021bc8ac2ec8de28e1d26f7a337c5d 100644
--- a/drivers/media/platform/qcom/camss/camss-csid.c
+++ b/drivers/media/platform/qcom/camss/camss-csid.c
@@ -792,9 +792,11 @@ static int csid_set_stream(struct v4l2_subdev *sd, int enable)
 	}
 
 	if (csid->phy.need_vc_update) {
-		/* Set to -1 as we want to do atomic_inc_return to read */
-		for (i = 0; i < MSM_CSID_MAX_SRC_STREAMS; i++)
+		for (i = 0; i < MSM_CSID_MAX_SRC_STREAMS; i++) {
+			/* Set to -1 as we want to do atomic_inc_return to read */
 			atomic_set(&csid->sof_seq[i], -1);
+			atomic64_set(&csid->sof_ts[i], 0);
+		}
 
 		csid->res->hw_ops->configure_stream(csid, enable);
 		csid->phy.need_vc_update = false;
@@ -1383,6 +1385,20 @@ atomic_t *csid_get_sof_seq(struct media_pad *pad)
 	return &csid->sof_seq[pad->index - MSM_CSID_PAD_FIRST_SRC];
 }
 
+atomic64_t *csid_get_sof_ts(struct media_pad *pad)
+{
+	struct csid_device *csid;
+
+	if (!pad || pad->index < MSM_CSID_PAD_FIRST_SRC)
+		return NULL;
+
+	csid = v4l2_get_subdevdata(media_entity_to_v4l2_subdev(pad->entity));
+	if (!csid->res->frame_sync_en)
+		return NULL;
+
+	return &csid->sof_ts[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);
@@ -1435,6 +1451,8 @@ void csid_sof_event(struct csid_device *csid, unsigned int id, u64 ts)
 		.timestamp.tv_nsec = sof.tv_nsec,
 	};
 
+	atomic64_set(&csid->sof_ts[id], ts ? ts : ktime_get_ns());
+
 	if (atomic_read(&csid->frame_sync_users))
 		v4l2_event_queue(csid->subdev.devnode, &event);
 }
diff --git a/drivers/media/platform/qcom/camss/camss-csid.h b/drivers/media/platform/qcom/camss/camss-csid.h
index 4f55c72a44ca3a6cbd29861aa5c90cbab9ed5932..50c4f96a691f2e1b7196427566b1c0cfc87da1ab 100644
--- a/drivers/media/platform/qcom/camss/camss-csid.h
+++ b/drivers/media/platform/qcom/camss/camss-csid.h
@@ -179,6 +179,7 @@ struct csid_device {
 	const struct csid_subdev_resources *res;
 	atomic_t frame_sync_users;
 	atomic_t sof_seq[MSM_CSID_MAX_SRC_STREAMS];
+	atomic64_t sof_ts[MSM_CSID_MAX_SRC_STREAMS];
 };
 
 struct camss_subdev_resources;
@@ -292,4 +293,13 @@ void csid_sof_event(struct csid_device *csid, unsigned int id, u64 ts);
  */
 atomic_t *csid_get_sof_seq(struct media_pad *pad);
 
+/*
+ * csid_get_sof_ts - Get a pointer to the SOF timestamp variable.
+ * @pad: Remote pad of a consumer's sink pad
+ *
+ * Return the timestamp or NULL if @pad isn't a CSID or the CSID doesn't support
+ * SOF tracking. It reads as 0 until the first SOF of the stream.
+ */
+atomic64_t *csid_get_sof_ts(struct media_pad *pad);
+
 #endif /* QC_MSM_CAMSS_CSID_H */
diff --git a/drivers/media/platform/qcom/camss/camss-vfe-17x.c b/drivers/media/platform/qcom/camss/camss-vfe-17x.c
index 4972d3ab65078387735ef2e853bf1c9c3d82baee..6b8df1f6e2a731ccd0e739edaa82221c18203f74 100644
--- a/drivers/media/platform/qcom/camss/camss-vfe-17x.c
+++ b/drivers/media/platform/qcom/camss/camss-vfe-17x.c
@@ -472,7 +472,6 @@ static void vfe_isr_wm_done(struct vfe_device *vfe, u8 wm)
 	struct vfe_output *output;
 	unsigned long flags;
 	u32 index;
-	u64 ts = ktime_get_ns();
 
 	spin_lock_irqsave(&vfe->output_lock, flags);
 
@@ -490,7 +489,11 @@ static void vfe_isr_wm_done(struct vfe_device *vfe, u8 wm)
 		goto out_unlock;
 	}
 
-	ready_buf->vb.vb2_buf.timestamp = ts;
+	if (line->sof_ts)
+		ready_buf->vb.vb2_buf.timestamp = atomic64_read(line->sof_ts);
+	else
+		ready_buf->vb.vb2_buf.timestamp = ktime_get_ns();
+
 	if (line->sof_seq)
 		ready_buf->vb.sequence = atomic_read(line->sof_seq);
 	else
diff --git a/drivers/media/platform/qcom/camss/camss-vfe.c b/drivers/media/platform/qcom/camss/camss-vfe.c
index f9bd2367671f6e50c6948a6405f78b8c2b03e5f7..ab4edb14ed7486724f12fbd024e0600d70f8355d 100644
--- a/drivers/media/platform/qcom/camss/camss-vfe.c
+++ b/drivers/media/platform/qcom/camss/camss-vfe.c
@@ -456,7 +456,6 @@ void vfe_buf_done(struct vfe_device *vfe, int wm)
 	struct vfe_output *output;
 	unsigned long flags;
 	u32 index;
-	u64 ts = ktime_get_ns();
 
 	spin_lock_irqsave(&vfe->output_lock, flags);
 
@@ -474,7 +473,11 @@ void vfe_buf_done(struct vfe_device *vfe, int wm)
 		goto out_unlock;
 	}
 
-	ready_buf->vb.vb2_buf.timestamp = ts;
+	if (line->sof_ts)
+		ready_buf->vb.vb2_buf.timestamp = atomic64_read(line->sof_ts);
+	else
+		ready_buf->vb.vb2_buf.timestamp = ktime_get_ns();
+
 	if (line->sof_seq)
 		ready_buf->vb.sequence = atomic_read(line->sof_seq);
 	else
@@ -532,6 +535,7 @@ int vfe_enable_output_v2(struct vfe_line *line)
 
 	csid_pad = media_pad_remote_pad_first(&line->pads[MSM_VFE_PAD_SINK]);
 	line->sof_seq = csid_get_sof_seq(csid_pad);
+	line->sof_ts = csid_get_sof_ts(csid_pad);
 
 	spin_lock_irqsave(&vfe->output_lock, flags);
 
diff --git a/drivers/media/platform/qcom/camss/camss-vfe.h b/drivers/media/platform/qcom/camss/camss-vfe.h
index c37526caf9a6fc052c3f4ab4215e635167f6da95..8eda4592257169d04027a9a3c45233ac937eecae 100644
--- a/drivers/media/platform/qcom/camss/camss-vfe.h
+++ b/drivers/media/platform/qcom/camss/camss-vfe.h
@@ -95,6 +95,7 @@ struct vfe_line {
 	const struct camss_format_info *formats;
 	unsigned int nformats;
 	atomic_t *sof_seq;
+	atomic64_t *sof_ts;
 };
 
 struct vfe_device;

-- 
2.34.1


^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 2/6] media: qcom: camss: csid: Pass the SOF time to csid_sof_event()
  2026-11-23 17:16 ` [PATCH 2/6] media: qcom: camss: csid: Pass the SOF time to csid_sof_event() Loic Poulain
@ 2026-11-24  9:56   ` Bryan O'Donoghue
  0 siblings, 0 replies; 16+ messages in thread
From: Bryan O'Donoghue @ 2026-11-24  9:56 UTC (permalink / raw)
  To: Loic Poulain, Mauro Carvalho Chehab, Vladimir Zapolskiy
  Cc: linux-media, linux-kernel, linux-arm-msm

On 23/11/2026 17:16, Loic Poulain wrote:
> csid_sof_event() queues the frame sync event with no notion of when the
> start-of-frame it reports actually happened, leaving the V4L2 core to
> timestamp it at the moment the event is queued. Some CSID revisions latch
> that time in hardware and can do considerably better, so take it as a
> parameter and let it reach the event.
> 
> Every caller passes 0 for now, which keeps the existing behaviour of
> timestamping at queue time.
> 
> Signed-off-by: Loic Poulain <loic.poulain@oss.qualcomm.com>
Reviewed-by: Bryan O'Donoghue <bryan.odonoghue@linaro.org>

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 3/6] media: qcom: camss: csid-340: Report the hardware SOF timestamp
  2026-11-23 17:16 ` [PATCH 3/6] media: qcom: camss: csid-340: Report the hardware SOF timestamp Loic Poulain
@ 2026-11-24  9:57   ` Bryan O'Donoghue
  2026-11-24 12:20     ` Loic Poulain
  0 siblings, 1 reply; 16+ messages in thread
From: Bryan O'Donoghue @ 2026-11-24  9:57 UTC (permalink / raw)
  To: Loic Poulain, Mauro Carvalho Chehab, Vladimir Zapolskiy
  Cc: linux-media, linux-kernel, linux-arm-msm

On 23/11/2026 17:16, Loic Poulain wrote:
> +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));
>   	}

I think a callback in csid_sof_event() might be cleaner or some other 
way to not have to replicate all of __csid_sof_timestamp() for each CSI 
decoder.

---
bod

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 0/6] media: qcom: camss: Report the hardware SOF timestamp
  2026-11-23 17:16 [PATCH 0/6] media: qcom: camss: Report the hardware SOF timestamp Loic Poulain
                   ` (5 preceding siblings ...)
  2026-11-23 17:16 ` [PATCH 6/6] media: qcom: camss: Timestamp buffers at start of frame Loic Poulain
@ 2026-11-24  9:59 ` Bryan O'Donoghue
  2026-11-24 10:21   ` Loic Poulain
  6 siblings, 1 reply; 16+ messages in thread
From: Bryan O'Donoghue @ 2026-11-24  9:59 UTC (permalink / raw)
  To: Loic Poulain, Mauro Carvalho Chehab, Vladimir Zapolskiy
  Cc: linux-media, linux-kernel, linux-arm-msm

On 23/11/2026 17:16, Loic Poulain wrote:
> The CAMSS CSID latches the ARM architected counter on every start of
> frame. This series exposes that time to userspace, both through the
> V4L2_EVENT_FRAME_SYNC event and as the buffer timestamp.
> 
> Until now both were taken when the interrupt was handled, or worse, when
> the frame had finished being written out to memory, which is about a
> frame period late. The hardware value is free of interrupt latency and
> is directly comparable with CLOCK_MONOTONIC, so it can be correlated
> with other timestamped data such as IMU samples.
> 
> Patch 1 lets a driver fill in the timestamp of a V4L2 event instead of
> having the core take it. Patch 2 passes the SOF time down to the common
> CSID event helper and adds the counter to CLOCK_MONOTONIC conversion.
> Patches 3 to 5 read the registers on csid-340 and csid-gen2. Patch 6
> uses the same time to stamp the captured buffers.
> 
> Patches 4 and 5 are picked from the timestamp support Bryan shared
> earlier, with patch 5 adapted to use the conversion helper added in
> patch 2 rather than its own.
> 
> Tested on QCM2290 (Arduino UNO Q) with an IMX219 sensor.
> 
> Signed-off-by: Loic Poulain <loic.poulain@oss.qualcomm.com>
> ---
> Bryan O'Donoghue (2):
>        media: camss: Define timestamp registers for csid-gen2
>        media: camss: csid-gen2: Report the hardware SOF timestamp
> 
> Loic Poulain (4):
>        media: v4l2-event: Allow drivers to provide the event timestamp
>        media: qcom: camss: csid: Pass the SOF time to csid_sof_event()
>        media: qcom: camss: csid-340: Report the hardware SOF timestamp
>        media: qcom: camss: Timestamp buffers at start of frame
So I was offlining this change and kind of left it at comments I 
received to make the type of timestamp selectable on a per-buffer basis.

IDK if that's really required for a first pass but, its where my own 
reviews were left at that time.

---
bod

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 0/6] media: qcom: camss: Report the hardware SOF timestamp
  2026-11-24  9:59 ` [PATCH 0/6] media: qcom: camss: Report the hardware SOF timestamp Bryan O'Donoghue
@ 2026-11-24 10:21   ` Loic Poulain
  2026-11-24 10:24     ` Loic Poulain
  2026-11-24 11:47     ` Bryan O'Donoghue
  0 siblings, 2 replies; 16+ messages in thread
From: Loic Poulain @ 2026-11-24 10:21 UTC (permalink / raw)
  To: Bryan O'Donoghue
  Cc: Mauro Carvalho Chehab, Vladimir Zapolskiy, linux-media,
	linux-kernel, linux-arm-msm

On Tue, Nov 24, 2026 at 10:59 AM Bryan O'Donoghue
<bryan.odonoghue@linaro.org> wrote:
>
> On 23/11/2026 17:16, Loic Poulain wrote:
> > The CAMSS CSID latches the ARM architected counter on every start of
> > frame. This series exposes that time to userspace, both through the
> > V4L2_EVENT_FRAME_SYNC event and as the buffer timestamp.
> >
> > Until now both were taken when the interrupt was handled, or worse, when
> > the frame had finished being written out to memory, which is about a
> > frame period late. The hardware value is free of interrupt latency and
> > is directly comparable with CLOCK_MONOTONIC, so it can be correlated
> > with other timestamped data such as IMU samples.
> >
> > Patch 1 lets a driver fill in the timestamp of a V4L2 event instead of
> > having the core take it. Patch 2 passes the SOF time down to the common
> > CSID event helper and adds the counter to CLOCK_MONOTONIC conversion.
> > Patches 3 to 5 read the registers on csid-340 and csid-gen2. Patch 6
> > uses the same time to stamp the captured buffers.
> >
> > Patches 4 and 5 are picked from the timestamp support Bryan shared
> > earlier, with patch 5 adapted to use the conversion helper added in
> > patch 2 rather than its own.
> >
> > Tested on QCM2290 (Arduino UNO Q) with an IMX219 sensor.
> >
> > Signed-off-by: Loic Poulain <loic.poulain@oss.qualcomm.com>
> > ---
> > Bryan O'Donoghue (2):
> >        media: camss: Define timestamp registers for csid-gen2
> >        media: camss: csid-gen2: Report the hardware SOF timestamp
> >
> > Loic Poulain (4):
> >        media: v4l2-event: Allow drivers to provide the event timestamp
> >        media: qcom: camss: csid: Pass the SOF time to csid_sof_event()
> >        media: qcom: camss: csid-340: Report the hardware SOF timestamp
> >        media: qcom: camss: Timestamp buffers at start of frame
> So I was offlining this change and kind of left it at comments I
> received to make the type of timestamp selectable on a per-buffer basis.
>
> IDK if that's really required for a first pass but, its where my own
> reviews were left at that time.

I can imagine this being useful in some scenarios, but today the
timestamp source is configured per queue, and there is no userspace
API to select or switch between timestamp source flags AFIK. So I
would consider support for that to be out of scope for this series.

>
> ---
> bod

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 0/6] media: qcom: camss: Report the hardware SOF timestamp
  2026-11-24 10:21   ` Loic Poulain
@ 2026-11-24 10:24     ` Loic Poulain
  2026-11-24 11:47     ` Bryan O'Donoghue
  1 sibling, 0 replies; 16+ messages in thread
From: Loic Poulain @ 2026-11-24 10:24 UTC (permalink / raw)
  To: Bryan O'Donoghue
  Cc: Mauro Carvalho Chehab, Vladimir Zapolskiy, linux-media,
	linux-kernel, linux-arm-msm

On Tue, Nov 24, 2026 at 11:21 AM Loic Poulain
<loic.poulain@oss.qualcomm.com> wrote:
>
> On Tue, Nov 24, 2026 at 10:59 AM Bryan O'Donoghue
> <bryan.odonoghue@linaro.org> wrote:
> >
> > On 23/11/2026 17:16, Loic Poulain wrote:
> > > The CAMSS CSID latches the ARM architected counter on every start of
> > > frame. This series exposes that time to userspace, both through the
> > > V4L2_EVENT_FRAME_SYNC event and as the buffer timestamp.
> > >
> > > Until now both were taken when the interrupt was handled, or worse, when
> > > the frame had finished being written out to memory, which is about a
> > > frame period late. The hardware value is free of interrupt latency and
> > > is directly comparable with CLOCK_MONOTONIC, so it can be correlated
> > > with other timestamped data such as IMU samples.
> > >
> > > Patch 1 lets a driver fill in the timestamp of a V4L2 event instead of
> > > having the core take it. Patch 2 passes the SOF time down to the common
> > > CSID event helper and adds the counter to CLOCK_MONOTONIC conversion.
> > > Patches 3 to 5 read the registers on csid-340 and csid-gen2. Patch 6
> > > uses the same time to stamp the captured buffers.
> > >
> > > Patches 4 and 5 are picked from the timestamp support Bryan shared
> > > earlier, with patch 5 adapted to use the conversion helper added in
> > > patch 2 rather than its own.
> > >
> > > Tested on QCM2290 (Arduino UNO Q) with an IMX219 sensor.
> > >
> > > Signed-off-by: Loic Poulain <loic.poulain@oss.qualcomm.com>
> > > ---
> > > Bryan O'Donoghue (2):
> > >        media: camss: Define timestamp registers for csid-gen2
> > >        media: camss: csid-gen2: Report the hardware SOF timestamp
> > >
> > > Loic Poulain (4):
> > >        media: v4l2-event: Allow drivers to provide the event timestamp
> > >        media: qcom: camss: csid: Pass the SOF time to csid_sof_event()
> > >        media: qcom: camss: csid-340: Report the hardware SOF timestamp
> > >        media: qcom: camss: Timestamp buffers at start of frame
> > So I was offlining this change and kind of left it at comments I
> > received to make the type of timestamp selectable on a per-buffer basis.
> >
> > IDK if that's really required for a first pass but, its where my own
> > reviews were left at that time.
>
> I can imagine this being useful in some scenarios, but today the
> timestamp source is configured per queue, and there is no userspace
> API to select or switch between timestamp source flags AFIK. So I
> would consider support for that to be out of scope for this series.

Ah, and that makes me think we may want to tag our timestamp as SOE now.

Regards,
Loic

>
> >
> > ---
> > bod

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 0/6] media: qcom: camss: Report the hardware SOF timestamp
  2026-11-24 10:21   ` Loic Poulain
  2026-11-24 10:24     ` Loic Poulain
@ 2026-11-24 11:47     ` Bryan O'Donoghue
  1 sibling, 0 replies; 16+ messages in thread
From: Bryan O'Donoghue @ 2026-11-24 11:47 UTC (permalink / raw)
  To: Loic Poulain
  Cc: Mauro Carvalho Chehab, Vladimir Zapolskiy, linux-media,
	linux-kernel, linux-arm-msm

On 24/11/2026 10:21, Loic Poulain wrote:
> On Tue, Nov 24, 2026 at 10:59 AM Bryan O'Donoghue
> <bryan.odonoghue@linaro.org> wrote:
>>
>> On 23/11/2026 17:16, Loic Poulain wrote:
>>> The CAMSS CSID latches the ARM architected counter on every start of
>>> frame. This series exposes that time to userspace, both through the
>>> V4L2_EVENT_FRAME_SYNC event and as the buffer timestamp.
>>>
>>> Until now both were taken when the interrupt was handled, or worse, when
>>> the frame had finished being written out to memory, which is about a
>>> frame period late. The hardware value is free of interrupt latency and
>>> is directly comparable with CLOCK_MONOTONIC, so it can be correlated
>>> with other timestamped data such as IMU samples.
>>>
>>> Patch 1 lets a driver fill in the timestamp of a V4L2 event instead of
>>> having the core take it. Patch 2 passes the SOF time down to the common
>>> CSID event helper and adds the counter to CLOCK_MONOTONIC conversion.
>>> Patches 3 to 5 read the registers on csid-340 and csid-gen2. Patch 6
>>> uses the same time to stamp the captured buffers.
>>>
>>> Patches 4 and 5 are picked from the timestamp support Bryan shared
>>> earlier, with patch 5 adapted to use the conversion helper added in
>>> patch 2 rather than its own.
>>>
>>> Tested on QCM2290 (Arduino UNO Q) with an IMX219 sensor.
>>>
>>> Signed-off-by: Loic Poulain <loic.poulain@oss.qualcomm.com>
>>> ---
>>> Bryan O'Donoghue (2):
>>>         media: camss: Define timestamp registers for csid-gen2
>>>         media: camss: csid-gen2: Report the hardware SOF timestamp
>>>
>>> Loic Poulain (4):
>>>         media: v4l2-event: Allow drivers to provide the event timestamp
>>>         media: qcom: camss: csid: Pass the SOF time to csid_sof_event()
>>>         media: qcom: camss: csid-340: Report the hardware SOF timestamp
>>>         media: qcom: camss: Timestamp buffers at start of frame
>> So I was offlining this change and kind of left it at comments I
>> received to make the type of timestamp selectable on a per-buffer basis.
>>
>> IDK if that's really required for a first pass but, its where my own
>> reviews were left at that time.
> 
> I can imagine this being useful in some scenarios, but today the
> timestamp source is configured per queue, and there is no userspace
> API to select or switch between timestamp source flags AFIK. So I
> would consider support for that to be out of scope for this series.
> 
>>
>> ---
>> bod

It's on at least one branch I have its not finished but on the way

git://git.kernel.org/pub/scm/linux/kernel/git/bod/linux.git 
linux-next-23-04-03-sm8250-typec-camss-qtimer

That's where the other two commits you've picked up come from.

Please have a look.

---
bod

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 3/6] media: qcom: camss: csid-340: Report the hardware SOF timestamp
  2026-11-24  9:57   ` Bryan O'Donoghue
@ 2026-11-24 12:20     ` Loic Poulain
  2026-11-24 12:28       ` Bryan O'Donoghue
  0 siblings, 1 reply; 16+ messages in thread
From: Loic Poulain @ 2026-11-24 12:20 UTC (permalink / raw)
  To: Bryan O'Donoghue
  Cc: Mauro Carvalho Chehab, Vladimir Zapolskiy, linux-media,
	linux-kernel, linux-arm-msm

On Tue, Nov 24, 2026 at 10:57 AM Bryan O'Donoghue
<bryan.odonoghue@linaro.org> wrote:
>
> On 23/11/2026 17:16, Loic Poulain wrote:
> > +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));
> >       }
>
> I think a callback in csid_sof_event() might be cleaner or some other
> way to not have to replicate all of __csid_sof_timestamp() for each CSI
> decoder.

I added it as a parameter because it is provided in much the same way
as the sequence number, given that the timestamp is directly related
to and coupled with it. So it is not clearly obvious why the sequence
number should be passed as a parameter while the timestamp should not.

So yes we could also introduce a hw_ops callback, but that would
ultimately require replicating the implementation for each CSI decoder
anyway, or having some semi-shared helpers.

Regards,
Loic

>
> ---
> bod

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 3/6] media: qcom: camss: csid-340: Report the hardware SOF timestamp
  2026-11-24 12:20     ` Loic Poulain
@ 2026-11-24 12:28       ` Bryan O'Donoghue
  2026-11-24 12:35         ` Bryan O'Donoghue
  0 siblings, 1 reply; 16+ messages in thread
From: Bryan O'Donoghue @ 2026-11-24 12:28 UTC (permalink / raw)
  To: Loic Poulain
  Cc: Mauro Carvalho Chehab, Vladimir Zapolskiy, linux-media,
	linux-kernel, linux-arm-msm

On 24/11/2026 12:20, Loic Poulain wrote:
>> On 23/11/2026 17:16, Loic Poulain wrote:
>>> +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));
>>>        }
>> I think a callback in csid_sof_event() might be cleaner or some other
>> way to not have to replicate all of __csid_sof_timestamp() for each CSI
>> decoder.
> I added it as a parameter because it is provided in much the same way
> as the sequence number, given that the timestamp is directly related
> to and coupled with it. So it is not clearly obvious why the sequence
> number should be passed as a parameter while the timestamp should not.

But pretty much every hardware implementation will have to do the above 
function. I'm asking for more functional decomposition.

You could pass the hi/lo for example and make the sof_timestamp() 
function common - called once from inside of csid_sof_event()

---
bod

^ permalink raw reply	[flat|nested] 16+ messages in thread

* Re: [PATCH 3/6] media: qcom: camss: csid-340: Report the hardware SOF timestamp
  2026-11-24 12:28       ` Bryan O'Donoghue
@ 2026-11-24 12:35         ` Bryan O'Donoghue
  0 siblings, 0 replies; 16+ messages in thread
From: Bryan O'Donoghue @ 2026-11-24 12:35 UTC (permalink / raw)
  To: Loic Poulain
  Cc: Mauro Carvalho Chehab, Vladimir Zapolskiy, linux-media,
	linux-kernel, linux-arm-msm

On 24/11/2026 12:28, Bryan O'Donoghue wrote:
> On 24/11/2026 12:20, Loic Poulain wrote:
>>> On 23/11/2026 17:16, Loic Poulain wrote:
>>>> +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));
>>>>        }
>>> I think a callback in csid_sof_event() might be cleaner or some other
>>> way to not have to replicate all of __csid_sof_timestamp() for each CSI
>>> decoder.
>> I added it as a parameter because it is provided in much the same way
>> as the sequence number, given that the timestamp is directly related
>> to and coupled with it. So it is not clearly obvious why the sequence
>> number should be passed as a parameter while the timestamp should not.
> 
> But pretty much every hardware implementation will have to do the above 
> function. I'm asking for more functional decomposition.
> 
> You could pass the hi/lo for example and make the sof_timestamp() 
> function common - called once from inside of csid_sof_event()
> 
> ---
> bod

Anyway yeah csid_sof_event() increments the seq_no itself so you could 
just have a ts = csid->get_sof_ts(csid, ...); and functionally decompose 
without passing hi/lo down the callstack too.

Either option really.

---
bod

^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2026-11-24 12:35 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-23 17:16 [PATCH 0/6] media: qcom: camss: Report the hardware SOF timestamp Loic Poulain
2026-11-23 17:16 ` [PATCH 1/6] media: v4l2-event: Allow drivers to provide the event timestamp Loic Poulain
2026-11-23 17:16 ` [PATCH 2/6] media: qcom: camss: csid: Pass the SOF time to csid_sof_event() Loic Poulain
2026-11-24  9:56   ` Bryan O'Donoghue
2026-11-23 17:16 ` [PATCH 3/6] media: qcom: camss: csid-340: Report the hardware SOF timestamp Loic Poulain
2026-11-24  9:57   ` Bryan O'Donoghue
2026-11-24 12:20     ` Loic Poulain
2026-11-24 12:28       ` Bryan O'Donoghue
2026-11-24 12:35         ` Bryan O'Donoghue
2026-11-23 17:16 ` [PATCH 4/6] media: camss: Define timestamp registers for csid-gen2 Loic Poulain
2026-11-23 17:16 ` [PATCH 5/6] media: camss: csid-gen2: Report the hardware SOF timestamp Loic Poulain
2026-11-23 17:16 ` [PATCH 6/6] media: qcom: camss: Timestamp buffers at start of frame Loic Poulain
2026-11-24  9:59 ` [PATCH 0/6] media: qcom: camss: Report the hardware SOF timestamp Bryan O'Donoghue
2026-11-24 10:21   ` Loic Poulain
2026-11-24 10:24     ` Loic Poulain
2026-11-24 11:47     ` Bryan O'Donoghue

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®