/**
 * @file hv_drv_vbo_tx.c
 * @brief VboTx module driver
 * @details This module provide interfaces for Hiview-Madia-Ware.
 * @author HiView SoC Software Team
 * @version 0.0.1
 * @date 20220903
 * @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>20220903       <td>Create and first Version.
 * </table>
 **/

#include "hv_drv_VboTx.h"
#include "hv_drv_Ktx.h"
#include "hv_cal_VboTx.h"
#include "hv_cal_Ktx.h"
#include "PanelConfig.h"
#include "hv_comm_Log.h"
#include "hv_comm_Assert.h"
#include "hv_comm_Define.h"
#include "hv_vos_Comm.h"
#include "hv_cal_PLL.h"

#define KTX_IN_USER_PRE_DE          (0)

#define VBO_PLL_STABLE          1
#define VBO_FW_HTPDN_SET        1
#define VBO_HTPDN_MODE_CTRL     0
#define VBO_REG_DELAY           206
#define VBO_LANE_ENABLE         255
#define VBO_DEBOUNCE_CNT        0
#define VBO_PIXEL_MODE          2

Status VboTxPreInit(const PanelParam *pstPanelParam)
{
    HvDrvKtxInParam stKtxInParam;
    HV_MEMSET(&stKtxInParam, 0, sizeof(stKtxInParam));

    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_VBO;
    stKtxInParam.usKtxInColorDepth = pstPanelParam->ucPanelColorDepth;
    stKtxInParam.usKtxInUsePreDe = KTX_IN_USER_PRE_DE;
    stKtxInParam.usKtxInFlipEnable = KTX_FLIP_ENABLE;
    stKtxInParam.usKtxInPreDeBeforeKtx = TX_IN_PRE_DE_BEFORE_KTX;
    stKtxInParam.usKtxInMinSta = 0;
    stKtxInParam.ucKtxVsyncBackPorch = pstPanelParam->ucPanelVsyncBackPorch;

    if (pstPanelParam->ucPanelLaneNum < 1)
    {
        return HV_FAILURE;
    }
    stKtxInParam.usKtxInPortNum = pstPanelParam->ucPanelPortNum;
    stKtxInParam.usKtxInPairNum = pstPanelParam->ucPanelLaneNum;

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

    HV_ASSERT_SUCCESS(Hv_Drv_Ktx_Init(&stKtxInParam, pstPanelParam));

    /*VBO fix settings*/
    HvVboTopParam stVboTopParam;
    HV_MEMSET(&stVboTopParam, 0, sizeof(stVboTopParam));
    if (pstPanelParam->ucPanelColorDepth == 10)
    {
        stVboTopParam.usByteMode = 4;
        stVboTopParam.usColorFormat = 1;
    }
    else if (pstPanelParam->ucPanelColorDepth == 12)
    {
        stVboTopParam.usByteMode = 5;
        stVboTopParam.usColorFormat = 0;
    }
    else
    {
        stVboTopParam.usByteMode = 3;
        stVboTopParam.usColorFormat = 2;
    }

    stVboTopParam.usSection             = pstPanelParam->ucPanelPortNum;
    stVboTopParam.usPllStable           = VBO_PLL_STABLE;
    stVboTopParam.usFwHtpdnSet          = VBO_FW_HTPDN_SET;
    stVboTopParam.usHtpdnModeCtrl       = VBO_HTPDN_MODE_CTRL;
    stVboTopParam.usRegDelay            = VBO_REG_DELAY;
    stVboTopParam.usLaneEnable          = VBO_LANE_ENABLE;
    stVboTopParam.usDebounceCnt         = VBO_DEBOUNCE_CNT;
    stVboTopParam.usLaneNum             = pstPanelParam->ucPanelLaneNum;
    stVboTopParam.usPixelMode           = VBO_PIXEL_MODE;

    Hv_Cal_VboTx_Init(stVboTopParam);
#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.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
    return HV_SUCCESS;
}

static TxPanelParam s_stTxPanelParam = {0};
VOID Hv_Drv_VboTX_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;
    HV_ASSERT_SUCCESS_VOID(VboTxPreInit(pstPanelParam));

    /* must ptcdpll stable */ 
    HV_Drv_Ktx_PllEnable(1);
    return;
}

VOID Hv_Drv_VboTx_Enable(BOOL bEn)
{
    Hv_Cal_VboTx_SetPifVboClkEn(bEn);

    Hv_Cal_VboTx_SetMacEn(bEn);

    Hv_Drv_Ktx_ComboTxPhyEnable(bEn, &s_stTxPanelParam);

    Hv_Cal_PLL_Txd_WaitUntilLock();

    return;
}

Status Hv_Drv_VboTx_GetVboLocknStatus(VOID)
{
    return Hv_Cal_VboTx_GetVboLocknStatus();
}

BOOL Hv_Drv_VboTx_CheckPanelExist(VOID)
{
    return Hv_Cal_VboTx_CheckPanelExist();
}

VOID HV_Drv_Panel_Init(PanelControl* pstPanelControl)
{
    pstPanelControl->pfPanelInit      = Hv_Drv_VboTX_Init;
    pstPanelControl->pfPanelEnable    = Hv_Drv_VboTx_Enable;
    pstPanelControl->pfGetPanelStatus = Hv_Drv_VboTx_GetVboLocknStatus;
    pstPanelControl->pfSetMNTuSize    = NULL;
    pstPanelControl->pfCalMNTuSize    = NULL;
    pstPanelControl->pfCheckPanelExist= Hv_Drv_VboTx_CheckPanelExist;
    pstPanelControl->pfPanelIrqEnable = NULL;
    pstPanelControl->pfPanelIrqDisable = NULL;
    return;
}

