1 // SPDX-License-Identifier: GPL-2.0-only
3 * TI Camera Access Layer (CAL) - Video Device
5 * Copyright (c) 2015-2020 Texas Instruments Inc.
8 * Benoit Parrot <bparrot@ti.com>
9 * Laurent Pinchart <laurent.pinchart@ideasonboard.com>
12 #include <linux/ioctl.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/videodev2.h>
16 #include <media/media-device.h>
17 #include <media/v4l2-common.h>
18 #include <media/v4l2-ctrls.h>
19 #include <media/v4l2-device.h>
20 #include <media/v4l2-event.h>
21 #include <media/v4l2-fh.h>
22 #include <media/v4l2-ioctl.h>
23 #include <media/videobuf2-core.h>
24 #include <media/videobuf2-dma-contig.h>
28 /* Print Four-character-code (FOURCC) */
29 static char *fourcc_to_str(u32 fmt)
33 code[0] = (unsigned char)(fmt & 0xff);
34 code[1] = (unsigned char)((fmt >> 8) & 0xff);
35 code[2] = (unsigned char)((fmt >> 16) & 0xff);
36 code[3] = (unsigned char)((fmt >> 24) & 0xff);
42 /* ------------------------------------------------------------------
44 * ------------------------------------------------------------------
47 static int cal_querycap(struct file *file, void *priv,
48 struct v4l2_capability *cap)
50 strscpy(cap->driver, CAL_MODULE_NAME, sizeof(cap->driver));
51 strscpy(cap->card, CAL_MODULE_NAME, sizeof(cap->card));
56 static int cal_g_fmt_vid_cap(struct file *file, void *priv,
57 struct v4l2_format *f)
59 struct cal_ctx *ctx = video_drvdata(file);
66 /* ------------------------------------------------------------------
67 * V4L2 Video Node Centric IOCTLs
68 * ------------------------------------------------------------------
71 static const struct cal_format_info *find_format_by_pix(struct cal_ctx *ctx,
74 const struct cal_format_info *fmtinfo;
77 for (k = 0; k < ctx->num_active_fmt; k++) {
78 fmtinfo = ctx->active_fmt[k];
79 if (fmtinfo->fourcc == pixelformat)
86 static const struct cal_format_info *find_format_by_code(struct cal_ctx *ctx,
89 const struct cal_format_info *fmtinfo;
92 for (k = 0; k < ctx->num_active_fmt; k++) {
93 fmtinfo = ctx->active_fmt[k];
94 if (fmtinfo->code == code)
101 static int cal_legacy_enum_fmt_vid_cap(struct file *file, void *priv,
102 struct v4l2_fmtdesc *f)
104 struct cal_ctx *ctx = video_drvdata(file);
105 const struct cal_format_info *fmtinfo;
107 if (f->index >= ctx->num_active_fmt)
110 fmtinfo = ctx->active_fmt[f->index];
112 f->pixelformat = fmtinfo->fourcc;
113 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
117 static int __subdev_get_format(struct cal_ctx *ctx,
118 struct v4l2_mbus_framefmt *fmt)
120 struct v4l2_subdev_format sd_fmt = {
121 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
124 struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
127 ret = v4l2_subdev_call(ctx->phy->source, pad, get_fmt, NULL, &sd_fmt);
133 ctx_dbg(1, ctx, "%s %dx%d code:%04X\n", __func__,
134 fmt->width, fmt->height, fmt->code);
139 static int __subdev_set_format(struct cal_ctx *ctx,
140 struct v4l2_mbus_framefmt *fmt)
142 struct v4l2_subdev_format sd_fmt = {
143 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
146 struct v4l2_mbus_framefmt *mbus_fmt = &sd_fmt.format;
151 ret = v4l2_subdev_call(ctx->phy->source, pad, set_fmt, NULL, &sd_fmt);
155 ctx_dbg(1, ctx, "%s %dx%d code:%04X\n", __func__,
156 fmt->width, fmt->height, fmt->code);
161 static void cal_calc_format_size(struct cal_ctx *ctx,
162 const struct cal_format_info *fmtinfo,
163 struct v4l2_format *f)
168 * Maximum width is bound by the DMA max width in bytes.
169 * We need to recalculate the actual maxi width depending on the
170 * number of bytes per pixels required.
172 max_width = CAL_MAX_WIDTH_BYTES / (ALIGN(fmtinfo->bpp, 8) >> 3);
173 v4l_bound_align_image(&f->fmt.pix.width, 48, max_width, 2,
174 &f->fmt.pix.height, 32, CAL_MAX_HEIGHT_LINES,
177 bpl = (f->fmt.pix.width * ALIGN(fmtinfo->bpp, 8)) >> 3;
178 f->fmt.pix.bytesperline = ALIGN(bpl, 16);
180 f->fmt.pix.sizeimage = f->fmt.pix.height *
181 f->fmt.pix.bytesperline;
183 ctx_dbg(3, ctx, "%s: fourcc: %s size: %dx%d bpl:%d img_size:%d\n",
184 __func__, fourcc_to_str(f->fmt.pix.pixelformat),
185 f->fmt.pix.width, f->fmt.pix.height,
186 f->fmt.pix.bytesperline, f->fmt.pix.sizeimage);
189 static int cal_legacy_try_fmt_vid_cap(struct file *file, void *priv,
190 struct v4l2_format *f)
192 struct cal_ctx *ctx = video_drvdata(file);
193 const struct cal_format_info *fmtinfo;
194 struct v4l2_subdev_frame_size_enum fse = {
195 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
199 fmtinfo = find_format_by_pix(ctx, f->fmt.pix.pixelformat);
201 ctx_dbg(3, ctx, "Fourcc format (0x%08x) not found.\n",
202 f->fmt.pix.pixelformat);
204 /* Just get the first one enumerated */
205 fmtinfo = ctx->active_fmt[0];
206 f->fmt.pix.pixelformat = fmtinfo->fourcc;
209 f->fmt.pix.field = ctx->v_fmt.fmt.pix.field;
211 /* check for/find a valid width/height */
214 fse.code = fmtinfo->code;
215 for (fse.index = 0; ; fse.index++) {
218 ret = v4l2_subdev_call(ctx->phy->source, pad, enum_frame_size,
223 if ((f->fmt.pix.width == fse.max_width) &&
224 (f->fmt.pix.height == fse.max_height)) {
227 } else if ((f->fmt.pix.width >= fse.min_width) &&
228 (f->fmt.pix.width <= fse.max_width) &&
229 (f->fmt.pix.height >= fse.min_height) &&
230 (f->fmt.pix.height <= fse.max_height)) {
237 /* use existing values as default */
238 f->fmt.pix.width = ctx->v_fmt.fmt.pix.width;
239 f->fmt.pix.height = ctx->v_fmt.fmt.pix.height;
243 * Use current colorspace for now, it will get
244 * updated properly during s_fmt
246 f->fmt.pix.colorspace = ctx->v_fmt.fmt.pix.colorspace;
247 cal_calc_format_size(ctx, fmtinfo, f);
251 static int cal_legacy_s_fmt_vid_cap(struct file *file, void *priv,
252 struct v4l2_format *f)
254 struct cal_ctx *ctx = video_drvdata(file);
255 struct vb2_queue *q = &ctx->vb_vidq;
256 struct v4l2_subdev_format sd_fmt = {
257 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
258 .pad = CAL_CAMERARX_PAD_SINK,
260 const struct cal_format_info *fmtinfo;
263 if (vb2_is_busy(q)) {
264 ctx_dbg(3, ctx, "%s device busy\n", __func__);
268 ret = cal_legacy_try_fmt_vid_cap(file, priv, f);
272 fmtinfo = find_format_by_pix(ctx, f->fmt.pix.pixelformat);
274 v4l2_fill_mbus_format(&sd_fmt.format, &f->fmt.pix, fmtinfo->code);
276 ret = __subdev_set_format(ctx, &sd_fmt.format);
280 /* Just double check nothing has gone wrong */
281 if (sd_fmt.format.code != fmtinfo->code) {
283 "%s subdev changed format on us, this should not happen\n",
288 v4l2_fill_pix_format(&ctx->v_fmt.fmt.pix, &sd_fmt.format);
289 ctx->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
290 ctx->v_fmt.fmt.pix.pixelformat = fmtinfo->fourcc;
291 ctx->v_fmt.fmt.pix.field = sd_fmt.format.field;
292 cal_calc_format_size(ctx, fmtinfo, &ctx->v_fmt);
294 v4l2_subdev_call(&ctx->phy->subdev, pad, set_fmt, NULL, &sd_fmt);
296 ctx->fmtinfo = fmtinfo;
302 static int cal_legacy_enum_framesizes(struct file *file, void *fh,
303 struct v4l2_frmsizeenum *fsize)
305 struct cal_ctx *ctx = video_drvdata(file);
306 const struct cal_format_info *fmtinfo;
307 struct v4l2_subdev_frame_size_enum fse = {
308 .index = fsize->index,
310 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
314 /* check for valid format */
315 fmtinfo = find_format_by_pix(ctx, fsize->pixel_format);
317 ctx_dbg(3, ctx, "Invalid pixel code: %x\n",
318 fsize->pixel_format);
322 fse.code = fmtinfo->code;
324 ret = v4l2_subdev_call(ctx->phy->source, pad, enum_frame_size, NULL,
329 ctx_dbg(1, ctx, "%s: index: %d code: %x W:[%d,%d] H:[%d,%d]\n",
330 __func__, fse.index, fse.code, fse.min_width, fse.max_width,
331 fse.min_height, fse.max_height);
333 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
334 fsize->discrete.width = fse.max_width;
335 fsize->discrete.height = fse.max_height;
340 static int cal_legacy_enum_input(struct file *file, void *priv,
341 struct v4l2_input *inp)
346 inp->type = V4L2_INPUT_TYPE_CAMERA;
347 sprintf(inp->name, "Camera %u", inp->index);
351 static int cal_legacy_g_input(struct file *file, void *priv, unsigned int *i)
357 static int cal_legacy_s_input(struct file *file, void *priv, unsigned int i)
359 return i > 0 ? -EINVAL : 0;
362 /* timeperframe is arbitrary and continuous */
363 static int cal_legacy_enum_frameintervals(struct file *file, void *priv,
364 struct v4l2_frmivalenum *fival)
366 struct cal_ctx *ctx = video_drvdata(file);
367 const struct cal_format_info *fmtinfo;
368 struct v4l2_subdev_frame_interval_enum fie = {
369 .index = fival->index,
370 .width = fival->width,
371 .height = fival->height,
372 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
376 fmtinfo = find_format_by_pix(ctx, fival->pixel_format);
380 fie.code = fmtinfo->code;
381 ret = v4l2_subdev_call(ctx->phy->source, pad, enum_frame_interval,
385 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
386 fival->discrete = fie.interval;
391 static int cal_legacy_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
393 struct cal_ctx *ctx = video_drvdata(file);
395 return v4l2_g_parm_cap(video_devdata(file), ctx->phy->source, a);
398 static int cal_legacy_s_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
400 struct cal_ctx *ctx = video_drvdata(file);
402 return v4l2_s_parm_cap(video_devdata(file), ctx->phy->source, a);
405 static const struct v4l2_ioctl_ops cal_ioctl_legacy_ops = {
406 .vidioc_querycap = cal_querycap,
407 .vidioc_enum_fmt_vid_cap = cal_legacy_enum_fmt_vid_cap,
408 .vidioc_g_fmt_vid_cap = cal_g_fmt_vid_cap,
409 .vidioc_try_fmt_vid_cap = cal_legacy_try_fmt_vid_cap,
410 .vidioc_s_fmt_vid_cap = cal_legacy_s_fmt_vid_cap,
411 .vidioc_enum_framesizes = cal_legacy_enum_framesizes,
412 .vidioc_reqbufs = vb2_ioctl_reqbufs,
413 .vidioc_create_bufs = vb2_ioctl_create_bufs,
414 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
415 .vidioc_querybuf = vb2_ioctl_querybuf,
416 .vidioc_qbuf = vb2_ioctl_qbuf,
417 .vidioc_dqbuf = vb2_ioctl_dqbuf,
418 .vidioc_expbuf = vb2_ioctl_expbuf,
419 .vidioc_enum_input = cal_legacy_enum_input,
420 .vidioc_g_input = cal_legacy_g_input,
421 .vidioc_s_input = cal_legacy_s_input,
422 .vidioc_enum_frameintervals = cal_legacy_enum_frameintervals,
423 .vidioc_streamon = vb2_ioctl_streamon,
424 .vidioc_streamoff = vb2_ioctl_streamoff,
425 .vidioc_log_status = v4l2_ctrl_log_status,
426 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
427 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
428 .vidioc_g_parm = cal_legacy_g_parm,
429 .vidioc_s_parm = cal_legacy_s_parm,
432 /* ------------------------------------------------------------------
433 * V4L2 Media Controller Centric IOCTLs
434 * ------------------------------------------------------------------
437 static int cal_mc_enum_fmt_vid_cap(struct file *file, void *priv,
438 struct v4l2_fmtdesc *f)
443 if (f->index >= cal_num_formats)
448 for (i = 0; i < cal_num_formats; ++i) {
449 if (f->mbus_code && cal_formats[i].code != f->mbus_code)
452 if (idx == f->index) {
453 f->pixelformat = cal_formats[i].fourcc;
454 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
464 static void cal_mc_try_fmt(struct cal_ctx *ctx, struct v4l2_format *f,
465 const struct cal_format_info **info)
467 struct v4l2_pix_format *format = &f->fmt.pix;
468 const struct cal_format_info *fmtinfo;
472 * Default to the first format if the requested pixel format code isn't
475 fmtinfo = cal_format_by_fourcc(f->fmt.pix.pixelformat);
477 fmtinfo = &cal_formats[0];
480 * Clamp the size, update the pixel format. The field and colorspace are
481 * accepted as-is, except for V4L2_FIELD_ANY that is turned into
484 bpp = ALIGN(fmtinfo->bpp, 8);
486 format->width = clamp_t(unsigned int, format->width,
487 CAL_MIN_WIDTH_BYTES * 8 / bpp,
488 CAL_MAX_WIDTH_BYTES * 8 / bpp);
489 format->height = clamp_t(unsigned int, format->height,
490 CAL_MIN_HEIGHT_LINES, CAL_MAX_HEIGHT_LINES);
491 format->pixelformat = fmtinfo->fourcc;
493 if (format->field == V4L2_FIELD_ANY)
494 format->field = V4L2_FIELD_NONE;
497 * Calculate the number of bytes per line and the image size. The
498 * hardware stores the stride as a number of 16 bytes words, in a
499 * signed 15-bit value. Only 14 bits are thus usable.
501 format->bytesperline = ALIGN(clamp(format->bytesperline,
502 format->width * bpp / 8,
503 ((1U << 14) - 1) * 16), 16);
505 format->sizeimage = format->height * format->bytesperline;
507 format->colorspace = ctx->v_fmt.fmt.pix.colorspace;
512 ctx_dbg(3, ctx, "%s: %s %ux%u (bytesperline %u sizeimage %u)\n",
513 __func__, fourcc_to_str(format->pixelformat),
514 format->width, format->height,
515 format->bytesperline, format->sizeimage);
518 static int cal_mc_try_fmt_vid_cap(struct file *file, void *priv,
519 struct v4l2_format *f)
521 struct cal_ctx *ctx = video_drvdata(file);
523 cal_mc_try_fmt(ctx, f, NULL);
527 static int cal_mc_s_fmt_vid_cap(struct file *file, void *priv,
528 struct v4l2_format *f)
530 struct cal_ctx *ctx = video_drvdata(file);
531 const struct cal_format_info *fmtinfo;
533 if (vb2_is_busy(&ctx->vb_vidq)) {
534 ctx_dbg(3, ctx, "%s device busy\n", __func__);
538 cal_mc_try_fmt(ctx, f, &fmtinfo);
541 ctx->fmtinfo = fmtinfo;
546 static int cal_mc_enum_framesizes(struct file *file, void *fh,
547 struct v4l2_frmsizeenum *fsize)
549 struct cal_ctx *ctx = video_drvdata(file);
550 const struct cal_format_info *fmtinfo;
553 if (fsize->index > 0)
556 fmtinfo = cal_format_by_fourcc(fsize->pixel_format);
558 ctx_dbg(3, ctx, "Invalid pixel format 0x%08x\n",
559 fsize->pixel_format);
563 bpp = ALIGN(fmtinfo->bpp, 8);
565 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
566 fsize->stepwise.min_width = CAL_MIN_WIDTH_BYTES * 8 / bpp;
567 fsize->stepwise.max_width = CAL_MAX_WIDTH_BYTES * 8 / bpp;
568 fsize->stepwise.step_width = 64 / bpp;
569 fsize->stepwise.min_height = CAL_MIN_HEIGHT_LINES;
570 fsize->stepwise.max_height = CAL_MAX_HEIGHT_LINES;
571 fsize->stepwise.step_height = 1;
576 static const struct v4l2_ioctl_ops cal_ioctl_mc_ops = {
577 .vidioc_querycap = cal_querycap,
578 .vidioc_enum_fmt_vid_cap = cal_mc_enum_fmt_vid_cap,
579 .vidioc_g_fmt_vid_cap = cal_g_fmt_vid_cap,
580 .vidioc_try_fmt_vid_cap = cal_mc_try_fmt_vid_cap,
581 .vidioc_s_fmt_vid_cap = cal_mc_s_fmt_vid_cap,
582 .vidioc_enum_framesizes = cal_mc_enum_framesizes,
583 .vidioc_reqbufs = vb2_ioctl_reqbufs,
584 .vidioc_create_bufs = vb2_ioctl_create_bufs,
585 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
586 .vidioc_querybuf = vb2_ioctl_querybuf,
587 .vidioc_qbuf = vb2_ioctl_qbuf,
588 .vidioc_dqbuf = vb2_ioctl_dqbuf,
589 .vidioc_expbuf = vb2_ioctl_expbuf,
590 .vidioc_streamon = vb2_ioctl_streamon,
591 .vidioc_streamoff = vb2_ioctl_streamoff,
592 .vidioc_log_status = v4l2_ctrl_log_status,
595 /* ------------------------------------------------------------------
596 * videobuf2 Common Operations
597 * ------------------------------------------------------------------
600 static int cal_queue_setup(struct vb2_queue *vq,
601 unsigned int *nbuffers, unsigned int *nplanes,
602 unsigned int sizes[], struct device *alloc_devs[])
604 struct cal_ctx *ctx = vb2_get_drv_priv(vq);
605 unsigned int size = ctx->v_fmt.fmt.pix.sizeimage;
607 if (vq->num_buffers + *nbuffers < 3)
608 *nbuffers = 3 - vq->num_buffers;
619 ctx_dbg(3, ctx, "nbuffers=%d, size=%d\n", *nbuffers, sizes[0]);
624 static int cal_buffer_prepare(struct vb2_buffer *vb)
626 struct cal_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
627 struct cal_buffer *buf = container_of(vb, struct cal_buffer,
631 size = ctx->v_fmt.fmt.pix.sizeimage;
632 if (vb2_plane_size(vb, 0) < size) {
634 "data will not fit into plane (%lu < %lu)\n",
635 vb2_plane_size(vb, 0), size);
639 vb2_set_plane_payload(&buf->vb.vb2_buf, 0, size);
643 static void cal_buffer_queue(struct vb2_buffer *vb)
645 struct cal_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
646 struct cal_buffer *buf = container_of(vb, struct cal_buffer,
650 /* recheck locking */
651 spin_lock_irqsave(&ctx->dma.lock, flags);
652 list_add_tail(&buf->list, &ctx->dma.queue);
653 spin_unlock_irqrestore(&ctx->dma.lock, flags);
656 static void cal_release_buffers(struct cal_ctx *ctx,
657 enum vb2_buffer_state state)
659 struct cal_buffer *buf, *tmp;
661 /* Release all queued buffers. */
662 spin_lock_irq(&ctx->dma.lock);
664 list_for_each_entry_safe(buf, tmp, &ctx->dma.queue, list) {
665 list_del(&buf->list);
666 vb2_buffer_done(&buf->vb.vb2_buf, state);
669 if (ctx->dma.pending) {
670 vb2_buffer_done(&ctx->dma.pending->vb.vb2_buf, state);
671 ctx->dma.pending = NULL;
674 if (ctx->dma.active) {
675 vb2_buffer_done(&ctx->dma.active->vb.vb2_buf, state);
676 ctx->dma.active = NULL;
679 spin_unlock_irq(&ctx->dma.lock);
682 /* ------------------------------------------------------------------
683 * videobuf2 Operations
684 * ------------------------------------------------------------------
687 static int cal_video_check_format(struct cal_ctx *ctx)
689 const struct v4l2_mbus_framefmt *format;
690 struct media_pad *remote_pad;
692 remote_pad = media_pad_remote_pad_first(&ctx->pad);
696 format = &ctx->phy->formats[remote_pad->index];
698 if (ctx->fmtinfo->code != format->code ||
699 ctx->v_fmt.fmt.pix.height != format->height ||
700 ctx->v_fmt.fmt.pix.width != format->width ||
701 ctx->v_fmt.fmt.pix.field != format->field)
707 static int cal_start_streaming(struct vb2_queue *vq, unsigned int count)
709 struct cal_ctx *ctx = vb2_get_drv_priv(vq);
710 struct cal_buffer *buf;
714 ret = video_device_pipeline_alloc_start(&ctx->vdev);
716 ctx_err(ctx, "Failed to start media pipeline: %d\n", ret);
717 goto error_release_buffers;
721 * Verify that the currently configured format matches the output of
722 * the connected CAMERARX.
724 ret = cal_video_check_format(ctx);
727 "Format mismatch between CAMERARX and video node\n");
731 ret = cal_ctx_prepare(ctx);
733 ctx_err(ctx, "Failed to prepare context: %d\n", ret);
737 spin_lock_irq(&ctx->dma.lock);
738 buf = list_first_entry(&ctx->dma.queue, struct cal_buffer, list);
739 ctx->dma.active = buf;
740 list_del(&buf->list);
741 spin_unlock_irq(&ctx->dma.lock);
743 addr = vb2_dma_contig_plane_dma_addr(&buf->vb.vb2_buf, 0);
745 ret = pm_runtime_resume_and_get(ctx->cal->dev);
749 cal_ctx_set_dma_addr(ctx, addr);
752 ret = v4l2_subdev_call(&ctx->phy->subdev, video, s_stream, 1);
757 cal_quickdump_regs(ctx->cal);
763 pm_runtime_put_sync(ctx->cal->dev);
764 cal_ctx_unprepare(ctx);
767 video_device_pipeline_stop(&ctx->vdev);
768 error_release_buffers:
769 cal_release_buffers(ctx, VB2_BUF_STATE_QUEUED);
774 static void cal_stop_streaming(struct vb2_queue *vq)
776 struct cal_ctx *ctx = vb2_get_drv_priv(vq);
780 v4l2_subdev_call(&ctx->phy->subdev, video, s_stream, 0);
782 pm_runtime_put_sync(ctx->cal->dev);
784 cal_ctx_unprepare(ctx);
786 cal_release_buffers(ctx, VB2_BUF_STATE_ERROR);
788 video_device_pipeline_stop(&ctx->vdev);
791 static const struct vb2_ops cal_video_qops = {
792 .queue_setup = cal_queue_setup,
793 .buf_prepare = cal_buffer_prepare,
794 .buf_queue = cal_buffer_queue,
795 .start_streaming = cal_start_streaming,
796 .stop_streaming = cal_stop_streaming,
797 .wait_prepare = vb2_ops_wait_prepare,
798 .wait_finish = vb2_ops_wait_finish,
801 /* ------------------------------------------------------------------
802 * V4L2 Initialization and Registration
803 * ------------------------------------------------------------------
806 static const struct v4l2_file_operations cal_fops = {
807 .owner = THIS_MODULE,
808 .open = v4l2_fh_open,
809 .release = vb2_fop_release,
810 .poll = vb2_fop_poll,
811 .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */
812 .mmap = vb2_fop_mmap,
815 static int cal_ctx_v4l2_init_formats(struct cal_ctx *ctx)
817 struct v4l2_mbus_framefmt mbus_fmt;
818 const struct cal_format_info *fmtinfo;
819 unsigned int i, j, k;
822 /* Enumerate sub device formats and enable all matching local formats */
823 ctx->active_fmt = devm_kcalloc(ctx->cal->dev, cal_num_formats,
824 sizeof(*ctx->active_fmt), GFP_KERNEL);
825 if (!ctx->active_fmt)
828 ctx->num_active_fmt = 0;
830 for (j = 0, i = 0; ; ++j) {
831 struct v4l2_subdev_mbus_code_enum mbus_code = {
833 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
836 ret = v4l2_subdev_call(ctx->phy->source, pad, enum_mbus_code,
842 ctx_err(ctx, "Error enumerating mbus codes in subdev %s: %d\n",
843 ctx->phy->source->name, ret);
848 "subdev %s: code: %04x idx: %u\n",
849 ctx->phy->source->name, mbus_code.code, j);
851 for (k = 0; k < cal_num_formats; k++) {
852 fmtinfo = &cal_formats[k];
854 if (mbus_code.code == fmtinfo->code) {
855 ctx->active_fmt[i] = fmtinfo;
857 "matched fourcc: %s: code: %04x idx: %u\n",
858 fourcc_to_str(fmtinfo->fourcc),
860 ctx->num_active_fmt = ++i;
866 ctx_err(ctx, "No suitable format reported by subdev %s\n",
867 ctx->phy->source->name);
871 ret = __subdev_get_format(ctx, &mbus_fmt);
875 fmtinfo = find_format_by_code(ctx, mbus_fmt.code);
877 ctx_dbg(3, ctx, "mbus code format (0x%08x) not found.\n",
882 /* Save current format */
883 v4l2_fill_pix_format(&ctx->v_fmt.fmt.pix, &mbus_fmt);
884 ctx->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
885 ctx->v_fmt.fmt.pix.pixelformat = fmtinfo->fourcc;
886 cal_calc_format_size(ctx, fmtinfo, &ctx->v_fmt);
887 ctx->fmtinfo = fmtinfo;
892 static int cal_ctx_v4l2_init_mc_format(struct cal_ctx *ctx)
894 const struct cal_format_info *fmtinfo;
895 struct v4l2_pix_format *pix_fmt = &ctx->v_fmt.fmt.pix;
897 fmtinfo = cal_format_by_code(MEDIA_BUS_FMT_UYVY8_1X16);
901 pix_fmt->width = 640;
902 pix_fmt->height = 480;
903 pix_fmt->field = V4L2_FIELD_NONE;
904 pix_fmt->colorspace = V4L2_COLORSPACE_SRGB;
905 pix_fmt->ycbcr_enc = V4L2_YCBCR_ENC_601;
906 pix_fmt->quantization = V4L2_QUANTIZATION_LIM_RANGE;
907 pix_fmt->xfer_func = V4L2_XFER_FUNC_SRGB;
908 pix_fmt->pixelformat = fmtinfo->fourcc;
910 ctx->v_fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
912 /* Save current format */
913 cal_calc_format_size(ctx, fmtinfo, &ctx->v_fmt);
914 ctx->fmtinfo = fmtinfo;
919 int cal_ctx_v4l2_register(struct cal_ctx *ctx)
921 struct video_device *vfd = &ctx->vdev;
925 struct v4l2_ctrl_handler *hdl = &ctx->ctrl_handler;
927 ret = cal_ctx_v4l2_init_formats(ctx);
929 ctx_err(ctx, "Failed to init formats: %d\n", ret);
933 ret = v4l2_ctrl_add_handler(hdl, ctx->phy->source->ctrl_handler,
936 ctx_err(ctx, "Failed to add source ctrl handler\n");
940 ret = cal_ctx_v4l2_init_mc_format(ctx);
942 ctx_err(ctx, "Failed to init format: %d\n", ret);
947 ret = video_register_device(vfd, VFL_TYPE_VIDEO, cal_video_nr);
949 ctx_err(ctx, "Failed to register video device\n");
953 ret = media_create_pad_link(&ctx->phy->subdev.entity,
954 CAL_CAMERARX_PAD_FIRST_SOURCE,
956 MEDIA_LNK_FL_IMMUTABLE |
957 MEDIA_LNK_FL_ENABLED);
959 ctx_err(ctx, "Failed to create media link for context %u\n",
961 video_unregister_device(vfd);
965 ctx_info(ctx, "V4L2 device registered as %s\n",
966 video_device_node_name(vfd));
971 void cal_ctx_v4l2_unregister(struct cal_ctx *ctx)
973 ctx_dbg(1, ctx, "unregistering %s\n",
974 video_device_node_name(&ctx->vdev));
976 video_unregister_device(&ctx->vdev);
979 int cal_ctx_v4l2_init(struct cal_ctx *ctx)
981 struct video_device *vfd = &ctx->vdev;
982 struct vb2_queue *q = &ctx->vb_vidq;
985 INIT_LIST_HEAD(&ctx->dma.queue);
986 spin_lock_init(&ctx->dma.lock);
987 mutex_init(&ctx->mutex);
988 init_waitqueue_head(&ctx->dma.wait);
990 /* Initialize the vb2 queue. */
991 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
992 q->io_modes = VB2_MMAP | VB2_DMABUF;
994 q->buf_struct_size = sizeof(struct cal_buffer);
995 q->ops = &cal_video_qops;
996 q->mem_ops = &vb2_dma_contig_memops;
997 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
998 q->lock = &ctx->mutex;
999 q->min_buffers_needed = 3;
1000 q->dev = ctx->cal->dev;
1002 ret = vb2_queue_init(q);
1006 /* Initialize the video device and media entity. */
1007 vfd->fops = &cal_fops;
1008 vfd->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING
1009 | (cal_mc_api ? V4L2_CAP_IO_MC : 0);
1010 vfd->v4l2_dev = &ctx->cal->v4l2_dev;
1012 snprintf(vfd->name, sizeof(vfd->name), "CAL output %u", ctx->dma_ctx);
1013 vfd->release = video_device_release_empty;
1014 vfd->ioctl_ops = cal_mc_api ? &cal_ioctl_mc_ops : &cal_ioctl_legacy_ops;
1015 vfd->lock = &ctx->mutex;
1016 video_set_drvdata(vfd, ctx);
1018 ctx->pad.flags = MEDIA_PAD_FL_SINK;
1019 ret = media_entity_pads_init(&vfd->entity, 1, &ctx->pad);
1024 /* Initialize the control handler. */
1025 struct v4l2_ctrl_handler *hdl = &ctx->ctrl_handler;
1027 ret = v4l2_ctrl_handler_init(hdl, 11);
1029 ctx_err(ctx, "Failed to init ctrl handler\n");
1033 vfd->ctrl_handler = hdl;
1039 media_entity_cleanup(&vfd->entity);
1043 void cal_ctx_v4l2_cleanup(struct cal_ctx *ctx)
1046 v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1048 media_entity_cleanup(&ctx->vdev.entity);