Automatic merge with /usr/src/ntfs-2.6.git.
[linux-2.6] / drivers / media / video / saa7134 / saa7134-tvaudio.c
1 /*
2  * $Id: saa7134-tvaudio.c,v 1.30 2005/06/28 23:41:47 mkrufky Exp $
3  *
4  * device driver for philips saa7134 based TV cards
5  * tv audio decoder (fm stereo, nicam, ...)
6  *
7  * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or
12  *  (at your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful,
15  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *  GNU General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License
20  *  along with this program; if not, write to the Free Software
21  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #include <linux/init.h>
25 #include <linux/list.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <linux/smp_lock.h>
32 #include <asm/div64.h>
33
34 #include "saa7134-reg.h"
35 #include "saa7134.h"
36
37 /* ------------------------------------------------------------------ */
38
39 static unsigned int audio_debug = 0;
40 module_param(audio_debug, int, 0644);
41 MODULE_PARM_DESC(audio_debug,"enable debug messages [tv audio]");
42
43 static unsigned int audio_ddep = 0;
44 module_param(audio_ddep, int, 0644);
45 MODULE_PARM_DESC(audio_ddep,"audio ddep overwrite");
46
47 static int audio_clock_override = UNSET;
48 module_param(audio_clock_override, int, 0644);
49
50 static int audio_clock_tweak = 0;
51 module_param(audio_clock_tweak, int, 0644);
52 MODULE_PARM_DESC(audio_clock_tweak, "Audio clock tick fine tuning for cards with audio crystal that's slightly off (range [-1024 .. 1024])");
53
54 #define dprintk(fmt, arg...)    if (audio_debug) \
55         printk(KERN_DEBUG "%s/audio: " fmt, dev->name , ## arg)
56 #define d2printk(fmt, arg...)   if (audio_debug > 1) \
57         printk(KERN_DEBUG "%s/audio: " fmt, dev->name, ## arg)
58
59 #define print_regb(reg) printk("%s:   reg 0x%03x [%-16s]: 0x%02x\n", \
60                 dev->name,(SAA7134_##reg),(#reg),saa_readb((SAA7134_##reg)))
61
62 /* msecs */
63 #define SCAN_INITIAL_DELAY     1000
64 #define SCAN_SAMPLE_DELAY       200
65 #define SCAN_SUBCARRIER_DELAY  2000
66
67 /* ------------------------------------------------------------------ */
68 /* saa7134 code                                                       */
69
70 static struct mainscan {
71         char         *name;
72         v4l2_std_id  std;
73         int          carr;
74 } mainscan[] = {
75         {
76                 .name = "M",
77                 .std  = V4L2_STD_NTSC | V4L2_STD_PAL_M,
78                 .carr = 4500,
79         },{
80                 .name = "BG",
81                 .std  = V4L2_STD_PAL_BG,
82                 .carr = 5500,
83         },{
84                 .name = "I",
85                 .std  = V4L2_STD_PAL_I,
86                 .carr = 6000,
87         },{
88                 .name = "DKL",
89                 .std  = V4L2_STD_PAL_DK | V4L2_STD_SECAM,
90                 .carr = 6500,
91         }
92 };
93
94 static struct saa7134_tvaudio tvaudio[] = {
95         {
96                 .name          = "PAL-B/G FM-stereo",
97                 .std           = V4L2_STD_PAL,
98                 .mode          = TVAUDIO_FM_BG_STEREO,
99                 .carr1         = 5500,
100                 .carr2         = 5742,
101         },{
102                 .name          = "PAL-D/K1 FM-stereo",
103                 .std           = V4L2_STD_PAL,
104                 .carr1         = 6500,
105                 .carr2         = 6258,
106                 .mode          = TVAUDIO_FM_BG_STEREO,
107         },{
108                 .name          = "PAL-D/K2 FM-stereo",
109                 .std           = V4L2_STD_PAL,
110                 .carr1         = 6500,
111                 .carr2         = 6742,
112                 .mode          = TVAUDIO_FM_BG_STEREO,
113         },{
114                 .name          = "PAL-D/K3 FM-stereo",
115                 .std           = V4L2_STD_PAL,
116                 .carr1         = 6500,
117                 .carr2         = 5742,
118                 .mode          = TVAUDIO_FM_BG_STEREO,
119         },{
120                 .name          = "PAL-B/G NICAM",
121                 .std           = V4L2_STD_PAL,
122                 .carr1         = 5500,
123                 .carr2         = 5850,
124                 .mode          = TVAUDIO_NICAM_FM,
125         },{
126                 .name          = "PAL-I NICAM",
127                 .std           = V4L2_STD_PAL,
128                 .carr1         = 6000,
129                 .carr2         = 6552,
130                 .mode          = TVAUDIO_NICAM_FM,
131         },{
132                 .name          = "PAL-D/K NICAM",
133                 .std           = V4L2_STD_PAL,
134                 .carr1         = 6500,
135                 .carr2         = 5850,
136                 .mode          = TVAUDIO_NICAM_FM,
137         },{
138                 .name          = "SECAM-L NICAM",
139                 .std           = V4L2_STD_SECAM,
140                 .carr1         = 6500,
141                 .carr2         = 5850,
142                 .mode          = TVAUDIO_NICAM_AM,
143         },{
144                 .name          = "SECAM-D/K",
145                 .std           = V4L2_STD_SECAM,
146                 .carr1         = 6500,
147                 .carr2         = -1,
148                 .mode          = TVAUDIO_FM_MONO,
149         },{
150                 .name          = "NTSC-M",
151                 .std           = V4L2_STD_NTSC,
152                 .carr1         = 4500,
153                 .carr2         = -1,
154                 .mode          = TVAUDIO_FM_MONO,
155         },{
156                 .name          = "NTSC-A2 FM-stereo",
157                 .std           = V4L2_STD_NTSC,
158                 .carr1         = 4500,
159                 .carr2         = 4724,
160                 .mode          = TVAUDIO_FM_K_STEREO,
161         }
162 };
163 #define TVAUDIO (sizeof(tvaudio)/sizeof(struct saa7134_tvaudio))
164
165 /* ------------------------------------------------------------------ */
166
167 static void tvaudio_init(struct saa7134_dev *dev)
168 {
169         int clock = saa7134_boards[dev->board].audio_clock;
170
171         if (UNSET != audio_clock_override)
172                 clock = audio_clock_override;
173
174         /* init all audio registers */
175         saa_writeb(SAA7134_AUDIO_PLL_CTRL,   0x00);
176         if (need_resched())
177                 schedule();
178         else
179                 udelay(10);
180
181         saa_writeb(SAA7134_AUDIO_CLOCK0,      clock        & 0xff);
182         saa_writeb(SAA7134_AUDIO_CLOCK1,     (clock >>  8) & 0xff);
183         saa_writeb(SAA7134_AUDIO_CLOCK2,     (clock >> 16) & 0xff);
184         // frame locked audio was reported not to be reliable
185         saa_writeb(SAA7134_AUDIO_PLL_CTRL,   0x02);
186
187         saa_writeb(SAA7134_NICAM_ERROR_LOW,  0x14);
188         saa_writeb(SAA7134_NICAM_ERROR_HIGH, 0x50);
189         saa_writeb(SAA7134_MONITOR_SELECT,   0xa0);
190         saa_writeb(SAA7134_FM_DEMATRIX,      0x80);
191 }
192
193 static u32 tvaudio_carr2reg(u32 carrier)
194 {
195         u64 a = carrier;
196
197         a <<= 24;
198         do_div(a,12288);
199         return a;
200 }
201
202 static void tvaudio_setcarrier(struct saa7134_dev *dev,
203                                int primary, int secondary)
204 {
205         if (-1 == secondary)
206                 secondary = primary;
207         saa_writel(SAA7134_CARRIER1_FREQ0 >> 2, tvaudio_carr2reg(primary));
208         saa_writel(SAA7134_CARRIER2_FREQ0 >> 2, tvaudio_carr2reg(secondary));
209 }
210
211 static void mute_input_7134(struct saa7134_dev *dev)
212 {
213         unsigned int mute;
214         struct saa7134_input *in;
215         int ausel=0, ics=0, ocs=0;
216         int mask;
217
218         /* look what is to do ... */
219         in   = dev->input;
220         mute = (dev->ctl_mute ||
221                 (dev->automute  &&  (&card(dev).radio) != in));
222         if (card(dev).mute.name) {
223                 /*
224                  * 7130 - we'll mute using some unconnected audio input
225                  * 7134 - we'll probably should switch external mux with gpio
226                  */
227                 if (mute)
228                         in = &card(dev).mute;
229         }
230
231         if (dev->hw_mute  == mute &&
232                 dev->hw_input == in) {
233                 dprintk("mute/input: nothing to do [mute=%d,input=%s]\n",
234                         mute,in->name);
235                 return;
236         }
237
238         dprintk("ctl_mute=%d automute=%d input=%s  =>  mute=%d input=%s\n",
239                 dev->ctl_mute,dev->automute,dev->input->name,mute,in->name);
240         dev->hw_mute  = mute;
241         dev->hw_input = in;
242
243         if (PCI_DEVICE_ID_PHILIPS_SAA7134 == dev->pci->device)
244                 /* 7134 mute */
245                 saa_writeb(SAA7134_AUDIO_MUTE_CTRL, mute ? 0xbf : 0xbb);
246
247         /* switch internal audio mux */
248         switch (in->amux) {
249         case TV:         ausel=0xc0; ics=0x00; ocs=0x02; break;
250         case LINE1:      ausel=0x80; ics=0x00; ocs=0x00; break;
251         case LINE2:      ausel=0x80; ics=0x08; ocs=0x01; break;
252         case LINE2_LEFT: ausel=0x80; ics=0x08; ocs=0x05; break;
253         }
254         saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, ausel);
255         saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, ics);
256         saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, ocs);
257         // for oss, we need to change the clock configuration
258         if (in->amux == TV)
259                 saa_andorb(SAA7134_SIF_SAMPLE_FREQ,   0x03, 0x00);
260         else
261                 saa_andorb(SAA7134_SIF_SAMPLE_FREQ,   0x03, 0x01);
262
263         /* switch gpio-connected external audio mux */
264         if (0 == card(dev).gpiomask)
265                 return;
266
267         mask = card(dev).gpiomask;
268         saa_andorl(SAA7134_GPIO_GPMODE0 >> 2,   mask, mask);
269         saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio);
270         saa7134_track_gpio(dev,in->name);
271 }
272
273 static void tvaudio_setmode(struct saa7134_dev *dev,
274                             struct saa7134_tvaudio *audio,
275                             char *note)
276 {
277         int acpf, tweak = 0;
278
279         if (dev->tvnorm->id == V4L2_STD_NTSC) {
280                 acpf = 0x19066;
281         } else {
282                 acpf = 0x1e000;
283         }
284         if (audio_clock_tweak > -1024 && audio_clock_tweak < 1024)
285                 tweak = audio_clock_tweak;
286
287         if (note)
288                 dprintk("tvaudio_setmode: %s %s [%d.%03d/%d.%03d MHz] acpf=%d%+d\n",
289                         note,audio->name,
290                         audio->carr1 / 1000, audio->carr1 % 1000,
291                         audio->carr2 / 1000, audio->carr2 % 1000,
292                         acpf, tweak);
293
294         acpf += tweak;
295         saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD0, (acpf & 0x0000ff) >> 0);
296         saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD1, (acpf & 0x00ff00) >> 8);
297         saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD2, (acpf & 0x030000) >> 16);
298         tvaudio_setcarrier(dev,audio->carr1,audio->carr2);
299
300         switch (audio->mode) {
301         case TVAUDIO_FM_MONO:
302         case TVAUDIO_FM_BG_STEREO:
303                 saa_writeb(SAA7134_DEMODULATOR,               0x00);
304                 saa_writeb(SAA7134_DCXO_IDENT_CTRL,           0x00);
305                 saa_writeb(SAA7134_FM_DEEMPHASIS,             0x22);
306                 saa_writeb(SAA7134_FM_DEMATRIX,               0x80);
307                 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT,  0xa0);
308                 break;
309         case TVAUDIO_FM_K_STEREO:
310                 saa_writeb(SAA7134_DEMODULATOR,               0x00);
311                 saa_writeb(SAA7134_DCXO_IDENT_CTRL,           0x01);
312                 saa_writeb(SAA7134_FM_DEEMPHASIS,             0x22);
313                 saa_writeb(SAA7134_FM_DEMATRIX,               0x80);
314                 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT,  0xa0);
315                 break;
316         case TVAUDIO_NICAM_FM:
317                 saa_writeb(SAA7134_DEMODULATOR,               0x10);
318                 saa_writeb(SAA7134_DCXO_IDENT_CTRL,           0x00);
319                 saa_writeb(SAA7134_FM_DEEMPHASIS,             0x44);
320                 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT,  0xa1);
321                 saa_writeb(SAA7134_NICAM_CONFIG,              0x00);
322                 break;
323         case TVAUDIO_NICAM_AM:
324                 saa_writeb(SAA7134_DEMODULATOR,               0x12);
325                 saa_writeb(SAA7134_DCXO_IDENT_CTRL,           0x00);
326                 saa_writeb(SAA7134_FM_DEEMPHASIS,             0x44);
327                 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT,  0xa1);
328                 saa_writeb(SAA7134_NICAM_CONFIG,              0x00);
329                 break;
330         case TVAUDIO_FM_SAT_STEREO:
331                 /* not implemented (yet) */
332                 break;
333         }
334 }
335
336 static int tvaudio_sleep(struct saa7134_dev *dev, int timeout)
337 {
338         DECLARE_WAITQUEUE(wait, current);
339
340         add_wait_queue(&dev->thread.wq, &wait);
341         if (dev->thread.scan1 == dev->thread.scan2 && !dev->thread.shutdown) {
342                 if (timeout < 0) {
343                         set_current_state(TASK_INTERRUPTIBLE);
344                         schedule();
345                 } else {
346                         set_current_state(TASK_INTERRUPTIBLE);
347                         schedule_timeout(msecs_to_jiffies(timeout));
348                 }
349         }
350         remove_wait_queue(&dev->thread.wq, &wait);
351         return dev->thread.scan1 != dev->thread.scan2;
352 }
353
354 static int tvaudio_checkcarrier(struct saa7134_dev *dev, struct mainscan *scan)
355 {
356         __s32 left,right,value;
357
358         if (audio_debug > 1) {
359                 int i;
360                 dprintk("debug %d:",scan->carr);
361                 for (i = -150; i <= 150; i += 30) {
362                         tvaudio_setcarrier(dev,scan->carr+i,scan->carr+i);
363                         saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
364                         if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
365                                 return -1;
366                         value = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
367                         if (0 == i)
368                                 printk("  #  %6d  # ",value >> 16);
369                         else
370                                 printk(" %6d",value >> 16);
371                 }
372                 printk("\n");
373         }
374
375         if (dev->tvnorm->id & scan->std) {
376                 tvaudio_setcarrier(dev,scan->carr-90,scan->carr-90);
377                 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
378                 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
379                         return -1;
380                 left = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
381
382                 tvaudio_setcarrier(dev,scan->carr+90,scan->carr+90);
383                 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
384                 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
385                         return -1;
386                 right = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
387
388                 left >>= 16;
389                 right >>= 16;
390                 value = left > right ? left - right : right - left;
391                 dprintk("scanning %d.%03d MHz [%4s] =>  dc is %5d [%d/%d]\n",
392                         scan->carr / 1000, scan->carr % 1000,
393                         scan->name, value, left, right);
394         } else {
395                 value = 0;
396                 dprintk("skipping %d.%03d MHz [%4s]\n",
397                         scan->carr / 1000, scan->carr % 1000, scan->name);
398         }
399         return value;
400 }
401
402
403 static int tvaudio_getstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio)
404 {
405         __u32 idp, nicam, nicam_status;
406         int retval = -1;
407
408         switch (audio->mode) {
409         case TVAUDIO_FM_MONO:
410                 return V4L2_TUNER_SUB_MONO;
411         case TVAUDIO_FM_K_STEREO:
412         case TVAUDIO_FM_BG_STEREO:
413                 idp = (saa_readb(SAA7134_IDENT_SIF) & 0xe0) >> 5;
414                 dprintk("getstereo: fm/stereo: idp=0x%x\n",idp);
415                 if (0x03 == (idp & 0x03))
416                         retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
417                 else if (0x05 == (idp & 0x05))
418                         retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
419                 else if (0x01 == (idp & 0x01))
420                         retval = V4L2_TUNER_SUB_MONO;
421                 break;
422         case TVAUDIO_FM_SAT_STEREO:
423                 /* not implemented (yet) */
424                 break;
425         case TVAUDIO_NICAM_FM:
426         case TVAUDIO_NICAM_AM:
427                 nicam = saa_readb(SAA7134_AUDIO_STATUS);
428                 dprintk("getstereo: nicam=0x%x\n",nicam);
429                 if (nicam & 0x1) {
430                         nicam_status = saa_readb(SAA7134_NICAM_STATUS);
431                         dprintk("getstereo: nicam_status=0x%x\n", nicam_status);
432
433                         switch (nicam_status & 0x03) {
434                             case 0x01:
435                                 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
436                                 break;
437                             case 0x02:
438                                 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
439                                 break;
440                             default:
441                                 retval = V4L2_TUNER_SUB_MONO;
442                         }
443                 } else {
444                         /* No nicam detected */
445                 }
446                 break;
447         }
448         if (retval != -1)
449                 dprintk("found audio subchannels:%s%s%s%s\n",
450                         (retval & V4L2_TUNER_SUB_MONO)   ? " mono"   : "",
451                         (retval & V4L2_TUNER_SUB_STEREO) ? " stereo" : "",
452                         (retval & V4L2_TUNER_SUB_LANG1)  ? " lang1"  : "",
453                         (retval & V4L2_TUNER_SUB_LANG2)  ? " lang2"  : "");
454         return retval;
455 }
456
457 static int tvaudio_setstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio,
458                              u32 mode)
459 {
460         static char *name[] = {
461                 [ V4L2_TUNER_MODE_MONO   ] = "mono",
462                 [ V4L2_TUNER_MODE_STEREO ] = "stereo",
463                 [ V4L2_TUNER_MODE_LANG1  ] = "lang1",
464                 [ V4L2_TUNER_MODE_LANG2  ] = "lang2",
465         };
466         static u32 fm[] = {
467                 [ V4L2_TUNER_MODE_MONO   ] = 0x00,  /* ch1  */
468                 [ V4L2_TUNER_MODE_STEREO ] = 0x80,  /* auto */
469                 [ V4L2_TUNER_MODE_LANG1  ] = 0x00,  /* ch1  */
470                 [ V4L2_TUNER_MODE_LANG2  ] = 0x01,  /* ch2  */
471         };
472         u32 reg;
473
474         switch (audio->mode) {
475         case TVAUDIO_FM_MONO:
476                 /* nothing to do ... */
477                 break;
478         case TVAUDIO_FM_K_STEREO:
479         case TVAUDIO_FM_BG_STEREO:
480         case TVAUDIO_NICAM_AM:
481         case TVAUDIO_NICAM_FM:
482                 dprintk("setstereo [fm] => %s\n",
483                         name[ mode % ARRAY_SIZE(name) ]);
484                 reg = fm[ mode % ARRAY_SIZE(fm) ];
485                 saa_writeb(SAA7134_FM_DEMATRIX, reg);
486                 break;
487         case TVAUDIO_FM_SAT_STEREO:
488                 /* Not implemented */
489                 break;
490         }
491         return 0;
492 }
493
494 static int tvaudio_thread(void *data)
495 {
496         struct saa7134_dev *dev = data;
497         int carr_vals[ARRAY_SIZE(mainscan)];
498         unsigned int i, audio, nscan;
499         int max1,max2,carrier,rx,mode,lastmode,default_carrier;
500
501         daemonize("%s", dev->name);
502         allow_signal(SIGTERM);
503         for (;;) {
504                 tvaudio_sleep(dev,-1);
505                 if (dev->thread.shutdown || signal_pending(current))
506                         goto done;
507
508         restart:
509                 dev->thread.scan1 = dev->thread.scan2;
510                 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
511                 dev->tvaudio  = NULL;
512                 tvaudio_init(dev);
513                 if (dev->ctl_automute)
514                         dev->automute = 1;
515                 mute_input_7134(dev);
516
517                 /* give the tuner some time */
518                 if (tvaudio_sleep(dev,SCAN_INITIAL_DELAY))
519                         goto restart;
520
521                 max1 = 0;
522                 max2 = 0;
523                 nscan = 0;
524                 carrier = 0;
525                 default_carrier = 0;
526                 for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
527                         if (!(dev->tvnorm->id & mainscan[i].std))
528                                 continue;
529                         if (!default_carrier)
530                                 default_carrier = mainscan[i].carr;
531                         nscan++;
532                 }
533
534                 if (1 == nscan) {
535                         /* only one candidate -- skip scan ;) */
536                         max1 = 12345;
537                         carrier = default_carrier;
538                 } else {
539                         /* scan for the main carrier */
540                         saa_writeb(SAA7134_MONITOR_SELECT,0x00);
541                         tvaudio_setmode(dev,&tvaudio[0],NULL);
542                         for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
543                                 carr_vals[i] = tvaudio_checkcarrier(dev, mainscan+i);
544                                 if (dev->thread.scan1 != dev->thread.scan2)
545                                         goto restart;
546                         }
547                         for (max1 = 0, max2 = 0, i = 0; i < ARRAY_SIZE(mainscan); i++) {
548                                 if (max1 < carr_vals[i]) {
549                                         max2 = max1;
550                                         max1 = carr_vals[i];
551                                         carrier = mainscan[i].carr;
552                                 } else if (max2 < carr_vals[i]) {
553                                         max2 = carr_vals[i];
554                                 }
555                         }
556                 }
557
558                 if (0 != carrier && max1 > 2000 && max1 > max2*3) {
559                         /* found good carrier */
560                         dprintk("found %s main sound carrier @ %d.%03d MHz [%d/%d]\n",
561                                 dev->tvnorm->name, carrier/1000, carrier%1000,
562                                 max1, max2);
563                         dev->last_carrier = carrier;
564
565                 } else if (0 != dev->last_carrier) {
566                         /* no carrier -- try last detected one as fallback */
567                         carrier = dev->last_carrier;
568                         dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
569                                "using %d.%03d MHz [last detected]\n",
570                                dev->name, carrier/1000, carrier%1000);
571
572                 } else {
573                         /* no carrier + no fallback -- use default */
574                         carrier = default_carrier;
575                         dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
576                                "using %d.%03d MHz [default]\n",
577                                dev->name, carrier/1000, carrier%1000);
578                 }
579                 tvaudio_setcarrier(dev,carrier,carrier);
580                 dev->automute = 0;
581                 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x00);
582                 saa7134_tvaudio_setmute(dev);
583
584                 /* find the exact tv audio norm */
585                 for (audio = UNSET, i = 0; i < TVAUDIO; i++) {
586                         if (dev->tvnorm->id != UNSET &&
587                                 !(dev->tvnorm->id & tvaudio[i].std))
588                                 continue;
589                         if (tvaudio[i].carr1 != carrier)
590                                 continue;
591
592                         if (UNSET == audio)
593                                 audio = i;
594                         tvaudio_setmode(dev,&tvaudio[i],"trying");
595                         if (tvaudio_sleep(dev,SCAN_SUBCARRIER_DELAY))
596                                 goto restart;
597                         if (-1 != tvaudio_getstereo(dev,&tvaudio[i])) {
598                                 audio = i;
599                                 break;
600                         }
601                 }
602                 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x30);
603                 if (UNSET == audio)
604                         continue;
605                 tvaudio_setmode(dev,&tvaudio[audio],"using");
606                 tvaudio_setstereo(dev,&tvaudio[audio],V4L2_TUNER_MODE_MONO);
607                 dev->tvaudio = &tvaudio[audio];
608
609                 lastmode = 42;
610                 for (;;) {
611                         if (tvaudio_sleep(dev,5000))
612                                 goto restart;
613                         if (dev->thread.shutdown || signal_pending(current))
614                                 break;
615                         if (UNSET == dev->thread.mode) {
616                                 rx = tvaudio_getstereo(dev,&tvaudio[i]);
617                                 mode = saa7134_tvaudio_rx2mode(rx);
618                         } else {
619                                 mode = dev->thread.mode;
620                         }
621                         if (lastmode != mode) {
622                                 tvaudio_setstereo(dev,&tvaudio[audio],mode);
623                                 lastmode = mode;
624                         }
625                 }
626         }
627
628  done:
629         complete_and_exit(&dev->thread.exit, 0);
630         return 0;
631 }
632
633 /* ------------------------------------------------------------------ */
634 /* saa7133 / saa7135 code                                             */
635
636 static char *stdres[0x20] = {
637         [0x00] = "no standard detected",
638         [0x01] = "B/G (in progress)",
639         [0x02] = "D/K (in progress)",
640         [0x03] = "M (in progress)",
641
642         [0x04] = "B/G A2",
643         [0x05] = "B/G NICAM",
644         [0x06] = "D/K A2 (1)",
645         [0x07] = "D/K A2 (2)",
646         [0x08] = "D/K A2 (3)",
647         [0x09] = "D/K NICAM",
648         [0x0a] = "L NICAM",
649         [0x0b] = "I NICAM",
650
651         [0x0c] = "M Korea",
652         [0x0d] = "M BTSC ",
653         [0x0e] = "M EIAJ",
654
655         [0x0f] = "FM radio / IF 10.7 / 50 deemp",
656         [0x10] = "FM radio / IF 10.7 / 75 deemp",
657         [0x11] = "FM radio / IF sel / 50 deemp",
658         [0x12] = "FM radio / IF sel / 75 deemp",
659
660         [0x13 ... 0x1e ] = "unknown",
661         [0x1f] = "??? [in progress]",
662 };
663
664 #define DSP_RETRY 32
665 #define DSP_DELAY 16
666
667 static inline int saa_dsp_wait_bit(struct saa7134_dev *dev, int bit)
668 {
669         int state, count = DSP_RETRY;
670
671         state = saa_readb(SAA7135_DSP_RWSTATE);
672         if (unlikely(state & SAA7135_DSP_RWSTATE_ERR)) {
673                 printk("%s: dsp access error\n",dev->name);
674                 /* FIXME: send ack ... */
675                 return -EIO;
676         }
677         while (0 == (state & bit)) {
678                 if (unlikely(0 == count)) {
679                         printk("%s: dsp access wait timeout [bit=%s]\n",
680                                dev->name,
681                                (bit & SAA7135_DSP_RWSTATE_WRR) ? "WRR" :
682                                (bit & SAA7135_DSP_RWSTATE_RDB) ? "RDB" :
683                                (bit & SAA7135_DSP_RWSTATE_IDA) ? "IDA" :
684                                "???");
685                         return -EIO;
686                 }
687                 saa_wait(DSP_DELAY);
688                 state = saa_readb(SAA7135_DSP_RWSTATE);
689                 count--;
690         }
691         return 0;
692 }
693
694
695 int saa_dsp_writel(struct saa7134_dev *dev, int reg, u32 value)
696 {
697         int err;
698
699         d2printk("dsp write reg 0x%x = 0x%06x\n",reg<<2,value);
700         err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
701         if (err < 0)
702                 return err;
703         saa_writel(reg,value);
704         err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
705         if (err < 0)
706                 return err;
707         return 0;
708 }
709
710 static int getstereo_7133(struct saa7134_dev *dev)
711 {
712         int retval = V4L2_TUNER_SUB_MONO;
713         u32 value;
714
715         value = saa_readl(0x528 >> 2);
716         if (value & 0x20)
717                 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
718         if (value & 0x40)
719                 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
720         return retval;
721 }
722
723 static int mute_input_7133(struct saa7134_dev *dev)
724 {
725         u32 reg = 0;
726         u32 xbarin, xbarout;
727         int mask;
728         struct saa7134_input *in;
729
730         /* Hac 0506 route OSS sound simultanously  */
731         xbarin = 0x03;
732         switch (dev->input->amux) {
733         case TV:
734                 reg = 0x02;
735                 xbarin = 0;
736                 break;
737         case LINE1:
738                 reg = 0x00;
739                 break;
740         case LINE2:
741         case LINE2_LEFT:
742                 reg = 0x09;
743                 break;
744         }
745         saa_dsp_writel(dev, 0x464 >> 2, xbarin);
746         if (dev->ctl_mute) {
747                 reg = 0x07;
748                 xbarout = 0xbbbbbb;
749         } else
750                 xbarout = 0xbbbb10;
751         saa_dsp_writel(dev, 0x46c >> 2, xbarout);
752
753         saa_writel(0x594 >> 2, reg);
754
755
756         /* switch gpio-connected external audio mux */
757         if (0 != card(dev).gpiomask) {
758                 mask = card(dev).gpiomask;
759
760                 if (card(dev).mute.name && dev->ctl_mute)
761                         in = &card(dev).mute;
762                 else
763                         in = dev->input;
764
765                 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2,   mask, mask);
766                 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio);
767                 saa7134_track_gpio(dev,in->name);
768         }
769
770         return 0;
771 }
772
773 static int tvaudio_thread_ddep(void *data)
774 {
775         struct saa7134_dev *dev = data;
776         u32 value, norms, clock;
777
778         daemonize("%s", dev->name);
779         allow_signal(SIGTERM);
780
781         clock = saa7134_boards[dev->board].audio_clock;
782         if (UNSET != audio_clock_override)
783                 clock = audio_clock_override;
784         saa_writel(0x598 >> 2, clock);
785
786         /* unmute */
787         saa_dsp_writel(dev, 0x474 >> 2, 0x00);
788         saa_dsp_writel(dev, 0x450 >> 2, 0x00);
789
790         for (;;) {
791                 tvaudio_sleep(dev,-1);
792                 if (dev->thread.shutdown || signal_pending(current))
793                         goto done;
794
795         restart:
796                 dev->thread.scan1 = dev->thread.scan2;
797                 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
798
799                 if (audio_ddep >= 0x04 && audio_ddep <= 0x0e) {
800                         /* insmod option override */
801                         norms = (audio_ddep << 2) | 0x01;
802                         dprintk("ddep override: %s\n",stdres[audio_ddep]);
803                 } else if (&card(dev).radio == dev->input) {
804                         dprintk("FM Radio\n");
805                         norms = (0x0f << 2) | 0x01;
806                 } else {
807                         /* (let chip) scan for sound carrier */
808                         norms = 0;
809                         if (dev->tvnorm->id & V4L2_STD_PAL) {
810                                 dprintk("PAL scan\n");
811                                 norms |= 0x2c; /* B/G + D/K + I */
812                         }
813                         if (dev->tvnorm->id & V4L2_STD_NTSC) {
814                                 dprintk("NTSC scan\n");
815                                 norms |= 0x40; /* M */
816                         }
817                         if (dev->tvnorm->id & V4L2_STD_SECAM) {
818                                 dprintk("SECAM scan\n");
819                                 norms |= 0x18; /* L + D/K */
820                         }
821                         if (0 == norms)
822                                 norms = 0x7c; /* all */
823                         dprintk("scanning:%s%s%s%s%s\n",
824                                 (norms & 0x04) ? " B/G"  : "",
825                                 (norms & 0x08) ? " D/K"  : "",
826                                 (norms & 0x10) ? " L/L'" : "",
827                                 (norms & 0x20) ? " I"    : "",
828                                 (norms & 0x40) ? " M"    : "");
829                 }
830
831                 /* kick automatic standard detection */
832                 saa_dsp_writel(dev, 0x454 >> 2, 0);
833                 saa_dsp_writel(dev, 0x454 >> 2, norms | 0x80);
834
835                 /* setup crossbars */
836                 saa_dsp_writel(dev, 0x464 >> 2, 0x000000);
837                 saa_dsp_writel(dev, 0x470 >> 2, 0x101010);
838
839                 if (tvaudio_sleep(dev,3000))
840                         goto restart;
841                 value = saa_readl(0x528 >> 2) & 0xffffff;
842
843                 dprintk("tvaudio thread status: 0x%x [%s%s%s]\n",
844                         value, stdres[value & 0x1f],
845                         (value & 0x000020) ? ",stereo" : "",
846                         (value & 0x000040) ? ",dual"   : "");
847                 dprintk("detailed status: "
848                         "%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n",
849                         (value & 0x000080) ? " A2/EIAJ pilot tone "     : "",
850                         (value & 0x000100) ? " A2/EIAJ dual "           : "",
851                         (value & 0x000200) ? " A2/EIAJ stereo "         : "",
852                         (value & 0x000400) ? " A2/EIAJ noise mute "     : "",
853
854                         (value & 0x000800) ? " BTSC/FM radio pilot "    : "",
855                         (value & 0x001000) ? " SAP carrier "            : "",
856                         (value & 0x002000) ? " BTSC stereo noise mute " : "",
857                         (value & 0x004000) ? " SAP noise mute "         : "",
858                         (value & 0x008000) ? " VDSP "                   : "",
859
860                         (value & 0x010000) ? " NICST "                  : "",
861                         (value & 0x020000) ? " NICDU "                  : "",
862                         (value & 0x040000) ? " NICAM muted "            : "",
863                         (value & 0x080000) ? " NICAM reserve sound "    : "",
864
865                         (value & 0x100000) ? " init done "              : "");
866         }
867
868  done:
869         complete_and_exit(&dev->thread.exit, 0);
870         return 0;
871 }
872
873 /* ------------------------------------------------------------------ */
874 /* common stuff + external entry points                               */
875
876 static void saa7134_enable_i2s(struct saa7134_dev *dev)
877 {
878         int i2s_format;
879
880         if (!card_is_empress(dev))
881                 return;
882         i2s_format = (dev->input->amux == TV) ? 0x00 : 0x01;
883
884         /* enable I2S audio output for the mpeg encoder */
885         saa_writeb(SAA7134_I2S_OUTPUT_SELECT,  0x80);
886         saa_writeb(SAA7134_I2S_OUTPUT_FORMAT,  i2s_format);
887         saa_writeb(SAA7134_I2S_OUTPUT_LEVEL,   0x0F);
888         saa_writeb(SAA7134_I2S_AUDIO_OUTPUT,   0x01);
889 }
890
891 int saa7134_tvaudio_rx2mode(u32 rx)
892 {
893         u32 mode;
894
895         mode = V4L2_TUNER_MODE_MONO;
896         if (rx & V4L2_TUNER_SUB_STEREO)
897                 mode = V4L2_TUNER_MODE_STEREO;
898         else if (rx & V4L2_TUNER_SUB_LANG1)
899                 mode = V4L2_TUNER_MODE_LANG1;
900         else if (rx & V4L2_TUNER_SUB_LANG2)
901                 mode = V4L2_TUNER_MODE_LANG2;
902         return mode;
903 }
904
905 void saa7134_tvaudio_setmute(struct saa7134_dev *dev)
906 {
907         switch (dev->pci->device) {
908         case PCI_DEVICE_ID_PHILIPS_SAA7130:
909         case PCI_DEVICE_ID_PHILIPS_SAA7134:
910                 mute_input_7134(dev);
911                 break;
912         case PCI_DEVICE_ID_PHILIPS_SAA7133:
913         case PCI_DEVICE_ID_PHILIPS_SAA7135:
914                 mute_input_7133(dev);
915                 break;
916         }
917 }
918
919 void saa7134_tvaudio_setinput(struct saa7134_dev *dev,
920                               struct saa7134_input *in)
921 {
922         dev->input = in;
923         switch (dev->pci->device) {
924         case PCI_DEVICE_ID_PHILIPS_SAA7130:
925         case PCI_DEVICE_ID_PHILIPS_SAA7134:
926                 mute_input_7134(dev);
927                 break;
928         case PCI_DEVICE_ID_PHILIPS_SAA7133:
929         case PCI_DEVICE_ID_PHILIPS_SAA7135:
930                 mute_input_7133(dev);
931                 break;
932         }
933         saa7134_enable_i2s(dev);
934 }
935
936 void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level)
937 {
938         switch (dev->pci->device) {
939         case PCI_DEVICE_ID_PHILIPS_SAA7134:
940                 saa_writeb(SAA7134_CHANNEL1_LEVEL,     level & 0x1f);
941                 saa_writeb(SAA7134_CHANNEL2_LEVEL,     level & 0x1f);
942                 saa_writeb(SAA7134_NICAM_LEVEL_ADJUST, level & 0x1f);
943                 break;
944         }
945 }
946
947 int saa7134_tvaudio_getstereo(struct saa7134_dev *dev)
948 {
949         int retval = V4L2_TUNER_SUB_MONO;
950
951         switch (dev->pci->device) {
952         case PCI_DEVICE_ID_PHILIPS_SAA7134:
953                 if (dev->tvaudio)
954                         retval = tvaudio_getstereo(dev,dev->tvaudio);
955                 break;
956         case PCI_DEVICE_ID_PHILIPS_SAA7133:
957         case PCI_DEVICE_ID_PHILIPS_SAA7135:
958                 retval = getstereo_7133(dev);
959                 break;
960         }
961         return retval;
962 }
963
964 int saa7134_tvaudio_init2(struct saa7134_dev *dev)
965 {
966         DECLARE_MUTEX_LOCKED(sem);
967         int (*my_thread)(void *data) = NULL;
968
969         switch (dev->pci->device) {
970         case PCI_DEVICE_ID_PHILIPS_SAA7134:
971                 my_thread = tvaudio_thread;
972                 break;
973         case PCI_DEVICE_ID_PHILIPS_SAA7133:
974         case PCI_DEVICE_ID_PHILIPS_SAA7135:
975                 my_thread = tvaudio_thread_ddep;
976                 break;
977         }
978
979         dev->thread.pid = -1;
980         if (my_thread) {
981                 /* start tvaudio thread */
982                 init_waitqueue_head(&dev->thread.wq);
983                 init_completion(&dev->thread.exit);
984                 dev->thread.pid = kernel_thread(my_thread,dev,0);
985                 if (dev->thread.pid < 0)
986                         printk(KERN_WARNING "%s: kernel_thread() failed\n",
987                                dev->name);
988                 saa7134_tvaudio_do_scan(dev);
989         }
990
991         saa7134_enable_i2s(dev);
992         return 0;
993 }
994
995 int saa7134_tvaudio_fini(struct saa7134_dev *dev)
996 {
997         /* shutdown tvaudio thread */
998         if (dev->thread.pid >= 0) {
999                 dev->thread.shutdown = 1;
1000                 wake_up_interruptible(&dev->thread.wq);
1001                 wait_for_completion(&dev->thread.exit);
1002         }
1003         saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, 0x00); /* LINE1 */
1004         return 0;
1005 }
1006
1007 int saa7134_tvaudio_do_scan(struct saa7134_dev *dev)
1008 {
1009         if (dev->thread.pid >= 0) {
1010                 dev->thread.mode = UNSET;
1011                 dev->thread.scan2++;
1012                 wake_up_interruptible(&dev->thread.wq);
1013         } else {
1014                 dev->automute = 0;
1015                 saa7134_tvaudio_setmute(dev);
1016         }
1017         return 0;
1018 }
1019
1020 /* ----------------------------------------------------------- */
1021 /*
1022  * Local variables:
1023  * c-basic-offset: 8
1024  * End:
1025  */