[PATCH 08/11] media: stm32: dcmipp: addition of a statistics capture device
From: Alain Volmat
Date: Fri Dec 04 2026 - 06:12:18 EST
This commit adds the statistics capture device from which
application are able to grab buffers containing structured
frame statistics gathered by the DCMIPP ISP block.
The statistics produced depends on configuration done via the
ISP parameter buffer given by the video output device for ISP
parameters.
Signed-off-by: Alain Volmat <alain.volmat@xxxxxxxxxxx>
---
drivers/media/platform/st/stm32/Kconfig | 2 +
.../media/platform/st/stm32/stm32-dcmipp/Makefile | 2 +-
.../platform/st/stm32/stm32-dcmipp/dcmipp-common.h | 56 ++
.../platform/st/stm32/stm32-dcmipp/dcmipp-isp.c | 1 +
.../st/stm32/stm32-dcmipp/dcmipp-statcap.c | 773 +++++++++++++++++++++
5 files changed, 833 insertions(+), 1 deletion(-)
diff --git a/drivers/media/platform/st/stm32/Kconfig b/drivers/media/platform/st/stm32/Kconfig
index f12e67bcc9bc..8ac1b1943e15 100644
--- a/drivers/media/platform/st/stm32/Kconfig
+++ b/drivers/media/platform/st/stm32/Kconfig
@@ -37,6 +37,8 @@ config VIDEO_STM32_DCMIPP
depends on ARCH_STM32 || COMPILE_TEST
select MEDIA_CONTROLLER
select VIDEOBUF2_DMA_CONTIG
+ select V4L2_ISP
+ select VIDEOBUF2_VMALLOC
select VIDEO_V4L2_SUBDEV_API
select V4L2_FWNODE
help
diff --git a/drivers/media/platform/st/stm32/stm32-dcmipp/Makefile b/drivers/media/platform/st/stm32/stm32-dcmipp/Makefile
index e35d45a0aca2..7af3c68cebdf 100644
--- a/drivers/media/platform/st/stm32/stm32-dcmipp/Makefile
+++ b/drivers/media/platform/st/stm32/stm32-dcmipp/Makefile
@@ -1,5 +1,5 @@
# SPDX-License-Identifier: GPL-2.0
stm32-dcmipp-y := dcmipp-core.o dcmipp-common.o dcmipp-input.o dcmipp-byteproc.o dcmipp-capture.o
-stm32-dcmipp-y += dcmipp-pixelcommon.o dcmipp-isp.o dcmipp-pixelproc.o
+stm32-dcmipp-y += dcmipp-pixelcommon.o dcmipp-isp.o dcmipp-pixelproc.o dcmipp-statcap.o
obj-$(CONFIG_VIDEO_STM32_DCMIPP) += stm32-dcmipp.o
diff --git a/drivers/media/platform/st/stm32/stm32-dcmipp/dcmipp-common.h b/drivers/media/platform/st/stm32/stm32-dcmipp/dcmipp-common.h
index 21ee903ae3a8..3a1e3085128f 100644
--- a/drivers/media/platform/st/stm32/stm32-dcmipp/dcmipp-common.h
+++ b/drivers/media/platform/st/stm32/stm32-dcmipp/dcmipp-common.h
@@ -19,6 +19,8 @@
#include <media/v4l2-fwnode.h>
#include <media/videobuf2-core.h>
+#include <uapi/linux/media/st/dcmipp_config.h>
+
#define DCMIPP_PDEV_NAME "dcmipp"
#define DCMIPP_FRAME_MAX_WIDTH 4096
@@ -270,6 +272,10 @@ struct dcmipp_isp_device {
u32 sink_code;
struct v4l2_rect sink_compose;
+ /* Statistics extraction configuration set by the isp-params device */
+ struct stm32_dcmipp_params_stat stat_cfg;
+ bool stat_cfg_updated;
+
u32 frame_sequence;
u32 vsync_mask;
@@ -287,6 +293,52 @@ struct dcmipp_pixelproc_device {
u32 pipe_id;
};
+/* This structure describe the state right after the VSYNC comes */
+enum stat_capture_state {
+ COLD_START, /* Shadow: AVERAGE (RGB), Physical: stopped */
+ /* Full capture profile */
+ PHY_AV_RGB_SHA_BIN_0, /* Shadow: BIN_0, Physical: AVERAGE (RGB) */
+ PHY_BIN_0_SHA_BIN_1, /* Shadow: BIN_1, Physical: BIN_0 */
+ PHY_BIN_1_SHA_BIN_2, /* Shadow: BIN_2, Physical: BIN_1 */
+ PHY_BIN_2_SHA_BIN_3, /* Shadow: BIN_3, Physical: BIN_2 */
+ PHY_BIN_3_SHA_AV_RGB, /* Shadow: AVERAGE (RGB), Physical: BIN_3 */
+ /* Average pre-post profile */
+ PHY_AV_RGB, /* Shadow: AVERAGE (RGB), Physical: AVERAGE (RGB) */
+ AV_READ, /* Capturing AVERAGE / Accumulators with valid AVERAGE */
+};
+
+struct dcmipp_statcap_device {
+ struct dcmipp_ent_device ved;
+ struct video_device vdev;
+ struct device *dev;
+ struct media_pad pad;
+ struct vb2_queue queue;
+ struct list_head buffers;
+ /* Protects the access of variables shared within the interrupt */
+ spinlock_t irqlock;
+ /* Protect this data structure */
+ struct mutex lock;
+ struct v4l2_subdev *s_subdev;
+ struct media_pad *s_pad;
+ u32 frame_format;
+ struct v4l2_rect stat_region;
+ u32 stat_location;
+ bool stat_ready;
+ /* Set while the irq thread may access registers, protected by irqlock */
+ bool streaming;
+
+ /* current and prev state of the capture stat machine */
+ enum stat_capture_state capture_state;
+ enum stat_capture_state prev_capture_state;
+
+ void __iomem *regs;
+
+ /* Local copy of statistics */
+ struct stm32_dcmipp_stats_avr_bins stat_pre;
+ struct stm32_dcmipp_stats_avr_bins stat_post;
+ u32 bad_pixel_count;
+};
+
/**
* dcmipp_pads_init - initialize pads
*
@@ -410,5 +462,9 @@ struct dcmipp_ent_device *
dcmipp_pixelproc_ent_init(const char *entity_name,
struct dcmipp_device *dcmipp);
void dcmipp_pixelproc_ent_release(struct dcmipp_ent_device *ved);
+struct dcmipp_ent_device *
+dcmipp_statcap_ent_init(const char *entity_name,
+ struct dcmipp_device *dcmipp);
+void dcmipp_statcap_ent_release(struct dcmipp_ent_device *ved);
#endif
diff --git a/drivers/media/platform/st/stm32/stm32-dcmipp/dcmipp-isp.c b/drivers/media/platform/st/stm32/stm32-dcmipp/dcmipp-isp.c
index 4678b6fefafc..2d941f1e867c 100644
--- a/drivers/media/platform/st/stm32/stm32-dcmipp/dcmipp-isp.c
+++ b/drivers/media/platform/st/stm32/stm32-dcmipp/dcmipp-isp.c
@@ -502,6 +502,7 @@ struct dcmipp_ent_device *dcmipp_isp_ent_init(const char *entity_name,
return ERR_PTR(-ENOMEM);
isp->regs = dcmipp->regs;
+ isp->stat_cfg.bin_comp = STM32_DCMIPP_ISP_STAT_BIN_COMP_L;
/* Initialize ved and sd */
ret = dcmipp_ent_sd_register(&isp->ved, &isp->sd,
diff --git a/drivers/media/platform/st/stm32/stm32-dcmipp/dcmipp-statcap.c b/drivers/media/platform/st/stm32/stm32-dcmipp/dcmipp-statcap.c
new file mode 100644
index 000000000000..48696dd8b57e
--- /dev/null
+++ b/drivers/media/platform/st/stm32/stm32-dcmipp/dcmipp-statcap.c
@@ -0,0 +1,773 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Driver for STM32 Digital Camera Memory Interface Pixel Processor
+ *
+ * Copyright (C) STMicroelectronics SA 2026
+ * Authors: Alain Volmat <alain.volmat@xxxxxxxxxxx>
+ * Fabien Dessenne <fabien.dessenne@xxxxxxxxxxx>
+ * Hugues Fruchet <hugues.fruchet@xxxxxxxxxxx>
+ * for STMicroelectronics.
+ */
+
+#include <linux/math64.h>
+#include <linux/pm_runtime.h>
+#include <media/v4l2-ioctl.h>
+#include <media/v4l2-isp.h>
+#include <media/v4l2-mc.h>
+#include <media/videobuf2-core.h>
+#include <media/videobuf2-vmalloc.h>
+
+#include "dcmipp-common.h"
+
+#define DCMIPP_STATS_BUF_SIZE v4l2_isp_buffer_size(STM32_DCMIPP_STATS_MAX_SIZE)
+
+static const struct v4l2_isp_stats_block_type_info dcmipp_stats_blocks_info[] = {
+ [STM32_DCMIPP_STATS_BLOCK_TYPE_PRE] = {
+ .size = sizeof(struct stm32_dcmipp_stats_avr_bins),
+ },
+ [STM32_DCMIPP_STATS_BLOCK_TYPE_POST] = {
+ .size = sizeof(struct stm32_dcmipp_stats_avr_bins),
+ },
+ [STM32_DCMIPP_STATS_BLOCK_TYPE_BPR] = {
+ .size = sizeof(struct stm32_dcmipp_stats_bpr),
+ },
+};
+
+#define DCMIPP_P1BPRSR 0x828
+#define DCMIPP_P1BPRSR_BADCNT_MASK GENMASK(11, 0)
+
+#define DCMIPP_P1STXCR(a) (0x850 + ((a) * 0x4))
+#define DCMIPP_P1STXCR_ENABLE BIT(0)
+#define DCMIPP_P1STXCR_BINS_SHIFT 2
+#define DCMIPP_P1STXCR_SRC_COMP_SHIFT 4
+#define DCMIPP_P1STXCR_SRC_LOC_PRE 0
+#define DCMIPP_P1STXCR_SRC_LOC_POST 1
+#define DCMIPP_P1STXCR_SRC_LOC_SHIFT 6
+#define DCMIPP_P1STXCR_MODE_AVERAGE 0
+#define DCMIPP_P1STXCR_MODE_BINS BIT(7)
+
+#define DCMIPP_P1STSTR 0x85c
+#define DCMIPP_P1STSTR_HSTART_SHIFT 0
+#define DCMIPP_P1STSTR_HSTART_MASK GENMASK(11, 0)
+#define DCMIPP_P1STSTR_VSTART_SHIFT 16
+#define DCMIPP_P1STSTR_VSTART_MASK GENMASK(27, 16)
+
+#define DCMIPP_P1STSZR 0x860
+#define DCMIPP_P1STSZR_HSIZE_SHIFT 0
+#define DCMIPP_P1STSZR_HSIZE_MASK GENMASK(11, 0)
+#define DCMIPP_P1STSZR_VSIZE_SHIFT 16
+#define DCMIPP_P1STSZR_VSIZE_MASK GENMASK(27, 16)
+#define DCMIPP_P1STSZR_ENABLE BIT(31)
+
+#define DCMIPP_P1STXSR(a) (0x864 + ((a) * 0x4))
+
+struct dcmipp_buf {
+ struct vb2_v4l2_buffer vb;
+ struct list_head list;
+};
+
+enum component {
+ COMP_RED = 0,
+ COMP_GREEN,
+ COMP_BLUE,
+};
+
+static int dcmipp_statcap_get_subdev_pad(struct dcmipp_statcap_device *vcap,
+ struct v4l2_subdev **s_subdev,
+ struct media_pad **s_pad)
+{
+ struct media_pad *pad;
+
+ if (!vcap->s_subdev || !vcap->s_pad) {
+ pad = media_pad_remote_pad_first(&vcap->vdev.entity.pads[0]);
+ if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
+ return -EINVAL;
+ vcap->s_subdev = media_entity_to_v4l2_subdev(pad->entity);
+ vcap->s_pad = pad;
+ }
+
+ if (s_subdev)
+ *s_subdev = vcap->s_subdev;
+ if (s_pad)
+ *s_pad = vcap->s_pad;
+
+ return 0;
+}
+
+static int dcmipp_statcap_querycap(struct file *file, void *priv,
+ struct v4l2_capability *cap)
+{
+ strscpy(cap->driver, DCMIPP_PDEV_NAME, sizeof(cap->driver));
+ strscpy(cap->card, KBUILD_MODNAME, sizeof(cap->card));
+
+ return 0;
+}
+
+static int dcmipp_statcap_g_fmt_meta_cap(struct file *file, void *priv,
+ struct v4l2_format *f)
+{
+ struct v4l2_meta_format *meta = &f->fmt.meta;
+
+ memset(meta, 0, sizeof(*meta));
+ meta->dataformat = V4L2_META_FMT_ST_DCMIPP_ISP_STAT;
+ meta->buffersize = DCMIPP_STATS_BUF_SIZE;
+
+ return 0;
+}
+
+static int dcmipp_statcap_enum_fmt_meta_cap(struct file *file, void *priv,
+ struct v4l2_fmtdesc *f)
+{
+ if (f->index > 0)
+ return -EINVAL;
+
+ if (f->mbus_code && f->mbus_code != MEDIA_BUS_FMT_METADATA_FIXED)
+ return -EINVAL;
+
+ f->pixelformat = V4L2_META_FMT_ST_DCMIPP_ISP_STAT;
+
+ return 0;
+}
+
+static const struct v4l2_file_operations dcmipp_statcap_fops = {
+ .owner = THIS_MODULE,
+ .open = v4l2_fh_open,
+ .release = vb2_fop_release,
+ .poll = vb2_fop_poll,
+ .unlocked_ioctl = video_ioctl2,
+ .mmap = vb2_fop_mmap,
+};
+
+static const struct v4l2_ioctl_ops dcmipp_statcap_ioctl_ops = {
+ .vidioc_querycap = dcmipp_statcap_querycap,
+
+ .vidioc_enum_fmt_meta_cap = dcmipp_statcap_enum_fmt_meta_cap,
+ .vidioc_g_fmt_meta_cap = dcmipp_statcap_g_fmt_meta_cap,
+ .vidioc_s_fmt_meta_cap = dcmipp_statcap_g_fmt_meta_cap,
+ .vidioc_try_fmt_meta_cap = dcmipp_statcap_g_fmt_meta_cap,
+
+ .vidioc_reqbufs = vb2_ioctl_reqbufs,
+ .vidioc_create_bufs = vb2_ioctl_create_bufs,
+ .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
+ .vidioc_querybuf = vb2_ioctl_querybuf,
+ .vidioc_qbuf = vb2_ioctl_qbuf,
+ .vidioc_dqbuf = vb2_ioctl_dqbuf,
+ .vidioc_expbuf = vb2_ioctl_expbuf,
+ .vidioc_streamon = vb2_ioctl_streamon,
+ .vidioc_streamoff = vb2_ioctl_streamoff,
+};
+
+static int dcmipp_statcap_start_streaming(struct vb2_queue *vq,
+ unsigned int count)
+{
+ struct dcmipp_statcap_device *vcap = vb2_get_drv_priv(vq);
+ struct media_entity *entity = &vcap->vdev.entity;
+ struct dcmipp_buf *buf, *node;
+ struct v4l2_subdev *s_subdev;
+ struct media_pad *s_pad;
+ int ret;
+
+ ret = pm_runtime_resume_and_get(vcap->dev);
+ if (ret < 0) {
+ dev_err(vcap->dev, "%s: Failed to start streaming, cannot get sync (%d)\n",
+ __func__, ret);
+ goto err_buffer_done;
+ }
+
+ ret = media_pipeline_start(entity->pads, &vcap->ved.dcmipp->pipe);
+ if (ret) {
+ dev_dbg(vcap->dev, "%s: Failed to start streaming, media pipeline start error (%d)\n",
+ __func__, ret);
+ goto err_pm_put;
+ }
+
+ /* Get pointer to the source subdev */
+ ret = dcmipp_statcap_get_subdev_pad(vcap, &s_subdev, &s_pad);
+ if (ret)
+ goto err_media_pipeline_stop;
+
+ /* Start all the elements within pipeline */
+ ret = v4l2_subdev_enable_streams(s_subdev, s_pad->index, BIT_ULL(0));
+ if (ret) {
+ dev_err(vcap->dev, "failed to start streaming (%d)\n", ret);
+ goto err_media_pipeline_stop;
+ }
+
+ spin_lock_irq(&vcap->irqlock);
+ vcap->capture_state = COLD_START;
+ vcap->streaming = true;
+ spin_unlock_irq(&vcap->irqlock);
+
+ return 0;
+
+err_media_pipeline_stop:
+ media_pipeline_stop(entity->pads);
+err_pm_put:
+ pm_runtime_put(vcap->dev);
+err_buffer_done:
+ spin_lock_irq(&vcap->irqlock);
+ /*
+ * Return all buffers to vb2 in QUEUED state.
+ * This will give ownership back to userspace
+ */
+ list_for_each_entry_safe(buf, node, &vcap->buffers, list) {
+ list_del_init(&buf->list);
+ vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_QUEUED);
+ }
+ spin_unlock_irq(&vcap->irqlock);
+
+ return ret;
+}
+
+/*
+ * Stop the stream engine. Any remaining buffers in the stream queue are
+ * dequeued and passed on to the vb2 framework marked as STATE_ERROR.
+ */
+static void dcmipp_statcap_stop_streaming(struct vb2_queue *vq)
+{
+ struct dcmipp_statcap_device *vcap = vb2_get_drv_priv(vq);
+ struct dcmipp_buf *buf, *node;
+ struct v4l2_subdev *s_subdev;
+ struct media_pad *s_pad;
+ int ret;
+
+ spin_lock_irq(&vcap->irqlock);
+ vcap->streaming = false;
+ vcap->capture_state = COLD_START;
+ spin_unlock_irq(&vcap->irqlock);
+
+ ret = dcmipp_statcap_get_subdev_pad(vcap, &s_subdev, &s_pad);
+ if (ret) {
+ dev_warn(vcap->dev, "Failed to get source subdev / pad\n");
+ return;
+ }
+
+ ret = v4l2_subdev_disable_streams(s_subdev, s_pad->index, BIT_ULL(0));
+ if (ret)
+ dev_err(vcap->dev, "failed to stop streaming (%d)\n", ret);
+
+ /* Stop the media pipeline */
+ media_pipeline_stop(vcap->vdev.entity.pads);
+
+ spin_lock_irq(&vcap->irqlock);
+
+ /* Return all queued buffers to vb2 in ERROR state */
+ list_for_each_entry_safe(buf, node, &vcap->buffers, list) {
+ list_del_init(&buf->list);
+ vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
+ }
+ INIT_LIST_HEAD(&vcap->buffers);
+
+ spin_unlock_irq(&vcap->irqlock);
+
+ pm_runtime_put(vcap->dev);
+}
+
+static int dcmipp_statcap_buf_prepare(struct vb2_buffer *vb)
+{
+ if (vb2_plane_size(vb, 0) < DCMIPP_STATS_BUF_SIZE)
+ return -EINVAL;
+
+ vb2_set_plane_payload(vb, 0, DCMIPP_STATS_BUF_SIZE);
+
+ return 0;
+}
+
+static void dcmipp_statcap_buf_queue(struct vb2_buffer *vb2_buf)
+{
+ struct dcmipp_statcap_device *vcap = vb2_get_drv_priv(vb2_buf->vb2_queue);
+ struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2_buf);
+ struct dcmipp_buf *buf = container_of(vbuf, struct dcmipp_buf, vb);
+
+ spin_lock_irq(&vcap->irqlock);
+ list_add_tail(&buf->list, &vcap->buffers);
+ spin_unlock_irq(&vcap->irqlock);
+}
+
+static int dcmipp_statcap_queue_setup(struct vb2_queue *vq,
+ unsigned int *nbuffers,
+ unsigned int *nplanes,
+ unsigned int sizes[],
+ struct device *alloc_devs[])
+{
+ if (*nplanes)
+ return sizes[0] < DCMIPP_STATS_BUF_SIZE ? -EINVAL : 0;
+
+ *nplanes = 1;
+ sizes[0] = DCMIPP_STATS_BUF_SIZE;
+
+ return 0;
+}
+
+static int dcmipp_statcap_buf_init(struct vb2_buffer *vb)
+{
+ struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
+ struct dcmipp_buf *buf = container_of(vbuf, struct dcmipp_buf, vb);
+
+ INIT_LIST_HEAD(&buf->list);
+
+ return 0;
+}
+
+static const struct vb2_ops dcmipp_statcap_qops = {
+ .start_streaming = dcmipp_statcap_start_streaming,
+ .stop_streaming = dcmipp_statcap_stop_streaming,
+ .buf_init = dcmipp_statcap_buf_init,
+ .buf_prepare = dcmipp_statcap_buf_prepare,
+ .buf_queue = dcmipp_statcap_buf_queue,
+ .queue_setup = dcmipp_statcap_queue_setup,
+};
+
+static void dcmipp_statcap_release(struct video_device *vdev)
+{
+ struct dcmipp_statcap_device *vcap =
+ container_of(vdev, struct dcmipp_statcap_device, vdev);
+
+ mutex_destroy(&vcap->lock);
+
+ kfree(vcap);
+}
+
+void dcmipp_statcap_ent_release(struct dcmipp_ent_device *ved)
+{
+ struct dcmipp_statcap_device *vcap =
+ container_of(ved, struct dcmipp_statcap_device, ved);
+
+ media_entity_cleanup(ved->ent);
+ vb2_video_unregister_device(&vcap->vdev);
+}
+
+static union stm32_dcmipp_stats_block *
+dcmipp_statcap_init_block(struct dcmipp_statcap_device *vcap,
+ struct v4l2_isp_buffer *stats, unsigned int type)
+{
+ return (union stm32_dcmipp_stats_block *)
+ v4l2_isp_stats_init_block(vcap->dev, stats,
+ dcmipp_stats_blocks_info,
+ ARRAY_SIZE(dcmipp_stats_blocks_info),
+ type, STM32_DCMIPP_STATS_MAX_SIZE);
+}
+
+static void
+dcmipp_statcap_fill_avr_bins(union stm32_dcmipp_stats_block *block,
+ const struct stm32_dcmipp_stats_avr_bins *src)
+{
+ memcpy(block->avr_bins.average_rgb, src->average_rgb,
+ sizeof(src->average_rgb));
+ memcpy(block->avr_bins.bins, src->bins, sizeof(src->bins));
+ block->avr_bins.reserved = 0;
+}
+
+static void dcmipp_statcap_buffer_done(struct dcmipp_statcap_device *vcap,
+ struct dcmipp_buf *dcmipp_buf)
+{
+ const struct stm32_dcmipp_params_stat *cfg = &vcap->ved.dcmipp->isp->stat_cfg;
+ struct v4l2_isp_buffer *stats;
+ union stm32_dcmipp_stats_block *block;
+
+ stats = vb2_plane_vaddr(&dcmipp_buf->vb.vb2_buf, 0);
+ v4l2_isp_stats_init_buffer(stats, V4L2_ISP_VERSION_V1);
+
+ if (cfg->profile != STM32_DCMIPP_ISP_STAT_PROFILE_AVERAGE_POST) {
+ block = dcmipp_statcap_init_block(vcap, stats,
+ STM32_DCMIPP_STATS_BLOCK_TYPE_PRE);
+ if (IS_ERR(block))
+ goto err;
+ dcmipp_statcap_fill_avr_bins(block, &vcap->stat_pre);
+ }
+
+ if (cfg->profile != STM32_DCMIPP_ISP_STAT_PROFILE_AVERAGE_PRE) {
+ block = dcmipp_statcap_init_block(vcap, stats,
+ STM32_DCMIPP_STATS_BLOCK_TYPE_POST);
+ if (IS_ERR(block))
+ goto err;
+ dcmipp_statcap_fill_avr_bins(block, &vcap->stat_post);
+ }
+
+ block = dcmipp_statcap_init_block(vcap, stats,
+ STM32_DCMIPP_STATS_BLOCK_TYPE_BPR);
+ if (IS_ERR(block))
+ goto err;
+ block->bpr.bad_pixel_count = vcap->bad_pixel_count;
+ block->bpr.reserved = 0;
+
+ vb2_set_plane_payload(&dcmipp_buf->vb.vb2_buf, 0,
+ v4l2_isp_buffer_size(stats->data_size));
+ /*
+ * Accumulators read upon this VSYNC contain values of the frame which
+ * has just ended, that is the last frame used to build these stats
+ */
+ dcmipp_buf->vb.sequence = vcap->ved.dcmipp->isp->frame_sequence - 1;
+ dcmipp_buf->vb.vb2_buf.timestamp = ktime_get_ns();
+ vb2_buffer_done(&dcmipp_buf->vb.vb2_buf, VB2_BUF_STATE_DONE);
+ return;
+
+err:
+ vb2_buffer_done(&dcmipp_buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
+}
+
+static u32 dcmipp_statcap_get_src(u32 location, u32 comp)
+{
+ return (location << DCMIPP_P1STXCR_SRC_LOC_SHIFT) | (comp << DCMIPP_P1STXCR_SRC_COMP_SHIFT);
+}
+
+static void dcmipp_statcap_read_avg_stats(struct dcmipp_statcap_device *vcap)
+{
+ struct stm32_dcmipp_stats_avr_bins *avr_bins =
+ vcap->stat_location == DCMIPP_P1STXCR_SRC_LOC_PRE ? &vcap->stat_pre :
+ &vcap->stat_post;
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(vcap->stat_pre.average_rgb); i++) {
+ /* 64 bits since the normalization overflows 32 bits */
+ u64 avg = reg_read(vcap, DCMIPP_P1STXSR(i));
+
+ /* Normalize values */
+ avg <<= 8;
+
+ /* Depending on the position & component, need to adjust in case of Bayer */
+ if (vcap->stat_location == DCMIPP_P1STXCR_SRC_LOC_PRE &&
+ vcap->frame_format >= MEDIA_BUS_FMT_SBGGR8_1X8 &&
+ vcap->frame_format <= MEDIA_BUS_FMT_SRGGB16_1X16) {
+ /* raw bayer: RGB component not present for all pixels */
+ if (i == COMP_RED || i == COMP_BLUE)
+ avg *= 4;
+ else if (i == COMP_GREEN)
+ avg *= 2;
+ }
+
+ /* Divide by number of pixels */
+ avr_bins->average_rgb[i] =
+ div_u64(avg, vcap->stat_region.width * vcap->stat_region.height);
+ }
+}
+
+static void dcmipp_statcap_config_area(struct dcmipp_statcap_device *vcap,
+ const struct stm32_dcmipp_params_stat *cfg)
+{
+ const struct v4l2_rect *frame = &vcap->ved.dcmipp->isp->sink_compose;
+
+ if (cfg->width &&
+ cfg->left + cfg->width <= frame->width &&
+ cfg->top + cfg->height <= frame->height) {
+ vcap->stat_region.left = cfg->left;
+ vcap->stat_region.top = cfg->top;
+ vcap->stat_region.width = cfg->width;
+ vcap->stat_region.height = cfg->height;
+
+ reg_clear(vcap, DCMIPP_P1STSTR,
+ DCMIPP_P1STSTR_HSTART_MASK | DCMIPP_P1STSTR_VSTART_MASK);
+ reg_set(vcap, DCMIPP_P1STSTR,
+ (cfg->left << DCMIPP_P1STSTR_HSTART_SHIFT) |
+ (cfg->top << DCMIPP_P1STSTR_VSTART_SHIFT));
+
+ reg_clear(vcap, DCMIPP_P1STSZR,
+ DCMIPP_P1STSZR_HSIZE_MASK | DCMIPP_P1STSZR_VSIZE_MASK);
+ reg_set(vcap, DCMIPP_P1STSZR,
+ (cfg->width << DCMIPP_P1STSZR_HSIZE_SHIFT) |
+ (cfg->height << DCMIPP_P1STSZR_VSIZE_SHIFT) |
+ DCMIPP_P1STSZR_ENABLE);
+ return;
+ }
+
+ if (cfg->width)
+ dev_dbg(vcap->dev, "Stat area not fitting into frame, using whole frame\n");
+
+ /* Statistics computed over the whole frame */
+ vcap->stat_region = *frame;
+ reg_clear(vcap, DCMIPP_P1STSZR, DCMIPP_P1STSZR_ENABLE);
+}
+
+static void dcmipp_statcap_update_local_buf(struct dcmipp_statcap_device *vcap)
+{
+ const struct stm32_dcmipp_params_stat *cfg = &vcap->ved.dcmipp->isp->stat_cfg;
+ struct stm32_dcmipp_stats_avr_bins *avr_bins =
+ vcap->stat_location == DCMIPP_P1STXCR_SRC_LOC_PRE ? &vcap->stat_pre :
+ &vcap->stat_post;
+ int i;
+
+ /* Read the bad pixel count stat and store it locally */
+ vcap->bad_pixel_count = reg_read(vcap, DCMIPP_P1BPRSR) &
+ DCMIPP_P1BPRSR_BADCNT_MASK;
+
+ /*
+ * This is the core function for statistic extraction, within the
+ * irq thread, on EACH VSYNC we update the shadow registers to
+ * read accumulators values, (except on first few frames), store
+ * them into the internal structure, and update the shadow registers
+ * to be able to get new values 2 VSYNCs after (the values read at
+ * the next VSYNC being already based on the configuration that has
+ * just been written on the previous VSYNC
+ */
+ switch (vcap->capture_state) {
+ case COLD_START:
+ vcap->stat_ready = false;
+ memset(&vcap->stat_pre, 0, sizeof(vcap->stat_pre));
+ memset(&vcap->stat_post, 0, sizeof(vcap->stat_post));
+
+ /* Get frame format info from ISP sink pad */
+ vcap->frame_format = vcap->ved.dcmipp->isp->sink_code;
+
+ dcmipp_statcap_config_area(vcap, cfg);
+
+ /*
+ * All stats profile starts from the PRE statistics, except the
+ * AVERAGE POST
+ */
+ if (cfg->profile == STM32_DCMIPP_ISP_STAT_PROFILE_AVERAGE_POST)
+ vcap->stat_location = DCMIPP_P1STXCR_SRC_LOC_POST;
+ else
+ vcap->stat_location = DCMIPP_P1STXCR_SRC_LOC_PRE;
+ /*
+ * We've just started, set control registers to capture
+ * AVERAGES (RGB) and leave
+ */
+ for (i = 0; i < 3; i++)
+ reg_write(vcap, DCMIPP_P1STXCR(i),
+ DCMIPP_P1STXCR_MODE_AVERAGE |
+ dcmipp_statcap_get_src(vcap->stat_location, i) |
+ cfg->avg_filter << DCMIPP_P1STXCR_BINS_SHIFT |
+ DCMIPP_P1STXCR_ENABLE);
+ break;
+
+ case PHY_AV_RGB_SHA_BIN_0:
+ /* Set control registers to capture the 1st set of BINS */
+ for (i = 0; i < 3; i++)
+ reg_write(vcap, DCMIPP_P1STXCR(i),
+ DCMIPP_P1STXCR_MODE_BINS |
+ dcmipp_statcap_get_src(vcap->stat_location, cfg->bin_comp) |
+ 0 << DCMIPP_P1STXCR_BINS_SHIFT |
+ DCMIPP_P1STXCR_ENABLE);
+
+ if (vcap->prev_capture_state == PHY_BIN_3_SHA_AV_RGB) {
+ /* The data capture refer to the previous location */
+ avr_bins = !(vcap->stat_location == DCMIPP_P1STXCR_SRC_LOC_PRE) ?
+ &vcap->stat_pre : &vcap->stat_post;
+ /* Accumulators contains the 4th set of BINS */
+ for (i = 0; i < 3; i++)
+ avr_bins->bins[i + 9] = reg_read(vcap, DCMIPP_P1STXSR(i));
+ /* By the time we get the 4th POST BINS, stat_location is again in PRE */
+ if (vcap->stat_location == DCMIPP_P1STXCR_SRC_LOC_PRE)
+ vcap->stat_ready = true;
+ }
+ break;
+
+ case PHY_BIN_0_SHA_BIN_1:
+ /* Set control registers to capture the 2nd set of BINS */
+ for (i = 0; i < 3; i++)
+ reg_write(vcap, DCMIPP_P1STXCR(i),
+ DCMIPP_P1STXCR_MODE_BINS |
+ dcmipp_statcap_get_src(vcap->stat_location, cfg->bin_comp) |
+ 1 << DCMIPP_P1STXCR_BINS_SHIFT |
+ DCMIPP_P1STXCR_ENABLE);
+
+ /* Accumulators contains the AVERAGES (RGB) */
+ dcmipp_statcap_read_avg_stats(vcap);
+ break;
+
+ case PHY_BIN_1_SHA_BIN_2:
+ /* Set control registers to capture the 3rd set of BINS */
+ for (i = 0; i < 3; i++)
+ reg_write(vcap, DCMIPP_P1STXCR(i),
+ DCMIPP_P1STXCR_MODE_BINS |
+ dcmipp_statcap_get_src(vcap->stat_location, cfg->bin_comp) |
+ 2 << DCMIPP_P1STXCR_BINS_SHIFT |
+ DCMIPP_P1STXCR_ENABLE);
+
+ /* Accumulators contains the 1st set of BINS */
+ for (i = 0; i < 3; i++)
+ avr_bins->bins[i] = reg_read(vcap, DCMIPP_P1STXSR(i));
+ break;
+
+ case PHY_BIN_2_SHA_BIN_3:
+ /* Set control registers to capture the 4th set of BINS */
+ for (i = 0; i < 3; i++)
+ reg_write(vcap, DCMIPP_P1STXCR(i),
+ DCMIPP_P1STXCR_MODE_BINS |
+ dcmipp_statcap_get_src(vcap->stat_location, cfg->bin_comp) |
+ 3 << DCMIPP_P1STXCR_BINS_SHIFT |
+ DCMIPP_P1STXCR_ENABLE);
+
+ /* Accumulators contains the 2nd set of BINS */
+ for (i = 0; i < 3; i++)
+ avr_bins->bins[i + 3] = reg_read(vcap, DCMIPP_P1STXSR(i));
+ break;
+
+ case PHY_BIN_3_SHA_AV_RGB:
+ /* Set control registers to capture the AVERAGES (RGB) */
+ for (i = 0; i < 3; i++)
+ /* Usage of !location is on purpose to switch to the other location */
+ reg_write(vcap, DCMIPP_P1STXCR(i),
+ DCMIPP_P1STXCR_MODE_AVERAGE |
+ dcmipp_statcap_get_src(!vcap->stat_location, i) |
+ cfg->avg_filter << DCMIPP_P1STXCR_BINS_SHIFT |
+ DCMIPP_P1STXCR_ENABLE);
+
+ /* Accumulators contains the 3rd set of BINS */
+ for (i = 0; i < 3; i++)
+ avr_bins->bins[i + 6] = reg_read(vcap, DCMIPP_P1STXSR(i));
+ break;
+
+ case AV_READ:
+ /* State used for the AVERAGE PRE capture mode */
+ dcmipp_statcap_read_avg_stats(vcap);
+ vcap->stat_ready = true;
+ break;
+
+ default:
+ break;
+ }
+
+ /* Update the capture_state & prev_capture_state */
+ switch (cfg->profile) {
+ case STM32_DCMIPP_ISP_STAT_PROFILE_FULL:
+ vcap->prev_capture_state = vcap->capture_state;
+ if (vcap->capture_state < PHY_BIN_3_SHA_AV_RGB) {
+ vcap->capture_state++;
+ } else {
+ vcap->stat_location = !vcap->stat_location;
+ vcap->capture_state = PHY_AV_RGB_SHA_BIN_0;
+ }
+ break;
+
+ case STM32_DCMIPP_ISP_STAT_PROFILE_AVERAGE_PRE:
+ case STM32_DCMIPP_ISP_STAT_PROFILE_AVERAGE_POST:
+ if (vcap->capture_state == COLD_START)
+ vcap->capture_state = PHY_AV_RGB;
+ else if (vcap->capture_state == PHY_AV_RGB)
+ vcap->capture_state = AV_READ;
+ break;
+ }
+}
+
+static irqreturn_t dcmipp_statcap_irq_thread(int irq, void *arg)
+{
+ struct dcmipp_statcap_device *vcap =
+ container_of(arg, struct dcmipp_statcap_device, ved);
+ struct dcmipp_isp_device *isp = vcap->ved.dcmipp->isp;
+ struct dcmipp_buf *cur_buf = NULL;
+
+ /* The statistics state machine progresses once per frame */
+ if (!(vcap->ved.cmsr2 & isp->vsync_mask))
+ return IRQ_HANDLED;
+
+ spin_lock_irq(&vcap->irqlock);
+
+ /* We only do things if we are streaming */
+ if (!vcap->streaming) {
+ spin_unlock_irq(&vcap->irqlock);
+ return IRQ_HANDLED;
+ }
+
+ /* Restart the capture cycle upon statistics configuration change */
+ if (isp->stat_cfg_updated) {
+ isp->stat_cfg_updated = false;
+ vcap->capture_state = COLD_START;
+ }
+
+ dcmipp_statcap_update_local_buf(vcap);
+
+ /* If a full capture cycle has been done, output data to a buffer */
+ if (vcap->stat_ready) {
+ /* Get an available buffer */
+ if (!list_empty(&vcap->buffers)) {
+ cur_buf = list_first_entry(&vcap->buffers, struct dcmipp_buf, list);
+ list_del(&cur_buf->list);
+ }
+ if (cur_buf)
+ dcmipp_statcap_buffer_done(vcap, cur_buf);
+ }
+
+ spin_unlock_irq(&vcap->irqlock);
+
+ return IRQ_HANDLED;
+}
+
+struct dcmipp_ent_device *
+dcmipp_statcap_ent_init(const char *entity_name, struct dcmipp_device *dcmipp)
+{
+ struct dcmipp_statcap_device *vcap;
+ struct device *dev = dcmipp->dev;
+ struct video_device *vdev;
+ struct vb2_queue *q;
+ int ret = 0;
+
+ /* Allocate the dcmipp_statcap_device struct */
+ vcap = kzalloc_obj(*vcap);
+ if (!vcap)
+ return ERR_PTR(-ENOMEM);
+
+ vcap->ved.dcmipp = dcmipp;
+
+ /* Initialize the media entity */
+ vcap->vdev.entity.name = entity_name;
+ vcap->vdev.entity.function = MEDIA_ENT_F_IO_V4L;
+ vcap->pad.flags = MEDIA_PAD_FL_SINK;
+ ret = media_entity_pads_init(&vcap->vdev.entity, 1, &vcap->pad);
+ if (ret)
+ goto err_free_vcap;
+
+ /* Initialize the lock */
+ mutex_init(&vcap->lock);
+
+ /* Initialize the vb2 queue */
+ q = &vcap->queue;
+ q->type = V4L2_BUF_TYPE_META_CAPTURE;
+ q->io_modes = VB2_MMAP | VB2_DMABUF;
+ q->lock = &vcap->lock;
+ q->drv_priv = vcap;
+ q->buf_struct_size = sizeof(struct dcmipp_buf);
+ q->ops = &dcmipp_statcap_qops;
+ q->mem_ops = &vb2_vmalloc_memops;
+ q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
+ q->min_queued_buffers = 1;
+ q->dev = dev;
+
+ ret = vb2_queue_init(q);
+ if (ret) {
+ dev_err(dev, "%s: vb2 queue init failed (err=%d)\n",
+ entity_name, ret);
+ goto err_clean_m_ent;
+ }
+
+ /* Initialize buffer list and its lock */
+ INIT_LIST_HEAD(&vcap->buffers);
+ spin_lock_init(&vcap->irqlock);
+
+ /* Fill the dcmipp_ent_device struct */
+ vcap->ved.ent = &vcap->vdev.entity;
+ vcap->ved.handler = NULL;
+ vcap->ved.thread_fn = dcmipp_statcap_irq_thread;
+ vcap->dev = dev;
+ vcap->regs = dcmipp->regs;
+
+ /* Initialize the video_device struct */
+ vdev = &vcap->vdev;
+ vdev->device_caps = V4L2_CAP_META_CAPTURE | V4L2_CAP_STREAMING |
+ V4L2_CAP_IO_MC;
+ vdev->release = dcmipp_statcap_release;
+ vdev->fops = &dcmipp_statcap_fops;
+ vdev->ioctl_ops = &dcmipp_statcap_ioctl_ops;
+ vdev->lock = &vcap->lock;
+ vdev->queue = q;
+ vdev->v4l2_dev = &dcmipp->v4l2_dev;
+ strscpy(vdev->name, entity_name, sizeof(vdev->name));
+ video_set_drvdata(vdev, &vcap->ved);
+
+ /* Register the video_device with the v4l2 and the media framework */
+ ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
+ if (ret) {
+ dev_err(dev, "%s: video register failed (err=%d)\n",
+ vcap->vdev.name, ret);
+ goto err_clean_m_ent;
+ }
+
+ return &vcap->ved;
+
+err_clean_m_ent:
+ media_entity_cleanup(&vcap->vdev.entity);
+err_free_vcap:
+ kfree(vcap);
+
+ return ERR_PTR(ret);
+}
--
2.34.1