Lines Matching +full:pix +full:- +full:limits

1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for Renesas R-Car VIN
6 * Copyright (C) 2011-2013 Renesas Solutions Corp.
10 * Based on the soc-camera rcar_vin driver
15 #include <media/v4l2-event.h>
16 #include <media/v4l2-ioctl.h>
17 #include <media/v4l2-mc.h>
18 #include <media/v4l2-rect.h>
20 #include "rcar-vin.h"
28 /* -----------------------------------------------------------------------------
98 if (vin->info->model == RCAR_M1) in rvin_format_from_pixel()
106 if (!vin->info->nv12 || !(BIT(vin->id) & 0x3333)) in rvin_format_from_pixel()
121 struct v4l2_pix_format *pix) in rvin_format_bytesperline() argument
126 fmt = rvin_format_from_pixel(vin, pix->pixelformat); in rvin_format_bytesperline()
129 return -EINVAL; in rvin_format_bytesperline()
131 switch (pix->pixelformat) { in rvin_format_bytesperline()
141 if (V4L2_FIELD_IS_SEQUENTIAL(pix->field)) in rvin_format_bytesperline()
144 return ALIGN(pix->width, align) * fmt->bpp; in rvin_format_bytesperline()
147 static u32 rvin_format_sizeimage(struct v4l2_pix_format *pix) in rvin_format_sizeimage() argument
149 switch (pix->pixelformat) { in rvin_format_sizeimage()
151 return pix->bytesperline * pix->height * 3 / 2; in rvin_format_sizeimage()
153 return pix->bytesperline * pix->height * 2; in rvin_format_sizeimage()
155 return pix->bytesperline * pix->height; in rvin_format_sizeimage()
159 static void rvin_format_align(struct rvin_dev *vin, struct v4l2_pix_format *pix) in rvin_format_align() argument
163 if (!rvin_format_from_pixel(vin, pix->pixelformat)) in rvin_format_align()
164 pix->pixelformat = RVIN_DEFAULT_FORMAT; in rvin_format_align()
166 switch (pix->field) { in rvin_format_align()
178 pix->field = RVIN_DEFAULT_FIELD; in rvin_format_align()
182 /* Hardware limits width alignment based on format. */ in rvin_format_align()
183 switch (pix->pixelformat) { in rvin_format_align()
201 v4l_bound_align_image(&pix->width, 5, vin->info->max_width, walign, in rvin_format_align()
202 &pix->height, 2, vin->info->max_height, 0, 0); in rvin_format_align()
204 pix->bytesperline = rvin_format_bytesperline(vin, pix); in rvin_format_align()
205 pix->sizeimage = rvin_format_sizeimage(pix); in rvin_format_align()
208 pix->width, pix->height, pix->bytesperline, pix->sizeimage); in rvin_format_align()
211 /* -----------------------------------------------------------------------------
219 .pad = vin->parallel.source_pad, in rvin_reset_format()
227 v4l2_fill_pix_format(&vin->format, &fmt.format); in rvin_reset_format()
229 vin->crop.top = 0; in rvin_reset_format()
230 vin->crop.left = 0; in rvin_reset_format()
231 vin->crop.width = vin->format.width; in rvin_reset_format()
232 vin->crop.height = vin->format.height; in rvin_reset_format()
235 if (vin->format.field == V4L2_FIELD_ALTERNATE) { in rvin_reset_format()
236 vin->format.field = V4L2_FIELD_INTERLACED; in rvin_reset_format()
237 vin->format.height *= 2; in rvin_reset_format()
240 rvin_format_align(vin, &vin->format); in rvin_reset_format()
242 vin->compose.top = 0; in rvin_reset_format()
243 vin->compose.left = 0; in rvin_reset_format()
244 vin->compose.width = vin->format.width; in rvin_reset_format()
245 vin->compose.height = vin->format.height; in rvin_reset_format()
251 struct v4l2_pix_format *pix, in rvin_try_format() argument
259 .pad = vin->parallel.source_pad, in rvin_try_format()
269 sd_state = __v4l2_subdev_state_alloc(sd, "rvin:state->lock", &key); in rvin_try_format()
273 if (!rvin_format_from_pixel(vin, pix->pixelformat)) in rvin_try_format()
274 pix->pixelformat = RVIN_DEFAULT_FORMAT; in rvin_try_format()
276 v4l2_fill_mbus_format(&format.format, pix, vin->mbus_code); in rvin_try_format()
279 field = pix->field; in rvin_try_format()
280 width = pix->width; in rvin_try_format()
281 height = pix->height; in rvin_try_format()
284 if (ret < 0 && ret != -ENOIOCTLCMD) in rvin_try_format()
288 v4l2_fill_pix_format(pix, &format.format); in rvin_try_format()
291 src_rect->top = 0; in rvin_try_format()
292 src_rect->left = 0; in rvin_try_format()
293 src_rect->width = pix->width; in rvin_try_format()
294 src_rect->height = pix->height; in rvin_try_format()
298 pix->field = field; in rvin_try_format()
300 pix->width = width; in rvin_try_format()
301 pix->height = height; in rvin_try_format()
303 rvin_format_align(vin, pix); in rvin_try_format()
313 strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver)); in rvin_querycap()
314 strscpy(cap->card, "R_Car_VIN", sizeof(cap->card)); in rvin_querycap()
323 return rvin_try_format(vin, V4L2_SUBDEV_FORMAT_TRY, &f->fmt.pix, NULL); in rvin_try_fmt_vid_cap()
333 if (vb2_is_busy(&vin->queue)) in rvin_s_fmt_vid_cap()
334 return -EBUSY; in rvin_s_fmt_vid_cap()
336 ret = rvin_try_format(vin, V4L2_SUBDEV_FORMAT_ACTIVE, &f->fmt.pix, in rvin_s_fmt_vid_cap()
341 vin->format = f->fmt.pix; in rvin_s_fmt_vid_cap()
345 fmt_rect.width = vin->format.width; in rvin_s_fmt_vid_cap()
346 fmt_rect.height = vin->format.height; in rvin_s_fmt_vid_cap()
348 v4l2_rect_map_inside(&vin->crop, &src_rect); in rvin_s_fmt_vid_cap()
349 v4l2_rect_map_inside(&vin->compose, &fmt_rect); in rvin_s_fmt_vid_cap()
359 f->fmt.pix = vin->format; in rvin_g_fmt_vid_cap()
382 switch (f->mbus_code) { in rvin_enum_fmt_vid_cap()
391 if (f->index) in rvin_enum_fmt_vid_cap()
392 return -EINVAL; in rvin_enum_fmt_vid_cap()
393 f->pixelformat = V4L2_PIX_FMT_SBGGR8; in rvin_enum_fmt_vid_cap()
396 if (f->index) in rvin_enum_fmt_vid_cap()
397 return -EINVAL; in rvin_enum_fmt_vid_cap()
398 f->pixelformat = V4L2_PIX_FMT_SGBRG8; in rvin_enum_fmt_vid_cap()
401 if (f->index) in rvin_enum_fmt_vid_cap()
402 return -EINVAL; in rvin_enum_fmt_vid_cap()
403 f->pixelformat = V4L2_PIX_FMT_SGRBG8; in rvin_enum_fmt_vid_cap()
406 if (f->index) in rvin_enum_fmt_vid_cap()
407 return -EINVAL; in rvin_enum_fmt_vid_cap()
408 f->pixelformat = V4L2_PIX_FMT_SRGGB8; in rvin_enum_fmt_vid_cap()
411 return -EINVAL; in rvin_enum_fmt_vid_cap()
414 matched = -1; in rvin_enum_fmt_vid_cap()
419 if (matched == f->index) { in rvin_enum_fmt_vid_cap()
420 f->pixelformat = rvin_formats[i].fourcc; in rvin_enum_fmt_vid_cap()
425 return -EINVAL; in rvin_enum_fmt_vid_cap()
437 if (vin->info->use_mc) { in rvin_remote_rectangle()
438 struct media_pad *pad = media_pad_remote_pad_first(&vin->pad); in rvin_remote_rectangle()
441 return -EINVAL; in rvin_remote_rectangle()
443 sd = media_entity_to_v4l2_subdev(pad->entity); in rvin_remote_rectangle()
444 index = pad->index; in rvin_remote_rectangle()
447 index = vin->parallel.source_pad; in rvin_remote_rectangle()
455 rect->left = rect->top = 0; in rvin_remote_rectangle()
456 rect->width = fmt.format.width; in rvin_remote_rectangle()
457 rect->height = fmt.format.height; in rvin_remote_rectangle()
460 switch (vin->format.field) { in rvin_remote_rectangle()
466 rect->height *= 2; in rvin_remote_rectangle()
480 if (!vin->scaler) in rvin_g_selection()
481 return -ENOIOCTLCMD; in rvin_g_selection()
483 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) in rvin_g_selection()
484 return -EINVAL; in rvin_g_selection()
486 switch (s->target) { in rvin_g_selection()
489 ret = rvin_remote_rectangle(vin, &s->r); in rvin_g_selection()
495 s->r = vin->crop; in rvin_g_selection()
499 s->r.left = s->r.top = 0; in rvin_g_selection()
500 s->r.width = vin->format.width; in rvin_g_selection()
501 s->r.height = vin->format.height; in rvin_g_selection()
504 s->r = vin->compose; in rvin_g_selection()
507 return -EINVAL; in rvin_g_selection()
518 struct v4l2_rect r = s->r; in rvin_s_selection()
526 if (!vin->scaler) in rvin_s_selection()
527 return -ENOIOCTLCMD; in rvin_s_selection()
529 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) in rvin_s_selection()
530 return -EINVAL; in rvin_s_selection()
534 switch (s->target) { in rvin_s_selection()
546 r.top = clamp_t(s32, r.top, 0, max_rect.height - r.height); in rvin_s_selection()
547 r.left = clamp_t(s32, r.left, 0, max_rect.width - r.width); in rvin_s_selection()
549 vin->crop = s->r = r; in rvin_s_selection()
558 max_rect.width = vin->format.width; in rvin_s_selection()
559 max_rect.height = vin->format.height; in rvin_s_selection()
567 while ((r.top * vin->format.bytesperline) & HW_BUFFER_MASK) in rvin_s_selection()
568 r.top--; in rvin_s_selection()
570 fmt = rvin_format_from_pixel(vin, vin->format.pixelformat); in rvin_s_selection()
571 while ((r.left * fmt->bpp) & HW_BUFFER_MASK) in rvin_s_selection()
572 r.left--; in rvin_s_selection()
574 vin->compose = s->r = r; in rvin_s_selection()
578 vin->format.width, vin->format.height); in rvin_s_selection()
581 return -EINVAL; in rvin_s_selection()
596 return v4l2_g_parm_cap(&vin->vdev, sd, parm); in rvin_g_parm()
605 return v4l2_s_parm_cap(&vin->vdev, sd, parm); in rvin_s_parm()
615 return -EINVAL; in rvin_g_pixelaspect()
627 if (i->index != 0) in rvin_enum_input()
628 return -EINVAL; in rvin_enum_input()
630 ret = v4l2_subdev_call(sd, video, g_input_status, &i->status); in rvin_enum_input()
631 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) in rvin_enum_input()
634 i->type = V4L2_INPUT_TYPE_CAMERA; in rvin_enum_input()
637 i->capabilities = V4L2_IN_CAP_DV_TIMINGS; in rvin_enum_input()
638 i->std = 0; in rvin_enum_input()
640 i->capabilities = V4L2_IN_CAP_STD; in rvin_enum_input()
641 i->std = vin->vdev.tvnorms; in rvin_enum_input()
644 strscpy(i->name, "Camera", sizeof(i->name)); in rvin_enum_input()
658 return -EINVAL; in rvin_s_input()
679 vin->std = a; in rvin_s_std()
690 return -ENOIOCTLCMD; in rvin_g_std()
692 *a = vin->std; in rvin_g_std()
700 switch (sub->type) { in rvin_subscribe_event()
714 if (timings->pad) in rvin_enum_dv_timings()
715 return -EINVAL; in rvin_enum_dv_timings()
717 timings->pad = vin->parallel.sink_pad; in rvin_enum_dv_timings()
721 timings->pad = 0; in rvin_enum_dv_timings()
734 vin->parallel.sink_pad, timings); in rvin_s_dv_timings()
749 vin->parallel.sink_pad, timings); in rvin_g_dv_timings()
759 vin->parallel.sink_pad, timings); in rvin_query_dv_timings()
769 if (cap->pad) in rvin_dv_timings_cap()
770 return -EINVAL; in rvin_dv_timings_cap()
772 cap->pad = vin->parallel.sink_pad; in rvin_dv_timings_cap()
776 cap->pad = 0; in rvin_dv_timings_cap()
787 if (edid->pad) in rvin_g_edid()
788 return -EINVAL; in rvin_g_edid()
790 edid->pad = vin->parallel.sink_pad; in rvin_g_edid()
794 edid->pad = 0; in rvin_g_edid()
805 if (edid->pad) in rvin_s_edid()
806 return -EINVAL; in rvin_s_edid()
808 edid->pad = vin->parallel.sink_pad; in rvin_s_edid()
812 edid->pad = 0; in rvin_s_edid()
864 /* -----------------------------------------------------------------------------
869 struct v4l2_pix_format *pix) in rvin_mc_try_format() argument
877 pix->colorspace = RVIN_DEFAULT_COLORSPACE; in rvin_mc_try_format()
878 pix->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(pix->colorspace); in rvin_mc_try_format()
879 pix->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(pix->colorspace); in rvin_mc_try_format()
880 pix->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true, pix->colorspace, in rvin_mc_try_format()
881 pix->ycbcr_enc); in rvin_mc_try_format()
883 rvin_format_align(vin, pix); in rvin_mc_try_format()
891 rvin_mc_try_format(vin, &f->fmt.pix); in rvin_mc_try_fmt_vid_cap()
901 if (vb2_is_busy(&vin->queue)) in rvin_mc_s_fmt_vid_cap()
902 return -EBUSY; in rvin_mc_s_fmt_vid_cap()
904 rvin_mc_try_format(vin, &f->fmt.pix); in rvin_mc_s_fmt_vid_cap()
906 vin->format = f->fmt.pix; in rvin_mc_s_fmt_vid_cap()
908 vin->crop.top = 0; in rvin_mc_s_fmt_vid_cap()
909 vin->crop.left = 0; in rvin_mc_s_fmt_vid_cap()
910 vin->crop.width = vin->format.width; in rvin_mc_s_fmt_vid_cap()
911 vin->crop.height = vin->format.height; in rvin_mc_s_fmt_vid_cap()
912 vin->compose = vin->crop; in rvin_mc_s_fmt_vid_cap()
942 /* -----------------------------------------------------------------------------
953 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) in rvin_power_parallel()
964 ret = pm_runtime_resume_and_get(vin->dev); in rvin_open()
968 ret = mutex_lock_interruptible(&vin->lock); in rvin_open()
972 file->private_data = vin; in rvin_open()
978 if (vin->info->use_mc) in rvin_open()
979 ret = v4l2_pipeline_pm_get(&vin->vdev.entity); in rvin_open()
986 ret = v4l2_ctrl_handler_setup(&vin->ctrl_handler); in rvin_open()
990 mutex_unlock(&vin->lock); in rvin_open()
994 if (vin->info->use_mc) in rvin_open()
995 v4l2_pipeline_pm_put(&vin->vdev.entity); in rvin_open()
1001 mutex_unlock(&vin->lock); in rvin_open()
1003 pm_runtime_put(vin->dev); in rvin_open()
1014 mutex_lock(&vin->lock); in rvin_release()
1016 /* Save the singular status before we call the clean-up helper */ in rvin_release()
1019 /* the release helper will cleanup any on-going streaming */ in rvin_release()
1022 if (vin->info->use_mc) { in rvin_release()
1023 v4l2_pipeline_pm_put(&vin->vdev.entity); in rvin_release()
1029 mutex_unlock(&vin->lock); in rvin_release()
1031 pm_runtime_put(vin->dev); in rvin_release()
1048 if (!video_is_registered(&vin->vdev)) in rvin_v4l2_unregister()
1051 v4l2_info(&vin->v4l2_dev, "Removing %s\n", in rvin_v4l2_unregister()
1052 video_device_node_name(&vin->vdev)); in rvin_v4l2_unregister()
1055 video_unregister_device(&vin->vdev); in rvin_v4l2_unregister()
1063 v4l2_event_queue(&vin->vdev, arg); in rvin_notify_video_device()
1077 container_of(sd->v4l2_dev, struct rvin_dev, v4l2_dev); in rvin_notify()
1081 if (!vin->info->use_mc) { in rvin_notify()
1086 group = vin->group; in rvin_notify()
1089 vin = group->vin[i]; in rvin_notify()
1093 pad = media_pad_remote_pad_first(&vin->pad); in rvin_notify()
1097 remote = media_entity_to_v4l2_subdev(pad->entity); in rvin_notify()
1107 struct video_device *vdev = &vin->vdev; in rvin_v4l2_register()
1110 vin->v4l2_dev.notify = rvin_notify; in rvin_v4l2_register()
1113 vdev->v4l2_dev = &vin->v4l2_dev; in rvin_v4l2_register()
1114 vdev->queue = &vin->queue; in rvin_v4l2_register()
1115 snprintf(vdev->name, sizeof(vdev->name), "VIN%u output", vin->id); in rvin_v4l2_register()
1116 vdev->release = video_device_release_empty; in rvin_v4l2_register()
1117 vdev->lock = &vin->lock; in rvin_v4l2_register()
1118 vdev->fops = &rvin_fops; in rvin_v4l2_register()
1119 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING | in rvin_v4l2_register()
1123 vin->format.pixelformat = RVIN_DEFAULT_FORMAT; in rvin_v4l2_register()
1124 vin->format.width = RVIN_DEFAULT_WIDTH; in rvin_v4l2_register()
1125 vin->format.height = RVIN_DEFAULT_HEIGHT; in rvin_v4l2_register()
1126 vin->format.field = RVIN_DEFAULT_FIELD; in rvin_v4l2_register()
1127 vin->format.colorspace = RVIN_DEFAULT_COLORSPACE; in rvin_v4l2_register()
1129 if (vin->info->use_mc) { in rvin_v4l2_register()
1130 vdev->device_caps |= V4L2_CAP_IO_MC; in rvin_v4l2_register()
1131 vdev->ioctl_ops = &rvin_mc_ioctl_ops; in rvin_v4l2_register()
1133 vdev->ioctl_ops = &rvin_ioctl_ops; in rvin_v4l2_register()
1137 rvin_format_align(vin, &vin->format); in rvin_v4l2_register()
1139 ret = video_register_device(&vin->vdev, VFL_TYPE_VIDEO, -1); in rvin_v4l2_register()
1145 video_set_drvdata(&vin->vdev, vin); in rvin_v4l2_register()
1147 v4l2_info(&vin->v4l2_dev, "Device registered as %s\n", in rvin_v4l2_register()
1148 video_device_node_name(&vin->vdev)); in rvin_v4l2_register()