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
,
71 const struct rvin_video_format
*rvin_format_from_pixel(struct rvin_dev
*vin
,
76 if (vin
->info
->model
== RCAR_M1
&& pixelformat
== V4L2_PIX_FMT_XBGR32
)
79 if (pixelformat
== V4L2_PIX_FMT_NV12
&& !vin
->info
->nv12
)
82 for (i
= 0; i
< ARRAY_SIZE(rvin_formats
); i
++)
83 if (rvin_formats
[i
].fourcc
== pixelformat
)
84 return rvin_formats
+ i
;
89 static u32
rvin_format_bytesperline(struct rvin_dev
*vin
,
90 struct v4l2_pix_format
*pix
)
92 const struct rvin_video_format
*fmt
;
95 fmt
= rvin_format_from_pixel(vin
, pix
->pixelformat
);
100 switch (pix
->pixelformat
) {
101 case V4L2_PIX_FMT_NV12
:
102 case V4L2_PIX_FMT_NV16
:
110 return ALIGN(pix
->width
, align
) * fmt
->bpp
;
113 static u32
rvin_format_sizeimage(struct v4l2_pix_format
*pix
)
115 switch (pix
->pixelformat
) {
116 case V4L2_PIX_FMT_NV12
:
117 return pix
->bytesperline
* pix
->height
* 3 / 2;
118 case V4L2_PIX_FMT_NV16
:
119 return pix
->bytesperline
* pix
->height
* 2;
121 return pix
->bytesperline
* pix
->height
;
125 static void rvin_format_align(struct rvin_dev
*vin
, struct v4l2_pix_format
*pix
)
129 if (!rvin_format_from_pixel(vin
, pix
->pixelformat
))
130 pix
->pixelformat
= RVIN_DEFAULT_FORMAT
;
132 switch (pix
->field
) {
134 case V4L2_FIELD_BOTTOM
:
135 case V4L2_FIELD_NONE
:
136 case V4L2_FIELD_INTERLACED_TB
:
137 case V4L2_FIELD_INTERLACED_BT
:
138 case V4L2_FIELD_INTERLACED
:
139 case V4L2_FIELD_ALTERNATE
:
142 pix
->field
= RVIN_DEFAULT_FIELD
;
146 /* HW limit width to a multiple of 32 (2^5) for NV12/16 else 2 (2^1) */
147 switch (pix
->pixelformat
) {
148 case V4L2_PIX_FMT_NV12
:
149 case V4L2_PIX_FMT_NV16
:
157 /* Limit to VIN capabilities */
158 v4l_bound_align_image(&pix
->width
, 2, vin
->info
->max_width
, walign
,
159 &pix
->height
, 4, vin
->info
->max_height
, 2, 0);
161 pix
->bytesperline
= rvin_format_bytesperline(vin
, pix
);
162 pix
->sizeimage
= rvin_format_sizeimage(pix
);
164 vin_dbg(vin
, "Format %ux%u bpl: %u size: %u\n",
165 pix
->width
, pix
->height
, pix
->bytesperline
, pix
->sizeimage
);
168 /* -----------------------------------------------------------------------------
172 static int rvin_reset_format(struct rvin_dev
*vin
)
174 struct v4l2_subdev_format fmt
= {
175 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
176 .pad
= vin
->parallel
->source_pad
,
180 ret
= v4l2_subdev_call(vin_to_source(vin
), pad
, get_fmt
, NULL
, &fmt
);
184 v4l2_fill_pix_format(&vin
->format
, &fmt
.format
);
186 vin
->src_rect
.top
= 0;
187 vin
->src_rect
.left
= 0;
188 vin
->src_rect
.width
= vin
->format
.width
;
189 vin
->src_rect
.height
= vin
->format
.height
;
191 /* Make use of the hardware interlacer by default. */
192 if (vin
->format
.field
== V4L2_FIELD_ALTERNATE
) {
193 vin
->format
.field
= V4L2_FIELD_INTERLACED
;
194 vin
->format
.height
*= 2;
197 rvin_format_align(vin
, &vin
->format
);
199 vin
->crop
= vin
->src_rect
;
201 vin
->compose
.top
= 0;
202 vin
->compose
.left
= 0;
203 vin
->compose
.width
= vin
->format
.width
;
204 vin
->compose
.height
= vin
->format
.height
;
209 static int rvin_try_format(struct rvin_dev
*vin
, u32 which
,
210 struct v4l2_pix_format
*pix
,
211 struct v4l2_rect
*src_rect
)
213 struct v4l2_subdev
*sd
= vin_to_source(vin
);
214 struct v4l2_subdev_pad_config
*pad_cfg
;
215 struct v4l2_subdev_format format
= {
217 .pad
= vin
->parallel
->source_pad
,
219 enum v4l2_field field
;
223 pad_cfg
= v4l2_subdev_alloc_pad_config(sd
);
227 if (!rvin_format_from_pixel(vin
, pix
->pixelformat
))
228 pix
->pixelformat
= RVIN_DEFAULT_FORMAT
;
230 v4l2_fill_mbus_format(&format
.format
, pix
, vin
->mbus_code
);
232 /* Allow the video device to override field and to scale */
235 height
= pix
->height
;
237 ret
= v4l2_subdev_call(sd
, pad
, set_fmt
, pad_cfg
, &format
);
238 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
242 v4l2_fill_pix_format(pix
, &format
.format
);
247 src_rect
->width
= pix
->width
;
248 src_rect
->height
= pix
->height
;
251 if (field
!= V4L2_FIELD_ANY
)
255 pix
->height
= height
;
257 rvin_format_align(vin
, pix
);
259 v4l2_subdev_free_pad_config(pad_cfg
);
264 static int rvin_querycap(struct file
*file
, void *priv
,
265 struct v4l2_capability
*cap
)
267 struct rvin_dev
*vin
= video_drvdata(file
);
269 strscpy(cap
->driver
, KBUILD_MODNAME
, sizeof(cap
->driver
));
270 strscpy(cap
->card
, "R_Car_VIN", sizeof(cap
->card
));
271 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
), "platform:%s",
276 static int rvin_try_fmt_vid_cap(struct file
*file
, void *priv
,
277 struct v4l2_format
*f
)
279 struct rvin_dev
*vin
= video_drvdata(file
);
281 return rvin_try_format(vin
, V4L2_SUBDEV_FORMAT_TRY
, &f
->fmt
.pix
, NULL
);
284 static int rvin_s_fmt_vid_cap(struct file
*file
, void *priv
,
285 struct v4l2_format
*f
)
287 struct rvin_dev
*vin
= video_drvdata(file
);
288 struct v4l2_rect fmt_rect
, src_rect
;
291 if (vb2_is_busy(&vin
->queue
))
294 ret
= rvin_try_format(vin
, V4L2_SUBDEV_FORMAT_ACTIVE
, &f
->fmt
.pix
,
299 vin
->format
= f
->fmt
.pix
;
303 fmt_rect
.width
= vin
->format
.width
;
304 fmt_rect
.height
= vin
->format
.height
;
306 v4l2_rect_map_inside(&vin
->crop
, &src_rect
);
307 v4l2_rect_map_inside(&vin
->compose
, &fmt_rect
);
308 vin
->src_rect
= src_rect
;
313 static int rvin_g_fmt_vid_cap(struct file
*file
, void *priv
,
314 struct v4l2_format
*f
)
316 struct rvin_dev
*vin
= video_drvdata(file
);
318 f
->fmt
.pix
= vin
->format
;
323 static int rvin_enum_fmt_vid_cap(struct file
*file
, void *priv
,
324 struct v4l2_fmtdesc
*f
)
326 struct rvin_dev
*vin
= video_drvdata(file
);
331 for (i
= 0; i
< ARRAY_SIZE(rvin_formats
); i
++) {
332 if (rvin_format_from_pixel(vin
, rvin_formats
[i
].fourcc
))
335 if (matched
== f
->index
) {
336 f
->pixelformat
= rvin_formats
[i
].fourcc
;
344 static int rvin_g_selection(struct file
*file
, void *fh
,
345 struct v4l2_selection
*s
)
347 struct rvin_dev
*vin
= video_drvdata(file
);
349 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
353 case V4L2_SEL_TGT_CROP_BOUNDS
:
354 case V4L2_SEL_TGT_CROP_DEFAULT
:
355 s
->r
.left
= s
->r
.top
= 0;
356 s
->r
.width
= vin
->src_rect
.width
;
357 s
->r
.height
= vin
->src_rect
.height
;
359 case V4L2_SEL_TGT_CROP
:
362 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
363 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
364 s
->r
.left
= s
->r
.top
= 0;
365 s
->r
.width
= vin
->format
.width
;
366 s
->r
.height
= vin
->format
.height
;
368 case V4L2_SEL_TGT_COMPOSE
:
378 static int rvin_s_selection(struct file
*file
, void *fh
,
379 struct v4l2_selection
*s
)
381 struct rvin_dev
*vin
= video_drvdata(file
);
382 const struct rvin_video_format
*fmt
;
383 struct v4l2_rect r
= s
->r
;
384 struct v4l2_rect max_rect
;
385 struct v4l2_rect min_rect
= {
390 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
393 v4l2_rect_set_min_size(&r
, &min_rect
);
396 case V4L2_SEL_TGT_CROP
:
397 /* Can't crop outside of source input */
398 max_rect
.top
= max_rect
.left
= 0;
399 max_rect
.width
= vin
->src_rect
.width
;
400 max_rect
.height
= vin
->src_rect
.height
;
401 v4l2_rect_map_inside(&r
, &max_rect
);
403 v4l_bound_align_image(&r
.width
, 6, vin
->src_rect
.width
, 0,
404 &r
.height
, 2, vin
->src_rect
.height
, 0, 0);
406 r
.top
= clamp_t(s32
, r
.top
, 0,
407 vin
->src_rect
.height
- r
.height
);
408 r
.left
= clamp_t(s32
, r
.left
, 0, vin
->src_rect
.width
- r
.width
);
410 vin
->crop
= s
->r
= r
;
412 vin_dbg(vin
, "Cropped %dx%d@%d:%d of %dx%d\n",
413 r
.width
, r
.height
, r
.left
, r
.top
,
414 vin
->src_rect
.width
, vin
->src_rect
.height
);
416 case V4L2_SEL_TGT_COMPOSE
:
417 /* Make sure compose rect fits inside output format */
418 max_rect
.top
= max_rect
.left
= 0;
419 max_rect
.width
= vin
->format
.width
;
420 max_rect
.height
= vin
->format
.height
;
421 v4l2_rect_map_inside(&r
, &max_rect
);
424 * Composing is done by adding a offset to the buffer address,
425 * the HW wants this address to be aligned to HW_BUFFER_MASK.
426 * Make sure the top and left values meets this requirement.
428 while ((r
.top
* vin
->format
.bytesperline
) & HW_BUFFER_MASK
)
431 fmt
= rvin_format_from_pixel(vin
, vin
->format
.pixelformat
);
432 while ((r
.left
* fmt
->bpp
) & HW_BUFFER_MASK
)
435 vin
->compose
= s
->r
= r
;
437 vin_dbg(vin
, "Compose %dx%d@%d:%d in %dx%d\n",
438 r
.width
, r
.height
, r
.left
, r
.top
,
439 vin
->format
.width
, vin
->format
.height
);
445 /* HW supports modifying configuration while running */
446 rvin_crop_scale_comp(vin
);
451 static int rvin_g_pixelaspect(struct file
*file
, void *priv
,
452 int type
, struct v4l2_fract
*f
)
454 struct rvin_dev
*vin
= video_drvdata(file
);
455 struct v4l2_subdev
*sd
= vin_to_source(vin
);
457 if (type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
460 return v4l2_subdev_call(sd
, video
, g_pixelaspect
, f
);
463 static int rvin_enum_input(struct file
*file
, void *priv
,
464 struct v4l2_input
*i
)
466 struct rvin_dev
*vin
= video_drvdata(file
);
467 struct v4l2_subdev
*sd
= vin_to_source(vin
);
473 ret
= v4l2_subdev_call(sd
, video
, g_input_status
, &i
->status
);
474 if (ret
< 0 && ret
!= -ENOIOCTLCMD
&& ret
!= -ENODEV
)
477 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
479 if (v4l2_subdev_has_op(sd
, pad
, dv_timings_cap
)) {
480 i
->capabilities
= V4L2_IN_CAP_DV_TIMINGS
;
483 i
->capabilities
= V4L2_IN_CAP_STD
;
484 i
->std
= vin
->vdev
.tvnorms
;
487 strscpy(i
->name
, "Camera", sizeof(i
->name
));
492 static int rvin_g_input(struct file
*file
, void *priv
, unsigned int *i
)
498 static int rvin_s_input(struct file
*file
, void *priv
, unsigned int i
)
505 static int rvin_querystd(struct file
*file
, void *priv
, v4l2_std_id
*a
)
507 struct rvin_dev
*vin
= video_drvdata(file
);
508 struct v4l2_subdev
*sd
= vin_to_source(vin
);
510 return v4l2_subdev_call(sd
, video
, querystd
, a
);
513 static int rvin_s_std(struct file
*file
, void *priv
, v4l2_std_id a
)
515 struct rvin_dev
*vin
= video_drvdata(file
);
518 ret
= v4l2_subdev_call(vin_to_source(vin
), video
, s_std
, a
);
524 /* Changing the standard will change the width/height */
525 return rvin_reset_format(vin
);
528 static int rvin_g_std(struct file
*file
, void *priv
, v4l2_std_id
*a
)
530 struct rvin_dev
*vin
= video_drvdata(file
);
532 if (v4l2_subdev_has_op(vin_to_source(vin
), pad
, dv_timings_cap
))
540 static int rvin_subscribe_event(struct v4l2_fh
*fh
,
541 const struct v4l2_event_subscription
*sub
)
544 case V4L2_EVENT_SOURCE_CHANGE
:
545 return v4l2_event_subscribe(fh
, sub
, 4, NULL
);
547 return v4l2_ctrl_subscribe_event(fh
, sub
);
550 static int rvin_enum_dv_timings(struct file
*file
, void *priv_fh
,
551 struct v4l2_enum_dv_timings
*timings
)
553 struct rvin_dev
*vin
= video_drvdata(file
);
554 struct v4l2_subdev
*sd
= vin_to_source(vin
);
560 timings
->pad
= vin
->parallel
->sink_pad
;
562 ret
= v4l2_subdev_call(sd
, pad
, enum_dv_timings
, timings
);
569 static int rvin_s_dv_timings(struct file
*file
, void *priv_fh
,
570 struct v4l2_dv_timings
*timings
)
572 struct rvin_dev
*vin
= video_drvdata(file
);
573 struct v4l2_subdev
*sd
= vin_to_source(vin
);
576 ret
= v4l2_subdev_call(sd
, video
, s_dv_timings
, timings
);
580 /* Changing the timings will change the width/height */
581 return rvin_reset_format(vin
);
584 static int rvin_g_dv_timings(struct file
*file
, void *priv_fh
,
585 struct v4l2_dv_timings
*timings
)
587 struct rvin_dev
*vin
= video_drvdata(file
);
588 struct v4l2_subdev
*sd
= vin_to_source(vin
);
590 return v4l2_subdev_call(sd
, video
, g_dv_timings
, timings
);
593 static int rvin_query_dv_timings(struct file
*file
, void *priv_fh
,
594 struct v4l2_dv_timings
*timings
)
596 struct rvin_dev
*vin
= video_drvdata(file
);
597 struct v4l2_subdev
*sd
= vin_to_source(vin
);
599 return v4l2_subdev_call(sd
, video
, query_dv_timings
, timings
);
602 static int rvin_dv_timings_cap(struct file
*file
, void *priv_fh
,
603 struct v4l2_dv_timings_cap
*cap
)
605 struct rvin_dev
*vin
= video_drvdata(file
);
606 struct v4l2_subdev
*sd
= vin_to_source(vin
);
612 cap
->pad
= vin
->parallel
->sink_pad
;
614 ret
= v4l2_subdev_call(sd
, pad
, dv_timings_cap
, cap
);
621 static int rvin_g_edid(struct file
*file
, void *fh
, struct v4l2_edid
*edid
)
623 struct rvin_dev
*vin
= video_drvdata(file
);
624 struct v4l2_subdev
*sd
= vin_to_source(vin
);
630 edid
->pad
= vin
->parallel
->sink_pad
;
632 ret
= v4l2_subdev_call(sd
, pad
, get_edid
, edid
);
639 static int rvin_s_edid(struct file
*file
, void *fh
, struct v4l2_edid
*edid
)
641 struct rvin_dev
*vin
= video_drvdata(file
);
642 struct v4l2_subdev
*sd
= vin_to_source(vin
);
648 edid
->pad
= vin
->parallel
->sink_pad
;
650 ret
= v4l2_subdev_call(sd
, pad
, set_edid
, edid
);
657 static const struct v4l2_ioctl_ops rvin_ioctl_ops
= {
658 .vidioc_querycap
= rvin_querycap
,
659 .vidioc_try_fmt_vid_cap
= rvin_try_fmt_vid_cap
,
660 .vidioc_g_fmt_vid_cap
= rvin_g_fmt_vid_cap
,
661 .vidioc_s_fmt_vid_cap
= rvin_s_fmt_vid_cap
,
662 .vidioc_enum_fmt_vid_cap
= rvin_enum_fmt_vid_cap
,
664 .vidioc_g_selection
= rvin_g_selection
,
665 .vidioc_s_selection
= rvin_s_selection
,
667 .vidioc_g_pixelaspect
= rvin_g_pixelaspect
,
669 .vidioc_enum_input
= rvin_enum_input
,
670 .vidioc_g_input
= rvin_g_input
,
671 .vidioc_s_input
= rvin_s_input
,
673 .vidioc_dv_timings_cap
= rvin_dv_timings_cap
,
674 .vidioc_enum_dv_timings
= rvin_enum_dv_timings
,
675 .vidioc_g_dv_timings
= rvin_g_dv_timings
,
676 .vidioc_s_dv_timings
= rvin_s_dv_timings
,
677 .vidioc_query_dv_timings
= rvin_query_dv_timings
,
679 .vidioc_g_edid
= rvin_g_edid
,
680 .vidioc_s_edid
= rvin_s_edid
,
682 .vidioc_querystd
= rvin_querystd
,
683 .vidioc_g_std
= rvin_g_std
,
684 .vidioc_s_std
= rvin_s_std
,
686 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
687 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
688 .vidioc_querybuf
= vb2_ioctl_querybuf
,
689 .vidioc_qbuf
= vb2_ioctl_qbuf
,
690 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
691 .vidioc_expbuf
= vb2_ioctl_expbuf
,
692 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
693 .vidioc_streamon
= vb2_ioctl_streamon
,
694 .vidioc_streamoff
= vb2_ioctl_streamoff
,
696 .vidioc_log_status
= v4l2_ctrl_log_status
,
697 .vidioc_subscribe_event
= rvin_subscribe_event
,
698 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
701 /* -----------------------------------------------------------------------------
702 * V4L2 Media Controller
705 static void rvin_mc_try_format(struct rvin_dev
*vin
,
706 struct v4l2_pix_format
*pix
)
709 * The V4L2 specification clearly documents the colorspace fields
710 * as being set by drivers for capture devices. Using the values
711 * supplied by userspace thus wouldn't comply with the API. Until
712 * the API is updated force fixed values.
714 pix
->colorspace
= RVIN_DEFAULT_COLORSPACE
;
715 pix
->xfer_func
= V4L2_MAP_XFER_FUNC_DEFAULT(pix
->colorspace
);
716 pix
->ycbcr_enc
= V4L2_MAP_YCBCR_ENC_DEFAULT(pix
->colorspace
);
717 pix
->quantization
= V4L2_MAP_QUANTIZATION_DEFAULT(true, pix
->colorspace
,
720 rvin_format_align(vin
, pix
);
723 static int rvin_mc_try_fmt_vid_cap(struct file
*file
, void *priv
,
724 struct v4l2_format
*f
)
726 struct rvin_dev
*vin
= video_drvdata(file
);
728 rvin_mc_try_format(vin
, &f
->fmt
.pix
);
733 static int rvin_mc_s_fmt_vid_cap(struct file
*file
, void *priv
,
734 struct v4l2_format
*f
)
736 struct rvin_dev
*vin
= video_drvdata(file
);
738 if (vb2_is_busy(&vin
->queue
))
741 rvin_mc_try_format(vin
, &f
->fmt
.pix
);
743 vin
->format
= f
->fmt
.pix
;
747 vin
->crop
.width
= vin
->format
.width
;
748 vin
->crop
.height
= vin
->format
.height
;
749 vin
->compose
= vin
->crop
;
754 static int rvin_mc_enum_input(struct file
*file
, void *priv
,
755 struct v4l2_input
*i
)
760 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
761 strscpy(i
->name
, "Camera", sizeof(i
->name
));
766 static const struct v4l2_ioctl_ops rvin_mc_ioctl_ops
= {
767 .vidioc_querycap
= rvin_querycap
,
768 .vidioc_try_fmt_vid_cap
= rvin_mc_try_fmt_vid_cap
,
769 .vidioc_g_fmt_vid_cap
= rvin_g_fmt_vid_cap
,
770 .vidioc_s_fmt_vid_cap
= rvin_mc_s_fmt_vid_cap
,
771 .vidioc_enum_fmt_vid_cap
= rvin_enum_fmt_vid_cap
,
773 .vidioc_enum_input
= rvin_mc_enum_input
,
774 .vidioc_g_input
= rvin_g_input
,
775 .vidioc_s_input
= rvin_s_input
,
777 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
778 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
779 .vidioc_querybuf
= vb2_ioctl_querybuf
,
780 .vidioc_qbuf
= vb2_ioctl_qbuf
,
781 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
782 .vidioc_expbuf
= vb2_ioctl_expbuf
,
783 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
784 .vidioc_streamon
= vb2_ioctl_streamon
,
785 .vidioc_streamoff
= vb2_ioctl_streamoff
,
787 .vidioc_log_status
= v4l2_ctrl_log_status
,
788 .vidioc_subscribe_event
= rvin_subscribe_event
,
789 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
792 /* -----------------------------------------------------------------------------
796 static int rvin_power_parallel(struct rvin_dev
*vin
, bool on
)
798 struct v4l2_subdev
*sd
= vin_to_source(vin
);
799 int power
= on
? 1 : 0;
802 ret
= v4l2_subdev_call(sd
, core
, s_power
, power
);
803 if (ret
< 0 && ret
!= -ENOIOCTLCMD
&& ret
!= -ENODEV
)
809 static int rvin_open(struct file
*file
)
811 struct rvin_dev
*vin
= video_drvdata(file
);
814 ret
= pm_runtime_get_sync(vin
->dev
);
818 ret
= mutex_lock_interruptible(&vin
->lock
);
822 file
->private_data
= vin
;
824 ret
= v4l2_fh_open(file
);
828 if (vin
->info
->use_mc
)
829 ret
= v4l2_pipeline_pm_use(&vin
->vdev
.entity
, 1);
830 else if (v4l2_fh_is_singular_file(file
))
831 ret
= rvin_power_parallel(vin
, true);
836 ret
= v4l2_ctrl_handler_setup(&vin
->ctrl_handler
);
840 mutex_unlock(&vin
->lock
);
844 if (vin
->info
->use_mc
)
845 v4l2_pipeline_pm_use(&vin
->vdev
.entity
, 0);
846 else if (v4l2_fh_is_singular_file(file
))
847 rvin_power_parallel(vin
, false);
849 v4l2_fh_release(file
);
851 mutex_unlock(&vin
->lock
);
853 pm_runtime_put(vin
->dev
);
858 static int rvin_release(struct file
*file
)
860 struct rvin_dev
*vin
= video_drvdata(file
);
864 mutex_lock(&vin
->lock
);
866 /* Save the singular status before we call the clean-up helper */
867 fh_singular
= v4l2_fh_is_singular_file(file
);
869 /* the release helper will cleanup any on-going streaming */
870 ret
= _vb2_fop_release(file
, NULL
);
872 if (vin
->info
->use_mc
) {
873 v4l2_pipeline_pm_use(&vin
->vdev
.entity
, 0);
876 rvin_power_parallel(vin
, false);
879 mutex_unlock(&vin
->lock
);
881 pm_runtime_put(vin
->dev
);
886 static const struct v4l2_file_operations rvin_fops
= {
887 .owner
= THIS_MODULE
,
888 .unlocked_ioctl
= video_ioctl2
,
890 .release
= rvin_release
,
891 .poll
= vb2_fop_poll
,
892 .mmap
= vb2_fop_mmap
,
893 .read
= vb2_fop_read
,
896 void rvin_v4l2_unregister(struct rvin_dev
*vin
)
898 if (!video_is_registered(&vin
->vdev
))
901 v4l2_info(&vin
->v4l2_dev
, "Removing %s\n",
902 video_device_node_name(&vin
->vdev
));
904 /* Checks internally if vdev have been init or not */
905 video_unregister_device(&vin
->vdev
);
908 static void rvin_notify(struct v4l2_subdev
*sd
,
909 unsigned int notification
, void *arg
)
911 struct rvin_dev
*vin
=
912 container_of(sd
->v4l2_dev
, struct rvin_dev
, v4l2_dev
);
914 switch (notification
) {
915 case V4L2_DEVICE_NOTIFY_EVENT
:
916 v4l2_event_queue(&vin
->vdev
, arg
);
923 int rvin_v4l2_register(struct rvin_dev
*vin
)
925 struct video_device
*vdev
= &vin
->vdev
;
928 vin
->v4l2_dev
.notify
= rvin_notify
;
931 vdev
->v4l2_dev
= &vin
->v4l2_dev
;
932 vdev
->queue
= &vin
->queue
;
933 snprintf(vdev
->name
, sizeof(vdev
->name
), "VIN%u output", vin
->id
);
934 vdev
->release
= video_device_release_empty
;
935 vdev
->lock
= &vin
->lock
;
936 vdev
->fops
= &rvin_fops
;
937 vdev
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
|
940 /* Set a default format */
941 vin
->format
.pixelformat
= RVIN_DEFAULT_FORMAT
;
942 vin
->format
.width
= RVIN_DEFAULT_WIDTH
;
943 vin
->format
.height
= RVIN_DEFAULT_HEIGHT
;
944 vin
->format
.field
= RVIN_DEFAULT_FIELD
;
945 vin
->format
.colorspace
= RVIN_DEFAULT_COLORSPACE
;
947 if (vin
->info
->use_mc
) {
948 vdev
->ioctl_ops
= &rvin_mc_ioctl_ops
;
950 vdev
->ioctl_ops
= &rvin_ioctl_ops
;
951 rvin_reset_format(vin
);
954 rvin_format_align(vin
, &vin
->format
);
956 ret
= video_register_device(&vin
->vdev
, VFL_TYPE_GRABBER
, -1);
958 vin_err(vin
, "Failed to register video device\n");
962 video_set_drvdata(&vin
->vdev
, vin
);
964 v4l2_info(&vin
->v4l2_dev
, "Device registered as %s\n",
965 video_device_node_name(&vin
->vdev
));