1 /*
2     bt819 - BT819A VideoStream Decoder (Rockwell Part)
3 
4     Copyright (C) 1999 Mike Bernson <mike@mlb.org>
5     Copyright (C) 1998 Dave Perks <dperks@ibm.net>
6 
7     Modifications for LML33/DC10plus unified driver
8     Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
9 
10     This code was modify/ported from the saa7111 driver written
11     by Dave Perks.
12 
13     This program is free software; you can redistribute it and/or modify
14     it under the terms of the GNU General Public License as published by
15     the Free Software Foundation; either version 2 of the License, or
16     (at your option) any later version.
17 
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21     GNU General Public License for more details.
22 
23     You should have received a copy of the GNU General Public License
24     along with this program; if not, write to the Free Software
25     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 */
27 
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/delay.h>
31 #include <linux/errno.h>
32 #include <linux/fs.h>
33 #include <linux/kernel.h>
34 #include <linux/slab.h>
35 #include <linux/mm.h>
36 #include <linux/pci.h>
37 #include <linux/signal.h>
38 #include <linux/sched.h>
39 
40 #include <linux/videodev.h>
41 
42 #include <linux/i2c-old.h>
43 #include <linux/video_decoder.h>
44 
45 #define DEBUG(x)       x	/* Debug driver */
46 
47 /* ----------------------------------------------------------------------- */
48 
49 struct bt819 {
50 	struct i2c_bus *bus;
51 	int addr;
52 	unsigned char reg[32];
53 
54 	int initialized;
55 	int norm;
56 	int input;
57 	int enable;
58 	int bright;
59 	int contrast;
60 	int hue;
61 	int sat;
62 };
63 
64 struct timing {
65 	int hactive;
66 	int hdelay;
67 	int vactive;
68 	int vdelay;
69 	int hscale;
70 	int vscale;
71 };
72 
73 struct timing timing_data[] = {
74 	{864 - 24, 2, 623, 1, 0x0504, 0x0000},
75 	{858 - 24, 2, 523, 1, 0x00f8, 0x0000},
76 //      { 858-68, 64, 523, 1, 0x00f8, 0x0000 },
77 };
78 
79 #define   I2C_BT819        0x8a
80 
81 #define   I2C_DELAY   10
82 
83 /* ----------------------------------------------------------------------- */
84 
bt819_write(struct bt819 * dev,unsigned char subaddr,unsigned char data)85 static int bt819_write(struct bt819 *dev, unsigned char subaddr,
86 		       unsigned char data)
87 {
88 	int ack;
89 
90 	LOCK_I2C_BUS(dev->bus);
91 	i2c_start(dev->bus);
92 	i2c_sendbyte(dev->bus, dev->addr, I2C_DELAY);
93 	i2c_sendbyte(dev->bus, subaddr, I2C_DELAY);
94 	ack = i2c_sendbyte(dev->bus, data, I2C_DELAY);
95 	dev->reg[subaddr] = data;
96 	i2c_stop(dev->bus);
97 	UNLOCK_I2C_BUS(dev->bus);
98 	return ack;
99 }
100 
bt819_setbit(struct bt819 * dev,int subaddr,int bit,int data)101 static int bt819_setbit(struct bt819 *dev, int subaddr, int bit, int data)
102 {
103 	return bt819_write(dev, subaddr, (dev->reg[subaddr] & ~(1 << bit)) | (data ? (1 << bit) : 0));
104 }
105 
bt819_write_block(struct bt819 * dev,unsigned const char * data,unsigned int len)106 static int bt819_write_block(struct bt819 *dev, unsigned const char *data,
107 			     unsigned int len)
108 {
109 	int ack;
110 	unsigned subaddr;
111 
112 	ack = 0;
113 	while (len > 1) {
114 		LOCK_I2C_BUS(dev->bus);
115 		i2c_start(dev->bus);
116 		i2c_sendbyte(dev->bus, dev->addr, I2C_DELAY);
117 		ack = i2c_sendbyte(dev->bus, (subaddr = *data++), I2C_DELAY);
118 		ack = i2c_sendbyte(dev->bus, (dev->reg[subaddr] = *data++),
119 				 I2C_DELAY);
120 		len -= 2;
121 		while (len > 1 && *data == ++subaddr) {
122 			data++;
123 			ack =
124 			    i2c_sendbyte(dev->bus, (dev->reg[subaddr] = *data++), I2C_DELAY);
125 			len -= 2;
126 		}
127 		i2c_stop(dev->bus);
128 		UNLOCK_I2C_BUS(dev->bus);
129 	}
130 	return ack;
131 }
132 
bt819_read(struct bt819 * dev,unsigned char subaddr)133 static int bt819_read(struct bt819 *dev, unsigned char subaddr)
134 {
135 	int data;
136 
137 	LOCK_I2C_BUS(dev->bus);
138 	i2c_start(dev->bus);
139 	i2c_sendbyte(dev->bus, dev->addr, I2C_DELAY);
140 	i2c_sendbyte(dev->bus, subaddr, I2C_DELAY);
141 	i2c_start(dev->bus);
142 	i2c_sendbyte(dev->bus, dev->addr | 1, I2C_DELAY);
143 	data = i2c_readbyte(dev->bus, 1);
144 	i2c_stop(dev->bus);
145 	UNLOCK_I2C_BUS(dev->bus);
146 	return data;
147 }
148 
bt819_init(struct i2c_device * device)149 static int bt819_init(struct i2c_device *device)
150 {
151 	struct bt819 *decoder;
152 
153 	static unsigned char init[] = {
154 		//0x1f, 0x00,     /* Reset */
155 		0x01, 0x59,	/* 0x01 input format */
156 		0x02, 0x00,	/* 0x02 temporal decimation */
157 		0x03, 0x12,	/* 0x03 Cropping msb */
158 		0x04, 0x16,	/* 0x04 Vertical Delay, lsb */
159 		0x05, 0xe0,	/* 0x05 Vertical Active lsb */
160 		0x06, 0x80,	/* 0x06 Horizontal Delay lsb */
161 		0x07, 0xd0,	/* 0x07 Horizontal Active lsb */
162 		0x08, 0x00,	/* 0x08 Horizontal Scaling msb */
163 		0x09, 0xf8,	/* 0x09 Horizontal Scaling lsb */
164 		0x0a, 0x00,	/* 0x0a Brightness control */
165 		0x0b, 0x30,	/* 0x0b Miscellaneous control */
166 		0x0c, 0xd8,	/* 0x0c Luma Gain lsb */
167 		0x0d, 0xfe,	/* 0x0d Chroma Gain (U) lsb */
168 		0x0e, 0xb4,	/* 0x0e Chroma Gain (V) msb */
169 		0x0f, 0x00,	/* 0x0f Hue control */
170 		0x12, 0x04,	/* 0x12 Output Format */
171 		0x13, 0x20,	/* 0x13 Vertial Scaling msb 0x60, */
172 		0x14, 0x00,	/* 0x14 Vertial Scaling lsb */
173 		0x16, 0x04,	/* 0x16 Video Timing Polarity 0x02, */
174 		0x18, 0x68,	/* 0x18 AGC Delay */
175 		0x19, 0x5d,	/* 0x19 Burst Gate Delay */
176 		0x1a, 0x80,	/* 0x1a ADC Interface */
177 	};
178 
179 	struct timing *timing;
180 
181 	decoder = device->data;
182 	timing = &timing_data[decoder->norm];
183 
184 	init[3 * 2 - 1] = (((timing->vdelay >> 8) & 0x03) << 6) |
185 	    (((timing->vactive >> 8) & 0x03) << 4) |
186 	    (((timing->hdelay >> 8) & 0x03) << 2) |
187 	    ((timing->hactive >> 8) & 0x03);
188 	init[4 * 2 - 1] = timing->vdelay & 0xff;
189 	init[5 * 2 - 1] = timing->vactive & 0xff;
190 	init[6 * 2 - 1] = timing->hdelay & 0xff;
191 	init[7 * 2 - 1] = timing->hactive & 0xff;
192 	init[8 * 2 - 1] = timing->hscale >> 8;
193 	init[9 * 2 - 1] = timing->hscale & 0xff;
194 
195 	bt819_write(decoder, 0x1f, 0x00);
196 	mdelay(1);
197 	return bt819_write_block(decoder, init, sizeof(init));
198 
199 }
200 
201 /* ----------------------------------------------------------------------- */
202 
bt819_attach(struct i2c_device * device)203 static int bt819_attach(struct i2c_device *device)
204 {
205 	int i;
206 	struct bt819 *decoder;
207 
208 	MOD_INC_USE_COUNT;
209 
210 	device->data = decoder = kmalloc(sizeof(struct bt819), GFP_KERNEL);
211 	if (decoder == NULL) {
212 		MOD_DEC_USE_COUNT;
213 		return -ENOMEM;
214 	}
215 
216 	memset(decoder, 0, sizeof(struct bt819));
217 	strcpy(device->name, "bt819");
218 	decoder->bus = device->bus;
219 	decoder->addr = device->addr;
220 	decoder->norm = VIDEO_MODE_NTSC;
221 	decoder->input = 0;
222 	decoder->enable = 1;
223 	decoder->bright = 32768;
224 	decoder->contrast = 32768;
225 	decoder->hue = 32768;
226 	decoder->sat = 32768;
227 	decoder->initialized = 0;
228 
229 	i = bt819_init(device);
230 	if (i < 0) {
231 		printk(KERN_ERR "%s: bt819_attach: init status %d\n",
232 		       decoder->bus->name, i);
233 	} else {
234 		printk(KERN_INFO "%s: bt819_attach: chip version %x\n",
235 		       decoder->bus->name, bt819_read(decoder,
236 						      0x17) & 0x0f);
237 	}
238 	return 0;
239 }
240 
bt819_detach(struct i2c_device * device)241 static int bt819_detach(struct i2c_device *device)
242 {
243 	kfree(device->data);
244 	MOD_DEC_USE_COUNT;
245 	return 0;
246 }
247 
bt819_command(struct i2c_device * device,unsigned int cmd,void * arg)248 static int bt819_command(struct i2c_device *device, unsigned int cmd, void *arg)
249 {
250 	int temp;
251 
252 	struct bt819 *decoder = device->data;
253 	//return 0;
254 
255 	if (!decoder->initialized) {	// First call to bt819_init could be
256 		bt819_init(device);	// without #FRST = 0
257 		decoder->initialized = 1;
258 	}
259 
260 	switch (cmd) {
261 
262 	case DECODER_GET_CAPABILITIES:
263 		{
264 			struct video_decoder_capability *cap = arg;
265 
266 			cap->flags
267 			    = VIDEO_DECODER_PAL
268 			    | VIDEO_DECODER_NTSC | VIDEO_DECODER_CCIR;
269 			cap->inputs = 8;
270 			cap->outputs = 1;
271 		}
272 		break;
273 
274 	case DECODER_GET_STATUS:
275 		{
276 			int *iarg = arg;
277 			int status;
278 			int res;
279 
280 			status = bt819_read(decoder, 0x00);
281 			res = 0;
282 			if ((status & 0x80)) {
283 				res |= DECODER_STATUS_GOOD;
284 			}
285 			switch (decoder->norm) {
286 			case VIDEO_MODE_NTSC:
287 				res |= DECODER_STATUS_NTSC;
288 				break;
289 			case VIDEO_MODE_PAL:
290 				res |= DECODER_STATUS_PAL;
291 				break;
292 			default:
293 			case VIDEO_MODE_AUTO:
294 				if ((status & 0x10)) {
295 					res |= DECODER_STATUS_PAL;
296 				} else {
297 					res |= DECODER_STATUS_NTSC;
298 				}
299 				break;
300 			}
301 			res |= DECODER_STATUS_COLOR;
302 			*iarg = res;
303 
304 			DEBUG(printk(KERN_INFO "%s-bt819: get status %x\n",
305 				     decoder->bus->name, *iarg));
306 		}
307 		break;
308 
309 	case DECODER_SET_NORM:
310 		{
311 			int *iarg = arg;
312 			struct timing *timing;
313 
314 			DEBUG(printk(KERN_INFO "%s-bt819: set norm %x\n",
315 				     decoder->bus->name, *iarg));
316 
317 			if (*iarg == VIDEO_MODE_NTSC) {
318 				bt819_setbit(decoder, 0x01, 0, 1);
319 				bt819_setbit(decoder, 0x01, 1, 0);
320 				bt819_write(decoder, 0x18, 0x68);
321 				bt819_write(decoder, 0x19, 0x5d);
322 				//bt819_setbit(decoder, 0x1a,  5, 1);
323 				timing = &timing_data[VIDEO_MODE_NTSC];
324 			} else {
325 				bt819_setbit(decoder, 0x01, 0, 1);
326 				bt819_setbit(decoder, 0x01, 1, 1);
327 				bt819_write(decoder, 0x18, 0x7f);
328 				bt819_write(decoder, 0x19, 0x72);
329 				//bt819_setbit(decoder, 0x1a,  5, 0);
330 				timing = &timing_data[VIDEO_MODE_PAL];
331 			}
332 
333 			bt819_write(decoder, 0x03,
334 				    (((timing->vdelay >> 8) & 0x03) << 6) |
335 				    (((timing->
336 				       vactive >> 8) & 0x03) << 4) |
337 				    (((timing->
338 				       hdelay >> 8) & 0x03) << 2) |
339 				    ((timing->hactive >> 8) & 0x03));
340 			bt819_write(decoder, 0x04, timing->vdelay & 0xff);
341 			bt819_write(decoder, 0x05, timing->vactive & 0xff);
342 			bt819_write(decoder, 0x06, timing->hdelay & 0xff);
343 			bt819_write(decoder, 0x07, timing->hactive & 0xff);
344 			bt819_write(decoder, 0x08, timing->hscale >> 8);
345 			bt819_write(decoder, 0x09, timing->hscale & 0xff);
346 			decoder->norm = *iarg;
347 		}
348 		break;
349 
350 	case DECODER_SET_INPUT:
351 		{
352 			int *iarg = arg;
353 
354 			DEBUG(printk(KERN_INFO "%s-bt819: set input %x\n",
355 				     decoder->bus->name, *iarg));
356 
357 			if (*iarg < 0 || *iarg > 7) {
358 				return -EINVAL;
359 			}
360 
361 			if (decoder->input != *iarg) {
362 				decoder->input = *iarg;
363 				/* select mode */
364 				if (decoder->input == 0) {
365 					bt819_setbit(decoder, 0x0b, 6, 0);
366 					bt819_setbit(decoder, 0x1a, 1, 1);
367 				} else {
368 					bt819_setbit(decoder, 0x0b, 6, 1);
369 					bt819_setbit(decoder, 0x1a, 1, 0);
370 				}
371 			}
372 		}
373 		break;
374 
375 	case DECODER_SET_OUTPUT:
376 		{
377 			int *iarg = arg;
378 
379 			DEBUG(printk(KERN_INFO "%s-bt819: set output %x\n",
380 				     decoder->bus->name, *iarg));
381 
382 			/* not much choice of outputs */
383 			if (*iarg != 0) {
384 				return -EINVAL;
385 			}
386 		}
387 		break;
388 
389 	case DECODER_ENABLE_OUTPUT:
390 		{
391 			int *iarg = arg;
392 			int enable = (*iarg != 0);
393 
394 			DEBUG(printk
395 			      (KERN_INFO "%s-bt819: enable output %x\n",
396 			       decoder->bus->name, *iarg));
397 
398 			if (decoder->enable != enable) {
399 				decoder->enable = enable;
400 
401 				if (decoder->enable) {
402 					bt819_setbit(decoder, 0x16, 7, 0);
403 				} else {
404 					bt819_setbit(decoder, 0x16, 7, 1);
405 				}
406 			}
407 		}
408 		break;
409 
410 	case DECODER_SET_PICTURE:
411 		{
412 			struct video_picture *pic = arg;
413 
414 			DEBUG(printk
415 			      (KERN_INFO
416 			       "%s-bt819: set picture brightness %d contrast %d colour %d\n",
417 			       decoder->bus->name, pic->brightness,
418 			       pic->contrast, pic->colour));
419 
420 
421 			if (decoder->bright != pic->brightness) {
422 				/* We want -128 to 127 we get 0-65535 */
423 				decoder->bright = pic->brightness;
424 				bt819_write(decoder, 0x0a,
425 					    (decoder->bright >> 8) - 128);
426 			}
427 
428 			if (decoder->contrast != pic->contrast) {
429 				/* We want 0 to 511 we get 0-65535 */
430 				decoder->contrast = pic->contrast;
431 				bt819_write(decoder, 0x0c,
432 					    (decoder->
433 					     contrast >> 7) & 0xff);
434 				bt819_setbit(decoder, 0x0b, 2,
435 					     ((decoder->
436 					       contrast >> 15) & 0x01));
437 			}
438 
439 			if (decoder->sat != pic->colour) {
440 				/* We want 0 to 511 we get 0-65535 */
441 				decoder->sat = pic->colour;
442 				bt819_write(decoder, 0x0d,
443 					    (decoder->sat >> 7) & 0xff);
444 				bt819_setbit(decoder, 0x0b, 1,
445 					     ((decoder->
446 					       sat >> 15) & 0x01));
447 
448 				temp = (decoder->sat * 201) / 237;
449 				bt819_write(decoder, 0x0e,
450 					    (temp >> 7) & 0xff);
451 				bt819_setbit(decoder, 0x0b, 0,
452 					     (temp >> 15) & 0x01);
453 			}
454 
455 			if (decoder->hue != pic->hue) {
456 				/* We want -128 to 127 we get 0-65535 */
457 				decoder->hue = pic->hue;
458 				bt819_write(decoder, 0x0f,
459 					    128 - (decoder->hue >> 8));
460 			}
461 		}
462 		break;
463 
464 	default:
465 		return -EINVAL;
466 	}
467 
468 	return 0;
469 }
470 
471 /* ----------------------------------------------------------------------- */
472 
473 static struct i2c_driver i2c_driver_bt819 = {
474 	"bt819",		/* name */
475 	I2C_DRIVERID_VIDEODECODER,	/* ID */
476 	I2C_BT819, I2C_BT819 + 1,
477 
478 	bt819_attach,
479 	bt819_detach,
480 	bt819_command
481 };
482 
483 EXPORT_NO_SYMBOLS;
484 
bt819_setup(void)485 static int bt819_setup(void)
486 {
487 	return i2c_register_driver(&i2c_driver_bt819);
488 }
489 
bt819_exit(void)490 static void bt819_exit(void)
491 {
492 	i2c_unregister_driver(&i2c_driver_bt819);
493 }
494 
495 module_init(bt819_setup);
496 module_exit(bt819_exit);
497 MODULE_LICENSE("GPL");
498