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",
485 [ V4L2_TUNER_MODE_LANG1_LANG2 ] = "lang1+lang2",
488 [ V4L2_TUNER_MODE_MONO ] = 0x00, /* ch1 */
489 [ V4L2_TUNER_MODE_STEREO ] = 0x80, /* auto */
490 [ V4L2_TUNER_MODE_LANG1 ] = 0x00, /* ch1 */
491 [ V4L2_TUNER_MODE_LANG2 ] = 0x01, /* ch2 */
492 [ V4L2_TUNER_MODE_LANG1_LANG2 ] = 0x80, /* auto */
496 switch (audio->mode) {
497 case TVAUDIO_FM_MONO:
498 case TVAUDIO_AM_MONO:
499 /* nothing to do ... */
501 case TVAUDIO_FM_K_STEREO:
502 case TVAUDIO_FM_BG_STEREO:
503 case TVAUDIO_NICAM_AM:
504 case TVAUDIO_NICAM_FM:
505 dprintk("setstereo [fm] => %s\n",
506 name[ mode % ARRAY_SIZE(name) ]);
507 reg = fm[ mode % ARRAY_SIZE(fm) ];
508 saa_writeb(SAA7134_FM_DEMATRIX, reg);
510 case TVAUDIO_FM_SAT_STEREO:
511 /* Not implemented */
517 static int tvaudio_thread(void *data)
519 struct saa7134_dev *dev = data;
520 int carr_vals[ARRAY_SIZE(mainscan)];
521 unsigned int i, audio, nscan;
522 int max1,max2,carrier,rx,mode,lastmode,default_carrier;
524 daemonize("%s", dev->name);
525 allow_signal(SIGTERM);
527 tvaudio_sleep(dev,-1);
528 if (dev->thread.shutdown || signal_pending(current))
532 dev->thread.scan1 = dev->thread.scan2;
533 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
536 if (dev->ctl_automute)
538 mute_input_7134(dev);
540 /* give the tuner some time */
541 if (tvaudio_sleep(dev,SCAN_INITIAL_DELAY))
549 for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
550 if (!(dev->tvnorm->id & mainscan[i].std))
552 if (!default_carrier)
553 default_carrier = mainscan[i].carr;
558 /* only one candidate -- skip scan ;) */
560 carrier = default_carrier;
562 /* scan for the main carrier */
563 saa_writeb(SAA7134_MONITOR_SELECT,0x00);
564 tvaudio_setmode(dev,&tvaudio[0],NULL);
565 for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
566 carr_vals[i] = tvaudio_checkcarrier(dev, mainscan+i);
567 if (dev->thread.scan1 != dev->thread.scan2)
570 for (max1 = 0, max2 = 0, i = 0; i < ARRAY_SIZE(mainscan); i++) {
571 if (max1 < carr_vals[i]) {
574 carrier = mainscan[i].carr;
575 } else if (max2 < carr_vals[i]) {
581 if (0 != carrier && max1 > 2000 && max1 > max2*3) {
582 /* found good carrier */
583 dprintk("found %s main sound carrier @ %d.%03d MHz [%d/%d]\n",
584 dev->tvnorm->name, carrier/1000, carrier%1000,
586 dev->last_carrier = carrier;
588 } else if (0 != dev->last_carrier) {
589 /* no carrier -- try last detected one as fallback */
590 carrier = dev->last_carrier;
591 dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
592 "using %d.%03d MHz [last detected]\n",
593 dev->name, carrier/1000, carrier%1000);
596 /* no carrier + no fallback -- use default */
597 carrier = default_carrier;
598 dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
599 "using %d.%03d MHz [default]\n",
600 dev->name, carrier/1000, carrier%1000);
602 tvaudio_setcarrier(dev,carrier,carrier);
604 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x00);
605 saa7134_tvaudio_setmute(dev);
607 /* find the exact tv audio norm */
608 for (audio = UNSET, i = 0; i < TVAUDIO; i++) {
609 if (dev->tvnorm->id != UNSET &&
610 !(dev->tvnorm->id & tvaudio[i].std))
612 if (tvaudio[i].carr1 != carrier)
617 tvaudio_setmode(dev,&tvaudio[i],"trying");
618 if (tvaudio_sleep(dev,SCAN_SUBCARRIER_DELAY))
620 if (-1 != tvaudio_getstereo(dev,&tvaudio[i])) {
625 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x30);
628 tvaudio_setmode(dev,&tvaudio[audio],"using");
629 tvaudio_setstereo(dev,&tvaudio[audio],V4L2_TUNER_MODE_MONO);
630 dev->tvaudio = &tvaudio[audio];
634 if (tvaudio_sleep(dev,5000))
636 if (dev->thread.shutdown || signal_pending(current))
638 if (UNSET == dev->thread.mode) {
639 rx = tvaudio_getstereo(dev,&tvaudio[i]);
640 mode = saa7134_tvaudio_rx2mode(rx);
642 mode = dev->thread.mode;
644 if (lastmode != mode) {
645 tvaudio_setstereo(dev,&tvaudio[audio],mode);
652 complete_and_exit(&dev->thread.exit, 0);
656 /* ------------------------------------------------------------------ */
657 /* saa7133 / saa7135 code */
659 static char *stdres[0x20] = {
660 [0x00] = "no standard detected",
661 [0x01] = "B/G (in progress)",
662 [0x02] = "D/K (in progress)",
663 [0x03] = "M (in progress)",
666 [0x05] = "B/G NICAM",
667 [0x06] = "D/K A2 (1)",
668 [0x07] = "D/K A2 (2)",
669 [0x08] = "D/K A2 (3)",
670 [0x09] = "D/K NICAM",
678 [0x0f] = "FM radio / IF 10.7 / 50 deemp",
679 [0x10] = "FM radio / IF 10.7 / 75 deemp",
680 [0x11] = "FM radio / IF sel / 50 deemp",
681 [0x12] = "FM radio / IF sel / 75 deemp",
683 [0x13 ... 0x1e ] = "unknown",
684 [0x1f] = "??? [in progress]",
690 static inline int saa_dsp_wait_bit(struct saa7134_dev *dev, int bit)
692 int state, count = DSP_RETRY;
694 state = saa_readb(SAA7135_DSP_RWSTATE);
695 if (unlikely(state & SAA7135_DSP_RWSTATE_ERR)) {
696 printk("%s: dsp access error\n",dev->name);
697 /* FIXME: send ack ... */
700 while (0 == (state & bit)) {
701 if (unlikely(0 == count)) {
702 printk("%s: dsp access wait timeout [bit=%s]\n",
704 (bit & SAA7135_DSP_RWSTATE_WRR) ? "WRR" :
705 (bit & SAA7135_DSP_RWSTATE_RDB) ? "RDB" :
706 (bit & SAA7135_DSP_RWSTATE_IDA) ? "IDA" :
711 state = saa_readb(SAA7135_DSP_RWSTATE);
718 int saa_dsp_writel(struct saa7134_dev *dev, int reg, u32 value)
722 d2printk("dsp write reg 0x%x = 0x%06x\n",reg<<2,value);
723 err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
726 saa_writel(reg,value);
727 err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
733 static int getstereo_7133(struct saa7134_dev *dev)
735 int retval = V4L2_TUNER_SUB_MONO;
738 value = saa_readl(0x528 >> 2);
740 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
742 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
746 static int mute_input_7133(struct saa7134_dev *dev)
751 struct saa7134_input *in;
753 /* Hac 0506 route OSS sound simultanously */
755 switch (dev->input->amux) {
768 saa_dsp_writel(dev, 0x464 >> 2, xbarin);
774 saa_dsp_writel(dev, 0x46c >> 2, xbarout);
776 saa_writel(0x594 >> 2, reg);
779 /* switch gpio-connected external audio mux */
780 if (0 != card(dev).gpiomask) {
781 mask = card(dev).gpiomask;
783 if (card(dev).mute.name && dev->ctl_mute)
784 in = &card(dev).mute;
788 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, mask, mask);
789 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio);
790 saa7134_track_gpio(dev,in->name);
796 static int tvaudio_thread_ddep(void *data)
798 struct saa7134_dev *dev = data;
799 u32 value, norms, clock;
801 daemonize("%s", dev->name);
802 allow_signal(SIGTERM);
804 clock = saa7134_boards[dev->board].audio_clock;
805 if (UNSET != audio_clock_override)
806 clock = audio_clock_override;
807 saa_writel(0x598 >> 2, clock);
810 saa_dsp_writel(dev, 0x474 >> 2, 0x00);
811 saa_dsp_writel(dev, 0x450 >> 2, 0x00);
814 tvaudio_sleep(dev,-1);
815 if (dev->thread.shutdown || signal_pending(current))
819 dev->thread.scan1 = dev->thread.scan2;
820 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
822 if (audio_ddep >= 0x04 && audio_ddep <= 0x0e) {
823 /* insmod option override */
824 norms = (audio_ddep << 2) | 0x01;
825 dprintk("ddep override: %s\n",stdres[audio_ddep]);
826 } else if (&card(dev).radio == dev->input) {
827 dprintk("FM Radio\n");
828 if (dev->tuner_type == TUNER_PHILIPS_TDA8290) {
829 norms = (0x11 << 2) | 0x01;
830 saa_dsp_writel(dev, 0x42c >> 2, 0x729555);
832 norms = (0x0f << 2) | 0x01;
835 /* (let chip) scan for sound carrier */
837 if (dev->tvnorm->id & V4L2_STD_PAL) {
838 dprintk("PAL scan\n");
839 norms |= 0x2c; /* B/G + D/K + I */
841 if (dev->tvnorm->id & V4L2_STD_NTSC) {
842 dprintk("NTSC scan\n");
843 norms |= 0x40; /* M */
845 if (dev->tvnorm->id & V4L2_STD_SECAM) {
846 dprintk("SECAM scan\n");
847 norms |= 0x18; /* L + D/K */
850 norms = 0x7c; /* all */
851 dprintk("scanning:%s%s%s%s%s\n",
852 (norms & 0x04) ? " B/G" : "",
853 (norms & 0x08) ? " D/K" : "",
854 (norms & 0x10) ? " L/L'" : "",
855 (norms & 0x20) ? " I" : "",
856 (norms & 0x40) ? " M" : "");
859 /* kick automatic standard detection */
860 saa_dsp_writel(dev, 0x454 >> 2, 0);
861 saa_dsp_writel(dev, 0x454 >> 2, norms | 0x80);
863 /* setup crossbars */
864 saa_dsp_writel(dev, 0x464 >> 2, 0x000000);
865 saa_dsp_writel(dev, 0x470 >> 2, 0x101010);
867 if (tvaudio_sleep(dev,3000))
869 value = saa_readl(0x528 >> 2) & 0xffffff;
871 dprintk("tvaudio thread status: 0x%x [%s%s%s]\n",
872 value, stdres[value & 0x1f],
873 (value & 0x000020) ? ",stereo" : "",
874 (value & 0x000040) ? ",dual" : "");
875 dprintk("detailed status: "
876 "%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n",
877 (value & 0x000080) ? " A2/EIAJ pilot tone " : "",
878 (value & 0x000100) ? " A2/EIAJ dual " : "",
879 (value & 0x000200) ? " A2/EIAJ stereo " : "",
880 (value & 0x000400) ? " A2/EIAJ noise mute " : "",
882 (value & 0x000800) ? " BTSC/FM radio pilot " : "",
883 (value & 0x001000) ? " SAP carrier " : "",
884 (value & 0x002000) ? " BTSC stereo noise mute " : "",
885 (value & 0x004000) ? " SAP noise mute " : "",
886 (value & 0x008000) ? " VDSP " : "",
888 (value & 0x010000) ? " NICST " : "",
889 (value & 0x020000) ? " NICDU " : "",
890 (value & 0x040000) ? " NICAM muted " : "",
891 (value & 0x080000) ? " NICAM reserve sound " : "",
893 (value & 0x100000) ? " init done " : "");
897 complete_and_exit(&dev->thread.exit, 0);
901 /* ------------------------------------------------------------------ */
902 /* common stuff + external entry points */
904 static void saa7134_enable_i2s(struct saa7134_dev *dev)
908 if (!card_is_empress(dev))
910 i2s_format = (dev->input->amux == TV) ? 0x00 : 0x01;
912 /* enable I2S audio output for the mpeg encoder */
913 saa_writeb(SAA7134_I2S_OUTPUT_SELECT, 0x80);
914 saa_writeb(SAA7134_I2S_OUTPUT_FORMAT, i2s_format);
915 saa_writeb(SAA7134_I2S_OUTPUT_LEVEL, 0x0F);
916 saa_writeb(SAA7134_I2S_AUDIO_OUTPUT, 0x01);
919 int saa7134_tvaudio_rx2mode(u32 rx)
923 mode = V4L2_TUNER_MODE_MONO;
924 if (rx & V4L2_TUNER_SUB_STEREO)
925 mode = V4L2_TUNER_MODE_STEREO;
926 else if (rx & V4L2_TUNER_SUB_LANG1)
927 mode = V4L2_TUNER_MODE_LANG1;
928 else if (rx & V4L2_TUNER_SUB_LANG2)
929 mode = V4L2_TUNER_MODE_LANG2;
933 void saa7134_tvaudio_setmute(struct saa7134_dev *dev)
935 switch (dev->pci->device) {
936 case PCI_DEVICE_ID_PHILIPS_SAA7130:
937 case PCI_DEVICE_ID_PHILIPS_SAA7134:
938 mute_input_7134(dev);
940 case PCI_DEVICE_ID_PHILIPS_SAA7133:
941 case PCI_DEVICE_ID_PHILIPS_SAA7135:
942 mute_input_7133(dev);
947 void saa7134_tvaudio_setinput(struct saa7134_dev *dev,
948 struct saa7134_input *in)
951 switch (dev->pci->device) {
952 case PCI_DEVICE_ID_PHILIPS_SAA7130:
953 case PCI_DEVICE_ID_PHILIPS_SAA7134:
954 mute_input_7134(dev);
956 case PCI_DEVICE_ID_PHILIPS_SAA7133:
957 case PCI_DEVICE_ID_PHILIPS_SAA7135:
958 mute_input_7133(dev);
961 saa7134_enable_i2s(dev);
964 void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level)
966 switch (dev->pci->device) {
967 case PCI_DEVICE_ID_PHILIPS_SAA7134:
968 saa_writeb(SAA7134_CHANNEL1_LEVEL, level & 0x1f);
969 saa_writeb(SAA7134_CHANNEL2_LEVEL, level & 0x1f);
970 saa_writeb(SAA7134_NICAM_LEVEL_ADJUST, level & 0x1f);
975 int saa7134_tvaudio_getstereo(struct saa7134_dev *dev)
977 int retval = V4L2_TUNER_SUB_MONO;
979 switch (dev->pci->device) {
980 case PCI_DEVICE_ID_PHILIPS_SAA7134:
982 retval = tvaudio_getstereo(dev,dev->tvaudio);
984 case PCI_DEVICE_ID_PHILIPS_SAA7133:
985 case PCI_DEVICE_ID_PHILIPS_SAA7135:
986 retval = getstereo_7133(dev);
992 int saa7134_tvaudio_init2(struct saa7134_dev *dev)
994 DECLARE_MUTEX_LOCKED(sem);
995 int (*my_thread)(void *data) = NULL;
997 switch (dev->pci->device) {
998 case PCI_DEVICE_ID_PHILIPS_SAA7134:
999 my_thread = tvaudio_thread;
1001 case PCI_DEVICE_ID_PHILIPS_SAA7133:
1002 case PCI_DEVICE_ID_PHILIPS_SAA7135:
1003 my_thread = tvaudio_thread_ddep;
1007 dev->thread.pid = -1;
1009 /* start tvaudio thread */
1010 init_waitqueue_head(&dev->thread.wq);
1011 init_completion(&dev->thread.exit);
1012 dev->thread.pid = kernel_thread(my_thread,dev,0);
1013 if (dev->thread.pid < 0)
1014 printk(KERN_WARNING "%s: kernel_thread() failed\n",
1016 saa7134_tvaudio_do_scan(dev);
1019 saa7134_enable_i2s(dev);
1023 int saa7134_tvaudio_fini(struct saa7134_dev *dev)
1025 /* shutdown tvaudio thread */
1026 if (dev->thread.pid >= 0) {
1027 dev->thread.shutdown = 1;
1028 wake_up_interruptible(&dev->thread.wq);
1029 wait_for_completion(&dev->thread.exit);
1031 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, 0x00); /* LINE1 */
1035 int saa7134_tvaudio_do_scan(struct saa7134_dev *dev)
1037 if (dev->thread.pid >= 0) {
1038 dev->thread.mode = UNSET;
1039 dev->thread.scan2++;
1040 wake_up_interruptible(&dev->thread.wq);
1043 saa7134_tvaudio_setmute(dev);
1048 EXPORT_SYMBOL(saa_dsp_writel);
1050 /* ----------------------------------------------------------- */