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