1/*-
2 * SPDX-License-Identifier: BSD-4-Clause
3 *
4 * 1. Redistributions of source code must retain the
5 * Copyright (c) 1997 Amancio Hasty, 1999 Roger Hardiman
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *      This product includes software developed by Amancio Hasty and
19 *      Roger Hardiman
20 * 4. The name of the author may not be used to endorse or promote products
21 *    derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36#include <sys/cdefs.h>
37__FBSDID("$FreeBSD$");
38
39/*
40 * This is part of the Driver for Video Capture Cards (Frame grabbers)
41 * and TV Tuner cards using the Brooktree Bt848, Bt848A, Bt849A, Bt878, Bt879
42 * chipset.
43 * Copyright Roger Hardiman and Amancio Hasty.
44 *
45 * bktr_audio : This deals with controlling the audio on TV cards,
46 *                controlling the Audio Multiplexer (audio source selector).
47 *                controlling any MSP34xx stereo audio decoders.
48 *                controlling any DPL35xx dolby surroud sound audio decoders.
49 *                initialising TDA98xx audio devices.
50 *
51 */
52
53#include "opt_bktr.h"               /* Include any kernel config options */
54
55#include <sys/param.h>
56#include <sys/systm.h>
57#include <sys/kernel.h>
58
59#ifdef __FreeBSD__
60
61#if (__FreeBSD_version < 500000)
62#include <machine/clock.h>              /* for DELAY */
63#include <pci/pcivar.h>
64#else
65#include <sys/lock.h>
66#include <sys/mutex.h>
67#include <sys/selinfo.h>
68#include <dev/pci/pcivar.h>
69#endif
70
71#include <machine/bus.h>
72#include <sys/bus.h>
73#endif
74
75#ifdef __NetBSD__
76#include <sys/proc.h>
77#include <dev/ic/bt8xx.h>	/* NetBSD location of .h files */
78#include <dev/pci/bktr/bktr_reg.h>
79#include <dev/pci/bktr/bktr_core.h>
80#include <dev/pci/bktr/bktr_tuner.h>
81#include <dev/pci/bktr/bktr_card.h>
82#include <dev/pci/bktr/bktr_audio.h>
83#else
84#include <dev/bktr/ioctl_meteor.h>
85#include <dev/bktr/ioctl_bt848.h>	/* extensions to ioctl_meteor.h */
86#include <dev/bktr/bktr_reg.h>
87#include <dev/bktr/bktr_core.h>
88#include <dev/bktr/bktr_tuner.h>
89#include <dev/bktr/bktr_card.h>
90#include <dev/bktr/bktr_audio.h>
91#endif
92
93/*
94 * Prototypes for the GV_BCTV2 specific functions.
95 */
96void    set_bctv2_audio( bktr_ptr_t bktr );
97void    bctv2_gpio_write( bktr_ptr_t bktr, int port, int val );
98/*int   bctv2_gpio_read( bktr_ptr_t bktr, int port );*/ /* Not used */
99
100/*
101 * init_audio_devices
102 * Reset any MSP34xx or TDA98xx audio devices.
103 */
104void init_audio_devices( bktr_ptr_t bktr ) {
105
106        /* enable stereo if appropriate on TDA audio chip */
107        if ( bktr->card.dbx )
108                init_BTSC( bktr );
109
110        /* reset the MSP34xx stereo audio chip */
111        if ( bktr->card.msp3400c )
112                msp_dpl_reset( bktr, bktr->msp_addr );
113
114        /* reset the DPL35xx dolby audio chip */
115        if ( bktr->card.dpl3518a )
116                msp_dpl_reset( bktr, bktr->dpl_addr );
117
118}
119
120
121/*
122 *
123 */
124#define AUDIOMUX_DISCOVER_NOT
125int
126set_audio( bktr_ptr_t bktr, int cmd )
127{
128	u_long		temp;
129	volatile u_char	idx;
130
131#if defined( AUDIOMUX_DISCOVER )
132	if ( cmd >= 200 )
133		cmd -= 200;
134	else
135#endif /* AUDIOMUX_DISCOVER */
136
137	/* check for existence of audio MUXes */
138	if ( !bktr->card.audiomuxs[ 4 ] )
139		return( -1 );
140
141	switch (cmd) {
142	case AUDIO_TUNER:
143#ifdef BKTR_REVERSEMUTE
144		bktr->audio_mux_select = 3;
145#else
146		bktr->audio_mux_select = 0;
147#endif
148
149		if (bktr->reverse_mute )
150		      bktr->audio_mux_select = 0;
151		else
152		    bktr->audio_mux_select = 3;
153
154		break;
155	case AUDIO_EXTERN:
156		bktr->audio_mux_select = 1;
157		break;
158	case AUDIO_INTERN:
159		bktr->audio_mux_select = 2;
160		break;
161	case AUDIO_MUTE:
162		bktr->audio_mute_state = TRUE;	/* set mute */
163		break;
164	case AUDIO_UNMUTE:
165		bktr->audio_mute_state = FALSE;	/* clear mute */
166		break;
167	default:
168		printf("%s: audio cmd error %02x\n", bktr_name(bktr),
169		       cmd);
170		return( -1 );
171	}
172
173
174	/* Most cards have a simple audio multiplexer to select the
175	 * audio source. The I/O_GV card has a more advanced multiplexer
176	 * and requires special handling.
177	 */
178        if ( bktr->bt848_card == CARD_IO_BCTV2 ) {
179                set_bctv2_audio( bktr );
180                return( 0 );
181	}
182
183	/* Proceed with the simpler audio multiplexer code for the majority
184	 * of Bt848 cards.
185	 */
186
187	/*
188	 * Leave the upper bits of the GPIO port alone in case they control
189	 * something like the dbx or teletext chips.  This doesn't guarantee
190	 * success, but follows the rule of least astonishment.
191	 */
192
193	if ( bktr->audio_mute_state == TRUE ) {
194#ifdef BKTR_REVERSEMUTE
195		idx = 0;
196#else
197		idx = 3;
198#endif
199
200		if (bktr->reverse_mute )
201		  idx  = 3;
202		else
203		  idx  = 0;
204
205	}
206	else
207		idx = bktr->audio_mux_select;
208
209
210	temp = INL(bktr, BKTR_GPIO_DATA) & ~bktr->card.gpio_mux_bits;
211#if defined( AUDIOMUX_DISCOVER )
212	OUTL(bktr, BKTR_GPIO_DATA, temp | (cmd & 0xff));
213	printf("%s: cmd: %d audio mux %x temp %x \n", bktr_name(bktr),
214	  	cmd, bktr->card.audiomuxs[ idx ], temp );
215#else
216	OUTL(bktr, BKTR_GPIO_DATA, temp | bktr->card.audiomuxs[ idx ]);
217#endif /* AUDIOMUX_DISCOVER */
218
219
220
221	/* Some new Hauppauge cards do not have an audio mux */
222	/* Instead we use the MSP34xx chip to select TV audio, Line-In */
223	/* FM Radio and Mute */
224	/* Examples of this are the Hauppauge 44xxx MSP34xx models */
225	/* It is ok to drive both the mux and the MSP34xx chip. */
226	/* If there is no mux, the MSP does the switching of the audio source */
227	/* If there is a mux, it does the switching of the audio source */
228
229	if ((bktr->card.msp3400c) && (bktr->audio_mux_present == 0)) {
230
231	  if (bktr->audio_mute_state == TRUE ) {
232		 msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0000, 0x0000); /* volume to MUTE */
233	  } else {
234		 if(bktr->audio_mux_select == 0) { /* TV Tuner */
235		    msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0000, 0x7300); /* 0 db volume */
236		    if (bktr->msp_source_selected != 0) msp_autodetect(bktr);  /* setup TV audio mode */
237		    bktr->msp_source_selected = 0;
238		 }
239		 if(bktr->audio_mux_select == 1) { /* Line In */
240		    msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0000, 0x7300); /* 0 db volume */
241		    msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x000d, 0x1900); /* scart prescale */
242		    msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0008, 0x0220); /* SCART | STEREO */
243		    msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0013, 0x0000); /* DSP In = SC1_IN_L/R */
244		    bktr->msp_source_selected = 1;
245		 }
246
247		 if(bktr->audio_mux_select == 2) { /* FM Radio */
248		    msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0000, 0x7300); /* 0 db volume */
249		    msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x000d, 0x1900); /* scart prescale */
250		    msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0008, 0x0220); /* SCART | STEREO */
251		    msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0013, 0x0200); /* DSP In = SC2_IN_L/R */
252		    bktr->msp_source_selected = 2;
253		 }
254	  }
255	}
256
257
258	return( 0 );
259}
260
261
262/*
263 *
264 */
265void
266temp_mute( bktr_ptr_t bktr, int flag )
267{
268	static int	muteState = FALSE;
269
270	if ( flag == TRUE ) {
271		muteState = bktr->audio_mute_state;
272		set_audio( bktr, AUDIO_MUTE );		/* prevent 'click' */
273	}
274	else {
275		tsleep( BKTR_SLEEP, PZERO, "tuning", hz/8 );
276		if ( muteState == FALSE )
277			set_audio( bktr, AUDIO_UNMUTE );
278	}
279}
280
281/* address of BTSC/SAP decoder chip */
282#define TDA9850_WADDR           0xb6
283#define TDA9850_RADDR           0xb7
284
285
286/* registers in the TDA9850 BTSC/dbx chip */
287#define CON1ADDR                0x04
288#define CON2ADDR                0x05
289#define CON3ADDR                0x06
290#define CON4ADDR                0x07
291#define ALI1ADDR                0x08
292#define ALI2ADDR                0x09
293#define ALI3ADDR                0x0a
294
295/*
296 * initialise the dbx chip
297 * taken from the Linux bttv driver TDA9850 initialisation code
298 */
299void
300init_BTSC( bktr_ptr_t bktr )
301{
302    i2cWrite(bktr, TDA9850_WADDR, CON1ADDR, 0x08); /* noise threshold st */
303    i2cWrite(bktr, TDA9850_WADDR, CON2ADDR, 0x08); /* noise threshold sap */
304    i2cWrite(bktr, TDA9850_WADDR, CON3ADDR, 0x40); /* stereo mode */
305    i2cWrite(bktr, TDA9850_WADDR, CON4ADDR, 0x07); /* 0 dB input gain? */
306    i2cWrite(bktr, TDA9850_WADDR, ALI1ADDR, 0x10); /* wideband alignment? */
307    i2cWrite(bktr, TDA9850_WADDR, ALI2ADDR, 0x10); /* spectral alignment? */
308    i2cWrite(bktr, TDA9850_WADDR, ALI3ADDR, 0x03);
309}
310
311/*
312 * setup the dbx chip
313 * XXX FIXME: a lot of work to be done here, this merely unmutes it.
314 */
315int
316set_BTSC( bktr_ptr_t bktr, int control )
317{
318	return( i2cWrite( bktr, TDA9850_WADDR, CON3ADDR, control ) );
319}
320
321/*
322 * CARD_GV_BCTV2 specific functions.
323 */
324
325#define BCTV2_AUDIO_MAIN              0x10    /* main audio program */
326#define BCTV2_AUDIO_SUB               0x20    /* sub audio program */
327#define BCTV2_AUDIO_BOTH              0x30    /* main(L) + sub(R) program */
328
329#define BCTV2_GPIO_REG0          1
330#define BCTV2_GPIO_REG1          3
331
332#define BCTV2_GR0_AUDIO_MODE     3
333#define BCTV2_GR0_AUDIO_MAIN     0       /* main program */
334#define BCTV2_GR0_AUDIO_SUB      3       /* sub program */
335#define BCTV2_GR0_AUDIO_BOTH     1       /* main(L) + sub(R) */
336#define BCTV2_GR0_AUDIO_MUTE     4       /* audio mute */
337#define BCTV2_GR0_AUDIO_MONO     8       /* force mono */
338
339void
340set_bctv2_audio( bktr_ptr_t bktr )
341{
342        int data;
343
344        switch (bktr->audio_mux_select) {
345        case 1:         /* external */
346        case 2:         /* internal */
347                bctv2_gpio_write(bktr, BCTV2_GPIO_REG1, 0);
348                break;
349        default:        /* tuner */
350                bctv2_gpio_write(bktr, BCTV2_GPIO_REG1, 1);
351                break;
352        }
353/*      switch (bktr->audio_sap_select) { */
354        switch (BCTV2_AUDIO_BOTH) {
355        case BCTV2_AUDIO_SUB:
356                data = BCTV2_GR0_AUDIO_SUB;
357                break;
358        case BCTV2_AUDIO_BOTH:
359                data = BCTV2_GR0_AUDIO_BOTH;
360                break;
361        case BCTV2_AUDIO_MAIN:
362        default:
363                data = BCTV2_GR0_AUDIO_MAIN;
364                break;
365        }
366        if (bktr->audio_mute_state == TRUE)
367                data |= BCTV2_GR0_AUDIO_MUTE;
368
369        bctv2_gpio_write(bktr, BCTV2_GPIO_REG0, data);
370
371        return;
372}
373
374/* gpio_data bit assignment */
375#define BCTV2_GPIO_ADDR_MASK     0x000300
376#define BCTV2_GPIO_WE            0x000400
377#define BCTV2_GPIO_OE            0x000800
378#define BCTV2_GPIO_VAL_MASK      0x00f000
379
380#define BCTV2_GPIO_PORT_MASK     3
381#define BCTV2_GPIO_ADDR_SHIFT    8
382#define BCTV2_GPIO_VAL_SHIFT     12
383
384/* gpio_out_en value for read/write */
385#define BCTV2_GPIO_OUT_RMASK     0x000f00
386#define BCTV2_GPIO_OUT_WMASK     0x00ff00
387
388#define BCTV2_BITS       100
389
390void
391bctv2_gpio_write( bktr_ptr_t bktr, int port, int val )
392{
393        u_long data, outbits;
394
395        port &= BCTV2_GPIO_PORT_MASK;
396        switch (port) {
397        case 1:
398        case 3:
399                data = ((val << BCTV2_GPIO_VAL_SHIFT) & BCTV2_GPIO_VAL_MASK) |
400                       ((port << BCTV2_GPIO_ADDR_SHIFT) & BCTV2_GPIO_ADDR_MASK) |
401                       BCTV2_GPIO_WE | BCTV2_GPIO_OE;
402                outbits = BCTV2_GPIO_OUT_WMASK;
403                break;
404        default:
405                return;
406        }
407        OUTL(bktr, BKTR_GPIO_OUT_EN, 0);
408        OUTL(bktr, BKTR_GPIO_DATA, data);
409        OUTL(bktr, BKTR_GPIO_OUT_EN, outbits);
410        DELAY(BCTV2_BITS);
411        OUTL(bktr, BKTR_GPIO_DATA, data & ~BCTV2_GPIO_WE);
412        DELAY(BCTV2_BITS);
413        OUTL(bktr, BKTR_GPIO_DATA, data);
414        DELAY(BCTV2_BITS);
415        OUTL(bktr, BKTR_GPIO_DATA, ~0);
416        OUTL(bktr, BKTR_GPIO_OUT_EN, 0);
417}
418
419/* Not yet used
420int
421bctv2_gpio_read( bktr_ptr_t bktr, int port )
422{
423        u_long data, outbits, ret;
424
425        port &= BCTV2_GPIO_PORT_MASK;
426        switch (port) {
427        case 1:
428        case 3:
429                data = ((port << BCTV2_GPIO_ADDR_SHIFT) & BCTV2_GPIO_ADDR_MASK) |
430                       BCTV2_GPIO_WE | BCTV2_GPIO_OE;
431                outbits = BCTV2_GPIO_OUT_RMASK;
432                break;
433        default:
434                return( -1 );
435        }
436        OUTL(bktr, BKTR_GPIO_OUT_EN, 0);
437        OUTL(bktr, BKTR_GPIO_DATA, data);
438        OUTL(bktr, BKTR_GPIO_OUT_EN, outbits);
439        DELAY(BCTV2_BITS);
440        OUTL(bktr, BKTR_GPIO_DATA, data & ~BCTV2_GPIO_OE);
441        DELAY(BCTV2_BITS);
442        ret = INL(bktr, BKTR_GPIO_DATA);
443        DELAY(BCTV2_BITS);
444        OUTL(bktr, BKTR_GPIO_DATA, data);
445        DELAY(BCTV2_BITS);
446        OUTL(bktr, BKTR_GPIO_DATA, ~0);
447        OUTL(bktr, BKTR_GPIO_OUT_EN, 0);
448        return( (ret & BCTV2_GPIO_VAL_MASK) >> BCTV2_GPIO_VAL_SHIFT );
449}
450*/
451
452/*
453 * setup the MSP34xx Stereo Audio Chip
454 * This uses the Auto Configuration Option on MSP3410D and MSP3415D chips
455 * and DBX mode selection for MSP3430G chips.
456 * For MSP3400C support, the full programming sequence is required and is
457 * not yet supported.
458 */
459
460/* Read the MSP version string */
461void msp_read_id( bktr_ptr_t bktr ){
462    int rev1=0, rev2=0;
463    rev1 = msp_dpl_read(bktr, bktr->msp_addr, 0x12, 0x001e);
464    rev2 = msp_dpl_read(bktr, bktr->msp_addr, 0x12, 0x001f);
465
466    sprintf(bktr->msp_version_string, "34%02d%c-%c%d",
467      (rev2>>8)&0xff, (rev1&0xff)+'@', ((rev1>>8)&0xff)+'@', rev2&0x1f);
468
469}
470
471
472/* Configure the MSP chip to Auto-detect the audio format.
473 * For the MSP3430G, we use fast autodetect mode
474 * For the MSP3410/3415 there are two schemes for this
475 *  a) Fast autodetection - the chip is put into autodetect mode, and the function
476 *     returns immediately. This works in most cases and is the Default Mode.
477 *  b) Slow mode. The function sets the MSP3410/3415 chip, then waits for feedback from
478 *     the chip and re-programs it if needed.
479 */
480void msp_autodetect( bktr_ptr_t bktr ) {
481
482#ifdef BKTR_NEW_MSP34XX_DRIVER
483
484  /* Just wake up the (maybe) sleeping thread, it'll do everything for us */
485  msp_wake_thread(bktr);
486
487#else
488  int auto_detect, loops;
489  int stereo;
490
491  /* MSP3430G - countries with mono and DBX stereo */
492  if (strncmp("3430G", bktr->msp_version_string, 5) == 0){
493
494    msp_dpl_write(bktr, bktr->msp_addr, 0x10, 0x0030,0x2003);/* Enable Auto format detection */
495    msp_dpl_write(bktr, bktr->msp_addr, 0x10, 0x0020,0x0020);/* Standard Select Reg. = BTSC-Stereo*/
496    msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x000E,0x2403);/* darned if I know */
497    msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0008,0x0320);/* Source select = (St or A) */
498					                     /* & Ch. Matrix = St */
499    msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0000,0x7300);/* Set volume to 0db gain */
500  }
501
502
503  /* MSP3415D SPECIAL CASE Use the Tuner's Mono audio output for the MSP */
504  /* (for Hauppauge 44xxx card with Tuner Type 0x2a) */
505  else if (  ( (strncmp("3415D", bktr->msp_version_string, 5) == 0)
506               &&(bktr->msp_use_mono_source == 1)
507              )
508           || (bktr->slow_msp_audio == 2) ){
509    msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0000, 0x7300); /* 0 db volume */
510    msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x000d, 0x1900); /* scart prescale */
511    msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0008, 0x0220); /* SCART | STEREO */
512    msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0013, 0x0100); /* DSP In = MONO IN */
513  }
514
515
516  /* MSP3410/MSP3415 - countries with mono, stereo using 2 FM channels and NICAM */
517  /* FAST sound scheme */
518  else if (bktr->slow_msp_audio == 0) {
519    msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0000,0x7300);/* Set volume to 0db gain */
520    msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0008,0x0000);/* Spkr Source = default(FM/AM) */
521    msp_dpl_write(bktr, bktr->msp_addr, 0x10, 0x0020,0x0001);/* Enable Auto format detection */
522    msp_dpl_write(bktr, bktr->msp_addr, 0x10, 0x0021,0x0001);/* Auto selection of NICAM/MONO mode */
523  }
524
525
526  /* MSP3410/MSP3415 - European Countries where the fast MSP3410/3415 programming fails */
527  /* SLOW sound scheme */
528  else if ( bktr->slow_msp_audio == 1) {
529    msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0000,0x7300);/* Set volume to 0db gain */
530    msp_dpl_write(bktr, bktr->msp_addr, 0x10, 0x0020,0x0001);/* Enable Auto format detection */
531
532    /* wait for 0.5s max for terrestrial sound autodetection */
533    loops = 10;
534    do {
535      DELAY(100000);
536      auto_detect = msp_dpl_read(bktr, bktr->msp_addr, 0x10, 0x007e);
537      loops++;
538    } while (auto_detect > 0xff && loops < 50);
539    if (bootverbose)printf ("%s: Result of autodetect after %dms: %d\n",
540			    bktr_name(bktr), loops*10, auto_detect);
541
542    /* Now set the audio baseband processing */
543    switch (auto_detect) {
544    case 0:                    /* no TV sound standard detected */
545      break;
546    case 2:                    /* M Dual FM */
547      break;
548    case 3:                    /* B/G Dual FM; German stereo */
549      /* Read the stereo detection value from DSP reg 0x0018 */
550      DELAY(20000);
551      stereo = msp_dpl_read(bktr, bktr->msp_addr, 0x12, 0x0018);
552      if (bootverbose)printf ("%s: Stereo reg 0x18 a: %d\n",
553			      bktr_name(bktr), stereo);
554      DELAY(20000);
555      stereo = msp_dpl_read(bktr, bktr->msp_addr, 0x12, 0x0018);
556      if (bootverbose)printf ("%s: Stereo reg 0x18 b: %d\n",
557			      bktr_name(bktr), stereo);
558      DELAY(20000);
559      stereo = msp_dpl_read(bktr, bktr->msp_addr, 0x12, 0x0018);
560      if (bootverbose)printf ("%s: Stereo reg 0x18 c: %d\n",
561			      bktr_name(bktr), stereo);
562      if (stereo > 0x0100 && stereo < 0x8000) { /* Seems to be stereo */
563        msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0008,0x0020);/* Loudspeaker set stereo*/
564        /*
565          set spatial effect strength to 50% enlargement
566          set spatial effect mode b, stereo basewidth enlargement only
567        */
568        msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0005,0x3f28);
569      } else if (stereo > 0x8000) {    /* bilingual mode */
570        if (bootverbose) printf ("%s: Bilingual mode detected\n",
571				 bktr_name(bktr));
572        msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0008,0x0000);/* Loudspeaker */
573        msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0005,0x0000);/* all spatial effects off */
574       } else {                 /* must be mono */
575        msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0008,0x0030);/* Loudspeaker */
576        /*
577          set spatial effect strength to 50% enlargement
578          set spatial effect mode a, stereo basewidth enlargement
579          and pseudo stereo effect with automatic high-pass filter
580        */
581        msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0005,0x3f08);
582      }
583#if 0
584       /* The reset value for Channel matrix mode is FM/AM and SOUNDA/LEFT */
585       /* We would like STEREO instead val: 0x0020 */
586       msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0008,0x0020);/* Loudspeaker */
587       msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0009,0x0020);/* Headphone */
588       msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x000a,0x0020);/* SCART1 */
589       msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0041,0x0020);/* SCART2 */
590       msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x000b,0x0020);/* I2S */
591       msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x000c,0x0020);/* Quasi-Peak Detector Source */
592       msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x000e,0x0001);
593#endif
594      break;
595    case 8:                    /* B/G FM NICAM */
596       msp_dpl_write(bktr, bktr->msp_addr, 0x10, 0x0021,0x0001);/* Auto selection of NICAM/MONO mode */
597       break;
598     case 9:                    /* L_AM NICAM or D/K*/
599     case 10:                   /* i-FM NICAM */
600       break;
601     default:
602       if (bootverbose) printf ("%s: Unknown autodetection result value: %d\n",
603				bktr_name(bktr), auto_detect);
604     }
605
606  }
607
608
609  /* uncomment the following line to enable the MSP34xx 1Khz Tone Generator */
610  /* turn your speaker volume down low before trying this */
611  /* msp_dpl_write(bktr, bktr->msp_addr, 0x12, 0x0014, 0x7f40); */
612
613#endif /* BKTR_NEW_MSP34XX_DRIVER */
614}
615
616/* Read the DPL version string */
617void dpl_read_id( bktr_ptr_t bktr ){
618    int rev1=0, rev2=0;
619    rev1 = msp_dpl_read(bktr, bktr->dpl_addr, 0x12, 0x001e);
620    rev2 = msp_dpl_read(bktr, bktr->dpl_addr, 0x12, 0x001f);
621
622    sprintf(bktr->dpl_version_string, "34%02d%c-%c%d",
623      ((rev2>>8)&0xff)-1, (rev1&0xff)+'@', ((rev1>>8)&0xff)+'@', rev2&0x1f);
624}
625
626/* Configure the DPL chip to Auto-detect the audio format */
627void dpl_autodetect( bktr_ptr_t bktr ) {
628
629    /* The following are empiric values tried from the DPL35xx data sheet */
630    msp_dpl_write(bktr, bktr->dpl_addr, 0x12, 0x000c,0x0320);	/* quasi peak detector source dolby
631								lr 0x03xx; quasi peak detector matrix
632								stereo 0xXX20 */
633    msp_dpl_write(bktr, bktr->dpl_addr, 0x12, 0x0040,0x0060);	/* Surround decoder mode;
634								ADAPTIVE/3D-PANORAMA, that means two
635								speakers and no center speaker, all
636								channels L/R/C/S mixed to L and R */
637    msp_dpl_write(bktr, bktr->dpl_addr, 0x12, 0x0041,0x0620);	/* surround source matrix;I2S2/STEREO*/
638    msp_dpl_write(bktr, bktr->dpl_addr, 0x12, 0x0042,0x1F00);	/* surround delay 31ms max */
639    msp_dpl_write(bktr, bktr->dpl_addr, 0x12, 0x0043,0x0000);	/* automatic surround input balance */
640    msp_dpl_write(bktr, bktr->dpl_addr, 0x12, 0x0044,0x4000);	/* surround spatial effect 50%
641								recommended*/
642    msp_dpl_write(bktr, bktr->dpl_addr, 0x12, 0x0045,0x5400);	/* surround panorama effect 66%
643								recommended with PANORAMA mode
644								in 0x0040 set to panorama */
645}
646
647