1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for Renesas R-Car VIN
5 * Copyright (C) 2016 Renesas Electronics Corp.
6 * Copyright (C) 2011-2013 Renesas Solutions Corp.
7 * Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com>
8 * Copyright (C) 2008 Magnus Damm
10 * Based on the soc-camera rcar_vin driver
13 #include <linux/pm_runtime.h>
15 #include <media/v4l2-event.h>
16 #include <media/v4l2-ioctl.h>
17 #include <media/v4l2-mc.h>
18 #include <media/v4l2-rect.h>
22 #define RVIN_DEFAULT_FORMAT V4L2_PIX_FMT_YUYV
23 #define RVIN_DEFAULT_WIDTH 800
24 #define RVIN_DEFAULT_HEIGHT 600
25 #define RVIN_DEFAULT_FIELD V4L2_FIELD_NONE
26 #define RVIN_DEFAULT_COLORSPACE V4L2_COLORSPACE_SRGB
28 /* -----------------------------------------------------------------------------
32 static const struct rvin_video_format rvin_formats[] = {
34 .fourcc = V4L2_PIX_FMT_NV16,
38 .fourcc = V4L2_PIX_FMT_YUYV,
42 .fourcc = V4L2_PIX_FMT_UYVY,
46 .fourcc = V4L2_PIX_FMT_RGB565,
50 .fourcc = V4L2_PIX_FMT_XRGB555,
54 .fourcc = V4L2_PIX_FMT_XBGR32,
59 const struct rvin_video_format *rvin_format_from_pixel(u32 pixelformat)
63 for (i = 0; i < ARRAY_SIZE(rvin_formats); i++)
64 if (rvin_formats[i].fourcc == pixelformat)
65 return rvin_formats + i;
70 static u32 rvin_format_bytesperline(struct v4l2_pix_format *pix)
72 const struct rvin_video_format *fmt;
74 fmt = rvin_format_from_pixel(pix->pixelformat);
79 return pix->width * fmt->bpp;
82 static u32 rvin_format_sizeimage(struct v4l2_pix_format *pix)
84 if (pix->pixelformat == V4L2_PIX_FMT_NV16)
85 return pix->bytesperline * pix->height * 2;
87 return pix->bytesperline * pix->height;
90 static void rvin_format_align(struct rvin_dev *vin, struct v4l2_pix_format *pix)
94 if (!rvin_format_from_pixel(pix->pixelformat) ||
95 (vin->info->model == RCAR_M1 &&
96 pix->pixelformat == V4L2_PIX_FMT_XBGR32))
97 pix->pixelformat = RVIN_DEFAULT_FORMAT;
101 case V4L2_FIELD_BOTTOM:
102 case V4L2_FIELD_NONE:
103 case V4L2_FIELD_INTERLACED_TB:
104 case V4L2_FIELD_INTERLACED_BT:
105 case V4L2_FIELD_INTERLACED:
107 case V4L2_FIELD_ALTERNATE:
109 * Driver does not (yet) support outputting ALTERNATE to a
110 * userspace. It does support outputting INTERLACED so use
111 * the VIN hardware to combine the two fields.
113 pix->field = V4L2_FIELD_INTERLACED;
117 pix->field = RVIN_DEFAULT_FIELD;
121 /* HW limit width to a multiple of 32 (2^5) for NV16 else 2 (2^1) */
122 walign = vin->format.pixelformat == V4L2_PIX_FMT_NV16 ? 5 : 1;
124 /* Limit to VIN capabilities */
125 v4l_bound_align_image(&pix->width, 2, vin->info->max_width, walign,
126 &pix->height, 4, vin->info->max_height, 2, 0);
128 pix->bytesperline = rvin_format_bytesperline(pix);
129 pix->sizeimage = rvin_format_sizeimage(pix);
131 vin_dbg(vin, "Format %ux%u bpl: %u size: %u\n",
132 pix->width, pix->height, pix->bytesperline, pix->sizeimage);
135 /* -----------------------------------------------------------------------------
139 static int rvin_reset_format(struct rvin_dev *vin)
141 struct v4l2_subdev_format fmt = {
142 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
143 .pad = vin->parallel->source_pad,
147 ret = v4l2_subdev_call(vin_to_source(vin), pad, get_fmt, NULL, &fmt);
151 v4l2_fill_pix_format(&vin->format, &fmt.format);
153 rvin_format_align(vin, &vin->format);
156 vin->source.left = 0;
157 vin->source.width = vin->format.width;
158 vin->source.height = vin->format.height;
160 vin->crop = vin->source;
161 vin->compose = vin->source;
166 static int rvin_try_format(struct rvin_dev *vin, u32 which,
167 struct v4l2_pix_format *pix,
168 struct v4l2_rect *crop, struct v4l2_rect *compose)
170 struct v4l2_subdev *sd = vin_to_source(vin);
171 struct v4l2_subdev_pad_config *pad_cfg;
172 struct v4l2_subdev_format format = {
174 .pad = vin->parallel->source_pad,
176 enum v4l2_field field;
180 pad_cfg = v4l2_subdev_alloc_pad_config(sd);
184 if (!rvin_format_from_pixel(pix->pixelformat) ||
185 (vin->info->model == RCAR_M1 &&
186 pix->pixelformat == V4L2_PIX_FMT_XBGR32))
187 pix->pixelformat = RVIN_DEFAULT_FORMAT;
189 v4l2_fill_mbus_format(&format.format, pix, vin->mbus_code);
191 /* Allow the video device to override field and to scale */
194 height = pix->height;
196 ret = v4l2_subdev_call(sd, pad, set_fmt, pad_cfg, &format);
197 if (ret < 0 && ret != -ENOIOCTLCMD)
201 v4l2_fill_pix_format(pix, &format.format);
206 crop->width = pix->width;
207 crop->height = pix->height;
210 * If source is ALTERNATE the driver will use the VIN hardware
211 * to INTERLACE it. The crop height then needs to be doubled.
213 if (pix->field == V4L2_FIELD_ALTERNATE)
217 if (field != V4L2_FIELD_ANY)
221 pix->height = height;
223 rvin_format_align(vin, pix);
228 compose->width = pix->width;
229 compose->height = pix->height;
232 v4l2_subdev_free_pad_config(pad_cfg);
237 static int rvin_querycap(struct file *file, void *priv,
238 struct v4l2_capability *cap)
240 struct rvin_dev *vin = video_drvdata(file);
242 strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
243 strlcpy(cap->card, "R_Car_VIN", sizeof(cap->card));
244 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
249 static int rvin_try_fmt_vid_cap(struct file *file, void *priv,
250 struct v4l2_format *f)
252 struct rvin_dev *vin = video_drvdata(file);
254 return rvin_try_format(vin, V4L2_SUBDEV_FORMAT_TRY, &f->fmt.pix, NULL,
258 static int rvin_s_fmt_vid_cap(struct file *file, void *priv,
259 struct v4l2_format *f)
261 struct rvin_dev *vin = video_drvdata(file);
262 struct v4l2_rect crop, compose;
265 if (vb2_is_busy(&vin->queue))
268 ret = rvin_try_format(vin, V4L2_SUBDEV_FORMAT_ACTIVE, &f->fmt.pix,
273 vin->format = f->fmt.pix;
275 vin->compose = compose;
281 static int rvin_g_fmt_vid_cap(struct file *file, void *priv,
282 struct v4l2_format *f)
284 struct rvin_dev *vin = video_drvdata(file);
286 f->fmt.pix = vin->format;
291 static int rvin_enum_fmt_vid_cap(struct file *file, void *priv,
292 struct v4l2_fmtdesc *f)
294 if (f->index >= ARRAY_SIZE(rvin_formats))
297 f->pixelformat = rvin_formats[f->index].fourcc;
302 static int rvin_g_selection(struct file *file, void *fh,
303 struct v4l2_selection *s)
305 struct rvin_dev *vin = video_drvdata(file);
307 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
311 case V4L2_SEL_TGT_CROP_BOUNDS:
312 case V4L2_SEL_TGT_CROP_DEFAULT:
313 s->r.left = s->r.top = 0;
314 s->r.width = vin->source.width;
315 s->r.height = vin->source.height;
317 case V4L2_SEL_TGT_CROP:
320 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
321 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
322 s->r.left = s->r.top = 0;
323 s->r.width = vin->format.width;
324 s->r.height = vin->format.height;
326 case V4L2_SEL_TGT_COMPOSE:
336 static int rvin_s_selection(struct file *file, void *fh,
337 struct v4l2_selection *s)
339 struct rvin_dev *vin = video_drvdata(file);
340 const struct rvin_video_format *fmt;
341 struct v4l2_rect r = s->r;
342 struct v4l2_rect max_rect;
343 struct v4l2_rect min_rect = {
348 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
351 v4l2_rect_set_min_size(&r, &min_rect);
354 case V4L2_SEL_TGT_CROP:
355 /* Can't crop outside of source input */
356 max_rect.top = max_rect.left = 0;
357 max_rect.width = vin->source.width;
358 max_rect.height = vin->source.height;
359 v4l2_rect_map_inside(&r, &max_rect);
361 v4l_bound_align_image(&r.width, 6, vin->source.width, 0,
362 &r.height, 2, vin->source.height, 0, 0);
364 r.top = clamp_t(s32, r.top, 0, vin->source.height - r.height);
365 r.left = clamp_t(s32, r.left, 0, vin->source.width - r.width);
367 vin->crop = s->r = r;
369 vin_dbg(vin, "Cropped %dx%d@%d:%d of %dx%d\n",
370 r.width, r.height, r.left, r.top,
371 vin->source.width, vin->source.height);
373 case V4L2_SEL_TGT_COMPOSE:
374 /* Make sure compose rect fits inside output format */
375 max_rect.top = max_rect.left = 0;
376 max_rect.width = vin->format.width;
377 max_rect.height = vin->format.height;
378 v4l2_rect_map_inside(&r, &max_rect);
381 * Composing is done by adding a offset to the buffer address,
382 * the HW wants this address to be aligned to HW_BUFFER_MASK.
383 * Make sure the top and left values meets this requirement.
385 while ((r.top * vin->format.bytesperline) & HW_BUFFER_MASK)
388 fmt = rvin_format_from_pixel(vin->format.pixelformat);
389 while ((r.left * fmt->bpp) & HW_BUFFER_MASK)
392 vin->compose = s->r = r;
394 vin_dbg(vin, "Compose %dx%d@%d:%d in %dx%d\n",
395 r.width, r.height, r.left, r.top,
396 vin->format.width, vin->format.height);
402 /* HW supports modifying configuration while running */
403 rvin_crop_scale_comp(vin);
408 static int rvin_cropcap(struct file *file, void *priv,
409 struct v4l2_cropcap *crop)
411 struct rvin_dev *vin = video_drvdata(file);
412 struct v4l2_subdev *sd = vin_to_source(vin);
414 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
417 return v4l2_subdev_call(sd, video, g_pixelaspect, &crop->pixelaspect);
420 static int rvin_enum_input(struct file *file, void *priv,
421 struct v4l2_input *i)
423 struct rvin_dev *vin = video_drvdata(file);
424 struct v4l2_subdev *sd = vin_to_source(vin);
430 ret = v4l2_subdev_call(sd, video, g_input_status, &i->status);
431 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
434 i->type = V4L2_INPUT_TYPE_CAMERA;
436 if (v4l2_subdev_has_op(sd, pad, dv_timings_cap)) {
437 i->capabilities = V4L2_IN_CAP_DV_TIMINGS;
440 i->capabilities = V4L2_IN_CAP_STD;
441 i->std = vin->vdev.tvnorms;
444 strlcpy(i->name, "Camera", sizeof(i->name));
449 static int rvin_g_input(struct file *file, void *priv, unsigned int *i)
455 static int rvin_s_input(struct file *file, void *priv, unsigned int i)
462 static int rvin_querystd(struct file *file, void *priv, v4l2_std_id *a)
464 struct rvin_dev *vin = video_drvdata(file);
465 struct v4l2_subdev *sd = vin_to_source(vin);
467 return v4l2_subdev_call(sd, video, querystd, a);
470 static int rvin_s_std(struct file *file, void *priv, v4l2_std_id a)
472 struct rvin_dev *vin = video_drvdata(file);
475 ret = v4l2_subdev_call(vin_to_source(vin), video, s_std, a);
481 /* Changing the standard will change the width/height */
482 return rvin_reset_format(vin);
485 static int rvin_g_std(struct file *file, void *priv, v4l2_std_id *a)
487 struct rvin_dev *vin = video_drvdata(file);
489 if (v4l2_subdev_has_op(vin_to_source(vin), pad, dv_timings_cap))
497 static int rvin_subscribe_event(struct v4l2_fh *fh,
498 const struct v4l2_event_subscription *sub)
501 case V4L2_EVENT_SOURCE_CHANGE:
502 return v4l2_event_subscribe(fh, sub, 4, NULL);
504 return v4l2_ctrl_subscribe_event(fh, sub);
507 static int rvin_enum_dv_timings(struct file *file, void *priv_fh,
508 struct v4l2_enum_dv_timings *timings)
510 struct rvin_dev *vin = video_drvdata(file);
511 struct v4l2_subdev *sd = vin_to_source(vin);
517 timings->pad = vin->parallel->sink_pad;
519 ret = v4l2_subdev_call(sd, pad, enum_dv_timings, timings);
526 static int rvin_s_dv_timings(struct file *file, void *priv_fh,
527 struct v4l2_dv_timings *timings)
529 struct rvin_dev *vin = video_drvdata(file);
530 struct v4l2_subdev *sd = vin_to_source(vin);
533 ret = v4l2_subdev_call(sd, video, s_dv_timings, timings);
537 /* Changing the timings will change the width/height */
538 return rvin_reset_format(vin);
541 static int rvin_g_dv_timings(struct file *file, void *priv_fh,
542 struct v4l2_dv_timings *timings)
544 struct rvin_dev *vin = video_drvdata(file);
545 struct v4l2_subdev *sd = vin_to_source(vin);
547 return v4l2_subdev_call(sd, video, g_dv_timings, timings);
550 static int rvin_query_dv_timings(struct file *file, void *priv_fh,
551 struct v4l2_dv_timings *timings)
553 struct rvin_dev *vin = video_drvdata(file);
554 struct v4l2_subdev *sd = vin_to_source(vin);
556 return v4l2_subdev_call(sd, video, query_dv_timings, timings);
559 static int rvin_dv_timings_cap(struct file *file, void *priv_fh,
560 struct v4l2_dv_timings_cap *cap)
562 struct rvin_dev *vin = video_drvdata(file);
563 struct v4l2_subdev *sd = vin_to_source(vin);
569 cap->pad = vin->parallel->sink_pad;
571 ret = v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
578 static int rvin_g_edid(struct file *file, void *fh, struct v4l2_edid *edid)
580 struct rvin_dev *vin = video_drvdata(file);
581 struct v4l2_subdev *sd = vin_to_source(vin);
587 edid->pad = vin->parallel->sink_pad;
589 ret = v4l2_subdev_call(sd, pad, get_edid, edid);
596 static int rvin_s_edid(struct file *file, void *fh, struct v4l2_edid *edid)
598 struct rvin_dev *vin = video_drvdata(file);
599 struct v4l2_subdev *sd = vin_to_source(vin);
605 edid->pad = vin->parallel->sink_pad;
607 ret = v4l2_subdev_call(sd, pad, set_edid, edid);
614 static const struct v4l2_ioctl_ops rvin_ioctl_ops = {
615 .vidioc_querycap = rvin_querycap,
616 .vidioc_try_fmt_vid_cap = rvin_try_fmt_vid_cap,
617 .vidioc_g_fmt_vid_cap = rvin_g_fmt_vid_cap,
618 .vidioc_s_fmt_vid_cap = rvin_s_fmt_vid_cap,
619 .vidioc_enum_fmt_vid_cap = rvin_enum_fmt_vid_cap,
621 .vidioc_g_selection = rvin_g_selection,
622 .vidioc_s_selection = rvin_s_selection,
624 .vidioc_cropcap = rvin_cropcap,
626 .vidioc_enum_input = rvin_enum_input,
627 .vidioc_g_input = rvin_g_input,
628 .vidioc_s_input = rvin_s_input,
630 .vidioc_dv_timings_cap = rvin_dv_timings_cap,
631 .vidioc_enum_dv_timings = rvin_enum_dv_timings,
632 .vidioc_g_dv_timings = rvin_g_dv_timings,
633 .vidioc_s_dv_timings = rvin_s_dv_timings,
634 .vidioc_query_dv_timings = rvin_query_dv_timings,
636 .vidioc_g_edid = rvin_g_edid,
637 .vidioc_s_edid = rvin_s_edid,
639 .vidioc_querystd = rvin_querystd,
640 .vidioc_g_std = rvin_g_std,
641 .vidioc_s_std = rvin_s_std,
643 .vidioc_reqbufs = vb2_ioctl_reqbufs,
644 .vidioc_create_bufs = vb2_ioctl_create_bufs,
645 .vidioc_querybuf = vb2_ioctl_querybuf,
646 .vidioc_qbuf = vb2_ioctl_qbuf,
647 .vidioc_dqbuf = vb2_ioctl_dqbuf,
648 .vidioc_expbuf = vb2_ioctl_expbuf,
649 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
650 .vidioc_streamon = vb2_ioctl_streamon,
651 .vidioc_streamoff = vb2_ioctl_streamoff,
653 .vidioc_log_status = v4l2_ctrl_log_status,
654 .vidioc_subscribe_event = rvin_subscribe_event,
655 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
658 /* -----------------------------------------------------------------------------
659 * V4L2 Media Controller
662 static void rvin_mc_try_format(struct rvin_dev *vin,
663 struct v4l2_pix_format *pix)
666 * The V4L2 specification clearly documents the colorspace fields
667 * as being set by drivers for capture devices. Using the values
668 * supplied by userspace thus wouldn't comply with the API. Until
669 * the API is updated force fixed vaules.
671 pix->colorspace = RVIN_DEFAULT_COLORSPACE;
672 pix->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(pix->colorspace);
673 pix->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(pix->colorspace);
674 pix->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true, pix->colorspace,
677 rvin_format_align(vin, pix);
680 static int rvin_mc_try_fmt_vid_cap(struct file *file, void *priv,
681 struct v4l2_format *f)
683 struct rvin_dev *vin = video_drvdata(file);
685 rvin_mc_try_format(vin, &f->fmt.pix);
690 static int rvin_mc_s_fmt_vid_cap(struct file *file, void *priv,
691 struct v4l2_format *f)
693 struct rvin_dev *vin = video_drvdata(file);
695 if (vb2_is_busy(&vin->queue))
698 rvin_mc_try_format(vin, &f->fmt.pix);
700 vin->format = f->fmt.pix;
704 vin->crop.width = vin->format.width;
705 vin->crop.height = vin->format.height;
706 vin->compose = vin->crop;
711 static int rvin_mc_enum_input(struct file *file, void *priv,
712 struct v4l2_input *i)
717 i->type = V4L2_INPUT_TYPE_CAMERA;
718 strlcpy(i->name, "Camera", sizeof(i->name));
723 static const struct v4l2_ioctl_ops rvin_mc_ioctl_ops = {
724 .vidioc_querycap = rvin_querycap,
725 .vidioc_try_fmt_vid_cap = rvin_mc_try_fmt_vid_cap,
726 .vidioc_g_fmt_vid_cap = rvin_g_fmt_vid_cap,
727 .vidioc_s_fmt_vid_cap = rvin_mc_s_fmt_vid_cap,
728 .vidioc_enum_fmt_vid_cap = rvin_enum_fmt_vid_cap,
730 .vidioc_enum_input = rvin_mc_enum_input,
731 .vidioc_g_input = rvin_g_input,
732 .vidioc_s_input = rvin_s_input,
734 .vidioc_reqbufs = vb2_ioctl_reqbufs,
735 .vidioc_create_bufs = vb2_ioctl_create_bufs,
736 .vidioc_querybuf = vb2_ioctl_querybuf,
737 .vidioc_qbuf = vb2_ioctl_qbuf,
738 .vidioc_dqbuf = vb2_ioctl_dqbuf,
739 .vidioc_expbuf = vb2_ioctl_expbuf,
740 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
741 .vidioc_streamon = vb2_ioctl_streamon,
742 .vidioc_streamoff = vb2_ioctl_streamoff,
744 .vidioc_log_status = v4l2_ctrl_log_status,
745 .vidioc_subscribe_event = rvin_subscribe_event,
746 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
749 /* -----------------------------------------------------------------------------
753 static int rvin_power_on(struct rvin_dev *vin)
756 struct v4l2_subdev *sd = vin_to_source(vin);
758 pm_runtime_get_sync(vin->v4l2_dev.dev);
760 ret = v4l2_subdev_call(sd, core, s_power, 1);
761 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
766 static int rvin_power_off(struct rvin_dev *vin)
769 struct v4l2_subdev *sd = vin_to_source(vin);
771 ret = v4l2_subdev_call(sd, core, s_power, 0);
773 pm_runtime_put(vin->v4l2_dev.dev);
775 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
781 static int rvin_initialize_device(struct file *file)
783 struct rvin_dev *vin = video_drvdata(file);
786 struct v4l2_format f = {
787 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
789 .width = vin->format.width,
790 .height = vin->format.height,
791 .field = vin->format.field,
792 .colorspace = vin->format.colorspace,
793 .pixelformat = vin->format.pixelformat,
797 ret = rvin_power_on(vin);
801 pm_runtime_enable(&vin->vdev.dev);
802 ret = pm_runtime_resume(&vin->vdev.dev);
803 if (ret < 0 && ret != -ENOSYS)
807 * Try to configure with default parameters. Notice: this is the
808 * very first open, so, we cannot race against other calls,
809 * apart from someone else calling open() simultaneously, but
810 * .host_lock is protecting us against it.
812 ret = rvin_s_fmt_vid_cap(file, NULL, &f);
816 v4l2_ctrl_handler_setup(&vin->ctrl_handler);
820 pm_runtime_disable(&vin->vdev.dev);
827 static int rvin_open(struct file *file)
829 struct rvin_dev *vin = video_drvdata(file);
832 mutex_lock(&vin->lock);
834 file->private_data = vin;
836 ret = v4l2_fh_open(file);
840 if (!v4l2_fh_is_singular_file(file))
843 if (rvin_initialize_device(file)) {
844 v4l2_fh_release(file);
849 mutex_unlock(&vin->lock);
853 static int rvin_release(struct file *file)
855 struct rvin_dev *vin = video_drvdata(file);
859 mutex_lock(&vin->lock);
861 /* Save the singular status before we call the clean-up helper */
862 fh_singular = v4l2_fh_is_singular_file(file);
864 /* the release helper will cleanup any on-going streaming */
865 ret = _vb2_fop_release(file, NULL);
868 * If this was the last open file.
869 * Then de-initialize hw module.
872 pm_runtime_suspend(&vin->vdev.dev);
873 pm_runtime_disable(&vin->vdev.dev);
877 mutex_unlock(&vin->lock);
882 static const struct v4l2_file_operations rvin_fops = {
883 .owner = THIS_MODULE,
884 .unlocked_ioctl = video_ioctl2,
886 .release = rvin_release,
887 .poll = vb2_fop_poll,
888 .mmap = vb2_fop_mmap,
889 .read = vb2_fop_read,
892 /* -----------------------------------------------------------------------------
893 * Media controller file operations
896 static int rvin_mc_open(struct file *file)
898 struct rvin_dev *vin = video_drvdata(file);
901 ret = mutex_lock_interruptible(&vin->lock);
905 ret = pm_runtime_get_sync(vin->dev);
909 ret = v4l2_pipeline_pm_use(&vin->vdev.entity, 1);
913 file->private_data = vin;
915 ret = v4l2_fh_open(file);
919 mutex_unlock(&vin->lock);
923 v4l2_pipeline_pm_use(&vin->vdev.entity, 0);
925 pm_runtime_put(vin->dev);
927 mutex_unlock(&vin->lock);
932 static int rvin_mc_release(struct file *file)
934 struct rvin_dev *vin = video_drvdata(file);
937 mutex_lock(&vin->lock);
939 /* the release helper will cleanup any on-going streaming. */
940 ret = _vb2_fop_release(file, NULL);
942 v4l2_pipeline_pm_use(&vin->vdev.entity, 0);
943 pm_runtime_put(vin->dev);
945 mutex_unlock(&vin->lock);
950 static const struct v4l2_file_operations rvin_mc_fops = {
951 .owner = THIS_MODULE,
952 .unlocked_ioctl = video_ioctl2,
953 .open = rvin_mc_open,
954 .release = rvin_mc_release,
955 .poll = vb2_fop_poll,
956 .mmap = vb2_fop_mmap,
957 .read = vb2_fop_read,
960 void rvin_v4l2_unregister(struct rvin_dev *vin)
962 if (!video_is_registered(&vin->vdev))
965 v4l2_info(&vin->v4l2_dev, "Removing %s\n",
966 video_device_node_name(&vin->vdev));
968 /* Checks internaly if vdev have been init or not */
969 video_unregister_device(&vin->vdev);
972 static void rvin_notify(struct v4l2_subdev *sd,
973 unsigned int notification, void *arg)
975 struct rvin_dev *vin =
976 container_of(sd->v4l2_dev, struct rvin_dev, v4l2_dev);
978 switch (notification) {
979 case V4L2_DEVICE_NOTIFY_EVENT:
980 v4l2_event_queue(&vin->vdev, arg);
987 int rvin_v4l2_register(struct rvin_dev *vin)
989 struct video_device *vdev = &vin->vdev;
992 vin->v4l2_dev.notify = rvin_notify;
995 vdev->v4l2_dev = &vin->v4l2_dev;
996 vdev->queue = &vin->queue;
997 snprintf(vdev->name, sizeof(vdev->name), "VIN%u output", vin->id);
998 vdev->release = video_device_release_empty;
999 vdev->lock = &vin->lock;
1000 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
1003 /* Set a default format */
1004 vin->format.pixelformat = RVIN_DEFAULT_FORMAT;
1005 vin->format.width = RVIN_DEFAULT_WIDTH;
1006 vin->format.height = RVIN_DEFAULT_HEIGHT;
1007 vin->format.field = RVIN_DEFAULT_FIELD;
1008 vin->format.colorspace = RVIN_DEFAULT_COLORSPACE;
1010 if (vin->info->use_mc) {
1011 vdev->fops = &rvin_mc_fops;
1012 vdev->ioctl_ops = &rvin_mc_ioctl_ops;
1014 vdev->fops = &rvin_fops;
1015 vdev->ioctl_ops = &rvin_ioctl_ops;
1016 rvin_reset_format(vin);
1019 rvin_format_align(vin, &vin->format);
1021 ret = video_register_device(&vin->vdev, VFL_TYPE_GRABBER, -1);
1023 vin_err(vin, "Failed to register video device\n");
1027 video_set_drvdata(&vin->vdev, vin);
1029 v4l2_info(&vin->v4l2_dev, "Device registered as %s\n",
1030 video_device_node_name(&vin->vdev));