1 // SPDX-License-Identifier: GPL-2.0-only
3 * vivid-vid-common.c - common video support functions.
5 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
8 #include <linux/errno.h>
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/videodev2.h>
12 #include <linux/v4l2-dv-timings.h>
13 #include <media/v4l2-common.h>
14 #include <media/v4l2-event.h>
15 #include <media/v4l2-dv-timings.h>
17 #include "vivid-core.h"
18 #include "vivid-vid-common.h"
20 const struct v4l2_dv_timings_cap vivid_dv_timings_cap = {
21 .type = V4L2_DV_BT_656_1120,
22 /* keep this initialization for compatibility with GCC < 4.4.6 */
24 V4L2_INIT_BT_TIMINGS(16, MAX_WIDTH, 16, MAX_HEIGHT, 14000000, 775000000,
25 V4L2_DV_BT_STD_CEA861 | V4L2_DV_BT_STD_DMT |
26 V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF,
27 V4L2_DV_BT_CAP_PROGRESSIVE | V4L2_DV_BT_CAP_INTERLACED)
30 /* ------------------------------------------------------------------
32 ------------------------------------------------------------------*/
34 struct vivid_fmt vivid_formats[] = {
36 .fourcc = V4L2_PIX_FMT_YUYV,
37 .vdownsampling = { 1 },
39 .color_enc = TGP_COLOR_ENC_YCBCR,
42 .data_offset = { PLANE0_DATA_OFFSET },
45 .fourcc = V4L2_PIX_FMT_UYVY,
46 .vdownsampling = { 1 },
48 .color_enc = TGP_COLOR_ENC_YCBCR,
53 .fourcc = V4L2_PIX_FMT_YVYU,
54 .vdownsampling = { 1 },
56 .color_enc = TGP_COLOR_ENC_YCBCR,
61 .fourcc = V4L2_PIX_FMT_VYUY,
62 .vdownsampling = { 1 },
64 .color_enc = TGP_COLOR_ENC_YCBCR,
69 .fourcc = V4L2_PIX_FMT_YUV422P,
70 .vdownsampling = { 1, 1, 1 },
71 .bit_depth = { 8, 4, 4 },
72 .color_enc = TGP_COLOR_ENC_YCBCR,
77 .fourcc = V4L2_PIX_FMT_YUV420,
78 .vdownsampling = { 1, 2, 2 },
79 .bit_depth = { 8, 4, 4 },
80 .color_enc = TGP_COLOR_ENC_YCBCR,
85 .fourcc = V4L2_PIX_FMT_YVU420,
86 .vdownsampling = { 1, 2, 2 },
87 .bit_depth = { 8, 4, 4 },
88 .color_enc = TGP_COLOR_ENC_YCBCR,
93 .fourcc = V4L2_PIX_FMT_NV12,
94 .vdownsampling = { 1, 2 },
95 .bit_depth = { 8, 8 },
96 .color_enc = TGP_COLOR_ENC_YCBCR,
101 .fourcc = V4L2_PIX_FMT_NV21,
102 .vdownsampling = { 1, 2 },
103 .bit_depth = { 8, 8 },
104 .color_enc = TGP_COLOR_ENC_YCBCR,
109 .fourcc = V4L2_PIX_FMT_NV16,
110 .vdownsampling = { 1, 1 },
111 .bit_depth = { 8, 8 },
112 .color_enc = TGP_COLOR_ENC_YCBCR,
117 .fourcc = V4L2_PIX_FMT_NV61,
118 .vdownsampling = { 1, 1 },
119 .bit_depth = { 8, 8 },
120 .color_enc = TGP_COLOR_ENC_YCBCR,
125 .fourcc = V4L2_PIX_FMT_NV24,
126 .vdownsampling = { 1, 1 },
127 .bit_depth = { 8, 16 },
128 .color_enc = TGP_COLOR_ENC_YCBCR,
133 .fourcc = V4L2_PIX_FMT_NV42,
134 .vdownsampling = { 1, 1 },
135 .bit_depth = { 8, 16 },
136 .color_enc = TGP_COLOR_ENC_YCBCR,
141 .fourcc = V4L2_PIX_FMT_YUV555, /* uuuvvvvv ayyyyyuu */
142 .vdownsampling = { 1 },
146 .alpha_mask = 0x8000,
149 .fourcc = V4L2_PIX_FMT_YUV565, /* uuuvvvvv yyyyyuuu */
150 .vdownsampling = { 1 },
156 .fourcc = V4L2_PIX_FMT_YUV444, /* uuuuvvvv aaaayyyy */
157 .vdownsampling = { 1 },
161 .alpha_mask = 0xf000,
164 .fourcc = V4L2_PIX_FMT_YUV32, /* ayuv */
165 .vdownsampling = { 1 },
169 .alpha_mask = 0x000000ff,
172 .fourcc = V4L2_PIX_FMT_GREY,
173 .vdownsampling = { 1 },
175 .color_enc = TGP_COLOR_ENC_LUMA,
180 .fourcc = V4L2_PIX_FMT_Y10,
181 .vdownsampling = { 1 },
183 .color_enc = TGP_COLOR_ENC_LUMA,
188 .fourcc = V4L2_PIX_FMT_Y12,
189 .vdownsampling = { 1 },
191 .color_enc = TGP_COLOR_ENC_LUMA,
196 .fourcc = V4L2_PIX_FMT_Y16,
197 .vdownsampling = { 1 },
199 .color_enc = TGP_COLOR_ENC_LUMA,
204 .fourcc = V4L2_PIX_FMT_Y16_BE,
205 .vdownsampling = { 1 },
207 .color_enc = TGP_COLOR_ENC_LUMA,
212 .fourcc = V4L2_PIX_FMT_RGB332, /* rrrgggbb */
213 .vdownsampling = { 1 },
219 .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
220 .vdownsampling = { 1 },
224 .can_do_overlay = true,
227 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
228 .vdownsampling = { 1 },
232 .can_do_overlay = true,
235 .fourcc = V4L2_PIX_FMT_RGB444, /* xxxxrrrr ggggbbbb */
236 .vdownsampling = { 1 },
242 .fourcc = V4L2_PIX_FMT_XRGB444, /* xxxxrrrr ggggbbbb */
243 .vdownsampling = { 1 },
249 .fourcc = V4L2_PIX_FMT_ARGB444, /* aaaarrrr ggggbbbb */
250 .vdownsampling = { 1 },
254 .alpha_mask = 0x00f0,
257 .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb xrrrrrgg */
258 .vdownsampling = { 1 },
262 .can_do_overlay = true,
265 .fourcc = V4L2_PIX_FMT_XRGB555, /* gggbbbbb xrrrrrgg */
266 .vdownsampling = { 1 },
270 .can_do_overlay = true,
273 .fourcc = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */
274 .vdownsampling = { 1 },
278 .can_do_overlay = true,
279 .alpha_mask = 0x8000,
282 .fourcc = V4L2_PIX_FMT_RGB555X, /* xrrrrrgg gggbbbbb */
283 .vdownsampling = { 1 },
289 .fourcc = V4L2_PIX_FMT_XRGB555X, /* xrrrrrgg gggbbbbb */
290 .vdownsampling = { 1 },
296 .fourcc = V4L2_PIX_FMT_ARGB555X, /* arrrrrgg gggbbbbb */
297 .vdownsampling = { 1 },
301 .alpha_mask = 0x0080,
304 .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
305 .vdownsampling = { 1 },
311 .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
312 .vdownsampling = { 1 },
318 .fourcc = V4L2_PIX_FMT_BGR666, /* bbbbbbgg ggggrrrr rrxxxxxx */
319 .vdownsampling = { 1 },
325 .fourcc = V4L2_PIX_FMT_RGB32, /* xrgb */
326 .vdownsampling = { 1 },
332 .fourcc = V4L2_PIX_FMT_BGR32, /* bgrx */
333 .vdownsampling = { 1 },
339 .fourcc = V4L2_PIX_FMT_XRGB32, /* xrgb */
340 .vdownsampling = { 1 },
346 .fourcc = V4L2_PIX_FMT_XBGR32, /* bgrx */
347 .vdownsampling = { 1 },
353 .fourcc = V4L2_PIX_FMT_ARGB32, /* argb */
354 .vdownsampling = { 1 },
358 .alpha_mask = 0x000000ff,
361 .fourcc = V4L2_PIX_FMT_ABGR32, /* bgra */
362 .vdownsampling = { 1 },
366 .alpha_mask = 0xff000000,
369 .fourcc = V4L2_PIX_FMT_SBGGR8, /* Bayer BG/GR */
370 .vdownsampling = { 1 },
376 .fourcc = V4L2_PIX_FMT_SGBRG8, /* Bayer GB/RG */
377 .vdownsampling = { 1 },
383 .fourcc = V4L2_PIX_FMT_SGRBG8, /* Bayer GR/BG */
384 .vdownsampling = { 1 },
390 .fourcc = V4L2_PIX_FMT_SRGGB8, /* Bayer RG/GB */
391 .vdownsampling = { 1 },
397 .fourcc = V4L2_PIX_FMT_SBGGR10, /* Bayer BG/GR */
398 .vdownsampling = { 1 },
404 .fourcc = V4L2_PIX_FMT_SGBRG10, /* Bayer GB/RG */
405 .vdownsampling = { 1 },
411 .fourcc = V4L2_PIX_FMT_SGRBG10, /* Bayer GR/BG */
412 .vdownsampling = { 1 },
418 .fourcc = V4L2_PIX_FMT_SRGGB10, /* Bayer RG/GB */
419 .vdownsampling = { 1 },
425 .fourcc = V4L2_PIX_FMT_SBGGR12, /* Bayer BG/GR */
426 .vdownsampling = { 1 },
432 .fourcc = V4L2_PIX_FMT_SGBRG12, /* Bayer GB/RG */
433 .vdownsampling = { 1 },
439 .fourcc = V4L2_PIX_FMT_SGRBG12, /* Bayer GR/BG */
440 .vdownsampling = { 1 },
446 .fourcc = V4L2_PIX_FMT_SRGGB12, /* Bayer RG/GB */
447 .vdownsampling = { 1 },
453 .fourcc = V4L2_PIX_FMT_HSV24, /* HSV 24bits */
454 .color_enc = TGP_COLOR_ENC_HSV,
455 .vdownsampling = { 1 },
461 .fourcc = V4L2_PIX_FMT_HSV32, /* HSV 32bits */
462 .color_enc = TGP_COLOR_ENC_HSV,
463 .vdownsampling = { 1 },
469 /* Multiplanar formats */
472 .fourcc = V4L2_PIX_FMT_NV16M,
473 .vdownsampling = { 1, 1 },
474 .bit_depth = { 8, 8 },
475 .color_enc = TGP_COLOR_ENC_YCBCR,
478 .data_offset = { PLANE0_DATA_OFFSET, 0 },
481 .fourcc = V4L2_PIX_FMT_NV61M,
482 .vdownsampling = { 1, 1 },
483 .bit_depth = { 8, 8 },
484 .color_enc = TGP_COLOR_ENC_YCBCR,
487 .data_offset = { 0, PLANE0_DATA_OFFSET },
490 .fourcc = V4L2_PIX_FMT_YUV420M,
491 .vdownsampling = { 1, 2, 2 },
492 .bit_depth = { 8, 4, 4 },
493 .color_enc = TGP_COLOR_ENC_YCBCR,
498 .fourcc = V4L2_PIX_FMT_YVU420M,
499 .vdownsampling = { 1, 2, 2 },
500 .bit_depth = { 8, 4, 4 },
501 .color_enc = TGP_COLOR_ENC_YCBCR,
506 .fourcc = V4L2_PIX_FMT_NV12M,
507 .vdownsampling = { 1, 2 },
508 .bit_depth = { 8, 8 },
509 .color_enc = TGP_COLOR_ENC_YCBCR,
514 .fourcc = V4L2_PIX_FMT_NV21M,
515 .vdownsampling = { 1, 2 },
516 .bit_depth = { 8, 8 },
517 .color_enc = TGP_COLOR_ENC_YCBCR,
522 .fourcc = V4L2_PIX_FMT_YUV422M,
523 .vdownsampling = { 1, 1, 1 },
524 .bit_depth = { 8, 4, 4 },
525 .color_enc = TGP_COLOR_ENC_YCBCR,
530 .fourcc = V4L2_PIX_FMT_YVU422M,
531 .vdownsampling = { 1, 1, 1 },
532 .bit_depth = { 8, 4, 4 },
533 .color_enc = TGP_COLOR_ENC_YCBCR,
538 .fourcc = V4L2_PIX_FMT_YUV444M,
539 .vdownsampling = { 1, 1, 1 },
540 .bit_depth = { 8, 8, 8 },
541 .color_enc = TGP_COLOR_ENC_YCBCR,
546 .fourcc = V4L2_PIX_FMT_YVU444M,
547 .vdownsampling = { 1, 1, 1 },
548 .bit_depth = { 8, 8, 8 },
549 .color_enc = TGP_COLOR_ENC_YCBCR,
555 /* There are this many multiplanar formats in the list */
556 #define VIVID_MPLANAR_FORMATS 10
558 const struct vivid_fmt *vivid_get_format(struct vivid_dev *dev, u32 pixelformat)
560 const struct vivid_fmt *fmt;
563 for (k = 0; k < ARRAY_SIZE(vivid_formats); k++) {
564 fmt = &vivid_formats[k];
565 if (fmt->fourcc == pixelformat)
566 if (fmt->buffers == 1 || dev->multiplanar)
573 bool vivid_vid_can_loop(struct vivid_dev *dev)
575 if (dev->src_rect.width != dev->sink_rect.width ||
576 dev->src_rect.height != dev->sink_rect.height)
578 if (dev->fmt_cap->fourcc != dev->fmt_out->fourcc)
580 if (dev->field_cap != dev->field_out)
583 * While this can be supported, it is just too much work
584 * to actually implement.
586 if (dev->field_cap == V4L2_FIELD_SEQ_TB ||
587 dev->field_cap == V4L2_FIELD_SEQ_BT)
589 if (vivid_is_svid_cap(dev) && vivid_is_svid_out(dev)) {
590 if (!(dev->std_cap & V4L2_STD_525_60) !=
591 !(dev->std_out & V4L2_STD_525_60))
595 if (vivid_is_hdmi_cap(dev) && vivid_is_hdmi_out(dev))
600 void vivid_send_source_change(struct vivid_dev *dev, unsigned type)
602 struct v4l2_event ev = {
603 .type = V4L2_EVENT_SOURCE_CHANGE,
604 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
608 for (i = 0; i < dev->num_inputs; i++) {
610 if (dev->input_type[i] == type) {
611 if (video_is_registered(&dev->vid_cap_dev) && dev->has_vid_cap)
612 v4l2_event_queue(&dev->vid_cap_dev, &ev);
613 if (video_is_registered(&dev->vbi_cap_dev) && dev->has_vbi_cap)
614 v4l2_event_queue(&dev->vbi_cap_dev, &ev);
620 * Conversion function that converts a single-planar format to a
621 * single-plane multiplanar format.
623 void fmt_sp2mp(const struct v4l2_format *sp_fmt, struct v4l2_format *mp_fmt)
625 struct v4l2_pix_format_mplane *mp = &mp_fmt->fmt.pix_mp;
626 struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
627 const struct v4l2_pix_format *pix = &sp_fmt->fmt.pix;
628 bool is_out = sp_fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT;
630 memset(mp->reserved, 0, sizeof(mp->reserved));
631 mp_fmt->type = is_out ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
632 V4L2_CAP_VIDEO_CAPTURE_MPLANE;
633 mp->width = pix->width;
634 mp->height = pix->height;
635 mp->pixelformat = pix->pixelformat;
636 mp->field = pix->field;
637 mp->colorspace = pix->colorspace;
638 mp->xfer_func = pix->xfer_func;
639 /* Also copies hsv_enc */
640 mp->ycbcr_enc = pix->ycbcr_enc;
641 mp->quantization = pix->quantization;
643 mp->flags = pix->flags;
644 ppix->sizeimage = pix->sizeimage;
645 ppix->bytesperline = pix->bytesperline;
646 memset(ppix->reserved, 0, sizeof(ppix->reserved));
649 int fmt_sp2mp_func(struct file *file, void *priv,
650 struct v4l2_format *f, fmtfunc func)
652 struct v4l2_format fmt;
653 struct v4l2_pix_format_mplane *mp = &fmt.fmt.pix_mp;
654 struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
655 struct v4l2_pix_format *pix = &f->fmt.pix;
658 /* Converts to a mplane format */
660 /* Passes it to the generic mplane format function */
661 ret = func(file, priv, &fmt);
662 /* Copies back the mplane data to the single plane format */
663 pix->width = mp->width;
664 pix->height = mp->height;
665 pix->pixelformat = mp->pixelformat;
666 pix->field = mp->field;
667 pix->colorspace = mp->colorspace;
668 pix->xfer_func = mp->xfer_func;
669 /* Also copies hsv_enc */
670 pix->ycbcr_enc = mp->ycbcr_enc;
671 pix->quantization = mp->quantization;
672 pix->sizeimage = ppix->sizeimage;
673 pix->bytesperline = ppix->bytesperline;
674 pix->flags = mp->flags;
678 int vivid_vid_adjust_sel(unsigned flags, struct v4l2_rect *r)
680 unsigned w = r->width;
681 unsigned h = r->height;
683 /* sanitize w and h in case someone passes ~0 as the value */
686 if (!(flags & V4L2_SEL_FLAG_LE)) {
694 if (!(flags & V4L2_SEL_FLAG_GE)) {
704 if (w > MAX_WIDTH || h > MAX_HEIGHT)
710 /* sanitize left and top in case someone passes ~0 as the value */
713 if (r->left + w > MAX_WIDTH)
714 r->left = MAX_WIDTH - w;
715 if (r->top + h > MAX_HEIGHT)
716 r->top = MAX_HEIGHT - h;
717 if ((flags & (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE)) ==
718 (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE) &&
719 (r->width != w || r->height != h))
726 int vivid_enum_fmt_vid(struct file *file, void *priv,
727 struct v4l2_fmtdesc *f)
729 struct vivid_dev *dev = video_drvdata(file);
730 const struct vivid_fmt *fmt;
732 if (f->index >= ARRAY_SIZE(vivid_formats) -
733 (dev->multiplanar ? 0 : VIVID_MPLANAR_FORMATS))
736 fmt = &vivid_formats[f->index];
738 f->pixelformat = fmt->fourcc;
742 int vidioc_enum_fmt_vid_mplane(struct file *file, void *priv,
743 struct v4l2_fmtdesc *f)
745 struct vivid_dev *dev = video_drvdata(file);
747 if (!dev->multiplanar)
749 return vivid_enum_fmt_vid(file, priv, f);
752 int vidioc_enum_fmt_vid(struct file *file, void *priv,
753 struct v4l2_fmtdesc *f)
755 struct vivid_dev *dev = video_drvdata(file);
757 if (dev->multiplanar)
759 return vivid_enum_fmt_vid(file, priv, f);
762 int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
764 struct vivid_dev *dev = video_drvdata(file);
765 struct video_device *vdev = video_devdata(file);
767 if (vdev->vfl_dir == VFL_DIR_RX) {
768 if (!vivid_is_sdtv_cap(dev))
772 if (!vivid_is_svid_out(dev))
779 int vidioc_g_dv_timings(struct file *file, void *_fh,
780 struct v4l2_dv_timings *timings)
782 struct vivid_dev *dev = video_drvdata(file);
783 struct video_device *vdev = video_devdata(file);
785 if (vdev->vfl_dir == VFL_DIR_RX) {
786 if (!vivid_is_hdmi_cap(dev))
788 *timings = dev->dv_timings_cap;
790 if (!vivid_is_hdmi_out(dev))
792 *timings = dev->dv_timings_out;
797 int vidioc_enum_dv_timings(struct file *file, void *_fh,
798 struct v4l2_enum_dv_timings *timings)
800 struct vivid_dev *dev = video_drvdata(file);
801 struct video_device *vdev = video_devdata(file);
803 if (vdev->vfl_dir == VFL_DIR_RX) {
804 if (!vivid_is_hdmi_cap(dev))
807 if (!vivid_is_hdmi_out(dev))
810 return v4l2_enum_dv_timings_cap(timings, &vivid_dv_timings_cap,
814 int vidioc_dv_timings_cap(struct file *file, void *_fh,
815 struct v4l2_dv_timings_cap *cap)
817 struct vivid_dev *dev = video_drvdata(file);
818 struct video_device *vdev = video_devdata(file);
820 if (vdev->vfl_dir == VFL_DIR_RX) {
821 if (!vivid_is_hdmi_cap(dev))
824 if (!vivid_is_hdmi_out(dev))
827 *cap = vivid_dv_timings_cap;
831 int vidioc_g_edid(struct file *file, void *_fh,
832 struct v4l2_edid *edid)
834 struct vivid_dev *dev = video_drvdata(file);
835 struct video_device *vdev = video_devdata(file);
836 struct cec_adapter *adap;
838 memset(edid->reserved, 0, sizeof(edid->reserved));
839 if (vdev->vfl_dir == VFL_DIR_RX) {
840 if (edid->pad >= dev->num_inputs)
842 if (dev->input_type[edid->pad] != HDMI)
844 adap = dev->cec_rx_adap;
846 unsigned int bus_idx;
848 if (edid->pad >= dev->num_outputs)
850 if (dev->output_type[edid->pad] != HDMI)
852 bus_idx = dev->cec_output2bus_map[edid->pad];
853 adap = dev->cec_tx_adap[bus_idx];
855 if (edid->start_block == 0 && edid->blocks == 0) {
856 edid->blocks = dev->edid_blocks;
859 if (dev->edid_blocks == 0)
861 if (edid->start_block >= dev->edid_blocks)
863 if (edid->blocks > dev->edid_blocks - edid->start_block)
864 edid->blocks = dev->edid_blocks - edid->start_block;
866 v4l2_set_edid_phys_addr(dev->edid, dev->edid_blocks * 128, adap->phys_addr);
867 memcpy(edid->edid, dev->edid + edid->start_block * 128, edid->blocks * 128);