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/kernel.h>
27 #include <linux/kthread.h>
28 #include <linux/slab.h>
29 #include <linux/delay.h>
30 #include <linux/freezer.h>
31 #include <asm/div64.h>
33 #include "saa7134-reg.h"
36 /* ------------------------------------------------------------------ */
38 static unsigned int audio_debug;
39 module_param(audio_debug, int, 0644);
40 MODULE_PARM_DESC(audio_debug,"enable debug messages [tv audio]");
42 static unsigned int audio_ddep;
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;
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 ARRAY_SIZE(tvaudio)
164 /* ------------------------------------------------------------------ */
166 static u32 tvaudio_carr2reg(u32 carrier)
175 static void tvaudio_setcarrier(struct saa7134_dev *dev,
176 int primary, int secondary)
180 saa_writel(SAA7134_CARRIER1_FREQ0 >> 2, tvaudio_carr2reg(primary));
181 saa_writel(SAA7134_CARRIER2_FREQ0 >> 2, tvaudio_carr2reg(secondary));
184 #define SAA7134_MUTE_MASK 0xbb
185 #define SAA7134_MUTE_ANALOG 0x04
186 #define SAA7134_MUTE_I2S 0x40
188 static void mute_input_7134(struct saa7134_dev *dev)
191 struct saa7134_input *in;
192 int ausel=0, ics=0, ocs=0;
195 /* look what is to do ... */
197 mute = (dev->ctl_mute ||
198 (dev->automute && (&card(dev).radio) != in));
199 if (card(dev).mute.name) {
201 * 7130 - we'll mute using some unconnected audio input
202 * 7134 - we'll probably should switch external mux with gpio
205 in = &card(dev).mute;
208 if (dev->hw_mute == mute &&
209 dev->hw_input == in && !dev->insuspend) {
210 dprintk("mute/input: nothing to do [mute=%d,input=%s]\n",
215 dprintk("ctl_mute=%d automute=%d input=%s => mute=%d input=%s\n",
216 dev->ctl_mute,dev->automute,dev->input->name,mute,in->name);
220 if (PCI_DEVICE_ID_PHILIPS_SAA7134 == dev->pci->device)
222 saa_writeb(SAA7134_AUDIO_MUTE_CTRL, mute ?
224 SAA7134_MUTE_ANALOG |
228 /* switch internal audio mux */
230 case TV: ausel=0xc0; ics=0x00; ocs=0x02; break;
231 case LINE1: ausel=0x80; ics=0x00; ocs=0x00; break;
232 case LINE2: ausel=0x80; ics=0x08; ocs=0x01; break;
233 case LINE2_LEFT: ausel=0x80; ics=0x08; ocs=0x05; break;
235 saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, ausel);
236 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, ics);
237 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, ocs);
238 // for oss, we need to change the clock configuration
240 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x00);
242 saa_andorb(SAA7134_SIF_SAMPLE_FREQ, 0x03, 0x01);
244 /* switch gpio-connected external audio mux */
245 if (0 == card(dev).gpiomask)
248 mask = card(dev).gpiomask;
249 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, mask, mask);
250 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio);
251 saa7134_track_gpio(dev,in->name);
254 static void tvaudio_setmode(struct saa7134_dev *dev,
255 struct saa7134_tvaudio *audio,
260 if (dev->tvnorm->id == V4L2_STD_NTSC) {
265 if (audio_clock_tweak > -1024 && audio_clock_tweak < 1024)
266 tweak = audio_clock_tweak;
269 dprintk("tvaudio_setmode: %s %s [%d.%03d/%d.%03d MHz] acpf=%d%+d\n",
271 audio->carr1 / 1000, audio->carr1 % 1000,
272 audio->carr2 / 1000, audio->carr2 % 1000,
276 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD0, (acpf & 0x0000ff) >> 0);
277 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD1, (acpf & 0x00ff00) >> 8);
278 saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD2, (acpf & 0x030000) >> 16);
279 tvaudio_setcarrier(dev,audio->carr1,audio->carr2);
281 switch (audio->mode) {
282 case TVAUDIO_FM_MONO:
283 case TVAUDIO_FM_BG_STEREO:
284 saa_writeb(SAA7134_DEMODULATOR, 0x00);
285 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
286 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x22);
287 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
288 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa0);
290 case TVAUDIO_FM_K_STEREO:
291 saa_writeb(SAA7134_DEMODULATOR, 0x00);
292 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x01);
293 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x22);
294 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
295 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa0);
297 case TVAUDIO_NICAM_FM:
298 saa_writeb(SAA7134_DEMODULATOR, 0x10);
299 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
300 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x44);
301 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa1);
302 saa_writeb(SAA7134_NICAM_CONFIG, 0x00);
304 case TVAUDIO_NICAM_AM:
305 saa_writeb(SAA7134_DEMODULATOR, 0x12);
306 saa_writeb(SAA7134_DCXO_IDENT_CTRL, 0x00);
307 saa_writeb(SAA7134_FM_DEEMPHASIS, 0x44);
308 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0xa1);
309 saa_writeb(SAA7134_NICAM_CONFIG, 0x00);
311 case TVAUDIO_FM_SAT_STEREO:
312 /* not implemented (yet) */
317 static int tvaudio_sleep(struct saa7134_dev *dev, int timeout)
319 if (dev->thread.scan1 == dev->thread.scan2 &&
320 !kthread_should_stop()) {
322 set_current_state(TASK_INTERRUPTIBLE);
325 schedule_timeout_interruptible
326 (msecs_to_jiffies(timeout));
329 return dev->thread.scan1 != dev->thread.scan2;
332 static int tvaudio_checkcarrier(struct saa7134_dev *dev, struct mainscan *scan)
334 __s32 left,right,value;
336 if (audio_debug > 1) {
338 dprintk("debug %d:",scan->carr);
339 for (i = -150; i <= 150; i += 30) {
340 tvaudio_setcarrier(dev,scan->carr+i,scan->carr+i);
341 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
342 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
344 value = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
346 printk(" # %6d # ",value >> 16);
348 printk(" %6d",value >> 16);
352 if (dev->tvnorm->id & scan->std) {
353 tvaudio_setcarrier(dev,scan->carr-90,scan->carr-90);
354 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
355 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
357 left = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
359 tvaudio_setcarrier(dev,scan->carr+90,scan->carr+90);
360 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
361 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
363 right = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
367 value = left > right ? left - right : right - left;
368 dprintk("scanning %d.%03d MHz [%4s] => dc is %5d [%d/%d]\n",
369 scan->carr / 1000, scan->carr % 1000,
370 scan->name, value, left, right);
373 dprintk("skipping %d.%03d MHz [%4s]\n",
374 scan->carr / 1000, scan->carr % 1000, scan->name);
380 static int tvaudio_getstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio)
382 __u32 idp, nicam, nicam_status;
385 switch (audio->mode) {
386 case TVAUDIO_FM_MONO:
387 return V4L2_TUNER_SUB_MONO;
388 case TVAUDIO_FM_K_STEREO:
389 case TVAUDIO_FM_BG_STEREO:
390 idp = (saa_readb(SAA7134_IDENT_SIF) & 0xe0) >> 5;
391 dprintk("getstereo: fm/stereo: idp=0x%x\n",idp);
392 if (0x03 == (idp & 0x03))
393 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
394 else if (0x05 == (idp & 0x05))
395 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
396 else if (0x01 == (idp & 0x01))
397 retval = V4L2_TUNER_SUB_MONO;
399 case TVAUDIO_FM_SAT_STEREO:
400 /* not implemented (yet) */
402 case TVAUDIO_NICAM_FM:
403 case TVAUDIO_NICAM_AM:
404 nicam = saa_readb(SAA7134_AUDIO_STATUS);
405 dprintk("getstereo: nicam=0x%x\n",nicam);
407 nicam_status = saa_readb(SAA7134_NICAM_STATUS);
408 dprintk("getstereo: nicam_status=0x%x\n", nicam_status);
410 switch (nicam_status & 0x03) {
412 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
415 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
418 retval = V4L2_TUNER_SUB_MONO;
421 /* No nicam detected */
426 dprintk("found audio subchannels:%s%s%s%s\n",
427 (retval & V4L2_TUNER_SUB_MONO) ? " mono" : "",
428 (retval & V4L2_TUNER_SUB_STEREO) ? " stereo" : "",
429 (retval & V4L2_TUNER_SUB_LANG1) ? " lang1" : "",
430 (retval & V4L2_TUNER_SUB_LANG2) ? " lang2" : "");
434 static int tvaudio_setstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio,
437 static char *name[] = {
438 [ V4L2_TUNER_MODE_MONO ] = "mono",
439 [ V4L2_TUNER_MODE_STEREO ] = "stereo",
440 [ V4L2_TUNER_MODE_LANG1 ] = "lang1",
441 [ V4L2_TUNER_MODE_LANG2 ] = "lang2",
442 [ V4L2_TUNER_MODE_LANG1_LANG2 ] = "lang1+lang2",
445 [ V4L2_TUNER_MODE_MONO ] = 0x00, /* ch1 */
446 [ V4L2_TUNER_MODE_STEREO ] = 0x80, /* auto */
447 [ V4L2_TUNER_MODE_LANG1 ] = 0x00, /* ch1 */
448 [ V4L2_TUNER_MODE_LANG2 ] = 0x01, /* ch2 */
449 [ V4L2_TUNER_MODE_LANG1_LANG2 ] = 0x80, /* auto */
453 switch (audio->mode) {
454 case TVAUDIO_FM_MONO:
455 /* nothing to do ... */
457 case TVAUDIO_FM_K_STEREO:
458 case TVAUDIO_FM_BG_STEREO:
459 case TVAUDIO_NICAM_AM:
460 case TVAUDIO_NICAM_FM:
461 dprintk("setstereo [fm] => %s\n",
462 name[ mode % ARRAY_SIZE(name) ]);
463 reg = fm[ mode % ARRAY_SIZE(fm) ];
464 saa_writeb(SAA7134_FM_DEMATRIX, reg);
466 case TVAUDIO_FM_SAT_STEREO:
467 /* Not implemented */
473 static int tvaudio_thread(void *data)
475 struct saa7134_dev *dev = data;
476 int carr_vals[ARRAY_SIZE(mainscan)];
477 unsigned int i, audio, nscan;
478 int max1,max2,carrier,rx,mode,lastmode,default_carrier;
483 tvaudio_sleep(dev,-1);
484 if (kthread_should_stop())
490 dev->thread.scan1 = dev->thread.scan2;
491 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
494 saa_writeb(SAA7134_MONITOR_SELECT, 0xa0);
495 saa_writeb(SAA7134_FM_DEMATRIX, 0x80);
497 if (dev->ctl_automute)
500 mute_input_7134(dev);
502 /* give the tuner some time */
503 if (tvaudio_sleep(dev,SCAN_INITIAL_DELAY))
511 for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
512 if (!(dev->tvnorm->id & mainscan[i].std))
514 if (!default_carrier)
515 default_carrier = mainscan[i].carr;
520 /* only one candidate -- skip scan ;) */
521 dprintk("only one main carrier candidate - skipping scan\n");
523 carrier = default_carrier;
525 /* scan for the main carrier */
526 saa_writeb(SAA7134_MONITOR_SELECT,0x00);
527 tvaudio_setmode(dev,&tvaudio[0],NULL);
528 for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
529 carr_vals[i] = tvaudio_checkcarrier(dev, mainscan+i);
530 if (dev->thread.scan1 != dev->thread.scan2)
533 for (max1 = 0, max2 = 0, i = 0; i < ARRAY_SIZE(mainscan); i++) {
534 if (max1 < carr_vals[i]) {
537 carrier = mainscan[i].carr;
538 } else if (max2 < carr_vals[i]) {
544 if (0 != carrier && max1 > 2000 && max1 > max2*3) {
545 /* found good carrier */
546 dprintk("found %s main sound carrier @ %d.%03d MHz [%d/%d]\n",
547 dev->tvnorm->name, carrier/1000, carrier%1000,
549 dev->last_carrier = carrier;
551 } else if (0 != dev->last_carrier) {
552 /* no carrier -- try last detected one as fallback */
553 carrier = dev->last_carrier;
554 dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
555 "using %d.%03d MHz [last detected]\n",
556 dev->name, carrier/1000, carrier%1000);
559 /* no carrier + no fallback -- use default */
560 carrier = default_carrier;
561 dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
562 "using %d.%03d MHz [default]\n",
563 dev->name, carrier/1000, carrier%1000);
565 tvaudio_setcarrier(dev,carrier,carrier);
567 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x00);
568 saa7134_tvaudio_setmute(dev);
569 /* find the exact tv audio norm */
570 for (audio = UNSET, i = 0; i < TVAUDIO; i++) {
571 if (dev->tvnorm->id != UNSET &&
572 !(dev->tvnorm->id & tvaudio[i].std))
574 if (tvaudio[i].carr1 != carrier)
576 /* Note: at least the primary carrier is right here */
579 tvaudio_setmode(dev,&tvaudio[i],"trying");
580 if (tvaudio_sleep(dev,SCAN_SUBCARRIER_DELAY))
582 if (-1 != tvaudio_getstereo(dev,&tvaudio[i])) {
587 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x30);
590 tvaudio_setmode(dev,&tvaudio[audio],"using");
592 tvaudio_setstereo(dev,&tvaudio[audio],V4L2_TUNER_MODE_MONO);
593 dev->tvaudio = &tvaudio[audio];
600 if (tvaudio_sleep(dev,5000))
602 if (kthread_should_stop())
604 if (UNSET == dev->thread.mode) {
605 rx = tvaudio_getstereo(dev,&tvaudio[i]);
606 mode = saa7134_tvaudio_rx2mode(rx);
608 mode = dev->thread.mode;
610 if (lastmode != mode) {
611 tvaudio_setstereo(dev,&tvaudio[audio],mode);
618 dev->thread.stopped = 1;
622 /* ------------------------------------------------------------------ */
623 /* saa7133 / saa7135 code */
625 static char *stdres[0x20] = {
626 [0x00] = "no standard detected",
627 [0x01] = "B/G (in progress)",
628 [0x02] = "D/K (in progress)",
629 [0x03] = "M (in progress)",
632 [0x05] = "B/G NICAM",
633 [0x06] = "D/K A2 (1)",
634 [0x07] = "D/K A2 (2)",
635 [0x08] = "D/K A2 (3)",
636 [0x09] = "D/K NICAM",
644 [0x0f] = "FM radio / IF 10.7 / 50 deemp",
645 [0x10] = "FM radio / IF 10.7 / 75 deemp",
646 [0x11] = "FM radio / IF sel / 50 deemp",
647 [0x12] = "FM radio / IF sel / 75 deemp",
649 [0x13 ... 0x1e ] = "unknown",
650 [0x1f] = "??? [in progress]",
655 #define SAA7135_DSP_RWCLEAR_RERR 1
657 static inline int saa_dsp_reset_error_bit(struct saa7134_dev *dev)
659 int state = saa_readb(SAA7135_DSP_RWSTATE);
660 if (unlikely(state & SAA7135_DSP_RWSTATE_ERR)) {
661 d2printk("%s: resetting error bit\n", dev->name);
662 saa_writeb(SAA7135_DSP_RWCLEAR, SAA7135_DSP_RWCLEAR_RERR);
667 static inline int saa_dsp_wait_bit(struct saa7134_dev *dev, int bit)
669 int state, count = DSP_RETRY;
671 state = saa_readb(SAA7135_DSP_RWSTATE);
672 if (unlikely(state & SAA7135_DSP_RWSTATE_ERR)) {
673 printk(KERN_WARNING "%s: dsp access error\n", dev->name);
674 saa_dsp_reset_error_bit(dev);
677 while (0 == (state & bit)) {
678 if (unlikely(0 == count)) {
679 printk("%s: dsp access wait timeout [bit=%s]\n",
681 (bit & SAA7135_DSP_RWSTATE_WRR) ? "WRR" :
682 (bit & SAA7135_DSP_RWSTATE_RDB) ? "RDB" :
683 (bit & SAA7135_DSP_RWSTATE_IDA) ? "IDA" :
688 state = saa_readb(SAA7135_DSP_RWSTATE);
695 int saa_dsp_writel(struct saa7134_dev *dev, int reg, u32 value)
699 d2printk("dsp write reg 0x%x = 0x%06x\n",reg<<2,value);
700 err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
703 saa_writel(reg,value);
704 err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
710 static int getstereo_7133(struct saa7134_dev *dev)
712 int retval = V4L2_TUNER_SUB_MONO;
715 value = saa_readl(0x528 >> 2);
717 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
719 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
723 static int mute_input_7133(struct saa7134_dev *dev)
728 struct saa7134_input *in;
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;
779 tvaudio_sleep(dev,-1);
780 if (kthread_should_stop())
785 dev->thread.scan1 = dev->thread.scan2;
786 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
788 if (audio_ddep >= 0x04 && audio_ddep <= 0x0e) {
789 /* insmod option override */
790 norms = (audio_ddep << 2) | 0x01;
791 dprintk("ddep override: %s\n",stdres[audio_ddep]);
792 } else if (&card(dev).radio == dev->input) {
793 dprintk("FM Radio\n");
794 if (dev->tuner_type == TUNER_PHILIPS_TDA8290) {
795 norms = (0x11 << 2) | 0x01;
796 saa_dsp_writel(dev, 0x42c >> 2, 0x729555);
798 norms = (0x0f << 2) | 0x01;
801 /* (let chip) scan for sound carrier */
803 if (dev->tvnorm->id & (V4L2_STD_B | V4L2_STD_GH))
805 if (dev->tvnorm->id & V4L2_STD_PAL_I)
807 if (dev->tvnorm->id & V4L2_STD_DK)
809 if (dev->tvnorm->id & V4L2_STD_MN)
811 if (dev->tvnorm->id & (V4L2_STD_SECAM_L | V4L2_STD_SECAM_LC))
814 norms = 0x7c; /* all */
815 dprintk("scanning:%s%s%s%s%s\n",
816 (norms & 0x04) ? " B/G" : "",
817 (norms & 0x08) ? " D/K" : "",
818 (norms & 0x10) ? " L/L'" : "",
819 (norms & 0x20) ? " I" : "",
820 (norms & 0x40) ? " M" : "");
823 /* kick automatic standard detection */
824 saa_dsp_writel(dev, 0x454 >> 2, 0);
825 saa_dsp_writel(dev, 0x454 >> 2, norms | 0x80);
827 /* setup crossbars */
828 saa_dsp_writel(dev, 0x464 >> 2, 0x000000);
829 saa_dsp_writel(dev, 0x470 >> 2, 0x101010);
831 if (tvaudio_sleep(dev,3000))
833 value = saa_readl(0x528 >> 2) & 0xffffff;
835 dprintk("tvaudio thread status: 0x%x [%s%s%s]\n",
836 value, stdres[value & 0x1f],
837 (value & 0x000020) ? ",stereo" : "",
838 (value & 0x000040) ? ",dual" : "");
839 dprintk("detailed status: "
840 "%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n",
841 (value & 0x000080) ? " A2/EIAJ pilot tone " : "",
842 (value & 0x000100) ? " A2/EIAJ dual " : "",
843 (value & 0x000200) ? " A2/EIAJ stereo " : "",
844 (value & 0x000400) ? " A2/EIAJ noise mute " : "",
846 (value & 0x000800) ? " BTSC/FM radio pilot " : "",
847 (value & 0x001000) ? " SAP carrier " : "",
848 (value & 0x002000) ? " BTSC stereo noise mute " : "",
849 (value & 0x004000) ? " SAP noise mute " : "",
850 (value & 0x008000) ? " VDSP " : "",
852 (value & 0x010000) ? " NICST " : "",
853 (value & 0x020000) ? " NICDU " : "",
854 (value & 0x040000) ? " NICAM muted " : "",
855 (value & 0x080000) ? " NICAM reserve sound " : "",
857 (value & 0x100000) ? " init done " : "");
861 dev->thread.stopped = 1;
865 /* ------------------------------------------------------------------ */
866 /* common stuff + external entry points */
868 void saa7134_enable_i2s(struct saa7134_dev *dev)
872 if (!card_is_empress(dev))
875 if (dev->pci->device == PCI_DEVICE_ID_PHILIPS_SAA7130)
878 /* configure GPIO for out */
879 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2, 0x0E000000, 0x00000000);
881 switch (dev->pci->device) {
882 case PCI_DEVICE_ID_PHILIPS_SAA7133:
883 case PCI_DEVICE_ID_PHILIPS_SAA7135:
884 /* Set I2S format (SONY) */
885 saa_writeb(SAA7133_I2S_AUDIO_CONTROL, 0x00);
887 saa_writeb(SAA7134_I2S_AUDIO_OUTPUT, 0x11);
890 case PCI_DEVICE_ID_PHILIPS_SAA7134:
891 i2s_format = (dev->input->amux == TV) ? 0x00 : 0x01;
893 /* enable I2S audio output for the mpeg encoder */
894 saa_writeb(SAA7134_I2S_OUTPUT_SELECT, 0x80);
895 saa_writeb(SAA7134_I2S_OUTPUT_FORMAT, i2s_format);
896 saa_writeb(SAA7134_I2S_OUTPUT_LEVEL, 0x0F);
897 saa_writeb(SAA7134_I2S_AUDIO_OUTPUT, 0x01);
904 int saa7134_tvaudio_rx2mode(u32 rx)
908 mode = V4L2_TUNER_MODE_MONO;
909 if (rx & V4L2_TUNER_SUB_STEREO)
910 mode = V4L2_TUNER_MODE_STEREO;
911 else if (rx & V4L2_TUNER_SUB_LANG1)
912 mode = V4L2_TUNER_MODE_LANG1;
913 else if (rx & V4L2_TUNER_SUB_LANG2)
914 mode = V4L2_TUNER_MODE_LANG2;
918 void saa7134_tvaudio_setmute(struct saa7134_dev *dev)
920 switch (dev->pci->device) {
921 case PCI_DEVICE_ID_PHILIPS_SAA7130:
922 case PCI_DEVICE_ID_PHILIPS_SAA7134:
923 mute_input_7134(dev);
925 case PCI_DEVICE_ID_PHILIPS_SAA7133:
926 case PCI_DEVICE_ID_PHILIPS_SAA7135:
927 mute_input_7133(dev);
932 void saa7134_tvaudio_setinput(struct saa7134_dev *dev,
933 struct saa7134_input *in)
936 switch (dev->pci->device) {
937 case PCI_DEVICE_ID_PHILIPS_SAA7130:
938 case PCI_DEVICE_ID_PHILIPS_SAA7134:
939 mute_input_7134(dev);
941 case PCI_DEVICE_ID_PHILIPS_SAA7133:
942 case PCI_DEVICE_ID_PHILIPS_SAA7135:
943 mute_input_7133(dev);
946 saa7134_enable_i2s(dev);
949 void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level)
951 switch (dev->pci->device) {
952 case PCI_DEVICE_ID_PHILIPS_SAA7134:
953 saa_writeb(SAA7134_CHANNEL1_LEVEL, level & 0x1f);
954 saa_writeb(SAA7134_CHANNEL2_LEVEL, level & 0x1f);
955 saa_writeb(SAA7134_NICAM_LEVEL_ADJUST, level & 0x1f);
960 int saa7134_tvaudio_getstereo(struct saa7134_dev *dev)
962 int retval = V4L2_TUNER_SUB_MONO;
964 switch (dev->pci->device) {
965 case PCI_DEVICE_ID_PHILIPS_SAA7134:
967 retval = tvaudio_getstereo(dev,dev->tvaudio);
969 case PCI_DEVICE_ID_PHILIPS_SAA7133:
970 case PCI_DEVICE_ID_PHILIPS_SAA7135:
971 retval = getstereo_7133(dev);
977 void saa7134_tvaudio_init(struct saa7134_dev *dev)
979 int clock = saa7134_boards[dev->board].audio_clock;
981 if (UNSET != audio_clock_override)
982 clock = audio_clock_override;
984 switch (dev->pci->device) {
985 case PCI_DEVICE_ID_PHILIPS_SAA7134:
986 /* init all audio registers */
987 saa_writeb(SAA7134_AUDIO_PLL_CTRL, 0x00);
993 saa_writeb(SAA7134_AUDIO_CLOCK0, clock & 0xff);
994 saa_writeb(SAA7134_AUDIO_CLOCK1, (clock >> 8) & 0xff);
995 saa_writeb(SAA7134_AUDIO_CLOCK2, (clock >> 16) & 0xff);
996 /* frame locked audio is mandatory for NICAM */
997 saa_writeb(SAA7134_AUDIO_PLL_CTRL, 0x01);
998 saa_writeb(SAA7134_NICAM_ERROR_LOW, 0x14);
999 saa_writeb(SAA7134_NICAM_ERROR_HIGH, 0x50);
1001 case PCI_DEVICE_ID_PHILIPS_SAA7133:
1002 case PCI_DEVICE_ID_PHILIPS_SAA7135:
1003 saa_writel(0x598 >> 2, clock);
1004 saa_dsp_writel(dev, 0x474 >> 2, 0x00);
1005 saa_dsp_writel(dev, 0x450 >> 2, 0x00);
1009 int saa7134_tvaudio_init2(struct saa7134_dev *dev)
1011 int (*my_thread)(void *data) = NULL;
1013 switch (dev->pci->device) {
1014 case PCI_DEVICE_ID_PHILIPS_SAA7134:
1015 my_thread = tvaudio_thread;
1017 case PCI_DEVICE_ID_PHILIPS_SAA7133:
1018 case PCI_DEVICE_ID_PHILIPS_SAA7135:
1019 my_thread = tvaudio_thread_ddep;
1023 dev->thread.thread = NULL;
1025 saa7134_tvaudio_init(dev);
1026 /* start tvaudio thread */
1027 dev->thread.thread = kthread_run(my_thread, dev, "%s", dev->name);
1028 if (IS_ERR(dev->thread.thread)) {
1029 printk(KERN_WARNING "%s: kernel_thread() failed\n",
1031 /* XXX: missing error handling here */
1033 saa7134_tvaudio_do_scan(dev);
1036 saa7134_enable_i2s(dev);
1040 int saa7134_tvaudio_fini(struct saa7134_dev *dev)
1042 /* shutdown tvaudio thread */
1043 if (dev->thread.thread && !dev->thread.stopped)
1044 kthread_stop(dev->thread.thread);
1046 saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, 0x00); /* LINE1 */
1050 int saa7134_tvaudio_do_scan(struct saa7134_dev *dev)
1052 if (dev->input->amux != TV) {
1053 dprintk("sound IF not in use, skipping scan\n");
1055 saa7134_tvaudio_setmute(dev);
1056 } else if (dev->thread.thread) {
1057 dev->thread.mode = UNSET;
1058 dev->thread.scan2++;
1060 if (!dev->insuspend && !dev->thread.stopped)
1061 wake_up_process(dev->thread.thread);
1064 saa7134_tvaudio_setmute(dev);
1069 EXPORT_SYMBOL(saa_dsp_writel);
1070 EXPORT_SYMBOL(saa7134_tvaudio_setmute);
1072 /* ----------------------------------------------------------- */