2 * Copyright (C) 2015 VanguardiaSur - www.vanguardiasur.com.ar
4 * Based on original driver by Krzysztof Ha?asa:
5 * Copyright (C) 2015 Industrial Research Institute for Automation
6 * and Measurements PIAP
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License
10 * as published by the Free Software Foundation.
14 #include <linux/init.h>
15 #include <linux/delay.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <linux/kernel.h>
19 #include <linux/slab.h>
20 #include <media/v4l2-common.h>
21 #include <media/v4l2-event.h>
22 #include <media/videobuf2-vmalloc.h>
24 #include "tw686x-regs.h"
26 #define TW686X_INPUTS_PER_CH 4
27 #define TW686X_VIDEO_WIDTH 720
28 #define TW686X_VIDEO_HEIGHT(id) ((id & V4L2_STD_625_50) ? 576 : 480)
30 static const struct tw686x_format formats
[] = {
32 .fourcc
= V4L2_PIX_FMT_UYVY
,
36 .fourcc
= V4L2_PIX_FMT_RGB565
,
40 .fourcc
= V4L2_PIX_FMT_YUYV
,
46 static unsigned int tw686x_fields_map(v4l2_std_id std
, unsigned int fps
)
48 static const unsigned int map
[15] = {
49 0x00000000, 0x00000001, 0x00004001, 0x00104001, 0x00404041,
50 0x01041041, 0x01104411, 0x01111111, 0x04444445, 0x04511445,
51 0x05145145, 0x05151515, 0x05515455, 0x05551555, 0x05555555
54 static const unsigned int std_625_50
[26] = {
55 0, 1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 7, 7,
56 8, 8, 9, 10, 10, 11, 11, 12, 13, 13, 14, 14, 0
59 static const unsigned int std_525_60
[31] = {
60 0, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7,
61 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 0, 0
65 (std
& V4L2_STD_625_50
) ? std_625_50
[fps
] : std_525_60
[fps
];
70 static void tw686x_set_framerate(struct tw686x_video_channel
*vc
,
78 map
= tw686x_fields_map(vc
->video_standard
, fps
) << 1;
82 reg_write(vc
->dev
, VIDEO_FIELD_CTRL
[vc
->ch
], map
);
86 static const struct tw686x_format
*format_by_fourcc(unsigned int fourcc
)
90 for (cnt
= 0; cnt
< ARRAY_SIZE(formats
); cnt
++)
91 if (formats
[cnt
].fourcc
== fourcc
)
96 static int tw686x_queue_setup(struct vb2_queue
*vq
,
97 unsigned int *nbuffers
, unsigned int *nplanes
,
98 unsigned int sizes
[], void *alloc_ctxs
[])
100 struct tw686x_video_channel
*vc
= vb2_get_drv_priv(vq
);
101 unsigned int szimage
=
102 (vc
->width
* vc
->height
* vc
->format
->depth
) >> 3;
105 * Let's request at least three buffers: two for the
106 * DMA engine and one for userspace.
108 if (vq
->num_buffers
+ *nbuffers
< 3)
109 *nbuffers
= 3 - vq
->num_buffers
;
112 if (*nplanes
!= 1 || sizes
[0] < szimage
)
122 static void tw686x_buf_queue(struct vb2_buffer
*vb
)
124 struct tw686x_video_channel
*vc
= vb2_get_drv_priv(vb
->vb2_queue
);
125 struct tw686x_dev
*dev
= vc
->dev
;
126 struct pci_dev
*pci_dev
;
128 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
129 struct tw686x_v4l2_buf
*buf
=
130 container_of(vbuf
, struct tw686x_v4l2_buf
, vb
);
132 /* Check device presence */
133 spin_lock_irqsave(&dev
->lock
, flags
);
134 pci_dev
= dev
->pci_dev
;
135 spin_unlock_irqrestore(&dev
->lock
, flags
);
137 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
141 spin_lock_irqsave(&vc
->qlock
, flags
);
142 list_add_tail(&buf
->list
, &vc
->vidq_queued
);
143 spin_unlock_irqrestore(&vc
->qlock
, flags
);
147 * We can call this even when alloc_dma failed for the given channel
149 static void tw686x_free_dma(struct tw686x_video_channel
*vc
, unsigned int pb
)
151 struct tw686x_dma_desc
*desc
= &vc
->dma_descs
[pb
];
152 struct tw686x_dev
*dev
= vc
->dev
;
153 struct pci_dev
*pci_dev
;
156 /* Check device presence. Shouldn't really happen! */
157 spin_lock_irqsave(&dev
->lock
, flags
);
158 pci_dev
= dev
->pci_dev
;
159 spin_unlock_irqrestore(&dev
->lock
, flags
);
161 WARN(1, "trying to deallocate on missing device\n");
166 pci_free_consistent(dev
->pci_dev
, desc
->size
,
167 desc
->virt
, desc
->phys
);
172 static int tw686x_alloc_dma(struct tw686x_video_channel
*vc
, unsigned int pb
)
174 struct tw686x_dev
*dev
= vc
->dev
;
175 u32 reg
= pb
? VDMA_B_ADDR
[vc
->ch
] : VDMA_P_ADDR
[vc
->ch
];
179 WARN(vc
->dma_descs
[pb
].virt
,
180 "Allocating buffer but previous still here\n");
182 len
= (vc
->width
* vc
->height
* vc
->format
->depth
) >> 3;
183 virt
= pci_alloc_consistent(dev
->pci_dev
, len
,
184 &vc
->dma_descs
[pb
].phys
);
186 v4l2_err(&dev
->v4l2_dev
,
187 "dma%d: unable to allocate %s-buffer\n",
188 vc
->ch
, pb
? "B" : "P");
191 vc
->dma_descs
[pb
].size
= len
;
192 vc
->dma_descs
[pb
].virt
= virt
;
193 reg_write(dev
, reg
, vc
->dma_descs
[pb
].phys
);
198 static void tw686x_buffer_refill(struct tw686x_video_channel
*vc
,
201 struct tw686x_v4l2_buf
*buf
;
203 while (!list_empty(&vc
->vidq_queued
)) {
205 buf
= list_first_entry(&vc
->vidq_queued
,
206 struct tw686x_v4l2_buf
, list
);
207 list_del(&buf
->list
);
209 vc
->curr_bufs
[pb
] = buf
;
212 vc
->curr_bufs
[pb
] = NULL
;
215 static void tw686x_clear_queue(struct tw686x_video_channel
*vc
,
216 enum vb2_buffer_state state
)
220 while (!list_empty(&vc
->vidq_queued
)) {
221 struct tw686x_v4l2_buf
*buf
;
223 buf
= list_first_entry(&vc
->vidq_queued
,
224 struct tw686x_v4l2_buf
, list
);
225 list_del(&buf
->list
);
226 vb2_buffer_done(&buf
->vb
.vb2_buf
, state
);
229 for (pb
= 0; pb
< 2; pb
++) {
230 if (vc
->curr_bufs
[pb
])
231 vb2_buffer_done(&vc
->curr_bufs
[pb
]->vb
.vb2_buf
, state
);
232 vc
->curr_bufs
[pb
] = NULL
;
236 static int tw686x_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
238 struct tw686x_video_channel
*vc
= vb2_get_drv_priv(vq
);
239 struct tw686x_dev
*dev
= vc
->dev
;
240 struct pci_dev
*pci_dev
;
244 /* Check device presence */
245 spin_lock_irqsave(&dev
->lock
, flags
);
246 pci_dev
= dev
->pci_dev
;
247 spin_unlock_irqrestore(&dev
->lock
, flags
);
250 goto err_clear_queue
;
253 spin_lock_irqsave(&vc
->qlock
, flags
);
256 if (!vc
->dma_descs
[0].virt
|| !vc
->dma_descs
[1].virt
) {
257 spin_unlock_irqrestore(&vc
->qlock
, flags
);
258 v4l2_err(&dev
->v4l2_dev
,
259 "video%d: refusing to start without DMA buffers\n",
262 goto err_clear_queue
;
265 for (pb
= 0; pb
< 2; pb
++)
266 tw686x_buffer_refill(vc
, pb
);
267 spin_unlock_irqrestore(&vc
->qlock
, flags
);
272 spin_lock_irqsave(&dev
->lock
, flags
);
273 tw686x_enable_channel(dev
, vc
->ch
);
274 spin_unlock_irqrestore(&dev
->lock
, flags
);
276 mod_timer(&dev
->dma_delay_timer
, jiffies
+ msecs_to_jiffies(100));
281 spin_lock_irqsave(&vc
->qlock
, flags
);
282 tw686x_clear_queue(vc
, VB2_BUF_STATE_QUEUED
);
283 spin_unlock_irqrestore(&vc
->qlock
, flags
);
287 static void tw686x_stop_streaming(struct vb2_queue
*vq
)
289 struct tw686x_video_channel
*vc
= vb2_get_drv_priv(vq
);
290 struct tw686x_dev
*dev
= vc
->dev
;
291 struct pci_dev
*pci_dev
;
294 /* Check device presence */
295 spin_lock_irqsave(&dev
->lock
, flags
);
296 pci_dev
= dev
->pci_dev
;
297 spin_unlock_irqrestore(&dev
->lock
, flags
);
299 tw686x_disable_channel(dev
, vc
->ch
);
301 spin_lock_irqsave(&vc
->qlock
, flags
);
302 tw686x_clear_queue(vc
, VB2_BUF_STATE_ERROR
);
303 spin_unlock_irqrestore(&vc
->qlock
, flags
);
306 static int tw686x_buf_prepare(struct vb2_buffer
*vb
)
308 struct tw686x_video_channel
*vc
= vb2_get_drv_priv(vb
->vb2_queue
);
310 (vc
->width
* vc
->height
* vc
->format
->depth
) >> 3;
312 if (vb2_plane_size(vb
, 0) < size
)
314 vb2_set_plane_payload(vb
, 0, size
);
318 static struct vb2_ops tw686x_video_qops
= {
319 .queue_setup
= tw686x_queue_setup
,
320 .buf_queue
= tw686x_buf_queue
,
321 .buf_prepare
= tw686x_buf_prepare
,
322 .start_streaming
= tw686x_start_streaming
,
323 .stop_streaming
= tw686x_stop_streaming
,
324 .wait_prepare
= vb2_ops_wait_prepare
,
325 .wait_finish
= vb2_ops_wait_finish
,
328 static int tw686x_s_ctrl(struct v4l2_ctrl
*ctrl
)
330 struct tw686x_video_channel
*vc
;
331 struct tw686x_dev
*dev
;
334 vc
= container_of(ctrl
->handler
, struct tw686x_video_channel
,
340 case V4L2_CID_BRIGHTNESS
:
341 reg_write(dev
, BRIGHT
[ch
], ctrl
->val
& 0xff);
344 case V4L2_CID_CONTRAST
:
345 reg_write(dev
, CONTRAST
[ch
], ctrl
->val
);
348 case V4L2_CID_SATURATION
:
349 reg_write(dev
, SAT_U
[ch
], ctrl
->val
);
350 reg_write(dev
, SAT_V
[ch
], ctrl
->val
);
354 reg_write(dev
, HUE
[ch
], ctrl
->val
& 0xff);
361 static const struct v4l2_ctrl_ops ctrl_ops
= {
362 .s_ctrl
= tw686x_s_ctrl
,
365 static int tw686x_g_fmt_vid_cap(struct file
*file
, void *priv
,
366 struct v4l2_format
*f
)
368 struct tw686x_video_channel
*vc
= video_drvdata(file
);
370 f
->fmt
.pix
.width
= vc
->width
;
371 f
->fmt
.pix
.height
= vc
->height
;
372 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
373 f
->fmt
.pix
.pixelformat
= vc
->format
->fourcc
;
374 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
375 f
->fmt
.pix
.bytesperline
= (f
->fmt
.pix
.width
* vc
->format
->depth
) / 8;
376 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
380 static int tw686x_try_fmt_vid_cap(struct file
*file
, void *priv
,
381 struct v4l2_format
*f
)
383 struct tw686x_video_channel
*vc
= video_drvdata(file
);
384 unsigned int video_height
= TW686X_VIDEO_HEIGHT(vc
->video_standard
);
385 const struct tw686x_format
*format
;
387 format
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
389 format
= &formats
[0];
390 f
->fmt
.pix
.pixelformat
= format
->fourcc
;
393 if (f
->fmt
.pix
.width
<= TW686X_VIDEO_WIDTH
/ 2)
394 f
->fmt
.pix
.width
= TW686X_VIDEO_WIDTH
/ 2;
396 f
->fmt
.pix
.width
= TW686X_VIDEO_WIDTH
;
398 if (f
->fmt
.pix
.height
<= video_height
/ 2)
399 f
->fmt
.pix
.height
= video_height
/ 2;
401 f
->fmt
.pix
.height
= video_height
;
403 f
->fmt
.pix
.bytesperline
= (f
->fmt
.pix
.width
* format
->depth
) / 8;
404 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
405 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
406 f
->fmt
.pix
.field
= V4L2_FIELD_INTERLACED
;
411 static int tw686x_s_fmt_vid_cap(struct file
*file
, void *priv
,
412 struct v4l2_format
*f
)
414 struct tw686x_video_channel
*vc
= video_drvdata(file
);
415 u32 val
, width
, line_width
, height
;
416 unsigned long bitsperframe
;
419 if (vb2_is_busy(&vc
->vidq
))
422 bitsperframe
= vc
->width
* vc
->height
* vc
->format
->depth
;
423 err
= tw686x_try_fmt_vid_cap(file
, priv
, f
);
427 vc
->format
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
428 vc
->width
= f
->fmt
.pix
.width
;
429 vc
->height
= f
->fmt
.pix
.height
;
431 /* We need new DMA buffers if the framesize has changed */
432 if (bitsperframe
!= vc
->width
* vc
->height
* vc
->format
->depth
) {
433 for (pb
= 0; pb
< 2; pb
++)
434 tw686x_free_dma(vc
, pb
);
436 for (pb
= 0; pb
< 2; pb
++) {
437 err
= tw686x_alloc_dma(vc
, pb
);
440 tw686x_free_dma(vc
, 0);
446 val
= reg_read(vc
->dev
, VDMA_CHANNEL_CONFIG
[vc
->ch
]);
448 if (vc
->width
<= TW686X_VIDEO_WIDTH
/ 2)
453 if (vc
->height
<= TW686X_VIDEO_HEIGHT(vc
->video_standard
) / 2)
459 val
|= vc
->format
->mode
<< 20;
460 reg_write(vc
->dev
, VDMA_CHANNEL_CONFIG
[vc
->ch
], val
);
462 /* Program the DMA frame size */
463 width
= (vc
->width
* 2) & 0x7ff;
464 height
= vc
->height
/ 2;
465 line_width
= (vc
->width
* 2) & 0x7ff;
466 val
= (height
<< 22) | (line_width
<< 11) | width
;
467 reg_write(vc
->dev
, VDMA_WHP
[vc
->ch
], val
);
471 static int tw686x_querycap(struct file
*file
, void *priv
,
472 struct v4l2_capability
*cap
)
474 struct tw686x_video_channel
*vc
= video_drvdata(file
);
475 struct tw686x_dev
*dev
= vc
->dev
;
477 strlcpy(cap
->driver
, "tw686x", sizeof(cap
->driver
));
478 strlcpy(cap
->card
, dev
->name
, sizeof(cap
->card
));
479 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
),
480 "PCI:%s", pci_name(dev
->pci_dev
));
481 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
|
483 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
487 static int tw686x_s_std(struct file
*file
, void *priv
, v4l2_std_id id
)
489 struct tw686x_video_channel
*vc
= video_drvdata(file
);
490 struct v4l2_format f
;
493 if (vc
->video_standard
== id
)
496 if (vb2_is_busy(&vc
->vidq
))
499 if (id
& V4L2_STD_NTSC
)
501 else if (id
& V4L2_STD_PAL
)
503 else if (id
& V4L2_STD_SECAM
)
505 else if (id
& V4L2_STD_NTSC_443
)
507 else if (id
& V4L2_STD_PAL_M
)
509 else if (id
& V4L2_STD_PAL_Nc
)
511 else if (id
& V4L2_STD_PAL_60
)
516 vc
->video_standard
= id
;
517 reg_write(vc
->dev
, SDT
[vc
->ch
], val
);
519 val
= reg_read(vc
->dev
, VIDEO_CONTROL1
);
520 if (id
& V4L2_STD_625_50
)
521 val
|= (1 << (SYS_MODE_DMA_SHIFT
+ vc
->ch
));
523 val
&= ~(1 << (SYS_MODE_DMA_SHIFT
+ vc
->ch
));
524 reg_write(vc
->dev
, VIDEO_CONTROL1
, val
);
527 * Adjust format after V4L2_STD_525_60/V4L2_STD_625_50 change,
528 * calling g_fmt and s_fmt will sanitize the height
529 * according to the standard.
531 ret
= tw686x_g_fmt_vid_cap(file
, priv
, &f
);
533 tw686x_s_fmt_vid_cap(file
, priv
, &f
);
537 static int tw686x_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std
)
539 struct tw686x_video_channel
*vc
= video_drvdata(file
);
540 struct tw686x_dev
*dev
= vc
->dev
;
541 unsigned int old_std
, detected_std
= 0;
544 if (vb2_is_streaming(&vc
->vidq
))
547 /* Enable and start standard detection */
548 old_std
= reg_read(dev
, SDT
[vc
->ch
]);
549 reg_write(dev
, SDT
[vc
->ch
], 0x7);
550 reg_write(dev
, SDT_EN
[vc
->ch
], 0xff);
552 end
= jiffies
+ msecs_to_jiffies(500);
553 while (time_is_after_jiffies(end
)) {
555 detected_std
= reg_read(dev
, SDT
[vc
->ch
]);
556 if (!(detected_std
& BIT(7)))
560 reg_write(dev
, SDT
[vc
->ch
], old_std
);
562 /* Exit if still busy */
563 if (detected_std
& BIT(7))
566 detected_std
= (detected_std
>> 4) & 0x7;
567 switch (detected_std
) {
568 case TW686X_STD_NTSC_M
:
569 *std
&= V4L2_STD_NTSC
;
571 case TW686X_STD_NTSC_443
:
572 *std
&= V4L2_STD_NTSC_443
;
574 case TW686X_STD_PAL_M
:
575 *std
&= V4L2_STD_PAL_M
;
577 case TW686X_STD_PAL_60
:
578 *std
&= V4L2_STD_PAL_60
;
581 *std
&= V4L2_STD_PAL
;
583 case TW686X_STD_PAL_CN
:
584 *std
&= V4L2_STD_PAL_Nc
;
586 case TW686X_STD_SECAM
:
587 *std
&= V4L2_STD_SECAM
;
595 static int tw686x_g_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
597 struct tw686x_video_channel
*vc
= video_drvdata(file
);
599 *id
= vc
->video_standard
;
603 static int tw686x_enum_fmt_vid_cap(struct file
*file
, void *priv
,
604 struct v4l2_fmtdesc
*f
)
606 if (f
->index
>= ARRAY_SIZE(formats
))
608 f
->pixelformat
= formats
[f
->index
].fourcc
;
612 static int tw686x_s_input(struct file
*file
, void *priv
, unsigned int i
)
614 struct tw686x_video_channel
*vc
= video_drvdata(file
);
617 if (i
>= TW686X_INPUTS_PER_CH
)
622 * Not sure we are able to support on the fly input change
624 if (vb2_is_busy(&vc
->vidq
))
629 val
= reg_read(vc
->dev
, VDMA_CHANNEL_CONFIG
[vc
->ch
]);
632 reg_write(vc
->dev
, VDMA_CHANNEL_CONFIG
[vc
->ch
], val
);
636 static int tw686x_g_input(struct file
*file
, void *priv
, unsigned int *i
)
638 struct tw686x_video_channel
*vc
= video_drvdata(file
);
644 static int tw686x_enum_input(struct file
*file
, void *priv
,
645 struct v4l2_input
*i
)
647 struct tw686x_video_channel
*vc
= video_drvdata(file
);
648 unsigned int vidstat
;
650 if (i
->index
>= TW686X_INPUTS_PER_CH
)
653 snprintf(i
->name
, sizeof(i
->name
), "Composite%d", i
->index
);
654 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
655 i
->std
= vc
->device
->tvnorms
;
656 i
->capabilities
= V4L2_IN_CAP_STD
;
658 vidstat
= reg_read(vc
->dev
, VIDSTAT
[vc
->ch
]);
660 if (vidstat
& TW686X_VIDSTAT_VDLOSS
)
661 i
->status
|= V4L2_IN_ST_NO_SIGNAL
;
662 if (!(vidstat
& TW686X_VIDSTAT_HLOCK
))
663 i
->status
|= V4L2_IN_ST_NO_H_LOCK
;
668 const struct v4l2_file_operations tw686x_video_fops
= {
669 .owner
= THIS_MODULE
,
670 .open
= v4l2_fh_open
,
671 .unlocked_ioctl
= video_ioctl2
,
672 .release
= vb2_fop_release
,
673 .poll
= vb2_fop_poll
,
674 .read
= vb2_fop_read
,
675 .mmap
= vb2_fop_mmap
,
678 const struct v4l2_ioctl_ops tw686x_video_ioctl_ops
= {
679 .vidioc_querycap
= tw686x_querycap
,
680 .vidioc_g_fmt_vid_cap
= tw686x_g_fmt_vid_cap
,
681 .vidioc_s_fmt_vid_cap
= tw686x_s_fmt_vid_cap
,
682 .vidioc_enum_fmt_vid_cap
= tw686x_enum_fmt_vid_cap
,
683 .vidioc_try_fmt_vid_cap
= tw686x_try_fmt_vid_cap
,
685 .vidioc_querystd
= tw686x_querystd
,
686 .vidioc_g_std
= tw686x_g_std
,
687 .vidioc_s_std
= tw686x_s_std
,
689 .vidioc_enum_input
= tw686x_enum_input
,
690 .vidioc_g_input
= tw686x_g_input
,
691 .vidioc_s_input
= tw686x_s_input
,
693 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
694 .vidioc_querybuf
= vb2_ioctl_querybuf
,
695 .vidioc_qbuf
= vb2_ioctl_qbuf
,
696 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
697 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
698 .vidioc_streamon
= vb2_ioctl_streamon
,
699 .vidioc_streamoff
= vb2_ioctl_streamoff
,
700 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
702 .vidioc_log_status
= v4l2_ctrl_log_status
,
703 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
704 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
707 static void tw686x_buffer_copy(struct tw686x_video_channel
*vc
,
708 unsigned int pb
, struct vb2_v4l2_buffer
*vb
)
710 struct tw686x_dma_desc
*desc
= &vc
->dma_descs
[pb
];
711 struct vb2_buffer
*vb2_buf
= &vb
->vb2_buf
;
713 vb
->field
= V4L2_FIELD_INTERLACED
;
714 vb
->sequence
= vc
->sequence
++;
716 memcpy(vb2_plane_vaddr(vb2_buf
, 0), desc
->virt
, desc
->size
);
717 vb2_buf
->timestamp
= ktime_get_ns();
718 vb2_buffer_done(vb2_buf
, VB2_BUF_STATE_DONE
);
721 void tw686x_video_irq(struct tw686x_dev
*dev
, unsigned long requests
,
722 unsigned int pb_status
, unsigned int fifo_status
,
723 unsigned int *reset_ch
)
725 struct tw686x_video_channel
*vc
;
726 struct vb2_v4l2_buffer
*vb
;
730 for_each_set_bit(ch
, &requests
, max_channels(dev
)) {
731 vc
= &dev
->video_channels
[ch
];
734 * This can either be a blue frame (with signal-lost bit set)
735 * or a good frame (with signal-lost bit clear). If we have just
736 * got signal, then this channel needs resetting.
738 if (vc
->no_signal
&& !(fifo_status
& BIT(ch
))) {
739 v4l2_printk(KERN_DEBUG
, &dev
->v4l2_dev
,
740 "video%d: signal recovered\n", vc
->num
);
741 vc
->no_signal
= false;
742 *reset_ch
|= BIT(ch
);
746 vc
->no_signal
= !!(fifo_status
& BIT(ch
));
748 /* Check FIFO errors only if there's signal */
749 if (!vc
->no_signal
) {
750 u32 fifo_ov
, fifo_bad
;
752 fifo_ov
= (fifo_status
>> 24) & BIT(ch
);
753 fifo_bad
= (fifo_status
>> 16) & BIT(ch
);
754 if (fifo_ov
|| fifo_bad
) {
755 /* Mark this channel for reset */
756 v4l2_printk(KERN_DEBUG
, &dev
->v4l2_dev
,
757 "video%d: FIFO error\n", vc
->num
);
758 *reset_ch
|= BIT(ch
);
764 pb
= !!(pb_status
& BIT(ch
));
766 /* Mark this channel for reset */
767 v4l2_printk(KERN_DEBUG
, &dev
->v4l2_dev
,
768 "video%d: unexpected p-b buffer!\n",
770 *reset_ch
|= BIT(ch
);
775 /* handle video stream */
776 spin_lock_irqsave(&vc
->qlock
, flags
);
777 if (vc
->curr_bufs
[pb
]) {
778 vb
= &vc
->curr_bufs
[pb
]->vb
;
779 tw686x_buffer_copy(vc
, pb
, vb
);
782 tw686x_buffer_refill(vc
, pb
);
783 spin_unlock_irqrestore(&vc
->qlock
, flags
);
787 void tw686x_video_free(struct tw686x_dev
*dev
)
791 for (ch
= 0; ch
< max_channels(dev
); ch
++) {
792 struct tw686x_video_channel
*vc
= &dev
->video_channels
[ch
];
795 video_unregister_device(vc
->device
);
797 for (pb
= 0; pb
< 2; pb
++)
798 tw686x_free_dma(vc
, pb
);
802 int tw686x_video_init(struct tw686x_dev
*dev
)
804 unsigned int ch
, val
, pb
;
807 err
= v4l2_device_register(&dev
->pci_dev
->dev
, &dev
->v4l2_dev
);
811 for (ch
= 0; ch
< max_channels(dev
); ch
++) {
812 struct tw686x_video_channel
*vc
= &dev
->video_channels
[ch
];
813 struct video_device
*vdev
;
815 mutex_init(&vc
->vb_mutex
);
816 spin_lock_init(&vc
->qlock
);
817 INIT_LIST_HEAD(&vc
->vidq_queued
);
822 /* default settings */
823 vc
->format
= &formats
[0];
824 vc
->video_standard
= V4L2_STD_NTSC
;
825 vc
->width
= TW686X_VIDEO_WIDTH
;
826 vc
->height
= TW686X_VIDEO_HEIGHT(vc
->video_standard
);
829 reg_write(vc
->dev
, SDT
[ch
], 0);
830 tw686x_set_framerate(vc
, 30);
832 reg_write(dev
, VDELAY_LO
[ch
], 0x14);
833 reg_write(dev
, HACTIVE_LO
[ch
], 0xd0);
834 reg_write(dev
, VIDEO_SIZE
[ch
], 0);
836 for (pb
= 0; pb
< 2; pb
++) {
837 err
= tw686x_alloc_dma(vc
, pb
);
842 vc
->vidq
.io_modes
= VB2_READ
| VB2_MMAP
| VB2_DMABUF
;
843 vc
->vidq
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
844 vc
->vidq
.drv_priv
= vc
;
845 vc
->vidq
.buf_struct_size
= sizeof(struct tw686x_v4l2_buf
);
846 vc
->vidq
.ops
= &tw686x_video_qops
;
847 vc
->vidq
.mem_ops
= &vb2_vmalloc_memops
;
848 vc
->vidq
.timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
849 vc
->vidq
.min_buffers_needed
= 2;
850 vc
->vidq
.lock
= &vc
->vb_mutex
;
852 err
= vb2_queue_init(&vc
->vidq
);
854 v4l2_err(&dev
->v4l2_dev
,
855 "dma%d: cannot init vb2 queue\n", ch
);
859 err
= v4l2_ctrl_handler_init(&vc
->ctrl_handler
, 4);
861 v4l2_err(&dev
->v4l2_dev
,
862 "dma%d: cannot init ctrl handler\n", ch
);
865 v4l2_ctrl_new_std(&vc
->ctrl_handler
, &ctrl_ops
,
866 V4L2_CID_BRIGHTNESS
, -128, 127, 1, 0);
867 v4l2_ctrl_new_std(&vc
->ctrl_handler
, &ctrl_ops
,
868 V4L2_CID_CONTRAST
, 0, 255, 1, 100);
869 v4l2_ctrl_new_std(&vc
->ctrl_handler
, &ctrl_ops
,
870 V4L2_CID_SATURATION
, 0, 255, 1, 128);
871 v4l2_ctrl_new_std(&vc
->ctrl_handler
, &ctrl_ops
,
872 V4L2_CID_HUE
, -128, 127, 1, 0);
873 err
= vc
->ctrl_handler
.error
;
877 err
= v4l2_ctrl_handler_setup(&vc
->ctrl_handler
);
881 vdev
= video_device_alloc();
883 v4l2_err(&dev
->v4l2_dev
,
884 "dma%d: unable to allocate device\n", ch
);
889 snprintf(vdev
->name
, sizeof(vdev
->name
), "%s video", dev
->name
);
890 vdev
->fops
= &tw686x_video_fops
;
891 vdev
->ioctl_ops
= &tw686x_video_ioctl_ops
;
892 vdev
->release
= video_device_release
;
893 vdev
->v4l2_dev
= &dev
->v4l2_dev
;
894 vdev
->queue
= &vc
->vidq
;
895 vdev
->tvnorms
= V4L2_STD_525_60
| V4L2_STD_625_50
;
897 vdev
->lock
= &vc
->vb_mutex
;
898 vdev
->ctrl_handler
= &vc
->ctrl_handler
;
900 video_set_drvdata(vdev
, vc
);
902 err
= video_register_device(vdev
, VFL_TYPE_GRABBER
, -1);
908 /* Set DMA frame mode on all channels. Only supported mode for now. */
909 val
= TW686X_DEF_PHASE_REF
;
910 for (ch
= 0; ch
< max_channels(dev
); ch
++)
911 val
|= TW686X_FRAME_MODE
<< (16 + ch
* 2);
912 reg_write(dev
, PHASE_REF
, val
);
914 reg_write(dev
, MISC2
[0], 0xe7);
915 reg_write(dev
, VCTRL1
[0], 0xcc);
916 reg_write(dev
, LOOP
[0], 0xa5);
917 if (max_channels(dev
) > 4) {
918 reg_write(dev
, VCTRL1
[1], 0xcc);
919 reg_write(dev
, LOOP
[1], 0xa5);
920 reg_write(dev
, MISC2
[1], 0xe7);
925 tw686x_video_free(dev
);