Merge branch 'master'
[linux-2.6] / drivers / media / video / saa7134 / saa7134-tvaudio.c
1 /*
2  *
3  * device driver for philips saa7134 based TV cards
4  * tv audio decoder (fm stereo, nicam, ...)
5  *
6  * (c) 2001-03 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #include <linux/init.h>
24 #include <linux/list.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/kernel.h>
28 #include <linux/slab.h>
29 #include <linux/delay.h>
30 #include <linux/smp_lock.h>
31 #include <asm/div64.h>
32
33 #include "saa7134-reg.h"
34 #include "saa7134.h"
35
36 /* ------------------------------------------------------------------ */
37
38 static unsigned int audio_debug = 0;
39 module_param(audio_debug, int, 0644);
40 MODULE_PARM_DESC(audio_debug,"enable debug messages [tv audio]");
41
42 static unsigned int audio_ddep = 0;
43 module_param(audio_ddep, int, 0644);
44 MODULE_PARM_DESC(audio_ddep,"audio ddep overwrite");
45
46 static int audio_clock_override = UNSET;
47 module_param(audio_clock_override, int, 0644);
48
49 static int audio_clock_tweak = 0;
50 module_param(audio_clock_tweak, int, 0644);
51 MODULE_PARM_DESC(audio_clock_tweak, "Audio clock tick fine tuning for cards with audio crystal that's slightly off (range [-1024 .. 1024])");
52
53 #define dprintk(fmt, arg...)    if (audio_debug) \
54         printk(KERN_DEBUG "%s/audio: " fmt, dev->name , ## arg)
55 #define d2printk(fmt, arg...)   if (audio_debug > 1) \
56         printk(KERN_DEBUG "%s/audio: " fmt, dev->name, ## arg)
57
58 #define print_regb(reg) printk("%s:   reg 0x%03x [%-16s]: 0x%02x\n", \
59                 dev->name,(SAA7134_##reg),(#reg),saa_readb((SAA7134_##reg)))
60
61 /* msecs */
62 #define SCAN_INITIAL_DELAY     1000
63 #define SCAN_SAMPLE_DELAY       200
64 #define SCAN_SUBCARRIER_DELAY  2000
65
66 /* ------------------------------------------------------------------ */
67 /* saa7134 code                                                       */
68
69 static struct mainscan {
70         char         *name;
71         v4l2_std_id  std;
72         int          carr;
73 } mainscan[] = {
74         {
75                 .name = "M",
76                 .std  = V4L2_STD_NTSC | V4L2_STD_PAL_M,
77                 .carr = 4500,
78         },{
79                 .name = "BG",
80                 .std  = V4L2_STD_PAL_BG,
81                 .carr = 5500,
82         },{
83                 .name = "I",
84                 .std  = V4L2_STD_PAL_I,
85                 .carr = 6000,
86         },{
87                 .name = "DKL",
88                 .std  = V4L2_STD_PAL_DK | V4L2_STD_SECAM,
89                 .carr = 6500,
90         }
91 };
92
93 static struct saa7134_tvaudio tvaudio[] = {
94         {
95                 .name          = "PAL-B/G FM-stereo",
96                 .std           = V4L2_STD_PAL,
97                 .mode          = TVAUDIO_FM_BG_STEREO,
98                 .carr1         = 5500,
99                 .carr2         = 5742,
100         },{
101                 .name          = "PAL-D/K1 FM-stereo",
102                 .std           = V4L2_STD_PAL,
103                 .carr1         = 6500,
104                 .carr2         = 6258,
105                 .mode          = TVAUDIO_FM_BG_STEREO,
106         },{
107                 .name          = "PAL-D/K2 FM-stereo",
108                 .std           = V4L2_STD_PAL,
109                 .carr1         = 6500,
110                 .carr2         = 6742,
111                 .mode          = TVAUDIO_FM_BG_STEREO,
112         },{
113                 .name          = "PAL-D/K3 FM-stereo",
114                 .std           = V4L2_STD_PAL,
115                 .carr1         = 6500,
116                 .carr2         = 5742,
117                 .mode          = TVAUDIO_FM_BG_STEREO,
118         },{
119                 .name          = "PAL-B/G NICAM",
120                 .std           = V4L2_STD_PAL,
121                 .carr1         = 5500,
122                 .carr2         = 5850,
123                 .mode          = TVAUDIO_NICAM_FM,
124         },{
125                 .name          = "PAL-I NICAM",
126                 .std           = V4L2_STD_PAL,
127                 .carr1         = 6000,
128                 .carr2         = 6552,
129                 .mode          = TVAUDIO_NICAM_FM,
130         },{
131                 .name          = "PAL-D/K NICAM",
132                 .std           = V4L2_STD_PAL,
133                 .carr1         = 6500,
134                 .carr2         = 5850,
135                 .mode          = TVAUDIO_NICAM_FM,
136         },{
137                 .name          = "SECAM-L NICAM",
138                 .std           = V4L2_STD_SECAM,
139                 .carr1         = 6500,
140                 .carr2         = 5850,
141                 .mode          = TVAUDIO_NICAM_AM,
142         },{
143                 .name          = "SECAM-D/K",
144                 .std           = V4L2_STD_SECAM,
145                 .carr1         = 6500,
146                 .carr2         = -1,
147                 .mode          = TVAUDIO_FM_MONO,
148         },{
149                 .name          = "NTSC-M",
150                 .std           = V4L2_STD_NTSC,
151                 .carr1         = 4500,
152                 .carr2         = -1,
153                 .mode          = TVAUDIO_FM_MONO,
154         },{
155                 .name          = "NTSC-A2 FM-stereo",
156                 .std           = V4L2_STD_NTSC,
157                 .carr1         = 4500,
158                 .carr2         = 4724,
159                 .mode          = TVAUDIO_FM_K_STEREO,
160         }
161 };
162 #define TVAUDIO (sizeof(tvaudio)/sizeof(struct saa7134_tvaudio))
163
164 /* ------------------------------------------------------------------ */
165
166 static void tvaudio_init(struct saa7134_dev *dev)
167 {
168         int clock = saa7134_boards[dev->board].audio_clock;
169
170         if (UNSET != audio_clock_override)
171                 clock = audio_clock_override;
172
173         /* init all audio registers */
174         saa_writeb(SAA7134_AUDIO_PLL_CTRL,   0x00);
175         if (need_resched())
176                 schedule();
177         else
178                 udelay(10);
179
180         saa_writeb(SAA7134_AUDIO_CLOCK0,      clock        & 0xff);
181         saa_writeb(SAA7134_AUDIO_CLOCK1,     (clock >>  8) & 0xff);
182         saa_writeb(SAA7134_AUDIO_CLOCK2,     (clock >> 16) & 0xff);
183         // frame locked audio was reported not to be reliable
184         saa_writeb(SAA7134_AUDIO_PLL_CTRL,   0x02);
185
186         saa_writeb(SAA7134_NICAM_ERROR_LOW,  0x14);
187         saa_writeb(SAA7134_NICAM_ERROR_HIGH, 0x50);
188         saa_writeb(SAA7134_MONITOR_SELECT,   0xa0);
189         saa_writeb(SAA7134_FM_DEMATRIX,      0x80);
190 }
191
192 static u32 tvaudio_carr2reg(u32 carrier)
193 {
194         u64 a = carrier;
195
196         a <<= 24;
197         do_div(a,12288);
198         return a;
199 }
200
201 static void tvaudio_setcarrier(struct saa7134_dev *dev,
202                                int primary, int secondary)
203 {
204         if (-1 == secondary)
205                 secondary = primary;
206         saa_writel(SAA7134_CARRIER1_FREQ0 >> 2, tvaudio_carr2reg(primary));
207         saa_writel(SAA7134_CARRIER2_FREQ0 >> 2, tvaudio_carr2reg(secondary));
208 }
209
210 #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) {
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         DECLARE_WAITQUEUE(wait, current);
346
347         add_wait_queue(&dev->thread.wq, &wait);
348         if (dev->thread.scan1 == dev->thread.scan2 && !dev->thread.shutdown) {
349                 if (timeout < 0) {
350                         set_current_state(TASK_INTERRUPTIBLE);
351                         schedule();
352                 } else {
353                         schedule_timeout_interruptible
354                                                 (msecs_to_jiffies(timeout));
355                 }
356         }
357         remove_wait_queue(&dev->thread.wq, &wait);
358         return dev->thread.scan1 != dev->thread.scan2;
359 }
360
361 static int tvaudio_checkcarrier(struct saa7134_dev *dev, struct mainscan *scan)
362 {
363         __s32 left,right,value;
364
365         if (audio_debug > 1) {
366                 int i;
367                 dprintk("debug %d:",scan->carr);
368                 for (i = -150; i <= 150; i += 30) {
369                         tvaudio_setcarrier(dev,scan->carr+i,scan->carr+i);
370                         saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
371                         if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
372                                 return -1;
373                         value = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
374                         if (0 == i)
375                                 printk("  #  %6d  # ",value >> 16);
376                         else
377                                 printk(" %6d",value >> 16);
378                 }
379                 printk("\n");
380         }
381
382         if (dev->tvnorm->id & scan->std) {
383                 tvaudio_setcarrier(dev,scan->carr-90,scan->carr-90);
384                 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
385                 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
386                         return -1;
387                 left = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
388
389                 tvaudio_setcarrier(dev,scan->carr+90,scan->carr+90);
390                 saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
391                 if (tvaudio_sleep(dev,SCAN_SAMPLE_DELAY))
392                         return -1;
393                 right = saa_readl(SAA7134_LEVEL_READOUT1 >> 2);
394
395                 left >>= 16;
396                 right >>= 16;
397                 value = left > right ? left - right : right - left;
398                 dprintk("scanning %d.%03d MHz [%4s] =>  dc is %5d [%d/%d]\n",
399                         scan->carr / 1000, scan->carr % 1000,
400                         scan->name, value, left, right);
401         } else {
402                 value = 0;
403                 dprintk("skipping %d.%03d MHz [%4s]\n",
404                         scan->carr / 1000, scan->carr % 1000, scan->name);
405         }
406         return value;
407 }
408
409
410 static int tvaudio_getstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio)
411 {
412         __u32 idp, nicam, nicam_status;
413         int retval = -1;
414
415         switch (audio->mode) {
416         case TVAUDIO_FM_MONO:
417                 return V4L2_TUNER_SUB_MONO;
418         case TVAUDIO_FM_K_STEREO:
419         case TVAUDIO_FM_BG_STEREO:
420                 idp = (saa_readb(SAA7134_IDENT_SIF) & 0xe0) >> 5;
421                 dprintk("getstereo: fm/stereo: idp=0x%x\n",idp);
422                 if (0x03 == (idp & 0x03))
423                         retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
424                 else if (0x05 == (idp & 0x05))
425                         retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
426                 else if (0x01 == (idp & 0x01))
427                         retval = V4L2_TUNER_SUB_MONO;
428                 break;
429         case TVAUDIO_FM_SAT_STEREO:
430                 /* not implemented (yet) */
431                 break;
432         case TVAUDIO_NICAM_FM:
433         case TVAUDIO_NICAM_AM:
434                 nicam = saa_readb(SAA7134_AUDIO_STATUS);
435                 dprintk("getstereo: nicam=0x%x\n",nicam);
436                 if (nicam & 0x1) {
437                         nicam_status = saa_readb(SAA7134_NICAM_STATUS);
438                         dprintk("getstereo: nicam_status=0x%x\n", nicam_status);
439
440                         switch (nicam_status & 0x03) {
441                             case 0x01:
442                                 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
443                                 break;
444                             case 0x02:
445                                 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
446                                 break;
447                             default:
448                                 retval = V4L2_TUNER_SUB_MONO;
449                         }
450                 } else {
451                         /* No nicam detected */
452                 }
453                 break;
454         }
455         if (retval != -1)
456                 dprintk("found audio subchannels:%s%s%s%s\n",
457                         (retval & V4L2_TUNER_SUB_MONO)   ? " mono"   : "",
458                         (retval & V4L2_TUNER_SUB_STEREO) ? " stereo" : "",
459                         (retval & V4L2_TUNER_SUB_LANG1)  ? " lang1"  : "",
460                         (retval & V4L2_TUNER_SUB_LANG2)  ? " lang2"  : "");
461         return retval;
462 }
463
464 static int tvaudio_setstereo(struct saa7134_dev *dev, struct saa7134_tvaudio *audio,
465                              u32 mode)
466 {
467         static char *name[] = {
468                 [ V4L2_TUNER_MODE_MONO   ] = "mono",
469                 [ V4L2_TUNER_MODE_STEREO ] = "stereo",
470                 [ V4L2_TUNER_MODE_LANG1  ] = "lang1",
471                 [ V4L2_TUNER_MODE_LANG2  ] = "lang2",
472         };
473         static u32 fm[] = {
474                 [ V4L2_TUNER_MODE_MONO   ] = 0x00,  /* ch1  */
475                 [ V4L2_TUNER_MODE_STEREO ] = 0x80,  /* auto */
476                 [ V4L2_TUNER_MODE_LANG1  ] = 0x00,  /* ch1  */
477                 [ V4L2_TUNER_MODE_LANG2  ] = 0x01,  /* ch2  */
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                         max1 = 12345;
544                         carrier = default_carrier;
545                 } else {
546                         /* scan for the main carrier */
547                         saa_writeb(SAA7134_MONITOR_SELECT,0x00);
548                         tvaudio_setmode(dev,&tvaudio[0],NULL);
549                         for (i = 0; i < ARRAY_SIZE(mainscan); i++) {
550                                 carr_vals[i] = tvaudio_checkcarrier(dev, mainscan+i);
551                                 if (dev->thread.scan1 != dev->thread.scan2)
552                                         goto restart;
553                         }
554                         for (max1 = 0, max2 = 0, i = 0; i < ARRAY_SIZE(mainscan); i++) {
555                                 if (max1 < carr_vals[i]) {
556                                         max2 = max1;
557                                         max1 = carr_vals[i];
558                                         carrier = mainscan[i].carr;
559                                 } else if (max2 < carr_vals[i]) {
560                                         max2 = carr_vals[i];
561                                 }
562                         }
563                 }
564
565                 if (0 != carrier && max1 > 2000 && max1 > max2*3) {
566                         /* found good carrier */
567                         dprintk("found %s main sound carrier @ %d.%03d MHz [%d/%d]\n",
568                                 dev->tvnorm->name, carrier/1000, carrier%1000,
569                                 max1, max2);
570                         dev->last_carrier = carrier;
571
572                 } else if (0 != dev->last_carrier) {
573                         /* no carrier -- try last detected one as fallback */
574                         carrier = dev->last_carrier;
575                         dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
576                                "using %d.%03d MHz [last detected]\n",
577                                dev->name, carrier/1000, carrier%1000);
578
579                 } else {
580                         /* no carrier + no fallback -- use default */
581                         carrier = default_carrier;
582                         dprintk(KERN_WARNING "%s/audio: audio carrier scan failed, "
583                                "using %d.%03d MHz [default]\n",
584                                dev->name, carrier/1000, carrier%1000);
585                 }
586                 tvaudio_setcarrier(dev,carrier,carrier);
587                 dev->automute = 0;
588                 saa_andorb(SAA7134_STEREO_DAC_OUTPUT_SELECT, 0x30, 0x00);
589                 saa7134_tvaudio_setmute(dev);
590
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
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                 tvaudio_setstereo(dev,&tvaudio[audio],V4L2_TUNER_MODE_MONO);
614                 dev->tvaudio = &tvaudio[audio];
615
616                 lastmode = 42;
617                 for (;;) {
618                         if (tvaudio_sleep(dev,5000))
619                                 goto restart;
620                         if (dev->thread.shutdown || signal_pending(current))
621                                 break;
622                         if (UNSET == dev->thread.mode) {
623                                 rx = tvaudio_getstereo(dev,&tvaudio[i]);
624                                 mode = saa7134_tvaudio_rx2mode(rx);
625                         } else {
626                                 mode = dev->thread.mode;
627                         }
628                         if (lastmode != mode) {
629                                 tvaudio_setstereo(dev,&tvaudio[audio],mode);
630                                 lastmode = mode;
631                         }
632                 }
633         }
634
635  done:
636         complete_and_exit(&dev->thread.exit, 0);
637         return 0;
638 }
639
640 /* ------------------------------------------------------------------ */
641 /* saa7133 / saa7135 code                                             */
642
643 static char *stdres[0x20] = {
644         [0x00] = "no standard detected",
645         [0x01] = "B/G (in progress)",
646         [0x02] = "D/K (in progress)",
647         [0x03] = "M (in progress)",
648
649         [0x04] = "B/G A2",
650         [0x05] = "B/G NICAM",
651         [0x06] = "D/K A2 (1)",
652         [0x07] = "D/K A2 (2)",
653         [0x08] = "D/K A2 (3)",
654         [0x09] = "D/K NICAM",
655         [0x0a] = "L NICAM",
656         [0x0b] = "I NICAM",
657
658         [0x0c] = "M Korea",
659         [0x0d] = "M BTSC ",
660         [0x0e] = "M EIAJ",
661
662         [0x0f] = "FM radio / IF 10.7 / 50 deemp",
663         [0x10] = "FM radio / IF 10.7 / 75 deemp",
664         [0x11] = "FM radio / IF sel / 50 deemp",
665         [0x12] = "FM radio / IF sel / 75 deemp",
666
667         [0x13 ... 0x1e ] = "unknown",
668         [0x1f] = "??? [in progress]",
669 };
670
671 #define DSP_RETRY 32
672 #define DSP_DELAY 16
673
674 static inline int saa_dsp_wait_bit(struct saa7134_dev *dev, int bit)
675 {
676         int state, count = DSP_RETRY;
677
678         state = saa_readb(SAA7135_DSP_RWSTATE);
679         if (unlikely(state & SAA7135_DSP_RWSTATE_ERR)) {
680                 printk("%s: dsp access error\n",dev->name);
681                 /* FIXME: send ack ... */
682                 return -EIO;
683         }
684         while (0 == (state & bit)) {
685                 if (unlikely(0 == count)) {
686                         printk("%s: dsp access wait timeout [bit=%s]\n",
687                                dev->name,
688                                (bit & SAA7135_DSP_RWSTATE_WRR) ? "WRR" :
689                                (bit & SAA7135_DSP_RWSTATE_RDB) ? "RDB" :
690                                (bit & SAA7135_DSP_RWSTATE_IDA) ? "IDA" :
691                                "???");
692                         return -EIO;
693                 }
694                 saa_wait(DSP_DELAY);
695                 state = saa_readb(SAA7135_DSP_RWSTATE);
696                 count--;
697         }
698         return 0;
699 }
700
701
702 int saa_dsp_writel(struct saa7134_dev *dev, int reg, u32 value)
703 {
704         int err;
705
706         d2printk("dsp write reg 0x%x = 0x%06x\n",reg<<2,value);
707         err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
708         if (err < 0)
709                 return err;
710         saa_writel(reg,value);
711         err = saa_dsp_wait_bit(dev,SAA7135_DSP_RWSTATE_WRR);
712         if (err < 0)
713                 return err;
714         return 0;
715 }
716
717 static int getstereo_7133(struct saa7134_dev *dev)
718 {
719         int retval = V4L2_TUNER_SUB_MONO;
720         u32 value;
721
722         value = saa_readl(0x528 >> 2);
723         if (value & 0x20)
724                 retval = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
725         if (value & 0x40)
726                 retval = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
727         return retval;
728 }
729
730 static int mute_input_7133(struct saa7134_dev *dev)
731 {
732         u32 reg = 0;
733         u32 xbarin, xbarout;
734         int mask;
735         struct saa7134_input *in;
736
737         /* Hac 0506 route OSS sound simultanously  */
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                         norms = (0x0f << 2) | 0x01;
813                 } else {
814                         /* (let chip) scan for sound carrier */
815                         norms = 0;
816                         if (dev->tvnorm->id & V4L2_STD_PAL) {
817                                 dprintk("PAL scan\n");
818                                 norms |= 0x2c; /* B/G + D/K + I */
819                         }
820                         if (dev->tvnorm->id & V4L2_STD_NTSC) {
821                                 dprintk("NTSC scan\n");
822                                 norms |= 0x40; /* M */
823                         }
824                         if (dev->tvnorm->id & V4L2_STD_SECAM) {
825                                 dprintk("SECAM scan\n");
826                                 norms |= 0x18; /* L + D/K */
827                         }
828                         if (0 == norms)
829                                 norms = 0x7c; /* all */
830                         dprintk("scanning:%s%s%s%s%s\n",
831                                 (norms & 0x04) ? " B/G"  : "",
832                                 (norms & 0x08) ? " D/K"  : "",
833                                 (norms & 0x10) ? " L/L'" : "",
834                                 (norms & 0x20) ? " I"    : "",
835                                 (norms & 0x40) ? " M"    : "");
836                 }
837
838                 /* kick automatic standard detection */
839                 saa_dsp_writel(dev, 0x454 >> 2, 0);
840                 saa_dsp_writel(dev, 0x454 >> 2, norms | 0x80);
841
842                 /* setup crossbars */
843                 saa_dsp_writel(dev, 0x464 >> 2, 0x000000);
844                 saa_dsp_writel(dev, 0x470 >> 2, 0x101010);
845
846                 if (tvaudio_sleep(dev,3000))
847                         goto restart;
848                 value = saa_readl(0x528 >> 2) & 0xffffff;
849
850                 dprintk("tvaudio thread status: 0x%x [%s%s%s]\n",
851                         value, stdres[value & 0x1f],
852                         (value & 0x000020) ? ",stereo" : "",
853                         (value & 0x000040) ? ",dual"   : "");
854                 dprintk("detailed status: "
855                         "%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n",
856                         (value & 0x000080) ? " A2/EIAJ pilot tone "     : "",
857                         (value & 0x000100) ? " A2/EIAJ dual "           : "",
858                         (value & 0x000200) ? " A2/EIAJ stereo "         : "",
859                         (value & 0x000400) ? " A2/EIAJ noise mute "     : "",
860
861                         (value & 0x000800) ? " BTSC/FM radio pilot "    : "",
862                         (value & 0x001000) ? " SAP carrier "            : "",
863                         (value & 0x002000) ? " BTSC stereo noise mute " : "",
864                         (value & 0x004000) ? " SAP noise mute "         : "",
865                         (value & 0x008000) ? " VDSP "                   : "",
866
867                         (value & 0x010000) ? " NICST "                  : "",
868                         (value & 0x020000) ? " NICDU "                  : "",
869                         (value & 0x040000) ? " NICAM muted "            : "",
870                         (value & 0x080000) ? " NICAM reserve sound "    : "",
871
872                         (value & 0x100000) ? " init done "              : "");
873         }
874
875  done:
876         complete_and_exit(&dev->thread.exit, 0);
877         return 0;
878 }
879
880 /* ------------------------------------------------------------------ */
881 /* common stuff + external entry points                               */
882
883 static void saa7134_enable_i2s(struct saa7134_dev *dev)
884 {
885         int i2s_format;
886
887         if (!card_is_empress(dev))
888                 return;
889         i2s_format = (dev->input->amux == TV) ? 0x00 : 0x01;
890
891         /* enable I2S audio output for the mpeg encoder */
892         saa_writeb(SAA7134_I2S_OUTPUT_SELECT,  0x80);
893         saa_writeb(SAA7134_I2S_OUTPUT_FORMAT,  i2s_format);
894         saa_writeb(SAA7134_I2S_OUTPUT_LEVEL,   0x0F);
895         saa_writeb(SAA7134_I2S_AUDIO_OUTPUT,   0x01);
896 }
897
898 int saa7134_tvaudio_rx2mode(u32 rx)
899 {
900         u32 mode;
901
902         mode = V4L2_TUNER_MODE_MONO;
903         if (rx & V4L2_TUNER_SUB_STEREO)
904                 mode = V4L2_TUNER_MODE_STEREO;
905         else if (rx & V4L2_TUNER_SUB_LANG1)
906                 mode = V4L2_TUNER_MODE_LANG1;
907         else if (rx & V4L2_TUNER_SUB_LANG2)
908                 mode = V4L2_TUNER_MODE_LANG2;
909         return mode;
910 }
911
912 void saa7134_tvaudio_setmute(struct saa7134_dev *dev)
913 {
914         switch (dev->pci->device) {
915         case PCI_DEVICE_ID_PHILIPS_SAA7130:
916         case PCI_DEVICE_ID_PHILIPS_SAA7134:
917                 mute_input_7134(dev);
918                 break;
919         case PCI_DEVICE_ID_PHILIPS_SAA7133:
920         case PCI_DEVICE_ID_PHILIPS_SAA7135:
921                 mute_input_7133(dev);
922                 break;
923         }
924 }
925
926 void saa7134_tvaudio_setinput(struct saa7134_dev *dev,
927                               struct saa7134_input *in)
928 {
929         dev->input = in;
930         switch (dev->pci->device) {
931         case PCI_DEVICE_ID_PHILIPS_SAA7130:
932         case PCI_DEVICE_ID_PHILIPS_SAA7134:
933                 mute_input_7134(dev);
934                 break;
935         case PCI_DEVICE_ID_PHILIPS_SAA7133:
936         case PCI_DEVICE_ID_PHILIPS_SAA7135:
937                 mute_input_7133(dev);
938                 break;
939         }
940         saa7134_enable_i2s(dev);
941 }
942
943 void saa7134_tvaudio_setvolume(struct saa7134_dev *dev, int level)
944 {
945         switch (dev->pci->device) {
946         case PCI_DEVICE_ID_PHILIPS_SAA7134:
947                 saa_writeb(SAA7134_CHANNEL1_LEVEL,     level & 0x1f);
948                 saa_writeb(SAA7134_CHANNEL2_LEVEL,     level & 0x1f);
949                 saa_writeb(SAA7134_NICAM_LEVEL_ADJUST, level & 0x1f);
950                 break;
951         }
952 }
953
954 int saa7134_tvaudio_getstereo(struct saa7134_dev *dev)
955 {
956         int retval = V4L2_TUNER_SUB_MONO;
957
958         switch (dev->pci->device) {
959         case PCI_DEVICE_ID_PHILIPS_SAA7134:
960                 if (dev->tvaudio)
961                         retval = tvaudio_getstereo(dev,dev->tvaudio);
962                 break;
963         case PCI_DEVICE_ID_PHILIPS_SAA7133:
964         case PCI_DEVICE_ID_PHILIPS_SAA7135:
965                 retval = getstereo_7133(dev);
966                 break;
967         }
968         return retval;
969 }
970
971 int saa7134_tvaudio_init2(struct saa7134_dev *dev)
972 {
973         DECLARE_MUTEX_LOCKED(sem);
974         int (*my_thread)(void *data) = NULL;
975
976         switch (dev->pci->device) {
977         case PCI_DEVICE_ID_PHILIPS_SAA7134:
978                 my_thread = tvaudio_thread;
979                 break;
980         case PCI_DEVICE_ID_PHILIPS_SAA7133:
981         case PCI_DEVICE_ID_PHILIPS_SAA7135:
982                 my_thread = tvaudio_thread_ddep;
983                 break;
984         }
985
986         dev->thread.pid = -1;
987         if (my_thread) {
988                 /* start tvaudio thread */
989                 init_waitqueue_head(&dev->thread.wq);
990                 init_completion(&dev->thread.exit);
991                 dev->thread.pid = kernel_thread(my_thread,dev,0);
992                 if (dev->thread.pid < 0)
993                         printk(KERN_WARNING "%s: kernel_thread() failed\n",
994                                dev->name);
995                 saa7134_tvaudio_do_scan(dev);
996         }
997
998         saa7134_enable_i2s(dev);
999         return 0;
1000 }
1001
1002 int saa7134_tvaudio_fini(struct saa7134_dev *dev)
1003 {
1004         /* shutdown tvaudio thread */
1005         if (dev->thread.pid >= 0) {
1006                 dev->thread.shutdown = 1;
1007                 wake_up_interruptible(&dev->thread.wq);
1008                 wait_for_completion(&dev->thread.exit);
1009         }
1010         saa_andorb(SAA7134_ANALOG_IO_SELECT, 0x07, 0x00); /* LINE1 */
1011         return 0;
1012 }
1013
1014 int saa7134_tvaudio_do_scan(struct saa7134_dev *dev)
1015 {
1016         if (dev->thread.pid >= 0) {
1017                 dev->thread.mode = UNSET;
1018                 dev->thread.scan2++;
1019                 wake_up_interruptible(&dev->thread.wq);
1020         } else {
1021                 dev->automute = 0;
1022                 saa7134_tvaudio_setmute(dev);
1023         }
1024         return 0;
1025 }
1026
1027 EXPORT_SYMBOL(saa_dsp_writel);
1028
1029 /* ----------------------------------------------------------- */
1030 /*
1031  * Local variables:
1032  * c-basic-offset: 8
1033  * End:
1034  */
1035