2 * $Id: saa7134-tvaudio.c,v 1.30 2005/06/28 23:41:47 mkrufky 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 (card(dev).mute.name) {
224 * 7130 - we'll mute using some unconnected audio input
225 * 7134 - we'll probably should switch external mux with gpio
228 in = &card(dev).mute;
231 if (dev->hw_mute == mute &&
232 dev->hw_input == in) {
233 dprintk("mute/input: nothing to do [mute=%d,input=%s]\n",
238 dprintk("ctl_mute=%d automute=%d input=%s => mute=%d input=%s\n",
239 dev->ctl_mute,dev->automute,dev->input->name,mute,in->name);
243 if (PCI_DEVICE_ID_PHILIPS_SAA7134 == dev->pci->device)
245 saa_writeb(SAA7134_AUDIO_MUTE_CTRL, mute ? 0xbf : 0xbb);
247 /* switch internal audio mux */
249 case TV: ausel=0xc0; ics=0x00; ocs=0x02; break;
250 case LINE1: ausel=0x80; ics=0x00; ocs=0x00; break;
251 case LINE2: ausel=0x80; ics=0x08; ocs=0x01; break;
252 case LINE2_LEFT: ausel=0x80; ics=0x08; ocs=0x05; break;
254 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, ausel);
255 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, ics);
256 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, ocs);
257 // for oss, we need to change the clock configuration
259 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
261 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x01);
263 /* switch gpio-connected external audio mux */
264 if (0 == card(dev).gpiomask)
267 mask = card(dev).gpiomask;
268 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, mask, mask);
269 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio);
270 saa7134_track_gpio(dev,in->name);
273 static void tvaudio_setmode(struct saa7134_dev *dev,
274 struct saa7134_tvaudio *audio,
279 if (dev->tvnorm->id == V4L2_STD_NTSC) {
284 if (audio_clock_tweak > -1024 && audio_clock_tweak < 1024)
285 tweak = audio_clock_tweak;
288 dprintk("tvaudio_setmode: %s %s [%d.%03d/%d.%03d MHz] acpf=%d%+d\n",
290 audio->carr1 / 1000, audio->carr1 % 1000,
291 audio->carr2 / 1000, audio->carr2 % 1000,
295 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD0, (acpf & 0x0000ff) >> 0);
296 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD1, (acpf & 0x00ff00) >> 8);
297 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD2, (acpf & 0x030000) >> 16);
298 tvaudio_setcarrier(dev,audio->carr1,audio->carr2);
300 switch (audio->mode) {
301 case TVAUDIO_FM_MONO:
302 case TVAUDIO_FM_BG_STEREO:
303 saa_writeb(SAA7134_DEMODULATOR, 0x00);
304 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
305 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x22);
306 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
307 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa0);
309 case TVAUDIO_FM_K_STEREO:
310 saa_writeb(SAA7134_DEMODULATOR, 0x00);
311 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x01);
312 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x22);
313 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
314 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa0);
316 case TVAUDIO_NICAM_FM:
317 saa_writeb(SAA7134_DEMODULATOR, 0x10);
318 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
319 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x44);
320 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa1);
321 saa_writeb(SAA7134_NICAM_CONFIG, 0x00);
323 case TVAUDIO_NICAM_AM:
324 saa_writeb(SAA7134_DEMODULATOR, 0x12);
325 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
326 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x44);
327 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa1);
328 saa_writeb(SAA7134_NICAM_CONFIG, 0x00);
330 case TVAUDIO_FM_SAT_STEREO:
331 /* not implemented (yet) */
336 static int tvaudio_sleep(struct saa7134_dev *dev, int timeout)
338 DECLARE_WAITQUEUE(wait, current);
340 add_wait_queue(&dev->thread.wq, &wait);
341 if (dev->thread.scan1 == dev->thread.scan2 && !dev->thread.shutdown) {
343 set_current_state(TASK_INTERRUPTIBLE);
346 set_current_state(TASK_INTERRUPTIBLE);
347 schedule_timeout(msecs_to_jiffies(timeout));
350 remove_wait_queue(&dev->thread.wq, &wait);
351 return dev->thread.scan1 != dev->thread.scan2;
354 static int tvaudio_checkcarrier(struct saa7134_dev *dev, struct mainscan *scan)
356 __s32 left,right,value;
358 if (audio_debug > 1) {
360 dprintk("debug %d:",scan->carr);
361 for (i = -150; i <= 150; i += 30) {
362 tvaudio_setcarrier(dev,scan->carr+i,scan->carr+i);
363 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
364 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
366 value = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
368 printk(" # %6d # ",value >> 16);
370 printk(" %6d",value >> 16);
375 if (dev->tvnorm->id & scan->std) {
376 tvaudio_setcarrier(dev,scan->carr-90,scan->carr-90);
377 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
378 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
380 left = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
382 tvaudio_setcarrier(dev,scan->carr+90,scan->carr+90);
383 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
384 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
386 right = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
390 value = left > right ? left - right : right - left;
391 dprintk("scanning %d.%03d MHz [%4s] => dc is %5d [%d/%d]\n",
392 scan->carr / 1000, scan->carr % 1000,
393 scan->name, value, left, right);
396 dprintk("skipping %d.%03d MHz [%4s]\n",
397 scan->carr / 1000, scan->carr % 1000, scan->name);
403 static int tvaudio_getstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio)
405 __u32 idp, nicam, nicam_status;
408 switch (audio->mode) {
409 case TVAUDIO_FM_MONO:
410 return V4L2_TUNER_SUB_MONO;
411 case TVAUDIO_FM_K_STEREO:
412 case TVAUDIO_FM_BG_STEREO:
413 idp = (saa_readb(SAA7134_IDENT_SIF) & 0xe0) >> 5;
414 dprintk("getstereo: fm/stereo: idp=0x%x\n",idp);
415 if (0x03 == (idp & 0x03))
416 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
417 else if (0x05 == (idp & 0x05))
418 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
419 else if (0x01 == (idp & 0x01))
420 retval = V4L2_TUNER_SUB_MONO;
422 case TVAUDIO_FM_SAT_STEREO:
423 /* not implemented (yet) */
425 case TVAUDIO_NICAM_FM:
426 case TVAUDIO_NICAM_AM:
427 nicam = saa_readb(SAA7134_AUDIO_STATUS);
428 dprintk("getstereo: nicam=0x%x\n",nicam);
430 nicam_status = saa_readb(SAA7134_NICAM_STATUS);
431 dprintk("getstereo: nicam_status=0x%x\n", nicam_status);
433 switch (nicam_status & 0x03) {
435 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
438 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
441 retval = V4L2_TUNER_SUB_MONO;
444 /* No nicam detected */
449 dprintk("found audio subchannels:%s%s%s%s\n",
450 (retval & V4L2_TUNER_SUB_MONO) ? " mono" : "",
451 (retval & V4L2_TUNER_SUB_STEREO) ? " stereo" : "",
452 (retval & V4L2_TUNER_SUB_LANG1) ? " lang1" : "",
453 (retval & V4L2_TUNER_SUB_LANG2) ? " lang2" : "");
457 static int tvaudio_setstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio,
460 static char *name[] = {
461 [ V4L2_TUNER_MODE_MONO ] = "mono",
462 [ V4L2_TUNER_MODE_STEREO ] = "stereo",
463 [ V4L2_TUNER_MODE_LANG1 ] = "lang1",
464 [ V4L2_TUNER_MODE_LANG2 ] = "lang2",
467 [ V4L2_TUNER_MODE_MONO ] = 0x00, /* ch1 */
468 [ V4L2_TUNER_MODE_STEREO ] = 0x80, /* auto */
469 [ V4L2_TUNER_MODE_LANG1 ] = 0x00, /* ch1 */
470 [ V4L2_TUNER_MODE_LANG2 ] = 0x01, /* ch2 */
474 switch (audio->mode) {
475 case TVAUDIO_FM_MONO:
476 /* nothing to do ... */
478 case TVAUDIO_FM_K_STEREO:
479 case TVAUDIO_FM_BG_STEREO:
480 case TVAUDIO_NICAM_AM:
481 case TVAUDIO_NICAM_FM:
482 dprintk("setstereo [fm] => %s\n",
483 name[ mode % ARRAY_SIZE(name) ]);
484 reg = fm[ mode % ARRAY_SIZE(fm) ];
485 saa_writeb(SAA7134_FM_DEMATRIX, reg);
487 case TVAUDIO_FM_SAT_STEREO:
488 /* Not implemented */
494 static int tvaudio_thread(void *data)
496 struct saa7134_dev *dev = data;
497 int carr_vals[ARRAY_SIZE(mainscan)];
498 unsigned int i, audio, nscan;
499 int max1,max2,carrier,rx,mode,lastmode,default_carrier;
501 daemonize("%s", dev->name);
502 allow_signal(SIGTERM);
504 tvaudio_sleep(dev,-1);
505 if (dev->thread.shutdown || signal_pending(current))
509 dev->thread.scan1 = dev->thread.scan2;
510 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
513 if (dev->ctl_automute)
515 mute_input_7134(dev);
517 /* give the tuner some time */
518 if (tvaudio_sleep(dev,SCAN_INITIAL_DELAY))
526 for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
527 if (!(dev->tvnorm->id & mainscan[i].std))
529 if (!default_carrier)
530 default_carrier = mainscan[i].carr;
535 /* only one candidate -- skip scan ;) */
537 carrier = default_carrier;
539 /* scan for the main carrier */
540 saa_writeb(SAA7134_MONITOR_SELECT,0x00);
541 tvaudio_setmode(dev,&tvaudio[0],NULL);
542 for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
543 carr_vals[i] = tvaudio_checkcarrier(dev, mainscan+i);
544 if (dev->thread.scan1 != dev->thread.scan2)
547 for (max1 = 0, max2 = 0, i = 0; i < ARRAY_SIZE(mainscan); i++) {
548 if (max1 < carr_vals[i]) {
551 carrier = mainscan[i].carr;
552 } else if (max2 < carr_vals[i]) {
558 if (0 != carrier && max1 > 2000 && max1 > max2*3) {
559 /* found good carrier */
560 dprintk("found %s main sound carrier @ %d.%03d MHz [%d/%d]\n",
561 dev->tvnorm->name, carrier/1000, carrier%1000,
563 dev->last_carrier = carrier;
565 } else if (0 != dev->last_carrier) {
566 /* no carrier -- try last detected one as fallback */
567 carrier = dev->last_carrier;
568 dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
569 "using %d.%03d MHz [last detected]\n",
570 dev->name, carrier/1000, carrier%1000);
573 /* no carrier + no fallback -- use default */
574 carrier = default_carrier;
575 dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
576 "using %d.%03d MHz [default]\n",
577 dev->name, carrier/1000, carrier%1000);
579 tvaudio_setcarrier(dev,carrier,carrier);
581 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x00);
582 saa7134_tvaudio_setmute(dev);
584 /* find the exact tv audio norm */
585 for (audio = UNSET, i = 0; i < TVAUDIO; i++) {
586 if (dev->tvnorm->id != UNSET &&
587 !(dev->tvnorm->id & tvaudio[i].std))
589 if (tvaudio[i].carr1 != carrier)
594 tvaudio_setmode(dev,&tvaudio[i],"trying");
595 if (tvaudio_sleep(dev,SCAN_SUBCARRIER_DELAY))
597 if (-1 != tvaudio_getstereo(dev,&tvaudio[i])) {
602 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x30);
605 tvaudio_setmode(dev,&tvaudio[audio],"using");
606 tvaudio_setstereo(dev,&tvaudio[audio],V4L2_TUNER_MODE_MONO);
607 dev->tvaudio = &tvaudio[audio];
611 if (tvaudio_sleep(dev,5000))
613 if (dev->thread.shutdown || signal_pending(current))
615 if (UNSET == dev->thread.mode) {
616 rx = tvaudio_getstereo(dev,&tvaudio[i]);
617 mode = saa7134_tvaudio_rx2mode(rx);
619 mode = dev->thread.mode;
621 if (lastmode != mode) {
622 tvaudio_setstereo(dev,&tvaudio[audio],mode);
629 complete_and_exit(&dev->thread.exit, 0);
633 /* ------------------------------------------------------------------ */
634 /* saa7133 / saa7135 code */
636 static char *stdres[0x20] = {
637 [0x00] = "no standard detected",
638 [0x01] = "B/G (in progress)",
639 [0x02] = "D/K (in progress)",
640 [0x03] = "M (in progress)",
643 [0x05] = "B/G NICAM",
644 [0x06] = "D/K A2 (1)",
645 [0x07] = "D/K A2 (2)",
646 [0x08] = "D/K A2 (3)",
647 [0x09] = "D/K NICAM",
655 [0x0f] = "FM radio / IF 10.7 / 50 deemp",
656 [0x10] = "FM radio / IF 10.7 / 75 deemp",
657 [0x11] = "FM radio / IF sel / 50 deemp",
658 [0x12] = "FM radio / IF sel / 75 deemp",
660 [0x13 ... 0x1e ] = "unknown",
661 [0x1f] = "??? [in progress]",
667 static inline int saa_dsp_wait_bit(struct saa7134_dev *dev, int bit)
669 int state, count = DSP_RETRY;
671 state = saa_readb(SAA7135_DSP_RWSTATE);
672 if (unlikely(state & SAA7135_DSP_RWSTATE_ERR)) {
673 printk("%s: dsp access error\n",dev->name);
674 /* FIXME: send ack ... */
677 while (0 == (state & bit)) {
678 if (unlikely(0 == count)) {
679 printk("%s: 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 d2printk("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;
730 /* Hac 0506 route OSS sound simultanously */
732 switch (dev->input->amux) {
745 saa_dsp_writel(dev, 0x464 >> 2, xbarin);
751 saa_dsp_writel(dev, 0x46c >> 2, xbarout);
753 saa_writel(0x594 >> 2, reg);
756 /* switch gpio-connected external audio mux */
757 if (0 != card(dev).gpiomask) {
758 mask = card(dev).gpiomask;
760 if (card(dev).mute.name && dev->ctl_mute)
761 in = &card(dev).mute;
765 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, mask, mask);
766 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio);
767 saa7134_track_gpio(dev,in->name);
773 static int tvaudio_thread_ddep(void *data)
775 struct saa7134_dev *dev = data;
776 u32 value, norms, clock;
778 daemonize("%s", dev->name);
779 allow_signal(SIGTERM);
781 clock = saa7134_boards[dev->board].audio_clock;
782 if (UNSET != audio_clock_override)
783 clock = audio_clock_override;
784 saa_writel(0x598 >> 2, clock);
787 saa_dsp_writel(dev, 0x474 >> 2, 0x00);
788 saa_dsp_writel(dev, 0x450 >> 2, 0x00);
791 tvaudio_sleep(dev,-1);
792 if (dev->thread.shutdown || signal_pending(current))
796 dev->thread.scan1 = dev->thread.scan2;
797 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
799 if (audio_ddep >= 0x04 && audio_ddep <= 0x0e) {
800 /* insmod option override */
801 norms = (audio_ddep << 2) | 0x01;
802 dprintk("ddep override: %s\n",stdres[audio_ddep]);
803 } else if (&card(dev).radio == dev->input) {
804 dprintk("FM Radio\n");
805 norms = (0x0f << 2) | 0x01;
807 /* (let chip) scan for sound carrier */
809 if (dev->tvnorm->id & V4L2_STD_PAL) {
810 dprintk("PAL scan\n");
811 norms |= 0x2c; /* B/G + D/K + I */
813 if (dev->tvnorm->id & V4L2_STD_NTSC) {
814 dprintk("NTSC scan\n");
815 norms |= 0x40; /* M */
817 if (dev->tvnorm->id & V4L2_STD_SECAM) {
818 dprintk("SECAM scan\n");
819 norms |= 0x18; /* L + D/K */
822 norms = 0x7c; /* all */
823 dprintk("scanning:%s%s%s%s%s\n",
824 (norms & 0x04) ? " B/G" : "",
825 (norms & 0x08) ? " D/K" : "",
826 (norms & 0x10) ? " L/L'" : "",
827 (norms & 0x20) ? " I" : "",
828 (norms & 0x40) ? " M" : "");
831 /* kick automatic standard detection */
832 saa_dsp_writel(dev, 0x454 >> 2, 0);
833 saa_dsp_writel(dev, 0x454 >> 2, norms | 0x80);
835 /* setup crossbars */
836 saa_dsp_writel(dev, 0x464 >> 2, 0x000000);
837 saa_dsp_writel(dev, 0x470 >> 2, 0x101010);
839 if (tvaudio_sleep(dev,3000))
841 value = saa_readl(0x528 >> 2) & 0xffffff;
843 dprintk("tvaudio thread status: 0x%x [%s%s%s]\n",
844 value, stdres[value & 0x1f],
845 (value & 0x000020) ? ",stereo" : "",
846 (value & 0x000040) ? ",dual" : "");
847 dprintk("detailed status: "
848 "%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n",
849 (value & 0x000080) ? " A2/EIAJ pilot tone " : "",
850 (value & 0x000100) ? " A2/EIAJ dual " : "",
851 (value & 0x000200) ? " A2/EIAJ stereo " : "",
852 (value & 0x000400) ? " A2/EIAJ noise mute " : "",
854 (value & 0x000800) ? " BTSC/FM radio pilot " : "",
855 (value & 0x001000) ? " SAP carrier " : "",
856 (value & 0x002000) ? " BTSC stereo noise mute " : "",
857 (value & 0x004000) ? " SAP noise mute " : "",
858 (value & 0x008000) ? " VDSP " : "",
860 (value & 0x010000) ? " NICST " : "",
861 (value & 0x020000) ? " NICDU " : "",
862 (value & 0x040000) ? " NICAM muted " : "",
863 (value & 0x080000) ? " NICAM reserve sound " : "",
865 (value & 0x100000) ? " init done " : "");
869 complete_and_exit(&dev->thread.exit, 0);
873 /* ------------------------------------------------------------------ */
874 /* common stuff + external entry points */
876 static void saa7134_enable_i2s(struct saa7134_dev *dev)
880 if (!card_is_empress(dev))
882 i2s_format = (dev->input->amux == TV) ? 0x00 : 0x01;
884 /* enable I2S audio output for the mpeg encoder */
885 saa_writeb(SAA7134_I2S_OUTPUT_SELECT, 0x80);
886 saa_writeb(SAA7134_I2S_OUTPUT_FORMAT, i2s_format);
887 saa_writeb(SAA7134_I2S_OUTPUT_LEVEL, 0x0F);
888 saa_writeb(SAA7134_I2S_AUDIO_OUTPUT, 0x01);
891 int saa7134_tvaudio_rx2mode(u32 rx)
895 mode = V4L2_TUNER_MODE_MONO;
896 if (rx & V4L2_TUNER_SUB_STEREO)
897 mode = V4L2_TUNER_MODE_STEREO;
898 else if (rx & V4L2_TUNER_SUB_LANG1)
899 mode = V4L2_TUNER_MODE_LANG1;
900 else if (rx & V4L2_TUNER_SUB_LANG2)
901 mode = V4L2_TUNER_MODE_LANG2;
905 void saa7134_tvaudio_setmute(struct saa7134_dev *dev)
907 switch (dev->pci->device) {
908 case PCI_DEVICE_ID_PHILIPS_SAA7130:
909 case PCI_DEVICE_ID_PHILIPS_SAA7134:
910 mute_input_7134(dev);
912 case PCI_DEVICE_ID_PHILIPS_SAA7133:
913 case PCI_DEVICE_ID_PHILIPS_SAA7135:
914 mute_input_7133(dev);
919 void saa7134_tvaudio_setinput(struct saa7134_dev *dev,
920 struct saa7134_input *in)
923 switch (dev->pci->device) {
924 case PCI_DEVICE_ID_PHILIPS_SAA7130:
925 case PCI_DEVICE_ID_PHILIPS_SAA7134:
926 mute_input_7134(dev);
928 case PCI_DEVICE_ID_PHILIPS_SAA7133:
929 case PCI_DEVICE_ID_PHILIPS_SAA7135:
930 mute_input_7133(dev);
933 saa7134_enable_i2s(dev);
936 void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level)
938 switch (dev->pci->device) {
939 case PCI_DEVICE_ID_PHILIPS_SAA7134:
940 saa_writeb(SAA7134_CHANNEL1_LEVEL, level & 0x1f);
941 saa_writeb(SAA7134_CHANNEL2_LEVEL, level & 0x1f);
942 saa_writeb(SAA7134_NICAM_LEVEL_ADJUST, level & 0x1f);
947 int saa7134_tvaudio_getstereo(struct saa7134_dev *dev)
949 int retval = V4L2_TUNER_SUB_MONO;
951 switch (dev->pci->device) {
952 case PCI_DEVICE_ID_PHILIPS_SAA7134:
954 retval = tvaudio_getstereo(dev,dev->tvaudio);
956 case PCI_DEVICE_ID_PHILIPS_SAA7133:
957 case PCI_DEVICE_ID_PHILIPS_SAA7135:
958 retval = getstereo_7133(dev);
964 int saa7134_tvaudio_init2(struct saa7134_dev *dev)
966 DECLARE_MUTEX_LOCKED(sem);
967 int (*my_thread)(void *data) = NULL;
969 switch (dev->pci->device) {
970 case PCI_DEVICE_ID_PHILIPS_SAA7134:
971 my_thread = tvaudio_thread;
973 case PCI_DEVICE_ID_PHILIPS_SAA7133:
974 case PCI_DEVICE_ID_PHILIPS_SAA7135:
975 my_thread = tvaudio_thread_ddep;
979 dev->thread.pid = -1;
981 /* start tvaudio thread */
982 init_waitqueue_head(&dev->thread.wq);
983 init_completion(&dev->thread.exit);
984 dev->thread.pid = kernel_thread(my_thread,dev,0);
985 if (dev->thread.pid < 0)
986 printk(KERN_WARNING "%s: kernel_thread() failed\n",
988 saa7134_tvaudio_do_scan(dev);
991 saa7134_enable_i2s(dev);
995 int saa7134_tvaudio_fini(struct saa7134_dev *dev)
997 /* shutdown tvaudio thread */
998 if (dev->thread.pid >= 0) {
999 dev->thread.shutdown = 1;
1000 wake_up_interruptible(&dev->thread.wq);
1001 wait_for_completion(&dev->thread.exit);
1003 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, 0x00); /* LINE1 */
1007 int saa7134_tvaudio_do_scan(struct saa7134_dev *dev)
1009 if (dev->thread.pid >= 0) {
1010 dev->thread.mode = UNSET;
1011 dev->thread.scan2++;
1012 wake_up_interruptible(&dev->thread.wq);
1015 saa7134_tvaudio_setmute(dev);
1020 /* ----------------------------------------------------------- */