1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for STM32 Digital Camera Memory Interface Pixel Processor
5 * Copyright (C) STMicroelectronics SA 2023
8 * for STMicroelectronics.
11 #include <linux/iopoll.h>
12 #include <linux/pm_runtime.h>
13 #include <media/v4l2-ioctl.h>
14 #include <media/v4l2-mc.h>
15 #include <media/videobuf2-core.h>
16 #include <media/videobuf2-dma-contig.h>
18 #include "dcmipp-common.h"
20 #define DCMIPP_PRSR 0x1f8
21 #define DCMIPP_CMIER 0x3f0
22 #define DCMIPP_CMIER_P0FRAMEIE BIT(9)
23 #define DCMIPP_CMIER_P0VSYNCIE BIT(10)
24 #define DCMIPP_CMIER_P0OVRIE BIT(15)
25 #define DCMIPP_CMIER_P0ALL (DCMIPP_CMIER_P0VSYNCIE |\
26 DCMIPP_CMIER_P0FRAMEIE |\
28 #define DCMIPP_CMSR1 0x3f4
29 #define DCMIPP_CMSR2 0x3f8
30 #define DCMIPP_CMSR2_P0FRAMEF BIT(9)
31 #define DCMIPP_CMSR2_P0VSYNCF BIT(10)
32 #define DCMIPP_CMSR2_P0OVRF BIT(15)
33 #define DCMIPP_CMFCR 0x3fc
34 #define DCMIPP_P0FSCR 0x404
35 #define DCMIPP_P0FSCR_PIPEN BIT(31)
36 #define DCMIPP_P0FCTCR 0x500
37 #define DCMIPP_P0FCTCR_CPTREQ BIT(3)
38 #define DCMIPP_P0DCCNTR 0x5b0
39 #define DCMIPP_P0DCLMTR 0x5b4
40 #define DCMIPP_P0DCLMTR_ENABLE BIT(31)
41 #define DCMIPP_P0DCLMTR_LIMIT_MASK GENMASK(23, 0)
42 #define DCMIPP_P0PPM0AR1 0x5c4
43 #define DCMIPP_P0SR 0x5f8
44 #define DCMIPP_P0SR_CPTACT BIT(23)
46 struct dcmipp_bytecap_pix_map {
51 #define PIXMAP_MBUS_PFMT(mbus, fmt) \
53 .code = MEDIA_BUS_FMT_##mbus, \
54 .pixelformat = V4L2_PIX_FMT_##fmt \
57 static const struct dcmipp_bytecap_pix_map dcmipp_bytecap_pix_map_list[] = {
58 PIXMAP_MBUS_PFMT(RGB565_2X8_LE, RGB565),
59 PIXMAP_MBUS_PFMT(YUYV8_2X8, YUYV),
60 PIXMAP_MBUS_PFMT(YVYU8_2X8, YVYU),
61 PIXMAP_MBUS_PFMT(UYVY8_2X8, UYVY),
62 PIXMAP_MBUS_PFMT(VYUY8_2X8, VYUY),
63 PIXMAP_MBUS_PFMT(Y8_1X8, GREY),
64 PIXMAP_MBUS_PFMT(SBGGR8_1X8, SBGGR8),
65 PIXMAP_MBUS_PFMT(SGBRG8_1X8, SGBRG8),
66 PIXMAP_MBUS_PFMT(SGRBG8_1X8, SGRBG8),
67 PIXMAP_MBUS_PFMT(SRGGB8_1X8, SRGGB8),
68 PIXMAP_MBUS_PFMT(JPEG_1X8, JPEG),
71 static const struct dcmipp_bytecap_pix_map *
72 dcmipp_bytecap_pix_map_by_pixelformat(u32 pixelformat)
76 for (i = 0; i < ARRAY_SIZE(dcmipp_bytecap_pix_map_list); i++) {
77 if (dcmipp_bytecap_pix_map_list[i].pixelformat == pixelformat)
78 return &dcmipp_bytecap_pix_map_list[i];
85 struct vb2_v4l2_buffer vb;
89 struct list_head list;
94 DCMIPP_WAIT_FOR_BUFFER,
98 struct dcmipp_bytecap_device {
99 struct dcmipp_ent_device ved;
100 struct video_device vdev;
102 struct v4l2_pix_format format;
103 struct vb2_queue queue;
104 struct list_head buffers;
106 * Protects concurrent calls of buf queue / irq handler
107 * and buffer handling related variables / lists
110 /* mutex used as vdev and queue lock */
113 struct media_pipeline pipe;
114 struct v4l2_subdev *s_subdev;
116 enum dcmipp_state state;
119 * DCMIPP driver is handling 2 buffers
120 * active: buffer into which DCMIPP is currently writing into
121 * next: buffer given to the DCMIPP and which will become
122 * automatically active on next VSYNC
124 struct dcmipp_buf *active, *next;
144 static const struct v4l2_pix_format fmt_default = {
145 .width = DCMIPP_FMT_WIDTH_DEFAULT,
146 .height = DCMIPP_FMT_HEIGHT_DEFAULT,
147 .pixelformat = V4L2_PIX_FMT_RGB565,
148 .field = V4L2_FIELD_NONE,
149 .bytesperline = DCMIPP_FMT_WIDTH_DEFAULT * 2,
150 .sizeimage = DCMIPP_FMT_WIDTH_DEFAULT * DCMIPP_FMT_HEIGHT_DEFAULT * 2,
151 .colorspace = DCMIPP_COLORSPACE_DEFAULT,
152 .ycbcr_enc = DCMIPP_YCBCR_ENC_DEFAULT,
153 .quantization = DCMIPP_QUANTIZATION_DEFAULT,
154 .xfer_func = DCMIPP_XFER_FUNC_DEFAULT,
157 static int dcmipp_bytecap_querycap(struct file *file, void *priv,
158 struct v4l2_capability *cap)
160 strscpy(cap->driver, DCMIPP_PDEV_NAME, sizeof(cap->driver));
161 strscpy(cap->card, KBUILD_MODNAME, sizeof(cap->card));
166 static int dcmipp_bytecap_g_fmt_vid_cap(struct file *file, void *priv,
167 struct v4l2_format *f)
169 struct dcmipp_bytecap_device *vcap = video_drvdata(file);
171 f->fmt.pix = vcap->format;
176 static int dcmipp_bytecap_try_fmt_vid_cap(struct file *file, void *priv,
177 struct v4l2_format *f)
179 struct dcmipp_bytecap_device *vcap = video_drvdata(file);
180 struct v4l2_pix_format *format = &f->fmt.pix;
181 const struct dcmipp_bytecap_pix_map *vpix;
184 /* Don't accept a pixelformat that is not on the table */
185 vpix = dcmipp_bytecap_pix_map_by_pixelformat(format->pixelformat);
187 format->pixelformat = fmt_default.pixelformat;
189 /* Adjust width & height */
190 in_w = format->width;
191 in_h = format->height;
192 v4l_bound_align_image(&format->width, DCMIPP_FRAME_MIN_WIDTH,
193 DCMIPP_FRAME_MAX_WIDTH, 0, &format->height,
194 DCMIPP_FRAME_MIN_HEIGHT, DCMIPP_FRAME_MAX_HEIGHT,
196 if (format->width != in_w || format->height != in_h)
197 dev_dbg(vcap->dev, "resolution updated: %dx%d -> %dx%d\n",
198 in_w, in_h, format->width, format->height);
200 if (format->pixelformat == V4L2_PIX_FMT_JPEG) {
201 format->bytesperline = format->width;
202 format->sizeimage = format->bytesperline * format->height;
204 v4l2_fill_pixfmt(format, format->pixelformat,
205 format->width, format->height);
208 if (format->field == V4L2_FIELD_ANY)
209 format->field = fmt_default.field;
211 dcmipp_colorimetry_clamp(format);
216 static int dcmipp_bytecap_s_fmt_vid_cap(struct file *file, void *priv,
217 struct v4l2_format *f)
219 struct dcmipp_bytecap_device *vcap = video_drvdata(file);
222 /* Do not change the format while stream is on */
223 if (vb2_is_busy(&vcap->queue))
226 ret = dcmipp_bytecap_try_fmt_vid_cap(file, priv, f);
230 dev_dbg(vcap->dev, "%s: format update: old:%ux%u (0x%p4cc, %u, %u, %u, %u) new:%ux%d (0x%p4cc, %u, %u, %u, %u)\n",
233 vcap->format.width, vcap->format.height,
234 &vcap->format.pixelformat, vcap->format.colorspace,
235 vcap->format.quantization, vcap->format.xfer_func,
236 vcap->format.ycbcr_enc,
238 f->fmt.pix.width, f->fmt.pix.height,
239 &f->fmt.pix.pixelformat, f->fmt.pix.colorspace,
240 f->fmt.pix.quantization, f->fmt.pix.xfer_func,
241 f->fmt.pix.ycbcr_enc);
243 vcap->format = f->fmt.pix;
248 static int dcmipp_bytecap_enum_fmt_vid_cap(struct file *file, void *priv,
249 struct v4l2_fmtdesc *f)
251 const struct dcmipp_bytecap_pix_map *vpix;
252 unsigned int index = f->index;
257 * If a media bus code is specified, only enumerate formats
258 * compatible with it.
260 for (i = 0; i < ARRAY_SIZE(dcmipp_bytecap_pix_map_list); i++) {
261 vpix = &dcmipp_bytecap_pix_map_list[i];
262 if (vpix->code != f->mbus_code)
271 if (i == ARRAY_SIZE(dcmipp_bytecap_pix_map_list))
274 /* Otherwise, enumerate all formats. */
275 if (f->index >= ARRAY_SIZE(dcmipp_bytecap_pix_map_list))
278 vpix = &dcmipp_bytecap_pix_map_list[f->index];
281 f->pixelformat = vpix->pixelformat;
286 static int dcmipp_bytecap_enum_framesizes(struct file *file, void *fh,
287 struct v4l2_frmsizeenum *fsize)
289 const struct dcmipp_bytecap_pix_map *vpix;
294 /* Only accept code in the pix map table */
295 vpix = dcmipp_bytecap_pix_map_by_pixelformat(fsize->pixel_format);
299 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS;
300 fsize->stepwise.min_width = DCMIPP_FRAME_MIN_WIDTH;
301 fsize->stepwise.max_width = DCMIPP_FRAME_MAX_WIDTH;
302 fsize->stepwise.min_height = DCMIPP_FRAME_MIN_HEIGHT;
303 fsize->stepwise.max_height = DCMIPP_FRAME_MAX_HEIGHT;
304 fsize->stepwise.step_width = 1;
305 fsize->stepwise.step_height = 1;
310 static const struct v4l2_file_operations dcmipp_bytecap_fops = {
311 .owner = THIS_MODULE,
312 .open = v4l2_fh_open,
313 .release = vb2_fop_release,
314 .read = vb2_fop_read,
315 .poll = vb2_fop_poll,
316 .unlocked_ioctl = video_ioctl2,
317 .mmap = vb2_fop_mmap,
320 static const struct v4l2_ioctl_ops dcmipp_bytecap_ioctl_ops = {
321 .vidioc_querycap = dcmipp_bytecap_querycap,
323 .vidioc_g_fmt_vid_cap = dcmipp_bytecap_g_fmt_vid_cap,
324 .vidioc_s_fmt_vid_cap = dcmipp_bytecap_s_fmt_vid_cap,
325 .vidioc_try_fmt_vid_cap = dcmipp_bytecap_try_fmt_vid_cap,
326 .vidioc_enum_fmt_vid_cap = dcmipp_bytecap_enum_fmt_vid_cap,
327 .vidioc_enum_framesizes = dcmipp_bytecap_enum_framesizes,
329 .vidioc_reqbufs = vb2_ioctl_reqbufs,
330 .vidioc_create_bufs = vb2_ioctl_create_bufs,
331 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
332 .vidioc_querybuf = vb2_ioctl_querybuf,
333 .vidioc_qbuf = vb2_ioctl_qbuf,
334 .vidioc_dqbuf = vb2_ioctl_dqbuf,
335 .vidioc_expbuf = vb2_ioctl_expbuf,
336 .vidioc_streamon = vb2_ioctl_streamon,
337 .vidioc_streamoff = vb2_ioctl_streamoff,
340 static int dcmipp_pipeline_s_stream(struct dcmipp_bytecap_device *vcap,
343 struct media_pad *pad;
347 * Get source subdev - since link is IMMUTABLE, pointer is cached
348 * within the dcmipp_bytecap_device structure
350 if (!vcap->s_subdev) {
351 pad = media_pad_remote_pad_first(&vcap->vdev.entity.pads[0]);
352 if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
354 vcap->s_subdev = media_entity_to_v4l2_subdev(pad->entity);
357 ret = v4l2_subdev_call(vcap->s_subdev, video, s_stream, state);
359 dev_err(vcap->dev, "failed to %s streaming (%d)\n",
360 state ? "start" : "stop", ret);
367 static void dcmipp_start_capture(struct dcmipp_bytecap_device *vcap,
368 struct dcmipp_buf *buf)
370 /* Set buffer address */
371 reg_write(vcap, DCMIPP_P0PPM0AR1, buf->addr);
373 /* Set buffer size */
374 reg_write(vcap, DCMIPP_P0DCLMTR, DCMIPP_P0DCLMTR_ENABLE |
375 ((buf->size / 4) & DCMIPP_P0DCLMTR_LIMIT_MASK));
377 /* Capture request */
378 reg_set(vcap, DCMIPP_P0FCTCR, DCMIPP_P0FCTCR_CPTREQ);
381 static void dcmipp_bytecap_all_buffers_done(struct dcmipp_bytecap_device *vcap,
382 enum vb2_buffer_state state)
384 struct dcmipp_buf *buf, *node;
386 list_for_each_entry_safe(buf, node, &vcap->buffers, list) {
387 list_del_init(&buf->list);
388 vb2_buffer_done(&buf->vb.vb2_buf, state);
392 static int dcmipp_bytecap_start_streaming(struct vb2_queue *vq,
395 struct dcmipp_bytecap_device *vcap = vb2_get_drv_priv(vq);
396 struct media_entity *entity = &vcap->vdev.entity;
397 struct dcmipp_buf *buf;
401 memset(&vcap->count, 0, sizeof(vcap->count));
403 ret = pm_runtime_resume_and_get(vcap->dev);
405 dev_err(vcap->dev, "%s: Failed to start streaming, cannot get sync (%d)\n",
407 goto err_buffer_done;
410 ret = media_pipeline_start(entity->pads, &vcap->pipe);
412 dev_dbg(vcap->dev, "%s: Failed to start streaming, media pipeline start error (%d)\n",
417 ret = dcmipp_pipeline_s_stream(vcap, 1);
419 goto err_media_pipeline_stop;
421 spin_lock_irq(&vcap->irqlock);
423 /* Enable pipe at the end of programming */
424 reg_set(vcap, DCMIPP_P0FSCR, DCMIPP_P0FSCR_PIPEN);
427 * vb2 framework guarantee that we have at least 'min_queued_buffers'
428 * buffers in the list at this moment
430 vcap->next = list_first_entry(&vcap->buffers, typeof(*buf), list);
431 dev_dbg(vcap->dev, "Start with next [%d] %p phy=%pad\n",
432 vcap->next->vb.vb2_buf.index, vcap->next, &vcap->next->addr);
434 dcmipp_start_capture(vcap, vcap->next);
436 /* Enable interruptions */
437 vcap->cmier |= DCMIPP_CMIER_P0ALL;
438 reg_set(vcap, DCMIPP_CMIER, vcap->cmier);
440 vcap->state = DCMIPP_RUNNING;
442 spin_unlock_irq(&vcap->irqlock);
446 err_media_pipeline_stop:
447 media_pipeline_stop(entity->pads);
449 pm_runtime_put(vcap->dev);
451 spin_lock_irq(&vcap->irqlock);
453 * Return all buffers to vb2 in QUEUED state.
454 * This will give ownership back to userspace
456 dcmipp_bytecap_all_buffers_done(vcap, VB2_BUF_STATE_QUEUED);
458 spin_unlock_irq(&vcap->irqlock);
463 static void dcmipp_dump_status(struct dcmipp_bytecap_device *vcap)
465 struct device *dev = vcap->dev;
467 dev_dbg(dev, "[DCMIPP_PRSR] =%#10.8x\n", reg_read(vcap, DCMIPP_PRSR));
468 dev_dbg(dev, "[DCMIPP_P0SR] =%#10.8x\n", reg_read(vcap, DCMIPP_P0SR));
469 dev_dbg(dev, "[DCMIPP_P0DCCNTR]=%#10.8x\n",
470 reg_read(vcap, DCMIPP_P0DCCNTR));
471 dev_dbg(dev, "[DCMIPP_CMSR1] =%#10.8x\n", reg_read(vcap, DCMIPP_CMSR1));
472 dev_dbg(dev, "[DCMIPP_CMSR2] =%#10.8x\n", reg_read(vcap, DCMIPP_CMSR2));
476 * Stop the stream engine. Any remaining buffers in the stream queue are
477 * dequeued and passed on to the vb2 framework marked as STATE_ERROR.
479 static void dcmipp_bytecap_stop_streaming(struct vb2_queue *vq)
481 struct dcmipp_bytecap_device *vcap = vb2_get_drv_priv(vq);
485 dcmipp_pipeline_s_stream(vcap, 0);
487 /* Stop the media pipeline */
488 media_pipeline_stop(vcap->vdev.entity.pads);
490 /* Disable interruptions */
491 reg_clear(vcap, DCMIPP_CMIER, vcap->cmier);
494 reg_clear(vcap, DCMIPP_P0FCTCR, DCMIPP_P0FCTCR_CPTREQ);
496 /* Wait until CPTACT become 0 */
497 ret = readl_relaxed_poll_timeout(vcap->regs + DCMIPP_P0SR, status,
498 !(status & DCMIPP_P0SR_CPTACT),
500 1000 * USEC_PER_MSEC);
502 dev_warn(vcap->dev, "Timeout when stopping\n");
505 reg_clear(vcap, DCMIPP_P0FSCR, DCMIPP_P0FSCR_PIPEN);
507 spin_lock_irq(&vcap->irqlock);
509 /* Return all queued buffers to vb2 in ERROR state */
510 dcmipp_bytecap_all_buffers_done(vcap, VB2_BUF_STATE_ERROR);
511 INIT_LIST_HEAD(&vcap->buffers);
514 vcap->state = DCMIPP_STOPPED;
516 spin_unlock_irq(&vcap->irqlock);
518 dcmipp_dump_status(vcap);
520 pm_runtime_put(vcap->dev);
522 if (vcap->count.errors)
523 dev_warn(vcap->dev, "Some errors found while streaming: errors=%d (overrun=%d, limit=%d, nactive=%d), underrun=%d, buffers=%d\n",
524 vcap->count.errors, vcap->count.overrun,
525 vcap->count.limit, vcap->count.nactive,
526 vcap->count.underrun, vcap->count.buffers);
529 static int dcmipp_bytecap_buf_prepare(struct vb2_buffer *vb)
531 struct dcmipp_bytecap_device *vcap = vb2_get_drv_priv(vb->vb2_queue);
532 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
533 struct dcmipp_buf *buf = container_of(vbuf, struct dcmipp_buf, vb);
536 size = vcap->format.sizeimage;
538 if (vb2_plane_size(vb, 0) < size) {
539 dev_err(vcap->dev, "%s data will not fit into plane (%lu < %lu)\n",
540 __func__, vb2_plane_size(vb, 0), size);
544 vb2_set_plane_payload(vb, 0, size);
546 if (!buf->prepared) {
547 /* Get memory addresses */
548 buf->addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
549 buf->size = vb2_plane_size(&buf->vb.vb2_buf, 0);
550 buf->prepared = true;
552 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->size);
554 dev_dbg(vcap->dev, "Setup [%d] phy=%pad size=%zu\n",
555 vb->index, &buf->addr, buf->size);
561 static void dcmipp_bytecap_buf_queue(struct vb2_buffer *vb2_buf)
563 struct dcmipp_bytecap_device *vcap =
564 vb2_get_drv_priv(vb2_buf->vb2_queue);
565 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2_buf);
566 struct dcmipp_buf *buf = container_of(vbuf, struct dcmipp_buf, vb);
568 dev_dbg(vcap->dev, "Queue [%d] %p phy=%pad\n", buf->vb.vb2_buf.index,
571 spin_lock_irq(&vcap->irqlock);
572 list_add_tail(&buf->list, &vcap->buffers);
574 if (vcap->state == DCMIPP_WAIT_FOR_BUFFER) {
576 dev_dbg(vcap->dev, "Restart with next [%d] %p phy=%pad\n",
577 buf->vb.vb2_buf.index, buf, &buf->addr);
579 dcmipp_start_capture(vcap, buf);
581 vcap->state = DCMIPP_RUNNING;
584 spin_unlock_irq(&vcap->irqlock);
587 static int dcmipp_bytecap_queue_setup(struct vb2_queue *vq,
588 unsigned int *nbuffers,
589 unsigned int *nplanes,
590 unsigned int sizes[],
591 struct device *alloc_devs[])
593 struct dcmipp_bytecap_device *vcap = vb2_get_drv_priv(vq);
596 size = vcap->format.sizeimage;
598 /* Make sure the image size is large enough */
600 return sizes[0] < vcap->format.sizeimage ? -EINVAL : 0;
603 sizes[0] = vcap->format.sizeimage;
605 dev_dbg(vcap->dev, "Setup queue, count=%d, size=%d\n",
611 static int dcmipp_bytecap_buf_init(struct vb2_buffer *vb)
613 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
614 struct dcmipp_buf *buf = container_of(vbuf, struct dcmipp_buf, vb);
616 INIT_LIST_HEAD(&buf->list);
621 static const struct vb2_ops dcmipp_bytecap_qops = {
622 .start_streaming = dcmipp_bytecap_start_streaming,
623 .stop_streaming = dcmipp_bytecap_stop_streaming,
624 .buf_init = dcmipp_bytecap_buf_init,
625 .buf_prepare = dcmipp_bytecap_buf_prepare,
626 .buf_queue = dcmipp_bytecap_buf_queue,
627 .queue_setup = dcmipp_bytecap_queue_setup,
630 static void dcmipp_bytecap_release(struct video_device *vdev)
632 struct dcmipp_bytecap_device *vcap =
633 container_of(vdev, struct dcmipp_bytecap_device, vdev);
635 dcmipp_pads_cleanup(vcap->ved.pads);
636 mutex_destroy(&vcap->lock);
641 void dcmipp_bytecap_ent_release(struct dcmipp_ent_device *ved)
643 struct dcmipp_bytecap_device *vcap =
644 container_of(ved, struct dcmipp_bytecap_device, ved);
646 media_entity_cleanup(ved->ent);
647 vb2_video_unregister_device(&vcap->vdev);
650 static void dcmipp_buffer_done(struct dcmipp_bytecap_device *vcap,
651 struct dcmipp_buf *buf,
655 struct vb2_v4l2_buffer *vbuf;
657 list_del_init(&buf->list);
661 vbuf->sequence = vcap->sequence++;
662 vbuf->field = V4L2_FIELD_NONE;
663 vbuf->vb2_buf.timestamp = ktime_get_ns();
664 vb2_set_plane_payload(&vbuf->vb2_buf, 0, bytesused);
665 vb2_buffer_done(&vbuf->vb2_buf,
666 err ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
667 dev_dbg(vcap->dev, "Done [%d] %p phy=%pad\n", buf->vb.vb2_buf.index,
669 vcap->count.buffers++;
672 /* irqlock must be held */
674 dcmipp_bytecap_set_next_frame_or_stop(struct dcmipp_bytecap_device *vcap)
676 if (!vcap->next && list_is_singular(&vcap->buffers)) {
678 * If there is no available buffer (none or a single one in the
679 * list while two are expected), stop the capture (effective
680 * for next frame). On-going frame capture will continue until
681 * FRAME END but no further capture will be done.
683 reg_clear(vcap, DCMIPP_P0FCTCR, DCMIPP_P0FCTCR_CPTREQ);
685 dev_dbg(vcap->dev, "Capture restart is deferred to next buffer queueing\n");
687 vcap->state = DCMIPP_WAIT_FOR_BUFFER;
691 /* If we don't have buffer yet, pick the one after active */
693 vcap->next = list_next_entry(vcap->active, list);
697 * This register is shadowed and will be taken into
698 * account on next VSYNC (start of next frame)
700 reg_write(vcap, DCMIPP_P0PPM0AR1, vcap->next->addr);
701 dev_dbg(vcap->dev, "Write [%d] %p phy=%pad\n",
702 vcap->next->vb.vb2_buf.index, vcap->next, &vcap->next->addr);
705 /* irqlock must be held */
706 static void dcmipp_bytecap_process_frame(struct dcmipp_bytecap_device *vcap,
710 struct dcmipp_buf *buf = vcap->active;
713 vcap->count.nactive++;
714 vcap->count.errors++;
718 if (bytesused > buf->size) {
719 dev_dbg(vcap->dev, "frame larger than expected (%zu > %zu)\n",
720 bytesused, buf->size);
721 /* Clip to buffer size and return buffer to V4L2 in error */
722 bytesused = buf->size;
724 vcap->count.errors++;
728 dcmipp_buffer_done(vcap, buf, bytesused, err);
732 static irqreturn_t dcmipp_bytecap_irq_thread(int irq, void *arg)
734 struct dcmipp_bytecap_device *vcap =
735 container_of(arg, struct dcmipp_bytecap_device, ved);
736 size_t bytesused = 0;
739 spin_lock_irq(&vcap->irqlock);
741 cmsr2 = vcap->cmsr2 & vcap->cmier;
744 * If we have an overrun, a frame-end will probably not be generated,
745 * in that case the active buffer will be recycled as next buffer by
748 if (cmsr2 & DCMIPP_CMSR2_P0OVRF) {
749 vcap->count.errors++;
750 vcap->count.overrun++;
753 if (cmsr2 & DCMIPP_CMSR2_P0FRAMEF) {
756 /* Read captured buffer size */
757 bytesused = reg_read(vcap, DCMIPP_P0DCCNTR);
758 dcmipp_bytecap_process_frame(vcap, bytesused);
761 if (cmsr2 & DCMIPP_CMSR2_P0VSYNCF) {
763 if (vcap->state == DCMIPP_WAIT_FOR_BUFFER) {
764 vcap->count.underrun++;
769 * On VSYNC, the previously set next buffer is going to become
770 * active thanks to the shadowing mechanism of the DCMIPP. In
771 * most of the cases, since a FRAMEEND has already come,
772 * pointer next is NULL since active is reset during the
773 * FRAMEEND handling. However, in case of framerate adjustment,
774 * there are more VSYNC than FRAMEEND. Thus we recycle the
775 * active (but not used) buffer and put it back into next.
777 swap(vcap->active, vcap->next);
778 dcmipp_bytecap_set_next_frame_or_stop(vcap);
782 spin_unlock_irq(&vcap->irqlock);
786 static irqreturn_t dcmipp_bytecap_irq_callback(int irq, void *arg)
788 struct dcmipp_bytecap_device *vcap =
789 container_of(arg, struct dcmipp_bytecap_device, ved);
791 /* Store interrupt status register */
792 vcap->cmsr2 = reg_read(vcap, DCMIPP_CMSR2) & vcap->cmier;
795 /* Clear interrupt */
796 reg_write(vcap, DCMIPP_CMFCR, vcap->cmsr2);
798 return IRQ_WAKE_THREAD;
801 static int dcmipp_bytecap_link_validate(struct media_link *link)
803 struct media_entity *entity = link->sink->entity;
804 struct video_device *vd = media_entity_to_video_device(entity);
805 struct dcmipp_bytecap_device *vcap = container_of(vd,
806 struct dcmipp_bytecap_device, vdev);
807 struct v4l2_subdev *source_sd =
808 media_entity_to_v4l2_subdev(link->source->entity);
809 struct v4l2_subdev_format source_fmt = {
810 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
811 .pad = link->source->index,
813 const struct dcmipp_bytecap_pix_map *vpix;
816 ret = v4l2_subdev_call(source_sd, pad, get_fmt, NULL, &source_fmt);
820 if (source_fmt.format.width != vcap->format.width ||
821 source_fmt.format.height != vcap->format.height) {
822 dev_err(vcap->dev, "Wrong width or height %ux%u (%ux%u expected)\n",
823 vcap->format.width, vcap->format.height,
824 source_fmt.format.width, source_fmt.format.height);
828 vpix = dcmipp_bytecap_pix_map_by_pixelformat(vcap->format.pixelformat);
829 if (source_fmt.format.code != vpix->code) {
830 dev_err(vcap->dev, "Wrong mbus_code 0x%x, (0x%x expected)\n",
831 vpix->code, source_fmt.format.code);
838 static const struct media_entity_operations dcmipp_bytecap_entity_ops = {
839 .link_validate = dcmipp_bytecap_link_validate,
842 struct dcmipp_ent_device *dcmipp_bytecap_ent_init(struct device *dev,
843 const char *entity_name,
844 struct v4l2_device *v4l2_dev,
847 struct dcmipp_bytecap_device *vcap;
848 struct video_device *vdev;
850 const unsigned long pad_flag = MEDIA_PAD_FL_SINK;
853 /* Allocate the dcmipp_bytecap_device struct */
854 vcap = kzalloc(sizeof(*vcap), GFP_KERNEL);
856 return ERR_PTR(-ENOMEM);
858 /* Allocate the pads */
859 vcap->ved.pads = dcmipp_pads_init(1, &pad_flag);
860 if (IS_ERR(vcap->ved.pads)) {
861 ret = PTR_ERR(vcap->ved.pads);
865 /* Initialize the media entity */
866 vcap->vdev.entity.name = entity_name;
867 vcap->vdev.entity.function = MEDIA_ENT_F_IO_V4L;
868 vcap->vdev.entity.ops = &dcmipp_bytecap_entity_ops;
869 ret = media_entity_pads_init(&vcap->vdev.entity, 1, vcap->ved.pads);
873 /* Initialize the lock */
874 mutex_init(&vcap->lock);
876 /* Initialize the vb2 queue */
878 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
879 q->io_modes = VB2_MMAP | VB2_DMABUF;
880 q->lock = &vcap->lock;
882 q->buf_struct_size = sizeof(struct dcmipp_buf);
883 q->ops = &dcmipp_bytecap_qops;
884 q->mem_ops = &vb2_dma_contig_memops;
885 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
886 q->min_queued_buffers = 1;
889 /* DCMIPP requires 16 bytes aligned buffers */
890 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32) & ~0x0f);
892 dev_err(dev, "Failed to set DMA mask\n");
893 goto err_mutex_destroy;
896 ret = vb2_queue_init(q);
898 dev_err(dev, "%s: vb2 queue init failed (err=%d)\n",
900 goto err_clean_m_ent;
903 /* Initialize buffer list and its lock */
904 INIT_LIST_HEAD(&vcap->buffers);
905 spin_lock_init(&vcap->irqlock);
907 /* Set default frame format */
908 vcap->format = fmt_default;
910 /* Fill the dcmipp_ent_device struct */
911 vcap->ved.ent = &vcap->vdev.entity;
912 vcap->ved.handler = dcmipp_bytecap_irq_callback;
913 vcap->ved.thread_fn = dcmipp_bytecap_irq_thread;
917 /* Initialize the video_device struct */
919 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
921 vdev->release = dcmipp_bytecap_release;
922 vdev->fops = &dcmipp_bytecap_fops;
923 vdev->ioctl_ops = &dcmipp_bytecap_ioctl_ops;
924 vdev->lock = &vcap->lock;
926 vdev->v4l2_dev = v4l2_dev;
927 strscpy(vdev->name, entity_name, sizeof(vdev->name));
928 video_set_drvdata(vdev, &vcap->ved);
930 /* Register the video_device with the v4l2 and the media framework */
931 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
933 dev_err(dev, "%s: video register failed (err=%d)\n",
934 vcap->vdev.name, ret);
935 goto err_clean_m_ent;
941 media_entity_cleanup(&vcap->vdev.entity);
943 mutex_destroy(&vcap->lock);
945 dcmipp_pads_cleanup(vcap->ved.pads);