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