24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
30 #include <asm/div64.h>
37 static unsigned int audio_debug;
41 static unsigned int audio_ddep;
45 static int audio_clock_override =
UNSET;
48 static int audio_clock_tweak;
50 MODULE_PARM_DESC(audio_clock_tweak,
"Audio clock tick fine tuning for cards with audio crystal that's slightly off (range [-1024 .. 1024])");
52 #define dprintk(fmt, arg...) if (audio_debug) \
53 printk(KERN_DEBUG "%s/audio: " fmt, dev->name , ## arg)
54 #define d2printk(fmt, arg...) if (audio_debug > 1) \
55 printk(KERN_DEBUG "%s/audio: " fmt, dev->name, ## arg)
57 #define print_regb(reg) printk("%s: reg 0x%03x [%-16s]: 0x%02x\n", \
58 dev->name,(SAA7134_##reg),(#reg),saa_readb((SAA7134_##reg)))
61 #define SCAN_INITIAL_DELAY 1000
62 #define SCAN_SAMPLE_DELAY 200
63 #define SCAN_SUBCARRIER_DELAY 2000
68 static struct mainscan {
94 .name =
"PAL-B/G FM-stereo",
100 .name =
"PAL-D/K1 FM-stereo",
106 .name =
"PAL-D/K2 FM-stereo",
112 .name =
"PAL-D/K3 FM-stereo",
118 .name =
"PAL-B/G NICAM",
124 .name =
"PAL-I NICAM",
130 .name =
"PAL-D/K NICAM",
136 .name =
"SECAM-L NICAM",
142 .name =
"SECAM-D/K NICAM",
148 .name =
"NTSC-A2 FM-stereo",
161 #define TVAUDIO ARRAY_SIZE(tvaudio)
165 static u32 tvaudio_carr2reg(
u32 carrier)
183 #define SAA7134_MUTE_MASK 0xbb
184 #define SAA7134_MUTE_ANALOG 0x04
185 #define SAA7134_MUTE_I2S 0x40
187 static void mute_input_7134(
struct saa7134_dev *dev)
191 int ausel=0,
ics=0, ocs=0;
198 if (
card(dev).mute.name) {
204 in = &
card(dev).mute;
209 dprintk(
"mute/input: nothing to do [mute=%d,input=%s]\n",
214 dprintk(
"ctl_mute=%d automute=%d input=%s => mute=%d input=%s\n",
229 case TV: ausel=0xc0;
ics=0x00; ocs=0x02;
break;
230 case LINE1: ausel=0x80;
ics=0x00; ocs=0x00;
break;
231 case LINE2: ausel=0x80;
ics=0x08; ocs=0x01;
break;
244 if (0 ==
card(dev).gpiomask)
247 mask =
card(dev).gpiomask;
253 static void tvaudio_setmode(
struct saa7134_dev *dev,
264 if (audio_clock_tweak > -1024 && audio_clock_tweak < 1024)
265 tweak = audio_clock_tweak;
268 dprintk(
"tvaudio_setmode: %s %s [%d.%03d/%d.%03d MHz] acpf=%d%+d\n",
278 tvaudio_setcarrier(dev,audio->
carr1,audio->
carr2);
280 switch (audio->
mode) {
316 static int tvaudio_sleep(
struct saa7134_dev *dev,
int timeout)
331 static int tvaudio_checkcarrier(
struct saa7134_dev *dev,
struct mainscan *
scan)
335 if (!(dev->
tvnorm->id & scan->std)) {
337 dprintk(
"skipping %d.%03d MHz [%4s]\n",
338 scan->carr / 1000, scan->carr % 1000, scan->name);
342 if (audio_debug > 1) {
344 dprintk(
"debug %d:",scan->carr);
345 for (i = -150; i <= 150; i += 30) {
346 tvaudio_setcarrier(dev,scan->carr+i,scan->carr+i);
352 printk(
" # %6d # ",value >> 16);
354 printk(
" %6d",value >> 16);
359 tvaudio_setcarrier(dev,scan->carr-90,scan->carr-90);
365 tvaudio_setcarrier(dev,scan->carr+90,scan->carr+90);
373 value = left > right ? left - right : right -
left;
374 dprintk(
"scanning %d.%03d MHz [%4s] => dc is %5d [%d/%d]\n",
375 scan->carr / 1000, scan->carr % 1000,
376 scan->name, value, left, right);
383 __u32 idp, nicam, nicam_status;
386 switch (audio->
mode) {
392 dprintk(
"getstereo: fm/stereo: idp=0x%x\n",idp);
393 if (0x03 == (idp & 0x03))
395 else if (0x05 == (idp & 0x05))
397 else if (0x01 == (idp & 0x01))
406 dprintk(
"getstereo: nicam=0x%x\n",nicam);
409 dprintk(
"getstereo: nicam_status=0x%x\n", nicam_status);
411 switch (nicam_status & 0x03) {
427 dprintk(
"found audio subchannels:%s%s%s%s\n",
438 static char *
name[] = {
454 switch (audio->
mode) {
462 dprintk(
"setstereo [fm] => %s\n",
474 static int tvaudio_thread(
void *
data)
478 unsigned int i,
audio, nscan;
479 int max1,max2,carrier,
rx,
mode,lastmode,default_carrier;
484 tvaudio_sleep(dev,-1);
492 dprintk(
"tvaudio thread scan start [%d]\n",dev->
thread.scan1);
501 mute_input_7134(dev);
513 if (!(dev->
tvnorm->id & mainscan[i].std))
515 if (!default_carrier)
516 default_carrier = mainscan[
i].carr;
522 dprintk(
"only one main carrier candidate - skipping scan\n");
524 carrier = default_carrier;
528 tvaudio_setmode(dev,&tvaudio[0],
NULL);
530 carr_vals[
i] = tvaudio_checkcarrier(dev, mainscan+i);
534 for (max1 = 0, max2 = 0, i = 0; i <
ARRAY_SIZE(mainscan); i++) {
535 if (max1 < carr_vals[i]) {
538 carrier = mainscan[
i].carr;
539 }
else if (max2 < carr_vals[i]) {
545 if (0 != carrier && max1 > 2000 && max1 > max2*3) {
547 dprintk(
"found %s main sound carrier @ %d.%03d MHz [%d/%d]\n",
548 dev->
tvnorm->name, carrier/1000, carrier%1000,
556 dprintk(
"audio carrier scan failed, "
557 "using %d.%03d MHz [last detected]\n",
558 carrier/1000, carrier%1000);
563 carrier = default_carrier;
564 dprintk(
"audio carrier scan failed, "
565 "using %d.%03d MHz [default]\n",
566 carrier/1000, carrier%1000);
569 tvaudio_setcarrier(dev,carrier,carrier);
577 if (tvaudio[i].carr1 != carrier)
582 tvaudio_setmode(dev,&tvaudio[i],
"trying");
585 if (-1 != tvaudio_getstereo(dev,&tvaudio[i])) {
593 tvaudio_setmode(dev,&tvaudio[audio],
"using");
603 if (tvaudio_sleep(dev,5000))
608 rx = tvaudio_getstereo(dev, &tvaudio[audio]);
613 if (lastmode != mode) {
614 tvaudio_setstereo(dev,&tvaudio[audio],mode);
628 static char *stdres[0x20] = {
629 [0x00] =
"no standard detected",
630 [0x01] =
"B/G (in progress)",
631 [0x02] =
"D/K (in progress)",
632 [0x03] =
"M (in progress)",
635 [0x05] =
"B/G NICAM",
636 [0x06] =
"D/K A2 (1)",
637 [0x07] =
"D/K A2 (2)",
638 [0x08] =
"D/K A2 (3)",
639 [0x09] =
"D/K NICAM",
647 [0x0f] =
"FM radio / IF 10.7 / 50 deemp",
648 [0x10] =
"FM radio / IF 10.7 / 75 deemp",
649 [0x11] =
"FM radio / IF sel / 50 deemp",
650 [0x12] =
"FM radio / IF sel / 75 deemp",
652 [0x13 ... 0x1e ] =
"unknown",
653 [0x1f] =
"??? [in progress]",
658 #define SAA7135_DSP_RWCLEAR_RERR 1
660 static inline int saa_dsp_reset_error_bit(
struct saa7134_dev *dev)
670 static inline int saa_dsp_wait_bit(
struct saa7134_dev *dev,
int bit)
677 saa_dsp_reset_error_bit(dev);
680 while (0 == (state & bit)) {
682 printk(
"%s: dsp access wait timeout [bit=%s]\n",
702 d2printk(
"dsp write reg 0x%x = 0x%06x\n",reg<<2,value);
726 static int mute_input_7133(
struct saa7134_dev *dev)
734 switch (dev->
input->amux) {
759 if (0 !=
card(dev).gpiomask) {
760 mask =
card(dev).gpiomask;
763 in = &
card(dev).mute;
775 static int tvaudio_thread_ddep(
void *data)
782 tvaudio_sleep(dev,-1);
789 dprintk(
"tvaudio thread scan start [%d]\n",dev->
thread.scan1);
791 if (audio_ddep >= 0x04 && audio_ddep <= 0x0e) {
793 norms = (audio_ddep << 2) | 0x01;
794 dprintk(
"ddep override: %s\n",stdres[audio_ddep]);
795 }
else if (&
card(dev).radio == dev->
input) {
797 if (dev->
tuner_type == TUNER_PHILIPS_TDA8290) {
798 norms = (0x11 << 2) | 0x01;
801 norms = (0x0f << 2) | 0x01;
818 dprintk(
"scanning:%s%s%s%s%s\n",
819 (norms & 0x04) ?
" B/G" :
"",
820 (norms & 0x08) ?
" D/K" :
"",
821 (norms & 0x10) ?
" L/L'" :
"",
822 (norms & 0x20) ?
" I" :
"",
823 (norms & 0x40) ?
" M" :
"");
834 if (tvaudio_sleep(dev,3000))
836 value =
saa_readl(0x528 >> 2) & 0xffffff;
838 dprintk(
"tvaudio thread status: 0x%x [%s%s%s]\n",
839 value, stdres[value & 0x1f],
840 (value & 0x000020) ?
",stereo" :
"",
841 (value & 0x000040) ?
",dual" :
"");
843 "%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n",
844 (value & 0x000080) ?
" A2/EIAJ pilot tone " :
"",
845 (value & 0x000100) ?
" A2/EIAJ dual " :
"",
846 (value & 0x000200) ?
" A2/EIAJ stereo " :
"",
847 (value & 0x000400) ?
" A2/EIAJ noise mute " :
"",
849 (value & 0x000800) ?
" BTSC/FM radio pilot " :
"",
850 (value & 0x001000) ?
" SAP carrier " :
"",
851 (value & 0x002000) ?
" BTSC stereo noise mute " :
"",
852 (value & 0x004000) ?
" SAP noise mute " :
"",
853 (value & 0x008000) ?
" VDSP " :
"",
855 (value & 0x010000) ?
" NICST " :
"",
856 (value & 0x020000) ?
" NICDU " :
"",
857 (value & 0x040000) ?
" NICAM muted " :
"",
858 (value & 0x080000) ?
" NICAM reserve sound " :
"",
860 (value & 0x100000) ?
" init done " :
"");
884 switch (dev->
pci->device) {
894 i2s_format = (dev->
input->amux ==
TV) ? 0x00 : 0x01;
912 if (rx & V4L2_TUNER_SUB_STEREO)
923 switch (dev->
pci->device) {
926 mute_input_7134(dev);
930 mute_input_7133(dev);
939 switch (dev->
pci->device) {
942 mute_input_7134(dev);
946 mute_input_7133(dev);
954 switch (dev->
pci->device) {
967 switch (dev->
pci->device) {
970 retval = tvaudio_getstereo(dev,dev->
tvaudio);
974 retval = getstereo_7133(dev);
984 if (
UNSET != audio_clock_override)
985 clock = audio_clock_override;
987 switch (dev->
pci->device) {
1016 switch (dev->
pci->device) {
1018 my_thread = tvaudio_thread;
1022 my_thread = tvaudio_thread_ddep;
1032 if (IS_ERR(dev->
thread.thread)) {
1063 dprintk(
"sound IF not in use, skipping scan\n");
1066 }
else if (dev->
thread.thread) {