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.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/sort.h>
30 #include <media/v4l2-common.h>
31 #include <media/v4l2-event.h>
32 #include <media/saa6588.h>
34 #include "saa7134-reg.h"
37 /* ------------------------------------------------------------------ */
39 unsigned int video_debug;
40 static unsigned int gbuffers = 8;
41 static unsigned int noninterlaced; /* 0 */
42 static unsigned int gbufsize = 720*576*4;
43 static unsigned int gbufsize_max = 720*576*4;
44 static char secam[] = "--";
45 module_param(video_debug, int, 0644);
46 MODULE_PARM_DESC(video_debug,"enable debug messages [video]");
47 module_param(gbuffers, int, 0444);
48 MODULE_PARM_DESC(gbuffers,"number of capture buffers, range 2-32");
49 module_param(noninterlaced, int, 0644);
50 MODULE_PARM_DESC(noninterlaced,"capture non interlaced video");
51 module_param_string(secam, secam, sizeof(secam), 0644);
52 MODULE_PARM_DESC(secam, "force SECAM variant, either DK,L or Lc");
55 #define dprintk(fmt, arg...) if (video_debug&0x04) \
56 printk(KERN_DEBUG "%s/video: " fmt, dev->name , ## arg)
58 /* ------------------------------------------------------------------ */
59 /* Defines for Video Output Port Register at address 0x191 */
61 /* Bit 0: VIP code T bit polarity */
63 #define VP_T_CODE_P_NON_INVERTED 0x00
64 #define VP_T_CODE_P_INVERTED 0x01
66 /* ------------------------------------------------------------------ */
67 /* Defines for Video Output Port Register at address 0x195 */
69 /* Bit 2: Video output clock delay control */
71 #define VP_CLK_CTRL2_NOT_DELAYED 0x00
72 #define VP_CLK_CTRL2_DELAYED 0x04
74 /* Bit 1: Video output clock invert control */
76 #define VP_CLK_CTRL1_NON_INVERTED 0x00
77 #define VP_CLK_CTRL1_INVERTED 0x02
79 /* ------------------------------------------------------------------ */
80 /* Defines for Video Output Port Register at address 0x196 */
82 /* Bits 2 to 0: VSYNC pin video vertical sync type */
84 #define VP_VS_TYPE_MASK 0x07
86 #define VP_VS_TYPE_OFF 0x00
87 #define VP_VS_TYPE_V123 0x01
88 #define VP_VS_TYPE_V_ITU 0x02
89 #define VP_VS_TYPE_VGATE_L 0x03
90 #define VP_VS_TYPE_RESERVED1 0x04
91 #define VP_VS_TYPE_RESERVED2 0x05
92 #define VP_VS_TYPE_F_ITU 0x06
93 #define VP_VS_TYPE_SC_FID 0x07
95 /* ------------------------------------------------------------------ */
96 /* data structs for video */
98 static int video_out[][9] = {
99 [CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
102 static struct saa7134_format formats[] = {
104 .name = "8 bpp gray",
105 .fourcc = V4L2_PIX_FMT_GREY,
109 .name = "15 bpp RGB, le",
110 .fourcc = V4L2_PIX_FMT_RGB555,
114 .name = "15 bpp RGB, be",
115 .fourcc = V4L2_PIX_FMT_RGB555X,
120 .name = "16 bpp RGB, le",
121 .fourcc = V4L2_PIX_FMT_RGB565,
125 .name = "16 bpp RGB, be",
126 .fourcc = V4L2_PIX_FMT_RGB565X,
131 .name = "24 bpp RGB, le",
132 .fourcc = V4L2_PIX_FMT_BGR24,
136 .name = "24 bpp RGB, be",
137 .fourcc = V4L2_PIX_FMT_RGB24,
142 .name = "32 bpp RGB, le",
143 .fourcc = V4L2_PIX_FMT_BGR32,
147 .name = "32 bpp RGB, be",
148 .fourcc = V4L2_PIX_FMT_RGB32,
154 .name = "4:2:2 packed, YUYV",
155 .fourcc = V4L2_PIX_FMT_YUYV,
161 .name = "4:2:2 packed, UYVY",
162 .fourcc = V4L2_PIX_FMT_UYVY,
167 .name = "4:2:2 planar, Y-Cb-Cr",
168 .fourcc = V4L2_PIX_FMT_YUV422P,
176 .name = "4:2:0 planar, Y-Cb-Cr",
177 .fourcc = V4L2_PIX_FMT_YUV420,
185 .name = "4:2:0 planar, Y-Cb-Cr",
186 .fourcc = V4L2_PIX_FMT_YVU420,
196 #define FORMATS ARRAY_SIZE(formats)
198 #define NORM_625_50 \
201 .video_v_start = 24, \
202 .video_v_stop = 311, \
203 .vbi_v_start_0 = 7, \
204 .vbi_v_stop_0 = 22, \
205 .vbi_v_start_1 = 319, \
208 #define NORM_525_60 \
211 .video_v_start = 23, \
212 .video_v_stop = 262, \
213 .vbi_v_start_0 = 10, \
214 .vbi_v_stop_0 = 21, \
215 .vbi_v_start_1 = 273, \
218 static struct saa7134_tvnorm tvnorms[] = {
220 .name = "PAL", /* autodetect */
224 .sync_control = 0x18,
225 .luma_control = 0x40,
226 .chroma_ctrl1 = 0x81,
228 .chroma_ctrl2 = 0x06,
233 .id = V4L2_STD_PAL_BG,
236 .sync_control = 0x18,
237 .luma_control = 0x40,
238 .chroma_ctrl1 = 0x81,
240 .chroma_ctrl2 = 0x06,
245 .id = V4L2_STD_PAL_I,
248 .sync_control = 0x18,
249 .luma_control = 0x40,
250 .chroma_ctrl1 = 0x81,
252 .chroma_ctrl2 = 0x06,
257 .id = V4L2_STD_PAL_DK,
260 .sync_control = 0x18,
261 .luma_control = 0x40,
262 .chroma_ctrl1 = 0x81,
264 .chroma_ctrl2 = 0x06,
272 .sync_control = 0x59,
273 .luma_control = 0x40,
274 .chroma_ctrl1 = 0x89,
276 .chroma_ctrl2 = 0x0e,
281 .id = V4L2_STD_SECAM,
284 .sync_control = 0x18,
285 .luma_control = 0x1b,
286 .chroma_ctrl1 = 0xd1,
288 .chroma_ctrl2 = 0x00,
293 .id = V4L2_STD_SECAM_DK,
296 .sync_control = 0x18,
297 .luma_control = 0x1b,
298 .chroma_ctrl1 = 0xd1,
300 .chroma_ctrl2 = 0x00,
305 .id = V4L2_STD_SECAM_L,
308 .sync_control = 0x18,
309 .luma_control = 0x1b,
310 .chroma_ctrl1 = 0xd1,
312 .chroma_ctrl2 = 0x00,
317 .id = V4L2_STD_SECAM_LC,
320 .sync_control = 0x18,
321 .luma_control = 0x1b,
322 .chroma_ctrl1 = 0xd1,
324 .chroma_ctrl2 = 0x00,
329 .id = V4L2_STD_PAL_M,
332 .sync_control = 0x59,
333 .luma_control = 0x40,
334 .chroma_ctrl1 = 0xb9,
336 .chroma_ctrl2 = 0x0e,
341 .id = V4L2_STD_PAL_Nc,
344 .sync_control = 0x18,
345 .luma_control = 0x40,
346 .chroma_ctrl1 = 0xa1,
348 .chroma_ctrl2 = 0x06,
353 .id = V4L2_STD_PAL_60,
361 .vbi_v_start_1 = 273,
364 .sync_control = 0x18,
365 .luma_control = 0x40,
366 .chroma_ctrl1 = 0x81,
368 .chroma_ctrl2 = 0x06,
372 #define TVNORMS ARRAY_SIZE(tvnorms)
374 static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
378 for (i = 0; i < FORMATS; i++)
379 if (formats[i].fourcc == fourcc)
384 /* ------------------------------------------------------------------ */
386 static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
388 dprintk("set tv norm = %s\n",norm->name);
392 dev->crop_bounds.left = norm->h_start;
393 dev->crop_defrect.left = norm->h_start;
394 dev->crop_bounds.width = norm->h_stop - norm->h_start +1;
395 dev->crop_defrect.width = norm->h_stop - norm->h_start +1;
397 dev->crop_bounds.top = (norm->vbi_v_stop_0+1)*2;
398 dev->crop_defrect.top = norm->video_v_start*2;
399 dev->crop_bounds.height = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
400 - dev->crop_bounds.top;
401 dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
403 dev->crop_current = dev->crop_defrect;
405 saa7134_set_tvnorm_hw(dev);
408 static void video_mux(struct saa7134_dev *dev, int input)
410 dprintk("video input = %d [%s]\n", input, card_in(dev, input).name);
411 dev->ctl_input = input;
412 set_tvnorm(dev, dev->tvnorm);
413 saa7134_tvaudio_setinput(dev, &card_in(dev, input));
417 static void saa7134_set_decoder(struct saa7134_dev *dev)
419 int luma_control, sync_control, chroma_ctrl1, mux;
421 struct saa7134_tvnorm *norm = dev->tvnorm;
422 mux = card_in(dev, dev->ctl_input).vmux;
424 luma_control = norm->luma_control;
425 sync_control = norm->sync_control;
426 chroma_ctrl1 = norm->chroma_ctrl1;
429 luma_control |= 0x80; /* svideo */
430 if (noninterlaced || dev->nosignal)
431 sync_control |= 0x20;
433 /* switch on auto standard detection */
434 sync_control |= SAA7134_SYNC_CTRL_AUFD;
435 chroma_ctrl1 |= SAA7134_CHROMA_CTRL1_AUTO0;
436 chroma_ctrl1 &= ~SAA7134_CHROMA_CTRL1_FCTC;
437 luma_control &= ~SAA7134_LUMA_CTRL_LDEL;
439 /* setup video decoder */
440 saa_writeb(SAA7134_INCR_DELAY, 0x08);
441 saa_writeb(SAA7134_ANALOG_IN_CTRL1, 0xc0 | mux);
442 saa_writeb(SAA7134_ANALOG_IN_CTRL2, 0x00);
444 saa_writeb(SAA7134_ANALOG_IN_CTRL3, 0x90);
445 saa_writeb(SAA7134_ANALOG_IN_CTRL4, 0x90);
446 saa_writeb(SAA7134_HSYNC_START, 0xeb);
447 saa_writeb(SAA7134_HSYNC_STOP, 0xe0);
448 saa_writeb(SAA7134_SOURCE_TIMING1, norm->src_timing);
450 saa_writeb(SAA7134_SYNC_CTRL, sync_control);
451 saa_writeb(SAA7134_LUMA_CTRL, luma_control);
452 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, dev->ctl_bright);
454 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
455 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
457 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
458 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
460 saa_writeb(SAA7134_DEC_CHROMA_HUE, dev->ctl_hue);
461 saa_writeb(SAA7134_CHROMA_CTRL1, chroma_ctrl1);
462 saa_writeb(SAA7134_CHROMA_GAIN, norm->chroma_gain);
464 saa_writeb(SAA7134_CHROMA_CTRL2, norm->chroma_ctrl2);
465 saa_writeb(SAA7134_MODE_DELAY_CTRL, 0x00);
467 saa_writeb(SAA7134_ANALOG_ADC, 0x01);
468 saa_writeb(SAA7134_VGATE_START, 0x11);
469 saa_writeb(SAA7134_VGATE_STOP, 0xfe);
470 saa_writeb(SAA7134_MISC_VGATE_MSB, norm->vgate_misc);
471 saa_writeb(SAA7134_RAW_DATA_GAIN, 0x40);
472 saa_writeb(SAA7134_RAW_DATA_OFFSET, 0x80);
475 void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
477 saa7134_set_decoder(dev);
479 if (card_in(dev, dev->ctl_input).tv)
480 saa_call_all(dev, video, s_std, dev->tvnorm->id);
481 /* Set the correct norm for the saa6752hs. This function
482 does nothing if there is no saa6752hs. */
483 saa_call_empress(dev, video, s_std, dev->tvnorm->id);
486 static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
488 static const struct {
495 /* XPSC XACL XC2_1 XDCG VPFY */
507 static const int count = ARRAY_SIZE(vals);
510 for (i = 0; i < count; i++)
511 if (vals[i].xpsc == prescale)
516 saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
517 saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
518 saa_writeb(SAA7134_LEVEL_CTRL(task),
519 (vals[i].xc2_1 << 3) | (vals[i].xdcg));
520 saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
521 (vals[i].vpfy << 2) | vals[i].vpfy);
524 static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
528 saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale & 0xff);
529 saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
531 mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
534 dprintk("yscale LPI yscale=%d\n",yscale);
535 saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
536 saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
537 saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
540 val = 0x40 * 1024 / yscale;
541 dprintk("yscale ACM yscale=%d val=0x%x\n",yscale,val);
542 saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
543 saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
544 saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
546 saa_writeb(SAA7134_LUMA_BRIGHT(task), 0x80);
549 static void set_size(struct saa7134_dev *dev, int task,
550 int width, int height, int interlace)
552 int prescale,xscale,yscale,y_even,y_odd;
553 int h_start, h_stop, v_start, v_stop;
554 int div = interlace ? 2 : 1;
556 /* setup video scaler */
557 h_start = dev->crop_current.left;
558 v_start = dev->crop_current.top/2;
559 h_stop = (dev->crop_current.left + dev->crop_current.width -1);
560 v_stop = (dev->crop_current.top + dev->crop_current.height -1)/2;
562 saa_writeb(SAA7134_VIDEO_H_START1(task), h_start & 0xff);
563 saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
564 saa_writeb(SAA7134_VIDEO_H_STOP1(task), h_stop & 0xff);
565 saa_writeb(SAA7134_VIDEO_H_STOP2(task), h_stop >> 8);
566 saa_writeb(SAA7134_VIDEO_V_START1(task), v_start & 0xff);
567 saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
568 saa_writeb(SAA7134_VIDEO_V_STOP1(task), v_stop & 0xff);
569 saa_writeb(SAA7134_VIDEO_V_STOP2(task), v_stop >> 8);
571 prescale = dev->crop_current.width / width;
574 xscale = 1024 * dev->crop_current.width / prescale / width;
575 yscale = 512 * div * dev->crop_current.height / height;
576 dprintk("prescale=%d xscale=%d yscale=%d\n",prescale,xscale,yscale);
577 set_h_prescale(dev,task,prescale);
578 saa_writeb(SAA7134_H_SCALE_INC1(task), xscale & 0xff);
579 saa_writeb(SAA7134_H_SCALE_INC2(task), xscale >> 8);
580 set_v_scale(dev,task,yscale);
582 saa_writeb(SAA7134_VIDEO_PIXELS1(task), width & 0xff);
583 saa_writeb(SAA7134_VIDEO_PIXELS2(task), width >> 8);
584 saa_writeb(SAA7134_VIDEO_LINES1(task), height/div & 0xff);
585 saa_writeb(SAA7134_VIDEO_LINES2(task), height/div >> 8);
587 /* deinterlace y offsets */
588 y_odd = dev->ctl_y_odd;
589 y_even = dev->ctl_y_even;
590 saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
591 saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
592 saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
593 saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
596 /* ------------------------------------------------------------------ */
604 static void set_cliplist(struct saa7134_dev *dev, int reg,
605 struct cliplist *cl, int entries, char *name)
610 for (i = 0; i < entries; i++) {
611 winbits |= cl[i].enable;
612 winbits &= ~cl[i].disable;
613 if (i < 15 && cl[i].position == cl[i+1].position)
615 saa_writeb(reg + 0, winbits);
616 saa_writeb(reg + 2, cl[i].position & 0xff);
617 saa_writeb(reg + 3, cl[i].position >> 8);
618 dprintk("clip: %s winbits=%02x pos=%d\n",
619 name,winbits,cl[i].position);
622 for (; reg < 0x400; reg += 8) {
623 saa_writeb(reg+ 0, 0);
624 saa_writeb(reg + 1, 0);
625 saa_writeb(reg + 2, 0);
626 saa_writeb(reg + 3, 0);
630 static int clip_range(int val)
637 /* Sort into smallest position first order */
638 static int cliplist_cmp(const void *a, const void *b)
640 const struct cliplist *cla = a;
641 const struct cliplist *clb = b;
642 if (cla->position < clb->position)
644 if (cla->position > clb->position)
649 static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
650 int nclips, int interlace)
652 struct cliplist col[16], row[16];
653 int cols = 0, rows = 0, i;
654 int div = interlace ? 2 : 1;
656 memset(col, 0, sizeof(col));
657 memset(row, 0, sizeof(row));
658 for (i = 0; i < nclips && i < 8; i++) {
659 col[cols].position = clip_range(clips[i].c.left);
660 col[cols].enable = (1 << i);
662 col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
663 col[cols].disable = (1 << i);
665 row[rows].position = clip_range(clips[i].c.top / div);
666 row[rows].enable = (1 << i);
668 row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
670 row[rows].disable = (1 << i);
673 sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
674 sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
675 set_cliplist(dev,0x380,col,cols,"cols");
676 set_cliplist(dev,0x384,row,rows,"rows");
680 static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win, bool try)
682 enum v4l2_field field;
685 if (!try && (dev->ovbuf.base == NULL || dev->ovfmt == NULL))
687 if (win->w.width < 48)
689 if (win->w.height < 32)
691 if (win->clipcount > 8)
695 win->global_alpha = 0;
697 maxw = dev->crop_current.width;
698 maxh = dev->crop_current.height;
700 if (V4L2_FIELD_ANY == field) {
701 field = (win->w.height > maxh/2)
702 ? V4L2_FIELD_INTERLACED
707 case V4L2_FIELD_BOTTOM:
711 field = V4L2_FIELD_INTERLACED;
716 if (win->w.width > maxw)
718 if (win->w.height > maxh)
719 win->w.height = maxh;
723 static int start_preview(struct saa7134_dev *dev)
725 unsigned long base,control,bpl;
728 err = verify_preview(dev, &dev->win, false);
732 dev->ovfield = dev->win.field;
733 dprintk("start_preview %dx%d+%d+%d %s field=%s\n",
734 dev->win.w.width, dev->win.w.height,
735 dev->win.w.left, dev->win.w.top,
736 dev->ovfmt->name, v4l2_field_names[dev->ovfield]);
738 /* setup window + clipping */
739 set_size(dev, TASK_B, dev->win.w.width, dev->win.w.height,
740 V4L2_FIELD_HAS_BOTH(dev->ovfield));
741 setup_clipping(dev, dev->clips, dev->nclips,
742 V4L2_FIELD_HAS_BOTH(dev->ovfield));
744 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
746 saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
747 saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
749 /* dma: setup channel 1 (= Video Task B) */
750 base = (unsigned long)dev->ovbuf.base;
751 base += dev->ovbuf.fmt.bytesperline * dev->win.w.top;
752 base += dev->ovfmt->depth/8 * dev->win.w.left;
753 bpl = dev->ovbuf.fmt.bytesperline;
754 control = SAA7134_RS_CONTROL_BURST_16;
755 if (dev->ovfmt->bswap)
756 control |= SAA7134_RS_CONTROL_BSWAP;
757 if (dev->ovfmt->wswap)
758 control |= SAA7134_RS_CONTROL_WSWAP;
759 if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
760 saa_writel(SAA7134_RS_BA1(1),base);
761 saa_writel(SAA7134_RS_BA2(1),base+bpl);
762 saa_writel(SAA7134_RS_PITCH(1),bpl*2);
763 saa_writel(SAA7134_RS_CONTROL(1),control);
765 saa_writel(SAA7134_RS_BA1(1),base);
766 saa_writel(SAA7134_RS_BA2(1),base);
767 saa_writel(SAA7134_RS_PITCH(1),bpl);
768 saa_writel(SAA7134_RS_CONTROL(1),control);
773 saa7134_set_dmabits(dev);
778 static int stop_preview(struct saa7134_dev *dev)
781 saa7134_set_dmabits(dev);
785 /* ------------------------------------------------------------------ */
787 static int buffer_activate(struct saa7134_dev *dev,
788 struct saa7134_buf *buf,
789 struct saa7134_buf *next)
791 struct saa7134_dmaqueue *dmaq = buf->vb2.vb2_queue->drv_priv;
792 unsigned long base,control,bpl;
793 unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
795 dprintk("buffer_activate buf=%p\n",buf);
798 set_size(dev, TASK_A, dev->width, dev->height,
799 V4L2_FIELD_HAS_BOTH(dev->field));
801 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
803 saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
804 saa_writeb(SAA7134_OFMT_VIDEO_A, dev->fmt->pm);
806 /* DMA: setup channel 0 (= Video Task A0) */
807 base = saa7134_buffer_base(buf);
808 if (dev->fmt->planar)
811 bpl = (dev->width * dev->fmt->depth) / 8;
812 control = SAA7134_RS_CONTROL_BURST_16 |
813 SAA7134_RS_CONTROL_ME |
814 (dmaq->pt.dma >> 12);
816 control |= SAA7134_RS_CONTROL_BSWAP;
818 control |= SAA7134_RS_CONTROL_WSWAP;
819 if (V4L2_FIELD_HAS_BOTH(dev->field)) {
821 saa_writel(SAA7134_RS_BA1(0),base);
822 saa_writel(SAA7134_RS_BA2(0),base+bpl);
823 saa_writel(SAA7134_RS_PITCH(0),bpl*2);
826 saa_writel(SAA7134_RS_BA1(0),base);
827 saa_writel(SAA7134_RS_BA2(0),base);
828 saa_writel(SAA7134_RS_PITCH(0),bpl);
830 saa_writel(SAA7134_RS_CONTROL(0),control);
832 if (dev->fmt->planar) {
833 /* DMA: setup channel 4+5 (= planar task A) */
834 bpl_uv = bpl >> dev->fmt->hshift;
835 lines_uv = dev->height >> dev->fmt->vshift;
836 base2 = base + bpl * dev->height;
837 base3 = base2 + bpl_uv * lines_uv;
838 if (dev->fmt->uvswap)
839 tmp = base2, base2 = base3, base3 = tmp;
840 dprintk("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
841 bpl_uv,lines_uv,base2,base3);
842 if (V4L2_FIELD_HAS_BOTH(dev->field)) {
844 saa_writel(SAA7134_RS_BA1(4),base2);
845 saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
846 saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
847 saa_writel(SAA7134_RS_BA1(5),base3);
848 saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
849 saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
852 saa_writel(SAA7134_RS_BA1(4),base2);
853 saa_writel(SAA7134_RS_BA2(4),base2);
854 saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
855 saa_writel(SAA7134_RS_BA1(5),base3);
856 saa_writel(SAA7134_RS_BA2(5),base3);
857 saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
859 saa_writel(SAA7134_RS_CONTROL(4),control);
860 saa_writel(SAA7134_RS_CONTROL(5),control);
864 saa7134_set_dmabits(dev);
865 mod_timer(&dmaq->timeout, jiffies + BUFFER_TIMEOUT);
869 static int buffer_init(struct vb2_buffer *vb2)
871 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
872 struct saa7134_buf *buf = container_of(vb2, struct saa7134_buf, vb2);
875 buf->activate = buffer_activate;
879 static int buffer_prepare(struct vb2_buffer *vb2)
881 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
882 struct saa7134_dev *dev = dmaq->dev;
883 struct saa7134_buf *buf = container_of(vb2, struct saa7134_buf, vb2);
884 struct sg_table *dma = vb2_dma_sg_plane_desc(&buf->vb2, 0);
888 if (dma->sgl->offset) {
889 pr_err("The buffer is not page-aligned\n");
892 size = (dev->width * dev->height * dev->fmt->depth) >> 3;
893 if (vb2_plane_size(vb2, 0) < size)
896 vb2_set_plane_payload(vb2, 0, size);
897 vb2->v4l2_buf.field = dev->field;
899 ret = dma_map_sg(&dev->pci->dev, dma->sgl, dma->nents, DMA_FROM_DEVICE);
902 return saa7134_pgtable_build(dev->pci, &dmaq->pt, dma->sgl, dma->nents,
903 saa7134_buffer_startpage(buf));
906 static void buffer_finish(struct vb2_buffer *vb2)
908 struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
909 struct saa7134_dev *dev = dmaq->dev;
910 struct saa7134_buf *buf = container_of(vb2, struct saa7134_buf, vb2);
911 struct sg_table *dma = vb2_dma_sg_plane_desc(&buf->vb2, 0);
913 dma_unmap_sg(&dev->pci->dev, dma->sgl, dma->nents, DMA_FROM_DEVICE);
916 static int queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
917 unsigned int *nbuffers, unsigned int *nplanes,
918 unsigned int sizes[], void *alloc_ctxs[])
920 struct saa7134_dmaqueue *dmaq = q->drv_priv;
921 struct saa7134_dev *dev = dmaq->dev;
922 int size = dev->fmt->depth * dev->width * dev->height >> 3;
924 if (dev->width < 48 ||
926 dev->width/4 > dev->crop_current.width ||
927 dev->height/4 > dev->crop_current.height ||
928 dev->width > dev->crop_bounds.width ||
929 dev->height > dev->crop_bounds.height)
932 *nbuffers = saa7134_buffer_count(size, *nbuffers);
939 * move buffer to hardware queue
941 void saa7134_vb2_buffer_queue(struct vb2_buffer *vb)
943 struct saa7134_dmaqueue *dmaq = vb->vb2_queue->drv_priv;
944 struct saa7134_dev *dev = dmaq->dev;
945 struct saa7134_buf *buf = container_of(vb, struct saa7134_buf, vb2);
947 saa7134_buffer_queue(dev, dmaq, buf);
949 EXPORT_SYMBOL_GPL(saa7134_vb2_buffer_queue);
951 int saa7134_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
953 struct saa7134_dmaqueue *dmaq = vq->drv_priv;
954 struct saa7134_dev *dev = dmaq->dev;
957 * Planar video capture and TS share the same DMA channel,
958 * so only one can be active at a time.
960 if (card_is_empress(dev) && vb2_is_busy(&dev->empress_vbq) &&
961 dmaq == &dev->video_q && dev->fmt->planar) {
962 struct saa7134_buf *buf, *tmp;
964 list_for_each_entry_safe(buf, tmp, &dmaq->queue, entry) {
965 list_del(&buf->entry);
966 vb2_buffer_done(&buf->vb2, VB2_BUF_STATE_QUEUED);
969 vb2_buffer_done(&dmaq->curr->vb2, VB2_BUF_STATE_QUEUED);
975 /* The SAA7134 has a 1K FIFO; the datasheet suggests that when
976 * configured conservatively, there's 22 usec of buffering for video.
977 * We therefore request a DMA latency of 20 usec, giving us 2 usec of
978 * margin in case the FIFO is configured differently to the datasheet.
979 * Unfortunately, I lack register-level documentation to check the
980 * Linux FIFO setup and confirm the perfect value.
982 if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
983 (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
984 pm_qos_add_request(&dev->qos_request,
985 PM_QOS_CPU_DMA_LATENCY, 20);
991 void saa7134_vb2_stop_streaming(struct vb2_queue *vq)
993 struct saa7134_dmaqueue *dmaq = vq->drv_priv;
994 struct saa7134_dev *dev = dmaq->dev;
996 saa7134_stop_streaming(dev, dmaq);
998 if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
999 (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
1000 pm_qos_remove_request(&dev->qos_request);
1003 static struct vb2_ops vb2_qops = {
1004 .queue_setup = queue_setup,
1005 .buf_init = buffer_init,
1006 .buf_prepare = buffer_prepare,
1007 .buf_finish = buffer_finish,
1008 .buf_queue = saa7134_vb2_buffer_queue,
1009 .wait_prepare = vb2_ops_wait_prepare,
1010 .wait_finish = vb2_ops_wait_finish,
1011 .start_streaming = saa7134_vb2_start_streaming,
1012 .stop_streaming = saa7134_vb2_stop_streaming,
1015 /* ------------------------------------------------------------------ */
1017 static int saa7134_s_ctrl(struct v4l2_ctrl *ctrl)
1019 struct saa7134_dev *dev = container_of(ctrl->handler, struct saa7134_dev, ctrl_handler);
1020 unsigned long flags;
1021 int restart_overlay = 0;
1024 case V4L2_CID_BRIGHTNESS:
1025 dev->ctl_bright = ctrl->val;
1026 saa_writeb(SAA7134_DEC_LUMA_BRIGHT, ctrl->val);
1029 dev->ctl_hue = ctrl->val;
1030 saa_writeb(SAA7134_DEC_CHROMA_HUE, ctrl->val);
1032 case V4L2_CID_CONTRAST:
1033 dev->ctl_contrast = ctrl->val;
1034 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1035 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1037 case V4L2_CID_SATURATION:
1038 dev->ctl_saturation = ctrl->val;
1039 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1040 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1042 case V4L2_CID_AUDIO_MUTE:
1043 dev->ctl_mute = ctrl->val;
1044 saa7134_tvaudio_setmute(dev);
1046 case V4L2_CID_AUDIO_VOLUME:
1047 dev->ctl_volume = ctrl->val;
1048 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1050 case V4L2_CID_PRIVATE_INVERT:
1051 dev->ctl_invert = ctrl->val;
1052 saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1053 dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1054 saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1055 dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1057 case V4L2_CID_HFLIP:
1058 dev->ctl_mirror = ctrl->val;
1059 restart_overlay = 1;
1061 case V4L2_CID_PRIVATE_Y_EVEN:
1062 dev->ctl_y_even = ctrl->val;
1063 restart_overlay = 1;
1065 case V4L2_CID_PRIVATE_Y_ODD:
1066 dev->ctl_y_odd = ctrl->val;
1067 restart_overlay = 1;
1069 case V4L2_CID_PRIVATE_AUTOMUTE:
1071 struct v4l2_priv_tun_config tda9887_cfg;
1073 tda9887_cfg.tuner = TUNER_TDA9887;
1074 tda9887_cfg.priv = &dev->tda9887_conf;
1076 dev->ctl_automute = ctrl->val;
1077 if (dev->tda9887_conf) {
1078 if (dev->ctl_automute)
1079 dev->tda9887_conf |= TDA9887_AUTOMUTE;
1081 dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1083 saa_call_all(dev, tuner, s_config, &tda9887_cfg);
1090 if (restart_overlay && dev->overlay_owner) {
1091 spin_lock_irqsave(&dev->slock, flags);
1094 spin_unlock_irqrestore(&dev->slock, flags);
1099 /* ------------------------------------------------------------------ */
1101 static inline struct vb2_queue *saa7134_queue(struct file *file)
1103 return video_devdata(file)->queue;
1106 static int video_open(struct file *file)
1108 struct video_device *vdev = video_devdata(file);
1109 struct saa7134_dev *dev = video_drvdata(file);
1110 int ret = v4l2_fh_open(file);
1115 mutex_lock(&dev->lock);
1116 if (vdev->vfl_type == VFL_TYPE_RADIO) {
1117 /* switch to radio mode */
1118 saa7134_tvaudio_setinput(dev, &card(dev).radio);
1119 saa_call_all(dev, tuner, s_radio);
1121 /* switch to video/vbi mode */
1122 video_mux(dev, dev->ctl_input);
1124 mutex_unlock(&dev->lock);
1129 static int video_release(struct file *file)
1131 struct video_device *vdev = video_devdata(file);
1132 struct saa7134_dev *dev = video_drvdata(file);
1133 struct v4l2_fh *fh = file->private_data;
1134 struct saa6588_command cmd;
1135 unsigned long flags;
1137 mutex_lock(&dev->lock);
1138 saa7134_tvaudio_close(dev);
1140 /* turn off overlay */
1141 if (fh == dev->overlay_owner) {
1142 spin_lock_irqsave(&dev->slock,flags);
1144 spin_unlock_irqrestore(&dev->slock,flags);
1145 dev->overlay_owner = NULL;
1148 if (vdev->vfl_type == VFL_TYPE_RADIO)
1149 v4l2_fh_release(file);
1151 _vb2_fop_release(file, NULL);
1153 /* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1154 saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1155 saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1156 saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1157 saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1159 saa_call_all(dev, core, s_power, 0);
1160 if (vdev->vfl_type == VFL_TYPE_RADIO)
1161 saa_call_all(dev, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
1162 mutex_unlock(&dev->lock);
1167 static ssize_t radio_read(struct file *file, char __user *data,
1168 size_t count, loff_t *ppos)
1170 struct saa7134_dev *dev = video_drvdata(file);
1171 struct saa6588_command cmd;
1173 cmd.block_count = count/3;
1174 cmd.nonblocking = file->f_flags & O_NONBLOCK;
1176 cmd.instance = file;
1177 cmd.result = -ENODEV;
1179 mutex_lock(&dev->lock);
1180 saa_call_all(dev, core, ioctl, SAA6588_CMD_READ, &cmd);
1181 mutex_unlock(&dev->lock);
1186 static unsigned int radio_poll(struct file *file, poll_table *wait)
1188 struct saa7134_dev *dev = video_drvdata(file);
1189 struct saa6588_command cmd;
1190 unsigned int rc = v4l2_ctrl_poll(file, wait);
1192 cmd.instance = file;
1193 cmd.event_list = wait;
1195 mutex_lock(&dev->lock);
1196 saa_call_all(dev, core, ioctl, SAA6588_CMD_POLL, &cmd);
1197 mutex_unlock(&dev->lock);
1199 return rc | cmd.result;
1202 /* ------------------------------------------------------------------ */
1204 static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
1205 struct v4l2_format *f)
1207 struct saa7134_dev *dev = video_drvdata(file);
1208 struct saa7134_tvnorm *norm = dev->tvnorm;
1210 memset(&f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
1211 f->fmt.vbi.sampling_rate = 6750000 * 4;
1212 f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1213 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1214 f->fmt.vbi.offset = 64 * 4;
1215 f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1216 f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1217 f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1218 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1219 f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1224 static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
1225 struct v4l2_format *f)
1227 struct saa7134_dev *dev = video_drvdata(file);
1229 f->fmt.pix.width = dev->width;
1230 f->fmt.pix.height = dev->height;
1231 f->fmt.pix.field = dev->field;
1232 f->fmt.pix.pixelformat = dev->fmt->fourcc;
1233 f->fmt.pix.bytesperline =
1234 (f->fmt.pix.width * dev->fmt->depth) >> 3;
1235 f->fmt.pix.sizeimage =
1236 f->fmt.pix.height * f->fmt.pix.bytesperline;
1237 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1238 f->fmt.pix.priv = 0;
1242 static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
1243 struct v4l2_format *f)
1245 struct saa7134_dev *dev = video_drvdata(file);
1246 struct v4l2_clip __user *clips = f->fmt.win.clips;
1247 u32 clipcount = f->fmt.win.clipcount;
1251 if (saa7134_no_overlay > 0) {
1252 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1255 f->fmt.win = dev->win;
1256 f->fmt.win.clips = clips;
1259 if (dev->nclips < clipcount)
1260 clipcount = dev->nclips;
1261 f->fmt.win.clipcount = clipcount;
1263 for (i = 0; !err && i < clipcount; i++) {
1264 if (copy_to_user(&f->fmt.win.clips[i].c, &dev->clips[i].c,
1265 sizeof(struct v4l2_rect)))
1272 static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
1273 struct v4l2_format *f)
1275 struct saa7134_dev *dev = video_drvdata(file);
1276 struct saa7134_format *fmt;
1277 enum v4l2_field field;
1278 unsigned int maxw, maxh;
1280 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1284 field = f->fmt.pix.field;
1285 maxw = min(dev->crop_current.width*4, dev->crop_bounds.width);
1286 maxh = min(dev->crop_current.height*4, dev->crop_bounds.height);
1288 if (V4L2_FIELD_ANY == field) {
1289 field = (f->fmt.pix.height > maxh/2)
1290 ? V4L2_FIELD_INTERLACED
1291 : V4L2_FIELD_BOTTOM;
1294 case V4L2_FIELD_TOP:
1295 case V4L2_FIELD_BOTTOM:
1299 field = V4L2_FIELD_INTERLACED;
1303 f->fmt.pix.field = field;
1304 if (f->fmt.pix.width < 48)
1305 f->fmt.pix.width = 48;
1306 if (f->fmt.pix.height < 32)
1307 f->fmt.pix.height = 32;
1308 if (f->fmt.pix.width > maxw)
1309 f->fmt.pix.width = maxw;
1310 if (f->fmt.pix.height > maxh)
1311 f->fmt.pix.height = maxh;
1312 f->fmt.pix.width &= ~0x03;
1313 f->fmt.pix.bytesperline =
1314 (f->fmt.pix.width * fmt->depth) >> 3;
1315 f->fmt.pix.sizeimage =
1316 f->fmt.pix.height * f->fmt.pix.bytesperline;
1317 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1318 f->fmt.pix.priv = 0;
1323 static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
1324 struct v4l2_format *f)
1326 struct saa7134_dev *dev = video_drvdata(file);
1328 if (saa7134_no_overlay > 0) {
1329 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1333 if (f->fmt.win.clips == NULL)
1334 f->fmt.win.clipcount = 0;
1335 return verify_preview(dev, &f->fmt.win, true);
1338 static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
1339 struct v4l2_format *f)
1341 struct saa7134_dev *dev = video_drvdata(file);
1344 err = saa7134_try_fmt_vid_cap(file, priv, f);
1348 dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1349 dev->width = f->fmt.pix.width;
1350 dev->height = f->fmt.pix.height;
1351 dev->field = f->fmt.pix.field;
1355 static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
1356 struct v4l2_format *f)
1358 struct saa7134_dev *dev = video_drvdata(file);
1360 unsigned long flags;
1362 if (saa7134_no_overlay > 0) {
1363 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1366 if (f->fmt.win.clips == NULL)
1367 f->fmt.win.clipcount = 0;
1368 err = verify_preview(dev, &f->fmt.win, true);
1372 dev->win = f->fmt.win;
1373 dev->nclips = f->fmt.win.clipcount;
1375 if (copy_from_user(dev->clips, f->fmt.win.clips,
1376 sizeof(struct v4l2_clip) * dev->nclips))
1379 if (priv == dev->overlay_owner) {
1380 spin_lock_irqsave(&dev->slock, flags);
1383 spin_unlock_irqrestore(&dev->slock, flags);
1389 int saa7134_enum_input(struct file *file, void *priv, struct v4l2_input *i)
1391 struct saa7134_dev *dev = video_drvdata(file);
1395 if (n >= SAA7134_INPUT_MAX)
1397 if (NULL == card_in(dev, i->index).name)
1400 i->type = V4L2_INPUT_TYPE_CAMERA;
1401 strcpy(i->name, card_in(dev, n).name);
1402 if (card_in(dev, n).tv)
1403 i->type = V4L2_INPUT_TYPE_TUNER;
1404 if (n == dev->ctl_input) {
1405 int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1406 int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1408 if (0 != (v1 & 0x40))
1409 i->status |= V4L2_IN_ST_NO_H_LOCK;
1410 if (0 != (v2 & 0x40))
1411 i->status |= V4L2_IN_ST_NO_SIGNAL;
1412 if (0 != (v2 & 0x0e))
1413 i->status |= V4L2_IN_ST_MACROVISION;
1415 i->std = SAA7134_NORMS;
1418 EXPORT_SYMBOL_GPL(saa7134_enum_input);
1420 int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
1422 struct saa7134_dev *dev = video_drvdata(file);
1424 *i = dev->ctl_input;
1427 EXPORT_SYMBOL_GPL(saa7134_g_input);
1429 int saa7134_s_input(struct file *file, void *priv, unsigned int i)
1431 struct saa7134_dev *dev = video_drvdata(file);
1433 if (i >= SAA7134_INPUT_MAX)
1435 if (NULL == card_in(dev, i).name)
1440 EXPORT_SYMBOL_GPL(saa7134_s_input);
1442 int saa7134_querycap(struct file *file, void *priv,
1443 struct v4l2_capability *cap)
1445 struct saa7134_dev *dev = video_drvdata(file);
1446 struct video_device *vdev = video_devdata(file);
1447 u32 radio_caps, video_caps, vbi_caps;
1449 unsigned int tuner_type = dev->tuner_type;
1451 strcpy(cap->driver, "saa7134");
1452 strlcpy(cap->card, saa7134_boards[dev->board].name,
1454 sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1456 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1457 if ((tuner_type != TUNER_ABSENT) && (tuner_type != UNSET))
1458 cap->device_caps |= V4L2_CAP_TUNER;
1460 radio_caps = V4L2_CAP_RADIO;
1462 radio_caps |= V4L2_CAP_RDS_CAPTURE;
1464 video_caps = V4L2_CAP_VIDEO_CAPTURE;
1465 if (saa7134_no_overlay <= 0 && !is_empress(file))
1466 video_caps |= V4L2_CAP_VIDEO_OVERLAY;
1468 vbi_caps = V4L2_CAP_VBI_CAPTURE;
1470 switch (vdev->vfl_type) {
1471 case VFL_TYPE_RADIO:
1472 cap->device_caps |= radio_caps;
1474 case VFL_TYPE_GRABBER:
1475 cap->device_caps |= video_caps;
1478 cap->device_caps |= vbi_caps;
1481 cap->capabilities = radio_caps | video_caps | vbi_caps |
1482 cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1483 if (vdev->vfl_type == VFL_TYPE_RADIO) {
1484 cap->device_caps &= ~V4L2_CAP_STREAMING;
1486 cap->device_caps &= ~V4L2_CAP_READWRITE;
1491 EXPORT_SYMBOL_GPL(saa7134_querycap);
1493 int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id)
1495 struct saa7134_dev *dev = video_drvdata(file);
1496 struct v4l2_fh *fh = priv;
1497 unsigned long flags;
1501 if (is_empress(file) && dev->overlay_owner) {
1502 /* Don't change the std from the mpeg device
1503 if overlay is active. */
1507 for (i = 0; i < TVNORMS; i++)
1508 if (id == tvnorms[i].id)
1512 for (i = 0; i < TVNORMS; i++)
1513 if (id & tvnorms[i].id)
1518 if ((id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1519 if (secam[0] == 'L' || secam[0] == 'l') {
1520 if (secam[1] == 'C' || secam[1] == 'c')
1521 fixup = V4L2_STD_SECAM_LC;
1523 fixup = V4L2_STD_SECAM_L;
1525 if (secam[0] == 'D' || secam[0] == 'd')
1526 fixup = V4L2_STD_SECAM_DK;
1528 fixup = V4L2_STD_SECAM;
1530 for (i = 0; i < TVNORMS; i++) {
1531 if (fixup == tvnorms[i].id)
1540 if (!is_empress(file) && fh == dev->overlay_owner) {
1541 spin_lock_irqsave(&dev->slock, flags);
1543 spin_unlock_irqrestore(&dev->slock, flags);
1545 set_tvnorm(dev, &tvnorms[i]);
1547 spin_lock_irqsave(&dev->slock, flags);
1549 spin_unlock_irqrestore(&dev->slock, flags);
1551 set_tvnorm(dev, &tvnorms[i]);
1553 saa7134_tvaudio_do_scan(dev);
1556 EXPORT_SYMBOL_GPL(saa7134_s_std);
1558 int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
1560 struct saa7134_dev *dev = video_drvdata(file);
1562 *id = dev->tvnorm->id;
1565 EXPORT_SYMBOL_GPL(saa7134_g_std);
1567 static v4l2_std_id saa7134_read_std(struct saa7134_dev *dev)
1569 static v4l2_std_id stds[] = {
1575 v4l2_std_id result = 0;
1577 u8 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
1578 u8 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
1580 if (!(st2 & 0x1)) /* RDCAP == 0 */
1581 result = V4L2_STD_UNKNOWN;
1583 result = stds[st1 & 0x03];
1588 int saa7134_querystd(struct file *file, void *priv, v4l2_std_id *std)
1590 struct saa7134_dev *dev = video_drvdata(file);
1591 *std &= saa7134_read_std(dev);
1594 EXPORT_SYMBOL_GPL(saa7134_querystd);
1596 static int saa7134_cropcap(struct file *file, void *priv,
1597 struct v4l2_cropcap *cap)
1599 struct saa7134_dev *dev = video_drvdata(file);
1601 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1602 cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1604 cap->bounds = dev->crop_bounds;
1605 cap->defrect = dev->crop_defrect;
1606 cap->pixelaspect.numerator = 1;
1607 cap->pixelaspect.denominator = 1;
1608 if (dev->tvnorm->id & V4L2_STD_525_60) {
1609 cap->pixelaspect.numerator = 11;
1610 cap->pixelaspect.denominator = 10;
1612 if (dev->tvnorm->id & V4L2_STD_625_50) {
1613 cap->pixelaspect.numerator = 54;
1614 cap->pixelaspect.denominator = 59;
1619 static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
1621 struct saa7134_dev *dev = video_drvdata(file);
1623 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1624 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1626 crop->c = dev->crop_current;
1630 static int saa7134_s_crop(struct file *file, void *f, const struct v4l2_crop *crop)
1632 struct saa7134_dev *dev = video_drvdata(file);
1633 struct v4l2_rect *b = &dev->crop_bounds;
1634 struct v4l2_rect *c = &dev->crop_current;
1636 if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1637 crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1640 if (dev->overlay_owner)
1642 if (vb2_is_streaming(&dev->video_vbq))
1646 if (c->top < b->top)
1648 if (c->top > b->top + b->height)
1649 c->top = b->top + b->height;
1650 if (c->height > b->top - c->top + b->height)
1651 c->height = b->top - c->top + b->height;
1653 if (c->left < b->left)
1655 if (c->left > b->left + b->width)
1656 c->left = b->left + b->width;
1657 if (c->width > b->left - c->left + b->width)
1658 c->width = b->left - c->left + b->width;
1662 int saa7134_g_tuner(struct file *file, void *priv,
1663 struct v4l2_tuner *t)
1665 struct saa7134_dev *dev = video_drvdata(file);
1670 memset(t, 0, sizeof(*t));
1671 for (n = 0; n < SAA7134_INPUT_MAX; n++) {
1672 if (card_in(dev, n).tv)
1675 if (n == SAA7134_INPUT_MAX)
1677 if (NULL != card_in(dev, n).name) {
1678 strcpy(t->name, "Television");
1679 t->type = V4L2_TUNER_ANALOG_TV;
1680 saa_call_all(dev, tuner, g_tuner, t);
1681 t->capability = V4L2_TUNER_CAP_NORM |
1682 V4L2_TUNER_CAP_STEREO |
1683 V4L2_TUNER_CAP_LANG1 |
1684 V4L2_TUNER_CAP_LANG2;
1685 t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1686 t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1688 if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1692 EXPORT_SYMBOL_GPL(saa7134_g_tuner);
1694 int saa7134_s_tuner(struct file *file, void *priv,
1695 const struct v4l2_tuner *t)
1697 struct saa7134_dev *dev = video_drvdata(file);
1703 mode = dev->thread.mode;
1704 if (UNSET == mode) {
1705 rx = saa7134_tvaudio_getstereo(dev);
1706 mode = saa7134_tvaudio_rx2mode(rx);
1708 if (mode != t->audmode)
1709 dev->thread.mode = t->audmode;
1713 EXPORT_SYMBOL_GPL(saa7134_s_tuner);
1715 int saa7134_g_frequency(struct file *file, void *priv,
1716 struct v4l2_frequency *f)
1718 struct saa7134_dev *dev = video_drvdata(file);
1723 saa_call_all(dev, tuner, g_frequency, f);
1727 EXPORT_SYMBOL_GPL(saa7134_g_frequency);
1729 int saa7134_s_frequency(struct file *file, void *priv,
1730 const struct v4l2_frequency *f)
1732 struct saa7134_dev *dev = video_drvdata(file);
1737 saa_call_all(dev, tuner, s_frequency, f);
1739 saa7134_tvaudio_do_scan(dev);
1742 EXPORT_SYMBOL_GPL(saa7134_s_frequency);
1744 static int saa7134_enum_fmt_vid_cap(struct file *file, void *priv,
1745 struct v4l2_fmtdesc *f)
1747 if (f->index >= FORMATS)
1750 strlcpy(f->description, formats[f->index].name,
1751 sizeof(f->description));
1753 f->pixelformat = formats[f->index].fourcc;
1758 static int saa7134_enum_fmt_vid_overlay(struct file *file, void *priv,
1759 struct v4l2_fmtdesc *f)
1761 if (saa7134_no_overlay > 0) {
1762 printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1766 if ((f->index >= FORMATS) || formats[f->index].planar)
1769 strlcpy(f->description, formats[f->index].name,
1770 sizeof(f->description));
1772 f->pixelformat = formats[f->index].fourcc;
1777 static int saa7134_g_fbuf(struct file *file, void *f,
1778 struct v4l2_framebuffer *fb)
1780 struct saa7134_dev *dev = video_drvdata(file);
1783 fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
1788 static int saa7134_s_fbuf(struct file *file, void *f,
1789 const struct v4l2_framebuffer *fb)
1791 struct saa7134_dev *dev = video_drvdata(file);
1792 struct saa7134_format *fmt;
1794 if (!capable(CAP_SYS_ADMIN) &&
1795 !capable(CAP_SYS_RAWIO))
1799 fmt = format_by_fourcc(fb->fmt.pixelformat);
1806 if (0 == dev->ovbuf.fmt.bytesperline)
1807 dev->ovbuf.fmt.bytesperline =
1808 dev->ovbuf.fmt.width*fmt->depth/8;
1812 static int saa7134_overlay(struct file *file, void *priv, unsigned int on)
1814 struct saa7134_dev *dev = video_drvdata(file);
1815 unsigned long flags;
1818 if (saa7134_no_overlay > 0) {
1819 dprintk("no_overlay\n");
1823 if (dev->overlay_owner && priv != dev->overlay_owner)
1825 dev->overlay_owner = priv;
1826 spin_lock_irqsave(&dev->slock, flags);
1828 spin_unlock_irqrestore(&dev->slock, flags);
1831 if (priv != dev->overlay_owner)
1833 spin_lock_irqsave(&dev->slock, flags);
1835 spin_unlock_irqrestore(&dev->slock, flags);
1836 dev->overlay_owner = NULL;
1841 #ifdef CONFIG_VIDEO_ADV_DEBUG
1842 static int vidioc_g_register (struct file *file, void *priv,
1843 struct v4l2_dbg_register *reg)
1845 struct saa7134_dev *dev = video_drvdata(file);
1847 reg->val = saa_readb(reg->reg & 0xffffff);
1852 static int vidioc_s_register (struct file *file, void *priv,
1853 const struct v4l2_dbg_register *reg)
1855 struct saa7134_dev *dev = video_drvdata(file);
1857 saa_writeb(reg->reg & 0xffffff, reg->val);
1862 static int radio_g_tuner(struct file *file, void *priv,
1863 struct v4l2_tuner *t)
1865 struct saa7134_dev *dev = video_drvdata(file);
1870 strcpy(t->name, "Radio");
1872 saa_call_all(dev, tuner, g_tuner, t);
1873 t->audmode &= V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO;
1874 if (dev->input->amux == TV) {
1875 t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
1876 t->rxsubchans = (saa_readb(0x529) & 0x08) ?
1877 V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
1881 static int radio_s_tuner(struct file *file, void *priv,
1882 const struct v4l2_tuner *t)
1884 struct saa7134_dev *dev = video_drvdata(file);
1889 saa_call_all(dev, tuner, s_tuner, t);
1893 static const struct v4l2_file_operations video_fops =
1895 .owner = THIS_MODULE,
1897 .release = video_release,
1898 .read = vb2_fop_read,
1899 .poll = vb2_fop_poll,
1900 .mmap = vb2_fop_mmap,
1901 .unlocked_ioctl = video_ioctl2,
1904 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1905 .vidioc_querycap = saa7134_querycap,
1906 .vidioc_enum_fmt_vid_cap = saa7134_enum_fmt_vid_cap,
1907 .vidioc_g_fmt_vid_cap = saa7134_g_fmt_vid_cap,
1908 .vidioc_try_fmt_vid_cap = saa7134_try_fmt_vid_cap,
1909 .vidioc_s_fmt_vid_cap = saa7134_s_fmt_vid_cap,
1910 .vidioc_enum_fmt_vid_overlay = saa7134_enum_fmt_vid_overlay,
1911 .vidioc_g_fmt_vid_overlay = saa7134_g_fmt_vid_overlay,
1912 .vidioc_try_fmt_vid_overlay = saa7134_try_fmt_vid_overlay,
1913 .vidioc_s_fmt_vid_overlay = saa7134_s_fmt_vid_overlay,
1914 .vidioc_g_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
1915 .vidioc_try_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
1916 .vidioc_s_fmt_vbi_cap = saa7134_try_get_set_fmt_vbi_cap,
1917 .vidioc_cropcap = saa7134_cropcap,
1918 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1919 .vidioc_querybuf = vb2_ioctl_querybuf,
1920 .vidioc_qbuf = vb2_ioctl_qbuf,
1921 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1922 .vidioc_s_std = saa7134_s_std,
1923 .vidioc_g_std = saa7134_g_std,
1924 .vidioc_querystd = saa7134_querystd,
1925 .vidioc_enum_input = saa7134_enum_input,
1926 .vidioc_g_input = saa7134_g_input,
1927 .vidioc_s_input = saa7134_s_input,
1928 .vidioc_streamon = vb2_ioctl_streamon,
1929 .vidioc_streamoff = vb2_ioctl_streamoff,
1930 .vidioc_g_tuner = saa7134_g_tuner,
1931 .vidioc_s_tuner = saa7134_s_tuner,
1932 .vidioc_g_crop = saa7134_g_crop,
1933 .vidioc_s_crop = saa7134_s_crop,
1934 .vidioc_g_fbuf = saa7134_g_fbuf,
1935 .vidioc_s_fbuf = saa7134_s_fbuf,
1936 .vidioc_overlay = saa7134_overlay,
1937 .vidioc_g_frequency = saa7134_g_frequency,
1938 .vidioc_s_frequency = saa7134_s_frequency,
1939 #ifdef CONFIG_VIDEO_ADV_DEBUG
1940 .vidioc_g_register = vidioc_g_register,
1941 .vidioc_s_register = vidioc_s_register,
1943 .vidioc_log_status = v4l2_ctrl_log_status,
1944 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1945 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1948 static const struct v4l2_file_operations radio_fops = {
1949 .owner = THIS_MODULE,
1952 .release = video_release,
1953 .unlocked_ioctl = video_ioctl2,
1957 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
1958 .vidioc_querycap = saa7134_querycap,
1959 .vidioc_g_tuner = radio_g_tuner,
1960 .vidioc_s_tuner = radio_s_tuner,
1961 .vidioc_g_frequency = saa7134_g_frequency,
1962 .vidioc_s_frequency = saa7134_s_frequency,
1963 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1964 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1967 /* ----------------------------------------------------------- */
1968 /* exported stuff */
1970 struct video_device saa7134_video_template = {
1971 .name = "saa7134-video",
1972 .fops = &video_fops,
1973 .ioctl_ops = &video_ioctl_ops,
1974 .tvnorms = SAA7134_NORMS,
1977 struct video_device saa7134_radio_template = {
1978 .name = "saa7134-radio",
1979 .fops = &radio_fops,
1980 .ioctl_ops = &radio_ioctl_ops,
1983 static const struct v4l2_ctrl_ops saa7134_ctrl_ops = {
1984 .s_ctrl = saa7134_s_ctrl,
1987 static const struct v4l2_ctrl_config saa7134_ctrl_invert = {
1988 .ops = &saa7134_ctrl_ops,
1989 .id = V4L2_CID_PRIVATE_INVERT,
1991 .type = V4L2_CTRL_TYPE_BOOLEAN,
1997 static const struct v4l2_ctrl_config saa7134_ctrl_y_odd = {
1998 .ops = &saa7134_ctrl_ops,
1999 .id = V4L2_CID_PRIVATE_Y_ODD,
2000 .name = "Y Offset Odd Field",
2001 .type = V4L2_CTRL_TYPE_INTEGER,
2007 static const struct v4l2_ctrl_config saa7134_ctrl_y_even = {
2008 .ops = &saa7134_ctrl_ops,
2009 .id = V4L2_CID_PRIVATE_Y_EVEN,
2010 .name = "Y Offset Even Field",
2011 .type = V4L2_CTRL_TYPE_INTEGER,
2017 static const struct v4l2_ctrl_config saa7134_ctrl_automute = {
2018 .ops = &saa7134_ctrl_ops,
2019 .id = V4L2_CID_PRIVATE_AUTOMUTE,
2021 .type = V4L2_CTRL_TYPE_BOOLEAN,
2028 int saa7134_video_init1(struct saa7134_dev *dev)
2030 struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
2031 struct vb2_queue *q;
2034 /* sanitycheck insmod options */
2035 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2037 if (gbufsize > gbufsize_max)
2038 gbufsize = gbufsize_max;
2039 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2041 v4l2_ctrl_handler_init(hdl, 11);
2042 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2043 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
2044 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2045 V4L2_CID_CONTRAST, 0, 127, 1, 68);
2046 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2047 V4L2_CID_SATURATION, 0, 127, 1, 64);
2048 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2049 V4L2_CID_HUE, -128, 127, 1, 0);
2050 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2051 V4L2_CID_HFLIP, 0, 1, 1, 0);
2052 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2053 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
2054 v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2055 V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
2056 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_invert, NULL);
2057 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_odd, NULL);
2058 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_even, NULL);
2059 v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_automute, NULL);
2062 if (card_has_radio(dev)) {
2063 hdl = &dev->radio_ctrl_handler;
2064 v4l2_ctrl_handler_init(hdl, 2);
2065 v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler,
2066 v4l2_ctrl_radio_filter);
2072 if (dev->tda9887_conf && saa7134_ctrl_automute.def)
2073 dev->tda9887_conf |= TDA9887_AUTOMUTE;
2076 INIT_LIST_HEAD(&dev->video_q.queue);
2077 init_timer(&dev->video_q.timeout);
2078 dev->video_q.timeout.function = saa7134_buffer_timeout;
2079 dev->video_q.timeout.data = (unsigned long)(&dev->video_q);
2080 dev->video_q.dev = dev;
2081 dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
2084 dev->field = V4L2_FIELD_INTERLACED;
2085 dev->win.w.width = dev->width;
2086 dev->win.w.height = dev->height;
2087 dev->win.field = V4L2_FIELD_INTERLACED;
2088 dev->ovbuf.fmt.width = dev->width;
2089 dev->ovbuf.fmt.height = dev->height;
2090 dev->ovbuf.fmt.pixelformat = dev->fmt->fourcc;
2091 dev->ovbuf.fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
2093 if (saa7134_boards[dev->board].video_out)
2094 saa7134_videoport_init(dev);
2096 q = &dev->video_vbq;
2097 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2099 * Do not add VB2_USERPTR unless explicitly requested: the saa7134 DMA
2100 * engine cannot handle transfers that do not start at the beginning
2101 * of a page. A user-provided pointer can start anywhere in a page, so
2102 * USERPTR support is a no-go unless the application knows about these
2103 * limitations and has special support for this.
2105 q->io_modes = VB2_MMAP | VB2_READ;
2106 if (saa7134_userptr)
2107 q->io_modes |= VB2_USERPTR;
2108 q->drv_priv = &dev->video_q;
2110 q->gfp_flags = GFP_DMA32;
2111 q->mem_ops = &vb2_dma_sg_memops;
2112 q->buf_struct_size = sizeof(struct saa7134_buf);
2113 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2114 q->lock = &dev->lock;
2115 ret = vb2_queue_init(q);
2118 saa7134_pgtable_alloc(dev->pci, &dev->video_q.pt);
2121 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
2122 /* Don't add VB2_USERPTR, see comment above */
2123 q->io_modes = VB2_MMAP | VB2_READ;
2124 if (saa7134_userptr)
2125 q->io_modes |= VB2_USERPTR;
2126 q->drv_priv = &dev->vbi_q;
2127 q->ops = &saa7134_vbi_qops;
2128 q->gfp_flags = GFP_DMA32;
2129 q->mem_ops = &vb2_dma_sg_memops;
2130 q->buf_struct_size = sizeof(struct saa7134_buf);
2131 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2132 q->lock = &dev->lock;
2133 ret = vb2_queue_init(q);
2136 saa7134_pgtable_alloc(dev->pci, &dev->vbi_q.pt);
2141 void saa7134_video_fini(struct saa7134_dev *dev)
2144 vb2_queue_release(&dev->video_vbq);
2145 saa7134_pgtable_free(dev->pci, &dev->video_q.pt);
2146 vb2_queue_release(&dev->vbi_vbq);
2147 saa7134_pgtable_free(dev->pci, &dev->vbi_q.pt);
2148 v4l2_ctrl_handler_free(&dev->ctrl_handler);
2149 if (card_has_radio(dev))
2150 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
2153 int saa7134_videoport_init(struct saa7134_dev *dev)
2155 /* enable video output */
2156 int vo = saa7134_boards[dev->board].video_out;
2158 unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
2160 /* Configure videoport */
2161 saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2162 video_reg = video_out[vo][1];
2163 if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2164 video_reg &= ~VP_T_CODE_P_INVERTED;
2165 saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2166 saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2167 saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2168 video_reg = video_out[vo][5];
2169 if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2170 video_reg &= ~VP_CLK_CTRL2_DELAYED;
2171 if (vid_port_opts & SET_CLOCK_INVERTED)
2172 video_reg |= VP_CLK_CTRL1_INVERTED;
2173 saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2174 video_reg = video_out[vo][6];
2175 if (vid_port_opts & SET_VSYNC_OFF) {
2176 video_reg &= ~VP_VS_TYPE_MASK;
2177 video_reg |= VP_VS_TYPE_OFF;
2179 saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2180 saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2181 saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2183 /* Start videoport */
2184 saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2189 int saa7134_video_init2(struct saa7134_dev *dev)
2192 set_tvnorm(dev,&tvnorms[0]);
2194 v4l2_ctrl_handler_setup(&dev->ctrl_handler);
2195 saa7134_tvaudio_setmute(dev);
2196 saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2200 void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
2202 static const char *st[] = {
2203 "(no signal)", "NTSC", "PAL", "SECAM" };
2206 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2207 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2208 dprintk("DCSDT: pll: %s, sync: %s, norm: %s\n",
2209 (st1 & 0x40) ? "not locked" : "locked",
2210 (st2 & 0x40) ? "no" : "yes",
2212 dev->nosignal = (st1 & 0x40) || (st2 & 0x40) || !(st2 & 0x1);
2214 if (dev->nosignal) {
2215 /* no video signal -> mute audio */
2216 if (dev->ctl_automute)
2218 saa7134_tvaudio_setmute(dev);
2220 /* wake up tvaudio audio carrier scan thread */
2221 saa7134_tvaudio_do_scan(dev);
2224 if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2225 saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2227 saa_setb(SAA7134_SYNC_CTRL, 0x20);
2229 if (dev->mops && dev->mops->signal_change)
2230 dev->mops->signal_change(dev);
2234 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2236 enum v4l2_field field;
2238 spin_lock(&dev->slock);
2239 if (dev->video_q.curr) {
2241 if (V4L2_FIELD_HAS_BOTH(field)) {
2242 /* make sure we have seen both fields */
2243 if ((status & 0x10) == 0x00) {
2244 dev->video_q.curr->top_seen = 1;
2247 if (!dev->video_q.curr->top_seen)
2249 } else if (field == V4L2_FIELD_TOP) {
2250 if ((status & 0x10) != 0x10)
2252 } else if (field == V4L2_FIELD_BOTTOM) {
2253 if ((status & 0x10) != 0x00)
2256 saa7134_buffer_finish(dev, &dev->video_q, VB2_BUF_STATE_DONE);
2258 saa7134_buffer_next(dev, &dev->video_q);
2261 spin_unlock(&dev->slock);