2 * saa7191.c - Philips SAA7191 video decoder driver
4 * Copyright (C) 2003 Ladislav Michl <ladis@linux-mips.org>
5 * Copyright (C) 2004,2005 Mikael Nousiainen <tmnousia@cc.hut.fi>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
12 #include <linux/delay.h>
13 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
17 #include <linux/major.h>
18 #include <linux/module.h>
20 #include <linux/sched.h>
21 #include <linux/slab.h>
23 #include <linux/videodev.h>
24 #include <linux/video_decoder.h>
25 #include <linux/i2c.h>
29 #define SAA7191_MODULE_VERSION "0.0.5"
31 MODULE_DESCRIPTION("Philips SAA7191 video decoder driver");
32 MODULE_VERSION(SAA7191_MODULE_VERSION
);
33 MODULE_AUTHOR("Mikael Nousiainen <tmnousia@cc.hut.fi>");
34 MODULE_LICENSE("GPL");
36 // #define SAA7191_DEBUG
39 #define dprintk(x...) printk("SAA7191: " x);
44 #define SAA7191_SYNC_COUNT 30
45 #define SAA7191_SYNC_DELAY 100 /* milliseconds */
48 struct i2c_client
*client
;
50 /* the register values are stored here as the actual
51 * I2C-registers are write-only */
58 static struct i2c_driver i2c_driver_saa7191
;
60 static const u8 initseq
[] = {
63 0x50, /* (0x50) SAA7191_REG_IDEL */
65 /* 50 Hz signal timing */
66 0x30, /* (0x30) SAA7191_REG_HSYB */
67 0x00, /* (0x00) SAA7191_REG_HSYS */
68 0xe8, /* (0xe8) SAA7191_REG_HCLB */
69 0xb6, /* (0xb6) SAA7191_REG_HCLS */
70 0xf4, /* (0xf4) SAA7191_REG_HPHI */
73 SAA7191_LUMA_APER_1
, /* (0x01) SAA7191_REG_LUMA - CVBS mode */
74 0x00, /* (0x00) SAA7191_REG_HUEC */
75 0xf8, /* (0xf8) SAA7191_REG_CKTQ */
76 0xf8, /* (0xf8) SAA7191_REG_CKTS */
77 0x90, /* (0x90) SAA7191_REG_PLSE */
78 0x90, /* (0x90) SAA7191_REG_SESE */
79 0x00, /* (0x00) SAA7191_REG_GAIN */
80 SAA7191_STDC_NFEN
| SAA7191_STDC_HRMV
, /* (0x0c) SAA7191_REG_STDC
82 * slow time constant */
83 SAA7191_IOCK_OEDC
| SAA7191_IOCK_OEHS
| SAA7191_IOCK_OEVS
84 | SAA7191_IOCK_OEDY
, /* (0x78) SAA7191_REG_IOCK
85 * - chroma from CVBS, GPSW1 & 2 off */
86 SAA7191_CTL3_AUFD
| SAA7191_CTL3_SCEN
| SAA7191_CTL3_OFTS
87 | SAA7191_CTL3_YDEL0
, /* (0x99) SAA7191_REG_CTL3
88 * - automatic field detection */
89 0x00, /* (0x00) SAA7191_REG_CTL4 */
90 0x2c, /* (0x2c) SAA7191_REG_CHCV - PAL nominal value */
94 /* 60 Hz signal timing */
95 0x34, /* (0x34) SAA7191_REG_HS6B */
96 0x0a, /* (0x0a) SAA7191_REG_HS6S */
97 0xf4, /* (0xf4) SAA7191_REG_HC6B */
98 0xce, /* (0xce) SAA7191_REG_HC6S */
99 0xf4, /* (0xf4) SAA7191_REG_HP6I */
102 /* SAA7191 register handling */
104 static u8
saa7191_read_reg(struct i2c_client
*client
,
107 return ((struct saa7191
*)i2c_get_clientdata(client
))->reg
[reg
];
110 static int saa7191_read_status(struct i2c_client
*client
,
115 ret
= i2c_master_recv(client
, value
, 1);
117 printk(KERN_ERR
"SAA7191: saa7191_read_status(): read failed\n");
125 static int saa7191_write_reg(struct i2c_client
*client
, u8 reg
,
128 ((struct saa7191
*)i2c_get_clientdata(client
))->reg
[reg
] = value
;
129 return i2c_smbus_write_byte_data(client
, reg
, value
);
132 /* the first byte of data must be the first subaddress number (register) */
133 static int saa7191_write_block(struct i2c_client
*client
,
139 struct saa7191
*decoder
= (struct saa7191
*)i2c_get_clientdata(client
);
140 for (i
= 0; i
< (length
- 1); i
++) {
141 decoder
->reg
[data
[0] + i
] = data
[i
+ 1];
144 ret
= i2c_master_send(client
, data
, length
);
146 printk(KERN_ERR
"SAA7191: saa7191_write_block(): "
154 /* Helper functions */
156 static int saa7191_set_input(struct i2c_client
*client
, int input
)
158 struct saa7191
*decoder
= i2c_get_clientdata(client
);
159 u8 luma
= saa7191_read_reg(client
, SAA7191_REG_LUMA
);
160 u8 iock
= saa7191_read_reg(client
, SAA7191_REG_IOCK
);
164 case SAA7191_INPUT_COMPOSITE
: /* Set Composite input */
165 iock
&= ~(SAA7191_IOCK_CHRS
| SAA7191_IOCK_GPSW1
166 | SAA7191_IOCK_GPSW2
);
167 /* Chrominance trap active */
168 luma
&= ~SAA7191_LUMA_BYPS
;
170 case SAA7191_INPUT_SVIDEO
: /* Set S-Video input */
171 iock
|= SAA7191_IOCK_CHRS
| SAA7191_IOCK_GPSW2
;
172 /* Chrominance trap bypassed */
173 luma
|= SAA7191_LUMA_BYPS
;
179 err
= saa7191_write_reg(client
, SAA7191_REG_LUMA
, luma
);
182 err
= saa7191_write_reg(client
, SAA7191_REG_IOCK
, iock
);
186 decoder
->input
= input
;
191 static int saa7191_set_norm(struct i2c_client
*client
, int norm
)
193 struct saa7191
*decoder
= i2c_get_clientdata(client
);
194 u8 stdc
= saa7191_read_reg(client
, SAA7191_REG_STDC
);
195 u8 ctl3
= saa7191_read_reg(client
, SAA7191_REG_CTL3
);
196 u8 chcv
= saa7191_read_reg(client
, SAA7191_REG_CHCV
);
200 case SAA7191_NORM_PAL
:
201 stdc
&= ~SAA7191_STDC_SECS
;
202 ctl3
&= ~(SAA7191_CTL3_AUFD
| SAA7191_CTL3_FSEL
);
203 chcv
= SAA7191_CHCV_PAL
;
205 case SAA7191_NORM_NTSC
:
206 stdc
&= ~SAA7191_STDC_SECS
;
207 ctl3
&= ~SAA7191_CTL3_AUFD
;
208 ctl3
|= SAA7191_CTL3_FSEL
;
209 chcv
= SAA7191_CHCV_NTSC
;
211 case SAA7191_NORM_SECAM
:
212 stdc
|= SAA7191_STDC_SECS
;
213 ctl3
&= ~(SAA7191_CTL3_AUFD
| SAA7191_CTL3_FSEL
);
214 chcv
= SAA7191_CHCV_PAL
;
220 err
= saa7191_write_reg(client
, SAA7191_REG_CTL3
, ctl3
);
223 err
= saa7191_write_reg(client
, SAA7191_REG_STDC
, stdc
);
226 err
= saa7191_write_reg(client
, SAA7191_REG_CHCV
, chcv
);
230 decoder
->norm
= norm
;
232 dprintk("ctl3: %02x stdc: %02x chcv: %02x\n", ctl3
,
234 dprintk("norm: %d\n", norm
);
239 static int saa7191_wait_for_signal(struct i2c_client
*client
, u8
*status
)
243 dprintk("Checking for signal...\n");
245 for (i
= 0; i
< SAA7191_SYNC_COUNT
; i
++) {
246 if (saa7191_read_status(client
, status
))
249 if (((*status
) & SAA7191_STATUS_HLCK
) == 0) {
250 dprintk("Signal found\n");
254 msleep(SAA7191_SYNC_DELAY
);
257 dprintk("No signal\n");
262 static int saa7191_autodetect_norm_extended(struct i2c_client
*client
)
264 u8 stdc
= saa7191_read_reg(client
, SAA7191_REG_STDC
);
265 u8 ctl3
= saa7191_read_reg(client
, SAA7191_REG_CTL3
);
269 dprintk("SAA7191 extended signal auto-detection...\n");
271 stdc
&= ~SAA7191_STDC_SECS
;
272 ctl3
&= ~(SAA7191_CTL3_FSEL
);
274 err
= saa7191_write_reg(client
, SAA7191_REG_STDC
, stdc
);
279 err
= saa7191_write_reg(client
, SAA7191_REG_CTL3
, ctl3
);
285 ctl3
|= SAA7191_CTL3_AUFD
;
286 err
= saa7191_write_reg(client
, SAA7191_REG_CTL3
, ctl3
);
292 msleep(SAA7191_SYNC_DELAY
);
294 err
= saa7191_wait_for_signal(client
, &status
);
298 if (status
& SAA7191_STATUS_FIDT
) {
299 /* 60Hz signal -> NTSC */
300 dprintk("60Hz signal: NTSC\n");
301 return saa7191_set_norm(client
, SAA7191_NORM_NTSC
);
305 dprintk("50Hz signal: Trying PAL...\n");
308 err
= saa7191_set_norm(client
, SAA7191_NORM_PAL
);
312 msleep(SAA7191_SYNC_DELAY
);
314 err
= saa7191_wait_for_signal(client
, &status
);
319 if (status
& SAA7191_STATUS_FIDT
) {
320 dprintk("No 50Hz signal\n");
325 if (status
& SAA7191_STATUS_CODE
) {
330 dprintk("No color detected with PAL - Trying SECAM...\n");
332 /* no color detected ? -> try SECAM */
333 err
= saa7191_set_norm(client
,
338 msleep(SAA7191_SYNC_DELAY
);
340 err
= saa7191_wait_for_signal(client
, &status
);
345 if (status
& SAA7191_STATUS_FIDT
) {
346 dprintk("No 50Hz signal\n");
351 if (status
& SAA7191_STATUS_CODE
) {
352 /* Color detected -> SECAM */
357 dprintk("No color detected with SECAM - Going back to PAL.\n");
359 /* still no color detected ?
360 * -> set norm back to PAL */
361 err
= saa7191_set_norm(client
,
367 ctl3
= saa7191_read_reg(client
, SAA7191_REG_CTL3
);
368 if (ctl3
& SAA7191_CTL3_AUFD
) {
369 ctl3
&= ~(SAA7191_CTL3_AUFD
);
370 err
= saa7191_write_reg(client
, SAA7191_REG_CTL3
, ctl3
);
379 static int saa7191_autodetect_norm(struct i2c_client
*client
)
383 dprintk("SAA7191 signal auto-detection...\n");
385 dprintk("Reading status...\n");
387 if (saa7191_read_status(client
, &status
))
390 dprintk("Checking for signal...\n");
393 if (status
& SAA7191_STATUS_HLCK
) {
394 dprintk("No signal\n");
398 dprintk("Signal found\n");
400 if (status
& SAA7191_STATUS_FIDT
) {
401 /* 60hz signal -> NTSC */
403 return saa7191_set_norm(client
, SAA7191_NORM_NTSC
);
405 /* 50hz signal -> PAL */
407 return saa7191_set_norm(client
, SAA7191_NORM_PAL
);
411 static int saa7191_get_control(struct i2c_client
*client
,
412 struct saa7191_control
*ctrl
)
417 switch (ctrl
->type
) {
418 case SAA7191_CONTROL_BANDPASS
:
419 case SAA7191_CONTROL_BANDPASS_WEIGHT
:
420 case SAA7191_CONTROL_CORING
:
421 reg
= saa7191_read_reg(client
, SAA7191_REG_LUMA
);
422 switch (ctrl
->type
) {
423 case SAA7191_CONTROL_BANDPASS
:
424 ctrl
->value
= ((s32
)reg
& SAA7191_LUMA_BPSS_MASK
)
425 >> SAA7191_LUMA_BPSS_SHIFT
;
427 case SAA7191_CONTROL_BANDPASS_WEIGHT
:
428 ctrl
->value
= ((s32
)reg
& SAA7191_LUMA_APER_MASK
)
429 >> SAA7191_LUMA_APER_SHIFT
;
431 case SAA7191_CONTROL_CORING
:
432 ctrl
->value
= ((s32
)reg
& SAA7191_LUMA_CORI_MASK
)
433 >> SAA7191_LUMA_CORI_SHIFT
;
437 case SAA7191_CONTROL_FORCE_COLOUR
:
438 case SAA7191_CONTROL_CHROMA_GAIN
:
439 reg
= saa7191_read_reg(client
, SAA7191_REG_GAIN
);
440 if (ctrl
->type
== SAA7191_CONTROL_FORCE_COLOUR
)
441 ctrl
->value
= ((s32
)reg
& SAA7191_GAIN_COLO
) ? 1 : 0;
443 ctrl
->value
= ((s32
)reg
& SAA7191_GAIN_LFIS_MASK
)
444 >> SAA7191_GAIN_LFIS_SHIFT
;
446 case SAA7191_CONTROL_HUE
:
447 reg
= saa7191_read_reg(client
, SAA7191_REG_HUEC
);
452 ctrl
->value
= (s32
)reg
;
454 case SAA7191_CONTROL_VTRC
:
455 reg
= saa7191_read_reg(client
, SAA7191_REG_STDC
);
456 ctrl
->value
= ((s32
)reg
& SAA7191_STDC_VTRC
) ? 1 : 0;
458 case SAA7191_CONTROL_LUMA_DELAY
:
459 reg
= saa7191_read_reg(client
, SAA7191_REG_CTL3
);
460 ctrl
->value
= ((s32
)reg
& SAA7191_CTL3_YDEL_MASK
)
461 >> SAA7191_CTL3_YDEL_SHIFT
;
462 if (ctrl
->value
>= 4)
465 case SAA7191_CONTROL_VNR
:
466 reg
= saa7191_read_reg(client
, SAA7191_REG_CTL4
);
467 ctrl
->value
= ((s32
)reg
& SAA7191_CTL4_VNOI_MASK
)
468 >> SAA7191_CTL4_VNOI_SHIFT
;
477 static int saa7191_set_control(struct i2c_client
*client
,
478 struct saa7191_control
*ctrl
)
483 switch (ctrl
->type
) {
484 case SAA7191_CONTROL_BANDPASS
:
485 case SAA7191_CONTROL_BANDPASS_WEIGHT
:
486 case SAA7191_CONTROL_CORING
:
487 reg
= saa7191_read_reg(client
, SAA7191_REG_LUMA
);
488 switch (ctrl
->type
) {
489 case SAA7191_CONTROL_BANDPASS
:
490 reg
&= ~SAA7191_LUMA_BPSS_MASK
;
491 reg
|= (ctrl
->value
<< SAA7191_LUMA_BPSS_SHIFT
)
492 & SAA7191_LUMA_BPSS_MASK
;
494 case SAA7191_CONTROL_BANDPASS_WEIGHT
:
495 reg
&= ~SAA7191_LUMA_APER_MASK
;
496 reg
|= (ctrl
->value
<< SAA7191_LUMA_APER_SHIFT
)
497 & SAA7191_LUMA_APER_MASK
;
499 case SAA7191_CONTROL_CORING
:
500 reg
&= ~SAA7191_LUMA_CORI_MASK
;
501 reg
|= (ctrl
->value
<< SAA7191_LUMA_CORI_SHIFT
)
502 & SAA7191_LUMA_CORI_MASK
;
505 ret
= saa7191_write_reg(client
, SAA7191_REG_LUMA
, reg
);
507 case SAA7191_CONTROL_FORCE_COLOUR
:
508 case SAA7191_CONTROL_CHROMA_GAIN
:
509 reg
= saa7191_read_reg(client
, SAA7191_REG_GAIN
);
510 if (ctrl
->type
== SAA7191_CONTROL_FORCE_COLOUR
) {
512 reg
|= SAA7191_GAIN_COLO
;
514 reg
&= ~SAA7191_GAIN_COLO
;
516 reg
&= ~SAA7191_GAIN_LFIS_MASK
;
517 reg
|= (ctrl
->value
<< SAA7191_GAIN_LFIS_SHIFT
)
518 & SAA7191_GAIN_LFIS_MASK
;
520 ret
= saa7191_write_reg(client
, SAA7191_REG_GAIN
, reg
);
522 case SAA7191_CONTROL_HUE
:
523 reg
= ctrl
->value
& 0xff;
528 ret
= saa7191_write_reg(client
, SAA7191_REG_HUEC
, reg
);
530 case SAA7191_CONTROL_VTRC
:
531 reg
= saa7191_read_reg(client
, SAA7191_REG_STDC
);
533 reg
|= SAA7191_STDC_VTRC
;
535 reg
&= ~SAA7191_STDC_VTRC
;
536 ret
= saa7191_write_reg(client
, SAA7191_REG_STDC
, reg
);
538 case SAA7191_CONTROL_LUMA_DELAY
: {
539 s32 value
= ctrl
->value
;
542 reg
= saa7191_read_reg(client
, SAA7191_REG_CTL3
);
543 reg
&= ~SAA7191_CTL3_YDEL_MASK
;
544 reg
|= (value
<< SAA7191_CTL3_YDEL_SHIFT
)
545 & SAA7191_CTL3_YDEL_MASK
;
546 ret
= saa7191_write_reg(client
, SAA7191_REG_CTL3
, reg
);
549 case SAA7191_CONTROL_VNR
:
550 reg
= saa7191_read_reg(client
, SAA7191_REG_CTL4
);
551 reg
&= ~SAA7191_CTL4_VNOI_MASK
;
552 reg
|= (ctrl
->value
<< SAA7191_CTL4_VNOI_SHIFT
)
553 & SAA7191_CTL4_VNOI_MASK
;
554 ret
= saa7191_write_reg(client
, SAA7191_REG_CTL4
, reg
);
565 static int saa7191_attach(struct i2c_adapter
*adap
, int addr
, int kind
)
568 struct saa7191
*decoder
;
569 struct i2c_client
*client
;
571 printk(KERN_INFO
"Philips SAA7191 driver version %s\n",
572 SAA7191_MODULE_VERSION
);
574 client
= kmalloc(sizeof(*client
), GFP_KERNEL
);
577 decoder
= kmalloc(sizeof(*decoder
), GFP_KERNEL
);
580 goto out_free_client
;
583 memset(client
, 0, sizeof(struct i2c_client
));
584 memset(decoder
, 0, sizeof(struct saa7191
));
587 client
->adapter
= adap
;
588 client
->driver
= &i2c_driver_saa7191
;
590 strcpy(client
->name
, "saa7191 client");
591 i2c_set_clientdata(client
, decoder
);
593 decoder
->client
= client
;
595 err
= i2c_attach_client(client
);
597 goto out_free_decoder
;
599 err
= saa7191_write_block(client
, sizeof(initseq
), (u8
*)initseq
);
601 printk(KERN_ERR
"SAA7191 initialization failed\n");
602 goto out_detach_client
;
605 printk(KERN_INFO
"SAA7191 initialized\n");
607 decoder
->input
= SAA7191_INPUT_COMPOSITE
;
608 decoder
->norm
= SAA7191_NORM_PAL
;
610 err
= saa7191_autodetect_norm(client
);
611 if (err
&& (err
!= -EBUSY
)) {
612 printk(KERN_ERR
"SAA7191: Signal auto-detection failed\n");
618 i2c_detach_client(client
);
626 static int saa7191_probe(struct i2c_adapter
*adap
)
628 /* Always connected to VINO */
629 if (adap
->id
== I2C_HW_SGI_VINO
)
630 return saa7191_attach(adap
, SAA7191_ADDR
, 0);
631 /* Feel free to add probe here :-) */
635 static int saa7191_detach(struct i2c_client
*client
)
637 struct saa7191
*decoder
= i2c_get_clientdata(client
);
639 i2c_detach_client(client
);
645 static int saa7191_command(struct i2c_client
*client
, unsigned int cmd
,
648 struct saa7191
*decoder
= i2c_get_clientdata(client
);
651 case DECODER_GET_CAPABILITIES
: {
652 struct video_decoder_capability
*cap
= arg
;
654 cap
->flags
= VIDEO_DECODER_PAL
| VIDEO_DECODER_NTSC
|
655 VIDEO_DECODER_SECAM
| VIDEO_DECODER_AUTO
;
656 cap
->inputs
= (client
->adapter
->id
== I2C_HW_SGI_VINO
) ? 2 : 1;
660 case DECODER_GET_STATUS
: {
665 if (saa7191_read_status(client
, &status
)) {
668 if ((status
& SAA7191_STATUS_HLCK
) == 0)
669 res
|= DECODER_STATUS_GOOD
;
670 if (status
& SAA7191_STATUS_CODE
)
671 res
|= DECODER_STATUS_COLOR
;
672 switch (decoder
->norm
) {
673 case SAA7191_NORM_NTSC
:
674 res
|= DECODER_STATUS_NTSC
;
676 case SAA7191_NORM_PAL
:
677 res
|= DECODER_STATUS_PAL
;
679 case SAA7191_NORM_SECAM
:
680 res
|= DECODER_STATUS_SECAM
;
682 case SAA7191_NORM_AUTO
:
684 if (status
& SAA7191_STATUS_FIDT
)
685 res
|= DECODER_STATUS_NTSC
;
687 res
|= DECODER_STATUS_PAL
;
693 case DECODER_SET_NORM
: {
697 case VIDEO_MODE_AUTO
:
698 return saa7191_autodetect_norm(client
);
700 return saa7191_set_norm(client
, SAA7191_NORM_PAL
);
701 case VIDEO_MODE_NTSC
:
702 return saa7191_set_norm(client
, SAA7191_NORM_NTSC
);
703 case VIDEO_MODE_SECAM
:
704 return saa7191_set_norm(client
, SAA7191_NORM_SECAM
);
710 case DECODER_SET_INPUT
: {
713 switch (client
->adapter
->id
) {
714 case I2C_HW_SGI_VINO
:
715 return saa7191_set_input(client
, *iarg
);
722 case DECODER_SET_OUTPUT
: {
725 /* not much choice of outputs */
730 case DECODER_ENABLE_OUTPUT
: {
734 case DECODER_SET_PICTURE
: {
735 struct video_picture
*pic
= arg
;
739 val
= (pic
->hue
>> 8) - 0x80;
741 err
= saa7191_write_reg(client
, SAA7191_REG_HUEC
, val
);
747 case DECODER_SAA7191_GET_STATUS
: {
748 struct saa7191_status
*status
= arg
;
751 if (saa7191_read_status(client
, &status_reg
))
754 status
->signal
= ((status_reg
& SAA7191_STATUS_HLCK
) == 0)
756 status
->signal_60hz
= (status_reg
& SAA7191_STATUS_FIDT
)
758 status
->color
= (status_reg
& SAA7191_STATUS_CODE
) ? 1 : 0;
760 status
->input
= decoder
->input
;
761 status
->norm
= decoder
->norm
;
765 case DECODER_SAA7191_SET_NORM
: {
769 case SAA7191_NORM_AUTO
:
770 return saa7191_autodetect_norm(client
);
771 case SAA7191_NORM_AUTO_EXT
:
772 return saa7191_autodetect_norm_extended(client
);
774 return saa7191_set_norm(client
, *norm
);
777 case DECODER_SAA7191_GET_CONTROL
: {
778 return saa7191_get_control(client
, arg
);
780 case DECODER_SAA7191_SET_CONTROL
: {
781 return saa7191_set_control(client
, arg
);
790 static struct i2c_driver i2c_driver_saa7191
= {
791 .owner
= THIS_MODULE
,
793 .id
= I2C_DRIVERID_SAA7191
,
794 .flags
= I2C_DF_NOTIFY
,
795 .attach_adapter
= saa7191_probe
,
796 .detach_client
= saa7191_detach
,
797 .command
= saa7191_command
800 static int saa7191_init(void)
802 return i2c_add_driver(&i2c_driver_saa7191
);
805 static void saa7191_exit(void)
807 i2c_del_driver(&i2c_driver_saa7191
);
810 module_init(saa7191_init
);
811 module_exit(saa7191_exit
);