1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Zoran zr36057/zr36067 PCI controller driver, for the
4  * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
5  * Media Labs LML33/LML33R10.
6  *
7  * This part handles card-specific data and detection
8  *
9  * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
10  */
11 
12 #include <linux/delay.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 
17 #include <linux/i2c.h>
18 #include <linux/i2c-algo-bit.h>
19 #include <linux/videodev2.h>
20 #include <linux/spinlock.h>
21 
22 #include <linux/pci.h>
23 #include <linux/interrupt.h>
24 #include <linux/io.h>
25 #include <media/v4l2-common.h>
26 #include <media/i2c/bt819.h>
27 
28 #include "videocodec.h"
29 #include "zoran.h"
30 #include "zoran_card.h"
31 #include "zoran_device.h"
32 #include "zr36016.h"
33 #include "zr36050.h"
34 #include "zr36060.h"
35 
36 extern const struct zoran_format zoran_formats[];
37 
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");
41 
42 /* Default input and video norm at startup of the driver. */
43 
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)");
48 
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)");
53 
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)");
57 
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)");
62 
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) */
65 int pass_through;
66 module_param(pass_through, int, 0644);
67 MODULE_PARM_DESC(pass_through,
68 		 "Pass TV signal through to TV-out when idling");
69 
70 int zr36067_debug = 1;
71 module_param_named(debug, zr36067_debug, int, 0644);
72 MODULE_PARM_DESC(debug, "Debug level (0-5)");
73 
74 #define ZORAN_VERSION "0.10.1"
75 
76 MODULE_DESCRIPTION("Zoran-36057/36067 JPEG codec driver");
77 MODULE_AUTHOR("Serguei Miridonov");
78 MODULE_LICENSE("GPL");
79 MODULE_VERSION(ZORAN_VERSION);
80 
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) }
84 
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),
91 	{0}
92 };
93 MODULE_DEVICE_TABLE(pci, zr36067_pci_tbl);
94 
95 static unsigned int zoran_num;		/* number of cards found */
96 
97 /* videocodec bus functions ZR36060 */
98 static u32 zr36060_read(struct videocodec *codec, u16 reg)
99 {
100 	struct zoran *zr = (struct zoran *)codec->master_data->data;
101 	__u32 data;
102 
103 	if (post_office_wait(zr) || post_office_write(zr, 0, 1, reg >> 8) ||
104 	    post_office_write(zr, 0, 2, reg & 0xff))
105 		return -1;
106 
107 	data = post_office_read(zr, 0, 3) & 0xff;
108 	return data;
109 }
110 
111 static void zr36060_write(struct videocodec *codec, u16 reg, u32 val)
112 {
113 	struct zoran *zr = (struct zoran *)codec->master_data->data;
114 
115 	if (post_office_wait(zr) || post_office_write(zr, 0, 1, reg >> 8) ||
116 	    post_office_write(zr, 0, 2, reg & 0xff))
117 		return;
118 
119 	post_office_write(zr, 0, 3, val & 0xff);
120 }
121 
122 /* videocodec bus functions ZR36050 */
123 static u32 zr36050_read(struct videocodec *codec, u16 reg)
124 {
125 	struct zoran *zr = (struct zoran *)codec->master_data->data;
126 	__u32 data;
127 
128 	if (post_office_wait(zr) || post_office_write(zr, 1, 0, reg >> 2))	// reg. HIGHBYTES
129 		return -1;
130 
131 	data = post_office_read(zr, 0, reg & 0x03) & 0xff;	// reg. LOWBYTES + read
132 	return data;
133 }
134 
135 static void zr36050_write(struct videocodec *codec, u16 reg, u32 val)
136 {
137 	struct zoran *zr = (struct zoran *)codec->master_data->data;
138 
139 	if (post_office_wait(zr) || post_office_write(zr, 1, 0, reg >> 2))	// reg. HIGHBYTES
140 		return;
141 
142 	post_office_write(zr, 0, reg & 0x03, val & 0xff);	// reg. LOWBYTES + wr. data
143 }
144 
145 /* videocodec bus functions ZR36016 */
146 static u32 zr36016_read(struct videocodec *codec, u16 reg)
147 {
148 	struct zoran *zr = (struct zoran *)codec->master_data->data;
149 	__u32 data;
150 
151 	if (post_office_wait(zr))
152 		return -1;
153 
154 	data = post_office_read(zr, 2, reg & 0x03) & 0xff;	// read
155 	return data;
156 }
157 
158 /* hack for in zoran_device.c */
159 void zr36016_write(struct videocodec *codec, u16 reg, u32 val)
160 {
161 	struct zoran *zr = (struct zoran *)codec->master_data->data;
162 
163 	if (post_office_wait(zr))
164 		return;
165 
166 	post_office_write(zr, 2, reg & 0x03, val & 0x0ff);	// wr. data
167 }
168 
169 /*
170  * Board specific information
171  */
172 
173 static void dc10_init(struct zoran *zr)
174 {
175 	/* Pixel clock selection */
176 	GPIO(zr, 4, 0);
177 	GPIO(zr, 5, 1);
178 	/* Enable the video bus sync signals */
179 	GPIO(zr, 7, 0);
180 }
181 
182 static void dc10plus_init(struct zoran *zr)
183 {
184 }
185 
186 static void buz_init(struct zoran *zr)
187 {
188 	/* some stuff from Iomega */
189 	pci_write_config_dword(zr->pci_dev, 0xfc, 0x90680f15);
190 	pci_write_config_dword(zr->pci_dev, 0x0c, 0x00012020);
191 	pci_write_config_dword(zr->pci_dev, 0xe8, 0xc0200000);
192 }
193 
194 static void lml33_init(struct zoran *zr)
195 {
196 	GPIO(zr, 2, 1);		// Set Composite input/output
197 }
198 
199 static void avs6eyes_init(struct zoran *zr)
200 {
201 	// AverMedia 6-Eyes original driver by Christer Weinigel
202 
203 	// Lifted straight from Christer's old driver and
204 	// modified slightly by Martin Samuelsson.
205 
206 	int mux = default_mux; /* 1 = BT866, 7 = VID1 */
207 
208 	GPIO(zr, 4, 1); /* Bt866 SLEEP on */
209 	udelay(2);
210 
211 	GPIO(zr, 0, 1); /* ZR36060 /RESET on */
212 	GPIO(zr, 1, 0); /* ZR36060 /SLEEP on */
213 	GPIO(zr, 2, mux & 1);   /* MUX S0 */
214 	GPIO(zr, 3, 0); /* /FRAME on */
215 	GPIO(zr, 4, 0); /* Bt866 SLEEP off */
216 	GPIO(zr, 5, mux & 2);   /* MUX S1 */
217 	GPIO(zr, 6, 0); /* ? */
218 	GPIO(zr, 7, mux & 4);   /* MUX S2 */
219 }
220 
221 static const char *codecid_to_modulename(u16 codecid)
222 {
223 	const char *name = NULL;
224 
225 	switch (codecid) {
226 	case CODEC_TYPE_ZR36060:
227 		name = "zr36060";
228 		break;
229 	case CODEC_TYPE_ZR36050:
230 		name = "zr36050";
231 		break;
232 	case CODEC_TYPE_ZR36016:
233 		name = "zr36016";
234 		break;
235 	}
236 
237 	return name;
238 }
239 
240 static int codec_init(struct zoran *zr, u16 codecid)
241 {
242 	switch (codecid) {
243 	case CODEC_TYPE_ZR36060:
244 #ifdef CONFIG_VIDEO_ZORAN_ZR36060
245 		return zr36060_init_module();
246 #else
247 		pci_err(zr->pci_dev, "ZR36060 support is not enabled\n");
248 		return -EINVAL;
249 #endif
250 		break;
251 	case CODEC_TYPE_ZR36050:
252 #ifdef CONFIG_VIDEO_ZORAN_DC30
253 		return zr36050_init_module();
254 #else
255 		pci_err(zr->pci_dev, "ZR36050 support is not enabled\n");
256 		return -EINVAL;
257 #endif
258 		break;
259 	case CODEC_TYPE_ZR36016:
260 #ifdef CONFIG_VIDEO_ZORAN_DC30
261 		return zr36016_init_module();
262 #else
263 		pci_err(zr->pci_dev, "ZR36016 support is not enabled\n");
264 		return -EINVAL;
265 #endif
266 		break;
267 	}
268 
269 	pci_err(zr->pci_dev, "unknown codec id %x\n", codecid);
270 	return -EINVAL;
271 }
272 
273 static void codec_exit(struct zoran *zr, u16 codecid)
274 {
275 	switch (codecid) {
276 	case CODEC_TYPE_ZR36060:
277 #ifdef CONFIG_VIDEO_ZORAN_ZR36060
278 		zr36060_cleanup_module();
279 #endif
280 		break;
281 	case CODEC_TYPE_ZR36050:
282 #ifdef CONFIG_VIDEO_ZORAN_DC30
283 		zr36050_cleanup_module();
284 #endif
285 		break;
286 	case CODEC_TYPE_ZR36016:
287 #ifdef CONFIG_VIDEO_ZORAN_DC30
288 		zr36016_cleanup_module();
289 #endif
290 		break;
291 	}
292 }
293 
294 static int videocodec_init(struct zoran *zr)
295 {
296 	const char *codec_name, *vfe_name;
297 	int result;
298 
299 	codec_name = codecid_to_modulename(zr->card.video_codec);
300 	if (codec_name) {
301 		result = codec_init(zr, zr->card.video_codec);
302 		if (result < 0) {
303 			pci_err(zr->pci_dev, "failed to load video codec %s: %d\n",
304 				codec_name, result);
305 			return result;
306 		}
307 	}
308 	vfe_name = codecid_to_modulename(zr->card.video_vfe);
309 	if (vfe_name) {
310 		result = codec_init(zr, zr->card.video_vfe);
311 		if (result < 0) {
312 			pci_err(zr->pci_dev, "failed to load video vfe %s: %d\n",
313 				vfe_name, result);
314 			if (codec_name)
315 				codec_exit(zr, zr->card.video_codec);
316 			return result;
317 		}
318 	}
319 	return 0;
320 }
321 
322 static void videocodec_exit(struct zoran *zr)
323 {
324 	if (zr->card.video_codec != CODEC_TYPE_NONE)
325 		codec_exit(zr, zr->card.video_codec);
326 	if (zr->card.video_vfe != CODEC_TYPE_NONE)
327 		codec_exit(zr, zr->card.video_vfe);
328 }
329 
330 static const struct tvnorm f50sqpixel = { 944, 768, 83, 880, 625, 576, 16 };
331 static const struct tvnorm f60sqpixel = { 780, 640, 51, 716, 525, 480, 12 };
332 static const struct tvnorm f50ccir601 = { 864, 720, 75, 804, 625, 576, 18 };
333 static const struct tvnorm f60ccir601 = { 858, 720, 57, 788, 525, 480, 16 };
334 
335 static const struct tvnorm f50ccir601_lml33 = { 864, 720, 75 + 34, 804, 625, 576, 18 };
336 static const struct tvnorm f60ccir601_lml33 = { 858, 720, 57 + 34, 788, 525, 480, 16 };
337 
338 /* The DC10 (57/16/50) uses VActive as HSync, so h_start must be 0 */
339 static const struct tvnorm f50sqpixel_dc10 = { 944, 768, 0, 880, 625, 576, 0 };
340 static const struct tvnorm f60sqpixel_dc10 = { 780, 640, 0, 716, 525, 480, 12 };
341 
342 /*
343  * FIXME: I cannot swap U and V in saa7114, so i do one pixel left shift in zoran (75 -> 74)
344  * (Maxim Yevtyushkin <max@linuxmedialabs.com>)
345  */
346 static const struct tvnorm f50ccir601_lm33r10 = { 864, 720, 74 + 54, 804, 625, 576, 18 };
347 static const struct tvnorm f60ccir601_lm33r10 = { 858, 720, 56 + 54, 788, 525, 480, 16 };
348 
349 /*
350  * FIXME: The ks0127 seem incapable of swapping U and V, too, which is why I copy Maxim's left
351  * shift hack for the 6 Eyes.
352  *
353  * Christer's driver used the unshifted norms, though...
354  * /Sam
355  */
356 static const struct tvnorm f50ccir601_avs6eyes = { 864, 720, 74, 804, 625, 576, 18 };
357 static const struct tvnorm f60ccir601_avs6eyes = { 858, 720, 56, 788, 525, 480, 16 };
358 
359 static const unsigned short vpx3220_addrs[] = { 0x43, 0x47, I2C_CLIENT_END };
360 static const unsigned short saa7110_addrs[] = { 0x4e, 0x4f, I2C_CLIENT_END };
361 static const unsigned short saa7111_addrs[] = { 0x25, 0x24, I2C_CLIENT_END };
362 static const unsigned short saa7114_addrs[] = { 0x21, 0x20, I2C_CLIENT_END };
363 static const unsigned short adv717x_addrs[] = { 0x6a, 0x6b, 0x2a, 0x2b, I2C_CLIENT_END };
364 static const unsigned short ks0127_addrs[] = { 0x6c, 0x6d, I2C_CLIENT_END };
365 static const unsigned short saa7185_addrs[] = { 0x44, I2C_CLIENT_END };
366 static const unsigned short bt819_addrs[] = { 0x45, I2C_CLIENT_END };
367 static const unsigned short bt856_addrs[] = { 0x44, I2C_CLIENT_END };
368 static const unsigned short bt866_addrs[] = { 0x44, I2C_CLIENT_END };
369 
370 static struct card_info zoran_cards[NUM_CARDS] = {
371 	{
372 		.type = DC10_OLD,
373 		.name = "DC10(old)",
374 		.i2c_decoder = "vpx3220a",
375 		.addrs_decoder = vpx3220_addrs,
376 		.video_codec = CODEC_TYPE_ZR36050,
377 		.video_vfe = CODEC_TYPE_ZR36016,
378 
379 		.inputs = 3,
380 		.input = {
381 			{ 1, "Composite" },
382 			{ 2, "S-Video" },
383 			{ 0, "Internal/comp" }
384 		},
385 		.norms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
386 		.tvn = {
387 			&f50sqpixel_dc10,
388 			&f60sqpixel_dc10,
389 			&f50sqpixel_dc10
390 		},
391 		.jpeg_int = 0,
392 		.vsync_int = ZR36057_ISR_GIRQ1,
393 		.gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
394 		.gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
395 		.gpcs = { -1, 0 },
396 		.vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
397 		.gws_not_connected = 0,
398 		.input_mux = 0,
399 		.init = &dc10_init,
400 	}, {
401 		.type = DC10_NEW,
402 		.name = "DC10(new)",
403 		.i2c_decoder = "saa7110",
404 		.addrs_decoder = saa7110_addrs,
405 		.i2c_encoder = "adv7175",
406 		.addrs_encoder = adv717x_addrs,
407 		.video_codec = CODEC_TYPE_ZR36060,
408 
409 		.inputs = 3,
410 		.input = {
411 				{ 0, "Composite" },
412 				{ 7, "S-Video" },
413 				{ 5, "Internal/comp" }
414 			},
415 		.norms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
416 		.tvn = {
417 				&f50sqpixel,
418 				&f60sqpixel,
419 				&f50sqpixel},
420 		.jpeg_int = ZR36057_ISR_GIRQ0,
421 		.vsync_int = ZR36057_ISR_GIRQ1,
422 		.gpio = { 3, 0, 6, 1, 2, -1, 4, 5 },
423 		.gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
424 		.gpcs = { -1, 1},
425 		.vfe_pol = { 1, 1, 1, 1, 0, 0, 0, 0 },
426 		.gws_not_connected = 0,
427 		.input_mux = 0,
428 		.init = &dc10plus_init,
429 	}, {
430 		.type = DC10_PLUS,
431 		.name = "DC10_PLUS",
432 		.i2c_decoder = "saa7110",
433 		.addrs_decoder = saa7110_addrs,
434 		.i2c_encoder = "adv7175",
435 		.addrs_encoder = adv717x_addrs,
436 		.video_codec = CODEC_TYPE_ZR36060,
437 
438 		.inputs = 3,
439 		.input = {
440 			{ 0, "Composite" },
441 			{ 7, "S-Video" },
442 			{ 5, "Internal/comp" }
443 		},
444 		.norms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
445 		.tvn = {
446 			&f50sqpixel,
447 			&f60sqpixel,
448 			&f50sqpixel
449 		},
450 		.jpeg_int = ZR36057_ISR_GIRQ0,
451 		.vsync_int = ZR36057_ISR_GIRQ1,
452 		.gpio = { 3, 0, 6, 1, 2, -1, 4, 5 },
453 		.gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
454 		.gpcs = { -1, 1 },
455 		.vfe_pol = { 1, 1, 1, 1, 0, 0, 0, 0 },
456 		.gws_not_connected = 0,
457 		.input_mux = 0,
458 		.init = &dc10plus_init,
459 	}, {
460 		.type = DC30,
461 		.name = "DC30",
462 		.i2c_decoder = "vpx3220a",
463 		.addrs_decoder = vpx3220_addrs,
464 		.i2c_encoder = "adv7175",
465 		.addrs_encoder = adv717x_addrs,
466 		.video_codec = CODEC_TYPE_ZR36050,
467 		.video_vfe = CODEC_TYPE_ZR36016,
468 
469 		.inputs = 3,
470 		.input = {
471 			{ 1, "Composite" },
472 			{ 2, "S-Video" },
473 			{ 0, "Internal/comp" }
474 		},
475 		.norms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
476 		.tvn = {
477 			&f50sqpixel_dc10,
478 			&f60sqpixel_dc10,
479 			&f50sqpixel_dc10
480 		},
481 		.jpeg_int = 0,
482 		.vsync_int = ZR36057_ISR_GIRQ1,
483 		.gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
484 		.gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
485 		.gpcs = { -1, 0 },
486 		.vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
487 		.gws_not_connected = 0,
488 		.input_mux = 0,
489 		.init = &dc10_init,
490 	}, {
491 		.type = DC30_PLUS,
492 		.name = "DC30_PLUS",
493 		.i2c_decoder = "vpx3220a",
494 		.addrs_decoder = vpx3220_addrs,
495 		.i2c_encoder = "adv7175",
496 		.addrs_encoder = adv717x_addrs,
497 		.video_codec = CODEC_TYPE_ZR36050,
498 		.video_vfe = CODEC_TYPE_ZR36016,
499 
500 		.inputs = 3,
501 		.input = {
502 			{ 1, "Composite" },
503 			{ 2, "S-Video" },
504 			{ 0, "Internal/comp" }
505 		},
506 		.norms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
507 		.tvn = {
508 			&f50sqpixel_dc10,
509 			&f60sqpixel_dc10,
510 			&f50sqpixel_dc10
511 		},
512 		.jpeg_int = 0,
513 		.vsync_int = ZR36057_ISR_GIRQ1,
514 		.gpio = { 2, 1, -1, 3, 7, 0, 4, 5 },
515 		.gpio_pol = { 0, 0, 0, 1, 0, 0, 0, 0 },
516 		.gpcs = { -1, 0 },
517 		.vfe_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
518 		.gws_not_connected = 0,
519 		.input_mux = 0,
520 		.init = &dc10_init,
521 	}, {
522 		.type = LML33,
523 		.name = "LML33",
524 		.i2c_decoder = "bt819a",
525 		.addrs_decoder = bt819_addrs,
526 		.i2c_encoder = "bt856",
527 		.addrs_encoder = bt856_addrs,
528 		.video_codec = CODEC_TYPE_ZR36060,
529 
530 		.inputs = 2,
531 		.input = {
532 			{ 0, "Composite" },
533 			{ 7, "S-Video" }
534 		},
535 		.norms = V4L2_STD_NTSC | V4L2_STD_PAL,
536 		.tvn = {
537 			&f50ccir601_lml33,
538 			&f60ccir601_lml33,
539 			NULL
540 		},
541 		.jpeg_int = ZR36057_ISR_GIRQ1,
542 		.vsync_int = ZR36057_ISR_GIRQ0,
543 		.gpio = { 1, -1, 3, 5, 7, -1, -1, -1 },
544 		.gpio_pol = { 0, 0, 0, 0, 1, 0, 0, 0 },
545 		.gpcs = { 3, 1 },
546 		.vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
547 		.gws_not_connected = 1,
548 		.input_mux = 0,
549 		.init = &lml33_init,
550 	}, {
551 		.type = LML33R10,
552 		.name = "LML33R10",
553 		.i2c_decoder = "saa7114",
554 		.addrs_decoder = saa7114_addrs,
555 		.i2c_encoder = "adv7170",
556 		.addrs_encoder = adv717x_addrs,
557 		.video_codec = CODEC_TYPE_ZR36060,
558 
559 		.inputs = 2,
560 		.input = {
561 			{ 0, "Composite" },
562 			{ 7, "S-Video" }
563 		},
564 		.norms = V4L2_STD_NTSC | V4L2_STD_PAL,
565 		.tvn = {
566 			&f50ccir601_lm33r10,
567 			&f60ccir601_lm33r10,
568 			NULL
569 		},
570 		.jpeg_int = ZR36057_ISR_GIRQ1,
571 		.vsync_int = ZR36057_ISR_GIRQ0,
572 		.gpio = { 1, -1, 3, 5, 7, -1, -1, -1 },
573 		.gpio_pol = { 0, 0, 0, 0, 1, 0, 0, 0 },
574 		.gpcs = { 3, 1 },
575 		.vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
576 		.gws_not_connected = 1,
577 		.input_mux = 0,
578 		.init = &lml33_init,
579 	}, {
580 		.type = BUZ,
581 		.name = "Buz",
582 		.i2c_decoder = "saa7111",
583 		.addrs_decoder = saa7111_addrs,
584 		.i2c_encoder = "saa7185",
585 		.addrs_encoder = saa7185_addrs,
586 		.video_codec = CODEC_TYPE_ZR36060,
587 
588 		.inputs = 2,
589 		.input = {
590 			{ 3, "Composite" },
591 			{ 7, "S-Video" }
592 		},
593 		.norms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM,
594 		.tvn = {
595 			&f50ccir601,
596 			&f60ccir601,
597 			&f50ccir601
598 		},
599 		.jpeg_int = ZR36057_ISR_GIRQ1,
600 		.vsync_int = ZR36057_ISR_GIRQ0,
601 		.gpio = { 1, -1, 3, -1, -1, -1, -1, -1 },
602 		.gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 },
603 		.gpcs = { 3, 1 },
604 		.vfe_pol = { 1, 1, 0, 0, 0, 1, 0, 0 },
605 		.gws_not_connected = 1,
606 		.input_mux = 0,
607 		.init = &buz_init,
608 	}, {
609 		.type = AVS6EYES,
610 		.name = "6-Eyes",
611 		/*
612 		 * AverMedia chose not to brand the 6-Eyes. Thus it can't be
613 		 * autodetected, and requires card=x.
614 		 */
615 		.i2c_decoder = "ks0127",
616 		.addrs_decoder = ks0127_addrs,
617 		.i2c_encoder = "bt866",
618 		.addrs_encoder = bt866_addrs,
619 		.video_codec = CODEC_TYPE_ZR36060,
620 
621 		.inputs = 10,
622 		.input = {
623 			{ 0, "Composite 1" },
624 			{ 1, "Composite 2" },
625 			{ 2, "Composite 3" },
626 			{ 4, "Composite 4" },
627 			{ 5, "Composite 5" },
628 			{ 6, "Composite 6" },
629 			{ 8, "S-Video 1" },
630 			{ 9, "S-Video 2" },
631 			{10, "S-Video 3" },
632 			{15, "YCbCr" }
633 		},
634 		.norms = V4L2_STD_NTSC | V4L2_STD_PAL,
635 		.tvn = {
636 			&f50ccir601_avs6eyes,
637 			&f60ccir601_avs6eyes,
638 			NULL
639 		},
640 		.jpeg_int = ZR36057_ISR_GIRQ1,
641 		.vsync_int = ZR36057_ISR_GIRQ0,
642 		.gpio = { 1, 0, 3, -1, -1, -1, -1, -1 },// Validity unknown /Sam
643 		.gpio_pol = { 0, 0, 0, 0, 0, 0, 0, 0 }, // Validity unknown /Sam
644 		.gpcs = { 3, 1 },			// Validity unknown /Sam
645 		.vfe_pol = { 1, 0, 0, 0, 0, 1, 0, 0 },  // Validity unknown /Sam
646 		.gws_not_connected = 1,
647 		.input_mux = 1,
648 		.init = &avs6eyes_init,
649 	}
650 
651 };
652 
653 /*
654  * I2C functions
655  */
656 /* software I2C functions */
657 static int zoran_i2c_getsda(void *data)
658 {
659 	struct zoran *zr = (struct zoran *)data;
660 
661 	return (btread(ZR36057_I2CBR) >> 1) & 1;
662 }
663 
664 static int zoran_i2c_getscl(void *data)
665 {
666 	struct zoran *zr = (struct zoran *)data;
667 
668 	return btread(ZR36057_I2CBR) & 1;
669 }
670 
671 static void zoran_i2c_setsda(void *data, int state)
672 {
673 	struct zoran *zr = (struct zoran *)data;
674 
675 	if (state)
676 		zr->i2cbr |= 2;
677 	else
678 		zr->i2cbr &= ~2;
679 	btwrite(zr->i2cbr, ZR36057_I2CBR);
680 }
681 
682 static void zoran_i2c_setscl(void *data, int state)
683 {
684 	struct zoran *zr = (struct zoran *)data;
685 
686 	if (state)
687 		zr->i2cbr |= 1;
688 	else
689 		zr->i2cbr &= ~1;
690 	btwrite(zr->i2cbr, ZR36057_I2CBR);
691 }
692 
693 static const struct i2c_algo_bit_data zoran_i2c_bit_data_template = {
694 	.setsda = zoran_i2c_setsda,
695 	.setscl = zoran_i2c_setscl,
696 	.getsda = zoran_i2c_getsda,
697 	.getscl = zoran_i2c_getscl,
698 	.udelay = 10,
699 	.timeout = 100,
700 };
701 
702 static int zoran_register_i2c(struct zoran *zr)
703 {
704 	zr->i2c_algo = zoran_i2c_bit_data_template;
705 	zr->i2c_algo.data = zr;
706 	strscpy(zr->i2c_adapter.name, ZR_DEVNAME(zr),
707 		sizeof(zr->i2c_adapter.name));
708 	i2c_set_adapdata(&zr->i2c_adapter, &zr->v4l2_dev);
709 	zr->i2c_adapter.algo_data = &zr->i2c_algo;
710 	zr->i2c_adapter.dev.parent = &zr->pci_dev->dev;
711 	return i2c_bit_add_bus(&zr->i2c_adapter);
712 }
713 
714 static void zoran_unregister_i2c(struct zoran *zr)
715 {
716 	i2c_del_adapter(&zr->i2c_adapter);
717 }
718 
719 /* Check a zoran_params struct for correctness, insert default params */
720 int zoran_check_jpg_settings(struct zoran *zr,
721 			     struct zoran_jpg_settings *settings, int try)
722 {
723 	int err = 0, err0 = 0;
724 
725 	pci_dbg(zr->pci_dev, "%s - dec: %d, Hdcm: %d, Vdcm: %d, Tdcm: %d\n",
726 		__func__, settings->decimation, settings->hor_dcm,
727 		settings->ver_dcm, settings->tmp_dcm);
728 	pci_dbg(zr->pci_dev, "%s - x: %d, y: %d, w: %d, y: %d\n", __func__,
729 		settings->img_x, settings->img_y,
730 		settings->img_width, settings->img_height);
731 	/* Check decimation, set default values for decimation = 1, 2, 4 */
732 	switch (settings->decimation) {
733 	case 1:
734 
735 		settings->hor_dcm = 1;
736 		settings->ver_dcm = 1;
737 		settings->tmp_dcm = 1;
738 		settings->field_per_buff = 2;
739 		settings->img_x = 0;
740 		settings->img_y = 0;
741 		settings->img_width = BUZ_MAX_WIDTH;
742 		settings->img_height = BUZ_MAX_HEIGHT / 2;
743 		break;
744 	case 2:
745 
746 		settings->hor_dcm = 2;
747 		settings->ver_dcm = 1;
748 		settings->tmp_dcm = 2;
749 		settings->field_per_buff = 1;
750 		settings->img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
751 		settings->img_y = 0;
752 		settings->img_width =
753 		    (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
754 		settings->img_height = BUZ_MAX_HEIGHT / 2;
755 		break;
756 	case 4:
757 
758 		if (zr->card.type == DC10_NEW) {
759 			pci_dbg(zr->pci_dev,
760 				"%s - HDec by 4 is not supported on the DC10\n",
761 				__func__);
762 			err0++;
763 			break;
764 		}
765 
766 		settings->hor_dcm = 4;
767 		settings->ver_dcm = 2;
768 		settings->tmp_dcm = 2;
769 		settings->field_per_buff = 1;
770 		settings->img_x = (BUZ_MAX_WIDTH == 720) ? 8 : 0;
771 		settings->img_y = 0;
772 		settings->img_width =
773 		    (BUZ_MAX_WIDTH == 720) ? 704 : BUZ_MAX_WIDTH;
774 		settings->img_height = BUZ_MAX_HEIGHT / 2;
775 		break;
776 	case 0:
777 
778 		/* We have to check the data the user has set */
779 
780 		if (settings->hor_dcm != 1 && settings->hor_dcm != 2 &&
781 		    (zr->card.type == DC10_NEW || settings->hor_dcm != 4)) {
782 			settings->hor_dcm = clamp(settings->hor_dcm, 1, 2);
783 			err0++;
784 		}
785 		if (settings->ver_dcm != 1 && settings->ver_dcm != 2) {
786 			settings->ver_dcm = clamp(settings->ver_dcm, 1, 2);
787 			err0++;
788 		}
789 		if (settings->tmp_dcm != 1 && settings->tmp_dcm != 2) {
790 			settings->tmp_dcm = clamp(settings->tmp_dcm, 1, 2);
791 			err0++;
792 		}
793 		if (settings->field_per_buff != 1 &&
794 		    settings->field_per_buff != 2) {
795 			settings->field_per_buff = clamp(settings->field_per_buff, 1, 2);
796 			err0++;
797 		}
798 		if (settings->img_x < 0) {
799 			settings->img_x = 0;
800 			err0++;
801 		}
802 		if (settings->img_y < 0) {
803 			settings->img_y = 0;
804 			err0++;
805 		}
806 		if (settings->img_width < 0 || settings->img_width > BUZ_MAX_WIDTH) {
807 			settings->img_width = clamp(settings->img_width, 0, (int)BUZ_MAX_WIDTH);
808 			err0++;
809 		}
810 		if (settings->img_height < 0 || settings->img_height > BUZ_MAX_HEIGHT / 2) {
811 			settings->img_height = clamp(settings->img_height, 0, BUZ_MAX_HEIGHT / 2);
812 			err0++;
813 		}
814 		if (settings->img_x + settings->img_width > BUZ_MAX_WIDTH) {
815 			settings->img_x = BUZ_MAX_WIDTH - settings->img_width;
816 			err0++;
817 		}
818 		if (settings->img_y + settings->img_height > BUZ_MAX_HEIGHT / 2) {
819 			settings->img_y = BUZ_MAX_HEIGHT / 2 - settings->img_height;
820 			err0++;
821 		}
822 		if (settings->img_width % (16 * settings->hor_dcm) != 0) {
823 			settings->img_width -= settings->img_width % (16 * settings->hor_dcm);
824 			if (settings->img_width == 0)
825 				settings->img_width = 16 * settings->hor_dcm;
826 			err0++;
827 		}
828 		if (settings->img_height % (8 * settings->ver_dcm) != 0) {
829 			settings->img_height -= settings->img_height % (8 * settings->ver_dcm);
830 			if (settings->img_height == 0)
831 				settings->img_height = 8 * settings->ver_dcm;
832 			err0++;
833 		}
834 
835 		if (!try && err0) {
836 			pci_err(zr->pci_dev, "%s - error in params for decimation = 0\n", __func__);
837 			err++;
838 		}
839 		break;
840 	default:
841 		pci_err(zr->pci_dev, "%s - decimation = %d, must be 0, 1, 2 or 4\n",
842 			__func__, settings->decimation);
843 		err++;
844 		break;
845 	}
846 
847 	if (settings->jpg_comp.quality > 100)
848 		settings->jpg_comp.quality = 100;
849 	if (settings->jpg_comp.quality < 5)
850 		settings->jpg_comp.quality = 5;
851 	if (settings->jpg_comp.APPn < 0)
852 		settings->jpg_comp.APPn = 0;
853 	if (settings->jpg_comp.APPn > 15)
854 		settings->jpg_comp.APPn = 15;
855 	if (settings->jpg_comp.APP_len < 0)
856 		settings->jpg_comp.APP_len = 0;
857 	if (settings->jpg_comp.APP_len > 60)
858 		settings->jpg_comp.APP_len = 60;
859 	if (settings->jpg_comp.COM_len < 0)
860 		settings->jpg_comp.COM_len = 0;
861 	if (settings->jpg_comp.COM_len > 60)
862 		settings->jpg_comp.COM_len = 60;
863 	if (err)
864 		return -EINVAL;
865 	return 0;
866 }
867 
868 static int zoran_init_video_device(struct zoran *zr, struct video_device *video_dev, int dir)
869 {
870 	int err;
871 
872 	/* Now add the template and register the device unit. */
873 	*video_dev = zoran_template;
874 	video_dev->v4l2_dev = &zr->v4l2_dev;
875 	video_dev->lock = &zr->lock;
876 	video_dev->device_caps = V4L2_CAP_STREAMING | dir;
877 
878 	strscpy(video_dev->name, ZR_DEVNAME(zr), sizeof(video_dev->name));
879 	video_dev->vfl_dir = VFL_DIR_RX;
880 	zoran_queue_init(zr, &zr->vq, V4L2_BUF_TYPE_VIDEO_CAPTURE);
881 
882 	err = video_register_device(video_dev, VFL_TYPE_VIDEO, video_nr[zr->id]);
883 	if (err < 0)
884 		return err;
885 	video_set_drvdata(video_dev, zr);
886 	return 0;
887 }
888 
889 static void zoran_exit_video_devices(struct zoran *zr)
890 {
891 	video_unregister_device(zr->video_dev);
892 	kfree(zr->video_dev);
893 }
894 
895 static int zoran_init_video_devices(struct zoran *zr)
896 {
897 	int err;
898 
899 	zr->video_dev = video_device_alloc();
900 	if (!zr->video_dev)
901 		return -ENOMEM;
902 
903 	err = zoran_init_video_device(zr, zr->video_dev, V4L2_CAP_VIDEO_CAPTURE);
904 	if (err)
905 		kfree(zr->video_dev);
906 	return err;
907 }
908 
909 /*
910  * v4l2_device_unregister() will care about removing zr->encoder/zr->decoder
911  * via v4l2_i2c_subdev_unregister()
912  */
913 static int zoran_i2c_init(struct zoran *zr)
914 {
915 	int err;
916 
917 	pci_info(zr->pci_dev, "Initializing i2c bus...\n");
918 
919 	err = zoran_register_i2c(zr);
920 	if (err) {
921 		pci_err(zr->pci_dev, "%s - cannot initialize i2c bus\n", __func__);
922 		return err;
923 	}
924 
925 	zr->decoder = v4l2_i2c_new_subdev(&zr->v4l2_dev, &zr->i2c_adapter,
926 					  zr->card.i2c_decoder, 0,
927 					  zr->card.addrs_decoder);
928 	if (!zr->decoder) {
929 		pci_err(zr->pci_dev, "Fail to get decoder %s\n", zr->card.i2c_decoder);
930 		err = -EINVAL;
931 		goto error_decoder;
932 	}
933 
934 	if (zr->card.i2c_encoder) {
935 		zr->encoder = v4l2_i2c_new_subdev(&zr->v4l2_dev, &zr->i2c_adapter,
936 						  zr->card.i2c_encoder, 0,
937 						  zr->card.addrs_encoder);
938 		if (!zr->encoder) {
939 			pci_err(zr->pci_dev, "Fail to get encoder %s\n", zr->card.i2c_encoder);
940 			err = -EINVAL;
941 			goto error_decoder;
942 		}
943 	}
944 	return 0;
945 
946 error_decoder:
947 	zoran_unregister_i2c(zr);
948 	return err;
949 }
950 
951 static void zoran_i2c_exit(struct zoran *zr)
952 {
953 	zoran_unregister_i2c(zr);
954 }
955 
956 void zoran_open_init_params(struct zoran *zr)
957 {
958 	int i;
959 
960 	zr->v4l_settings.width = 192;
961 	zr->v4l_settings.height = 144;
962 	zr->v4l_settings.format = &zoran_formats[7];	/* YUY2 - YUV-4:2:2 packed */
963 	zr->v4l_settings.bytesperline = zr->v4l_settings.width *
964 		((zr->v4l_settings.format->depth + 7) / 8);
965 
966 	/* Set necessary params and call zoran_check_jpg_settings to set the defaults */
967 	zr->jpg_settings.decimation = 1;
968 	zr->jpg_settings.jpg_comp.quality = 50;	/* default compression factor 8 */
969 	if (zr->card.type != BUZ)
970 		zr->jpg_settings.odd_even = 1;
971 	else
972 		zr->jpg_settings.odd_even = 0;
973 	zr->jpg_settings.jpg_comp.APPn = 0;
974 	zr->jpg_settings.jpg_comp.APP_len = 0;	/* No APPn marker */
975 	memset(zr->jpg_settings.jpg_comp.APP_data, 0,
976 	       sizeof(zr->jpg_settings.jpg_comp.APP_data));
977 	zr->jpg_settings.jpg_comp.COM_len = 0;	/* No COM marker */
978 	memset(zr->jpg_settings.jpg_comp.COM_data, 0,
979 	       sizeof(zr->jpg_settings.jpg_comp.COM_data));
980 	zr->jpg_settings.jpg_comp.jpeg_markers =
981 	    V4L2_JPEG_MARKER_DHT | V4L2_JPEG_MARKER_DQT;
982 	i = zoran_check_jpg_settings(zr, &zr->jpg_settings, 0);
983 	if (i)
984 		pci_err(zr->pci_dev, "%s internal error\n", __func__);
985 
986 	zr->buffer_size = zr->v4l_settings.bytesperline * zr->v4l_settings.height;
987 
988 	clear_interrupt_counters(zr);
989 }
990 
991 static int zr36057_init(struct zoran *zr)
992 {
993 	int j, err;
994 
995 	pci_info(zr->pci_dev, "initializing card[%d]\n", zr->id);
996 
997 	/* Avoid nonsense settings from user for default input/norm */
998 	if (default_norm < 0 || default_norm > 2)
999 		default_norm = 0;
1000 	if (default_norm == 0) {
1001 		zr->norm = V4L2_STD_PAL;
1002 		zr->timing = zr->card.tvn[ZR_NORM_PAL];
1003 	} else if (default_norm == 1) {
1004 		zr->norm = V4L2_STD_NTSC;
1005 		zr->timing = zr->card.tvn[ZR_NORM_NTSC];
1006 	} else {
1007 		zr->norm = V4L2_STD_SECAM;
1008 		zr->timing = zr->card.tvn[ZR_NORM_SECAM];
1009 	}
1010 	if (!zr->timing) {
1011 		pci_warn(zr->pci_dev,
1012 			 "%s - default TV standard not supported by hardware. PAL will be used.\n",
1013 			 __func__);
1014 		zr->norm = V4L2_STD_PAL;
1015 		zr->timing = zr->card.tvn[ZR_NORM_PAL];
1016 	}
1017 
1018 	if (default_input > zr->card.inputs - 1) {
1019 		pci_warn(zr->pci_dev, "default_input value %d out of range (0-%d)\n",
1020 			 default_input, zr->card.inputs - 1);
1021 		default_input = 0;
1022 	}
1023 	zr->input = default_input;
1024 
1025 	/* default setup (will be repeated at every open) */
1026 	zoran_open_init_params(zr);
1027 
1028 	/* allocate memory *before* doing anything to the hardware in case allocation fails */
1029 	zr->stat_com = dma_alloc_coherent(&zr->pci_dev->dev,
1030 					  BUZ_NUM_STAT_COM * sizeof(u32),
1031 					  &zr->p_sc, GFP_KERNEL);
1032 	if (!zr->stat_com)
1033 		return -ENOMEM;
1034 
1035 	for (j = 0; j < BUZ_NUM_STAT_COM; j++)
1036 		zr->stat_com[j] = cpu_to_le32(1); /* mark as unavailable to zr36057 */
1037 
1038 	zr->stat_comb = dma_alloc_coherent(&zr->pci_dev->dev,
1039 					   BUZ_NUM_STAT_COM * sizeof(u32) * 2,
1040 					   &zr->p_scb, GFP_KERNEL);
1041 	if (!zr->stat_comb) {
1042 		err = -ENOMEM;
1043 		goto exit_statcom;
1044 	}
1045 
1046 	err = zoran_init_video_devices(zr);
1047 	if (err)
1048 		goto exit_statcomb;
1049 
1050 	zoran_init_hardware(zr);
1051 	if (!pass_through) {
1052 		decoder_call(zr, video, s_stream, 0);
1053 		encoder_call(zr, video, s_routing, 2, 0, 0);
1054 	}
1055 
1056 	zr->initialized = 1;
1057 	return 0;
1058 
1059 exit_statcomb:
1060 	dma_free_coherent(&zr->pci_dev->dev, BUZ_NUM_STAT_COM * sizeof(u32) * 2,
1061 			  zr->stat_comb, zr->p_scb);
1062 exit_statcom:
1063 	dma_free_coherent(&zr->pci_dev->dev, BUZ_NUM_STAT_COM * sizeof(u32),
1064 			  zr->stat_com, zr->p_sc);
1065 	return err;
1066 }
1067 
1068 static void zoran_remove(struct pci_dev *pdev)
1069 {
1070 	struct v4l2_device *v4l2_dev = dev_get_drvdata(&pdev->dev);
1071 	struct zoran *zr = to_zoran(v4l2_dev);
1072 
1073 	if (!zr->initialized)
1074 		goto exit_free;
1075 
1076 	debugfs_remove_recursive(zr->dbgfs_dir);
1077 
1078 	zoran_queue_exit(zr);
1079 
1080 	/* unregister videocodec bus */
1081 	if (zr->codec)
1082 		videocodec_detach(zr->codec);
1083 	if (zr->vfe)
1084 		videocodec_detach(zr->vfe);
1085 	videocodec_exit(zr);
1086 
1087 	/* unregister i2c bus */
1088 	zoran_i2c_exit(zr);
1089 	/* disable PCI bus-mastering */
1090 	zoran_set_pci_master(zr, 0);
1091 	/* put chip into reset */
1092 	btwrite(0, ZR36057_SPGPPCR);
1093 	pci_free_irq(zr->pci_dev, 0, zr);
1094 	/* unmap and free memory */
1095 	dma_free_coherent(&zr->pci_dev->dev, BUZ_NUM_STAT_COM * sizeof(u32),
1096 			  zr->stat_com, zr->p_sc);
1097 	dma_free_coherent(&zr->pci_dev->dev, BUZ_NUM_STAT_COM * sizeof(u32) * 2,
1098 			  zr->stat_comb, zr->p_scb);
1099 	pci_release_regions(pdev);
1100 	pci_disable_device(zr->pci_dev);
1101 	zoran_exit_video_devices(zr);
1102 exit_free:
1103 	v4l2_ctrl_handler_free(&zr->hdl);
1104 	v4l2_device_unregister(&zr->v4l2_dev);
1105 }
1106 
1107 void zoran_vdev_release(struct video_device *vdev)
1108 {
1109 	kfree(vdev);
1110 }
1111 
1112 static struct videocodec_master *zoran_setup_videocodec(struct zoran *zr,
1113 							int type)
1114 {
1115 	struct videocodec_master *m = NULL;
1116 
1117 	m = devm_kmalloc(&zr->pci_dev->dev, sizeof(*m), GFP_KERNEL);
1118 	if (!m)
1119 		return m;
1120 
1121 	/*
1122 	 * magic and type are unused for master struct. Makes sense only at codec structs.
1123 	 * In the past, .type were initialized to the old V4L1 .hardware value,
1124 	 * as VID_HARDWARE_ZR36067
1125 	 */
1126 	m->magic = 0L;
1127 	m->type = 0;
1128 
1129 	m->flags = CODEC_FLAG_ENCODER | CODEC_FLAG_DECODER;
1130 	strscpy(m->name, ZR_DEVNAME(zr), sizeof(m->name));
1131 	m->data = zr;
1132 
1133 	switch (type) {
1134 	case CODEC_TYPE_ZR36060:
1135 		m->readreg = zr36060_read;
1136 		m->writereg = zr36060_write;
1137 		m->flags |= CODEC_FLAG_JPEG | CODEC_FLAG_VFE;
1138 		break;
1139 	case CODEC_TYPE_ZR36050:
1140 		m->readreg = zr36050_read;
1141 		m->writereg = zr36050_write;
1142 		m->flags |= CODEC_FLAG_JPEG;
1143 		break;
1144 	case CODEC_TYPE_ZR36016:
1145 		m->readreg = zr36016_read;
1146 		m->writereg = zr36016_write;
1147 		m->flags |= CODEC_FLAG_VFE;
1148 		break;
1149 	}
1150 
1151 	return m;
1152 }
1153 
1154 static void zoran_subdev_notify(struct v4l2_subdev *sd, unsigned int cmd, void *arg)
1155 {
1156 	struct zoran *zr = to_zoran(sd->v4l2_dev);
1157 
1158 	/*
1159 	 * Bt819 needs to reset its FIFO buffer using #FRST pin and
1160 	 * LML33 card uses GPIO(7) for that.
1161 	 */
1162 	if (cmd == BT819_FIFO_RESET_LOW)
1163 		GPIO(zr, 7, 0);
1164 	else if (cmd == BT819_FIFO_RESET_HIGH)
1165 		GPIO(zr, 7, 1);
1166 }
1167 
1168 static int zoran_video_set_ctrl(struct v4l2_ctrl *ctrl)
1169 {
1170 	struct zoran *zr = container_of(ctrl->handler, struct zoran, hdl);
1171 
1172 	switch (ctrl->id) {
1173 	case V4L2_CID_JPEG_COMPRESSION_QUALITY:
1174 		zr->jpg_settings.jpg_comp.quality = ctrl->val;
1175 		return zoran_check_jpg_settings(zr, &zr->jpg_settings, 0);
1176 	default:
1177 		return -EINVAL;
1178 	}
1179 
1180 	return 0;
1181 }
1182 
1183 static const struct v4l2_ctrl_ops zoran_video_ctrl_ops = {
1184 	.s_ctrl = zoran_video_set_ctrl,
1185 };
1186 
1187 static int zoran_debugfs_show(struct seq_file *seq, void *v)
1188 {
1189 	struct zoran *zr = seq->private;
1190 
1191 	seq_printf(seq, "Running mode %x\n", zr->running);
1192 	seq_printf(seq, "Codec mode %x\n", zr->codec_mode);
1193 	seq_printf(seq, "Norm %llx\n", zr->norm);
1194 	seq_printf(seq, "Input %d\n", zr->input);
1195 	seq_printf(seq, "Buffersize %d\n", zr->buffer_size);
1196 
1197 	seq_printf(seq, "V4L width %dx%d\n", zr->v4l_settings.width, zr->v4l_settings.height);
1198 	seq_printf(seq, "V4L bytesperline %d\n", zr->v4l_settings.bytesperline);
1199 
1200 	seq_printf(seq, "JPG decimation %u\n", zr->jpg_settings.decimation);
1201 	seq_printf(seq, "JPG hor_dcm %u\n", zr->jpg_settings.hor_dcm);
1202 	seq_printf(seq, "JPG ver_dcm %u\n", zr->jpg_settings.ver_dcm);
1203 	seq_printf(seq, "JPG tmp_dcm %u\n", zr->jpg_settings.tmp_dcm);
1204 	seq_printf(seq, "JPG odd_even %u\n", zr->jpg_settings.odd_even);
1205 	seq_printf(seq, "JPG crop %dx%d %d %d\n",
1206 		   zr->jpg_settings.img_x,
1207 		   zr->jpg_settings.img_y,
1208 		   zr->jpg_settings.img_width,
1209 		   zr->jpg_settings.img_height);
1210 
1211 	seq_printf(seq, "Prepared %u\n", zr->prepared);
1212 	seq_printf(seq, "Queued %u\n", zr->queued);
1213 
1214 	videocodec_debugfs_show(seq);
1215 	return 0;
1216 }
1217 
1218 DEFINE_SHOW_ATTRIBUTE(zoran_debugfs);
1219 
1220 /*
1221  *   Scan for a Buz card (actually for the PCI controller ZR36057),
1222  *   request the irq and map the io memory
1223  */
1224 static int zoran_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1225 {
1226 	unsigned char latency, need_latency;
1227 	struct zoran *zr;
1228 	int result;
1229 	struct videocodec_master *master_vfe = NULL;
1230 	struct videocodec_master *master_codec = NULL;
1231 	int card_num;
1232 	unsigned int nr;
1233 	int err;
1234 
1235 	pci_info(pdev, "Zoran MJPEG board driver version %s\n", ZORAN_VERSION);
1236 
1237 	/* some mainboards might not do PCI-PCI data transfer well */
1238 	if (pci_pci_problems & (PCIPCI_FAIL | PCIAGP_FAIL | PCIPCI_ALIMAGIK))
1239 		pci_warn(pdev, "%s: chipset does not support reliable PCI-PCI DMA\n",
1240 			 ZORAN_NAME);
1241 
1242 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
1243 	if (err)
1244 		return err;
1245 	err = vb2_dma_contig_set_max_seg_size(&pdev->dev, U32_MAX);
1246 	if (err)
1247 		return err;
1248 
1249 	nr = zoran_num++;
1250 	if (nr >= BUZ_MAX) {
1251 		pci_err(pdev, "driver limited to %d card(s) maximum\n", BUZ_MAX);
1252 		return -ENOENT;
1253 	}
1254 
1255 	zr = devm_kzalloc(&pdev->dev, sizeof(*zr), GFP_KERNEL);
1256 	if (!zr)
1257 		return -ENOMEM;
1258 
1259 	zr->v4l2_dev.notify = zoran_subdev_notify;
1260 	if (v4l2_device_register(&pdev->dev, &zr->v4l2_dev))
1261 		goto zr_free_mem;
1262 	zr->pci_dev = pdev;
1263 	zr->id = nr;
1264 	snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)), "MJPEG[%u]", zr->id);
1265 	if (v4l2_ctrl_handler_init(&zr->hdl, 10))
1266 		goto zr_unreg;
1267 	zr->v4l2_dev.ctrl_handler = &zr->hdl;
1268 	v4l2_ctrl_new_std(&zr->hdl, &zoran_video_ctrl_ops,
1269 			  V4L2_CID_JPEG_COMPRESSION_QUALITY, 0,
1270 			  100, 1, 50);
1271 	spin_lock_init(&zr->spinlock);
1272 	mutex_init(&zr->lock);
1273 	if (pci_enable_device(pdev))
1274 		goto zr_unreg;
1275 	zr->revision = zr->pci_dev->revision;
1276 
1277 	pci_info(zr->pci_dev, "Zoran ZR360%c7 (rev %d), irq: %d, memory: 0x%08llx\n",
1278 		 zr->revision < 2 ? '5' : '6', zr->revision,
1279 		 zr->pci_dev->irq, (uint64_t)pci_resource_start(zr->pci_dev, 0));
1280 	if (zr->revision >= 2)
1281 		pci_info(zr->pci_dev, "Subsystem vendor=0x%04x id=0x%04x\n",
1282 			 zr->pci_dev->subsystem_vendor, zr->pci_dev->subsystem_device);
1283 
1284 	/* Use auto-detected card type? */
1285 	if (card[nr] == -1) {
1286 		if (zr->revision < 2) {
1287 			pci_err(pdev, "No card type specified, please use the card=X module parameter\n");
1288 			pci_err(pdev, "It is not possible to auto-detect ZR36057 based cards\n");
1289 			goto zr_unreg;
1290 		}
1291 
1292 		card_num = ent->driver_data;
1293 		if (card_num >= NUM_CARDS) {
1294 			pci_err(pdev, "Unknown card, try specifying card=X module parameter\n");
1295 			goto zr_unreg;
1296 		}
1297 		pci_info(zr->pci_dev, "%s() - card %s detected\n", __func__,
1298 			 zoran_cards[card_num].name);
1299 	} else {
1300 		card_num = card[nr];
1301 		if (card_num >= NUM_CARDS || card_num < 0) {
1302 			pci_err(pdev, "User specified card type %d out of range (0 .. %d)\n",
1303 				card_num, NUM_CARDS - 1);
1304 			goto zr_unreg;
1305 		}
1306 	}
1307 
1308 	/*
1309 	 * even though we make this a non pointer and thus
1310 	 * theoretically allow for making changes to this struct
1311 	 * on a per-individual card basis at runtime, this is
1312 	 * strongly discouraged. This structure is intended to
1313 	 * keep general card information, no settings or anything
1314 	 */
1315 	zr->card = zoran_cards[card_num];
1316 	snprintf(ZR_DEVNAME(zr), sizeof(ZR_DEVNAME(zr)), "%s[%u]",
1317 		 zr->card.name, zr->id);
1318 
1319 	err = pci_request_regions(pdev, ZR_DEVNAME(zr));
1320 	if (err)
1321 		goto zr_unreg;
1322 
1323 	zr->zr36057_mem = devm_ioremap(&pdev->dev, pci_resource_start(pdev, 0),
1324 				       pci_resource_len(pdev, 0));
1325 	if (!zr->zr36057_mem) {
1326 		pci_err(pdev, "%s() - ioremap failed\n", __func__);
1327 		goto zr_pci_release;
1328 	}
1329 
1330 	result = pci_request_irq(pdev, 0, zoran_irq, NULL, zr, ZR_DEVNAME(zr));
1331 	if (result < 0) {
1332 		if (result == -EINVAL) {
1333 			pci_err(pdev, "%s - bad IRQ number or handler\n", __func__);
1334 		} else if (result == -EBUSY) {
1335 			pci_err(pdev, "%s - IRQ %d busy, change your PnP config in BIOS\n",
1336 				__func__, zr->pci_dev->irq);
1337 		} else {
1338 			pci_err(pdev, "%s - cannot assign IRQ, error code %d\n", __func__, result);
1339 		}
1340 		goto zr_pci_release;
1341 	}
1342 
1343 	/* set PCI latency timer */
1344 	pci_read_config_byte(zr->pci_dev, PCI_LATENCY_TIMER,
1345 			     &latency);
1346 	need_latency = zr->revision > 1 ? 32 : 48;
1347 	if (latency != need_latency) {
1348 		pci_info(zr->pci_dev, "Changing PCI latency from %d to %d\n",
1349 			 latency, need_latency);
1350 		pci_write_config_byte(zr->pci_dev, PCI_LATENCY_TIMER, need_latency);
1351 	}
1352 
1353 	zr36057_restart(zr);
1354 
1355 	err = zoran_i2c_init(zr);
1356 	if (err)
1357 		goto zr_free_irq;
1358 
1359 	pci_info(zr->pci_dev, "Initializing videocodec bus...\n");
1360 	err = videocodec_init(zr);
1361 	if (err)
1362 		goto zr_unreg_i2c;
1363 
1364 	/* reset JPEG codec */
1365 	jpeg_codec_sleep(zr, 1);
1366 	jpeg_codec_reset(zr);
1367 	/* video bus enabled */
1368 	/* display codec revision */
1369 	if (zr->card.video_codec != 0) {
1370 		master_codec = zoran_setup_videocodec(zr, zr->card.video_codec);
1371 		if (!master_codec)
1372 			goto zr_unreg_videocodec;
1373 		zr->codec = videocodec_attach(master_codec);
1374 		if (!zr->codec) {
1375 			pci_err(pdev, "%s - no codec found\n", __func__);
1376 			goto zr_unreg_videocodec;
1377 		}
1378 		if (zr->codec->type != zr->card.video_codec) {
1379 			pci_err(pdev, "%s - wrong codec\n", __func__);
1380 			goto zr_unreg_videocodec;
1381 		}
1382 	}
1383 	if (zr->card.video_vfe != 0) {
1384 		master_vfe = zoran_setup_videocodec(zr, zr->card.video_vfe);
1385 		if (!master_vfe)
1386 			goto zr_detach_codec;
1387 		zr->vfe = videocodec_attach(master_vfe);
1388 		if (!zr->vfe) {
1389 			pci_err(pdev, "%s - no VFE found\n", __func__);
1390 			goto zr_detach_codec;
1391 		}
1392 		if (zr->vfe->type != zr->card.video_vfe) {
1393 			pci_err(pdev, "%s = wrong VFE\n", __func__);
1394 			goto zr_detach_vfe;
1395 		}
1396 	}
1397 
1398 	/* take care of Natoma chipset and a revision 1 zr36057 */
1399 	if ((pci_pci_problems & PCIPCI_NATOMA) && zr->revision <= 1)
1400 		pci_info(zr->pci_dev, "ZR36057/Natoma bug, max. buffer size is 128K\n");
1401 
1402 	if (zr36057_init(zr) < 0)
1403 		goto zr_detach_vfe;
1404 
1405 	zr->map_mode = ZORAN_MAP_MODE_RAW;
1406 
1407 	zr->dbgfs_dir = debugfs_create_dir(ZR_DEVNAME(zr), NULL);
1408 	debugfs_create_file("debug", 0444, zr->dbgfs_dir, zr,
1409 			    &zoran_debugfs_fops);
1410 	return 0;
1411 
1412 zr_detach_vfe:
1413 	videocodec_detach(zr->vfe);
1414 zr_detach_codec:
1415 	videocodec_detach(zr->codec);
1416 zr_unreg_videocodec:
1417 	videocodec_exit(zr);
1418 zr_unreg_i2c:
1419 	zoran_i2c_exit(zr);
1420 zr_free_irq:
1421 	btwrite(0, ZR36057_SPGPPCR);
1422 	pci_free_irq(zr->pci_dev, 0, zr);
1423 zr_pci_release:
1424 	pci_release_regions(pdev);
1425 zr_unreg:
1426 	v4l2_ctrl_handler_free(&zr->hdl);
1427 	v4l2_device_unregister(&zr->v4l2_dev);
1428 zr_free_mem:
1429 
1430 	return -ENODEV;
1431 }
1432 
1433 static struct pci_driver zoran_driver = {
1434 	.name = "zr36067",
1435 	.id_table = zr36067_pci_tbl,
1436 	.probe = zoran_probe,
1437 	.remove = zoran_remove,
1438 };
1439 
1440 module_pci_driver(zoran_driver);
1441