/* 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: dtd_if.h,v $
 * $Revision: #1 $
 *
 *---------------------------------------------------------------------------*/

/** @file dtd_if.h
 *  This file list DTD primitive APIs.
 */

#ifndef DTD_IF_H
#define DTD_IF_H

//-----------------------------------------------------------------------------
// Include files
//-----------------------------------------------------------------------------
#include "fe_tuner_common_if.h"
#include "dtd_tuner_if.h"

//-----------------------------------------------------------------------------
// Constant definitions
//-----------------------------------------------------------------------------
#if 0
#define TUNER_FUNC_EN_MAX_SIZE    16
#define TUNER_PARA_MAX_SIZE       32
#endif

enum
{
   DEMOD_CONN_TYPE_UNKNOWN = 0,
   DEMOD_CONN_TYPE_SCAN,
   DEMOD_CONN_TYPE_SURF   /* Channel Change */
};

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

//-----------------------------------------------------------------------------
#if 0
/** tuner device driver RF tuner context.
 *  This data structure is used to store tuner device driver RF tuner context.
*/
typedef struct
{
    UINT8   u1TunerType;
    UINT8   I2cAddress;
    UINT8   u1IF_I2cAddr;
    UINT16  u2IF_Freq;
    UINT16  u2LO_Step;
    UINT16  u2Ana_LO_Step;
    UINT32  u4LO_Freq;
    UINT32  u4RF_Freq;
    UINT8   m_aucFunc[TUNER_FUNC_EN_MAX_SIZE];    
    UINT8   m_aucPara[TUNER_PARA_MAX_SIZE];    
// Access Link
    VOID    *psDemodCtx;
    VOID                    *psI2cCtx;
} RF_TUNER_CTX_T;
#endif


enum
{
    cAGC_RF_CTRL = 0,
    cAGC_IF_CTRL,
    cAGC_IF_VOL,
    cAGC_IF_PWR_DBM,
    cAGC_SIG_STRENGTH,
    cVCR_CFO,
    cAGC_INFO_SIZE
};

/***********************************************************************/
/** tuner device driver demod context.
 *  This data structure is used to store tuner device driver demod context.
 */

typedef struct _DEMOD_CTX_T
{
// Hardware Configure
    UINT8           u1ChipGen;
    UINT8           u1ChipRev;
    UINT8           I2cAddress;
    INT32           Frequency ;             /* carrier frequency (in Hz)   */
    UINT32          SymbolRate;             /* Symbol Rate (in Baud)       */
    UINT8           Modulation;             /* Modulation (8VSB, 64QAM, 256QAM) */
    UINT8           ConnectionType;
    INT32           PreFrequency;
// Status
    BOOL            fgLockSignal;
    BOOL            fgAgcLockStatus;        //zhishang add for maple 2014.4.22
    UINT8           ucLockStatus;
    UINT8           ucLockState;
    UINT8           ucSigCnt;
    UINT16          u2SigLeveldBm;
    BOOL            fgAcqStart;
    UINT16          u2AcqTime;
    UINT8           u1ReAcqCnt;
    UINT16          u2AccReAcqCnt;
    UINT8           u1StatusInd;
    UINT8           u1QSpecInv;
    BOOL            fgAutoQam;
    BOOL            fgMosaicUnLock;
//    float           SNR;
    UINT8           u1SNR;
    UINT16          u2SNRRaw;
    UINT16          u2SNRRaw_HighRes;
    UINT8           u1SNR_FS;
    UINT16          u2SNRRaw_FS;
//    FLOAT           fErrorRate;
    UINT16          u2ErrorRate;
    UINT16          u2PktErr;
    UINT16          u2PktCnt;
    UINT16          u2PrePktErr;
    UINT32          u4AccErrorRate;
    UINT32          u4AccPktErr;
    UINT32          u4AccPktCnt;
    //lei140701 	
    UINT32          u4UecPktErr;//Acc error packet
    UINT32          u4UecErrCnt;//unlock counter  
    //mtk40739 20131026 	
    UINT32	  u4PreTCMBitErr;	
    UINT32	  u4PostTCMBitErr;	  
    UINT32	  u4ErrorPkt;
	
    UINT8           u1ErrClass;
    UINT8           u1PERRaw1;
    UINT8           u1PERRaw2;
    UINT8           u1ErrRateIdx;
    UINT8           u1LossRateIdx;
    INT8            i1MosaicCnt;
    UINT8           u1CurTSIFCnt;
    BOOL            fgResetFlag;
    UINT8           ucConnFlag;
    BOOL            fgInitFlag;
    UINT8           u1PecFlag;
    //BOOL            fgFreqGapEn;
    UINT16          u2PecRefreshTime;
    UINT32          u4PecObserveTime;
    UINT8           u1PecErrSec;
//    UINT8           u1I2cNakCnt;
    UINT8           u1UpChkCnt;
    UINT8           u1UpNakCnt;
    UINT8           u1UpMisCnt;
    BOOL            fgStatusReady;
    BOOL            fgInfoReady;
    BOOL            fgPecReady;
    UINT8           u1TunerAcqType;
    UINT16          u2FwAcqTime;
    UINT16          u2FwAcqTime_PreLock;    // iven, 20110804
    BOOL            fgUserGetInfo;
    UINT8           u1TransPhase;
    UINT8           u1TunerType;
    
// Output Format Parameters
//    MPEG_FMT_T      sTS_Format;
// Access Link
    ITUNER_CTX_T*  sTunerCtx;
    void            *psI2cCtx;
    HANDLE_T          hHostCmdLock;      // Semaphore
    HANDLE_T          hRiscIfLock;       // Semaphore
    HANDLE_T          hRiscIfAtdLock;    // Semaphore  
    HANDLE_T          hLogLock;          
    BOOL            fgDemodInitFlag;
    HANDLE_T        hDemodAcq;           // Semaphore  
    UINT32          u4DrvCustTunerCtrl;
    UINT8           u1DrvCustTunerDefType;
        // sawless PRA state
    UINT8           ucSldPraState;
    INT16           s2SldPraRfAgc;  
    INT16           s2SldPraIfAgc;
    UINT8           ucSldPraGainIdx;
    INT16           s2SldPraOut;
    INT16           s2LegPraOut;
    
    UINT16          u2Rmetric;
    UINT16          u2Cmetric;
    UINT8           u1PilotLevel;
    UINT8           u1IIR_BW;
    UINT8           u1VprInfo;    
    // VSB GS related
    UINT8		uGS_FW_Cfg_1;
    UINT8		uGS_FW_Cfg_2;
    UINT8		uVCR_Dlp_Ind;
    UINT8		uVCR_Dlp_Cnt;
    // Iven, 20111013, Add for _MTFE_ATSC_GetVer
    UCHAR        ucFwVer[4];    
} DEMOD_CTX_T;


/***********************************************************************/
/*              Public Functions                                       */
/***********************************************************************/
//-------------------------------------------------------------------------
/** DTD_SetReg
 *  Set consecutive demod registers. The length is at most 4 bytes.
 *  @param  psDemodCtx  Pointer of demod device driver context.
 *  @param  u2RegAddr   16-bit register address.
 *  @param  *pu1Buffer  Pointer of data buffer values to be set.
 *  @param  u2ByteCount Length to be read. u2ByteCount <= 4.
 *  @retval 0: Fail, Others: Success.
 */
//-------------------------------------------------------------------------
UINT16 DTD_SetReg(DEMOD_CTX_T *psDemodCtx, UINT16 u2RegAddr, UINT8 *pu1Buffer, UINT16 u2ByteCount);

//-------------------------------------------------------------------------
/** DTD_GetReg
 *  Get consecutive demod registers. The length is at most 4 bytes.
 *  @param  psDemodCtx  Pointer of demod device driver context.
 *  @param  u2RegAddr   16-bit register address.
 *  @param  *pu1Buffer  Pointer of data buffer to be stored read values.
 *  @param  u2ByteCount Length to be set. u2ByteCount <= 4.
 *  @retval 0: Fail, Others: Success.
 */
//-------------------------------------------------------------------------
UINT16 DTD_GetReg(DEMOD_CTX_T *psDemodCtx, UINT16 u2RegAddr, UINT8 *pu1Buffer, UINT16 u2ByteCount);

/***********************************************************************/
/*              Interface APIs                                         */
/***********************************************************************/
//-------------------------------------------------------------------------
/** DTD_CheckTimeout
 *  Check if No-signal case is detected.
 *  @param  psDemodCtx  Pointer of demod device driver context.
 *  @retval TRUE    No-signal case is detected.
 *  @retval FALSE   Normal case.
 */
//-------------------------------------------------------------------------
UCHAR DTD_CheckTimeout(DEMOD_CTX_T *psDemodCtx);

//-------------------------------------------------------------------------
/** DTD_Connected
 *  Set Demod connection flag to be connected.
 *  @param  psDemodCtx  Pointer of DTD device driver context.
 *  @retval None
 */
//-------------------------------------------------------------------------
VOID DTD_Connected(DEMOD_CTX_T *psDemodCtx);

//-------------------------------------------------------------------------
/** DTD_AcqEnd
 *  Update some parameters which need to be updated when the acquisition is finished.
 *  @param  psDemodCtx  Pointer of demod device driver context.
 *  @retval None
 */
//-------------------------------------------------------------------------
VOID DTD_AcqEnd(DEMOD_CTX_T *psDemodCtx, BOOL fgBreak);

//-------------------------------------------------------------------------
/** DTD_DisConnect
 *  Stop acquisition and put Demod into sleep mode.
 *  This API should be invoked when the middleware disconnects the channel.
 *  @param  psDemodCtx  Pointer of demod device driver context.
 *  @retval None
 */
//-------------------------------------------------------------------------
VOID DTD_DisConnect(DEMOD_CTX_T *psDemodCtx);

//-------------------------------------------------------------------------
/** DTD_Initialize
 *  Initialize tuner device driver.
 *  @param  psDemodCtx  Pointer of demod device driver context.
 *  @param  u1I2cAddress    Demod I2C address. Default is 0x92
 *  @param  fgHardwareInit  Init demod hardware
 *  @retval 0           Success
 *  @retval -1          Fail
 */
//-------------------------------------------------------------------------
INT32 DTD_Initialize(DEMOD_CTX_T *psDemodCtx, UINT8 u1I2cAddress,BOOL fgHardwareInit);

//-------------------------------------------------------------------------
/** DTD_Close
 *  Cleanup system resource, and put demod into sleep mode
 *  @param  psDemodCtx  Pointer of demod device driver context.
 *  @retval 0           Success
 *          Others      System error
 */
//-------------------------------------------------------------------------
INT32 DTD_Close(DEMOD_CTX_T *psDemodCtx);

//-------------------------------------------------------------------------
/** DTD_Connect
 *  Start acquisition to specified frequency and modulation type
 *  @param  psDemodCtx  Pointer of demod device driver context.
 *  @param  u4Freq      RF center frequency in kHz
 *  @param  u1Modulation    Modulation type: MOD_ATSC_8VSB, MOD_J83B_64QAM & MOD_J83B_256QAM
 *  @param  u1ConnType      Connection type: DEMOD_CONN_TYPE_SCAN & DEMOD_CONN_TYPE_SURF
 *  @retval FALSE       Hardware error
 */
//-------------------------------------------------------------------------
//BOOL DTD_Connect(DEMOD_CTX_T *psDemodCtx, UINT32 u4Freq, UINT8 u1Modulation);
BOOL DTD_Connect(DEMOD_CTX_T *psDemodCtx, UINT32 u4Freq, UINT8 u1Modulation,UINT8 u1ConnType);

VOID DTD_ResetDemod(DEMOD_CTX_T *psDemodCtx);
//-------------------------------------------------------------------------
/** DTD_GetSync
 *  Get demod and tuner lock status
 *  @param  psDemodCtx  Pointer of demod device driver context.
 *  @retval TRUE        Locked
 */
//-------------------------------------------------------------------------
INT16 DTD_GetSync(DEMOD_CTX_T *psDemodCtx);

//-------------------------------------------------------------------------
/** DTD_SetSpcPolarity
 *  Set spectrum polarity.
 *  @param  psDemodCtx  Pointer of demod device driver context.
 *  @param  fgDebug       1 (Manual. For debug) / 0 (Auto. Normal flow use).
 *  @param  u1SpecInv   
 *  @retval void
 */
//-------------------------------------------------------------------------
VOID DTD_SetSpcPolarity(DEMOD_CTX_T *psDemodCtx, BOOL fgDebug, UINT8 u1SpecInv);

//-------------------------------------------------------------------------
/** DTD_GetTcmBER
 *  Set spectrum polarity.
 *  @param  psDemodCtx  Pointer of demod device driver context.
 *  @param  pu4TotalPkt        Received total packets.
 *  @param  pu4ErrPkt          Received error symbols. 
 *  @retval void
 */
//-------------------------------------------------------------------------
VOID DTD_GetTcmBER(DEMOD_CTX_T *psDemodCtx, UINT32 *pu4TotalPkt, UINT32 *pu4ErrSym);

//-------------------------------------------------------------------------
/** DTD_GetIfAgcVol
 *  Get IF AGC Voltage in mV.
 *  @param  psDemodCtx  Pointer of demod device driver context.
 *  @param  pu2IfAgcVol   IF AGC voltage in mV.
 *  @retval void
 */
//-------------------------------------------------------------------------
VOID DTD_GetIfAgcVol(DEMOD_CTX_T *psDemodCtx, UINT16 *pu2IfAgcVol);

//-------------------------------------------------------------------------
/** DTD_GetMod
 *  Get modulation type.
 *  @param  psDemodCtx  Pointer of demod device driver context.
 *  @param  peModType   Modulation type.
 *  @retval INT32
 */
//-------------------------------------------------------------------------
INT32 DTD_GetMod(DEMOD_CTX_T *psDemodCtx, UINT8 *peModType);

//-------------------------------------------------------------------------
/** DTD_SetIF
 *  Set If Ctrl Word
 *  @param  psDemodCtx  Pointer of demod device driver context.
 *  @param  u2If_Freq   Tuner IF Freqeucny(KHz)
 *  @retval VOID
 */
//-------------------------------------------------------------------------

VOID DTD_SetIF(DEMOD_CTX_T * psDemodCtx,UINT16 u2If_Freq);



// move the declaration from pi_demod.h, fixed io_mtfe_atsc.c build warning.(implicit declaration)
VOID DTD_UpdateInfo(DEMOD_CTX_T *psDemodCtx);

VOID DTD_AccumPER(DEMOD_CTX_T *psDemodCtx);

VOID *TunerGetCtx(UINT16 TunerID);

INT16 DTD_GetSignalLeveldBm(DEMOD_CTX_T *psDemodCtx);

VOID DTD_Get_RevPkt(DEMOD_CTX_T *psDemodCtx, UINT32  *pu4TotalPkt, UINT32  *pu4ErrPkt );

UINT16 DTD_GetSignalSNR(DEMOD_CTX_T *psDemodCtx);

VOID DTD_SetTSDirectOut(DEMOD_CTX_T *psDemodCtx, BOOL fgOut);

VOID DTD_SetTunerPara(DEMOD_CTX_T *psDemodCtx);

BOOL DTD_Get_AGCGain_CFO(DEMOD_CTX_T *psDemodCtx, INT32 *pi4Buffer, BOOL *fgIsCFO);

VOID DTD_TunerI2c(DEMOD_CTX_T *psDemodCtx, BOOL fgSwitchOn);

VOID DTD_Acquire(DEMOD_CTX_T *psDemodCtx, BOOL fgStart);

VOID vGetAsicVer(DEMOD_CTX_T *psDemodCtx);

VOID DTD_SetTsFmt(DEMOD_CTX_T *psDemodCtx, UINT8 u1MpegFmt);

BOOL DTD_GetTsFmt(DEMOD_CTX_T *psDemodCtx, UINT8 *u1MpegFmt);

VOID DTD_SetTsMode(DEMOD_CTX_T *psDemodCtx, BOOL fgParallel);

UINT32  DTD_GetPreTCMBER(DEMOD_CTX_T *psDemodCtx); //MTK07043,20141204, asliu add for sony api

#endif /* DTD_IF_H */

