Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / platform / rcar-vin / rcar-v4l2.c
blobb479b882da12f62d1d14ad5bed14897baafb91fd
1 /*
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>
23 #include "rcar-vin.h"
25 #define RVIN_DEFAULT_FORMAT V4L2_PIX_FMT_YUYV
26 #define RVIN_MAX_WIDTH 2048
27 #define RVIN_MAX_HEIGHT 2048
29 /* -----------------------------------------------------------------------------
30 * Format Conversions
33 static const struct rvin_video_format rvin_formats[] = {
35 .fourcc = V4L2_PIX_FMT_NV16,
36 .bpp = 1,
39 .fourcc = V4L2_PIX_FMT_YUYV,
40 .bpp = 2,
43 .fourcc = V4L2_PIX_FMT_UYVY,
44 .bpp = 2,
47 .fourcc = V4L2_PIX_FMT_RGB565,
48 .bpp = 2,
51 .fourcc = V4L2_PIX_FMT_XRGB555,
52 .bpp = 2,
55 .fourcc = V4L2_PIX_FMT_XBGR32,
56 .bpp = 4,
60 const struct rvin_video_format *rvin_format_from_pixel(u32 pixelformat)
62 int i;
64 for (i = 0; i < ARRAY_SIZE(rvin_formats); i++)
65 if (rvin_formats[i].fourcc == pixelformat)
66 return rvin_formats + i;
68 return NULL;
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);
77 if (WARN_ON(!fmt))
78 return -EINVAL;
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 /* -----------------------------------------------------------------------------
92 * V4L2
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;
112 int ret;
114 fmt.pad = vin->digital->source_pad;
116 ret = v4l2_subdev_call(vin_to_source(vin), pad, get_fmt, NULL, &fmt);
117 if (ret)
118 return ret;
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) {
137 case V4L2_FIELD_TOP:
138 case V4L2_FIELD_BOTTOM:
139 case V4L2_FIELD_ALTERNATE:
140 vin->format.height /= 2;
141 break;
142 case V4L2_FIELD_NONE:
143 case V4L2_FIELD_INTERLACED_TB:
144 case V4L2_FIELD_INTERLACED_BT:
145 case V4L2_FIELD_INTERLACED:
146 break;
147 default:
148 vin->format.field = V4L2_FIELD_NONE;
149 break;
152 rvin_reset_crop_compose(vin);
154 vin->format.bytesperline = rvin_format_bytesperline(&vin->format);
155 vin->format.sizeimage = rvin_format_sizeimage(&vin->format);
157 return 0;
160 static int __rvin_try_format_source(struct rvin_dev *vin,
161 u32 which,
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 = {
168 .which = which,
170 enum v4l2_field field;
171 int ret;
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);
178 if (pad_cfg == NULL)
179 return -ENOMEM;
181 format.pad = vin->digital->source_pad;
183 field = pix->field;
185 ret = v4l2_subdev_call(sd, pad, set_fmt, pad_cfg, &format);
186 if (ret < 0 && ret != -ENOIOCTLCMD)
187 goto done;
189 v4l2_fill_pix_format(pix, &format.format);
191 pix->field = field;
193 source->width = pix->width;
194 source->height = pix->height;
196 vin_dbg(vin, "Source resolution: %ux%u\n", source->width,
197 source->height);
199 done:
200 v4l2_subdev_free_pad_config(pad_cfg);
201 return ret;
204 static int __rvin_try_format(struct rvin_dev *vin,
205 u32 which,
206 struct v4l2_pix_format *pix,
207 struct rvin_source_fmt *source)
209 u32 rwidth, rheight, walign;
210 int ret;
212 /* Requested */
213 rwidth = pix->width;
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;
229 pix->sizeimage = 0;
231 /* Limit to source capabilities */
232 ret = __rvin_try_format_source(vin, which, pix, source);
233 if (ret)
234 return ret;
236 switch (pix->field) {
237 case V4L2_FIELD_TOP:
238 case V4L2_FIELD_BOTTOM:
239 case V4L2_FIELD_ALTERNATE:
240 pix->height /= 2;
241 source->height /= 2;
242 break;
243 case V4L2_FIELD_NONE:
244 case V4L2_FIELD_INTERLACED_TB:
245 case V4L2_FIELD_INTERLACED_BT:
246 case V4L2_FIELD_INTERLACED:
247 break;
248 default:
249 pix->field = V4L2_FIELD_NONE;
250 break;
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");
271 return -EINVAL;
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);
278 return 0;
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",
289 dev_name(vin->dev));
290 return 0;
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,
300 &source);
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;
308 int ret;
310 if (vb2_is_busy(&vin->queue))
311 return -EBUSY;
313 ret = __rvin_try_format(vin, V4L2_SUBDEV_FORMAT_ACTIVE, &f->fmt.pix,
314 &source);
315 if (ret)
316 return ret;
318 vin->source.width = source.width;
319 vin->source.height = source.height;
321 vin->format = f->fmt.pix;
323 rvin_reset_crop_compose(vin);
325 return 0;
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;
335 return 0;
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))
342 return -EINVAL;
344 f->pixelformat = rvin_formats[f->index].fourcc;
346 return 0;
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)
355 return -EINVAL;
357 switch (s->target) {
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;
363 break;
364 case V4L2_SEL_TGT_CROP:
365 s->r = vin->crop;
366 break;
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;
372 break;
373 case V4L2_SEL_TGT_COMPOSE:
374 s->r = vin->compose;
375 break;
376 default:
377 return -EINVAL;
380 return 0;
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 = {
391 .width = 6,
392 .height = 2,
395 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
396 return -EINVAL;
398 v4l2_rect_set_min_size(&r, &min_rect);
400 switch (s->target) {
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);
419 break;
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)
433 r.top--;
435 fmt = rvin_format_from_pixel(vin->format.pixelformat);
436 while ((r.left * fmt->bpp) & HW_BUFFER_MASK)
437 r.left--;
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);
444 break;
445 default:
446 return -EINVAL;
449 /* HW supports modifying configuration while running */
450 rvin_crop_scale_comp(vin);
452 return 0;
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)
462 return -EINVAL;
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);
472 int ret;
474 if (i->index != 0)
475 return -EINVAL;
477 ret = v4l2_subdev_call(sd, video, g_input_status, &i->status);
478 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
479 return ret;
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;
485 i->std = 0;
486 } else {
487 i->capabilities = V4L2_IN_CAP_STD;
488 i->std = vin->vdev.tvnorms;
491 strlcpy(i->name, "Camera", sizeof(i->name));
493 return 0;
496 static int rvin_g_input(struct file *file, void *priv, unsigned int *i)
498 *i = 0;
499 return 0;
502 static int rvin_s_input(struct file *file, void *priv, unsigned int i)
504 if (i > 0)
505 return -EINVAL;
506 return 0;
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);
520 int ret;
522 ret = v4l2_subdev_call(vin_to_source(vin), video, s_std, a);
523 if (ret < 0)
524 return ret;
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)
541 switch (sub->type) {
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);
553 int ret;
555 if (timings->pad)
556 return -EINVAL;
558 timings->pad = vin->digital->sink_pad;
560 ret = v4l2_subdev_call(sd, pad, enum_dv_timings, timings);
562 timings->pad = 0;
564 return ret;
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);
572 int ret;
574 ret = v4l2_subdev_call(sd, video, s_dv_timings, timings);
575 if (ret)
576 return ret;
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);
605 int ret;
607 if (cap->pad)
608 return -EINVAL;
610 cap->pad = vin->digital->sink_pad;
612 ret = v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
614 cap->pad = 0;
616 return ret;
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);
623 int ret;
625 if (edid->pad)
626 return -EINVAL;
628 edid->pad = vin->digital->sink_pad;
630 ret = v4l2_subdev_call(sd, pad, get_edid, edid);
632 edid->pad = 0;
634 return ret;
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);
641 int ret;
643 if (edid->pad)
644 return -EINVAL;
646 edid->pad = vin->digital->sink_pad;
648 ret = v4l2_subdev_call(sd, pad, set_edid, edid);
650 edid->pad = 0;
652 return ret;
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 /* -----------------------------------------------------------------------------
700 * File Operations
703 static int rvin_power_on(struct rvin_dev *vin)
705 int ret;
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)
712 return ret;
713 return 0;
716 static int rvin_power_off(struct rvin_dev *vin)
718 int ret;
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)
726 return ret;
728 return 0;
731 static int rvin_initialize_device(struct file *file)
733 struct rvin_dev *vin = video_drvdata(file);
734 int ret;
736 struct v4l2_format f = {
737 .type = V4L2_BUF_TYPE_VIDEO_CAPTURE,
738 .fmt.pix = {
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);
748 if (ret < 0)
749 return ret;
751 pm_runtime_enable(&vin->vdev.dev);
752 ret = pm_runtime_resume(&vin->vdev.dev);
753 if (ret < 0 && ret != -ENOSYS)
754 goto eresume;
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);
763 if (ret < 0)
764 goto esfmt;
766 v4l2_ctrl_handler_setup(&vin->ctrl_handler);
768 return 0;
769 esfmt:
770 pm_runtime_disable(&vin->vdev.dev);
771 eresume:
772 rvin_power_off(vin);
774 return ret;
777 static int rvin_open(struct file *file)
779 struct rvin_dev *vin = video_drvdata(file);
780 int ret;
782 mutex_lock(&vin->lock);
784 file->private_data = vin;
786 ret = v4l2_fh_open(file);
787 if (ret)
788 goto unlock;
790 if (!v4l2_fh_is_singular_file(file))
791 goto unlock;
793 if (rvin_initialize_device(file)) {
794 v4l2_fh_release(file);
795 ret = -ENODEV;
798 unlock:
799 mutex_unlock(&vin->lock);
800 return ret;
803 static int rvin_release(struct file *file)
805 struct rvin_dev *vin = video_drvdata(file);
806 bool fh_singular;
807 int ret;
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.
821 if (fh_singular) {
822 pm_runtime_suspend(&vin->vdev.dev);
823 pm_runtime_disable(&vin->vdev.dev);
824 rvin_power_off(vin);
827 mutex_unlock(&vin->lock);
829 return ret;
832 static const struct v4l2_file_operations rvin_fops = {
833 .owner = THIS_MODULE,
834 .unlocked_ioctl = video_ioctl2,
835 .open = rvin_open,
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);
863 break;
864 default:
865 break;
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);
873 int ret;
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)
881 return ret;
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);
900 if (ret < 0)
901 return ret;
903 ret = v4l2_ctrl_add_handler(&vin->ctrl_handler, sd->ctrl_handler, NULL);
904 if (ret < 0)
905 return ret;
907 /* video node */
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 |
917 V4L2_CAP_READWRITE;
919 vin->format.pixelformat = RVIN_DEFAULT_FORMAT;
920 rvin_reset_format(vin);
922 ret = video_register_device(&vin->vdev, VFL_TYPE_GRABBER, -1);
923 if (ret) {
924 vin_err(vin, "Failed to register video device\n");
925 return ret;
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));
933 return ret;