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-rect.h>
25 #define RVIN_DEFAULT_FORMAT V4L2_PIX_FMT_YUYV
26 #define RVIN_MAX_WIDTH 2048
27 #define RVIN_MAX_HEIGHT 2048
29 /* -----------------------------------------------------------------------------
33 static const struct rvin_video_format rvin_formats
[] = {
35 .fourcc
= V4L2_PIX_FMT_NV16
,
39 .fourcc
= V4L2_PIX_FMT_YUYV
,
43 .fourcc
= V4L2_PIX_FMT_UYVY
,
47 .fourcc
= V4L2_PIX_FMT_RGB565
,
51 .fourcc
= V4L2_PIX_FMT_XRGB555
,
55 .fourcc
= V4L2_PIX_FMT_XBGR32
,
60 const struct rvin_video_format
*rvin_format_from_pixel(u32 pixelformat
)
64 for (i
= 0; i
< ARRAY_SIZE(rvin_formats
); i
++)
65 if (rvin_formats
[i
].fourcc
== pixelformat
)
66 return rvin_formats
+ i
;
71 static u32
rvin_format_bytesperline(struct v4l2_pix_format
*pix
)
73 const struct rvin_video_format
*fmt
;
75 fmt
= rvin_format_from_pixel(pix
->pixelformat
);
80 return pix
->width
* fmt
->bpp
;
83 static u32
rvin_format_sizeimage(struct v4l2_pix_format
*pix
)
85 if (pix
->pixelformat
== V4L2_PIX_FMT_NV16
)
86 return pix
->bytesperline
* pix
->height
* 2;
88 return pix
->bytesperline
* pix
->height
;
91 /* -----------------------------------------------------------------------------
95 static void rvin_reset_crop_compose(struct rvin_dev
*vin
)
97 vin
->crop
.top
= vin
->crop
.left
= 0;
98 vin
->crop
.width
= vin
->source
.width
;
99 vin
->crop
.height
= vin
->source
.height
;
101 vin
->compose
.top
= vin
->compose
.left
= 0;
102 vin
->compose
.width
= vin
->format
.width
;
103 vin
->compose
.height
= vin
->format
.height
;
106 static int rvin_reset_format(struct rvin_dev
*vin
)
108 struct v4l2_subdev_format fmt
= {
109 .which
= V4L2_SUBDEV_FORMAT_ACTIVE
,
111 struct v4l2_mbus_framefmt
*mf
= &fmt
.format
;
114 fmt
.pad
= vin
->digital
->source_pad
;
116 ret
= v4l2_subdev_call(vin_to_source(vin
), pad
, get_fmt
, NULL
, &fmt
);
120 vin
->format
.width
= mf
->width
;
121 vin
->format
.height
= mf
->height
;
122 vin
->format
.colorspace
= mf
->colorspace
;
123 vin
->format
.field
= mf
->field
;
126 * If the subdevice uses ALTERNATE field mode and G_STD is
127 * implemented use the VIN HW to combine the two fields to
128 * one INTERLACED frame. The ALTERNATE field mode can still
129 * be requested in S_FMT and be respected, this is just the
130 * default which is applied at probing or when S_STD is called.
132 if (vin
->format
.field
== V4L2_FIELD_ALTERNATE
&&
133 v4l2_subdev_has_op(vin_to_source(vin
), video
, g_std
))
134 vin
->format
.field
= V4L2_FIELD_INTERLACED
;
136 switch (vin
->format
.field
) {
138 case V4L2_FIELD_BOTTOM
:
139 case V4L2_FIELD_ALTERNATE
:
140 vin
->format
.height
/= 2;
142 case V4L2_FIELD_NONE
:
143 case V4L2_FIELD_INTERLACED_TB
:
144 case V4L2_FIELD_INTERLACED_BT
:
145 case V4L2_FIELD_INTERLACED
:
148 vin
->format
.field
= V4L2_FIELD_NONE
;
152 rvin_reset_crop_compose(vin
);
154 vin
->format
.bytesperline
= rvin_format_bytesperline(&vin
->format
);
155 vin
->format
.sizeimage
= rvin_format_sizeimage(&vin
->format
);
160 static int __rvin_try_format_source(struct rvin_dev
*vin
,
162 struct v4l2_pix_format
*pix
,
163 struct rvin_source_fmt
*source
)
165 struct v4l2_subdev
*sd
;
166 struct v4l2_subdev_pad_config
*pad_cfg
;
167 struct v4l2_subdev_format format
= {
170 enum v4l2_field field
;
173 sd
= vin_to_source(vin
);
175 v4l2_fill_mbus_format(&format
.format
, pix
, vin
->digital
->code
);
177 pad_cfg
= v4l2_subdev_alloc_pad_config(sd
);
181 format
.pad
= vin
->digital
->source_pad
;
185 ret
= v4l2_subdev_call(sd
, pad
, set_fmt
, pad_cfg
, &format
);
186 if (ret
< 0 && ret
!= -ENOIOCTLCMD
)
189 v4l2_fill_pix_format(pix
, &format
.format
);
193 source
->width
= pix
->width
;
194 source
->height
= pix
->height
;
196 vin_dbg(vin
, "Source resolution: %ux%u\n", source
->width
,
200 v4l2_subdev_free_pad_config(pad_cfg
);
204 static int __rvin_try_format(struct rvin_dev
*vin
,
206 struct v4l2_pix_format
*pix
,
207 struct rvin_source_fmt
*source
)
209 u32 rwidth
, rheight
, walign
;
214 rheight
= pix
->height
;
216 /* Keep current field if no specific one is asked for */
217 if (pix
->field
== V4L2_FIELD_ANY
)
218 pix
->field
= vin
->format
.field
;
220 /* If requested format is not supported fallback to the default */
221 if (!rvin_format_from_pixel(pix
->pixelformat
)) {
222 vin_dbg(vin
, "Format 0x%x not found, using default 0x%x\n",
223 pix
->pixelformat
, RVIN_DEFAULT_FORMAT
);
224 pix
->pixelformat
= RVIN_DEFAULT_FORMAT
;
227 /* Always recalculate */
228 pix
->bytesperline
= 0;
231 /* Limit to source capabilities */
232 ret
= __rvin_try_format_source(vin
, which
, pix
, source
);
236 switch (pix
->field
) {
238 case V4L2_FIELD_BOTTOM
:
239 case V4L2_FIELD_ALTERNATE
:
243 case V4L2_FIELD_NONE
:
244 case V4L2_FIELD_INTERLACED_TB
:
245 case V4L2_FIELD_INTERLACED_BT
:
246 case V4L2_FIELD_INTERLACED
:
249 pix
->field
= V4L2_FIELD_NONE
;
253 /* If source can't match format try if VIN can scale */
254 if (source
->width
!= rwidth
|| source
->height
!= rheight
)
255 rvin_scale_try(vin
, pix
, rwidth
, rheight
);
257 /* HW limit width to a multiple of 32 (2^5) for NV16 else 2 (2^1) */
258 walign
= vin
->format
.pixelformat
== V4L2_PIX_FMT_NV16
? 5 : 1;
260 /* Limit to VIN capabilities */
261 v4l_bound_align_image(&pix
->width
, 2, RVIN_MAX_WIDTH
, walign
,
262 &pix
->height
, 4, RVIN_MAX_HEIGHT
, 2, 0);
264 pix
->bytesperline
= max_t(u32
, pix
->bytesperline
,
265 rvin_format_bytesperline(pix
));
266 pix
->sizeimage
= max_t(u32
, pix
->sizeimage
,
267 rvin_format_sizeimage(pix
));
269 if (vin
->chip
== RCAR_M1
&& pix
->pixelformat
== V4L2_PIX_FMT_XBGR32
) {
270 vin_err(vin
, "pixel format XBGR32 not supported on M1\n");
274 vin_dbg(vin
, "Requested %ux%u Got %ux%u bpl: %d size: %d\n",
275 rwidth
, rheight
, pix
->width
, pix
->height
,
276 pix
->bytesperline
, pix
->sizeimage
);
281 static int rvin_querycap(struct file
*file
, void *priv
,
282 struct v4l2_capability
*cap
)
284 struct rvin_dev
*vin
= video_drvdata(file
);
286 strlcpy(cap
->driver
, KBUILD_MODNAME
, sizeof(cap
->driver
));
287 strlcpy(cap
->card
, "R_Car_VIN", sizeof(cap
->card
));
288 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
), "platform:%s",
293 static int rvin_try_fmt_vid_cap(struct file
*file
, void *priv
,
294 struct v4l2_format
*f
)
296 struct rvin_dev
*vin
= video_drvdata(file
);
297 struct rvin_source_fmt source
;
299 return __rvin_try_format(vin
, V4L2_SUBDEV_FORMAT_TRY
, &f
->fmt
.pix
,
303 static int rvin_s_fmt_vid_cap(struct file
*file
, void *priv
,
304 struct v4l2_format
*f
)
306 struct rvin_dev
*vin
= video_drvdata(file
);
307 struct rvin_source_fmt source
;
310 if (vb2_is_busy(&vin
->queue
))
313 ret
= __rvin_try_format(vin
, V4L2_SUBDEV_FORMAT_ACTIVE
, &f
->fmt
.pix
,
318 vin
->source
.width
= source
.width
;
319 vin
->source
.height
= source
.height
;
321 vin
->format
= f
->fmt
.pix
;
323 rvin_reset_crop_compose(vin
);
328 static int rvin_g_fmt_vid_cap(struct file
*file
, void *priv
,
329 struct v4l2_format
*f
)
331 struct rvin_dev
*vin
= video_drvdata(file
);
333 f
->fmt
.pix
= vin
->format
;
338 static int rvin_enum_fmt_vid_cap(struct file
*file
, void *priv
,
339 struct v4l2_fmtdesc
*f
)
341 if (f
->index
>= ARRAY_SIZE(rvin_formats
))
344 f
->pixelformat
= rvin_formats
[f
->index
].fourcc
;
349 static int rvin_g_selection(struct file
*file
, void *fh
,
350 struct v4l2_selection
*s
)
352 struct rvin_dev
*vin
= video_drvdata(file
);
354 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
358 case V4L2_SEL_TGT_CROP_BOUNDS
:
359 case V4L2_SEL_TGT_CROP_DEFAULT
:
360 s
->r
.left
= s
->r
.top
= 0;
361 s
->r
.width
= vin
->source
.width
;
362 s
->r
.height
= vin
->source
.height
;
364 case V4L2_SEL_TGT_CROP
:
367 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
368 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
369 s
->r
.left
= s
->r
.top
= 0;
370 s
->r
.width
= vin
->format
.width
;
371 s
->r
.height
= vin
->format
.height
;
373 case V4L2_SEL_TGT_COMPOSE
:
383 static int rvin_s_selection(struct file
*file
, void *fh
,
384 struct v4l2_selection
*s
)
386 struct rvin_dev
*vin
= video_drvdata(file
);
387 const struct rvin_video_format
*fmt
;
388 struct v4l2_rect r
= s
->r
;
389 struct v4l2_rect max_rect
;
390 struct v4l2_rect min_rect
= {
395 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
398 v4l2_rect_set_min_size(&r
, &min_rect
);
401 case V4L2_SEL_TGT_CROP
:
402 /* Can't crop outside of source input */
403 max_rect
.top
= max_rect
.left
= 0;
404 max_rect
.width
= vin
->source
.width
;
405 max_rect
.height
= vin
->source
.height
;
406 v4l2_rect_map_inside(&r
, &max_rect
);
408 v4l_bound_align_image(&r
.width
, 2, vin
->source
.width
, 1,
409 &r
.height
, 4, vin
->source
.height
, 2, 0);
411 r
.top
= clamp_t(s32
, r
.top
, 0, vin
->source
.height
- r
.height
);
412 r
.left
= clamp_t(s32
, r
.left
, 0, vin
->source
.width
- r
.width
);
414 vin
->crop
= s
->r
= r
;
416 vin_dbg(vin
, "Cropped %dx%d@%d:%d of %dx%d\n",
417 r
.width
, r
.height
, r
.left
, r
.top
,
418 vin
->source
.width
, vin
->source
.height
);
420 case V4L2_SEL_TGT_COMPOSE
:
421 /* Make sure compose rect fits inside output format */
422 max_rect
.top
= max_rect
.left
= 0;
423 max_rect
.width
= vin
->format
.width
;
424 max_rect
.height
= vin
->format
.height
;
425 v4l2_rect_map_inside(&r
, &max_rect
);
428 * Composing is done by adding a offset to the buffer address,
429 * the HW wants this address to be aligned to HW_BUFFER_MASK.
430 * Make sure the top and left values meets this requirement.
432 while ((r
.top
* vin
->format
.bytesperline
) & HW_BUFFER_MASK
)
435 fmt
= rvin_format_from_pixel(vin
->format
.pixelformat
);
436 while ((r
.left
* fmt
->bpp
) & HW_BUFFER_MASK
)
439 vin
->compose
= s
->r
= r
;
441 vin_dbg(vin
, "Compose %dx%d@%d:%d in %dx%d\n",
442 r
.width
, r
.height
, r
.left
, r
.top
,
443 vin
->format
.width
, vin
->format
.height
);
449 /* HW supports modifying configuration while running */
450 rvin_crop_scale_comp(vin
);
455 static int rvin_cropcap(struct file
*file
, void *priv
,
456 struct v4l2_cropcap
*crop
)
458 struct rvin_dev
*vin
= video_drvdata(file
);
459 struct v4l2_subdev
*sd
= vin_to_source(vin
);
461 if (crop
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
464 return v4l2_subdev_call(sd
, video
, g_pixelaspect
, &crop
->pixelaspect
);
467 static int rvin_enum_input(struct file
*file
, void *priv
,
468 struct v4l2_input
*i
)
470 struct rvin_dev
*vin
= video_drvdata(file
);
471 struct v4l2_subdev
*sd
= vin_to_source(vin
);
477 ret
= v4l2_subdev_call(sd
, video
, g_input_status
, &i
->status
);
478 if (ret
< 0 && ret
!= -ENOIOCTLCMD
&& ret
!= -ENODEV
)
481 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
483 if (v4l2_subdev_has_op(sd
, pad
, dv_timings_cap
)) {
484 i
->capabilities
= V4L2_IN_CAP_DV_TIMINGS
;
487 i
->capabilities
= V4L2_IN_CAP_STD
;
488 i
->std
= vin
->vdev
.tvnorms
;
491 strlcpy(i
->name
, "Camera", sizeof(i
->name
));
496 static int rvin_g_input(struct file
*file
, void *priv
, unsigned int *i
)
502 static int rvin_s_input(struct file
*file
, void *priv
, unsigned int i
)
509 static int rvin_querystd(struct file
*file
, void *priv
, v4l2_std_id
*a
)
511 struct rvin_dev
*vin
= video_drvdata(file
);
512 struct v4l2_subdev
*sd
= vin_to_source(vin
);
514 return v4l2_subdev_call(sd
, video
, querystd
, a
);
517 static int rvin_s_std(struct file
*file
, void *priv
, v4l2_std_id a
)
519 struct rvin_dev
*vin
= video_drvdata(file
);
522 ret
= v4l2_subdev_call(vin_to_source(vin
), video
, s_std
, a
);
526 /* Changing the standard will change the width/height */
527 return rvin_reset_format(vin
);
530 static int rvin_g_std(struct file
*file
, void *priv
, v4l2_std_id
*a
)
532 struct rvin_dev
*vin
= video_drvdata(file
);
533 struct v4l2_subdev
*sd
= vin_to_source(vin
);
535 return v4l2_subdev_call(sd
, video
, g_std
, a
);
538 static int rvin_subscribe_event(struct v4l2_fh
*fh
,
539 const struct v4l2_event_subscription
*sub
)
542 case V4L2_EVENT_SOURCE_CHANGE
:
543 return v4l2_event_subscribe(fh
, sub
, 4, NULL
);
545 return v4l2_ctrl_subscribe_event(fh
, sub
);
548 static int rvin_enum_dv_timings(struct file
*file
, void *priv_fh
,
549 struct v4l2_enum_dv_timings
*timings
)
551 struct rvin_dev
*vin
= video_drvdata(file
);
552 struct v4l2_subdev
*sd
= vin_to_source(vin
);
558 timings
->pad
= vin
->digital
->sink_pad
;
560 ret
= v4l2_subdev_call(sd
, pad
, enum_dv_timings
, timings
);
567 static int rvin_s_dv_timings(struct file
*file
, void *priv_fh
,
568 struct v4l2_dv_timings
*timings
)
570 struct rvin_dev
*vin
= video_drvdata(file
);
571 struct v4l2_subdev
*sd
= vin_to_source(vin
);
574 ret
= v4l2_subdev_call(sd
, video
, s_dv_timings
, timings
);
578 /* Changing the timings will change the width/height */
579 return rvin_reset_format(vin
);
582 static int rvin_g_dv_timings(struct file
*file
, void *priv_fh
,
583 struct v4l2_dv_timings
*timings
)
585 struct rvin_dev
*vin
= video_drvdata(file
);
586 struct v4l2_subdev
*sd
= vin_to_source(vin
);
588 return v4l2_subdev_call(sd
, video
, g_dv_timings
, timings
);
591 static int rvin_query_dv_timings(struct file
*file
, void *priv_fh
,
592 struct v4l2_dv_timings
*timings
)
594 struct rvin_dev
*vin
= video_drvdata(file
);
595 struct v4l2_subdev
*sd
= vin_to_source(vin
);
597 return v4l2_subdev_call(sd
, video
, query_dv_timings
, timings
);
600 static int rvin_dv_timings_cap(struct file
*file
, void *priv_fh
,
601 struct v4l2_dv_timings_cap
*cap
)
603 struct rvin_dev
*vin
= video_drvdata(file
);
604 struct v4l2_subdev
*sd
= vin_to_source(vin
);
610 cap
->pad
= vin
->digital
->sink_pad
;
612 ret
= v4l2_subdev_call(sd
, pad
, dv_timings_cap
, cap
);
619 static int rvin_g_edid(struct file
*file
, void *fh
, struct v4l2_edid
*edid
)
621 struct rvin_dev
*vin
= video_drvdata(file
);
622 struct v4l2_subdev
*sd
= vin_to_source(vin
);
628 edid
->pad
= vin
->digital
->sink_pad
;
630 ret
= v4l2_subdev_call(sd
, pad
, get_edid
, edid
);
637 static int rvin_s_edid(struct file
*file
, void *fh
, struct v4l2_edid
*edid
)
639 struct rvin_dev
*vin
= video_drvdata(file
);
640 struct v4l2_subdev
*sd
= vin_to_source(vin
);
646 edid
->pad
= vin
->digital
->sink_pad
;
648 ret
= v4l2_subdev_call(sd
, pad
, set_edid
, edid
);
655 static const struct v4l2_ioctl_ops rvin_ioctl_ops
= {
656 .vidioc_querycap
= rvin_querycap
,
657 .vidioc_try_fmt_vid_cap
= rvin_try_fmt_vid_cap
,
658 .vidioc_g_fmt_vid_cap
= rvin_g_fmt_vid_cap
,
659 .vidioc_s_fmt_vid_cap
= rvin_s_fmt_vid_cap
,
660 .vidioc_enum_fmt_vid_cap
= rvin_enum_fmt_vid_cap
,
662 .vidioc_g_selection
= rvin_g_selection
,
663 .vidioc_s_selection
= rvin_s_selection
,
665 .vidioc_cropcap
= rvin_cropcap
,
667 .vidioc_enum_input
= rvin_enum_input
,
668 .vidioc_g_input
= rvin_g_input
,
669 .vidioc_s_input
= rvin_s_input
,
671 .vidioc_dv_timings_cap
= rvin_dv_timings_cap
,
672 .vidioc_enum_dv_timings
= rvin_enum_dv_timings
,
673 .vidioc_g_dv_timings
= rvin_g_dv_timings
,
674 .vidioc_s_dv_timings
= rvin_s_dv_timings
,
675 .vidioc_query_dv_timings
= rvin_query_dv_timings
,
677 .vidioc_g_edid
= rvin_g_edid
,
678 .vidioc_s_edid
= rvin_s_edid
,
680 .vidioc_querystd
= rvin_querystd
,
681 .vidioc_g_std
= rvin_g_std
,
682 .vidioc_s_std
= rvin_s_std
,
684 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
685 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
686 .vidioc_querybuf
= vb2_ioctl_querybuf
,
687 .vidioc_qbuf
= vb2_ioctl_qbuf
,
688 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
689 .vidioc_expbuf
= vb2_ioctl_expbuf
,
690 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
691 .vidioc_streamon
= vb2_ioctl_streamon
,
692 .vidioc_streamoff
= vb2_ioctl_streamoff
,
694 .vidioc_log_status
= v4l2_ctrl_log_status
,
695 .vidioc_subscribe_event
= rvin_subscribe_event
,
696 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
699 /* -----------------------------------------------------------------------------
703 static int rvin_power_on(struct rvin_dev
*vin
)
706 struct v4l2_subdev
*sd
= vin_to_source(vin
);
708 pm_runtime_get_sync(vin
->v4l2_dev
.dev
);
710 ret
= v4l2_subdev_call(sd
, core
, s_power
, 1);
711 if (ret
< 0 && ret
!= -ENOIOCTLCMD
&& ret
!= -ENODEV
)
716 static int rvin_power_off(struct rvin_dev
*vin
)
719 struct v4l2_subdev
*sd
= vin_to_source(vin
);
721 ret
= v4l2_subdev_call(sd
, core
, s_power
, 0);
723 pm_runtime_put(vin
->v4l2_dev
.dev
);
725 if (ret
< 0 && ret
!= -ENOIOCTLCMD
&& ret
!= -ENODEV
)
731 static int rvin_initialize_device(struct file
*file
)
733 struct rvin_dev
*vin
= video_drvdata(file
);
736 struct v4l2_format f
= {
737 .type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
,
739 .width
= vin
->format
.width
,
740 .height
= vin
->format
.height
,
741 .field
= vin
->format
.field
,
742 .colorspace
= vin
->format
.colorspace
,
743 .pixelformat
= vin
->format
.pixelformat
,
747 ret
= rvin_power_on(vin
);
751 pm_runtime_enable(&vin
->vdev
.dev
);
752 ret
= pm_runtime_resume(&vin
->vdev
.dev
);
753 if (ret
< 0 && ret
!= -ENOSYS
)
757 * Try to configure with default parameters. Notice: this is the
758 * very first open, so, we cannot race against other calls,
759 * apart from someone else calling open() simultaneously, but
760 * .host_lock is protecting us against it.
762 ret
= rvin_s_fmt_vid_cap(file
, NULL
, &f
);
766 v4l2_ctrl_handler_setup(&vin
->ctrl_handler
);
770 pm_runtime_disable(&vin
->vdev
.dev
);
777 static int rvin_open(struct file
*file
)
779 struct rvin_dev
*vin
= video_drvdata(file
);
782 mutex_lock(&vin
->lock
);
784 file
->private_data
= vin
;
786 ret
= v4l2_fh_open(file
);
790 if (!v4l2_fh_is_singular_file(file
))
793 if (rvin_initialize_device(file
)) {
794 v4l2_fh_release(file
);
799 mutex_unlock(&vin
->lock
);
803 static int rvin_release(struct file
*file
)
805 struct rvin_dev
*vin
= video_drvdata(file
);
809 mutex_lock(&vin
->lock
);
811 /* Save the singular status before we call the clean-up helper */
812 fh_singular
= v4l2_fh_is_singular_file(file
);
814 /* the release helper will cleanup any on-going streaming */
815 ret
= _vb2_fop_release(file
, NULL
);
818 * If this was the last open file.
819 * Then de-initialize hw module.
822 pm_runtime_suspend(&vin
->vdev
.dev
);
823 pm_runtime_disable(&vin
->vdev
.dev
);
827 mutex_unlock(&vin
->lock
);
832 static const struct v4l2_file_operations rvin_fops
= {
833 .owner
= THIS_MODULE
,
834 .unlocked_ioctl
= video_ioctl2
,
836 .release
= rvin_release
,
837 .poll
= vb2_fop_poll
,
838 .mmap
= vb2_fop_mmap
,
839 .read
= vb2_fop_read
,
842 void rvin_v4l2_remove(struct rvin_dev
*vin
)
844 v4l2_info(&vin
->v4l2_dev
, "Removing %s\n",
845 video_device_node_name(&vin
->vdev
));
847 /* Checks internaly if handlers have been init or not */
848 v4l2_ctrl_handler_free(&vin
->ctrl_handler
);
850 /* Checks internaly if vdev have been init or not */
851 video_unregister_device(&vin
->vdev
);
854 static void rvin_notify(struct v4l2_subdev
*sd
,
855 unsigned int notification
, void *arg
)
857 struct rvin_dev
*vin
=
858 container_of(sd
->v4l2_dev
, struct rvin_dev
, v4l2_dev
);
860 switch (notification
) {
861 case V4L2_DEVICE_NOTIFY_EVENT
:
862 v4l2_event_queue(&vin
->vdev
, arg
);
869 int rvin_v4l2_probe(struct rvin_dev
*vin
)
871 struct video_device
*vdev
= &vin
->vdev
;
872 struct v4l2_subdev
*sd
= vin_to_source(vin
);
875 v4l2_set_subdev_hostdata(sd
, vin
);
877 vin
->v4l2_dev
.notify
= rvin_notify
;
879 ret
= v4l2_subdev_call(sd
, video
, g_tvnorms
, &vin
->vdev
.tvnorms
);
880 if (ret
< 0 && ret
!= -ENOIOCTLCMD
&& ret
!= -ENODEV
)
883 if (vin
->vdev
.tvnorms
== 0) {
884 /* Disable the STD API if there are no tvnorms defined */
885 v4l2_disable_ioctl(&vin
->vdev
, VIDIOC_G_STD
);
886 v4l2_disable_ioctl(&vin
->vdev
, VIDIOC_S_STD
);
887 v4l2_disable_ioctl(&vin
->vdev
, VIDIOC_QUERYSTD
);
888 v4l2_disable_ioctl(&vin
->vdev
, VIDIOC_ENUMSTD
);
891 /* Add the controls */
893 * Currently the subdev with the largest number of controls (13) is
894 * ov6550. So let's pick 16 as a hint for the control handler. Note
895 * that this is a hint only: too large and you waste some memory, too
896 * small and there is a (very) small performance hit when looking up
897 * controls in the internal hash.
899 ret
= v4l2_ctrl_handler_init(&vin
->ctrl_handler
, 16);
903 ret
= v4l2_ctrl_add_handler(&vin
->ctrl_handler
, sd
->ctrl_handler
, NULL
);
908 vdev
->fops
= &rvin_fops
;
909 vdev
->v4l2_dev
= &vin
->v4l2_dev
;
910 vdev
->queue
= &vin
->queue
;
911 strlcpy(vdev
->name
, KBUILD_MODNAME
, sizeof(vdev
->name
));
912 vdev
->release
= video_device_release_empty
;
913 vdev
->ioctl_ops
= &rvin_ioctl_ops
;
914 vdev
->lock
= &vin
->lock
;
915 vdev
->ctrl_handler
= &vin
->ctrl_handler
;
916 vdev
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
|
919 vin
->format
.pixelformat
= RVIN_DEFAULT_FORMAT
;
920 rvin_reset_format(vin
);
922 ret
= video_register_device(&vin
->vdev
, VFL_TYPE_GRABBER
, -1);
924 vin_err(vin
, "Failed to register video device\n");
928 video_set_drvdata(&vin
->vdev
, vin
);
930 v4l2_info(&vin
->v4l2_dev
, "Device registered as %s\n",
931 video_device_node_name(&vin
->vdev
));