mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/7] media: qcom: camss: Report the hardware SOF timestamp
@ 2026-11-25 10:19 Loic Poulain
  2026-11-25 10:19 ` [PATCH v2 1/7] media: v4l2-event: Allow drivers to provide the event timestamp Loic Poulain
                   ` (6 more replies)
  0 siblings, 7 replies; 17+ messages in thread
From: Loic Poulain @ 2026-11-25 10:19 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 adds a CSID operation for reading the
counter latched at SOF, converts it to CLOCK_MONOTONIC and puts it in
the event. Patches 3 to 5 implement that operation on csid-340 and
csid-gen2. Patch 6 uses the same time to stamp the captured buffers and
patch 7 reports the new source to userspace.

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

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

Signed-off-by: Loic Poulain <loic.poulain@oss.qualcomm.com>
---
Changes in v2:
- Patch 2: read the latched counter through a new csid_hw_ops operation
  and do the conversion once in the CSID core, instead of passing the
  time into csid_sof_event() and open coding the register read in every
  CSI decoder (Bryan).
- New patch 7: report the timestamp source as V4L2_BUF_FLAG_TSTAMP_SRC_SOE
  on the SoCs that can track SOF, rather than leaving the queue
  advertising V4L2_BUF_FLAG_TSTAMP_SRC_EOF.
- Link to v1: https://lore.kernel.org/r/20261123-camss-hw-ts-v1-0-7b62dda6d7a9@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 (5):
      media: v4l2-event: Allow drivers to provide the event timestamp
      media: qcom: camss: csid: Add an operation to read the SOF timestamp
      media: qcom: camss: csid-340: Report the hardware SOF timestamp
      media: qcom: camss: Timestamp buffers at start of frame
      media: qcom: camss: Tell userspace we're using start-of-frame timestamps

 drivers/media/platform/qcom/camss/camss-csid-340.c | 37 ++++++++-
 .../media/platform/qcom/camss/camss-csid-gen2.c    | 31 +++++++
 drivers/media/platform/qcom/camss/camss-csid.c     | 94 +++++++++++++++++++++-
 drivers/media/platform/qcom/camss/camss-csid.h     | 19 +++++
 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/platform/qcom/camss/camss-video.c    |  2 +
 drivers/media/platform/qcom/camss/camss.c          | 12 +++
 drivers/media/platform/qcom/camss/camss.h          |  1 +
 drivers/media/v4l2-core/v4l2-event.c               | 20 ++++-
 include/media/v4l2-event.h                         |  9 ++-
 12 files changed, 228 insertions(+), 13 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] 17+ messages in thread

* [PATCH v2 1/7] media: v4l2-event: Allow drivers to provide the event timestamp
  2026-11-25 10:19 [PATCH v2 0/7] media: qcom: camss: Report the hardware SOF timestamp Loic Poulain
@ 2026-11-25 10:19 ` Loic Poulain
  2026-11-25 12:29   ` Gjorgji Rosikopulos (Consultant)
  2026-11-25 10:19 ` [PATCH v2 2/7] media: qcom: camss: csid: Add an operation to read the SOF timestamp Loic Poulain
                   ` (5 subsequent siblings)
  6 siblings, 1 reply; 17+ messages in thread
From: Loic Poulain @ 2026-11-25 10:19 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] 17+ messages in thread

* [PATCH v2 2/7] media: qcom: camss: csid: Add an operation to read the SOF timestamp
  2026-11-25 10:19 [PATCH v2 0/7] media: qcom: camss: Report the hardware SOF timestamp Loic Poulain
  2026-11-25 10:19 ` [PATCH v2 1/7] media: v4l2-event: Allow drivers to provide the event timestamp Loic Poulain
@ 2026-11-25 10:19 ` Loic Poulain
  2026-11-25 12:04   ` Gjorgji Rosikopulos (Consultant)
  2026-11-25 10:19 ` [PATCH v2 3/7] media: qcom: camss: csid-340: Report the hardware " Loic Poulain
                   ` (4 subsequent siblings)
  6 siblings, 1 reply; 17+ messages in thread
From: Loic Poulain @ 2026-11-25 10:19 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Bryan O'Donoghue, Vladimir Zapolskiy
  Cc: linux-media, linux-kernel, linux-arm-msm, Loic Poulain

Modern CSID revisions latch the global timestamp counter at SOF. 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.

Add a hardware operation returning that counter value and do the
correlation in one place, leaving a CSID revision with nothing to do but
read its own registers. Revisions not implementing it keep being
timestamped at queue time.

Suggested-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.c | 72 ++++++++++++++++++++++++++
 drivers/media/platform/qcom/camss/camss-csid.h |  9 ++++
 2 files changed, 81 insertions(+)

diff --git a/drivers/media/platform/qcom/camss/camss-csid.c b/drivers/media/platform/qcom/camss/camss-csid.c
index 588122beec854ae5b78de9997b68c7a81d4207e1..0d9b4f7dd768aeaed65d01da58b4dd8a3a425cbc 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. 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,12 +1383,71 @@ 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);
+}
+
+static 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 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) {
+		dev_warn_once(csid->camss->dev,
+			      "CSID%u: Can't use hardware timestamp, invalid clock source (%d)\n",
+			      csid->id, snap.cs_id);
+		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;
+}
+
+static u64 csid_hw_sof_ts(struct csid_device *csid, unsigned int id)
+{
+	const struct csid_hw_ops *hw_ops = csid->res->hw_ops;
+
+	if (!hw_ops->sof_ts)
+		return 0;
+
+	return csid_timestamp_to_monotonic(csid, hw_ops->sof_ts(csid, id));
+}
+
 void csid_sof_event(struct csid_device *csid, unsigned int id)
 {
+	u64 ts = csid_hw_sof_ts(csid, id);
+	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..17cc3b42c8d83180d6abfcc3efe22178dcc2f2e7 100644
--- a/drivers/media/platform/qcom/camss/camss-csid.h
+++ b/drivers/media/platform/qcom/camss/camss-csid.h
@@ -111,6 +111,15 @@ struct csid_hw_ops {
 	 */
 	int (*reset)(struct csid_device *csid);
 
+	/*
+	 * sof_ts - Read the counter value the hardware latched at SOF
+	 * @csid: CSID device
+	 * @id: CSID src-stream ID
+	 *
+	 * Return the raw counter value of the most recent SOF
+	 */
+	u64 (*sof_ts)(struct csid_device *csid, unsigned int id);
+
 	/*
 	 * src_pad_code - Pick an output/src format based on the input/sink format
 	 * @csid: CSID device

-- 
2.34.1


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

* [PATCH v2 3/7] media: qcom: camss: csid-340: Report the hardware SOF timestamp
  2026-11-25 10:19 [PATCH v2 0/7] media: qcom: camss: Report the hardware SOF timestamp Loic Poulain
  2026-11-25 10:19 ` [PATCH v2 1/7] media: v4l2-event: Allow drivers to provide the event timestamp Loic Poulain
  2026-11-25 10:19 ` [PATCH v2 2/7] media: qcom: camss: csid: Add an operation to read the SOF timestamp Loic Poulain
@ 2026-11-25 10:19 ` Loic Poulain
  2026-11-25 10:19 ` [PATCH v2 4/7] media: camss: Define timestamp registers for csid-gen2 Loic Poulain
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 17+ messages in thread
From: Loic Poulain @ 2026-11-25 10:19 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. Implement the sof_ts operation to
read it, so that the frame sync event carries 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 +++++++++++++++++++---
 1 file changed, 33 insertions(+), 4 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..ec4b1d9a86fcf08c2ceb12b4c4c2b85b5c7b8e51 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,21 @@ static int csid_reset(struct csid_device *csid)
 	return 0;
 }
 
+static u64 csid_sof_ts(struct csid_device *csid, unsigned int id)
+{
+	enum csid_iface iface = csid_port_iface_map[id];
+	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 ((u64)hi << 32) | lo;
+}
+
 static irqreturn_t csid_isr(int irq, void *dev)
 {
 	struct csid_device *csid = dev;
@@ -233,6 +261,7 @@ const struct csid_hw_ops csid_ops_340 = {
 	.hw_version = csid_hw_version,
 	.isr = csid_isr,
 	.reset = csid_reset,
+	.sof_ts = csid_sof_ts,
 	.src_pad_code = csid_src_pad_code,
 	.subdev_init = csid_subdev_init,
 };

-- 
2.34.1


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

* [PATCH v2 4/7] media: camss: Define timestamp registers for csid-gen2
  2026-11-25 10:19 [PATCH v2 0/7] media: qcom: camss: Report the hardware SOF timestamp Loic Poulain
                   ` (2 preceding siblings ...)
  2026-11-25 10:19 ` [PATCH v2 3/7] media: qcom: camss: csid-340: Report the hardware " Loic Poulain
@ 2026-11-25 10:19 ` Loic Poulain
  2026-11-25 10:19 ` [PATCH v2 5/7] media: camss: csid-gen2: Report the hardware SOF timestamp Loic Poulain
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 17+ messages in thread
From: Loic Poulain @ 2026-11-25 10:19 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 a351ac4b91728b981fe80e030fcb5ebfdab138b2..85e9eaf78df75f95d5f44f83b45dc7a3633401d0 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] 17+ messages in thread

* [PATCH v2 5/7] media: camss: csid-gen2: Report the hardware SOF timestamp
  2026-11-25 10:19 [PATCH v2 0/7] media: qcom: camss: Report the hardware SOF timestamp Loic Poulain
                   ` (3 preceding siblings ...)
  2026-11-25 10:19 ` [PATCH v2 4/7] media: camss: Define timestamp registers for csid-gen2 Loic Poulain
@ 2026-11-25 10:19 ` Loic Poulain
  2026-11-25 10:19 ` [PATCH v2 6/7] media: qcom: camss: Timestamp buffers at start of frame Loic Poulain
  2026-11-25 10:19 ` [PATCH v2 7/7] media: qcom: camss: Tell userspace we're using start-of-frame timestamps Loic Poulain
  6 siblings, 0 replies; 17+ messages in thread
From: Loic Poulain @ 2026-11-25 10:19 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.

Implement the sof_ts operation to read the CURRENT_SOF pair
instead, and let the CSID core put 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 | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/drivers/media/platform/qcom/camss/camss-csid-gen2.c b/drivers/media/platform/qcom/camss/camss-csid-gen2.c
index 85e9eaf78df75f95d5f44f83b45dc7a3633401d0..47b0881e9c9942eeeaef53d8aa6b466ca21a366a 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_ts(struct csid_device *csid, unsigned int 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 ((u64)hi << 32) | lo;
+}
+
 /*
  * csid_isr - CSID module interrupt service routine
  * @irq: Interrupt line
@@ -459,6 +473,7 @@ const struct csid_hw_ops csid_ops_gen2 = {
 	.hw_version = csid_hw_version,
 	.isr = csid_isr,
 	.reset = csid_reset,
+	.sof_ts = csid_sof_ts,
 	.src_pad_code = csid_src_pad_code,
 	.subdev_init = csid_subdev_init,
 };

-- 
2.34.1


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

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

Buffers are stamped from the write master done interrupt, once the frame
has been written out to memory, while V4L2 defines vb2_buf.timestamp as
the time the first data byte was captured. Use the SOF time the CSID
already computes instead, handed to the VFE the same way as the SOF
sequence number. Hardware without SOF tracking keeps using the buf done
time.

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 0d9b4f7dd768aeaed65d01da58b4dd8a3a425cbc..4ac2aa588bfb47332c13baf4a16c7f613177b9ff 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);
@@ -1450,6 +1466,8 @@ void csid_sof_event(struct csid_device *csid, unsigned int id)
 		.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 17cc3b42c8d83180d6abfcc3efe22178dcc2f2e7..f7c0935ea25cee5ac87cca470803a7391c12f9f0 100644
--- a/drivers/media/platform/qcom/camss/camss-csid.h
+++ b/drivers/media/platform/qcom/camss/camss-csid.h
@@ -188,6 +188,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;
@@ -288,4 +289,13 @@ void csid_sof_event(struct csid_device *csid, unsigned int id);
  */
 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] 17+ messages in thread

* [PATCH v2 7/7] media: qcom: camss: Tell userspace we're using start-of-frame timestamps
  2026-11-25 10:19 [PATCH v2 0/7] media: qcom: camss: Report the hardware SOF timestamp Loic Poulain
                   ` (5 preceding siblings ...)
  2026-11-25 10:19 ` [PATCH v2 6/7] media: qcom: camss: Timestamp buffers at start of frame Loic Poulain
@ 2026-11-25 10:19 ` Loic Poulain
  6 siblings, 0 replies; 17+ messages in thread
From: Loic Poulain @ 2026-11-25 10:19 UTC (permalink / raw)
  To: Mauro Carvalho Chehab, Bryan O'Donoghue, Vladimir Zapolskiy
  Cc: linux-media, linux-kernel, linux-arm-msm, Loic Poulain

On the SoCs whose CSID can track the start of frame, the buffer
timestamp is taken when the reception of the frame begins, not once it
has been written out to memory. Show this to userspace with the
V4L2_BUF_FLAG_TSTAMP_SRC_SOE buffer flag, the closest the API has to
offer, it is defined as the start of the exposure, which on a rolling
shutter sensor precedes the start of the readout by the exposure time
of the first row.

Signed-off-by: Loic Poulain <loic.poulain@oss.qualcomm.com>
---
 drivers/media/platform/qcom/camss/camss-video.c |  2 ++
 drivers/media/platform/qcom/camss/camss.c       | 12 ++++++++++++
 drivers/media/platform/qcom/camss/camss.h       |  1 +
 3 files changed, 15 insertions(+)

diff --git a/drivers/media/platform/qcom/camss/camss-video.c b/drivers/media/platform/qcom/camss/camss-video.c
index 5de6ff5ecc76a2863e536fca1c40ae6b2de6dc0c..4ce87527b9b984068f5c9a9cc3bbdca645cff1d3 100644
--- a/drivers/media/platform/qcom/camss/camss-video.c
+++ b/drivers/media/platform/qcom/camss/camss-video.c
@@ -698,6 +698,8 @@ int msm_video_register(struct camss_video *video, struct v4l2_device *v4l2_dev,
 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
 	q->io_modes = VB2_DMABUF | VB2_MMAP | VB2_READ;
 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+	if (camss_has_frame_sync(video->camss))
+		q->timestamp_flags |= V4L2_BUF_FLAG_TSTAMP_SRC_SOE;
 	q->buf_struct_size = sizeof(struct camss_buffer);
 	q->dev = video->camss->dev;
 	q->lock = &video->q_lock;
diff --git a/drivers/media/platform/qcom/camss/camss.c b/drivers/media/platform/qcom/camss/camss.c
index 26e3f2aa3ef2485e0486eaa89f93b804926b6e32..ac99e84b94e73152b82050e6c6727dec238e188b 100644
--- a/drivers/media/platform/qcom/camss/camss.c
+++ b/drivers/media/platform/qcom/camss/camss.c
@@ -4839,6 +4839,18 @@ int camss_get_pixel_clock(struct media_entity *entity, u64 *pixel_clock)
 	return 0;
 }
 
+/*
+ * camss_has_frame_sync - Check if the CSIDs can report the start of frame
+ * @camss: CAMSS device
+ *
+ * Return whether SOF tracking is available
+ */
+bool camss_has_frame_sync(struct camss *camss)
+{
+	/* All the CSIDs of a given SoC have the same capability */
+	return camss->res->csid_res[0].csid.frame_sync_en;
+}
+
 int camss_pm_domain_on(struct camss *camss, int id)
 {
 	int ret = 0;
diff --git a/drivers/media/platform/qcom/camss/camss.h b/drivers/media/platform/qcom/camss/camss.h
index 8154fab54f99b12456cb377dc4ddfcadfcf8278e..da1095c436323639bc0fda5ebac8026a09565108 100644
--- a/drivers/media/platform/qcom/camss/camss.h
+++ b/drivers/media/platform/qcom/camss/camss.h
@@ -177,6 +177,7 @@ struct media_pad *camss_find_sensor_pad(struct media_entity *entity);
 s64 camss_get_link_freq(struct media_entity *entity, unsigned int bpp,
 			unsigned int lanes);
 int camss_get_pixel_clock(struct media_entity *entity, u64 *pixel_clock);
+bool camss_has_frame_sync(struct camss *camss);
 int camss_pm_domain_on(struct camss *camss, int id);
 void camss_pm_domain_off(struct camss *camss, int id);
 int camss_vfe_get(struct camss *camss, int id);

-- 
2.34.1


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

* Re: [PATCH v2 2/7] media: qcom: camss: csid: Add an operation to read the SOF timestamp
  2026-11-25 10:19 ` [PATCH v2 2/7] media: qcom: camss: csid: Add an operation to read the SOF timestamp Loic Poulain
@ 2026-11-25 12:04   ` Gjorgji Rosikopulos (Consultant)
  2026-11-25 12:06     ` Gjorgji Rosikopulos (Consultant)
  0 siblings, 1 reply; 17+ messages in thread
From: Gjorgji Rosikopulos (Consultant) @ 2026-11-25 12:04 UTC (permalink / raw)
  To: Loic Poulain, Mauro Carvalho Chehab, Bryan O'Donoghue,
	Vladimir Zapolskiy
  Cc: linux-media, linux-kernel, linux-arm-msm

Hi Loic,

On 11/25/2026 12:19 PM, Loic Poulain wrote:
> Modern CSID revisions latch the global timestamp counter at SOF. 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.
> 
> Add a hardware operation returning that counter value and do the
> correlation in one place, leaving a CSID revision with nothing to do but
> read its own registers. Revisions not implementing it keep being
> timestamped at queue time.
> 
> Suggested-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.c | 72 ++++++++++++++++++++++++++
>  drivers/media/platform/qcom/camss/camss-csid.h |  9 ++++
>  2 files changed, 81 insertions(+)
> 
> diff --git a/drivers/media/platform/qcom/camss/camss-csid.c b/drivers/media/platform/qcom/camss/camss-csid.c
> index 588122beec854ae5b78de9997b68c7a81d4207e1..0d9b4f7dd768aeaed65d01da58b4dd8a3a425cbc 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. 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,12 +1383,71 @@ 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);
> +}
> +
> +static 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 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) {
> +		dev_warn_once(csid->camss->dev,
> +			      "CSID%u: Can't use hardware timestamp, invalid clock source (%d)\n",
> +			      csid->id, snap.cs_id);
> +		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;
> +}
> +
> +static u64 csid_hw_sof_ts(struct csid_device *csid, unsigned int id)
> +{
> +	const struct csid_hw_ops *hw_ops = csid->res->hw_ops;
> +
> +	if (!hw_ops->sof_ts)
> +		return 0;
> +
> +	return csid_timestamp_to_monotonic(csid, hw_ops->sof_ts(csid, id));

I am not sure if we need to do this every SOF. Most probably the delta will
remain the same during the streaming, so I think it's best to use the delta within
tolerance and then apply that for the whole streaming session. Also, I think it's better
to have a constant delta from the monotonic clock rather than a fluctuating one.

~Gjorgji

> +}
> +
>  void csid_sof_event(struct csid_device *csid, unsigned int id)
>  {
> +	u64 ts = csid_hw_sof_ts(csid, id);
> +	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..17cc3b42c8d83180d6abfcc3efe22178dcc2f2e7 100644
> --- a/drivers/media/platform/qcom/camss/camss-csid.h
> +++ b/drivers/media/platform/qcom/camss/camss-csid.h
> @@ -111,6 +111,15 @@ struct csid_hw_ops {
>  	 */
>  	int (*reset)(struct csid_device *csid);
>  
> +	/*
> +	 * sof_ts - Read the counter value the hardware latched at SOF
> +	 * @csid: CSID device
> +	 * @id: CSID src-stream ID
> +	 *
> +	 * Return the raw counter value of the most recent SOF
> +	 */
> +	u64 (*sof_ts)(struct csid_device *csid, unsigned int id);
> +
>  	/*
>  	 * src_pad_code - Pick an output/src format based on the input/sink format
>  	 * @csid: CSID device
> 


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

* Re: [PATCH v2 2/7] media: qcom: camss: csid: Add an operation to read the SOF timestamp
  2026-11-25 12:04   ` Gjorgji Rosikopulos (Consultant)
@ 2026-11-25 12:06     ` Gjorgji Rosikopulos (Consultant)
  2026-11-25 13:18       ` Loic Poulain
  0 siblings, 1 reply; 17+ messages in thread
From: Gjorgji Rosikopulos (Consultant) @ 2026-11-25 12:06 UTC (permalink / raw)
  To: Loic Poulain, Mauro Carvalho Chehab, Bryan O'Donoghue,
	Vladimir Zapolskiy
  Cc: linux-media, linux-kernel, linux-arm-msm



On 11/25/2026 2:04 PM, Gjorgji Rosikopulos (Consultant) wrote:
> Hi Loic,
> 
> On 11/25/2026 12:19 PM, Loic Poulain wrote:
>> Modern CSID revisions latch the global timestamp counter at SOF. 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.
>>
>> Add a hardware operation returning that counter value and do the
>> correlation in one place, leaving a CSID revision with nothing to do but
>> read its own registers. Revisions not implementing it keep being
>> timestamped at queue time.
>>
>> Suggested-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.c | 72 ++++++++++++++++++++++++++
>>  drivers/media/platform/qcom/camss/camss-csid.h |  9 ++++
>>  2 files changed, 81 insertions(+)
>>
>> diff --git a/drivers/media/platform/qcom/camss/camss-csid.c b/drivers/media/platform/qcom/camss/camss-csid.c
>> index 588122beec854ae5b78de9997b68c7a81d4207e1..0d9b4f7dd768aeaed65d01da58b4dd8a3a425cbc 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. 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,12 +1383,71 @@ 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);
>> +}
>> +
>> +static 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 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) {
>> +		dev_warn_once(csid->camss->dev,
>> +			      "CSID%u: Can't use hardware timestamp, invalid clock source (%d)\n",
>> +			      csid->id, snap.cs_id);
>> +		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;
>> +}
>> +
>> +static u64 csid_hw_sof_ts(struct csid_device *csid, unsigned int id)
>> +{
>> +	const struct csid_hw_ops *hw_ops = csid->res->hw_ops;
>> +
>> +	if (!hw_ops->sof_ts)
>> +		return 0;
>> +
>> +	return csid_timestamp_to_monotonic(csid, hw_ops->sof_ts(csid, id));
> 
> I am not sure if we need to do this every SOF. Most probably the delta will
> remain the same during the streaming, so I think it's best to use the delta within
> tolerance and then apply that for the whole streaming session. Also, I think it's better
> to have a constant delta from the monotonic clock rather than a fluctuating one.

I think it will be the best to send both timestamps to userspace, and give the freedom
to userspace to decide by him self. But the API is not allowing that as discussed.

> 
> ~Gjorgji
> 
>> +}
>> +
>>  void csid_sof_event(struct csid_device *csid, unsigned int id)
>>  {
>> +	u64 ts = csid_hw_sof_ts(csid, id);
>> +	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..17cc3b42c8d83180d6abfcc3efe22178dcc2f2e7 100644
>> --- a/drivers/media/platform/qcom/camss/camss-csid.h
>> +++ b/drivers/media/platform/qcom/camss/camss-csid.h
>> @@ -111,6 +111,15 @@ struct csid_hw_ops {
>>  	 */
>>  	int (*reset)(struct csid_device *csid);
>>  
>> +	/*
>> +	 * sof_ts - Read the counter value the hardware latched at SOF
>> +	 * @csid: CSID device
>> +	 * @id: CSID src-stream ID
>> +	 *
>> +	 * Return the raw counter value of the most recent SOF
>> +	 */
>> +	u64 (*sof_ts)(struct csid_device *csid, unsigned int id);
>> +
>>  	/*
>>  	 * src_pad_code - Pick an output/src format based on the input/sink format
>>  	 * @csid: CSID device
>>
> 


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

* Re: [PATCH v2 1/7] media: v4l2-event: Allow drivers to provide the event timestamp
  2026-11-25 10:19 ` [PATCH v2 1/7] media: v4l2-event: Allow drivers to provide the event timestamp Loic Poulain
@ 2026-11-25 12:29   ` Gjorgji Rosikopulos (Consultant)
  2026-11-25 12:58     ` Vladimir Zapolskiy
  2026-11-25 13:21     ` Loic Poulain
  0 siblings, 2 replies; 17+ messages in thread
From: Gjorgji Rosikopulos (Consultant) @ 2026-11-25 12:29 UTC (permalink / raw)
  To: Loic Poulain, Mauro Carvalho Chehab, Bryan O'Donoghue,
	Vladimir Zapolskiy
  Cc: linux-media, linux-kernel, linux-arm-msm

Hello,

On 11/25/2026 12:19 PM, Loic Poulain wrote:
> 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();
> +}

I think it makes sense to add an additional field for hardware-based timestamps.
Converting the timestamp to the monotonic time domain introduces precision loss,
so it's better to expose both to userspace and let userspace decide what to do with
them.

~Gjorgji

> +
>  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);
>  
> 


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

* Re: [PATCH v2 1/7] media: v4l2-event: Allow drivers to provide the event timestamp
  2026-11-25 12:29   ` Gjorgji Rosikopulos (Consultant)
@ 2026-11-25 12:58     ` Vladimir Zapolskiy
  2026-11-25 13:21     ` Loic Poulain
  1 sibling, 0 replies; 17+ messages in thread
From: Vladimir Zapolskiy @ 2026-11-25 12:58 UTC (permalink / raw)
  To: Gjorgji Rosikopulos (Consultant),
	Loic Poulain, Mauro Carvalho Chehab, Bryan O'Donoghue
  Cc: linux-media, linux-kernel, linux-arm-msm

On 11/25/26 14:29, Gjorgji Rosikopulos (Consultant) wrote:
> Hello,
> 
> On 11/25/2026 12:19 PM, Loic Poulain wrote:
>> 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();
>> +}
> 
> I think it makes sense to add an additional field for hardware-based timestamps.
> Converting the timestamp to the monotonic time domain introduces precision loss,
> so it's better to expose both to userspace and let userspace decide what to do with
> them.
> 

I support this.

-- 
Best wishes,
Vladimir

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

* Re: [PATCH v2 2/7] media: qcom: camss: csid: Add an operation to read the SOF timestamp
  2026-11-25 12:06     ` Gjorgji Rosikopulos (Consultant)
@ 2026-11-25 13:18       ` Loic Poulain
  2026-11-25 15:01         ` Gjorgji Rosikopulos (Consultant)
  0 siblings, 1 reply; 17+ messages in thread
From: Loic Poulain @ 2026-11-25 13:18 UTC (permalink / raw)
  To: Gjorgji Rosikopulos (Consultant)
  Cc: Mauro Carvalho Chehab, Bryan O'Donoghue, Vladimir Zapolskiy,
	linux-media, linux-kernel, linux-arm-msm

On Wed, Nov 25, 2026 at 1:06 PM Gjorgji Rosikopulos (Consultant)
<gjorgji.rosikopulos@oss.qualcomm.com> wrote:
>
>
>
> On 11/25/2026 2:04 PM, Gjorgji Rosikopulos (Consultant) wrote:
> > Hi Loic,
> >
> > On 11/25/2026 12:19 PM, Loic Poulain wrote:
> >> Modern CSID revisions latch the global timestamp counter at SOF. 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.
> >>
> >> Add a hardware operation returning that counter value and do the
> >> correlation in one place, leaving a CSID revision with nothing to do but
> >> read its own registers. Revisions not implementing it keep being
> >> timestamped at queue time.
> >>
> >> Suggested-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.c | 72 ++++++++++++++++++++++++++
> >>  drivers/media/platform/qcom/camss/camss-csid.h |  9 ++++
> >>  2 files changed, 81 insertions(+)
> >>
> >> diff --git a/drivers/media/platform/qcom/camss/camss-csid.c b/drivers/media/platform/qcom/camss/camss-csid.c
> >> index 588122beec854ae5b78de9997b68c7a81d4207e1..0d9b4f7dd768aeaed65d01da58b4dd8a3a425cbc 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. 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,12 +1383,71 @@ 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);
> >> +}
> >> +
> >> +static 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 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) {
> >> +            dev_warn_once(csid->camss->dev,
> >> +                          "CSID%u: Can't use hardware timestamp, invalid clock source (%d)\n",
> >> +                          csid->id, snap.cs_id);
> >> +            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;
> >> +}
> >> +
> >> +static u64 csid_hw_sof_ts(struct csid_device *csid, unsigned int id)
> >> +{
> >> +    const struct csid_hw_ops *hw_ops = csid->res->hw_ops;
> >> +
> >> +    if (!hw_ops->sof_ts)
> >> +            return 0;
> >> +
> >> +    return csid_timestamp_to_monotonic(csid, hw_ops->sof_ts(csid, id));
> >
> > I am not sure if we need to do this every SOF. Most probably the delta will
> > remain the same during the streaming, so I think it's best to use the delta within
> > tolerance and then apply that for the whole streaming session. Also, I think it's better
> > to have a constant delta from the monotonic clock rather than a fluctuating one.
>
> I think it will be the best to send both timestamps to userspace, and give the freedom
> to userspace to decide by him self. But the API is not allowing that as discussed.

No, the delta is not constant. In fact, that's precisely why we use
it, it captures the variable CPU and software latencies between the
hardware event and the timestamp acquisition. By combining the
captured hardware timestamp with the current system timing
information, we can reconstruct an accurate event timestamp that
remains aligned with the Sensor/CSI timing, rather than being affected
by software scheduling and processing delays. According to my testing,
the resulting timing error is below 1 µs, from which we can derive an
accurate sensor framerate.

--
watching /dev/v4l-subdev2 (msm_csid0), id 0
id0    478 ev   60.06 fps   16.650 ms
  events           : 478
  timestamp (mono) : 180.160315690 .. 188.102256050
  duration         : 7.942 s
  frame rate       : 60.061 fps
  frame_sequence   : 539 .. 1016 (missing: 0)
  interval (ms)    : mean 16.650  median 16.650  min 16.650  max 16.650
                     p95 16.650  p99 16.650
---

Regards,
Loic

>
> >
> > ~Gjorgji
> >
> >> +}
> >> +
> >>  void csid_sof_event(struct csid_device *csid, unsigned int id)
> >>  {
> >> +    u64 ts = csid_hw_sof_ts(csid, id);
> >> +    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..17cc3b42c8d83180d6abfcc3efe22178dcc2f2e7 100644
> >> --- a/drivers/media/platform/qcom/camss/camss-csid.h
> >> +++ b/drivers/media/platform/qcom/camss/camss-csid.h
> >> @@ -111,6 +111,15 @@ struct csid_hw_ops {
> >>       */
> >>      int (*reset)(struct csid_device *csid);
> >>
> >> +    /*
> >> +     * sof_ts - Read the counter value the hardware latched at SOF
> >> +     * @csid: CSID device
> >> +     * @id: CSID src-stream ID
> >> +     *
> >> +     * Return the raw counter value of the most recent SOF
> >> +     */
> >> +    u64 (*sof_ts)(struct csid_device *csid, unsigned int id);
> >> +
> >>      /*
> >>       * src_pad_code - Pick an output/src format based on the input/sink format
> >>       * @csid: CSID device
> >>
> >
>

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

* Re: [PATCH v2 1/7] media: v4l2-event: Allow drivers to provide the event timestamp
  2026-11-25 12:29   ` Gjorgji Rosikopulos (Consultant)
  2026-11-25 12:58     ` Vladimir Zapolskiy
@ 2026-11-25 13:21     ` Loic Poulain
  1 sibling, 0 replies; 17+ messages in thread
From: Loic Poulain @ 2026-11-25 13:21 UTC (permalink / raw)
  To: Gjorgji Rosikopulos (Consultant)
  Cc: Mauro Carvalho Chehab, Bryan O'Donoghue, Vladimir Zapolskiy,
	linux-media, linux-kernel, linux-arm-msm

Hi Gjorgji,

On Wed, Nov 25, 2026 at 1:29 PM Gjorgji Rosikopulos (Consultant)
<gjorgji.rosikopulos@oss.qualcomm.com> wrote:
>
> Hello,
>
> On 11/25/2026 12:19 PM, Loic Poulain wrote:
> > 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();
> > +}
>
> I think it makes sense to add an additional field for hardware-based timestamps.
> Converting the timestamp to the monotonic time domain introduces precision loss,
> so it's better to expose both to userspace and let userspace decide what to do with
> them.

There is no loss of precision. The timestamp is actually more tightly
aligned with the actual event. So I don't see the point of having two
values, with one being less precise than the other.

>
> > +
> >  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);
> >
> >
>

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

* Re: [PATCH v2 2/7] media: qcom: camss: csid: Add an operation to read the SOF timestamp
  2026-11-25 13:18       ` Loic Poulain
@ 2026-11-25 15:01         ` Gjorgji Rosikopulos (Consultant)
  2026-11-25 16:21           ` Loic Poulain
  0 siblings, 1 reply; 17+ messages in thread
From: Gjorgji Rosikopulos (Consultant) @ 2026-11-25 15:01 UTC (permalink / raw)
  To: Loic Poulain
  Cc: Mauro Carvalho Chehab, Bryan O'Donoghue, Vladimir Zapolskiy,
	linux-media, linux-kernel, linux-arm-msm



On 11/25/2026 3:18 PM, Loic Poulain wrote:
> On Wed, Nov 25, 2026 at 1:06 PM Gjorgji Rosikopulos (Consultant)
> <gjorgji.rosikopulos@oss.qualcomm.com> wrote:
>>
>>
>>
>> On 11/25/2026 2:04 PM, Gjorgji Rosikopulos (Consultant) wrote:
>>> Hi Loic,
>>>
>>> On 11/25/2026 12:19 PM, Loic Poulain wrote:
>>>> Modern CSID revisions latch the global timestamp counter at SOF. 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.
>>>>
>>>> Add a hardware operation returning that counter value and do the
>>>> correlation in one place, leaving a CSID revision with nothing to do but
>>>> read its own registers. Revisions not implementing it keep being
>>>> timestamped at queue time.
>>>>
>>>> Suggested-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.c | 72 ++++++++++++++++++++++++++
>>>>  drivers/media/platform/qcom/camss/camss-csid.h |  9 ++++
>>>>  2 files changed, 81 insertions(+)
>>>>
>>>> diff --git a/drivers/media/platform/qcom/camss/camss-csid.c b/drivers/media/platform/qcom/camss/camss-csid.c
>>>> index 588122beec854ae5b78de9997b68c7a81d4207e1..0d9b4f7dd768aeaed65d01da58b4dd8a3a425cbc 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. 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,12 +1383,71 @@ 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);
>>>> +}
>>>> +
>>>> +static 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 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) {
>>>> +            dev_warn_once(csid->camss->dev,
>>>> +                          "CSID%u: Can't use hardware timestamp, invalid clock source (%d)\n",
>>>> +                          csid->id, snap.cs_id);
>>>> +            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;
>>>> +}
>>>> +
>>>> +static u64 csid_hw_sof_ts(struct csid_device *csid, unsigned int id)
>>>> +{
>>>> +    const struct csid_hw_ops *hw_ops = csid->res->hw_ops;
>>>> +
>>>> +    if (!hw_ops->sof_ts)
>>>> +            return 0;
>>>> +
>>>> +    return csid_timestamp_to_monotonic(csid, hw_ops->sof_ts(csid, id));
>>>
>>> I am not sure if we need to do this every SOF. Most probably the delta will
>>> remain the same during the streaming, so I think it's best to use the delta within
>>> tolerance and then apply that for the whole streaming session. Also, I think it's better
>>> to have a constant delta from the monotonic clock rather than a fluctuating one.
>>
>> I think it will be the best to send both timestamps to userspace, and give the freedom
>> to userspace to decide by him self. But the API is not allowing that as discussed.
> 
> No, the delta is not constant. In fact, that's precisely why we use
> it, it captures the variable CPU and software latencies between the
> hardware event and the timestamp acquisition. By combining the
> captured hardware timestamp with the current system timing
> information, we can reconstruct an accurate event timestamp that
> remains aligned with the Sensor/CSI timing, rather than being affected
> by software scheduling and processing delays. According to my testing,
> the resulting timing error is below 1 µs, from which we can derive an
> accurate sensor framerate.
> 
> --
> watching /dev/v4l-subdev2 (msm_csid0), id 0
> id0    478 ev   60.06 fps   16.650 ms
>   events           : 478
>   timestamp (mono) : 180.160315690 .. 188.102256050
>   duration         : 7.942 s
>   frame rate       : 60.061 fps
>   frame_sequence   : 539 .. 1016 (missing: 0)
>   interval (ms)    : mean 16.650  median 16.650  min 16.650  max 16.650
>                      p95 16.650  p99 16.650
> ---

Ok i will test the changes on my side. Just for other people we have conflicting names
CSID (stands from clock fw perspective to Clock Source ID, and from camss for the
csi decoder). If you are sure that the for csi decoder the CSID_ARM_ARCH_COUNTER
is always the clock source this will work i agree.
But if that is not the case and we end up with big delta what is the idea to do 
that every frame. It is kind of confusing to me at least. Also i think we need
to check for the other platforms if the clock source is always the same.

~Gjorgji

> 
> Regards,
> Loic
> 
>>
>>>
>>> ~Gjorgji
>>>
>>>> +}
>>>> +
>>>>  void csid_sof_event(struct csid_device *csid, unsigned int id)
>>>>  {
>>>> +    u64 ts = csid_hw_sof_ts(csid, id);
>>>> +    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..17cc3b42c8d83180d6abfcc3efe22178dcc2f2e7 100644
>>>> --- a/drivers/media/platform/qcom/camss/camss-csid.h
>>>> +++ b/drivers/media/platform/qcom/camss/camss-csid.h
>>>> @@ -111,6 +111,15 @@ struct csid_hw_ops {
>>>>       */
>>>>      int (*reset)(struct csid_device *csid);
>>>>
>>>> +    /*
>>>> +     * sof_ts - Read the counter value the hardware latched at SOF
>>>> +     * @csid: CSID device
>>>> +     * @id: CSID src-stream ID
>>>> +     *
>>>> +     * Return the raw counter value of the most recent SOF
>>>> +     */
>>>> +    u64 (*sof_ts)(struct csid_device *csid, unsigned int id);
>>>> +
>>>>      /*
>>>>       * src_pad_code - Pick an output/src format based on the input/sink format
>>>>       * @csid: CSID device
>>>>
>>>
>>


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

* Re: [PATCH v2 2/7] media: qcom: camss: csid: Add an operation to read the SOF timestamp
  2026-11-25 15:01         ` Gjorgji Rosikopulos (Consultant)
@ 2026-11-25 16:21           ` Loic Poulain
  2026-11-25 17:23             ` Gjorgji Rosikopulos (Consultant)
  0 siblings, 1 reply; 17+ messages in thread
From: Loic Poulain @ 2026-11-25 16:21 UTC (permalink / raw)
  To: Gjorgji Rosikopulos (Consultant)
  Cc: Mauro Carvalho Chehab, Bryan O'Donoghue, Vladimir Zapolskiy,
	linux-media, linux-kernel, linux-arm-msm

On Wed, Nov 25, 2026 at 4:01 PM Gjorgji Rosikopulos (Consultant)
<gjorgji.rosikopulos@oss.qualcomm.com> wrote:
>
>
>
> On 11/25/2026 3:18 PM, Loic Poulain wrote:
> > On Wed, Nov 25, 2026 at 1:06 PM Gjorgji Rosikopulos (Consultant)
> > <gjorgji.rosikopulos@oss.qualcomm.com> wrote:
> >>
> >>
> >>
> >> On 11/25/2026 2:04 PM, Gjorgji Rosikopulos (Consultant) wrote:
> >>> Hi Loic,
> >>>
> >>> On 11/25/2026 12:19 PM, Loic Poulain wrote:
> >>>> Modern CSID revisions latch the global timestamp counter at SOF. 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.
> >>>>
> >>>> Add a hardware operation returning that counter value and do the
> >>>> correlation in one place, leaving a CSID revision with nothing to do but
> >>>> read its own registers. Revisions not implementing it keep being
> >>>> timestamped at queue time.
> >>>>
> >>>> Suggested-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.c | 72 ++++++++++++++++++++++++++
> >>>>  drivers/media/platform/qcom/camss/camss-csid.h |  9 ++++
> >>>>  2 files changed, 81 insertions(+)
> >>>>
> >>>> diff --git a/drivers/media/platform/qcom/camss/camss-csid.c b/drivers/media/platform/qcom/camss/camss-csid.c
> >>>> index 588122beec854ae5b78de9997b68c7a81d4207e1..0d9b4f7dd768aeaed65d01da58b4dd8a3a425cbc 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. 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,12 +1383,71 @@ 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);
> >>>> +}
> >>>> +
> >>>> +static 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 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) {
> >>>> +            dev_warn_once(csid->camss->dev,
> >>>> +                          "CSID%u: Can't use hardware timestamp, invalid clock source (%d)\n",
> >>>> +                          csid->id, snap.cs_id);
> >>>> +            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;
> >>>> +}
> >>>> +
> >>>> +static u64 csid_hw_sof_ts(struct csid_device *csid, unsigned int id)
> >>>> +{
> >>>> +    const struct csid_hw_ops *hw_ops = csid->res->hw_ops;
> >>>> +
> >>>> +    if (!hw_ops->sof_ts)
> >>>> +            return 0;
> >>>> +
> >>>> +    return csid_timestamp_to_monotonic(csid, hw_ops->sof_ts(csid, id));
> >>>
> >>> I am not sure if we need to do this every SOF. Most probably the delta will
> >>> remain the same during the streaming, so I think it's best to use the delta within
> >>> tolerance and then apply that for the whole streaming session. Also, I think it's better
> >>> to have a constant delta from the monotonic clock rather than a fluctuating one.
> >>
> >> I think it will be the best to send both timestamps to userspace, and give the freedom
> >> to userspace to decide by him self. But the API is not allowing that as discussed.
> >
> > No, the delta is not constant. In fact, that's precisely why we use
> > it, it captures the variable CPU and software latencies between the
> > hardware event and the timestamp acquisition. By combining the
> > captured hardware timestamp with the current system timing
> > information, we can reconstruct an accurate event timestamp that
> > remains aligned with the Sensor/CSI timing, rather than being affected
> > by software scheduling and processing delays. According to my testing,
> > the resulting timing error is below 1 µs, from which we can derive an
> > accurate sensor framerate.
> >
> > --
> > watching /dev/v4l-subdev2 (msm_csid0), id 0
> > id0    478 ev   60.06 fps   16.650 ms
> >   events           : 478
> >   timestamp (mono) : 180.160315690 .. 188.102256050
> >   duration         : 7.942 s
> >   frame rate       : 60.061 fps
> >   frame_sequence   : 539 .. 1016 (missing: 0)
> >   interval (ms)    : mean 16.650  median 16.650  min 16.650  max 16.650
> >                      p95 16.650  p99 16.650
> > ---
>
> Ok i will test the changes on my side. Just for other people we have conflicting names
> CSID (stands from clock fw perspective to Clock Source ID, and from camss for the
> csi decoder). If you are sure that the for csi decoder the CSID_ARM_ARCH_COUNTER
> is always the clock source this will work i agree.
> But if that is not the case and we end up with big delta what is the idea to do
> that every frame. It is kind of confusing to me at least. Also i think we need
> to check for the other platforms if the clock source is always the same.

Yes, the camera hardware always snapshots the architecture counter. If
we detect that the monotonic clock is not derived from the same timer,
we simply fall back to the traditional IRQ timestamping path. The same
fallback is used if the computed delta is found to be inconsistent.

When Linux receives the interrupt, we know:
- The counter value now (C_NOW)
- The Linux timestamp now (T_NOW)
- The counter value captured by hardware when the frame started (C_SOF)

Since the Linux monotonic clock and the camera counter both come from
the same hardware timer, we can convert the difference between C_NOW
and C_SOF into time (T()) and "rewind" from T_NOW back to the
**exact** frame start time:

T_SOF = T_NOW - T(C_NOW - C_SOF)

Regards,
Loic

>
> ~Gjorgji
>
> >
> > Regards,
> > Loic
> >
> >>
> >>>
> >>> ~Gjorgji
> >>>
> >>>> +}
> >>>> +
> >>>>  void csid_sof_event(struct csid_device *csid, unsigned int id)
> >>>>  {
> >>>> +    u64 ts = csid_hw_sof_ts(csid, id);
> >>>> +    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..17cc3b42c8d83180d6abfcc3efe22178dcc2f2e7 100644
> >>>> --- a/drivers/media/platform/qcom/camss/camss-csid.h
> >>>> +++ b/drivers/media/platform/qcom/camss/camss-csid.h
> >>>> @@ -111,6 +111,15 @@ struct csid_hw_ops {
> >>>>       */
> >>>>      int (*reset)(struct csid_device *csid);
> >>>>
> >>>> +    /*
> >>>> +     * sof_ts - Read the counter value the hardware latched at SOF
> >>>> +     * @csid: CSID device
> >>>> +     * @id: CSID src-stream ID
> >>>> +     *
> >>>> +     * Return the raw counter value of the most recent SOF
> >>>> +     */
> >>>> +    u64 (*sof_ts)(struct csid_device *csid, unsigned int id);
> >>>> +
> >>>>      /*
> >>>>       * src_pad_code - Pick an output/src format based on the input/sink format
> >>>>       * @csid: CSID device
> >>>>
> >>>
> >>
>

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

* Re: [PATCH v2 2/7] media: qcom: camss: csid: Add an operation to read the SOF timestamp
  2026-11-25 16:21           ` Loic Poulain
@ 2026-11-25 17:23             ` Gjorgji Rosikopulos (Consultant)
  0 siblings, 0 replies; 17+ messages in thread
From: Gjorgji Rosikopulos (Consultant) @ 2026-11-25 17:23 UTC (permalink / raw)
  To: Loic Poulain
  Cc: Mauro Carvalho Chehab, Bryan O'Donoghue, Vladimir Zapolskiy,
	linux-media, linux-kernel, linux-arm-msm



On 11/25/2026 6:21 PM, Loic Poulain wrote:
> On Wed, Nov 25, 2026 at 4:01 PM Gjorgji Rosikopulos (Consultant)
> <gjorgji.rosikopulos@oss.qualcomm.com> wrote:
>>
>>
>>
>> On 11/25/2026 3:18 PM, Loic Poulain wrote:
>>> On Wed, Nov 25, 2026 at 1:06 PM Gjorgji Rosikopulos (Consultant)
>>> <gjorgji.rosikopulos@oss.qualcomm.com> wrote:
>>>>
>>>>
>>>>
>>>> On 11/25/2026 2:04 PM, Gjorgji Rosikopulos (Consultant) wrote:
>>>>> Hi Loic,
>>>>>
>>>>> On 11/25/2026 12:19 PM, Loic Poulain wrote:
>>>>>> Modern CSID revisions latch the global timestamp counter at SOF. 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.
>>>>>>
>>>>>> Add a hardware operation returning that counter value and do the
>>>>>> correlation in one place, leaving a CSID revision with nothing to do but
>>>>>> read its own registers. Revisions not implementing it keep being
>>>>>> timestamped at queue time.
>>>>>>
>>>>>> Suggested-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.c | 72 ++++++++++++++++++++++++++
>>>>>>  drivers/media/platform/qcom/camss/camss-csid.h |  9 ++++
>>>>>>  2 files changed, 81 insertions(+)
>>>>>>
>>>>>> diff --git a/drivers/media/platform/qcom/camss/camss-csid.c b/drivers/media/platform/qcom/camss/camss-csid.c
>>>>>> index 588122beec854ae5b78de9997b68c7a81d4207e1..0d9b4f7dd768aeaed65d01da58b4dd8a3a425cbc 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. 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,12 +1383,71 @@ 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);
>>>>>> +}
>>>>>> +
>>>>>> +static 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 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) {
>>>>>> +            dev_warn_once(csid->camss->dev,
>>>>>> +                          "CSID%u: Can't use hardware timestamp, invalid clock source (%d)\n",
>>>>>> +                          csid->id, snap.cs_id);
>>>>>> +            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;
>>>>>> +}
>>>>>> +
>>>>>> +static u64 csid_hw_sof_ts(struct csid_device *csid, unsigned int id)
>>>>>> +{
>>>>>> +    const struct csid_hw_ops *hw_ops = csid->res->hw_ops;
>>>>>> +
>>>>>> +    if (!hw_ops->sof_ts)
>>>>>> +            return 0;
>>>>>> +
>>>>>> +    return csid_timestamp_to_monotonic(csid, hw_ops->sof_ts(csid, id));
>>>>>
>>>>> I am not sure if we need to do this every SOF. Most probably the delta will
>>>>> remain the same during the streaming, so I think it's best to use the delta within
>>>>> tolerance and then apply that for the whole streaming session. Also, I think it's better
>>>>> to have a constant delta from the monotonic clock rather than a fluctuating one.
>>>>
>>>> I think it will be the best to send both timestamps to userspace, and give the freedom
>>>> to userspace to decide by him self. But the API is not allowing that as discussed.
>>>
>>> No, the delta is not constant. In fact, that's precisely why we use
>>> it, it captures the variable CPU and software latencies between the
>>> hardware event and the timestamp acquisition. By combining the
>>> captured hardware timestamp with the current system timing
>>> information, we can reconstruct an accurate event timestamp that
>>> remains aligned with the Sensor/CSI timing, rather than being affected
>>> by software scheduling and processing delays. According to my testing,
>>> the resulting timing error is below 1 µs, from which we can derive an
>>> accurate sensor framerate.
>>>
>>> --
>>> watching /dev/v4l-subdev2 (msm_csid0), id 0
>>> id0    478 ev   60.06 fps   16.650 ms
>>>   events           : 478
>>>   timestamp (mono) : 180.160315690 .. 188.102256050
>>>   duration         : 7.942 s
>>>   frame rate       : 60.061 fps
>>>   frame_sequence   : 539 .. 1016 (missing: 0)
>>>   interval (ms)    : mean 16.650  median 16.650  min 16.650  max 16.650
>>>                      p95 16.650  p99 16.650
>>> ---
>>
>> Ok i will test the changes on my side. Just for other people we have conflicting names
>> CSID (stands from clock fw perspective to Clock Source ID, and from camss for the
>> csi decoder). If you are sure that the for csi decoder the CSID_ARM_ARCH_COUNTER
>> is always the clock source this will work i agree.
>> But if that is not the case and we end up with big delta what is the idea to do
>> that every frame. It is kind of confusing to me at least. Also i think we need
>> to check for the other platforms if the clock source is always the same.
> 
> Yes, the camera hardware always snapshots the architecture counter. If
> we detect that the monotonic clock is not derived from the same timer,
> we simply fall back to the traditional IRQ timestamping path. The same
> fallback is used if the computed delta is found to be inconsistent.

If the both clocks are from same timer there are two checks:

1. Big delta big delta something is completely wrong,
   most probably the csid timestamp has some stall data.
2. snap.cs_id != CSID_ARM_ARCH_COUNTER. If that is true do 
   we need to check on every SOF, most probably every time
   you will get the same result.

If the error can be recovered on next SOF maybe make sense
to enter this function. But i think in both cases most probably
you will end up there for every SOF, which is for each pipeline.
If all all streaming 4 times per frame.

> 
> When Linux receives the interrupt, we know:
> - The counter value now (C_NOW)
> - The Linux timestamp now (T_NOW)
> - The counter value captured by hardware when the frame started (C_SOF)
> 
> Since the Linux monotonic clock and the camera counter both come from
> the same hardware timer, we can convert the difference between C_NOW
> and C_SOF into time (T()) and "rewind" from T_NOW back to the
> **exact** frame start time:
> 
> T_SOF = T_NOW - T(C_NOW - C_SOF)
> 
> Regards,
> Loic
> 
>>
>> ~Gjorgji
>>
>>>
>>> Regards,
>>> Loic
>>>
>>>>
>>>>>
>>>>> ~Gjorgji
>>>>>
>>>>>> +}
>>>>>> +
>>>>>>  void csid_sof_event(struct csid_device *csid, unsigned int id)
>>>>>>  {
>>>>>> +    u64 ts = csid_hw_sof_ts(csid, id);
>>>>>> +    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..17cc3b42c8d83180d6abfcc3efe22178dcc2f2e7 100644
>>>>>> --- a/drivers/media/platform/qcom/camss/camss-csid.h
>>>>>> +++ b/drivers/media/platform/qcom/camss/camss-csid.h
>>>>>> @@ -111,6 +111,15 @@ struct csid_hw_ops {
>>>>>>       */
>>>>>>      int (*reset)(struct csid_device *csid);
>>>>>>
>>>>>> +    /*
>>>>>> +     * sof_ts - Read the counter value the hardware latched at SOF
>>>>>> +     * @csid: CSID device
>>>>>> +     * @id: CSID src-stream ID
>>>>>> +     *
>>>>>> +     * Return the raw counter value of the most recent SOF
>>>>>> +     */
>>>>>> +    u64 (*sof_ts)(struct csid_device *csid, unsigned int id);
>>>>>> +
>>>>>>      /*
>>>>>>       * src_pad_code - Pick an output/src format based on the input/sink format
>>>>>>       * @csid: CSID device
>>>>>>
>>>>>
>>>>
>>


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

end of thread, other threads:[~2026-11-25 17:23 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 10:19 [PATCH v2 0/7] media: qcom: camss: Report the hardware SOF timestamp Loic Poulain
2026-11-25 10:19 ` [PATCH v2 1/7] media: v4l2-event: Allow drivers to provide the event timestamp Loic Poulain
2026-11-25 12:29   ` Gjorgji Rosikopulos (Consultant)
2026-11-25 12:58     ` Vladimir Zapolskiy
2026-11-25 13:21     ` Loic Poulain
2026-11-25 10:19 ` [PATCH v2 2/7] media: qcom: camss: csid: Add an operation to read the SOF timestamp Loic Poulain
2026-11-25 12:04   ` Gjorgji Rosikopulos (Consultant)
2026-11-25 12:06     ` Gjorgji Rosikopulos (Consultant)
2026-11-25 13:18       ` Loic Poulain
2026-11-25 15:01         ` Gjorgji Rosikopulos (Consultant)
2026-11-25 16:21           ` Loic Poulain
2026-11-25 17:23             ` Gjorgji Rosikopulos (Consultant)
2026-11-25 10:19 ` [PATCH v2 3/7] media: qcom: camss: csid-340: Report the hardware " Loic Poulain
2026-11-25 10:19 ` [PATCH v2 4/7] media: camss: Define timestamp registers for csid-gen2 Loic Poulain
2026-11-25 10:19 ` [PATCH v2 5/7] media: camss: csid-gen2: Report the hardware SOF timestamp Loic Poulain
2026-11-25 10:19 ` [PATCH v2 6/7] media: qcom: camss: Timestamp buffers at start of frame Loic Poulain
2026-11-25 10:19 ` [PATCH v2 7/7] media: qcom: camss: Tell userspace we're using start-of-frame timestamps Loic Poulain

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®