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_AYUV32,
173 .vdownsampling = { 1 },
177 .alpha_mask = 0x000000ff,
180 .fourcc = V4L2_PIX_FMT_XYUV32,
181 .vdownsampling = { 1 },
187 .fourcc = V4L2_PIX_FMT_VUYA32,
188 .vdownsampling = { 1 },
192 .alpha_mask = 0xff000000,
195 .fourcc = V4L2_PIX_FMT_VUYX32,
196 .vdownsampling = { 1 },
202 .fourcc = V4L2_PIX_FMT_GREY,
203 .vdownsampling = { 1 },
205 .color_enc = TGP_COLOR_ENC_LUMA,
210 .fourcc = V4L2_PIX_FMT_Y10,
211 .vdownsampling = { 1 },
213 .color_enc = TGP_COLOR_ENC_LUMA,
218 .fourcc = V4L2_PIX_FMT_Y12,
219 .vdownsampling = { 1 },
221 .color_enc = TGP_COLOR_ENC_LUMA,
226 .fourcc = V4L2_PIX_FMT_Y16,
227 .vdownsampling = { 1 },
229 .color_enc = TGP_COLOR_ENC_LUMA,
234 .fourcc = V4L2_PIX_FMT_Y16_BE,
235 .vdownsampling = { 1 },
237 .color_enc = TGP_COLOR_ENC_LUMA,
242 .fourcc = V4L2_PIX_FMT_RGB332, /* rrrgggbb */
243 .vdownsampling = { 1 },
249 .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
250 .vdownsampling = { 1 },
254 .can_do_overlay = true,
257 .fourcc = V4L2_PIX_FMT_RGB565X, /* rrrrrggg gggbbbbb */
258 .vdownsampling = { 1 },
262 .can_do_overlay = true,
265 .fourcc = V4L2_PIX_FMT_RGB444, /* xxxxrrrr ggggbbbb */
266 .vdownsampling = { 1 },
272 .fourcc = V4L2_PIX_FMT_XRGB444, /* xxxxrrrr ggggbbbb */
273 .vdownsampling = { 1 },
279 .fourcc = V4L2_PIX_FMT_ARGB444, /* aaaarrrr ggggbbbb */
280 .vdownsampling = { 1 },
284 .alpha_mask = 0x00f0,
287 .fourcc = V4L2_PIX_FMT_RGB555, /* gggbbbbb xrrrrrgg */
288 .vdownsampling = { 1 },
292 .can_do_overlay = true,
295 .fourcc = V4L2_PIX_FMT_XRGB555, /* gggbbbbb xrrrrrgg */
296 .vdownsampling = { 1 },
300 .can_do_overlay = true,
303 .fourcc = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */
304 .vdownsampling = { 1 },
308 .can_do_overlay = true,
309 .alpha_mask = 0x8000,
312 .fourcc = V4L2_PIX_FMT_RGB555X, /* xrrrrrgg gggbbbbb */
313 .vdownsampling = { 1 },
319 .fourcc = V4L2_PIX_FMT_XRGB555X, /* xrrrrrgg gggbbbbb */
320 .vdownsampling = { 1 },
326 .fourcc = V4L2_PIX_FMT_ARGB555X, /* arrrrrgg gggbbbbb */
327 .vdownsampling = { 1 },
331 .alpha_mask = 0x0080,
334 .fourcc = V4L2_PIX_FMT_RGB24, /* rgb */
335 .vdownsampling = { 1 },
341 .fourcc = V4L2_PIX_FMT_BGR24, /* bgr */
342 .vdownsampling = { 1 },
348 .fourcc = V4L2_PIX_FMT_BGR666, /* bbbbbbgg ggggrrrr rrxxxxxx */
349 .vdownsampling = { 1 },
355 .fourcc = V4L2_PIX_FMT_RGB32, /* xrgb */
356 .vdownsampling = { 1 },
362 .fourcc = V4L2_PIX_FMT_BGR32, /* bgrx */
363 .vdownsampling = { 1 },
369 .fourcc = V4L2_PIX_FMT_XRGB32, /* xrgb */
370 .vdownsampling = { 1 },
376 .fourcc = V4L2_PIX_FMT_XBGR32, /* bgrx */
377 .vdownsampling = { 1 },
383 .fourcc = V4L2_PIX_FMT_ARGB32, /* argb */
384 .vdownsampling = { 1 },
388 .alpha_mask = 0x000000ff,
391 .fourcc = V4L2_PIX_FMT_ABGR32, /* bgra */
392 .vdownsampling = { 1 },
396 .alpha_mask = 0xff000000,
399 .fourcc = V4L2_PIX_FMT_SBGGR8, /* Bayer BG/GR */
400 .vdownsampling = { 1 },
406 .fourcc = V4L2_PIX_FMT_SGBRG8, /* Bayer GB/RG */
407 .vdownsampling = { 1 },
413 .fourcc = V4L2_PIX_FMT_SGRBG8, /* Bayer GR/BG */
414 .vdownsampling = { 1 },
420 .fourcc = V4L2_PIX_FMT_SRGGB8, /* Bayer RG/GB */
421 .vdownsampling = { 1 },
427 .fourcc = V4L2_PIX_FMT_SBGGR10, /* Bayer BG/GR */
428 .vdownsampling = { 1 },
434 .fourcc = V4L2_PIX_FMT_SGBRG10, /* Bayer GB/RG */
435 .vdownsampling = { 1 },
441 .fourcc = V4L2_PIX_FMT_SGRBG10, /* Bayer GR/BG */
442 .vdownsampling = { 1 },
448 .fourcc = V4L2_PIX_FMT_SRGGB10, /* Bayer RG/GB */
449 .vdownsampling = { 1 },
455 .fourcc = V4L2_PIX_FMT_SBGGR12, /* Bayer BG/GR */
456 .vdownsampling = { 1 },
462 .fourcc = V4L2_PIX_FMT_SGBRG12, /* Bayer GB/RG */
463 .vdownsampling = { 1 },
469 .fourcc = V4L2_PIX_FMT_SGRBG12, /* Bayer GR/BG */
470 .vdownsampling = { 1 },
476 .fourcc = V4L2_PIX_FMT_SRGGB12, /* Bayer RG/GB */
477 .vdownsampling = { 1 },
483 .fourcc = V4L2_PIX_FMT_SBGGR16, /* Bayer BG/GR */
484 .vdownsampling = { 1 },
490 .fourcc = V4L2_PIX_FMT_SGBRG16, /* Bayer GB/RG */
491 .vdownsampling = { 1 },
497 .fourcc = V4L2_PIX_FMT_SGRBG16, /* Bayer GR/BG */
498 .vdownsampling = { 1 },
504 .fourcc = V4L2_PIX_FMT_SRGGB16, /* Bayer RG/GB */
505 .vdownsampling = { 1 },
511 .fourcc = V4L2_PIX_FMT_HSV24, /* HSV 24bits */
512 .color_enc = TGP_COLOR_ENC_HSV,
513 .vdownsampling = { 1 },
519 .fourcc = V4L2_PIX_FMT_HSV32, /* HSV 32bits */
520 .color_enc = TGP_COLOR_ENC_HSV,
521 .vdownsampling = { 1 },
527 /* Multiplanar formats */
530 .fourcc = V4L2_PIX_FMT_NV16M,
531 .vdownsampling = { 1, 1 },
532 .bit_depth = { 8, 8 },
533 .color_enc = TGP_COLOR_ENC_YCBCR,
536 .data_offset = { PLANE0_DATA_OFFSET, 0 },
539 .fourcc = V4L2_PIX_FMT_NV61M,
540 .vdownsampling = { 1, 1 },
541 .bit_depth = { 8, 8 },
542 .color_enc = TGP_COLOR_ENC_YCBCR,
545 .data_offset = { 0, PLANE0_DATA_OFFSET },
548 .fourcc = V4L2_PIX_FMT_YUV420M,
549 .vdownsampling = { 1, 2, 2 },
550 .bit_depth = { 8, 4, 4 },
551 .color_enc = TGP_COLOR_ENC_YCBCR,
556 .fourcc = V4L2_PIX_FMT_YVU420M,
557 .vdownsampling = { 1, 2, 2 },
558 .bit_depth = { 8, 4, 4 },
559 .color_enc = TGP_COLOR_ENC_YCBCR,
564 .fourcc = V4L2_PIX_FMT_NV12M,
565 .vdownsampling = { 1, 2 },
566 .bit_depth = { 8, 8 },
567 .color_enc = TGP_COLOR_ENC_YCBCR,
572 .fourcc = V4L2_PIX_FMT_NV21M,
573 .vdownsampling = { 1, 2 },
574 .bit_depth = { 8, 8 },
575 .color_enc = TGP_COLOR_ENC_YCBCR,
580 .fourcc = V4L2_PIX_FMT_YUV422M,
581 .vdownsampling = { 1, 1, 1 },
582 .bit_depth = { 8, 4, 4 },
583 .color_enc = TGP_COLOR_ENC_YCBCR,
588 .fourcc = V4L2_PIX_FMT_YVU422M,
589 .vdownsampling = { 1, 1, 1 },
590 .bit_depth = { 8, 4, 4 },
591 .color_enc = TGP_COLOR_ENC_YCBCR,
596 .fourcc = V4L2_PIX_FMT_YUV444M,
597 .vdownsampling = { 1, 1, 1 },
598 .bit_depth = { 8, 8, 8 },
599 .color_enc = TGP_COLOR_ENC_YCBCR,
604 .fourcc = V4L2_PIX_FMT_YVU444M,
605 .vdownsampling = { 1, 1, 1 },
606 .bit_depth = { 8, 8, 8 },
607 .color_enc = TGP_COLOR_ENC_YCBCR,
613 /* There are this many multiplanar formats in the list */
614 #define VIVID_MPLANAR_FORMATS 10
616 const struct vivid_fmt *vivid_get_format(struct vivid_dev *dev, u32 pixelformat)
618 const struct vivid_fmt *fmt;
621 for (k = 0; k < ARRAY_SIZE(vivid_formats); k++) {
622 fmt = &vivid_formats[k];
623 if (fmt->fourcc == pixelformat)
624 if (fmt->buffers == 1 || dev->multiplanar)
631 bool vivid_vid_can_loop(struct vivid_dev *dev)
633 if (dev->src_rect.width != dev->sink_rect.width ||
634 dev->src_rect.height != dev->sink_rect.height)
636 if (dev->fmt_cap->fourcc != dev->fmt_out->fourcc)
638 if (dev->field_cap != dev->field_out)
641 * While this can be supported, it is just too much work
642 * to actually implement.
644 if (dev->field_cap == V4L2_FIELD_SEQ_TB ||
645 dev->field_cap == V4L2_FIELD_SEQ_BT)
647 if (vivid_is_svid_cap(dev) && vivid_is_svid_out(dev)) {
648 if (!(dev->std_cap[dev->input] & V4L2_STD_525_60) !=
649 !(dev->std_out & V4L2_STD_525_60))
653 if (vivid_is_hdmi_cap(dev) && vivid_is_hdmi_out(dev))
658 void vivid_send_source_change(struct vivid_dev *dev, unsigned type)
660 struct v4l2_event ev = {
661 .type = V4L2_EVENT_SOURCE_CHANGE,
662 .u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
666 for (i = 0; i < dev->num_inputs; i++) {
668 if (dev->input_type[i] == type) {
669 if (video_is_registered(&dev->vid_cap_dev) && dev->has_vid_cap)
670 v4l2_event_queue(&dev->vid_cap_dev, &ev);
671 if (video_is_registered(&dev->vbi_cap_dev) && dev->has_vbi_cap)
672 v4l2_event_queue(&dev->vbi_cap_dev, &ev);
678 * Conversion function that converts a single-planar format to a
679 * single-plane multiplanar format.
681 void fmt_sp2mp(const struct v4l2_format *sp_fmt, struct v4l2_format *mp_fmt)
683 struct v4l2_pix_format_mplane *mp = &mp_fmt->fmt.pix_mp;
684 struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
685 const struct v4l2_pix_format *pix = &sp_fmt->fmt.pix;
686 bool is_out = sp_fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT;
688 memset(mp->reserved, 0, sizeof(mp->reserved));
689 mp_fmt->type = is_out ? V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
690 V4L2_CAP_VIDEO_CAPTURE_MPLANE;
691 mp->width = pix->width;
692 mp->height = pix->height;
693 mp->pixelformat = pix->pixelformat;
694 mp->field = pix->field;
695 mp->colorspace = pix->colorspace;
696 mp->xfer_func = pix->xfer_func;
697 /* Also copies hsv_enc */
698 mp->ycbcr_enc = pix->ycbcr_enc;
699 mp->quantization = pix->quantization;
701 mp->flags = pix->flags;
702 ppix->sizeimage = pix->sizeimage;
703 ppix->bytesperline = pix->bytesperline;
704 memset(ppix->reserved, 0, sizeof(ppix->reserved));
707 int fmt_sp2mp_func(struct file *file, void *priv,
708 struct v4l2_format *f, fmtfunc func)
710 struct v4l2_format fmt;
711 struct v4l2_pix_format_mplane *mp = &fmt.fmt.pix_mp;
712 struct v4l2_plane_pix_format *ppix = &mp->plane_fmt[0];
713 struct v4l2_pix_format *pix = &f->fmt.pix;
716 /* Converts to a mplane format */
718 /* Passes it to the generic mplane format function */
719 ret = func(file, priv, &fmt);
720 /* Copies back the mplane data to the single plane format */
721 pix->width = mp->width;
722 pix->height = mp->height;
723 pix->pixelformat = mp->pixelformat;
724 pix->field = mp->field;
725 pix->colorspace = mp->colorspace;
726 pix->xfer_func = mp->xfer_func;
727 /* Also copies hsv_enc */
728 pix->ycbcr_enc = mp->ycbcr_enc;
729 pix->quantization = mp->quantization;
730 pix->sizeimage = ppix->sizeimage;
731 pix->bytesperline = ppix->bytesperline;
732 pix->flags = mp->flags;
736 int vivid_vid_adjust_sel(unsigned flags, struct v4l2_rect *r)
738 unsigned w = r->width;
739 unsigned h = r->height;
741 /* sanitize w and h in case someone passes ~0 as the value */
744 if (!(flags & V4L2_SEL_FLAG_LE)) {
752 if (!(flags & V4L2_SEL_FLAG_GE)) {
762 if (w > MAX_WIDTH || h > MAX_HEIGHT)
768 /* sanitize left and top in case someone passes ~0 as the value */
771 if (r->left + w > MAX_WIDTH)
772 r->left = MAX_WIDTH - w;
773 if (r->top + h > MAX_HEIGHT)
774 r->top = MAX_HEIGHT - h;
775 if ((flags & (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE)) ==
776 (V4L2_SEL_FLAG_GE | V4L2_SEL_FLAG_LE) &&
777 (r->width != w || r->height != h))
784 int vivid_enum_fmt_vid(struct file *file, void *priv,
785 struct v4l2_fmtdesc *f)
787 struct vivid_dev *dev = video_drvdata(file);
788 const struct vivid_fmt *fmt;
790 if (f->index >= ARRAY_SIZE(vivid_formats) -
791 (dev->multiplanar ? 0 : VIVID_MPLANAR_FORMATS))
794 fmt = &vivid_formats[f->index];
796 f->pixelformat = fmt->fourcc;
800 int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
802 struct vivid_dev *dev = video_drvdata(file);
803 struct video_device *vdev = video_devdata(file);
805 if (vdev->vfl_dir == VFL_DIR_RX) {
806 if (!vivid_is_sdtv_cap(dev))
808 *id = dev->std_cap[dev->input];
810 if (!vivid_is_svid_out(dev))
817 int vidioc_g_dv_timings(struct file *file, void *_fh,
818 struct v4l2_dv_timings *timings)
820 struct vivid_dev *dev = video_drvdata(file);
821 struct video_device *vdev = video_devdata(file);
823 if (vdev->vfl_dir == VFL_DIR_RX) {
824 if (!vivid_is_hdmi_cap(dev))
826 *timings = dev->dv_timings_cap[dev->input];
828 if (!vivid_is_hdmi_out(dev))
830 *timings = dev->dv_timings_out;
835 int vidioc_enum_dv_timings(struct file *file, void *_fh,
836 struct v4l2_enum_dv_timings *timings)
838 struct vivid_dev *dev = video_drvdata(file);
839 struct video_device *vdev = video_devdata(file);
841 if (vdev->vfl_dir == VFL_DIR_RX) {
842 if (!vivid_is_hdmi_cap(dev))
845 if (!vivid_is_hdmi_out(dev))
848 return v4l2_enum_dv_timings_cap(timings, &vivid_dv_timings_cap,
852 int vidioc_dv_timings_cap(struct file *file, void *_fh,
853 struct v4l2_dv_timings_cap *cap)
855 struct vivid_dev *dev = video_drvdata(file);
856 struct video_device *vdev = video_devdata(file);
858 if (vdev->vfl_dir == VFL_DIR_RX) {
859 if (!vivid_is_hdmi_cap(dev))
862 if (!vivid_is_hdmi_out(dev))
865 *cap = vivid_dv_timings_cap;
869 int vidioc_g_edid(struct file *file, void *_fh,
870 struct v4l2_edid *edid)
872 struct vivid_dev *dev = video_drvdata(file);
873 struct video_device *vdev = video_devdata(file);
874 struct cec_adapter *adap;
876 memset(edid->reserved, 0, sizeof(edid->reserved));
877 if (vdev->vfl_dir == VFL_DIR_RX) {
878 if (edid->pad >= dev->num_inputs)
880 if (dev->input_type[edid->pad] != HDMI)
882 adap = dev->cec_rx_adap;
884 unsigned int bus_idx;
886 if (edid->pad >= dev->num_outputs)
888 if (dev->output_type[edid->pad] != HDMI)
890 if (!dev->display_present[edid->pad])
892 bus_idx = dev->cec_output2bus_map[edid->pad];
893 adap = dev->cec_tx_adap[bus_idx];
895 if (edid->start_block == 0 && edid->blocks == 0) {
896 edid->blocks = dev->edid_blocks;
899 if (dev->edid_blocks == 0)
901 if (edid->start_block >= dev->edid_blocks)
903 if (edid->blocks > dev->edid_blocks - edid->start_block)
904 edid->blocks = dev->edid_blocks - edid->start_block;
906 v4l2_set_edid_phys_addr(dev->edid, dev->edid_blocks * 128, adap->phys_addr);
907 memcpy(edid->edid, dev->edid + edid->start_block * 128, edid->blocks * 128);