4 * Derived from ivtv-ioctl.c and cx18-fileops.c
6 * Copyright 2012-2015 Cisco Systems, Inc. and/or its affiliates.
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.
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
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>
29 #include <media/v4l2-ctrls.h>
30 #include <media/v4l2-event.h>
31 #include <media/v4l2-dv-timings.h>
32 #include <media/i2c/adv7604.h>
33 #include <media/i2c/adv7842.h>
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"
42 static const struct v4l2_dv_timings cea1080p60 = V4L2_DV_BT_CEA_1920X1080P60;
44 /* vb2 DMA streaming ops */
46 static int cobalt_queue_setup(struct vb2_queue *q,
47 unsigned int *num_buffers, unsigned int *num_planes,
48 unsigned int sizes[], struct device *alloc_devs[])
50 struct cobalt_stream *s = q->drv_priv;
51 unsigned size = s->stride * s->height;
55 if (*num_buffers > NR_BUFS)
56 *num_buffers = NR_BUFS;
58 return sizes[0] < size ? -EINVAL : 0;
64 static int cobalt_buf_init(struct vb2_buffer *vb)
66 struct cobalt_stream *s = vb->vb2_queue->drv_priv;
67 struct cobalt *cobalt = s->cobalt;
68 const size_t max_pages_per_line =
69 (COBALT_MAX_WIDTH * COBALT_MAX_BPP) / PAGE_SIZE + 2;
71 COBALT_MAX_HEIGHT * max_pages_per_line * 0x20;
72 const size_t audio_bytes = ((1920 * 4) / PAGE_SIZE + 1) * 0x20;
73 struct sg_dma_desc_info *desc = &s->dma_desc_info[vb->index];
74 struct sg_table *sg_desc = vb2_dma_sg_plane_desc(vb, 0);
78 size = s->stride * s->height;
79 if (vb2_plane_size(vb, 0) < size) {
80 cobalt_info("data will not fit into plane (%lu < %u)\n",
81 vb2_plane_size(vb, 0), size);
85 if (desc->virt == NULL) {
86 desc->dev = &cobalt->pci_dev->dev;
87 descriptor_list_allocate(desc,
88 s->is_audio ? audio_bytes : bytes);
89 if (desc->virt == NULL)
92 ret = descriptor_list_create(cobalt, sg_desc->sgl,
93 !s->is_output, sg_desc->nents, size,
94 s->width * s->bpp, s->stride, desc);
96 descriptor_list_free(desc);
100 static void cobalt_buf_cleanup(struct vb2_buffer *vb)
102 struct cobalt_stream *s = vb->vb2_queue->drv_priv;
103 struct sg_dma_desc_info *desc = &s->dma_desc_info[vb->index];
105 descriptor_list_free(desc);
108 static int cobalt_buf_prepare(struct vb2_buffer *vb)
110 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
111 struct cobalt_stream *s = vb->vb2_queue->drv_priv;
113 vb2_set_plane_payload(vb, 0, s->stride * s->height);
114 vbuf->field = V4L2_FIELD_NONE;
118 static void chain_all_buffers(struct cobalt_stream *s)
120 struct sg_dma_desc_info *desc[NR_BUFS];
121 struct cobalt_buffer *cb;
125 list_for_each(p, &s->bufs) {
126 cb = list_entry(p, struct cobalt_buffer, list);
127 desc[i] = &s->dma_desc_info[cb->vb.vb2_buf.index];
129 descriptor_list_chain(desc[i-1], desc[i]);
134 static void cobalt_buf_queue(struct vb2_buffer *vb)
136 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
137 struct vb2_queue *q = vb->vb2_queue;
138 struct cobalt_stream *s = q->drv_priv;
139 struct cobalt_buffer *cb = to_cobalt_buffer(vbuf);
140 struct sg_dma_desc_info *desc = &s->dma_desc_info[vb->index];
143 /* Prepare new buffer */
144 descriptor_list_loopback(desc);
145 descriptor_list_interrupt_disable(desc);
147 spin_lock_irqsave(&s->irqlock, flags);
148 list_add_tail(&cb->list, &s->bufs);
149 chain_all_buffers(s);
150 spin_unlock_irqrestore(&s->irqlock, flags);
153 static void cobalt_enable_output(struct cobalt_stream *s)
155 struct cobalt *cobalt = s->cobalt;
156 struct v4l2_bt_timings *bt = &s->timings.bt;
157 struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
158 COBALT_TX_BASE(cobalt);
159 unsigned fmt = s->pixfmt != V4L2_PIX_FMT_BGR32 ?
160 M00514_CONTROL_BITMAP_FORMAT_16_BPP_MSK : 0;
161 struct v4l2_subdev_format sd_fmt = {
162 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
165 if (!cobalt_cpld_set_freq(cobalt, bt->pixelclock)) {
166 cobalt_err("pixelclock out of range\n");
170 sd_fmt.format.colorspace = s->colorspace;
171 sd_fmt.format.xfer_func = s->xfer_func;
172 sd_fmt.format.ycbcr_enc = s->ycbcr_enc;
173 sd_fmt.format.quantization = s->quantization;
174 sd_fmt.format.width = bt->width;
175 sd_fmt.format.height = bt->height;
177 /* Set up FDMA packer */
179 case V4L2_PIX_FMT_YUYV:
180 sd_fmt.format.code = MEDIA_BUS_FMT_UYVY8_1X16;
182 case V4L2_PIX_FMT_BGR32:
183 sd_fmt.format.code = MEDIA_BUS_FMT_RGB888_1X24;
186 v4l2_subdev_call(s->sd, pad, set_fmt, NULL, &sd_fmt);
188 iowrite32(0, &vo->control);
190 iowrite32(bt->hsync, &vo->sync_generator_h_sync_length);
191 iowrite32(bt->hbackporch, &vo->sync_generator_h_backporch_length);
192 iowrite32(bt->width, &vo->sync_generator_h_active_length);
193 iowrite32(bt->hfrontporch, &vo->sync_generator_h_frontporch_length);
194 iowrite32(bt->vsync, &vo->sync_generator_v_sync_length);
195 iowrite32(bt->vbackporch, &vo->sync_generator_v_backporch_length);
196 iowrite32(bt->height, &vo->sync_generator_v_active_length);
197 iowrite32(bt->vfrontporch, &vo->sync_generator_v_frontporch_length);
198 iowrite32(0x9900c1, &vo->error_color);
200 iowrite32(M00514_CONTROL_BITMAP_SYNC_GENERATOR_LOAD_PARAM_MSK | fmt,
202 iowrite32(M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK | fmt, &vo->control);
203 iowrite32(M00514_CONTROL_BITMAP_SYNC_GENERATOR_ENABLE_MSK |
204 M00514_CONTROL_BITMAP_FLOW_CTRL_OUTPUT_ENABLE_MSK |
208 static void cobalt_enable_input(struct cobalt_stream *s)
210 struct cobalt *cobalt = s->cobalt;
211 int ch = (int)s->video_channel;
212 struct m00235_fdma_packer_regmap __iomem *packer;
213 struct v4l2_subdev_format sd_fmt_yuyv = {
214 .pad = s->pad_source,
215 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
216 .format.code = MEDIA_BUS_FMT_YUYV8_1X16,
218 struct v4l2_subdev_format sd_fmt_rgb = {
219 .pad = s->pad_source,
220 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
221 .format.code = MEDIA_BUS_FMT_RGB888_1X24,
224 cobalt_dbg(1, "video_channel %d (%s, %s)\n",
226 s->input == 0 ? "hdmi" : "generator",
229 packer = COBALT_CVI_PACKER(cobalt, ch);
231 /* Set up FDMA packer */
233 case V4L2_PIX_FMT_YUYV:
234 iowrite32(M00235_CONTROL_BITMAP_ENABLE_MSK |
235 (1 << M00235_CONTROL_BITMAP_PACK_FORMAT_OFST),
237 v4l2_subdev_call(s->sd, pad, set_fmt, NULL,
240 case V4L2_PIX_FMT_RGB24:
241 iowrite32(M00235_CONTROL_BITMAP_ENABLE_MSK |
242 (2 << M00235_CONTROL_BITMAP_PACK_FORMAT_OFST),
244 v4l2_subdev_call(s->sd, pad, set_fmt, NULL,
247 case V4L2_PIX_FMT_BGR32:
248 iowrite32(M00235_CONTROL_BITMAP_ENABLE_MSK |
249 M00235_CONTROL_BITMAP_ENDIAN_FORMAT_MSK |
250 (3 << M00235_CONTROL_BITMAP_PACK_FORMAT_OFST),
252 v4l2_subdev_call(s->sd, pad, set_fmt, NULL,
258 static void cobalt_dma_start_streaming(struct cobalt_stream *s)
260 struct cobalt *cobalt = s->cobalt;
261 int rx = s->video_channel;
262 struct m00460_evcnt_regmap __iomem *evcnt =
263 COBALT_CVI_EVCNT(cobalt, rx);
264 struct cobalt_buffer *cb;
267 spin_lock_irqsave(&s->irqlock, flags);
269 iowrite32(M00460_CONTROL_BITMAP_CLEAR_MSK, &evcnt->control);
270 iowrite32(M00460_CONTROL_BITMAP_ENABLE_MSK, &evcnt->control);
272 struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
273 COBALT_TX_BASE(cobalt);
274 u32 ctrl = ioread32(&vo->control);
276 ctrl &= ~(M00514_CONTROL_BITMAP_EVCNT_ENABLE_MSK |
277 M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK);
278 iowrite32(ctrl | M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK,
280 iowrite32(ctrl | M00514_CONTROL_BITMAP_EVCNT_ENABLE_MSK,
283 cb = list_first_entry(&s->bufs, struct cobalt_buffer, list);
284 omni_sg_dma_start(s, &s->dma_desc_info[cb->vb.vb2_buf.index]);
285 spin_unlock_irqrestore(&s->irqlock, flags);
288 static int cobalt_start_streaming(struct vb2_queue *q, unsigned int count)
290 struct cobalt_stream *s = q->drv_priv;
291 struct cobalt *cobalt = s->cobalt;
292 struct m00233_video_measure_regmap __iomem *vmr;
293 struct m00473_freewheel_regmap __iomem *fw;
294 struct m00479_clk_loss_detector_regmap __iomem *clkloss;
295 int rx = s->video_channel;
296 struct m00389_cvi_regmap __iomem *cvi = COBALT_CVI(cobalt, rx);
297 struct m00460_evcnt_regmap __iomem *evcnt = COBALT_CVI_EVCNT(cobalt, rx);
298 struct v4l2_bt_timings *bt = &s->timings.bt;
305 s->unstable_frame = false;
306 cobalt_enable_output(s);
310 cobalt_enable_input(s);
312 fw = COBALT_CVI_FREEWHEEL(cobalt, rx);
313 vmr = COBALT_CVI_VMR(cobalt, rx);
314 clkloss = COBALT_CVI_CLK_LOSS(cobalt, rx);
316 iowrite32(M00460_CONTROL_BITMAP_CLEAR_MSK, &evcnt->control);
317 iowrite32(M00460_CONTROL_BITMAP_ENABLE_MSK, &evcnt->control);
318 iowrite32(bt->width, &cvi->frame_width);
319 iowrite32(bt->height, &cvi->frame_height);
320 tot_size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
321 iowrite32(div_u64((u64)V4L2_DV_BT_FRAME_WIDTH(bt) * COBALT_CLK * 4,
322 bt->pixelclock), &vmr->hsync_timeout_val);
323 iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK, &vmr->control);
324 clk_freq = ioread32(&fw->clk_freq);
325 iowrite32(clk_freq / 1000000, &clkloss->ref_clk_cnt_val);
326 /* The lower bound for the clock frequency is 0.5% lower as is
327 * allowed by the spec */
328 iowrite32(div_u64(bt->pixelclock * 995, 1000000000),
329 &clkloss->test_clk_cnt_val);
330 /* will be enabled after the first frame has been received */
331 iowrite32(bt->width * bt->height, &fw->active_length);
332 iowrite32(div_u64((u64)clk_freq * tot_size, bt->pixelclock),
334 iowrite32(M00233_IRQ_TRIGGERS_BITMAP_VACTIVE_AREA_MSK |
335 M00233_IRQ_TRIGGERS_BITMAP_HACTIVE_AREA_MSK,
337 iowrite32(0, &cvi->control);
338 iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK, &vmr->control);
340 iowrite32(0xff, &fw->output_color);
341 iowrite32(M00479_CTRL_BITMAP_ENABLE_MSK, &clkloss->ctrl);
342 iowrite32(M00473_CTRL_BITMAP_ENABLE_MSK |
343 M00473_CTRL_BITMAP_FORCE_FREEWHEEL_MODE_MSK, &fw->ctrl);
344 s->unstable_frame = true;
345 s->enable_freewheel = false;
346 s->enable_cvi = false;
347 s->skip_first_frames = 0;
351 cobalt_dma_start_streaming(s);
355 static void cobalt_dma_stop_streaming(struct cobalt_stream *s)
357 struct cobalt *cobalt = s->cobalt;
358 struct sg_dma_desc_info *desc;
359 struct cobalt_buffer *cb;
362 int timeout_msec = 100;
363 int rx = s->video_channel;
364 struct m00460_evcnt_regmap __iomem *evcnt =
365 COBALT_CVI_EVCNT(cobalt, rx);
368 iowrite32(0, &evcnt->control);
369 } else if (!s->is_audio) {
370 struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
371 COBALT_TX_BASE(cobalt);
373 iowrite32(M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK, &vo->control);
374 iowrite32(0, &vo->control);
377 /* Try to stop the DMA engine gracefully */
378 spin_lock_irqsave(&s->irqlock, flags);
379 list_for_each(p, &s->bufs) {
380 cb = list_entry(p, struct cobalt_buffer, list);
381 desc = &s->dma_desc_info[cb->vb.vb2_buf.index];
382 /* Stop DMA after this descriptor chain */
383 descriptor_list_end_of_chain(desc);
385 spin_unlock_irqrestore(&s->irqlock, flags);
387 /* Wait 100 milisecond for DMA to finish, abort on timeout. */
388 if (!wait_event_timeout(s->q.done_wq, is_dma_done(s),
389 msecs_to_jiffies(timeout_msec))) {
390 omni_sg_dma_abort_channel(s);
391 pr_warn("aborted\n");
393 cobalt_write_bar0(cobalt, DMA_INTERRUPT_STATUS_REG,
394 1 << s->dma_channel);
397 static void cobalt_stop_streaming(struct vb2_queue *q)
399 struct cobalt_stream *s = q->drv_priv;
400 struct cobalt *cobalt = s->cobalt;
401 int rx = s->video_channel;
402 struct m00233_video_measure_regmap __iomem *vmr;
403 struct m00473_freewheel_regmap __iomem *fw;
404 struct m00479_clk_loss_detector_regmap __iomem *clkloss;
405 struct cobalt_buffer *cb;
406 struct list_head *p, *safe;
409 cobalt_dma_stop_streaming(s);
411 /* Return all buffers to user space */
412 spin_lock_irqsave(&s->irqlock, flags);
413 list_for_each_safe(p, safe, &s->bufs) {
414 cb = list_entry(p, struct cobalt_buffer, list);
416 vb2_buffer_done(&cb->vb.vb2_buf, VB2_BUF_STATE_ERROR);
418 spin_unlock_irqrestore(&s->irqlock, flags);
420 if (s->is_audio || s->is_output)
423 fw = COBALT_CVI_FREEWHEEL(cobalt, rx);
424 vmr = COBALT_CVI_VMR(cobalt, rx);
425 clkloss = COBALT_CVI_CLK_LOSS(cobalt, rx);
426 iowrite32(0, &vmr->control);
427 iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK, &vmr->control);
428 iowrite32(0, &fw->ctrl);
429 iowrite32(0, &clkloss->ctrl);
432 static const struct vb2_ops cobalt_qops = {
433 .queue_setup = cobalt_queue_setup,
434 .buf_init = cobalt_buf_init,
435 .buf_cleanup = cobalt_buf_cleanup,
436 .buf_prepare = cobalt_buf_prepare,
437 .buf_queue = cobalt_buf_queue,
438 .start_streaming = cobalt_start_streaming,
439 .stop_streaming = cobalt_stop_streaming,
440 .wait_prepare = vb2_ops_wait_prepare,
441 .wait_finish = vb2_ops_wait_finish,
446 #ifdef CONFIG_VIDEO_ADV_DEBUG
447 static int cobalt_cobaltc(struct cobalt *cobalt, unsigned int cmd, void *arg)
449 struct v4l2_dbg_register *regs = arg;
450 void __iomem *adrs = cobalt->bar1 + regs->reg;
452 cobalt_info("cobalt_cobaltc: adrs = %p\n", adrs);
454 if (!capable(CAP_SYS_ADMIN))
458 if (cmd == VIDIOC_DBG_S_REGISTER)
459 iowrite32(regs->val, adrs);
461 regs->val = ioread32(adrs);
465 static int cobalt_g_register(struct file *file, void *priv_fh,
466 struct v4l2_dbg_register *reg)
468 struct cobalt_stream *s = video_drvdata(file);
469 struct cobalt *cobalt = s->cobalt;
471 return cobalt_cobaltc(cobalt, VIDIOC_DBG_G_REGISTER, reg);
474 static int cobalt_s_register(struct file *file, void *priv_fh,
475 const struct v4l2_dbg_register *reg)
477 struct cobalt_stream *s = video_drvdata(file);
478 struct cobalt *cobalt = s->cobalt;
480 return cobalt_cobaltc(cobalt, VIDIOC_DBG_S_REGISTER,
481 (struct v4l2_dbg_register *)reg);
485 static int cobalt_querycap(struct file *file, void *priv_fh,
486 struct v4l2_capability *vcap)
488 struct cobalt_stream *s = video_drvdata(file);
489 struct cobalt *cobalt = s->cobalt;
491 strlcpy(vcap->driver, "cobalt", sizeof(vcap->driver));
492 strlcpy(vcap->card, "cobalt", sizeof(vcap->card));
493 snprintf(vcap->bus_info, sizeof(vcap->bus_info),
494 "PCIe:%s", pci_name(cobalt->pci_dev));
495 vcap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
497 vcap->device_caps |= V4L2_CAP_VIDEO_OUTPUT;
499 vcap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
500 vcap->capabilities = vcap->device_caps | V4L2_CAP_DEVICE_CAPS |
501 V4L2_CAP_VIDEO_CAPTURE;
502 if (cobalt->have_hsma_tx)
503 vcap->capabilities |= V4L2_CAP_VIDEO_OUTPUT;
507 static void cobalt_video_input_status_show(struct cobalt_stream *s)
509 struct m00389_cvi_regmap __iomem *cvi;
510 struct m00233_video_measure_regmap __iomem *vmr;
511 struct m00473_freewheel_regmap __iomem *fw;
512 struct m00479_clk_loss_detector_regmap __iomem *clkloss;
513 struct m00235_fdma_packer_regmap __iomem *packer;
514 int rx = s->video_channel;
515 struct cobalt *cobalt = s->cobalt;
516 u32 cvi_ctrl, cvi_stat;
517 u32 vmr_ctrl, vmr_stat;
519 cvi = COBALT_CVI(cobalt, rx);
520 vmr = COBALT_CVI_VMR(cobalt, rx);
521 fw = COBALT_CVI_FREEWHEEL(cobalt, rx);
522 clkloss = COBALT_CVI_CLK_LOSS(cobalt, rx);
523 packer = COBALT_CVI_PACKER(cobalt, rx);
524 cvi_ctrl = ioread32(&cvi->control);
525 cvi_stat = ioread32(&cvi->status);
526 vmr_ctrl = ioread32(&vmr->control);
527 vmr_stat = ioread32(&vmr->control);
528 cobalt_info("rx%d: cvi resolution: %dx%d\n", rx,
529 ioread32(&cvi->frame_width), ioread32(&cvi->frame_height));
530 cobalt_info("rx%d: cvi control: %s%s%s\n", rx,
531 (cvi_ctrl & M00389_CONTROL_BITMAP_ENABLE_MSK) ?
532 "enable " : "disable ",
533 (cvi_ctrl & M00389_CONTROL_BITMAP_HSYNC_POLARITY_LOW_MSK) ?
534 "HSync- " : "HSync+ ",
535 (cvi_ctrl & M00389_CONTROL_BITMAP_VSYNC_POLARITY_LOW_MSK) ?
536 "VSync- " : "VSync+ ");
537 cobalt_info("rx%d: cvi status: %s%s\n", rx,
538 (cvi_stat & M00389_STATUS_BITMAP_LOCK_MSK) ?
539 "lock " : "no-lock ",
540 (cvi_stat & M00389_STATUS_BITMAP_ERROR_MSK) ?
541 "error " : "no-error ");
543 cobalt_info("rx%d: Measurements: %s%s%s%s%s%s%s\n", rx,
544 (vmr_ctrl & M00233_CONTROL_BITMAP_HSYNC_POLARITY_LOW_MSK) ?
545 "HSync- " : "HSync+ ",
546 (vmr_ctrl & M00233_CONTROL_BITMAP_VSYNC_POLARITY_LOW_MSK) ?
547 "VSync- " : "VSync+ ",
548 (vmr_ctrl & M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK) ?
549 "enabled " : "disabled ",
550 (vmr_ctrl & M00233_CONTROL_BITMAP_ENABLE_INTERRUPT_MSK) ?
551 "irq-enabled " : "irq-disabled ",
552 (vmr_ctrl & M00233_CONTROL_BITMAP_UPDATE_ON_HSYNC_MSK) ?
553 "update-on-hsync " : "",
554 (vmr_stat & M00233_STATUS_BITMAP_HSYNC_TIMEOUT_MSK) ?
555 "hsync-timeout " : "",
556 (vmr_stat & M00233_STATUS_BITMAP_INIT_DONE_MSK) ?
558 cobalt_info("rx%d: irq_status: 0x%02x irq_triggers: 0x%02x\n", rx,
559 ioread32(&vmr->irq_status) & 0xff,
560 ioread32(&vmr->irq_triggers) & 0xff);
561 cobalt_info("rx%d: vsync: %d\n", rx, ioread32(&vmr->vsync_time));
562 cobalt_info("rx%d: vbp: %d\n", rx, ioread32(&vmr->vback_porch));
563 cobalt_info("rx%d: vact: %d\n", rx, ioread32(&vmr->vactive_area));
564 cobalt_info("rx%d: vfb: %d\n", rx, ioread32(&vmr->vfront_porch));
565 cobalt_info("rx%d: hsync: %d\n", rx, ioread32(&vmr->hsync_time));
566 cobalt_info("rx%d: hbp: %d\n", rx, ioread32(&vmr->hback_porch));
567 cobalt_info("rx%d: hact: %d\n", rx, ioread32(&vmr->hactive_area));
568 cobalt_info("rx%d: hfb: %d\n", rx, ioread32(&vmr->hfront_porch));
569 cobalt_info("rx%d: Freewheeling: %s%s%s\n", rx,
570 (ioread32(&fw->ctrl) & M00473_CTRL_BITMAP_ENABLE_MSK) ?
571 "enabled " : "disabled ",
572 (ioread32(&fw->ctrl) & M00473_CTRL_BITMAP_FORCE_FREEWHEEL_MODE_MSK) ?
574 (ioread32(&fw->status) & M00473_STATUS_BITMAP_FREEWHEEL_MODE_MSK) ?
575 "freewheeling " : "video-passthrough ");
576 iowrite32(0xff, &vmr->irq_status);
577 cobalt_info("rx%d: Clock Loss Detection: %s%s\n", rx,
578 (ioread32(&clkloss->ctrl) & M00479_CTRL_BITMAP_ENABLE_MSK) ?
579 "enabled " : "disabled ",
580 (ioread32(&clkloss->status) & M00479_STATUS_BITMAP_CLOCK_MISSING_MSK) ?
581 "clock-missing " : "found-clock ");
582 cobalt_info("rx%d: Packer: %x\n", rx, ioread32(&packer->control));
585 static int cobalt_log_status(struct file *file, void *priv_fh)
587 struct cobalt_stream *s = video_drvdata(file);
588 struct cobalt *cobalt = s->cobalt;
589 struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
590 COBALT_TX_BASE(cobalt);
593 cobalt_info("%s", cobalt->hdl_info);
594 cobalt_info("sysctrl: %08x, sysstat: %08x\n",
595 cobalt_g_sysctrl(cobalt),
596 cobalt_g_sysstat(cobalt));
597 cobalt_info("dma channel: %d, video channel: %d\n",
598 s->dma_channel, s->video_channel);
599 cobalt_pcie_status_show(cobalt);
600 cobalt_cpld_status(cobalt);
601 cobalt_irq_log_status(cobalt);
602 v4l2_subdev_call(s->sd, core, log_status);
604 cobalt_video_input_status_show(s);
608 stat = ioread32(&vo->rd_status);
610 cobalt_info("tx: status: %s%s\n",
611 (stat & M00514_RD_STATUS_BITMAP_FLOW_CTRL_NO_DATA_ERROR_MSK) ?
613 (stat & M00514_RD_STATUS_BITMAP_READY_BUFFER_FULL_MSK) ?
614 "ready_buffer_full " : "");
615 cobalt_info("tx: evcnt: %d\n", ioread32(&vo->rd_evcnt_count));
619 static int cobalt_enum_dv_timings(struct file *file, void *priv_fh,
620 struct v4l2_enum_dv_timings *timings)
622 struct cobalt_stream *s = video_drvdata(file);
627 memset(timings->reserved, 0, sizeof(timings->reserved));
628 timings->timings = cea1080p60;
632 return v4l2_subdev_call(s->sd,
633 pad, enum_dv_timings, timings);
636 static int cobalt_s_dv_timings(struct file *file, void *priv_fh,
637 struct v4l2_dv_timings *timings)
639 struct cobalt_stream *s = video_drvdata(file);
643 *timings = cea1080p60;
647 if (v4l2_match_dv_timings(timings, &s->timings, 0, false))
650 if (vb2_is_busy(&s->q))
653 err = v4l2_subdev_call(s->sd,
654 video, s_dv_timings, timings);
656 s->timings = *timings;
657 s->width = timings->bt.width;
658 s->height = timings->bt.height;
659 s->stride = timings->bt.width * s->bpp;
664 static int cobalt_g_dv_timings(struct file *file, void *priv_fh,
665 struct v4l2_dv_timings *timings)
667 struct cobalt_stream *s = video_drvdata(file);
670 *timings = cea1080p60;
673 return v4l2_subdev_call(s->sd,
674 video, g_dv_timings, timings);
677 static int cobalt_query_dv_timings(struct file *file, void *priv_fh,
678 struct v4l2_dv_timings *timings)
680 struct cobalt_stream *s = video_drvdata(file);
683 *timings = cea1080p60;
686 return v4l2_subdev_call(s->sd,
687 video, query_dv_timings, timings);
690 static int cobalt_dv_timings_cap(struct file *file, void *priv_fh,
691 struct v4l2_dv_timings_cap *cap)
693 struct cobalt_stream *s = video_drvdata(file);
696 return v4l2_subdev_call(s->sd,
697 pad, dv_timings_cap, cap);
700 static int cobalt_enum_fmt_vid_cap(struct file *file, void *priv_fh,
701 struct v4l2_fmtdesc *f)
705 strlcpy(f->description, "YUV 4:2:2", sizeof(f->description));
706 f->pixelformat = V4L2_PIX_FMT_YUYV;
709 strlcpy(f->description, "RGB24", sizeof(f->description));
710 f->pixelformat = V4L2_PIX_FMT_RGB24;
713 strlcpy(f->description, "RGB32", sizeof(f->description));
714 f->pixelformat = V4L2_PIX_FMT_BGR32;
723 static int cobalt_g_fmt_vid_cap(struct file *file, void *priv_fh,
724 struct v4l2_format *f)
726 struct cobalt_stream *s = video_drvdata(file);
727 struct v4l2_pix_format *pix = &f->fmt.pix;
728 struct v4l2_subdev_format sd_fmt;
730 pix->width = s->width;
731 pix->height = s->height;
732 pix->bytesperline = s->stride;
733 pix->field = V4L2_FIELD_NONE;
736 pix->colorspace = V4L2_COLORSPACE_SRGB;
738 sd_fmt.pad = s->pad_source;
739 sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
740 v4l2_subdev_call(s->sd, pad, get_fmt, NULL, &sd_fmt);
741 v4l2_fill_pix_format(pix, &sd_fmt.format);
744 pix->pixelformat = s->pixfmt;
745 pix->sizeimage = pix->bytesperline * pix->height;
750 static int cobalt_try_fmt_vid_cap(struct file *file, void *priv_fh,
751 struct v4l2_format *f)
753 struct cobalt_stream *s = video_drvdata(file);
754 struct v4l2_pix_format *pix = &f->fmt.pix;
755 struct v4l2_subdev_format sd_fmt;
757 /* Check for min (QCIF) and max (Full HD) size */
758 if ((pix->width < 176) || (pix->height < 144)) {
763 if ((pix->width > 1920) || (pix->height > 1080)) {
768 /* Make width multiple of 4 */
771 /* Make height multiple of 2 */
775 /* Generator => fixed format only */
778 pix->colorspace = V4L2_COLORSPACE_SRGB;
780 sd_fmt.pad = s->pad_source;
781 sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
782 v4l2_subdev_call(s->sd, pad, get_fmt, NULL, &sd_fmt);
783 v4l2_fill_pix_format(pix, &sd_fmt.format);
786 switch (pix->pixelformat) {
787 case V4L2_PIX_FMT_YUYV:
789 pix->bytesperline = max(pix->bytesperline & ~0x3,
790 pix->width * COBALT_BYTES_PER_PIXEL_YUYV);
791 pix->pixelformat = V4L2_PIX_FMT_YUYV;
793 case V4L2_PIX_FMT_RGB24:
794 pix->bytesperline = max(pix->bytesperline & ~0x3,
795 pix->width * COBALT_BYTES_PER_PIXEL_RGB24);
797 case V4L2_PIX_FMT_BGR32:
798 pix->bytesperline = max(pix->bytesperline & ~0x3,
799 pix->width * COBALT_BYTES_PER_PIXEL_RGB32);
803 pix->sizeimage = pix->bytesperline * pix->height;
804 pix->field = V4L2_FIELD_NONE;
810 static int cobalt_s_fmt_vid_cap(struct file *file, void *priv_fh,
811 struct v4l2_format *f)
813 struct cobalt_stream *s = video_drvdata(file);
814 struct v4l2_pix_format *pix = &f->fmt.pix;
816 if (vb2_is_busy(&s->q))
819 if (cobalt_try_fmt_vid_cap(file, priv_fh, f))
822 s->width = pix->width;
823 s->height = pix->height;
824 s->stride = pix->bytesperline;
825 switch (pix->pixelformat) {
826 case V4L2_PIX_FMT_YUYV:
827 s->bpp = COBALT_BYTES_PER_PIXEL_YUYV;
829 case V4L2_PIX_FMT_RGB24:
830 s->bpp = COBALT_BYTES_PER_PIXEL_RGB24;
832 case V4L2_PIX_FMT_BGR32:
833 s->bpp = COBALT_BYTES_PER_PIXEL_RGB32;
838 s->pixfmt = pix->pixelformat;
839 cobalt_enable_input(s);
844 static int cobalt_try_fmt_vid_out(struct file *file, void *priv_fh,
845 struct v4l2_format *f)
847 struct v4l2_pix_format *pix = &f->fmt.pix;
849 /* Check for min (QCIF) and max (Full HD) size */
850 if ((pix->width < 176) || (pix->height < 144)) {
855 if ((pix->width > 1920) || (pix->height > 1080)) {
860 /* Make width multiple of 4 */
863 /* Make height multiple of 2 */
866 switch (pix->pixelformat) {
867 case V4L2_PIX_FMT_YUYV:
869 pix->bytesperline = max(pix->bytesperline & ~0x3,
870 pix->width * COBALT_BYTES_PER_PIXEL_YUYV);
871 pix->pixelformat = V4L2_PIX_FMT_YUYV;
873 case V4L2_PIX_FMT_BGR32:
874 pix->bytesperline = max(pix->bytesperline & ~0x3,
875 pix->width * COBALT_BYTES_PER_PIXEL_RGB32);
879 pix->sizeimage = pix->bytesperline * pix->height;
880 pix->field = V4L2_FIELD_NONE;
885 static int cobalt_g_fmt_vid_out(struct file *file, void *priv_fh,
886 struct v4l2_format *f)
888 struct cobalt_stream *s = video_drvdata(file);
889 struct v4l2_pix_format *pix = &f->fmt.pix;
891 pix->width = s->width;
892 pix->height = s->height;
893 pix->bytesperline = s->stride;
894 pix->field = V4L2_FIELD_NONE;
895 pix->pixelformat = s->pixfmt;
896 pix->colorspace = s->colorspace;
897 pix->xfer_func = s->xfer_func;
898 pix->ycbcr_enc = s->ycbcr_enc;
899 pix->quantization = s->quantization;
900 pix->sizeimage = pix->bytesperline * pix->height;
905 static int cobalt_enum_fmt_vid_out(struct file *file, void *priv_fh,
906 struct v4l2_fmtdesc *f)
910 strlcpy(f->description, "YUV 4:2:2", sizeof(f->description));
911 f->pixelformat = V4L2_PIX_FMT_YUYV;
914 strlcpy(f->description, "RGB32", sizeof(f->description));
915 f->pixelformat = V4L2_PIX_FMT_BGR32;
924 static int cobalt_s_fmt_vid_out(struct file *file, void *priv_fh,
925 struct v4l2_format *f)
927 struct cobalt_stream *s = video_drvdata(file);
928 struct v4l2_pix_format *pix = &f->fmt.pix;
929 struct v4l2_subdev_format sd_fmt = { 0 };
932 if (cobalt_try_fmt_vid_out(file, priv_fh, f))
935 if (vb2_is_busy(&s->q) && (pix->pixelformat != s->pixfmt ||
936 pix->width != s->width || pix->height != s->height ||
937 pix->bytesperline != s->stride))
940 switch (pix->pixelformat) {
941 case V4L2_PIX_FMT_YUYV:
942 s->bpp = COBALT_BYTES_PER_PIXEL_YUYV;
943 code = MEDIA_BUS_FMT_UYVY8_1X16;
945 case V4L2_PIX_FMT_BGR32:
946 s->bpp = COBALT_BYTES_PER_PIXEL_RGB32;
947 code = MEDIA_BUS_FMT_RGB888_1X24;
952 s->width = pix->width;
953 s->height = pix->height;
954 s->stride = pix->bytesperline;
955 s->pixfmt = pix->pixelformat;
956 s->colorspace = pix->colorspace;
957 s->xfer_func = pix->xfer_func;
958 s->ycbcr_enc = pix->ycbcr_enc;
959 s->quantization = pix->quantization;
960 sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
961 v4l2_fill_mbus_format(&sd_fmt.format, pix, code);
962 v4l2_subdev_call(s->sd, pad, set_fmt, NULL, &sd_fmt);
966 static int cobalt_enum_input(struct file *file, void *priv_fh,
967 struct v4l2_input *inp)
969 struct cobalt_stream *s = video_drvdata(file);
974 snprintf(inp->name, sizeof(inp->name),
975 "HDMI-%d", s->video_channel);
977 snprintf(inp->name, sizeof(inp->name),
978 "Generator-%d", s->video_channel);
979 inp->type = V4L2_INPUT_TYPE_CAMERA;
980 inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
983 return v4l2_subdev_call(s->sd,
984 video, g_input_status, &inp->status);
987 static int cobalt_g_input(struct file *file, void *priv_fh, unsigned int *i)
989 struct cobalt_stream *s = video_drvdata(file);
995 static int cobalt_s_input(struct file *file, void *priv_fh, unsigned int i)
997 struct cobalt_stream *s = video_drvdata(file);
1001 if (vb2_is_busy(&s->q))
1005 cobalt_enable_input(s);
1007 if (s->input == 1) /* Test Pattern Generator */
1010 return v4l2_subdev_call(s->sd, video, s_routing,
1011 ADV76XX_PAD_HDMI_PORT_A, 0, 0);
1014 static int cobalt_enum_output(struct file *file, void *priv_fh,
1015 struct v4l2_output *out)
1019 snprintf(out->name, sizeof(out->name), "HDMI-%d", out->index);
1020 out->type = V4L2_OUTPUT_TYPE_ANALOG;
1021 out->capabilities = V4L2_OUT_CAP_DV_TIMINGS;
1025 static int cobalt_g_output(struct file *file, void *priv_fh, unsigned int *i)
1031 static int cobalt_s_output(struct file *file, void *priv_fh, unsigned int i)
1033 return i ? -EINVAL : 0;
1036 static int cobalt_g_edid(struct file *file, void *fh, struct v4l2_edid *edid)
1038 struct cobalt_stream *s = video_drvdata(file);
1039 u32 pad = edid->pad;
1042 if (edid->pad >= (s->is_output ? 1 : 2))
1045 ret = v4l2_subdev_call(s->sd, pad, get_edid, edid);
1050 static int cobalt_s_edid(struct file *file, void *fh, struct v4l2_edid *edid)
1052 struct cobalt_stream *s = video_drvdata(file);
1053 u32 pad = edid->pad;
1059 ret = v4l2_subdev_call(s->sd, pad, set_edid, edid);
1064 static int cobalt_subscribe_event(struct v4l2_fh *fh,
1065 const struct v4l2_event_subscription *sub)
1067 switch (sub->type) {
1068 case V4L2_EVENT_SOURCE_CHANGE:
1069 return v4l2_event_subscribe(fh, sub, 4, NULL);
1071 return v4l2_ctrl_subscribe_event(fh, sub);
1074 static int cobalt_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1076 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1078 a->parm.capture.timeperframe.numerator = 1;
1079 a->parm.capture.timeperframe.denominator = 60;
1080 a->parm.capture.readbuffers = 3;
1084 static const struct v4l2_ioctl_ops cobalt_ioctl_ops = {
1085 .vidioc_querycap = cobalt_querycap,
1086 .vidioc_g_parm = cobalt_g_parm,
1087 .vidioc_log_status = cobalt_log_status,
1088 .vidioc_streamon = vb2_ioctl_streamon,
1089 .vidioc_streamoff = vb2_ioctl_streamoff,
1090 .vidioc_enum_input = cobalt_enum_input,
1091 .vidioc_g_input = cobalt_g_input,
1092 .vidioc_s_input = cobalt_s_input,
1093 .vidioc_enum_fmt_vid_cap = cobalt_enum_fmt_vid_cap,
1094 .vidioc_g_fmt_vid_cap = cobalt_g_fmt_vid_cap,
1095 .vidioc_s_fmt_vid_cap = cobalt_s_fmt_vid_cap,
1096 .vidioc_try_fmt_vid_cap = cobalt_try_fmt_vid_cap,
1097 .vidioc_enum_output = cobalt_enum_output,
1098 .vidioc_g_output = cobalt_g_output,
1099 .vidioc_s_output = cobalt_s_output,
1100 .vidioc_enum_fmt_vid_out = cobalt_enum_fmt_vid_out,
1101 .vidioc_g_fmt_vid_out = cobalt_g_fmt_vid_out,
1102 .vidioc_s_fmt_vid_out = cobalt_s_fmt_vid_out,
1103 .vidioc_try_fmt_vid_out = cobalt_try_fmt_vid_out,
1104 .vidioc_s_dv_timings = cobalt_s_dv_timings,
1105 .vidioc_g_dv_timings = cobalt_g_dv_timings,
1106 .vidioc_query_dv_timings = cobalt_query_dv_timings,
1107 .vidioc_enum_dv_timings = cobalt_enum_dv_timings,
1108 .vidioc_dv_timings_cap = cobalt_dv_timings_cap,
1109 .vidioc_g_edid = cobalt_g_edid,
1110 .vidioc_s_edid = cobalt_s_edid,
1111 .vidioc_subscribe_event = cobalt_subscribe_event,
1112 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1113 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1114 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1115 .vidioc_querybuf = vb2_ioctl_querybuf,
1116 .vidioc_qbuf = vb2_ioctl_qbuf,
1117 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1118 .vidioc_expbuf = vb2_ioctl_expbuf,
1119 #ifdef CONFIG_VIDEO_ADV_DEBUG
1120 .vidioc_g_register = cobalt_g_register,
1121 .vidioc_s_register = cobalt_s_register,
1125 static const struct v4l2_ioctl_ops cobalt_ioctl_empty_ops = {
1126 #ifdef CONFIG_VIDEO_ADV_DEBUG
1127 .vidioc_g_register = cobalt_g_register,
1128 .vidioc_s_register = cobalt_s_register,
1132 /* Register device nodes */
1134 static const struct v4l2_file_operations cobalt_fops = {
1135 .owner = THIS_MODULE,
1136 .open = v4l2_fh_open,
1137 .unlocked_ioctl = video_ioctl2,
1138 .release = vb2_fop_release,
1139 .poll = vb2_fop_poll,
1140 .mmap = vb2_fop_mmap,
1141 .read = vb2_fop_read,
1144 static const struct v4l2_file_operations cobalt_out_fops = {
1145 .owner = THIS_MODULE,
1146 .open = v4l2_fh_open,
1147 .unlocked_ioctl = video_ioctl2,
1148 .release = vb2_fop_release,
1149 .poll = vb2_fop_poll,
1150 .mmap = vb2_fop_mmap,
1151 .write = vb2_fop_write,
1154 static const struct v4l2_file_operations cobalt_empty_fops = {
1155 .owner = THIS_MODULE,
1156 .open = v4l2_fh_open,
1157 .unlocked_ioctl = video_ioctl2,
1158 .release = v4l2_fh_release,
1161 static int cobalt_node_register(struct cobalt *cobalt, int node)
1163 static const struct v4l2_dv_timings dv1080p60 =
1164 V4L2_DV_BT_CEA_1920X1080P60;
1165 struct cobalt_stream *s = cobalt->streams + node;
1166 struct video_device *vdev = &s->vdev;
1167 struct vb2_queue *q = &s->q;
1170 mutex_init(&s->lock);
1171 spin_lock_init(&s->irqlock);
1173 snprintf(vdev->name, sizeof(vdev->name),
1174 "%s-%d", cobalt->v4l2_dev.name, node);
1176 /* Audio frames are just 4 lines of 1920 bytes */
1177 s->height = s->is_audio ? 4 : 1080;
1181 s->pixfmt = V4L2_PIX_FMT_GREY;
1182 } else if (s->is_output) {
1183 s->bpp = COBALT_BYTES_PER_PIXEL_RGB32;
1184 s->pixfmt = V4L2_PIX_FMT_BGR32;
1186 s->bpp = COBALT_BYTES_PER_PIXEL_YUYV;
1187 s->pixfmt = V4L2_PIX_FMT_YUYV;
1189 s->colorspace = V4L2_COLORSPACE_SRGB;
1190 s->stride = s->width * s->bpp;
1194 cobalt_warn("Setting up dummy video node %d\n", node);
1195 vdev->v4l2_dev = &cobalt->v4l2_dev;
1197 vdev->fops = &cobalt_empty_fops;
1199 vdev->fops = s->is_output ? &cobalt_out_fops :
1201 vdev->release = video_device_release_empty;
1202 vdev->vfl_dir = s->is_output ? VFL_DIR_TX : VFL_DIR_RX;
1203 vdev->lock = &s->lock;
1205 vdev->ctrl_handler = s->sd->ctrl_handler;
1206 s->timings = dv1080p60;
1207 v4l2_subdev_call(s->sd, video, s_dv_timings, &s->timings);
1208 if (!s->is_output && s->sd)
1209 cobalt_enable_input(s);
1210 vdev->ioctl_ops = s->is_dummy ? &cobalt_ioctl_empty_ops :
1214 INIT_LIST_HEAD(&s->bufs);
1215 q->type = s->is_output ? V4L2_BUF_TYPE_VIDEO_OUTPUT :
1216 V4L2_BUF_TYPE_VIDEO_CAPTURE;
1217 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1218 q->io_modes |= s->is_output ? VB2_WRITE : VB2_READ;
1220 q->buf_struct_size = sizeof(struct cobalt_buffer);
1221 q->ops = &cobalt_qops;
1222 q->mem_ops = &vb2_dma_sg_memops;
1223 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1224 q->min_buffers_needed = 2;
1226 q->dev = &cobalt->pci_dev->dev;
1229 video_set_drvdata(vdev, s);
1230 ret = vb2_queue_init(q);
1231 if (!s->is_audio && ret == 0)
1232 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1233 else if (!s->is_dummy)
1234 ret = cobalt_alsa_init(s);
1238 cobalt_err("couldn't register v4l2 device node %d\n",
1242 cobalt_info("registered node %d\n", node);
1246 /* Initialize v4l2 variables and register v4l2 devices */
1247 int cobalt_nodes_register(struct cobalt *cobalt)
1251 /* Setup V4L2 Devices */
1252 for (node = 0; node < COBALT_NUM_STREAMS; node++) {
1253 ret = cobalt_node_register(cobalt, node);
1260 /* Unregister v4l2 devices */
1261 void cobalt_nodes_unregister(struct cobalt *cobalt)
1265 /* Teardown all streams */
1266 for (node = 0; node < COBALT_NUM_STREAMS; node++) {
1267 struct cobalt_stream *s = cobalt->streams + node;
1268 struct video_device *vdev = &s->vdev;
1271 video_unregister_device(vdev);
1272 else if (!s->is_dummy)
1273 cobalt_alsa_exit(s);