/* 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: brianpc.huang $
 * $Date: 2015/05/29 $
 * $RCSfile: pi_dvbt_if.h,v $
 * $Revision: #1 $
 *
 *---------------------------------------------------------------------------*/

/** @file pi_dvbt_if.h
 *  DVB-T Demod pi primitive API.
 */

#ifndef PI_DVBT_IF_H
#define PI_DVBT_IF_H

//-----------------------------------------------------------------------------
// Include files
//-----------------------------------------------------------------------------
#include "x_typedef.h"
#include "u_handle.h"
#include "dvbt2_tuner_if.h"
#include "tuner_interface_if.h"
#include "fe_tuner_common_if.h"
//-----------------------------------------------------------------------------
// Configurations
//-----------------------------------------------------------------------------


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

enum
{
    DVBT_HIR_PRIORITY_UNKNOWN = 0,
    DVBT_HIR_PRIORITY_NONE,
    DVBT_HIR_PRIORITY_HIGH,
    DVBT_HIR_PRIORITY_LOW
};

enum
{
    DVBT_DEMOD_STATE_UNLOCK = 0,
    DVBT_DEMOD_STATE_LOCK,
    DVBT_DEMOD_STATE_UNKNOWN
};

typedef struct _DEMOD_CTX_T
{
// Hardware Configure
    UINT8           I2cAddress;
    INT32           Frequency ;             /* carrier frequency (in Hz)   */
    UINT8           fgEn24MClk;
    UINT8           ucEnFpga;
    UINT8           u1Bw;
    UCHAR           ucConnType;
// Status
    UINT8           u1Ver1;
    UINT8           u1Ver0;
    UINT8           u1Fft;
    UINT8           u1GI;
    UINT8           u1CR;
    UINT8           u1Mod;
    UINT8           u1Hier;
    UINT8           u1HpLp;
    UINT8           u1LpCR;
    UINT8           u1HpCR;
    UINT8           u1Indepth;
    INT16           s2Ifo;
    INT16           s2Ffo;
    INT32           s4Cfo;
    UINT8           u1Si;
    UINT8           m_TunerType;
    UCHAR           m_TunerMode; // momo, 061116, for silicon tuner
    UINT16          u2CellId;
    INT8            s1IfAgc;
    UINT8           u1ControlStatus;
    BOOL            fgAgcLock;
    UINT8           ucMdFftMode;
    UINT8           ucLastDemodStatus;
    BOOL            fgTpsLock;
    BOOL            fgTsLock;
    UINT8           u1Rssi;
    INT16           s2Snr;   // SNR*512
    UINT8           s2snr_cc; //SNR coefficient of correction,lei111014;
    UINT16          u2ErrorPkt;
    UINT16          u2TotalPkt;
    UINT32          u4CorrBit;
	//lei140701
    UINT32          u4UecPktErr;//Acc error packet
    UINT32          u4UecErrCnt;//unlock counter
    UINT16          u2VdTotBit;
    UINT16          u2VdErrBit;
    BOOL            fgDemodReset;
    UINT8           u1UpChkCnt;
    UINT8           u1UpNakCnt;
    UINT8           u1UpMisCnt;
    INT16           s2SldPraRfAgc;  //For MT5135 SLD. Hui @ 20100412.
    INT16           s2SldPraIfAgc;
    UINT16         u2RfAgcRssi;
    UINT8           ucSldPraGainIdx;
    INT16           s2SldPraOut;
    // RF AGC
    INT8            m_s1AciPow;
    INT8            m_s1InbPow;
    UINT8           ucTsIfFormat;
    UINT8           ucTsIfCtrl;
    UINT8           ucTsIfRate;

    UINT8           u1ChSearchMode;
    BOOL            fgSelectANIR;
    UINT8           u1MdMode;
    UINT8           u1TpsCr;
    UINT8           u1TpsCons;
    UINT8           u1TpsHier;
    INT8            s1FreqBias40khz;

    UINT8           u1TunerAcqType; // for SDAL, tune freq only or tune Demod only
    ITUNER_CTX_T*   psTunerCtx;

    //void            *psI2cCtx;
    HANDLE_T          hHostCmdLock;     // Semaphore
    HANDLE_T          hRegLock;         // Semaphore
    HANDLE_T          hLogLock;         // Semaphore
    BOOL              fgDemodInitFlag;
    HANDLE_T          hDemodAcq;        // Semaphore
    //THREAD_T        t_monitor_thread;
    BOOL            fgMonitorThreadRunning;
    INT32           i4BerTh;
    
    UINT16          u2PecRefreshTime;
    UINT32          u4PecObserveTime;
    UINT8           u1PecErrSec;    
} DEMOD_CTX_T;


UINT8 DVBT_ChipInit(DEMOD_CTX_T *psDemodCtx);
BOOL DVBT_FwSwitch(DEMOD_CTX_T *psDemodCtx);
void DVBT_UpdateInfo(DEMOD_CTX_T *psDemodCtx);
void DVBT_DriverStart(DEMOD_CTX_T *psDemodCtx, UINT8 u1Mon1Sel, UINT8 u1Mon2Sel);
void DVBT_DriverStop(DEMOD_CTX_T *psDemodCtx);
void DVBT_SetIF(DEMOD_CTX_T *psDemodCtx);
INT32 DVBT_Initialize(DEMOD_CTX_T *psDemodCtx, UINT8 ucI2cAddress, BOOL fgLoadCode);
UINT16 DVBT_GetIfAgcVol(DEMOD_CTX_T *psDemodCtx);
UINT8 DVBT_GetSignalLevel(DEMOD_CTX_T *psDemodCtx);
UINT16 DVBT_GetSignalSNR(DEMOD_CTX_T *psDemodCtx);
UINT32 DVBT_GetTotPkt(DEMOD_CTX_T *psDemodCtx);
UINT32 DVBT_GetErrPkt(DEMOD_CTX_T *psDemodCtx);
BOOL DVBT_GetTsLock(DEMOD_CTX_T *psDemodCtx);
INT32 DVBT_GetFreqOffset_kHz(DEMOD_CTX_T *psDemodCtx);
void DVBT_SetBW(DEMOD_CTX_T *psDemodCtx, UINT8 ucBW);
void DVBT_SetTsOutputMode(DEMOD_CTX_T *psDemodCtx, BOOL fgParallel);
void DVBT_SetHpLp(DEMOD_CTX_T *psDemodCtx, UCHAR ucHpLpSel);
INT32 DVBT_GetPreVBer(DEMOD_CTX_T *psDemodCtx);
INT32 DVBT_GetPostVBer(DEMOD_CTX_T *psDemodCtx);
UINT8 DVBT_SetNormalMode(DEMOD_CTX_T *psDemodCtx);
UINT8 DVBT_Connect(DEMOD_CTX_T *psDemodCtx, UINT8 ucBW);
VOID DVBT_DisConnect(DEMOD_CTX_T *psDemodCtx);
UINT8 DVBT_GetSync(DEMOD_CTX_T *psDemodCtx);
UINT8 DVBT_SetArCoef(DEMOD_CTX_T *psDemodCtx, UINT8 ucData);
UINT32 DVBT_GetSQI(DEMOD_CTX_T *psDemodCtx);
VOID DVBT_SetTsOutput(DEMOD_CTX_T *psDemodCtx, BOOL fgOutputEnable);
void DVBT_SetIfAgcCurve(DEMOD_CTX_T *psDemodCtx);
VOID DVBT_SetTsFormat(DEMOD_CTX_T *psDemodCtx, UCHAR u1TsFmt);
void DVBT_SetTsClkFreq(DEMOD_CTX_T *psDemodCtx, UCHAR ucManual);
void DVBT_Set_R_Setting(DEMOD_CTX_T *psDemodCtx);
UCHAR DVBT_GetTsFormat(DEMOD_CTX_T *psDemodCtx);
INT32 DVBT_Initialize(DEMOD_CTX_T *psDemodCtx, UINT8 ucI2cAddress, BOOL fgLoadCode);
void   DVBT_DemodCtxDestroy(DEMOD_CTX_T *p);



#endif /* PI_DVBT_IF_H */

