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