2 * Copyright (C) 2015 Industrial Research Institute for Automation
3 * and Measurements PIAP
5 * Written by Krzysztof Ha?asa.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of version 2 of the GNU General Public License
9 * as published by the Free Software Foundation.
12 #include <linux/init.h>
13 #include <linux/list.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
17 #include <media/v4l2-common.h>
18 #include <media/v4l2-event.h>
19 #include "tw686x-kh.h"
20 #include "tw686x-kh-regs.h"
22 #define MAX_SG_ENTRY_SIZE (/* 8192 - 128 */ 4096)
23 #define MAX_SG_DESC_COUNT 256 /* PAL 704x576 needs up to 198 4-KB pages */
25 static const struct tw686x_format formats[] = {
27 .name = "4:2:2 packed, UYVY", /* aka Y422 */
28 .fourcc = V4L2_PIX_FMT_UYVY,
33 .name = "4:2:0 packed, YUV",
34 .mode = 1, /* non-standard */
37 .name = "4:1:1 packed, YUV",
38 .mode = 2, /* non-standard */
42 .name = "4:1:1 packed, YUV",
43 .fourcc = V4L2_PIX_FMT_Y41P,
48 .fourcc = V4L2_PIX_FMT_RGB555,
53 .fourcc = V4L2_PIX_FMT_RGB565,
57 .name = "4:2:2 packed, YUYV",
58 .fourcc = V4L2_PIX_FMT_YUYV,
62 /* mode 7 is "reserved" */
65 static const v4l2_std_id video_standards[7] = {
75 static const struct tw686x_format *format_by_fourcc(unsigned int fourcc)
79 for (cnt = 0; cnt < ARRAY_SIZE(formats); cnt++)
80 if (formats[cnt].fourcc == fourcc)
85 static void tw686x_get_format(struct tw686x_video_channel *vc,
86 struct v4l2_format *f)
88 const struct tw686x_format *format;
89 unsigned int width, height, height_div = 1;
91 format = format_by_fourcc(f->fmt.pix.pixelformat);
94 f->fmt.pix.pixelformat = format->fourcc;
98 if (f->fmt.pix.width < width * 3 / 4 /* halfway */)
101 height = (vc->video_standard & V4L2_STD_625_50) ? 576 : 480;
102 if (f->fmt.pix.height < height * 3 / 4 /* halfway */)
105 switch (f->fmt.pix.field) {
107 case V4L2_FIELD_BOTTOM:
110 case V4L2_FIELD_SEQ_BT:
112 f->fmt.pix.field = V4L2_FIELD_BOTTOM;
116 f->fmt.pix.field = V4L2_FIELD_TOP;
118 f->fmt.pix.field = V4L2_FIELD_SEQ_TB;
120 height /= height_div;
122 f->fmt.pix.width = width;
123 f->fmt.pix.height = height;
124 f->fmt.pix.bytesperline = f->fmt.pix.width * format->depth / 8;
125 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
126 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
129 /* video queue operations */
131 static int tw686x_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
132 unsigned int *nplanes, unsigned int sizes[],
135 struct tw686x_video_channel *vc = vb2_get_drv_priv(vq);
136 unsigned int size = vc->width * vc->height * vc->format->depth / 8;
138 alloc_ctxs[0] = vc->alloc_ctx;
143 return sizes[0] < size ? -EINVAL : 0;
146 *nplanes = 1; /* packed formats only */
150 static void tw686x_buf_queue(struct vb2_buffer *vb)
152 struct tw686x_video_channel *vc = vb2_get_drv_priv(vb->vb2_queue);
153 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
154 struct tw686x_vb2_buf *buf;
156 buf = container_of(vbuf, struct tw686x_vb2_buf, vb);
158 spin_lock(&vc->qlock);
159 list_add_tail(&buf->list, &vc->vidq_queued);
160 spin_unlock(&vc->qlock);
163 static void setup_descs(struct tw686x_video_channel *vc, unsigned int n)
166 while (!list_empty(&vc->vidq_queued)) {
167 struct vdma_desc *descs = vc->sg_descs[n];
168 struct tw686x_vb2_buf *buf;
169 struct sg_table *vbuf;
170 struct scatterlist *sg;
171 unsigned int buf_len, count = 0;
174 buf = list_first_entry(&vc->vidq_queued, struct tw686x_vb2_buf,
176 list_del(&buf->list);
178 buf_len = vc->width * vc->height * vc->format->depth / 8;
179 if (vb2_plane_size(&buf->vb.vb2_buf, 0) < buf_len) {
180 pr_err("Video buffer size too small\n");
181 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
182 goto loop; /* try another */
185 vbuf = vb2_dma_sg_plane_desc(&buf->vb.vb2_buf, 0);
186 for_each_sg(vbuf->sgl, sg, vbuf->nents, i) {
187 dma_addr_t phys = sg_dma_address(sg);
188 unsigned int len = sg_dma_len(sg);
190 while (len && buf_len) {
191 unsigned int entry_len = min_t(unsigned int, len,
193 entry_len = min(entry_len, buf_len);
194 if (count == MAX_SG_DESC_COUNT) {
195 pr_err("Video buffer size too fragmented\n");
196 vb2_buffer_done(&buf->vb.vb2_buf,
197 VB2_BUF_STATE_ERROR);
200 descs[count].phys = cpu_to_le32(phys);
201 descs[count++].flags_length =
202 cpu_to_le32(0x40000000 /* available */ |
206 buf_len -= entry_len;
212 /* clear the remaining entries */
213 while (count < MAX_SG_DESC_COUNT) {
214 descs[count].phys = 0;
215 descs[count++].flags_length = 0; /* unavailable */
218 buf->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
219 vc->curr_bufs[n] = buf;
222 vc->curr_bufs[n] = NULL;
225 /* On TW6864 and TW6868, all channels share the pair of video DMA SG tables,
226 with 10-bit start_idx and end_idx determining start and end of frame buffer
227 for particular channel.
228 TW6868 with all its 8 channels would be problematic (only 127 SG entries per
229 channel) but we support only 4 channels on this chip anyway (the first
230 4 channels are driven with internal video decoder, the other 4 would require
231 an external TW286x part).
233 On TW6865 and TW6869, each channel has its own DMA SG table, with indexes
234 starting with 0. Both chips have complete sets of internal video decoders
235 (respectively 4 or 8-channel).
237 All chips have separate SG tables for two video frames. */
239 static void setup_dma_cfg(struct tw686x_video_channel *vc)
241 unsigned int field_width = 704;
242 unsigned int field_height = (vc->video_standard & V4L2_STD_625_50) ?
244 unsigned int start_idx = is_second_gen(vc->dev) ? 0 :
245 vc->ch * MAX_SG_DESC_COUNT;
246 unsigned int end_idx = start_idx + MAX_SG_DESC_COUNT - 1;
247 u32 dma_cfg = (0 << 30) /* input selection */ |
248 (1 << 29) /* field2 dropped (if any) */ |
249 ((vc->height < 300) << 28) /* field dropping */ |
250 (1 << 27) /* master */ |
251 (0 << 25) /* master channel (for slave only) */ |
252 (0 << 24) /* (no) vertical (line) decimation */ |
253 ((vc->width < 400) << 23) /* horizontal decimation */ |
254 (vc->format->mode << 20) /* output video format */ |
255 (end_idx << 10) /* DMA end index */ |
256 start_idx /* DMA start index */;
259 reg_write(vc->dev, VDMA_CHANNEL_CONFIG[vc->ch], dma_cfg);
260 reg_write(vc->dev, VIDEO_SIZE[vc->ch], (1 << 31) | (field_height << 16)
262 reg = reg_read(vc->dev, VIDEO_CONTROL1);
263 if (vc->video_standard & V4L2_STD_625_50)
264 reg |= 1 << (vc->ch + 13);
266 reg &= ~(1 << (vc->ch + 13));
267 reg_write(vc->dev, VIDEO_CONTROL1, reg);
270 static int tw686x_start_streaming(struct vb2_queue *vq, unsigned int count)
272 struct tw686x_video_channel *vc = vb2_get_drv_priv(vq);
273 struct tw686x_dev *dev = vc->dev;
279 /* queue video buffers if available */
280 spin_lock(&vc->qlock);
281 for (n = 0; n < 2; n++)
283 spin_unlock(&vc->qlock);
285 dev->video_active |= 1 << vc->ch;
287 dma_ch_mask = reg_read(dev, DMA_CHANNEL_ENABLE) | (1 << vc->ch);
288 reg_write(dev, DMA_CHANNEL_ENABLE, dma_ch_mask);
289 reg_write(dev, DMA_CMD, (1 << 31) | dma_ch_mask);
293 static void tw686x_stop_streaming(struct vb2_queue *vq)
295 struct tw686x_video_channel *vc = vb2_get_drv_priv(vq);
296 struct tw686x_dev *dev = vc->dev;
297 u32 dma_ch_mask = reg_read(dev, DMA_CHANNEL_ENABLE);
298 u32 dma_cmd = reg_read(dev, DMA_CMD);
301 dma_ch_mask &= ~(1 << vc->ch);
302 reg_write(dev, DMA_CHANNEL_ENABLE, dma_ch_mask);
304 dev->video_active &= ~(1 << vc->ch);
306 dma_cmd &= ~(1 << vc->ch);
307 reg_write(dev, DMA_CMD, dma_cmd);
309 if (!dev->video_active) {
310 reg_write(dev, DMA_CMD, 0);
311 reg_write(dev, DMA_CHANNEL_ENABLE, 0);
314 spin_lock(&vc->qlock);
315 while (!list_empty(&vc->vidq_queued)) {
316 struct tw686x_vb2_buf *buf;
318 buf = list_entry(vc->vidq_queued.next, struct tw686x_vb2_buf,
320 list_del(&buf->list);
321 vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR);
324 for (n = 0; n < 2; n++)
325 if (vc->curr_bufs[n])
326 vb2_buffer_done(&vc->curr_bufs[n]->vb.vb2_buf,
327 VB2_BUF_STATE_ERROR);
329 spin_unlock(&vc->qlock);
332 static struct vb2_ops tw686x_video_qops = {
333 .queue_setup = tw686x_queue_setup,
334 .buf_queue = tw686x_buf_queue,
335 .start_streaming = tw686x_start_streaming,
336 .stop_streaming = tw686x_stop_streaming,
337 .wait_prepare = vb2_ops_wait_prepare,
338 .wait_finish = vb2_ops_wait_finish,
341 static int tw686x_s_ctrl(struct v4l2_ctrl *ctrl)
343 struct tw686x_video_channel *vc;
344 struct tw686x_dev *dev;
347 vc = container_of(ctrl->handler, struct tw686x_video_channel,
353 case V4L2_CID_BRIGHTNESS:
354 reg_write(dev, BRIGHT[ch], ctrl->val & 0xFF);
357 case V4L2_CID_CONTRAST:
358 reg_write(dev, CONTRAST[ch], ctrl->val);
361 case V4L2_CID_SATURATION:
362 reg_write(dev, SAT_U[ch], ctrl->val);
363 reg_write(dev, SAT_V[ch], ctrl->val);
367 reg_write(dev, HUE[ch], ctrl->val & 0xFF);
374 static const struct v4l2_ctrl_ops ctrl_ops = {
375 .s_ctrl = tw686x_s_ctrl,
378 static int tw686x_g_fmt_vid_cap(struct file *file, void *priv,
379 struct v4l2_format *f)
381 struct tw686x_video_channel *vc = video_drvdata(file);
383 f->fmt.pix.width = vc->width;
384 f->fmt.pix.height = vc->height;
385 f->fmt.pix.field = vc->field;
386 f->fmt.pix.pixelformat = vc->format->fourcc;
387 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
388 f->fmt.pix.bytesperline = f->fmt.pix.width * vc->format->depth / 8;
389 f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
393 static int tw686x_try_fmt_vid_cap(struct file *file, void *priv,
394 struct v4l2_format *f)
396 tw686x_get_format(video_drvdata(file), f);
400 static int tw686x_s_fmt_vid_cap(struct file *file, void *priv,
401 struct v4l2_format *f)
403 struct tw686x_video_channel *vc = video_drvdata(file);
405 tw686x_get_format(vc, f);
406 vc->format = format_by_fourcc(f->fmt.pix.pixelformat);
407 vc->field = f->fmt.pix.field;
408 vc->width = f->fmt.pix.width;
409 vc->height = f->fmt.pix.height;
413 static int tw686x_querycap(struct file *file, void *priv,
414 struct v4l2_capability *cap)
416 struct tw686x_video_channel *vc = video_drvdata(file);
417 struct tw686x_dev *dev = vc->dev;
419 strcpy(cap->driver, "tw686x-kh");
420 strcpy(cap->card, dev->name);
421 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci_dev));
422 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
423 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
427 static int tw686x_s_std(struct file *file, void *priv, v4l2_std_id id)
429 struct tw686x_video_channel *vc = video_drvdata(file);
431 u32 sdt = 0; /* default */
433 for (cnt = 0; cnt < ARRAY_SIZE(video_standards); cnt++)
434 if (id & video_standards[cnt]) {
439 reg_write(vc->dev, SDT[vc->ch], sdt);
440 vc->video_standard = video_standards[sdt];
444 static int tw686x_g_std(struct file *file, void *priv, v4l2_std_id *id)
446 struct tw686x_video_channel *vc = video_drvdata(file);
448 *id = vc->video_standard;
452 static int tw686x_enum_fmt_vid_cap(struct file *file, void *priv,
453 struct v4l2_fmtdesc *f)
455 if (f->index >= ARRAY_SIZE(formats))
458 strlcpy(f->description, formats[f->index].name, sizeof(f->description));
459 f->pixelformat = formats[f->index].fourcc;
463 static int tw686x_g_parm(struct file *file, void *priv,
464 struct v4l2_streamparm *sp)
466 struct tw686x_video_channel *vc = video_drvdata(file);
468 if (sp->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
470 memset(&sp->parm.capture, 0, sizeof(sp->parm.capture));
471 sp->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
472 v4l2_video_std_frame_period(vc->video_standard,
473 &sp->parm.capture.timeperframe);
478 static int tw686x_enum_input(struct file *file, void *priv,
479 struct v4l2_input *inp)
481 /* the chip has internal multiplexer, support can be added
482 if the actual hw uses it */
486 snprintf(inp->name, sizeof(inp->name), "Composite");
487 inp->type = V4L2_INPUT_TYPE_CAMERA;
488 inp->std = V4L2_STD_ALL;
489 inp->capabilities = V4L2_IN_CAP_STD;
493 static int tw686x_g_input(struct file *file, void *priv, unsigned int *v)
499 static int tw686x_s_input(struct file *file, void *priv, unsigned int v)
506 const struct v4l2_file_operations tw686x_video_fops = {
507 .owner = THIS_MODULE,
508 .open = v4l2_fh_open,
509 .unlocked_ioctl = video_ioctl2,
510 .release = vb2_fop_release,
511 .poll = vb2_fop_poll,
512 .read = vb2_fop_read,
513 .mmap = vb2_fop_mmap,
516 const struct v4l2_ioctl_ops tw686x_video_ioctl_ops = {
517 .vidioc_querycap = tw686x_querycap,
518 .vidioc_enum_fmt_vid_cap = tw686x_enum_fmt_vid_cap,
519 .vidioc_g_fmt_vid_cap = tw686x_g_fmt_vid_cap,
520 .vidioc_s_fmt_vid_cap = tw686x_s_fmt_vid_cap,
521 .vidioc_try_fmt_vid_cap = tw686x_try_fmt_vid_cap,
522 .vidioc_reqbufs = vb2_ioctl_reqbufs,
523 .vidioc_querybuf = vb2_ioctl_querybuf,
524 .vidioc_qbuf = vb2_ioctl_qbuf,
525 .vidioc_dqbuf = vb2_ioctl_dqbuf,
526 .vidioc_create_bufs = vb2_ioctl_create_bufs,
527 .vidioc_streamon = vb2_ioctl_streamon,
528 .vidioc_streamoff = vb2_ioctl_streamoff,
529 .vidioc_g_std = tw686x_g_std,
530 .vidioc_s_std = tw686x_s_std,
531 .vidioc_g_parm = tw686x_g_parm,
532 .vidioc_enum_input = tw686x_enum_input,
533 .vidioc_g_input = tw686x_g_input,
534 .vidioc_s_input = tw686x_s_input,
535 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
536 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
539 static int video_thread(void *arg)
541 struct tw686x_dev *dev = arg;
542 DECLARE_WAITQUEUE(wait, current);
545 add_wait_queue(&dev->video_thread_wait, &wait);
548 long timeout = schedule_timeout_interruptible(HZ);
551 if (timeout == -ERESTARTSYS || kthread_should_stop())
554 for (ch = 0; ch < max_channels(dev); ch++) {
555 struct tw686x_video_channel *vc;
557 u32 request, n, stat = VB2_BUF_STATE_DONE;
559 vc = &dev->video_channels[ch];
560 if (!(dev->video_active & (1 << ch)))
563 spin_lock_irq(&dev->irq_lock);
564 request = dev->dma_requests & (0x01000001 << ch);
566 dev->dma_requests &= ~request;
567 spin_unlock_irq(&dev->irq_lock);
574 /* handle channel events */
575 if ((request & 0x01000000) |
576 (reg_read(dev, VIDEO_FIFO_STATUS) & (0x01010001 << ch)) |
577 (reg_read(dev, VIDEO_PARSER_STATUS) & (0x00000101 << ch))) {
578 /* DMA Errors - reset channel */
581 spin_lock_irqsave(&dev->irq_lock, flags);
582 reg = reg_read(dev, DMA_CMD);
583 /* Reset DMA channel */
584 reg_write(dev, DMA_CMD, reg & ~(1 << ch));
585 reg_write(dev, DMA_CMD, reg);
586 spin_unlock_irqrestore(&dev->irq_lock, flags);
587 stat = VB2_BUF_STATE_ERROR;
590 /* handle video stream */
591 mutex_lock(&vc->vb_mutex);
592 spin_lock(&vc->qlock);
593 n = !!(reg_read(dev, PB_STATUS) & (1 << ch));
594 if (vc->curr_bufs[n]) {
595 struct vb2_v4l2_buffer *vb;
597 vb = &vc->curr_bufs[n]->vb;
598 vb->vb2_buf.timestamp = ktime_get_ns();
599 vb->field = vc->field;
600 if (V4L2_FIELD_HAS_BOTH(vc->field))
601 vb->sequence = vc->seq++;
603 vb->sequence = (vc->seq++) / 2;
604 vb2_set_plane_payload(&vb->vb2_buf, 0,
605 vc->width * vc->height * vc->format->depth / 8);
606 vb2_buffer_done(&vb->vb2_buf, stat);
609 spin_unlock(&vc->qlock);
610 mutex_unlock(&vc->vb_mutex);
615 remove_wait_queue(&dev->video_thread_wait, &wait);
619 int tw686x_video_irq(struct tw686x_dev *dev)
621 unsigned long flags, handled = 0;
624 spin_lock_irqsave(&dev->irq_lock, flags);
625 requests = dev->dma_requests;
626 spin_unlock_irqrestore(&dev->irq_lock, flags);
628 if (requests & dev->video_active) {
629 wake_up_interruptible_all(&dev->video_thread_wait);
635 void tw686x_video_free(struct tw686x_dev *dev)
639 if (dev->video_thread)
640 kthread_stop(dev->video_thread);
642 for (ch = 0; ch < max_channels(dev); ch++) {
643 struct tw686x_video_channel *vc = &dev->video_channels[ch];
645 v4l2_ctrl_handler_free(&vc->ctrl_handler);
647 video_unregister_device(vc->device);
648 vb2_dma_sg_cleanup_ctx(vc->alloc_ctx);
649 for (n = 0; n < 2; n++) {
650 struct dma_desc *descs = &vc->sg_tables[n];
653 pci_free_consistent(dev->pci_dev, descs->size,
654 descs->virt, descs->phys);
658 v4l2_device_unregister(&dev->v4l2_dev);
661 #define SG_TABLE_SIZE (MAX_SG_DESC_COUNT * sizeof(struct vdma_desc))
663 int tw686x_video_init(struct tw686x_dev *dev)
668 init_waitqueue_head(&dev->video_thread_wait);
670 err = v4l2_device_register(&dev->pci_dev->dev, &dev->v4l2_dev);
674 reg_write(dev, VIDEO_CONTROL1, 0); /* NTSC, disable scaler */
675 reg_write(dev, PHASE_REF, 0x00001518); /* Scatter-gather DMA mode */
677 /* setup required SG table sizes */
678 for (n = 0; n < 2; n++)
679 if (is_second_gen(dev)) {
680 /* TW 6865, TW6869 - each channel needs a pair of
682 for (ch = 0; ch < max_channels(dev); ch++)
683 dev->video_channels[ch].sg_tables[n].size =
687 /* TW 6864, TW6868 - we need to allocate a pair of
688 descriptor tables, common for all channels.
689 Each table will be bigger than 4 KB. */
690 dev->video_channels[0].sg_tables[n].size =
691 max_channels(dev) * SG_TABLE_SIZE;
693 /* allocate SG tables and initialize video channels */
694 for (ch = 0; ch < max_channels(dev); ch++) {
695 struct tw686x_video_channel *vc = &dev->video_channels[ch];
696 struct video_device *vdev;
698 mutex_init(&vc->vb_mutex);
699 spin_lock_init(&vc->qlock);
700 INIT_LIST_HEAD(&vc->vidq_queued);
705 /* default settings: NTSC */
706 vc->format = &formats[0];
707 vc->video_standard = V4L2_STD_NTSC;
708 reg_write(vc->dev, SDT[vc->ch], 0);
709 vc->field = V4L2_FIELD_SEQ_BT;
713 for (n = 0; n < 2; n++) {
716 if (vc->sg_tables[n].size) {
717 unsigned int reg = n ? DMA_PAGE_TABLE1_ADDR[ch] :
718 DMA_PAGE_TABLE0_ADDR[ch];
720 cpu = pci_alloc_consistent(dev->pci_dev,
721 vc->sg_tables[n].size,
722 &vc->sg_tables[n].phys);
724 pr_err("Error allocating video DMA scatter-gather tables\n");
728 vc->sg_tables[n].virt = cpu;
729 reg_write(dev, reg, vc->sg_tables[n].phys);
731 cpu = dev->video_channels[0].sg_tables[n].virt +
734 vc->sg_descs[n] = cpu;
737 reg_write(dev, VCTRL1[0], 0x24);
738 reg_write(dev, LOOP[0], 0xA5);
739 if (max_channels(dev) > 4) {
740 reg_write(dev, VCTRL1[1], 0x24);
741 reg_write(dev, LOOP[1], 0xA5);
743 reg_write(dev, VIDEO_FIELD_CTRL[ch], 0);
744 reg_write(dev, VDELAY_LO[ch], 0x14);
746 vdev = video_device_alloc();
748 pr_warn("Unable to allocate video device\n");
753 vc->alloc_ctx = vb2_dma_sg_init_ctx(&dev->pci_dev->dev);
754 if (IS_ERR(vc->alloc_ctx)) {
755 pr_warn("Unable to initialize DMA scatter-gather context\n");
756 err = PTR_ERR(vc->alloc_ctx);
760 vc->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
761 vc->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
762 vc->vidq.drv_priv = vc;
763 vc->vidq.buf_struct_size = sizeof(struct tw686x_vb2_buf);
764 vc->vidq.ops = &tw686x_video_qops;
765 vc->vidq.mem_ops = &vb2_dma_sg_memops;
766 vc->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
767 vc->vidq.min_buffers_needed = 2;
768 vc->vidq.lock = &vc->vb_mutex;
770 err = vb2_queue_init(&vc->vidq);
774 strcpy(vdev->name, "TW686x-video");
775 snprintf(vdev->name, sizeof(vdev->name), "%s video", dev->name);
776 vdev->fops = &tw686x_video_fops;
777 vdev->ioctl_ops = &tw686x_video_ioctl_ops;
778 vdev->release = video_device_release;
779 vdev->v4l2_dev = &dev->v4l2_dev;
780 vdev->queue = &vc->vidq;
781 vdev->tvnorms = V4L2_STD_ALL;
783 vdev->lock = &vc->vb_mutex;
785 dev->video_channels[ch].device = vdev;
786 video_set_drvdata(vdev, vc);
787 err = video_register_device(vdev, VFL_TYPE_GRABBER, -1);
791 v4l2_ctrl_handler_init(&vc->ctrl_handler,
792 4 /* number of controls */);
793 vdev->ctrl_handler = &vc->ctrl_handler;
794 v4l2_ctrl_new_std(&vc->ctrl_handler, &ctrl_ops,
795 V4L2_CID_BRIGHTNESS, -128, 127, 1, 0);
796 v4l2_ctrl_new_std(&vc->ctrl_handler, &ctrl_ops,
797 V4L2_CID_CONTRAST, 0, 255, 1, 64);
798 v4l2_ctrl_new_std(&vc->ctrl_handler, &ctrl_ops,
799 V4L2_CID_SATURATION, 0, 255, 1, 128);
800 v4l2_ctrl_new_std(&vc->ctrl_handler, &ctrl_ops, V4L2_CID_HUE,
802 err = vc->ctrl_handler.error;
806 v4l2_ctrl_handler_setup(&vc->ctrl_handler);
809 dev->video_thread = kthread_run(video_thread, dev, "tw686x_video");
810 if (IS_ERR(dev->video_thread)) {
811 err = PTR_ERR(dev->video_thread);
818 tw686x_video_free(dev);