/**
 * @file hv_drv_ktx.h
 * @brief Header file of ktx module.
 *
 * @verbatim
 * ==============================================================================
 *                        ##### How to use #####
 * ==============================================================================
 *
 * @endverbatim
 *
 * @author HiView SoC Software Team
 * @version 0.0.0
 * @date 2022-09-03
 */

#ifndef _HV_DRV_KTX_H_
#define _HV_DRV_KTX_H_

#include "hv_comm_DataType.h"
#include "hv_cal_KtxCommon.h"
#include "hv_comm_Define.h"

typedef enum
{
    KTX_PHY_MLVDS,      // 0
    KTX_PHY_CMPI,       // 1
    KTX_PHY_EPI,        // 2
    KTX_PHY_ISP,        // 3
    KTX_PHY_USIT,       // 4
    KTX_PHY_VBO,        // 5
    KTX_PHY_CHI,        // 6
    KTX_PHY_EDP         // 7
} HvDrvKtxPhy;

typedef struct _HvDrvKtxInParam
{
    USHORT16               usKtxInHact;
    USHORT16               usKtxInVact;
    USHORT16               usKtxInHTotal;
    USHORT16               usKtxInVTotal;
    USHORT16               usKtxInPortNum;
    USHORT16               usKtxInPairNum;
    USHORT16               usKtxInGateNum;
    USHORT16               usKtxInSourceNum;
    USHORT16               usKtxInReverseScan;
    USHORT16               usKtxInUsePreDe;
    USHORT16               usKtxInPreDeBeforeKtx;
    USHORT16               usKtxInFlipEnable;
    UINT32                 uiKtxInPixClkFreq;
    UINT32                 uiKtxInRefClkFreq;
    HvDrvKtxPhy            eHvDrvKtxPhy;
    USHORT16               usKtxInColorDepth;
    UINT32                 uiKtxInDptxFreq; // Khz for edp tx
    UINT32                 uiKtxInApbFreq;  // Khz
    UCHAR8                 ucKtxVsyncBackPorch;

    USHORT16               usKtxInMinSta;
    USHORT16               usKtxInCalSta;
} HvDrvKtxInParam;

typedef struct _HvDrvKtxOutParam
{
    HvKtxPostTopCfgParam    stHvDrvKtxPostCfgParam;
    HvKtxPIFCfgParam        stHvDrvKtxPifCfgParam;
    HvKtxPllCfgParam        stHvDrvKtxPllCfgParam;
} HvDrvKtxOutParam;

typedef struct _HvDrvKtxDesign
{
    USHORT16    usPPC;
    USHORT16    usTxLineBufNum;
    UCHAR8      ucTxOutDataMode;
    UINT32      uiTxPllMinVco; // KHz
    UINT32      uiTxPllMaxVco; // KHz
    UINT32      uiTxPllMinRef; // KHz
    UINT32      uiTxPllMaxRef; // KHz
} HvDrvKtxDesign;

typedef struct _TxPanelParam
{
    BOOL    bTxPhy0MuxEn;
    BOOL    bTxPhy1MuxEn;
    BOOL    bTxPhy2MuxEn;
    UCHAR8  ucTxPortNum;
}TxPanelParam;

/**
 * @brief 计算并生成 E1 - KTX_POST_TOP 所使用的参数.
 * @param[in] pstKtxInParam KTX输入参数.
 * @param[out] pstKtxPostCfgParam 输出KTX_POST_TOP需要写入的参数
 * @return 0: Success or other: Fail.
 */
static Status KtxPostTopCfgGen(HvDrvKtxInParam *pstKtxInParam, HvDrvKtxDesign *pstKtxDesign,
                              HvKtxPostTopCfgParam *pstKtxPostCfgParam);

/**
 * @brief 计算并生成 F0 - COMBOTX_PHY_0 所使用的参数.
 * @param[in] pstKtxInParam KTX输入参数.
 * @param[out] pstTxPifCfgParam 计算输出一部分PIF参数
 * @param[out] pstTxPllCfg 输出COMBOTX_PHY_0需要写入的参数
 * @return 0: Success or other: Fail.
 */
static Status KtxPllCfgGen(
    HvDrvKtxInParam *pstKtxInParam,
    HvKtxPllCfgParam *pstTxPllCfg,
    HvKtxPIFCfgParam *pstTxPifCfgParam);

/**
 * @brief 计算并生成 KTX reg_txpll_0_postdiv2.
 * @param[in] pstKtxInParam KTX输入参数.
 * @param[out] pstTxPllCfg 输出COMBOTX_PHY_0 txpll_0_postdiv2
 * @return 0: Success or other: Fail.
 */
static Status Hv_Drv_KTX_SetKTXSelfClock(const HvDrvKtxInParam *pstKtxInParam, HvKtxPllCfgParam *pstKtxPllCfgParam);

/**
 * @brief 计算并生成 E0 - PIF_TX_TOP 所使用的参数.
 * @param[in] pstKtxInParam KTX输入参数.
 * @param[in] pstDesign VBO相关.
 * @param[in] pstTxPostTopCfgParam PostTop参数
 * @param[out] pstTxPifCfgParam 输出 PIF_TX_TOP 需要写入的参数
 * @return 0: Success or other: Fail.
 */
static Status KtxPifCfgGen(
    HvDrvKtxInParam *pstKtxInParam,
    HvDrvKtxDesign *pstKtxDesign,
    HvKtxPostTopCfgParam *pstTxPostTopCfgParam,
    HvKtxPIFCfgParam *pstTxPifCfgParam);

/**
 * @brief 初始化KTX，计算PIF PLL POST参数并写入寄存器.
 * @param[in] pstKtxInParam KTX输入参数.
 * @return 0: Success or 1: Fail.
 */
Status Hv_Drv_Ktx_Init(HvDrvKtxInParam *pstKtxInParam, const PanelParam *pstPanelParam);

/**
 * @brief 打开KTX PLL
 */
VOID HV_Drv_Ktx_PllEnable(BOOL bEn);

/**
 * @brief 打开KTX Phy
 */
VOID Hv_Drv_Ktx_ComboTxPhyEnable(BOOL bEn, TxPanelParam *pstTxPanelParam);

/**
 * @brief 打开KTX Pif
 */
VOID Hv_Drv_Ktx_PifStart(BOOL bStart);

/**
 * @brief rest edp mac
 */
VOID Hv_Drv_Ktx_TxMacRst(BOOL bStart);

/**
 * @brief rest Pif
 */
VOID Hv_Drv_Ktx_PifRst(BOOL bStart);

/**
 * @brief 获取TxPhyAfifo状态
 */
BOOL Hv_Drv_Ktx_GetTxPhyAfifoFlag(VOID);

/**
 * @brief 清空TxPhyAfifo标志位
 */
VOID Hv_Drv_Ktx_TxPhyAfifoFlagClear(VOID);

/**
 * @brief 设置背光板5V 电源
 */
VOID Hv_Drv_Ktx_SetBacklightPower(BOOL bEn);

/**
 * @brief 获取背光板5V 电源状态
 */
BOOL Hv_Drv_Ktx_GetBacklightPower(VOID);

/**
 * @brief 设置KTX pattern
 */
Status Hv_Drv_Ktx_SetKtxPatt(UCHAR8 ucKtxPattSwitch, UCHAR8 ucPatternMode);

/**
 * @brief 设置Panel 12V 电源
 */
VOID Hv_Drv_Ktx_PanelVideoPower(BOOL bEn);

/**
 * @brief open ktx post cursor
 */
VOID Hv_Drv_Ktx_SetTxPostCursroParam(UINT32 uiHpost, UINT32 uiVpost,UINT32 uiR, UINT32 uiG, UINT32 uiB);

/**
 * @brief 保存panel状态到寄存器
 */
VOID Hv_Drv_Ktx_SetPanelState(USHORT16 usState);

/**
 * @brief 保存power状态到寄存器
 */
VOID Hv_Drv_Ktx_SetPowerState(USHORT16 usState);

/**
 * @brief 保存开机原因到寄存器
 */
VOID Hv_Drv_Ktx_SetPowerOnReason(USHORT16 usState);

/**
 * @brief 设置ldc vcc电源
 */
VOID Hv_Drv_Ktx_SetLdcVcc(UCHAR8 ucEn);
/**
 * @brief 设置ldc vin电源
 */
VOID Hv_Drv_Ktx_SetLdcVin(UCHAR8 ucEn);

/**
 * @brief 设置ldc en引脚
 */
VOID Hv_Drv_Ktx_SetLdcEn(UCHAR8 ucEn);
#endif
