16884db3cSHans Verkuil // SPDX-License-Identifier: GPL-2.0-only
285756a06SHans Verkuil /*
385756a06SHans Verkuil * cobalt V4L2 API
485756a06SHans Verkuil *
585756a06SHans Verkuil * Derived from ivtv-ioctl.c and cx18-fileops.c
685756a06SHans Verkuil *
785756a06SHans Verkuil * Copyright 2012-2015 Cisco Systems, Inc. and/or its affiliates.
885756a06SHans Verkuil * All rights reserved.
985756a06SHans Verkuil */
1085756a06SHans Verkuil
1185756a06SHans Verkuil #include <linux/dma-mapping.h>
1285756a06SHans Verkuil #include <linux/delay.h>
1399cabb18SHans Verkuil #include <linux/math64.h>
1485756a06SHans Verkuil #include <linux/pci.h>
1585756a06SHans Verkuil #include <linux/v4l2-dv-timings.h>
1685756a06SHans Verkuil
1785756a06SHans Verkuil #include <media/v4l2-ctrls.h>
1885756a06SHans Verkuil #include <media/v4l2-event.h>
19810c168cSHans Verkuil #include <media/v4l2-dv-timings.h>
20b5dcee22SMauro Carvalho Chehab #include <media/i2c/adv7604.h>
21b5dcee22SMauro Carvalho Chehab #include <media/i2c/adv7842.h>
2285756a06SHans Verkuil
2385756a06SHans Verkuil #include "cobalt-alsa.h"
2485756a06SHans Verkuil #include "cobalt-cpld.h"
2585756a06SHans Verkuil #include "cobalt-driver.h"
2685756a06SHans Verkuil #include "cobalt-v4l2.h"
2785756a06SHans Verkuil #include "cobalt-irq.h"
2885756a06SHans Verkuil #include "cobalt-omnitek.h"
2985756a06SHans Verkuil
3085756a06SHans Verkuil static const struct v4l2_dv_timings cea1080p60 = V4L2_DV_BT_CEA_1920X1080P60;
3185756a06SHans Verkuil
3285756a06SHans Verkuil /* vb2 DMA streaming ops */
3385756a06SHans Verkuil
cobalt_queue_setup(struct vb2_queue * q,unsigned int * num_buffers,unsigned int * num_planes,unsigned int sizes[],struct device * alloc_devs[])34df9ecb0cSHans Verkuil static int cobalt_queue_setup(struct vb2_queue *q,
3585756a06SHans Verkuil unsigned int *num_buffers, unsigned int *num_planes,
3636c0f8b3SHans Verkuil unsigned int sizes[], struct device *alloc_devs[])
3785756a06SHans Verkuil {
3885756a06SHans Verkuil struct cobalt_stream *s = q->drv_priv;
3985756a06SHans Verkuil unsigned size = s->stride * s->height;
4085756a06SHans Verkuil
4185756a06SHans Verkuil if (*num_buffers < 3)
4285756a06SHans Verkuil *num_buffers = 3;
4385756a06SHans Verkuil if (*num_buffers > NR_BUFS)
4485756a06SHans Verkuil *num_buffers = NR_BUFS;
45df9ecb0cSHans Verkuil if (*num_planes)
46df9ecb0cSHans Verkuil return sizes[0] < size ? -EINVAL : 0;
47df9ecb0cSHans Verkuil *num_planes = 1;
48df9ecb0cSHans Verkuil sizes[0] = size;
4985756a06SHans Verkuil return 0;
5085756a06SHans Verkuil }
5185756a06SHans Verkuil
cobalt_buf_init(struct vb2_buffer * vb)5285756a06SHans Verkuil static int cobalt_buf_init(struct vb2_buffer *vb)
5385756a06SHans Verkuil {
5485756a06SHans Verkuil struct cobalt_stream *s = vb->vb2_queue->drv_priv;
5585756a06SHans Verkuil struct cobalt *cobalt = s->cobalt;
5685756a06SHans Verkuil const size_t max_pages_per_line =
5785756a06SHans Verkuil (COBALT_MAX_WIDTH * COBALT_MAX_BPP) / PAGE_SIZE + 2;
5885756a06SHans Verkuil const size_t bytes =
5985756a06SHans Verkuil COBALT_MAX_HEIGHT * max_pages_per_line * 0x20;
6085756a06SHans Verkuil const size_t audio_bytes = ((1920 * 4) / PAGE_SIZE + 1) * 0x20;
612d700715SJunghak Sung struct sg_dma_desc_info *desc = &s->dma_desc_info[vb->index];
6285756a06SHans Verkuil struct sg_table *sg_desc = vb2_dma_sg_plane_desc(vb, 0);
6385756a06SHans Verkuil unsigned size;
6485756a06SHans Verkuil int ret;
6585756a06SHans Verkuil
6685756a06SHans Verkuil size = s->stride * s->height;
6785756a06SHans Verkuil if (vb2_plane_size(vb, 0) < size) {
6885756a06SHans Verkuil cobalt_info("data will not fit into plane (%lu < %u)\n",
6985756a06SHans Verkuil vb2_plane_size(vb, 0), size);
7085756a06SHans Verkuil return -EINVAL;
7185756a06SHans Verkuil }
7285756a06SHans Verkuil
7385756a06SHans Verkuil if (desc->virt == NULL) {
7485756a06SHans Verkuil desc->dev = &cobalt->pci_dev->dev;
7585756a06SHans Verkuil descriptor_list_allocate(desc,
7685756a06SHans Verkuil s->is_audio ? audio_bytes : bytes);
7785756a06SHans Verkuil if (desc->virt == NULL)
7885756a06SHans Verkuil return -ENOMEM;
7985756a06SHans Verkuil }
8085756a06SHans Verkuil ret = descriptor_list_create(cobalt, sg_desc->sgl,
8185756a06SHans Verkuil !s->is_output, sg_desc->nents, size,
8285756a06SHans Verkuil s->width * s->bpp, s->stride, desc);
8385756a06SHans Verkuil if (ret)
8485756a06SHans Verkuil descriptor_list_free(desc);
8585756a06SHans Verkuil return ret;
8685756a06SHans Verkuil }
8785756a06SHans Verkuil
cobalt_buf_cleanup(struct vb2_buffer * vb)8885756a06SHans Verkuil static void cobalt_buf_cleanup(struct vb2_buffer *vb)
8985756a06SHans Verkuil {
9085756a06SHans Verkuil struct cobalt_stream *s = vb->vb2_queue->drv_priv;
912d700715SJunghak Sung struct sg_dma_desc_info *desc = &s->dma_desc_info[vb->index];
9285756a06SHans Verkuil
9385756a06SHans Verkuil descriptor_list_free(desc);
9485756a06SHans Verkuil }
9585756a06SHans Verkuil
cobalt_buf_prepare(struct vb2_buffer * vb)9685756a06SHans Verkuil static int cobalt_buf_prepare(struct vb2_buffer *vb)
9785756a06SHans Verkuil {
982d700715SJunghak Sung struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
9985756a06SHans Verkuil struct cobalt_stream *s = vb->vb2_queue->drv_priv;
10085756a06SHans Verkuil
10185756a06SHans Verkuil vb2_set_plane_payload(vb, 0, s->stride * s->height);
1022d700715SJunghak Sung vbuf->field = V4L2_FIELD_NONE;
10385756a06SHans Verkuil return 0;
10485756a06SHans Verkuil }
10585756a06SHans Verkuil
chain_all_buffers(struct cobalt_stream * s)10685756a06SHans Verkuil static void chain_all_buffers(struct cobalt_stream *s)
10785756a06SHans Verkuil {
10885756a06SHans Verkuil struct sg_dma_desc_info *desc[NR_BUFS];
10985756a06SHans Verkuil struct cobalt_buffer *cb;
11085756a06SHans Verkuil struct list_head *p;
11185756a06SHans Verkuil int i = 0;
11285756a06SHans Verkuil
11385756a06SHans Verkuil list_for_each(p, &s->bufs) {
11485756a06SHans Verkuil cb = list_entry(p, struct cobalt_buffer, list);
1152d700715SJunghak Sung desc[i] = &s->dma_desc_info[cb->vb.vb2_buf.index];
11685756a06SHans Verkuil if (i > 0)
11785756a06SHans Verkuil descriptor_list_chain(desc[i-1], desc[i]);
11885756a06SHans Verkuil i++;
11985756a06SHans Verkuil }
12085756a06SHans Verkuil }
12185756a06SHans Verkuil
cobalt_buf_queue(struct vb2_buffer * vb)12285756a06SHans Verkuil static void cobalt_buf_queue(struct vb2_buffer *vb)
12385756a06SHans Verkuil {
1242d700715SJunghak Sung struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
12585756a06SHans Verkuil struct vb2_queue *q = vb->vb2_queue;
12685756a06SHans Verkuil struct cobalt_stream *s = q->drv_priv;
1272d700715SJunghak Sung struct cobalt_buffer *cb = to_cobalt_buffer(vbuf);
1282d700715SJunghak Sung struct sg_dma_desc_info *desc = &s->dma_desc_info[vb->index];
12985756a06SHans Verkuil unsigned long flags;
13085756a06SHans Verkuil
13185756a06SHans Verkuil /* Prepare new buffer */
13285756a06SHans Verkuil descriptor_list_loopback(desc);
13385756a06SHans Verkuil descriptor_list_interrupt_disable(desc);
13485756a06SHans Verkuil
13585756a06SHans Verkuil spin_lock_irqsave(&s->irqlock, flags);
13685756a06SHans Verkuil list_add_tail(&cb->list, &s->bufs);
13785756a06SHans Verkuil chain_all_buffers(s);
13885756a06SHans Verkuil spin_unlock_irqrestore(&s->irqlock, flags);
13985756a06SHans Verkuil }
14085756a06SHans Verkuil
cobalt_enable_output(struct cobalt_stream * s)14185756a06SHans Verkuil static void cobalt_enable_output(struct cobalt_stream *s)
14285756a06SHans Verkuil {
14385756a06SHans Verkuil struct cobalt *cobalt = s->cobalt;
14485756a06SHans Verkuil struct v4l2_bt_timings *bt = &s->timings.bt;
1454da70768SHans Verkuil struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
14685756a06SHans Verkuil COBALT_TX_BASE(cobalt);
14785756a06SHans Verkuil unsigned fmt = s->pixfmt != V4L2_PIX_FMT_BGR32 ?
14885756a06SHans Verkuil M00514_CONTROL_BITMAP_FORMAT_16_BPP_MSK : 0;
14985756a06SHans Verkuil struct v4l2_subdev_format sd_fmt = {
15085756a06SHans Verkuil .which = V4L2_SUBDEV_FORMAT_ACTIVE,
15185756a06SHans Verkuil };
152605a74e6SHans Verkuil u64 clk = bt->pixelclock;
15385756a06SHans Verkuil
154605a74e6SHans Verkuil if (bt->flags & V4L2_DV_FL_REDUCED_FPS)
155605a74e6SHans Verkuil clk = div_u64(clk * 1000ULL, 1001);
156605a74e6SHans Verkuil if (!cobalt_cpld_set_freq(cobalt, clk)) {
15785756a06SHans Verkuil cobalt_err("pixelclock out of range\n");
15885756a06SHans Verkuil return;
15985756a06SHans Verkuil }
16085756a06SHans Verkuil
16185756a06SHans Verkuil sd_fmt.format.colorspace = s->colorspace;
162136a5e9eSHans Verkuil sd_fmt.format.xfer_func = s->xfer_func;
16385756a06SHans Verkuil sd_fmt.format.ycbcr_enc = s->ycbcr_enc;
16485756a06SHans Verkuil sd_fmt.format.quantization = s->quantization;
16585756a06SHans Verkuil sd_fmt.format.width = bt->width;
16685756a06SHans Verkuil sd_fmt.format.height = bt->height;
16785756a06SHans Verkuil
16885756a06SHans Verkuil /* Set up FDMA packer */
16985756a06SHans Verkuil switch (s->pixfmt) {
17085756a06SHans Verkuil case V4L2_PIX_FMT_YUYV:
17185756a06SHans Verkuil sd_fmt.format.code = MEDIA_BUS_FMT_UYVY8_1X16;
17285756a06SHans Verkuil break;
17385756a06SHans Verkuil case V4L2_PIX_FMT_BGR32:
17485756a06SHans Verkuil sd_fmt.format.code = MEDIA_BUS_FMT_RGB888_1X24;
17585756a06SHans Verkuil break;
17685756a06SHans Verkuil }
17785756a06SHans Verkuil v4l2_subdev_call(s->sd, pad, set_fmt, NULL, &sd_fmt);
17885756a06SHans Verkuil
1794da70768SHans Verkuil iowrite32(0, &vo->control);
18085756a06SHans Verkuil /* 1080p60 */
1814da70768SHans Verkuil iowrite32(bt->hsync, &vo->sync_generator_h_sync_length);
1824da70768SHans Verkuil iowrite32(bt->hbackporch, &vo->sync_generator_h_backporch_length);
1834da70768SHans Verkuil iowrite32(bt->width, &vo->sync_generator_h_active_length);
1844da70768SHans Verkuil iowrite32(bt->hfrontporch, &vo->sync_generator_h_frontporch_length);
1854da70768SHans Verkuil iowrite32(bt->vsync, &vo->sync_generator_v_sync_length);
1864da70768SHans Verkuil iowrite32(bt->vbackporch, &vo->sync_generator_v_backporch_length);
1874da70768SHans Verkuil iowrite32(bt->height, &vo->sync_generator_v_active_length);
1884da70768SHans Verkuil iowrite32(bt->vfrontporch, &vo->sync_generator_v_frontporch_length);
1894da70768SHans Verkuil iowrite32(0x9900c1, &vo->error_color);
19085756a06SHans Verkuil
1914da70768SHans Verkuil iowrite32(M00514_CONTROL_BITMAP_SYNC_GENERATOR_LOAD_PARAM_MSK | fmt,
1924da70768SHans Verkuil &vo->control);
1934da70768SHans Verkuil iowrite32(M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK | fmt, &vo->control);
1944da70768SHans Verkuil iowrite32(M00514_CONTROL_BITMAP_SYNC_GENERATOR_ENABLE_MSK |
19585756a06SHans Verkuil M00514_CONTROL_BITMAP_FLOW_CTRL_OUTPUT_ENABLE_MSK |
1964da70768SHans Verkuil fmt, &vo->control);
19785756a06SHans Verkuil }
19885756a06SHans Verkuil
cobalt_enable_input(struct cobalt_stream * s)19985756a06SHans Verkuil static void cobalt_enable_input(struct cobalt_stream *s)
20085756a06SHans Verkuil {
20185756a06SHans Verkuil struct cobalt *cobalt = s->cobalt;
20285756a06SHans Verkuil int ch = (int)s->video_channel;
2034da70768SHans Verkuil struct m00235_fdma_packer_regmap __iomem *packer;
20485756a06SHans Verkuil struct v4l2_subdev_format sd_fmt_yuyv = {
20585756a06SHans Verkuil .pad = s->pad_source,
20685756a06SHans Verkuil .which = V4L2_SUBDEV_FORMAT_ACTIVE,
20785756a06SHans Verkuil .format.code = MEDIA_BUS_FMT_YUYV8_1X16,
20885756a06SHans Verkuil };
20985756a06SHans Verkuil struct v4l2_subdev_format sd_fmt_rgb = {
21085756a06SHans Verkuil .pad = s->pad_source,
21185756a06SHans Verkuil .which = V4L2_SUBDEV_FORMAT_ACTIVE,
21285756a06SHans Verkuil .format.code = MEDIA_BUS_FMT_RGB888_1X24,
21385756a06SHans Verkuil };
21485756a06SHans Verkuil
21585756a06SHans Verkuil cobalt_dbg(1, "video_channel %d (%s, %s)\n",
21685756a06SHans Verkuil s->video_channel,
21785756a06SHans Verkuil s->input == 0 ? "hdmi" : "generator",
21885756a06SHans Verkuil "YUYV");
21985756a06SHans Verkuil
22085756a06SHans Verkuil packer = COBALT_CVI_PACKER(cobalt, ch);
22185756a06SHans Verkuil
22285756a06SHans Verkuil /* Set up FDMA packer */
22385756a06SHans Verkuil switch (s->pixfmt) {
22485756a06SHans Verkuil case V4L2_PIX_FMT_YUYV:
2254da70768SHans Verkuil iowrite32(M00235_CONTROL_BITMAP_ENABLE_MSK |
2264da70768SHans Verkuil (1 << M00235_CONTROL_BITMAP_PACK_FORMAT_OFST),
2274da70768SHans Verkuil &packer->control);
22885756a06SHans Verkuil v4l2_subdev_call(s->sd, pad, set_fmt, NULL,
22985756a06SHans Verkuil &sd_fmt_yuyv);
23085756a06SHans Verkuil break;
23185756a06SHans Verkuil case V4L2_PIX_FMT_RGB24:
2324da70768SHans Verkuil iowrite32(M00235_CONTROL_BITMAP_ENABLE_MSK |
2334da70768SHans Verkuil (2 << M00235_CONTROL_BITMAP_PACK_FORMAT_OFST),
2344da70768SHans Verkuil &packer->control);
23585756a06SHans Verkuil v4l2_subdev_call(s->sd, pad, set_fmt, NULL,
23685756a06SHans Verkuil &sd_fmt_rgb);
23785756a06SHans Verkuil break;
23885756a06SHans Verkuil case V4L2_PIX_FMT_BGR32:
2394da70768SHans Verkuil iowrite32(M00235_CONTROL_BITMAP_ENABLE_MSK |
24085756a06SHans Verkuil M00235_CONTROL_BITMAP_ENDIAN_FORMAT_MSK |
2414da70768SHans Verkuil (3 << M00235_CONTROL_BITMAP_PACK_FORMAT_OFST),
2424da70768SHans Verkuil &packer->control);
24385756a06SHans Verkuil v4l2_subdev_call(s->sd, pad, set_fmt, NULL,
24485756a06SHans Verkuil &sd_fmt_rgb);
24585756a06SHans Verkuil break;
24685756a06SHans Verkuil }
24785756a06SHans Verkuil }
24885756a06SHans Verkuil
cobalt_dma_start_streaming(struct cobalt_stream * s)24985756a06SHans Verkuil static void cobalt_dma_start_streaming(struct cobalt_stream *s)
25085756a06SHans Verkuil {
25185756a06SHans Verkuil struct cobalt *cobalt = s->cobalt;
25285756a06SHans Verkuil int rx = s->video_channel;
2534da70768SHans Verkuil struct m00460_evcnt_regmap __iomem *evcnt =
25485756a06SHans Verkuil COBALT_CVI_EVCNT(cobalt, rx);
25585756a06SHans Verkuil struct cobalt_buffer *cb;
25685756a06SHans Verkuil unsigned long flags;
25785756a06SHans Verkuil
25885756a06SHans Verkuil spin_lock_irqsave(&s->irqlock, flags);
25985756a06SHans Verkuil if (!s->is_output) {
2604da70768SHans Verkuil iowrite32(M00460_CONTROL_BITMAP_CLEAR_MSK, &evcnt->control);
2614da70768SHans Verkuil iowrite32(M00460_CONTROL_BITMAP_ENABLE_MSK, &evcnt->control);
26285756a06SHans Verkuil } else {
2634da70768SHans Verkuil struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
26485756a06SHans Verkuil COBALT_TX_BASE(cobalt);
2654da70768SHans Verkuil u32 ctrl = ioread32(&vo->control);
26685756a06SHans Verkuil
26785756a06SHans Verkuil ctrl &= ~(M00514_CONTROL_BITMAP_EVCNT_ENABLE_MSK |
26885756a06SHans Verkuil M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK);
2694da70768SHans Verkuil iowrite32(ctrl | M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK,
2704da70768SHans Verkuil &vo->control);
2714da70768SHans Verkuil iowrite32(ctrl | M00514_CONTROL_BITMAP_EVCNT_ENABLE_MSK,
2724da70768SHans Verkuil &vo->control);
27385756a06SHans Verkuil }
27485756a06SHans Verkuil cb = list_first_entry(&s->bufs, struct cobalt_buffer, list);
2752d700715SJunghak Sung omni_sg_dma_start(s, &s->dma_desc_info[cb->vb.vb2_buf.index]);
27685756a06SHans Verkuil spin_unlock_irqrestore(&s->irqlock, flags);
27785756a06SHans Verkuil }
27885756a06SHans Verkuil
cobalt_start_streaming(struct vb2_queue * q,unsigned int count)27985756a06SHans Verkuil static int cobalt_start_streaming(struct vb2_queue *q, unsigned int count)
28085756a06SHans Verkuil {
28185756a06SHans Verkuil struct cobalt_stream *s = q->drv_priv;
28285756a06SHans Verkuil struct cobalt *cobalt = s->cobalt;
2834da70768SHans Verkuil struct m00233_video_measure_regmap __iomem *vmr;
2844da70768SHans Verkuil struct m00473_freewheel_regmap __iomem *fw;
2854da70768SHans Verkuil struct m00479_clk_loss_detector_regmap __iomem *clkloss;
28685756a06SHans Verkuil int rx = s->video_channel;
2874da70768SHans Verkuil struct m00389_cvi_regmap __iomem *cvi = COBALT_CVI(cobalt, rx);
2884da70768SHans Verkuil struct m00460_evcnt_regmap __iomem *evcnt = COBALT_CVI_EVCNT(cobalt, rx);
28985756a06SHans Verkuil struct v4l2_bt_timings *bt = &s->timings.bt;
29085756a06SHans Verkuil u64 tot_size;
2914da70768SHans Verkuil u32 clk_freq;
29285756a06SHans Verkuil
29385756a06SHans Verkuil if (s->is_audio)
29485756a06SHans Verkuil goto done;
29585756a06SHans Verkuil if (s->is_output) {
29685756a06SHans Verkuil s->unstable_frame = false;
29785756a06SHans Verkuil cobalt_enable_output(s);
29885756a06SHans Verkuil goto done;
29985756a06SHans Verkuil }
30085756a06SHans Verkuil
30185756a06SHans Verkuil cobalt_enable_input(s);
30285756a06SHans Verkuil
30385756a06SHans Verkuil fw = COBALT_CVI_FREEWHEEL(cobalt, rx);
30485756a06SHans Verkuil vmr = COBALT_CVI_VMR(cobalt, rx);
30585756a06SHans Verkuil clkloss = COBALT_CVI_CLK_LOSS(cobalt, rx);
30685756a06SHans Verkuil
3074da70768SHans Verkuil iowrite32(M00460_CONTROL_BITMAP_CLEAR_MSK, &evcnt->control);
3084da70768SHans Verkuil iowrite32(M00460_CONTROL_BITMAP_ENABLE_MSK, &evcnt->control);
3094da70768SHans Verkuil iowrite32(bt->width, &cvi->frame_width);
3104da70768SHans Verkuil iowrite32(bt->height, &cvi->frame_height);
31185756a06SHans Verkuil tot_size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
3124da70768SHans Verkuil iowrite32(div_u64((u64)V4L2_DV_BT_FRAME_WIDTH(bt) * COBALT_CLK * 4,
3134da70768SHans Verkuil bt->pixelclock), &vmr->hsync_timeout_val);
3144da70768SHans Verkuil iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK, &vmr->control);
3154da70768SHans Verkuil clk_freq = ioread32(&fw->clk_freq);
3164da70768SHans Verkuil iowrite32(clk_freq / 1000000, &clkloss->ref_clk_cnt_val);
31785756a06SHans Verkuil /* The lower bound for the clock frequency is 0.5% lower as is
31885756a06SHans Verkuil * allowed by the spec */
31995a86e43SHans Verkuil iowrite32(div_u64(bt->pixelclock * 995, 1000000000),
3204da70768SHans Verkuil &clkloss->test_clk_cnt_val);
32185756a06SHans Verkuil /* will be enabled after the first frame has been received */
3224da70768SHans Verkuil iowrite32(bt->width * bt->height, &fw->active_length);
3234da70768SHans Verkuil iowrite32(div_u64((u64)clk_freq * tot_size, bt->pixelclock),
3244da70768SHans Verkuil &fw->total_length);
3254da70768SHans Verkuil iowrite32(M00233_IRQ_TRIGGERS_BITMAP_VACTIVE_AREA_MSK |
3264da70768SHans Verkuil M00233_IRQ_TRIGGERS_BITMAP_HACTIVE_AREA_MSK,
3274da70768SHans Verkuil &vmr->irq_triggers);
3284da70768SHans Verkuil iowrite32(0, &cvi->control);
3294da70768SHans Verkuil iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK, &vmr->control);
33085756a06SHans Verkuil
3314da70768SHans Verkuil iowrite32(0xff, &fw->output_color);
3324da70768SHans Verkuil iowrite32(M00479_CTRL_BITMAP_ENABLE_MSK, &clkloss->ctrl);
3334da70768SHans Verkuil iowrite32(M00473_CTRL_BITMAP_ENABLE_MSK |
3344da70768SHans Verkuil M00473_CTRL_BITMAP_FORCE_FREEWHEEL_MODE_MSK, &fw->ctrl);
33585756a06SHans Verkuil s->unstable_frame = true;
33685756a06SHans Verkuil s->enable_freewheel = false;
33785756a06SHans Verkuil s->enable_cvi = false;
33885756a06SHans Verkuil s->skip_first_frames = 0;
33985756a06SHans Verkuil
34085756a06SHans Verkuil done:
34185756a06SHans Verkuil s->sequence = 0;
34285756a06SHans Verkuil cobalt_dma_start_streaming(s);
34385756a06SHans Verkuil return 0;
34485756a06SHans Verkuil }
34585756a06SHans Verkuil
cobalt_dma_stop_streaming(struct cobalt_stream * s)34685756a06SHans Verkuil static void cobalt_dma_stop_streaming(struct cobalt_stream *s)
34785756a06SHans Verkuil {
34885756a06SHans Verkuil struct cobalt *cobalt = s->cobalt;
34985756a06SHans Verkuil struct sg_dma_desc_info *desc;
35085756a06SHans Verkuil struct cobalt_buffer *cb;
35185756a06SHans Verkuil struct list_head *p;
35285756a06SHans Verkuil unsigned long flags;
35385756a06SHans Verkuil int timeout_msec = 100;
35485756a06SHans Verkuil int rx = s->video_channel;
3554da70768SHans Verkuil struct m00460_evcnt_regmap __iomem *evcnt =
35685756a06SHans Verkuil COBALT_CVI_EVCNT(cobalt, rx);
35785756a06SHans Verkuil
35885756a06SHans Verkuil if (!s->is_output) {
3594da70768SHans Verkuil iowrite32(0, &evcnt->control);
36085756a06SHans Verkuil } else if (!s->is_audio) {
3614da70768SHans Verkuil struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
36285756a06SHans Verkuil COBALT_TX_BASE(cobalt);
36385756a06SHans Verkuil
3644da70768SHans Verkuil iowrite32(M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK, &vo->control);
3654da70768SHans Verkuil iowrite32(0, &vo->control);
36685756a06SHans Verkuil }
36785756a06SHans Verkuil
36885756a06SHans Verkuil /* Try to stop the DMA engine gracefully */
36985756a06SHans Verkuil spin_lock_irqsave(&s->irqlock, flags);
37085756a06SHans Verkuil list_for_each(p, &s->bufs) {
37185756a06SHans Verkuil cb = list_entry(p, struct cobalt_buffer, list);
3722d700715SJunghak Sung desc = &s->dma_desc_info[cb->vb.vb2_buf.index];
37385756a06SHans Verkuil /* Stop DMA after this descriptor chain */
37485756a06SHans Verkuil descriptor_list_end_of_chain(desc);
37585756a06SHans Verkuil }
37685756a06SHans Verkuil spin_unlock_irqrestore(&s->irqlock, flags);
37785756a06SHans Verkuil
37816790554SMauro Carvalho Chehab /* Wait 100 millisecond for DMA to finish, abort on timeout. */
37985756a06SHans Verkuil if (!wait_event_timeout(s->q.done_wq, is_dma_done(s),
38085756a06SHans Verkuil msecs_to_jiffies(timeout_msec))) {
38185756a06SHans Verkuil omni_sg_dma_abort_channel(s);
38285756a06SHans Verkuil pr_warn("aborted\n");
38385756a06SHans Verkuil }
38485756a06SHans Verkuil cobalt_write_bar0(cobalt, DMA_INTERRUPT_STATUS_REG,
38585756a06SHans Verkuil 1 << s->dma_channel);
38685756a06SHans Verkuil }
38785756a06SHans Verkuil
cobalt_stop_streaming(struct vb2_queue * q)38885756a06SHans Verkuil static void cobalt_stop_streaming(struct vb2_queue *q)
38985756a06SHans Verkuil {
39085756a06SHans Verkuil struct cobalt_stream *s = q->drv_priv;
39185756a06SHans Verkuil struct cobalt *cobalt = s->cobalt;
39285756a06SHans Verkuil int rx = s->video_channel;
3934da70768SHans Verkuil struct m00233_video_measure_regmap __iomem *vmr;
3944da70768SHans Verkuil struct m00473_freewheel_regmap __iomem *fw;
3954da70768SHans Verkuil struct m00479_clk_loss_detector_regmap __iomem *clkloss;
39685756a06SHans Verkuil struct cobalt_buffer *cb;
39785756a06SHans Verkuil struct list_head *p, *safe;
39885756a06SHans Verkuil unsigned long flags;
39985756a06SHans Verkuil
40085756a06SHans Verkuil cobalt_dma_stop_streaming(s);
40185756a06SHans Verkuil
40285756a06SHans Verkuil /* Return all buffers to user space */
40385756a06SHans Verkuil spin_lock_irqsave(&s->irqlock, flags);
40485756a06SHans Verkuil list_for_each_safe(p, safe, &s->bufs) {
40585756a06SHans Verkuil cb = list_entry(p, struct cobalt_buffer, list);
40685756a06SHans Verkuil list_del(&cb->list);
4072d700715SJunghak Sung vb2_buffer_done(&cb->vb.vb2_buf, VB2_BUF_STATE_ERROR);
40885756a06SHans Verkuil }
40985756a06SHans Verkuil spin_unlock_irqrestore(&s->irqlock, flags);
41085756a06SHans Verkuil
41185756a06SHans Verkuil if (s->is_audio || s->is_output)
41285756a06SHans Verkuil return;
41385756a06SHans Verkuil
41485756a06SHans Verkuil fw = COBALT_CVI_FREEWHEEL(cobalt, rx);
41585756a06SHans Verkuil vmr = COBALT_CVI_VMR(cobalt, rx);
41685756a06SHans Verkuil clkloss = COBALT_CVI_CLK_LOSS(cobalt, rx);
4174da70768SHans Verkuil iowrite32(0, &vmr->control);
4184da70768SHans Verkuil iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK, &vmr->control);
4194da70768SHans Verkuil iowrite32(0, &fw->ctrl);
4204da70768SHans Verkuil iowrite32(0, &clkloss->ctrl);
42185756a06SHans Verkuil }
42285756a06SHans Verkuil
42385756a06SHans Verkuil static const struct vb2_ops cobalt_qops = {
42485756a06SHans Verkuil .queue_setup = cobalt_queue_setup,
42585756a06SHans Verkuil .buf_init = cobalt_buf_init,
42685756a06SHans Verkuil .buf_cleanup = cobalt_buf_cleanup,
42785756a06SHans Verkuil .buf_prepare = cobalt_buf_prepare,
42885756a06SHans Verkuil .buf_queue = cobalt_buf_queue,
42985756a06SHans Verkuil .start_streaming = cobalt_start_streaming,
43085756a06SHans Verkuil .stop_streaming = cobalt_stop_streaming,
43185756a06SHans Verkuil .wait_prepare = vb2_ops_wait_prepare,
43285756a06SHans Verkuil .wait_finish = vb2_ops_wait_finish,
43385756a06SHans Verkuil };
43485756a06SHans Verkuil
43585756a06SHans Verkuil /* V4L2 ioctls */
43685756a06SHans Verkuil
43785756a06SHans Verkuil #ifdef CONFIG_VIDEO_ADV_DEBUG
cobalt_cobaltc(struct cobalt * cobalt,unsigned int cmd,void * arg)43885756a06SHans Verkuil static int cobalt_cobaltc(struct cobalt *cobalt, unsigned int cmd, void *arg)
43985756a06SHans Verkuil {
44085756a06SHans Verkuil struct v4l2_dbg_register *regs = arg;
44185756a06SHans Verkuil void __iomem *adrs = cobalt->bar1 + regs->reg;
44285756a06SHans Verkuil
44385756a06SHans Verkuil cobalt_info("cobalt_cobaltc: adrs = %p\n", adrs);
44485756a06SHans Verkuil
44585756a06SHans Verkuil if (!capable(CAP_SYS_ADMIN))
44685756a06SHans Verkuil return -EPERM;
44785756a06SHans Verkuil
44885756a06SHans Verkuil regs->size = 4;
44985756a06SHans Verkuil if (cmd == VIDIOC_DBG_S_REGISTER)
45085756a06SHans Verkuil iowrite32(regs->val, adrs);
45185756a06SHans Verkuil else
45285756a06SHans Verkuil regs->val = ioread32(adrs);
45385756a06SHans Verkuil return 0;
45485756a06SHans Verkuil }
45585756a06SHans Verkuil
cobalt_g_register(struct file * file,void * priv_fh,struct v4l2_dbg_register * reg)45685756a06SHans Verkuil static int cobalt_g_register(struct file *file, void *priv_fh,
45785756a06SHans Verkuil struct v4l2_dbg_register *reg)
45885756a06SHans Verkuil {
45985756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file);
46085756a06SHans Verkuil struct cobalt *cobalt = s->cobalt;
46185756a06SHans Verkuil
46285756a06SHans Verkuil return cobalt_cobaltc(cobalt, VIDIOC_DBG_G_REGISTER, reg);
46385756a06SHans Verkuil }
46485756a06SHans Verkuil
cobalt_s_register(struct file * file,void * priv_fh,const struct v4l2_dbg_register * reg)46585756a06SHans Verkuil static int cobalt_s_register(struct file *file, void *priv_fh,
46685756a06SHans Verkuil const struct v4l2_dbg_register *reg)
46785756a06SHans Verkuil {
46885756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file);
46985756a06SHans Verkuil struct cobalt *cobalt = s->cobalt;
47085756a06SHans Verkuil
47185756a06SHans Verkuil return cobalt_cobaltc(cobalt, VIDIOC_DBG_S_REGISTER,
47285756a06SHans Verkuil (struct v4l2_dbg_register *)reg);
47385756a06SHans Verkuil }
47485756a06SHans Verkuil #endif
47585756a06SHans Verkuil
cobalt_querycap(struct file * file,void * priv_fh,struct v4l2_capability * vcap)47685756a06SHans Verkuil static int cobalt_querycap(struct file *file, void *priv_fh,
47785756a06SHans Verkuil struct v4l2_capability *vcap)
47885756a06SHans Verkuil {
47985756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file);
48085756a06SHans Verkuil struct cobalt *cobalt = s->cobalt;
48185756a06SHans Verkuil
482c0decac1SMauro Carvalho Chehab strscpy(vcap->driver, "cobalt", sizeof(vcap->driver));
483c0decac1SMauro Carvalho Chehab strscpy(vcap->card, "cobalt", sizeof(vcap->card));
48485756a06SHans Verkuil snprintf(vcap->bus_info, sizeof(vcap->bus_info),
48585756a06SHans Verkuil "PCIe:%s", pci_name(cobalt->pci_dev));
48621615365SHans Verkuil vcap->capabilities = V4L2_CAP_STREAMING | V4L2_CAP_READWRITE |
48721615365SHans Verkuil V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_DEVICE_CAPS;
48885756a06SHans Verkuil if (cobalt->have_hsma_tx)
48985756a06SHans Verkuil vcap->capabilities |= V4L2_CAP_VIDEO_OUTPUT;
49085756a06SHans Verkuil return 0;
49185756a06SHans Verkuil }
49285756a06SHans Verkuil
cobalt_video_input_status_show(struct cobalt_stream * s)49385756a06SHans Verkuil static void cobalt_video_input_status_show(struct cobalt_stream *s)
49485756a06SHans Verkuil {
4954da70768SHans Verkuil struct m00389_cvi_regmap __iomem *cvi;
4964da70768SHans Verkuil struct m00233_video_measure_regmap __iomem *vmr;
4974da70768SHans Verkuil struct m00473_freewheel_regmap __iomem *fw;
4984da70768SHans Verkuil struct m00479_clk_loss_detector_regmap __iomem *clkloss;
4994da70768SHans Verkuil struct m00235_fdma_packer_regmap __iomem *packer;
50085756a06SHans Verkuil int rx = s->video_channel;
50185756a06SHans Verkuil struct cobalt *cobalt = s->cobalt;
5024da70768SHans Verkuil u32 cvi_ctrl, cvi_stat;
5034da70768SHans Verkuil u32 vmr_ctrl, vmr_stat;
50485756a06SHans Verkuil
50585756a06SHans Verkuil cvi = COBALT_CVI(cobalt, rx);
50685756a06SHans Verkuil vmr = COBALT_CVI_VMR(cobalt, rx);
50785756a06SHans Verkuil fw = COBALT_CVI_FREEWHEEL(cobalt, rx);
50885756a06SHans Verkuil clkloss = COBALT_CVI_CLK_LOSS(cobalt, rx);
50985756a06SHans Verkuil packer = COBALT_CVI_PACKER(cobalt, rx);
5104da70768SHans Verkuil cvi_ctrl = ioread32(&cvi->control);
5114da70768SHans Verkuil cvi_stat = ioread32(&cvi->status);
5124da70768SHans Verkuil vmr_ctrl = ioread32(&vmr->control);
5139f750b2bSHans Verkuil vmr_stat = ioread32(&vmr->status);
51485756a06SHans Verkuil cobalt_info("rx%d: cvi resolution: %dx%d\n", rx,
5154da70768SHans Verkuil ioread32(&cvi->frame_width), ioread32(&cvi->frame_height));
51685756a06SHans Verkuil cobalt_info("rx%d: cvi control: %s%s%s\n", rx,
5174da70768SHans Verkuil (cvi_ctrl & M00389_CONTROL_BITMAP_ENABLE_MSK) ?
51885756a06SHans Verkuil "enable " : "disable ",
5194da70768SHans Verkuil (cvi_ctrl & M00389_CONTROL_BITMAP_HSYNC_POLARITY_LOW_MSK) ?
52085756a06SHans Verkuil "HSync- " : "HSync+ ",
5214da70768SHans Verkuil (cvi_ctrl & M00389_CONTROL_BITMAP_VSYNC_POLARITY_LOW_MSK) ?
52285756a06SHans Verkuil "VSync- " : "VSync+ ");
52385756a06SHans Verkuil cobalt_info("rx%d: cvi status: %s%s\n", rx,
5244da70768SHans Verkuil (cvi_stat & M00389_STATUS_BITMAP_LOCK_MSK) ?
52585756a06SHans Verkuil "lock " : "no-lock ",
5264da70768SHans Verkuil (cvi_stat & M00389_STATUS_BITMAP_ERROR_MSK) ?
52785756a06SHans Verkuil "error " : "no-error ");
52885756a06SHans Verkuil
52985756a06SHans Verkuil cobalt_info("rx%d: Measurements: %s%s%s%s%s%s%s\n", rx,
5304da70768SHans Verkuil (vmr_ctrl & M00233_CONTROL_BITMAP_HSYNC_POLARITY_LOW_MSK) ?
53185756a06SHans Verkuil "HSync- " : "HSync+ ",
5324da70768SHans Verkuil (vmr_ctrl & M00233_CONTROL_BITMAP_VSYNC_POLARITY_LOW_MSK) ?
53385756a06SHans Verkuil "VSync- " : "VSync+ ",
5344da70768SHans Verkuil (vmr_ctrl & M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK) ?
53585756a06SHans Verkuil "enabled " : "disabled ",
5364da70768SHans Verkuil (vmr_ctrl & M00233_CONTROL_BITMAP_ENABLE_INTERRUPT_MSK) ?
53785756a06SHans Verkuil "irq-enabled " : "irq-disabled ",
5384da70768SHans Verkuil (vmr_ctrl & M00233_CONTROL_BITMAP_UPDATE_ON_HSYNC_MSK) ?
53985756a06SHans Verkuil "update-on-hsync " : "",
5404da70768SHans Verkuil (vmr_stat & M00233_STATUS_BITMAP_HSYNC_TIMEOUT_MSK) ?
54185756a06SHans Verkuil "hsync-timeout " : "",
5424da70768SHans Verkuil (vmr_stat & M00233_STATUS_BITMAP_INIT_DONE_MSK) ?
54385756a06SHans Verkuil "init-done" : "");
54485756a06SHans Verkuil cobalt_info("rx%d: irq_status: 0x%02x irq_triggers: 0x%02x\n", rx,
5454da70768SHans Verkuil ioread32(&vmr->irq_status) & 0xff,
5464da70768SHans Verkuil ioread32(&vmr->irq_triggers) & 0xff);
5474da70768SHans Verkuil cobalt_info("rx%d: vsync: %d\n", rx, ioread32(&vmr->vsync_time));
5484da70768SHans Verkuil cobalt_info("rx%d: vbp: %d\n", rx, ioread32(&vmr->vback_porch));
5494da70768SHans Verkuil cobalt_info("rx%d: vact: %d\n", rx, ioread32(&vmr->vactive_area));
5504da70768SHans Verkuil cobalt_info("rx%d: vfb: %d\n", rx, ioread32(&vmr->vfront_porch));
5514da70768SHans Verkuil cobalt_info("rx%d: hsync: %d\n", rx, ioread32(&vmr->hsync_time));
5524da70768SHans Verkuil cobalt_info("rx%d: hbp: %d\n", rx, ioread32(&vmr->hback_porch));
5534da70768SHans Verkuil cobalt_info("rx%d: hact: %d\n", rx, ioread32(&vmr->hactive_area));
5544da70768SHans Verkuil cobalt_info("rx%d: hfb: %d\n", rx, ioread32(&vmr->hfront_porch));
55585756a06SHans Verkuil cobalt_info("rx%d: Freewheeling: %s%s%s\n", rx,
5564da70768SHans Verkuil (ioread32(&fw->ctrl) & M00473_CTRL_BITMAP_ENABLE_MSK) ?
55785756a06SHans Verkuil "enabled " : "disabled ",
5584da70768SHans Verkuil (ioread32(&fw->ctrl) & M00473_CTRL_BITMAP_FORCE_FREEWHEEL_MODE_MSK) ?
55985756a06SHans Verkuil "forced " : "",
5604da70768SHans Verkuil (ioread32(&fw->status) & M00473_STATUS_BITMAP_FREEWHEEL_MODE_MSK) ?
56185756a06SHans Verkuil "freewheeling " : "video-passthrough ");
5624da70768SHans Verkuil iowrite32(0xff, &vmr->irq_status);
56385756a06SHans Verkuil cobalt_info("rx%d: Clock Loss Detection: %s%s\n", rx,
5644da70768SHans Verkuil (ioread32(&clkloss->ctrl) & M00479_CTRL_BITMAP_ENABLE_MSK) ?
56585756a06SHans Verkuil "enabled " : "disabled ",
5664da70768SHans Verkuil (ioread32(&clkloss->status) & M00479_STATUS_BITMAP_CLOCK_MISSING_MSK) ?
56785756a06SHans Verkuil "clock-missing " : "found-clock ");
5684da70768SHans Verkuil cobalt_info("rx%d: Packer: %x\n", rx, ioread32(&packer->control));
56985756a06SHans Verkuil }
57085756a06SHans Verkuil
cobalt_log_status(struct file * file,void * priv_fh)57185756a06SHans Verkuil static int cobalt_log_status(struct file *file, void *priv_fh)
57285756a06SHans Verkuil {
57385756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file);
57485756a06SHans Verkuil struct cobalt *cobalt = s->cobalt;
5754da70768SHans Verkuil struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
57685756a06SHans Verkuil COBALT_TX_BASE(cobalt);
57785756a06SHans Verkuil u8 stat;
57885756a06SHans Verkuil
57985756a06SHans Verkuil cobalt_info("%s", cobalt->hdl_info);
58085756a06SHans Verkuil cobalt_info("sysctrl: %08x, sysstat: %08x\n",
58185756a06SHans Verkuil cobalt_g_sysctrl(cobalt),
58285756a06SHans Verkuil cobalt_g_sysstat(cobalt));
58385756a06SHans Verkuil cobalt_info("dma channel: %d, video channel: %d\n",
58485756a06SHans Verkuil s->dma_channel, s->video_channel);
58585756a06SHans Verkuil cobalt_pcie_status_show(cobalt);
58685756a06SHans Verkuil cobalt_cpld_status(cobalt);
58785756a06SHans Verkuil cobalt_irq_log_status(cobalt);
58885756a06SHans Verkuil v4l2_subdev_call(s->sd, core, log_status);
58985756a06SHans Verkuil if (!s->is_output) {
59085756a06SHans Verkuil cobalt_video_input_status_show(s);
59185756a06SHans Verkuil return 0;
59285756a06SHans Verkuil }
59385756a06SHans Verkuil
5944da70768SHans Verkuil stat = ioread32(&vo->rd_status);
59585756a06SHans Verkuil
59685756a06SHans Verkuil cobalt_info("tx: status: %s%s\n",
59785756a06SHans Verkuil (stat & M00514_RD_STATUS_BITMAP_FLOW_CTRL_NO_DATA_ERROR_MSK) ?
59885756a06SHans Verkuil "no_data " : "",
59985756a06SHans Verkuil (stat & M00514_RD_STATUS_BITMAP_READY_BUFFER_FULL_MSK) ?
60085756a06SHans Verkuil "ready_buffer_full " : "");
6014da70768SHans Verkuil cobalt_info("tx: evcnt: %d\n", ioread32(&vo->rd_evcnt_count));
60285756a06SHans Verkuil return 0;
60385756a06SHans Verkuil }
60485756a06SHans Verkuil
cobalt_enum_dv_timings(struct file * file,void * priv_fh,struct v4l2_enum_dv_timings * timings)60585756a06SHans Verkuil static int cobalt_enum_dv_timings(struct file *file, void *priv_fh,
60685756a06SHans Verkuil struct v4l2_enum_dv_timings *timings)
60785756a06SHans Verkuil {
60885756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file);
60985756a06SHans Verkuil
61085756a06SHans Verkuil if (s->input == 1) {
61185756a06SHans Verkuil if (timings->index)
61285756a06SHans Verkuil return -EINVAL;
61385756a06SHans Verkuil memset(timings->reserved, 0, sizeof(timings->reserved));
61485756a06SHans Verkuil timings->timings = cea1080p60;
61585756a06SHans Verkuil return 0;
61685756a06SHans Verkuil }
61785756a06SHans Verkuil timings->pad = 0;
61885756a06SHans Verkuil return v4l2_subdev_call(s->sd,
61985756a06SHans Verkuil pad, enum_dv_timings, timings);
62085756a06SHans Verkuil }
62185756a06SHans Verkuil
cobalt_s_dv_timings(struct file * file,void * priv_fh,struct v4l2_dv_timings * timings)62285756a06SHans Verkuil static int cobalt_s_dv_timings(struct file *file, void *priv_fh,
62385756a06SHans Verkuil struct v4l2_dv_timings *timings)
62485756a06SHans Verkuil {
62585756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file);
62685756a06SHans Verkuil int err;
62785756a06SHans Verkuil
62885756a06SHans Verkuil if (s->input == 1) {
62985756a06SHans Verkuil *timings = cea1080p60;
63085756a06SHans Verkuil return 0;
63185756a06SHans Verkuil }
632810c168cSHans Verkuil
633605a74e6SHans Verkuil if (v4l2_match_dv_timings(timings, &s->timings, 0, true))
634810c168cSHans Verkuil return 0;
635810c168cSHans Verkuil
636810c168cSHans Verkuil if (vb2_is_busy(&s->q))
637810c168cSHans Verkuil return -EBUSY;
638810c168cSHans Verkuil
63985756a06SHans Verkuil err = v4l2_subdev_call(s->sd,
64085756a06SHans Verkuil video, s_dv_timings, timings);
64185756a06SHans Verkuil if (!err) {
64285756a06SHans Verkuil s->timings = *timings;
64385756a06SHans Verkuil s->width = timings->bt.width;
64485756a06SHans Verkuil s->height = timings->bt.height;
64585756a06SHans Verkuil s->stride = timings->bt.width * s->bpp;
64685756a06SHans Verkuil }
64785756a06SHans Verkuil return err;
64885756a06SHans Verkuil }
64985756a06SHans Verkuil
cobalt_g_dv_timings(struct file * file,void * priv_fh,struct v4l2_dv_timings * timings)65085756a06SHans Verkuil static int cobalt_g_dv_timings(struct file *file, void *priv_fh,
65185756a06SHans Verkuil struct v4l2_dv_timings *timings)
65285756a06SHans Verkuil {
65385756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file);
65485756a06SHans Verkuil
65585756a06SHans Verkuil if (s->input == 1) {
65685756a06SHans Verkuil *timings = cea1080p60;
65785756a06SHans Verkuil return 0;
65885756a06SHans Verkuil }
65985756a06SHans Verkuil return v4l2_subdev_call(s->sd,
66085756a06SHans Verkuil video, g_dv_timings, timings);
66185756a06SHans Verkuil }
66285756a06SHans Verkuil
cobalt_query_dv_timings(struct file * file,void * priv_fh,struct v4l2_dv_timings * timings)66385756a06SHans Verkuil static int cobalt_query_dv_timings(struct file *file, void *priv_fh,
66485756a06SHans Verkuil struct v4l2_dv_timings *timings)
66585756a06SHans Verkuil {
66685756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file);
66785756a06SHans Verkuil
66885756a06SHans Verkuil if (s->input == 1) {
66985756a06SHans Verkuil *timings = cea1080p60;
67085756a06SHans Verkuil return 0;
67185756a06SHans Verkuil }
67285756a06SHans Verkuil return v4l2_subdev_call(s->sd,
67385756a06SHans Verkuil video, query_dv_timings, timings);
67485756a06SHans Verkuil }
67585756a06SHans Verkuil
cobalt_dv_timings_cap(struct file * file,void * priv_fh,struct v4l2_dv_timings_cap * cap)67685756a06SHans Verkuil static int cobalt_dv_timings_cap(struct file *file, void *priv_fh,
67785756a06SHans Verkuil struct v4l2_dv_timings_cap *cap)
67885756a06SHans Verkuil {
67985756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file);
68085756a06SHans Verkuil
68185756a06SHans Verkuil cap->pad = 0;
68285756a06SHans Verkuil return v4l2_subdev_call(s->sd,
68385756a06SHans Verkuil pad, dv_timings_cap, cap);
68485756a06SHans Verkuil }
68585756a06SHans Verkuil
cobalt_enum_fmt_vid_cap(struct file * file,void * priv_fh,struct v4l2_fmtdesc * f)68685756a06SHans Verkuil static int cobalt_enum_fmt_vid_cap(struct file *file, void *priv_fh,
68785756a06SHans Verkuil struct v4l2_fmtdesc *f)
68885756a06SHans Verkuil {
68985756a06SHans Verkuil switch (f->index) {
69085756a06SHans Verkuil case 0:
69185756a06SHans Verkuil f->pixelformat = V4L2_PIX_FMT_YUYV;
69285756a06SHans Verkuil break;
69385756a06SHans Verkuil case 1:
69485756a06SHans Verkuil f->pixelformat = V4L2_PIX_FMT_RGB24;
69585756a06SHans Verkuil break;
69685756a06SHans Verkuil case 2:
69785756a06SHans Verkuil f->pixelformat = V4L2_PIX_FMT_BGR32;
69885756a06SHans Verkuil break;
69985756a06SHans Verkuil default:
70085756a06SHans Verkuil return -EINVAL;
70185756a06SHans Verkuil }
70285756a06SHans Verkuil
70385756a06SHans Verkuil return 0;
70485756a06SHans Verkuil }
70585756a06SHans Verkuil
cobalt_g_fmt_vid_cap(struct file * file,void * priv_fh,struct v4l2_format * f)70685756a06SHans Verkuil static int cobalt_g_fmt_vid_cap(struct file *file, void *priv_fh,
70785756a06SHans Verkuil struct v4l2_format *f)
70885756a06SHans Verkuil {
70985756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file);
71085756a06SHans Verkuil struct v4l2_pix_format *pix = &f->fmt.pix;
71185756a06SHans Verkuil
71285756a06SHans Verkuil pix->width = s->width;
71385756a06SHans Verkuil pix->height = s->height;
71485756a06SHans Verkuil pix->bytesperline = s->stride;
71585756a06SHans Verkuil pix->field = V4L2_FIELD_NONE;
71685756a06SHans Verkuil
71785756a06SHans Verkuil if (s->input == 1) {
71885756a06SHans Verkuil pix->colorspace = V4L2_COLORSPACE_SRGB;
71985756a06SHans Verkuil } else {
720ecefa105SLaurent Pinchart struct v4l2_subdev_format sd_fmt = {
721ecefa105SLaurent Pinchart .pad = s->pad_source,
722ecefa105SLaurent Pinchart .which = V4L2_SUBDEV_FORMAT_ACTIVE,
723ecefa105SLaurent Pinchart };
724ecefa105SLaurent Pinchart
72585756a06SHans Verkuil v4l2_subdev_call(s->sd, pad, get_fmt, NULL, &sd_fmt);
72685756a06SHans Verkuil v4l2_fill_pix_format(pix, &sd_fmt.format);
72785756a06SHans Verkuil }
72885756a06SHans Verkuil
72985756a06SHans Verkuil pix->pixelformat = s->pixfmt;
73085756a06SHans Verkuil pix->sizeimage = pix->bytesperline * pix->height;
73185756a06SHans Verkuil
73285756a06SHans Verkuil return 0;
73385756a06SHans Verkuil }
73485756a06SHans Verkuil
cobalt_try_fmt_vid_cap(struct file * file,void * priv_fh,struct v4l2_format * f)73585756a06SHans Verkuil static int cobalt_try_fmt_vid_cap(struct file *file, void *priv_fh,
73685756a06SHans Verkuil struct v4l2_format *f)
73785756a06SHans Verkuil {
73885756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file);
73985756a06SHans Verkuil struct v4l2_pix_format *pix = &f->fmt.pix;
74085756a06SHans Verkuil
74185756a06SHans Verkuil /* Check for min (QCIF) and max (Full HD) size */
74285756a06SHans Verkuil if ((pix->width < 176) || (pix->height < 144)) {
74385756a06SHans Verkuil pix->width = 176;
74485756a06SHans Verkuil pix->height = 144;
74585756a06SHans Verkuil }
74685756a06SHans Verkuil
74785756a06SHans Verkuil if ((pix->width > 1920) || (pix->height > 1080)) {
74885756a06SHans Verkuil pix->width = 1920;
74985756a06SHans Verkuil pix->height = 1080;
75085756a06SHans Verkuil }
75185756a06SHans Verkuil
75285756a06SHans Verkuil /* Make width multiple of 4 */
75385756a06SHans Verkuil pix->width &= ~0x3;
75485756a06SHans Verkuil
75585756a06SHans Verkuil /* Make height multiple of 2 */
75685756a06SHans Verkuil pix->height &= ~0x1;
75785756a06SHans Verkuil
75885756a06SHans Verkuil if (s->input == 1) {
75985756a06SHans Verkuil /* Generator => fixed format only */
76085756a06SHans Verkuil pix->width = 1920;
76185756a06SHans Verkuil pix->height = 1080;
76285756a06SHans Verkuil pix->colorspace = V4L2_COLORSPACE_SRGB;
76385756a06SHans Verkuil } else {
764ecefa105SLaurent Pinchart struct v4l2_subdev_format sd_fmt = {
765ecefa105SLaurent Pinchart .pad = s->pad_source,
766ecefa105SLaurent Pinchart .which = V4L2_SUBDEV_FORMAT_ACTIVE,
767ecefa105SLaurent Pinchart };
768ecefa105SLaurent Pinchart
76985756a06SHans Verkuil v4l2_subdev_call(s->sd, pad, get_fmt, NULL, &sd_fmt);
77085756a06SHans Verkuil v4l2_fill_pix_format(pix, &sd_fmt.format);
77185756a06SHans Verkuil }
77285756a06SHans Verkuil
77385756a06SHans Verkuil switch (pix->pixelformat) {
77485756a06SHans Verkuil case V4L2_PIX_FMT_YUYV:
77585756a06SHans Verkuil default:
77685756a06SHans Verkuil pix->bytesperline = max(pix->bytesperline & ~0x3,
77785756a06SHans Verkuil pix->width * COBALT_BYTES_PER_PIXEL_YUYV);
77885756a06SHans Verkuil pix->pixelformat = V4L2_PIX_FMT_YUYV;
77985756a06SHans Verkuil break;
78085756a06SHans Verkuil case V4L2_PIX_FMT_RGB24:
78185756a06SHans Verkuil pix->bytesperline = max(pix->bytesperline & ~0x3,
78285756a06SHans Verkuil pix->width * COBALT_BYTES_PER_PIXEL_RGB24);
78385756a06SHans Verkuil break;
78485756a06SHans Verkuil case V4L2_PIX_FMT_BGR32:
78585756a06SHans Verkuil pix->bytesperline = max(pix->bytesperline & ~0x3,
78685756a06SHans Verkuil pix->width * COBALT_BYTES_PER_PIXEL_RGB32);
78785756a06SHans Verkuil break;
78885756a06SHans Verkuil }
78985756a06SHans Verkuil
79085756a06SHans Verkuil pix->sizeimage = pix->bytesperline * pix->height;
79185756a06SHans Verkuil pix->field = V4L2_FIELD_NONE;
79285756a06SHans Verkuil
79385756a06SHans Verkuil return 0;
79485756a06SHans Verkuil }
79585756a06SHans Verkuil
cobalt_s_fmt_vid_cap(struct file * file,void * priv_fh,struct v4l2_format * f)79685756a06SHans Verkuil static int cobalt_s_fmt_vid_cap(struct file *file, void *priv_fh,
79785756a06SHans Verkuil struct v4l2_format *f)
79885756a06SHans Verkuil {
79985756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file);
80085756a06SHans Verkuil struct v4l2_pix_format *pix = &f->fmt.pix;
80185756a06SHans Verkuil
80285756a06SHans Verkuil if (vb2_is_busy(&s->q))
80385756a06SHans Verkuil return -EBUSY;
80485756a06SHans Verkuil
80585756a06SHans Verkuil if (cobalt_try_fmt_vid_cap(file, priv_fh, f))
80685756a06SHans Verkuil return -EINVAL;
80785756a06SHans Verkuil
80885756a06SHans Verkuil s->width = pix->width;
80985756a06SHans Verkuil s->height = pix->height;
81085756a06SHans Verkuil s->stride = pix->bytesperline;
81185756a06SHans Verkuil switch (pix->pixelformat) {
81285756a06SHans Verkuil case V4L2_PIX_FMT_YUYV:
81385756a06SHans Verkuil s->bpp = COBALT_BYTES_PER_PIXEL_YUYV;
81485756a06SHans Verkuil break;
81585756a06SHans Verkuil case V4L2_PIX_FMT_RGB24:
81685756a06SHans Verkuil s->bpp = COBALT_BYTES_PER_PIXEL_RGB24;
81785756a06SHans Verkuil break;
81885756a06SHans Verkuil case V4L2_PIX_FMT_BGR32:
81985756a06SHans Verkuil s->bpp = COBALT_BYTES_PER_PIXEL_RGB32;
82085756a06SHans Verkuil break;
82185756a06SHans Verkuil default:
82285756a06SHans Verkuil return -EINVAL;
82385756a06SHans Verkuil }
82485756a06SHans Verkuil s->pixfmt = pix->pixelformat;
82585756a06SHans Verkuil cobalt_enable_input(s);
82685756a06SHans Verkuil
82785756a06SHans Verkuil return 0;
82885756a06SHans Verkuil }
82985756a06SHans Verkuil
cobalt_try_fmt_vid_out(struct file * file,void * priv_fh,struct v4l2_format * f)83085756a06SHans Verkuil static int cobalt_try_fmt_vid_out(struct file *file, void *priv_fh,
83185756a06SHans Verkuil struct v4l2_format *f)
83285756a06SHans Verkuil {
83385756a06SHans Verkuil struct v4l2_pix_format *pix = &f->fmt.pix;
83485756a06SHans Verkuil
83585756a06SHans Verkuil /* Check for min (QCIF) and max (Full HD) size */
83685756a06SHans Verkuil if ((pix->width < 176) || (pix->height < 144)) {
83785756a06SHans Verkuil pix->width = 176;
83885756a06SHans Verkuil pix->height = 144;
83985756a06SHans Verkuil }
84085756a06SHans Verkuil
84185756a06SHans Verkuil if ((pix->width > 1920) || (pix->height > 1080)) {
84285756a06SHans Verkuil pix->width = 1920;
84385756a06SHans Verkuil pix->height = 1080;
84485756a06SHans Verkuil }
84585756a06SHans Verkuil
84685756a06SHans Verkuil /* Make width multiple of 4 */
84785756a06SHans Verkuil pix->width &= ~0x3;
84885756a06SHans Verkuil
84985756a06SHans Verkuil /* Make height multiple of 2 */
85085756a06SHans Verkuil pix->height &= ~0x1;
85185756a06SHans Verkuil
85285756a06SHans Verkuil switch (pix->pixelformat) {
85385756a06SHans Verkuil case V4L2_PIX_FMT_YUYV:
85485756a06SHans Verkuil default:
85585756a06SHans Verkuil pix->bytesperline = max(pix->bytesperline & ~0x3,
85685756a06SHans Verkuil pix->width * COBALT_BYTES_PER_PIXEL_YUYV);
85785756a06SHans Verkuil pix->pixelformat = V4L2_PIX_FMT_YUYV;
85885756a06SHans Verkuil break;
85985756a06SHans Verkuil case V4L2_PIX_FMT_BGR32:
86085756a06SHans Verkuil pix->bytesperline = max(pix->bytesperline & ~0x3,
86185756a06SHans Verkuil pix->width * COBALT_BYTES_PER_PIXEL_RGB32);
86285756a06SHans Verkuil break;
86385756a06SHans Verkuil }
86485756a06SHans Verkuil
86585756a06SHans Verkuil pix->sizeimage = pix->bytesperline * pix->height;
86685756a06SHans Verkuil pix->field = V4L2_FIELD_NONE;
86785756a06SHans Verkuil
86885756a06SHans Verkuil return 0;
86985756a06SHans Verkuil }
87085756a06SHans Verkuil
cobalt_g_fmt_vid_out(struct file * file,void * priv_fh,struct v4l2_format * f)87185756a06SHans Verkuil static int cobalt_g_fmt_vid_out(struct file *file, void *priv_fh,
87285756a06SHans Verkuil struct v4l2_format *f)
87385756a06SHans Verkuil {
87485756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file);
87585756a06SHans Verkuil struct v4l2_pix_format *pix = &f->fmt.pix;
87685756a06SHans Verkuil
87785756a06SHans Verkuil pix->width = s->width;
87885756a06SHans Verkuil pix->height = s->height;
87985756a06SHans Verkuil pix->bytesperline = s->stride;
88085756a06SHans Verkuil pix->field = V4L2_FIELD_NONE;
88185756a06SHans Verkuil pix->pixelformat = s->pixfmt;
88285756a06SHans Verkuil pix->colorspace = s->colorspace;
883136a5e9eSHans Verkuil pix->xfer_func = s->xfer_func;
88485756a06SHans Verkuil pix->ycbcr_enc = s->ycbcr_enc;
88585756a06SHans Verkuil pix->quantization = s->quantization;
88685756a06SHans Verkuil pix->sizeimage = pix->bytesperline * pix->height;
88785756a06SHans Verkuil
88885756a06SHans Verkuil return 0;
88985756a06SHans Verkuil }
89085756a06SHans Verkuil
cobalt_enum_fmt_vid_out(struct file * file,void * priv_fh,struct v4l2_fmtdesc * f)89185756a06SHans Verkuil static int cobalt_enum_fmt_vid_out(struct file *file, void *priv_fh,
89285756a06SHans Verkuil struct v4l2_fmtdesc *f)
89385756a06SHans Verkuil {
89485756a06SHans Verkuil switch (f->index) {
89585756a06SHans Verkuil case 0:
89685756a06SHans Verkuil f->pixelformat = V4L2_PIX_FMT_YUYV;
89785756a06SHans Verkuil break;
89885756a06SHans Verkuil case 1:
89985756a06SHans Verkuil f->pixelformat = V4L2_PIX_FMT_BGR32;
90085756a06SHans Verkuil break;
90185756a06SHans Verkuil default:
90285756a06SHans Verkuil return -EINVAL;
90385756a06SHans Verkuil }
90485756a06SHans Verkuil
90585756a06SHans Verkuil return 0;
90685756a06SHans Verkuil }
90785756a06SHans Verkuil
cobalt_s_fmt_vid_out(struct file * file,void * priv_fh,struct v4l2_format * f)90885756a06SHans Verkuil static int cobalt_s_fmt_vid_out(struct file *file, void *priv_fh,
90985756a06SHans Verkuil struct v4l2_format *f)
91085756a06SHans Verkuil {
91185756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file);
91285756a06SHans Verkuil struct v4l2_pix_format *pix = &f->fmt.pix;
913*e18a7e9aSLaurent Pinchart struct v4l2_subdev_format sd_fmt = {
914*e18a7e9aSLaurent Pinchart .which = V4L2_SUBDEV_FORMAT_ACTIVE,
915*e18a7e9aSLaurent Pinchart };
916e1dedcffSHans Verkuil u32 code;
91785756a06SHans Verkuil
91885756a06SHans Verkuil if (cobalt_try_fmt_vid_out(file, priv_fh, f))
91985756a06SHans Verkuil return -EINVAL;
92085756a06SHans Verkuil
92185756a06SHans Verkuil if (vb2_is_busy(&s->q) && (pix->pixelformat != s->pixfmt ||
92285756a06SHans Verkuil pix->width != s->width || pix->height != s->height ||
92385756a06SHans Verkuil pix->bytesperline != s->stride))
92485756a06SHans Verkuil return -EBUSY;
92585756a06SHans Verkuil
92685756a06SHans Verkuil switch (pix->pixelformat) {
92785756a06SHans Verkuil case V4L2_PIX_FMT_YUYV:
92885756a06SHans Verkuil s->bpp = COBALT_BYTES_PER_PIXEL_YUYV;
929e1dedcffSHans Verkuil code = MEDIA_BUS_FMT_UYVY8_1X16;
93085756a06SHans Verkuil break;
93185756a06SHans Verkuil case V4L2_PIX_FMT_BGR32:
93285756a06SHans Verkuil s->bpp = COBALT_BYTES_PER_PIXEL_RGB32;
933e1dedcffSHans Verkuil code = MEDIA_BUS_FMT_RGB888_1X24;
93485756a06SHans Verkuil break;
93585756a06SHans Verkuil default:
93685756a06SHans Verkuil return -EINVAL;
93785756a06SHans Verkuil }
93885756a06SHans Verkuil s->width = pix->width;
93985756a06SHans Verkuil s->height = pix->height;
94085756a06SHans Verkuil s->stride = pix->bytesperline;
94185756a06SHans Verkuil s->pixfmt = pix->pixelformat;
94285756a06SHans Verkuil s->colorspace = pix->colorspace;
943136a5e9eSHans Verkuil s->xfer_func = pix->xfer_func;
94485756a06SHans Verkuil s->ycbcr_enc = pix->ycbcr_enc;
94585756a06SHans Verkuil s->quantization = pix->quantization;
946e1dedcffSHans Verkuil v4l2_fill_mbus_format(&sd_fmt.format, pix, code);
94785756a06SHans Verkuil v4l2_subdev_call(s->sd, pad, set_fmt, NULL, &sd_fmt);
94885756a06SHans Verkuil return 0;
94985756a06SHans Verkuil }
95085756a06SHans Verkuil
cobalt_enum_input(struct file * file,void * priv_fh,struct v4l2_input * inp)95185756a06SHans Verkuil static int cobalt_enum_input(struct file *file, void *priv_fh,
95285756a06SHans Verkuil struct v4l2_input *inp)
95385756a06SHans Verkuil {
95485756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file);
95585756a06SHans Verkuil
95685756a06SHans Verkuil if (inp->index > 1)
95785756a06SHans Verkuil return -EINVAL;
95885756a06SHans Verkuil if (inp->index == 0)
95985756a06SHans Verkuil snprintf(inp->name, sizeof(inp->name),
96085756a06SHans Verkuil "HDMI-%d", s->video_channel);
96185756a06SHans Verkuil else
96285756a06SHans Verkuil snprintf(inp->name, sizeof(inp->name),
96385756a06SHans Verkuil "Generator-%d", s->video_channel);
96485756a06SHans Verkuil inp->type = V4L2_INPUT_TYPE_CAMERA;
96585756a06SHans Verkuil inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
96685756a06SHans Verkuil if (inp->index == 1)
96785756a06SHans Verkuil return 0;
96885756a06SHans Verkuil return v4l2_subdev_call(s->sd,
96985756a06SHans Verkuil video, g_input_status, &inp->status);
97085756a06SHans Verkuil }
97185756a06SHans Verkuil
cobalt_g_input(struct file * file,void * priv_fh,unsigned int * i)97285756a06SHans Verkuil static int cobalt_g_input(struct file *file, void *priv_fh, unsigned int *i)
97385756a06SHans Verkuil {
97485756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file);
97585756a06SHans Verkuil
97685756a06SHans Verkuil *i = s->input;
97785756a06SHans Verkuil return 0;
97885756a06SHans Verkuil }
97985756a06SHans Verkuil
cobalt_s_input(struct file * file,void * priv_fh,unsigned int i)98085756a06SHans Verkuil static int cobalt_s_input(struct file *file, void *priv_fh, unsigned int i)
98185756a06SHans Verkuil {
98285756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file);
98385756a06SHans Verkuil
98485756a06SHans Verkuil if (i >= 2)
98585756a06SHans Verkuil return -EINVAL;
98685756a06SHans Verkuil if (vb2_is_busy(&s->q))
98785756a06SHans Verkuil return -EBUSY;
98885756a06SHans Verkuil s->input = i;
98985756a06SHans Verkuil
99085756a06SHans Verkuil cobalt_enable_input(s);
99185756a06SHans Verkuil
99285756a06SHans Verkuil if (s->input == 1) /* Test Pattern Generator */
99385756a06SHans Verkuil return 0;
99485756a06SHans Verkuil
99585756a06SHans Verkuil return v4l2_subdev_call(s->sd, video, s_routing,
99685756a06SHans Verkuil ADV76XX_PAD_HDMI_PORT_A, 0, 0);
99785756a06SHans Verkuil }
99885756a06SHans Verkuil
cobalt_enum_output(struct file * file,void * priv_fh,struct v4l2_output * out)99985756a06SHans Verkuil static int cobalt_enum_output(struct file *file, void *priv_fh,
100085756a06SHans Verkuil struct v4l2_output *out)
100185756a06SHans Verkuil {
100285756a06SHans Verkuil if (out->index)
100385756a06SHans Verkuil return -EINVAL;
100485756a06SHans Verkuil snprintf(out->name, sizeof(out->name), "HDMI-%d", out->index);
100585756a06SHans Verkuil out->type = V4L2_OUTPUT_TYPE_ANALOG;
100685756a06SHans Verkuil out->capabilities = V4L2_OUT_CAP_DV_TIMINGS;
100785756a06SHans Verkuil return 0;
100885756a06SHans Verkuil }
100985756a06SHans Verkuil
cobalt_g_output(struct file * file,void * priv_fh,unsigned int * i)101085756a06SHans Verkuil static int cobalt_g_output(struct file *file, void *priv_fh, unsigned int *i)
101185756a06SHans Verkuil {
101285756a06SHans Verkuil *i = 0;
101385756a06SHans Verkuil return 0;
101485756a06SHans Verkuil }
101585756a06SHans Verkuil
cobalt_s_output(struct file * file,void * priv_fh,unsigned int i)101685756a06SHans Verkuil static int cobalt_s_output(struct file *file, void *priv_fh, unsigned int i)
101785756a06SHans Verkuil {
101885756a06SHans Verkuil return i ? -EINVAL : 0;
101985756a06SHans Verkuil }
102085756a06SHans Verkuil
cobalt_g_edid(struct file * file,void * fh,struct v4l2_edid * edid)102185756a06SHans Verkuil static int cobalt_g_edid(struct file *file, void *fh, struct v4l2_edid *edid)
102285756a06SHans Verkuil {
102385756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file);
102485756a06SHans Verkuil u32 pad = edid->pad;
102585756a06SHans Verkuil int ret;
102685756a06SHans Verkuil
102785756a06SHans Verkuil if (edid->pad >= (s->is_output ? 1 : 2))
102885756a06SHans Verkuil return -EINVAL;
102985756a06SHans Verkuil edid->pad = 0;
103085756a06SHans Verkuil ret = v4l2_subdev_call(s->sd, pad, get_edid, edid);
103185756a06SHans Verkuil edid->pad = pad;
103285756a06SHans Verkuil return ret;
103385756a06SHans Verkuil }
103485756a06SHans Verkuil
cobalt_s_edid(struct file * file,void * fh,struct v4l2_edid * edid)103585756a06SHans Verkuil static int cobalt_s_edid(struct file *file, void *fh, struct v4l2_edid *edid)
103685756a06SHans Verkuil {
103785756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file);
103885756a06SHans Verkuil u32 pad = edid->pad;
103985756a06SHans Verkuil int ret;
104085756a06SHans Verkuil
104185756a06SHans Verkuil if (edid->pad >= 2)
104285756a06SHans Verkuil return -EINVAL;
104385756a06SHans Verkuil edid->pad = 0;
104485756a06SHans Verkuil ret = v4l2_subdev_call(s->sd, pad, set_edid, edid);
104585756a06SHans Verkuil edid->pad = pad;
104685756a06SHans Verkuil return ret;
104785756a06SHans Verkuil }
104885756a06SHans Verkuil
cobalt_subscribe_event(struct v4l2_fh * fh,const struct v4l2_event_subscription * sub)104985756a06SHans Verkuil static int cobalt_subscribe_event(struct v4l2_fh *fh,
105085756a06SHans Verkuil const struct v4l2_event_subscription *sub)
105185756a06SHans Verkuil {
105285756a06SHans Verkuil switch (sub->type) {
105385756a06SHans Verkuil case V4L2_EVENT_SOURCE_CHANGE:
105485756a06SHans Verkuil return v4l2_event_subscribe(fh, sub, 4, NULL);
105585756a06SHans Verkuil }
105685756a06SHans Verkuil return v4l2_ctrl_subscribe_event(fh, sub);
105785756a06SHans Verkuil }
105885756a06SHans Verkuil
cobalt_g_parm(struct file * file,void * fh,struct v4l2_streamparm * a)105985756a06SHans Verkuil static int cobalt_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
106085756a06SHans Verkuil {
10613ae17c66SJose Abreu struct cobalt_stream *s = video_drvdata(file);
10623ae17c66SJose Abreu struct v4l2_fract fps;
10633ae17c66SJose Abreu
106485756a06SHans Verkuil if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
106585756a06SHans Verkuil return -EINVAL;
10663ae17c66SJose Abreu
10673ae17c66SJose Abreu fps = v4l2_calc_timeperframe(&s->timings);
10683ae17c66SJose Abreu a->parm.capture.timeperframe.numerator = fps.numerator;
10693ae17c66SJose Abreu a->parm.capture.timeperframe.denominator = fps.denominator;
107085756a06SHans Verkuil a->parm.capture.readbuffers = 3;
107185756a06SHans Verkuil return 0;
107285756a06SHans Verkuil }
107385756a06SHans Verkuil
cobalt_g_pixelaspect(struct file * file,void * fh,int type,struct v4l2_fract * f)10745200ab6aSHans Verkuil static int cobalt_g_pixelaspect(struct file *file, void *fh,
10755200ab6aSHans Verkuil int type, struct v4l2_fract *f)
1076d736e348SHans Verkuil {
1077d736e348SHans Verkuil struct cobalt_stream *s = video_drvdata(file);
1078d736e348SHans Verkuil struct v4l2_dv_timings timings;
1079d736e348SHans Verkuil int err = 0;
1080d736e348SHans Verkuil
10815200ab6aSHans Verkuil if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1082d736e348SHans Verkuil return -EINVAL;
10835200ab6aSHans Verkuil
1084d736e348SHans Verkuil if (s->input == 1)
1085d736e348SHans Verkuil timings = cea1080p60;
1086d736e348SHans Verkuil else
1087d736e348SHans Verkuil err = v4l2_subdev_call(s->sd, video, g_dv_timings, &timings);
1088ee10dc36SHans Verkuil if (!err)
10895200ab6aSHans Verkuil *f = v4l2_dv_timings_aspect_ratio(&timings);
1090d736e348SHans Verkuil return err;
1091d736e348SHans Verkuil }
1092d736e348SHans Verkuil
cobalt_g_selection(struct file * file,void * fh,struct v4l2_selection * sel)1093ee10dc36SHans Verkuil static int cobalt_g_selection(struct file *file, void *fh,
1094ee10dc36SHans Verkuil struct v4l2_selection *sel)
1095ee10dc36SHans Verkuil {
1096ee10dc36SHans Verkuil struct cobalt_stream *s = video_drvdata(file);
1097ee10dc36SHans Verkuil struct v4l2_dv_timings timings;
1098ee10dc36SHans Verkuil int err = 0;
1099ee10dc36SHans Verkuil
1100ee10dc36SHans Verkuil if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1101ee10dc36SHans Verkuil return -EINVAL;
1102ee10dc36SHans Verkuil
1103ee10dc36SHans Verkuil if (s->input == 1)
1104ee10dc36SHans Verkuil timings = cea1080p60;
1105ee10dc36SHans Verkuil else
1106ee10dc36SHans Verkuil err = v4l2_subdev_call(s->sd, video, g_dv_timings, &timings);
1107ee10dc36SHans Verkuil
1108ee10dc36SHans Verkuil if (err)
1109ee10dc36SHans Verkuil return err;
1110ee10dc36SHans Verkuil
1111ee10dc36SHans Verkuil switch (sel->target) {
1112ee10dc36SHans Verkuil case V4L2_SEL_TGT_CROP_BOUNDS:
1113ee10dc36SHans Verkuil case V4L2_SEL_TGT_CROP_DEFAULT:
1114ee10dc36SHans Verkuil sel->r.top = 0;
1115ee10dc36SHans Verkuil sel->r.left = 0;
1116ee10dc36SHans Verkuil sel->r.width = timings.bt.width;
1117ee10dc36SHans Verkuil sel->r.height = timings.bt.height;
1118ee10dc36SHans Verkuil break;
1119ee10dc36SHans Verkuil default:
1120ee10dc36SHans Verkuil return -EINVAL;
1121ee10dc36SHans Verkuil }
1122ee10dc36SHans Verkuil return 0;
1123ee10dc36SHans Verkuil }
1124ee10dc36SHans Verkuil
112585756a06SHans Verkuil static const struct v4l2_ioctl_ops cobalt_ioctl_ops = {
112685756a06SHans Verkuil .vidioc_querycap = cobalt_querycap,
112785756a06SHans Verkuil .vidioc_g_parm = cobalt_g_parm,
112885756a06SHans Verkuil .vidioc_log_status = cobalt_log_status,
112985756a06SHans Verkuil .vidioc_streamon = vb2_ioctl_streamon,
113085756a06SHans Verkuil .vidioc_streamoff = vb2_ioctl_streamoff,
11315200ab6aSHans Verkuil .vidioc_g_pixelaspect = cobalt_g_pixelaspect,
1132ee10dc36SHans Verkuil .vidioc_g_selection = cobalt_g_selection,
113385756a06SHans Verkuil .vidioc_enum_input = cobalt_enum_input,
113485756a06SHans Verkuil .vidioc_g_input = cobalt_g_input,
113585756a06SHans Verkuil .vidioc_s_input = cobalt_s_input,
113685756a06SHans Verkuil .vidioc_enum_fmt_vid_cap = cobalt_enum_fmt_vid_cap,
113785756a06SHans Verkuil .vidioc_g_fmt_vid_cap = cobalt_g_fmt_vid_cap,
113885756a06SHans Verkuil .vidioc_s_fmt_vid_cap = cobalt_s_fmt_vid_cap,
113985756a06SHans Verkuil .vidioc_try_fmt_vid_cap = cobalt_try_fmt_vid_cap,
114085756a06SHans Verkuil .vidioc_enum_output = cobalt_enum_output,
114185756a06SHans Verkuil .vidioc_g_output = cobalt_g_output,
114285756a06SHans Verkuil .vidioc_s_output = cobalt_s_output,
114385756a06SHans Verkuil .vidioc_enum_fmt_vid_out = cobalt_enum_fmt_vid_out,
114485756a06SHans Verkuil .vidioc_g_fmt_vid_out = cobalt_g_fmt_vid_out,
114585756a06SHans Verkuil .vidioc_s_fmt_vid_out = cobalt_s_fmt_vid_out,
114685756a06SHans Verkuil .vidioc_try_fmt_vid_out = cobalt_try_fmt_vid_out,
114785756a06SHans Verkuil .vidioc_s_dv_timings = cobalt_s_dv_timings,
114885756a06SHans Verkuil .vidioc_g_dv_timings = cobalt_g_dv_timings,
114985756a06SHans Verkuil .vidioc_query_dv_timings = cobalt_query_dv_timings,
115085756a06SHans Verkuil .vidioc_enum_dv_timings = cobalt_enum_dv_timings,
115185756a06SHans Verkuil .vidioc_dv_timings_cap = cobalt_dv_timings_cap,
115285756a06SHans Verkuil .vidioc_g_edid = cobalt_g_edid,
115385756a06SHans Verkuil .vidioc_s_edid = cobalt_s_edid,
115485756a06SHans Verkuil .vidioc_subscribe_event = cobalt_subscribe_event,
115585756a06SHans Verkuil .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
115685756a06SHans Verkuil .vidioc_reqbufs = vb2_ioctl_reqbufs,
115785756a06SHans Verkuil .vidioc_create_bufs = vb2_ioctl_create_bufs,
115885756a06SHans Verkuil .vidioc_querybuf = vb2_ioctl_querybuf,
115985756a06SHans Verkuil .vidioc_qbuf = vb2_ioctl_qbuf,
116085756a06SHans Verkuil .vidioc_dqbuf = vb2_ioctl_dqbuf,
116185756a06SHans Verkuil .vidioc_expbuf = vb2_ioctl_expbuf,
116285756a06SHans Verkuil #ifdef CONFIG_VIDEO_ADV_DEBUG
116385756a06SHans Verkuil .vidioc_g_register = cobalt_g_register,
116485756a06SHans Verkuil .vidioc_s_register = cobalt_s_register,
116585756a06SHans Verkuil #endif
116685756a06SHans Verkuil };
116785756a06SHans Verkuil
116885756a06SHans Verkuil static const struct v4l2_ioctl_ops cobalt_ioctl_empty_ops = {
116985756a06SHans Verkuil #ifdef CONFIG_VIDEO_ADV_DEBUG
117085756a06SHans Verkuil .vidioc_g_register = cobalt_g_register,
117185756a06SHans Verkuil .vidioc_s_register = cobalt_s_register,
117285756a06SHans Verkuil #endif
117385756a06SHans Verkuil };
117485756a06SHans Verkuil
117585756a06SHans Verkuil /* Register device nodes */
117685756a06SHans Verkuil
117785756a06SHans Verkuil static const struct v4l2_file_operations cobalt_fops = {
117885756a06SHans Verkuil .owner = THIS_MODULE,
117985756a06SHans Verkuil .open = v4l2_fh_open,
118085756a06SHans Verkuil .unlocked_ioctl = video_ioctl2,
118185756a06SHans Verkuil .release = vb2_fop_release,
118285756a06SHans Verkuil .poll = vb2_fop_poll,
118385756a06SHans Verkuil .mmap = vb2_fop_mmap,
118485756a06SHans Verkuil .read = vb2_fop_read,
118585756a06SHans Verkuil };
118685756a06SHans Verkuil
118785756a06SHans Verkuil static const struct v4l2_file_operations cobalt_out_fops = {
118885756a06SHans Verkuil .owner = THIS_MODULE,
118985756a06SHans Verkuil .open = v4l2_fh_open,
119085756a06SHans Verkuil .unlocked_ioctl = video_ioctl2,
119185756a06SHans Verkuil .release = vb2_fop_release,
119285756a06SHans Verkuil .poll = vb2_fop_poll,
119385756a06SHans Verkuil .mmap = vb2_fop_mmap,
119485756a06SHans Verkuil .write = vb2_fop_write,
119585756a06SHans Verkuil };
119685756a06SHans Verkuil
119785756a06SHans Verkuil static const struct v4l2_file_operations cobalt_empty_fops = {
119885756a06SHans Verkuil .owner = THIS_MODULE,
119985756a06SHans Verkuil .open = v4l2_fh_open,
120085756a06SHans Verkuil .unlocked_ioctl = video_ioctl2,
120185756a06SHans Verkuil .release = v4l2_fh_release,
120285756a06SHans Verkuil };
120385756a06SHans Verkuil
cobalt_node_register(struct cobalt * cobalt,int node)120485756a06SHans Verkuil static int cobalt_node_register(struct cobalt *cobalt, int node)
120585756a06SHans Verkuil {
120685756a06SHans Verkuil static const struct v4l2_dv_timings dv1080p60 =
120785756a06SHans Verkuil V4L2_DV_BT_CEA_1920X1080P60;
120885756a06SHans Verkuil struct cobalt_stream *s = cobalt->streams + node;
120985756a06SHans Verkuil struct video_device *vdev = &s->vdev;
121085756a06SHans Verkuil struct vb2_queue *q = &s->q;
121185756a06SHans Verkuil int ret;
121285756a06SHans Verkuil
121385756a06SHans Verkuil mutex_init(&s->lock);
121485756a06SHans Verkuil spin_lock_init(&s->irqlock);
121585756a06SHans Verkuil
121685756a06SHans Verkuil snprintf(vdev->name, sizeof(vdev->name),
121785756a06SHans Verkuil "%s-%d", cobalt->v4l2_dev.name, node);
121885756a06SHans Verkuil s->width = 1920;
121985756a06SHans Verkuil /* Audio frames are just 4 lines of 1920 bytes */
122085756a06SHans Verkuil s->height = s->is_audio ? 4 : 1080;
122185756a06SHans Verkuil
122285756a06SHans Verkuil if (s->is_audio) {
122385756a06SHans Verkuil s->bpp = 1;
122485756a06SHans Verkuil s->pixfmt = V4L2_PIX_FMT_GREY;
122585756a06SHans Verkuil } else if (s->is_output) {
122685756a06SHans Verkuil s->bpp = COBALT_BYTES_PER_PIXEL_RGB32;
122785756a06SHans Verkuil s->pixfmt = V4L2_PIX_FMT_BGR32;
122885756a06SHans Verkuil } else {
122985756a06SHans Verkuil s->bpp = COBALT_BYTES_PER_PIXEL_YUYV;
123085756a06SHans Verkuil s->pixfmt = V4L2_PIX_FMT_YUYV;
123185756a06SHans Verkuil }
123285756a06SHans Verkuil s->colorspace = V4L2_COLORSPACE_SRGB;
123385756a06SHans Verkuil s->stride = s->width * s->bpp;
123485756a06SHans Verkuil
123585756a06SHans Verkuil if (!s->is_audio) {
123685756a06SHans Verkuil if (s->is_dummy)
123785756a06SHans Verkuil cobalt_warn("Setting up dummy video node %d\n", node);
123885756a06SHans Verkuil vdev->v4l2_dev = &cobalt->v4l2_dev;
123985756a06SHans Verkuil if (s->is_dummy)
124085756a06SHans Verkuil vdev->fops = &cobalt_empty_fops;
124185756a06SHans Verkuil else
124285756a06SHans Verkuil vdev->fops = s->is_output ? &cobalt_out_fops :
124385756a06SHans Verkuil &cobalt_fops;
124485756a06SHans Verkuil vdev->release = video_device_release_empty;
124585756a06SHans Verkuil vdev->vfl_dir = s->is_output ? VFL_DIR_TX : VFL_DIR_RX;
124685756a06SHans Verkuil vdev->lock = &s->lock;
124785756a06SHans Verkuil if (s->sd)
124885756a06SHans Verkuil vdev->ctrl_handler = s->sd->ctrl_handler;
124985756a06SHans Verkuil s->timings = dv1080p60;
125085756a06SHans Verkuil v4l2_subdev_call(s->sd, video, s_dv_timings, &s->timings);
125185756a06SHans Verkuil if (!s->is_output && s->sd)
125285756a06SHans Verkuil cobalt_enable_input(s);
125385756a06SHans Verkuil vdev->ioctl_ops = s->is_dummy ? &cobalt_ioctl_empty_ops :
125485756a06SHans Verkuil &cobalt_ioctl_ops;
125585756a06SHans Verkuil }
125685756a06SHans Verkuil
125785756a06SHans Verkuil INIT_LIST_HEAD(&s->bufs);
125885756a06SHans Verkuil q->type = s->is_output ? V4L2_BUF_TYPE_VIDEO_OUTPUT :
125985756a06SHans Verkuil V4L2_BUF_TYPE_VIDEO_CAPTURE;
126085756a06SHans Verkuil q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
126185756a06SHans Verkuil q->io_modes |= s->is_output ? VB2_WRITE : VB2_READ;
126285756a06SHans Verkuil q->drv_priv = s;
126385756a06SHans Verkuil q->buf_struct_size = sizeof(struct cobalt_buffer);
126485756a06SHans Verkuil q->ops = &cobalt_qops;
126585756a06SHans Verkuil q->mem_ops = &vb2_dma_sg_memops;
126685756a06SHans Verkuil q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
126785756a06SHans Verkuil q->min_buffers_needed = 2;
126885756a06SHans Verkuil q->lock = &s->lock;
12692bc46b3aSHans Verkuil q->dev = &cobalt->pci_dev->dev;
127085756a06SHans Verkuil vdev->queue = q;
127121615365SHans Verkuil vdev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
127221615365SHans Verkuil if (s->is_output)
127321615365SHans Verkuil vdev->device_caps |= V4L2_CAP_VIDEO_OUTPUT;
127421615365SHans Verkuil else
127521615365SHans Verkuil vdev->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
127685756a06SHans Verkuil
127785756a06SHans Verkuil video_set_drvdata(vdev, s);
127885756a06SHans Verkuil ret = vb2_queue_init(q);
127985756a06SHans Verkuil if (!s->is_audio && ret == 0)
12803e30a927SHans Verkuil ret = video_register_device(vdev, VFL_TYPE_VIDEO, -1);
128185756a06SHans Verkuil else if (!s->is_dummy)
128285756a06SHans Verkuil ret = cobalt_alsa_init(s);
128385756a06SHans Verkuil
128485756a06SHans Verkuil if (ret < 0) {
128585756a06SHans Verkuil if (!s->is_audio)
128685756a06SHans Verkuil cobalt_err("couldn't register v4l2 device node %d\n",
128785756a06SHans Verkuil node);
128885756a06SHans Verkuil return ret;
128985756a06SHans Verkuil }
129085756a06SHans Verkuil cobalt_info("registered node %d\n", node);
129185756a06SHans Verkuil return 0;
129285756a06SHans Verkuil }
129385756a06SHans Verkuil
129485756a06SHans Verkuil /* Initialize v4l2 variables and register v4l2 devices */
cobalt_nodes_register(struct cobalt * cobalt)129585756a06SHans Verkuil int cobalt_nodes_register(struct cobalt *cobalt)
129685756a06SHans Verkuil {
129785756a06SHans Verkuil int node, ret;
129885756a06SHans Verkuil
129985756a06SHans Verkuil /* Setup V4L2 Devices */
130085756a06SHans Verkuil for (node = 0; node < COBALT_NUM_STREAMS; node++) {
130185756a06SHans Verkuil ret = cobalt_node_register(cobalt, node);
130285756a06SHans Verkuil if (ret)
130385756a06SHans Verkuil return ret;
130485756a06SHans Verkuil }
130585756a06SHans Verkuil return 0;
130685756a06SHans Verkuil }
130785756a06SHans Verkuil
130885756a06SHans Verkuil /* Unregister v4l2 devices */
cobalt_nodes_unregister(struct cobalt * cobalt)130985756a06SHans Verkuil void cobalt_nodes_unregister(struct cobalt *cobalt)
131085756a06SHans Verkuil {
131185756a06SHans Verkuil int node;
131285756a06SHans Verkuil
131385756a06SHans Verkuil /* Teardown all streams */
131485756a06SHans Verkuil for (node = 0; node < COBALT_NUM_STREAMS; node++) {
131585756a06SHans Verkuil struct cobalt_stream *s = cobalt->streams + node;
131685756a06SHans Verkuil struct video_device *vdev = &s->vdev;
131785756a06SHans Verkuil
131885756a06SHans Verkuil if (!s->is_audio)
131985756a06SHans Verkuil video_unregister_device(vdev);
132085756a06SHans Verkuil else if (!s->is_dummy)
132185756a06SHans Verkuil cobalt_alsa_exit(s);
132285756a06SHans Verkuil }
132385756a06SHans Verkuil }
1324