/* Copyright 2021 Sony Corporation */

//---------------------------------------------------------------------------
// Include files
//---------------------------------------------------------------------------

#include "aud_cust_amp_if.h"
#include "sif_if.h"
#include "aud_if.h"
#include "drvcust_if.h"
#include "sakura_aud_aq_data.h"
#include "aud_cust_amp_RT9119.h"

//---------------------------------------------------------------------------
// Configurations
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Constant definitions
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Type definitions
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Macro definitions
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Imported variables
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Imported functions
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Static function forward declarations
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Static variables
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Static functions
//---------------------------------------------------------------------------
static void _AMP_RT9119_le32toDataBuf(UINT32 le32,
                                     unsigned char* dst,
                                     const unsigned int size) {
    int i;

    if(size > 4) {  // range over
        return;     // argument error
    }
    for( i = size ; i ; i-- ) {
        dst[i - 1] = (unsigned char)(le32 & 0xFF);
        le32 = le32 >> 8;
    }
}

static void _AMP_RT9119_RegConvertWrite( AMP_DATA_T *pAmp,
                                         UINT8 u1RegAddr,
                                         UINT32 u4RegValue,
                                         const unsigned int size )
{
    UINT8 convertValue[8];
    _AMP_RT9119_le32toDataBuf(u4RegValue, convertValue, size);
    AUD_CUST_AMP_RegWrite(pAmp, u1RegAddr, convertValue, size);
    LOG(2, "%s RT9119(%d) 0x%02x 0x%08x\n", __func__, pAmp->u1AmpTable, u1RegAddr, u4RegValue);
}

UINT32 _AMP_RT9119_Calc_SYS_CTL(AMP_DATA_T *pAmp, BOOL isShutDown)
{
    UINT32 u4Return;
    AQDataDa15782General *gen;
    AQDataDa15782General gen1 = {0};
    AQDataDa25782General gen2 = {0};
    AQDataDa35782General gen3 = {0};
    AQDataDa45782General gen4 = {0};
    AQDataDaGeneralConfig genConf = {0};

    UINT32 u4TargetAmp;
    UINT32 u4ShutDown = 0x0;

    if(isShutDown) {
        u4ShutDown = AMP_RT9119_REG_SYS_CTL_SHUTDOWN;
    }

    DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_DA_GEN_CFG, &genConf, 0, _gu4CurrentAqKey, 0);

    switch (pAmp->u1AmpTable) {
    case DA_RT9119_TYPE_1:
        DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_DA1_5782_GENERAL, &gen1, 0, _gu4CurrentAqKey, 0);
        gen = (AQDataDa15782General *)(&gen1);
        u4TargetAmp = DA_RT9119_TYPE_1_BIT_ASSIGN;
        break;

    case DA_RT9119_TYPE_2:
        DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_DA2_5782_GENERAL, &gen2, 0, _gu4CurrentAqKey, 0);
        gen = (AQDataDa15782General *)(&gen2);
        u4TargetAmp = DA_RT9119_TYPE_2_BIT_ASSIGN;
        break;

    case DA_RT9119_TYPE_3:
        DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_DA3_5782_GENERAL, &gen3, 0, _gu4CurrentAqKey, 0);
        gen = (AQDataDa15782General *)(&gen3);
        u4TargetAmp = DA_RT9119_TYPE_3_BIT_ASSIGN;
        break;

    case DA_RT9119_TYPE_4:
        DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_DA4_5782_GENERAL, &gen4, 0, _gu4CurrentAqKey, 0);
        gen = (AQDataDa15782General *)(&gen4);
        u4TargetAmp = DA_RT9119_TYPE_4_BIT_ASSIGN;
        break;

    default:
        // Should not reach. Same setting DAMP#1
        DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_DA1_5782_GENERAL, &gen1, 0, _gu4CurrentAqKey, 0);
        gen = (AQDataDa15782General *)(&gen1);
        u4TargetAmp = DA_RT9119_TYPE_1_BIT_ASSIGN;
        break;
    }

    if((gen->m_Da1GeneralL2l == 0) && (gen->m_Da1GeneralL2r == 0)) {
        if(genConf.m_DaTypeIicC34 & u4TargetAmp) {
            u4Return = AMP_RT9119_REG_SYS_CTL_PBTL_400_VAL | u4ShutDown;
        }
        else {
            u4Return = AMP_RT9119_REG_SYS_CTL_PBTL_800_VAL | u4ShutDown;
        }
    }
    else if((gen->m_Da1GeneralR2l == 0) && (gen->m_Da1GeneralR2r == 0)) {
        if(genConf.m_DaTypeIicC34 & u4TargetAmp) {
            u4Return = AMP_RT9119_REG_SYS_CTL_PBTL_400_VAL | u4ShutDown;
        }
        else {
            u4Return = AMP_RT9119_REG_SYS_CTL_PBTL_800_VAL | u4ShutDown;
        }
    }
    else if(genConf.m_DaTypeIicC34 & u4TargetAmp) {
        u4Return = AMP_RT9119_REG_SYS_CTL_BTL_400_VAL | u4ShutDown;
    }
    else {
        u4Return = AMP_RT9119_REG_SYS_CTL_BTL_800_VAL | u4ShutDown;
    }

    LOG(2, "%s RT9119(%x) L2L(0x%08x) L2R(0x%08x) R2L(0x%08x) R2R(0x%08x).\n",
                   __func__, pAmp->u1AmpTable,
                   gen->m_Da1GeneralL2l, gen->m_Da1GeneralL2r, gen->m_Da1GeneralR2l, gen->m_Da1GeneralR2r);
    LOG(2, "%s IIC_C_34(%d) ShutDown(0x%08x) ResultCalc(0x%08x).\n",
                   __func__, genConf.m_DaTypeIicC34, u4ShutDown, u4Return);

    return u4Return;
}

UINT32 _AMP_RT9119_Calc_HARD_CLIP(AMP_DATA_T *pAmp)
{
    UINT32 u4Return;
    AQDataDa15782General *gen;
    AQDataDa15782General gen1 = {0};
    AQDataDa25782General gen2 = {0};
    AQDataDa35782General gen3 = {0};
    AQDataDa45782General gen4 = {0};

    switch (pAmp->u1AmpTable) {
    case DA_RT9119_TYPE_1:
        DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_DA1_5782_GENERAL, &gen1, 0, _gu4CurrentAqKey, 0);
        gen = (AQDataDa15782General *)(&gen1);
        break;

    case DA_RT9119_TYPE_2:
        DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_DA2_5782_GENERAL, &gen2, 0, _gu4CurrentAqKey, 0);
        gen = (AQDataDa15782General *)(&gen2);
        break;

    case DA_RT9119_TYPE_3:
        DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_DA3_5782_GENERAL, &gen3, 0, _gu4CurrentAqKey, 0);
        gen = (AQDataDa15782General *)(&gen3);
        break;

    case DA_RT9119_TYPE_4:
        DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_DA4_5782_GENERAL, &gen4, 0, _gu4CurrentAqKey, 0);
        gen = (AQDataDa15782General *)(&gen4);
        break;

    default:
        // Should not reach. Same setting DAMP#1
        DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_DA1_5782_GENERAL, &gen1, 0, _gu4CurrentAqKey, 0);
        gen = (AQDataDa15782General *)(&gen1);
        break;
    }

    for(u4Return=0; u4Return<sizeof(dbToGainIndex)/4; u4Return++) {
        if(dbToGainIndex[u4Return] < gen->m_Da1GeneralThdBoost) {
            break;
        }
    }

    u4Return = AMP_RT9119_REG_HARD_CLIP_BASE_SETTING | (u4Return << 8);
    LOG(2, "%s RT9119(%d) ThdBoost(0x%08x) ResultCalc(0x%08x).\n",
                   __func__, pAmp->u1AmpTable, gen->m_Da1GeneralThdBoost, u4Return);
    return u4Return;
}

UINT32 _AMP_RT9119_Calc_PRE_SCALE(AMP_DATA_T *pAmp)
{
    UINT32 u4Return;
    AQDataMmSlt slt = {0};
    INT32 i4Level = 0;

    DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_SLT, &slt, 0, _gu4CurrentAqKey, 0);
    if(slt.m_MmSpOut < (-48 * 10)) {  // less than -48dB
        i4Level = -48 * 10;
    }
    else if((48 * 10) < slt.m_MmSpOut) {  // more than +48dB
        i4Level = 48 * 10;
    }
    else {
        i4Level = slt.m_MmSpOut;
    }

    // 480 = 0.0dB i4Level = -480(-48.0dB) to +480(+48.0dB)
    u4Return = db0p1ToLenear20000forDampCtrl[480+i4Level];

    LOG(2, "%s AmpNo(%d) mmSpOut(0x%08x) ResultCalc(0x%08x).\n",
                   __func__, pAmp->u1AmpTable, slt.m_MmSpOut, u4Return);
    return u4Return;
}

void _AMP_RT9119_Calc_CHx_MIX_And_Write(AMP_DATA_T *pAmp)
{
    AQDataDa15782General *gen;
    AQDataDa15782General gen1 = {0};
    AQDataDa25782General gen2 = {0};
    AQDataDa35782General gen3 = {0};
    AQDataDa45782General gen4 = {0};
    UINT32 u4OutMixL2l;
    UINT32 u4OutMixR2l;
    UINT32 u4OutMixL2r;
    UINT32 u4OutMixR2r;
    AMP_RT9119_ChMix chL;
    AMP_RT9119_ChMix chR;

    switch (pAmp->u1AmpTable) {
    case DA_RT9119_TYPE_1:
        DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_DA1_5782_GENERAL, &gen1, 0, _gu4CurrentAqKey, 0);
        gen = (AQDataDa15782General *)(&gen1);
        break;

    case DA_RT9119_TYPE_2:
        DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_DA2_5782_GENERAL, &gen2, 0, _gu4CurrentAqKey, 0);
        gen = (AQDataDa15782General *)(&gen2);
        break;

    case DA_RT9119_TYPE_3:
        DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_DA3_5782_GENERAL, &gen3, 0, _gu4CurrentAqKey, 0);
        gen = (AQDataDa15782General *)(&gen3);
        break;

    case DA_RT9119_TYPE_4:
        DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_DA4_5782_GENERAL, &gen4, 0, _gu4CurrentAqKey, 0);
        gen = (AQDataDa15782General *)(&gen4);
        break;

    default:
        // Should not reach. Same setting DAMP#1
        DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_DA1_5782_GENERAL, &gen1, 0, _gu4CurrentAqKey, 0);
        gen = (AQDataDa15782General *)(&gen1);
        break;
    }

    _AMP_RT9119_le32toDataBuf(gen->m_Da1GeneralL2l, chL.mix_1, sizeof(chL.mix_1));
    _AMP_RT9119_le32toDataBuf(gen->m_Da1GeneralR2l, chL.mix_0, sizeof(chL.mix_0));
    AUD_CUST_AMP_RegWrite(pAmp, AMP_RT9119_REG_CH1_IN_MIX, (UINT8*)&chL, AMP_RT9119_REG_CHx_MIX_SIZE);

    _AMP_RT9119_le32toDataBuf(gen->m_Da1GeneralL2r, chR.mix_1, sizeof(chR.mix_1));
    _AMP_RT9119_le32toDataBuf(gen->m_Da1GeneralR2r, chR.mix_0, sizeof(chR.mix_0));
    AUD_CUST_AMP_RegWrite(pAmp, AMP_RT9119_REG_CH2_IN_MIX, (UINT8*)&chR, AMP_RT9119_REG_CHx_MIX_SIZE);

    u4OutMixL2l = 0x00800000;
    u4OutMixR2l = 0x00000000;
    _AMP_RT9119_le32toDataBuf(u4OutMixL2l, chL.mix_1, sizeof(chL.mix_1));
    _AMP_RT9119_le32toDataBuf(u4OutMixR2l, chL.mix_0, sizeof(chL.mix_0));
    AUD_CUST_AMP_RegWrite(pAmp, AMP_RT9119_REG_CH1_OUT_MIX, (UINT8*)&chL, AMP_RT9119_REG_CHx_MIX_SIZE);

    u4OutMixL2r = 0x00000000;
    u4OutMixR2r = 0x00800000;
    _AMP_RT9119_le32toDataBuf(u4OutMixL2r, chR.mix_1, sizeof(chR.mix_1));
    _AMP_RT9119_le32toDataBuf(u4OutMixR2r, chR.mix_0, sizeof(chR.mix_0));
    AUD_CUST_AMP_RegWrite(pAmp, AMP_RT9119_REG_CH2_OUT_MIX, (UINT8*)&chR, AMP_RT9119_REG_CHx_MIX_SIZE);

    LOG(2, "%s RT9119(%d) %02x CH1_IN_MIX_1=(0x%08x) CH1_IN_MIX_0(0x%08x).\n",
                   __func__, pAmp->u1AmpTable, AMP_RT9119_REG_CH1_IN_MIX,
                   gen->m_Da1GeneralL2l, gen->m_Da1GeneralR2l);
    LOG(2, "%s RT9119(%d) %02x CH2_IN_MIX_1=(0x%08x) CH2_IN_MIX_0=(0x%08x).\n",
                   __func__, pAmp->u1AmpTable, AMP_RT9119_REG_CH2_IN_MIX,
                   gen->m_Da1GeneralL2r, gen->m_Da1GeneralR2r);
    LOG(2, "%s RT9119(%d) %02x CH1_OUT_MIX_1=(0x%08x) CH1_OUT_MIX_0=(0x%08x).\n",
                   __func__, pAmp->u1AmpTable, AMP_RT9119_REG_CH1_OUT_MIX,
                   u4OutMixL2l, u4OutMixR2l);
    LOG(2, "%s RT9119(%d) %02x CH2_OUT_MIX_1=(0x%08x) CH2_OUT_MIX_0=(0x%08x).\n",
                   __func__, pAmp->u1AmpTable, AMP_RT9119_REG_CH2_OUT_MIX,
                   u4OutMixL2r, u4OutMixR2r);
    return;
}

UINT32 _AMP_RT9119_Calc_MS_VOL(AMP_DATA_T *pAmp)
{
    UINT32 u4Return;
    AQDataDa15782Vol *vol;
    AQDataDa15782Vol vol1 = {0};
    AQDataDa25782Vol vol2 = {0};
    AQDataDa35782Vol vol3 = {0};
    AQDataDa45782Vol vol4 = {0};

    switch (pAmp->u1AmpTable) {
    case DA_RT9119_TYPE_1:
        DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_DA1_5782_VOL, &vol1, 0, _gu4CurrentAqKey, 0);
        vol = (AQDataDa15782Vol *)(&vol1);
        break;

    case DA_RT9119_TYPE_2:
        DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_DA2_5782_VOL, &vol2, 0, _gu4CurrentAqKey, 0);
        vol = (AQDataDa15782Vol *)(&vol2);
        break;

    case DA_RT9119_TYPE_3:
        DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_DA3_5782_VOL, &vol3, 0, _gu4CurrentAqKey, 0);
        vol = (AQDataDa15782Vol *)(&vol3);
        break;

    case DA_RT9119_TYPE_4:
        DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_DA4_5782_VOL, &vol4, 0, _gu4CurrentAqKey, 0);
        vol = (AQDataDa15782Vol *)(&vol4);
        break;

    default:
        // Should not reach. Same setting DAMP#1
        DrvCust_AQ_SetGet(AUD_DrvCust_AQ_Get_DA1_5782_VOL, &vol1, 0, _gu4CurrentAqKey, 0);
        vol = (AQDataDa15782Vol *)(&vol1);
        break;
    }

    for(u4Return=0; u4Return<sizeof(dbToGainIndex)/4; u4Return++) {
        if(dbToGainIndex[u4Return] < vol->m_Da1ChVolume) {
            break;
        }
    }

    LOG(2, "%s RT9119(%d) ChVolume(0x%08x) ResultCalc(0x%08x).\n",
                   __func__, pAmp->u1AmpTable, vol->m_Da1ChVolume, u4Return);
    return u4Return;
}

void AUD_CUST_AMP_RT9119_Init(AMP_DATA_T *pAmp)
{
    UINT32  u4RegValue;

    // Set Master Volume to MUTE
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_MS_VOL, 0x7FF, AMP_RT9119_REG_MS_VOL_SIZE);

    // Set SYS_CTL Reg
    u4RegValue = _AMP_RT9119_Calc_SYS_CTL( pAmp, TRUE );
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_SYS_CTL, u4RegValue, AMP_RT9119_REG_SYS_CTL_SIZE);

    // Enable Hi pass Filter, Hard Mute after recovery,
    // Compensation Filter Disabled, Enable Post HPF, Disable FIR
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_FLTR_MISC, 0xC2, AMP_RT9119_REG_FLTR_MISC_SIZE);

    // Set DC Protection register to enabled:
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_DC_PROTECT, 0x21, AMP_RT9119_REG_DC_PROTECT_SIZE);

    // Set DRC_CTRL1
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_DRC_CTRL1, 0xF0, AMP_RT9119_REG_DRC_CTRL1_SIZE);

    // Set DRC_CTRL2
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_DRC_CTRL2, 0x0B, AMP_RT9119_REG_DRC_CTRL2_SIZE);

    // Set Gain_Pair control and disable final hard gain
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_FINAL_GAIN_OUT, 0x00, AMP_RT9119_REG_FINAL_GAIN_OUT_SIZE);

    // Set Audio Interface Mode to 32Bit I2S
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_I2S_FMT, 0x07, AMP_RT9119_REG_I2S_FMT_SIZE);

    // Set VOL_RAMP reg
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_VOL_RAMP, 0x00, AMP_RT9119_REG_VOL_RAMP_SIZE);

    // Set AUTO_RCVRY reg
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_AUTO_RCVRY, 0x02, AMP_RT9119_REG_AUTO_RCVRY_SIZE);

    // Set ERR_TYPE
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_ERROR_TYPE, 0x0E, AMP_RT9119_REG_ERROR_TYPE_SIZE);

    // Set ERR_MASK
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_ERROR_MASK, 0x71, AMP_RT9119_REG_ERROR_MASK_SIZE);

    // Set S-Data Input Setting to Normal (CH1 -> LEFT, CH2 -> RIGHT)
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_SDIO_SEL, 0x01, AMP_RT9119_REG_SDIO_SEL_SIZE);

    // Set register below to set CH1 & CH2 volume level to 0dB
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_CH1_VOL, 0x180, AMP_RT9119_REG_CH1_VOL_SIZE);
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_CH2_VOL, 0x180, AMP_RT9119_REG_CH2_VOL_SIZE);

    // Set POST_IDF gain setting & Enable DF_CLIP
    u4RegValue = _AMP_RT9119_Calc_HARD_CLIP( pAmp );
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_HARD_CLIP, u4RegValue, AMP_RT9119_REG_HARD_CLIP_SIZE);

    // Set PWM_SS_OPT
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_PWM_SS_OPT, 0x01, AMP_RT9119_REG_PWM_SS_OPT_SIZE);

    // Set register below to set "Stereo/Monaural" setting to gStereoh for main speaker
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_INTER_PWR_CTRL, 0xFF, AMP_RT9119_REG_INTER_PWR_CTRL_SIZE);

    // Set Class D gain setting (x8)
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_SPK_GAIN, 0x07, AMP_RT9119_REG_SPK_GAIN_SIZE);

    // Disable DE-POP function
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_DEPOP_GLITCH, 0x01, AMP_RT9119_REG_DEPOP_GLITCH_SIZE);

    // Set SST 40ms
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_SERVO_TIME_OPT, 0x31, AMP_RT9119_REG_SERVO_TIME_OPT_SIZE);

    // Clear error flags
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_ERR_RPT, 0x00, AMP_RT9119_REG_ERR_RPT_SIZE);

    // Set up DSP function  EQ_CTRL
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_EQ_CTRL, 0x18, AMP_RT9119_REG_EQ_CTRL_SIZE);

    // Set up DSP function  EQ_BOOST
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_L_BQ_BOOST, 0x00000000, AMP_RT9119_REG_L_BQ_BOOST_SIZE);
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_R_BQ_BOOST, 0x00000000, AMP_RT9119_REG_R_BQ_BOOST_SIZE);

    // Set up DSP function  SMOOTH_BQ_ALPHA
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_SBQ_ALPHA, 0x00800000, AMP_RT9119_REG_SBQ_ALPHA_SIZE);

    // Set up DSP function  SMOOTH_BQ_CTRL
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_SBQ_CTRL, 0x0000FF0C, AMP_RT9119_REG_SBQ_CTRL_SIZE);

    // Set up DSP function  PRE_SCALE
    u4RegValue = _AMP_RT9119_Calc_PRE_SCALE( pAmp );
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_PRE_SCALE, u4RegValue, AMP_RT9119_REG_PRE_SCALE_SIZE);

    // Set up DSP function  POST_SCALE
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_POST_SCALE, 0x00800000, AMP_RT9119_REG_POST_SCALE_SIZE);

    // Adjust PWR_SEL
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_PWR_SEL, 0x01, AMP_RT9119_REG_PWR_SEL_SIZE);

    // Adjust ANA_MODULTR2
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_ANA_MODULTR2, 0x0C, AMP_RT9119_REG_ANA_MODULTR2_SIZE);

    // Shutdown = Low
    u4RegValue = _AMP_RT9119_Calc_SYS_CTL( pAmp, FALSE );
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_SYS_CTL, u4RegValue, AMP_RT9119_REG_SYS_CTL_SIZE);

    // Set register below for Input and Output Mixer
    _AMP_RT9119_Calc_CHx_MIX_And_Write( pAmp );

    // Master Volume
    u4RegValue = _AMP_RT9119_Calc_MS_VOL( pAmp );
    _AMP_RT9119_RegConvertWrite( pAmp, AMP_RT9119_REG_MS_VOL, u4RegValue, AMP_RT9119_REG_MS_VOL_SIZE);
}

static struct aud_cust_amp_func_tbl _rRT9119Funcs =
{
    AUD_CUST_AMP_RT9119_Init,     //pfnCustInit;
    NULL,                                         //pfnCustSuspend;
    NULL,                                         //pfnCustResume;
    NULL,                                         //pfnCustMute;
    NULL,                                         //pfnCustRefresh
};

void AUD_CUST_AMP_RegistAmp_RT9119(AMP_DATA_T *pAmp)
{
    strncpy(pAmp->name, "RT9119", sizeof(pAmp->name) - 1);

    pAmp->funcTbl = &_rRT9119Funcs;
}

// EOF
