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
= kzalloc(sizeof(*client
), GFP_KERNEL
);
577 decoder
= kzalloc(sizeof(*decoder
), GFP_KERNEL
);
580 goto out_free_client
;
584 client
->adapter
= adap
;
585 client
->driver
= &i2c_driver_saa7191
;
587 strcpy(client
->name
, "saa7191 client");
588 i2c_set_clientdata(client
, decoder
);
590 decoder
->client
= client
;
592 err
= i2c_attach_client(client
);
594 goto out_free_decoder
;
596 err
= saa7191_write_block(client
, sizeof(initseq
), (u8
*)initseq
);
598 printk(KERN_ERR
"SAA7191 initialization failed\n");
599 goto out_detach_client
;
602 printk(KERN_INFO
"SAA7191 initialized\n");
604 decoder
->input
= SAA7191_INPUT_COMPOSITE
;
605 decoder
->norm
= SAA7191_NORM_PAL
;
607 err
= saa7191_autodetect_norm(client
);
608 if (err
&& (err
!= -EBUSY
)) {
609 printk(KERN_ERR
"SAA7191: Signal auto-detection failed\n");
615 i2c_detach_client(client
);
623 static int saa7191_probe(struct i2c_adapter
*adap
)
625 /* Always connected to VINO */
626 if (adap
->id
== I2C_HW_SGI_VINO
)
627 return saa7191_attach(adap
, SAA7191_ADDR
, 0);
628 /* Feel free to add probe here :-) */
632 static int saa7191_detach(struct i2c_client
*client
)
634 struct saa7191
*decoder
= i2c_get_clientdata(client
);
636 i2c_detach_client(client
);
642 static int saa7191_command(struct i2c_client
*client
, unsigned int cmd
,
645 struct saa7191
*decoder
= i2c_get_clientdata(client
);
648 case DECODER_GET_CAPABILITIES
: {
649 struct video_decoder_capability
*cap
= arg
;
651 cap
->flags
= VIDEO_DECODER_PAL
| VIDEO_DECODER_NTSC
|
652 VIDEO_DECODER_SECAM
| VIDEO_DECODER_AUTO
;
653 cap
->inputs
= (client
->adapter
->id
== I2C_HW_SGI_VINO
) ? 2 : 1;
657 case DECODER_GET_STATUS
: {
662 if (saa7191_read_status(client
, &status
)) {
665 if ((status
& SAA7191_STATUS_HLCK
) == 0)
666 res
|= DECODER_STATUS_GOOD
;
667 if (status
& SAA7191_STATUS_CODE
)
668 res
|= DECODER_STATUS_COLOR
;
669 switch (decoder
->norm
) {
670 case SAA7191_NORM_NTSC
:
671 res
|= DECODER_STATUS_NTSC
;
673 case SAA7191_NORM_PAL
:
674 res
|= DECODER_STATUS_PAL
;
676 case SAA7191_NORM_SECAM
:
677 res
|= DECODER_STATUS_SECAM
;
679 case SAA7191_NORM_AUTO
:
681 if (status
& SAA7191_STATUS_FIDT
)
682 res
|= DECODER_STATUS_NTSC
;
684 res
|= DECODER_STATUS_PAL
;
690 case DECODER_SET_NORM
: {
694 case VIDEO_MODE_AUTO
:
695 return saa7191_autodetect_norm(client
);
697 return saa7191_set_norm(client
, SAA7191_NORM_PAL
);
698 case VIDEO_MODE_NTSC
:
699 return saa7191_set_norm(client
, SAA7191_NORM_NTSC
);
700 case VIDEO_MODE_SECAM
:
701 return saa7191_set_norm(client
, SAA7191_NORM_SECAM
);
707 case DECODER_SET_INPUT
: {
710 switch (client
->adapter
->id
) {
711 case I2C_HW_SGI_VINO
:
712 return saa7191_set_input(client
, *iarg
);
719 case DECODER_SET_OUTPUT
: {
722 /* not much choice of outputs */
727 case DECODER_ENABLE_OUTPUT
: {
731 case DECODER_SET_PICTURE
: {
732 struct video_picture
*pic
= arg
;
736 val
= (pic
->hue
>> 8) - 0x80;
738 err
= saa7191_write_reg(client
, SAA7191_REG_HUEC
, val
);
744 case DECODER_SAA7191_GET_STATUS
: {
745 struct saa7191_status
*status
= arg
;
748 if (saa7191_read_status(client
, &status_reg
))
751 status
->signal
= ((status_reg
& SAA7191_STATUS_HLCK
) == 0)
753 status
->signal_60hz
= (status_reg
& SAA7191_STATUS_FIDT
)
755 status
->color
= (status_reg
& SAA7191_STATUS_CODE
) ? 1 : 0;
757 status
->input
= decoder
->input
;
758 status
->norm
= decoder
->norm
;
762 case DECODER_SAA7191_SET_NORM
: {
766 case SAA7191_NORM_AUTO
:
767 return saa7191_autodetect_norm(client
);
768 case SAA7191_NORM_AUTO_EXT
:
769 return saa7191_autodetect_norm_extended(client
);
771 return saa7191_set_norm(client
, *norm
);
774 case DECODER_SAA7191_GET_CONTROL
: {
775 return saa7191_get_control(client
, arg
);
777 case DECODER_SAA7191_SET_CONTROL
: {
778 return saa7191_set_control(client
, arg
);
787 static struct i2c_driver i2c_driver_saa7191
= {
791 .id
= I2C_DRIVERID_SAA7191
,
792 .attach_adapter
= saa7191_probe
,
793 .detach_client
= saa7191_detach
,
794 .command
= saa7191_command
797 static int saa7191_init(void)
799 return i2c_add_driver(&i2c_driver_saa7191
);
802 static void saa7191_exit(void)
804 i2c_del_driver(&i2c_driver_saa7191
);
807 module_init(saa7191_init
);
808 module_exit(saa7191_exit
);