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