16884db3cSHans Verkuil // SPDX-License-Identifier: GPL-2.0-only 285756a06SHans Verkuil /* 385756a06SHans Verkuil * cobalt V4L2 API 485756a06SHans Verkuil * 585756a06SHans Verkuil * Derived from ivtv-ioctl.c and cx18-fileops.c 685756a06SHans Verkuil * 785756a06SHans Verkuil * Copyright 2012-2015 Cisco Systems, Inc. and/or its affiliates. 885756a06SHans Verkuil * All rights reserved. 985756a06SHans Verkuil */ 1085756a06SHans Verkuil 1185756a06SHans Verkuil #include <linux/dma-mapping.h> 1285756a06SHans Verkuil #include <linux/delay.h> 1399cabb18SHans Verkuil #include <linux/math64.h> 1485756a06SHans Verkuil #include <linux/pci.h> 1585756a06SHans Verkuil #include <linux/v4l2-dv-timings.h> 1685756a06SHans Verkuil 1785756a06SHans Verkuil #include <media/v4l2-ctrls.h> 1885756a06SHans Verkuil #include <media/v4l2-event.h> 19810c168cSHans Verkuil #include <media/v4l2-dv-timings.h> 20b5dcee22SMauro Carvalho Chehab #include <media/i2c/adv7604.h> 21b5dcee22SMauro Carvalho Chehab #include <media/i2c/adv7842.h> 2285756a06SHans Verkuil 2385756a06SHans Verkuil #include "cobalt-alsa.h" 2485756a06SHans Verkuil #include "cobalt-cpld.h" 2585756a06SHans Verkuil #include "cobalt-driver.h" 2685756a06SHans Verkuil #include "cobalt-v4l2.h" 2785756a06SHans Verkuil #include "cobalt-irq.h" 2885756a06SHans Verkuil #include "cobalt-omnitek.h" 2985756a06SHans Verkuil 3085756a06SHans Verkuil static const struct v4l2_dv_timings cea1080p60 = V4L2_DV_BT_CEA_1920X1080P60; 3185756a06SHans Verkuil 3285756a06SHans Verkuil /* vb2 DMA streaming ops */ 3385756a06SHans Verkuil 34df9ecb0cSHans Verkuil static int cobalt_queue_setup(struct vb2_queue *q, 3585756a06SHans Verkuil unsigned int *num_buffers, unsigned int *num_planes, 3636c0f8b3SHans Verkuil unsigned int sizes[], struct device *alloc_devs[]) 3785756a06SHans Verkuil { 3885756a06SHans Verkuil struct cobalt_stream *s = q->drv_priv; 3985756a06SHans Verkuil unsigned size = s->stride * s->height; 4085756a06SHans Verkuil 4185756a06SHans Verkuil if (*num_buffers < 3) 4285756a06SHans Verkuil *num_buffers = 3; 4385756a06SHans Verkuil if (*num_buffers > NR_BUFS) 4485756a06SHans Verkuil *num_buffers = NR_BUFS; 45df9ecb0cSHans Verkuil if (*num_planes) 46df9ecb0cSHans Verkuil return sizes[0] < size ? -EINVAL : 0; 47df9ecb0cSHans Verkuil *num_planes = 1; 48df9ecb0cSHans Verkuil sizes[0] = size; 4985756a06SHans Verkuil return 0; 5085756a06SHans Verkuil } 5185756a06SHans Verkuil 5285756a06SHans Verkuil static int cobalt_buf_init(struct vb2_buffer *vb) 5385756a06SHans Verkuil { 5485756a06SHans Verkuil struct cobalt_stream *s = vb->vb2_queue->drv_priv; 5585756a06SHans Verkuil struct cobalt *cobalt = s->cobalt; 5685756a06SHans Verkuil const size_t max_pages_per_line = 5785756a06SHans Verkuil (COBALT_MAX_WIDTH * COBALT_MAX_BPP) / PAGE_SIZE + 2; 5885756a06SHans Verkuil const size_t bytes = 5985756a06SHans Verkuil COBALT_MAX_HEIGHT * max_pages_per_line * 0x20; 6085756a06SHans Verkuil const size_t audio_bytes = ((1920 * 4) / PAGE_SIZE + 1) * 0x20; 612d700715SJunghak Sung struct sg_dma_desc_info *desc = &s->dma_desc_info[vb->index]; 6285756a06SHans Verkuil struct sg_table *sg_desc = vb2_dma_sg_plane_desc(vb, 0); 6385756a06SHans Verkuil unsigned size; 6485756a06SHans Verkuil int ret; 6585756a06SHans Verkuil 6685756a06SHans Verkuil size = s->stride * s->height; 6785756a06SHans Verkuil if (vb2_plane_size(vb, 0) < size) { 6885756a06SHans Verkuil cobalt_info("data will not fit into plane (%lu < %u)\n", 6985756a06SHans Verkuil vb2_plane_size(vb, 0), size); 7085756a06SHans Verkuil return -EINVAL; 7185756a06SHans Verkuil } 7285756a06SHans Verkuil 7385756a06SHans Verkuil if (desc->virt == NULL) { 7485756a06SHans Verkuil desc->dev = &cobalt->pci_dev->dev; 7585756a06SHans Verkuil descriptor_list_allocate(desc, 7685756a06SHans Verkuil s->is_audio ? audio_bytes : bytes); 7785756a06SHans Verkuil if (desc->virt == NULL) 7885756a06SHans Verkuil return -ENOMEM; 7985756a06SHans Verkuil } 8085756a06SHans Verkuil ret = descriptor_list_create(cobalt, sg_desc->sgl, 8185756a06SHans Verkuil !s->is_output, sg_desc->nents, size, 8285756a06SHans Verkuil s->width * s->bpp, s->stride, desc); 8385756a06SHans Verkuil if (ret) 8485756a06SHans Verkuil descriptor_list_free(desc); 8585756a06SHans Verkuil return ret; 8685756a06SHans Verkuil } 8785756a06SHans Verkuil 8885756a06SHans Verkuil static void cobalt_buf_cleanup(struct vb2_buffer *vb) 8985756a06SHans Verkuil { 9085756a06SHans Verkuil struct cobalt_stream *s = vb->vb2_queue->drv_priv; 912d700715SJunghak Sung struct sg_dma_desc_info *desc = &s->dma_desc_info[vb->index]; 9285756a06SHans Verkuil 9385756a06SHans Verkuil descriptor_list_free(desc); 9485756a06SHans Verkuil } 9585756a06SHans Verkuil 9685756a06SHans Verkuil static int cobalt_buf_prepare(struct vb2_buffer *vb) 9785756a06SHans Verkuil { 982d700715SJunghak Sung struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 9985756a06SHans Verkuil struct cobalt_stream *s = vb->vb2_queue->drv_priv; 10085756a06SHans Verkuil 10185756a06SHans Verkuil vb2_set_plane_payload(vb, 0, s->stride * s->height); 1022d700715SJunghak Sung vbuf->field = V4L2_FIELD_NONE; 10385756a06SHans Verkuil return 0; 10485756a06SHans Verkuil } 10585756a06SHans Verkuil 10685756a06SHans Verkuil static void chain_all_buffers(struct cobalt_stream *s) 10785756a06SHans Verkuil { 10885756a06SHans Verkuil struct sg_dma_desc_info *desc[NR_BUFS]; 10985756a06SHans Verkuil struct cobalt_buffer *cb; 11085756a06SHans Verkuil struct list_head *p; 11185756a06SHans Verkuil int i = 0; 11285756a06SHans Verkuil 11385756a06SHans Verkuil list_for_each(p, &s->bufs) { 11485756a06SHans Verkuil cb = list_entry(p, struct cobalt_buffer, list); 1152d700715SJunghak Sung desc[i] = &s->dma_desc_info[cb->vb.vb2_buf.index]; 11685756a06SHans Verkuil if (i > 0) 11785756a06SHans Verkuil descriptor_list_chain(desc[i-1], desc[i]); 11885756a06SHans Verkuil i++; 11985756a06SHans Verkuil } 12085756a06SHans Verkuil } 12185756a06SHans Verkuil 12285756a06SHans Verkuil static void cobalt_buf_queue(struct vb2_buffer *vb) 12385756a06SHans Verkuil { 1242d700715SJunghak Sung struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 12585756a06SHans Verkuil struct vb2_queue *q = vb->vb2_queue; 12685756a06SHans Verkuil struct cobalt_stream *s = q->drv_priv; 1272d700715SJunghak Sung struct cobalt_buffer *cb = to_cobalt_buffer(vbuf); 1282d700715SJunghak Sung struct sg_dma_desc_info *desc = &s->dma_desc_info[vb->index]; 12985756a06SHans Verkuil unsigned long flags; 13085756a06SHans Verkuil 13185756a06SHans Verkuil /* Prepare new buffer */ 13285756a06SHans Verkuil descriptor_list_loopback(desc); 13385756a06SHans Verkuil descriptor_list_interrupt_disable(desc); 13485756a06SHans Verkuil 13585756a06SHans Verkuil spin_lock_irqsave(&s->irqlock, flags); 13685756a06SHans Verkuil list_add_tail(&cb->list, &s->bufs); 13785756a06SHans Verkuil chain_all_buffers(s); 13885756a06SHans Verkuil spin_unlock_irqrestore(&s->irqlock, flags); 13985756a06SHans Verkuil } 14085756a06SHans Verkuil 14185756a06SHans Verkuil static void cobalt_enable_output(struct cobalt_stream *s) 14285756a06SHans Verkuil { 14385756a06SHans Verkuil struct cobalt *cobalt = s->cobalt; 14485756a06SHans Verkuil struct v4l2_bt_timings *bt = &s->timings.bt; 1454da70768SHans Verkuil struct m00514_syncgen_flow_evcnt_regmap __iomem *vo = 14685756a06SHans Verkuil COBALT_TX_BASE(cobalt); 14785756a06SHans Verkuil unsigned fmt = s->pixfmt != V4L2_PIX_FMT_BGR32 ? 14885756a06SHans Verkuil M00514_CONTROL_BITMAP_FORMAT_16_BPP_MSK : 0; 14985756a06SHans Verkuil struct v4l2_subdev_format sd_fmt = { 15085756a06SHans Verkuil .which = V4L2_SUBDEV_FORMAT_ACTIVE, 15185756a06SHans Verkuil }; 152605a74e6SHans Verkuil u64 clk = bt->pixelclock; 15385756a06SHans Verkuil 154605a74e6SHans Verkuil if (bt->flags & V4L2_DV_FL_REDUCED_FPS) 155605a74e6SHans Verkuil clk = div_u64(clk * 1000ULL, 1001); 156605a74e6SHans Verkuil if (!cobalt_cpld_set_freq(cobalt, clk)) { 15785756a06SHans Verkuil cobalt_err("pixelclock out of range\n"); 15885756a06SHans Verkuil return; 15985756a06SHans Verkuil } 16085756a06SHans Verkuil 16185756a06SHans Verkuil sd_fmt.format.colorspace = s->colorspace; 162136a5e9eSHans Verkuil sd_fmt.format.xfer_func = s->xfer_func; 16385756a06SHans Verkuil sd_fmt.format.ycbcr_enc = s->ycbcr_enc; 16485756a06SHans Verkuil sd_fmt.format.quantization = s->quantization; 16585756a06SHans Verkuil sd_fmt.format.width = bt->width; 16685756a06SHans Verkuil sd_fmt.format.height = bt->height; 16785756a06SHans Verkuil 16885756a06SHans Verkuil /* Set up FDMA packer */ 16985756a06SHans Verkuil switch (s->pixfmt) { 17085756a06SHans Verkuil case V4L2_PIX_FMT_YUYV: 17185756a06SHans Verkuil sd_fmt.format.code = MEDIA_BUS_FMT_UYVY8_1X16; 17285756a06SHans Verkuil break; 17385756a06SHans Verkuil case V4L2_PIX_FMT_BGR32: 17485756a06SHans Verkuil sd_fmt.format.code = MEDIA_BUS_FMT_RGB888_1X24; 17585756a06SHans Verkuil break; 17685756a06SHans Verkuil } 17785756a06SHans Verkuil v4l2_subdev_call(s->sd, pad, set_fmt, NULL, &sd_fmt); 17885756a06SHans Verkuil 1794da70768SHans Verkuil iowrite32(0, &vo->control); 18085756a06SHans Verkuil /* 1080p60 */ 1814da70768SHans Verkuil iowrite32(bt->hsync, &vo->sync_generator_h_sync_length); 1824da70768SHans Verkuil iowrite32(bt->hbackporch, &vo->sync_generator_h_backporch_length); 1834da70768SHans Verkuil iowrite32(bt->width, &vo->sync_generator_h_active_length); 1844da70768SHans Verkuil iowrite32(bt->hfrontporch, &vo->sync_generator_h_frontporch_length); 1854da70768SHans Verkuil iowrite32(bt->vsync, &vo->sync_generator_v_sync_length); 1864da70768SHans Verkuil iowrite32(bt->vbackporch, &vo->sync_generator_v_backporch_length); 1874da70768SHans Verkuil iowrite32(bt->height, &vo->sync_generator_v_active_length); 1884da70768SHans Verkuil iowrite32(bt->vfrontporch, &vo->sync_generator_v_frontporch_length); 1894da70768SHans Verkuil iowrite32(0x9900c1, &vo->error_color); 19085756a06SHans Verkuil 1914da70768SHans Verkuil iowrite32(M00514_CONTROL_BITMAP_SYNC_GENERATOR_LOAD_PARAM_MSK | fmt, 1924da70768SHans Verkuil &vo->control); 1934da70768SHans Verkuil iowrite32(M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK | fmt, &vo->control); 1944da70768SHans Verkuil iowrite32(M00514_CONTROL_BITMAP_SYNC_GENERATOR_ENABLE_MSK | 19585756a06SHans Verkuil M00514_CONTROL_BITMAP_FLOW_CTRL_OUTPUT_ENABLE_MSK | 1964da70768SHans Verkuil fmt, &vo->control); 19785756a06SHans Verkuil } 19885756a06SHans Verkuil 19985756a06SHans Verkuil static void cobalt_enable_input(struct cobalt_stream *s) 20085756a06SHans Verkuil { 20185756a06SHans Verkuil struct cobalt *cobalt = s->cobalt; 20285756a06SHans Verkuil int ch = (int)s->video_channel; 2034da70768SHans Verkuil struct m00235_fdma_packer_regmap __iomem *packer; 20485756a06SHans Verkuil struct v4l2_subdev_format sd_fmt_yuyv = { 20585756a06SHans Verkuil .pad = s->pad_source, 20685756a06SHans Verkuil .which = V4L2_SUBDEV_FORMAT_ACTIVE, 20785756a06SHans Verkuil .format.code = MEDIA_BUS_FMT_YUYV8_1X16, 20885756a06SHans Verkuil }; 20985756a06SHans Verkuil struct v4l2_subdev_format sd_fmt_rgb = { 21085756a06SHans Verkuil .pad = s->pad_source, 21185756a06SHans Verkuil .which = V4L2_SUBDEV_FORMAT_ACTIVE, 21285756a06SHans Verkuil .format.code = MEDIA_BUS_FMT_RGB888_1X24, 21385756a06SHans Verkuil }; 21485756a06SHans Verkuil 21585756a06SHans Verkuil cobalt_dbg(1, "video_channel %d (%s, %s)\n", 21685756a06SHans Verkuil s->video_channel, 21785756a06SHans Verkuil s->input == 0 ? "hdmi" : "generator", 21885756a06SHans Verkuil "YUYV"); 21985756a06SHans Verkuil 22085756a06SHans Verkuil packer = COBALT_CVI_PACKER(cobalt, ch); 22185756a06SHans Verkuil 22285756a06SHans Verkuil /* Set up FDMA packer */ 22385756a06SHans Verkuil switch (s->pixfmt) { 22485756a06SHans Verkuil case V4L2_PIX_FMT_YUYV: 2254da70768SHans Verkuil iowrite32(M00235_CONTROL_BITMAP_ENABLE_MSK | 2264da70768SHans Verkuil (1 << M00235_CONTROL_BITMAP_PACK_FORMAT_OFST), 2274da70768SHans Verkuil &packer->control); 22885756a06SHans Verkuil v4l2_subdev_call(s->sd, pad, set_fmt, NULL, 22985756a06SHans Verkuil &sd_fmt_yuyv); 23085756a06SHans Verkuil break; 23185756a06SHans Verkuil case V4L2_PIX_FMT_RGB24: 2324da70768SHans Verkuil iowrite32(M00235_CONTROL_BITMAP_ENABLE_MSK | 2334da70768SHans Verkuil (2 << M00235_CONTROL_BITMAP_PACK_FORMAT_OFST), 2344da70768SHans Verkuil &packer->control); 23585756a06SHans Verkuil v4l2_subdev_call(s->sd, pad, set_fmt, NULL, 23685756a06SHans Verkuil &sd_fmt_rgb); 23785756a06SHans Verkuil break; 23885756a06SHans Verkuil case V4L2_PIX_FMT_BGR32: 2394da70768SHans Verkuil iowrite32(M00235_CONTROL_BITMAP_ENABLE_MSK | 24085756a06SHans Verkuil M00235_CONTROL_BITMAP_ENDIAN_FORMAT_MSK | 2414da70768SHans Verkuil (3 << M00235_CONTROL_BITMAP_PACK_FORMAT_OFST), 2424da70768SHans Verkuil &packer->control); 24385756a06SHans Verkuil v4l2_subdev_call(s->sd, pad, set_fmt, NULL, 24485756a06SHans Verkuil &sd_fmt_rgb); 24585756a06SHans Verkuil break; 24685756a06SHans Verkuil } 24785756a06SHans Verkuil } 24885756a06SHans Verkuil 24985756a06SHans Verkuil static void cobalt_dma_start_streaming(struct cobalt_stream *s) 25085756a06SHans Verkuil { 25185756a06SHans Verkuil struct cobalt *cobalt = s->cobalt; 25285756a06SHans Verkuil int rx = s->video_channel; 2534da70768SHans Verkuil struct m00460_evcnt_regmap __iomem *evcnt = 25485756a06SHans Verkuil COBALT_CVI_EVCNT(cobalt, rx); 25585756a06SHans Verkuil struct cobalt_buffer *cb; 25685756a06SHans Verkuil unsigned long flags; 25785756a06SHans Verkuil 25885756a06SHans Verkuil spin_lock_irqsave(&s->irqlock, flags); 25985756a06SHans Verkuil if (!s->is_output) { 2604da70768SHans Verkuil iowrite32(M00460_CONTROL_BITMAP_CLEAR_MSK, &evcnt->control); 2614da70768SHans Verkuil iowrite32(M00460_CONTROL_BITMAP_ENABLE_MSK, &evcnt->control); 26285756a06SHans Verkuil } else { 2634da70768SHans Verkuil struct m00514_syncgen_flow_evcnt_regmap __iomem *vo = 26485756a06SHans Verkuil COBALT_TX_BASE(cobalt); 2654da70768SHans Verkuil u32 ctrl = ioread32(&vo->control); 26685756a06SHans Verkuil 26785756a06SHans Verkuil ctrl &= ~(M00514_CONTROL_BITMAP_EVCNT_ENABLE_MSK | 26885756a06SHans Verkuil M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK); 2694da70768SHans Verkuil iowrite32(ctrl | M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK, 2704da70768SHans Verkuil &vo->control); 2714da70768SHans Verkuil iowrite32(ctrl | M00514_CONTROL_BITMAP_EVCNT_ENABLE_MSK, 2724da70768SHans Verkuil &vo->control); 27385756a06SHans Verkuil } 27485756a06SHans Verkuil cb = list_first_entry(&s->bufs, struct cobalt_buffer, list); 2752d700715SJunghak Sung omni_sg_dma_start(s, &s->dma_desc_info[cb->vb.vb2_buf.index]); 27685756a06SHans Verkuil spin_unlock_irqrestore(&s->irqlock, flags); 27785756a06SHans Verkuil } 27885756a06SHans Verkuil 27985756a06SHans Verkuil static int cobalt_start_streaming(struct vb2_queue *q, unsigned int count) 28085756a06SHans Verkuil { 28185756a06SHans Verkuil struct cobalt_stream *s = q->drv_priv; 28285756a06SHans Verkuil struct cobalt *cobalt = s->cobalt; 2834da70768SHans Verkuil struct m00233_video_measure_regmap __iomem *vmr; 2844da70768SHans Verkuil struct m00473_freewheel_regmap __iomem *fw; 2854da70768SHans Verkuil struct m00479_clk_loss_detector_regmap __iomem *clkloss; 28685756a06SHans Verkuil int rx = s->video_channel; 2874da70768SHans Verkuil struct m00389_cvi_regmap __iomem *cvi = COBALT_CVI(cobalt, rx); 2884da70768SHans Verkuil struct m00460_evcnt_regmap __iomem *evcnt = COBALT_CVI_EVCNT(cobalt, rx); 28985756a06SHans Verkuil struct v4l2_bt_timings *bt = &s->timings.bt; 29085756a06SHans Verkuil u64 tot_size; 2914da70768SHans Verkuil u32 clk_freq; 29285756a06SHans Verkuil 29385756a06SHans Verkuil if (s->is_audio) 29485756a06SHans Verkuil goto done; 29585756a06SHans Verkuil if (s->is_output) { 29685756a06SHans Verkuil s->unstable_frame = false; 29785756a06SHans Verkuil cobalt_enable_output(s); 29885756a06SHans Verkuil goto done; 29985756a06SHans Verkuil } 30085756a06SHans Verkuil 30185756a06SHans Verkuil cobalt_enable_input(s); 30285756a06SHans Verkuil 30385756a06SHans Verkuil fw = COBALT_CVI_FREEWHEEL(cobalt, rx); 30485756a06SHans Verkuil vmr = COBALT_CVI_VMR(cobalt, rx); 30585756a06SHans Verkuil clkloss = COBALT_CVI_CLK_LOSS(cobalt, rx); 30685756a06SHans Verkuil 3074da70768SHans Verkuil iowrite32(M00460_CONTROL_BITMAP_CLEAR_MSK, &evcnt->control); 3084da70768SHans Verkuil iowrite32(M00460_CONTROL_BITMAP_ENABLE_MSK, &evcnt->control); 3094da70768SHans Verkuil iowrite32(bt->width, &cvi->frame_width); 3104da70768SHans Verkuil iowrite32(bt->height, &cvi->frame_height); 31185756a06SHans Verkuil tot_size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt); 3124da70768SHans Verkuil iowrite32(div_u64((u64)V4L2_DV_BT_FRAME_WIDTH(bt) * COBALT_CLK * 4, 3134da70768SHans Verkuil bt->pixelclock), &vmr->hsync_timeout_val); 3144da70768SHans Verkuil iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK, &vmr->control); 3154da70768SHans Verkuil clk_freq = ioread32(&fw->clk_freq); 3164da70768SHans Verkuil iowrite32(clk_freq / 1000000, &clkloss->ref_clk_cnt_val); 31785756a06SHans Verkuil /* The lower bound for the clock frequency is 0.5% lower as is 31885756a06SHans Verkuil * allowed by the spec */ 31995a86e43SHans Verkuil iowrite32(div_u64(bt->pixelclock * 995, 1000000000), 3204da70768SHans Verkuil &clkloss->test_clk_cnt_val); 32185756a06SHans Verkuil /* will be enabled after the first frame has been received */ 3224da70768SHans Verkuil iowrite32(bt->width * bt->height, &fw->active_length); 3234da70768SHans Verkuil iowrite32(div_u64((u64)clk_freq * tot_size, bt->pixelclock), 3244da70768SHans Verkuil &fw->total_length); 3254da70768SHans Verkuil iowrite32(M00233_IRQ_TRIGGERS_BITMAP_VACTIVE_AREA_MSK | 3264da70768SHans Verkuil M00233_IRQ_TRIGGERS_BITMAP_HACTIVE_AREA_MSK, 3274da70768SHans Verkuil &vmr->irq_triggers); 3284da70768SHans Verkuil iowrite32(0, &cvi->control); 3294da70768SHans Verkuil iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK, &vmr->control); 33085756a06SHans Verkuil 3314da70768SHans Verkuil iowrite32(0xff, &fw->output_color); 3324da70768SHans Verkuil iowrite32(M00479_CTRL_BITMAP_ENABLE_MSK, &clkloss->ctrl); 3334da70768SHans Verkuil iowrite32(M00473_CTRL_BITMAP_ENABLE_MSK | 3344da70768SHans Verkuil M00473_CTRL_BITMAP_FORCE_FREEWHEEL_MODE_MSK, &fw->ctrl); 33585756a06SHans Verkuil s->unstable_frame = true; 33685756a06SHans Verkuil s->enable_freewheel = false; 33785756a06SHans Verkuil s->enable_cvi = false; 33885756a06SHans Verkuil s->skip_first_frames = 0; 33985756a06SHans Verkuil 34085756a06SHans Verkuil done: 34185756a06SHans Verkuil s->sequence = 0; 34285756a06SHans Verkuil cobalt_dma_start_streaming(s); 34385756a06SHans Verkuil return 0; 34485756a06SHans Verkuil } 34585756a06SHans Verkuil 34685756a06SHans Verkuil static void cobalt_dma_stop_streaming(struct cobalt_stream *s) 34785756a06SHans Verkuil { 34885756a06SHans Verkuil struct cobalt *cobalt = s->cobalt; 34985756a06SHans Verkuil struct sg_dma_desc_info *desc; 35085756a06SHans Verkuil struct cobalt_buffer *cb; 35185756a06SHans Verkuil struct list_head *p; 35285756a06SHans Verkuil unsigned long flags; 35385756a06SHans Verkuil int timeout_msec = 100; 35485756a06SHans Verkuil int rx = s->video_channel; 3554da70768SHans Verkuil struct m00460_evcnt_regmap __iomem *evcnt = 35685756a06SHans Verkuil COBALT_CVI_EVCNT(cobalt, rx); 35785756a06SHans Verkuil 35885756a06SHans Verkuil if (!s->is_output) { 3594da70768SHans Verkuil iowrite32(0, &evcnt->control); 36085756a06SHans Verkuil } else if (!s->is_audio) { 3614da70768SHans Verkuil struct m00514_syncgen_flow_evcnt_regmap __iomem *vo = 36285756a06SHans Verkuil COBALT_TX_BASE(cobalt); 36385756a06SHans Verkuil 3644da70768SHans Verkuil iowrite32(M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK, &vo->control); 3654da70768SHans Verkuil iowrite32(0, &vo->control); 36685756a06SHans Verkuil } 36785756a06SHans Verkuil 36885756a06SHans Verkuil /* Try to stop the DMA engine gracefully */ 36985756a06SHans Verkuil spin_lock_irqsave(&s->irqlock, flags); 37085756a06SHans Verkuil list_for_each(p, &s->bufs) { 37185756a06SHans Verkuil cb = list_entry(p, struct cobalt_buffer, list); 3722d700715SJunghak Sung desc = &s->dma_desc_info[cb->vb.vb2_buf.index]; 37385756a06SHans Verkuil /* Stop DMA after this descriptor chain */ 37485756a06SHans Verkuil descriptor_list_end_of_chain(desc); 37585756a06SHans Verkuil } 37685756a06SHans Verkuil spin_unlock_irqrestore(&s->irqlock, flags); 37785756a06SHans Verkuil 378*16790554SMauro Carvalho Chehab /* Wait 100 millisecond for DMA to finish, abort on timeout. */ 37985756a06SHans Verkuil if (!wait_event_timeout(s->q.done_wq, is_dma_done(s), 38085756a06SHans Verkuil msecs_to_jiffies(timeout_msec))) { 38185756a06SHans Verkuil omni_sg_dma_abort_channel(s); 38285756a06SHans Verkuil pr_warn("aborted\n"); 38385756a06SHans Verkuil } 38485756a06SHans Verkuil cobalt_write_bar0(cobalt, DMA_INTERRUPT_STATUS_REG, 38585756a06SHans Verkuil 1 << s->dma_channel); 38685756a06SHans Verkuil } 38785756a06SHans Verkuil 38885756a06SHans Verkuil static void cobalt_stop_streaming(struct vb2_queue *q) 38985756a06SHans Verkuil { 39085756a06SHans Verkuil struct cobalt_stream *s = q->drv_priv; 39185756a06SHans Verkuil struct cobalt *cobalt = s->cobalt; 39285756a06SHans Verkuil int rx = s->video_channel; 3934da70768SHans Verkuil struct m00233_video_measure_regmap __iomem *vmr; 3944da70768SHans Verkuil struct m00473_freewheel_regmap __iomem *fw; 3954da70768SHans Verkuil struct m00479_clk_loss_detector_regmap __iomem *clkloss; 39685756a06SHans Verkuil struct cobalt_buffer *cb; 39785756a06SHans Verkuil struct list_head *p, *safe; 39885756a06SHans Verkuil unsigned long flags; 39985756a06SHans Verkuil 40085756a06SHans Verkuil cobalt_dma_stop_streaming(s); 40185756a06SHans Verkuil 40285756a06SHans Verkuil /* Return all buffers to user space */ 40385756a06SHans Verkuil spin_lock_irqsave(&s->irqlock, flags); 40485756a06SHans Verkuil list_for_each_safe(p, safe, &s->bufs) { 40585756a06SHans Verkuil cb = list_entry(p, struct cobalt_buffer, list); 40685756a06SHans Verkuil list_del(&cb->list); 4072d700715SJunghak Sung vb2_buffer_done(&cb->vb.vb2_buf, VB2_BUF_STATE_ERROR); 40885756a06SHans Verkuil } 40985756a06SHans Verkuil spin_unlock_irqrestore(&s->irqlock, flags); 41085756a06SHans Verkuil 41185756a06SHans Verkuil if (s->is_audio || s->is_output) 41285756a06SHans Verkuil return; 41385756a06SHans Verkuil 41485756a06SHans Verkuil fw = COBALT_CVI_FREEWHEEL(cobalt, rx); 41585756a06SHans Verkuil vmr = COBALT_CVI_VMR(cobalt, rx); 41685756a06SHans Verkuil clkloss = COBALT_CVI_CLK_LOSS(cobalt, rx); 4174da70768SHans Verkuil iowrite32(0, &vmr->control); 4184da70768SHans Verkuil iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK, &vmr->control); 4194da70768SHans Verkuil iowrite32(0, &fw->ctrl); 4204da70768SHans Verkuil iowrite32(0, &clkloss->ctrl); 42185756a06SHans Verkuil } 42285756a06SHans Verkuil 42385756a06SHans Verkuil static const struct vb2_ops cobalt_qops = { 42485756a06SHans Verkuil .queue_setup = cobalt_queue_setup, 42585756a06SHans Verkuil .buf_init = cobalt_buf_init, 42685756a06SHans Verkuil .buf_cleanup = cobalt_buf_cleanup, 42785756a06SHans Verkuil .buf_prepare = cobalt_buf_prepare, 42885756a06SHans Verkuil .buf_queue = cobalt_buf_queue, 42985756a06SHans Verkuil .start_streaming = cobalt_start_streaming, 43085756a06SHans Verkuil .stop_streaming = cobalt_stop_streaming, 43185756a06SHans Verkuil .wait_prepare = vb2_ops_wait_prepare, 43285756a06SHans Verkuil .wait_finish = vb2_ops_wait_finish, 43385756a06SHans Verkuil }; 43485756a06SHans Verkuil 43585756a06SHans Verkuil /* V4L2 ioctls */ 43685756a06SHans Verkuil 43785756a06SHans Verkuil #ifdef CONFIG_VIDEO_ADV_DEBUG 43885756a06SHans Verkuil static int cobalt_cobaltc(struct cobalt *cobalt, unsigned int cmd, void *arg) 43985756a06SHans Verkuil { 44085756a06SHans Verkuil struct v4l2_dbg_register *regs = arg; 44185756a06SHans Verkuil void __iomem *adrs = cobalt->bar1 + regs->reg; 44285756a06SHans Verkuil 44385756a06SHans Verkuil cobalt_info("cobalt_cobaltc: adrs = %p\n", adrs); 44485756a06SHans Verkuil 44585756a06SHans Verkuil if (!capable(CAP_SYS_ADMIN)) 44685756a06SHans Verkuil return -EPERM; 44785756a06SHans Verkuil 44885756a06SHans Verkuil regs->size = 4; 44985756a06SHans Verkuil if (cmd == VIDIOC_DBG_S_REGISTER) 45085756a06SHans Verkuil iowrite32(regs->val, adrs); 45185756a06SHans Verkuil else 45285756a06SHans Verkuil regs->val = ioread32(adrs); 45385756a06SHans Verkuil return 0; 45485756a06SHans Verkuil } 45585756a06SHans Verkuil 45685756a06SHans Verkuil static int cobalt_g_register(struct file *file, void *priv_fh, 45785756a06SHans Verkuil struct v4l2_dbg_register *reg) 45885756a06SHans Verkuil { 45985756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file); 46085756a06SHans Verkuil struct cobalt *cobalt = s->cobalt; 46185756a06SHans Verkuil 46285756a06SHans Verkuil return cobalt_cobaltc(cobalt, VIDIOC_DBG_G_REGISTER, reg); 46385756a06SHans Verkuil } 46485756a06SHans Verkuil 46585756a06SHans Verkuil static int cobalt_s_register(struct file *file, void *priv_fh, 46685756a06SHans Verkuil const struct v4l2_dbg_register *reg) 46785756a06SHans Verkuil { 46885756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file); 46985756a06SHans Verkuil struct cobalt *cobalt = s->cobalt; 47085756a06SHans Verkuil 47185756a06SHans Verkuil return cobalt_cobaltc(cobalt, VIDIOC_DBG_S_REGISTER, 47285756a06SHans Verkuil (struct v4l2_dbg_register *)reg); 47385756a06SHans Verkuil } 47485756a06SHans Verkuil #endif 47585756a06SHans Verkuil 47685756a06SHans Verkuil static int cobalt_querycap(struct file *file, void *priv_fh, 47785756a06SHans Verkuil struct v4l2_capability *vcap) 47885756a06SHans Verkuil { 47985756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file); 48085756a06SHans Verkuil struct cobalt *cobalt = s->cobalt; 48185756a06SHans Verkuil 482c0decac1SMauro Carvalho Chehab strscpy(vcap->driver, "cobalt", sizeof(vcap->driver)); 483c0decac1SMauro Carvalho Chehab strscpy(vcap->card, "cobalt", sizeof(vcap->card)); 48485756a06SHans Verkuil snprintf(vcap->bus_info, sizeof(vcap->bus_info), 48585756a06SHans Verkuil "PCIe:%s", pci_name(cobalt->pci_dev)); 48685756a06SHans Verkuil vcap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_READWRITE; 48785756a06SHans Verkuil if (s->is_output) 48885756a06SHans Verkuil vcap->device_caps |= V4L2_CAP_VIDEO_OUTPUT; 48985756a06SHans Verkuil else 49085756a06SHans Verkuil vcap->device_caps |= V4L2_CAP_VIDEO_CAPTURE; 49185756a06SHans Verkuil vcap->capabilities = vcap->device_caps | V4L2_CAP_DEVICE_CAPS | 49285756a06SHans Verkuil V4L2_CAP_VIDEO_CAPTURE; 49385756a06SHans Verkuil if (cobalt->have_hsma_tx) 49485756a06SHans Verkuil vcap->capabilities |= V4L2_CAP_VIDEO_OUTPUT; 49585756a06SHans Verkuil return 0; 49685756a06SHans Verkuil } 49785756a06SHans Verkuil 49885756a06SHans Verkuil static void cobalt_video_input_status_show(struct cobalt_stream *s) 49985756a06SHans Verkuil { 5004da70768SHans Verkuil struct m00389_cvi_regmap __iomem *cvi; 5014da70768SHans Verkuil struct m00233_video_measure_regmap __iomem *vmr; 5024da70768SHans Verkuil struct m00473_freewheel_regmap __iomem *fw; 5034da70768SHans Verkuil struct m00479_clk_loss_detector_regmap __iomem *clkloss; 5044da70768SHans Verkuil struct m00235_fdma_packer_regmap __iomem *packer; 50585756a06SHans Verkuil int rx = s->video_channel; 50685756a06SHans Verkuil struct cobalt *cobalt = s->cobalt; 5074da70768SHans Verkuil u32 cvi_ctrl, cvi_stat; 5084da70768SHans Verkuil u32 vmr_ctrl, vmr_stat; 50985756a06SHans Verkuil 51085756a06SHans Verkuil cvi = COBALT_CVI(cobalt, rx); 51185756a06SHans Verkuil vmr = COBALT_CVI_VMR(cobalt, rx); 51285756a06SHans Verkuil fw = COBALT_CVI_FREEWHEEL(cobalt, rx); 51385756a06SHans Verkuil clkloss = COBALT_CVI_CLK_LOSS(cobalt, rx); 51485756a06SHans Verkuil packer = COBALT_CVI_PACKER(cobalt, rx); 5154da70768SHans Verkuil cvi_ctrl = ioread32(&cvi->control); 5164da70768SHans Verkuil cvi_stat = ioread32(&cvi->status); 5174da70768SHans Verkuil vmr_ctrl = ioread32(&vmr->control); 5189f750b2bSHans Verkuil vmr_stat = ioread32(&vmr->status); 51985756a06SHans Verkuil cobalt_info("rx%d: cvi resolution: %dx%d\n", rx, 5204da70768SHans Verkuil ioread32(&cvi->frame_width), ioread32(&cvi->frame_height)); 52185756a06SHans Verkuil cobalt_info("rx%d: cvi control: %s%s%s\n", rx, 5224da70768SHans Verkuil (cvi_ctrl & M00389_CONTROL_BITMAP_ENABLE_MSK) ? 52385756a06SHans Verkuil "enable " : "disable ", 5244da70768SHans Verkuil (cvi_ctrl & M00389_CONTROL_BITMAP_HSYNC_POLARITY_LOW_MSK) ? 52585756a06SHans Verkuil "HSync- " : "HSync+ ", 5264da70768SHans Verkuil (cvi_ctrl & M00389_CONTROL_BITMAP_VSYNC_POLARITY_LOW_MSK) ? 52785756a06SHans Verkuil "VSync- " : "VSync+ "); 52885756a06SHans Verkuil cobalt_info("rx%d: cvi status: %s%s\n", rx, 5294da70768SHans Verkuil (cvi_stat & M00389_STATUS_BITMAP_LOCK_MSK) ? 53085756a06SHans Verkuil "lock " : "no-lock ", 5314da70768SHans Verkuil (cvi_stat & M00389_STATUS_BITMAP_ERROR_MSK) ? 53285756a06SHans Verkuil "error " : "no-error "); 53385756a06SHans Verkuil 53485756a06SHans Verkuil cobalt_info("rx%d: Measurements: %s%s%s%s%s%s%s\n", rx, 5354da70768SHans Verkuil (vmr_ctrl & M00233_CONTROL_BITMAP_HSYNC_POLARITY_LOW_MSK) ? 53685756a06SHans Verkuil "HSync- " : "HSync+ ", 5374da70768SHans Verkuil (vmr_ctrl & M00233_CONTROL_BITMAP_VSYNC_POLARITY_LOW_MSK) ? 53885756a06SHans Verkuil "VSync- " : "VSync+ ", 5394da70768SHans Verkuil (vmr_ctrl & M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK) ? 54085756a06SHans Verkuil "enabled " : "disabled ", 5414da70768SHans Verkuil (vmr_ctrl & M00233_CONTROL_BITMAP_ENABLE_INTERRUPT_MSK) ? 54285756a06SHans Verkuil "irq-enabled " : "irq-disabled ", 5434da70768SHans Verkuil (vmr_ctrl & M00233_CONTROL_BITMAP_UPDATE_ON_HSYNC_MSK) ? 54485756a06SHans Verkuil "update-on-hsync " : "", 5454da70768SHans Verkuil (vmr_stat & M00233_STATUS_BITMAP_HSYNC_TIMEOUT_MSK) ? 54685756a06SHans Verkuil "hsync-timeout " : "", 5474da70768SHans Verkuil (vmr_stat & M00233_STATUS_BITMAP_INIT_DONE_MSK) ? 54885756a06SHans Verkuil "init-done" : ""); 54985756a06SHans Verkuil cobalt_info("rx%d: irq_status: 0x%02x irq_triggers: 0x%02x\n", rx, 5504da70768SHans Verkuil ioread32(&vmr->irq_status) & 0xff, 5514da70768SHans Verkuil ioread32(&vmr->irq_triggers) & 0xff); 5524da70768SHans Verkuil cobalt_info("rx%d: vsync: %d\n", rx, ioread32(&vmr->vsync_time)); 5534da70768SHans Verkuil cobalt_info("rx%d: vbp: %d\n", rx, ioread32(&vmr->vback_porch)); 5544da70768SHans Verkuil cobalt_info("rx%d: vact: %d\n", rx, ioread32(&vmr->vactive_area)); 5554da70768SHans Verkuil cobalt_info("rx%d: vfb: %d\n", rx, ioread32(&vmr->vfront_porch)); 5564da70768SHans Verkuil cobalt_info("rx%d: hsync: %d\n", rx, ioread32(&vmr->hsync_time)); 5574da70768SHans Verkuil cobalt_info("rx%d: hbp: %d\n", rx, ioread32(&vmr->hback_porch)); 5584da70768SHans Verkuil cobalt_info("rx%d: hact: %d\n", rx, ioread32(&vmr->hactive_area)); 5594da70768SHans Verkuil cobalt_info("rx%d: hfb: %d\n", rx, ioread32(&vmr->hfront_porch)); 56085756a06SHans Verkuil cobalt_info("rx%d: Freewheeling: %s%s%s\n", rx, 5614da70768SHans Verkuil (ioread32(&fw->ctrl) & M00473_CTRL_BITMAP_ENABLE_MSK) ? 56285756a06SHans Verkuil "enabled " : "disabled ", 5634da70768SHans Verkuil (ioread32(&fw->ctrl) & M00473_CTRL_BITMAP_FORCE_FREEWHEEL_MODE_MSK) ? 56485756a06SHans Verkuil "forced " : "", 5654da70768SHans Verkuil (ioread32(&fw->status) & M00473_STATUS_BITMAP_FREEWHEEL_MODE_MSK) ? 56685756a06SHans Verkuil "freewheeling " : "video-passthrough "); 5674da70768SHans Verkuil iowrite32(0xff, &vmr->irq_status); 56885756a06SHans Verkuil cobalt_info("rx%d: Clock Loss Detection: %s%s\n", rx, 5694da70768SHans Verkuil (ioread32(&clkloss->ctrl) & M00479_CTRL_BITMAP_ENABLE_MSK) ? 57085756a06SHans Verkuil "enabled " : "disabled ", 5714da70768SHans Verkuil (ioread32(&clkloss->status) & M00479_STATUS_BITMAP_CLOCK_MISSING_MSK) ? 57285756a06SHans Verkuil "clock-missing " : "found-clock "); 5734da70768SHans Verkuil cobalt_info("rx%d: Packer: %x\n", rx, ioread32(&packer->control)); 57485756a06SHans Verkuil } 57585756a06SHans Verkuil 57685756a06SHans Verkuil static int cobalt_log_status(struct file *file, void *priv_fh) 57785756a06SHans Verkuil { 57885756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file); 57985756a06SHans Verkuil struct cobalt *cobalt = s->cobalt; 5804da70768SHans Verkuil struct m00514_syncgen_flow_evcnt_regmap __iomem *vo = 58185756a06SHans Verkuil COBALT_TX_BASE(cobalt); 58285756a06SHans Verkuil u8 stat; 58385756a06SHans Verkuil 58485756a06SHans Verkuil cobalt_info("%s", cobalt->hdl_info); 58585756a06SHans Verkuil cobalt_info("sysctrl: %08x, sysstat: %08x\n", 58685756a06SHans Verkuil cobalt_g_sysctrl(cobalt), 58785756a06SHans Verkuil cobalt_g_sysstat(cobalt)); 58885756a06SHans Verkuil cobalt_info("dma channel: %d, video channel: %d\n", 58985756a06SHans Verkuil s->dma_channel, s->video_channel); 59085756a06SHans Verkuil cobalt_pcie_status_show(cobalt); 59185756a06SHans Verkuil cobalt_cpld_status(cobalt); 59285756a06SHans Verkuil cobalt_irq_log_status(cobalt); 59385756a06SHans Verkuil v4l2_subdev_call(s->sd, core, log_status); 59485756a06SHans Verkuil if (!s->is_output) { 59585756a06SHans Verkuil cobalt_video_input_status_show(s); 59685756a06SHans Verkuil return 0; 59785756a06SHans Verkuil } 59885756a06SHans Verkuil 5994da70768SHans Verkuil stat = ioread32(&vo->rd_status); 60085756a06SHans Verkuil 60185756a06SHans Verkuil cobalt_info("tx: status: %s%s\n", 60285756a06SHans Verkuil (stat & M00514_RD_STATUS_BITMAP_FLOW_CTRL_NO_DATA_ERROR_MSK) ? 60385756a06SHans Verkuil "no_data " : "", 60485756a06SHans Verkuil (stat & M00514_RD_STATUS_BITMAP_READY_BUFFER_FULL_MSK) ? 60585756a06SHans Verkuil "ready_buffer_full " : ""); 6064da70768SHans Verkuil cobalt_info("tx: evcnt: %d\n", ioread32(&vo->rd_evcnt_count)); 60785756a06SHans Verkuil return 0; 60885756a06SHans Verkuil } 60985756a06SHans Verkuil 61085756a06SHans Verkuil static int cobalt_enum_dv_timings(struct file *file, void *priv_fh, 61185756a06SHans Verkuil struct v4l2_enum_dv_timings *timings) 61285756a06SHans Verkuil { 61385756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file); 61485756a06SHans Verkuil 61585756a06SHans Verkuil if (s->input == 1) { 61685756a06SHans Verkuil if (timings->index) 61785756a06SHans Verkuil return -EINVAL; 61885756a06SHans Verkuil memset(timings->reserved, 0, sizeof(timings->reserved)); 61985756a06SHans Verkuil timings->timings = cea1080p60; 62085756a06SHans Verkuil return 0; 62185756a06SHans Verkuil } 62285756a06SHans Verkuil timings->pad = 0; 62385756a06SHans Verkuil return v4l2_subdev_call(s->sd, 62485756a06SHans Verkuil pad, enum_dv_timings, timings); 62585756a06SHans Verkuil } 62685756a06SHans Verkuil 62785756a06SHans Verkuil static int cobalt_s_dv_timings(struct file *file, void *priv_fh, 62885756a06SHans Verkuil struct v4l2_dv_timings *timings) 62985756a06SHans Verkuil { 63085756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file); 63185756a06SHans Verkuil int err; 63285756a06SHans Verkuil 63385756a06SHans Verkuil if (s->input == 1) { 63485756a06SHans Verkuil *timings = cea1080p60; 63585756a06SHans Verkuil return 0; 63685756a06SHans Verkuil } 637810c168cSHans Verkuil 638605a74e6SHans Verkuil if (v4l2_match_dv_timings(timings, &s->timings, 0, true)) 639810c168cSHans Verkuil return 0; 640810c168cSHans Verkuil 641810c168cSHans Verkuil if (vb2_is_busy(&s->q)) 642810c168cSHans Verkuil return -EBUSY; 643810c168cSHans Verkuil 64485756a06SHans Verkuil err = v4l2_subdev_call(s->sd, 64585756a06SHans Verkuil video, s_dv_timings, timings); 64685756a06SHans Verkuil if (!err) { 64785756a06SHans Verkuil s->timings = *timings; 64885756a06SHans Verkuil s->width = timings->bt.width; 64985756a06SHans Verkuil s->height = timings->bt.height; 65085756a06SHans Verkuil s->stride = timings->bt.width * s->bpp; 65185756a06SHans Verkuil } 65285756a06SHans Verkuil return err; 65385756a06SHans Verkuil } 65485756a06SHans Verkuil 65585756a06SHans Verkuil static int cobalt_g_dv_timings(struct file *file, void *priv_fh, 65685756a06SHans Verkuil struct v4l2_dv_timings *timings) 65785756a06SHans Verkuil { 65885756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file); 65985756a06SHans Verkuil 66085756a06SHans Verkuil if (s->input == 1) { 66185756a06SHans Verkuil *timings = cea1080p60; 66285756a06SHans Verkuil return 0; 66385756a06SHans Verkuil } 66485756a06SHans Verkuil return v4l2_subdev_call(s->sd, 66585756a06SHans Verkuil video, g_dv_timings, timings); 66685756a06SHans Verkuil } 66785756a06SHans Verkuil 66885756a06SHans Verkuil static int cobalt_query_dv_timings(struct file *file, void *priv_fh, 66985756a06SHans Verkuil struct v4l2_dv_timings *timings) 67085756a06SHans Verkuil { 67185756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file); 67285756a06SHans Verkuil 67385756a06SHans Verkuil if (s->input == 1) { 67485756a06SHans Verkuil *timings = cea1080p60; 67585756a06SHans Verkuil return 0; 67685756a06SHans Verkuil } 67785756a06SHans Verkuil return v4l2_subdev_call(s->sd, 67885756a06SHans Verkuil video, query_dv_timings, timings); 67985756a06SHans Verkuil } 68085756a06SHans Verkuil 68185756a06SHans Verkuil static int cobalt_dv_timings_cap(struct file *file, void *priv_fh, 68285756a06SHans Verkuil struct v4l2_dv_timings_cap *cap) 68385756a06SHans Verkuil { 68485756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file); 68585756a06SHans Verkuil 68685756a06SHans Verkuil cap->pad = 0; 68785756a06SHans Verkuil return v4l2_subdev_call(s->sd, 68885756a06SHans Verkuil pad, dv_timings_cap, cap); 68985756a06SHans Verkuil } 69085756a06SHans Verkuil 69185756a06SHans Verkuil static int cobalt_enum_fmt_vid_cap(struct file *file, void *priv_fh, 69285756a06SHans Verkuil struct v4l2_fmtdesc *f) 69385756a06SHans Verkuil { 69485756a06SHans Verkuil switch (f->index) { 69585756a06SHans Verkuil case 0: 696c0decac1SMauro Carvalho Chehab strscpy(f->description, "YUV 4:2:2", sizeof(f->description)); 69785756a06SHans Verkuil f->pixelformat = V4L2_PIX_FMT_YUYV; 69885756a06SHans Verkuil break; 69985756a06SHans Verkuil case 1: 700c0decac1SMauro Carvalho Chehab strscpy(f->description, "RGB24", sizeof(f->description)); 70185756a06SHans Verkuil f->pixelformat = V4L2_PIX_FMT_RGB24; 70285756a06SHans Verkuil break; 70385756a06SHans Verkuil case 2: 704c0decac1SMauro Carvalho Chehab strscpy(f->description, "RGB32", sizeof(f->description)); 70585756a06SHans Verkuil f->pixelformat = V4L2_PIX_FMT_BGR32; 70685756a06SHans Verkuil break; 70785756a06SHans Verkuil default: 70885756a06SHans Verkuil return -EINVAL; 70985756a06SHans Verkuil } 71085756a06SHans Verkuil 71185756a06SHans Verkuil return 0; 71285756a06SHans Verkuil } 71385756a06SHans Verkuil 71485756a06SHans Verkuil static int cobalt_g_fmt_vid_cap(struct file *file, void *priv_fh, 71585756a06SHans Verkuil struct v4l2_format *f) 71685756a06SHans Verkuil { 71785756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file); 71885756a06SHans Verkuil struct v4l2_pix_format *pix = &f->fmt.pix; 71985756a06SHans Verkuil struct v4l2_subdev_format sd_fmt; 72085756a06SHans Verkuil 72185756a06SHans Verkuil pix->width = s->width; 72285756a06SHans Verkuil pix->height = s->height; 72385756a06SHans Verkuil pix->bytesperline = s->stride; 72485756a06SHans Verkuil pix->field = V4L2_FIELD_NONE; 72585756a06SHans Verkuil 72685756a06SHans Verkuil if (s->input == 1) { 72785756a06SHans Verkuil pix->colorspace = V4L2_COLORSPACE_SRGB; 72885756a06SHans Verkuil } else { 72985756a06SHans Verkuil sd_fmt.pad = s->pad_source; 73085756a06SHans Verkuil sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; 73185756a06SHans Verkuil v4l2_subdev_call(s->sd, pad, get_fmt, NULL, &sd_fmt); 73285756a06SHans Verkuil v4l2_fill_pix_format(pix, &sd_fmt.format); 73385756a06SHans Verkuil } 73485756a06SHans Verkuil 73585756a06SHans Verkuil pix->pixelformat = s->pixfmt; 73685756a06SHans Verkuil pix->sizeimage = pix->bytesperline * pix->height; 73785756a06SHans Verkuil 73885756a06SHans Verkuil return 0; 73985756a06SHans Verkuil } 74085756a06SHans Verkuil 74185756a06SHans Verkuil static int cobalt_try_fmt_vid_cap(struct file *file, void *priv_fh, 74285756a06SHans Verkuil struct v4l2_format *f) 74385756a06SHans Verkuil { 74485756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file); 74585756a06SHans Verkuil struct v4l2_pix_format *pix = &f->fmt.pix; 74685756a06SHans Verkuil struct v4l2_subdev_format sd_fmt; 74785756a06SHans Verkuil 74885756a06SHans Verkuil /* Check for min (QCIF) and max (Full HD) size */ 74985756a06SHans Verkuil if ((pix->width < 176) || (pix->height < 144)) { 75085756a06SHans Verkuil pix->width = 176; 75185756a06SHans Verkuil pix->height = 144; 75285756a06SHans Verkuil } 75385756a06SHans Verkuil 75485756a06SHans Verkuil if ((pix->width > 1920) || (pix->height > 1080)) { 75585756a06SHans Verkuil pix->width = 1920; 75685756a06SHans Verkuil pix->height = 1080; 75785756a06SHans Verkuil } 75885756a06SHans Verkuil 75985756a06SHans Verkuil /* Make width multiple of 4 */ 76085756a06SHans Verkuil pix->width &= ~0x3; 76185756a06SHans Verkuil 76285756a06SHans Verkuil /* Make height multiple of 2 */ 76385756a06SHans Verkuil pix->height &= ~0x1; 76485756a06SHans Verkuil 76585756a06SHans Verkuil if (s->input == 1) { 76685756a06SHans Verkuil /* Generator => fixed format only */ 76785756a06SHans Verkuil pix->width = 1920; 76885756a06SHans Verkuil pix->height = 1080; 76985756a06SHans Verkuil pix->colorspace = V4L2_COLORSPACE_SRGB; 77085756a06SHans Verkuil } else { 77185756a06SHans Verkuil sd_fmt.pad = s->pad_source; 77285756a06SHans Verkuil sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; 77385756a06SHans Verkuil v4l2_subdev_call(s->sd, pad, get_fmt, NULL, &sd_fmt); 77485756a06SHans Verkuil v4l2_fill_pix_format(pix, &sd_fmt.format); 77585756a06SHans Verkuil } 77685756a06SHans Verkuil 77785756a06SHans Verkuil switch (pix->pixelformat) { 77885756a06SHans Verkuil case V4L2_PIX_FMT_YUYV: 77985756a06SHans Verkuil default: 78085756a06SHans Verkuil pix->bytesperline = max(pix->bytesperline & ~0x3, 78185756a06SHans Verkuil pix->width * COBALT_BYTES_PER_PIXEL_YUYV); 78285756a06SHans Verkuil pix->pixelformat = V4L2_PIX_FMT_YUYV; 78385756a06SHans Verkuil break; 78485756a06SHans Verkuil case V4L2_PIX_FMT_RGB24: 78585756a06SHans Verkuil pix->bytesperline = max(pix->bytesperline & ~0x3, 78685756a06SHans Verkuil pix->width * COBALT_BYTES_PER_PIXEL_RGB24); 78785756a06SHans Verkuil break; 78885756a06SHans Verkuil case V4L2_PIX_FMT_BGR32: 78985756a06SHans Verkuil pix->bytesperline = max(pix->bytesperline & ~0x3, 79085756a06SHans Verkuil pix->width * COBALT_BYTES_PER_PIXEL_RGB32); 79185756a06SHans Verkuil break; 79285756a06SHans Verkuil } 79385756a06SHans Verkuil 79485756a06SHans Verkuil pix->sizeimage = pix->bytesperline * pix->height; 79585756a06SHans Verkuil pix->field = V4L2_FIELD_NONE; 79685756a06SHans Verkuil pix->priv = 0; 79785756a06SHans Verkuil 79885756a06SHans Verkuil return 0; 79985756a06SHans Verkuil } 80085756a06SHans Verkuil 80185756a06SHans Verkuil static int cobalt_s_fmt_vid_cap(struct file *file, void *priv_fh, 80285756a06SHans Verkuil struct v4l2_format *f) 80385756a06SHans Verkuil { 80485756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file); 80585756a06SHans Verkuil struct v4l2_pix_format *pix = &f->fmt.pix; 80685756a06SHans Verkuil 80785756a06SHans Verkuil if (vb2_is_busy(&s->q)) 80885756a06SHans Verkuil return -EBUSY; 80985756a06SHans Verkuil 81085756a06SHans Verkuil if (cobalt_try_fmt_vid_cap(file, priv_fh, f)) 81185756a06SHans Verkuil return -EINVAL; 81285756a06SHans Verkuil 81385756a06SHans Verkuil s->width = pix->width; 81485756a06SHans Verkuil s->height = pix->height; 81585756a06SHans Verkuil s->stride = pix->bytesperline; 81685756a06SHans Verkuil switch (pix->pixelformat) { 81785756a06SHans Verkuil case V4L2_PIX_FMT_YUYV: 81885756a06SHans Verkuil s->bpp = COBALT_BYTES_PER_PIXEL_YUYV; 81985756a06SHans Verkuil break; 82085756a06SHans Verkuil case V4L2_PIX_FMT_RGB24: 82185756a06SHans Verkuil s->bpp = COBALT_BYTES_PER_PIXEL_RGB24; 82285756a06SHans Verkuil break; 82385756a06SHans Verkuil case V4L2_PIX_FMT_BGR32: 82485756a06SHans Verkuil s->bpp = COBALT_BYTES_PER_PIXEL_RGB32; 82585756a06SHans Verkuil break; 82685756a06SHans Verkuil default: 82785756a06SHans Verkuil return -EINVAL; 82885756a06SHans Verkuil } 82985756a06SHans Verkuil s->pixfmt = pix->pixelformat; 83085756a06SHans Verkuil cobalt_enable_input(s); 83185756a06SHans Verkuil 83285756a06SHans Verkuil return 0; 83385756a06SHans Verkuil } 83485756a06SHans Verkuil 83585756a06SHans Verkuil static int cobalt_try_fmt_vid_out(struct file *file, void *priv_fh, 83685756a06SHans Verkuil struct v4l2_format *f) 83785756a06SHans Verkuil { 83885756a06SHans Verkuil struct v4l2_pix_format *pix = &f->fmt.pix; 83985756a06SHans Verkuil 84085756a06SHans Verkuil /* Check for min (QCIF) and max (Full HD) size */ 84185756a06SHans Verkuil if ((pix->width < 176) || (pix->height < 144)) { 84285756a06SHans Verkuil pix->width = 176; 84385756a06SHans Verkuil pix->height = 144; 84485756a06SHans Verkuil } 84585756a06SHans Verkuil 84685756a06SHans Verkuil if ((pix->width > 1920) || (pix->height > 1080)) { 84785756a06SHans Verkuil pix->width = 1920; 84885756a06SHans Verkuil pix->height = 1080; 84985756a06SHans Verkuil } 85085756a06SHans Verkuil 85185756a06SHans Verkuil /* Make width multiple of 4 */ 85285756a06SHans Verkuil pix->width &= ~0x3; 85385756a06SHans Verkuil 85485756a06SHans Verkuil /* Make height multiple of 2 */ 85585756a06SHans Verkuil pix->height &= ~0x1; 85685756a06SHans Verkuil 85785756a06SHans Verkuil switch (pix->pixelformat) { 85885756a06SHans Verkuil case V4L2_PIX_FMT_YUYV: 85985756a06SHans Verkuil default: 86085756a06SHans Verkuil pix->bytesperline = max(pix->bytesperline & ~0x3, 86185756a06SHans Verkuil pix->width * COBALT_BYTES_PER_PIXEL_YUYV); 86285756a06SHans Verkuil pix->pixelformat = V4L2_PIX_FMT_YUYV; 86385756a06SHans Verkuil break; 86485756a06SHans Verkuil case V4L2_PIX_FMT_BGR32: 86585756a06SHans Verkuil pix->bytesperline = max(pix->bytesperline & ~0x3, 86685756a06SHans Verkuil pix->width * COBALT_BYTES_PER_PIXEL_RGB32); 86785756a06SHans Verkuil break; 86885756a06SHans Verkuil } 86985756a06SHans Verkuil 87085756a06SHans Verkuil pix->sizeimage = pix->bytesperline * pix->height; 87185756a06SHans Verkuil pix->field = V4L2_FIELD_NONE; 87285756a06SHans Verkuil 87385756a06SHans Verkuil return 0; 87485756a06SHans Verkuil } 87585756a06SHans Verkuil 87685756a06SHans Verkuil static int cobalt_g_fmt_vid_out(struct file *file, void *priv_fh, 87785756a06SHans Verkuil struct v4l2_format *f) 87885756a06SHans Verkuil { 87985756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file); 88085756a06SHans Verkuil struct v4l2_pix_format *pix = &f->fmt.pix; 88185756a06SHans Verkuil 88285756a06SHans Verkuil pix->width = s->width; 88385756a06SHans Verkuil pix->height = s->height; 88485756a06SHans Verkuil pix->bytesperline = s->stride; 88585756a06SHans Verkuil pix->field = V4L2_FIELD_NONE; 88685756a06SHans Verkuil pix->pixelformat = s->pixfmt; 88785756a06SHans Verkuil pix->colorspace = s->colorspace; 888136a5e9eSHans Verkuil pix->xfer_func = s->xfer_func; 88985756a06SHans Verkuil pix->ycbcr_enc = s->ycbcr_enc; 89085756a06SHans Verkuil pix->quantization = s->quantization; 89185756a06SHans Verkuil pix->sizeimage = pix->bytesperline * pix->height; 89285756a06SHans Verkuil 89385756a06SHans Verkuil return 0; 89485756a06SHans Verkuil } 89585756a06SHans Verkuil 89685756a06SHans Verkuil static int cobalt_enum_fmt_vid_out(struct file *file, void *priv_fh, 89785756a06SHans Verkuil struct v4l2_fmtdesc *f) 89885756a06SHans Verkuil { 89985756a06SHans Verkuil switch (f->index) { 90085756a06SHans Verkuil case 0: 901c0decac1SMauro Carvalho Chehab strscpy(f->description, "YUV 4:2:2", sizeof(f->description)); 90285756a06SHans Verkuil f->pixelformat = V4L2_PIX_FMT_YUYV; 90385756a06SHans Verkuil break; 90485756a06SHans Verkuil case 1: 905c0decac1SMauro Carvalho Chehab strscpy(f->description, "RGB32", sizeof(f->description)); 90685756a06SHans Verkuil f->pixelformat = V4L2_PIX_FMT_BGR32; 90785756a06SHans Verkuil break; 90885756a06SHans Verkuil default: 90985756a06SHans Verkuil return -EINVAL; 91085756a06SHans Verkuil } 91185756a06SHans Verkuil 91285756a06SHans Verkuil return 0; 91385756a06SHans Verkuil } 91485756a06SHans Verkuil 91585756a06SHans Verkuil static int cobalt_s_fmt_vid_out(struct file *file, void *priv_fh, 91685756a06SHans Verkuil struct v4l2_format *f) 91785756a06SHans Verkuil { 91885756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file); 91985756a06SHans Verkuil struct v4l2_pix_format *pix = &f->fmt.pix; 92085756a06SHans Verkuil struct v4l2_subdev_format sd_fmt = { 0 }; 921e1dedcffSHans Verkuil u32 code; 92285756a06SHans Verkuil 92385756a06SHans Verkuil if (cobalt_try_fmt_vid_out(file, priv_fh, f)) 92485756a06SHans Verkuil return -EINVAL; 92585756a06SHans Verkuil 92685756a06SHans Verkuil if (vb2_is_busy(&s->q) && (pix->pixelformat != s->pixfmt || 92785756a06SHans Verkuil pix->width != s->width || pix->height != s->height || 92885756a06SHans Verkuil pix->bytesperline != s->stride)) 92985756a06SHans Verkuil return -EBUSY; 93085756a06SHans Verkuil 93185756a06SHans Verkuil switch (pix->pixelformat) { 93285756a06SHans Verkuil case V4L2_PIX_FMT_YUYV: 93385756a06SHans Verkuil s->bpp = COBALT_BYTES_PER_PIXEL_YUYV; 934e1dedcffSHans Verkuil code = MEDIA_BUS_FMT_UYVY8_1X16; 93585756a06SHans Verkuil break; 93685756a06SHans Verkuil case V4L2_PIX_FMT_BGR32: 93785756a06SHans Verkuil s->bpp = COBALT_BYTES_PER_PIXEL_RGB32; 938e1dedcffSHans Verkuil code = MEDIA_BUS_FMT_RGB888_1X24; 93985756a06SHans Verkuil break; 94085756a06SHans Verkuil default: 94185756a06SHans Verkuil return -EINVAL; 94285756a06SHans Verkuil } 94385756a06SHans Verkuil s->width = pix->width; 94485756a06SHans Verkuil s->height = pix->height; 94585756a06SHans Verkuil s->stride = pix->bytesperline; 94685756a06SHans Verkuil s->pixfmt = pix->pixelformat; 94785756a06SHans Verkuil s->colorspace = pix->colorspace; 948136a5e9eSHans Verkuil s->xfer_func = pix->xfer_func; 94985756a06SHans Verkuil s->ycbcr_enc = pix->ycbcr_enc; 95085756a06SHans Verkuil s->quantization = pix->quantization; 95185756a06SHans Verkuil sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE; 952e1dedcffSHans Verkuil v4l2_fill_mbus_format(&sd_fmt.format, pix, code); 95385756a06SHans Verkuil v4l2_subdev_call(s->sd, pad, set_fmt, NULL, &sd_fmt); 95485756a06SHans Verkuil return 0; 95585756a06SHans Verkuil } 95685756a06SHans Verkuil 95785756a06SHans Verkuil static int cobalt_enum_input(struct file *file, void *priv_fh, 95885756a06SHans Verkuil struct v4l2_input *inp) 95985756a06SHans Verkuil { 96085756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file); 96185756a06SHans Verkuil 96285756a06SHans Verkuil if (inp->index > 1) 96385756a06SHans Verkuil return -EINVAL; 96485756a06SHans Verkuil if (inp->index == 0) 96585756a06SHans Verkuil snprintf(inp->name, sizeof(inp->name), 96685756a06SHans Verkuil "HDMI-%d", s->video_channel); 96785756a06SHans Verkuil else 96885756a06SHans Verkuil snprintf(inp->name, sizeof(inp->name), 96985756a06SHans Verkuil "Generator-%d", s->video_channel); 97085756a06SHans Verkuil inp->type = V4L2_INPUT_TYPE_CAMERA; 97185756a06SHans Verkuil inp->capabilities = V4L2_IN_CAP_DV_TIMINGS; 97285756a06SHans Verkuil if (inp->index == 1) 97385756a06SHans Verkuil return 0; 97485756a06SHans Verkuil return v4l2_subdev_call(s->sd, 97585756a06SHans Verkuil video, g_input_status, &inp->status); 97685756a06SHans Verkuil } 97785756a06SHans Verkuil 97885756a06SHans Verkuil static int cobalt_g_input(struct file *file, void *priv_fh, unsigned int *i) 97985756a06SHans Verkuil { 98085756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file); 98185756a06SHans Verkuil 98285756a06SHans Verkuil *i = s->input; 98385756a06SHans Verkuil return 0; 98485756a06SHans Verkuil } 98585756a06SHans Verkuil 98685756a06SHans Verkuil static int cobalt_s_input(struct file *file, void *priv_fh, unsigned int i) 98785756a06SHans Verkuil { 98885756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file); 98985756a06SHans Verkuil 99085756a06SHans Verkuil if (i >= 2) 99185756a06SHans Verkuil return -EINVAL; 99285756a06SHans Verkuil if (vb2_is_busy(&s->q)) 99385756a06SHans Verkuil return -EBUSY; 99485756a06SHans Verkuil s->input = i; 99585756a06SHans Verkuil 99685756a06SHans Verkuil cobalt_enable_input(s); 99785756a06SHans Verkuil 99885756a06SHans Verkuil if (s->input == 1) /* Test Pattern Generator */ 99985756a06SHans Verkuil return 0; 100085756a06SHans Verkuil 100185756a06SHans Verkuil return v4l2_subdev_call(s->sd, video, s_routing, 100285756a06SHans Verkuil ADV76XX_PAD_HDMI_PORT_A, 0, 0); 100385756a06SHans Verkuil } 100485756a06SHans Verkuil 100585756a06SHans Verkuil static int cobalt_enum_output(struct file *file, void *priv_fh, 100685756a06SHans Verkuil struct v4l2_output *out) 100785756a06SHans Verkuil { 100885756a06SHans Verkuil if (out->index) 100985756a06SHans Verkuil return -EINVAL; 101085756a06SHans Verkuil snprintf(out->name, sizeof(out->name), "HDMI-%d", out->index); 101185756a06SHans Verkuil out->type = V4L2_OUTPUT_TYPE_ANALOG; 101285756a06SHans Verkuil out->capabilities = V4L2_OUT_CAP_DV_TIMINGS; 101385756a06SHans Verkuil return 0; 101485756a06SHans Verkuil } 101585756a06SHans Verkuil 101685756a06SHans Verkuil static int cobalt_g_output(struct file *file, void *priv_fh, unsigned int *i) 101785756a06SHans Verkuil { 101885756a06SHans Verkuil *i = 0; 101985756a06SHans Verkuil return 0; 102085756a06SHans Verkuil } 102185756a06SHans Verkuil 102285756a06SHans Verkuil static int cobalt_s_output(struct file *file, void *priv_fh, unsigned int i) 102385756a06SHans Verkuil { 102485756a06SHans Verkuil return i ? -EINVAL : 0; 102585756a06SHans Verkuil } 102685756a06SHans Verkuil 102785756a06SHans Verkuil static int cobalt_g_edid(struct file *file, void *fh, struct v4l2_edid *edid) 102885756a06SHans Verkuil { 102985756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file); 103085756a06SHans Verkuil u32 pad = edid->pad; 103185756a06SHans Verkuil int ret; 103285756a06SHans Verkuil 103385756a06SHans Verkuil if (edid->pad >= (s->is_output ? 1 : 2)) 103485756a06SHans Verkuil return -EINVAL; 103585756a06SHans Verkuil edid->pad = 0; 103685756a06SHans Verkuil ret = v4l2_subdev_call(s->sd, pad, get_edid, edid); 103785756a06SHans Verkuil edid->pad = pad; 103885756a06SHans Verkuil return ret; 103985756a06SHans Verkuil } 104085756a06SHans Verkuil 104185756a06SHans Verkuil static int cobalt_s_edid(struct file *file, void *fh, struct v4l2_edid *edid) 104285756a06SHans Verkuil { 104385756a06SHans Verkuil struct cobalt_stream *s = video_drvdata(file); 104485756a06SHans Verkuil u32 pad = edid->pad; 104585756a06SHans Verkuil int ret; 104685756a06SHans Verkuil 104785756a06SHans Verkuil if (edid->pad >= 2) 104885756a06SHans Verkuil return -EINVAL; 104985756a06SHans Verkuil edid->pad = 0; 105085756a06SHans Verkuil ret = v4l2_subdev_call(s->sd, pad, set_edid, edid); 105185756a06SHans Verkuil edid->pad = pad; 105285756a06SHans Verkuil return ret; 105385756a06SHans Verkuil } 105485756a06SHans Verkuil 105585756a06SHans Verkuil static int cobalt_subscribe_event(struct v4l2_fh *fh, 105685756a06SHans Verkuil const struct v4l2_event_subscription *sub) 105785756a06SHans Verkuil { 105885756a06SHans Verkuil switch (sub->type) { 105985756a06SHans Verkuil case V4L2_EVENT_SOURCE_CHANGE: 106085756a06SHans Verkuil return v4l2_event_subscribe(fh, sub, 4, NULL); 106185756a06SHans Verkuil } 106285756a06SHans Verkuil return v4l2_ctrl_subscribe_event(fh, sub); 106385756a06SHans Verkuil } 106485756a06SHans Verkuil 106585756a06SHans Verkuil static int cobalt_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a) 106685756a06SHans Verkuil { 10673ae17c66SJose Abreu struct cobalt_stream *s = video_drvdata(file); 10683ae17c66SJose Abreu struct v4l2_fract fps; 10693ae17c66SJose Abreu 107085756a06SHans Verkuil if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 107185756a06SHans Verkuil return -EINVAL; 10723ae17c66SJose Abreu 10733ae17c66SJose Abreu fps = v4l2_calc_timeperframe(&s->timings); 10743ae17c66SJose Abreu a->parm.capture.timeperframe.numerator = fps.numerator; 10753ae17c66SJose Abreu a->parm.capture.timeperframe.denominator = fps.denominator; 107685756a06SHans Verkuil a->parm.capture.readbuffers = 3; 107785756a06SHans Verkuil return 0; 107885756a06SHans Verkuil } 107985756a06SHans Verkuil 10805200ab6aSHans Verkuil static int cobalt_g_pixelaspect(struct file *file, void *fh, 10815200ab6aSHans Verkuil int type, struct v4l2_fract *f) 1082d736e348SHans Verkuil { 1083d736e348SHans Verkuil struct cobalt_stream *s = video_drvdata(file); 1084d736e348SHans Verkuil struct v4l2_dv_timings timings; 1085d736e348SHans Verkuil int err = 0; 1086d736e348SHans Verkuil 10875200ab6aSHans Verkuil if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1088d736e348SHans Verkuil return -EINVAL; 10895200ab6aSHans Verkuil 1090d736e348SHans Verkuil if (s->input == 1) 1091d736e348SHans Verkuil timings = cea1080p60; 1092d736e348SHans Verkuil else 1093d736e348SHans Verkuil err = v4l2_subdev_call(s->sd, video, g_dv_timings, &timings); 1094ee10dc36SHans Verkuil if (!err) 10955200ab6aSHans Verkuil *f = v4l2_dv_timings_aspect_ratio(&timings); 1096d736e348SHans Verkuil return err; 1097d736e348SHans Verkuil } 1098d736e348SHans Verkuil 1099ee10dc36SHans Verkuil static int cobalt_g_selection(struct file *file, void *fh, 1100ee10dc36SHans Verkuil struct v4l2_selection *sel) 1101ee10dc36SHans Verkuil { 1102ee10dc36SHans Verkuil struct cobalt_stream *s = video_drvdata(file); 1103ee10dc36SHans Verkuil struct v4l2_dv_timings timings; 1104ee10dc36SHans Verkuil int err = 0; 1105ee10dc36SHans Verkuil 1106ee10dc36SHans Verkuil if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) 1107ee10dc36SHans Verkuil return -EINVAL; 1108ee10dc36SHans Verkuil 1109ee10dc36SHans Verkuil if (s->input == 1) 1110ee10dc36SHans Verkuil timings = cea1080p60; 1111ee10dc36SHans Verkuil else 1112ee10dc36SHans Verkuil err = v4l2_subdev_call(s->sd, video, g_dv_timings, &timings); 1113ee10dc36SHans Verkuil 1114ee10dc36SHans Verkuil if (err) 1115ee10dc36SHans Verkuil return err; 1116ee10dc36SHans Verkuil 1117ee10dc36SHans Verkuil switch (sel->target) { 1118ee10dc36SHans Verkuil case V4L2_SEL_TGT_CROP_BOUNDS: 1119ee10dc36SHans Verkuil case V4L2_SEL_TGT_CROP_DEFAULT: 1120ee10dc36SHans Verkuil sel->r.top = 0; 1121ee10dc36SHans Verkuil sel->r.left = 0; 1122ee10dc36SHans Verkuil sel->r.width = timings.bt.width; 1123ee10dc36SHans Verkuil sel->r.height = timings.bt.height; 1124ee10dc36SHans Verkuil break; 1125ee10dc36SHans Verkuil default: 1126ee10dc36SHans Verkuil return -EINVAL; 1127ee10dc36SHans Verkuil } 1128ee10dc36SHans Verkuil return 0; 1129ee10dc36SHans Verkuil } 1130ee10dc36SHans Verkuil 113185756a06SHans Verkuil static const struct v4l2_ioctl_ops cobalt_ioctl_ops = { 113285756a06SHans Verkuil .vidioc_querycap = cobalt_querycap, 113385756a06SHans Verkuil .vidioc_g_parm = cobalt_g_parm, 113485756a06SHans Verkuil .vidioc_log_status = cobalt_log_status, 113585756a06SHans Verkuil .vidioc_streamon = vb2_ioctl_streamon, 113685756a06SHans Verkuil .vidioc_streamoff = vb2_ioctl_streamoff, 11375200ab6aSHans Verkuil .vidioc_g_pixelaspect = cobalt_g_pixelaspect, 1138ee10dc36SHans Verkuil .vidioc_g_selection = cobalt_g_selection, 113985756a06SHans Verkuil .vidioc_enum_input = cobalt_enum_input, 114085756a06SHans Verkuil .vidioc_g_input = cobalt_g_input, 114185756a06SHans Verkuil .vidioc_s_input = cobalt_s_input, 114285756a06SHans Verkuil .vidioc_enum_fmt_vid_cap = cobalt_enum_fmt_vid_cap, 114385756a06SHans Verkuil .vidioc_g_fmt_vid_cap = cobalt_g_fmt_vid_cap, 114485756a06SHans Verkuil .vidioc_s_fmt_vid_cap = cobalt_s_fmt_vid_cap, 114585756a06SHans Verkuil .vidioc_try_fmt_vid_cap = cobalt_try_fmt_vid_cap, 114685756a06SHans Verkuil .vidioc_enum_output = cobalt_enum_output, 114785756a06SHans Verkuil .vidioc_g_output = cobalt_g_output, 114885756a06SHans Verkuil .vidioc_s_output = cobalt_s_output, 114985756a06SHans Verkuil .vidioc_enum_fmt_vid_out = cobalt_enum_fmt_vid_out, 115085756a06SHans Verkuil .vidioc_g_fmt_vid_out = cobalt_g_fmt_vid_out, 115185756a06SHans Verkuil .vidioc_s_fmt_vid_out = cobalt_s_fmt_vid_out, 115285756a06SHans Verkuil .vidioc_try_fmt_vid_out = cobalt_try_fmt_vid_out, 115385756a06SHans Verkuil .vidioc_s_dv_timings = cobalt_s_dv_timings, 115485756a06SHans Verkuil .vidioc_g_dv_timings = cobalt_g_dv_timings, 115585756a06SHans Verkuil .vidioc_query_dv_timings = cobalt_query_dv_timings, 115685756a06SHans Verkuil .vidioc_enum_dv_timings = cobalt_enum_dv_timings, 115785756a06SHans Verkuil .vidioc_dv_timings_cap = cobalt_dv_timings_cap, 115885756a06SHans Verkuil .vidioc_g_edid = cobalt_g_edid, 115985756a06SHans Verkuil .vidioc_s_edid = cobalt_s_edid, 116085756a06SHans Verkuil .vidioc_subscribe_event = cobalt_subscribe_event, 116185756a06SHans Verkuil .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 116285756a06SHans Verkuil .vidioc_reqbufs = vb2_ioctl_reqbufs, 116385756a06SHans Verkuil .vidioc_create_bufs = vb2_ioctl_create_bufs, 116485756a06SHans Verkuil .vidioc_querybuf = vb2_ioctl_querybuf, 116585756a06SHans Verkuil .vidioc_qbuf = vb2_ioctl_qbuf, 116685756a06SHans Verkuil .vidioc_dqbuf = vb2_ioctl_dqbuf, 116785756a06SHans Verkuil .vidioc_expbuf = vb2_ioctl_expbuf, 116885756a06SHans Verkuil #ifdef CONFIG_VIDEO_ADV_DEBUG 116985756a06SHans Verkuil .vidioc_g_register = cobalt_g_register, 117085756a06SHans Verkuil .vidioc_s_register = cobalt_s_register, 117185756a06SHans Verkuil #endif 117285756a06SHans Verkuil }; 117385756a06SHans Verkuil 117485756a06SHans Verkuil static const struct v4l2_ioctl_ops cobalt_ioctl_empty_ops = { 117585756a06SHans Verkuil #ifdef CONFIG_VIDEO_ADV_DEBUG 117685756a06SHans Verkuil .vidioc_g_register = cobalt_g_register, 117785756a06SHans Verkuil .vidioc_s_register = cobalt_s_register, 117885756a06SHans Verkuil #endif 117985756a06SHans Verkuil }; 118085756a06SHans Verkuil 118185756a06SHans Verkuil /* Register device nodes */ 118285756a06SHans Verkuil 118385756a06SHans Verkuil static const struct v4l2_file_operations cobalt_fops = { 118485756a06SHans Verkuil .owner = THIS_MODULE, 118585756a06SHans Verkuil .open = v4l2_fh_open, 118685756a06SHans Verkuil .unlocked_ioctl = video_ioctl2, 118785756a06SHans Verkuil .release = vb2_fop_release, 118885756a06SHans Verkuil .poll = vb2_fop_poll, 118985756a06SHans Verkuil .mmap = vb2_fop_mmap, 119085756a06SHans Verkuil .read = vb2_fop_read, 119185756a06SHans Verkuil }; 119285756a06SHans Verkuil 119385756a06SHans Verkuil static const struct v4l2_file_operations cobalt_out_fops = { 119485756a06SHans Verkuil .owner = THIS_MODULE, 119585756a06SHans Verkuil .open = v4l2_fh_open, 119685756a06SHans Verkuil .unlocked_ioctl = video_ioctl2, 119785756a06SHans Verkuil .release = vb2_fop_release, 119885756a06SHans Verkuil .poll = vb2_fop_poll, 119985756a06SHans Verkuil .mmap = vb2_fop_mmap, 120085756a06SHans Verkuil .write = vb2_fop_write, 120185756a06SHans Verkuil }; 120285756a06SHans Verkuil 120385756a06SHans Verkuil static const struct v4l2_file_operations cobalt_empty_fops = { 120485756a06SHans Verkuil .owner = THIS_MODULE, 120585756a06SHans Verkuil .open = v4l2_fh_open, 120685756a06SHans Verkuil .unlocked_ioctl = video_ioctl2, 120785756a06SHans Verkuil .release = v4l2_fh_release, 120885756a06SHans Verkuil }; 120985756a06SHans Verkuil 121085756a06SHans Verkuil static int cobalt_node_register(struct cobalt *cobalt, int node) 121185756a06SHans Verkuil { 121285756a06SHans Verkuil static const struct v4l2_dv_timings dv1080p60 = 121385756a06SHans Verkuil V4L2_DV_BT_CEA_1920X1080P60; 121485756a06SHans Verkuil struct cobalt_stream *s = cobalt->streams + node; 121585756a06SHans Verkuil struct video_device *vdev = &s->vdev; 121685756a06SHans Verkuil struct vb2_queue *q = &s->q; 121785756a06SHans Verkuil int ret; 121885756a06SHans Verkuil 121985756a06SHans Verkuil mutex_init(&s->lock); 122085756a06SHans Verkuil spin_lock_init(&s->irqlock); 122185756a06SHans Verkuil 122285756a06SHans Verkuil snprintf(vdev->name, sizeof(vdev->name), 122385756a06SHans Verkuil "%s-%d", cobalt->v4l2_dev.name, node); 122485756a06SHans Verkuil s->width = 1920; 122585756a06SHans Verkuil /* Audio frames are just 4 lines of 1920 bytes */ 122685756a06SHans Verkuil s->height = s->is_audio ? 4 : 1080; 122785756a06SHans Verkuil 122885756a06SHans Verkuil if (s->is_audio) { 122985756a06SHans Verkuil s->bpp = 1; 123085756a06SHans Verkuil s->pixfmt = V4L2_PIX_FMT_GREY; 123185756a06SHans Verkuil } else if (s->is_output) { 123285756a06SHans Verkuil s->bpp = COBALT_BYTES_PER_PIXEL_RGB32; 123385756a06SHans Verkuil s->pixfmt = V4L2_PIX_FMT_BGR32; 123485756a06SHans Verkuil } else { 123585756a06SHans Verkuil s->bpp = COBALT_BYTES_PER_PIXEL_YUYV; 123685756a06SHans Verkuil s->pixfmt = V4L2_PIX_FMT_YUYV; 123785756a06SHans Verkuil } 123885756a06SHans Verkuil s->colorspace = V4L2_COLORSPACE_SRGB; 123985756a06SHans Verkuil s->stride = s->width * s->bpp; 124085756a06SHans Verkuil 124185756a06SHans Verkuil if (!s->is_audio) { 124285756a06SHans Verkuil if (s->is_dummy) 124385756a06SHans Verkuil cobalt_warn("Setting up dummy video node %d\n", node); 124485756a06SHans Verkuil vdev->v4l2_dev = &cobalt->v4l2_dev; 124585756a06SHans Verkuil if (s->is_dummy) 124685756a06SHans Verkuil vdev->fops = &cobalt_empty_fops; 124785756a06SHans Verkuil else 124885756a06SHans Verkuil vdev->fops = s->is_output ? &cobalt_out_fops : 124985756a06SHans Verkuil &cobalt_fops; 125085756a06SHans Verkuil vdev->release = video_device_release_empty; 125185756a06SHans Verkuil vdev->vfl_dir = s->is_output ? VFL_DIR_TX : VFL_DIR_RX; 125285756a06SHans Verkuil vdev->lock = &s->lock; 125385756a06SHans Verkuil if (s->sd) 125485756a06SHans Verkuil vdev->ctrl_handler = s->sd->ctrl_handler; 125585756a06SHans Verkuil s->timings = dv1080p60; 125685756a06SHans Verkuil v4l2_subdev_call(s->sd, video, s_dv_timings, &s->timings); 125785756a06SHans Verkuil if (!s->is_output && s->sd) 125885756a06SHans Verkuil cobalt_enable_input(s); 125985756a06SHans Verkuil vdev->ioctl_ops = s->is_dummy ? &cobalt_ioctl_empty_ops : 126085756a06SHans Verkuil &cobalt_ioctl_ops; 126185756a06SHans Verkuil } 126285756a06SHans Verkuil 126385756a06SHans Verkuil INIT_LIST_HEAD(&s->bufs); 126485756a06SHans Verkuil q->type = s->is_output ? V4L2_BUF_TYPE_VIDEO_OUTPUT : 126585756a06SHans Verkuil V4L2_BUF_TYPE_VIDEO_CAPTURE; 126685756a06SHans Verkuil q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; 126785756a06SHans Verkuil q->io_modes |= s->is_output ? VB2_WRITE : VB2_READ; 126885756a06SHans Verkuil q->drv_priv = s; 126985756a06SHans Verkuil q->buf_struct_size = sizeof(struct cobalt_buffer); 127085756a06SHans Verkuil q->ops = &cobalt_qops; 127185756a06SHans Verkuil q->mem_ops = &vb2_dma_sg_memops; 127285756a06SHans Verkuil q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 127385756a06SHans Verkuil q->min_buffers_needed = 2; 127485756a06SHans Verkuil q->lock = &s->lock; 12752bc46b3aSHans Verkuil q->dev = &cobalt->pci_dev->dev; 127685756a06SHans Verkuil vdev->queue = q; 127785756a06SHans Verkuil 127885756a06SHans Verkuil video_set_drvdata(vdev, s); 127985756a06SHans Verkuil ret = vb2_queue_init(q); 128085756a06SHans Verkuil if (!s->is_audio && ret == 0) 128185756a06SHans Verkuil ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1); 128285756a06SHans Verkuil else if (!s->is_dummy) 128385756a06SHans Verkuil ret = cobalt_alsa_init(s); 128485756a06SHans Verkuil 128585756a06SHans Verkuil if (ret < 0) { 128685756a06SHans Verkuil if (!s->is_audio) 128785756a06SHans Verkuil cobalt_err("couldn't register v4l2 device node %d\n", 128885756a06SHans Verkuil node); 128985756a06SHans Verkuil return ret; 129085756a06SHans Verkuil } 129185756a06SHans Verkuil cobalt_info("registered node %d\n", node); 129285756a06SHans Verkuil return 0; 129385756a06SHans Verkuil } 129485756a06SHans Verkuil 129585756a06SHans Verkuil /* Initialize v4l2 variables and register v4l2 devices */ 129685756a06SHans Verkuil int cobalt_nodes_register(struct cobalt *cobalt) 129785756a06SHans Verkuil { 129885756a06SHans Verkuil int node, ret; 129985756a06SHans Verkuil 130085756a06SHans Verkuil /* Setup V4L2 Devices */ 130185756a06SHans Verkuil for (node = 0; node < COBALT_NUM_STREAMS; node++) { 130285756a06SHans Verkuil ret = cobalt_node_register(cobalt, node); 130385756a06SHans Verkuil if (ret) 130485756a06SHans Verkuil return ret; 130585756a06SHans Verkuil } 130685756a06SHans Verkuil return 0; 130785756a06SHans Verkuil } 130885756a06SHans Verkuil 130985756a06SHans Verkuil /* Unregister v4l2 devices */ 131085756a06SHans Verkuil void cobalt_nodes_unregister(struct cobalt *cobalt) 131185756a06SHans Verkuil { 131285756a06SHans Verkuil int node; 131385756a06SHans Verkuil 131485756a06SHans Verkuil /* Teardown all streams */ 131585756a06SHans Verkuil for (node = 0; node < COBALT_NUM_STREAMS; node++) { 131685756a06SHans Verkuil struct cobalt_stream *s = cobalt->streams + node; 131785756a06SHans Verkuil struct video_device *vdev = &s->vdev; 131885756a06SHans Verkuil 131985756a06SHans Verkuil if (!s->is_audio) 132085756a06SHans Verkuil video_unregister_device(vdev); 132185756a06SHans Verkuil else if (!s->is_dummy) 132285756a06SHans Verkuil cobalt_alsa_exit(s); 132385756a06SHans Verkuil } 132485756a06SHans Verkuil } 1325