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/moduleparam.h>
27 #include <linux/kernel.h>
28 #include <linux/kthread.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <asm/div64.h>
33 #include "saa7134-reg.h"
36 /* ------------------------------------------------------------------ */
38 static unsigned int audio_debug = 0;
39 module_param(audio_debug, int, 0644);
40 MODULE_PARM_DESC(audio_debug,"enable debug messages [tv audio]");
42 static unsigned int audio_ddep = 0;
43 module_param(audio_ddep, int, 0644);
44 MODULE_PARM_DESC(audio_ddep,"audio ddep overwrite");
46 static int audio_clock_override = UNSET;
47 module_param(audio_clock_override, int, 0644);
49 static int audio_clock_tweak = 0;
50 module_param(audio_clock_tweak, int, 0644);
51 MODULE_PARM_DESC(audio_clock_tweak, "Audio clock tick fine tuning for cards with audio crystal that's slightly off (range [-1024 .. 1024])");
53 #define dprintk(fmt, arg...) if (audio_debug) \
54 printk(KERN_DEBUG "%s/audio: " fmt, dev->name , ## arg)
55 #define d2printk(fmt, arg...) if (audio_debug > 1) \
56 printk(KERN_DEBUG "%s/audio: " fmt, dev->name, ## arg)
58 #define print_regb(reg) printk("%s: reg 0x%03x [%-16s]: 0x%02x\n", \
59 dev->name,(SAA7134_##reg),(#reg),saa_readb((SAA7134_##reg)))
62 #define SCAN_INITIAL_DELAY 1000
63 #define SCAN_SAMPLE_DELAY 200
64 #define SCAN_SUBCARRIER_DELAY 2000
66 /* ------------------------------------------------------------------ */
69 static struct mainscan {
80 .std = V4L2_STD_B | V4L2_STD_GH,
84 .std = V4L2_STD_PAL_I,
88 .std = V4L2_STD_DK | V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC,
93 static struct saa7134_tvaudio tvaudio[] = {
95 .name = "PAL-B/G FM-stereo",
96 .std = V4L2_STD_PAL_BG,
97 .mode = TVAUDIO_FM_BG_STEREO,
101 .name = "PAL-D/K1 FM-stereo",
102 .std = V4L2_STD_PAL_DK,
105 .mode = TVAUDIO_FM_BG_STEREO,
107 .name = "PAL-D/K2 FM-stereo",
108 .std = V4L2_STD_PAL_DK,
111 .mode = TVAUDIO_FM_BG_STEREO,
113 .name = "PAL-D/K3 FM-stereo",
114 .std = V4L2_STD_PAL_DK,
117 .mode = TVAUDIO_FM_BG_STEREO,
119 .name = "PAL-B/G NICAM",
120 .std = V4L2_STD_PAL_BG,
123 .mode = TVAUDIO_NICAM_FM,
125 .name = "PAL-I NICAM",
126 .std = V4L2_STD_PAL_I,
129 .mode = TVAUDIO_NICAM_FM,
131 .name = "PAL-D/K NICAM",
132 .std = V4L2_STD_PAL_DK,
135 .mode = TVAUDIO_NICAM_FM,
137 .name = "SECAM-L NICAM",
138 .std = V4L2_STD_SECAM_L,
141 .mode = TVAUDIO_NICAM_AM,
143 .name = "SECAM-D/K NICAM",
144 .std = V4L2_STD_SECAM_DK,
147 .mode = TVAUDIO_NICAM_FM,
149 .name = "NTSC-A2 FM-stereo",
150 .std = V4L2_STD_NTSC,
153 .mode = TVAUDIO_FM_K_STEREO,
156 .std = V4L2_STD_NTSC,
159 .mode = TVAUDIO_FM_MONO,
162 #define TVAUDIO (sizeof(tvaudio)/sizeof(struct saa7134_tvaudio))
164 /* ------------------------------------------------------------------ */
166 static void tvaudio_init(struct saa7134_dev *dev)
168 int clock = saa7134_boards[dev->board].audio_clock;
170 if (UNSET != audio_clock_override)
171 clock = audio_clock_override;
173 /* init all audio registers */
174 saa_writeb(SAA7134_AUDIO_PLL_CTRL, 0x00);
180 saa_writeb(SAA7134_AUDIO_CLOCK0, clock & 0xff);
181 saa_writeb(SAA7134_AUDIO_CLOCK1, (clock >> 8) & 0xff);
182 saa_writeb(SAA7134_AUDIO_CLOCK2, (clock >> 16) & 0xff);
183 /* frame locked audio is mandatory for NICAM */
184 saa_writeb(SAA7134_AUDIO_PLL_CTRL, 0x01);
186 saa_writeb(SAA7134_NICAM_ERROR_LOW, 0x14);
187 saa_writeb(SAA7134_NICAM_ERROR_HIGH, 0x50);
188 saa_writeb(SAA7134_MONITOR_SELECT, 0xa0);
189 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
192 static u32 tvaudio_carr2reg(u32 carrier)
201 static void tvaudio_setcarrier(struct saa7134_dev *dev,
202 int primary, int secondary)
206 saa_writel(SAA7134_CARRIER1_FREQ0 >> 2, tvaudio_carr2reg(primary));
207 saa_writel(SAA7134_CARRIER2_FREQ0 >> 2, tvaudio_carr2reg(secondary));
210 #define SAA7134_MUTE_MASK 0xbb
211 #define SAA7134_MUTE_ANALOG 0x04
212 #define SAA7134_MUTE_I2S 0x40
214 static void mute_input_7134(struct saa7134_dev *dev)
217 struct saa7134_input *in;
218 int ausel=0, ics=0, ocs=0;
221 /* look what is to do ... */
223 mute = (dev->ctl_mute ||
224 (dev->automute && (&card(dev).radio) != in));
225 if (card(dev).mute.name) {
227 * 7130 - we'll mute using some unconnected audio input
228 * 7134 - we'll probably should switch external mux with gpio
231 in = &card(dev).mute;
234 if (dev->hw_mute == mute &&
235 dev->hw_input == in) {
236 dprintk("mute/input: nothing to do [mute=%d,input=%s]\n",
241 dprintk("ctl_mute=%d automute=%d input=%s => mute=%d input=%s\n",
242 dev->ctl_mute,dev->automute,dev->input->name,mute,in->name);
246 if (PCI_DEVICE_ID_PHILIPS_SAA7134 == dev->pci->device)
248 saa_writeb(SAA7134_AUDIO_MUTE_CTRL, mute ?
250 SAA7134_MUTE_ANALOG |
254 /* switch internal audio mux */
256 case TV: ausel=0xc0; ics=0x00; ocs=0x02; break;
257 case LINE1: ausel=0x80; ics=0x00; ocs=0x00; break;
258 case LINE2: ausel=0x80; ics=0x08; ocs=0x01; break;
259 case LINE2_LEFT: ausel=0x80; ics=0x08; ocs=0x05; break;
261 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, ausel);
262 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, ics);
263 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, ocs);
264 // for oss, we need to change the clock configuration
266 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
268 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x01);
270 /* switch gpio-connected external audio mux */
271 if (0 == card(dev).gpiomask)
274 mask = card(dev).gpiomask;
275 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, mask, mask);
276 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio);
277 saa7134_track_gpio(dev,in->name);
280 static void tvaudio_setmode(struct saa7134_dev *dev,
281 struct saa7134_tvaudio *audio,
286 if (dev->tvnorm->id == V4L2_STD_NTSC) {
291 if (audio_clock_tweak > -1024 && audio_clock_tweak < 1024)
292 tweak = audio_clock_tweak;
295 dprintk("tvaudio_setmode: %s %s [%d.%03d/%d.%03d MHz] acpf=%d%+d\n",
297 audio->carr1 / 1000, audio->carr1 % 1000,
298 audio->carr2 / 1000, audio->carr2 % 1000,
302 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD0, (acpf & 0x0000ff) >> 0);
303 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD1, (acpf & 0x00ff00) >> 8);
304 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD2, (acpf & 0x030000) >> 16);
305 tvaudio_setcarrier(dev,audio->carr1,audio->carr2);
307 switch (audio->mode) {
308 case TVAUDIO_FM_MONO:
309 case TVAUDIO_FM_BG_STEREO:
310 saa_writeb(SAA7134_DEMODULATOR, 0x00);
311 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
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_FM_K_STEREO:
317 saa_writeb(SAA7134_DEMODULATOR, 0x00);
318 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x01);
319 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x22);
320 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
321 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa0);
323 case TVAUDIO_NICAM_FM:
324 saa_writeb(SAA7134_DEMODULATOR, 0x10);
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_NICAM_AM:
331 saa_writeb(SAA7134_DEMODULATOR, 0x12);
332 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
333 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x44);
334 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa1);
335 saa_writeb(SAA7134_NICAM_CONFIG, 0x00);
337 case TVAUDIO_FM_SAT_STEREO:
338 /* not implemented (yet) */
343 static int tvaudio_sleep(struct saa7134_dev *dev, int timeout)
345 if (dev->thread.scan1 == dev->thread.scan2 &&
346 !kthread_should_stop()) {
348 set_current_state(TASK_INTERRUPTIBLE);
351 schedule_timeout_interruptible
352 (msecs_to_jiffies(timeout));
355 return dev->thread.scan1 != dev->thread.scan2;
358 static int tvaudio_checkcarrier(struct saa7134_dev *dev, struct mainscan *scan)
360 __s32 left,right,value;
362 if (audio_debug > 1) {
364 dprintk("debug %d:",scan->carr);
365 for (i = -150; i <= 150; i += 30) {
366 tvaudio_setcarrier(dev,scan->carr+i,scan->carr+i);
367 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
368 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
370 value = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
372 printk(" # %6d # ",value >> 16);
374 printk(" %6d",value >> 16);
378 if (dev->tvnorm->id & scan->std) {
379 tvaudio_setcarrier(dev,scan->carr-90,scan->carr-90);
380 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
381 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
383 left = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
385 tvaudio_setcarrier(dev,scan->carr+90,scan->carr+90);
386 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
387 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
389 right = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
393 value = left > right ? left - right : right - left;
394 dprintk("scanning %d.%03d MHz [%4s] => dc is %5d [%d/%d]\n",
395 scan->carr / 1000, scan->carr % 1000,
396 scan->name, value, left, right);
399 dprintk("skipping %d.%03d MHz [%4s]\n",
400 scan->carr / 1000, scan->carr % 1000, scan->name);
406 static int tvaudio_getstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio)
408 __u32 idp, nicam, nicam_status;
411 switch (audio->mode) {
412 case TVAUDIO_FM_MONO:
413 return V4L2_TUNER_SUB_MONO;
414 case TVAUDIO_FM_K_STEREO:
415 case TVAUDIO_FM_BG_STEREO:
416 idp = (saa_readb(SAA7134_IDENT_SIF) & 0xe0) >> 5;
417 dprintk("getstereo: fm/stereo: idp=0x%x\n",idp);
418 if (0x03 == (idp & 0x03))
419 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
420 else if (0x05 == (idp & 0x05))
421 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
422 else if (0x01 == (idp & 0x01))
423 retval = V4L2_TUNER_SUB_MONO;
425 case TVAUDIO_FM_SAT_STEREO:
426 /* not implemented (yet) */
428 case TVAUDIO_NICAM_FM:
429 case TVAUDIO_NICAM_AM:
430 nicam = saa_readb(SAA7134_AUDIO_STATUS);
431 dprintk("getstereo: nicam=0x%x\n",nicam);
433 nicam_status = saa_readb(SAA7134_NICAM_STATUS);
434 dprintk("getstereo: nicam_status=0x%x\n", nicam_status);
436 switch (nicam_status & 0x03) {
438 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
441 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
444 retval = V4L2_TUNER_SUB_MONO;
447 /* No nicam detected */
452 dprintk("found audio subchannels:%s%s%s%s\n",
453 (retval & V4L2_TUNER_SUB_MONO) ? " mono" : "",
454 (retval & V4L2_TUNER_SUB_STEREO) ? " stereo" : "",
455 (retval & V4L2_TUNER_SUB_LANG1) ? " lang1" : "",
456 (retval & V4L2_TUNER_SUB_LANG2) ? " lang2" : "");
460 static int tvaudio_setstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio,
463 static char *name[] = {
464 [ V4L2_TUNER_MODE_MONO ] = "mono",
465 [ V4L2_TUNER_MODE_STEREO ] = "stereo",
466 [ V4L2_TUNER_MODE_LANG1 ] = "lang1",
467 [ V4L2_TUNER_MODE_LANG2 ] = "lang2",
468 [ V4L2_TUNER_MODE_LANG1_LANG2 ] = "lang1+lang2",
471 [ V4L2_TUNER_MODE_MONO ] = 0x00, /* ch1 */
472 [ V4L2_TUNER_MODE_STEREO ] = 0x80, /* auto */
473 [ V4L2_TUNER_MODE_LANG1 ] = 0x00, /* ch1 */
474 [ V4L2_TUNER_MODE_LANG2 ] = 0x01, /* ch2 */
475 [ V4L2_TUNER_MODE_LANG1_LANG2 ] = 0x80, /* auto */
479 switch (audio->mode) {
480 case TVAUDIO_FM_MONO:
481 /* nothing to do ... */
483 case TVAUDIO_FM_K_STEREO:
484 case TVAUDIO_FM_BG_STEREO:
485 case TVAUDIO_NICAM_AM:
486 case TVAUDIO_NICAM_FM:
487 dprintk("setstereo [fm] => %s\n",
488 name[ mode % ARRAY_SIZE(name) ]);
489 reg = fm[ mode % ARRAY_SIZE(fm) ];
490 saa_writeb(SAA7134_FM_DEMATRIX, reg);
492 case TVAUDIO_FM_SAT_STEREO:
493 /* Not implemented */
499 static int tvaudio_thread(void *data)
501 struct saa7134_dev *dev = data;
502 int carr_vals[ARRAY_SIZE(mainscan)];
503 unsigned int i, audio, nscan;
504 int max1,max2,carrier,rx,mode,lastmode,default_carrier;
506 allow_signal(SIGTERM);
508 tvaudio_sleep(dev,-1);
509 if (kthread_should_stop() || signal_pending(current))
513 dev->thread.scan1 = dev->thread.scan2;
514 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
517 if (dev->ctl_automute)
519 mute_input_7134(dev);
521 /* give the tuner some time */
522 if (tvaudio_sleep(dev,SCAN_INITIAL_DELAY))
530 for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
531 if (!(dev->tvnorm->id & mainscan[i].std))
533 if (!default_carrier)
534 default_carrier = mainscan[i].carr;
539 /* only one candidate -- skip scan ;) */
540 dprintk("only one main carrier candidate - skipping scan\n");
542 carrier = default_carrier;
544 /* scan for the main carrier */
545 saa_writeb(SAA7134_MONITOR_SELECT,0x00);
546 tvaudio_setmode(dev,&tvaudio[0],NULL);
547 for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
548 carr_vals[i] = tvaudio_checkcarrier(dev, mainscan+i);
549 if (dev->thread.scan1 != dev->thread.scan2)
552 for (max1 = 0, max2 = 0, i = 0; i < ARRAY_SIZE(mainscan); i++) {
553 if (max1 < carr_vals[i]) {
556 carrier = mainscan[i].carr;
557 } else if (max2 < carr_vals[i]) {
563 if (0 != carrier && max1 > 2000 && max1 > max2*3) {
564 /* found good carrier */
565 dprintk("found %s main sound carrier @ %d.%03d MHz [%d/%d]\n",
566 dev->tvnorm->name, carrier/1000, carrier%1000,
568 dev->last_carrier = carrier;
570 } else if (0 != dev->last_carrier) {
571 /* no carrier -- try last detected one as fallback */
572 carrier = dev->last_carrier;
573 dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
574 "using %d.%03d MHz [last detected]\n",
575 dev->name, carrier/1000, carrier%1000);
578 /* no carrier + no fallback -- use default */
579 carrier = default_carrier;
580 dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
581 "using %d.%03d MHz [default]\n",
582 dev->name, carrier/1000, carrier%1000);
584 tvaudio_setcarrier(dev,carrier,carrier);
586 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x00);
587 saa7134_tvaudio_setmute(dev);
588 /* find the exact tv audio norm */
589 for (audio = UNSET, i = 0; i < TVAUDIO; i++) {
590 if (dev->tvnorm->id != UNSET &&
591 !(dev->tvnorm->id & tvaudio[i].std))
593 if (tvaudio[i].carr1 != carrier)
595 /* Note: at least the primary carrier is right here */
598 tvaudio_setmode(dev,&tvaudio[i],"trying");
599 if (tvaudio_sleep(dev,SCAN_SUBCARRIER_DELAY))
601 if (-1 != tvaudio_getstereo(dev,&tvaudio[i])) {
606 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x30);
609 tvaudio_setmode(dev,&tvaudio[audio],"using");
611 tvaudio_setstereo(dev,&tvaudio[audio],V4L2_TUNER_MODE_MONO);
612 dev->tvaudio = &tvaudio[audio];
616 if (tvaudio_sleep(dev,5000))
618 if (kthread_should_stop() || signal_pending(current))
620 if (UNSET == dev->thread.mode) {
621 rx = tvaudio_getstereo(dev,&tvaudio[i]);
622 mode = saa7134_tvaudio_rx2mode(rx);
624 mode = dev->thread.mode;
626 if (lastmode != mode) {
627 tvaudio_setstereo(dev,&tvaudio[audio],mode);
637 /* ------------------------------------------------------------------ */
638 /* saa7133 / saa7135 code */
640 static char *stdres[0x20] = {
641 [0x00] = "no standard detected",
642 [0x01] = "B/G (in progress)",
643 [0x02] = "D/K (in progress)",
644 [0x03] = "M (in progress)",
647 [0x05] = "B/G NICAM",
648 [0x06] = "D/K A2 (1)",
649 [0x07] = "D/K A2 (2)",
650 [0x08] = "D/K A2 (3)",
651 [0x09] = "D/K NICAM",
659 [0x0f] = "FM radio / IF 10.7 / 50 deemp",
660 [0x10] = "FM radio / IF 10.7 / 75 deemp",
661 [0x11] = "FM radio / IF sel / 50 deemp",
662 [0x12] = "FM radio / IF sel / 75 deemp",
664 [0x13 ... 0x1e ] = "unknown",
665 [0x1f] = "??? [in progress]",
671 static inline int saa_dsp_wait_bit(struct saa7134_dev *dev, int bit)
673 int state, count = DSP_RETRY;
675 state = saa_readb(SAA7135_DSP_RWSTATE);
676 if (unlikely(state & SAA7135_DSP_RWSTATE_ERR)) {
677 printk("%s: dsp access error\n",dev->name);
678 /* FIXME: send ack ... */
681 while (0 == (state & bit)) {
682 if (unlikely(0 == count)) {
683 printk("%s: dsp access wait timeout [bit=%s]\n",
685 (bit & SAA7135_DSP_RWSTATE_WRR) ? "WRR" :
686 (bit & SAA7135_DSP_RWSTATE_RDB) ? "RDB" :
687 (bit & SAA7135_DSP_RWSTATE_IDA) ? "IDA" :
692 state = saa_readb(SAA7135_DSP_RWSTATE);
699 int saa_dsp_writel(struct saa7134_dev *dev, int reg, u32 value)
703 d2printk("dsp write reg 0x%x = 0x%06x\n",reg<<2,value);
704 err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
707 saa_writel(reg,value);
708 err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
714 static int getstereo_7133(struct saa7134_dev *dev)
716 int retval = V4L2_TUNER_SUB_MONO;
719 value = saa_readl(0x528 >> 2);
721 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
723 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
727 static int mute_input_7133(struct saa7134_dev *dev)
732 struct saa7134_input *in;
735 switch (dev->input->amux) {
748 saa_dsp_writel(dev, 0x464 >> 2, xbarin);
754 saa_dsp_writel(dev, 0x46c >> 2, xbarout);
756 saa_writel(0x594 >> 2, reg);
759 /* switch gpio-connected external audio mux */
760 if (0 != card(dev).gpiomask) {
761 mask = card(dev).gpiomask;
763 if (card(dev).mute.name && dev->ctl_mute)
764 in = &card(dev).mute;
768 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, mask, mask);
769 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio);
770 saa7134_track_gpio(dev,in->name);
776 static int tvaudio_thread_ddep(void *data)
778 struct saa7134_dev *dev = data;
779 u32 value, norms, clock;
781 allow_signal(SIGTERM);
783 clock = saa7134_boards[dev->board].audio_clock;
784 if (UNSET != audio_clock_override)
785 clock = audio_clock_override;
786 saa_writel(0x598 >> 2, clock);
789 saa_dsp_writel(dev, 0x474 >> 2, 0x00);
790 saa_dsp_writel(dev, 0x450 >> 2, 0x00);
793 tvaudio_sleep(dev,-1);
794 if (kthread_should_stop() || signal_pending(current))
798 dev->thread.scan1 = dev->thread.scan2;
799 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
801 if (audio_ddep >= 0x04 && audio_ddep <= 0x0e) {
802 /* insmod option override */
803 norms = (audio_ddep << 2) | 0x01;
804 dprintk("ddep override: %s\n",stdres[audio_ddep]);
805 } else if (&card(dev).radio == dev->input) {
806 dprintk("FM Radio\n");
807 if (dev->tuner_type == TUNER_PHILIPS_TDA8290) {
808 norms = (0x11 << 2) | 0x01;
809 saa_dsp_writel(dev, 0x42c >> 2, 0x729555);
811 norms = (0x0f << 2) | 0x01;
814 /* (let chip) scan for sound carrier */
816 if (dev->tvnorm->id & (V4L2_STD_B | V4L2_STD_GH))
818 if (dev->tvnorm->id & V4L2_STD_PAL_I)
820 if (dev->tvnorm->id & V4L2_STD_DK)
822 if (dev->tvnorm->id & V4L2_STD_MN)
824 if (dev->tvnorm->id & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC))
827 norms = 0x7c; /* all */
828 dprintk("scanning:%s%s%s%s%s\n",
829 (norms & 0x04) ? " B/G" : "",
830 (norms & 0x08) ? " D/K" : "",
831 (norms & 0x10) ? " L/L'" : "",
832 (norms & 0x20) ? " I" : "",
833 (norms & 0x40) ? " M" : "");
836 /* kick automatic standard detection */
837 saa_dsp_writel(dev, 0x454 >> 2, 0);
838 saa_dsp_writel(dev, 0x454 >> 2, norms | 0x80);
840 /* setup crossbars */
841 saa_dsp_writel(dev, 0x464 >> 2, 0x000000);
842 saa_dsp_writel(dev, 0x470 >> 2, 0x101010);
844 if (tvaudio_sleep(dev,3000))
846 value = saa_readl(0x528 >> 2) & 0xffffff;
848 dprintk("tvaudio thread status: 0x%x [%s%s%s]\n",
849 value, stdres[value & 0x1f],
850 (value & 0x000020) ? ",stereo" : "",
851 (value & 0x000040) ? ",dual" : "");
852 dprintk("detailed status: "
853 "%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n",
854 (value & 0x000080) ? " A2/EIAJ pilot tone " : "",
855 (value & 0x000100) ? " A2/EIAJ dual " : "",
856 (value & 0x000200) ? " A2/EIAJ stereo " : "",
857 (value & 0x000400) ? " A2/EIAJ noise mute " : "",
859 (value & 0x000800) ? " BTSC/FM radio pilot " : "",
860 (value & 0x001000) ? " SAP carrier " : "",
861 (value & 0x002000) ? " BTSC stereo noise mute " : "",
862 (value & 0x004000) ? " SAP noise mute " : "",
863 (value & 0x008000) ? " VDSP " : "",
865 (value & 0x010000) ? " NICST " : "",
866 (value & 0x020000) ? " NICDU " : "",
867 (value & 0x040000) ? " NICAM muted " : "",
868 (value & 0x080000) ? " NICAM reserve sound " : "",
870 (value & 0x100000) ? " init done " : "");
877 /* ------------------------------------------------------------------ */
878 /* common stuff + external entry points */
880 static void saa7134_enable_i2s(struct saa7134_dev *dev)
884 if (!card_is_empress(dev))
886 i2s_format = (dev->input->amux == TV) ? 0x00 : 0x01;
888 /* enable I2S audio output for the mpeg encoder */
889 saa_writeb(SAA7134_I2S_OUTPUT_SELECT, 0x80);
890 saa_writeb(SAA7134_I2S_OUTPUT_FORMAT, i2s_format);
891 saa_writeb(SAA7134_I2S_OUTPUT_LEVEL, 0x0F);
892 saa_writeb(SAA7134_I2S_AUDIO_OUTPUT, 0x01);
895 int saa7134_tvaudio_rx2mode(u32 rx)
899 mode = V4L2_TUNER_MODE_MONO;
900 if (rx & V4L2_TUNER_SUB_STEREO)
901 mode = V4L2_TUNER_MODE_STEREO;
902 else if (rx & V4L2_TUNER_SUB_LANG1)
903 mode = V4L2_TUNER_MODE_LANG1;
904 else if (rx & V4L2_TUNER_SUB_LANG2)
905 mode = V4L2_TUNER_MODE_LANG2;
909 void saa7134_tvaudio_setmute(struct saa7134_dev *dev)
911 switch (dev->pci->device) {
912 case PCI_DEVICE_ID_PHILIPS_SAA7130:
913 case PCI_DEVICE_ID_PHILIPS_SAA7134:
914 mute_input_7134(dev);
916 case PCI_DEVICE_ID_PHILIPS_SAA7133:
917 case PCI_DEVICE_ID_PHILIPS_SAA7135:
918 mute_input_7133(dev);
923 void saa7134_tvaudio_setinput(struct saa7134_dev *dev,
924 struct saa7134_input *in)
927 switch (dev->pci->device) {
928 case PCI_DEVICE_ID_PHILIPS_SAA7130:
929 case PCI_DEVICE_ID_PHILIPS_SAA7134:
930 mute_input_7134(dev);
932 case PCI_DEVICE_ID_PHILIPS_SAA7133:
933 case PCI_DEVICE_ID_PHILIPS_SAA7135:
934 mute_input_7133(dev);
937 saa7134_enable_i2s(dev);
940 void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level)
942 switch (dev->pci->device) {
943 case PCI_DEVICE_ID_PHILIPS_SAA7134:
944 saa_writeb(SAA7134_CHANNEL1_LEVEL, level & 0x1f);
945 saa_writeb(SAA7134_CHANNEL2_LEVEL, level & 0x1f);
946 saa_writeb(SAA7134_NICAM_LEVEL_ADJUST, level & 0x1f);
951 int saa7134_tvaudio_getstereo(struct saa7134_dev *dev)
953 int retval = V4L2_TUNER_SUB_MONO;
955 switch (dev->pci->device) {
956 case PCI_DEVICE_ID_PHILIPS_SAA7134:
958 retval = tvaudio_getstereo(dev,dev->tvaudio);
960 case PCI_DEVICE_ID_PHILIPS_SAA7133:
961 case PCI_DEVICE_ID_PHILIPS_SAA7135:
962 retval = getstereo_7133(dev);
968 int saa7134_tvaudio_init2(struct saa7134_dev *dev)
970 int (*my_thread)(void *data) = NULL;
972 switch (dev->pci->device) {
973 case PCI_DEVICE_ID_PHILIPS_SAA7134:
974 my_thread = tvaudio_thread;
976 case PCI_DEVICE_ID_PHILIPS_SAA7133:
977 case PCI_DEVICE_ID_PHILIPS_SAA7135:
978 my_thread = tvaudio_thread_ddep;
982 dev->thread.thread = NULL;
984 /* start tvaudio thread */
985 dev->thread.thread = kthread_run(my_thread, dev, "%s", dev->name);
986 if (IS_ERR(dev->thread.thread)) {
987 printk(KERN_WARNING "%s: kernel_thread() failed\n",
989 /* XXX: missing error handling here */
991 saa7134_tvaudio_do_scan(dev);
994 saa7134_enable_i2s(dev);
998 int saa7134_tvaudio_fini(struct saa7134_dev *dev)
1000 /* shutdown tvaudio thread */
1001 if (dev->thread.thread)
1002 kthread_stop(dev->thread.thread);
1004 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, 0x00); /* LINE1 */
1008 int saa7134_tvaudio_do_scan(struct saa7134_dev *dev)
1010 if (dev->input->amux != TV) {
1011 dprintk("sound IF not in use, skipping scan\n");
1013 saa7134_tvaudio_setmute(dev);
1014 } else if (dev->thread.thread) {
1015 dev->thread.mode = UNSET;
1016 dev->thread.scan2++;
1017 wake_up_process(dev->thread.thread);
1020 saa7134_tvaudio_setmute(dev);
1025 EXPORT_SYMBOL(saa_dsp_writel);
1026 EXPORT_SYMBOL(saa7134_tvaudio_setmute);
1028 /* ----------------------------------------------------------- */