/* SPDX-License-Identifier: LGPL-2.1-only OR BSD-3-Clause */
/******************************************************************************
 *
 * This file is provided under a dual license.  When you use or
 * distribute this software, you may choose to be licensed under
 * version 2.1 of the GNU Lesser General Public License
 * ("LGPLv2.1 License") or BSD License.
 *
 * LGPLv2.1 License
 *
 * Copyright(C) 2019 MediaTek Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html for more
 * details.
 *
 * BSD LICENSE
 *
 * Copyright(C) 2019 MediaTek Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *  * Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *****************************************************************************/

/*-----------------------------------------------------------------------------
 *
 * $Author: sunman.chang $
 * $Date: 2015/11/25 $
 * $RCSfile: aud_amp_5760.c,v $
 * $Revision: #2 $
 *
 *---------------------------------------------------------------------------*/

#define AUD_EXTERN_FILE

//=======================
// [**CAUTION**] 
// Please exclude Loader
//=======================
#if ((!defined(CC_MTK_PRELOADER)) && (!defined(CC_MTK_LOADER))) 

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

/* =======================================================
 [**CAUTION**] 
 You can only include header files in 
 \target\mtxxxx\xxxx_driver\drv_inc\  
 or files in \vm_linux\output\sysbuild\xxxx\rel or debug\inc\drv_inc\
 or files in the same directory.
========================================================= */
#include "aud_amp_5760.h"    
#include "sif_if.h"
#include "aud_if.h"
#include "drvcust_if.h"
#include "sakura_aud_aq_data.h"

//---------------------------------------------------------------------------
// Configurations
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Constant definitions
//---------------------------------------------------------------------------
#define SAMPLE_RATE_NUM         3
#define SOURCE_NUM              3
    #define SOURCE_FROM_AV      0
    #define SOURCE_FROM_ATV     1
    #define SOURCE_FROM_DTV     2
#define GEN_CONFIG_NUM          45
#define EQ_BIQUAD_NUM           4
#define HL_BIQUAD_NUM           2
#ifdef CC_AUD_SAKURA_RENDER_KDAMP
#define AMP_MAX_NUM             7
#define MUX_NUM                 8
#define VOL_NUM                 5
#define BQ_NUM                  7
#define BT_NUM                  4
#define NTFY_ERR_NUM_5760       16
#define NTFY_ERR_NUM_5538       352
#endif
//---------------------------------------------------------------------------
// Type definitions
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Macro definitions
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Imported variables
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Imported functions
//---------------------------------------------------------------------------
#ifdef CUSTOM_SELF_DIAG_ENABLE
extern void SelfDiagTriggerAudI2CError(void);
#endif

//---------------------------------------------------------------------------
// Static function forward declarations
//---------------------------------------------------------------------------
#ifdef CC_AUD_SAKURA_RENDER
static void _KD_AQ_AMP_RegWrite(UINT8 u1Add, UINT8* u1Data, UINT8 Index, UINT8 length);
static UINT8 _KD_AQ_AMP_RegRead(UINT8 u1Add, UINT8 Index, UINT8* readData);
#endif

static void _MTK_AQ_AMP_RegWrite1(UINT8 u1Addr, UINT8 u1Data);
static UINT8 _MTK_AQ_AMP_RegRead1(UINT8 u1Addr);
static void _MTK_AQ_AMP_RegWrite2(UINT8 u1Addr, UINT8 u1Data);
static UINT8 _MTK_AQ_AMP_RegRead2(UINT8 u1Addr);

#ifdef CC_AUD_SAKURA_RENDER_KDAMP
static void _KD_AQ_5538_Init(UINT8 Index);
static void _KD_AQ_5760_Init(UINT8 Index);
static void _KD_AQ_Set_5538_Gen_Config(UINT8 Index);
static void _KD_AQ_Set_5538_BQ(UINT8 Index);
static void _KD_AQ_Set_5538_BassTreble(UINT8 Index);
static void _KD_AQ_Set_5538_DRC(UINT8 Index);
static void _KD_AQ_Set_5538_LOUD(UINT8 Index);
#endif 

static void _MTK_AQ_AMP_Init1(void);
static void _MTK_AQ_AMP_Init2(void);

#ifdef CC_AUD_SAKURA_RENDER_KDAMP
static void _KD_AQ_DATA_CPY(UINT8* data, UINT32 ref, UINT8 size);
static void _KD_AQ_5538_Update_Data(void);
#endif

//---------------------------------------------------------------------------
// Static variables
//---------------------------------------------------------------------------
static BOOL fgMtk_Sta339Mute=FALSE;

static AMP_FUNCTBL_T _rAmpFunTbl1 = 
{
    _MTK_AQ_AMP_Init1,                           //pfnInit
    NULL,                           //pfnMute 
    NULL,                                       //pfnSetDacInFmt;
    NULL,                                       //pfnSetDacGain;
    _MTK_AQ_AMP_RegWrite1,                       //pfnWrite;
    _MTK_AQ_AMP_RegRead1,                        //pfnRead;
    NULL,                                       //pfnDrvCB;    
};

static AMP_FUNCTBL_T _rAmpFunTbl2 = 
{
    _MTK_AQ_AMP_Init2,                           //pfnInit
    NULL,                           //pfnMute 
    NULL,                                       //pfnSetDacInFmt;
    NULL,                                       //pfnSetDacGain;
    _MTK_AQ_AMP_RegWrite2,                       //pfnWrite;
    _MTK_AQ_AMP_RegRead2,                        //pfnRead;
    NULL,                                       //pfnDrvCB;    
};

static AMP_T _rAmpMtk_AQ_AMP1 = 
{
    "AQ AMP1",               // szName[32];
    256,                        // i2MaxGain
    -256,                       // i2MaxGain
    2,                          // u1DacNum
    &_rAmpFunTbl1                // rCodecFuncTbl
};

static AMP_T _rAmpMtk_AQ_AMP2 = 
{
    "AQ AMP2",               // szName[32];
    256,                        // i2MaxGain
    -256,                       // i2MaxGain
    2,                          // u1DacNum
    &_rAmpFunTbl2                // rCodecFuncTbl
};

#ifdef CC_AUD_SAKURA_RENDER_KDAMP
static UINT8 COL_INDEX[MUX_NUM] = {0};
static UINT8 GEN_CONFIG[GEN_CONFIG_NUM]  = {0};
static UINT8 SLEW_RATE[4] = {0};
static UINT8 INPUT_MUX[MUX_NUM][32] = {0};
static UINT8 OUTPUT_MUX[MUX_NUM][8] = {0}; 
static UINT8 VOLUME [VOL_NUM][4] = {0};
static UINT8 BQ_CH12[BQ_NUM][20] = {0};
static UINT8 BQ_CH34[BQ_NUM][20] = {0};
static UINT8 BQ_CH56[BQ_NUM][20] = {0};
static UINT8 BQ_CH78[BQ_NUM][20] = {0};
static UINT8 BT_ENABLE[BT_NUM][8] = {0};
static UINT8 BASS_FILTER_FC[4] = {0};
static UINT8 BASS_FILTER_INDEX[4] = {0};
static UINT8 TREBLE_FILTER_FC[4] = {0};
static UINT8 TREBLE_FILTER_INDEX[4] = {0};
static UINT8 DRC1_CONTROL[4] = {0};
static UINT8 DRC2_CONTROL[4] = {0};
static UINT8 DRC1_K0K1K2[12] = {0};
static UINT8 DRC2_K0K1K2[12] = {0};
static UINT8 DRC1_T1T2[8] = {0};
static UINT8 DRC2_T1T2[8] = {0};
static UINT8 DRC1_O1O2[8] = {0};
static UINT8 DRC2_O1O2[8] = {0};
static UINT8 DRC1_EST_TIME[8] = {0};
static UINT8 DRC2_EST_TIME[8] = {0};
static UINT8 DRC1_ATK_REL_TIME[16] = {0};
static UINT8 DRC2_ATK_REL_TIME[16] = {0};
static UINT8 DRC_ENABLE[4][8] = {0};
static UINT8 LOUD_LG[4] = {0};
static UINT8 LOUD_LO[4] = {0};
static UINT8 LOUD_GAIN[4] = {0};
static UINT8 LOUD_OFFSET[4] = {0};
static UINT8 LOUD_BQ[20] = {0};

static UINT32 ErrCount5760 = 0;
static UINT32 ErrCount5538 = 0;
static UINT32 ResetErrCount5760 = 0;
static UINT32 ResetErrCount5538 = 0;

static UINT32 fgAmpInit = 0;
#endif


//---------------------------------------------------------------------------
// Static functions
//---------------------------------------------------------------------------

#ifdef CC_MAPLE_CUST_DRV_AUD
// =================================================
// [**CAUTION**] 
// 1. Use printf, not LOG
// 2. You can only call public functions in xxx_if.c
//==================================================
#ifdef CC_AUD_SAKURA_RENDER_KDAMP
static void _KD_AQ_AMP_RegWrite(UINT8 u1Addr, UINT8* u1Data, UINT8 Index, UINT8 length){
    UINT32 u4WriteCount = 0; 

    u4WriteCount = SIF_X_Write(gAmpData[Index].u2ClkDivAQAmp, SIF_CLK_DIV, gAmpData[Index].u1DevAddrAQAmp, 1 ,u1Addr, u1Data, length);
    if (u4WriteCount == 0)
    {
        if (gAmpData[Index].u1DevAddrAQAmp != 0x34){ //5760 Err case
            ErrCount5760++;
            ResetErrCount5760 = 0;
        }
        else{ // 5538 Err case
            ErrCount5538++;
            ResetErrCount5538 = 0;
        }
        
#ifdef CUSTOM_SELF_DIAG_ENABLE
        if (ErrCount5760 >= NTFY_ERR_NUM_5760){
            LOG(0, "_KD_AQ_AMP_RegWrite 5760 FAIL\n");
            SelfDiagTriggerAudI2CError();
        }
        if (ErrCount5538 >= NTFY_ERR_NUM_5538){
            LOG(0, "_KD_AQ_AMP_RegWrite 5538 FAIL\n");
            SelfDiagTriggerAudI2CError();
        }
#endif
    }
    else{
        if (gAmpData[Index].u1DevAddrAQAmp != 0x34){ //5760 OK case
            ResetErrCount5760++;
            if (ResetErrCount5760 > 1){
                ErrCount5760 = 0;
                ResetErrCount5760 = 0;
            }
        }
        else{ //5538 OK case
            ResetErrCount5538++;
            if (ResetErrCount5538 > 1){
                ErrCount5538 = 0;
                ResetErrCount5538 = 0;
            }
        }
    }
}

static UINT8 _KD_AQ_AMP_RegRead(UINT8 u1Addr, UINT8 Index, UINT8* readData){
    UINT32 u4ReadCount = 0;
    UINT8 u1Data;

    u4ReadCount = SIF_X_Read(gAmpData[Index].u2ClkDivAQAmp, SIF_CLK_DIV, gAmpData[Index].u1DevAddrAQAmp, 1 , u1Addr, &u1Data, 1);

    if (u4ReadCount == 0)
    {
        if (fgAmpInit != 0){
        if (gAmpData[Index].u1DevAddrAQAmp != 0x34){ //5760 Err case
            ErrCount5760++;
        }
        else{ // 5538 Err case
            ErrCount5538++;
        }
        
#ifdef CUSTOM_SELF_DIAG_ENABLE
        if (ErrCount5760 >= NTFY_ERR_NUM_5760){
            SelfDiagTriggerAudI2CError();
                LOG(0, "_KD_AQ_AMP_RegWrite 5760 FAIL\n");
        }
        if (ErrCount5538 >= NTFY_ERR_NUM_5538){
            SelfDiagTriggerAudI2CError();
                LOG(0, "_KD_AQ_AMP_RegWrite 5760 FAIL\n");
        }
#endif
        }
        return 0;
    }
    else{
        if (gAmpData[Index].u1DevAddrAQAmp != 0x34){ //5760 OK case
            ResetErrCount5760++;
            if (ResetErrCount5760 > 1){
                ErrCount5760 = 0;
                ResetErrCount5760 = 0;
            }
        }
        else{ //5538 OK case
            ResetErrCount5538++;
            if (ResetErrCount5538 > 1){
                ErrCount5538 = 0;
                ResetErrCount5538 = 0;
            }
        }        
        *readData = u1Data;
        return 1;
    }  
}
#endif
static void _MTK_AQ_AMP_RegWrite1(UINT8 u1Addr, UINT8 u1Data)
{
    UINT32 u4WriteCount = 0; 

    u4WriteCount = SIF_X_Write(gAmpData[0].u2ClkDivAQAmp, SIF_CLK_DIV, gAmpData[0].u1DevAddrAQAmp, 1 ,u1Addr, &u1Data, 1);
    if (u4WriteCount == 0)
    {
#ifdef CUSTOM_SELF_DIAG_ENABLE
        SelfDiagTriggerAudI2CError();
#endif
        LOG(1,"_MTK_AQ_AMP_RegWrite FAIL\n");
    }    
}

static UINT8 _MTK_AQ_AMP_RegRead1(UINT8 u1Addr) 
{
    UINT32 u4ReadCount = 0;
    UINT8 u1Data;

    u4ReadCount = SIF_X_Read(gAmpData[0].u2ClkDivAQAmp, SIF_CLK_DIV, gAmpData[0].u1DevAddrAQAmp, 1 , u1Addr, &u1Data, 1);

    if (u4ReadCount > 0)
    {
        return u1Data;
    }    
    else
    {
#ifdef CUSTOM_SELF_DIAG_ENABLE
        SelfDiagTriggerAudI2CError();
#endif
        LOG(1,"_MTK_AQ_AMP_RegRead FAIL\n");
        return 0;
    }    
}

static void _MTK_AQ_AMP_RegWrite2(UINT8 u1Addr, UINT8 u1Data)
{
    UINT32 u4WriteCount = 0; 

    u4WriteCount = SIF_X_Write(gAmpData[1].u2ClkDivAQAmp, SIF_CLK_DIV, gAmpData[1].u1DevAddrAQAmp, 1 ,u1Addr, &u1Data, 1);
    if (u4WriteCount == 0)
    {
#ifdef CUSTOM_SELF_DIAG_ENABLE
        SelfDiagTriggerAudI2CError();
#endif
        LOG(1,"_MTK_AQ_AMP_RegWrite1 FAIL\n");
    }    
}

static UINT8 _MTK_AQ_AMP_RegRead2(UINT8 u1Addr) 
{
    UINT32 u4ReadCount = 0;
    UINT8 u1Data;

    u4ReadCount = SIF_X_Read(gAmpData[1].u2ClkDivAQAmp, SIF_CLK_DIV, gAmpData[1].u1DevAddrAQAmp, 1 , u1Addr, &u1Data, 1);

    if (u4ReadCount > 0)
    {
        return u1Data;
    }    
    else
    {
#ifdef CUSTOM_SELF_DIAG_ENABLE
        SelfDiagTriggerAudI2CError();
#endif
        LOG(1,"_MTK_AQ_AMP_RegRead1 FAIL\n");
        return 0;
    }    
}

#ifdef CC_AUD_SAKURA_RENDER_KDAMP
static void _KD_AQ_5538_Update_Data(void)
{
    UINT32 i;
    
    GEN_CONFIG[0] = gDa5538CfgData.m_DaSystemControl1;
    GEN_CONFIG[1] = gDa5538CfgData.m_DaSystemControl2;
    GEN_CONFIG[2] = gDa5538CfgData.m_DaChannelConfigCh1;
    GEN_CONFIG[3] = gDa5538CfgData.m_DaChannelConfigCh2;
    GEN_CONFIG[4] = gDa5538CfgData.m_DaChannelConfigCh3;
    GEN_CONFIG[5] = gDa5538CfgData.m_DaChannelConfigCh4;
    GEN_CONFIG[6] = gDa5538CfgData.m_DaChannelConfigCh5;
    GEN_CONFIG[7] = gDa5538CfgData.m_DaChannelConfigCh6;
    GEN_CONFIG[8] = gDa5538CfgData.m_DaChannelConfigCh7;
    GEN_CONFIG[9] = gDa5538CfgData.m_DaChannelConfigCh8;
    GEN_CONFIG[10] = gDa5538CfgData.m_DaSerialDataFormat;
    GEN_CONFIG[11] = gDa5538CfgData.m_DaAutomuteControl;
    GEN_CONFIG[12] = gDa5538CfgData.m_DaAutomutePwmTh;
    GEN_CONFIG[13] = gDa5538CfgData.m_DaModLimitCh12;
    GEN_CONFIG[14] = gDa5538CfgData.m_DaModLimitCh34;
    GEN_CONFIG[15] = gDa5538CfgData.m_DaModLimitCh56;
    GEN_CONFIG[16] = gDa5538CfgData.m_DaModLimitCh78;
    GEN_CONFIG[17] = gDa5538CfgData.m_DaIcDelayCh1;
    GEN_CONFIG[18] = gDa5538CfgData.m_DaIcDelayCh2;
    GEN_CONFIG[19] = gDa5538CfgData.m_DaIcDelayCh3;
    GEN_CONFIG[20] = gDa5538CfgData.m_DaIcDelayCh4;
    GEN_CONFIG[21] = gDa5538CfgData.m_DaIcDelayCh5;
    GEN_CONFIG[22] = gDa5538CfgData.m_DaIcDelayCh6;
    GEN_CONFIG[23] = gDa5538CfgData.m_DaIcDelayCh7;
    GEN_CONFIG[24] = gDa5538CfgData.m_DaIcDelayCh8;
    GEN_CONFIG[25] = gDa5538CfgData.m_DaIcOffsetDelay;
    GEN_CONFIG[26] = gDa5538CfgData.m_DaPwmSeqTiming; 
    GEN_CONFIG[27] = gDa5538CfgData.m_DaPwmControl;
    GEN_CONFIG[28] = gDa5538CfgData.m_DaIndividualChShutdown;
    GEN_CONFIG[29] = gDa5538CfgData.m_DaInputMuxCh12;
    GEN_CONFIG[30] = gDa5538CfgData.m_DaInputMuxCh34;
    GEN_CONFIG[31] = gDa5538CfgData.m_DaInputMuxCh56;
    GEN_CONFIG[32] = gDa5538CfgData.m_DaInputMuxCh78;
    GEN_CONFIG[33] = gDa5538CfgData.m_DaPwmMuxCh12;
    GEN_CONFIG[34] = gDa5538CfgData.m_DaPwmMuxCh34;
    GEN_CONFIG[35] = gDa5538CfgData.m_DaPwmMuxCh56;
    GEN_CONFIG[36] = gDa5538CfgData.m_DaPwmMuxCh78;
    GEN_CONFIG[37] = gDa5538CfgData.m_DaIcDelayBdmCh1;
    GEN_CONFIG[38] = gDa5538CfgData.m_DaIcDelayBdmCh2;
    GEN_CONFIG[39] = gDa5538CfgData.m_DaIcDelayBdmCh3;
    GEN_CONFIG[40] = gDa5538CfgData.m_DaIcDelayBdmCh4;
    GEN_CONFIG[41] = gDa5538CfgData.m_DaIcDelayBdmCh5;
    GEN_CONFIG[42] = gDa5538CfgData.m_DaIcDelayBdmCh6;
    GEN_CONFIG[43] = gDa5538CfgData.m_DaIcDelayBdmCh7;
    GEN_CONFIG[44] = gDa5538CfgData.m_DaIcDelayBdmCh8;
    
    COL_INDEX[0] = ((gDa5538CfgData.m_DaInputMux1 & 0xFF000000) >> 28) * 4;
    COL_INDEX[1] = ((gDa5538CfgData.m_DaInputMux2 & 0xFF000000) >> 28) * 4;
    COL_INDEX[2] = ((gDa5538CfgData.m_DaInputMux3 & 0xFF000000) >> 28) * 4;
    COL_INDEX[3] = ((gDa5538CfgData.m_DaInputMux4 & 0xFF000000) >> 28) * 4;    
    COL_INDEX[4] = ((gDa5538CfgData.m_DaInputMux5 & 0xFF000000) >> 28) * 4;
    COL_INDEX[5] = ((gDa5538CfgData.m_DaInputMux6 & 0xFF000000) >> 28) * 4;
    COL_INDEX[6] = ((gDa5538CfgData.m_DaInputMux7 & 0xFF000000) >> 28) * 4;
    COL_INDEX[7] = ((gDa5538CfgData.m_DaInputMux8 & 0xFF000000) >> 28) * 4;

    for( i=0; i<MUX_NUM; i++){
        if(COL_INDEX[i] > 28){
            COL_INDEX[i] = 0;
            Printf("Input Mux Data is wrong!\n");
        }
    }
    
    // Input Mux Data Input
    // To ignore first 4bit this process can't use _KD_AQ_DATA_CPY
    INPUT_MUX[0][COL_INDEX[0]]  =((gDa5538CfgData.m_DaInputMux1 & 0x0F000000) >> 24);
    INPUT_MUX[0][COL_INDEX[0]+1]=((gDa5538CfgData.m_DaInputMux1 & 0xFF0000) >> 16);
    INPUT_MUX[0][COL_INDEX[0]+2]=((gDa5538CfgData.m_DaInputMux1 & 0xFF00) >> 8);
    INPUT_MUX[0][COL_INDEX[0]+3]=((gDa5538CfgData.m_DaInputMux1 & 0xFF));
    INPUT_MUX[1][COL_INDEX[1]]  =((gDa5538CfgData.m_DaInputMux2 & 0x0F000000) >> 24);
    INPUT_MUX[1][COL_INDEX[1]+1]=((gDa5538CfgData.m_DaInputMux2 & 0xFF0000) >> 16);
    INPUT_MUX[1][COL_INDEX[1]+2]=((gDa5538CfgData.m_DaInputMux2 & 0xFF00) >> 8);
    INPUT_MUX[1][COL_INDEX[1]+3]=((gDa5538CfgData.m_DaInputMux2 & 0xFF));
    INPUT_MUX[2][COL_INDEX[2]]  =((gDa5538CfgData.m_DaInputMux3 & 0x0F000000) >> 24);
    INPUT_MUX[2][COL_INDEX[2]+1]=((gDa5538CfgData.m_DaInputMux3 & 0xFF0000) >> 16);
    INPUT_MUX[2][COL_INDEX[2]+2]=((gDa5538CfgData.m_DaInputMux3 & 0xFF00) >> 8);
    INPUT_MUX[2][COL_INDEX[2]+3]=((gDa5538CfgData.m_DaInputMux3 & 0xFF));
    INPUT_MUX[3][COL_INDEX[3]]  =((gDa5538CfgData.m_DaInputMux4 & 0x0F000000) >> 24);
    INPUT_MUX[3][COL_INDEX[3]+1]=((gDa5538CfgData.m_DaInputMux4 & 0xFF0000) >> 16);
    INPUT_MUX[3][COL_INDEX[3]+2]=((gDa5538CfgData.m_DaInputMux4 & 0xFF00) >> 8);
    INPUT_MUX[3][COL_INDEX[3]+3]=((gDa5538CfgData.m_DaInputMux4 & 0xFF));
    INPUT_MUX[4][COL_INDEX[4]]  =((gDa5538CfgData.m_DaInputMux5 & 0x0F000000) >> 24);
    INPUT_MUX[4][COL_INDEX[4]+1]=((gDa5538CfgData.m_DaInputMux5 & 0xFF0000) >> 16);
    INPUT_MUX[4][COL_INDEX[4]+2]=((gDa5538CfgData.m_DaInputMux5 & 0xFF00) >> 8);
    INPUT_MUX[4][COL_INDEX[4]+3]=((gDa5538CfgData.m_DaInputMux5 & 0xFF));
    INPUT_MUX[5][COL_INDEX[5]]  =((gDa5538CfgData.m_DaInputMux6 & 0x0F000000) >> 24);
    INPUT_MUX[5][COL_INDEX[5]+1]=((gDa5538CfgData.m_DaInputMux6 & 0xFF0000) >> 16);
    INPUT_MUX[5][COL_INDEX[5]+2]=((gDa5538CfgData.m_DaInputMux6 & 0xFF00) >> 8);
    INPUT_MUX[5][COL_INDEX[5]+3]=((gDa5538CfgData.m_DaInputMux6 & 0xFF));
    INPUT_MUX[6][COL_INDEX[6]]  =((gDa5538CfgData.m_DaInputMux7 & 0x0F000000) >> 24);
    INPUT_MUX[6][COL_INDEX[6]+1]=((gDa5538CfgData.m_DaInputMux7 & 0xFF0000) >> 16);
    INPUT_MUX[6][COL_INDEX[6]+2]=((gDa5538CfgData.m_DaInputMux7 & 0xFF00) >> 8);
    INPUT_MUX[6][COL_INDEX[6]+3]=((gDa5538CfgData.m_DaInputMux7 & 0xFF));
    INPUT_MUX[7][COL_INDEX[7]]  =((gDa5538CfgData.m_DaInputMux8 & 0x0F000000) >> 24);
    INPUT_MUX[7][COL_INDEX[7]+1]=((gDa5538CfgData.m_DaInputMux8 & 0xFF0000) >> 16); 
    INPUT_MUX[7][COL_INDEX[7]+2]=((gDa5538CfgData.m_DaInputMux8 & 0xFF00) >> 8);
    INPUT_MUX[7][COL_INDEX[7]+3]=((gDa5538CfgData.m_DaInputMux8 & 0xFF));
    
    //Output Mux Data
    _KD_AQ_DATA_CPY(OUTPUT_MUX[0], gDa5538CfgData.m_DaOutputMuxPwm1, 4);
    _KD_AQ_DATA_CPY(OUTPUT_MUX[1], gDa5538CfgData.m_DaOutputMuxPwm2, 4);
    _KD_AQ_DATA_CPY(OUTPUT_MUX[2], gDa5538CfgData.m_DaOutputMuxPwm3, 4);
    _KD_AQ_DATA_CPY(OUTPUT_MUX[3], gDa5538CfgData.m_DaOutputMuxPwm4, 4);
    _KD_AQ_DATA_CPY(OUTPUT_MUX[4], gDa5538CfgData.m_DaOutputMuxPwm5, 4);
    _KD_AQ_DATA_CPY(OUTPUT_MUX[5], gDa5538CfgData.m_DaOutputMuxPwm6, 4);
    _KD_AQ_DATA_CPY(OUTPUT_MUX[6], gDa5538CfgData.m_DaOutputMuxPwm7, 4);
    _KD_AQ_DATA_CPY(OUTPUT_MUX[7], gDa5538CfgData.m_DaOutputMuxPwm8, 4);
    
    //Slew Rate
    SLEW_RATE[2]=(gDa5538CfgData.m_DaVolumeSlewRate);
    SLEW_RATE[3]=(gDa5538CfgData.m_DaBassTrebleSlewRate);
    
    //Volume
    _KD_AQ_DATA_CPY(&VOLUME[0][2], gDa5538CfgData.m_DaVolumeCh12, 2);
    _KD_AQ_DATA_CPY(&VOLUME[1][2], gDa5538CfgData.m_DaVolumeCh34, 2);
    _KD_AQ_DATA_CPY(&VOLUME[2][2], gDa5538CfgData.m_DaVolumeCh56, 2);
    _KD_AQ_DATA_CPY(&VOLUME[3][2], gDa5538CfgData.m_DaVolumeCh78, 2);
    _KD_AQ_DATA_CPY(&VOLUME[4][2], gDa5538CfgData.m_DaMasterVolume, 2);

    //BQ CH12
    _KD_AQ_DATA_CPY(&BQ_CH12[0][0], gDa5538BqData.m_DaCh12Bq1B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[0][4], gDa5538BqData.m_DaCh12Bq1B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[0][8], gDa5538BqData.m_DaCh12Bq1B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[0][12],gDa5538BqData.m_DaCh12Bq1A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[0][16],gDa5538BqData.m_DaCh12Bq1A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[1][0], gDa5538BqData.m_DaCh12Bq2B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[1][4], gDa5538BqData.m_DaCh12Bq2B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[1][8], gDa5538BqData.m_DaCh12Bq2B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[1][12],gDa5538BqData.m_DaCh12Bq2A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[1][16],gDa5538BqData.m_DaCh12Bq2A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[2][0], gDa5538BqData.m_DaCh12Bq3B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[2][4], gDa5538BqData.m_DaCh12Bq3B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[2][8], gDa5538BqData.m_DaCh12Bq3B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[2][12],gDa5538BqData.m_DaCh12Bq3A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[2][16],gDa5538BqData.m_DaCh12Bq3A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[3][0], gDa5538BqData.m_DaCh12Bq4B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[3][4], gDa5538BqData.m_DaCh12Bq4B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[3][8], gDa5538BqData.m_DaCh12Bq4B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[3][12],gDa5538BqData.m_DaCh12Bq4A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[3][16],gDa5538BqData.m_DaCh12Bq4A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[4][0], gDa5538BqData.m_DaCh12Bq5B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[4][4], gDa5538BqData.m_DaCh12Bq5B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[4][8], gDa5538BqData.m_DaCh12Bq5B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[4][12],gDa5538BqData.m_DaCh12Bq5A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[4][16],gDa5538BqData.m_DaCh12Bq5A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[5][0], gDa5538BqData.m_DaCh12Bq6B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[5][4], gDa5538BqData.m_DaCh12Bq6B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[5][8], gDa5538BqData.m_DaCh12Bq6B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[5][12],gDa5538BqData.m_DaCh12Bq6A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[5][16],gDa5538BqData.m_DaCh12Bq6A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[6][0], gDa5538BqData.m_DaCh12Bq7B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[6][4], gDa5538BqData.m_DaCh12Bq7B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[6][8], gDa5538BqData.m_DaCh12Bq7B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[6][12],gDa5538BqData.m_DaCh12Bq7A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH12[6][16],gDa5538BqData.m_DaCh12Bq7A2, 4);
    
    //BQ CH34
    _KD_AQ_DATA_CPY(&BQ_CH34[0][0], gDa5538BqData.m_DaCh34Bq1B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[0][4], gDa5538BqData.m_DaCh34Bq1B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[0][8], gDa5538BqData.m_DaCh34Bq1B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[0][12],gDa5538BqData.m_DaCh34Bq1A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[0][16],gDa5538BqData.m_DaCh34Bq1A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[1][0], gDa5538BqData.m_DaCh34Bq2B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[1][4], gDa5538BqData.m_DaCh34Bq2B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[1][8], gDa5538BqData.m_DaCh34Bq2B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[1][12],gDa5538BqData.m_DaCh34Bq2A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[1][16],gDa5538BqData.m_DaCh34Bq2A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[2][0], gDa5538BqData.m_DaCh34Bq3B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[2][4], gDa5538BqData.m_DaCh34Bq3B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[2][8], gDa5538BqData.m_DaCh34Bq3B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[2][12],gDa5538BqData.m_DaCh34Bq3A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[2][16],gDa5538BqData.m_DaCh34Bq3A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[3][0], gDa5538BqData.m_DaCh34Bq4B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[3][4], gDa5538BqData.m_DaCh34Bq4B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[3][8], gDa5538BqData.m_DaCh34Bq4B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[3][12],gDa5538BqData.m_DaCh34Bq4A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[3][16],gDa5538BqData.m_DaCh34Bq4A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[4][0], gDa5538BqData.m_DaCh34Bq5B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[4][4], gDa5538BqData.m_DaCh34Bq5B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[4][8], gDa5538BqData.m_DaCh34Bq5B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[4][12],gDa5538BqData.m_DaCh34Bq5A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[4][16],gDa5538BqData.m_DaCh34Bq5A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[5][0], gDa5538BqData.m_DaCh34Bq6B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[5][4], gDa5538BqData.m_DaCh34Bq6B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[5][8], gDa5538BqData.m_DaCh34Bq6B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[5][12],gDa5538BqData.m_DaCh34Bq6A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[5][16],gDa5538BqData.m_DaCh34Bq6A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[6][0], gDa5538BqData.m_DaCh34Bq7B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[6][4], gDa5538BqData.m_DaCh34Bq7B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[6][8], gDa5538BqData.m_DaCh34Bq7B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[6][12],gDa5538BqData.m_DaCh34Bq7A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH34[6][16],gDa5538BqData.m_DaCh34Bq7A2, 4);
    
    //BQ CH56
    _KD_AQ_DATA_CPY(&BQ_CH56[0][0], gDa5538BqData.m_DaCh56Bq1B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[0][4], gDa5538BqData.m_DaCh56Bq1B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[0][8], gDa5538BqData.m_DaCh56Bq1B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[0][12],gDa5538BqData.m_DaCh56Bq1A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[0][16],gDa5538BqData.m_DaCh56Bq1A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[1][0], gDa5538BqData.m_DaCh56Bq2B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[1][4], gDa5538BqData.m_DaCh56Bq2B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[1][8], gDa5538BqData.m_DaCh56Bq2B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[1][12],gDa5538BqData.m_DaCh56Bq2A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[1][16],gDa5538BqData.m_DaCh56Bq2A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[2][0], gDa5538BqData.m_DaCh56Bq3B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[2][4], gDa5538BqData.m_DaCh56Bq3B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[2][8], gDa5538BqData.m_DaCh56Bq3B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[2][12],gDa5538BqData.m_DaCh56Bq3A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[2][16],gDa5538BqData.m_DaCh56Bq3A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[3][0], gDa5538BqData.m_DaCh56Bq4B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[3][4], gDa5538BqData.m_DaCh56Bq4B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[3][8], gDa5538BqData.m_DaCh56Bq4B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[3][12],gDa5538BqData.m_DaCh56Bq4A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[3][16],gDa5538BqData.m_DaCh56Bq4A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[4][0], gDa5538BqData.m_DaCh56Bq5B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[4][4], gDa5538BqData.m_DaCh56Bq5B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[4][8], gDa5538BqData.m_DaCh56Bq5B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[4][12],gDa5538BqData.m_DaCh56Bq5A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[4][16],gDa5538BqData.m_DaCh56Bq5A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[5][0], gDa5538BqData.m_DaCh56Bq6B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[5][4], gDa5538BqData.m_DaCh56Bq6B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[5][8], gDa5538BqData.m_DaCh56Bq6B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[5][12],gDa5538BqData.m_DaCh56Bq6A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[5][16],gDa5538BqData.m_DaCh56Bq6A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[6][0], gDa5538BqData.m_DaCh56Bq7B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[6][4], gDa5538BqData.m_DaCh56Bq7B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[6][8], gDa5538BqData.m_DaCh56Bq7B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[6][12],gDa5538BqData.m_DaCh56Bq7A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH56[6][16],gDa5538BqData.m_DaCh56Bq7A2, 4);
    
    //BQ CH78
    _KD_AQ_DATA_CPY(&BQ_CH78[0][0], gDa5538BqData.m_DaCh78Bq1B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[0][4], gDa5538BqData.m_DaCh78Bq1B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[0][8], gDa5538BqData.m_DaCh78Bq1B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[0][12],gDa5538BqData.m_DaCh78Bq1A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[0][16],gDa5538BqData.m_DaCh78Bq1A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[1][0], gDa5538BqData.m_DaCh78Bq2B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[1][4], gDa5538BqData.m_DaCh78Bq2B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[1][8], gDa5538BqData.m_DaCh78Bq2B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[1][12],gDa5538BqData.m_DaCh78Bq2A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[1][16],gDa5538BqData.m_DaCh78Bq2A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[2][0], gDa5538BqData.m_DaCh78Bq3B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[2][4], gDa5538BqData.m_DaCh78Bq3B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[2][8], gDa5538BqData.m_DaCh78Bq3B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[2][12],gDa5538BqData.m_DaCh78Bq3A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[2][16],gDa5538BqData.m_DaCh78Bq3A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[3][0], gDa5538BqData.m_DaCh78Bq4B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[3][4], gDa5538BqData.m_DaCh78Bq4B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[3][8], gDa5538BqData.m_DaCh78Bq4B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[3][12],gDa5538BqData.m_DaCh78Bq4A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[3][16],gDa5538BqData.m_DaCh78Bq4A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[4][0], gDa5538BqData.m_DaCh78Bq5B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[4][4], gDa5538BqData.m_DaCh78Bq5B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[4][8], gDa5538BqData.m_DaCh78Bq5B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[4][12],gDa5538BqData.m_DaCh78Bq5A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[4][16],gDa5538BqData.m_DaCh78Bq5A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[5][0], gDa5538BqData.m_DaCh78Bq6B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[5][4], gDa5538BqData.m_DaCh78Bq6B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[5][8], gDa5538BqData.m_DaCh78Bq6B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[5][12],gDa5538BqData.m_DaCh78Bq6A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[5][16],gDa5538BqData.m_DaCh78Bq6A2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[6][0], gDa5538BqData.m_DaCh78Bq7B0, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[6][4], gDa5538BqData.m_DaCh78Bq7B1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[6][8], gDa5538BqData.m_DaCh78Bq7B2, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[6][12],gDa5538BqData.m_DaCh78Bq7A1, 4);
    _KD_AQ_DATA_CPY(&BQ_CH78[6][16],gDa5538BqData.m_DaCh78Bq7A2, 4);
    
    //BassTreble
    if( gDa5538BtData.m_DaBassTrebleEnableCh12 <= 1){
        BT_ENABLE[0][(4 * gDa5538BtData.m_DaBassTrebleEnableCh12) + 1] = 0x80;
    }
    else{
        BT_ENABLE[0][1] = 0x80;
    }
    if( gDa5538BtData.m_DaBassTrebleEnableCh34 <= 1){
        BT_ENABLE[1][(4 * gDa5538BtData.m_DaBassTrebleEnableCh34) + 1] = 0x80;
    }
    else{
        BT_ENABLE[1][1] = 0x80;
    }
    if( gDa5538BtData.m_DaBassTrebleEnableCh56 <= 1){
        BT_ENABLE[2][(4 * gDa5538BtData.m_DaBassTrebleEnableCh56) + 1] = 0x80;
    }
    else{
        BT_ENABLE[2][1] = 0x80;
    }
    if( gDa5538BtData.m_DaBassTrebleEnableCh78 <= 1){
        BT_ENABLE[3][(4 * gDa5538BtData.m_DaBassTrebleEnableCh78) + 1] = 0x80;
    }
    else{
        BT_ENABLE[3][1] = 0x80;
    }
    
    
    BASS_FILTER_FC[0] = gDa5538BtData.m_DaBassFilterSetCh127;
    BASS_FILTER_FC[1] = gDa5538BtData.m_DaBassFilterSetCh34;
    BASS_FILTER_FC[2] = gDa5538BtData.m_DaBassFilterSetCh56;
    BASS_FILTER_FC[3] = gDa5538BtData.m_DaBassFilterSetCh8;
    
    BASS_FILTER_INDEX[0] = gDa5538BtData.m_DaBassFilterIndexCh127;
    BASS_FILTER_INDEX[1] = gDa5538BtData.m_DaBassFilterIndexCh34;
    BASS_FILTER_INDEX[2] = gDa5538BtData.m_DaBassFilterIndexCh56;
    BASS_FILTER_INDEX[3] = gDa5538BtData.m_DaBassFilterIndexCh8;
    
    TREBLE_FILTER_FC[0] = gDa5538BtData.m_DaTrebleFilterSetCh127;
    TREBLE_FILTER_FC[1] = gDa5538BtData.m_DaTrebleFilterSetCh34;
    TREBLE_FILTER_FC[2] = gDa5538BtData.m_DaTrebleFilterSetCh56;
    TREBLE_FILTER_FC[3] = gDa5538BtData.m_DaTrebleFilterSetCh8;
    
    TREBLE_FILTER_INDEX[0] = gDa5538BtData.m_DaTrebleFilterIndexCh127;
    TREBLE_FILTER_INDEX[1] = gDa5538BtData.m_DaTrebleFilterIndexCh34;
    TREBLE_FILTER_INDEX[2] = gDa5538BtData.m_DaTrebleFilterIndexCh56;
    TREBLE_FILTER_INDEX[3] = gDa5538BtData.m_DaTrebleFilterIndexCh8;
    
    BASS_FILTER_FC[0] = gDa5538BtData.m_DaBassFilterSetCh127;
    BASS_FILTER_FC[1] = gDa5538BtData.m_DaBassFilterSetCh34;
    
    //DRC
    _KD_AQ_DATA_CPY(&DRC1_CONTROL[2], gDa5538DrcData.m_DaDrc1Control, 2);
    _KD_AQ_DATA_CPY(&DRC2_CONTROL[3], gDa5538DrcData.m_DaDrc2Control, 1);
    
    _KD_AQ_DATA_CPY(&DRC1_K0K1K2[0], gDa5538DrcData.m_DaDrc1K0, 4);
    _KD_AQ_DATA_CPY(&DRC1_K0K1K2[4], gDa5538DrcData.m_DaDrc1K1, 4);
    _KD_AQ_DATA_CPY(&DRC1_K0K1K2[8], gDa5538DrcData.m_DaDrc1K2, 4);
    
    _KD_AQ_DATA_CPY(&DRC2_K0K1K2[0], gDa5538DrcData.m_DaDrc2K0, 4);
    _KD_AQ_DATA_CPY(&DRC2_K0K1K2[4], gDa5538DrcData.m_DaDrc2K1, 4);
    _KD_AQ_DATA_CPY(&DRC2_K0K1K2[8], gDa5538DrcData.m_DaDrc2K2, 4);
    
    _KD_AQ_DATA_CPY(&DRC1_T1T2[0], gDa5538DrcData.m_DaDrc1T1, 4);
    _KD_AQ_DATA_CPY(&DRC1_T1T2[4], gDa5538DrcData.m_DaDrc1T2, 4);
    _KD_AQ_DATA_CPY(&DRC2_T1T2[0], gDa5538DrcData.m_DaDrc2T1, 4);
    _KD_AQ_DATA_CPY(&DRC2_T1T2[4], gDa5538DrcData.m_DaDrc2T2, 4);
    
    _KD_AQ_DATA_CPY(&DRC1_O1O2[0], gDa5538DrcData.m_DaDrc1O1, 4);
    _KD_AQ_DATA_CPY(&DRC1_O1O2[4], gDa5538DrcData.m_DaDrc1O2, 4);
    _KD_AQ_DATA_CPY(&DRC2_O1O2[0], gDa5538DrcData.m_DaDrc2O1, 4);
    _KD_AQ_DATA_CPY(&DRC2_O1O2[4], gDa5538DrcData.m_DaDrc2O2, 4);
    
    _KD_AQ_DATA_CPY(&DRC1_EST_TIME[0], gDa5538DrcData.m_DaDrc1EstTime1, 4);
    _KD_AQ_DATA_CPY(&DRC1_EST_TIME[4], gDa5538DrcData.m_DaDrc1EstTime2, 4);
    _KD_AQ_DATA_CPY(&DRC2_EST_TIME[0], gDa5538DrcData.m_DaDrc2EstTime1, 4);
    _KD_AQ_DATA_CPY(&DRC2_EST_TIME[4], gDa5538DrcData.m_DaDrc2EstTime2, 4);
    
    _KD_AQ_DATA_CPY(&DRC1_ATK_REL_TIME[0], gDa5538DrcData.m_DaDrc1EstTime1, 4);
    _KD_AQ_DATA_CPY(&DRC1_ATK_REL_TIME[4], gDa5538DrcData.m_DaDrc1AtkTime2, 4);
    _KD_AQ_DATA_CPY(&DRC1_ATK_REL_TIME[8], gDa5538DrcData.m_DaDrc1RelTime1, 4);
    _KD_AQ_DATA_CPY(&DRC1_ATK_REL_TIME[12], gDa5538DrcData.m_DaDrc1RelTime2, 4);
    
    _KD_AQ_DATA_CPY(&DRC2_ATK_REL_TIME[0], gDa5538DrcData.m_DaDrc2EstTime1, 4);
    _KD_AQ_DATA_CPY(&DRC2_ATK_REL_TIME[4], gDa5538DrcData.m_DaDrc2AtkTime2, 4);
    _KD_AQ_DATA_CPY(&DRC2_ATK_REL_TIME[8], gDa5538DrcData.m_DaDrc2RelTime1, 4);
    _KD_AQ_DATA_CPY(&DRC2_ATK_REL_TIME[12], gDa5538DrcData.m_DaDrc2RelTime2, 4);
    
    if(gDa5538DrcData.m_DaDrcEnableCh12 == 0){
        DRC_ENABLE[0][1] = 0x80;
    }
    else{
        DRC_ENABLE[0][5] = 0x80;
    }
    if(gDa5538DrcData.m_DaDrcEnableCh34 == 0){
        DRC_ENABLE[1][1] = 0x80;
    }
    else{
        DRC_ENABLE[1][5] = 0x80;
    }
    if(gDa5538DrcData.m_DaDrcEnableCh56 == 0){
        DRC_ENABLE[2][1] = 0x80;
    }
    else{
        DRC_ENABLE[2][5] = 0x80;
    }
    if(gDa5538DrcData.m_DaDrcEnableCh78 == 0){
        DRC_ENABLE[3][1] = 0x80;
    }
    else{
        DRC_ENABLE[3][5] = 0x80;
    }
    
    _KD_AQ_DATA_CPY(LOUD_LG, gDa5538LoudData.m_DaLoudLg, 4);
    _KD_AQ_DATA_CPY(LOUD_LO, gDa5538LoudData.m_DaLoudLo, 4);
    _KD_AQ_DATA_CPY(LOUD_GAIN, gDa5538LoudData.m_DaLoudGain, 4);
    _KD_AQ_DATA_CPY(LOUD_OFFSET, gDa5538LoudData.m_DaLoudOffset, 4);
    _KD_AQ_DATA_CPY(&LOUD_BQ[0], gDa5538LoudData.m_DaLoudBqB0, 4);
    _KD_AQ_DATA_CPY(&LOUD_BQ[4], gDa5538LoudData.m_DaLoudBqB1, 4);
    _KD_AQ_DATA_CPY(&LOUD_BQ[8], gDa5538LoudData.m_DaLoudBqB2, 4);
    _KD_AQ_DATA_CPY(&LOUD_BQ[12], gDa5538LoudData.m_DaLoudBqA1, 4);
    _KD_AQ_DATA_CPY(&LOUD_BQ[16], gDa5538LoudData.m_DaLoudBqA2, 4);
    
}
static void _KD_AQ_5538_Init(UINT8 Index)
{
    UINT32 i, j;
    UINT8 S_Data;
    UINT8 readData;
    if(gAmpData[Index].u1AmpConnect == 0)
    {
        return;
    }
    
    _KD_AQ_5538_Update_Data();
    
    x_thread_delay(15); // wait 15ms before powersequence setting 1
    
    UINT32 num = 0;
    while(1){
        LOG(1, "read %dst time\n", num);
        if(_KD_AQ_AMP_RegRead(0x01, Index, &readData) != 0 && readData == 0x03){
            x_os_drv_set_timestamp("KD_READ_AMP5538_OK");
            fgAmpInit = 1;
            break;
        }
        
        x_thread_delay(50);
        num++;
    }
        

    S_Data = 0xC0;
    _KD_AQ_AMP_RegWrite(0x12, &S_Data, Index, 1);
    
    x_thread_delay(50); // wait 50ms before powersequence setting 2
    
    KD_AQ_AMP_5538_Mute(KD_AUD_AMP_MUTE);
    S_Data = 0xB0;
    _KD_AQ_AMP_RegWrite(0x03, &S_Data, Index, 1);
    
    _KD_AQ_Set_5538_Gen_Config(Index);
    _KD_AQ_Set_5538_BQ(Index);
    //_KD_AQ_Set_5538_BassTreble(Index);
        
    
}

static void _KD_AQ_Set_5538_Gen_Config(UINT8 Index){
    
    UINT32 i;
    
    _KD_AQ_AMP_RegWrite(0x03, &GEN_CONFIG[0], Index, 1);
    _KD_AQ_AMP_RegWrite(0x04, &GEN_CONFIG[1], Index, 1);
    _KD_AQ_AMP_RegWrite(0x05, &GEN_CONFIG[2], Index, 1);
    _KD_AQ_AMP_RegWrite(0x06, &GEN_CONFIG[3], Index, 1);
    _KD_AQ_AMP_RegWrite(0x07, &GEN_CONFIG[4], Index, 1);
    _KD_AQ_AMP_RegWrite(0x08, &GEN_CONFIG[5], Index, 1);
    _KD_AQ_AMP_RegWrite(0x09, &GEN_CONFIG[6], Index, 1);
    _KD_AQ_AMP_RegWrite(0x0A, &GEN_CONFIG[7], Index, 1);
    _KD_AQ_AMP_RegWrite(0x0B, &GEN_CONFIG[8], Index, 1);
    _KD_AQ_AMP_RegWrite(0x0C, &GEN_CONFIG[9], Index, 1);
    _KD_AQ_AMP_RegWrite(0x0E, &GEN_CONFIG[10], Index, 1);
    _KD_AQ_AMP_RegWrite(0x14, &GEN_CONFIG[11], Index, 1);
    _KD_AQ_AMP_RegWrite(0x15, &GEN_CONFIG[12], Index, 1);
    _KD_AQ_AMP_RegWrite(0x16, &GEN_CONFIG[13], Index, 1);
    _KD_AQ_AMP_RegWrite(0x17, &GEN_CONFIG[14], Index, 1);
    _KD_AQ_AMP_RegWrite(0x18, &GEN_CONFIG[15], Index, 1);
    _KD_AQ_AMP_RegWrite(0x19, &GEN_CONFIG[16], Index, 1);
    _KD_AQ_AMP_RegWrite(0x1B, &GEN_CONFIG[17], Index, 1);
    _KD_AQ_AMP_RegWrite(0x1C, &GEN_CONFIG[18], Index, 1);
    _KD_AQ_AMP_RegWrite(0x1D, &GEN_CONFIG[19], Index, 1);
    _KD_AQ_AMP_RegWrite(0x1E, &GEN_CONFIG[20], Index, 1);
    _KD_AQ_AMP_RegWrite(0x1F, &GEN_CONFIG[21], Index, 1);
    _KD_AQ_AMP_RegWrite(0x20, &GEN_CONFIG[22], Index, 1);
    _KD_AQ_AMP_RegWrite(0x21, &GEN_CONFIG[23], Index, 1);
    _KD_AQ_AMP_RegWrite(0x22, &GEN_CONFIG[24], Index, 1);
    _KD_AQ_AMP_RegWrite(0x23, &GEN_CONFIG[25], Index, 1);
    _KD_AQ_AMP_RegWrite(0x24, &GEN_CONFIG[26], Index, 1);
    _KD_AQ_AMP_RegWrite(0x25, &GEN_CONFIG[27], Index, 1);
    _KD_AQ_AMP_RegWrite(0x27, &GEN_CONFIG[28], Index, 1);
    _KD_AQ_AMP_RegWrite(0x30, &GEN_CONFIG[29], Index, 1);
    _KD_AQ_AMP_RegWrite(0x31, &GEN_CONFIG[30], Index, 1);
    _KD_AQ_AMP_RegWrite(0x32, &GEN_CONFIG[31], Index, 1);
    _KD_AQ_AMP_RegWrite(0x33, &GEN_CONFIG[32], Index, 1);
    _KD_AQ_AMP_RegWrite(0x34, &GEN_CONFIG[33], Index, 1);
    _KD_AQ_AMP_RegWrite(0x35, &GEN_CONFIG[34], Index, 1);
    _KD_AQ_AMP_RegWrite(0x36, &GEN_CONFIG[35], Index, 1);
    _KD_AQ_AMP_RegWrite(0x37, &GEN_CONFIG[36], Index, 1);
    _KD_AQ_AMP_RegWrite(0x38, &GEN_CONFIG[37], Index, 1);
    _KD_AQ_AMP_RegWrite(0x39, &GEN_CONFIG[38], Index, 1);
    _KD_AQ_AMP_RegWrite(0x3A, &GEN_CONFIG[39], Index, 1);
    _KD_AQ_AMP_RegWrite(0x3B, &GEN_CONFIG[40], Index, 1);
    _KD_AQ_AMP_RegWrite(0x3C, &GEN_CONFIG[41], Index, 1);
    _KD_AQ_AMP_RegWrite(0x3D, &GEN_CONFIG[42], Index, 1);
    _KD_AQ_AMP_RegWrite(0x3E, &GEN_CONFIG[43], Index, 1);
    _KD_AQ_AMP_RegWrite(0x3F, &GEN_CONFIG[44], Index, 1);

    _KD_AQ_AMP_RegWrite(0x41, INPUT_MUX[0], Index, 32);
    _KD_AQ_AMP_RegWrite(0x42, INPUT_MUX[1], Index, 32);
    _KD_AQ_AMP_RegWrite(0x43, INPUT_MUX[2], Index, 32);
    _KD_AQ_AMP_RegWrite(0x44, INPUT_MUX[3], Index, 32);
    _KD_AQ_AMP_RegWrite(0x45, INPUT_MUX[4], Index, 32);
    _KD_AQ_AMP_RegWrite(0x46, INPUT_MUX[5], Index, 32);
    _KD_AQ_AMP_RegWrite(0x47, INPUT_MUX[6], Index, 32);
    _KD_AQ_AMP_RegWrite(0x48, INPUT_MUX[7], Index, 32);
    
    _KD_AQ_AMP_RegWrite(0xAA, OUTPUT_MUX[0], Index, 8);
    _KD_AQ_AMP_RegWrite(0xAB, OUTPUT_MUX[1], Index, 8);
    _KD_AQ_AMP_RegWrite(0xAC, OUTPUT_MUX[2], Index, 8);
    _KD_AQ_AMP_RegWrite(0xAD, OUTPUT_MUX[3], Index, 8);
    _KD_AQ_AMP_RegWrite(0xAE, OUTPUT_MUX[4], Index, 8);
    _KD_AQ_AMP_RegWrite(0xAF, OUTPUT_MUX[5], Index, 8);
    _KD_AQ_AMP_RegWrite(0xB0, OUTPUT_MUX[6], Index, 8);
    _KD_AQ_AMP_RegWrite(0xB1, OUTPUT_MUX[7], Index, 8);
 
    _KD_AQ_AMP_RegWrite(0xD0, SLEW_RATE, Index, 4); 
    
    _KD_AQ_AMP_RegWrite(0xD1, VOLUME[0], Index, 4);
    _KD_AQ_AMP_RegWrite(0xD2, VOLUME[0], Index, 4);
    _KD_AQ_AMP_RegWrite(0xD3, VOLUME[1], Index, 4);
    _KD_AQ_AMP_RegWrite(0xD4, VOLUME[1], Index, 4);
    _KD_AQ_AMP_RegWrite(0xD5, VOLUME[2], Index, 4);
    _KD_AQ_AMP_RegWrite(0xD6, VOLUME[2], Index, 4);
    _KD_AQ_AMP_RegWrite(0xD7, VOLUME[3], Index, 4);
    _KD_AQ_AMP_RegWrite(0xD8, VOLUME[3], Index, 4);
    _KD_AQ_AMP_RegWrite(0xD9, VOLUME[4], Index, 4);
    
    _KD_AQ_AMP_RegWrite(0x03, &gDa5538CfgData.m_DaSystemControl1, Index, 1);
}

static void _KD_AQ_Set_5538_BQ(UINT8 Index){
    
    UINT32 i;
    
    for(i=0; i<BQ_NUM; i++){
        _KD_AQ_AMP_RegWrite(0x51+i, BQ_CH12[i], Index, 20);
        _KD_AQ_AMP_RegWrite(0x58+i, BQ_CH12[i], Index, 20);
        _KD_AQ_AMP_RegWrite(0x5F+i, BQ_CH34[i], Index, 20);
        _KD_AQ_AMP_RegWrite(0x66+i, BQ_CH34[i], Index, 20);
        _KD_AQ_AMP_RegWrite(0x6D+i, BQ_CH56[i], Index, 20);
        _KD_AQ_AMP_RegWrite(0x74+i, BQ_CH56[i], Index, 20);
        _KD_AQ_AMP_RegWrite(0x7B+i, BQ_CH78[i], Index, 20);
        _KD_AQ_AMP_RegWrite(0x82+i, BQ_CH78[i], Index, 20);
    }
    
}

static void _KD_AQ_Set_5538_BassTreble(UINT8 Index){
    
    UINT32 i;
    
    for(i=0; i<BT_NUM; i++){
        _KD_AQ_AMP_RegWrite(0x89+i, BT_ENABLE[i], Index, 8);
        _KD_AQ_AMP_RegWrite(0x8A+i, BT_ENABLE[i], Index, 8);
    }
    
    _KD_AQ_AMP_RegWrite(0xDA, BASS_FILTER_FC, Index, 4);
    _KD_AQ_AMP_RegWrite(0xDB, BASS_FILTER_INDEX, Index, 4);
    _KD_AQ_AMP_RegWrite(0xDC, TREBLE_FILTER_FC, Index, 4);
    _KD_AQ_AMP_RegWrite(0xDD, TREBLE_FILTER_INDEX, Index, 4);
    
}

static void _KD_AQ_Set_5538_DRC(UINT8 Index){
    
    _KD_AQ_AMP_RegWrite(0x96, DRC1_CONTROL, Index, 4);
    _KD_AQ_AMP_RegWrite(0x97, DRC2_CONTROL, Index, 4);
    _KD_AQ_AMP_RegWrite(0x98, DRC1_EST_TIME, Index, 8);
    _KD_AQ_AMP_RegWrite(0x99, DRC1_T1T2, Index, 8);
    _KD_AQ_AMP_RegWrite(0x9A, DRC1_K0K1K2, Index, 12);
    _KD_AQ_AMP_RegWrite(0x9B, DRC1_O1O2, Index, 8);
    _KD_AQ_AMP_RegWrite(0x9C, DRC1_ATK_REL_TIME, Index, 16);
    _KD_AQ_AMP_RegWrite(0x9D, DRC2_EST_TIME, Index, 8);
    _KD_AQ_AMP_RegWrite(0x9E, DRC2_T1T2, Index, 8);
    _KD_AQ_AMP_RegWrite(0x9F, DRC2_K0K1K2, Index, 12);
    _KD_AQ_AMP_RegWrite(0xA0, DRC2_O1O2, Index, 8);
    _KD_AQ_AMP_RegWrite(0xA1, DRC2_ATK_REL_TIME, Index, 16);
    _KD_AQ_AMP_RegWrite(0xA2, DRC_ENABLE[0], Index, 8);
    _KD_AQ_AMP_RegWrite(0xA3, DRC_ENABLE[0], Index, 8);
    _KD_AQ_AMP_RegWrite(0xA4, DRC_ENABLE[1], Index, 8);
    _KD_AQ_AMP_RegWrite(0xA5, DRC_ENABLE[1], Index, 8);
    _KD_AQ_AMP_RegWrite(0xA6, DRC_ENABLE[2], Index, 8);
    _KD_AQ_AMP_RegWrite(0xA7, DRC_ENABLE[2], Index, 8);
    _KD_AQ_AMP_RegWrite(0xA8, DRC_ENABLE[3], Index, 8);
    _KD_AQ_AMP_RegWrite(0xA9, DRC_ENABLE[3], Index, 8);
    
}

static void _KD_AQ_Set_5538_LOUD(UINT8 Index){
    
    _KD_AQ_AMP_RegWrite(0x91, LOUD_LG, Index, 4);
    _KD_AQ_AMP_RegWrite(0x92, LOUD_LO, Index, 4);
    _KD_AQ_AMP_RegWrite(0x93, LOUD_GAIN, Index, 4);
    _KD_AQ_AMP_RegWrite(0x94, LOUD_OFFSET, Index, 4);
    _KD_AQ_AMP_RegWrite(0x95, LOUD_BQ, Index, 20);
    
}

static void _KD_AQ_5760_Init(UINT8 Index)
{
    UINT8 S_Data;
    UINT8 readData;
    
    if(gAmpData[Index].u1AmpConnect == 0)
        return;
    
    x_thread_delay(10);
    KD_AQ_AMP_5760_Mute(KD_AUD_AMP_MUTE);
    
    UINT32 num = 0;
    while(1){
        LOG(1, "read %dst time\n", num);
        if(_KD_AQ_AMP_RegRead(0x00, Index, &readData) != 0 && readData == 0x00){
            x_os_drv_set_timestamp("KD_READ_AMP5760_OK");
            fgAmpInit = 1;
            break;
        }
        
        x_thread_delay(50);
        num++;
    }
       
    
    if(gAmpData[Index].u1AmpTable == 0)
    {
        LOG(1, "5890 Data type is 0\n");
        // Power control
        if(_KD_AQ_AMP_RegRead(0x1, Index, &readData) != 0){
            S_Data = (gDa5760Cfg1Data.m_Da1DigitalClip3 & 0xFC) + (readData & 0x3);
        _KD_AQ_AMP_RegWrite(0x1, &S_Data, Index, 1);
        }
        else{
            LOG(0, "Amp Read Error! Skip RegWrite\n");
        }
        // Digital control
        if(_KD_AQ_AMP_RegRead(0x2, Index, &readData) != 0){
            S_Data = (gDa5760Cfg1Data.m_Da1DigitalControl & 0xF7) + (readData & 0x08);
        _KD_AQ_AMP_RegWrite(0x2, &S_Data, Index, 1);
        }
        else{
            LOG(0, "Amp Read Error! Skip RegWrite\n");
        }
        //Left channel volume control
        _KD_AQ_AMP_RegWrite(0x4, &gDa5760Cfg1Data.m_Da1Volume, Index, 1);
        //Right channel volume control
        _KD_AQ_AMP_RegWrite(0x5, &gDa5760Cfg1Data.m_Da1Volume, Index, 1);
        //Analog control
        _KD_AQ_AMP_RegWrite(0x6, &gDa5760Cfg1Data.m_Da1AnalogControl, Index, 1);
        //Digital clipper2
        _KD_AQ_AMP_RegWrite(0x10, &gDa5760Cfg1Data.m_Da1DigitalClip2, Index, 1);
        //Digital clipper1
        _KD_AQ_AMP_RegWrite(0x11, &gDa5760Cfg1Data.m_Da1DigitalClip1, Index, 1);
    }
    else if(gAmpData[Index].u1AmpTable == 1)
{
        LOG(1, "5890 Data type is 1\n");
        // Power control
        if(_KD_AQ_AMP_RegRead(0x1, Index, &readData) != 0){
            S_Data = (gDa5760Cfg2Data.m_Da2DigitalClip3 & 0xFC) + (readData & 0x3);
        _KD_AQ_AMP_RegWrite(0x1, &S_Data, Index, 1);
        }
        else{
            LOG(0, "Amp Read Error! Skip RegWrite\n");
        }
        // Digital control
        if(_KD_AQ_AMP_RegRead(0x2, Index, &readData) != 0){
            S_Data = (gDa5760Cfg2Data.m_Da2DigitalControl & 0xF7) + (readData & 0x08);
        _KD_AQ_AMP_RegWrite(0x2, &S_Data, Index, 1);
        }
        else{
            LOG(0, "Amp Read Error! Skip RegWrite\n");
        }
        //Left channel volume control
        _KD_AQ_AMP_RegWrite(0x4, &gDa5760Cfg2Data.m_Da2Volume, Index, 1);
        //Right channel volume control
        _KD_AQ_AMP_RegWrite(0x5, &gDa5760Cfg2Data.m_Da2Volume, Index, 1);
        //Analog control
        _KD_AQ_AMP_RegWrite(0x6, &gDa5760Cfg2Data.m_Da2AnalogControl, Index, 1);
        //Digital clipper2
        _KD_AQ_AMP_RegWrite(0x10, &gDa5760Cfg2Data.m_Da2DigitalClip2, Index, 1);
        //Digital clipper1
        _KD_AQ_AMP_RegWrite(0x11, &gDa5760Cfg2Data.m_Da2DigitalClip1, Index, 1);
    }
}
#endif //CC_AUD_SAKURA_RENDER_KDAMP



static void _MTK_AQ_AMP_Init1(void)
{

    if(gAmpData[0].u1AmpConnect == 0)
        return;

    if(gAmpData[0].u1DevAddrAQAmp == 0x34) // AMP 5538
    {
    
    }
    else // AMP5760
    {
        if(gAmpData[0].u1AmpTable == 0)
        {
            // Power control
            _MTK_AQ_AMP_RegWrite1(0x1,gDa5760Cfg1Data.m_Da1DigitalClip3 + (_MTK_AQ_AMP_RegRead1(0x1) & 0x3));
            // Digital control
            _MTK_AQ_AMP_RegWrite1(0x2,(gDa5760Cfg1Data.m_Da1DigitalControl & 0xF7) + (_MTK_AQ_AMP_RegRead1(0x2) & 0x08));
            //Left channel volume control
            _MTK_AQ_AMP_RegWrite1(0x4, gDa5760Cfg1Data.m_Da1Volume);
            //Right channel volume control
            _MTK_AQ_AMP_RegWrite1(0x5, gDa5760Cfg1Data.m_Da1Volume);
            //Analog control
            _MTK_AQ_AMP_RegWrite1(0x6, gDa5760Cfg1Data.m_Da1AnalogControl);
            //Digital clipper2
            _MTK_AQ_AMP_RegWrite1(0x10, gDa5760Cfg1Data.m_Da1DigitalClip2);
            //Digital clipper1
            _MTK_AQ_AMP_RegWrite1(0x11, gDa5760Cfg1Data.m_Da1DigitalClip1);
        }
        else if(gAmpData[0].u1AmpTable == 1)
        {
            // Power control
            _MTK_AQ_AMP_RegWrite1(0x1,gDa5760Cfg2Data.m_Da2DigitalClip3 + (_MTK_AQ_AMP_RegRead1(0x1) & 0x3));
            // Digital control
            _MTK_AQ_AMP_RegWrite1(0x2,(gDa5760Cfg2Data.m_Da2DigitalControl & 0xF7) + (_MTK_AQ_AMP_RegRead1(0x2) & 0x08));
            //Left channel volume control
            _MTK_AQ_AMP_RegWrite1(0x4, gDa5760Cfg2Data.m_Da2Volume);
            //Right channel volume control
            _MTK_AQ_AMP_RegWrite1(0x5, gDa5760Cfg2Data.m_Da2Volume);
            //Analog control
            _MTK_AQ_AMP_RegWrite1(0x6, gDa5760Cfg2Data.m_Da2AnalogControl);
            //Digital clipper2
            _MTK_AQ_AMP_RegWrite1(0x10, gDa5760Cfg2Data.m_Da2DigitalClip2);
            //Digital clipper1
            _MTK_AQ_AMP_RegWrite1(0x11, gDa5760Cfg2Data.m_Da2DigitalClip1);
        }
    }
}

static void _MTK_AQ_AMP_Init2(void)
{
    if(gAmpData[1].u1AmpConnect == 0)
        return;

    if(gAmpData[1].u1AmpTable == 0)
    {
        // Power control
        _MTK_AQ_AMP_RegWrite2(0x1,gDa5760Cfg1Data.m_Da1DigitalClip3 + (_MTK_AQ_AMP_RegRead2(0x1) & 0x3));
        // Digital control
        _MTK_AQ_AMP_RegWrite2(0x2,(gDa5760Cfg1Data.m_Da1DigitalControl & 0xF7) + (_MTK_AQ_AMP_RegRead2(0x2) & 0x08));
        //Left channel volume control
        _MTK_AQ_AMP_RegWrite2(0x4, gDa5760Cfg1Data.m_Da1Volume);
        //Right channel volume control
        _MTK_AQ_AMP_RegWrite2(0x5, gDa5760Cfg1Data.m_Da1Volume);
        //Analog control
        _MTK_AQ_AMP_RegWrite2(0x6, gDa5760Cfg1Data.m_Da1AnalogControl);
        //Digital clipper2
        _MTK_AQ_AMP_RegWrite2(0x10, gDa5760Cfg1Data.m_Da1DigitalClip2);
        //Digital clipper1
        _MTK_AQ_AMP_RegWrite2(0x11, gDa5760Cfg1Data.m_Da1DigitalClip1);
    }
    else if(gAmpData[1].u1AmpTable == 1)
    {
        // Power control
        _MTK_AQ_AMP_RegWrite2(0x1,gDa5760Cfg2Data.m_Da2DigitalClip3 + (_MTK_AQ_AMP_RegRead2(0x1) & 0x3));
        // Digital control
        _MTK_AQ_AMP_RegWrite2(0x2,(gDa5760Cfg2Data.m_Da2DigitalControl & 0xF7) + (_MTK_AQ_AMP_RegRead2(0x2) & 0x08));
        //Left channel volume control
        _MTK_AQ_AMP_RegWrite2(0x4, gDa5760Cfg2Data.m_Da2Volume);
        //Right channel volume control
        _MTK_AQ_AMP_RegWrite2(0x5, gDa5760Cfg2Data.m_Da2Volume);
        //Analog control
        _MTK_AQ_AMP_RegWrite2(0x6, gDa5760Cfg2Data.m_Da2AnalogControl);
        //Digital clipper2
        _MTK_AQ_AMP_RegWrite2(0x10, gDa5760Cfg2Data.m_Da2DigitalClip2);
        //Digital clipper1
        _MTK_AQ_AMP_RegWrite2(0x11, gDa5760Cfg2Data.m_Da2DigitalClip1);
    }
}

#ifdef CC_AUD_SAKURA_RENDER_KDAMP
static void _KD_AQ_DATA_CPY(UINT8* data, UINT32 ref, UINT8 size)
{
    UINT32 i;
    UINT32 mask;
    UINT32 sf;
    
    
    mask = (0xFF << ((size - 1) * 8));
    sf = (size - 1) * 8;
    
    for(i=0; i<size; i++){
        
        data[i] = ((ref & mask) >> sf);
        mask = mask >> 8;
        sf = sf - 8;
    }
}
#endif
// ===============================================================
// [**CAUTION**] 
//  MUST provide this API with this naming "AUD_AMP_GetExtFunTbl."
// ===============================================================


AMP_T* AUD_AMP_GetAQCustFunTbl1(void)
{
    return &_rAmpMtk_AQ_AMP1;
}

AMP_T* AUD_AMP_GetAQCustFunTbl2(void)
{
    return &_rAmpMtk_AQ_AMP2;
}


#endif //CC_MTK_PRELOADER, CC_MTK_LOADER

/*
Debug Amp Init API
Index : index of gAmpData
*/
#ifdef CC_AUD_SAKURA_RENDER_KDAMP
void KD_AQ_AMP_INIT(void)
{
    UINT32 Index;
    
    for(Index=0; Index<AMP_MAX_NUM; Index++){
       if(gAmpData[Index].u1AmpConnect == 0)
       {
           return;
       }
       
       if(gAmpData[Index].u1DevAddrAQAmp == 0x34)/*for 5538 amp*/
       { 
           LOG(1, "Amp type is 5538\n");
           _KD_AQ_5538_Init(Index);
       }
       else
       {
           LOG(1, "Amp type is 5890\n");
           _KD_AQ_5760_Init(Index);
       }
    }

}

void KD_AQ_AMP_Refresh(void)
{
    UINT32 Index;
    UINT8 S_Data = 0;
    UINT8 readData;
    
    _KD_AQ_5538_Update_Data();
    
    for(Index=0; Index<AMP_MAX_NUM; Index++){
        
        if(gAmpData[Index].u1AmpConnect == 0)
        {
            return;
        }
        if(gAmpData[Index].u1DevAddrAQAmp == 0x34)/*for 5538 amp*/
        {
            LOG(1, "Amp type is 5538\n");
            _KD_AQ_Set_5538_Gen_Config(Index);
            _KD_AQ_Set_5538_BQ(Index);
            _KD_AQ_Set_5538_BassTreble(Index);
            _KD_AQ_Set_5538_DRC(Index);
            _KD_AQ_Set_5538_LOUD(Index);
        }
        else
        {
            LOG(1, "Amp type is 5760\n");
            if(gAmpData[Index].u1AmpTable == 0)
            {
                LOG(1, "5760 Data type is 0\n");
                // Power control
                if(_KD_AQ_AMP_RegRead(0x1, Index, &readData) != 0){
                    S_Data = (gDa5760Cfg1Data.m_Da1DigitalClip3 & 0xFC) + (readData & 0x3);
                _KD_AQ_AMP_RegWrite(0x1, &S_Data, Index, 1);
                }
                else{
                    LOG(0, "Amp Read Error! Skip RegWrite\n");
                }
                // Digital control
                if(_KD_AQ_AMP_RegRead(0x2, Index, &readData) != 0){
                    S_Data = (gDa5760Cfg1Data.m_Da1DigitalControl & 0xF7) + (readData & 0x08);
                _KD_AQ_AMP_RegWrite(0x2, &S_Data, Index, 1);
                }
                else{
                    LOG(0, "Amp Read Error! Skip RegWrite\n");
                }
                //Left channel volume control
                _KD_AQ_AMP_RegWrite(0x4, &gDa5760Cfg1Data.m_Da1Volume, Index, 1);
                //Right channel volume control
                _KD_AQ_AMP_RegWrite(0x5, &gDa5760Cfg1Data.m_Da1Volume, Index, 1);
                //Analog control
                _KD_AQ_AMP_RegWrite(0x6, &gDa5760Cfg1Data.m_Da1AnalogControl, Index, 1);
                //Digital clipper2
                _KD_AQ_AMP_RegWrite(0x10, &gDa5760Cfg1Data.m_Da1DigitalClip2, Index, 1);
                //Digital clipper1
                _KD_AQ_AMP_RegWrite(0x11, &gDa5760Cfg1Data.m_Da1DigitalClip1, Index, 1);
            }
            else if(gAmpData[Index].u1AmpTable == 1)
            {
                LOG(1, "5760 Data type is 1\n");
                // Power control
                if(_KD_AQ_AMP_RegRead(0x1, Index, &readData) != 0){
                    S_Data = (gDa5760Cfg2Data.m_Da2DigitalClip3 & 0xFC) + (readData & 0x3);
                _KD_AQ_AMP_RegWrite(0x1, &S_Data, Index, 1);
                }
                else{
                    LOG(0, "Amp Read Error! Skip RegWrite\n");
                }
                // Digital control
                if(_KD_AQ_AMP_RegRead(0x2, Index, &readData) != 0){
                    S_Data = (gDa5760Cfg2Data.m_Da2DigitalControl & 0xF7) + (readData & 0x08);
                _KD_AQ_AMP_RegWrite(0x2, &S_Data, Index, 1);
                }
                else{
                    LOG(0, "Amp Read Error! Skip RegWrite\n");
                }
                //Left channel volume control
                _KD_AQ_AMP_RegWrite(0x4, &gDa5760Cfg2Data.m_Da2Volume, Index, 1);
                //Right channel volume control
                _KD_AQ_AMP_RegWrite(0x5, &gDa5760Cfg2Data.m_Da2Volume, Index, 1);
                //Analog control
                _KD_AQ_AMP_RegWrite(0x6, &gDa5760Cfg2Data.m_Da2AnalogControl, Index, 1);
                //Digital clipper2
                _KD_AQ_AMP_RegWrite(0x10, &gDa5760Cfg2Data.m_Da2DigitalClip2, Index, 1);
                //Digital clipper1
                _KD_AQ_AMP_RegWrite(0x11, &gDa5760Cfg2Data.m_Da2DigitalClip1, Index, 1);
            }
        }
    }
}

void KD_AQ_AMP_5760_FS_Change(AQ_FS fs){
    
    UINT32 Index;
    UINT8 S_Data;
    
    for(Index=0; Index<AMP_MAX_NUM; Index++){
        if(gAmpData[Index].u1AmpConnect == 0){
            return;
        }
        if(gAmpData[Index].u1DevAddrAQAmp != 0x34){
            if(fs == AQ_FS_48KHZ){
                S_Data = (gDa5760Cfg2Data.m_Da2DigitalControl & 0xF7);
            }
            else{
                S_Data = (gDa5760Cfg2Data.m_Da2DigitalControl & 0xF7) + 0x08;
            }
            
            _KD_AQ_AMP_RegWrite(0x02, &S_Data, Index, 1);
        }
    }
}
    
void KD_AQ_AMP_5538_Mute(KD_AUD_AMP_MUTE_T mute){
    
    UINT32 Index;
    UINT8 S_Data = 0;
    
    for(Index=0; Index<AMP_MAX_NUM; Index++){
        if(gAmpData[Index].u1AmpConnect == 0){
            return;
        }
        if(gAmpData[Index].u1DevAddrAQAmp == 0x34){
            if( mute == KD_AUD_AMP_MUTE ){
                S_Data = 0xFF;
                _KD_AQ_AMP_RegWrite(0x0F, &S_Data, Index, 1);
            }
            else if( mute == KD_AUD_AMP_UNMUTE ){//Unmute
                S_Data = 0x0;
                _KD_AQ_AMP_RegWrite(0x0F, &S_Data, Index, 1);
            }
            else{
                LOG(0, "Nothing done. Pass Mute or Unmute value!\n");
            }
        }
    }
    
}
    
void KD_AQ_AMP_5760_Mute(KD_AUD_AMP_MUTE_T mute){

    UINT32 Index;
    UINT8 S_Data;
    
    for(Index=0; Index<AMP_MAX_NUM; Index++){
        if(gAmpData[Index].u1AmpConnect == 0){
            return;
        }
        if(gAmpData[Index].u1DevAddrAQAmp != 0x34){
            if( mute == KD_AUD_AMP_MUTE ){
                
                S_Data = (gDa5760Cfg1Data.m_Da1DigitalClip3 & 0xFC) + 0x03;
                _KD_AQ_AMP_RegWrite(0x1, &S_Data, Index, 1);
            }
            else if( mute == KD_AUD_AMP_UNMUTE ){//Unmute
                S_Data = (gDa5760Cfg1Data.m_Da1DigitalClip3 & 0xFC) + 0x01;
                _KD_AQ_AMP_RegWrite(0x1, &S_Data, Index, 1);
            }
            else{
                LOG(0, "Nothing done. Pass Mute or Unmute value!\n");
            }
        }
    }
}
#endif    
#endif
