2 * saa7111 - Philips SAA7113A 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 <asm/segment.h>
46 #include <linux/types.h>
47 #include <asm/uaccess.h>
48 #include <linux/videodev.h>
50 MODULE_DESCRIPTION("Philips SAA7113 video decoder driver");
51 MODULE_AUTHOR("Dave Perks, Jose Ignacio Gijon, Joerg Heckenbach, Mark McClelland, Dwaine Garden");
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_PARM(debug
, "i");
63 MODULE_PARM_DESC(debug
, " Set the default Debug level. Default: 0 (Off) - (0-1)");
66 #define dprintk(num, format, args...) \
69 printk(format , ##args); \
72 /* ----------------------------------------------------------------------- */
75 unsigned char reg
[32];
86 #define I2C_SAA7113 0x4A
88 /* ----------------------------------------------------------------------- */
91 saa7113_write (struct i2c_client
*client
,
95 struct saa7113
*decoder
= i2c_get_clientdata(client
);
97 decoder
->reg
[reg
] = value
;
98 return i2c_smbus_write_byte_data(client
, reg
, value
);
102 saa7113_write_block (struct i2c_client
*client
,
109 /* the saa7113 has an autoincrement function, use it if
110 * the adapter understands raw I2C */
111 if (i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
)) {
112 /* do raw I2C, not smbus compatible */
113 struct saa7113
*decoder
= i2c_get_clientdata(client
);
117 msg
.addr
= client
->addr
;
120 msg
.buf
= (char *) block_data
;
122 block_data
[msg
.len
++] = reg
= data
[0];
124 block_data
[msg
.len
++] =
125 decoder
->reg
[reg
++] = data
[1];
128 } while (len
>= 2 && data
[0] == reg
&&
130 if ((ret
= i2c_transfer(client
->adapter
,
135 /* do some slow I2C emulation kind of thing */
138 if ((ret
= saa7113_write(client
, reg
,
149 saa7113_init_decoder (struct i2c_client
*client
,
150 struct video_decoder_init
*init
)
152 return saa7113_write_block(client
, init
->data
, init
->len
);
156 saa7113_read (struct i2c_client
*client
,
159 return i2c_smbus_read_byte_data(client
, reg
);
162 /* ----------------------------------------------------------------------- */
164 static const unsigned char saa7113_i2c_init
[] = {
165 0x00, 0x00, /* PH7113_CHIP_VERSION 00 - ID byte */
166 0x01, 0x08, /* PH7113_INCREMENT_DELAY - (1) (1) (1) (1) IDEL3 IDEL2 IDELL1 IDEL0 */
167 0x02, 0xc0, /* PH7113_ANALOG_INPUT_CONTR_1 - FUSE1 FUSE0 GUDL1 GUDL0 MODE3 MODE2 MODE1 MODE0 */
168 0x03, 0x23, /* PH7113_ANALOG_INPUT_CONTR_2 - (1) HLNRS VBSL WPOFF HOLDG GAFIX GAI28 GAI18 */
169 0x04, 0x00, /* PH7113_ANALOG_INPUT_CONTR_3 - GAI17 GAI16 GAI15 GAI14 GAI13 GAI12 GAI11 GAI10 */
170 0x05, 0x00, /* PH7113_ANALOG_INPUT_CONTR_4 - GAI27 GAI26 GAI25 GAI24 GAI23 GAI22 GAI21 GAI20 */
171 0x06, 0xeb, /* PH7113_HORIZONTAL_SYNC_START - HSB7 HSB6 HSB5 HSB4 HSB3 HSB2 HSB1 HSB0 */
172 0x07, 0xe0, /* PH7113_HORIZONTAL_SYNC_STOP - HSS7 HSS6 HSS5 HSS4 HSS3 HSS2 HSS1 HSS0 */
173 0x08, 0x88, /* PH7113_SYNC_CONTROL - AUFD FSEL FOET HTC1 HTC0 HPLL VNOI1 VNOI0 */
174 0x09, 0x00, /* PH7113_LUMINANCE_CONTROL - BYPS PREF BPSS1 BPSS0 VBLB UPTCV APER1 APER0 */
175 0x0a, 0x80, /* PH7113_LUMINANCE_BRIGHTNESS - BRIG7 BRIG6 BRIG5 BRIG4 BRIG3 BRIG2 BRIG1 BRIG0 */
176 0x0b, 0x47, /* PH7113_LUMINANCE_CONTRAST - CONT7 CONT6 CONT5 CONT4 CONT3 CONT2 CONT1 CONT0 */
177 0x0c, 0x40, /* PH7113_CHROMA_SATURATION - SATN7 SATN6 SATN5 SATN4 SATN3 SATN2 SATN1 SATN0 */
178 0x0d, 0x00, /* PH7113_CHROMA_HUE_CONTROL - HUEC7 HUEC6 HUEC5 HUEC4 HUEC3 HUEC2 HUEC1 HUEC0 */
179 0x0e, 0x01, /* PH7113_CHROMA_CONTROL - CDTO CSTD2 CSTD1 CSTD0 DCCF FCTC CHBW1 CHBW0 */
180 0x0f, 0xaa, /* PH7113_CHROMA_GAIN_CONTROL - ACGC CGAIN6 CGAIN5 CGAIN4 CGAIN3 CGAIN2 CGAIN1 CGAIN0 */
181 0x10, 0x00, /* PH7113_FORMAT_DELAY_CONTROL - OFTS1 OFTS0 HDEL1 HDEL0 VRLN YDEL2 YDEL1 YDEL0 */
182 0x11, 0x1C, /* PH7113_OUTPUT_CONTROL_1 - GPSW1 CM99 GPSW0 HLSEL OEYC OERT VIPB COLO */
183 0x12, 0x01, /* PH7113_OUTPUT_CONTROL_2 - RTSE13 RTSE12 RTSE11 RTSE10 RTSE03 RTSE02 RTSE01 RTSE00 */
184 0x13, 0x00, /* PH7113_OUTPUT_CONTROL_3 - ADLSB (1) (1) OLDSB FIDP (1) AOSL1 AOSL0 */
185 0x14, 0x00, /* RESERVED 14 - (1) (1) (1) (1) (1) (1) (1) (1) */
186 0x15, 0x00, /* PH7113_V_GATE1_START - VSTA7 VSTA6 VSTA5 VSTA4 VSTA3 VSTA2 VSTA1 VSTA0 */
187 0x16, 0x00, /* PH7113_V_GATE1_STOP - VSTO7 VSTO6 VSTO5 VSTO4 VSTO3 VSTO2 VSTO1 VSTO0 */
188 0x17, 0x00, /* PH7113_V_GATE1_MSB - (1) (1) (1) (1) (1) (1) VSTO8 VSTA8 */
192 saa7113_command (struct i2c_client
*client
,
196 struct saa7113
*decoder
= i2c_get_clientdata(client
);
203 struct video_decoder_init
*init
= arg
;
205 return saa7113_init_decoder(client
, init
);
207 struct video_decoder_init vdi
;
208 vdi
.data
= saa7113_i2c_init
;
209 vdi
.len
= sizeof(saa7113_i2c_init
);
210 return saa7113_init_decoder(client
, &vdi
);
218 for (i
= 0; i
< 32; i
+= 16) {
221 printk(KERN_DEBUG
"%s: %03x", I2C_NAME(client
), i
);
222 for (j
= 0; j
< 16; ++j
) {
224 saa7113_read(client
, i
+ j
));
231 case DECODER_GET_CAPABILITIES
:
233 struct video_decoder_capability
*cap
= arg
;
235 cap
->flags
= VIDEO_DECODER_PAL
|
237 VIDEO_DECODER_SECAM
|
245 case DECODER_GET_STATUS
:
251 status
= saa7113_read(client
, 0x1f);
252 dprintk(1, KERN_DEBUG
"%s status: 0x%02x\n", I2C_NAME(client
),
255 if ((status
& (1 << 6)) == 0) {
256 res
|= DECODER_STATUS_GOOD
;
258 switch (decoder
->norm
) {
259 case VIDEO_MODE_NTSC
:
260 res
|= DECODER_STATUS_NTSC
;
263 res
|= DECODER_STATUS_PAL
;
265 case VIDEO_MODE_SECAM
:
266 res
|= DECODER_STATUS_SECAM
;
269 case VIDEO_MODE_AUTO
:
270 if ((status
& (1 << 5)) != 0) {
271 res
|= DECODER_STATUS_NTSC
;
273 res
|= DECODER_STATUS_PAL
;
277 if ((status
& (1 << 0)) != 0) {
278 res
|= DECODER_STATUS_COLOR
;
284 case DECODER_SET_GPIO
:
288 saa7113_write(client
, 0x11,
289 (decoder
->reg
[0x11] | 0x80));
291 saa7113_write(client
, 0x11,
292 (decoder
->reg
[0x11] & 0x7f));
297 case DECODER_SET_VBI_BYPASS
:
301 saa7113_write(client
, 0x13,
302 (decoder
->reg
[0x13] & 0xf0) | 0x0a);
304 saa7113_write(client
, 0x13,
305 (decoder
->reg
[0x13] & 0xf0));
310 case DECODER_SET_NORM
:
316 case VIDEO_MODE_NTSC
:
317 saa7113_write(client
, 0x08,
318 (decoder
->reg
[0x08] & 0x3f) | 0x40);
319 saa7113_write(client
, 0x0e,
320 (decoder
->reg
[0x0e] & 0x8f));
324 saa7113_write(client
, 0x08,
325 (decoder
->reg
[0x08] & 0x3f) | 0x00);
326 saa7113_write(client
, 0x0e,
327 (decoder
->reg
[0x0e] & 0x8f));
330 case VIDEO_MODE_SECAM
:
331 saa7113_write(client
, 0x08,
332 (decoder
->reg
[0x0e] & 0x3f) | 0x00);
333 saa7113_write(client
, 0x0e,
334 (decoder
->reg
[0x0e] & 0x8f) | 0x50);
337 case VIDEO_MODE_AUTO
:
338 saa7113_write(client
, 0x08,
339 (decoder
->reg
[0x08] & 0x3f) | 0x80);
340 saa7113_write(client
, 0x0e,
341 (decoder
->reg
[0x0e] & 0x8f));
348 decoder
->norm
= *iarg
;
352 case DECODER_SET_INPUT
:
355 if (*iarg
< 0 || *iarg
> 9) {
358 if (decoder
->input
!= *iarg
) {
359 decoder
->input
= *iarg
;
361 saa7113_write(client
, 0x02,
362 (decoder
->reg
[0x02] & 0xf0) | decoder
->input
);
363 /* bypass chrominance trap for modes 4..7 */
364 saa7113_write(client
, 0x09,
365 (decoder
->reg
[0x09] & 0x7f) | ((decoder
->input
> 3) ? 0x80 : 0));
370 case DECODER_SET_OUTPUT
:
374 /* not much choice of outputs */
381 case DECODER_ENABLE_OUTPUT
:
384 int enable
= (*iarg
!= 0);
386 if (decoder
->enable
!= enable
) {
387 decoder
->enable
= enable
;
389 /* RJ: If output should be disabled (for
390 * playing videos), we also need a open PLL.
391 * The input is set to 0 (where no input
392 * source is connected), although this
395 * If output should be enabled, we have to
399 if (decoder
->enable
) {
400 saa7113_write(client
, 0x02,
404 saa7113_write(client
, 0x08,
405 (decoder
->reg
[0x08] & 0xfb));
406 saa7113_write(client
, 0x11,
408 reg
[0x11] & 0xf3) | 0x0c);
410 saa7113_write(client
, 0x02,
411 (decoder
->reg
[0x02] & 0xf8));
412 saa7113_write(client
, 0x08,
414 reg
[0x08] & 0xfb) | 0x04);
415 saa7113_write(client
, 0x11,
416 (decoder
->reg
[0x11] & 0xf3));
422 case DECODER_SET_PICTURE
:
424 struct video_picture
*pic
= arg
;
426 if (decoder
->bright
!= pic
->brightness
) {
427 /* We want 0 to 255 we get 0-65535 */
428 decoder
->bright
= pic
->brightness
;
429 saa7113_write(client
, 0x0a, decoder
->bright
>> 8);
431 if (decoder
->contrast
!= pic
->contrast
) {
432 /* We want 0 to 127 we get 0-65535 */
433 decoder
->contrast
= pic
->contrast
;
434 saa7113_write(client
, 0x0b,
435 decoder
->contrast
>> 9);
437 if (decoder
->sat
!= pic
->colour
) {
438 /* We want 0 to 127 we get 0-65535 */
439 decoder
->sat
= pic
->colour
;
440 saa7113_write(client
, 0x0c, decoder
->sat
>> 9);
442 if (decoder
->hue
!= pic
->hue
) {
443 /* We want -128 to 127 we get 0-65535 */
444 decoder
->hue
= pic
->hue
;
445 saa7113_write(client
, 0x0d,
446 (decoder
->hue
- 32768) >> 8);
458 /* ----------------------------------------------------------------------- */
462 * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
465 /* standard i2c insmod options */
466 static unsigned short normal_i2c
[] = {
467 I2C_SAA7113
>>1, /* saa7113 */
474 static struct i2c_driver i2c_driver_saa7113
;
477 saa7113_detect_client (struct i2c_adapter
*adapter
,
482 struct i2c_client
*client
;
483 struct saa7113
*decoder
;
484 struct video_decoder_init vdi
;
488 "saa7113.c: detecting saa7113 client on address 0x%x\n",
491 /* Check if the adapter supports the needed features */
492 if (!i2c_check_functionality(adapter
, I2C_FUNC_SMBUS_BYTE_DATA
))
495 client
= kmalloc(sizeof(struct i2c_client
), GFP_KERNEL
);
498 memset(client
, 0, sizeof(struct i2c_client
));
499 client
->addr
= address
;
500 client
->adapter
= adapter
;
501 client
->driver
= &i2c_driver_saa7113
;
502 client
->flags
= I2C_CLIENT_ALLOW_USE
;
503 strlcpy(I2C_NAME(client
), "saa7113", sizeof(I2C_NAME(client
)));
504 decoder
= kmalloc(sizeof(struct saa7113
), GFP_KERNEL
);
505 if (decoder
== NULL
) {
509 memset(decoder
, 0, sizeof(struct saa7113
));
510 decoder
->norm
= VIDEO_MODE_NTSC
;
513 decoder
->bright
= 32768;
514 decoder
->contrast
= 32768;
515 decoder
->hue
= 32768;
516 decoder
->sat
= 32768;
517 i2c_set_clientdata(client
, decoder
);
519 i
= i2c_attach_client(client
);
526 vdi
.data
= saa7113_i2c_init
;
527 vdi
.len
= sizeof(saa7113_i2c_init
);
528 i
= saa7113_init_decoder(client
, &vdi
);
530 dprintk(1, KERN_ERR
"%s_attach error: init status %d\n",
531 I2C_NAME(client
), i
);
535 "%s_attach: chip version %x at address 0x%x\n",
536 I2C_NAME(client
), saa7113_read(client
, 0x00) >> 4,
544 saa7113_attach_adapter (struct i2c_adapter
*adapter
)
548 "saa7113.c: starting probe for adapter %s (0x%x)\n",
549 I2C_NAME(adapter
), adapter
->id
);
550 return i2c_probe(adapter
, &addr_data
, &saa7113_detect_client
);
554 saa7113_detach_client (struct i2c_client
*client
)
556 struct saa7113
*decoder
= i2c_get_clientdata(client
);
559 err
= i2c_detach_client(client
);
570 /* ----------------------------------------------------------------------- */
572 static struct i2c_driver i2c_driver_saa7113
= {
573 .owner
= THIS_MODULE
,
576 .id
= I2C_DRIVERID_SAA7113
,
577 .flags
= I2C_DF_NOTIFY
,
579 .attach_adapter
= saa7113_attach_adapter
,
580 .detach_client
= saa7113_detach_client
,
581 .command
= saa7113_command
,
587 return i2c_add_driver(&i2c_driver_saa7113
);
593 i2c_del_driver(&i2c_driver_saa7113
);
596 module_init(saa7113_init
);
597 module_exit(saa7113_exit
);