Lines Matching +full:2 +full:- +full:pixel +full:- +full:align

1 // SPDX-License-Identifier: GPL-2.0
3 * camss-video.c
5 * Qualcomm MSM Camera Subsystem - V4L2 device node
7 * Copyright (c) 2013-2015, The Linux Foundation. All rights reserved.
8 * Copyright (C) 2015-2018 Linaro Ltd.
11 #include <media/media-entity.h>
12 #include <media/v4l2-dev.h>
13 #include <media/v4l2-device.h>
14 #include <media/v4l2-ioctl.h>
15 #include <media/v4l2-mc.h>
16 #include <media/videobuf2-dma-sg.h>
18 #include "camss-video.h"
33 * struct camss_format_info - ISP media bus format information
35 * @pixelformat: V4L2 pixel format FCC identifier
39 * @bpp: Bits per pixel when stored in memory (for each plane)
189 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
191 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
193 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
195 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
197 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
199 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
201 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
203 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
205 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
207 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
209 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
211 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
213 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
215 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
217 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
219 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
224 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
226 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
228 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
230 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
232 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
234 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
236 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
238 { { 1, 1 } }, { { 2, 3 } }, { 8 } },
240 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
242 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
244 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
246 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
248 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
250 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
252 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
254 { { 1, 1 } }, { { 1, 2 } }, { 8 } },
265 /* -----------------------------------------------------------------------------
287 return -EINVAL; in video_find_format()
291 * video_mbus_to_pix_mp - Convert v4l2_mbus_framefmt to v4l2_pix_format_mplane
311 pix->pixelformat = f->pixelformat; in video_mbus_to_pix_mp()
312 pix->num_planes = f->planes; in video_mbus_to_pix_mp()
313 for (i = 0; i < pix->num_planes; i++) { in video_mbus_to_pix_mp()
314 bytesperline = pix->width / f->hsub[i].numerator * in video_mbus_to_pix_mp()
315 f->hsub[i].denominator * f->bpp[i] / 8; in video_mbus_to_pix_mp()
316 bytesperline = ALIGN(bytesperline, alignment); in video_mbus_to_pix_mp()
317 pix->plane_fmt[i].bytesperline = bytesperline; in video_mbus_to_pix_mp()
318 pix->plane_fmt[i].sizeimage = pix->height / in video_mbus_to_pix_mp()
319 f->vsub[i].numerator * f->vsub[i].denominator * in video_mbus_to_pix_mp()
331 remote = media_pad_remote_pad_first(&video->pad); in video_remote_subdev()
333 if (!remote || !is_media_entity_v4l2_subdev(remote->entity)) in video_remote_subdev()
337 *pad = remote->index; in video_remote_subdev()
339 return media_entity_to_v4l2_subdev(remote->entity); in video_remote_subdev()
354 return -EPIPE; in video_get_subdev_format()
363 format->fmt.pix_mp.pixelformat, in video_get_subdev_format()
364 video->formats, video->nformats); in video_get_subdev_format()
368 format->type = video->type; in video_get_subdev_format()
370 return video_mbus_to_pix_mp(&fmt.format, &format->fmt.pix_mp, in video_get_subdev_format()
371 &video->formats[ret], video->bpl_alignment); in video_get_subdev_format()
374 /* -----------------------------------------------------------------------------
384 &video->active_fmt.fmt.pix_mp; in video_queue_setup()
388 if (*num_planes != format->num_planes) in video_queue_setup()
389 return -EINVAL; in video_queue_setup()
392 if (sizes[i] < format->plane_fmt[i].sizeimage) in video_queue_setup()
393 return -EINVAL; in video_queue_setup()
398 *num_planes = format->num_planes; in video_queue_setup()
401 sizes[i] = format->plane_fmt[i].sizeimage; in video_queue_setup()
409 struct camss_video *video = vb2_get_drv_priv(vb->vb2_queue); in video_buf_init()
413 &video->active_fmt.fmt.pix_mp; in video_buf_init()
417 for (i = 0; i < format->num_planes; i++) { in video_buf_init()
420 return -EFAULT; in video_buf_init()
422 buffer->addr[i] = sg_dma_address(sgt->sgl); in video_buf_init()
425 if (format->pixelformat == V4L2_PIX_FMT_NV12 || in video_buf_init()
426 format->pixelformat == V4L2_PIX_FMT_NV21 || in video_buf_init()
427 format->pixelformat == V4L2_PIX_FMT_NV16 || in video_buf_init()
428 format->pixelformat == V4L2_PIX_FMT_NV61) in video_buf_init()
429 buffer->addr[1] = buffer->addr[0] + in video_buf_init()
430 format->plane_fmt[0].bytesperline * in video_buf_init()
431 format->height; in video_buf_init()
439 struct camss_video *video = vb2_get_drv_priv(vb->vb2_queue); in video_buf_prepare()
441 &video->active_fmt.fmt.pix_mp; in video_buf_prepare()
444 for (i = 0; i < format->num_planes; i++) { in video_buf_prepare()
445 if (format->plane_fmt[i].sizeimage > vb2_plane_size(vb, i)) in video_buf_prepare()
446 return -EINVAL; in video_buf_prepare()
448 vb2_set_plane_payload(vb, i, format->plane_fmt[i].sizeimage); in video_buf_prepare()
451 vbuf->field = V4L2_FIELD_NONE; in video_buf_prepare()
459 struct camss_video *video = vb2_get_drv_priv(vb->vb2_queue); in video_buf_queue()
463 video->ops->queue_buffer(video, buffer); in video_buf_queue()
468 struct v4l2_pix_format_mplane *pix = &video->active_fmt.fmt.pix_mp; in video_check_format()
473 sd_pix->pixelformat = pix->pixelformat; in video_check_format()
478 if (pix->pixelformat != sd_pix->pixelformat || in video_check_format()
479 pix->height != sd_pix->height || in video_check_format()
480 pix->width != sd_pix->width || in video_check_format()
481 pix->num_planes != sd_pix->num_planes || in video_check_format()
482 pix->field != format.fmt.pix_mp.field) in video_check_format()
483 return -EPIPE; in video_check_format()
491 struct video_device *vdev = &video->vdev; in video_start_streaming()
499 dev_err(video->camss->dev, "Failed to start media pipeline: %d\n", ret); in video_start_streaming()
507 entity = &vdev->entity; in video_start_streaming()
509 pad = &entity->pads[0]; in video_start_streaming()
510 if (!(pad->flags & MEDIA_PAD_FL_SINK)) in video_start_streaming()
514 if (!pad || !is_media_entity_v4l2_subdev(pad->entity)) in video_start_streaming()
517 entity = pad->entity; in video_start_streaming()
521 if (ret < 0 && ret != -ENOIOCTLCMD) in video_start_streaming()
531 video->ops->flush_buffers(video, VB2_BUF_STATE_QUEUED); in video_start_streaming()
539 struct video_device *vdev = &video->vdev; in video_stop_streaming()
545 entity = &vdev->entity; in video_stop_streaming()
547 pad = &entity->pads[0]; in video_stop_streaming()
548 if (!(pad->flags & MEDIA_PAD_FL_SINK)) in video_stop_streaming()
552 if (!pad || !is_media_entity_v4l2_subdev(pad->entity)) in video_stop_streaming()
555 entity = pad->entity; in video_stop_streaming()
560 if (entity->use_count > 1) { in video_stop_streaming()
562 dev_dbg(video->camss->dev, "Video pipeline still used, don't stop streaming.\n"); in video_stop_streaming()
567 dev_err(video->camss->dev, "Video pipeline stop failed: %d\n", ret); in video_stop_streaming()
574 video->ops->flush_buffers(video, VB2_BUF_STATE_ERROR); in video_stop_streaming()
588 /* -----------------------------------------------------------------------------
595 strscpy(cap->driver, "qcom-camss", sizeof(cap->driver)); in video_querycap()
596 strscpy(cap->card, "Qualcomm Camera Subsystem", sizeof(cap->card)); in video_querycap()
605 u32 mcode = f->mbus_code; in video_enum_fmt()
607 if (f->type != video->type) in video_enum_fmt()
608 return -EINVAL; in video_enum_fmt()
610 if (f->index >= video->nformats) in video_enum_fmt()
611 return -EINVAL; in video_enum_fmt()
616 * If f->mbus_code passed to video_enum_fmt() is not zero, a device in video_enum_fmt()
618 * pixel formats that can be produced from that media bus code. in video_enum_fmt()
619 * This is implemented by skipping video->formats[] entries with in video_enum_fmt()
620 * code != f->mbus_code (if f->mbus_code is not zero). in video_enum_fmt()
621 * If the f->mbus_code passed to video_enum_fmt() is not supported, in video_enum_fmt()
622 * -EINVAL is returned. in video_enum_fmt()
623 * If f->mbus_code is zero, all the pixel formats are enumerated. in video_enum_fmt()
625 k = -1; in video_enum_fmt()
626 for (i = 0; i < video->nformats; i++) { in video_enum_fmt()
627 if (mcode != 0 && video->formats[i].code != mcode) in video_enum_fmt()
631 if (mcode != 0 && video->formats[j].code != mcode) in video_enum_fmt()
633 if (video->formats[i].pixelformat == in video_enum_fmt()
634 video->formats[j].pixelformat) in video_enum_fmt()
641 if (k == f->index) in video_enum_fmt()
645 if (k == -1 || k < f->index) in video_enum_fmt()
647 * All the unique pixel formats matching the arguments in video_enum_fmt()
648 * have been enumerated (k >= 0 and f->index > 0), or in video_enum_fmt()
649 * no pixel formats match the non-zero f->mbus_code (k == -1). in video_enum_fmt()
651 return -EINVAL; in video_enum_fmt()
653 f->pixelformat = video->formats[i].pixelformat; in video_enum_fmt()
664 if (fsize->index) in video_enum_framesizes()
665 return -EINVAL; in video_enum_framesizes()
667 /* Only accept pixel format present in the formats[] table */ in video_enum_framesizes()
668 for (i = 0; i < video->nformats; i++) { in video_enum_framesizes()
669 if (video->formats[i].pixelformat == fsize->pixel_format) in video_enum_framesizes()
673 if (i == video->nformats) in video_enum_framesizes()
674 return -EINVAL; in video_enum_framesizes()
676 fsize->type = V4L2_FRMSIZE_TYPE_CONTINUOUS; in video_enum_framesizes()
677 fsize->stepwise.min_width = CAMSS_FRAME_MIN_WIDTH; in video_enum_framesizes()
678 fsize->stepwise.max_width = CAMSS_FRAME_MAX_WIDTH; in video_enum_framesizes()
679 fsize->stepwise.min_height = CAMSS_FRAME_MIN_HEIGHT; in video_enum_framesizes()
680 fsize->stepwise.max_height = (video->line_based) ? in video_enum_framesizes()
682 fsize->stepwise.step_width = 1; in video_enum_framesizes()
683 fsize->stepwise.step_height = 1; in video_enum_framesizes()
692 *f = video->active_fmt; in video_g_fmt()
708 pix_mp = &f->fmt.pix_mp; in __video_try_fmt()
710 if (video->line_based) in __video_try_fmt()
711 for (i = 0; i < pix_mp->num_planes && i < 3; i++) { in __video_try_fmt()
712 p = &pix_mp->plane_fmt[i]; in __video_try_fmt()
713 bytesperline[i] = clamp_t(u32, p->bytesperline, in __video_try_fmt()
715 sizeimage[i] = clamp_t(u32, p->sizeimage, in __video_try_fmt()
720 for (j = 0; j < video->nformats; j++) in __video_try_fmt()
721 if (pix_mp->pixelformat == video->formats[j].pixelformat) in __video_try_fmt()
724 if (j == video->nformats) in __video_try_fmt()
727 fi = &video->formats[j]; in __video_try_fmt()
728 width = pix_mp->width; in __video_try_fmt()
729 height = pix_mp->height; in __video_try_fmt()
733 pix_mp->pixelformat = fi->pixelformat; in __video_try_fmt()
734 pix_mp->width = clamp_t(u32, width, 1, CAMSS_FRAME_MAX_WIDTH); in __video_try_fmt()
735 pix_mp->height = clamp_t(u32, height, 1, CAMSS_FRAME_MAX_HEIGHT_RDI); in __video_try_fmt()
736 pix_mp->num_planes = fi->planes; in __video_try_fmt()
737 for (i = 0; i < pix_mp->num_planes; i++) { in __video_try_fmt()
738 bpl = pix_mp->width / fi->hsub[i].numerator * in __video_try_fmt()
739 fi->hsub[i].denominator * fi->bpp[i] / 8; in __video_try_fmt()
740 bpl = ALIGN(bpl, video->bpl_alignment); in __video_try_fmt()
741 pix_mp->plane_fmt[i].bytesperline = bpl; in __video_try_fmt()
742 pix_mp->plane_fmt[i].sizeimage = pix_mp->height / in __video_try_fmt()
743 fi->vsub[i].numerator * fi->vsub[i].denominator * bpl; in __video_try_fmt()
746 pix_mp->field = V4L2_FIELD_NONE; in __video_try_fmt()
747 pix_mp->colorspace = V4L2_COLORSPACE_SRGB; in __video_try_fmt()
748 pix_mp->flags = 0; in __video_try_fmt()
749 pix_mp->ycbcr_enc = V4L2_MAP_YCBCR_ENC_DEFAULT(pix_mp->colorspace); in __video_try_fmt()
750 pix_mp->quantization = V4L2_MAP_QUANTIZATION_DEFAULT(true, in __video_try_fmt()
751 pix_mp->colorspace, pix_mp->ycbcr_enc); in __video_try_fmt()
752 pix_mp->xfer_func = V4L2_MAP_XFER_FUNC_DEFAULT(pix_mp->colorspace); in __video_try_fmt()
754 if (video->line_based) in __video_try_fmt()
755 for (i = 0; i < pix_mp->num_planes; i++) { in __video_try_fmt()
756 p = &pix_mp->plane_fmt[i]; in __video_try_fmt()
757 p->bytesperline = clamp_t(u32, p->bytesperline, in __video_try_fmt()
759 p->sizeimage = clamp_t(u32, p->sizeimage, in __video_try_fmt()
760 p->bytesperline, in __video_try_fmt()
761 p->bytesperline * CAMSS_FRAME_MAX_HEIGHT_PIX); in __video_try_fmt()
762 lines = p->sizeimage / p->bytesperline; in __video_try_fmt()
764 if (p->bytesperline < bytesperline[i]) in __video_try_fmt()
765 p->bytesperline = ALIGN(bytesperline[i], 8); in __video_try_fmt()
767 if (p->sizeimage < p->bytesperline * lines) in __video_try_fmt()
768 p->sizeimage = p->bytesperline * lines; in __video_try_fmt()
770 if (p->sizeimage < sizeimage[i]) in __video_try_fmt()
771 p->sizeimage = sizeimage[i]; in __video_try_fmt()
789 if (vb2_is_busy(&video->vb2_q)) in video_s_fmt()
790 return -EBUSY; in video_s_fmt()
796 video->active_fmt = *f; in video_s_fmt()
804 if (input->index > 0) in video_enum_input()
805 return -EINVAL; in video_enum_input()
807 strscpy(input->name, "camera", sizeof(input->name)); in video_enum_input()
808 input->type = V4L2_INPUT_TYPE_CAMERA; in video_enum_input()
822 return input == 0 ? 0 : -EINVAL; in video_s_input()
846 /* -----------------------------------------------------------------------------
857 mutex_lock(&video->lock); in video_open()
861 ret = -ENOMEM; in video_open()
868 file->private_data = vfh; in video_open()
870 ret = v4l2_pipeline_pm_get(&vdev->entity); in video_open()
872 dev_err(video->camss->dev, "Failed to power up pipeline: %d\n", in video_open()
877 mutex_unlock(&video->lock); in video_open()
885 mutex_unlock(&video->lock); in video_open()
896 v4l2_pipeline_pm_put(&vdev->entity); in video_release()
898 file->private_data = NULL; in video_release()
913 /* -----------------------------------------------------------------------------
921 media_entity_cleanup(&vdev->entity); in msm_video_release()
923 mutex_destroy(&video->q_lock); in msm_video_release()
924 mutex_destroy(&video->lock); in msm_video_release()
926 if (atomic_dec_and_test(&video->camss->ref_count)) in msm_video_release()
927 camss_delete(video->camss); in msm_video_release()
931 * msm_video_init_format - Helper function to initialize format
946 .pixelformat = video->formats[0].pixelformat, in msm_video_init_format()
954 video->active_fmt = format; in msm_video_init_format()
960 * msm_video_register - Register a video device node
974 struct media_pad *pad = &video->pad; in msm_video_register()
979 vdev = &video->vdev; in msm_video_register()
981 mutex_init(&video->q_lock); in msm_video_register()
983 q = &video->vb2_q; in msm_video_register()
984 q->drv_priv = video; in msm_video_register()
985 q->mem_ops = &vb2_dma_sg_memops; in msm_video_register()
986 q->ops = &msm_video_vb2_q_ops; in msm_video_register()
987 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; in msm_video_register()
988 q->io_modes = VB2_DMABUF | VB2_MMAP | VB2_READ; in msm_video_register()
989 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; in msm_video_register()
990 q->buf_struct_size = sizeof(struct camss_buffer); in msm_video_register()
991 q->dev = video->camss->dev; in msm_video_register()
992 q->lock = &video->q_lock; in msm_video_register()
995 dev_err(v4l2_dev->dev, "Failed to init vb2 queue: %d\n", ret); in msm_video_register()
999 pad->flags = MEDIA_PAD_FL_SINK; in msm_video_register()
1000 ret = media_entity_pads_init(&vdev->entity, 1, pad); in msm_video_register()
1002 dev_err(v4l2_dev->dev, "Failed to init video entity: %d\n", in msm_video_register()
1007 mutex_init(&video->lock); in msm_video_register()
1009 switch (video->camss->res->version) { in msm_video_register()
1012 video->formats = formats_pix_8x16; in msm_video_register()
1013 video->nformats = ARRAY_SIZE(formats_pix_8x16); in msm_video_register()
1015 video->formats = formats_rdi_8x16; in msm_video_register()
1016 video->nformats = ARRAY_SIZE(formats_rdi_8x16); in msm_video_register()
1022 video->formats = formats_pix_8x96; in msm_video_register()
1023 video->nformats = ARRAY_SIZE(formats_pix_8x96); in msm_video_register()
1025 video->formats = formats_rdi_8x96; in msm_video_register()
1026 video->nformats = ARRAY_SIZE(formats_rdi_8x96); in msm_video_register()
1031 video->formats = formats_rdi_845; in msm_video_register()
1032 video->nformats = ARRAY_SIZE(formats_rdi_845); in msm_video_register()
1038 dev_err(v4l2_dev->dev, "Failed to init format: %d\n", ret); in msm_video_register()
1042 vdev->fops = &msm_vid_fops; in msm_video_register()
1043 vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE_MPLANE | V4L2_CAP_STREAMING in msm_video_register()
1045 vdev->ioctl_ops = &msm_vid_ioctl_ops; in msm_video_register()
1046 vdev->release = msm_video_release; in msm_video_register()
1047 vdev->v4l2_dev = v4l2_dev; in msm_video_register()
1048 vdev->vfl_dir = VFL_DIR_RX; in msm_video_register()
1049 vdev->queue = &video->vb2_q; in msm_video_register()
1050 vdev->lock = &video->lock; in msm_video_register()
1051 strscpy(vdev->name, name, sizeof(vdev->name)); in msm_video_register()
1053 ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1); in msm_video_register()
1055 dev_err(v4l2_dev->dev, "Failed to register video device: %d\n", in msm_video_register()
1061 atomic_inc(&video->camss->ref_count); in msm_video_register()
1066 media_entity_cleanup(&vdev->entity); in msm_video_register()
1067 mutex_destroy(&video->lock); in msm_video_register()
1069 mutex_destroy(&video->q_lock); in msm_video_register()
1076 atomic_inc(&video->camss->ref_count); in msm_video_unregister()
1077 vb2_video_unregister_device(&video->vdev); in msm_video_unregister()
1078 atomic_dec(&video->camss->ref_count); in msm_video_unregister()