[PATCH 3/3] media: ti: j721e-csi2rx: Generate capture-progress events for sub-frames

Siddharth Vadapalli s-vadapalli at ti.com
Sun Nov 29 05:28:17 PST 2026


A capture buffer is only returned to userspace once the whole frame has
been captured. For low latency use-cases, generate capture-progress events
(V4L2_EVENT_BUF_PROGRESS) to allow userspace applications to consume and
start processing sub-frames.

Since a single DMA descriptor is associated with an entire video frame,
completion of descriptors queued to DMA cannot be an indicator of the
readiness of sub-frames. Hence, use the standard dmaengine_tx_status()
dmaengine API to query progress of the capture. Additionally, use the
High Resolution Timers in Linux to trigger the query periodically.

The number of lines in a sub-frame for which the capture-progress event
has to be generated is set using V4L2_CID_BUF_PROGRESS_SIZE. If number
of lines is set to '0', the capture-progress event is disabled. For a
valid value of the number of lines in a sub-frame, the event is generated.
Similarly, the interval at which the capture-progress has to be sampled
is set using V4L2_CID_BUF_PROGRESS_INTERVAL, which is limited to a minimum
of 100 microseconds to bound the CPU load that userspace can cause.

The parameters are sampled at the start and cannot be modified in the
middle of streaming. They can however be modified when streaming is not in
progress.

Signed-off-by: Siddharth Vadapalli <s-vadapalli at ti.com>
---
 .../platform/ti/j721e-csi2rx/j721e-csi2rx.c   | 251 +++++++++++++++++-
 1 file changed, 246 insertions(+), 5 deletions(-)

diff --git a/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c b/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c
index ba7abb723da6..79d2118301fe 100644
--- a/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c
+++ b/drivers/media/platform/ti/j721e-csi2rx/j721e-csi2rx.c
@@ -10,6 +10,7 @@
 
 #include <linux/bitfield.h>
 #include <linux/dmaengine.h>
+#include <linux/hrtimer.h>
 #include <linux/module.h>
 #include <linux/of_platform.h>
 #include <linux/platform_device.h>
@@ -18,7 +19,9 @@
 
 #include <media/cadence/cdns-csi2rx.h>
 #include <media/mipi-csi2.h>
+#include <media/v4l2-ctrls.h>
 #include <media/v4l2-device.h>
+#include <media/v4l2-event.h>
 #include <media/v4l2-ioctl.h>
 #include <media/v4l2-mc.h>
 #include <media/videobuf2-dma-contig.h>
@@ -66,6 +69,11 @@
 
 #define CSI2RX_BRIDGE_SOURCE_PAD	1
 
+#define TI_CSI2RX_PROGRESS_EVENTS		32
+#define TI_CSI2RX_PROGRESS_INTERVAL_MIN	100
+#define TI_CSI2RX_PROGRESS_INTERVAL_MAX	5000
+#define TI_CSI2RX_PROGRESS_INTERVAL_DEF	200
+
 struct ti_csi2rx_fmt {
 	u32				fourcc;	/* Four character code. */
 	u32				code;	/* Mbus code. */
@@ -79,6 +87,7 @@ struct ti_csi2rx_buffer {
 	struct vb2_v4l2_buffer		vb;
 	struct list_head		list;
 	struct ti_csi2rx_ctx		*ctx;
+	dma_cookie_t			cookie;
 };
 
 enum ti_csi2rx_dma_state {
@@ -100,6 +109,16 @@ struct ti_csi2rx_dma {
 	struct list_head		submitted;
 };
 
+struct ti_csi2rx_progress {
+	struct hrtimer			timer;
+	ktime_t				period;
+	u32				lines;
+	struct ti_csi2rx_buffer		*cur;
+	u32				last;
+	u32				base;
+	bool				have_base;
+};
+
 struct ti_csi2rx_dev;
 
 struct ti_csi2rx_ctx {
@@ -109,6 +128,10 @@ struct ti_csi2rx_ctx {
 	struct mutex			mutex; /* To serialize ioctls. */
 	struct v4l2_format		v_fmt;
 	struct ti_csi2rx_dma		dma;
+	struct ti_csi2rx_progress	progress;
+	struct v4l2_ctrl_handler	ctrl_hdl;
+	struct v4l2_ctrl		*ctrl_progress_size;
+	struct v4l2_ctrl		*ctrl_progress_interval;
 	struct media_pad		pad;
 	struct completion		drain_complete;
 	u32				sequence;
@@ -427,6 +450,20 @@ static int ti_csi2rx_enum_framesizes(struct file *file, void *fh,
 	return 0;
 }
 
+static int ti_csi2rx_subscribe_event(struct v4l2_fh *fh,
+				     const struct v4l2_event_subscription *sub)
+{
+	switch (sub->type) {
+	case V4L2_EVENT_CTRL:
+		return v4l2_ctrl_subscribe_event(fh, sub);
+	case V4L2_EVENT_BUF_PROGRESS:
+		return v4l2_event_subscribe(fh, sub, TI_CSI2RX_PROGRESS_EVENTS,
+					    NULL);
+	default:
+		return -EINVAL;
+	}
+}
+
 static const struct v4l2_ioctl_ops csi_ioctl_ops = {
 	.vidioc_querycap      = ti_csi2rx_querycap,
 	.vidioc_enum_fmt_vid_cap = ti_csi2rx_enum_fmt_vid_cap,
@@ -443,6 +480,8 @@ static const struct v4l2_ioctl_ops csi_ioctl_ops = {
 	.vidioc_expbuf        = vb2_ioctl_expbuf,
 	.vidioc_streamon      = vb2_ioctl_streamon,
 	.vidioc_streamoff     = vb2_ioctl_streamoff,
+	.vidioc_subscribe_event = ti_csi2rx_subscribe_event,
+	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
 };
 
 static const struct v4l2_file_operations csi_fops = {
@@ -728,6 +767,157 @@ static int ti_csi2rx_dma_submit_pending(struct ti_csi2rx_ctx *ctx)
 	return ret;
 }
 
+static void ti_csi2rx_progress_event(struct ti_csi2rx_buffer *buf,
+				     struct ti_csi2rx_ctx *ctx, u32 sequence,
+				     u32 bytesused)
+{
+	struct v4l2_event ev = {
+		.type = V4L2_EVENT_BUF_PROGRESS,
+		.u.buf_progress = {
+			.index = buf->vb.vb2_buf.index,
+			.sequence = sequence,
+			.bytesused = bytesused,
+		},
+	};
+
+	v4l2_event_queue(&ctx->vdev, &ev);
+}
+
+static void ti_csi2rx_progress_sample(struct ti_csi2rx_ctx *ctx,
+				      struct ti_csi2rx_buffer *buf)
+{
+	struct ti_csi2rx_progress *prog = &ctx->progress;
+	struct v4l2_pix_format *pix = &ctx->v_fmt.fmt.pix;
+	struct dma_tx_state st = { };
+	u32 bytes, lines;
+
+	if (buf != prog->cur) {
+		prog->cur = buf;
+		prog->last = 0;
+		prog->have_base = false;
+	}
+
+	if (dmaengine_tx_status(ctx->dma.chan, buf->cookie, &st) !=
+	    DMA_IN_PROGRESS)
+		return;
+
+	if (st.residue > pix->sizeimage)
+		return;
+
+	if (!prog->have_base) {
+		prog->base = st.residue;
+		prog->have_base = true;
+	}
+
+	bytes = (prog->base + pix->sizeimage - st.residue) % pix->sizeimage;
+
+	lines = bytes / pix->bytesperline;
+	lines -= lines % prog->lines;
+	if (lines <= prog->last || lines >= pix->height)
+		return;
+
+	prog->last = lines;
+	ti_csi2rx_progress_event(buf, ctx, ctx->sequence,
+				 lines * pix->bytesperline);
+}
+
+static enum hrtimer_restart ti_csi2rx_progress_timer(struct hrtimer *timer)
+{
+	struct ti_csi2rx_ctx *ctx = container_of(timer, struct ti_csi2rx_ctx,
+						 progress.timer);
+	struct ti_csi2rx_dma *dma = &ctx->dma;
+	struct ti_csi2rx_buffer *buf;
+	unsigned long flags;
+
+	spin_lock_irqsave(&dma->lock, flags);
+
+	buf = list_first_entry_or_null(&dma->submitted,
+				       struct ti_csi2rx_buffer, list);
+	if (buf && dma->state == TI_CSI2RX_DMA_ACTIVE && ctx->progress.lines)
+		ti_csi2rx_progress_sample(ctx, buf);
+
+	spin_unlock_irqrestore(&dma->lock, flags);
+
+	hrtimer_forward_now(timer, ctx->progress.period);
+
+	return HRTIMER_RESTART;
+}
+
+static void ti_csi2rx_progress_start(struct ti_csi2rx_ctx *ctx)
+{
+	struct ti_csi2rx_progress *prog = &ctx->progress;
+	u32 lines, interval;
+	unsigned long flags;
+
+	v4l2_ctrl_grab(ctx->ctrl_progress_size, true);
+	v4l2_ctrl_grab(ctx->ctrl_progress_interval, true);
+
+	lines = v4l2_ctrl_g_ctrl(ctx->ctrl_progress_size);
+	interval = v4l2_ctrl_g_ctrl(ctx->ctrl_progress_interval);
+
+	if (!lines)
+		return;
+
+	prog->period = ns_to_ktime((u64)interval * NSEC_PER_USEC);
+
+	spin_lock_irqsave(&ctx->dma.lock, flags);
+	prog->lines = lines;
+	prog->cur = NULL;
+	spin_unlock_irqrestore(&ctx->dma.lock, flags);
+
+	hrtimer_start(&prog->timer, prog->period, HRTIMER_MODE_REL_SOFT);
+}
+
+static void ti_csi2rx_progress_pause(struct ti_csi2rx_ctx *ctx)
+{
+	struct ti_csi2rx_progress *prog = &ctx->progress;
+	unsigned long flags;
+
+	hrtimer_cancel(&prog->timer);
+
+	spin_lock_irqsave(&ctx->dma.lock, flags);
+	prog->cur = NULL;
+	spin_unlock_irqrestore(&ctx->dma.lock, flags);
+}
+
+static void ti_csi2rx_progress_resume(struct ti_csi2rx_ctx *ctx)
+{
+	struct ti_csi2rx_progress *prog = &ctx->progress;
+	unsigned long flags;
+
+	spin_lock_irqsave(&ctx->dma.lock, flags);
+	if (prog->lines)
+		hrtimer_start(&prog->timer, prog->period,
+			      HRTIMER_MODE_REL_SOFT);
+	spin_unlock_irqrestore(&ctx->dma.lock, flags);
+}
+
+static void ti_csi2rx_progress_stop(struct ti_csi2rx_ctx *ctx)
+{
+	struct ti_csi2rx_progress *prog = &ctx->progress;
+	unsigned long flags;
+
+	spin_lock_irqsave(&ctx->dma.lock, flags);
+	prog->lines = 0;
+	prog->cur = NULL;
+	spin_unlock_irqrestore(&ctx->dma.lock, flags);
+
+	hrtimer_cancel(&prog->timer);
+
+	v4l2_ctrl_grab(ctx->ctrl_progress_size, false);
+	v4l2_ctrl_grab(ctx->ctrl_progress_interval, false);
+}
+
+static void ti_csi2rx_progress_done(struct ti_csi2rx_ctx *ctx,
+				    struct ti_csi2rx_buffer *buf)
+{
+	if (!ctx->progress.lines)
+		return;
+
+	ti_csi2rx_progress_event(buf, ctx, buf->vb.sequence,
+				 ctx->v_fmt.fmt.pix.sizeimage);
+}
+
 static void ti_csi2rx_dma_callback(void *param)
 {
 	struct ti_csi2rx_buffer *buf = param;
@@ -735,21 +925,23 @@ static void ti_csi2rx_dma_callback(void *param)
 	struct ti_csi2rx_dma *dma = &ctx->dma;
 	unsigned long flags;
 
+	spin_lock_irqsave(&dma->lock, flags);
+
+	WARN_ON(!list_is_first(&buf->list, &dma->submitted));
+
 	/*
 	 * TODO: Derive the sequence number from the CSI2RX frame number
 	 * hardware monitor registers.
 	 */
 	buf->vb.vb2_buf.timestamp = ktime_get_ns();
 	buf->vb.sequence = ctx->sequence++;
-
-	spin_lock_irqsave(&dma->lock, flags);
-
-	WARN_ON(!list_is_first(&buf->list, &dma->submitted));
+	ctx->progress.cur = NULL;
 
 	if (dma->state == TI_CSI2RX_DMA_DRAINING) {
 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
 		dma->state = TI_CSI2RX_DMA_ACTIVE;
 	} else {
+		ti_csi2rx_progress_done(ctx, buf);
 		vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
 	}
 
@@ -790,6 +982,8 @@ static int ti_csi2rx_start_dma(struct ti_csi2rx_ctx *ctx,
 	if (ret)
 		return ret;
 
+	buf->cookie = cookie;
+
 	dma_async_issue_pending(ctx->dma.chan);
 
 	return 0;
@@ -1013,6 +1207,8 @@ static int ti_csi2rx_start_streaming(struct vb2_queue *vq, unsigned int count)
 	if (ret)
 		goto err_dma;
 
+	ti_csi2rx_progress_start(ctx);
+
 	return 0;
 
 err_dma:
@@ -1033,6 +1229,8 @@ static void ti_csi2rx_stop_streaming(struct vb2_queue *vq)
 	struct ti_csi2rx_dev *csi = ctx->csi;
 	int ret;
 
+	ti_csi2rx_progress_stop(ctx);
+
 	video_device_pipeline_stop(&ctx->vdev);
 
 	ret = v4l2_subdev_disable_streams(&csi->subdev,
@@ -1297,6 +1495,8 @@ static void ti_csi2rx_cleanup_ctx(struct ti_csi2rx_ctx *ctx)
 
 	video_unregister_device(&ctx->vdev);
 
+	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
+
 	mutex_destroy(&ctx->mutex);
 }
 
@@ -1480,6 +1680,33 @@ static int ti_csi2rx_v4l2_init(struct ti_csi2rx_dev *csi)
 	return ret;
 }
 
+static int ti_csi2rx_init_ctrls(struct ti_csi2rx_ctx *ctx)
+{
+	struct v4l2_ctrl_handler *hdl = &ctx->ctrl_hdl;
+
+	v4l2_ctrl_handler_init(hdl, 2);
+
+	ctx->ctrl_progress_size = v4l2_ctrl_new_std(hdl, NULL,
+						    V4L2_CID_BUF_PROGRESS_SIZE,
+						    0, MAX_HEIGHT_LINES, 1, 0);
+	ctx->ctrl_progress_interval =
+		v4l2_ctrl_new_std(hdl, NULL, V4L2_CID_BUF_PROGRESS_INTERVAL,
+				  TI_CSI2RX_PROGRESS_INTERVAL_MIN,
+				  TI_CSI2RX_PROGRESS_INTERVAL_MAX, 1,
+				  TI_CSI2RX_PROGRESS_INTERVAL_DEF);
+
+	if (hdl->error) {
+		int ret = hdl->error;
+
+		v4l2_ctrl_handler_free(hdl);
+		return ret;
+	}
+
+	ctx->vdev.ctrl_handler = hdl;
+
+	return 0;
+}
+
 static int ti_csi2rx_init_ctx(struct ti_csi2rx_ctx *ctx)
 {
 	struct ti_csi2rx_dev *csi = ctx->csi;
@@ -1526,11 +1753,17 @@ static int ti_csi2rx_init_ctx(struct ti_csi2rx_ctx *ctx)
 	INIT_LIST_HEAD(&ctx->dma.submitted);
 	spin_lock_init(&ctx->dma.lock);
 	ctx->dma.state = TI_CSI2RX_DMA_STOPPED;
+	hrtimer_setup(&ctx->progress.timer, ti_csi2rx_progress_timer,
+		      CLOCK_MONOTONIC, HRTIMER_MODE_REL_SOFT);
 
-	ret = ti_csi2rx_init_dma(ctx);
+	ret = ti_csi2rx_init_ctrls(ctx);
 	if (ret)
 		return ret;
 
+	ret = ti_csi2rx_init_dma(ctx);
+	if (ret)
+		goto cleanup_ctrls;
+
 	ret = ti_csi2rx_init_vb2q(ctx);
 	if (ret)
 		goto cleanup_dma;
@@ -1539,6 +1772,8 @@ static int ti_csi2rx_init_ctx(struct ti_csi2rx_ctx *ctx)
 
 cleanup_dma:
 	dma_release_channel(ctx->dma.chan);
+cleanup_ctrls:
+	v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
 	return ret;
 }
 
@@ -1597,6 +1832,8 @@ static int ti_csi2rx_suspend(struct device *dev)
 		spin_unlock_irqrestore(&dma->lock, flags);
 
 		if (state != TI_CSI2RX_DMA_STOPPED) {
+			ti_csi2rx_progress_pause(ctx);
+
 			/* Disable source */
 			ret = v4l2_subdev_disable_streams(&csi->subdev,
 							  TI_CSI2RX_PAD_FIRST_SOURCE + ctx->idx,
@@ -1643,6 +1880,8 @@ static int ti_csi2rx_resume(struct device *dev)
 		dma = &ctx->dma;
 		spin_lock_irqsave(&dma->lock, flags);
 		if (dma->state != TI_CSI2RX_DMA_STOPPED) {
+			ctx->progress.cur = NULL;
+
 			/* Re-submit all previously submitted buffers to DMA */
 			list_for_each_entry(buf, &ctx->dma.submitted, list) {
 				ti_csi2rx_start_dma(ctx, buf);
@@ -1657,6 +1896,8 @@ static int ti_csi2rx_resume(struct device *dev)
 							 BIT(0));
 			if (ret)
 				dev_err(ctx->csi->dev, "Failed to start subdev\n");
+
+			ti_csi2rx_progress_resume(ctx);
 		} else {
 			spin_unlock_irqrestore(&dma->lock, flags);
 		}
-- 
2.56.0




More information about the linux-arm-kernel mailing list