1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * device driver for philips saa7134 based TV cards
5 * tv audio decoder (fm stereo, nicam, ...)
7 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
11 #include "saa7134-reg.h"
13 #include <linux/init.h>
14 #include <linux/list.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/kthread.h>
18 #include <linux/delay.h>
19 #include <linux/freezer.h>
20 #include <asm/div64.h>
22 /* ------------------------------------------------------------------ */
24 static unsigned int audio_debug
;
25 module_param(audio_debug
, int, 0644);
26 MODULE_PARM_DESC(audio_debug
,"enable debug messages [tv audio]");
28 static unsigned int audio_ddep
;
29 module_param(audio_ddep
, int, 0644);
30 MODULE_PARM_DESC(audio_ddep
,"audio ddep overwrite");
32 static int audio_clock_override
= UNSET
;
33 module_param(audio_clock_override
, int, 0644);
35 static int audio_clock_tweak
;
36 module_param(audio_clock_tweak
, int, 0644);
37 MODULE_PARM_DESC(audio_clock_tweak
, "Audio clock tick fine tuning for cards with audio crystal that's slightly off (range [-1024 .. 1024])");
39 #define audio_dbg(level, fmt, arg...) do { \
40 if (audio_debug >= level) \
41 printk(KERN_DEBUG pr_fmt("audio: " fmt), ## arg); \
45 #define SCAN_INITIAL_DELAY 1000
46 #define SCAN_SAMPLE_DELAY 200
47 #define SCAN_SUBCARRIER_DELAY 2000
49 /* ------------------------------------------------------------------ */
52 static struct mainscan
{
63 .std
= V4L2_STD_B
| V4L2_STD_GH
,
67 .std
= V4L2_STD_PAL_I
,
71 .std
= V4L2_STD_DK
| V4L2_STD_SECAM_L
| V4L2_STD_SECAM_LC
,
76 static struct saa7134_tvaudio tvaudio
[] = {
78 .name
= "PAL-B/G FM-stereo",
79 .std
= V4L2_STD_PAL_BG
,
80 .mode
= TVAUDIO_FM_BG_STEREO
,
84 .name
= "PAL-D/K1 FM-stereo",
85 .std
= V4L2_STD_PAL_DK
,
88 .mode
= TVAUDIO_FM_BG_STEREO
,
90 .name
= "PAL-D/K2 FM-stereo",
91 .std
= V4L2_STD_PAL_DK
,
94 .mode
= TVAUDIO_FM_BG_STEREO
,
96 .name
= "PAL-D/K3 FM-stereo",
97 .std
= V4L2_STD_PAL_DK
,
100 .mode
= TVAUDIO_FM_BG_STEREO
,
102 .name
= "PAL-B/G NICAM",
103 .std
= V4L2_STD_PAL_BG
,
106 .mode
= TVAUDIO_NICAM_FM
,
108 .name
= "PAL-I NICAM",
109 .std
= V4L2_STD_PAL_I
,
112 .mode
= TVAUDIO_NICAM_FM
,
114 .name
= "PAL-D/K NICAM",
115 .std
= V4L2_STD_PAL_DK
,
118 .mode
= TVAUDIO_NICAM_FM
,
120 .name
= "SECAM-L NICAM",
121 .std
= V4L2_STD_SECAM_L
,
124 .mode
= TVAUDIO_NICAM_AM
,
126 .name
= "SECAM-D/K NICAM",
127 .std
= V4L2_STD_SECAM_DK
,
130 .mode
= TVAUDIO_NICAM_FM
,
132 .name
= "NTSC-A2 FM-stereo",
133 .std
= V4L2_STD_NTSC
,
136 .mode
= TVAUDIO_FM_K_STEREO
,
139 .std
= V4L2_STD_NTSC
,
142 .mode
= TVAUDIO_FM_MONO
,
145 #define TVAUDIO ARRAY_SIZE(tvaudio)
147 /* ------------------------------------------------------------------ */
149 static u32
tvaudio_carr2reg(u32 carrier
)
158 static void tvaudio_setcarrier(struct saa7134_dev
*dev
,
159 int primary
, int secondary
)
163 saa_writel(SAA7134_CARRIER1_FREQ0
>> 2, tvaudio_carr2reg(primary
));
164 saa_writel(SAA7134_CARRIER2_FREQ0
>> 2, tvaudio_carr2reg(secondary
));
167 #define SAA7134_MUTE_MASK 0xbb
168 #define SAA7134_MUTE_ANALOG 0x04
169 #define SAA7134_MUTE_I2S 0x40
171 static void mute_input_7134(struct saa7134_dev
*dev
)
174 struct saa7134_input
*in
;
175 int ausel
=0, ics
=0, ocs
=0;
178 /* look what is to do ... */
180 mute
= (dev
->ctl_mute
||
181 (dev
->automute
&& (&card(dev
).radio
) != in
));
182 if (card(dev
).mute
.type
) {
184 * 7130 - we'll mute using some unconnected audio input
185 * 7134 - we'll probably should switch external mux with gpio
188 in
= &card(dev
).mute
;
191 if (dev
->hw_mute
== mute
&&
192 dev
->hw_input
== in
&& !dev
->insuspend
) {
193 audio_dbg(1, "mute/input: nothing to do [mute=%d,input=%s]\n",
194 mute
, saa7134_input_name
[in
->type
]);
198 audio_dbg(1, "ctl_mute=%d automute=%d input=%s => mute=%d input=%s\n",
199 dev
->ctl_mute
, dev
->automute
,
200 saa7134_input_name
[dev
->input
->type
], mute
,
201 saa7134_input_name
[in
->type
]);
205 if (PCI_DEVICE_ID_PHILIPS_SAA7134
== dev
->pci
->device
)
207 saa_writeb(SAA7134_AUDIO_MUTE_CTRL
, mute
?
209 SAA7134_MUTE_ANALOG
|
213 /* switch internal audio mux */
215 case TV
: ausel
=0xc0; ics
=0x00; ocs
=0x02; break;
216 case LINE1
: ausel
=0x80; ics
=0x00; ocs
=0x00; break;
217 case LINE2
: ausel
=0x80; ics
=0x08; ocs
=0x01; break;
218 case LINE2_LEFT
: ausel
=0x80; ics
=0x08; ocs
=0x05; break;
220 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL
, 0xc0, ausel
);
221 saa_andorb(SAA7134_ANALOG_IO_SELECT
, 0x08, ics
);
222 saa_andorb(SAA7134_ANALOG_IO_SELECT
, 0x07, ocs
);
223 // for oss, we need to change the clock configuration
225 saa_andorb(SAA7134_SIF_SAMPLE_FREQ
, 0x03, 0x00);
227 saa_andorb(SAA7134_SIF_SAMPLE_FREQ
, 0x03, 0x01);
229 /* switch gpio-connected external audio mux */
230 if (0 == card(dev
).gpiomask
)
233 mask
= card(dev
).gpiomask
;
234 saa_andorl(SAA7134_GPIO_GPMODE0
>> 2, mask
, mask
);
235 saa_andorl(SAA7134_GPIO_GPSTATUS0
>> 2, mask
, in
->gpio
);
236 saa7134_track_gpio(dev
, saa7134_input_name
[in
->type
]);
239 static void tvaudio_setmode(struct saa7134_dev
*dev
,
240 struct saa7134_tvaudio
*audio
,
245 if (dev
->tvnorm
->id
== V4L2_STD_NTSC
) {
250 if (audio_clock_tweak
> -1024 && audio_clock_tweak
< 1024)
251 tweak
= audio_clock_tweak
;
254 audio_dbg(1, "tvaudio_setmode: %s %s [%d.%03d/%d.%03d MHz] acpf=%d%+d\n",
256 audio
->carr1
/ 1000, audio
->carr1
% 1000,
257 audio
->carr2
/ 1000, audio
->carr2
% 1000,
261 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD0
, (acpf
& 0x0000ff) >> 0);
262 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD1
, (acpf
& 0x00ff00) >> 8);
263 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD2
, (acpf
& 0x030000) >> 16);
264 tvaudio_setcarrier(dev
,audio
->carr1
,audio
->carr2
);
266 switch (audio
->mode
) {
267 case TVAUDIO_FM_MONO
:
268 case TVAUDIO_FM_BG_STEREO
:
269 saa_writeb(SAA7134_DEMODULATOR
, 0x00);
270 saa_writeb(SAA7134_DCXO_IDENT_CTRL
, 0x00);
271 saa_writeb(SAA7134_FM_DEEMPHASIS
, 0x22);
272 saa_writeb(SAA7134_FM_DEMATRIX
, 0x80);
273 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT
, 0xa0);
275 case TVAUDIO_FM_K_STEREO
:
276 saa_writeb(SAA7134_DEMODULATOR
, 0x00);
277 saa_writeb(SAA7134_DCXO_IDENT_CTRL
, 0x01);
278 saa_writeb(SAA7134_FM_DEEMPHASIS
, 0x22);
279 saa_writeb(SAA7134_FM_DEMATRIX
, 0x80);
280 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT
, 0xa0);
282 case TVAUDIO_NICAM_FM
:
283 saa_writeb(SAA7134_DEMODULATOR
, 0x10);
284 saa_writeb(SAA7134_DCXO_IDENT_CTRL
, 0x00);
285 saa_writeb(SAA7134_FM_DEEMPHASIS
, 0x44);
286 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT
, 0xa1);
287 saa_writeb(SAA7134_NICAM_CONFIG
, 0x00);
289 case TVAUDIO_NICAM_AM
:
290 saa_writeb(SAA7134_DEMODULATOR
, 0x12);
291 saa_writeb(SAA7134_DCXO_IDENT_CTRL
, 0x00);
292 saa_writeb(SAA7134_FM_DEEMPHASIS
, 0x44);
293 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT
, 0xa1);
294 saa_writeb(SAA7134_NICAM_CONFIG
, 0x00);
296 case TVAUDIO_FM_SAT_STEREO
:
297 /* not implemented (yet) */
302 static int tvaudio_sleep(struct saa7134_dev
*dev
, int timeout
)
304 if (dev
->thread
.scan1
== dev
->thread
.scan2
&&
305 !kthread_should_stop()) {
307 set_current_state(TASK_INTERRUPTIBLE
);
310 schedule_timeout_interruptible
311 (msecs_to_jiffies(timeout
));
314 return dev
->thread
.scan1
!= dev
->thread
.scan2
;
317 static int tvaudio_checkcarrier(struct saa7134_dev
*dev
, struct mainscan
*scan
)
319 __s32 left
,right
,value
;
321 if (!(dev
->tvnorm
->id
& scan
->std
)) {
322 audio_dbg(1, "skipping %d.%03d MHz [%4s]\n",
323 scan
->carr
/ 1000, scan
->carr
% 1000, scan
->name
);
327 if (audio_debug
> 1) {
329 audio_dbg(1, "debug %d:", scan
->carr
);
330 for (i
= -150; i
<= 150; i
+= 30) {
331 tvaudio_setcarrier(dev
,scan
->carr
+i
,scan
->carr
+i
);
332 saa_readl(SAA7134_LEVEL_READOUT1
>> 2);
333 if (tvaudio_sleep(dev
,SCAN_SAMPLE_DELAY
))
335 value
= saa_readl(SAA7134_LEVEL_READOUT1
>> 2);
337 pr_cont(" # %6d # ", value
>> 16);
339 pr_cont(" %6d", value
>> 16);
344 tvaudio_setcarrier(dev
,scan
->carr
-90,scan
->carr
-90);
345 saa_readl(SAA7134_LEVEL_READOUT1
>> 2);
346 if (tvaudio_sleep(dev
,SCAN_SAMPLE_DELAY
))
348 left
= saa_readl(SAA7134_LEVEL_READOUT1
>> 2);
350 tvaudio_setcarrier(dev
,scan
->carr
+90,scan
->carr
+90);
351 saa_readl(SAA7134_LEVEL_READOUT1
>> 2);
352 if (tvaudio_sleep(dev
,SCAN_SAMPLE_DELAY
))
354 right
= saa_readl(SAA7134_LEVEL_READOUT1
>> 2);
358 value
= left
> right
? left
- right
: right
- left
;
359 audio_dbg(1, "scanning %d.%03d MHz [%4s] => dc is %5d [%d/%d]\n",
360 scan
->carr
/ 1000, scan
->carr
% 1000,
361 scan
->name
, value
, left
, right
);
366 static int tvaudio_getstereo(struct saa7134_dev
*dev
, struct saa7134_tvaudio
*audio
)
368 __u32 idp
, nicam
, nicam_status
;
371 switch (audio
->mode
) {
372 case TVAUDIO_FM_MONO
:
373 return V4L2_TUNER_SUB_MONO
;
374 case TVAUDIO_FM_K_STEREO
:
375 case TVAUDIO_FM_BG_STEREO
:
376 idp
= (saa_readb(SAA7134_IDENT_SIF
) & 0xe0) >> 5;
377 audio_dbg(1, "getstereo: fm/stereo: idp=0x%x\n", idp
);
378 if (0x03 == (idp
& 0x03))
379 retval
= V4L2_TUNER_SUB_LANG1
| V4L2_TUNER_SUB_LANG2
;
380 else if (0x05 == (idp
& 0x05))
381 retval
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_STEREO
;
382 else if (0x01 == (idp
& 0x01))
383 retval
= V4L2_TUNER_SUB_MONO
;
385 case TVAUDIO_FM_SAT_STEREO
:
386 /* not implemented (yet) */
388 case TVAUDIO_NICAM_FM
:
389 case TVAUDIO_NICAM_AM
:
390 nicam
= saa_readb(SAA7134_AUDIO_STATUS
);
391 audio_dbg(1, "getstereo: nicam=0x%x\n", nicam
);
393 nicam_status
= saa_readb(SAA7134_NICAM_STATUS
);
394 audio_dbg(1, "getstereo: nicam_status=0x%x\n",
397 switch (nicam_status
& 0x03) {
399 retval
= V4L2_TUNER_SUB_LANG1
| V4L2_TUNER_SUB_LANG2
;
402 retval
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_STEREO
;
405 retval
= V4L2_TUNER_SUB_MONO
;
408 /* No nicam detected */
413 audio_dbg(1, "found audio subchannels:%s%s%s%s\n",
414 (retval
& V4L2_TUNER_SUB_MONO
) ? " mono" : "",
415 (retval
& V4L2_TUNER_SUB_STEREO
) ? " stereo" : "",
416 (retval
& V4L2_TUNER_SUB_LANG1
) ? " lang1" : "",
417 (retval
& V4L2_TUNER_SUB_LANG2
) ? " lang2" : "");
421 static int tvaudio_setstereo(struct saa7134_dev
*dev
, struct saa7134_tvaudio
*audio
,
424 static char *name
[] = {
425 [ V4L2_TUNER_MODE_MONO
] = "mono",
426 [ V4L2_TUNER_MODE_STEREO
] = "stereo",
427 [ V4L2_TUNER_MODE_LANG1
] = "lang1",
428 [ V4L2_TUNER_MODE_LANG2
] = "lang2",
429 [ V4L2_TUNER_MODE_LANG1_LANG2
] = "lang1+lang2",
432 [ V4L2_TUNER_MODE_MONO
] = 0x00, /* ch1 */
433 [ V4L2_TUNER_MODE_STEREO
] = 0x80, /* auto */
434 [ V4L2_TUNER_MODE_LANG1
] = 0x00, /* ch1 */
435 [ V4L2_TUNER_MODE_LANG2
] = 0x01, /* ch2 */
436 [ V4L2_TUNER_MODE_LANG1_LANG2
] = 0x80, /* auto */
440 switch (audio
->mode
) {
441 case TVAUDIO_FM_MONO
:
442 /* nothing to do ... */
444 case TVAUDIO_FM_K_STEREO
:
445 case TVAUDIO_FM_BG_STEREO
:
446 case TVAUDIO_NICAM_AM
:
447 case TVAUDIO_NICAM_FM
:
448 audio_dbg(1, "setstereo [fm] => %s\n",
449 name
[mode
% ARRAY_SIZE(name
)]);
450 reg
= fm
[ mode
% ARRAY_SIZE(fm
) ];
451 saa_writeb(SAA7134_FM_DEMATRIX
, reg
);
453 case TVAUDIO_FM_SAT_STEREO
:
454 /* Not implemented */
460 static int tvaudio_thread(void *data
)
462 struct saa7134_dev
*dev
= data
;
463 int carr_vals
[ARRAY_SIZE(mainscan
)];
464 unsigned int i
, audio
, nscan
;
465 int max1
,max2
,carrier
,rx
,mode
,lastmode
,default_carrier
;
470 tvaudio_sleep(dev
,-1);
471 if (kthread_should_stop())
477 dev
->thread
.scan1
= dev
->thread
.scan2
;
478 audio_dbg(1, "tvaudio thread scan start [%d]\n",
482 saa_writeb(SAA7134_MONITOR_SELECT
, 0xa0);
483 saa_writeb(SAA7134_FM_DEMATRIX
, 0x80);
485 if (dev
->ctl_automute
)
488 mute_input_7134(dev
);
490 /* give the tuner some time */
491 if (tvaudio_sleep(dev
,SCAN_INITIAL_DELAY
))
499 for (i
= 0; i
< ARRAY_SIZE(mainscan
); i
++) {
500 if (!(dev
->tvnorm
->id
& mainscan
[i
].std
))
502 if (!default_carrier
)
503 default_carrier
= mainscan
[i
].carr
;
508 /* only one candidate -- skip scan ;) */
509 audio_dbg(1, "only one main carrier candidate - skipping scan\n");
511 carrier
= default_carrier
;
513 /* scan for the main carrier */
514 saa_writeb(SAA7134_MONITOR_SELECT
,0x00);
515 tvaudio_setmode(dev
,&tvaudio
[0],NULL
);
516 for (i
= 0; i
< ARRAY_SIZE(mainscan
); i
++) {
517 carr_vals
[i
] = tvaudio_checkcarrier(dev
, mainscan
+i
);
518 if (dev
->thread
.scan1
!= dev
->thread
.scan2
)
521 for (max1
= 0, max2
= 0, i
= 0; i
< ARRAY_SIZE(mainscan
); i
++) {
522 if (max1
< carr_vals
[i
]) {
525 carrier
= mainscan
[i
].carr
;
526 } else if (max2
< carr_vals
[i
]) {
532 if (0 != carrier
&& max1
> 2000 && max1
> max2
*3) {
533 /* found good carrier */
534 audio_dbg(1, "found %s main sound carrier @ %d.%03d MHz [%d/%d]\n",
535 dev
->tvnorm
->name
, carrier
/1000, carrier
%1000,
537 dev
->last_carrier
= carrier
;
540 } else if (0 != dev
->last_carrier
) {
541 /* no carrier -- try last detected one as fallback */
542 carrier
= dev
->last_carrier
;
543 audio_dbg(1, "audio carrier scan failed, using %d.%03d MHz [last detected]\n",
544 carrier
/1000, carrier
%1000);
548 /* no carrier + no fallback -- use default */
549 carrier
= default_carrier
;
550 audio_dbg(1, "audio carrier scan failed, using %d.%03d MHz [default]\n",
551 carrier
/1000, carrier
%1000);
554 tvaudio_setcarrier(dev
,carrier
,carrier
);
555 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT
, 0x30, 0x00);
556 saa7134_tvaudio_setmute(dev
);
557 /* find the exact tv audio norm */
558 for (audio
= UNSET
, i
= 0; i
< TVAUDIO
; i
++) {
559 if (dev
->tvnorm
->id
!= UNSET
&&
560 !(dev
->tvnorm
->id
& tvaudio
[i
].std
))
562 if (tvaudio
[i
].carr1
!= carrier
)
564 /* Note: at least the primary carrier is right here */
567 tvaudio_setmode(dev
,&tvaudio
[i
],"trying");
568 if (tvaudio_sleep(dev
,SCAN_SUBCARRIER_DELAY
))
570 if (-1 != tvaudio_getstereo(dev
,&tvaudio
[i
])) {
575 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT
, 0x30, 0x30);
578 tvaudio_setmode(dev
,&tvaudio
[audio
],"using");
580 tvaudio_setstereo(dev
,&tvaudio
[audio
],V4L2_TUNER_MODE_MONO
);
581 dev
->tvaudio
= &tvaudio
[audio
];
588 if (tvaudio_sleep(dev
,5000))
590 if (kthread_should_stop())
592 if (UNSET
== dev
->thread
.mode
) {
593 rx
= tvaudio_getstereo(dev
, &tvaudio
[audio
]);
594 mode
= saa7134_tvaudio_rx2mode(rx
);
596 mode
= dev
->thread
.mode
;
598 if (lastmode
!= mode
) {
599 tvaudio_setstereo(dev
,&tvaudio
[audio
],mode
);
606 dev
->thread
.stopped
= 1;
610 /* ------------------------------------------------------------------ */
611 /* saa7133 / saa7135 code */
613 static char *stdres
[0x20] = {
614 [0x00] = "no standard detected",
615 [0x01] = "B/G (in progress)",
616 [0x02] = "D/K (in progress)",
617 [0x03] = "M (in progress)",
620 [0x05] = "B/G NICAM",
621 [0x06] = "D/K A2 (1)",
622 [0x07] = "D/K A2 (2)",
623 [0x08] = "D/K A2 (3)",
624 [0x09] = "D/K NICAM",
632 [0x0f] = "FM radio / IF 10.7 / 50 deemp",
633 [0x10] = "FM radio / IF 10.7 / 75 deemp",
634 [0x11] = "FM radio / IF sel / 50 deemp",
635 [0x12] = "FM radio / IF sel / 75 deemp",
637 [0x13 ... 0x1e ] = "unknown",
638 [0x1f] = "??? [in progress]",
643 #define SAA7135_DSP_RWCLEAR_RERR 1
645 static inline int saa_dsp_reset_error_bit(struct saa7134_dev
*dev
)
647 int state
= saa_readb(SAA7135_DSP_RWSTATE
);
648 if (unlikely(state
& SAA7135_DSP_RWSTATE_ERR
)) {
649 audio_dbg(2, "%s: resetting error bit\n", dev
->name
);
650 saa_writeb(SAA7135_DSP_RWCLEAR
, SAA7135_DSP_RWCLEAR_RERR
);
655 static inline int saa_dsp_wait_bit(struct saa7134_dev
*dev
, int bit
)
657 int state
, count
= DSP_RETRY
;
659 state
= saa_readb(SAA7135_DSP_RWSTATE
);
660 if (unlikely(state
& SAA7135_DSP_RWSTATE_ERR
)) {
661 pr_warn("%s: dsp access error\n", dev
->name
);
662 saa_dsp_reset_error_bit(dev
);
665 while (0 == (state
& bit
)) {
666 if (unlikely(0 == count
)) {
667 pr_err("dsp access wait timeout [bit=%s]\n",
668 (bit
& SAA7135_DSP_RWSTATE_WRR
) ? "WRR" :
669 (bit
& SAA7135_DSP_RWSTATE_RDB
) ? "RDB" :
670 (bit
& SAA7135_DSP_RWSTATE_IDA
) ? "IDA" :
675 state
= saa_readb(SAA7135_DSP_RWSTATE
);
682 int saa_dsp_writel(struct saa7134_dev
*dev
, int reg
, u32 value
)
686 audio_dbg(2, "dsp write reg 0x%x = 0x%06x\n", reg
<< 2, value
);
687 err
= saa_dsp_wait_bit(dev
,SAA7135_DSP_RWSTATE_WRR
);
690 saa_writel(reg
,value
);
691 err
= saa_dsp_wait_bit(dev
,SAA7135_DSP_RWSTATE_WRR
);
697 static int getstereo_7133(struct saa7134_dev
*dev
)
699 int retval
= V4L2_TUNER_SUB_MONO
;
702 value
= saa_readl(0x528 >> 2);
704 retval
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_STEREO
;
706 retval
= V4L2_TUNER_SUB_LANG1
| V4L2_TUNER_SUB_LANG2
;
710 static int mute_input_7133(struct saa7134_dev
*dev
)
715 struct saa7134_input
*in
;
718 switch (dev
->input
->amux
) {
731 saa_dsp_writel(dev
, 0x464 >> 2, xbarin
);
737 saa_dsp_writel(dev
, 0x46c >> 2, xbarout
);
739 saa_writel(0x594 >> 2, reg
);
742 /* switch gpio-connected external audio mux */
743 if (0 != card(dev
).gpiomask
) {
744 mask
= card(dev
).gpiomask
;
746 if (card(dev
).mute
.type
&& dev
->ctl_mute
)
747 in
= &card(dev
).mute
;
751 saa_andorl(SAA7134_GPIO_GPMODE0
>> 2, mask
, mask
);
752 saa_andorl(SAA7134_GPIO_GPSTATUS0
>> 2, mask
, in
->gpio
);
753 saa7134_track_gpio(dev
, saa7134_input_name
[in
->type
]);
759 static int tvaudio_thread_ddep(void *data
)
761 struct saa7134_dev
*dev
= data
;
766 tvaudio_sleep(dev
,-1);
767 if (kthread_should_stop())
772 dev
->thread
.scan1
= dev
->thread
.scan2
;
773 audio_dbg(1, "tvaudio thread scan start [%d]\n",
776 if (audio_ddep
>= 0x04 && audio_ddep
<= 0x0e) {
777 /* insmod option override */
778 norms
= (audio_ddep
<< 2) | 0x01;
779 audio_dbg(1, "ddep override: %s\n",
781 } else if (&card(dev
).radio
== dev
->input
) {
782 audio_dbg(1, "FM Radio\n");
783 if (dev
->tuner_type
== TUNER_PHILIPS_TDA8290
) {
784 norms
= (0x11 << 2) | 0x01;
785 /* set IF frequency to 5.5 MHz */
786 saa_dsp_writel(dev
, 0x42c >> 2, 0x729555);
788 norms
= (0x0f << 2) | 0x01;
791 /* (let chip) scan for sound carrier */
793 if (dev
->tvnorm
->id
& (V4L2_STD_B
| V4L2_STD_GH
))
795 if (dev
->tvnorm
->id
& V4L2_STD_PAL_I
)
797 if (dev
->tvnorm
->id
& V4L2_STD_DK
)
799 if (dev
->tvnorm
->id
& V4L2_STD_MN
)
801 if (dev
->tvnorm
->id
& (V4L2_STD_SECAM_L
| V4L2_STD_SECAM_LC
))
804 norms
= 0x7c; /* all */
805 audio_dbg(1, "scanning:%s%s%s%s%s\n",
806 (norms
& 0x04) ? " B/G" : "",
807 (norms
& 0x08) ? " D/K" : "",
808 (norms
& 0x10) ? " L/L'" : "",
809 (norms
& 0x20) ? " I" : "",
810 (norms
& 0x40) ? " M" : "");
813 /* kick automatic standard detection */
814 saa_dsp_writel(dev
, 0x454 >> 2, 0);
815 saa_dsp_writel(dev
, 0x454 >> 2, norms
| 0x80);
817 /* setup crossbars */
818 saa_dsp_writel(dev
, 0x464 >> 2, 0x000000);
819 saa_dsp_writel(dev
, 0x470 >> 2, 0x101010);
821 if (tvaudio_sleep(dev
,3000))
823 value
= saa_readl(0x528 >> 2) & 0xffffff;
825 audio_dbg(1, "tvaudio thread status: 0x%x [%s%s%s]\n",
826 value
, stdres
[value
& 0x1f],
827 (value
& 0x000020) ? ",stereo" : "",
828 (value
& 0x000040) ? ",dual" : "");
829 audio_dbg(1, "detailed status: %s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n",
830 (value
& 0x000080) ? " A2/EIAJ pilot tone " : "",
831 (value
& 0x000100) ? " A2/EIAJ dual " : "",
832 (value
& 0x000200) ? " A2/EIAJ stereo " : "",
833 (value
& 0x000400) ? " A2/EIAJ noise mute " : "",
835 (value
& 0x000800) ? " BTSC/FM radio pilot " : "",
836 (value
& 0x001000) ? " SAP carrier " : "",
837 (value
& 0x002000) ? " BTSC stereo noise mute " : "",
838 (value
& 0x004000) ? " SAP noise mute " : "",
839 (value
& 0x008000) ? " VDSP " : "",
841 (value
& 0x010000) ? " NICST " : "",
842 (value
& 0x020000) ? " NICDU " : "",
843 (value
& 0x040000) ? " NICAM muted " : "",
844 (value
& 0x080000) ? " NICAM reserve sound " : "",
846 (value
& 0x100000) ? " init done " : "");
850 dev
->thread
.stopped
= 1;
854 /* ------------------------------------------------------------------ */
855 /* common stuff + external entry points */
857 void saa7134_enable_i2s(struct saa7134_dev
*dev
)
861 if (!card_is_empress(dev
))
864 if (dev
->pci
->device
== PCI_DEVICE_ID_PHILIPS_SAA7130
)
867 /* configure GPIO for out */
868 saa_andorl(SAA7134_GPIO_GPMODE0
>> 2, 0x0E000000, 0x00000000);
870 switch (dev
->pci
->device
) {
871 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
872 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
873 /* Set I2S format (SONY) */
874 saa_writeb(SAA7133_I2S_AUDIO_CONTROL
, 0x00);
876 saa_writeb(SAA7134_I2S_AUDIO_OUTPUT
, 0x11);
879 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
880 i2s_format
= (dev
->input
->amux
== TV
) ? 0x00 : 0x01;
882 /* enable I2S audio output for the mpeg encoder */
883 saa_writeb(SAA7134_I2S_OUTPUT_SELECT
, 0x80);
884 saa_writeb(SAA7134_I2S_OUTPUT_FORMAT
, i2s_format
);
885 saa_writeb(SAA7134_I2S_OUTPUT_LEVEL
, 0x0F);
886 saa_writeb(SAA7134_I2S_AUDIO_OUTPUT
, 0x01);
893 int saa7134_tvaudio_rx2mode(u32 rx
)
897 mode
= V4L2_TUNER_MODE_MONO
;
898 if (rx
& V4L2_TUNER_SUB_STEREO
)
899 mode
= V4L2_TUNER_MODE_STEREO
;
900 else if (rx
& V4L2_TUNER_SUB_LANG1
)
901 mode
= V4L2_TUNER_MODE_LANG1
;
902 else if (rx
& V4L2_TUNER_SUB_LANG2
)
903 mode
= V4L2_TUNER_MODE_LANG2
;
907 void saa7134_tvaudio_setmute(struct saa7134_dev
*dev
)
909 switch (dev
->pci
->device
) {
910 case PCI_DEVICE_ID_PHILIPS_SAA7130
:
911 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
912 mute_input_7134(dev
);
914 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
915 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
916 mute_input_7133(dev
);
921 void saa7134_tvaudio_setinput(struct saa7134_dev
*dev
,
922 struct saa7134_input
*in
)
925 switch (dev
->pci
->device
) {
926 case PCI_DEVICE_ID_PHILIPS_SAA7130
:
927 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
928 mute_input_7134(dev
);
930 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
931 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
932 mute_input_7133(dev
);
935 saa7134_enable_i2s(dev
);
938 void saa7134_tvaudio_setvolume(struct saa7134_dev
*dev
, int level
)
940 switch (dev
->pci
->device
) {
941 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
942 saa_writeb(SAA7134_CHANNEL1_LEVEL
, level
& 0x1f);
943 saa_writeb(SAA7134_CHANNEL2_LEVEL
, level
& 0x1f);
944 saa_writeb(SAA7134_NICAM_LEVEL_ADJUST
, level
& 0x1f);
949 int saa7134_tvaudio_getstereo(struct saa7134_dev
*dev
)
951 int retval
= V4L2_TUNER_SUB_MONO
;
953 switch (dev
->pci
->device
) {
954 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
956 retval
= tvaudio_getstereo(dev
,dev
->tvaudio
);
958 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
959 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
960 retval
= getstereo_7133(dev
);
966 void saa7134_tvaudio_init(struct saa7134_dev
*dev
)
968 int clock
= saa7134_boards
[dev
->board
].audio_clock
;
970 if (UNSET
!= audio_clock_override
)
971 clock
= audio_clock_override
;
973 switch (dev
->pci
->device
) {
974 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
975 /* init all audio registers */
976 saa_writeb(SAA7134_AUDIO_PLL_CTRL
, 0x00);
982 saa_writeb(SAA7134_AUDIO_CLOCK0
, clock
& 0xff);
983 saa_writeb(SAA7134_AUDIO_CLOCK1
, (clock
>> 8) & 0xff);
984 saa_writeb(SAA7134_AUDIO_CLOCK2
, (clock
>> 16) & 0xff);
985 /* frame locked audio is mandatory for NICAM */
986 saa_writeb(SAA7134_AUDIO_PLL_CTRL
, 0x01);
987 saa_writeb(SAA7134_NICAM_ERROR_LOW
, 0x14);
988 saa_writeb(SAA7134_NICAM_ERROR_HIGH
, 0x50);
990 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
991 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
992 saa_writel(0x598 >> 2, clock
);
993 saa_dsp_writel(dev
, 0x474 >> 2, 0x00);
994 saa_dsp_writel(dev
, 0x450 >> 2, 0x00);
998 int saa7134_tvaudio_init2(struct saa7134_dev
*dev
)
1000 int (*my_thread
)(void *data
) = NULL
;
1002 switch (dev
->pci
->device
) {
1003 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
1004 my_thread
= tvaudio_thread
;
1006 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
1007 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
1008 my_thread
= tvaudio_thread_ddep
;
1012 dev
->thread
.thread
= NULL
;
1013 dev
->thread
.scan1
= dev
->thread
.scan2
= 0;
1015 saa7134_tvaudio_init(dev
);
1016 /* start tvaudio thread */
1017 dev
->thread
.thread
= kthread_run(my_thread
, dev
, "%s", dev
->name
);
1018 if (IS_ERR(dev
->thread
.thread
)) {
1019 pr_warn("%s: kernel_thread() failed\n",
1021 /* XXX: missing error handling here */
1025 saa7134_enable_i2s(dev
);
1029 int saa7134_tvaudio_close(struct saa7134_dev
*dev
)
1032 /* anything else to undo? */
1036 int saa7134_tvaudio_fini(struct saa7134_dev
*dev
)
1038 /* shutdown tvaudio thread */
1039 if (dev
->thread
.thread
&& !dev
->thread
.stopped
)
1040 kthread_stop(dev
->thread
.thread
);
1042 saa_andorb(SAA7134_ANALOG_IO_SELECT
, 0x07, 0x00); /* LINE1 */
1046 int saa7134_tvaudio_do_scan(struct saa7134_dev
*dev
)
1048 if (dev
->input
->amux
!= TV
) {
1049 audio_dbg(1, "sound IF not in use, skipping scan\n");
1051 saa7134_tvaudio_setmute(dev
);
1052 } else if (dev
->thread
.thread
) {
1053 dev
->thread
.mode
= UNSET
;
1054 dev
->thread
.scan2
++;
1056 if (!dev
->insuspend
&& !dev
->thread
.stopped
)
1057 wake_up_process(dev
->thread
.thread
);
1060 saa7134_tvaudio_setmute(dev
);
1065 EXPORT_SYMBOL(saa_dsp_writel
);
1066 EXPORT_SYMBOL(saa7134_tvaudio_setmute
);