/* 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: vdo_misc.h $
 *
 * Project:
 * --------
 *   MT8205
 *
 * Description:
 * ------------
 * Video Misc Function
 *
 * Author:
 * -------
 *
 *
 * Last changed:
 * -------------
 * $Author: chang.ren $
 *
 * $Modtime: 04/05/31 8:25p $
 *
 * $Revision: #2 $
*************************************************************************/

#ifndef _VDO_MISC_H_
#define _VDO_MISC_H_

#include "hw_sw.h"
#include "debug.h"
#include "feature.h"


#ifdef DRAM_DUMP_INOUT

typedef enum {
    DUMP_MOD_MIB = 0,
    DUMP_MOD_SCL,
    DUMP_MOD_MJC,
    DUMP_MOD_MAX,
}VDO_DUMP_MODULE;

#endif

#define MAX_FILE_LEN 20

#define MT8280 0
#define MT8280_E1 1
#define SCPIP_SUPPORT_POST_SCALER

//extern UINT8 bGetICVersion(void);
//extern UINT8 _bExtmjcICVersion;
#define REAL_TIME_FRAME_DELAY_EN 

typedef struct
{
    UINT32  u4Addr;
    UINT32  u4Fld;
    char    szName[32];
} VDO_DRAM_STATUS_T;

typedef struct
{
    UINT32  u4Addr;
    UINT32  u4Fld;
} VDO_DRAM_STATUS_CLR_T;

typedef enum {
	COLOR_NON_BT2020,
	COLOR_BT2020_YCC_CONST,
	COLOR_BT2020_YCC_NON_CONST,
	COLOR_BT2020_RGB_NON_CONST,
	COLOR_BT2020_TYPE_MAX,
}VDO_HDMIBT2020_TYPW;

typedef enum
{
    MIB_VS_IN_ISR_R,
    MIB_VS_IN_ISR_F,
    MIB_IN_ADJ_ISR,
    MIB_VS_OUT_ISR_R,
    MIB_VS_OUT_ISR_F,
    MIB_OUT_ADJ_ISR,
    SCALER_VS_OUT_ISR_R,
    SCALER_VS_OUT_ISR_F,
    OSTG_ISR,
    MEMC_VS_OUT_ISR_R,
    MEMC_VS_OUT_ISR_F,
    VDO_MLOOP,
    VDO_VRM_PREDOWN_TRIGGER,

    SCALER_VDE_OUT_ISR_R,
    SCALER_VDE_OUT_ISR_F,

    MEMC_VDE_OUT_ISR_R,
    MEMC_VDE_OUT_ISR_F,

    VDO_MEASURE_POINT_MAX
}VDO_TIME_STAMP_MEASURE_POINT_E;

typedef struct
{
    UINT64 u8Time;
    UINT32 u4Type;
    UINT32 u4Reserved1;
    UINT32 u4Reserved2;
    UINT32 u4Reserved3;
}VDO_TIME_STAMP_T;

typedef enum
{
    TBL_4K2K_HFR = 0,
    TBL_OTHER = 1,
    TBL_INVALID,
}VDO_TBL_IDX;

typedef enum
 {
    HDMI_TBL_4K2K_HFR_VRR_OFF= 0,
    HDMI_TBL_OTHER_VRR_OFF = 1,
    HDMI_TBL_4K2K_HFR_VRR_ON = 2,
    HDMI_TBL_OTHER_VRR_ON = 3,
    HDMI_TBL_MAX,
 }HDMI_TBL_NUM;

typedef enum
{
    SIG_MAIN_PQ_PATH = 0,
    SIG_BYPASS_PATH = 1,
    SIG_INVALID_PATH,
}VDO_SIG_PATH;

extern void vDrvVideoSwInit(void) ;
extern void vDrvVideoHwInit(void) ;
extern UINT8 bIsScalerInput444(UINT8 bPath) reentrant;
extern UINT8 bIsScalerInputRGB(UINT8 bPath) reentrant;


extern UINT8 bDrvVideoGetType(UINT8 bPath) ;
#ifdef CC_SUPPORT_PREPROC_TIMING
void vPreProcConnect(UINT8 u1Path, UINT8 fgIsOn);
#endif
#define VDOTYPE_NTSC 0
#define VDOTYPE_PAL 1
#define VDOTYPE_720P 2
#define VDOTYPE_1080I 3
#define VDOTYPE_OTHER 4

#define B2R_BASE_ADDRE                 (IO_VIRT + 0x26000)
#define B2R_SRCSEL                     (0x0064)
#define B2R_Color_Transf               (0x0068)
#define B2R_Color_Enable               (0x01)
#define B2R_Color_Disable              (0)
#define B2R_CT_EN                      Fld(1,31,AC_MSKB3)
#define VB1_PATH_SEL                   Fld(1,8,AC_MSKB1)//[8:8]
#define B2R_COLOR_TRANSFORM            (0x007C)
#define B2R_COLOR_INT_TABLE            Fld(4,28,AC_MSKB3)//[28:31]
#define IO_B2R_WRITE(RegName,FieldName,value) vIO32WriteFldAlign((B2R_BASE_ADDRE+(RegName)) ,(value) ,(FieldName))
#define BT601_2_RGB                     (0x06)
#define BT709_2_RGB                     (0x07)
#define JPEG_2_RGB                      (0x04)
extern void vSetVB1SrcType(BOOL fgPath);
extern void vSetVB1Color_Transf(BOOL fgEnable,UINT16 u2ColorMode);


//#define GAZELLE_2K2K_HYBRID  (IS_IC_5861() && (GetCurrentPanelIndex()== PANEL_VB1_V420DK1_KS1))

#ifndef CC_UP8032_ATV
extern void vDrvVideoAllocateDram(void) ;
#else
void vDrvVideoSRMChk(UINT8 bPath);
#endif
void vDrvNptvResume(void);
void vDrvVideoSysClockInit(void);

#ifdef CC_SUPPORT_STR
void vDrvNptvResume(void);
void vDrvVideoResume(void);
void vDrvVideoSuspend(void);
#endif

#ifdef CC_DTVDRV_RUNTIME_PM

UINT8 fgVdoPMIsPowerOnFlgSet(UINT32 u4Module);
void vVdoPMSetPowerOnFlg(UINT32 u4Module);
void vVdoPMClrPowerOnFlg(UINT32 u4Module);

void vDrvVideoOut_PM_Suspend(void);
void vDrvVideoOut_PM_Resume(void);
void vDrvVideoIn_PM_Suspend(void);
void vDrvVideoIn_PM_Resume(void);
#endif

// video call-back function
void vDrvVideoConnect(UINT8 bPath, UINT8 bOnOff) ;
void vDrvVideoTrigModeDetect(UINT8 bPath) ;
UINT16 wDrvVideoInputWidth(UINT8 bPath) ;
UINT16 wDrvVideoInputHeight(UINT8 bPath) ;
UINT8 bDrvVideoGetRefreshRate(UINT8 bPath)   ;
UINT32 bDrvVideoGetSRCFrameRate(UINT8 bPath);

UINT8 bDrvVideoIsSrcInterlace(UINT8 bPath)   ;
UINT16 wDrvMDDIGetVPorch(UINT8 bPath) ;
UINT16 wDrvVideoGetVPorch(UINT8 bPath) ;
UINT16 wDrvVideoGetHPorch(UINT8 bPath) ;
UINT8 bDrvVideoSignalStatus_DBL(UINT8 bPath);
UINT8 bDrvVideoSignalStatus(UINT8 bPath)   ;
UINT16 wDrvVideoGetHTotal(UINT8 bPath) ;
UINT16 wDrvVideoGetVTotal(UINT8 bPath) ;
UINT8 bDrvVideoGetTiming(UINT8 bPath) ;
const CHAR* strDrvVideoGetTimingString(UINT8 bTiming);
UINT8 bDrvVideoGetWidthOverSample(UINT8 bPath) ;

extern UINT8 bDrvVideoIsMainSrcTV(void);
extern UINT8 bDrvVideoGetSourceTypeTiming(UINT8 bPath) reentrant;
extern UINT16 wDrvVideoInputDEWidth(UINT8 bPath);
extern UINT16 bDrvVideoGetFrameDelay(UINT8 bPath);
extern UINT16 bDrvVideoGetFrameDelayReal(UINT8 bPath,UINT8 u1Real);


#ifdef CC_SUPPORT_DOLBY_VISION
extern INT16 bDrvVideoGetVideoFrameDelayCnt(UINT8 bPath);
extern INT16 bDrvVideoGetPwmDelayCnt(UINT8 bPath);
#endif


extern UINT8 bDrvPorchTune[2]; //porch tune mode
extern UINT8 bDrvGetPorchTune(UINT8 bPath);
extern void vDrvSetPorchTune(UINT8 bPath, UINT8 bModemsk);

//#define bDrvGetPorchTune(bPath)			bDrvPorchTune[bPath]
//#define vDrvSetPorchTune(bPath,bModemsk)	(bDrvPorchTune[bPath]=(bModemsk))

extern UINT16 wDrvVideoPorchStd(UINT8 bPath, UINT8 bPorchType);
extern UINT16 wDrvVideoGetPorch(UINT8 bPath, UINT8 bPorchType);
extern UINT16 wDrvVideoSetPorch(UINT8 bPath, UINT8 bPorchType, UINT16 wValue);

extern UINT16 wTvdGetPorch(UINT8 bPath, UINT8 bPorchType); /* bPath is for wDrvVideoPorchStd()*/
extern UINT16 wVgaGetPorch(UINT8 bPath, UINT8 bPorchType);
extern UINT16 wHdtvGetPorch(UINT8 bPath, UINT8 bPorchType);
extern UINT16 wDviGetPorch(UINT8 bPath, UINT8 bPorchType);
extern UINT8 u1DrvDIGetBob (UINT8 u1VdpId);
//extern void vVDOIN_MPCLK_Setting(void);
//extern void vTvdSetPorch(UINT8 bPath,UINT8 bPorchType, UINT16 wValue);
#ifndef NOT_SUPPORT_VGA
extern UINT16 wVgaGetPorch(UINT8 bPath, UINT8 bPorchType);
extern void vVgaSetPorch(UINT8 bPath,UINT8 bPorchType, UINT16 wValue);
#endif
extern void vHdtvSetPorch(UINT8 bPath,UINT8 bPorchType, UINT16 wValue);
extern void vDviSetPorch(UINT8 bPath,UINT8 bPorchType, UINT16 wValue);
extern void vDrvMainSubFifoReset(UINT8 bPath);
//if current signal is non-standard signal
extern UINT8 bDrvVideoIsNonStandardSignal(UINT8 bPath);
extern BOOL bDrvVideoIs2KSignal(UINT8 bPath);

#if 0
// DXX_HV_POS
UINT16 wTVDecGetHPos(UINT8 bPath);
UINT16 wTVDecAdjHPos(UINT8 bPath, UINT8 dir, UINT16 value);
UINT16 wTVDecGetVPos(UINT8 bPath);
UINT16 wTVDecAdjVPos(UINT8 bPath, UINT8 dir, UINT16 value);
INT16 wDrvAdjHPos(UINT8 bPath, UINT8 dir, UINT16 value);
INT16 wDrvAdjVPos(UINT8 bPath, UINT8 dir, UINT16 value);
#endif
//extern void vApiRedoTwoChipConnection(BOOL fgEnable, UINT8 u1Path);
extern UINT8 bDrvVideoGetWidthOverSample(UINT8 bPath);
extern UINT32 u4DrvVideoGetNSDOffset(UINT8 bPath);

 typedef struct 
{
	UINT32 u4Reg;
	UINT32 u4Value;
	UINT32 u4Mask;
} REGTBL_T;

#define REGTBL_END	0xFFFFFFFF

void vDrvLoadRegTbl(REGTBL_T const * pRegTbl) reentrant;

#ifdef  CC_SCPOS_PATTERN_GENERATOR
void vSetScposPatternGen(UINT8 u1Enable);
UINT8 u1GetScposPtGenEnable(void); 
#endif

UINT32 u43DGetOutputLR(void);
void vDrvVideoQueryInputTimingInfo(UINT32 u4Path);
UINT8 u1DrvVideoGetIs44PullDownInfo(void);
void vDrvVideoDramStatusRst(void);
void vDrvVideoDramStatusChk(void);
extern UINT32 u4VdoDramStatusMonCount;
extern void vDrvForceFrameRate(UINT8 u1FrameRate);
#if  CC_CHECK_DEC_STABLE
extern void vCheckDeocerStable(UINT8 bState) ;
#else
#define vCheckDeocerStable(x)   
#endif
void vSetHDMIColorMode(UINT8 bPath, UINT8 bMode);
UINT8 u1GetHDMIColorMode(void);
void vSetVDPColorMode(UINT8 bMode);
UINT8 vGetVDPColorMode(UINT8 bPath);
void vSetVGAColorMode(UINT8 bMode);
UINT8 u1GetVGAColorMode(void);

extern    void vShowOmuxStatus(UINT8 u1Path);
//PQ dump
extern void CopyScalerDRAM(void);
extern void vTriggerScalerDumpCopyDram(void);

//4k2k
BOOL fgIs4K60HzPanel(void);
BOOL fgIs4K2KInBypassPath(void);
BOOL fgIs8K4KInBypassPath(void);
BOOL fgIs4k2kNativeTimingOut(UINT8 bPath);
BOOL fgNeedRecalPanelFrameRate(void);
BOOL fgIs4k2kScalerBypass(UINT8 bPath);
BOOL fgIs4k2kPhoto3DSupport(UINT8 bPath);
//Gazelle dual hybrid mode
UINT8 bDrvVideoIsDecDualMode(void);
void vDrvVideoForceDualMode(UINT8 u1OnOff);

//Capy dual hybrid mode
UINT8 u1DrvVideoIsDec2PMode(UINT8 bPath);
UINT8 u1DrvIs4K2K120InBypassPath(UINT8 bPath);
UINT8 u1vDrvSetHdmiSignalPath(HDMI_TBL_NUM u4num,VDO_SIG_PATH u4path);
UINT32 u4DrvGetHdmiSignalPath(void);



//Simulate waveform data related function
void vDrvStartGetWaveFormData(void);
void vDrvStopGetWaveFormData(void);
void  vDrvSetTimeRawData(VDO_TIME_STAMP_MEASURE_POINT_E ePoint);
void vDrvGetTimeRawData(void);
#if defined(CC_DRAM_96BIT)
void vDrvUpdateVideoInfo(UINT8 bPath);
BOOL DrvVideoIs8KSignal(UINT8 bPath);
BOOL bDrvIsFrameBufferLessMode(void);
UINT16 DrvVideoInputSourceType(UINT8 bPath);
void vDrvGetHDMIInfo(UINT8 bPath);
void vDrvApplyVideoSetting(UINT8 bPath);
#endif

BOOL bDrvIs4KHFR(UINT8 u1VdpId);
BOOL bDrvIs4KHFRToPQPath(UINT8 u1VdpId);
UINT8 u1DrvIsVdpBypCase(void);
void vDrvPathSelDbg(UINT8 u1Mode, UINT8 u1Path);

void vDrvSetVDECSignalPath(VDO_TBL_IDX num,VDO_SIG_PATH path);
UINT32 u4DrvGetVDECSignalPath(VDO_TBL_IDX num);

BOOL bDrvIsHdmiVRRMode(void);

void bDrvBWSavingModeChk(void);

#endif
