3 * device driver for philips saa7134 based TV cards
4 * tv audio decoder (fm stereo, nicam, ...)
6 * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "saa7134-reg.h"
26 #include <linux/init.h>
27 #include <linux/list.h>
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/kthread.h>
31 #include <linux/delay.h>
32 #include <linux/freezer.h>
33 #include <asm/div64.h>
35 /* ------------------------------------------------------------------ */
37 static unsigned int audio_debug
;
38 module_param(audio_debug
, int, 0644);
39 MODULE_PARM_DESC(audio_debug
,"enable debug messages [tv audio]");
41 static unsigned int audio_ddep
;
42 module_param(audio_ddep
, int, 0644);
43 MODULE_PARM_DESC(audio_ddep
,"audio ddep overwrite");
45 static int audio_clock_override
= UNSET
;
46 module_param(audio_clock_override
, int, 0644);
48 static int audio_clock_tweak
;
49 module_param(audio_clock_tweak
, int, 0644);
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 audio_dbg(level, fmt, arg...) do { \
53 if (audio_debug >= level) \
54 printk(KERN_DEBUG pr_fmt("audio: " fmt), ## arg); \
58 #define SCAN_INITIAL_DELAY 1000
59 #define SCAN_SAMPLE_DELAY 200
60 #define SCAN_SUBCARRIER_DELAY 2000
62 /* ------------------------------------------------------------------ */
65 static struct mainscan
{
76 .std
= V4L2_STD_B
| V4L2_STD_GH
,
80 .std
= V4L2_STD_PAL_I
,
84 .std
= V4L2_STD_DK
| V4L2_STD_SECAM_L
| V4L2_STD_SECAM_LC
,
89 static struct saa7134_tvaudio tvaudio
[] = {
91 .name
= "PAL-B/G FM-stereo",
92 .std
= V4L2_STD_PAL_BG
,
93 .mode
= TVAUDIO_FM_BG_STEREO
,
97 .name
= "PAL-D/K1 FM-stereo",
98 .std
= V4L2_STD_PAL_DK
,
101 .mode
= TVAUDIO_FM_BG_STEREO
,
103 .name
= "PAL-D/K2 FM-stereo",
104 .std
= V4L2_STD_PAL_DK
,
107 .mode
= TVAUDIO_FM_BG_STEREO
,
109 .name
= "PAL-D/K3 FM-stereo",
110 .std
= V4L2_STD_PAL_DK
,
113 .mode
= TVAUDIO_FM_BG_STEREO
,
115 .name
= "PAL-B/G NICAM",
116 .std
= V4L2_STD_PAL_BG
,
119 .mode
= TVAUDIO_NICAM_FM
,
121 .name
= "PAL-I NICAM",
122 .std
= V4L2_STD_PAL_I
,
125 .mode
= TVAUDIO_NICAM_FM
,
127 .name
= "PAL-D/K NICAM",
128 .std
= V4L2_STD_PAL_DK
,
131 .mode
= TVAUDIO_NICAM_FM
,
133 .name
= "SECAM-L NICAM",
134 .std
= V4L2_STD_SECAM_L
,
137 .mode
= TVAUDIO_NICAM_AM
,
139 .name
= "SECAM-D/K NICAM",
140 .std
= V4L2_STD_SECAM_DK
,
143 .mode
= TVAUDIO_NICAM_FM
,
145 .name
= "NTSC-A2 FM-stereo",
146 .std
= V4L2_STD_NTSC
,
149 .mode
= TVAUDIO_FM_K_STEREO
,
152 .std
= V4L2_STD_NTSC
,
155 .mode
= TVAUDIO_FM_MONO
,
158 #define TVAUDIO ARRAY_SIZE(tvaudio)
160 /* ------------------------------------------------------------------ */
162 static u32
tvaudio_carr2reg(u32 carrier
)
171 static void tvaudio_setcarrier(struct saa7134_dev
*dev
,
172 int primary
, int secondary
)
176 saa_writel(SAA7134_CARRIER1_FREQ0
>> 2, tvaudio_carr2reg(primary
));
177 saa_writel(SAA7134_CARRIER2_FREQ0
>> 2, tvaudio_carr2reg(secondary
));
180 #define SAA7134_MUTE_MASK 0xbb
181 #define SAA7134_MUTE_ANALOG 0x04
182 #define SAA7134_MUTE_I2S 0x40
184 static void mute_input_7134(struct saa7134_dev
*dev
)
187 struct saa7134_input
*in
;
188 int ausel
=0, ics
=0, ocs
=0;
191 /* look what is to do ... */
193 mute
= (dev
->ctl_mute
||
194 (dev
->automute
&& (&card(dev
).radio
) != in
));
195 if (card(dev
).mute
.name
) {
197 * 7130 - we'll mute using some unconnected audio input
198 * 7134 - we'll probably should switch external mux with gpio
201 in
= &card(dev
).mute
;
204 if (dev
->hw_mute
== mute
&&
205 dev
->hw_input
== in
&& !dev
->insuspend
) {
206 audio_dbg(1, "mute/input: nothing to do [mute=%d,input=%s]\n",
211 audio_dbg(1, "ctl_mute=%d automute=%d input=%s => mute=%d input=%s\n",
212 dev
->ctl_mute
, dev
->automute
,
213 dev
->input
->name
, mute
, in
->name
);
217 if (PCI_DEVICE_ID_PHILIPS_SAA7134
== dev
->pci
->device
)
219 saa_writeb(SAA7134_AUDIO_MUTE_CTRL
, mute
?
221 SAA7134_MUTE_ANALOG
|
225 /* switch internal audio mux */
227 case TV
: ausel
=0xc0; ics
=0x00; ocs
=0x02; break;
228 case LINE1
: ausel
=0x80; ics
=0x00; ocs
=0x00; break;
229 case LINE2
: ausel
=0x80; ics
=0x08; ocs
=0x01; break;
230 case LINE2_LEFT
: ausel
=0x80; ics
=0x08; ocs
=0x05; break;
232 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL
, 0xc0, ausel
);
233 saa_andorb(SAA7134_ANALOG_IO_SELECT
, 0x08, ics
);
234 saa_andorb(SAA7134_ANALOG_IO_SELECT
, 0x07, ocs
);
235 // for oss, we need to change the clock configuration
237 saa_andorb(SAA7134_SIF_SAMPLE_FREQ
, 0x03, 0x00);
239 saa_andorb(SAA7134_SIF_SAMPLE_FREQ
, 0x03, 0x01);
241 /* switch gpio-connected external audio mux */
242 if (0 == card(dev
).gpiomask
)
245 mask
= card(dev
).gpiomask
;
246 saa_andorl(SAA7134_GPIO_GPMODE0
>> 2, mask
, mask
);
247 saa_andorl(SAA7134_GPIO_GPSTATUS0
>> 2, mask
, in
->gpio
);
248 saa7134_track_gpio(dev
,in
->name
);
251 static void tvaudio_setmode(struct saa7134_dev
*dev
,
252 struct saa7134_tvaudio
*audio
,
257 if (dev
->tvnorm
->id
== V4L2_STD_NTSC
) {
262 if (audio_clock_tweak
> -1024 && audio_clock_tweak
< 1024)
263 tweak
= audio_clock_tweak
;
266 audio_dbg(1, "tvaudio_setmode: %s %s [%d.%03d/%d.%03d MHz] acpf=%d%+d\n",
268 audio
->carr1
/ 1000, audio
->carr1
% 1000,
269 audio
->carr2
/ 1000, audio
->carr2
% 1000,
273 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD0
, (acpf
& 0x0000ff) >> 0);
274 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD1
, (acpf
& 0x00ff00) >> 8);
275 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD2
, (acpf
& 0x030000) >> 16);
276 tvaudio_setcarrier(dev
,audio
->carr1
,audio
->carr2
);
278 switch (audio
->mode
) {
279 case TVAUDIO_FM_MONO
:
280 case TVAUDIO_FM_BG_STEREO
:
281 saa_writeb(SAA7134_DEMODULATOR
, 0x00);
282 saa_writeb(SAA7134_DCXO_IDENT_CTRL
, 0x00);
283 saa_writeb(SAA7134_FM_DEEMPHASIS
, 0x22);
284 saa_writeb(SAA7134_FM_DEMATRIX
, 0x80);
285 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT
, 0xa0);
287 case TVAUDIO_FM_K_STEREO
:
288 saa_writeb(SAA7134_DEMODULATOR
, 0x00);
289 saa_writeb(SAA7134_DCXO_IDENT_CTRL
, 0x01);
290 saa_writeb(SAA7134_FM_DEEMPHASIS
, 0x22);
291 saa_writeb(SAA7134_FM_DEMATRIX
, 0x80);
292 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT
, 0xa0);
294 case TVAUDIO_NICAM_FM
:
295 saa_writeb(SAA7134_DEMODULATOR
, 0x10);
296 saa_writeb(SAA7134_DCXO_IDENT_CTRL
, 0x00);
297 saa_writeb(SAA7134_FM_DEEMPHASIS
, 0x44);
298 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT
, 0xa1);
299 saa_writeb(SAA7134_NICAM_CONFIG
, 0x00);
301 case TVAUDIO_NICAM_AM
:
302 saa_writeb(SAA7134_DEMODULATOR
, 0x12);
303 saa_writeb(SAA7134_DCXO_IDENT_CTRL
, 0x00);
304 saa_writeb(SAA7134_FM_DEEMPHASIS
, 0x44);
305 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT
, 0xa1);
306 saa_writeb(SAA7134_NICAM_CONFIG
, 0x00);
308 case TVAUDIO_FM_SAT_STEREO
:
309 /* not implemented (yet) */
314 static int tvaudio_sleep(struct saa7134_dev
*dev
, int timeout
)
316 if (dev
->thread
.scan1
== dev
->thread
.scan2
&&
317 !kthread_should_stop()) {
319 set_current_state(TASK_INTERRUPTIBLE
);
322 schedule_timeout_interruptible
323 (msecs_to_jiffies(timeout
));
326 return dev
->thread
.scan1
!= dev
->thread
.scan2
;
329 static int tvaudio_checkcarrier(struct saa7134_dev
*dev
, struct mainscan
*scan
)
331 __s32 left
,right
,value
;
333 if (!(dev
->tvnorm
->id
& scan
->std
)) {
335 audio_dbg(1, "skipping %d.%03d MHz [%4s]\n",
336 scan
->carr
/ 1000, scan
->carr
% 1000, scan
->name
);
340 if (audio_debug
> 1) {
342 audio_dbg(1, "debug %d:", scan
->carr
);
343 for (i
= -150; i
<= 150; i
+= 30) {
344 tvaudio_setcarrier(dev
,scan
->carr
+i
,scan
->carr
+i
);
345 saa_readl(SAA7134_LEVEL_READOUT1
>> 2);
346 if (tvaudio_sleep(dev
,SCAN_SAMPLE_DELAY
))
348 value
= saa_readl(SAA7134_LEVEL_READOUT1
>> 2);
350 pr_cont(" # %6d # ", value
>> 16);
352 pr_cont(" %6d", value
>> 16);
357 tvaudio_setcarrier(dev
,scan
->carr
-90,scan
->carr
-90);
358 saa_readl(SAA7134_LEVEL_READOUT1
>> 2);
359 if (tvaudio_sleep(dev
,SCAN_SAMPLE_DELAY
))
361 left
= saa_readl(SAA7134_LEVEL_READOUT1
>> 2);
363 tvaudio_setcarrier(dev
,scan
->carr
+90,scan
->carr
+90);
364 saa_readl(SAA7134_LEVEL_READOUT1
>> 2);
365 if (tvaudio_sleep(dev
,SCAN_SAMPLE_DELAY
))
367 right
= saa_readl(SAA7134_LEVEL_READOUT1
>> 2);
371 value
= left
> right
? left
- right
: right
- left
;
372 audio_dbg(1, "scanning %d.%03d MHz [%4s] => dc is %5d [%d/%d]\n",
373 scan
->carr
/ 1000, scan
->carr
% 1000,
374 scan
->name
, value
, left
, right
);
379 static int tvaudio_getstereo(struct saa7134_dev
*dev
, struct saa7134_tvaudio
*audio
)
381 __u32 idp
, nicam
, nicam_status
;
384 switch (audio
->mode
) {
385 case TVAUDIO_FM_MONO
:
386 return V4L2_TUNER_SUB_MONO
;
387 case TVAUDIO_FM_K_STEREO
:
388 case TVAUDIO_FM_BG_STEREO
:
389 idp
= (saa_readb(SAA7134_IDENT_SIF
) & 0xe0) >> 5;
390 audio_dbg(1, "getstereo: fm/stereo: idp=0x%x\n", idp
);
391 if (0x03 == (idp
& 0x03))
392 retval
= V4L2_TUNER_SUB_LANG1
| V4L2_TUNER_SUB_LANG2
;
393 else if (0x05 == (idp
& 0x05))
394 retval
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_STEREO
;
395 else if (0x01 == (idp
& 0x01))
396 retval
= V4L2_TUNER_SUB_MONO
;
398 case TVAUDIO_FM_SAT_STEREO
:
399 /* not implemented (yet) */
401 case TVAUDIO_NICAM_FM
:
402 case TVAUDIO_NICAM_AM
:
403 nicam
= saa_readb(SAA7134_AUDIO_STATUS
);
404 audio_dbg(1, "getstereo: nicam=0x%x\n", nicam
);
406 nicam_status
= saa_readb(SAA7134_NICAM_STATUS
);
407 audio_dbg(1, "getstereo: nicam_status=0x%x\n",
410 switch (nicam_status
& 0x03) {
412 retval
= V4L2_TUNER_SUB_LANG1
| V4L2_TUNER_SUB_LANG2
;
415 retval
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_STEREO
;
418 retval
= V4L2_TUNER_SUB_MONO
;
421 /* No nicam detected */
426 audio_dbg(1, "found audio subchannels:%s%s%s%s\n",
427 (retval
& V4L2_TUNER_SUB_MONO
) ? " mono" : "",
428 (retval
& V4L2_TUNER_SUB_STEREO
) ? " stereo" : "",
429 (retval
& V4L2_TUNER_SUB_LANG1
) ? " lang1" : "",
430 (retval
& V4L2_TUNER_SUB_LANG2
) ? " lang2" : "");
434 static int tvaudio_setstereo(struct saa7134_dev
*dev
, struct saa7134_tvaudio
*audio
,
437 static char *name
[] = {
438 [ V4L2_TUNER_MODE_MONO
] = "mono",
439 [ V4L2_TUNER_MODE_STEREO
] = "stereo",
440 [ V4L2_TUNER_MODE_LANG1
] = "lang1",
441 [ V4L2_TUNER_MODE_LANG2
] = "lang2",
442 [ V4L2_TUNER_MODE_LANG1_LANG2
] = "lang1+lang2",
445 [ V4L2_TUNER_MODE_MONO
] = 0x00, /* ch1 */
446 [ V4L2_TUNER_MODE_STEREO
] = 0x80, /* auto */
447 [ V4L2_TUNER_MODE_LANG1
] = 0x00, /* ch1 */
448 [ V4L2_TUNER_MODE_LANG2
] = 0x01, /* ch2 */
449 [ V4L2_TUNER_MODE_LANG1_LANG2
] = 0x80, /* auto */
453 switch (audio
->mode
) {
454 case TVAUDIO_FM_MONO
:
455 /* nothing to do ... */
457 case TVAUDIO_FM_K_STEREO
:
458 case TVAUDIO_FM_BG_STEREO
:
459 case TVAUDIO_NICAM_AM
:
460 case TVAUDIO_NICAM_FM
:
461 audio_dbg(1, "setstereo [fm] => %s\n",
462 name
[mode
% ARRAY_SIZE(name
)]);
463 reg
= fm
[ mode
% ARRAY_SIZE(fm
) ];
464 saa_writeb(SAA7134_FM_DEMATRIX
, reg
);
466 case TVAUDIO_FM_SAT_STEREO
:
467 /* Not implemented */
473 static int tvaudio_thread(void *data
)
475 struct saa7134_dev
*dev
= data
;
476 int carr_vals
[ARRAY_SIZE(mainscan
)];
477 unsigned int i
, audio
, nscan
;
478 int max1
,max2
,carrier
,rx
,mode
,lastmode
,default_carrier
;
483 tvaudio_sleep(dev
,-1);
484 if (kthread_should_stop())
490 dev
->thread
.scan1
= dev
->thread
.scan2
;
491 audio_dbg(1, "tvaudio thread scan start [%d]\n",
495 saa_writeb(SAA7134_MONITOR_SELECT
, 0xa0);
496 saa_writeb(SAA7134_FM_DEMATRIX
, 0x80);
498 if (dev
->ctl_automute
)
501 mute_input_7134(dev
);
503 /* give the tuner some time */
504 if (tvaudio_sleep(dev
,SCAN_INITIAL_DELAY
))
512 for (i
= 0; i
< ARRAY_SIZE(mainscan
); i
++) {
513 if (!(dev
->tvnorm
->id
& mainscan
[i
].std
))
515 if (!default_carrier
)
516 default_carrier
= mainscan
[i
].carr
;
521 /* only one candidate -- skip scan ;) */
522 audio_dbg(1, "only one main carrier candidate - skipping scan\n");
524 carrier
= default_carrier
;
526 /* scan for the main carrier */
527 saa_writeb(SAA7134_MONITOR_SELECT
,0x00);
528 tvaudio_setmode(dev
,&tvaudio
[0],NULL
);
529 for (i
= 0; i
< ARRAY_SIZE(mainscan
); i
++) {
530 carr_vals
[i
] = tvaudio_checkcarrier(dev
, mainscan
+i
);
531 if (dev
->thread
.scan1
!= dev
->thread
.scan2
)
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) {
546 /* found good carrier */
547 audio_dbg(1, "found %s main sound carrier @ %d.%03d MHz [%d/%d]\n",
548 dev
->tvnorm
->name
, carrier
/1000, carrier
%1000,
550 dev
->last_carrier
= carrier
;
553 } else if (0 != dev
->last_carrier
) {
554 /* no carrier -- try last detected one as fallback */
555 carrier
= dev
->last_carrier
;
556 audio_dbg(1, "audio carrier scan failed, using %d.%03d MHz [last detected]\n",
557 carrier
/1000, carrier
%1000);
561 /* no carrier + no fallback -- use default */
562 carrier
= default_carrier
;
563 audio_dbg(1, "audio carrier scan failed, using %d.%03d MHz [default]\n",
564 carrier
/1000, carrier
%1000);
567 tvaudio_setcarrier(dev
,carrier
,carrier
);
568 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT
, 0x30, 0x00);
569 saa7134_tvaudio_setmute(dev
);
570 /* find the exact tv audio norm */
571 for (audio
= UNSET
, i
= 0; i
< TVAUDIO
; i
++) {
572 if (dev
->tvnorm
->id
!= UNSET
&&
573 !(dev
->tvnorm
->id
& tvaudio
[i
].std
))
575 if (tvaudio
[i
].carr1
!= carrier
)
577 /* Note: at least the primary carrier is right here */
580 tvaudio_setmode(dev
,&tvaudio
[i
],"trying");
581 if (tvaudio_sleep(dev
,SCAN_SUBCARRIER_DELAY
))
583 if (-1 != tvaudio_getstereo(dev
,&tvaudio
[i
])) {
588 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT
, 0x30, 0x30);
591 tvaudio_setmode(dev
,&tvaudio
[audio
],"using");
593 tvaudio_setstereo(dev
,&tvaudio
[audio
],V4L2_TUNER_MODE_MONO
);
594 dev
->tvaudio
= &tvaudio
[audio
];
601 if (tvaudio_sleep(dev
,5000))
603 if (kthread_should_stop())
605 if (UNSET
== dev
->thread
.mode
) {
606 rx
= tvaudio_getstereo(dev
, &tvaudio
[audio
]);
607 mode
= saa7134_tvaudio_rx2mode(rx
);
609 mode
= dev
->thread
.mode
;
611 if (lastmode
!= mode
) {
612 tvaudio_setstereo(dev
,&tvaudio
[audio
],mode
);
619 dev
->thread
.stopped
= 1;
623 /* ------------------------------------------------------------------ */
624 /* saa7133 / saa7135 code */
626 static char *stdres
[0x20] = {
627 [0x00] = "no standard detected",
628 [0x01] = "B/G (in progress)",
629 [0x02] = "D/K (in progress)",
630 [0x03] = "M (in progress)",
633 [0x05] = "B/G NICAM",
634 [0x06] = "D/K A2 (1)",
635 [0x07] = "D/K A2 (2)",
636 [0x08] = "D/K A2 (3)",
637 [0x09] = "D/K NICAM",
645 [0x0f] = "FM radio / IF 10.7 / 50 deemp",
646 [0x10] = "FM radio / IF 10.7 / 75 deemp",
647 [0x11] = "FM radio / IF sel / 50 deemp",
648 [0x12] = "FM radio / IF sel / 75 deemp",
650 [0x13 ... 0x1e ] = "unknown",
651 [0x1f] = "??? [in progress]",
656 #define SAA7135_DSP_RWCLEAR_RERR 1
658 static inline int saa_dsp_reset_error_bit(struct saa7134_dev
*dev
)
660 int state
= saa_readb(SAA7135_DSP_RWSTATE
);
661 if (unlikely(state
& SAA7135_DSP_RWSTATE_ERR
)) {
662 audio_dbg(2, "%s: resetting error bit\n", dev
->name
);
663 saa_writeb(SAA7135_DSP_RWCLEAR
, SAA7135_DSP_RWCLEAR_RERR
);
668 static inline int saa_dsp_wait_bit(struct saa7134_dev
*dev
, int bit
)
670 int state
, count
= DSP_RETRY
;
672 state
= saa_readb(SAA7135_DSP_RWSTATE
);
673 if (unlikely(state
& SAA7135_DSP_RWSTATE_ERR
)) {
674 pr_warn("%s: dsp access error\n", dev
->name
);
675 saa_dsp_reset_error_bit(dev
);
678 while (0 == (state
& bit
)) {
679 if (unlikely(0 == count
)) {
680 pr_err("dsp access wait timeout [bit=%s]\n",
681 (bit
& SAA7135_DSP_RWSTATE_WRR
) ? "WRR" :
682 (bit
& SAA7135_DSP_RWSTATE_RDB
) ? "RDB" :
683 (bit
& SAA7135_DSP_RWSTATE_IDA
) ? "IDA" :
688 state
= saa_readb(SAA7135_DSP_RWSTATE
);
695 int saa_dsp_writel(struct saa7134_dev
*dev
, int reg
, u32 value
)
699 audio_dbg(2, "dsp write reg 0x%x = 0x%06x\n", reg
<< 2, value
);
700 err
= saa_dsp_wait_bit(dev
,SAA7135_DSP_RWSTATE_WRR
);
703 saa_writel(reg
,value
);
704 err
= saa_dsp_wait_bit(dev
,SAA7135_DSP_RWSTATE_WRR
);
710 static int getstereo_7133(struct saa7134_dev
*dev
)
712 int retval
= V4L2_TUNER_SUB_MONO
;
715 value
= saa_readl(0x528 >> 2);
717 retval
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_STEREO
;
719 retval
= V4L2_TUNER_SUB_LANG1
| V4L2_TUNER_SUB_LANG2
;
723 static int mute_input_7133(struct saa7134_dev
*dev
)
728 struct saa7134_input
*in
;
731 switch (dev
->input
->amux
) {
744 saa_dsp_writel(dev
, 0x464 >> 2, xbarin
);
750 saa_dsp_writel(dev
, 0x46c >> 2, xbarout
);
752 saa_writel(0x594 >> 2, reg
);
755 /* switch gpio-connected external audio mux */
756 if (0 != card(dev
).gpiomask
) {
757 mask
= card(dev
).gpiomask
;
759 if (card(dev
).mute
.name
&& dev
->ctl_mute
)
760 in
= &card(dev
).mute
;
764 saa_andorl(SAA7134_GPIO_GPMODE0
>> 2, mask
, mask
);
765 saa_andorl(SAA7134_GPIO_GPSTATUS0
>> 2, mask
, in
->gpio
);
766 saa7134_track_gpio(dev
,in
->name
);
772 static int tvaudio_thread_ddep(void *data
)
774 struct saa7134_dev
*dev
= data
;
779 tvaudio_sleep(dev
,-1);
780 if (kthread_should_stop())
785 dev
->thread
.scan1
= dev
->thread
.scan2
;
786 audio_dbg(1, "tvaudio thread scan start [%d]\n",
789 if (audio_ddep
>= 0x04 && audio_ddep
<= 0x0e) {
790 /* insmod option override */
791 norms
= (audio_ddep
<< 2) | 0x01;
792 audio_dbg(1, "ddep override: %s\n",
794 } else if (&card(dev
).radio
== dev
->input
) {
795 audio_dbg(1, "FM Radio\n");
796 if (dev
->tuner_type
== TUNER_PHILIPS_TDA8290
) {
797 norms
= (0x11 << 2) | 0x01;
798 /* set IF frequency to 5.5 MHz */
799 saa_dsp_writel(dev
, 0x42c >> 2, 0x729555);
801 norms
= (0x0f << 2) | 0x01;
804 /* (let chip) scan for sound carrier */
806 if (dev
->tvnorm
->id
& (V4L2_STD_B
| V4L2_STD_GH
))
808 if (dev
->tvnorm
->id
& V4L2_STD_PAL_I
)
810 if (dev
->tvnorm
->id
& V4L2_STD_DK
)
812 if (dev
->tvnorm
->id
& V4L2_STD_MN
)
814 if (dev
->tvnorm
->id
& (V4L2_STD_SECAM_L
| V4L2_STD_SECAM_LC
))
817 norms
= 0x7c; /* all */
818 audio_dbg(1, "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" : "");
826 /* kick automatic standard detection */
827 saa_dsp_writel(dev
, 0x454 >> 2, 0);
828 saa_dsp_writel(dev
, 0x454 >> 2, norms
| 0x80);
830 /* setup crossbars */
831 saa_dsp_writel(dev
, 0x464 >> 2, 0x000000);
832 saa_dsp_writel(dev
, 0x470 >> 2, 0x101010);
834 if (tvaudio_sleep(dev
,3000))
836 value
= saa_readl(0x528 >> 2) & 0xffffff;
838 audio_dbg(1, "tvaudio thread status: 0x%x [%s%s%s]\n",
839 value
, stdres
[value
& 0x1f],
840 (value
& 0x000020) ? ",stereo" : "",
841 (value
& 0x000040) ? ",dual" : "");
842 audio_dbg(1, "detailed status: %s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n",
843 (value
& 0x000080) ? " A2/EIAJ pilot tone " : "",
844 (value
& 0x000100) ? " A2/EIAJ dual " : "",
845 (value
& 0x000200) ? " A2/EIAJ stereo " : "",
846 (value
& 0x000400) ? " A2/EIAJ noise mute " : "",
848 (value
& 0x000800) ? " BTSC/FM radio pilot " : "",
849 (value
& 0x001000) ? " SAP carrier " : "",
850 (value
& 0x002000) ? " BTSC stereo noise mute " : "",
851 (value
& 0x004000) ? " SAP noise mute " : "",
852 (value
& 0x008000) ? " VDSP " : "",
854 (value
& 0x010000) ? " NICST " : "",
855 (value
& 0x020000) ? " NICDU " : "",
856 (value
& 0x040000) ? " NICAM muted " : "",
857 (value
& 0x080000) ? " NICAM reserve sound " : "",
859 (value
& 0x100000) ? " init done " : "");
863 dev
->thread
.stopped
= 1;
867 /* ------------------------------------------------------------------ */
868 /* common stuff + external entry points */
870 void saa7134_enable_i2s(struct saa7134_dev
*dev
)
874 if (!card_is_empress(dev
))
877 if (dev
->pci
->device
== PCI_DEVICE_ID_PHILIPS_SAA7130
)
880 /* configure GPIO for out */
881 saa_andorl(SAA7134_GPIO_GPMODE0
>> 2, 0x0E000000, 0x00000000);
883 switch (dev
->pci
->device
) {
884 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
885 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
886 /* Set I2S format (SONY) */
887 saa_writeb(SAA7133_I2S_AUDIO_CONTROL
, 0x00);
889 saa_writeb(SAA7134_I2S_AUDIO_OUTPUT
, 0x11);
892 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
893 i2s_format
= (dev
->input
->amux
== TV
) ? 0x00 : 0x01;
895 /* enable I2S audio output for the mpeg encoder */
896 saa_writeb(SAA7134_I2S_OUTPUT_SELECT
, 0x80);
897 saa_writeb(SAA7134_I2S_OUTPUT_FORMAT
, i2s_format
);
898 saa_writeb(SAA7134_I2S_OUTPUT_LEVEL
, 0x0F);
899 saa_writeb(SAA7134_I2S_AUDIO_OUTPUT
, 0x01);
906 int saa7134_tvaudio_rx2mode(u32 rx
)
910 mode
= V4L2_TUNER_MODE_MONO
;
911 if (rx
& V4L2_TUNER_SUB_STEREO
)
912 mode
= V4L2_TUNER_MODE_STEREO
;
913 else if (rx
& V4L2_TUNER_SUB_LANG1
)
914 mode
= V4L2_TUNER_MODE_LANG1
;
915 else if (rx
& V4L2_TUNER_SUB_LANG2
)
916 mode
= V4L2_TUNER_MODE_LANG2
;
920 void saa7134_tvaudio_setmute(struct saa7134_dev
*dev
)
922 switch (dev
->pci
->device
) {
923 case PCI_DEVICE_ID_PHILIPS_SAA7130
:
924 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
925 mute_input_7134(dev
);
927 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
928 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
929 mute_input_7133(dev
);
934 void saa7134_tvaudio_setinput(struct saa7134_dev
*dev
,
935 struct saa7134_input
*in
)
938 switch (dev
->pci
->device
) {
939 case PCI_DEVICE_ID_PHILIPS_SAA7130
:
940 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
941 mute_input_7134(dev
);
943 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
944 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
945 mute_input_7133(dev
);
948 saa7134_enable_i2s(dev
);
951 void saa7134_tvaudio_setvolume(struct saa7134_dev
*dev
, int level
)
953 switch (dev
->pci
->device
) {
954 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
955 saa_writeb(SAA7134_CHANNEL1_LEVEL
, level
& 0x1f);
956 saa_writeb(SAA7134_CHANNEL2_LEVEL
, level
& 0x1f);
957 saa_writeb(SAA7134_NICAM_LEVEL_ADJUST
, level
& 0x1f);
962 int saa7134_tvaudio_getstereo(struct saa7134_dev
*dev
)
964 int retval
= V4L2_TUNER_SUB_MONO
;
966 switch (dev
->pci
->device
) {
967 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
969 retval
= tvaudio_getstereo(dev
,dev
->tvaudio
);
971 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
972 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
973 retval
= getstereo_7133(dev
);
979 void saa7134_tvaudio_init(struct saa7134_dev
*dev
)
981 int clock
= saa7134_boards
[dev
->board
].audio_clock
;
983 if (UNSET
!= audio_clock_override
)
984 clock
= audio_clock_override
;
986 switch (dev
->pci
->device
) {
987 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
988 /* init all audio registers */
989 saa_writeb(SAA7134_AUDIO_PLL_CTRL
, 0x00);
995 saa_writeb(SAA7134_AUDIO_CLOCK0
, clock
& 0xff);
996 saa_writeb(SAA7134_AUDIO_CLOCK1
, (clock
>> 8) & 0xff);
997 saa_writeb(SAA7134_AUDIO_CLOCK2
, (clock
>> 16) & 0xff);
998 /* frame locked audio is mandatory for NICAM */
999 saa_writeb(SAA7134_AUDIO_PLL_CTRL
, 0x01);
1000 saa_writeb(SAA7134_NICAM_ERROR_LOW
, 0x14);
1001 saa_writeb(SAA7134_NICAM_ERROR_HIGH
, 0x50);
1003 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
1004 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
1005 saa_writel(0x598 >> 2, clock
);
1006 saa_dsp_writel(dev
, 0x474 >> 2, 0x00);
1007 saa_dsp_writel(dev
, 0x450 >> 2, 0x00);
1011 int saa7134_tvaudio_init2(struct saa7134_dev
*dev
)
1013 int (*my_thread
)(void *data
) = NULL
;
1015 switch (dev
->pci
->device
) {
1016 case PCI_DEVICE_ID_PHILIPS_SAA7134
:
1017 my_thread
= tvaudio_thread
;
1019 case PCI_DEVICE_ID_PHILIPS_SAA7133
:
1020 case PCI_DEVICE_ID_PHILIPS_SAA7135
:
1021 my_thread
= tvaudio_thread_ddep
;
1025 dev
->thread
.thread
= NULL
;
1026 dev
->thread
.scan1
= dev
->thread
.scan2
= 0;
1028 saa7134_tvaudio_init(dev
);
1029 /* start tvaudio thread */
1030 dev
->thread
.thread
= kthread_run(my_thread
, dev
, "%s", dev
->name
);
1031 if (IS_ERR(dev
->thread
.thread
)) {
1032 pr_warn("%s: kernel_thread() failed\n",
1034 /* XXX: missing error handling here */
1038 saa7134_enable_i2s(dev
);
1042 int saa7134_tvaudio_close(struct saa7134_dev
*dev
)
1045 /* anything else to undo? */
1049 int saa7134_tvaudio_fini(struct saa7134_dev
*dev
)
1051 /* shutdown tvaudio thread */
1052 if (dev
->thread
.thread
&& !dev
->thread
.stopped
)
1053 kthread_stop(dev
->thread
.thread
);
1055 saa_andorb(SAA7134_ANALOG_IO_SELECT
, 0x07, 0x00); /* LINE1 */
1059 int saa7134_tvaudio_do_scan(struct saa7134_dev
*dev
)
1061 if (dev
->input
->amux
!= TV
) {
1062 audio_dbg(1, "sound IF not in use, skipping scan\n");
1064 saa7134_tvaudio_setmute(dev
);
1065 } else if (dev
->thread
.thread
) {
1066 dev
->thread
.mode
= UNSET
;
1067 dev
->thread
.scan2
++;
1069 if (!dev
->insuspend
&& !dev
->thread
.stopped
)
1070 wake_up_process(dev
->thread
.thread
);
1073 saa7134_tvaudio_setmute(dev
);
1078 EXPORT_SYMBOL(saa_dsp_writel
);
1079 EXPORT_SYMBOL(saa7134_tvaudio_setmute
);