drm/rockchip: Don't change hdmi reference clock rate
[drm/drm-misc.git] / drivers / media / pci / bt8xx / bttv-driver.c
blob2782832f5eb8f272cf9acf29d7b4c31620c99227
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
4 bttv - Bt848 frame grabber driver
6 Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
7 & Marcus Metzler <mocm@thp.uni-koeln.de>
8 (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
10 some v4l2 code lines are taken from Justin's bttv2 driver which is
11 (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
13 V4L1 removal from:
14 (c) 2005-2006 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
16 Fixes to be fully V4L2 compliant by
17 (c) 2006 Mauro Carvalho Chehab <mchehab@kernel.org>
19 Cropping and overscan support
20 Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
21 Sponsored by OPQ Systems AB
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/delay.h>
30 #include <linux/slab.h>
31 #include <linux/errno.h>
32 #include <linux/fs.h>
33 #include <linux/kernel.h>
34 #include <linux/sched.h>
35 #include <linux/interrupt.h>
36 #include <linux/kdev_t.h>
37 #include "bttvp.h"
38 #include <media/v4l2-common.h>
39 #include <media/v4l2-ioctl.h>
40 #include <media/v4l2-event.h>
41 #include <media/i2c/tvaudio.h>
42 #include <media/drv-intf/msp3400.h>
44 #include <linux/dma-mapping.h>
46 #include <asm/io.h>
47 #include <asm/byteorder.h>
49 #include <media/i2c/saa6588.h>
51 #define BTTV_VERSION "0.9.19"
53 unsigned int bttv_num; /* number of Bt848s in use */
54 struct bttv *bttvs[BTTV_MAX];
56 unsigned int bttv_debug;
57 unsigned int bttv_verbose = 1;
58 unsigned int bttv_gpio;
60 /* config variables */
61 #ifdef __BIG_ENDIAN
62 static unsigned int bigendian=1;
63 #else
64 static unsigned int bigendian;
65 #endif
66 static unsigned int radio[BTTV_MAX];
67 static unsigned int irq_debug;
68 static unsigned int gbuffers = 8;
69 static unsigned int gbufsize = 0x208000;
70 static unsigned int reset_crop = 1;
72 static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
73 static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
74 static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
75 static int debug_latency;
76 static int disable_ir;
78 static unsigned int fdsr;
80 /* options */
81 static unsigned int combfilter;
82 static unsigned int lumafilter;
83 static unsigned int automute = 1;
84 static unsigned int chroma_agc;
85 static unsigned int agc_crush = 1;
86 static unsigned int whitecrush_upper = 0xCF;
87 static unsigned int whitecrush_lower = 0x7F;
88 static unsigned int vcr_hack;
89 static unsigned int irq_iswitch;
90 static unsigned int uv_ratio = 50;
91 static unsigned int full_luma_range;
92 static unsigned int coring;
94 /* API features (turn on/off stuff for testing) */
95 static unsigned int v4l2 = 1;
97 /* insmod args */
98 module_param(bttv_verbose, int, 0644);
99 module_param(bttv_gpio, int, 0644);
100 module_param(bttv_debug, int, 0644);
101 module_param(irq_debug, int, 0644);
102 module_param(debug_latency, int, 0644);
103 module_param(disable_ir, int, 0444);
105 module_param(fdsr, int, 0444);
106 module_param(gbuffers, int, 0444);
107 module_param(gbufsize, int, 0444);
108 module_param(reset_crop, int, 0444);
110 module_param(v4l2, int, 0644);
111 module_param(bigendian, int, 0644);
112 module_param(irq_iswitch, int, 0644);
113 module_param(combfilter, int, 0444);
114 module_param(lumafilter, int, 0444);
115 module_param(automute, int, 0444);
116 module_param(chroma_agc, int, 0444);
117 module_param(agc_crush, int, 0444);
118 module_param(whitecrush_upper, int, 0444);
119 module_param(whitecrush_lower, int, 0444);
120 module_param(vcr_hack, int, 0444);
121 module_param(uv_ratio, int, 0444);
122 module_param(full_luma_range, int, 0444);
123 module_param(coring, int, 0444);
125 module_param_array(radio, int, NULL, 0444);
126 module_param_array(video_nr, int, NULL, 0444);
127 module_param_array(radio_nr, int, NULL, 0444);
128 module_param_array(vbi_nr, int, NULL, 0444);
130 MODULE_PARM_DESC(radio, "The TV card supports radio, default is 0 (no)");
131 MODULE_PARM_DESC(bigendian, "byte order of the framebuffer, default is native endian");
132 MODULE_PARM_DESC(bttv_verbose, "verbose startup messages, default is 1 (yes)");
133 MODULE_PARM_DESC(bttv_gpio, "log gpio changes, default is 0 (no)");
134 MODULE_PARM_DESC(bttv_debug, "debug messages, default is 0 (no)");
135 MODULE_PARM_DESC(irq_debug, "irq handler debug messages, default is 0 (no)");
136 MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
137 MODULE_PARM_DESC(gbuffers, "number of capture buffers. range 2-32, default 8");
138 MODULE_PARM_DESC(gbufsize, "size of the capture buffers, default is 0x208000");
139 MODULE_PARM_DESC(reset_crop, "reset cropping parameters at open(), default is 1 (yes) for compatibility with older applications");
140 MODULE_PARM_DESC(automute, "mute audio on bad/missing video signal, default is 1 (yes)");
141 MODULE_PARM_DESC(chroma_agc, "enables the AGC of chroma signal, default is 0 (no)");
142 MODULE_PARM_DESC(agc_crush, "enables the luminance AGC crush, default is 1 (yes)");
143 MODULE_PARM_DESC(whitecrush_upper, "sets the white crush upper value, default is 207");
144 MODULE_PARM_DESC(whitecrush_lower, "sets the white crush lower value, default is 127");
145 MODULE_PARM_DESC(vcr_hack, "enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
146 MODULE_PARM_DESC(irq_iswitch, "switch inputs in irq handler");
147 MODULE_PARM_DESC(uv_ratio, "ratio between u and v gains, default is 50");
148 MODULE_PARM_DESC(full_luma_range, "use the full luma range, default is 0 (no)");
149 MODULE_PARM_DESC(coring, "set the luma coring level, default is 0 (no)");
150 MODULE_PARM_DESC(video_nr, "video device numbers");
151 MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
152 MODULE_PARM_DESC(radio_nr, "radio device numbers");
154 MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
155 MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
156 MODULE_LICENSE("GPL");
157 MODULE_VERSION(BTTV_VERSION);
159 #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_USER_BTTV_BASE + 0)
160 #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_USER_BTTV_BASE + 1)
161 #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_USER_BTTV_BASE + 2)
162 #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_USER_BTTV_BASE + 3)
163 #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_USER_BTTV_BASE + 4)
164 #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_USER_BTTV_BASE + 5)
165 #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_USER_BTTV_BASE + 6)
166 #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_USER_BTTV_BASE + 7)
167 #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_USER_BTTV_BASE + 8)
168 #define V4L2_CID_PRIVATE_CORING (V4L2_CID_USER_BTTV_BASE + 9)
170 /* ----------------------------------------------------------------------- */
171 /* sysfs */
173 static ssize_t card_show(struct device *cd,
174 struct device_attribute *attr, char *buf)
176 struct video_device *vfd = to_video_device(cd);
177 struct bttv *btv = video_get_drvdata(vfd);
178 return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
180 static DEVICE_ATTR_RO(card);
182 /* ----------------------------------------------------------------------- */
183 /* dvb auto-load setup */
184 #if defined(CONFIG_MODULES) && defined(MODULE)
185 static void request_module_async(struct work_struct *work)
187 request_module("dvb-bt8xx");
190 static void request_modules(struct bttv *dev)
192 INIT_WORK(&dev->request_module_wk, request_module_async);
193 schedule_work(&dev->request_module_wk);
196 static void flush_request_modules(struct bttv *dev)
198 flush_work(&dev->request_module_wk);
200 #else
201 #define request_modules(dev)
202 #define flush_request_modules(dev) do {} while(0)
203 #endif /* CONFIG_MODULES */
206 /* ----------------------------------------------------------------------- */
207 /* static data */
209 /* special timing tables from conexant... */
210 static u8 SRAM_Table[][60] =
212 /* PAL digital input over GPIO[7:0] */
214 45, // 45 bytes following
215 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
216 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
217 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
218 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
219 0x37,0x00,0xAF,0x21,0x00
221 /* NTSC digital input over GPIO[7:0] */
223 51, // 51 bytes following
224 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
225 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
226 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
227 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
228 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
229 0x00,
231 // TGB_NTSC392 // quartzsight
232 // This table has been modified to be used for Fusion Rev D
234 0x2A, // size of table = 42
235 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
236 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
237 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
238 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
239 0x20, 0x00
243 /* minhdelayx1 first video pixel we can capture on a line and
244 hdelayx1 start of active video, both relative to rising edge of
245 /HRESET pulse (0H) in 1 / fCLKx1.
246 swidth width of active video and
247 totalwidth total line width, both in 1 / fCLKx1.
248 sqwidth total line width in square pixels.
249 vdelay start of active video in 2 * field lines relative to
250 trailing edge of /VRESET pulse (VDELAY register).
251 sheight height of active video in 2 * field lines.
252 extraheight Added to sheight for cropcap.bounds.height only
253 videostart0 ITU-R frame line number of the line corresponding
254 to vdelay in the first field. */
255 #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
256 vdelay, sheight, extraheight, videostart0) \
257 .cropcap.bounds.left = minhdelayx1, \
258 /* * 2 because vertically we count field lines times two, */ \
259 /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
260 .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
261 /* 4 is a safety margin at the end of the line. */ \
262 .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
263 .cropcap.bounds.height = (sheight) + (extraheight) + (vdelay) - \
264 MIN_VDELAY, \
265 .cropcap.defrect.left = hdelayx1, \
266 .cropcap.defrect.top = (videostart0) * 2, \
267 .cropcap.defrect.width = swidth, \
268 .cropcap.defrect.height = sheight, \
269 .cropcap.pixelaspect.numerator = totalwidth, \
270 .cropcap.pixelaspect.denominator = sqwidth,
272 const struct bttv_tvnorm bttv_tvnorms[] = {
273 /* PAL-BDGHI */
274 /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
275 /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
277 .v4l2_id = V4L2_STD_PAL,
278 .name = "PAL",
279 .Fsc = 35468950,
280 .swidth = 924,
281 .sheight = 576,
282 .totalwidth = 1135,
283 .adelay = 0x7f,
284 .bdelay = 0x72,
285 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
286 .scaledtwidth = 1135,
287 .hdelayx1 = 186,
288 .hactivex1 = 924,
289 .vdelay = 0x20,
290 .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
291 .sram = 0,
292 /* ITU-R frame line number of the first VBI line
293 we can capture, of the first and second field.
294 The last line is determined by cropcap.bounds. */
295 .vbistart = { 7, 320 },
296 CROPCAP(/* minhdelayx1 */ 68,
297 /* hdelayx1 */ 186,
298 /* Should be (768 * 1135 + 944 / 2) / 944.
299 cropcap.defrect is used for image width
300 checks, so we keep the old value 924. */
301 /* swidth */ 924,
302 /* totalwidth */ 1135,
303 /* sqwidth */ 944,
304 /* vdelay */ 0x20,
305 /* sheight */ 576,
306 /* bt878 (and bt848?) can capture another
307 line below active video. */
308 /* extraheight */ 2,
309 /* videostart0 */ 23)
311 .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
312 .name = "NTSC",
313 .Fsc = 28636363,
314 .swidth = 768,
315 .sheight = 480,
316 .totalwidth = 910,
317 .adelay = 0x68,
318 .bdelay = 0x5d,
319 .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
320 .scaledtwidth = 910,
321 .hdelayx1 = 128,
322 .hactivex1 = 910,
323 .vdelay = 0x1a,
324 .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
325 .sram = 1,
326 .vbistart = { 10, 273 },
327 CROPCAP(/* minhdelayx1 */ 68,
328 /* hdelayx1 */ 128,
329 /* Should be (640 * 910 + 780 / 2) / 780? */
330 /* swidth */ 768,
331 /* totalwidth */ 910,
332 /* sqwidth */ 780,
333 /* vdelay */ 0x1a,
334 /* sheight */ 480,
335 /* extraheight */ 0,
336 /* videostart0 */ 23)
338 .v4l2_id = V4L2_STD_SECAM,
339 .name = "SECAM",
340 .Fsc = 35468950,
341 .swidth = 924,
342 .sheight = 576,
343 .totalwidth = 1135,
344 .adelay = 0x7f,
345 .bdelay = 0xb0,
346 .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
347 .scaledtwidth = 1135,
348 .hdelayx1 = 186,
349 .hactivex1 = 922,
350 .vdelay = 0x20,
351 .vbipack = 255,
352 .sram = 0, /* like PAL, correct? */
353 .vbistart = { 7, 320 },
354 CROPCAP(/* minhdelayx1 */ 68,
355 /* hdelayx1 */ 186,
356 /* swidth */ 924,
357 /* totalwidth */ 1135,
358 /* sqwidth */ 944,
359 /* vdelay */ 0x20,
360 /* sheight */ 576,
361 /* extraheight */ 0,
362 /* videostart0 */ 23)
364 .v4l2_id = V4L2_STD_PAL_Nc,
365 .name = "PAL-Nc",
366 .Fsc = 28636363,
367 .swidth = 640,
368 .sheight = 576,
369 .totalwidth = 910,
370 .adelay = 0x68,
371 .bdelay = 0x5d,
372 .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
373 .scaledtwidth = 780,
374 .hdelayx1 = 130,
375 .hactivex1 = 734,
376 .vdelay = 0x1a,
377 .vbipack = 144,
378 .sram = -1,
379 .vbistart = { 7, 320 },
380 CROPCAP(/* minhdelayx1 */ 68,
381 /* hdelayx1 */ 130,
382 /* swidth */ (640 * 910 + 780 / 2) / 780,
383 /* totalwidth */ 910,
384 /* sqwidth */ 780,
385 /* vdelay */ 0x1a,
386 /* sheight */ 576,
387 /* extraheight */ 0,
388 /* videostart0 */ 23)
390 .v4l2_id = V4L2_STD_PAL_M,
391 .name = "PAL-M",
392 .Fsc = 28636363,
393 .swidth = 640,
394 .sheight = 480,
395 .totalwidth = 910,
396 .adelay = 0x68,
397 .bdelay = 0x5d,
398 .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
399 .scaledtwidth = 780,
400 .hdelayx1 = 135,
401 .hactivex1 = 754,
402 .vdelay = 0x1a,
403 .vbipack = 144,
404 .sram = -1,
405 .vbistart = { 10, 273 },
406 CROPCAP(/* minhdelayx1 */ 68,
407 /* hdelayx1 */ 135,
408 /* swidth */ (640 * 910 + 780 / 2) / 780,
409 /* totalwidth */ 910,
410 /* sqwidth */ 780,
411 /* vdelay */ 0x1a,
412 /* sheight */ 480,
413 /* extraheight */ 0,
414 /* videostart0 */ 23)
416 .v4l2_id = V4L2_STD_PAL_N,
417 .name = "PAL-N",
418 .Fsc = 35468950,
419 .swidth = 768,
420 .sheight = 576,
421 .totalwidth = 1135,
422 .adelay = 0x7f,
423 .bdelay = 0x72,
424 .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
425 .scaledtwidth = 944,
426 .hdelayx1 = 186,
427 .hactivex1 = 922,
428 .vdelay = 0x20,
429 .vbipack = 144,
430 .sram = -1,
431 .vbistart = { 7, 320 },
432 CROPCAP(/* minhdelayx1 */ 68,
433 /* hdelayx1 */ 186,
434 /* swidth */ (768 * 1135 + 944 / 2) / 944,
435 /* totalwidth */ 1135,
436 /* sqwidth */ 944,
437 /* vdelay */ 0x20,
438 /* sheight */ 576,
439 /* extraheight */ 0,
440 /* videostart0 */ 23)
442 .v4l2_id = V4L2_STD_NTSC_M_JP,
443 .name = "NTSC-JP",
444 .Fsc = 28636363,
445 .swidth = 640,
446 .sheight = 480,
447 .totalwidth = 910,
448 .adelay = 0x68,
449 .bdelay = 0x5d,
450 .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
451 .scaledtwidth = 780,
452 .hdelayx1 = 135,
453 .hactivex1 = 754,
454 .vdelay = 0x16,
455 .vbipack = 144,
456 .sram = -1,
457 .vbistart = { 10, 273 },
458 CROPCAP(/* minhdelayx1 */ 68,
459 /* hdelayx1 */ 135,
460 /* swidth */ (640 * 910 + 780 / 2) / 780,
461 /* totalwidth */ 910,
462 /* sqwidth */ 780,
463 /* vdelay */ 0x16,
464 /* sheight */ 480,
465 /* extraheight */ 0,
466 /* videostart0 */ 23)
468 /* that one hopefully works with the strange timing
469 * which video recorders produce when playing a NTSC
470 * tape on a PAL TV ... */
471 .v4l2_id = V4L2_STD_PAL_60,
472 .name = "PAL-60",
473 .Fsc = 35468950,
474 .swidth = 924,
475 .sheight = 480,
476 .totalwidth = 1135,
477 .adelay = 0x7f,
478 .bdelay = 0x72,
479 .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
480 .scaledtwidth = 1135,
481 .hdelayx1 = 186,
482 .hactivex1 = 924,
483 .vdelay = 0x1a,
484 .vbipack = 255,
485 .vtotal = 524,
486 .sram = -1,
487 .vbistart = { 10, 273 },
488 CROPCAP(/* minhdelayx1 */ 68,
489 /* hdelayx1 */ 186,
490 /* swidth */ 924,
491 /* totalwidth */ 1135,
492 /* sqwidth */ 944,
493 /* vdelay */ 0x1a,
494 /* sheight */ 480,
495 /* extraheight */ 0,
496 /* videostart0 */ 23)
499 static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
501 /* ----------------------------------------------------------------------- */
502 /* bttv format list
503 packed pixel formats must come first */
504 static const struct bttv_format formats[] = {
506 .fourcc = V4L2_PIX_FMT_GREY,
507 .btformat = BT848_COLOR_FMT_Y8,
508 .depth = 8,
509 .flags = FORMAT_FLAGS_PACKED,
511 .fourcc = V4L2_PIX_FMT_HI240,
512 .btformat = BT848_COLOR_FMT_RGB8,
513 .depth = 8,
514 .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
516 .fourcc = V4L2_PIX_FMT_RGB555,
517 .btformat = BT848_COLOR_FMT_RGB15,
518 .depth = 16,
519 .flags = FORMAT_FLAGS_PACKED,
521 .fourcc = V4L2_PIX_FMT_RGB555X,
522 .btformat = BT848_COLOR_FMT_RGB15,
523 .btswap = 0x03, /* byteswap */
524 .depth = 16,
525 .flags = FORMAT_FLAGS_PACKED,
527 .fourcc = V4L2_PIX_FMT_RGB565,
528 .btformat = BT848_COLOR_FMT_RGB16,
529 .depth = 16,
530 .flags = FORMAT_FLAGS_PACKED,
532 .fourcc = V4L2_PIX_FMT_RGB565X,
533 .btformat = BT848_COLOR_FMT_RGB16,
534 .btswap = 0x03, /* byteswap */
535 .depth = 16,
536 .flags = FORMAT_FLAGS_PACKED,
538 .fourcc = V4L2_PIX_FMT_BGR24,
539 .btformat = BT848_COLOR_FMT_RGB24,
540 .depth = 24,
541 .flags = FORMAT_FLAGS_PACKED,
543 .fourcc = V4L2_PIX_FMT_BGR32,
544 .btformat = BT848_COLOR_FMT_RGB32,
545 .depth = 32,
546 .flags = FORMAT_FLAGS_PACKED,
548 .fourcc = V4L2_PIX_FMT_RGB32,
549 .btformat = BT848_COLOR_FMT_RGB32,
550 .btswap = 0x0f, /* byte+word swap */
551 .depth = 32,
552 .flags = FORMAT_FLAGS_PACKED,
554 .fourcc = V4L2_PIX_FMT_YUYV,
555 .btformat = BT848_COLOR_FMT_YUY2,
556 .depth = 16,
557 .flags = FORMAT_FLAGS_PACKED,
559 .fourcc = V4L2_PIX_FMT_UYVY,
560 .btformat = BT848_COLOR_FMT_YUY2,
561 .btswap = 0x03, /* byteswap */
562 .depth = 16,
563 .flags = FORMAT_FLAGS_PACKED,
565 .fourcc = V4L2_PIX_FMT_YUV422P,
566 .btformat = BT848_COLOR_FMT_YCrCb422,
567 .depth = 16,
568 .flags = FORMAT_FLAGS_PLANAR,
569 .hshift = 1,
570 .vshift = 0,
572 .fourcc = V4L2_PIX_FMT_YUV420,
573 .btformat = BT848_COLOR_FMT_YCrCb422,
574 .depth = 12,
575 .flags = FORMAT_FLAGS_PLANAR,
576 .hshift = 1,
577 .vshift = 1,
579 .fourcc = V4L2_PIX_FMT_YVU420,
580 .btformat = BT848_COLOR_FMT_YCrCb422,
581 .depth = 12,
582 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
583 .hshift = 1,
584 .vshift = 1,
586 .fourcc = V4L2_PIX_FMT_YUV411P,
587 .btformat = BT848_COLOR_FMT_YCrCb411,
588 .depth = 12,
589 .flags = FORMAT_FLAGS_PLANAR,
590 .hshift = 2,
591 .vshift = 0,
593 .fourcc = V4L2_PIX_FMT_YUV410,
594 .btformat = BT848_COLOR_FMT_YCrCb411,
595 .depth = 9,
596 .flags = FORMAT_FLAGS_PLANAR,
597 .hshift = 2,
598 .vshift = 2,
600 .fourcc = V4L2_PIX_FMT_YVU410,
601 .btformat = BT848_COLOR_FMT_YCrCb411,
602 .depth = 9,
603 .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
604 .hshift = 2,
605 .vshift = 2,
607 .fourcc = -1,
608 .btformat = BT848_COLOR_FMT_RAW,
609 .depth = 8,
610 .flags = FORMAT_FLAGS_RAW,
613 static const unsigned int FORMATS = ARRAY_SIZE(formats);
615 /* ----------------------------------------------------------------------- */
616 /* resource management */
619 RESOURCE_ allocated by freed by
621 VIDEO_READ bttv_read 1) bttv_read 2)
623 VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
624 VIDIOC_QBUF 1) bttv_release
625 VIDIOCMCAPTURE 1)
627 VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
628 VIDIOC_QBUF 1) bttv_release
629 bttv_read, bttv_poll 1) 3)
631 1) The resource must be allocated when we enter buffer prepare functions
632 and remain allocated while buffers are in the DMA queue.
633 2) This is a single frame read.
634 3) This is a continuous read, implies VIDIOC_STREAMON.
636 Note this driver permits video input and standard changes regardless if
637 resources are allocated.
640 #define VBI_RESOURCES (RESOURCE_VBI)
641 #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
642 RESOURCE_VIDEO_STREAM)
644 int check_alloc_btres_lock(struct bttv *btv, int bit)
646 int xbits; /* mutual exclusive resources */
648 xbits = bit;
649 if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
650 xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
652 /* is it free? */
653 if (btv->resources & xbits) {
654 /* no, someone else uses it */
655 goto fail;
658 if ((bit & VIDEO_RESOURCES)
659 && 0 == (btv->resources & VIDEO_RESOURCES)) {
660 /* Do crop - use current, don't - use default parameters. */
661 __s32 top = btv->crop[!!btv->do_crop].rect.top;
663 if (btv->vbi_end > top)
664 goto fail;
666 /* We cannot capture the same line as video and VBI data.
667 Claim scan lines crop[].rect.top to bottom. */
668 btv->crop_start = top;
669 } else if (bit & VBI_RESOURCES) {
670 __s32 end = btv->vbi_fmt.end;
672 if (end > btv->crop_start)
673 goto fail;
675 /* Claim scan lines above btv->vbi_fmt.end. */
676 btv->vbi_end = end;
679 /* it's free, grab it */
680 btv->resources |= bit;
681 return 1;
683 fail:
684 return 0;
687 static
688 int check_btres(struct bttv *btv, int bit)
690 return (btv->resources & bit);
693 static
694 int locked_btres(struct bttv *btv, int bit)
696 return (btv->resources & bit);
699 /* Call with btv->lock down. */
700 static void
701 disclaim_vbi_lines(struct bttv *btv)
703 btv->vbi_end = 0;
706 /* Call with btv->lock down. */
707 static void
708 disclaim_video_lines(struct bttv *btv)
710 const struct bttv_tvnorm *tvnorm;
711 u8 crop;
713 tvnorm = &bttv_tvnorms[btv->tvnorm];
714 btv->crop_start = tvnorm->cropcap.bounds.top
715 + tvnorm->cropcap.bounds.height;
717 /* VBI capturing ends at VDELAY, start of video capturing, no
718 matter how many lines the VBI RISC program expects. When video
719 capturing is off, it shall no longer "preempt" VBI capturing,
720 so we set VDELAY to maximum. */
721 crop = btread(BT848_E_CROP) | 0xc0;
722 btwrite(crop, BT848_E_CROP);
723 btwrite(0xfe, BT848_E_VDELAY_LO);
724 btwrite(crop, BT848_O_CROP);
725 btwrite(0xfe, BT848_O_VDELAY_LO);
728 void free_btres_lock(struct bttv *btv, int bits)
730 if ((btv->resources & bits) != bits) {
731 /* trying to free resources not allocated by us ... */
732 pr_err("BUG! (btres)\n");
734 btv->resources &= ~bits;
736 bits = btv->resources;
738 if (0 == (bits & VIDEO_RESOURCES))
739 disclaim_video_lines(btv);
741 if (0 == (bits & VBI_RESOURCES))
742 disclaim_vbi_lines(btv);
745 /* ----------------------------------------------------------------------- */
746 /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
748 /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
749 PLL_X = Reference pre-divider (0=1, 1=2)
750 PLL_C = Post divider (0=6, 1=4)
751 PLL_I = Integer input
752 PLL_F = Fractional input
754 F_input = 28.636363 MHz:
755 PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
758 static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
760 unsigned char fl, fh, fi;
762 /* prevent overflows */
763 fin/=4;
764 fout/=4;
766 fout*=12;
767 fi=fout/fin;
769 fout=(fout%fin)*256;
770 fh=fout/fin;
772 fout=(fout%fin)*256;
773 fl=fout/fin;
775 btwrite(fl, BT848_PLL_F_LO);
776 btwrite(fh, BT848_PLL_F_HI);
777 btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
780 static void set_pll(struct bttv *btv)
782 int i;
784 if (!btv->pll.pll_crystal)
785 return;
787 if (btv->pll.pll_ofreq == btv->pll.pll_current) {
788 dprintk("%d: PLL: no change required\n", btv->c.nr);
789 return;
792 if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
793 /* no PLL needed */
794 if (btv->pll.pll_current == 0)
795 return;
796 if (bttv_verbose)
797 pr_info("%d: PLL can sleep, using XTAL (%d)\n",
798 btv->c.nr, btv->pll.pll_ifreq);
799 btwrite(0x00,BT848_TGCTRL);
800 btwrite(0x00,BT848_PLL_XCI);
801 btv->pll.pll_current = 0;
802 return;
805 if (bttv_verbose)
806 pr_info("%d: Setting PLL: %d => %d (needs up to 100ms)\n",
807 btv->c.nr,
808 btv->pll.pll_ifreq, btv->pll.pll_ofreq);
809 set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
811 for (i=0; i<10; i++) {
812 /* Let other people run while the PLL stabilizes */
813 msleep(10);
815 if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
816 btwrite(0,BT848_DSTATUS);
817 } else {
818 btwrite(0x08,BT848_TGCTRL);
819 btv->pll.pll_current = btv->pll.pll_ofreq;
820 if (bttv_verbose)
821 pr_info("PLL set ok\n");
822 return;
825 btv->pll.pll_current = -1;
826 if (bttv_verbose)
827 pr_info("Setting PLL failed\n");
828 return;
831 /* used to switch between the bt848's analog/digital video capture modes */
832 static void bt848A_set_timing(struct bttv *btv)
834 int i, len;
835 int table_idx = bttv_tvnorms[btv->tvnorm].sram;
836 int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
838 if (btv->input == btv->dig) {
839 dprintk("%d: load digital timing table (table_idx=%d)\n",
840 btv->c.nr,table_idx);
842 /* timing change...reset timing generator address */
843 btwrite(0x00, BT848_TGCTRL);
844 btwrite(0x02, BT848_TGCTRL);
845 btwrite(0x00, BT848_TGCTRL);
847 len=SRAM_Table[table_idx][0];
848 for(i = 1; i <= len; i++)
849 btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
850 btv->pll.pll_ofreq = 27000000;
852 set_pll(btv);
853 btwrite(0x11, BT848_TGCTRL);
854 btwrite(0x41, BT848_DVSIF);
855 } else {
856 btv->pll.pll_ofreq = fsc;
857 set_pll(btv);
858 btwrite(0x0, BT848_DVSIF);
862 /* ----------------------------------------------------------------------- */
864 static void bt848_bright(struct bttv *btv, int bright)
866 int value;
868 // printk("set bright: %d\n", bright); // DEBUG
869 btv->bright = bright;
871 /* We want -128 to 127 we get 0-65535 */
872 value = (bright >> 8) - 128;
873 btwrite(value & 0xff, BT848_BRIGHT);
876 static void bt848_hue(struct bttv *btv, int hue)
878 int value;
880 btv->hue = hue;
882 /* -128 to 127 */
883 value = (hue >> 8) - 128;
884 btwrite(value & 0xff, BT848_HUE);
887 static void bt848_contrast(struct bttv *btv, int cont)
889 int value,hibit;
891 btv->contrast = cont;
893 /* 0-511 */
894 value = (cont >> 7);
895 hibit = (value >> 6) & 4;
896 btwrite(value & 0xff, BT848_CONTRAST_LO);
897 btaor(hibit, ~4, BT848_E_CONTROL);
898 btaor(hibit, ~4, BT848_O_CONTROL);
901 static void bt848_sat(struct bttv *btv, int color)
903 int val_u,val_v,hibits;
905 btv->saturation = color;
907 /* 0-511 for the color */
908 val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
909 val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
910 hibits = (val_u >> 7) & 2;
911 hibits |= (val_v >> 8) & 1;
912 btwrite(val_u & 0xff, BT848_SAT_U_LO);
913 btwrite(val_v & 0xff, BT848_SAT_V_LO);
914 btaor(hibits, ~3, BT848_E_CONTROL);
915 btaor(hibits, ~3, BT848_O_CONTROL);
918 /* ----------------------------------------------------------------------- */
920 static int
921 video_mux(struct bttv *btv, unsigned int input)
923 int mux,mask2;
925 if (input >= bttv_tvcards[btv->c.type].video_inputs)
926 return -EINVAL;
928 /* needed by RemoteVideo MX */
929 mask2 = bttv_tvcards[btv->c.type].gpiomask2;
930 if (mask2)
931 gpio_inout(mask2,mask2);
933 if (input == btv->svhs) {
934 btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
935 btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
936 } else {
937 btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
938 btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
940 mux = bttv_muxsel(btv, input);
941 btaor(mux<<5, ~(3<<5), BT848_IFORM);
942 dprintk("%d: video mux: input=%d mux=%d\n", btv->c.nr, input, mux);
944 /* card specific hook */
945 if(bttv_tvcards[btv->c.type].muxsel_hook)
946 bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
947 return 0;
950 static char *audio_modes[] = {
951 "audio: tuner", "audio: radio", "audio: extern",
952 "audio: intern", "audio: mute"
955 static void
956 audio_mux_gpio(struct bttv *btv, int input, int mute)
958 int gpio_val, signal, mute_gpio;
960 gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
961 bttv_tvcards[btv->c.type].gpiomask);
962 signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
964 /* automute */
965 mute_gpio = mute || (btv->opt_automute && (!signal || !btv->users)
966 && !btv->has_radio_tuner);
968 if (mute_gpio)
969 gpio_val = bttv_tvcards[btv->c.type].gpiomute;
970 else
971 gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
973 switch (btv->c.type) {
974 case BTTV_BOARD_VOODOOTV_FM:
975 case BTTV_BOARD_VOODOOTV_200:
976 gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
977 break;
979 default:
980 gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
983 if (bttv_gpio)
984 bttv_gpio_tracking(btv, audio_modes[mute_gpio ? 4 : input]);
987 static int
988 audio_mute(struct bttv *btv, int mute)
990 struct v4l2_ctrl *ctrl;
992 audio_mux_gpio(btv, btv->audio_input, mute);
994 if (btv->sd_msp34xx) {
995 ctrl = v4l2_ctrl_find(btv->sd_msp34xx->ctrl_handler, V4L2_CID_AUDIO_MUTE);
996 if (ctrl)
997 v4l2_ctrl_s_ctrl(ctrl, mute);
999 if (btv->sd_tvaudio) {
1000 ctrl = v4l2_ctrl_find(btv->sd_tvaudio->ctrl_handler, V4L2_CID_AUDIO_MUTE);
1001 if (ctrl)
1002 v4l2_ctrl_s_ctrl(ctrl, mute);
1004 if (btv->sd_tda7432) {
1005 ctrl = v4l2_ctrl_find(btv->sd_tda7432->ctrl_handler, V4L2_CID_AUDIO_MUTE);
1006 if (ctrl)
1007 v4l2_ctrl_s_ctrl(ctrl, mute);
1009 return 0;
1012 static int
1013 audio_input(struct bttv *btv, int input)
1015 audio_mux_gpio(btv, input, btv->mute);
1017 if (btv->sd_msp34xx) {
1018 u32 in;
1020 /* Note: the inputs tuner/radio/extern/intern are translated
1021 to msp routings. This assumes common behavior for all msp3400
1022 based TV cards. When this assumption fails, then the
1023 specific MSP routing must be added to the card table.
1024 For now this is sufficient. */
1025 switch (input) {
1026 case TVAUDIO_INPUT_RADIO:
1027 /* Some boards need the msp do to the radio demod */
1028 if (btv->radio_uses_msp_demodulator) {
1029 in = MSP_INPUT_DEFAULT;
1030 break;
1032 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1033 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1034 break;
1035 case TVAUDIO_INPUT_EXTERN:
1036 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
1037 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1038 break;
1039 case TVAUDIO_INPUT_INTERN:
1040 /* Yes, this is the same input as for RADIO. I doubt
1041 if this is ever used. The only board with an INTERN
1042 input is the BTTV_BOARD_AVERMEDIA98. I wonder how
1043 that was tested. My guess is that the whole INTERN
1044 input does not work. */
1045 in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
1046 MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
1047 break;
1048 case TVAUDIO_INPUT_TUNER:
1049 default:
1050 /* This is the only card that uses TUNER2, and afaik,
1051 is the only difference between the VOODOOTV_FM
1052 and VOODOOTV_200 */
1053 if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
1054 in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
1055 MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
1056 else
1057 in = MSP_INPUT_DEFAULT;
1058 break;
1060 v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
1061 in, MSP_OUTPUT_DEFAULT, 0);
1063 if (btv->sd_tvaudio) {
1064 v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
1065 input, 0, 0);
1067 return 0;
1070 static void
1071 bttv_crop_calc_limits(struct bttv_crop *c)
1073 /* Scale factor min. 1:1, max. 16:1. Min. image size
1074 48 x 32. Scaled width must be a multiple of 4. */
1076 if (1) {
1077 /* For bug compatibility with VIDIOCGCAP and image
1078 size checks in earlier driver versions. */
1079 c->min_scaled_width = 48;
1080 c->min_scaled_height = 32;
1081 } else {
1082 c->min_scaled_width =
1083 (max_t(unsigned int, 48, c->rect.width >> 4) + 3) & ~3;
1084 c->min_scaled_height =
1085 max_t(unsigned int, 32, c->rect.height >> 4);
1088 c->max_scaled_width = c->rect.width & ~3;
1089 c->max_scaled_height = c->rect.height;
1092 static void
1093 bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
1095 c->rect = bttv_tvnorms[norm].cropcap.defrect;
1096 bttv_crop_calc_limits(c);
1099 /* Call with btv->lock down. */
1100 static int
1101 set_tvnorm(struct bttv *btv, unsigned int norm)
1103 const struct bttv_tvnorm *tvnorm;
1104 v4l2_std_id id;
1106 WARN_ON(norm >= BTTV_TVNORMS);
1107 WARN_ON(btv->tvnorm >= BTTV_TVNORMS);
1109 tvnorm = &bttv_tvnorms[norm];
1111 if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
1112 sizeof (tvnorm->cropcap))) {
1113 bttv_crop_reset(&btv->crop[0], norm);
1114 btv->crop[1] = btv->crop[0]; /* current = default */
1116 if (0 == (btv->resources & VIDEO_RESOURCES)) {
1117 btv->crop_start = tvnorm->cropcap.bounds.top
1118 + tvnorm->cropcap.bounds.height;
1122 btv->tvnorm = norm;
1124 btwrite(tvnorm->adelay, BT848_ADELAY);
1125 btwrite(tvnorm->bdelay, BT848_BDELAY);
1126 btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
1127 BT848_IFORM);
1128 btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
1129 btwrite(1, BT848_VBI_PACK_DEL);
1130 bt848A_set_timing(btv);
1132 switch (btv->c.type) {
1133 case BTTV_BOARD_VOODOOTV_FM:
1134 case BTTV_BOARD_VOODOOTV_200:
1135 bttv_tda9880_setnorm(btv, gpio_read());
1136 break;
1138 id = tvnorm->v4l2_id;
1139 bttv_call_all(btv, video, s_std, id);
1141 return 0;
1144 /* Call with btv->lock down. */
1145 static void
1146 set_input(struct bttv *btv, unsigned int input, unsigned int norm)
1148 unsigned long flags;
1150 btv->input = input;
1151 if (irq_iswitch) {
1152 spin_lock_irqsave(&btv->s_lock,flags);
1153 if (btv->curr.frame_irq) {
1154 /* active capture -> delayed input switch */
1155 btv->new_input = input;
1156 } else {
1157 video_mux(btv,input);
1159 spin_unlock_irqrestore(&btv->s_lock,flags);
1160 } else {
1161 video_mux(btv,input);
1163 btv->audio_input = (btv->tuner_type != TUNER_ABSENT && input == 0) ?
1164 TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN;
1165 audio_input(btv, btv->audio_input);
1166 set_tvnorm(btv, norm);
1169 void init_irqreg(struct bttv *btv)
1171 /* clear status */
1172 btwrite(0xfffffUL, BT848_INT_STAT);
1174 if (bttv_tvcards[btv->c.type].no_video) {
1175 /* i2c only */
1176 btwrite(BT848_INT_I2CDONE,
1177 BT848_INT_MASK);
1178 } else {
1179 /* full video */
1180 btwrite((btv->triton1) |
1181 (btv->gpioirq ? BT848_INT_GPINT : 0) |
1182 BT848_INT_SCERR |
1183 (fdsr ? BT848_INT_FDSR : 0) |
1184 BT848_INT_RISCI | BT848_INT_OCERR |
1185 BT848_INT_FMTCHG|BT848_INT_HLOCK|
1186 BT848_INT_I2CDONE,
1187 BT848_INT_MASK);
1191 static void init_bt848(struct bttv *btv)
1193 if (bttv_tvcards[btv->c.type].no_video) {
1194 /* very basic init only */
1195 init_irqreg(btv);
1196 return;
1199 btwrite(0x00, BT848_CAP_CTL);
1200 btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
1201 btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
1203 /* set planar and packed mode trigger points and */
1204 /* set rising edge of inverted GPINTR pin as irq trigger */
1205 btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
1206 BT848_GPIO_DMA_CTL_PLTP1_16|
1207 BT848_GPIO_DMA_CTL_PLTP23_16|
1208 BT848_GPIO_DMA_CTL_GPINTC|
1209 BT848_GPIO_DMA_CTL_GPINTI,
1210 BT848_GPIO_DMA_CTL);
1212 btwrite(0x20, BT848_E_VSCALE_HI);
1213 btwrite(0x20, BT848_O_VSCALE_HI);
1215 v4l2_ctrl_handler_setup(&btv->ctrl_handler);
1217 /* interrupt */
1218 init_irqreg(btv);
1221 static void bttv_reinit_bt848(struct bttv *btv)
1223 unsigned long flags;
1225 if (bttv_verbose)
1226 pr_info("%d: reset, reinitialize\n", btv->c.nr);
1227 spin_lock_irqsave(&btv->s_lock,flags);
1228 btv->errors=0;
1229 bttv_set_dma(btv,0);
1230 spin_unlock_irqrestore(&btv->s_lock,flags);
1232 init_bt848(btv);
1233 btv->pll.pll_current = -1;
1234 set_input(btv, btv->input, btv->tvnorm);
1237 static int bttv_s_ctrl(struct v4l2_ctrl *c)
1239 struct bttv *btv = container_of(c->handler, struct bttv, ctrl_handler);
1240 int val;
1242 switch (c->id) {
1243 case V4L2_CID_BRIGHTNESS:
1244 bt848_bright(btv, c->val);
1245 break;
1246 case V4L2_CID_HUE:
1247 bt848_hue(btv, c->val);
1248 break;
1249 case V4L2_CID_CONTRAST:
1250 bt848_contrast(btv, c->val);
1251 break;
1252 case V4L2_CID_SATURATION:
1253 bt848_sat(btv, c->val);
1254 break;
1255 case V4L2_CID_COLOR_KILLER:
1256 if (c->val) {
1257 btor(BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1258 btor(BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1259 } else {
1260 btand(~BT848_SCLOOP_CKILL, BT848_E_SCLOOP);
1261 btand(~BT848_SCLOOP_CKILL, BT848_O_SCLOOP);
1263 break;
1264 case V4L2_CID_AUDIO_MUTE:
1265 audio_mute(btv, c->val);
1266 btv->mute = c->val;
1267 break;
1268 case V4L2_CID_AUDIO_VOLUME:
1269 btv->volume_gpio(btv, c->val);
1270 break;
1272 case V4L2_CID_CHROMA_AGC:
1273 val = c->val ? BT848_SCLOOP_CAGC : 0;
1274 btwrite(val, BT848_E_SCLOOP);
1275 btwrite(val, BT848_O_SCLOOP);
1276 break;
1277 case V4L2_CID_PRIVATE_COMBFILTER:
1278 btv->opt_combfilter = c->val;
1279 break;
1280 case V4L2_CID_PRIVATE_LUMAFILTER:
1281 if (c->val) {
1282 btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
1283 btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
1284 } else {
1285 btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
1286 btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
1288 break;
1289 case V4L2_CID_PRIVATE_AUTOMUTE:
1290 btv->opt_automute = c->val;
1291 break;
1292 case V4L2_CID_PRIVATE_AGC_CRUSH:
1293 btwrite(BT848_ADC_RESERVED |
1294 (c->val ? BT848_ADC_CRUSH : 0),
1295 BT848_ADC);
1296 break;
1297 case V4L2_CID_PRIVATE_VCR_HACK:
1298 btv->opt_vcr_hack = c->val;
1299 break;
1300 case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
1301 btwrite(c->val, BT848_WC_UP);
1302 break;
1303 case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
1304 btwrite(c->val, BT848_WC_DOWN);
1305 break;
1306 case V4L2_CID_PRIVATE_UV_RATIO:
1307 btv->opt_uv_ratio = c->val;
1308 bt848_sat(btv, btv->saturation);
1309 break;
1310 case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
1311 btaor((c->val << 7), ~BT848_OFORM_RANGE, BT848_OFORM);
1312 break;
1313 case V4L2_CID_PRIVATE_CORING:
1314 btaor((c->val << 5), ~BT848_OFORM_CORE32, BT848_OFORM);
1315 break;
1316 default:
1317 return -EINVAL;
1319 return 0;
1322 /* ----------------------------------------------------------------------- */
1324 static const struct v4l2_ctrl_ops bttv_ctrl_ops = {
1325 .s_ctrl = bttv_s_ctrl,
1328 static struct v4l2_ctrl_config bttv_ctrl_combfilter = {
1329 .ops = &bttv_ctrl_ops,
1330 .id = V4L2_CID_PRIVATE_COMBFILTER,
1331 .name = "Comb Filter",
1332 .type = V4L2_CTRL_TYPE_BOOLEAN,
1333 .min = 0,
1334 .max = 1,
1335 .step = 1,
1336 .def = 1,
1339 static struct v4l2_ctrl_config bttv_ctrl_automute = {
1340 .ops = &bttv_ctrl_ops,
1341 .id = V4L2_CID_PRIVATE_AUTOMUTE,
1342 .name = "Auto Mute",
1343 .type = V4L2_CTRL_TYPE_BOOLEAN,
1344 .min = 0,
1345 .max = 1,
1346 .step = 1,
1347 .def = 1,
1350 static struct v4l2_ctrl_config bttv_ctrl_lumafilter = {
1351 .ops = &bttv_ctrl_ops,
1352 .id = V4L2_CID_PRIVATE_LUMAFILTER,
1353 .name = "Luma Decimation Filter",
1354 .type = V4L2_CTRL_TYPE_BOOLEAN,
1355 .min = 0,
1356 .max = 1,
1357 .step = 1,
1358 .def = 1,
1361 static struct v4l2_ctrl_config bttv_ctrl_agc_crush = {
1362 .ops = &bttv_ctrl_ops,
1363 .id = V4L2_CID_PRIVATE_AGC_CRUSH,
1364 .name = "AGC Crush",
1365 .type = V4L2_CTRL_TYPE_BOOLEAN,
1366 .min = 0,
1367 .max = 1,
1368 .step = 1,
1369 .def = 1,
1372 static struct v4l2_ctrl_config bttv_ctrl_vcr_hack = {
1373 .ops = &bttv_ctrl_ops,
1374 .id = V4L2_CID_PRIVATE_VCR_HACK,
1375 .name = "VCR Hack",
1376 .type = V4L2_CTRL_TYPE_BOOLEAN,
1377 .min = 0,
1378 .max = 1,
1379 .step = 1,
1380 .def = 1,
1383 static struct v4l2_ctrl_config bttv_ctrl_whitecrush_lower = {
1384 .ops = &bttv_ctrl_ops,
1385 .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
1386 .name = "Whitecrush Lower",
1387 .type = V4L2_CTRL_TYPE_INTEGER,
1388 .min = 0,
1389 .max = 255,
1390 .step = 1,
1391 .def = 0x7f,
1394 static struct v4l2_ctrl_config bttv_ctrl_whitecrush_upper = {
1395 .ops = &bttv_ctrl_ops,
1396 .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
1397 .name = "Whitecrush Upper",
1398 .type = V4L2_CTRL_TYPE_INTEGER,
1399 .min = 0,
1400 .max = 255,
1401 .step = 1,
1402 .def = 0xcf,
1405 static struct v4l2_ctrl_config bttv_ctrl_uv_ratio = {
1406 .ops = &bttv_ctrl_ops,
1407 .id = V4L2_CID_PRIVATE_UV_RATIO,
1408 .name = "UV Ratio",
1409 .type = V4L2_CTRL_TYPE_INTEGER,
1410 .min = 0,
1411 .max = 100,
1412 .step = 1,
1413 .def = 50,
1416 static struct v4l2_ctrl_config bttv_ctrl_full_luma = {
1417 .ops = &bttv_ctrl_ops,
1418 .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
1419 .name = "Full Luma Range",
1420 .type = V4L2_CTRL_TYPE_BOOLEAN,
1421 .min = 0,
1422 .max = 1,
1423 .step = 1,
1426 static struct v4l2_ctrl_config bttv_ctrl_coring = {
1427 .ops = &bttv_ctrl_ops,
1428 .id = V4L2_CID_PRIVATE_CORING,
1429 .name = "Coring",
1430 .type = V4L2_CTRL_TYPE_INTEGER,
1431 .min = 0,
1432 .max = 3,
1433 .step = 1,
1437 /* ----------------------------------------------------------------------- */
1439 void bttv_gpio_tracking(struct bttv *btv, char *comment)
1441 unsigned int outbits, data;
1442 outbits = btread(BT848_GPIO_OUT_EN);
1443 data = btread(BT848_GPIO_DATA);
1444 pr_debug("%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
1445 btv->c.nr, outbits, data & outbits, data & ~outbits, comment);
1448 static const struct bttv_format*
1449 format_by_fourcc(int fourcc)
1451 unsigned int i;
1453 for (i = 0; i < FORMATS; i++) {
1454 if (-1 == formats[i].fourcc)
1455 continue;
1456 if (formats[i].fourcc == fourcc)
1457 return formats+i;
1459 return NULL;
1462 /* ----------------------------------------------------------------------- */
1463 /* video4linux (1) interface */
1465 static int queue_setup(struct vb2_queue *q, unsigned int *num_buffers,
1466 unsigned int *num_planes, unsigned int sizes[],
1467 struct device *alloc_devs[])
1469 struct bttv *btv = vb2_get_drv_priv(q);
1470 unsigned int size = btv->fmt->depth * btv->width * btv->height >> 3;
1472 if (*num_planes)
1473 return sizes[0] < size ? -EINVAL : 0;
1474 *num_planes = 1;
1475 sizes[0] = size;
1477 return 0;
1480 static void buf_queue(struct vb2_buffer *vb)
1482 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1483 struct vb2_queue *vq = vb->vb2_queue;
1484 struct bttv *btv = vb2_get_drv_priv(vq);
1485 struct bttv_buffer *buf = container_of(vbuf, struct bttv_buffer, vbuf);
1486 unsigned long flags;
1488 spin_lock_irqsave(&btv->s_lock, flags);
1489 if (list_empty(&btv->capture)) {
1490 btv->loop_irq = BT848_RISC_VIDEO;
1491 if (vb2_is_streaming(&btv->vbiq))
1492 btv->loop_irq |= BT848_RISC_VBI;
1493 bttv_set_dma(btv, BT848_CAP_CTL_CAPTURE_ODD |
1494 BT848_CAP_CTL_CAPTURE_EVEN);
1496 list_add_tail(&buf->list, &btv->capture);
1497 spin_unlock_irqrestore(&btv->s_lock, flags);
1500 static int buf_prepare(struct vb2_buffer *vb)
1502 struct vb2_queue *vq = vb->vb2_queue;
1503 struct bttv *btv = vb2_get_drv_priv(vq);
1504 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1505 struct bttv_buffer *buf = container_of(vbuf, struct bttv_buffer, vbuf);
1506 unsigned int size = (btv->fmt->depth * btv->width * btv->height) >> 3;
1508 if (vb2_plane_size(vb, 0) < size)
1509 return -EINVAL;
1510 vb2_set_plane_payload(vb, 0, size);
1512 if (btv->field != V4L2_FIELD_ALTERNATE) {
1513 buf->vbuf.field = btv->field;
1514 } else if (btv->field_last == V4L2_FIELD_TOP) {
1515 buf->vbuf.field = V4L2_FIELD_BOTTOM;
1516 btv->field_last = V4L2_FIELD_BOTTOM;
1517 } else {
1518 buf->vbuf.field = V4L2_FIELD_TOP;
1519 btv->field_last = V4L2_FIELD_TOP;
1522 /* Allocate memory for risc struct and create the risc program. */
1523 return bttv_buffer_risc(btv, buf);
1526 static void buf_cleanup(struct vb2_buffer *vb)
1528 struct vb2_queue *vq = vb->vb2_queue;
1529 struct bttv *btv = vb2_get_drv_priv(vq);
1530 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1531 struct bttv_buffer *buf = container_of(vbuf, struct bttv_buffer, vbuf);
1533 btcx_riscmem_free(btv->c.pci, &buf->top);
1534 btcx_riscmem_free(btv->c.pci, &buf->bottom);
1537 static int start_streaming(struct vb2_queue *q, unsigned int count)
1539 int seqnr = 0;
1540 struct bttv_buffer *buf;
1541 struct bttv *btv = vb2_get_drv_priv(q);
1543 if (!check_alloc_btres_lock(btv, RESOURCE_VIDEO_STREAM)) {
1544 if (btv->field_count)
1545 seqnr++;
1546 while (!list_empty(&btv->capture)) {
1547 buf = list_entry(btv->capture.next,
1548 struct bttv_buffer, list);
1549 list_del(&buf->list);
1550 buf->vbuf.sequence = (btv->field_count >> 1) + seqnr++;
1551 vb2_buffer_done(&buf->vbuf.vb2_buf,
1552 VB2_BUF_STATE_QUEUED);
1554 return -EBUSY;
1556 if (!vb2_is_streaming(&btv->vbiq)) {
1557 init_irqreg(btv);
1558 btv->field_count = 0;
1560 btv->framedrop = 0;
1562 return 0;
1565 static void stop_streaming(struct vb2_queue *q)
1567 unsigned long flags;
1568 struct bttv *btv = vb2_get_drv_priv(q);
1570 vb2_wait_for_all_buffers(q);
1571 spin_lock_irqsave(&btv->s_lock, flags);
1572 free_btres_lock(btv, RESOURCE_VIDEO_STREAM);
1573 if (!vb2_is_streaming(&btv->vbiq)) {
1574 /* stop field counter */
1575 btand(~BT848_INT_VSYNC, BT848_INT_MASK);
1577 spin_unlock_irqrestore(&btv->s_lock, flags);
1580 static const struct vb2_ops bttv_video_qops = {
1581 .queue_setup = queue_setup,
1582 .buf_queue = buf_queue,
1583 .buf_prepare = buf_prepare,
1584 .buf_cleanup = buf_cleanup,
1585 .start_streaming = start_streaming,
1586 .stop_streaming = stop_streaming,
1589 static void radio_enable(struct bttv *btv)
1591 /* Switch to the radio tuner */
1592 if (!btv->has_radio_tuner) {
1593 btv->has_radio_tuner = 1;
1594 bttv_call_all(btv, tuner, s_radio);
1595 btv->audio_input = TVAUDIO_INPUT_RADIO;
1596 audio_input(btv, btv->audio_input);
1600 static int bttv_s_std(struct file *file, void *priv, v4l2_std_id id)
1602 struct bttv *btv = video_drvdata(file);
1603 unsigned int i;
1605 for (i = 0; i < BTTV_TVNORMS; i++)
1606 if (id & bttv_tvnorms[i].v4l2_id)
1607 break;
1608 if (i == BTTV_TVNORMS)
1609 return -EINVAL;
1610 btv->std = id;
1611 set_tvnorm(btv, i);
1612 return 0;
1615 static int bttv_g_std(struct file *file, void *priv, v4l2_std_id *id)
1617 struct bttv *btv = video_drvdata(file);
1619 *id = btv->std;
1620 return 0;
1623 static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
1625 struct bttv *btv = video_drvdata(file);
1627 if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
1628 *id &= V4L2_STD_625_50;
1629 else
1630 *id &= V4L2_STD_525_60;
1631 return 0;
1634 static int bttv_enum_input(struct file *file, void *priv,
1635 struct v4l2_input *i)
1637 struct bttv *btv = video_drvdata(file);
1639 if (i->index >= bttv_tvcards[btv->c.type].video_inputs)
1640 return -EINVAL;
1642 i->type = V4L2_INPUT_TYPE_CAMERA;
1643 i->audioset = 0;
1645 if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
1646 sprintf(i->name, "Television");
1647 i->type = V4L2_INPUT_TYPE_TUNER;
1648 i->tuner = 0;
1649 } else if (i->index == btv->svhs) {
1650 sprintf(i->name, "S-Video");
1651 } else {
1652 sprintf(i->name, "Composite%d", i->index);
1655 if (i->index == btv->input) {
1656 __u32 dstatus = btread(BT848_DSTATUS);
1657 if (0 == (dstatus & BT848_DSTATUS_PRES))
1658 i->status |= V4L2_IN_ST_NO_SIGNAL;
1659 if (0 == (dstatus & BT848_DSTATUS_HLOC))
1660 i->status |= V4L2_IN_ST_NO_H_LOCK;
1663 i->std = BTTV_NORMS;
1664 return 0;
1667 static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
1669 struct bttv *btv = video_drvdata(file);
1671 *i = btv->input;
1673 return 0;
1676 static int bttv_s_input(struct file *file, void *priv, unsigned int i)
1678 struct bttv *btv = video_drvdata(file);
1680 if (i >= bttv_tvcards[btv->c.type].video_inputs)
1681 return -EINVAL;
1683 set_input(btv, i, btv->tvnorm);
1684 return 0;
1687 static int bttv_s_tuner(struct file *file, void *priv,
1688 const struct v4l2_tuner *t)
1690 struct bttv *btv = video_drvdata(file);
1692 if (t->index)
1693 return -EINVAL;
1695 bttv_call_all(btv, tuner, s_tuner, t);
1697 if (btv->audio_mode_gpio) {
1698 struct v4l2_tuner copy = *t;
1700 btv->audio_mode_gpio(btv, &copy, 1);
1702 return 0;
1705 static int bttv_g_frequency(struct file *file, void *priv,
1706 struct v4l2_frequency *f)
1708 struct bttv *btv = video_drvdata(file);
1710 if (f->tuner)
1711 return -EINVAL;
1713 if (f->type == V4L2_TUNER_RADIO)
1714 radio_enable(btv);
1715 f->frequency = f->type == V4L2_TUNER_RADIO ?
1716 btv->radio_freq : btv->tv_freq;
1718 return 0;
1721 static void bttv_set_frequency(struct bttv *btv, const struct v4l2_frequency *f)
1723 struct v4l2_frequency new_freq = *f;
1725 bttv_call_all(btv, tuner, s_frequency, f);
1726 /* s_frequency may clamp the frequency, so get the actual
1727 frequency before assigning radio/tv_freq. */
1728 bttv_call_all(btv, tuner, g_frequency, &new_freq);
1729 if (new_freq.type == V4L2_TUNER_RADIO) {
1730 radio_enable(btv);
1731 btv->radio_freq = new_freq.frequency;
1732 if (btv->has_tea575x) {
1733 btv->tea.freq = btv->radio_freq;
1734 snd_tea575x_set_freq(&btv->tea);
1736 } else {
1737 btv->tv_freq = new_freq.frequency;
1741 static int bttv_s_frequency(struct file *file, void *priv,
1742 const struct v4l2_frequency *f)
1744 struct bttv *btv = video_drvdata(file);
1746 if (f->tuner)
1747 return -EINVAL;
1749 bttv_set_frequency(btv, f);
1750 return 0;
1753 static int bttv_log_status(struct file *file, void *f)
1755 struct video_device *vdev = video_devdata(file);
1756 struct bttv *btv = video_drvdata(file);
1758 v4l2_ctrl_handler_log_status(vdev->ctrl_handler, btv->c.v4l2_dev.name);
1759 bttv_call_all(btv, core, log_status);
1760 return 0;
1763 #ifdef CONFIG_VIDEO_ADV_DEBUG
1764 static int bttv_g_register(struct file *file, void *f,
1765 struct v4l2_dbg_register *reg)
1767 struct bttv *btv = video_drvdata(file);
1769 /* bt848 has a 12-bit register space */
1770 reg->reg &= 0xfff;
1771 reg->val = btread(reg->reg);
1772 reg->size = 1;
1774 return 0;
1777 static int bttv_s_register(struct file *file, void *f,
1778 const struct v4l2_dbg_register *reg)
1780 struct bttv *btv = video_drvdata(file);
1782 /* bt848 has a 12-bit register space */
1783 btwrite(reg->val, reg->reg & 0xfff);
1785 return 0;
1787 #endif
1789 /* Given cropping boundaries b and the scaled width and height of a
1790 single field or frame, which must not exceed hardware limits, this
1791 function adjusts the cropping parameters c. */
1792 static void
1793 bttv_crop_adjust (struct bttv_crop * c,
1794 const struct v4l2_rect * b,
1795 __s32 width,
1796 __s32 height,
1797 enum v4l2_field field)
1799 __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
1800 __s32 max_left;
1801 __s32 max_top;
1803 if (width < c->min_scaled_width) {
1804 /* Max. hor. scale factor 16:1. */
1805 c->rect.width = width * 16;
1806 } else if (width > c->max_scaled_width) {
1807 /* Min. hor. scale factor 1:1. */
1808 c->rect.width = width;
1810 max_left = b->left + b->width - width;
1811 max_left = min(max_left, (__s32) MAX_HDELAY);
1812 if (c->rect.left > max_left)
1813 c->rect.left = max_left;
1816 if (height < c->min_scaled_height) {
1817 /* Max. vert. scale factor 16:1, single fields 8:1. */
1818 c->rect.height = height * 16;
1819 } else if (frame_height > c->max_scaled_height) {
1820 /* Min. vert. scale factor 1:1.
1821 Top and height count field lines times two. */
1822 c->rect.height = (frame_height + 1) & ~1;
1824 max_top = b->top + b->height - c->rect.height;
1825 if (c->rect.top > max_top)
1826 c->rect.top = max_top;
1829 bttv_crop_calc_limits(c);
1832 /* Returns an error if scaling to a frame or single field with the given
1833 width and height is not possible with the current cropping parameters
1834 and width aligned according to width_mask. If adjust_size is TRUE the
1835 function may adjust the width and/or height instead, rounding width
1836 to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
1837 also adjust the current cropping parameters to get closer to the
1838 desired image size. */
1839 static int
1840 limit_scaled_size_lock(struct bttv *btv, __s32 *width, __s32 *height,
1841 enum v4l2_field field, unsigned int width_mask,
1842 unsigned int width_bias, int adjust_size,
1843 int adjust_crop)
1845 const struct v4l2_rect *b;
1846 struct bttv_crop *c;
1847 __s32 min_width;
1848 __s32 min_height;
1849 __s32 max_width;
1850 __s32 max_height;
1851 int rc;
1853 WARN_ON((int)width_mask >= 0 ||
1854 width_bias >= (unsigned int)(-width_mask));
1856 /* Make sure tvnorm, vbi_end and the current cropping parameters
1857 remain consistent until we're done. */
1859 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
1861 /* Do crop - use current, don't - use default parameters. */
1862 c = &btv->crop[!!btv->do_crop];
1864 if (btv->do_crop
1865 && adjust_size
1866 && adjust_crop
1867 && !locked_btres(btv, VIDEO_RESOURCES)) {
1868 min_width = 48;
1869 min_height = 32;
1871 /* We cannot scale up. When the scaled image is larger
1872 than crop.rect we adjust the crop.rect as required
1873 by the V4L2 spec, hence cropcap.bounds are our limit. */
1874 max_width = min_t(unsigned int, b->width, MAX_HACTIVE);
1875 max_height = b->height;
1877 /* We cannot capture the same line as video and VBI data.
1878 Note btv->vbi_end is really a minimum, see
1879 bttv_vbi_try_fmt(). */
1880 if (btv->vbi_end > b->top) {
1881 max_height -= btv->vbi_end - b->top;
1882 rc = -EBUSY;
1883 if (min_height > max_height)
1884 goto fail;
1886 } else {
1887 rc = -EBUSY;
1888 if (btv->vbi_end > c->rect.top)
1889 goto fail;
1891 min_width = c->min_scaled_width;
1892 min_height = c->min_scaled_height;
1893 max_width = c->max_scaled_width;
1894 max_height = c->max_scaled_height;
1896 adjust_crop = 0;
1899 min_width = (min_width - width_mask - 1) & width_mask;
1900 max_width = max_width & width_mask;
1902 /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
1903 /* Min. scale factor is 1:1. */
1904 max_height >>= !V4L2_FIELD_HAS_BOTH(field);
1906 if (adjust_size) {
1907 *width = clamp(*width, min_width, max_width);
1908 *height = clamp(*height, min_height, max_height);
1910 /* Round after clamping to avoid overflow. */
1911 *width = (*width + width_bias) & width_mask;
1913 if (adjust_crop) {
1914 bttv_crop_adjust(c, b, *width, *height, field);
1916 if (btv->vbi_end > c->rect.top) {
1917 /* Move the crop window out of the way. */
1918 c->rect.top = btv->vbi_end;
1921 } else {
1922 rc = -EINVAL;
1923 if (*width < min_width ||
1924 *height < min_height ||
1925 *width > max_width ||
1926 *height > max_height ||
1927 0 != (*width & ~width_mask))
1928 goto fail;
1931 rc = 0; /* success */
1933 fail:
1935 return rc;
1938 static int bttv_switch_type(struct bttv *btv, enum v4l2_buf_type type)
1940 int res;
1941 struct vb2_queue *q;
1943 switch (type) {
1944 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1945 q = &btv->capq;
1946 res = RESOURCE_VIDEO_STREAM;
1947 break;
1948 case V4L2_BUF_TYPE_VBI_CAPTURE:
1949 q = &btv->vbiq;
1950 res = RESOURCE_VBI;
1951 break;
1952 default:
1953 WARN_ON(1);
1954 return -EINVAL;
1957 if (check_btres(btv, res))
1958 return -EBUSY;
1959 if (vb2_is_busy(q))
1960 return -EBUSY;
1961 btv->type = type;
1963 return 0;
1966 static void
1967 pix_format_set_size (struct v4l2_pix_format * f,
1968 const struct bttv_format * fmt,
1969 unsigned int width,
1970 unsigned int height)
1972 f->width = width;
1973 f->height = height;
1975 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
1976 f->bytesperline = width; /* Y plane */
1977 f->sizeimage = (width * height * fmt->depth) >> 3;
1978 } else {
1979 f->bytesperline = (width * fmt->depth) >> 3;
1980 f->sizeimage = height * f->bytesperline;
1984 static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
1985 struct v4l2_format *f)
1987 struct bttv *btv = video_drvdata(file);
1989 pix_format_set_size(&f->fmt.pix, btv->fmt, btv->width, btv->height);
1990 f->fmt.pix.field = btv->field;
1991 f->fmt.pix.pixelformat = btv->fmt->fourcc;
1992 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1994 return 0;
1997 static void bttv_get_width_mask_vid_cap(const struct bttv_format *fmt,
1998 unsigned int *width_mask,
1999 unsigned int *width_bias)
2001 if (fmt->flags & FORMAT_FLAGS_PLANAR) {
2002 *width_mask = ~15; /* width must be a multiple of 16 pixels */
2003 *width_bias = 8; /* nearest */
2004 } else {
2005 *width_mask = ~3; /* width must be a multiple of 4 pixels */
2006 *width_bias = 2; /* nearest */
2010 static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
2011 struct v4l2_format *f)
2013 const struct bttv_format *fmt;
2014 struct bttv *btv = video_drvdata(file);
2015 enum v4l2_field field;
2016 __s32 width, height;
2017 __s32 height2;
2018 unsigned int width_mask, width_bias;
2019 int rc;
2021 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2022 if (NULL == fmt)
2023 return -EINVAL;
2025 field = f->fmt.pix.field;
2027 switch (field) {
2028 case V4L2_FIELD_TOP:
2029 case V4L2_FIELD_BOTTOM:
2030 case V4L2_FIELD_ALTERNATE:
2031 case V4L2_FIELD_INTERLACED:
2032 break;
2033 case V4L2_FIELD_SEQ_BT:
2034 case V4L2_FIELD_SEQ_TB:
2035 if (!(fmt->flags & FORMAT_FLAGS_PLANAR)) {
2036 field = V4L2_FIELD_SEQ_TB;
2037 break;
2039 fallthrough;
2040 default: /* FIELD_ANY case */
2041 height2 = btv->crop[!!btv->do_crop].rect.height >> 1;
2042 field = (f->fmt.pix.height > height2)
2043 ? V4L2_FIELD_INTERLACED
2044 : V4L2_FIELD_BOTTOM;
2045 break;
2048 width = f->fmt.pix.width;
2049 height = f->fmt.pix.height;
2051 bttv_get_width_mask_vid_cap(fmt, &width_mask, &width_bias);
2052 rc = limit_scaled_size_lock(btv, &width, &height, field, width_mask,
2053 width_bias, 1, 0);
2054 if (0 != rc)
2055 return rc;
2057 /* update data for the application */
2058 f->fmt.pix.field = field;
2059 pix_format_set_size(&f->fmt.pix, fmt, width, height);
2060 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
2062 return 0;
2065 static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
2066 struct v4l2_format *f)
2068 int retval;
2069 const struct bttv_format *fmt;
2070 struct bttv *btv = video_drvdata(file);
2071 __s32 width, height;
2072 unsigned int width_mask, width_bias;
2073 enum v4l2_field field;
2075 retval = bttv_switch_type(btv, f->type);
2076 if (0 != retval)
2077 return retval;
2079 retval = bttv_try_fmt_vid_cap(file, priv, f);
2080 if (0 != retval)
2081 return retval;
2083 width = f->fmt.pix.width;
2084 height = f->fmt.pix.height;
2085 field = f->fmt.pix.field;
2087 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
2088 bttv_get_width_mask_vid_cap(fmt, &width_mask, &width_bias);
2089 retval = limit_scaled_size_lock(btv, &width, &height, f->fmt.pix.field,
2090 width_mask, width_bias, 1, 1);
2091 if (0 != retval)
2092 return retval;
2094 f->fmt.pix.field = field;
2096 /* update our state information */
2097 btv->fmt = fmt;
2098 btv->width = f->fmt.pix.width;
2099 btv->height = f->fmt.pix.height;
2100 btv->field = f->fmt.pix.field;
2102 * When field is V4L2_FIELD_ALTERNATE, buffers will be either
2103 * V4L2_FIELD_TOP or V4L2_FIELD_BOTTOM depending on the value of
2104 * field_last. Initialize field_last to V4L2_FIELD_BOTTOM so that
2105 * streaming starts with a V4L2_FIELD_TOP buffer.
2107 btv->field_last = V4L2_FIELD_BOTTOM;
2109 return 0;
2112 static int bttv_querycap(struct file *file, void *priv,
2113 struct v4l2_capability *cap)
2115 struct bttv *btv = video_drvdata(file);
2117 if (0 == v4l2)
2118 return -EINVAL;
2120 strscpy(cap->driver, "bttv", sizeof(cap->driver));
2121 strscpy(cap->card, btv->video_dev.name, sizeof(cap->card));
2122 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE |
2123 V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
2124 if (video_is_registered(&btv->vbi_dev))
2125 cap->capabilities |= V4L2_CAP_VBI_CAPTURE;
2126 if (video_is_registered(&btv->radio_dev)) {
2127 cap->capabilities |= V4L2_CAP_RADIO;
2128 if (btv->has_tea575x)
2129 cap->capabilities |= V4L2_CAP_HW_FREQ_SEEK;
2133 * No need to lock here: those vars are initialized during board
2134 * probe and remains untouched during the rest of the driver lifecycle
2136 if (btv->has_saa6588)
2137 cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
2138 if (btv->tuner_type != TUNER_ABSENT)
2139 cap->capabilities |= V4L2_CAP_TUNER;
2140 return 0;
2143 static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
2144 struct v4l2_fmtdesc *f)
2146 int index = -1, i;
2148 for (i = 0; i < FORMATS; i++) {
2149 if (formats[i].fourcc != -1)
2150 index++;
2151 if ((unsigned int)index == f->index)
2152 break;
2154 if (FORMATS == i)
2155 return -EINVAL;
2157 f->pixelformat = formats[i].fourcc;
2159 return 0;
2162 static int bttv_g_parm(struct file *file, void *f,
2163 struct v4l2_streamparm *parm)
2165 struct bttv *btv = video_drvdata(file);
2167 if (parm->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2168 return -EINVAL;
2169 parm->parm.capture.readbuffers = gbuffers;
2170 v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
2171 &parm->parm.capture.timeperframe);
2173 return 0;
2176 static int bttv_g_tuner(struct file *file, void *priv,
2177 struct v4l2_tuner *t)
2179 struct bttv *btv = video_drvdata(file);
2181 if (0 != t->index)
2182 return -EINVAL;
2184 t->rxsubchans = V4L2_TUNER_SUB_MONO;
2185 t->capability = V4L2_TUNER_CAP_NORM;
2186 bttv_call_all(btv, tuner, g_tuner, t);
2187 strscpy(t->name, "Television", sizeof(t->name));
2188 t->type = V4L2_TUNER_ANALOG_TV;
2189 if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
2190 t->signal = 0xffff;
2192 if (btv->audio_mode_gpio)
2193 btv->audio_mode_gpio(btv, t, 0);
2195 return 0;
2198 static int bttv_g_pixelaspect(struct file *file, void *priv,
2199 int type, struct v4l2_fract *f)
2201 struct bttv *btv = video_drvdata(file);
2203 if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2204 return -EINVAL;
2206 /* defrect and bounds are set via g_selection */
2207 *f = bttv_tvnorms[btv->tvnorm].cropcap.pixelaspect;
2208 return 0;
2211 static int bttv_g_selection(struct file *file, void *f, struct v4l2_selection *sel)
2213 struct bttv *btv = video_drvdata(file);
2215 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2216 return -EINVAL;
2218 switch (sel->target) {
2219 case V4L2_SEL_TGT_CROP:
2220 sel->r = btv->crop[!!btv->do_crop].rect;
2221 break;
2222 case V4L2_SEL_TGT_CROP_DEFAULT:
2223 sel->r = bttv_tvnorms[btv->tvnorm].cropcap.defrect;
2224 break;
2225 case V4L2_SEL_TGT_CROP_BOUNDS:
2226 sel->r = bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2227 break;
2228 default:
2229 return -EINVAL;
2232 return 0;
2235 static int bttv_s_selection(struct file *file, void *f, struct v4l2_selection *sel)
2237 struct bttv *btv = video_drvdata(file);
2238 const struct v4l2_rect *b;
2239 int retval;
2240 struct bttv_crop c;
2241 __s32 b_left;
2242 __s32 b_top;
2243 __s32 b_right;
2244 __s32 b_bottom;
2246 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
2247 return -EINVAL;
2249 if (sel->target != V4L2_SEL_TGT_CROP)
2250 return -EINVAL;
2252 /* Make sure tvnorm, vbi_end and the current cropping
2253 parameters remain consistent until we're done. Note
2254 read() may change vbi_end in check_alloc_btres_lock(). */
2255 retval = -EBUSY;
2257 if (locked_btres(btv, VIDEO_RESOURCES))
2258 return retval;
2260 b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
2262 b_left = b->left;
2263 b_right = b_left + b->width;
2264 b_bottom = b->top + b->height;
2266 b_top = max(b->top, btv->vbi_end);
2267 if (b_top + 32 >= b_bottom) {
2268 return retval;
2271 /* Min. scaled size 48 x 32. */
2272 c.rect.left = clamp_t(s32, sel->r.left, b_left, b_right - 48);
2273 c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
2275 c.rect.width = clamp_t(s32, sel->r.width,
2276 48, b_right - c.rect.left);
2278 c.rect.top = clamp_t(s32, sel->r.top, b_top, b_bottom - 32);
2279 /* Top and height must be a multiple of two. */
2280 c.rect.top = (c.rect.top + 1) & ~1;
2282 c.rect.height = clamp_t(s32, sel->r.height,
2283 32, b_bottom - c.rect.top);
2284 c.rect.height = (c.rect.height + 1) & ~1;
2286 bttv_crop_calc_limits(&c);
2288 sel->r = c.rect;
2290 btv->crop[1] = c;
2292 btv->do_crop = 1;
2294 if (btv->width < c.min_scaled_width)
2295 btv->width = c.min_scaled_width;
2296 else if (btv->width > c.max_scaled_width)
2297 btv->width = c.max_scaled_width;
2299 if (btv->height < c.min_scaled_height)
2300 btv->height = c.min_scaled_height;
2301 else if (btv->height > c.max_scaled_height)
2302 btv->height = c.max_scaled_height;
2304 return 0;
2307 static const struct v4l2_file_operations bttv_fops =
2309 .owner = THIS_MODULE,
2310 .open = v4l2_fh_open,
2311 .release = vb2_fop_release,
2312 .unlocked_ioctl = video_ioctl2,
2313 .read = vb2_fop_read,
2314 .mmap = vb2_fop_mmap,
2315 .poll = vb2_fop_poll,
2318 static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
2319 .vidioc_querycap = bttv_querycap,
2320 .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
2321 .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
2322 .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
2323 .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
2324 .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
2325 .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
2326 .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
2327 .vidioc_g_pixelaspect = bttv_g_pixelaspect,
2328 .vidioc_reqbufs = vb2_ioctl_reqbufs,
2329 .vidioc_create_bufs = vb2_ioctl_create_bufs,
2330 .vidioc_querybuf = vb2_ioctl_querybuf,
2331 .vidioc_qbuf = vb2_ioctl_qbuf,
2332 .vidioc_dqbuf = vb2_ioctl_dqbuf,
2333 .vidioc_streamon = vb2_ioctl_streamon,
2334 .vidioc_streamoff = vb2_ioctl_streamoff,
2335 .vidioc_s_std = bttv_s_std,
2336 .vidioc_g_std = bttv_g_std,
2337 .vidioc_enum_input = bttv_enum_input,
2338 .vidioc_g_input = bttv_g_input,
2339 .vidioc_s_input = bttv_s_input,
2340 .vidioc_g_tuner = bttv_g_tuner,
2341 .vidioc_s_tuner = bttv_s_tuner,
2342 .vidioc_g_selection = bttv_g_selection,
2343 .vidioc_s_selection = bttv_s_selection,
2344 .vidioc_g_parm = bttv_g_parm,
2345 .vidioc_g_frequency = bttv_g_frequency,
2346 .vidioc_s_frequency = bttv_s_frequency,
2347 .vidioc_log_status = bttv_log_status,
2348 .vidioc_querystd = bttv_querystd,
2349 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2350 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2351 #ifdef CONFIG_VIDEO_ADV_DEBUG
2352 .vidioc_g_register = bttv_g_register,
2353 .vidioc_s_register = bttv_s_register,
2354 #endif
2357 static struct video_device bttv_video_template = {
2358 .fops = &bttv_fops,
2359 .ioctl_ops = &bttv_ioctl_ops,
2360 .tvnorms = BTTV_NORMS,
2363 /* ----------------------------------------------------------------------- */
2364 /* radio interface */
2366 static int radio_open(struct file *file)
2368 struct video_device *vdev = video_devdata(file);
2369 struct bttv *btv = video_drvdata(file);
2370 int ret = v4l2_fh_open(file);
2372 if (ret)
2373 return ret;
2375 dprintk("open dev=%s\n", video_device_node_name(vdev));
2376 dprintk("%d: open called (radio)\n", btv->c.nr);
2378 btv->radio_user++;
2379 audio_mute(btv, btv->mute);
2381 return 0;
2384 static int radio_release(struct file *file)
2386 struct bttv *btv = video_drvdata(file);
2387 struct saa6588_command cmd;
2389 btv->radio_user--;
2391 bttv_call_all(btv, core, command, SAA6588_CMD_CLOSE, &cmd);
2393 if (btv->radio_user == 0)
2394 btv->has_radio_tuner = 0;
2396 v4l2_fh_release(file);
2398 return 0;
2401 static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
2403 struct bttv *btv = video_drvdata(file);
2405 if (0 != t->index)
2406 return -EINVAL;
2407 strscpy(t->name, "Radio", sizeof(t->name));
2408 t->type = V4L2_TUNER_RADIO;
2409 radio_enable(btv);
2411 bttv_call_all(btv, tuner, g_tuner, t);
2413 if (btv->audio_mode_gpio)
2414 btv->audio_mode_gpio(btv, t, 0);
2416 if (btv->has_tea575x)
2417 return snd_tea575x_g_tuner(&btv->tea, t);
2419 return 0;
2422 static int radio_s_tuner(struct file *file, void *priv,
2423 const struct v4l2_tuner *t)
2425 struct bttv *btv = video_drvdata(file);
2427 if (0 != t->index)
2428 return -EINVAL;
2430 radio_enable(btv);
2431 bttv_call_all(btv, tuner, s_tuner, t);
2432 return 0;
2435 static int radio_s_hw_freq_seek(struct file *file, void *priv,
2436 const struct v4l2_hw_freq_seek *a)
2438 struct bttv *btv = video_drvdata(file);
2440 if (btv->has_tea575x)
2441 return snd_tea575x_s_hw_freq_seek(file, &btv->tea, a);
2443 return -ENOTTY;
2446 static int radio_enum_freq_bands(struct file *file, void *priv,
2447 struct v4l2_frequency_band *band)
2449 struct bttv *btv = video_drvdata(file);
2451 if (btv->has_tea575x)
2452 return snd_tea575x_enum_freq_bands(&btv->tea, band);
2454 return -ENOTTY;
2457 static ssize_t radio_read(struct file *file, char __user *data,
2458 size_t count, loff_t *ppos)
2460 struct bttv *btv = video_drvdata(file);
2461 struct saa6588_command cmd;
2463 cmd.block_count = count / 3;
2464 cmd.nonblocking = file->f_flags & O_NONBLOCK;
2465 cmd.buffer = data;
2466 cmd.instance = file;
2467 cmd.result = -ENODEV;
2468 radio_enable(btv);
2470 bttv_call_all(btv, core, command, SAA6588_CMD_READ, &cmd);
2472 return cmd.result;
2475 static __poll_t radio_poll(struct file *file, poll_table *wait)
2477 struct bttv *btv = video_drvdata(file);
2478 struct saa6588_command cmd;
2479 __poll_t rc = v4l2_ctrl_poll(file, wait);
2481 radio_enable(btv);
2482 cmd.instance = file;
2483 cmd.event_list = wait;
2484 cmd.poll_mask = 0;
2485 bttv_call_all(btv, core, command, SAA6588_CMD_POLL, &cmd);
2487 return rc | cmd.poll_mask;
2490 static const struct v4l2_file_operations radio_fops =
2492 .owner = THIS_MODULE,
2493 .open = radio_open,
2494 .read = radio_read,
2495 .release = radio_release,
2496 .unlocked_ioctl = video_ioctl2,
2497 .poll = radio_poll,
2500 static const struct v4l2_ioctl_ops radio_ioctl_ops = {
2501 .vidioc_querycap = bttv_querycap,
2502 .vidioc_log_status = bttv_log_status,
2503 .vidioc_g_tuner = radio_g_tuner,
2504 .vidioc_s_tuner = radio_s_tuner,
2505 .vidioc_g_frequency = bttv_g_frequency,
2506 .vidioc_s_frequency = bttv_s_frequency,
2507 .vidioc_s_hw_freq_seek = radio_s_hw_freq_seek,
2508 .vidioc_enum_freq_bands = radio_enum_freq_bands,
2509 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2510 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
2513 static struct video_device radio_template = {
2514 .fops = &radio_fops,
2515 .ioctl_ops = &radio_ioctl_ops,
2518 /* ----------------------------------------------------------------------- */
2519 /* some debug code */
2521 static int bttv_risc_decode(u32 risc)
2523 static char *instr[16] = {
2524 [ BT848_RISC_WRITE >> 28 ] = "write",
2525 [ BT848_RISC_SKIP >> 28 ] = "skip",
2526 [ BT848_RISC_WRITEC >> 28 ] = "writec",
2527 [ BT848_RISC_JUMP >> 28 ] = "jump",
2528 [ BT848_RISC_SYNC >> 28 ] = "sync",
2529 [ BT848_RISC_WRITE123 >> 28 ] = "write123",
2530 [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
2531 [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
2533 static int incr[16] = {
2534 [ BT848_RISC_WRITE >> 28 ] = 2,
2535 [ BT848_RISC_JUMP >> 28 ] = 2,
2536 [ BT848_RISC_SYNC >> 28 ] = 2,
2537 [ BT848_RISC_WRITE123 >> 28 ] = 5,
2538 [ BT848_RISC_SKIP123 >> 28 ] = 2,
2539 [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
2541 static char *bits[] = {
2542 "be0", "be1", "be2", "be3/resync",
2543 "set0", "set1", "set2", "set3",
2544 "clr0", "clr1", "clr2", "clr3",
2545 "irq", "res", "eol", "sol",
2547 int i;
2549 pr_cont("0x%08x [ %s", risc,
2550 instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
2551 for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
2552 if (risc & (1 << (i + 12)))
2553 pr_cont(" %s", bits[i]);
2554 pr_cont(" count=%d ]\n", risc & 0xfff);
2555 return incr[risc >> 28] ? incr[risc >> 28] : 1;
2558 static void bttv_risc_disasm(struct bttv *btv,
2559 struct btcx_riscmem *risc)
2561 unsigned int i,j,n;
2563 pr_info("%s: risc disasm: %p [dma=0x%08lx]\n",
2564 btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
2565 for (i = 0; i < (risc->size >> 2); i += n) {
2566 pr_info("%s: 0x%lx: ",
2567 btv->c.v4l2_dev.name,
2568 (unsigned long)(risc->dma + (i<<2)));
2569 n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
2570 for (j = 1; j < n; j++)
2571 pr_info("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
2572 btv->c.v4l2_dev.name,
2573 (unsigned long)(risc->dma + ((i+j)<<2)),
2574 risc->cpu[i+j], j);
2575 if (0 == risc->cpu[i])
2576 break;
2580 static void bttv_print_riscaddr(struct bttv *btv)
2582 pr_info(" main: %08llx\n", (unsigned long long)btv->main.dma);
2583 pr_info(" vbi : o=%08llx e=%08llx\n",
2584 btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
2585 btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
2586 pr_info(" cap : o=%08llx e=%08llx\n",
2587 btv->curr.top
2588 ? (unsigned long long)btv->curr.top->top.dma : 0,
2589 btv->curr.bottom
2590 ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
2591 bttv_risc_disasm(btv, &btv->main);
2594 /* ----------------------------------------------------------------------- */
2595 /* irq handler */
2597 static char *irq_name[] = {
2598 "FMTCHG", // format change detected (525 vs. 625)
2599 "VSYNC", // vertical sync (new field)
2600 "HSYNC", // horizontal sync
2601 "OFLOW", // chroma/luma AGC overflow
2602 "HLOCK", // horizontal lock changed
2603 "VPRES", // video presence changed
2604 "6", "7",
2605 "I2CDONE", // hw irc operation finished
2606 "GPINT", // gpio port triggered irq
2607 "10",
2608 "RISCI", // risc instruction triggered irq
2609 "FBUS", // pixel data fifo dropped data (high pci bus latencies)
2610 "FTRGT", // pixel data fifo overrun
2611 "FDSR", // fifo data stream resyncronisation
2612 "PPERR", // parity error (data transfer)
2613 "RIPERR", // parity error (read risc instructions)
2614 "PABORT", // pci abort
2615 "OCERR", // risc instruction error
2616 "SCERR", // syncronisation error
2619 static void bttv_print_irqbits(u32 print, u32 mark)
2621 unsigned int i;
2623 pr_cont("bits:");
2624 for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
2625 if (print & (1 << i))
2626 pr_cont(" %s", irq_name[i]);
2627 if (mark & (1 << i))
2628 pr_cont("*");
2632 static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
2634 pr_warn("%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
2635 btv->c.nr,
2636 (unsigned long)btv->main.dma,
2637 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
2638 (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
2639 (unsigned long)rc);
2641 if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
2642 pr_notice("%d: Oh, there (temporarily?) is no input signal. Ok, then this is harmless, don't worry ;)\n",
2643 btv->c.nr);
2644 return;
2646 pr_notice("%d: Uhm. Looks like we have unusual high IRQ latencies\n",
2647 btv->c.nr);
2648 pr_notice("%d: Lets try to catch the culprit red-handed ...\n",
2649 btv->c.nr);
2650 dump_stack();
2653 static int
2654 bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
2656 struct bttv_buffer *item;
2658 memset(set,0,sizeof(*set));
2660 /* capture request ? */
2661 if (!list_empty(&btv->capture)) {
2662 set->frame_irq = BT848_RISC_VIDEO;
2663 item = list_entry(btv->capture.next, struct bttv_buffer, list);
2665 if (V4L2_FIELD_HAS_TOP(item->vbuf.field))
2666 set->top = item;
2667 if (V4L2_FIELD_HAS_BOTTOM(item->vbuf.field))
2668 set->bottom = item;
2670 /* capture request for other field ? */
2671 if (!V4L2_FIELD_HAS_BOTH(item->vbuf.field) &&
2672 item->list.next != &btv->capture) {
2673 item = list_entry(item->list.next,
2674 struct bttv_buffer, list);
2675 /* Mike Isely <isely@pobox.com> - Only check
2676 * and set up the bottom field in the logic
2677 * below. Don't ever do the top field. This
2678 * of course means that if we set up the
2679 * bottom field in the above code that we'll
2680 * actually skip a field. But that's OK.
2681 * Having processed only a single buffer this
2682 * time, then the next time around the first
2683 * available buffer should be for a top field.
2684 * That will then cause us here to set up a
2685 * top then a bottom field in the normal way.
2686 * The alternative to this understanding is
2687 * that we set up the second available buffer
2688 * as a top field, but that's out of order
2689 * since this driver always processes the top
2690 * field first - the effect will be the two
2691 * buffers being returned in the wrong order,
2692 * with the second buffer also being delayed
2693 * by one field time (owing to the fifo nature
2694 * of videobuf). Worse still, we'll be stuck
2695 * doing fields out of order now every time
2696 * until something else causes a field to be
2697 * dropped. By effectively forcing a field to
2698 * drop this way then we always get back into
2699 * sync within a single frame time. (Out of
2700 * order fields can screw up deinterlacing
2701 * algorithms.) */
2702 if (!V4L2_FIELD_HAS_BOTH(item->vbuf.field)) {
2703 if (!set->bottom &&
2704 item->vbuf.field == V4L2_FIELD_BOTTOM)
2705 set->bottom = item;
2706 if (set->top && set->bottom) {
2708 * The buffer set has a top buffer and
2709 * a bottom buffer and they are not
2710 * copies of each other.
2712 set->top_irq = BT848_RISC_TOP;
2718 dprintk("%d: next set: top=%p bottom=%p [irq=%d,%d]\n",
2719 btv->c.nr, set->top, set->bottom,
2720 set->frame_irq, set->top_irq);
2721 return 0;
2724 static void
2725 bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
2726 struct bttv_buffer_set *curr, unsigned int state)
2728 u64 ts = ktime_get_ns();
2730 if (wakeup->top == wakeup->bottom) {
2731 if (NULL != wakeup->top && curr->top != wakeup->top) {
2732 if (irq_debug > 1)
2733 pr_debug("%d: wakeup: both=%p\n",
2734 btv->c.nr, wakeup->top);
2735 wakeup->top->vbuf.vb2_buf.timestamp = ts;
2736 wakeup->top->vbuf.sequence = btv->field_count >> 1;
2737 vb2_buffer_done(&wakeup->top->vbuf.vb2_buf, state);
2738 if (btv->field_count == 0)
2739 btor(BT848_INT_VSYNC, BT848_INT_MASK);
2741 } else {
2742 if (NULL != wakeup->top && curr->top != wakeup->top) {
2743 if (irq_debug > 1)
2744 pr_debug("%d: wakeup: top=%p\n",
2745 btv->c.nr, wakeup->top);
2746 wakeup->top->vbuf.vb2_buf.timestamp = ts;
2747 wakeup->top->vbuf.sequence = btv->field_count >> 1;
2748 vb2_buffer_done(&wakeup->top->vbuf.vb2_buf, state);
2749 if (btv->field_count == 0)
2750 btor(BT848_INT_VSYNC, BT848_INT_MASK);
2752 if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
2753 if (irq_debug > 1)
2754 pr_debug("%d: wakeup: bottom=%p\n",
2755 btv->c.nr, wakeup->bottom);
2756 wakeup->bottom->vbuf.vb2_buf.timestamp = ts;
2757 wakeup->bottom->vbuf.sequence = btv->field_count >> 1;
2758 vb2_buffer_done(&wakeup->bottom->vbuf.vb2_buf, state);
2759 if (btv->field_count == 0)
2760 btor(BT848_INT_VSYNC, BT848_INT_MASK);
2765 static void
2766 bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
2767 unsigned int state)
2769 if (NULL == wakeup)
2770 return;
2771 wakeup->vbuf.vb2_buf.timestamp = ktime_get_ns();
2772 wakeup->vbuf.sequence = btv->field_count >> 1;
2775 * Ugly hack for backwards compatibility.
2776 * Some applications expect that the last 4 bytes of
2777 * the VBI data contains the sequence number.
2779 * This makes it possible to associate the VBI data
2780 * with the video frame if you use read() to get the
2781 * VBI data.
2783 if (vb2_fileio_is_active(wakeup->vbuf.vb2_buf.vb2_queue)) {
2784 u32 *vaddr = vb2_plane_vaddr(&wakeup->vbuf.vb2_buf, 0);
2785 unsigned long size =
2786 vb2_get_plane_payload(&wakeup->vbuf.vb2_buf, 0) / 4;
2788 if (vaddr && size) {
2789 vaddr += size - 1;
2790 *vaddr = wakeup->vbuf.sequence;
2794 vb2_buffer_done(&wakeup->vbuf.vb2_buf, state);
2795 if (btv->field_count == 0)
2796 btor(BT848_INT_VSYNC, BT848_INT_MASK);
2799 static void bttv_irq_timeout(struct timer_list *t)
2801 struct bttv *btv = from_timer(btv, t, timeout);
2802 struct bttv_buffer_set old,new;
2803 struct bttv_buffer *ovbi;
2804 struct bttv_buffer *item;
2805 unsigned long flags;
2806 int seqnr = 0;
2808 if (bttv_verbose) {
2809 pr_info("%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
2810 btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
2811 btread(BT848_RISC_COUNT));
2812 bttv_print_irqbits(btread(BT848_INT_STAT),0);
2813 pr_cont("\n");
2816 spin_lock_irqsave(&btv->s_lock,flags);
2818 /* deactivate stuff */
2819 memset(&new,0,sizeof(new));
2820 old = btv->curr;
2821 ovbi = btv->cvbi;
2822 btv->curr = new;
2823 btv->cvbi = NULL;
2824 btv->loop_irq = 0;
2825 bttv_buffer_activate_video(btv, &new);
2826 bttv_buffer_activate_vbi(btv, NULL);
2827 bttv_set_dma(btv, 0);
2829 /* wake up */
2830 bttv_irq_wakeup_video(btv, &old, &new, VB2_BUF_STATE_DONE);
2831 bttv_irq_wakeup_vbi(btv, ovbi, VB2_BUF_STATE_DONE);
2833 /* cancel all outstanding capture / vbi requests */
2834 if (btv->field_count)
2835 seqnr++;
2836 while (!list_empty(&btv->capture)) {
2837 item = list_entry(btv->capture.next, struct bttv_buffer, list);
2838 list_del(&item->list);
2839 item->vbuf.vb2_buf.timestamp = ktime_get_ns();
2840 item->vbuf.sequence = (btv->field_count >> 1) + seqnr++;
2841 vb2_buffer_done(&item->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
2843 while (!list_empty(&btv->vcapture)) {
2844 item = list_entry(btv->vcapture.next, struct bttv_buffer, list);
2845 list_del(&item->list);
2846 item->vbuf.vb2_buf.timestamp = ktime_get_ns();
2847 item->vbuf.sequence = (btv->field_count >> 1) + seqnr++;
2848 vb2_buffer_done(&item->vbuf.vb2_buf, VB2_BUF_STATE_ERROR);
2851 btv->errors++;
2852 spin_unlock_irqrestore(&btv->s_lock,flags);
2855 static void
2856 bttv_irq_wakeup_top(struct bttv *btv)
2858 struct bttv_buffer *wakeup = btv->curr.top;
2860 if (NULL == wakeup)
2861 return;
2863 spin_lock(&btv->s_lock);
2864 btv->curr.top_irq = 0;
2865 btv->curr.top = NULL;
2866 bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
2867 wakeup->vbuf.vb2_buf.timestamp = ktime_get_ns();
2868 wakeup->vbuf.sequence = btv->field_count >> 1;
2869 vb2_buffer_done(&wakeup->vbuf.vb2_buf, VB2_BUF_STATE_DONE);
2870 if (btv->field_count == 0)
2871 btor(BT848_INT_VSYNC, BT848_INT_MASK);
2872 spin_unlock(&btv->s_lock);
2875 static inline int is_active(struct btcx_riscmem *risc, u32 rc)
2877 if (rc < risc->dma)
2878 return 0;
2879 if (rc > risc->dma + risc->size)
2880 return 0;
2881 return 1;
2884 static void
2885 bttv_irq_switch_video(struct bttv *btv)
2887 struct bttv_buffer_set new;
2888 struct bttv_buffer_set old;
2889 dma_addr_t rc;
2891 spin_lock(&btv->s_lock);
2893 /* new buffer set */
2894 bttv_irq_next_video(btv, &new);
2895 rc = btread(BT848_RISC_COUNT);
2896 if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
2897 (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
2898 btv->framedrop++;
2899 if (debug_latency)
2900 bttv_irq_debug_low_latency(btv, rc);
2901 spin_unlock(&btv->s_lock);
2902 return;
2905 /* switch over */
2906 old = btv->curr;
2907 btv->curr = new;
2908 btv->loop_irq &= ~BT848_RISC_VIDEO;
2909 bttv_buffer_activate_video(btv, &new);
2910 bttv_set_dma(btv, 0);
2912 /* switch input */
2913 if (UNSET != btv->new_input) {
2914 video_mux(btv,btv->new_input);
2915 btv->new_input = UNSET;
2918 /* wake up finished buffers */
2919 bttv_irq_wakeup_video(btv, &old, &new, VB2_BUF_STATE_DONE);
2920 spin_unlock(&btv->s_lock);
2923 static void
2924 bttv_irq_switch_vbi(struct bttv *btv)
2926 struct bttv_buffer *new = NULL;
2927 struct bttv_buffer *old;
2928 u32 rc;
2930 spin_lock(&btv->s_lock);
2932 if (!list_empty(&btv->vcapture))
2933 new = list_entry(btv->vcapture.next, struct bttv_buffer, list);
2934 old = btv->cvbi;
2936 rc = btread(BT848_RISC_COUNT);
2937 if (NULL != old && (is_active(&old->top, rc) ||
2938 is_active(&old->bottom, rc))) {
2939 btv->framedrop++;
2940 if (debug_latency)
2941 bttv_irq_debug_low_latency(btv, rc);
2942 spin_unlock(&btv->s_lock);
2943 return;
2946 /* switch */
2947 btv->cvbi = new;
2948 btv->loop_irq &= ~BT848_RISC_VBI;
2949 bttv_buffer_activate_vbi(btv, new);
2950 bttv_set_dma(btv, 0);
2952 bttv_irq_wakeup_vbi(btv, old, VB2_BUF_STATE_DONE);
2953 spin_unlock(&btv->s_lock);
2956 static irqreturn_t bttv_irq(int irq, void *dev_id)
2958 u32 stat,astat;
2959 u32 dstat;
2960 int count;
2961 struct bttv *btv;
2962 int handled = 0;
2964 btv=(struct bttv *)dev_id;
2966 count=0;
2967 while (1) {
2968 /* get/clear interrupt status bits */
2969 stat=btread(BT848_INT_STAT);
2970 astat=stat&btread(BT848_INT_MASK);
2971 if (!astat)
2972 break;
2973 handled = 1;
2974 btwrite(stat,BT848_INT_STAT);
2976 /* get device status bits */
2977 dstat=btread(BT848_DSTATUS);
2979 if (irq_debug) {
2980 pr_debug("%d: irq loop=%d fc=%d riscs=%x, riscc=%08x, ",
2981 btv->c.nr, count, btv->field_count,
2982 stat>>28, btread(BT848_RISC_COUNT));
2983 bttv_print_irqbits(stat,astat);
2984 if (stat & BT848_INT_HLOCK)
2985 pr_cont(" HLOC => %s",
2986 dstat & BT848_DSTATUS_HLOC
2987 ? "yes" : "no");
2988 if (stat & BT848_INT_VPRES)
2989 pr_cont(" PRES => %s",
2990 dstat & BT848_DSTATUS_PRES
2991 ? "yes" : "no");
2992 if (stat & BT848_INT_FMTCHG)
2993 pr_cont(" NUML => %s",
2994 dstat & BT848_DSTATUS_NUML
2995 ? "625" : "525");
2996 pr_cont("\n");
2999 if (astat&BT848_INT_VSYNC)
3000 btv->field_count++;
3002 if ((astat & BT848_INT_GPINT) && btv->remote) {
3003 bttv_input_irq(btv);
3006 if (astat & BT848_INT_I2CDONE) {
3007 btv->i2c_done = stat;
3008 wake_up(&btv->i2c_queue);
3011 if ((astat & BT848_INT_RISCI) && (stat & BT848_INT_RISCS_VBI))
3012 bttv_irq_switch_vbi(btv);
3014 if ((astat & BT848_INT_RISCI) && (stat & BT848_INT_RISCS_TOP))
3015 bttv_irq_wakeup_top(btv);
3017 if ((astat & BT848_INT_RISCI) && (stat & BT848_INT_RISCS_VIDEO))
3018 bttv_irq_switch_video(btv);
3020 if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
3021 /* trigger automute */
3022 audio_mux_gpio(btv, btv->audio_input, btv->mute);
3024 if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
3025 pr_info("%d: %s%s @ %08x,",
3026 btv->c.nr,
3027 (astat & BT848_INT_SCERR) ? "SCERR" : "",
3028 (astat & BT848_INT_OCERR) ? "OCERR" : "",
3029 btread(BT848_RISC_COUNT));
3030 bttv_print_irqbits(stat,astat);
3031 pr_cont("\n");
3032 if (bttv_debug)
3033 bttv_print_riscaddr(btv);
3035 if (fdsr && astat & BT848_INT_FDSR) {
3036 pr_info("%d: FDSR @ %08x\n",
3037 btv->c.nr, btread(BT848_RISC_COUNT));
3038 if (bttv_debug)
3039 bttv_print_riscaddr(btv);
3042 count++;
3043 if (count > 4) {
3045 if (count > 8 || !(astat & BT848_INT_GPINT)) {
3046 btwrite(0, BT848_INT_MASK);
3048 pr_err("%d: IRQ lockup, cleared int mask [",
3049 btv->c.nr);
3050 } else {
3051 pr_err("%d: IRQ lockup, clearing GPINT from int mask [",
3052 btv->c.nr);
3054 btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
3055 BT848_INT_MASK);
3058 bttv_print_irqbits(stat,astat);
3060 pr_cont("]\n");
3063 btv->irq_total++;
3064 if (handled)
3065 btv->irq_me++;
3066 return IRQ_RETVAL(handled);
3070 /* ----------------------------------------------------------------------- */
3071 /* initialization */
3073 static int vdev_init(struct bttv *btv, struct video_device *vfd,
3074 const struct video_device *template,
3075 const char *type_name)
3077 int err;
3078 struct vb2_queue *q;
3079 *vfd = *template;
3080 vfd->v4l2_dev = &btv->c.v4l2_dev;
3081 vfd->release = video_device_release_empty;
3082 video_set_drvdata(vfd, btv);
3083 snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
3084 btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
3085 type_name, bttv_tvcards[btv->c.type].name);
3086 if (btv->tuner_type == TUNER_ABSENT) {
3087 v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
3088 v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
3089 v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
3090 v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
3093 if (strcmp(type_name, "radio") == 0)
3094 return 0;
3096 if (strcmp(type_name, "video") == 0) {
3097 q = &btv->capq;
3098 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
3099 q->ops = &bttv_video_qops;
3100 } else if (strcmp(type_name, "vbi") == 0) {
3101 q = &btv->vbiq;
3102 q->type = V4L2_BUF_TYPE_VBI_CAPTURE;
3103 q->ops = &bttv_vbi_qops;
3104 } else {
3105 return -EINVAL;
3107 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
3108 q->io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ | VB2_DMABUF;
3109 q->mem_ops = &vb2_dma_sg_memops;
3110 q->drv_priv = btv;
3111 q->gfp_flags = __GFP_DMA32;
3112 q->buf_struct_size = sizeof(struct bttv_buffer);
3113 q->lock = &btv->lock;
3114 q->min_queued_buffers = 2;
3115 q->dev = &btv->c.pci->dev;
3116 err = vb2_queue_init(q);
3117 if (err)
3118 return err;
3119 vfd->queue = q;
3121 return 0;
3124 static void bttv_unregister_video(struct bttv *btv)
3126 video_unregister_device(&btv->video_dev);
3127 video_unregister_device(&btv->vbi_dev);
3128 video_unregister_device(&btv->radio_dev);
3131 /* register video4linux devices */
3132 static int bttv_register_video(struct bttv *btv)
3134 /* video */
3135 vdev_init(btv, &btv->video_dev, &bttv_video_template, "video");
3136 btv->video_dev.device_caps = V4L2_CAP_VIDEO_CAPTURE |
3137 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
3138 if (btv->tuner_type != TUNER_ABSENT)
3139 btv->video_dev.device_caps |= V4L2_CAP_TUNER;
3141 if (video_register_device(&btv->video_dev, VFL_TYPE_VIDEO,
3142 video_nr[btv->c.nr]) < 0)
3143 goto err;
3144 pr_info("%d: registered device %s\n",
3145 btv->c.nr, video_device_node_name(&btv->video_dev));
3146 if (device_create_file(&btv->video_dev.dev,
3147 &dev_attr_card)<0) {
3148 pr_err("%d: device_create_file 'card' failed\n", btv->c.nr);
3149 goto err;
3152 /* vbi */
3153 vdev_init(btv, &btv->vbi_dev, &bttv_video_template, "vbi");
3154 btv->vbi_dev.device_caps = V4L2_CAP_VBI_CAPTURE | V4L2_CAP_READWRITE |
3155 V4L2_CAP_STREAMING;
3156 if (btv->tuner_type != TUNER_ABSENT)
3157 btv->vbi_dev.device_caps |= V4L2_CAP_TUNER;
3159 if (video_register_device(&btv->vbi_dev, VFL_TYPE_VBI,
3160 vbi_nr[btv->c.nr]) < 0)
3161 goto err;
3162 pr_info("%d: registered device %s\n",
3163 btv->c.nr, video_device_node_name(&btv->vbi_dev));
3165 if (!btv->has_radio)
3166 return 0;
3167 /* radio */
3168 vdev_init(btv, &btv->radio_dev, &radio_template, "radio");
3169 btv->radio_dev.device_caps = V4L2_CAP_RADIO | V4L2_CAP_TUNER;
3170 if (btv->has_saa6588)
3171 btv->radio_dev.device_caps |= V4L2_CAP_READWRITE |
3172 V4L2_CAP_RDS_CAPTURE;
3173 if (btv->has_tea575x)
3174 btv->radio_dev.device_caps |= V4L2_CAP_HW_FREQ_SEEK;
3175 btv->radio_dev.ctrl_handler = &btv->radio_ctrl_handler;
3176 if (video_register_device(&btv->radio_dev, VFL_TYPE_RADIO,
3177 radio_nr[btv->c.nr]) < 0)
3178 goto err;
3179 pr_info("%d: registered device %s\n",
3180 btv->c.nr, video_device_node_name(&btv->radio_dev));
3182 /* all done */
3183 return 0;
3185 err:
3186 bttv_unregister_video(btv);
3187 return -1;
3191 /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
3192 /* response on cards with no firmware is not enabled by OF */
3193 static void pci_set_command(struct pci_dev *dev)
3195 #if defined(__powerpc__)
3196 unsigned int cmd;
3198 pci_read_config_dword(dev, PCI_COMMAND, &cmd);
3199 cmd = (cmd | PCI_COMMAND_MEMORY );
3200 pci_write_config_dword(dev, PCI_COMMAND, cmd);
3201 #endif
3204 static int bttv_probe(struct pci_dev *dev, const struct pci_device_id *pci_id)
3206 struct v4l2_frequency init_freq = {
3207 .tuner = 0,
3208 .type = V4L2_TUNER_ANALOG_TV,
3209 .frequency = 980,
3211 int result;
3212 unsigned char lat;
3213 struct bttv *btv;
3214 struct v4l2_ctrl_handler *hdl;
3216 if (bttv_num == BTTV_MAX)
3217 return -ENOMEM;
3218 pr_info("Bt8xx card found (%d)\n", bttv_num);
3219 bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
3220 if (btv == NULL) {
3221 pr_err("out of memory\n");
3222 return -ENOMEM;
3224 btv->c.nr = bttv_num;
3225 snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
3226 "bttv%d", btv->c.nr);
3228 /* initialize structs / fill in defaults */
3229 mutex_init(&btv->lock);
3230 spin_lock_init(&btv->s_lock);
3231 spin_lock_init(&btv->gpio_lock);
3232 init_waitqueue_head(&btv->i2c_queue);
3233 INIT_LIST_HEAD(&btv->c.subs);
3234 INIT_LIST_HEAD(&btv->capture);
3235 INIT_LIST_HEAD(&btv->vcapture);
3237 timer_setup(&btv->timeout, bttv_irq_timeout, 0);
3239 btv->i2c_rc = -1;
3240 btv->tuner_type = UNSET;
3241 btv->new_input = UNSET;
3242 btv->has_radio=radio[btv->c.nr];
3244 /* pci stuff (init, get irq/mmio, ... */
3245 btv->c.pci = dev;
3246 btv->id = dev->device;
3247 if (pci_enable_device(dev)) {
3248 pr_warn("%d: Can't enable device\n", btv->c.nr);
3249 result = -EIO;
3250 goto free_mem;
3252 if (dma_set_mask(&dev->dev, DMA_BIT_MASK(32))) {
3253 pr_warn("%d: No suitable DMA available\n", btv->c.nr);
3254 result = -EIO;
3255 goto free_mem;
3257 if (!request_mem_region(pci_resource_start(dev,0),
3258 pci_resource_len(dev,0),
3259 btv->c.v4l2_dev.name)) {
3260 pr_warn("%d: can't request iomem (0x%llx)\n",
3261 btv->c.nr,
3262 (unsigned long long)pci_resource_start(dev, 0));
3263 result = -EBUSY;
3264 goto free_mem;
3266 pci_set_master(dev);
3267 pci_set_command(dev);
3269 result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
3270 if (result < 0) {
3271 pr_warn("%d: v4l2_device_register() failed\n", btv->c.nr);
3272 goto fail0;
3274 hdl = &btv->ctrl_handler;
3275 v4l2_ctrl_handler_init(hdl, 20);
3276 btv->c.v4l2_dev.ctrl_handler = hdl;
3277 v4l2_ctrl_handler_init(&btv->radio_ctrl_handler, 6);
3279 btv->revision = dev->revision;
3280 pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
3281 pr_info("%d: Bt%d (rev %d) at %s, irq: %d, latency: %d, mmio: 0x%llx\n",
3282 bttv_num, btv->id, btv->revision, pci_name(dev),
3283 btv->c.pci->irq, lat,
3284 (unsigned long long)pci_resource_start(dev, 0));
3285 schedule();
3287 btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
3288 if (NULL == btv->bt848_mmio) {
3289 pr_err("%d: ioremap() failed\n", btv->c.nr);
3290 result = -EIO;
3291 goto fail1;
3294 /* identify card */
3295 bttv_idcard(btv);
3297 /* disable irqs, register irq handler */
3298 btwrite(0, BT848_INT_MASK);
3299 result = request_irq(btv->c.pci->irq, bttv_irq,
3300 IRQF_SHARED, btv->c.v4l2_dev.name, (void *)btv);
3301 if (result < 0) {
3302 pr_err("%d: can't get IRQ %d\n",
3303 bttv_num, btv->c.pci->irq);
3304 goto fail1;
3307 if (0 != bttv_handle_chipset(btv)) {
3308 result = -EIO;
3309 goto fail2;
3312 /* init options from insmod args */
3313 btv->opt_combfilter = combfilter;
3314 bttv_ctrl_combfilter.def = combfilter;
3315 bttv_ctrl_lumafilter.def = lumafilter;
3316 btv->opt_automute = automute;
3317 bttv_ctrl_automute.def = automute;
3318 bttv_ctrl_agc_crush.def = agc_crush;
3319 btv->opt_vcr_hack = vcr_hack;
3320 bttv_ctrl_vcr_hack.def = vcr_hack;
3321 bttv_ctrl_whitecrush_upper.def = whitecrush_upper;
3322 bttv_ctrl_whitecrush_lower.def = whitecrush_lower;
3323 btv->opt_uv_ratio = uv_ratio;
3324 bttv_ctrl_uv_ratio.def = uv_ratio;
3325 bttv_ctrl_full_luma.def = full_luma_range;
3326 bttv_ctrl_coring.def = coring;
3328 /* fill struct bttv with some useful defaults */
3329 btv->fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
3330 btv->width = 320;
3331 btv->height = 240;
3332 btv->field = V4L2_FIELD_INTERLACED;
3333 btv->input = 0;
3334 btv->tvnorm = 0; /* Index into bttv_tvnorms[] i.e. PAL. */
3335 bttv_vbi_fmt_reset(&btv->vbi_fmt, btv->tvnorm);
3336 btv->vbi_count[0] = VBI_DEFLINES;
3337 btv->vbi_count[1] = VBI_DEFLINES;
3338 btv->do_crop = 0;
3340 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3341 V4L2_CID_BRIGHTNESS, 0, 0xff00, 0x100, 32768);
3342 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3343 V4L2_CID_CONTRAST, 0, 0xff80, 0x80, 0x6c00);
3344 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3345 V4L2_CID_SATURATION, 0, 0xff80, 0x80, 32768);
3346 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3347 V4L2_CID_COLOR_KILLER, 0, 1, 1, 0);
3348 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3349 V4L2_CID_HUE, 0, 0xff00, 0x100, 32768);
3350 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3351 V4L2_CID_CHROMA_AGC, 0, 1, 1, !!chroma_agc);
3352 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3353 V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
3354 if (btv->volume_gpio)
3355 v4l2_ctrl_new_std(hdl, &bttv_ctrl_ops,
3356 V4L2_CID_AUDIO_VOLUME, 0, 0xff00, 0x100, 0xff00);
3357 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_combfilter, NULL);
3358 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_automute, NULL);
3359 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_lumafilter, NULL);
3360 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_agc_crush, NULL);
3361 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_vcr_hack, NULL);
3362 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_lower, NULL);
3363 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_whitecrush_upper, NULL);
3364 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_uv_ratio, NULL);
3365 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_full_luma, NULL);
3366 v4l2_ctrl_new_custom(hdl, &bttv_ctrl_coring, NULL);
3368 /* initialize hardware */
3369 if (bttv_gpio)
3370 bttv_gpio_tracking(btv,"pre-init");
3372 bttv_risc_init_main(btv);
3373 init_bt848(btv);
3375 /* gpio */
3376 btwrite(0x00, BT848_GPIO_REG_INP);
3377 btwrite(0x00, BT848_GPIO_OUT_EN);
3378 if (bttv_verbose)
3379 bttv_gpio_tracking(btv,"init");
3381 /* needs to be done before i2c is registered */
3382 bttv_init_card1(btv);
3384 /* register i2c + gpio */
3385 init_bttv_i2c(btv);
3387 /* some card-specific stuff (needs working i2c) */
3388 bttv_init_card2(btv);
3389 bttv_init_tuner(btv);
3390 if (btv->tuner_type != TUNER_ABSENT) {
3391 bttv_set_frequency(btv, &init_freq);
3392 btv->radio_freq = 90500 * 16; /* 90.5Mhz default */
3394 btv->std = V4L2_STD_PAL;
3395 init_irqreg(btv);
3396 if (!bttv_tvcards[btv->c.type].no_video)
3397 v4l2_ctrl_handler_setup(hdl);
3398 if (hdl->error) {
3399 result = hdl->error;
3400 goto fail2;
3402 /* mute device */
3403 audio_mute(btv, 1);
3405 /* register video4linux + input */
3406 if (!bttv_tvcards[btv->c.type].no_video) {
3407 v4l2_ctrl_add_handler(&btv->radio_ctrl_handler, hdl,
3408 v4l2_ctrl_radio_filter, false);
3409 if (btv->radio_ctrl_handler.error) {
3410 result = btv->radio_ctrl_handler.error;
3411 goto fail2;
3413 set_input(btv, btv->input, btv->tvnorm);
3414 bttv_crop_reset(&btv->crop[0], btv->tvnorm);
3415 btv->crop[1] = btv->crop[0]; /* current = default */
3416 disclaim_vbi_lines(btv);
3417 disclaim_video_lines(btv);
3418 bttv_register_video(btv);
3421 /* add subdevices and autoload dvb-bt8xx if needed */
3422 if (bttv_tvcards[btv->c.type].has_dvb) {
3423 bttv_sub_add_device(&btv->c, "dvb");
3424 request_modules(btv);
3427 if (!disable_ir) {
3428 init_bttv_i2c_ir(btv);
3429 bttv_input_init(btv);
3432 /* everything is fine */
3433 bttv_num++;
3434 return 0;
3436 fail2:
3437 free_irq(btv->c.pci->irq,btv);
3439 fail1:
3440 v4l2_ctrl_handler_free(&btv->ctrl_handler);
3441 v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
3442 v4l2_device_unregister(&btv->c.v4l2_dev);
3444 fail0:
3445 if (btv->bt848_mmio)
3446 iounmap(btv->bt848_mmio);
3447 release_mem_region(pci_resource_start(btv->c.pci,0),
3448 pci_resource_len(btv->c.pci,0));
3449 pci_disable_device(btv->c.pci);
3451 free_mem:
3452 bttvs[btv->c.nr] = NULL;
3453 kfree(btv);
3454 return result;
3457 static void bttv_remove(struct pci_dev *pci_dev)
3459 struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
3460 struct bttv *btv = to_bttv(v4l2_dev);
3462 if (bttv_verbose)
3463 pr_info("%d: unloading\n", btv->c.nr);
3465 if (bttv_tvcards[btv->c.type].has_dvb)
3466 flush_request_modules(btv);
3468 /* shutdown everything (DMA+IRQs) */
3469 btand(~15, BT848_GPIO_DMA_CTL);
3470 btwrite(0, BT848_INT_MASK);
3471 btwrite(~0x0, BT848_INT_STAT);
3472 btwrite(0x0, BT848_GPIO_OUT_EN);
3473 if (bttv_gpio)
3474 bttv_gpio_tracking(btv,"cleanup");
3476 /* tell gpio modules we are leaving ... */
3477 btv->shutdown=1;
3478 bttv_input_fini(btv);
3479 bttv_sub_del_devices(&btv->c);
3481 /* unregister i2c_bus + input */
3482 fini_bttv_i2c(btv);
3484 /* unregister video4linux */
3485 bttv_unregister_video(btv);
3487 /* free allocated memory */
3488 v4l2_ctrl_handler_free(&btv->ctrl_handler);
3489 v4l2_ctrl_handler_free(&btv->radio_ctrl_handler);
3490 btcx_riscmem_free(btv->c.pci,&btv->main);
3492 /* free resources */
3493 free_irq(btv->c.pci->irq,btv);
3494 del_timer_sync(&btv->timeout);
3495 iounmap(btv->bt848_mmio);
3496 release_mem_region(pci_resource_start(btv->c.pci,0),
3497 pci_resource_len(btv->c.pci,0));
3498 pci_disable_device(btv->c.pci);
3500 v4l2_device_unregister(&btv->c.v4l2_dev);
3501 bttvs[btv->c.nr] = NULL;
3502 kfree(btv);
3504 return;
3507 static int __maybe_unused bttv_suspend(struct device *dev)
3509 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
3510 struct bttv *btv = to_bttv(v4l2_dev);
3511 struct bttv_buffer_set idle;
3512 unsigned long flags;
3514 dprintk("%d: suspend\n", btv->c.nr);
3516 /* stop dma + irqs */
3517 spin_lock_irqsave(&btv->s_lock,flags);
3518 memset(&idle, 0, sizeof(idle));
3519 btv->state.video = btv->curr;
3520 btv->state.vbi = btv->cvbi;
3521 btv->state.loop_irq = btv->loop_irq;
3522 btv->curr = idle;
3523 btv->loop_irq = 0;
3524 bttv_buffer_activate_video(btv, &idle);
3525 bttv_buffer_activate_vbi(btv, NULL);
3526 bttv_set_dma(btv, 0);
3527 btwrite(0, BT848_INT_MASK);
3528 spin_unlock_irqrestore(&btv->s_lock,flags);
3530 /* save bt878 state */
3531 btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
3532 btv->state.gpio_data = gpio_read();
3534 btv->state.disabled = 1;
3535 return 0;
3538 static int __maybe_unused bttv_resume(struct device *dev)
3540 struct v4l2_device *v4l2_dev = dev_get_drvdata(dev);
3541 struct bttv *btv = to_bttv(v4l2_dev);
3542 unsigned long flags;
3544 dprintk("%d: resume\n", btv->c.nr);
3546 btv->state.disabled = 0;
3548 /* restore bt878 state */
3549 bttv_reinit_bt848(btv);
3550 gpio_inout(0xffffff, btv->state.gpio_enable);
3551 gpio_write(btv->state.gpio_data);
3553 /* restart dma */
3554 spin_lock_irqsave(&btv->s_lock,flags);
3555 btv->curr = btv->state.video;
3556 btv->cvbi = btv->state.vbi;
3557 btv->loop_irq = btv->state.loop_irq;
3558 bttv_buffer_activate_video(btv, &btv->curr);
3559 bttv_buffer_activate_vbi(btv, btv->cvbi);
3560 bttv_set_dma(btv, 0);
3561 spin_unlock_irqrestore(&btv->s_lock,flags);
3562 return 0;
3565 static const struct pci_device_id bttv_pci_tbl[] = {
3566 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
3567 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
3568 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
3569 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
3570 {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_FUSION879), 0},
3571 {0,}
3574 MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
3576 static SIMPLE_DEV_PM_OPS(bttv_pm_ops,
3577 bttv_suspend,
3578 bttv_resume);
3580 static struct pci_driver bttv_pci_driver = {
3581 .name = "bttv",
3582 .id_table = bttv_pci_tbl,
3583 .probe = bttv_probe,
3584 .remove = bttv_remove,
3585 .driver.pm = &bttv_pm_ops,
3588 static int __init bttv_init_module(void)
3590 int ret;
3592 bttv_num = 0;
3594 pr_info("driver version %s loaded\n", BTTV_VERSION);
3595 if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
3596 gbuffers = 2;
3597 if (gbufsize > BTTV_MAX_FBUF)
3598 gbufsize = BTTV_MAX_FBUF;
3599 gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
3600 if (bttv_verbose)
3601 pr_info("using %d buffers with %dk (%d pages) each for capture\n",
3602 gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
3604 bttv_check_chipset();
3606 ret = bus_register(&bttv_sub_bus_type);
3607 if (ret < 0) {
3608 pr_warn("bus_register error: %d\n", ret);
3609 return ret;
3611 ret = pci_register_driver(&bttv_pci_driver);
3612 if (ret < 0)
3613 bus_unregister(&bttv_sub_bus_type);
3615 return ret;
3618 static void __exit bttv_cleanup_module(void)
3620 pci_unregister_driver(&bttv_pci_driver);
3621 bus_unregister(&bttv_sub_bus_type);
3624 module_init(bttv_init_module);
3625 module_exit(bttv_cleanup_module);