2 * Driver for Renesas R-Car VIN
4 * Copyright (C) 2016 Renesas Electronics Corp.
5 * Copyright (C) 2011-2013 Renesas Solutions Corp.
6 * Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com>
7 * Copyright (C) 2008 Magnus Damm
9 * Based on the soc-camera rcar_vin driver
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
17 #include <linux/pm_runtime.h>
19 #include <media/v4l2-event.h>
20 #include <media/v4l2-ioctl.h>
21 #include <media/v4l2-mc.h>
22 #include <media/v4l2-rect.h>
26 #define RVIN_DEFAULT_FORMAT V4L2_PIX_FMT_YUYV
27 #define RVIN_DEFAULT_WIDTH 800
28 #define RVIN_DEFAULT_HEIGHT 600
29 #define RVIN_DEFAULT_FIELD V4L2_FIELD_NONE
30 #define RVIN_DEFAULT_COLORSPACE V4L2_COLORSPACE_SRGB
32 /* -----------------------------------------------------------------------------
36 static const struct rvin_video_format rvin_formats[] = {
38 .fourcc = V4L2_PIX_FMT_NV16,
42 .fourcc = V4L2_PIX_FMT_YUYV,
46 .fourcc = V4L2_PIX_FMT_UYVY,
50 .fourcc = V4L2_PIX_FMT_RGB565,
54 .fourcc = V4L2_PIX_FMT_XRGB555,
58 .fourcc = V4L2_PIX_FMT_XBGR32,
63 const struct rvin_video_format *rvin_format_from_pixel(u32 pixelformat)
67 for (i = 0; i < ARRAY_SIZE(rvin_formats); i++)
68 if (rvin_formats[i].fourcc == pixelformat)
69 return rvin_formats + i;
74 static u32 rvin_format_bytesperline(struct v4l2_pix_format *pix)
76 const struct rvin_video_format *fmt;
78 fmt = rvin_format_from_pixel(pix->pixelformat);
83 return pix->width * fmt->bpp;
86 static u32 rvin_format_sizeimage(struct v4l2_pix_format *pix)
88 if (pix->pixelformat == V4L2_PIX_FMT_NV16)
89 return pix->bytesperline * pix->height * 2;
91 return pix->bytesperline * pix->height;
94 static void rvin_format_align(struct rvin_dev *vin, struct v4l2_pix_format *pix)
98 if (!rvin_format_from_pixel(pix->pixelformat) ||
99 (vin->info->model == RCAR_M1 &&
100 pix->pixelformat == V4L2_PIX_FMT_XBGR32))
101 pix->pixelformat = RVIN_DEFAULT_FORMAT;
103 switch (pix->field) {
105 case V4L2_FIELD_BOTTOM:
106 case V4L2_FIELD_NONE:
107 case V4L2_FIELD_INTERLACED_TB:
108 case V4L2_FIELD_INTERLACED_BT:
109 case V4L2_FIELD_INTERLACED:
111 case V4L2_FIELD_ALTERNATE:
113 * Driver does not (yet) support outputting ALTERNATE to a
114 * userspace. It does support outputting INTERLACED so use
115 * the VIN hardware to combine the two fields.
117 pix->field = V4L2_FIELD_INTERLACED;
121 pix->field = RVIN_DEFAULT_FIELD;
125 /* HW limit width to a multiple of 32 (2^5) for NV16 else 2 (2^1) */
126 walign = vin->format.pixelformat == V4L2_PIX_FMT_NV16 ? 5 : 1;
128 /* Limit to VIN capabilities */
129 v4l_bound_align_image(&pix->width, 2, vin->info->max_width, walign,
130 &pix->height, 4, vin->info->max_height, 2, 0);
132 pix->bytesperline = rvin_format_bytesperline(pix);
133 pix->sizeimage = rvin_format_sizeimage(pix);
135 vin_dbg(vin, "Format %ux%u bpl: %u size: %u\n",
136 pix->width, pix->height, pix->bytesperline, pix->sizeimage);
139 /* -----------------------------------------------------------------------------
143 static int rvin_reset_format(struct rvin_dev *vin)
145 struct v4l2_subdev_format fmt = {
146 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
147 .pad = vin->digital->source_pad,
151 ret = v4l2_subdev_call(vin_to_source(vin), pad, get_fmt, NULL, &fmt);
155 v4l2_fill_pix_format(&vin->format, &fmt.format);
157 rvin_format_align(vin, &vin->format);
160 vin->source.left = 0;
161 vin->source.width = vin->format.width;
162 vin->source.height = vin->format.height;
164 vin->crop = vin->source;
165 vin->compose = vin->source;
170 static int rvin_try_format(struct rvin_dev *vin, u32 which,
171 struct v4l2_pix_format *pix,
172 struct v4l2_rect *crop, struct v4l2_rect *compose)
174 struct v4l2_subdev *sd = vin_to_source(vin);
175 struct v4l2_subdev_pad_config *pad_cfg;
176 struct v4l2_subdev_format format = {
178 .pad = vin->digital->source_pad,
180 enum v4l2_field field;
184 pad_cfg = v4l2_subdev_alloc_pad_config(sd);
188 if (!rvin_format_from_pixel(pix->pixelformat) ||
189 (vin->info->model == RCAR_M1 &&
190 pix->pixelformat == V4L2_PIX_FMT_XBGR32))
191 pix->pixelformat = RVIN_DEFAULT_FORMAT;
193 v4l2_fill_mbus_format(&format.format, pix, vin->mbus_code);
195 /* Allow the video device to override field and to scale */
198 height = pix->height;
200 ret = v4l2_subdev_call(sd, pad, set_fmt, pad_cfg, &format);
201 if (ret < 0 && ret != -ENOIOCTLCMD)
204 v4l2_fill_pix_format(pix, &format.format);
209 crop->width = pix->width;
210 crop->height = pix->height;
213 * If source is ALTERNATE the driver will use the VIN hardware
214 * to INTERLACE it. The crop height then needs to be doubled.
216 if (pix->field == V4L2_FIELD_ALTERNATE)
220 if (field != V4L2_FIELD_ANY)
224 pix->height = height;
226 rvin_format_align(vin, pix);
231 compose->width = pix->width;
232 compose->height = pix->height;
235 v4l2_subdev_free_pad_config(pad_cfg);
240 static int rvin_querycap(struct file *file, void *priv,
241 struct v4l2_capability *cap)
243 struct rvin_dev *vin = video_drvdata(file);
245 strlcpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
246 strlcpy(cap->card, "R_Car_VIN", sizeof(cap->card));
247 snprintf(cap->bus_info, sizeof(cap->bus_info), "platform:%s",
252 static int rvin_try_fmt_vid_cap(struct file *file, void *priv,
253 struct v4l2_format *f)
255 struct rvin_dev *vin = video_drvdata(file);
257 return rvin_try_format(vin, V4L2_SUBDEV_FORMAT_TRY, &f->fmt.pix, NULL,
261 static int rvin_s_fmt_vid_cap(struct file *file, void *priv,
262 struct v4l2_format *f)
264 struct rvin_dev *vin = video_drvdata(file);
265 struct v4l2_rect crop, compose;
268 if (vb2_is_busy(&vin->queue))
271 ret = rvin_try_format(vin, V4L2_SUBDEV_FORMAT_ACTIVE, &f->fmt.pix,
276 vin->format = f->fmt.pix;
278 vin->compose = compose;
284 static int rvin_g_fmt_vid_cap(struct file *file, void *priv,
285 struct v4l2_format *f)
287 struct rvin_dev *vin = video_drvdata(file);
289 f->fmt.pix = vin->format;
294 static int rvin_enum_fmt_vid_cap(struct file *file, void *priv,
295 struct v4l2_fmtdesc *f)
297 if (f->index >= ARRAY_SIZE(rvin_formats))
300 f->pixelformat = rvin_formats[f->index].fourcc;
305 static int rvin_g_selection(struct file *file, void *fh,
306 struct v4l2_selection *s)
308 struct rvin_dev *vin = video_drvdata(file);
310 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
314 case V4L2_SEL_TGT_CROP_BOUNDS:
315 case V4L2_SEL_TGT_CROP_DEFAULT:
316 s->r.left = s->r.top = 0;
317 s->r.width = vin->source.width;
318 s->r.height = vin->source.height;
320 case V4L2_SEL_TGT_CROP:
323 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
324 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
325 s->r.left = s->r.top = 0;
326 s->r.width = vin->format.width;
327 s->r.height = vin->format.height;
329 case V4L2_SEL_TGT_COMPOSE:
339 static int rvin_s_selection(struct file *file, void *fh,
340 struct v4l2_selection *s)
342 struct rvin_dev *vin = video_drvdata(file);
343 const struct rvin_video_format *fmt;
344 struct v4l2_rect r = s->r;
345 struct v4l2_rect max_rect;
346 struct v4l2_rect min_rect = {
351 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
354 v4l2_rect_set_min_size(&r, &min_rect);
357 case V4L2_SEL_TGT_CROP:
358 /* Can't crop outside of source input */
359 max_rect.top = max_rect.left = 0;
360 max_rect.width = vin->source.width;
361 max_rect.height = vin->source.height;
362 v4l2_rect_map_inside(&r, &max_rect);
364 v4l_bound_align_image(&r.width, 6, vin->source.width, 0,
365 &r.height, 2, vin->source.height, 0, 0);
367 r.top = clamp_t(s32, r.top, 0, vin->source.height - r.height);
368 r.left = clamp_t(s32, r.left, 0, vin->source.width - r.width);
370 vin->crop = s->r = r;
372 vin_dbg(vin, "Cropped %dx%d@%d:%d of %dx%d\n",
373 r.width, r.height, r.left, r.top,
374 vin->source.width, vin->source.height);
376 case V4L2_SEL_TGT_COMPOSE:
377 /* Make sure compose rect fits inside output format */
378 max_rect.top = max_rect.left = 0;
379 max_rect.width = vin->format.width;
380 max_rect.height = vin->format.height;
381 v4l2_rect_map_inside(&r, &max_rect);
384 * Composing is done by adding a offset to the buffer address,
385 * the HW wants this address to be aligned to HW_BUFFER_MASK.
386 * Make sure the top and left values meets this requirement.
388 while ((r.top * vin->format.bytesperline) & HW_BUFFER_MASK)
391 fmt = rvin_format_from_pixel(vin->format.pixelformat);
392 while ((r.left * fmt->bpp) & HW_BUFFER_MASK)
395 vin->compose = s->r = r;
397 vin_dbg(vin, "Compose %dx%d@%d:%d in %dx%d\n",
398 r.width, r.height, r.left, r.top,
399 vin->format.width, vin->format.height);
405 /* HW supports modifying configuration while running */
406 rvin_crop_scale_comp(vin);
411 static int rvin_cropcap(struct file *file, void *priv,
412 struct v4l2_cropcap *crop)
414 struct rvin_dev *vin = video_drvdata(file);
415 struct v4l2_subdev *sd = vin_to_source(vin);
417 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
420 return v4l2_subdev_call(sd, video, g_pixelaspect, &crop->pixelaspect);
423 static int rvin_enum_input(struct file *file, void *priv,
424 struct v4l2_input *i)
426 struct rvin_dev *vin = video_drvdata(file);
427 struct v4l2_subdev *sd = vin_to_source(vin);
433 ret = v4l2_subdev_call(sd, video, g_input_status, &i->status);
434 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
437 i->type = V4L2_INPUT_TYPE_CAMERA;
439 if (v4l2_subdev_has_op(sd, pad, dv_timings_cap)) {
440 i->capabilities = V4L2_IN_CAP_DV_TIMINGS;
443 i->capabilities = V4L2_IN_CAP_STD;
444 i->std = vin->vdev.tvnorms;
447 strlcpy(i->name, "Camera", sizeof(i->name));
452 static int rvin_g_input(struct file *file, void *priv, unsigned int *i)
458 static int rvin_s_input(struct file *file, void *priv, unsigned int i)
465 static int rvin_querystd(struct file *file, void *priv, v4l2_std_id *a)
467 struct rvin_dev *vin = video_drvdata(file);
468 struct v4l2_subdev *sd = vin_to_source(vin);
470 return v4l2_subdev_call(sd, video, querystd, a);
473 static int rvin_s_std(struct file *file, void *priv, v4l2_std_id a)
475 struct rvin_dev *vin = video_drvdata(file);
478 ret = v4l2_subdev_call(vin_to_source(vin), video, s_std, a);
484 /* Changing the standard will change the width/height */
485 return rvin_reset_format(vin);
488 static int rvin_g_std(struct file *file, void *priv, v4l2_std_id *a)
490 struct rvin_dev *vin = video_drvdata(file);
492 if (v4l2_subdev_has_op(vin_to_source(vin), pad, dv_timings_cap))
500 static int rvin_subscribe_event(struct v4l2_fh *fh,
501 const struct v4l2_event_subscription *sub)
504 case V4L2_EVENT_SOURCE_CHANGE:
505 return v4l2_event_subscribe(fh, sub, 4, NULL);
507 return v4l2_ctrl_subscribe_event(fh, sub);
510 static int rvin_enum_dv_timings(struct file *file, void *priv_fh,
511 struct v4l2_enum_dv_timings *timings)
513 struct rvin_dev *vin = video_drvdata(file);
514 struct v4l2_subdev *sd = vin_to_source(vin);
520 timings->pad = vin->digital->sink_pad;
522 ret = v4l2_subdev_call(sd, pad, enum_dv_timings, timings);
529 static int rvin_s_dv_timings(struct file *file, void *priv_fh,
530 struct v4l2_dv_timings *timings)
532 struct rvin_dev *vin = video_drvdata(file);
533 struct v4l2_subdev *sd = vin_to_source(vin);
536 ret = v4l2_subdev_call(sd, video, s_dv_timings, timings);
540 /* Changing the timings will change the width/height */
541 return rvin_reset_format(vin);
544 static int rvin_g_dv_timings(struct file *file, void *priv_fh,
545 struct v4l2_dv_timings *timings)
547 struct rvin_dev *vin = video_drvdata(file);
548 struct v4l2_subdev *sd = vin_to_source(vin);
550 return v4l2_subdev_call(sd, video, g_dv_timings, timings);
553 static int rvin_query_dv_timings(struct file *file, void *priv_fh,
554 struct v4l2_dv_timings *timings)
556 struct rvin_dev *vin = video_drvdata(file);
557 struct v4l2_subdev *sd = vin_to_source(vin);
559 return v4l2_subdev_call(sd, video, query_dv_timings, timings);
562 static int rvin_dv_timings_cap(struct file *file, void *priv_fh,
563 struct v4l2_dv_timings_cap *cap)
565 struct rvin_dev *vin = video_drvdata(file);
566 struct v4l2_subdev *sd = vin_to_source(vin);
572 cap->pad = vin->digital->sink_pad;
574 ret = v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
581 static int rvin_g_edid(struct file *file, void *fh, struct v4l2_edid *edid)
583 struct rvin_dev *vin = video_drvdata(file);
584 struct v4l2_subdev *sd = vin_to_source(vin);
590 edid->pad = vin->digital->sink_pad;
592 ret = v4l2_subdev_call(sd, pad, get_edid, edid);
599 static int rvin_s_edid(struct file *file, void *fh, struct v4l2_edid *edid)
601 struct rvin_dev *vin = video_drvdata(file);
602 struct v4l2_subdev *sd = vin_to_source(vin);
608 edid->pad = vin->digital->sink_pad;
610 ret = v4l2_subdev_call(sd, pad, set_edid, edid);
617 static const struct v4l2_ioctl_ops rvin_ioctl_ops = {
618 .vidioc_querycap = rvin_querycap,
619 .vidioc_try_fmt_vid_cap = rvin_try_fmt_vid_cap,
620 .vidioc_g_fmt_vid_cap = rvin_g_fmt_vid_cap,
621 .vidioc_s_fmt_vid_cap = rvin_s_fmt_vid_cap,
622 .vidioc_enum_fmt_vid_cap = rvin_enum_fmt_vid_cap,
624 .vidioc_g_selection = rvin_g_selection,
625 .vidioc_s_selection = rvin_s_selection,
627 .vidioc_cropcap = rvin_cropcap,
629 .vidioc_enum_input = rvin_enum_input,
630 .vidioc_g_input = rvin_g_input,
631 .vidioc_s_input = rvin_s_input,
633 .vidioc_dv_timings_cap = rvin_dv_timings_cap,
634 .vidioc_enum_dv_timings = rvin_enum_dv_timings,
635 .vidioc_g_dv_timings = rvin_g_dv_timings,
636 .vidioc_s_dv_timings = rvin_s_dv_timings,
637 .vidioc_query_dv_timings = rvin_query_dv_timings,
639 .vidioc_g_edid = rvin_g_edid,
640 .vidioc_s_edid = rvin_s_edid,
642 .vidioc_querystd = rvin_querystd,
643 .vidioc_g_std = rvin_g_std,
644 .vidioc_s_std = rvin_s_std,
646 .vidioc_reqbufs = vb2_ioctl_reqbufs,
647 .vidioc_create_bufs = vb2_ioctl_create_bufs,
648 .vidioc_querybuf = vb2_ioctl_querybuf,
649 .vidioc_qbuf = vb2_ioctl_qbuf,
650 .vidioc_dqbuf = vb2_ioctl_dqbuf,
651 .vidioc_expbuf = vb2_ioctl_expbuf,
652 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
653 .vidioc_streamon = vb2_ioctl_streamon,
654 .vidioc_streamoff = vb2_ioctl_streamoff,
656 .vidioc_log_status = v4l2_ctrl_log_status,
657 .vidioc_subscribe_event = rvin_subscribe_event,
658 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
661 /* -----------------------------------------------------------------------------
662 * V4L2 Media Controller
665 static void rvin_mc_try_format(struct rvin_dev *vin,
666 struct v4l2_pix_format *pix)
669 * The V4L2 specification clearly documents the colorspace fields
670 * as being set by drivers for capture devices. Using the values
671 * supplied by userspace thus wouldn't comply with the API. Until
672 * the API is updated force fixed vaules.
674 pix->colorspace = RVIN_DEFAULT_COLORSPACE;
675 pix->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(pix->colorspace);
676 pix->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(pix->colorspace);
677 pix->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true, pix->colorspace,
680 rvin_format_align(vin, pix);
683 static int rvin_mc_try_fmt_vid_cap(struct file *file, void *priv,
684 struct v4l2_format *f)
686 struct rvin_dev *vin = video_drvdata(file);
688 rvin_mc_try_format(vin, &f->fmt.pix);
693 static int rvin_mc_s_fmt_vid_cap(struct file *file, void *priv,
694 struct v4l2_format *f)
696 struct rvin_dev *vin = video_drvdata(file);
698 if (vb2_is_busy(&vin->queue))
701 rvin_mc_try_format(vin, &f->fmt.pix);
703 vin->format = f->fmt.pix;
707 vin->crop.width = vin->format.width;
708 vin->crop.height = vin->format.height;
709 vin->compose = vin->crop;
714 static int rvin_mc_enum_input(struct file *file, void *priv,
715 struct v4l2_input *i)
720 i->type = V4L2_INPUT_TYPE_CAMERA;
721 strlcpy(i->name, "Camera", sizeof(i->name));
726 static const struct v4l2_ioctl_ops rvin_mc_ioctl_ops = {
727 .vidioc_querycap = rvin_querycap,
728 .vidioc_try_fmt_vid_cap = rvin_mc_try_fmt_vid_cap,
729 .vidioc_g_fmt_vid_cap = rvin_g_fmt_vid_cap,
730 .vidioc_s_fmt_vid_cap = rvin_mc_s_fmt_vid_cap,
731 .vidioc_enum_fmt_vid_cap = rvin_enum_fmt_vid_cap,
733 .vidioc_enum_input = rvin_mc_enum_input,
734 .vidioc_g_input = rvin_g_input,
735 .vidioc_s_input = rvin_s_input,
737 .vidioc_reqbufs = vb2_ioctl_reqbufs,
738 .vidioc_create_bufs = vb2_ioctl_create_bufs,
739 .vidioc_querybuf = vb2_ioctl_querybuf,
740 .vidioc_qbuf = vb2_ioctl_qbuf,
741 .vidioc_dqbuf = vb2_ioctl_dqbuf,
742 .vidioc_expbuf = vb2_ioctl_expbuf,
743 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
744 .vidioc_streamon = vb2_ioctl_streamon,
745 .vidioc_streamoff = vb2_ioctl_streamoff,
747 .vidioc_log_status = v4l2_ctrl_log_status,
748 .vidioc_subscribe_event = rvin_subscribe_event,
749 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
752 /* -----------------------------------------------------------------------------
756 static int rvin_power_on(struct rvin_dev *vin)
759 struct v4l2_subdev *sd = vin_to_source(vin);
761 pm_runtime_get_sync(vin->v4l2_dev.dev);
763 ret = v4l2_subdev_call(sd, core, s_power, 1);
764 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
769 static int rvin_power_off(struct rvin_dev *vin)
772 struct v4l2_subdev *sd = vin_to_source(vin);
774 ret = v4l2_subdev_call(sd, core, s_power, 0);
776 pm_runtime_put(vin->v4l2_dev.dev);
778 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
784 static int rvin_initialize_device(struct file *file)
786 struct rvin_dev *vin = video_drvdata(file);
789 struct v4l2_format f = {
790 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
792 .width = vin->format.width,
793 .height = vin->format.height,
794 .field = vin->format.field,
795 .colorspace = vin->format.colorspace,
796 .pixelformat = vin->format.pixelformat,
800 ret = rvin_power_on(vin);
804 pm_runtime_enable(&vin->vdev.dev);
805 ret = pm_runtime_resume(&vin->vdev.dev);
806 if (ret < 0 && ret != -ENOSYS)
810 * Try to configure with default parameters. Notice: this is the
811 * very first open, so, we cannot race against other calls,
812 * apart from someone else calling open() simultaneously, but
813 * .host_lock is protecting us against it.
815 ret = rvin_s_fmt_vid_cap(file, NULL, &f);
819 v4l2_ctrl_handler_setup(&vin->ctrl_handler);
823 pm_runtime_disable(&vin->vdev.dev);
830 static int rvin_open(struct file *file)
832 struct rvin_dev *vin = video_drvdata(file);
835 mutex_lock(&vin->lock);
837 file->private_data = vin;
839 ret = v4l2_fh_open(file);
843 if (!v4l2_fh_is_singular_file(file))
846 if (rvin_initialize_device(file)) {
847 v4l2_fh_release(file);
852 mutex_unlock(&vin->lock);
856 static int rvin_release(struct file *file)
858 struct rvin_dev *vin = video_drvdata(file);
862 mutex_lock(&vin->lock);
864 /* Save the singular status before we call the clean-up helper */
865 fh_singular = v4l2_fh_is_singular_file(file);
867 /* the release helper will cleanup any on-going streaming */
868 ret = _vb2_fop_release(file, NULL);
871 * If this was the last open file.
872 * Then de-initialize hw module.
875 pm_runtime_suspend(&vin->vdev.dev);
876 pm_runtime_disable(&vin->vdev.dev);
880 mutex_unlock(&vin->lock);
885 static const struct v4l2_file_operations rvin_fops = {
886 .owner = THIS_MODULE,
887 .unlocked_ioctl = video_ioctl2,
889 .release = rvin_release,
890 .poll = vb2_fop_poll,
891 .mmap = vb2_fop_mmap,
892 .read = vb2_fop_read,
895 /* -----------------------------------------------------------------------------
896 * Media controller file operations
899 static int rvin_mc_open(struct file *file)
901 struct rvin_dev *vin = video_drvdata(file);
904 ret = mutex_lock_interruptible(&vin->lock);
908 ret = pm_runtime_get_sync(vin->dev);
912 ret = v4l2_pipeline_pm_use(&vin->vdev.entity, 1);
916 file->private_data = vin;
918 ret = v4l2_fh_open(file);
922 mutex_unlock(&vin->lock);
926 v4l2_pipeline_pm_use(&vin->vdev.entity, 0);
928 pm_runtime_put(vin->dev);
930 mutex_unlock(&vin->lock);
935 static int rvin_mc_release(struct file *file)
937 struct rvin_dev *vin = video_drvdata(file);
940 mutex_lock(&vin->lock);
942 /* the release helper will cleanup any on-going streaming. */
943 ret = _vb2_fop_release(file, NULL);
945 v4l2_pipeline_pm_use(&vin->vdev.entity, 0);
946 pm_runtime_put(vin->dev);
948 mutex_unlock(&vin->lock);
953 static const struct v4l2_file_operations rvin_mc_fops = {
954 .owner = THIS_MODULE,
955 .unlocked_ioctl = video_ioctl2,
956 .open = rvin_mc_open,
957 .release = rvin_mc_release,
958 .poll = vb2_fop_poll,
959 .mmap = vb2_fop_mmap,
960 .read = vb2_fop_read,
963 void rvin_v4l2_unregister(struct rvin_dev *vin)
965 if (!video_is_registered(&vin->vdev))
968 v4l2_info(&vin->v4l2_dev, "Removing %s\n",
969 video_device_node_name(&vin->vdev));
971 /* Checks internaly if vdev have been init or not */
972 video_unregister_device(&vin->vdev);
975 static void rvin_notify(struct v4l2_subdev *sd,
976 unsigned int notification, void *arg)
978 struct rvin_dev *vin =
979 container_of(sd->v4l2_dev, struct rvin_dev, v4l2_dev);
981 switch (notification) {
982 case V4L2_DEVICE_NOTIFY_EVENT:
983 v4l2_event_queue(&vin->vdev, arg);
990 int rvin_v4l2_register(struct rvin_dev *vin)
992 struct video_device *vdev = &vin->vdev;
995 vin->v4l2_dev.notify = rvin_notify;
998 vdev->v4l2_dev = &vin->v4l2_dev;
999 vdev->queue = &vin->queue;
1000 snprintf(vdev->name, sizeof(vdev->name), "VIN%u output", vin->id);
1001 vdev->release = video_device_release_empty;
1002 vdev->lock = &vin->lock;
1003 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
1006 /* Set a default format */
1007 vin->format.pixelformat = RVIN_DEFAULT_FORMAT;
1008 vin->format.width = RVIN_DEFAULT_WIDTH;
1009 vin->format.height = RVIN_DEFAULT_HEIGHT;
1010 vin->format.field = RVIN_DEFAULT_FIELD;
1011 vin->format.colorspace = RVIN_DEFAULT_COLORSPACE;
1013 if (vin->info->use_mc) {
1014 vdev->fops = &rvin_mc_fops;
1015 vdev->ioctl_ops = &rvin_mc_ioctl_ops;
1017 vdev->fops = &rvin_fops;
1018 vdev->ioctl_ops = &rvin_ioctl_ops;
1019 rvin_reset_format(vin);
1022 rvin_format_align(vin, &vin->format);
1024 ret = video_register_device(&vin->vdev, VFL_TYPE_GRABBER, -1);
1026 vin_err(vin, "Failed to register video device\n");
1030 video_set_drvdata(&vin->vdev, vin);
1032 v4l2_info(&vin->v4l2_dev, "Device registered as %s\n",
1033 video_device_node_name(&vin->vdev));