2 * saa7111 - Philips SAA7111A video decoder driver version 0.0.3
4 * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
6 * Slight changes for video timing and attachment output by
7 * Wolfgang Scherr <scherr@net4you.net>
9 * Changes by Ronald Bultje <rbultje@ronald.bitfreak.net>
10 * - moved over to linux>=2.4.x i2c protocol (1/1/2003)
12 * Changes by Michael Hunold <michael@mihu.de>
13 * - implemented DECODER_SET_GPIO, DECODER_INIT, DECODER_SET_VBI_BYPASS
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/delay.h>
33 #include <linux/errno.h>
35 #include <linux/kernel.h>
36 #include <linux/major.h>
37 #include <linux/slab.h>
39 #include <linux/pci.h>
40 #include <linux/signal.h>
42 #include <asm/pgtable.h>
44 #include <linux/sched.h>
45 #include <linux/types.h>
47 #include <linux/videodev.h>
48 #include <asm/uaccess.h>
50 MODULE_DESCRIPTION("Philips SAA7111 video decoder driver");
51 MODULE_AUTHOR("Dave Perks");
52 MODULE_LICENSE("GPL");
54 #include <linux/i2c.h>
55 #include <linux/i2c-dev.h>
57 #define I2C_NAME(s) (s)->name
59 #include <linux/video_decoder.h>
62 module_param(debug, int, 0644);
63 MODULE_PARM_DESC(debug, "Debug level (0-1)");
65 #define dprintk(num, format, args...) \
68 printk(format, ##args); \
71 /* ----------------------------------------------------------------------- */
73 #define SAA7111_NR_REG 0x18
76 unsigned char reg[SAA7111_NR_REG];
87 #define I2C_SAA7111 0x48
89 /* ----------------------------------------------------------------------- */
92 saa7111_write (struct i2c_client *client,
96 struct saa7111 *decoder = i2c_get_clientdata(client);
98 decoder->reg[reg] = value;
99 return i2c_smbus_write_byte_data(client, reg, value);
103 saa7111_write_block (struct i2c_client *client,
110 /* the saa7111 has an autoincrement function, use it if
111 * the adapter understands raw I2C */
112 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
113 /* do raw I2C, not smbus compatible */
114 struct saa7111 *decoder = i2c_get_clientdata(client);
118 msg.addr = client->addr;
121 msg.buf = (char *) block_data;
123 block_data[msg.len++] = reg = data[0];
125 block_data[msg.len++] =
126 decoder->reg[reg++] = data[1];
129 } while (len >= 2 && data[0] == reg &&
131 if ((ret = i2c_transfer(client->adapter,
136 /* do some slow I2C emulation kind of thing */
139 if ((ret = saa7111_write(client, reg,
150 saa7111_init_decoder (struct i2c_client *client,
151 struct video_decoder_init *init)
153 return saa7111_write_block(client, init->data, init->len);
157 saa7111_read (struct i2c_client *client,
160 return i2c_smbus_read_byte_data(client, reg);
163 /* ----------------------------------------------------------------------- */
165 static const unsigned char saa7111_i2c_init[] = {
166 0x00, 0x00, /* 00 - ID byte */
167 0x01, 0x00, /* 01 - reserved */
170 0x02, 0xd0, /* 02 - FUSE=3, GUDL=2, MODE=0 */
171 0x03, 0x23, /* 03 - HLNRS=0, VBSL=1, WPOFF=0,
172 * HOLDG=0, GAFIX=0, GAI1=256, GAI2=256 */
173 0x04, 0x00, /* 04 - GAI1=256 */
174 0x05, 0x00, /* 05 - GAI2=256 */
177 0x06, 0xf3, /* 06 - HSB at 13(50Hz) / 17(60Hz)
178 * pixels after end of last line */
179 /*0x07, 0x13, * 07 - HSS at 113(50Hz) / 117(60Hz) pixels
180 * after end of last line */
181 0x07, 0xe8, /* 07 - HSS seems to be needed to
182 * work with NTSC, too */
183 0x08, 0xc8, /* 08 - AUFD=1, FSEL=1, EXFIL=0,
184 * VTRC=1, HPLL=0, VNOI=0 */
185 0x09, 0x01, /* 09 - BYPS=0, PREF=0, BPSS=0,
186 * VBLB=0, UPTCV=0, APER=1 */
187 0x0a, 0x80, /* 0a - BRIG=128 */
188 0x0b, 0x47, /* 0b - CONT=1.109 */
189 0x0c, 0x40, /* 0c - SATN=1.0 */
190 0x0d, 0x00, /* 0d - HUE=0 */
191 0x0e, 0x01, /* 0e - CDTO=0, CSTD=0, DCCF=0,
193 0x0f, 0x00, /* 0f - reserved */
194 0x10, 0x48, /* 10 - OFTS=1, HDEL=0, VRLN=1, YDEL=0 */
195 0x11, 0x1c, /* 11 - GPSW=0, CM99=0, FECO=0, COMPO=1,
196 * OEYC=1, OEHV=1, VIPB=0, COLO=0 */
197 0x12, 0x00, /* 12 - output control 2 */
198 0x13, 0x00, /* 13 - output control 3 */
199 0x14, 0x00, /* 14 - reserved */
200 0x15, 0x00, /* 15 - VBI */
201 0x16, 0x00, /* 16 - VBI */
202 0x17, 0x00, /* 17 - VBI */
206 saa7111_command (struct i2c_client *client,
210 struct saa7111 *decoder = i2c_get_clientdata(client);
217 struct video_decoder_init *init = arg;
219 return saa7111_init_decoder(client, init);
221 struct video_decoder_init vdi;
222 vdi.data = saa7111_i2c_init;
223 vdi.len = sizeof(saa7111_i2c_init);
224 return saa7111_init_decoder(client, &vdi);
232 for (i = 0; i < SAA7111_NR_REG; i += 16) {
235 printk(KERN_DEBUG "%s: %03x", I2C_NAME(client), i);
236 for (j = 0; j < 16 && i + j < SAA7111_NR_REG; ++j) {
238 saa7111_read(client, i + j));
245 case DECODER_GET_CAPABILITIES:
247 struct video_decoder_capability *cap = arg;
249 cap->flags = VIDEO_DECODER_PAL |
251 VIDEO_DECODER_SECAM |
259 case DECODER_GET_STATUS:
265 status = saa7111_read(client, 0x1f);
266 dprintk(1, KERN_DEBUG "%s status: 0x%02x\n", I2C_NAME(client),
269 if ((status & (1 << 6)) == 0) {
270 res |= DECODER_STATUS_GOOD;
272 switch (decoder->norm) {
273 case VIDEO_MODE_NTSC:
274 res |= DECODER_STATUS_NTSC;
277 res |= DECODER_STATUS_PAL;
279 case VIDEO_MODE_SECAM:
280 res |= DECODER_STATUS_SECAM;
283 case VIDEO_MODE_AUTO:
284 if ((status & (1 << 5)) != 0) {
285 res |= DECODER_STATUS_NTSC;
287 res |= DECODER_STATUS_PAL;
291 if ((status & (1 << 0)) != 0) {
292 res |= DECODER_STATUS_COLOR;
298 case DECODER_SET_GPIO:
302 saa7111_write(client, 0x11,
303 (decoder->reg[0x11] | 0x80));
305 saa7111_write(client, 0x11,
306 (decoder->reg[0x11] & 0x7f));
311 case DECODER_SET_VBI_BYPASS:
315 saa7111_write(client, 0x13,
316 (decoder->reg[0x13] & 0xf0) | 0x0a);
318 saa7111_write(client, 0x13,
319 (decoder->reg[0x13] & 0xf0));
324 case DECODER_SET_NORM:
330 case VIDEO_MODE_NTSC:
331 saa7111_write(client, 0x08,
332 (decoder->reg[0x08] & 0x3f) | 0x40);
333 saa7111_write(client, 0x0e,
334 (decoder->reg[0x0e] & 0x8f));
338 saa7111_write(client, 0x08,
339 (decoder->reg[0x08] & 0x3f) | 0x00);
340 saa7111_write(client, 0x0e,
341 (decoder->reg[0x0e] & 0x8f));
344 case VIDEO_MODE_SECAM:
345 saa7111_write(client, 0x08,
346 (decoder->reg[0x08] & 0x3f) | 0x00);
347 saa7111_write(client, 0x0e,
348 (decoder->reg[0x0e] & 0x8f) | 0x50);
351 case VIDEO_MODE_AUTO:
352 saa7111_write(client, 0x08,
353 (decoder->reg[0x08] & 0x3f) | 0x80);
354 saa7111_write(client, 0x0e,
355 (decoder->reg[0x0e] & 0x8f));
362 decoder->norm = *iarg;
366 case DECODER_SET_INPUT:
370 if (*iarg < 0 || *iarg > 7) {
374 if (decoder->input != *iarg) {
375 decoder->input = *iarg;
377 saa7111_write(client, 0x02,
379 reg[0x02] & 0xf8) | decoder->input);
380 /* bypass chrominance trap for modes 4..7 */
381 saa7111_write(client, 0x09,
383 reg[0x09] & 0x7f) | ((decoder->
391 case DECODER_SET_OUTPUT:
395 /* not much choice of outputs */
402 case DECODER_ENABLE_OUTPUT:
405 int enable = (*iarg != 0);
407 if (decoder->enable != enable) {
408 decoder->enable = enable;
410 /* RJ: If output should be disabled (for
411 * playing videos), we also need a open PLL.
412 * The input is set to 0 (where no input
413 * source is connected), although this
416 * If output should be enabled, we have to
420 if (decoder->enable) {
421 saa7111_write(client, 0x02,
425 saa7111_write(client, 0x08,
426 (decoder->reg[0x08] & 0xfb));
427 saa7111_write(client, 0x11,
429 reg[0x11] & 0xf3) | 0x0c);
431 saa7111_write(client, 0x02,
432 (decoder->reg[0x02] & 0xf8));
433 saa7111_write(client, 0x08,
435 reg[0x08] & 0xfb) | 0x04);
436 saa7111_write(client, 0x11,
437 (decoder->reg[0x11] & 0xf3));
443 case DECODER_SET_PICTURE:
445 struct video_picture *pic = arg;
447 if (decoder->bright != pic->brightness) {
448 /* We want 0 to 255 we get 0-65535 */
449 decoder->bright = pic->brightness;
450 saa7111_write(client, 0x0a, decoder->bright >> 8);
452 if (decoder->contrast != pic->contrast) {
453 /* We want 0 to 127 we get 0-65535 */
454 decoder->contrast = pic->contrast;
455 saa7111_write(client, 0x0b,
456 decoder->contrast >> 9);
458 if (decoder->sat != pic->colour) {
459 /* We want 0 to 127 we get 0-65535 */
460 decoder->sat = pic->colour;
461 saa7111_write(client, 0x0c, decoder->sat >> 9);
463 if (decoder->hue != pic->hue) {
464 /* We want -128 to 127 we get 0-65535 */
465 decoder->hue = pic->hue;
466 saa7111_write(client, 0x0d,
467 (decoder->hue - 32768) >> 8);
479 /* ----------------------------------------------------------------------- */
483 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
485 static unsigned short normal_i2c[] = { I2C_SAA7111 >> 1, I2C_CLIENT_END };
487 static unsigned short ignore = I2C_CLIENT_END;
489 static struct i2c_client_address_data addr_data = {
490 .normal_i2c = normal_i2c,
495 static struct i2c_driver i2c_driver_saa7111;
498 saa7111_detect_client (struct i2c_adapter *adapter,
503 struct i2c_client *client;
504 struct saa7111 *decoder;
505 struct video_decoder_init vdi;
509 "saa7111.c: detecting saa7111 client on address 0x%x\n",
512 /* Check if the adapter supports the needed features */
513 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
516 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
519 client->addr = address;
520 client->adapter = adapter;
521 client->driver = &i2c_driver_saa7111;
522 strlcpy(I2C_NAME(client), "saa7111", sizeof(I2C_NAME(client)));
524 decoder = kzalloc(sizeof(struct saa7111), GFP_KERNEL);
525 if (decoder == NULL) {
529 decoder->norm = VIDEO_MODE_NTSC;
532 decoder->bright = 32768;
533 decoder->contrast = 32768;
534 decoder->hue = 32768;
535 decoder->sat = 32768;
536 i2c_set_clientdata(client, decoder);
538 i = i2c_attach_client(client);
545 vdi.data = saa7111_i2c_init;
546 vdi.len = sizeof(saa7111_i2c_init);
547 i = saa7111_init_decoder(client, &vdi);
549 dprintk(1, KERN_ERR "%s_attach error: init status %d\n",
550 I2C_NAME(client), i);
554 "%s_attach: chip version %x at address 0x%x\n",
555 I2C_NAME(client), saa7111_read(client, 0x00) >> 4,
563 saa7111_attach_adapter (struct i2c_adapter *adapter)
567 "saa7111.c: starting probe for adapter %s (0x%x)\n",
568 I2C_NAME(adapter), adapter->id);
569 return i2c_probe(adapter, &addr_data, &saa7111_detect_client);
573 saa7111_detach_client (struct i2c_client *client)
575 struct saa7111 *decoder = i2c_get_clientdata(client);
578 err = i2c_detach_client(client);
589 /* ----------------------------------------------------------------------- */
591 static struct i2c_driver i2c_driver_saa7111 = {
596 .id = I2C_DRIVERID_SAA7111A,
598 .attach_adapter = saa7111_attach_adapter,
599 .detach_client = saa7111_detach_client,
600 .command = saa7111_command,
606 return i2c_add_driver(&i2c_driver_saa7111);
612 i2c_del_driver(&i2c_driver_saa7111);
615 module_init(saa7111_init);
616 module_exit(saa7111_exit);