Lines Matching +full:top +full:- +full:left

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()
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()
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()
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()
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->src_rect.top = 0; in rvin_reset_format()
230 vin->src_rect.left = 0; in rvin_reset_format()
231 vin->src_rect.width = vin->format.width; in rvin_reset_format()
232 vin->src_rect.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->crop = vin->src_rect; in rvin_reset_format()
244 vin->compose.top = 0; in rvin_reset_format()
245 vin->compose.left = 0; in rvin_reset_format()
246 vin->compose.width = vin->format.width; in rvin_reset_format()
247 vin->compose.height = vin->format.height; in rvin_reset_format()
261 .pad = vin->parallel.source_pad, in rvin_try_format()
271 sd_state = __v4l2_subdev_state_alloc(sd, "rvin:state->lock", &key); in rvin_try_format()
275 if (!rvin_format_from_pixel(vin, pix->pixelformat)) in rvin_try_format()
276 pix->pixelformat = RVIN_DEFAULT_FORMAT; in rvin_try_format()
278 v4l2_fill_mbus_format(&format.format, pix, vin->mbus_code); in rvin_try_format()
281 field = pix->field; in rvin_try_format()
282 width = pix->width; in rvin_try_format()
283 height = pix->height; in rvin_try_format()
286 if (ret < 0 && ret != -ENOIOCTLCMD) in rvin_try_format()
293 src_rect->top = 0; in rvin_try_format()
294 src_rect->left = 0; in rvin_try_format()
295 src_rect->width = pix->width; in rvin_try_format()
296 src_rect->height = pix->height; in rvin_try_format()
300 pix->field = field; in rvin_try_format()
302 pix->width = width; in rvin_try_format()
303 pix->height = height; in rvin_try_format()
315 strscpy(cap->driver, KBUILD_MODNAME, sizeof(cap->driver)); in rvin_querycap()
316 strscpy(cap->card, "R_Car_VIN", sizeof(cap->card)); in rvin_querycap()
325 return rvin_try_format(vin, V4L2_SUBDEV_FORMAT_TRY, &f->fmt.pix, NULL); in rvin_try_fmt_vid_cap()
335 if (vb2_is_busy(&vin->queue)) in rvin_s_fmt_vid_cap()
336 return -EBUSY; in rvin_s_fmt_vid_cap()
338 ret = rvin_try_format(vin, V4L2_SUBDEV_FORMAT_ACTIVE, &f->fmt.pix, in rvin_s_fmt_vid_cap()
343 vin->format = f->fmt.pix; in rvin_s_fmt_vid_cap()
345 fmt_rect.top = 0; in rvin_s_fmt_vid_cap()
346 fmt_rect.left = 0; in rvin_s_fmt_vid_cap()
347 fmt_rect.width = vin->format.width; in rvin_s_fmt_vid_cap()
348 fmt_rect.height = vin->format.height; in rvin_s_fmt_vid_cap()
350 v4l2_rect_map_inside(&vin->crop, &src_rect); in rvin_s_fmt_vid_cap()
351 v4l2_rect_map_inside(&vin->compose, &fmt_rect); in rvin_s_fmt_vid_cap()
352 vin->src_rect = src_rect; in rvin_s_fmt_vid_cap()
362 f->fmt.pix = vin->format; in rvin_g_fmt_vid_cap()
385 switch (f->mbus_code) { in rvin_enum_fmt_vid_cap()
394 if (f->index) in rvin_enum_fmt_vid_cap()
395 return -EINVAL; in rvin_enum_fmt_vid_cap()
396 f->pixelformat = V4L2_PIX_FMT_SBGGR8; in rvin_enum_fmt_vid_cap()
399 if (f->index) in rvin_enum_fmt_vid_cap()
400 return -EINVAL; in rvin_enum_fmt_vid_cap()
401 f->pixelformat = V4L2_PIX_FMT_SGBRG8; in rvin_enum_fmt_vid_cap()
404 if (f->index) in rvin_enum_fmt_vid_cap()
405 return -EINVAL; in rvin_enum_fmt_vid_cap()
406 f->pixelformat = V4L2_PIX_FMT_SGRBG8; in rvin_enum_fmt_vid_cap()
409 if (f->index) in rvin_enum_fmt_vid_cap()
410 return -EINVAL; in rvin_enum_fmt_vid_cap()
411 f->pixelformat = V4L2_PIX_FMT_SRGGB8; in rvin_enum_fmt_vid_cap()
414 return -EINVAL; in rvin_enum_fmt_vid_cap()
417 matched = -1; in rvin_enum_fmt_vid_cap()
422 if (matched == f->index) { in rvin_enum_fmt_vid_cap()
423 f->pixelformat = rvin_formats[i].fourcc; in rvin_enum_fmt_vid_cap()
428 return -EINVAL; in rvin_enum_fmt_vid_cap()
436 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) in rvin_g_selection()
437 return -EINVAL; in rvin_g_selection()
439 switch (s->target) { in rvin_g_selection()
442 s->r.left = s->r.top = 0; in rvin_g_selection()
443 s->r.width = vin->src_rect.width; in rvin_g_selection()
444 s->r.height = vin->src_rect.height; in rvin_g_selection()
447 s->r = vin->crop; in rvin_g_selection()
451 s->r.left = s->r.top = 0; in rvin_g_selection()
452 s->r.width = vin->format.width; in rvin_g_selection()
453 s->r.height = vin->format.height; in rvin_g_selection()
456 s->r = vin->compose; in rvin_g_selection()
459 return -EINVAL; in rvin_g_selection()
470 struct v4l2_rect r = s->r; in rvin_s_selection()
477 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) in rvin_s_selection()
478 return -EINVAL; in rvin_s_selection()
482 switch (s->target) { in rvin_s_selection()
485 max_rect.top = max_rect.left = 0; in rvin_s_selection()
486 max_rect.width = vin->src_rect.width; in rvin_s_selection()
487 max_rect.height = vin->src_rect.height; in rvin_s_selection()
490 v4l_bound_align_image(&r.width, 6, vin->src_rect.width, 0, in rvin_s_selection()
491 &r.height, 2, vin->src_rect.height, 0, 0); in rvin_s_selection()
493 r.top = clamp_t(s32, r.top, 0, in rvin_s_selection()
494 vin->src_rect.height - r.height); in rvin_s_selection()
495 r.left = clamp_t(s32, r.left, 0, vin->src_rect.width - r.width); in rvin_s_selection()
497 vin->crop = s->r = r; in rvin_s_selection()
500 r.width, r.height, r.left, r.top, in rvin_s_selection()
501 vin->src_rect.width, vin->src_rect.height); in rvin_s_selection()
505 max_rect.top = max_rect.left = 0; in rvin_s_selection()
506 max_rect.width = vin->format.width; in rvin_s_selection()
507 max_rect.height = vin->format.height; in rvin_s_selection()
513 * Make sure the top and left values meets this requirement. in rvin_s_selection()
515 while ((r.top * vin->format.bytesperline) & HW_BUFFER_MASK) in rvin_s_selection()
516 r.top--; in rvin_s_selection()
518 fmt = rvin_format_from_pixel(vin, vin->format.pixelformat); in rvin_s_selection()
519 while ((r.left * fmt->bpp) & HW_BUFFER_MASK) in rvin_s_selection()
520 r.left--; in rvin_s_selection()
522 vin->compose = s->r = r; in rvin_s_selection()
525 r.width, r.height, r.left, r.top, in rvin_s_selection()
526 vin->format.width, vin->format.height); in rvin_s_selection()
529 return -EINVAL; in rvin_s_selection()
544 return v4l2_g_parm_cap(&vin->vdev, sd, parm); in rvin_g_parm()
553 return v4l2_s_parm_cap(&vin->vdev, sd, parm); in rvin_s_parm()
563 return -EINVAL; in rvin_g_pixelaspect()
575 if (i->index != 0) in rvin_enum_input()
576 return -EINVAL; in rvin_enum_input()
578 ret = v4l2_subdev_call(sd, video, g_input_status, &i->status); in rvin_enum_input()
579 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) in rvin_enum_input()
582 i->type = V4L2_INPUT_TYPE_CAMERA; in rvin_enum_input()
585 i->capabilities = V4L2_IN_CAP_DV_TIMINGS; in rvin_enum_input()
586 i->std = 0; in rvin_enum_input()
588 i->capabilities = V4L2_IN_CAP_STD; in rvin_enum_input()
589 i->std = vin->vdev.tvnorms; in rvin_enum_input()
592 strscpy(i->name, "Camera", sizeof(i->name)); in rvin_enum_input()
606 return -EINVAL; in rvin_s_input()
627 vin->std = a; in rvin_s_std()
638 return -ENOIOCTLCMD; in rvin_g_std()
640 *a = vin->std; in rvin_g_std()
648 switch (sub->type) { in rvin_subscribe_event()
662 if (timings->pad) in rvin_enum_dv_timings()
663 return -EINVAL; in rvin_enum_dv_timings()
665 timings->pad = vin->parallel.sink_pad; in rvin_enum_dv_timings()
669 timings->pad = 0; in rvin_enum_dv_timings()
714 if (cap->pad) in rvin_dv_timings_cap()
715 return -EINVAL; in rvin_dv_timings_cap()
717 cap->pad = vin->parallel.sink_pad; in rvin_dv_timings_cap()
721 cap->pad = 0; in rvin_dv_timings_cap()
732 if (edid->pad) in rvin_g_edid()
733 return -EINVAL; in rvin_g_edid()
735 edid->pad = vin->parallel.sink_pad; in rvin_g_edid()
739 edid->pad = 0; in rvin_g_edid()
750 if (edid->pad) in rvin_s_edid()
751 return -EINVAL; in rvin_s_edid()
753 edid->pad = vin->parallel.sink_pad; in rvin_s_edid()
757 edid->pad = 0; in rvin_s_edid()
809 /* -----------------------------------------------------------------------------
822 pix->colorspace = RVIN_DEFAULT_COLORSPACE; in rvin_mc_try_format()
823 pix->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(pix->colorspace); in rvin_mc_try_format()
824 pix->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(pix->colorspace); in rvin_mc_try_format()
825 pix->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true, pix->colorspace, in rvin_mc_try_format()
826 pix->ycbcr_enc); in rvin_mc_try_format()
836 rvin_mc_try_format(vin, &f->fmt.pix); in rvin_mc_try_fmt_vid_cap()
846 if (vb2_is_busy(&vin->queue)) in rvin_mc_s_fmt_vid_cap()
847 return -EBUSY; in rvin_mc_s_fmt_vid_cap()
849 rvin_mc_try_format(vin, &f->fmt.pix); in rvin_mc_s_fmt_vid_cap()
851 vin->format = f->fmt.pix; in rvin_mc_s_fmt_vid_cap()
853 vin->crop.top = 0; in rvin_mc_s_fmt_vid_cap()
854 vin->crop.left = 0; in rvin_mc_s_fmt_vid_cap()
855 vin->crop.width = vin->format.width; in rvin_mc_s_fmt_vid_cap()
856 vin->crop.height = vin->format.height; in rvin_mc_s_fmt_vid_cap()
857 vin->compose = vin->crop; in rvin_mc_s_fmt_vid_cap()
884 /* -----------------------------------------------------------------------------
895 if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV) in rvin_power_parallel()
906 ret = pm_runtime_resume_and_get(vin->dev); in rvin_open()
910 ret = mutex_lock_interruptible(&vin->lock); in rvin_open()
914 file->private_data = vin; in rvin_open()
920 if (vin->info->use_mc) in rvin_open()
921 ret = v4l2_pipeline_pm_get(&vin->vdev.entity); in rvin_open()
928 ret = v4l2_ctrl_handler_setup(&vin->ctrl_handler); in rvin_open()
932 mutex_unlock(&vin->lock); in rvin_open()
936 if (vin->info->use_mc) in rvin_open()
937 v4l2_pipeline_pm_put(&vin->vdev.entity); in rvin_open()
943 mutex_unlock(&vin->lock); in rvin_open()
945 pm_runtime_put(vin->dev); in rvin_open()
956 mutex_lock(&vin->lock); in rvin_release()
958 /* Save the singular status before we call the clean-up helper */ in rvin_release()
961 /* the release helper will cleanup any on-going streaming */ in rvin_release()
964 if (vin->info->use_mc) { in rvin_release()
965 v4l2_pipeline_pm_put(&vin->vdev.entity); in rvin_release()
971 mutex_unlock(&vin->lock); in rvin_release()
973 pm_runtime_put(vin->dev); in rvin_release()
990 if (!video_is_registered(&vin->vdev)) in rvin_v4l2_unregister()
993 v4l2_info(&vin->v4l2_dev, "Removing %s\n", in rvin_v4l2_unregister()
994 video_device_node_name(&vin->vdev)); in rvin_v4l2_unregister()
997 video_unregister_device(&vin->vdev); in rvin_v4l2_unregister()
1005 v4l2_event_queue(&vin->vdev, arg); in rvin_notify_video_device()
1019 container_of(sd->v4l2_dev, struct rvin_dev, v4l2_dev); in rvin_notify()
1023 if (!vin->info->use_mc) { in rvin_notify()
1028 group = vin->group; in rvin_notify()
1031 vin = group->vin[i]; in rvin_notify()
1035 pad = media_pad_remote_pad_first(&vin->pad); in rvin_notify()
1039 remote = media_entity_to_v4l2_subdev(pad->entity); in rvin_notify()
1049 struct video_device *vdev = &vin->vdev; in rvin_v4l2_register()
1052 vin->v4l2_dev.notify = rvin_notify; in rvin_v4l2_register()
1055 vdev->v4l2_dev = &vin->v4l2_dev; in rvin_v4l2_register()
1056 vdev->queue = &vin->queue; in rvin_v4l2_register()
1057 snprintf(vdev->name, sizeof(vdev->name), "VIN%u output", vin->id); in rvin_v4l2_register()
1058 vdev->release = video_device_release_empty; in rvin_v4l2_register()
1059 vdev->lock = &vin->lock; in rvin_v4l2_register()
1060 vdev->fops = &rvin_fops; in rvin_v4l2_register()
1061 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING | in rvin_v4l2_register()
1065 vin->format.pixelformat = RVIN_DEFAULT_FORMAT; in rvin_v4l2_register()
1066 vin->format.width = RVIN_DEFAULT_WIDTH; in rvin_v4l2_register()
1067 vin->format.height = RVIN_DEFAULT_HEIGHT; in rvin_v4l2_register()
1068 vin->format.field = RVIN_DEFAULT_FIELD; in rvin_v4l2_register()
1069 vin->format.colorspace = RVIN_DEFAULT_COLORSPACE; in rvin_v4l2_register()
1071 if (vin->info->use_mc) { in rvin_v4l2_register()
1072 vdev->device_caps |= V4L2_CAP_IO_MC; in rvin_v4l2_register()
1073 vdev->ioctl_ops = &rvin_mc_ioctl_ops; in rvin_v4l2_register()
1075 vdev->ioctl_ops = &rvin_ioctl_ops; in rvin_v4l2_register()
1079 rvin_format_align(vin, &vin->format); in rvin_v4l2_register()
1081 ret = video_register_device(&vin->vdev, VFL_TYPE_VIDEO, -1); in rvin_v4l2_register()
1087 video_set_drvdata(&vin->vdev, vin); in rvin_v4l2_register()
1089 v4l2_info(&vin->v4l2_dev, "Device registered as %s\n", in rvin_v4l2_register()
1090 video_device_node_name(&vin->vdev)); in rvin_v4l2_register()