blob: 72b081f9cde3db85776f061957002053cb2db67f [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;
Hans Verkuil136a5e92015-05-31 06:48:00 -0300173 sd_fmt.format.xfer_func = s->xfer_func;
Hans Verkuil85756a02015-05-12 08:52:21 -0300174 sd_fmt.format.ycbcr_enc = s->ycbcr_enc;
175 sd_fmt.format.quantization = s->quantization;
176 sd_fmt.format.width = bt->width;
177 sd_fmt.format.height = bt->height;
178
179 /* Set up FDMA packer */
180 switch (s->pixfmt) {
181 case V4L2_PIX_FMT_YUYV:
182 sd_fmt.format.code = MEDIA_BUS_FMT_UYVY8_1X16;
183 break;
184 case V4L2_PIX_FMT_BGR32:
185 sd_fmt.format.code = MEDIA_BUS_FMT_RGB888_1X24;
186 break;
187 }
188 v4l2_subdev_call(s->sd, pad, set_fmt, NULL, &sd_fmt);
189
Hans Verkuil4da70762015-05-22 06:30:59 -0300190 iowrite32(0, &vo->control);
Hans Verkuil85756a02015-05-12 08:52:21 -0300191 /* 1080p60 */
Hans Verkuil4da70762015-05-22 06:30:59 -0300192 iowrite32(bt->hsync, &vo->sync_generator_h_sync_length);
193 iowrite32(bt->hbackporch, &vo->sync_generator_h_backporch_length);
194 iowrite32(bt->width, &vo->sync_generator_h_active_length);
195 iowrite32(bt->hfrontporch, &vo->sync_generator_h_frontporch_length);
196 iowrite32(bt->vsync, &vo->sync_generator_v_sync_length);
197 iowrite32(bt->vbackporch, &vo->sync_generator_v_backporch_length);
198 iowrite32(bt->height, &vo->sync_generator_v_active_length);
199 iowrite32(bt->vfrontporch, &vo->sync_generator_v_frontporch_length);
200 iowrite32(0x9900c1, &vo->error_color);
Hans Verkuil85756a02015-05-12 08:52:21 -0300201
Hans Verkuil4da70762015-05-22 06:30:59 -0300202 iowrite32(M00514_CONTROL_BITMAP_SYNC_GENERATOR_LOAD_PARAM_MSK | fmt,
203 &vo->control);
204 iowrite32(M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK | fmt, &vo->control);
205 iowrite32(M00514_CONTROL_BITMAP_SYNC_GENERATOR_ENABLE_MSK |
206 M00514_CONTROL_BITMAP_FLOW_CTRL_OUTPUT_ENABLE_MSK |
207 fmt, &vo->control);
Hans Verkuil85756a02015-05-12 08:52:21 -0300208}
209
210static void cobalt_enable_input(struct cobalt_stream *s)
211{
212 struct cobalt *cobalt = s->cobalt;
213 int ch = (int)s->video_channel;
Hans Verkuil4da70762015-05-22 06:30:59 -0300214 struct m00235_fdma_packer_regmap __iomem *packer;
Hans Verkuil85756a02015-05-12 08:52:21 -0300215 struct v4l2_subdev_format sd_fmt_yuyv = {
216 .pad = s->pad_source,
217 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
218 .format.code = MEDIA_BUS_FMT_YUYV8_1X16,
219 };
220 struct v4l2_subdev_format sd_fmt_rgb = {
221 .pad = s->pad_source,
222 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
223 .format.code = MEDIA_BUS_FMT_RGB888_1X24,
224 };
225
226 cobalt_dbg(1, "video_channel %d (%s, %s)\n",
227 s->video_channel,
228 s->input == 0 ? "hdmi" : "generator",
229 "YUYV");
230
231 packer = COBALT_CVI_PACKER(cobalt, ch);
232
233 /* Set up FDMA packer */
234 switch (s->pixfmt) {
235 case V4L2_PIX_FMT_YUYV:
Hans Verkuil4da70762015-05-22 06:30:59 -0300236 iowrite32(M00235_CONTROL_BITMAP_ENABLE_MSK |
237 (1 << M00235_CONTROL_BITMAP_PACK_FORMAT_OFST),
238 &packer->control);
Hans Verkuil85756a02015-05-12 08:52:21 -0300239 v4l2_subdev_call(s->sd, pad, set_fmt, NULL,
240 &sd_fmt_yuyv);
241 break;
242 case V4L2_PIX_FMT_RGB24:
Hans Verkuil4da70762015-05-22 06:30:59 -0300243 iowrite32(M00235_CONTROL_BITMAP_ENABLE_MSK |
244 (2 << M00235_CONTROL_BITMAP_PACK_FORMAT_OFST),
245 &packer->control);
Hans Verkuil85756a02015-05-12 08:52:21 -0300246 v4l2_subdev_call(s->sd, pad, set_fmt, NULL,
247 &sd_fmt_rgb);
248 break;
249 case V4L2_PIX_FMT_BGR32:
Hans Verkuil4da70762015-05-22 06:30:59 -0300250 iowrite32(M00235_CONTROL_BITMAP_ENABLE_MSK |
251 M00235_CONTROL_BITMAP_ENDIAN_FORMAT_MSK |
252 (3 << M00235_CONTROL_BITMAP_PACK_FORMAT_OFST),
253 &packer->control);
Hans Verkuil85756a02015-05-12 08:52:21 -0300254 v4l2_subdev_call(s->sd, pad, set_fmt, NULL,
255 &sd_fmt_rgb);
256 break;
257 }
258}
259
260static void cobalt_dma_start_streaming(struct cobalt_stream *s)
261{
262 struct cobalt *cobalt = s->cobalt;
263 int rx = s->video_channel;
Hans Verkuil4da70762015-05-22 06:30:59 -0300264 struct m00460_evcnt_regmap __iomem *evcnt =
Hans Verkuil85756a02015-05-12 08:52:21 -0300265 COBALT_CVI_EVCNT(cobalt, rx);
266 struct cobalt_buffer *cb;
267 unsigned long flags;
268
269 spin_lock_irqsave(&s->irqlock, flags);
270 if (!s->is_output) {
Hans Verkuil4da70762015-05-22 06:30:59 -0300271 iowrite32(M00460_CONTROL_BITMAP_CLEAR_MSK, &evcnt->control);
272 iowrite32(M00460_CONTROL_BITMAP_ENABLE_MSK, &evcnt->control);
Hans Verkuil85756a02015-05-12 08:52:21 -0300273 } else {
Hans Verkuil4da70762015-05-22 06:30:59 -0300274 struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
Hans Verkuil85756a02015-05-12 08:52:21 -0300275 COBALT_TX_BASE(cobalt);
Hans Verkuil4da70762015-05-22 06:30:59 -0300276 u32 ctrl = ioread32(&vo->control);
Hans Verkuil85756a02015-05-12 08:52:21 -0300277
278 ctrl &= ~(M00514_CONTROL_BITMAP_EVCNT_ENABLE_MSK |
279 M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK);
Hans Verkuil4da70762015-05-22 06:30:59 -0300280 iowrite32(ctrl | M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK,
281 &vo->control);
282 iowrite32(ctrl | M00514_CONTROL_BITMAP_EVCNT_ENABLE_MSK,
283 &vo->control);
Hans Verkuil85756a02015-05-12 08:52:21 -0300284 }
285 cb = list_first_entry(&s->bufs, struct cobalt_buffer, list);
286 omni_sg_dma_start(s, &s->dma_desc_info[cb->vb.v4l2_buf.index]);
287 spin_unlock_irqrestore(&s->irqlock, flags);
288}
289
290static int cobalt_start_streaming(struct vb2_queue *q, unsigned int count)
291{
292 struct cobalt_stream *s = q->drv_priv;
293 struct cobalt *cobalt = s->cobalt;
Hans Verkuil4da70762015-05-22 06:30:59 -0300294 struct m00233_video_measure_regmap __iomem *vmr;
295 struct m00473_freewheel_regmap __iomem *fw;
296 struct m00479_clk_loss_detector_regmap __iomem *clkloss;
Hans Verkuil85756a02015-05-12 08:52:21 -0300297 int rx = s->video_channel;
Hans Verkuil4da70762015-05-22 06:30:59 -0300298 struct m00389_cvi_regmap __iomem *cvi = COBALT_CVI(cobalt, rx);
299 struct m00460_evcnt_regmap __iomem *evcnt = COBALT_CVI_EVCNT(cobalt, rx);
Hans Verkuil85756a02015-05-12 08:52:21 -0300300 struct v4l2_bt_timings *bt = &s->timings.bt;
301 u64 tot_size;
Hans Verkuil4da70762015-05-22 06:30:59 -0300302 u32 clk_freq;
Hans Verkuil85756a02015-05-12 08:52:21 -0300303
304 if (s->is_audio)
305 goto done;
306 if (s->is_output) {
307 s->unstable_frame = false;
308 cobalt_enable_output(s);
309 goto done;
310 }
311
312 cobalt_enable_input(s);
313
314 fw = COBALT_CVI_FREEWHEEL(cobalt, rx);
315 vmr = COBALT_CVI_VMR(cobalt, rx);
316 clkloss = COBALT_CVI_CLK_LOSS(cobalt, rx);
317
Hans Verkuil4da70762015-05-22 06:30:59 -0300318 iowrite32(M00460_CONTROL_BITMAP_CLEAR_MSK, &evcnt->control);
319 iowrite32(M00460_CONTROL_BITMAP_ENABLE_MSK, &evcnt->control);
320 iowrite32(bt->width, &cvi->frame_width);
321 iowrite32(bt->height, &cvi->frame_height);
Hans Verkuil85756a02015-05-12 08:52:21 -0300322 tot_size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
Hans Verkuil4da70762015-05-22 06:30:59 -0300323 iowrite32(div_u64((u64)V4L2_DV_BT_FRAME_WIDTH(bt) * COBALT_CLK * 4,
324 bt->pixelclock), &vmr->hsync_timeout_val);
325 iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK, &vmr->control);
326 clk_freq = ioread32(&fw->clk_freq);
327 iowrite32(clk_freq / 1000000, &clkloss->ref_clk_cnt_val);
Hans Verkuil85756a02015-05-12 08:52:21 -0300328 /* The lower bound for the clock frequency is 0.5% lower as is
329 * allowed by the spec */
Hans Verkuil4da70762015-05-22 06:30:59 -0300330 iowrite32((((u64)bt->pixelclock * 995) / 1000) / 1000000,
331 &clkloss->test_clk_cnt_val);
Hans Verkuil85756a02015-05-12 08:52:21 -0300332 /* will be enabled after the first frame has been received */
Hans Verkuil4da70762015-05-22 06:30:59 -0300333 iowrite32(bt->width * bt->height, &fw->active_length);
334 iowrite32(div_u64((u64)clk_freq * tot_size, bt->pixelclock),
335 &fw->total_length);
336 iowrite32(M00233_IRQ_TRIGGERS_BITMAP_VACTIVE_AREA_MSK |
337 M00233_IRQ_TRIGGERS_BITMAP_HACTIVE_AREA_MSK,
338 &vmr->irq_triggers);
339 iowrite32(0, &cvi->control);
340 iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK, &vmr->control);
Hans Verkuil85756a02015-05-12 08:52:21 -0300341
Hans Verkuil4da70762015-05-22 06:30:59 -0300342 iowrite32(0xff, &fw->output_color);
343 iowrite32(M00479_CTRL_BITMAP_ENABLE_MSK, &clkloss->ctrl);
344 iowrite32(M00473_CTRL_BITMAP_ENABLE_MSK |
345 M00473_CTRL_BITMAP_FORCE_FREEWHEEL_MODE_MSK, &fw->ctrl);
Hans Verkuil85756a02015-05-12 08:52:21 -0300346 s->unstable_frame = true;
347 s->enable_freewheel = false;
348 s->enable_cvi = false;
349 s->skip_first_frames = 0;
350
351done:
352 s->sequence = 0;
353 cobalt_dma_start_streaming(s);
354 return 0;
355}
356
357static void cobalt_dma_stop_streaming(struct cobalt_stream *s)
358{
359 struct cobalt *cobalt = s->cobalt;
360 struct sg_dma_desc_info *desc;
361 struct cobalt_buffer *cb;
362 struct list_head *p;
363 unsigned long flags;
364 int timeout_msec = 100;
365 int rx = s->video_channel;
Hans Verkuil4da70762015-05-22 06:30:59 -0300366 struct m00460_evcnt_regmap __iomem *evcnt =
Hans Verkuil85756a02015-05-12 08:52:21 -0300367 COBALT_CVI_EVCNT(cobalt, rx);
368
369 if (!s->is_output) {
Hans Verkuil4da70762015-05-22 06:30:59 -0300370 iowrite32(0, &evcnt->control);
Hans Verkuil85756a02015-05-12 08:52:21 -0300371 } else if (!s->is_audio) {
Hans Verkuil4da70762015-05-22 06:30:59 -0300372 struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
Hans Verkuil85756a02015-05-12 08:52:21 -0300373 COBALT_TX_BASE(cobalt);
374
Hans Verkuil4da70762015-05-22 06:30:59 -0300375 iowrite32(M00514_CONTROL_BITMAP_EVCNT_CLEAR_MSK, &vo->control);
376 iowrite32(0, &vo->control);
Hans Verkuil85756a02015-05-12 08:52:21 -0300377 }
378
379 /* Try to stop the DMA engine gracefully */
380 spin_lock_irqsave(&s->irqlock, flags);
381 list_for_each(p, &s->bufs) {
382 cb = list_entry(p, struct cobalt_buffer, list);
383 desc = &s->dma_desc_info[cb->vb.v4l2_buf.index];
384 /* Stop DMA after this descriptor chain */
385 descriptor_list_end_of_chain(desc);
386 }
387 spin_unlock_irqrestore(&s->irqlock, flags);
388
389 /* Wait 100 milisecond for DMA to finish, abort on timeout. */
390 if (!wait_event_timeout(s->q.done_wq, is_dma_done(s),
391 msecs_to_jiffies(timeout_msec))) {
392 omni_sg_dma_abort_channel(s);
393 pr_warn("aborted\n");
394 }
395 cobalt_write_bar0(cobalt, DMA_INTERRUPT_STATUS_REG,
396 1 << s->dma_channel);
397}
398
399static void cobalt_stop_streaming(struct vb2_queue *q)
400{
401 struct cobalt_stream *s = q->drv_priv;
402 struct cobalt *cobalt = s->cobalt;
403 int rx = s->video_channel;
Hans Verkuil4da70762015-05-22 06:30:59 -0300404 struct m00233_video_measure_regmap __iomem *vmr;
405 struct m00473_freewheel_regmap __iomem *fw;
406 struct m00479_clk_loss_detector_regmap __iomem *clkloss;
Hans Verkuil85756a02015-05-12 08:52:21 -0300407 struct cobalt_buffer *cb;
408 struct list_head *p, *safe;
409 unsigned long flags;
410
411 cobalt_dma_stop_streaming(s);
412
413 /* Return all buffers to user space */
414 spin_lock_irqsave(&s->irqlock, flags);
415 list_for_each_safe(p, safe, &s->bufs) {
416 cb = list_entry(p, struct cobalt_buffer, list);
417 list_del(&cb->list);
418 vb2_buffer_done(&cb->vb, VB2_BUF_STATE_ERROR);
419 }
420 spin_unlock_irqrestore(&s->irqlock, flags);
421
422 if (s->is_audio || s->is_output)
423 return;
424
425 fw = COBALT_CVI_FREEWHEEL(cobalt, rx);
426 vmr = COBALT_CVI_VMR(cobalt, rx);
427 clkloss = COBALT_CVI_CLK_LOSS(cobalt, rx);
Hans Verkuil4da70762015-05-22 06:30:59 -0300428 iowrite32(0, &vmr->control);
429 iowrite32(M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK, &vmr->control);
430 iowrite32(0, &fw->ctrl);
431 iowrite32(0, &clkloss->ctrl);
Hans Verkuil85756a02015-05-12 08:52:21 -0300432}
433
434static const struct vb2_ops cobalt_qops = {
435 .queue_setup = cobalt_queue_setup,
436 .buf_init = cobalt_buf_init,
437 .buf_cleanup = cobalt_buf_cleanup,
438 .buf_prepare = cobalt_buf_prepare,
439 .buf_queue = cobalt_buf_queue,
440 .start_streaming = cobalt_start_streaming,
441 .stop_streaming = cobalt_stop_streaming,
442 .wait_prepare = vb2_ops_wait_prepare,
443 .wait_finish = vb2_ops_wait_finish,
444};
445
446/* V4L2 ioctls */
447
448#ifdef CONFIG_VIDEO_ADV_DEBUG
449static int cobalt_cobaltc(struct cobalt *cobalt, unsigned int cmd, void *arg)
450{
451 struct v4l2_dbg_register *regs = arg;
452 void __iomem *adrs = cobalt->bar1 + regs->reg;
453
454 cobalt_info("cobalt_cobaltc: adrs = %p\n", adrs);
455
456 if (!capable(CAP_SYS_ADMIN))
457 return -EPERM;
458
459 regs->size = 4;
460 if (cmd == VIDIOC_DBG_S_REGISTER)
461 iowrite32(regs->val, adrs);
462 else
463 regs->val = ioread32(adrs);
464 return 0;
465}
466
467static int cobalt_g_register(struct file *file, void *priv_fh,
468 struct v4l2_dbg_register *reg)
469{
470 struct cobalt_stream *s = video_drvdata(file);
471 struct cobalt *cobalt = s->cobalt;
472
473 return cobalt_cobaltc(cobalt, VIDIOC_DBG_G_REGISTER, reg);
474}
475
476static int cobalt_s_register(struct file *file, void *priv_fh,
477 const struct v4l2_dbg_register *reg)
478{
479 struct cobalt_stream *s = video_drvdata(file);
480 struct cobalt *cobalt = s->cobalt;
481
482 return cobalt_cobaltc(cobalt, VIDIOC_DBG_S_REGISTER,
483 (struct v4l2_dbg_register *)reg);
484}
485#endif
486
487static int cobalt_querycap(struct file *file, void *priv_fh,
488 struct v4l2_capability *vcap)
489{
490 struct cobalt_stream *s = video_drvdata(file);
491 struct cobalt *cobalt = s->cobalt;
492
493 strlcpy(vcap->driver, "cobalt", sizeof(vcap->driver));
494 strlcpy(vcap->card, "cobalt", sizeof(vcap->card));
495 snprintf(vcap->bus_info, sizeof(vcap->bus_info),
496 "PCIe:%s", pci_name(cobalt->pci_dev));
497 vcap->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
498 if (s->is_output)
499 vcap->device_caps |= V4L2_CAP_VIDEO_OUTPUT;
500 else
501 vcap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
502 vcap->capabilities = vcap->device_caps | V4L2_CAP_DEVICE_CAPS |
503 V4L2_CAP_VIDEO_CAPTURE;
504 if (cobalt->have_hsma_tx)
505 vcap->capabilities |= V4L2_CAP_VIDEO_OUTPUT;
506 return 0;
507}
508
509static void cobalt_video_input_status_show(struct cobalt_stream *s)
510{
Hans Verkuil4da70762015-05-22 06:30:59 -0300511 struct m00389_cvi_regmap __iomem *cvi;
512 struct m00233_video_measure_regmap __iomem *vmr;
513 struct m00473_freewheel_regmap __iomem *fw;
514 struct m00479_clk_loss_detector_regmap __iomem *clkloss;
515 struct m00235_fdma_packer_regmap __iomem *packer;
Hans Verkuil85756a02015-05-12 08:52:21 -0300516 int rx = s->video_channel;
517 struct cobalt *cobalt = s->cobalt;
Hans Verkuil4da70762015-05-22 06:30:59 -0300518 u32 cvi_ctrl, cvi_stat;
519 u32 vmr_ctrl, vmr_stat;
Hans Verkuil85756a02015-05-12 08:52:21 -0300520
521 cvi = COBALT_CVI(cobalt, rx);
522 vmr = COBALT_CVI_VMR(cobalt, rx);
523 fw = COBALT_CVI_FREEWHEEL(cobalt, rx);
524 clkloss = COBALT_CVI_CLK_LOSS(cobalt, rx);
525 packer = COBALT_CVI_PACKER(cobalt, rx);
Hans Verkuil4da70762015-05-22 06:30:59 -0300526 cvi_ctrl = ioread32(&cvi->control);
527 cvi_stat = ioread32(&cvi->status);
528 vmr_ctrl = ioread32(&vmr->control);
529 vmr_stat = ioread32(&vmr->control);
Hans Verkuil85756a02015-05-12 08:52:21 -0300530 cobalt_info("rx%d: cvi resolution: %dx%d\n", rx,
Hans Verkuil4da70762015-05-22 06:30:59 -0300531 ioread32(&cvi->frame_width), ioread32(&cvi->frame_height));
Hans Verkuil85756a02015-05-12 08:52:21 -0300532 cobalt_info("rx%d: cvi control: %s%s%s\n", rx,
Hans Verkuil4da70762015-05-22 06:30:59 -0300533 (cvi_ctrl & M00389_CONTROL_BITMAP_ENABLE_MSK) ?
Hans Verkuil85756a02015-05-12 08:52:21 -0300534 "enable " : "disable ",
Hans Verkuil4da70762015-05-22 06:30:59 -0300535 (cvi_ctrl & M00389_CONTROL_BITMAP_HSYNC_POLARITY_LOW_MSK) ?
Hans Verkuil85756a02015-05-12 08:52:21 -0300536 "HSync- " : "HSync+ ",
Hans Verkuil4da70762015-05-22 06:30:59 -0300537 (cvi_ctrl & M00389_CONTROL_BITMAP_VSYNC_POLARITY_LOW_MSK) ?
Hans Verkuil85756a02015-05-12 08:52:21 -0300538 "VSync- " : "VSync+ ");
539 cobalt_info("rx%d: cvi status: %s%s\n", rx,
Hans Verkuil4da70762015-05-22 06:30:59 -0300540 (cvi_stat & M00389_STATUS_BITMAP_LOCK_MSK) ?
Hans Verkuil85756a02015-05-12 08:52:21 -0300541 "lock " : "no-lock ",
Hans Verkuil4da70762015-05-22 06:30:59 -0300542 (cvi_stat & M00389_STATUS_BITMAP_ERROR_MSK) ?
Hans Verkuil85756a02015-05-12 08:52:21 -0300543 "error " : "no-error ");
544
545 cobalt_info("rx%d: Measurements: %s%s%s%s%s%s%s\n", rx,
Hans Verkuil4da70762015-05-22 06:30:59 -0300546 (vmr_ctrl & M00233_CONTROL_BITMAP_HSYNC_POLARITY_LOW_MSK) ?
Hans Verkuil85756a02015-05-12 08:52:21 -0300547 "HSync- " : "HSync+ ",
Hans Verkuil4da70762015-05-22 06:30:59 -0300548 (vmr_ctrl & M00233_CONTROL_BITMAP_VSYNC_POLARITY_LOW_MSK) ?
Hans Verkuil85756a02015-05-12 08:52:21 -0300549 "VSync- " : "VSync+ ",
Hans Verkuil4da70762015-05-22 06:30:59 -0300550 (vmr_ctrl & M00233_CONTROL_BITMAP_ENABLE_MEASURE_MSK) ?
Hans Verkuil85756a02015-05-12 08:52:21 -0300551 "enabled " : "disabled ",
Hans Verkuil4da70762015-05-22 06:30:59 -0300552 (vmr_ctrl & M00233_CONTROL_BITMAP_ENABLE_INTERRUPT_MSK) ?
Hans Verkuil85756a02015-05-12 08:52:21 -0300553 "irq-enabled " : "irq-disabled ",
Hans Verkuil4da70762015-05-22 06:30:59 -0300554 (vmr_ctrl & M00233_CONTROL_BITMAP_UPDATE_ON_HSYNC_MSK) ?
Hans Verkuil85756a02015-05-12 08:52:21 -0300555 "update-on-hsync " : "",
Hans Verkuil4da70762015-05-22 06:30:59 -0300556 (vmr_stat & M00233_STATUS_BITMAP_HSYNC_TIMEOUT_MSK) ?
Hans Verkuil85756a02015-05-12 08:52:21 -0300557 "hsync-timeout " : "",
Hans Verkuil4da70762015-05-22 06:30:59 -0300558 (vmr_stat & M00233_STATUS_BITMAP_INIT_DONE_MSK) ?
Hans Verkuil85756a02015-05-12 08:52:21 -0300559 "init-done" : "");
560 cobalt_info("rx%d: irq_status: 0x%02x irq_triggers: 0x%02x\n", rx,
Hans Verkuil4da70762015-05-22 06:30:59 -0300561 ioread32(&vmr->irq_status) & 0xff,
562 ioread32(&vmr->irq_triggers) & 0xff);
563 cobalt_info("rx%d: vsync: %d\n", rx, ioread32(&vmr->vsync_time));
564 cobalt_info("rx%d: vbp: %d\n", rx, ioread32(&vmr->vback_porch));
565 cobalt_info("rx%d: vact: %d\n", rx, ioread32(&vmr->vactive_area));
566 cobalt_info("rx%d: vfb: %d\n", rx, ioread32(&vmr->vfront_porch));
567 cobalt_info("rx%d: hsync: %d\n", rx, ioread32(&vmr->hsync_time));
568 cobalt_info("rx%d: hbp: %d\n", rx, ioread32(&vmr->hback_porch));
569 cobalt_info("rx%d: hact: %d\n", rx, ioread32(&vmr->hactive_area));
570 cobalt_info("rx%d: hfb: %d\n", rx, ioread32(&vmr->hfront_porch));
Hans Verkuil85756a02015-05-12 08:52:21 -0300571 cobalt_info("rx%d: Freewheeling: %s%s%s\n", rx,
Hans Verkuil4da70762015-05-22 06:30:59 -0300572 (ioread32(&fw->ctrl) & M00473_CTRL_BITMAP_ENABLE_MSK) ?
Hans Verkuil85756a02015-05-12 08:52:21 -0300573 "enabled " : "disabled ",
Hans Verkuil4da70762015-05-22 06:30:59 -0300574 (ioread32(&fw->ctrl) & M00473_CTRL_BITMAP_FORCE_FREEWHEEL_MODE_MSK) ?
Hans Verkuil85756a02015-05-12 08:52:21 -0300575 "forced " : "",
Hans Verkuil4da70762015-05-22 06:30:59 -0300576 (ioread32(&fw->status) & M00473_STATUS_BITMAP_FREEWHEEL_MODE_MSK) ?
Hans Verkuil85756a02015-05-12 08:52:21 -0300577 "freewheeling " : "video-passthrough ");
Hans Verkuil4da70762015-05-22 06:30:59 -0300578 iowrite32(0xff, &vmr->irq_status);
Hans Verkuil85756a02015-05-12 08:52:21 -0300579 cobalt_info("rx%d: Clock Loss Detection: %s%s\n", rx,
Hans Verkuil4da70762015-05-22 06:30:59 -0300580 (ioread32(&clkloss->ctrl) & M00479_CTRL_BITMAP_ENABLE_MSK) ?
Hans Verkuil85756a02015-05-12 08:52:21 -0300581 "enabled " : "disabled ",
Hans Verkuil4da70762015-05-22 06:30:59 -0300582 (ioread32(&clkloss->status) & M00479_STATUS_BITMAP_CLOCK_MISSING_MSK) ?
Hans Verkuil85756a02015-05-12 08:52:21 -0300583 "clock-missing " : "found-clock ");
Hans Verkuil4da70762015-05-22 06:30:59 -0300584 cobalt_info("rx%d: Packer: %x\n", rx, ioread32(&packer->control));
Hans Verkuil85756a02015-05-12 08:52:21 -0300585}
586
587static int cobalt_log_status(struct file *file, void *priv_fh)
588{
589 struct cobalt_stream *s = video_drvdata(file);
590 struct cobalt *cobalt = s->cobalt;
Hans Verkuil4da70762015-05-22 06:30:59 -0300591 struct m00514_syncgen_flow_evcnt_regmap __iomem *vo =
Hans Verkuil85756a02015-05-12 08:52:21 -0300592 COBALT_TX_BASE(cobalt);
593 u8 stat;
594
595 cobalt_info("%s", cobalt->hdl_info);
596 cobalt_info("sysctrl: %08x, sysstat: %08x\n",
597 cobalt_g_sysctrl(cobalt),
598 cobalt_g_sysstat(cobalt));
599 cobalt_info("dma channel: %d, video channel: %d\n",
600 s->dma_channel, s->video_channel);
601 cobalt_pcie_status_show(cobalt);
602 cobalt_cpld_status(cobalt);
603 cobalt_irq_log_status(cobalt);
604 v4l2_subdev_call(s->sd, core, log_status);
605 if (!s->is_output) {
606 cobalt_video_input_status_show(s);
607 return 0;
608 }
609
Hans Verkuil4da70762015-05-22 06:30:59 -0300610 stat = ioread32(&vo->rd_status);
Hans Verkuil85756a02015-05-12 08:52:21 -0300611
612 cobalt_info("tx: status: %s%s\n",
613 (stat & M00514_RD_STATUS_BITMAP_FLOW_CTRL_NO_DATA_ERROR_MSK) ?
614 "no_data " : "",
615 (stat & M00514_RD_STATUS_BITMAP_READY_BUFFER_FULL_MSK) ?
616 "ready_buffer_full " : "");
Hans Verkuil4da70762015-05-22 06:30:59 -0300617 cobalt_info("tx: evcnt: %d\n", ioread32(&vo->rd_evcnt_count));
Hans Verkuil85756a02015-05-12 08:52:21 -0300618 return 0;
619}
620
621static int cobalt_enum_dv_timings(struct file *file, void *priv_fh,
622 struct v4l2_enum_dv_timings *timings)
623{
624 struct cobalt_stream *s = video_drvdata(file);
625
626 if (s->input == 1) {
627 if (timings->index)
628 return -EINVAL;
629 memset(timings->reserved, 0, sizeof(timings->reserved));
630 timings->timings = cea1080p60;
631 return 0;
632 }
633 timings->pad = 0;
634 return v4l2_subdev_call(s->sd,
635 pad, enum_dv_timings, timings);
636}
637
638static int cobalt_s_dv_timings(struct file *file, void *priv_fh,
639 struct v4l2_dv_timings *timings)
640{
641 struct cobalt_stream *s = video_drvdata(file);
642 int err;
643
644 if (vb2_is_busy(&s->q))
645 return -EBUSY;
646
647 if (s->input == 1) {
648 *timings = cea1080p60;
649 return 0;
650 }
651 err = v4l2_subdev_call(s->sd,
652 video, s_dv_timings, timings);
653 if (!err) {
654 s->timings = *timings;
655 s->width = timings->bt.width;
656 s->height = timings->bt.height;
657 s->stride = timings->bt.width * s->bpp;
658 }
659 return err;
660}
661
662static int cobalt_g_dv_timings(struct file *file, void *priv_fh,
663 struct v4l2_dv_timings *timings)
664{
665 struct cobalt_stream *s = video_drvdata(file);
666
667 if (s->input == 1) {
668 *timings = cea1080p60;
669 return 0;
670 }
671 return v4l2_subdev_call(s->sd,
672 video, g_dv_timings, timings);
673}
674
675static int cobalt_query_dv_timings(struct file *file, void *priv_fh,
676 struct v4l2_dv_timings *timings)
677{
678 struct cobalt_stream *s = video_drvdata(file);
679
680 if (s->input == 1) {
681 *timings = cea1080p60;
682 return 0;
683 }
684 return v4l2_subdev_call(s->sd,
685 video, query_dv_timings, timings);
686}
687
688static int cobalt_dv_timings_cap(struct file *file, void *priv_fh,
689 struct v4l2_dv_timings_cap *cap)
690{
691 struct cobalt_stream *s = video_drvdata(file);
692
693 cap->pad = 0;
694 return v4l2_subdev_call(s->sd,
695 pad, dv_timings_cap, cap);
696}
697
698static int cobalt_enum_fmt_vid_cap(struct file *file, void *priv_fh,
699 struct v4l2_fmtdesc *f)
700{
701 switch (f->index) {
702 case 0:
703 strlcpy(f->description, "YUV 4:2:2", sizeof(f->description));
704 f->pixelformat = V4L2_PIX_FMT_YUYV;
705 break;
706 case 1:
707 strlcpy(f->description, "RGB24", sizeof(f->description));
708 f->pixelformat = V4L2_PIX_FMT_RGB24;
709 break;
710 case 2:
711 strlcpy(f->description, "RGB32", sizeof(f->description));
712 f->pixelformat = V4L2_PIX_FMT_BGR32;
713 break;
714 default:
715 return -EINVAL;
716 }
717
718 return 0;
719}
720
721static int cobalt_g_fmt_vid_cap(struct file *file, void *priv_fh,
722 struct v4l2_format *f)
723{
724 struct cobalt_stream *s = video_drvdata(file);
725 struct v4l2_pix_format *pix = &f->fmt.pix;
726 struct v4l2_subdev_format sd_fmt;
727
728 pix->width = s->width;
729 pix->height = s->height;
730 pix->bytesperline = s->stride;
731 pix->field = V4L2_FIELD_NONE;
732
733 if (s->input == 1) {
734 pix->colorspace = V4L2_COLORSPACE_SRGB;
735 } else {
736 sd_fmt.pad = s->pad_source;
737 sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
738 v4l2_subdev_call(s->sd, pad, get_fmt, NULL, &sd_fmt);
739 v4l2_fill_pix_format(pix, &sd_fmt.format);
740 pix->colorspace = sd_fmt.format.colorspace;
Hans Verkuil136a5e92015-05-31 06:48:00 -0300741 pix->xfer_func = sd_fmt.format.xfer_func;
Hans Verkuil85756a02015-05-12 08:52:21 -0300742 pix->ycbcr_enc = sd_fmt.format.ycbcr_enc;
743 pix->quantization = sd_fmt.format.quantization;
744 }
745
746 pix->pixelformat = s->pixfmt;
747 pix->sizeimage = pix->bytesperline * pix->height;
748
749 return 0;
750}
751
752static int cobalt_try_fmt_vid_cap(struct file *file, void *priv_fh,
753 struct v4l2_format *f)
754{
755 struct cobalt_stream *s = video_drvdata(file);
756 struct v4l2_pix_format *pix = &f->fmt.pix;
757 struct v4l2_subdev_format sd_fmt;
758
759 /* Check for min (QCIF) and max (Full HD) size */
760 if ((pix->width < 176) || (pix->height < 144)) {
761 pix->width = 176;
762 pix->height = 144;
763 }
764
765 if ((pix->width > 1920) || (pix->height > 1080)) {
766 pix->width = 1920;
767 pix->height = 1080;
768 }
769
770 /* Make width multiple of 4 */
771 pix->width &= ~0x3;
772
773 /* Make height multiple of 2 */
774 pix->height &= ~0x1;
775
776 if (s->input == 1) {
777 /* Generator => fixed format only */
778 pix->width = 1920;
779 pix->height = 1080;
780 pix->colorspace = V4L2_COLORSPACE_SRGB;
781 } else {
782 sd_fmt.pad = s->pad_source;
783 sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
784 v4l2_subdev_call(s->sd, pad, get_fmt, NULL, &sd_fmt);
785 v4l2_fill_pix_format(pix, &sd_fmt.format);
786 pix->colorspace = sd_fmt.format.colorspace;
Hans Verkuil136a5e92015-05-31 06:48:00 -0300787 pix->xfer_func = sd_fmt.format.xfer_func;
Hans Verkuil85756a02015-05-12 08:52:21 -0300788 pix->ycbcr_enc = sd_fmt.format.ycbcr_enc;
789 pix->quantization = sd_fmt.format.quantization;
790 }
791
792 switch (pix->pixelformat) {
793 case V4L2_PIX_FMT_YUYV:
794 default:
795 pix->bytesperline = max(pix->bytesperline & ~0x3,
796 pix->width * COBALT_BYTES_PER_PIXEL_YUYV);
797 pix->pixelformat = V4L2_PIX_FMT_YUYV;
798 break;
799 case V4L2_PIX_FMT_RGB24:
800 pix->bytesperline = max(pix->bytesperline & ~0x3,
801 pix->width * COBALT_BYTES_PER_PIXEL_RGB24);
802 break;
803 case V4L2_PIX_FMT_BGR32:
804 pix->bytesperline = max(pix->bytesperline & ~0x3,
805 pix->width * COBALT_BYTES_PER_PIXEL_RGB32);
806 break;
807 }
808
809 pix->sizeimage = pix->bytesperline * pix->height;
810 pix->field = V4L2_FIELD_NONE;
811 pix->priv = 0;
812
813 return 0;
814}
815
816static int cobalt_s_fmt_vid_cap(struct file *file, void *priv_fh,
817 struct v4l2_format *f)
818{
819 struct cobalt_stream *s = video_drvdata(file);
820 struct v4l2_pix_format *pix = &f->fmt.pix;
821
822 if (vb2_is_busy(&s->q))
823 return -EBUSY;
824
825 if (cobalt_try_fmt_vid_cap(file, priv_fh, f))
826 return -EINVAL;
827
828 s->width = pix->width;
829 s->height = pix->height;
830 s->stride = pix->bytesperline;
831 switch (pix->pixelformat) {
832 case V4L2_PIX_FMT_YUYV:
833 s->bpp = COBALT_BYTES_PER_PIXEL_YUYV;
834 break;
835 case V4L2_PIX_FMT_RGB24:
836 s->bpp = COBALT_BYTES_PER_PIXEL_RGB24;
837 break;
838 case V4L2_PIX_FMT_BGR32:
839 s->bpp = COBALT_BYTES_PER_PIXEL_RGB32;
840 break;
841 default:
842 return -EINVAL;
843 }
844 s->pixfmt = pix->pixelformat;
845 cobalt_enable_input(s);
846
847 return 0;
848}
849
850static int cobalt_try_fmt_vid_out(struct file *file, void *priv_fh,
851 struct v4l2_format *f)
852{
853 struct v4l2_pix_format *pix = &f->fmt.pix;
854
855 /* Check for min (QCIF) and max (Full HD) size */
856 if ((pix->width < 176) || (pix->height < 144)) {
857 pix->width = 176;
858 pix->height = 144;
859 }
860
861 if ((pix->width > 1920) || (pix->height > 1080)) {
862 pix->width = 1920;
863 pix->height = 1080;
864 }
865
866 /* Make width multiple of 4 */
867 pix->width &= ~0x3;
868
869 /* Make height multiple of 2 */
870 pix->height &= ~0x1;
871
872 switch (pix->pixelformat) {
873 case V4L2_PIX_FMT_YUYV:
874 default:
875 pix->bytesperline = max(pix->bytesperline & ~0x3,
876 pix->width * COBALT_BYTES_PER_PIXEL_YUYV);
877 pix->pixelformat = V4L2_PIX_FMT_YUYV;
878 break;
879 case V4L2_PIX_FMT_BGR32:
880 pix->bytesperline = max(pix->bytesperline & ~0x3,
881 pix->width * COBALT_BYTES_PER_PIXEL_RGB32);
882 break;
883 }
884
885 pix->sizeimage = pix->bytesperline * pix->height;
886 pix->field = V4L2_FIELD_NONE;
887
888 return 0;
889}
890
891static int cobalt_g_fmt_vid_out(struct file *file, void *priv_fh,
892 struct v4l2_format *f)
893{
894 struct cobalt_stream *s = video_drvdata(file);
895 struct v4l2_pix_format *pix = &f->fmt.pix;
896
897 pix->width = s->width;
898 pix->height = s->height;
899 pix->bytesperline = s->stride;
900 pix->field = V4L2_FIELD_NONE;
901 pix->pixelformat = s->pixfmt;
902 pix->colorspace = s->colorspace;
Hans Verkuil136a5e92015-05-31 06:48:00 -0300903 pix->xfer_func = s->xfer_func;
Hans Verkuil85756a02015-05-12 08:52:21 -0300904 pix->ycbcr_enc = s->ycbcr_enc;
905 pix->quantization = s->quantization;
906 pix->sizeimage = pix->bytesperline * pix->height;
907
908 return 0;
909}
910
911static int cobalt_enum_fmt_vid_out(struct file *file, void *priv_fh,
912 struct v4l2_fmtdesc *f)
913{
914 switch (f->index) {
915 case 0:
916 strlcpy(f->description, "YUV 4:2:2", sizeof(f->description));
917 f->pixelformat = V4L2_PIX_FMT_YUYV;
918 break;
919 case 1:
920 strlcpy(f->description, "RGB32", sizeof(f->description));
921 f->pixelformat = V4L2_PIX_FMT_BGR32;
922 break;
923 default:
924 return -EINVAL;
925 }
926
927 return 0;
928}
929
930static int cobalt_s_fmt_vid_out(struct file *file, void *priv_fh,
931 struct v4l2_format *f)
932{
933 struct cobalt_stream *s = video_drvdata(file);
934 struct v4l2_pix_format *pix = &f->fmt.pix;
935 struct v4l2_subdev_format sd_fmt = { 0 };
936
937 if (cobalt_try_fmt_vid_out(file, priv_fh, f))
938 return -EINVAL;
939
940 if (vb2_is_busy(&s->q) && (pix->pixelformat != s->pixfmt ||
941 pix->width != s->width || pix->height != s->height ||
942 pix->bytesperline != s->stride))
943 return -EBUSY;
944
945 switch (pix->pixelformat) {
946 case V4L2_PIX_FMT_YUYV:
947 s->bpp = COBALT_BYTES_PER_PIXEL_YUYV;
948 break;
949 case V4L2_PIX_FMT_BGR32:
950 s->bpp = COBALT_BYTES_PER_PIXEL_RGB32;
951 break;
952 default:
953 return -EINVAL;
954 }
955 s->width = pix->width;
956 s->height = pix->height;
957 s->stride = pix->bytesperline;
958 s->pixfmt = pix->pixelformat;
959 s->colorspace = pix->colorspace;
Hans Verkuil136a5e92015-05-31 06:48:00 -0300960 s->xfer_func = pix->xfer_func;
Hans Verkuil85756a02015-05-12 08:52:21 -0300961 s->ycbcr_enc = pix->ycbcr_enc;
962 s->quantization = pix->quantization;
963 sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
964 v4l2_subdev_call(s->sd, pad, get_fmt, NULL, &sd_fmt);
965 sd_fmt.format.colorspace = pix->colorspace;
Hans Verkuil136a5e92015-05-31 06:48:00 -0300966 sd_fmt.format.xfer_func = pix->xfer_func;
Hans Verkuil85756a02015-05-12 08:52:21 -0300967 sd_fmt.format.ycbcr_enc = pix->ycbcr_enc;
968 sd_fmt.format.quantization = pix->quantization;
969 v4l2_subdev_call(s->sd, pad, set_fmt, NULL, &sd_fmt);
970 return 0;
971}
972
973static int cobalt_enum_input(struct file *file, void *priv_fh,
974 struct v4l2_input *inp)
975{
976 struct cobalt_stream *s = video_drvdata(file);
977
978 if (inp->index > 1)
979 return -EINVAL;
980 if (inp->index == 0)
981 snprintf(inp->name, sizeof(inp->name),
982 "HDMI-%d", s->video_channel);
983 else
984 snprintf(inp->name, sizeof(inp->name),
985 "Generator-%d", s->video_channel);
986 inp->type = V4L2_INPUT_TYPE_CAMERA;
987 inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
988 if (inp->index == 1)
989 return 0;
990 return v4l2_subdev_call(s->sd,
991 video, g_input_status, &inp->status);
992}
993
994static int cobalt_g_input(struct file *file, void *priv_fh, unsigned int *i)
995{
996 struct cobalt_stream *s = video_drvdata(file);
997
998 *i = s->input;
999 return 0;
1000}
1001
1002static int cobalt_s_input(struct file *file, void *priv_fh, unsigned int i)
1003{
1004 struct cobalt_stream *s = video_drvdata(file);
1005
1006 if (i >= 2)
1007 return -EINVAL;
1008 if (vb2_is_busy(&s->q))
1009 return -EBUSY;
1010 s->input = i;
1011
1012 cobalt_enable_input(s);
1013
1014 if (s->input == 1) /* Test Pattern Generator */
1015 return 0;
1016
1017 return v4l2_subdev_call(s->sd, video, s_routing,
1018 ADV76XX_PAD_HDMI_PORT_A, 0, 0);
1019}
1020
1021static int cobalt_enum_output(struct file *file, void *priv_fh,
1022 struct v4l2_output *out)
1023{
1024 if (out->index)
1025 return -EINVAL;
1026 snprintf(out->name, sizeof(out->name), "HDMI-%d", out->index);
1027 out->type = V4L2_OUTPUT_TYPE_ANALOG;
1028 out->capabilities = V4L2_OUT_CAP_DV_TIMINGS;
1029 return 0;
1030}
1031
1032static int cobalt_g_output(struct file *file, void *priv_fh, unsigned int *i)
1033{
1034 *i = 0;
1035 return 0;
1036}
1037
1038static int cobalt_s_output(struct file *file, void *priv_fh, unsigned int i)
1039{
1040 return i ? -EINVAL : 0;
1041}
1042
1043static int cobalt_g_edid(struct file *file, void *fh, struct v4l2_edid *edid)
1044{
1045 struct cobalt_stream *s = video_drvdata(file);
1046 u32 pad = edid->pad;
1047 int ret;
1048
1049 if (edid->pad >= (s->is_output ? 1 : 2))
1050 return -EINVAL;
1051 edid->pad = 0;
1052 ret = v4l2_subdev_call(s->sd, pad, get_edid, edid);
1053 edid->pad = pad;
1054 return ret;
1055}
1056
1057static int cobalt_s_edid(struct file *file, void *fh, struct v4l2_edid *edid)
1058{
1059 struct cobalt_stream *s = video_drvdata(file);
1060 u32 pad = edid->pad;
1061 int ret;
1062
1063 if (edid->pad >= 2)
1064 return -EINVAL;
1065 edid->pad = 0;
1066 ret = v4l2_subdev_call(s->sd, pad, set_edid, edid);
1067 edid->pad = pad;
1068 return ret;
1069}
1070
1071static int cobalt_subscribe_event(struct v4l2_fh *fh,
1072 const struct v4l2_event_subscription *sub)
1073{
1074 switch (sub->type) {
1075 case V4L2_EVENT_SOURCE_CHANGE:
1076 return v4l2_event_subscribe(fh, sub, 4, NULL);
1077 }
1078 return v4l2_ctrl_subscribe_event(fh, sub);
1079}
1080
1081static int cobalt_g_parm(struct file *file, void *fh, struct v4l2_streamparm *a)
1082{
1083 if (a->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1084 return -EINVAL;
1085 a->parm.capture.timeperframe.numerator = 1;
1086 a->parm.capture.timeperframe.denominator = 60;
1087 a->parm.capture.readbuffers = 3;
1088 return 0;
1089}
1090
1091static const struct v4l2_ioctl_ops cobalt_ioctl_ops = {
1092 .vidioc_querycap = cobalt_querycap,
1093 .vidioc_g_parm = cobalt_g_parm,
1094 .vidioc_log_status = cobalt_log_status,
1095 .vidioc_streamon = vb2_ioctl_streamon,
1096 .vidioc_streamoff = vb2_ioctl_streamoff,
1097 .vidioc_enum_input = cobalt_enum_input,
1098 .vidioc_g_input = cobalt_g_input,
1099 .vidioc_s_input = cobalt_s_input,
1100 .vidioc_enum_fmt_vid_cap = cobalt_enum_fmt_vid_cap,
1101 .vidioc_g_fmt_vid_cap = cobalt_g_fmt_vid_cap,
1102 .vidioc_s_fmt_vid_cap = cobalt_s_fmt_vid_cap,
1103 .vidioc_try_fmt_vid_cap = cobalt_try_fmt_vid_cap,
1104 .vidioc_enum_output = cobalt_enum_output,
1105 .vidioc_g_output = cobalt_g_output,
1106 .vidioc_s_output = cobalt_s_output,
1107 .vidioc_enum_fmt_vid_out = cobalt_enum_fmt_vid_out,
1108 .vidioc_g_fmt_vid_out = cobalt_g_fmt_vid_out,
1109 .vidioc_s_fmt_vid_out = cobalt_s_fmt_vid_out,
1110 .vidioc_try_fmt_vid_out = cobalt_try_fmt_vid_out,
1111 .vidioc_s_dv_timings = cobalt_s_dv_timings,
1112 .vidioc_g_dv_timings = cobalt_g_dv_timings,
1113 .vidioc_query_dv_timings = cobalt_query_dv_timings,
1114 .vidioc_enum_dv_timings = cobalt_enum_dv_timings,
1115 .vidioc_dv_timings_cap = cobalt_dv_timings_cap,
1116 .vidioc_g_edid = cobalt_g_edid,
1117 .vidioc_s_edid = cobalt_s_edid,
1118 .vidioc_subscribe_event = cobalt_subscribe_event,
1119 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1120 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1121 .vidioc_create_bufs = vb2_ioctl_create_bufs,
1122 .vidioc_querybuf = vb2_ioctl_querybuf,
1123 .vidioc_qbuf = vb2_ioctl_qbuf,
1124 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1125 .vidioc_expbuf = vb2_ioctl_expbuf,
1126#ifdef CONFIG_VIDEO_ADV_DEBUG
1127 .vidioc_g_register = cobalt_g_register,
1128 .vidioc_s_register = cobalt_s_register,
1129#endif
1130};
1131
1132static const struct v4l2_ioctl_ops cobalt_ioctl_empty_ops = {
1133#ifdef CONFIG_VIDEO_ADV_DEBUG
1134 .vidioc_g_register = cobalt_g_register,
1135 .vidioc_s_register = cobalt_s_register,
1136#endif
1137};
1138
1139/* Register device nodes */
1140
1141static const struct v4l2_file_operations cobalt_fops = {
1142 .owner = THIS_MODULE,
1143 .open = v4l2_fh_open,
1144 .unlocked_ioctl = video_ioctl2,
1145 .release = vb2_fop_release,
1146 .poll = vb2_fop_poll,
1147 .mmap = vb2_fop_mmap,
1148 .read = vb2_fop_read,
1149};
1150
1151static const struct v4l2_file_operations cobalt_out_fops = {
1152 .owner = THIS_MODULE,
1153 .open = v4l2_fh_open,
1154 .unlocked_ioctl = video_ioctl2,
1155 .release = vb2_fop_release,
1156 .poll = vb2_fop_poll,
1157 .mmap = vb2_fop_mmap,
1158 .write = vb2_fop_write,
1159};
1160
1161static const struct v4l2_file_operations cobalt_empty_fops = {
1162 .owner = THIS_MODULE,
1163 .open = v4l2_fh_open,
1164 .unlocked_ioctl = video_ioctl2,
1165 .release = v4l2_fh_release,
1166};
1167
1168static int cobalt_node_register(struct cobalt *cobalt, int node)
1169{
1170 static const struct v4l2_dv_timings dv1080p60 =
1171 V4L2_DV_BT_CEA_1920X1080P60;
1172 struct cobalt_stream *s = cobalt->streams + node;
1173 struct video_device *vdev = &s->vdev;
1174 struct vb2_queue *q = &s->q;
1175 int ret;
1176
1177 mutex_init(&s->lock);
1178 spin_lock_init(&s->irqlock);
1179
1180 snprintf(vdev->name, sizeof(vdev->name),
1181 "%s-%d", cobalt->v4l2_dev.name, node);
1182 s->width = 1920;
1183 /* Audio frames are just 4 lines of 1920 bytes */
1184 s->height = s->is_audio ? 4 : 1080;
1185
1186 if (s->is_audio) {
1187 s->bpp = 1;
1188 s->pixfmt = V4L2_PIX_FMT_GREY;
1189 } else if (s->is_output) {
1190 s->bpp = COBALT_BYTES_PER_PIXEL_RGB32;
1191 s->pixfmt = V4L2_PIX_FMT_BGR32;
1192 } else {
1193 s->bpp = COBALT_BYTES_PER_PIXEL_YUYV;
1194 s->pixfmt = V4L2_PIX_FMT_YUYV;
1195 }
1196 s->colorspace = V4L2_COLORSPACE_SRGB;
1197 s->stride = s->width * s->bpp;
1198
1199 if (!s->is_audio) {
1200 if (s->is_dummy)
1201 cobalt_warn("Setting up dummy video node %d\n", node);
1202 vdev->v4l2_dev = &cobalt->v4l2_dev;
1203 if (s->is_dummy)
1204 vdev->fops = &cobalt_empty_fops;
1205 else
1206 vdev->fops = s->is_output ? &cobalt_out_fops :
1207 &cobalt_fops;
1208 vdev->release = video_device_release_empty;
1209 vdev->vfl_dir = s->is_output ? VFL_DIR_TX : VFL_DIR_RX;
1210 vdev->lock = &s->lock;
1211 if (s->sd)
1212 vdev->ctrl_handler = s->sd->ctrl_handler;
1213 s->timings = dv1080p60;
1214 v4l2_subdev_call(s->sd, video, s_dv_timings, &s->timings);
1215 if (!s->is_output && s->sd)
1216 cobalt_enable_input(s);
1217 vdev->ioctl_ops = s->is_dummy ? &cobalt_ioctl_empty_ops :
1218 &cobalt_ioctl_ops;
1219 }
1220
1221 INIT_LIST_HEAD(&s->bufs);
1222 q->type = s->is_output ? V4L2_BUF_TYPE_VIDEO_OUTPUT :
1223 V4L2_BUF_TYPE_VIDEO_CAPTURE;
1224 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1225 q->io_modes |= s->is_output ? VB2_WRITE : VB2_READ;
1226 q->drv_priv = s;
1227 q->buf_struct_size = sizeof(struct cobalt_buffer);
1228 q->ops = &cobalt_qops;
1229 q->mem_ops = &vb2_dma_sg_memops;
1230 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1231 q->min_buffers_needed = 2;
1232 q->lock = &s->lock;
1233 vdev->queue = q;
1234
1235 video_set_drvdata(vdev, s);
1236 ret = vb2_queue_init(q);
1237 if (!s->is_audio && ret == 0)
1238 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
1239 else if (!s->is_dummy)
1240 ret = cobalt_alsa_init(s);
1241
1242 if (ret < 0) {
1243 if (!s->is_audio)
1244 cobalt_err("couldn't register v4l2 device node %d\n",
1245 node);
1246 return ret;
1247 }
1248 cobalt_info("registered node %d\n", node);
1249 return 0;
1250}
1251
1252/* Initialize v4l2 variables and register v4l2 devices */
1253int cobalt_nodes_register(struct cobalt *cobalt)
1254{
1255 int node, ret;
1256
1257 /* Setup V4L2 Devices */
1258 for (node = 0; node < COBALT_NUM_STREAMS; node++) {
1259 ret = cobalt_node_register(cobalt, node);
1260 if (ret)
1261 return ret;
1262 }
1263 return 0;
1264}
1265
1266/* Unregister v4l2 devices */
1267void cobalt_nodes_unregister(struct cobalt *cobalt)
1268{
1269 int node;
1270
1271 /* Teardown all streams */
1272 for (node = 0; node < COBALT_NUM_STREAMS; node++) {
1273 struct cobalt_stream *s = cobalt->streams + node;
1274 struct video_device *vdev = &s->vdev;
1275
1276 if (!s->is_audio)
1277 video_unregister_device(vdev);
1278 else if (!s->is_dummy)
1279 cobalt_alsa_exit(s);
1280 }
1281}