Auto merge with /home/aegl/GIT/ia64-test
[linux-2.6] / drivers / media / video / saa7134 / saa7134-tvaudio.c
1 /*
2  * $Id: saa7134-tvaudio.c,v 1.25 2005/06/07 19:00:38 nsh 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 (PCI_DEVICE_ID_PHILIPS_SAA7130 == dev->pci->device &&
223             card(dev).mute.name) {
224                 /* 7130 - we'll mute using some unconnected audio input */
225                 if (mute)
226                         in = &card(dev).mute;
227         }
228         if (dev->hw_mute  == mute &&
229             dev->hw_input == in) {
230                 dprintk("mute/input: nothing to do [mute=%d,input=%s]\n",
231                         mute,in->name);
232                 return;
233         }
234
235         dprintk("ctl_mute=%d automute=%d input=%s  =>  mute=%d input=%s\n",
236                 dev->ctl_mute,dev->automute,dev->input->name,mute,in->name);
237         dev->hw_mute  = mute;
238         dev->hw_input = in;
239
240         if (PCI_DEVICE_ID_PHILIPS_SAA7134 == dev->pci->device)
241                 /* 7134 mute */
242                 saa_writeb(SAA7134_AUDIO_MUTE_CTRL, mute ? 0xbf : 0xbb);
243
244         /* switch internal audio mux */
245         switch (in->amux) {
246         case TV:         ausel=0xc0; ics=0x00; ocs=0x02; break;
247         case LINE1:      ausel=0x80; ics=0x00; ocs=0x00; break;
248         case LINE2:      ausel=0x80; ics=0x08; ocs=0x01; break;
249         case LINE2_LEFT: ausel=0x80; ics=0x08; ocs=0x05; break;
250         }
251         saa_andorb(SAA7134_AUDIO_FORMAT_CTRL, 0xc0, ausel);
252         saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x08, ics);
253         saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, ocs);
254         // for oss, we need to change the clock configuration
255         if (in->amux == TV)
256                 saa_andorb(SAA7134_SIF_SAMPLE_FREQ,   0x03, 0x00);
257         else
258                 saa_andorb(SAA7134_SIF_SAMPLE_FREQ,   0x03, 0x01);
259
260         /* switch gpio-connected external audio mux */
261         if (0 == card(dev).gpiomask)
262                 return;
263         mask = card(dev).gpiomask;
264         saa_andorl(SAA7134_GPIO_GPMODE0 >> 2,   mask, mask);
265         saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio);
266         saa7134_track_gpio(dev,in->name);
267 }
268
269 static void tvaudio_setmode(struct saa7134_dev *dev,
270                             struct saa7134_tvaudio *audio,
271                             char *note)
272 {
273         int acpf, tweak = 0;
274
275         if (dev->tvnorm->id == V4L2_STD_NTSC) {
276                 acpf = 0x19066;
277         } else {
278                 acpf = 0x1e000;
279         }
280         if (audio_clock_tweak > -1024 && audio_clock_tweak < 1024)
281                 tweak = audio_clock_tweak;
282
283         if (note)
284                 dprintk("tvaudio_setmode: %s %s [%d.%03d/%d.%03d MHz] acpf=%d%+d\n",
285                         note,audio->name,
286                         audio->carr1 / 1000, audio->carr1 % 1000,
287                         audio->carr2 / 1000, audio->carr2 % 1000,
288                         acpf, tweak);
289
290         acpf += tweak;
291         saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD0, (acpf & 0x0000ff) >> 0);
292         saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD1, (acpf & 0x00ff00) >> 8);
293         saa_writeb(SAA7134_AUDIO_CLOCKS_PER_FIELD2, (acpf & 0x030000) >> 16);
294         tvaudio_setcarrier(dev,audio->carr1,audio->carr2);
295
296         switch (audio->mode) {
297         case TVAUDIO_FM_MONO:
298         case TVAUDIO_FM_BG_STEREO:
299                 saa_writeb(SAA7134_DEMODULATOR,               0x00);
300                 saa_writeb(SAA7134_DCXO_IDENT_CTRL,           0x00);
301                 saa_writeb(SAA7134_FM_DEEMPHASIS,             0x22);
302                 saa_writeb(SAA7134_FM_DEMATRIX,               0x80);
303                 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT,  0xa0);
304                 break;
305         case TVAUDIO_FM_K_STEREO:
306                 saa_writeb(SAA7134_DEMODULATOR,               0x00);
307                 saa_writeb(SAA7134_DCXO_IDENT_CTRL,           0x01);
308                 saa_writeb(SAA7134_FM_DEEMPHASIS,             0x22);
309                 saa_writeb(SAA7134_FM_DEMATRIX,               0x80);
310                 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT,  0xa0);
311                 break;
312         case TVAUDIO_NICAM_FM:
313                 saa_writeb(SAA7134_DEMODULATOR,               0x10);
314                 saa_writeb(SAA7134_DCXO_IDENT_CTRL,           0x00);
315                 saa_writeb(SAA7134_FM_DEEMPHASIS,             0x44);
316                 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT,  0xa1);
317                 saa_writeb(SAA7134_NICAM_CONFIG,              0x00);
318                 break;
319         case TVAUDIO_NICAM_AM:
320                 saa_writeb(SAA7134_DEMODULATOR,               0x12);
321                 saa_writeb(SAA7134_DCXO_IDENT_CTRL,           0x00);
322                 saa_writeb(SAA7134_FM_DEEMPHASIS,             0x44);
323                 saa_writeb(SAA7134_STEREO_DAC_OUTPUT_SELECT,  0xa1);
324                 saa_writeb(SAA7134_NICAM_CONFIG,              0x00);
325                 break;
326         case TVAUDIO_FM_SAT_STEREO:
327                 /* not implemented (yet) */
328                 break;
329         }
330 }
331
332 static int tvaudio_sleep(struct saa7134_dev *dev, int timeout)
333 {
334         DECLARE_WAITQUEUE(wait, current);
335
336         add_wait_queue(&dev->thread.wq, &wait);
337         if (dev->thread.scan1 == dev->thread.scan2 && !dev->thread.shutdown) {
338                 if (timeout < 0) {
339                         set_current_state(TASK_INTERRUPTIBLE);
340                         schedule();
341                 } else {
342 #if 0
343                         /* hmm, that one doesn't return on wakeup ... */
344                         msleep_interruptible(timeout);
345 #else
346                         set_current_state(TASK_INTERRUPTIBLE);
347                         schedule_timeout(msecs_to_jiffies(timeout));
348 #endif
349                 }
350         }
351         remove_wait_queue(&dev->thread.wq, &wait);
352         return dev->thread.scan1 != dev->thread.scan2;
353 }
354
355 static int tvaudio_checkcarrier(struct saa7134_dev *dev, struct mainscan *scan)
356 {
357         __s32 left,right,value;
358
359         if (audio_debug > 1) {
360                 int i;
361                 dprintk("debug %d:",scan->carr);
362                 for (i = -150; i <= 150; i += 30) {
363                         tvaudio_setcarrier(dev,scan->carr+i,scan->carr+i);
364                         saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
365                         if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
366                                 return -1;
367                         value = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
368                         if (0 == i)
369                                 printk("  #  %6d  # ",value >> 16);
370                         else
371                                 printk(" %6d",value >> 16);
372                 }
373                 printk("\n");
374         }
375
376         if (dev->tvnorm->id & scan->std) {
377                 tvaudio_setcarrier(dev,scan->carr-90,scan->carr-90);
378                 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
379                 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
380                         return -1;
381                 left = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
382
383                 tvaudio_setcarrier(dev,scan->carr+90,scan->carr+90);
384                 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
385                 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
386                         return -1;
387                 right = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
388
389                 left >>= 16;
390                 right >>= 16;
391                 value = left > right ? left - right : right - left;
392                 dprintk("scanning %d.%03d MHz [%4s] =>  dc is %5d [%d/%d]\n",
393                         scan->carr / 1000, scan->carr % 1000,
394                         scan->name, value, left, right);
395         } else {
396                 value = 0;
397                 dprintk("skipping %d.%03d MHz [%4s]\n",
398                         scan->carr / 1000, scan->carr % 1000, scan->name);
399         }
400         return value;
401 }
402
403 #if 0
404 static void sifdebug_dump_regs(struct saa7134_dev *dev)
405 {
406         print_regb(AUDIO_STATUS);
407         print_regb(IDENT_SIF);
408         print_regb(LEVEL_READOUT1);
409         print_regb(LEVEL_READOUT2);
410         print_regb(DCXO_IDENT_CTRL);
411         print_regb(DEMODULATOR);
412         print_regb(AGC_GAIN_SELECT);
413         print_regb(MONITOR_SELECT);
414         print_regb(FM_DEEMPHASIS);
415         print_regb(FM_DEMATRIX);
416         print_regb(SIF_SAMPLE_FREQ);
417         print_regb(ANALOG_IO_SELECT);
418 }
419 #endif
420
421 static int tvaudio_getstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio)
422 {
423         __u32 idp,nicam;
424         int retval = -1;
425
426         switch (audio->mode) {
427         case TVAUDIO_FM_MONO:
428                 return V4L2_TUNER_SUB_MONO;
429         case TVAUDIO_FM_K_STEREO:
430         case TVAUDIO_FM_BG_STEREO:
431                 idp = (saa_readb(SAA7134_IDENT_SIF) & 0xe0) >> 5;
432                 dprintk("getstereo: fm/stereo: idp=0x%x\n",idp);
433                 if (0x03 == (idp & 0x03))
434                         retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
435                 else if (0x05 == (idp & 0x05))
436                         retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
437                 else if (0x01 == (idp & 0x01))
438                         retval = V4L2_TUNER_SUB_MONO;
439                 break;
440         case TVAUDIO_FM_SAT_STEREO:
441                 /* not implemented (yet) */
442                 break;
443         case TVAUDIO_NICAM_FM:
444         case TVAUDIO_NICAM_AM:
445                 nicam = saa_readb(SAA7134_NICAM_STATUS);
446                 dprintk("getstereo: nicam=0x%x\n",nicam);
447                 switch (nicam & 0x0b) {
448                 case 0x08:
449                         retval = V4L2_TUNER_SUB_MONO;
450                         break;
451                 case 0x09:
452                         retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
453                         break;
454                 case 0x0a:
455                         retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
456                         break;
457                 }
458                 break;
459         }
460         if (retval != -1)
461                 dprintk("found audio subchannels:%s%s%s%s\n",
462                         (retval & V4L2_TUNER_SUB_MONO)   ? " mono"   : "",
463                         (retval & V4L2_TUNER_SUB_STEREO) ? " stereo" : "",
464                         (retval & V4L2_TUNER_SUB_LANG1)  ? " lang1"  : "",
465                         (retval & V4L2_TUNER_SUB_LANG2)  ? " lang2"  : "");
466         return retval;
467 }
468
469 static int tvaudio_setstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio,
470                              u32 mode)
471 {
472         static char *name[] = {
473                 [ V4L2_TUNER_MODE_MONO   ] = "mono",
474                 [ V4L2_TUNER_MODE_STEREO ] = "stereo",
475                 [ V4L2_TUNER_MODE_LANG1  ] = "lang1",
476                 [ V4L2_TUNER_MODE_LANG2  ] = "lang2",
477         };
478         static u32 fm[] = {
479                 [ V4L2_TUNER_MODE_MONO   ] = 0x00,  /* ch1  */
480                 [ V4L2_TUNER_MODE_STEREO ] = 0x80,  /* auto */
481                 [ V4L2_TUNER_MODE_LANG1  ] = 0x00,  /* ch1  */
482                 [ V4L2_TUNER_MODE_LANG2  ] = 0x01,  /* ch2  */
483         };
484         u32 reg;
485
486         switch (audio->mode) {
487         case TVAUDIO_FM_MONO:
488                 /* nothing to do ... */
489                 break;
490         case TVAUDIO_FM_K_STEREO:
491         case TVAUDIO_FM_BG_STEREO:
492                 dprintk("setstereo [fm] => %s\n",
493                         name[ mode % ARRAY_SIZE(name) ]);
494                 reg = fm[ mode % ARRAY_SIZE(fm) ];
495                 saa_writeb(SAA7134_FM_DEMATRIX, reg);
496                 break;
497         case TVAUDIO_FM_SAT_STEREO:
498         case TVAUDIO_NICAM_AM:
499         case TVAUDIO_NICAM_FM:
500                 /* FIXME */
501                 break;
502         }
503         return 0;
504 }
505
506 static int tvaudio_thread(void *data)
507 {
508         struct saa7134_dev *dev = data;
509         int carr_vals[ARRAY_SIZE(mainscan)];
510         unsigned int i, audio, nscan;
511         int max1,max2,carrier,rx,mode,lastmode,default_carrier;
512
513         daemonize("%s", dev->name);
514         allow_signal(SIGTERM);
515         for (;;) {
516                 tvaudio_sleep(dev,-1);
517                 if (dev->thread.shutdown || signal_pending(current))
518                         goto done;
519
520         restart:
521                 dev->thread.scan1 = dev->thread.scan2;
522                 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
523                 dev->tvaudio  = NULL;
524                 tvaudio_init(dev);
525                 if (dev->ctl_automute)
526                         dev->automute = 1;
527                 mute_input_7134(dev);
528
529                 /* give the tuner some time */
530                 if (tvaudio_sleep(dev,SCAN_INITIAL_DELAY))
531                         goto restart;
532
533                 max1 = 0;
534                 max2 = 0;
535                 nscan = 0;
536                 carrier = 0;
537                 default_carrier = 0;
538                 for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
539                         if (!(dev->tvnorm->id & mainscan[i].std))
540                                 continue;
541                         if (!default_carrier)
542                                 default_carrier = mainscan[i].carr;
543                         nscan++;
544                 }
545
546                 if (1 == nscan) {
547                         /* only one candidate -- skip scan ;) */
548                         max1 = 12345;
549                         carrier = default_carrier;
550                 } else {
551                         /* scan for the main carrier */
552                         saa_writeb(SAA7134_MONITOR_SELECT,0x00);
553                         tvaudio_setmode(dev,&tvaudio[0],NULL);
554                         for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
555                                 carr_vals[i] = tvaudio_checkcarrier(dev, mainscan+i);
556                                 if (dev->thread.scan1 != dev->thread.scan2)
557                                         goto restart;
558                         }
559                         for (max1 = 0, max2 = 0, i = 0; i < ARRAY_SIZE(mainscan); i++) {
560                                 if (max1 < carr_vals[i]) {
561                                         max2 = max1;
562                                         max1 = carr_vals[i];
563                                         carrier = mainscan[i].carr;
564                                 } else if (max2 < carr_vals[i]) {
565                                         max2 = carr_vals[i];
566                                 }
567                         }
568                 }
569
570                 if (0 != carrier && max1 > 2000 && max1 > max2*3) {
571                         /* found good carrier */
572                         dprintk("found %s main sound carrier @ %d.%03d MHz [%d/%d]\n",
573                                 dev->tvnorm->name, carrier/1000, carrier%1000,
574                                 max1, max2);
575                         dev->last_carrier = carrier;
576
577                 } else if (0 != dev->last_carrier) {
578                         /* no carrier -- try last detected one as fallback */
579                         carrier = dev->last_carrier;
580                         dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
581                                "using %d.%03d MHz [last detected]\n",
582                                dev->name, carrier/1000, carrier%1000);
583
584                 } else {
585                         /* no carrier + no fallback -- use default */
586                         carrier = default_carrier;
587                         dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
588                                "using %d.%03d MHz [default]\n",
589                                dev->name, carrier/1000, carrier%1000);
590                 }
591                 tvaudio_setcarrier(dev,carrier,carrier);
592                 dev->automute = 0;
593                 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x00);
594                 saa7134_tvaudio_setmute(dev);
595
596                 /* find the exact tv audio norm */
597                 for (audio = UNSET, i = 0; i < TVAUDIO; i++) {
598                         if (dev->tvnorm->id != UNSET &&
599                             !(dev->tvnorm->id & tvaudio[i].std))
600                                 continue;
601                         if (tvaudio[i].carr1 != carrier)
602                                 continue;
603
604                         if (UNSET == audio)
605                                 audio = i;
606                         tvaudio_setmode(dev,&tvaudio[i],"trying");
607                         if (tvaudio_sleep(dev,SCAN_SUBCARRIER_DELAY))
608                                 goto restart;
609                         if (-1 != tvaudio_getstereo(dev,&tvaudio[i])) {
610                                 audio = i;
611                                 break;
612                         }
613                 }
614                 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x30);
615                 if (UNSET == audio)
616                         continue;
617                 tvaudio_setmode(dev,&tvaudio[audio],"using");
618                 tvaudio_setstereo(dev,&tvaudio[audio],V4L2_TUNER_MODE_MONO);
619                 dev->tvaudio = &tvaudio[audio];
620
621                 lastmode = 42;
622                 for (;;) {
623                         if (tvaudio_sleep(dev,5000))
624                                 goto restart;
625                         if (dev->thread.shutdown || signal_pending(current))
626                                 break;
627                         if (UNSET == dev->thread.mode) {
628                                 rx = tvaudio_getstereo(dev,&tvaudio[i]);
629                                 mode = saa7134_tvaudio_rx2mode(rx);
630                         } else {
631                                 mode = dev->thread.mode;
632                         }
633                         if (lastmode != mode) {
634                                 tvaudio_setstereo(dev,&tvaudio[audio],mode);
635                                 lastmode = mode;
636                         }
637                 }
638         }
639
640  done:
641         complete_and_exit(&dev->thread.exit, 0);
642         return 0;
643 }
644
645 /* ------------------------------------------------------------------ */
646 /* saa7133 / saa7135 code                                             */
647
648 static char *stdres[0x20] = {
649         [0x00] = "no standard detected",
650         [0x01] = "B/G (in progress)",
651         [0x02] = "D/K (in progress)",
652         [0x03] = "M (in progress)",
653
654         [0x04] = "B/G A2",
655         [0x05] = "B/G NICAM",
656         [0x06] = "D/K A2 (1)",
657         [0x07] = "D/K A2 (2)",
658         [0x08] = "D/K A2 (3)",
659         [0x09] = "D/K NICAM",
660         [0x0a] = "L NICAM",
661         [0x0b] = "I NICAM",
662
663         [0x0c] = "M Korea",
664         [0x0d] = "M BTSC ",
665         [0x0e] = "M EIAJ",
666
667         [0x0f] = "FM radio / IF 10.7 / 50 deemp",
668         [0x10] = "FM radio / IF 10.7 / 75 deemp",
669         [0x11] = "FM radio / IF sel / 50 deemp",
670         [0x12] = "FM radio / IF sel / 75 deemp",
671
672         [0x13 ... 0x1e ] = "unknown",
673         [0x1f] = "??? [in progress]",
674 };
675
676 #define DSP_RETRY 32
677 #define DSP_DELAY 16
678
679 static inline int saa_dsp_wait_bit(struct saa7134_dev *dev, int bit)
680 {
681         int state, count = DSP_RETRY;
682
683         state = saa_readb(SAA7135_DSP_RWSTATE);
684         if (unlikely(state & SAA7135_DSP_RWSTATE_ERR)) {
685                 printk("%s: dsp access error\n",dev->name);
686                 /* FIXME: send ack ... */
687                 return -EIO;
688         }
689         while (0 == (state & bit)) {
690                 if (unlikely(0 == count)) {
691                         printk("%s: dsp access wait timeout [bit=%s]\n",
692                                dev->name,
693                                (bit & SAA7135_DSP_RWSTATE_WRR) ? "WRR" :
694                                (bit & SAA7135_DSP_RWSTATE_RDB) ? "RDB" :
695                                (bit & SAA7135_DSP_RWSTATE_IDA) ? "IDA" :
696                                "???");
697                         return -EIO;
698                 }
699                 saa_wait(DSP_DELAY);
700                 state = saa_readb(SAA7135_DSP_RWSTATE);
701                 count--;
702         }
703         return 0;
704 }
705
706 #if 0
707 static int saa_dsp_readl(struct saa7134_dev *dev, int reg, u32 *value)
708 {
709         int err;
710
711         d2printk("dsp read reg 0x%x\n", reg<<2);
712         saa_readl(reg);
713         err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_RDB);
714         if (err < 0)
715                 return err;
716         *value = saa_readl(reg);
717         d2printk("dsp read   => 0x%06x\n", *value & 0xffffff);
718         err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_IDA);
719         if (err < 0)
720                 return err;
721         return 0;
722 }
723 #endif
724
725 int saa_dsp_writel(struct saa7134_dev *dev, int reg, u32 value)
726 {
727         int err;
728
729         d2printk("dsp write reg 0x%x = 0x%06x\n",reg<<2,value);
730         err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
731         if (err < 0)
732                 return err;
733         saa_writel(reg,value);
734         err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
735         if (err < 0)
736                 return err;
737         return 0;
738 }
739
740 static int getstereo_7133(struct saa7134_dev *dev)
741 {
742         int retval = V4L2_TUNER_SUB_MONO;
743         u32 value;
744
745         value = saa_readl(0x528 >> 2);
746         if (value & 0x20)
747                 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
748         if (value & 0x40)
749                 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
750         return retval;
751 }
752
753 static int mute_input_7133(struct saa7134_dev *dev)
754 {
755         u32 reg = 0;
756         int mask;
757
758         switch (dev->input->amux) {
759         case TV:
760                 reg = 0x02;
761                 break;
762         case LINE1:
763                 reg = 0x00;
764                 break;
765         case LINE2:
766         case LINE2_LEFT:
767                 reg = 0x01;
768                 break;
769         }
770         if (dev->ctl_mute)
771                 reg = 0x07;
772         saa_writel(0x594 >> 2, reg);
773
774         /* switch gpio-connected external audio mux */
775         if (0 != card(dev).gpiomask) {
776                 mask = card(dev).gpiomask;
777                 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2,   mask, mask);
778                 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, dev->input->gpio);
779                 saa7134_track_gpio(dev,dev->input->name);
780         }
781         return 0;
782 }
783
784 static int tvaudio_thread_ddep(void *data)
785 {
786         struct saa7134_dev *dev = data;
787         u32 value, norms, clock;
788
789         daemonize("%s", dev->name);
790         allow_signal(SIGTERM);
791
792         clock = saa7134_boards[dev->board].audio_clock;
793         if (UNSET != audio_clock_override)
794                 clock = audio_clock_override;
795         saa_writel(0x598 >> 2, clock);
796
797         /* unmute */
798         saa_dsp_writel(dev, 0x474 >> 2, 0x00);
799         saa_dsp_writel(dev, 0x450 >> 2, 0x00);
800
801         for (;;) {
802                 tvaudio_sleep(dev,-1);
803                 if (dev->thread.shutdown || signal_pending(current))
804                         goto done;
805
806         restart:
807                 dev->thread.scan1 = dev->thread.scan2;
808                 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
809
810                 if (audio_ddep >= 0x04 && audio_ddep <= 0x0e) {
811                         /* insmod option override */
812                         norms = (audio_ddep << 2) | 0x01;
813                         dprintk("ddep override: %s\n",stdres[audio_ddep]);
814                 } else if (&card(dev).radio == dev->input) {
815                         dprintk("FM Radio\n");
816                         norms = (0x0f << 2) | 0x01;
817                 } else {
818                         /* (let chip) scan for sound carrier */
819                         norms = 0;
820                         if (dev->tvnorm->id & V4L2_STD_PAL) {
821                                 dprintk("PAL scan\n");
822                                 norms |= 0x2c; /* B/G + D/K + I */
823                         }
824                         if (dev->tvnorm->id & V4L2_STD_NTSC) {
825                                 dprintk("NTSC scan\n");
826                                 norms |= 0x40; /* M */
827                         }
828                         if (dev->tvnorm->id & V4L2_STD_SECAM) {
829                                 dprintk("SECAM scan\n");
830                                 norms |= 0x18; /* L + D/K */
831                         }
832                         if (0 == norms)
833                                 norms = 0x7c; /* all */
834                         dprintk("scanning:%s%s%s%s%s\n",
835                                 (norms & 0x04) ? " B/G"  : "",
836                                 (norms & 0x08) ? " D/K"  : "",
837                                 (norms & 0x10) ? " L/L'" : "",
838                                 (norms & 0x20) ? " I"    : "",
839                                 (norms & 0x40) ? " M"    : "");
840                 }
841
842                 /* kick automatic standard detection */
843                 saa_dsp_writel(dev, 0x454 >> 2, 0);
844                 saa_dsp_writel(dev, 0x454 >> 2, norms | 0x80);
845
846                 /* setup crossbars */
847                 saa_dsp_writel(dev, 0x464 >> 2, 0x000000);
848                 saa_dsp_writel(dev, 0x470 >> 2, 0x101010);
849
850                 if (tvaudio_sleep(dev,3000))
851                         goto restart;
852                 value = saa_readl(0x528 >> 2) & 0xffffff;
853
854                 dprintk("tvaudio thread status: 0x%x [%s%s%s]\n",
855                         value, stdres[value & 0x1f],
856                         (value & 0x000020) ? ",stereo" : "",
857                         (value & 0x000040) ? ",dual"   : "");
858                 dprintk("detailed status: "
859                         "%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n",
860                         (value & 0x000080) ? " A2/EIAJ pilot tone "     : "",
861                         (value & 0x000100) ? " A2/EIAJ dual "           : "",
862                         (value & 0x000200) ? " A2/EIAJ stereo "         : "",
863                         (value & 0x000400) ? " A2/EIAJ noise mute "     : "",
864
865                         (value & 0x000800) ? " BTSC/FM radio pilot "    : "",
866                         (value & 0x001000) ? " SAP carrier "            : "",
867                         (value & 0x002000) ? " BTSC stereo noise mute " : "",
868                         (value & 0x004000) ? " SAP noise mute "         : "",
869                         (value & 0x008000) ? " VDSP "                   : "",
870
871                         (value & 0x010000) ? " NICST "                  : "",
872                         (value & 0x020000) ? " NICDU "                  : "",
873                         (value & 0x040000) ? " NICAM muted "            : "",
874                         (value & 0x080000) ? " NICAM reserve sound "    : "",
875
876                         (value & 0x100000) ? " init done "              : "");
877         }
878
879  done:
880         complete_and_exit(&dev->thread.exit, 0);
881         return 0;
882 }
883
884 /* ------------------------------------------------------------------ */
885 /* common stuff + external entry points                               */
886
887 static void saa7134_enable_i2s(struct saa7134_dev *dev)
888 {
889         int i2s_format;
890
891         if (!card_is_empress(dev))
892                 return;
893         i2s_format = (dev->input->amux == TV) ? 0x00 : 0x01;
894
895         /* enable I2S audio output for the mpeg encoder */
896         saa_writeb(SAA7134_I2S_OUTPUT_SELECT,  0x80);
897         saa_writeb(SAA7134_I2S_OUTPUT_FORMAT,  i2s_format);
898         saa_writeb(SAA7134_I2S_OUTPUT_LEVEL,   0x0F);
899         saa_writeb(SAA7134_I2S_AUDIO_OUTPUT,   0x01);
900 }
901
902 int saa7134_tvaudio_rx2mode(u32 rx)
903 {
904         u32 mode;
905
906         mode = V4L2_TUNER_MODE_MONO;
907         if (rx & V4L2_TUNER_SUB_STEREO)
908                 mode = V4L2_TUNER_MODE_STEREO;
909         else if (rx & V4L2_TUNER_SUB_LANG1)
910                 mode = V4L2_TUNER_MODE_LANG1;
911         else if (rx & V4L2_TUNER_SUB_LANG2)
912                 mode = V4L2_TUNER_MODE_LANG2;
913         return mode;
914 }
915
916 void saa7134_tvaudio_setmute(struct saa7134_dev *dev)
917 {
918         switch (dev->pci->device) {
919         case PCI_DEVICE_ID_PHILIPS_SAA7130:
920         case PCI_DEVICE_ID_PHILIPS_SAA7134:
921                 mute_input_7134(dev);
922                 break;
923         case PCI_DEVICE_ID_PHILIPS_SAA7133:
924         case PCI_DEVICE_ID_PHILIPS_SAA7135:
925                 mute_input_7133(dev);
926                 break;
927         }
928 }
929
930 void saa7134_tvaudio_setinput(struct saa7134_dev *dev,
931                               struct saa7134_input *in)
932 {
933         dev->input = in;
934         switch (dev->pci->device) {
935         case PCI_DEVICE_ID_PHILIPS_SAA7130:
936         case PCI_DEVICE_ID_PHILIPS_SAA7134:
937                 mute_input_7134(dev);
938                 break;
939         case PCI_DEVICE_ID_PHILIPS_SAA7133:
940         case PCI_DEVICE_ID_PHILIPS_SAA7135:
941                 mute_input_7133(dev);
942                 break;
943         }
944         saa7134_enable_i2s(dev);
945 }
946
947 void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level)
948 {
949         switch (dev->pci->device) {
950         case PCI_DEVICE_ID_PHILIPS_SAA7134:
951                 saa_writeb(SAA7134_CHANNEL1_LEVEL,     level & 0x1f);
952                 saa_writeb(SAA7134_CHANNEL2_LEVEL,     level & 0x1f);
953                 saa_writeb(SAA7134_NICAM_LEVEL_ADJUST, level & 0x1f);
954                 break;
955         }
956 }
957
958 int saa7134_tvaudio_getstereo(struct saa7134_dev *dev)
959 {
960         int retval = V4L2_TUNER_SUB_MONO;
961
962         switch (dev->pci->device) {
963         case PCI_DEVICE_ID_PHILIPS_SAA7134:
964                 if (dev->tvaudio)
965                         retval = tvaudio_getstereo(dev,dev->tvaudio);
966                 break;
967         case PCI_DEVICE_ID_PHILIPS_SAA7133:
968         case PCI_DEVICE_ID_PHILIPS_SAA7135:
969                 retval = getstereo_7133(dev);
970                 break;
971         }
972         return retval;
973 }
974
975 int saa7134_tvaudio_init2(struct saa7134_dev *dev)
976 {
977         DECLARE_MUTEX_LOCKED(sem);
978         int (*my_thread)(void *data) = NULL;
979
980         switch (dev->pci->device) {
981         case PCI_DEVICE_ID_PHILIPS_SAA7134:
982                 my_thread = tvaudio_thread;
983                 break;
984         case PCI_DEVICE_ID_PHILIPS_SAA7133:
985         case PCI_DEVICE_ID_PHILIPS_SAA7135:
986                 my_thread = tvaudio_thread_ddep;
987                 break;
988         }
989
990         dev->thread.pid = -1;
991         if (my_thread) {
992                 /* start tvaudio thread */
993                 init_waitqueue_head(&dev->thread.wq);
994                 init_completion(&dev->thread.exit);
995                 dev->thread.pid = kernel_thread(my_thread,dev,0);
996                 if (dev->thread.pid < 0)
997                         printk(KERN_WARNING "%s: kernel_thread() failed\n",
998                                dev->name);
999                 saa7134_tvaudio_do_scan(dev);
1000         }
1001
1002         saa7134_enable_i2s(dev);
1003         return 0;
1004 }
1005
1006 int saa7134_tvaudio_fini(struct saa7134_dev *dev)
1007 {
1008         /* shutdown tvaudio thread */
1009         if (dev->thread.pid >= 0) {
1010                 dev->thread.shutdown = 1;
1011                 wake_up_interruptible(&dev->thread.wq);
1012                 wait_for_completion(&dev->thread.exit);
1013         }
1014         saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, 0x00); /* LINE1 */
1015         return 0;
1016 }
1017
1018 int saa7134_tvaudio_do_scan(struct saa7134_dev *dev)
1019 {
1020         if (dev->thread.pid >= 0) {
1021                 dev->thread.mode = UNSET;
1022                 dev->thread.scan2++;
1023                 wake_up_interruptible(&dev->thread.wq);
1024         } else {
1025                 dev->automute = 0;
1026                 saa7134_tvaudio_setmute(dev);
1027         }
1028         return 0;
1029 }
1030
1031 /* ----------------------------------------------------------- */
1032 /*
1033  * Local variables:
1034  * c-basic-offset: 8
1035  * End:
1036  */