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