3 * device driver for philips saa7134 based TV cards
4 * video4linux video interface
6 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
20 #include "saa7134-reg.h"
22 #include <linux/init.h>
23 #include <linux/list.h>
24 #include <linux/module.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/sort.h>
29 #include <media/v4l2-common.h>
30 #include <media/v4l2-event.h>
31 #include <media/i2c/saa6588.h>
33 /* ------------------------------------------------------------------ */
35 unsigned int video_debug;
36 static unsigned int gbuffers = 8;
37 static unsigned int noninterlaced; /* 0 */
38 static unsigned int gbufsize = 720*576*4;
39 static unsigned int gbufsize_max = 720*576*4;
40 static char secam[] = "--";
41 module_param(video_debug, int, 0644);
42 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
43 module_param(gbuffers, int, 0444);
44 MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
45 module_param(noninterlaced, int, 0644);
46 MODULE_PARM_DESC(noninterlaced,"capture non interlaced video");
47 module_param_string(secam, secam, sizeof(secam), 0644);
48 MODULE_PARM_DESC(secam, "force SECAM variant, either DK,L or Lc");
51 #define video_dbg(fmt, arg...) do { \
52 if (video_debug & 0x04) \
53 printk(KERN_DEBUG pr_fmt("video: " fmt), ## arg); \
56 /* ------------------------------------------------------------------ */
57 /* Defines for Video Output Port Register at address 0x191 */
59 /* Bit 0: VIP code T bit polarity */
61 #define VP_T_CODE_P_NON_INVERTED 0x00
62 #define VP_T_CODE_P_INVERTED 0x01
64 /* ------------------------------------------------------------------ */
65 /* Defines for Video Output Port Register at address 0x195 */
67 /* Bit 2: Video output clock delay control */
69 #define VP_CLK_CTRL2_NOT_DELAYED 0x00
70 #define VP_CLK_CTRL2_DELAYED 0x04
72 /* Bit 1: Video output clock invert control */
74 #define VP_CLK_CTRL1_NON_INVERTED 0x00
75 #define VP_CLK_CTRL1_INVERTED 0x02
77 /* ------------------------------------------------------------------ */
78 /* Defines for Video Output Port Register at address 0x196 */
80 /* Bits 2 to 0: VSYNC pin video vertical sync type */
82 #define VP_VS_TYPE_MASK 0x07
84 #define VP_VS_TYPE_OFF 0x00
85 #define VP_VS_TYPE_V123 0x01
86 #define VP_VS_TYPE_V_ITU 0x02
87 #define VP_VS_TYPE_VGATE_L 0x03
88 #define VP_VS_TYPE_RESERVED1 0x04
89 #define VP_VS_TYPE_RESERVED2 0x05
90 #define VP_VS_TYPE_F_ITU 0x06
91 #define VP_VS_TYPE_SC_FID 0x07
93 /* ------------------------------------------------------------------ */
94 /* data structs for video */
96 static int video_out[][9] = {
97 [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
100 static struct saa7134_format formats[] = {
102 .name = "8 bpp gray",
103 .fourcc = V4L2_PIX_FMT_GREY,
107 .name = "15 bpp RGB, le",
108 .fourcc = V4L2_PIX_FMT_RGB555,
112 .name = "15 bpp RGB, be",
113 .fourcc = V4L2_PIX_FMT_RGB555X,
118 .name = "16 bpp RGB, le",
119 .fourcc = V4L2_PIX_FMT_RGB565,
123 .name = "16 bpp RGB, be",
124 .fourcc = V4L2_PIX_FMT_RGB565X,
129 .name = "24 bpp RGB, le",
130 .fourcc = V4L2_PIX_FMT_BGR24,
134 .name = "24 bpp RGB, be",
135 .fourcc = V4L2_PIX_FMT_RGB24,
140 .name = "32 bpp RGB, le",
141 .fourcc = V4L2_PIX_FMT_BGR32,
145 .name = "32 bpp RGB, be",
146 .fourcc = V4L2_PIX_FMT_RGB32,
152 .name = "4:2:2 packed, YUYV",
153 .fourcc = V4L2_PIX_FMT_YUYV,
159 .name = "4:2:2 packed, UYVY",
160 .fourcc = V4L2_PIX_FMT_UYVY,
165 .name = "4:2:2 planar, Y-Cb-Cr",
166 .fourcc = V4L2_PIX_FMT_YUV422P,
174 .name = "4:2:0 planar, Y-Cb-Cr",
175 .fourcc = V4L2_PIX_FMT_YUV420,
183 .name = "4:2:0 planar, Y-Cb-Cr",
184 .fourcc = V4L2_PIX_FMT_YVU420,
194 #define FORMATS ARRAY_SIZE(formats)
196 #define NORM_625_50 \
199 .video_v_start = 24, \
200 .video_v_stop = 311, \
201 .vbi_v_start_0 = 7, \
202 .vbi_v_stop_0 = 23, \
203 .vbi_v_start_1 = 319, \
206 #define NORM_525_60 \
209 .video_v_start = 23, \
210 .video_v_stop = 262, \
211 .vbi_v_start_0 = 10, \
212 .vbi_v_stop_0 = 21, \
213 .vbi_v_start_1 = 273, \
216 static struct saa7134_tvnorm tvnorms[] = {
218 .name = "PAL", /* autodetect */
222 .sync_control = 0x18,
223 .luma_control = 0x40,
224 .chroma_ctrl1 = 0x81,
226 .chroma_ctrl2 = 0x06,
231 .id = V4L2_STD_PAL_BG,
234 .sync_control = 0x18,
235 .luma_control = 0x40,
236 .chroma_ctrl1 = 0x81,
238 .chroma_ctrl2 = 0x06,
243 .id = V4L2_STD_PAL_I,
246 .sync_control = 0x18,
247 .luma_control = 0x40,
248 .chroma_ctrl1 = 0x81,
250 .chroma_ctrl2 = 0x06,
255 .id = V4L2_STD_PAL_DK,
258 .sync_control = 0x18,
259 .luma_control = 0x40,
260 .chroma_ctrl1 = 0x81,
262 .chroma_ctrl2 = 0x06,
270 .sync_control = 0x59,
271 .luma_control = 0x40,
272 .chroma_ctrl1 = 0x89,
274 .chroma_ctrl2 = 0x0e,
279 .id = V4L2_STD_SECAM,
282 .sync_control = 0x18,
283 .luma_control = 0x1b,
284 .chroma_ctrl1 = 0xd1,
286 .chroma_ctrl2 = 0x00,
291 .id = V4L2_STD_SECAM_DK,
294 .sync_control = 0x18,
295 .luma_control = 0x1b,
296 .chroma_ctrl1 = 0xd1,
298 .chroma_ctrl2 = 0x00,
303 .id = V4L2_STD_SECAM_L,
306 .sync_control = 0x18,
307 .luma_control = 0x1b,
308 .chroma_ctrl1 = 0xd1,
310 .chroma_ctrl2 = 0x00,
315 .id = V4L2_STD_SECAM_LC,
318 .sync_control = 0x18,
319 .luma_control = 0x1b,
320 .chroma_ctrl1 = 0xd1,
322 .chroma_ctrl2 = 0x00,
327 .id = V4L2_STD_PAL_M,
330 .sync_control = 0x59,
331 .luma_control = 0x40,
332 .chroma_ctrl1 = 0xb9,
334 .chroma_ctrl2 = 0x0e,
339 .id = V4L2_STD_PAL_Nc,
342 .sync_control = 0x18,
343 .luma_control = 0x40,
344 .chroma_ctrl1 = 0xa1,
346 .chroma_ctrl2 = 0x06,
351 .id = V4L2_STD_PAL_60,
359 .vbi_v_start_1 = 273,
362 .sync_control = 0x18,
363 .luma_control = 0x40,
364 .chroma_ctrl1 = 0x81,
366 .chroma_ctrl2 = 0x06,
370 #define TVNORMS ARRAY_SIZE(tvnorms)
372 static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
376 for (i = 0; i < FORMATS; i++)
377 if (formats[i].fourcc == fourcc)
382 /* ------------------------------------------------------------------ */
384 static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
386 video_dbg("set tv norm = %s\n", norm->name);
390 dev->crop_bounds.left = norm->h_start;
391 dev->crop_defrect.left = norm->h_start;
392 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
393 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
395 dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
396 dev->crop_defrect.top = norm->video_v_start*2;
397 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
398 - dev->crop_bounds.top;
399 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
401 dev->crop_current = dev->crop_defrect;
403 saa7134_set_tvnorm_hw(dev);
406 static void video_mux(struct saa7134_dev *dev, int input)
408 video_dbg("video input = %d [%s]\n",
409 input, saa7134_input_name[card_in(dev, input).type]);
410 dev->ctl_input = input;
411 set_tvnorm(dev, dev->tvnorm);
412 saa7134_tvaudio_setinput(dev, &card_in(dev, input));
416 static void saa7134_set_decoder(struct saa7134_dev *dev)
418 int luma_control, sync_control, chroma_ctrl1, mux;
420 struct saa7134_tvnorm *norm = dev->tvnorm;
421 mux = card_in(dev, dev->ctl_input).vmux;
423 luma_control = norm->luma_control;
424 sync_control = norm->sync_control;
425 chroma_ctrl1 = norm->chroma_ctrl1;
428 luma_control |= 0x80; /* svideo */
429 if (noninterlaced || dev->nosignal)
430 sync_control |= 0x20;
432 /* switch on auto standard detection */
433 sync_control |= SAA7134_SYNC_CTRL_AUFD;
434 chroma_ctrl1 |= SAA7134_CHROMA_CTRL1_AUTO0;
435 chroma_ctrl1 &= ~SAA7134_CHROMA_CTRL1_FCTC;
436 luma_control &= ~SAA7134_LUMA_CTRL_LDEL;
438 /* setup video decoder */
439 saa_writeb(SAA7134_INCR_DELAY, 0x08);
440 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
441 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
443 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
444 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
445 saa_writeb(SAA7134_HSYNC_START, 0xeb);
446 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
447 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
449 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
450 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
451 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
453 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
454 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
456 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
457 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
459 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
460 saa_writeb(SAA7134_CHROMA_CTRL1, chroma_ctrl1);
461 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
463 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
464 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
466 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
467 saa_writeb(SAA7134_VGATE_START, 0x11);
468 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
469 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
470 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
471 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
474 void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
476 saa7134_set_decoder(dev);
478 saa_call_all(dev, video, s_std, dev->tvnorm->id);
479 /* Set the correct norm for the saa6752hs. This function
480 does nothing if there is no saa6752hs. */
481 saa_call_empress(dev, video, s_std, dev->tvnorm->id);
484 static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
486 static const struct {
493 /* XPSC XACL XC2_1 XDCG VPFY */
505 static const int count = ARRAY_SIZE(vals);
508 for (i = 0; i < count; i++)
509 if (vals[i].xpsc == prescale)
514 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
515 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
516 saa_writeb(SAA7134_LEVEL_CTRL(task),
517 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
518 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
519 (vals[i].vpfy << 2) | vals[i].vpfy);
522 static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
526 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
527 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
529 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
532 video_dbg("yscale LPI yscale=%d\n", yscale);
533 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
534 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
535 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
538 val = 0x40 * 1024 / yscale;
539 video_dbg("yscale ACM yscale=%d val=0x%x\n", yscale, val);
540 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
541 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
542 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
544 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
547 static void set_size(struct saa7134_dev *dev, int task,
548 int width, int height, int interlace)
550 int prescale,xscale,yscale,y_even,y_odd;
551 int h_start, h_stop, v_start, v_stop;
552 int div = interlace ? 2 : 1;
554 /* setup video scaler */
555 h_start = dev->crop_current.left;
556 v_start = dev->crop_current.top/2;
557 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
558 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
560 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
561 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
562 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
563 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
564 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
565 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
566 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
567 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
569 prescale = dev->crop_current.width / width;
572 xscale = 1024 * dev->crop_current.width / prescale / width;
573 yscale = 512 * div * dev->crop_current.height / height;
574 video_dbg("prescale=%d xscale=%d yscale=%d\n",
575 prescale, xscale, yscale);
576 set_h_prescale(dev,task,prescale);
577 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
578 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
579 set_v_scale(dev,task,yscale);
581 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
582 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
583 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
584 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
586 /* deinterlace y offsets */
587 y_odd = dev->ctl_y_odd;
588 y_even = dev->ctl_y_even;
589 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
590 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
591 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
592 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
595 /* ------------------------------------------------------------------ */
603 static void set_cliplist(struct saa7134_dev *dev, int reg,
604 struct cliplist *cl, int entries, char *name)
609 for (i = 0; i < entries; i++) {
610 winbits |= cl[i].enable;
611 winbits &= ~cl[i].disable;
612 if (i < 15 && cl[i].position == cl[i+1].position)
614 saa_writeb(reg + 0, winbits);
615 saa_writeb(reg + 2, cl[i].position & 0xff);
616 saa_writeb(reg + 3, cl[i].position >> 8);
617 video_dbg("clip: %s winbits=%02x pos=%d\n",
618 name,winbits,cl[i].position);
621 for (; reg < 0x400; reg += 8) {
622 saa_writeb(reg+ 0, 0);
623 saa_writeb(reg + 1, 0);
624 saa_writeb(reg + 2, 0);
625 saa_writeb(reg + 3, 0);
629 static int clip_range(int val)
636 /* Sort into smallest position first order */
637 static int cliplist_cmp(const void *a, const void *b)
639 const struct cliplist *cla = a;
640 const struct cliplist *clb = b;
641 if (cla->position < clb->position)
643 if (cla->position > clb->position)
648 static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
649 int nclips, int interlace)
651 struct cliplist col[16], row[16];
652 int cols = 0, rows = 0, i;
653 int div = interlace ? 2 : 1;
655 memset(col, 0, sizeof(col));
656 memset(row, 0, sizeof(row));
657 for (i = 0; i < nclips && i < 8; i++) {
658 col[cols].position = clip_range(clips[i].c.left);
659 col[cols].enable = (1 << i);
661 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
662 col[cols].disable = (1 << i);
664 row[rows].position = clip_range(clips[i].c.top / div);
665 row[rows].enable = (1 << i);
667 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
669 row[rows].disable = (1 << i);
672 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
673 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
674 set_cliplist(dev,0x380,col,cols,"cols");
675 set_cliplist(dev,0x384,row,rows,"rows");
679 static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win, bool try)
681 enum v4l2_field field;
684 if (!try && (dev->ovbuf.base == NULL || dev->ovfmt == NULL))
686 if (win->w.width < 48)
688 if (win->w.height < 32)
690 if (win->clipcount > 8)
694 win->global_alpha = 0;
696 maxw = dev->crop_current.width;
697 maxh = dev->crop_current.height;
699 if (V4L2_FIELD_ANY == field) {
700 field = (win->w.height > maxh/2)
701 ? V4L2_FIELD_INTERLACED
706 case V4L2_FIELD_BOTTOM:
710 field = V4L2_FIELD_INTERLACED;
715 if (win->w.width > maxw)
717 if (win->w.height > maxh)
718 win->w.height = maxh;
722 static int start_preview(struct saa7134_dev *dev)
724 unsigned long base,control,bpl;
727 err = verify_preview(dev, &dev->win, false);
731 dev->ovfield = dev->win.field;
732 video_dbg("start_preview %dx%d+%d+%d %s field=%s\n",
733 dev->win.w.width, dev->win.w.height,
734 dev->win.w.left, dev->win.w.top,
735 dev->ovfmt->name, v4l2_field_names[dev->ovfield]);
737 /* setup window + clipping */
738 set_size(dev, TASK_B, dev->win.w.width, dev->win.w.height,
739 V4L2_FIELD_HAS_BOTH(dev->ovfield));
740 setup_clipping(dev, dev->clips, dev->nclips,
741 V4L2_FIELD_HAS_BOTH(dev->ovfield));
743 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
745 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
746 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
748 /* dma: setup channel 1 (= Video Task B) */
749 base = (unsigned long)dev->ovbuf.base;
750 base += dev->ovbuf.fmt.bytesperline * dev->win.w.top;
751 base += dev->ovfmt->depth/8 * dev->win.w.left;
752 bpl = dev->ovbuf.fmt.bytesperline;
753 control = SAA7134_RS_CONTROL_BURST_16;
754 if (dev->ovfmt->bswap)
755 control |= SAA7134_RS_CONTROL_BSWAP;
756 if (dev->ovfmt->wswap)
757 control |= SAA7134_RS_CONTROL_WSWAP;
758 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
759 saa_writel(SAA7134_RS_BA1(1),base);
760 saa_writel(SAA7134_RS_BA2(1),base+bpl);
761 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
762 saa_writel(SAA7134_RS_CONTROL(1),control);
764 saa_writel(SAA7134_RS_BA1(1),base);
765 saa_writel(SAA7134_RS_BA2(1),base);
766 saa_writel(SAA7134_RS_PITCH(1),bpl);
767 saa_writel(SAA7134_RS_CONTROL(1),control);
772 saa7134_set_dmabits(dev);
777 static int stop_preview(struct saa7134_dev *dev)
780 saa7134_set_dmabits(dev);
785 * Media Controller helper functions
788 static int saa7134_enable_analog_tuner(struct saa7134_dev *dev)
790 #ifdef CONFIG_MEDIA_CONTROLLER
791 struct media_device *mdev = dev->media_dev;
792 struct media_entity *source;
793 struct media_link *link, *found_link = NULL;
794 int ret, active_links = 0;
796 if (!mdev || !dev->decoder)
800 * This will find the tuner that is connected into the decoder.
801 * Technically, this is not 100% correct, as the device may be
802 * using an analog input instead of the tuner. However, as we can't
803 * do DVB streaming while the DMA engine is being used for V4L2,
804 * this should be enough for the actual needs.
806 list_for_each_entry(link, &dev->decoder->links, list) {
807 if (link->sink->entity == dev->decoder) {
809 if (link->flags & MEDIA_LNK_FL_ENABLED)
815 if (active_links == 1 || !found_link)
818 source = found_link->source->entity;
819 list_for_each_entry(link, &source->links, list) {
820 struct media_entity *sink;
823 sink = link->sink->entity;
825 if (sink == dev->decoder)
826 flags = MEDIA_LNK_FL_ENABLED;
828 ret = media_entity_setup_link(link, flags);
830 pr_err("Couldn't change link %s->%s to %s. Error %d\n",
831 source->name, sink->name,
832 flags ? "enabled" : "disabled",
841 /* ------------------------------------------------------------------ */
843 static int buffer_activate(struct saa7134_dev *dev,
844 struct saa7134_buf *buf,
845 struct saa7134_buf *next)
847 struct saa7134_dmaqueue *dmaq = buf->vb2.vb2_buf.vb2_queue->drv_priv;
848 unsigned long base,control,bpl;
849 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
851 video_dbg("buffer_activate buf=%p\n", buf);
854 set_size(dev, TASK_A, dev->width, dev->height,
855 V4L2_FIELD_HAS_BOTH(dev->field));
857 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
859 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
860 saa_writeb(SAA7134_OFMT_VIDEO_A, dev->fmt->pm);
862 /* DMA: setup channel 0 (= Video Task A0) */
863 base = saa7134_buffer_base(buf);
864 if (dev->fmt->planar)
867 bpl = (dev->width * dev->fmt->depth) / 8;
868 control = SAA7134_RS_CONTROL_BURST_16 |
869 SAA7134_RS_CONTROL_ME |
870 (dmaq->pt.dma >> 12);
872 control |= SAA7134_RS_CONTROL_BSWAP;
874 control |= SAA7134_RS_CONTROL_WSWAP;
875 if (V4L2_FIELD_HAS_BOTH(dev->field)) {
877 saa_writel(SAA7134_RS_BA1(0),base);
878 saa_writel(SAA7134_RS_BA2(0),base+bpl);
879 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
882 saa_writel(SAA7134_RS_BA1(0),base);
883 saa_writel(SAA7134_RS_BA2(0),base);
884 saa_writel(SAA7134_RS_PITCH(0),bpl);
886 saa_writel(SAA7134_RS_CONTROL(0),control);
888 if (dev->fmt->planar) {
889 /* DMA: setup channel 4+5 (= planar task A) */
890 bpl_uv = bpl >> dev->fmt->hshift;
891 lines_uv = dev->height >> dev->fmt->vshift;
892 base2 = base + bpl * dev->height;
893 base3 = base2 + bpl_uv * lines_uv;
894 if (dev->fmt->uvswap)
895 tmp = base2, base2 = base3, base3 = tmp;
896 video_dbg("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
897 bpl_uv,lines_uv,base2,base3);
898 if (V4L2_FIELD_HAS_BOTH(dev->field)) {
900 saa_writel(SAA7134_RS_BA1(4),base2);
901 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
902 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
903 saa_writel(SAA7134_RS_BA1(5),base3);
904 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
905 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
908 saa_writel(SAA7134_RS_BA1(4),base2);
909 saa_writel(SAA7134_RS_BA2(4),base2);
910 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
911 saa_writel(SAA7134_RS_BA1(5),base3);
912 saa_writel(SAA7134_RS_BA2(5),base3);
913 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
915 saa_writel(SAA7134_RS_CONTROL(4),control);
916 saa_writel(SAA7134_RS_CONTROL(5),control);
920 saa7134_set_dmabits(dev);
921 mod_timer(&dmaq->timeout, jiffies + BUFFER_TIMEOUT);
925 static int buffer_init(struct vb2_buffer *vb2)
927 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
928 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
929 struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
932 buf->activate = buffer_activate;
936 static int buffer_prepare(struct vb2_buffer *vb2)
938 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
939 struct saa7134_dev *dev = dmaq->dev;
940 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
941 struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
942 struct sg_table *dma = vb2_dma_sg_plane_desc(vb2, 0);
945 if (dma->sgl->offset) {
946 pr_err("The buffer is not page-aligned\n");
949 size = (dev->width * dev->height * dev->fmt->depth) >> 3;
950 if (vb2_plane_size(vb2, 0) < size)
953 vb2_set_plane_payload(vb2, 0, size);
954 vbuf->field = dev->field;
956 return saa7134_pgtable_build(dev->pci, &dmaq->pt, dma->sgl, dma->nents,
957 saa7134_buffer_startpage(buf));
960 static int queue_setup(struct vb2_queue *q,
961 unsigned int *nbuffers, unsigned int *nplanes,
962 unsigned int sizes[], struct device *alloc_devs[])
964 struct saa7134_dmaqueue *dmaq = q->drv_priv;
965 struct saa7134_dev *dev = dmaq->dev;
966 int size = dev->fmt->depth * dev->width * dev->height >> 3;
968 if (dev->width < 48 ||
970 dev->width/4 > dev->crop_current.width ||
971 dev->height/4 > dev->crop_current.height ||
972 dev->width > dev->crop_bounds.width ||
973 dev->height > dev->crop_bounds.height)
976 *nbuffers = saa7134_buffer_count(size, *nbuffers);
980 saa7134_enable_analog_tuner(dev);
986 * move buffer to hardware queue
988 void saa7134_vb2_buffer_queue(struct vb2_buffer *vb)
990 struct saa7134_dmaqueue *dmaq = vb->vb2_queue->drv_priv;
991 struct saa7134_dev *dev = dmaq->dev;
992 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
993 struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
995 saa7134_buffer_queue(dev, dmaq, buf);
997 EXPORT_SYMBOL_GPL(saa7134_vb2_buffer_queue);
999 int saa7134_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
1001 struct saa7134_dmaqueue *dmaq = vq->drv_priv;
1002 struct saa7134_dev *dev = dmaq->dev;
1005 * Planar video capture and TS share the same DMA channel,
1006 * so only one can be active at a time.
1008 if (card_is_empress(dev) && vb2_is_busy(&dev->empress_vbq) &&
1009 dmaq == &dev->video_q && dev->fmt->planar) {
1010 struct saa7134_buf *buf, *tmp;
1012 list_for_each_entry_safe(buf, tmp, &dmaq->queue, entry) {
1013 list_del(&buf->entry);
1014 vb2_buffer_done(&buf->vb2.vb2_buf,
1015 VB2_BUF_STATE_QUEUED);
1018 vb2_buffer_done(&dmaq->curr->vb2.vb2_buf,
1019 VB2_BUF_STATE_QUEUED);
1025 /* The SAA7134 has a 1K FIFO; the datasheet suggests that when
1026 * configured conservatively, there's 22 usec of buffering for video.
1027 * We therefore request a DMA latency of 20 usec, giving us 2 usec of
1028 * margin in case the FIFO is configured differently to the datasheet.
1029 * Unfortunately, I lack register-level documentation to check the
1030 * Linux FIFO setup and confirm the perfect value.
1032 if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
1033 (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
1034 pm_qos_add_request(&dev->qos_request,
1035 PM_QOS_CPU_DMA_LATENCY, 20);
1041 void saa7134_vb2_stop_streaming(struct vb2_queue *vq)
1043 struct saa7134_dmaqueue *dmaq = vq->drv_priv;
1044 struct saa7134_dev *dev = dmaq->dev;
1046 saa7134_stop_streaming(dev, dmaq);
1048 if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
1049 (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
1050 pm_qos_remove_request(&dev->qos_request);
1053 static const struct vb2_ops vb2_qops = {
1054 .queue_setup = queue_setup,
1055 .buf_init = buffer_init,
1056 .buf_prepare = buffer_prepare,
1057 .buf_queue = saa7134_vb2_buffer_queue,
1058 .wait_prepare = vb2_ops_wait_prepare,
1059 .wait_finish = vb2_ops_wait_finish,
1060 .start_streaming = saa7134_vb2_start_streaming,
1061 .stop_streaming = saa7134_vb2_stop_streaming,
1064 /* ------------------------------------------------------------------ */
1066 static int saa7134_s_ctrl(struct v4l2_ctrl *ctrl)
1068 struct saa7134_dev *dev = container_of(ctrl->handler, struct saa7134_dev, ctrl_handler);
1069 unsigned long flags;
1070 int restart_overlay = 0;
1073 case V4L2_CID_BRIGHTNESS:
1074 dev->ctl_bright = ctrl->val;
1075 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, ctrl->val);
1078 dev->ctl_hue = ctrl->val;
1079 saa_writeb(SAA7134_DEC_CHROMA_HUE, ctrl->val);
1081 case V4L2_CID_CONTRAST:
1082 dev->ctl_contrast = ctrl->val;
1083 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1084 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1086 case V4L2_CID_SATURATION:
1087 dev->ctl_saturation = ctrl->val;
1088 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1089 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1091 case V4L2_CID_AUDIO_MUTE:
1092 dev->ctl_mute = ctrl->val;
1093 saa7134_tvaudio_setmute(dev);
1095 case V4L2_CID_AUDIO_VOLUME:
1096 dev->ctl_volume = ctrl->val;
1097 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1099 case V4L2_CID_PRIVATE_INVERT:
1100 dev->ctl_invert = ctrl->val;
1101 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1102 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1103 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1104 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1106 case V4L2_CID_HFLIP:
1107 dev->ctl_mirror = ctrl->val;
1108 restart_overlay = 1;
1110 case V4L2_CID_PRIVATE_Y_EVEN:
1111 dev->ctl_y_even = ctrl->val;
1112 restart_overlay = 1;
1114 case V4L2_CID_PRIVATE_Y_ODD:
1115 dev->ctl_y_odd = ctrl->val;
1116 restart_overlay = 1;
1118 case V4L2_CID_PRIVATE_AUTOMUTE:
1120 struct v4l2_priv_tun_config tda9887_cfg;
1122 tda9887_cfg.tuner = TUNER_TDA9887;
1123 tda9887_cfg.priv = &dev->tda9887_conf;
1125 dev->ctl_automute = ctrl->val;
1126 if (dev->tda9887_conf) {
1127 if (dev->ctl_automute)
1128 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1130 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1132 saa_call_all(dev, tuner, s_config, &tda9887_cfg);
1139 if (restart_overlay && dev->overlay_owner) {
1140 spin_lock_irqsave(&dev->slock, flags);
1143 spin_unlock_irqrestore(&dev->slock, flags);
1148 /* ------------------------------------------------------------------ */
1150 static int video_open(struct file *file)
1152 struct video_device *vdev = video_devdata(file);
1153 struct saa7134_dev *dev = video_drvdata(file);
1154 int ret = v4l2_fh_open(file);
1159 mutex_lock(&dev->lock);
1160 if (vdev->vfl_type == VFL_TYPE_RADIO) {
1161 /* switch to radio mode */
1162 saa7134_tvaudio_setinput(dev, &card(dev).radio);
1163 saa_call_all(dev, tuner, s_radio);
1165 /* switch to video/vbi mode */
1166 video_mux(dev, dev->ctl_input);
1168 mutex_unlock(&dev->lock);
1173 static int video_release(struct file *file)
1175 struct video_device *vdev = video_devdata(file);
1176 struct saa7134_dev *dev = video_drvdata(file);
1177 struct v4l2_fh *fh = file->private_data;
1178 struct saa6588_command cmd;
1179 unsigned long flags;
1181 mutex_lock(&dev->lock);
1182 saa7134_tvaudio_close(dev);
1184 /* turn off overlay */
1185 if (fh == dev->overlay_owner) {
1186 spin_lock_irqsave(&dev->slock,flags);
1188 spin_unlock_irqrestore(&dev->slock,flags);
1189 dev->overlay_owner = NULL;
1192 if (vdev->vfl_type == VFL_TYPE_RADIO)
1193 v4l2_fh_release(file);
1195 _vb2_fop_release(file, NULL);
1197 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1198 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1199 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1200 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1201 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1203 saa_call_all(dev, tuner, standby);
1204 if (vdev->vfl_type == VFL_TYPE_RADIO)
1205 saa_call_all(dev, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
1206 mutex_unlock(&dev->lock);
1211 static ssize_t radio_read(struct file *file, char __user *data,
1212 size_t count, loff_t *ppos)
1214 struct saa7134_dev *dev = video_drvdata(file);
1215 struct saa6588_command cmd;
1217 cmd.block_count = count/3;
1218 cmd.nonblocking = file->f_flags & O_NONBLOCK;
1220 cmd.instance = file;
1221 cmd.result = -ENODEV;
1223 mutex_lock(&dev->lock);
1224 saa_call_all(dev, core, ioctl, SAA6588_CMD_READ, &cmd);
1225 mutex_unlock(&dev->lock);
1230 static __poll_t radio_poll(struct file *file, poll_table *wait)
1232 struct saa7134_dev *dev = video_drvdata(file);
1233 struct saa6588_command cmd;
1234 __poll_t rc = v4l2_ctrl_poll(file, wait);
1236 cmd.instance = file;
1237 cmd.event_list = wait;
1239 mutex_lock(&dev->lock);
1240 saa_call_all(dev, core, ioctl, SAA6588_CMD_POLL, &cmd);
1241 mutex_unlock(&dev->lock);
1243 return rc | cmd.poll_mask;
1246 /* ------------------------------------------------------------------ */
1248 static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
1249 struct v4l2_format *f)
1251 struct saa7134_dev *dev = video_drvdata(file);
1252 struct saa7134_tvnorm *norm = dev->tvnorm;
1254 memset(&f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
1255 f->fmt.vbi.sampling_rate = 6750000 * 4;
1256 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1257 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1258 f->fmt.vbi.offset = 64 * 4;
1259 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1260 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1261 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1262 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1263 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1268 static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
1269 struct v4l2_format *f)
1271 struct saa7134_dev *dev = video_drvdata(file);
1273 f->fmt.pix.width = dev->width;
1274 f->fmt.pix.height = dev->height;
1275 f->fmt.pix.field = dev->field;
1276 f->fmt.pix.pixelformat = dev->fmt->fourcc;
1277 if (dev->fmt->planar)
1278 f->fmt.pix.bytesperline = f->fmt.pix.width;
1280 f->fmt.pix.bytesperline =
1281 (f->fmt.pix.width * dev->fmt->depth) / 8;
1282 f->fmt.pix.sizeimage =
1283 (f->fmt.pix.height * f->fmt.pix.width * dev->fmt->depth) / 8;
1284 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1288 static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
1289 struct v4l2_format *f)
1291 struct saa7134_dev *dev = video_drvdata(file);
1292 struct v4l2_clip __user *clips = f->fmt.win.clips;
1293 u32 clipcount = f->fmt.win.clipcount;
1297 if (saa7134_no_overlay > 0) {
1298 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1301 f->fmt.win = dev->win;
1302 f->fmt.win.clips = clips;
1305 if (dev->nclips < clipcount)
1306 clipcount = dev->nclips;
1307 f->fmt.win.clipcount = clipcount;
1309 for (i = 0; !err && i < clipcount; i++) {
1310 if (copy_to_user(&f->fmt.win.clips[i].c, &dev->clips[i].c,
1311 sizeof(struct v4l2_rect)))
1318 static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
1319 struct v4l2_format *f)
1321 struct saa7134_dev *dev = video_drvdata(file);
1322 struct saa7134_format *fmt;
1323 enum v4l2_field field;
1324 unsigned int maxw, maxh;
1326 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1330 field = f->fmt.pix.field;
1331 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1332 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1334 if (V4L2_FIELD_ANY == field) {
1335 field = (f->fmt.pix.height > maxh/2)
1336 ? V4L2_FIELD_INTERLACED
1337 : V4L2_FIELD_BOTTOM;
1340 case V4L2_FIELD_TOP:
1341 case V4L2_FIELD_BOTTOM:
1345 field = V4L2_FIELD_INTERLACED;
1349 f->fmt.pix.field = field;
1350 if (f->fmt.pix.width < 48)
1351 f->fmt.pix.width = 48;
1352 if (f->fmt.pix.height < 32)
1353 f->fmt.pix.height = 32;
1354 if (f->fmt.pix.width > maxw)
1355 f->fmt.pix.width = maxw;
1356 if (f->fmt.pix.height > maxh)
1357 f->fmt.pix.height = maxh;
1358 f->fmt.pix.width &= ~0x03;
1360 f->fmt.pix.bytesperline = f->fmt.pix.width;
1362 f->fmt.pix.bytesperline =
1363 (f->fmt.pix.width * fmt->depth) / 8;
1364 f->fmt.pix.sizeimage =
1365 (f->fmt.pix.height * f->fmt.pix.width * fmt->depth) / 8;
1366 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1371 static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
1372 struct v4l2_format *f)
1374 struct saa7134_dev *dev = video_drvdata(file);
1376 if (saa7134_no_overlay > 0) {
1377 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1381 if (f->fmt.win.clips == NULL)
1382 f->fmt.win.clipcount = 0;
1383 return verify_preview(dev, &f->fmt.win, true);
1386 static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
1387 struct v4l2_format *f)
1389 struct saa7134_dev *dev = video_drvdata(file);
1392 err = saa7134_try_fmt_vid_cap(file, priv, f);
1396 dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1397 dev->width = f->fmt.pix.width;
1398 dev->height = f->fmt.pix.height;
1399 dev->field = f->fmt.pix.field;
1403 static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
1404 struct v4l2_format *f)
1406 struct saa7134_dev *dev = video_drvdata(file);
1408 unsigned long flags;
1410 if (saa7134_no_overlay > 0) {
1411 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1414 if (f->fmt.win.clips == NULL)
1415 f->fmt.win.clipcount = 0;
1416 err = verify_preview(dev, &f->fmt.win, true);
1420 dev->win = f->fmt.win;
1421 dev->nclips = f->fmt.win.clipcount;
1423 if (copy_from_user(dev->clips, f->fmt.win.clips,
1424 sizeof(struct v4l2_clip) * dev->nclips))
1427 if (priv == dev->overlay_owner) {
1428 spin_lock_irqsave(&dev->slock, flags);
1431 spin_unlock_irqrestore(&dev->slock, flags);
1437 int saa7134_enum_input(struct file *file, void *priv, struct v4l2_input *i)
1439 struct saa7134_dev *dev = video_drvdata(file);
1443 if (n >= SAA7134_INPUT_MAX)
1445 if (card_in(dev, i->index).type == SAA7134_NO_INPUT)
1448 strscpy(i->name, saa7134_input_name[card_in(dev, n).type],
1450 switch (card_in(dev, n).type) {
1451 case SAA7134_INPUT_TV:
1452 case SAA7134_INPUT_TV_MONO:
1453 i->type = V4L2_INPUT_TYPE_TUNER;
1456 i->type = V4L2_INPUT_TYPE_CAMERA;
1459 if (n == dev->ctl_input) {
1460 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1461 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1463 if (0 != (v1 & 0x40))
1464 i->status |= V4L2_IN_ST_NO_H_LOCK;
1465 if (0 != (v2 & 0x40))
1466 i->status |= V4L2_IN_ST_NO_SIGNAL;
1467 if (0 != (v2 & 0x0e))
1468 i->status |= V4L2_IN_ST_MACROVISION;
1470 i->std = SAA7134_NORMS;
1473 EXPORT_SYMBOL_GPL(saa7134_enum_input);
1475 int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
1477 struct saa7134_dev *dev = video_drvdata(file);
1479 *i = dev->ctl_input;
1482 EXPORT_SYMBOL_GPL(saa7134_g_input);
1484 int saa7134_s_input(struct file *file, void *priv, unsigned int i)
1486 struct saa7134_dev *dev = video_drvdata(file);
1488 if (i >= SAA7134_INPUT_MAX)
1490 if (card_in(dev, i).type == SAA7134_NO_INPUT)
1495 EXPORT_SYMBOL_GPL(saa7134_s_input);
1497 int saa7134_querycap(struct file *file, void *priv,
1498 struct v4l2_capability *cap)
1500 struct saa7134_dev *dev = video_drvdata(file);
1501 struct video_device *vdev = video_devdata(file);
1502 u32 radio_caps, video_caps, vbi_caps;
1504 unsigned int tuner_type = dev->tuner_type;
1506 strscpy(cap->driver, "saa7134", sizeof(cap->driver));
1507 strscpy(cap->card, saa7134_boards[dev->board].name,
1509 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1511 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1512 if ((tuner_type != TUNER_ABSENT) && (tuner_type != UNSET))
1513 cap->device_caps |= V4L2_CAP_TUNER;
1515 radio_caps = V4L2_CAP_RADIO;
1517 radio_caps |= V4L2_CAP_RDS_CAPTURE;
1519 video_caps = V4L2_CAP_VIDEO_CAPTURE;
1520 if (saa7134_no_overlay <= 0 && !is_empress(file))
1521 video_caps |= V4L2_CAP_VIDEO_OVERLAY;
1523 vbi_caps = V4L2_CAP_VBI_CAPTURE;
1525 switch (vdev->vfl_type) {
1526 case VFL_TYPE_RADIO:
1527 cap->device_caps |= radio_caps;
1529 case VFL_TYPE_GRABBER:
1530 cap->device_caps |= video_caps;
1533 cap->device_caps |= vbi_caps;
1538 cap->capabilities = radio_caps | video_caps | vbi_caps |
1539 cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1540 if (vdev->vfl_type == VFL_TYPE_RADIO) {
1541 cap->device_caps &= ~V4L2_CAP_STREAMING;
1543 cap->device_caps &= ~V4L2_CAP_READWRITE;
1548 EXPORT_SYMBOL_GPL(saa7134_querycap);
1550 int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id)
1552 struct saa7134_dev *dev = video_drvdata(file);
1553 struct v4l2_fh *fh = priv;
1554 unsigned long flags;
1558 if (is_empress(file) && dev->overlay_owner) {
1559 /* Don't change the std from the mpeg device
1560 if overlay is active. */
1564 for (i = 0; i < TVNORMS; i++)
1565 if (id == tvnorms[i].id)
1569 for (i = 0; i < TVNORMS; i++)
1570 if (id & tvnorms[i].id)
1575 if ((id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1576 if (secam[0] == 'L' || secam[0] == 'l') {
1577 if (secam[1] == 'C' || secam[1] == 'c')
1578 fixup = V4L2_STD_SECAM_LC;
1580 fixup = V4L2_STD_SECAM_L;
1582 if (secam[0] == 'D' || secam[0] == 'd')
1583 fixup = V4L2_STD_SECAM_DK;
1585 fixup = V4L2_STD_SECAM;
1587 for (i = 0; i < TVNORMS; i++) {
1588 if (fixup == tvnorms[i].id)
1597 if (!is_empress(file) && fh == dev->overlay_owner) {
1598 spin_lock_irqsave(&dev->slock, flags);
1600 spin_unlock_irqrestore(&dev->slock, flags);
1602 set_tvnorm(dev, &tvnorms[i]);
1604 spin_lock_irqsave(&dev->slock, flags);
1606 spin_unlock_irqrestore(&dev->slock, flags);
1608 set_tvnorm(dev, &tvnorms[i]);
1610 saa7134_tvaudio_do_scan(dev);
1613 EXPORT_SYMBOL_GPL(saa7134_s_std);
1615 int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
1617 struct saa7134_dev *dev = video_drvdata(file);
1619 *id = dev->tvnorm->id;
1622 EXPORT_SYMBOL_GPL(saa7134_g_std);
1624 static v4l2_std_id saa7134_read_std(struct saa7134_dev *dev)
1626 static v4l2_std_id stds[] = {
1632 v4l2_std_id result = 0;
1634 u8 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
1635 u8 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
1637 if (!(st2 & 0x1)) /* RDCAP == 0 */
1638 result = V4L2_STD_UNKNOWN;
1640 result = stds[st1 & 0x03];
1645 int saa7134_querystd(struct file *file, void *priv, v4l2_std_id *std)
1647 struct saa7134_dev *dev = video_drvdata(file);
1648 *std &= saa7134_read_std(dev);
1651 EXPORT_SYMBOL_GPL(saa7134_querystd);
1653 static int saa7134_cropcap(struct file *file, void *priv,
1654 struct v4l2_cropcap *cap)
1656 struct saa7134_dev *dev = video_drvdata(file);
1658 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1659 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1661 cap->pixelaspect.numerator = 1;
1662 cap->pixelaspect.denominator = 1;
1663 if (dev->tvnorm->id & V4L2_STD_525_60) {
1664 cap->pixelaspect.numerator = 11;
1665 cap->pixelaspect.denominator = 10;
1667 if (dev->tvnorm->id & V4L2_STD_625_50) {
1668 cap->pixelaspect.numerator = 54;
1669 cap->pixelaspect.denominator = 59;
1674 static int saa7134_g_selection(struct file *file, void *f, struct v4l2_selection *sel)
1676 struct saa7134_dev *dev = video_drvdata(file);
1678 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1679 sel->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1682 switch (sel->target) {
1683 case V4L2_SEL_TGT_CROP:
1684 sel->r = dev->crop_current;
1686 case V4L2_SEL_TGT_CROP_DEFAULT:
1687 sel->r = dev->crop_defrect;
1689 case V4L2_SEL_TGT_CROP_BOUNDS:
1690 sel->r = dev->crop_bounds;
1698 static int saa7134_s_selection(struct file *file, void *f, struct v4l2_selection *sel)
1700 struct saa7134_dev *dev = video_drvdata(file);
1701 struct v4l2_rect *b = &dev->crop_bounds;
1702 struct v4l2_rect *c = &dev->crop_current;
1704 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1705 sel->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1708 if (sel->target != V4L2_SEL_TGT_CROP)
1711 if (dev->overlay_owner)
1713 if (vb2_is_streaming(&dev->video_vbq))
1717 if (c->top < b->top)
1719 if (c->top > b->top + b->height)
1720 c->top = b->top + b->height;
1721 if (c->height > b->top - c->top + b->height)
1722 c->height = b->top - c->top + b->height;
1724 if (c->left < b->left)
1726 if (c->left > b->left + b->width)
1727 c->left = b->left + b->width;
1728 if (c->width > b->left - c->left + b->width)
1729 c->width = b->left - c->left + b->width;
1734 int saa7134_g_tuner(struct file *file, void *priv,
1735 struct v4l2_tuner *t)
1737 struct saa7134_dev *dev = video_drvdata(file);
1742 memset(t, 0, sizeof(*t));
1743 for (n = 0; n < SAA7134_INPUT_MAX; n++) {
1744 if (card_in(dev, n).type == SAA7134_INPUT_TV ||
1745 card_in(dev, n).type == SAA7134_INPUT_TV_MONO)
1748 if (n == SAA7134_INPUT_MAX)
1750 if (card_in(dev, n).type != SAA7134_NO_INPUT) {
1751 strscpy(t->name, "Television", sizeof(t->name));
1752 t->type = V4L2_TUNER_ANALOG_TV;
1753 saa_call_all(dev, tuner, g_tuner, t);
1754 t->capability = V4L2_TUNER_CAP_NORM |
1755 V4L2_TUNER_CAP_STEREO |
1756 V4L2_TUNER_CAP_LANG1 |
1757 V4L2_TUNER_CAP_LANG2;
1758 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1759 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1761 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1765 EXPORT_SYMBOL_GPL(saa7134_g_tuner);
1767 int saa7134_s_tuner(struct file *file, void *priv,
1768 const struct v4l2_tuner *t)
1770 struct saa7134_dev *dev = video_drvdata(file);
1776 mode = dev->thread.mode;
1777 if (UNSET == mode) {
1778 rx = saa7134_tvaudio_getstereo(dev);
1779 mode = saa7134_tvaudio_rx2mode(rx);
1781 if (mode != t->audmode)
1782 dev->thread.mode = t->audmode;
1786 EXPORT_SYMBOL_GPL(saa7134_s_tuner);
1788 int saa7134_g_frequency(struct file *file, void *priv,
1789 struct v4l2_frequency *f)
1791 struct saa7134_dev *dev = video_drvdata(file);
1796 saa_call_all(dev, tuner, g_frequency, f);
1800 EXPORT_SYMBOL_GPL(saa7134_g_frequency);
1802 int saa7134_s_frequency(struct file *file, void *priv,
1803 const struct v4l2_frequency *f)
1805 struct saa7134_dev *dev = video_drvdata(file);
1810 saa_call_all(dev, tuner, s_frequency, f);
1812 saa7134_tvaudio_do_scan(dev);
1815 EXPORT_SYMBOL_GPL(saa7134_s_frequency);
1817 static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
1818 struct v4l2_fmtdesc *f)
1820 if (f->index >= FORMATS)
1823 strscpy(f->description, formats[f->index].name,
1824 sizeof(f->description));
1826 f->pixelformat = formats[f->index].fourcc;
1831 static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
1832 struct v4l2_fmtdesc *f)
1834 if (saa7134_no_overlay > 0) {
1835 pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1839 if ((f->index >= FORMATS) || formats[f->index].planar)
1842 strscpy(f->description, formats[f->index].name,
1843 sizeof(f->description));
1845 f->pixelformat = formats[f->index].fourcc;
1850 static int saa7134_g_fbuf(struct file *file, void *f,
1851 struct v4l2_framebuffer *fb)
1853 struct saa7134_dev *dev = video_drvdata(file);
1856 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
1861 static int saa7134_s_fbuf(struct file *file, void *f,
1862 const struct v4l2_framebuffer *fb)
1864 struct saa7134_dev *dev = video_drvdata(file);
1865 struct saa7134_format *fmt;
1867 if (!capable(CAP_SYS_ADMIN) &&
1868 !capable(CAP_SYS_RAWIO))
1872 fmt = format_by_fourcc(fb->fmt.pixelformat);
1879 if (0 == dev->ovbuf.fmt.bytesperline)
1880 dev->ovbuf.fmt.bytesperline =
1881 dev->ovbuf.fmt.width*fmt->depth/8;
1885 static int saa7134_overlay(struct file *file, void *priv, unsigned int on)
1887 struct saa7134_dev *dev = video_drvdata(file);
1888 unsigned long flags;
1891 if (saa7134_no_overlay > 0) {
1892 video_dbg("no_overlay\n");
1896 if (dev->overlay_owner && priv != dev->overlay_owner)
1898 dev->overlay_owner = priv;
1899 spin_lock_irqsave(&dev->slock, flags);
1901 spin_unlock_irqrestore(&dev->slock, flags);
1904 if (priv != dev->overlay_owner)
1906 spin_lock_irqsave(&dev->slock, flags);
1908 spin_unlock_irqrestore(&dev->slock, flags);
1909 dev->overlay_owner = NULL;
1914 #ifdef CONFIG_VIDEO_ADV_DEBUG
1915 static int vidioc_g_register (struct file *file, void *priv,
1916 struct v4l2_dbg_register *reg)
1918 struct saa7134_dev *dev = video_drvdata(file);
1920 reg->val = saa_readb(reg->reg & 0xffffff);
1925 static int vidioc_s_register (struct file *file, void *priv,
1926 const struct v4l2_dbg_register *reg)
1928 struct saa7134_dev *dev = video_drvdata(file);
1930 saa_writeb(reg->reg & 0xffffff, reg->val);
1935 static int radio_g_tuner(struct file *file, void *priv,
1936 struct v4l2_tuner *t)
1938 struct saa7134_dev *dev = video_drvdata(file);
1943 strscpy(t->name, "Radio", sizeof(t->name));
1945 saa_call_all(dev, tuner, g_tuner, t);
1946 t->audmode &= V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO;
1947 if (dev->input->amux == TV) {
1948 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
1949 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
1950 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
1954 static int radio_s_tuner(struct file *file, void *priv,
1955 const struct v4l2_tuner *t)
1957 struct saa7134_dev *dev = video_drvdata(file);
1962 saa_call_all(dev, tuner, s_tuner, t);
1966 static const struct v4l2_file_operations video_fops =
1968 .owner = THIS_MODULE,
1970 .release = video_release,
1971 .read = vb2_fop_read,
1972 .poll = vb2_fop_poll,
1973 .mmap = vb2_fop_mmap,
1974 .unlocked_ioctl = video_ioctl2,
1977 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1978 .vidioc_querycap = saa7134_querycap,
1979 .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
1980 .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
1981 .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
1982 .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
1983 .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
1984 .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
1985 .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
1986 .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
1987 .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
1988 .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
1989 .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
1990 .vidioc_cropcap = saa7134_cropcap,
1991 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1992 .vidioc_querybuf = vb2_ioctl_querybuf,
1993 .vidioc_qbuf = vb2_ioctl_qbuf,
1994 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1995 .vidioc_expbuf = vb2_ioctl_expbuf,
1996 .vidioc_s_std = saa7134_s_std,
1997 .vidioc_g_std = saa7134_g_std,
1998 .vidioc_querystd = saa7134_querystd,
1999 .vidioc_enum_input = saa7134_enum_input,
2000 .vidioc_g_input = saa7134_g_input,
2001 .vidioc_s_input = saa7134_s_input,
2002 .vidioc_streamon = vb2_ioctl_streamon,
2003 .vidioc_streamoff = vb2_ioctl_streamoff,
2004 .vidioc_g_tuner = saa7134_g_tuner,
2005 .vidioc_s_tuner = saa7134_s_tuner,
2006 .vidioc_g_selection = saa7134_g_selection,
2007 .vidioc_s_selection = saa7134_s_selection,
2008 .vidioc_g_fbuf = saa7134_g_fbuf,
2009 .vidioc_s_fbuf = saa7134_s_fbuf,
2010 .vidioc_overlay = saa7134_overlay,
2011 .vidioc_g_frequency = saa7134_g_frequency,
2012 .vidioc_s_frequency = saa7134_s_frequency,
2013 #ifdef CONFIG_VIDEO_ADV_DEBUG
2014 .vidioc_g_register = vidioc_g_register,
2015 .vidioc_s_register = vidioc_s_register,
2017 .vidioc_log_status = v4l2_ctrl_log_status,
2018 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2019 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2022 static const struct v4l2_file_operations radio_fops = {
2023 .owner = THIS_MODULE,
2026 .release = video_release,
2027 .unlocked_ioctl = video_ioctl2,
2031 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2032 .vidioc_querycap = saa7134_querycap,
2033 .vidioc_g_tuner = radio_g_tuner,
2034 .vidioc_s_tuner = radio_s_tuner,
2035 .vidioc_g_frequency = saa7134_g_frequency,
2036 .vidioc_s_frequency = saa7134_s_frequency,
2037 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2038 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2041 /* ----------------------------------------------------------- */
2042 /* exported stuff */
2044 struct video_device saa7134_video_template = {
2045 .name = "saa7134-video",
2046 .fops = &video_fops,
2047 .ioctl_ops = &video_ioctl_ops,
2048 .tvnorms = SAA7134_NORMS,
2051 struct video_device saa7134_radio_template = {
2052 .name = "saa7134-radio",
2053 .fops = &radio_fops,
2054 .ioctl_ops = &radio_ioctl_ops,
2057 static const struct v4l2_ctrl_ops saa7134_ctrl_ops = {
2058 .s_ctrl = saa7134_s_ctrl,
2061 static const struct v4l2_ctrl_config saa7134_ctrl_invert = {
2062 .ops = &saa7134_ctrl_ops,
2063 .id = V4L2_CID_PRIVATE_INVERT,
2065 .type = V4L2_CTRL_TYPE_BOOLEAN,
2071 static const struct v4l2_ctrl_config saa7134_ctrl_y_odd = {
2072 .ops = &saa7134_ctrl_ops,
2073 .id = V4L2_CID_PRIVATE_Y_ODD,
2074 .name = "Y Offset Odd Field",
2075 .type = V4L2_CTRL_TYPE_INTEGER,
2081 static const struct v4l2_ctrl_config saa7134_ctrl_y_even = {
2082 .ops = &saa7134_ctrl_ops,
2083 .id = V4L2_CID_PRIVATE_Y_EVEN,
2084 .name = "Y Offset Even Field",
2085 .type = V4L2_CTRL_TYPE_INTEGER,
2091 static const struct v4l2_ctrl_config saa7134_ctrl_automute = {
2092 .ops = &saa7134_ctrl_ops,
2093 .id = V4L2_CID_PRIVATE_AUTOMUTE,
2095 .type = V4L2_CTRL_TYPE_BOOLEAN,
2102 int saa7134_video_init1(struct saa7134_dev *dev)
2104 struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
2105 struct vb2_queue *q;
2108 /* sanitycheck insmod options */
2109 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2111 if (gbufsize > gbufsize_max)
2112 gbufsize = gbufsize_max;
2113 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2115 v4l2_ctrl_handler_init(hdl, 11);
2116 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2117 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
2118 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2119 V4L2_CID_CONTRAST, 0, 127, 1, 68);
2120 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2121 V4L2_CID_SATURATION, 0, 127, 1, 64);
2122 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2123 V4L2_CID_HUE, -128, 127, 1, 0);
2124 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2125 V4L2_CID_HFLIP, 0, 1, 1, 0);
2126 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2127 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
2128 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2129 V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
2130 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_invert, NULL);
2131 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_odd, NULL);
2132 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_even, NULL);
2133 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_automute, NULL);
2136 if (card_has_radio(dev)) {
2137 hdl = &dev->radio_ctrl_handler;
2138 v4l2_ctrl_handler_init(hdl, 2);
2139 v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler,
2140 v4l2_ctrl_radio_filter);
2146 if (dev->tda9887_conf && saa7134_ctrl_automute.def)
2147 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2150 INIT_LIST_HEAD(&dev->video_q.queue);
2151 timer_setup(&dev->video_q.timeout, saa7134_buffer_timeout, 0);
2152 dev->video_q.dev = dev;
2153 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
2156 dev->field = V4L2_FIELD_INTERLACED;
2157 dev->win.w.width = dev->width;
2158 dev->win.w.height = dev->height;
2159 dev->win.field = V4L2_FIELD_INTERLACED;
2160 dev->ovbuf.fmt.width = dev->width;
2161 dev->ovbuf.fmt.height = dev->height;
2162 dev->ovbuf.fmt.pixelformat = dev->fmt->fourcc;
2163 dev->ovbuf.fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
2165 if (saa7134_boards[dev->board].video_out)
2166 saa7134_videoport_init(dev);
2168 q = &dev->video_vbq;
2169 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2171 * Do not add VB2_USERPTR unless explicitly requested: the saa7134 DMA
2172 * engine cannot handle transfers that do not start at the beginning
2173 * of a page. A user-provided pointer can start anywhere in a page, so
2174 * USERPTR support is a no-go unless the application knows about these
2175 * limitations and has special support for this.
2177 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2178 if (saa7134_userptr)
2179 q->io_modes |= VB2_USERPTR;
2180 q->drv_priv = &dev->video_q;
2182 q->gfp_flags = GFP_DMA32;
2183 q->mem_ops = &vb2_dma_sg_memops;
2184 q->buf_struct_size = sizeof(struct saa7134_buf);
2185 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2186 q->lock = &dev->lock;
2187 q->dev = &dev->pci->dev;
2188 ret = vb2_queue_init(q);
2191 saa7134_pgtable_alloc(dev->pci, &dev->video_q.pt);
2194 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
2195 /* Don't add VB2_USERPTR, see comment above */
2196 q->io_modes = VB2_MMAP | VB2_READ;
2197 if (saa7134_userptr)
2198 q->io_modes |= VB2_USERPTR;
2199 q->drv_priv = &dev->vbi_q;
2200 q->ops = &saa7134_vbi_qops;
2201 q->gfp_flags = GFP_DMA32;
2202 q->mem_ops = &vb2_dma_sg_memops;
2203 q->buf_struct_size = sizeof(struct saa7134_buf);
2204 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2205 q->lock = &dev->lock;
2206 q->dev = &dev->pci->dev;
2207 ret = vb2_queue_init(q);
2210 saa7134_pgtable_alloc(dev->pci, &dev->vbi_q.pt);
2215 void saa7134_video_fini(struct saa7134_dev *dev)
2218 vb2_queue_release(&dev->video_vbq);
2219 saa7134_pgtable_free(dev->pci, &dev->video_q.pt);
2220 vb2_queue_release(&dev->vbi_vbq);
2221 saa7134_pgtable_free(dev->pci, &dev->vbi_q.pt);
2222 v4l2_ctrl_handler_free(&dev->ctrl_handler);
2223 if (card_has_radio(dev))
2224 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
2227 int saa7134_videoport_init(struct saa7134_dev *dev)
2229 /* enable video output */
2230 int vo = saa7134_boards[dev->board].video_out;
2232 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
2234 /* Configure videoport */
2235 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2236 video_reg = video_out[vo][1];
2237 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2238 video_reg &= ~VP_T_CODE_P_INVERTED;
2239 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2240 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2241 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2242 video_reg = video_out[vo][5];
2243 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2244 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2245 if (vid_port_opts & SET_CLOCK_INVERTED)
2246 video_reg |= VP_CLK_CTRL1_INVERTED;
2247 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2248 video_reg = video_out[vo][6];
2249 if (vid_port_opts & SET_VSYNC_OFF) {
2250 video_reg &= ~VP_VS_TYPE_MASK;
2251 video_reg |= VP_VS_TYPE_OFF;
2253 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2254 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2255 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2257 /* Start videoport */
2258 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2263 int saa7134_video_init2(struct saa7134_dev *dev)
2266 set_tvnorm(dev,&tvnorms[0]);
2268 v4l2_ctrl_handler_setup(&dev->ctrl_handler);
2269 saa7134_tvaudio_setmute(dev);
2270 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2274 void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
2276 static const char *st[] = {
2277 "(no signal)", "NTSC", "PAL", "SECAM" };
2280 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2281 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2282 video_dbg("DCSDT: pll: %s, sync: %s, norm: %s\n",
2283 (st1 & 0x40) ? "not locked" : "locked",
2284 (st2 & 0x40) ? "no" : "yes",
2286 dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
2288 if (dev->nosignal) {
2289 /* no video signal -> mute audio */
2290 if (dev->ctl_automute)
2292 saa7134_tvaudio_setmute(dev);
2294 /* wake up tvaudio audio carrier scan thread */
2295 saa7134_tvaudio_do_scan(dev);
2298 if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2299 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2301 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2303 if (dev->mops && dev->mops->signal_change)
2304 dev->mops->signal_change(dev);
2308 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2310 enum v4l2_field field;
2312 spin_lock(&dev->slock);
2313 if (dev->video_q.curr) {
2315 if (V4L2_FIELD_HAS_BOTH(field)) {
2316 /* make sure we have seen both fields */
2317 if ((status & 0x10) == 0x00) {
2318 dev->video_q.curr->top_seen = 1;
2321 if (!dev->video_q.curr->top_seen)
2323 } else if (field == V4L2_FIELD_TOP) {
2324 if ((status & 0x10) != 0x10)
2326 } else if (field == V4L2_FIELD_BOTTOM) {
2327 if ((status & 0x10) != 0x00)
2330 saa7134_buffer_finish(dev, &dev->video_q, VB2_BUF_STATE_DONE);
2332 saa7134_buffer_next(dev, &dev->video_q);
2335 spin_unlock(&dev->slock);