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