/* 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: dtvbm11 $
 * $Date: 2015/11/10 $
 * $RCSfile: codec_mt8292.c,v $
 * $Revision: #1 $
 *
 *---------------------------------------------------------------------------*/

/** @file codec_WM8776.c
 *  Brief of file codec_WM8776.c.
 *  Details of file codec_WM8776.c (optional).
 */

#define AUD_EXTERN_FILE

//AMP_DRV_CUST
//=======================
// [**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 "x_typedef.h"
#include "sif_if.h"  // for i2c
#include "aud_if.h"
#include "drvcust_if.h"
#include "x_pinmux.h"
#include "mtk_aud_amp_82581.h"

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

//---------------------------------------------------------------------------
// Constant definitions
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Type definitions
//---------------------------------------------------------------------------

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

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

//---------------------------------------------------------------------------
// Imported functions
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
// Static function forward declarations
//---------------------------------------------------------------------------
static void _MTK_82581_WRITE(UINT8 u1Addr, UINT8 u1Data);
static UINT8 _MTK_82581_READ(UINT8 u1Addr);
static void _MTK_82581_Init(void);
static void _MTK_82581_Mute(BOOL fgEnable);
static void _MTK_82581_DisableEffect(void);

//---------------------------------------------------------------------------
// Static variables 
//---------------------------------------------------------------------------

static AMP_DRV_CB_T _rAmp_Mtk_82581DrvCB = 
{
    _MTK_82581_DisableEffect // pfnDisableEffect, disable sound effect   
};

static AMP_FUNCTBL_T _rAmpFunTbl =
{
    _MTK_82581_Init,          // pfnInit
    _MTK_82581_Mute,          // pfnMute 
    NULL,                     // pfnSetDacInFmt;
    NULL,                     // pfnSetDacGain;
    _MTK_82581_WRITE,         // pfnWrite;
    _MTK_82581_READ,          // pfnRead;
    &_rAmp_Mtk_82581DrvCB     // pfnDrvCB;
};

static AMP_T _rAmpMtk_82581 = 
{
    "AD82581",          // szName[32];
    256,                // i2MaxGain
    -256,               // i2MaxGain
    2,                  // u1DacNum
    &_rAmpFunTbl        // rCodecFuncTbl
};

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

// =================================================
// [**CAUTION**] 
// 1. Use printf, not LOG
// 2. You can only call public functions in xxx_if.c
//==================================================
static void _MTK_82581_WRITE(UINT8 u1Addr, UINT8 u1Data)
{
    UINT32 u4WriteCount = 0; 
    
    //u4WriteCount = SIF_Write(SIF_CLK_DIV, AD82581_DEV_ADDR, u1Addr, &u1Data, 1);
    u4WriteCount = SIF_X_Write(AUD_AMP_BUS_ID, SIF_CLK_DIV, AD82581_DEV_ADDR, 1 ,u1Addr, &u1Data, 1);

    if (u4WriteCount <= 0)
    {
        //LOG(0, "Error AD82581 register setting\n");
    }    
}

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

    //u4ReadCount = SIF_Read(SIF_CLK_DIV, AD82581_DEV_ADDR, u1Addr, &u1Data, 1);
    u4ReadCount = SIF_X_Read(AUD_AMP_BUS_ID, SIF_CLK_DIV, AD82581_DEV_ADDR, 1 , u1Addr, &u1Data, 1);    

    if (u4ReadCount > 0)
    {
        return u1Data;
    }    
    else
    {
        //LOG(5, "AD82581 READ FAIL\n");
        return FALSE;
    }    

}
static void _MTK_82581_Init(void)
{
    // Make sure I2c is init
    SIF_Init(); 

    //release A_MUTE and AMP_Enable to let AMP_PD# charge up
    //20100504 [DTV00080261] move to loader
    //A_MUTE pin   
    //ADAC_GpioCodecMute(FALSE);
    //AMP mute GPIO
    //ADAC_GpioAmpEnable(TRUE);
    //AMP_PD# pin charge up time needed
    //x_thread_delay(500);

    _MTK_82581_WRITE(0x2, 0x0);
}
static void _MTK_82581_Mute(BOOL fgEnable)
{
   
    if (fgEnable)
    {
        _MTK_82581_WRITE(REG_AD82581_02,MUTE);
        //LOG(8,"_AD82581_Mute.\n");
    }
    else
    {
        _MTK_82581_WRITE(REG_AD82581_02,UNMUTE);
        //LOG(8,"_AD82581_UN-Mute.\n");
    }
}

static void _MTK_82581_DisableEffect (void)
{
    // FIX ME!!!
    // Please add disable sound effect commands in this function.
    // Let the freq. response the same after amp.
}

//---------------------------------------------------------------------------
// Public functions
//---------------------------------------------------------------------------

// ===============================================================
// [**CAUTION**] 
//  MUST provide this API with this naming "AUD_AMP_GetExtFunTbl."
// ===============================================================
AMP_T* AUD_AMP_GetDrvCustFunTbl(void)
{
    return &_rAmpMtk_82581;
}

#endif //CC_MTK_PRELOADER, CC_MTK_LOADER


