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