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