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