/**
 * @file hv_cal_panel.c
 * @brief Init Panel of Project.
 *
 * @verbatim
 * ==============================================================================
 *                        ##### How to use #####
 * ==============================================================================
 *
 *
 @ endverbatim
 *
 * @author HiView SoC Software Team
 * @version 1.0.0
 * @date 2024-01-29
 */
#include "hv_panel_Param.h"

/*Panel data  load from flash*/
static PanelDataDBRegion s_stPanelDataDBRegion = {0};

/*Dynamic pannel param config data*/
#define PANEL_DATA_VERSION 2
const static PanelDataDBRegion s_stDefaultPanelDataDBRegion = 
{
    .uiMagicNumber                  = DATABASE_VALID_MAGIC_NUMBER,
    .usVersion                      = PANEL_DATA_VERSION,
    .ucPanelNumber                  = 5,
    .stPanelParam =
    {
        {   /*index 0*/
            .usPanelHtotalType      = 2720,
            .usPanelHtotalMax       = 2760,
            .usPanelHtotalMin       = 2720,
            .usPanelVtotalType      = 1481,
            .usPanelVtotalMax       = 5320,
            .usPanelVtotalMin       = 1465,
            .usPanelHactive         = 2560,
            .usPanelVactive         = 1440,
            .usPanelFrameRateType   = 60,
            .usPanelFrameRateMax    = 165,
            .usPanelFrameRateMin    = 48,
            .ucPanelHsyncWidth      = 16,
            .ucPanelVsyncWidth      = 4,
            .ucPanelHsyncBackPorch  = 150,
            .ucPanelVsyncBackPorch  = 32,
            .ucPanelColorDepth      = 8,
            .ucPanelLaneNum         = 4,
            .ucPanelPortNum         = 1,
            .ucPanelLaneRate        = 20,
            .uiPanelPixelClockType  = 246740,
            .uiPanelPixelClockMax   = 678540,
            .uiPanelPixelClockMin   = 191270,
            .usPanelPowerOnTime     = 800,
            .usPanelDataOnTime      = 200,
            .usPanelBlkOnTime       = 100,
            .usPanelBlkOffTime      = 5,
            .usPanelDataOfTime      = 10,
            .usPanelPowerOffTime    = 10,
            .usPanelPllLockTime     = 210,
            .bPanelEdpAux0PnSwapEn  = 0,
            .bPanelEdpAux1PnSwapEn  = 0,
            .bPanelPhy0MuxEn        = 0,
            .ucPanelPhy0Lane0MuxSel = 0,
            .ucPanelPhy0Lane1MuxSel = 1,
            .ucPanelPhy0Lane2MuxSel = 2,
            .ucPanelPhy0Lane3MuxSel = 3,
            .bPanelPhy0Lane0PnSwap  = 1,
            .bPanelPhy0Lane1PnSwap  = 1,
            .bPanelPhy0Lane2PnSwap  = 1,
            .bPanelPhy0Lane3PnSwap  = 1,
            .bPanelPhy1MuxEn        = 1,
            .ucPanelPhy1Lane0MuxSel = 0,
            .ucPanelPhy1Lane1MuxSel = 1,
            .ucPanelPhy1Lane2MuxSel = 2,
            .ucPanelPhy1Lane3MuxSel = 3,
            .bPanelPhy1Lane0PnSwap  = 1,
            .bPanelPhy1Lane1PnSwap  = 1,
            .bPanelPhy1Lane2PnSwap  = 1,
            .bPanelPhy1Lane3PnSwap  = 1,
            .bPanelPhy2MuxEn        = 0,
            .ucPanelPhy2Lane0MuxSel = 0,
            .ucPanelPhy2Lane1MuxSel = 1,
            .ucPanelPhy2Lane2MuxSel = 2,
            .ucPanelPhy2Lane3MuxSel = 3,
            .bPanelPhy2Lane0PnSwap  = 1,
            .bPanelPhy2Lane1PnSwap  = 1,
            .bPanelPhy2Lane2PnSwap  = 1,
            .bPanelPhy2Lane3PnSwap  = 1,
        },
        {   /*index 1*/
            .usPanelHtotalType      = 2700,
            .usPanelHtotalMax       = 2760,
            .usPanelHtotalMin       = 2640,
            .usPanelVtotalType      = 1480,
            .usPanelVtotalMax       = 5535,
            .usPanelVtotalMin       = 1470,
            .usPanelHactive         = 2560,
            .usPanelVactive         = 1440,
            .usPanelFrameRateType   = 60,
            .usPanelFrameRateMax    = 180,
            .usPanelFrameRateMin    = 48,
            .ucPanelHsyncWidth      = 16,
            .ucPanelVsyncWidth      = 4,
            .ucPanelHsyncBackPorch  = 126,
            .ucPanelVsyncBackPorch  = 32,
            .ucPanelColorDepth      = 8,
            .ucPanelLaneNum         = 4,
            .ucPanelPortNum         = 1,
            .ucPanelLaneRate        = 20,
            .uiPanelPixelClockType  = 239760,
            .uiPanelPixelClockMax   = 733290,
            .uiPanelPixelClockMin   = 186910,
            .usPanelPowerOnTime     = 800,
            .usPanelDataOnTime      = 200,
            .usPanelBlkOnTime       = 100,
            .usPanelBlkOffTime      = 5,
            .usPanelDataOfTime      = 10,
            .usPanelPowerOffTime    = 10,
            .usPanelPllLockTime     = 210,
            .bPanelEdpAux0PnSwapEn  = 0,
            .bPanelEdpAux1PnSwapEn  = 0,
            .bPanelPhy0MuxEn        = 0,
            .ucPanelPhy0Lane0MuxSel = 0,
            .ucPanelPhy0Lane1MuxSel = 1,
            .ucPanelPhy0Lane2MuxSel = 2,
            .ucPanelPhy0Lane3MuxSel = 3,
            .bPanelPhy0Lane0PnSwap  = 1,
            .bPanelPhy0Lane1PnSwap  = 1,
            .bPanelPhy0Lane2PnSwap  = 1,
            .bPanelPhy0Lane3PnSwap  = 1,
            .bPanelPhy1MuxEn        = 1,
            .ucPanelPhy1Lane0MuxSel = 0,
            .ucPanelPhy1Lane1MuxSel = 1,
            .ucPanelPhy1Lane2MuxSel = 2,
            .ucPanelPhy1Lane3MuxSel = 3,
            .bPanelPhy1Lane0PnSwap  = 1,
            .bPanelPhy1Lane1PnSwap  = 1,
            .bPanelPhy1Lane2PnSwap  = 1,
            .bPanelPhy1Lane3PnSwap  = 1,
            .bPanelPhy2MuxEn        = 0,
            .ucPanelPhy2Lane0MuxSel = 0,
            .ucPanelPhy2Lane1MuxSel = 1,
            .ucPanelPhy2Lane2MuxSel = 2,
            .ucPanelPhy2Lane3MuxSel = 3,
            .bPanelPhy2Lane0PnSwap  = 1,
            .bPanelPhy2Lane1PnSwap  = 1,
            .bPanelPhy2Lane2PnSwap  = 1,
            .bPanelPhy2Lane3PnSwap  = 1,
        },
        {
            /*index 2*/
            .usPanelHtotalType      = 4000,
            .usPanelHtotalMax       = 4088,
            .usPanelHtotalMin       = 3950,
            .usPanelVtotalType      = 2222,
            .usPanelVtotalMax       = 2290,
            .usPanelVtotalMin       = 2213,
            .usPanelHactive         = 3840,
            .usPanelVactive         = 2160,
            .usPanelFrameRateType   = 60,
            .usPanelFrameRateMax    = 62,
            .usPanelFrameRateMin    = 50,
            .ucPanelHsyncWidth      = 16,
            .ucPanelVsyncWidth      = 4,
            .ucPanelHsyncBackPorch  = 50,
            .ucPanelVsyncBackPorch  = 16,
            .ucPanelColorDepth      = 8,
            .ucPanelLaneNum         = 4,
            .ucPanelPortNum         = 1,
            .ucPanelLaneRate        = 20,
            .uiPanelPixelClockType  = 526310,
            .uiPanelPixelClockMax   = 555550,
            .uiPanelPixelClockMin   = 454540,
            .usPanelPowerOnTime     = 800,
            .usPanelDataOnTime      = 200,
            .usPanelBlkOnTime       = 100,
            .usPanelBlkOffTime      = 5,
            .usPanelDataOfTime      = 10,
            .usPanelPowerOffTime    = 10,
            .usPanelPllLockTime     = 250,
            .bPanelEdpAux0PnSwapEn  = 0,
            .bPanelEdpAux1PnSwapEn  = 0,
            .bPanelPhy0MuxEn        = 0,
            .ucPanelPhy0Lane0MuxSel = 0,
            .ucPanelPhy0Lane1MuxSel = 1,
            .ucPanelPhy0Lane2MuxSel = 2,
            .ucPanelPhy0Lane3MuxSel = 3,
            .bPanelPhy0Lane0PnSwap  = 1,
            .bPanelPhy0Lane1PnSwap  = 1,
            .bPanelPhy0Lane2PnSwap  = 1,
            .bPanelPhy0Lane3PnSwap  = 1,
            .bPanelPhy1MuxEn        = 1,
            .ucPanelPhy1Lane0MuxSel = 0,
            .ucPanelPhy1Lane1MuxSel = 1,
            .ucPanelPhy1Lane2MuxSel = 2,
            .ucPanelPhy1Lane3MuxSel = 3,
            .bPanelPhy1Lane0PnSwap  = 1,
            .bPanelPhy1Lane1PnSwap  = 1,
            .bPanelPhy1Lane2PnSwap  = 1,
            .bPanelPhy1Lane3PnSwap  = 1,
            .bPanelPhy2MuxEn        = 0,
            .ucPanelPhy2Lane0MuxSel = 0,
            .ucPanelPhy2Lane1MuxSel = 1,
            .ucPanelPhy2Lane2MuxSel = 2,
            .ucPanelPhy2Lane3MuxSel = 3,
            .bPanelPhy2Lane0PnSwap  = 1,
            .bPanelPhy2Lane1PnSwap  = 1,
            .bPanelPhy2Lane2PnSwap  = 1,
            .bPanelPhy2Lane3PnSwap  = 1,
        },
        {
            /*index 3*/
            .usPanelHtotalType      = 4400,
            .usPanelHtotalMax       = 4400,
            .usPanelHtotalMin       = 4400,
            .usPanelVtotalType      = 2250,
            .usPanelVtotalMax       = 2250,
            .usPanelVtotalMin       = 2250,
            .usPanelHactive         = 3840,
            .usPanelVactive         = 2160,
            .usPanelFrameRateType   = 60,
            .usPanelFrameRateMax    = 75,
            .usPanelFrameRateMin    = 40,
            .ucPanelHsyncWidth      = 8,
            .ucPanelVsyncWidth      = 2,
            .ucPanelHsyncBackPorch  = 73,
            .ucPanelVsyncBackPorch  = 16,
            .ucPanelColorDepth      = 10,
            .ucPanelLaneNum         = 4,
            .ucPanelPortNum         = 2,
            .ucPanelLaneRate        = 20,
            .uiPanelPixelClockType  = 594000,
            .uiPanelPixelClockMax   = 594000,
            .uiPanelPixelClockMin   = 594000,
            .usPanelPowerOnTime     = 800,
            .usPanelDataOnTime      = 200,
            .usPanelBlkOnTime       = 100,
            .usPanelBlkOffTime      = 5,
            .usPanelDataOfTime      = 10,
            .usPanelPowerOffTime    = 10,
            .usPanelPllLockTime     = 210,
            .bPanelEdpAux0PnSwapEn  = 0,
            .bPanelEdpAux1PnSwapEn  = 0,
            .bPanelPhy0MuxEn        = 1,
            .ucPanelPhy0Lane0MuxSel = 4,
            .ucPanelPhy0Lane1MuxSel = 5,
            .ucPanelPhy0Lane2MuxSel = 6,
            .ucPanelPhy0Lane3MuxSel = 7,
            .bPanelPhy0Lane0PnSwap  = 0,
            .bPanelPhy0Lane1PnSwap  = 0,
            .bPanelPhy0Lane2PnSwap  = 0,
            .bPanelPhy0Lane3PnSwap  = 0,
            .bPanelPhy1MuxEn        = 1,
            .ucPanelPhy1Lane0MuxSel = 0,
            .ucPanelPhy1Lane1MuxSel = 1,
            .ucPanelPhy1Lane2MuxSel = 2,
            .ucPanelPhy1Lane3MuxSel = 3,
            .bPanelPhy1Lane0PnSwap  = 0,
            .bPanelPhy1Lane1PnSwap  = 0,
            .bPanelPhy1Lane2PnSwap  = 0,
            .bPanelPhy1Lane3PnSwap  = 0,
            .bPanelPhy2MuxEn        = 1,
            .ucPanelPhy2Lane0MuxSel = 0,
            .ucPanelPhy2Lane1MuxSel = 1,
            .ucPanelPhy2Lane2MuxSel = 2,
            .ucPanelPhy2Lane3MuxSel = 3,
            .bPanelPhy2Lane0PnSwap  = 0,
            .bPanelPhy2Lane1PnSwap  = 0,
            .bPanelPhy2Lane2PnSwap  = 0,
            .bPanelPhy2Lane3PnSwap  = 0,
        },
        {
            /*index 4*/
            .usPanelHtotalType      = 4400,
            .usPanelHtotalMax       = 4800,
            .usPanelHtotalMin       = 4240,
            .usPanelVtotalType      = 2250,
            .usPanelVtotalMax       = 2260,
            .usPanelVtotalMin       = 2200,
            .usPanelHactive         = 3840,
            .usPanelVactive         = 2160,
            .usPanelFrameRateType   = 60,
            .usPanelFrameRateMax    = 63,
            .usPanelFrameRateMin    = 45,
            .ucPanelHsyncWidth      = 8,
            .ucPanelVsyncWidth      = 2,
            .ucPanelHsyncBackPorch  = 73,
            .ucPanelVsyncBackPorch  = 16,
            .ucPanelColorDepth      = 10,
            .ucPanelLaneNum         = 8,
            .ucPanelPortNum         = 1,
            .ucPanelLaneRate        = 20,
            .uiPanelPixelClockType  = 594000,
            .uiPanelPixelClockMax   = 616000,
            .uiPanelPixelClockMin   = 528000,
            .usPanelPowerOnTime     = 800,
            .usPanelDataOnTime      = 200,
            .usPanelBlkOnTime       = 100,
            .usPanelBlkOffTime      = 5,
            .usPanelDataOfTime      = 10,
            .usPanelPowerOffTime    = 10,
            .usPanelPllLockTime     = 210,
            .bPanelEdpAux0PnSwapEn  = 0,
            .bPanelEdpAux1PnSwapEn  = 0,
            .bPanelPhy0MuxEn        = 1,
            .ucPanelPhy0Lane0MuxSel = 4,
            .ucPanelPhy0Lane1MuxSel = 5,
            .ucPanelPhy0Lane2MuxSel = 6,
            .ucPanelPhy0Lane3MuxSel = 7,
            .bPanelPhy0Lane0PnSwap  = 0,
            .bPanelPhy0Lane1PnSwap  = 0,
            .bPanelPhy0Lane2PnSwap  = 0,
            .bPanelPhy0Lane3PnSwap  = 0,
            .bPanelPhy1MuxEn        = 1,
            .ucPanelPhy1Lane0MuxSel = 0,
            .ucPanelPhy1Lane1MuxSel = 1,
            .ucPanelPhy1Lane2MuxSel = 2,
            .ucPanelPhy1Lane3MuxSel = 3,
            .bPanelPhy1Lane0PnSwap  = 0,
            .bPanelPhy1Lane1PnSwap  = 0,
            .bPanelPhy1Lane2PnSwap  = 0,
            .bPanelPhy1Lane3PnSwap  = 0,
            .bPanelPhy2MuxEn        = 0,
            .ucPanelPhy2Lane0MuxSel = 0,
            .ucPanelPhy2Lane1MuxSel = 1,
            .ucPanelPhy2Lane2MuxSel = 2,
            .ucPanelPhy2Lane3MuxSel = 3,
            .bPanelPhy2Lane0PnSwap  = 0,
            .bPanelPhy2Lane1PnSwap  = 0,
            .bPanelPhy2Lane2PnSwap  = 0,
            .bPanelPhy2Lane3PnSwap  = 0,
        },
    },
    .bModifyFlag                    = HV_FALSE,
};

VOID Hv_Panel_UpdaeDefaultDynamicPanlParam()
{
    FactoryDataDBRegion* pstFactoryDataDBRegion = Hv_Common_Db_GetFactoryDataDBRegionPtr();
    UserDataDBRegion* pstUserDataDBRegion = Hv_Common_Db_GetUserDataDBRegionPtr();
    DiagDataDBRegion* pstDiagDataDBRegion = Hv_Common_Db_GetDiagDataDBRegionPtr();

    if (pstFactoryDataDBRegion->bDynamicPanelEn)
    {
        pstDiagDataDBRegion->stDisplayDebug.usMaxMainFrameBufferHsize = s_stPanelDataDBRegion.stPanelParam[pstFactoryDataDBRegion->ucDynamicPanelIndex].usPanelHactive;
        pstDiagDataDBRegion->stDisplayDebug.usMaxMainFrameBufferVsize = s_stPanelDataDBRegion.stPanelParam[pstFactoryDataDBRegion->ucDynamicPanelIndex].usPanelVactive;
        pstDiagDataDBRegion->stDisplayDebug.usMaxPipFrameBufferHsize = s_stPanelDataDBRegion.stPanelParam[pstFactoryDataDBRegion->ucDynamicPanelIndex].usPanelHactive / 2;
        pstDiagDataDBRegion->stDisplayDebug.usMaxPipFrameBufferVsize = s_stPanelDataDBRegion.stPanelParam[pstFactoryDataDBRegion->ucDynamicPanelIndex].usPanelVactive / 2;
#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_MAGNIFY)
        pstUserDataDBRegion->usMagnifyGlassPositionH = s_stPanelDataDBRegion.stPanelParam[pstFactoryDataDBRegion->ucDynamicPanelIndex].usPanelHactive / 2;
        pstUserDataDBRegion->usMagnifyGlassPositionV = s_stPanelDataDBRegion.stPanelParam[pstFactoryDataDBRegion->ucDynamicPanelIndex].usPanelVactive / 2;
        pstUserDataDBRegion->bModifyFlag = 1;
#endif
#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_PXP)
        pstUserDataDBRegion->usPipSmallWindowHSize = s_stPanelDataDBRegion.stPanelParam[pstFactoryDataDBRegion->ucDynamicPanelIndex].usPanelHactive * 2 / 5;
        pstUserDataDBRegion->usPipSmallWindowVSize = s_stPanelDataDBRegion.stPanelParam[pstFactoryDataDBRegion->ucDynamicPanelIndex].usPanelVactive * 2 / 5;
        pstUserDataDBRegion->usPipBigWindowHSize = s_stPanelDataDBRegion.stPanelParam[pstFactoryDataDBRegion->ucDynamicPanelIndex].usPanelHactive / 2;
        pstUserDataDBRegion->usPipBigWindowVSize = s_stPanelDataDBRegion.stPanelParam[pstFactoryDataDBRegion->ucDynamicPanelIndex].usPanelVactive / 2;
        pstUserDataDBRegion->bModifyFlag = 1;
#endif
    }
    return;
}

PanelDataDBRegion* Hv_Panel_GetPanelDataDBRegionPtr(VOID)
{
    return &s_stPanelDataDBRegion;
}

VOID Hv_Panel_InitPanelParam(VOID)
{
    BOOL bDataBaseValid = HV_FALSE;
    Hv_Mw_DataBase_MonitorData_Load(PANEL_DATA_START_ADDRESS, (UCHAR8*)&s_stPanelDataDBRegion, sizeof(PanelDataDBRegion));

    /*check panel data valid*/
    if (DATABASE_VALID_MAGIC_NUMBER == s_stPanelDataDBRegion.uiMagicNumber)
    {
        bDataBaseValid = HV_TRUE;
        if (s_stDefaultPanelDataDBRegion.usVersion > s_stPanelDataDBRegion.usVersion)
        {
            bDataBaseValid = HV_FALSE;
        }
    }
    else
    {
        bDataBaseValid = HV_FALSE;
    }
    if (HV_FALSE == bDataBaseValid)
    {
        /*copy default data to global variables*/
        HV_MEMCPY(&s_stPanelDataDBRegion, &s_stDefaultPanelDataDBRegion, sizeof(s_stPanelDataDBRegion));
        strncpy(s_stPanelDataDBRegion.stPanelParam[0].ucPanelName, "HKC_EDP_SN270ES01_QHD165", HV_PANEL_NAME_BYTE_MAX -1);
        strncpy(s_stPanelDataDBRegion.stPanelParam[1].ucPanelName, "HKC_EDP_SN270ES01_QHD180", HV_PANEL_NAME_BYTE_MAX -1);
        strncpy(s_stPanelDataDBRegion.stPanelParam[2].ucPanelName, "BOE_EDP_MV270QUM_N60_UHD60", HV_PANEL_NAME_BYTE_MAX -1);
        strncpy(s_stPanelDataDBRegion.stPanelParam[3].ucPanelName, "HIVIEW_VBO_SILICON65VBO_UHD60", HV_PANEL_NAME_BYTE_MAX-1);
        strncpy(s_stPanelDataDBRegion.stPanelParam[4].ucPanelName, "AUO_VBO_G270ZAN01_1_UHD60", HV_PANEL_NAME_BYTE_MAX-1);

        /*设置modify标志后，databsesync定时器超时会自动将UserDataDBRegion,FactoryDataDBRegion,SystemDataDBRegion数据反写到flash中
        这样反写flash不会占用系统启动时间!!!*/
        Hv_Panel_GetPanelDataDBRegionPtr()->bModifyFlag = 1;
        Hv_Panel_UpdaeDefaultDynamicPanlParam();
    }
    return;
}

