/* 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_dvi.h,v $
 * $Revision$
 *
 *---------------------------------------------------------------------------*/
/**
 * @file DVI/HDMI/CCIR decoder driver interface
 *
 * @author daniel_hsiao@mtk.com.tw
 */

#ifndef _DRV_DVI2_H_
#define _DRV_DVI2_H_

#include "typedef.h"
#include "drv_hdmi2.h"

enum
{
    DVI_NO_SIGNAL,
    DVI_CHK_MODECHG,
    DVI_WAIT_STABLE
};

enum
{
    DVI_3D_NONE,
    DVI_FP_progressive,
    DVI_FP_interlace,
    DVI_SBS_full_progressive,
    DVI_SBS_full_interlace,
    DVI_Field_Alternative
};

typedef enum
{
    DVI_DOLBY_HDR_NONE,
    DVI_DOLBY_HDR_LOW_LATENCY,
    DVI_DOLBY_HDR_STANDARD
}E_DVI_DOLBY_HDR_STATUS;

typedef struct
{

    UINT8   _bDviChkState;
    UINT8   _bDviModeChgFlg;
    UINT8    _bDviVclk;
    UINT32    _wDviHclk; // timing table: table value; otherwise, the value used by timing search
    UINT32    _wDviPixclk;
    UINT32    _wDviWidth;
    UINT32    _wDviHeight;
    UINT32    _wDviHtotal;
    UINT32    _wDviVtotal;
    UINT8   _bInterlace;
    UINT8    _bDviTiming;
    UINT8   _bWidthStableCnt;
    UINT8   _bHeightStableCnt;
    UINT8   _bVclkStableCnt;
    UINT8   _bWidthStableFlg;
    UINT8   _bHeightStableFlg;

    UINT8  _bModeChgCnt;
    UINT8  _bDeChgCnt;
    UINT8  _bPixelClkChgCnt;
    UINT8  _bColorChgCnt;
    UINT8  _bNoSigalCnt;
    UINT8  _bNoConnectCnt;
    UINT8  _bInfoChgCnt;
    UINT8  _bVdpMuteCnt;

    UINT8    _bDviVclkTmp;
    UINT32    _wDviWidthTmp;
    UINT32    _wDviHeightTmp;
    UINT32    _wDviHtotalTmp;
    UINT32    _wDviVtotalTmp;
    UINT8   _bDviWaitStableCnt;
//Monitor mode change status start
    UINT8   _bHdmiMD;
    UINT8   _bHdmi4KPsStaChg;
    UINT8   _bHdmiRange;
    UINT8   _bHdmi422Input;
    UINT8   _bHdmiColorMetry;
    UINT8   _bHdmi3DEnable;
    UINT8   _bHdmi3DStructure;
    UINT8   _bHDMIColorSpace;
//Monitor mode change status end
    UINT32 _bQueryMode;
    BOOL   _fgIsDetectDone;
    UINT8  _bModeDoneTiming;
    UINT16 _wDviVstartDefault;
    UINT8  _bBoundary_flag;
    E_DVI_DOLBY_HDR_STATUS _rDolbyHdrStatus;
    UINT16 _wDolbyHdrMuteCount;
}DviStatus_t;

extern UINT8   _bDviExtSrc;
#define fgIsExtSrcDVI() (_bDviExtSrc&SV_DV_DVI)
#define fgIsExtSrcHDMI() (_bDviExtSrc&SV_DV_HDMI)
#define fgIsExtSrcAdYPbPr() (_bDviExtSrc&SV_DV_AD_YPbPr)
#define fgIsExtSrcAdRGB() (_bDviExtSrc&SV_DV_AD_RGB)
#define fgIsExtSrcCCIR() (_bDviExtSrc&SV_DV_CCIR)
#define fgIsExtSrcFromCCIR() (_bDviExtSrc&SV_DV_FROM_CCIR)
#define fgIsExtSrcDTV() (_bDviExtSrc&SV_DV_DTV)

/**
 * @brief         DVI initial function for each mode change or connect/disconnect.
 * @param        bchannel---- Video path number(SV_VP_MAIN/SV_VP_PIP)
*/
extern void vDvi2Initial(UINT8 bchannel);

/**
 * @brief        DVI source select connect or disconnect.
 * @param        bchannel---- Video path number(SV_VP_MAIN/SV_VP_PIP)
 * @param        fgIsOn---- TRUE for connect. FALSE for disconnect
*/
extern void vDvi2Connect(UINT8 bchannel, UINT8 fgIsOn);
/**
 * @brief        Check DVI mode change
 * @param        bchannel---- Video path number(SV_VP_MAIN/SV_VP_PIP)
 * @retval       TRUE for DVI is in mode change state, FALSE is for DVI is in mode done state
*/
extern BOOL fgDvi2IsModeChg(UINT8 bchannel);
/**
 * @brief        Get path for HDMI port
 * @param        ePort---- HDMI port number
 * @retval       Video path number, SV_VP_MAX is returned if ePort is not selected to any path, otherwise, SV_VP_MAIN/SV_VP_PIP is returned
*/
extern UINT8 bDvi2GetPath(E_HDMI_SWITCH_NUM ePort);
/**
 * @brief        Get HDMI port number for video path
 * @param        u1Type---- Video path number(SV_VP_MAIN/SV_VP_PIP)
 * @retval       HDMI port number
*/
extern UINT8 bDvi2GetActivePort(UINT8 u1Type);
/**
 * @brief        Get DVI H total
 * @param        u1Type---- Video path number(SV_VP_MAIN/SV_VP_PIP)
 * @retval       DVI H total
*/
extern UINT16 wDvi2InputHTotal(UINT8 u1Type);
/**
 * @brief        Get DVI V total
 * @param        u1Type---- Video path number(SV_VP_MAIN/SV_VP_PIP)
 * @retval       DVI V total
*/
extern UINT16 wDvi2InputVTotal(UINT8 u1Type);
/**
 * @brief        Get DVI width
 * @param        u1Type---- Video path number(SV_VP_MAIN/SV_VP_PIP)
 * @retval       DVI width
*/
extern UINT16 wDvi2InputWidth(UINT8 u1Type);
/**
 * @brief        Get DVI height
 * @param        u1Type---- Video path number(SV_VP_MAIN/SV_VP_PIP)
 * @retval       DVI height
*/
extern UINT16 wDvi2InputHeight(UINT8 u1Type);
/**
 * @brief        Get DVI color space
 * @param        u1Type---- Video path number(SV_VP_MAIN/SV_VP_PIP)
 * @retval       1 for RGB, 0 for YCbCr
*/
extern UINT8 bDvi2GetColorSpace(UINT8 u1Type);
/**
 * @brief        Set DVI color space, currently it is used for other module such as VDP to trigger HDMI mode change manually
 * @param        u1Type---- Video path number(SV_VP_MAIN/SV_VP_PIP)
 * @param        u1ClrSpc---- Currently it is set to 0xff for an invalid color space
*/
extern void vDvi2SetColorSpace(UINT8 u1Type,UINT8 u1ClrSpc);
/**
 * @brief        Get DVI refresh rate
 * @param        u1Type---- Video path number(SV_VP_MAIN/SV_VP_PIP)
 * @retval        DVI refresh rate
*/
extern UINT8 bDvi2RefreshRate(UINT8 u1Type);
/**
 * @brief        Get DVI signal status
 * @param        u1Type---- Video path number(SV_VP_MAIN/SV_VP_PIP)
 * @retval       See enum VIDE_SIGNAL_STATUS_LIST
*/
extern UINT8 bDvi2SigStatus(UINT8 u1Type);
/**
 * @brief        Check video/dvi timing
 * @param        u1Type---- Video path number(SV_VP_MAIN/SV_VP_PIP)
 * @retval       TRUE for video timing, FALSE for dvi timing
*/
extern BOOL fgIsDvi2VideoTiming(UINT8 u1Type);
/**
 * @brief        Set DVI mode change
 * @param        u1Type---- Video path number(SV_VP_MAIN/SV_VP_PIP)
*/
extern void vDvi2SetModeChange(UINT8 u1Type);
/**
 * @brief        Check 3D packet vaild or not
 * @param        u1Type---- Video path number(SV_VP_MAIN/SV_VP_PIP)
 * @retval       1 for valid, 0 for invalid
*/
extern UINT8 bDvi23DPacketVaild(UINT8 u1Type);
/**
 * @brief        Get DVI 3D format
 * @param        u1Type---- Video path number(SV_VP_MAIN/SV_VP_PIP)
 * @retval       DVI 3D format
*/
extern UINT8 bDvi2Get3DFormat(UINT8 u1Type);
/**
 * @brief        Get DVI interlace or progressive
 * @param        u1Type----
 * @retval       1 for interlace, 0 for progressive
*/
extern UINT8 bDvi2GetInterlaced(UINT8 u1Type);
/**
 * @brief
 * @param        bPath----
 * @retval
*/
extern UINT8 bGetSupportHdmi2ModeVideoTiming(UINT8 bPath);
/**
 * @brief        DVI check mode change function after detect done
 * @param        u1Type---- Video path number(SV_VP_MAIN/SV_VP_PIP)
*/
extern void vDvi2ChkModeChange(UINT8 u1Type);
/**
 * @brief        Set DVI mode done
 * @param        u1Type---- Video path number(SV_VP_MAIN/SV_VP_PIP)
*/
extern void vDvi2SetModeDone(UINT8 u1Type);
/**
 * @brief        DVI mode detect function before detect done
 * @param        u1Type---- Video path number(SV_VP_MAIN/SV_VP_PIP)
*/
extern void vDvi2ModeDetect(UINT8 u1Type);
/**
 * @brief        Get DVI porch
 * @param        bPath---- Video path number(SV_VP_MAIN/SV_VP_PIP)
 * @param        bPorchType---- Porch type, see Enum SV_xPORCH_xxxxx in sv_const.h
 * @retval
*/
extern UINT16 wDvi2GetPorch(UINT8 bPath, UINT8 bPorchType);
/**
 * @brief        Set DVI porch
 * @param        bPath---- Video path number(SV_VP_MAIN/SV_VP_PIP)
 * @param        bPorchType---- Porch type, see Enum SV_xPORCH_xxxxx in sv_const.h
 * @param        wValue---- Porch value
*/
extern void vDvi2SetPorch(UINT8 bPath, UINT8 bPorchType, UINT16 wValue);
/**
 * @brief        Check DVI detect done
 * @param        u1Type---- Video path number(SV_VP_MAIN/SV_VP_PIP)
 * @retval       TRUE for detect done, FALSE for not
*/
extern BOOL fgDvi2IsDetectDone(UINT8 u1Type);
/**
 * @brief        Get DVI timing
 * @param        u1Type---- Video path number(SV_VP_MAIN/SV_VP_PIP)
 * @retval       DVI timing
*/
extern UINT8 bDvi2GetTiming(UINT8 u1Type);
/**
 * @brief        Check DVI UHD timing or not
 * @param        u1Type---- Video path number(SV_VP_MAIN/SV_VP_PIP)
 * @retval       TRUE for UHD timing, FALSE for not
*/
extern BOOL fgDvi2IsUHDTiming(UINT8 u1Type);
/**
 * @brief        Print DVI debug message
 * @param        u1Type---- Video path number(SV_VP_MAIN/SV_VP_PIP)
*/
extern void vDvi2GetDebugMessage(UINT8 u1Type);

//Functions defined in drv_ccir_osd.c >>> BEGIN
extern void vCCIROSDOnOff(UINT8 fgIsOn);
extern void vCCIROSDInitial(void);
extern void vCCIROSDConnect(UINT8 bchannel, UINT8 fgIsOn) ;
extern void vCCIROSDConnect(UINT8 bchannel, UINT8 fgIsOn);
extern UINT16 wCCIROSDInputHTotal(void);
extern UINT16 wCCIROSDInputVTotal(void);
extern UINT16 wCCIROSDInputWidth(void);
extern UINT16 wCCIROSDInputHeight(void);
extern UINT8 bCCIROSDRefreshRate(void);
extern UINT8 bCCIROSDInterlace(void);
extern void vCCIROSDSETVSTART(void);
extern UINT8 bCCIROSDSigStatus(void);
extern void vCCIROSDDebugMsg(void);
//Functions defined in drv_ccir_osd.c >>> END
#endif
