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