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