xref: /dragonfly/sys/dev/video/bktr/msp34xx.c (revision b7367ef6)
1 /*-
2  * Copyright (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/dev/bktr/msp34xx.c,v 1.5 2004/12/16 23:19:57 julian Exp
27  * $DragonFly: src/sys/dev/video/bktr/msp34xx.c,v 1.8 2007/10/03 19:27:08 swildner Exp $
28  */
29 
30 /*
31  * programming the msp34* sound processor family
32  *
33  * (c) 1997-2001 Gerd Knorr <kraxel@bytesex.org>
34  *
35  * what works and what doesn't:
36  *
37  *  AM-Mono
38  *      Support for Hauppauge cards added (decoding handled by tuner) added by
39  *      Frederic Crozat <fcrozat@mail.dotcom.fr>
40  *
41  *  FM-Mono
42  *      should work. The stereo modes are backward compatible to FM-mono,
43  *      therefore FM-Mono should be allways available.
44  *
45  *  FM-Stereo (B/G, used in germany)
46  *      should work, with autodetect
47  *
48  *  FM-Stereo (satellite)
49  *      should work, no autodetect (i.e. default is mono, but you can
50  *      switch to stereo -- untested)
51  *
52  *  NICAM (B/G, L , used in UK, Scandinavia, Spain and France)
53  *      should work, with autodetect. Support for NICAM was added by
54  *      Pekka Pietikainen <pp@netppl.fi>
55  *
56  *
57  * TODO:
58  *   - better SAT support
59  *
60  *
61  * 980623  Thomas Sailer (sailer@ife.ee.ethz.ch)
62  *         using soundcore instead of OSS
63  *
64  *
65  * The FreeBSD modifications by Alexander Langer <alex@FreeBSD.org>
66  * are in the public domain.  Please contact me (Alex) and not Gerd for
67  * any problems you encounter under FreeBSD.
68  *
69  * FreeBSD TODO:
70  * - mutex handling (currently not mp-safe)
71  * - the various options here as loader tunables or compile time or whatever
72  * - how does the new dolby flag work with the current dpl_* stuff?
73  *   Maybe it's just enough to set the dolby flag to 1 and it works.
74  *   As I don't have a dolby card myself, I can't test it, though.
75  */
76 
77 #include "opt_bktr.h"			/* Include any kernel config options */
78 #ifdef BKTR_NEW_MSP34XX_DRIVER		/* file only needed for new driver */
79 
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
83 #include <sys/vnode.h>
84 #include <sys/bus.h>
85 #include <sys/unistd.h>
86 #include <sys/kthread.h>
87 #include <sys/malloc.h>
88 
89 #include <dev/video/meteor/ioctl_meteor.h>
90 #include <dev/video/bktr/ioctl_bt848.h>	/* extensions to ioctl_meteor.h */
91 #include <dev/video/bktr/bktr_reg.h>
92 #include <dev/video/bktr/bktr_tuner.h>
93 #include <dev/video/bktr/bktr_audio.h>
94 #include <dev/video/bktr/bktr_core.h>
95 
96 #define VIDEO_MODE_PAL          0
97 #define VIDEO_MODE_NTSC         1
98 #define VIDEO_MODE_SECAM        2
99 #define VIDEO_MODE_AUTO         3
100 
101 #define VIDEO_SOUND_MONO	1
102 #define VIDEO_SOUND_STEREO	2
103 #define VIDEO_SOUND_LANG1	4
104 #define VIDEO_SOUND_LANG2	8
105 
106 #define DFP_COUNT 0x41
107 static const int bl_dfp[] = {
108 	0x00, 0x01, 0x02, 0x03,  0x06, 0x08, 0x09, 0x0a,
109 	0x0b, 0x0d, 0x0e, 0x10
110 };
111 
112 struct msp3400c {
113 	int simple;
114 	int nicam;
115 	int mode;
116 	int norm;
117 	int stereo;
118 	int nicam_on;
119 	int acb;
120 	int main, second;	/* sound carrier */
121 	int input;
122 
123 	int muted;
124 	int left, right;	/* volume */
125 	int bass, treble;
126 
127 	/* shadow register set */
128 	int dfp_regs[DFP_COUNT];
129 
130 	/* thread */
131 	struct thread	    *kthread;
132 	char                *threaddesc;
133 
134 	int                  active,restart,rmmod;
135 
136 	int                  watch_stereo;
137 	int		     halt_thread;
138 };
139 
140 #define VIDEO_MODE_RADIO 16      /* norm magic for radio mode */
141 
142 /* ---------------------------------------------------------------------- */
143 
144 #define dprintk(args) do {						\
145 	if (bootverbose) {						\
146 		kprintf("%s: ", bktr_name(client));			\
147 		kprintf args;						\
148 	}								\
149 } while (0)
150 
151 /* ---------------------------------------------------------------------- */
152 
153 #define I2C_MSP3400C       0x80
154 #define I2C_MSP3400C_DEM   0x10
155 #define I2C_MSP3400C_DFP   0x12
156 
157 /* ----------------------------------------------------------------------- */
158 /* functions for talking to the MSP3400C Sound processor                   */
159 
160 static int msp3400c_reset(bktr_ptr_t client)
161 {
162 	/* use our own which handles config(8) options */
163 	msp_dpl_reset(client, client->msp_addr);
164 
165         return 0;
166 }
167 
168 static int
169 msp3400c_read(bktr_ptr_t client, int dev, int addr)
170 {
171 	/* use our own */
172 	return(msp_dpl_read(client, client->msp_addr, dev, addr));
173 }
174 
175 static int
176 msp3400c_write(bktr_ptr_t client, int dev, int addr, int val)
177 {
178 	/* use our own */
179 	msp_dpl_write(client, client->msp_addr, dev, addr, val);
180 
181 	return(0);
182 }
183 
184 /* ------------------------------------------------------------------------ */
185 
186 /* This macro is allowed for *constants* only, gcc must calculate it
187    at compile time.  Remember -- no floats in kernel mode */
188 #define MSP_CARRIER(freq) ((int)((float)(freq/18.432)*(1<<24)))
189 
190 #define MSP_MODE_AM_DETECT   0
191 #define MSP_MODE_FM_RADIO    2
192 #define MSP_MODE_FM_TERRA    3
193 #define MSP_MODE_FM_SAT      4
194 #define MSP_MODE_FM_NICAM1   5
195 #define MSP_MODE_FM_NICAM2   6
196 #define MSP_MODE_AM_NICAM    7
197 #define MSP_MODE_BTSC        8
198 #define MSP_MODE_EXTERN      9
199 
200 static struct MSP_INIT_DATA_DEM {
201 	int fir1[6];
202 	int fir2[6];
203 	int cdo1;
204 	int cdo2;
205 	int ad_cv;
206 	int mode_reg;
207 	int dfp_src;
208 	int dfp_matrix;
209 } msp_init_data[] = {
210 	/* AM (for carrier detect / msp3400) */
211 	{ { 75, 19, 36, 35, 39, 40 }, { 75, 19, 36, 35, 39, 40 },
212 	  MSP_CARRIER(5.5), MSP_CARRIER(5.5),
213 	  0x00d0, 0x0500,   0x0020, 0x3000},
214 
215 	/* AM (for carrier detect / msp3410) */
216 	{ { -1, -1, -8, 2, 59, 126 }, { -1, -1, -8, 2, 59, 126 },
217 	  MSP_CARRIER(5.5), MSP_CARRIER(5.5),
218 	  0x00d0, 0x0100,   0x0020, 0x3000},
219 
220 	/* FM Radio */
221 	{ { -8, -8, 4, 6, 78, 107 }, { -8, -8, 4, 6, 78, 107 },
222 	  MSP_CARRIER(10.7), MSP_CARRIER(10.7),
223 	  0x00d0, 0x0480, 0x0020, 0x3000 },
224 
225 	/* Terrestial FM-mono + FM-stereo */
226 	{ {  3, 18, 27, 48, 66, 72 }, {  3, 18, 27, 48, 66, 72 },
227 	  MSP_CARRIER(5.5), MSP_CARRIER(5.5),
228 	  0x00d0, 0x0480,   0x0030, 0x3000},
229 
230 	/* Sat FM-mono */
231 	{ {  1,  9, 14, 24, 33, 37 }, {  3, 18, 27, 48, 66, 72 },
232 	  MSP_CARRIER(6.5), MSP_CARRIER(6.5),
233 	  0x00c6, 0x0480,   0x0000, 0x3000},
234 
235 	/* NICAM/FM --  B/G (5.5/5.85), D/K (6.5/5.85) */
236 	{ { -2, -8, -10, 10, 50, 86 }, {  3, 18, 27, 48, 66, 72 },
237 	  MSP_CARRIER(5.5), MSP_CARRIER(5.5),
238 	  0x00d0, 0x0040,   0x0120, 0x3000},
239 
240 	/* NICAM/FM -- I (6.0/6.552) */
241 	{ {  2, 4, -6, -4, 40, 94 }, {  3, 18, 27, 48, 66, 72 },
242 	  MSP_CARRIER(6.0), MSP_CARRIER(6.0),
243 	  0x00d0, 0x0040,   0x0120, 0x3000},
244 
245 	/* NICAM/AM -- L (6.5/5.85) */
246 	{ {  -2, -8, -10, 10, 50, 86 }, {  -4, -12, -9, 23, 79, 126 },
247 	  MSP_CARRIER(6.5), MSP_CARRIER(6.5),
248 	  0x00c6, 0x0140,   0x0120, 0x7c03},
249 };
250 
251 struct CARRIER_DETECT {
252 	int   cdo;
253 	char *name;
254 };
255 
256 static struct CARRIER_DETECT carrier_detect_main[] = {
257 	/* main carrier */
258 	{ MSP_CARRIER(4.5),        "4.5   NTSC"                   },
259 	{ MSP_CARRIER(5.5),        "5.5   PAL B/G"                },
260 	{ MSP_CARRIER(6.0),        "6.0   PAL I"                  },
261 	{ MSP_CARRIER(6.5),        "6.5   PAL D/K + SAT + SECAM"  }
262 };
263 
264 static struct CARRIER_DETECT carrier_detect_55[] = {
265 	/* PAL B/G */
266 	{ MSP_CARRIER(5.7421875),  "5.742 PAL B/G FM-stereo"     },
267 	{ MSP_CARRIER(5.85),       "5.85  PAL B/G NICAM"         }
268 };
269 
270 static struct CARRIER_DETECT carrier_detect_65[] = {
271 	/* PAL SAT / SECAM */
272 	{ MSP_CARRIER(5.85),       "5.85  PAL D/K + SECAM NICAM" },
273 	{ MSP_CARRIER(6.2578125),  "6.25  PAL D/K1 FM-stereo" },
274 	{ MSP_CARRIER(6.7421875),  "6.74  PAL D/K2 FM-stereo" },
275 	{ MSP_CARRIER(7.02),       "7.02  PAL SAT FM-stereo s/b" },
276 	{ MSP_CARRIER(7.20),       "7.20  PAL SAT FM-stereo s"   },
277 	{ MSP_CARRIER(7.38),       "7.38  PAL SAT FM-stereo b"   },
278 };
279 
280 #define CARRIER_COUNT(x) (sizeof(x)/sizeof(struct CARRIER_DETECT))
281 
282 /* ----------------------------------------------------------------------- */
283 
284 #define SCART_MASK    0
285 #define SCART_IN1     1
286 #define SCART_IN2     2
287 #define SCART_IN1_DA  3
288 #define SCART_IN2_DA  4
289 #define SCART_IN3     5
290 #define SCART_IN4     6
291 #define SCART_MONO    7
292 #define SCART_MUTE    8
293 
294 static int scarts[3][9] = {
295   /* MASK    IN1     IN2     IN1_DA  IN2_DA  IN3     IN4     MONO    MUTE   */
296   {  0x0320, 0x0000, 0x0200, -1,     -1,     0x0300, 0x0020, 0x0100, 0x0320 },
297   {  0x0c40, 0x0440, 0x0400, 0x0c00, 0x0040, 0x0000, 0x0840, 0x0800, 0x0c40 },
298   {  0x3080, 0x1000, 0x1080, 0x0000, 0x0080, 0x2080, 0x3080, 0x2000, 0x3000 },
299 };
300 
301 static char *scart_names[] = {
302   "mask", "in1", "in2", "in1 da", "in2 da", "in3", "in4", "mono", "mute"
303 };
304 
305 static void
306 msp3400c_set_scart(bktr_ptr_t client, int in, int out)
307 {
308 	struct msp3400c *msp = client->msp3400c_info;
309 
310 	if (-1 == scarts[out][in])
311 		return;
312 
313 	dprintk(("msp34xx: scart switch: %s => %d\n", scart_names[in], out));
314 	msp->acb &= ~scarts[out][SCART_MASK];
315 	msp->acb |=  scarts[out][in];
316 	msp3400c_write(client,I2C_MSP3400C_DFP, 0x0013, msp->acb);
317 }
318 
319 /* ------------------------------------------------------------------------ */
320 
321 static void msp3400c_setcarrier(bktr_ptr_t client, int cdo1, int cdo2)
322 {
323 	msp3400c_write(client,I2C_MSP3400C_DEM, 0x0093, cdo1 & 0xfff);
324 	msp3400c_write(client,I2C_MSP3400C_DEM, 0x009b, cdo1 >> 12);
325 	msp3400c_write(client,I2C_MSP3400C_DEM, 0x00a3, cdo2 & 0xfff);
326 	msp3400c_write(client,I2C_MSP3400C_DEM, 0x00ab, cdo2 >> 12);
327 	msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
328 }
329 
330 static void msp3400c_setvolume(bktr_ptr_t client,
331 			       int muted, int left, int right)
332 {
333 	int vol = 0,val = 0,balance = 0;
334 
335 	if (!muted) {
336 		vol     = (left > right) ? left : right;
337 		val     = (vol * 0x73 / 65535) << 8;
338 	}
339 	if (vol > 0) {
340 		balance = ((right-left) * 127) / vol;
341 	}
342 
343 	dprintk(("msp34xx: setvolume: mute=%s %d:%d  v=0x%02x b=0x%02x\n",
344 		muted ? "on" : "off", left, right, val>>8, balance));
345 	msp3400c_write(client,I2C_MSP3400C_DFP, 0x0000, val); /* loudspeaker */
346 	msp3400c_write(client,I2C_MSP3400C_DFP, 0x0006, val); /* headphones  */
347 	/* scart - on/off only */
348 	msp3400c_write(client,I2C_MSP3400C_DFP, 0x0007, val ? 0x4000 : 0);
349 	msp3400c_write(client,I2C_MSP3400C_DFP, 0x0001, balance << 8);
350 }
351 
352 static void msp3400c_setbass(bktr_ptr_t client, int bass)
353 {
354 	int val = ((bass-32768) * 0x60 / 65535) << 8;
355 
356 	dprintk(("msp34xx: setbass: %d 0x%02x\n",bass, val>>8));
357 	msp3400c_write(client,I2C_MSP3400C_DFP, 0x0002, val); /* loudspeaker */
358 }
359 
360 static void msp3400c_settreble(bktr_ptr_t client, int treble)
361 {
362 	int val = ((treble-32768) * 0x60 / 65535) << 8;
363 
364 	dprintk(("msp34xx: settreble: %d 0x%02x\n",treble, val>>8));
365 	msp3400c_write(client,I2C_MSP3400C_DFP, 0x0003, val); /* loudspeaker */
366 }
367 
368 static void msp3400c_setmode(bktr_ptr_t client, int type)
369 {
370 	struct msp3400c *msp = client->msp3400c_info;
371 	int i;
372 
373 	dprintk(("msp3400: setmode: %d\n",type));
374 	msp->mode   = type;
375 	msp->stereo = VIDEO_SOUND_MONO;
376 
377 	msp3400c_write(client,I2C_MSP3400C_DEM, 0x00bb,          /* ad_cv */
378 		       msp_init_data[type].ad_cv);
379 
380 	for (i = 5; i >= 0; i--)                                   /* fir 1 */
381 		msp3400c_write(client,I2C_MSP3400C_DEM, 0x0001,
382 			       msp_init_data[type].fir1[i]);
383 
384 	msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0004); /* fir 2 */
385 	msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0040);
386 	msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005, 0x0000);
387 	for (i = 5; i >= 0; i--)
388 		msp3400c_write(client,I2C_MSP3400C_DEM, 0x0005,
389 			       msp_init_data[type].fir2[i]);
390 
391 	msp3400c_write(client,I2C_MSP3400C_DEM, 0x0083,     /* MODE_REG */
392 		       msp_init_data[type].mode_reg);
393 
394 	msp3400c_setcarrier(client, msp_init_data[type].cdo1,
395 			    msp_init_data[type].cdo2);
396 
397 	msp3400c_write(client,I2C_MSP3400C_DEM, 0x0056, 0); /*LOAD_REG_1/2*/
398 
399 	if (client->dolby) {
400 		msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
401 			       0x0520); /* I2S1 */
402 		msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
403 			       0x0620); /* I2S2 */
404 		msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
405 			       msp_init_data[type].dfp_src);
406 	} else {
407 		msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,
408 			       msp_init_data[type].dfp_src);
409 		msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,
410 			       msp_init_data[type].dfp_src);
411 		msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,
412 			       msp_init_data[type].dfp_src);
413 	}
414 	msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,
415 		       msp_init_data[type].dfp_src);
416 	msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e,
417 		       msp_init_data[type].dfp_matrix);
418 
419 	if (msp->nicam) {
420 		/* nicam prescale */
421 		msp3400c_write(client,I2C_MSP3400C_DFP, 0x0010, 0x5a00); /* was: 0x3000 */
422 	}
423 }
424 
425 /* turn on/off nicam + stereo */
426 static void msp3400c_setstereo(bktr_ptr_t client, int mode)
427 {
428 	static char *strmode[] = { "0", "mono", "stereo", "3",
429 				   "lang1", "5", "6", "7", "lang2" };
430 	struct msp3400c *msp = client->msp3400c_info;
431 	int nicam=0; /* channel source: FM/AM or nicam */
432 	int src=0;
433 
434 	/* switch demodulator */
435 	switch (msp->mode) {
436 	case MSP_MODE_FM_TERRA:
437 		dprintk(("msp3400: FM setstereo: %s\n",strmode[mode]));
438 		msp3400c_setcarrier(client,msp->second,msp->main);
439 		switch (mode) {
440 		case VIDEO_SOUND_STEREO:
441 			msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3001);
442 			break;
443 		case VIDEO_SOUND_MONO:
444 		case VIDEO_SOUND_LANG1:
445 		case VIDEO_SOUND_LANG2:
446 			msp3400c_write(client,I2C_MSP3400C_DFP, 0x000e, 0x3000);
447 			break;
448 		}
449 		break;
450 	case MSP_MODE_FM_SAT:
451 		dprintk(("msp3400: SAT setstereo: %s\n",strmode[mode]));
452 		switch (mode) {
453 		case VIDEO_SOUND_MONO:
454 			msp3400c_setcarrier(client, MSP_CARRIER(6.5), MSP_CARRIER(6.5));
455 			break;
456 		case VIDEO_SOUND_STEREO:
457 			msp3400c_setcarrier(client, MSP_CARRIER(7.2), MSP_CARRIER(7.02));
458 			break;
459 		case VIDEO_SOUND_LANG1:
460 			msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
461 			break;
462 		case VIDEO_SOUND_LANG2:
463 			msp3400c_setcarrier(client, MSP_CARRIER(7.38), MSP_CARRIER(7.02));
464 			break;
465 		}
466 		break;
467 	case MSP_MODE_FM_NICAM1:
468 	case MSP_MODE_FM_NICAM2:
469 	case MSP_MODE_AM_NICAM:
470 		dprintk(("msp3400: NICAM setstereo: %s\n",strmode[mode]));
471 		msp3400c_setcarrier(client,msp->second,msp->main);
472 		if (msp->nicam_on)
473 			nicam=0x0100;
474 		break;
475 	case MSP_MODE_BTSC:
476 		dprintk(("msp3400: BTSC setstereo: %s\n",strmode[mode]));
477 		nicam=0x0300;
478 		break;
479 	case MSP_MODE_EXTERN:
480 		dprintk(("msp3400: extern setstereo: %s\n",strmode[mode]));
481 		nicam = 0x0200;
482 		break;
483 	case MSP_MODE_FM_RADIO:
484 		dprintk(("msp3400: FM-Radio setstereo: %s\n",strmode[mode]));
485 		break;
486 	default:
487 		dprintk(("msp3400: mono setstereo\n"));
488 		return;
489 	}
490 
491 	/* switch audio */
492 	switch (mode) {
493 	case VIDEO_SOUND_STEREO:
494 		src = 0x0020 | nicam;
495 #if 0
496 		/* spatial effect */
497 		msp3400c_write(client,I2C_MSP3400C_DFP, 0x0005,0x4000);
498 #endif
499 		break;
500 	case VIDEO_SOUND_MONO:
501 		if (msp->mode == MSP_MODE_AM_NICAM) {
502 			dprintk(("msp3400: switching to AM mono\n"));
503 			/* AM mono decoding is handled by tuner, not MSP chip */
504 			/* SCART switching control register */
505 			msp3400c_set_scart(client,SCART_MONO,0);
506 			src = 0x0200;
507 			break;
508 		}
509 	case VIDEO_SOUND_LANG1:
510 		src = 0x0000 | nicam;
511 		break;
512 	case VIDEO_SOUND_LANG2:
513 		src = 0x0010 | nicam;
514 		break;
515 	}
516 	dprintk(("msp3400: setstereo final source/matrix = 0x%x\n", src));
517 
518 	if (client->dolby) {
519 		msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,0x0520);
520 		msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,0x0620);
521 		msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
522 		msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
523 	} else {
524 		msp3400c_write(client,I2C_MSP3400C_DFP, 0x0008,src);
525 		msp3400c_write(client,I2C_MSP3400C_DFP, 0x0009,src);
526 		msp3400c_write(client,I2C_MSP3400C_DFP, 0x000a,src);
527 		msp3400c_write(client,I2C_MSP3400C_DFP, 0x000b,src);
528 	}
529 }
530 
531 static void
532 msp3400c_print_mode(struct msp3400c *msp)
533 {
534 	if (msp->main == msp->second) {
535 		kprintf("bktr: msp3400: mono sound carrier: %d.%03d MHz\n",
536 		       msp->main/910000,(msp->main/910)%1000);
537 	} else {
538 		kprintf("bktr: msp3400: main sound carrier: %d.%03d MHz\n",
539 		       msp->main/910000,(msp->main/910)%1000);
540 	}
541 	if (msp->mode == MSP_MODE_FM_NICAM1 ||
542 	    msp->mode == MSP_MODE_FM_NICAM2)
543 		kprintf("bktr: msp3400: NICAM/FM carrier   : %d.%03d MHz\n",
544 		       msp->second/910000,(msp->second/910)%1000);
545 	if (msp->mode == MSP_MODE_AM_NICAM)
546 		kprintf("bktr: msp3400: NICAM/AM carrier   : %d.%03d MHz\n",
547 		       msp->second/910000,(msp->second/910)%1000);
548 	if (msp->mode == MSP_MODE_FM_TERRA &&
549 	    msp->main != msp->second) {
550 		kprintf("bktr: msp3400: FM-stereo carrier : %d.%03d MHz\n",
551 		       msp->second/910000,(msp->second/910)%1000);
552 	}
553 }
554 
555 static void
556 msp3400c_restore_dfp(bktr_ptr_t client)
557 {
558 	struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
559 	int i;
560 
561 	for (i = 0; i < DFP_COUNT; i++) {
562 		if (-1 == msp->dfp_regs[i])
563 			continue;
564 		msp3400c_write(client,I2C_MSP3400C_DFP, i, msp->dfp_regs[i]);
565 	}
566 }
567 
568 /* ----------------------------------------------------------------------- */
569 
570 struct REGISTER_DUMP {
571 	int   addr;
572 	char *name;
573 };
574 
575 struct REGISTER_DUMP d1[] = {
576 	{ 0x007e, "autodetect" },
577 	{ 0x0023, "C_AD_BITS " },
578 	{ 0x0038, "ADD_BITS  " },
579 	{ 0x003e, "CIB_BITS  " },
580 	{ 0x0057, "ERROR_RATE" },
581 };
582 
583 static int
584 autodetect_stereo(bktr_ptr_t client)
585 {
586 	struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
587 	int val;
588 	int newstereo = msp->stereo;
589 	int newnicam  = msp->nicam_on;
590 	int update = 0;
591 
592 	switch (msp->mode) {
593 	case MSP_MODE_FM_TERRA:
594 		val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x18);
595 		if (val > 32767)
596 			val -= 65536;
597 		dprintk(("msp34xx: stereo detect register: %d\n",val));
598 
599 		if (val > 4096) {
600 			newstereo = VIDEO_SOUND_STEREO | VIDEO_SOUND_MONO;
601 		} else if (val < -4096) {
602 			newstereo = VIDEO_SOUND_LANG1 | VIDEO_SOUND_LANG2;
603 		} else {
604 			newstereo = VIDEO_SOUND_MONO;
605 		}
606 		newnicam = 0;
607 		break;
608 	case MSP_MODE_FM_NICAM1:
609 	case MSP_MODE_FM_NICAM2:
610 	case MSP_MODE_AM_NICAM:
611 		val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x23);
612 		dprintk(("msp34xx: nicam sync=%d, mode=%d\n",
613 			 val & 1, (val & 0x1e) >> 1));
614 
615 		if (val & 1) {
616 			/* nicam synced */
617 			switch ((val & 0x1e) >> 1)  {
618 			case 0:
619 			case 8:
620 				newstereo = VIDEO_SOUND_STEREO;
621 				break;
622 			case 1:
623 			case 9:
624 				newstereo = VIDEO_SOUND_MONO
625 					| VIDEO_SOUND_LANG1;
626 				break;
627 			case 2:
628 			case 10:
629 				newstereo = VIDEO_SOUND_MONO
630 					| VIDEO_SOUND_LANG1
631 					| VIDEO_SOUND_LANG2;
632 				break;
633 			default:
634 				newstereo = VIDEO_SOUND_MONO;
635 				break;
636 			}
637 			newnicam=1;
638 		} else {
639 			newnicam = 0;
640 			newstereo = VIDEO_SOUND_MONO;
641 		}
642 		break;
643 	case MSP_MODE_BTSC:
644 		val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x200);
645 		dprintk(("msp3410: status=0x%x (pri=%s, sec=%s, %s%s%s)\n",
646 			 val,
647 			 (val & 0x0002) ? "no"     : "yes",
648 			 (val & 0x0004) ? "no"     : "yes",
649 			 (val & 0x0040) ? "stereo" : "mono",
650 			 (val & 0x0080) ? ", nicam 2nd mono" : "",
651 			 (val & 0x0100) ? ", bilingual/SAP"  : ""));
652 		newstereo = VIDEO_SOUND_MONO;
653 		if (val & 0x0040) newstereo |= VIDEO_SOUND_STEREO;
654 		if (val & 0x0100) newstereo |= VIDEO_SOUND_LANG1;
655 		break;
656 	}
657 	if (newstereo != msp->stereo) {
658 		update = 1;
659 		dprintk(("msp34xx: watch: stereo %d => %d\n",
660 			 msp->stereo,newstereo));
661 		msp->stereo   = newstereo;
662 	}
663 	if (newnicam != msp->nicam_on) {
664 		update = 1;
665 		dprintk(("msp34xx: watch: nicam %d => %d\n",
666 			 msp->nicam_on,newnicam));
667 		msp->nicam_on = newnicam;
668 	}
669 	return update;
670 }
671 
672 /*
673  * A kernel thread for msp3400 control -- we don't want to block the
674  * in the ioctl while doing the sound carrier & stereo detect
675  */
676 
677 /* stereo/multilang monitoring */
678 static void watch_stereo(bktr_ptr_t client)
679 {
680 	struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
681 
682 	if (autodetect_stereo(client)) {
683 		if (msp->stereo & VIDEO_SOUND_STEREO)
684 			msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
685 		else if (msp->stereo & VIDEO_SOUND_LANG1)
686 			msp3400c_setstereo(client,VIDEO_SOUND_LANG1);
687 		else
688 			msp3400c_setstereo(client,VIDEO_SOUND_MONO);
689 	}
690 	if (client->stereo_once)
691 		msp->watch_stereo = 0;
692 
693 }
694 
695 static void msp3400c_thread(void *data)
696 {
697 	bktr_ptr_t client = data;
698 	struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
699 
700 	struct CARRIER_DETECT *cd;
701 	int count, max1,max2,val1,val2, val,this;
702 
703 	dprintk(("msp3400: thread started\n"));
704 
705 	for (;;) {
706 		if (msp->rmmod)
707 			goto done;
708 		tsleep(msp->kthread, 0, "idle", 0);
709 		if (msp->rmmod)
710 			goto done;
711 		if (msp->halt_thread) {
712 			msp->watch_stereo = 0;
713 			msp->halt_thread = 0;
714 			continue;
715 		}
716 
717 		if (VIDEO_MODE_RADIO == msp->norm ||
718 		    MSP_MODE_EXTERN  == msp->mode)
719 			continue;  /* nothing to do */
720 
721 		msp->active = 1;
722 
723 		if (msp->watch_stereo) {
724 			watch_stereo(client);
725 			msp->active = 0;
726 			continue;
727 		}
728 
729 		/* some time for the tuner to sync */
730 		tsleep(msp->kthread, 0, "tuner sync", hz/2);
731 
732 	restart:
733 		if (VIDEO_MODE_RADIO == msp->norm ||
734 		    MSP_MODE_EXTERN  == msp->mode)
735 			continue;  /* nothing to do */
736 		msp->restart = 0;
737 		msp3400c_setvolume(client, msp->muted, 0, 0);
738 		msp3400c_setmode(client, MSP_MODE_AM_DETECT /* +1 */ );
739 		val1 = val2 = 0;
740 		max1 = max2 = -1;
741 		msp->watch_stereo = 0;
742 
743 		/* carrier detect pass #1 -- main carrier */
744 		cd = carrier_detect_main; count = CARRIER_COUNT(carrier_detect_main);
745 
746 		if (client->amsound && (msp->norm == VIDEO_MODE_SECAM)) {
747 			/* autodetect doesn't work well with AM ... */
748 			max1 = 3;
749 			count = 0;
750 			dprintk(("msp3400: AM sound override\n"));
751 		}
752 
753 		for (this = 0; this < count; this++) {
754 			msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
755 
756 			tsleep(msp->kthread, 0, "carrier detect", hz/100);
757 
758 			if (msp->restart)
759 				msp->restart = 0;
760 
761 			val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
762 			if (val > 32767)
763 				val -= 65536;
764 			if (val1 < val)
765 				val1 = val, max1 = this;
766 			dprintk(("msp3400: carrier1 val: %5d / %s\n", val,
767 				 cd[this].name));
768 		}
769 
770 		/* carrier detect pass #2 -- second (stereo) carrier */
771 		switch (max1) {
772 		case 1: /* 5.5 */
773 			cd = carrier_detect_55; count = CARRIER_COUNT(carrier_detect_55);
774 			break;
775 		case 3: /* 6.5 */
776 			cd = carrier_detect_65; count = CARRIER_COUNT(carrier_detect_65);
777 			break;
778 		case 0: /* 4.5 */
779 		case 2: /* 6.0 */
780 		default:
781 			cd = NULL; count = 0;
782 			break;
783 		}
784 
785 		if (client->amsound && (msp->norm == VIDEO_MODE_SECAM)) {
786 			/* autodetect doesn't work well with AM ... */
787 			cd = NULL; count = 0; max2 = 0;
788 		}
789 		for (this = 0; this < count; this++) {
790 			msp3400c_setcarrier(client, cd[this].cdo,cd[this].cdo);
791 
792 			tsleep(msp->kthread, 0, "carrier detection", hz/100);
793 			if (msp->restart)
794 				goto restart;
795 
796 			val = msp3400c_read(client, I2C_MSP3400C_DFP, 0x1b);
797 			if (val > 32767)
798 				val -= 65536;
799 			if (val2 < val)
800 				val2 = val, max2 = this;
801 			dprintk(("msp3400: carrier2 val: %5d / %s\n", val,
802 				 cd[this].name));
803 		}
804 
805 		/* programm the msp3400 according to the results */
806 		msp->main   = carrier_detect_main[max1].cdo;
807 		switch (max1) {
808 		case 1: /* 5.5 */
809 			if (max2 == 0) {
810 				/* B/G FM-stereo */
811 				msp->second = carrier_detect_55[max2].cdo;
812 				msp3400c_setmode(client, MSP_MODE_FM_TERRA);
813 				msp->nicam_on = 0;
814 				/* XXX why mono? this probably can do stereo... - Alex*/
815 				msp3400c_setstereo(client, VIDEO_SOUND_MONO);
816 				msp->watch_stereo = 1;
817 			} else if (max2 == 1 && msp->nicam) {
818 				/* B/G NICAM */
819 				msp->second = carrier_detect_55[max2].cdo;
820 				msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
821 				msp->nicam_on = 1;
822 				msp3400c_setcarrier(client, msp->second, msp->main);
823 				msp->watch_stereo = 1;
824 			} else {
825 				goto no_second;
826 			}
827 			break;
828 		case 2: /* 6.0 */
829 			/* PAL I NICAM */
830 			msp->second = MSP_CARRIER(6.552);
831 			msp3400c_setmode(client, MSP_MODE_FM_NICAM2);
832 			msp->nicam_on = 1;
833 			msp3400c_setcarrier(client, msp->second, msp->main);
834 			msp->watch_stereo = 1;
835 			break;
836 		case 3: /* 6.5 */
837 			if (max2 == 1 || max2 == 2) {
838 				/* D/K FM-stereo */
839 				msp->second = carrier_detect_65[max2].cdo;
840 				msp3400c_setmode(client, MSP_MODE_FM_TERRA);
841 				msp->nicam_on = 0;
842 				msp3400c_setstereo(client, VIDEO_SOUND_MONO);
843 				msp->watch_stereo = 1;
844 			} else if (max2 == 0 &&
845 				   msp->norm == VIDEO_MODE_SECAM) {
846 				/* L NICAM or AM-mono */
847 				msp->second = carrier_detect_65[max2].cdo;
848 				msp3400c_setmode(client, MSP_MODE_AM_NICAM);
849 				msp->nicam_on = 0;
850 				msp3400c_setstereo(client, VIDEO_SOUND_MONO);
851 				msp3400c_setcarrier(client, msp->second, msp->main);
852 				/* volume prescale for SCART (AM mono input) */
853 				msp3400c_write(client,I2C_MSP3400C_DFP, 0x000d, 0x1900);
854 				msp->watch_stereo = 1;
855 			} else if (max2 == 0 && msp->nicam) {
856 				/* D/K NICAM */
857 				msp->second = carrier_detect_65[max2].cdo;
858 				msp3400c_setmode(client, MSP_MODE_FM_NICAM1);
859 				msp->nicam_on = 1;
860 				msp3400c_setcarrier(client, msp->second, msp->main);
861 				msp->watch_stereo = 1;
862 			} else {
863 				goto no_second;
864 			}
865 			break;
866 		case 0: /* 4.5 */
867 		default:
868 		no_second:
869 			msp->second = carrier_detect_main[max1].cdo;
870 			msp3400c_setmode(client, MSP_MODE_FM_TERRA);
871 			msp->nicam_on = 0;
872 			msp3400c_setcarrier(client, msp->second, msp->main);
873 			msp->stereo = VIDEO_SOUND_MONO;
874 			msp3400c_setstereo(client, VIDEO_SOUND_MONO);
875 			break;
876 		}
877 
878 		if (msp->watch_stereo)
879 			watch_stereo(client);
880 
881 		/* unmute + restore dfp registers */
882 		msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
883 		msp3400c_restore_dfp(client);
884 
885 		if (bootverbose)
886 			msp3400c_print_mode(msp);
887 
888 		msp->active = 0;
889 	}
890 
891 done:
892 	dprintk(("msp3400: thread: exit\n"));
893 	msp->active = 0;
894 
895 	msp->kthread = NULL;
896 	wakeup(&msp->kthread);
897 
898 	kthread_exit();
899 }
900 
901 /* ----------------------------------------------------------------------- */
902 /* this one uses the automatic sound standard detection of newer           */
903 /* msp34xx chip versions                                                   */
904 
905 static struct MODES {
906 	int retval;
907 	int main, second;
908 	char *name;
909 } modelist[] = {
910 	{ 0x0000, 0, 0, "ERROR" },
911 	{ 0x0001, 0, 0, "autodetect start" },
912 	{ 0x0002, MSP_CARRIER(4.5), MSP_CARRIER(4.72), "4.5/4.72  M Dual FM-Stereo" },
913 	{ 0x0003, MSP_CARRIER(5.5), MSP_CARRIER(5.7421875), "5.5/5.74  B/G Dual FM-Stereo" },
914 	{ 0x0004, MSP_CARRIER(6.5), MSP_CARRIER(6.2578125), "6.5/6.25  D/K1 Dual FM-Stereo" },
915 	{ 0x0005, MSP_CARRIER(6.5), MSP_CARRIER(6.7421875), "6.5/6.74  D/K2 Dual FM-Stereo" },
916 	{ 0x0006, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5  D/K FM-Mono (HDEV3)" },
917 	{ 0x0008, MSP_CARRIER(5.5), MSP_CARRIER(5.85), "5.5/5.85  B/G NICAM FM" },
918 	{ 0x0009, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85  L NICAM AM" },
919 	{ 0x000a, MSP_CARRIER(6.0), MSP_CARRIER(6.55), "6.0/6.55  I NICAM FM" },
920 	{ 0x000b, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85  D/K NICAM FM" },
921 	{ 0x000c, MSP_CARRIER(6.5), MSP_CARRIER(5.85), "6.5/5.85  D/K NICAM FM (HDEV2)" },
922 	{ 0x0020, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5  M BTSC-Stereo" },
923 	{ 0x0021, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5  M BTSC-Mono + SAP" },
924 	{ 0x0030, MSP_CARRIER(4.5), MSP_CARRIER(4.5), "4.5  M EIA-J Japan Stereo" },
925 	{ 0x0040, MSP_CARRIER(10.7), MSP_CARRIER(10.7), "10.7  FM-Stereo Radio" },
926 	{ 0x0050, MSP_CARRIER(6.5), MSP_CARRIER(6.5), "6.5  SAT-Mono" },
927 	{ 0x0051, MSP_CARRIER(7.02), MSP_CARRIER(7.20), "7.02/7.20  SAT-Stereo" },
928 	{ 0x0060, MSP_CARRIER(7.2), MSP_CARRIER(7.2), "7.2  SAT ADR" },
929 	{     -1, 0, 0, NULL }, /* EOF */
930 };
931 
932 static void msp3410d_thread(void *data)
933 {
934 	bktr_ptr_t client = data;
935 	struct msp3400c *msp = (struct msp3400c*)client->msp3400c_info;
936 	int mode,val,i,std;
937 	int timo = 0;
938 
939 	dprintk(("msp3410: thread started\n"));
940 
941 	for (;;) {
942 		if (msp->rmmod)
943 			goto done;
944 		if (!msp->watch_stereo)
945 			timo = 0;
946 		else
947 			timo = 10*hz;
948 		tsleep(msp->kthread, 0, "idle", timo);
949 		if (msp->rmmod)
950 			goto done;
951 		if (msp->halt_thread) {
952 			msp->watch_stereo = 0;
953 			msp->halt_thread = 0;
954 			dprintk(("msp3410: thread halted\n"));
955 			continue;
956 		}
957 
958 		if (msp->mode == MSP_MODE_EXTERN)
959 			continue;
960 
961 		msp->active = 1;
962 
963 		if (msp->watch_stereo) {
964 			watch_stereo(client);
965 			msp->active = 0;
966 			continue;
967 		}
968 
969 		/* some time for the tuner to sync */
970 		tsleep(msp->kthread, 0, "tuner sync", hz/2);
971 
972 	restart:
973 		if (msp->mode == MSP_MODE_EXTERN)
974 			continue;
975 		msp->restart = 0;
976 		msp->watch_stereo = 0;
977 
978 		/* put into sane state (and mute) */
979 		msp3400c_reset(client);
980 
981 		/* start autodetect */
982 		switch (msp->norm) {
983 		case VIDEO_MODE_PAL:
984 			mode = 0x1003;
985 			std  = 1;
986 			break;
987 		case VIDEO_MODE_NTSC:  /* BTSC */
988 			mode = 0x2003;
989 			std  = 0x0020;
990 			break;
991 		case VIDEO_MODE_SECAM:
992 			mode = 0x0003;
993 			std  = 1;
994 			break;
995 		case VIDEO_MODE_RADIO:
996 			mode = 0x0003;
997 			std  = 0x0040;
998 			break;
999 		default:
1000 			mode = 0x0003;
1001 			std  = 1;
1002 			break;
1003 		}
1004 		msp3400c_write(client, I2C_MSP3400C_DEM, 0x30, mode);
1005 		msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, std);
1006 
1007 		if (bootverbose) {
1008 			int i;
1009 			for (i = 0; modelist[i].name != NULL; i++)
1010 				if (modelist[i].retval == std)
1011 					break;
1012 			dprintk(("msp3410: setting mode: %s (0x%04x)\n",
1013 			        modelist[i].name ? modelist[i].name : "unknown",
1014 				std));
1015 		}
1016 
1017 		if (std != 1) {
1018 			/* programmed some specific mode */
1019 			val = std;
1020 		} else {
1021 			/* triggered autodetect */
1022 			for (;;) {
1023 				tsleep(msp->kthread, 0, "autodetection", hz/10);
1024 				if (msp->restart)
1025 					goto restart;
1026 
1027 				/* check results */
1028 				val = msp3400c_read(client, I2C_MSP3400C_DEM, 0x7e);
1029 				if (val < 0x07ff)
1030 					break;
1031 				dprintk(("msp3410: detection still in progress\n"));
1032 			}
1033 		}
1034 		for (i = 0; modelist[i].name != NULL; i++)
1035 			if (modelist[i].retval == val)
1036 				break;
1037 		dprintk(("msp3410: current mode: %s (0x%04x)\n",
1038 			 modelist[i].name ? modelist[i].name : "unknown",
1039 			 val));
1040 		msp->main   = modelist[i].main;
1041 		msp->second = modelist[i].second;
1042 
1043 		if (client->amsound && (msp->norm == VIDEO_MODE_SECAM) && (val != 0x0009)) {
1044 			/* autodetection has failed, let backup */
1045 			dprintk(("msp3410: autodetection failed, switching to backup mode: %s (0x%04x)\n",
1046 				modelist[8].name ? modelist[8].name : "unknown",val));
1047 			val = 0x0009;
1048 			msp3400c_write(client, I2C_MSP3400C_DEM, 0x20, val);
1049 		}
1050 
1051 		/* set various prescales */
1052 		msp3400c_write(client, I2C_MSP3400C_DFP, 0x0d, 0x1900); /* scart */
1053 		msp3400c_write(client, I2C_MSP3400C_DFP, 0x0e, 0x2403); /* FM */
1054 		msp3400c_write(client, I2C_MSP3400C_DFP, 0x10, 0x5a00); /* nicam */
1055 
1056 		/* set stereo */
1057 		switch (val) {
1058 		case 0x0008: /* B/G NICAM */
1059 		case 0x000a: /* I NICAM */
1060 			if (val == 0x0008)
1061 				msp->mode = MSP_MODE_FM_NICAM1;
1062 			else
1063 				msp->mode = MSP_MODE_FM_NICAM2;
1064 			/* just turn on stereo */
1065 			msp->stereo = VIDEO_SOUND_STEREO;
1066 			msp->nicam_on = 1;
1067 			msp->watch_stereo = 1;
1068 			msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
1069 			break;
1070 		case 0x0009:
1071 			msp->mode = MSP_MODE_AM_NICAM;
1072 			msp->stereo = VIDEO_SOUND_MONO;
1073 			msp->nicam_on = 1;
1074 			msp3400c_setstereo(client,VIDEO_SOUND_MONO);
1075 			msp->watch_stereo = 1;
1076 			break;
1077 		case 0x0020: /* BTSC */
1078 			/* just turn on stereo */
1079 			msp->mode   = MSP_MODE_BTSC;
1080 			msp->stereo = VIDEO_SOUND_STEREO;
1081 			msp->nicam_on = 0;
1082 			msp->watch_stereo = 1;
1083 			msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
1084 			break;
1085 		case 0x0040: /* FM radio */
1086 			msp->mode   = MSP_MODE_FM_RADIO;
1087 			msp->stereo = VIDEO_SOUND_STEREO;
1088 			msp->nicam_on = 0;
1089 			msp->watch_stereo = 0;
1090 			/* scart routing */
1091 			msp3400c_set_scart(client,SCART_IN2,0);
1092 			msp3400c_write(client,I2C_MSP3400C_DFP, 0x08, 0x0220);
1093 			msp3400c_write(client,I2C_MSP3400C_DFP, 0x09, 0x0220);
1094 			msp3400c_write(client,I2C_MSP3400C_DFP, 0x0b, 0x0220);
1095 			break;
1096 		case 0x0003:
1097 			msp->mode   = MSP_MODE_FM_TERRA;
1098 			msp->stereo = VIDEO_SOUND_STEREO;
1099 			msp->nicam_on = 0;
1100 			msp->watch_stereo = 1;
1101 			msp3400c_setstereo(client,VIDEO_SOUND_STEREO);
1102 			break;
1103 		}
1104 
1105 		if (msp->watch_stereo)
1106 			watch_stereo(client);
1107 
1108 		/* unmute + restore dfp registers */
1109 		msp3400c_setbass(client, msp->bass);
1110 		msp3400c_settreble(client, msp->treble);
1111 		msp3400c_setvolume(client, msp->muted, msp->left, msp->right);
1112 		msp3400c_restore_dfp(client);
1113 
1114 		msp->active = 0;
1115 	}
1116 
1117 done:
1118 	dprintk(("msp3410: thread: exit\n"));
1119 	msp->active = 0;
1120 
1121 	msp->kthread = NULL;
1122 	wakeup(&msp->kthread);
1123 
1124 	kthread_exit();
1125 }
1126 
1127 int msp_attach(bktr_ptr_t bktr)
1128 {
1129 	struct msp3400c *msp;
1130 	int              rev1,rev2,i;
1131 	int		 err;
1132 	char		 buf[20];
1133 
1134 	msp = (struct msp3400c *) kmalloc(sizeof(struct msp3400c), M_DEVBUF, M_NOWAIT);
1135 	if (msp == NULL)
1136                 return ENOMEM;
1137 	bktr->msp3400c_info = msp;
1138 
1139 	memset(msp,0,sizeof(struct msp3400c));
1140 	msp->left   = 65535;
1141 	msp->right  = 65535;
1142 	msp->bass   = 32768;
1143 	msp->treble = 32768;
1144 	msp->input  = -1;
1145 	msp->threaddesc = kmalloc(15 * sizeof(char), M_DEVBUF, M_NOWAIT);
1146 	if (msp->threaddesc == NULL) {
1147 		kfree(msp, M_DEVBUF);
1148                 return ENOMEM;
1149 	}
1150 	ksnprintf(msp->threaddesc, 14, "%s_msp34xx_thread", bktr->bktr_xname);
1151 
1152 	for (i = 0; i < DFP_COUNT; i++)
1153 		msp->dfp_regs[i] = -1;
1154 
1155 	msp3400c_reset(bktr);
1156 
1157 	rev1 = msp3400c_read(bktr, I2C_MSP3400C_DFP, 0x1e);
1158 	if (-1 != rev1)
1159 		rev2 = msp3400c_read(bktr, I2C_MSP3400C_DFP, 0x1f);
1160 	if ((-1 == rev1) || (0 == rev1 && 0 == rev2)) {
1161 		kfree(msp->threaddesc, M_DEVBUF);
1162 		kfree(msp, M_DEVBUF);
1163 		bktr->msp3400c_info = NULL;
1164 		kprintf("%s: msp3400: error while reading chip version\n", bktr_name(bktr));
1165 		return ENXIO;
1166 	}
1167 
1168 #if 0
1169 	/* this will turn on a 1kHz beep - might be useful for debugging... */
1170 	msp3400c_write(bktr,I2C_MSP3400C_DFP, 0x0014, 0x1040);
1171 #endif
1172 
1173 	ksprintf(buf,"MSP34%02d%c-%c%d",
1174 		(rev2>>8)&0xff, (rev1&0xff)+'@', ((rev1>>8)&0xff)+'@', rev2&0x1f);
1175 	msp->nicam = (((rev2>>8)&0xff) != 00) ? 1 : 0;
1176 
1177 	if (bktr->mspsimple == -1) {
1178 		/* default mode */
1179 		/* msp->simple = (((rev2>>8)&0xff) == 0) ? 0 : 1; */
1180 		msp->simple = ((rev1&0xff)+'@' > 'C');
1181 	} else {
1182 		/* use kenv value */
1183 		msp->simple = bktr->mspsimple;
1184 	}
1185 
1186 	/* hello world :-) */
1187 	if (bootverbose) {
1188 		kprintf("%s: msp34xx: init: chip=%s", bktr_name(bktr), buf);
1189 		if (msp->nicam)
1190 			kprintf(", has NICAM support");
1191 		kprintf("\n");
1192 	}
1193 
1194 	/* startup control thread */
1195 	err = kthread_create(msp->simple ? msp3410d_thread : msp3400c_thread,
1196 			     bktr, &msp->kthread, msp->threaddesc);
1197 	if (err) {
1198 		kprintf("%s: Error returned by kthread_create: %d", bktr_name(bktr), err);
1199 		kfree(msp->threaddesc, M_DEVBUF);
1200 		kfree(msp, M_DEVBUF);
1201 		bktr->msp3400c_info = NULL;
1202 		return ENXIO;
1203 	}
1204 	wakeup(msp->kthread);
1205 
1206 	/* done */
1207 	return 0;
1208 }
1209 
1210 int msp_detach(bktr_ptr_t client)
1211 {
1212 	struct msp3400c *msp  = (struct msp3400c*)client->msp3400c_info;
1213 
1214 	/* shutdown control thread */
1215 	if (msp->kthread)
1216 	{
1217 		/* XXX mutex lock required */
1218 		msp->rmmod = 1;
1219 		msp->watch_stereo = 0;
1220 		wakeup(msp->kthread);
1221 
1222 		while (msp->kthread)
1223 			tsleep(&msp->kthread, 0, "wait for kthread", hz/10);
1224 	}
1225 
1226 	if (client->msp3400c_info != NULL) {
1227 		kfree(client->msp3400c_info, M_DEVBUF);
1228 		client->msp3400c_info = NULL;
1229 	}
1230 
1231     	msp3400c_reset(client);
1232 
1233 	return 0;
1234 }
1235 
1236 void msp_wake_thread(bktr_ptr_t client)
1237 {
1238 	struct msp3400c *msp  = (struct msp3400c*)client->msp3400c_info;
1239 
1240 	msp3400c_setvolume(client,msp->muted,0,0);
1241 	msp->watch_stereo=0;
1242 	if (msp->active)
1243 		msp->restart = 1;
1244 	wakeup(msp->kthread);
1245 }
1246 
1247 void msp_halt_thread(bktr_ptr_t client)
1248 {
1249 	struct msp3400c *msp  = (struct msp3400c*)client->msp3400c_info;
1250 
1251 	msp3400c_setvolume(client,msp->muted,0,0);
1252 	if (msp->active)
1253 		msp->restart = 1;
1254 	msp->halt_thread = 1;
1255 	wakeup(msp->kthread);
1256 }
1257 #endif /* BKTR_NEW_MSP34XX_DRIVER */
1258