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