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-dma-contig.h>
23 #include <media/videobuf2-dma-sg.h>
24 #include <media/videobuf2-vmalloc.h>
26 #include "tw686x-regs.h"
28 #define TW686X_INPUTS_PER_CH 4
29 #define TW686X_VIDEO_WIDTH 720
30 #define TW686X_VIDEO_HEIGHT(id) ((id & V4L2_STD_525_60) ? 480 : 576)
32 #define TW686X_MAX_SG_ENTRY_SIZE 4096
33 #define TW686X_MAX_SG_DESC_COUNT 256 /* PAL 720x576 needs 203 4-KB pages */
34 #define TW686X_SG_TABLE_SIZE (TW686X_MAX_SG_DESC_COUNT * sizeof(struct tw686x_sg_desc))
36 static const struct tw686x_format formats
[] = {
38 .fourcc
= V4L2_PIX_FMT_UYVY
,
42 .fourcc
= V4L2_PIX_FMT_RGB565
,
46 .fourcc
= V4L2_PIX_FMT_YUYV
,
52 static void tw686x_buf_done(struct tw686x_video_channel
*vc
,
55 struct tw686x_dma_desc
*desc
= &vc
->dma_descs
[pb
];
56 struct tw686x_dev
*dev
= vc
->dev
;
57 struct vb2_v4l2_buffer
*vb
;
58 struct vb2_buffer
*vb2_buf
;
60 if (vc
->curr_bufs
[pb
]) {
61 vb
= &vc
->curr_bufs
[pb
]->vb
;
63 vb
->field
= dev
->dma_ops
->field
;
64 vb
->sequence
= vc
->sequence
++;
65 vb2_buf
= &vb
->vb2_buf
;
67 if (dev
->dma_mode
== TW686X_DMA_MODE_MEMCPY
)
68 memcpy(vb2_plane_vaddr(vb2_buf
, 0), desc
->virt
,
70 vb2_buf
->timestamp
= ktime_get_ns();
71 vb2_buffer_done(vb2_buf
, VB2_BUF_STATE_DONE
);
78 * We can call this even when alloc_dma failed for the given channel
80 static void tw686x_memcpy_dma_free(struct tw686x_video_channel
*vc
,
83 struct tw686x_dma_desc
*desc
= &vc
->dma_descs
[pb
];
84 struct tw686x_dev
*dev
= vc
->dev
;
85 struct pci_dev
*pci_dev
;
88 /* Check device presence. Shouldn't really happen! */
89 spin_lock_irqsave(&dev
->lock
, flags
);
90 pci_dev
= dev
->pci_dev
;
91 spin_unlock_irqrestore(&dev
->lock
, flags
);
93 WARN(1, "trying to deallocate on missing device\n");
98 pci_free_consistent(dev
->pci_dev
, desc
->size
,
99 desc
->virt
, desc
->phys
);
104 static int tw686x_memcpy_dma_alloc(struct tw686x_video_channel
*vc
,
107 struct tw686x_dev
*dev
= vc
->dev
;
108 u32 reg
= pb
? VDMA_B_ADDR
[vc
->ch
] : VDMA_P_ADDR
[vc
->ch
];
112 WARN(vc
->dma_descs
[pb
].virt
,
113 "Allocating buffer but previous still here\n");
115 len
= (vc
->width
* vc
->height
* vc
->format
->depth
) >> 3;
116 virt
= pci_alloc_consistent(dev
->pci_dev
, len
,
117 &vc
->dma_descs
[pb
].phys
);
119 v4l2_err(&dev
->v4l2_dev
,
120 "dma%d: unable to allocate %s-buffer\n",
121 vc
->ch
, pb
? "B" : "P");
124 vc
->dma_descs
[pb
].size
= len
;
125 vc
->dma_descs
[pb
].virt
= virt
;
126 reg_write(dev
, reg
, vc
->dma_descs
[pb
].phys
);
131 static void tw686x_memcpy_buf_refill(struct tw686x_video_channel
*vc
,
134 struct tw686x_v4l2_buf
*buf
;
136 while (!list_empty(&vc
->vidq_queued
)) {
138 buf
= list_first_entry(&vc
->vidq_queued
,
139 struct tw686x_v4l2_buf
, list
);
140 list_del(&buf
->list
);
142 vc
->curr_bufs
[pb
] = buf
;
145 vc
->curr_bufs
[pb
] = NULL
;
148 const struct tw686x_dma_ops memcpy_dma_ops
= {
149 .alloc
= tw686x_memcpy_dma_alloc
,
150 .free
= tw686x_memcpy_dma_free
,
151 .buf_refill
= tw686x_memcpy_buf_refill
,
152 .mem_ops
= &vb2_vmalloc_memops
,
153 .hw_dma_mode
= TW686X_FRAME_MODE
,
154 .field
= V4L2_FIELD_INTERLACED
,
157 static void tw686x_contig_buf_refill(struct tw686x_video_channel
*vc
,
160 struct tw686x_v4l2_buf
*buf
;
162 while (!list_empty(&vc
->vidq_queued
)) {
163 u32 reg
= pb
? VDMA_B_ADDR
[vc
->ch
] : VDMA_P_ADDR
[vc
->ch
];
166 buf
= list_first_entry(&vc
->vidq_queued
,
167 struct tw686x_v4l2_buf
, list
);
168 list_del(&buf
->list
);
170 phys
= vb2_dma_contig_plane_dma_addr(&buf
->vb
.vb2_buf
, 0);
171 reg_write(vc
->dev
, reg
, phys
);
173 buf
->vb
.vb2_buf
.state
= VB2_BUF_STATE_ACTIVE
;
174 vc
->curr_bufs
[pb
] = buf
;
177 vc
->curr_bufs
[pb
] = NULL
;
180 static void tw686x_contig_cleanup(struct tw686x_dev
*dev
)
182 vb2_dma_contig_cleanup_ctx(dev
->alloc_ctx
);
185 static int tw686x_contig_setup(struct tw686x_dev
*dev
)
187 dev
->alloc_ctx
= vb2_dma_contig_init_ctx(&dev
->pci_dev
->dev
);
188 if (IS_ERR(dev
->alloc_ctx
)) {
189 dev_err(&dev
->pci_dev
->dev
, "unable to init DMA context\n");
190 return PTR_ERR(dev
->alloc_ctx
);
195 const struct tw686x_dma_ops contig_dma_ops
= {
196 .setup
= tw686x_contig_setup
,
197 .cleanup
= tw686x_contig_cleanup
,
198 .buf_refill
= tw686x_contig_buf_refill
,
199 .mem_ops
= &vb2_dma_contig_memops
,
200 .hw_dma_mode
= TW686X_FRAME_MODE
,
201 .field
= V4L2_FIELD_INTERLACED
,
204 static int tw686x_sg_desc_fill(struct tw686x_sg_desc
*descs
,
205 struct tw686x_v4l2_buf
*buf
,
206 unsigned int buf_len
)
208 struct sg_table
*vbuf
= vb2_dma_sg_plane_desc(&buf
->vb
.vb2_buf
, 0);
209 unsigned int len
, entry_len
;
210 struct scatterlist
*sg
;
213 /* Clear the scatter-gather table */
214 memset(descs
, 0, TW686X_SG_TABLE_SIZE
);
217 for_each_sg(vbuf
->sgl
, sg
, vbuf
->nents
, i
) {
218 dma_addr_t phys
= sg_dma_address(sg
);
219 len
= sg_dma_len(sg
);
221 while (len
&& buf_len
) {
223 if (count
== TW686X_MAX_SG_DESC_COUNT
)
226 entry_len
= min_t(unsigned int, len
,
227 TW686X_MAX_SG_ENTRY_SIZE
);
228 entry_len
= min_t(unsigned int, entry_len
, buf_len
);
229 descs
[count
].phys
= cpu_to_le32(phys
);
230 descs
[count
++].flags_length
=
231 cpu_to_le32(BIT(30) | entry_len
);
234 buf_len
-= entry_len
;
244 static void tw686x_sg_buf_refill(struct tw686x_video_channel
*vc
,
247 struct tw686x_dev
*dev
= vc
->dev
;
248 struct tw686x_v4l2_buf
*buf
;
250 while (!list_empty(&vc
->vidq_queued
)) {
251 unsigned int buf_len
;
253 buf
= list_first_entry(&vc
->vidq_queued
,
254 struct tw686x_v4l2_buf
, list
);
255 list_del(&buf
->list
);
257 buf_len
= (vc
->width
* vc
->height
* vc
->format
->depth
) >> 3;
258 if (tw686x_sg_desc_fill(vc
->sg_descs
[pb
], buf
, buf_len
)) {
259 v4l2_err(&dev
->v4l2_dev
,
260 "dma%d: unable to fill %s-buffer\n",
261 vc
->ch
, pb
? "B" : "P");
262 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
266 buf
->vb
.vb2_buf
.state
= VB2_BUF_STATE_ACTIVE
;
267 vc
->curr_bufs
[pb
] = buf
;
271 vc
->curr_bufs
[pb
] = NULL
;
274 static void tw686x_sg_dma_free(struct tw686x_video_channel
*vc
,
277 struct tw686x_dma_desc
*desc
= &vc
->dma_descs
[pb
];
278 struct tw686x_dev
*dev
= vc
->dev
;
281 pci_free_consistent(dev
->pci_dev
, desc
->size
,
282 desc
->virt
, desc
->phys
);
286 vc
->sg_descs
[pb
] = NULL
;
289 static int tw686x_sg_dma_alloc(struct tw686x_video_channel
*vc
,
292 struct tw686x_dma_desc
*desc
= &vc
->dma_descs
[pb
];
293 struct tw686x_dev
*dev
= vc
->dev
;
294 u32 reg
= pb
? DMA_PAGE_TABLE1_ADDR
[vc
->ch
] :
295 DMA_PAGE_TABLE0_ADDR
[vc
->ch
];
300 virt
= pci_alloc_consistent(dev
->pci_dev
, desc
->size
,
303 v4l2_err(&dev
->v4l2_dev
,
304 "dma%d: unable to allocate %s-buffer\n",
305 vc
->ch
, pb
? "B" : "P");
309 reg_write(dev
, reg
, desc
->phys
);
311 virt
= dev
->video_channels
[0].dma_descs
[pb
].virt
+
312 vc
->ch
* TW686X_SG_TABLE_SIZE
;
315 vc
->sg_descs
[pb
] = virt
;
319 static void tw686x_sg_cleanup(struct tw686x_dev
*dev
)
321 vb2_dma_sg_cleanup_ctx(dev
->alloc_ctx
);
324 static int tw686x_sg_setup(struct tw686x_dev
*dev
)
326 unsigned int sg_table_size
, pb
, ch
, channels
;
328 dev
->alloc_ctx
= vb2_dma_sg_init_ctx(&dev
->pci_dev
->dev
);
329 if (IS_ERR(dev
->alloc_ctx
)) {
330 dev_err(&dev
->pci_dev
->dev
, "unable to init DMA context\n");
331 return PTR_ERR(dev
->alloc_ctx
);
334 if (is_second_gen(dev
)) {
336 * TW6865/TW6869: each channel needs a pair of
337 * P-B descriptor tables.
339 channels
= max_channels(dev
);
340 sg_table_size
= TW686X_SG_TABLE_SIZE
;
343 * TW6864/TW6868: we need to allocate a pair of
344 * P-B descriptor tables, common for all channels.
345 * Each table will be bigger than 4 KB.
348 sg_table_size
= max_channels(dev
) * TW686X_SG_TABLE_SIZE
;
351 for (ch
= 0; ch
< channels
; ch
++) {
352 struct tw686x_video_channel
*vc
= &dev
->video_channels
[ch
];
354 for (pb
= 0; pb
< 2; pb
++)
355 vc
->dma_descs
[pb
].size
= sg_table_size
;
361 const struct tw686x_dma_ops sg_dma_ops
= {
362 .setup
= tw686x_sg_setup
,
363 .cleanup
= tw686x_sg_cleanup
,
364 .alloc
= tw686x_sg_dma_alloc
,
365 .free
= tw686x_sg_dma_free
,
366 .buf_refill
= tw686x_sg_buf_refill
,
367 .mem_ops
= &vb2_dma_sg_memops
,
368 .hw_dma_mode
= TW686X_SG_MODE
,
369 .field
= V4L2_FIELD_SEQ_TB
,
372 static unsigned int tw686x_fields_map(v4l2_std_id std
, unsigned int fps
)
374 static const unsigned int map
[15] = {
375 0x00000000, 0x00000001, 0x00004001, 0x00104001, 0x00404041,
376 0x01041041, 0x01104411, 0x01111111, 0x04444445, 0x04511445,
377 0x05145145, 0x05151515, 0x05515455, 0x05551555, 0x05555555
380 static const unsigned int std_625_50
[26] = {
381 0, 1, 1, 2, 3, 3, 4, 4, 5, 5, 6, 7, 7,
382 8, 8, 9, 10, 10, 11, 11, 12, 13, 13, 14, 14, 0
385 static const unsigned int std_525_60
[31] = {
386 0, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7,
387 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 0, 0
392 if (std
& V4L2_STD_525_60
) {
393 if (fps
>= ARRAY_SIZE(std_525_60
))
397 if (fps
>= ARRAY_SIZE(std_625_50
))
405 static void tw686x_set_framerate(struct tw686x_video_channel
*vc
,
413 map
= tw686x_fields_map(vc
->video_standard
, fps
) << 1;
417 reg_write(vc
->dev
, VIDEO_FIELD_CTRL
[vc
->ch
], map
);
421 static const struct tw686x_format
*format_by_fourcc(unsigned int fourcc
)
425 for (cnt
= 0; cnt
< ARRAY_SIZE(formats
); cnt
++)
426 if (formats
[cnt
].fourcc
== fourcc
)
427 return &formats
[cnt
];
431 static int tw686x_queue_setup(struct vb2_queue
*vq
,
432 unsigned int *nbuffers
, unsigned int *nplanes
,
433 unsigned int sizes
[], void *alloc_ctxs
[])
435 struct tw686x_video_channel
*vc
= vb2_get_drv_priv(vq
);
436 unsigned int szimage
=
437 (vc
->width
* vc
->height
* vc
->format
->depth
) >> 3;
440 * Let's request at least three buffers: two for the
441 * DMA engine and one for userspace.
443 if (vq
->num_buffers
+ *nbuffers
< 3)
444 *nbuffers
= 3 - vq
->num_buffers
;
447 if (*nplanes
!= 1 || sizes
[0] < szimage
)
452 alloc_ctxs
[0] = vc
->dev
->alloc_ctx
;
458 static void tw686x_buf_queue(struct vb2_buffer
*vb
)
460 struct tw686x_video_channel
*vc
= vb2_get_drv_priv(vb
->vb2_queue
);
461 struct tw686x_dev
*dev
= vc
->dev
;
462 struct pci_dev
*pci_dev
;
464 struct vb2_v4l2_buffer
*vbuf
= to_vb2_v4l2_buffer(vb
);
465 struct tw686x_v4l2_buf
*buf
=
466 container_of(vbuf
, struct tw686x_v4l2_buf
, vb
);
468 /* Check device presence */
469 spin_lock_irqsave(&dev
->lock
, flags
);
470 pci_dev
= dev
->pci_dev
;
471 spin_unlock_irqrestore(&dev
->lock
, flags
);
473 vb2_buffer_done(&buf
->vb
.vb2_buf
, VB2_BUF_STATE_ERROR
);
477 spin_lock_irqsave(&vc
->qlock
, flags
);
478 list_add_tail(&buf
->list
, &vc
->vidq_queued
);
479 spin_unlock_irqrestore(&vc
->qlock
, flags
);
482 static void tw686x_clear_queue(struct tw686x_video_channel
*vc
,
483 enum vb2_buffer_state state
)
487 while (!list_empty(&vc
->vidq_queued
)) {
488 struct tw686x_v4l2_buf
*buf
;
490 buf
= list_first_entry(&vc
->vidq_queued
,
491 struct tw686x_v4l2_buf
, list
);
492 list_del(&buf
->list
);
493 vb2_buffer_done(&buf
->vb
.vb2_buf
, state
);
496 for (pb
= 0; pb
< 2; pb
++) {
497 if (vc
->curr_bufs
[pb
])
498 vb2_buffer_done(&vc
->curr_bufs
[pb
]->vb
.vb2_buf
, state
);
499 vc
->curr_bufs
[pb
] = NULL
;
503 static int tw686x_start_streaming(struct vb2_queue
*vq
, unsigned int count
)
505 struct tw686x_video_channel
*vc
= vb2_get_drv_priv(vq
);
506 struct tw686x_dev
*dev
= vc
->dev
;
507 struct pci_dev
*pci_dev
;
511 /* Check device presence */
512 spin_lock_irqsave(&dev
->lock
, flags
);
513 pci_dev
= dev
->pci_dev
;
514 spin_unlock_irqrestore(&dev
->lock
, flags
);
517 goto err_clear_queue
;
520 spin_lock_irqsave(&vc
->qlock
, flags
);
523 if (dev
->dma_mode
== TW686X_DMA_MODE_MEMCPY
&&
524 (!vc
->dma_descs
[0].virt
|| !vc
->dma_descs
[1].virt
)) {
525 spin_unlock_irqrestore(&vc
->qlock
, flags
);
526 v4l2_err(&dev
->v4l2_dev
,
527 "video%d: refusing to start without DMA buffers\n",
530 goto err_clear_queue
;
533 for (pb
= 0; pb
< 2; pb
++)
534 dev
->dma_ops
->buf_refill(vc
, pb
);
535 spin_unlock_irqrestore(&vc
->qlock
, flags
);
540 spin_lock_irqsave(&dev
->lock
, flags
);
541 tw686x_enable_channel(dev
, vc
->ch
);
542 spin_unlock_irqrestore(&dev
->lock
, flags
);
544 mod_timer(&dev
->dma_delay_timer
, jiffies
+ msecs_to_jiffies(100));
549 spin_lock_irqsave(&vc
->qlock
, flags
);
550 tw686x_clear_queue(vc
, VB2_BUF_STATE_QUEUED
);
551 spin_unlock_irqrestore(&vc
->qlock
, flags
);
555 static void tw686x_stop_streaming(struct vb2_queue
*vq
)
557 struct tw686x_video_channel
*vc
= vb2_get_drv_priv(vq
);
558 struct tw686x_dev
*dev
= vc
->dev
;
559 struct pci_dev
*pci_dev
;
562 /* Check device presence */
563 spin_lock_irqsave(&dev
->lock
, flags
);
564 pci_dev
= dev
->pci_dev
;
565 spin_unlock_irqrestore(&dev
->lock
, flags
);
567 tw686x_disable_channel(dev
, vc
->ch
);
569 spin_lock_irqsave(&vc
->qlock
, flags
);
570 tw686x_clear_queue(vc
, VB2_BUF_STATE_ERROR
);
571 spin_unlock_irqrestore(&vc
->qlock
, flags
);
574 static int tw686x_buf_prepare(struct vb2_buffer
*vb
)
576 struct tw686x_video_channel
*vc
= vb2_get_drv_priv(vb
->vb2_queue
);
578 (vc
->width
* vc
->height
* vc
->format
->depth
) >> 3;
580 if (vb2_plane_size(vb
, 0) < size
)
582 vb2_set_plane_payload(vb
, 0, size
);
586 static struct vb2_ops tw686x_video_qops
= {
587 .queue_setup
= tw686x_queue_setup
,
588 .buf_queue
= tw686x_buf_queue
,
589 .buf_prepare
= tw686x_buf_prepare
,
590 .start_streaming
= tw686x_start_streaming
,
591 .stop_streaming
= tw686x_stop_streaming
,
592 .wait_prepare
= vb2_ops_wait_prepare
,
593 .wait_finish
= vb2_ops_wait_finish
,
596 static int tw686x_s_ctrl(struct v4l2_ctrl
*ctrl
)
598 struct tw686x_video_channel
*vc
;
599 struct tw686x_dev
*dev
;
602 vc
= container_of(ctrl
->handler
, struct tw686x_video_channel
,
608 case V4L2_CID_BRIGHTNESS
:
609 reg_write(dev
, BRIGHT
[ch
], ctrl
->val
& 0xff);
612 case V4L2_CID_CONTRAST
:
613 reg_write(dev
, CONTRAST
[ch
], ctrl
->val
);
616 case V4L2_CID_SATURATION
:
617 reg_write(dev
, SAT_U
[ch
], ctrl
->val
);
618 reg_write(dev
, SAT_V
[ch
], ctrl
->val
);
622 reg_write(dev
, HUE
[ch
], ctrl
->val
& 0xff);
629 static const struct v4l2_ctrl_ops ctrl_ops
= {
630 .s_ctrl
= tw686x_s_ctrl
,
633 static int tw686x_g_fmt_vid_cap(struct file
*file
, void *priv
,
634 struct v4l2_format
*f
)
636 struct tw686x_video_channel
*vc
= video_drvdata(file
);
637 struct tw686x_dev
*dev
= vc
->dev
;
639 f
->fmt
.pix
.width
= vc
->width
;
640 f
->fmt
.pix
.height
= vc
->height
;
641 f
->fmt
.pix
.field
= dev
->dma_ops
->field
;
642 f
->fmt
.pix
.pixelformat
= vc
->format
->fourcc
;
643 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
644 f
->fmt
.pix
.bytesperline
= (f
->fmt
.pix
.width
* vc
->format
->depth
) / 8;
645 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
649 static int tw686x_try_fmt_vid_cap(struct file
*file
, void *priv
,
650 struct v4l2_format
*f
)
652 struct tw686x_video_channel
*vc
= video_drvdata(file
);
653 struct tw686x_dev
*dev
= vc
->dev
;
654 unsigned int video_height
= TW686X_VIDEO_HEIGHT(vc
->video_standard
);
655 const struct tw686x_format
*format
;
657 format
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
659 format
= &formats
[0];
660 f
->fmt
.pix
.pixelformat
= format
->fourcc
;
663 if (f
->fmt
.pix
.width
<= TW686X_VIDEO_WIDTH
/ 2)
664 f
->fmt
.pix
.width
= TW686X_VIDEO_WIDTH
/ 2;
666 f
->fmt
.pix
.width
= TW686X_VIDEO_WIDTH
;
668 if (f
->fmt
.pix
.height
<= video_height
/ 2)
669 f
->fmt
.pix
.height
= video_height
/ 2;
671 f
->fmt
.pix
.height
= video_height
;
673 f
->fmt
.pix
.bytesperline
= (f
->fmt
.pix
.width
* format
->depth
) / 8;
674 f
->fmt
.pix
.sizeimage
= f
->fmt
.pix
.height
* f
->fmt
.pix
.bytesperline
;
675 f
->fmt
.pix
.colorspace
= V4L2_COLORSPACE_SMPTE170M
;
676 f
->fmt
.pix
.field
= dev
->dma_ops
->field
;
681 static int tw686x_s_fmt_vid_cap(struct file
*file
, void *priv
,
682 struct v4l2_format
*f
)
684 struct tw686x_video_channel
*vc
= video_drvdata(file
);
685 struct tw686x_dev
*dev
= vc
->dev
;
686 u32 val
, width
, line_width
, height
;
687 unsigned long bitsperframe
;
690 if (vb2_is_busy(&vc
->vidq
))
693 bitsperframe
= vc
->width
* vc
->height
* vc
->format
->depth
;
694 err
= tw686x_try_fmt_vid_cap(file
, priv
, f
);
698 vc
->format
= format_by_fourcc(f
->fmt
.pix
.pixelformat
);
699 vc
->width
= f
->fmt
.pix
.width
;
700 vc
->height
= f
->fmt
.pix
.height
;
702 /* We need new DMA buffers if the framesize has changed */
703 if (dev
->dma_ops
->alloc
&&
704 bitsperframe
!= vc
->width
* vc
->height
* vc
->format
->depth
) {
705 for (pb
= 0; pb
< 2; pb
++)
706 dev
->dma_ops
->free(vc
, pb
);
708 for (pb
= 0; pb
< 2; pb
++) {
709 err
= dev
->dma_ops
->alloc(vc
, pb
);
712 dev
->dma_ops
->free(vc
, 0);
718 val
= reg_read(vc
->dev
, VDMA_CHANNEL_CONFIG
[vc
->ch
]);
720 if (vc
->width
<= TW686X_VIDEO_WIDTH
/ 2)
725 if (vc
->height
<= TW686X_VIDEO_HEIGHT(vc
->video_standard
) / 2)
732 /* Program the DMA scatter-gather */
733 if (dev
->dma_mode
== TW686X_DMA_MODE_SG
) {
734 u32 start_idx
, end_idx
;
736 start_idx
= is_second_gen(dev
) ?
737 0 : vc
->ch
* TW686X_MAX_SG_DESC_COUNT
;
738 end_idx
= start_idx
+ TW686X_MAX_SG_DESC_COUNT
- 1;
740 val
|= (end_idx
<< 10) | start_idx
;
744 val
|= vc
->format
->mode
<< 20;
745 reg_write(vc
->dev
, VDMA_CHANNEL_CONFIG
[vc
->ch
], val
);
747 /* Program the DMA frame size */
748 width
= (vc
->width
* 2) & 0x7ff;
749 height
= vc
->height
/ 2;
750 line_width
= (vc
->width
* 2) & 0x7ff;
751 val
= (height
<< 22) | (line_width
<< 11) | width
;
752 reg_write(vc
->dev
, VDMA_WHP
[vc
->ch
], val
);
756 static int tw686x_querycap(struct file
*file
, void *priv
,
757 struct v4l2_capability
*cap
)
759 struct tw686x_video_channel
*vc
= video_drvdata(file
);
760 struct tw686x_dev
*dev
= vc
->dev
;
762 strlcpy(cap
->driver
, "tw686x", sizeof(cap
->driver
));
763 strlcpy(cap
->card
, dev
->name
, sizeof(cap
->card
));
764 snprintf(cap
->bus_info
, sizeof(cap
->bus_info
),
765 "PCI:%s", pci_name(dev
->pci_dev
));
766 cap
->device_caps
= V4L2_CAP_VIDEO_CAPTURE
| V4L2_CAP_STREAMING
|
768 cap
->capabilities
= cap
->device_caps
| V4L2_CAP_DEVICE_CAPS
;
772 static int tw686x_s_std(struct file
*file
, void *priv
, v4l2_std_id id
)
774 struct tw686x_video_channel
*vc
= video_drvdata(file
);
775 struct v4l2_format f
;
778 if (vc
->video_standard
== id
)
781 if (vb2_is_busy(&vc
->vidq
))
784 if (id
& V4L2_STD_NTSC
)
786 else if (id
& V4L2_STD_PAL
)
788 else if (id
& V4L2_STD_SECAM
)
790 else if (id
& V4L2_STD_NTSC_443
)
792 else if (id
& V4L2_STD_PAL_M
)
794 else if (id
& V4L2_STD_PAL_Nc
)
796 else if (id
& V4L2_STD_PAL_60
)
801 vc
->video_standard
= id
;
802 reg_write(vc
->dev
, SDT
[vc
->ch
], val
);
804 val
= reg_read(vc
->dev
, VIDEO_CONTROL1
);
805 if (id
& V4L2_STD_525_60
)
806 val
&= ~(1 << (SYS_MODE_DMA_SHIFT
+ vc
->ch
));
808 val
|= (1 << (SYS_MODE_DMA_SHIFT
+ vc
->ch
));
809 reg_write(vc
->dev
, VIDEO_CONTROL1
, val
);
812 * Adjust format after V4L2_STD_525_60/V4L2_STD_625_50 change,
813 * calling g_fmt and s_fmt will sanitize the height
814 * according to the standard.
816 ret
= tw686x_g_fmt_vid_cap(file
, priv
, &f
);
818 tw686x_s_fmt_vid_cap(file
, priv
, &f
);
822 static int tw686x_querystd(struct file
*file
, void *priv
, v4l2_std_id
*std
)
824 struct tw686x_video_channel
*vc
= video_drvdata(file
);
825 struct tw686x_dev
*dev
= vc
->dev
;
826 unsigned int old_std
, detected_std
= 0;
829 if (vb2_is_streaming(&vc
->vidq
))
832 /* Enable and start standard detection */
833 old_std
= reg_read(dev
, SDT
[vc
->ch
]);
834 reg_write(dev
, SDT
[vc
->ch
], 0x7);
835 reg_write(dev
, SDT_EN
[vc
->ch
], 0xff);
837 end
= jiffies
+ msecs_to_jiffies(500);
838 while (time_is_after_jiffies(end
)) {
840 detected_std
= reg_read(dev
, SDT
[vc
->ch
]);
841 if (!(detected_std
& BIT(7)))
845 reg_write(dev
, SDT
[vc
->ch
], old_std
);
847 /* Exit if still busy */
848 if (detected_std
& BIT(7))
851 detected_std
= (detected_std
>> 4) & 0x7;
852 switch (detected_std
) {
853 case TW686X_STD_NTSC_M
:
854 *std
&= V4L2_STD_NTSC
;
856 case TW686X_STD_NTSC_443
:
857 *std
&= V4L2_STD_NTSC_443
;
859 case TW686X_STD_PAL_M
:
860 *std
&= V4L2_STD_PAL_M
;
862 case TW686X_STD_PAL_60
:
863 *std
&= V4L2_STD_PAL_60
;
866 *std
&= V4L2_STD_PAL
;
868 case TW686X_STD_PAL_CN
:
869 *std
&= V4L2_STD_PAL_Nc
;
871 case TW686X_STD_SECAM
:
872 *std
&= V4L2_STD_SECAM
;
880 static int tw686x_g_std(struct file
*file
, void *priv
, v4l2_std_id
*id
)
882 struct tw686x_video_channel
*vc
= video_drvdata(file
);
884 *id
= vc
->video_standard
;
888 static int tw686x_enum_fmt_vid_cap(struct file
*file
, void *priv
,
889 struct v4l2_fmtdesc
*f
)
891 if (f
->index
>= ARRAY_SIZE(formats
))
893 f
->pixelformat
= formats
[f
->index
].fourcc
;
897 static int tw686x_s_input(struct file
*file
, void *priv
, unsigned int i
)
899 struct tw686x_video_channel
*vc
= video_drvdata(file
);
902 if (i
>= TW686X_INPUTS_PER_CH
)
907 * Not sure we are able to support on the fly input change
909 if (vb2_is_busy(&vc
->vidq
))
914 val
= reg_read(vc
->dev
, VDMA_CHANNEL_CONFIG
[vc
->ch
]);
917 reg_write(vc
->dev
, VDMA_CHANNEL_CONFIG
[vc
->ch
], val
);
921 static int tw686x_g_input(struct file
*file
, void *priv
, unsigned int *i
)
923 struct tw686x_video_channel
*vc
= video_drvdata(file
);
929 static int tw686x_enum_input(struct file
*file
, void *priv
,
930 struct v4l2_input
*i
)
932 struct tw686x_video_channel
*vc
= video_drvdata(file
);
933 unsigned int vidstat
;
935 if (i
->index
>= TW686X_INPUTS_PER_CH
)
938 snprintf(i
->name
, sizeof(i
->name
), "Composite%d", i
->index
);
939 i
->type
= V4L2_INPUT_TYPE_CAMERA
;
940 i
->std
= vc
->device
->tvnorms
;
941 i
->capabilities
= V4L2_IN_CAP_STD
;
943 vidstat
= reg_read(vc
->dev
, VIDSTAT
[vc
->ch
]);
945 if (vidstat
& TW686X_VIDSTAT_VDLOSS
)
946 i
->status
|= V4L2_IN_ST_NO_SIGNAL
;
947 if (!(vidstat
& TW686X_VIDSTAT_HLOCK
))
948 i
->status
|= V4L2_IN_ST_NO_H_LOCK
;
953 static const struct v4l2_file_operations tw686x_video_fops
= {
954 .owner
= THIS_MODULE
,
955 .open
= v4l2_fh_open
,
956 .unlocked_ioctl
= video_ioctl2
,
957 .release
= vb2_fop_release
,
958 .poll
= vb2_fop_poll
,
959 .read
= vb2_fop_read
,
960 .mmap
= vb2_fop_mmap
,
963 static const struct v4l2_ioctl_ops tw686x_video_ioctl_ops
= {
964 .vidioc_querycap
= tw686x_querycap
,
965 .vidioc_g_fmt_vid_cap
= tw686x_g_fmt_vid_cap
,
966 .vidioc_s_fmt_vid_cap
= tw686x_s_fmt_vid_cap
,
967 .vidioc_enum_fmt_vid_cap
= tw686x_enum_fmt_vid_cap
,
968 .vidioc_try_fmt_vid_cap
= tw686x_try_fmt_vid_cap
,
970 .vidioc_querystd
= tw686x_querystd
,
971 .vidioc_g_std
= tw686x_g_std
,
972 .vidioc_s_std
= tw686x_s_std
,
974 .vidioc_enum_input
= tw686x_enum_input
,
975 .vidioc_g_input
= tw686x_g_input
,
976 .vidioc_s_input
= tw686x_s_input
,
978 .vidioc_reqbufs
= vb2_ioctl_reqbufs
,
979 .vidioc_querybuf
= vb2_ioctl_querybuf
,
980 .vidioc_qbuf
= vb2_ioctl_qbuf
,
981 .vidioc_dqbuf
= vb2_ioctl_dqbuf
,
982 .vidioc_create_bufs
= vb2_ioctl_create_bufs
,
983 .vidioc_streamon
= vb2_ioctl_streamon
,
984 .vidioc_streamoff
= vb2_ioctl_streamoff
,
985 .vidioc_prepare_buf
= vb2_ioctl_prepare_buf
,
987 .vidioc_log_status
= v4l2_ctrl_log_status
,
988 .vidioc_subscribe_event
= v4l2_ctrl_subscribe_event
,
989 .vidioc_unsubscribe_event
= v4l2_event_unsubscribe
,
992 void tw686x_video_irq(struct tw686x_dev
*dev
, unsigned long requests
,
993 unsigned int pb_status
, unsigned int fifo_status
,
994 unsigned int *reset_ch
)
996 struct tw686x_video_channel
*vc
;
1000 for_each_set_bit(ch
, &requests
, max_channels(dev
)) {
1001 vc
= &dev
->video_channels
[ch
];
1004 * This can either be a blue frame (with signal-lost bit set)
1005 * or a good frame (with signal-lost bit clear). If we have just
1006 * got signal, then this channel needs resetting.
1008 if (vc
->no_signal
&& !(fifo_status
& BIT(ch
))) {
1009 v4l2_printk(KERN_DEBUG
, &dev
->v4l2_dev
,
1010 "video%d: signal recovered\n", vc
->num
);
1011 vc
->no_signal
= false;
1012 *reset_ch
|= BIT(ch
);
1016 vc
->no_signal
= !!(fifo_status
& BIT(ch
));
1018 /* Check FIFO errors only if there's signal */
1019 if (!vc
->no_signal
) {
1020 u32 fifo_ov
, fifo_bad
;
1022 fifo_ov
= (fifo_status
>> 24) & BIT(ch
);
1023 fifo_bad
= (fifo_status
>> 16) & BIT(ch
);
1024 if (fifo_ov
|| fifo_bad
) {
1025 /* Mark this channel for reset */
1026 v4l2_printk(KERN_DEBUG
, &dev
->v4l2_dev
,
1027 "video%d: FIFO error\n", vc
->num
);
1028 *reset_ch
|= BIT(ch
);
1034 pb
= !!(pb_status
& BIT(ch
));
1036 /* Mark this channel for reset */
1037 v4l2_printk(KERN_DEBUG
, &dev
->v4l2_dev
,
1038 "video%d: unexpected p-b buffer!\n",
1040 *reset_ch
|= BIT(ch
);
1045 spin_lock_irqsave(&vc
->qlock
, flags
);
1046 tw686x_buf_done(vc
, pb
);
1047 dev
->dma_ops
->buf_refill(vc
, pb
);
1048 spin_unlock_irqrestore(&vc
->qlock
, flags
);
1052 void tw686x_video_free(struct tw686x_dev
*dev
)
1054 unsigned int ch
, pb
;
1056 for (ch
= 0; ch
< max_channels(dev
); ch
++) {
1057 struct tw686x_video_channel
*vc
= &dev
->video_channels
[ch
];
1060 video_unregister_device(vc
->device
);
1062 if (dev
->dma_ops
->free
)
1063 for (pb
= 0; pb
< 2; pb
++)
1064 dev
->dma_ops
->free(vc
, pb
);
1067 if (dev
->dma_ops
->cleanup
)
1068 dev
->dma_ops
->cleanup(dev
);
1071 int tw686x_video_init(struct tw686x_dev
*dev
)
1073 unsigned int ch
, val
, pb
;
1076 if (dev
->dma_mode
== TW686X_DMA_MODE_MEMCPY
)
1077 dev
->dma_ops
= &memcpy_dma_ops
;
1078 else if (dev
->dma_mode
== TW686X_DMA_MODE_CONTIG
)
1079 dev
->dma_ops
= &contig_dma_ops
;
1080 else if (dev
->dma_mode
== TW686X_DMA_MODE_SG
)
1081 dev
->dma_ops
= &sg_dma_ops
;
1085 err
= v4l2_device_register(&dev
->pci_dev
->dev
, &dev
->v4l2_dev
);
1089 if (dev
->dma_ops
->setup
) {
1090 err
= dev
->dma_ops
->setup(dev
);
1095 for (ch
= 0; ch
< max_channels(dev
); ch
++) {
1096 struct tw686x_video_channel
*vc
= &dev
->video_channels
[ch
];
1097 struct video_device
*vdev
;
1099 mutex_init(&vc
->vb_mutex
);
1100 spin_lock_init(&vc
->qlock
);
1101 INIT_LIST_HEAD(&vc
->vidq_queued
);
1106 /* default settings */
1107 vc
->format
= &formats
[0];
1108 vc
->video_standard
= V4L2_STD_NTSC
;
1109 vc
->width
= TW686X_VIDEO_WIDTH
;
1110 vc
->height
= TW686X_VIDEO_HEIGHT(vc
->video_standard
);
1113 reg_write(vc
->dev
, SDT
[ch
], 0);
1114 tw686x_set_framerate(vc
, 30);
1116 reg_write(dev
, VDELAY_LO
[ch
], 0x14);
1117 reg_write(dev
, HACTIVE_LO
[ch
], 0xd0);
1118 reg_write(dev
, VIDEO_SIZE
[ch
], 0);
1120 if (dev
->dma_ops
->alloc
) {
1121 for (pb
= 0; pb
< 2; pb
++) {
1122 err
= dev
->dma_ops
->alloc(vc
, pb
);
1128 vc
->vidq
.io_modes
= VB2_READ
| VB2_MMAP
| VB2_DMABUF
;
1129 vc
->vidq
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
1130 vc
->vidq
.drv_priv
= vc
;
1131 vc
->vidq
.buf_struct_size
= sizeof(struct tw686x_v4l2_buf
);
1132 vc
->vidq
.ops
= &tw686x_video_qops
;
1133 vc
->vidq
.mem_ops
= dev
->dma_ops
->mem_ops
;
1134 vc
->vidq
.timestamp_flags
= V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
;
1135 vc
->vidq
.min_buffers_needed
= 2;
1136 vc
->vidq
.lock
= &vc
->vb_mutex
;
1137 vc
->vidq
.gfp_flags
= GFP_DMA32
;
1139 err
= vb2_queue_init(&vc
->vidq
);
1141 v4l2_err(&dev
->v4l2_dev
,
1142 "dma%d: cannot init vb2 queue\n", ch
);
1146 err
= v4l2_ctrl_handler_init(&vc
->ctrl_handler
, 4);
1148 v4l2_err(&dev
->v4l2_dev
,
1149 "dma%d: cannot init ctrl handler\n", ch
);
1152 v4l2_ctrl_new_std(&vc
->ctrl_handler
, &ctrl_ops
,
1153 V4L2_CID_BRIGHTNESS
, -128, 127, 1, 0);
1154 v4l2_ctrl_new_std(&vc
->ctrl_handler
, &ctrl_ops
,
1155 V4L2_CID_CONTRAST
, 0, 255, 1, 100);
1156 v4l2_ctrl_new_std(&vc
->ctrl_handler
, &ctrl_ops
,
1157 V4L2_CID_SATURATION
, 0, 255, 1, 128);
1158 v4l2_ctrl_new_std(&vc
->ctrl_handler
, &ctrl_ops
,
1159 V4L2_CID_HUE
, -128, 127, 1, 0);
1160 err
= vc
->ctrl_handler
.error
;
1164 err
= v4l2_ctrl_handler_setup(&vc
->ctrl_handler
);
1168 vdev
= video_device_alloc();
1170 v4l2_err(&dev
->v4l2_dev
,
1171 "dma%d: unable to allocate device\n", ch
);
1176 snprintf(vdev
->name
, sizeof(vdev
->name
), "%s video", dev
->name
);
1177 vdev
->fops
= &tw686x_video_fops
;
1178 vdev
->ioctl_ops
= &tw686x_video_ioctl_ops
;
1179 vdev
->release
= video_device_release
;
1180 vdev
->v4l2_dev
= &dev
->v4l2_dev
;
1181 vdev
->queue
= &vc
->vidq
;
1182 vdev
->tvnorms
= V4L2_STD_525_60
| V4L2_STD_625_50
;
1184 vdev
->lock
= &vc
->vb_mutex
;
1185 vdev
->ctrl_handler
= &vc
->ctrl_handler
;
1187 video_set_drvdata(vdev
, vc
);
1189 err
= video_register_device(vdev
, VFL_TYPE_GRABBER
, -1);
1192 vc
->num
= vdev
->num
;
1195 val
= TW686X_DEF_PHASE_REF
;
1196 for (ch
= 0; ch
< max_channels(dev
); ch
++)
1197 val
|= dev
->dma_ops
->hw_dma_mode
<< (16 + ch
* 2);
1198 reg_write(dev
, PHASE_REF
, val
);
1200 reg_write(dev
, MISC2
[0], 0xe7);
1201 reg_write(dev
, VCTRL1
[0], 0xcc);
1202 reg_write(dev
, LOOP
[0], 0xa5);
1203 if (max_channels(dev
) > 4) {
1204 reg_write(dev
, VCTRL1
[1], 0xcc);
1205 reg_write(dev
, LOOP
[1], 0xa5);
1206 reg_write(dev
, MISC2
[1], 0xe7);
1211 tw686x_video_free(dev
);