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.
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/kthread.h>
28 #include <linux/slab.h>
29 #include <linux/delay.h>
30 #include <asm/div64.h>
32 #include "saa7134-reg.h"
35 /* ------------------------------------------------------------------ */
37 static unsigned int audio_debug = 0;
38 module_param(audio_debug, int, 0644);
39 MODULE_PARM_DESC(audio_debug,"enable debug messages [tv audio]");
41 static unsigned int audio_ddep = 0;
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 = 0;
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 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
65 /* ------------------------------------------------------------------ */
68 static struct mainscan {
79 .std = V4L2_STD_B | V4L2_STD_GH,
83 .std = V4L2_STD_PAL_I,
87 .std = V4L2_STD_DK | V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC,
92 static struct saa7134_tvaudio tvaudio[] = {
94 .name = "PAL-B/G FM-stereo",
95 .std = V4L2_STD_PAL_BG,
96 .mode = TVAUDIO_FM_BG_STEREO,
100 .name = "PAL-D/K1 FM-stereo",
101 .std = V4L2_STD_PAL_DK,
104 .mode = TVAUDIO_FM_BG_STEREO,
106 .name = "PAL-D/K2 FM-stereo",
107 .std = V4L2_STD_PAL_DK,
110 .mode = TVAUDIO_FM_BG_STEREO,
112 .name = "PAL-D/K3 FM-stereo",
113 .std = V4L2_STD_PAL_DK,
116 .mode = TVAUDIO_FM_BG_STEREO,
118 .name = "PAL-B/G NICAM",
119 .std = V4L2_STD_PAL_BG,
122 .mode = TVAUDIO_NICAM_FM,
124 .name = "PAL-I NICAM",
125 .std = V4L2_STD_PAL_I,
128 .mode = TVAUDIO_NICAM_FM,
130 .name = "PAL-D/K NICAM",
131 .std = V4L2_STD_PAL_DK,
134 .mode = TVAUDIO_NICAM_FM,
136 .name = "SECAM-L NICAM",
137 .std = V4L2_STD_SECAM_L,
140 .mode = TVAUDIO_NICAM_AM,
142 .name = "SECAM-D/K NICAM",
143 .std = V4L2_STD_SECAM_DK,
146 .mode = TVAUDIO_NICAM_FM,
148 .name = "NTSC-A2 FM-stereo",
149 .std = V4L2_STD_NTSC,
152 .mode = TVAUDIO_FM_K_STEREO,
155 .std = V4L2_STD_NTSC,
158 .mode = TVAUDIO_FM_MONO,
161 #define TVAUDIO (sizeof(tvaudio)/sizeof(struct saa7134_tvaudio))
163 /* ------------------------------------------------------------------ */
165 static void tvaudio_init(struct saa7134_dev *dev)
167 int clock = saa7134_boards[dev->board].audio_clock;
169 if (UNSET != audio_clock_override)
170 clock = audio_clock_override;
172 /* init all audio registers */
173 saa_writeb(SAA7134_AUDIO_PLL_CTRL, 0x00);
179 saa_writeb(SAA7134_AUDIO_CLOCK0, clock & 0xff);
180 saa_writeb(SAA7134_AUDIO_CLOCK1, (clock >> 8) & 0xff);
181 saa_writeb(SAA7134_AUDIO_CLOCK2, (clock >> 16) & 0xff);
182 /* frame locked audio is mandatory for NICAM */
183 saa_writeb(SAA7134_AUDIO_PLL_CTRL, 0x01);
185 saa_writeb(SAA7134_NICAM_ERROR_LOW, 0x14);
186 saa_writeb(SAA7134_NICAM_ERROR_HIGH, 0x50);
187 saa_writeb(SAA7134_MONITOR_SELECT, 0xa0);
188 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
191 static u32 tvaudio_carr2reg(u32 carrier)
200 static void tvaudio_setcarrier(struct saa7134_dev *dev,
201 int primary, int secondary)
205 saa_writel(SAA7134_CARRIER1_FREQ0 >> 2, tvaudio_carr2reg(primary));
206 saa_writel(SAA7134_CARRIER2_FREQ0 >> 2, tvaudio_carr2reg(secondary));
209 #define SAA7134_MUTE_MASK 0xbb
210 #define SAA7134_MUTE_ANALOG 0x04
211 #define SAA7134_MUTE_I2S 0x40
213 static void mute_input_7134(struct saa7134_dev *dev)
216 struct saa7134_input *in;
217 int ausel=0, ics=0, ocs=0;
220 /* look what is to do ... */
222 mute = (dev->ctl_mute ||
223 (dev->automute && (&card(dev).radio) != in));
224 if (card(dev).mute.name) {
226 * 7130 - we'll mute using some unconnected audio input
227 * 7134 - we'll probably should switch external mux with gpio
230 in = &card(dev).mute;
233 if (dev->hw_mute == mute &&
234 dev->hw_input == in && !dev->inresume) {
235 dprintk("mute/input: nothing to do [mute=%d,input=%s]\n",
240 dprintk("ctl_mute=%d automute=%d input=%s => mute=%d input=%s\n",
241 dev->ctl_mute,dev->automute,dev->input->name,mute,in->name);
245 if (PCI_DEVICE_ID_PHILIPS_SAA7134 == dev->pci->device)
247 saa_writeb(SAA7134_AUDIO_MUTE_CTRL, mute ?
249 SAA7134_MUTE_ANALOG |
253 /* switch internal audio mux */
255 case TV: ausel=0xc0; ics=0x00; ocs=0x02; break;
256 case LINE1: ausel=0x80; ics=0x00; ocs=0x00; break;
257 case LINE2: ausel=0x80; ics=0x08; ocs=0x01; break;
258 case LINE2_LEFT: ausel=0x80; ics=0x08; ocs=0x05; break;
260 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, ausel);
261 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, ics);
262 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, ocs);
263 // for oss, we need to change the clock configuration
265 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
267 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x01);
269 /* switch gpio-connected external audio mux */
270 if (0 == card(dev).gpiomask)
273 mask = card(dev).gpiomask;
274 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, mask, mask);
275 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio);
276 saa7134_track_gpio(dev,in->name);
279 static void tvaudio_setmode(struct saa7134_dev *dev,
280 struct saa7134_tvaudio *audio,
285 if (dev->tvnorm->id == V4L2_STD_NTSC) {
290 if (audio_clock_tweak > -1024 && audio_clock_tweak < 1024)
291 tweak = audio_clock_tweak;
294 dprintk("tvaudio_setmode: %s %s [%d.%03d/%d.%03d MHz] acpf=%d%+d\n",
296 audio->carr1 / 1000, audio->carr1 % 1000,
297 audio->carr2 / 1000, audio->carr2 % 1000,
301 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD0, (acpf & 0x0000ff) >> 0);
302 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD1, (acpf & 0x00ff00) >> 8);
303 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD2, (acpf & 0x030000) >> 16);
304 tvaudio_setcarrier(dev,audio->carr1,audio->carr2);
306 switch (audio->mode) {
307 case TVAUDIO_FM_MONO:
308 case TVAUDIO_FM_BG_STEREO:
309 saa_writeb(SAA7134_DEMODULATOR, 0x00);
310 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
311 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x22);
312 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
313 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa0);
315 case TVAUDIO_FM_K_STEREO:
316 saa_writeb(SAA7134_DEMODULATOR, 0x00);
317 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x01);
318 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x22);
319 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
320 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa0);
322 case TVAUDIO_NICAM_FM:
323 saa_writeb(SAA7134_DEMODULATOR, 0x10);
324 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
325 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x44);
326 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa1);
327 saa_writeb(SAA7134_NICAM_CONFIG, 0x00);
329 case TVAUDIO_NICAM_AM:
330 saa_writeb(SAA7134_DEMODULATOR, 0x12);
331 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
332 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x44);
333 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa1);
334 saa_writeb(SAA7134_NICAM_CONFIG, 0x00);
336 case TVAUDIO_FM_SAT_STEREO:
337 /* not implemented (yet) */
342 static int tvaudio_sleep(struct saa7134_dev *dev, int timeout)
344 if (dev->thread.scan1 == dev->thread.scan2 &&
345 !kthread_should_stop()) {
347 set_current_state(TASK_INTERRUPTIBLE);
350 schedule_timeout_interruptible
351 (msecs_to_jiffies(timeout));
354 return dev->thread.scan1 != dev->thread.scan2;
357 static int tvaudio_checkcarrier(struct saa7134_dev *dev, struct mainscan *scan)
359 __s32 left,right,value;
361 if (audio_debug > 1) {
363 dprintk("debug %d:",scan->carr);
364 for (i = -150; i <= 150; i += 30) {
365 tvaudio_setcarrier(dev,scan->carr+i,scan->carr+i);
366 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
367 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
369 value = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
371 printk(" # %6d # ",value >> 16);
373 printk(" %6d",value >> 16);
377 if (dev->tvnorm->id & scan->std) {
378 tvaudio_setcarrier(dev,scan->carr-90,scan->carr-90);
379 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
380 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
382 left = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
384 tvaudio_setcarrier(dev,scan->carr+90,scan->carr+90);
385 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
386 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
388 right = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
392 value = left > right ? left - right : right - left;
393 dprintk("scanning %d.%03d MHz [%4s] => dc is %5d [%d/%d]\n",
394 scan->carr / 1000, scan->carr % 1000,
395 scan->name, value, left, right);
398 dprintk("skipping %d.%03d MHz [%4s]\n",
399 scan->carr / 1000, scan->carr % 1000, scan->name);
405 static int tvaudio_getstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio)
407 __u32 idp, nicam, nicam_status;
410 switch (audio->mode) {
411 case TVAUDIO_FM_MONO:
412 return V4L2_TUNER_SUB_MONO;
413 case TVAUDIO_FM_K_STEREO:
414 case TVAUDIO_FM_BG_STEREO:
415 idp = (saa_readb(SAA7134_IDENT_SIF) & 0xe0) >> 5;
416 dprintk("getstereo: fm/stereo: idp=0x%x\n",idp);
417 if (0x03 == (idp & 0x03))
418 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
419 else if (0x05 == (idp & 0x05))
420 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
421 else if (0x01 == (idp & 0x01))
422 retval = V4L2_TUNER_SUB_MONO;
424 case TVAUDIO_FM_SAT_STEREO:
425 /* not implemented (yet) */
427 case TVAUDIO_NICAM_FM:
428 case TVAUDIO_NICAM_AM:
429 nicam = saa_readb(SAA7134_AUDIO_STATUS);
430 dprintk("getstereo: nicam=0x%x\n",nicam);
432 nicam_status = saa_readb(SAA7134_NICAM_STATUS);
433 dprintk("getstereo: nicam_status=0x%x\n", nicam_status);
435 switch (nicam_status & 0x03) {
437 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
440 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
443 retval = V4L2_TUNER_SUB_MONO;
446 /* No nicam detected */
451 dprintk("found audio subchannels:%s%s%s%s\n",
452 (retval & V4L2_TUNER_SUB_MONO) ? " mono" : "",
453 (retval & V4L2_TUNER_SUB_STEREO) ? " stereo" : "",
454 (retval & V4L2_TUNER_SUB_LANG1) ? " lang1" : "",
455 (retval & V4L2_TUNER_SUB_LANG2) ? " lang2" : "");
459 static int tvaudio_setstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio,
462 static char *name[] = {
463 [ V4L2_TUNER_MODE_MONO ] = "mono",
464 [ V4L2_TUNER_MODE_STEREO ] = "stereo",
465 [ V4L2_TUNER_MODE_LANG1 ] = "lang1",
466 [ V4L2_TUNER_MODE_LANG2 ] = "lang2",
467 [ V4L2_TUNER_MODE_LANG1_LANG2 ] = "lang1+lang2",
470 [ V4L2_TUNER_MODE_MONO ] = 0x00, /* ch1 */
471 [ V4L2_TUNER_MODE_STEREO ] = 0x80, /* auto */
472 [ V4L2_TUNER_MODE_LANG1 ] = 0x00, /* ch1 */
473 [ V4L2_TUNER_MODE_LANG2 ] = 0x01, /* ch2 */
474 [ V4L2_TUNER_MODE_LANG1_LANG2 ] = 0x80, /* auto */
478 switch (audio->mode) {
479 case TVAUDIO_FM_MONO:
480 /* nothing to do ... */
482 case TVAUDIO_FM_K_STEREO:
483 case TVAUDIO_FM_BG_STEREO:
484 case TVAUDIO_NICAM_AM:
485 case TVAUDIO_NICAM_FM:
486 dprintk("setstereo [fm] => %s\n",
487 name[ mode % ARRAY_SIZE(name) ]);
488 reg = fm[ mode % ARRAY_SIZE(fm) ];
489 saa_writeb(SAA7134_FM_DEMATRIX, reg);
491 case TVAUDIO_FM_SAT_STEREO:
492 /* Not implemented */
498 static int tvaudio_thread(void *data)
500 struct saa7134_dev *dev = data;
501 int carr_vals[ARRAY_SIZE(mainscan)];
502 unsigned int i, audio, nscan;
503 int max1,max2,carrier,rx,mode,lastmode,default_carrier;
505 allow_signal(SIGTERM);
507 tvaudio_sleep(dev,-1);
508 if (kthread_should_stop() || signal_pending(current))
512 dev->thread.scan1 = dev->thread.scan2;
513 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
516 if (dev->ctl_automute)
518 mute_input_7134(dev);
520 /* give the tuner some time */
521 if (tvaudio_sleep(dev,SCAN_INITIAL_DELAY))
529 for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
530 if (!(dev->tvnorm->id & mainscan[i].std))
532 if (!default_carrier)
533 default_carrier = mainscan[i].carr;
538 /* only one candidate -- skip scan ;) */
539 dprintk("only one main carrier candidate - skipping scan\n");
541 carrier = default_carrier;
543 /* scan for the main carrier */
544 saa_writeb(SAA7134_MONITOR_SELECT,0x00);
545 tvaudio_setmode(dev,&tvaudio[0],NULL);
546 for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
547 carr_vals[i] = tvaudio_checkcarrier(dev, mainscan+i);
548 if (dev->thread.scan1 != dev->thread.scan2)
551 for (max1 = 0, max2 = 0, i = 0; i < ARRAY_SIZE(mainscan); i++) {
552 if (max1 < carr_vals[i]) {
555 carrier = mainscan[i].carr;
556 } else if (max2 < carr_vals[i]) {
562 if (0 != carrier && max1 > 2000 && max1 > max2*3) {
563 /* found good carrier */
564 dprintk("found %s main sound carrier @ %d.%03d MHz [%d/%d]\n",
565 dev->tvnorm->name, carrier/1000, carrier%1000,
567 dev->last_carrier = carrier;
569 } else if (0 != dev->last_carrier) {
570 /* no carrier -- try last detected one as fallback */
571 carrier = dev->last_carrier;
572 dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
573 "using %d.%03d MHz [last detected]\n",
574 dev->name, carrier/1000, carrier%1000);
577 /* no carrier + no fallback -- use default */
578 carrier = default_carrier;
579 dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
580 "using %d.%03d MHz [default]\n",
581 dev->name, carrier/1000, carrier%1000);
583 tvaudio_setcarrier(dev,carrier,carrier);
585 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x00);
586 saa7134_tvaudio_setmute(dev);
587 /* find the exact tv audio norm */
588 for (audio = UNSET, i = 0; i < TVAUDIO; i++) {
589 if (dev->tvnorm->id != UNSET &&
590 !(dev->tvnorm->id & tvaudio[i].std))
592 if (tvaudio[i].carr1 != carrier)
594 /* Note: at least the primary carrier is right here */
597 tvaudio_setmode(dev,&tvaudio[i],"trying");
598 if (tvaudio_sleep(dev,SCAN_SUBCARRIER_DELAY))
600 if (-1 != tvaudio_getstereo(dev,&tvaudio[i])) {
605 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x30);
608 tvaudio_setmode(dev,&tvaudio[audio],"using");
610 tvaudio_setstereo(dev,&tvaudio[audio],V4L2_TUNER_MODE_MONO);
611 dev->tvaudio = &tvaudio[audio];
615 if (tvaudio_sleep(dev,5000))
617 if (kthread_should_stop() || signal_pending(current))
619 if (UNSET == dev->thread.mode) {
620 rx = tvaudio_getstereo(dev,&tvaudio[i]);
621 mode = saa7134_tvaudio_rx2mode(rx);
623 mode = dev->thread.mode;
625 if (lastmode != mode) {
626 tvaudio_setstereo(dev,&tvaudio[audio],mode);
636 /* ------------------------------------------------------------------ */
637 /* saa7133 / saa7135 code */
639 static char *stdres[0x20] = {
640 [0x00] = "no standard detected",
641 [0x01] = "B/G (in progress)",
642 [0x02] = "D/K (in progress)",
643 [0x03] = "M (in progress)",
646 [0x05] = "B/G NICAM",
647 [0x06] = "D/K A2 (1)",
648 [0x07] = "D/K A2 (2)",
649 [0x08] = "D/K A2 (3)",
650 [0x09] = "D/K NICAM",
658 [0x0f] = "FM radio / IF 10.7 / 50 deemp",
659 [0x10] = "FM radio / IF 10.7 / 75 deemp",
660 [0x11] = "FM radio / IF sel / 50 deemp",
661 [0x12] = "FM radio / IF sel / 75 deemp",
663 [0x13 ... 0x1e ] = "unknown",
664 [0x1f] = "??? [in progress]",
670 static inline int saa_dsp_wait_bit(struct saa7134_dev *dev, int bit)
672 int state, count = DSP_RETRY;
674 state = saa_readb(SAA7135_DSP_RWSTATE);
675 if (unlikely(state & SAA7135_DSP_RWSTATE_ERR)) {
676 printk("%s: dsp access error\n",dev->name);
677 /* FIXME: send ack ... */
680 while (0 == (state & bit)) {
681 if (unlikely(0 == count)) {
682 printk("%s: dsp access wait timeout [bit=%s]\n",
684 (bit & SAA7135_DSP_RWSTATE_WRR) ? "WRR" :
685 (bit & SAA7135_DSP_RWSTATE_RDB) ? "RDB" :
686 (bit & SAA7135_DSP_RWSTATE_IDA) ? "IDA" :
691 state = saa_readb(SAA7135_DSP_RWSTATE);
698 int saa_dsp_writel(struct saa7134_dev *dev, int reg, u32 value)
702 d2printk("dsp write reg 0x%x = 0x%06x\n",reg<<2,value);
703 err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
706 saa_writel(reg,value);
707 err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
713 static int getstereo_7133(struct saa7134_dev *dev)
715 int retval = V4L2_TUNER_SUB_MONO;
718 value = saa_readl(0x528 >> 2);
720 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
722 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
726 static int mute_input_7133(struct saa7134_dev *dev)
731 struct saa7134_input *in;
734 switch (dev->input->amux) {
747 saa_dsp_writel(dev, 0x464 >> 2, xbarin);
753 saa_dsp_writel(dev, 0x46c >> 2, xbarout);
755 saa_writel(0x594 >> 2, reg);
758 /* switch gpio-connected external audio mux */
759 if (0 != card(dev).gpiomask) {
760 mask = card(dev).gpiomask;
762 if (card(dev).mute.name && dev->ctl_mute)
763 in = &card(dev).mute;
767 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, mask, mask);
768 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio);
769 saa7134_track_gpio(dev,in->name);
775 static int tvaudio_thread_ddep(void *data)
777 struct saa7134_dev *dev = data;
778 u32 value, norms, clock;
780 allow_signal(SIGTERM);
782 clock = saa7134_boards[dev->board].audio_clock;
783 if (UNSET != audio_clock_override)
784 clock = audio_clock_override;
785 saa_writel(0x598 >> 2, clock);
788 saa_dsp_writel(dev, 0x474 >> 2, 0x00);
789 saa_dsp_writel(dev, 0x450 >> 2, 0x00);
792 tvaudio_sleep(dev,-1);
793 if (kthread_should_stop() || signal_pending(current))
797 dev->thread.scan1 = dev->thread.scan2;
798 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
800 if (audio_ddep >= 0x04 && audio_ddep <= 0x0e) {
801 /* insmod option override */
802 norms = (audio_ddep << 2) | 0x01;
803 dprintk("ddep override: %s\n",stdres[audio_ddep]);
804 } else if (&card(dev).radio == dev->input) {
805 dprintk("FM Radio\n");
806 if (dev->tuner_type == TUNER_PHILIPS_TDA8290) {
807 norms = (0x11 << 2) | 0x01;
808 saa_dsp_writel(dev, 0x42c >> 2, 0x729555);
810 norms = (0x0f << 2) | 0x01;
813 /* (let chip) scan for sound carrier */
815 if (dev->tvnorm->id & (V4L2_STD_B | V4L2_STD_GH))
817 if (dev->tvnorm->id & V4L2_STD_PAL_I)
819 if (dev->tvnorm->id & V4L2_STD_DK)
821 if (dev->tvnorm->id & V4L2_STD_MN)
823 if (dev->tvnorm->id & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC))
826 norms = 0x7c; /* all */
827 dprintk("scanning:%s%s%s%s%s\n",
828 (norms & 0x04) ? " B/G" : "",
829 (norms & 0x08) ? " D/K" : "",
830 (norms & 0x10) ? " L/L'" : "",
831 (norms & 0x20) ? " I" : "",
832 (norms & 0x40) ? " M" : "");
835 /* kick automatic standard detection */
836 saa_dsp_writel(dev, 0x454 >> 2, 0);
837 saa_dsp_writel(dev, 0x454 >> 2, norms | 0x80);
839 /* setup crossbars */
840 saa_dsp_writel(dev, 0x464 >> 2, 0x000000);
841 saa_dsp_writel(dev, 0x470 >> 2, 0x101010);
843 if (tvaudio_sleep(dev,3000))
845 value = saa_readl(0x528 >> 2) & 0xffffff;
847 dprintk("tvaudio thread status: 0x%x [%s%s%s]\n",
848 value, stdres[value & 0x1f],
849 (value & 0x000020) ? ",stereo" : "",
850 (value & 0x000040) ? ",dual" : "");
851 dprintk("detailed status: "
852 "%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n",
853 (value & 0x000080) ? " A2/EIAJ pilot tone " : "",
854 (value & 0x000100) ? " A2/EIAJ dual " : "",
855 (value & 0x000200) ? " A2/EIAJ stereo " : "",
856 (value & 0x000400) ? " A2/EIAJ noise mute " : "",
858 (value & 0x000800) ? " BTSC/FM radio pilot " : "",
859 (value & 0x001000) ? " SAP carrier " : "",
860 (value & 0x002000) ? " BTSC stereo noise mute " : "",
861 (value & 0x004000) ? " SAP noise mute " : "",
862 (value & 0x008000) ? " VDSP " : "",
864 (value & 0x010000) ? " NICST " : "",
865 (value & 0x020000) ? " NICDU " : "",
866 (value & 0x040000) ? " NICAM muted " : "",
867 (value & 0x080000) ? " NICAM reserve sound " : "",
869 (value & 0x100000) ? " init done " : "");
876 /* ------------------------------------------------------------------ */
877 /* common stuff + external entry points */
879 void saa7134_enable_i2s(struct saa7134_dev *dev)
883 if (!card_is_empress(dev))
885 i2s_format = (dev->input->amux == TV) ? 0x00 : 0x01;
887 /* enable I2S audio output for the mpeg encoder */
888 saa_writeb(SAA7134_I2S_OUTPUT_SELECT, 0x80);
889 saa_writeb(SAA7134_I2S_OUTPUT_FORMAT, i2s_format);
890 saa_writeb(SAA7134_I2S_OUTPUT_LEVEL, 0x0F);
891 saa_writeb(SAA7134_I2S_AUDIO_OUTPUT, 0x01);
894 int saa7134_tvaudio_rx2mode(u32 rx)
898 mode = V4L2_TUNER_MODE_MONO;
899 if (rx & V4L2_TUNER_SUB_STEREO)
900 mode = V4L2_TUNER_MODE_STEREO;
901 else if (rx & V4L2_TUNER_SUB_LANG1)
902 mode = V4L2_TUNER_MODE_LANG1;
903 else if (rx & V4L2_TUNER_SUB_LANG2)
904 mode = V4L2_TUNER_MODE_LANG2;
908 void saa7134_tvaudio_setmute(struct saa7134_dev *dev)
910 switch (dev->pci->device) {
911 case PCI_DEVICE_ID_PHILIPS_SAA7130:
912 case PCI_DEVICE_ID_PHILIPS_SAA7134:
913 mute_input_7134(dev);
915 case PCI_DEVICE_ID_PHILIPS_SAA7133:
916 case PCI_DEVICE_ID_PHILIPS_SAA7135:
917 mute_input_7133(dev);
922 void saa7134_tvaudio_setinput(struct saa7134_dev *dev,
923 struct saa7134_input *in)
926 switch (dev->pci->device) {
927 case PCI_DEVICE_ID_PHILIPS_SAA7130:
928 case PCI_DEVICE_ID_PHILIPS_SAA7134:
929 mute_input_7134(dev);
931 case PCI_DEVICE_ID_PHILIPS_SAA7133:
932 case PCI_DEVICE_ID_PHILIPS_SAA7135:
933 mute_input_7133(dev);
936 saa7134_enable_i2s(dev);
939 void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level)
941 switch (dev->pci->device) {
942 case PCI_DEVICE_ID_PHILIPS_SAA7134:
943 saa_writeb(SAA7134_CHANNEL1_LEVEL, level & 0x1f);
944 saa_writeb(SAA7134_CHANNEL2_LEVEL, level & 0x1f);
945 saa_writeb(SAA7134_NICAM_LEVEL_ADJUST, level & 0x1f);
950 int saa7134_tvaudio_getstereo(struct saa7134_dev *dev)
952 int retval = V4L2_TUNER_SUB_MONO;
954 switch (dev->pci->device) {
955 case PCI_DEVICE_ID_PHILIPS_SAA7134:
957 retval = tvaudio_getstereo(dev,dev->tvaudio);
959 case PCI_DEVICE_ID_PHILIPS_SAA7133:
960 case PCI_DEVICE_ID_PHILIPS_SAA7135:
961 retval = getstereo_7133(dev);
967 int saa7134_tvaudio_init2(struct saa7134_dev *dev)
969 int (*my_thread)(void *data) = NULL;
971 switch (dev->pci->device) {
972 case PCI_DEVICE_ID_PHILIPS_SAA7134:
973 my_thread = tvaudio_thread;
975 case PCI_DEVICE_ID_PHILIPS_SAA7133:
976 case PCI_DEVICE_ID_PHILIPS_SAA7135:
977 my_thread = tvaudio_thread_ddep;
981 dev->thread.thread = NULL;
983 /* start tvaudio thread */
984 dev->thread.thread = kthread_run(my_thread, dev, "%s", dev->name);
985 if (IS_ERR(dev->thread.thread)) {
986 printk(KERN_WARNING "%s: kernel_thread() failed\n",
988 /* XXX: missing error handling here */
990 saa7134_tvaudio_do_scan(dev);
993 saa7134_enable_i2s(dev);
997 int saa7134_tvaudio_fini(struct saa7134_dev *dev)
999 /* shutdown tvaudio thread */
1000 if (dev->thread.thread)
1001 kthread_stop(dev->thread.thread);
1003 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, 0x00); /* LINE1 */
1007 int saa7134_tvaudio_do_scan(struct saa7134_dev *dev)
1009 if (dev->input->amux != TV) {
1010 dprintk("sound IF not in use, skipping scan\n");
1012 saa7134_tvaudio_setmute(dev);
1013 } else if (dev->thread.thread) {
1014 dev->thread.mode = UNSET;
1015 dev->thread.scan2++;
1016 wake_up_process(dev->thread.thread);
1019 saa7134_tvaudio_setmute(dev);
1024 EXPORT_SYMBOL(saa_dsp_writel);
1025 EXPORT_SYMBOL(saa7134_tvaudio_setmute);
1027 /* ----------------------------------------------------------- */