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"
33 extern const struct zoran_format zoran_formats[];
35 static int card[BUZ_MAX] = { [0 ... (BUZ_MAX - 1)] = -1 };
36 module_param_array(card, int, NULL, 0444);
37 MODULE_PARM_DESC(card, "Card type");
40 * The video mem address of the video card. The driver has a little database for some videocards
41 * to determine it from there. If your video card is not in there you have either to give it to
42 * the driver as a parameter or set in in a VIDIOCSFBUF ioctl
45 static unsigned long vidmem; /* default = 0 - Video memory base address */
46 module_param_hw(vidmem, ulong, iomem, 0444);
47 MODULE_PARM_DESC(vidmem, "Default video memory base address");
49 /* Default input and video norm at startup of the driver. */
51 static unsigned int default_input; /* default 0 = Composite, 1 = S-Video */
52 module_param(default_input, uint, 0444);
53 MODULE_PARM_DESC(default_input,
54 "Default input (0=Composite, 1=S-Video, 2=Internal)");
56 static int default_mux = 1; /* 6 Eyes input selection */
57 module_param(default_mux, int, 0644);
58 MODULE_PARM_DESC(default_mux,
59 "Default 6 Eyes mux setting (Input selection)");
61 static int default_norm; /* default 0 = PAL, 1 = NTSC 2 = SECAM */
62 module_param(default_norm, int, 0444);
63 MODULE_PARM_DESC(default_norm, "Default norm (0=PAL, 1=NTSC, 2=SECAM)");
65 /* /dev/videoN, -1 for autodetect */
66 static int video_nr[BUZ_MAX] = { [0 ... (BUZ_MAX - 1)] = -1 };
67 module_param_array(video_nr, int, NULL, 0444);
68 MODULE_PARM_DESC(video_nr, "Video device number (-1=Auto)");
71 int v4l_bufsize = 864; /* Everybody should be able to work with this setting */
72 module_param(v4l_nbufs, int, 0644);
73 MODULE_PARM_DESC(v4l_nbufs, "Maximum number of V4L buffers to use");
74 module_param(v4l_bufsize, int, 0644);
75 MODULE_PARM_DESC(v4l_bufsize, "Maximum size per V4L buffer (in kB)");
78 int jpg_bufsize = 512; /* max size for 100% quality full-PAL frame */
79 module_param(jpg_nbufs, int, 0644);
80 MODULE_PARM_DESC(jpg_nbufs, "Maximum number of JPG buffers to use");
81 module_param(jpg_bufsize, int, 0644);
82 MODULE_PARM_DESC(jpg_bufsize, "Maximum size per JPG buffer (in kB)");
84 /* 1=Pass through TV signal when device is not used */
85 /* 0=Show color bar when device is not used (LML33: only if lml33dpath=1) */
87 module_param(pass_through, int, 0644);
88 MODULE_PARM_DESC(pass_through,
89 "Pass TV signal through to TV-out when idling");
91 int zr36067_debug = 1;
92 module_param_named(debug, zr36067_debug, int, 0644);
93 MODULE_PARM_DESC(debug, "Debug level (0-5)");
95 #define ZORAN_VERSION "0.10.1"
97 MODULE_DESCRIPTION("Zoran-36057/36067 JPEG codec driver");
98 MODULE_AUTHOR("Serguei Miridonov");
99 MODULE_LICENSE("GPL");
100 MODULE_VERSION(ZORAN_VERSION);
102 #define ZR_DEVICE(subven, subdev, data) { \
103 .vendor = PCI_VENDOR_ID_ZORAN, .device = PCI_DEVICE_ID_ZORAN_36057, \
104 .subvendor = (subven), .subdevice = (subdev), .driver_data = (data) }
106 static const struct pci_device_id zr36067_pci_tbl[] = {
107 ZR_DEVICE(PCI_VENDOR_ID_MIRO, PCI_DEVICE_ID_MIRO_DC10PLUS, DC10_PLUS),
108 ZR_DEVICE(PCI_VENDOR_ID_MIRO, PCI_DEVICE_ID_MIRO_DC30PLUS, DC30_PLUS),
109 ZR_DEVICE(PCI_VENDOR_ID_ELECTRONICDESIGNGMBH, PCI_DEVICE_ID_LML_33R10, LML33R10),
110 ZR_DEVICE(PCI_VENDOR_ID_IOMEGA, PCI_DEVICE_ID_IOMEGA_BUZ, BUZ),
111 ZR_DEVICE(PCI_ANY_ID, PCI_ANY_ID, NUM_CARDS),
114 MODULE_DEVICE_TABLE(pci, zr36067_pci_tbl);
116 static unsigned int zoran_num; /* number of cards found */
118 /* videocodec bus functions ZR36060 */
119 static u32 zr36060_read(struct videocodec *codec, u16 reg)
121 struct zoran *zr = (struct zoran *)codec->master_data->data;
124 if (post_office_wait(zr) || post_office_write(zr, 0, 1, reg >> 8) ||
125 post_office_write(zr, 0, 2, reg & 0xff))
128 data = post_office_read(zr, 0, 3) & 0xff;
132 static void zr36060_write(struct videocodec *codec, u16 reg, u32 val)
134 struct zoran *zr = (struct zoran *)codec->master_data->data;
136 if (post_office_wait(zr) || post_office_write(zr, 0, 1, reg >> 8) ||
137 post_office_write(zr, 0, 2, reg & 0xff))
140 post_office_write(zr, 0, 3, val & 0xff);
143 /* videocodec bus functions ZR36050 */
144 static u32 zr36050_read(struct videocodec *codec, u16 reg)
146 struct zoran *zr = (struct zoran *)codec->master_data->data;
149 if (post_office_wait(zr) || post_office_write(zr, 1, 0, reg >> 2)) // reg. HIGHBYTES
152 data = post_office_read(zr, 0, reg & 0x03) & 0xff; // reg. LOWBYTES + read
156 static void zr36050_write(struct videocodec *codec, u16 reg, u32 val)
158 struct zoran *zr = (struct zoran *)codec->master_data->data;
160 if (post_office_wait(zr) || post_office_write(zr, 1, 0, reg >> 2)) // reg. HIGHBYTES
163 post_office_write(zr, 0, reg & 0x03, val & 0xff); // reg. LOWBYTES + wr. data
166 /* videocodec bus functions ZR36016 */
167 static u32 zr36016_read(struct videocodec *codec, u16 reg)
169 struct zoran *zr = (struct zoran *)codec->master_data->data;
172 if (post_office_wait(zr))
175 data = post_office_read(zr, 2, reg & 0x03) & 0xff; // read
179 /* hack for in zoran_device.c */
180 void zr36016_write(struct videocodec *codec, u16 reg, u32 val)
182 struct zoran *zr = (struct zoran *)codec->master_data->data;
184 if (post_office_wait(zr))
187 post_office_write(zr, 2, reg & 0x03, val & 0x0ff); // wr. data
191 * Board specific information
194 static void dc10_init(struct zoran *zr)
196 pci_dbg(zr->pci_dev, "%s\n", __func__);
198 /* Pixel clock selection */
201 /* Enable the video bus sync signals */
205 static void dc10plus_init(struct zoran *zr)
207 pci_dbg(zr->pci_dev, "%s\n", __func__);
210 static void buz_init(struct zoran *zr)
212 pci_dbg(zr->pci_dev, "%s\n", __func__);
214 /* some stuff from Iomega */
215 pci_write_config_dword(zr->pci_dev, 0xfc, 0x90680f15);
216 pci_write_config_dword(zr->pci_dev, 0x0c, 0x00012020);
217 pci_write_config_dword(zr->pci_dev, 0xe8, 0xc0200000);
220 static void lml33_init(struct zoran *zr)
222 pci_dbg(zr->pci_dev, "%s\n", __func__);
224 GPIO(zr, 2, 1); // Set Composite input/output
227 static void avs6eyes_init(struct zoran *zr)
229 // AverMedia 6-Eyes original driver by Christer Weinigel
231 // Lifted straight from Christer's old driver and
232 // modified slightly by Martin Samuelsson.
234 int mux = default_mux; /* 1 = BT866, 7 = VID1 */
236 GPIO(zr, 4, 1); /* Bt866 SLEEP on */
239 GPIO(zr, 0, 1); /* ZR36060 /RESET on */
240 GPIO(zr, 1, 0); /* ZR36060 /SLEEP on */
241 GPIO(zr, 2, mux & 1); /* MUX S0 */
242 GPIO(zr, 3, 0); /* /FRAME on */
243 GPIO(zr, 4, 0); /* Bt866 SLEEP off */
244 GPIO(zr, 5, mux & 2); /* MUX S1 */
245 GPIO(zr, 6, 0); /* ? */
246 GPIO(zr, 7, mux & 4); /* MUX S2 */
249 static const char *codecid_to_modulename(u16 codecid)
251 const char *name = NULL;
254 case CODEC_TYPE_ZR36060:
257 case CODEC_TYPE_ZR36050:
260 case CODEC_TYPE_ZR36016:
269 // u16 wt, wa, h_start, h_sync_start, ht, ha, v_start;
272 static const struct tvnorm f50sqpixel = { 944, 768, 83, 880, 625, 576, 16 };
273 static const struct tvnorm f60sqpixel = { 780, 640, 51, 716, 525, 480, 12 };
274 static const struct tvnorm f50ccir601 = { 864, 720, 75, 804, 625, 576, 18 };
275 static const struct tvnorm f60ccir601 = { 858, 720, 57, 788, 525, 480, 16 };
277 static const struct tvnorm f50ccir601_lml33 = { 864, 720, 75 + 34, 804, 625, 576, 18 };
278 static const struct tvnorm f60ccir601_lml33 = { 858, 720, 57 + 34, 788, 525, 480, 16 };
280 /* The DC10 (57/16/50) uses VActive as HSync, so h_start must be 0 */
281 static const struct tvnorm f50sqpixel_dc10 = { 944, 768, 0, 880, 625, 576, 0 };
282 static const struct tvnorm f60sqpixel_dc10 = { 780, 640, 0, 716, 525, 480, 12 };
285 * FIXME: I cannot swap U and V in saa7114, so i do one pixel left shift in zoran (75 -> 74)
286 * (Maxim Yevtyushkin <max@linuxmedialabs.com>)
288 static const struct tvnorm f50ccir601_lm33r10 = { 864, 720, 74 + 54, 804, 625, 576, 18 };
289 static const struct tvnorm f60ccir601_lm33r10 = { 858, 720, 56 + 54, 788, 525, 480, 16 };
292 * FIXME: The ks0127 seem incapable of swapping U and V, too, which is why I copy Maxim's left
293 * shift hack for the 6 Eyes.
295 * Christer's driver used the unshifted norms, though...
298 static const struct tvnorm f50ccir601_avs6eyes = { 864, 720, 74, 804, 625, 576, 18 };
299 static const struct tvnorm f60ccir601_avs6eyes = { 858, 720, 56, 788, 525, 480, 16 };
301 static const unsigned short vpx3220_addrs[] = { 0x43, 0x47, I2C_CLIENT_END };
302 static const unsigned short saa7110_addrs[] = { 0x4e, 0x4f, I2C_CLIENT_END };
303 static const unsigned short saa7111_addrs[] = { 0x25, 0x24, I2C_CLIENT_END };
304 static const unsigned short saa7114_addrs[] = { 0x21, 0x20, I2C_CLIENT_END };
305 static const unsigned short adv717x_addrs[] = { 0x6a, 0x6b, 0x2a, 0x2b, I2C_CLIENT_END };
306 static const unsigned short ks0127_addrs[] = { 0x6c, 0x6d, I2C_CLIENT_END };
307 static const unsigned short saa7185_addrs[] = { 0x44, I2C_CLIENT_END };
308 static const unsigned short bt819_addrs[] = { 0x45, I2C_CLIENT_END };
309 static const unsigned short bt856_addrs[] = { 0x44, I2C_CLIENT_END };
310 static const unsigned short bt866_addrs[] = { 0x44, I2C_CLIENT_END };
312 static struct card_info zoran_cards[NUM_CARDS] = {
316 .i2c_decoder = "vpx3220a",
317 .addrs_decoder = vpx3220_addrs,
318 .video_codec = CODEC_TYPE_ZR36050,
319 .video_vfe = CODEC_TYPE_ZR36016,
325 { 0, "Internal/comp" }
327 .norms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
334 .vsync_int = ZR36057_ISR_GIRQ1,
335 .gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
336 .gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
338 .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
339 .gws_not_connected = 0,
345 .i2c_decoder = "saa7110",
346 .addrs_decoder = saa7110_addrs,
347 .i2c_encoder = "adv7175",
348 .addrs_encoder = adv717x_addrs,
349 .video_codec = CODEC_TYPE_ZR36060,
355 { 5, "Internal/comp" }
357 .norms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
362 .jpeg_int = ZR36057_ISR_GIRQ0,
363 .vsync_int = ZR36057_ISR_GIRQ1,
364 .gpio = { 3, 0, 6, 1, 2, -1, 4, 5 },
365 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
367 .vfe_pol = { 1, 1, 1, 1, 0, 0, 0, 0 },
368 .gws_not_connected = 0,
370 .init = &dc10plus_init,
374 .i2c_decoder = "saa7110",
375 .addrs_decoder = saa7110_addrs,
376 .i2c_encoder = "adv7175",
377 .addrs_encoder = adv717x_addrs,
378 .video_codec = CODEC_TYPE_ZR36060,
384 { 5, "Internal/comp" }
386 .norms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
392 .jpeg_int = ZR36057_ISR_GIRQ0,
393 .vsync_int = ZR36057_ISR_GIRQ1,
394 .gpio = { 3, 0, 6, 1, 2, -1, 4, 5 },
395 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
397 .vfe_pol = { 1, 1, 1, 1, 0, 0, 0, 0 },
398 .gws_not_connected = 0,
400 .init = &dc10plus_init,
404 .i2c_decoder = "vpx3220a",
405 .addrs_decoder = vpx3220_addrs,
406 .i2c_encoder = "adv7175",
407 .addrs_encoder = adv717x_addrs,
408 .video_codec = CODEC_TYPE_ZR36050,
409 .video_vfe = CODEC_TYPE_ZR36016,
415 { 0, "Internal/comp" }
417 .norms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
424 .vsync_int = ZR36057_ISR_GIRQ1,
425 .gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
426 .gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
428 .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
429 .gws_not_connected = 0,
435 .i2c_decoder = "vpx3220a",
436 .addrs_decoder = vpx3220_addrs,
437 .i2c_encoder = "adv7175",
438 .addrs_encoder = adv717x_addrs,
439 .video_codec = CODEC_TYPE_ZR36050,
440 .video_vfe = CODEC_TYPE_ZR36016,
446 { 0, "Internal/comp" }
448 .norms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
455 .vsync_int = ZR36057_ISR_GIRQ1,
456 .gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
457 .gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
459 .vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
460 .gws_not_connected = 0,
466 .i2c_decoder = "bt819a",
467 .addrs_decoder = bt819_addrs,
468 .i2c_encoder = "bt856",
469 .addrs_encoder = bt856_addrs,
470 .video_codec = CODEC_TYPE_ZR36060,
477 .norms = V4L2_STD_NTSC | V4L2_STD_PAL,
483 .jpeg_int = ZR36057_ISR_GIRQ1,
484 .vsync_int = ZR36057_ISR_GIRQ0,
485 .gpio = { 1, -1, 3, 5, 7, -1, -1, -1 },
486 .gpio_pol = { 0, 0, 0, 0, 1, 0, 0, 0 },
488 .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
489 .gws_not_connected = 1,
495 .i2c_decoder = "saa7114",
496 .addrs_decoder = saa7114_addrs,
497 .i2c_encoder = "adv7170",
498 .addrs_encoder = adv717x_addrs,
499 .video_codec = CODEC_TYPE_ZR36060,
506 .norms = V4L2_STD_NTSC | V4L2_STD_PAL,
512 .jpeg_int = ZR36057_ISR_GIRQ1,
513 .vsync_int = ZR36057_ISR_GIRQ0,
514 .gpio = { 1, -1, 3, 5, 7, -1, -1, -1 },
515 .gpio_pol = { 0, 0, 0, 0, 1, 0, 0, 0 },
517 .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
518 .gws_not_connected = 1,
524 .i2c_decoder = "saa7111",
525 .addrs_decoder = saa7111_addrs,
526 .i2c_encoder = "saa7185",
527 .addrs_encoder = saa7185_addrs,
528 .video_codec = CODEC_TYPE_ZR36060,
535 .norms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
541 .jpeg_int = ZR36057_ISR_GIRQ1,
542 .vsync_int = ZR36057_ISR_GIRQ0,
543 .gpio = { 1, -1, 3, -1, -1, -1, -1, -1 },
544 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
546 .vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
547 .gws_not_connected = 1,
553 /* AverMedia chose not to brand the 6-Eyes. Thus it can't be autodetected, and requires card=x. */
554 .i2c_decoder = "ks0127",
555 .addrs_decoder = ks0127_addrs,
556 .i2c_encoder = "bt866",
557 .addrs_encoder = bt866_addrs,
558 .video_codec = CODEC_TYPE_ZR36060,
562 { 0, "Composite 1" },
563 { 1, "Composite 2" },
564 { 2, "Composite 3" },
565 { 4, "Composite 4" },
566 { 5, "Composite 5" },
567 { 6, "Composite 6" },
573 .norms = V4L2_STD_NTSC | V4L2_STD_PAL,
575 &f50ccir601_avs6eyes,
576 &f60ccir601_avs6eyes,
579 .jpeg_int = ZR36057_ISR_GIRQ1,
580 .vsync_int = ZR36057_ISR_GIRQ0,
581 .gpio = { 1, 0, 3, -1, -1, -1, -1, -1 },// Validity unknown /Sam
582 .gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 }, // Validity unknown /Sam
583 .gpcs = { 3, 1 }, // Validity unknown /Sam
584 .vfe_pol = { 1, 0, 0, 0, 0, 1, 0, 0 }, // Validity unknown /Sam
585 .gws_not_connected = 1,
587 .init = &avs6eyes_init,
595 /* software I2C functions */
596 static int zoran_i2c_getsda(void *data)
598 struct zoran *zr = (struct zoran *)data;
600 return (btread(ZR36057_I2CBR) >> 1) & 1;
603 static int zoran_i2c_getscl(void *data)
605 struct zoran *zr = (struct zoran *)data;
607 return btread(ZR36057_I2CBR) & 1;
610 static void zoran_i2c_setsda(void *data, int state)
612 struct zoran *zr = (struct zoran *)data;
618 btwrite(zr->i2cbr, ZR36057_I2CBR);
621 static void zoran_i2c_setscl(void *data, int state)
623 struct zoran *zr = (struct zoran *)data;
629 btwrite(zr->i2cbr, ZR36057_I2CBR);
632 static const struct i2c_algo_bit_data zoran_i2c_bit_data_template = {
633 .setsda = zoran_i2c_setsda,
634 .setscl = zoran_i2c_setscl,
635 .getsda = zoran_i2c_getsda,
636 .getscl = zoran_i2c_getscl,
641 static int zoran_register_i2c(struct zoran *zr)
643 zr->i2c_algo = zoran_i2c_bit_data_template;
644 zr->i2c_algo.data = zr;
645 strscpy(zr->i2c_adapter.name, ZR_DEVNAME(zr),
646 sizeof(zr->i2c_adapter.name));
647 i2c_set_adapdata(&zr->i2c_adapter, &zr->v4l2_dev);
648 zr->i2c_adapter.algo_data = &zr->i2c_algo;
649 zr->i2c_adapter.dev.parent = &zr->pci_dev->dev;
650 return i2c_bit_add_bus(&zr->i2c_adapter);
653 static void zoran_unregister_i2c(struct zoran *zr)
655 i2c_del_adapter(&zr->i2c_adapter);
658 /* Check a zoran_params struct for correctness, insert default params */
659 int zoran_check_jpg_settings(struct zoran *zr,
660 struct zoran_jpg_settings *settings, int try)
662 int err = 0, err0 = 0;
664 pci_dbg(zr->pci_dev, "%s - dec: %d, Hdcm: %d, Vdcm: %d, Tdcm: %d\n",
665 __func__, settings->decimation, settings->hor_dcm,
666 settings->ver_dcm, settings->tmp_dcm);
667 pci_dbg(zr->pci_dev, "%s - x: %d, y: %d, w: %d, y: %d\n", __func__,
668 settings->img_x, settings->img_y,
669 settings->img_width, settings->img_height);
670 /* Check decimation, set default values for decimation = 1, 2, 4 */
671 switch (settings->decimation) {
674 settings->hor_dcm = 1;
675 settings->ver_dcm = 1;
676 settings->tmp_dcm = 1;
677 settings->field_per_buff = 2;
680 settings->img_width = BUZ_MAX_WIDTH;
681 settings->img_height = BUZ_MAX_HEIGHT / 2;
685 settings->hor_dcm = 2;
686 settings->ver_dcm = 1;
687 settings->tmp_dcm = 2;
688 settings->field_per_buff = 1;
689 settings->img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
691 settings->img_width =
692 (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
693 settings->img_height = BUZ_MAX_HEIGHT / 2;
697 if (zr->card.type == DC10_NEW) {
698 pci_dbg(zr->pci_dev, "%s - HDec by 4 is not supported on the DC10\n", __func__);
703 settings->hor_dcm = 4;
704 settings->ver_dcm = 2;
705 settings->tmp_dcm = 2;
706 settings->field_per_buff = 1;
707 settings->img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
709 settings->img_width =
710 (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
711 settings->img_height = BUZ_MAX_HEIGHT / 2;
715 /* We have to check the data the user has set */
717 if (settings->hor_dcm != 1 && settings->hor_dcm != 2 &&
718 (zr->card.type == DC10_NEW || settings->hor_dcm != 4)) {
719 settings->hor_dcm = clamp(settings->hor_dcm, 1, 2);
722 if (settings->ver_dcm != 1 && settings->ver_dcm != 2) {
723 settings->ver_dcm = clamp(settings->ver_dcm, 1, 2);
726 if (settings->tmp_dcm != 1 && settings->tmp_dcm != 2) {
727 settings->tmp_dcm = clamp(settings->tmp_dcm, 1, 2);
730 if (settings->field_per_buff != 1 &&
731 settings->field_per_buff != 2) {
732 settings->field_per_buff = clamp(settings->field_per_buff, 1, 2);
735 if (settings->img_x < 0) {
739 if (settings->img_y < 0) {
743 if (settings->img_width < 0 || settings->img_width > BUZ_MAX_WIDTH) {
744 settings->img_width = clamp(settings->img_width, 0, (int)BUZ_MAX_WIDTH);
747 if (settings->img_height < 0 || settings->img_height > BUZ_MAX_HEIGHT / 2) {
748 settings->img_height = clamp(settings->img_height, 0, BUZ_MAX_HEIGHT / 2);
751 if (settings->img_x + settings->img_width > BUZ_MAX_WIDTH) {
752 settings->img_x = BUZ_MAX_WIDTH - settings->img_width;
755 if (settings->img_y + settings->img_height > BUZ_MAX_HEIGHT / 2) {
756 settings->img_y = BUZ_MAX_HEIGHT / 2 - settings->img_height;
759 if (settings->img_width % (16 * settings->hor_dcm) != 0) {
760 settings->img_width -= settings->img_width % (16 * settings->hor_dcm);
761 if (settings->img_width == 0)
762 settings->img_width = 16 * settings->hor_dcm;
765 if (settings->img_height % (8 * settings->ver_dcm) != 0) {
766 settings->img_height -= settings->img_height % (8 * settings->ver_dcm);
767 if (settings->img_height == 0)
768 settings->img_height = 8 * settings->ver_dcm;
773 pci_err(zr->pci_dev, "%s - error in params for decimation = 0\n", __func__);
778 pci_err(zr->pci_dev, "%s - decimation = %d, must be 0, 1, 2 or 4\n",
779 __func__, settings->decimation);
784 if (settings->jpg_comp.quality > 100)
785 settings->jpg_comp.quality = 100;
786 if (settings->jpg_comp.quality < 5)
787 settings->jpg_comp.quality = 5;
788 if (settings->jpg_comp.APPn < 0)
789 settings->jpg_comp.APPn = 0;
790 if (settings->jpg_comp.APPn > 15)
791 settings->jpg_comp.APPn = 15;
792 if (settings->jpg_comp.APP_len < 0)
793 settings->jpg_comp.APP_len = 0;
794 if (settings->jpg_comp.APP_len > 60)
795 settings->jpg_comp.APP_len = 60;
796 if (settings->jpg_comp.COM_len < 0)
797 settings->jpg_comp.COM_len = 0;
798 if (settings->jpg_comp.COM_len > 60)
799 settings->jpg_comp.COM_len = 60;
805 static int zoran_init_video_device(struct zoran *zr, struct video_device *video_dev, int dir)
809 /* Now add the template and register the device unit. */
810 *video_dev = zoran_template;
811 video_dev->v4l2_dev = &zr->v4l2_dev;
812 video_dev->lock = &zr->lock;
813 video_dev->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_READWRITE | dir;
815 strscpy(video_dev->name, ZR_DEVNAME(zr), sizeof(video_dev->name));
817 * It's not a mem2mem device, but you can both capture and output from one and the same
818 * device. This should really be split up into two device nodes, but that's a job for
821 video_dev->vfl_dir = VFL_DIR_M2M;
822 zoran_queue_init(zr, &zr->vq, V4L2_BUF_TYPE_VIDEO_CAPTURE);
824 err = video_register_device(video_dev, VFL_TYPE_VIDEO, video_nr[zr->id]);
827 video_set_drvdata(video_dev, zr);
831 static void zoran_exit_video_devices(struct zoran *zr)
833 video_unregister_device(zr->video_dev);
834 kfree(zr->video_dev);
837 static int zoran_init_video_devices(struct zoran *zr)
841 zr->video_dev = video_device_alloc();
845 err = zoran_init_video_device(zr, zr->video_dev, V4L2_CAP_VIDEO_CAPTURE);
847 kfree(zr->video_dev);
851 void zoran_open_init_params(struct zoran *zr)
855 zr->v4l_settings.width = 192;
856 zr->v4l_settings.height = 144;
857 zr->v4l_settings.format = &zoran_formats[7]; /* YUY2 - YUV-4:2:2 packed */
858 zr->v4l_settings.bytesperline = zr->v4l_settings.width *
859 ((zr->v4l_settings.format->depth + 7) / 8);
861 /* Set necessary params and call zoran_check_jpg_settings to set the defaults */
862 zr->jpg_settings.decimation = 1;
863 zr->jpg_settings.jpg_comp.quality = 50; /* default compression factor 8 */
864 if (zr->card.type != BUZ)
865 zr->jpg_settings.odd_even = 1;
867 zr->jpg_settings.odd_even = 0;
868 zr->jpg_settings.jpg_comp.APPn = 0;
869 zr->jpg_settings.jpg_comp.APP_len = 0; /* No APPn marker */
870 memset(zr->jpg_settings.jpg_comp.APP_data, 0,
871 sizeof(zr->jpg_settings.jpg_comp.APP_data));
872 zr->jpg_settings.jpg_comp.COM_len = 0; /* No COM marker */
873 memset(zr->jpg_settings.jpg_comp.COM_data, 0,
874 sizeof(zr->jpg_settings.jpg_comp.COM_data));
875 zr->jpg_settings.jpg_comp.jpeg_markers =
876 V4L2_JPEG_MARKER_DHT | V4L2_JPEG_MARKER_DQT;
877 i = zoran_check_jpg_settings(zr, &zr->jpg_settings, 0);
879 pci_err(zr->pci_dev, "%s internal error\n", __func__);
881 zr->buffer_size = zr->v4l_settings.bytesperline * zr->v4l_settings.height;
883 clear_interrupt_counters(zr);
886 static int zr36057_init(struct zoran *zr)
890 pci_info(zr->pci_dev, "initializing card[%d]\n", zr->id);
892 /* Avoid nonsense settings from user for default input/norm */
893 if (default_norm < 0 || default_norm > 2)
895 if (default_norm == 0) {
896 zr->norm = V4L2_STD_PAL;
897 zr->timing = zr->card.tvn[ZR_NORM_PAL];
898 } else if (default_norm == 1) {
899 zr->norm = V4L2_STD_NTSC;
900 zr->timing = zr->card.tvn[ZR_NORM_NTSC];
902 zr->norm = V4L2_STD_SECAM;
903 zr->timing = zr->card.tvn[ZR_NORM_SECAM];
906 pci_warn(zr->pci_dev, "%s - default TV standard not supported by hardware. PAL will be used.\n", __func__);
907 zr->norm = V4L2_STD_PAL;
908 zr->timing = zr->card.tvn[ZR_NORM_PAL];
911 if (default_input > zr->card.inputs - 1) {
912 pci_warn(zr->pci_dev, "default_input value %d out of range (0-%d)\n",
913 default_input, zr->card.inputs - 1);
916 zr->input = default_input;
918 /* default setup (will be repeated at every open) */
919 zoran_open_init_params(zr);
921 /* allocate memory *before* doing anything to the hardware in case allocation fails */
922 zr->stat_com = dma_alloc_coherent(&zr->pci_dev->dev,
923 BUZ_NUM_STAT_COM * sizeof(u32),
924 &zr->p_sc, GFP_KERNEL);
928 for (j = 0; j < BUZ_NUM_STAT_COM; j++)
929 zr->stat_com[j] = cpu_to_le32(1); /* mark as unavailable to zr36057 */
931 zr->stat_comb = dma_alloc_coherent(&zr->pci_dev->dev,
932 BUZ_NUM_STAT_COM * sizeof(u32) * 2,
933 &zr->p_scb, GFP_KERNEL);
934 if (!zr->stat_comb) {
939 err = zoran_init_video_devices(zr);
943 zoran_init_hardware(zr);
945 decoder_call(zr, video, s_stream, 0);
946 encoder_call(zr, video, s_routing, 2, 0, 0);
953 dma_free_coherent(&zr->pci_dev->dev, BUZ_NUM_STAT_COM * sizeof(u32) * 2, zr->stat_comb, zr->p_scb);
955 dma_free_coherent(&zr->pci_dev->dev, BUZ_NUM_STAT_COM * sizeof(u32), zr->stat_com, zr->p_sc);
959 static void zoran_remove(struct pci_dev *pdev)
961 struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
962 struct zoran *zr = to_zoran(v4l2_dev);
964 if (!zr->initialized)
967 zoran_queue_exit(zr);
969 /* unregister videocodec bus */
971 videocodec_detach(zr->codec);
973 videocodec_detach(zr->vfe);
975 /* unregister i2c bus */
976 zoran_unregister_i2c(zr);
977 /* disable PCI bus-mastering */
978 zoran_set_pci_master(zr, 0);
979 /* put chip into reset */
980 btwrite(0, ZR36057_SPGPPCR);
981 pci_free_irq(zr->pci_dev, 0, zr);
982 /* unmap and free memory */
983 dma_free_coherent(&zr->pci_dev->dev, BUZ_NUM_STAT_COM * sizeof(u32), zr->stat_com, zr->p_sc);
984 dma_free_coherent(&zr->pci_dev->dev, BUZ_NUM_STAT_COM * sizeof(u32) * 2, zr->stat_comb, zr->p_scb);
985 pci_release_regions(pdev);
986 pci_disable_device(zr->pci_dev);
987 zoran_exit_video_devices(zr);
989 v4l2_ctrl_handler_free(&zr->hdl);
990 v4l2_device_unregister(&zr->v4l2_dev);
993 void zoran_vdev_release(struct video_device *vdev)
998 static struct videocodec_master *zoran_setup_videocodec(struct zoran *zr,
1001 struct videocodec_master *m = NULL;
1003 m = devm_kmalloc(&zr->pci_dev->dev, sizeof(*m), GFP_KERNEL);
1008 * magic and type are unused for master struct. Makes sense only at codec structs.
1009 * In the past, .type were initialized to the old V4L1 .hardware value,
1010 * as VID_HARDWARE_ZR36067
1015 m->flags = CODEC_FLAG_ENCODER | CODEC_FLAG_DECODER;
1016 strscpy(m->name, ZR_DEVNAME(zr), sizeof(m->name));
1020 case CODEC_TYPE_ZR36060:
1021 m->readreg = zr36060_read;
1022 m->writereg = zr36060_write;
1023 m->flags |= CODEC_FLAG_JPEG | CODEC_FLAG_VFE;
1025 case CODEC_TYPE_ZR36050:
1026 m->readreg = zr36050_read;
1027 m->writereg = zr36050_write;
1028 m->flags |= CODEC_FLAG_JPEG;
1030 case CODEC_TYPE_ZR36016:
1031 m->readreg = zr36016_read;
1032 m->writereg = zr36016_write;
1033 m->flags |= CODEC_FLAG_VFE;
1040 static void zoran_subdev_notify(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1042 struct zoran *zr = to_zoran(sd->v4l2_dev);
1045 * Bt819 needs to reset its FIFO buffer using #FRST pin and
1046 * LML33 card uses GPIO(7) for that.
1048 if (cmd == BT819_FIFO_RESET_LOW)
1050 else if (cmd == BT819_FIFO_RESET_HIGH)
1054 static int zoran_video_set_ctrl(struct v4l2_ctrl *ctrl)
1056 struct zoran *zr = container_of(ctrl->handler, struct zoran, hdl);
1059 case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1060 zr->jpg_settings.jpg_comp.quality = ctrl->val;
1061 return zoran_check_jpg_settings(zr, &zr->jpg_settings, 0);
1069 static const struct v4l2_ctrl_ops zoran_video_ctrl_ops = {
1070 .s_ctrl = zoran_video_set_ctrl,
1074 * Scan for a Buz card (actually for the PCI controller ZR36057),
1075 * request the irq and map the io memory
1077 static int zoran_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1079 unsigned char latency, need_latency;
1082 struct videocodec_master *master_vfe = NULL;
1083 struct videocodec_master *master_codec = NULL;
1085 const char *codec_name, *vfe_name;
1089 err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1092 err = vb2_dma_contig_set_max_seg_size(&pdev->dev, U32_MAX);
1097 if (nr >= BUZ_MAX) {
1098 pci_err(pdev, "driver limited to %d card(s) maximum\n", BUZ_MAX);
1102 zr = devm_kzalloc(&pdev->dev, sizeof(*zr), GFP_KERNEL);
1106 zr->v4l2_dev.notify = zoran_subdev_notify;
1107 if (v4l2_device_register(&pdev->dev, &zr->v4l2_dev))
1111 snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)), "MJPEG[%u]", zr->id);
1112 if (v4l2_ctrl_handler_init(&zr->hdl, 10))
1114 zr->v4l2_dev.ctrl_handler = &zr->hdl;
1115 v4l2_ctrl_new_std(&zr->hdl, &zoran_video_ctrl_ops,
1116 V4L2_CID_JPEG_COMPRESSION_QUALITY, 0,
1118 spin_lock_init(&zr->spinlock);
1119 mutex_init(&zr->lock);
1120 if (pci_enable_device(pdev))
1122 zr->revision = zr->pci_dev->revision;
1124 pci_info(zr->pci_dev, "Zoran ZR360%c7 (rev %d), irq: %d, memory: 0x%08llx\n",
1125 zr->revision < 2 ? '5' : '6', zr->revision,
1126 zr->pci_dev->irq, (uint64_t)pci_resource_start(zr->pci_dev, 0));
1127 if (zr->revision >= 2)
1128 pci_info(zr->pci_dev, "Subsystem vendor=0x%04x id=0x%04x\n",
1129 zr->pci_dev->subsystem_vendor, zr->pci_dev->subsystem_device);
1131 /* Use auto-detected card type? */
1132 if (card[nr] == -1) {
1133 if (zr->revision < 2) {
1134 pci_err(pdev, "No card type specified, please use the card=X module parameter\n");
1135 pci_err(pdev, "It is not possible to auto-detect ZR36057 based cards\n");
1139 card_num = ent->driver_data;
1140 if (card_num >= NUM_CARDS) {
1141 pci_err(pdev, "Unknown card, try specifying card=X module parameter\n");
1144 pci_info(zr->pci_dev, "%s() - card %s detected\n", __func__, zoran_cards[card_num].name);
1146 card_num = card[nr];
1147 if (card_num >= NUM_CARDS || card_num < 0) {
1148 pci_err(pdev, "User specified card type %d out of range (0 .. %d)\n",
1149 card_num, NUM_CARDS - 1);
1155 * even though we make this a non pointer and thus
1156 * theoretically allow for making changes to this struct
1157 * on a per-individual card basis at runtime, this is
1158 * strongly discouraged. This structure is intended to
1159 * keep general card information, no settings or anything
1161 zr->card = zoran_cards[card_num];
1162 snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)), "%s[%u]",
1163 zr->card.name, zr->id);
1165 err = pci_request_regions(pdev, ZR_DEVNAME(zr));
1169 zr->zr36057_mem = devm_ioremap(&pdev->dev, pci_resource_start(pdev, 0), pci_resource_len(pdev, 0));
1170 if (!zr->zr36057_mem) {
1171 pci_err(pdev, "%s() - ioremap failed\n", __func__);
1172 goto zr_pci_release;
1175 result = pci_request_irq(pdev, 0, zoran_irq, NULL, zr, ZR_DEVNAME(zr));
1177 if (result == -EINVAL) {
1178 pci_err(pdev, "%s - bad IRQ number or handler\n", __func__);
1179 } else if (result == -EBUSY) {
1180 pci_err(pdev, "%s - IRQ %d busy, change your PnP config in BIOS\n",
1181 __func__, zr->pci_dev->irq);
1183 pci_err(pdev, "%s - cannot assign IRQ, error code %d\n", __func__, result);
1185 goto zr_pci_release;
1188 /* set PCI latency timer */
1189 pci_read_config_byte(zr->pci_dev, PCI_LATENCY_TIMER,
1191 need_latency = zr->revision > 1 ? 32 : 48;
1192 if (latency != need_latency) {
1193 pci_info(zr->pci_dev, "Changing PCI latency from %d to %d\n", latency, need_latency);
1194 pci_write_config_byte(zr->pci_dev, PCI_LATENCY_TIMER, need_latency);
1197 zr36057_restart(zr);
1199 pci_info(zr->pci_dev, "Initializing i2c bus...\n");
1201 if (zoran_register_i2c(zr) < 0) {
1202 pci_err(pdev, "%s - can't initialize i2c bus\n", __func__);
1206 zr->decoder = v4l2_i2c_new_subdev(&zr->v4l2_dev, &zr->i2c_adapter,
1207 zr->card.i2c_decoder, 0,
1208 zr->card.addrs_decoder);
1210 if (zr->card.i2c_encoder)
1211 zr->encoder = v4l2_i2c_new_subdev(&zr->v4l2_dev, &zr->i2c_adapter,
1212 zr->card.i2c_encoder, 0,
1213 zr->card.addrs_encoder);
1215 pci_info(zr->pci_dev, "Initializing videocodec bus...\n");
1217 if (zr->card.video_codec) {
1218 codec_name = codecid_to_modulename(zr->card.video_codec);
1220 result = request_module(codec_name);
1222 pci_err(pdev, "failed to load modules %s: %d\n", codec_name, result);
1225 if (zr->card.video_vfe) {
1226 vfe_name = codecid_to_modulename(zr->card.video_vfe);
1228 result = request_module(vfe_name);
1230 pci_err(pdev, "failed to load modules %s: %d\n", vfe_name, result);
1234 /* reset JPEG codec */
1235 jpeg_codec_sleep(zr, 1);
1236 jpeg_codec_reset(zr);
1237 /* video bus enabled */
1238 /* display codec revision */
1239 if (zr->card.video_codec != 0) {
1240 master_codec = zoran_setup_videocodec(zr, zr->card.video_codec);
1243 zr->codec = videocodec_attach(master_codec);
1245 pci_err(pdev, "%s - no codec found\n", __func__);
1248 if (zr->codec->type != zr->card.video_codec) {
1249 pci_err(pdev, "%s - wrong codec\n", __func__);
1250 goto zr_detach_codec;
1253 if (zr->card.video_vfe != 0) {
1254 master_vfe = zoran_setup_videocodec(zr, zr->card.video_vfe);
1256 goto zr_detach_codec;
1257 zr->vfe = videocodec_attach(master_vfe);
1259 pci_err(pdev, "%s - no VFE found\n", __func__);
1260 goto zr_detach_codec;
1262 if (zr->vfe->type != zr->card.video_vfe) {
1263 pci_err(pdev, "%s = wrong VFE\n", __func__);
1268 /* take care of Natoma chipset and a revision 1 zr36057 */
1269 if ((pci_pci_problems & PCIPCI_NATOMA) && zr->revision <= 1)
1270 pci_info(zr->pci_dev, "ZR36057/Natoma bug, max. buffer size is 128K\n");
1272 if (zr36057_init(zr) < 0)
1275 zr->map_mode = ZORAN_MAP_MODE_RAW;
1280 videocodec_detach(zr->vfe);
1282 videocodec_detach(zr->codec);
1284 zoran_unregister_i2c(zr);
1286 btwrite(0, ZR36057_SPGPPCR);
1287 pci_free_irq(zr->pci_dev, 0, zr);
1289 pci_release_regions(pdev);
1291 v4l2_ctrl_handler_free(&zr->hdl);
1292 v4l2_device_unregister(&zr->v4l2_dev);
1298 static struct pci_driver zoran_driver = {
1300 .id_table = zr36067_pci_tbl,
1301 .probe = zoran_probe,
1302 .remove = zoran_remove,
1305 static int __init zoran_init(void)
1309 pr_info("Zoran MJPEG board driver version %s\n", ZORAN_VERSION);
1311 /* check the parameters we have been given, adjust if necessary */
1314 if (v4l_nbufs > VIDEO_MAX_FRAME)
1315 v4l_nbufs = VIDEO_MAX_FRAME;
1316 /* The user specifies the in KB, we want them in byte (and page aligned) */
1317 v4l_bufsize = PAGE_ALIGN(v4l_bufsize * 1024);
1318 if (v4l_bufsize < 32768)
1319 v4l_bufsize = 32768;
1320 /* 2 MB is arbitrary but sufficient for the maximum possible images */
1321 if (v4l_bufsize > 2048 * 1024)
1322 v4l_bufsize = 2048 * 1024;
1325 if (jpg_nbufs > BUZ_MAX_FRAME)
1326 jpg_nbufs = BUZ_MAX_FRAME;
1327 jpg_bufsize = PAGE_ALIGN(jpg_bufsize * 1024);
1328 if (jpg_bufsize < 8192)
1330 if (jpg_bufsize > (512 * 1024))
1331 jpg_bufsize = 512 * 1024;
1332 /* Use parameter for vidmem or try to find a video card */
1334 pr_info("%s: Using supplied video memory base address @ 0x%lx\n", ZORAN_NAME, vidmem);
1336 /* some mainboards might not do PCI-PCI data transfer well */
1337 if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1338 pr_warn("%s: chipset does not support reliable PCI-PCI DMA\n", ZORAN_NAME);
1340 res = pci_register_driver(&zoran_driver);
1342 pr_err("Unable to register ZR36057 driver\n");
1349 static void __exit zoran_exit(void)
1351 pci_unregister_driver(&zoran_driver);
1354 module_init(zoran_init);
1355 module_exit(zoran_exit);