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_NV12,
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,
62 .fourcc = V4L2_PIX_FMT_ARGB555,
66 .fourcc = V4L2_PIX_FMT_ABGR32,
70 .fourcc = V4L2_PIX_FMT_SBGGR8,
74 .fourcc = V4L2_PIX_FMT_SGBRG8,
78 .fourcc = V4L2_PIX_FMT_SGRBG8,
82 .fourcc = V4L2_PIX_FMT_SRGGB8,
86 .fourcc = V4L2_PIX_FMT_GREY,
91 const struct rvin_video_format *rvin_format_from_pixel(struct rvin_dev *vin,
96 switch (pixelformat) {
97 case V4L2_PIX_FMT_XBGR32:
98 if (vin->info->model == RCAR_M1)
101 case V4L2_PIX_FMT_NV12:
103 * If NV12 is supported it's only supported on channels 0, 1, 4,
104 * 5, 8, 9, 12 and 13.
106 if (!vin->info->nv12 || !(BIT(vin->id) & 0x3333))
113 for (i = 0; i < ARRAY_SIZE(rvin_formats); i++)
114 if (rvin_formats[i].fourcc == pixelformat)
115 return rvin_formats + i;
120 static u32 rvin_format_bytesperline(struct rvin_dev *vin,
121 struct v4l2_pix_format *pix)
123 const struct rvin_video_format *fmt;
126 fmt = rvin_format_from_pixel(vin, pix->pixelformat);
131 switch (pix->pixelformat) {
132 case V4L2_PIX_FMT_NV12:
133 case V4L2_PIX_FMT_NV16:
141 if (V4L2_FIELD_IS_SEQUENTIAL(pix->field))
144 return ALIGN(pix->width, align) * fmt->bpp;
147 static u32 rvin_format_sizeimage(struct v4l2_pix_format *pix)
149 switch (pix->pixelformat) {
150 case V4L2_PIX_FMT_NV12:
151 return pix->bytesperline * pix->height * 3 / 2;
152 case V4L2_PIX_FMT_NV16:
153 return pix->bytesperline * pix->height * 2;
155 return pix->bytesperline * pix->height;
159 static void rvin_format_align(struct rvin_dev *vin, struct v4l2_pix_format *pix)
163 if (!rvin_format_from_pixel(vin, pix->pixelformat))
164 pix->pixelformat = RVIN_DEFAULT_FORMAT;
166 switch (pix->field) {
168 case V4L2_FIELD_BOTTOM:
169 case V4L2_FIELD_NONE:
170 case V4L2_FIELD_INTERLACED_TB:
171 case V4L2_FIELD_INTERLACED_BT:
172 case V4L2_FIELD_INTERLACED:
173 case V4L2_FIELD_ALTERNATE:
174 case V4L2_FIELD_SEQ_TB:
175 case V4L2_FIELD_SEQ_BT:
178 pix->field = RVIN_DEFAULT_FIELD;
182 /* Hardware limits width alignment based on format. */
183 switch (pix->pixelformat) {
184 /* Multiple of 32 (2^5) for NV12/16. */
185 case V4L2_PIX_FMT_NV12:
186 case V4L2_PIX_FMT_NV16:
189 /* Multiple of 2 (2^1) for YUV. */
190 case V4L2_PIX_FMT_YUYV:
191 case V4L2_PIX_FMT_UYVY:
194 /* No multiple for RGB. */
200 /* Limit to VIN capabilities */
201 v4l_bound_align_image(&pix->width, 5, vin->info->max_width, walign,
202 &pix->height, 2, vin->info->max_height, 0, 0);
204 pix->bytesperline = rvin_format_bytesperline(vin, pix);
205 pix->sizeimage = rvin_format_sizeimage(pix);
207 vin_dbg(vin, "Format %ux%u bpl: %u size: %u\n",
208 pix->width, pix->height, pix->bytesperline, pix->sizeimage);
211 /* -----------------------------------------------------------------------------
215 static int rvin_reset_format(struct rvin_dev *vin)
217 struct v4l2_subdev_format fmt = {
218 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
219 .pad = vin->parallel.source_pad,
223 ret = v4l2_subdev_call(vin_to_source(vin), pad, get_fmt, NULL, &fmt);
227 v4l2_fill_pix_format(&vin->format, &fmt.format);
231 vin->crop.width = vin->format.width;
232 vin->crop.height = vin->format.height;
234 /* Make use of the hardware interlacer by default. */
235 if (vin->format.field == V4L2_FIELD_ALTERNATE) {
236 vin->format.field = V4L2_FIELD_INTERLACED;
237 vin->format.height *= 2;
240 rvin_format_align(vin, &vin->format);
242 vin->compose.top = 0;
243 vin->compose.left = 0;
244 vin->compose.width = vin->format.width;
245 vin->compose.height = vin->format.height;
250 static int rvin_try_format(struct rvin_dev *vin, u32 which,
251 struct v4l2_pix_format *pix,
252 struct v4l2_rect *src_rect)
254 struct v4l2_subdev *sd = vin_to_source(vin);
255 struct v4l2_subdev_state *sd_state;
256 static struct lock_class_key key;
257 struct v4l2_subdev_format format = {
259 .pad = vin->parallel.source_pad,
261 enum v4l2_field field;
266 * FIXME: Drop this call, drivers are not supposed to use
267 * __v4l2_subdev_state_alloc().
269 sd_state = __v4l2_subdev_state_alloc(sd, "rvin:state->lock", &key);
270 if (IS_ERR(sd_state))
271 return PTR_ERR(sd_state);
273 if (!rvin_format_from_pixel(vin, pix->pixelformat))
274 pix->pixelformat = RVIN_DEFAULT_FORMAT;
276 v4l2_fill_mbus_format(&format.format, pix, vin->mbus_code);
278 /* Allow the video device to override field and to scale */
281 height = pix->height;
283 ret = v4l2_subdev_call(sd, pad, set_fmt, sd_state, &format);
284 if (ret < 0 && ret != -ENOIOCTLCMD)
288 v4l2_fill_pix_format(pix, &format.format);
293 src_rect->width = pix->width;
294 src_rect->height = pix->height;
297 if (field != V4L2_FIELD_ANY)
301 pix->height = height;
303 rvin_format_align(vin, pix);
305 __v4l2_subdev_state_free(sd_state);
310 static int rvin_querycap(struct file *file, void *priv,
311 struct v4l2_capability *cap)
313 strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
314 strscpy(cap->card, "R_Car_VIN", sizeof(cap->card));
318 static int rvin_try_fmt_vid_cap(struct file *file, void *priv,
319 struct v4l2_format *f)
321 struct rvin_dev *vin = video_drvdata(file);
323 return rvin_try_format(vin, V4L2_SUBDEV_FORMAT_TRY, &f->fmt.pix, NULL);
326 static int rvin_s_fmt_vid_cap(struct file *file, void *priv,
327 struct v4l2_format *f)
329 struct rvin_dev *vin = video_drvdata(file);
330 struct v4l2_rect fmt_rect, src_rect;
333 if (vb2_is_busy(&vin->queue))
336 ret = rvin_try_format(vin, V4L2_SUBDEV_FORMAT_ACTIVE, &f->fmt.pix,
341 vin->format = f->fmt.pix;
345 fmt_rect.width = vin->format.width;
346 fmt_rect.height = vin->format.height;
348 v4l2_rect_map_inside(&vin->crop, &src_rect);
349 v4l2_rect_map_inside(&vin->compose, &fmt_rect);
354 static int rvin_g_fmt_vid_cap(struct file *file, void *priv,
355 struct v4l2_format *f)
357 struct rvin_dev *vin = video_drvdata(file);
359 f->fmt.pix = vin->format;
364 static int rvin_enum_fmt_vid_cap(struct file *file, void *priv,
365 struct v4l2_fmtdesc *f)
367 struct rvin_dev *vin = video_drvdata(file);
372 * If mbus_code is set only enumerate supported pixel formats for that
373 * bus code. Converting from YCbCr to RGB and RGB to YCbCr is possible
374 * with VIN, so all supported YCbCr and RGB media bus codes can produce
375 * all of the related pixel formats. If mbus_code is not set enumerate
376 * all possible pixelformats.
378 * TODO: Once raw MEDIA_BUS_FMT_SRGGB12_1X12 format is added to the
379 * driver this needs to be extended so raw media bus code only result in
382 switch (f->mbus_code) {
384 case MEDIA_BUS_FMT_YUYV8_1X16:
385 case MEDIA_BUS_FMT_UYVY8_1X16:
386 case MEDIA_BUS_FMT_UYVY8_2X8:
387 case MEDIA_BUS_FMT_UYVY10_2X10:
388 case MEDIA_BUS_FMT_RGB888_1X24:
390 case MEDIA_BUS_FMT_SBGGR8_1X8:
393 f->pixelformat = V4L2_PIX_FMT_SBGGR8;
395 case MEDIA_BUS_FMT_SGBRG8_1X8:
398 f->pixelformat = V4L2_PIX_FMT_SGBRG8;
400 case MEDIA_BUS_FMT_SGRBG8_1X8:
403 f->pixelformat = V4L2_PIX_FMT_SGRBG8;
405 case MEDIA_BUS_FMT_SRGGB8_1X8:
408 f->pixelformat = V4L2_PIX_FMT_SRGGB8;
415 for (i = 0; i < ARRAY_SIZE(rvin_formats); i++) {
416 if (rvin_format_from_pixel(vin, rvin_formats[i].fourcc))
419 if (matched == f->index) {
420 f->pixelformat = rvin_formats[i].fourcc;
428 static int rvin_remote_rectangle(struct rvin_dev *vin, struct v4l2_rect *rect)
430 struct v4l2_subdev_format fmt = {
431 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
433 struct v4l2_subdev *sd;
437 if (vin->info->use_mc) {
438 struct media_pad *pad = media_pad_remote_pad_first(&vin->pad);
443 sd = media_entity_to_v4l2_subdev(pad->entity);
446 sd = vin_to_source(vin);
447 index = vin->parallel.source_pad;
451 ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt);
455 rect->left = rect->top = 0;
456 rect->width = fmt.format.width;
457 rect->height = fmt.format.height;
459 if (fmt.format.field == V4L2_FIELD_ALTERNATE) {
460 switch (vin->format.field) {
461 case V4L2_FIELD_INTERLACED_TB:
462 case V4L2_FIELD_INTERLACED_BT:
463 case V4L2_FIELD_INTERLACED:
464 case V4L2_FIELD_SEQ_TB:
465 case V4L2_FIELD_SEQ_BT:
474 static int rvin_g_selection(struct file *file, void *fh,
475 struct v4l2_selection *s)
477 struct rvin_dev *vin = video_drvdata(file);
483 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
487 case V4L2_SEL_TGT_CROP_BOUNDS:
488 case V4L2_SEL_TGT_CROP_DEFAULT:
489 ret = rvin_remote_rectangle(vin, &s->r);
494 case V4L2_SEL_TGT_CROP:
497 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
498 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
499 s->r.left = s->r.top = 0;
500 s->r.width = vin->format.width;
501 s->r.height = vin->format.height;
503 case V4L2_SEL_TGT_COMPOSE:
513 static int rvin_s_selection(struct file *file, void *fh,
514 struct v4l2_selection *s)
516 struct rvin_dev *vin = video_drvdata(file);
517 const struct rvin_video_format *fmt;
518 struct v4l2_rect r = s->r;
519 struct v4l2_rect max_rect;
520 struct v4l2_rect min_rect = {
529 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
532 v4l2_rect_set_min_size(&r, &min_rect);
535 case V4L2_SEL_TGT_CROP:
536 /* Can't crop outside of source input */
537 ret = rvin_remote_rectangle(vin, &max_rect);
541 v4l2_rect_map_inside(&r, &max_rect);
543 v4l_bound_align_image(&r.width, 6, max_rect.width, 0,
544 &r.height, 2, max_rect.height, 0, 0);
546 r.top = clamp_t(s32, r.top, 0, max_rect.height - r.height);
547 r.left = clamp_t(s32, r.left, 0, max_rect.width - r.width);
549 vin->crop = s->r = r;
551 vin_dbg(vin, "Cropped %dx%d@%d:%d of %dx%d\n",
552 r.width, r.height, r.left, r.top,
553 max_rect.width, max_rect.height);
555 case V4L2_SEL_TGT_COMPOSE:
556 /* Make sure compose rect fits inside output format */
557 max_rect.top = max_rect.left = 0;
558 max_rect.width = vin->format.width;
559 max_rect.height = vin->format.height;
560 v4l2_rect_map_inside(&r, &max_rect);
563 * Composing is done by adding a offset to the buffer address,
564 * the HW wants this address to be aligned to HW_BUFFER_MASK.
565 * Make sure the top and left values meets this requirement.
567 while ((r.top * vin->format.bytesperline) & HW_BUFFER_MASK)
570 fmt = rvin_format_from_pixel(vin, vin->format.pixelformat);
571 while ((r.left * fmt->bpp) & HW_BUFFER_MASK)
574 vin->compose = s->r = r;
576 vin_dbg(vin, "Compose %dx%d@%d:%d in %dx%d\n",
577 r.width, r.height, r.left, r.top,
578 vin->format.width, vin->format.height);
584 /* HW supports modifying configuration while running */
585 rvin_crop_scale_comp(vin);
590 static int rvin_g_parm(struct file *file, void *priv,
591 struct v4l2_streamparm *parm)
593 struct rvin_dev *vin = video_drvdata(file);
594 struct v4l2_subdev *sd = vin_to_source(vin);
596 return v4l2_g_parm_cap(&vin->vdev, sd, parm);
599 static int rvin_s_parm(struct file *file, void *priv,
600 struct v4l2_streamparm *parm)
602 struct rvin_dev *vin = video_drvdata(file);
603 struct v4l2_subdev *sd = vin_to_source(vin);
605 return v4l2_s_parm_cap(&vin->vdev, sd, parm);
608 static int rvin_g_pixelaspect(struct file *file, void *priv,
609 int type, struct v4l2_fract *f)
611 struct rvin_dev *vin = video_drvdata(file);
612 struct v4l2_subdev *sd = vin_to_source(vin);
614 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
617 return v4l2_subdev_call(sd, video, g_pixelaspect, f);
620 static int rvin_enum_input(struct file *file, void *priv,
621 struct v4l2_input *i)
623 struct rvin_dev *vin = video_drvdata(file);
624 struct v4l2_subdev *sd = vin_to_source(vin);
630 ret = v4l2_subdev_call(sd, video, g_input_status, &i->status);
631 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
634 i->type = V4L2_INPUT_TYPE_CAMERA;
636 if (v4l2_subdev_has_op(sd, pad, dv_timings_cap)) {
637 i->capabilities = V4L2_IN_CAP_DV_TIMINGS;
640 i->capabilities = V4L2_IN_CAP_STD;
641 i->std = vin->vdev.tvnorms;
644 strscpy(i->name, "Camera", sizeof(i->name));
649 static int rvin_g_input(struct file *file, void *priv, unsigned int *i)
655 static int rvin_s_input(struct file *file, void *priv, unsigned int i)
662 static int rvin_querystd(struct file *file, void *priv, v4l2_std_id *a)
664 struct rvin_dev *vin = video_drvdata(file);
665 struct v4l2_subdev *sd = vin_to_source(vin);
667 return v4l2_subdev_call(sd, video, querystd, a);
670 static int rvin_s_std(struct file *file, void *priv, v4l2_std_id a)
672 struct rvin_dev *vin = video_drvdata(file);
675 ret = v4l2_subdev_call(vin_to_source(vin), video, s_std, a);
681 /* Changing the standard will change the width/height */
682 return rvin_reset_format(vin);
685 static int rvin_g_std(struct file *file, void *priv, v4l2_std_id *a)
687 struct rvin_dev *vin = video_drvdata(file);
689 if (v4l2_subdev_has_op(vin_to_source(vin), pad, dv_timings_cap))
697 static int rvin_subscribe_event(struct v4l2_fh *fh,
698 const struct v4l2_event_subscription *sub)
701 case V4L2_EVENT_SOURCE_CHANGE:
702 return v4l2_event_subscribe(fh, sub, 4, NULL);
704 return v4l2_ctrl_subscribe_event(fh, sub);
707 static int rvin_enum_dv_timings(struct file *file, void *priv_fh,
708 struct v4l2_enum_dv_timings *timings)
710 struct rvin_dev *vin = video_drvdata(file);
711 struct v4l2_subdev *sd = vin_to_source(vin);
717 timings->pad = vin->parallel.sink_pad;
719 ret = v4l2_subdev_call(sd, pad, enum_dv_timings, timings);
726 static int rvin_s_dv_timings(struct file *file, void *priv_fh,
727 struct v4l2_dv_timings *timings)
729 struct rvin_dev *vin = video_drvdata(file);
730 struct v4l2_subdev *sd = vin_to_source(vin);
733 ret = v4l2_subdev_call(sd, video, s_dv_timings, timings);
737 /* Changing the timings will change the width/height */
738 return rvin_reset_format(vin);
741 static int rvin_g_dv_timings(struct file *file, void *priv_fh,
742 struct v4l2_dv_timings *timings)
744 struct rvin_dev *vin = video_drvdata(file);
745 struct v4l2_subdev *sd = vin_to_source(vin);
747 return v4l2_subdev_call(sd, video, g_dv_timings, timings);
750 static int rvin_query_dv_timings(struct file *file, void *priv_fh,
751 struct v4l2_dv_timings *timings)
753 struct rvin_dev *vin = video_drvdata(file);
754 struct v4l2_subdev *sd = vin_to_source(vin);
756 return v4l2_subdev_call(sd, video, query_dv_timings, timings);
759 static int rvin_dv_timings_cap(struct file *file, void *priv_fh,
760 struct v4l2_dv_timings_cap *cap)
762 struct rvin_dev *vin = video_drvdata(file);
763 struct v4l2_subdev *sd = vin_to_source(vin);
769 cap->pad = vin->parallel.sink_pad;
771 ret = v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
778 static int rvin_g_edid(struct file *file, void *fh, struct v4l2_edid *edid)
780 struct rvin_dev *vin = video_drvdata(file);
781 struct v4l2_subdev *sd = vin_to_source(vin);
787 edid->pad = vin->parallel.sink_pad;
789 ret = v4l2_subdev_call(sd, pad, get_edid, edid);
796 static int rvin_s_edid(struct file *file, void *fh, struct v4l2_edid *edid)
798 struct rvin_dev *vin = video_drvdata(file);
799 struct v4l2_subdev *sd = vin_to_source(vin);
805 edid->pad = vin->parallel.sink_pad;
807 ret = v4l2_subdev_call(sd, pad, set_edid, edid);
814 static const struct v4l2_ioctl_ops rvin_ioctl_ops = {
815 .vidioc_querycap = rvin_querycap,
816 .vidioc_try_fmt_vid_cap = rvin_try_fmt_vid_cap,
817 .vidioc_g_fmt_vid_cap = rvin_g_fmt_vid_cap,
818 .vidioc_s_fmt_vid_cap = rvin_s_fmt_vid_cap,
819 .vidioc_enum_fmt_vid_cap = rvin_enum_fmt_vid_cap,
821 .vidioc_g_selection = rvin_g_selection,
822 .vidioc_s_selection = rvin_s_selection,
824 .vidioc_g_parm = rvin_g_parm,
825 .vidioc_s_parm = rvin_s_parm,
827 .vidioc_g_pixelaspect = rvin_g_pixelaspect,
829 .vidioc_enum_input = rvin_enum_input,
830 .vidioc_g_input = rvin_g_input,
831 .vidioc_s_input = rvin_s_input,
833 .vidioc_dv_timings_cap = rvin_dv_timings_cap,
834 .vidioc_enum_dv_timings = rvin_enum_dv_timings,
835 .vidioc_g_dv_timings = rvin_g_dv_timings,
836 .vidioc_s_dv_timings = rvin_s_dv_timings,
837 .vidioc_query_dv_timings = rvin_query_dv_timings,
839 .vidioc_g_edid = rvin_g_edid,
840 .vidioc_s_edid = rvin_s_edid,
842 .vidioc_querystd = rvin_querystd,
843 .vidioc_g_std = rvin_g_std,
844 .vidioc_s_std = rvin_s_std,
846 .vidioc_reqbufs = vb2_ioctl_reqbufs,
847 .vidioc_create_bufs = vb2_ioctl_create_bufs,
848 .vidioc_querybuf = vb2_ioctl_querybuf,
849 .vidioc_qbuf = vb2_ioctl_qbuf,
850 .vidioc_dqbuf = vb2_ioctl_dqbuf,
851 .vidioc_expbuf = vb2_ioctl_expbuf,
852 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
853 .vidioc_streamon = vb2_ioctl_streamon,
854 .vidioc_streamoff = vb2_ioctl_streamoff,
856 .vidioc_log_status = v4l2_ctrl_log_status,
857 .vidioc_subscribe_event = rvin_subscribe_event,
858 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
861 /* -----------------------------------------------------------------------------
862 * V4L2 Media Controller
865 static void rvin_mc_try_format(struct rvin_dev *vin,
866 struct v4l2_pix_format *pix)
869 * The V4L2 specification clearly documents the colorspace fields
870 * as being set by drivers for capture devices. Using the values
871 * supplied by userspace thus wouldn't comply with the API. Until
872 * the API is updated force fixed values.
874 pix->colorspace = RVIN_DEFAULT_COLORSPACE;
875 pix->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(pix->colorspace);
876 pix->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(pix->colorspace);
877 pix->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true, pix->colorspace,
880 rvin_format_align(vin, pix);
883 static int rvin_mc_try_fmt_vid_cap(struct file *file, void *priv,
884 struct v4l2_format *f)
886 struct rvin_dev *vin = video_drvdata(file);
888 rvin_mc_try_format(vin, &f->fmt.pix);
893 static int rvin_mc_s_fmt_vid_cap(struct file *file, void *priv,
894 struct v4l2_format *f)
896 struct rvin_dev *vin = video_drvdata(file);
898 if (vb2_is_busy(&vin->queue))
901 rvin_mc_try_format(vin, &f->fmt.pix);
903 vin->format = f->fmt.pix;
907 vin->crop.width = vin->format.width;
908 vin->crop.height = vin->format.height;
909 vin->compose = vin->crop;
914 static const struct v4l2_ioctl_ops rvin_mc_ioctl_ops = {
915 .vidioc_querycap = rvin_querycap,
916 .vidioc_try_fmt_vid_cap = rvin_mc_try_fmt_vid_cap,
917 .vidioc_g_fmt_vid_cap = rvin_g_fmt_vid_cap,
918 .vidioc_s_fmt_vid_cap = rvin_mc_s_fmt_vid_cap,
919 .vidioc_enum_fmt_vid_cap = rvin_enum_fmt_vid_cap,
921 .vidioc_g_selection = rvin_g_selection,
922 .vidioc_s_selection = rvin_s_selection,
924 .vidioc_reqbufs = vb2_ioctl_reqbufs,
925 .vidioc_create_bufs = vb2_ioctl_create_bufs,
926 .vidioc_querybuf = vb2_ioctl_querybuf,
927 .vidioc_qbuf = vb2_ioctl_qbuf,
928 .vidioc_dqbuf = vb2_ioctl_dqbuf,
929 .vidioc_expbuf = vb2_ioctl_expbuf,
930 .vidioc_prepare_buf = vb2_ioctl_prepare_buf,
931 .vidioc_streamon = vb2_ioctl_streamon,
932 .vidioc_streamoff = vb2_ioctl_streamoff,
934 .vidioc_log_status = v4l2_ctrl_log_status,
935 .vidioc_subscribe_event = rvin_subscribe_event,
936 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
939 /* -----------------------------------------------------------------------------
943 static int rvin_power_parallel(struct rvin_dev *vin, bool on)
945 struct v4l2_subdev *sd = vin_to_source(vin);
946 int power = on ? 1 : 0;
949 ret = v4l2_subdev_call(sd, core, s_power, power);
950 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
956 static int rvin_open(struct file *file)
958 struct rvin_dev *vin = video_drvdata(file);
961 ret = pm_runtime_resume_and_get(vin->dev);
965 ret = mutex_lock_interruptible(&vin->lock);
969 file->private_data = vin;
971 ret = v4l2_fh_open(file);
975 if (vin->info->use_mc)
976 ret = v4l2_pipeline_pm_get(&vin->vdev.entity);
977 else if (v4l2_fh_is_singular_file(file))
978 ret = rvin_power_parallel(vin, true);
983 ret = v4l2_ctrl_handler_setup(&vin->ctrl_handler);
987 mutex_unlock(&vin->lock);
991 if (vin->info->use_mc)
992 v4l2_pipeline_pm_put(&vin->vdev.entity);
993 else if (v4l2_fh_is_singular_file(file))
994 rvin_power_parallel(vin, false);
996 v4l2_fh_release(file);
998 mutex_unlock(&vin->lock);
1000 pm_runtime_put(vin->dev);
1005 static int rvin_release(struct file *file)
1007 struct rvin_dev *vin = video_drvdata(file);
1011 mutex_lock(&vin->lock);
1013 /* Save the singular status before we call the clean-up helper */
1014 fh_singular = v4l2_fh_is_singular_file(file);
1016 /* the release helper will cleanup any on-going streaming */
1017 ret = _vb2_fop_release(file, NULL);
1019 if (vin->info->use_mc) {
1020 v4l2_pipeline_pm_put(&vin->vdev.entity);
1023 rvin_power_parallel(vin, false);
1026 mutex_unlock(&vin->lock);
1028 pm_runtime_put(vin->dev);
1033 static const struct v4l2_file_operations rvin_fops = {
1034 .owner = THIS_MODULE,
1035 .unlocked_ioctl = video_ioctl2,
1037 .release = rvin_release,
1038 .poll = vb2_fop_poll,
1039 .mmap = vb2_fop_mmap,
1040 .read = vb2_fop_read,
1043 void rvin_v4l2_unregister(struct rvin_dev *vin)
1045 if (!video_is_registered(&vin->vdev))
1048 v4l2_info(&vin->v4l2_dev, "Removing %s\n",
1049 video_device_node_name(&vin->vdev));
1051 /* Checks internally if vdev have been init or not */
1052 video_unregister_device(&vin->vdev);
1055 static void rvin_notify_video_device(struct rvin_dev *vin,
1056 unsigned int notification, void *arg)
1058 switch (notification) {
1059 case V4L2_DEVICE_NOTIFY_EVENT:
1060 v4l2_event_queue(&vin->vdev, arg);
1067 static void rvin_notify(struct v4l2_subdev *sd,
1068 unsigned int notification, void *arg)
1070 struct v4l2_subdev *remote;
1071 struct rvin_group *group;
1072 struct media_pad *pad;
1073 struct rvin_dev *vin =
1074 container_of(sd->v4l2_dev, struct rvin_dev, v4l2_dev);
1077 /* If no media controller, no need to route the event. */
1078 if (!vin->info->use_mc) {
1079 rvin_notify_video_device(vin, notification, arg);
1085 for (i = 0; i < RCAR_VIN_NUM; i++) {
1086 vin = group->vin[i];
1090 pad = media_pad_remote_pad_first(&vin->pad);
1094 remote = media_entity_to_v4l2_subdev(pad->entity);
1098 rvin_notify_video_device(vin, notification, arg);
1102 int rvin_v4l2_register(struct rvin_dev *vin)
1104 struct video_device *vdev = &vin->vdev;
1107 vin->v4l2_dev.notify = rvin_notify;
1110 vdev->v4l2_dev = &vin->v4l2_dev;
1111 vdev->queue = &vin->queue;
1112 snprintf(vdev->name, sizeof(vdev->name), "VIN%u output", vin->id);
1113 vdev->release = video_device_release_empty;
1114 vdev->lock = &vin->lock;
1115 vdev->fops = &rvin_fops;
1116 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
1119 /* Set a default format */
1120 vin->format.pixelformat = RVIN_DEFAULT_FORMAT;
1121 vin->format.width = RVIN_DEFAULT_WIDTH;
1122 vin->format.height = RVIN_DEFAULT_HEIGHT;
1123 vin->format.field = RVIN_DEFAULT_FIELD;
1124 vin->format.colorspace = RVIN_DEFAULT_COLORSPACE;
1126 if (vin->info->use_mc) {
1127 vdev->device_caps |= V4L2_CAP_IO_MC;
1128 vdev->ioctl_ops = &rvin_mc_ioctl_ops;
1130 vdev->ioctl_ops = &rvin_ioctl_ops;
1131 rvin_reset_format(vin);
1134 rvin_format_align(vin, &vin->format);
1136 ret = video_register_device(&vin->vdev, VFL_TYPE_VIDEO, -1);
1138 vin_err(vin, "Failed to register video device\n");
1142 video_set_drvdata(&vin->vdev, vin);
1144 v4l2_info(&vin->v4l2_dev, "Device registered as %s\n",
1145 video_device_node_name(&vin->vdev));