hw_spdif.c 4.02 KB
#include "c_def.h"
#include "debug.h"
#include "oem.h"

#include "hw_dma.h"
#include "hw_da_pp.h"
#include "hw_spdif.h"
#include "hw_gpio.h"

#include "app_gpio.h"


//U8 spdif_sr_double; /*0, normal, 2, x2; 4, x4*/ 

//#define SPDIF_OUT_ENABLE
void spdif_init(int channel)
{
	volatile U32 tmp;
	SPDIF_CONFIG_AHB *spdif_config;

	tmp = SPDIF_CONFIG;
	spdif_config = (SPDIF_CONFIG_AHB *) &tmp;

#ifdef SPDIF_OUT_ENABLE
	spdif_config->spdif_enc_enable = 1;
#else
	spdif_config->spdif_enc_enable = 0;
#endif

	spdif_config->spdif_dec_enable = 1;
	spdif_config->spdif_data_sel = 0;
	spdif_config->spdif_enc_inv = 0;
	spdif_config->spdif_dec_inv = 0;
	//spdif_config->spdif_dec_sr_detect_level = 1;
	spdif_config->spdif_dec_sr_detect_start = 0;
	//spdif_config->spdif_dec_aux_mask = 0;
	spdif_config->sync_err_auto_recover = 0;

	//if (MI2S_CHANs == 3) 
	//	spdif_config->spdif_enc_stream = 2;
	//else    
	spdif_config->spdif_enc_stream = 0;

	spdif_config->spdif_enc_sample_delay = 6;

	SPDIF_CONFIG = tmp;	
	
	//spdif_sr_double = 0;
	
	SPDIF_ENC_STATUS_L = 0x0110004;
	SPDIF_ENC_STATUS_H = 0x1fb;
}


void spdif_open(int channel)
{
	//U32 *pDmaSrc;
	//pDmaSrc = (U32 *)DA_SPDIF_IN_FIFO_ADDR;

	//DMA_Channel2_Init((U32 *)awInSampleStore[0], pDmaSrc, CD_DA_SECTOR_SIZE/4, SOURCE_DMA_SPDIF_IN);

	if (channel == SPDIF_IN_CH_0)
	{
		spdif_din_select(SPDIF_IN_CH_0);
	}
	else if (channel == SPDIF_IN_CH_1)
	{
		spdif_din_select(SPDIF_IN_CH_1);
	}
	else if (channel == SPDIF_IN_CH_2)
	{
		spdif_din_select(SPDIF_IN_CH_2);
	}
	else if (channel == SPDIF_IN_CH_3)
	{
		spdif_din_select(SPDIF_IN_CH_3);
	}
}

void spdif_close(int channel)
{
}

void spdif_fini(void)
{
	volatile U32 tmp;
	SPDIF_CONFIG_AHB *spdif_config;
	
	tmp = SPDIF_CONFIG;
	spdif_config = (SPDIF_CONFIG_AHB *) &tmp;
	spdif_config->spdif_enc_enable = 0;
	spdif_config->spdif_dec_enable = 0;
	spdif_config->spdif_dec_sr_detect_start = 0;

	SPDIF_CONFIG = tmp;	
}

void spdif_sr_detect_enable(void)
{
	volatile U32 tmp;
	SPDIF_CONFIG_AHB *spdif_config;
	
	tmp = SPDIF_CONFIG;
	spdif_config = (SPDIF_CONFIG_AHB *) &tmp;
	spdif_config->spdif_dec_enable = 1;
	spdif_config->spdif_dec_sr_detect_start = 1;
	
	SPDIF_CONFIG = tmp;	
}

void spdif_dec_enable(void)
{
	volatile U32 tmp;
	SPDIF_CONFIG_AHB *spdif_config;
	tmp = SPDIF_CONFIG;
	spdif_config = (SPDIF_CONFIG_AHB *) &tmp;
	spdif_config->spdif_dec_enable = 1;
	SPDIF_CONFIG = tmp;	
}

void spdif_dec_disable(void)
{
	volatile U32 tmp;
	SPDIF_CONFIG_AHB *spdif_config;
	tmp = SPDIF_CONFIG;
	spdif_config = (SPDIF_CONFIG_AHB *) &tmp;
	spdif_config->spdif_dec_enable = 0;
	spdif_config->spdif_dec_sr_detect_start = 0;
	SPDIF_CONFIG = tmp;	
}


void spdif_sr_detect_disable(void)
{
	volatile U32 tmp;
	SPDIF_CONFIG_AHB *spdif_config;
	
	tmp = SPDIF_CONFIG;
	spdif_config = (SPDIF_CONFIG_AHB *) &tmp;
	spdif_config->spdif_dec_sr_detect_start = 0;
	
	SPDIF_CONFIG = tmp;		
}

void spdif_din_select(U16 sel)
{
	volatile U32 tmp;
	SPDIF_CONFIG_AHB *spdif_config;
	
	tmp = SPDIF_CONFIG;
	spdif_config = (SPDIF_CONFIG_AHB *) &tmp;
	spdif_config->spdif_data_sel = (sel & 0x3);
	
	SPDIF_CONFIG = tmp;		
}

void spdif_detect_level(u8 high)
{
#if 0	
	volatile U32 tmp;
	SPDIF_CONFIG_AHB *spdif_config;

	tmp = SPDIF_CONFIG;
	spdif_config = (SPDIF_CONFIG_AHB *) &tmp;

	if (high == 0) {
		//detect low
		spdif_config->spdif_dec_sr_detect_level = 0;
	}
	else {
		spdif_config->spdif_dec_sr_detect_level = 1;
	}
	
	SPDIF_CONFIG = tmp;	
#endif	
}

void spdif_output_set(U8 con)
{
#ifndef AUDIO_PWM_OUTPUT 
	volatile U32 temp;
	//SPDIF_CONFIGs *spdif_config;
	SPDIF_CONFIG_AHB *spdif_config;

	//spdif_config = (SPDIF_CONFIGs *)&temp;
	spdif_config = (SPDIF_CONFIG_AHB *)&temp;

	//temp = 0;
	temp = SPDIF_CONFIG;

	spdif_config->spdif_enc_enable = TRUE;
	SPDIF_CONFIG = temp;

	//SPDIF_ENC_STATUS_L = 0x0110004;
	//SPDIF_ENC_STATUS_H = 0x1fb;

	if (con)
	{
		//gpio0_SetFunction(SPDIF_OUT_CH_1_PIN , FALSE);
		app_gpio_MUXfunction_select(SPDIF_OUT_CH_1_PIN, MUX_SEL_FUNCTION2);
	}
	else
	{
		//gpio0_SetFunction(SPDIF_OUT_CH_1_PIN , TRUE);		
		app_gpio_MUXfunction_select(SPDIF_OUT_CH_1_PIN, MUX_SEL_GPIO_INPUT);
	}
#endif
}