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