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/signal.h>
41 #include <asm/pgtable.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>
54 #define I2C_NAME(s) (s)->name
56 #include <linux/video_decoder.h>
59 module_param(debug, int, 0644);
60 MODULE_PARM_DESC(debug, "Debug level (0-1)");
62 #define dprintk(num, format, args...) \
65 printk(format, ##args); \
68 /* ----------------------------------------------------------------------- */
70 #define SAA7111_NR_REG 0x18
73 unsigned char reg[SAA7111_NR_REG];
80 #define I2C_SAA7111 0x48
82 /* ----------------------------------------------------------------------- */
85 saa7111_write (struct i2c_client *client,
89 struct saa7111 *decoder = i2c_get_clientdata(client);
91 decoder->reg[reg] = value;
92 return i2c_smbus_write_byte_data(client, reg, value);
96 saa7111_write_if_changed(struct i2c_client *client, u8 reg, u8 value)
98 struct saa7111 *decoder = i2c_get_clientdata(client);
100 if (decoder->reg[reg] != value) {
101 decoder->reg[reg] = value;
102 i2c_smbus_write_byte_data(client, reg, value);
107 saa7111_write_block (struct i2c_client *client,
114 /* the saa7111 has an autoincrement function, use it if
115 * the adapter understands raw I2C */
116 if (i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
117 /* do raw I2C, not smbus compatible */
118 struct saa7111 *decoder = i2c_get_clientdata(client);
124 block_data[block_len++] = reg = data[0];
126 block_data[block_len++] =
127 decoder->reg[reg++] = data[1];
130 } while (len >= 2 && data[0] == reg &&
132 if ((ret = i2c_master_send(client, block_data,
137 /* do some slow I2C emulation kind of thing */
140 if ((ret = saa7111_write(client, reg,
151 saa7111_init_decoder (struct i2c_client *client,
152 struct video_decoder_init *init)
154 return saa7111_write_block(client, init->data, init->len);
158 saa7111_read (struct i2c_client *client,
161 return i2c_smbus_read_byte_data(client, reg);
164 /* ----------------------------------------------------------------------- */
166 static const unsigned char saa7111_i2c_init[] = {
167 0x00, 0x00, /* 00 - ID byte */
168 0x01, 0x00, /* 01 - reserved */
171 0x02, 0xd0, /* 02 - FUSE=3, GUDL=2, MODE=0 */
172 0x03, 0x23, /* 03 - HLNRS=0, VBSL=1, WPOFF=0,
173 * HOLDG=0, GAFIX=0, GAI1=256, GAI2=256 */
174 0x04, 0x00, /* 04 - GAI1=256 */
175 0x05, 0x00, /* 05 - GAI2=256 */
178 0x06, 0xf3, /* 06 - HSB at 13(50Hz) / 17(60Hz)
179 * pixels after end of last line */
180 /*0x07, 0x13, * 07 - HSS at 113(50Hz) / 117(60Hz) pixels
181 * after end of last line */
182 0x07, 0xe8, /* 07 - HSS seems to be needed to
183 * work with NTSC, too */
184 0x08, 0xc8, /* 08 - AUFD=1, FSEL=1, EXFIL=0,
185 * VTRC=1, HPLL=0, VNOI=0 */
186 0x09, 0x01, /* 09 - BYPS=0, PREF=0, BPSS=0,
187 * VBLB=0, UPTCV=0, APER=1 */
188 0x0a, 0x80, /* 0a - BRIG=128 */
189 0x0b, 0x47, /* 0b - CONT=1.109 */
190 0x0c, 0x40, /* 0c - SATN=1.0 */
191 0x0d, 0x00, /* 0d - HUE=0 */
192 0x0e, 0x01, /* 0e - CDTO=0, CSTD=0, DCCF=0,
194 0x0f, 0x00, /* 0f - reserved */
195 0x10, 0x48, /* 10 - OFTS=1, HDEL=0, VRLN=1, YDEL=0 */
196 0x11, 0x1c, /* 11 - GPSW=0, CM99=0, FECO=0, COMPO=1,
197 * OEYC=1, OEHV=1, VIPB=0, COLO=0 */
198 0x12, 0x00, /* 12 - output control 2 */
199 0x13, 0x00, /* 13 - output control 3 */
200 0x14, 0x00, /* 14 - reserved */
201 0x15, 0x00, /* 15 - VBI */
202 0x16, 0x00, /* 16 - VBI */
203 0x17, 0x00, /* 17 - VBI */
207 saa7111_command (struct i2c_client *client,
211 struct saa7111 *decoder = i2c_get_clientdata(client);
219 struct video_decoder_init *init = arg;
221 return saa7111_init_decoder(client, init);
223 struct video_decoder_init vdi;
224 vdi.data = saa7111_i2c_init;
225 vdi.len = sizeof(saa7111_i2c_init);
226 return saa7111_init_decoder(client, &vdi);
234 for (i = 0; i < SAA7111_NR_REG; i += 16) {
237 printk(KERN_DEBUG "%s: %03x", I2C_NAME(client), i);
238 for (j = 0; j < 16 && i + j < SAA7111_NR_REG; ++j) {
240 saa7111_read(client, i + j));
247 case DECODER_GET_CAPABILITIES:
249 struct video_decoder_capability *cap = arg;
251 cap->flags = VIDEO_DECODER_PAL |
253 VIDEO_DECODER_SECAM |
261 case DECODER_GET_STATUS:
267 status = saa7111_read(client, 0x1f);
268 dprintk(1, KERN_DEBUG "%s status: 0x%02x\n", I2C_NAME(client),
271 if ((status & (1 << 6)) == 0) {
272 res |= DECODER_STATUS_GOOD;
274 switch (decoder->norm) {
275 case VIDEO_MODE_NTSC:
276 res |= DECODER_STATUS_NTSC;
279 res |= DECODER_STATUS_PAL;
281 case VIDEO_MODE_SECAM:
282 res |= DECODER_STATUS_SECAM;
285 case VIDEO_MODE_AUTO:
286 if ((status & (1 << 5)) != 0) {
287 res |= DECODER_STATUS_NTSC;
289 res |= DECODER_STATUS_PAL;
293 if ((status & (1 << 0)) != 0) {
294 res |= DECODER_STATUS_COLOR;
300 case DECODER_SET_GPIO:
304 saa7111_write(client, 0x11,
305 (decoder->reg[0x11] | 0x80));
307 saa7111_write(client, 0x11,
308 (decoder->reg[0x11] & 0x7f));
313 case DECODER_SET_VBI_BYPASS:
317 saa7111_write(client, 0x13,
318 (decoder->reg[0x13] & 0xf0) | 0x0a);
320 saa7111_write(client, 0x13,
321 (decoder->reg[0x13] & 0xf0));
326 case DECODER_SET_NORM:
332 case VIDEO_MODE_NTSC:
333 saa7111_write(client, 0x08,
334 (decoder->reg[0x08] & 0x3f) | 0x40);
335 saa7111_write(client, 0x0e,
336 (decoder->reg[0x0e] & 0x8f));
340 saa7111_write(client, 0x08,
341 (decoder->reg[0x08] & 0x3f) | 0x00);
342 saa7111_write(client, 0x0e,
343 (decoder->reg[0x0e] & 0x8f));
346 case VIDEO_MODE_SECAM:
347 saa7111_write(client, 0x08,
348 (decoder->reg[0x08] & 0x3f) | 0x00);
349 saa7111_write(client, 0x0e,
350 (decoder->reg[0x0e] & 0x8f) | 0x50);
353 case VIDEO_MODE_AUTO:
354 saa7111_write(client, 0x08,
355 (decoder->reg[0x08] & 0x3f) | 0x80);
356 saa7111_write(client, 0x0e,
357 (decoder->reg[0x0e] & 0x8f));
364 decoder->norm = *iarg;
368 case DECODER_SET_INPUT:
372 if (*iarg < 0 || *iarg > 7) {
376 if (decoder->input != *iarg) {
377 decoder->input = *iarg;
379 saa7111_write(client, 0x02,
381 reg[0x02] & 0xf8) | decoder->input);
382 /* bypass chrominance trap for modes 4..7 */
383 saa7111_write(client, 0x09,
385 reg[0x09] & 0x7f) | ((decoder->
393 case DECODER_SET_OUTPUT:
397 /* not much choice of outputs */
404 case DECODER_ENABLE_OUTPUT:
407 int enable = (*iarg != 0);
409 if (decoder->enable != enable) {
410 decoder->enable = enable;
412 /* RJ: If output should be disabled (for
413 * playing videos), we also need a open PLL.
414 * The input is set to 0 (where no input
415 * source is connected), although this
418 * If output should be enabled, we have to
422 if (decoder->enable) {
423 saa7111_write(client, 0x02,
427 saa7111_write(client, 0x08,
428 (decoder->reg[0x08] & 0xfb));
429 saa7111_write(client, 0x11,
431 reg[0x11] & 0xf3) | 0x0c);
433 saa7111_write(client, 0x02,
434 (decoder->reg[0x02] & 0xf8));
435 saa7111_write(client, 0x08,
437 reg[0x08] & 0xfb) | 0x04);
438 saa7111_write(client, 0x11,
439 (decoder->reg[0x11] & 0xf3));
445 case DECODER_SET_PICTURE:
447 struct video_picture *pic = arg;
449 /* We want 0 to 255 we get 0-65535 */
450 saa7111_write_if_changed(client, 0x0a, pic->brightness >> 8);
451 /* We want 0 to 127 we get 0-65535 */
452 saa7111_write(client, 0x0b, pic->contrast >> 9);
453 /* We want 0 to 127 we get 0-65535 */
454 saa7111_write(client, 0x0c, pic->colour >> 9);
455 /* We want -128 to 127 we get 0-65535 */
456 saa7111_write(client, 0x0d, (pic->hue - 32768) >> 8);
467 /* ----------------------------------------------------------------------- */
471 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
473 static unsigned short normal_i2c[] = { I2C_SAA7111 >> 1, I2C_CLIENT_END };
475 static unsigned short ignore = I2C_CLIENT_END;
477 static struct i2c_client_address_data addr_data = {
478 .normal_i2c = normal_i2c,
483 static struct i2c_driver i2c_driver_saa7111;
486 saa7111_detect_client (struct i2c_adapter *adapter,
491 struct i2c_client *client;
492 struct saa7111 *decoder;
493 struct video_decoder_init vdi;
497 "saa7111.c: detecting saa7111 client on address 0x%x\n",
500 /* Check if the adapter supports the needed features */
501 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
504 client = kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
507 client->addr = address;
508 client->adapter = adapter;
509 client->driver = &i2c_driver_saa7111;
510 strlcpy(I2C_NAME(client), "saa7111", sizeof(I2C_NAME(client)));
512 decoder = kzalloc(sizeof(struct saa7111), GFP_KERNEL);
513 if (decoder == NULL) {
517 decoder->norm = VIDEO_MODE_NTSC;
520 i2c_set_clientdata(client, decoder);
522 i = i2c_attach_client(client);
529 vdi.data = saa7111_i2c_init;
530 vdi.len = sizeof(saa7111_i2c_init);
531 i = saa7111_init_decoder(client, &vdi);
533 dprintk(1, KERN_ERR "%s_attach error: init status %d\n",
534 I2C_NAME(client), i);
538 "%s_attach: chip version %x at address 0x%x\n",
539 I2C_NAME(client), saa7111_read(client, 0x00) >> 4,
547 saa7111_attach_adapter (struct i2c_adapter *adapter)
551 "saa7111.c: starting probe for adapter %s (0x%x)\n",
552 I2C_NAME(adapter), adapter->id);
553 return i2c_probe(adapter, &addr_data, &saa7111_detect_client);
557 saa7111_detach_client (struct i2c_client *client)
559 struct saa7111 *decoder = i2c_get_clientdata(client);
562 err = i2c_detach_client(client);
573 /* ----------------------------------------------------------------------- */
575 static struct i2c_driver i2c_driver_saa7111 = {
580 .id = I2C_DRIVERID_SAA7111A,
582 .attach_adapter = saa7111_attach_adapter,
583 .detach_client = saa7111_detach_client,
584 .command = saa7111_command,
590 return i2c_add_driver(&i2c_driver_saa7111);
596 i2c_del_driver(&i2c_driver_saa7111);
599 module_init(saa7111_init);
600 module_exit(saa7111_exit);