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 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/delay.h>
30 #include <linux/errno.h>
32 #include <linux/kernel.h>
33 #include <linux/major.h>
34 #include <linux/slab.h>
36 #include <linux/pci.h>
37 #include <linux/signal.h>
39 #include <asm/pgtable.h>
41 #include <linux/sched.h>
42 #include <asm/segment.h>
43 #include <linux/types.h>
45 #include <linux/videodev.h>
46 #include <asm/uaccess.h>
48 MODULE_DESCRIPTION("Philips SAA7111 video decoder driver");
49 MODULE_AUTHOR("Dave Perks");
50 MODULE_LICENSE("GPL");
52 #include <linux/i2c.h>
53 #include <linux/i2c-dev.h>
55 #define I2C_NAME(s) (s)->name
57 #include <linux/video_decoder.h>
60 MODULE_PARM(debug, "i");
61 MODULE_PARM_DESC(debug, "Debug level (0-1)");
63 #define dprintk(num, format, args...) \
66 printk(format, ##args); \
69 /* ----------------------------------------------------------------------- */
72 unsigned char reg[32];
83 #define I2C_SAA7111 0x48
85 /* ----------------------------------------------------------------------- */
88 saa7111_write (struct i2c_client *client,
92 struct saa7111 *decoder = i2c_get_clientdata(client);
94 decoder->reg[reg] = value;
95 return i2c_smbus_write_byte_data(client, reg, value);
99 saa7111_write_block (struct i2c_client *client,
106 /* the saa7111 has an autoincrement function, use it if
107 * the adapter understands raw I2C */
108 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
109 /* do raw I2C, not smbus compatible */
110 struct saa7111 *decoder = i2c_get_clientdata(client);
114 msg.addr = client->addr;
115 msg.flags = client->flags;
117 msg.buf = (char *) block_data;
119 block_data[msg.len++] = reg = data[0];
121 block_data[msg.len++] =
122 decoder->reg[reg++] = data[1];
125 } while (len >= 2 && data[0] == reg &&
127 if ((ret = i2c_transfer(client->adapter,
132 /* do some slow I2C emulation kind of thing */
135 if ((ret = saa7111_write(client, reg,
146 saa7111_read (struct i2c_client *client,
149 return i2c_smbus_read_byte_data(client, reg);
152 /* ----------------------------------------------------------------------- */
154 static const unsigned char init[] = {
155 0x00, 0x00, /* 00 - ID byte */
156 0x01, 0x00, /* 01 - reserved */
159 0x02, 0xd0, /* 02 - FUSE=3, GUDL=2, MODE=0 */
160 0x03, 0x23, /* 03 - HLNRS=0, VBSL=1, WPOFF=0,
161 * HOLDG=0, GAFIX=0, GAI1=256, GAI2=256 */
162 0x04, 0x00, /* 04 - GAI1=256 */
163 0x05, 0x00, /* 05 - GAI2=256 */
166 0x06, 0xf3, /* 06 - HSB at 13(50Hz) / 17(60Hz)
167 * pixels after end of last line */
168 /*0x07, 0x13, * 07 - HSS at 113(50Hz) / 117(60Hz) pixels
169 * after end of last line */
170 0x07, 0xe8, /* 07 - HSS seems to be needed to
171 * work with NTSC, too */
172 0x08, 0xc8, /* 08 - AUFD=1, FSEL=1, EXFIL=0,
173 * VTRC=1, HPLL=0, VNOI=0 */
174 0x09, 0x01, /* 09 - BYPS=0, PREF=0, BPSS=0,
175 * VBLB=0, UPTCV=0, APER=1 */
176 0x0a, 0x80, /* 0a - BRIG=128 */
177 0x0b, 0x47, /* 0b - CONT=1.109 */
178 0x0c, 0x40, /* 0c - SATN=1.0 */
179 0x0d, 0x00, /* 0d - HUE=0 */
180 0x0e, 0x01, /* 0e - CDTO=0, CSTD=0, DCCF=0,
182 0x0f, 0x00, /* 0f - reserved */
183 0x10, 0x48, /* 10 - OFTS=1, HDEL=0, VRLN=1, YDEL=0 */
184 0x11, 0x1c, /* 11 - GPSW=0, CM99=0, FECO=0, COMPO=1,
185 * OEYC=1, OEHV=1, VIPB=0, COLO=0 */
186 0x12, 0x00, /* 12 - output control 2 */
187 0x13, 0x00, /* 13 - output control 3 */
188 0x14, 0x00, /* 14 - reserved */
189 0x15, 0x00, /* 15 - VBI */
190 0x16, 0x00, /* 16 - VBI */
191 0x17, 0x00, /* 17 - VBI */
195 saa7111_command (struct i2c_client *client,
199 struct saa7111 *decoder = i2c_get_clientdata(client);
204 //saa7111_write_block(client, init, sizeof(init));
211 for (i = 0; i < 32; i += 16) {
214 printk(KERN_DEBUG "%s: %03x", I2C_NAME(client), i);
215 for (j = 0; j < 16; ++j) {
217 saa7111_read(client, i + j));
224 case DECODER_GET_CAPABILITIES:
226 struct video_decoder_capability *cap = arg;
228 cap->flags = VIDEO_DECODER_PAL |
230 VIDEO_DECODER_SECAM |
238 case DECODER_GET_STATUS:
244 status = saa7111_read(client, 0x1f);
245 dprintk(1, KERN_DEBUG "%s status: 0x%02x\n", I2C_NAME(client),
248 if ((status & (1 << 6)) == 0) {
249 res |= DECODER_STATUS_GOOD;
251 switch (decoder->norm) {
252 case VIDEO_MODE_NTSC:
253 res |= DECODER_STATUS_NTSC;
256 res |= DECODER_STATUS_PAL;
258 case VIDEO_MODE_SECAM:
259 res |= DECODER_STATUS_SECAM;
262 case VIDEO_MODE_AUTO:
263 if ((status & (1 << 5)) != 0) {
264 res |= DECODER_STATUS_NTSC;
266 res |= DECODER_STATUS_PAL;
270 if ((status & (1 << 0)) != 0) {
271 res |= DECODER_STATUS_COLOR;
277 case DECODER_SET_NORM:
283 case VIDEO_MODE_NTSC:
284 saa7111_write(client, 0x08,
285 (decoder->reg[0x08] & 0x3f) | 0x40);
286 saa7111_write(client, 0x0e,
287 (decoder->reg[0x0e] & 0x8f));
291 saa7111_write(client, 0x08,
292 (decoder->reg[0x08] & 0x3f) | 0x00);
293 saa7111_write(client, 0x0e,
294 (decoder->reg[0x0e] & 0x8f));
297 case VIDEO_MODE_SECAM:
298 saa7111_write(client, 0x08,
299 (decoder->reg[0x0e] & 0x3f) | 0x00);
300 saa7111_write(client, 0x0e,
301 (decoder->reg[0x0e] & 0x8f) | 0x50);
304 case VIDEO_MODE_AUTO:
305 saa7111_write(client, 0x08,
306 (decoder->reg[0x08] & 0x3f) | 0x80);
307 saa7111_write(client, 0x0e,
308 (decoder->reg[0x0e] & 0x8f));
315 decoder->norm = *iarg;
319 case DECODER_SET_INPUT:
323 if (*iarg < 0 || *iarg > 7) {
327 if (decoder->input != *iarg) {
328 decoder->input = *iarg;
330 saa7111_write(client, 0x02,
332 reg[0x02] & 0xf8) | decoder->input);
333 /* bypass chrominance trap for modes 4..7 */
334 saa7111_write(client, 0x09,
336 reg[0x09] & 0x7f) | ((decoder->
344 case DECODER_SET_OUTPUT:
348 /* not much choice of outputs */
355 case DECODER_ENABLE_OUTPUT:
358 int enable = (*iarg != 0);
360 if (decoder->enable != enable) {
361 decoder->enable = enable;
363 /* RJ: If output should be disabled (for
364 * playing videos), we also need a open PLL.
365 * The input is set to 0 (where no input
366 * source is connected), although this
369 * If output should be enabled, we have to
373 if (decoder->enable) {
374 saa7111_write(client, 0x02,
378 saa7111_write(client, 0x08,
379 (decoder->reg[0x08] & 0xfb));
380 saa7111_write(client, 0x11,
382 reg[0x11] & 0xf3) | 0x0c);
384 saa7111_write(client, 0x02,
385 (decoder->reg[0x02] & 0xf8));
386 saa7111_write(client, 0x08,
388 reg[0x08] & 0xfb) | 0x04);
389 saa7111_write(client, 0x11,
390 (decoder->reg[0x11] & 0xf3));
396 case DECODER_SET_PICTURE:
398 struct video_picture *pic = arg;
400 if (decoder->bright != pic->brightness) {
401 /* We want 0 to 255 we get 0-65535 */
402 decoder->bright = pic->brightness;
403 saa7111_write(client, 0x0a, decoder->bright >> 8);
405 if (decoder->contrast != pic->contrast) {
406 /* We want 0 to 127 we get 0-65535 */
407 decoder->contrast = pic->contrast;
408 saa7111_write(client, 0x0b,
409 decoder->contrast >> 9);
411 if (decoder->sat != pic->colour) {
412 /* We want 0 to 127 we get 0-65535 */
413 decoder->sat = pic->colour;
414 saa7111_write(client, 0x0c, decoder->sat >> 9);
416 if (decoder->hue != pic->hue) {
417 /* We want -128 to 127 we get 0-65535 */
418 decoder->hue = pic->hue;
419 saa7111_write(client, 0x0d,
420 (decoder->hue - 32768) >> 8);
432 /* ----------------------------------------------------------------------- */
436 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
438 static unsigned short normal_i2c[] = { I2C_SAA7111 >> 1, I2C_CLIENT_END };
439 static unsigned short normal_i2c_range[] = { I2C_CLIENT_END };
441 static unsigned short probe[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
442 static unsigned short probe_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
443 static unsigned short ignore[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
444 static unsigned short ignore_range[2] = { I2C_CLIENT_END, I2C_CLIENT_END };
445 static unsigned short force[2] = { I2C_CLIENT_END , I2C_CLIENT_END };
447 static struct i2c_client_address_data addr_data = {
448 .normal_i2c = normal_i2c,
449 .normal_i2c_range = normal_i2c_range,
451 .probe_range = probe_range,
453 .ignore_range = ignore_range,
457 static int saa7111_i2c_id = 0;
458 static struct i2c_driver i2c_driver_saa7111;
461 saa7111_detect_client (struct i2c_adapter *adapter,
466 struct i2c_client *client;
467 struct saa7111 *decoder;
471 "saa7111.c: detecting saa7111 client on address 0x%x\n",
474 /* Check if the adapter supports the needed features */
475 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
478 client = kmalloc(sizeof(struct i2c_client), GFP_KERNEL);
481 memset(client, 0, sizeof(struct i2c_client));
482 client->addr = address;
483 client->adapter = adapter;
484 client->driver = &i2c_driver_saa7111;
485 client->flags = I2C_CLIENT_ALLOW_USE;
486 client->id = saa7111_i2c_id++;
487 snprintf(I2C_NAME(client), sizeof(I2C_NAME(client)) - 1,
488 "saa7111[%d]", client->id);
490 decoder = kmalloc(sizeof(struct saa7111), GFP_KERNEL);
491 if (decoder == NULL) {
495 memset(decoder, 0, sizeof(struct saa7111));
496 decoder->norm = VIDEO_MODE_NTSC;
499 decoder->bright = 32768;
500 decoder->contrast = 32768;
501 decoder->hue = 32768;
502 decoder->sat = 32768;
503 i2c_set_clientdata(client, decoder);
505 i = i2c_attach_client(client);
512 i = saa7111_write_block(client, init, sizeof(init));
514 dprintk(1, KERN_ERR "%s_attach error: init status %d\n",
515 I2C_NAME(client), i);
519 "%s_attach: chip version %x at address 0x%x\n",
520 I2C_NAME(client), saa7111_read(client, 0x00) >> 4,
528 saa7111_attach_adapter (struct i2c_adapter *adapter)
532 "saa7111.c: starting probe for adapter %s (0x%x)\n",
533 I2C_NAME(adapter), adapter->id);
534 return i2c_probe(adapter, &addr_data, &saa7111_detect_client);
538 saa7111_detach_client (struct i2c_client *client)
540 struct saa7111 *decoder = i2c_get_clientdata(client);
543 err = i2c_detach_client(client);
554 /* ----------------------------------------------------------------------- */
556 static struct i2c_driver i2c_driver_saa7111 = {
557 .owner = THIS_MODULE,
560 .id = I2C_DRIVERID_SAA7111A,
561 .flags = I2C_DF_NOTIFY,
563 .attach_adapter = saa7111_attach_adapter,
564 .detach_client = saa7111_detach_client,
565 .command = saa7111_command,
571 return i2c_add_driver(&i2c_driver_saa7111);
577 i2c_del_driver(&i2c_driver_saa7111);
580 module_init(saa7111_init);
581 module_exit(saa7111_exit);