/**
 * @file hv_drv_EdpTx.c
 * @brief EdpTx module driver
 * @details This module provide interfaces for Hiview-Madia-Ware.
 * @author HiView SoC Software Team
 * @version 0.0.1
 * @date 20221013
 * @copyright Copyright(c),2022-9, Hiview Software. All rights reserved.
 * @par History:
 * <table>
 * <tr><th>Author                         <th>Date             <th>Details
 * <tr><td>HiView SoC Software Team   <td>20221013       <td>Create and first Version.
 * </table>
 **/
#include "hv_comm_Assert.h"
#include "hv_comm_Log.h"
#include "hv_comm_Define.h"
#include "hv_vos_Comm.h"
#include "hv_chip_Irq.h"

#include "hv_drv_EdpTx.h"
#include "hv_cal_EdpTx.h"
#include "hv_drv_Ktx.h"
#include "hv_cal_Ktx.h"
#include "Common/hv_comm_DataType.h"
#include "hv_drv_EdpTxTraining.h"
#include "hv_cal_EdpTxCommon.h"
#include "hv_cal_KtxCommon.h"
#include "PanelConfig.h"
#include "hv_cal_EdpTxTraining.h"

static HvEdpTxTrainingInputParam s_stTrainingInputParam = {0};
static UCHAR8 s_ucEdpTxTrainingResutl = 0;
static Status EdpTxTopCfgGen(const PanelParam *pstPanelParam, HvDrvKtxInParam *pstKtxInParam, HvEdpTxTopCfgParam *pstEdpTxTopCfgParam)
{
    UINT64 uiEdpVal;
    pstEdpTxTopCfgParam->ucEdpTxLaneNum = (pstKtxInParam->usKtxInPairNum * pstKtxInParam->usKtxInPortNum);
    if (pstKtxInParam->usKtxInColorDepth == 6)
    {
        pstEdpTxTopCfgParam->ucEdpTxBitMode = 1;
        pstEdpTxTopCfgParam->ucEdpTxMisc0 = 0;
    }
    else if (pstKtxInParam->usKtxInColorDepth == 10)
    {
        pstEdpTxTopCfgParam->ucEdpTxBitMode = 2;
        pstEdpTxTopCfgParam->ucEdpTxMisc0 = 0x40;
    }
    else
    {
        pstEdpTxTopCfgParam->ucEdpTxBitMode = 0;
        pstEdpTxTopCfgParam->ucEdpTxMisc0 = 0x20;
    }

    pstEdpTxTopCfgParam->uiEdpTxDataSymbolNum = ((pstKtxInParam->usKtxInHact * 3 * pstKtxInParam->usKtxInColorDepth) \
                                                / pstEdpTxTopCfgParam->ucEdpTxLaneNum) >> 3;
    
    /*uiEdpTxMvid = 0x8000 * (UINT64) pstKtxInParam->uiKtxInPixClkFreq / (pstKtxInParam->uiKtxInDptxFreq / 10);*/
    uiEdpVal = ((UINT64)pstKtxInParam->uiKtxInPixClkFreq << 15)  / pstKtxInParam->uiKtxInDptxFreq * 10;
    pstEdpTxTopCfgParam->uiEdpTxMvid = uiEdpVal / pstKtxInParam->usKtxInPortNum;

    pstEdpTxTopCfgParam->uiEdpTxNvid = 32768;
    pstEdpTxTopCfgParam->uiEdpTxMsaHTotal = pstKtxInParam->usKtxInHTotal / pstKtxInParam->usKtxInPortNum;
    pstEdpTxTopCfgParam->uiEdpTxMsaVTotal = pstKtxInParam->usKtxInVTotal;
    pstEdpTxTopCfgParam->usEdpTxMasHact = pstKtxInParam->usKtxInHact / pstKtxInParam->usKtxInPortNum;
    pstEdpTxTopCfgParam->usEdpTxMasVact = pstKtxInParam->usKtxInVact;
    pstEdpTxTopCfgParam->usEdpTxMasHbPorch = pstPanelParam->ucPanelHsyncBackPorch;
    pstEdpTxTopCfgParam->usEdpTxMasVbPorch = pstPanelParam->ucPanelVsyncBackPorch;
    pstEdpTxTopCfgParam->usEdpTxMasHsWidth = pstPanelParam->ucPanelHsyncWidth;
    pstEdpTxTopCfgParam->usEdpTxMasVsWidth = pstPanelParam->ucPanelVsyncWidth;

    /*uiEdpVal = (64 * (UINT64) pstKtxInParam->uiKtxInPixClkFreq * pstKtxInParam->usKtxInColorDepth * 3 * 10) * 1024 \
            / (pstEdpTxTopCfgParam->ucEdpTxLaneNum * 8 * pstKtxInParam->uiKtxInDptxFreq);*/
    uiEdpVal = (((UINT64)pstKtxInParam->uiKtxInPixClkFreq * pstKtxInParam->usKtxInColorDepth * 3 * 10) << 13)  \
            / (pstEdpTxTopCfgParam->ucEdpTxLaneNum * pstKtxInParam->uiKtxInDptxFreq);
    pstEdpTxTopCfgParam->uiEdpTxTuDataSize = uiEdpVal;
    HV_LOG_DEBUG(PANEL, " EdpTxTopCfgGen uiEdpTxTuDataSize %d ", pstEdpTxTopCfgParam->uiEdpTxTuDataSize);

    /*pstEdpTxTopCfgParam->usEdpTxDhDenSta = (pstKtxInParam->uiKtxInDptxFreq/1000)/20*
    (10+1+(pstKtxInParam->usKtxInHTotal-pstKtxInParam->usKtxInHact-10-1)*2/3)/
    (pstKtxInParam->uiKtxInPixClkFreq/1000);*/
    pstEdpTxTopCfgParam->usEdpTxDhDenSta = pstKtxInParam->uiKtxInDptxFreq* \
                                            (11+(pstKtxInParam->usKtxInHTotal-pstKtxInParam->usKtxInHact-11)*2/3) / \
                                           pstKtxInParam->uiKtxInPixClkFreq/20;
    HV_LOG_DEBUG(PANEL, "usEdpTxDhDenSta: %d", pstEdpTxTopCfgParam->usEdpTxDhDenSta);

    pstEdpTxTopCfgParam->usEdpTxDvDenSta = pstKtxInParam->ucKtxVsyncBackPorch + 1;
    pstEdpTxTopCfgParam->usEdpTxDvDenEnd = pstEdpTxTopCfgParam->usEdpTxDvDenSta + pstKtxInParam->usKtxInVact;
    pstEdpTxTopCfgParam->usEdpTxMsaLineIdx = pstKtxInParam->usKtxInVact + 5;
    pstEdpTxTopCfgParam->usEdpTxPixSymbolNum = pstKtxInParam->usKtxInHact / pstEdpTxTopCfgParam->ucEdpTxLaneNum;
    pstEdpTxTopCfgParam->usAuxS2PClkRatio = pstKtxInParam->uiKtxInApbFreq / 1000 - 1;
    pstEdpTxTopCfgParam->ucAuxGlitchTh = (pstKtxInParam->uiKtxInApbFreq / 1000) / 4;     //pstKtxInParam->uiKtxInApbFreq / 1000 / 10
    /*PPC=1:5; PPC=1:1; PPC=4 2; PPC=8 3;*/
    pstEdpTxTopCfgParam->ucEdpTxMvidMode = 5;

    HV_LOG_DEBUG(PANEL, " pstEdpTxTopCfgParam->ucEdpTxBitMode:%d", pstEdpTxTopCfgParam->ucEdpTxBitMode);
    HV_LOG_DEBUG(PANEL, " pstEdpTxTopCfgParam->uiEdpTxDataSymbolNum:%d", pstEdpTxTopCfgParam->uiEdpTxDataSymbolNum);
    HV_LOG_DEBUG(PANEL, " pstEdpTxTopCfgParam->uiEdpTxMvid:%d", pstEdpTxTopCfgParam->uiEdpTxMvid);
    HV_LOG_DEBUG(PANEL, " pstEdpTxTopCfgParam->uiEdpTxMsaHTotal:%d", pstEdpTxTopCfgParam->uiEdpTxMsaHTotal);
    HV_LOG_DEBUG(PANEL, " pstEdpTxTopCfgParam->uiEdpTxMsaVTotal:%d", pstEdpTxTopCfgParam->uiEdpTxMsaVTotal);
    HV_LOG_DEBUG(PANEL, " pstEdpTxTopCfgParam->usEdpTxMasHact:%d", pstEdpTxTopCfgParam->usEdpTxMasHact);
    HV_LOG_DEBUG(PANEL, " pstEdpTxTopCfgParam->usEdpTxMasVact:%d", pstEdpTxTopCfgParam->usEdpTxMasVact);
    HV_LOG_DEBUG(PANEL, " pstEdpTxTopCfgParam->ucEdpTxLaneNum:%d", pstEdpTxTopCfgParam->ucEdpTxLaneNum);
    HV_LOG_DEBUG(PANEL, " pstEdpTxTopCfgParam->ucEdpTxMisc0:%d", pstEdpTxTopCfgParam->ucEdpTxMisc0);
    HV_LOG_DEBUG(PANEL, " pstEdpTxTopCfgParam->uiEdpTxTuDataSize:%d", pstEdpTxTopCfgParam->uiEdpTxTuDataSize);
    HV_LOG_DEBUG(PANEL, " pstEdpTxTopCfgParam->usEdpTxMsaLineIdx:%d", pstEdpTxTopCfgParam->usEdpTxMsaLineIdx);
    HV_LOG_DEBUG(PANEL, " pstEdpTxTopCfgParam->usEdpTxPixSymbolNum:%d", pstEdpTxTopCfgParam->usEdpTxPixSymbolNum);
    HV_LOG_DEBUG(PANEL, " pstEdpTxTopCfgParam->usAuxS2PClkRatio:%d", pstEdpTxTopCfgParam->usAuxS2PClkRatio);
    HV_LOG_DEBUG(PANEL, " pstEdpTxTopCfgParam->ucAuxGlitchTh:%d", pstEdpTxTopCfgParam->ucAuxGlitchTh);
    HV_LOG_DEBUG(PANEL, " pstEdpTxTopCfgParam->ucEdpTxMvidMode:%d", pstEdpTxTopCfgParam->ucEdpTxMvidMode);
    return HV_SUCCESS;
}

static Status EdpTxPreInit(const PanelParam *pstPanelParam)
{
    HV_LOG_DEBUG(PANEL, "********** EdpTxPreInit **********");
    HvDrvKtxInParam stKtxInParam = {0};
    HvEdpTxTopCfgParam stEdpTxTopCfgParam = {0};
    stKtxInParam.usKtxInVact = pstPanelParam->usPanelVactive;
    stKtxInParam.usKtxInHact = pstPanelParam->usPanelHactive;
    stKtxInParam.usKtxInVTotal = pstPanelParam->usPanelVtotalType;
    stKtxInParam.usKtxInHTotal = pstPanelParam->usPanelHtotalType;

    stKtxInParam.usKtxInGateNum = KTX_GATE_NUM;
    stKtxInParam.usKtxInSourceNum = KTX_SOURCE_NUM;
    stKtxInParam.eHvDrvKtxPhy = KTX_PHY_EDP;
    stKtxInParam.usKtxInColorDepth = pstPanelParam->ucPanelColorDepth;
    stKtxInParam.usKtxInFlipEnable = KTX_FLIP_ENABLE;
    stKtxInParam.usKtxInPreDeBeforeKtx = TX_IN_PRE_DE_BEFORE_KTX;
    stKtxInParam.usKtxInMinSta = 90;
    stKtxInParam.ucKtxVsyncBackPorch = pstPanelParam->ucPanelVsyncBackPorch;

    stKtxInParam.usKtxInPortNum = pstPanelParam->ucPanelPortNum;
    stKtxInParam.usKtxInPairNum = pstPanelParam->ucPanelLaneNum;

    stKtxInParam.uiKtxInPixClkFreq = pstPanelParam->usPanelVtotalType * pstPanelParam->usPanelHtotalType * pstPanelParam->usPanelFrameRateType / 1000;

    stKtxInParam.uiKtxInDptxFreq = pstPanelParam->ucPanelLaneRate;
    stKtxInParam.uiKtxInDptxFreq *= 270000;
    stKtxInParam.usKtxInUsePreDe = 0;

    stKtxInParam.uiKtxInApbFreq = Hv_Cal_EdpTx_GetApbClock() * 1000;

    stKtxInParam.uiKtxInRefClkFreq = 27000;
    s_stTrainingInputParam.ucLinkRate = pstPanelParam->ucPanelLaneRate;
    s_stTrainingInputParam.ucLaneCount = pstPanelParam->ucPanelLaneNum;
    
#ifdef HV_SCALER_DEBUG_VERSION
    HV_LOG_DEBUG(PANEL, "********** KTX_IN_PARAM stKtxInParam.usKtxInVact:%d", stKtxInParam.usKtxInVact);
    HV_LOG_DEBUG(PANEL, "********** KTX_IN_PARAM stKtxInParam.usKtxInHact:%d", stKtxInParam.usKtxInHact);
    HV_LOG_DEBUG(PANEL, "********** KTX_IN_PARAM stKtxInParam.usKtxInVTotal:%d", stKtxInParam.usKtxInVTotal);
    HV_LOG_DEBUG(PANEL, "********** KTX_IN_PARAM stKtxInParam.usKtxInHTotal:%d", stKtxInParam.usKtxInHTotal);
    HV_LOG_DEBUG(PANEL, "********** KTX_IN_PARAM stKtxInParam.usKtxInGateNum:%d", stKtxInParam.usKtxInGateNum);
    HV_LOG_DEBUG(PANEL, "********** KTX_IN_PARAM stKtxInParam.usKtxInSourceNum:%d", stKtxInParam.usKtxInSourceNum);
    HV_LOG_DEBUG(PANEL, "********** KTX_IN_PARAM stKtxInParam.eHvDrvKtxPhy:%d", stKtxInParam.eHvDrvKtxPhy);
    HV_LOG_DEBUG(PANEL, "********** KTX_IN_PARAM stKtxInParam.usKtxInColorDepth:%d", stKtxInParam.usKtxInColorDepth);
    HV_LOG_DEBUG(PANEL, "********** KTX_IN_PARAM stKtxInParam.usKtxInPortNum:%d", stKtxInParam.usKtxInPortNum);
    HV_LOG_DEBUG(PANEL, "********** KTX_IN_PARAM stKtxInParam.usKtxInPairNum:%d", stKtxInParam.usKtxInPairNum);
    HV_LOG_DEBUG(PANEL, "********** KTX_IN_PARAM stKtxInParam.usKtxInReverseScan:%d", stKtxInParam.usKtxInReverseScan);
    HV_LOG_DEBUG(PANEL, "********** KTX_IN_PARAM stKtxInParam.usKtxInUsePreDe:%d", stKtxInParam.usKtxInUsePreDe);
    HV_LOG_DEBUG(PANEL, "********** KTX_IN_PARAM stKtxInParam.usKtxInMinSta:%d", stKtxInParam.usKtxInMinSta);
    HV_LOG_DEBUG(PANEL, "********** KTX_IN_PARAM stKtxInParam.usKtxInCalSta:%d", stKtxInParam.usKtxInCalSta);
    HV_LOG_DEBUG(PANEL, "********** KTX_IN_PARAM stKtxInParam.usKtxInPreDeBeforeKtx:%d", stKtxInParam.usKtxInPreDeBeforeKtx);
    HV_LOG_DEBUG(PANEL, "********** KTX_IN_PARAM stKtxInParam.usKtxInFlipEnable:%d", stKtxInParam.usKtxInFlipEnable);
    HV_LOG_DEBUG(PANEL, "********** KTX_IN_PARAM stKtxInParam.uiKtxInRefClkFreq:%d", stKtxInParam.uiKtxInRefClkFreq);
    HV_LOG_DEBUG(PANEL, "********** KTX_IN_PARAM stKtxInParam.uiKtxInPixClkFreq:%d", stKtxInParam.uiKtxInPixClkFreq);
    HV_LOG_DEBUG(PANEL, "********** KTX_IN_PARAM stKtxInParam.uiKtxInDptxFreq:%d", stKtxInParam.uiKtxInDptxFreq);
    HV_LOG_DEBUG(PANEL, "********** KTX_IN_PARAM stKtxInParam.uiKtxInApbFreq:%d", stKtxInParam.uiKtxInApbFreq);
#endif
    HV_ASSERT_SUCCESS(Hv_Drv_Ktx_Init(&stKtxInParam, pstPanelParam));

    HV_ASSERT_SUCCESS(EdpTxTopCfgGen(pstPanelParam, &stKtxInParam, &stEdpTxTopCfgParam));
    Hv_Cal_EdpTx_SetTopCfg(stEdpTxTopCfgParam);

    /* EDP_TX初始设定*/
    Hv_Cal_EdpTx_FixSettings();
    Hv_Cal_EdpTx_SetAuxAnaCfg();
    Hv_Cal_EdpTx_SetAuxPnSwap(EDP_TX_PROT_INDEX0, pstPanelParam->bPanelEdpAux0PnSwapEn);
    Hv_Cal_EdpTx_SetAuxPnSwap(EDP_TX_PROT_INDEX1, pstPanelParam->bPanelEdpAux1PnSwapEn);
    Hv_Cal_EdpTx_SetHpdSoftMode(EDP_TX_PROT_INDEX0, pstPanelParam->ucPanelEdpHpd0SoftMode);
    Hv_Cal_EdpTx_SetHpdSoftMode(EDP_TX_PROT_INDEX1, pstPanelParam->ucPanelEdpHpd1SoftMode);
    return HV_SUCCESS;
}

static TxPanelParam s_stTxPanelParam = {0};

VOID Hv_Drv_EdpTx_Init(const PanelParam *pstPanelParam)
{
    HV_LOG_INFO(PANEL, "%s", pstPanelParam->ucPanelName);
    s_stTxPanelParam.bTxPhy0MuxEn = pstPanelParam->bPanelPhy0MuxEn;
    s_stTxPanelParam.bTxPhy1MuxEn = pstPanelParam->bPanelPhy1MuxEn;
    s_stTxPanelParam.bTxPhy2MuxEn = pstPanelParam->bPanelPhy2MuxEn;
    s_stTxPanelParam.ucTxPortNum  = pstPanelParam->ucPanelPortNum;
    HV_ASSERT_SUCCESS_VOID(EdpTxPreInit(pstPanelParam));
    Hv_Drv_EdpTx_HpqIrqInit();
    /* must ptcdpll stable */ 
    HV_Drv_Ktx_PllEnable(HV_TRUE);
    return;
}
VOID Hv_Drv_EdpTx_Enable(BOOL bEn)
{
    s_stTrainingInputParam.uiAuxDelay = 3;
    s_stTrainingInputParam.enEdpTxPortIndex = EDP_TX_PROT_INDEX0;
    s_stTrainingInputParam.enEdpAuxPortIndex = EDP_AUX_PROT_INDEX0;
    s_stTrainingInputParam.enEdpHpdPortIndex = EDP_HPD_PROT_INDEX0;

    Hv_Cal_EdpTx_PifEn(bEn);

    Hv_Cal_EdpTx_PifRst(bEn);

    Hv_Drv_Ktx_ComboTxPhyEnable(bEn, &s_stTxPanelParam);

    Hv_Cal_EdpTx_MainLinkEn(bEn);

    if (HV_TRUE == Hv_Drv_EdpTx_CheckPanelExist())
    {
        Hv_Drv_EdpTx_Training(bEn);
    }
    return;
}

VOID Hv_Drv_EdpTx_Training(BOOL bEn)
{
    if (bEn)
    {
    /* No Link Training */
#if (HV_PROJECT_CONFIG_EDP_TX_TRAINING_TYPE == HV_EDP_TX_NO_LINK_TRAINING)
        Hv_Cal_EdpTx_Tps1Done(bEn);
        Hv_Cal_EdpTx_EqDone(bEn);
#endif

#if (HV_PROJECT_CONFIG_EDP_TX_TRAINING_TYPE == HV_EDP_TX_FAST_LINK_TRAINING)
        /*TODO 未验证*/
        Hv_Cal_EdpTx_Tps1Done(HV_FALSE);
        Hv_Cal_EdpTx_EqDone(HV_FALSE);
        Hv_Vos_Delayus(100);
        Hv_Cal_EdpTx_Tps1Done(HV_TRUE);
        Hv_Vos_Delayus(4000);
        Hv_Cal_EdpTx_EqDone(HV_TRUE);
#endif

#if (HV_PROJECT_CONFIG_EDP_TX_TRAINING_TYPE == HV_EDP_TX_FULL_LINK_TRAINING)
        HV_LOG_DEBUG(PANEL, "Hv_Drv_EdpTx_Enable ExecuteTraining");
        s_stTrainingInputParam.enEdpTxPortIndex = EDP_TX_PROT_INDEX0;
        s_stTrainingInputParam.enEdpAuxPortIndex = HV_PROJECT_CONFIG_EDP_TX_AUX0_PORT;
        s_stTrainingInputParam.enEdpHpdPortIndex = HV_PROJECT_CONFIG_EDP_TX_HPD0_PORT;
        s_ucEdpTxTrainingResutl = Hv_Drv_EdpTx_ExecuteTraining(&s_stTrainingInputParam);
        if (2 == s_stTxPanelParam.ucTxPortNum)
        {
            s_stTrainingInputParam.enEdpAuxPortIndex = HV_PROJECT_CONFIG_EDP_TX_AUX1_PORT;
            s_stTrainingInputParam.enEdpHpdPortIndex = HV_PROJECT_CONFIG_EDP_TX_HPD1_PORT;
            s_stTrainingInputParam.enEdpTxPortIndex = EDP_TX_PROT_INDEX1;
            s_ucEdpTxTrainingResutl += Hv_Drv_EdpTx_ExecuteTraining(&s_stTrainingInputParam);
        }
#endif
    }
    return;
}

Status Hv_Drv_EdpTx_ReturnFullTrainingStatus(VOID)
{
    if ((2 == s_stTxPanelParam.ucTxPortNum) && (2 == s_ucEdpTxTrainingResutl))
    {
        return HV_SUCCESS;
    }
    else if ((1 == s_stTxPanelParam.ucTxPortNum) && (1 == s_ucEdpTxTrainingResutl))
    {
        return HV_SUCCESS;
    }
    else
    {
        return HV_FAILURE;
    }
}

BOOL Hv_Drv_EdpTx_CheckPanelExist(VOID)
{
    /*判断HPD是否拉高，如果不看HPD，则应该把HPD设为软件模式，并拉高*/
    if (1 == s_stTxPanelParam.ucTxPortNum)
    {
        if ((HV_TRUE == Hv_Cal_EdpTx_GetHpdStatus(HV_PROJECT_CONFIG_EDP_TX_HPD0_PORT)) || (HV_TRUE == Hv_Cal_EdpTx_GetHpdStatus(HV_PROJECT_CONFIG_EDP_TX_HPD1_PORT)))
        {
            return HV_TRUE;
        }
        else
        {
            return HV_FALSE;
        }
    }
    else
    {
        if((HV_TRUE == Hv_Cal_EdpTx_GetHpdStatus(HV_PROJECT_CONFIG_EDP_TX_HPD0_PORT)) && (HV_TRUE == Hv_Cal_EdpTx_GetHpdStatus(HV_PROJECT_CONFIG_EDP_TX_HPD1_PORT)))
        {
            return HV_TRUE;
        }
        else
        {
            return HV_FALSE;
        }
    }
}

VOID Hv_Drv_EdpTx_CalcEdpTxMvidTuSize(VideoConfigParams* pstVideoConfigParams, const PanelParam *pstPanelParam, UINT32 uiClock)
{
    pstVideoConfigParams->uiEdpTuDataSize = ((DOUBLE64)uiClock / pstPanelParam->ucPanelPortNum * 3 * pstPanelParam->ucPanelColorDepth * 10 / 8) / pstPanelParam->ucPanelLaneNum / (pstPanelParam->ucPanelLaneRate * 270000) * 64 * 1024;
    pstVideoConfigParams->uiMvid = ((DOUBLE64)uiClock / pstPanelParam->ucPanelPortNum * 32768)  / (pstPanelParam->ucPanelLaneRate * 270000 / 10);
    if (pstVideoConfigParams->uiEdpTuDataSize > 63488)
    {
        HV_LOG_INFO(PANEL, "calc max tu data size: %u", pstVideoConfigParams->uiEdpTuDataSize);
        /*限制tu data size 最大的62<<10,不能超过64*/
        pstVideoConfigParams->uiEdpTuDataSize = 63488;
    }
    HV_LOG_DEBUG(PANEL, "Hv_Drv_EdpTx_CalcEdpTxMvidTuSize uiPixClk: %u, uiMvid: %u, uiEdpTuDataSize: %u", uiClock, pstVideoConfigParams->uiMvid, pstVideoConfigParams->uiEdpTuDataSize);
    return;
}

VOID Hv_Drv_EdpTx_SetEdpTxMvidTuSize(UINT32 uiEdpTuDataSize, UINT32 uiMvid)
{
    Hv_Cal_EdpTx_SetEdpTxMvid(uiMvid);
    Hv_Cal_EdpTx_SetEdpTxTuSize(uiEdpTuDataSize);
}

static HV_VOS_ISR_RESULT_E Hv_Drv_EdpTx_HpdIrqHandler(UINT32 irqNum, void *arg)
{
    /*清中断！*/
    Hv_Vos_WClrMiscInt(irqNum - HV_CHIP_IRQ_BASE);
    EdpTxInterruptDefine stEdpIntDef = {0};
    stEdpIntDef = Hv_Cal_EdpTx_GetHpdInt();
    Hv_Cal_EdpTx_ClearHpdInt();
    HV_LOG_INFO(PANEL, "irqNum:%u, uiHpdEvt:%u, uiHpdIrq:%u uiHpdPlug:%u, panel exist:%u.", (irqNum - HV_CHIP_IRQ_BASE), stEdpIntDef.uiHpdEvt, stEdpIntDef.uiHpdIrq, stEdpIntDef.uiHpdPlug, Hv_Drv_EdpTx_CheckPanelExist());
    if ((1 == stEdpIntDef.uiHpdEvt) || (1 == stEdpIntDef.uiHpdPlug))
    {
        if (HV_TRUE == Hv_Drv_EdpTx_CheckPanelExist())
        {
            /*edp插入，打开tx,重新开始training*/
            Hv_Drv_EdpTx_Enable(HV_TRUE);
        }
        else
        {
            /*被拔出，关闭tx*/
            Hv_Drv_EdpTx_Enable(HV_FALSE);
        }
    }
    else if (1 == stEdpIntDef.uiHpdIrq)
    {
        /*检查连接状态*/
        if ((HV_TRUE == Hv_Drv_EdpTx_CheckPanelExist()) && (HV_SUCCESS == Hv_Drv_EdpTx_NoLinkTrainingStatus()))
        {
            HV_LOG_INFO(PANEL, "HPD irq, connect ok.");
        }
        else
        {
            //Hv_Drv_EdpTx_Enable(HV_FALSE);
            Hv_Drv_EdpTx_Enable(HV_TRUE);
        }
    }
    
    return HV_VOS_ISR_HANDLED;
}

VOID Hv_Drv_EdpTx_SetEdpHpqIrqEnable(EdpHpdPortIndex enHpdPort)
{
    if (EDP_HPD_PROT_INDEX0 == enHpdPort)
    {
        Hv_Vos_WClrMiscInt(HV_CHIP_IRQ_EDPTX_00_HPD - HV_CHIP_IRQ_BASE);
        Hv_Cal_EdpTx_SetEdpHpqIrqEn(HV_ENABLE, EDP_HPD_PROT_INDEX0);
        Hv_Vos_UnmaskIrq(HV_CHIP_IRQ_EDPTX_00_HPD);
    }
    else
    {
        Hv_Vos_WClrMiscInt(HV_CHIP_IRQ_EDPTX_01_HPD - HV_CHIP_IRQ_BASE);
        Hv_Cal_EdpTx_SetEdpHpqIrqEn(HV_ENABLE, EDP_HPD_PROT_INDEX1);
        Hv_Vos_UnmaskIrq(HV_CHIP_IRQ_EDPTX_01_HPD);
    }
    return;
}

VOID Hv_Drv_EdpTx_SetEdpHpqIrqDisable(EdpHpdPortIndex enHpdPort)
{
    if (EDP_HPD_PROT_INDEX0 == enHpdPort)
    {
        Hv_Vos_WClrMiscInt(HV_CHIP_IRQ_EDPTX_00_HPD - HV_CHIP_IRQ_BASE);
        Hv_Cal_EdpTx_SetEdpHpqIrqEn(HV_DISABLE, EDP_HPD_PROT_INDEX0);
        Hv_Vos_MaskIrq(HV_CHIP_IRQ_EDPTX_00_HPD);
    }
    else
    {
        Hv_Vos_WClrMiscInt(HV_CHIP_IRQ_EDPTX_01_HPD - HV_CHIP_IRQ_BASE);
        Hv_Cal_EdpTx_SetEdpHpqIrqEn(HV_DISABLE, EDP_HPD_PROT_INDEX1);
        Hv_Vos_MaskIrq(HV_CHIP_IRQ_EDPTX_01_HPD);
    }
    return;
}

VOID Hv_Drv_EdpTx_HpqIrqInit(VOID)
{
    Hv_Vos_ConfigIrq(HV_CHIP_IRQ_EDPTX_00_HPD, HV_TRUE, 0);
    Hv_Vos_AttachIsr(HV_CHIP_IRQ_EDPTX_00_HPD, Hv_Drv_EdpTx_HpdIrqHandler, NULL);
    Hv_Drv_EdpTx_SetEdpHpqIrqDisable(EDP_HPD_PROT_INDEX0);

    Hv_Vos_ConfigIrq(HV_CHIP_IRQ_EDPTX_01_HPD, HV_TRUE, 0);
    Hv_Vos_AttachIsr(HV_CHIP_IRQ_EDPTX_01_HPD, Hv_Drv_EdpTx_HpdIrqHandler, NULL);
    Hv_Drv_EdpTx_SetEdpHpqIrqDisable(EDP_HPD_PROT_INDEX1);
    return;
}

VOID Hv_Drv_EdpTx_SendIdlePatternEn(EdpTxPortIndex enEdpTxPort, BOOL bEn)
{
    Hv_Cal_EdpTx_SendIdlePatternEn(enEdpTxPort, bEn);
    return;
}

VOID Hv_Drv_EdpTx_SetAuxPnSwap(EdpTxPortIndex enPort, BOOL bEn)
{
    Hv_Cal_EdpTx_SetAuxPnSwap(enPort, bEn);
    return;
}

VOID Hv_Drv_EdpTx_SetTps1Done(BOOL bDone)
{
    Hv_Cal_EdpTx_Tps1Done(bDone);
    return;
}

VOID Hv_Drv_EdpTx_SetEqDone(BOOL bDone)
{
    Hv_Cal_EdpTx_EqDone(bDone);
    return;
}

VOID HV_Drv_Panel_Init(PanelControl* pstPanelControl)
{
    pstPanelControl->pfPanelInit      = Hv_Drv_EdpTx_Init;
    pstPanelControl->pfPanelEnable    = Hv_Drv_EdpTx_Enable;
#if(HV_EDP_TX_FULL_LINK_TRAINING == HV_PROJECT_CONFIG_EDP_TX_TRAINING_TYPE)
    pstPanelControl->pfGetPanelStatus = Hv_Drv_EdpTx_ReturnFullTrainingStatus;
#else
    pstPanelControl->pfGetPanelStatus = Hv_Drv_EdpTx_NoLinkTrainingStatus;
#endif
    pstPanelControl->pfSetMNTuSize    = Hv_Drv_EdpTx_SetEdpTxMvidTuSize;
    pstPanelControl->pfCalMNTuSize    = Hv_Drv_EdpTx_CalcEdpTxMvidTuSize;
    pstPanelControl->pfCheckPanelExist= Hv_Drv_EdpTx_CheckPanelExist;
    pstPanelControl->pfPanelIrqEnable = Hv_Drv_EdpTx_SetEdpHpqIrqEnable;
    pstPanelControl->pfPanelIrqDisable = Hv_Drv_EdpTx_SetEdpHpqIrqDisable;
    return;
}

