xref: /openbmc/linux/drivers/media/pci/cobalt/cobalt-v4l2.c (revision 136a5e9e01ed89d8891845027b3ee731bb662926)
185756a06SHans Verkuil /*
285756a06SHans Verkuil  *  cobalt V4L2 API
385756a06SHans Verkuil  *
485756a06SHans Verkuil  *  Derived from ivtv-ioctl.c and cx18-fileops.c
585756a06SHans Verkuil  *
685756a06SHans Verkuil  *  Copyright 2012-2015 Cisco Systems, Inc. and/or its affiliates.
785756a06SHans Verkuil  *  All rights reserved.
885756a06SHans Verkuil  *
985756a06SHans Verkuil  *  This program is free software; you may redistribute it and/or modify
1085756a06SHans Verkuil  *  it under the terms of the GNU General Public License as published by
1185756a06SHans Verkuil  *  the Free Software Foundation; version 2 of the License.
1285756a06SHans Verkuil  *
1385756a06SHans Verkuil  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1485756a06SHans Verkuil  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1585756a06SHans Verkuil  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1685756a06SHans Verkuil  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
1785756a06SHans Verkuil  *  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
1885756a06SHans Verkuil  *  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1985756a06SHans Verkuil  *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2085756a06SHans Verkuil  *  SOFTWARE.
2185756a06SHans Verkuil  */
2285756a06SHans Verkuil 
2385756a06SHans Verkuil #include <linux/dma-mapping.h>
2485756a06SHans Verkuil #include <linux/delay.h>
2599cabb18SHans Verkuil #include <linux/math64.h>
2685756a06SHans Verkuil #include <linux/pci.h>
2785756a06SHans Verkuil #include <linux/v4l2-dv-timings.h>
2885756a06SHans Verkuil 
2985756a06SHans Verkuil #include <media/v4l2-ctrls.h>
3085756a06SHans Verkuil #include <media/v4l2-event.h>
3185756a06SHans Verkuil #include <media/adv7604.h>
3285756a06SHans Verkuil #include <media/adv7842.h>
3385756a06SHans Verkuil 
3485756a06SHans Verkuil #include "cobalt-alsa.h"
3585756a06SHans Verkuil #include "cobalt-cpld.h"
3685756a06SHans Verkuil #include "cobalt-driver.h"
3785756a06SHans Verkuil #include "cobalt-v4l2.h"
3885756a06SHans Verkuil #include "cobalt-irq.h"
3985756a06SHans Verkuil #include "cobalt-omnitek.h"
4085756a06SHans Verkuil 
4185756a06SHans Verkuil static const struct v4l2_dv_timings cea1080p60 = V4L2_DV_BT_CEA_1920X1080P60;
4285756a06SHans Verkuil 
4385756a06SHans Verkuil /* vb2 DMA streaming ops */
4485756a06SHans Verkuil 
4585756a06SHans Verkuil static int cobalt_queue_setup(struct vb2_queue *q,
4685756a06SHans Verkuil 			const struct v4l2_format *fmt,
4785756a06SHans Verkuil 			unsigned int *num_buffers, unsigned int *num_planes,
4885756a06SHans Verkuil 			unsigned int sizes[], void *alloc_ctxs[])
4985756a06SHans Verkuil {
5085756a06SHans Verkuil 	struct cobalt_stream *s = q->drv_priv;
5185756a06SHans Verkuil 	unsigned size = s->stride * s->height;
5285756a06SHans Verkuil 
5385756a06SHans Verkuil 	if (*num_buffers < 3)
5485756a06SHans Verkuil 		*num_buffers = 3;
5585756a06SHans Verkuil 	if (*num_buffers > NR_BUFS)
5685756a06SHans Verkuil 		*num_buffers = NR_BUFS;
5785756a06SHans Verkuil 	*num_planes = 1;
5885756a06SHans Verkuil 	if (fmt) {
5985756a06SHans Verkuil 		if (fmt->fmt.pix.sizeimage < size)
6085756a06SHans Verkuil 			return -EINVAL;
6185756a06SHans Verkuil 		size = fmt->fmt.pix.sizeimage;
6285756a06SHans Verkuil 	}
6385756a06SHans Verkuil 	sizes[0] = size;
6485756a06SHans Verkuil 	alloc_ctxs[0] = s->cobalt->alloc_ctx;
6585756a06SHans Verkuil 	return 0;
6685756a06SHans Verkuil }
6785756a06SHans Verkuil 
6885756a06SHans Verkuil static int cobalt_buf_init(struct vb2_buffer *vb)
6985756a06SHans Verkuil {
7085756a06SHans Verkuil 	struct cobalt_stream *s = vb->vb2_queue->drv_priv;
7185756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
7285756a06SHans Verkuil 	const size_t max_pages_per_line =
7385756a06SHans Verkuil 		(COBALT_MAX_WIDTH * COBALT_MAX_BPP) / PAGE_SIZE + 2;
7485756a06SHans Verkuil 	const size_t bytes =
7585756a06SHans Verkuil 		COBALT_MAX_HEIGHT * max_pages_per_line * 0x20;
7685756a06SHans Verkuil 	const size_t audio_bytes = ((1920 * 4) / PAGE_SIZE + 1) * 0x20;
7785756a06SHans Verkuil 	struct sg_dma_desc_info *desc = &s->dma_desc_info[vb->v4l2_buf.index];
7885756a06SHans Verkuil 	struct sg_table *sg_desc = vb2_dma_sg_plane_desc(vb, 0);
7985756a06SHans Verkuil 	unsigned size;
8085756a06SHans Verkuil 	int ret;
8185756a06SHans Verkuil 
8285756a06SHans Verkuil 	size = s->stride * s->height;
8385756a06SHans Verkuil 	if (vb2_plane_size(vb, 0) < size) {
8485756a06SHans Verkuil 		cobalt_info("data will not fit into plane (%lu < %u)\n",
8585756a06SHans Verkuil 					vb2_plane_size(vb, 0), size);
8685756a06SHans Verkuil 		return -EINVAL;
8785756a06SHans Verkuil 	}
8885756a06SHans Verkuil 
8985756a06SHans Verkuil 	if (desc->virt == NULL) {
9085756a06SHans Verkuil 		desc->dev = &cobalt->pci_dev->dev;
9185756a06SHans Verkuil 		descriptor_list_allocate(desc,
9285756a06SHans Verkuil 			s->is_audio ? audio_bytes : bytes);
9385756a06SHans Verkuil 		if (desc->virt == NULL)
9485756a06SHans Verkuil 			return -ENOMEM;
9585756a06SHans Verkuil 	}
9685756a06SHans Verkuil 	ret = descriptor_list_create(cobalt, sg_desc->sgl,
9785756a06SHans Verkuil 			!s->is_output, sg_desc->nents, size,
9885756a06SHans Verkuil 			s->width * s->bpp, s->stride, desc);
9985756a06SHans Verkuil 	if (ret)
10085756a06SHans Verkuil 		descriptor_list_free(desc);
10185756a06SHans Verkuil 	return ret;
10285756a06SHans Verkuil }
10385756a06SHans Verkuil 
10485756a06SHans Verkuil static void cobalt_buf_cleanup(struct vb2_buffer *vb)
10585756a06SHans Verkuil {
10685756a06SHans Verkuil 	struct cobalt_stream *s = vb->vb2_queue->drv_priv;
10785756a06SHans Verkuil 	struct sg_dma_desc_info *desc = &s->dma_desc_info[vb->v4l2_buf.index];
10885756a06SHans Verkuil 
10985756a06SHans Verkuil 	descriptor_list_free(desc);
11085756a06SHans Verkuil }
11185756a06SHans Verkuil 
11285756a06SHans Verkuil static int cobalt_buf_prepare(struct vb2_buffer *vb)
11385756a06SHans Verkuil {
11485756a06SHans Verkuil 	struct cobalt_stream *s = vb->vb2_queue->drv_priv;
11585756a06SHans Verkuil 
11685756a06SHans Verkuil 	vb2_set_plane_payload(vb, 0, s->stride * s->height);
11785756a06SHans Verkuil 	vb->v4l2_buf.field = V4L2_FIELD_NONE;
11885756a06SHans Verkuil 	return 0;
11985756a06SHans Verkuil }
12085756a06SHans Verkuil 
12185756a06SHans Verkuil static void chain_all_buffers(struct cobalt_stream *s)
12285756a06SHans Verkuil {
12385756a06SHans Verkuil 	struct sg_dma_desc_info *desc[NR_BUFS];
12485756a06SHans Verkuil 	struct cobalt_buffer *cb;
12585756a06SHans Verkuil 	struct list_head *p;
12685756a06SHans Verkuil 	int i = 0;
12785756a06SHans Verkuil 
12885756a06SHans Verkuil 	list_for_each(p, &s->bufs) {
12985756a06SHans Verkuil 		cb = list_entry(p, struct cobalt_buffer, list);
13085756a06SHans Verkuil 		desc[i] = &s->dma_desc_info[cb->vb.v4l2_buf.index];
13185756a06SHans Verkuil 		if (i > 0)
13285756a06SHans Verkuil 			descriptor_list_chain(desc[i-1], desc[i]);
13385756a06SHans Verkuil 		i++;
13485756a06SHans Verkuil 	}
13585756a06SHans Verkuil }
13685756a06SHans Verkuil 
13785756a06SHans Verkuil static void cobalt_buf_queue(struct vb2_buffer *vb)
13885756a06SHans Verkuil {
13985756a06SHans Verkuil 	struct vb2_queue *q = vb->vb2_queue;
14085756a06SHans Verkuil 	struct cobalt_stream *s = q->drv_priv;
14185756a06SHans Verkuil 	struct cobalt_buffer *cb = to_cobalt_buffer(vb);
14285756a06SHans Verkuil 	struct sg_dma_desc_info *desc = &s->dma_desc_info[vb->v4l2_buf.index];
14385756a06SHans Verkuil 	unsigned long flags;
14485756a06SHans Verkuil 
14585756a06SHans Verkuil 	/* Prepare new buffer */
14685756a06SHans Verkuil 	descriptor_list_loopback(desc);
14785756a06SHans Verkuil 	descriptor_list_interrupt_disable(desc);
14885756a06SHans Verkuil 
14985756a06SHans Verkuil 	spin_lock_irqsave(&s->irqlock, flags);
15085756a06SHans Verkuil 	list_add_tail(&cb->list, &s->bufs);
15185756a06SHans Verkuil 	chain_all_buffers(s);
15285756a06SHans Verkuil 	spin_unlock_irqrestore(&s->irqlock, flags);
15385756a06SHans Verkuil }
15485756a06SHans Verkuil 
15585756a06SHans Verkuil static void cobalt_enable_output(struct cobalt_stream *s)
15685756a06SHans Verkuil {
15785756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
15885756a06SHans Verkuil 	struct v4l2_bt_timings *bt = &s->timings.bt;
1594da70768SHans Verkuil 	struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
16085756a06SHans Verkuil 		COBALT_TX_BASE(cobalt);
16185756a06SHans Verkuil 	unsigned fmt = s->pixfmt != V4L2_PIX_FMT_BGR32 ?
16285756a06SHans Verkuil 			M00514_CONTROL_BITMAP_FORMAT_16_BPP_MSK : 0;
16385756a06SHans Verkuil 	struct v4l2_subdev_format sd_fmt = {
16485756a06SHans Verkuil 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
16585756a06SHans Verkuil 	};
16685756a06SHans Verkuil 
16785756a06SHans Verkuil 	if (!cobalt_cpld_set_freq(cobalt, bt->pixelclock)) {
16885756a06SHans Verkuil 		cobalt_err("pixelclock out of range\n");
16985756a06SHans Verkuil 		return;
17085756a06SHans Verkuil 	}
17185756a06SHans Verkuil 
17285756a06SHans Verkuil 	sd_fmt.format.colorspace = s->colorspace;
173*136a5e9eSHans Verkuil 	sd_fmt.format.xfer_func = s->xfer_func;
17485756a06SHans Verkuil 	sd_fmt.format.ycbcr_enc = s->ycbcr_enc;
17585756a06SHans Verkuil 	sd_fmt.format.quantization = s->quantization;
17685756a06SHans Verkuil 	sd_fmt.format.width = bt->width;
17785756a06SHans Verkuil 	sd_fmt.format.height = bt->height;
17885756a06SHans Verkuil 
17985756a06SHans Verkuil 	/* Set up FDMA packer */
18085756a06SHans Verkuil 	switch (s->pixfmt) {
18185756a06SHans Verkuil 	case V4L2_PIX_FMT_YUYV:
18285756a06SHans Verkuil 		sd_fmt.format.code = MEDIA_BUS_FMT_UYVY8_1X16;
18385756a06SHans Verkuil 		break;
18485756a06SHans Verkuil 	case V4L2_PIX_FMT_BGR32:
18585756a06SHans Verkuil 		sd_fmt.format.code = MEDIA_BUS_FMT_RGB888_1X24;
18685756a06SHans Verkuil 		break;
18785756a06SHans Verkuil 	}
18885756a06SHans Verkuil 	v4l2_subdev_call(s->sd, pad, set_fmt, NULL, &sd_fmt);
18985756a06SHans Verkuil 
1904da70768SHans Verkuil 	iowrite32(0, &vo->control);
19185756a06SHans Verkuil 	/* 1080p60 */
1924da70768SHans Verkuil 	iowrite32(bt->hsync, &vo->sync_generator_h_sync_length);
1934da70768SHans Verkuil 	iowrite32(bt->hbackporch, &vo->sync_generator_h_backporch_length);
1944da70768SHans Verkuil 	iowrite32(bt->width, &vo->sync_generator_h_active_length);
1954da70768SHans Verkuil 	iowrite32(bt->hfrontporch, &vo->sync_generator_h_frontporch_length);
1964da70768SHans Verkuil 	iowrite32(bt->vsync, &vo->sync_generator_v_sync_length);
1974da70768SHans Verkuil 	iowrite32(bt->vbackporch, &vo->sync_generator_v_backporch_length);
1984da70768SHans Verkuil 	iowrite32(bt->height, &vo->sync_generator_v_active_length);
1994da70768SHans Verkuil 	iowrite32(bt->vfrontporch, &vo->sync_generator_v_frontporch_length);
2004da70768SHans Verkuil 	iowrite32(0x9900c1, &vo->error_color);
20185756a06SHans Verkuil 
2024da70768SHans Verkuil 	iowrite32(M00514_CONTROL_BITMAP_SYNC_GENERATOR_LOAD_PARAM_MSK | fmt,
2034da70768SHans Verkuil 		  &vo->control);
2044da70768SHans Verkuil 	iowrite32(M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK | fmt, &vo->control);
2054da70768SHans Verkuil 	iowrite32(M00514_CONTROL_BITMAP_SYNC_GENERATOR_ENABLE_MSK |
20685756a06SHans Verkuil 		  M00514_CONTROL_BITMAP_FLOW_CTRL_OUTPUT_ENABLE_MSK |
2074da70768SHans Verkuil 		  fmt, &vo->control);
20885756a06SHans Verkuil }
20985756a06SHans Verkuil 
21085756a06SHans Verkuil static void cobalt_enable_input(struct cobalt_stream *s)
21185756a06SHans Verkuil {
21285756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
21385756a06SHans Verkuil 	int ch = (int)s->video_channel;
2144da70768SHans Verkuil 	struct m00235_fdma_packer_regmap __iomem *packer;
21585756a06SHans Verkuil 	struct v4l2_subdev_format sd_fmt_yuyv = {
21685756a06SHans Verkuil 		.pad = s->pad_source,
21785756a06SHans Verkuil 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
21885756a06SHans Verkuil 		.format.code = MEDIA_BUS_FMT_YUYV8_1X16,
21985756a06SHans Verkuil 	};
22085756a06SHans Verkuil 	struct v4l2_subdev_format sd_fmt_rgb = {
22185756a06SHans Verkuil 		.pad = s->pad_source,
22285756a06SHans Verkuil 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
22385756a06SHans Verkuil 		.format.code = MEDIA_BUS_FMT_RGB888_1X24,
22485756a06SHans Verkuil 	};
22585756a06SHans Verkuil 
22685756a06SHans Verkuil 	cobalt_dbg(1, "video_channel %d (%s, %s)\n",
22785756a06SHans Verkuil 		   s->video_channel,
22885756a06SHans Verkuil 		   s->input == 0 ? "hdmi" : "generator",
22985756a06SHans Verkuil 		   "YUYV");
23085756a06SHans Verkuil 
23185756a06SHans Verkuil 	packer = COBALT_CVI_PACKER(cobalt, ch);
23285756a06SHans Verkuil 
23385756a06SHans Verkuil 	/* Set up FDMA packer */
23485756a06SHans Verkuil 	switch (s->pixfmt) {
23585756a06SHans Verkuil 	case V4L2_PIX_FMT_YUYV:
2364da70768SHans Verkuil 		iowrite32(M00235_CONTROL_BITMAP_ENABLE_MSK |
2374da70768SHans Verkuil 			  (1 << M00235_CONTROL_BITMAP_PACK_FORMAT_OFST),
2384da70768SHans Verkuil 			  &packer->control);
23985756a06SHans Verkuil 		v4l2_subdev_call(s->sd, pad, set_fmt, NULL,
24085756a06SHans Verkuil 				 &sd_fmt_yuyv);
24185756a06SHans Verkuil 		break;
24285756a06SHans Verkuil 	case V4L2_PIX_FMT_RGB24:
2434da70768SHans Verkuil 		iowrite32(M00235_CONTROL_BITMAP_ENABLE_MSK |
2444da70768SHans Verkuil 			  (2 << M00235_CONTROL_BITMAP_PACK_FORMAT_OFST),
2454da70768SHans Verkuil 			  &packer->control);
24685756a06SHans Verkuil 		v4l2_subdev_call(s->sd, pad, set_fmt, NULL,
24785756a06SHans Verkuil 				 &sd_fmt_rgb);
24885756a06SHans Verkuil 		break;
24985756a06SHans Verkuil 	case V4L2_PIX_FMT_BGR32:
2504da70768SHans Verkuil 		iowrite32(M00235_CONTROL_BITMAP_ENABLE_MSK |
25185756a06SHans Verkuil 			  M00235_CONTROL_BITMAP_ENDIAN_FORMAT_MSK |
2524da70768SHans Verkuil 			  (3 << M00235_CONTROL_BITMAP_PACK_FORMAT_OFST),
2534da70768SHans Verkuil 			  &packer->control);
25485756a06SHans Verkuil 		v4l2_subdev_call(s->sd, pad, set_fmt, NULL,
25585756a06SHans Verkuil 				 &sd_fmt_rgb);
25685756a06SHans Verkuil 		break;
25785756a06SHans Verkuil 	}
25885756a06SHans Verkuil }
25985756a06SHans Verkuil 
26085756a06SHans Verkuil static void cobalt_dma_start_streaming(struct cobalt_stream *s)
26185756a06SHans Verkuil {
26285756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
26385756a06SHans Verkuil 	int rx = s->video_channel;
2644da70768SHans Verkuil 	struct m00460_evcnt_regmap __iomem *evcnt =
26585756a06SHans Verkuil 		COBALT_CVI_EVCNT(cobalt, rx);
26685756a06SHans Verkuil 	struct cobalt_buffer *cb;
26785756a06SHans Verkuil 	unsigned long flags;
26885756a06SHans Verkuil 
26985756a06SHans Verkuil 	spin_lock_irqsave(&s->irqlock, flags);
27085756a06SHans Verkuil 	if (!s->is_output) {
2714da70768SHans Verkuil 		iowrite32(M00460_CONTROL_BITMAP_CLEAR_MSK, &evcnt->control);
2724da70768SHans Verkuil 		iowrite32(M00460_CONTROL_BITMAP_ENABLE_MSK, &evcnt->control);
27385756a06SHans Verkuil 	} else {
2744da70768SHans Verkuil 		struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
27585756a06SHans Verkuil 			COBALT_TX_BASE(cobalt);
2764da70768SHans Verkuil 		u32 ctrl = ioread32(&vo->control);
27785756a06SHans Verkuil 
27885756a06SHans Verkuil 		ctrl &= ~(M00514_CONTROL_BITMAP_EVCNT_ENABLE_MSK |
27985756a06SHans Verkuil 			  M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK);
2804da70768SHans Verkuil 		iowrite32(ctrl | M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK,
2814da70768SHans Verkuil 			  &vo->control);
2824da70768SHans Verkuil 		iowrite32(ctrl | M00514_CONTROL_BITMAP_EVCNT_ENABLE_MSK,
2834da70768SHans Verkuil 			  &vo->control);
28485756a06SHans Verkuil 	}
28585756a06SHans Verkuil 	cb = list_first_entry(&s->bufs, struct cobalt_buffer, list);
28685756a06SHans Verkuil 	omni_sg_dma_start(s, &s->dma_desc_info[cb->vb.v4l2_buf.index]);
28785756a06SHans Verkuil 	spin_unlock_irqrestore(&s->irqlock, flags);
28885756a06SHans Verkuil }
28985756a06SHans Verkuil 
29085756a06SHans Verkuil static int cobalt_start_streaming(struct vb2_queue *q, unsigned int count)
29185756a06SHans Verkuil {
29285756a06SHans Verkuil 	struct cobalt_stream *s = q->drv_priv;
29385756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
2944da70768SHans Verkuil 	struct m00233_video_measure_regmap __iomem *vmr;
2954da70768SHans Verkuil 	struct m00473_freewheel_regmap __iomem *fw;
2964da70768SHans Verkuil 	struct m00479_clk_loss_detector_regmap __iomem *clkloss;
29785756a06SHans Verkuil 	int rx = s->video_channel;
2984da70768SHans Verkuil 	struct m00389_cvi_regmap __iomem *cvi = COBALT_CVI(cobalt, rx);
2994da70768SHans Verkuil 	struct m00460_evcnt_regmap __iomem *evcnt = COBALT_CVI_EVCNT(cobalt, rx);
30085756a06SHans Verkuil 	struct v4l2_bt_timings *bt = &s->timings.bt;
30185756a06SHans Verkuil 	u64 tot_size;
3024da70768SHans Verkuil 	u32 clk_freq;
30385756a06SHans Verkuil 
30485756a06SHans Verkuil 	if (s->is_audio)
30585756a06SHans Verkuil 		goto done;
30685756a06SHans Verkuil 	if (s->is_output) {
30785756a06SHans Verkuil 		s->unstable_frame = false;
30885756a06SHans Verkuil 		cobalt_enable_output(s);
30985756a06SHans Verkuil 		goto done;
31085756a06SHans Verkuil 	}
31185756a06SHans Verkuil 
31285756a06SHans Verkuil 	cobalt_enable_input(s);
31385756a06SHans Verkuil 
31485756a06SHans Verkuil 	fw = COBALT_CVI_FREEWHEEL(cobalt, rx);
31585756a06SHans Verkuil 	vmr = COBALT_CVI_VMR(cobalt, rx);
31685756a06SHans Verkuil 	clkloss = COBALT_CVI_CLK_LOSS(cobalt, rx);
31785756a06SHans Verkuil 
3184da70768SHans Verkuil 	iowrite32(M00460_CONTROL_BITMAP_CLEAR_MSK, &evcnt->control);
3194da70768SHans Verkuil 	iowrite32(M00460_CONTROL_BITMAP_ENABLE_MSK, &evcnt->control);
3204da70768SHans Verkuil 	iowrite32(bt->width, &cvi->frame_width);
3214da70768SHans Verkuil 	iowrite32(bt->height, &cvi->frame_height);
32285756a06SHans Verkuil 	tot_size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
3234da70768SHans Verkuil 	iowrite32(div_u64((u64)V4L2_DV_BT_FRAME_WIDTH(bt) * COBALT_CLK * 4,
3244da70768SHans Verkuil 			  bt->pixelclock), &vmr->hsync_timeout_val);
3254da70768SHans Verkuil 	iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK, &vmr->control);
3264da70768SHans Verkuil 	clk_freq = ioread32(&fw->clk_freq);
3274da70768SHans Verkuil 	iowrite32(clk_freq / 1000000, &clkloss->ref_clk_cnt_val);
32885756a06SHans Verkuil 	/* The lower bound for the clock frequency is 0.5% lower as is
32985756a06SHans Verkuil 	 * allowed by the spec */
3304da70768SHans Verkuil 	iowrite32((((u64)bt->pixelclock * 995) / 1000) / 1000000,
3314da70768SHans Verkuil 		  &clkloss->test_clk_cnt_val);
33285756a06SHans Verkuil 	/* will be enabled after the first frame has been received */
3334da70768SHans Verkuil 	iowrite32(bt->width * bt->height, &fw->active_length);
3344da70768SHans Verkuil 	iowrite32(div_u64((u64)clk_freq * tot_size, bt->pixelclock),
3354da70768SHans Verkuil 		  &fw->total_length);
3364da70768SHans Verkuil 	iowrite32(M00233_IRQ_TRIGGERS_BITMAP_VACTIVE_AREA_MSK |
3374da70768SHans Verkuil 		  M00233_IRQ_TRIGGERS_BITMAP_HACTIVE_AREA_MSK,
3384da70768SHans Verkuil 		  &vmr->irq_triggers);
3394da70768SHans Verkuil 	iowrite32(0, &cvi->control);
3404da70768SHans Verkuil 	iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK, &vmr->control);
34185756a06SHans Verkuil 
3424da70768SHans Verkuil 	iowrite32(0xff, &fw->output_color);
3434da70768SHans Verkuil 	iowrite32(M00479_CTRL_BITMAP_ENABLE_MSK, &clkloss->ctrl);
3444da70768SHans Verkuil 	iowrite32(M00473_CTRL_BITMAP_ENABLE_MSK |
3454da70768SHans Verkuil 		  M00473_CTRL_BITMAP_FORCE_FREEWHEEL_MODE_MSK, &fw->ctrl);
34685756a06SHans Verkuil 	s->unstable_frame = true;
34785756a06SHans Verkuil 	s->enable_freewheel = false;
34885756a06SHans Verkuil 	s->enable_cvi = false;
34985756a06SHans Verkuil 	s->skip_first_frames = 0;
35085756a06SHans Verkuil 
35185756a06SHans Verkuil done:
35285756a06SHans Verkuil 	s->sequence = 0;
35385756a06SHans Verkuil 	cobalt_dma_start_streaming(s);
35485756a06SHans Verkuil 	return 0;
35585756a06SHans Verkuil }
35685756a06SHans Verkuil 
35785756a06SHans Verkuil static void cobalt_dma_stop_streaming(struct cobalt_stream *s)
35885756a06SHans Verkuil {
35985756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
36085756a06SHans Verkuil 	struct sg_dma_desc_info *desc;
36185756a06SHans Verkuil 	struct cobalt_buffer *cb;
36285756a06SHans Verkuil 	struct list_head *p;
36385756a06SHans Verkuil 	unsigned long flags;
36485756a06SHans Verkuil 	int timeout_msec = 100;
36585756a06SHans Verkuil 	int rx = s->video_channel;
3664da70768SHans Verkuil 	struct m00460_evcnt_regmap __iomem *evcnt =
36785756a06SHans Verkuil 		COBALT_CVI_EVCNT(cobalt, rx);
36885756a06SHans Verkuil 
36985756a06SHans Verkuil 	if (!s->is_output) {
3704da70768SHans Verkuil 		iowrite32(0, &evcnt->control);
37185756a06SHans Verkuil 	} else if (!s->is_audio) {
3724da70768SHans Verkuil 		struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
37385756a06SHans Verkuil 			COBALT_TX_BASE(cobalt);
37485756a06SHans Verkuil 
3754da70768SHans Verkuil 		iowrite32(M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK, &vo->control);
3764da70768SHans Verkuil 		iowrite32(0, &vo->control);
37785756a06SHans Verkuil 	}
37885756a06SHans Verkuil 
37985756a06SHans Verkuil 	/* Try to stop the DMA engine gracefully */
38085756a06SHans Verkuil 	spin_lock_irqsave(&s->irqlock, flags);
38185756a06SHans Verkuil 	list_for_each(p, &s->bufs) {
38285756a06SHans Verkuil 		cb = list_entry(p, struct cobalt_buffer, list);
38385756a06SHans Verkuil 		desc = &s->dma_desc_info[cb->vb.v4l2_buf.index];
38485756a06SHans Verkuil 		/* Stop DMA after this descriptor chain */
38585756a06SHans Verkuil 		descriptor_list_end_of_chain(desc);
38685756a06SHans Verkuil 	}
38785756a06SHans Verkuil 	spin_unlock_irqrestore(&s->irqlock, flags);
38885756a06SHans Verkuil 
38985756a06SHans Verkuil 	/* Wait 100 milisecond for DMA to finish, abort on timeout. */
39085756a06SHans Verkuil 	if (!wait_event_timeout(s->q.done_wq, is_dma_done(s),
39185756a06SHans Verkuil 				msecs_to_jiffies(timeout_msec))) {
39285756a06SHans Verkuil 		omni_sg_dma_abort_channel(s);
39385756a06SHans Verkuil 		pr_warn("aborted\n");
39485756a06SHans Verkuil 	}
39585756a06SHans Verkuil 	cobalt_write_bar0(cobalt, DMA_INTERRUPT_STATUS_REG,
39685756a06SHans Verkuil 			1 << s->dma_channel);
39785756a06SHans Verkuil }
39885756a06SHans Verkuil 
39985756a06SHans Verkuil static void cobalt_stop_streaming(struct vb2_queue *q)
40085756a06SHans Verkuil {
40185756a06SHans Verkuil 	struct cobalt_stream *s = q->drv_priv;
40285756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
40385756a06SHans Verkuil 	int rx = s->video_channel;
4044da70768SHans Verkuil 	struct m00233_video_measure_regmap __iomem *vmr;
4054da70768SHans Verkuil 	struct m00473_freewheel_regmap __iomem *fw;
4064da70768SHans Verkuil 	struct m00479_clk_loss_detector_regmap __iomem *clkloss;
40785756a06SHans Verkuil 	struct cobalt_buffer *cb;
40885756a06SHans Verkuil 	struct list_head *p, *safe;
40985756a06SHans Verkuil 	unsigned long flags;
41085756a06SHans Verkuil 
41185756a06SHans Verkuil 	cobalt_dma_stop_streaming(s);
41285756a06SHans Verkuil 
41385756a06SHans Verkuil 	/* Return all buffers to user space */
41485756a06SHans Verkuil 	spin_lock_irqsave(&s->irqlock, flags);
41585756a06SHans Verkuil 	list_for_each_safe(p, safe, &s->bufs) {
41685756a06SHans Verkuil 		cb = list_entry(p, struct cobalt_buffer, list);
41785756a06SHans Verkuil 		list_del(&cb->list);
41885756a06SHans Verkuil 		vb2_buffer_done(&cb->vb, VB2_BUF_STATE_ERROR);
41985756a06SHans Verkuil 	}
42085756a06SHans Verkuil 	spin_unlock_irqrestore(&s->irqlock, flags);
42185756a06SHans Verkuil 
42285756a06SHans Verkuil 	if (s->is_audio || s->is_output)
42385756a06SHans Verkuil 		return;
42485756a06SHans Verkuil 
42585756a06SHans Verkuil 	fw = COBALT_CVI_FREEWHEEL(cobalt, rx);
42685756a06SHans Verkuil 	vmr = COBALT_CVI_VMR(cobalt, rx);
42785756a06SHans Verkuil 	clkloss = COBALT_CVI_CLK_LOSS(cobalt, rx);
4284da70768SHans Verkuil 	iowrite32(0, &vmr->control);
4294da70768SHans Verkuil 	iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK, &vmr->control);
4304da70768SHans Verkuil 	iowrite32(0, &fw->ctrl);
4314da70768SHans Verkuil 	iowrite32(0, &clkloss->ctrl);
43285756a06SHans Verkuil }
43385756a06SHans Verkuil 
43485756a06SHans Verkuil static const struct vb2_ops cobalt_qops = {
43585756a06SHans Verkuil 	.queue_setup = cobalt_queue_setup,
43685756a06SHans Verkuil 	.buf_init = cobalt_buf_init,
43785756a06SHans Verkuil 	.buf_cleanup = cobalt_buf_cleanup,
43885756a06SHans Verkuil 	.buf_prepare = cobalt_buf_prepare,
43985756a06SHans Verkuil 	.buf_queue = cobalt_buf_queue,
44085756a06SHans Verkuil 	.start_streaming = cobalt_start_streaming,
44185756a06SHans Verkuil 	.stop_streaming = cobalt_stop_streaming,
44285756a06SHans Verkuil 	.wait_prepare = vb2_ops_wait_prepare,
44385756a06SHans Verkuil 	.wait_finish = vb2_ops_wait_finish,
44485756a06SHans Verkuil };
44585756a06SHans Verkuil 
44685756a06SHans Verkuil /* V4L2 ioctls */
44785756a06SHans Verkuil 
44885756a06SHans Verkuil #ifdef CONFIG_VIDEO_ADV_DEBUG
44985756a06SHans Verkuil static int cobalt_cobaltc(struct cobalt *cobalt, unsigned int cmd, void *arg)
45085756a06SHans Verkuil {
45185756a06SHans Verkuil 	struct v4l2_dbg_register *regs = arg;
45285756a06SHans Verkuil 	void __iomem *adrs = cobalt->bar1 + regs->reg;
45385756a06SHans Verkuil 
45485756a06SHans Verkuil 	cobalt_info("cobalt_cobaltc: adrs = %p\n", adrs);
45585756a06SHans Verkuil 
45685756a06SHans Verkuil 	if (!capable(CAP_SYS_ADMIN))
45785756a06SHans Verkuil 		return -EPERM;
45885756a06SHans Verkuil 
45985756a06SHans Verkuil 	regs->size = 4;
46085756a06SHans Verkuil 	if (cmd == VIDIOC_DBG_S_REGISTER)
46185756a06SHans Verkuil 		iowrite32(regs->val, adrs);
46285756a06SHans Verkuil 	else
46385756a06SHans Verkuil 		regs->val = ioread32(adrs);
46485756a06SHans Verkuil 	return 0;
46585756a06SHans Verkuil }
46685756a06SHans Verkuil 
46785756a06SHans Verkuil static int cobalt_g_register(struct file *file, void *priv_fh,
46885756a06SHans Verkuil 		struct v4l2_dbg_register *reg)
46985756a06SHans Verkuil {
47085756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
47185756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
47285756a06SHans Verkuil 
47385756a06SHans Verkuil 	return cobalt_cobaltc(cobalt, VIDIOC_DBG_G_REGISTER, reg);
47485756a06SHans Verkuil }
47585756a06SHans Verkuil 
47685756a06SHans Verkuil static int cobalt_s_register(struct file *file, void *priv_fh,
47785756a06SHans Verkuil 		const struct v4l2_dbg_register *reg)
47885756a06SHans Verkuil {
47985756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
48085756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
48185756a06SHans Verkuil 
48285756a06SHans Verkuil 	return cobalt_cobaltc(cobalt, VIDIOC_DBG_S_REGISTER,
48385756a06SHans Verkuil 			(struct v4l2_dbg_register *)reg);
48485756a06SHans Verkuil }
48585756a06SHans Verkuil #endif
48685756a06SHans Verkuil 
48785756a06SHans Verkuil static int cobalt_querycap(struct file *file, void *priv_fh,
48885756a06SHans Verkuil 				struct v4l2_capability *vcap)
48985756a06SHans Verkuil {
49085756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
49185756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
49285756a06SHans Verkuil 
49385756a06SHans Verkuil 	strlcpy(vcap->driver, "cobalt", sizeof(vcap->driver));
49485756a06SHans Verkuil 	strlcpy(vcap->card, "cobalt", sizeof(vcap->card));
49585756a06SHans Verkuil 	snprintf(vcap->bus_info, sizeof(vcap->bus_info),
49685756a06SHans Verkuil 		 "PCIe:%s", pci_name(cobalt->pci_dev));
49785756a06SHans Verkuil 	vcap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
49885756a06SHans Verkuil 	if (s->is_output)
49985756a06SHans Verkuil 		vcap->device_caps |= V4L2_CAP_VIDEO_OUTPUT;
50085756a06SHans Verkuil 	else
50185756a06SHans Verkuil 		vcap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
50285756a06SHans Verkuil 	vcap->capabilities = vcap->device_caps | V4L2_CAP_DEVICE_CAPS |
50385756a06SHans Verkuil 		V4L2_CAP_VIDEO_CAPTURE;
50485756a06SHans Verkuil 	if (cobalt->have_hsma_tx)
50585756a06SHans Verkuil 		vcap->capabilities |= V4L2_CAP_VIDEO_OUTPUT;
50685756a06SHans Verkuil 	return 0;
50785756a06SHans Verkuil }
50885756a06SHans Verkuil 
50985756a06SHans Verkuil static void cobalt_video_input_status_show(struct cobalt_stream *s)
51085756a06SHans Verkuil {
5114da70768SHans Verkuil 	struct m00389_cvi_regmap __iomem *cvi;
5124da70768SHans Verkuil 	struct m00233_video_measure_regmap __iomem *vmr;
5134da70768SHans Verkuil 	struct m00473_freewheel_regmap __iomem *fw;
5144da70768SHans Verkuil 	struct m00479_clk_loss_detector_regmap __iomem *clkloss;
5154da70768SHans Verkuil 	struct m00235_fdma_packer_regmap __iomem *packer;
51685756a06SHans Verkuil 	int rx = s->video_channel;
51785756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
5184da70768SHans Verkuil 	u32 cvi_ctrl, cvi_stat;
5194da70768SHans Verkuil 	u32 vmr_ctrl, vmr_stat;
52085756a06SHans Verkuil 
52185756a06SHans Verkuil 	cvi = COBALT_CVI(cobalt, rx);
52285756a06SHans Verkuil 	vmr = COBALT_CVI_VMR(cobalt, rx);
52385756a06SHans Verkuil 	fw = COBALT_CVI_FREEWHEEL(cobalt, rx);
52485756a06SHans Verkuil 	clkloss = COBALT_CVI_CLK_LOSS(cobalt, rx);
52585756a06SHans Verkuil 	packer = COBALT_CVI_PACKER(cobalt, rx);
5264da70768SHans Verkuil 	cvi_ctrl = ioread32(&cvi->control);
5274da70768SHans Verkuil 	cvi_stat = ioread32(&cvi->status);
5284da70768SHans Verkuil 	vmr_ctrl = ioread32(&vmr->control);
5294da70768SHans Verkuil 	vmr_stat = ioread32(&vmr->control);
53085756a06SHans Verkuil 	cobalt_info("rx%d: cvi resolution: %dx%d\n", rx,
5314da70768SHans Verkuil 		    ioread32(&cvi->frame_width), ioread32(&cvi->frame_height));
53285756a06SHans Verkuil 	cobalt_info("rx%d: cvi control: %s%s%s\n", rx,
5334da70768SHans Verkuil 		(cvi_ctrl & M00389_CONTROL_BITMAP_ENABLE_MSK) ?
53485756a06SHans Verkuil 			"enable " : "disable ",
5354da70768SHans Verkuil 		(cvi_ctrl & M00389_CONTROL_BITMAP_HSYNC_POLARITY_LOW_MSK) ?
53685756a06SHans Verkuil 			"HSync- " : "HSync+ ",
5374da70768SHans Verkuil 		(cvi_ctrl & M00389_CONTROL_BITMAP_VSYNC_POLARITY_LOW_MSK) ?
53885756a06SHans Verkuil 			"VSync- " : "VSync+ ");
53985756a06SHans Verkuil 	cobalt_info("rx%d: cvi status: %s%s\n", rx,
5404da70768SHans Verkuil 		(cvi_stat & M00389_STATUS_BITMAP_LOCK_MSK) ?
54185756a06SHans Verkuil 			"lock " : "no-lock ",
5424da70768SHans Verkuil 		(cvi_stat & M00389_STATUS_BITMAP_ERROR_MSK) ?
54385756a06SHans Verkuil 			"error " : "no-error ");
54485756a06SHans Verkuil 
54585756a06SHans Verkuil 	cobalt_info("rx%d: Measurements: %s%s%s%s%s%s%s\n", rx,
5464da70768SHans Verkuil 		(vmr_ctrl & M00233_CONTROL_BITMAP_HSYNC_POLARITY_LOW_MSK) ?
54785756a06SHans Verkuil 			"HSync- " : "HSync+ ",
5484da70768SHans Verkuil 		(vmr_ctrl & M00233_CONTROL_BITMAP_VSYNC_POLARITY_LOW_MSK) ?
54985756a06SHans Verkuil 			"VSync- " : "VSync+ ",
5504da70768SHans Verkuil 		(vmr_ctrl & M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK) ?
55185756a06SHans Verkuil 			"enabled " : "disabled ",
5524da70768SHans Verkuil 		(vmr_ctrl & M00233_CONTROL_BITMAP_ENABLE_INTERRUPT_MSK) ?
55385756a06SHans Verkuil 			"irq-enabled " : "irq-disabled ",
5544da70768SHans Verkuil 		(vmr_ctrl & M00233_CONTROL_BITMAP_UPDATE_ON_HSYNC_MSK) ?
55585756a06SHans Verkuil 			"update-on-hsync " : "",
5564da70768SHans Verkuil 		(vmr_stat & M00233_STATUS_BITMAP_HSYNC_TIMEOUT_MSK) ?
55785756a06SHans Verkuil 			"hsync-timeout " : "",
5584da70768SHans Verkuil 		(vmr_stat & M00233_STATUS_BITMAP_INIT_DONE_MSK) ?
55985756a06SHans Verkuil 			"init-done" : "");
56085756a06SHans Verkuil 	cobalt_info("rx%d: irq_status: 0x%02x irq_triggers: 0x%02x\n", rx,
5614da70768SHans Verkuil 			ioread32(&vmr->irq_status) & 0xff,
5624da70768SHans Verkuil 			ioread32(&vmr->irq_triggers) & 0xff);
5634da70768SHans Verkuil 	cobalt_info("rx%d: vsync: %d\n", rx, ioread32(&vmr->vsync_time));
5644da70768SHans Verkuil 	cobalt_info("rx%d: vbp: %d\n", rx, ioread32(&vmr->vback_porch));
5654da70768SHans Verkuil 	cobalt_info("rx%d: vact: %d\n", rx, ioread32(&vmr->vactive_area));
5664da70768SHans Verkuil 	cobalt_info("rx%d: vfb: %d\n", rx, ioread32(&vmr->vfront_porch));
5674da70768SHans Verkuil 	cobalt_info("rx%d: hsync: %d\n", rx, ioread32(&vmr->hsync_time));
5684da70768SHans Verkuil 	cobalt_info("rx%d: hbp: %d\n", rx, ioread32(&vmr->hback_porch));
5694da70768SHans Verkuil 	cobalt_info("rx%d: hact: %d\n", rx, ioread32(&vmr->hactive_area));
5704da70768SHans Verkuil 	cobalt_info("rx%d: hfb: %d\n", rx, ioread32(&vmr->hfront_porch));
57185756a06SHans Verkuil 	cobalt_info("rx%d: Freewheeling: %s%s%s\n", rx,
5724da70768SHans Verkuil 		(ioread32(&fw->ctrl) & M00473_CTRL_BITMAP_ENABLE_MSK) ?
57385756a06SHans Verkuil 			"enabled " : "disabled ",
5744da70768SHans Verkuil 		(ioread32(&fw->ctrl) & M00473_CTRL_BITMAP_FORCE_FREEWHEEL_MODE_MSK) ?
57585756a06SHans Verkuil 			"forced " : "",
5764da70768SHans Verkuil 		(ioread32(&fw->status) & M00473_STATUS_BITMAP_FREEWHEEL_MODE_MSK) ?
57785756a06SHans Verkuil 			"freewheeling " : "video-passthrough ");
5784da70768SHans Verkuil 	iowrite32(0xff, &vmr->irq_status);
57985756a06SHans Verkuil 	cobalt_info("rx%d: Clock Loss Detection: %s%s\n", rx,
5804da70768SHans Verkuil 		(ioread32(&clkloss->ctrl) & M00479_CTRL_BITMAP_ENABLE_MSK) ?
58185756a06SHans Verkuil 			"enabled " : "disabled ",
5824da70768SHans Verkuil 		(ioread32(&clkloss->status) & M00479_STATUS_BITMAP_CLOCK_MISSING_MSK) ?
58385756a06SHans Verkuil 			"clock-missing " : "found-clock ");
5844da70768SHans Verkuil 	cobalt_info("rx%d: Packer: %x\n", rx, ioread32(&packer->control));
58585756a06SHans Verkuil }
58685756a06SHans Verkuil 
58785756a06SHans Verkuil static int cobalt_log_status(struct file *file, void *priv_fh)
58885756a06SHans Verkuil {
58985756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
59085756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
5914da70768SHans Verkuil 	struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
59285756a06SHans Verkuil 		COBALT_TX_BASE(cobalt);
59385756a06SHans Verkuil 	u8 stat;
59485756a06SHans Verkuil 
59585756a06SHans Verkuil 	cobalt_info("%s", cobalt->hdl_info);
59685756a06SHans Verkuil 	cobalt_info("sysctrl: %08x, sysstat: %08x\n",
59785756a06SHans Verkuil 			cobalt_g_sysctrl(cobalt),
59885756a06SHans Verkuil 			cobalt_g_sysstat(cobalt));
59985756a06SHans Verkuil 	cobalt_info("dma channel: %d, video channel: %d\n",
60085756a06SHans Verkuil 			s->dma_channel, s->video_channel);
60185756a06SHans Verkuil 	cobalt_pcie_status_show(cobalt);
60285756a06SHans Verkuil 	cobalt_cpld_status(cobalt);
60385756a06SHans Verkuil 	cobalt_irq_log_status(cobalt);
60485756a06SHans Verkuil 	v4l2_subdev_call(s->sd, core, log_status);
60585756a06SHans Verkuil 	if (!s->is_output) {
60685756a06SHans Verkuil 		cobalt_video_input_status_show(s);
60785756a06SHans Verkuil 		return 0;
60885756a06SHans Verkuil 	}
60985756a06SHans Verkuil 
6104da70768SHans Verkuil 	stat = ioread32(&vo->rd_status);
61185756a06SHans Verkuil 
61285756a06SHans Verkuil 	cobalt_info("tx: status: %s%s\n",
61385756a06SHans Verkuil 		(stat & M00514_RD_STATUS_BITMAP_FLOW_CTRL_NO_DATA_ERROR_MSK) ?
61485756a06SHans Verkuil 			"no_data " : "",
61585756a06SHans Verkuil 		(stat & M00514_RD_STATUS_BITMAP_READY_BUFFER_FULL_MSK) ?
61685756a06SHans Verkuil 			"ready_buffer_full " : "");
6174da70768SHans Verkuil 	cobalt_info("tx: evcnt: %d\n", ioread32(&vo->rd_evcnt_count));
61885756a06SHans Verkuil 	return 0;
61985756a06SHans Verkuil }
62085756a06SHans Verkuil 
62185756a06SHans Verkuil static int cobalt_enum_dv_timings(struct file *file, void *priv_fh,
62285756a06SHans Verkuil 				    struct v4l2_enum_dv_timings *timings)
62385756a06SHans Verkuil {
62485756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
62585756a06SHans Verkuil 
62685756a06SHans Verkuil 	if (s->input == 1) {
62785756a06SHans Verkuil 		if (timings->index)
62885756a06SHans Verkuil 			return -EINVAL;
62985756a06SHans Verkuil 		memset(timings->reserved, 0, sizeof(timings->reserved));
63085756a06SHans Verkuil 		timings->timings = cea1080p60;
63185756a06SHans Verkuil 		return 0;
63285756a06SHans Verkuil 	}
63385756a06SHans Verkuil 	timings->pad = 0;
63485756a06SHans Verkuil 	return v4l2_subdev_call(s->sd,
63585756a06SHans Verkuil 			pad, enum_dv_timings, timings);
63685756a06SHans Verkuil }
63785756a06SHans Verkuil 
63885756a06SHans Verkuil static int cobalt_s_dv_timings(struct file *file, void *priv_fh,
63985756a06SHans Verkuil 				    struct v4l2_dv_timings *timings)
64085756a06SHans Verkuil {
64185756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
64285756a06SHans Verkuil 	int err;
64385756a06SHans Verkuil 
64485756a06SHans Verkuil 	if (vb2_is_busy(&s->q))
64585756a06SHans Verkuil 		return -EBUSY;
64685756a06SHans Verkuil 
64785756a06SHans Verkuil 	if (s->input == 1) {
64885756a06SHans Verkuil 		*timings = cea1080p60;
64985756a06SHans Verkuil 		return 0;
65085756a06SHans Verkuil 	}
65185756a06SHans Verkuil 	err = v4l2_subdev_call(s->sd,
65285756a06SHans Verkuil 			video, s_dv_timings, timings);
65385756a06SHans Verkuil 	if (!err) {
65485756a06SHans Verkuil 		s->timings = *timings;
65585756a06SHans Verkuil 		s->width = timings->bt.width;
65685756a06SHans Verkuil 		s->height = timings->bt.height;
65785756a06SHans Verkuil 		s->stride = timings->bt.width * s->bpp;
65885756a06SHans Verkuil 	}
65985756a06SHans Verkuil 	return err;
66085756a06SHans Verkuil }
66185756a06SHans Verkuil 
66285756a06SHans Verkuil static int cobalt_g_dv_timings(struct file *file, void *priv_fh,
66385756a06SHans Verkuil 				    struct v4l2_dv_timings *timings)
66485756a06SHans Verkuil {
66585756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
66685756a06SHans Verkuil 
66785756a06SHans Verkuil 	if (s->input == 1) {
66885756a06SHans Verkuil 		*timings = cea1080p60;
66985756a06SHans Verkuil 		return 0;
67085756a06SHans Verkuil 	}
67185756a06SHans Verkuil 	return v4l2_subdev_call(s->sd,
67285756a06SHans Verkuil 			video, g_dv_timings, timings);
67385756a06SHans Verkuil }
67485756a06SHans Verkuil 
67585756a06SHans Verkuil static int cobalt_query_dv_timings(struct file *file, void *priv_fh,
67685756a06SHans Verkuil 				    struct v4l2_dv_timings *timings)
67785756a06SHans Verkuil {
67885756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
67985756a06SHans Verkuil 
68085756a06SHans Verkuil 	if (s->input == 1) {
68185756a06SHans Verkuil 		*timings = cea1080p60;
68285756a06SHans Verkuil 		return 0;
68385756a06SHans Verkuil 	}
68485756a06SHans Verkuil 	return v4l2_subdev_call(s->sd,
68585756a06SHans Verkuil 			video, query_dv_timings, timings);
68685756a06SHans Verkuil }
68785756a06SHans Verkuil 
68885756a06SHans Verkuil static int cobalt_dv_timings_cap(struct file *file, void *priv_fh,
68985756a06SHans Verkuil 				    struct v4l2_dv_timings_cap *cap)
69085756a06SHans Verkuil {
69185756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
69285756a06SHans Verkuil 
69385756a06SHans Verkuil 	cap->pad = 0;
69485756a06SHans Verkuil 	return v4l2_subdev_call(s->sd,
69585756a06SHans Verkuil 			pad, dv_timings_cap, cap);
69685756a06SHans Verkuil }
69785756a06SHans Verkuil 
69885756a06SHans Verkuil static int cobalt_enum_fmt_vid_cap(struct file *file, void *priv_fh,
69985756a06SHans Verkuil 		struct v4l2_fmtdesc *f)
70085756a06SHans Verkuil {
70185756a06SHans Verkuil 	switch (f->index) {
70285756a06SHans Verkuil 	case 0:
70385756a06SHans Verkuil 		strlcpy(f->description, "YUV 4:2:2", sizeof(f->description));
70485756a06SHans Verkuil 		f->pixelformat = V4L2_PIX_FMT_YUYV;
70585756a06SHans Verkuil 		break;
70685756a06SHans Verkuil 	case 1:
70785756a06SHans Verkuil 		strlcpy(f->description, "RGB24", sizeof(f->description));
70885756a06SHans Verkuil 		f->pixelformat = V4L2_PIX_FMT_RGB24;
70985756a06SHans Verkuil 		break;
71085756a06SHans Verkuil 	case 2:
71185756a06SHans Verkuil 		strlcpy(f->description, "RGB32", sizeof(f->description));
71285756a06SHans Verkuil 		f->pixelformat = V4L2_PIX_FMT_BGR32;
71385756a06SHans Verkuil 		break;
71485756a06SHans Verkuil 	default:
71585756a06SHans Verkuil 		return -EINVAL;
71685756a06SHans Verkuil 	}
71785756a06SHans Verkuil 
71885756a06SHans Verkuil 	return 0;
71985756a06SHans Verkuil }
72085756a06SHans Verkuil 
72185756a06SHans Verkuil static int cobalt_g_fmt_vid_cap(struct file *file, void *priv_fh,
72285756a06SHans Verkuil 		struct v4l2_format *f)
72385756a06SHans Verkuil {
72485756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
72585756a06SHans Verkuil 	struct v4l2_pix_format *pix = &f->fmt.pix;
72685756a06SHans Verkuil 	struct v4l2_subdev_format sd_fmt;
72785756a06SHans Verkuil 
72885756a06SHans Verkuil 	pix->width = s->width;
72985756a06SHans Verkuil 	pix->height = s->height;
73085756a06SHans Verkuil 	pix->bytesperline = s->stride;
73185756a06SHans Verkuil 	pix->field = V4L2_FIELD_NONE;
73285756a06SHans Verkuil 
73385756a06SHans Verkuil 	if (s->input == 1) {
73485756a06SHans Verkuil 		pix->colorspace = V4L2_COLORSPACE_SRGB;
73585756a06SHans Verkuil 	} else {
73685756a06SHans Verkuil 		sd_fmt.pad = s->pad_source;
73785756a06SHans Verkuil 		sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
73885756a06SHans Verkuil 		v4l2_subdev_call(s->sd, pad, get_fmt, NULL, &sd_fmt);
73985756a06SHans Verkuil 		v4l2_fill_pix_format(pix, &sd_fmt.format);
74085756a06SHans Verkuil 		pix->colorspace = sd_fmt.format.colorspace;
741*136a5e9eSHans Verkuil 		pix->xfer_func = sd_fmt.format.xfer_func;
74285756a06SHans Verkuil 		pix->ycbcr_enc = sd_fmt.format.ycbcr_enc;
74385756a06SHans Verkuil 		pix->quantization = sd_fmt.format.quantization;
74485756a06SHans Verkuil 	}
74585756a06SHans Verkuil 
74685756a06SHans Verkuil 	pix->pixelformat = s->pixfmt;
74785756a06SHans Verkuil 	pix->sizeimage = pix->bytesperline * pix->height;
74885756a06SHans Verkuil 
74985756a06SHans Verkuil 	return 0;
75085756a06SHans Verkuil }
75185756a06SHans Verkuil 
75285756a06SHans Verkuil static int cobalt_try_fmt_vid_cap(struct file *file, void *priv_fh,
75385756a06SHans Verkuil 		struct v4l2_format *f)
75485756a06SHans Verkuil {
75585756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
75685756a06SHans Verkuil 	struct v4l2_pix_format *pix = &f->fmt.pix;
75785756a06SHans Verkuil 	struct v4l2_subdev_format sd_fmt;
75885756a06SHans Verkuil 
75985756a06SHans Verkuil 	/* Check for min (QCIF) and max (Full HD) size */
76085756a06SHans Verkuil 	if ((pix->width < 176) || (pix->height < 144)) {
76185756a06SHans Verkuil 		pix->width = 176;
76285756a06SHans Verkuil 		pix->height = 144;
76385756a06SHans Verkuil 	}
76485756a06SHans Verkuil 
76585756a06SHans Verkuil 	if ((pix->width > 1920) || (pix->height > 1080)) {
76685756a06SHans Verkuil 		pix->width = 1920;
76785756a06SHans Verkuil 		pix->height = 1080;
76885756a06SHans Verkuil 	}
76985756a06SHans Verkuil 
77085756a06SHans Verkuil 	/* Make width multiple of 4 */
77185756a06SHans Verkuil 	pix->width &= ~0x3;
77285756a06SHans Verkuil 
77385756a06SHans Verkuil 	/* Make height multiple of 2 */
77485756a06SHans Verkuil 	pix->height &= ~0x1;
77585756a06SHans Verkuil 
77685756a06SHans Verkuil 	if (s->input == 1) {
77785756a06SHans Verkuil 		/* Generator => fixed format only */
77885756a06SHans Verkuil 		pix->width = 1920;
77985756a06SHans Verkuil 		pix->height = 1080;
78085756a06SHans Verkuil 		pix->colorspace = V4L2_COLORSPACE_SRGB;
78185756a06SHans Verkuil 	} else {
78285756a06SHans Verkuil 		sd_fmt.pad = s->pad_source;
78385756a06SHans Verkuil 		sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
78485756a06SHans Verkuil 		v4l2_subdev_call(s->sd, pad, get_fmt, NULL, &sd_fmt);
78585756a06SHans Verkuil 		v4l2_fill_pix_format(pix, &sd_fmt.format);
78685756a06SHans Verkuil 		pix->colorspace = sd_fmt.format.colorspace;
787*136a5e9eSHans Verkuil 		pix->xfer_func = sd_fmt.format.xfer_func;
78885756a06SHans Verkuil 		pix->ycbcr_enc = sd_fmt.format.ycbcr_enc;
78985756a06SHans Verkuil 		pix->quantization = sd_fmt.format.quantization;
79085756a06SHans Verkuil 	}
79185756a06SHans Verkuil 
79285756a06SHans Verkuil 	switch (pix->pixelformat) {
79385756a06SHans Verkuil 	case V4L2_PIX_FMT_YUYV:
79485756a06SHans Verkuil 	default:
79585756a06SHans Verkuil 		pix->bytesperline = max(pix->bytesperline & ~0x3,
79685756a06SHans Verkuil 				pix->width * COBALT_BYTES_PER_PIXEL_YUYV);
79785756a06SHans Verkuil 		pix->pixelformat = V4L2_PIX_FMT_YUYV;
79885756a06SHans Verkuil 		break;
79985756a06SHans Verkuil 	case V4L2_PIX_FMT_RGB24:
80085756a06SHans Verkuil 		pix->bytesperline = max(pix->bytesperline & ~0x3,
80185756a06SHans Verkuil 				pix->width * COBALT_BYTES_PER_PIXEL_RGB24);
80285756a06SHans Verkuil 		break;
80385756a06SHans Verkuil 	case V4L2_PIX_FMT_BGR32:
80485756a06SHans Verkuil 		pix->bytesperline = max(pix->bytesperline & ~0x3,
80585756a06SHans Verkuil 				pix->width * COBALT_BYTES_PER_PIXEL_RGB32);
80685756a06SHans Verkuil 		break;
80785756a06SHans Verkuil 	}
80885756a06SHans Verkuil 
80985756a06SHans Verkuil 	pix->sizeimage = pix->bytesperline * pix->height;
81085756a06SHans Verkuil 	pix->field = V4L2_FIELD_NONE;
81185756a06SHans Verkuil 	pix->priv = 0;
81285756a06SHans Verkuil 
81385756a06SHans Verkuil 	return 0;
81485756a06SHans Verkuil }
81585756a06SHans Verkuil 
81685756a06SHans Verkuil static int cobalt_s_fmt_vid_cap(struct file *file, void *priv_fh,
81785756a06SHans Verkuil 		struct v4l2_format *f)
81885756a06SHans Verkuil {
81985756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
82085756a06SHans Verkuil 	struct v4l2_pix_format *pix = &f->fmt.pix;
82185756a06SHans Verkuil 
82285756a06SHans Verkuil 	if (vb2_is_busy(&s->q))
82385756a06SHans Verkuil 		return -EBUSY;
82485756a06SHans Verkuil 
82585756a06SHans Verkuil 	if (cobalt_try_fmt_vid_cap(file, priv_fh, f))
82685756a06SHans Verkuil 		return -EINVAL;
82785756a06SHans Verkuil 
82885756a06SHans Verkuil 	s->width = pix->width;
82985756a06SHans Verkuil 	s->height = pix->height;
83085756a06SHans Verkuil 	s->stride = pix->bytesperline;
83185756a06SHans Verkuil 	switch (pix->pixelformat) {
83285756a06SHans Verkuil 	case V4L2_PIX_FMT_YUYV:
83385756a06SHans Verkuil 		s->bpp = COBALT_BYTES_PER_PIXEL_YUYV;
83485756a06SHans Verkuil 		break;
83585756a06SHans Verkuil 	case V4L2_PIX_FMT_RGB24:
83685756a06SHans Verkuil 		s->bpp = COBALT_BYTES_PER_PIXEL_RGB24;
83785756a06SHans Verkuil 		break;
83885756a06SHans Verkuil 	case V4L2_PIX_FMT_BGR32:
83985756a06SHans Verkuil 		s->bpp = COBALT_BYTES_PER_PIXEL_RGB32;
84085756a06SHans Verkuil 		break;
84185756a06SHans Verkuil 	default:
84285756a06SHans Verkuil 		return -EINVAL;
84385756a06SHans Verkuil 	}
84485756a06SHans Verkuil 	s->pixfmt = pix->pixelformat;
84585756a06SHans Verkuil 	cobalt_enable_input(s);
84685756a06SHans Verkuil 
84785756a06SHans Verkuil 	return 0;
84885756a06SHans Verkuil }
84985756a06SHans Verkuil 
85085756a06SHans Verkuil static int cobalt_try_fmt_vid_out(struct file *file, void *priv_fh,
85185756a06SHans Verkuil 		struct v4l2_format *f)
85285756a06SHans Verkuil {
85385756a06SHans Verkuil 	struct v4l2_pix_format *pix = &f->fmt.pix;
85485756a06SHans Verkuil 
85585756a06SHans Verkuil 	/* Check for min (QCIF) and max (Full HD) size */
85685756a06SHans Verkuil 	if ((pix->width < 176) || (pix->height < 144)) {
85785756a06SHans Verkuil 		pix->width = 176;
85885756a06SHans Verkuil 		pix->height = 144;
85985756a06SHans Verkuil 	}
86085756a06SHans Verkuil 
86185756a06SHans Verkuil 	if ((pix->width > 1920) || (pix->height > 1080)) {
86285756a06SHans Verkuil 		pix->width = 1920;
86385756a06SHans Verkuil 		pix->height = 1080;
86485756a06SHans Verkuil 	}
86585756a06SHans Verkuil 
86685756a06SHans Verkuil 	/* Make width multiple of 4 */
86785756a06SHans Verkuil 	pix->width &= ~0x3;
86885756a06SHans Verkuil 
86985756a06SHans Verkuil 	/* Make height multiple of 2 */
87085756a06SHans Verkuil 	pix->height &= ~0x1;
87185756a06SHans Verkuil 
87285756a06SHans Verkuil 	switch (pix->pixelformat) {
87385756a06SHans Verkuil 	case V4L2_PIX_FMT_YUYV:
87485756a06SHans Verkuil 	default:
87585756a06SHans Verkuil 		pix->bytesperline = max(pix->bytesperline & ~0x3,
87685756a06SHans Verkuil 				pix->width * COBALT_BYTES_PER_PIXEL_YUYV);
87785756a06SHans Verkuil 		pix->pixelformat = V4L2_PIX_FMT_YUYV;
87885756a06SHans Verkuil 		break;
87985756a06SHans Verkuil 	case V4L2_PIX_FMT_BGR32:
88085756a06SHans Verkuil 		pix->bytesperline = max(pix->bytesperline & ~0x3,
88185756a06SHans Verkuil 				pix->width * COBALT_BYTES_PER_PIXEL_RGB32);
88285756a06SHans Verkuil 		break;
88385756a06SHans Verkuil 	}
88485756a06SHans Verkuil 
88585756a06SHans Verkuil 	pix->sizeimage = pix->bytesperline * pix->height;
88685756a06SHans Verkuil 	pix->field = V4L2_FIELD_NONE;
88785756a06SHans Verkuil 
88885756a06SHans Verkuil 	return 0;
88985756a06SHans Verkuil }
89085756a06SHans Verkuil 
89185756a06SHans Verkuil static int cobalt_g_fmt_vid_out(struct file *file, void *priv_fh,
89285756a06SHans Verkuil 		struct v4l2_format *f)
89385756a06SHans Verkuil {
89485756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
89585756a06SHans Verkuil 	struct v4l2_pix_format *pix = &f->fmt.pix;
89685756a06SHans Verkuil 
89785756a06SHans Verkuil 	pix->width = s->width;
89885756a06SHans Verkuil 	pix->height = s->height;
89985756a06SHans Verkuil 	pix->bytesperline = s->stride;
90085756a06SHans Verkuil 	pix->field = V4L2_FIELD_NONE;
90185756a06SHans Verkuil 	pix->pixelformat = s->pixfmt;
90285756a06SHans Verkuil 	pix->colorspace = s->colorspace;
903*136a5e9eSHans Verkuil 	pix->xfer_func = s->xfer_func;
90485756a06SHans Verkuil 	pix->ycbcr_enc = s->ycbcr_enc;
90585756a06SHans Verkuil 	pix->quantization = s->quantization;
90685756a06SHans Verkuil 	pix->sizeimage = pix->bytesperline * pix->height;
90785756a06SHans Verkuil 
90885756a06SHans Verkuil 	return 0;
90985756a06SHans Verkuil }
91085756a06SHans Verkuil 
91185756a06SHans Verkuil static int cobalt_enum_fmt_vid_out(struct file *file, void *priv_fh,
91285756a06SHans Verkuil 		struct v4l2_fmtdesc *f)
91385756a06SHans Verkuil {
91485756a06SHans Verkuil 	switch (f->index) {
91585756a06SHans Verkuil 	case 0:
91685756a06SHans Verkuil 		strlcpy(f->description, "YUV 4:2:2", sizeof(f->description));
91785756a06SHans Verkuil 		f->pixelformat = V4L2_PIX_FMT_YUYV;
91885756a06SHans Verkuil 		break;
91985756a06SHans Verkuil 	case 1:
92085756a06SHans Verkuil 		strlcpy(f->description, "RGB32", sizeof(f->description));
92185756a06SHans Verkuil 		f->pixelformat = V4L2_PIX_FMT_BGR32;
92285756a06SHans Verkuil 		break;
92385756a06SHans Verkuil 	default:
92485756a06SHans Verkuil 		return -EINVAL;
92585756a06SHans Verkuil 	}
92685756a06SHans Verkuil 
92785756a06SHans Verkuil 	return 0;
92885756a06SHans Verkuil }
92985756a06SHans Verkuil 
93085756a06SHans Verkuil static int cobalt_s_fmt_vid_out(struct file *file, void *priv_fh,
93185756a06SHans Verkuil 		struct v4l2_format *f)
93285756a06SHans Verkuil {
93385756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
93485756a06SHans Verkuil 	struct v4l2_pix_format *pix = &f->fmt.pix;
93585756a06SHans Verkuil 	struct v4l2_subdev_format sd_fmt = { 0 };
93685756a06SHans Verkuil 
93785756a06SHans Verkuil 	if (cobalt_try_fmt_vid_out(file, priv_fh, f))
93885756a06SHans Verkuil 		return -EINVAL;
93985756a06SHans Verkuil 
94085756a06SHans Verkuil 	if (vb2_is_busy(&s->q) && (pix->pixelformat != s->pixfmt ||
94185756a06SHans Verkuil 	    pix->width != s->width || pix->height != s->height ||
94285756a06SHans Verkuil 	    pix->bytesperline != s->stride))
94385756a06SHans Verkuil 		return -EBUSY;
94485756a06SHans Verkuil 
94585756a06SHans Verkuil 	switch (pix->pixelformat) {
94685756a06SHans Verkuil 	case V4L2_PIX_FMT_YUYV:
94785756a06SHans Verkuil 		s->bpp = COBALT_BYTES_PER_PIXEL_YUYV;
94885756a06SHans Verkuil 		break;
94985756a06SHans Verkuil 	case V4L2_PIX_FMT_BGR32:
95085756a06SHans Verkuil 		s->bpp = COBALT_BYTES_PER_PIXEL_RGB32;
95185756a06SHans Verkuil 		break;
95285756a06SHans Verkuil 	default:
95385756a06SHans Verkuil 		return -EINVAL;
95485756a06SHans Verkuil 	}
95585756a06SHans Verkuil 	s->width = pix->width;
95685756a06SHans Verkuil 	s->height = pix->height;
95785756a06SHans Verkuil 	s->stride = pix->bytesperline;
95885756a06SHans Verkuil 	s->pixfmt = pix->pixelformat;
95985756a06SHans Verkuil 	s->colorspace = pix->colorspace;
960*136a5e9eSHans Verkuil 	s->xfer_func = pix->xfer_func;
96185756a06SHans Verkuil 	s->ycbcr_enc = pix->ycbcr_enc;
96285756a06SHans Verkuil 	s->quantization = pix->quantization;
96385756a06SHans Verkuil 	sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
96485756a06SHans Verkuil 	v4l2_subdev_call(s->sd, pad, get_fmt, NULL, &sd_fmt);
96585756a06SHans Verkuil 	sd_fmt.format.colorspace = pix->colorspace;
966*136a5e9eSHans Verkuil 	sd_fmt.format.xfer_func = pix->xfer_func;
96785756a06SHans Verkuil 	sd_fmt.format.ycbcr_enc = pix->ycbcr_enc;
96885756a06SHans Verkuil 	sd_fmt.format.quantization = pix->quantization;
96985756a06SHans Verkuil 	v4l2_subdev_call(s->sd, pad, set_fmt, NULL, &sd_fmt);
97085756a06SHans Verkuil 	return 0;
97185756a06SHans Verkuil }
97285756a06SHans Verkuil 
97385756a06SHans Verkuil static int cobalt_enum_input(struct file *file, void *priv_fh,
97485756a06SHans Verkuil 				 struct v4l2_input *inp)
97585756a06SHans Verkuil {
97685756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
97785756a06SHans Verkuil 
97885756a06SHans Verkuil 	if (inp->index > 1)
97985756a06SHans Verkuil 		return -EINVAL;
98085756a06SHans Verkuil 	if (inp->index == 0)
98185756a06SHans Verkuil 		snprintf(inp->name, sizeof(inp->name),
98285756a06SHans Verkuil 				"HDMI-%d", s->video_channel);
98385756a06SHans Verkuil 	else
98485756a06SHans Verkuil 		snprintf(inp->name, sizeof(inp->name),
98585756a06SHans Verkuil 				"Generator-%d", s->video_channel);
98685756a06SHans Verkuil 	inp->type = V4L2_INPUT_TYPE_CAMERA;
98785756a06SHans Verkuil 	inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
98885756a06SHans Verkuil 	if (inp->index == 1)
98985756a06SHans Verkuil 		return 0;
99085756a06SHans Verkuil 	return v4l2_subdev_call(s->sd,
99185756a06SHans Verkuil 			video, g_input_status, &inp->status);
99285756a06SHans Verkuil }
99385756a06SHans Verkuil 
99485756a06SHans Verkuil static int cobalt_g_input(struct file *file, void *priv_fh, unsigned int *i)
99585756a06SHans Verkuil {
99685756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
99785756a06SHans Verkuil 
99885756a06SHans Verkuil 	*i = s->input;
99985756a06SHans Verkuil 	return 0;
100085756a06SHans Verkuil }
100185756a06SHans Verkuil 
100285756a06SHans Verkuil static int cobalt_s_input(struct file *file, void *priv_fh, unsigned int i)
100385756a06SHans Verkuil {
100485756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
100585756a06SHans Verkuil 
100685756a06SHans Verkuil 	if (i >= 2)
100785756a06SHans Verkuil 		return -EINVAL;
100885756a06SHans Verkuil 	if (vb2_is_busy(&s->q))
100985756a06SHans Verkuil 		return -EBUSY;
101085756a06SHans Verkuil 	s->input = i;
101185756a06SHans Verkuil 
101285756a06SHans Verkuil 	cobalt_enable_input(s);
101385756a06SHans Verkuil 
101485756a06SHans Verkuil 	if (s->input == 1) /* Test Pattern Generator */
101585756a06SHans Verkuil 		return 0;
101685756a06SHans Verkuil 
101785756a06SHans Verkuil 	return v4l2_subdev_call(s->sd, video, s_routing,
101885756a06SHans Verkuil 			ADV76XX_PAD_HDMI_PORT_A, 0, 0);
101985756a06SHans Verkuil }
102085756a06SHans Verkuil 
102185756a06SHans Verkuil static int cobalt_enum_output(struct file *file, void *priv_fh,
102285756a06SHans Verkuil 				 struct v4l2_output *out)
102385756a06SHans Verkuil {
102485756a06SHans Verkuil 	if (out->index)
102585756a06SHans Verkuil 		return -EINVAL;
102685756a06SHans Verkuil 	snprintf(out->name, sizeof(out->name), "HDMI-%d", out->index);
102785756a06SHans Verkuil 	out->type = V4L2_OUTPUT_TYPE_ANALOG;
102885756a06SHans Verkuil 	out->capabilities = V4L2_OUT_CAP_DV_TIMINGS;
102985756a06SHans Verkuil 	return 0;
103085756a06SHans Verkuil }
103185756a06SHans Verkuil 
103285756a06SHans Verkuil static int cobalt_g_output(struct file *file, void *priv_fh, unsigned int *i)
103385756a06SHans Verkuil {
103485756a06SHans Verkuil 	*i = 0;
103585756a06SHans Verkuil 	return 0;
103685756a06SHans Verkuil }
103785756a06SHans Verkuil 
103885756a06SHans Verkuil static int cobalt_s_output(struct file *file, void *priv_fh, unsigned int i)
103985756a06SHans Verkuil {
104085756a06SHans Verkuil 	return i ? -EINVAL : 0;
104185756a06SHans Verkuil }
104285756a06SHans Verkuil 
104385756a06SHans Verkuil static int cobalt_g_edid(struct file *file, void *fh, struct v4l2_edid *edid)
104485756a06SHans Verkuil {
104585756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
104685756a06SHans Verkuil 	u32 pad = edid->pad;
104785756a06SHans Verkuil 	int ret;
104885756a06SHans Verkuil 
104985756a06SHans Verkuil 	if (edid->pad >= (s->is_output ? 1 : 2))
105085756a06SHans Verkuil 		return -EINVAL;
105185756a06SHans Verkuil 	edid->pad = 0;
105285756a06SHans Verkuil 	ret = v4l2_subdev_call(s->sd, pad, get_edid, edid);
105385756a06SHans Verkuil 	edid->pad = pad;
105485756a06SHans Verkuil 	return ret;
105585756a06SHans Verkuil }
105685756a06SHans Verkuil 
105785756a06SHans Verkuil static int cobalt_s_edid(struct file *file, void *fh, struct v4l2_edid *edid)
105885756a06SHans Verkuil {
105985756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
106085756a06SHans Verkuil 	u32 pad = edid->pad;
106185756a06SHans Verkuil 	int ret;
106285756a06SHans Verkuil 
106385756a06SHans Verkuil 	if (edid->pad >= 2)
106485756a06SHans Verkuil 		return -EINVAL;
106585756a06SHans Verkuil 	edid->pad = 0;
106685756a06SHans Verkuil 	ret = v4l2_subdev_call(s->sd, pad, set_edid, edid);
106785756a06SHans Verkuil 	edid->pad = pad;
106885756a06SHans Verkuil 	return ret;
106985756a06SHans Verkuil }
107085756a06SHans Verkuil 
107185756a06SHans Verkuil static int cobalt_subscribe_event(struct v4l2_fh *fh,
107285756a06SHans Verkuil 				  const struct v4l2_event_subscription *sub)
107385756a06SHans Verkuil {
107485756a06SHans Verkuil 	switch (sub->type) {
107585756a06SHans Verkuil 	case V4L2_EVENT_SOURCE_CHANGE:
107685756a06SHans Verkuil 		return v4l2_event_subscribe(fh, sub, 4, NULL);
107785756a06SHans Verkuil 	}
107885756a06SHans Verkuil 	return v4l2_ctrl_subscribe_event(fh, sub);
107985756a06SHans Verkuil }
108085756a06SHans Verkuil 
108185756a06SHans Verkuil static int cobalt_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
108285756a06SHans Verkuil {
108385756a06SHans Verkuil 	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
108485756a06SHans Verkuil 		return -EINVAL;
108585756a06SHans Verkuil 	a->parm.capture.timeperframe.numerator = 1;
108685756a06SHans Verkuil 	a->parm.capture.timeperframe.denominator = 60;
108785756a06SHans Verkuil 	a->parm.capture.readbuffers = 3;
108885756a06SHans Verkuil 	return 0;
108985756a06SHans Verkuil }
109085756a06SHans Verkuil 
109185756a06SHans Verkuil static const struct v4l2_ioctl_ops cobalt_ioctl_ops = {
109285756a06SHans Verkuil 	.vidioc_querycap		= cobalt_querycap,
109385756a06SHans Verkuil 	.vidioc_g_parm			= cobalt_g_parm,
109485756a06SHans Verkuil 	.vidioc_log_status		= cobalt_log_status,
109585756a06SHans Verkuil 	.vidioc_streamon		= vb2_ioctl_streamon,
109685756a06SHans Verkuil 	.vidioc_streamoff		= vb2_ioctl_streamoff,
109785756a06SHans Verkuil 	.vidioc_enum_input		= cobalt_enum_input,
109885756a06SHans Verkuil 	.vidioc_g_input			= cobalt_g_input,
109985756a06SHans Verkuil 	.vidioc_s_input			= cobalt_s_input,
110085756a06SHans Verkuil 	.vidioc_enum_fmt_vid_cap	= cobalt_enum_fmt_vid_cap,
110185756a06SHans Verkuil 	.vidioc_g_fmt_vid_cap		= cobalt_g_fmt_vid_cap,
110285756a06SHans Verkuil 	.vidioc_s_fmt_vid_cap		= cobalt_s_fmt_vid_cap,
110385756a06SHans Verkuil 	.vidioc_try_fmt_vid_cap		= cobalt_try_fmt_vid_cap,
110485756a06SHans Verkuil 	.vidioc_enum_output		= cobalt_enum_output,
110585756a06SHans Verkuil 	.vidioc_g_output		= cobalt_g_output,
110685756a06SHans Verkuil 	.vidioc_s_output		= cobalt_s_output,
110785756a06SHans Verkuil 	.vidioc_enum_fmt_vid_out	= cobalt_enum_fmt_vid_out,
110885756a06SHans Verkuil 	.vidioc_g_fmt_vid_out		= cobalt_g_fmt_vid_out,
110985756a06SHans Verkuil 	.vidioc_s_fmt_vid_out		= cobalt_s_fmt_vid_out,
111085756a06SHans Verkuil 	.vidioc_try_fmt_vid_out		= cobalt_try_fmt_vid_out,
111185756a06SHans Verkuil 	.vidioc_s_dv_timings		= cobalt_s_dv_timings,
111285756a06SHans Verkuil 	.vidioc_g_dv_timings		= cobalt_g_dv_timings,
111385756a06SHans Verkuil 	.vidioc_query_dv_timings	= cobalt_query_dv_timings,
111485756a06SHans Verkuil 	.vidioc_enum_dv_timings		= cobalt_enum_dv_timings,
111585756a06SHans Verkuil 	.vidioc_dv_timings_cap		= cobalt_dv_timings_cap,
111685756a06SHans Verkuil 	.vidioc_g_edid			= cobalt_g_edid,
111785756a06SHans Verkuil 	.vidioc_s_edid			= cobalt_s_edid,
111885756a06SHans Verkuil 	.vidioc_subscribe_event		= cobalt_subscribe_event,
111985756a06SHans Verkuil 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
112085756a06SHans Verkuil 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
112185756a06SHans Verkuil 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
112285756a06SHans Verkuil 	.vidioc_querybuf		= vb2_ioctl_querybuf,
112385756a06SHans Verkuil 	.vidioc_qbuf			= vb2_ioctl_qbuf,
112485756a06SHans Verkuil 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
112585756a06SHans Verkuil 	.vidioc_expbuf			= vb2_ioctl_expbuf,
112685756a06SHans Verkuil #ifdef CONFIG_VIDEO_ADV_DEBUG
112785756a06SHans Verkuil 	.vidioc_g_register              = cobalt_g_register,
112885756a06SHans Verkuil 	.vidioc_s_register              = cobalt_s_register,
112985756a06SHans Verkuil #endif
113085756a06SHans Verkuil };
113185756a06SHans Verkuil 
113285756a06SHans Verkuil static const struct v4l2_ioctl_ops cobalt_ioctl_empty_ops = {
113385756a06SHans Verkuil #ifdef CONFIG_VIDEO_ADV_DEBUG
113485756a06SHans Verkuil 	.vidioc_g_register              = cobalt_g_register,
113585756a06SHans Verkuil 	.vidioc_s_register              = cobalt_s_register,
113685756a06SHans Verkuil #endif
113785756a06SHans Verkuil };
113885756a06SHans Verkuil 
113985756a06SHans Verkuil /* Register device nodes */
114085756a06SHans Verkuil 
114185756a06SHans Verkuil static const struct v4l2_file_operations cobalt_fops = {
114285756a06SHans Verkuil 	.owner = THIS_MODULE,
114385756a06SHans Verkuil 	.open = v4l2_fh_open,
114485756a06SHans Verkuil 	.unlocked_ioctl = video_ioctl2,
114585756a06SHans Verkuil 	.release = vb2_fop_release,
114685756a06SHans Verkuil 	.poll = vb2_fop_poll,
114785756a06SHans Verkuil 	.mmap = vb2_fop_mmap,
114885756a06SHans Verkuil 	.read = vb2_fop_read,
114985756a06SHans Verkuil };
115085756a06SHans Verkuil 
115185756a06SHans Verkuil static const struct v4l2_file_operations cobalt_out_fops = {
115285756a06SHans Verkuil 	.owner = THIS_MODULE,
115385756a06SHans Verkuil 	.open = v4l2_fh_open,
115485756a06SHans Verkuil 	.unlocked_ioctl = video_ioctl2,
115585756a06SHans Verkuil 	.release = vb2_fop_release,
115685756a06SHans Verkuil 	.poll = vb2_fop_poll,
115785756a06SHans Verkuil 	.mmap = vb2_fop_mmap,
115885756a06SHans Verkuil 	.write = vb2_fop_write,
115985756a06SHans Verkuil };
116085756a06SHans Verkuil 
116185756a06SHans Verkuil static const struct v4l2_file_operations cobalt_empty_fops = {
116285756a06SHans Verkuil 	.owner = THIS_MODULE,
116385756a06SHans Verkuil 	.open = v4l2_fh_open,
116485756a06SHans Verkuil 	.unlocked_ioctl = video_ioctl2,
116585756a06SHans Verkuil 	.release = v4l2_fh_release,
116685756a06SHans Verkuil };
116785756a06SHans Verkuil 
116885756a06SHans Verkuil static int cobalt_node_register(struct cobalt *cobalt, int node)
116985756a06SHans Verkuil {
117085756a06SHans Verkuil 	static const struct v4l2_dv_timings dv1080p60 =
117185756a06SHans Verkuil 		V4L2_DV_BT_CEA_1920X1080P60;
117285756a06SHans Verkuil 	struct cobalt_stream *s = cobalt->streams + node;
117385756a06SHans Verkuil 	struct video_device *vdev = &s->vdev;
117485756a06SHans Verkuil 	struct vb2_queue *q = &s->q;
117585756a06SHans Verkuil 	int ret;
117685756a06SHans Verkuil 
117785756a06SHans Verkuil 	mutex_init(&s->lock);
117885756a06SHans Verkuil 	spin_lock_init(&s->irqlock);
117985756a06SHans Verkuil 
118085756a06SHans Verkuil 	snprintf(vdev->name, sizeof(vdev->name),
118185756a06SHans Verkuil 			"%s-%d", cobalt->v4l2_dev.name, node);
118285756a06SHans Verkuil 	s->width = 1920;
118385756a06SHans Verkuil 	/* Audio frames are just 4 lines of 1920 bytes */
118485756a06SHans Verkuil 	s->height = s->is_audio ? 4 : 1080;
118585756a06SHans Verkuil 
118685756a06SHans Verkuil 	if (s->is_audio) {
118785756a06SHans Verkuil 		s->bpp = 1;
118885756a06SHans Verkuil 		s->pixfmt = V4L2_PIX_FMT_GREY;
118985756a06SHans Verkuil 	} else if (s->is_output) {
119085756a06SHans Verkuil 		s->bpp = COBALT_BYTES_PER_PIXEL_RGB32;
119185756a06SHans Verkuil 		s->pixfmt = V4L2_PIX_FMT_BGR32;
119285756a06SHans Verkuil 	} else {
119385756a06SHans Verkuil 		s->bpp = COBALT_BYTES_PER_PIXEL_YUYV;
119485756a06SHans Verkuil 		s->pixfmt = V4L2_PIX_FMT_YUYV;
119585756a06SHans Verkuil 	}
119685756a06SHans Verkuil 	s->colorspace = V4L2_COLORSPACE_SRGB;
119785756a06SHans Verkuil 	s->stride = s->width * s->bpp;
119885756a06SHans Verkuil 
119985756a06SHans Verkuil 	if (!s->is_audio) {
120085756a06SHans Verkuil 		if (s->is_dummy)
120185756a06SHans Verkuil 			cobalt_warn("Setting up dummy video node %d\n", node);
120285756a06SHans Verkuil 		vdev->v4l2_dev = &cobalt->v4l2_dev;
120385756a06SHans Verkuil 		if (s->is_dummy)
120485756a06SHans Verkuil 			vdev->fops = &cobalt_empty_fops;
120585756a06SHans Verkuil 		else
120685756a06SHans Verkuil 			vdev->fops = s->is_output ? &cobalt_out_fops :
120785756a06SHans Verkuil 						    &cobalt_fops;
120885756a06SHans Verkuil 		vdev->release = video_device_release_empty;
120985756a06SHans Verkuil 		vdev->vfl_dir = s->is_output ? VFL_DIR_TX : VFL_DIR_RX;
121085756a06SHans Verkuil 		vdev->lock = &s->lock;
121185756a06SHans Verkuil 		if (s->sd)
121285756a06SHans Verkuil 			vdev->ctrl_handler = s->sd->ctrl_handler;
121385756a06SHans Verkuil 		s->timings = dv1080p60;
121485756a06SHans Verkuil 		v4l2_subdev_call(s->sd, video, s_dv_timings, &s->timings);
121585756a06SHans Verkuil 		if (!s->is_output && s->sd)
121685756a06SHans Verkuil 			cobalt_enable_input(s);
121785756a06SHans Verkuil 		vdev->ioctl_ops = s->is_dummy ? &cobalt_ioctl_empty_ops :
121885756a06SHans Verkuil 				  &cobalt_ioctl_ops;
121985756a06SHans Verkuil 	}
122085756a06SHans Verkuil 
122185756a06SHans Verkuil 	INIT_LIST_HEAD(&s->bufs);
122285756a06SHans Verkuil 	q->type = s->is_output ? V4L2_BUF_TYPE_VIDEO_OUTPUT :
122385756a06SHans Verkuil 				 V4L2_BUF_TYPE_VIDEO_CAPTURE;
122485756a06SHans Verkuil 	q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
122585756a06SHans Verkuil 	q->io_modes |= s->is_output ? VB2_WRITE : VB2_READ;
122685756a06SHans Verkuil 	q->drv_priv = s;
122785756a06SHans Verkuil 	q->buf_struct_size = sizeof(struct cobalt_buffer);
122885756a06SHans Verkuil 	q->ops = &cobalt_qops;
122985756a06SHans Verkuil 	q->mem_ops = &vb2_dma_sg_memops;
123085756a06SHans Verkuil 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
123185756a06SHans Verkuil 	q->min_buffers_needed = 2;
123285756a06SHans Verkuil 	q->lock = &s->lock;
123385756a06SHans Verkuil 	vdev->queue = q;
123485756a06SHans Verkuil 
123585756a06SHans Verkuil 	video_set_drvdata(vdev, s);
123685756a06SHans Verkuil 	ret = vb2_queue_init(q);
123785756a06SHans Verkuil 	if (!s->is_audio && ret == 0)
123885756a06SHans Verkuil 		ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
123985756a06SHans Verkuil 	else if (!s->is_dummy)
124085756a06SHans Verkuil 		ret = cobalt_alsa_init(s);
124185756a06SHans Verkuil 
124285756a06SHans Verkuil 	if (ret < 0) {
124385756a06SHans Verkuil 		if (!s->is_audio)
124485756a06SHans Verkuil 			cobalt_err("couldn't register v4l2 device node %d\n",
124585756a06SHans Verkuil 					node);
124685756a06SHans Verkuil 		return ret;
124785756a06SHans Verkuil 	}
124885756a06SHans Verkuil 	cobalt_info("registered node %d\n", node);
124985756a06SHans Verkuil 	return 0;
125085756a06SHans Verkuil }
125185756a06SHans Verkuil 
125285756a06SHans Verkuil /* Initialize v4l2 variables and register v4l2 devices */
125385756a06SHans Verkuil int cobalt_nodes_register(struct cobalt *cobalt)
125485756a06SHans Verkuil {
125585756a06SHans Verkuil 	int node, ret;
125685756a06SHans Verkuil 
125785756a06SHans Verkuil 	/* Setup V4L2 Devices */
125885756a06SHans Verkuil 	for (node = 0; node < COBALT_NUM_STREAMS; node++) {
125985756a06SHans Verkuil 		ret = cobalt_node_register(cobalt, node);
126085756a06SHans Verkuil 		if (ret)
126185756a06SHans Verkuil 			return ret;
126285756a06SHans Verkuil 	}
126385756a06SHans Verkuil 	return 0;
126485756a06SHans Verkuil }
126585756a06SHans Verkuil 
126685756a06SHans Verkuil /* Unregister v4l2 devices */
126785756a06SHans Verkuil void cobalt_nodes_unregister(struct cobalt *cobalt)
126885756a06SHans Verkuil {
126985756a06SHans Verkuil 	int node;
127085756a06SHans Verkuil 
127185756a06SHans Verkuil 	/* Teardown all streams */
127285756a06SHans Verkuil 	for (node = 0; node < COBALT_NUM_STREAMS; node++) {
127385756a06SHans Verkuil 		struct cobalt_stream *s = cobalt->streams + node;
127485756a06SHans Verkuil 		struct video_device *vdev = &s->vdev;
127585756a06SHans Verkuil 
127685756a06SHans Verkuil 		if (!s->is_audio)
127785756a06SHans Verkuil 			video_unregister_device(vdev);
127885756a06SHans Verkuil 		else if (!s->is_dummy)
127985756a06SHans Verkuil 			cobalt_alsa_exit(s);
128085756a06SHans Verkuil 	}
128185756a06SHans Verkuil }
1282