/* 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$
 * $Date$
 * $RCSfile: drv_display.c,v $
 * $Revision$
 *
 *---------------------------------------------------------------------------*/

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


//-----------------------------------------------------------------------------
// Include files
//-----------------------------------------------------------------------------
#ifdef CC_UP8032_ATV
#include "general_mt82.h"
#endif
#include "general.h"
#include "panel.h"
#include "panel_cust.h"
#include "hw_vdoin.h"
#include "drv_vdoclk.h"
#include "util.h"
#include "hw_scpos.h"
#include "hw_mlvds.h"
#include "hw_tcon.h"
#include "hw_tg.h"
#include "hw_ycproc.h"
#include "hw_ospe.h"
#include "hw_ckgen.h"
#include "hw_tdsharp.h"
#include "hw_srs_int.h"
#include "hw_mjc.h"
#include "hw_psc.h"
#include "drv_scpos.h"
#include "hw_ckgen.h"
#include "drv_display.h"
#include "drv_scaler.h"
#include "drv_scaler_ref.h"
#include "drv_ycproc.h"
#include "drv_lvds.h"
#include "drv_di.h"
#include "vdo_misc.h"
#include "drv_hdtv.h"
#include "video_def.h"
#include "drv_video.h"
#include "drv_tcon.h"
#include "vdp_display.h"
#include "source_select.h"
#include "drv_lcdim_v2.h"
#include "hw_lcdim_v2.h"
#include "hw_sw.h"
#include "nptv_debug.h"
#include "api_backlt.h"
#include "mute_if.h"
#include "srm_drvif.h"
#include "drv_ldmspi.h"
#include "hw_lc_bitplus.h"
#ifndef CC_COPLAT_MT82
#include "osd_drvif.h"
#include "pmx_drvif.h"
#endif
#include "x_assert.h"
#include "x_lint.h"
#include "x_pinmux.h"
#include "c_model.h"
#include "x_timer.h"
#include "ostg_if.h"
#include "drv_od.h"
#ifdef THE_3RD_PARTY_HEADER_FILE_1
#include THE_3RD_PARTY_HEADER_FILE_1
#endif
#ifdef CC_DRV_SUPPORT_SMICRO
#include "SMicro/SMicro_if.h"
#endif
#include "x_gpio.h"
#ifndef CC_MTK_LOADER
//#include "drv_mjc.h"
#include "drv_mjc_if.h"
//#include "hw_mjc.h"
#endif

#include "drv_tdtv.h"
#include "drv_vrr.h"

#include "drv_pwm.h"
#include "drv_vdoclk.h"
#include "drv_scaler_drvif.h"
#include "frametrack_drvif.h"
#ifdef DRV_SUPPORT_EXTMJC
#include "extmjc_if.h"
#endif

#include "eeprom_if.h"
#include "osd_drvif.h"
#include "drv_scaler_psc.h"
#ifdef CC_SUPPORT_4K2K
#include "drv_mpeg.h"
#include "drv_dvi2.h"
#endif
#include "drv_vbyone.h"
#include "b2r_if.h"
#include "pe_if.h"
#include "tve_if.h"
#include "drvcust_if.h"
#ifdef CC_MAPLE_CUST_DRV
#include "../../../3rd_party/linux/maple/d_inc/CustomCtrl.h"
#endif

#ifndef CC_MTK_LOADER
#include "osd_exp.h"
#endif

#if defined(CC_PANEL_FROM_FLASH) && defined(CC_S_PLATFORM)
EXTERN UINT8 FlashPanel_GetPanelAttr(void);
extern UINT32 FlashPanel_GetControlWord_2SecEn(void);
extern UINT32 FlashPanel_GetControlWord_4SecEn(void);
extern UINT32 FlashPanel_GetControlWord_8SecEn(void);
extern UINT32 FlashPanel_GetControlWord2_GraphModeEn(void);
extern UINT32 DRVCUST_Get_InterMemc_type(void);
#endif
#ifdef CC_MAPLE_CUST_DRV
void u1DrvMruaCUCInit(void);
#endif

#ifdef CC_LCE_SUPPORT
#ifndef CC_MTK_LOADER
extern UINT8 u1LCESWInitialized;
extern void vDrvLCEInit(void);
#endif
#endif

#if 0  // Capy remove
extern void vVByOneLaneSwapForHk(void);  // Only for Husky(5893)
#endif

#ifdef CC_MTK_LOADER
INT32 VDP_LogChkMsk(UCHAR ucVdpId, VDP_LOG_DBG_MSK_T u4Mask)
{
    //only for loader stage build pass.
    UNUSED(ucVdpId);
    UNUSED(u4Mask);
    return 0;
}

#endif

//luis@2007/7/5, for vIO32WriteFldXXX
/*lint -save -e666*/

//-----------------------------------------------------------------------------
// Constant definitions
//-----------------------------------------------------------------------------
#if defined(CC_S_PLATFORM)
#define CUST_SCANPWM_MAX_CH 4
#define CUST_SCANPWM_FIRST_CH 3
#endif

#define CC_BACKPORCH_MIN (99 + 15)
#define CC_FRONTPORCH_MIN (10)
#define CC_BACKPORCH_MAX (2047)
#define CC_FRONTPORCH_MAX (30)
#define OSTG_UPDATE_PIXEL_SHIFT_H 1
#define OSTG_UPDATE_PIXEL_SHIFT_V 2

#define WOBBLE_MAX	16
#define WOBBLE_MIN	-16



// force the HTotal to be 4x-aligned in Dispmode + Dual or Four port panel
#define CC_HTOTAL_4X_LIMIT_WA

// when VOUT_H is smaller than HTotal, VSync will be lost.
#define CC_OSTG_WA1

// 3D_LR/3D_EN setting for the turnkey board
//#define TURNKEY_3D_CTRL_SETTING

#define SYSPLL_CLK_SS 648000000

#if CC_OSD_MUTE_WA
UINT8 _bDddsDelayFlag = 0 ;
extern void vVdoSendEvevntToSRM(UINT8 bPath);
#endif

#ifdef CC_FLIP_MIRROR_SUPPORT
static UINT8 u1Init = 0 ;
static UINT8 u1FlipMirrorEnable= 0 ;

#ifdef CC_FLIP_MIRROR_FROM_EEPROM
static UINT8 uAcOn1Init = 0;
static UINT8 u1FlipConfig = SV_OFF;
static UINT8 u1MirrorConfig = SV_OFF;
#endif

#endif



UINT32 _u4NSDHTotal=0xff;
UINT32 _u4NSDVTotal=0xff;
UINT32 _u4NSDFrameRate=0xff;
UINT8 u1SRSDynSwitchEn  = 0;
UINT8 u1CroppingCnt = 32;
UINT8 u1CroppingEna = SV_OFF;
BOOL bfgCroppingChg=SV_FALSE;

BOOL fgPhotoRasterModeIn4k= SV_FALSE;//4k photo in video path

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

#ifdef CC_LOAD_UBOOT
UINT32 _u4VPllFrequency;
#else
extern UINT32 _u4VPllFrequency;
#endif

UINT8 _u1StepLimitLevel=0;

//-----------------------------------------------------------------------------
// Public variables
//-----------------------------------------------------------------------------
//Give initial value instead of using "CC_DVBT_SUPPORT" define
UINT8 bSI_DISPLAY_DCLK_TYPE = SV_DCLK_UNKNOW;
UINT8 bOUTPUT_REFRESH_RATE = 0;
UINT8 bFORCE_FRAMERATE_TYPE  = ~0;

UINT8 bDISPLAY_INTERLACE_ENABLE;
BOOL _fgFRS_ENA;
BOOL fgDDDSEn = SV_OFF;
enum eSyncLockMode _eSyncLockMode = eSYNCLOCK_IGNORED;

UINT8 u1CustFRC = 0;

#if !defined(CC_S_PLATFORM)
extern UINT32 _u4BrightLevel;
#endif

static UINT32 _u4OstgUpdateEvent = 0;

static UINT16 _u2IntFrameRate = 0;

UINT8 _bForceFrameRate = 0;
UINT8 _bForceFixMode = 0;
UINT8 fgDDDSInit=FALSE;
BOOL _fgFrTrkHalfStep = FALSE;
UINT8 _uDDDSDivision = 0;

EXTERN UINT8 bSupportLocalDimming;

UINT16 w3DGlassesDelay = 0;

UINT8 fgPentouchMode = SV_OFF;
UINT8 fgApplyScalerSetData=0;
BOOL fgWaitRefSetData=SV_FALSE;

UINT8 _u12SecWaitCnt=0;
BOOL  _fg2SecTimingWait=SV_FALSE;

UINT8 fgBLCustomCtrl=0;  // 1: Backlight can be controled by the customer.
#ifdef CC_S_PLATFORM
UINT8 fgMotionFlow = SV_OFF;
UINT8 fgPreMotionFlow = SV_OFF;
BOOL fgMotionFlowChg=SV_FALSE;
UINT8 u1ScalingMode = 0;
#endif
extern BOOL _fgVOPLLPowerOn;
#ifndef CC_MTK_LOADER
extern BOOL fgIsDitherInited;
#endif

UINT32 _u4FrameTrackMode = FRAME_TRACK_MODE_DDDS_BY_PSTSCALER + FRAME_TRACK_MODE_VTOTAL_BY_FNTSCALER;

BOOL fgVB1VideoMute = SV_FALSE;
UINT8 u1BrowserModeEn = SV_OFF;
UINT8 u1SpeIPTVSEn = SV_OFF;
BOOL fgFixModeChg=SV_FALSE;

#ifdef CC_MAPLE_CUST_DRV
#if (defined(CC_MT5893) || defined(CC_MT5895)) && defined(CC_SUPPORT_4KBE) 
extern UINT8 u1BePath[3];
#endif
#endif

#ifdef SUPPORT_SW_FRAME_CHECK
UINT32 _u4VDistUpperBound = 0;      // target in-out VSync distance
UINT32 _u4VDistLowerBound = 0;      // target in-out VSync distance
UINT32 _u4VLossDistTorance = 5;
UINT32 _u4VDistTorance = 0;
UINT32 _u4VDistTrackState = 0;          // 1: down, 2: up, 3:locked
UINT16 _u4VTrackOutCnt = 0;// 1: down, 2: up, 3:locked
UINT32 _u4UpTrackRange = 0;
UINT32 _u4DownTrackRange = 0;
UINT32 _u4VDistTrackMaxStep = 5;          
UINT32 _u4VDistTrackEn = 0;  
BOOL fgStartTrack= SV_FALSE;
UINT8 _u4VDistTrackLocked = 0;
#endif

static UINT8 su4CustDCLKType = SV_DCLK_60HZ;
static UINT8 su4CustFR = 60;

typedef enum
{
    VDP_PHOTO_2D       = 0,
    VDP_PHOTO_3D       = 1,
    VDP_PHOTO_3D_2_2D  = 2,
    VDP_PHOTO_2D_2_3D  = 3,
    VDP_PHOTO_NA 
}VDP_PHOTO_TYPE;

typedef struct{
    UINT16 u2FrameRate;
    UINT16 u2Width;
    UINT16 u2Height;
    UINT16 u2HTotal;
    UINT16 u2VTotal;
    UINT32 u4PixelClk;
    UINT16 u2VsyncWidth;
    UINT16 u2VsyncBp;
    UINT16 u2HsyncWidth;
    UINT16 u2HsyncFp;
}S_PANEL_CONFIG;

typedef struct{
    UINT32 u4PixleClockMax;
    UINT32 u4PixleClockMin;
    UINT16 u2HTotalMax;
    UINT16 u2HTotalMin;
    UINT16 u2VTotalMax;
    UINT16 u2VTotalMin;
    UINT16 u2VClkMax;
    UINT16 u2VClkMin;
}S_PANEL_CONFIG_COMMON;

static vDisplayTimingCB DiplayTimingCB[E_TIMEGEN_PATH_MAX][E_DISPLAY_TIMING_NUM_OF_CB] =
{
    {NULL, NULL,NULL,NULL,NULL,NULL,NULL,NULL},  // E_DISPLAY_TIMING_CB_H_ACTIVE
    {NULL, NULL,NULL,NULL,NULL,NULL,NULL,NULL},   // E_DISPLAY_TIMING_CB_H_TOTAL
    {NULL, NULL,NULL,NULL,NULL,NULL,NULL,NULL},   // E_DISPLAY_TIMING_CB_H_FP
};

UINT32 _4k2kPanelType = PANEL_TYPE_MAX;
BOOL fg4k2kPNLTypeChg = SV_FALSE;
BOOL fg4k2kBypPathChg = SV_FALSE;
BOOL fg4k2kFrmTrkChg = SV_FALSE;
BOOL fg4k2kVrrModChg = SV_FALSE;

S_PANEL_CONFIG rREF_4K_TG[3];

UINT8 _u1Force4K2KTiming =0;  //[JW]???
BOOL _fgBypassToPQ= SV_FALSE;
HANDLE_T h4k2kUnMuteTimer ;
HANDLE_T _hFrcUnmuteSema=(HANDLE_T)(NULL);
HANDLE_T _hFrcSetPanelSema=(HANDLE_T)(NULL);
void vFrcUnmute(void *pvArgs);
void vSaveRefAttribute_4K2K(void);

void DRV_GetCustDCLK(UINT8  *pu1CustDCLKType,UINT32 *pu4CustFrameRate);
void vDrvSetWobblingFinalCrop(UINT8 bOnOff, UINT8 bCropValue, UINT8 bCropRst, UINT8 bCropBypass);

#if defined(SUPPORT_PANEL_GRAPH_MODE)
UINT32 u4PreGFXType = 0xff;
static BOOL fgForceGfxTimingUpdate=FALSE;
#endif

BOOL _fgESDProtect = FALSE;

extern BOOL bDrvVideoIs2KSignal(UINT8 bPath);

#ifdef CC_MAPLE_CUST_DRV
/*****using notes*******
1. coeff bit width is 10bit, 0x100 is 1 time. offset bit width is 10bit, real use it to 8 bit,0xFF equal to 10bit 1023.
2. bit 12 is sign bit. 1 is stands for negative value. ex. -0.5, then the setting is 0x1080
3. the video output sequence is CbYCr, so the matrix is for RGB->CbYCr convertion.
************************/
UINT16 OSDMatrix_RGB2YUV601[12]=
{
   0x0082, 0x106D, 0x1015,
   0x004C, 0x0096, 0x001D,
   0x102C, 0x1056, 0x0082,
   0x0080, 0x0000, 0x0080
};

UINT8 u1FrontDitherSetting = 0x0;
UINT8 u1PostDitherSetting = 0x0;
#endif

#ifdef CC_PHP_SUPPORT
extern void DRVCUST_PQBAfterSocOutputChanged(UINT32 u4OutputHeigt,UINT32 u4OutputWidth,UINT8 u4OutputFrameRate);
#endif
//-----------------------------------------------------------------------------
// Static functions
//-----------------------------------------------------------------------------
#ifndef CC_MTK_LOADER
#ifdef CC_SCPOS_3DTV_SUPPORT
static void vDrvDisp3DModeCB(void);
static void vSavePanelAttribute(void);
static BOOL fgPDPL12R12 = ePanel_PDP_L1R1;
#ifdef DRV_SUPPORT_MT8283
BOOL fgForceChangeRegion = FALSE;
#endif
#endif

#endif

LCDTIMING_INFO_T lcdtimingInfo;
LCDTIMING_INFO_T ReftimingInfo;

//vbone control bit
#if (defined(CC_MT5893) || defined(CC_MT5895)) && defined(CC_SUPPORT_4KBE)
CONTROL_BIT_VB1_T  gVB1ControlBit;///control bit
E_TIMING_TRIGGER_STATE E_VB1ControlBit_Trig_Sta = E_TIMING_TRIGGER_STATE_IDLE;
#endif


#if defined(SUPPORT_PANEL_GRAPH_MODE)
TIMING_INFO_T GFXTimingInfo;
CONTROL_BIT_GFX_T gGFXControlBit;
#endif

//-----------------------------------------------------------------------------
// Public functions
//-----------------------------------------------------------------------------
#if defined(SUPPORT_PANEL_GRAPH_MODE)
void vDrvSetGfxTimingForceUpdated(BOOL fgOnOff)
{
#ifndef CC_MTK_LOADER
	CRIT_STATE_T csState;
	csState = x_crit_start();
#endif
    fgForceGfxTimingUpdate = fgOnOff;
#ifndef CC_MTK_LOADER
	x_crit_end(csState);
#endif
}
#endif

UINT32 GetPANEL4K2K_TYPE(void)
{
    return _4k2kPanelType;
}

void SetPANEL4K2K_TYPE(UINT32 u4Value)
{
	_4k2kPanelType = u4Value;
}

UINT16 wDrvGetPnlHTotal(void)
{
	return ((UINT16)lcdtimingInfo.u4HTotal);
}

UINT16 wDrvGetPnlVTotal(void)
{
	return ((UINT16)lcdtimingInfo.u4VTotal);
}

UINT16 wDrvGetPnlFP(void)
{
	return ((UINT16)lcdtimingInfo.u4VFP);
}

UINT16 wDrvGetPnlBP(void)
{
	return ((UINT16)lcdtimingInfo.u4VBP);
}
UINT16 wDrvGetPnlVActive(void)
{
	return ((UINT16)lcdtimingInfo.u4VActive);
}

UINT16 wDrvGetOutputHTotal(void)
{
    if (fgApplyScalerSetData)
    {
    #if 0  // [Husky] no bypass path, need to review later[JW]
	#ifndef CC_MTK_LOADER
	#if defined(CC_MT5893)
		if((GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60_BYP) && fgIsMainDec())
		{
			return ((UINT16)(lcdtimingInfo.u4HTotal>>1));
		}
		else
	#endif
	#endif
	#endif
        if(u1IsPanelSupport4K2K120() || u1IsPanelSupport8K4K30())
        {
            return ((UINT16)lcdtimingInfo.u4HTotal)/ 2;
        }
        else
        {
            return ((UINT16)lcdtimingInfo.u4HTotal);
        }
    }
    else
    {
    #if 0  // [Husky] no bypass path, need to review later[JW]
	#ifndef CC_MTK_LOADER
	#if defined(CC_MT5893)
		if((GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60_BYP) && fgIsMainDec())
	    {
			//return (VDP_GetBypassHTotal()+1);
		}
		else
	#endif
	#endif
	#endif
		{
	        if (fgIsMJCToOSTG())
	        {
	            return ((UINT16) u4PscGetPanelHTotal());
	        }
	        else
	        {
	            return (UINT16)u4ScpipGetPanelHtotal(E_FSC_TG);
	        }
    	}
    }
}

void wDrvSetOutputHTotal(UINT16 u2HTotal)
{
    UINT16 u2OriHTotal;
    if(fgIsMJCToOSTG())
    {
        u2OriHTotal = (UINT16)u4PscGetPanelHTotal();
        if(u2OriHTotal != u2HTotal)
        {
            #ifndef CC_MTK_LOADER
            _vDrvVideoSetMute(MUTE_MODULE_MJC, SV_VP_MAIN, 5, SV_TRUE);
            #endif
        }
        vPscSetPanelHTotal(u2HTotal);
    }
   else
    {
        vScpipSetPanelHtotal(E_FSC_TG, u2HTotal);
    }
#ifndef CC_MTK_LOADER
    vVRMSetEventFlg(SV_VP_MAIN, VRM_EVENT_BY_PANEL);
    vVRMSetEventFlg(SV_VP_PIP , VRM_EVENT_BY_PANEL);
#endif

}

UINT16 wDrvGetOutputHTotalStatus(void)
{
    if (fgIsMJCToOSTG())
    {
        return ((UINT16) u4PscGetOutputHTotalStatus());
    }
    else
    {
        return ((UINT16) u4ScpipGetOutputHTotalStatus(0));
    }
}

UINT16 wDrvGetOutputVTotal(void)
{
    if (fgApplyScalerSetData)
    {
        return ((UINT16)lcdtimingInfo.u4VTotal);
    }
    else
    {
    #if 0  // [Husky] no bypass path, need to review later[JW]
	#ifndef CC_MTK_LOADER
    #if	defined(CC_MT5893)
		if((GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60_BYP) && fgIsMainDec())
		{
			//return (VDP_GetBypassVTotal()+1);
		}
		else
	#endif
	#endif
	#endif
		{
	        if (fgIsMJCToOSTG())
	        {
	            return ((UINT16) u4PscGetPanelVTotal());
	        }
	        else
	        {
	            return ((UINT16) u4ScpipGetPanelVtotal(E_FSC_TG));
	        }
    	}
    }
}

UINT16 wDrvGetOutputVTotalStatus(void)
{
    if (fgIsMJCToOSTG())
    {
        return ((UINT16) u4PscGetOutputVTotalStatus());
    }
    else
    {
        return ((UINT16) u4ScpipGetOutputVTotalStatus(0));
    }
}

void wDrvSetOutputVTotal(UINT16 u2VToal)
{
    UINT16 u2VRRVToal = u2VToal;
#ifndef CC_MTK_LOADER
    if(bDrvIsHdmiVRRMode())
    {
        u2VRRVToal = DRV_MAX_VRR_VT;
    }
#endif

    if(fgIsMJCToOSTG())
    {
        vPscSetPanelVTotal(u2VRRVToal);

    }
    else
    {
        vScpipSetPanelVtotal(E_FSC_TG,u2VRRVToal);
    }

#ifndef CC_MTK_LOADER
    vVRMSetEventFlg(SV_VP_MAIN, VRM_EVENT_BY_PANEL);
    vVRMSetEventFlg(SV_VP_PIP , VRM_EVENT_BY_PANEL);
#endif

}

void wDrvSetOutputHActive(UINT16 u2HActive)
{
    if(fgIsMJCToOSTG())
    {
        vPscSetPanelWidth(u2HActive);
    }
    else
    {
        vScpipSetPanelWidth(E_FSC_TG,u2HActive);
    }

#ifndef CC_MTK_LOADER
    vVRMSetEventFlg(SV_VP_MAIN, VRM_EVENT_BY_PANEL);
    vVRMSetEventFlg(SV_VP_PIP , VRM_EVENT_BY_PANEL);
#endif

}

UINT16 wDrvGetOutputHActive(void)
{
    if (fgApplyScalerSetData)
    {
    #if 0  // [Husky] no bypass path, need to review later[JW]
	#ifndef CC_MTK_LOADER
    #if defined(CC_MT5893)
		if((GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60_BYP) && fgIsMainDec())
		{
			return ((UINT16)(lcdtimingInfo.u4HActive>>1));
		}
		else
	#endif
	#endif
	#endif
        if(u1IsPanelSupport4K2K120() || u1IsPanelSupport8K4K30())
        {
            return ((UINT16)lcdtimingInfo.u4HActive)/2 + vDrvGetOSTGCroppingCnt();
        }
        else
        {
            return ((UINT16)lcdtimingInfo.u4HActive);
        }
    }
    else
    {
    #if 0  // [Husky] no bypass path, need to review later[JW]
	#ifndef CC_MTK_LOADER
    #if defined(CC_MT5893)
		if((GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60_BYP) && fgIsMainDec())
	    {
			//return (VDP_GetNB2R_HActive()+1);
		}
		else
	#endif
	#endif
	#endif
		{
	        if (fgIsMJCToOSTG())
	        {
	            return ((UINT16) u4PscGetPanelWidth());
	        }
	        else
	        {
	            return ((UINT16) u4ScpipGetPanelWidth(E_FSC_TG));
	        }
    	}
    }
}


void wDrvSetOutputVActive(UINT16 u2VActive)
{
    if(fgIsMJCToOSTG())
    {
        vPscSetPanelHeight(u2VActive);
    }
    else
    {
        vScpipSetPanelHeight(E_FSC_TG,u2VActive);
    }

#ifndef CC_MTK_LOADER
    vVRMSetEventFlg(SV_VP_MAIN, VRM_EVENT_BY_PANEL);
    vVRMSetEventFlg(SV_VP_PIP , VRM_EVENT_BY_PANEL);
#endif

}

UINT16 wDrvGetOutputVActive(void)
{
    if (fgApplyScalerSetData)
    {
    	return ((UINT16)lcdtimingInfo.u4VActive);
    }
    else
    {
    #if 0  // [Husky] no bypass path, need to review later[JW]
	#ifndef CC_MTK_LOADER
	#if defined(CC_MT5893)
		if((GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60_BYP) && fgIsMainDec())
		{
			//return (VDP_GetNB2R_VActive()+1);
		}
		else
	#endif
	#endif
	#endif
		{
	        if (fgIsMJCToOSTG())
	        {
	            return ((UINT16) u4PscGetPanelHeight());
	        }
	        else
	        {
	            return ((UINT16) u4ScpipGetPanelHeight(E_FSC_TG));
	        }
    	}
    }
}

void vDrvSetHsyncFp(UINT16 u2Fp)
{

    if(fgIsMJCToOSTG())
    {
        vPscSetPanelHSyncFp(u2Fp);
    }
    else
    {
        vScpipSetHsyncFp(E_FSC_TG,u2Fp);
    }
}

void vDrvSetHsyncBp(UINT16 u2Bp)
{
    if(fgIsMJCToOSTG())
    {
        vPscSetPanelHSyncBp(u2Bp);
    }
    else
    {
        vScpipSetHsyncBp(E_FSC_TG,u2Bp);
    }
}

UINT16 wDrvGetHsyncBp(void)
{
    if (fgApplyScalerSetData)
    {
    #if 0  // [Husky] no bypass path, need to review later[JW]
	#if defined(CC_MT5893)
		if((GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60_BYP) && fgIsMainDec())
		{
			return ((UINT16)(lcdtimingInfo.u4HBP>>1));
		}
		else
	#endif
	#endif
        if(u1IsPanelSupport4K2K120() || u1IsPanelSupport8K4K30())
        {
            return ((UINT16)lcdtimingInfo.u4HBP)/2;
        }
        else
        {
            return ((UINT16)lcdtimingInfo.u4HBP);
        }
    }
    else
    {
    #if 0  // [Husky] no bypass path, need to review later[JW]
	#if defined(CC_MT5893)
		if((GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60_BYP) && fgIsMainDec())
		{
			return ((UINT16)(lcdtimingInfo.u4HBP>>1));
		}
		else
	#endif
	#endif
		{
	        if (fgIsMJCToOSTG())
	        {
	            return ((UINT16) u4PscGetPanelHSyncBp());
	        }
	        else
	        {
	            return ((UINT16)u4ScpipGetHsyncBp(E_FSC_TG));
	        }
    	}
    }
}

UINT16 wDrvGetHsyncFp(void)
{
    if (fgApplyScalerSetData)
    {
    #if 0  // [Husky] no bypass path, need to review later[JW]
	#ifndef CC_MTK_LOADER
    #if defined(CC_MT5893)
		if((GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60_BYP) && fgIsMainDec())
		{
			return ((UINT16)(lcdtimingInfo.u4HFP>>1));
		}
		else
	#endif
	#endif
	#endif
        if(u1IsPanelSupport4K2K120() || u1IsPanelSupport8K4K30())
        {
            return ((lcdtimingInfo.u4HFP/2) >= vDrvGetOSTGCroppingCnt())? ((lcdtimingInfo.u4HFP/2)- vDrvGetOSTGCroppingCnt()):(lcdtimingInfo.u4HFP/2);
        }
        else
        {
            return ((UINT16)lcdtimingInfo.u4HFP);
        }
    }
    else
    {
    #if 0  // [Husky] no bypass path, need to review later[JW]
	#ifndef CC_MTK_LOADER
    #if defined(CC_MT5893)
		if((GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60_BYP) && fgIsMainDec())
		{
			return ((UINT16)(lcdtimingInfo.u4HFP>>1));
		}
		else
	#endif
	#endif
	#endif
		{
	        if (fgIsMJCToOSTG())
	        {
	         return ((UINT16)u4PscGetPanelHSyncFp());
	        }
	        else
	        {
	         return ((UINT16)u4ScpipGetHsyncFp(E_FSC_TG));
	        }
    	}
    }
}

UINT16 wDrvGetHsyncWidth(void)
{
    UINT16 u2SyncWidth=0;

    u2SyncWidth =  wDrvGetOutputHTotal() - wDrvGetOutputHActive()- wDrvGetHsyncFp()-wDrvGetHsyncBp();
    return ((UINT16)u2SyncWidth);
}

UINT16 wDrvGetVsyncWidthSW(void)
{
    UINT16 u2SyncWidth=0;

    u2SyncWidth = lcdtimingInfo.u4VTotal - lcdtimingInfo.u4VActive -
        lcdtimingInfo.u4VFP - lcdtimingInfo.u4VBP;

    if(GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60_SRS && !fgIsMJCToOSTG())
    {
        u2SyncWidth /= 2;
    }

    return u2SyncWidth;
}

void vDrvSetVsyncFp(UINT16 u2VFp)
{
    UINT16 u2SwapVSyncToVFp = u2VFp;
    if(fgIsMJCToOSTG())
    {
        if(u4ScpipGetVTModeCtrl(E_PSC_TG) == E_TRK_FRONT_PORCH)
        {
            u2SwapVSyncToVFp = wDrvGetVsyncWidthSW();
            LOG(3,"PSC swapVFP %d -> %d \n",u2VFp,u2SwapVSyncToVFp);
        }
        vPscSetPanelVSyncFp(u2SwapVSyncToVFp);
    }
    else
    {
        if(u4ScpipGetVTModeCtrl(E_FSC_TG) == E_TRK_FRONT_PORCH)
        {
            u2SwapVSyncToVFp = wDrvGetVsyncWidthSW();
            LOG(3,"FSC swapVFP %d -> %d \n",u2VFp,u2SwapVSyncToVFp);
        }
        vScpipSetVsyncFp(E_FSC_TG,u2SwapVSyncToVFp);
    }
}

void vDrvSetVsyncBp(UINT16 u2VBp)
{
    if(fgIsMJCToOSTG())
    {
        vPscSetPanelVSyncBp(u2VBp);
    }
    else
    {
        vScpipSetVsyncBp(E_FSC_TG,u2VBp);
    }
}

UINT16 wDrvGetVsyncFp(void)
{
    if (fgApplyScalerSetData)
    {
        return ((UINT16)lcdtimingInfo.u4VFP);
    }
    else
    {
    #if 0  // [Husky] no bypass path, need to review later[JW]
	#ifndef CC_MTK_LOADER
    #if defined(CC_MT5893)
		if((GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60_BYP) && fgIsMainDec())
		{
			return ((UINT16)lcdtimingInfo.u4VFP);
		}
		else
	#endif
	#endif
	#endif
		{
	        if (fgIsMJCToOSTG())
	        {
	            return ((UINT16)u4PscGetPanelVSyncFp());
	        }
	        else
	        {
	            return ((UINT16)u4ScpipGetVsyncFp(E_FSC_TG));
	        }
    	}
    }
}

UINT16 wDrvGetVsyncBp(void)
{
    if (fgApplyScalerSetData)
    {
        return ((UINT16)lcdtimingInfo.u4VBP);
    }
    else
    {
    #if 0  // [Husky] no bypass path, need to review later[JW]
	#ifndef CC_MTK_LOADER
	#if defined(CC_MT5893)
		if((GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60_BYP) && fgIsMainDec())
		{
			return ((UINT16)lcdtimingInfo.u4VBP);
		}
		else
	#endif
	#endif
	#endif
		{
	        if (fgIsMJCToOSTG())
	        {
	            return ((UINT16)u4PscGetPanelVSyncBp());
	        }
	        else
	        {
	            return ((UINT16)u4ScpipGetVsyncBp(E_FSC_TG));
	        }
    	}
    }
}

UINT16 wDrvGetVsyncWidth(void)
{
    UINT16 u2SyncWidth=0;

    u2SyncWidth =  wDrvGetOutputVTotal() - wDrvGetOutputVActive()- wDrvGetVsyncFp()-wDrvGetVsyncBp();
    return ((UINT16)u2SyncWidth);
}

void vDrvSet2SECVTotal(UINT16 u2VToal)
{
    vIO32WriteFldAlign(VINTF_P29,  (u2VToal-1),  VINTF_P29_2SEC_V_TOTAL);
    LOG(3, "[VB1][2-SEC] 2SEC_V_total=%d\n", IO32ReadFldAlign(VINTF_P29, VINTF_P29_2SEC_V_TOTAL));
}

void vDrvSet2SECVsyncWidth(UINT16 u2VsyncWidth)
{
    vIO32WriteFldAlign(VINTF_P29,  (u2VsyncWidth-1),  VINTF_P29_2SEC_V_WIDTH);
    LOG(3, "[VB1][2-SEC] 2SEC_V_width=%d\n", IO32ReadFldAlign(VINTF_P29, VINTF_P29_2SEC_V_WIDTH));
}

#ifndef CC_MTK_LOADER
/**
 * @brief fgDrvSupport24Hz

 * use variable to control the frame rate control under 24Hz

 * @param  void
 * @retval TRUE/FALSE
 */
UINT8 fgDrvSupport24Hz(void)
{
    if (IS_PANEL_2D_N_3D_L12R12)
    {
        if( bGetVideoDecType(SV_VP_MAIN) != SV_VD_DVI)
        return (SV_FALSE);
        else
        return (SV_TRUE);
    }
    else
    {
        return (SV_TRUE);
    }
}
#endif

UINT8 u1DrvCal48HzOutputType(void)
{
	if (SUPPORT_PANEL_48HZ)
		return (SV_DCLK_48HZ);
	else if (IS_DISP_48HZ_TO_50HZ)
		return (SV_DCLK_50HZ);
	else // define DISP_48HZ_TO_60HZ or define none
		return (SV_DCLK_60HZ);
}

/**
 * @brief bDrvCheckMTKGoodStatus

 * Check MTK Good Clock Lock or Unlock

 * @param  void
 * @retval TRUE/FALSE
 */
UINT8 bDrvCheckMTKGoodStatus(void)
{
    return (IO32ReadFldAlign(STA_DDDS_00, STA_DDDS_00_DDDS_LOCK));
}

UINT32 fgDrvSetMJCToOSTG(UINT32 fgOnOff)
{
   #if !defined(CC_BYPASS_MJC)
    if (fgOnOff == fgIsMJCToOSTG())
    {
        return FALSE;
    }
	vDrvSetOSTGMjcEn(fgOnOff); 
    vDrvSetPanelTiming();

    return TRUE;
    #else
    return FALSE;
    #endif
}

UINT32 fgIsMJCToOSTG(void)
{
#if !defined(CC_BYPASS_MJC)
    #if defined(CC_MT5893) || defined(CC_MT5895)
    if(IS_DISP_4K2K_TYPE3)
    {
#ifndef CC_MTK_LOADER
        if(u1DrvIsVdpBypCase())
        {
            return TRUE;
        }
        else
#endif
        {
            return FALSE;
        }
    }
    else
    {
        if(((IO32ReadFldAlign(OSTG_01, REG_MJC_IN_EN)==0) && u1IsPanelSupport4K2K120() == 0)
            || u1DrvIsPnl4k2k60WithMemc()) 
        {
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
    #else
    return FALSE;
		    
    #endif
#else
    	return FALSE;  
#endif
}

 #ifndef CC_MTK_LOADER
/**
 * @brief vDrvSetMTKGoodDclk

 * Set MTK Good Dclk Parameters

 * @param  wVERT_LINE: Input Total Lines in the Vertical Direction
 * @retval void
 */
void vDrvSetMTKGoodDclk(UINT16 u2InputVTotal, UINT8 bInputFrameRate) // Called by Video
{
    UINT32 DISPLAY_SIZE, HLEN_INT, HLEN_NUM;
    UINT32 u4HTotal, u4VTotal;
	#if ENABLE_DDDS2_FEEDBACK
    UINT8 u1VopllRatio;
	#endif
	UINT32 u4MUTE_CNT_LIMIT;
	UINT8 bType;

    if ((u2InputVTotal == 0) || (bInputFrameRate == 0))
    {
        LINT_SUPPRESS_NEXT_EXPRESSION(527);
        return;
    }
	//for FP FS, case, bInputFrameRate is half of real input video frame in vidoe path
	bInputFrameRate <<= (u4DrvTDTV3DModeQuery() == E_TDTV_UI_3D_MODE_FS) ? 1 : 0;


	bType = bGetVideoDecType(SV_VP_MAIN);
	vDrvSetDDDSInput(bType);
	
	if(bDrvVideoIsSrcInterlace(SV_VP_MAIN))
	u4MUTE_CNT_LIMIT = (SYSPLL_CLK_SS/(u2InputVTotal*bInputFrameRate));
	else
	u4MUTE_CNT_LIMIT = (SYSPLL_CLK_SS/(u2InputVTotal*bInputFrameRate))>>1;
	
	u4MUTE_CNT_LIMIT = (u4MUTE_CNT_LIMIT>0)?(u4MUTE_CNT_LIMIT-1):0;
	vIO32WriteFldAlign(DDDS_12, u4MUTE_CNT_LIMIT, DDDS_12_MUTE_CNT_LIMIT);
	LOG(3, "5DDDS MUTE_CNT_LIMIT=%d\n", u4MUTE_CNT_LIMIT);

    u4HTotal = wDrvGetPnlHTotal();//(UINT32)wDrvGetOutputHTotal();
    u4VTotal = wDrvGetPnlVTotal();//(UINT32)wDrvGetOutputVTotal();
    DISPLAY_SIZE = u4HTotal * u4VTotal;

    DISPLAY_SIZE = DISPLAY_SIZE * bOUTPUT_REFRESH_RATE / bInputFrameRate;
    //DISPLAY_SIZE = _u4PnlClk / bInputFrameRate;
    LOG(3, "5DDDS %dHz --> %dHz\n", bInputFrameRate, bOUTPUT_REFRESH_RATE);

    //LOG(0,"5DDDS %dHz --> %dHz %d x %d\n", bInputFrameRate, _u4PnlClk, u4HTotal, u4VTotal);

#if 0  // [Husky] no bypass path, need to review later[JW]    
    DISPLAY_SIZE = bDrvVideoIsSrcInterlace(SV_VP_MAIN) ? DISPLAY_SIZE * 2 : 
		((GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60_BYP) && fgIsMainDec() && (BSP_GetIcVersion()>=IC_VER_5890_AC) && PANEL_IsSupport4K2KDD())? DISPLAY_SIZE * 2 : DISPLAY_SIZE;    
#else
	DISPLAY_SIZE = bDrvVideoIsSrcInterlace(SV_VP_MAIN) ? DISPLAY_SIZE * 2 : DISPLAY_SIZE;
#endif

    #if ENABLE_DDDS2_FEEDBACK
    if ((LVDS_OUTPUT_PORT == SINGLE_PORT)&&(IS_LVDS_HIGHSPEED)) u1VopllRatio=1;
    else  if (LVDS_OUTPUT_PORT == SINGLE_PORT) u1VopllRatio=0;
    else  if ((LVDS_OUTPUT_PORT == DUAL_PORT)&&(IS_LVDS_HIGHSPEED)) u1VopllRatio=2;
    else  if ((LVDS_OUTPUT_PORT == DUAL_PORT)&&(IS_PANEL_L12R12)) u1VopllRatio=2;
    else  if (LVDS_OUTPUT_PORT == DUAL_PORT) u1VopllRatio=1;
    else  if ((LVDS_OUTPUT_PORT == FOUR_PORT)&&(IS_LVDS_HIGHSPEED)) u1VopllRatio=3;
    else  if ((LVDS_OUTPUT_PORT == FOUR_PORT)&&(IS_PANEL_L12R12)) u1VopllRatio=3;
    else  if (LVDS_OUTPUT_PORT == OCTA_PORT)
    {
    	if (PANEL_IsSupport4K2KDD())
			u1VopllRatio=3;  // /8
		else
			u1VopllRatio=2;
    }
	else if (LVDS_OUTPUT_PORT == HEXA_PORT)
	{
        if(IS_DISP_4K2K_TYPE3 && !u1DrvIsVdpBypCase())
        {
            u1VopllRatio=3;
        }
        else
        {
            u1VopllRatio=4;  // /16
        }
	}
    else 
    {
		u1VopllRatio=2;
    }

	DISPLAY_SIZE = DISPLAY_SIZE >> u1VopllRatio;
    #endif


    LOG(0,"[SetGoodDclk]u1VopllRatio = %d\n",u1VopllRatio);

    HLEN_INT = (UINT32) (DISPLAY_SIZE / u2InputVTotal);
    HLEN_NUM = DISPLAY_SIZE - (HLEN_INT * (UINT32) u2InputVTotal);
    LOG(0, "[SetGoodDclk]inVTaol: 0x%X, outV: 0x%X, outH: 0x%X, HLEN_INT: 0x%X, HLEN_NUM: 0x%X\n",
    u2InputVTotal, wDrvGetOutputVTotal(), wDrvGetOutputHTotal(), HLEN_INT, HLEN_NUM);

    vIO32WriteFldAlign(DDDS_02,(UINT16)u2InputVTotal,DDDS_02_HLEN_DEN);
    vIO32WriteFldAlign(DDDS_01,(UINT16)HLEN_NUM,DDDS_01_HLEN_NUM);
    vIO32WriteFldAlign(DDDS_01,HLEN_INT,DDDS_01_HLEN_INT);

	LOG(0, "[SetGoodDclk]HLEN=0x%x . [0x%x / 0x%x] \n",HLEN_INT, HLEN_NUM, u2InputVTotal);
#ifdef FT_USE_NEW_WAY
	vIO32WriteFldAlign(DDDS_0C,SV_ON,DDDS_0C_VSYNC_TRACK_EN);
#endif
}

static BOOL vDrvDDDSLockStatusCnt(UINT32 u4Count)
{
 UINT32 u4LoopCnt=u4Count;
 UINT32 u4UnlockCnt=0;

 do
 {
    if (!bDrvCheckMTKGoodStatus())
    {
    	u4UnlockCnt++;
    }
    u4LoopCnt--;
	HAL_Delay_us(100);
 }while(u4LoopCnt);
 
 if(u4UnlockCnt)
 {
	return 0;   
 }
 else
 {
	 return 1;
 }
}
 
void vScanGoodDclkRegion(UINT16 u2StartHsyncFreg, UINT16 u2EndHsyncFreg, UINT16 u2step)
{
	UINT16 u2votal, u2HsyncFreq;
	UINT16 u2Framerate=bDrvVideoGetRefreshRate(SV_VP_MAIN);
	UINT16 u2delayCnt=0;
	BOOL fglockstatus=0;

	if((IO32ReadFldAlign(DDDS_02, DDDS_02_SCPOS_H_SEL)==4)||(IO32ReadFldAlign(DDDS_00, DDDS_00_DISP_EN)==0))
	{
		LOG(0,"Open loop/ mute clk \n");
		return;
	}

	if(u2Framerate ==0)	u2Framerate = 60;

	LOG(1,"u2StartHsyncFreg =%d, u2EndHsyncFreg = %d,u2step =%d u2Framerate = %d \n", u2StartHsyncFreg,u2EndHsyncFreg, u2step, u2Framerate);
	vIO32WriteFldAlign(DDDS_0A, 1, DDDS_0A_SC_DEBUG_MODE);	
	vIO32WriteFldAlign(DDDS_0A, 1, DDDS_0A_MJC_DEBUG_MODE);		
	vIO32WriteFldAlign(DDDS_03, 0x1F, DDDS_03_DDDS_ERR_LIM);
	vIO32WriteFldAlign(DDDS_0C, 0x0, DDDS_0C_VSYNC_TRACK_EN);	
	
	for(u2HsyncFreq=u2StartHsyncFreg; u2HsyncFreq<=u2EndHsyncFreg; u2HsyncFreq+=u2step)
	{
	 vIO32WriteFldMulti(DDDS_00,P_Fld(0,DDDS_00_DISP_EN)|P_Fld(0,DDDS_00_FIX_FS_DDDS_SEL));
	 for(u2delayCnt=0; u2delayCnt<=1000; u2delayCnt++)	 
	 HAL_Delay_us(1000);  // 1ms	 
	 u2votal = u2HsyncFreq * 100 / u2Framerate;
	 vDrvSetMTKGoodDclk(u2votal, (UINT8)u2Framerate);
	 vIO32WriteFldMulti(DDDS_00,P_Fld(1,DDDS_00_DISP_EN)|P_Fld(1,DDDS_00_FIX_FS_DDDS_SEL));
	 for(u2delayCnt=0; u2delayCnt<=1000; u2delayCnt++)	 
	 HAL_Delay_us(1000);  // 1ms
	 fglockstatus = vDrvDDDSLockStatusCnt(10000);
	 if(fglockstatus)
     LOG(0,"lock = %d, u2HsyncFreq=%3d, u2votal = %d HLEN INT+NUM/DEN %d+%d/%d \n ", fglockstatus, u2HsyncFreq, u2votal, 
	  		 IO32ReadFldAlign(DDDS_01 ,DDDS_01_HLEN_INT) , IO32ReadFldAlign(DDDS_01 ,DDDS_01_HLEN_NUM), IO32ReadFldAlign(DDDS_02 ,DDDS_02_HLEN_DEN));
	 else
	 LOG(0,"lock = %d, u2HsyncFreq=%3d, u2votal = %d\n ", fglockstatus, u2HsyncFreq, u2votal);	 
	}
}
#endif // end of #ifndef CC_MTK_LOADER

BOOL fgIsVOPLLPowerOn(void)
{
	if(IO32ReadFldAlign(DDDS_08, FLAG_VOPLL_POWERON))
	{
		_fgVOPLLPowerOn = TRUE;
		return TRUE;
	}
	else
	{
		_fgVOPLLPowerOn = FALSE;
		return FALSE;
	}
}

UINT8 fgDisplayFreeRun = SV_FALSE;
void vDrvSetDisplayFreeRun(UINT8 bOn_Off)
{
    fgDisplayFreeRun = bOn_Off;
}

void vDrvSetDDDSCloseloop(UINT8 bOn_Off)
{
 static UINT8 fgCloseloop_2nd = 0;

 if ((IS_DISPLAY_FREERUN) || (fgDisplayFreeRun))
 {
     vIO32WriteFldMulti(DDDS_00,P_Fld(0,DDDS_00_DISP_EN)|P_Fld(0,DDDS_00_FIX_FS_DDDS_SEL));
     vIO32WriteFldAlign(DDDS_03, 1, DDDS_03_SPREAD_INIT);
     #ifdef CC_COPLAT_MT82
     vUtDelay2us(50);
     #else
     HAL_Delay_us(100);
     #endif
     vIO32WriteFldAlign(DDDS_03, 0, DDDS_03_SPREAD_INIT);
 }
 else
 {
    if (bOn_Off)
    {
        if(fgCloseloop_2nd)
        {
            vIO32WriteFldMulti(DDDS_00,P_Fld(1,DDDS_00_DISP_EN)|P_Fld(1,DDDS_00_FIX_FS_DDDS_SEL));
        }
        else
        {
            // overcome the clock jump when 1st enter close loop
            vIO32WriteFldAlign(DDDS_00, 1, DDDS_00_DISP_EN);
            HAL_Delay_us(20);
            vIO32WriteFldAlign(DDDS_00, 1, DDDS_00_FIX_FS_DDDS_SEL);
            fgCloseloop_2nd = 1;
        }
    }
    else
    {
	#if 0
    	vIO32WriteFldMulti(DDDS_00,P_Fld(0,DDDS_00_DISP_EN)|P_Fld(0,DDDS_00_FIX_FS_DDDS_SEL));
	#else	
	#ifdef FT_USE_NEW_WAY
		vIO32WriteFldAlign(DDDS_0C,SV_OFF,DDDS_0C_VSYNC_TRACK_EN);
	#endif
    	vIO32WriteFldAlign(DDDS_02, 4, DDDS_02_SCPOS_H_SEL);  // use MUTE_HSYNC
	#endif
    }
 }
}


/**
 * @brief vDrvSwitchMTKGoodDclk

 * MTK Good Dclk ON/OFF

 * @param  bOn_Off: SV_ON/SV_OFF
 * @retval void
 */
void vDrvSwitchMTKGoodDclk(UINT8 bOn_Off)
{
#ifndef CC_MTK_LOADER

    if(eVRRGetSyncMode() == E_VRR_SYNC_MODE_FREERUN)
    {
        bOn_Off = SV_OFF;
    }

    if (bOn_Off == SV_ON)
    {
        vDrvSetDDDSCloseloop(1);
    }
    else
    {
        vDrvSetDDDSCloseloop(0);

        vIO32WriteFldAlign(DDDS_03, 1, DDDS_03_SPREAD_INIT);
        #ifdef CC_COPLAT_MT82
        vUtDelay2us(50);
        #else
        HAL_Delay_us(100);
        #endif
        vIO32WriteFldAlign(DDDS_03, 0, DDDS_03_SPREAD_INIT);
    }
    #endif //end of #ifndef CC_MTK_LOADER
}

UINT32 u4GetPanelVSyncBp(void)
{
 UINT32 u4VPorchB;

 #ifdef SUPPORT_PANEL_CUSTOMER_SPEC
 u4VPorchB = PANEL_GetVSyncBpByTiming(bSI_DISPLAY_DCLK_TYPE);
 #else
 u4VPorchB = (V_POS < (0x400 - VS_WIDTH + 6))?((0x400 - V_POS - VS_WIDTH + 6)):(0);
 #endif

 return (u4VPorchB);
}

// LVDS power------------------------------------------------------------------
void vDrvTurnOnPanel()
{
    do
    {
        UINT32 gpio, value;
        if(DRVCUST_PanelQuery(ePanelPowerGpio, &gpio) == 0 &&
           DRVCUST_PanelQuery(ePanelPowerOnVal, &value) == 0)
        {
            vDrvGpioSet((INT32)gpio, (INT32)value);
        }
    } while (0);
}

void vDrvTurnOffPanel()
{
    do
    {
        UINT32 gpio, value;
        if(DRVCUST_PanelQuery(ePanelPowerGpio, &gpio) == 0 &&
           DRVCUST_PanelQuery(ePanelPowerOnVal, &value) == 0)
        {
            vDrvGpioSet((INT32)gpio, (INT32)!value);
        }
    } while (0);
}

void vDrvPanelPowerOn()
{
    PFN_PANLE_SET_PANEL_POWER_1_ON_FUNC pfnPanelSetPanelPower1OnFunc = NULL;

    do
    {
        UINT32 gpio, value;
        if(DRVCUST_PanelQuery(ePanelPower1Gpio, &gpio) == 0 &&
           DRVCUST_PanelQuery(ePanelPower1OnVal, &value) == 0)
        {
            pfnPanelSetPanelPower1OnFunc = (PFN_PANLE_SET_PANEL_POWER_1_ON_FUNC)DRVCUST_InitGetUptr(ePanelSetPanelPower1OnFunc);
            if(pfnPanelSetPanelPower1OnFunc)
            {
                pfnPanelSetPanelPower1OnFunc();
            }
            vDrvGpioSet((INT32)gpio, (INT32)value);
        }
    } while (0);
}

void vDrvPanelPowerOff()
{
    PFN_PANLE_SET_PANEL_POWER_1_OFF_FUNC pfnPanelSetPanelPower1OffFunc = NULL;

    do
    {
        UINT32 gpio, value;
        if(DRVCUST_PanelQuery(ePanelPower1Gpio, &gpio) == 0 &&
           DRVCUST_PanelQuery(ePanelPower1OnVal, &value) == 0)
        {
            pfnPanelSetPanelPower1OffFunc = (PFN_PANLE_SET_PANEL_POWER_1_OFF_FUNC)DRVCUST_InitGetUptr(ePanelSetPanelPower1OffFunc);
            if(pfnPanelSetPanelPower1OffFunc)
            {
                pfnPanelSetPanelPower1OffFunc();
            }
            vDrvGpioSet((INT32)gpio, (INT32)!value);
        }
    } while (0);
}

void vDrvPanelPower2On()
{
    PFN_PANLE_SET_PANEL_POWER_2_ON_FUNC pfnPanelSetPanelPower2OnFunc = NULL;

    do
    {
        UINT32 gpio, value;
        if(DRVCUST_PanelQuery(ePanelPower2Gpio, &gpio) == 0 &&
           DRVCUST_PanelQuery(ePanelPower2OnVal, &value) == 0)
        {
            pfnPanelSetPanelPower2OnFunc = (PFN_PANLE_SET_PANEL_POWER_2_ON_FUNC)DRVCUST_InitGetUptr(ePanelSetPanelPower2OnFunc);
            if(pfnPanelSetPanelPower2OnFunc)
            {
                pfnPanelSetPanelPower2OnFunc();
            }
            vDrvGpioSet((INT32)gpio, (INT32)value);
        }
    } while (0);
}

void vDrvPanelPower2Off()
{
    PFN_PANLE_SET_PANEL_POWER_2_OFF_FUNC pfnPanelSetPanelPower2OffFunc = NULL;

    do
    {
        UINT32 gpio, value;
        if(DRVCUST_PanelQuery(ePanelPower2Gpio, &gpio) == 0 &&
           DRVCUST_PanelQuery(ePanelPower2OnVal, &value) == 0)
        {
            pfnPanelSetPanelPower2OffFunc = (PFN_PANLE_SET_PANEL_POWER_2_OFF_FUNC)DRVCUST_InitGetUptr(ePanelSetPanelPower2OffFunc);
            if(pfnPanelSetPanelPower2OffFunc)
            {
                pfnPanelSetPanelPower2OffFunc();
            }
            vDrvGpioSet((INT32)gpio, (INT32)!value);
        }
    } while (0);
}

void vDrvBackltMuteOn()
{
    PFN_PANLE_SET_BACKLIGHT_MUTE_ON_FUNC pfnPanelSetBackLightMuteOnFunc = NULL;

    do
    {
        UINT32 gpio, value;
        if(DRVCUST_PanelQuery(eBackLightMuteGpio, &gpio) == 0 &&
           DRVCUST_PanelQuery(eBackLightMuteOnVal, &value) == 0)
        {
            pfnPanelSetBackLightMuteOnFunc = (PFN_PANLE_SET_BACKLIGHT_MUTE_ON_FUNC)DRVCUST_InitGetUptr(ePanelSetBackLightMuteOnFunc);
            if(pfnPanelSetBackLightMuteOnFunc)
            {
                pfnPanelSetBackLightMuteOnFunc();
            }
            vDrvGpioSet((INT32)gpio, (INT32)value);
        }
    } while (0);
}

void vDrvBackltMuteOff()
{
    PFN_PANLE_SET_BACKLIGHT_MUTE_OFF_FUNC pfnPanelSetBackLightMuteOffFunc = NULL;

    do
    {
        UINT32 gpio, value;
        if(DRVCUST_PanelQuery(eBackLightMuteGpio, &gpio) == 0 &&
           DRVCUST_PanelQuery(eBackLightMuteOnVal, &value) == 0)
        {
            pfnPanelSetBackLightMuteOffFunc = (PFN_PANLE_SET_BACKLIGHT_MUTE_OFF_FUNC)DRVCUST_InitGetUptr(ePanelSetBackLightMuteOffFunc);
            if(pfnPanelSetBackLightMuteOffFunc)
            {
                pfnPanelSetBackLightMuteOffFunc();
            }
            vDrvGpioSet((INT32)gpio, (INT32)!value);
        }
    } while (0);
}

//-----------------------------------------------------------------------------
/** turn on/off panel lvds signal. (used by MTAL)
 *  @param fgEnable: Control mode. It could be TRUE or FALSE
 *  @return
 */
//-----------------------------------------------------------------------------

void vDrvLVDSSignalOnOff(BOOL fgOnOff)
{
    if(PANEL_GetDisplayType()== PANEL_DISPLAY_TYPE_LVDS)
    {
    if(fgOnOff)
    {
        vDrvLVDSOn();
    }
    else
    {
        vDrvLVDSOff();
    }
    }
}

//-----------------------------------------------------------------------------
/** turn on/off panel Vx1 signal. (used by MTAL)
 *  @param fgEnable: Control mode. It could be TRUE or FALSE
 *  @return
 */
//-----------------------------------------------------------------------------

void vDrvVByOneSignalOnOff(BOOL fgOnOff)
{
    if(PANEL_GetDisplayType()== PANEL_DISPLAY_TYPE_VB1)
    {
		if(fgOnOff)
		{
			vDrvVByOneOn();
		}
		else
		{
			vDrvVByOneOff();
		}
	}
}

//-----------------------------------------------------------------------------
/** turn on/off panel lvds powe. (used by MTAL)
 *  @param fgEnable: Control mode. It could be TRUE or FALSE
 *  @return
 */
//-----------------------------------------------------------------------------

void vDrvLVDSPowerOnOff(BOOL fgOnOff)
{
    PFN_SET_TURN_ON_PANEL pfnSetTurnOnPanel = NULL;
    PFN_SET_TURN_OFF_PANEL pfnSetTurnOffPanel = NULL;
    
    if(fgOnOff)
    {
        UNUSED(DRVCUST_InitQueryUptr(eDrvTurnOnPanel, (UPTR *)(void *)&(pfnSetTurnOnPanel)));
        if(pfnSetTurnOnPanel) pfnSetTurnOnPanel();
    }
    else
    {
        UNUSED(DRVCUST_InitQueryUptr(eDrvTurnOffPanel, (UPTR *)(void *)&(pfnSetTurnOffPanel)));
        if(pfnSetTurnOffPanel) pfnSetTurnOffPanel();
    }
}

void vDrvVsyncISRSetPosition(UINT16 u2INTRLine)
{
    vIO32WriteFldAlign(INTR_01, u2INTRLine, REG_INTR_LINE);
}

void vDrvVsyncISRPositionTrigger(UINT16 u2INTRLine)
{
    vIO32WriteFldAlign(INTR_01, 1, REG_INTR_LINE_LATCH_OFF);
    vIO32WriteFldAlign(INTR_01, u2INTRLine, REG_INTR_LINE);
    vIO32WriteFldAlign(INTR_01, 0, REG_INTR_LINE_LATCH_OFF);
}

UINT16 vDrvVsyncISRGetPosition(void)
{
    return(IO32ReadFldAlign(INTR_01, REG_INTR_LINE));
}

UINT8 u1DrvGetVsyncISRFCNTStatus(void)
{
    return(IO32ReadFldAlign(INTR_00, FCONT));
}

static void vDrvVsyncISRSetLineCounter(UINT16 u2Vfp)
{
    if (IS_PANEL_L12R12)
    {
        vDrvVsyncISRSetPosition((wDrvGetOutputVTotal()/2)-u2Vfp);
    }
    else
    {
        vDrvVsyncISRSetPosition(lcdtimingInfo.u4VTotal-u2Vfp);
    }
}

static void vDrvVsyncISRInit(void)
{
    vDrvVsyncISRPositionTrigger(0);
}

void vDrvSetTCONIrqClear(UINT32 u4INTRSource)
{
    vIO32WriteFldAlign(INTR_02, u4INTRSource, C_INTR_CLEAR);
    vIO32WriteFldAlign(INTR_02, 0, C_INTR_CLEAR);
}

void vDrvGetTCONIrqStatus(UINT32 * u4INTRStatus)
{
    *u4INTRStatus = IO32ReadFldAlign(INTR_00, STATUS_INTR);
}

static void vDrvVsyncISRSetMask(UINT16 u2Mask)
{
    vIO32WriteFldAlign(INTR_02, u2Mask,C_INTR_MASK);
}

static UINT16 vDrvVsyncISRGetMask(void)
{
    return(IO32ReadFldAlign(INTR_02, C_INTR_MASK));
}

void vDrvVsyncISRStatus(void)
{
    UINT16 u2MaskStatus;

    u2MaskStatus = vDrvVsyncISRGetMask();

    LOG(0,"====Interrupt status====\n");
    LOG(0,"[Interrupt Line] %d \n",vDrvVsyncISRGetPosition());
    LOG(0,"[Interrupt Mask] 0x%x \n",u2MaskStatus);
    if((u2MaskStatus >> 0) & 0x01)
    LOG(0,"[Interrupt Mask] disable LR_RISIN \n");
    if((u2MaskStatus >> 1) & 0x01)
    LOG(0,"[Interrupt Mask] disable LR_FALLING \n");
    if((u2MaskStatus >> 2) & 0x01)
    LOG(0,"[Interrupt Mask] disable L_RISING \n");
    if((u2MaskStatus >> 3) & 0x01)
    LOG(0,"[Interrupt Mask] disable L_FALLING \n");
    if((u2MaskStatus >> 4) & 0x01)
    LOG(0,"[Interrupt Mask] disable R_RISING \n");
    if((u2MaskStatus >> 5) & 0x01)
    LOG(0,"[Interrupt Mask] disable R_FALLING \n");
    if((u2MaskStatus >> 6) & 0x01)
    LOG(0,"[Interrupt Mask] disable OSTGLINE \n");
    if((u2MaskStatus >> 7) & 0x01)
    LOG(0,"[Interrupt Mask] disable AMBILIT \n");

}

void vDrvVsyncISRSetting(UINT16 u2Line, UINT16 u2Mask)
{
    vDrvVsyncISRSetPosition(u2Line);
    vDrvVsyncISRSetMask(u2Mask);

    if ((u2Line>wDrvGetOutputVTotal())&&(!(IS_PANEL_L12R12)))
    {
     LOG(0,"u2INTRLine(%d) over Panel V total (%d)\n",u2Line, wDrvGetOutputVTotal());
     LOG(0,"Interrupt will not active\n");
    }
}


UINT8 wDrvGetScalerDISPR_INStatus(void)
{
    if (fgIsMJCToOSTG())
    {
        return ((UINT8) u1PscGetInDispR());
    }
    else
    {
        return ((UINT8) u1ScpipGetInDispR());
    }
}

UINT8 wDrvGetScalerDISPR_OUTStatus(void)
{
    if (fgIsMJCToOSTG())
    {
        return ((UINT8) u1PscGetOutDispR());
    }
    else
    {
        return ((UINT8) u1ScpipGetOutDispR());
    }
}

UINT8 wDrvGetLVDSDISPR_OUTStatus(void)
{
    return ((UINT8) IO32ReadFldAlign(LVDSB_REG14, ST_LVDS_DISP_R));
}

#if defined(SUPPORT_PANEL_GRAPH_MODE)
UINT32 u4GetGraphFrameRate(void)
{
    return (UINT32)GFXTimingInfo.u1FrameRate;
}

void vGetGraphModeScalerStatus(void)
{
    LOG(0,"===== [Graph Setting] =====\n");
    LOG(0,"Frame-rate = %d Hz\n", GFXTimingInfo.u1FrameRate);
    LOG(0,"[Hori] HTotal=%04d HActive=%4d HFP=%4d HSyncWidth=%3d HBP=%4d\n",
        (UINT16)u4ScpipGetPanelHtotal(E_GFX_TG),
        (UINT16)u4ScpipGetPanelWidth(E_GFX_TG),
        (UINT16)u4ScpipGetHsyncFp(E_GFX_TG),
        (UINT16)(u4ScpipGetPanelHtotal(E_GFX_TG) - u4ScpipGetPanelWidth(E_GFX_TG) - u4ScpipGetHsyncFp(E_GFX_TG) - u4ScpipGetHsyncBp(E_GFX_TG)),
        (UINT16)u4ScpipGetHsyncBp(E_GFX_TG)
        );
    LOG(0,"[Vert] VTotal=%04d VActive=%4d VFP=%4d VSyncWidth=%3d VBP=%4d\n",
        (UINT16)u4ScpipGetPanelVtotal(E_GFX_TG),
        (UINT16)u4ScpipGetPanelHeight(E_GFX_TG),
        (UINT16)u4ScpipGetVsyncFp(E_GFX_TG),
        (UINT16)(u4ScpipGetPanelVtotal(E_GFX_TG) - u4ScpipGetPanelHeight(E_GFX_TG) - u4ScpipGetVsyncFp(E_GFX_TG) - u4ScpipGetVsyncBp(E_GFX_TG)),
        (UINT16)u4ScpipGetVsyncBp(E_GFX_TG)
        );

    if(fgIsMJCToOSTG())
    {
        LOG(0,"OSD Blending:%s \n",(OSD_EXP_GetOSDMixPreMJCEn()?"Pre":"Post"));
    }
    else
    {
        LOG(0,"OSD No Blending \n");
    }
}
#endif

void vGetPostScalerStatus(void)
{
    UINT8 u1Cnt;

#ifndef CC_MTK_LOADER	
		LOG(0,"_fgESDProtect=%d\n", _fgESDProtect);
#endif

    LOG(0,"===== [Setting] ===== \n");
	#ifdef CC_FLIP_MIRROR_SUPPORT
	LOG(0,"Current flip %d  mir %d init %d \n",(u1FlipMirrorEnable & SYS_FLIP_CONFIG_ON),(u1FlipMirrorEnable & SYS_MIRROR_CONFIG_ON),u1Init);
	#endif
    LOG(0,"[HSync] Htoal=%d Hactive=%d  Hfp= %d Hbp= %d Hwidth=%d\n",
           wDrvGetOutputHTotal(),
           wDrvGetOutputHActive(),
           wDrvGetHsyncFp(),
           wDrvGetHsyncBp(),
           wDrvGetOutputHTotal()-wDrvGetOutputHActive()-wDrvGetHsyncFp()-wDrvGetHsyncBp()
    );
    LOG(0,"[VSync] Vtoal=%d Vactive=%d  Vfp= %d Vbp= %d Vwidth=%d\n",
           wDrvGetOutputVTotal(),
           wDrvGetOutputVActive(),
           wDrvGetVsyncFp(),
           wDrvGetVsyncBp(),
           wDrvGetOutputVTotal()-wDrvGetOutputVActive()-wDrvGetVsyncFp()-wDrvGetVsyncBp()
    );
    if (bSI_DISPLAY_DCLK_TYPE == SV_DCLK_48HZ)
    LOG(0,"[Frame rate] SV_DCLK_48HZ\n");
    else if  (bSI_DISPLAY_DCLK_TYPE == SV_DCLK_50HZ)
     LOG(0,"[Frame rate] SV_DCLK_50HZ\n");
    else if  (bSI_DISPLAY_DCLK_TYPE == SV_DCLK_60HZ)
     LOG(0,"[Frame rate] SV_DCLK_60HZ\n");
    else if  (bSI_DISPLAY_DCLK_TYPE == SV_DCLK_OTHERS)
     LOG(0,"[Frame rate] SV_DCLK_OTHERS\n");
    else if  ( bSI_DISPLAY_DCLK_TYPE == SV_DCLK_UNKNOW)
     LOG(0,"[Frame rate] SV_DCLK_UNKNOW\n");

     LOG(0,"_fgPanelDynamicChg =%d\n", _fgPanelDynamicChg);
     LOG(0,"_bForceFrameRatee =%d Hz\n", _bForceFrameRate);
     LOG(0,"fgDisplayFreeRun =%d \n", fgDisplayFreeRun);

     LOG(0,"Output refresh rate =%d Hz\n", vDrvGetLCDFreq());

     LOG(0,"===== [Status] ===== \n");
    LOG(0,"HTotal =%d\n", wDrvGetOutputHTotalStatus());
    LOG(0,"VTotal =%d\n", wDrvGetOutputVTotalStatus());
    LOG(0,"DISP_R_IN =%d\n", wDrvGetScalerDISPR_INStatus());
    LOG(0,"DISP_R_OUT=%d\n", wDrvGetScalerDISPR_OUTStatus());

    LOG(0,"===== [OSTG Status] ===== \n");
    LOG(0,"[Input]DISP_R=%d\n", (UINT16)IO32ReadFldAlign(DEBUG_01, DEBUG_01_PANEL_DISP_R));
    LOG(0,"[Input]HTotal =%d\n", (UINT16)IO32ReadFldAlign(DEBUG_02, DEBUG_02_INPUT_HTOTAL));
    LOG(0,"[Input]VTotal =%d\n", (UINT16)IO32ReadFldAlign(DEBUG_02, DEBUG_02_INPUT_VTOTAL));

    LOG(0,"[Color]HTotal =%d\n", (UINT16)IO32ReadFldAlign(TIMING_DETECTION_0, COLOR_HTOTAL_CNT));
    LOG(0,"[Color]HActive =%d\n", (UINT16)IO32ReadFldAlign(TIMING_DETECTION_1, COLOR_HDE_CNT));
    LOG(0,"[Color]VTotal =%d\n", (UINT16)IO32ReadFldAlign(TIMING_DETECTION_0, COLOR_VTOTAL_CNT));
    LOG(0,"[Color]VActive =%d\n", (UINT16)IO32ReadFldAlign(TIMING_DETECTION_1, COLOR_VDE_CNT));

    vIO32WriteFldAlign(DEBUG_00, 1, DEBUG_00_R_DEBUG_OSTG_EN);
    HAL_Delay_us(20000);

    for (u1Cnt = 0; u1Cnt<4; u1Cnt++)
    {
      vIO32WriteFldAlign(DEBUG_01, u1Cnt, DEBUG_01_R_HVM_INPUT_SEL);
      HAL_Delay_us(40000);

      if (IO32ReadFldAlign(DEBUG_01,DEBUG_01_R_HVM_INPUT_SEL) == 0)
      {
       LOG(0,"The interface between OD to OSTG (default)\n");
      }
      else if (IO32ReadFldAlign(DEBUG_01,DEBUG_01_R_HVM_INPUT_SEL) == 1)
      {
       LOG(0,"The interface between OSTG to OD\n");
      }
      else if (IO32ReadFldAlign(DEBUG_01,DEBUG_01_R_HVM_INPUT_SEL) == 2)
      {
       LOG(0,"The interface between MJC to OSTG\n");
      }
      else if (IO32ReadFldAlign(DEBUG_01,DEBUG_01_R_HVM_INPUT_SEL) == 3)
      {
       LOG(0,"The input of OSTG (Note: this option will be fail when project exists MJC)\n");
      }

     LOG(0,"[Output]HTotal =%d\n", IO32ReadFldAlign(DEBUG_03, DEBUG_03_HVM_HTOTAL_CNT));
     LOG(0,"[Output]H Active =%d\n", IO32ReadFldAlign(DEBUG_04, DEBUG_04_HVM_HDE_CNT));

     LOG(0,"[Output]VTotal =%d\n", IO32ReadFldAlign(DEBUG_03, DEBUG_03_HVM_VTOTAL_CNT));
     LOG(0,"[Output]V Active =%d\n", IO32ReadFldAlign(DEBUG_04, DEBUG_04_HVM_VDE_CNT));
    }

    vIO32WriteFldAlign(DEBUG_00, 0, DEBUG_00_R_DEBUG_OSTG_EN);

}


void vGetDISP_RStatus(void)
{
    LOG(0,"==[DISP_R Setting - Normal Mode] == \n");
    if(IO32ReadFldAlign(LVDSB_REG06,RG_RES_FLD))
    LOG(0,"DISP_R Encode in A Channel\n");
    if(IO32ReadFldAlign(LVDSB_REG18,RG_RES_FIELD_L))
    LOG(0,"DISP_R Encode in B Channel\n");
    if(IO32ReadFldAlign(LVDSB_REG19,RG_RES_FIELD_U_4CH))
    LOG(0,"DISP_R Encode in C Channel\n");
    if(IO32ReadFldAlign(LVDSB_REG19,RG_RES_FIELD_L_4CH))
    LOG(0,"DISP_R Encode in D Channel\n");

    LOG(0,"==[DISP_R Setting - OSD Mode] == \n");
    if(IO32ReadFldAlign(LVDSB_REG08,RG_OSD_3D_IN_HS))
    LOG(0,"DISP_R Encode in HSync\n");
    if(IO32ReadFldAlign(LVDSB_REG08,RG_OSD_3D_IN_VS))
    LOG(0,"DISP_R Encode in VSync\n");

    LOG(0,"==[DISP_R Status] == \n");
    LOG(0,"[Scaler] DISP_R_IN =%d\n", wDrvGetScalerDISPR_INStatus());
    LOG(0,"[Scaler] DISP_R_OUT=%d\n", wDrvGetScalerDISPR_OUTStatus());
    LOG(0,"[LVDS] DISP_R_OUT=%d\n", wDrvGetLVDSDISPR_OUTStatus());
}

void u1SetDISP_REncodeEn(UINT8 u1Enable)
{
    vIO32WriteFldAlign(LVDSB_REG06,(u1Enable>>0 & 0x01),RG_RES_FLD);
    vIO32WriteFldAlign(LVDSB_REG18,(u1Enable>>1 & 0x01),RG_RES_FIELD_L);
    vIO32WriteFldAlign(LVDSB_REG19,(u1Enable>>2 & 0x01),RG_RES_FIELD_U_4CH);
    vIO32WriteFldAlign(LVDSB_REG19,(u1Enable>>3 & 0x01),RG_RES_FIELD_L_4CH);
    vIO32WriteFldAlign(LVDSB_REG08,(u1Enable>>4 & 0x01),RG_OSD_3D_IN_HS);
    vIO32WriteFldAlign(LVDSB_REG08,(u1Enable>>5 & 0x01),RG_OSD_3D_IN_VS);
    if ((u1Enable>>4 & 0x01)==1 || (u1Enable>>5 & 0x01))
    {
     vIO32WriteFldAlign(LVDSB_REG06,0x01,RG_RES_FLD);
    }
}

void u1SetDISP_REncodeEn_L4(BOOL bValue)
{
    vIO32WriteFldAlign(LVDSB_REG06,bValue,RG_RES);
}

void u1SetDISP_REncodeEnHW_L3(BOOL bValue)
{
    vIO32WriteFldAlign(LVDSB_REG06,bValue,RG_RES_FLD);
    vIO32WriteFldAlign(LVDSB_REG18,bValue,RG_RES_FIELD_L);
    vIO32WriteFldAlign(LVDSB_REG19,bValue,RG_RES_FIELD_L_4CH);
    vIO32WriteFldAlign(LVDSB_REG19,bValue,RG_RES_FIELD_U_4CH);
}

void u1SetDISP_REncodeEnHW_L4(BOOL bValue)
{
    vIO32WriteFldAlign(LVDSB_REG19,bValue,RG_RES2_FLD);
    vIO32WriteFldAlign(LVDSB_REG19,bValue,RG_RES2_FLD_L);
    vIO32WriteFldAlign(LVDSB_REG19,bValue,RG_RES2_FLD_L_4CH);
    vIO32WriteFldAlign(LVDSB_REG19,bValue,RG_RES2_FLD_U_4CH);
}

static void vSetOSD_IndexEncodeEnable(BOOL bEn)
{
    vIO32WriteFldAlign(LVDSB_REG19,bEn,RG_RES_OSD_INDEX);
    vIO32WriteFldAlign(LVDSB_REG19,bEn,RG_RES_OSD_INDEX_L);
    vIO32WriteFldAlign(LVDSB_REG19,bEn,RG_RES_OSD_INDEX_U_4CH);
    vIO32WriteFldAlign(LVDSB_REG19,bEn,RG_RES_OSD_INDEX_L_4CH);
}

void vGetLVDS_HVEncodeStatus(void)
{
    LOG(0,"==[LVDS H/V Encode Value] == \n");
    if (IO32ReadFldAlign(LVDSB_REG06,RG_VS_SEL))
    {
     LOG(0,"[VSYNC Enocde] Vsync encode value=%d\n", (UINT16)IO32ReadFldAlign(LVDSB_REG06,RG_CNTLF));
    }
    if (IO32ReadFldAlign(LVDSB_REG06,RG_HS_SEL))
    {
     LOG(0,"[HSYNC Enocde] Hsync encode value=%d\n", (UINT16)IO32ReadFldAlign(LVDSB_REG06,RG_CNTLE));
    }
}

void u1SetLVDS_HVEncodeValue(UINT8 u1Enable)
{
    vIO32WriteFldAlign(LVDSB_REG06,(u1Enable>>0 & 0x01),RG_VS_SEL);
    vIO32WriteFldAlign(LVDSB_REG06,(u1Enable>>1 & 0x01),RG_CNTLF);
    vIO32WriteFldAlign(LVDSB_REG06,(u1Enable>>2 & 0x01),RG_HS_SEL);
    vIO32WriteFldAlign(LVDSB_REG06,(u1Enable>>3 & 0x01),RG_CNTLE);
}

BOOL fgTconISRNeedRst = SV_FALSE;
extern BOOL fgIsVsyncIsrStart;

extern UINT8 fgApplyScanPWMSetData;
extern UINT8 u1ScanPwmCrossFlg;

vDisplayTimingCB fpDrvRegisterDisplayTimingUpdateCB(E_TIMEGEN_PATH ePath, E_DISPLAY_TIMING_CB eType, vDisplayTimingCB fpDispTimingCB)
{
    vDisplayTimingCB fpPreCB = DiplayTimingCB[ePath][eType];
    DiplayTimingCB[ePath][eType] = fpDispTimingCB;

    return fpPreCB;
}

#if defined(CC_MT5893) || defined(CC_MT5895)
#ifdef CC_SUPPORT_4KBE
void vDrvVB1ControlBitInit(void)
{
    if (PANEL_GetDisplayType()== PANEL_DISPLAY_TYPE_VB1)
    {
        vDrvVByOneCrtlBitCBV(SV_OFF);
        vDrvVByOneCrtlBitTRIG(SV_OFF);// 1:effective immediately 0:effective until next V
        vDrvVByOneCrtlBitEnable(SV_ON);// 1:use different register 4 lane 0:use one lane
        vDrvVByOneCrtlBitDisTRIG(SV_OFF);// 1:do not effective 0:disable
        vDrvVByOneCrtlBitVEDGE(eVB1_ControlBit_FALLING);
    }
}

void vDrvSetVB1ControlBit(void)
{
    if (PANEL_GetDisplayType() == PANEL_DISPLAY_TYPE_VB1)
    {
        vDrvVByOneCrtlBitPicHSize(gVB1ControlBit.u4PicHSize);
        vDrvVByOneCrtlBitPicVSize(gVB1ControlBit.u4PicVSize);
        vDrvVByOneCrtlBitCropHPos(gVB1ControlBit.u4CropHPosition);
        vDrvVByOneCrtlBitCropVPos(gVB1ControlBit.u4CropVPosition);
        vDrvVByOneCrtlBitCropHSize(gVB1ControlBit.u4CropHSize);
        vDrvVByOneCrtlBitCropVSize(gVB1ControlBit.u4CropVSize);
        vDrvVByOneCrtlBitAT(gVB1ControlBit.u1AspectType);
        vDrvVByOneCrtlBitAR(gVB1ControlBit.u1AspectRatio);
        vDrvVByOneCrtlBitSarHSize(gVB1ControlBit.u4SarHSize);
        vDrvVByOneCrtlBitSarVSize(gVB1ControlBit.u4SarVSize);
        vDrvVByOneCrtlBitCT(gVB1ControlBit.u1CodeType);
        vDrvVByOneCrtlBitFNI(gVB1ControlBit.u2FrameNum);
        vDrvVByOneCrtlBitFeSocType(SOC_TYPE_M5);
        //vDrvVByOneCrtlBitCBV(gVB1ControlBit.u1CtrlBitValid);
    }
}

void vDrvUpdateVB1ControlBit(CONTROL_BIT_VB1_T* ptVB1ControlBit)
{
    if (ptVB1ControlBit == NULL)
    {
       LOG(0, "Error :ptVB1ControlBit == NULL\n");
       return; 
    }

    LOG(2, "B2R Update the Bypass Control Bit Info\n");
    x_memcpy((void*)(&gVB1ControlBit), (void*)ptVB1ControlBit, sizeof(CONTROL_BIT_VB1_T));
    E_VB1ControlBit_Trig_Sta = E_TIMING_TRIGGER_STATE_CTRL_BIT;
}

void vDrvUpdateVB1ControlBitInternal(void)
{
    LOG(2, "Update the Bypass Control Bit Info\n");

    gVB1ControlBit.u4PicHSize = 3840;
    gVB1ControlBit.u4PicVSize = 2160;

    E_VB1ControlBit_Trig_Sta = E_TIMING_TRIGGER_STATE_CTRL_BIT;
}

void vDrvdumpControlBitinfo(void)
{
    if (PANEL_GetDisplayType() == PANEL_DISPLAY_TYPE_VB1)
    {
        LOG(0, "===========VB1 Control Bit Info============\n");
        LOG(0, "u1VideoPath %d    u1CtrlBitValid %d    u13DStructure %d\n",gVB1ControlBit.u1VideoPath,gVB1ControlBit.u1CtrlBitValid,gVB1ControlBit.u13DStructure);
        LOG(0, "u1Range     %d    u1DoubleRF     %d    u1ColorFormat %d\n",gVB1ControlBit.u1Range,gVB1ControlBit.u1DoubleRF,gVB1ControlBit.u1ColorFormat);
        LOG(0, "u1CodeType  %d    u1AspectType   %d    u1AspectRatio %d\n",gVB1ControlBit.u1CodeType,gVB1ControlBit.u1AspectType,gVB1ControlBit.u1AspectRatio);
        LOG(0, "u4SarHSize  %d    u4SarVSize     %d    u4PicHSize    %d  u4PicVSize   %d\n",gVB1ControlBit.u4SarHSize,gVB1ControlBit.u4SarVSize,gVB1ControlBit.u4PicHSize,gVB1ControlBit.u4PicVSize);
        LOG(0, "u4CropHPos  %d    u4CropVPos     %d    u4CropHSize   %d  u4CropVSize  %d\n",gVB1ControlBit.u4CropHPosition,gVB1ControlBit.u4CropVPosition,gVB1ControlBit.u4CropHSize,gVB1ControlBit.u4CropVSize);

        #if defined(SUPPORT_PANEL_GRAPH_MODE)
        LOG(0, "===========GFX Control Bit Info===============\n");
        LOG(0, "ValidFlag    %d       Graphic Format  %d\n",gGFXControlBit.u4CBVF,gGFXControlBit.u4CBGF);
        LOG(0, "HStart Pos   %d     VStart Pos      %d\n",gGFXControlBit.u4CBHSPos,gGFXControlBit.u4CBVSPos);
        LOG(0, "HTotal       %d    VTotal          %d\n",gGFXControlBit.u4CBHTOTAL,gGFXControlBit.u4CBVTOTAL);
        #endif       
    }
    else
    {
        LOG(0, "Error :PANEL_DISPLAY_TYPE_VB1 = 0\n");
    }
}

void vDrvSetControlBitinfo(UINT8 u1path,UINT8 u1sel2,UINT32 u4value)
{
    if (PANEL_GetDisplayType() == PANEL_DISPLAY_TYPE_VB1)
    {
        if(u1path == 0) // vbo control bit
        {
            switch (u1sel2)
            {
                case E_CONTROL_BIT_CB_3DST:
                     gVB1ControlBit.u13DStructure = u4value;
                     break;
                case E_CONTROL_BIT_CB_HSZ:
                     gVB1ControlBit.u4PicHSize = u4value;
                     break;
                case E_CONTROL_BIT_CB_VSZ:
                     gVB1ControlBit.u4PicVSize = u4value;
                     break;
                case E_CONTROL_BIT_CB_CRHP:
                     gVB1ControlBit.u4CropHPosition = u4value;
                     break;
                case E_CONTROL_BIT_CB_CRVP:
                     gVB1ControlBit.u4CropVPosition = u4value; 
                     break;
                case E_CONTROL_BIT_CB_CRHSZ:
                     gVB1ControlBit.u4CropHSize = u4value;
                     break;
                case E_CONTROL_BIT_CB_CRVSZ:
                     gVB1ControlBit.u4CropVSize = u4value;
                     break;
                case E_CONTROL_BIT_CB_AT:
                     gVB1ControlBit.u1AspectType = u4value;
                     break;
                case E_CONTROL_BIT_CB_AR:
                     gVB1ControlBit.u1AspectRatio = u4value;
                     break;
                case E_CONTROL_BIT_CB_SHSZ:
                     gVB1ControlBit.u4SarHSize = u4value;
                     break;
                case E_CONTROL_BIT_CB_SVSZ:
                     gVB1ControlBit.u4SarVSize = u4value;
                     break;
                case E_CONTROL_BIT_CB_CT:
                     gVB1ControlBit.u1CodeType = u4value;
                     break;
                case E_CONTROL_BIT_CB_RF:
                     gVB1ControlBit.u1DoubleRF = u4value;
                     break;
                case E_CONTROL_BIT_CB_VP:
                     gVB1ControlBit.u1VideoPath = u4value;
                     break;
                case E_CONTROL_BIT_CB_CF:
                     gVB1ControlBit.u1ColorFormat = u4value;
                     break;
                case E_CONTROL_BIT_CB_RA:
                     gVB1ControlBit.u1Range = u4value;
                     break;
                case E_CONTROL_BIT_CB_CBV:
                     gVB1ControlBit.u1CtrlBitValid = u4value;
                     break;
                default:
                     break;
            }
            E_VB1ControlBit_Trig_Sta = E_TIMING_TRIGGER_STATE_CTRL_BIT;                
        }
        
        #if defined(SUPPORT_PANEL_GRAPH_MODE)
        if (u1path == 1) // gfx control bit
        {
            switch (u1sel2)
            {
                case E_CONTROL_BIT_CB_HTOTAL:
                     gGFXControlBit.u4CBHTOTAL = u4value;
                     break;
                case E_CONTROL_BIT_CB_VTOTAL:
                     gGFXControlBit.u4CBVTOTAL = u4value;
                     break;
                case E_CONTROL_BIT_CB_HSPos:
                     gGFXControlBit.u4CBHSPos = u4value;
                     break;
                case E_CONTROL_BIT_CB_VSPos:
                     gGFXControlBit.u4CBVSPos = u4value;
                     break;
                case E_CONTROL_BIT_CB_GF:
                     gGFXControlBit.u4CBGF = u4value;
                     break;
                case E_CONTROL_BIT_CB_VF:
                     gGFXControlBit.u4CBVF = u4value;
                     break;                    
                default:
                    break;
            } 
            GFXTimingInfo.u1TriggerState = (UINT8)E_TIMING_TRIGGER_STATE_CTRL_BIT;
        }
        #endif         
    }
    else
    {
        LOG(0, "Error :PANEL_DISPLAY_TYPE_VB1 = 0\n");
    }
    
}

void vDrvControlBitinfoTrigger(UINT8 u1sel)
{
	if (PANEL_GetDisplayType() == PANEL_DISPLAY_TYPE_VB1)
	{
		if (u1sel == 0)
	    {
			vDrvSetVB1ControlBit();
	    }
	    
	    else if(u1sel == 1)
	    {
	    #if defined(SUPPORT_PANEL_GRAPH_MODE)
			#ifdef LOAD_PANEL_FROM_FLASH_CUSTOM
			if (FlashPanel_GetControlWord2_GraphModeEn())
			#else
			if (IS_SUPPORT_VB1_GRAPH_MODE)
			#endif
			{
	       		vDrvSetGFXControlBit();
			}
		#endif
	    }
	    
	    else if (u1sel == 2)
	    {
	        vDrvSetVB1ControlBit();
	    #if defined(SUPPORT_PANEL_GRAPH_MODE)
			#ifdef LOAD_PANEL_FROM_FLASH_CUSTOM
			if (FlashPanel_GetControlWord2_GraphModeEn())
			#else
			if (IS_SUPPORT_VB1_GRAPH_MODE)
			#endif
			{
	        	vDrvSetGFXControlBit(); 
			}
	    #endif
	    }
	}
	else
	{
	    LOG(0, "Error :PANEL_DISPLAY_TYPE_VB1 = 0\n");
	}
    
}
#endif
#endif

UINT8 fgIsOSDFromGFX(void)
{
	if(IO32ReadFldAlign(SCPIP_SCSYS_14,SCSYS_14_SC_OSD_SRC_SEL)==2)
	{
		return 1;
	}
	else 
	{
		return 0;
	}
}

#if defined(CC_MT5893) || defined(CC_MT5895)
void vDrvSwitchOSDRoute(void)
{
#ifdef LOAD_PANEL_FROM_FLASH_CUSTOM
	if (FlashPanel_GetControlWord2_GraphModeEn() != 0)
#else
    if(IS_SUPPORT_VB1_GRAPH_MODE != 0)
#endif
	{
	     LOG(0, "vDrvSwitchOSDRoute = 2\n");  
	     vIO32WriteFldAlign(SCPIP_SCSYS_14, 0x2, SCSYS_14_SC_OSD_SRC_SEL);
	  //vIO32WriteFldAlign(GC_MIX_02, 0x0, GC_MIX_02_VIDEO_SEL_MAIN); 
	  //vIO32WriteFldAlign(GC_MIX_02, 0x0, GC_MIX_02_TIMING_SEL_MAIN); // from GFX timing GEN
	}
	else
	{
	     LOG(0, "vDrvSwitchOSDRoute = %d    pnl ctrl word=0x%x\n",fgIsMJCToOSTG(),PANEL_GetControlWord2());
        if(GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60_SRS)
        {
            vIO32WriteFldAlign(SCPIP_SCSYS_14, 0x2, SCSYS_14_SC_OSD_SRC_SEL);
        }
        else
        {
        	//it need set 0,because the hw have change to select by osd.
            vIO32WriteFldAlign(SCPIP_SCSYS_14, 0x0, SCSYS_14_SC_OSD_SRC_SEL);
            //vIO32WriteFldAlign(SCPIP_SCSYS_14, fgIsMJCToOSTG()? 0x1 : 0x0, SCSYS_14_SC_OSD_SRC_SEL);
        }

	}
}
#endif

#if defined(SUPPORT_PANEL_GRAPH_MODE)
void vDrvGFXInit(void)
{
    if (PANEL_GetDisplayType()== PANEL_DISPLAY_TYPE_VB1)
    {
        //vDrvVByOneGMCrtlBitVF(SV_OFF);
        vDrvVByOneGMCrtlBitTRIG(SV_OFF); // 1:effective immediately 0:effective until next V
        vDrvVByOneGMCrtlBitEnable(SV_ON);// 1:use different register 4 lane 0:use one lane
        vDrvVByOneGMCrtlBitDisTRIG(SV_OFF);// 1:do not effective 0:disable
        vDrvVByOneGMCrtlBitVEDGE(eVB1_ControlBit_FALLING);
    }
}

void vDrvSetGFXControlBit(void)
{
    if (PANEL_GetDisplayType() == PANEL_DISPLAY_TYPE_VB1)
    {
        //set register
        //vDrvVByOneGMCrtlBitVF(gGFXControlBit.u4CBVF);
        vDrvVByOneGMCrtlBitGF(gGFXControlBit.u4CBGF);
        vDrvVByOneGMCrtlBitHTotal(gGFXControlBit.u4CBHTOTAL);
        vDrvVByOneGMCrtlBitVTotal(gGFXControlBit.u4CBVTOTAL);
        vDrvVByOneGMCrtlBitHDEStart(gGFXControlBit.u4CBHSPos);
        vDrvVByOneGMCrtlBitVDEStart(gGFXControlBit.u4CBVSPos);
    }
}

void vDrvUpdateGFXControlBit(void)
{
    if (PANEL_GetDisplayType() == PANEL_DISPLAY_TYPE_VB1)
    {
        //update structrue
        gGFXControlBit.u4CBVF = SV_ON;
        gGFXControlBit.u4CBGF = GFXTimingInfo.u4VActive > 1080;
        gGFXControlBit.u4CBHTOTAL = GFXTimingInfo.u4HTotal;
        gGFXControlBit.u4CBVTOTAL = GFXTimingInfo.u4VTotal;
        gGFXControlBit.u4CBHSPos = GFXTimingInfo.u4HSyncWidth + GFXTimingInfo.u4HBP;
        gGFXControlBit.u4CBVSPos = GFXTimingInfo.u4VSyncWidth + GFXTimingInfo.u4VBP;
    }
    
}


void vDrvSwitchGFXTiming(UINT32 u4Type)
{
#ifdef LOAD_PANEL_FROM_FLASH_CUSTOM
	if (FlashPanel_GetControlWord2_GraphModeEn() == 0)
#else
    if (IS_SUPPORT_VB1_GRAPH_MODE == 0)
#endif
    {
        LOG(0,"@MTK_DISP [vDrvSwitchGFXTiming] Not support Vx1 graph mode %d\n",u4Type);
        return;
    }

    if (u4Type == GraphMode_GetPanelType())
    {
        LOG(0,"@MTK_DISP [vDrvSwitchGFXTiming] The same graph mode %d\n",u4Type);
        return;
    }

	LOG(0,"@MTK_DISP [vDrvSwitchGFXTiming] panel_type = %d\n", u4Type);

    GraphMode_SetPanelType(u4Type);

    #if (defined(CC_MT5893) || defined(CC_MT5895)) && (!defined(CC_MTK_LOADER))
    vSetPlaneEnable(VDP_GFXOUT, SV_ON);
    if (u4Type == PANEL_TYPE_4K1K_60)
    {
        vSetPlaneWidth(VDP_GFXOUT,  1920);
        vSetPlaneHeight(VDP_GFXOUT, 1080);
    }
    if (u4Type == PANEL_TYPE_4K1K_120)
    {
        vSetPlaneWidth(VDP_GFXOUT,  3840);
        vSetPlaneHeight(VDP_GFXOUT, 1080);
    }
    else if (u4Type == PANEL_TYPE_4K2K_30 || u4Type == PANEL_TYPE_4K2K_60  )
    {     
        vSetPlaneWidth(VDP_GFXOUT,  3840);
        vSetPlaneHeight(VDP_GFXOUT, 2160);
    }
    else if ((u4Type == PANEL_TYPE_2K1K_60)||(u4Type == PANEL_TYPE_2K1K_120))
    {
        vSetPlaneWidth(VDP_GFXOUT,  1920);
        vSetPlaneHeight(VDP_GFXOUT, 1080);
    }
	
	if(IS_DISP_4K2K_TYPE3)
	{
		UINT32 srcW=0,srcH=0,dstW=0,dstH=0,Enable=0,u4bpp=0;
		UINT32 u4Width = u4GetPlaneWidth(VDP_GFXOUT);
		UINT32 u4Height = u4GetPlaneHeight(VDP_GFXOUT);
		OSD_SC_GetScalerInfo(OSD_SCALER_3,&Enable,&srcW,&srcH,&dstW,&dstH,&u4bpp);
		OSD_SC_Scale(OSD_SCALER_3,Enable,srcW,srcH,u4Width,u4Height);
		OSD_SC_GetScalerInfo(OSD_SCALER_2,&Enable,&srcW,&srcH,&dstW,&dstH,&u4bpp);
		OSD_SC_Scale(OSD_SCALER_2,Enable,srcW,srcH,u4Width,u4Height);
		OSD_SC_GetScalerInfo(OSD_SCALER_1,&Enable,&srcW,&srcH,&dstW,&dstH,&u4bpp);
		OSD_SC_Scale(OSD_SCALER_1,Enable,srcW,srcH,u4Width,u4Height);
    }	
    #endif
    
    GFXTimingInfo.u1DCLKType = bSI_DISPLAY_DCLK_TYPE;
    vDrvSetGFXTiming(GFXTimingInfo.u1DCLKType);
}

void vDrvSetGFXTiming(UINT8 u1GfxDclkType)
{
    GFXTimingInfo.u1DCLKType = u1GfxDclkType;

	if (u1GfxDclkType == SV_DCLK_48HZ)
	{
		u1GfxDclkType = u1DrvCal48HzOutputType();
	}

    switch (u1GfxDclkType)
    {
		case SV_DCLK_48HZ:
			GFXTimingInfo.u1FrameRate = 48;
			break;

		case SV_DCLK_50HZ:
			GFXTimingInfo.u1FrameRate = 50;
			break;

		case SV_DCLK_60HZ:
		default:
			GFXTimingInfo.u1FrameRate = 60;
			break;
    }

    if (GraphMode_GetFrameRateMax() <= 30)
    {
        GFXTimingInfo.u1FrameRate >>= 1;
    }

    if (GFXTimingInfo.u4HTotal == GRAPHMODE_GetHtotalByTiming(GFXTimingInfo.u1DCLKType) &&
        GFXTimingInfo.u4VTotal == GRAPHMODE_GetVtotalByTiming(GFXTimingInfo.u1DCLKType))
    {
        if(!fgForceGfxTimingUpdate)
        {
			LOG(1, "[vDrvSetGFXTiming] return!\n");
			return;
        }
		else
		{
			LOG(0,"[GFX]Disable fgForceGfxTimingUpdate AFTER resume!!\n");
			vDrvSetGfxTimingForceUpdated(FALSE);
		}
    }

    {
        #ifndef CC_MTK_LOADER
        CRIT_STATE_T csState;
        csState = x_crit_start();
        #endif
        GFXTimingInfo.u4HTotal      = GRAPHMODE_GetHtotalByTiming(GFXTimingInfo.u1DCLKType);
        GFXTimingInfo.u4HActive     = GRAPHMODE_GetPanelWidth();
        GFXTimingInfo.u4HFP         = GRAPHMODE_GetHSyncFpByTiming(GFXTimingInfo.u1DCLKType);
        GFXTimingInfo.u4HSyncWidth  = GRAPHMODE_GetHSyncWidthByTiming(GFXTimingInfo.u1DCLKType);
        GFXTimingInfo.u4HBP         = GRAPHMODE_GetHSyncBpByTiming(GFXTimingInfo.u1DCLKType);

        GFXTimingInfo.u4VTotal      = GRAPHMODE_GetVtotalByTiming(GFXTimingInfo.u1DCLKType);
        GFXTimingInfo.u4VActive     = GRAPHMODE_GetPanelHeight();
        GFXTimingInfo.u4VFP         = GRAPHMODE_GetVSyncFpByTiming(GFXTimingInfo.u1DCLKType);
        GFXTimingInfo.u4VSyncWidth  = GRAPHMODE_GetVSyncWidthByTiming(GFXTimingInfo.u1DCLKType);
        GFXTimingInfo.u4VBP         = GRAPHMODE_GetVSyncBpByTiming(GFXTimingInfo.u1DCLKType);

        vDrvUpdateGFXControlBit();
        #ifndef CC_MTK_LOADER
        x_crit_end(csState);
        #endif
    }

    #ifndef CC_MTK_LOADER
    DRVCUST_SendEvent(E_CUST_OUT_FMT_CHANGE, E_CUST_BE_TX_PATH_GFX);
    if ((fgIsVsyncIsrStart) && (_fgVOPLLPowerOn))
    {
        GFXTimingInfo.u1TriggerState = (UINT8)E_TIMING_TRIGGER_STATE_CTRL_BIT;
    }
    else
    #endif
    {
        vDrvSetGFXControlBit();
        vDrvVByOneGMCrtlBitVF(SV_ON);
        vDrvApplyGFXTiming();
    }
}

void vDrvApplyGFXTiming(void)
{
   #ifndef CC_MTK_LOADER
	//CRIT_STATE_T csState;
   #endif
    UINT32 newGfxVFp = GFXTimingInfo.u4VFP;

    LOG(2, "vDrvApplyGFXTiming: state = %d\n", GFXTimingInfo.u1TriggerState);

    if(u4ScpipGetVTModeCtrl(E_GFX_TG) == E_TRK_FRONT_PORCH)
    {
        newGfxVFp = GFXTimingInfo.u4VTotal - GFXTimingInfo.u4VActive - GFXTimingInfo.u4VBP - GFXTimingInfo.u4VFP;
    }
   
    switch (GFXTimingInfo.u1TriggerState)
    {
        case E_TIMING_TRIGGER_STATE_CTRL_BIT:
            vDrvVByOneGMCrtlBitVF(SV_OFF);
            vDrvSetGFXControlBit();
            GFXTimingInfo.u1TriggerState = (UINT8)E_TIMING_TRIGGER_STATE_TIMING;
            break;

        case E_TIMING_TRIGGER_STATE_VALID:
            vDrvVByOneGMCrtlBitVF(SV_ON);
            GFXTimingInfo.u1TriggerState = (UINT8)E_TIMING_TRIGGER_STATE_IDLE;
			#ifndef CC_MTK_LOADER
				vScpipGfxSetData(SV_ON);
				vScpipRefSetData(ON);
			#endif
            break;

        case E_TIMING_TRIGGER_STATE_WAITING:
            break;

        case E_TIMING_TRIGGER_STATE_TIMING:
            if (GraphMode_GetPanelType() == PANEL_TYPE_4K2K_30)
            {
                vDrvVByOneGMCrtlBitVF(SV_ON);
            }
            else
            {
                GFXTimingInfo.u1TriggerState = (UINT8)E_TIMING_TRIGGER_STATE_VALID;
            }
            //GFXTimingInfo.u1TriggerState = E_TIMING_TRIGGER_STATE_IDLE;
            OSD_TimingNfy(); // Notify OSD GFX Timing changed	

            DiplayTimingCB[E_TIMEGEN_PATH_GFX][E_DISPLAY_TIMING_CB_H_ACTIVE](GFXTimingInfo.u4HActive);
            DiplayTimingCB[E_TIMEGEN_PATH_GFX][E_DISPLAY_TIMING_CB_H_TOTAL](GFXTimingInfo.u4HTotal);
            DiplayTimingCB[E_TIMEGEN_PATH_GFX][E_DISPLAY_TIMING_CB_H_FP](GFXTimingInfo.u4HFP);
            DiplayTimingCB[E_TIMEGEN_PATH_GFX][E_DISPLAY_TIMING_CB_H_BP](GFXTimingInfo.u4HBP);

            DiplayTimingCB[E_TIMEGEN_PATH_GFX][E_DISPLAY_TIMING_CB_V_ACTIVE](GFXTimingInfo.u4VActive);
            DiplayTimingCB[E_TIMEGEN_PATH_GFX][E_DISPLAY_TIMING_CB_V_TOTAL](GFXTimingInfo.u4VTotal);
            DiplayTimingCB[E_TIMEGEN_PATH_GFX][E_DISPLAY_TIMING_CB_V_FP](newGfxVFp);
            DiplayTimingCB[E_TIMEGEN_PATH_GFX][E_DISPLAY_TIMING_CB_V_BP](GFXTimingInfo.u4VBP);

            #ifdef CC_MTK_LOADER
            vScpipGfxSetData(SV_ON);
            #endif
            break;

        case E_TIMING_TRIGGER_STATE_IDLE:
        default:
            OSD_TimingNfy(); // Notify OSD GFX Timing changed	
            
            DiplayTimingCB[E_TIMEGEN_PATH_GFX][E_DISPLAY_TIMING_CB_H_ACTIVE](GFXTimingInfo.u4HActive);
            DiplayTimingCB[E_TIMEGEN_PATH_GFX][E_DISPLAY_TIMING_CB_H_TOTAL](GFXTimingInfo.u4HTotal);
            DiplayTimingCB[E_TIMEGEN_PATH_GFX][E_DISPLAY_TIMING_CB_H_FP](GFXTimingInfo.u4HFP);
            DiplayTimingCB[E_TIMEGEN_PATH_GFX][E_DISPLAY_TIMING_CB_H_BP](GFXTimingInfo.u4HBP);
            
            DiplayTimingCB[E_TIMEGEN_PATH_GFX][E_DISPLAY_TIMING_CB_V_ACTIVE](GFXTimingInfo.u4VActive);
            DiplayTimingCB[E_TIMEGEN_PATH_GFX][E_DISPLAY_TIMING_CB_V_TOTAL](GFXTimingInfo.u4VTotal);
            DiplayTimingCB[E_TIMEGEN_PATH_GFX][E_DISPLAY_TIMING_CB_V_FP](newGfxVFp);
            DiplayTimingCB[E_TIMEGEN_PATH_GFX][E_DISPLAY_TIMING_CB_V_BP](GFXTimingInfo.u4VBP);

            #if defined(CC_MTK_LOADER) || defined(CC_SONY_FILE_BASED_RECIPE_SUPPORT)
            vScpipGfxSetData(SV_ON);
            #endif

		
            break;
    }
}

void vDrvSetGFXTimingTriggerState(E_TIMING_TRIGGER_STATE eTriggerState)
{
    GFXTimingInfo.u1TriggerState = (UINT8)eTriggerState;
}

void vDrvSetGFXHActive(UINT16 u2Active)
{
    vScpipSetPanelWidth(E_GFX_TG, u2Active);
}

void vDrvSetGFXHTotal(UINT16 u2Total)
{
    vScpipSetPanelHtotal(E_GFX_TG, u2Total);
}

void vDrvSetGFXHFP(UINT16 u2FP)
{
    vScpipSetHsyncFp(E_GFX_TG, u2FP);
}

void vDrvSetGFXHBP(UINT16 u2BP)
{
    vScpipSetHsyncBp(E_GFX_TG, u2BP);
}

void vDrvSetGFXVActive(UINT16 u2Active)
{
    vScpipSetPanelHeight(E_GFX_TG, u2Active);
}

void vDrvSetGFXVTotal(UINT16 u2Total)
{
    UINT16 u2VRRVToal = u2Total;
#ifndef CC_MTK_LOADER
    if(bDrvIsHdmiVRRMode())
    {
        u2VRRVToal = DRV_MAX_VRR_VT;
    }
#endif

    vScpipSetPanelVtotal(E_GFX_TG, u2VRRVToal);
}

void vDrvSetGFXVFP(UINT16 u2FP)
{
    vScpipSetVsyncFp(E_GFX_TG, u2FP);
}

void vDrvSetGFXVBP(UINT16 u2BP)
{
    vScpipSetVsyncBp(E_GFX_TG, u2BP);
}
#endif

#ifndef CC_MTK_LOADER
void vDrvVB1VideoMuteTiming(void)
{
    static UINT8 u1VideoMuteCnt = 0;
    UINT8 MuteTh = IO32ReadFldAlign(BETEST_00,VB1_STABLE_TH);
    MuteTh = MAX(MuteTh,20);

    if(fgVB1VideoMute)
    {
        u1VideoMuteCnt++;
    }

    if (u1VideoMuteCnt == MuteTh)
    {
        //vDrvSetAllMute(SV_OFF);
        u1VideoMuteCnt = 0;
        fgVB1VideoMute = SV_FALSE;

        //pwm on
        vDrvSetSCANPWMforSPIOnOff(SV_TRUE);
        LOG(2, "UnMute now\n");
        LOG(2, "fgVB1VideoMute = %d\n", fgVB1VideoMute);
        DRVCUST_SendEvent(E_CUST_OUT_MUTE_CHANGE, 0);
    }
	
}

#else
void vDrvVB1VideoMuteTiming(void)
{
    
}
#endif

void vDrvRegisterDisplayCB(void)
{
    fpDrvRegisterDisplayTimingUpdateCB(E_TIMEGEN_PATH_PQ, E_DISPLAY_TIMING_CB_H_ACTIVE, wDrvSetOutputHActive);
    fpDrvRegisterDisplayTimingUpdateCB(E_TIMEGEN_PATH_PQ, E_DISPLAY_TIMING_CB_H_TOTAL, wDrvSetOutputHTotal);
    fpDrvRegisterDisplayTimingUpdateCB(E_TIMEGEN_PATH_PQ, E_DISPLAY_TIMING_CB_H_FP, vDrvSetHsyncFp);
    fpDrvRegisterDisplayTimingUpdateCB(E_TIMEGEN_PATH_PQ, E_DISPLAY_TIMING_CB_H_BP, vDrvSetHsyncBp);
    fpDrvRegisterDisplayTimingUpdateCB(E_TIMEGEN_PATH_PQ, E_DISPLAY_TIMING_CB_V_ACTIVE, wDrvSetOutputVActive);
    fpDrvRegisterDisplayTimingUpdateCB(E_TIMEGEN_PATH_PQ, E_DISPLAY_TIMING_CB_V_TOTAL, wDrvSetOutputVTotal);
    fpDrvRegisterDisplayTimingUpdateCB(E_TIMEGEN_PATH_PQ, E_DISPLAY_TIMING_CB_V_FP, vDrvSetVsyncFp);
    fpDrvRegisterDisplayTimingUpdateCB(E_TIMEGEN_PATH_PQ, E_DISPLAY_TIMING_CB_V_BP, vDrvSetVsyncBp);


#if defined(SUPPORT_PANEL_GRAPH_MODE)
	#ifdef LOAD_PANEL_FROM_FLASH_CUSTOM
	if (FlashPanel_GetControlWord2_GraphModeEn())
	#else
	if (IS_SUPPORT_VB1_GRAPH_MODE)
	#endif
	{
	    fpDrvRegisterDisplayTimingUpdateCB(E_TIMEGEN_PATH_GFX, E_DISPLAY_TIMING_CB_H_ACTIVE, vDrvSetGFXHActive);
	    fpDrvRegisterDisplayTimingUpdateCB(E_TIMEGEN_PATH_GFX, E_DISPLAY_TIMING_CB_H_TOTAL, vDrvSetGFXHTotal);
	    fpDrvRegisterDisplayTimingUpdateCB(E_TIMEGEN_PATH_GFX, E_DISPLAY_TIMING_CB_H_FP, vDrvSetGFXHFP);
	    fpDrvRegisterDisplayTimingUpdateCB(E_TIMEGEN_PATH_GFX, E_DISPLAY_TIMING_CB_H_BP, vDrvSetGFXHBP);
	    fpDrvRegisterDisplayTimingUpdateCB(E_TIMEGEN_PATH_GFX, E_DISPLAY_TIMING_CB_V_ACTIVE, vDrvSetGFXVActive);
	    fpDrvRegisterDisplayTimingUpdateCB(E_TIMEGEN_PATH_GFX, E_DISPLAY_TIMING_CB_V_TOTAL, vDrvSetGFXVTotal);
	    fpDrvRegisterDisplayTimingUpdateCB(E_TIMEGEN_PATH_GFX, E_DISPLAY_TIMING_CB_V_FP, vDrvSetGFXVFP);
	    fpDrvRegisterDisplayTimingUpdateCB(E_TIMEGEN_PATH_GFX, E_DISPLAY_TIMING_CB_V_BP, vDrvSetGFXVBP);
	}
#endif
}

#ifndef CC_MTK_LOADER
#ifdef CC_S_PLATFORM
void vDrvOSDScalerConfig(void)
{//Set OSD scaling
    UINT16 u2Width=0, u2Height=0;
    UINT32 srcW=0,srcH=0,dstW=0,dstH=0,Enable=0,u4bpp=0;

    if (!IS_DISP_4K2K_TYPE3 && OSD_Base_Init_Status() 
#ifdef CC_SUPPORT_STR
      && !OSD_Base_Suspend_Status()
#endif
      )// GFX wil keep 4k1k in 4KBE model
    {
#ifdef LOAD_PANEL_FROM_FLASH_CUSTOM
        if (FlashPanel_GetControlWord2_GraphModeEn() != 0)
#else
        if(IS_SUPPORT_VB1_GRAPH_MODE != 0)
#endif
        {
            u2Width = GRAPHMODE_GetPanelWidth();
            u2Height = GRAPHMODE_GetPanelHeight();
        }
        else
        {
            u2Width = PANEL_GetPanelWidth();
            u2Height = PANEL_GetPanelHeight();
        }
        LOG(0,"[4KBE]OSD_Scale: u2Width = %d, u2Height = %d\n", u2Width, u2Height);
        OSD_SC_GetScalerInfo(OSD_SCALER_3,&Enable,&srcW,&srcH,&dstW,&dstH,&u4bpp);
        OSD_SC_Scale(OSD_SCALER_3,Enable,srcW,srcH,u2Width,u2Height);
        OSD_SC_GetScalerInfo(OSD_SCALER_2,&Enable,&srcW,&srcH,&dstW,&dstH,&u4bpp);
        OSD_SC_Scale(OSD_SCALER_2,Enable,srcW,srcH,u2Width,u2Height);
        OSD_SC_GetScalerInfo(OSD_SCALER_1,&Enable,&srcW,&srcH,&dstW,&dstH,&u4bpp);
        OSD_SC_Scale(OSD_SCALER_1,Enable,srcW,srcH,u2Width,u2Height);
    }
}
#endif
#endif

void vDrvSetPanelTiming(void)
{
    UINT32 u4HSyncWidth, u4VSyncWidth, u4HPosition;
    UINT32 u4HFrontPorch, u4VFrontPorch;
    UINT32 u4HBackPorch, u4VBackPorch;
    UINT32 u4HTotal, u4VTotal;

	if (bSI_DISPLAY_DCLK_TYPE == SV_DCLK_48HZ)
	{
		bSI_DISPLAY_DCLK_TYPE = u1DrvCal48HzOutputType();
	}

    LOG(0,"@MTK_DISP [%s][%d] \n",__FUNCTION__,__LINE__);
	
    switch (bSI_DISPLAY_DCLK_TYPE)
    {
        case SV_DCLK_48HZ:
	        u4HPosition = H_POS_48HZ;
	        u4HTotal = HLEN_TOTAL_48HZ;
	        u4VTotal = VLEN_TOTAL_48HZ;
			break;

        case SV_DCLK_50HZ:
            u4HPosition = H_POS_50HZ;
            u4HTotal = HLEN_TOTAL_50HZ;
            u4VTotal = VLEN_TOTAL_50HZ;
            break;

        case SV_DCLK_OTHERS:
        case SV_DCLK_60HZ:
        default:
            u4HPosition = H_POS_60HZ;
            u4HTotal = HLEN_TOTAL_TYPI;
            u4VTotal = VLEN_TOTAL_TYPI;
            break;
    }

    #ifdef SUPPORT_PANEL_CUSTOMER_SPEC
    u4HSyncWidth = PANEL_GetHSyncWidthByTiming(bSI_DISPLAY_DCLK_TYPE);
    u4VSyncWidth = PANEL_GetVSyncWidthByTiming(bSI_DISPLAY_DCLK_TYPE);
    u4VBackPorch = PANEL_GetVSyncBpByTiming(bSI_DISPLAY_DCLK_TYPE);
    //u4HPosition = PANEL_GetHPositionByTiming(bSI_DISPLAY_DCLK_TYPE);
    #else
    u4HSyncWidth = PANEL_GetHSyncWidth();
    u4VSyncWidth = PANEL_GetVSyncWidth();
    u4VBackPorch = u4GetPanelVSyncBp();
    #endif

    u4HFrontPorch = (u4HPosition > 0x800) ? (u4HPosition - 0x800) : 0;
	if(u4HTotal > (wDISPLAY_WIDTH + u4HFrontPorch + u4HSyncWidth))
	{
        u4HBackPorch = u4HTotal - wDISPLAY_WIDTH - u4HFrontPorch - u4HSyncWidth;
	}
	else
	{
	    return;
	}
	if(u4VTotal > (wDISPLAY_HEIGHT + u4VSyncWidth + u4VBackPorch))
	{
	    u4VFrontPorch = u4VTotal - wDISPLAY_HEIGHT - u4VSyncWidth - u4VBackPorch;
	}
	else
	{
	    return;
	}
    {
        #define ODFPLimit 4
        #define REORDERBPLimit 4
        u4HFrontPorch = (u4HFrontPorch < ODFPLimit) ? ODFPLimit : u4HFrontPorch;
        u4HBackPorch = (u4HBackPorch < REORDERBPLimit) ? REORDERBPLimit : u4HBackPorch;
    }

    if ((lcdtimingInfo.u4HActive != wDISPLAY_WIDTH) || (lcdtimingInfo.u4VActive != wDISPLAY_HEIGHT) ||
        (lcdtimingInfo.u4HTotal != u4HTotal) || (lcdtimingInfo.u4VTotal != u4VTotal) ||
        (lcdtimingInfo.u4HFP != u4HFrontPorch) || (lcdtimingInfo.u4HBP != u4HBackPorch) ||
        (lcdtimingInfo.u4HSyncWidth != u4HSyncWidth) || (lcdtimingInfo.u4VSyncWidth != u4VSyncWidth)||
        (lcdtimingInfo.u4VFP != u4VFrontPorch) || (lcdtimingInfo.u4VBP != u4VBackPorch))
    {
        #ifndef CC_MTK_LOADER
        CRIT_STATE_T csState;
        csState = x_crit_start();
        #endif
        lcdtimingInfo.u4HTotal = u4HTotal;
        lcdtimingInfo.u4HActive = wDISPLAY_WIDTH;
        lcdtimingInfo.u4HFP = u4HFrontPorch;
        lcdtimingInfo.u4HBP = u4HBackPorch;
		lcdtimingInfo.u4HSyncWidth = u4HSyncWidth;

        lcdtimingInfo.u4VTotal = u4VTotal;
        lcdtimingInfo.u4VActive = wDISPLAY_HEIGHT;
        lcdtimingInfo.u4VFP = u4VFrontPorch;
        lcdtimingInfo.u4VBP = u4VBackPorch;
		lcdtimingInfo.u4VSyncWidth = u4VSyncWidth;
        #ifndef CC_MTK_LOADER
        x_crit_end(csState);
        #endif

        LOG(0,"MTK_DISP [%s][%d] HTxVT=%dx%d HAxVA=%dx%d \n",__FUNCTION__,__LINE__,
        lcdtimingInfo.u4HTotal,lcdtimingInfo.u4VTotal,lcdtimingInfo.u4HActive,lcdtimingInfo.u4VActive);
    }

    #ifdef CC_PHP_SUPPORT
#ifndef CC_MTK_LOADER
    DRVCUST_PQBAfterSocOutputChanged(lcdtimingInfo.u4VActive, lcdtimingInfo.u4HActive, bOUTPUT_REFRESH_RATE);
#endif
    #endif

    vDrvVsyncISRInit();
	
	if (fgTconISRNeedRst)
	{
		vDrvTCONTimingGenFrameCounterRst();
		LOG(3, "[TCON_TCH] Frame counter Rst\n");
		fgTconISRNeedRst = FALSE;
	}
		
    // When MJC on/off update PSC Global to  FSC Global
    #ifndef CC_MTK_LOADER
    #if defined(CC_MT5893) || defined(CC_MT5895)
    //if (!fgIsMJCToOSTG())
    {
       vDrvUpdateFSCTimeGenPrm();
    }
    #endif
    #endif

#ifndef CC_MTK_LOADER
#ifdef CC_S_PLATFORM
    if(fgVB1VideoMute != SV_TRUE)
    {
        fgVB1VideoMute = SV_TRUE;
        if(DRVCUST_Get_InterMemc_type()) vDrvSetPSCMute(SV_TRUE);
        DRVCUST_SendEvent(E_CUST_OUT_MUTE_CHANGE, 0);
    }
    vDrvOSDScalerConfig();
#endif
    LOG(0,"@MTK_DISP [%s][%d] flg (%d) (%d) \n",__FUNCTION__,__LINE__,fgIsVsyncIsrStart,_fgVOPLLPowerOn);
    if ((fgIsVsyncIsrStart) && (_fgVOPLLPowerOn))
    {
    #if (defined(CC_MT5893) || defined(CC_MT5895)) && defined(CC_SUPPORT_4KBE)   
        vDrvUpdateVB1ControlBitInternal();
        LOG(2, "VB1ControlBit = TRIGGER_STATE_CTRL_BIT\n");
    #endif
        fgApplyScalerSetData = 1;
        fgApplyScanPWMSetData = 1;
        u1ScanPwmCrossFlg = 2;
        LOG(2, "fgApplyScalerSetData = SV_TRUE\n");
    #ifdef CC_S_PLATFORM
        if(!IS_DISP_4K2K_TYPE3)
        {
            vDrvApplyPanelTiming(); 
            if(GetPANEL4K2K_TYPE() != PANEL_TYPE_MAX) 
            {
                vDrvFireImportPortection();
                vScpipRefSetData(SV_ON);
                vScpipGfxSetData(SV_ON);

                
                if(fgFrameTrackIsRefTrk())
                {                    
                    vScpipSetSlaveSyncMode(E_GFX_TG,SV_ON);
                    vScpipSetSlaveSyncMode(E_FSC_TG,SV_ON);
                    vScpipSetSlaveSyncMode(E_PSC_TG,SV_ON);
                }
                else
                {
                    vScpipSetSlaveSyncMode(E_GFX_TG,SV_OFF);//back bledning don't need slave mode to sync vidoe and osd.
                    vScpipSetSlaveSyncMode(E_FSC_TG,SV_OFF);
                    vScpipSetSlaveSyncMode(E_PSC_TG,SV_OFF);
                }
            }
            else
            {
                vDrvFireImportPortection(); 
            }
        }
        else
        {
            DRVCUST_SendEvent(E_CUST_OUT_FMT_CHANGE, E_CUST_BE_TX_PATH_VIDEO);
            DRVCUST_SendEvent(E_CUST_OUT_POSI_CHANGE, 0);
            DRVCUST_SendEvent(E_CUST_SRC_AREA_CHANGE, 0);
        }
    #endif
    }
    else
#endif
    {
        LOG(0,"@MTK_DISP [%s][%d] \n",__FUNCTION__,__LINE__);
		LOG(1,"[LVDS]vDrvApplyPanelTiming.....\n");
        vDrvUpdateVB1ControlBitInternal();
        vDrvApplyPanelTiming();
        vDrvFireImportPortection();
       // VDP_SetBypassUFOModeEnable(1,0);
    }

	
}

void vVdoOutISRCheckApplyPanelTiming(void)
{
    #if !defined(CC_MTK_LOADER) && defined(CC_SUPPORT_4KBE)
    switch (E_VB1ControlBit_Trig_Sta)
    {
        case E_TIMING_TRIGGER_STATE_CTRL_BIT:
            vDrvVByOneCrtlBitCBV(SV_OFF);
            vDrvSetVB1ControlBit();
            E_VB1ControlBit_Trig_Sta = E_TIMING_TRIGGER_STATE_TIMING;
            break;

        case E_TIMING_TRIGGER_STATE_VALID:
            E_VB1ControlBit_Trig_Sta = E_TIMING_TRIGGER_STATE_IDLE;
            break;

        case E_TIMING_TRIGGER_STATE_WAITING:
            break;

        case E_TIMING_TRIGGER_STATE_TIMING:
            if(u1DrvIsVdpBypCase())
            {
                vDrvVByOneCrtlBitCBV(SV_ON);  
            }
            E_VB1ControlBit_Trig_Sta = E_TIMING_TRIGGER_STATE_IDLE;
            vVdoOutISRCheckSetDataTrigger();
            break;
        case E_TIMING_TRIGGER_STATE_IDLE:
        default:
            vVdoOutISRCheckSetDataTrigger();
            break;
    }
    #else
    #if !defined(CC_MTK_LOADER)
    vVdoOutISRCheckSetDataTrigger();
    #endif   

    gVB1ControlBit.u2FrameNum = (gVB1ControlBit.u2FrameNum + 1)%256;
    #endif
}

#ifndef CC_MTK_LOADER
#ifdef CC_S_PLATFORM
UINT8 u1NeedSendEventToSony = 0;
#endif
#endif
void vDrvApplyPanelTiming(void)
{
    E_TIMEGEN_PATH ePath;
    
   LOG(0, "@MTK_DISP vDrvApplyPanelTiming %d %d\n",GetPANEL4K2K_TYPE(),u1IsPanelSupport4K2K120());

  //  ePath = vDrvGetTconTopVdoSel() ? E_TIMEGEN_PATH_BYPASS : E_TIMEGEN_PATH_PQ;
    ePath = E_TIMEGEN_PATH_PQ;
    #if 0  // [Husky] no bypass path, need to review later[JW] 
    #if defined(CC_MT5893)    //(CC_SUPPORT_4K2K) && defined(CC_MT5890)
    if(GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60_BYP)		
    {
            ePath = E_TIMEGEN_PATH_BYPASS_MM;
    }
	#endif
	#endif
		
	#ifdef LOAD_PANEL_FROM_FLASH_CUSTOM
		if (FlashPanel_GetControlWord_2SecEn())
#else
		if (IS_VB1_2SEC_ENABLE || IS_VB1_4SEC_ENABLE || IS_VB1_8SEC_ENABLE)
#endif
		{
			LOG(0,"%d->%d\n", wDrvGetOutputVTotal(), lcdtimingInfo.u4VTotal);
			if (lcdtimingInfo.u4VTotal >= u4ScpipGetPanelVtotal(E_FSC_TG))
			{
				LOG(0,"2sec#1\n");
				vDrvSet2SECVsyncWidth((UINT16)(lcdtimingInfo.u4VSyncWidth));
				vDrvSet2SECVTotal((UINT16)(lcdtimingInfo.u4VTotal));
			}
			else
			{
				_fg2SecTimingWait = SV_TRUE;
			}
		}
	if(u1IsPanelSupport4K2K120())
	{          
	    DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_H_ACTIVE](lcdtimingInfo.u4HActive/2 + vDrvGetOSTGCroppingCnt());
	    DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_H_TOTAL](lcdtimingInfo.u4HTotal/2);
	    DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_H_FP](((lcdtimingInfo.u4HFP/2) >= vDrvGetOSTGCroppingCnt())? ((lcdtimingInfo.u4HFP/2)- vDrvGetOSTGCroppingCnt()):(lcdtimingInfo.u4HFP/2));
	    DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_H_BP](lcdtimingInfo.u4HBP/2);
	    DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_V_TOTAL](lcdtimingInfo.u4VTotal);
	    DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_V_ACTIVE](lcdtimingInfo.u4VActive);
	    DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_V_FP](lcdtimingInfo.u4VFP);
	    DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_V_BP](lcdtimingInfo.u4VBP);
		#ifndef CC_MTK_LOADER 
		if(u1DrvIsVdpBypCase())
		{
#if 0
		    x_memcpy(&ReftimingInfo,&lcdtimingInfo,sizeof(LCDTIMING_INFO_T));
			ReftimingInfo.u4HActive = lcdtimingInfo.u4HActive/2 + vDrvGetOSTGCroppingCnt();
			ReftimingInfo.u4HTotal = lcdtimingInfo.u4HTotal/2;
			ReftimingInfo.u4HFP = lcdtimingInfo.u4HFP/2;
			ReftimingInfo.u4HBP = lcdtimingInfo.u4HBP/2;
		    vDrvSetRefTiming_SETDATA();
#else
            vDrvSetRefTiming();
#endif
			fgWaitRefSetData = SV_TRUE;
			vScpipSetSlaveMode(E_PSC_TG, SV_ON);
		}
        else if(!u1DrvGetSRSDynEn())
        {
            fgWaitRefSetData = SV_FALSE;
            vScpipSetSlaveMode(E_PSC_TG, SV_OFF);
            vScpipSetSlaveSyncMode(E_PSC_TG,SV_OFF);
        }
		#endif
    }
	else
	{
	    if(GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60_SRS && !fgIsMJCToOSTG())
	    { 
	        DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_H_ACTIVE](lcdtimingInfo.u4HActive/2);
	        DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_H_TOTAL](lcdtimingInfo.u4HTotal/2);
	        DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_H_FP](lcdtimingInfo.u4HFP/2);
	        DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_H_BP](lcdtimingInfo.u4HBP/2);
	        DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_V_TOTAL](lcdtimingInfo.u4VTotal/2);
	        DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_V_ACTIVE](lcdtimingInfo.u4VActive/2);
	        DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_V_FP](lcdtimingInfo.u4VFP/2);
	        DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_V_BP](lcdtimingInfo.u4VBP/2);
	    }
	    else
	    {  
	        DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_H_ACTIVE](lcdtimingInfo.u4HActive);
	        DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_H_TOTAL](lcdtimingInfo.u4HTotal);
	        DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_H_FP](lcdtimingInfo.u4HFP);
	        DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_H_BP](lcdtimingInfo.u4HBP);
	        DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_V_TOTAL](lcdtimingInfo.u4VTotal);
	        DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_V_ACTIVE](lcdtimingInfo.u4VActive);
	        DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_V_FP](lcdtimingInfo.u4VFP);
	        DiplayTimingCB[ePath][E_DISPLAY_TIMING_CB_V_BP](lcdtimingInfo.u4VBP);
	    }
	}

#if defined(CC_FBM_SUPPORT_SRS)
#ifndef CC_MTK_LOADER
    if(!IS_DISP_4K2K_TYPE3)
    {
        DRVCUST_SendEvent(E_CUST_OUT_SCALING_CHANGE, SV_VP_MAIN);
    }
    else
    {
        u1NeedSendEventToSony = 1;
    }

    if(u1SRSDynSwitchEn )//&& (bDrvVideoSignalStatus(VDP_1)==SV_VDO_STABLE))
    {
        vDrvSwitch4KSRSConfig();
    }
    else if(fgFrameTrackIsRefTrk())
    {
    #if 0
        x_memcpy(&ReftimingInfo,&lcdtimingInfo,sizeof(LCDTIMING_INFO_T));
        vDrvSetRefTiming_SETDATA();
    #else
        vDrvSetRefTiming();
    #endif

        vScpipSetSlaveMode(E_FSC_TG, SV_ON);
        vScpipSetSlaveMode(E_PSC_TG, SV_ON);
    }
#endif
#endif

#ifndef CC_MTK_LOADER
	//vDrvMJCUpdateOutputTiming((UINT16)(lcdtimingInfo.u4HTotal),(UINT16)(lcdtimingInfo.u4VTotal),(UINT16)(lcdtimingInfo.u4HActive),(UINT16)(lcdtimingInfo.u4VActive));
#endif
    // ISR Setting
    vDrvVsyncISRSetLineCounter((UINT16)(lcdtimingInfo.u4VFP));
    vDrvTCONISRSetLineCounter(lcdtimingInfo.u4VFP, lcdtimingInfo.u4VActive, lcdtimingInfo.u4VBP);

    // For 3D_Glasses Control - software work around
    vIO32WriteFldAlign(VINTF_P8, lcdtimingInfo.u4VTotal,  VINTF_REYE_WIDTH);
    vIO32WriteFldAlign(VINTF_P9, 1,  VINTF_REYE_RST_MODE);
    vIO32WriteFldAlign(VINTF_P10, lcdtimingInfo.u4VTotal,  VINTF_LEYE_WIDTH);
    vIO32WriteFldAlign(VINTF_P11, 1,  VINTF_LEYE_RST_MODE);

    vErrorHandleSetByTiming();
    // osd Hstart setting
    OSD_Base_SetHStart();
 
    // update dimming frequency
    #if !defined(CC_S_PLATFORM)
	if (PANEL_IsUseScanPwm())
	{
	// These APIs need to remove later
    vDrvSetScanPWMLatchMode(SCAN_PWM_LATCH_MODE_SETDATA,SCAN_PWM_LATCH_MODE_OFF,SCAN_PWM_ALIGN_MODE_VSYNC);  // Set data
    LOG(3, "_|-|_ vDrvApplyPanelTiming Bright:%d%\n", _u4BrightLevel);
    BACKLT_SetDimming(BACKLT_GetDimmingFrequency(), _u4BrightLevel);
    #ifndef CC_MTK_LOADER
    DRVCUST_UpdateBacklightState(BL_STATE_OUTPUT_FR_CHANGE, (UINT16)_u4BrightLevel);
    #endif
	}
    #endif
    #ifndef CC_MTK_LOADER
    if(IS_PANEL_L12R12)
    {
        Panel_3DGlassesCTL(e3DGlasses_P_3DBL,e3DGlasses_S_BL,e3DGlasses_POl_Bypass,e3DGlasses_POS_1stR,0,w3DGlassesDelay);
        Panel_3DGlassesDutyCTL(e3DGlasses_S_BL,50);
    }

    if(!IS_DISP_4K2K_TYPE3 && !((PANEL_GetControlWord() & DISP_TYPE_MASK) == 0))
    {
        DRVCUST_SendEvent(E_CUST_OUT_FMT_CHANGE, E_CUST_BE_TX_PATH_VIDEO);
        DRVCUST_SendEvent(E_CUST_OUT_POSI_CHANGE, 0);
        DRVCUST_SendEvent(E_CUST_SRC_AREA_CHANGE, 0);
    }
    #endif

}

/*
//reference timing generator initial steps:
//1.	Disable FSC/PSC/GFX time-gen
//2.	Set HTOT/VTOT for reference time-gen
//3.	Set VSYNC gen and N/M mask settings
//4.	Set FSC/PSC/GFX time-gen as slave mode <-- setting initial state 
//5.	Enable FSC/PSC/GFX time-gen
*/
static UINT8 bSI_DISPLAY_DCLK_TYPE_bak=0xff;

void vDrvSetRefTiming( void )
{
    BOOL   bHFPLarge32 = SV_FALSE;

    if(VDP_LogChkMsk(VDP_1,VDP_LOG_DBG_MSK_TRK)) LOG(0,"@MTK_TRK -vDrvSetRefTiming-- %d %d %d %d\n",bSI_DISPLAY_DCLK_TYPE_bak,bSI_DISPLAY_DCLK_TYPE,bfgCroppingChg,fg4k2kPNLTypeChg);

    if ( rREF_4K_TG[bSI_DISPLAY_DCLK_TYPE].u2HTotal == 0 || rREF_4K_TG[bSI_DISPLAY_DCLK_TYPE].u2VTotal == 0 )
    {
        if(VDP_LogChkMsk(VDP_1,VDP_LOG_DBG_MSK_TRK)) LOG(0,"@MTK_TRK vDrvSetRefTiming u2HTotal/u2VTotal =  0 return\n");
        return;
    }

    if ( bSI_DISPLAY_DCLK_TYPE_bak == bSI_DISPLAY_DCLK_TYPE && (bfgCroppingChg == SV_FALSE) && 
            (fg4k2kPNLTypeChg == SV_FALSE) && (fg4k2kVrrModChg == SV_FALSE) && 
            (fg4k2kBypPathChg == SV_FALSE) && (fg4k2kFrmTrkChg == SV_FALSE) )
    {
        if(VDP_LogChkMsk(VDP_1,VDP_LOG_DBG_MSK_TRK)) LOG(0,"@MTK_TRK [%s][%d] \n",__FUNCTION__,__LINE__);
        return;    //if output frame has no change, just keep the ref_tg setting.
    }
    bSI_DISPLAY_DCLK_TYPE_bak=bSI_DISPLAY_DCLK_TYPE;

    if(PANEL_Is4K2K120())
    {
        // ------ 4k2k@60Hz ------ 
        ReftimingInfo.u4HTotal  = rREF_4K_TG[bSI_DISPLAY_DCLK_TYPE].u2HTotal;
        ReftimingInfo.u4VTotal  = rREF_4K_TG[bSI_DISPLAY_DCLK_TYPE].u2VTotal;
        //ReftimingInfo.u4HActive = rREF_4K_TG[bSI_DISPLAY_DCLK_TYPE].u2Width + vDrvGetOSTGCroppingCnt();
        ReftimingInfo.u4HActive = rREF_4K_TG[bSI_DISPLAY_DCLK_TYPE].u2Width;
        ReftimingInfo.u4VActive = rREF_4K_TG[bSI_DISPLAY_DCLK_TYPE].u2Height;
        ReftimingInfo.u4HSyncWidth = rREF_4K_TG[bSI_DISPLAY_DCLK_TYPE].u2HsyncWidth;
        ReftimingInfo.u4HFP     = rREF_4K_TG[bSI_DISPLAY_DCLK_TYPE].u2HsyncFp;
        ReftimingInfo.u4VSyncWidth = rREF_4K_TG[bSI_DISPLAY_DCLK_TYPE].u2VsyncWidth;
        ReftimingInfo.u4VBP     = rREF_4K_TG[bSI_DISPLAY_DCLK_TYPE].u2VsyncBp;

        ReftimingInfo.u4HBP     = ReftimingInfo.u4HTotal - ReftimingInfo.u4HActive - ReftimingInfo.u4HSyncWidth - ReftimingInfo.u4HFP;
        ReftimingInfo.u4VFP     = ReftimingInfo.u4VTotal - ReftimingInfo.u4VActive - ReftimingInfo.u4VSyncWidth - ReftimingInfo.u4VBP;
        // -----------------------

    #ifndef CC_MTK_LOADER
        if(bDrvVideoGetRefreshRate(VDP_1) >= 96)
        {
        #if 0
            if (bDrvIs4KHFRToPQPath(SV_VP_MAIN))
            {
                // 4k1k@120Hz
                LOG(0, "[Ref_timing]4K1K@96/100/120Hz \n"); 
                ReftimingInfo.u4VTotal = ReftimingInfo.u4VTotal/2;
                ReftimingInfo.u4VActive = ReftimingInfo.u4VActive/2;
                ReftimingInfo.u4VSyncWidth = ReftimingInfo.u4VSyncWidth/2;
                ReftimingInfo.u4VBP = ReftimingInfo.u4VBP/2;
                ReftimingInfo.u4VFP = ReftimingInfo.u4VFP/2;
            }
            else  // Bypass path
            {
                //  2k2k@120Hz
                LOG(0, "[Ref_timing]2K2K@96/100/120Hz \n"); 
                ReftimingInfo.u4HTotal = ReftimingInfo.u4HTotal/2;
                ReftimingInfo.u4HActive = ReftimingInfo.u4HActive/2;
                ReftimingInfo.u4HSyncWidth = ReftimingInfo.u4HSyncWidth/2;
                ReftimingInfo.u4HBP = ReftimingInfo.u4HBP/2;
                ReftimingInfo.u4HFP = ReftimingInfo.u4HFP/2;
            }
        #endif
            // 2k2k@120Hz
            LOG(0, "[Ref_timing]2K2K@96/100/120Hz \n"); 
            ReftimingInfo.u4HTotal = ReftimingInfo.u4HTotal/2;
            ReftimingInfo.u4HActive = ReftimingInfo.u4HActive/2;
            ReftimingInfo.u4HSyncWidth = ReftimingInfo.u4HSyncWidth/2;
            ReftimingInfo.u4HBP = ReftimingInfo.u4HBP/2;
            ReftimingInfo.u4HFP = ReftimingInfo.u4HFP/2;
        }
    #endif
    }
    else
    {
        ReftimingInfo.u4HTotal  = rREF_4K_TG[bSI_DISPLAY_DCLK_TYPE].u2HTotal;
        ReftimingInfo.u4VTotal  = rREF_4K_TG[bSI_DISPLAY_DCLK_TYPE].u2VTotal;
        ReftimingInfo.u4HActive = rREF_4K_TG[bSI_DISPLAY_DCLK_TYPE].u2Width + vDrvGetOSTGCroppingCnt();
        ReftimingInfo.u4VActive = rREF_4K_TG[bSI_DISPLAY_DCLK_TYPE].u2Height;
        ReftimingInfo.u4HSyncWidth = rREF_4K_TG[bSI_DISPLAY_DCLK_TYPE].u2HsyncWidth;
        ReftimingInfo.u4HFP     = rREF_4K_TG[bSI_DISPLAY_DCLK_TYPE].u2HsyncFp;
        ReftimingInfo.u4VSyncWidth = rREF_4K_TG[bSI_DISPLAY_DCLK_TYPE].u2VsyncWidth;
        ReftimingInfo.u4VBP     = rREF_4K_TG[bSI_DISPLAY_DCLK_TYPE].u2VsyncBp;
        
        if (PANEL_Is8K4K30()/* || PANEL_Is4K2K120()*/)
        {
            if(ReftimingInfo.u4HFP >= vDrvGetOSTGCroppingCnt() && vDrvGetOSTGCroppingCnt() != 0)
            {
               bHFPLarge32 = SV_TRUE;
            }
            else
            {
               bHFPLarge32 = SV_FALSE;
            }
            ReftimingInfo.u4HFP = ((ReftimingInfo.u4HFP >= vDrvGetOSTGCroppingCnt())? (ReftimingInfo.u4HFP - vDrvGetOSTGCroppingCnt()) : ReftimingInfo.u4HFP);
        }

        if(bHFPLarge32 == SV_TRUE)
        {
            ReftimingInfo.u4HBP = ReftimingInfo.u4HTotal - ReftimingInfo.u4HActive - ReftimingInfo.u4HSyncWidth - ReftimingInfo.u4HFP;
        }
        else
        {
            ReftimingInfo.u4HBP = ReftimingInfo.u4HTotal - ReftimingInfo.u4HActive - ReftimingInfo.u4HSyncWidth - ReftimingInfo.u4HFP + vDrvGetOSTGCroppingCnt();
        }
        ReftimingInfo.u4VFP     = ReftimingInfo.u4VTotal - ReftimingInfo.u4VActive - ReftimingInfo.u4VSyncWidth - ReftimingInfo.u4VBP;
    }

    if(VDP_LogChkMsk(VDP_1,VDP_LOG_DBG_MSK_TRK)) LOG(0,"ref ReftimingInfo.u4HTotal=%d\n", ReftimingInfo.u4HTotal);
    if(VDP_LogChkMsk(VDP_1,VDP_LOG_DBG_MSK_TRK)) LOG(0,"ref ReftimingInfo.u4HActive=%d\n", ReftimingInfo.u4HActive);
    if(VDP_LogChkMsk(VDP_1,VDP_LOG_DBG_MSK_TRK)) LOG(0,"ref ReftimingInfo.u4HSyncWidth=%d\n", ReftimingInfo.u4HSyncWidth);
    if(VDP_LogChkMsk(VDP_1,VDP_LOG_DBG_MSK_TRK)) LOG(0,"ref ReftimingInfo.u4HFP=%d  CroppingCnt=%d\n", ReftimingInfo.u4HFP, vDrvGetOSTGCroppingCnt());
    if(VDP_LogChkMsk(VDP_1,VDP_LOG_DBG_MSK_TRK)) LOG(0,"ref ReftimingInfo.u4HBP=%d\n", ReftimingInfo.u4HBP);
    
    if(VDP_LogChkMsk(VDP_1,VDP_LOG_DBG_MSK_TRK)) LOG(0,"ref ReftimingInfo.u4VTotal=%d\n", ReftimingInfo.u4VTotal);
    if(VDP_LogChkMsk(VDP_1,VDP_LOG_DBG_MSK_TRK)) LOG(0,"ref ReftimingInfo.u4VSyncWidth=%d\n", ReftimingInfo.u4VSyncWidth);
    if(VDP_LogChkMsk(VDP_1,VDP_LOG_DBG_MSK_TRK)) LOG(0,"ref ReftimingInfo.u4VActive=%d\n", ReftimingInfo.u4VActive);
    if(VDP_LogChkMsk(VDP_1,VDP_LOG_DBG_MSK_TRK)) LOG(0,"ref ReftimingInfo.u4VFP=%d \n", ReftimingInfo.u4VFP);
    if(VDP_LogChkMsk(VDP_1,VDP_LOG_DBG_MSK_TRK)) LOG(0,"ref ReftimingInfo.u4VBP=%d\n", ReftimingInfo.u4VBP);

    vDrvSetRefTiming_SETDATA();

#ifndef CC_MTK_LOADER
    ReftimingInfo.u1SetData = 1;
    LOG(2,"------ vScpipRefSetData #0\n");
#else
    vScpipRefSetData(ON);
    LOG(2,"------ vScpipRefSetData #1\n");
#endif
}

void vDrvSetVB1ControlBit_Valid(BOOL fgValid)
{
    if(PANEL_GetDisplayType()== PANEL_DISPLAY_TYPE_VB1)
    {
    
    }
}

void vDrvSetRefTiming_SETDATA(void)
{
    UINT32 newVfp = ReftimingInfo.u4VFP;
    if(u4ScpipGetVTModeCtrl(E_REF_TG) == E_TRK_FRONT_PORCH)
    {
        newVfp = ReftimingInfo.u4VTotal - ReftimingInfo.u4VActive - ReftimingInfo.u4VBP - ReftimingInfo.u4VFP;
    }
    vScpipSetPanelHtotal( E_REF_TG , ReftimingInfo.u4HTotal );
    vScpipSetPanelVtotal( E_REF_TG , ReftimingInfo.u4VTotal );
    vScpipSetPanelWidth ( E_REF_TG , ReftimingInfo.u4HActive );
    vScpipSetPanelHeight( E_REF_TG , ReftimingInfo.u4VActive );

    vScpipSetHsyncFp( E_REF_TG , ReftimingInfo.u4HFP );
    vScpipSetHsyncBp( E_REF_TG , ReftimingInfo.u4HBP );
    vScpipSetVsyncFp( E_REF_TG , newVfp);
    vScpipSetVsyncBp( E_REF_TG , ReftimingInfo.u4VBP );

    if(VDP_LogChkMsk(VDP_1,VDP_LOG_DBG_MSK_TRK)) LOG(0,"@MTK_TRK [%s][%d] HA:%d HFP:%d HBP:%d VA:%d VFP:%d VBP:%d \n",__FUNCTION__,__LINE__,
            ReftimingInfo.u4HActive, ReftimingInfo.u4HFP, ReftimingInfo.u4HBP,
            ReftimingInfo.u4VActive, ReftimingInfo.u4VFP, ReftimingInfo.u4VBP);

	vScpipRefSetTgImportOnOff(ON);
    //vScpipRefSetData(ON);
}





/**
 * @brief     vDrvSetOstgCHSelFlag
 * Set the flag of OSTG CH0 or CH1
 * @param  UINT8 bCHSel
 *                0: CH0 only; 1: CH1 only; 2: CH0 & CH1
 * @retval   void
 */
void vDrvSetOstgCHSelFlag(UINT8 bCHSel)
{
	vIO32WriteFldAlign(DDDS_08, (bCHSel&0x3), FLAG_HK_OSTG_CH_SEL);
}

UINT8 u1DrvGetOstgCHSelFlag(void)
{
	return (IO32ReadFldAlign(DDDS_08, FLAG_HK_OSTG_CH_SEL));
}

/**
 * @brief vDrvOutputStageInit

 * Init the Register Settings of Output Stage Logic

 * @param  void
 * @retval void
 */

void vDrvVideoDitherInit(void)
{
	UINT32 u4DitherOnOff = 0;
	UINT32 u4DitherType = 0; //0:no dither 1: 12 to 10 2:12 to 8 3: 12 to 6
	#ifdef CC_PANEL_FROM_FLASH
	UINT32 u4ControlWord = FlashPanel_GetControlWord();
	UINT32 u4ControlWord3 = FlashPanel_GetControlWord3();// bit[27:26]	0: no 3D , 1:active 3D	,2:Passive 3D 
	#else
	UINT32 u4ControlWord = 0;
	UINT32 u4ControlWord3 = 0;// bit[27:26]  0: no 3D , 1:active 3D  ,2:Passive 3D 
	#endif
	UINT8 u1DisplayBit = 0;
	UINT8 u1BeDevice = 0;
    // 1.1 set Front dither to disable
    vOSTG_IO32WriteFldAlign(OS_DITHER_02, 0, DITHER_LSB_OFF);
    vOSTG_IO32WriteFldAlign(OS_DITHER_00, 0, DITHER_ROUND_EN);
    vOSTG_IO32WriteFldAlign(OS_DITHER_00, 0, RDITHER_EN);
    vOSTG_IO32WriteFldAlign(OS_DITHER_00, 0, EDITHER_EN);
    vOSTG_IO32WriteFldAlign(OS_DITHER_00, 0, DITHER_LFSR_EN);
    vOSTG_IO32WriteFldAlign(OS_DITHER_00, 0, REG_SUBPIX_EN);
    // 1.2 set Post dither Fixed value
    vOSTG_IO32WriteFldAlign(OS_DITHER_05, 1, REG_LSB_OFF_POST);
    vOSTG_IO32WriteFldAlign(OS_DITHER_03, 0, REG_ROUND_EN_POST);
    vOSTG_IO32WriteFldAlign(OS_DITHER_03, 0, REG_RDITHER_EN_POST);
    vOSTG_IO32WriteFldAlign(OS_DITHER_03, 0, REG_EDITHER_EN_POST);
    vOSTG_IO32WriteFldAlign(OS_DITHER_03, 0, REG_SUBPIX_EN_POST);
	// 1.3 get ditehr on/off and type from panel data
	u1DisplayBit = (UINT8) ((u4ControlWord& 0x00000060)>>5);
	u1BeDevice = (UINT8) ((u4ControlWord3& 0x70000000)>>28);
	if(u1DisplayBit==1)
	{
		u4DitherOnOff = 1;
		u4DitherType = 2;//12 tp 8
	}
	else
	{
		if(u1BeDevice == 0)//no BE and no FRC
		{
			u4DitherOnOff = 1;
			u4DitherType = 1;//12 tp 10
		}
		else if((u1BeDevice == 1)||(u1BeDevice == 2))//extFRC
		{
			u4DitherOnOff = 1;
			u4DitherType = 1;//12 tp 10
		}
		else//4kBE
		{
			u4DitherOnOff = 0;
			u4DitherType = 0;//12 tp 10
		}
	}

	
	// 1.4 set on off
	vOSTG_IO32WriteFldAlign(OS_DITHER_03, u4DitherOnOff, REG_LFSR_EN_POST);
	vOSTG_IO32WriteFldAlign(OS_DITHER_05, u4DitherType, REG_DRMOD_R_POST);
	vOSTG_IO32WriteFldAlign(OS_DITHER_05, u4DitherType, REG_DRMOD_G_POST);
	vOSTG_IO32WriteFldAlign(OS_DITHER_05, u4DitherType, REG_DRMOD_B_POST);
	printf("Post Dither Init data from Flash panel ONOFF:%d , Type %d\n",u4DitherOnOff,u4DitherType);
    // 2.2 Common Dither Setting
    vOSTG_IO32WriteFldAlign(OS_DITHER_03, 0, REG_SUB_R_POST);
    vOSTG_IO32WriteFldAlign(OS_DITHER_03, 1, REG_SUB_G_POST);
    vOSTG_IO32WriteFldAlign(OS_DITHER_03, 2, REG_SUB_B_POST);

    vOSTG_IO32WriteFldAlign(OS_DITHER_03, SV_TRUE, REG_FPHASE_EN_POST);
    vOSTG_IO32WriteFldAlign(OS_DITHER_03, 0, REG_FPHASE_R_POST);      // phase select for running order.
                                                                //  0: internal control register from reg_fphase[5:0]
                                                                //  1: LFSR

    vOSTG_IO32WriteFldAlign(OS_DITHER_03, 3, REG_FPHASE_POST);        // Running order dithering frame phase increment.
                                                                // 1. If ROUND_EN = 1, the output will only rounding only
                                                                // (no matter what RDITHER_EN and EDITHER_EN setting)
                                                                // 2. RDITHER_EN and EDITHER_EN can work together.
    vOSTG_IO32WriteFldAlign(OS_DITHER_03, 1, REG_FPHASE_SEL_POST);    // fphase XOR function bit. Only bit2,bit4,bit6 is available.
    vOSTG_IO32WriteFldAlign(OS_DITHER_03, 1, REG_FPHASE_BIT_POST);    // fphase_bit XOR bus number.
                                                                // Note: this function will check reg_fphase_sel.
                                                                // Only 0,1,2,4,6 is available."
}


void vDrvOutputStageInit(void)
{

    #ifdef CC_SCPOS_3DTV_SUPPORT
    UINT32 u4EnPair;
    #endif

    #if (SRGB_FUNCTION_EN == SRGB_EN_ON)
    {
        vIO32WriteFldAlign(OSTG_PTGEN_00, 1, FLD_DEN_EN);
    }
    #else
    {
        vIO32WriteFldAlign(OSTG_PTGEN_00, 0, FLD_DEN_EN);
    }
    #endif

    #if (DEN_TYPE == DEN_ACIVE)
    {
        vIO32WriteFldAlign(OSTG_PTGEN_00, 1, FLD_DENTPY);
    }
    #else
    {
        vIO32WriteFldAlign(OSTG_PTGEN_00, 0, FLD_DENTPY);
    }
    #endif

    #if (DEN_POLARITY == DEN_LOW)
    {
        vIO32WriteFldAlign(OSTG_PTGEN_00, 1, FLD_DENP);
    }
    #else
    {
        vIO32WriteFldAlign(OSTG_PTGEN_00, 0, FLD_DENP);
    }
    #endif

    vIO32WriteFldAlign(OSTG_PTGEN_00, 0, FLD_HSYNCP);
    vIO32WriteFldAlign(OSTG_PTGEN_00, 0, FLD_VSYNCP);

//    vDrvVideoDitherInit();

    // Config HV alignment
    //vIO32WriteFldAlign(OUTSTG_OS_08, 3, OS_08_REG_VSYNC_OUT_SEL);

    if ((DISP_BIT == DISP_30BIT)
        #ifdef DRV_SUPPORT_EXTMJC
        || (IS_COMPANION_CHIP_ON() && (vDrvGetCurLvdsMode() !=  eLvdsVidNormal))
        #endif
        )
    {
        vIO32WriteFldAlign(LVDSB_REG06, 1, RG_10B_EN);
    }
    else
    {
        vIO32WriteFldAlign(LVDSB_REG06, 0, RG_10B_EN);
    }

#ifdef CC_SCPOS_3DTV_SUPPORT
	if (DRVCUST_PanelQuery(eDISP_RLVDSEncodeLink, &u4EnPair) == 0)	// some link, pair3
	{
	  u1SetDISP_REncodeEn((UINT8)u4EnPair);
	}
	else if (IS_DISPR_ENCODEL4)  // all links, pair4
	{
		u1SetDISP_REncodeEnHW_L4(1);
		u1SetDISP_REncodeEnHW_L3(0);
	}
	else  // all links, pair3
	{
		u1SetDISP_REncodeEnHW_L4(0);
		u1SetDISP_REncodeEnHW_L3(1);
	}
	vOSTG_IO32WriteFldAlign(OSTG_01, 0x1, REG_3DTV_OUT_SEL);
#endif

        if(IS_SUPPORT_OSD_INDEX)
        vSetOSD_IndexEncodeEnable(1);
        else
        vSetOSD_IndexEncodeEnable(0);


    // scaler timing initial
    /*  cobra don't have SET_DATA_POS_SEL
    if (fgIsMJCToOSTG())
    vIO32WriteFldAlign(PSPIP_OA_07, 1, PSPIP_OA_07_SCALER_SET_DATA_POS_SEL);
    else
    vIO32WriteFldAlign(SCPIP_PIP_OA_07, 1 ,PIP_OA_07_SCALER_SET_DATA_POS_SEL);
    */
    // local dimming initialization
    #ifdef CC_MTK_LOADER
    // init local dimming panel with default setting.
    #else

	#ifdef CC_MAPLE_CUST_DRV
    vIO32WriteFldAlign(LCDIM_LD2_CONTRAL, 1, LD2_CONTRAL_C_LD_SELECTION);//switch the LD path before LDR_PanelLogo. (refer to loader_main.c) In vDrvOutputStageInit(), switch to the 3rd-party LCDIM for STR case.
    vIO32WriteFldAlign(LCDIM_NEW_LD_CONTROL, 1, NEW_LD_CONTROL_C_NEW_LD_SPI_MOSI_SEL);
    vIO32WriteFldAlign(LCDIM_NEW_LD_CONTROL, 0, NEW_LD_CONTROL_C_NEW_LD_FO_N400_BYPASS);
    vIO32WriteFldAlign(LCDIM_NEW_LD_CONTROL, (u4GetPanelVSyncBp() + 1) + (wDISPLAY_HEIGHT - 1), NEW_LD_CONTROL_C_NEW_LD_FDE_END);
    vIO32WriteFldAlign(LCDIM_NEW_LD_CONTROL, (u4GetPanelVSyncBp() + 1), NEW_LD_CONTROL_C_NEW_LD_FDE_START);
    vOSTG_IO32WriteFldAlign(ADL_CONTROL, 1 ,ADL_CONTROL_C_MIU2SMI_BRIDGE_BURST_EN);
    vDrvSetSCANPWMforSPIInit();//pwm.i
//    dump_stack();
	MAPI_LD_Set_SPI_Output_Clk(1);//n.ycproc.lcdim.mspiclk 1 (6.75MHz)
    if(DRVCUST_Get_InterMemc_type() == 1) {
   	    vIO32WriteFldAlign(CKGEN_PMUX6, 3, Fld(4,20,AC_MSKB2));//output emit from sf_in
//   	    vIO32WriteFldAlign(CKGEN_PMUX7, 3, Fld(4,12,AC_MSKB1));//output CS from GPIO6
        BSP_PinSet(PIN_GPIO6, PINMUX_FUNCTION3);
    }
    LOG(0, "[LDIM_INIT] VBP=%d, Height=%d\n", u4GetPanelVSyncBp(), wDISPLAY_HEIGHT);
    if (fgIsSWRegInit()!=SV_FALSE)
    {
        if(PANEL_IsSupportLocalDimming())
        {
            if(bDrvLcdimSpiInit() == SV_FAIL)
            {
                bSupportLocalDimming = SV_FALSE;
            }
            else    // PQ setting is only initialized when SPI is ready
            {
                if(bDrvLcDimPanelInit() == SV_SUCCESS)
                {
                    vDrvLcDimQualityInit();
                    bSupportLocalDimming = SV_TRUE;
                }
                else
                {
                    bSupportLocalDimming = SV_FALSE;
                }
            }
        }
        else
        {
            bSupportLocalDimming = SV_FALSE;
        }
    }
	#endif
    #endif

    switch(PANEL_GetDisplayType())
    {
        default:
        case PANEL_DISPLAY_TYPE_MAX:
            // error display type, defaultly do nothing
            break;
			
        case PANEL_DISPLAY_TYPE_LVDS:
            vIO32WriteFldAlign(TMGR5, 3, TMGR5_RG_FIFO_CTRL);
            vIO32WriteFldAlign(TMGR2, 0, TMGR2_RG_FIFO_CK_SEL);			
            vIO32WriteFldAlign(TMGR18, 0, TMGR18_RG_FIFO_SEL);
            break;
 
       #if defined(CC_MT5893) || defined(CC_MT5895)
            case PANEL_DISPLAY_TYPE_VB1:
                vIO32WriteFldAlign(TMGR5, 0, TMGR5_RG_FIFO_CTRL);
                vIO32WriteFldAlign(TMGR2, 2, TMGR2_RG_FIFO_CK_SEL);
                vIO32WriteFldAlign(TMGR18, 2, TMGR18_RG_FIFO_SEL);
                break;
        #endif
    }
    vIO32WriteFldAlign(TMGR5, 1, TMGR5_RG_FIFO_EN);

#ifndef CC_MTK_LOADER
#ifdef CC_SCPOS_3DTV_SUPPORT
    vSavePanelAttribute();
    fpDrvRegisterTDTVModeChangeCB(E_TDTV_CB_OSTG, (vTDTVModeChangeCB) vDrvDisp3DModeCB);
#endif
#endif
	vSaveRefAttribute_4K2K();
#ifdef LOAD_PANEL_FROM_FLASH_CUSTOM
if (FlashPanel_GetControlWord2_GraphModeEn() != 0)
#else
if(IS_SUPPORT_VB1_GRAPH_MODE != 0 || (u1IsPanelSupport4K2K120() || u1IsPanelSupport8K4K30()))
#endif
    {
        if(fgFrameTrackIsRefTrk())
        {
            vScpipSetSlaveMode(E_FSC_TG, SV_ON);
            vScpipSetSlaveMode(E_PSC_TG, SV_ON);

            vScpipSetSlaveSyncMode(E_FSC_TG, SV_ON);
            vScpipSetSlaveSyncMode(E_PSC_TG, SV_ON);
        }
    }
    else
    {
        if(fgFrameTrackIsRefTrk())
        {
            vScpipSetSlaveMode(E_FSC_TG, SV_ON);
            vScpipSetSlaveMode(E_PSC_TG, SV_ON);

            vScpipSetSlaveSyncMode(E_FSC_TG, SV_ON);
            vScpipSetSlaveSyncMode(E_PSC_TG, SV_ON);
        }
        else
        {
            vScpipSetSlaveMode(E_ALL_TG, OFF);  //gazelle no need slaved mode
            vScpipSetSlaveSyncMode(E_ALL_TG, OFF);
        }
    }

#if (defined(CC_MT5893) || defined(CC_MT5895)) && defined(CC_SUPPORT_4KBE)
    vDrvVB1ControlBitInit();
#endif
//#endif

#if defined(SUPPORT_PANEL_GRAPH_MODE)
	#ifdef LOAD_PANEL_FROM_FLASH_CUSTOM
	if (FlashPanel_GetControlWord2_GraphModeEn())
	#else
    if (IS_SUPPORT_VB1_GRAPH_MODE)
	#endif
    {
		vDrvGFXInit();
    }
#endif

	vIO32WriteFldAlign(OSTG_01, 0, REG_MAIN_TOP_EN); // set sub plane mixer order above main
	vIO32WriteFldAlign(OSTG_01 + OSTG_2CH_OFFSET, 0, REG_MAIN_TOP_EN); // set sub plane mixer order above main

#if defined(CC_MT5886) || defined(CC_MT5893) || defined(CC_MT5895)
#ifdef CC_BYPASS_MJC
    vDrvSetOSTGMjcEn(0);
#else
    if (PANEL_IsSupport4K2KDD() && (!IS_DISP_4K2K_TYPE3))
    {
        vDrvSetOSTGMjcEn(1);
    }
    else
    {
        vDrvSetOSTGMjcEn(0);
    }
#endif
#endif
}


void vDrvSetFixedFrameRate(UINT8 u1FrameRate)
{
    _bForceFrameRate = u1FrameRate;
}

void vDrvGfxTimingInit(UINT8 u1GfxDclkType)
{
    GFXTimingInfo.u1DCLKType = u1GfxDclkType;

    if (u1GfxDclkType == SV_DCLK_48HZ)
    {
        u1GfxDclkType = u1DrvCal48HzOutputType();
    }

    switch (u1GfxDclkType)
    {
        case SV_DCLK_48HZ:
            GFXTimingInfo.u1FrameRate = 48;
            break;

        case SV_DCLK_50HZ:
            GFXTimingInfo.u1FrameRate = 50;
            break;

        case SV_DCLK_60HZ:
        default:
            GFXTimingInfo.u1FrameRate = 60;
            break;
    }

    if (GraphMode_GetFrameRateMax() <= 30)
    {
        GFXTimingInfo.u1FrameRate >>= 1;
    }

    if (GFXTimingInfo.u4HTotal == GRAPHMODE_GetHtotalByTiming(GFXTimingInfo.u1DCLKType) &&
        GFXTimingInfo.u4VTotal == GRAPHMODE_GetVtotalByTiming(GFXTimingInfo.u1DCLKType))
    {
        if(!fgForceGfxTimingUpdate)
        {
            LOG(1, "[vDrvSetGFXTiming] return!\n");
            return;
        }
        else
        {
            LOG(0,"[GFX]Disable fgForceGfxTimingUpdate AFTER resume!!\n");
            vDrvSetGfxTimingForceUpdated(FALSE);
        }
    }

    {
    #ifndef CC_MTK_LOADER
        CRIT_STATE_T csState;
        csState = x_crit_start();
    #endif
        GFXTimingInfo.u4HTotal      = GRAPHMODE_GetHtotalByTiming(GFXTimingInfo.u1DCLKType);
        GFXTimingInfo.u4HActive     = GRAPHMODE_GetPanelWidth();
        GFXTimingInfo.u4HFP         = GRAPHMODE_GetHSyncFpByTiming(GFXTimingInfo.u1DCLKType);
        GFXTimingInfo.u4HSyncWidth  = GRAPHMODE_GetHSyncWidthByTiming(GFXTimingInfo.u1DCLKType);
        GFXTimingInfo.u4HBP         = GRAPHMODE_GetHSyncBpByTiming(GFXTimingInfo.u1DCLKType);

        GFXTimingInfo.u4VTotal      = GRAPHMODE_GetVtotalByTiming(GFXTimingInfo.u1DCLKType);
        GFXTimingInfo.u4VActive     = GRAPHMODE_GetPanelHeight();
        GFXTimingInfo.u4VFP         = GRAPHMODE_GetVSyncFpByTiming(GFXTimingInfo.u1DCLKType);
        GFXTimingInfo.u4VSyncWidth  = GRAPHMODE_GetVSyncWidthByTiming(GFXTimingInfo.u1DCLKType);
        GFXTimingInfo.u4VBP         = GRAPHMODE_GetVSyncBpByTiming(GFXTimingInfo.u1DCLKType);

        vDrvUpdateGFXControlBit();
    #ifndef CC_MTK_LOADER
        x_crit_end(csState);
    #endif
    }
}

void vDrvPanelTimingInit(void)
{
    UINT32 u4HSyncWidth, u4VSyncWidth, u4HPosition;
    UINT32 u4HFrontPorch, u4VFrontPorch;
    UINT32 u4HBackPorch, u4VBackPorch;
    UINT32 u4HTotal, u4VTotal;

	if (bSI_DISPLAY_DCLK_TYPE == SV_DCLK_48HZ)
	{
		bSI_DISPLAY_DCLK_TYPE = u1DrvCal48HzOutputType();
	}

    switch (bSI_DISPLAY_DCLK_TYPE)
    {
        case SV_DCLK_48HZ:
	        u4HPosition = H_POS_48HZ;
	        u4HTotal = HLEN_TOTAL_48HZ;
	        u4VTotal = VLEN_TOTAL_48HZ;
			break;

        case SV_DCLK_50HZ:
            u4HPosition = H_POS_50HZ;
            u4HTotal = HLEN_TOTAL_50HZ;
            u4VTotal = VLEN_TOTAL_50HZ;
            break;

        case SV_DCLK_OTHERS:
        case SV_DCLK_60HZ:
        default:
            u4HPosition = H_POS_60HZ;
            u4HTotal = HLEN_TOTAL_TYPI;
            u4VTotal = VLEN_TOTAL_TYPI;
            break;
    }

    #ifdef SUPPORT_PANEL_CUSTOMER_SPEC
    u4HSyncWidth = PANEL_GetHSyncWidthByTiming(bSI_DISPLAY_DCLK_TYPE);
    u4VSyncWidth = PANEL_GetVSyncWidthByTiming(bSI_DISPLAY_DCLK_TYPE);
    u4VBackPorch = PANEL_GetVSyncBpByTiming(bSI_DISPLAY_DCLK_TYPE);
    //u4HPosition = PANEL_GetHPositionByTiming(bSI_DISPLAY_DCLK_TYPE);
    #else
    u4HSyncWidth = PANEL_GetHSyncWidth();
    u4VSyncWidth = PANEL_GetVSyncWidth();
    u4VBackPorch = u4GetPanelVSyncBp();
    #endif

    u4HFrontPorch = (u4HPosition > 0x800) ? (u4HPosition - 0x800) : 0;
	if(u4HTotal > (wDISPLAY_WIDTH + u4HFrontPorch + u4HSyncWidth))
	{
        u4HBackPorch = u4HTotal - wDISPLAY_WIDTH - u4HFrontPorch - u4HSyncWidth;
	}
	else
	{
	    return;
	}
	if(u4VTotal > (wDISPLAY_HEIGHT + u4VSyncWidth + u4VBackPorch))
	{
	    u4VFrontPorch = u4VTotal - wDISPLAY_HEIGHT - u4VSyncWidth - u4VBackPorch;
	}
	else
	{
	    return;
	}
    {
        #define ODFPLimit 4
        #define REORDERBPLimit 4
        u4HFrontPorch = (u4HFrontPorch < ODFPLimit) ? ODFPLimit : u4HFrontPorch;
        u4HBackPorch = (u4HBackPorch < REORDERBPLimit) ? REORDERBPLimit : u4HBackPorch;
    }

    if ((lcdtimingInfo.u4HActive != wDISPLAY_WIDTH) || (lcdtimingInfo.u4VActive != wDISPLAY_HEIGHT) ||
        (lcdtimingInfo.u4HTotal != u4HTotal) || (lcdtimingInfo.u4VTotal != u4VTotal) ||
        (lcdtimingInfo.u4HFP != u4HFrontPorch) || (lcdtimingInfo.u4HBP != u4HBackPorch) ||
        (lcdtimingInfo.u4HSyncWidth != u4HSyncWidth) || (lcdtimingInfo.u4VSyncWidth != u4VSyncWidth)||
        (lcdtimingInfo.u4VFP != u4VFrontPorch) || (lcdtimingInfo.u4VBP != u4VBackPorch))
    {
        #ifndef CC_MTK_LOADER
        CRIT_STATE_T csState;
        csState = x_crit_start();
        #endif

        lcdtimingInfo.u4HTotal = u4HTotal;
        lcdtimingInfo.u4HActive = wDISPLAY_WIDTH;
        lcdtimingInfo.u4HFP = u4HFrontPorch;
        lcdtimingInfo.u4HBP = u4HBackPorch;
		lcdtimingInfo.u4HSyncWidth = u4HSyncWidth;

        lcdtimingInfo.u4VTotal = u4VTotal;
        lcdtimingInfo.u4VActive = wDISPLAY_HEIGHT;
        lcdtimingInfo.u4VFP = u4VFrontPorch;
        lcdtimingInfo.u4VBP = u4VBackPorch;
		lcdtimingInfo.u4VSyncWidth = u4VSyncWidth;
        
        #ifndef CC_MTK_LOADER
        x_crit_end(csState);
        #endif
    }
    //LOG(0, init %d %d %d %d \n",lcdtimingInfo.u4VFP ,lcdtimingInfo.u4VBP,lcdtimingInfo.u4VTotal,lcdtimingInfo.u4VSyncWidth);
}

#ifndef CC_MTK_LOADER
extern void vDrvMJCClockInit(void);
#endif

void vFrameTrackModeInit(void)
{
    if(IS_DISP_4K2K_TYPE3 || u1DrvIsPnl4k2k60WithMemc())
    {
        _u4FrameTrackMode = FRAME_TRACK_MODE_DDDS_BY_FNTSCALER;
    }
    else
    {
        _u4FrameTrackMode = FRAME_TRACK_MODE_DDDS_BY_PSTSCALER + FRAME_TRACK_MODE_VTOTAL_BY_FNTSCALER;
        //_u4FrameTrackMode = FRAME_TRACK_MODE_DDDS_BY_REF;
    }
    LOG(0,"@MTK_TRK [%s][%d] _u4FrameTrackMode:0x%x \n",__FUNCTION__,__LINE__,_u4FrameTrackMode);
}

/**
 * @brief vDrvDisplayInit

 * Init the Display Settings

 * @param  void
 * @retval void
 */
void vDrvDisplayInit(void)
{
#ifndef CC_MTK_LOADER
	UINT32 u4Mask = 0;
#ifndef TURNKEY_3D_CTRL_SETTING
	UINT32 u4Ext3DLRPin, u4Ext3DLRPinFunc;
#endif
#endif

	UINT32 u4Country;
#if defined(CC_S_PLATFORM)
#ifdef CC_MTK_LOADER
    UINT8  u1Src=0;
#endif	
#endif	

    UINT8  u1CustDCLKType = SV_DCLK_UNKNOW;
    UINT32 u4CustFrameRate = 0;

    DRV_GetCustDCLK(&u1CustDCLKType,&u4CustFrameRate);

#ifndef CC_MTK_LOADER
#ifndef TURNKEY_3D_CTRL_SETTING
	u4Ext3DLRPin = 0; 
	u4Ext3DLRPinFunc = 0;
#endif
#endif
// Fix the issue: there is the post&FRC mute when any source display after boots up.
#ifndef CC_MTK_LOADER
#if 0
	if (IS_DISP_4K2K_TYPE1)
	{
		SetPANEL4K2K_TYPE(PANEL_TYPE_4K2K_60);
	}
	else if (IS_DISP_4K2K_TYPE2 || IS_DISP_4K2K_TYPE4)
	{
		SetPANEL4K2K_TYPE(PANEL_TYPE_2K1K_60);
	}
	else if (IS_DISP_4K2K_TYPE3)  //type3 for S_BE
	{
		SetPANEL4K2K_TYPE(PANEL_TYPE_4K2K_60);
	}
    else
    {
        SetPANEL4K2K_TYPE(PANEL_TYPE_MAX);
    }
#endif

	if (((u4DDDSGetErrorLimit()& 0xff) <= 0x07) || IS_FT_HALF_STEP)
	{
		_fgFrTrkHalfStep = TRUE;
		LOG(0, "DisplayInit --> Half step!\n");
	}
#endif

    vScpipSetVTModeCtrl(E_FSC_TG,E_TRK_FRONT_PORCH);
    vScpipSetVTModeCtrl(E_PSC_TG,E_TRK_FRONT_PORCH);
    vScpipSetVTModeCtrl(E_GFX_TG,E_TRK_FRONT_PORCH);
    vScpipSetVTModeCtrl(E_REF_TG,E_TRK_FRONT_PORCH);
    
#ifdef CC_BYPASS_MJC
    vDrvSetMjcPostPathSel(SV_OFF);
    //vIO32WriteFldAlign(OSTG_01, 0, REG_MJC_IN_EN);
#else
    if (PANEL_IsSupport4K2KDD() && (!IS_DISP_4K2K_TYPE3))
    {
        Printf("vDrvDisplayInit mjc config \n");
        vDrvSetMjcPostPathSel(SV_ON);
    }
    else
    {
#ifndef CC_MTK_LOADER
        vDrvMJCClockInit();
#endif
        vDrvSetMjcPostPathSel(SV_OFF);
    }
#endif

    #ifndef CC_MTK_LOADER
    vDrvSetOSTGCroppingMode(0);
    #else
    vDrvSetOSTGCroppingMode(1);
    #endif
    if(!IS_DISP_4K2K_TYPE3)
    {
        if(PANEL_Is8K4K30())
        {
            #ifdef CC_SUPPORT_8K_OVERLAP_PIX
            vDrvSetOSTGCropping(1,32);
            vDrvSetWobblingFinalCrop(1, 32 1, 0);
            //vRegWriteFldAlign(MISC_CTRL, 0, C_OSTG_IN_SEL);//bypass post timgen + post scaler
            u1DrvSetOSTGInSel(0);
            #else
            vDrvSetOSTGCropping(0,0);
            vDrvSetWobblingFinalCrop(0, 0, 0, 1);
            //vRegWriteFldAlign(MISC_CTRL, 1, C_OSTG_IN_SEL);//enable post timgen + post scaler
            u1DrvSetOSTGInSel(1);
            #endif

            #ifndef CC_MTK_LOADER
            vDrvMJCSetMuxToOSTG(1);
            #endif
            vDrvSetOSTG4K2KMode(1); 
        }
        else if(PANEL_Is4K2K120())
        {
            vDrvSetOSTGCropping(1,32);
            vDrvSetWobblingFinalCrop(1, 32, 1, 0);

            #ifndef CC_MTK_LOADER
            vDrvMJCSetMuxToOSTG(1);
            #endif
            vDrvSetOSTG4K2KMode(1); 
            //vRegWriteFldAlign(MISC_CTRL, 1, C_OSTG_IN_SEL);//enable post timgen + post scaler
            u1DrvSetOSTGInSel(1);
        }
        else if(u1DrvIsPnl4k2k60WithMemc())
        {
            vDrvSetOSTGCropping(0,0);
#ifndef CC_MTK_LOADER //mjc tg from FSC
            vDrvMJCSetMuxToOSTG(0);
#endif
            vRegWriteFldAlign(PSCSYS_00, 1, PSC_TIME_GEN_ACK_SEL);
            vRegWriteFldAlign(REG_MEMC_TOP_CTRL_4, 1, MEMC_TOP_C_TG_FROM_OSTG);
            //vRegWriteFldAlign(MISC_CTRL, 1, C_OSTG_IN_SEL);
            vDrvSetOSTG4K2KMode(SV_OFF);
            vDrvSetOSTGMjcEn(SV_ON);
            u1DrvSetOSTGInSel(SV_ON);
            vRegWriteFldAlign(OSTG_01, 0, C_MJC_MASK_SEL);
        }
        else
        {
            vDrvSetOSTGCropping(0,0);
            vDrvSetWobblingFinalCrop(0, 0, 0, 1);

            #ifndef CC_MTK_LOADER
            vDrvMJCSetMuxToOSTG(0);
            #endif
            //vDrvSetOSTG4K2KMode(0);
            //vRegWriteFldAlign(MISC_CTRL, 0, C_OSTG_IN_SEL);//bypass post timgen + post scaler
            //u1DrvSetOSTGInSel(0);
        }
    }
	bSI_DISPLAY_DCLK_TYPE_bak=0xff;

#if defined(SCPIP_SUPPORT_POST_SCALER)
    vPscPreInit();
#endif
    vDrvRegisterDisplayCB();

	if(IS_ENGINEER_DISPLAY_MODE)
	{
		if (PANEL_IsSupport240Hz())
        {
            vDrvSetFixedFrameRate(240);
        }
        else if (PANEL_IsSupport120Hz())
        {
            vDrvSetFixedFrameRate(120);
        }
        else
        {
            vDrvSetFixedFrameRate(60);
        }		
	}
    vFrameTrackModeInit();
	vDrvOutputStageInit();   //cc_supoort_uhd  moved

    if (DRVCUST_InitQuery(eCountryDefaultSetting, &u4Country) != 0)
    {
        /* Cannot get CountryDefaultSetting */
        u4Country = COUNTRY_EU;
    }

    /* PQ Model -> 60Hz (no unnecessary frequency change) */
#if defined(CC_COMBINE_4K_2K)
    if (u4Country != COUNTRY_US)
    {
        UINT32 u4PQType = 0;
        if (DRVCUST_InitQuery(eGetPQType, &u4PQType) != 0)
        {
            if (u4PQType != DRV_CUST_PQ_TYPE_NONE)
            {
                u4Country = COUNTRY_US;
            }
        }
    }
#endif
    LOG(0,"[DisplayInit] country=0x%x (EU=0x%x, US=0x%x)\n", u4Country, COUNTRY_EU, COUNTRY_US);

	// vpll init
	if (!fgIsVOPLLPowerOn())
    {
	#ifdef CC_UBOOT
		vDDDSInit_uboot();
	#else
		vDDDSInit();
	#endif

        if (u1CustDCLKType != SV_DCLK_UNKNOW)  // for NO_SIGNAL: the video path frame rate must be 60Hz, as the same as the GFX path
        {
            vDrvCalPanelTiming(u1CustDCLKType, u4CustFrameRate);
        }
        else
        {
            if(u4Country == COUNTRY_EU)  // EU/CN: 50Hz
            {
                if(IS_ENGINEER_DISPLAY_MODE)
                {
                    if (PANEL_IsSupport240Hz())
                    {
                        vDrvCalPanelTiming(SV_DCLK_60HZ, 240);
                    }
                    else if (PANEL_IsSupport120Hz())
                    {
                        vDrvCalPanelTiming(SV_DCLK_60HZ, 120);
                    }
                    else
                    {
                        vDrvCalPanelTiming(SV_DCLK_60HZ, 60);
                    }
                }
                else
                {
                    LOG(0,"[DisplayInit] ##1 SV_DCLK_50HZ\n");
                    if (PANEL_IsSupport240Hz())
                    {
                        vDrvCalPanelTiming(SV_DCLK_50HZ, 200);
                    }
                    else if (PANEL_IsSupport120Hz())
                    {
                        vDrvCalPanelTiming(SV_DCLK_50HZ, 100);
                    }
                    else
                    {
                        vDrvCalPanelTiming(SV_DCLK_50HZ, 50);
                    }
                }
            }
            else  // US: 60Hz
            {
                LOG(0,"[DisplayInit] ##1 SV_DCLK_60HZ\n");
                if (PANEL_IsSupport240Hz())
                {
                vDrvCalPanelTiming(SV_DCLK_60HZ, 240);
                }
                else if (PANEL_IsSupport120Hz())
                {
                vDrvCalPanelTiming(SV_DCLK_60HZ, 120);
                }
                else
                {
                vDrvCalPanelTiming(SV_DCLK_60HZ, 60);
                }
            }
        }
    }
    else
    {
        if(u1CustDCLKType != SV_DCLK_UNKNOW)
        {
            bSI_DISPLAY_DCLK_TYPE = u1CustDCLKType;
            bOUTPUT_REFRESH_RATE = u4CustFrameRate;
            if(u1CustDCLKType == SV_DCLK_60HZ)
            {
                _u4VPllFrequency = (PANEL_Is4K2K120()||PANEL_Is8K4K30())?(PANEL_GetPixelClk60Hz()>>1):PANEL_GetPixelClk60Hz();//for dynamic MDR
            }
            else
            {
                _u4VPllFrequency = (PANEL_Is4K2K120()||PANEL_Is8K4K30())?(PANEL_GetPixelClk50Hz()>>1):PANEL_GetPixelClk50Hz();//for dynamic MDR
            }
        }
        else
        {
            if(u4Country == COUNTRY_EU)  // EU/CN: 50Hz
            {
                LOG(0,"[DisplayInit] ##2 SV_DCLK_60HZ\n");
                //bootloader use 60Hz display logo.
                bSI_DISPLAY_DCLK_TYPE = SV_DCLK_60HZ;
                if (PANEL_IsSupport240Hz())
                {
                    bOUTPUT_REFRESH_RATE = 200;
                }
                else if (PANEL_IsSupport120Hz())
                {
                    bOUTPUT_REFRESH_RATE = 100;
                }
                else
                {
                    bOUTPUT_REFRESH_RATE = 50;
                }
                _u4VPllFrequency = (PANEL_Is4K2K120()||PANEL_Is8K4K30())?(PANEL_GetPixelClk50Hz()>>1):PANEL_GetPixelClk50Hz();//for dynamic MDR
            }
            else  // US: 60Hz
            {
                LOG(0,"[DisplayInit] ##2 SV_DCLK_60HZ\n");
                bSI_DISPLAY_DCLK_TYPE = SV_DCLK_60HZ;
                if (PANEL_IsSupport240Hz())
                {
                    bOUTPUT_REFRESH_RATE = 240;
                }
                else if (PANEL_IsSupport120Hz())
                {
                    bOUTPUT_REFRESH_RATE = 120;
                }
                else
                {
                    bOUTPUT_REFRESH_RATE = 60;
                }
                _u4VPllFrequency = (PANEL_Is4K2K120()||PANEL_Is8K4K30())? (PANEL_GetPixelClk60Hz()>>1):PANEL_GetPixelClk60Hz();//for dynamic MDR
            }
        }

        
    #ifndef CC_MTK_LOADER
        vDrvPanelTimingInit();
        if(IS_DISP_4K2K_TYPE3) 
        {
            if(u1CustDCLKType != SV_DCLK_UNKNOW)
            {
                vDrvGfxTimingInit(u1CustDCLKType);
            }
            else
            {
                vDrvGfxTimingInit(SV_DCLK_60HZ);
            }
        }
        vDrvSetSSMDRAsInputHsync();//for dynamic MDR
        #if defined(CC_MT5893) || defined(CC_MT5895)
        bDrvUpdateInternalFrameRate();
        #endif
    #endif
        vDrvSwitchImportProtection(0);
    }
	
#if !defined(CC_MTK_LOADER) 
#ifdef SUPPORT_DISP_50TO120
	if(SUPPORT_DISP_50TO120 & SUPPORT_50TO60_2D)
	{	   
	   u4Mask |= SUPPORT_50TO60_2D;
	}
	if(SUPPORT_DISP_50TO120 & SUPPORT_50TO60_3D)
	{
	   u4Mask |= SUPPORT_50TO60_3D;
	}
#endif
#ifdef SUPPORT_DISP_60TO100
	if(SUPPORT_DISP_60TO100 & SUPPORT_60TO50_2D)
	{
	   u4Mask |= SUPPORT_60TO50_2D;
	}
	if(SUPPORT_DISP_60TO100 & SUPPORT_60TO50_3D)
	{
	   u4Mask |= SUPPORT_60TO50_3D;
	}
#endif

#ifdef SUPPORT_DISP_FILM22_48TO120
	if(SUPPORT_DISP_FILM22_48TO120 & SUPPORT_FILM22_48TO120_2D)
	{
	   u4Mask |= SUPPORT_FILM22_48TO120_2D;
	}
#endif  

#ifdef FORCE_3D_60HZ_OUTPUT_ENABLE
	u4Mask |= (SUPPORT_50TO60_3D|SUPPORT_3D_OUTPUT_60);
#endif
#endif

#if !defined(CC_S_PLATFORM)
    if (PANEL_IsUseScanPwm())
    {
		_u4BrightLevel = 50;   // 50 means 50%; 0%:dark --> 100%:bright
    }
    else
    {
		_u4BrightLevel = 0x80; // 0:dark --> 255:bright
    }
#endif

    //vDrvOutputStageInit();    //cc_support_uhd mask
    //vDrvRegisterDisplayCB();
if (!PANEL_IsUseScanPwm())
{	
	vDrvInitPWM((UINT8)DRVCUST_PanelGet(ePanelBacklightPwmPort));
}
else
{
#if defined(CC_S_PLATFORM)	
	#ifdef CC_MTK_LOADER
	for (u1Src=0;u1Src<CUST_SCANPWM_MAX_CH;u1Src++)
		vDrvInitSCANPWM(u1Src+CUST_SCANPWM_FIRST_CH);
	#endif
#else
    vDrvInitSCANPWM((UINT8)DRVCUST_PanelGet(ePanelBacklightPwmPort));
#endif
}
	fgBLCustomCtrl = 1;

    //SET main/pip mute works immediately
    vOSTG_IO32WriteFldAlign(MUTE_00, 1 ,R_MUTE_POST_SEL);
    vOSTG_IO32WriteFldAlign(MUTE_00, 1 ,R_P_MUTE_FRONT_SEL);
    vOSTG_IO32WriteFldAlign(MUTE_00, 1 ,R_M_MUTE_FRONT_SEL);
    // disable video plane
    //vIO32WriteFldAlign(SCPIP_PIP1_02, 0, PIP1_02_RVL_1);
    //vIO32WriteFldAlign(SCPIP_PIP2_02, 0, PIP2_02_RVL_2);

#ifndef CC_MTK_LOADER
    //if ((GetCurrentPanelIndex() == PANEL_CMI_4K2K_FHD120))
    {
        HANDLE_T  hThread;
        if(!_hFrcUnmuteSema)
        {
            VERIFY(x_sema_create(&_hFrcUnmuteSema, X_SEMA_TYPE_BINARY, X_SEMA_STATE_LOCK) == OSR_OK);
            VERIFY(x_sema_create(&_hFrcSetPanelSema, X_SEMA_TYPE_BINARY, X_SEMA_STATE_UNLOCK) == OSR_OK);
            VERIFY(x_thread_create(&hThread, "FRC Unmute", 4096, 60, vFrcUnmute, 0, NULL) == OSR_OK);
            LOG(1,"----[4k2k][I2C] @vDrvDisplayInit 4k2k  ----\n");
            VERIFY(x_timer_create(&h4k2kUnMuteTimer) == OSR_OK);
        }
		vExt4k2kFrcInit();
    }
	
#ifndef TURNKEY_3D_CTRL_SETTING
	if ((DRVCUST_OptQuery(e3DLRPin,&u4Ext3DLRPin)==0)&&(DRVCUST_OptQuery(e3DLRPinFunc,&u4Ext3DLRPinFunc)==0))
#endif
	{
		if(IS_DISPR_INVERSE)
			Panel_3DGlassesCTL(e3DGlasses_P_3DL,e3DGlasses_S_LEYE,e3DGlasses_POl_Bypass,e3DGlasses_POS_1stR,0,0);
		else
			Panel_3DGlassesCTL(e3DGlasses_P_3DL,e3DGlasses_S_DISPR,e3DGlasses_POl_Bypass,e3DGlasses_POS_1stR,0,0);
		Panel_DISPR_FIELD_Delay(eDispRFieldAlign_S_DispR,eDispRFieldAlign_M_Bypass,0);
		Panel_3DGlassesCTLEn(e3DGlasses_P_3DL,1);
	}

    u4DrvSetCustFRC(u4Mask);
	#ifdef CC_MAPLE_CUST_DRV
    LOG(2, "[%s:%d]\n", __FUNCTION__, __LINE__);
    vDrvSetSCANPWMforSPIModeChange();
    vDrvSetSCANPWMforSPIOnOff(TRUE);
    #endif
#endif

	vDrvSwitchOSDRoute();
    LDR_PQModuleInit();
    OSD_Base_SetHStart();

#ifndef CC_MTK_LOADER
#if defined(CC_FBM_SUPPORT_SRS)
    if(IS_IC_5893_ES1())
    {
        u1SRSDynSwitchEn = SV_FALSE;
    }
    else
    {
        u1SRSDynSwitchEn = SV_TRUE;
    }
#endif
#endif

    x_memset(&gVB1ControlBit,0,sizeof(CONTROL_BIT_VB1_T));
}

BOOL _fgPanelDynamicChg = 0;

void PanelSupportDynamicChg(BOOL fgEnable)
{
    #ifdef SUPPORT_PANEL_DYNAMIC_CHANGE
 _fgPanelDynamicChg = fgEnable;
    #endif
}
void vDrvLCDSetFrame(UINT16 wFrameRate)
{
#ifdef SUPPORT_PANEL_DYNAMIC_CHANGE
    if(_fgPanelDynamicChg)
    {
     bSI_DISPLAY_DCLK_TYPE = SV_DCLK_UNKNOW;
    }
#endif

    if (wFrameRate==50)
    {
        vDrvCalPanelTiming(SV_DCLK_50HZ, 50);
    }
    else if (wFrameRate==60)
    {
        vDrvCalPanelTiming(SV_DCLK_60HZ, 60);
    }
    else if (wFrameRate==100)
    {
        vDrvCalPanelTiming(SV_DCLK_50HZ, 100);
    }
    else if (wFrameRate==120)
    {
        vDrvCalPanelTiming(SV_DCLK_60HZ, 120);
    }
    else if (SUPPORT_PANEL_48HZ && (wFrameRate == 48))
    {
        vDrvCalPanelTiming(SV_DCLK_48HZ, wFrameRate);
    }
    else
    {
        vDrvCalPanelTiming(SV_DCLK_OTHERS, (UINT8)wFrameRate);
    }
    #ifdef SUPPORT_PANEL_DYNAMIC_CHANGE
    if (_fgPanelDynamicChg)
    {
        //vDrvMJCSetModeChangeTrig();
        vDrvSetErrorLimit(0);
    }
    #endif
}


#ifndef CC_MTK_LOADER
/**
 * @brief vDrvUpdateInternalFrameRate

 * update internal frame rate between up scalar and MJC

 * @param  void
 * @retval update or not
 */
UINT8 bDrvUpdateInternalFrameRate(void)
{
    #if defined(CC_MT5893) || defined(CC_MT5895)
        UINT32 u4PCLK_Sel;
		UINT8 tveEnable;
	    TVE_GetEnable(TVE_1, &tveEnable);    

        if (fgIsMJCToOSTG())
        {
            u4PCLK_Sel = 2; // poclk_dpll
            if(fgFrameTrackIsRefTrk())
            {
                u4PCLK_Sel = FLD_POCLK_SEL_OCLK;
            }
        }
        else
        {
            u4PCLK_Sel = 0; // oclk
        }
        vIO32WriteFldAlign(CKGEN_DISP_CKCFG, u4PCLK_Sel, FLD_POCLK_SEL);
		if(tveEnable==SV_FALSE)
	    {
	        //set OCLK_TVE (for sub path)
	        vScpipSetOclkTveSelReg((UINT8)u4PCLK_Sel);
	    }
    #endif

    return SV_TRUE;
}
#endif // CC_MTK_LOADER

//get internal frame rate between Front scaler and MJC
UINT16 u2DrvGetInternalFrameRate(void)
{
    if (fgIsMJCToOSTG())
    {
        return _u2IntFrameRate;
    }
    else
    {
        return (UINT16)bOUTPUT_REFRESH_RATE;
    }
}

//set internal frame rate between Front scaler and MJC
void vDrvSetInternalFrameRate(UINT16 frameRate)
{
    if(VDP_LogChkMsk(VDP_1,VDP_LOG_DBG_MSK_TRK)) LOG(0,"@MTK_TRK [%s][%d] FR:%d \n",__FUNCTION__,__LINE__,frameRate);
    _u2IntFrameRate = frameRate;
}

EXTERN BOOL fgIs4k2kNativeTimingOut(UINT8 bPath);


void vDrvCalPanelTiming(UINT8 u1DCLKType, UINT32 u4FrameRate)
{
    UINT32 u4PanelClock;
    UINT32 u4PanelRatio;
    #if !defined(CC_S_PLATFORM)
    static BOOL fgInit = FALSE;
    #endif

    LOG(0, "@MTK_TRK vDrvCalPanelTiming: (DCLKType, FrameRate) = (%d, %d) -> (%d, %d)\n",
        bSI_DISPLAY_DCLK_TYPE, bOUTPUT_REFRESH_RATE,
        u1DCLKType, u4FrameRate);

    if ((bSI_DISPLAY_DCLK_TYPE == u1DCLKType)
        #ifndef CC_MTK_LOADER
        && (bOUTPUT_REFRESH_RATE == u4FrameRate)
        #endif
        && (fg4k2kPNLTypeChg == SV_FALSE)
        && (bfgCroppingChg ==SV_FALSE)
        && (fg4k2kBypPathChg == SV_FALSE)
        && (fg4k2kFrmTrkChg == SV_FALSE)
        && (fg4k2kVrrModChg == SV_FALSE))
    {
        if(VDP_LogChkMsk(VDP_1,VDP_LOG_DBG_MSK_TRK)) LOG(0, "@MTK_TRK vDrvCalPanelTiming Ignore current event");
        #ifndef CC_MTK_LOADER
        bDrvUpdateInternalFrameRate();
        vDrvSetSSMDRAsInputHsync();
        #endif
		return;
    }
    bfgCroppingChg=SV_FALSE;
    fg4k2kBypPathChg = SV_FALSE;
    fg4k2kVrrModChg = SV_FALSE;
    fg4k2kFrmTrkChg = SV_FALSE;

#ifdef CC_S_PLATFORM
#ifndef CC_MTK_LOADER
   if(fgMotionFlowChg || fgFixModeChg)
    {
        _vDrvVideoSetMute(MUTE_MODULE_FRC, SV_VP_MAIN, 10, FALSE);//when signal stabe but AP setting change output frame rate, need mute here to avoid transient garbage.
        fgMotionFlowChg = SV_FALSE;
        fgFixModeChg = SV_FALSE;
    }
#endif
#endif
#ifndef CC_MTK_LOADER
#ifdef CC_S_PLATFORM
            if(fgVB1VideoMute != SV_TRUE)
            {
                fgVB1VideoMute = SV_TRUE;
                if(DRVCUST_Get_InterMemc_type()) vDrvSetPSCMute(SV_TRUE);
                DRVCUST_SendEvent(E_CUST_OUT_MUTE_CHANGE, 0);
            }
#endif
#endif

	u4PanelRatio = u4DrvCalPanelRatio();
    bOUTPUT_REFRESH_RATE = (UINT8)u4FrameRate;

	if (u1DCLKType == SV_DCLK_48HZ)
	{
		u1DCLKType = u1DrvCal48HzOutputType();
	}

    switch (u1DCLKType)
    {
        case SV_DCLK_48HZ:
            u4PanelClock = PANEL_GetPixelClk48Hz();
            bSI_DISPLAY_DCLK_TYPE = SV_DCLK_48HZ;
            break;
		case SV_DCLK_50HZ:
            u4PanelClock = PANEL_GetPixelClk50Hz();
            bSI_DISPLAY_DCLK_TYPE = SV_DCLK_50HZ;
            break;
        case SV_DCLK_60HZ:
            u4PanelClock = PANEL_GetPixelClk60Hz();
            bSI_DISPLAY_DCLK_TYPE = SV_DCLK_60HZ;
            break;
        case SV_DCLK_OTHERS:
        default:
            if (u4FrameRate < (50 << u4PanelRatio))
            {
                u4PanelClock = (PANEL_GetPixelClk50Hz() / (50 << u4PanelRatio)) * bOUTPUT_REFRESH_RATE;
                bSI_DISPLAY_DCLK_TYPE = SV_DCLK_50HZ;
            }
            else
            {
                u4PanelClock = (PANEL_GetPixelClk60Hz() / (60 << u4PanelRatio)) * bOUTPUT_REFRESH_RATE;
                bSI_DISPLAY_DCLK_TYPE = SV_DCLK_60HZ;
            }
            break;
    }

    #if !defined(CC_MTK_LOADER) && defined(CC_DRV_SUPPORT_SMICRO)
    {
        SMICRO_VERTICAL_FREQ eFreq;
        eFreq = (bSI_DISPLAY_DCLK_TYPE == SV_DCLK_48HZ)? FREQ_48HZ :
                (bSI_DISPLAY_DCLK_TYPE == SV_DCLK_50HZ)? FREQ_50HZ : FREQ_60HZ;
        vSMicroPreSetFreq(eFreq);
    }
    #endif

    #if !defined(CC_S_PLATFORM)
    if ((!PANEL_IsUseScanPwm())&&fgInit)
    {
        BeforeOutputFrameRateChange(bOUTPUT_REFRESH_RATE);
    }
    #endif

    vDrvSwitchMTKGoodDclk(SV_OFF);

    vDrvSetRefTiming();
    #if defined(SUPPORT_PANEL_GRAPH_MODE) 
	#ifdef LOAD_PANEL_FROM_FLASH_CUSTOM
	if (FlashPanel_GetControlWord2_GraphModeEn())
	#else
	if (IS_SUPPORT_VB1_GRAPH_MODE)
	#endif
	{ 
	    vDrvSetGFXTiming(bSI_DISPLAY_DCLK_TYPE);
	}
    #endif 
    vDrvSetPanelTiming();

    
    #if defined(CC_SCPOS_3DTV_SUPPORT) && !defined(CC_MTK_LOADER)
    if (u4DrvGetDisp3DModeDclk() != 0)
    {
        u4PanelClock = u4DrvGetDisp3DModeDclk();
        bOUTPUT_REFRESH_RATE = u1DrvGetDisp3DModeRefreshRate();
    }
    #endif

    vDrvVOFreqSet(u4PanelClock);
    
    #ifndef CC_MTK_LOADER
    vDrvSetSSMDRAsInputHsync(); // for dynamic MDR
    #endif

    if (_fgFRS_ENA == SV_ON)
    {
        vDrvSwitchMTKGoodDclk(SV_ON);
    }

    #if !defined(CC_S_PLATFORM)
    if ((!PANEL_IsUseScanPwm())&&fgInit)
    {
        AfterOutputFrameRateChange();
    }
    #endif

    #if !defined(CC_MTK_LOADER) && defined(CC_DRV_SUPPORT_SMICRO)
    {
        SMICRO_VERTICAL_FREQ eFreq;
        SMICRO_MSG rMsg;
        if (SUPPORT_PANEL_48HZ && (bOUTPUT_REFRESH_RATE == (UINT8)(48 * u4Ratio)))
        {
            eFreq = FREQ_48HZ;
        }
        else if (bOUTPUT_REFRESH_RATE < (UINT8)(55 * u4Ratio))
        {
            eFreq = FREQ_50HZ;
        }
        else
        {
            eFreq = FREQ_60HZ;
        }
        rMsg.u4ModuleId = MODULEID_PANEL_PARAM;
        rMsg.u4CommandId = CMD17_SET_VERTICAL_FREQ;
        rMsg.aPrm[0] = (UINT8)eFreq;
        rMsg.pCallbackFcn = NULL;
        vSMicroSendEvent(rMsg);
    }
    #endif

    #if !defined(CC_S_PLATFORM)
    if ((!PANEL_IsUseScanPwm())&&(!fgInit))
    {
        fgInit = TRUE;
    }
    #endif
}


UINT8 vDrvGetLCDFreq(void)
{
    return bOUTPUT_REFRESH_RATE;
}

void OSTG_SetPixelShift(BOOL fgEnable, INT32 i4HPixelShift, INT32 i4VPixelShift)
{
    BOOL fgOriPixelShiftEnable;

    if ((ABS(i4HPixelShift) > PIXEL_SHIFT_H_MAX) || (ABS(i4VPixelShift) > PIXEL_SHIFT_V_MAX))
    {
        //LOG(1, "pixel shift out of range, i4HPixelShift=%d, i4VPixelShift=%d\n",  i4HPixelShift, i4VPixelShift);
        return;
    }
    //LOG(5, "PMX_SetPixelShift, i4HPixelShift=%d, i4VPixelShift=%d\n",  i4HPixelShift, i4VPixelShift);

    fgOriPixelShiftEnable =  PANEL_IsPixelShift();
    PANEL_SetPixelShift(fgEnable, i4HPixelShift, i4VPixelShift);

    // turn on pixel shift
    if (fgEnable)
    {
//        _i4HPixelShift = i4HPixelShift;
        if (!fgOriPixelShiftEnable)
        {
            //LOG(5, "turn on pixel shift, width=%d, height=%d\n", PANEL_GetPanelWidth(), PANEL_GetPanelHeight());
        }
        _u4OstgUpdateEvent |= OSTG_UPDATE_PIXEL_SHIFT_H;
        _u4OstgUpdateEvent |= OSTG_UPDATE_PIXEL_SHIFT_V;
    }
    else if (!fgEnable && fgOriPixelShiftEnable)     // previous is enable, restore timing
    {
        vDrvSetVsyncBp((UINT16)u4GetPanelVSyncBp());

      //  LOG(5, "turn off pixel shift, width=%d, height=%d\n", PANEL_GetPanelWidth(), PANEL_GetPanelHeight());
    }
}

void Panel_SetDisplayShift(BOOL fgEnable, UINT8 u1ShiftMode,UINT8 u1L1DisplayShift, UINT8 u1R2DisplayShift)
{
  vIO32WriteFldAlign(VINTF_P17, fgEnable, VINTF_3D_DISP_SHIFT_EN);
  vIO32WriteFldAlign(VINTF_P17, u1ShiftMode, VINTF_3D_DISP_SHIFT_FRAME);
  vIO32WriteFldAlign(VINTF_P17, 1, VINTF_3D_DISP_SHIFT_CTL);
  vIO32WriteFldAlign(VINTF_P17, u1L1DisplayShift, VINTF_3D_DISP_SHIFT_VALUE_L);
  vIO32WriteFldAlign(VINTF_P17, u1R2DisplayShift, VINTF_3D_DISP_SHIFT_VALUE_R);
}

void Panel_SetDisplayShiftColor(UINT8 u1ShiftColorB,UINT8 u1ShiftColorG, UINT8 u1ShiftColorR)
{
  vIO32WriteFldAlign(VINTF_P18, u1ShiftColorB, VINTF_3D_DISP_SHIFT_B);
  vIO32WriteFldAlign(VINTF_P18, u1ShiftColorG, VINTF_3D_DISP_SHIFT_G);
  vIO32WriteFldAlign(VINTF_P18, u1ShiftColorR, VINTF_3D_DISP_SHIFT_R);
}

void Panel_DISPR_FIELD_Delay(UINT8 u1Tpye, UINT8 u1AlignType,UINT16 u1DelayLine)
{
 if (u1Tpye == eDispRFieldAlign_S_Field)
 {

 }
 else
 {
    #if 0
   //#if defined(CC_MT5368) || defined(CC_MT5389)
   if ((BSP_GetIcVersion() >= IC_VER_5368_AB)||(BSP_GetIcVersion() >= IC_VER_5389_AB))
   {
    vIO32WriteFldAlign(VINTF_P19, u1AlignType, VINTF_DISP_R_ALLIGN_TYPE);
   }
   else
   {
    vIO32WriteFldAlign(VINTF_P19, u1AlignType, VINTF_DISP_R_ALLIGN_TYPE_NOECO);
   }
   #else
   vIO32WriteFldAlign(VINTF_P19, u1AlignType, VINTF_DISP_R_ALLIGN_TYPE);
   #endif

   // DISP_R delay limitaion: "0~1122" OK, "1123" need bypass 6 line buffer, 1124 can't reach
   if(u1DelayLine < (wDrvGetOutputVTotal()-2))
   {
    vIO32WriteFldAlign(VINTF_P19, u1DelayLine, VINTF_DISP_R_H_DLY);
   }
   else
   {
    vIO32WriteFldAlign(VINTF_P19, 0, VINTF_DISP_R_H_DLY);
   }
 }
}

void Panel_3DGlassesCTL(UINT8 u1Pin, UINT8 u1Tpye, BOOL fgPol,BOOL fgPos, UINT8 u1DelayFrame, UINT16 u2DelayLine)
{

#ifndef CC_MTK_LOADER
	UINT32 u4Ext3DLRPin =0, u4Ext3DLRPinFunc = 0;
	if ((DRVCUST_OptQuery(e3DLRPin,&u4Ext3DLRPin)==0)&&(DRVCUST_OptQuery(e3DLRPinFunc,&u4Ext3DLRPinFunc)==0))
	{
		LOG(3, "[3DGlassesCTL] 0x%x set to FUNCTION%d\n", u4Ext3DLRPin, u4Ext3DLRPinFunc);
        UNUSED(BSP_PinSet(u4Ext3DLRPin, u4Ext3DLRPinFunc));
	}
	else
#endif
	{	
	#ifdef TURNKEY_3D_CTRL_SETTING	
		UINT32 u4Ext3DLRPin =0, u4Ext3DLRPinFunc = 0;
	#if defined(CC_MT5893) || defined(CC_MT5895)
		if (u1Pin == e3DGlasses_P_3DR)
		{
			u4Ext3DLRPin = PIN_GPIO52;
			u4Ext3DLRPinFunc = PINMUX_FUNCTION1;
			LOG(3, "[3DGlassesCTL] 3DR:0x%x set to FUNCTION%d\n", PIN_GPIO52, PINMUX_FUNCTION1);
		}
		else if (u1Pin == e3DGlasses_P_3DL)
		{
			u4Ext3DLRPin = PIN_GPIO53;
			u4Ext3DLRPinFunc = PINMUX_FUNCTION1;
			LOG(3, "[3DGlassesCTL] 3DL:0x%x set to FUNCTION%d\n", PIN_GPIO53, PINMUX_FUNCTION1);
		}
		else if (u1Pin == e3DGlasses_P_3DBL)
		{
			u4Ext3DLRPin = PIN_GPIO54;
			u4Ext3DLRPinFunc = PINMUX_FUNCTION1;
			LOG(3, "[3DGlassesCTL] 3DBL:0x%x set to FUNCTION%d\n", PIN_GPIO54, PINMUX_FUNCTION1);
		}
	// #elif defined(CC_MT5880)
	#else
		if (u1Pin == e3DGlasses_P_3DR)
		{
			u4Ext3DLRPin = PIN_GPIO6;
			u4Ext3DLRPinFunc = PINMUX_FUNCTION2;
			LOG(3, "[3DGlassesCTL] 3DR:0x%x set to FUNCTION%d\n", PIN_GPIO6, PINMUX_FUNCTION2);
		}
		else if (u1Pin == e3DGlasses_P_3DL)
		{
			u4Ext3DLRPin = PIN_GPIO7;
			u4Ext3DLRPinFunc = PINMUX_FUNCTION2;
			LOG(3, "[3DGlassesCTL] 3DL:0x%x set to FUNCTION%d\n", PIN_GPIO7, PINMUX_FUNCTION2);
		}
		else if (u1Pin == e3DGlasses_P_3DBL)
		{
			u4Ext3DLRPin = PIN_GPIO8;
			u4Ext3DLRPinFunc = PINMUX_FUNCTION2;
			LOG(3, "[3DGlassesCTL] 3DBL:0x%x set to FUNCTION%d\n", PIN_GPIO8, PINMUX_FUNCTION2);
		}
	#endif
	
		UNUSED(BSP_PinSet(u4Ext3DLRPin, u4Ext3DLRPinFunc));
	#endif
	}
  vIO32WriteFldAlign(VINTF_P9 + (u1Pin *8), u1Tpye, VINTF_REYE_SEL);

  vIO32WriteFldAlign(VINTF_P7 + (u1Tpye *8), fgPos, VINTF_2EYE_POL);
  vIO32WriteFldAlign(VINTF_P7 + (u1Tpye *8), fgPos, VINTF_REYE_V_POS); // 2EYE with V_POS

   if (u1Tpye == e3DGlasses_S_BL)
   {
    vIO32WriteFldAlign(VINTF_P7 + (u1Tpye *8), 1, VINTF_3DBL_LRLR);
   }
   else
   {
   vIO32WriteFldAlign(VINTF_P7 + (u1Tpye *8), 1, VINTF_2EYE_LRLR);
   }

  vIO32WriteFldAlign(VINTF_P7 + (u1Tpye *8), u1DelayFrame, VINTF_2EYE_V_DLY);
  vIO32WriteFldAlign(VINTF_P6 + (u1Tpye *8), u2DelayLine, VINTF_2EYE_H_DLY);

  if (u1Tpye == e3DGlasses_S_2EYE)
  {
   vIO32WriteFldAlign(VINTF_P7, u1Tpye, VINTF_2EYE_SEL);
   vIO32WriteFldAlign(VINTF_P7, fgPol, VINTF_2EYE_POL);
   vIO32WriteFldAlign(VINTF_P7, u1DelayFrame, VINTF_2EYE_V_DLY);
   vIO32WriteFldAlign(VINTF_P6, u2DelayLine, VINTF_2EYE_H_DLY);
  }

  if (u1Tpye == e3DGlasses_S_BL2)
  {
   vIO32WriteFldAlign(VINTF_P26, u1Tpye, VINTF_3DBL_2_SEL);
   vIO32WriteFldAlign(VINTF_P26, fgPol, VINTF_3DBL_2_POL);
   vIO32WriteFldAlign(VINTF_P26, 1, VINTF_3DBL_2_LRLR);
   vIO32WriteFldAlign(VINTF_P26, u1DelayFrame, VINTF_3DBL_2_V_DLY);
   vIO32WriteFldAlign(VINTF_P25, u2DelayLine, VINTF_3DBL_2_H_DLY);
  }
}

void Panel_3DGlassesDelay(UINT8 u1Pin, UINT16 u2DelayLine)
{
    if (u1Pin == e3DGlasses_P_3DR)
    {
        vIO32WriteFldAlign(VINTF_P8, u2DelayLine, VINTF_REYE_H_DLY);
    }
    else if (u1Pin == e3DGlasses_P_3DL)
    {
        vIO32WriteFldAlign(VINTF_P10, u2DelayLine, VINTF_LEYE_H_DLY);
    }
    else if (u1Pin == e3DGlasses_P_3DBL)
    {
        vIO32WriteFldAlign(VINTF_P12, u2DelayLine, VINTF_3DBL_H_DLY);
    }

    w3DGlassesDelay = u2DelayLine;
}

void Panel_3DGlassesRSTCTL(UINT8 u1Tpye, BOOL fgOnOff, UINT16 u2RSTLine)
{
  vIO32WriteFldAlign(VINTF_P7 + (u1Tpye *8), fgOnOff, VINTF_2EYE_RST_MODE);
  vIO32WriteFldAlign(VINTF_P6 + (u1Tpye *8), u2RSTLine, VINTF_2EYE_WIDTH);
}

void Panel_3DGlassesDutyCTL(UINT8 u1Tpye, UINT8 u1DutyH)
{
    UINT16 u4Vtotal,u4VtotalH;
    u4Vtotal = wDrvGetOutputVTotal();

    if (IS_PANEL_L12R12)
    {
     u4Vtotal = u4Vtotal >>1;
    }

     u4VtotalH = u4Vtotal *  u1DutyH / 100 ;


    if (u4VtotalH >= 1)
    {
      u4VtotalH = u4VtotalH - 1;
    }

    Panel_3DGlassesRSTCTL(u1Tpye, 1, u4VtotalH);
}

// fgonoff = 0 [off ] fgonoff = 1 [on ]
void Panel_3DGlassesCTLEn(UINT8 u1Pin, BOOL fgOnfff)
{
    vIO32WriteFldAlign(VINTF_P9 + (u1Pin * 8), !fgOnfff, VINTF_REYE_OP_EN);
    LOG(0, "Panel_3DGlassesCTLEn: %d, %d\n", u1Pin, fgOnfff);
    if (e3DGlasses_P_3DBL == u1Pin)
    {
        LOG(0, "Panel_3DGlassesCTLEn: %d\n", fgOnfff);
        vIO32WriteFldAlign(VINTF_P9 + (u1Pin * 8), (fgOnfff) ? 3 : 7, VINTF_REYE_SEL);
    }
}

void Panel_2ndChannelOnOff(BOOL fgOnfff)
{
    if (IS_PANEL_L12R12)
    {
        if (LVDS_OUTPUT_PORT == FOUR_PORT)
        {
            LOG(0, "Panel_2ndChannelOnOff: %d, %d\n", fgOnfff);
            vIO32WriteFldAlign(LVDSB_REG07, (fgOnfff)? 0x0 : 0xCFFC00, RG_PD);
        }
        else if (LVDS_OUTPUT_PORT == DUAL_PORT)
        {
            LOG(0, "Panel_2ndChannelOnOff: %d, %d\n", fgOnfff);
            vIO32WriteFldAlign(LVDSB_REG07, (fgOnfff)? 0x0 : 0xEFFFE0, RG_PD);
        }
    }
}

void vDrvDisplaySetPentouchMode(UINT32 fgOnOff)
{
    if (fgPentouchMode != fgOnOff)
    {
        fgPentouchMode = (UINT8)fgOnOff;
        vSetMainFlg(MAIN_FLG_MODE_CHG);
        vSetMainFlg(MAIN_FLG_MODE_DET_DONE);
    }
}

UINT32 fgDrvDisplayGetPentouchMode(void)
{
    return fgPentouchMode;
}

void vDrvVideoSetMuteColor(UINT8 bPath, UINT32 u4Bg)
{
    RDispInfo *prDispInfo;
    prDispInfo = getDispInfo(bPath);

    if (prDispInfo->u4MuteColor != u4Bg)
    {
        prDispInfo->u4MuteColor = u4Bg;
        if (bPath == SV_VP_MAIN)
        {
            vSetScposFlg(MAIN_MUTECOLOR_CHG);
        }
        else
        {
            vSetScposFlg(PIP_MUTECOLOR_CHG);
        }
    }
}

/**
* @brief Output stage normproc
*/
void vOstgNormProc(void)
{

}

// OSTG Event on Output VSync
// Note: Keep this function simple since this function is called by ISR
void OSTG_OnOutputVSync(void)
{

#ifndef CC_MTK_PRELOADER
    if (_u4OstgUpdateEvent & OSTG_UPDATE_PIXEL_SHIFT_H)
    {
        _u4OstgUpdateEvent &= ~OSTG_UPDATE_PIXEL_SHIFT_H;
    }
    if (_u4OstgUpdateEvent & OSTG_UPDATE_PIXEL_SHIFT_V)
    {
        vDrvSetVsyncBp((UINT16)u4GetPanelVSyncBp());
        _u4OstgUpdateEvent &= ~OSTG_UPDATE_PIXEL_SHIFT_V;
    }
#endif

#ifndef CC_MTK_LOADER
    if (fgIsScposFlgSet(MAIN_MUTECOLOR_CHG))
    {
        vDrvVideoMuteColor(SV_VP_MAIN, _rMDispInfo.u4MuteColor);
        vClrScposFlg(MAIN_MUTECOLOR_CHG);
    }
    if (fgIsScposFlgSet(PIP_MUTECOLOR_CHG))
    {
        vDrvVideoMuteColor(SV_VP_PIP, _rPDispInfo.u4MuteColor);
        vClrScposFlg(PIP_MUTECOLOR_CHG);
    }

#endif

#ifndef CC_MTK_LOADER
#ifdef LVDS_ESD_PROTECT
	if (_fgESDProtect)
	{
		if (fgIsLVDSFIFOSkew())
		{
			vLVDSFIFOSkew_Recover();
		}
	}
#endif
#endif 
}

//----------------------------------------------------------
//REG_MJC_IN_EN=1 OSTG connect to MJC 
//REG_MJC_IN_EN=0, && C_MJC_SYNC_EN=0   ==>   bypass MJC
//----------------------------------------------------------
void vDrvSetOSTGMjcEn(UINT8 fgCtrl)
{
   vRegWriteFldAlign(OSTG_01, fgCtrl, REG_MJC_IN_EN);
   if(u1IsPanelSupport4K2K120())
		vRegWriteFldAlign(OSTG_01 + OSTG_2CH_OFFSET, fgCtrl, REG_MJC_IN_EN);
}

UINT8 DrvGetOSTGMjcEn(void)
{
    if (IO32ReadFldAlign(OSTG_01, REG_MJC_IN_EN))    //mt5887 bring up test
        return( SV_TRUE );
    else
        return ( SV_FALSE);
}
//--------------------------------------------------------------
//REG_MJC_IN_EN=0, && C_MJC_SYNC_EN=0   ==>   bypass MJC
//-----------------------------------------------------------

void vDrvSetOSTG4K2KMode(UINT8 u1Enable)
{
    //vRegWriteFldAlign(MISC_CTRL, u1Enable, C_OSTG_IN_SEL);   
    vRegWriteFldAlign(OSTG_00, u1Enable, C_MJC_SYNC_EN);
    //vRegWriteFldAlign(OSTG_00 + OSTG_2CH_OFFSET, u1Enable, C_MJC_SYNC_EN);    
    if(u1IsPanelSupport4K2K120())
		vRegWriteFldAlign(OSTG_00 + OSTG_2CH_OFFSET, u1Enable, C_MJC_SYNC_EN);
}

//------------------------------------------------------------
//C_OSTG_IN_SELostg in connect to MJC 
//C_OSTG_IN_SEL=0, ostg in connect to 0
//--------------------------------------------------------------
UINT8 u1DrvGetOSTGInSel(void)
{
   return (UINT8)RegReadFldAlign(MISC_CTRL, C_OSTG_IN_SEL);    
}

void  u1DrvSetOSTGInSel(UINT8 u1OstgInsel)
{
   vRegWriteFldAlign(MISC_CTRL,u1OstgInsel, C_OSTG_IN_SEL);    
   if(u1IsPanelSupport4K2K120())
   		vRegWriteFldAlign(MISC_CTRL + OSTG_2CH_OFFSET,u1OstgInsel, C_OSTG_IN_SEL);  
}
//--------------------------------------------------------------
//--------------------------------------------------------------
UINT8 u1DrvGetOSTG4K2KMode(void)
{
   return (UINT8)RegReadFldAlign(OSTG_00, C_MJC_SYNC_EN);
}

// Off should bypass memc
void vDrvSetMjcPostPathSel(UINT8 u1OnOff)
{
    if(u1DrvIsPnl4k2k60WithMemc())
    {
        vDrvSetOSTG4K2KMode(SV_FALSE);
    }
    else
    {
        vDrvSetOSTG4K2KMode(u1OnOff);
    }

    vDrvSetOSTGMjcEn(u1OnOff);
    u1DrvSetOSTGInSel(u1OnOff);
}

void vDrvSetOSTGCroppingMode(UINT8 u1Mode)
{
    vRegWriteFldAlign(DEMURA_CTRL_0 + OSTG_2CH_OFFSET, u1Mode, C_CROP_MODE);  
}

void vDrvSetOSTGCropping(UINT8 u1Enable,UINT8 u1Cnt)
{
    if(u1CroppingCnt != u1Cnt || u1Enable != u1CroppingEna)
	{
	    bfgCroppingChg= SV_TRUE;
        if(u1Enable == SV_TRUE)
        {
            //OSD_SetCropWidth(u1Cnt); 
        }
        else
        {
            //OSD_SetCropWidth(0); 
        }
	}
    u1CroppingCnt = u1Cnt;
    u1CroppingEna = u1Enable;
    vRegWriteFldAlign(CROPPING_DLY, !u1Enable, CROPPING_BYPASS);
    vRegWriteFldAlign(CROPPING_DLY, u1Cnt, CROPPING_LATENCY); 
}

UINT8 vDrvGetOSTGCroppingCnt(void)
{
    if(u1CroppingEna == SV_TRUE)
    {
        return u1CroppingCnt;
    }
    else
    {
        return 0;
    } 
}

UINT8 u1IsPanelSupport4K2K120(void)
{
	if (PANEL_Is4K2K120())
	{
    	return 1;
	}
	else
	{
		return 0;
	}
}

UINT8 u1IsPanelSupport8K4K30(void)
{
	if (PANEL_Is8K4K30())
	{
    	return 1;
	}
	else
	{
		return 0;
	}
}


UINT8 u1IsPanelSupport8K4K60(void)
{
    if (PANEL_Is8K4K60())
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

void vDrvSwitchImportProtection(UINT8 bOn_Off)
{
}

void vDrvFireImportPortection(void)
{
    vDrvSetSCANPWMforSPIOnOff(FALSE);
    if(fgIsMJCToOSTG())
    {
        vPscSetData(1);
        LOG(3,"PSC Set data\n");
    }
    else
    {
        vScpipSetData(1);
        LOG(3,"FSC Set data\n");
    }
}

void vDrvSetErrorLimit(UINT32 u4Error)
{
 	UINT16 u2ErrLim=0;
	UINT16 u2ErrLimLow=0;
	UINT32 u4ErrLimPre=0;

	u4ErrLimPre = u4DDDSGetErrorLimit();

	if (u4Error == 0xFFFFFFFF)
	{
    	u4Error = (IS_VBYONE?ERROR_LIMIT_FOR_VB1:(((IS_PANEL_L12R12)||(IS_PANEL_2D_N_3D_L12R12))?ERROR_LIMIT_FOR_PDP:ERRORLIMIT_DEFAULT));
	}

	LOG(0, "[DDDS]u4Error=0x%8lx, u4ErrLimPre=0x%8lx\n", u4Error, u4ErrLimPre);
    if (u4Error != u4ErrLimPre)
    {
		u2ErrLim = (UINT16)(u4Error & 0xff);
		u2ErrLimLow = (UINT16)(u4Error >> 16);
		vIO32WriteFldAlign(DDDS_03, u2ErrLim, DDDS_03_DDDS_ERR_LIM);
		vIO32WriteFldAlign(DDDS_1C, u2ErrLimLow, DDDS_1C_NOR_DDDS_LIM_LOW);
    }
}

void vDDDSCalErrLimDown(UINT32 u4ErrLim)
{
#if DDDS_SYMMETRIC_ERR_LIM
	vDrvCalDownErrorLimit(u4ErrLim);
#else
	
#endif
}

UINT32 u4DDDSCalStepValue(UINT8 bLevel)
{
	return (u4DrvCalStepValue(bLevel));
}

void vDDDSCalStepLimit(UINT32 u4Value)
{
	vDrvCalStepLimit(u4Value);
}

void vDDDSEnableStepLimit(UINT8 bEnable)
{
	vDrvEnableStepLimit(bEnable);
}

UINT32 u4DDDSGetErrorLimit(void)
{
	UINT32 u4ErrorLimit = 0;
	u4ErrorLimit = (IO32ReadFldAlign(DDDS_1C, DDDS_1C_NOR_DDDS_LIM_LOW)<<16) + IO32ReadFldAlign(DDDS_03, DDDS_03_DDDS_ERR_LIM);
	return u4ErrorLimit;
}

UINT8 u1DDDSGetStepLimitLevel(void)
{
	return (_u1StepLimitLevel);
}

#ifndef CC_MTK_LOADER
#ifdef SUPPORT_SW_FRAME_CHECK
void vSetSWFrameTrackOnOff(UINT32 u4OnOff)
{
    if (_u4VDistTrackEn != u4OnOff)
    {
        LOG(0, "SW Frame Track (%d)\n", u4OnOff);
        _u4VDistTrackEn = u4OnOff;
    }
}

UINT32 vGetSWFrameTrackOnOff(void)
{
    return _u4VDistTrackEn;
}

void vSetSWFrameTrackStep(UINT32 u4Step)
{
    if (_u4VDistTrackMaxStep != u4Step)
    {
        LOG(0, "SW Frame Track Step (%d)\n", u4Step);
        _u4VDistTrackMaxStep = u4Step;
    }
}

void vSetSWFrameTrackLockRange(UINT32 u4Upper, UINT32 u4Lower)
{
    if (_u4VDistUpperBound != u4Upper)
    {
        LOG(0, "SW Frame Track Upper Bound (%d --> %d)\n", _u4VDistUpperBound, u4Upper);
        _u4VDistUpperBound = u4Upper;
    }
    if (_u4VDistLowerBound != u4Lower)
    {
        LOG(0, "SW Frame Track Lower Bound (%d --> %d)\n", _u4VDistLowerBound, u4Lower);
        _u4VDistLowerBound = u4Lower;
    }
}

void vSetSWFrameTrackQueary(void)
{
	LOG(0, "SW Frame Track enable %d\n", _u4VDistTrackEn);
	LOG(0, "SW Frame Track stp    %d _u4VDistTrackLocked %d _u4VDistTrackState %d\n", _u4VDistTrackMaxStep,_u4VDistTrackLocked,_u4VDistTrackState);
	LOG(0, "SW Frame Track UpperBound  %d LowerBound %d\n", _u4VDistUpperBound, _u4VDistLowerBound);
}

void vDrvVDistanceCheck(void)
{
	UINT32 u4VDistance,u4FrameTrackLine;
	UINT32 u4LowerBound, u4UpperBound,u4LockWin;
	UINT32 u4VTotal;
	UINT32 u4UpTrackRange, u4DownTrackRange;
	UINT8 bScaleMode =	u1ScpipGetDispMode(SV_VP_MAIN) ;

	if((IO32ReadFldAlign(DDDS_00,DDDS_00_DISP_EN)== 0) || (_u4VDistTrackEn == 0) || (bDrvCheckMTKGoodStatus() == SV_FALSE))	 
	{
		_u4UpTrackRange = 0;
		_u4DownTrackRange = 0;
		_u4VDistTrackState = 0;
		_u4VDistTrackLocked = 0;
		LOG(5, "vDrvVDistanceCheck() : DISP_EN=0, _u4VDistTrackEn =0,return.\n");
		
		return;
	}

	u4LockWin = vScpipGetFrmTrkLockWin();
	u4VDistance = u4ScpipGetInOutVDistance();
	u4VTotal = (UINT32)wDrvGetOutputVTotal();
	u4FrameTrackLine = (UINT32)vScpipGetFrmTrkLines();
	
	
	u4UpperBound = u4FrameTrackLine + u4LockWin;
	if(u4FrameTrackLine >= u4LockWin)
	{
	    u4LowerBound = u4FrameTrackLine - u4LockWin;
	}
	else
	{
	    u4LowerBound = u4VTotal + u4LockWin - u4FrameTrackLine;
	}

	// get tracking distance
	u4UpTrackRange = 0;
	u4DownTrackRange = 0;
	if (u4UpperBound >= u4LowerBound)
	{
		if (u4VDistance > u4UpperBound)
		{
			u4DownTrackRange = u4VDistance - u4UpperBound;
			u4UpTrackRange = u4VTotal + u4LowerBound - u4VDistance;
			_u4VDistTrackState =(u4DownTrackRange <= u4UpTrackRange)? 1 : 2;
			LOG(5,"case 1: track state(%d),Current Distance(%d),u4DownTrackRange(%d),u4UpTrackRange(%d)\n",_u4VDistTrackState,u4VDistance,u4DownTrackRange,u4UpTrackRange);
		}
		else if (u4VDistance < u4LowerBound)
		{
			u4DownTrackRange = u4VDistance + u4VTotal - u4UpperBound;
			u4UpTrackRange = u4LowerBound - u4VDistance;
			_u4VDistTrackState =(u4DownTrackRange <= u4UpTrackRange)? 1 : 2;
			LOG(5,"case 1: track state(%d),Current Distance(%d),u4DownTrackRange(%d),u4UpTrackRange(%d)\n",_u4VDistTrackState,u4VDistance,u4DownTrackRange,u4UpTrackRange);
		}
		else
		{
		    _u4VDistTrackState = 0;
		}
	}
	else  // u4UpperBound < u4LowerBound
	{
		if ((u4VDistance > u4UpperBound) && (u4VDistance < u4LowerBound))
		{
			u4DownTrackRange = u4VDistance - u4UpperBound;
			u4UpTrackRange = u4LowerBound - u4VDistance;		
			_u4VDistTrackState =(u4DownTrackRange <= u4UpTrackRange)? 1 : 2;
			LOG(1,"case 5: track state(%d),Current Distance(%d),u4DownTrackRange(%d),u4UpTrackRange(%d)\n",_u4VDistTrackState,u4VDistance,u4DownTrackRange,u4UpTrackRange);
		}
	}

	if (_u4VDistTrackEn)
	{
		if (_u4VDistTrackLocked < 2)
		{
			if ((u4UpTrackRange > _u4VDistTorance) && (u4DownTrackRange > _u4VDistTorance))
			{
				// start tracking
				_u4VDistTrackLocked = 0;
				_u4UpTrackRange = u4UpTrackRange;
				_u4DownTrackRange = u4DownTrackRange;
				LOG(5, "SW frame tracking.\n");
			}
			else
			{
				_u4VDistTrackLocked ++;
				if (_u4VDistTrackLocked == 2)
				{
					_u4VDistTrackState = 3;// SW frame track locked.
					LOG(5, "SW frame track locked.\n");
				}
			}
			vDrvTuneFrontPorchBySWTrack();
		}
		else  if ((u4UpTrackRange > _u4VLossDistTorance) && (u4DownTrackRange > _u4VLossDistTorance))
		{
			_u4VDistTrackLocked = 0;
			_u4VDistTrackState = 0;
		    LOG(2, "SW frame track : Loss lock,need re-track\n");
		}
	}
	else
	{
		if (_u4VDistTrackState)
		{
			// recover
			_u4UpTrackRange = 0;
			_u4DownTrackRange = 0;
			_u4VDistTrackState = 0;
			_u4VDistTrackLocked = 0;
			LOG(5, "SW frame track : Off.\n");
		}
	}
}

void vDrvTuneFrontPorchBySWTrack(void)
{
	UINT8 u4VStep;
	UINT16 u2TrackFrame;
    UINT32  u4VDistance, u4FP, u4Vtotal;
	static UINT32 u4OriVtotal = 0;
	u4FP = lcdtimingInfo.u4VFP;
	u4Vtotal = lcdtimingInfo.u4VTotal;
	u2TrackFrame = _u4VTrackOutCnt + 1;

	if((_u4VDistTrackState == 3)|| (_u4VDistTrackLocked != 0))
	{
		LOG(5, "SW frame u4VDistTrackLocked(%d)\n",_u4VDistTrackLocked);
    }
	else if (_u4VDistTrackState == 1) // down tracking
    {
        u4VStep =  (_u4DownTrackRange > _u4VDistTrackMaxStep*u2TrackFrame)? _u4VDistTrackMaxStep: 1;
		u4Vtotal = u4Vtotal + u4VStep;
		u4FP = u4FP + u4VStep;
		
		LOG(5, "down Tracking u4VStep(%d),new vtotal(%d),new FP(%d),u2TrackFrame(%d)\n",u4VStep,u4Vtotal,u4FP,u2TrackFrame);
    }
    else if (_u4VDistTrackState == 2)  // up tracking
    {
        u4VStep =  (_u4UpTrackRange > _u4VDistTrackMaxStep*u2TrackFrame)? _u4VDistTrackMaxStep: 1;
		if(u4Vtotal > u4VStep)
		{
		    u4Vtotal = u4Vtotal - u4VStep;
			u4FP = u4FP - u4VStep;
		}
		LOG(5, "up Tracking u4VStep(%d),new vtotal(%d),new FP(%d)\n",u4Vtotal,u4FP);
    }
	if((u4OriVtotal != u4Vtotal)|| (u4Vtotal != wDrvGetOutputVTotal()))
	{
	    LOG(5, "adjust new vtotal to (%d),new FP to (%d)\n",u4VStep,u4Vtotal,u4FP);
	    u4OriVtotal = u4Vtotal;
		vDrvSetVsyncFp(u4FP);
		wDrvSetOutputVTotal(u4Vtotal);
		vDrvFireImportPortection();
	}
	
}
#endif
#endif

#ifdef CC_ENABLE_MTK_MODEL_INDEX
UINT8 u1UseSuperFlipMirror=0;
UINT8 u1SuperFlipMirrorCfg=0;
UINT8 u1SetFlipMirrorConfig(BOOL fgMirrorEn, BOOL fgFlipEn);

void vSetSuperFlipMirrorConfig(UINT8 u1SuperCfgOnOff, UINT8 u1SuperFlip, UINT8 u1SuperMirror)
{
    u1UseSuperFlipMirror =u1SuperCfgOnOff;

    if(u1SuperFlip)
    {
        u1SuperFlipMirrorCfg |= SYS_FLIP_CONFIG_ON;
    }
    else
    {
        u1SuperFlipMirrorCfg &= (~SYS_FLIP_CONFIG_ON);
    }

    if(u1SuperMirror)
    {
        u1SuperFlipMirrorCfg  |= SYS_MIRROR_CONFIG_ON;

    }
    else
    {
        u1SuperFlipMirrorCfg &= (~SYS_MIRROR_CONFIG_ON);
    }

    UNUSED(u1SetFlipMirrorConfig(u1SuperMirror, u1SuperFlip));
}
#endif

#ifdef CC_FLIP_MIRROR_FROM_DDI
#ifdef CC_FLIP_MIRROR_SUPPORT
static BOOL _fgIsMirrorOn = FALSE;
static BOOL _fgIsFlipOn = FALSE;

UINT8 u1SetFlipMirrorConfig(BOOL fgMirrorEn, BOOL fgFlipEn)
{
    _fgIsMirrorOn = fgMirrorEn;
    _fgIsFlipOn = fgFlipEn;

    return SV_SUCCESS;
}

UINT8 u1GetFlipMirrorConfig(void)
{  
    if (_fgIsMirrorOn)
    {
        u1FlipMirrorEnable |= SYS_MIRROR_CONFIG_ON ;
    }
    if(_fgIsFlipOn)
    {
        u1FlipMirrorEnable |= SYS_FLIP_CONFIG_ON ;
    }

    return u1FlipMirrorEnable;
}
#endif

#else
UINT8 u1SetFlipMirrorConfig(BOOL fgMirrorEn, BOOL fgFlipEn)
{
#if defined(CC_PANEL_FROM_FLASH) && defined(CC_S_PLATFORM)
	LOG(0,"Flip/Mirror setting decided by Flash panel\n");
    return SV_FAIL;

#elif defined(CC_FLIP_MIRROR_FROM_EEPROM)
    DTVCFG_T rDtvCfg;

    if(EEPDTV_GetCfg(&rDtvCfg))
    {
        LOG(0,"Read Flip-Mirror Config Fail\n");
        return SV_FAIL;
    }

    if (fgMirrorEn)
    {
        rDtvCfg.u1Flags2 |= (DTVCFG_FLAG2_MIRROR_ON);
    }
    else
    {
        rDtvCfg.u1Flags2 &= ~(DTVCFG_FLAG2_MIRROR_ON);
    }
    if(EEPDTV_SetCfg(&rDtvCfg))
    {
        LOG(0,"Set Mirror Config Fail\n");
        return SV_FAIL;
    }

    if (fgFlipEn)
    {
        rDtvCfg.u1Flags2 |= (DTVCFG_FLAG2_FLIP_ON);
    }
    else
    {
        rDtvCfg.u1Flags2 &= ~(DTVCFG_FLAG2_FLIP_ON);
    }
    if(EEPDTV_SetCfg(&rDtvCfg))
    {
        LOG(0,"Set Flip Config Fail\n");
        return SV_FAIL;
    }
    LOG(0,"fgMirrorEn: %d\tfgFlipEn: %d\trDtvCfg.u1Flags2: %d\n", fgMirrorEn, fgFlipEn, rDtvCfg.u1Flags2);
    return SV_SUCCESS;

#else
    LOG(0,"CC_FLIP_MIRROR_SUPPORT is not defined!\n");
    return SV_FAIL;
#endif
}
	

#ifdef CC_FLIP_MIRROR_SUPPORT
#if defined(CC_PANEL_FROM_FLASH) && defined(CC_S_PLATFORM)
UINT8 u1GetFlipMirrorConfig(void)
{
    if(u1Init == 0)
    {
                u1FlipConfig=SV_OFF;
                u1MirrorConfig=SV_OFF;
                u1FlipMirrorEnable= 0;
		switch(FlashPanel_GetPanelAttr() & 0x3)//bit0:flip, bit1:mirror
		{
		    case 0x3:
				u1FlipConfig=SV_ON;
			    u1MirrorConfig=SV_ON;
				break;
			case 0x2:
				u1FlipConfig=SV_OFF;
			    u1MirrorConfig=SV_ON;
				break;
			case 0x1:
				u1FlipConfig=SV_ON;
			    u1MirrorConfig=SV_OFF;
				break;
			default:
				u1FlipConfig=SV_OFF;
			    u1MirrorConfig=SV_OFF;
				break;
							
		}
		if ( u1FlipConfig == SV_ON )
		{
			u1FlipMirrorEnable |= SYS_FLIP_CONFIG_ON;
		}
		else
		{
			u1FlipMirrorEnable &= ~(SYS_FLIP_CONFIG_ON);
		}
		
		if(u1MirrorConfig == SV_ON )
		{
			u1FlipMirrorEnable |= SYS_MIRROR_CONFIG_ON;
		}
		else
		{
			u1FlipMirrorEnable &= ~(SYS_MIRROR_CONFIG_ON);
		}
        u1Init = 1 ;
    }

#ifdef CC_ENABLE_MTK_MODEL_INDEX
    if(u1UseSuperFlipMirror)
    {
        LOG(9, "======u1SuperFlipMirrorCfg %d\n\n", u1SuperFlipMirrorCfg);
        return u1SuperFlipMirrorCfg;
    }
    else
#endif
    {
        LOG(9, "======u1FlipMirrorEnable %d\n\n", u1FlipMirrorEnable);
        return u1FlipMirrorEnable;
    }
}
#else

#include "eeprom_if.h"
UINT8 u1GetFlipMirrorConfig(void)
{
#ifdef CC_FLIP_MIRROR_FROM_EEPROM
    DTVCFG_T rDtvCfg;
#endif

    if(u1Init == 0)
    {
#ifdef CC_FLIP_MIRROR_FROM_EEPROM		
	    if ( uAcOn1Init == 0 )
		{
			if(EEPDTV_GetCfg(&rDtvCfg))
			{
				LOG(0," Get DTV config fail\n");
			}
			else
			{
				if(rDtvCfg.u1Flags2 & DTVCFG_FLAG2_FLIP_ON)
				{
                    u1FlipConfig=SV_ON;
				}
				else
				{
					u1FlipConfig=SV_OFF;
				}
				if(rDtvCfg.u1Flags2 & DTVCFG_FLAG2_MIRROR_ON)
				{
					u1MirrorConfig=SV_ON;
				}
				else
				{
					u1MirrorConfig=SV_OFF;
				}
			}
            uAcOn1Init=1;
	    }
		
		if (u1FlipConfig == SV_ON)
		{
			u1FlipMirrorEnable |= SYS_FLIP_CONFIG_ON;
		}
		else
		{
			u1FlipMirrorEnable &= ~(SYS_FLIP_CONFIG_ON);
		}
		
		if(u1MirrorConfig == SV_ON )
		{
			u1FlipMirrorEnable |= SYS_MIRROR_CONFIG_ON;
		}
		else
		{
			u1FlipMirrorEnable &= ~(SYS_MIRROR_CONFIG_ON);
		}	   
#else
        #ifdef CC_FLIP_ENABLE
        u1FlipMirrorEnable |= SYS_FLIP_CONFIG_ON ;
        #endif
        #ifdef CC_MIRROR_ENABLE
        u1FlipMirrorEnable |= SYS_MIRROR_CONFIG_ON ;
        #endif
#endif
        u1Init = 1 ;
    }

#ifdef CC_ENABLE_MTK_MODEL_INDEX
    if(u1UseSuperFlipMirror)
    {
        LOG(9, "======u1SuperFlipMirrorCfg %d\n\n", u1SuperFlipMirrorCfg);
        return u1SuperFlipMirrorCfg;
    }
    else
#endif
    {
        LOG(9, "======u1FlipMirrorEnable %d\n\n", u1FlipMirrorEnable);
        return u1FlipMirrorEnable;
    }
}
#endif
#endif
#endif

#ifdef CC_FLIP_MIRROR_SUPPORT
void vReinitFlipMirrorConfig(void)
{
    u1Init = 0;
	#ifdef CC_FLIP_MIRROR_FROM_EEPROM
	uAcOn1Init = 0;
	#endif
	LOG(2,"===========vReinitFlipMirrorConfig %d %\n",u1Init,uAcOn1Init);
}

UINT32 u4GetFlipMirrorModule(UINT32 u4VdpId)
{
#ifndef CC_MTK_LOADER
    UINT32 u4Module =FLIP_NONE;
    if(u1GetFlipMirrorConfig())
    {
        u4Module= u4QueryFlipModule(u4VdpId);
    }

   LOG(9, "======u4GetFlipMirrorModule(%d) %d\n\n", u4VdpId, u4Module);

    return u4Module;
#else
    return 0;
#endif
}
#endif  //CC_FLIP_MIRROR_SUPPORT

void vDrvSetAllMute(UINT8 bOnOff)
{
    vRegWriteFldAlign(MUTE_00, bOnOff,  R_MUTE_POST_EN);
    if(u1IsPanelSupport4K2K120())
       vRegWriteFldAlign(MUTE_00+OSTG_2CH_OFFSET, bOnOff,  R_MUTE_POST_EN);
}

UINT32 u4DrvCalPanelRatio(void)
{
    if (PANEL_IsSupport240Hz())
    {
        return 2;
    }
    else if (PANEL_IsSupport120Hz())
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

void vDrvSetWobblingFinalCrop(UINT8 bOnOff, UINT8 bCropValue, UINT8 bCropRst, UINT8 bCropBypass)
{
    vIO32WriteFldAlign(CROPPING_DLY,bOnOff,CROPPING_LATENCY_SET_DISABLE);
    vIO32WriteFldAlign(CROPPING_DLY,bCropValue,CROPPING_LATENCY);
    vIO32WriteFldAlign(CROPPING_DLY,bCropRst,CROPPING_CNT_RST);
    vIO32WriteFldAlign(CROPPING_DLY,bCropBypass,CROPPING_BYPASS);
}

#ifndef CC_MTK_LOADER
void vDrvSetSyncLockMode(enum eSyncLockMode eMode)
{
	UINT16 u2VTotal    = wDrvVideoGetVTotal(SV_VP_MAIN);
	
	UINT8  u1FrameRate = bDrvVideoGetRefreshRate(SV_VP_MAIN);
	
    _eSyncLockMode = eMode;

    if(_eSyncLockMode == eSYNCLOCK_OFF)
    {
        vDrvSwitchMTKGoodDclk(SV_OFF);
    }
    else
    if(_eSyncLockMode == eSYNCLOCK_ON)
    {
    	if(u2VTotal != 0 && u1FrameRate != 0)
		{
			vDrvSetMTKGoodDclk(u2VTotal,u1FrameRate);
		}
        vDrvSwitchMTKGoodDclk(SV_ON);
        vFrameTrackDDDSConfig(0, 0);
    }
    else
    {
        /* do nothing. */
    }
    return;
}

/**
 * @brief vDrvSetDDDSFrameTrackTarget

 * Set DDDS Frame track Parameters

 * @param  u1SlowFast: slow/fast parameter
 * @param  u4TargetVTotal: target output Vtotal
 * @retval void
 */
void vDrvSetDDDSFrameTrackTarget(UINT8 u1SlowFast, UINT32 u4TargetVTotal)
{
    UINT32 DISPLAY_SIZE, HLEN_INT, HLEN_NUM, InputVTotal;
    UINT32 u4HTotal, u4VTotal;
#ifdef FT_USE_NEW_WAY
	UINT32 u4DeltaLine, u4DeltaDISPLAY_SIZE;
#endif
		
	LOG(3, "[FT]slowfast(%d), Target_vtotal(%d)\n",u1SlowFast,u4TargetVTotal);

    InputVTotal = IO32ReadFldAlign(DDDS_02, DDDS_02_HLEN_DEN);
    HLEN_NUM = IO32ReadFldAlign(DDDS_01, DDDS_01_HLEN_NUM);
    HLEN_INT = IO32ReadFldAlign(DDDS_01, DDDS_01_HLEN_INT);
    DISPLAY_SIZE = (HLEN_INT * InputVTotal) + HLEN_NUM;

    u4HTotal = (UINT32)wDrvGetOutputHTotal();
    u4VTotal = (UINT32)wDrvGetOutputVTotal();

	LOG(3, "[FT]InputVTotal(%d) HLEN_INT(%d) HLEN_NUM(%d) Htotal(%d) Vtotal(%d)\n", InputVTotal, HLEN_INT, HLEN_NUM, u4HTotal, u4VTotal);

	 if (_fgFrTrkHalfStep)
    {
        _uDDDSDivision = 1;
    }
    else
    {
        _uDDDSDivision = 0;
    }
	
#ifdef FT_USE_NEW_WAY
	if(u4TargetVTotal >= u4VTotal)
	{
		u4DeltaLine = u4TargetVTotal - u4VTotal;
	}
	else
	{
		u4DeltaLine = u4VTotal - u4TargetVTotal;
	}
	u4DeltaDISPLAY_SIZE = (DISPLAY_SIZE * u4DeltaLine / u4VTotal)>>_uDDDSDivision;
	if(u4TargetVTotal >= u4VTotal)
	{
		DISPLAY_SIZE = DISPLAY_SIZE + u4DeltaDISPLAY_SIZE;
	}
	else
	{
		DISPLAY_SIZE = DISPLAY_SIZE - u4DeltaDISPLAY_SIZE;
	}
	//DISPLAY_SIZE = DISPLAY_SIZE * u4TargetVTotal / u4VTotal;
#else
    if(u4TargetVTotal > u4VTotal)
        DISPLAY_SIZE = DISPLAY_SIZE +(((u4TargetVTotal - u4VTotal)*u4HTotal)>>_uDDDSDivision);
    else
        DISPLAY_SIZE = DISPLAY_SIZE -(((u4VTotal - u4TargetVTotal)*u4HTotal)>>_uDDDSDivision);
#endif

    if (InputVTotal ==0) InputVTotal = 1;

    HLEN_INT = (DISPLAY_SIZE / InputVTotal);
    HLEN_NUM = DISPLAY_SIZE - (HLEN_INT * InputVTotal);
	
	LOG(3, "[FT]******DISPLAY_SIZE(%d) HLEN_INT(%d) HLEN_NUM(%d)******\n", DISPLAY_SIZE, HLEN_INT, HLEN_NUM);

    switch(u1SlowFast)
    {
    case SV_DDDS_FRAMETRACK_FAST1:
        vIO32WriteFldAlign(DDDS_0C,(UINT16)InputVTotal,DDDS_0C_HLEN_DEN_E1);
        vIO32WriteFldAlign(DDDS_0D,(UINT16)HLEN_NUM,DDDS_0D_HLEN_NUM_E1);
        vIO32WriteFldAlign(DDDS_0C,HLEN_INT,DDDS_0C_HLEN_INT_E1);
        break;

    case SV_DDDS_FRAMETRACK_FAST2:
        vIO32WriteFldAlign(DDDS_0E,(UINT16)InputVTotal,DDDS_0E_HLEN_DEN_E2);
        vIO32WriteFldAlign(DDDS_0E,(UINT16)HLEN_NUM,DDDS_0E_HLEN_NUM_E2);
        vIO32WriteFldAlign(DDDS_0D,HLEN_INT,DDDS_0D_HLEN_INT_E2);
        break;

    case SV_DDDS_FRAMETRACK_SLOW1:
        vIO32WriteFldAlign(DDDS_0F,(UINT16)InputVTotal,DDDS_0F_HLEN_DEN_L1);
        vIO32WriteFldAlign(DDDS_10,(UINT16)HLEN_NUM,DDDS_10_HLEN_NUM_L1);
        vIO32WriteFldAlign(DDDS_0F,HLEN_INT,DDDS_0F_HLEN_INT_L1);
        break;

    case SV_DDDS_FRAMETRACK_SLOW2:
        vIO32WriteFldAlign(DDDS_11,(UINT16)InputVTotal,DDDS_11_HLEN_DEN_L2);
        vIO32WriteFldAlign(DDDS_11,(UINT16)HLEN_NUM,DDDS_11_HLEN_NUM_L2);
        vIO32WriteFldAlign(DDDS_10,HLEN_INT,DDDS_10_HLEN_INT_L2);
        break;

    default:
        break;
    }

}

/**
 * @brief vDrvSetDDDSFrameTrackMode

 * Set DDDS Frame track mode

 * @param  u1Mode: 0 => off, 1 => form scaler, 2 => form MJC
 * @retval void
 */
void vDrvSetDDDSFrameTrackMode(UINT8 u1Mode)
{
    static UINT8 u1PrevMode = 0xFF;

    if (u1PrevMode == u1Mode)
    {
        return;
    }

    u1PrevMode = u1Mode;

    switch(u1Mode)
    {
    case 0: //off
        vIO32WriteFldAlign(DDDS_0C,SV_OFF,DDDS_0C_VSYNC_TRACK_EN);
        break;

    case 1: //from scaler
        vIO32WriteFldAlign(DDDS_0C,SV_ON,DDDS_0C_VSYNC_TRACK_EN);
        vIO32WriteFldAlign(DDDS_03,1,DDDS_03_SC_MJC_TRACK_SEL);
        break;

    case 2: //from MJC
        vIO32WriteFldAlign(DDDS_0C,SV_ON,DDDS_0C_VSYNC_TRACK_EN);
        vIO32WriteFldAlign(DDDS_03,0,DDDS_03_SC_MJC_TRACK_SEL);
        break;

    default:
        break;
    }
}

void vDrvSetWobblingMode(WOBBLE_MODE _eWobbleMode)
{
	switch(_eWobbleMode)
    {
    case WOBBLE_OFF: //off
        vIO32WriteFldAlign(WOBBLE_0,SV_OFF,WOBBLE_BUFFER_H_EN);
		vIO32WriteFldAlign(WOBBLE_0,SV_OFF,WOBBLE_DE_H_EN);
		vIO32WriteFldAlign(WOBBLE_0,SV_OFF,WOBBLE_DE_V_EN);
        break;

    case WOBBLE_BUFFER_MODE: //buffer mode
        vIO32WriteFldAlign(WOBBLE_0,SV_ON,WOBBLE_BUFFER_H_EN);
		vIO32WriteFldAlign(WOBBLE_0,SV_OFF,WOBBLE_DE_H_EN);
		vIO32WriteFldAlign(WOBBLE_0,SV_OFF,WOBBLE_DE_V_EN);
        break;

    case WOBBLE_DE_MODE: //de mode
        vIO32WriteFldAlign(WOBBLE_0,SV_OFF,WOBBLE_BUFFER_H_EN);
		vIO32WriteFldAlign(WOBBLE_0,SV_ON,WOBBLE_DE_H_EN);
		vIO32WriteFldAlign(WOBBLE_0,SV_ON,WOBBLE_DE_V_EN);
        break;

    default:
        break;
    }
	vIO32WriteFldAlign(WOBBLE_0,SV_ON,WOBBLE_FORCE_COMMIT);	
}

void vDrvSetWobblingShift_HV(INT8 u1Shift_H, INT8 u1Shift_V)
{
	if(u1Shift_H<WOBBLE_MIN || u1Shift_H>WOBBLE_MAX || u1Shift_V<WOBBLE_MIN || u1Shift_V>WOBBLE_MAX)
	{
		LOG(1, "Wobbling shift out of range, u1Shift_H=%d, u1Shift_V=%d\n",  u1Shift_H, u1Shift_V);
        return;
	}
	
	if(u1Shift_H<0)
		u1Shift_H+=64;
	if(u1Shift_V<0)// only for de mode
		u1Shift_V+=64;
	vIO32WriteFldAlign(WOBBLE_0,u1Shift_H,WOBBLE_SHIFT_H);
	vIO32WriteFldAlign(WOBBLE_0,u1Shift_V,WOBBLE_SHIFT_V);

	vIO32WriteFldAlign(WOBBLE_0,SV_ON,WOBBLE_FORCE_COMMIT);	
}

void vDrvSetWobblingBlackInsert(UINT8 bOnOff, UINT16 u2ColorR, UINT16 u2ColorG, UINT16 u2ColorB)
{
	vIO32WriteFldAlign(BLACK_INSERT_1, bOnOff, BLACK_MASK_EN);
	vIO32WriteFldAlign(BLACK_INSERT_0, u2ColorR, BLACK_MASK_VALUE_R);
	vIO32WriteFldAlign(BLACK_INSERT_0, u2ColorG, BLACK_MASK_VALUE_G);
	vIO32WriteFldAlign(BLACK_INSERT_1, u2ColorB, BLACK_MASK_VALUE_B);

	vIO32WriteFldAlign(BLACK_INSERT_1, SV_ON, BLACK_FORCE_COMMIT);	
}

void vDrvSetWobblingAlphaBlend(UINT8 bOnOff, UINT8 bAlphaValue, UINT16 u2ColorR, UINT16 u2ColorG, UINT16 u2ColorB)
{
	vIO32WriteFldAlign(BLACK_INSERT_3, bOnOff, BLACK_ALPHA_EN);
	vIO32WriteFldAlign(BLACK_INSERT_3, bAlphaValue, BLACK_ALPHA);
	vIO32WriteFldAlign(BLACK_INSERT_2, u2ColorR, BLACK_BACKGROUND_R);
	vIO32WriteFldAlign(BLACK_INSERT_2, u2ColorG, BLACK_BACKGROUND_G);
	vIO32WriteFldAlign(BLACK_INSERT_3, u2ColorB, BLACK_BACKGROUND_B);

	vIO32WriteFldAlign(BLACK_INSERT_1, SV_ON, BLACK_FORCE_COMMIT);	
}

void vDrvSetWobblingTiming(void)
{
	UINT32 u4Width, u4Height;
	UINT32 u4HFrontPorch, u4VFrontPorch;
	UINT32 u4HBackPorch, u4VBackPorch;
	UINT32 u4HTotal, u4VTotal;

	//read input monitor value
	u4Width=IO32ReadFldAlign(WOBBLE_10, WOBBLE_H_WIDTH_IN_MON);
	u4Height=IO32ReadFldAlign(WOBBLE_12, WOBBLE_V_HEIGHT_IN_MON);
	u4HTotal=IO32ReadFldAlign(WOBBLE_9, WOBBLE_H_TOTAL_IN_MON);
	u4VTotal=IO32ReadFldAlign(WOBBLE_11, WOBBLE_V_TOTAL_IN_MON);
	u4HFrontPorch=IO32ReadFldAlign(WOBBLE_9, WOBBLE_H_FRONT_IN_MON);
	u4VFrontPorch=IO32ReadFldAlign(WOBBLE_12, WOBBLE_V_FRONT_IN_MON);
	u4HBackPorch=IO32ReadFldAlign(WOBBLE_10, WOBBLE_H_BACK_IN_MON);
	u4VBackPorch=IO32ReadFldAlign(WOBBLE_13, WOBBLE_V_BACK_IN_MON);

	//write to timing reg.
	vIO32WriteFldAlign(WOBBLE_4, u4Width, WOBBLE_HWIDTH);
	vIO32WriteFldAlign(WOBBLE_2, u4Height, WOBBLE_VHEIGH);
	vIO32WriteFldAlign(WOBBLE_3, u4HTotal, WOBBLE_HTOTAL);
	vIO32WriteFldAlign(WOBBLE_1, u4VTotal, WOBBLE_VTOTAL);
	vIO32WriteFldAlign(WOBBLE_3, u4HFrontPorch, WOBBLE_FRNT_HPORCH);
	vIO32WriteFldAlign(WOBBLE_1, u4VFrontPorch, WOBBLE_FRNT_VPORCH);
	vIO32WriteFldAlign(WOBBLE_4, u4HBackPorch, WOBBLE_BACK_HPORCH);
	vIO32WriteFldAlign(WOBBLE_2, u4VBackPorch, WOBBLE_BACK_VPORCH);

	vIO32WriteFldAlign(WOBBLE_0,SV_ON,WOBBLE_FORCE_COMMIT);
	
}

void vDrvSetWobblingAutoRun(UINT8 bOnOff, UINT8 u1delaySec, UINT8 bMode, UINT8 u1ShiftNum)
{
	INT8 u1Shift_H=0; INT8 u1Shift_V=0;
	UINT32 u4HFrontPorch, u4VFrontPorch;
	UINT32 u4HBackPorch, u4VBackPorch;
	INT32 H_shiftMax, H_shiftMin, V_shiftMax, V_shiftMin;
	INT8 i,j;
	u4HFrontPorch=IO32ReadFldAlign(WOBBLE_9, WOBBLE_H_FRONT_IN_MON);
	u4VFrontPorch=IO32ReadFldAlign(WOBBLE_12, WOBBLE_V_FRONT_IN_MON);
	u4HBackPorch=IO32ReadFldAlign(WOBBLE_10, WOBBLE_H_BACK_IN_MON);
	u4VBackPorch=IO32ReadFldAlign(WOBBLE_13, WOBBLE_V_BACK_IN_MON);

	H_shiftMax=MIN(u4HFrontPorch, u4HBackPorch);
	V_shiftMax=MIN(u4VFrontPorch, u4VBackPorch);
	if(bMode==1)
	{
		H_shiftMax=MIN(u1ShiftNum, H_shiftMax);
		V_shiftMax=MIN(u1ShiftNum, V_shiftMax);
	}
	H_shiftMin=(~H_shiftMax)+1;
	V_shiftMin=(~V_shiftMax)+1;
	LOG(0, "Wobbling auto test start!!\n");
    //1. h + shift 
	for(u1Shift_H=0;u1Shift_H<H_shiftMax;u1Shift_H++)
	{
		vDrvSetWobblingShift_HV(u1Shift_H, u1Shift_V);
		x_thread_delay(u1delaySec*1000);
	}
	//2. v + shift
	for(u1Shift_V=0;u1Shift_V<V_shiftMax;u1Shift_V++)
	{
		vDrvSetWobblingShift_HV(u1Shift_H, u1Shift_V);
		x_thread_delay(u1delaySec*1000);
	}
	//3. h - shift
	for(i=u1Shift_H;i>H_shiftMin;i--)
	{
		vDrvSetWobblingShift_HV(i, u1Shift_V);
		x_thread_delay(u1delaySec*1000);
	}
	//4. v - shift
	for(j=u1Shift_V;j>V_shiftMin;j--)
	{
		vDrvSetWobblingShift_HV(i, j);
		x_thread_delay(u1delaySec*1000);
	}
	//5. v + shift
	for(u1Shift_V=j;u1Shift_V<0;u1Shift_V++)
	{
		vDrvSetWobblingShift_HV(i, u1Shift_V);
		x_thread_delay(u1delaySec*1000);
	}
	//6. h + shift 
	for(u1Shift_H=i;u1Shift_H<=0;u1Shift_H++)
	{
		vDrvSetWobblingShift_HV(u1Shift_H, u1Shift_V);
		x_thread_delay(u1delaySec*1000);
	}
	LOG(0, "Wobbling auto test done!!\n");
}

void vDrvPrintWobblingInfo(void)
{
	UINT8 u1CropCnt;
	UINT32 u4Width, u4Height;
	UINT32 u4HFrontPorch, u4VFrontPorch;
	UINT32 u4HBackPorch, u4VBackPorch;
	UINT32 u4HTotal, u4VTotal;
	u4Width=IO32ReadFldAlign(WOBBLE_10, WOBBLE_H_WIDTH_IN_MON);
	u4Height=IO32ReadFldAlign(WOBBLE_12, WOBBLE_V_HEIGHT_IN_MON);
	u4HTotal=IO32ReadFldAlign(WOBBLE_9, WOBBLE_H_TOTAL_IN_MON);
	u4VTotal=IO32ReadFldAlign(WOBBLE_11, WOBBLE_V_TOTAL_IN_MON);
	u4HFrontPorch=IO32ReadFldAlign(WOBBLE_9, WOBBLE_H_FRONT_IN_MON);
	u4VFrontPorch=IO32ReadFldAlign(WOBBLE_12, WOBBLE_V_FRONT_IN_MON);
	u4HBackPorch=IO32ReadFldAlign(WOBBLE_10, WOBBLE_H_BACK_IN_MON);
	u4VBackPorch=IO32ReadFldAlign(WOBBLE_13, WOBBLE_V_BACK_IN_MON);

	u1CropCnt=IO32ReadFldAlign(CROPPING_DLY, CROPPING_LATENCY_CNT);
	
	vDrvSetWobblingTiming();

	LOG(0,"===== [Wobbling Status] ===== \n");
    LOG(0,"Width=%d\n", u4Width);
    LOG(0,"Height =%d\n", u4Height);
    LOG(0,"HTotal =%d\n", u4HTotal);
    LOG(0,"VTotal =%d\n", u4VTotal);
    LOG(0,"HFrontPorch =%d\n", u4HFrontPorch);
    LOG(0,"VFrontPorch =%d\n", u4VFrontPorch);
    LOG(0,"HBackPorch =%d\n", u4HBackPorch);
    LOG(0,"VBackPorch =%d\n", u4VBackPorch);
    LOG(0,"Cropping latency =%d\n", u1CropCnt);

}


UINT32 fgDrvIsSupport48(void)
{
	if(SUPPORT_PANEL_48HZ == 0)
	{
		return FALSE;
	}
    
    if (IS_PANEL_2D_N_3D_L12R12)
    {
        return fgDrvSupport24Hz() ? TRUE : FALSE;
    }
    else
    {
    #if defined(SUPPORT_CUSTOMER_FRAME_RATE_SPEC)
		return TRUE;
    #elif defined(SUPPORT_48HZ_NOT_96HZ)  // for LG spec.
        return (u1DrvVideoGetIs44PullDownInfo() && PANEL_IsSupport120Hz() == FALSE) ? TRUE : FALSE;
	#else
		if (SUPPORT_PANEL_48HZ)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
    #endif
    }
}

UINT32 fgDrvIsSupport60To50(void)
{
    #ifdef SUPPORT_60TO50
    if (u1DrvTDTVGetCur3DMode() == E_TDTV_UI_3D_MODE_OFF)
    {
        return u1CustFRC & SUPPORT_60TO50_2D;
    }
    else
    {
        return u1CustFRC & SUPPORT_60TO50_3D;
    }
    #else
    return FALSE;
    #endif
}

UINT32 fgDrvIsSupport50To60(void)
{
    #ifdef SUPPORT_50TO60
    if (u1DrvTDTVGetCur3DMode() == E_TDTV_UI_3D_MODE_OFF)
    {
        return u1CustFRC & SUPPORT_50TO60_2D;
    }
    else
    {
        return u1CustFRC & SUPPORT_50TO60_3D;
    }
    #else
    return FALSE;
    #endif
}
UINT32 fgDrvIsSupport3D60Output(void)
{
    #ifdef FORCE_3D_60HZ_OUTPUT_ENABLE
    if (u1DrvTDTVGetCur3DMode() != E_TDTV_UI_3D_MODE_OFF)
    {
        return u1CustFRC & SUPPORT_3D_OUTPUT_60;
    }
    else
    {
        return FALSE;
    }
    #else
    return FALSE;
    #endif
}

void VDP_SetBrowserInput(UINT8 uEnable)
{
    LOG(1,"SetBrowserInput : %s\n", uEnable ? "True" : "False");
	if(u1BrowserModeEn == uEnable)
	{
	    return;
	}
	else
	{
	    u1BrowserModeEn = uEnable;
	}

#ifndef CC_MTK_LOADER
    VDP_MutexLock();
#endif
    if(bDrvVideoSignalStatus(SV_VP_MAIN) == SV_VDO_STABLE)
    {
        fgFixModeChg = SV_TRUE;
        vDrvCalPanelFrameRate(wDrvVideoGetVTotal(SV_VP_MAIN), bDrvVideoGetRefreshRate(SV_VP_MAIN));
    }
#ifndef CC_MTK_LOADER
    VDP_MutexUnlock();
#endif

	return;
		
}

void VDP_SetSpeIPTVS(UINT8 uEnable)
{
    LOG(1,"SetSpecialIPTVS : %s\n", uEnable ? "True" : "False");
    if(u1SpeIPTVSEn == uEnable)
	{
	    return;
	}
	else
	{
	    u1SpeIPTVSEn = uEnable;
	}
#ifndef CC_MTK_LOADER
    VDP_MutexLock();
#endif
    if(bDrvVideoSignalStatus(SV_VP_MAIN) == SV_VDO_STABLE)
    {
        fgFixModeChg = SV_TRUE;
        vDrvCalPanelFrameRate(wDrvVideoGetVTotal(SV_VP_MAIN), bDrvVideoGetRefreshRate(SV_VP_MAIN));
    }
#ifndef CC_MTK_LOADER
    VDP_MutexUnlock();
#endif

	return;
		
}


BOOL fgIsBrowseInput(void)
{
    LOG(0,"fgIsBrowseInput u1BrowserModeEn(%d)\n",u1BrowserModeEn);
    if(u1BrowserModeEn)
	{
	    return SV_TRUE;
	}
	else
	{
	    return SV_FALSE;
	}
}

BOOL fgIsSpeIPTVS(void)
{
    LOG(0,"fgIsSpeIPTVS u1SpeIPTVSEn(%d)\n",u1SpeIPTVSEn);
    if(u1SpeIPTVSEn)
	{
	    return SV_TRUE;
	}
	else
	{
	    return SV_FALSE;
	}
}
UINT32 fgIsSupportFixMode(void)
{
   #if defined(SUPPORT_CUSTOMER_FRAME_RATE_SPEC)
    UINT32 u4VdoFrameRate;
    u4VdoFrameRate = bDrvVideoGetRefreshRate(SV_VP_MAIN);
    u4VdoFrameRate >>= (u4DrvTDTV3DModeQuery() == E_TDTV_UI_3D_MODE_FS) ? 1 : 0;
    if(fgIsBrowseInput() || fgIsSpeIPTVS() || _bForceFixMode)
	{   
		return TRUE;
	}
	else
	{
        return FALSE;
	}
	#else
	return FALSE;
	#endif
}
#ifdef CC_S_PLATFORM
extern UINT32 FlashPanel_GetControlWord3(void);
#ifndef CC_MTK_LOADER
void VDP_MutexLock(void);
void VDP_MutexUnlock(void);
#endif

void vDrvSetMotionFlow(UINT8 u1MotionFlow)
{
#ifdef CC_PANEL_FROM_FLASH
    UINT32 u4ControlWord3 = FlashPanel_GetControlWord3();// bit[27:26]  0: no 3D , 1:active 3D  ,2:Passive 3D 
#else
    UINT32 u4ControlWord3 = 0;// bit[27:26]  0: no 3D , 1:active 3D  ,2:Passive 3D 
#endif
    UINT32 u4PanelType = 0;
    UINT32 u4WCGType = 0;
#if defined(CC_S_PLATFORM) && (defined (CC_WK_CUST) || defined (CC_HK_CUST) || defined(CC_CP_CUST))
    UINT32 u4MVINo3D = 0;
    EEPROM_Read(0x10206, (UPTR)&u4MVINo3D, 1);   
#endif

    u4PanelType = ((u4ControlWord3 & 0x0C000000) >> 26);
    u4WCGType = (u4ControlWord3 & 0x00000003);

    
    if(u4WCGType == 2)
    {
        u1MotionFlow = SV_ON;
    }
    //Motion flow only works for active 3D model.
    if(u4PanelType == 0x1
        #if defined(CC_S_PLATFORM) && (defined (CC_WK_CUST) || defined (CC_HK_CUST) || defined(CC_CP_CUST))
        &&((u4MVINo3D & 0x1) == 0)
        #endif
        )
    {
        #ifndef CC_MTK_LOADER
        VDP_MutexLock();
        #endif
        fgMotionFlow = u1MotionFlow;
        if((fgPreMotionFlow != fgMotionFlow) && ((fgPreMotionFlow==0)||(fgMotionFlow==0)))
        {
            fgPreMotionFlow = fgMotionFlow;
            if(bDrvVideoSignalStatus(SV_VP_MAIN) == SV_VDO_STABLE)
            {
                vDrvCalPanelFrameRate(wDrvVideoGetVTotal(SV_VP_MAIN), bDrvVideoGetRefreshRate(SV_VP_MAIN));
            }
        }
        #ifndef CC_MTK_LOADER
        VDP_MutexUnlock();
        #endif
        LOG(1,"vDrvSetMotionFlow-->%d\n",u1MotionFlow);
    }
}

void vDrvSet4KScalingOnOff(UINT8 u1Mode)
{
    //u1Mode 1: fix scaler 4K mode, SRS 4K->4K
    //u1Mode 0: no fix
    if(u1ScalingMode != u1Mode)
    {
#ifndef CC_MTK_LOADER
        VDP_MutexLock();
#endif
        u1ScalingMode = u1Mode;
        if(!IS_DISP_4K2K_TYPE3)
        {
            vDrvCalPanelFrameRate(wDrvVideoGetVTotal(SV_VP_MAIN), bDrvVideoGetRefreshRate(SV_VP_MAIN));
        }
#ifndef CC_MTK_LOADER
        VDP_MutexUnlock();
#endif
    }
}
#endif

UINT32 fgIsSupportFlickerLessMode(void)
{
    #if defined(SUPPORT_CUSTOMER_FRAME_RATE_SPEC) && defined(CC_S_PLATFORM)
	    UINT32 u4VdoFrameRate;
	    u4VdoFrameRate = bDrvVideoGetRefreshRate(SV_VP_MAIN);
	    u4VdoFrameRate >>= (u4DrvTDTV3DModeQuery() == E_TDTV_UI_3D_MODE_FS) ? 1 : 0;
		
    if(u4DrvTDTV3DModeQuery() != E_TDTV_UI_3D_MODE_OFF) 
    {   
        if((fgMotionFlow != 0) && (PANEL_IsSupport120Hz()))
        {
            return TRUE;
        }
        else
        {
            return FALSE;
        }
		}
		else
		{
	        return FALSE;
		}
	#else
	return FALSE;
	#endif
}

/*
* @SUPPORT_CUSTOMER_FRAME_RATE_SPEC
* @VDP_PHOTO_2D:         1920*1080 60hz
* @VDP_PHOTO_3D_2_2D: 1920*1080 60hz
* @VDP_PHOTO_2D_2_3D: 1920*1080 60hz
* @VDP_PHOTO_3D:         1920*540 120hz
*/
extern TDTV_3D_CROP_INTO_T eCropInfo;
UINT32 u4VDPGetPhotoType(void)
{
    UINT32  u4PhotoType = VDP_PHOTO_NA;
    //#ifdef SUPPORT_CUSTOMER_FRAME_RATE_SPEC
    if((SV_TRUE==eCropInfo.fgIsFixSrcAspectRatio)
     &&(bGetSignalType(SV_VP_MAIN)==SV_ST_MPEG))
    {
    if(E_TDTV_UI_3D_MODE_OFF!=u4DrvTDTV3DModeQuery())
    {
        if(TD_TTT_OUT(u4DrvTDTVTTTModeQuery(SV_VP_MAIN))==E_TD_OUT_3D_2_2D)
        {
            u4PhotoType = VDP_PHOTO_3D_2_2D;
        }
        else if(E_TDTV_UI_3D_MODE_TTD==u4DrvTDTV3DModeQuery())
        {
            u4PhotoType = VDP_PHOTO_2D_2_3D;
        }
        else
        {
            u4PhotoType = VDP_PHOTO_3D;
        }
    }
    else
    {
        u4PhotoType = VDP_PHOTO_2D;
    }
    }
    else
    {
        u4PhotoType = VDP_PHOTO_NA;
    }
    //#endif
    return u4PhotoType;
}


UINT32 u4DrvSetCustFRC(UINT32 u4Mask)
{
    if (u1CustFRC != u4Mask)
    {
        u1CustFRC = (UINT8)u4Mask;
        vDrvCalPanelFrameRate(wDrvVideoGetVTotal(SV_VP_MAIN), bDrvVideoGetRefreshRate(SV_VP_MAIN));
        vVrmOnVdoModeChangeDone(SV_VP_MAIN);
    }
    return u1CustFRC;
}

UINT32 u4DrvGetCustFRC(void)
{
    return u1CustFRC;
}
void  vDrvCheck3DOuputFrameRate(void)
{
	static UINT8 u1PreIs3DFmt=0 ;
	//UINT32 u4Type = PANEL_TYPE_2K1K_60;
	
	if(fgDrvIsSupport50To60() ||fgDrvIsSupport60To50())
	{
		//just for UI control 3d mode after signal stable case 
		if(bDrvVideoSignalStatus(SV_VP_MAIN) == SV_VDO_STABLE)
		{
			vDrvCalPanelFrameRate(wDrvVideoGetVTotal(SV_VP_MAIN), bDrvVideoGetRefreshRate(SV_VP_MAIN));
	        //vVrmOnVdoModeChangeDone(SV_VP_MAIN);
		}		
	}

	if (u1PreIs3DFmt !=u1DrvTDTVCurIs3DMode())
	{
		u1PreIs3DFmt = u1DrvTDTVCurIs3DMode();
		DRVCUST_SendEvent(E_CUST_TV_OUT_FMT_CHANGE, 0);

        fgVB1VideoMute = SV_TRUE;
        if(DRVCUST_Get_InterMemc_type()) vDrvSetPSCMute(SV_TRUE);
        DRVCUST_SendEvent(E_CUST_OUT_MUTE_CHANGE, 0);
        DRVCUST_SendEvent(E_CUST_OUT_FMT_CHANGE, E_CUST_BE_TX_PATH_VIDEO);
        if(bDrvVideoSignalStatus(SV_VP_MAIN) == SV_VDO_STABLE)
        {
            vDrvCalPanelFrameRate(wDrvVideoGetVTotal(SV_VP_MAIN), bDrvVideoGetRefreshRate(SV_VP_MAIN));
        }
        else
        {
            vDrvCalPanelFrameRate(2250, 60);
        }

	}	
}

#ifndef CC_MTK_LOADER 
#if CC_SUPPORT_32_DROP_TO_22
extern BOOL bDoConvert;
#endif
#endif
void vDrvCalPanelFrameRate(UINT16 u2VdoVTotal, UINT32 u4VdoFrameRate)
{
    UINT32 u4DCLKType;
    UINT32 u4PanelFrameRate;
    UINT32 u4PanelRatio;
    UINT32 u4InputFrameRateMark;
    #ifndef CC_MTK_LOADER
    UINT32 u4Type;
    #endif

    u4InputFrameRateMark = u4VdoFrameRate;

    #ifndef CC_MTK_LOADER
    LOG(0,"@MTK_TRK vDrvCalPanelFrameRate u2VdoVTotal = %d u4VdoFrameRate = %d\n",u2VdoVTotal,u4VdoFrameRate);

    u4Type = vDrvCal4K2KPanelType();
    vDrvSwitchVideoType(u4Type);
    #endif
    

    u4PanelRatio = u4DrvCalPanelRatio();
    u4VdoFrameRate >>= (u4DrvTDTV3DModeQuery() == E_TDTV_UI_3D_MODE_FS) ? 1 : 0;
    if (_bForceFrameRate != 0)
    {
        u4VdoFrameRate = _bForceFrameRate;
    }

    switch (u4VdoFrameRate)
    {
        case 25:
        case 50:
        case 100:
            u4DCLKType = (fgDrvIsSupport50To60() || fgIsSupportFixMode() || fgIsSupportFlickerLessMode()) ? SV_DCLK_60HZ : SV_DCLK_50HZ;
            break;
        case 30:
        case 60:
        case 120:
            u4DCLKType = fgDrvIsSupport60To50() ? SV_DCLK_50HZ : SV_DCLK_60HZ;
            break;
        case 24:
			u4DCLKType = ( fgDrvIsSupport48() && !(fgIsSupportFixMode() || fgIsSupportFlickerLessMode()) && !fgDrvIsSupport3D60Output() ) ? SV_DCLK_48HZ : SV_DCLK_60HZ;
            break;
        case 48:
            if (SUPPORT_PANEL_48HZ)
            {
	            if(fgIsSupportFixMode() || fgIsSupportFlickerLessMode())
				{
				    u4DCLKType = SV_DCLK_60HZ;
				}
				else
				{
				    u4DCLKType = SV_DCLK_48HZ;
	    	    }
            }
            else if (IS_DISP_48HZ_TO_50HZ)
            {
				u4DCLKType = SV_DCLK_50HZ;
            }
			else
			{
				u4DCLKType = SV_DCLK_60HZ;
			}
				
			if(fgDrvIsSupport3D60Output())
			{
				u4DCLKType = SV_DCLK_60HZ;
			}
            break;
        case 20:
            if (fgIsMJCToOSTG())
            {
                #ifndef SUPPORT_20TO60
                u4DCLKType = SV_DCLK_50HZ;
                break;
                #endif
            }
        case 12:
        case 15:
            u4DCLKType = SV_DCLK_60HZ;
            break;
#ifdef SUPPORT_CUSTOMER_FRAME_RATE_SPEC
		case 8:
		u4DCLKType = (fgDrvIsSupport48() && !(fgIsSupportFixMode() || fgIsSupportFlickerLessMode())) ? SV_DCLK_48HZ : SV_DCLK_60HZ;
		break;
#endif
        default:
        #ifdef SUPPORT_CUSTOMER_FRAME_RATE_SPEC
			u4DCLKType = SV_DCLK_60HZ;
		#else
            if ((u4VdoFrameRate < 27) ||
                (u4VdoFrameRate > 45 && u4VdoFrameRate < 55) ||
                (u4VdoFrameRate > 90 && u4VdoFrameRate < 110))
            {
                u4DCLKType = (fgDrvIsSupport50To60() || fgIsSupportFixMode() || fgIsSupportFlickerLessMode()) ? SV_DCLK_60HZ : SV_DCLK_50HZ;
            }
            else
            {
                u4DCLKType = fgDrvIsSupport60To50() ? SV_DCLK_50HZ : SV_DCLK_60HZ;
            }
		#endif
            break;
    }
#ifdef SUPPORT_CUSTOMER_FRAME_RATE_SPEC
    if(fgApiVideoIsVgaTiming(SV_VP_MAIN) && wDrvVideoInputWidth(SV_VP_MAIN) != 4096)
	{
	    u4DCLKType = SV_DCLK_60HZ;
	}
#endif

#ifndef CC_MTK_LOADER 
#if CC_SUPPORT_32_DROP_TO_22
    if(bDoConvert)
    {
        u4DCLKType = SV_DCLK_48HZ;
    }
#endif
#endif
    u4PanelFrameRate = (u4DCLKType == SV_DCLK_48HZ)? 48 :
                       (u4DCLKType == SV_DCLK_50HZ)? 50 : 60;
    u4PanelFrameRate <<= u4PanelRatio;
	
	if(GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_30)
	{
		u4PanelFrameRate = (u4DCLKType == SV_DCLK_48HZ)? 24 :
                           (u4DCLKType == SV_DCLK_50HZ)? 25 : 30;
	}

    if  (u4VdoFrameRate == 24 || u4VdoFrameRate == 25 || u4VdoFrameRate == 30)
    {
        if (DRVCUST_PanelGet(ePanel242530pOutput) == 1)
        {
            u4DCLKType = SV_DCLK_OTHERS;
            u4PanelFrameRate = u4VdoFrameRate;
        }
    }

    if(_bForceFrameRate != 0)
    {
        u4PanelFrameRate = _bForceFrameRate;

        // Customer requests freerun at picture wizard mode to prevent VSYNC flicker for PDP
        fgDDDSEn = (IS_PANEL_2D_N_3D_L12R12|IS_ENGINEER_DISPLAY_MODE) ? SV_OFF : SV_ON;
    }
    else if (fgDrvDisplayGetPentouchMode())
    {
        u4PanelFrameRate = 60;
        fgDDDSEn = SV_OFF;
    }
    else
    {
        fgDDDSEn = SV_ON;
    }

	if (_bForceFrameRate != 0)
    {
        u4VdoFrameRate = u4InputFrameRateMark;
    }

    vDrvCalPanelTiming((UINT8)u4DCLKType, u4PanelFrameRate);

	if (fgDDDSEn == SV_ON && _eSyncLockMode != eSYNCLOCK_OFF)
    {
    	vDrvSetMTKGoodDclk(u2VdoVTotal, (UINT8)u4VdoFrameRate);
        if (fgIsMainTvd3d())
        {
            _fgFRS_ENA = SV_ON;    //Delay for VCR mode: Only for TVD
        }
        else
        {
            vDrvSwitchMTKGoodDclk(SV_ON);
        }
    }
    else
    {
        _fgFRS_ENA = SV_OFF;
        vDrvSwitchMTKGoodDclk(SV_OFF);
    }

    vFrameTrackDynMode();
    vFrameTrackDDDSConfig(0, 0);
}


#ifndef CC_MTK_LOADER
#if defined(CC_FBM_SUPPORT_SRS)
void vDrvSRSInOutClkConfig(E_SRS_IN_OUT_TYPE u1Type)
{
    if(u1Type == E_SRS_2K_TO_4K)//2kin 4k out
    {
        vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG,0,FLD_VOCLK_SEL); 
        vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG,2,FLD_SRSCLK_SEL); 
        vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG,2,FLD_VICLK_SEL); 

        if(IO32ReadFldAlign(CKGEN_DISP_CKCFG,FLD_POCLK_SEL) == 0)
        {
            vIO32WriteFldAlign(CKGEN_REG_MIB_OCLK_CKCFG,2,FLD_SC_POCLK_SEL); 
        }
        else
        {
            vIO32WriteFldAlign(CKGEN_REG_MIB_OCLK_CKCFG,6,FLD_SC_POCLK_SEL); 
        }
    }
    else if(u1Type == E_SRS_2K_TO_2K)
    {
    	if (IS_DISP_4K2K_TYPE3 && (GetPANEL4K2K_TYPE() == PANEL_TYPE_2K1K_120))  // For 4K2K60 OSD
		{
			vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG,1,FLD_VOCLK_SEL);   // 600/2, SRS out clk
	        vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG,2,FLD_SRSCLK_SEL);  // 600/4, SRS core clk(works at 150M) 
	        vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG,1,FLD_VICLK_SEL);   // 600/2, SRS in clk
		}
		else
		{
	        vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG,0,FLD_VOCLK_SEL); 
	        vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG,1,FLD_SRSCLK_SEL); 
	        vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG,0,FLD_VICLK_SEL); 
		}

        if(IO32ReadFldAlign(CKGEN_DISP_CKCFG,FLD_POCLK_SEL) == 0)
        {
            vIO32WriteFldAlign(CKGEN_REG_MIB_OCLK_CKCFG,0,FLD_SC_POCLK_SEL); 
        }
        else
        {
        	if (IS_DISP_4K2K_TYPE3 && (GetPANEL4K2K_TYPE() == PANEL_TYPE_2K1K_120))  // For 4K2K60 OSD
    		{	
				vIO32WriteFldAlign(CKGEN_REG_MIB_OCLK_CKCFG,1,FLD_SC_POCLK_SEL); 
			}
			else
			{
	            vIO32WriteFldAlign(CKGEN_REG_MIB_OCLK_CKCFG,4,FLD_SC_POCLK_SEL); 
			}
        }
    }
    else if(u1Type == E_SRS_4K_TO_4K)
    {
        vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG,0,FLD_VOCLK_SEL); 
        vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG,2,FLD_SRSCLK_SEL); 
        vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG,0,FLD_VICLK_SEL); 
        if(IO32ReadFldAlign(CKGEN_DISP_CKCFG,FLD_POCLK_SEL) == 0)
        {
            vIO32WriteFldAlign(CKGEN_REG_MIB_OCLK_CKCFG,0,FLD_SC_POCLK_SEL); 
        }
        else
        {
            vIO32WriteFldAlign(CKGEN_REG_MIB_OCLK_CKCFG,4,FLD_SC_POCLK_SEL); 
        }
    }
    else
    {
        vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG,0,FLD_VOCLK_SEL); 
        vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG,2,FLD_SRSCLK_SEL); 
        vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG,0,FLD_VICLK_SEL); 
        if(IO32ReadFldAlign(CKGEN_DISP_CKCFG,FLD_POCLK_SEL) == 0)
        {
            vIO32WriteFldAlign(CKGEN_REG_MIB_OCLK_CKCFG,0,FLD_SC_POCLK_SEL); 
        }
        else
        {
            vIO32WriteFldAlign(CKGEN_REG_MIB_OCLK_CKCFG,4,FLD_SC_POCLK_SEL); 
        }
    }
}

void vDrvSwitch4KSRSConfig()
{
    E_TIMEGEN_PATH ePath;
    ePath = E_TIMEGEN_PATH_PQ;
    if(VDP_LogChkMsk(VDP_1,VDP_LOG_DBG_MSK_TRK)) LOG(0,"@MTK_TRK [%s][%d] GetPANEL4K2K_TYPE():%d \n",__FUNCTION__,__LINE__,GetPANEL4K2K_TYPE());

    if(GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60_SRS)
    {
        vIO32WriteFldAlign(SRS_CTRL,1,C_SRS_CLK_EN); 
        //vOSTG_IO32WriteFldAlign(OSTG_00,1,C_BYPASS_TTD);
        vDrvSRSInOutClkConfig(E_SRS_2K_TO_4K);
        if(!IS_DISP_4K2K_TYPE3)
        {
            vScpipSetGfxOsdDly_V(4);
            vScpipSetGfxOsdDly_H(15);
        }

        vIO32WriteFldAlign(SRS_CTRL,0,C_SRS_BYPASS); 
    }
    else if((GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60) ||
        (GetPANEL4K2K_TYPE() == PANEL_TYPE_4K1K_120))
    {
        //vOSTG_IO32WriteFldAlign(OSTG_00,1,C_BYPASS_TTD);
        vIO32WriteFldAlign(SRS_CTRL,1,C_SRS_CLK_EN);
        vDrvSRSInOutClkConfig(E_SRS_4K_TO_4K);
        if(bDrvVideoSignalStatus(VDP_1)==SV_VDO_STABLE)
        {
            vIO32WriteFldAlign(SRS_CTRL,0,C_SRS_BYPASS); 
        }
        if(!IS_DISP_4K2K_TYPE3)
        {
            vScpipSetGfxOsdDly_V(0);
            vScpipSetGfxOsdDly_H(1);
        }
    }
    else if(GetPANEL4K2K_TYPE() == PANEL_TYPE_2K1K_120)
    {
        vOSTG_IO32WriteFldAlign(OSTG_00,1,C_BYPASS_TTD);   // need enable TTD for 3D depth control.
        vOSTG_IO32WriteFldAlign(OSTG_00, 0x1, C_TTD_BEFORE_SRS);// customer hase enabled C_SHP_DSR_EN, need set TTD before SRS to correct TTD performance.

        vIO32WriteFldAlign(SRS_CTRL,1,C_SRS_CLK_EN);
        vDrvSRSInOutClkConfig(E_SRS_2K_TO_2K);
        if(bDrvVideoSignalStatus(VDP_1)==SV_VDO_STABLE)
        {
            vIO32WriteFldAlign(SRS_CTRL,0,C_SRS_BYPASS); 
        }
        if(IS_DISP_4K2K_TYPE3)
        {
            vScpipSetGfxOsdDly_V(0);
            vScpipSetGfxOsdDly_H(0);

            //for new 4kBE, Video and Osd working clk is same,so no need works with slave mode.
            //vScpipSetSlaveSyncMode(E_GFX_TG,SV_OFF);
            //vScpipSetSlaveSyncMode(E_FSC_TG,SV_OFF);
        }
        else
        {
            vScpipSetGfxOsdDly_V(0);
            vScpipSetGfxOsdDly_H(1);
        }
    }
    else if(GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60_BYP)
    {
        if(IS_DISP_4K2K_TYPE3)
        {
            vScpipSetGfxOsdDly_V(0);
            vScpipSetGfxOsdDly_H(0);
        }
        else
        {
            vScpipSetGfxOsdDly_V(0);
            vScpipSetGfxOsdDly_H(1);
        }
    }
    else
    {
        vIO32WriteFldAlign(SRS_CTRL,1,C_SRS_BYPASS);
		vIO32WriteFldAlign(CKGEN_REG_MIB_OCLK_CKCFG,0,FLD_SC_POCLK_SEL); 
    }

    if(IS_DISP_4K2K_TYPE3)
    {
        if(u1DrvIsVdpBypCase())
        {
            //vScpipSetSlaveSyncMode(E_PSC_TG,SV_ON);
            vScpipSetSlaveMode(E_PSC_TG,SV_ON);
            vDrvSetMjcPostPathSel(SV_ON);
        }
        else
        {
            //vScpipSetSlaveSyncMode(E_PSC_TG,SV_OFF);
            vScpipSetSlaveMode(E_PSC_TG,SV_OFF);
            vDrvSetMjcPostPathSel(SV_OFF);
        }
    }

    if((!IS_DISP_4K2K_TYPE3) && (GetPANEL4K2K_TYPE() != PANEL_TYPE_MAX) &&
        !u1DrvIsVdpBypCase())
    {
        UINT32 newGfxVFp = lcdtimingInfo.u4VFP;
        UINT16 u2VRRVTotal = lcdtimingInfo.u4VTotal;
        if(VDP_LogChkMsk(VDP_1,VDP_LOG_DBG_MSK_TRK)) LOG(0,"@MTK_TRK [%s][%d] \n",__FUNCTION__,__LINE__);

        #if 0
        x_memcpy(&ReftimingInfo,&lcdtimingInfo,sizeof(LCDTIMING_INFO_T));
        vDrvSetRefTiming_SETDATA();
        #else
        vDrvSetRefTiming();
        #endif

        if(u4ScpipGetVTModeCtrl(E_GFX_TG) == E_TRK_FRONT_PORCH)
        {
            newGfxVFp = lcdtimingInfo.u4VTotal - lcdtimingInfo.u4VActive - lcdtimingInfo.u4VBP - lcdtimingInfo.u4VFP;
        }

        vScpipSetPanelHtotal(E_GFX_TG, lcdtimingInfo.u4HTotal);
        vScpipSetPanelWidth (E_GFX_TG, lcdtimingInfo.u4HActive);
        vScpipSetHsyncFp(E_GFX_TG, lcdtimingInfo.u4HFP);
        vScpipSetHsyncBp(E_GFX_TG, lcdtimingInfo.u4HBP);

        if(GetPANEL4K2K_TYPE() == PANEL_TYPE_4K1K_120)
        {
            u2VRRVTotal = lcdtimingInfo.u4VTotal/2;
            //vScpipSetPanelVtotal(E_GFX_TG, lcdtimingInfo.u4VTotal/2);
            vScpipSetPanelHeight(E_GFX_TG, lcdtimingInfo.u4VActive/2);
            vScpipSetVsyncFp(E_GFX_TG, newGfxVFp/2);
            vScpipSetVsyncBp(E_GFX_TG, lcdtimingInfo.u4VBP/2);
        }
        else
        {
            //vScpipSetPanelVtotal(E_GFX_TG, lcdtimingInfo.u4VTotal);
            vScpipSetPanelHeight(E_GFX_TG, lcdtimingInfo.u4VActive);
            vScpipSetVsyncFp(E_GFX_TG, newGfxVFp);
            vScpipSetVsyncBp(E_GFX_TG, lcdtimingInfo.u4VBP);
        }

        if(bDrvIsHdmiVRRMode())
        {
            u2VRRVTotal = DRV_MAX_VRR_VT;
        }
        vScpipSetPanelVtotal(E_GFX_TG, u2VRRVTotal);

        if(OSD_EXP_GetOSDMixPreMJCEn())
        {
            vScpipSetSlaveMode(E_GFX_TG, SV_ON);

            vIO32WriteFldAlign(SCPIP_SCSYS_14,2,SCSYS_14_SC_OSD_SRC_SEL);
        }
        else if(fgFrameTrackIsRefTrk())
        {
            vScpipSetSlaveMode(E_FSC_TG, SV_ON);
            vScpipSetSlaveMode(E_PSC_TG, SV_ON);
        }
        else
        {
            vScpipSetSlaveMode(E_FSC_TG, SV_OFF);
            vScpipSetSlaveMode(E_PSC_TG, SV_OFF);
        }
    }
    vScpipUpdateDelay(); 

}
#endif
#endif
void vDddsSettingCheck(void)
{

#if CC_OSD_MUTE_WA
    //channel change will reset out region
    if (_bDddsDelayFlag && (!(_fgAutoSearch || SRM_IsEpgMode())) && SRM_IsMainVideoStrictStable())
    {
        vDrvCalPanelFrameRate(wDrvVideoGetVTotal(SV_VP_MAIN), bDrvVideoGetRefreshRate(SV_VP_MAIN));
        // reset non-standard signal status
        _u4NSDHTotal=0xff;
        _u4NSDVTotal=0xff;
        _u4NSDFrameRate=0xff;
        _bDddsDelayFlag = 0 ;
        vVdoSendEvevntToSRM(SV_VP_MAIN);
        #ifdef DRV_SUPPORT_EXTMJC
        vDrvExtmjcRedoTwoChipConnection( SV_VP_MAIN, SV_TRUE);
        #endif
    }
#endif

    if ((_fgFRS_ENA == SV_ON) && fgIsMainTvd3d()
        && (_eSyncLockMode == eSYNCLOCK_IGNORED))   // Switch MTK Good Clock
    {
        if (bhwVCRSetting||(_na_state == 0x03)) //||!_fgHLOCK) //When VCR mode or Signal unsatble will OFF
        {
            vDrvSwitchMTKGoodDclk(SV_OFF);
        }
        else
        {
            vDrvSwitchMTKGoodDclk(SV_ON);
        }
    }
}

void vDrvDDDSLockStatus(UINT32 u4Count)
{
 UINT32 u4LoopCnt=u4Count;
 UINT32 u4UnlockCnt=0;
 static UINT8 u1CheckGoing=0;

 if (u1CheckGoing)
 {
    return;
 }
 do
 {
  u1CheckGoing = 1;
  if (IO32ReadFldAlign(DDDS_00,DDDS_00_DISP_EN)== 1)
  {
    if (!bDrvCheckMTKGoodStatus())
    {
    u4UnlockCnt++;
    }
    u4LoopCnt--;
  }
  else
  {
    LOG(0,"Open loop\n");
    u4LoopCnt = 0;
  }
 }while(u4LoopCnt);
 if(u4UnlockCnt)
 {
    LOG(0,"[UNLOCK] vDrvDDDSLockStatus check Finished unlock/check = %d / %d\n",u4UnlockCnt,u4Count);
 }
 else
 {
    if (IO32ReadFldAlign(DDDS_00,DDDS_00_DISP_EN)== 1)
    {
        LOG(0,"[LOCK] vDrvDDDSLockStatus check Finished OK\n");
    }
 }
 u1CheckGoing = 0;
}


#ifdef CC_SCPOS_3DTV_SUPPORT

static void updatePanelAttribute(UINT16 u2HTotal,
                                 UINT16 u2VTotal,
                                 UINT16 u2Width,
                                 UINT16 u2Height,
                                 UINT16 u2FrameRate)
{
    PANEL_SetPanelWidth(u2Width);
    PANEL_SetPanelHeight(u2Height);

    if(u2FrameRate >= 96)
    {
        PANEL_SetVClkMax(122);
    }
    else
    {
        PANEL_SetVClkMax(61);
    }

    if(u2FrameRate == 25 || u2FrameRate == 50 || u2FrameRate == 100)
    {
        PANEL_SetHTotal50Hz(u2HTotal);
        PANEL_SetVTotal50Hz(u2VTotal);
    }
    else if(SUPPORT_PANEL_48HZ && (u2FrameRate == 48 || u2FrameRate == 96))
    {
        PANEL_SetHTotal48Hz(u2HTotal);
        PANEL_SetVTotal48Hz(u2VTotal);
    }
    else
    {
        PANEL_SetHTotal60Hz(u2HTotal);
        PANEL_SetVTotal60Hz(u2VTotal);
    }

        // update OSD scaler parameters
    {
    UINT32 srcW=0,srcH=0,dstW=0,dstH=0,Enable=0,u4bpp=0;
    OSD_SC_GetScalerInfo(OSD_SCALER_3,&Enable,&srcW,&srcH,&dstW,&dstH,&u4bpp);
    OSD_SC_Scale(OSD_SCALER_3,Enable,srcW,srcH,u2Width,u2Height);
    OSD_SC_GetScalerInfo(OSD_SCALER_2,&Enable,&srcW,&srcH,&dstW,&dstH,&u4bpp);
    OSD_SC_Scale(OSD_SCALER_2,Enable,srcW,srcH,u2Width,u2Height);
    }

    return;
}

static void updatePanelAttribute_PDP(UINT16 u2HTotal,
                                 UINT16 u2VTotal,
                                 UINT32 u2PixelClk,
                                 UINT16 u2Width,
                                 UINT16 u2Height,
                                 UINT16 u2VSyncWidth,
                                 UINT16 u2VSyncFP,
                                 UINT16 u2FrameRate)
{
    PANEL_SetPanelWidth(u2Width);
    PANEL_SetPanelHeight(u2Height);

    if(u2FrameRate >= 96)
    {
        PANEL_SetVClkMax(122);
    }
    else
    {
        PANEL_SetVClkMax(61);
    }

    if(u2FrameRate == 25 || u2FrameRate == 50 || u2FrameRate == 100)
    {
        PANEL_SetHTotal50Hz(u2HTotal);
        PANEL_SetVTotal50Hz(u2VTotal);
        PANEL_SetPixelClk50Hz(u2PixelClk);
        #ifdef SUPPORT_PANEL_CUSTOMER_SPEC
        PANEL_SetVSyncWidthByTiming(SV_DCLK_50HZ,u2VSyncWidth);
        PANEL_SetVSyncBpByTiming(SV_DCLK_50HZ, u2VSyncFP);
        #endif
    }
    else if(SUPPORT_PANEL_48HZ && (u2FrameRate == 48 || u2FrameRate == 96))
    {
        PANEL_SetHTotal48Hz(u2HTotal);
        PANEL_SetVTotal48Hz(u2VTotal);
        PANEL_SetPixelClk48Hz(u2PixelClk);
        #ifdef SUPPORT_PANEL_CUSTOMER_SPEC
        PANEL_SetVSyncWidthByTiming(SV_DCLK_48HZ,u2VSyncWidth);
        PANEL_SetVSyncBpByTiming(SV_DCLK_48HZ, u2VSyncFP);
        #endif
    }
    else
    {
        PANEL_SetHTotal60Hz(u2HTotal);
        PANEL_SetVTotal60Hz(u2VTotal);
        PANEL_SetPixelClk60Hz(u2PixelClk);
        #ifdef SUPPORT_PANEL_CUSTOMER_SPEC
        PANEL_SetVSyncWidthByTiming(SV_DCLK_60HZ,u2VSyncWidth);
        PANEL_SetVSyncBpByTiming(SV_DCLK_60HZ, u2VSyncFP);
        #endif
    }

    return;
}

static UINT16 au2OrigHTotal[3];
static UINT16 au2OrigVTotal[3];
static UINT16 u2OrigWidth, u2OrigHeight;
static UINT32 au4OrigPixelMinMax[2];
static UINT8 u1OrigVClkMax;

#if 0  // need to review later
typedef struct{
    UINT16 u2FrameRate;
    UINT16 u2Width;
    UINT16 u2Height;
    UINT16 u2HTotal;
    UINT16 u2VTotal;
    UINT32 u4PixelClk;
    UINT16 u4VsyncWidth;
    UINT16 u4VsyncBp;
}S_PANEL_CONFIG;

typedef struct{
    UINT32 u4PixleClockMax;
    UINT32 u4PixleClockMin;
    UINT16 u2VTotalMax;
    UINT16 u2VTotalMin;
    UINT16 u2VClkMax;   ;
}S_PANEL_CONFIG_COMMON;
#endif

S_PANEL_CONFIG rPANEL_2D[3];
S_PANEL_CONFIG rPANEL_3D[3];

S_PANEL_CONFIG_COMMON rPANEL_2D_COMMON;
S_PANEL_CONFIG_COMMON rPANEL_3D_COMMON;
static UINT32 u43DModeDclk = 0;
static UINT8 u13DModeRefreshRate = 0;
#define DBG_3D_PANEL_ATTRIBUTE 0
void vPanelAttribute_Status(void)
{
    #if DBG_3D_PANEL_ATTRIBUTE
    UINT8 u1Cnt=0;

	if(SUPPORT_PANEL_48HZ)
	{
		u1Cnt=SV_DCLK_48HZ;
	}
	else
	{
		u1Cnt=SV_DCLK_50HZ;
	}

	for( ;u1Cnt<SV_DCLK_OTHERS;u1Cnt++)
    {
    LOG(0,"2D_u2FrameRate[%d] = %d\n",u1Cnt, rPANEL_2D[u1Cnt].u2FrameRate);
    LOG(0,"2D_u2Width[%d] = %d\n",u1Cnt, rPANEL_2D[u1Cnt].u2Width);
    LOG(0,"2D_u2Height[%d] = %d\n", u1Cnt,rPANEL_2D[u1Cnt].u2Height );
    LOG(0,"2D_u2HTotal[%d] = %d\n", u1Cnt,rPANEL_2D[u1Cnt].u2HTotal);
    LOG(0,"2D_u2VTotal[%d] = %d\n", u1Cnt,rPANEL_2D[u1Cnt].u2VTotal);
    LOG(0,"2D_u4PixelClk[%d] = %d\n", u1Cnt,rPANEL_2D[u1Cnt].u4PixelClk);
    LOG(0,"2D_u4VsyncWidth[%d] = %d\n", u1Cnt, rPANEL_2D[u1Cnt].u4VsyncWidth);
    LOG(0,"2D_u4VsyncBp[%d] = %d\n", u1Cnt, rPANEL_2D[u1Cnt].u4VsyncBp);

    LOG(0,"3D_u2FrameRate[%d] = %d\n",u1Cnt, rPANEL_3D[u1Cnt].u2FrameRate);
    LOG(0,"3D_u2Width[%d] = %d\n",u1Cnt, rPANEL_3D[u1Cnt].u2Width);
    LOG(0,"3D_u2Height[%d] = %d\n", u1Cnt,rPANEL_3D[u1Cnt].u2Height );
    LOG(0,"3D_u2HTotal[%d] = %d\n", u1Cnt,rPANEL_3D[u1Cnt].u2HTotal);
    LOG(0,"3D_u2VTotal[%d] = %d\n", u1Cnt,rPANEL_3D[u1Cnt].u2VTotal);
    LOG(0,"3D_u4PixelClk[%d] = %d\n", u1Cnt,rPANEL_3D[u1Cnt].u4PixelClk);
    LOG(0,"3D_u4VsyncWidth[%d] = %d\n", u1Cnt, rPANEL_3D[u1Cnt].u4VsyncWidth);
    LOG(0,"3D_u4VsyncBp[%d] = %d\n", u1Cnt, rPANEL_3D[u1Cnt].u4VsyncBp);
    }

    LOG(0,"rPANEL_2D_COMMON.u4PixleClockMax = %d \n ", rPANEL_2D_COMMON.u4PixleClockMax);
    LOG(0,"rPANEL_2D_COMMON.u4PixleClockMin = %d \n ", rPANEL_2D_COMMON.u4PixleClockMin);
    LOG(0,"rPANEL_2D_COMMON.u2VTotalMax = %d \n ", rPANEL_2D_COMMON.u2VTotalMax);
    LOG(0,"rPANEL_2D_COMMON.u2VTotalMin = %d \n ", rPANEL_2D_COMMON.u2VTotalMin);
    LOG(0,"rPANEL_2D_COMMON.u2VClkMax = %d \n ", rPANEL_2D_COMMON.u2VClkMax);

    LOG(0,"rPANEL_3D_COMMON.u4PixleClockMax = %d \n ", rPANEL_3D_COMMON.u4PixleClockMax);
    LOG(0,"rPANEL_3D_COMMON.u4PixleClockMin = %d \n ", rPANEL_3D_COMMON.u4PixleClockMin);
    LOG(0,"rPANEL_3D_COMMON.u2VTotalMax = %d \n ", rPANEL_3D_COMMON.u2VTotalMax);
    LOG(0,"rPANEL_3D_COMMON.u2VTotalMin = %d \n ", rPANEL_3D_COMMON.u2VTotalMin);
    LOG(0,"rPANEL_3D_COMMON.u2VClkMax = %d \n ", rPANEL_3D_COMMON.u2VClkMax);
    #endif

}

void vSavePanelAttribute_PDP(void)
{
        UINT8 u1Cnt=0;

        if(!IS_PANEL_2D_N_3D_L12R12)
        {
            return;
        }

        if(LVDS_OUTPUT_PORT == DUAL_PORT)
            fgPDPL12R12 = ePanel_PDP_L12R12; // L12R12
        else
            fgPDPL12R12 = ePanel_PDP_L1R1; // L1R1

        rPANEL_2D[SV_DCLK_60HZ].u2FrameRate = 60;
        rPANEL_2D[SV_DCLK_60HZ].u2Width = (UINT16)PANEL_GetPanelWidth();
        rPANEL_2D[SV_DCLK_60HZ].u2Height = (UINT16)PANEL_GetPanelHeight();
        rPANEL_2D[SV_DCLK_60HZ].u2HTotal = (UINT16)PANEL_GetHTotal60Hz();
        rPANEL_2D[SV_DCLK_60HZ].u2VTotal = (UINT16)PANEL_GetVTotal60Hz();
        rPANEL_2D[SV_DCLK_60HZ].u4PixelClk = PANEL_GetPixelClk60Hz();
        #ifdef SUPPORT_PANEL_CUSTOMER_SPEC
        rPANEL_2D[SV_DCLK_60HZ].u2VsyncWidth = (UINT16)PANEL_GetVSyncWidthByTiming(SV_DCLK_60HZ);
        rPANEL_2D[SV_DCLK_60HZ].u2VsyncBp = (UINT16)PANEL_GetVSyncBpByTiming(SV_DCLK_60HZ);
        #else
        rPANEL_2D[SV_DCLK_60HZ].u2VsyncWidth = PANEL_GetVSyncWidth();
        rPANEL_2D[SV_DCLK_60HZ].u2VsyncBp = u4GetPanelVSyncBp();
        #endif

        rPANEL_2D[SV_DCLK_50HZ].u2FrameRate = 50;
        rPANEL_2D[SV_DCLK_50HZ].u2Width = (UINT16)PANEL_GetPanelWidth();
        rPANEL_2D[SV_DCLK_50HZ].u2Height = (UINT16)PANEL_GetPanelHeight();
        rPANEL_2D[SV_DCLK_50HZ].u2HTotal = (UINT16)PANEL_GetHTotal50Hz();
        rPANEL_2D[SV_DCLK_50HZ].u2VTotal = (UINT16)PANEL_GetVTotal50Hz();
        rPANEL_2D[SV_DCLK_50HZ].u4PixelClk = PANEL_GetPixelClk50Hz();
        #ifdef SUPPORT_PANEL_CUSTOMER_SPEC
        rPANEL_2D[SV_DCLK_50HZ].u2VsyncWidth = (UINT16)PANEL_GetVSyncWidthByTiming(SV_DCLK_50HZ);
        rPANEL_2D[SV_DCLK_50HZ].u2VsyncBp = (UINT16)PANEL_GetVSyncBpByTiming(SV_DCLK_50HZ);
        #else
        rPANEL_2D[SV_DCLK_50HZ].u2VsyncWidth = (UINT16)PANEL_GetVSyncWidth();
        rPANEL_2D[SV_DCLK_50HZ].u2VsyncBp = (UINT16)u4GetPanelVSyncBp();
        #endif

        if (SUPPORT_PANEL_48HZ)
        {
	        rPANEL_2D[SV_DCLK_48HZ].u2FrameRate = 48;
	        rPANEL_2D[SV_DCLK_48HZ].u2Width = (UINT16)PANEL_GetPanelWidth();
	        rPANEL_2D[SV_DCLK_48HZ].u2Height = (UINT16)PANEL_GetPanelHeight();
	        rPANEL_2D[SV_DCLK_48HZ].u2HTotal = (UINT16)PANEL_GetHTotal48Hz();
	        rPANEL_2D[SV_DCLK_48HZ].u2VTotal = (UINT16)PANEL_GetVTotal48Hz();
	        rPANEL_2D[SV_DCLK_48HZ].u4PixelClk = PANEL_GetPixelClk48Hz();
	        #ifdef SUPPORT_PANEL_CUSTOMER_SPEC
	        rPANEL_2D[SV_DCLK_48HZ].u2VsyncWidth = (UINT16)PANEL_GetVSyncWidthByTiming(SV_DCLK_48HZ);
	        rPANEL_2D[SV_DCLK_48HZ].u2VsyncBp = (UINT16)PANEL_GetVSyncBpByTiming(SV_DCLK_48HZ);
	        #else
	        rPANEL_2D[SV_DCLK_48HZ].u2VsyncWidth = (UINT16)PANEL_GetVSyncWidth();
	        rPANEL_2D[SV_DCLK_48HZ].u2VsyncBp = (UINT16)u4GetPanelVSyncBp();
			#endif
		}

        rPANEL_2D_COMMON.u4PixleClockMax = PANEL_GetPixelClkMax();
        rPANEL_2D_COMMON.u4PixleClockMin = PANEL_GetPixelClkMin();
        rPANEL_2D_COMMON.u2VTotalMax = (UINT16)PANEL_GetVTotalMax();
        rPANEL_2D_COMMON.u2VTotalMin = (UINT16)PANEL_GetVTotalMin();
        rPANEL_2D_COMMON.u2VClkMax = (UINT16)PANEL_GetVClkMax();

		
		if(SUPPORT_PANEL_48HZ)
		{
			u1Cnt=SV_DCLK_48HZ;
		}
		else
		{
			u1Cnt=SV_DCLK_50HZ;
		}

        for( ;u1Cnt<SV_DCLK_OTHERS;u1Cnt++)
        {
            rPANEL_3D[u1Cnt].u2FrameRate = rPANEL_2D[u1Cnt].u2FrameRate;
            rPANEL_3D[u1Cnt].u2Width = rPANEL_2D[u1Cnt].u2Width;
            rPANEL_3D[u1Cnt].u2Height = rPANEL_2D[u1Cnt].u2Height << 1;
            rPANEL_3D[u1Cnt].u2HTotal = rPANEL_2D[u1Cnt].u2HTotal;
            rPANEL_3D[u1Cnt].u2VTotal = rPANEL_2D[u1Cnt].u2VTotal << 1;
            rPANEL_3D[u1Cnt].u4PixelClk = rPANEL_2D[u1Cnt].u4PixelClk << 1;
            rPANEL_3D[u1Cnt].u2VsyncWidth = rPANEL_2D[u1Cnt].u2VsyncWidth << 1;
            rPANEL_3D[u1Cnt].u2VsyncBp = rPANEL_2D[u1Cnt].u2VsyncBp << 1;
        }

        rPANEL_3D_COMMON.u4PixleClockMax = rPANEL_2D_COMMON.u4PixleClockMax << 1;
        rPANEL_3D_COMMON.u4PixleClockMin = rPANEL_2D_COMMON.u4PixleClockMin << 1;
        rPANEL_3D_COMMON.u2VTotalMax = rPANEL_2D_COMMON.u2VTotalMax << 1;
        rPANEL_3D_COMMON.u2VTotalMin = rPANEL_2D_COMMON.u2VTotalMin << 1;
        rPANEL_3D_COMMON.u2VClkMax = rPANEL_2D_COMMON.u2VClkMax << 1;

        vPanelAttribute_Status();

}

static void vSavePanelAttribute(void)
{
    u2OrigWidth = (UINT16)PANEL_GetPanelWidth();
    u2OrigHeight = (UINT16)PANEL_GetPanelHeight();

    au2OrigHTotal[0] = (UINT16)PANEL_GetHTotal60Hz();
    au2OrigVTotal[0] = (UINT16)PANEL_GetVTotal60Hz();

    au2OrigHTotal[1] = (UINT16)PANEL_GetHTotal50Hz();
    au2OrigVTotal[1] = (UINT16)PANEL_GetVTotal50Hz();

	if (SUPPORT_PANEL_48HZ)
	{
	    au2OrigHTotal[2] = (UINT16)PANEL_GetHTotal48Hz();
	    au2OrigVTotal[2] = (UINT16)PANEL_GetVTotal48Hz();
	}

    au4OrigPixelMinMax[0] = PANEL_GetPixelClkMin();
    au4OrigPixelMinMax[1] = PANEL_GetPixelClkMax();
    u1OrigVClkMax = (UINT8)PANEL_GetVClkMax();

}

void vDrvDisp3DModeChg(UINT8 u13DMode)
{
    static UINT8 u1fg3DMode_Backup=0;
    UINT8 u1fg3DMode=0;
    UINT8 u1fg3DMode_ModChg=0;
    UINT32 u4CtlWord;
    UINT8 u1Cnt=0;
    UINT32 dwDCLK = PANEL_GetPixelClk60Hz();


    #if DBG_3D_PANEL_ATTRIBUTE
    LOG(0,"[PDP_NEW] vDrvDisp3DModeChg u13DMode= %d\n", u13DMode);
    #endif

    if(!IS_PANEL_2D_N_3D_L12R12)
    {
        return;
    }

    if(u13DMode)
    {
        u1fg3DMode = 1;
    }
    else
    {
        u1fg3DMode = 0;
    }

    if (u1fg3DMode != u1fg3DMode_Backup)
    {
        u1fg3DMode_ModChg = 1;
        u1fg3DMode_Backup = u1fg3DMode;
    }
    else
    {
        u1fg3DMode_ModChg =0;
    }

    #if DBG_3D_PANEL_ATTRIBUTE
    vPanelAttribute_Status();
    #endif

    #if DBG_3D_PANEL_ATTRIBUTE
    LOG(0,"[PDP_NEW] vDrvDisp3DModeChg u1fg3DMode_ModChg= %d\n", u1fg3DMode_ModChg);
    #endif

    if(u1fg3DMode_ModChg)
    {

    vDrvSetAllMute(SV_ON);

    vDrvLVDSOff();

    if(u1fg3DMode)
    {
        if(fgPDPL12R12)
            PANEL_SetLvdsPixel(2); // DUAL -> FHD
        else
            PANEL_SetLvdsPixel(1); // SINGLE -> DUAL
    }
    else
    {
        if(fgPDPL12R12)
           PANEL_SetLvdsPixel(1); // DUAL
        else
           PANEL_SetLvdsPixel(0); // SINGLE
    }

    u4CtlWord = PANEL_GetControlWord() & 0xFBFFFFFF;
	
	if(SUPPORT_PANEL_48HZ)
	{
		u1Cnt=SV_DCLK_48HZ;
	}
	else
	{
		u1Cnt=SV_DCLK_50HZ;
	}

    if(u1fg3DMode)
    {
        u4CtlWord = u4CtlWord | PANEL_L12R12;
		
		for( ;u1Cnt<SV_DCLK_OTHERS;u1Cnt++)
        {
            updatePanelAttribute_PDP(rPANEL_3D[u1Cnt].u2HTotal,
                                rPANEL_3D[u1Cnt].u2VTotal,
                                rPANEL_3D[u1Cnt].u4PixelClk,
                                rPANEL_3D[u1Cnt].u2Width,
                                rPANEL_3D[u1Cnt].u2Height,
                                rPANEL_3D[u1Cnt].u2VsyncWidth,
                                rPANEL_3D[u1Cnt].u2VsyncBp,
                                rPANEL_3D[u1Cnt].u2FrameRate);
        }

        PANEL_SetPixelClkMax(rPANEL_3D_COMMON.u4PixleClockMax);
        PANEL_SetPixelClkMin(rPANEL_3D_COMMON.u4PixleClockMin);
        PANEL_SetVTotalMax(rPANEL_3D_COMMON.u2VTotalMax);
        PANEL_SetVTotalMin(rPANEL_3D_COMMON.u2VTotalMin);
        PANEL_SetVClkMax(rPANEL_3D_COMMON.u2VClkMax);
     }
     else
     {
		 for( ;u1Cnt<SV_DCLK_OTHERS;u1Cnt++)
         {
             updatePanelAttribute_PDP(rPANEL_2D[u1Cnt].u2HTotal,
                                 rPANEL_2D[u1Cnt].u2VTotal,
                                 rPANEL_2D[u1Cnt].u4PixelClk,
                                 rPANEL_2D[u1Cnt].u2Width,
                                 rPANEL_2D[u1Cnt].u2Height,
                                 rPANEL_2D[u1Cnt].u2VsyncWidth,
                                 rPANEL_2D[u1Cnt].u2VsyncBp,
                                 rPANEL_2D[u1Cnt].u2FrameRate);
         }

        PANEL_SetPixelClkMax(rPANEL_2D_COMMON.u4PixleClockMax);
        PANEL_SetPixelClkMin(rPANEL_2D_COMMON.u4PixleClockMin);
        PANEL_SetVTotalMax(rPANEL_2D_COMMON.u2VTotalMax);
        PANEL_SetVTotalMin(rPANEL_2D_COMMON.u2VTotalMin);
        PANEL_SetVClkMax(rPANEL_2D_COMMON.u2VClkMax);
        }

    PANEL_SetControlWord(u4CtlWord);
    vDrvLVDSConfig();
    vDrvLVDSInit();
    vErrorHandleSetByPanel();
    if ((bSI_DISPLAY_DCLK_TYPE == SV_DCLK_60HZ) || (bSI_DISPLAY_DCLK_TYPE == SV_DCLK_OTHERS))
    {
        dwDCLK = PANEL_GetPixelClk60Hz();
    }
    else if (SUPPORT_PANEL_48HZ && (bSI_DISPLAY_DCLK_TYPE == SV_DCLK_48HZ))
    {
        dwDCLK = PANEL_GetPixelClk48Hz();
    }
    else // (bSI_DISPLAY_DCLK_TYPE == SV_DCLK_50HZ)
    {
        dwDCLK = PANEL_GetPixelClk50Hz();
    }

    #if DBG_3D_PANEL_ATTRIBUTE
    LOG(0,"[PDP_NEW] dwDCLK %d\n", dwDCLK);
    #endif

    #ifdef CC_MTK_LOADER
    vDrvVOPLLFreqSet(dwDCLK);
    #else
    vDrvVOFreqSet(dwDCLK);
    #endif
    fgIsVsyncIsrStart = 0;
    vDrvSetPanelTiming();

    OSD_BASE_SwitchResolution();

    if(bDrvVideoSignalStatus(SV_VP_MAIN) == (UINT8)SV_VDO_STABLE)
    {
        vSetMainFlg(MAIN_FLG_MODE_CHG);
        vSetMainFlg(MAIN_FLG_MODE_DET_DONE);
    }
    vDrvLVDSOn();
    vDrvSetAllMute(SV_OFF);
    }

    return;
}

static void vDrvDisp3DModeCB(void)
{
    S_TDTV_OSTG_CONFIG rOstgCnf;
    //UINT16 u2FrameRate;

    rOstgCnf = eDrvTDTVOSTGModeQuery();
    if(rOstgCnf.eOSTGCfg == E_TDTV_OSTG_CFG_BY_PANEL_TBL)
    {
        //resotred the panel attributes
#ifdef PANEL_SUPPORT_48HZ
        updatePanelAttribute(au2OrigHTotal[2],
                             au2OrigVTotal[2],
                             u2OrigWidth,
                             u2OrigHeight,
                             48);
#endif
        updatePanelAttribute(au2OrigHTotal[1],
                             au2OrigVTotal[1],
                             u2OrigWidth,
                             u2OrigHeight,
                             50);
        updatePanelAttribute(au2OrigHTotal[0],
                             au2OrigVTotal[0],
                             u2OrigWidth,
                             u2OrigHeight,
                             60);

        u43DModeDclk = 0;
        PANEL_SetPixelClkMin(au4OrigPixelMinMax[0]);
        PANEL_SetPixelClkMax(au4OrigPixelMinMax[1]);
        PANEL_SetVClkMax(u1OrigVClkMax);
    }
    else
    {
        updatePanelAttribute((UINT16)rOstgCnf.u4HTotal,
                             (UINT16)rOstgCnf.u4VTotal,
                             (UINT16)rOstgCnf.u4Width,
                             (UINT16)rOstgCnf.u4Height,
                             (UINT16)rOstgCnf.u4FrameRate);

        u43DModeDclk = rOstgCnf.u4Clk;
        u13DModeRefreshRate = (UINT8)rOstgCnf.u4FrameRate;

        // clock range to cover 24/25/30Hz output clock
        PANEL_SetPixelClkMin(50000000);
    }
    vDrvOutputStageInit();
    vDrvLVDSConfig();
    vErrorHandleSetByPanel();
    vDrvSetPanelTiming();
    #ifdef DRV_SUPPORT_EXTMJC
    vDrvExtmjcRedoTwoChipConnection( SV_VP_MAIN, SV_TRUE);
    #endif
#ifdef DRV_SUPPORT_MT8283
    fgForceChangeRegion = TRUE;
#endif
    return;
}

UINT32 u4DrvGetDisp3DModeDclk(void)
{
    return u43DModeDclk;
}

UINT8 u1DrvGetDisp3DModeRefreshRate(void)
{
    return u13DModeRefreshRate;
}

#endif

#endif

#if defined(CC_FAST_INIT) || defined(CC_SUPPORT_STR)
extern void VOPLL_pm_resume(void);
void vDrvDisplayInit_pm_resume(void)
{
  bSI_DISPLAY_DCLK_TYPE = SV_DCLK_UNKNOW;
  fgDDDSEn = SV_OFF;
#if defined(SUPPORT_PANEL_GRAPH_MODE) 
	#ifdef LOAD_PANEL_FROM_FLASH_CUSTOM
	if (FlashPanel_GetControlWord2_GraphModeEn())
	#else
	if (IS_SUPPORT_VB1_GRAPH_MODE)
	#endif
	{
		LOG(0,"[GFX]Force set GFX_timing while resume!!\n");
		vDrvSetGfxTimingForceUpdated(TRUE);
	}
#endif
  LOG(0,"[LVDS]FLD_TCLK_SEL=%d\n",(UINT16)IO32ReadFldAlign(CKGEN_TCLK_TEST,FLD_TCLK_SEL));
#ifdef CC_FLIP_MIRROR_SUPPORT
  vReinitFlipMirrorConfig();
#endif
  vIO32WriteFldAlign(CKGEN_TCLK_TEST, 0, FLD_TCLK_SEL);	// xtal to tclk for tco isr.
  VOPLL_pm_resume();
  
#ifndef CC_MTK_LOADER
  vDrvUpdatePanelParam(GetPANEL4K2K_TYPE());
#endif
  
  #if SUPPORT_DISPLAY
  vDrvLVDSInit();
  vErrorHandleInit();
  vScpipHwInitOnPanelInit(SV_VP_MAIN);     //[DTV00702027]main/sub output windows need to be set to 0 before backlight on
  vScpipHwInitOnPanelInit(SV_VP_PIP);
  vDrvDisplayInit();
  u1DrvMruaCUCInit();
  LOG(0, "Display Init");
  #endif
}
#endif

#define FRC_UNMUTE_DELAY 500 // 2 sec

#ifndef CC_MTK_LOADER
static void updatePanelAttribute_4k2k(UINT16 u2HTotal,
                                 UINT16 u2VTotal,
                                 UINT32 u2PixelClk,
                                 UINT16 u2Width,
                                 UINT16 u2Height,
                                 UINT16 u2VSyncWidth,
                                 UINT16 u2VSyncBP,
                                 UINT16 u2HSyncWidth,
                                 UINT16 u2HsyncFP,
                                 UINT16 u2FrameRate)
{
    PANEL_SetPanelWidth(u2Width);
    PANEL_SetPanelHeight(u2Height);

    if(u2FrameRate == 25 || u2FrameRate == 50 || u2FrameRate == 100)
    {
        LOG(0,"[4k2k][LVDS]%dHz input --> 50Hz output\n", u2FrameRate);
        PANEL_SetHTotal50Hz(u2HTotal);
        PANEL_SetVTotal50Hz(u2VTotal);
        PANEL_SetPixelClk50Hz(u2PixelClk);
        #ifdef SUPPORT_PANEL_CUSTOMER_SPEC
        PANEL_SetVSyncWidthByTiming(SV_DCLK_50HZ,u2VSyncWidth);
        PANEL_SetVSyncBpByTiming(SV_DCLK_50HZ, u2VSyncBP);
		PANEL_SetHSyncWidthByTiming(SV_DCLK_50HZ,(UINT8)u2HSyncWidth);
		PANEL_SetHSyncFpByTiming(SV_DCLK_50HZ,u2HsyncFP);
        #endif
    }
    else if(SUPPORT_PANEL_48HZ && (u2FrameRate == 24 || u2FrameRate == 48 || u2FrameRate == 96))
    {
        LOG(0,"[4k2k][LVDS]%dHz input --> 48Hz output\n", u2FrameRate);
        PANEL_SetHTotal48Hz(u2HTotal);
        PANEL_SetVTotal48Hz(u2VTotal);
        PANEL_SetPixelClk48Hz(u2PixelClk);
        #ifdef SUPPORT_PANEL_CUSTOMER_SPEC
        PANEL_SetVSyncWidthByTiming(SV_DCLK_48HZ,u2VSyncWidth);
        PANEL_SetVSyncBpByTiming(SV_DCLK_48HZ, u2VSyncBP);
		PANEL_SetHSyncWidthByTiming(SV_DCLK_48HZ,(UINT8)u2HSyncWidth);
		PANEL_SetHSyncFpByTiming(SV_DCLK_48HZ,u2HsyncFP);
        #endif
    }
    else
    {
        LOG(0,"[4k2k][LVDS]%dHz input --> 60Hz output\n", u2FrameRate);
        PANEL_SetHTotal60Hz(u2HTotal);
        PANEL_SetVTotal60Hz(u2VTotal);
        PANEL_SetPixelClk60Hz(u2PixelClk);
        #ifdef SUPPORT_PANEL_CUSTOMER_SPEC
        PANEL_SetVSyncWidthByTiming(SV_DCLK_60HZ,u2VSyncWidth);
        PANEL_SetVSyncBpByTiming(SV_DCLK_60HZ, u2VSyncBP);
		PANEL_SetHSyncWidthByTiming(SV_DCLK_60HZ,(UINT8)u2HSyncWidth);
		PANEL_SetHSyncFpByTiming(SV_DCLK_60HZ,u2HsyncFP);
        #endif
    }
}

extern SRM_TV_MODE_T _eApplicationMode;
void vDrvVideoPlaneReConfig(UINT32 u4type)
{
#ifdef CC_S_PLATFORM
#ifndef CC_MTK_LOADER
	if(IS_DISP_4K2K_TYPE3)
	{
	    static UINT32 u4Pretype = PANEL_TYPE_MAX;
            if(((IS_IC_5893_ES1() && (vGetVDPColorMode(SV_VP_MAIN) == SV_COLOR_MODE_444))||
            (_eApplicationMode == SRM_TV_MODE_TYPE_PIP) || (_eApplicationMode == SRM_TV_MODE_TYPE_POP) ||
            (wDrvVideoInputHeight(VDP_1) > 1088)) &&
            (u4type != PANEL_TYPE_4K2K_60_SRS || fgIsMJCToOSTG()))
		{
	    	vSetPlaneWidth(VDP_PQPATH, 3840);
	    	vSetPlaneHeight(VDP_PQPATH, 2160);
		}
		else
		{//TODO,temp to bring up 4k60 BE
		    vSetPlaneWidth(VDP_PQPATH, 1920);
		    vSetPlaneHeight(VDP_PQPATH, 1080);
		}
        if(u4Pretype != u4type && fgVB1VideoMute != SV_TRUE)
        {
            fgVB1VideoMute = SV_TRUE;
            if(DRVCUST_Get_InterMemc_type()) vDrvSetPSCMute(SV_TRUE);
            DRVCUST_SendEvent(E_CUST_OUT_MUTE_CHANGE, 0);
        }

		DRVCUST_SendEvent(E_CUST_SRC_AREA_CHANGE, u1BePath[0]);
		u4Pretype = u4type;
	}
#endif
#endif
}

UINT32 vDrvCal4K2KPanelType(void)
{  
    UINT32 u4Type = PANEL_TYPE_4K2K_60;
#ifndef CC_MTK_LOADER
#ifndef CC_S_PLATFORM
    if(IS_SUPPORT_4K2K_DISPLAY)
#endif
    {
        if(IS_DISP_4K2K_TYPE1)
        {
            u4Type = fgIs4k2kNativeTimingOut(SV_VP_MAIN) ? PANEL_TYPE_4K2K_60 : PANEL_TYPE_2K1K_120;
        }
#ifdef CC_S_PLATFORM
        else if(IS_DISP_4K2K_TYPE2)//sony FRC model
        {
            if(bDrvVideoGetRefreshRate(SV_VP_MAIN) >= 96)
            {// input frame rate > 96, output FHD 120
                u4Type = PANEL_TYPE_2K1K_120;
            }
            else if((_eApplicationMode == SRM_TV_MODE_TYPE_PIP) ||(_eApplicationMode == SRM_TV_MODE_TYPE_POP))
            {
                u4Type = PANEL_TYPE_4K2K_60;
            }
            else if(u1SRSDynSwitchEn && bDrvVideoIs2KSignal(VDP_1) && !u1ScalingMode)
            {
                B2R_CUST_INFO_T t_B2RCustInfo;
                UINT8 bSignalType = bGetSignalType(VDP_1);
                if(B2R_Get_Cust_info(VDP_1, B2R_GET_TYPE_CUST_INFO, &t_B2RCustInfo, sizeof(B2R_CUST_INFO_T)))
                {
                    LOG(4, "\n====== B2R_Get_Cust_info fail drv_display.c======\n");
                }
                if(bSignalType == SV_ST_MPEG)
                {
                    if(t_B2RCustInfo.u4resolutionChg)
                    {
                        u4Type = PANEL_TYPE_4K2K_60;
                    }
                    else
                    {
                        u4Type = PANEL_TYPE_4K2K_60_SRS;
                    }
                }
                else
                {
                    u4Type = PANEL_TYPE_4K2K_60_SRS;
                }
            }
            else
            {
                u4Type = PANEL_TYPE_4K2K_60;
            }
        }
        else if(IS_DISP_4K2K_TYPE3)//sony BE model
        {
            if((IS_LVDS_DISP_3D_SHUTTER && (u1DrvTDTVCurIs3DMode() == SV_TRUE)) || (bDrvVideoGetRefreshRate(SV_VP_MAIN) >= 96))
            {// 3D or >96 frame rate input, output FHD 120

                if(u1DrvIsVdpBypCase())
                {
                    u4Type = PANEL_TYPE_4K2K_60_BYP;
                }
                else
                {
                    if(bDrvVideoIs2KSignal(VDP_1))
                    {
                        u4Type = PANEL_TYPE_2K1K_120;
                    }
                    else
                    {
                        u4Type = PANEL_TYPE_4K1K_120;
                    }
                }
            }
            else
            {
                if(IS_IC_5893_ES1())
                {
                    u4Type = PANEL_TYPE_4K2K_60;
                }
                else
                {
                    #ifdef CC_MIN_LOW_LATENCY_SUPPORT
                    if((vGetVDPColorMode(SV_VP_MAIN) == SV_COLOR_MODE_444) &&
                    u1SRSDynSwitchEn && bDrvVideoIs2KSignal(VDP_1) &&
                    (_eApplicationMode != SRM_TV_MODE_TYPE_PIP) &&
                    (_eApplicationMode != SRM_TV_MODE_TYPE_POP))
                    {
                        B2R_CUST_INFO_T t_B2RCustInfo;
                        UINT8 bSignalType = bGetSignalType(VDP_1);
                        if(B2R_Get_Cust_info(VDP_1, B2R_GET_TYPE_CUST_INFO, &t_B2RCustInfo, sizeof(B2R_CUST_INFO_T)))
                        {
                            LOG(4, "\n====== B2R_Get_Cust_info fail drv_display.c======\n");
                        }
                        
                        if((bSignalType == SV_ST_MPEG) && (t_B2RCustInfo.u4resolutionChg))
                        {
                            u4Type = PANEL_TYPE_4K2K_60;
                        }
                        else
                        {
                            u4Type = PANEL_TYPE_4K2K_60_SRS;
                        }
                    }
                    else
                    #endif
                    {
                        u4Type = PANEL_TYPE_4K2K_60;
                    }
               
                }
                vDrvVideoPlaneReConfig(u4Type);
            }
        }
        else //type3 for S_BE
        {
            if(bDrvVideoGetRefreshRate(SV_VP_MAIN) >= 96)
            {
                u4Type = PANEL_TYPE_4K1K_120;
            }
            else if((_eApplicationMode == SRM_TV_MODE_TYPE_PIP) ||(_eApplicationMode == SRM_TV_MODE_TYPE_POP))
            {
                u4Type = PANEL_TYPE_4K2K_60;
            }
            else if(u1SRSDynSwitchEn && bDrvVideoIs2KSignal(VDP_1) && !u1ScalingMode)
            {
                B2R_CUST_INFO_T t_B2RCustInfo;
                UINT8 bSignalType = bGetSignalType(VDP_1);
                if(B2R_Get_Cust_info(VDP_1, B2R_GET_TYPE_CUST_INFO, &t_B2RCustInfo, sizeof(B2R_CUST_INFO_T)))
                {
                    LOG(4, "\n====== B2R_Get_Cust_info fail drv_display.c======\n");
                }

                if(bSignalType == SV_ST_MPEG)
                {
                    if(t_B2RCustInfo.u4resolutionChg)
                    {
                        u4Type = PANEL_TYPE_4K2K_60;
                    }
                    else
                    {
                        u4Type = PANEL_TYPE_4K2K_60_SRS;
                    }
                }
                else
                {
                    u4Type = PANEL_TYPE_4K2K_60_SRS;
                }
            }
            else
            {
                u4Type = PANEL_TYPE_4K2K_60;
            }
        }
#endif
    }
#endif
    return u4Type;
}

void vDrvUpdatePanelParam(UINT32 u4Type)
{
    GRAPHMODE_ATTRIBUTE_T * prVideoModeAttr;
    //UINT32 u4Type = GetPANEL4K2K_TYPE();
    prVideoModeAttr = VideoMode_GetAttributeByType(u4Type);
    if(prVideoModeAttr==NULL)
    {
        LOG(0,"[4K2K]VideoModeAttribute[]for panel_#%d is NULL!!\n", GetCurrentPanelIndex());
    }
    else
    {
        if (SUPPORT_PANEL_48HZ)
        {
            updatePanelAttribute_4k2k(
            prVideoModeAttr->au2HTotal[SV_DCLK_48HZ],
            prVideoModeAttr->au2VTotal[SV_DCLK_48HZ],
            prVideoModeAttr->au4PixelClk[SV_DCLK_48HZ],
            prVideoModeAttr->u2GraphModeWidth,
            prVideoModeAttr->u2GraphModeHeight,
            prVideoModeAttr->au1VSyncWidth[SV_DCLK_48HZ],
            prVideoModeAttr->au2VBackPorch[SV_DCLK_48HZ],
            prVideoModeAttr->au1HSyncWidth[SV_DCLK_48HZ],
            prVideoModeAttr->au2HFrontPorch[SV_DCLK_48HZ],
            48);
        }
        updatePanelAttribute_4k2k(
        prVideoModeAttr->au2HTotal[SV_DCLK_50HZ],
        prVideoModeAttr->au2VTotal[SV_DCLK_50HZ],
        prVideoModeAttr->au4PixelClk[SV_DCLK_50HZ],
        prVideoModeAttr->u2GraphModeWidth,
        prVideoModeAttr->u2GraphModeHeight,
        prVideoModeAttr->au1VSyncWidth[SV_DCLK_50HZ],
        prVideoModeAttr->au2VBackPorch[SV_DCLK_50HZ],
        prVideoModeAttr->au1HSyncWidth[SV_DCLK_50HZ],
        prVideoModeAttr->au2HFrontPorch[SV_DCLK_50HZ],
        50);
        updatePanelAttribute_4k2k(
        prVideoModeAttr->au2HTotal[SV_DCLK_60HZ],
        prVideoModeAttr->au2VTotal[SV_DCLK_60HZ],
        prVideoModeAttr->au4PixelClk[SV_DCLK_60HZ],
        prVideoModeAttr->u2GraphModeWidth,
        prVideoModeAttr->u2GraphModeHeight,
        prVideoModeAttr->au1VSyncWidth[SV_DCLK_60HZ],
        prVideoModeAttr->au2VBackPorch[SV_DCLK_60HZ],
        prVideoModeAttr->au1HSyncWidth[SV_DCLK_60HZ],
        prVideoModeAttr->au2HFrontPorch[SV_DCLK_60HZ],
        60);

        LOG(0,"MTK_DISP [%s][%d] 60Hz HTxVT=%dx%d HAxVA=%dx%d\n",__FUNCTION__,__LINE__,
            prVideoModeAttr->au2HTotal[SV_DCLK_60HZ],prVideoModeAttr->au2VTotal[SV_DCLK_60HZ],
            prVideoModeAttr->u2GraphModeWidth,prVideoModeAttr->u2GraphModeHeight);

        PANEL_SetVClkMax(prVideoModeAttr->u1FrameRateMax);
        PANEL_SetVClkMin(prVideoModeAttr->u1FrameRateMin);
    }
}

void vDrvSwitchVideoType(UINT32 u4Type)
{
  /*
   #if defined(CC_FBM_SUPPORT_SRS)
    UINT8 i = 0;
    BOOL bEnable;
    #endif
    */
  
    UINT32 u4CtlWord = 0;
	UINT32 u4CtlWord2 = 0;
	UINT32 i4panelType = PANEL_4K2K_FHD60;
#ifndef CC_MTK_LOADER
#ifndef CC_S_PLATFORM
	UINT32 srcW=0,srcH=0,dstW=0,dstH=0,Enable=0,u4bpp=0;
	UINT32 u2Width=0, u2Height=0;
#endif
#endif
	UINT8 fgNeedFRCMute = TRUE;
    BOOL fgNeedRstVB1= SV_FALSE;

    if (_4k2kPanelType == u4Type)
    {
        LOG(0,"[4KBE] Skip switch panel mode(%d).\n", _4k2kPanelType);
		fg4k2kPNLTypeChg = SV_FALSE;
        return;
    }
    if(IS_DISP_4K2K_TYPE3)
    {
        fgNeedRstVB1 = SV_TRUE;
    }
    else if(IS_DISP_4K2K_TYPE2)
    {
        if((u4Type == PANEL_TYPE_2K1K_120) || (_4k2kPanelType == PANEL_TYPE_2K1K_120))
        {
            fgNeedRstVB1 = SV_TRUE;
        }
    }

    
	LOG(0,"[4KBE] old panel mode(%d)  new(%d)\n", _4k2kPanelType, u4Type);

#ifdef CC_S_PLATFORM
    if(fgVB1VideoMute != SV_TRUE)
    {
        fgVB1VideoMute = SV_TRUE;
        if(DRVCUST_Get_InterMemc_type()) vDrvSetPSCMute(SV_TRUE);
        DRVCUST_SendEvent(E_CUST_OUT_MUTE_CHANGE, 0);
    }
#endif

#if 0  // [Husky] no bypass path, need to review later[JW]
	if((_4k2kPanelType == PANEL_TYPE_4K2K_60_BYP)&&(u4Type != PANEL_TYPE_4K2K_60_BYP))
	{
	    _fgBypassToPQ = SV_TRUE;
		fgIsVsyncISRInit = SV_FALSE;
	}
#endif
#if 0  // Need to open if the panel timing changes.
	if (((_4k2kPanelType == PANEL_TYPE_2K1K_120)&&(u4Type != PANEL_TYPE_2K1K_120))
		|| ((_4k2kPanelType != PANEL_TYPE_2K1K_120)&&(u4Type == PANEL_TYPE_2K1K_120)))
	{
		fgTconISRNeedRst = TRUE;
	}
#endif
#if 0
	if (((_4k2kPanelType == PANEL_TYPE_4K1K_120)||(_4k2kPanelType == PANEL_TYPE_4K2K_60))
		&&((u4Type == PANEL_TYPE_4K1K_120)||(u4Type == PANEL_TYPE_4K2K_60)))  // for Wukong
	{
		fgNeedFRCMute = FALSE;
	}
#endif
	LOG(2, "@%s fgNeedFRCMute=%d\n",__FUNCTION__, fgNeedFRCMute);
   
	fg4k2kPNLTypeChg = SV_TRUE;
    _4k2kPanelType = u4Type;
	bSI_DISPLAY_DCLK_TYPE = SV_DCLK_UNKNOW;  // Force vDrvCalPanelTiming(...) to run, if _4k2kPanelType changes.

#if defined(CC_S_PLATFORM) && (defined(CC_HK_CUST) || defined(CC_WK_CUST) || defined(CC_CP_CUST))
    if(IS_DISP_4K2K_TYPE4)
#else
    if(IS_DISP_4K2K_TYPE2 || IS_DISP_4K2K_TYPE4 || IS_DISP_4K2K_TYPE3)
#endif
	{
	    VERIFY (x_sema_lock(_hFrcSetPanelSema, X_SEMA_OPTION_WAIT) == OSR_OK);
	    u4CtlWord = PANEL_GetControlWord() & 0xFFFFFFE7;
        u4CtlWord2 = PANEL_GetControlWord2() & 0xFFFFFDFF;  // clean linebuffer bit first
	}
    switch (u4Type)
    {
    #if 0  // [Husky] no bypass path, need to review later[JW]
        case PANEL_TYPE_4K2K_60_BYP:
            vSetPlaneEnable(VDP_BYPASS, SV_ON);
            vSetPlaneEnable(VDP_PQPATH, SV_OFF);
            vSetPlaneWidth(VDP_BYPASS, 3840);
            vSetPlaneHeight(VDP_BYPASS, 2160);
        	vSetOutputPlane(VDP_1, VDP_BYPASS);
        	vSetOutputPlane(VDP_2, VDP_GFXOUT);
        	vScpipTGInOutSel(E_GFX_TG);
			if(IS_DISP_4K2K_TYPE4)
			{
				u4CtlWord = u4CtlWord | OCTA_PORT;
				u4CtlWord2 = u4CtlWord2 | DISABLE_LVDS_LINEBUFFER;
				i4panelType = PANEL_4K2K_60;
			}
            break;
	#endif
		case PANEL_TYPE_4K2K_60:
            vSetPlaneEnable(VDP_BYPASS, SV_OFF);
            vSetPlaneEnable(VDP_PQPATH, SV_ON);
			#ifdef CC_S_PLATFORM
			if(IS_DISP_4K2K_TYPE3)
			{
                if((IS_IC_5893_ES1() && (vGetVDPColorMode(SV_VP_MAIN) == SV_COLOR_MODE_444)) || 
                    (_eApplicationMode == SRM_TV_MODE_TYPE_PIP) || (_eApplicationMode == SRM_TV_MODE_TYPE_POP) || 
                    (wDrvVideoInputHeight(VDP_1) > 1088))
				{
			    	vSetPlaneWidth(VDP_PQPATH, 3840);
			    	vSetPlaneHeight(VDP_PQPATH, 2160);
				}
				else
				{//TODO ,temp to bring up 4k60 BE
				    vSetPlaneWidth(VDP_PQPATH, 1920);
				    vSetPlaneHeight(VDP_PQPATH, 1080);
				}
			
			}
			else
			#endif
            {
                vSetPlaneWidth(VDP_PQPATH, 3840);
                vSetPlaneHeight(VDP_PQPATH, 2160);
            }

			vSetOutputPlane(VDP_1, VDP_PQPATH);
        	vSetOutputPlane(VDP_2, VDP_PQPATH);
        	vScpipTGInOutSel(E_FSC_TG);
			if(IS_DISP_4K2K_TYPE4)
			{
				u4CtlWord = u4CtlWord | OCTA_PORT;
				u4CtlWord2 = u4CtlWord2 | DISABLE_LVDS_LINEBUFFER;
				i4panelType = PANEL_4K2K_60;
			}
            break;
        #if defined(CC_FBM_SUPPORT_SRS)
        case PANEL_TYPE_4K2K_60_SRS:		
            vSetPlaneEnable(VDP_BYPASS, SV_OFF);
            vSetPlaneEnable(VDP_PQPATH, SV_ON);

            if(fgIsMJCToOSTG())
            {
                vSetPlaneWidth(VDP_PQPATH, 3840);
                vSetPlaneHeight(VDP_PQPATH, 2160);
            }
            else
            {
                vSetPlaneWidth(VDP_PQPATH, 1920);
                vSetPlaneHeight(VDP_PQPATH, 1080);
            }

			vSetOutputPlane(VDP_1, VDP_PQPATH);
        	vSetOutputPlane(VDP_2, VDP_PQPATH);
        	vScpipTGInOutSel(E_FSC_TG);
			if(IS_DISP_4K2K_TYPE4)
			{
				u4CtlWord = u4CtlWord | OCTA_PORT;
				u4CtlWord2 = u4CtlWord2 | DISABLE_LVDS_LINEBUFFER;
				i4panelType = PANEL_4K2K_60;
			}
            break;
        #endif    
        case PANEL_TYPE_4K1K_60:
            vSetPlaneEnable(VDP_BYPASS, SV_OFF);
            vSetPlaneEnable(VDP_PQPATH, SV_ON);
            vSetPlaneWidth(VDP_PQPATH, 1920);
            vSetPlaneHeight(VDP_PQPATH, 1080);
            vSetOutputPlane(VDP_1, VDP_PQPATH);
            vSetOutputPlane(VDP_2, VDP_PQPATH);
            vScpipTGInOutSel(E_FSC_TG);
			if (IS_DISP_4K2K_TYPE2 || IS_DISP_4K2K_TYPE4)
			{
				u4CtlWord = u4CtlWord | FOUR_PORT;	
				u4CtlWord2 = u4CtlWord2 | DISABLE_LVDS_LINEBUFFER;
			}
            break;
		case PANEL_TYPE_4K2K_30:
            vSetPlaneEnable(VDP_BYPASS, SV_OFF);
            vSetPlaneEnable(VDP_PQPATH, SV_ON);
            vSetPlaneWidth(VDP_PQPATH, 3840);
            vSetPlaneHeight(VDP_PQPATH, 2160);
            vSetOutputPlane(VDP_1, VDP_PQPATH);
            vSetOutputPlane(VDP_2, VDP_PQPATH);
            vScpipTGInOutSel(E_FSC_TG);
			if (IS_DISP_4K2K_TYPE2 || IS_DISP_4K2K_TYPE4)
			{
				u4CtlWord = u4CtlWord | FOUR_PORT;
				u4CtlWord2 = u4CtlWord2 | DISABLE_LVDS_LINEBUFFER;
				i4panelType = PANEL_4K2K_30;
			}
            break;
		case PANEL_TYPE_2K1K_60:
            vSetPlaneEnable(VDP_BYPASS, SV_OFF);
            vSetPlaneEnable(VDP_PQPATH, SV_ON);
            vSetPlaneWidth(VDP_PQPATH, 1920);
            vSetPlaneHeight(VDP_PQPATH, 1080);
            vSetOutputPlane(VDP_1, VDP_PQPATH);
            vSetOutputPlane(VDP_2, VDP_PQPATH);
            vScpipTGInOutSel(E_FSC_TG);
			if (IS_DISP_4K2K_TYPE2 || IS_DISP_4K2K_TYPE4)
			{
			    
				u4CtlWord = u4CtlWord | DUAL_PORT;	
				i4panelType = PANEL_4K2K_FHD60;
			}
            break;
		case PANEL_TYPE_720P_120:
            vSetPlaneEnable(VDP_BYPASS, SV_OFF);
            vSetPlaneEnable(VDP_PQPATH, SV_ON);
            vSetPlaneWidth(VDP_PQPATH, 1280);
            vSetPlaneHeight(VDP_PQPATH, 720);
            vSetOutputPlane(VDP_1, VDP_PQPATH);
            vSetOutputPlane(VDP_2, VDP_PQPATH);
            vScpipTGInOutSel(E_FSC_TG);
            break;
		case PANEL_TYPE_1920_540P:
            vSetPlaneEnable(VDP_BYPASS, SV_OFF);
            vSetPlaneEnable(VDP_PQPATH, SV_ON);
            vSetPlaneWidth(VDP_PQPATH, 1920);
            vSetPlaneHeight(VDP_PQPATH, 540);
            vSetOutputPlane(VDP_1, VDP_PQPATH);
            vSetOutputPlane(VDP_2, VDP_PQPATH);
            vScpipTGInOutSel(E_FSC_TG);
            break;
        case PANEL_TYPE_4K1K_120:
            vSetPlaneEnable(VDP_BYPASS, SV_OFF);
            vSetPlaneEnable(VDP_PQPATH, SV_ON);
            if(fgIsMJCToOSTG())
            {
                vSetPlaneWidth(VDP_PQPATH, 3840);
                vSetPlaneHeight(VDP_PQPATH, 2160);
            }
            else
            {
                vSetPlaneWidth(VDP_PQPATH, 3840);
                vSetPlaneHeight(VDP_PQPATH, 1080);
            }
            vSetOutputPlane(VDP_1, VDP_PQPATH);
            vSetOutputPlane(VDP_2, VDP_PQPATH);
            vScpipTGInOutSel(E_FSC_TG);
            break;
        case PANEL_TYPE_2K1K_120:
            vSetPlaneEnable(VDP_BYPASS, SV_OFF);
            vSetPlaneEnable(VDP_PQPATH, SV_ON);
            vSetPlaneWidth(VDP_PQPATH, 1920);
            vSetPlaneHeight(VDP_PQPATH, 1080);
            vSetOutputPlane(VDP_1, VDP_PQPATH);
            vSetOutputPlane(VDP_2, VDP_PQPATH);
            vScpipTGInOutSel(E_FSC_TG);
            if (IS_DISP_4K2K_TYPE2 || IS_DISP_4K2K_TYPE4)
            {
            u4CtlWord = u4CtlWord | FOUR_PORT;
            i4panelType = PANEL_4K2K_FHD120;
            }
            break;
        default:
            break;
    }
#if defined(CC_S_PLATFORM) && (defined(CC_HK_CUST) || defined(CC_WK_CUST) || defined(CC_CP_CUST))
    if (IS_DISP_4K2K_TYPE4)
#else
    if (IS_DISP_4K2K_TYPE2 || IS_DISP_4K2K_TYPE4)
#endif
	{
		if (fgNeedFRCMute)
		{
		    vExt4k2kFrcMute(SV_ON);
			//vDrvSetAllMute(SV_ON);
			if (fgIsTCONIntrEn())
			{
				vDrvTCONIntrClr();
				vDrvTCONIntrDisable();	
				vDrvTCONDisableBIMInt();
				LOG(3, "[TCON_TCH]@%s Disable\n", __FUNCTION__);
			}
		    vDrvVByOneOff();	
		}
	    PANEL_SetControlWord(u4CtlWord);
		PANEL_SetControlWord2(u4CtlWord2);
    	if(PANEL_GetDisplayType()== PANEL_DISPLAY_TYPE_VB1)
	    {		
			vDrvVB14K2KPanelInterfaceConfig();	    
    	}
    }
    if(fgNeedRstVB1)
    {
        vDrvVByOneOff();
    }

    vDrvUpdatePanelParam(GetPANEL4K2K_TYPE());
    vSaveRefAttribute_4K2K();

#if defined(CC_S_PLATFORM) && (defined(CC_HK_CUST) || defined(CC_WK_CUST) || defined(CC_CP_CUST))
    if (IS_DISP_4K2K_TYPE4)
#else
    if (IS_DISP_4K2K_TYPE2 || IS_DISP_4K2K_TYPE4)
#endif
	{
		vErrorHandleSetByPanel();
		#ifdef CC_MTK_LOADER
		vDrvVOPLLFreqSet(
					(vDrvGetLCDFreq()==25 || vDrvGetLCDFreq()==50 || vDrvGetLCDFreq()==100)?
						(PANEL_GetPixelClk50Hz()):
						(SUPPORT_PANEL_48HZ && (vDrvGetLCDFreq()==24 || vDrvGetLCDFreq()==48))?
						(PANEL_GetPixelClk48Hz()):
						(PANEL_GetPixelClk60Hz()));			
        #else

	    vDrvVOFreqSet(
	        (vDrvGetLCDFreq()==25 || vDrvGetLCDFreq()==50 || vDrvGetLCDFreq()==100)?
	            (PANEL_GetPixelClk50Hz()):
            (SUPPORT_PANEL_48HZ && (vDrvGetLCDFreq()==24 || vDrvGetLCDFreq()==48))?
            (PANEL_GetPixelClk48Hz()):
            (PANEL_GetPixelClk60Hz()));
       #endif 

        vDrvSetPanelTiming();
		IGNORE_RET(OSD_Base_SetHStart());

    	if(PANEL_GetDisplayType()== PANEL_DISPLAY_TYPE_VB1)
    	{
    		if (fgNeedFRCMute)
    		{
            #if 0  // Capy remove
				vVByOneLaneSwapForHk();
			#endif
				vDrvVByOneOn();
				if (!fgIsTCONIntrEn())
				{
					vDrvTCONEnableBIMInt();
					vDrvTCONIntrClr();
					vDrvTCONIntrEn();				
					LOG(3, "[TCON_TCH]@%s Enable\n", __FUNCTION__);
				}
    		}
    	}

		vSend4k2kFrcPanelTypeCmd(i4panelType);  // move to here
		if (fgNeedFRCMute)
		{
			if ((psCur4k2kHandler==NULL) || (psCur4k2kHandler->frc_snd_mute==NULL))
		    {
		        LOG(1,"[4k2k]No FRC mute --> set all mute!\n");
		        vDrvSetAllMute(SV_OFF);
		    }
		#ifndef NDEBUG
		    LOG(1,"[4k2k]unmute timer start %d\n", u2Ext4k2kGetMuteTime());
		#endif
		    vExt4k2kFrcUmMuteTimer(u2Ext4k2kGetMuteTime());
		}
		x_sema_unlock(_hFrcSetPanelSema);
		
	}
    if (fgNeedRstVB1)
    {
        vErrorHandleSetByPanel();

        if(PANEL_GetDisplayType()== PANEL_DISPLAY_TYPE_VB1)
        {
            vDrvVByOneOn();
        }
        fgNeedRstVB1 = SV_FALSE;
    }

	#ifndef CC_MTK_LOADER
	#ifdef CC_S_PLATFORM

	DRVCUST_SendEvent(E_CUST_SRC_AREA_CHANGE, u1BePath[0]);
	DRVCUST_SendEvent(E_CUST_SCALING_REQ_CHANGE, u1BePath[0]);
	#endif

    #ifdef CC_LCE_SUPPORT
    //init local contrast hw&sw
    u1LCESWInitialized = SV_FALSE;
    vDrvLCEInit();
    #endif
	#endif
}

void vFrcUnmute(void *pvArgs)
{
    while(TRUE)
    {
        VERIFY (x_sema_lock(_hFrcUnmuteSema, X_SEMA_OPTION_WAIT) == OSR_OK);
        vExt4k2kFrcMute(SV_OFF);
        LOG(1,"\n[4k2k].....FRC Unmute\n");
        vDrvSetAllMute(SV_OFF);
    }
}

void Panel4K2KStatus(void)
{
    LOG(0,"[4k2k] Panel4K2K Type: %d\n", _4k2kPanelType);
}

#endif // CC_MTK_LOADER

#define DBG_4K_REF_ATTRIBUTE 0
void vPanelAttribute_Status_4k_Ref(void)
{
#if DBG_4K_REF_ATTRIBUTE
    UINT8 u1Cnt=0;

	if(SUPPORT_PANEL_48HZ)
	{
		u1Cnt=SV_DCLK_48HZ;
	}
	else
	{
		u1Cnt=SV_DCLK_50HZ;
	}

	for( ;u1Cnt<SV_DCLK_OTHERS;u1Cnt++)	
    {
        LOG(0,"REF_4K_TG_u2FrameRate[%d] = %d\n",u1Cnt, rREF_4K_TG[u1Cnt].u2FrameRate);
        LOG(0,"REF_4K_TG_u2Width[%d] = %d\n",u1Cnt, rREF_4K_TG[u1Cnt].u2Width);
        LOG(0,"REF_4K_TG_u2Height[%d] = %d\n", u1Cnt,rREF_4K_TG[u1Cnt].u2Height );
        LOG(0,"REF_4K_TG_u2HTotal[%d] = %d\n", u1Cnt,rREF_4K_TG[u1Cnt].u2HTotal);
        LOG(0,"REF_4K_TG_u2VTotal[%d] = %d\n", u1Cnt,rREF_4K_TG[u1Cnt].u2VTotal);
        LOG(0,"REF_4K_TG_u4PixelClk[%d] = %d\n", u1Cnt,rREF_4K_TG[u1Cnt].u4PixelClk);
        LOG(0,"REF_4K_TG_u4VsyncWidth[%d] = %d\n", u1Cnt, rREF_4K_TG[u1Cnt].u4VsyncWidth);
        LOG(0,"REF_4K_TG_u4VsyncBp[%d] = %d\n", u1Cnt, rREF_4K_TG[u1Cnt].u4VsyncBp);
    }
#endif
}

void vSaveRefAttribute_4K2K(void)
{
	// GRAPHMODE_ATTRIBUTE_T * prVideoModeAttr;

    LOG(0, " vSaveRefAttribute_4K2K\n");


    if (PANEL_Is8K4K30())
    {
        // 4k4k@30Hz
        LOG(3, "[8K30][Ref_timing]4K4K@30Hz\n");
        rREF_4K_TG[SV_DCLK_60HZ].u2FrameRate = 30;
        rREF_4K_TG[SV_DCLK_60HZ].u2Width = PANEL_GetPanelWidth() >> 1;
        rREF_4K_TG[SV_DCLK_60HZ].u2Height = PANEL_GetPanelHeight();
        rREF_4K_TG[SV_DCLK_60HZ].u2HTotal = PANEL_GetHTotal60Hz() >>1;
        rREF_4K_TG[SV_DCLK_60HZ].u2VTotal = PANEL_GetVTotal60Hz();
        rREF_4K_TG[SV_DCLK_60HZ].u4PixelClk = PANEL_GetPixelClk60Hz()>>1;
        rREF_4K_TG[SV_DCLK_60HZ].u2VsyncWidth = PANEL_GetVSyncWidthByTiming(SV_DCLK_60HZ);
        rREF_4K_TG[SV_DCLK_60HZ].u2VsyncBp = PANEL_GetVSyncBpByTiming(SV_DCLK_60HZ);
        rREF_4K_TG[SV_DCLK_60HZ].u2HsyncWidth = PANEL_GetHSyncWidthByTiming(SV_DCLK_60HZ) >> 1;
        rREF_4K_TG[SV_DCLK_60HZ].u2HsyncFp = PANEL_GetHSyncFpByTiming(SV_DCLK_60HZ) >> 1;

        rREF_4K_TG[SV_DCLK_50HZ].u2FrameRate = 25;
        rREF_4K_TG[SV_DCLK_50HZ].u2Width = PANEL_GetPanelWidth() >>1;
        rREF_4K_TG[SV_DCLK_50HZ].u2Height = PANEL_GetPanelHeight();
        rREF_4K_TG[SV_DCLK_50HZ].u2HTotal = PANEL_GetHTotal50Hz()>>1;
        rREF_4K_TG[SV_DCLK_50HZ].u2VTotal = PANEL_GetVTotal50Hz();
        rREF_4K_TG[SV_DCLK_50HZ].u4PixelClk = PANEL_GetPixelClk50Hz()>>1;
        rREF_4K_TG[SV_DCLK_50HZ].u2VsyncWidth = PANEL_GetVSyncWidthByTiming(SV_DCLK_50HZ);
        rREF_4K_TG[SV_DCLK_50HZ].u2VsyncBp = PANEL_GetVSyncBpByTiming(SV_DCLK_50HZ);
        rREF_4K_TG[SV_DCLK_50HZ].u2HsyncWidth = PANEL_GetHSyncWidthByTiming(SV_DCLK_50HZ) >> 1;
        rREF_4K_TG[SV_DCLK_50HZ].u2HsyncFp = PANEL_GetHSyncFpByTiming(SV_DCLK_50HZ) >> 1;

        if (SUPPORT_PANEL_48HZ)
        {
            rREF_4K_TG[SV_DCLK_48HZ].u2FrameRate = 24;
            rREF_4K_TG[SV_DCLK_48HZ].u2Width = PANEL_GetPanelWidth() >> 1;
            rREF_4K_TG[SV_DCLK_48HZ].u2Height = PANEL_GetPanelHeight();
            rREF_4K_TG[SV_DCLK_48HZ].u2HTotal = PANEL_GetHTotal48Hz() >>1;
            rREF_4K_TG[SV_DCLK_48HZ].u2VTotal = PANEL_GetVTotal48Hz();
            rREF_4K_TG[SV_DCLK_48HZ].u4PixelClk = PANEL_GetPixelClk48Hz()>>1;
            rREF_4K_TG[SV_DCLK_48HZ].u2VsyncWidth = PANEL_GetVSyncWidthByTiming(SV_DCLK_48HZ);
            rREF_4K_TG[SV_DCLK_48HZ].u2VsyncBp = PANEL_GetVSyncBpByTiming(SV_DCLK_48HZ);
            rREF_4K_TG[SV_DCLK_48HZ].u2HsyncWidth = PANEL_GetHSyncWidthByTiming(SV_DCLK_48HZ) >> 1;
            rREF_4K_TG[SV_DCLK_48HZ].u2HsyncFp = PANEL_GetHSyncFpByTiming(SV_DCLK_48HZ) >> 1;
        }
    }
    else if (PANEL_Is4K2K120())
    {
        // 4k2k@60Hz
        if(VDP_LogChkMsk(VDP_1,VDP_LOG_DBG_MSK_TRK)) LOG(0, "@MTK_TRK [Ref_timing]4K2K@120Hz\n");
        if(VDP_LogChkMsk(VDP_1,VDP_LOG_DBG_MSK_TRK)) 
            LOG(0,"@MTK_REF Pnl WxH=%dx%d HTxVT=%dx%d Clk:%d Vsync:%d VBp:%d \n",
                PANEL_GetPanelWidth(),PANEL_GetPanelHeight(),
                PANEL_GetHTotal60Hz(),PANEL_GetVTotal60Hz(),
                PANEL_GetPixelClk60Hz(),
                PANEL_GetVSyncWidthByTiming(SV_DCLK_60HZ),PANEL_GetVSyncBpByTiming(SV_DCLK_60HZ));

        rREF_4K_TG[SV_DCLK_60HZ].u2FrameRate = 60;
        rREF_4K_TG[SV_DCLK_60HZ].u2Width = PANEL_GetPanelWidth();
        rREF_4K_TG[SV_DCLK_60HZ].u2Height = PANEL_GetPanelHeight();
        rREF_4K_TG[SV_DCLK_60HZ].u2HTotal = PANEL_GetHTotal60Hz();
        rREF_4K_TG[SV_DCLK_60HZ].u2VTotal = PANEL_GetVTotal60Hz();
        rREF_4K_TG[SV_DCLK_60HZ].u4PixelClk = PANEL_GetPixelClk60Hz()>>1;
        rREF_4K_TG[SV_DCLK_60HZ].u2VsyncWidth = PANEL_GetVSyncWidthByTiming(SV_DCLK_60HZ);
        rREF_4K_TG[SV_DCLK_60HZ].u2VsyncBp = PANEL_GetVSyncBpByTiming(SV_DCLK_60HZ);
        rREF_4K_TG[SV_DCLK_60HZ].u2HsyncWidth = PANEL_GetHSyncWidthByTiming(SV_DCLK_60HZ);
        rREF_4K_TG[SV_DCLK_60HZ].u2HsyncFp = PANEL_GetHSyncFpByTiming(SV_DCLK_60HZ);

        rREF_4K_TG[SV_DCLK_50HZ].u2FrameRate = 50;
        rREF_4K_TG[SV_DCLK_50HZ].u2Width = PANEL_GetPanelWidth();
        rREF_4K_TG[SV_DCLK_50HZ].u2Height = PANEL_GetPanelHeight();
        rREF_4K_TG[SV_DCLK_50HZ].u2HTotal = PANEL_GetHTotal50Hz();
        rREF_4K_TG[SV_DCLK_50HZ].u2VTotal = PANEL_GetVTotal50Hz();
        rREF_4K_TG[SV_DCLK_50HZ].u4PixelClk = PANEL_GetPixelClk50Hz()>>1;
        rREF_4K_TG[SV_DCLK_50HZ].u2VsyncWidth = PANEL_GetVSyncWidthByTiming(SV_DCLK_50HZ);
        rREF_4K_TG[SV_DCLK_50HZ].u2VsyncBp = PANEL_GetVSyncBpByTiming(SV_DCLK_50HZ);
        rREF_4K_TG[SV_DCLK_50HZ].u2HsyncWidth = PANEL_GetHSyncWidthByTiming(SV_DCLK_50HZ);
        rREF_4K_TG[SV_DCLK_50HZ].u2HsyncFp = PANEL_GetHSyncFpByTiming(SV_DCLK_50HZ);

        if (SUPPORT_PANEL_48HZ)
        {
            rREF_4K_TG[SV_DCLK_48HZ].u2FrameRate = 48;
            rREF_4K_TG[SV_DCLK_48HZ].u2Width = PANEL_GetPanelWidth();
            rREF_4K_TG[SV_DCLK_48HZ].u2Height = PANEL_GetPanelHeight();
            rREF_4K_TG[SV_DCLK_48HZ].u2HTotal = PANEL_GetHTotal48Hz();
            rREF_4K_TG[SV_DCLK_48HZ].u2VTotal = PANEL_GetVTotal48Hz();
            rREF_4K_TG[SV_DCLK_48HZ].u4PixelClk = PANEL_GetPixelClk48Hz()>>1;
            rREF_4K_TG[SV_DCLK_48HZ].u2VsyncWidth = PANEL_GetVSyncWidthByTiming(SV_DCLK_48HZ);
            rREF_4K_TG[SV_DCLK_48HZ].u2VsyncBp = PANEL_GetVSyncBpByTiming(SV_DCLK_48HZ);
            rREF_4K_TG[SV_DCLK_48HZ].u2HsyncWidth = PANEL_GetHSyncWidthByTiming(SV_DCLK_48HZ);
            rREF_4K_TG[SV_DCLK_48HZ].u2HsyncFp = PANEL_GetHSyncFpByTiming(SV_DCLK_48HZ);
        }
    }
    #if 0
    if (PANEL_Is4K2K120() || PANEL_Is8K4K30())
    {
        // 4k4k@30Hz
        if(VDP_LogChkMsk(VDP_1,VDP_LOG_DBG_MSK_TRK)) LOG(0, "@MTK_TRK [Ref_timing]4K2K@120Hz\n");
        rREF_4K_TG[SV_DCLK_60HZ].u2FrameRate = 30;
        rREF_4K_TG[SV_DCLK_60HZ].u2Width = PANEL_GetPanelWidth() >> 1;
        rREF_4K_TG[SV_DCLK_60HZ].u2Height = PANEL_GetPanelHeight();
        rREF_4K_TG[SV_DCLK_60HZ].u2HTotal = PANEL_GetHTotal60Hz() >>1; 
        rREF_4K_TG[SV_DCLK_60HZ].u2VTotal = PANEL_GetVTotal60Hz();
        rREF_4K_TG[SV_DCLK_60HZ].u4PixelClk = PANEL_GetPixelClk60Hz()>>1;
        rREF_4K_TG[SV_DCLK_60HZ].u2VsyncWidth = PANEL_GetVSyncWidthByTiming(SV_DCLK_60HZ);
        rREF_4K_TG[SV_DCLK_60HZ].u2VsyncBp = PANEL_GetVSyncBpByTiming(SV_DCLK_60HZ);

        rREF_4K_TG[SV_DCLK_50HZ].u2FrameRate = 25;
        rREF_4K_TG[SV_DCLK_50HZ].u2Width = PANEL_GetPanelWidth() >>1;
        rREF_4K_TG[SV_DCLK_50HZ].u2Height = PANEL_GetPanelHeight();
        rREF_4K_TG[SV_DCLK_50HZ].u2HTotal = PANEL_GetHTotal50Hz()>>1;
        rREF_4K_TG[SV_DCLK_50HZ].u2VTotal = PANEL_GetVTotal50Hz();
        rREF_4K_TG[SV_DCLK_50HZ].u4PixelClk = PANEL_GetPixelClk50Hz()>>1;
        rREF_4K_TG[SV_DCLK_50HZ].u2VsyncWidth = PANEL_GetVSyncWidthByTiming(SV_DCLK_50HZ);
        rREF_4K_TG[SV_DCLK_50HZ].u2VsyncBp = PANEL_GetVSyncBpByTiming(SV_DCLK_50HZ);
         
        if (SUPPORT_PANEL_48HZ)
        {
            rREF_4K_TG[SV_DCLK_48HZ].u2FrameRate = 24;
            rREF_4K_TG[SV_DCLK_48HZ].u2Width = PANEL_GetPanelWidth() >> 1;
            rREF_4K_TG[SV_DCLK_48HZ].u2Height = PANEL_GetPanelHeight();
            rREF_4K_TG[SV_DCLK_48HZ].u2HTotal = PANEL_GetHTotal48Hz() >>1; 
            rREF_4K_TG[SV_DCLK_48HZ].u2VTotal = PANEL_GetVTotal48Hz();
            rREF_4K_TG[SV_DCLK_48HZ].u4PixelClk = PANEL_GetPixelClk48Hz()>>1;
            rREF_4K_TG[SV_DCLK_48HZ].u2VsyncWidth = PANEL_GetVSyncWidthByTiming(SV_DCLK_48HZ);
            rREF_4K_TG[SV_DCLK_48HZ].u2VsyncBp = PANEL_GetVSyncBpByTiming(SV_DCLK_48HZ);
        }
    }
    #endif
    else
    {
		 // 2k2k@60
		rREF_4K_TG[SV_DCLK_60HZ].u2FrameRate = 60;
		rREF_4K_TG[SV_DCLK_60HZ].u2Width = (UINT16)PANEL_GetPanelWidth();
		rREF_4K_TG[SV_DCLK_60HZ].u2Height = (UINT16)PANEL_GetPanelHeight();
		rREF_4K_TG[SV_DCLK_60HZ].u2HTotal = (UINT16)PANEL_GetHTotal60Hz(); 
		rREF_4K_TG[SV_DCLK_60HZ].u2VTotal = (UINT16)PANEL_GetVTotal60Hz();
		rREF_4K_TG[SV_DCLK_60HZ].u4PixelClk = PANEL_GetPixelClk60Hz();
		rREF_4K_TG[SV_DCLK_60HZ].u2VsyncWidth =(UINT16) PANEL_GetVSyncWidthByTiming(SV_DCLK_60HZ);
		rREF_4K_TG[SV_DCLK_60HZ].u2VsyncBp = (UINT16)PANEL_GetVSyncBpByTiming(SV_DCLK_60HZ);

		rREF_4K_TG[SV_DCLK_50HZ].u2FrameRate = 50;
		rREF_4K_TG[SV_DCLK_50HZ].u2Width = (UINT16)PANEL_GetPanelWidth();
		rREF_4K_TG[SV_DCLK_50HZ].u2Height = (UINT16)PANEL_GetPanelHeight();
		rREF_4K_TG[SV_DCLK_50HZ].u2HTotal = (UINT16)PANEL_GetHTotal50Hz();
		rREF_4K_TG[SV_DCLK_50HZ].u2VTotal = (UINT16)PANEL_GetVTotal50Hz();
		rREF_4K_TG[SV_DCLK_50HZ].u4PixelClk = PANEL_GetPixelClk50Hz();
		rREF_4K_TG[SV_DCLK_50HZ].u2VsyncWidth = (UINT16)PANEL_GetVSyncWidthByTiming(SV_DCLK_50HZ);
		rREF_4K_TG[SV_DCLK_50HZ].u2VsyncBp = (UINT16)PANEL_GetVSyncBpByTiming(SV_DCLK_50HZ);
		 
		   if (SUPPORT_PANEL_48HZ)
		   {
			rREF_4K_TG[SV_DCLK_48HZ].u2FrameRate = 48;
			rREF_4K_TG[SV_DCLK_48HZ].u2Width = (UINT16)PANEL_GetPanelWidth();
			rREF_4K_TG[SV_DCLK_48HZ].u2Height = (UINT16)PANEL_GetPanelHeight();
			rREF_4K_TG[SV_DCLK_48HZ].u2HTotal = (UINT16)PANEL_GetHTotal48Hz(); 
			rREF_4K_TG[SV_DCLK_48HZ].u2VTotal = (UINT16)PANEL_GetVTotal48Hz();
			rREF_4K_TG[SV_DCLK_48HZ].u4PixelClk = PANEL_GetPixelClk48Hz();
			rREF_4K_TG[SV_DCLK_48HZ].u2VsyncWidth =(UINT16) PANEL_GetVSyncWidthByTiming(SV_DCLK_48HZ);
			rREF_4K_TG[SV_DCLK_48HZ].u2VsyncBp = (UINT16)PANEL_GetVSyncBpByTiming(SV_DCLK_48HZ);
	   }
	    

	vPanelAttribute_Status_4k_Ref();
    }
}


void LDR_PQModuleInit(void)
{
	// SRS	
	#ifdef CC_FBM_SUPPORT_SRS	
    // vIO32WriteFldAlign(SRS_CTRL, 0, C_SRS_BYPASS);    
	vIO32WriteFldAlign(MTK_SR_0, 0, SR_SEL);
	// initial SRS to 4K4K mode CLK
	#ifndef CC_MTK_LOADER
	if(GetPANEL4K2K_TYPE() ==  PANEL_TYPE_MAX)
	{
        vIO32WriteFldAlign(SRS_CTRL, SV_OFF ,C_SRS_FSAMPLE_EN);

        vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG,0,FLD_VOCLK_SEL); 
        vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG,2,FLD_SRSCLK_SEL); 
        vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG,0,FLD_VICLK_SEL); 
        if(IO32ReadFldAlign(CKGEN_DISP_CKCFG,FLD_POCLK_SEL) == 0)
        {
            vIO32WriteFldAlign(CKGEN_REG_MIB_OCLK_CKCFG,0,FLD_SC_POCLK_SEL); 
        }
        else
        {
            vIO32WriteFldAlign(CKGEN_REG_MIB_OCLK_CKCFG,4,FLD_SC_POCLK_SEL); 
        }
    }
    #endif
	#else
	vIO32WriteFldAlign(SRS_CTRL, 0, C_SRS_BYPASS);
	vIO32WriteFldAlign(SUPERRES_SHADOW_CTRL, 1, FORCE_COMMIT);
	vIO32WriteFldAlign(SUPERRES_00, 0, SR_BYPASS_EN);	
	vIO32WriteFldAlign(MTK_SR_0, 1, SR_SEL);	
	//vIO32WriteFldAlign(MTK_SR_1, 1, MTK_SR_MASK_EN);
	#endif
	
    //sharpness
    vIO32WriteFldAlign(TDPROC_MISC_00, 0, TDPROC_BYPASS_ALL);
    vIO32WriteFldAlign(TDPROC_MISC_00, 0, NO_VERTICAL_SHARP);	

	vIO32WriteFldAlign(TDPROC_MISC_01, 0x0, C_SHP_DSR_EN);

    //SRS
    #if 0//def CC_FBM_SUPPORT_SRS   
	if(PANEL_GetPanelWidth() > 1920)
	{
		vIO32WriteFldAlign(SRS_CTRL, 0x0, C_SRS_FSAMPLE_EN);

		vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG, 0x0, FLD_VICLK_SEL);
		vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG, 0x2, FLD_SRSCLK_SEL);
		vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG, 0x0, FLD_VOCLK_SEL);	

		vIO32WriteFldAlign(SRS_MODE, 0x0, SRS_DISABLE);

		vIO32WriteFldAlign(SRS_CTRL, 0x1, C_SRS_FSAMPLE_EN);

		vIO32WriteFldAlign(SRS_CTRL, 0x1, C_SRS_CLK_EN);
		vIO32WriteFldAlign(SRS_CTRL, 0x0, C_SRS_BYPASS);
	}
    #endif
    //3D colormapping
    vIO32WriteFldAlign(COLOR_MAPPING_CONTROL, 0, CM_BYPASS); 

	//init local contrast
	#ifdef CC_LCE_SUPPORT
	vIO32WriteFldAlign(LCC_DISP_AAL_SHADOW_CTRL, 1, LCC_FORCE_COMMIT);
	vIO32WriteFldAlign(LCC_DISP_AAL_EN, 1, LCC_AAL_EN);
	vIO32WriteFldAlign(LCC_DISP_AAL_SIZE, PANEL_GetPanelWidth(), LCC_HSIZE);
	vIO32WriteFldAlign(LCC_DISP_AAL_SIZE, PANEL_GetPanelHeight(), LCC_VSIZE);
	vIO32WriteFldAlign(DISP_AAL_1, 0, BYPASS_DISP_AAL);
	#else
	vIO32WriteFldAlign(DISP_AAL_1, 1, BYPASS_DISP_AAL);	
	#endif

    //inint XvYCC
    vOSTG_IO32WriteFldAlign(XVYCC_00, 0, REG_BYPASS_XV_IN2OUT);

    //gamma option
    vDrvOstgHWInit_LOADER();
    
    //Local diming
    vIO32WriteFldAlign(LD2_MODE_00, 1, C_LD_BYPASS);

	//temprally for verirication
//    vOSTG_IO32WriteFldAlign(DEMURA_CTRL_0, 1, C_QPQE_DISABLE);
//    vOSTG_IO32WriteFldAlign(DEMURA_CTRL_0, 0, C_DEMURA_BYPASS);
#ifdef CC_MTK_LOADER
    u1DrvMruaCUCInit();
#else
    if(IO32ReadFldAlign(DEMURA_CTRL_0, C_DEMURA_BYPASS) == 1)
    {
        u1DrvMruaCUCInit();
    }	
#endif    
    //force dither update in ISR.
    #ifndef CC_MTK_LOADER
	fgIsDitherInited = SV_FALSE;
	#endif
	
}

void u1DrvMruaCUCInit(void)
{
    // from MVI , if not support no need to init(HW default is disable)
    if(IsSupportMura()|| IsSupportCuc())
    {	
        // set Demura clk. 
        vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG, 0x2, FLD_QPQE_QCLK_SEL);
        vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG, 0x2, FLD_QPQE_ZCLK_SEL);
        vIO32WriteFldAlign(CKGEN_REG_SRE_OCLK_CKCFG, 0x0, FLD_QPQE_VOCLK_SEL);

        //sw rest
        vOSTG_IO32WriteFldAlign(DEMURA_CTRL_0, SV_ON, C_QPQE_SW_RST);
        vOSTG_IO32WriteFldAlign(DEMURA_CTRL_0, SV_OFF, C_QPQE_SW_RST);

        //enable Demura
        vOSTG_IO32WriteFldAlign(DEMURA_CTRL_0, SV_ON, C_QPQE_CLK_EN);
        vOSTG_IO32WriteFldAlign(DEMURA_CTRL_0, SV_OFF, C_DEMURA_BYPASS);	

        //enable QPQE
        vOSTG_IO32WriteFldAlign(DEMURA_CTRL_0, SV_OFF, C_QPQE_DISABLE);
        vIO32WriteFldAlign(0x1200F120, SV_ON, Fld(1, 0, AC_MSKB0));
        printf("ZDO support Mura Cuc\n");
    }
    else
    {
        printf("ZDO not initial Mura Cuc\n");
    }
}

//--------------------add pq module status for calculating osd shift ----------------------
/***************** TTD *******************/
//TTD removed

/***************** SRS *******************/
UINT8 u1DrvSRSIsBypassforOSD(void)
{
#ifdef CC_FBM_SUPPORT_SRS
    return (IO32ReadFldAlign(SRS_CTRL, C_SRS_BYPASS));
#else
    return (IO32ReadFldAlign(SUPERRES_00, SR_BYPASS_EN));
#endif
}

/***************** Sharpness *******************/
UINT8 u1DrvSharpnessIsBypassforOSD(void)
{
    return (IO32ReadFldAlign(TDPROC_MISC_00, TDPROC_BYPASS_ALL));
}
UINT8 u1DrvVerticalSharpBypassforOSD(void)
{
    return (IO32ReadFldAlign(TDPROC_MISC_00, NO_VERTICAL_SHARP));
}
/***************** CBE *******************/
UINT8 u1DrvCBEIsBypassforOSD(void)
{
    return (IO32ReadFldAlign(CBE_05, BYPASS_CBE));
}
/***************** Adaptiive Luma position *******************/
UINT8 u1DrvYContPositionforOSD(void)
{
	//1, parallel to Sharpness, 0, serial to Sharpness
	return	IO32ReadFldAlign(TDPROC_MISC_01, C_SHP_DSR_EN); 
}

/***************** BitPlus *******************/
UINT8 u1DrvBitPlusYIsBypassforOSD(void)
{
    return (IO32ReadFldAlign(BITPLUS_3, Y_BITPLUS_BYPASS));
}

UINT8 u1DrvBitPlusSIsBypassforOSD(void)
{
    return (IO32ReadFldAlign(BITPLUS_6, S_BITPLUS_BYPASS));
}
/***************** 3D colormapping *******************/
UINT8 u1Drv3DCMSIsBypassforOSD(void)
{
    return (IO32ReadFldAlign(COLOR_MAPPING_CONTROL, CM_BYPASS));
}
/*********************Blue Stretch ************************/
UINT8 u1DrvBSIsBypassforOSD(void)
{
    return (IO32ReadFldAlign(BLUESTCH_00, C_BS_BYPASS));
}

/********************* xvYCC ************************/
UINT8 u1DrvXvYCCIsBypassforOSD(void)
{
    return (IO32ReadFldAlign(XVYCC_00, REG_BYPASS_XV_IN2OUT));
}
/***************** gamma option *******************/
UINT8 u1DrvGammaPositionforOSD(void)
{
    return (IO32ReadFldAlign(OSTG_01, REG_OP_SEL));
}
/***************** local contrast*******************/
UINT8 u1DrvLCEIsBypassforOSD(void)
{
    return (IO32ReadFldAlign(DISP_AAL_1, BYPASS_DISP_AAL));
}

VOID vDrvAdjustOsdOffsetBaseOnPQModule(INT32 *pi4PosH, INT32 *pi4PosV)
{
    INT16 byp_delay_tab[OSD_BYP_MAX][2] =
    {
        { 59,  6}, //BYPASS_SHARPNESS      
        { 27,  1}, //BYPASS_CBE_CTL
        { 27,  1}, //OSD_YCONT_POSITION, Adaptive Luma parallel to Sharpness
        { 21,  0}, //BYPASS_BITPLUS_Y
        { 21,  0}, //BYPASS_BITPLUS_S
        { 4,   0}, //BYPASS_3DCOLORMAP        
        #ifdef CC_FBM_SUPPORT_SRS
        {-314, 11}, //BYPASS_SRS : input <=2k source{-0x420, -0x35} // {-311, 12}
        {-303, 1}, //BYPASS_SRS : input 4k source{-0x223, -0x2}
        #else
        {-52, -9},
        #endif		
        {  3,  6}, //BYPASS_VERTICAL_SHARP
        {-12,  0}, //BYPASS_XVYCC
        { 4,   0}, //bypass bluestretch OSD_BYP_BS
        { 11,  0}, //GAMMA_POS
        { -34,-2}, //local contrast enable
    };

    LOG(0,"[OSD]Before PQ Module offset:i4H=%d i4V=%d \n", *pi4PosH, *pi4PosV);
    if(u1DrvSharpnessIsBypassforOSD())
    {
        LOG(5,"[OSD]DBG u1DrvSharpnessIsBypass\n");
        *pi4PosH -= byp_delay_tab[OSD_BYP_SHARPNESS][0];
        *pi4PosV -= byp_delay_tab[OSD_BYP_SHARPNESS][1];
    }

    if(u1DrvVerticalSharpBypassforOSD())
    {
        LOG(5,"[OSD]DBG vertical sharpness bypass \n");
        *pi4PosH -= byp_delay_tab[OSD_BYP_VERTICALSHARP][0];
        *pi4PosV -= byp_delay_tab[OSD_BYP_VERTICALSHARP][1];
    }
    if(u1DrvCBEIsBypassforOSD())
    {
        LOG(5,"[OSD]DBG u1DrvCBEIsBypass\n");
        *pi4PosH -= byp_delay_tab[OSD_BYP_CBE_CTL][0];
        *pi4PosV -= byp_delay_tab[OSD_BYP_CBE_CTL][1];
    }
	if(u1DrvYContPositionforOSD())
	{
		LOG(5,"[OSD]DBG Adaptive Luma parallel to Sharpness\n");
        *pi4PosH -= byp_delay_tab[OSD_YCONT_POSITION][0];
        *pi4PosV -= byp_delay_tab[OSD_YCONT_POSITION][1];		
	}
    if (u1DrvBitPlusYIsBypassforOSD())
    {
        LOG(5,"[OSD]DBG u1DrvBitPlusYIsBypass\n");
        *pi4PosH -= byp_delay_tab[OSD_BYP_BITPLUS_Y][0];
        *pi4PosV -= byp_delay_tab[OSD_BYP_BITPLUS_Y][1];
    }

    if (u1DrvBitPlusSIsBypassforOSD())
    {
        LOG(5,"[OSD]DBG u1DrvBitPlusSIsBypass\n");
        *pi4PosH -= byp_delay_tab[OSD_BYP_BITPLUS_S][0];
        *pi4PosV -= byp_delay_tab[OSD_BYP_BITPLUS_S][1];
    }

    if (u1Drv3DCMSIsBypassforOSD())
    {
        LOG(5,"[OSD]DBG 3D colormapping bypass \n");
        *pi4PosH -= byp_delay_tab[OSD_BYP_3DCOLORMAP][0];
        *pi4PosV -= byp_delay_tab[OSD_BYP_3DCOLORMAP][1];
    }
    
    //load sync srs to mp branch , need adjust osd's position

    #if defined(CC_FBM_SUPPORT_SRS)
    if(!u1DrvSRSIsBypassforOSD() && !IS_SUPPORT_VB1_GRAPH_MODE)
    {
        LOG(5,"[OSD]DBG enable SRS\n");
        if(GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60_SRS)
        {
            LOG(5,"[OSD]DBG SRS PANEL_TYPE_4K2K_SRS \n");
            *pi4PosH -= byp_delay_tab[OSD_BYP_2K_SRS][0];
            *pi4PosV -= byp_delay_tab[OSD_BYP_2K_SRS][1];
        }
        else if(GetPANEL4K2K_TYPE() == PANEL_TYPE_4K2K_60)
        {
            LOG(5,"[OSD]DBG SRS PANEL_TYPE_4K2K_60 \n");
            *pi4PosH -= byp_delay_tab[OSD_BYP_4K_SRS][0];
            *pi4PosV -= byp_delay_tab[OSD_BYP_4K_SRS][1];
        }
    }
	#else
	if(!u1DrvSRSIsBypassforOSD())
	{
		LOG(5,"[OSD]DBG SRS INT\n");
		*pi4PosH -= byp_delay_tab[OSD_BYP_SRS_INT][0];
		*pi4PosV -= byp_delay_tab[OSD_BYP_SRS_INT][1];
	} 
    #endif
    
    if (!u1DrvXvYCCIsBypassforOSD())
    {
        LOG(5,"[OSD]DBG XvYCC bypass \n");
        *pi4PosH -= byp_delay_tab[OSD_BYP_XVYCC][0];
        *pi4PosV -= byp_delay_tab[OSD_BYP_XVYCC][1];
    }
	if(u1DrvBSIsBypassforOSD())
	{
        LOG(5,"[OSD]DBG Blue Stretch bypass \n");		
        *pi4PosH -= byp_delay_tab[OSD_BYP_BS][0];
        *pi4PosV -= byp_delay_tab[OSD_BYP_BS][1];
    }
    switch(u1DrvGammaPositionforOSD())
    {
        case 0:
        case 1:
            LOG(5,"[OSD]DBG Gamma position 0 or 1 \n");
            //*pi4PosH -= byp_delay_tab[OSD_BYPASS_GAMMAPOS][0];
            //*pi4PosV -= byp_delay_tab[OSD_BYPASS_GAMMAPOS][1];
            break;
        case 2:
        case 3:
            LOG(5,"[OSD]DBG Gamma position 2 or 3 \n");
            *pi4PosH -= byp_delay_tab[OSD_BYP_GAMMAPOS][0];
            *pi4PosV -= byp_delay_tab[OSD_BYP_GAMMAPOS][1];
            break;
        default:
            break;
    }
    
    #ifdef SMALL_OUTWINDOW_TTD_NEWSETTING
    LOG(5,"[OSD]DBG defined SMALL_OUTWINDOW_TTD_NEWSETTING\n");
    *pi4PosH -= 155;    //for TTD basic offset
    #endif
    if(!u1DrvLCEIsBypassforOSD())
    {
    	LOG(5,"[OSD]DBG enable LCE\n");
		*pi4PosH -= byp_delay_tab[OSD_BYP_LCE][0];
        *pi4PosV -= byp_delay_tab[OSD_BYP_LCE][1];
    }
    LOG(0,"[OSD]After PQ Module offset:i4H=%d i4V=%d \n", *pi4PosH,*pi4PosV);
    
}

static UINT32 vDrvGetOsdDisplayWidth(VOID)
{
    if (fgIsOSDFromGFX())
    {
        #if defined(SUPPORT_PANEL_GRAPH_MODE)
        UINT32 u4Width, u4Height;
        u4Width = GRAPHMODE_GetPanelWidth();
        u4Height = GRAPHMODE_GetPanelHeight();

        if ((u4Width == 3840) && (u4Height == 1080))
        {
            u4Width = u4Width / 2;
        }

        return u4Width;
        #endif
    }

    return PANEL_GetPanelWidth();
}

static UINT32 vDrvGetOsdDisplayHeight(VOID)
{
    if (fgIsOSDFromGFX())
    {
        #if defined(SUPPORT_PANEL_GRAPH_MODE)
        return GRAPHMODE_GetPanelHeight();
        #endif
    }

    return PANEL_GetPanelHeight();
}

static BOOL bIsOSDTimingSrc4K2K(VOID)
{
    if (vScpipGetOsdTgSrcSel() == 0x2 && vDrvGetOsdDisplayWidth() == 3840 &&
            vDrvGetOsdDisplayHeight() == 2160)
    {
        return TRUE;
    }

    return FALSE;
}

static BOOL bIsOSDTimingSrcFHD(VOID)
{
    if (vScpipGetOsdTgSrcSel() == 0x2 && vDrvGetOsdDisplayWidth() == 1920 &&
            vDrvGetOsdDisplayHeight() == 1080)
    {
        return TRUE;
    }

    return FALSE;
}

#ifndef USERDRV_KERNEL_MOD
VOID vDrvGetOsdStartPosition(OSD_START_POS_T *pPosition)
{
    INT32 i4PosH , i4PosV;

    if (bIsOSDTimingSrcFHD())
    {
        i4PosH = 0x9F;
        i4PosV = 0x7;
    }
    else if (bIsOSDTimingSrc4K2K())
    {
        i4PosH = 0x20B;
        i4PosV = 0xC;
    }
    else if (IS_DISP_4K2K_TYPE5)    //CC_SUPPORT_UHD
    {
        i4PosH = 0x1b2; //0x2b9;  //UHD_PIP_adj
        i4PosV = 0x10;//0x4;
    }
    else
    {
        i4PosH = 0xBE;
        i4PosV = -2;
    }
	
    #if defined(CC_FBM_SUPPORT_SRS)
	if(!IO32ReadFldAlign(SRS_CTRL,C_SRS_BYPASS) && !IS_SUPPORT_VB1_GRAPH_MODE)
	{
	    if(vScpipGetOsdTgSrcSel() == 0x2)//osd timing from gfx path
    	{
		    i4PosH = 0x249 + lcdtimingInfo.u4HBP + lcdtimingInfo.u4HSyncWidth;
		    i4PosV = 0x6 + lcdtimingInfo.u4VBP + lcdtimingInfo.u4VSyncWidth;
    	}
		else//osd timing from video path, and default is DE mode
		{
			i4PosH = 0x249;
			i4PosV = 0x6;	
		}
    }
    #endif
    vDrvAdjustOsdOffsetBaseOnPQModule(&i4PosH, &i4PosV);

    pPosition->i4Hstart = i4PosH;
    pPosition->i4Vstart = i4PosV;
}
#endif

void DRV_GetCustDCLK(UINT8  *pu1CustDCLKType,UINT32 *pu4CustFrameRate)
{
    if(su4CustDCLKType == SV_DCLK_60HZ)
    {
        if (PANEL_IsSupport240Hz())
        {
            su4CustFR = 240;
        }
        else if (PANEL_IsSupport120Hz())
        {
            su4CustFR = 120;
        }
        else
        {
            su4CustFR = 60;
        }
    }
    else
    {
        if (PANEL_IsSupport240Hz())
        {
            su4CustFR = 200;
        }
        else if (PANEL_IsSupport120Hz())
        {
            su4CustFR = 100;
        }
        else
        {
            su4CustFR = 50;
        }
    }

    *pu1CustDCLKType = su4CustDCLKType;
    *pu4CustFrameRate = su4CustFR;
}

UINT8 u1DrvGetSRSDynEn()
{
    return u1SRSDynSwitchEn;
}

#ifndef CC_MTK_LOADER

UINT32 u4DrvGetPanelFrameRateType(UINT32 u4VdoFrameRate)
{
    UINT32 u4DCLKType;
    switch (u4VdoFrameRate)
    {
        case 25:
        case 50:
        case 100:
            u4DCLKType = (fgDrvIsSupport50To60() || fgIsSupportFixMode() || fgIsSupportFlickerLessMode()) ? SV_DCLK_60HZ : SV_DCLK_50HZ;
            break;
        case 30:
        case 60:
        case 120:
            u4DCLKType = fgDrvIsSupport60To50() ? SV_DCLK_50HZ : SV_DCLK_60HZ;
            break;
        case 24:
            u4DCLKType = ( fgDrvIsSupport48() && !(fgIsSupportFixMode() || fgIsSupportFlickerLessMode()) && !fgDrvIsSupport3D60Output() ) ? SV_DCLK_48HZ : SV_DCLK_60HZ;
            break;
        case 48:
            if (SUPPORT_PANEL_48HZ)
            {
                if(fgIsSupportFixMode() || fgIsSupportFlickerLessMode())
                {
                    u4DCLKType = SV_DCLK_60HZ;
                }
                else
                {
                    u4DCLKType = SV_DCLK_48HZ;
                }
            }
            else if (IS_DISP_48HZ_TO_50HZ)
            {
                u4DCLKType = SV_DCLK_50HZ;
            }
            else
            {
                u4DCLKType = SV_DCLK_60HZ;
            }

            if(fgDrvIsSupport3D60Output())
            {
                u4DCLKType = SV_DCLK_60HZ;
            }
            break;
        case 20:
            #ifdef SUPPORT_20TO60
                u4DCLKType = SV_DCLK_60HZ;
            #else
                u4DCLKType = SV_DCLK_50HZ;
            #endif
            break;
        case 12:
        case 15:
            u4DCLKType = SV_DCLK_60HZ;
            break;
#ifdef SUPPORT_CUSTOMER_FRAME_RATE_SPEC
        case 8:
            u4DCLKType = (fgDrvIsSupport48() && !(fgIsSupportFixMode() || fgIsSupportFlickerLessMode())) ? SV_DCLK_48HZ : SV_DCLK_60HZ;
        break;
#endif
        default:
        #ifdef SUPPORT_CUSTOMER_FRAME_RATE_SPEC
            u4DCLKType = SV_DCLK_60HZ;
        #else
            if ((u4VdoFrameRate < 27) ||
                (u4VdoFrameRate > 45 && u4VdoFrameRate < 55) ||
                (u4VdoFrameRate > 90 && u4VdoFrameRate < 110))
            {
                u4DCLKType = (fgDrvIsSupport50To60() || fgIsSupportFixMode() || fgIsSupportFlickerLessMode()) ? SV_DCLK_60HZ : SV_DCLK_50HZ;
            }
            else
            {
                u4DCLKType = fgDrvIsSupport60To50() ? SV_DCLK_50HZ : SV_DCLK_60HZ;
            }
        #endif
            break;
    }
#ifdef SUPPORT_CUSTOMER_FRAME_RATE_SPEC
    if(fgApiVideoIsVgaTiming(SV_VP_MAIN) && wDrvVideoInputWidth(SV_VP_MAIN) != 4096)
    {
    u4DCLKType = SV_DCLK_60HZ;
    }
#endif

    if  (u4VdoFrameRate == 24 || u4VdoFrameRate == 25 || u4VdoFrameRate == 30)
    {
        if (DRVCUST_PanelGet(ePanel242530pOutput) == 1)
        {
            u4DCLKType = SV_DCLK_OTHERS;
        }
    }
    return u4DCLKType;
}
#endif

UINT8 u1DrvIsPnl4k2k60WithMemc(void)
{
    if(PANEL_Is4K2K60() && !IS_DISPLAY_FREERUN && !IS_DISP_4K2K_TYPE3)
    {
        return SV_TRUE;
    }
    else
    {
        return SV_FALSE;
    }
}

UINT32 vDrvCalBypPanelType(void)
{
    UINT32 u4PnlType = PANEL_TYPE_MAX;
    if(IS_DISP_4K2K_TYPE3)
    {
        u4PnlType = PANEL_TYPE_4K2K_60_BYP;
    }
    return u4PnlType;
}

BOOL fgFrameTrackIsRefTrk(void)
{
    if (IS_REF_FRAME_TRACK(_u4FrameTrackMode))
        return TRUE;
    else
        return FALSE;
}

LINT_RESTORE

