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