xref: /openbmc/linux/drivers/media/pci/saa7134/saa7134-video.c (revision b802fb99ae964681d1754428f67970911e0476e9)
1 /*
2  *
3  * device driver for philips saa7134 based TV cards
4  * video4linux video interface
5  *
6  * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7  *
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.
12  *
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.
17  *
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.
21  */
22 
23 #include "saa7134.h"
24 #include "saa7134-reg.h"
25 
26 #include <linux/init.h>
27 #include <linux/list.h>
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/sort.h>
32 
33 #include <media/v4l2-common.h>
34 #include <media/v4l2-event.h>
35 #include <media/i2c/saa6588.h>
36 
37 /* ------------------------------------------------------------------ */
38 
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");
53 
54 
55 #define video_dbg(fmt, arg...) do { \
56 	if (video_debug & 0x04) \
57 		printk(KERN_DEBUG pr_fmt("video: " fmt), ## arg); \
58 	} while (0)
59 
60 /* ------------------------------------------------------------------ */
61 /* Defines for Video Output Port Register at address 0x191            */
62 
63 /* Bit 0: VIP code T bit polarity */
64 
65 #define VP_T_CODE_P_NON_INVERTED	0x00
66 #define VP_T_CODE_P_INVERTED		0x01
67 
68 /* ------------------------------------------------------------------ */
69 /* Defines for Video Output Port Register at address 0x195            */
70 
71 /* Bit 2: Video output clock delay control */
72 
73 #define VP_CLK_CTRL2_NOT_DELAYED	0x00
74 #define VP_CLK_CTRL2_DELAYED		0x04
75 
76 /* Bit 1: Video output clock invert control */
77 
78 #define VP_CLK_CTRL1_NON_INVERTED	0x00
79 #define VP_CLK_CTRL1_INVERTED		0x02
80 
81 /* ------------------------------------------------------------------ */
82 /* Defines for Video Output Port Register at address 0x196            */
83 
84 /* Bits 2 to 0: VSYNC pin video vertical sync type */
85 
86 #define VP_VS_TYPE_MASK			0x07
87 
88 #define VP_VS_TYPE_OFF			0x00
89 #define VP_VS_TYPE_V123			0x01
90 #define VP_VS_TYPE_V_ITU		0x02
91 #define VP_VS_TYPE_VGATE_L		0x03
92 #define VP_VS_TYPE_RESERVED1		0x04
93 #define VP_VS_TYPE_RESERVED2		0x05
94 #define VP_VS_TYPE_F_ITU		0x06
95 #define VP_VS_TYPE_SC_FID		0x07
96 
97 /* ------------------------------------------------------------------ */
98 /* data structs for video                                             */
99 
100 static int video_out[][9] = {
101 	[CCIR656] = { 0x00, 0xb1, 0x00, 0xa1, 0x00, 0x04, 0x06, 0x00, 0x00 },
102 };
103 
104 static struct saa7134_format formats[] = {
105 	{
106 		.name     = "8 bpp gray",
107 		.fourcc   = V4L2_PIX_FMT_GREY,
108 		.depth    = 8,
109 		.pm       = 0x06,
110 	},{
111 		.name     = "15 bpp RGB, le",
112 		.fourcc   = V4L2_PIX_FMT_RGB555,
113 		.depth    = 16,
114 		.pm       = 0x13 | 0x80,
115 	},{
116 		.name     = "15 bpp RGB, be",
117 		.fourcc   = V4L2_PIX_FMT_RGB555X,
118 		.depth    = 16,
119 		.pm       = 0x13 | 0x80,
120 		.bswap    = 1,
121 	},{
122 		.name     = "16 bpp RGB, le",
123 		.fourcc   = V4L2_PIX_FMT_RGB565,
124 		.depth    = 16,
125 		.pm       = 0x10 | 0x80,
126 	},{
127 		.name     = "16 bpp RGB, be",
128 		.fourcc   = V4L2_PIX_FMT_RGB565X,
129 		.depth    = 16,
130 		.pm       = 0x10 | 0x80,
131 		.bswap    = 1,
132 	},{
133 		.name     = "24 bpp RGB, le",
134 		.fourcc   = V4L2_PIX_FMT_BGR24,
135 		.depth    = 24,
136 		.pm       = 0x11,
137 	},{
138 		.name     = "24 bpp RGB, be",
139 		.fourcc   = V4L2_PIX_FMT_RGB24,
140 		.depth    = 24,
141 		.pm       = 0x11,
142 		.bswap    = 1,
143 	},{
144 		.name     = "32 bpp RGB, le",
145 		.fourcc   = V4L2_PIX_FMT_BGR32,
146 		.depth    = 32,
147 		.pm       = 0x12,
148 	},{
149 		.name     = "32 bpp RGB, be",
150 		.fourcc   = V4L2_PIX_FMT_RGB32,
151 		.depth    = 32,
152 		.pm       = 0x12,
153 		.bswap    = 1,
154 		.wswap    = 1,
155 	},{
156 		.name     = "4:2:2 packed, YUYV",
157 		.fourcc   = V4L2_PIX_FMT_YUYV,
158 		.depth    = 16,
159 		.pm       = 0x00,
160 		.bswap    = 1,
161 		.yuv      = 1,
162 	},{
163 		.name     = "4:2:2 packed, UYVY",
164 		.fourcc   = V4L2_PIX_FMT_UYVY,
165 		.depth    = 16,
166 		.pm       = 0x00,
167 		.yuv      = 1,
168 	},{
169 		.name     = "4:2:2 planar, Y-Cb-Cr",
170 		.fourcc   = V4L2_PIX_FMT_YUV422P,
171 		.depth    = 16,
172 		.pm       = 0x09,
173 		.yuv      = 1,
174 		.planar   = 1,
175 		.hshift   = 1,
176 		.vshift   = 0,
177 	},{
178 		.name     = "4:2:0 planar, Y-Cb-Cr",
179 		.fourcc   = V4L2_PIX_FMT_YUV420,
180 		.depth    = 12,
181 		.pm       = 0x0a,
182 		.yuv      = 1,
183 		.planar   = 1,
184 		.hshift   = 1,
185 		.vshift   = 1,
186 	},{
187 		.name     = "4:2:0 planar, Y-Cb-Cr",
188 		.fourcc   = V4L2_PIX_FMT_YVU420,
189 		.depth    = 12,
190 		.pm       = 0x0a,
191 		.yuv      = 1,
192 		.planar   = 1,
193 		.uvswap   = 1,
194 		.hshift   = 1,
195 		.vshift   = 1,
196 	}
197 };
198 #define FORMATS ARRAY_SIZE(formats)
199 
200 #define NORM_625_50			\
201 		.h_start       = 0,	\
202 		.h_stop        = 719,	\
203 		.video_v_start = 24,	\
204 		.video_v_stop  = 311,	\
205 		.vbi_v_start_0 = 7,	\
206 		.vbi_v_stop_0  = 23,	\
207 		.vbi_v_start_1 = 319,   \
208 		.src_timing    = 4
209 
210 #define NORM_525_60			\
211 		.h_start       = 0,	\
212 		.h_stop        = 719,	\
213 		.video_v_start = 23,	\
214 		.video_v_stop  = 262,	\
215 		.vbi_v_start_0 = 10,	\
216 		.vbi_v_stop_0  = 21,	\
217 		.vbi_v_start_1 = 273,	\
218 		.src_timing    = 7
219 
220 static struct saa7134_tvnorm tvnorms[] = {
221 	{
222 		.name          = "PAL", /* autodetect */
223 		.id            = V4L2_STD_PAL,
224 		NORM_625_50,
225 
226 		.sync_control  = 0x18,
227 		.luma_control  = 0x40,
228 		.chroma_ctrl1  = 0x81,
229 		.chroma_gain   = 0x2a,
230 		.chroma_ctrl2  = 0x06,
231 		.vgate_misc    = 0x1c,
232 
233 	},{
234 		.name          = "PAL-BG",
235 		.id            = V4L2_STD_PAL_BG,
236 		NORM_625_50,
237 
238 		.sync_control  = 0x18,
239 		.luma_control  = 0x40,
240 		.chroma_ctrl1  = 0x81,
241 		.chroma_gain   = 0x2a,
242 		.chroma_ctrl2  = 0x06,
243 		.vgate_misc    = 0x1c,
244 
245 	},{
246 		.name          = "PAL-I",
247 		.id            = V4L2_STD_PAL_I,
248 		NORM_625_50,
249 
250 		.sync_control  = 0x18,
251 		.luma_control  = 0x40,
252 		.chroma_ctrl1  = 0x81,
253 		.chroma_gain   = 0x2a,
254 		.chroma_ctrl2  = 0x06,
255 		.vgate_misc    = 0x1c,
256 
257 	},{
258 		.name          = "PAL-DK",
259 		.id            = V4L2_STD_PAL_DK,
260 		NORM_625_50,
261 
262 		.sync_control  = 0x18,
263 		.luma_control  = 0x40,
264 		.chroma_ctrl1  = 0x81,
265 		.chroma_gain   = 0x2a,
266 		.chroma_ctrl2  = 0x06,
267 		.vgate_misc    = 0x1c,
268 
269 	},{
270 		.name          = "NTSC",
271 		.id            = V4L2_STD_NTSC,
272 		NORM_525_60,
273 
274 		.sync_control  = 0x59,
275 		.luma_control  = 0x40,
276 		.chroma_ctrl1  = 0x89,
277 		.chroma_gain   = 0x2a,
278 		.chroma_ctrl2  = 0x0e,
279 		.vgate_misc    = 0x18,
280 
281 	},{
282 		.name          = "SECAM",
283 		.id            = V4L2_STD_SECAM,
284 		NORM_625_50,
285 
286 		.sync_control  = 0x18,
287 		.luma_control  = 0x1b,
288 		.chroma_ctrl1  = 0xd1,
289 		.chroma_gain   = 0x80,
290 		.chroma_ctrl2  = 0x00,
291 		.vgate_misc    = 0x1c,
292 
293 	},{
294 		.name          = "SECAM-DK",
295 		.id            = V4L2_STD_SECAM_DK,
296 		NORM_625_50,
297 
298 		.sync_control  = 0x18,
299 		.luma_control  = 0x1b,
300 		.chroma_ctrl1  = 0xd1,
301 		.chroma_gain   = 0x80,
302 		.chroma_ctrl2  = 0x00,
303 		.vgate_misc    = 0x1c,
304 
305 	},{
306 		.name          = "SECAM-L",
307 		.id            = V4L2_STD_SECAM_L,
308 		NORM_625_50,
309 
310 		.sync_control  = 0x18,
311 		.luma_control  = 0x1b,
312 		.chroma_ctrl1  = 0xd1,
313 		.chroma_gain   = 0x80,
314 		.chroma_ctrl2  = 0x00,
315 		.vgate_misc    = 0x1c,
316 
317 	},{
318 		.name          = "SECAM-Lc",
319 		.id            = V4L2_STD_SECAM_LC,
320 		NORM_625_50,
321 
322 		.sync_control  = 0x18,
323 		.luma_control  = 0x1b,
324 		.chroma_ctrl1  = 0xd1,
325 		.chroma_gain   = 0x80,
326 		.chroma_ctrl2  = 0x00,
327 		.vgate_misc    = 0x1c,
328 
329 	},{
330 		.name          = "PAL-M",
331 		.id            = V4L2_STD_PAL_M,
332 		NORM_525_60,
333 
334 		.sync_control  = 0x59,
335 		.luma_control  = 0x40,
336 		.chroma_ctrl1  = 0xb9,
337 		.chroma_gain   = 0x2a,
338 		.chroma_ctrl2  = 0x0e,
339 		.vgate_misc    = 0x18,
340 
341 	},{
342 		.name          = "PAL-Nc",
343 		.id            = V4L2_STD_PAL_Nc,
344 		NORM_625_50,
345 
346 		.sync_control  = 0x18,
347 		.luma_control  = 0x40,
348 		.chroma_ctrl1  = 0xa1,
349 		.chroma_gain   = 0x2a,
350 		.chroma_ctrl2  = 0x06,
351 		.vgate_misc    = 0x1c,
352 
353 	},{
354 		.name          = "PAL-60",
355 		.id            = V4L2_STD_PAL_60,
356 
357 		.h_start       = 0,
358 		.h_stop        = 719,
359 		.video_v_start = 23,
360 		.video_v_stop  = 262,
361 		.vbi_v_start_0 = 10,
362 		.vbi_v_stop_0  = 21,
363 		.vbi_v_start_1 = 273,
364 		.src_timing    = 7,
365 
366 		.sync_control  = 0x18,
367 		.luma_control  = 0x40,
368 		.chroma_ctrl1  = 0x81,
369 		.chroma_gain   = 0x2a,
370 		.chroma_ctrl2  = 0x06,
371 		.vgate_misc    = 0x1c,
372 	}
373 };
374 #define TVNORMS ARRAY_SIZE(tvnorms)
375 
376 static struct saa7134_format* format_by_fourcc(unsigned int fourcc)
377 {
378 	unsigned int i;
379 
380 	for (i = 0; i < FORMATS; i++)
381 		if (formats[i].fourcc == fourcc)
382 			return formats+i;
383 	return NULL;
384 }
385 
386 /* ------------------------------------------------------------------ */
387 
388 static void set_tvnorm(struct saa7134_dev *dev, struct saa7134_tvnorm *norm)
389 {
390 	video_dbg("set tv norm = %s\n", norm->name);
391 	dev->tvnorm = norm;
392 
393 	/* setup cropping */
394 	dev->crop_bounds.left    = norm->h_start;
395 	dev->crop_defrect.left   = norm->h_start;
396 	dev->crop_bounds.width   = norm->h_stop - norm->h_start +1;
397 	dev->crop_defrect.width  = norm->h_stop - norm->h_start +1;
398 
399 	dev->crop_bounds.top     = (norm->vbi_v_stop_0+1)*2;
400 	dev->crop_defrect.top    = norm->video_v_start*2;
401 	dev->crop_bounds.height  = ((norm->id & V4L2_STD_525_60) ? 524 : 624)
402 		- dev->crop_bounds.top;
403 	dev->crop_defrect.height = (norm->video_v_stop - norm->video_v_start +1)*2;
404 
405 	dev->crop_current = dev->crop_defrect;
406 
407 	saa7134_set_tvnorm_hw(dev);
408 }
409 
410 static void video_mux(struct saa7134_dev *dev, int input)
411 {
412 	video_dbg("video input = %d [%s]\n",
413 		  input, saa7134_input_name[card_in(dev, input).type]);
414 	dev->ctl_input = input;
415 	set_tvnorm(dev, dev->tvnorm);
416 	saa7134_tvaudio_setinput(dev, &card_in(dev, input));
417 }
418 
419 
420 static void saa7134_set_decoder(struct saa7134_dev *dev)
421 {
422 	int luma_control, sync_control, chroma_ctrl1, mux;
423 
424 	struct saa7134_tvnorm *norm = dev->tvnorm;
425 	mux = card_in(dev, dev->ctl_input).vmux;
426 
427 	luma_control = norm->luma_control;
428 	sync_control = norm->sync_control;
429 	chroma_ctrl1 = norm->chroma_ctrl1;
430 
431 	if (mux > 5)
432 		luma_control |= 0x80; /* svideo */
433 	if (noninterlaced || dev->nosignal)
434 		sync_control |= 0x20;
435 
436 	/* switch on auto standard detection */
437 	sync_control |= SAA7134_SYNC_CTRL_AUFD;
438 	chroma_ctrl1 |= SAA7134_CHROMA_CTRL1_AUTO0;
439 	chroma_ctrl1 &= ~SAA7134_CHROMA_CTRL1_FCTC;
440 	luma_control &= ~SAA7134_LUMA_CTRL_LDEL;
441 
442 	/* setup video decoder */
443 	saa_writeb(SAA7134_INCR_DELAY,            0x08);
444 	saa_writeb(SAA7134_ANALOG_IN_CTRL1,       0xc0 | mux);
445 	saa_writeb(SAA7134_ANALOG_IN_CTRL2,       0x00);
446 
447 	saa_writeb(SAA7134_ANALOG_IN_CTRL3,       0x90);
448 	saa_writeb(SAA7134_ANALOG_IN_CTRL4,       0x90);
449 	saa_writeb(SAA7134_HSYNC_START,           0xeb);
450 	saa_writeb(SAA7134_HSYNC_STOP,            0xe0);
451 	saa_writeb(SAA7134_SOURCE_TIMING1,        norm->src_timing);
452 
453 	saa_writeb(SAA7134_SYNC_CTRL,             sync_control);
454 	saa_writeb(SAA7134_LUMA_CTRL,             luma_control);
455 	saa_writeb(SAA7134_DEC_LUMA_BRIGHT,       dev->ctl_bright);
456 
457 	saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
458 		dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
459 
460 	saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
461 		dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
462 
463 	saa_writeb(SAA7134_DEC_CHROMA_HUE,        dev->ctl_hue);
464 	saa_writeb(SAA7134_CHROMA_CTRL1,          chroma_ctrl1);
465 	saa_writeb(SAA7134_CHROMA_GAIN,           norm->chroma_gain);
466 
467 	saa_writeb(SAA7134_CHROMA_CTRL2,          norm->chroma_ctrl2);
468 	saa_writeb(SAA7134_MODE_DELAY_CTRL,       0x00);
469 
470 	saa_writeb(SAA7134_ANALOG_ADC,            0x01);
471 	saa_writeb(SAA7134_VGATE_START,           0x11);
472 	saa_writeb(SAA7134_VGATE_STOP,            0xfe);
473 	saa_writeb(SAA7134_MISC_VGATE_MSB,        norm->vgate_misc);
474 	saa_writeb(SAA7134_RAW_DATA_GAIN,         0x40);
475 	saa_writeb(SAA7134_RAW_DATA_OFFSET,       0x80);
476 }
477 
478 void saa7134_set_tvnorm_hw(struct saa7134_dev *dev)
479 {
480 	saa7134_set_decoder(dev);
481 
482 	saa_call_all(dev, video, s_std, dev->tvnorm->id);
483 	/* Set the correct norm for the saa6752hs. This function
484 	   does nothing if there is no saa6752hs. */
485 	saa_call_empress(dev, video, s_std, dev->tvnorm->id);
486 }
487 
488 static void set_h_prescale(struct saa7134_dev *dev, int task, int prescale)
489 {
490 	static const struct {
491 		int xpsc;
492 		int xacl;
493 		int xc2_1;
494 		int xdcg;
495 		int vpfy;
496 	} vals[] = {
497 		/* XPSC XACL XC2_1 XDCG VPFY */
498 		{    1,   0,    0,    0,   0 },
499 		{    2,   2,    1,    2,   2 },
500 		{    3,   4,    1,    3,   2 },
501 		{    4,   8,    1,    4,   2 },
502 		{    5,   8,    1,    4,   2 },
503 		{    6,   8,    1,    4,   3 },
504 		{    7,   8,    1,    4,   3 },
505 		{    8,  15,    0,    4,   3 },
506 		{    9,  15,    0,    4,   3 },
507 		{   10,  16,    1,    5,   3 },
508 	};
509 	static const int count = ARRAY_SIZE(vals);
510 	int i;
511 
512 	for (i = 0; i < count; i++)
513 		if (vals[i].xpsc == prescale)
514 			break;
515 	if (i == count)
516 		return;
517 
518 	saa_writeb(SAA7134_H_PRESCALE(task), vals[i].xpsc);
519 	saa_writeb(SAA7134_ACC_LENGTH(task), vals[i].xacl);
520 	saa_writeb(SAA7134_LEVEL_CTRL(task),
521 		   (vals[i].xc2_1 << 3) | (vals[i].xdcg));
522 	saa_andorb(SAA7134_FIR_PREFILTER_CTRL(task), 0x0f,
523 		   (vals[i].vpfy << 2) | vals[i].vpfy);
524 }
525 
526 static void set_v_scale(struct saa7134_dev *dev, int task, int yscale)
527 {
528 	int val,mirror;
529 
530 	saa_writeb(SAA7134_V_SCALE_RATIO1(task), yscale &  0xff);
531 	saa_writeb(SAA7134_V_SCALE_RATIO2(task), yscale >> 8);
532 
533 	mirror = (dev->ctl_mirror) ? 0x02 : 0x00;
534 	if (yscale < 2048) {
535 		/* LPI */
536 		video_dbg("yscale LPI yscale=%d\n", yscale);
537 		saa_writeb(SAA7134_V_FILTER(task), 0x00 | mirror);
538 		saa_writeb(SAA7134_LUMA_CONTRAST(task), 0x40);
539 		saa_writeb(SAA7134_CHROMA_SATURATION(task), 0x40);
540 	} else {
541 		/* ACM */
542 		val = 0x40 * 1024 / yscale;
543 		video_dbg("yscale ACM yscale=%d val=0x%x\n", yscale, val);
544 		saa_writeb(SAA7134_V_FILTER(task), 0x01 | mirror);
545 		saa_writeb(SAA7134_LUMA_CONTRAST(task), val);
546 		saa_writeb(SAA7134_CHROMA_SATURATION(task), val);
547 	}
548 	saa_writeb(SAA7134_LUMA_BRIGHT(task),       0x80);
549 }
550 
551 static void set_size(struct saa7134_dev *dev, int task,
552 		     int width, int height, int interlace)
553 {
554 	int prescale,xscale,yscale,y_even,y_odd;
555 	int h_start, h_stop, v_start, v_stop;
556 	int div = interlace ? 2 : 1;
557 
558 	/* setup video scaler */
559 	h_start = dev->crop_current.left;
560 	v_start = dev->crop_current.top/2;
561 	h_stop  = (dev->crop_current.left + dev->crop_current.width -1);
562 	v_stop  = (dev->crop_current.top + dev->crop_current.height -1)/2;
563 
564 	saa_writeb(SAA7134_VIDEO_H_START1(task), h_start &  0xff);
565 	saa_writeb(SAA7134_VIDEO_H_START2(task), h_start >> 8);
566 	saa_writeb(SAA7134_VIDEO_H_STOP1(task),  h_stop  &  0xff);
567 	saa_writeb(SAA7134_VIDEO_H_STOP2(task),  h_stop  >> 8);
568 	saa_writeb(SAA7134_VIDEO_V_START1(task), v_start &  0xff);
569 	saa_writeb(SAA7134_VIDEO_V_START2(task), v_start >> 8);
570 	saa_writeb(SAA7134_VIDEO_V_STOP1(task),  v_stop  &  0xff);
571 	saa_writeb(SAA7134_VIDEO_V_STOP2(task),  v_stop  >> 8);
572 
573 	prescale = dev->crop_current.width / width;
574 	if (0 == prescale)
575 		prescale = 1;
576 	xscale = 1024 * dev->crop_current.width / prescale / width;
577 	yscale = 512 * div * dev->crop_current.height / height;
578 	video_dbg("prescale=%d xscale=%d yscale=%d\n",
579 		  prescale, xscale, yscale);
580 	set_h_prescale(dev,task,prescale);
581 	saa_writeb(SAA7134_H_SCALE_INC1(task),      xscale &  0xff);
582 	saa_writeb(SAA7134_H_SCALE_INC2(task),      xscale >> 8);
583 	set_v_scale(dev,task,yscale);
584 
585 	saa_writeb(SAA7134_VIDEO_PIXELS1(task),     width  & 0xff);
586 	saa_writeb(SAA7134_VIDEO_PIXELS2(task),     width  >> 8);
587 	saa_writeb(SAA7134_VIDEO_LINES1(task),      height/div & 0xff);
588 	saa_writeb(SAA7134_VIDEO_LINES2(task),      height/div >> 8);
589 
590 	/* deinterlace y offsets */
591 	y_odd  = dev->ctl_y_odd;
592 	y_even = dev->ctl_y_even;
593 	saa_writeb(SAA7134_V_PHASE_OFFSET0(task), y_odd);
594 	saa_writeb(SAA7134_V_PHASE_OFFSET1(task), y_even);
595 	saa_writeb(SAA7134_V_PHASE_OFFSET2(task), y_odd);
596 	saa_writeb(SAA7134_V_PHASE_OFFSET3(task), y_even);
597 }
598 
599 /* ------------------------------------------------------------------ */
600 
601 struct cliplist {
602 	__u16 position;
603 	__u8  enable;
604 	__u8  disable;
605 };
606 
607 static void set_cliplist(struct saa7134_dev *dev, int reg,
608 			struct cliplist *cl, int entries, char *name)
609 {
610 	__u8 winbits = 0;
611 	int i;
612 
613 	for (i = 0; i < entries; i++) {
614 		winbits |= cl[i].enable;
615 		winbits &= ~cl[i].disable;
616 		if (i < 15 && cl[i].position == cl[i+1].position)
617 			continue;
618 		saa_writeb(reg + 0, winbits);
619 		saa_writeb(reg + 2, cl[i].position & 0xff);
620 		saa_writeb(reg + 3, cl[i].position >> 8);
621 		video_dbg("clip: %s winbits=%02x pos=%d\n",
622 			name,winbits,cl[i].position);
623 		reg += 8;
624 	}
625 	for (; reg < 0x400; reg += 8) {
626 		saa_writeb(reg+ 0, 0);
627 		saa_writeb(reg + 1, 0);
628 		saa_writeb(reg + 2, 0);
629 		saa_writeb(reg + 3, 0);
630 	}
631 }
632 
633 static int clip_range(int val)
634 {
635 	if (val < 0)
636 		val = 0;
637 	return val;
638 }
639 
640 /* Sort into smallest position first order */
641 static int cliplist_cmp(const void *a, const void *b)
642 {
643 	const struct cliplist *cla = a;
644 	const struct cliplist *clb = b;
645 	if (cla->position < clb->position)
646 		return -1;
647 	if (cla->position > clb->position)
648 		return 1;
649 	return 0;
650 }
651 
652 static int setup_clipping(struct saa7134_dev *dev, struct v4l2_clip *clips,
653 			  int nclips, int interlace)
654 {
655 	struct cliplist col[16], row[16];
656 	int cols = 0, rows = 0, i;
657 	int div = interlace ? 2 : 1;
658 
659 	memset(col, 0, sizeof(col));
660 	memset(row, 0, sizeof(row));
661 	for (i = 0; i < nclips && i < 8; i++) {
662 		col[cols].position = clip_range(clips[i].c.left);
663 		col[cols].enable   = (1 << i);
664 		cols++;
665 		col[cols].position = clip_range(clips[i].c.left+clips[i].c.width);
666 		col[cols].disable  = (1 << i);
667 		cols++;
668 		row[rows].position = clip_range(clips[i].c.top / div);
669 		row[rows].enable   = (1 << i);
670 		rows++;
671 		row[rows].position = clip_range((clips[i].c.top + clips[i].c.height)
672 						/ div);
673 		row[rows].disable  = (1 << i);
674 		rows++;
675 	}
676 	sort(col, cols, sizeof col[0], cliplist_cmp, NULL);
677 	sort(row, rows, sizeof row[0], cliplist_cmp, NULL);
678 	set_cliplist(dev,0x380,col,cols,"cols");
679 	set_cliplist(dev,0x384,row,rows,"rows");
680 	return 0;
681 }
682 
683 static int verify_preview(struct saa7134_dev *dev, struct v4l2_window *win, bool try)
684 {
685 	enum v4l2_field field;
686 	int maxw, maxh;
687 
688 	if (!try && (dev->ovbuf.base == NULL || dev->ovfmt == NULL))
689 		return -EINVAL;
690 	if (win->w.width < 48)
691 		win->w.width = 48;
692 	if (win->w.height < 32)
693 		win->w.height = 32;
694 	if (win->clipcount > 8)
695 		win->clipcount = 8;
696 
697 	win->chromakey = 0;
698 	win->global_alpha = 0;
699 	field = win->field;
700 	maxw  = dev->crop_current.width;
701 	maxh  = dev->crop_current.height;
702 
703 	if (V4L2_FIELD_ANY == field) {
704 		field = (win->w.height > maxh/2)
705 			? V4L2_FIELD_INTERLACED
706 			: V4L2_FIELD_TOP;
707 	}
708 	switch (field) {
709 	case V4L2_FIELD_TOP:
710 	case V4L2_FIELD_BOTTOM:
711 		maxh = maxh / 2;
712 		break;
713 	default:
714 		field = V4L2_FIELD_INTERLACED;
715 		break;
716 	}
717 
718 	win->field = field;
719 	if (win->w.width > maxw)
720 		win->w.width = maxw;
721 	if (win->w.height > maxh)
722 		win->w.height = maxh;
723 	return 0;
724 }
725 
726 static int start_preview(struct saa7134_dev *dev)
727 {
728 	unsigned long base,control,bpl;
729 	int err;
730 
731 	err = verify_preview(dev, &dev->win, false);
732 	if (0 != err)
733 		return err;
734 
735 	dev->ovfield = dev->win.field;
736 	video_dbg("start_preview %dx%d+%d+%d %s field=%s\n",
737 		dev->win.w.width, dev->win.w.height,
738 		dev->win.w.left, dev->win.w.top,
739 		dev->ovfmt->name, v4l2_field_names[dev->ovfield]);
740 
741 	/* setup window + clipping */
742 	set_size(dev, TASK_B, dev->win.w.width, dev->win.w.height,
743 		 V4L2_FIELD_HAS_BOTH(dev->ovfield));
744 	setup_clipping(dev, dev->clips, dev->nclips,
745 		       V4L2_FIELD_HAS_BOTH(dev->ovfield));
746 	if (dev->ovfmt->yuv)
747 		saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x03);
748 	else
749 		saa_andorb(SAA7134_DATA_PATH(TASK_B), 0x3f, 0x01);
750 	saa_writeb(SAA7134_OFMT_VIDEO_B, dev->ovfmt->pm | 0x20);
751 
752 	/* dma: setup channel 1 (= Video Task B) */
753 	base  = (unsigned long)dev->ovbuf.base;
754 	base += dev->ovbuf.fmt.bytesperline * dev->win.w.top;
755 	base += dev->ovfmt->depth/8         * dev->win.w.left;
756 	bpl   = dev->ovbuf.fmt.bytesperline;
757 	control = SAA7134_RS_CONTROL_BURST_16;
758 	if (dev->ovfmt->bswap)
759 		control |= SAA7134_RS_CONTROL_BSWAP;
760 	if (dev->ovfmt->wswap)
761 		control |= SAA7134_RS_CONTROL_WSWAP;
762 	if (V4L2_FIELD_HAS_BOTH(dev->ovfield)) {
763 		saa_writel(SAA7134_RS_BA1(1),base);
764 		saa_writel(SAA7134_RS_BA2(1),base+bpl);
765 		saa_writel(SAA7134_RS_PITCH(1),bpl*2);
766 		saa_writel(SAA7134_RS_CONTROL(1),control);
767 	} else {
768 		saa_writel(SAA7134_RS_BA1(1),base);
769 		saa_writel(SAA7134_RS_BA2(1),base);
770 		saa_writel(SAA7134_RS_PITCH(1),bpl);
771 		saa_writel(SAA7134_RS_CONTROL(1),control);
772 	}
773 
774 	/* start dma */
775 	dev->ovenable = 1;
776 	saa7134_set_dmabits(dev);
777 
778 	return 0;
779 }
780 
781 static int stop_preview(struct saa7134_dev *dev)
782 {
783 	dev->ovenable = 0;
784 	saa7134_set_dmabits(dev);
785 	return 0;
786 }
787 
788 /*
789  * Media Controller helper functions
790  */
791 
792 static int saa7134_enable_analog_tuner(struct saa7134_dev *dev)
793 {
794 #ifdef CONFIG_MEDIA_CONTROLLER
795 	struct media_device *mdev = dev->media_dev;
796 	struct media_entity *source;
797 	struct media_link *link, *found_link = NULL;
798 	int ret, active_links = 0;
799 
800 	if (!mdev || !dev->decoder)
801 		return 0;
802 
803 	/*
804 	 * This will find the tuner that is connected into the decoder.
805 	 * Technically, this is not 100% correct, as the device may be
806 	 * using an analog input instead of the tuner. However, as we can't
807 	 * do DVB streaming while the DMA engine is being used for V4L2,
808 	 * this should be enough for the actual needs.
809 	 */
810 	list_for_each_entry(link, &dev->decoder->links, list) {
811 		if (link->sink->entity == dev->decoder) {
812 			found_link = link;
813 			if (link->flags & MEDIA_LNK_FL_ENABLED)
814 				active_links++;
815 			break;
816 		}
817 	}
818 
819 	if (active_links == 1 || !found_link)
820 		return 0;
821 
822 	source = found_link->source->entity;
823 	list_for_each_entry(link, &source->links, list) {
824 		struct media_entity *sink;
825 		int flags = 0;
826 
827 		sink = link->sink->entity;
828 
829 		if (sink == dev->decoder)
830 			flags = MEDIA_LNK_FL_ENABLED;
831 
832 		ret = media_entity_setup_link(link, flags);
833 		if (ret) {
834 			pr_err("Couldn't change link %s->%s to %s. Error %d\n",
835 			       source->name, sink->name,
836 			       flags ? "enabled" : "disabled",
837 			       ret);
838 			return ret;
839 		}
840 	}
841 #endif
842 	return 0;
843 }
844 
845 /* ------------------------------------------------------------------ */
846 
847 static int buffer_activate(struct saa7134_dev *dev,
848 			   struct saa7134_buf *buf,
849 			   struct saa7134_buf *next)
850 {
851 	struct saa7134_dmaqueue *dmaq = buf->vb2.vb2_buf.vb2_queue->drv_priv;
852 	unsigned long base,control,bpl;
853 	unsigned long bpl_uv,lines_uv,base2,base3,tmp; /* planar */
854 
855 	video_dbg("buffer_activate buf=%p\n", buf);
856 	buf->top_seen = 0;
857 
858 	set_size(dev, TASK_A, dev->width, dev->height,
859 		 V4L2_FIELD_HAS_BOTH(dev->field));
860 	if (dev->fmt->yuv)
861 		saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x03);
862 	else
863 		saa_andorb(SAA7134_DATA_PATH(TASK_A), 0x3f, 0x01);
864 	saa_writeb(SAA7134_OFMT_VIDEO_A, dev->fmt->pm);
865 
866 	/* DMA: setup channel 0 (= Video Task A0) */
867 	base  = saa7134_buffer_base(buf);
868 	if (dev->fmt->planar)
869 		bpl = dev->width;
870 	else
871 		bpl = (dev->width * dev->fmt->depth) / 8;
872 	control = SAA7134_RS_CONTROL_BURST_16 |
873 		SAA7134_RS_CONTROL_ME |
874 		(dmaq->pt.dma >> 12);
875 	if (dev->fmt->bswap)
876 		control |= SAA7134_RS_CONTROL_BSWAP;
877 	if (dev->fmt->wswap)
878 		control |= SAA7134_RS_CONTROL_WSWAP;
879 	if (V4L2_FIELD_HAS_BOTH(dev->field)) {
880 		/* interlaced */
881 		saa_writel(SAA7134_RS_BA1(0),base);
882 		saa_writel(SAA7134_RS_BA2(0),base+bpl);
883 		saa_writel(SAA7134_RS_PITCH(0),bpl*2);
884 	} else {
885 		/* non-interlaced */
886 		saa_writel(SAA7134_RS_BA1(0),base);
887 		saa_writel(SAA7134_RS_BA2(0),base);
888 		saa_writel(SAA7134_RS_PITCH(0),bpl);
889 	}
890 	saa_writel(SAA7134_RS_CONTROL(0),control);
891 
892 	if (dev->fmt->planar) {
893 		/* DMA: setup channel 4+5 (= planar task A) */
894 		bpl_uv   = bpl >> dev->fmt->hshift;
895 		lines_uv = dev->height >> dev->fmt->vshift;
896 		base2    = base + bpl * dev->height;
897 		base3    = base2 + bpl_uv * lines_uv;
898 		if (dev->fmt->uvswap)
899 			tmp = base2, base2 = base3, base3 = tmp;
900 		video_dbg("uv: bpl=%ld lines=%ld base2/3=%ld/%ld\n",
901 			bpl_uv,lines_uv,base2,base3);
902 		if (V4L2_FIELD_HAS_BOTH(dev->field)) {
903 			/* interlaced */
904 			saa_writel(SAA7134_RS_BA1(4),base2);
905 			saa_writel(SAA7134_RS_BA2(4),base2+bpl_uv);
906 			saa_writel(SAA7134_RS_PITCH(4),bpl_uv*2);
907 			saa_writel(SAA7134_RS_BA1(5),base3);
908 			saa_writel(SAA7134_RS_BA2(5),base3+bpl_uv);
909 			saa_writel(SAA7134_RS_PITCH(5),bpl_uv*2);
910 		} else {
911 			/* non-interlaced */
912 			saa_writel(SAA7134_RS_BA1(4),base2);
913 			saa_writel(SAA7134_RS_BA2(4),base2);
914 			saa_writel(SAA7134_RS_PITCH(4),bpl_uv);
915 			saa_writel(SAA7134_RS_BA1(5),base3);
916 			saa_writel(SAA7134_RS_BA2(5),base3);
917 			saa_writel(SAA7134_RS_PITCH(5),bpl_uv);
918 		}
919 		saa_writel(SAA7134_RS_CONTROL(4),control);
920 		saa_writel(SAA7134_RS_CONTROL(5),control);
921 	}
922 
923 	/* start DMA */
924 	saa7134_set_dmabits(dev);
925 	mod_timer(&dmaq->timeout, jiffies + BUFFER_TIMEOUT);
926 	return 0;
927 }
928 
929 static int buffer_init(struct vb2_buffer *vb2)
930 {
931 	struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
932 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
933 	struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
934 
935 	dmaq->curr = NULL;
936 	buf->activate = buffer_activate;
937 	return 0;
938 }
939 
940 static int buffer_prepare(struct vb2_buffer *vb2)
941 {
942 	struct saa7134_dmaqueue *dmaq = vb2->vb2_queue->drv_priv;
943 	struct saa7134_dev *dev = dmaq->dev;
944 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb2);
945 	struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
946 	struct sg_table *dma = vb2_dma_sg_plane_desc(vb2, 0);
947 	unsigned int size;
948 
949 	if (dma->sgl->offset) {
950 		pr_err("The buffer is not page-aligned\n");
951 		return -EINVAL;
952 	}
953 	size = (dev->width * dev->height * dev->fmt->depth) >> 3;
954 	if (vb2_plane_size(vb2, 0) < size)
955 		return -EINVAL;
956 
957 	vb2_set_plane_payload(vb2, 0, size);
958 	vbuf->field = dev->field;
959 
960 	return saa7134_pgtable_build(dev->pci, &dmaq->pt, dma->sgl, dma->nents,
961 				    saa7134_buffer_startpage(buf));
962 }
963 
964 static int queue_setup(struct vb2_queue *q,
965 			   unsigned int *nbuffers, unsigned int *nplanes,
966 			   unsigned int sizes[], void *alloc_ctxs[])
967 {
968 	struct saa7134_dmaqueue *dmaq = q->drv_priv;
969 	struct saa7134_dev *dev = dmaq->dev;
970 	int size = dev->fmt->depth * dev->width * dev->height >> 3;
971 
972 	if (dev->width    < 48 ||
973 	    dev->height   < 32 ||
974 	    dev->width/4  > dev->crop_current.width  ||
975 	    dev->height/4 > dev->crop_current.height ||
976 	    dev->width    > dev->crop_bounds.width  ||
977 	    dev->height   > dev->crop_bounds.height)
978 		return -EINVAL;
979 
980 	*nbuffers = saa7134_buffer_count(size, *nbuffers);
981 	*nplanes = 1;
982 	sizes[0] = size;
983 	alloc_ctxs[0] = dev->alloc_ctx;
984 
985 	saa7134_enable_analog_tuner(dev);
986 
987 	return 0;
988 }
989 
990 /*
991  * move buffer to hardware queue
992  */
993 void saa7134_vb2_buffer_queue(struct vb2_buffer *vb)
994 {
995 	struct saa7134_dmaqueue *dmaq = vb->vb2_queue->drv_priv;
996 	struct saa7134_dev *dev = dmaq->dev;
997 	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
998 	struct saa7134_buf *buf = container_of(vbuf, struct saa7134_buf, vb2);
999 
1000 	saa7134_buffer_queue(dev, dmaq, buf);
1001 }
1002 EXPORT_SYMBOL_GPL(saa7134_vb2_buffer_queue);
1003 
1004 int saa7134_vb2_start_streaming(struct vb2_queue *vq, unsigned int count)
1005 {
1006 	struct saa7134_dmaqueue *dmaq = vq->drv_priv;
1007 	struct saa7134_dev *dev = dmaq->dev;
1008 
1009 	/*
1010 	 * Planar video capture and TS share the same DMA channel,
1011 	 * so only one can be active at a time.
1012 	 */
1013 	if (card_is_empress(dev) && vb2_is_busy(&dev->empress_vbq) &&
1014 	    dmaq == &dev->video_q && dev->fmt->planar) {
1015 		struct saa7134_buf *buf, *tmp;
1016 
1017 		list_for_each_entry_safe(buf, tmp, &dmaq->queue, entry) {
1018 			list_del(&buf->entry);
1019 			vb2_buffer_done(&buf->vb2.vb2_buf,
1020 					VB2_BUF_STATE_QUEUED);
1021 		}
1022 		if (dmaq->curr) {
1023 			vb2_buffer_done(&dmaq->curr->vb2.vb2_buf,
1024 					VB2_BUF_STATE_QUEUED);
1025 			dmaq->curr = NULL;
1026 		}
1027 		return -EBUSY;
1028 	}
1029 
1030 	/* The SAA7134 has a 1K FIFO; the datasheet suggests that when
1031 	 * configured conservatively, there's 22 usec of buffering for video.
1032 	 * We therefore request a DMA latency of 20 usec, giving us 2 usec of
1033 	 * margin in case the FIFO is configured differently to the datasheet.
1034 	 * Unfortunately, I lack register-level documentation to check the
1035 	 * Linux FIFO setup and confirm the perfect value.
1036 	 */
1037 	if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
1038 	    (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
1039 		pm_qos_add_request(&dev->qos_request,
1040 			PM_QOS_CPU_DMA_LATENCY, 20);
1041 	dmaq->seq_nr = 0;
1042 
1043 	return 0;
1044 }
1045 
1046 void saa7134_vb2_stop_streaming(struct vb2_queue *vq)
1047 {
1048 	struct saa7134_dmaqueue *dmaq = vq->drv_priv;
1049 	struct saa7134_dev *dev = dmaq->dev;
1050 
1051 	saa7134_stop_streaming(dev, dmaq);
1052 
1053 	if ((dmaq == &dev->video_q && !vb2_is_streaming(&dev->vbi_vbq)) ||
1054 	    (dmaq == &dev->vbi_q && !vb2_is_streaming(&dev->video_vbq)))
1055 		pm_qos_remove_request(&dev->qos_request);
1056 }
1057 
1058 static struct vb2_ops vb2_qops = {
1059 	.queue_setup	= queue_setup,
1060 	.buf_init	= buffer_init,
1061 	.buf_prepare	= buffer_prepare,
1062 	.buf_queue	= saa7134_vb2_buffer_queue,
1063 	.wait_prepare	= vb2_ops_wait_prepare,
1064 	.wait_finish	= vb2_ops_wait_finish,
1065 	.start_streaming = saa7134_vb2_start_streaming,
1066 	.stop_streaming = saa7134_vb2_stop_streaming,
1067 };
1068 
1069 /* ------------------------------------------------------------------ */
1070 
1071 static int saa7134_s_ctrl(struct v4l2_ctrl *ctrl)
1072 {
1073 	struct saa7134_dev *dev = container_of(ctrl->handler, struct saa7134_dev, ctrl_handler);
1074 	unsigned long flags;
1075 	int restart_overlay = 0;
1076 
1077 	switch (ctrl->id) {
1078 	case V4L2_CID_BRIGHTNESS:
1079 		dev->ctl_bright = ctrl->val;
1080 		saa_writeb(SAA7134_DEC_LUMA_BRIGHT, ctrl->val);
1081 		break;
1082 	case V4L2_CID_HUE:
1083 		dev->ctl_hue = ctrl->val;
1084 		saa_writeb(SAA7134_DEC_CHROMA_HUE, ctrl->val);
1085 		break;
1086 	case V4L2_CID_CONTRAST:
1087 		dev->ctl_contrast = ctrl->val;
1088 		saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1089 			   dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1090 		break;
1091 	case V4L2_CID_SATURATION:
1092 		dev->ctl_saturation = ctrl->val;
1093 		saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1094 			   dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1095 		break;
1096 	case V4L2_CID_AUDIO_MUTE:
1097 		dev->ctl_mute = ctrl->val;
1098 		saa7134_tvaudio_setmute(dev);
1099 		break;
1100 	case V4L2_CID_AUDIO_VOLUME:
1101 		dev->ctl_volume = ctrl->val;
1102 		saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
1103 		break;
1104 	case V4L2_CID_PRIVATE_INVERT:
1105 		dev->ctl_invert = ctrl->val;
1106 		saa_writeb(SAA7134_DEC_LUMA_CONTRAST,
1107 			   dev->ctl_invert ? -dev->ctl_contrast : dev->ctl_contrast);
1108 		saa_writeb(SAA7134_DEC_CHROMA_SATURATION,
1109 			   dev->ctl_invert ? -dev->ctl_saturation : dev->ctl_saturation);
1110 		break;
1111 	case V4L2_CID_HFLIP:
1112 		dev->ctl_mirror = ctrl->val;
1113 		restart_overlay = 1;
1114 		break;
1115 	case V4L2_CID_PRIVATE_Y_EVEN:
1116 		dev->ctl_y_even = ctrl->val;
1117 		restart_overlay = 1;
1118 		break;
1119 	case V4L2_CID_PRIVATE_Y_ODD:
1120 		dev->ctl_y_odd = ctrl->val;
1121 		restart_overlay = 1;
1122 		break;
1123 	case V4L2_CID_PRIVATE_AUTOMUTE:
1124 	{
1125 		struct v4l2_priv_tun_config tda9887_cfg;
1126 
1127 		tda9887_cfg.tuner = TUNER_TDA9887;
1128 		tda9887_cfg.priv = &dev->tda9887_conf;
1129 
1130 		dev->ctl_automute = ctrl->val;
1131 		if (dev->tda9887_conf) {
1132 			if (dev->ctl_automute)
1133 				dev->tda9887_conf |= TDA9887_AUTOMUTE;
1134 			else
1135 				dev->tda9887_conf &= ~TDA9887_AUTOMUTE;
1136 
1137 			saa_call_all(dev, tuner, s_config, &tda9887_cfg);
1138 		}
1139 		break;
1140 	}
1141 	default:
1142 		return -EINVAL;
1143 	}
1144 	if (restart_overlay && dev->overlay_owner) {
1145 		spin_lock_irqsave(&dev->slock, flags);
1146 		stop_preview(dev);
1147 		start_preview(dev);
1148 		spin_unlock_irqrestore(&dev->slock, flags);
1149 	}
1150 	return 0;
1151 }
1152 
1153 /* ------------------------------------------------------------------ */
1154 
1155 static int video_open(struct file *file)
1156 {
1157 	struct video_device *vdev = video_devdata(file);
1158 	struct saa7134_dev *dev = video_drvdata(file);
1159 	int ret = v4l2_fh_open(file);
1160 
1161 	if (ret < 0)
1162 		return ret;
1163 
1164 	mutex_lock(&dev->lock);
1165 	if (vdev->vfl_type == VFL_TYPE_RADIO) {
1166 		/* switch to radio mode */
1167 		saa7134_tvaudio_setinput(dev, &card(dev).radio);
1168 		saa_call_all(dev, tuner, s_radio);
1169 	} else {
1170 		/* switch to video/vbi mode */
1171 		video_mux(dev, dev->ctl_input);
1172 	}
1173 	mutex_unlock(&dev->lock);
1174 
1175 	return 0;
1176 }
1177 
1178 static int video_release(struct file *file)
1179 {
1180 	struct video_device *vdev = video_devdata(file);
1181 	struct saa7134_dev *dev = video_drvdata(file);
1182 	struct v4l2_fh *fh = file->private_data;
1183 	struct saa6588_command cmd;
1184 	unsigned long flags;
1185 
1186 	mutex_lock(&dev->lock);
1187 	saa7134_tvaudio_close(dev);
1188 
1189 	/* turn off overlay */
1190 	if (fh == dev->overlay_owner) {
1191 		spin_lock_irqsave(&dev->slock,flags);
1192 		stop_preview(dev);
1193 		spin_unlock_irqrestore(&dev->slock,flags);
1194 		dev->overlay_owner = NULL;
1195 	}
1196 
1197 	if (vdev->vfl_type == VFL_TYPE_RADIO)
1198 		v4l2_fh_release(file);
1199 	else
1200 		_vb2_fop_release(file, NULL);
1201 
1202 	/* ts-capture will not work in planar mode, so turn it off Hac: 04.05*/
1203 	saa_andorb(SAA7134_OFMT_VIDEO_A, 0x1f, 0);
1204 	saa_andorb(SAA7134_OFMT_VIDEO_B, 0x1f, 0);
1205 	saa_andorb(SAA7134_OFMT_DATA_A, 0x1f, 0);
1206 	saa_andorb(SAA7134_OFMT_DATA_B, 0x1f, 0);
1207 
1208 	saa_call_all(dev, core, s_power, 0);
1209 	if (vdev->vfl_type == VFL_TYPE_RADIO)
1210 		saa_call_all(dev, core, ioctl, SAA6588_CMD_CLOSE, &cmd);
1211 	mutex_unlock(&dev->lock);
1212 
1213 	return 0;
1214 }
1215 
1216 static ssize_t radio_read(struct file *file, char __user *data,
1217 			 size_t count, loff_t *ppos)
1218 {
1219 	struct saa7134_dev *dev = video_drvdata(file);
1220 	struct saa6588_command cmd;
1221 
1222 	cmd.block_count = count/3;
1223 	cmd.nonblocking = file->f_flags & O_NONBLOCK;
1224 	cmd.buffer = data;
1225 	cmd.instance = file;
1226 	cmd.result = -ENODEV;
1227 
1228 	mutex_lock(&dev->lock);
1229 	saa_call_all(dev, core, ioctl, SAA6588_CMD_READ, &cmd);
1230 	mutex_unlock(&dev->lock);
1231 
1232 	return cmd.result;
1233 }
1234 
1235 static unsigned int radio_poll(struct file *file, poll_table *wait)
1236 {
1237 	struct saa7134_dev *dev = video_drvdata(file);
1238 	struct saa6588_command cmd;
1239 	unsigned int rc = v4l2_ctrl_poll(file, wait);
1240 
1241 	cmd.instance = file;
1242 	cmd.event_list = wait;
1243 	cmd.result = 0;
1244 	mutex_lock(&dev->lock);
1245 	saa_call_all(dev, core, ioctl, SAA6588_CMD_POLL, &cmd);
1246 	mutex_unlock(&dev->lock);
1247 
1248 	return rc | cmd.result;
1249 }
1250 
1251 /* ------------------------------------------------------------------ */
1252 
1253 static int saa7134_try_get_set_fmt_vbi_cap(struct file *file, void *priv,
1254 						struct v4l2_format *f)
1255 {
1256 	struct saa7134_dev *dev = video_drvdata(file);
1257 	struct saa7134_tvnorm *norm = dev->tvnorm;
1258 
1259 	memset(&f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
1260 	f->fmt.vbi.sampling_rate = 6750000 * 4;
1261 	f->fmt.vbi.samples_per_line = 2048 /* VBI_LINE_LENGTH */;
1262 	f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
1263 	f->fmt.vbi.offset = 64 * 4;
1264 	f->fmt.vbi.start[0] = norm->vbi_v_start_0;
1265 	f->fmt.vbi.count[0] = norm->vbi_v_stop_0 - norm->vbi_v_start_0 +1;
1266 	f->fmt.vbi.start[1] = norm->vbi_v_start_1;
1267 	f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
1268 	f->fmt.vbi.flags = 0; /* VBI_UNSYNC VBI_INTERLACED */
1269 
1270 	return 0;
1271 }
1272 
1273 static int saa7134_g_fmt_vid_cap(struct file *file, void *priv,
1274 				struct v4l2_format *f)
1275 {
1276 	struct saa7134_dev *dev = video_drvdata(file);
1277 
1278 	f->fmt.pix.width        = dev->width;
1279 	f->fmt.pix.height       = dev->height;
1280 	f->fmt.pix.field        = dev->field;
1281 	f->fmt.pix.pixelformat  = dev->fmt->fourcc;
1282 	f->fmt.pix.bytesperline =
1283 		(f->fmt.pix.width * dev->fmt->depth) >> 3;
1284 	f->fmt.pix.sizeimage =
1285 		f->fmt.pix.height * f->fmt.pix.bytesperline;
1286 	f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
1287 	return 0;
1288 }
1289 
1290 static int saa7134_g_fmt_vid_overlay(struct file *file, void *priv,
1291 				struct v4l2_format *f)
1292 {
1293 	struct saa7134_dev *dev = video_drvdata(file);
1294 	struct v4l2_clip __user *clips = f->fmt.win.clips;
1295 	u32 clipcount = f->fmt.win.clipcount;
1296 	int err = 0;
1297 	int i;
1298 
1299 	if (saa7134_no_overlay > 0) {
1300 		pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1301 		return -EINVAL;
1302 	}
1303 	f->fmt.win = dev->win;
1304 	f->fmt.win.clips = clips;
1305 	if (clips == NULL)
1306 		clipcount = 0;
1307 	if (dev->nclips < clipcount)
1308 		clipcount = dev->nclips;
1309 	f->fmt.win.clipcount = clipcount;
1310 
1311 	for (i = 0; !err && i < clipcount; i++) {
1312 		if (copy_to_user(&f->fmt.win.clips[i].c, &dev->clips[i].c,
1313 					sizeof(struct v4l2_rect)))
1314 			err = -EFAULT;
1315 	}
1316 
1317 	return err;
1318 }
1319 
1320 static int saa7134_try_fmt_vid_cap(struct file *file, void *priv,
1321 						struct v4l2_format *f)
1322 {
1323 	struct saa7134_dev *dev = video_drvdata(file);
1324 	struct saa7134_format *fmt;
1325 	enum v4l2_field field;
1326 	unsigned int maxw, maxh;
1327 
1328 	fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1329 	if (NULL == fmt)
1330 		return -EINVAL;
1331 
1332 	field = f->fmt.pix.field;
1333 	maxw  = min(dev->crop_current.width*4,  dev->crop_bounds.width);
1334 	maxh  = min(dev->crop_current.height*4, dev->crop_bounds.height);
1335 
1336 	if (V4L2_FIELD_ANY == field) {
1337 		field = (f->fmt.pix.height > maxh/2)
1338 			? V4L2_FIELD_INTERLACED
1339 			: V4L2_FIELD_BOTTOM;
1340 	}
1341 	switch (field) {
1342 	case V4L2_FIELD_TOP:
1343 	case V4L2_FIELD_BOTTOM:
1344 		maxh = maxh / 2;
1345 		break;
1346 	default:
1347 		field = V4L2_FIELD_INTERLACED;
1348 		break;
1349 	}
1350 
1351 	f->fmt.pix.field = field;
1352 	if (f->fmt.pix.width  < 48)
1353 		f->fmt.pix.width  = 48;
1354 	if (f->fmt.pix.height < 32)
1355 		f->fmt.pix.height = 32;
1356 	if (f->fmt.pix.width > maxw)
1357 		f->fmt.pix.width = maxw;
1358 	if (f->fmt.pix.height > maxh)
1359 		f->fmt.pix.height = maxh;
1360 	f->fmt.pix.width &= ~0x03;
1361 	f->fmt.pix.bytesperline =
1362 		(f->fmt.pix.width * fmt->depth) >> 3;
1363 	f->fmt.pix.sizeimage =
1364 		f->fmt.pix.height * f->fmt.pix.bytesperline;
1365 	f->fmt.pix.colorspace   = V4L2_COLORSPACE_SMPTE170M;
1366 
1367 	return 0;
1368 }
1369 
1370 static int saa7134_try_fmt_vid_overlay(struct file *file, void *priv,
1371 						struct v4l2_format *f)
1372 {
1373 	struct saa7134_dev *dev = video_drvdata(file);
1374 
1375 	if (saa7134_no_overlay > 0) {
1376 		pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1377 		return -EINVAL;
1378 	}
1379 
1380 	if (f->fmt.win.clips == NULL)
1381 		f->fmt.win.clipcount = 0;
1382 	return verify_preview(dev, &f->fmt.win, true);
1383 }
1384 
1385 static int saa7134_s_fmt_vid_cap(struct file *file, void *priv,
1386 					struct v4l2_format *f)
1387 {
1388 	struct saa7134_dev *dev = video_drvdata(file);
1389 	int err;
1390 
1391 	err = saa7134_try_fmt_vid_cap(file, priv, f);
1392 	if (0 != err)
1393 		return err;
1394 
1395 	dev->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
1396 	dev->width = f->fmt.pix.width;
1397 	dev->height = f->fmt.pix.height;
1398 	dev->field = f->fmt.pix.field;
1399 	return 0;
1400 }
1401 
1402 static int saa7134_s_fmt_vid_overlay(struct file *file, void *priv,
1403 					struct v4l2_format *f)
1404 {
1405 	struct saa7134_dev *dev = video_drvdata(file);
1406 	int err;
1407 	unsigned long flags;
1408 
1409 	if (saa7134_no_overlay > 0) {
1410 		pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1411 		return -EINVAL;
1412 	}
1413 	if (f->fmt.win.clips == NULL)
1414 		f->fmt.win.clipcount = 0;
1415 	err = verify_preview(dev, &f->fmt.win, true);
1416 	if (0 != err)
1417 		return err;
1418 
1419 	dev->win    = f->fmt.win;
1420 	dev->nclips = f->fmt.win.clipcount;
1421 
1422 	if (copy_from_user(dev->clips, f->fmt.win.clips,
1423 			   sizeof(struct v4l2_clip) * dev->nclips))
1424 		return -EFAULT;
1425 
1426 	if (priv == dev->overlay_owner) {
1427 		spin_lock_irqsave(&dev->slock, flags);
1428 		stop_preview(dev);
1429 		start_preview(dev);
1430 		spin_unlock_irqrestore(&dev->slock, flags);
1431 	}
1432 
1433 	return 0;
1434 }
1435 
1436 int saa7134_enum_input(struct file *file, void *priv, struct v4l2_input *i)
1437 {
1438 	struct saa7134_dev *dev = video_drvdata(file);
1439 	unsigned int n;
1440 
1441 	n = i->index;
1442 	if (n >= SAA7134_INPUT_MAX)
1443 		return -EINVAL;
1444 	if (card_in(dev, i->index).type == SAA7134_NO_INPUT)
1445 		return -EINVAL;
1446 	i->index = n;
1447 	strcpy(i->name, saa7134_input_name[card_in(dev, n).type]);
1448 	switch (card_in(dev, n).type) {
1449 	case SAA7134_INPUT_TV:
1450 	case SAA7134_INPUT_TV_MONO:
1451 		i->type = V4L2_INPUT_TYPE_TUNER;
1452 		break;
1453 	default:
1454 		i->type  = V4L2_INPUT_TYPE_CAMERA;
1455 		break;
1456 	}
1457 	if (n == dev->ctl_input) {
1458 		int v1 = saa_readb(SAA7134_STATUS_VIDEO1);
1459 		int v2 = saa_readb(SAA7134_STATUS_VIDEO2);
1460 
1461 		if (0 != (v1 & 0x40))
1462 			i->status |= V4L2_IN_ST_NO_H_LOCK;
1463 		if (0 != (v2 & 0x40))
1464 			i->status |= V4L2_IN_ST_NO_SIGNAL;
1465 		if (0 != (v2 & 0x0e))
1466 			i->status |= V4L2_IN_ST_MACROVISION;
1467 	}
1468 	i->std = SAA7134_NORMS;
1469 	return 0;
1470 }
1471 EXPORT_SYMBOL_GPL(saa7134_enum_input);
1472 
1473 int saa7134_g_input(struct file *file, void *priv, unsigned int *i)
1474 {
1475 	struct saa7134_dev *dev = video_drvdata(file);
1476 
1477 	*i = dev->ctl_input;
1478 	return 0;
1479 }
1480 EXPORT_SYMBOL_GPL(saa7134_g_input);
1481 
1482 int saa7134_s_input(struct file *file, void *priv, unsigned int i)
1483 {
1484 	struct saa7134_dev *dev = video_drvdata(file);
1485 
1486 	if (i >= SAA7134_INPUT_MAX)
1487 		return -EINVAL;
1488 	if (card_in(dev, i).type == SAA7134_NO_INPUT)
1489 		return -EINVAL;
1490 	video_mux(dev, i);
1491 	return 0;
1492 }
1493 EXPORT_SYMBOL_GPL(saa7134_s_input);
1494 
1495 int saa7134_querycap(struct file *file, void *priv,
1496 					struct v4l2_capability *cap)
1497 {
1498 	struct saa7134_dev *dev = video_drvdata(file);
1499 	struct video_device *vdev = video_devdata(file);
1500 	u32 radio_caps, video_caps, vbi_caps;
1501 
1502 	unsigned int tuner_type = dev->tuner_type;
1503 
1504 	strcpy(cap->driver, "saa7134");
1505 	strlcpy(cap->card, saa7134_boards[dev->board].name,
1506 		sizeof(cap->card));
1507 	sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
1508 
1509 	cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
1510 	if ((tuner_type != TUNER_ABSENT) && (tuner_type != UNSET))
1511 		cap->device_caps |= V4L2_CAP_TUNER;
1512 
1513 	radio_caps = V4L2_CAP_RADIO;
1514 	if (dev->has_rds)
1515 		radio_caps |= V4L2_CAP_RDS_CAPTURE;
1516 
1517 	video_caps = V4L2_CAP_VIDEO_CAPTURE;
1518 	if (saa7134_no_overlay <= 0 && !is_empress(file))
1519 		video_caps |= V4L2_CAP_VIDEO_OVERLAY;
1520 
1521 	vbi_caps = V4L2_CAP_VBI_CAPTURE;
1522 
1523 	switch (vdev->vfl_type) {
1524 	case VFL_TYPE_RADIO:
1525 		cap->device_caps |= radio_caps;
1526 		break;
1527 	case VFL_TYPE_GRABBER:
1528 		cap->device_caps |= video_caps;
1529 		break;
1530 	case VFL_TYPE_VBI:
1531 		cap->device_caps |= vbi_caps;
1532 		break;
1533 	}
1534 	cap->capabilities = radio_caps | video_caps | vbi_caps |
1535 		cap->device_caps | V4L2_CAP_DEVICE_CAPS;
1536 	if (vdev->vfl_type == VFL_TYPE_RADIO) {
1537 		cap->device_caps &= ~V4L2_CAP_STREAMING;
1538 		if (!dev->has_rds)
1539 			cap->device_caps &= ~V4L2_CAP_READWRITE;
1540 	}
1541 
1542 	return 0;
1543 }
1544 EXPORT_SYMBOL_GPL(saa7134_querycap);
1545 
1546 int saa7134_s_std(struct file *file, void *priv, v4l2_std_id id)
1547 {
1548 	struct saa7134_dev *dev = video_drvdata(file);
1549 	struct v4l2_fh *fh = priv;
1550 	unsigned long flags;
1551 	unsigned int i;
1552 	v4l2_std_id fixup;
1553 
1554 	if (is_empress(file) && dev->overlay_owner) {
1555 		/* Don't change the std from the mpeg device
1556 		   if overlay is active. */
1557 		return -EBUSY;
1558 	}
1559 
1560 	for (i = 0; i < TVNORMS; i++)
1561 		if (id == tvnorms[i].id)
1562 			break;
1563 
1564 	if (i == TVNORMS)
1565 		for (i = 0; i < TVNORMS; i++)
1566 			if (id & tvnorms[i].id)
1567 				break;
1568 	if (i == TVNORMS)
1569 		return -EINVAL;
1570 
1571 	if ((id & V4L2_STD_SECAM) && (secam[0] != '-')) {
1572 		if (secam[0] == 'L' || secam[0] == 'l') {
1573 			if (secam[1] == 'C' || secam[1] == 'c')
1574 				fixup = V4L2_STD_SECAM_LC;
1575 			else
1576 				fixup = V4L2_STD_SECAM_L;
1577 		} else {
1578 			if (secam[0] == 'D' || secam[0] == 'd')
1579 				fixup = V4L2_STD_SECAM_DK;
1580 			else
1581 				fixup = V4L2_STD_SECAM;
1582 		}
1583 		for (i = 0; i < TVNORMS; i++) {
1584 			if (fixup == tvnorms[i].id)
1585 				break;
1586 		}
1587 		if (i == TVNORMS)
1588 			return -EINVAL;
1589 	}
1590 
1591 	id = tvnorms[i].id;
1592 
1593 	if (!is_empress(file) && fh == dev->overlay_owner) {
1594 		spin_lock_irqsave(&dev->slock, flags);
1595 		stop_preview(dev);
1596 		spin_unlock_irqrestore(&dev->slock, flags);
1597 
1598 		set_tvnorm(dev, &tvnorms[i]);
1599 
1600 		spin_lock_irqsave(&dev->slock, flags);
1601 		start_preview(dev);
1602 		spin_unlock_irqrestore(&dev->slock, flags);
1603 	} else
1604 		set_tvnorm(dev, &tvnorms[i]);
1605 
1606 	saa7134_tvaudio_do_scan(dev);
1607 	return 0;
1608 }
1609 EXPORT_SYMBOL_GPL(saa7134_s_std);
1610 
1611 int saa7134_g_std(struct file *file, void *priv, v4l2_std_id *id)
1612 {
1613 	struct saa7134_dev *dev = video_drvdata(file);
1614 
1615 	*id = dev->tvnorm->id;
1616 	return 0;
1617 }
1618 EXPORT_SYMBOL_GPL(saa7134_g_std);
1619 
1620 static v4l2_std_id saa7134_read_std(struct saa7134_dev *dev)
1621 {
1622 	static v4l2_std_id stds[] = {
1623 		V4L2_STD_UNKNOWN,
1624 		V4L2_STD_NTSC,
1625 		V4L2_STD_PAL,
1626 		V4L2_STD_SECAM };
1627 
1628 	v4l2_std_id result = 0;
1629 
1630 	u8 st1 = saa_readb(SAA7134_STATUS_VIDEO1);
1631 	u8 st2 = saa_readb(SAA7134_STATUS_VIDEO2);
1632 
1633 	if (!(st2 & 0x1)) /* RDCAP == 0 */
1634 		result = V4L2_STD_UNKNOWN;
1635 	else
1636 		result = stds[st1 & 0x03];
1637 
1638 	return result;
1639 }
1640 
1641 int saa7134_querystd(struct file *file, void *priv, v4l2_std_id *std)
1642 {
1643 	struct saa7134_dev *dev = video_drvdata(file);
1644 	*std &= saa7134_read_std(dev);
1645 	return 0;
1646 }
1647 EXPORT_SYMBOL_GPL(saa7134_querystd);
1648 
1649 static int saa7134_cropcap(struct file *file, void *priv,
1650 					struct v4l2_cropcap *cap)
1651 {
1652 	struct saa7134_dev *dev = video_drvdata(file);
1653 
1654 	if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1655 	    cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1656 		return -EINVAL;
1657 	cap->bounds  = dev->crop_bounds;
1658 	cap->defrect = dev->crop_defrect;
1659 	cap->pixelaspect.numerator   = 1;
1660 	cap->pixelaspect.denominator = 1;
1661 	if (dev->tvnorm->id & V4L2_STD_525_60) {
1662 		cap->pixelaspect.numerator   = 11;
1663 		cap->pixelaspect.denominator = 10;
1664 	}
1665 	if (dev->tvnorm->id & V4L2_STD_625_50) {
1666 		cap->pixelaspect.numerator   = 54;
1667 		cap->pixelaspect.denominator = 59;
1668 	}
1669 	return 0;
1670 }
1671 
1672 static int saa7134_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
1673 {
1674 	struct saa7134_dev *dev = video_drvdata(file);
1675 
1676 	if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1677 	    crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1678 		return -EINVAL;
1679 	crop->c = dev->crop_current;
1680 	return 0;
1681 }
1682 
1683 static int saa7134_s_crop(struct file *file, void *f, const struct v4l2_crop *crop)
1684 {
1685 	struct saa7134_dev *dev = video_drvdata(file);
1686 	struct v4l2_rect *b = &dev->crop_bounds;
1687 	struct v4l2_rect *c = &dev->crop_current;
1688 
1689 	if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1690 	    crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
1691 		return -EINVAL;
1692 
1693 	if (dev->overlay_owner)
1694 		return -EBUSY;
1695 	if (vb2_is_streaming(&dev->video_vbq))
1696 		return -EBUSY;
1697 
1698 	*c = crop->c;
1699 	if (c->top < b->top)
1700 		c->top = b->top;
1701 	if (c->top > b->top + b->height)
1702 		c->top = b->top + b->height;
1703 	if (c->height > b->top - c->top + b->height)
1704 		c->height = b->top - c->top + b->height;
1705 
1706 	if (c->left < b->left)
1707 		c->left = b->left;
1708 	if (c->left > b->left + b->width)
1709 		c->left = b->left + b->width;
1710 	if (c->width > b->left - c->left + b->width)
1711 		c->width = b->left - c->left + b->width;
1712 	return 0;
1713 }
1714 
1715 int saa7134_g_tuner(struct file *file, void *priv,
1716 					struct v4l2_tuner *t)
1717 {
1718 	struct saa7134_dev *dev = video_drvdata(file);
1719 	int n;
1720 
1721 	if (0 != t->index)
1722 		return -EINVAL;
1723 	memset(t, 0, sizeof(*t));
1724 	for (n = 0; n < SAA7134_INPUT_MAX; n++) {
1725 		if (card_in(dev, n).type == SAA7134_INPUT_TV ||
1726 		    card_in(dev, n).type == SAA7134_INPUT_TV_MONO)
1727 			break;
1728 	}
1729 	if (n == SAA7134_INPUT_MAX)
1730 		return -EINVAL;
1731 	if (card_in(dev, n).type != SAA7134_NO_INPUT) {
1732 		strcpy(t->name, "Television");
1733 		t->type = V4L2_TUNER_ANALOG_TV;
1734 		saa_call_all(dev, tuner, g_tuner, t);
1735 		t->capability = V4L2_TUNER_CAP_NORM |
1736 			V4L2_TUNER_CAP_STEREO |
1737 			V4L2_TUNER_CAP_LANG1 |
1738 			V4L2_TUNER_CAP_LANG2;
1739 		t->rxsubchans = saa7134_tvaudio_getstereo(dev);
1740 		t->audmode = saa7134_tvaudio_rx2mode(t->rxsubchans);
1741 	}
1742 	if (0 != (saa_readb(SAA7134_STATUS_VIDEO1) & 0x03))
1743 		t->signal = 0xffff;
1744 	return 0;
1745 }
1746 EXPORT_SYMBOL_GPL(saa7134_g_tuner);
1747 
1748 int saa7134_s_tuner(struct file *file, void *priv,
1749 					const struct v4l2_tuner *t)
1750 {
1751 	struct saa7134_dev *dev = video_drvdata(file);
1752 	int rx, mode;
1753 
1754 	if (0 != t->index)
1755 		return -EINVAL;
1756 
1757 	mode = dev->thread.mode;
1758 	if (UNSET == mode) {
1759 		rx   = saa7134_tvaudio_getstereo(dev);
1760 		mode = saa7134_tvaudio_rx2mode(rx);
1761 	}
1762 	if (mode != t->audmode)
1763 		dev->thread.mode = t->audmode;
1764 
1765 	return 0;
1766 }
1767 EXPORT_SYMBOL_GPL(saa7134_s_tuner);
1768 
1769 int saa7134_g_frequency(struct file *file, void *priv,
1770 					struct v4l2_frequency *f)
1771 {
1772 	struct saa7134_dev *dev = video_drvdata(file);
1773 
1774 	if (0 != f->tuner)
1775 		return -EINVAL;
1776 
1777 	saa_call_all(dev, tuner, g_frequency, f);
1778 
1779 	return 0;
1780 }
1781 EXPORT_SYMBOL_GPL(saa7134_g_frequency);
1782 
1783 int saa7134_s_frequency(struct file *file, void *priv,
1784 					const struct v4l2_frequency *f)
1785 {
1786 	struct saa7134_dev *dev = video_drvdata(file);
1787 
1788 	if (0 != f->tuner)
1789 		return -EINVAL;
1790 
1791 	saa_call_all(dev, tuner, s_frequency, f);
1792 
1793 	saa7134_tvaudio_do_scan(dev);
1794 	return 0;
1795 }
1796 EXPORT_SYMBOL_GPL(saa7134_s_frequency);
1797 
1798 static int saa7134_enum_fmt_vid_cap(struct file *file, void  *priv,
1799 					struct v4l2_fmtdesc *f)
1800 {
1801 	if (f->index >= FORMATS)
1802 		return -EINVAL;
1803 
1804 	strlcpy(f->description, formats[f->index].name,
1805 		sizeof(f->description));
1806 
1807 	f->pixelformat = formats[f->index].fourcc;
1808 
1809 	return 0;
1810 }
1811 
1812 static int saa7134_enum_fmt_vid_overlay(struct file *file, void  *priv,
1813 					struct v4l2_fmtdesc *f)
1814 {
1815 	if (saa7134_no_overlay > 0) {
1816 		pr_err("V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
1817 		return -EINVAL;
1818 	}
1819 
1820 	if ((f->index >= FORMATS) || formats[f->index].planar)
1821 		return -EINVAL;
1822 
1823 	strlcpy(f->description, formats[f->index].name,
1824 		sizeof(f->description));
1825 
1826 	f->pixelformat = formats[f->index].fourcc;
1827 
1828 	return 0;
1829 }
1830 
1831 static int saa7134_g_fbuf(struct file *file, void *f,
1832 				struct v4l2_framebuffer *fb)
1833 {
1834 	struct saa7134_dev *dev = video_drvdata(file);
1835 
1836 	*fb = dev->ovbuf;
1837 	fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
1838 
1839 	return 0;
1840 }
1841 
1842 static int saa7134_s_fbuf(struct file *file, void *f,
1843 					const struct v4l2_framebuffer *fb)
1844 {
1845 	struct saa7134_dev *dev = video_drvdata(file);
1846 	struct saa7134_format *fmt;
1847 
1848 	if (!capable(CAP_SYS_ADMIN) &&
1849 	   !capable(CAP_SYS_RAWIO))
1850 		return -EPERM;
1851 
1852 	/* check args */
1853 	fmt = format_by_fourcc(fb->fmt.pixelformat);
1854 	if (NULL == fmt)
1855 		return -EINVAL;
1856 
1857 	/* ok, accept it */
1858 	dev->ovbuf = *fb;
1859 	dev->ovfmt = fmt;
1860 	if (0 == dev->ovbuf.fmt.bytesperline)
1861 		dev->ovbuf.fmt.bytesperline =
1862 			dev->ovbuf.fmt.width*fmt->depth/8;
1863 	return 0;
1864 }
1865 
1866 static int saa7134_overlay(struct file *file, void *priv, unsigned int on)
1867 {
1868 	struct saa7134_dev *dev = video_drvdata(file);
1869 	unsigned long flags;
1870 
1871 	if (on) {
1872 		if (saa7134_no_overlay > 0) {
1873 			video_dbg("no_overlay\n");
1874 			return -EINVAL;
1875 		}
1876 
1877 		if (dev->overlay_owner && priv != dev->overlay_owner)
1878 			return -EBUSY;
1879 		dev->overlay_owner = priv;
1880 		spin_lock_irqsave(&dev->slock, flags);
1881 		start_preview(dev);
1882 		spin_unlock_irqrestore(&dev->slock, flags);
1883 	}
1884 	if (!on) {
1885 		if (priv != dev->overlay_owner)
1886 			return -EINVAL;
1887 		spin_lock_irqsave(&dev->slock, flags);
1888 		stop_preview(dev);
1889 		spin_unlock_irqrestore(&dev->slock, flags);
1890 		dev->overlay_owner = NULL;
1891 	}
1892 	return 0;
1893 }
1894 
1895 #ifdef CONFIG_VIDEO_ADV_DEBUG
1896 static int vidioc_g_register (struct file *file, void *priv,
1897 			      struct v4l2_dbg_register *reg)
1898 {
1899 	struct saa7134_dev *dev = video_drvdata(file);
1900 
1901 	reg->val = saa_readb(reg->reg & 0xffffff);
1902 	reg->size = 1;
1903 	return 0;
1904 }
1905 
1906 static int vidioc_s_register (struct file *file, void *priv,
1907 				const struct v4l2_dbg_register *reg)
1908 {
1909 	struct saa7134_dev *dev = video_drvdata(file);
1910 
1911 	saa_writeb(reg->reg & 0xffffff, reg->val);
1912 	return 0;
1913 }
1914 #endif
1915 
1916 static int radio_g_tuner(struct file *file, void *priv,
1917 					struct v4l2_tuner *t)
1918 {
1919 	struct saa7134_dev *dev = video_drvdata(file);
1920 
1921 	if (0 != t->index)
1922 		return -EINVAL;
1923 
1924 	strcpy(t->name, "Radio");
1925 
1926 	saa_call_all(dev, tuner, g_tuner, t);
1927 	t->audmode &= V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO;
1928 	if (dev->input->amux == TV) {
1929 		t->signal = 0xf800 - ((saa_readb(0x581) & 0x1f) << 11);
1930 		t->rxsubchans = (saa_readb(0x529) & 0x08) ?
1931 				V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
1932 	}
1933 	return 0;
1934 }
1935 static int radio_s_tuner(struct file *file, void *priv,
1936 					const struct v4l2_tuner *t)
1937 {
1938 	struct saa7134_dev *dev = video_drvdata(file);
1939 
1940 	if (0 != t->index)
1941 		return -EINVAL;
1942 
1943 	saa_call_all(dev, tuner, s_tuner, t);
1944 	return 0;
1945 }
1946 
1947 static const struct v4l2_file_operations video_fops =
1948 {
1949 	.owner	  = THIS_MODULE,
1950 	.open	  = video_open,
1951 	.release  = video_release,
1952 	.read	  = vb2_fop_read,
1953 	.poll     = vb2_fop_poll,
1954 	.mmap	  = vb2_fop_mmap,
1955 	.unlocked_ioctl	  = video_ioctl2,
1956 };
1957 
1958 static const struct v4l2_ioctl_ops video_ioctl_ops = {
1959 	.vidioc_querycap		= saa7134_querycap,
1960 	.vidioc_enum_fmt_vid_cap	= saa7134_enum_fmt_vid_cap,
1961 	.vidioc_g_fmt_vid_cap		= saa7134_g_fmt_vid_cap,
1962 	.vidioc_try_fmt_vid_cap		= saa7134_try_fmt_vid_cap,
1963 	.vidioc_s_fmt_vid_cap		= saa7134_s_fmt_vid_cap,
1964 	.vidioc_enum_fmt_vid_overlay	= saa7134_enum_fmt_vid_overlay,
1965 	.vidioc_g_fmt_vid_overlay	= saa7134_g_fmt_vid_overlay,
1966 	.vidioc_try_fmt_vid_overlay	= saa7134_try_fmt_vid_overlay,
1967 	.vidioc_s_fmt_vid_overlay	= saa7134_s_fmt_vid_overlay,
1968 	.vidioc_g_fmt_vbi_cap		= saa7134_try_get_set_fmt_vbi_cap,
1969 	.vidioc_try_fmt_vbi_cap		= saa7134_try_get_set_fmt_vbi_cap,
1970 	.vidioc_s_fmt_vbi_cap		= saa7134_try_get_set_fmt_vbi_cap,
1971 	.vidioc_cropcap			= saa7134_cropcap,
1972 	.vidioc_reqbufs			= vb2_ioctl_reqbufs,
1973 	.vidioc_querybuf		= vb2_ioctl_querybuf,
1974 	.vidioc_qbuf			= vb2_ioctl_qbuf,
1975 	.vidioc_dqbuf			= vb2_ioctl_dqbuf,
1976 	.vidioc_expbuf			= vb2_ioctl_expbuf,
1977 	.vidioc_s_std			= saa7134_s_std,
1978 	.vidioc_g_std			= saa7134_g_std,
1979 	.vidioc_querystd		= saa7134_querystd,
1980 	.vidioc_enum_input		= saa7134_enum_input,
1981 	.vidioc_g_input			= saa7134_g_input,
1982 	.vidioc_s_input			= saa7134_s_input,
1983 	.vidioc_streamon		= vb2_ioctl_streamon,
1984 	.vidioc_streamoff		= vb2_ioctl_streamoff,
1985 	.vidioc_g_tuner			= saa7134_g_tuner,
1986 	.vidioc_s_tuner			= saa7134_s_tuner,
1987 	.vidioc_g_crop			= saa7134_g_crop,
1988 	.vidioc_s_crop			= saa7134_s_crop,
1989 	.vidioc_g_fbuf			= saa7134_g_fbuf,
1990 	.vidioc_s_fbuf			= saa7134_s_fbuf,
1991 	.vidioc_overlay			= saa7134_overlay,
1992 	.vidioc_g_frequency		= saa7134_g_frequency,
1993 	.vidioc_s_frequency		= saa7134_s_frequency,
1994 #ifdef CONFIG_VIDEO_ADV_DEBUG
1995 	.vidioc_g_register              = vidioc_g_register,
1996 	.vidioc_s_register              = vidioc_s_register,
1997 #endif
1998 	.vidioc_log_status		= v4l2_ctrl_log_status,
1999 	.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
2000 	.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,
2001 };
2002 
2003 static const struct v4l2_file_operations radio_fops = {
2004 	.owner	  = THIS_MODULE,
2005 	.open	  = video_open,
2006 	.read     = radio_read,
2007 	.release  = video_release,
2008 	.unlocked_ioctl	= video_ioctl2,
2009 	.poll     = radio_poll,
2010 };
2011 
2012 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2013 	.vidioc_querycap	= saa7134_querycap,
2014 	.vidioc_g_tuner		= radio_g_tuner,
2015 	.vidioc_s_tuner		= radio_s_tuner,
2016 	.vidioc_g_frequency	= saa7134_g_frequency,
2017 	.vidioc_s_frequency	= saa7134_s_frequency,
2018 	.vidioc_subscribe_event	= v4l2_ctrl_subscribe_event,
2019 	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2020 };
2021 
2022 /* ----------------------------------------------------------- */
2023 /* exported stuff                                              */
2024 
2025 struct video_device saa7134_video_template = {
2026 	.name				= "saa7134-video",
2027 	.fops				= &video_fops,
2028 	.ioctl_ops 			= &video_ioctl_ops,
2029 	.tvnorms			= SAA7134_NORMS,
2030 };
2031 
2032 struct video_device saa7134_radio_template = {
2033 	.name			= "saa7134-radio",
2034 	.fops			= &radio_fops,
2035 	.ioctl_ops 		= &radio_ioctl_ops,
2036 };
2037 
2038 static const struct v4l2_ctrl_ops saa7134_ctrl_ops = {
2039 	.s_ctrl = saa7134_s_ctrl,
2040 };
2041 
2042 static const struct v4l2_ctrl_config saa7134_ctrl_invert = {
2043 	.ops = &saa7134_ctrl_ops,
2044 	.id = V4L2_CID_PRIVATE_INVERT,
2045 	.name = "Invert",
2046 	.type = V4L2_CTRL_TYPE_BOOLEAN,
2047 	.min = 0,
2048 	.max = 1,
2049 	.step = 1,
2050 };
2051 
2052 static const struct v4l2_ctrl_config saa7134_ctrl_y_odd = {
2053 	.ops = &saa7134_ctrl_ops,
2054 	.id = V4L2_CID_PRIVATE_Y_ODD,
2055 	.name = "Y Offset Odd Field",
2056 	.type = V4L2_CTRL_TYPE_INTEGER,
2057 	.min = 0,
2058 	.max = 128,
2059 	.step = 1,
2060 };
2061 
2062 static const struct v4l2_ctrl_config saa7134_ctrl_y_even = {
2063 	.ops = &saa7134_ctrl_ops,
2064 	.id = V4L2_CID_PRIVATE_Y_EVEN,
2065 	.name = "Y Offset Even Field",
2066 	.type = V4L2_CTRL_TYPE_INTEGER,
2067 	.min = 0,
2068 	.max = 128,
2069 	.step = 1,
2070 };
2071 
2072 static const struct v4l2_ctrl_config saa7134_ctrl_automute = {
2073 	.ops = &saa7134_ctrl_ops,
2074 	.id = V4L2_CID_PRIVATE_AUTOMUTE,
2075 	.name = "Automute",
2076 	.type = V4L2_CTRL_TYPE_BOOLEAN,
2077 	.min = 0,
2078 	.max = 1,
2079 	.step = 1,
2080 	.def = 1,
2081 };
2082 
2083 int saa7134_video_init1(struct saa7134_dev *dev)
2084 {
2085 	struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
2086 	struct vb2_queue *q;
2087 	int ret;
2088 
2089 	/* sanitycheck insmod options */
2090 	if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
2091 		gbuffers = 2;
2092 	if (gbufsize > gbufsize_max)
2093 		gbufsize = gbufsize_max;
2094 	gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
2095 
2096 	v4l2_ctrl_handler_init(hdl, 11);
2097 	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2098 			V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
2099 	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2100 			V4L2_CID_CONTRAST, 0, 127, 1, 68);
2101 	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2102 			V4L2_CID_SATURATION, 0, 127, 1, 64);
2103 	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2104 			V4L2_CID_HUE, -128, 127, 1, 0);
2105 	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2106 			V4L2_CID_HFLIP, 0, 1, 1, 0);
2107 	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2108 			V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
2109 	v4l2_ctrl_new_std(hdl, &saa7134_ctrl_ops,
2110 			V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0);
2111 	v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_invert, NULL);
2112 	v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_odd, NULL);
2113 	v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_y_even, NULL);
2114 	v4l2_ctrl_new_custom(hdl, &saa7134_ctrl_automute, NULL);
2115 	if (hdl->error)
2116 		return hdl->error;
2117 	if (card_has_radio(dev)) {
2118 		hdl = &dev->radio_ctrl_handler;
2119 		v4l2_ctrl_handler_init(hdl, 2);
2120 		v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler,
2121 				v4l2_ctrl_radio_filter);
2122 		if (hdl->error)
2123 			return hdl->error;
2124 	}
2125 	dev->ctl_mute       = 1;
2126 
2127 	if (dev->tda9887_conf && saa7134_ctrl_automute.def)
2128 		dev->tda9887_conf |= TDA9887_AUTOMUTE;
2129 	dev->automute       = 0;
2130 
2131 	INIT_LIST_HEAD(&dev->video_q.queue);
2132 	init_timer(&dev->video_q.timeout);
2133 	dev->video_q.timeout.function = saa7134_buffer_timeout;
2134 	dev->video_q.timeout.data     = (unsigned long)(&dev->video_q);
2135 	dev->video_q.dev              = dev;
2136 	dev->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
2137 	dev->width    = 720;
2138 	dev->height   = 576;
2139 	dev->field = V4L2_FIELD_INTERLACED;
2140 	dev->win.w.width = dev->width;
2141 	dev->win.w.height = dev->height;
2142 	dev->win.field = V4L2_FIELD_INTERLACED;
2143 	dev->ovbuf.fmt.width = dev->width;
2144 	dev->ovbuf.fmt.height = dev->height;
2145 	dev->ovbuf.fmt.pixelformat = dev->fmt->fourcc;
2146 	dev->ovbuf.fmt.colorspace = V4L2_COLORSPACE_SMPTE170M;
2147 
2148 	if (saa7134_boards[dev->board].video_out)
2149 		saa7134_videoport_init(dev);
2150 
2151 	q = &dev->video_vbq;
2152 	q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
2153 	/*
2154 	 * Do not add VB2_USERPTR unless explicitly requested: the saa7134 DMA
2155 	 * engine cannot handle transfers that do not start at the beginning
2156 	 * of a page. A user-provided pointer can start anywhere in a page, so
2157 	 * USERPTR support is a no-go unless the application knows about these
2158 	 * limitations and has special support for this.
2159 	 */
2160 	q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
2161 	if (saa7134_userptr)
2162 		q->io_modes |= VB2_USERPTR;
2163 	q->drv_priv = &dev->video_q;
2164 	q->ops = &vb2_qops;
2165 	q->gfp_flags = GFP_DMA32;
2166 	q->mem_ops = &vb2_dma_sg_memops;
2167 	q->buf_struct_size = sizeof(struct saa7134_buf);
2168 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2169 	q->lock = &dev->lock;
2170 	ret = vb2_queue_init(q);
2171 	if (ret)
2172 		return ret;
2173 	saa7134_pgtable_alloc(dev->pci, &dev->video_q.pt);
2174 
2175 	q = &dev->vbi_vbq;
2176 	q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
2177 	/* Don't add VB2_USERPTR, see comment above */
2178 	q->io_modes = VB2_MMAP | VB2_READ;
2179 	if (saa7134_userptr)
2180 		q->io_modes |= VB2_USERPTR;
2181 	q->drv_priv = &dev->vbi_q;
2182 	q->ops = &saa7134_vbi_qops;
2183 	q->gfp_flags = GFP_DMA32;
2184 	q->mem_ops = &vb2_dma_sg_memops;
2185 	q->buf_struct_size = sizeof(struct saa7134_buf);
2186 	q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
2187 	q->lock = &dev->lock;
2188 	ret = vb2_queue_init(q);
2189 	if (ret)
2190 		return ret;
2191 	saa7134_pgtable_alloc(dev->pci, &dev->vbi_q.pt);
2192 
2193 	return 0;
2194 }
2195 
2196 void saa7134_video_fini(struct saa7134_dev *dev)
2197 {
2198 	/* free stuff */
2199 	vb2_queue_release(&dev->video_vbq);
2200 	saa7134_pgtable_free(dev->pci, &dev->video_q.pt);
2201 	vb2_queue_release(&dev->vbi_vbq);
2202 	saa7134_pgtable_free(dev->pci, &dev->vbi_q.pt);
2203 	v4l2_ctrl_handler_free(&dev->ctrl_handler);
2204 	if (card_has_radio(dev))
2205 		v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
2206 }
2207 
2208 int saa7134_videoport_init(struct saa7134_dev *dev)
2209 {
2210 	/* enable video output */
2211 	int vo = saa7134_boards[dev->board].video_out;
2212 	int video_reg;
2213 	unsigned int vid_port_opts = saa7134_boards[dev->board].vid_port_opts;
2214 
2215 	/* Configure videoport */
2216 	saa_writeb(SAA7134_VIDEO_PORT_CTRL0, video_out[vo][0]);
2217 	video_reg = video_out[vo][1];
2218 	if (vid_port_opts & SET_T_CODE_POLARITY_NON_INVERTED)
2219 		video_reg &= ~VP_T_CODE_P_INVERTED;
2220 	saa_writeb(SAA7134_VIDEO_PORT_CTRL1, video_reg);
2221 	saa_writeb(SAA7134_VIDEO_PORT_CTRL2, video_out[vo][2]);
2222 	saa_writeb(SAA7134_VIDEO_PORT_CTRL4, video_out[vo][4]);
2223 	video_reg = video_out[vo][5];
2224 	if (vid_port_opts & SET_CLOCK_NOT_DELAYED)
2225 		video_reg &= ~VP_CLK_CTRL2_DELAYED;
2226 	if (vid_port_opts & SET_CLOCK_INVERTED)
2227 		video_reg |= VP_CLK_CTRL1_INVERTED;
2228 	saa_writeb(SAA7134_VIDEO_PORT_CTRL5, video_reg);
2229 	video_reg = video_out[vo][6];
2230 	if (vid_port_opts & SET_VSYNC_OFF) {
2231 		video_reg &= ~VP_VS_TYPE_MASK;
2232 		video_reg |= VP_VS_TYPE_OFF;
2233 	}
2234 	saa_writeb(SAA7134_VIDEO_PORT_CTRL6, video_reg);
2235 	saa_writeb(SAA7134_VIDEO_PORT_CTRL7, video_out[vo][7]);
2236 	saa_writeb(SAA7134_VIDEO_PORT_CTRL8, video_out[vo][8]);
2237 
2238 	/* Start videoport */
2239 	saa_writeb(SAA7134_VIDEO_PORT_CTRL3, video_out[vo][3]);
2240 
2241 	return 0;
2242 }
2243 
2244 int saa7134_video_init2(struct saa7134_dev *dev)
2245 {
2246 	/* init video hw */
2247 	set_tvnorm(dev,&tvnorms[0]);
2248 	video_mux(dev,0);
2249 	v4l2_ctrl_handler_setup(&dev->ctrl_handler);
2250 	saa7134_tvaudio_setmute(dev);
2251 	saa7134_tvaudio_setvolume(dev,dev->ctl_volume);
2252 	return 0;
2253 }
2254 
2255 void saa7134_irq_video_signalchange(struct saa7134_dev *dev)
2256 {
2257 	static const char *st[] = {
2258 		"(no signal)", "NTSC", "PAL", "SECAM" };
2259 	u32 st1,st2;
2260 
2261 	st1 = saa_readb(SAA7134_STATUS_VIDEO1);
2262 	st2 = saa_readb(SAA7134_STATUS_VIDEO2);
2263 	video_dbg("DCSDT: pll: %s, sync: %s, norm: %s\n",
2264 		(st1 & 0x40) ? "not locked" : "locked",
2265 		(st2 & 0x40) ? "no"         : "yes",
2266 		st[st1 & 0x03]);
2267 	dev->nosignal = (st1 & 0x40) || (st2 & 0x40)  || !(st2 & 0x1);
2268 
2269 	if (dev->nosignal) {
2270 		/* no video signal -> mute audio */
2271 		if (dev->ctl_automute)
2272 			dev->automute = 1;
2273 		saa7134_tvaudio_setmute(dev);
2274 	} else {
2275 		/* wake up tvaudio audio carrier scan thread */
2276 		saa7134_tvaudio_do_scan(dev);
2277 	}
2278 
2279 	if ((st2 & 0x80) && !noninterlaced && !dev->nosignal)
2280 		saa_clearb(SAA7134_SYNC_CTRL, 0x20);
2281 	else
2282 		saa_setb(SAA7134_SYNC_CTRL, 0x20);
2283 
2284 	if (dev->mops && dev->mops->signal_change)
2285 		dev->mops->signal_change(dev);
2286 }
2287 
2288 
2289 void saa7134_irq_video_done(struct saa7134_dev *dev, unsigned long status)
2290 {
2291 	enum v4l2_field field;
2292 
2293 	spin_lock(&dev->slock);
2294 	if (dev->video_q.curr) {
2295 		field = dev->field;
2296 		if (V4L2_FIELD_HAS_BOTH(field)) {
2297 			/* make sure we have seen both fields */
2298 			if ((status & 0x10) == 0x00) {
2299 				dev->video_q.curr->top_seen = 1;
2300 				goto done;
2301 			}
2302 			if (!dev->video_q.curr->top_seen)
2303 				goto done;
2304 		} else if (field == V4L2_FIELD_TOP) {
2305 			if ((status & 0x10) != 0x10)
2306 				goto done;
2307 		} else if (field == V4L2_FIELD_BOTTOM) {
2308 			if ((status & 0x10) != 0x00)
2309 				goto done;
2310 		}
2311 		saa7134_buffer_finish(dev, &dev->video_q, VB2_BUF_STATE_DONE);
2312 	}
2313 	saa7134_buffer_next(dev, &dev->video_q);
2314 
2315  done:
2316 	spin_unlock(&dev->slock);
2317 }
2318