/* 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.
 *
 *****************************************************************************/

 /*************************************************************************
 *
 * Filename:
 * ---------
 *   $Workfile: drv_tvd.h $
 *
 * Project: MT5381
 * --------
 *
 *
 * Description:
 * ------------
 *    TVD status macro
 *
 * Author:
 * -------
 *
 *
 * Last changed:
 * -------------
 * $Author: brianpc.huang $
 *
 * $Modtime: 04/05/31 9:52p $
 *
 * $Revision: #1 $
*************************************************************************/
/**
* @file drv_tvd.h
*/
#ifndef _DRV_TVD_H_
#define _DRV_TVD_H_


/**************************************************************************
 * Header Files
 *************************************************************************/
#include "hw_tvd.h"
#include "typedef.h"
/**************************************************************************
 * Configure Definitions
 *************************************************************************/

#define TVD_NR_AVG                1
#define TVD_CLI_SUPPORT           1
#define TVD_AUTO_CALIB            1
#define TVD_FRS_FOR_NONSTDSIGNAL  1
#define TVD_CHECK_BURST  1
#define TVD_VPRES_STATE     1
#define TVD_VCR_BV 1

#define ENABLE_PRBS_BY_DRIVER 0
#ifdef CC_TVD_SUPPORT_DDI
#define TVD_SET_ENABLED_CS 1
#define TVD_NEW_VPORCH 0
#define TVD_SERRCNT_WA 1
#else
#define TVD_SET_ENABLED_CS 0
#define TVD_NEW_VPORCH 1
#define TVD_SERRCNT_WA 0
#endif

#ifdef CC_SUPPORT_ATV_DESCRAMBLE
#define TVD_MANUAL_TUNER_SCAN 1
#else
#define TVD_MANUAL_TUNER_SCAN 0
#endif


/**************************************************************************
 * Type definitions & Enum
 *************************************************************************/
/** Function prototype for CLI cmd. */
typedef void (*TVD_PFN_CLI_NFY) (void *pInput);

typedef struct _RTvdLineInfo
{
	UINT16 wLinecnt;
	UINT16 wERR_VLEN;
	UINT16 wLOCK_VLEN;
    #if TVD_FRS_FOR_NONSTDSIGNAL
	UINT16 wAvg_VLen;
    #endif
}RTvdLineInfo;
typedef struct _RTvdNAInfo
{
 UINT8  na_HysterCnt; // For hystersis of NA calculation.
 UINT8  na_HysterCntThres;
 UINT8  na_cnt;
UINT16 na_sum;
}RTvdNAInfo;


typedef struct RTvdStatus
{
	UINT8 eSourceType;
	UINT8 fgIs525;
	UINT8 fgIsPhaseAlt;
	UINT8 fgVPres;
	UINT8 fgIsVCR;
	UINT8 bTvdMode;
	UINT8 bSigStatus;
	UINT8 fgTrick;
	UINT8 bColSys;
	UINT8 fgSVPres;
	UINT8 fgIsMV;
    UINT8 bCAGC;
    UINT8 bAgainCode;
	#if TVD_VCR_BV
	UINT8 fgIsVCRBV;
	#endif
    BOOL  fgIsBurstLock; 
	BOOL  fgIsCkill;
	BOOL  fgIsPSync;
	BOOL  fgIsCoChannel;
	BOOL  fgCNRLow;
	BOOL  fgIs443nPHALT;
	BOOL  fgHLOCK;
	BOOL  fgVLOCK;
	BOOL  fgHSYNC_LOCK;
	INT16 wLine_err;
    RTvdLineInfo LineInfo;
    RTvdNAInfo  NAInfo;
    UINT32 u4LLockStatus;
    UINT32 u4Y, u4U, u4V;
} RTvdStatus;

typedef struct RTvdAGCInfo
{
    #ifndef CC_UP8032_ATV
    BOOL bmanualAGC;
    #else
    UINT8 bmanualAGC;
    #endif
    UINT8 minHWidth;
    UINT8 maxHWidth;
    UINT8 thHigh;
    UINT8 thSuper;
} RTvdAGCInfo;

typedef struct 
{
    UINT8 MCAGC_EN;
    UINT8 MCAGC;
    UINT32 Bound;
    UINT8 L_Step;
    UINT8 S_Step;
} RTvdCAGCInfo;

typedef enum _TVD_AGC_T
{
    E_HOLD_GAIN     = 0x1,  /// Hold Current AGain & DGain 
    E_MENU_GAIN     = 0x6,  /// Menu DGain & AGain
    E_AUTO_GAIN     = 0x8,  /// Auto DGain & AGain 
    E_MENU_DGAIN    = 0xA,  /// Menu DGain    
    E_MENU_AGAIN    = 0xC,  /// Menu AGain
    E_DFT_MENU_GAIN = 0xE   /// Menu DGain & AGain with default gain code when VPRES OFF
} TVD_AGC_T;

typedef struct RTvdNSTDStatus
{
	UINT8 fgIsNSTD;
	UINT8 bRefreshRate;
	UINT16 wVTotal;
	UINT16 wHTotal;
	INT16 wVTotalDiff;
} RTvdNSTDStatus;

enum TVDCTL_ITEM{
     TCTL_TVD_RESET=0,
     TCTL_RESET_DEFAULT,
     TCTL_RECONNECT,
     TCTL_INPUTTYPE,
     TCTL_COUNTRY,
     TCTL_DFTMODE,
     TCTL_MDSTBPROT,
     TCTL_NTSCONLY,
     TCTL_COLORSYSTEM,
     TCTL_NOISELVL,
     TCTL_VCR,
     TCTL_COCH,
     TCTL_CKILL,
     TCTL_SERRTYPE,
     TCTL_SERRCNT,
     TCTL_ACODE,
     TCTL_DCODE,
     TCTL_CAGC,
     TCTL_BP_ISR,
     TCTL_BP_MODECHG,
     TCTL_BP_MLOOP,
     TCTL_BP_TC_INIT,
     TCTL_REG_SAVE_RESTORE,
     TCTL_AGCPEDEN,
     TCTL_MCNT,
     TCTL_MAX
};

enum TVDCTL_CMD{
	TC_DISABLE=0,	/* ctrl disable, reset val to 0 */
	TC_ENABLE,	    /* ctrl enable, set val by arg */
	TC_APPLY,	    /* TC_GETEN? call TC_ENABLE : call TC_DISABLE  */
	TC_GETEN,	    /* check ctrl enable */
	TC_GETVAL,	    /* get ctrl value */
	/* speical flags*/
	TC_INIT=0x80	/* for initial*/
};

enum TVDFT_ITEM{
	TVDFT_ALL=0,	/* apply to all finetune items*/
	TVDFT_CHROMA_SAT,
};
enum TVDFT_CMD{
	/* general finetune command*/
	FTCMD_RESET=0,
	FTCMD_GO,
	FTCMD_HOLD,
	/* item related command */
	FTCMD_00=16,
	FTCMD_01,
	FTCMD_02,
	FTCMD_03,
	FTCMD_04,
	FTCMD_05,
	FTCMD_06,
	FTCMD_07,
};

#if TVD_CHECK_BURST
enum TVD_BLOCK{
    TVD_BLOCK_IS443=0,
    TVD_BLOCK_IS358,
    TVD_BLOCK_UNKNOW,
};
#endif

#if TVD_SET_ENABLED_CS
typedef struct _RTvdEnabledCS_T
{
    UINT8 u1TvdEnPALN;
    UINT8 u1TvdEnPAL;
    UINT8 u1TvdEnPALM;
    UINT8 u1TvdEnNTSC358;
    UINT8 u1TvdEnSECAM;
    UINT8 u1TvdEnPAL60;
    UINT8 u1TvdEnNTSC443;
} RTvdEnabledCS_T;
#endif


/**************************************************************************
 * Export variables
 *************************************************************************/
// TVD Status
extern RTvdAGCInfo    _rTvd3dAGCInfo;
extern RTvdCAGCInfo   _rTvd3dCAGCInfo;

extern RTvdStatus   _rTvd3dStatus;
extern UINT8        _bTvdHoldMode;
extern UINT8        _na_state;
extern UINT8        _bTvdISRCnt;
extern BOOL         _fgHLOCK;
extern UINT8        _bSetChannelCnt; //Counter for tuner, set 60 when change channel number rock 20060324
extern UINT8        bhwVCRSetting;

#if TVD_COCH_FLICKER
extern UINT8 bIsBlankVarLarge;
#endif

#if TVD_AUTO_CALIB
extern UINT8 _bTVDAutoCalibState;
#endif
extern BOOL    _fgDescramblerOn;
extern BOOL    _fgAutoSearch;
extern UINT16  wTVDNRavg;
extern UINT8 _bAgcPedEn;

/**************************************************************************
 * Export Functions
 *************************************************************************/
extern void vTvd3dSetChanelChange(UINT8 bMode);
extern UINT8 vTvd3dGetChanelChange(void);
extern void wDrvReadADCBlankAveSlt(void);
extern void vApiSetForDescrambler(BOOL fgDescramOn);
extern UINT16 wTvdCCI(void);
extern BOOL fgHwTvdVPres(void);
extern UINT8 bTvdCtrl(UINT8 bItem,UINT8 bCmd,UINT32 bArg) reentrant; 
extern void vTvdSetAGCMode(TVD_AGC_T eAgcType) reentrant;
extern void vDrvColorSystem(UINT8 bTVD, UINT8 bColSys) ;
extern void vTvd3dReset(void) ;
extern BOOL fgIs525Mode(UINT8 bMode) ;
extern void vTvdResetChromaLock(void);
extern void vTvdHoldMode(UINT8 bHoldOnOff);
extern void vTvd3dChannelChange(void);

extern void vTvd3dFastChannelChange(UINT8 bOnOff);
extern void vTvd3dSVInit(UINT8 bSVOnOff);
extern void vTvd3dSetYCDelay(UINT8 bYCDelay);
extern void vTvd3dSetYCDelaybyAP(UINT8 bYDelay, UINT8 bCDelay);
extern void vTVDCAGCTARGET(INT8 bOffset);
#if TVD_AUTO_CALIB
extern BOOL bDrvTVDAdcCalib(void);
extern void vDrvTVDAutoCalibStart(void);
extern BOOL bDrvTVDAdcCalib4Auto(void);
extern void vDrvTVDAutoCalibState(void);
extern UINT8 wDrvGetTVDADCMAX(void);
extern void  vCust_TVD_ADC_MAX_Update(UINT8 value);
extern void vCust_TVD_Gain_Update(UINT8 value);
extern UINT16 bDrvGetTVDADCOffset(void);
extern void vDrvSetTVDADCOffset(UINT16 value);
#endif
extern void vDrvSetTVDADCGain(UINT8 value);
extern void vDrvSetTVDADCDGain(UINT16 value);
extern void vTvd3dSetCAGCbyAP(void);
extern void vTVDAVDelay(UINT8 bDelay);        // For CLI
extern void vTVDPVDelay(UINT8 bDelay);		  // For CLI
//extern void vTvd3dSetCAGCBound(UINT32 dwBound);
extern void vTvd3dSetCAGCADAP(UINT8 bOnOff);
extern void vDrvTvd3dVPresOnOff(UINT8 bGain, UINT8 bThrs) reentrant;
extern void vMainCKill(UINT8 bOnOff);
extern void vSubCKill(UINT8 bOnOff);
extern void vDrvWhitePeakInit(void);
extern void vDrvTvd3dSetChromaPGain(UINT8 bGain);
extern void vDrvTvd3dMCKill(UINT8 bOnOff);
extern void vTvd3dSetBUNLOCKThres(UINT16 wThres2, UINT16 wThres1);
extern void vSVPresDet(void);
extern void vDrvSetSVFBypass(UINT8 bOnOff);
extern void vTvd3dATDInit(UINT8 bATDOnOff);
extern void vDrvSetTVDGain(UINT16 value);
extern void vDrvSetTVDOffset(UINT16 value);
extern void vTvdForceTvdManualVPres(UINT8 bMVPresOnOff);
extern void vDrvTvd3dSetVPresOnOffGain(UINT8 bOffGain, UINT8 bOnGain, UINT8 bThrs);
extern void vDrvTvd3dSetSyncDetForTuning(BOOL fgLowSens);
extern void vDrvTvd3dSetHPLL(UINT8 u1KPGain, UINT8 u1KIGain, UINT32 u4ColSys);
// Function Pointer
extern void vTvd3dConnect(UINT8 bPath, UINT8 bOnOff) ;
extern void vTvd3dTrigModeDet(void) ;
extern UINT16 wTvdWidth(void) ;
extern UINT16 wTvd3dHeight(void) ;
extern void vPreTvdWidthHeight(UINT16 *pu2Width,UINT16 *pu2Height);
extern UINT8 bTvd3dRefreshRate(void) ;
extern UINT8 bTvd3dSignalStatus(void) ;
extern void vTvd3dVSyncISR(void) ;
extern UINT8 bTvdLumaSignalRefreshRate(void) ;
extern void vDrvTvd3dDClmpOn(void) ;
extern void vDrvTvdSetDftType(UINT8 fgIsPAL); 
extern void vDrvWhitePeakLimit(UINT8 bOnOff);
extern UINT8 bTvdNRLevel(void);
extern UINT8 bTvdSignalStrengthLevel(UINT8 bMode);
extern void vTvd3dSetAAFbyAP(UINT8 bOnOff, UINT8 bValue);
extern void vTvd3dMainLoop(void);
extern void vTvd3dBHModeChg(void);
extern void vTvd3dBHModeDone(void);
extern void vTvd3dGetUIDelay(void);
extern UINT8 bTvd3dGetColorSystem(void);
extern void vDrvTvd3dSetColorSystem(UINT8 bColSys);
extern UINT8 vDrvTvd3dSetEnabledColorSystem(UINT32 u4ColSys);
#if TVD_SET_ENABLED_CS
extern void vDrvTvd3dCheckMModeForEnabledCS(void);
#endif
extern void vDrvTvdQueryStatus(void);
extern UINT8 bDrvGetTVDADCGain(void);
extern void vDrvSetTVDADCGain(UINT8 value);
extern void vTvdSetOverSampleEnable(UINT8 bmode);
extern UINT8 vTvdGetOverSampleEnable(void);
extern void vCust_TVD_Offset_Update(UINT16 value);
extern void vTvdFineTune(UINT8  bItem, UINT8 bCmd, UINT32 dArg);
extern BOOL fgDrvTvdCheckTVDLock( UINT16 bTryLoopCnt );
extern BOOL fgDrvTvdCheckVPresOn(void);
extern void vDrvTvdSetChannelChange(UINT8 bOnOff);
extern BOOL fgDrvTvdCheckVideoStable(void);
extern UINT8 bTvdNAState4Step(void);
extern UINT16 wTvdHTotal(void);
extern UINT16 wTvdVTotal(void);
extern void vTvd3dGetNSTDStatus(RTvdNSTDStatus* pTvdNSTDStatus);
extern void vTvdSetModeDetTime(UINT8 bMDCnt);
extern UINT8 bTvdGetModeDetTime(void);
extern void vTvdSetPorch(UINT8 bPorchType, UINT16 wValue);
extern void  _svSnowModeSet(BOOL fgSnowMode, UINT8 bSnowIntensity);
extern UINT16 wTvdGetPorch(UINT8 bPath,UINT8 bPorchType);
extern UINT16 wTvdGetDefaultPorch(UINT8 bPath,UINT8 bPorchType);
extern void vTvd3dInit(void);
extern void vTvd3dSetAAF(UINT8 bValue);
extern void vTvdSetSFSetting(UINT32 u4value);
extern BOOL vTvd3dGetAnalogCopyProtectPSync(void);
extern UINT8 vTvd3dGetAnalogCopyProtectCStripe(void);
#if 1//defined(CC_S_PLATFORM)&&defined(CC_FRONTEND_MULTIPATH)
extern UINT16 wDrvGetTVDMaxYLvl(void);
extern UINT16 wDrvGetTVDDgain(void);
#endif

#if TVD_NR_AVG
extern void vTvdMeasureNR(BOOL  fgEnable );
extern UINT8 vTvdGetNRLevel(void);
#endif

#if TVD_MANUAL_TUNER_SCAN
extern void vDrvTvdGetScanadjInfo(UINT8 * u1VpresGain, UINT8 * u1VlockRatio);
extern void vDrvTvdSetScanadjInfo(UINT8 u1VpresGain, UINT8 u1VlockRatio);
#endif

#ifdef CC_SUPPORT_RECORD_AV
BOOL bTvd3dIsConnected(void);
#endif

#if TVD_CLI_SUPPORT
extern void vTvdSetCLINfy(TVD_PFN_CLI_NFY pfnCliNfy);
extern void vTvd3dSetFixAgc(UINT8 bOnOff);
extern void vTVD3dAGCPedEn(UINT8 bOnOff);       
extern void vTvd3dSetPatGen(UINT8 bOnOff);
extern void vTvd3dSetHNChChgMCntTh(UINT8 bTh);
extern void vTvd3dSetPhaltWaOnOff(UINT8 bEnable);
#endif
#if TVD_CHECK_BURST
extern UINT8 bDrvTvdCheckBurstFreq(void);
#endif
extern UINT16 wDrvGetTVDYLvl(void);
#ifdef  SUPPORT_AV_COMP_AUTO
extern void vAVCompAutoDet(void);
#endif
#endif
