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