Re: [PATCH v3 1/8] media: qcom: camss: csid: Add an operation to read the SOF timestamp
From: Loic Poulain
Date: Tue Dec 01 2026 - 09:20:50 EST
On Tue, Dec 1, 2026 at 12:19 PM Gjorgji Rosikopulos (Consultant)
<gjorgji.rosikopulos@xxxxxxxxxxxxxxxx> wrote:
>
> Hi Loic,
>
> On 11/27/2026 6:11 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. Keep the result for the frame being captured so
> > a consumer can pick it up the same way it already picks up the SOF
> > sequence number. Revisions not implementing the operation keep reporting
> > the time the SOF interrupt was handled.
> >
> > Suggested-by: Bryan O'Donoghue <bryan.odonoghue@xxxxxxxxxx>
> > Signed-off-by: Loic Poulain <loic.poulain@xxxxxxxxxxxxxxxx>
> > ---
> > drivers/media/platform/qcom/camss/camss-csid.c | 112 ++++++++++++++++++++++++-
> > drivers/media/platform/qcom/camss/camss-csid.h | 19 +++++
> > 2 files changed, 129 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/media/platform/qcom/camss/camss-csid.c b/drivers/media/platform/qcom/camss/camss-csid.c
> > index 588122beec854ae5b78de9997b68c7a81d4207e1..436478fe1f65642ff11ae3707df726d619801a8d 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)
>
> I am not sure if we need to move this in to per csid hw implementation. But as of today
> all of the hw use the same timer/precission i think i am ok with this as well. Just do
> add note.
>
> > +
> > #define MSM_CSID_NAME "msm_csid"
> >
> > const char * const csid_testgen_modes[] = {
> > @@ -779,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;
> > @@ -1370,14 +1385,107 @@ 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];
>
> In the multistream patches i have added helper pad to port which does this
> conversino maybe can be taken as part of this series since i belive
> will be taken before multistream implementation.
Ok I will look at this.
>
> > +}
> > +
> > +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)
>
> Some comment of the function will be good to mention that if arch counter is not
> available it will return just the monotonic timestamp.
Ack.
>
> > +{
> > + struct system_time_snapshot snap;
> > + u64 delta, delta_ns, now;
> > +
> > + ktime_get_snapshot_id(CLOCK_MONOTONIC, &snap);
> > +
> > + if (!snap.valid) {
> > + dev_warn_once(csid->camss->dev,
> > + "CSID%u: Can't use hardware timestamp, no clock snapshot\n",
> > + csid->id);
> > + return ktime_get_ns();
> > + }
> > +
> > + now = ktime_to_ns(snap.systime);
> > +
> > + /*
> > + * Translating requires reading the counter the CSID latched from.
> > + * If timekeeping is driven by something else there is nothing to
> > + * correlate against, so report the time this event was handled.
> > + * Below CSID means clock-source ID.
> > + */
> > + if (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 now;
> > + }
> > +
> > + /* How long ago, in counter ticks, the timestamp was latched */
> > + delta = (snap.cycles - ticks) & CSID_TIMESTAMP_MASK;
> > + delta_ns = csid_timestamp_to_ns(delta);
> > +
> > + /* 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 now;
> > + }
> > +
> > + 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;
> > + u64 ticks;
> > +
> > + if (!hw_ops->sof_ts)
> > + return ktime_get_ns();
> > +
> > + ticks = hw_ops->sof_ts(csid, id);
> > +
> > + /*
> > + * For double-buffered CSID registers, the CSID configuration is
> > + * only latched on the first SOF, so no timestamp is captured for
> > + * that first SOF.
> > + */
> > + if (!ticks && atomic_read(&csid->sof_seq[id]) == -1)
> > + return ktime_get_ns();
> > +
> > + return csid_timestamp_to_monotonic(csid, ticks);
> > +}
> > +
> > void csid_sof_event(struct csid_device *csid, unsigned int id)
> > {
> > + /* Read before the sequence number below, which arms the first SOF */
> > + u64 ts = csid_hw_sof_ts(csid, id);
> > struct v4l2_event event = {
> > .type = V4L2_EVENT_FRAME_SYNC,
> > .u.frame_sync.frame_sequence =
> > atomic_inc_return(&csid->sof_seq[id]),
> > };
> >
> > + atomic64_set(&csid->sof_ts[id], ts);
> > +
> > 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 ccafdbf3a9fe0c282da68ccdd04661b3fab81ff5..f7c0935ea25cee5ac87cca470803a7391c12f9f0 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
> > @@ -179,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];
>
> I think we should group those two in structure
>
> struct {
> atomic_t seq;
> atomic64_t ts;
> } sof_info[MSM_CSID_MAX_SRC_STREAMS]
>
> I believe it may grow in the future. I am not sure if _info is needed it can be just sof.
Ok, looks like a good idea.
Thanks,
Loic
>
> ~Gjorgji
>