Merge branch 'audit.b3' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/audit...
[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                 [ V4L2_TUNER_MODE_LANG1_LANG2  ] = "lang1+lang2",
486         };
487         static u32 fm[] = {
488                 [ V4L2_TUNER_MODE_MONO   ] = 0x00,  /* ch1  */
489                 [ V4L2_TUNER_MODE_STEREO ] = 0x80,  /* auto */
490                 [ V4L2_TUNER_MODE_LANG1  ] = 0x00,  /* ch1  */
491                 [ V4L2_TUNER_MODE_LANG2  ] = 0x01,  /* ch2  */
492                 [ V4L2_TUNER_MODE_LANG1_LANG2 ] = 0x80,  /* auto */
493         };
494         u32 reg;
495
496         switch (audio->mode) {
497         case TVAUDIO_FM_MONO:
498         case TVAUDIO_AM_MONO:
499                 /* nothing to do ... */
500                 break;
501         case TVAUDIO_FM_K_STEREO:
502         case TVAUDIO_FM_BG_STEREO:
503         case TVAUDIO_NICAM_AM:
504         case TVAUDIO_NICAM_FM:
505                 dprintk("setstereo [fm] => %s\n",
506                         name[ mode % ARRAY_SIZE(name) ]);
507                 reg = fm[ mode % ARRAY_SIZE(fm) ];
508                 saa_writeb(SAA7134_FM_DEMATRIX, reg);
509                 break;
510         case TVAUDIO_FM_SAT_STEREO:
511                 /* Not implemented */
512                 break;
513         }
514         return 0;
515 }
516
517 static int tvaudio_thread(void *data)
518 {
519         struct saa7134_dev *dev = data;
520         int carr_vals[ARRAY_SIZE(mainscan)];
521         unsigned int i, audio, nscan;
522         int max1,max2,carrier,rx,mode,lastmode,default_carrier;
523
524         daemonize("%s", dev->name);
525         allow_signal(SIGTERM);
526         for (;;) {
527                 tvaudio_sleep(dev,-1);
528                 if (dev->thread.shutdown || signal_pending(current))
529                         goto done;
530
531         restart:
532                 dev->thread.scan1 = dev->thread.scan2;
533                 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
534                 dev->tvaudio  = NULL;
535                 tvaudio_init(dev);
536                 if (dev->ctl_automute)
537                         dev->automute = 1;
538                 mute_input_7134(dev);
539
540                 /* give the tuner some time */
541                 if (tvaudio_sleep(dev,SCAN_INITIAL_DELAY))
542                         goto restart;
543
544                 max1 = 0;
545                 max2 = 0;
546                 nscan = 0;
547                 carrier = 0;
548                 default_carrier = 0;
549                 for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
550                         if (!(dev->tvnorm->id & mainscan[i].std))
551                                 continue;
552                         if (!default_carrier)
553                                 default_carrier = mainscan[i].carr;
554                         nscan++;
555                 }
556
557                 if (1 == nscan) {
558                         /* only one candidate -- skip scan ;) */
559                         max1 = 12345;
560                         carrier = default_carrier;
561                 } else {
562                         /* scan for the main carrier */
563                         saa_writeb(SAA7134_MONITOR_SELECT,0x00);
564                         tvaudio_setmode(dev,&tvaudio[0],NULL);
565                         for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
566                                 carr_vals[i] = tvaudio_checkcarrier(dev, mainscan+i);
567                                 if (dev->thread.scan1 != dev->thread.scan2)
568                                         goto restart;
569                         }
570                         for (max1 = 0, max2 = 0, i = 0; i < ARRAY_SIZE(mainscan); i++) {
571                                 if (max1 < carr_vals[i]) {
572                                         max2 = max1;
573                                         max1 = carr_vals[i];
574                                         carrier = mainscan[i].carr;
575                                 } else if (max2 < carr_vals[i]) {
576                                         max2 = carr_vals[i];
577                                 }
578                         }
579                 }
580
581                 if (0 != carrier && max1 > 2000 && max1 > max2*3) {
582                         /* found good carrier */
583                         dprintk("found %s main sound carrier @ %d.%03d MHz [%d/%d]\n",
584                                 dev->tvnorm->name, carrier/1000, carrier%1000,
585                                 max1, max2);
586                         dev->last_carrier = carrier;
587
588                 } else if (0 != dev->last_carrier) {
589                         /* no carrier -- try last detected one as fallback */
590                         carrier = dev->last_carrier;
591                         dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
592                                "using %d.%03d MHz [last detected]\n",
593                                dev->name, carrier/1000, carrier%1000);
594
595                 } else {
596                         /* no carrier + no fallback -- use default */
597                         carrier = default_carrier;
598                         dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
599                                "using %d.%03d MHz [default]\n",
600                                dev->name, carrier/1000, carrier%1000);
601                 }
602                 tvaudio_setcarrier(dev,carrier,carrier);
603                 dev->automute = 0;
604                 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x00);
605                 saa7134_tvaudio_setmute(dev);
606
607                 /* find the exact tv audio norm */
608                 for (audio = UNSET, i = 0; i < TVAUDIO; i++) {
609                         if (dev->tvnorm->id != UNSET &&
610                                 !(dev->tvnorm->id & tvaudio[i].std))
611                                 continue;
612                         if (tvaudio[i].carr1 != carrier)
613                                 continue;
614
615                         if (UNSET == audio)
616                                 audio = i;
617                         tvaudio_setmode(dev,&tvaudio[i],"trying");
618                         if (tvaudio_sleep(dev,SCAN_SUBCARRIER_DELAY))
619                                 goto restart;
620                         if (-1 != tvaudio_getstereo(dev,&tvaudio[i])) {
621                                 audio = i;
622                                 break;
623                         }
624                 }
625                 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x30);
626                 if (UNSET == audio)
627                         continue;
628                 tvaudio_setmode(dev,&tvaudio[audio],"using");
629                 tvaudio_setstereo(dev,&tvaudio[audio],V4L2_TUNER_MODE_MONO);
630                 dev->tvaudio = &tvaudio[audio];
631
632                 lastmode = 42;
633                 for (;;) {
634                         if (tvaudio_sleep(dev,5000))
635                                 goto restart;
636                         if (dev->thread.shutdown || signal_pending(current))
637                                 break;
638                         if (UNSET == dev->thread.mode) {
639                                 rx = tvaudio_getstereo(dev,&tvaudio[i]);
640                                 mode = saa7134_tvaudio_rx2mode(rx);
641                         } else {
642                                 mode = dev->thread.mode;
643                         }
644                         if (lastmode != mode) {
645                                 tvaudio_setstereo(dev,&tvaudio[audio],mode);
646                                 lastmode = mode;
647                         }
648                 }
649         }
650
651  done:
652         complete_and_exit(&dev->thread.exit, 0);
653         return 0;
654 }
655
656 /* ------------------------------------------------------------------ */
657 /* saa7133 / saa7135 code                                             */
658
659 static char *stdres[0x20] = {
660         [0x00] = "no standard detected",
661         [0x01] = "B/G (in progress)",
662         [0x02] = "D/K (in progress)",
663         [0x03] = "M (in progress)",
664
665         [0x04] = "B/G A2",
666         [0x05] = "B/G NICAM",
667         [0x06] = "D/K A2 (1)",
668         [0x07] = "D/K A2 (2)",
669         [0x08] = "D/K A2 (3)",
670         [0x09] = "D/K NICAM",
671         [0x0a] = "L NICAM",
672         [0x0b] = "I NICAM",
673
674         [0x0c] = "M Korea",
675         [0x0d] = "M BTSC ",
676         [0x0e] = "M EIAJ",
677
678         [0x0f] = "FM radio / IF 10.7 / 50 deemp",
679         [0x10] = "FM radio / IF 10.7 / 75 deemp",
680         [0x11] = "FM radio / IF sel / 50 deemp",
681         [0x12] = "FM radio / IF sel / 75 deemp",
682
683         [0x13 ... 0x1e ] = "unknown",
684         [0x1f] = "??? [in progress]",
685 };
686
687 #define DSP_RETRY 32
688 #define DSP_DELAY 16
689
690 static inline int saa_dsp_wait_bit(struct saa7134_dev *dev, int bit)
691 {
692         int state, count = DSP_RETRY;
693
694         state = saa_readb(SAA7135_DSP_RWSTATE);
695         if (unlikely(state & SAA7135_DSP_RWSTATE_ERR)) {
696                 printk("%s: dsp access error\n",dev->name);
697                 /* FIXME: send ack ... */
698                 return -EIO;
699         }
700         while (0 == (state & bit)) {
701                 if (unlikely(0 == count)) {
702                         printk("%s: dsp access wait timeout [bit=%s]\n",
703                                dev->name,
704                                (bit & SAA7135_DSP_RWSTATE_WRR) ? "WRR" :
705                                (bit & SAA7135_DSP_RWSTATE_RDB) ? "RDB" :
706                                (bit & SAA7135_DSP_RWSTATE_IDA) ? "IDA" :
707                                "???");
708                         return -EIO;
709                 }
710                 saa_wait(DSP_DELAY);
711                 state = saa_readb(SAA7135_DSP_RWSTATE);
712                 count--;
713         }
714         return 0;
715 }
716
717
718 int saa_dsp_writel(struct saa7134_dev *dev, int reg, u32 value)
719 {
720         int err;
721
722         d2printk("dsp write reg 0x%x = 0x%06x\n",reg<<2,value);
723         err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
724         if (err < 0)
725                 return err;
726         saa_writel(reg,value);
727         err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
728         if (err < 0)
729                 return err;
730         return 0;
731 }
732
733 static int getstereo_7133(struct saa7134_dev *dev)
734 {
735         int retval = V4L2_TUNER_SUB_MONO;
736         u32 value;
737
738         value = saa_readl(0x528 >> 2);
739         if (value & 0x20)
740                 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
741         if (value & 0x40)
742                 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
743         return retval;
744 }
745
746 static int mute_input_7133(struct saa7134_dev *dev)
747 {
748         u32 reg = 0;
749         u32 xbarin, xbarout;
750         int mask;
751         struct saa7134_input *in;
752
753         /* Hac 0506 route OSS sound simultanously  */
754         xbarin = 0x03;
755         switch (dev->input->amux) {
756         case TV:
757                 reg = 0x02;
758                 xbarin = 0;
759                 break;
760         case LINE1:
761                 reg = 0x00;
762                 break;
763         case LINE2:
764         case LINE2_LEFT:
765                 reg = 0x09;
766                 break;
767         }
768         saa_dsp_writel(dev, 0x464 >> 2, xbarin);
769         if (dev->ctl_mute) {
770                 reg = 0x07;
771                 xbarout = 0xbbbbbb;
772         } else
773                 xbarout = 0xbbbb10;
774         saa_dsp_writel(dev, 0x46c >> 2, xbarout);
775
776         saa_writel(0x594 >> 2, reg);
777
778
779         /* switch gpio-connected external audio mux */
780         if (0 != card(dev).gpiomask) {
781                 mask = card(dev).gpiomask;
782
783                 if (card(dev).mute.name && dev->ctl_mute)
784                         in = &card(dev).mute;
785                 else
786                         in = dev->input;
787
788                 saa_andorl(SAA7134_GPIO_GPMODE0 >> 2,   mask, mask);
789                 saa_andorl(SAA7134_GPIO_GPSTATUS0 >> 2, mask, in->gpio);
790                 saa7134_track_gpio(dev,in->name);
791         }
792
793         return 0;
794 }
795
796 static int tvaudio_thread_ddep(void *data)
797 {
798         struct saa7134_dev *dev = data;
799         u32 value, norms, clock;
800
801         daemonize("%s", dev->name);
802         allow_signal(SIGTERM);
803
804         clock = saa7134_boards[dev->board].audio_clock;
805         if (UNSET != audio_clock_override)
806                 clock = audio_clock_override;
807         saa_writel(0x598 >> 2, clock);
808
809         /* unmute */
810         saa_dsp_writel(dev, 0x474 >> 2, 0x00);
811         saa_dsp_writel(dev, 0x450 >> 2, 0x00);
812
813         for (;;) {
814                 tvaudio_sleep(dev,-1);
815                 if (dev->thread.shutdown || signal_pending(current))
816                         goto done;
817
818         restart:
819                 dev->thread.scan1 = dev->thread.scan2;
820                 dprintk("tvaudio thread scan start [%d]\n",dev->thread.scan1);
821
822                 if (audio_ddep >= 0x04 && audio_ddep <= 0x0e) {
823                         /* insmod option override */
824                         norms = (audio_ddep << 2) | 0x01;
825                         dprintk("ddep override: %s\n",stdres[audio_ddep]);
826                 } else if (&card(dev).radio == dev->input) {
827                         dprintk("FM Radio\n");
828                         if (dev->tuner_type == TUNER_PHILIPS_TDA8290) {
829                                 norms = (0x11 << 2) | 0x01;
830                                 saa_dsp_writel(dev, 0x42c >> 2, 0x729555);
831                         } else {
832                                 norms = (0x0f << 2) | 0x01;
833                         }
834                 } else {
835                         /* (let chip) scan for sound carrier */
836                         norms = 0;
837                         if (dev->tvnorm->id & V4L2_STD_PAL) {
838                                 dprintk("PAL scan\n");
839                                 norms |= 0x2c; /* B/G + D/K + I */
840                         }
841                         if (dev->tvnorm->id & V4L2_STD_NTSC) {
842                                 dprintk("NTSC scan\n");
843                                 norms |= 0x40; /* M */
844                         }
845                         if (dev->tvnorm->id & V4L2_STD_SECAM) {
846                                 dprintk("SECAM scan\n");
847                                 norms |= 0x18; /* L + D/K */
848                         }
849                         if (0 == norms)
850                                 norms = 0x7c; /* all */
851                         dprintk("scanning:%s%s%s%s%s\n",
852                                 (norms & 0x04) ? " B/G"  : "",
853                                 (norms & 0x08) ? " D/K"  : "",
854                                 (norms & 0x10) ? " L/L'" : "",
855                                 (norms & 0x20) ? " I"    : "",
856                                 (norms & 0x40) ? " M"    : "");
857                 }
858
859                 /* kick automatic standard detection */
860                 saa_dsp_writel(dev, 0x454 >> 2, 0);
861                 saa_dsp_writel(dev, 0x454 >> 2, norms | 0x80);
862
863                 /* setup crossbars */
864                 saa_dsp_writel(dev, 0x464 >> 2, 0x000000);
865                 saa_dsp_writel(dev, 0x470 >> 2, 0x101010);
866
867                 if (tvaudio_sleep(dev,3000))
868                         goto restart;
869                 value = saa_readl(0x528 >> 2) & 0xffffff;
870
871                 dprintk("tvaudio thread status: 0x%x [%s%s%s]\n",
872                         value, stdres[value & 0x1f],
873                         (value & 0x000020) ? ",stereo" : "",
874                         (value & 0x000040) ? ",dual"   : "");
875                 dprintk("detailed status: "
876                         "%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n",
877                         (value & 0x000080) ? " A2/EIAJ pilot tone "     : "",
878                         (value & 0x000100) ? " A2/EIAJ dual "           : "",
879                         (value & 0x000200) ? " A2/EIAJ stereo "         : "",
880                         (value & 0x000400) ? " A2/EIAJ noise mute "     : "",
881
882                         (value & 0x000800) ? " BTSC/FM radio pilot "    : "",
883                         (value & 0x001000) ? " SAP carrier "            : "",
884                         (value & 0x002000) ? " BTSC stereo noise mute " : "",
885                         (value & 0x004000) ? " SAP noise mute "         : "",
886                         (value & 0x008000) ? " VDSP "                   : "",
887
888                         (value & 0x010000) ? " NICST "                  : "",
889                         (value & 0x020000) ? " NICDU "                  : "",
890                         (value & 0x040000) ? " NICAM muted "            : "",
891                         (value & 0x080000) ? " NICAM reserve sound "    : "",
892
893                         (value & 0x100000) ? " init done "              : "");
894         }
895
896  done:
897         complete_and_exit(&dev->thread.exit, 0);
898         return 0;
899 }
900
901 /* ------------------------------------------------------------------ */
902 /* common stuff + external entry points                               */
903
904 static void saa7134_enable_i2s(struct saa7134_dev *dev)
905 {
906         int i2s_format;
907
908         if (!card_is_empress(dev))
909                 return;
910         i2s_format = (dev->input->amux == TV) ? 0x00 : 0x01;
911
912         /* enable I2S audio output for the mpeg encoder */
913         saa_writeb(SAA7134_I2S_OUTPUT_SELECT,  0x80);
914         saa_writeb(SAA7134_I2S_OUTPUT_FORMAT,  i2s_format);
915         saa_writeb(SAA7134_I2S_OUTPUT_LEVEL,   0x0F);
916         saa_writeb(SAA7134_I2S_AUDIO_OUTPUT,   0x01);
917 }
918
919 int saa7134_tvaudio_rx2mode(u32 rx)
920 {
921         u32 mode;
922
923         mode = V4L2_TUNER_MODE_MONO;
924         if (rx & V4L2_TUNER_SUB_STEREO)
925                 mode = V4L2_TUNER_MODE_STEREO;
926         else if (rx & V4L2_TUNER_SUB_LANG1)
927                 mode = V4L2_TUNER_MODE_LANG1;
928         else if (rx & V4L2_TUNER_SUB_LANG2)
929                 mode = V4L2_TUNER_MODE_LANG2;
930         return mode;
931 }
932
933 void saa7134_tvaudio_setmute(struct saa7134_dev *dev)
934 {
935         switch (dev->pci->device) {
936         case PCI_DEVICE_ID_PHILIPS_SAA7130:
937         case PCI_DEVICE_ID_PHILIPS_SAA7134:
938                 mute_input_7134(dev);
939                 break;
940         case PCI_DEVICE_ID_PHILIPS_SAA7133:
941         case PCI_DEVICE_ID_PHILIPS_SAA7135:
942                 mute_input_7133(dev);
943                 break;
944         }
945 }
946
947 void saa7134_tvaudio_setinput(struct saa7134_dev *dev,
948                               struct saa7134_input *in)
949 {
950         dev->input = in;
951         switch (dev->pci->device) {
952         case PCI_DEVICE_ID_PHILIPS_SAA7130:
953         case PCI_DEVICE_ID_PHILIPS_SAA7134:
954                 mute_input_7134(dev);
955                 break;
956         case PCI_DEVICE_ID_PHILIPS_SAA7133:
957         case PCI_DEVICE_ID_PHILIPS_SAA7135:
958                 mute_input_7133(dev);
959                 break;
960         }
961         saa7134_enable_i2s(dev);
962 }
963
964 void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level)
965 {
966         switch (dev->pci->device) {
967         case PCI_DEVICE_ID_PHILIPS_SAA7134:
968                 saa_writeb(SAA7134_CHANNEL1_LEVEL,     level & 0x1f);
969                 saa_writeb(SAA7134_CHANNEL2_LEVEL,     level & 0x1f);
970                 saa_writeb(SAA7134_NICAM_LEVEL_ADJUST, level & 0x1f);
971                 break;
972         }
973 }
974
975 int saa7134_tvaudio_getstereo(struct saa7134_dev *dev)
976 {
977         int retval = V4L2_TUNER_SUB_MONO;
978
979         switch (dev->pci->device) {
980         case PCI_DEVICE_ID_PHILIPS_SAA7134:
981                 if (dev->tvaudio)
982                         retval = tvaudio_getstereo(dev,dev->tvaudio);
983                 break;
984         case PCI_DEVICE_ID_PHILIPS_SAA7133:
985         case PCI_DEVICE_ID_PHILIPS_SAA7135:
986                 retval = getstereo_7133(dev);
987                 break;
988         }
989         return retval;
990 }
991
992 int saa7134_tvaudio_init2(struct saa7134_dev *dev)
993 {
994         DECLARE_MUTEX_LOCKED(sem);
995         int (*my_thread)(void *data) = NULL;
996
997         switch (dev->pci->device) {
998         case PCI_DEVICE_ID_PHILIPS_SAA7134:
999                 my_thread = tvaudio_thread;
1000                 break;
1001         case PCI_DEVICE_ID_PHILIPS_SAA7133:
1002         case PCI_DEVICE_ID_PHILIPS_SAA7135:
1003                 my_thread = tvaudio_thread_ddep;
1004                 break;
1005         }
1006
1007         dev->thread.pid = -1;
1008         if (my_thread) {
1009                 /* start tvaudio thread */
1010                 init_waitqueue_head(&dev->thread.wq);
1011                 init_completion(&dev->thread.exit);
1012                 dev->thread.pid = kernel_thread(my_thread,dev,0);
1013                 if (dev->thread.pid < 0)
1014                         printk(KERN_WARNING "%s: kernel_thread() failed\n",
1015                                dev->name);
1016                 saa7134_tvaudio_do_scan(dev);
1017         }
1018
1019         saa7134_enable_i2s(dev);
1020         return 0;
1021 }
1022
1023 int saa7134_tvaudio_fini(struct saa7134_dev *dev)
1024 {
1025         /* shutdown tvaudio thread */
1026         if (dev->thread.pid >= 0) {
1027                 dev->thread.shutdown = 1;
1028                 wake_up_interruptible(&dev->thread.wq);
1029                 wait_for_completion(&dev->thread.exit);
1030         }
1031         saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, 0x00); /* LINE1 */
1032         return 0;
1033 }
1034
1035 int saa7134_tvaudio_do_scan(struct saa7134_dev *dev)
1036 {
1037         if (dev->thread.pid >= 0) {
1038                 dev->thread.mode = UNSET;
1039                 dev->thread.scan2++;
1040                 wake_up_interruptible(&dev->thread.wq);
1041         } else {
1042                 dev->automute = 0;
1043                 saa7134_tvaudio_setmute(dev);
1044         }
1045         return 0;
1046 }
1047
1048 EXPORT_SYMBOL(saa_dsp_writel);
1049
1050 /* ----------------------------------------------------------- */
1051 /*
1052  * Local variables:
1053  * c-basic-offset: 8
1054  * End:
1055  */
1056