1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Zoran zr36057/zr36067 PCI controller driver, for the
4 * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
5 * Media Labs LML33/LML33R10.
7 * This part handles card-specific data and detection
9 * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
12 #include <linux/delay.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
17 #include <linux/i2c.h>
18 #include <linux/i2c-algo-bit.h>
19 #include <linux/videodev2.h>
20 #include <linux/spinlock.h>
22 #include <linux/pci.h>
23 #include <linux/interrupt.h>
25 #include <media/v4l2-common.h>
26 #include <media/i2c/bt819.h>
28 #include "videocodec.h"
30 #include "zoran_card.h"
31 #include "zoran_device.h"
36 extern const struct zoran_format zoran_formats[];
38 static int card[BUZ_MAX] = { [0 ... (BUZ_MAX - 1)] = -1 };
39 module_param_array(card, int, NULL, 0444);
40 MODULE_PARM_DESC(card, "Card type");
42 /* Default input and video norm at startup of the driver. */
44 static unsigned int default_input; /* default 0 = Composite, 1 = S-Video */
45 module_param(default_input, uint, 0444);
46 MODULE_PARM_DESC(default_input,
47 "Default input (0=Composite, 1=S-Video, 2=Internal)");
49 static int default_mux = 1; /* 6 Eyes input selection */
50 module_param(default_mux, int, 0644);
51 MODULE_PARM_DESC(default_mux,
52 "Default 6 Eyes mux setting (Input selection)");
54 static int default_norm; /* default 0 = PAL, 1 = NTSC 2 = SECAM */
55 module_param(default_norm, int, 0444);
56 MODULE_PARM_DESC(default_norm, "Default norm (0=PAL, 1=NTSC, 2=SECAM)");
58 /* /dev/videoN, -1 for autodetect */
59 static int video_nr[BUZ_MAX] = { [0 ... (BUZ_MAX - 1)] = -1 };
60 module_param_array(video_nr, int, NULL, 0444);
61 MODULE_PARM_DESC(video_nr, "Video device number (-1=Auto)");
63 /* 1=Pass through TV signal when device is not used */
64 /* 0=Show color bar when device is not used (LML33: only if lml33dpath=1) */
66 module_param(pass_through, int, 0644);
67 MODULE_PARM_DESC(pass_through,
68 "Pass TV signal through to TV-out when idling");
70 int zr36067_debug = 1;
71 module_param_named(debug, zr36067_debug, int, 0644);
72 MODULE_PARM_DESC(debug, "Debug level (0-5)");
74 #define ZORAN_VERSION "0.10.1"
76 MODULE_DESCRIPTION("Zoran-36057/36067 JPEG codec driver");
77 MODULE_AUTHOR("Serguei Miridonov");
78 MODULE_LICENSE("GPL");
79 MODULE_VERSION(ZORAN_VERSION);
81 #define ZR_DEVICE(subven, subdev, data) { \
82 .vendor = PCI_VENDOR_ID_ZORAN, .device = PCI_DEVICE_ID_ZORAN_36057, \
83 .subvendor = (subven), .subdevice = (subdev), .driver_data = (data) }
85 static const struct pci_device_id zr36067_pci_tbl[] = {
86 ZR_DEVICE(PCI_VENDOR_ID_MIRO, PCI_DEVICE_ID_MIRO_DC10PLUS, DC10_PLUS),
87 ZR_DEVICE(PCI_VENDOR_ID_MIRO, PCI_DEVICE_ID_MIRO_DC30PLUS, DC30_PLUS),
88 ZR_DEVICE(PCI_VENDOR_ID_ELECTRONICDESIGNGMBH, PCI_DEVICE_ID_LML_33R10, LML33R10),
89 ZR_DEVICE(PCI_VENDOR_ID_IOMEGA, PCI_DEVICE_ID_IOMEGA_BUZ, BUZ),
90 ZR_DEVICE(PCI_ANY_ID, PCI_ANY_ID, NUM_CARDS),
93 MODULE_DEVICE_TABLE(pci, zr36067_pci_tbl);
95 static unsigned int zoran_num; /* number of cards found */
97 /* videocodec bus functions ZR36060 */
98 static u32 zr36060_read(struct videocodec *codec, u16 reg)
100 struct zoran *zr = (struct zoran *)codec->master_data->data;
103 if (post_office_wait(zr) || post_office_write(zr, 0, 1, reg >> 8) ||
104 post_office_write(zr, 0, 2, reg & 0xff))
107 data = post_office_read(zr, 0, 3) & 0xff;
111 static void zr36060_write(struct videocodec *codec, u16 reg, u32 val)
113 struct zoran *zr = (struct zoran *)codec->master_data->data;
115 if (post_office_wait(zr) || post_office_write(zr, 0, 1, reg >> 8) ||
116 post_office_write(zr, 0, 2, reg & 0xff))
119 post_office_write(zr, 0, 3, val & 0xff);
122 /* videocodec bus functions ZR36050 */
123 static u32 zr36050_read(struct videocodec *codec, u16 reg)
125 struct zoran *zr = (struct zoran *)codec->master_data->data;
128 if (post_office_wait(zr) || post_office_write(zr, 1, 0, reg >> 2)) // reg. HIGHBYTES
131 data = post_office_read(zr, 0, reg & 0x03) & 0xff; // reg. LOWBYTES + read
135 static void zr36050_write(struct videocodec *codec, u16 reg, u32 val)
137 struct zoran *zr = (struct zoran *)codec->master_data->data;
139 if (post_office_wait(zr) || post_office_write(zr, 1, 0, reg >> 2)) // reg. HIGHBYTES
142 post_office_write(zr, 0, reg & 0x03, val & 0xff); // reg. LOWBYTES + wr. data
145 /* videocodec bus functions ZR36016 */
146 static u32 zr36016_read(struct videocodec *codec, u16 reg)
148 struct zoran *zr = (struct zoran *)codec->master_data->data;
151 if (post_office_wait(zr))
154 data = post_office_read(zr, 2, reg & 0x03) & 0xff; // read
158 /* hack for in zoran_device.c */
159 void zr36016_write(struct videocodec *codec, u16 reg, u32 val)
161 struct zoran *zr = (struct zoran *)codec->master_data->data;
163 if (post_office_wait(zr))
166 post_office_write(zr, 2, reg & 0x03, val & 0x0ff); // wr. data
170 * Board specific information
173 static void dc10_init(struct zoran *zr)
175 pci_dbg(zr->pci_dev, "%s\n", __func__);
177 /* Pixel clock selection */
180 /* Enable the video bus sync signals */
184 static void dc10plus_init(struct zoran *zr)
186 pci_dbg(zr->pci_dev, "%s\n", __func__);
189 static void buz_init(struct zoran *zr)
191 pci_dbg(zr->pci_dev, "%s\n", __func__);
193 /* some stuff from Iomega */
194 pci_write_config_dword(zr->pci_dev, 0xfc, 0x90680f15);
195 pci_write_config_dword(zr->pci_dev, 0x0c, 0x00012020);
196 pci_write_config_dword(zr->pci_dev, 0xe8, 0xc0200000);
199 static void lml33_init(struct zoran *zr)
201 pci_dbg(zr->pci_dev, "%s\n", __func__);
203 GPIO(zr, 2, 1); // Set Composite input/output
206 static void avs6eyes_init(struct zoran *zr)
208 // AverMedia 6-Eyes original driver by Christer Weinigel
210 // Lifted straight from Christer's old driver and
211 // modified slightly by Martin Samuelsson.
213 int mux = default_mux; /* 1 = BT866, 7 = VID1 */
215 GPIO(zr, 4, 1); /* Bt866 SLEEP on */
218 GPIO(zr, 0, 1); /* ZR36060 /RESET on */
219 GPIO(zr, 1, 0); /* ZR36060 /SLEEP on */
220 GPIO(zr, 2, mux & 1); /* MUX S0 */
221 GPIO(zr, 3, 0); /* /FRAME on */
222 GPIO(zr, 4, 0); /* Bt866 SLEEP off */
223 GPIO(zr, 5, mux & 2); /* MUX S1 */
224 GPIO(zr, 6, 0); /* ? */
225 GPIO(zr, 7, mux & 4); /* MUX S2 */
228 static const char *codecid_to_modulename(u16 codecid)
230 const char *name = NULL;
233 case CODEC_TYPE_ZR36060:
236 case CODEC_TYPE_ZR36050:
239 case CODEC_TYPE_ZR36016:
247 static int codec_init(struct zoran *zr, u16 codecid)
250 case CODEC_TYPE_ZR36060:
251 #ifdef CONFIG_VIDEO_ZORAN_ZR36060
252 return zr36060_init_module();
254 pci_err(zr->pci_dev, "ZR36060 support is not enabled\n");
258 case CODEC_TYPE_ZR36050:
259 #ifdef CONFIG_VIDEO_ZORAN_DC30
260 return zr36050_init_module();
262 pci_err(zr->pci_dev, "ZR36050 support is not enabled\n");
266 case CODEC_TYPE_ZR36016:
267 #ifdef CONFIG_VIDEO_ZORAN_DC30
268 return zr36016_init_module();
270 pci_err(zr->pci_dev, "ZR36016 support is not enabled\n");
276 pci_err(zr->pci_dev, "unknown codec id %x\n", codecid);
280 static void codec_exit(struct zoran *zr, u16 codecid)
283 case CODEC_TYPE_ZR36060:
284 #ifdef CONFIG_VIDEO_ZORAN_ZR36060
285 zr36060_cleanup_module();
288 case CODEC_TYPE_ZR36050:
289 #ifdef CONFIG_VIDEO_ZORAN_DC30
290 zr36050_cleanup_module();
293 case CODEC_TYPE_ZR36016:
294 #ifdef CONFIG_VIDEO_ZORAN_DC30
295 zr36016_cleanup_module();
301 static int videocodec_init(struct zoran *zr)
303 const char *codec_name, *vfe_name;
306 codec_name = codecid_to_modulename(zr->card.video_codec);
308 result = codec_init(zr, zr->card.video_codec);
310 pci_err(zr->pci_dev, "failed to load video codec %s: %d\n",
315 vfe_name = codecid_to_modulename(zr->card.video_vfe);
317 result = codec_init(zr, zr->card.video_vfe);
319 pci_err(zr->pci_dev, "failed to load video vfe %s: %d\n",
322 codec_exit(zr, zr->card.video_codec);
329 static void videocodec_exit(struct zoran *zr)
331 if (zr->card.video_codec != CODEC_TYPE_NONE)
332 codec_exit(zr, zr->card.video_codec);
333 if (zr->card.video_vfe != CODEC_TYPE_NONE)
334 codec_exit(zr, zr->card.video_vfe);
338 // u16 wt, wa, h_start, h_sync_start, ht, ha, v_start;
341 static const struct tvnorm f50sqpixel = { 944, 768, 83, 880, 625, 576, 16 };
342 static const struct tvnorm f60sqpixel = { 780, 640, 51, 716, 525, 480, 12 };
343 static const struct tvnorm f50ccir601 = { 864, 720, 75, 804, 625, 576, 18 };
344 static const struct tvnorm f60ccir601 = { 858, 720, 57, 788, 525, 480, 16 };
346 static const struct tvnorm f50ccir601_lml33 = { 864, 720, 75 + 34, 804, 625, 576, 18 };
347 static const struct tvnorm f60ccir601_lml33 = { 858, 720, 57 + 34, 788, 525, 480, 16 };
349 /* The DC10 (57/16/50) uses VActive as HSync, so h_start must be 0 */
350 static const struct tvnorm f50sqpixel_dc10 = { 944, 768, 0, 880, 625, 576, 0 };
351 static const struct tvnorm f60sqpixel_dc10 = { 780, 640, 0, 716, 525, 480, 12 };
354 * FIXME: I cannot swap U and V in saa7114, so i do one pixel left shift in zoran (75 -> 74)
355 * (Maxim Yevtyushkin <max@linuxmedialabs.com>)
357 static const struct tvnorm f50ccir601_lm33r10 = { 864, 720, 74 + 54, 804, 625, 576, 18 };
358 static const struct tvnorm f60ccir601_lm33r10 = { 858, 720, 56 + 54, 788, 525, 480, 16 };
361 * FIXME: The ks0127 seem incapable of swapping U and V, too, which is why I copy Maxim's left
362 * shift hack for the 6 Eyes.
364 * Christer's driver used the unshifted norms, though...
367 static const struct tvnorm f50ccir601_avs6eyes = { 864, 720, 74, 804, 625, 576, 18 };
368 static const struct tvnorm f60ccir601_avs6eyes = { 858, 720, 56, 788, 525, 480, 16 };
370 static const unsigned short vpx3220_addrs[] = { 0x43, 0x47, I2C_CLIENT_END };
371 static const unsigned short saa7110_addrs[] = { 0x4e, 0x4f, I2C_CLIENT_END };
372 static const unsigned short saa7111_addrs[] = { 0x25, 0x24, I2C_CLIENT_END };
373 static const unsigned short saa7114_addrs[] = { 0x21, 0x20, I2C_CLIENT_END };
374 static const unsigned short adv717x_addrs[] = { 0x6a, 0x6b, 0x2a, 0x2b, I2C_CLIENT_END };
375 static const unsigned short ks0127_addrs[] = { 0x6c, 0x6d, I2C_CLIENT_END };
376 static const unsigned short saa7185_addrs[] = { 0x44, I2C_CLIENT_END };
377 static const unsigned short bt819_addrs[] = { 0x45, I2C_CLIENT_END };
378 static const unsigned short bt856_addrs[] = { 0x44, I2C_CLIENT_END };
379 static const unsigned short bt866_addrs[] = { 0x44, I2C_CLIENT_END };
381 static struct card_info zoran_cards[NUM_CARDS] = {
385 .i2c_decoder = "vpx3220a",
386 .addrs_decoder = vpx3220_addrs,
387 .video_codec = CODEC_TYPE_ZR36050,
388 .video_vfe = CODEC_TYPE_ZR36016,
394 { 0, "Internal/comp" }
396 .norms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
403 .vsync_int = ZR36057_ISR_GIRQ1,
404 .gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
405 .gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
407 .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
408 .gws_not_connected = 0,
414 .i2c_decoder = "saa7110",
415 .addrs_decoder = saa7110_addrs,
416 .i2c_encoder = "adv7175",
417 .addrs_encoder = adv717x_addrs,
418 .video_codec = CODEC_TYPE_ZR36060,
424 { 5, "Internal/comp" }
426 .norms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
431 .jpeg_int = ZR36057_ISR_GIRQ0,
432 .vsync_int = ZR36057_ISR_GIRQ1,
433 .gpio = { 3, 0, 6, 1, 2, -1, 4, 5 },
434 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
436 .vfe_pol = { 1, 1, 1, 1, 0, 0, 0, 0 },
437 .gws_not_connected = 0,
439 .init = &dc10plus_init,
443 .i2c_decoder = "saa7110",
444 .addrs_decoder = saa7110_addrs,
445 .i2c_encoder = "adv7175",
446 .addrs_encoder = adv717x_addrs,
447 .video_codec = CODEC_TYPE_ZR36060,
453 { 5, "Internal/comp" }
455 .norms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
461 .jpeg_int = ZR36057_ISR_GIRQ0,
462 .vsync_int = ZR36057_ISR_GIRQ1,
463 .gpio = { 3, 0, 6, 1, 2, -1, 4, 5 },
464 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
466 .vfe_pol = { 1, 1, 1, 1, 0, 0, 0, 0 },
467 .gws_not_connected = 0,
469 .init = &dc10plus_init,
473 .i2c_decoder = "vpx3220a",
474 .addrs_decoder = vpx3220_addrs,
475 .i2c_encoder = "adv7175",
476 .addrs_encoder = adv717x_addrs,
477 .video_codec = CODEC_TYPE_ZR36050,
478 .video_vfe = CODEC_TYPE_ZR36016,
484 { 0, "Internal/comp" }
486 .norms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
493 .vsync_int = ZR36057_ISR_GIRQ1,
494 .gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
495 .gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
497 .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
498 .gws_not_connected = 0,
504 .i2c_decoder = "vpx3220a",
505 .addrs_decoder = vpx3220_addrs,
506 .i2c_encoder = "adv7175",
507 .addrs_encoder = adv717x_addrs,
508 .video_codec = CODEC_TYPE_ZR36050,
509 .video_vfe = CODEC_TYPE_ZR36016,
515 { 0, "Internal/comp" }
517 .norms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
524 .vsync_int = ZR36057_ISR_GIRQ1,
525 .gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
526 .gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
528 .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
529 .gws_not_connected = 0,
535 .i2c_decoder = "bt819a",
536 .addrs_decoder = bt819_addrs,
537 .i2c_encoder = "bt856",
538 .addrs_encoder = bt856_addrs,
539 .video_codec = CODEC_TYPE_ZR36060,
546 .norms = V4L2_STD_NTSC | V4L2_STD_PAL,
552 .jpeg_int = ZR36057_ISR_GIRQ1,
553 .vsync_int = ZR36057_ISR_GIRQ0,
554 .gpio = { 1, -1, 3, 5, 7, -1, -1, -1 },
555 .gpio_pol = { 0, 0, 0, 0, 1, 0, 0, 0 },
557 .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
558 .gws_not_connected = 1,
564 .i2c_decoder = "saa7114",
565 .addrs_decoder = saa7114_addrs,
566 .i2c_encoder = "adv7170",
567 .addrs_encoder = adv717x_addrs,
568 .video_codec = CODEC_TYPE_ZR36060,
575 .norms = V4L2_STD_NTSC | V4L2_STD_PAL,
581 .jpeg_int = ZR36057_ISR_GIRQ1,
582 .vsync_int = ZR36057_ISR_GIRQ0,
583 .gpio = { 1, -1, 3, 5, 7, -1, -1, -1 },
584 .gpio_pol = { 0, 0, 0, 0, 1, 0, 0, 0 },
586 .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
587 .gws_not_connected = 1,
593 .i2c_decoder = "saa7111",
594 .addrs_decoder = saa7111_addrs,
595 .i2c_encoder = "saa7185",
596 .addrs_encoder = saa7185_addrs,
597 .video_codec = CODEC_TYPE_ZR36060,
604 .norms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
610 .jpeg_int = ZR36057_ISR_GIRQ1,
611 .vsync_int = ZR36057_ISR_GIRQ0,
612 .gpio = { 1, -1, 3, -1, -1, -1, -1, -1 },
613 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
615 .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
616 .gws_not_connected = 1,
622 /* AverMedia chose not to brand the 6-Eyes. Thus it can't be autodetected, and requires card=x. */
623 .i2c_decoder = "ks0127",
624 .addrs_decoder = ks0127_addrs,
625 .i2c_encoder = "bt866",
626 .addrs_encoder = bt866_addrs,
627 .video_codec = CODEC_TYPE_ZR36060,
631 { 0, "Composite 1" },
632 { 1, "Composite 2" },
633 { 2, "Composite 3" },
634 { 4, "Composite 4" },
635 { 5, "Composite 5" },
636 { 6, "Composite 6" },
642 .norms = V4L2_STD_NTSC | V4L2_STD_PAL,
644 &f50ccir601_avs6eyes,
645 &f60ccir601_avs6eyes,
648 .jpeg_int = ZR36057_ISR_GIRQ1,
649 .vsync_int = ZR36057_ISR_GIRQ0,
650 .gpio = { 1, 0, 3, -1, -1, -1, -1, -1 },// Validity unknown /Sam
651 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 }, // Validity unknown /Sam
652 .gpcs = { 3, 1 }, // Validity unknown /Sam
653 .vfe_pol = { 1, 0, 0, 0, 0, 1, 0, 0 }, // Validity unknown /Sam
654 .gws_not_connected = 1,
656 .init = &avs6eyes_init,
664 /* software I2C functions */
665 static int zoran_i2c_getsda(void *data)
667 struct zoran *zr = (struct zoran *)data;
669 return (btread(ZR36057_I2CBR) >> 1) & 1;
672 static int zoran_i2c_getscl(void *data)
674 struct zoran *zr = (struct zoran *)data;
676 return btread(ZR36057_I2CBR) & 1;
679 static void zoran_i2c_setsda(void *data, int state)
681 struct zoran *zr = (struct zoran *)data;
687 btwrite(zr->i2cbr, ZR36057_I2CBR);
690 static void zoran_i2c_setscl(void *data, int state)
692 struct zoran *zr = (struct zoran *)data;
698 btwrite(zr->i2cbr, ZR36057_I2CBR);
701 static const struct i2c_algo_bit_data zoran_i2c_bit_data_template = {
702 .setsda = zoran_i2c_setsda,
703 .setscl = zoran_i2c_setscl,
704 .getsda = zoran_i2c_getsda,
705 .getscl = zoran_i2c_getscl,
710 static int zoran_register_i2c(struct zoran *zr)
712 zr->i2c_algo = zoran_i2c_bit_data_template;
713 zr->i2c_algo.data = zr;
714 strscpy(zr->i2c_adapter.name, ZR_DEVNAME(zr),
715 sizeof(zr->i2c_adapter.name));
716 i2c_set_adapdata(&zr->i2c_adapter, &zr->v4l2_dev);
717 zr->i2c_adapter.algo_data = &zr->i2c_algo;
718 zr->i2c_adapter.dev.parent = &zr->pci_dev->dev;
719 return i2c_bit_add_bus(&zr->i2c_adapter);
722 static void zoran_unregister_i2c(struct zoran *zr)
724 i2c_del_adapter(&zr->i2c_adapter);
727 /* Check a zoran_params struct for correctness, insert default params */
728 int zoran_check_jpg_settings(struct zoran *zr,
729 struct zoran_jpg_settings *settings, int try)
731 int err = 0, err0 = 0;
733 pci_dbg(zr->pci_dev, "%s - dec: %d, Hdcm: %d, Vdcm: %d, Tdcm: %d\n",
734 __func__, settings->decimation, settings->hor_dcm,
735 settings->ver_dcm, settings->tmp_dcm);
736 pci_dbg(zr->pci_dev, "%s - x: %d, y: %d, w: %d, y: %d\n", __func__,
737 settings->img_x, settings->img_y,
738 settings->img_width, settings->img_height);
739 /* Check decimation, set default values for decimation = 1, 2, 4 */
740 switch (settings->decimation) {
743 settings->hor_dcm = 1;
744 settings->ver_dcm = 1;
745 settings->tmp_dcm = 1;
746 settings->field_per_buff = 2;
749 settings->img_width = BUZ_MAX_WIDTH;
750 settings->img_height = BUZ_MAX_HEIGHT / 2;
754 settings->hor_dcm = 2;
755 settings->ver_dcm = 1;
756 settings->tmp_dcm = 2;
757 settings->field_per_buff = 1;
758 settings->img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
760 settings->img_width =
761 (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
762 settings->img_height = BUZ_MAX_HEIGHT / 2;
766 if (zr->card.type == DC10_NEW) {
767 pci_dbg(zr->pci_dev, "%s - HDec by 4 is not supported on the DC10\n", __func__);
772 settings->hor_dcm = 4;
773 settings->ver_dcm = 2;
774 settings->tmp_dcm = 2;
775 settings->field_per_buff = 1;
776 settings->img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
778 settings->img_width =
779 (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
780 settings->img_height = BUZ_MAX_HEIGHT / 2;
784 /* We have to check the data the user has set */
786 if (settings->hor_dcm != 1 && settings->hor_dcm != 2 &&
787 (zr->card.type == DC10_NEW || settings->hor_dcm != 4)) {
788 settings->hor_dcm = clamp(settings->hor_dcm, 1, 2);
791 if (settings->ver_dcm != 1 && settings->ver_dcm != 2) {
792 settings->ver_dcm = clamp(settings->ver_dcm, 1, 2);
795 if (settings->tmp_dcm != 1 && settings->tmp_dcm != 2) {
796 settings->tmp_dcm = clamp(settings->tmp_dcm, 1, 2);
799 if (settings->field_per_buff != 1 &&
800 settings->field_per_buff != 2) {
801 settings->field_per_buff = clamp(settings->field_per_buff, 1, 2);
804 if (settings->img_x < 0) {
808 if (settings->img_y < 0) {
812 if (settings->img_width < 0 || settings->img_width > BUZ_MAX_WIDTH) {
813 settings->img_width = clamp(settings->img_width, 0, (int)BUZ_MAX_WIDTH);
816 if (settings->img_height < 0 || settings->img_height > BUZ_MAX_HEIGHT / 2) {
817 settings->img_height = clamp(settings->img_height, 0, BUZ_MAX_HEIGHT / 2);
820 if (settings->img_x + settings->img_width > BUZ_MAX_WIDTH) {
821 settings->img_x = BUZ_MAX_WIDTH - settings->img_width;
824 if (settings->img_y + settings->img_height > BUZ_MAX_HEIGHT / 2) {
825 settings->img_y = BUZ_MAX_HEIGHT / 2 - settings->img_height;
828 if (settings->img_width % (16 * settings->hor_dcm) != 0) {
829 settings->img_width -= settings->img_width % (16 * settings->hor_dcm);
830 if (settings->img_width == 0)
831 settings->img_width = 16 * settings->hor_dcm;
834 if (settings->img_height % (8 * settings->ver_dcm) != 0) {
835 settings->img_height -= settings->img_height % (8 * settings->ver_dcm);
836 if (settings->img_height == 0)
837 settings->img_height = 8 * settings->ver_dcm;
842 pci_err(zr->pci_dev, "%s - error in params for decimation = 0\n", __func__);
847 pci_err(zr->pci_dev, "%s - decimation = %d, must be 0, 1, 2 or 4\n",
848 __func__, settings->decimation);
853 if (settings->jpg_comp.quality > 100)
854 settings->jpg_comp.quality = 100;
855 if (settings->jpg_comp.quality < 5)
856 settings->jpg_comp.quality = 5;
857 if (settings->jpg_comp.APPn < 0)
858 settings->jpg_comp.APPn = 0;
859 if (settings->jpg_comp.APPn > 15)
860 settings->jpg_comp.APPn = 15;
861 if (settings->jpg_comp.APP_len < 0)
862 settings->jpg_comp.APP_len = 0;
863 if (settings->jpg_comp.APP_len > 60)
864 settings->jpg_comp.APP_len = 60;
865 if (settings->jpg_comp.COM_len < 0)
866 settings->jpg_comp.COM_len = 0;
867 if (settings->jpg_comp.COM_len > 60)
868 settings->jpg_comp.COM_len = 60;
874 static int zoran_init_video_device(struct zoran *zr, struct video_device *video_dev, int dir)
878 /* Now add the template and register the device unit. */
879 *video_dev = zoran_template;
880 video_dev->v4l2_dev = &zr->v4l2_dev;
881 video_dev->lock = &zr->lock;
882 video_dev->device_caps = V4L2_CAP_STREAMING | dir;
884 strscpy(video_dev->name, ZR_DEVNAME(zr), sizeof(video_dev->name));
886 * It's not a mem2mem device, but you can both capture and output from one and the same
887 * device. This should really be split up into two device nodes, but that's a job for
890 video_dev->vfl_dir = VFL_DIR_M2M;
891 zoran_queue_init(zr, &zr->vq, V4L2_BUF_TYPE_VIDEO_CAPTURE);
893 err = video_register_device(video_dev, VFL_TYPE_VIDEO, video_nr[zr->id]);
896 video_set_drvdata(video_dev, zr);
900 static void zoran_exit_video_devices(struct zoran *zr)
902 video_unregister_device(zr->video_dev);
903 kfree(zr->video_dev);
906 static int zoran_init_video_devices(struct zoran *zr)
910 zr->video_dev = video_device_alloc();
914 err = zoran_init_video_device(zr, zr->video_dev, V4L2_CAP_VIDEO_CAPTURE);
916 kfree(zr->video_dev);
921 * v4l2_device_unregister() will care about removing zr->encoder/zr->decoder
922 * via v4l2_i2c_subdev_unregister()
924 static int zoran_i2c_init(struct zoran *zr)
928 pci_info(zr->pci_dev, "Initializing i2c bus...\n");
930 err = zoran_register_i2c(zr);
932 pci_err(zr->pci_dev, "%s - cannot initialize i2c bus\n", __func__);
936 zr->decoder = v4l2_i2c_new_subdev(&zr->v4l2_dev, &zr->i2c_adapter,
937 zr->card.i2c_decoder, 0,
938 zr->card.addrs_decoder);
940 pci_err(zr->pci_dev, "Fail to get decoder %s\n", zr->card.i2c_decoder);
945 if (zr->card.i2c_encoder) {
946 zr->encoder = v4l2_i2c_new_subdev(&zr->v4l2_dev, &zr->i2c_adapter,
947 zr->card.i2c_encoder, 0,
948 zr->card.addrs_encoder);
950 pci_err(zr->pci_dev, "Fail to get encoder %s\n", zr->card.i2c_encoder);
958 zoran_unregister_i2c(zr);
962 static void zoran_i2c_exit(struct zoran *zr)
964 zoran_unregister_i2c(zr);
967 void zoran_open_init_params(struct zoran *zr)
971 zr->v4l_settings.width = 192;
972 zr->v4l_settings.height = 144;
973 zr->v4l_settings.format = &zoran_formats[7]; /* YUY2 - YUV-4:2:2 packed */
974 zr->v4l_settings.bytesperline = zr->v4l_settings.width *
975 ((zr->v4l_settings.format->depth + 7) / 8);
977 /* Set necessary params and call zoran_check_jpg_settings to set the defaults */
978 zr->jpg_settings.decimation = 1;
979 zr->jpg_settings.jpg_comp.quality = 50; /* default compression factor 8 */
980 if (zr->card.type != BUZ)
981 zr->jpg_settings.odd_even = 1;
983 zr->jpg_settings.odd_even = 0;
984 zr->jpg_settings.jpg_comp.APPn = 0;
985 zr->jpg_settings.jpg_comp.APP_len = 0; /* No APPn marker */
986 memset(zr->jpg_settings.jpg_comp.APP_data, 0,
987 sizeof(zr->jpg_settings.jpg_comp.APP_data));
988 zr->jpg_settings.jpg_comp.COM_len = 0; /* No COM marker */
989 memset(zr->jpg_settings.jpg_comp.COM_data, 0,
990 sizeof(zr->jpg_settings.jpg_comp.COM_data));
991 zr->jpg_settings.jpg_comp.jpeg_markers =
992 V4L2_JPEG_MARKER_DHT | V4L2_JPEG_MARKER_DQT;
993 i = zoran_check_jpg_settings(zr, &zr->jpg_settings, 0);
995 pci_err(zr->pci_dev, "%s internal error\n", __func__);
997 zr->buffer_size = zr->v4l_settings.bytesperline * zr->v4l_settings.height;
999 clear_interrupt_counters(zr);
1002 static int zr36057_init(struct zoran *zr)
1006 pci_info(zr->pci_dev, "initializing card[%d]\n", zr->id);
1008 /* Avoid nonsense settings from user for default input/norm */
1009 if (default_norm < 0 || default_norm > 2)
1011 if (default_norm == 0) {
1012 zr->norm = V4L2_STD_PAL;
1013 zr->timing = zr->card.tvn[ZR_NORM_PAL];
1014 } else if (default_norm == 1) {
1015 zr->norm = V4L2_STD_NTSC;
1016 zr->timing = zr->card.tvn[ZR_NORM_NTSC];
1018 zr->norm = V4L2_STD_SECAM;
1019 zr->timing = zr->card.tvn[ZR_NORM_SECAM];
1022 pci_warn(zr->pci_dev, "%s - default TV standard not supported by hardware. PAL will be used.\n", __func__);
1023 zr->norm = V4L2_STD_PAL;
1024 zr->timing = zr->card.tvn[ZR_NORM_PAL];
1027 if (default_input > zr->card.inputs - 1) {
1028 pci_warn(zr->pci_dev, "default_input value %d out of range (0-%d)\n",
1029 default_input, zr->card.inputs - 1);
1032 zr->input = default_input;
1034 /* default setup (will be repeated at every open) */
1035 zoran_open_init_params(zr);
1037 /* allocate memory *before* doing anything to the hardware in case allocation fails */
1038 zr->stat_com = dma_alloc_coherent(&zr->pci_dev->dev,
1039 BUZ_NUM_STAT_COM * sizeof(u32),
1040 &zr->p_sc, GFP_KERNEL);
1041 if (!zr->stat_com) {
1044 for (j = 0; j < BUZ_NUM_STAT_COM; j++)
1045 zr->stat_com[j] = cpu_to_le32(1); /* mark as unavailable to zr36057 */
1047 zr->stat_comb = dma_alloc_coherent(&zr->pci_dev->dev,
1048 BUZ_NUM_STAT_COM * sizeof(u32) * 2,
1049 &zr->p_scb, GFP_KERNEL);
1050 if (!zr->stat_comb) {
1055 err = zoran_init_video_devices(zr);
1059 zoran_init_hardware(zr);
1060 if (!pass_through) {
1061 decoder_call(zr, video, s_stream, 0);
1062 encoder_call(zr, video, s_routing, 2, 0, 0);
1065 zr->initialized = 1;
1069 dma_free_coherent(&zr->pci_dev->dev, BUZ_NUM_STAT_COM * sizeof(u32) * 2, zr->stat_comb, zr->p_scb);
1071 dma_free_coherent(&zr->pci_dev->dev, BUZ_NUM_STAT_COM * sizeof(u32), zr->stat_com, zr->p_sc);
1075 static void zoran_remove(struct pci_dev *pdev)
1077 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
1078 struct zoran *zr = to_zoran(v4l2_dev);
1080 if (!zr->initialized)
1083 debugfs_remove_recursive(zr->dbgfs_dir);
1085 zoran_queue_exit(zr);
1087 /* unregister videocodec bus */
1089 videocodec_detach(zr->codec);
1091 videocodec_detach(zr->vfe);
1092 videocodec_exit(zr);
1094 /* unregister i2c bus */
1096 /* disable PCI bus-mastering */
1097 zoran_set_pci_master(zr, 0);
1098 /* put chip into reset */
1099 btwrite(0, ZR36057_SPGPPCR);
1100 pci_free_irq(zr->pci_dev, 0, zr);
1101 /* unmap and free memory */
1102 dma_free_coherent(&zr->pci_dev->dev, BUZ_NUM_STAT_COM * sizeof(u32), zr->stat_com, zr->p_sc);
1103 dma_free_coherent(&zr->pci_dev->dev, BUZ_NUM_STAT_COM * sizeof(u32) * 2, zr->stat_comb, zr->p_scb);
1104 pci_release_regions(pdev);
1105 pci_disable_device(zr->pci_dev);
1106 zoran_exit_video_devices(zr);
1108 v4l2_ctrl_handler_free(&zr->hdl);
1109 v4l2_device_unregister(&zr->v4l2_dev);
1112 void zoran_vdev_release(struct video_device *vdev)
1117 static struct videocodec_master *zoran_setup_videocodec(struct zoran *zr,
1120 struct videocodec_master *m = NULL;
1122 m = devm_kmalloc(&zr->pci_dev->dev, sizeof(*m), GFP_KERNEL);
1127 * magic and type are unused for master struct. Makes sense only at codec structs.
1128 * In the past, .type were initialized to the old V4L1 .hardware value,
1129 * as VID_HARDWARE_ZR36067
1134 m->flags = CODEC_FLAG_ENCODER | CODEC_FLAG_DECODER;
1135 strscpy(m->name, ZR_DEVNAME(zr), sizeof(m->name));
1139 case CODEC_TYPE_ZR36060:
1140 m->readreg = zr36060_read;
1141 m->writereg = zr36060_write;
1142 m->flags |= CODEC_FLAG_JPEG | CODEC_FLAG_VFE;
1144 case CODEC_TYPE_ZR36050:
1145 m->readreg = zr36050_read;
1146 m->writereg = zr36050_write;
1147 m->flags |= CODEC_FLAG_JPEG;
1149 case CODEC_TYPE_ZR36016:
1150 m->readreg = zr36016_read;
1151 m->writereg = zr36016_write;
1152 m->flags |= CODEC_FLAG_VFE;
1159 static void zoran_subdev_notify(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1161 struct zoran *zr = to_zoran(sd->v4l2_dev);
1164 * Bt819 needs to reset its FIFO buffer using #FRST pin and
1165 * LML33 card uses GPIO(7) for that.
1167 if (cmd == BT819_FIFO_RESET_LOW)
1169 else if (cmd == BT819_FIFO_RESET_HIGH)
1173 static int zoran_video_set_ctrl(struct v4l2_ctrl *ctrl)
1175 struct zoran *zr = container_of(ctrl->handler, struct zoran, hdl);
1178 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1179 zr->jpg_settings.jpg_comp.quality = ctrl->val;
1180 return zoran_check_jpg_settings(zr, &zr->jpg_settings, 0);
1188 static const struct v4l2_ctrl_ops zoran_video_ctrl_ops = {
1189 .s_ctrl = zoran_video_set_ctrl,
1192 static int zoran_debugfs_show(struct seq_file *seq, void *v)
1194 struct zoran *zr = seq->private;
1196 seq_printf(seq, "Running mode %x\n", zr->running);
1197 seq_printf(seq, "Codec mode %x\n", zr->codec_mode);
1198 seq_printf(seq, "Norm %llx\n", zr->norm);
1199 seq_printf(seq, "Input %d\n", zr->input);
1200 seq_printf(seq, "Buffersize %d\n", zr->buffer_size);
1202 seq_printf(seq, "V4L width %dx%d\n", zr->v4l_settings.width, zr->v4l_settings.height);
1203 seq_printf(seq, "V4L bytesperline %d\n", zr->v4l_settings.bytesperline);
1205 seq_printf(seq, "JPG decimation %u\n", zr->jpg_settings.decimation);
1206 seq_printf(seq, "JPG hor_dcm %u\n", zr->jpg_settings.hor_dcm);
1207 seq_printf(seq, "JPG ver_dcm %u\n", zr->jpg_settings.ver_dcm);
1208 seq_printf(seq, "JPG tmp_dcm %u\n", zr->jpg_settings.tmp_dcm);
1209 seq_printf(seq, "JPG odd_even %u\n", zr->jpg_settings.odd_even);
1210 seq_printf(seq, "JPG crop %dx%d %d %d\n",
1211 zr->jpg_settings.img_x,
1212 zr->jpg_settings.img_y,
1213 zr->jpg_settings.img_width,
1214 zr->jpg_settings.img_height);
1216 seq_printf(seq, "Prepared %u\n", zr->prepared);
1217 seq_printf(seq, "Queued %u\n", zr->queued);
1219 videocodec_debugfs_show(seq);
1223 DEFINE_SHOW_ATTRIBUTE(zoran_debugfs);
1226 * Scan for a Buz card (actually for the PCI controller ZR36057),
1227 * request the irq and map the io memory
1229 static int zoran_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1231 unsigned char latency, need_latency;
1234 struct videocodec_master *master_vfe = NULL;
1235 struct videocodec_master *master_codec = NULL;
1240 pci_info(pdev, "Zoran MJPEG board driver version %s\n", ZORAN_VERSION);
1242 /* some mainboards might not do PCI-PCI data transfer well */
1243 if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1244 pci_warn(pdev, "%s: chipset does not support reliable PCI-PCI DMA\n",
1247 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1250 err = vb2_dma_contig_set_max_seg_size(&pdev->dev, U32_MAX);
1255 if (nr >= BUZ_MAX) {
1256 pci_err(pdev, "driver limited to %d card(s) maximum\n", BUZ_MAX);
1260 zr = devm_kzalloc(&pdev->dev, sizeof(*zr), GFP_KERNEL);
1264 zr->v4l2_dev.notify = zoran_subdev_notify;
1265 if (v4l2_device_register(&pdev->dev, &zr->v4l2_dev))
1269 snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)), "MJPEG[%u]", zr->id);
1270 if (v4l2_ctrl_handler_init(&zr->hdl, 10))
1272 zr->v4l2_dev.ctrl_handler = &zr->hdl;
1273 v4l2_ctrl_new_std(&zr->hdl, &zoran_video_ctrl_ops,
1274 V4L2_CID_JPEG_COMPRESSION_QUALITY, 0,
1276 spin_lock_init(&zr->spinlock);
1277 mutex_init(&zr->lock);
1278 if (pci_enable_device(pdev))
1280 zr->revision = zr->pci_dev->revision;
1282 pci_info(zr->pci_dev, "Zoran ZR360%c7 (rev %d), irq: %d, memory: 0x%08llx\n",
1283 zr->revision < 2 ? '5' : '6', zr->revision,
1284 zr->pci_dev->irq, (uint64_t)pci_resource_start(zr->pci_dev, 0));
1285 if (zr->revision >= 2)
1286 pci_info(zr->pci_dev, "Subsystem vendor=0x%04x id=0x%04x\n",
1287 zr->pci_dev->subsystem_vendor, zr->pci_dev->subsystem_device);
1289 /* Use auto-detected card type? */
1290 if (card[nr] == -1) {
1291 if (zr->revision < 2) {
1292 pci_err(pdev, "No card type specified, please use the card=X module parameter\n");
1293 pci_err(pdev, "It is not possible to auto-detect ZR36057 based cards\n");
1297 card_num = ent->driver_data;
1298 if (card_num >= NUM_CARDS) {
1299 pci_err(pdev, "Unknown card, try specifying card=X module parameter\n");
1302 pci_info(zr->pci_dev, "%s() - card %s detected\n", __func__, zoran_cards[card_num].name);
1304 card_num = card[nr];
1305 if (card_num >= NUM_CARDS || card_num < 0) {
1306 pci_err(pdev, "User specified card type %d out of range (0 .. %d)\n",
1307 card_num, NUM_CARDS - 1);
1313 * even though we make this a non pointer and thus
1314 * theoretically allow for making changes to this struct
1315 * on a per-individual card basis at runtime, this is
1316 * strongly discouraged. This structure is intended to
1317 * keep general card information, no settings or anything
1319 zr->card = zoran_cards[card_num];
1320 snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)), "%s[%u]",
1321 zr->card.name, zr->id);
1323 err = pci_request_regions(pdev, ZR_DEVNAME(zr));
1327 zr->zr36057_mem = devm_ioremap(&pdev->dev, pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
1328 if (!zr->zr36057_mem) {
1329 pci_err(pdev, "%s() - ioremap failed\n", __func__);
1330 goto zr_pci_release;
1333 result = pci_request_irq(pdev, 0, zoran_irq, NULL, zr, ZR_DEVNAME(zr));
1335 if (result == -EINVAL) {
1336 pci_err(pdev, "%s - bad IRQ number or handler\n", __func__);
1337 } else if (result == -EBUSY) {
1338 pci_err(pdev, "%s - IRQ %d busy, change your PnP config in BIOS\n",
1339 __func__, zr->pci_dev->irq);
1341 pci_err(pdev, "%s - cannot assign IRQ, error code %d\n", __func__, result);
1343 goto zr_pci_release;
1346 /* set PCI latency timer */
1347 pci_read_config_byte(zr->pci_dev, PCI_LATENCY_TIMER,
1349 need_latency = zr->revision > 1 ? 32 : 48;
1350 if (latency != need_latency) {
1351 pci_info(zr->pci_dev, "Changing PCI latency from %d to %d\n", latency, need_latency);
1352 pci_write_config_byte(zr->pci_dev, PCI_LATENCY_TIMER, need_latency);
1355 zr36057_restart(zr);
1357 err = zoran_i2c_init(zr);
1361 pci_info(zr->pci_dev, "Initializing videocodec bus...\n");
1362 err = videocodec_init(zr);
1366 /* reset JPEG codec */
1367 jpeg_codec_sleep(zr, 1);
1368 jpeg_codec_reset(zr);
1369 /* video bus enabled */
1370 /* display codec revision */
1371 if (zr->card.video_codec != 0) {
1372 master_codec = zoran_setup_videocodec(zr, zr->card.video_codec);
1374 goto zr_unreg_videocodec;
1375 zr->codec = videocodec_attach(master_codec);
1377 pci_err(pdev, "%s - no codec found\n", __func__);
1378 goto zr_unreg_videocodec;
1380 if (zr->codec->type != zr->card.video_codec) {
1381 pci_err(pdev, "%s - wrong codec\n", __func__);
1382 goto zr_unreg_videocodec;
1385 if (zr->card.video_vfe != 0) {
1386 master_vfe = zoran_setup_videocodec(zr, zr->card.video_vfe);
1388 goto zr_detach_codec;
1389 zr->vfe = videocodec_attach(master_vfe);
1391 pci_err(pdev, "%s - no VFE found\n", __func__);
1392 goto zr_detach_codec;
1394 if (zr->vfe->type != zr->card.video_vfe) {
1395 pci_err(pdev, "%s = wrong VFE\n", __func__);
1400 /* take care of Natoma chipset and a revision 1 zr36057 */
1401 if ((pci_pci_problems & PCIPCI_NATOMA) && zr->revision <= 1)
1402 pci_info(zr->pci_dev, "ZR36057/Natoma bug, max. buffer size is 128K\n");
1404 if (zr36057_init(zr) < 0)
1407 zr->map_mode = ZORAN_MAP_MODE_RAW;
1409 zr->dbgfs_dir = debugfs_create_dir(ZR_DEVNAME(zr), NULL);
1410 debugfs_create_file("debug", 0444, zr->dbgfs_dir, zr,
1411 &zoran_debugfs_fops);
1415 videocodec_detach(zr->vfe);
1417 videocodec_detach(zr->codec);
1418 zr_unreg_videocodec:
1419 videocodec_exit(zr);
1423 btwrite(0, ZR36057_SPGPPCR);
1424 pci_free_irq(zr->pci_dev, 0, zr);
1426 pci_release_regions(pdev);
1428 v4l2_ctrl_handler_free(&zr->hdl);
1429 v4l2_device_unregister(&zr->v4l2_dev);
1435 static struct pci_driver zoran_driver = {
1437 .id_table = zr36067_pci_tbl,
1438 .probe = zoran_probe,
1439 .remove = zoran_remove,
1442 module_pci_driver(zoran_driver);