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