/* 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.
 *
 *****************************************************************************/


#ifndef _TVE_IF_H_
#define _TVE_IF_H_

#include "x_typedef.h"
#include "u_common.h"
#if (defined(CC_MT5881)||defined(CC_MT5399)||defined(CC_MT5890)|| defined(CC_MT5882)|| defined(CC_MT5883)||defined(CC_MT5891))
#include "feature.h"
#endif

#if (defined(CC_MT5365) || defined(CC_MT5395) || defined(CC_MT5368) || defined(CC_MT5396) || defined(CC_MT5389) || defined(CC_MT5398) || defined(CC_MT5880) || defined(CC_MT5881) || defined(CC_MT5399)||defined(CC_MT5890)|| defined(CC_MT5882)|| defined(CC_MT5883)||defined(CC_MT5891))
#define CC_TVE6595
#endif

#if(defined(CC_MT5881)||defined(CC_MT5399)||defined(CC_MT5890)|| defined(CC_MT5882)|| defined(CC_MT5883)||defined(CC_MT5891))
#define CC_TVE_SUPPORT_VDAC108M 1
#endif

/* TVE OUT Config */

#ifndef CC_MT5387
#define CC_TVE_INSERT_VPS
#endif

//ITU_R BT. 470.  Offset = 9 pixels
#define VIDEO_ALIGNMENT_SUPPORT 1
#if VIDEO_ALIGNMENT_SUPPORT
#define VIDEO_ALIGNMENT_OFFSET 10
#endif

enum
{
	SV_TVE_NA,
	SV_TVE_DAC_BYPASS_CVBS,		
	SV_TVE_DAC_BYPASS_DEMOD,
	SV_TVE_DAC_PIP,
	SV_TVE_DAC_VIDEO_IN, // Video In
	SV_TVE_HW_BYPASS, // Only for TV SCART
	SV_TVE_D2S_BYPASS, // Only for Monitor SCART
	SV_TVE_DAC_BYPASS_SIF,
	SV_TVE_MAX
};


/* TVE Video Status Notify Type */
enum
{	
	SV_TVE_NOTIFY_VIDEO_NO_SIGNAL,
	SV_TVE_NOTIFY_VIDEO_STABLE_SIGNAL,		
	SV_TVE_NOTIFY_VIDEO_MODE_CHANGE,
	SV_TVE_NOTIFY_VIDEO_UNMUTE,
	SV_TVE_NOTIFY_MAX
};


/* Scart Out ID */
enum
{
	SV_TVE_SCART_OUT_1,
	SV_TVE_SCART_OUT_2,
	SV_TVE_SCART_OUT_MAX
};

/* Scart Out Fmt */
enum
{
	SV_TVE_OFMT_PAL,
	SV_TVE_OFMT_NTSC,
	SV_TVE_OFMT_MAX,
	SV_TVE_OFMT_NONE = SV_TVE_OFMT_MAX
};

enum
{
    DAC_1_4_CURRENT = 0,
    DAC_1_2_CURRENT,
    DAC_3_4_CURRENT,
    DAC_FULL_CURRENT
};


/* SCART Src type */
#define TVE_SCART_DECODER 7

typedef struct TveUsage_s
{
    UINT8    TveIsUsedBy : 4;
    UINT8    TveReferenceCnt : 4;
} TveUsage_t;

#define TVE_DAC_MUTE_MASK_MW  (1)
#define TVE_DAC_MUTE_MASK_DRV (1<<1)

#define TVE_BYP_EXT_PATH   (1)
#define TVE_BYP_AVMUX_PATH (2)
#define TVE_BYP_PIP_PATH   (3)

#define TVE_BYP_EXT_MUX_PATH (0x80)

#include "x_typedef.h"
#include "x_os.h"

/* Maximum number of plane mixer */
#define TVE_1				0	/* Main display */
#define TVE_2				1	/* Aux display */
#define TVE_MAX_NS			2

/* TVE configuration return value */
#define TVE_SET_ERROR		0
#define TVE_SET_OK			1
#define TVE_SET_VBI_OVERFLOW	2

/* TVE output */
#define TVE_OUT_CVBS		0
#define TVE_OUT_S_VIDEO	    1
#define TVE_OUT_YBR			2
#define TVE_OUT_RGB			3
#define TVE_MAX_OUT			4

/* TVE mode */
#define TVE_MOD_COMPOSITE		0
#define TVE_MOD_COMPONENT		1
#define TVE_MOD_RGB			    2
#define TVE_MOD_VGA             TVE_MOD_RGB
#define TVE_MAX_MOD			    3

/* TVE format */

// Plane mixer mode
#define PMX_MODE_480I			0
#define PMX_MODE_576I			1
#define PMX_MODE_480P			2
#define PMX_MODE_576P			3
#define PMX_MODE_720P			4
#define PMX_MODE_1080I			5

#define PMX_MODE_768P			6
#define PMX_MODE_540P			7
#define PMX_MODE_768I			8
#define PMX_MODE_1536I			9
#define PMX_MODE_720I			10
#define PMX_MODE_1440I			11
#define PMX_MODE_UNKNOWN		12

#define TVE_FMT_480I                    PMX_MODE_480I
#define TVE_FMT_576I                    PMX_MODE_576I
#define TVE_FMT_480P                    PMX_MODE_480P
#define TVE_FMT_576P                    PMX_MODE_576P
#define TVE_FMT_720P                    PMX_MODE_720P
#define TVE_FMT_1080I                   PMX_MODE_1080I
#define TVE_FMT_768P                    PMX_MODE_768P
#define TVE_MAX_FMT                     2

/* TVE field */
#define TVE_FIELD_ODD			0
#define TVE_FIELD_EVEN			1
#define TVE_FIELD_BOTH			2

/* TVE Macrovision APS types */
#define TVE_MV_APS_Type0		0
#define TVE_MV_APS_Type1		1
#define TVE_MV_APS_Type2		2
#define TVE_MV_APS_Type3		3
#define TVE_MAX_MV_APS			4

#define TVE_MAX_MV_TEST_CASES   6

/* TVE Low/High Impedence */
#define TVE_LOW_IMPEDENCE       0   /* for MT5351_DEMO_BOARD */
#define TVE_HIGH_IMPEDENCE      1   /* for MT5351_M1_BOARD */

/* TVE WSS/CGMS Aspect Ratio
   NOR: Normal, LB: LetterBox, C: Center, T: Top, LG: Larger than
   525-Line (CGMS) */
#define CGMS_NOR_4_3			0
#define CGMS_NOR_16_9			1
#define CGMS_LB_4_3             2
#define CGMS_LB_16_9			3
/* 625-Line (WSS)  */
#define WSS_LB_14_9_C			1
#define WSS_LB_14_9_T			2
#define WSS_LB_16_9_T			4
#define WSS_ANA_16_9			7
#define WSS_FULL_4_3			8
#define WSS_LB_16_9_C			11
#define WSS_LB_LG_16_9_C		13
#define WSS_FULL_14_9_C			14

/* TVE Mixer select */
#define TVE_VDOIN_MIXER	0
#define TVE_DEMOD_MIXER	1
#define TVE_SIF_MIXER		2

//TVE field invert WA
#if defined(CC_MT5363) || defined(CC_TVE6595)
#define TVE_WA
#endif

/******************************************************************************
* TVE CTRL API
******************************************************************************/
void vApiTVESetNotifyFunc(void (*pSigChgNfy)(UINT8,UINT8), void (*pUnmuteNfy)(UINT8));
void vApiTVEVideoStatusNotify(UINT8 bPath, UINT8 bMode);
// Cassy
void vApiTVEVideoMuteChangeNotify(UINT8 bPath, UINT8 bMute);
extern void vApiTVESetDacMuteMask(UINT8 u1DacId, UINT8 u1Mask, UINT8 u1OnOff);
extern UINT8 bApiTVEGetBypPath(UINT8 u1DacId);
extern void vApiTVESetScartOutCtrl(UINT8 u1Scart_id, UINT8 *u1SrcType, void* pv_extra_arg);
extern void vApiTVESetOutputFormat(UINT32 u4fmt);
void vApiTVEQueryStatus(void);

/* customization API */
extern void vApiTVEOutPathSelect(UINT8 out_id, UINT8 out_type);
extern void vApiTVEGetConfig(UINT8 u1SrcType, UINT8* pu1Type);

extern UINT8 bApiTVEIsWorking(UINT8 u1DacId);


/******************************************************************************
* TVE API
******************************************************************************/
extern UINT32 TVE_Init(void);
extern UINT32 TVE_UnInit(void);
extern UINT32 TVE_Reset(UCHAR ucTveId);
extern UINT32 TVE_QueryStatus(void);

extern UINT32 TVE_SetEnable(UCHAR ucTveId, UCHAR ucEnable);
extern UINT32 TVE_GetEnable(UCHAR ucTveId, UCHAR* pucEnable);

extern UINT32 TVE_SetMode(UCHAR ucTveId, UCHAR ucMode);
extern UINT32 TVE_GetMode(UCHAR ucTveId, UCHAR* pucMode);

extern UINT32 TVE_SetFmt(UCHAR ucTveId, UCHAR ucFmt);
extern UINT32 TVE_GetFmt(UCHAR ucTveId, UCHAR* pucFmt);

extern UINT32 TVE_SetImpedence(UCHAR ucTveId, UCHAR ucImpedence);
extern UINT32 TVE_GetImpedence(UCHAR ucTveId, UCHAR* pucImpedence);

extern UINT32 TVE_SetCurrent(UCHAR ucDacId, UCHAR ucCurrent);

extern UINT32 TVE_SetColorBar(UCHAR ucTveId, UCHAR ucOn);
extern UINT32 TVE_SetCcEnable(UCHAR ucTveId, UCHAR ucEnable);
extern UINT32 TVE_GetCcEnable(UCHAR ucTveId, UCHAR* pucCcEnable);
extern UINT32 TVE_SendCc(UCHAR ucTveId, UCHAR ucField, const UCHAR *pucData, PTS_T u8Pts);
extern UINT32 TVE_SendCcIsr(UCHAR ucTveId, UCHAR ucIsOddField, UINT32 u4PmxPts);

extern UINT32 TVE_SetWssEnable(UCHAR ucTveId, UCHAR ucEnable);
extern UINT32 TVE_GetWssEnable(UCHAR ucTveId, UCHAR* pucWssEnable);
extern UINT32 TVE_SendWss(UCHAR ucTveId,const UCHAR *pu1Buf, UINT32 length);
extern UINT32 TVE_SetAspectRatio(UCHAR ucTveId, UCHAR ucAspectRatio);
extern UINT32 TVE_GetAspectRatio(UCHAR ucTveId, UCHAR* pucAspectRatio);
extern UINT32 TVE_SetCgmsaValue(UCHAR ucTveId, UCHAR ucValue);
extern UINT32 TVE_GetCgmsaValue(UCHAR ucTveId, UCHAR* pucValue);
#ifdef CC_TVE_INSERT_VPS//  for vps
extern UINT32 TVE_SetVPSEnable(UCHAR ucTveId, UCHAR ucEnable);
extern UINT32 TVE_GetVPSEnable(UCHAR ucTveId, UCHAR* pucVPSEnable);
extern UINT32 TVE_SendVPS(UCHAR ucTveId, const UCHAR *pucData);
#endif

extern UINT32 TVE_PlayCc(UCHAR ucTveId, UCHAR ucEsId);
extern UINT32 TVE_StopCc(UCHAR ucTveId, UCHAR ucEsId);
extern UINT32 TVE_SetTTXEnable(UCHAR ucTveId, UCHAR ucEnable);
extern UINT32 TVE_GetTTXEnable(UCHAR ucTveId, UCHAR* pucTTXEnable);
extern UINT32 TVE_SendTTX(UCHAR ucTveId,const UCHAR *pu1Buf, UINT32 length);

extern UINT32 TVE_DACPower(UCHAR ucTveId, UCHAR DAC, UCHAR ucEnable);
extern UINT32 TVE_OnInputVSync(void);
extern UINT32 TVE_OnOutputVSync(void);

extern void Bypass_Init(void);
extern UINT32 TVE_GetFieldInfo(UCHAR ucTveId);
extern void  _TVE_Mainloop(void);

extern UINT32 TVE_SelectInput(UCHAR ucTveSrc);
#if defined(CC_MT5363) || defined(CC_TVE6595)
extern UINT32 TVE_PIPMixer(UCHAR ucTveId, UCHAR ucEnable);
#endif
extern void _vApiTveSetSclPara(UINT8 u1OnOff, INT32 i4Width, INT32 i4Height);
#if defined(CC_SUPPORT_TVE) && defined(CC_SUPPORT_TVE_VIDEO_CLIP_BY_MTAL)
extern UINT32 TVE_SetHDispArea(UCHAR ucFmt, UCHAR bSrc, UINT16 u2BgnValue, UINT16 u2EndValue);
extern UINT32 TVE_SetVDispArea(UCHAR ucFmt, UINT16 u2BgnValue, UINT16 u2EndValue);
#endif
#if (defined(CC_MT5881)||defined(CC_MT5399)||defined(CC_MT5890)|| defined(CC_MT5882)|| defined(CC_MT5883)||defined(CC_MT5891))
#if CC_TVE_SUPPORT_VDAC_SOFTWARE_CAL
extern UINT32 TveSetAbistBypass(UINT8 bPattern);
extern UINT32 TveVdacStartSoftCalibration(void);
#if (defined(CC_MT5890)||defined(CC_MT5891))
extern UINT32 TveSetAbistDcWaveFullCmd(void);
extern UINT32 TveSetAbistDcWaveZeroCmd(void);
extern UINT32 TveReadAdcValueCmd(void);
#endif

#endif
#endif

extern BOOL TVE_SetEnableBypassMonOut(UCHAR ucTveId, BOOL ucEnable);
extern BOOL TVE_GetEnableBypassMonOut(UCHAR ucTveId);

#endif /* _TVE_IF_H_ */

