2 * $Id: saa7134-tvaudio.c,v 1.25 2005/06/07 19:00:38 nsh Exp $
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]
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <linux/smp_lock.h>
32 #include <asm/div64.h>
34 #include "saa7134-reg.h"
37 /* ------------------------------------------------------------------ */
39 static unsigned int audio_debug = 0;
40 module_param(audio_debug, int, 0644);
41 MODULE_PARM_DESC(audio_debug,"enable debug messages [tv audio]");
43 static unsigned int audio_ddep = 0;
44 module_param(audio_ddep, int, 0644);
45 MODULE_PARM_DESC(audio_ddep,"audio ddep overwrite");
47 static int audio_clock_override = UNSET;
48 module_param(audio_clock_override, int, 0644);
50 static int audio_clock_tweak = 0;
51 module_param(audio_clock_tweak, int, 0644);
52 MODULE_PARM_DESC(audio_clock_tweak, "Audio clock tick fine tuning for cards with audio crystal that's slightly off (range [-1024 .. 1024])");
54 #define dprintk(fmt, arg...) if (audio_debug) \
55 printk(KERN_DEBUG "%s/audio: " fmt, dev->name , ## arg)
56 #define d2printk(fmt, arg...) if (audio_debug > 1) \
57 printk(KERN_DEBUG "%s/audio: " fmt, dev->name, ## arg)
59 #define print_regb(reg) printk("%s: reg 0x%03x [%-16s]: 0x%02x\n", \
60 dev->name,(SAA7134_##reg),(#reg),saa_readb((SAA7134_##reg)))
63 #define SCAN_INITIAL_DELAY 1000
64 #define SCAN_SAMPLE_DELAY 200
65 #define SCAN_SUBCARRIER_DELAY 2000
67 /* ------------------------------------------------------------------ */
70 static struct mainscan {
77 .std = V4L2_STD_NTSC | V4L2_STD_PAL_M,
81 .std = V4L2_STD_PAL_BG,
85 .std = V4L2_STD_PAL_I,
89 .std = V4L2_STD_PAL_DK | V4L2_STD_SECAM,
94 static struct saa7134_tvaudio tvaudio[] = {
96 .name = "PAL-B/G FM-stereo",
98 .mode = TVAUDIO_FM_BG_STEREO,
102 .name = "PAL-D/K1 FM-stereo",
106 .mode = TVAUDIO_FM_BG_STEREO,
108 .name = "PAL-D/K2 FM-stereo",
112 .mode = TVAUDIO_FM_BG_STEREO,
114 .name = "PAL-D/K3 FM-stereo",
118 .mode = TVAUDIO_FM_BG_STEREO,
120 .name = "PAL-B/G NICAM",
124 .mode = TVAUDIO_NICAM_FM,
126 .name = "PAL-I NICAM",
130 .mode = TVAUDIO_NICAM_FM,
132 .name = "PAL-D/K NICAM",
136 .mode = TVAUDIO_NICAM_FM,
138 .name = "SECAM-L NICAM",
139 .std = V4L2_STD_SECAM,
142 .mode = TVAUDIO_NICAM_AM,
145 .std = V4L2_STD_SECAM,
148 .mode = TVAUDIO_FM_MONO,
151 .std = V4L2_STD_NTSC,
154 .mode = TVAUDIO_FM_MONO,
156 .name = "NTSC-A2 FM-stereo",
157 .std = V4L2_STD_NTSC,
160 .mode = TVAUDIO_FM_K_STEREO,
163 #define TVAUDIO (sizeof(tvaudio)/sizeof(struct saa7134_tvaudio))
165 /* ------------------------------------------------------------------ */
167 static void tvaudio_init(struct saa7134_dev *dev)
169 int clock = saa7134_boards[dev->board].audio_clock;
171 if (UNSET != audio_clock_override)
172 clock = audio_clock_override;
174 /* init all audio registers */
175 saa_writeb(SAA7134_AUDIO_PLL_CTRL, 0x00);
181 saa_writeb(SAA7134_AUDIO_CLOCK0, clock & 0xff);
182 saa_writeb(SAA7134_AUDIO_CLOCK1, (clock >> 8) & 0xff);
183 saa_writeb(SAA7134_AUDIO_CLOCK2, (clock >> 16) & 0xff);
184 // frame locked audio was reported not to be reliable
185 saa_writeb(SAA7134_AUDIO_PLL_CTRL, 0x02);
187 saa_writeb(SAA7134_NICAM_ERROR_LOW, 0x14);
188 saa_writeb(SAA7134_NICAM_ERROR_HIGH, 0x50);
189 saa_writeb(SAA7134_MONITOR_SELECT, 0xa0);
190 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
193 static u32 tvaudio_carr2reg(u32 carrier)
202 static void tvaudio_setcarrier(struct saa7134_dev *dev,
203 int primary, int secondary)
207 saa_writel(SAA7134_CARRIER1_FREQ0 >> 2, tvaudio_carr2reg(primary));
208 saa_writel(SAA7134_CARRIER2_FREQ0 >> 2, tvaudio_carr2reg(secondary));
211 static void mute_input_7134(struct saa7134_dev *dev)
214 struct saa7134_input *in;
215 int ausel=0, ics=0, ocs=0;
218 /* look what is to do ... */
220 mute = (dev->ctl_mute ||
221 (dev->automute && (&card(dev).radio) != in));
222 if (PCI_DEVICE_ID_PHILIPS_SAA7130 == dev->pci->device &&
223 card(dev).mute.name) {
224 /* 7130 - we'll mute using some unconnected audio input */
226 in = &card(dev).mute;
228 if (dev->hw_mute == mute &&
229 dev->hw_input == in) {
230 dprintk("mute/input: nothing to do [mute=%d,input=%s]\n",
235 dprintk("ctl_mute=%d automute=%d input=%s => mute=%d input=%s\n",
236 dev->ctl_mute,dev->automute,dev->input->name,mute,in->name);
240 if (PCI_DEVICE_ID_PHILIPS_SAA7134 == dev->pci->device)
242 saa_writeb(SAA7134_AUDIO_MUTE_CTRL, mute ? 0xbf : 0xbb);
244 /* switch internal audio mux */
246 case TV: ausel=0xc0; ics=0x00; ocs=0x02; break;
247 case LINE1: ausel=0x80; ics=0x00; ocs=0x00; break;
248 case LINE2: ausel=0x80; ics=0x08; ocs=0x01; break;
249 case LINE2_LEFT: ausel=0x80; ics=0x08; ocs=0x05; break;
251 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, ausel);
252 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, ics);
253 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, ocs);
254 // for oss, we need to change the clock configuration
256 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
258 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x01);
260 /* switch gpio-connected external audio mux */
261 if (0 == card(dev).gpiomask)
263 mask = card(dev).gpiomask;
264 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, mask, mask);
265 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio);
266 saa7134_track_gpio(dev,in->name);
269 static void tvaudio_setmode(struct saa7134_dev *dev,
270 struct saa7134_tvaudio *audio,
275 if (dev->tvnorm->id == V4L2_STD_NTSC) {
280 if (audio_clock_tweak > -1024 && audio_clock_tweak < 1024)
281 tweak = audio_clock_tweak;
284 dprintk("tvaudio_setmode: %s %s [%d.%03d/%d.%03d MHz] acpf=%d%+d\n",
286 audio->carr1 / 1000, audio->carr1 % 1000,
287 audio->carr2 / 1000, audio->carr2 % 1000,
291 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD0, (acpf & 0x0000ff) >> 0);
292 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD1, (acpf & 0x00ff00) >> 8);
293 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD2, (acpf & 0x030000) >> 16);
294 tvaudio_setcarrier(dev,audio->carr1,audio->carr2);
296 switch (audio->mode) {
297 case TVAUDIO_FM_MONO:
298 case TVAUDIO_FM_BG_STEREO:
299 saa_writeb(SAA7134_DEMODULATOR, 0x00);
300 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
301 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x22);
302 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
303 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa0);
305 case TVAUDIO_FM_K_STEREO:
306 saa_writeb(SAA7134_DEMODULATOR, 0x00);
307 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x01);
308 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x22);
309 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
310 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa0);
312 case TVAUDIO_NICAM_FM:
313 saa_writeb(SAA7134_DEMODULATOR, 0x10);
314 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
315 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x44);
316 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa1);
317 saa_writeb(SAA7134_NICAM_CONFIG, 0x00);
319 case TVAUDIO_NICAM_AM:
320 saa_writeb(SAA7134_DEMODULATOR, 0x12);
321 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
322 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x44);
323 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa1);
324 saa_writeb(SAA7134_NICAM_CONFIG, 0x00);
326 case TVAUDIO_FM_SAT_STEREO:
327 /* not implemented (yet) */
332 static int tvaudio_sleep(struct saa7134_dev *dev, int timeout)
334 DECLARE_WAITQUEUE(wait, current);
336 add_wait_queue(&dev->thread.wq, &wait);
337 if (dev->thread.scan1 == dev->thread.scan2 && !dev->thread.shutdown) {
339 set_current_state(TASK_INTERRUPTIBLE);
343 /* hmm, that one doesn't return on wakeup ... */
344 msleep_interruptible(timeout);
346 set_current_state(TASK_INTERRUPTIBLE);
347 schedule_timeout(msecs_to_jiffies(timeout));
351 remove_wait_queue(&dev->thread.wq, &wait);
352 return dev->thread.scan1 != dev->thread.scan2;
355 static int tvaudio_checkcarrier(struct saa7134_dev *dev, struct mainscan *scan)
357 __s32 left,right,value;
359 if (audio_debug > 1) {
361 dprintk("debug %d:",scan->carr);
362 for (i = -150; i <= 150; i += 30) {
363 tvaudio_setcarrier(dev,scan->carr+i,scan->carr+i);
364 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
365 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
367 value = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
369 printk(" # %6d # ",value >> 16);
371 printk(" %6d",value >> 16);
376 if (dev->tvnorm->id & scan->std) {
377 tvaudio_setcarrier(dev,scan->carr-90,scan->carr-90);
378 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
379 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
381 left = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
383 tvaudio_setcarrier(dev,scan->carr+90,scan->carr+90);
384 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
385 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
387 right = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
391 value = left > right ? left - right : right - left;
392 dprintk("scanning %d.%03d MHz [%4s] => dc is %5d [%d/%d]\n",
393 scan->carr / 1000, scan->carr % 1000,
394 scan->name, value, left, right);
397 dprintk("skipping %d.%03d MHz [%4s]\n",
398 scan->carr / 1000, scan->carr % 1000, scan->name);
404 static void sifdebug_dump_regs(struct saa7134_dev *dev)
406 print_regb(AUDIO_STATUS);
407 print_regb(IDENT_SIF);
408 print_regb(LEVEL_READOUT1);
409 print_regb(LEVEL_READOUT2);
410 print_regb(DCXO_IDENT_CTRL);
411 print_regb(DEMODULATOR);
412 print_regb(AGC_GAIN_SELECT);
413 print_regb(MONITOR_SELECT);
414 print_regb(FM_DEEMPHASIS);
415 print_regb(FM_DEMATRIX);
416 print_regb(SIF_SAMPLE_FREQ);
417 print_regb(ANALOG_IO_SELECT);
421 static int tvaudio_getstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio)
426 switch (audio->mode) {
427 case TVAUDIO_FM_MONO:
428 return V4L2_TUNER_SUB_MONO;
429 case TVAUDIO_FM_K_STEREO:
430 case TVAUDIO_FM_BG_STEREO:
431 idp = (saa_readb(SAA7134_IDENT_SIF) & 0xe0) >> 5;
432 dprintk("getstereo: fm/stereo: idp=0x%x\n",idp);
433 if (0x03 == (idp & 0x03))
434 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
435 else if (0x05 == (idp & 0x05))
436 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
437 else if (0x01 == (idp & 0x01))
438 retval = V4L2_TUNER_SUB_MONO;
440 case TVAUDIO_FM_SAT_STEREO:
441 /* not implemented (yet) */
443 case TVAUDIO_NICAM_FM:
444 case TVAUDIO_NICAM_AM:
445 nicam = saa_readb(SAA7134_NICAM_STATUS);
446 dprintk("getstereo: nicam=0x%x\n",nicam);
447 switch (nicam & 0x0b) {
449 retval = V4L2_TUNER_SUB_MONO;
452 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
455 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
461 dprintk("found audio subchannels:%s%s%s%s\n",
462 (retval & V4L2_TUNER_SUB_MONO) ? " mono" : "",
463 (retval & V4L2_TUNER_SUB_STEREO) ? " stereo" : "",
464 (retval & V4L2_TUNER_SUB_LANG1) ? " lang1" : "",
465 (retval & V4L2_TUNER_SUB_LANG2) ? " lang2" : "");
469 static int tvaudio_setstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio,
472 static char *name[] = {
473 [ V4L2_TUNER_MODE_MONO ] = "mono",
474 [ V4L2_TUNER_MODE_STEREO ] = "stereo",
475 [ V4L2_TUNER_MODE_LANG1 ] = "lang1",
476 [ V4L2_TUNER_MODE_LANG2 ] = "lang2",
479 [ V4L2_TUNER_MODE_MONO ] = 0x00, /* ch1 */
480 [ V4L2_TUNER_MODE_STEREO ] = 0x80, /* auto */
481 [ V4L2_TUNER_MODE_LANG1 ] = 0x00, /* ch1 */
482 [ V4L2_TUNER_MODE_LANG2 ] = 0x01, /* ch2 */
486 switch (audio->mode) {
487 case TVAUDIO_FM_MONO:
488 /* nothing to do ... */
490 case TVAUDIO_FM_K_STEREO:
491 case TVAUDIO_FM_BG_STEREO:
492 dprintk("setstereo [fm] => %s\n",
493 name[ mode % ARRAY_SIZE(name) ]);
494 reg = fm[ mode % ARRAY_SIZE(fm) ];
495 saa_writeb(SAA7134_FM_DEMATRIX, reg);
497 case TVAUDIO_FM_SAT_STEREO:
498 case TVAUDIO_NICAM_AM:
499 case TVAUDIO_NICAM_FM:
506 static int tvaudio_thread(void *data)
508 struct saa7134_dev *dev = data;
509 int carr_vals[ARRAY_SIZE(mainscan)];
510 unsigned int i, audio, nscan;
511 int max1,max2,carrier,rx,mode,lastmode,default_carrier;
513 daemonize("%s", dev->name);
514 allow_signal(SIGTERM);
516 tvaudio_sleep(dev,-1);
517 if (dev->thread.shutdown || signal_pending(current))
521 dev->thread.scan1 = dev->thread.scan2;
522 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
525 if (dev->ctl_automute)
527 mute_input_7134(dev);
529 /* give the tuner some time */
530 if (tvaudio_sleep(dev,SCAN_INITIAL_DELAY))
538 for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
539 if (!(dev->tvnorm->id & mainscan[i].std))
541 if (!default_carrier)
542 default_carrier = mainscan[i].carr;
547 /* only one candidate -- skip scan ;) */
549 carrier = default_carrier;
551 /* scan for the main carrier */
552 saa_writeb(SAA7134_MONITOR_SELECT,0x00);
553 tvaudio_setmode(dev,&tvaudio[0],NULL);
554 for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
555 carr_vals[i] = tvaudio_checkcarrier(dev, mainscan+i);
556 if (dev->thread.scan1 != dev->thread.scan2)
559 for (max1 = 0, max2 = 0, i = 0; i < ARRAY_SIZE(mainscan); i++) {
560 if (max1 < carr_vals[i]) {
563 carrier = mainscan[i].carr;
564 } else if (max2 < carr_vals[i]) {
570 if (0 != carrier && max1 > 2000 && max1 > max2*3) {
571 /* found good carrier */
572 dprintk("found %s main sound carrier @ %d.%03d MHz [%d/%d]\n",
573 dev->tvnorm->name, carrier/1000, carrier%1000,
575 dev->last_carrier = carrier;
577 } else if (0 != dev->last_carrier) {
578 /* no carrier -- try last detected one as fallback */
579 carrier = dev->last_carrier;
580 dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
581 "using %d.%03d MHz [last detected]\n",
582 dev->name, carrier/1000, carrier%1000);
585 /* no carrier + no fallback -- use default */
586 carrier = default_carrier;
587 dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
588 "using %d.%03d MHz [default]\n",
589 dev->name, carrier/1000, carrier%1000);
591 tvaudio_setcarrier(dev,carrier,carrier);
593 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x00);
594 saa7134_tvaudio_setmute(dev);
596 /* find the exact tv audio norm */
597 for (audio = UNSET, i = 0; i < TVAUDIO; i++) {
598 if (dev->tvnorm->id != UNSET &&
599 !(dev->tvnorm->id & tvaudio[i].std))
601 if (tvaudio[i].carr1 != carrier)
606 tvaudio_setmode(dev,&tvaudio[i],"trying");
607 if (tvaudio_sleep(dev,SCAN_SUBCARRIER_DELAY))
609 if (-1 != tvaudio_getstereo(dev,&tvaudio[i])) {
614 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x30);
617 tvaudio_setmode(dev,&tvaudio[audio],"using");
618 tvaudio_setstereo(dev,&tvaudio[audio],V4L2_TUNER_MODE_MONO);
619 dev->tvaudio = &tvaudio[audio];
623 if (tvaudio_sleep(dev,5000))
625 if (dev->thread.shutdown || signal_pending(current))
627 if (UNSET == dev->thread.mode) {
628 rx = tvaudio_getstereo(dev,&tvaudio[i]);
629 mode = saa7134_tvaudio_rx2mode(rx);
631 mode = dev->thread.mode;
633 if (lastmode != mode) {
634 tvaudio_setstereo(dev,&tvaudio[audio],mode);
641 complete_and_exit(&dev->thread.exit, 0);
645 /* ------------------------------------------------------------------ */
646 /* saa7133 / saa7135 code */
648 static char *stdres[0x20] = {
649 [0x00] = "no standard detected",
650 [0x01] = "B/G (in progress)",
651 [0x02] = "D/K (in progress)",
652 [0x03] = "M (in progress)",
655 [0x05] = "B/G NICAM",
656 [0x06] = "D/K A2 (1)",
657 [0x07] = "D/K A2 (2)",
658 [0x08] = "D/K A2 (3)",
659 [0x09] = "D/K NICAM",
667 [0x0f] = "FM radio / IF 10.7 / 50 deemp",
668 [0x10] = "FM radio / IF 10.7 / 75 deemp",
669 [0x11] = "FM radio / IF sel / 50 deemp",
670 [0x12] = "FM radio / IF sel / 75 deemp",
672 [0x13 ... 0x1e ] = "unknown",
673 [0x1f] = "??? [in progress]",
679 static inline int saa_dsp_wait_bit(struct saa7134_dev *dev, int bit)
681 int state, count = DSP_RETRY;
683 state = saa_readb(SAA7135_DSP_RWSTATE);
684 if (unlikely(state & SAA7135_DSP_RWSTATE_ERR)) {
685 printk("%s: dsp access error\n",dev->name);
686 /* FIXME: send ack ... */
689 while (0 == (state & bit)) {
690 if (unlikely(0 == count)) {
691 printk("%s: dsp access wait timeout [bit=%s]\n",
693 (bit & SAA7135_DSP_RWSTATE_WRR) ? "WRR" :
694 (bit & SAA7135_DSP_RWSTATE_RDB) ? "RDB" :
695 (bit & SAA7135_DSP_RWSTATE_IDA) ? "IDA" :
700 state = saa_readb(SAA7135_DSP_RWSTATE);
707 static int saa_dsp_readl(struct saa7134_dev *dev, int reg, u32 *value)
711 d2printk("dsp read reg 0x%x\n", reg<<2);
713 err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_RDB);
716 *value = saa_readl(reg);
717 d2printk("dsp read => 0x%06x\n", *value & 0xffffff);
718 err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_IDA);
725 int saa_dsp_writel(struct saa7134_dev *dev, int reg, u32 value)
729 d2printk("dsp write reg 0x%x = 0x%06x\n",reg<<2,value);
730 err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
733 saa_writel(reg,value);
734 err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
740 static int getstereo_7133(struct saa7134_dev *dev)
742 int retval = V4L2_TUNER_SUB_MONO;
745 value = saa_readl(0x528 >> 2);
747 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
749 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
753 static int mute_input_7133(struct saa7134_dev *dev)
758 switch (dev->input->amux) {
772 saa_writel(0x594 >> 2, reg);
774 /* switch gpio-connected external audio mux */
775 if (0 != card(dev).gpiomask) {
776 mask = card(dev).gpiomask;
777 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, mask, mask);
778 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, dev->input->gpio);
779 saa7134_track_gpio(dev,dev->input->name);
784 static int tvaudio_thread_ddep(void *data)
786 struct saa7134_dev *dev = data;
787 u32 value, norms, clock;
789 daemonize("%s", dev->name);
790 allow_signal(SIGTERM);
792 clock = saa7134_boards[dev->board].audio_clock;
793 if (UNSET != audio_clock_override)
794 clock = audio_clock_override;
795 saa_writel(0x598 >> 2, clock);
798 saa_dsp_writel(dev, 0x474 >> 2, 0x00);
799 saa_dsp_writel(dev, 0x450 >> 2, 0x00);
802 tvaudio_sleep(dev,-1);
803 if (dev->thread.shutdown || signal_pending(current))
807 dev->thread.scan1 = dev->thread.scan2;
808 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
810 if (audio_ddep >= 0x04 && audio_ddep <= 0x0e) {
811 /* insmod option override */
812 norms = (audio_ddep << 2) | 0x01;
813 dprintk("ddep override: %s\n",stdres[audio_ddep]);
814 } else if (&card(dev).radio == dev->input) {
815 dprintk("FM Radio\n");
816 norms = (0x0f << 2) | 0x01;
818 /* (let chip) scan for sound carrier */
820 if (dev->tvnorm->id & V4L2_STD_PAL) {
821 dprintk("PAL scan\n");
822 norms |= 0x2c; /* B/G + D/K + I */
824 if (dev->tvnorm->id & V4L2_STD_NTSC) {
825 dprintk("NTSC scan\n");
826 norms |= 0x40; /* M */
828 if (dev->tvnorm->id & V4L2_STD_SECAM) {
829 dprintk("SECAM scan\n");
830 norms |= 0x18; /* L + D/K */
833 norms = 0x7c; /* all */
834 dprintk("scanning:%s%s%s%s%s\n",
835 (norms & 0x04) ? " B/G" : "",
836 (norms & 0x08) ? " D/K" : "",
837 (norms & 0x10) ? " L/L'" : "",
838 (norms & 0x20) ? " I" : "",
839 (norms & 0x40) ? " M" : "");
842 /* kick automatic standard detection */
843 saa_dsp_writel(dev, 0x454 >> 2, 0);
844 saa_dsp_writel(dev, 0x454 >> 2, norms | 0x80);
846 /* setup crossbars */
847 saa_dsp_writel(dev, 0x464 >> 2, 0x000000);
848 saa_dsp_writel(dev, 0x470 >> 2, 0x101010);
850 if (tvaudio_sleep(dev,3000))
852 value = saa_readl(0x528 >> 2) & 0xffffff;
854 dprintk("tvaudio thread status: 0x%x [%s%s%s]\n",
855 value, stdres[value & 0x1f],
856 (value & 0x000020) ? ",stereo" : "",
857 (value & 0x000040) ? ",dual" : "");
858 dprintk("detailed status: "
859 "%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n",
860 (value & 0x000080) ? " A2/EIAJ pilot tone " : "",
861 (value & 0x000100) ? " A2/EIAJ dual " : "",
862 (value & 0x000200) ? " A2/EIAJ stereo " : "",
863 (value & 0x000400) ? " A2/EIAJ noise mute " : "",
865 (value & 0x000800) ? " BTSC/FM radio pilot " : "",
866 (value & 0x001000) ? " SAP carrier " : "",
867 (value & 0x002000) ? " BTSC stereo noise mute " : "",
868 (value & 0x004000) ? " SAP noise mute " : "",
869 (value & 0x008000) ? " VDSP " : "",
871 (value & 0x010000) ? " NICST " : "",
872 (value & 0x020000) ? " NICDU " : "",
873 (value & 0x040000) ? " NICAM muted " : "",
874 (value & 0x080000) ? " NICAM reserve sound " : "",
876 (value & 0x100000) ? " init done " : "");
880 complete_and_exit(&dev->thread.exit, 0);
884 /* ------------------------------------------------------------------ */
885 /* common stuff + external entry points */
887 static void saa7134_enable_i2s(struct saa7134_dev *dev)
891 if (!card_is_empress(dev))
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);
902 int saa7134_tvaudio_rx2mode(u32 rx)
906 mode = V4L2_TUNER_MODE_MONO;
907 if (rx & V4L2_TUNER_SUB_STEREO)
908 mode = V4L2_TUNER_MODE_STEREO;
909 else if (rx & V4L2_TUNER_SUB_LANG1)
910 mode = V4L2_TUNER_MODE_LANG1;
911 else if (rx & V4L2_TUNER_SUB_LANG2)
912 mode = V4L2_TUNER_MODE_LANG2;
916 void saa7134_tvaudio_setmute(struct saa7134_dev *dev)
918 switch (dev->pci->device) {
919 case PCI_DEVICE_ID_PHILIPS_SAA7130:
920 case PCI_DEVICE_ID_PHILIPS_SAA7134:
921 mute_input_7134(dev);
923 case PCI_DEVICE_ID_PHILIPS_SAA7133:
924 case PCI_DEVICE_ID_PHILIPS_SAA7135:
925 mute_input_7133(dev);
930 void saa7134_tvaudio_setinput(struct saa7134_dev *dev,
931 struct saa7134_input *in)
934 switch (dev->pci->device) {
935 case PCI_DEVICE_ID_PHILIPS_SAA7130:
936 case PCI_DEVICE_ID_PHILIPS_SAA7134:
937 mute_input_7134(dev);
939 case PCI_DEVICE_ID_PHILIPS_SAA7133:
940 case PCI_DEVICE_ID_PHILIPS_SAA7135:
941 mute_input_7133(dev);
944 saa7134_enable_i2s(dev);
947 void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level)
949 switch (dev->pci->device) {
950 case PCI_DEVICE_ID_PHILIPS_SAA7134:
951 saa_writeb(SAA7134_CHANNEL1_LEVEL, level & 0x1f);
952 saa_writeb(SAA7134_CHANNEL2_LEVEL, level & 0x1f);
953 saa_writeb(SAA7134_NICAM_LEVEL_ADJUST, level & 0x1f);
958 int saa7134_tvaudio_getstereo(struct saa7134_dev *dev)
960 int retval = V4L2_TUNER_SUB_MONO;
962 switch (dev->pci->device) {
963 case PCI_DEVICE_ID_PHILIPS_SAA7134:
965 retval = tvaudio_getstereo(dev,dev->tvaudio);
967 case PCI_DEVICE_ID_PHILIPS_SAA7133:
968 case PCI_DEVICE_ID_PHILIPS_SAA7135:
969 retval = getstereo_7133(dev);
975 int saa7134_tvaudio_init2(struct saa7134_dev *dev)
977 DECLARE_MUTEX_LOCKED(sem);
978 int (*my_thread)(void *data) = NULL;
980 switch (dev->pci->device) {
981 case PCI_DEVICE_ID_PHILIPS_SAA7134:
982 my_thread = tvaudio_thread;
984 case PCI_DEVICE_ID_PHILIPS_SAA7133:
985 case PCI_DEVICE_ID_PHILIPS_SAA7135:
986 my_thread = tvaudio_thread_ddep;
990 dev->thread.pid = -1;
992 /* start tvaudio thread */
993 init_waitqueue_head(&dev->thread.wq);
994 init_completion(&dev->thread.exit);
995 dev->thread.pid = kernel_thread(my_thread,dev,0);
996 if (dev->thread.pid < 0)
997 printk(KERN_WARNING "%s: kernel_thread() failed\n",
999 saa7134_tvaudio_do_scan(dev);
1002 saa7134_enable_i2s(dev);
1006 int saa7134_tvaudio_fini(struct saa7134_dev *dev)
1008 /* shutdown tvaudio thread */
1009 if (dev->thread.pid >= 0) {
1010 dev->thread.shutdown = 1;
1011 wake_up_interruptible(&dev->thread.wq);
1012 wait_for_completion(&dev->thread.exit);
1014 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, 0x00); /* LINE1 */
1018 int saa7134_tvaudio_do_scan(struct saa7134_dev *dev)
1020 if (dev->thread.pid >= 0) {
1021 dev->thread.mode = UNSET;
1022 dev->thread.scan2++;
1023 wake_up_interruptible(&dev->thread.wq);
1026 saa7134_tvaudio_setmute(dev);
1031 /* ----------------------------------------------------------- */