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