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