xref: /linux/drivers/media/platform/renesas/rcar-vin/rcar-v4l2.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Driver for Renesas R-Car VIN
4  *
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
9  *
10  * Based on the soc-camera rcar_vin driver
11  */
12 
13 #include <linux/pm_runtime.h>
14 
15 #include <media/v4l2-event.h>
16 #include <media/v4l2-ioctl.h>
17 #include <media/v4l2-mc.h>
18 #include <media/v4l2-rect.h>
19 
20 #include "rcar-vin.h"
21 
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
27 
28 /* -----------------------------------------------------------------------------
29  * Format Conversions
30  */
31 
32 static const struct rvin_video_format rvin_formats[] = {
33 	{
34 		.fourcc			= V4L2_PIX_FMT_NV12,
35 		.bpp			= 1,
36 	},
37 	{
38 		.fourcc			= V4L2_PIX_FMT_NV16,
39 		.bpp			= 1,
40 	},
41 	{
42 		.fourcc			= V4L2_PIX_FMT_YUYV,
43 		.bpp			= 2,
44 	},
45 	{
46 		.fourcc			= V4L2_PIX_FMT_UYVY,
47 		.bpp			= 2,
48 	},
49 	{
50 		.fourcc			= V4L2_PIX_FMT_RGB565,
51 		.bpp			= 2,
52 	},
53 	{
54 		.fourcc			= V4L2_PIX_FMT_XRGB555,
55 		.bpp			= 2,
56 	},
57 	{
58 		.fourcc			= V4L2_PIX_FMT_XBGR32,
59 		.bpp			= 4,
60 	},
61 	{
62 		.fourcc			= V4L2_PIX_FMT_ARGB555,
63 		.bpp			= 2,
64 	},
65 	{
66 		.fourcc			= V4L2_PIX_FMT_ABGR32,
67 		.bpp			= 4,
68 	},
69 	{
70 		.fourcc			= V4L2_PIX_FMT_SBGGR8,
71 		.bpp			= 1,
72 	},
73 	{
74 		.fourcc			= V4L2_PIX_FMT_SGBRG8,
75 		.bpp			= 1,
76 	},
77 	{
78 		.fourcc			= V4L2_PIX_FMT_SGRBG8,
79 		.bpp			= 1,
80 	},
81 	{
82 		.fourcc			= V4L2_PIX_FMT_SRGGB8,
83 		.bpp			= 1,
84 	},
85 	{
86 		.fourcc			= V4L2_PIX_FMT_GREY,
87 		.bpp			= 1,
88 	},
89 	{
90 		.fourcc			= V4L2_PIX_FMT_SBGGR10,
91 		.bpp			= 4,
92 	},
93 	{
94 		.fourcc			= V4L2_PIX_FMT_SGBRG10,
95 		.bpp			= 4,
96 	},
97 	{
98 		.fourcc			= V4L2_PIX_FMT_SGRBG10,
99 		.bpp			= 4,
100 	},
101 	{
102 		.fourcc			= V4L2_PIX_FMT_SRGGB10,
103 		.bpp			= 4,
104 	},
105 };
106 
107 const struct rvin_video_format *rvin_format_from_pixel(struct rvin_dev *vin,
108 						       u32 pixelformat)
109 {
110 	int i;
111 
112 	switch (pixelformat) {
113 	case V4L2_PIX_FMT_XBGR32:
114 		if (vin->info->model == RCAR_M1)
115 			return NULL;
116 		break;
117 	case V4L2_PIX_FMT_NV12:
118 		/*
119 		 * If NV12 is supported it's only supported on channels 0, 1, 4,
120 		 * 5, 8, 9, 12 and 13.
121 		 */
122 		if (!vin->info->nv12 || !(BIT(vin->id) & 0x3333))
123 			return NULL;
124 		break;
125 	case V4L2_PIX_FMT_SBGGR10:
126 	case V4L2_PIX_FMT_SGBRG10:
127 	case V4L2_PIX_FMT_SGRBG10:
128 	case V4L2_PIX_FMT_SRGGB10:
129 		if (!vin->info->raw10)
130 			return NULL;
131 		break;
132 	default:
133 		break;
134 	}
135 
136 	for (i = 0; i < ARRAY_SIZE(rvin_formats); i++)
137 		if (rvin_formats[i].fourcc == pixelformat)
138 			return rvin_formats + i;
139 
140 	return NULL;
141 }
142 
143 static u32 rvin_format_bytesperline(struct rvin_dev *vin,
144 				    struct v4l2_pix_format *pix)
145 {
146 	const struct rvin_video_format *fmt;
147 	u32 align;
148 
149 	fmt = rvin_format_from_pixel(vin, pix->pixelformat);
150 
151 	if (WARN_ON(!fmt))
152 		return -EINVAL;
153 
154 	switch (pix->pixelformat) {
155 	case V4L2_PIX_FMT_NV12:
156 	case V4L2_PIX_FMT_NV16:
157 		align = 0x20;
158 		break;
159 	default:
160 		align = 0x10;
161 		break;
162 	}
163 
164 	if (V4L2_FIELD_IS_SEQUENTIAL(pix->field))
165 		align = 0x80;
166 
167 	return ALIGN(pix->width, align) * fmt->bpp;
168 }
169 
170 static u32 rvin_format_sizeimage(struct v4l2_pix_format *pix)
171 {
172 	switch (pix->pixelformat) {
173 	case V4L2_PIX_FMT_NV12:
174 		return pix->bytesperline * pix->height * 3 / 2;
175 	case V4L2_PIX_FMT_NV16:
176 		return pix->bytesperline * pix->height * 2;
177 	default:
178 		return pix->bytesperline * pix->height;
179 	}
180 }
181 
182 static void rvin_format_align(struct rvin_dev *vin, struct v4l2_pix_format *pix)
183 {
184 	u32 walign;
185 
186 	if (!rvin_format_from_pixel(vin, pix->pixelformat))
187 		pix->pixelformat = RVIN_DEFAULT_FORMAT;
188 
189 	switch (pix->field) {
190 	case V4L2_FIELD_TOP:
191 	case V4L2_FIELD_BOTTOM:
192 	case V4L2_FIELD_NONE:
193 	case V4L2_FIELD_INTERLACED_TB:
194 	case V4L2_FIELD_INTERLACED_BT:
195 	case V4L2_FIELD_INTERLACED:
196 	case V4L2_FIELD_ALTERNATE:
197 	case V4L2_FIELD_SEQ_TB:
198 	case V4L2_FIELD_SEQ_BT:
199 		break;
200 	default:
201 		pix->field = RVIN_DEFAULT_FIELD;
202 		break;
203 	}
204 
205 	/* Hardware limits width alignment based on format. */
206 	switch (pix->pixelformat) {
207 	/* Multiple of 32 (2^5) for NV12/16. */
208 	case V4L2_PIX_FMT_NV12:
209 	case V4L2_PIX_FMT_NV16:
210 		walign = 5;
211 		break;
212 	/* Multiple of 2 (2^1) for YUV. */
213 	case V4L2_PIX_FMT_YUYV:
214 	case V4L2_PIX_FMT_UYVY:
215 		walign = 1;
216 		break;
217 	/* No multiple for RGB. */
218 	default:
219 		walign = 0;
220 		break;
221 	}
222 
223 	/* Limit to VIN capabilities */
224 	v4l_bound_align_image(&pix->width, 5, vin->info->max_width, walign,
225 			      &pix->height, 2, vin->info->max_height, 0, 0);
226 
227 	pix->bytesperline = rvin_format_bytesperline(vin, pix);
228 	pix->sizeimage = rvin_format_sizeimage(pix);
229 
230 	vin_dbg(vin, "Format %ux%u bpl: %u size: %u\n",
231 		pix->width, pix->height, pix->bytesperline, pix->sizeimage);
232 }
233 
234 /* -----------------------------------------------------------------------------
235  * V4L2
236  */
237 
238 static int rvin_reset_format(struct rvin_dev *vin)
239 {
240 	struct v4l2_subdev_format fmt = {
241 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
242 		.pad = vin->parallel.source_pad,
243 	};
244 	int ret;
245 
246 	ret = v4l2_subdev_call(vin_to_source(vin), pad, get_fmt, NULL, &fmt);
247 	if (ret)
248 		return ret;
249 
250 	v4l2_fill_pix_format(&vin->format, &fmt.format);
251 
252 	vin->crop.top = 0;
253 	vin->crop.left = 0;
254 	vin->crop.width = vin->format.width;
255 	vin->crop.height = vin->format.height;
256 
257 	/*  Make use of the hardware interlacer by default. */
258 	if (vin->format.field == V4L2_FIELD_ALTERNATE) {
259 		vin->format.field = V4L2_FIELD_INTERLACED;
260 		vin->format.height *= 2;
261 	}
262 
263 	rvin_format_align(vin, &vin->format);
264 
265 	vin->compose.top = 0;
266 	vin->compose.left = 0;
267 	vin->compose.width = vin->format.width;
268 	vin->compose.height = vin->format.height;
269 
270 	return 0;
271 }
272 
273 static int rvin_try_format(struct rvin_dev *vin, u32 which,
274 			   struct v4l2_pix_format *pix,
275 			   struct v4l2_rect *src_rect)
276 {
277 	struct v4l2_subdev *sd = vin_to_source(vin);
278 	struct v4l2_subdev_state *sd_state;
279 	static struct lock_class_key key;
280 	struct v4l2_subdev_format format = {
281 		.which = which,
282 		.pad = vin->parallel.source_pad,
283 	};
284 	enum v4l2_field field;
285 	u32 width, height;
286 	int ret;
287 
288 	/*
289 	 * FIXME: Drop this call, drivers are not supposed to use
290 	 * __v4l2_subdev_state_alloc().
291 	 */
292 	sd_state = __v4l2_subdev_state_alloc(sd, "rvin:state->lock", &key);
293 	if (IS_ERR(sd_state))
294 		return PTR_ERR(sd_state);
295 
296 	if (!rvin_format_from_pixel(vin, pix->pixelformat))
297 		pix->pixelformat = RVIN_DEFAULT_FORMAT;
298 
299 	v4l2_fill_mbus_format(&format.format, pix, vin->mbus_code);
300 
301 	/* Allow the video device to override field and to scale */
302 	field = pix->field;
303 	width = pix->width;
304 	height = pix->height;
305 
306 	ret = v4l2_subdev_call(sd, pad, set_fmt, sd_state, &format);
307 	if (ret < 0 && ret != -ENOIOCTLCMD)
308 		goto done;
309 	ret = 0;
310 
311 	v4l2_fill_pix_format(pix, &format.format);
312 
313 	if (src_rect) {
314 		src_rect->top = 0;
315 		src_rect->left = 0;
316 		src_rect->width = pix->width;
317 		src_rect->height = pix->height;
318 	}
319 
320 	if (field != V4L2_FIELD_ANY)
321 		pix->field = field;
322 
323 	pix->width = width;
324 	pix->height = height;
325 
326 	rvin_format_align(vin, pix);
327 done:
328 	__v4l2_subdev_state_free(sd_state);
329 
330 	return ret;
331 }
332 
333 static int rvin_querycap(struct file *file, void *priv,
334 			 struct v4l2_capability *cap)
335 {
336 	strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver));
337 	strscpy(cap->card, "R_Car_VIN", sizeof(cap->card));
338 	return 0;
339 }
340 
341 static int rvin_try_fmt_vid_cap(struct file *file, void *priv,
342 				struct v4l2_format *f)
343 {
344 	struct rvin_dev *vin = video_drvdata(file);
345 
346 	return rvin_try_format(vin, V4L2_SUBDEV_FORMAT_TRY, &f->fmt.pix, NULL);
347 }
348 
349 static int rvin_s_fmt_vid_cap(struct file *file, void *priv,
350 			      struct v4l2_format *f)
351 {
352 	struct rvin_dev *vin = video_drvdata(file);
353 	struct v4l2_rect fmt_rect, src_rect;
354 	int ret;
355 
356 	if (vb2_is_busy(&vin->queue))
357 		return -EBUSY;
358 
359 	ret = rvin_try_format(vin, V4L2_SUBDEV_FORMAT_ACTIVE, &f->fmt.pix,
360 			      &src_rect);
361 	if (ret)
362 		return ret;
363 
364 	vin->format = f->fmt.pix;
365 
366 	fmt_rect.top = 0;
367 	fmt_rect.left = 0;
368 	fmt_rect.width = vin->format.width;
369 	fmt_rect.height = vin->format.height;
370 
371 	v4l2_rect_map_inside(&vin->crop, &src_rect);
372 	v4l2_rect_map_inside(&vin->compose, &fmt_rect);
373 
374 	return 0;
375 }
376 
377 static int rvin_g_fmt_vid_cap(struct file *file, void *priv,
378 			      struct v4l2_format *f)
379 {
380 	struct rvin_dev *vin = video_drvdata(file);
381 
382 	f->fmt.pix = vin->format;
383 
384 	return 0;
385 }
386 
387 static int rvin_enum_fmt_vid_cap(struct file *file, void *priv,
388 				 struct v4l2_fmtdesc *f)
389 {
390 	struct rvin_dev *vin = video_drvdata(file);
391 	unsigned int i;
392 	int matched;
393 
394 	/*
395 	 * If mbus_code is set only enumerate supported pixel formats for that
396 	 * bus code. Converting from YCbCr to RGB and RGB to YCbCr is possible
397 	 * with VIN, so all supported YCbCr and RGB media bus codes can produce
398 	 * all of the related pixel formats. If mbus_code is not set enumerate
399 	 * all possible pixelformats.
400 	 *
401 	 * TODO: Once raw MEDIA_BUS_FMT_SRGGB12_1X12 format is added to the
402 	 * driver this needs to be extended so raw media bus code only result in
403 	 * raw pixel format.
404 	 */
405 	switch (f->mbus_code) {
406 	case 0:
407 	case MEDIA_BUS_FMT_YUYV8_1X16:
408 	case MEDIA_BUS_FMT_UYVY8_1X16:
409 	case MEDIA_BUS_FMT_UYVY8_2X8:
410 	case MEDIA_BUS_FMT_UYVY10_2X10:
411 	case MEDIA_BUS_FMT_RGB888_1X24:
412 		break;
413 	case MEDIA_BUS_FMT_SBGGR8_1X8:
414 		if (f->index)
415 			return -EINVAL;
416 		f->pixelformat = V4L2_PIX_FMT_SBGGR8;
417 		return 0;
418 	case MEDIA_BUS_FMT_SGBRG8_1X8:
419 		if (f->index)
420 			return -EINVAL;
421 		f->pixelformat = V4L2_PIX_FMT_SGBRG8;
422 		return 0;
423 	case MEDIA_BUS_FMT_SGRBG8_1X8:
424 		if (f->index)
425 			return -EINVAL;
426 		f->pixelformat = V4L2_PIX_FMT_SGRBG8;
427 		return 0;
428 	case MEDIA_BUS_FMT_SRGGB8_1X8:
429 		if (f->index)
430 			return -EINVAL;
431 		f->pixelformat = V4L2_PIX_FMT_SRGGB8;
432 		return 0;
433 	case MEDIA_BUS_FMT_SBGGR10_1X10:
434 		if (f->index)
435 			return -EINVAL;
436 		f->pixelformat = V4L2_PIX_FMT_SBGGR10;
437 		return 0;
438 	case MEDIA_BUS_FMT_SGBRG10_1X10:
439 		if (f->index)
440 			return -EINVAL;
441 		f->pixelformat = V4L2_PIX_FMT_SGBRG10;
442 		return 0;
443 	case MEDIA_BUS_FMT_SGRBG10_1X10:
444 		if (f->index)
445 			return -EINVAL;
446 		f->pixelformat = V4L2_PIX_FMT_SGRBG10;
447 		return 0;
448 	case MEDIA_BUS_FMT_SRGGB10_1X10:
449 		if (f->index)
450 			return -EINVAL;
451 		f->pixelformat = V4L2_PIX_FMT_SRGGB10;
452 		return 0;
453 	default:
454 		return -EINVAL;
455 	}
456 
457 	matched = -1;
458 	for (i = 0; i < ARRAY_SIZE(rvin_formats); i++) {
459 		if (rvin_format_from_pixel(vin, rvin_formats[i].fourcc))
460 			matched++;
461 
462 		if (matched == f->index) {
463 			f->pixelformat = rvin_formats[i].fourcc;
464 			return 0;
465 		}
466 	}
467 
468 	return -EINVAL;
469 }
470 
471 static int rvin_remote_rectangle(struct rvin_dev *vin, struct v4l2_rect *rect)
472 {
473 	struct v4l2_subdev_format fmt = {
474 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
475 	};
476 	struct v4l2_subdev *sd;
477 	unsigned int index;
478 	int ret;
479 
480 	if (vin->info->use_mc) {
481 		struct media_pad *pad = media_pad_remote_pad_first(&vin->pad);
482 
483 		if (!pad)
484 			return -EINVAL;
485 
486 		sd = media_entity_to_v4l2_subdev(pad->entity);
487 		index = pad->index;
488 	} else {
489 		sd = vin_to_source(vin);
490 		index = vin->parallel.source_pad;
491 	}
492 
493 	fmt.pad = index;
494 	ret = v4l2_subdev_call(sd, pad, get_fmt, NULL, &fmt);
495 	if (ret)
496 		return ret;
497 
498 	rect->left = rect->top = 0;
499 	rect->width = fmt.format.width;
500 	rect->height = fmt.format.height;
501 
502 	if (fmt.format.field == V4L2_FIELD_ALTERNATE) {
503 		switch (vin->format.field) {
504 		case V4L2_FIELD_INTERLACED_TB:
505 		case V4L2_FIELD_INTERLACED_BT:
506 		case V4L2_FIELD_INTERLACED:
507 		case V4L2_FIELD_SEQ_TB:
508 		case V4L2_FIELD_SEQ_BT:
509 			rect->height *= 2;
510 			break;
511 		}
512 	}
513 
514 	return 0;
515 }
516 
517 static int rvin_g_selection(struct file *file, void *fh,
518 			    struct v4l2_selection *s)
519 {
520 	struct rvin_dev *vin = video_drvdata(file);
521 	int ret;
522 
523 	if (!vin->scaler)
524 		return -ENOIOCTLCMD;
525 
526 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
527 		return -EINVAL;
528 
529 	switch (s->target) {
530 	case V4L2_SEL_TGT_CROP_BOUNDS:
531 	case V4L2_SEL_TGT_CROP_DEFAULT:
532 		ret = rvin_remote_rectangle(vin, &s->r);
533 		if (ret)
534 			return ret;
535 
536 		break;
537 	case V4L2_SEL_TGT_CROP:
538 		s->r = vin->crop;
539 		break;
540 	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
541 	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
542 		s->r.left = s->r.top = 0;
543 		s->r.width = vin->format.width;
544 		s->r.height = vin->format.height;
545 		break;
546 	case V4L2_SEL_TGT_COMPOSE:
547 		s->r = vin->compose;
548 		break;
549 	default:
550 		return -EINVAL;
551 	}
552 
553 	return 0;
554 }
555 
556 static int rvin_s_selection(struct file *file, void *fh,
557 			    struct v4l2_selection *s)
558 {
559 	struct rvin_dev *vin = video_drvdata(file);
560 	const struct rvin_video_format *fmt;
561 	struct v4l2_rect r = s->r;
562 	struct v4l2_rect max_rect;
563 	struct v4l2_rect min_rect = {
564 		.width = 6,
565 		.height = 2,
566 	};
567 	int ret;
568 
569 	if (!vin->scaler)
570 		return -ENOIOCTLCMD;
571 
572 	if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
573 		return -EINVAL;
574 
575 	v4l2_rect_set_min_size(&r, &min_rect);
576 
577 	switch (s->target) {
578 	case V4L2_SEL_TGT_CROP:
579 		/* Can't crop outside of source input */
580 		ret = rvin_remote_rectangle(vin, &max_rect);
581 		if (ret)
582 			return ret;
583 
584 		v4l2_rect_map_inside(&r, &max_rect);
585 
586 		v4l_bound_align_image(&r.width, 6, max_rect.width, 0,
587 				      &r.height, 2, max_rect.height, 0, 0);
588 
589 		r.top  = clamp_t(s32, r.top, 0, max_rect.height - r.height);
590 		r.left = clamp_t(s32, r.left, 0, max_rect.width - r.width);
591 
592 		vin->crop = s->r = r;
593 
594 		vin_dbg(vin, "Cropped %dx%d@%d:%d of %dx%d\n",
595 			r.width, r.height, r.left, r.top,
596 			max_rect.width, max_rect.height);
597 		break;
598 	case V4L2_SEL_TGT_COMPOSE:
599 		/* Make sure compose rect fits inside output format */
600 		max_rect.top = max_rect.left = 0;
601 		max_rect.width = vin->format.width;
602 		max_rect.height = vin->format.height;
603 		v4l2_rect_map_inside(&r, &max_rect);
604 
605 		/*
606 		 * Composing is done by adding a offset to the buffer address,
607 		 * the HW wants this address to be aligned to HW_BUFFER_MASK.
608 		 * Make sure the top and left values meets this requirement.
609 		 */
610 		while ((r.top * vin->format.bytesperline) & HW_BUFFER_MASK)
611 			r.top--;
612 
613 		fmt = rvin_format_from_pixel(vin, vin->format.pixelformat);
614 		while ((r.left * fmt->bpp) & HW_BUFFER_MASK)
615 			r.left--;
616 
617 		vin->compose = s->r = r;
618 
619 		vin_dbg(vin, "Compose %dx%d@%d:%d in %dx%d\n",
620 			r.width, r.height, r.left, r.top,
621 			vin->format.width, vin->format.height);
622 		break;
623 	default:
624 		return -EINVAL;
625 	}
626 
627 	/* HW supports modifying configuration while running */
628 	rvin_crop_scale_comp(vin);
629 
630 	return 0;
631 }
632 
633 static int rvin_g_parm(struct file *file, void *priv,
634 		       struct v4l2_streamparm *parm)
635 {
636 	struct rvin_dev *vin = video_drvdata(file);
637 	struct v4l2_subdev *sd = vin_to_source(vin);
638 
639 	return v4l2_g_parm_cap(&vin->vdev, sd, parm);
640 }
641 
642 static int rvin_s_parm(struct file *file, void *priv,
643 		       struct v4l2_streamparm *parm)
644 {
645 	struct rvin_dev *vin = video_drvdata(file);
646 	struct v4l2_subdev *sd = vin_to_source(vin);
647 
648 	return v4l2_s_parm_cap(&vin->vdev, sd, parm);
649 }
650 
651 static int rvin_g_pixelaspect(struct file *file, void *priv,
652 			      int type, struct v4l2_fract *f)
653 {
654 	struct rvin_dev *vin = video_drvdata(file);
655 	struct v4l2_subdev *sd = vin_to_source(vin);
656 
657 	if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
658 		return -EINVAL;
659 
660 	return v4l2_subdev_call(sd, video, g_pixelaspect, f);
661 }
662 
663 static int rvin_enum_input(struct file *file, void *priv,
664 			   struct v4l2_input *i)
665 {
666 	struct rvin_dev *vin = video_drvdata(file);
667 	struct v4l2_subdev *sd = vin_to_source(vin);
668 	int ret;
669 
670 	if (i->index != 0)
671 		return -EINVAL;
672 
673 	ret = v4l2_subdev_call(sd, video, g_input_status, &i->status);
674 	if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
675 		return ret;
676 
677 	i->type = V4L2_INPUT_TYPE_CAMERA;
678 
679 	if (v4l2_subdev_has_op(sd, pad, dv_timings_cap)) {
680 		i->capabilities = V4L2_IN_CAP_DV_TIMINGS;
681 		i->std = 0;
682 	} else {
683 		i->capabilities = V4L2_IN_CAP_STD;
684 		i->std = vin->vdev.tvnorms;
685 	}
686 
687 	strscpy(i->name, "Camera", sizeof(i->name));
688 
689 	return 0;
690 }
691 
692 static int rvin_g_input(struct file *file, void *priv, unsigned int *i)
693 {
694 	*i = 0;
695 	return 0;
696 }
697 
698 static int rvin_s_input(struct file *file, void *priv, unsigned int i)
699 {
700 	if (i > 0)
701 		return -EINVAL;
702 	return 0;
703 }
704 
705 static int rvin_querystd(struct file *file, void *priv, v4l2_std_id *a)
706 {
707 	struct rvin_dev *vin = video_drvdata(file);
708 	struct v4l2_subdev *sd = vin_to_source(vin);
709 
710 	return v4l2_subdev_call(sd, video, querystd, a);
711 }
712 
713 static int rvin_s_std(struct file *file, void *priv, v4l2_std_id a)
714 {
715 	struct rvin_dev *vin = video_drvdata(file);
716 	int ret;
717 
718 	ret = v4l2_subdev_call(vin_to_source(vin), video, s_std, a);
719 	if (ret < 0)
720 		return ret;
721 
722 	vin->std = a;
723 
724 	/* Changing the standard will change the width/height */
725 	return rvin_reset_format(vin);
726 }
727 
728 static int rvin_g_std(struct file *file, void *priv, v4l2_std_id *a)
729 {
730 	struct rvin_dev *vin = video_drvdata(file);
731 
732 	if (v4l2_subdev_has_op(vin_to_source(vin), pad, dv_timings_cap))
733 		return -ENOIOCTLCMD;
734 
735 	*a = vin->std;
736 
737 	return 0;
738 }
739 
740 static int rvin_subscribe_event(struct v4l2_fh *fh,
741 				const struct v4l2_event_subscription *sub)
742 {
743 	switch (sub->type) {
744 	case V4L2_EVENT_SOURCE_CHANGE:
745 		return v4l2_event_subscribe(fh, sub, 4, NULL);
746 	}
747 	return v4l2_ctrl_subscribe_event(fh, sub);
748 }
749 
750 static int rvin_enum_dv_timings(struct file *file, void *priv_fh,
751 				struct v4l2_enum_dv_timings *timings)
752 {
753 	struct rvin_dev *vin = video_drvdata(file);
754 	struct v4l2_subdev *sd = vin_to_source(vin);
755 	int ret;
756 
757 	if (timings->pad)
758 		return -EINVAL;
759 
760 	timings->pad = vin->parallel.sink_pad;
761 
762 	ret = v4l2_subdev_call(sd, pad, enum_dv_timings, timings);
763 
764 	timings->pad = 0;
765 
766 	return ret;
767 }
768 
769 static int rvin_s_dv_timings(struct file *file, void *priv_fh,
770 			     struct v4l2_dv_timings *timings)
771 {
772 	struct rvin_dev *vin = video_drvdata(file);
773 	struct v4l2_subdev *sd = vin_to_source(vin);
774 	int ret;
775 
776 	ret = v4l2_subdev_call(sd, pad, s_dv_timings,
777 			       vin->parallel.sink_pad, timings);
778 	if (ret)
779 		return ret;
780 
781 	/* Changing the timings will change the width/height */
782 	return rvin_reset_format(vin);
783 }
784 
785 static int rvin_g_dv_timings(struct file *file, void *priv_fh,
786 			     struct v4l2_dv_timings *timings)
787 {
788 	struct rvin_dev *vin = video_drvdata(file);
789 	struct v4l2_subdev *sd = vin_to_source(vin);
790 
791 	return v4l2_subdev_call(sd, pad, g_dv_timings,
792 				vin->parallel.sink_pad, timings);
793 }
794 
795 static int rvin_query_dv_timings(struct file *file, void *priv_fh,
796 				 struct v4l2_dv_timings *timings)
797 {
798 	struct rvin_dev *vin = video_drvdata(file);
799 	struct v4l2_subdev *sd = vin_to_source(vin);
800 
801 	return v4l2_subdev_call(sd, pad, query_dv_timings,
802 				vin->parallel.sink_pad, timings);
803 }
804 
805 static int rvin_dv_timings_cap(struct file *file, void *priv_fh,
806 			       struct v4l2_dv_timings_cap *cap)
807 {
808 	struct rvin_dev *vin = video_drvdata(file);
809 	struct v4l2_subdev *sd = vin_to_source(vin);
810 	int ret;
811 
812 	if (cap->pad)
813 		return -EINVAL;
814 
815 	cap->pad = vin->parallel.sink_pad;
816 
817 	ret = v4l2_subdev_call(sd, pad, dv_timings_cap, cap);
818 
819 	cap->pad = 0;
820 
821 	return ret;
822 }
823 
824 static int rvin_g_edid(struct file *file, void *fh, struct v4l2_edid *edid)
825 {
826 	struct rvin_dev *vin = video_drvdata(file);
827 	struct v4l2_subdev *sd = vin_to_source(vin);
828 	int ret;
829 
830 	if (edid->pad)
831 		return -EINVAL;
832 
833 	edid->pad = vin->parallel.sink_pad;
834 
835 	ret = v4l2_subdev_call(sd, pad, get_edid, edid);
836 
837 	edid->pad = 0;
838 
839 	return ret;
840 }
841 
842 static int rvin_s_edid(struct file *file, void *fh, struct v4l2_edid *edid)
843 {
844 	struct rvin_dev *vin = video_drvdata(file);
845 	struct v4l2_subdev *sd = vin_to_source(vin);
846 	int ret;
847 
848 	if (edid->pad)
849 		return -EINVAL;
850 
851 	edid->pad = vin->parallel.sink_pad;
852 
853 	ret = v4l2_subdev_call(sd, pad, set_edid, edid);
854 
855 	edid->pad = 0;
856 
857 	return ret;
858 }
859 
860 static const struct v4l2_ioctl_ops rvin_ioctl_ops = {
861 	.vidioc_querycap		= rvin_querycap,
862 	.vidioc_try_fmt_vid_cap		= rvin_try_fmt_vid_cap,
863 	.vidioc_g_fmt_vid_cap		= rvin_g_fmt_vid_cap,
864 	.vidioc_s_fmt_vid_cap		= rvin_s_fmt_vid_cap,
865 	.vidioc_enum_fmt_vid_cap	= rvin_enum_fmt_vid_cap,
866 
867 	.vidioc_g_selection		= rvin_g_selection,
868 	.vidioc_s_selection		= rvin_s_selection,
869 
870 	.vidioc_g_parm			= rvin_g_parm,
871 	.vidioc_s_parm			= rvin_s_parm,
872 
873 	.vidioc_g_pixelaspect		= rvin_g_pixelaspect,
874 
875 	.vidioc_enum_input		= rvin_enum_input,
876 	.vidioc_g_input			= rvin_g_input,
877 	.vidioc_s_input			= rvin_s_input,
878 
879 	.vidioc_dv_timings_cap		= rvin_dv_timings_cap,
880 	.vidioc_enum_dv_timings		= rvin_enum_dv_timings,
881 	.vidioc_g_dv_timings		= rvin_g_dv_timings,
882 	.vidioc_s_dv_timings		= rvin_s_dv_timings,
883 	.vidioc_query_dv_timings	= rvin_query_dv_timings,
884 
885 	.vidioc_g_edid			= rvin_g_edid,
886 	.vidioc_s_edid			= rvin_s_edid,
887 
888 	.vidioc_querystd		= rvin_querystd,
889 	.vidioc_g_std			= rvin_g_std,
890 	.vidioc_s_std			= rvin_s_std,
891 
892 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
893 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
894 	.vidioc_querybuf		= vb2_ioctl_querybuf,
895 	.vidioc_qbuf			= vb2_ioctl_qbuf,
896 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
897 	.vidioc_expbuf			= vb2_ioctl_expbuf,
898 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
899 	.vidioc_streamon		= vb2_ioctl_streamon,
900 	.vidioc_streamoff		= vb2_ioctl_streamoff,
901 
902 	.vidioc_log_status		= v4l2_ctrl_log_status,
903 	.vidioc_subscribe_event		= rvin_subscribe_event,
904 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
905 };
906 
907 /* -----------------------------------------------------------------------------
908  * V4L2 Media Controller
909  */
910 
911 static void rvin_mc_try_format(struct rvin_dev *vin,
912 			       struct v4l2_pix_format *pix)
913 {
914 	/*
915 	 * The V4L2 specification clearly documents the colorspace fields
916 	 * as being set by drivers for capture devices. Using the values
917 	 * supplied by userspace thus wouldn't comply with the API. Until
918 	 * the API is updated force fixed values.
919 	 */
920 	pix->colorspace = RVIN_DEFAULT_COLORSPACE;
921 	pix->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(pix->colorspace);
922 	pix->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(pix->colorspace);
923 	pix->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true, pix->colorspace,
924 							  pix->ycbcr_enc);
925 
926 	rvin_format_align(vin, pix);
927 }
928 
929 static int rvin_mc_try_fmt_vid_cap(struct file *file, void *priv,
930 				   struct v4l2_format *f)
931 {
932 	struct rvin_dev *vin = video_drvdata(file);
933 
934 	rvin_mc_try_format(vin, &f->fmt.pix);
935 
936 	return 0;
937 }
938 
939 static int rvin_mc_s_fmt_vid_cap(struct file *file, void *priv,
940 				 struct v4l2_format *f)
941 {
942 	struct rvin_dev *vin = video_drvdata(file);
943 
944 	if (vb2_is_busy(&vin->queue))
945 		return -EBUSY;
946 
947 	rvin_mc_try_format(vin, &f->fmt.pix);
948 
949 	vin->format = f->fmt.pix;
950 
951 	vin->crop.top = 0;
952 	vin->crop.left = 0;
953 	vin->crop.width = vin->format.width;
954 	vin->crop.height = vin->format.height;
955 	vin->compose = vin->crop;
956 
957 	return 0;
958 }
959 
960 static const struct v4l2_ioctl_ops rvin_mc_ioctl_ops = {
961 	.vidioc_querycap		= rvin_querycap,
962 	.vidioc_try_fmt_vid_cap		= rvin_mc_try_fmt_vid_cap,
963 	.vidioc_g_fmt_vid_cap		= rvin_g_fmt_vid_cap,
964 	.vidioc_s_fmt_vid_cap		= rvin_mc_s_fmt_vid_cap,
965 	.vidioc_enum_fmt_vid_cap	= rvin_enum_fmt_vid_cap,
966 
967 	.vidioc_g_selection		= rvin_g_selection,
968 	.vidioc_s_selection		= rvin_s_selection,
969 
970 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
971 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
972 	.vidioc_querybuf		= vb2_ioctl_querybuf,
973 	.vidioc_qbuf			= vb2_ioctl_qbuf,
974 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
975 	.vidioc_expbuf			= vb2_ioctl_expbuf,
976 	.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
977 	.vidioc_streamon		= vb2_ioctl_streamon,
978 	.vidioc_streamoff		= vb2_ioctl_streamoff,
979 
980 	.vidioc_log_status		= v4l2_ctrl_log_status,
981 	.vidioc_subscribe_event		= rvin_subscribe_event,
982 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
983 };
984 
985 /* -----------------------------------------------------------------------------
986  * File Operations
987  */
988 
989 static int rvin_power_parallel(struct rvin_dev *vin, bool on)
990 {
991 	struct v4l2_subdev *sd = vin_to_source(vin);
992 	int power = on ? 1 : 0;
993 	int ret;
994 
995 	ret = v4l2_subdev_call(sd, core, s_power, power);
996 	if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
997 		return ret;
998 
999 	return 0;
1000 }
1001 
1002 static int rvin_open(struct file *file)
1003 {
1004 	struct rvin_dev *vin = video_drvdata(file);
1005 	int ret;
1006 
1007 	ret = pm_runtime_resume_and_get(vin->dev);
1008 	if (ret < 0)
1009 		return ret;
1010 
1011 	ret = mutex_lock_interruptible(&vin->lock);
1012 	if (ret)
1013 		goto err_pm;
1014 
1015 	file->private_data = vin;
1016 
1017 	ret = v4l2_fh_open(file);
1018 	if (ret)
1019 		goto err_unlock;
1020 
1021 	if (vin->info->use_mc)
1022 		ret = v4l2_pipeline_pm_get(&vin->vdev.entity);
1023 	else if (v4l2_fh_is_singular_file(file))
1024 		ret = rvin_power_parallel(vin, true);
1025 
1026 	if (ret < 0)
1027 		goto err_open;
1028 
1029 	ret = v4l2_ctrl_handler_setup(&vin->ctrl_handler);
1030 	if (ret)
1031 		goto err_power;
1032 
1033 	mutex_unlock(&vin->lock);
1034 
1035 	return 0;
1036 err_power:
1037 	if (vin->info->use_mc)
1038 		v4l2_pipeline_pm_put(&vin->vdev.entity);
1039 	else if (v4l2_fh_is_singular_file(file))
1040 		rvin_power_parallel(vin, false);
1041 err_open:
1042 	v4l2_fh_release(file);
1043 err_unlock:
1044 	mutex_unlock(&vin->lock);
1045 err_pm:
1046 	pm_runtime_put(vin->dev);
1047 
1048 	return ret;
1049 }
1050 
1051 static int rvin_release(struct file *file)
1052 {
1053 	struct rvin_dev *vin = video_drvdata(file);
1054 	bool fh_singular;
1055 	int ret;
1056 
1057 	mutex_lock(&vin->lock);
1058 
1059 	/* Save the singular status before we call the clean-up helper */
1060 	fh_singular = v4l2_fh_is_singular_file(file);
1061 
1062 	/* the release helper will cleanup any on-going streaming */
1063 	ret = _vb2_fop_release(file, NULL);
1064 
1065 	if (vin->info->use_mc) {
1066 		v4l2_pipeline_pm_put(&vin->vdev.entity);
1067 	} else {
1068 		if (fh_singular)
1069 			rvin_power_parallel(vin, false);
1070 	}
1071 
1072 	mutex_unlock(&vin->lock);
1073 
1074 	pm_runtime_put(vin->dev);
1075 
1076 	return ret;
1077 }
1078 
1079 static const struct v4l2_file_operations rvin_fops = {
1080 	.owner		= THIS_MODULE,
1081 	.unlocked_ioctl	= video_ioctl2,
1082 	.open		= rvin_open,
1083 	.release	= rvin_release,
1084 	.poll		= vb2_fop_poll,
1085 	.mmap		= vb2_fop_mmap,
1086 	.read		= vb2_fop_read,
1087 };
1088 
1089 void rvin_v4l2_unregister(struct rvin_dev *vin)
1090 {
1091 	if (!video_is_registered(&vin->vdev))
1092 		return;
1093 
1094 	v4l2_info(&vin->v4l2_dev, "Removing %s\n",
1095 		  video_device_node_name(&vin->vdev));
1096 
1097 	/* Checks internally if vdev have been init or not */
1098 	video_unregister_device(&vin->vdev);
1099 }
1100 
1101 static void rvin_notify_video_device(struct rvin_dev *vin,
1102 				     unsigned int notification, void *arg)
1103 {
1104 	switch (notification) {
1105 	case V4L2_DEVICE_NOTIFY_EVENT:
1106 		v4l2_event_queue(&vin->vdev, arg);
1107 		break;
1108 	default:
1109 		break;
1110 	}
1111 }
1112 
1113 static void rvin_notify(struct v4l2_subdev *sd,
1114 			unsigned int notification, void *arg)
1115 {
1116 	struct v4l2_subdev *remote;
1117 	struct rvin_group *group;
1118 	struct media_pad *pad;
1119 	struct rvin_dev *vin =
1120 		container_of(sd->v4l2_dev, struct rvin_dev, v4l2_dev);
1121 	unsigned int i;
1122 
1123 	/* If no media controller, no need to route the event. */
1124 	if (!vin->info->use_mc) {
1125 		rvin_notify_video_device(vin, notification, arg);
1126 		return;
1127 	}
1128 
1129 	group = vin->group;
1130 
1131 	for (i = 0; i < RCAR_VIN_NUM; i++) {
1132 		vin = group->vin[i];
1133 		if (!vin)
1134 			continue;
1135 
1136 		pad = media_pad_remote_pad_first(&vin->pad);
1137 		if (!pad)
1138 			continue;
1139 
1140 		remote = media_entity_to_v4l2_subdev(pad->entity);
1141 		if (remote != sd)
1142 			continue;
1143 
1144 		rvin_notify_video_device(vin, notification, arg);
1145 	}
1146 }
1147 
1148 int rvin_v4l2_register(struct rvin_dev *vin)
1149 {
1150 	struct video_device *vdev = &vin->vdev;
1151 	int ret;
1152 
1153 	vin->v4l2_dev.notify = rvin_notify;
1154 
1155 	/* video node */
1156 	vdev->v4l2_dev = &vin->v4l2_dev;
1157 	vdev->queue = &vin->queue;
1158 	snprintf(vdev->name, sizeof(vdev->name), "VIN%u output", vin->id);
1159 	vdev->release = video_device_release_empty;
1160 	vdev->lock = &vin->lock;
1161 	vdev->fops = &rvin_fops;
1162 	vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
1163 		V4L2_CAP_READWRITE;
1164 
1165 	/* Set a default format */
1166 	vin->format.pixelformat	= RVIN_DEFAULT_FORMAT;
1167 	vin->format.width = RVIN_DEFAULT_WIDTH;
1168 	vin->format.height = RVIN_DEFAULT_HEIGHT;
1169 	vin->format.field = RVIN_DEFAULT_FIELD;
1170 	vin->format.colorspace = RVIN_DEFAULT_COLORSPACE;
1171 
1172 	if (vin->info->use_mc) {
1173 		vdev->device_caps |= V4L2_CAP_IO_MC;
1174 		vdev->ioctl_ops = &rvin_mc_ioctl_ops;
1175 	} else {
1176 		vdev->ioctl_ops = &rvin_ioctl_ops;
1177 		rvin_reset_format(vin);
1178 	}
1179 
1180 	rvin_format_align(vin, &vin->format);
1181 
1182 	ret = video_register_device(&vin->vdev, VFL_TYPE_VIDEO, -1);
1183 	if (ret) {
1184 		vin_err(vin, "Failed to register video device\n");
1185 		return ret;
1186 	}
1187 
1188 	video_set_drvdata(&vin->vdev, vin);
1189 
1190 	v4l2_info(&vin->v4l2_dev, "Device registered as %s\n",
1191 		  video_device_node_name(&vin->vdev));
1192 
1193 	return ret;
1194 }
1195