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