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/slab.h>
29 #include <linux/delay.h>
30 #include <linux/smp_lock.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 {
76 .std = V4L2_STD_NTSC | V4L2_STD_PAL_M,
80 .std = V4L2_STD_PAL_BG,
84 .std = V4L2_STD_PAL_I,
88 .std = V4L2_STD_PAL_DK | V4L2_STD_SECAM,
93 static struct saa7134_tvaudio tvaudio[] = {
95 .name = "PAL-B/G FM-stereo",
97 .mode = TVAUDIO_FM_BG_STEREO,
101 .name = "PAL-D/K1 FM-stereo",
105 .mode = TVAUDIO_FM_BG_STEREO,
107 .name = "PAL-D/K2 FM-stereo",
111 .mode = TVAUDIO_FM_BG_STEREO,
113 .name = "PAL-D/K3 FM-stereo",
117 .mode = TVAUDIO_FM_BG_STEREO,
119 .name = "PAL-B/G NICAM",
123 .mode = TVAUDIO_NICAM_FM,
125 .name = "PAL-I NICAM",
129 .mode = TVAUDIO_NICAM_FM,
131 .name = "PAL-D/K NICAM",
135 .mode = TVAUDIO_NICAM_FM,
137 .name = "SECAM-L NICAM",
138 .std = V4L2_STD_SECAM,
141 .mode = TVAUDIO_NICAM_AM,
144 .std = V4L2_STD_SECAM,
147 .mode = TVAUDIO_FM_MONO,
150 .std = V4L2_STD_NTSC,
153 .mode = TVAUDIO_FM_MONO,
155 .name = "NTSC-A2 FM-stereo",
156 .std = V4L2_STD_NTSC,
159 .mode = TVAUDIO_FM_K_STEREO,
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 was reported not to be reliable
184 saa_writeb(SAA7134_AUDIO_PLL_CTRL, 0x02);
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 static void mute_input_7134(struct saa7134_dev *dev)
213 struct saa7134_input *in;
214 int ausel=0, ics=0, ocs=0;
217 /* look what is to do ... */
219 mute = (dev->ctl_mute ||
220 (dev->automute && (&card(dev).radio) != in));
221 if (card(dev).mute.name) {
223 * 7130 - we'll mute using some unconnected audio input
224 * 7134 - we'll probably should switch external mux with gpio
227 in = &card(dev).mute;
230 if (dev->hw_mute == mute &&
231 dev->hw_input == in) {
232 dprintk("mute/input: nothing to do [mute=%d,input=%s]\n",
237 dprintk("ctl_mute=%d automute=%d input=%s => mute=%d input=%s\n",
238 dev->ctl_mute,dev->automute,dev->input->name,mute,in->name);
242 if (PCI_DEVICE_ID_PHILIPS_SAA7134 == dev->pci->device)
244 saa_writeb(SAA7134_AUDIO_MUTE_CTRL, mute ? 0xbf : 0xbb);
246 /* switch internal audio mux */
248 case TV: ausel=0xc0; ics=0x00; ocs=0x02; break;
249 case LINE1: ausel=0x80; ics=0x00; ocs=0x00; break;
250 case LINE2: ausel=0x80; ics=0x08; ocs=0x01; break;
251 case LINE2_LEFT: ausel=0x80; ics=0x08; ocs=0x05; break;
253 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, ausel);
254 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, ics);
255 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, ocs);
256 // for oss, we need to change the clock configuration
258 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
260 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x01);
262 /* switch gpio-connected external audio mux */
263 if (0 == card(dev).gpiomask)
266 mask = card(dev).gpiomask;
267 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, mask, mask);
268 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio);
269 saa7134_track_gpio(dev,in->name);
272 static void tvaudio_setmode(struct saa7134_dev *dev,
273 struct saa7134_tvaudio *audio,
278 if (dev->tvnorm->id == V4L2_STD_NTSC) {
283 if (audio_clock_tweak > -1024 && audio_clock_tweak < 1024)
284 tweak = audio_clock_tweak;
287 dprintk("tvaudio_setmode: %s %s [%d.%03d/%d.%03d MHz] acpf=%d%+d\n",
289 audio->carr1 / 1000, audio->carr1 % 1000,
290 audio->carr2 / 1000, audio->carr2 % 1000,
294 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD0, (acpf & 0x0000ff) >> 0);
295 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD1, (acpf & 0x00ff00) >> 8);
296 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD2, (acpf & 0x030000) >> 16);
297 tvaudio_setcarrier(dev,audio->carr1,audio->carr2);
299 switch (audio->mode) {
300 case TVAUDIO_FM_MONO:
301 case TVAUDIO_FM_BG_STEREO:
302 saa_writeb(SAA7134_DEMODULATOR, 0x00);
303 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
304 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x22);
305 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
306 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa0);
308 case TVAUDIO_FM_K_STEREO:
309 saa_writeb(SAA7134_DEMODULATOR, 0x00);
310 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x01);
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_NICAM_FM:
316 saa_writeb(SAA7134_DEMODULATOR, 0x10);
317 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
318 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x44);
319 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa1);
320 saa_writeb(SAA7134_NICAM_CONFIG, 0x00);
322 case TVAUDIO_NICAM_AM:
323 saa_writeb(SAA7134_DEMODULATOR, 0x12);
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_FM_SAT_STEREO:
330 /* not implemented (yet) */
335 static int tvaudio_sleep(struct saa7134_dev *dev, int timeout)
337 DECLARE_WAITQUEUE(wait, current);
339 add_wait_queue(&dev->thread.wq, &wait);
340 if (dev->thread.scan1 == dev->thread.scan2 && !dev->thread.shutdown) {
342 set_current_state(TASK_INTERRUPTIBLE);
345 set_current_state(TASK_INTERRUPTIBLE);
346 schedule_timeout(msecs_to_jiffies(timeout));
349 remove_wait_queue(&dev->thread.wq, &wait);
350 return dev->thread.scan1 != dev->thread.scan2;
353 static int tvaudio_checkcarrier(struct saa7134_dev *dev, struct mainscan *scan)
355 __s32 left,right,value;
357 if (audio_debug > 1) {
359 dprintk("debug %d:",scan->carr);
360 for (i = -150; i <= 150; i += 30) {
361 tvaudio_setcarrier(dev,scan->carr+i,scan->carr+i);
362 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
363 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
365 value = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
367 printk(" # %6d # ",value >> 16);
369 printk(" %6d",value >> 16);
374 if (dev->tvnorm->id & scan->std) {
375 tvaudio_setcarrier(dev,scan->carr-90,scan->carr-90);
376 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
377 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
379 left = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
381 tvaudio_setcarrier(dev,scan->carr+90,scan->carr+90);
382 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
383 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
385 right = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
389 value = left > right ? left - right : right - left;
390 dprintk("scanning %d.%03d MHz [%4s] => dc is %5d [%d/%d]\n",
391 scan->carr / 1000, scan->carr % 1000,
392 scan->name, value, left, right);
395 dprintk("skipping %d.%03d MHz [%4s]\n",
396 scan->carr / 1000, scan->carr % 1000, scan->name);
402 static int tvaudio_getstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio)
404 __u32 idp, nicam, nicam_status;
407 switch (audio->mode) {
408 case TVAUDIO_FM_MONO:
409 return V4L2_TUNER_SUB_MONO;
410 case TVAUDIO_FM_K_STEREO:
411 case TVAUDIO_FM_BG_STEREO:
412 idp = (saa_readb(SAA7134_IDENT_SIF) & 0xe0) >> 5;
413 dprintk("getstereo: fm/stereo: idp=0x%x\n",idp);
414 if (0x03 == (idp & 0x03))
415 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
416 else if (0x05 == (idp & 0x05))
417 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
418 else if (0x01 == (idp & 0x01))
419 retval = V4L2_TUNER_SUB_MONO;
421 case TVAUDIO_FM_SAT_STEREO:
422 /* not implemented (yet) */
424 case TVAUDIO_NICAM_FM:
425 case TVAUDIO_NICAM_AM:
426 nicam = saa_readb(SAA7134_AUDIO_STATUS);
427 dprintk("getstereo: nicam=0x%x\n",nicam);
429 nicam_status = saa_readb(SAA7134_NICAM_STATUS);
430 dprintk("getstereo: nicam_status=0x%x\n", nicam_status);
432 switch (nicam_status & 0x03) {
434 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
437 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
440 retval = V4L2_TUNER_SUB_MONO;
443 /* No nicam detected */
448 dprintk("found audio subchannels:%s%s%s%s\n",
449 (retval & V4L2_TUNER_SUB_MONO) ? " mono" : "",
450 (retval & V4L2_TUNER_SUB_STEREO) ? " stereo" : "",
451 (retval & V4L2_TUNER_SUB_LANG1) ? " lang1" : "",
452 (retval & V4L2_TUNER_SUB_LANG2) ? " lang2" : "");
456 static int tvaudio_setstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio,
459 static char *name[] = {
460 [ V4L2_TUNER_MODE_MONO ] = "mono",
461 [ V4L2_TUNER_MODE_STEREO ] = "stereo",
462 [ V4L2_TUNER_MODE_LANG1 ] = "lang1",
463 [ V4L2_TUNER_MODE_LANG2 ] = "lang2",
466 [ V4L2_TUNER_MODE_MONO ] = 0x00, /* ch1 */
467 [ V4L2_TUNER_MODE_STEREO ] = 0x80, /* auto */
468 [ V4L2_TUNER_MODE_LANG1 ] = 0x00, /* ch1 */
469 [ V4L2_TUNER_MODE_LANG2 ] = 0x01, /* ch2 */
473 switch (audio->mode) {
474 case TVAUDIO_FM_MONO:
475 /* nothing to do ... */
477 case TVAUDIO_FM_K_STEREO:
478 case TVAUDIO_FM_BG_STEREO:
479 case TVAUDIO_NICAM_AM:
480 case TVAUDIO_NICAM_FM:
481 dprintk("setstereo [fm] => %s\n",
482 name[ mode % ARRAY_SIZE(name) ]);
483 reg = fm[ mode % ARRAY_SIZE(fm) ];
484 saa_writeb(SAA7134_FM_DEMATRIX, reg);
486 case TVAUDIO_FM_SAT_STEREO:
487 /* Not implemented */
493 static int tvaudio_thread(void *data)
495 struct saa7134_dev *dev = data;
496 int carr_vals[ARRAY_SIZE(mainscan)];
497 unsigned int i, audio, nscan;
498 int max1,max2,carrier,rx,mode,lastmode,default_carrier;
500 daemonize("%s", dev->name);
501 allow_signal(SIGTERM);
503 tvaudio_sleep(dev,-1);
504 if (dev->thread.shutdown || signal_pending(current))
508 dev->thread.scan1 = dev->thread.scan2;
509 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
512 if (dev->ctl_automute)
514 mute_input_7134(dev);
516 /* give the tuner some time */
517 if (tvaudio_sleep(dev,SCAN_INITIAL_DELAY))
525 for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
526 if (!(dev->tvnorm->id & mainscan[i].std))
528 if (!default_carrier)
529 default_carrier = mainscan[i].carr;
534 /* only one candidate -- skip scan ;) */
536 carrier = default_carrier;
538 /* scan for the main carrier */
539 saa_writeb(SAA7134_MONITOR_SELECT,0x00);
540 tvaudio_setmode(dev,&tvaudio[0],NULL);
541 for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
542 carr_vals[i] = tvaudio_checkcarrier(dev, mainscan+i);
543 if (dev->thread.scan1 != dev->thread.scan2)
546 for (max1 = 0, max2 = 0, i = 0; i < ARRAY_SIZE(mainscan); i++) {
547 if (max1 < carr_vals[i]) {
550 carrier = mainscan[i].carr;
551 } else if (max2 < carr_vals[i]) {
557 if (0 != carrier && max1 > 2000 && max1 > max2*3) {
558 /* found good carrier */
559 dprintk("found %s main sound carrier @ %d.%03d MHz [%d/%d]\n",
560 dev->tvnorm->name, carrier/1000, carrier%1000,
562 dev->last_carrier = carrier;
564 } else if (0 != dev->last_carrier) {
565 /* no carrier -- try last detected one as fallback */
566 carrier = dev->last_carrier;
567 dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
568 "using %d.%03d MHz [last detected]\n",
569 dev->name, carrier/1000, carrier%1000);
572 /* no carrier + no fallback -- use default */
573 carrier = default_carrier;
574 dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
575 "using %d.%03d MHz [default]\n",
576 dev->name, carrier/1000, carrier%1000);
578 tvaudio_setcarrier(dev,carrier,carrier);
580 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x00);
581 saa7134_tvaudio_setmute(dev);
583 /* find the exact tv audio norm */
584 for (audio = UNSET, i = 0; i < TVAUDIO; i++) {
585 if (dev->tvnorm->id != UNSET &&
586 !(dev->tvnorm->id & tvaudio[i].std))
588 if (tvaudio[i].carr1 != carrier)
593 tvaudio_setmode(dev,&tvaudio[i],"trying");
594 if (tvaudio_sleep(dev,SCAN_SUBCARRIER_DELAY))
596 if (-1 != tvaudio_getstereo(dev,&tvaudio[i])) {
601 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x30);
604 tvaudio_setmode(dev,&tvaudio[audio],"using");
605 tvaudio_setstereo(dev,&tvaudio[audio],V4L2_TUNER_MODE_MONO);
606 dev->tvaudio = &tvaudio[audio];
610 if (tvaudio_sleep(dev,5000))
612 if (dev->thread.shutdown || signal_pending(current))
614 if (UNSET == dev->thread.mode) {
615 rx = tvaudio_getstereo(dev,&tvaudio[i]);
616 mode = saa7134_tvaudio_rx2mode(rx);
618 mode = dev->thread.mode;
620 if (lastmode != mode) {
621 tvaudio_setstereo(dev,&tvaudio[audio],mode);
628 complete_and_exit(&dev->thread.exit, 0);
632 /* ------------------------------------------------------------------ */
633 /* saa7133 / saa7135 code */
635 static char *stdres[0x20] = {
636 [0x00] = "no standard detected",
637 [0x01] = "B/G (in progress)",
638 [0x02] = "D/K (in progress)",
639 [0x03] = "M (in progress)",
642 [0x05] = "B/G NICAM",
643 [0x06] = "D/K A2 (1)",
644 [0x07] = "D/K A2 (2)",
645 [0x08] = "D/K A2 (3)",
646 [0x09] = "D/K NICAM",
654 [0x0f] = "FM radio / IF 10.7 / 50 deemp",
655 [0x10] = "FM radio / IF 10.7 / 75 deemp",
656 [0x11] = "FM radio / IF sel / 50 deemp",
657 [0x12] = "FM radio / IF sel / 75 deemp",
659 [0x13 ... 0x1e ] = "unknown",
660 [0x1f] = "??? [in progress]",
666 static inline int saa_dsp_wait_bit(struct saa7134_dev *dev, int bit)
668 int state, count = DSP_RETRY;
670 state = saa_readb(SAA7135_DSP_RWSTATE);
671 if (unlikely(state & SAA7135_DSP_RWSTATE_ERR)) {
672 printk("%s: dsp access error\n",dev->name);
673 /* FIXME: send ack ... */
676 while (0 == (state & bit)) {
677 if (unlikely(0 == count)) {
678 printk("%s: dsp access wait timeout [bit=%s]\n",
680 (bit & SAA7135_DSP_RWSTATE_WRR) ? "WRR" :
681 (bit & SAA7135_DSP_RWSTATE_RDB) ? "RDB" :
682 (bit & SAA7135_DSP_RWSTATE_IDA) ? "IDA" :
687 state = saa_readb(SAA7135_DSP_RWSTATE);
694 int saa_dsp_writel(struct saa7134_dev *dev, int reg, u32 value)
698 d2printk("dsp write reg 0x%x = 0x%06x\n",reg<<2,value);
699 err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
702 saa_writel(reg,value);
703 err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
709 static int getstereo_7133(struct saa7134_dev *dev)
711 int retval = V4L2_TUNER_SUB_MONO;
714 value = saa_readl(0x528 >> 2);
716 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
718 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
722 static int mute_input_7133(struct saa7134_dev *dev)
727 struct saa7134_input *in;
729 /* Hac 0506 route OSS sound simultanously */
731 switch (dev->input->amux) {
744 saa_dsp_writel(dev, 0x464 >> 2, xbarin);
750 saa_dsp_writel(dev, 0x46c >> 2, xbarout);
752 saa_writel(0x594 >> 2, reg);
755 /* switch gpio-connected external audio mux */
756 if (0 != card(dev).gpiomask) {
757 mask = card(dev).gpiomask;
759 if (card(dev).mute.name && dev->ctl_mute)
760 in = &card(dev).mute;
764 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, mask, mask);
765 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio);
766 saa7134_track_gpio(dev,in->name);
772 static int tvaudio_thread_ddep(void *data)
774 struct saa7134_dev *dev = data;
775 u32 value, norms, clock;
777 daemonize("%s", dev->name);
778 allow_signal(SIGTERM);
780 clock = saa7134_boards[dev->board].audio_clock;
781 if (UNSET != audio_clock_override)
782 clock = audio_clock_override;
783 saa_writel(0x598 >> 2, clock);
786 saa_dsp_writel(dev, 0x474 >> 2, 0x00);
787 saa_dsp_writel(dev, 0x450 >> 2, 0x00);
790 tvaudio_sleep(dev,-1);
791 if (dev->thread.shutdown || signal_pending(current))
795 dev->thread.scan1 = dev->thread.scan2;
796 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
798 if (audio_ddep >= 0x04 && audio_ddep <= 0x0e) {
799 /* insmod option override */
800 norms = (audio_ddep << 2) | 0x01;
801 dprintk("ddep override: %s\n",stdres[audio_ddep]);
802 } else if (&card(dev).radio == dev->input) {
803 dprintk("FM Radio\n");
804 norms = (0x0f << 2) | 0x01;
806 /* (let chip) scan for sound carrier */
808 if (dev->tvnorm->id & V4L2_STD_PAL) {
809 dprintk("PAL scan\n");
810 norms |= 0x2c; /* B/G + D/K + I */
812 if (dev->tvnorm->id & V4L2_STD_NTSC) {
813 dprintk("NTSC scan\n");
814 norms |= 0x40; /* M */
816 if (dev->tvnorm->id & V4L2_STD_SECAM) {
817 dprintk("SECAM scan\n");
818 norms |= 0x18; /* L + D/K */
821 norms = 0x7c; /* all */
822 dprintk("scanning:%s%s%s%s%s\n",
823 (norms & 0x04) ? " B/G" : "",
824 (norms & 0x08) ? " D/K" : "",
825 (norms & 0x10) ? " L/L'" : "",
826 (norms & 0x20) ? " I" : "",
827 (norms & 0x40) ? " M" : "");
830 /* kick automatic standard detection */
831 saa_dsp_writel(dev, 0x454 >> 2, 0);
832 saa_dsp_writel(dev, 0x454 >> 2, norms | 0x80);
834 /* setup crossbars */
835 saa_dsp_writel(dev, 0x464 >> 2, 0x000000);
836 saa_dsp_writel(dev, 0x470 >> 2, 0x101010);
838 if (tvaudio_sleep(dev,3000))
840 value = saa_readl(0x528 >> 2) & 0xffffff;
842 dprintk("tvaudio thread status: 0x%x [%s%s%s]\n",
843 value, stdres[value & 0x1f],
844 (value & 0x000020) ? ",stereo" : "",
845 (value & 0x000040) ? ",dual" : "");
846 dprintk("detailed status: "
847 "%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n",
848 (value & 0x000080) ? " A2/EIAJ pilot tone " : "",
849 (value & 0x000100) ? " A2/EIAJ dual " : "",
850 (value & 0x000200) ? " A2/EIAJ stereo " : "",
851 (value & 0x000400) ? " A2/EIAJ noise mute " : "",
853 (value & 0x000800) ? " BTSC/FM radio pilot " : "",
854 (value & 0x001000) ? " SAP carrier " : "",
855 (value & 0x002000) ? " BTSC stereo noise mute " : "",
856 (value & 0x004000) ? " SAP noise mute " : "",
857 (value & 0x008000) ? " VDSP " : "",
859 (value & 0x010000) ? " NICST " : "",
860 (value & 0x020000) ? " NICDU " : "",
861 (value & 0x040000) ? " NICAM muted " : "",
862 (value & 0x080000) ? " NICAM reserve sound " : "",
864 (value & 0x100000) ? " init done " : "");
868 complete_and_exit(&dev->thread.exit, 0);
872 /* ------------------------------------------------------------------ */
873 /* common stuff + external entry points */
875 static void saa7134_enable_i2s(struct saa7134_dev *dev)
879 if (!card_is_empress(dev))
881 i2s_format = (dev->input->amux == TV) ? 0x00 : 0x01;
883 /* enable I2S audio output for the mpeg encoder */
884 saa_writeb(SAA7134_I2S_OUTPUT_SELECT, 0x80);
885 saa_writeb(SAA7134_I2S_OUTPUT_FORMAT, i2s_format);
886 saa_writeb(SAA7134_I2S_OUTPUT_LEVEL, 0x0F);
887 saa_writeb(SAA7134_I2S_AUDIO_OUTPUT, 0x01);
890 int saa7134_tvaudio_rx2mode(u32 rx)
894 mode = V4L2_TUNER_MODE_MONO;
895 if (rx & V4L2_TUNER_SUB_STEREO)
896 mode = V4L2_TUNER_MODE_STEREO;
897 else if (rx & V4L2_TUNER_SUB_LANG1)
898 mode = V4L2_TUNER_MODE_LANG1;
899 else if (rx & V4L2_TUNER_SUB_LANG2)
900 mode = V4L2_TUNER_MODE_LANG2;
904 void saa7134_tvaudio_setmute(struct saa7134_dev *dev)
906 switch (dev->pci->device) {
907 case PCI_DEVICE_ID_PHILIPS_SAA7130:
908 case PCI_DEVICE_ID_PHILIPS_SAA7134:
909 mute_input_7134(dev);
911 case PCI_DEVICE_ID_PHILIPS_SAA7133:
912 case PCI_DEVICE_ID_PHILIPS_SAA7135:
913 mute_input_7133(dev);
918 void saa7134_tvaudio_setinput(struct saa7134_dev *dev,
919 struct saa7134_input *in)
922 switch (dev->pci->device) {
923 case PCI_DEVICE_ID_PHILIPS_SAA7130:
924 case PCI_DEVICE_ID_PHILIPS_SAA7134:
925 mute_input_7134(dev);
927 case PCI_DEVICE_ID_PHILIPS_SAA7133:
928 case PCI_DEVICE_ID_PHILIPS_SAA7135:
929 mute_input_7133(dev);
932 saa7134_enable_i2s(dev);
935 void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level)
937 switch (dev->pci->device) {
938 case PCI_DEVICE_ID_PHILIPS_SAA7134:
939 saa_writeb(SAA7134_CHANNEL1_LEVEL, level & 0x1f);
940 saa_writeb(SAA7134_CHANNEL2_LEVEL, level & 0x1f);
941 saa_writeb(SAA7134_NICAM_LEVEL_ADJUST, level & 0x1f);
946 int saa7134_tvaudio_getstereo(struct saa7134_dev *dev)
948 int retval = V4L2_TUNER_SUB_MONO;
950 switch (dev->pci->device) {
951 case PCI_DEVICE_ID_PHILIPS_SAA7134:
953 retval = tvaudio_getstereo(dev,dev->tvaudio);
955 case PCI_DEVICE_ID_PHILIPS_SAA7133:
956 case PCI_DEVICE_ID_PHILIPS_SAA7135:
957 retval = getstereo_7133(dev);
963 int saa7134_tvaudio_init2(struct saa7134_dev *dev)
965 DECLARE_MUTEX_LOCKED(sem);
966 int (*my_thread)(void *data) = NULL;
968 switch (dev->pci->device) {
969 case PCI_DEVICE_ID_PHILIPS_SAA7134:
970 my_thread = tvaudio_thread;
972 case PCI_DEVICE_ID_PHILIPS_SAA7133:
973 case PCI_DEVICE_ID_PHILIPS_SAA7135:
974 my_thread = tvaudio_thread_ddep;
978 dev->thread.pid = -1;
980 /* start tvaudio thread */
981 init_waitqueue_head(&dev->thread.wq);
982 init_completion(&dev->thread.exit);
983 dev->thread.pid = kernel_thread(my_thread,dev,0);
984 if (dev->thread.pid < 0)
985 printk(KERN_WARNING "%s: kernel_thread() failed\n",
987 saa7134_tvaudio_do_scan(dev);
990 saa7134_enable_i2s(dev);
994 int saa7134_tvaudio_fini(struct saa7134_dev *dev)
996 /* shutdown tvaudio thread */
997 if (dev->thread.pid >= 0) {
998 dev->thread.shutdown = 1;
999 wake_up_interruptible(&dev->thread.wq);
1000 wait_for_completion(&dev->thread.exit);
1002 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, 0x00); /* LINE1 */
1006 int saa7134_tvaudio_do_scan(struct saa7134_dev *dev)
1008 if (dev->thread.pid >= 0) {
1009 dev->thread.mode = UNSET;
1010 dev->thread.scan2++;
1011 wake_up_interruptible(&dev->thread.wq);
1014 saa7134_tvaudio_setmute(dev);
1019 /* ----------------------------------------------------------- */