xref: /openbmc/linux/drivers/media/pci/cobalt/cobalt-v4l2.c (revision 85756a069c55e0315ac5990806899cfb607b987f)
1*85756a06SHans Verkuil /*
2*85756a06SHans Verkuil  *  cobalt V4L2 API
3*85756a06SHans Verkuil  *
4*85756a06SHans Verkuil  *  Derived from ivtv-ioctl.c and cx18-fileops.c
5*85756a06SHans Verkuil  *
6*85756a06SHans Verkuil  *  Copyright 2012-2015 Cisco Systems, Inc. and/or its affiliates.
7*85756a06SHans Verkuil  *  All rights reserved.
8*85756a06SHans Verkuil  *
9*85756a06SHans Verkuil  *  This program is free software; you may redistribute it and/or modify
10*85756a06SHans Verkuil  *  it under the terms of the GNU General Public License as published by
11*85756a06SHans Verkuil  *  the Free Software Foundation; version 2 of the License.
12*85756a06SHans Verkuil  *
13*85756a06SHans Verkuil  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14*85756a06SHans Verkuil  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15*85756a06SHans Verkuil  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16*85756a06SHans Verkuil  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
17*85756a06SHans Verkuil  *  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
18*85756a06SHans Verkuil  *  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19*85756a06SHans Verkuil  *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20*85756a06SHans Verkuil  *  SOFTWARE.
21*85756a06SHans Verkuil  */
22*85756a06SHans Verkuil 
23*85756a06SHans Verkuil #include <linux/dma-mapping.h>
24*85756a06SHans Verkuil #include <linux/delay.h>
25*85756a06SHans Verkuil #include <linux/pci.h>
26*85756a06SHans Verkuil #include <linux/v4l2-dv-timings.h>
27*85756a06SHans Verkuil 
28*85756a06SHans Verkuil #include <media/v4l2-ctrls.h>
29*85756a06SHans Verkuil #include <media/v4l2-event.h>
30*85756a06SHans Verkuil #include <media/adv7604.h>
31*85756a06SHans Verkuil #include <media/adv7842.h>
32*85756a06SHans Verkuil 
33*85756a06SHans Verkuil #include "cobalt-alsa.h"
34*85756a06SHans Verkuil #include "cobalt-cpld.h"
35*85756a06SHans Verkuil #include "cobalt-driver.h"
36*85756a06SHans Verkuil #include "cobalt-v4l2.h"
37*85756a06SHans Verkuil #include "cobalt-irq.h"
38*85756a06SHans Verkuil #include "cobalt-omnitek.h"
39*85756a06SHans Verkuil 
40*85756a06SHans Verkuil static const struct v4l2_dv_timings cea1080p60 = V4L2_DV_BT_CEA_1920X1080P60;
41*85756a06SHans Verkuil 
42*85756a06SHans Verkuil /* vb2 DMA streaming ops */
43*85756a06SHans Verkuil 
44*85756a06SHans Verkuil static int cobalt_queue_setup(struct vb2_queue *q,
45*85756a06SHans Verkuil 			const struct v4l2_format *fmt,
46*85756a06SHans Verkuil 			unsigned int *num_buffers, unsigned int *num_planes,
47*85756a06SHans Verkuil 			unsigned int sizes[], void *alloc_ctxs[])
48*85756a06SHans Verkuil {
49*85756a06SHans Verkuil 	struct cobalt_stream *s = q->drv_priv;
50*85756a06SHans Verkuil 	unsigned size = s->stride * s->height;
51*85756a06SHans Verkuil 
52*85756a06SHans Verkuil 	if (*num_buffers < 3)
53*85756a06SHans Verkuil 		*num_buffers = 3;
54*85756a06SHans Verkuil 	if (*num_buffers > NR_BUFS)
55*85756a06SHans Verkuil 		*num_buffers = NR_BUFS;
56*85756a06SHans Verkuil 	*num_planes = 1;
57*85756a06SHans Verkuil 	if (fmt) {
58*85756a06SHans Verkuil 		if (fmt->fmt.pix.sizeimage < size)
59*85756a06SHans Verkuil 			return -EINVAL;
60*85756a06SHans Verkuil 		size = fmt->fmt.pix.sizeimage;
61*85756a06SHans Verkuil 	}
62*85756a06SHans Verkuil 	sizes[0] = size;
63*85756a06SHans Verkuil 	alloc_ctxs[0] = s->cobalt->alloc_ctx;
64*85756a06SHans Verkuil 	return 0;
65*85756a06SHans Verkuil }
66*85756a06SHans Verkuil 
67*85756a06SHans Verkuil static int cobalt_buf_init(struct vb2_buffer *vb)
68*85756a06SHans Verkuil {
69*85756a06SHans Verkuil 	struct cobalt_stream *s = vb->vb2_queue->drv_priv;
70*85756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
71*85756a06SHans Verkuil 	const size_t max_pages_per_line =
72*85756a06SHans Verkuil 		(COBALT_MAX_WIDTH * COBALT_MAX_BPP) / PAGE_SIZE + 2;
73*85756a06SHans Verkuil 	const size_t bytes =
74*85756a06SHans Verkuil 		COBALT_MAX_HEIGHT * max_pages_per_line * 0x20;
75*85756a06SHans Verkuil 	const size_t audio_bytes = ((1920 * 4) / PAGE_SIZE + 1) * 0x20;
76*85756a06SHans Verkuil 	struct sg_dma_desc_info *desc = &s->dma_desc_info[vb->v4l2_buf.index];
77*85756a06SHans Verkuil 	struct sg_table *sg_desc = vb2_dma_sg_plane_desc(vb, 0);
78*85756a06SHans Verkuil 	unsigned size;
79*85756a06SHans Verkuil 	int ret;
80*85756a06SHans Verkuil 
81*85756a06SHans Verkuil 	size = s->stride * s->height;
82*85756a06SHans Verkuil 	if (vb2_plane_size(vb, 0) < size) {
83*85756a06SHans Verkuil 		cobalt_info("data will not fit into plane (%lu < %u)\n",
84*85756a06SHans Verkuil 					vb2_plane_size(vb, 0), size);
85*85756a06SHans Verkuil 		return -EINVAL;
86*85756a06SHans Verkuil 	}
87*85756a06SHans Verkuil 
88*85756a06SHans Verkuil 	if (desc->virt == NULL) {
89*85756a06SHans Verkuil 		desc->dev = &cobalt->pci_dev->dev;
90*85756a06SHans Verkuil 		descriptor_list_allocate(desc,
91*85756a06SHans Verkuil 			s->is_audio ? audio_bytes : bytes);
92*85756a06SHans Verkuil 		if (desc->virt == NULL)
93*85756a06SHans Verkuil 			return -ENOMEM;
94*85756a06SHans Verkuil 	}
95*85756a06SHans Verkuil 	ret = descriptor_list_create(cobalt, sg_desc->sgl,
96*85756a06SHans Verkuil 			!s->is_output, sg_desc->nents, size,
97*85756a06SHans Verkuil 			s->width * s->bpp, s->stride, desc);
98*85756a06SHans Verkuil 	if (ret)
99*85756a06SHans Verkuil 		descriptor_list_free(desc);
100*85756a06SHans Verkuil 	return ret;
101*85756a06SHans Verkuil }
102*85756a06SHans Verkuil 
103*85756a06SHans Verkuil static void cobalt_buf_cleanup(struct vb2_buffer *vb)
104*85756a06SHans Verkuil {
105*85756a06SHans Verkuil 	struct cobalt_stream *s = vb->vb2_queue->drv_priv;
106*85756a06SHans Verkuil 	struct sg_dma_desc_info *desc = &s->dma_desc_info[vb->v4l2_buf.index];
107*85756a06SHans Verkuil 
108*85756a06SHans Verkuil 	descriptor_list_free(desc);
109*85756a06SHans Verkuil }
110*85756a06SHans Verkuil 
111*85756a06SHans Verkuil static int cobalt_buf_prepare(struct vb2_buffer *vb)
112*85756a06SHans Verkuil {
113*85756a06SHans Verkuil 	struct cobalt_stream *s = vb->vb2_queue->drv_priv;
114*85756a06SHans Verkuil 
115*85756a06SHans Verkuil 	vb2_set_plane_payload(vb, 0, s->stride * s->height);
116*85756a06SHans Verkuil 	vb->v4l2_buf.field = V4L2_FIELD_NONE;
117*85756a06SHans Verkuil 	return 0;
118*85756a06SHans Verkuil }
119*85756a06SHans Verkuil 
120*85756a06SHans Verkuil static void chain_all_buffers(struct cobalt_stream *s)
121*85756a06SHans Verkuil {
122*85756a06SHans Verkuil 	struct sg_dma_desc_info *desc[NR_BUFS];
123*85756a06SHans Verkuil 	struct cobalt_buffer *cb;
124*85756a06SHans Verkuil 	struct list_head *p;
125*85756a06SHans Verkuil 	int i = 0;
126*85756a06SHans Verkuil 
127*85756a06SHans Verkuil 	list_for_each(p, &s->bufs) {
128*85756a06SHans Verkuil 		cb = list_entry(p, struct cobalt_buffer, list);
129*85756a06SHans Verkuil 		desc[i] = &s->dma_desc_info[cb->vb.v4l2_buf.index];
130*85756a06SHans Verkuil 		if (i > 0)
131*85756a06SHans Verkuil 			descriptor_list_chain(desc[i-1], desc[i]);
132*85756a06SHans Verkuil 		i++;
133*85756a06SHans Verkuil 	}
134*85756a06SHans Verkuil }
135*85756a06SHans Verkuil 
136*85756a06SHans Verkuil static void cobalt_buf_queue(struct vb2_buffer *vb)
137*85756a06SHans Verkuil {
138*85756a06SHans Verkuil 	struct vb2_queue *q = vb->vb2_queue;
139*85756a06SHans Verkuil 	struct cobalt_stream *s = q->drv_priv;
140*85756a06SHans Verkuil 	struct cobalt_buffer *cb = to_cobalt_buffer(vb);
141*85756a06SHans Verkuil 	struct sg_dma_desc_info *desc = &s->dma_desc_info[vb->v4l2_buf.index];
142*85756a06SHans Verkuil 	unsigned long flags;
143*85756a06SHans Verkuil 
144*85756a06SHans Verkuil 	/* Prepare new buffer */
145*85756a06SHans Verkuil 	descriptor_list_loopback(desc);
146*85756a06SHans Verkuil 	descriptor_list_interrupt_disable(desc);
147*85756a06SHans Verkuil 
148*85756a06SHans Verkuil 	spin_lock_irqsave(&s->irqlock, flags);
149*85756a06SHans Verkuil 	list_add_tail(&cb->list, &s->bufs);
150*85756a06SHans Verkuil 	chain_all_buffers(s);
151*85756a06SHans Verkuil 	spin_unlock_irqrestore(&s->irqlock, flags);
152*85756a06SHans Verkuil }
153*85756a06SHans Verkuil 
154*85756a06SHans Verkuil static void cobalt_enable_output(struct cobalt_stream *s)
155*85756a06SHans Verkuil {
156*85756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
157*85756a06SHans Verkuil 	struct v4l2_bt_timings *bt = &s->timings.bt;
158*85756a06SHans Verkuil 	volatile struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
159*85756a06SHans Verkuil 		COBALT_TX_BASE(cobalt);
160*85756a06SHans Verkuil 	unsigned fmt = s->pixfmt != V4L2_PIX_FMT_BGR32 ?
161*85756a06SHans Verkuil 			M00514_CONTROL_BITMAP_FORMAT_16_BPP_MSK : 0;
162*85756a06SHans Verkuil 	struct v4l2_subdev_format sd_fmt = {
163*85756a06SHans Verkuil 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
164*85756a06SHans Verkuil 	};
165*85756a06SHans Verkuil 
166*85756a06SHans Verkuil 	if (!cobalt_cpld_set_freq(cobalt, bt->pixelclock)) {
167*85756a06SHans Verkuil 		cobalt_err("pixelclock out of range\n");
168*85756a06SHans Verkuil 		return;
169*85756a06SHans Verkuil 	}
170*85756a06SHans Verkuil 
171*85756a06SHans Verkuil 	sd_fmt.format.colorspace = s->colorspace;
172*85756a06SHans Verkuil 	sd_fmt.format.ycbcr_enc = s->ycbcr_enc;
173*85756a06SHans Verkuil 	sd_fmt.format.quantization = s->quantization;
174*85756a06SHans Verkuil 	sd_fmt.format.width = bt->width;
175*85756a06SHans Verkuil 	sd_fmt.format.height = bt->height;
176*85756a06SHans Verkuil 
177*85756a06SHans Verkuil 	/* Set up FDMA packer */
178*85756a06SHans Verkuil 	switch (s->pixfmt) {
179*85756a06SHans Verkuil 	case V4L2_PIX_FMT_YUYV:
180*85756a06SHans Verkuil 		sd_fmt.format.code = MEDIA_BUS_FMT_UYVY8_1X16;
181*85756a06SHans Verkuil 		break;
182*85756a06SHans Verkuil 	case V4L2_PIX_FMT_BGR32:
183*85756a06SHans Verkuil 		sd_fmt.format.code = MEDIA_BUS_FMT_RGB888_1X24;
184*85756a06SHans Verkuil 		break;
185*85756a06SHans Verkuil 	}
186*85756a06SHans Verkuil 	v4l2_subdev_call(s->sd, pad, set_fmt, NULL, &sd_fmt);
187*85756a06SHans Verkuil 
188*85756a06SHans Verkuil 	vo->control = 0;
189*85756a06SHans Verkuil 	/* 1080p60 */
190*85756a06SHans Verkuil 	vo->sync_generator_h_sync_length = bt->hsync;
191*85756a06SHans Verkuil 	vo->sync_generator_h_backporch_length = bt->hbackporch;
192*85756a06SHans Verkuil 	vo->sync_generator_h_active_length = bt->width;
193*85756a06SHans Verkuil 	vo->sync_generator_h_frontporch_length = bt->hfrontporch;
194*85756a06SHans Verkuil 	vo->sync_generator_v_sync_length = bt->vsync;
195*85756a06SHans Verkuil 	vo->sync_generator_v_backporch_length = bt->vbackporch;
196*85756a06SHans Verkuil 	vo->sync_generator_v_active_length = bt->height;
197*85756a06SHans Verkuil 	vo->sync_generator_v_frontporch_length = bt->vfrontporch;
198*85756a06SHans Verkuil 	vo->error_color = 0x9900c1;
199*85756a06SHans Verkuil 
200*85756a06SHans Verkuil 	vo->control = M00514_CONTROL_BITMAP_SYNC_GENERATOR_LOAD_PARAM_MSK | fmt;
201*85756a06SHans Verkuil 	vo->control = M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK | fmt;
202*85756a06SHans Verkuil 	vo->control = M00514_CONTROL_BITMAP_SYNC_GENERATOR_ENABLE_MSK |
203*85756a06SHans Verkuil 		      M00514_CONTROL_BITMAP_FLOW_CTRL_OUTPUT_ENABLE_MSK |
204*85756a06SHans Verkuil 		      fmt;
205*85756a06SHans Verkuil }
206*85756a06SHans Verkuil 
207*85756a06SHans Verkuil static void cobalt_enable_input(struct cobalt_stream *s)
208*85756a06SHans Verkuil {
209*85756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
210*85756a06SHans Verkuil 	int ch = (int)s->video_channel;
211*85756a06SHans Verkuil 	volatile struct m00235_fdma_packer_regmap __iomem *packer;
212*85756a06SHans Verkuil 	struct v4l2_subdev_format sd_fmt_yuyv = {
213*85756a06SHans Verkuil 		.pad = s->pad_source,
214*85756a06SHans Verkuil 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
215*85756a06SHans Verkuil 		.format.code = MEDIA_BUS_FMT_YUYV8_1X16,
216*85756a06SHans Verkuil 	};
217*85756a06SHans Verkuil 	struct v4l2_subdev_format sd_fmt_rgb = {
218*85756a06SHans Verkuil 		.pad = s->pad_source,
219*85756a06SHans Verkuil 		.which = V4L2_SUBDEV_FORMAT_ACTIVE,
220*85756a06SHans Verkuil 		.format.code = MEDIA_BUS_FMT_RGB888_1X24,
221*85756a06SHans Verkuil 	};
222*85756a06SHans Verkuil 
223*85756a06SHans Verkuil 	cobalt_dbg(1, "video_channel %d (%s, %s)\n",
224*85756a06SHans Verkuil 		   s->video_channel,
225*85756a06SHans Verkuil 		   s->input == 0 ? "hdmi" : "generator",
226*85756a06SHans Verkuil 		   "YUYV");
227*85756a06SHans Verkuil 
228*85756a06SHans Verkuil 	packer = COBALT_CVI_PACKER(cobalt, ch);
229*85756a06SHans Verkuil 
230*85756a06SHans Verkuil 	/* Set up FDMA packer */
231*85756a06SHans Verkuil 	switch (s->pixfmt) {
232*85756a06SHans Verkuil 	case V4L2_PIX_FMT_YUYV:
233*85756a06SHans Verkuil 		packer->control = M00235_CONTROL_BITMAP_ENABLE_MSK |
234*85756a06SHans Verkuil 			(1 << M00235_CONTROL_BITMAP_PACK_FORMAT_OFST);
235*85756a06SHans Verkuil 		v4l2_subdev_call(s->sd, pad, set_fmt, NULL,
236*85756a06SHans Verkuil 				 &sd_fmt_yuyv);
237*85756a06SHans Verkuil 		break;
238*85756a06SHans Verkuil 	case V4L2_PIX_FMT_RGB24:
239*85756a06SHans Verkuil 		packer->control = M00235_CONTROL_BITMAP_ENABLE_MSK |
240*85756a06SHans Verkuil 			(2 << M00235_CONTROL_BITMAP_PACK_FORMAT_OFST);
241*85756a06SHans Verkuil 		v4l2_subdev_call(s->sd, pad, set_fmt, NULL,
242*85756a06SHans Verkuil 				 &sd_fmt_rgb);
243*85756a06SHans Verkuil 		break;
244*85756a06SHans Verkuil 	case V4L2_PIX_FMT_BGR32:
245*85756a06SHans Verkuil 		packer->control = M00235_CONTROL_BITMAP_ENABLE_MSK |
246*85756a06SHans Verkuil 			M00235_CONTROL_BITMAP_ENDIAN_FORMAT_MSK |
247*85756a06SHans Verkuil 			(3 << M00235_CONTROL_BITMAP_PACK_FORMAT_OFST);
248*85756a06SHans Verkuil 		v4l2_subdev_call(s->sd, pad, set_fmt, NULL,
249*85756a06SHans Verkuil 				 &sd_fmt_rgb);
250*85756a06SHans Verkuil 		break;
251*85756a06SHans Verkuil 	}
252*85756a06SHans Verkuil }
253*85756a06SHans Verkuil 
254*85756a06SHans Verkuil static void cobalt_dma_start_streaming(struct cobalt_stream *s)
255*85756a06SHans Verkuil {
256*85756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
257*85756a06SHans Verkuil 	int rx = s->video_channel;
258*85756a06SHans Verkuil 	volatile struct m00460_evcnt_regmap __iomem *evcnt =
259*85756a06SHans Verkuil 		COBALT_CVI_EVCNT(cobalt, rx);
260*85756a06SHans Verkuil 	struct cobalt_buffer *cb;
261*85756a06SHans Verkuil 	unsigned long flags;
262*85756a06SHans Verkuil 
263*85756a06SHans Verkuil 	spin_lock_irqsave(&s->irqlock, flags);
264*85756a06SHans Verkuil 	if (!s->is_output) {
265*85756a06SHans Verkuil 		evcnt->control = M00460_CONTROL_BITMAP_CLEAR_MSK;
266*85756a06SHans Verkuil 		evcnt->control = M00460_CONTROL_BITMAP_ENABLE_MSK;
267*85756a06SHans Verkuil 	} else {
268*85756a06SHans Verkuil 		volatile struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
269*85756a06SHans Verkuil 			COBALT_TX_BASE(cobalt);
270*85756a06SHans Verkuil 		u32 ctrl = vo->control;
271*85756a06SHans Verkuil 
272*85756a06SHans Verkuil 		ctrl &= ~(M00514_CONTROL_BITMAP_EVCNT_ENABLE_MSK |
273*85756a06SHans Verkuil 			  M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK);
274*85756a06SHans Verkuil 		vo->control = ctrl | M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK;
275*85756a06SHans Verkuil 		vo->control = ctrl | M00514_CONTROL_BITMAP_EVCNT_ENABLE_MSK;
276*85756a06SHans Verkuil 	}
277*85756a06SHans Verkuil 	cb = list_first_entry(&s->bufs, struct cobalt_buffer, list);
278*85756a06SHans Verkuil 	omni_sg_dma_start(s, &s->dma_desc_info[cb->vb.v4l2_buf.index]);
279*85756a06SHans Verkuil 	spin_unlock_irqrestore(&s->irqlock, flags);
280*85756a06SHans Verkuil }
281*85756a06SHans Verkuil 
282*85756a06SHans Verkuil static int cobalt_start_streaming(struct vb2_queue *q, unsigned int count)
283*85756a06SHans Verkuil {
284*85756a06SHans Verkuil 	struct cobalt_stream *s = q->drv_priv;
285*85756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
286*85756a06SHans Verkuil 	volatile struct m00233_video_measure_regmap __iomem *vmr;
287*85756a06SHans Verkuil 	volatile struct m00473_freewheel_regmap __iomem *fw;
288*85756a06SHans Verkuil 	volatile struct m00479_clk_loss_detector_regmap __iomem *clkloss;
289*85756a06SHans Verkuil 	int rx = s->video_channel;
290*85756a06SHans Verkuil 	volatile struct m00389_cvi_regmap __iomem *cvi =
291*85756a06SHans Verkuil 		COBALT_CVI(cobalt, rx);
292*85756a06SHans Verkuil 	volatile struct m00460_evcnt_regmap __iomem *evcnt =
293*85756a06SHans Verkuil 		COBALT_CVI_EVCNT(cobalt, rx);
294*85756a06SHans Verkuil 	struct v4l2_bt_timings *bt = &s->timings.bt;
295*85756a06SHans Verkuil 	u64 tot_size;
296*85756a06SHans Verkuil 
297*85756a06SHans Verkuil 	if (s->is_audio)
298*85756a06SHans Verkuil 		goto done;
299*85756a06SHans Verkuil 	if (s->is_output) {
300*85756a06SHans Verkuil 		s->unstable_frame = false;
301*85756a06SHans Verkuil 		cobalt_enable_output(s);
302*85756a06SHans Verkuil 		goto done;
303*85756a06SHans Verkuil 	}
304*85756a06SHans Verkuil 
305*85756a06SHans Verkuil 	cobalt_enable_input(s);
306*85756a06SHans Verkuil 
307*85756a06SHans Verkuil 	fw = COBALT_CVI_FREEWHEEL(cobalt, rx);
308*85756a06SHans Verkuil 	vmr = COBALT_CVI_VMR(cobalt, rx);
309*85756a06SHans Verkuil 	clkloss = COBALT_CVI_CLK_LOSS(cobalt, rx);
310*85756a06SHans Verkuil 
311*85756a06SHans Verkuil 	evcnt->control = M00460_CONTROL_BITMAP_CLEAR_MSK;
312*85756a06SHans Verkuil 	evcnt->control = M00460_CONTROL_BITMAP_ENABLE_MSK;
313*85756a06SHans Verkuil 	cvi->frame_width = bt->width;
314*85756a06SHans Verkuil 	cvi->frame_height = bt->height;
315*85756a06SHans Verkuil 	tot_size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
316*85756a06SHans Verkuil 	vmr->hsync_timeout_val =
317*85756a06SHans Verkuil 		((u64)V4L2_DV_BT_FRAME_WIDTH(bt) * COBALT_CLK * 4) /
318*85756a06SHans Verkuil 		bt->pixelclock;
319*85756a06SHans Verkuil 	vmr->control = M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK;
320*85756a06SHans Verkuil 	clkloss->ref_clk_cnt_val = fw->clk_freq / 1000000;
321*85756a06SHans Verkuil 	/* The lower bound for the clock frequency is 0.5% lower as is
322*85756a06SHans Verkuil 	 * allowed by the spec */
323*85756a06SHans Verkuil 	clkloss->test_clk_cnt_val =
324*85756a06SHans Verkuil 		(((u64)bt->pixelclock * 995) / 1000) / 1000000;
325*85756a06SHans Verkuil 	/* will be enabled after the first frame has been received */
326*85756a06SHans Verkuil 	fw->active_length = bt->width * bt->height;
327*85756a06SHans Verkuil 	fw->total_length = ((u64)fw->clk_freq * tot_size) / bt->pixelclock;
328*85756a06SHans Verkuil 	vmr->irq_triggers = M00233_IRQ_TRIGGERS_BITMAP_VACTIVE_AREA_MSK |
329*85756a06SHans Verkuil 		M00233_IRQ_TRIGGERS_BITMAP_HACTIVE_AREA_MSK;
330*85756a06SHans Verkuil 	cvi->control = 0;
331*85756a06SHans Verkuil 	vmr->control = M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK;
332*85756a06SHans Verkuil 
333*85756a06SHans Verkuil 	fw->output_color = 0xff;
334*85756a06SHans Verkuil 	clkloss->ctrl = M00479_CTRL_BITMAP_ENABLE_MSK;
335*85756a06SHans Verkuil 	fw->ctrl = M00473_CTRL_BITMAP_ENABLE_MSK |
336*85756a06SHans Verkuil 		   M00473_CTRL_BITMAP_FORCE_FREEWHEEL_MODE_MSK;
337*85756a06SHans Verkuil 	s->unstable_frame = true;
338*85756a06SHans Verkuil 	s->enable_freewheel = false;
339*85756a06SHans Verkuil 	s->enable_cvi = false;
340*85756a06SHans Verkuil 	s->skip_first_frames = 0;
341*85756a06SHans Verkuil 
342*85756a06SHans Verkuil done:
343*85756a06SHans Verkuil 	s->sequence = 0;
344*85756a06SHans Verkuil 	cobalt_dma_start_streaming(s);
345*85756a06SHans Verkuil 	return 0;
346*85756a06SHans Verkuil }
347*85756a06SHans Verkuil 
348*85756a06SHans Verkuil static void cobalt_dma_stop_streaming(struct cobalt_stream *s)
349*85756a06SHans Verkuil {
350*85756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
351*85756a06SHans Verkuil 	struct sg_dma_desc_info *desc;
352*85756a06SHans Verkuil 	struct cobalt_buffer *cb;
353*85756a06SHans Verkuil 	struct list_head *p;
354*85756a06SHans Verkuil 	unsigned long flags;
355*85756a06SHans Verkuil 	int timeout_msec = 100;
356*85756a06SHans Verkuil 	int rx = s->video_channel;
357*85756a06SHans Verkuil 	volatile struct m00460_evcnt_regmap __iomem *evcnt =
358*85756a06SHans Verkuil 		COBALT_CVI_EVCNT(cobalt, rx);
359*85756a06SHans Verkuil 
360*85756a06SHans Verkuil 	if (!s->is_output) {
361*85756a06SHans Verkuil 		evcnt->control = 0;
362*85756a06SHans Verkuil 	} else if (!s->is_audio) {
363*85756a06SHans Verkuil 		volatile struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
364*85756a06SHans Verkuil 			COBALT_TX_BASE(cobalt);
365*85756a06SHans Verkuil 
366*85756a06SHans Verkuil 		vo->control = M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK;
367*85756a06SHans Verkuil 		vo->control = 0;
368*85756a06SHans Verkuil 	}
369*85756a06SHans Verkuil 
370*85756a06SHans Verkuil 	/* Try to stop the DMA engine gracefully */
371*85756a06SHans Verkuil 	spin_lock_irqsave(&s->irqlock, flags);
372*85756a06SHans Verkuil 	list_for_each(p, &s->bufs) {
373*85756a06SHans Verkuil 		cb = list_entry(p, struct cobalt_buffer, list);
374*85756a06SHans Verkuil 		desc = &s->dma_desc_info[cb->vb.v4l2_buf.index];
375*85756a06SHans Verkuil 		/* Stop DMA after this descriptor chain */
376*85756a06SHans Verkuil 		descriptor_list_end_of_chain(desc);
377*85756a06SHans Verkuil 	}
378*85756a06SHans Verkuil 	spin_unlock_irqrestore(&s->irqlock, flags);
379*85756a06SHans Verkuil 
380*85756a06SHans Verkuil 	/* Wait 100 milisecond for DMA to finish, abort on timeout. */
381*85756a06SHans Verkuil 	if (!wait_event_timeout(s->q.done_wq, is_dma_done(s),
382*85756a06SHans Verkuil 				msecs_to_jiffies(timeout_msec))) {
383*85756a06SHans Verkuil 		omni_sg_dma_abort_channel(s);
384*85756a06SHans Verkuil 		pr_warn("aborted\n");
385*85756a06SHans Verkuil 	}
386*85756a06SHans Verkuil 	cobalt_write_bar0(cobalt, DMA_INTERRUPT_STATUS_REG,
387*85756a06SHans Verkuil 			1 << s->dma_channel);
388*85756a06SHans Verkuil }
389*85756a06SHans Verkuil 
390*85756a06SHans Verkuil static void cobalt_stop_streaming(struct vb2_queue *q)
391*85756a06SHans Verkuil {
392*85756a06SHans Verkuil 	struct cobalt_stream *s = q->drv_priv;
393*85756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
394*85756a06SHans Verkuil 	int rx = s->video_channel;
395*85756a06SHans Verkuil 	volatile struct m00233_video_measure_regmap __iomem *vmr;
396*85756a06SHans Verkuil 	volatile struct m00473_freewheel_regmap __iomem *fw;
397*85756a06SHans Verkuil 	volatile struct m00479_clk_loss_detector_regmap __iomem *clkloss;
398*85756a06SHans Verkuil 	struct cobalt_buffer *cb;
399*85756a06SHans Verkuil 	struct list_head *p, *safe;
400*85756a06SHans Verkuil 	unsigned long flags;
401*85756a06SHans Verkuil 
402*85756a06SHans Verkuil 	cobalt_dma_stop_streaming(s);
403*85756a06SHans Verkuil 
404*85756a06SHans Verkuil 	/* Return all buffers to user space */
405*85756a06SHans Verkuil 	spin_lock_irqsave(&s->irqlock, flags);
406*85756a06SHans Verkuil 	list_for_each_safe(p, safe, &s->bufs) {
407*85756a06SHans Verkuil 		cb = list_entry(p, struct cobalt_buffer, list);
408*85756a06SHans Verkuil 		list_del(&cb->list);
409*85756a06SHans Verkuil 		vb2_buffer_done(&cb->vb, VB2_BUF_STATE_ERROR);
410*85756a06SHans Verkuil 	}
411*85756a06SHans Verkuil 	spin_unlock_irqrestore(&s->irqlock, flags);
412*85756a06SHans Verkuil 
413*85756a06SHans Verkuil 	if (s->is_audio || s->is_output)
414*85756a06SHans Verkuil 		return;
415*85756a06SHans Verkuil 
416*85756a06SHans Verkuil 	fw = COBALT_CVI_FREEWHEEL(cobalt, rx);
417*85756a06SHans Verkuil 	vmr = COBALT_CVI_VMR(cobalt, rx);
418*85756a06SHans Verkuil 	clkloss = COBALT_CVI_CLK_LOSS(cobalt, rx);
419*85756a06SHans Verkuil 	vmr->control = 0;
420*85756a06SHans Verkuil 	vmr->control = M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK;
421*85756a06SHans Verkuil 	fw->ctrl = 0;
422*85756a06SHans Verkuil 	clkloss->ctrl = 0;
423*85756a06SHans Verkuil }
424*85756a06SHans Verkuil 
425*85756a06SHans Verkuil static const struct vb2_ops cobalt_qops = {
426*85756a06SHans Verkuil 	.queue_setup = cobalt_queue_setup,
427*85756a06SHans Verkuil 	.buf_init = cobalt_buf_init,
428*85756a06SHans Verkuil 	.buf_cleanup = cobalt_buf_cleanup,
429*85756a06SHans Verkuil 	.buf_prepare = cobalt_buf_prepare,
430*85756a06SHans Verkuil 	.buf_queue = cobalt_buf_queue,
431*85756a06SHans Verkuil 	.start_streaming = cobalt_start_streaming,
432*85756a06SHans Verkuil 	.stop_streaming = cobalt_stop_streaming,
433*85756a06SHans Verkuil 	.wait_prepare = vb2_ops_wait_prepare,
434*85756a06SHans Verkuil 	.wait_finish = vb2_ops_wait_finish,
435*85756a06SHans Verkuil };
436*85756a06SHans Verkuil 
437*85756a06SHans Verkuil /* V4L2 ioctls */
438*85756a06SHans Verkuil 
439*85756a06SHans Verkuil #ifdef CONFIG_VIDEO_ADV_DEBUG
440*85756a06SHans Verkuil static int cobalt_cobaltc(struct cobalt *cobalt, unsigned int cmd, void *arg)
441*85756a06SHans Verkuil {
442*85756a06SHans Verkuil 	struct v4l2_dbg_register *regs = arg;
443*85756a06SHans Verkuil 	void __iomem *adrs = cobalt->bar1 + regs->reg;
444*85756a06SHans Verkuil 
445*85756a06SHans Verkuil 	cobalt_info("cobalt_cobaltc: adrs = %p\n", adrs);
446*85756a06SHans Verkuil 
447*85756a06SHans Verkuil 	if (!capable(CAP_SYS_ADMIN))
448*85756a06SHans Verkuil 		return -EPERM;
449*85756a06SHans Verkuil 
450*85756a06SHans Verkuil 	regs->size = 4;
451*85756a06SHans Verkuil 	if (cmd == VIDIOC_DBG_S_REGISTER)
452*85756a06SHans Verkuil 		iowrite32(regs->val, adrs);
453*85756a06SHans Verkuil 	else
454*85756a06SHans Verkuil 		regs->val = ioread32(adrs);
455*85756a06SHans Verkuil 	return 0;
456*85756a06SHans Verkuil }
457*85756a06SHans Verkuil 
458*85756a06SHans Verkuil static int cobalt_g_register(struct file *file, void *priv_fh,
459*85756a06SHans Verkuil 		struct v4l2_dbg_register *reg)
460*85756a06SHans Verkuil {
461*85756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
462*85756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
463*85756a06SHans Verkuil 
464*85756a06SHans Verkuil 	return cobalt_cobaltc(cobalt, VIDIOC_DBG_G_REGISTER, reg);
465*85756a06SHans Verkuil }
466*85756a06SHans Verkuil 
467*85756a06SHans Verkuil static int cobalt_s_register(struct file *file, void *priv_fh,
468*85756a06SHans Verkuil 		const struct v4l2_dbg_register *reg)
469*85756a06SHans Verkuil {
470*85756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
471*85756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
472*85756a06SHans Verkuil 
473*85756a06SHans Verkuil 	return cobalt_cobaltc(cobalt, VIDIOC_DBG_S_REGISTER,
474*85756a06SHans Verkuil 			(struct v4l2_dbg_register *)reg);
475*85756a06SHans Verkuil }
476*85756a06SHans Verkuil #endif
477*85756a06SHans Verkuil 
478*85756a06SHans Verkuil static int cobalt_querycap(struct file *file, void *priv_fh,
479*85756a06SHans Verkuil 				struct v4l2_capability *vcap)
480*85756a06SHans Verkuil {
481*85756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
482*85756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
483*85756a06SHans Verkuil 
484*85756a06SHans Verkuil 	strlcpy(vcap->driver, "cobalt", sizeof(vcap->driver));
485*85756a06SHans Verkuil 	strlcpy(vcap->card, "cobalt", sizeof(vcap->card));
486*85756a06SHans Verkuil 	snprintf(vcap->bus_info, sizeof(vcap->bus_info),
487*85756a06SHans Verkuil 		 "PCIe:%s", pci_name(cobalt->pci_dev));
488*85756a06SHans Verkuil 	vcap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
489*85756a06SHans Verkuil 	if (s->is_output)
490*85756a06SHans Verkuil 		vcap->device_caps |= V4L2_CAP_VIDEO_OUTPUT;
491*85756a06SHans Verkuil 	else
492*85756a06SHans Verkuil 		vcap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
493*85756a06SHans Verkuil 	vcap->capabilities = vcap->device_caps | V4L2_CAP_DEVICE_CAPS |
494*85756a06SHans Verkuil 		V4L2_CAP_VIDEO_CAPTURE;
495*85756a06SHans Verkuil 	if (cobalt->have_hsma_tx)
496*85756a06SHans Verkuil 		vcap->capabilities |= V4L2_CAP_VIDEO_OUTPUT;
497*85756a06SHans Verkuil 	return 0;
498*85756a06SHans Verkuil }
499*85756a06SHans Verkuil 
500*85756a06SHans Verkuil static void cobalt_video_input_status_show(struct cobalt_stream *s)
501*85756a06SHans Verkuil {
502*85756a06SHans Verkuil 	volatile struct m00389_cvi_regmap __iomem *cvi;
503*85756a06SHans Verkuil 	volatile struct m00233_video_measure_regmap __iomem *vmr;
504*85756a06SHans Verkuil 	volatile struct m00473_freewheel_regmap __iomem *fw;
505*85756a06SHans Verkuil 	volatile struct m00479_clk_loss_detector_regmap __iomem *clkloss;
506*85756a06SHans Verkuil 	volatile struct m00235_fdma_packer_regmap __iomem *packer;
507*85756a06SHans Verkuil 	int rx = s->video_channel;
508*85756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
509*85756a06SHans Verkuil 
510*85756a06SHans Verkuil 	cvi = COBALT_CVI(cobalt, rx);
511*85756a06SHans Verkuil 	vmr = COBALT_CVI_VMR(cobalt, rx);
512*85756a06SHans Verkuil 	fw = COBALT_CVI_FREEWHEEL(cobalt, rx);
513*85756a06SHans Verkuil 	clkloss = COBALT_CVI_CLK_LOSS(cobalt, rx);
514*85756a06SHans Verkuil 	packer = COBALT_CVI_PACKER(cobalt, rx);
515*85756a06SHans Verkuil 	cobalt_info("rx%d: cvi resolution: %dx%d\n", rx,
516*85756a06SHans Verkuil 			cvi->frame_width, cvi->frame_height);
517*85756a06SHans Verkuil 	cobalt_info("rx%d: cvi control: %s%s%s\n", rx,
518*85756a06SHans Verkuil 		(cvi->control & M00389_CONTROL_BITMAP_ENABLE_MSK) ?
519*85756a06SHans Verkuil 			"enable " : "disable ",
520*85756a06SHans Verkuil 		(cvi->control & M00389_CONTROL_BITMAP_HSYNC_POLARITY_LOW_MSK) ?
521*85756a06SHans Verkuil 			"HSync- " : "HSync+ ",
522*85756a06SHans Verkuil 		(cvi->control & M00389_CONTROL_BITMAP_VSYNC_POLARITY_LOW_MSK) ?
523*85756a06SHans Verkuil 			"VSync- " : "VSync+ ");
524*85756a06SHans Verkuil 	cobalt_info("rx%d: cvi status: %s%s\n", rx,
525*85756a06SHans Verkuil 		(cvi->status & M00389_STATUS_BITMAP_LOCK_MSK) ?
526*85756a06SHans Verkuil 			"lock " : "no-lock ",
527*85756a06SHans Verkuil 		(cvi->status & M00389_STATUS_BITMAP_ERROR_MSK) ?
528*85756a06SHans Verkuil 			"error " : "no-error ");
529*85756a06SHans Verkuil 
530*85756a06SHans Verkuil 	cobalt_info("rx%d: Measurements: %s%s%s%s%s%s%s\n", rx,
531*85756a06SHans Verkuil 		(vmr->control & M00233_CONTROL_BITMAP_HSYNC_POLARITY_LOW_MSK) ?
532*85756a06SHans Verkuil 			"HSync- " : "HSync+ ",
533*85756a06SHans Verkuil 		(vmr->control & M00233_CONTROL_BITMAP_VSYNC_POLARITY_LOW_MSK) ?
534*85756a06SHans Verkuil 			"VSync- " : "VSync+ ",
535*85756a06SHans Verkuil 		(vmr->control & M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK) ?
536*85756a06SHans Verkuil 			"enabled " : "disabled ",
537*85756a06SHans Verkuil 		(vmr->control & M00233_CONTROL_BITMAP_ENABLE_INTERRUPT_MSK) ?
538*85756a06SHans Verkuil 			"irq-enabled " : "irq-disabled ",
539*85756a06SHans Verkuil 		(vmr->control & M00233_CONTROL_BITMAP_UPDATE_ON_HSYNC_MSK) ?
540*85756a06SHans Verkuil 			"update-on-hsync " : "",
541*85756a06SHans Verkuil 		(vmr->status & M00233_STATUS_BITMAP_HSYNC_TIMEOUT_MSK) ?
542*85756a06SHans Verkuil 			"hsync-timeout " : "",
543*85756a06SHans Verkuil 		(vmr->status & M00233_STATUS_BITMAP_INIT_DONE_MSK) ?
544*85756a06SHans Verkuil 			"init-done" : "");
545*85756a06SHans Verkuil 	cobalt_info("rx%d: irq_status: 0x%02x irq_triggers: 0x%02x\n", rx,
546*85756a06SHans Verkuil 			vmr->irq_status & 0xff, vmr->irq_triggers & 0xff);
547*85756a06SHans Verkuil 	cobalt_info("rx%d: vsync: %d\n", rx, vmr->vsync_time);
548*85756a06SHans Verkuil 	cobalt_info("rx%d: vbp: %d\n", rx, vmr->vback_porch);
549*85756a06SHans Verkuil 	cobalt_info("rx%d: vact: %d\n", rx, vmr->vactive_area);
550*85756a06SHans Verkuil 	cobalt_info("rx%d: vfb: %d\n", rx, vmr->vfront_porch);
551*85756a06SHans Verkuil 	cobalt_info("rx%d: hsync: %d\n", rx, vmr->hsync_time);
552*85756a06SHans Verkuil 	cobalt_info("rx%d: hbp: %d\n", rx, vmr->hback_porch);
553*85756a06SHans Verkuil 	cobalt_info("rx%d: hact: %d\n", rx, vmr->hactive_area);
554*85756a06SHans Verkuil 	cobalt_info("rx%d: hfb: %d\n", rx, vmr->hfront_porch);
555*85756a06SHans Verkuil 	cobalt_info("rx%d: Freewheeling: %s%s%s\n", rx,
556*85756a06SHans Verkuil 		(fw->ctrl & M00473_CTRL_BITMAP_ENABLE_MSK) ?
557*85756a06SHans Verkuil 			"enabled " : "disabled ",
558*85756a06SHans Verkuil 		(fw->ctrl & M00473_CTRL_BITMAP_FORCE_FREEWHEEL_MODE_MSK) ?
559*85756a06SHans Verkuil 			"forced " : "",
560*85756a06SHans Verkuil 		(fw->status & M00473_STATUS_BITMAP_FREEWHEEL_MODE_MSK) ?
561*85756a06SHans Verkuil 			"freewheeling " : "video-passthrough ");
562*85756a06SHans Verkuil 	vmr->irq_status = 0xff;
563*85756a06SHans Verkuil 	cobalt_info("rx%d: Clock Loss Detection: %s%s\n", rx,
564*85756a06SHans Verkuil 		(clkloss->ctrl & M00479_CTRL_BITMAP_ENABLE_MSK) ?
565*85756a06SHans Verkuil 			"enabled " : "disabled ",
566*85756a06SHans Verkuil 		(clkloss->status & M00479_STATUS_BITMAP_CLOCK_MISSING_MSK) ?
567*85756a06SHans Verkuil 			"clock-missing " : "found-clock ");
568*85756a06SHans Verkuil 	cobalt_info("rx%d: Packer: %x\n", rx, packer->control);
569*85756a06SHans Verkuil }
570*85756a06SHans Verkuil 
571*85756a06SHans Verkuil static int cobalt_log_status(struct file *file, void *priv_fh)
572*85756a06SHans Verkuil {
573*85756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
574*85756a06SHans Verkuil 	struct cobalt *cobalt = s->cobalt;
575*85756a06SHans Verkuil 	volatile struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
576*85756a06SHans Verkuil 		COBALT_TX_BASE(cobalt);
577*85756a06SHans Verkuil 	u8 stat;
578*85756a06SHans Verkuil 
579*85756a06SHans Verkuil 	cobalt_info("%s", cobalt->hdl_info);
580*85756a06SHans Verkuil 	cobalt_info("sysctrl: %08x, sysstat: %08x\n",
581*85756a06SHans Verkuil 			cobalt_g_sysctrl(cobalt),
582*85756a06SHans Verkuil 			cobalt_g_sysstat(cobalt));
583*85756a06SHans Verkuil 	cobalt_info("dma channel: %d, video channel: %d\n",
584*85756a06SHans Verkuil 			s->dma_channel, s->video_channel);
585*85756a06SHans Verkuil 	cobalt_pcie_status_show(cobalt);
586*85756a06SHans Verkuil 	cobalt_cpld_status(cobalt);
587*85756a06SHans Verkuil 	cobalt_irq_log_status(cobalt);
588*85756a06SHans Verkuil 	v4l2_subdev_call(s->sd, core, log_status);
589*85756a06SHans Verkuil 	if (!s->is_output) {
590*85756a06SHans Verkuil 		cobalt_video_input_status_show(s);
591*85756a06SHans Verkuil 		return 0;
592*85756a06SHans Verkuil 	}
593*85756a06SHans Verkuil 
594*85756a06SHans Verkuil 	stat = vo->rd_status;
595*85756a06SHans Verkuil 
596*85756a06SHans Verkuil 	cobalt_info("tx: status: %s%s\n",
597*85756a06SHans Verkuil 		(stat & M00514_RD_STATUS_BITMAP_FLOW_CTRL_NO_DATA_ERROR_MSK) ?
598*85756a06SHans Verkuil 			"no_data " : "",
599*85756a06SHans Verkuil 		(stat & M00514_RD_STATUS_BITMAP_READY_BUFFER_FULL_MSK) ?
600*85756a06SHans Verkuil 			"ready_buffer_full " : "");
601*85756a06SHans Verkuil 	cobalt_info("tx: evcnt: %d\n", vo->rd_evcnt_count);
602*85756a06SHans Verkuil 	return 0;
603*85756a06SHans Verkuil }
604*85756a06SHans Verkuil 
605*85756a06SHans Verkuil static int cobalt_enum_dv_timings(struct file *file, void *priv_fh,
606*85756a06SHans Verkuil 				    struct v4l2_enum_dv_timings *timings)
607*85756a06SHans Verkuil {
608*85756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
609*85756a06SHans Verkuil 
610*85756a06SHans Verkuil 	if (s->input == 1) {
611*85756a06SHans Verkuil 		if (timings->index)
612*85756a06SHans Verkuil 			return -EINVAL;
613*85756a06SHans Verkuil 		memset(timings->reserved, 0, sizeof(timings->reserved));
614*85756a06SHans Verkuil 		timings->timings = cea1080p60;
615*85756a06SHans Verkuil 		return 0;
616*85756a06SHans Verkuil 	}
617*85756a06SHans Verkuil 	timings->pad = 0;
618*85756a06SHans Verkuil 	return v4l2_subdev_call(s->sd,
619*85756a06SHans Verkuil 			pad, enum_dv_timings, timings);
620*85756a06SHans Verkuil }
621*85756a06SHans Verkuil 
622*85756a06SHans Verkuil static int cobalt_s_dv_timings(struct file *file, void *priv_fh,
623*85756a06SHans Verkuil 				    struct v4l2_dv_timings *timings)
624*85756a06SHans Verkuil {
625*85756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
626*85756a06SHans Verkuil 	int err;
627*85756a06SHans Verkuil 
628*85756a06SHans Verkuil 	if (vb2_is_busy(&s->q))
629*85756a06SHans Verkuil 		return -EBUSY;
630*85756a06SHans Verkuil 
631*85756a06SHans Verkuil 	if (s->input == 1) {
632*85756a06SHans Verkuil 		*timings = cea1080p60;
633*85756a06SHans Verkuil 		return 0;
634*85756a06SHans Verkuil 	}
635*85756a06SHans Verkuil 	err = v4l2_subdev_call(s->sd,
636*85756a06SHans Verkuil 			video, s_dv_timings, timings);
637*85756a06SHans Verkuil 	if (!err) {
638*85756a06SHans Verkuil 		s->timings = *timings;
639*85756a06SHans Verkuil 		s->width = timings->bt.width;
640*85756a06SHans Verkuil 		s->height = timings->bt.height;
641*85756a06SHans Verkuil 		s->stride = timings->bt.width * s->bpp;
642*85756a06SHans Verkuil 	}
643*85756a06SHans Verkuil 	return err;
644*85756a06SHans Verkuil }
645*85756a06SHans Verkuil 
646*85756a06SHans Verkuil static int cobalt_g_dv_timings(struct file *file, void *priv_fh,
647*85756a06SHans Verkuil 				    struct v4l2_dv_timings *timings)
648*85756a06SHans Verkuil {
649*85756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
650*85756a06SHans Verkuil 
651*85756a06SHans Verkuil 	if (s->input == 1) {
652*85756a06SHans Verkuil 		*timings = cea1080p60;
653*85756a06SHans Verkuil 		return 0;
654*85756a06SHans Verkuil 	}
655*85756a06SHans Verkuil 	return v4l2_subdev_call(s->sd,
656*85756a06SHans Verkuil 			video, g_dv_timings, timings);
657*85756a06SHans Verkuil }
658*85756a06SHans Verkuil 
659*85756a06SHans Verkuil static int cobalt_query_dv_timings(struct file *file, void *priv_fh,
660*85756a06SHans Verkuil 				    struct v4l2_dv_timings *timings)
661*85756a06SHans Verkuil {
662*85756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
663*85756a06SHans Verkuil 
664*85756a06SHans Verkuil 	if (s->input == 1) {
665*85756a06SHans Verkuil 		*timings = cea1080p60;
666*85756a06SHans Verkuil 		return 0;
667*85756a06SHans Verkuil 	}
668*85756a06SHans Verkuil 	return v4l2_subdev_call(s->sd,
669*85756a06SHans Verkuil 			video, query_dv_timings, timings);
670*85756a06SHans Verkuil }
671*85756a06SHans Verkuil 
672*85756a06SHans Verkuil static int cobalt_dv_timings_cap(struct file *file, void *priv_fh,
673*85756a06SHans Verkuil 				    struct v4l2_dv_timings_cap *cap)
674*85756a06SHans Verkuil {
675*85756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
676*85756a06SHans Verkuil 
677*85756a06SHans Verkuil 	cap->pad = 0;
678*85756a06SHans Verkuil 	return v4l2_subdev_call(s->sd,
679*85756a06SHans Verkuil 			pad, dv_timings_cap, cap);
680*85756a06SHans Verkuil }
681*85756a06SHans Verkuil 
682*85756a06SHans Verkuil static int cobalt_enum_fmt_vid_cap(struct file *file, void *priv_fh,
683*85756a06SHans Verkuil 		struct v4l2_fmtdesc *f)
684*85756a06SHans Verkuil {
685*85756a06SHans Verkuil 	switch (f->index) {
686*85756a06SHans Verkuil 	case 0:
687*85756a06SHans Verkuil 		strlcpy(f->description, "YUV 4:2:2", sizeof(f->description));
688*85756a06SHans Verkuil 		f->pixelformat = V4L2_PIX_FMT_YUYV;
689*85756a06SHans Verkuil 		break;
690*85756a06SHans Verkuil 	case 1:
691*85756a06SHans Verkuil 		strlcpy(f->description, "RGB24", sizeof(f->description));
692*85756a06SHans Verkuil 		f->pixelformat = V4L2_PIX_FMT_RGB24;
693*85756a06SHans Verkuil 		break;
694*85756a06SHans Verkuil 	case 2:
695*85756a06SHans Verkuil 		strlcpy(f->description, "RGB32", sizeof(f->description));
696*85756a06SHans Verkuil 		f->pixelformat = V4L2_PIX_FMT_BGR32;
697*85756a06SHans Verkuil 		break;
698*85756a06SHans Verkuil 	default:
699*85756a06SHans Verkuil 		return -EINVAL;
700*85756a06SHans Verkuil 	}
701*85756a06SHans Verkuil 
702*85756a06SHans Verkuil 	return 0;
703*85756a06SHans Verkuil }
704*85756a06SHans Verkuil 
705*85756a06SHans Verkuil static int cobalt_g_fmt_vid_cap(struct file *file, void *priv_fh,
706*85756a06SHans Verkuil 		struct v4l2_format *f)
707*85756a06SHans Verkuil {
708*85756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
709*85756a06SHans Verkuil 	struct v4l2_pix_format *pix = &f->fmt.pix;
710*85756a06SHans Verkuil 	struct v4l2_subdev_format sd_fmt;
711*85756a06SHans Verkuil 
712*85756a06SHans Verkuil 	pix->width = s->width;
713*85756a06SHans Verkuil 	pix->height = s->height;
714*85756a06SHans Verkuil 	pix->bytesperline = s->stride;
715*85756a06SHans Verkuil 	pix->field = V4L2_FIELD_NONE;
716*85756a06SHans Verkuil 
717*85756a06SHans Verkuil 	if (s->input == 1) {
718*85756a06SHans Verkuil 		pix->colorspace = V4L2_COLORSPACE_SRGB;
719*85756a06SHans Verkuil 	} else {
720*85756a06SHans Verkuil 		sd_fmt.pad = s->pad_source;
721*85756a06SHans Verkuil 		sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
722*85756a06SHans Verkuil 		v4l2_subdev_call(s->sd, pad, get_fmt, NULL, &sd_fmt);
723*85756a06SHans Verkuil 		v4l2_fill_pix_format(pix, &sd_fmt.format);
724*85756a06SHans Verkuil 		pix->colorspace = sd_fmt.format.colorspace;
725*85756a06SHans Verkuil 		pix->ycbcr_enc = sd_fmt.format.ycbcr_enc;
726*85756a06SHans Verkuil 		pix->quantization = sd_fmt.format.quantization;
727*85756a06SHans Verkuil 	}
728*85756a06SHans Verkuil 
729*85756a06SHans Verkuil 	pix->pixelformat = s->pixfmt;
730*85756a06SHans Verkuil 	pix->sizeimage = pix->bytesperline * pix->height;
731*85756a06SHans Verkuil 
732*85756a06SHans Verkuil 	return 0;
733*85756a06SHans Verkuil }
734*85756a06SHans Verkuil 
735*85756a06SHans Verkuil static int cobalt_try_fmt_vid_cap(struct file *file, void *priv_fh,
736*85756a06SHans Verkuil 		struct v4l2_format *f)
737*85756a06SHans Verkuil {
738*85756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
739*85756a06SHans Verkuil 	struct v4l2_pix_format *pix = &f->fmt.pix;
740*85756a06SHans Verkuil 	struct v4l2_subdev_format sd_fmt;
741*85756a06SHans Verkuil 
742*85756a06SHans Verkuil 	/* Check for min (QCIF) and max (Full HD) size */
743*85756a06SHans Verkuil 	if ((pix->width < 176) || (pix->height < 144)) {
744*85756a06SHans Verkuil 		pix->width = 176;
745*85756a06SHans Verkuil 		pix->height = 144;
746*85756a06SHans Verkuil 	}
747*85756a06SHans Verkuil 
748*85756a06SHans Verkuil 	if ((pix->width > 1920) || (pix->height > 1080)) {
749*85756a06SHans Verkuil 		pix->width = 1920;
750*85756a06SHans Verkuil 		pix->height = 1080;
751*85756a06SHans Verkuil 	}
752*85756a06SHans Verkuil 
753*85756a06SHans Verkuil 	/* Make width multiple of 4 */
754*85756a06SHans Verkuil 	pix->width &= ~0x3;
755*85756a06SHans Verkuil 
756*85756a06SHans Verkuil 	/* Make height multiple of 2 */
757*85756a06SHans Verkuil 	pix->height &= ~0x1;
758*85756a06SHans Verkuil 
759*85756a06SHans Verkuil 	if (s->input == 1) {
760*85756a06SHans Verkuil 		/* Generator => fixed format only */
761*85756a06SHans Verkuil 		pix->width = 1920;
762*85756a06SHans Verkuil 		pix->height = 1080;
763*85756a06SHans Verkuil 		pix->colorspace = V4L2_COLORSPACE_SRGB;
764*85756a06SHans Verkuil 	} else {
765*85756a06SHans Verkuil 		sd_fmt.pad = s->pad_source;
766*85756a06SHans Verkuil 		sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
767*85756a06SHans Verkuil 		v4l2_subdev_call(s->sd, pad, get_fmt, NULL, &sd_fmt);
768*85756a06SHans Verkuil 		v4l2_fill_pix_format(pix, &sd_fmt.format);
769*85756a06SHans Verkuil 		pix->colorspace = sd_fmt.format.colorspace;
770*85756a06SHans Verkuil 		pix->ycbcr_enc = sd_fmt.format.ycbcr_enc;
771*85756a06SHans Verkuil 		pix->quantization = sd_fmt.format.quantization;
772*85756a06SHans Verkuil 	}
773*85756a06SHans Verkuil 
774*85756a06SHans Verkuil 	switch (pix->pixelformat) {
775*85756a06SHans Verkuil 	case V4L2_PIX_FMT_YUYV:
776*85756a06SHans Verkuil 	default:
777*85756a06SHans Verkuil 		pix->bytesperline = max(pix->bytesperline & ~0x3,
778*85756a06SHans Verkuil 				pix->width * COBALT_BYTES_PER_PIXEL_YUYV);
779*85756a06SHans Verkuil 		pix->pixelformat = V4L2_PIX_FMT_YUYV;
780*85756a06SHans Verkuil 		break;
781*85756a06SHans Verkuil 	case V4L2_PIX_FMT_RGB24:
782*85756a06SHans Verkuil 		pix->bytesperline = max(pix->bytesperline & ~0x3,
783*85756a06SHans Verkuil 				pix->width * COBALT_BYTES_PER_PIXEL_RGB24);
784*85756a06SHans Verkuil 		break;
785*85756a06SHans Verkuil 	case V4L2_PIX_FMT_BGR32:
786*85756a06SHans Verkuil 		pix->bytesperline = max(pix->bytesperline & ~0x3,
787*85756a06SHans Verkuil 				pix->width * COBALT_BYTES_PER_PIXEL_RGB32);
788*85756a06SHans Verkuil 		break;
789*85756a06SHans Verkuil 	}
790*85756a06SHans Verkuil 
791*85756a06SHans Verkuil 	pix->sizeimage = pix->bytesperline * pix->height;
792*85756a06SHans Verkuil 	pix->field = V4L2_FIELD_NONE;
793*85756a06SHans Verkuil 	pix->priv = 0;
794*85756a06SHans Verkuil 
795*85756a06SHans Verkuil 	return 0;
796*85756a06SHans Verkuil }
797*85756a06SHans Verkuil 
798*85756a06SHans Verkuil static int cobalt_s_fmt_vid_cap(struct file *file, void *priv_fh,
799*85756a06SHans Verkuil 		struct v4l2_format *f)
800*85756a06SHans Verkuil {
801*85756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
802*85756a06SHans Verkuil 	struct v4l2_pix_format *pix = &f->fmt.pix;
803*85756a06SHans Verkuil 
804*85756a06SHans Verkuil 	if (vb2_is_busy(&s->q))
805*85756a06SHans Verkuil 		return -EBUSY;
806*85756a06SHans Verkuil 
807*85756a06SHans Verkuil 	if (cobalt_try_fmt_vid_cap(file, priv_fh, f))
808*85756a06SHans Verkuil 		return -EINVAL;
809*85756a06SHans Verkuil 
810*85756a06SHans Verkuil 	s->width = pix->width;
811*85756a06SHans Verkuil 	s->height = pix->height;
812*85756a06SHans Verkuil 	s->stride = pix->bytesperline;
813*85756a06SHans Verkuil 	switch (pix->pixelformat) {
814*85756a06SHans Verkuil 	case V4L2_PIX_FMT_YUYV:
815*85756a06SHans Verkuil 		s->bpp = COBALT_BYTES_PER_PIXEL_YUYV;
816*85756a06SHans Verkuil 		break;
817*85756a06SHans Verkuil 	case V4L2_PIX_FMT_RGB24:
818*85756a06SHans Verkuil 		s->bpp = COBALT_BYTES_PER_PIXEL_RGB24;
819*85756a06SHans Verkuil 		break;
820*85756a06SHans Verkuil 	case V4L2_PIX_FMT_BGR32:
821*85756a06SHans Verkuil 		s->bpp = COBALT_BYTES_PER_PIXEL_RGB32;
822*85756a06SHans Verkuil 		break;
823*85756a06SHans Verkuil 	default:
824*85756a06SHans Verkuil 		return -EINVAL;
825*85756a06SHans Verkuil 	}
826*85756a06SHans Verkuil 	s->pixfmt = pix->pixelformat;
827*85756a06SHans Verkuil 	cobalt_enable_input(s);
828*85756a06SHans Verkuil 
829*85756a06SHans Verkuil 	return 0;
830*85756a06SHans Verkuil }
831*85756a06SHans Verkuil 
832*85756a06SHans Verkuil static int cobalt_try_fmt_vid_out(struct file *file, void *priv_fh,
833*85756a06SHans Verkuil 		struct v4l2_format *f)
834*85756a06SHans Verkuil {
835*85756a06SHans Verkuil 	struct v4l2_pix_format *pix = &f->fmt.pix;
836*85756a06SHans Verkuil 
837*85756a06SHans Verkuil 	/* Check for min (QCIF) and max (Full HD) size */
838*85756a06SHans Verkuil 	if ((pix->width < 176) || (pix->height < 144)) {
839*85756a06SHans Verkuil 		pix->width = 176;
840*85756a06SHans Verkuil 		pix->height = 144;
841*85756a06SHans Verkuil 	}
842*85756a06SHans Verkuil 
843*85756a06SHans Verkuil 	if ((pix->width > 1920) || (pix->height > 1080)) {
844*85756a06SHans Verkuil 		pix->width = 1920;
845*85756a06SHans Verkuil 		pix->height = 1080;
846*85756a06SHans Verkuil 	}
847*85756a06SHans Verkuil 
848*85756a06SHans Verkuil 	/* Make width multiple of 4 */
849*85756a06SHans Verkuil 	pix->width &= ~0x3;
850*85756a06SHans Verkuil 
851*85756a06SHans Verkuil 	/* Make height multiple of 2 */
852*85756a06SHans Verkuil 	pix->height &= ~0x1;
853*85756a06SHans Verkuil 
854*85756a06SHans Verkuil 	switch (pix->pixelformat) {
855*85756a06SHans Verkuil 	case V4L2_PIX_FMT_YUYV:
856*85756a06SHans Verkuil 	default:
857*85756a06SHans Verkuil 		pix->bytesperline = max(pix->bytesperline & ~0x3,
858*85756a06SHans Verkuil 				pix->width * COBALT_BYTES_PER_PIXEL_YUYV);
859*85756a06SHans Verkuil 		pix->pixelformat = V4L2_PIX_FMT_YUYV;
860*85756a06SHans Verkuil 		break;
861*85756a06SHans Verkuil 	case V4L2_PIX_FMT_BGR32:
862*85756a06SHans Verkuil 		pix->bytesperline = max(pix->bytesperline & ~0x3,
863*85756a06SHans Verkuil 				pix->width * COBALT_BYTES_PER_PIXEL_RGB32);
864*85756a06SHans Verkuil 		break;
865*85756a06SHans Verkuil 	}
866*85756a06SHans Verkuil 
867*85756a06SHans Verkuil 	pix->sizeimage = pix->bytesperline * pix->height;
868*85756a06SHans Verkuil 	pix->field = V4L2_FIELD_NONE;
869*85756a06SHans Verkuil 
870*85756a06SHans Verkuil 	return 0;
871*85756a06SHans Verkuil }
872*85756a06SHans Verkuil 
873*85756a06SHans Verkuil static int cobalt_g_fmt_vid_out(struct file *file, void *priv_fh,
874*85756a06SHans Verkuil 		struct v4l2_format *f)
875*85756a06SHans Verkuil {
876*85756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
877*85756a06SHans Verkuil 	struct v4l2_pix_format *pix = &f->fmt.pix;
878*85756a06SHans Verkuil 
879*85756a06SHans Verkuil 	pix->width = s->width;
880*85756a06SHans Verkuil 	pix->height = s->height;
881*85756a06SHans Verkuil 	pix->bytesperline = s->stride;
882*85756a06SHans Verkuil 	pix->field = V4L2_FIELD_NONE;
883*85756a06SHans Verkuil 	pix->pixelformat = s->pixfmt;
884*85756a06SHans Verkuil 	pix->colorspace = s->colorspace;
885*85756a06SHans Verkuil 	pix->ycbcr_enc = s->ycbcr_enc;
886*85756a06SHans Verkuil 	pix->quantization = s->quantization;
887*85756a06SHans Verkuil 	pix->sizeimage = pix->bytesperline * pix->height;
888*85756a06SHans Verkuil 
889*85756a06SHans Verkuil 	return 0;
890*85756a06SHans Verkuil }
891*85756a06SHans Verkuil 
892*85756a06SHans Verkuil static int cobalt_enum_fmt_vid_out(struct file *file, void *priv_fh,
893*85756a06SHans Verkuil 		struct v4l2_fmtdesc *f)
894*85756a06SHans Verkuil {
895*85756a06SHans Verkuil 	switch (f->index) {
896*85756a06SHans Verkuil 	case 0:
897*85756a06SHans Verkuil 		strlcpy(f->description, "YUV 4:2:2", sizeof(f->description));
898*85756a06SHans Verkuil 		f->pixelformat = V4L2_PIX_FMT_YUYV;
899*85756a06SHans Verkuil 		break;
900*85756a06SHans Verkuil 	case 1:
901*85756a06SHans Verkuil 		strlcpy(f->description, "RGB32", sizeof(f->description));
902*85756a06SHans Verkuil 		f->pixelformat = V4L2_PIX_FMT_BGR32;
903*85756a06SHans Verkuil 		break;
904*85756a06SHans Verkuil 	default:
905*85756a06SHans Verkuil 		return -EINVAL;
906*85756a06SHans Verkuil 	}
907*85756a06SHans Verkuil 
908*85756a06SHans Verkuil 	return 0;
909*85756a06SHans Verkuil }
910*85756a06SHans Verkuil 
911*85756a06SHans Verkuil static int cobalt_s_fmt_vid_out(struct file *file, void *priv_fh,
912*85756a06SHans Verkuil 		struct v4l2_format *f)
913*85756a06SHans Verkuil {
914*85756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
915*85756a06SHans Verkuil 	struct v4l2_pix_format *pix = &f->fmt.pix;
916*85756a06SHans Verkuil 	struct v4l2_subdev_format sd_fmt = { 0 };
917*85756a06SHans Verkuil 
918*85756a06SHans Verkuil 	if (cobalt_try_fmt_vid_out(file, priv_fh, f))
919*85756a06SHans Verkuil 		return -EINVAL;
920*85756a06SHans Verkuil 
921*85756a06SHans Verkuil 	if (vb2_is_busy(&s->q) && (pix->pixelformat != s->pixfmt ||
922*85756a06SHans Verkuil 	    pix->width != s->width || pix->height != s->height ||
923*85756a06SHans Verkuil 	    pix->bytesperline != s->stride))
924*85756a06SHans Verkuil 		return -EBUSY;
925*85756a06SHans Verkuil 
926*85756a06SHans Verkuil 	switch (pix->pixelformat) {
927*85756a06SHans Verkuil 	case V4L2_PIX_FMT_YUYV:
928*85756a06SHans Verkuil 		s->bpp = COBALT_BYTES_PER_PIXEL_YUYV;
929*85756a06SHans Verkuil 		break;
930*85756a06SHans Verkuil 	case V4L2_PIX_FMT_BGR32:
931*85756a06SHans Verkuil 		s->bpp = COBALT_BYTES_PER_PIXEL_RGB32;
932*85756a06SHans Verkuil 		break;
933*85756a06SHans Verkuil 	default:
934*85756a06SHans Verkuil 		return -EINVAL;
935*85756a06SHans Verkuil 	}
936*85756a06SHans Verkuil 	s->width = pix->width;
937*85756a06SHans Verkuil 	s->height = pix->height;
938*85756a06SHans Verkuil 	s->stride = pix->bytesperline;
939*85756a06SHans Verkuil 	s->pixfmt = pix->pixelformat;
940*85756a06SHans Verkuil 	s->colorspace = pix->colorspace;
941*85756a06SHans Verkuil 	s->ycbcr_enc = pix->ycbcr_enc;
942*85756a06SHans Verkuil 	s->quantization = pix->quantization;
943*85756a06SHans Verkuil 	sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
944*85756a06SHans Verkuil 	v4l2_subdev_call(s->sd, pad, get_fmt, NULL, &sd_fmt);
945*85756a06SHans Verkuil 	sd_fmt.format.colorspace = pix->colorspace;
946*85756a06SHans Verkuil 	sd_fmt.format.ycbcr_enc = pix->ycbcr_enc;
947*85756a06SHans Verkuil 	sd_fmt.format.quantization = pix->quantization;
948*85756a06SHans Verkuil 	v4l2_subdev_call(s->sd, pad, set_fmt, NULL, &sd_fmt);
949*85756a06SHans Verkuil 	return 0;
950*85756a06SHans Verkuil }
951*85756a06SHans Verkuil 
952*85756a06SHans Verkuil static int cobalt_enum_input(struct file *file, void *priv_fh,
953*85756a06SHans Verkuil 				 struct v4l2_input *inp)
954*85756a06SHans Verkuil {
955*85756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
956*85756a06SHans Verkuil 
957*85756a06SHans Verkuil 	if (inp->index > 1)
958*85756a06SHans Verkuil 		return -EINVAL;
959*85756a06SHans Verkuil 	if (inp->index == 0)
960*85756a06SHans Verkuil 		snprintf(inp->name, sizeof(inp->name),
961*85756a06SHans Verkuil 				"HDMI-%d", s->video_channel);
962*85756a06SHans Verkuil 	else
963*85756a06SHans Verkuil 		snprintf(inp->name, sizeof(inp->name),
964*85756a06SHans Verkuil 				"Generator-%d", s->video_channel);
965*85756a06SHans Verkuil 	inp->type = V4L2_INPUT_TYPE_CAMERA;
966*85756a06SHans Verkuil 	inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
967*85756a06SHans Verkuil 	if (inp->index == 1)
968*85756a06SHans Verkuil 		return 0;
969*85756a06SHans Verkuil 	return v4l2_subdev_call(s->sd,
970*85756a06SHans Verkuil 			video, g_input_status, &inp->status);
971*85756a06SHans Verkuil }
972*85756a06SHans Verkuil 
973*85756a06SHans Verkuil static int cobalt_g_input(struct file *file, void *priv_fh, unsigned int *i)
974*85756a06SHans Verkuil {
975*85756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
976*85756a06SHans Verkuil 
977*85756a06SHans Verkuil 	*i = s->input;
978*85756a06SHans Verkuil 	return 0;
979*85756a06SHans Verkuil }
980*85756a06SHans Verkuil 
981*85756a06SHans Verkuil static int cobalt_s_input(struct file *file, void *priv_fh, unsigned int i)
982*85756a06SHans Verkuil {
983*85756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
984*85756a06SHans Verkuil 
985*85756a06SHans Verkuil 	if (i >= 2)
986*85756a06SHans Verkuil 		return -EINVAL;
987*85756a06SHans Verkuil 	if (vb2_is_busy(&s->q))
988*85756a06SHans Verkuil 		return -EBUSY;
989*85756a06SHans Verkuil 	s->input = i;
990*85756a06SHans Verkuil 
991*85756a06SHans Verkuil 	cobalt_enable_input(s);
992*85756a06SHans Verkuil 
993*85756a06SHans Verkuil 	if (s->input == 1) /* Test Pattern Generator */
994*85756a06SHans Verkuil 		return 0;
995*85756a06SHans Verkuil 
996*85756a06SHans Verkuil 	return v4l2_subdev_call(s->sd, video, s_routing,
997*85756a06SHans Verkuil 			ADV76XX_PAD_HDMI_PORT_A, 0, 0);
998*85756a06SHans Verkuil }
999*85756a06SHans Verkuil 
1000*85756a06SHans Verkuil static int cobalt_enum_output(struct file *file, void *priv_fh,
1001*85756a06SHans Verkuil 				 struct v4l2_output *out)
1002*85756a06SHans Verkuil {
1003*85756a06SHans Verkuil 	if (out->index)
1004*85756a06SHans Verkuil 		return -EINVAL;
1005*85756a06SHans Verkuil 	snprintf(out->name, sizeof(out->name), "HDMI-%d", out->index);
1006*85756a06SHans Verkuil 	out->type = V4L2_OUTPUT_TYPE_ANALOG;
1007*85756a06SHans Verkuil 	out->capabilities = V4L2_OUT_CAP_DV_TIMINGS;
1008*85756a06SHans Verkuil 	return 0;
1009*85756a06SHans Verkuil }
1010*85756a06SHans Verkuil 
1011*85756a06SHans Verkuil static int cobalt_g_output(struct file *file, void *priv_fh, unsigned int *i)
1012*85756a06SHans Verkuil {
1013*85756a06SHans Verkuil 	*i = 0;
1014*85756a06SHans Verkuil 	return 0;
1015*85756a06SHans Verkuil }
1016*85756a06SHans Verkuil 
1017*85756a06SHans Verkuil static int cobalt_s_output(struct file *file, void *priv_fh, unsigned int i)
1018*85756a06SHans Verkuil {
1019*85756a06SHans Verkuil 	return i ? -EINVAL : 0;
1020*85756a06SHans Verkuil }
1021*85756a06SHans Verkuil 
1022*85756a06SHans Verkuil static int cobalt_g_edid(struct file *file, void *fh, struct v4l2_edid *edid)
1023*85756a06SHans Verkuil {
1024*85756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
1025*85756a06SHans Verkuil 	u32 pad = edid->pad;
1026*85756a06SHans Verkuil 	int ret;
1027*85756a06SHans Verkuil 
1028*85756a06SHans Verkuil 	if (edid->pad >= (s->is_output ? 1 : 2))
1029*85756a06SHans Verkuil 		return -EINVAL;
1030*85756a06SHans Verkuil 	edid->pad = 0;
1031*85756a06SHans Verkuil 	ret = v4l2_subdev_call(s->sd, pad, get_edid, edid);
1032*85756a06SHans Verkuil 	edid->pad = pad;
1033*85756a06SHans Verkuil 	return ret;
1034*85756a06SHans Verkuil }
1035*85756a06SHans Verkuil 
1036*85756a06SHans Verkuil static int cobalt_s_edid(struct file *file, void *fh, struct v4l2_edid *edid)
1037*85756a06SHans Verkuil {
1038*85756a06SHans Verkuil 	struct cobalt_stream *s = video_drvdata(file);
1039*85756a06SHans Verkuil 	u32 pad = edid->pad;
1040*85756a06SHans Verkuil 	int ret;
1041*85756a06SHans Verkuil 
1042*85756a06SHans Verkuil 	if (edid->pad >= 2)
1043*85756a06SHans Verkuil 		return -EINVAL;
1044*85756a06SHans Verkuil 	edid->pad = 0;
1045*85756a06SHans Verkuil 	ret = v4l2_subdev_call(s->sd, pad, set_edid, edid);
1046*85756a06SHans Verkuil 	edid->pad = pad;
1047*85756a06SHans Verkuil 	return ret;
1048*85756a06SHans Verkuil }
1049*85756a06SHans Verkuil 
1050*85756a06SHans Verkuil static int cobalt_subscribe_event(struct v4l2_fh *fh,
1051*85756a06SHans Verkuil 				  const struct v4l2_event_subscription *sub)
1052*85756a06SHans Verkuil {
1053*85756a06SHans Verkuil 	switch (sub->type) {
1054*85756a06SHans Verkuil 	case V4L2_EVENT_SOURCE_CHANGE:
1055*85756a06SHans Verkuil 		return v4l2_event_subscribe(fh, sub, 4, NULL);
1056*85756a06SHans Verkuil 	}
1057*85756a06SHans Verkuil 	return v4l2_ctrl_subscribe_event(fh, sub);
1058*85756a06SHans Verkuil }
1059*85756a06SHans Verkuil 
1060*85756a06SHans Verkuil static int cobalt_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1061*85756a06SHans Verkuil {
1062*85756a06SHans Verkuil 	if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1063*85756a06SHans Verkuil 		return -EINVAL;
1064*85756a06SHans Verkuil 	a->parm.capture.timeperframe.numerator = 1;
1065*85756a06SHans Verkuil 	a->parm.capture.timeperframe.denominator = 60;
1066*85756a06SHans Verkuil 	a->parm.capture.readbuffers = 3;
1067*85756a06SHans Verkuil 	return 0;
1068*85756a06SHans Verkuil }
1069*85756a06SHans Verkuil 
1070*85756a06SHans Verkuil static const struct v4l2_ioctl_ops cobalt_ioctl_ops = {
1071*85756a06SHans Verkuil 	.vidioc_querycap		= cobalt_querycap,
1072*85756a06SHans Verkuil 	.vidioc_g_parm			= cobalt_g_parm,
1073*85756a06SHans Verkuil 	.vidioc_log_status		= cobalt_log_status,
1074*85756a06SHans Verkuil 	.vidioc_streamon		= vb2_ioctl_streamon,
1075*85756a06SHans Verkuil 	.vidioc_streamoff		= vb2_ioctl_streamoff,
1076*85756a06SHans Verkuil 	.vidioc_enum_input		= cobalt_enum_input,
1077*85756a06SHans Verkuil 	.vidioc_g_input			= cobalt_g_input,
1078*85756a06SHans Verkuil 	.vidioc_s_input			= cobalt_s_input,
1079*85756a06SHans Verkuil 	.vidioc_enum_fmt_vid_cap	= cobalt_enum_fmt_vid_cap,
1080*85756a06SHans Verkuil 	.vidioc_g_fmt_vid_cap		= cobalt_g_fmt_vid_cap,
1081*85756a06SHans Verkuil 	.vidioc_s_fmt_vid_cap		= cobalt_s_fmt_vid_cap,
1082*85756a06SHans Verkuil 	.vidioc_try_fmt_vid_cap		= cobalt_try_fmt_vid_cap,
1083*85756a06SHans Verkuil 	.vidioc_enum_output		= cobalt_enum_output,
1084*85756a06SHans Verkuil 	.vidioc_g_output		= cobalt_g_output,
1085*85756a06SHans Verkuil 	.vidioc_s_output		= cobalt_s_output,
1086*85756a06SHans Verkuil 	.vidioc_enum_fmt_vid_out	= cobalt_enum_fmt_vid_out,
1087*85756a06SHans Verkuil 	.vidioc_g_fmt_vid_out		= cobalt_g_fmt_vid_out,
1088*85756a06SHans Verkuil 	.vidioc_s_fmt_vid_out		= cobalt_s_fmt_vid_out,
1089*85756a06SHans Verkuil 	.vidioc_try_fmt_vid_out		= cobalt_try_fmt_vid_out,
1090*85756a06SHans Verkuil 	.vidioc_s_dv_timings		= cobalt_s_dv_timings,
1091*85756a06SHans Verkuil 	.vidioc_g_dv_timings		= cobalt_g_dv_timings,
1092*85756a06SHans Verkuil 	.vidioc_query_dv_timings	= cobalt_query_dv_timings,
1093*85756a06SHans Verkuil 	.vidioc_enum_dv_timings		= cobalt_enum_dv_timings,
1094*85756a06SHans Verkuil 	.vidioc_dv_timings_cap		= cobalt_dv_timings_cap,
1095*85756a06SHans Verkuil 	.vidioc_g_edid			= cobalt_g_edid,
1096*85756a06SHans Verkuil 	.vidioc_s_edid			= cobalt_s_edid,
1097*85756a06SHans Verkuil 	.vidioc_subscribe_event		= cobalt_subscribe_event,
1098*85756a06SHans Verkuil 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
1099*85756a06SHans Verkuil 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
1100*85756a06SHans Verkuil 	.vidioc_create_bufs		= vb2_ioctl_create_bufs,
1101*85756a06SHans Verkuil 	.vidioc_querybuf		= vb2_ioctl_querybuf,
1102*85756a06SHans Verkuil 	.vidioc_qbuf			= vb2_ioctl_qbuf,
1103*85756a06SHans Verkuil 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
1104*85756a06SHans Verkuil 	.vidioc_expbuf			= vb2_ioctl_expbuf,
1105*85756a06SHans Verkuil #ifdef CONFIG_VIDEO_ADV_DEBUG
1106*85756a06SHans Verkuil 	.vidioc_g_register              = cobalt_g_register,
1107*85756a06SHans Verkuil 	.vidioc_s_register              = cobalt_s_register,
1108*85756a06SHans Verkuil #endif
1109*85756a06SHans Verkuil };
1110*85756a06SHans Verkuil 
1111*85756a06SHans Verkuil static const struct v4l2_ioctl_ops cobalt_ioctl_empty_ops = {
1112*85756a06SHans Verkuil #ifdef CONFIG_VIDEO_ADV_DEBUG
1113*85756a06SHans Verkuil 	.vidioc_g_register              = cobalt_g_register,
1114*85756a06SHans Verkuil 	.vidioc_s_register              = cobalt_s_register,
1115*85756a06SHans Verkuil #endif
1116*85756a06SHans Verkuil };
1117*85756a06SHans Verkuil 
1118*85756a06SHans Verkuil /* Register device nodes */
1119*85756a06SHans Verkuil 
1120*85756a06SHans Verkuil static const struct v4l2_file_operations cobalt_fops = {
1121*85756a06SHans Verkuil 	.owner = THIS_MODULE,
1122*85756a06SHans Verkuil 	.open = v4l2_fh_open,
1123*85756a06SHans Verkuil 	.unlocked_ioctl = video_ioctl2,
1124*85756a06SHans Verkuil 	.release = vb2_fop_release,
1125*85756a06SHans Verkuil 	.poll = vb2_fop_poll,
1126*85756a06SHans Verkuil 	.mmap = vb2_fop_mmap,
1127*85756a06SHans Verkuil 	.read = vb2_fop_read,
1128*85756a06SHans Verkuil };
1129*85756a06SHans Verkuil 
1130*85756a06SHans Verkuil static const struct v4l2_file_operations cobalt_out_fops = {
1131*85756a06SHans Verkuil 	.owner = THIS_MODULE,
1132*85756a06SHans Verkuil 	.open = v4l2_fh_open,
1133*85756a06SHans Verkuil 	.unlocked_ioctl = video_ioctl2,
1134*85756a06SHans Verkuil 	.release = vb2_fop_release,
1135*85756a06SHans Verkuil 	.poll = vb2_fop_poll,
1136*85756a06SHans Verkuil 	.mmap = vb2_fop_mmap,
1137*85756a06SHans Verkuil 	.write = vb2_fop_write,
1138*85756a06SHans Verkuil };
1139*85756a06SHans Verkuil 
1140*85756a06SHans Verkuil static const struct v4l2_file_operations cobalt_empty_fops = {
1141*85756a06SHans Verkuil 	.owner = THIS_MODULE,
1142*85756a06SHans Verkuil 	.open = v4l2_fh_open,
1143*85756a06SHans Verkuil 	.unlocked_ioctl = video_ioctl2,
1144*85756a06SHans Verkuil 	.release = v4l2_fh_release,
1145*85756a06SHans Verkuil };
1146*85756a06SHans Verkuil 
1147*85756a06SHans Verkuil static int cobalt_node_register(struct cobalt *cobalt, int node)
1148*85756a06SHans Verkuil {
1149*85756a06SHans Verkuil 	static const struct v4l2_dv_timings dv1080p60 =
1150*85756a06SHans Verkuil 		V4L2_DV_BT_CEA_1920X1080P60;
1151*85756a06SHans Verkuil 	struct cobalt_stream *s = cobalt->streams + node;
1152*85756a06SHans Verkuil 	struct video_device *vdev = &s->vdev;
1153*85756a06SHans Verkuil 	struct vb2_queue *q = &s->q;
1154*85756a06SHans Verkuil 	int ret;
1155*85756a06SHans Verkuil 
1156*85756a06SHans Verkuil 	mutex_init(&s->lock);
1157*85756a06SHans Verkuil 	spin_lock_init(&s->irqlock);
1158*85756a06SHans Verkuil 
1159*85756a06SHans Verkuil 	snprintf(vdev->name, sizeof(vdev->name),
1160*85756a06SHans Verkuil 			"%s-%d", cobalt->v4l2_dev.name, node);
1161*85756a06SHans Verkuil 	s->width = 1920;
1162*85756a06SHans Verkuil 	/* Audio frames are just 4 lines of 1920 bytes */
1163*85756a06SHans Verkuil 	s->height = s->is_audio ? 4 : 1080;
1164*85756a06SHans Verkuil 
1165*85756a06SHans Verkuil 	if (s->is_audio) {
1166*85756a06SHans Verkuil 		s->bpp = 1;
1167*85756a06SHans Verkuil 		s->pixfmt = V4L2_PIX_FMT_GREY;
1168*85756a06SHans Verkuil 	} else if (s->is_output) {
1169*85756a06SHans Verkuil 		s->bpp = COBALT_BYTES_PER_PIXEL_RGB32;
1170*85756a06SHans Verkuil 		s->pixfmt = V4L2_PIX_FMT_BGR32;
1171*85756a06SHans Verkuil 	} else {
1172*85756a06SHans Verkuil 		s->bpp = COBALT_BYTES_PER_PIXEL_YUYV;
1173*85756a06SHans Verkuil 		s->pixfmt = V4L2_PIX_FMT_YUYV;
1174*85756a06SHans Verkuil 	}
1175*85756a06SHans Verkuil 	s->colorspace = V4L2_COLORSPACE_SRGB;
1176*85756a06SHans Verkuil 	s->stride = s->width * s->bpp;
1177*85756a06SHans Verkuil 
1178*85756a06SHans Verkuil 	if (!s->is_audio) {
1179*85756a06SHans Verkuil 		if (s->is_dummy)
1180*85756a06SHans Verkuil 			cobalt_warn("Setting up dummy video node %d\n", node);
1181*85756a06SHans Verkuil 		vdev->v4l2_dev = &cobalt->v4l2_dev;
1182*85756a06SHans Verkuil 		if (s->is_dummy)
1183*85756a06SHans Verkuil 			vdev->fops = &cobalt_empty_fops;
1184*85756a06SHans Verkuil 		else
1185*85756a06SHans Verkuil 			vdev->fops = s->is_output ? &cobalt_out_fops :
1186*85756a06SHans Verkuil 						    &cobalt_fops;
1187*85756a06SHans Verkuil 		vdev->release = video_device_release_empty;
1188*85756a06SHans Verkuil 		vdev->vfl_dir = s->is_output ? VFL_DIR_TX : VFL_DIR_RX;
1189*85756a06SHans Verkuil 		vdev->lock = &s->lock;
1190*85756a06SHans Verkuil 		if (s->sd)
1191*85756a06SHans Verkuil 			vdev->ctrl_handler = s->sd->ctrl_handler;
1192*85756a06SHans Verkuil 		s->timings = dv1080p60;
1193*85756a06SHans Verkuil 		v4l2_subdev_call(s->sd, video, s_dv_timings, &s->timings);
1194*85756a06SHans Verkuil 		if (!s->is_output && s->sd)
1195*85756a06SHans Verkuil 			cobalt_enable_input(s);
1196*85756a06SHans Verkuil 		vdev->ioctl_ops = s->is_dummy ? &cobalt_ioctl_empty_ops :
1197*85756a06SHans Verkuil 				  &cobalt_ioctl_ops;
1198*85756a06SHans Verkuil 	}
1199*85756a06SHans Verkuil 
1200*85756a06SHans Verkuil 	INIT_LIST_HEAD(&s->bufs);
1201*85756a06SHans Verkuil 	q->type = s->is_output ? V4L2_BUF_TYPE_VIDEO_OUTPUT :
1202*85756a06SHans Verkuil 				 V4L2_BUF_TYPE_VIDEO_CAPTURE;
1203*85756a06SHans Verkuil 	q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1204*85756a06SHans Verkuil 	q->io_modes |= s->is_output ? VB2_WRITE : VB2_READ;
1205*85756a06SHans Verkuil 	q->drv_priv = s;
1206*85756a06SHans Verkuil 	q->buf_struct_size = sizeof(struct cobalt_buffer);
1207*85756a06SHans Verkuil 	q->ops = &cobalt_qops;
1208*85756a06SHans Verkuil 	q->mem_ops = &vb2_dma_sg_memops;
1209*85756a06SHans Verkuil 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1210*85756a06SHans Verkuil 	q->min_buffers_needed = 2;
1211*85756a06SHans Verkuil 	q->lock = &s->lock;
1212*85756a06SHans Verkuil 	vdev->queue = q;
1213*85756a06SHans Verkuil 
1214*85756a06SHans Verkuil 	video_set_drvdata(vdev, s);
1215*85756a06SHans Verkuil 	ret = vb2_queue_init(q);
1216*85756a06SHans Verkuil 	if (!s->is_audio && ret == 0)
1217*85756a06SHans Verkuil 		ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1218*85756a06SHans Verkuil 	else if (!s->is_dummy)
1219*85756a06SHans Verkuil 		ret = cobalt_alsa_init(s);
1220*85756a06SHans Verkuil 
1221*85756a06SHans Verkuil 	if (ret < 0) {
1222*85756a06SHans Verkuil 		if (!s->is_audio)
1223*85756a06SHans Verkuil 			cobalt_err("couldn't register v4l2 device node %d\n",
1224*85756a06SHans Verkuil 					node);
1225*85756a06SHans Verkuil 		return ret;
1226*85756a06SHans Verkuil 	}
1227*85756a06SHans Verkuil 	cobalt_info("registered node %d\n", node);
1228*85756a06SHans Verkuil 	return 0;
1229*85756a06SHans Verkuil }
1230*85756a06SHans Verkuil 
1231*85756a06SHans Verkuil /* Initialize v4l2 variables and register v4l2 devices */
1232*85756a06SHans Verkuil int cobalt_nodes_register(struct cobalt *cobalt)
1233*85756a06SHans Verkuil {
1234*85756a06SHans Verkuil 	int node, ret;
1235*85756a06SHans Verkuil 
1236*85756a06SHans Verkuil 	/* Setup V4L2 Devices */
1237*85756a06SHans Verkuil 	for (node = 0; node < COBALT_NUM_STREAMS; node++) {
1238*85756a06SHans Verkuil 		ret = cobalt_node_register(cobalt, node);
1239*85756a06SHans Verkuil 		if (ret)
1240*85756a06SHans Verkuil 			return ret;
1241*85756a06SHans Verkuil 	}
1242*85756a06SHans Verkuil 	return 0;
1243*85756a06SHans Verkuil }
1244*85756a06SHans Verkuil 
1245*85756a06SHans Verkuil /* Unregister v4l2 devices */
1246*85756a06SHans Verkuil void cobalt_nodes_unregister(struct cobalt *cobalt)
1247*85756a06SHans Verkuil {
1248*85756a06SHans Verkuil 	int node;
1249*85756a06SHans Verkuil 
1250*85756a06SHans Verkuil 	/* Teardown all streams */
1251*85756a06SHans Verkuil 	for (node = 0; node < COBALT_NUM_STREAMS; node++) {
1252*85756a06SHans Verkuil 		struct cobalt_stream *s = cobalt->streams + node;
1253*85756a06SHans Verkuil 		struct video_device *vdev = &s->vdev;
1254*85756a06SHans Verkuil 
1255*85756a06SHans Verkuil 		if (!s->is_audio)
1256*85756a06SHans Verkuil 			video_unregister_device(vdev);
1257*85756a06SHans Verkuil 		else if (!s->is_dummy)
1258*85756a06SHans Verkuil 			cobalt_alsa_exit(s);
1259*85756a06SHans Verkuil 	}
1260*85756a06SHans Verkuil }
1261