[PATCH 6/6] media: qcom: camss: Timestamp buffers at start of frame
From: Loic Poulain
Date: Mon Nov 23 2026 - 12:17:28 EST
Buffers are currently stamped with ktime_get_ns() from the write master
done interrupt, i.e. once the frame has been written out to memory. V4L2
defines vb2_buf.timestamp as the time the first data byte was captured,
so the reported time is late by about a frame period, plus whatever
interrupt latency the system adds.
The CSID already computes the SOF time, from hardware where that is
available, so hand it to the VFE the same way the SOF sequence number is
handed over: the CSID stores it in a per-src-stream atomic64_t, the VFE
picks up a pointer to it when it enables the output and reads it when a
buffer completes. Hardware without SOF tracking keeps using the buf done
time.
The frame to SOF association is best effort, as it is for the sequence
number: buf done reads the most recent SOF, so if write-out of frame N
completes after SOF N+1 has been signalled, the timestamp belongs to
N+1. That is still closer to the capture time than the buf done time is.
Signed-off-by: Loic Poulain <loic.poulain@xxxxxxxxxxxxxxxx>
---
drivers/media/platform/qcom/camss/camss-csid.c | 22 ++++++++++++++++++++--
drivers/media/platform/qcom/camss/camss-csid.h | 10 ++++++++++
drivers/media/platform/qcom/camss/camss-vfe-17x.c | 7 +++++--
drivers/media/platform/qcom/camss/camss-vfe.c | 8 ++++++--
drivers/media/platform/qcom/camss/camss-vfe.h | 1 +
5 files changed, 42 insertions(+), 6 deletions(-)
diff --git a/drivers/media/platform/qcom/camss/camss-csid.c b/drivers/media/platform/qcom/camss/camss-csid.c
index c77fafdb9fe450aa3bd245144de353cd0edb44c6..fceb5055bc021bc8ac2ec8de28e1d26f7a337c5d 100644
--- a/drivers/media/platform/qcom/camss/camss-csid.c
+++ b/drivers/media/platform/qcom/camss/camss-csid.c
@@ -792,9 +792,11 @@ static int csid_set_stream(struct v4l2_subdev *sd, int enable)
}
if (csid->phy.need_vc_update) {
- /* Set to -1 as we want to do atomic_inc_return to read */
- for (i = 0; i < MSM_CSID_MAX_SRC_STREAMS; i++)
+ for (i = 0; i < MSM_CSID_MAX_SRC_STREAMS; i++) {
+ /* Set to -1 as we want to do atomic_inc_return to read */
atomic_set(&csid->sof_seq[i], -1);
+ atomic64_set(&csid->sof_ts[i], 0);
+ }
csid->res->hw_ops->configure_stream(csid, enable);
csid->phy.need_vc_update = false;
@@ -1383,6 +1385,20 @@ atomic_t *csid_get_sof_seq(struct media_pad *pad)
return &csid->sof_seq[pad->index - MSM_CSID_PAD_FIRST_SRC];
}
+atomic64_t *csid_get_sof_ts(struct media_pad *pad)
+{
+ struct csid_device *csid;
+
+ if (!pad || pad->index < MSM_CSID_PAD_FIRST_SRC)
+ return NULL;
+
+ csid = v4l2_get_subdevdata(media_entity_to_v4l2_subdev(pad->entity));
+ if (!csid->res->frame_sync_en)
+ return NULL;
+
+ return &csid->sof_ts[pad->index - MSM_CSID_PAD_FIRST_SRC];
+}
+
static u64 csid_timestamp_to_ns(u64 ticks)
{
return mul_u64_u32_div(ticks, CSID_TIMESTAMP_NS_MUL, CSID_TIMESTAMP_NS_DIV);
@@ -1435,6 +1451,8 @@ void csid_sof_event(struct csid_device *csid, unsigned int id, u64 ts)
.timestamp.tv_nsec = sof.tv_nsec,
};
+ atomic64_set(&csid->sof_ts[id], ts ? ts : ktime_get_ns());
+
if (atomic_read(&csid->frame_sync_users))
v4l2_event_queue(csid->subdev.devnode, &event);
}
diff --git a/drivers/media/platform/qcom/camss/camss-csid.h b/drivers/media/platform/qcom/camss/camss-csid.h
index 4f55c72a44ca3a6cbd29861aa5c90cbab9ed5932..50c4f96a691f2e1b7196427566b1c0cfc87da1ab 100644
--- a/drivers/media/platform/qcom/camss/camss-csid.h
+++ b/drivers/media/platform/qcom/camss/camss-csid.h
@@ -179,6 +179,7 @@ struct csid_device {
const struct csid_subdev_resources *res;
atomic_t frame_sync_users;
atomic_t sof_seq[MSM_CSID_MAX_SRC_STREAMS];
+ atomic64_t sof_ts[MSM_CSID_MAX_SRC_STREAMS];
};
struct camss_subdev_resources;
@@ -292,4 +293,13 @@ void csid_sof_event(struct csid_device *csid, unsigned int id, u64 ts);
*/
atomic_t *csid_get_sof_seq(struct media_pad *pad);
+/*
+ * csid_get_sof_ts - Get a pointer to the SOF timestamp variable.
+ * @pad: Remote pad of a consumer's sink pad
+ *
+ * Return the timestamp or NULL if @pad isn't a CSID or the CSID doesn't support
+ * SOF tracking. It reads as 0 until the first SOF of the stream.
+ */
+atomic64_t *csid_get_sof_ts(struct media_pad *pad);
+
#endif /* QC_MSM_CAMSS_CSID_H */
diff --git a/drivers/media/platform/qcom/camss/camss-vfe-17x.c b/drivers/media/platform/qcom/camss/camss-vfe-17x.c
index 4972d3ab65078387735ef2e853bf1c9c3d82baee..6b8df1f6e2a731ccd0e739edaa82221c18203f74 100644
--- a/drivers/media/platform/qcom/camss/camss-vfe-17x.c
+++ b/drivers/media/platform/qcom/camss/camss-vfe-17x.c
@@ -472,7 +472,6 @@ static void vfe_isr_wm_done(struct vfe_device *vfe, u8 wm)
struct vfe_output *output;
unsigned long flags;
u32 index;
- u64 ts = ktime_get_ns();
spin_lock_irqsave(&vfe->output_lock, flags);
@@ -490,7 +489,11 @@ static void vfe_isr_wm_done(struct vfe_device *vfe, u8 wm)
goto out_unlock;
}
- ready_buf->vb.vb2_buf.timestamp = ts;
+ if (line->sof_ts)
+ ready_buf->vb.vb2_buf.timestamp = atomic64_read(line->sof_ts);
+ else
+ ready_buf->vb.vb2_buf.timestamp = ktime_get_ns();
+
if (line->sof_seq)
ready_buf->vb.sequence = atomic_read(line->sof_seq);
else
diff --git a/drivers/media/platform/qcom/camss/camss-vfe.c b/drivers/media/platform/qcom/camss/camss-vfe.c
index f9bd2367671f6e50c6948a6405f78b8c2b03e5f7..ab4edb14ed7486724f12fbd024e0600d70f8355d 100644
--- a/drivers/media/platform/qcom/camss/camss-vfe.c
+++ b/drivers/media/platform/qcom/camss/camss-vfe.c
@@ -456,7 +456,6 @@ void vfe_buf_done(struct vfe_device *vfe, int wm)
struct vfe_output *output;
unsigned long flags;
u32 index;
- u64 ts = ktime_get_ns();
spin_lock_irqsave(&vfe->output_lock, flags);
@@ -474,7 +473,11 @@ void vfe_buf_done(struct vfe_device *vfe, int wm)
goto out_unlock;
}
- ready_buf->vb.vb2_buf.timestamp = ts;
+ if (line->sof_ts)
+ ready_buf->vb.vb2_buf.timestamp = atomic64_read(line->sof_ts);
+ else
+ ready_buf->vb.vb2_buf.timestamp = ktime_get_ns();
+
if (line->sof_seq)
ready_buf->vb.sequence = atomic_read(line->sof_seq);
else
@@ -532,6 +535,7 @@ int vfe_enable_output_v2(struct vfe_line *line)
csid_pad = media_pad_remote_pad_first(&line->pads[MSM_VFE_PAD_SINK]);
line->sof_seq = csid_get_sof_seq(csid_pad);
+ line->sof_ts = csid_get_sof_ts(csid_pad);
spin_lock_irqsave(&vfe->output_lock, flags);
diff --git a/drivers/media/platform/qcom/camss/camss-vfe.h b/drivers/media/platform/qcom/camss/camss-vfe.h
index c37526caf9a6fc052c3f4ab4215e635167f6da95..8eda4592257169d04027a9a3c45233ac937eecae 100644
--- a/drivers/media/platform/qcom/camss/camss-vfe.h
+++ b/drivers/media/platform/qcom/camss/camss-vfe.h
@@ -95,6 +95,7 @@ struct vfe_line {
const struct camss_format_info *formats;
unsigned int nformats;
atomic_t *sof_seq;
+ atomic64_t *sof_ts;
};
struct vfe_device;
--
2.34.1