/* 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: brianpc.huang $
 * $Date: 2015/05/29 $
 * $RCSfile: vga_table.h,v $
 * $Revision: #1 $
 *
 *---------------------------------------------------------------------------*/

#ifndef _VGA_TABLE_H_
#define _VGA_TABLE_H_

#include "general.h"

#define CO(_PRO_INT, _DIV_MUL, _ADCPLLDIV, _CLKIN_MCODE) ((_PRO_INT<<6) | ((_DIV_MUL>>1) <<5) |((_ADCPLLDIV>>1)<<4) |_CLKIN_MCODE)
#define Displaymode_delay 0 //Modify for Disaplymode delay

#ifndef _VGAMODE_STRUCT_
#define _VGAMODE_STRUCT_
typedef struct VGAMODE		// 14 bytes
{
	UINT16 IHF; // Horizontal Frequency for timing search
	UINT8 IVF; // Vertical Frequency for timing search
	UINT16 ICLK; // Pixel Frequency
	UINT16 IHTOTAL; // H Total
	UINT16 IVTOTAL; // V Total 
	UINT16 IPH_SYNCW; // H Sync Width
	UINT16 IPH_WID; // H Resolution
	UINT16 IPH_BP; // H Back Porch
	UINT16 IPV_STA; // V Back Porch + Sync Width
	UINT16 IPV_LEN; // V Resolution
	UINT16 COMBINE; // ??
	//UINT32 CLKIN_CWCODE; // ??
}  VGAMODE ; // using __attribute__((packed)) make armcc --gnu  internal fault ?
#endif


extern const UINT8 bHdtvTimings;
extern const UINT8 bVgaTimings;
extern const UINT8 bUserVgaTimings;
extern const UINT8 bAllTimings;

extern const UINT8 bUserVgaTimingBegin;

#if SUPPORT_VGA_USERMODE
#define  USERMODE_TIMING 8
typedef struct VGA_USRMODE	
{
	UINT16 vlen:11;	/* reference STA12 */
	UINT16 hsync_wvar:4;
	UINT16 vpol:1;	/* reference STA12 */
	UINT16 hlen:11;	/* reference STA12 */
	UINT16 hlen_var:4;
	UINT16 hpol:1;	/* reference STA12 */
	UINT8 hsync_w;	
	UINT8 isCVTRB:1;
	UINT8 unused:3;
	UINT8 forceXY:2;	/* force XY resolution*/
	UINT8 override:1; /* override preset timing */
	UINT8 id:1;		/*inverse to change return mode number */
} VGA_USRMODE;

typedef struct VGA_USRMODE_EXT
{
	UINT16	h_res;	//orig input h resolotion
} VGA_USRMODE_EXT;

extern VGA_USRMODE rVgaUsrEEP[USERMODE_TIMING];		//both  EEP & RAM
extern VGA_USRMODE_EXT rVgaUsrExt[USERMODE_TIMING]; //only on RAM
#endif

extern VGAMODE  VGATIMING_TABLE[];

// VGA Mode
extern UINT16 Get_VGAMODE_IHF(UINT8 mode) ;
extern UINT8 Get_VGAMODE_IVF(UINT8 mode) ;
extern UINT16 Get_VGAMODE_ICLK(UINT8 mode) ;
extern UINT16 Get_VGAMODE_IHTOTAL(UINT8 mode) ;
extern UINT16 Get_VGAMODE_IVTOTAL(UINT8 mode) ;
extern UINT16 Get_VGAMODE_IPH_STA(UINT8 mode) ;
extern UINT16 Get_VGAMODE_IPH_SYNCW(UINT8 mode) ;
extern UINT16 Get_VGAMODE_IPH_WID(UINT8 mode) ;
extern UINT16 Get_VGAMODE_IPH_BP(UINT8 mode) ;
extern UINT8 Get_VGAMODE_IPV_STA(UINT8 mode) ;
extern UINT16 Get_VGAMODE_IPV_LEN(UINT8 mode) ;
extern UINT16 Get_VGAMODE_COMBINE(UINT8 mode) ;
extern UINT8 Get_VGAMODE_OverSample(UINT8 mode) ;


//#define Get_VGAMODE_OverSample(bMode) (Get_VGAMODE_COMBINE(bMode)&0x01) //oversample
#define Get_VGAMODE_HSyncWidthChk(bMode) ((Get_VGAMODE_COMBINE(bMode)>>1)&0x01)
#define Get_VGAMODE_AmbiguousH(bMode) ((Get_VGAMODE_COMBINE(bMode)>>2)&0x01)
#define Get_VGAMODE_VPol(bMode) ((Get_VGAMODE_COMBINE(bMode)>>3)&0x01)
#define Get_VGAMODE_HPol(bMode) ((Get_VGAMODE_COMBINE(bMode)>>4)&0x01)
#define Get_VGAMODE_VSyncWidthChk(bMode) ((Get_VGAMODE_COMBINE(bMode)>>5)&0x01)
#define Get_VGAMODE_INTERLACE(bMode) ((Get_VGAMODE_COMBINE(bMode)>>6)&0x01)
#define Get_VGAMODE_PolChk(bMode) ((Get_VGAMODE_COMBINE(bMode)>>7)&0x01)
#define Get_HDMIMODE_SupportVideo(bMode) ((Get_VGAMODE_COMBINE(bMode)>>8)&0x01)
#define Get_VGAMODE_VgaDisabled(bMode) ((Get_VGAMODE_COMBINE(bMode)>>9)&0x01)
#define Get_VGAMODE_YpbprDisabled(bMode) ((Get_VGAMODE_COMBINE(bMode)>>10)&0x01)


#endif
