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