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,
143 .name = "SECAM-L MONO",
144 .std = V4L2_STD_SECAM,
147 .mode = TVAUDIO_AM_MONO,
150 .std = V4L2_STD_SECAM,
153 .mode = TVAUDIO_FM_MONO,
156 .std = V4L2_STD_NTSC,
159 .mode = TVAUDIO_FM_MONO,
161 .name = "NTSC-A2 FM-stereo",
162 .std = V4L2_STD_NTSC,
165 .mode = TVAUDIO_FM_K_STEREO,
168 #define TVAUDIO (sizeof(tvaudio)/sizeof(struct saa7134_tvaudio))
170 /* ------------------------------------------------------------------ */
172 static void tvaudio_init(struct saa7134_dev *dev)
174 int clock = saa7134_boards[dev->board].audio_clock;
176 if (UNSET != audio_clock_override)
177 clock = audio_clock_override;
179 /* init all audio registers */
180 saa_writeb(SAA7134_AUDIO_PLL_CTRL, 0x00);
186 saa_writeb(SAA7134_AUDIO_CLOCK0, clock & 0xff);
187 saa_writeb(SAA7134_AUDIO_CLOCK1, (clock >> 8) & 0xff);
188 saa_writeb(SAA7134_AUDIO_CLOCK2, (clock >> 16) & 0xff);
189 /* frame locked audio is mandatory for NICAM */
190 saa_writeb(SAA7134_AUDIO_PLL_CTRL, 0x01);
192 saa_writeb(SAA7134_NICAM_ERROR_LOW, 0x14);
193 saa_writeb(SAA7134_NICAM_ERROR_HIGH, 0x50);
194 saa_writeb(SAA7134_MONITOR_SELECT, 0xa0);
195 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
198 static u32 tvaudio_carr2reg(u32 carrier)
207 static void tvaudio_setcarrier(struct saa7134_dev *dev,
208 int primary, int secondary)
212 saa_writel(SAA7134_CARRIER1_FREQ0 >> 2, tvaudio_carr2reg(primary));
213 saa_writel(SAA7134_CARRIER2_FREQ0 >> 2, tvaudio_carr2reg(secondary));
216 #define SAA7134_MUTE_MASK 0xbb
217 #define SAA7134_MUTE_ANALOG 0x04
218 #define SAA7134_MUTE_I2S 0x40
220 static void mute_input_7134(struct saa7134_dev *dev)
223 struct saa7134_input *in;
224 int ausel=0, ics=0, ocs=0;
227 /* look what is to do ... */
229 mute = (dev->ctl_mute ||
230 (dev->automute && (&card(dev).radio) != in));
231 if (card(dev).mute.name) {
233 * 7130 - we'll mute using some unconnected audio input
234 * 7134 - we'll probably should switch external mux with gpio
237 in = &card(dev).mute;
240 if (dev->hw_mute == mute &&
241 dev->hw_input == in) {
242 dprintk("mute/input: nothing to do [mute=%d,input=%s]\n",
247 dprintk("ctl_mute=%d automute=%d input=%s => mute=%d input=%s\n",
248 dev->ctl_mute,dev->automute,dev->input->name,mute,in->name);
252 if (PCI_DEVICE_ID_PHILIPS_SAA7134 == dev->pci->device)
254 saa_writeb(SAA7134_AUDIO_MUTE_CTRL, mute ?
256 SAA7134_MUTE_ANALOG |
260 /* switch internal audio mux */
262 case TV: ausel=0xc0; ics=0x00; ocs=0x02; break;
263 case LINE1: ausel=0x80; ics=0x00; ocs=0x00; break;
264 case LINE2: ausel=0x80; ics=0x08; ocs=0x01; break;
265 case LINE2_LEFT: ausel=0x80; ics=0x08; ocs=0x05; break;
267 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, ausel);
268 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, ics);
269 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, ocs);
270 // for oss, we need to change the clock configuration
272 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
274 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x01);
276 /* switch gpio-connected external audio mux */
277 if (0 == card(dev).gpiomask)
280 mask = card(dev).gpiomask;
281 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, mask, mask);
282 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio);
283 saa7134_track_gpio(dev,in->name);
286 static void tvaudio_setmode(struct saa7134_dev *dev,
287 struct saa7134_tvaudio *audio,
292 if (dev->tvnorm->id == V4L2_STD_NTSC) {
297 if (audio_clock_tweak > -1024 && audio_clock_tweak < 1024)
298 tweak = audio_clock_tweak;
301 dprintk("tvaudio_setmode: %s %s [%d.%03d/%d.%03d MHz] acpf=%d%+d\n",
303 audio->carr1 / 1000, audio->carr1 % 1000,
304 audio->carr2 / 1000, audio->carr2 % 1000,
308 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD0, (acpf & 0x0000ff) >> 0);
309 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD1, (acpf & 0x00ff00) >> 8);
310 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD2, (acpf & 0x030000) >> 16);
311 tvaudio_setcarrier(dev,audio->carr1,audio->carr2);
313 switch (audio->mode) {
314 case TVAUDIO_FM_MONO:
315 case TVAUDIO_FM_BG_STEREO:
316 saa_writeb(SAA7134_DEMODULATOR, 0x00);
317 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
318 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x22);
319 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
320 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa0);
322 case TVAUDIO_FM_K_STEREO:
323 saa_writeb(SAA7134_DEMODULATOR, 0x00);
324 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x01);
325 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x22);
326 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
327 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa0);
329 case TVAUDIO_NICAM_FM:
330 saa_writeb(SAA7134_DEMODULATOR, 0x10);
331 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
332 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x44);
333 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa1);
334 saa_writeb(SAA7134_NICAM_CONFIG, 0x00);
336 case TVAUDIO_NICAM_AM:
337 saa_writeb(SAA7134_DEMODULATOR, 0x12);
338 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
339 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x44);
340 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa1);
341 saa_writeb(SAA7134_NICAM_CONFIG, 0x00);
343 case TVAUDIO_AM_MONO:
344 saa_writeb(SAA7134_DEMODULATOR, 0x12);
345 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
346 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x44);
347 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa0);
349 case TVAUDIO_FM_SAT_STEREO:
350 /* not implemented (yet) */
355 static int tvaudio_sleep(struct saa7134_dev *dev, int timeout)
357 DECLARE_WAITQUEUE(wait, current);
359 add_wait_queue(&dev->thread.wq, &wait);
360 if (dev->thread.scan1 == dev->thread.scan2 && !dev->thread.shutdown) {
362 set_current_state(TASK_INTERRUPTIBLE);
365 schedule_timeout_interruptible
366 (msecs_to_jiffies(timeout));
369 remove_wait_queue(&dev->thread.wq, &wait);
370 return dev->thread.scan1 != dev->thread.scan2;
373 static int tvaudio_checkcarrier(struct saa7134_dev *dev, struct mainscan *scan)
375 __s32 left,right,value;
377 if (audio_debug > 1) {
379 dprintk("debug %d:",scan->carr);
380 for (i = -150; i <= 150; i += 30) {
381 tvaudio_setcarrier(dev,scan->carr+i,scan->carr+i);
382 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
383 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
385 value = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
387 printk(" # %6d # ",value >> 16);
389 printk(" %6d",value >> 16);
394 if (dev->tvnorm->id & scan->std) {
395 tvaudio_setcarrier(dev,scan->carr-90,scan->carr-90);
396 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
397 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
399 left = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
401 tvaudio_setcarrier(dev,scan->carr+90,scan->carr+90);
402 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
403 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
405 right = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
409 value = left > right ? left - right : right - left;
410 dprintk("scanning %d.%03d MHz [%4s] => dc is %5d [%d/%d]\n",
411 scan->carr / 1000, scan->carr % 1000,
412 scan->name, value, left, right);
415 dprintk("skipping %d.%03d MHz [%4s]\n",
416 scan->carr / 1000, scan->carr % 1000, scan->name);
422 static int tvaudio_getstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio)
424 __u32 idp, nicam, nicam_status;
427 switch (audio->mode) {
428 case TVAUDIO_FM_MONO:
429 case TVAUDIO_AM_MONO:
430 return V4L2_TUNER_SUB_MONO;
431 case TVAUDIO_FM_K_STEREO:
432 case TVAUDIO_FM_BG_STEREO:
433 idp = (saa_readb(SAA7134_IDENT_SIF) & 0xe0) >> 5;
434 dprintk("getstereo: fm/stereo: idp=0x%x\n",idp);
435 if (0x03 == (idp & 0x03))
436 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
437 else if (0x05 == (idp & 0x05))
438 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
439 else if (0x01 == (idp & 0x01))
440 retval = V4L2_TUNER_SUB_MONO;
442 case TVAUDIO_FM_SAT_STEREO:
443 /* not implemented (yet) */
445 case TVAUDIO_NICAM_FM:
446 case TVAUDIO_NICAM_AM:
447 nicam = saa_readb(SAA7134_AUDIO_STATUS);
448 dprintk("getstereo: nicam=0x%x\n",nicam);
450 nicam_status = saa_readb(SAA7134_NICAM_STATUS);
451 dprintk("getstereo: nicam_status=0x%x\n", nicam_status);
453 switch (nicam_status & 0x03) {
455 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
458 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
461 retval = V4L2_TUNER_SUB_MONO;
464 /* No nicam detected */
469 dprintk("found audio subchannels:%s%s%s%s\n",
470 (retval & V4L2_TUNER_SUB_MONO) ? " mono" : "",
471 (retval & V4L2_TUNER_SUB_STEREO) ? " stereo" : "",
472 (retval & V4L2_TUNER_SUB_LANG1) ? " lang1" : "",
473 (retval & V4L2_TUNER_SUB_LANG2) ? " lang2" : "");
477 static int tvaudio_setstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio,
480 static char *name[] = {
481 [ V4L2_TUNER_MODE_MONO ] = "mono",
482 [ V4L2_TUNER_MODE_STEREO ] = "stereo",
483 [ V4L2_TUNER_MODE_LANG1 ] = "lang1",
484 [ V4L2_TUNER_MODE_LANG2 ] = "lang2",
487 [ V4L2_TUNER_MODE_MONO ] = 0x00, /* ch1 */
488 [ V4L2_TUNER_MODE_STEREO ] = 0x80, /* auto */
489 [ V4L2_TUNER_MODE_LANG1 ] = 0x00, /* ch1 */
490 [ V4L2_TUNER_MODE_LANG2 ] = 0x01, /* ch2 */
494 switch (audio->mode) {
495 case TVAUDIO_FM_MONO:
496 case TVAUDIO_AM_MONO:
497 /* nothing to do ... */
499 case TVAUDIO_FM_K_STEREO:
500 case TVAUDIO_FM_BG_STEREO:
501 case TVAUDIO_NICAM_AM:
502 case TVAUDIO_NICAM_FM:
503 dprintk("setstereo [fm] => %s\n",
504 name[ mode % ARRAY_SIZE(name) ]);
505 reg = fm[ mode % ARRAY_SIZE(fm) ];
506 saa_writeb(SAA7134_FM_DEMATRIX, reg);
508 case TVAUDIO_FM_SAT_STEREO:
509 /* Not implemented */
515 static int tvaudio_thread(void *data)
517 struct saa7134_dev *dev = data;
518 int carr_vals[ARRAY_SIZE(mainscan)];
519 unsigned int i, audio, nscan;
520 int max1,max2,carrier,rx,mode,lastmode,default_carrier;
522 daemonize("%s", dev->name);
523 allow_signal(SIGTERM);
525 tvaudio_sleep(dev,-1);
526 if (dev->thread.shutdown || signal_pending(current))
530 dev->thread.scan1 = dev->thread.scan2;
531 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
534 if (dev->ctl_automute)
536 mute_input_7134(dev);
538 /* give the tuner some time */
539 if (tvaudio_sleep(dev,SCAN_INITIAL_DELAY))
547 for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
548 if (!(dev->tvnorm->id & mainscan[i].std))
550 if (!default_carrier)
551 default_carrier = mainscan[i].carr;
556 /* only one candidate -- skip scan ;) */
558 carrier = default_carrier;
560 /* scan for the main carrier */
561 saa_writeb(SAA7134_MONITOR_SELECT,0x00);
562 tvaudio_setmode(dev,&tvaudio[0],NULL);
563 for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
564 carr_vals[i] = tvaudio_checkcarrier(dev, mainscan+i);
565 if (dev->thread.scan1 != dev->thread.scan2)
568 for (max1 = 0, max2 = 0, i = 0; i < ARRAY_SIZE(mainscan); i++) {
569 if (max1 < carr_vals[i]) {
572 carrier = mainscan[i].carr;
573 } else if (max2 < carr_vals[i]) {
579 if (0 != carrier && max1 > 2000 && max1 > max2*3) {
580 /* found good carrier */
581 dprintk("found %s main sound carrier @ %d.%03d MHz [%d/%d]\n",
582 dev->tvnorm->name, carrier/1000, carrier%1000,
584 dev->last_carrier = carrier;
586 } else if (0 != dev->last_carrier) {
587 /* no carrier -- try last detected one as fallback */
588 carrier = dev->last_carrier;
589 dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
590 "using %d.%03d MHz [last detected]\n",
591 dev->name, carrier/1000, carrier%1000);
594 /* no carrier + no fallback -- use default */
595 carrier = default_carrier;
596 dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
597 "using %d.%03d MHz [default]\n",
598 dev->name, carrier/1000, carrier%1000);
600 tvaudio_setcarrier(dev,carrier,carrier);
602 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x00);
603 saa7134_tvaudio_setmute(dev);
605 /* find the exact tv audio norm */
606 for (audio = UNSET, i = 0; i < TVAUDIO; i++) {
607 if (dev->tvnorm->id != UNSET &&
608 !(dev->tvnorm->id & tvaudio[i].std))
610 if (tvaudio[i].carr1 != carrier)
615 tvaudio_setmode(dev,&tvaudio[i],"trying");
616 if (tvaudio_sleep(dev,SCAN_SUBCARRIER_DELAY))
618 if (-1 != tvaudio_getstereo(dev,&tvaudio[i])) {
623 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x30);
626 tvaudio_setmode(dev,&tvaudio[audio],"using");
627 tvaudio_setstereo(dev,&tvaudio[audio],V4L2_TUNER_MODE_MONO);
628 dev->tvaudio = &tvaudio[audio];
632 if (tvaudio_sleep(dev,5000))
634 if (dev->thread.shutdown || signal_pending(current))
636 if (UNSET == dev->thread.mode) {
637 rx = tvaudio_getstereo(dev,&tvaudio[i]);
638 mode = saa7134_tvaudio_rx2mode(rx);
640 mode = dev->thread.mode;
642 if (lastmode != mode) {
643 tvaudio_setstereo(dev,&tvaudio[audio],mode);
650 complete_and_exit(&dev->thread.exit, 0);
654 /* ------------------------------------------------------------------ */
655 /* saa7133 / saa7135 code */
657 static char *stdres[0x20] = {
658 [0x00] = "no standard detected",
659 [0x01] = "B/G (in progress)",
660 [0x02] = "D/K (in progress)",
661 [0x03] = "M (in progress)",
664 [0x05] = "B/G NICAM",
665 [0x06] = "D/K A2 (1)",
666 [0x07] = "D/K A2 (2)",
667 [0x08] = "D/K A2 (3)",
668 [0x09] = "D/K NICAM",
676 [0x0f] = "FM radio / IF 10.7 / 50 deemp",
677 [0x10] = "FM radio / IF 10.7 / 75 deemp",
678 [0x11] = "FM radio / IF sel / 50 deemp",
679 [0x12] = "FM radio / IF sel / 75 deemp",
681 [0x13 ... 0x1e ] = "unknown",
682 [0x1f] = "??? [in progress]",
688 static inline int saa_dsp_wait_bit(struct saa7134_dev *dev, int bit)
690 int state, count = DSP_RETRY;
692 state = saa_readb(SAA7135_DSP_RWSTATE);
693 if (unlikely(state & SAA7135_DSP_RWSTATE_ERR)) {
694 printk("%s: dsp access error\n",dev->name);
695 /* FIXME: send ack ... */
698 while (0 == (state & bit)) {
699 if (unlikely(0 == count)) {
700 printk("%s: dsp access wait timeout [bit=%s]\n",
702 (bit & SAA7135_DSP_RWSTATE_WRR) ? "WRR" :
703 (bit & SAA7135_DSP_RWSTATE_RDB) ? "RDB" :
704 (bit & SAA7135_DSP_RWSTATE_IDA) ? "IDA" :
709 state = saa_readb(SAA7135_DSP_RWSTATE);
716 int saa_dsp_writel(struct saa7134_dev *dev, int reg, u32 value)
720 d2printk("dsp write reg 0x%x = 0x%06x\n",reg<<2,value);
721 err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
724 saa_writel(reg,value);
725 err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
731 static int getstereo_7133(struct saa7134_dev *dev)
733 int retval = V4L2_TUNER_SUB_MONO;
736 value = saa_readl(0x528 >> 2);
738 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
740 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
744 static int mute_input_7133(struct saa7134_dev *dev)
749 struct saa7134_input *in;
751 /* Hac 0506 route OSS sound simultanously */
753 switch (dev->input->amux) {
766 saa_dsp_writel(dev, 0x464 >> 2, xbarin);
772 saa_dsp_writel(dev, 0x46c >> 2, xbarout);
774 saa_writel(0x594 >> 2, reg);
777 /* switch gpio-connected external audio mux */
778 if (0 != card(dev).gpiomask) {
779 mask = card(dev).gpiomask;
781 if (card(dev).mute.name && dev->ctl_mute)
782 in = &card(dev).mute;
786 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, mask, mask);
787 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio);
788 saa7134_track_gpio(dev,in->name);
794 static int tvaudio_thread_ddep(void *data)
796 struct saa7134_dev *dev = data;
797 u32 value, norms, clock;
799 daemonize("%s", dev->name);
800 allow_signal(SIGTERM);
802 clock = saa7134_boards[dev->board].audio_clock;
803 if (UNSET != audio_clock_override)
804 clock = audio_clock_override;
805 saa_writel(0x598 >> 2, clock);
808 saa_dsp_writel(dev, 0x474 >> 2, 0x00);
809 saa_dsp_writel(dev, 0x450 >> 2, 0x00);
812 tvaudio_sleep(dev,-1);
813 if (dev->thread.shutdown || signal_pending(current))
817 dev->thread.scan1 = dev->thread.scan2;
818 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
820 if (audio_ddep >= 0x04 && audio_ddep <= 0x0e) {
821 /* insmod option override */
822 norms = (audio_ddep << 2) | 0x01;
823 dprintk("ddep override: %s\n",stdres[audio_ddep]);
824 } else if (&card(dev).radio == dev->input) {
825 dprintk("FM Radio\n");
826 if (dev->tuner_type == TUNER_PHILIPS_TDA8290) {
827 norms = (0x11 << 2) | 0x01;
828 saa_dsp_writel(dev, 0x42c >> 2, 0x729555);
830 norms = (0x0f << 2) | 0x01;
833 /* (let chip) scan for sound carrier */
835 if (dev->tvnorm->id & V4L2_STD_PAL) {
836 dprintk("PAL scan\n");
837 norms |= 0x2c; /* B/G + D/K + I */
839 if (dev->tvnorm->id & V4L2_STD_NTSC) {
840 dprintk("NTSC scan\n");
841 norms |= 0x40; /* M */
843 if (dev->tvnorm->id & V4L2_STD_SECAM) {
844 dprintk("SECAM scan\n");
845 norms |= 0x18; /* L + D/K */
848 norms = 0x7c; /* all */
849 dprintk("scanning:%s%s%s%s%s\n",
850 (norms & 0x04) ? " B/G" : "",
851 (norms & 0x08) ? " D/K" : "",
852 (norms & 0x10) ? " L/L'" : "",
853 (norms & 0x20) ? " I" : "",
854 (norms & 0x40) ? " M" : "");
857 /* kick automatic standard detection */
858 saa_dsp_writel(dev, 0x454 >> 2, 0);
859 saa_dsp_writel(dev, 0x454 >> 2, norms | 0x80);
861 /* setup crossbars */
862 saa_dsp_writel(dev, 0x464 >> 2, 0x000000);
863 saa_dsp_writel(dev, 0x470 >> 2, 0x101010);
865 if (tvaudio_sleep(dev,3000))
867 value = saa_readl(0x528 >> 2) & 0xffffff;
869 dprintk("tvaudio thread status: 0x%x [%s%s%s]\n",
870 value, stdres[value & 0x1f],
871 (value & 0x000020) ? ",stereo" : "",
872 (value & 0x000040) ? ",dual" : "");
873 dprintk("detailed status: "
874 "%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n",
875 (value & 0x000080) ? " A2/EIAJ pilot tone " : "",
876 (value & 0x000100) ? " A2/EIAJ dual " : "",
877 (value & 0x000200) ? " A2/EIAJ stereo " : "",
878 (value & 0x000400) ? " A2/EIAJ noise mute " : "",
880 (value & 0x000800) ? " BTSC/FM radio pilot " : "",
881 (value & 0x001000) ? " SAP carrier " : "",
882 (value & 0x002000) ? " BTSC stereo noise mute " : "",
883 (value & 0x004000) ? " SAP noise mute " : "",
884 (value & 0x008000) ? " VDSP " : "",
886 (value & 0x010000) ? " NICST " : "",
887 (value & 0x020000) ? " NICDU " : "",
888 (value & 0x040000) ? " NICAM muted " : "",
889 (value & 0x080000) ? " NICAM reserve sound " : "",
891 (value & 0x100000) ? " init done " : "");
895 complete_and_exit(&dev->thread.exit, 0);
899 /* ------------------------------------------------------------------ */
900 /* common stuff + external entry points */
902 static void saa7134_enable_i2s(struct saa7134_dev *dev)
906 if (!card_is_empress(dev))
908 i2s_format = (dev->input->amux == TV) ? 0x00 : 0x01;
910 /* enable I2S audio output for the mpeg encoder */
911 saa_writeb(SAA7134_I2S_OUTPUT_SELECT, 0x80);
912 saa_writeb(SAA7134_I2S_OUTPUT_FORMAT, i2s_format);
913 saa_writeb(SAA7134_I2S_OUTPUT_LEVEL, 0x0F);
914 saa_writeb(SAA7134_I2S_AUDIO_OUTPUT, 0x01);
917 int saa7134_tvaudio_rx2mode(u32 rx)
921 mode = V4L2_TUNER_MODE_MONO;
922 if (rx & V4L2_TUNER_SUB_STEREO)
923 mode = V4L2_TUNER_MODE_STEREO;
924 else if (rx & V4L2_TUNER_SUB_LANG1)
925 mode = V4L2_TUNER_MODE_LANG1;
926 else if (rx & V4L2_TUNER_SUB_LANG2)
927 mode = V4L2_TUNER_MODE_LANG2;
931 void saa7134_tvaudio_setmute(struct saa7134_dev *dev)
933 switch (dev->pci->device) {
934 case PCI_DEVICE_ID_PHILIPS_SAA7130:
935 case PCI_DEVICE_ID_PHILIPS_SAA7134:
936 mute_input_7134(dev);
938 case PCI_DEVICE_ID_PHILIPS_SAA7133:
939 case PCI_DEVICE_ID_PHILIPS_SAA7135:
940 mute_input_7133(dev);
945 void saa7134_tvaudio_setinput(struct saa7134_dev *dev,
946 struct saa7134_input *in)
949 switch (dev->pci->device) {
950 case PCI_DEVICE_ID_PHILIPS_SAA7130:
951 case PCI_DEVICE_ID_PHILIPS_SAA7134:
952 mute_input_7134(dev);
954 case PCI_DEVICE_ID_PHILIPS_SAA7133:
955 case PCI_DEVICE_ID_PHILIPS_SAA7135:
956 mute_input_7133(dev);
959 saa7134_enable_i2s(dev);
962 void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level)
964 switch (dev->pci->device) {
965 case PCI_DEVICE_ID_PHILIPS_SAA7134:
966 saa_writeb(SAA7134_CHANNEL1_LEVEL, level & 0x1f);
967 saa_writeb(SAA7134_CHANNEL2_LEVEL, level & 0x1f);
968 saa_writeb(SAA7134_NICAM_LEVEL_ADJUST, level & 0x1f);
973 int saa7134_tvaudio_getstereo(struct saa7134_dev *dev)
975 int retval = V4L2_TUNER_SUB_MONO;
977 switch (dev->pci->device) {
978 case PCI_DEVICE_ID_PHILIPS_SAA7134:
980 retval = tvaudio_getstereo(dev,dev->tvaudio);
982 case PCI_DEVICE_ID_PHILIPS_SAA7133:
983 case PCI_DEVICE_ID_PHILIPS_SAA7135:
984 retval = getstereo_7133(dev);
990 int saa7134_tvaudio_init2(struct saa7134_dev *dev)
992 DECLARE_MUTEX_LOCKED(sem);
993 int (*my_thread)(void *data) = NULL;
995 switch (dev->pci->device) {
996 case PCI_DEVICE_ID_PHILIPS_SAA7134:
997 my_thread = tvaudio_thread;
999 case PCI_DEVICE_ID_PHILIPS_SAA7133:
1000 case PCI_DEVICE_ID_PHILIPS_SAA7135:
1001 my_thread = tvaudio_thread_ddep;
1005 dev->thread.pid = -1;
1007 /* start tvaudio thread */
1008 init_waitqueue_head(&dev->thread.wq);
1009 init_completion(&dev->thread.exit);
1010 dev->thread.pid = kernel_thread(my_thread,dev,0);
1011 if (dev->thread.pid < 0)
1012 printk(KERN_WARNING "%s: kernel_thread() failed\n",
1014 saa7134_tvaudio_do_scan(dev);
1017 saa7134_enable_i2s(dev);
1021 int saa7134_tvaudio_fini(struct saa7134_dev *dev)
1023 /* shutdown tvaudio thread */
1024 if (dev->thread.pid >= 0) {
1025 dev->thread.shutdown = 1;
1026 wake_up_interruptible(&dev->thread.wq);
1027 wait_for_completion(&dev->thread.exit);
1029 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, 0x00); /* LINE1 */
1033 int saa7134_tvaudio_do_scan(struct saa7134_dev *dev)
1035 if (dev->thread.pid >= 0) {
1036 dev->thread.mode = UNSET;
1037 dev->thread.scan2++;
1038 wake_up_interruptible(&dev->thread.wq);
1041 saa7134_tvaudio_setmute(dev);
1046 EXPORT_SYMBOL(saa_dsp_writel);
1048 /* ----------------------------------------------------------- */