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