//<MStar Software>
//******************************************************************************
// MStar Software
// Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
// All software, firmware and related documentation herein ("MStar Software") are
// intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
// law, including, but not limited to, copyright law and international treaties.
// Any use, modification, reproduction, retransmission, or republication of all
// or part of MStar Software is expressly prohibited, unless prior written
// permission has been granted by MStar.
//
// By accessing, browsing and/or using MStar Software, you acknowledge that you
// have read, understood, and agree, to be bound by below terms ("Terms") and to
// comply with all applicable laws and regulations:
//
// 1. MStar shall retain any and all right, ownership and interest to MStar
//    Software and any modification/derivatives thereof.
//    No right, ownership, or interest to MStar Software and any
//    modification/derivatives thereof is transferred to you under Terms.
//
// 2. You understand that MStar Software might include, incorporate or be
//    supplied together with third party`s software and the use of MStar
//    Software may require additional licenses from third parties.
//    Therefore, you hereby agree it is your sole responsibility to separately
//    obtain any and all third party right and license necessary for your use of
//    such third party`s software.
//
// 3. MStar Software and any modification/derivatives thereof shall be deemed as
//    MStar`s confidential information and you agree to keep MStar`s
//    confidential information in strictest confidence and not disclose to any
//    third party.
//
// 4. MStar Software is provided on an "AS IS" basis without warranties of any
//    kind. Any warranties are hereby expressly disclaimed by MStar, including
//    without limitation, any warranties of merchantability, non-infringement of
//    intellectual property rights, fitness for a particular purpose, error free
//    and in conformity with any international standard.  You agree to waive any
//    claim against MStar for any loss, damage, cost or expense that you may
//    incur related to your use of MStar Software.
//    In no event shall MStar be liable for any direct, indirect, incidental or
//    consequential damages, including without limitation, lost of profit or
//    revenues, lost or damage of data, and unauthorized system use.
//    You agree that this Section 4 shall still apply without being affected
//    even if MStar Software has been modified by MStar in accordance with your
//    request or instruction for your use, except otherwise agreed by both
//    parties in writing.
//
// 5. If requested, MStar may from time to time provide technical supports or
//    services in relation with MStar Software to you for your use of
//    MStar Software in conjunction with your or your customer`s product
//    ("Services").
//    You understand and agree that, except otherwise agreed by both parties in
//    writing, Services are provided on an "AS IS" basis and the warranty
//    disclaimer set forth in Section 4 above shall apply.
//
// 6. Nothing contained herein shall be construed as by implication, estoppels
//    or otherwise:
//    (a) conferring any license or right to use MStar name, trademark, service
//        mark, symbol or any other identification;
//    (b) obligating MStar or any of its affiliates to furnish any person,
//        including without limitation, you and your customers, any assistance
//        of any kind whatsoever, or any information; or
//    (c) conferring any license or right under any intellectual property right.
//
// 7. These terms shall be governed by and construed in accordance with the laws
//    of Taiwan, R.O.C., excluding its conflict of law rules.
//    Any and all dispute arising out hereof or related hereto shall be finally
//    settled by arbitration referred to the Chinese Arbitration Association,
//    Taipei in accordance with the ROC Arbitration Law and the Arbitration
//    Rules of the Association by three (3) arbitrators appointed in accordance
//    with the said Rules.
//    The place of arbitration shall be in Taipei, Taiwan and the language shall
//    be English.
//    The arbitration award shall be final and binding to both parties.
//
//******************************************************************************
//<MStar Software>
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2008-2009 MStar Semiconductor, Inc.
// All rights reserved.
//
// Unless otherwise stipulated in writing, any and all information contained
// herein regardless in any format shall remain the sole proprietary of
// MStar Semiconductor Inc. and be kept in strict confidence
// ("MStar Confidential Information") by the recipient.
// Any unauthorized act including without limitation unauthorized disclosure,
// copying, use, reproduction, sale, distribution, modification, disassembling,
// reverse engineering and compiling of the contents of MStar Confidential
// Information is unlawful and strictly prohibited. MStar hereby reserves the
// rights to any and all damages, losses, costs and expenses resulting therefrom.
//
////////////////////////////////////////////////////////////////////////////////
#ifndef _DRV_PQ_BIN_H_
#define _DRV_PQ_BIN_H_

//#ifdef DRVPQ_BIN_C
//#define INTERFACE
//#else
//#define INTERFACE extern
//#endif

//-------------------------------------------------------------------------------------------------
//  Macro and Define
//-------------------------------------------------------------------------------------------------

#define PQ_BIN_MAX_IP                   (65535)
#define PQ_BIN_MAX_PNL                  (30)

#define LEN_HSY_CONFIG                  (2)
#define CFD_HSY_VERSION                 (1)
#define CFD_HSY_TABLE_VERSION           (1)
#define CFD_HSY_TABLE_ENTRY_VERSION     (1)

#define PQ_BIN_IP_ALL                   (0xFFFF)
#define PQ_BIN_IP_NULL                  (0xFFFF)
#define PQ_BIN_IP_COMM                  (0xFFFE)
#define PQ_BIN_ADDR_NULL                (0xFFFFFFFF)

#define PQ_BIN_BANK_SIZE                (1)
#define PQ_BIN_BANK_SIZE_2BYTE          (2)
#define PQ_BIN_ADDR_SIZE                (1)
#define PQ_BIN_MASK_SIZE                (1)

#define PQ_BIN_HEADER_LEN               (16)
#define PQ_BIN_VERSION_LEN              (16)
#define PQ_BIN_DUMMY                    (8)

#define PQ_TEXT_BIN_HEADER_LEN          (16)
#define PQ_TEXT_BIN_VERSION_LEN         (16)
#define PQ_BIN_IPNAME_LEN               (80)
#define PQ_TEXT_BIN_DUMMY               (8)

#define PQ_SIZE_1920                    (1920)
#define PQ_SIZE_1080                    (1080)

#define PQ_RATIO_1X                     (1000)

//Size of each table
#define PQ_BIN_GRULE_INFO_SIZE          (14)
#define PQ_BIN_GRULE_LEVEL_INFO_SIZE    (6)
#define PQ_BIN_XRULE_INFO_SIZE          (12)
#define PQ_BIN_SKIPRULE_INFO_SIZE       (6)
#define PQ_BIN_IP_COMM_INFO_SIZE        (16)
#define PQ_BIN_SOURCELUT_INFO_SIZE      (8)


// Item Offset of each table
// GRule
#define PQ_BIN_GRULE_HEADER_LEN             (16)
#define PQ_BIN_GRULE_RULE_NUM_LEN           (2)
#define PQ_BIN_GRULE_PANEL_NUM_LEN          (2)

#define PQ_BIN_GRULE_IP_NUM_OFFSET          (0)
#define PQ_BIN_GRULE_SOURCE_NUM_OFFSET      (PQ_BIN_GRULE_IP_NUM_OFFSET+2)
#define PQ_BIN_GRULE_GROUP_NUM_OFFSET       (PQ_BIN_GRULE_SOURCE_NUM_OFFSET+2)
#define PQ_BIN_GRULE_IPOFFSET_OFFSET        (PQ_BIN_GRULE_GROUP_NUM_OFFSET+2)
#define PQ_BIN_GRULE_RULEOFFSET_OFFSET      (PQ_BIN_GRULE_IPOFFSET_OFFSET+4)

#define PQ_BIN_GRULE_LEVEL_LVL_NUM_OFFSET   (0)
#define PQ_BIN_GRULE_LEVEL_OFFSET_OFFSET    (PQ_BIN_GRULE_LEVEL_LVL_NUM_OFFSET+2)


// XRule
#define PQ_BIN_XRULE_HEADER_LEN             (16)
#define PQ_BIN_XRULE_RULE_NUM_LEN           (2)

#define PQ_BIN_XRULE_IP_NUM_OFFSET          (0)
#define PQ_BIN_XRULE_GROUP_NUM_OFFSET       (PQ_BIN_XRULE_IP_NUM_OFFSET+2)
#define PQ_BIN_XRULE_IPOFFSET_OFFSET        (PQ_BIN_XRULE_GROUP_NUM_OFFSET+2)
#define PQ_BIN_XRULE_GROUPOFFSET_OFFSET     (PQ_BIN_XRULE_IPOFFSET_OFFSET+4)


// SkipRule
#define PQ_BIN_SKIPRULE_HEADER_LEN          (16)
#define PQ_BIN_SKIPRULE_IP_NUM_OFFSET       (0)
#define PQ_BIN_SKIPRULE_OFFSET_OFFSET       (PQ_BIN_SKIPRULE_IP_NUM_OFFSET+2)


// IP & Common
#define PQ_BIN_IP_COMM_HEADER_LEN           (16)
#define PQ_BIN_IP_COMM_IP_NUM_LEN           (2)
#define PQ_BIN_IP_COMM_TABLE_TYPE_OFFSET    (0)
#define PQ_BIN_IP_COMM_COMM_REG_NUM_OFFSET  (PQ_BIN_IP_COMM_TABLE_TYPE_OFFSET+2)
#define PQ_BIN_IP_COMM_IP_REG_NUM_OFFSET    (PQ_BIN_IP_COMM_COMM_REG_NUM_OFFSET+2)
#define PQ_BIN_IP_COMM_IP_GROUP_NUM_OFFSET  (PQ_BIN_IP_COMM_IP_REG_NUM_OFFSET+2)
#define PQ_BIN_IP_COMM_COMMOFFSET_OFFSET    (PQ_BIN_IP_COMM_IP_GROUP_NUM_OFFSET+2)
#define PQ_BIN_IP_COMM_IPOFFSET_OFFSET      (PQ_BIN_IP_COMM_COMMOFFSET_OFFSET+4)


// SourceLUT
#define PQ_BIN_SOURCELUT_HEADER_LEN         (16)
#define PQ_BIN_SOURCELUT_PANEL_NUM_LEN      (2)

#define PQ_BIN_SOURCELUT_SOURCE_NUM_OFFSET  (0)
#define PQ_BIN_SOURCELUT_IP_NUM_OFFSET      (PQ_BIN_SOURCELUT_SOURCE_NUM_OFFSET+2)
#define PQ_BIN_SOURCELUT_OFFSET_OFFSET      (PQ_BIN_SOURCELUT_IP_NUM_OFFSET+2)



#define PQ_BIN_IP_COMM_DATA_LEN             (1) // 1 byte


// Text Bin
//Size of each table
#define PQ_TEXT_BIN_SRCTYPE_INFO_SIZE       (6)
#define PQ_TEXT_BIN_IPNAME_INFO_SIZE        (6)
#define PQ_TEXT_BIN_GROUPNAME_INFO_SIZE     (6)

// SrcType
#define PQ_TEXT_BIN_SRCTYPE_HEADER_LEN      (16)
#define PQ_TEXT_BIN_SRCTYPE_SRCNUM_LEN      (2)
#define PQ_TEXT_BIN_SRCTYPE_SRC_NUM_OFFSET  (0)
#define PQ_TEXT_BIN_SRCTYPE_OFFSET_OFFSET   (PQ_TEXT_BIN_SRCTYPE_SRC_NUM_OFFSET+2)


// IPName
#define PQ_TEXT_BIN_IPNAME_HEADER_LEN       (16)
#define PQ_TEXT_BIN_IPNAME_IPNUM_LEN        (2)
#define PQ_TEXT_BIN_IPNAME_IPNUM_OFFSET     (0)
#define PQ_TEXT_BIN_IPNAME_OFFSET_OFFSET    (PQ_TEXT_BIN_IPNAME_IPNUM_OFFSET+2)

// GroupName
#define PQ_TEXT_BIN_GROUPNAME_HEADER_LEN                (16)
#define PQ_TEXT_BIN_GROUPNAME_IPNUM_LEN                 (2)
#define PQ_TEXT_BIN_GROUPNAME_IPGROUP_GROUPNUM_OFFSET   (0)
#define PQ_TEXT_BIN_GROUPNAME_IPGROUP_OFFSET_OFFSET     (PQ_TEXT_BIN_GROUPNAME_IPGROUP_GROUPNUM_OFFSET+2)

#define PQ_TEXT_BIN_GROUPNAME_GROUPOFFSET_LEN           (4)
#define PQ_TEXT_BIN_GROUPNAME_GROUPLENGTH_LEN           (2)
#define PQ_TEXT_BIN_GROUPNAME_GROUPOFFSET_OFFSET        (0)

// PnlType
#define PQ_BIN_PNLTYPE_1920           (0)
#define PQ_BIN_PNLTYPE_1366           (1)
#define PQ_BIN_PNLTYPE_4K2K           (2)

#define PQ_VR_BEGIN_STRING            (0xF000)
#define PQ_SC_BEGIN_STRING            (0x1300)
#define PQ_SC_EXT_BEGIN_STRING        (0x3300)
#if (ENABLE_PQ_BIN_HSY)
#if !defined(MSOS_TYPE_LINUX_KERNEL)
#define MEM_ALLOC(sz, num)  calloc(sz, num)
#define MEM_FREE(ptr)  free(ptr)
#else
#define MEM_ALLOC(sz, num)  kmalloc(sz*num,GFP_KERNEL)//calloc(sz, num)
#define MEM_FREE(ptr)  kfree(ptr)
#endif
/* fundamental structure define*/
#define  ST_RULE_MAX_IDX  3

/* predefined identifier */
#define   HEADER_TYPE_COMPACT   0x1
#define   HEADER_TYPE_DETAILED  0x2

#define ReportRetval(errno) do{\
        if (errno > 0)\
            printf(stderr, "[%s, %s():%d] %s\n", __FILE__, __func__, __LINE__, errorMsg[errno]);\
    }while(0)


#define FMT_RIU 1
#define FMT_ADL 2
#define FMT_DS  3

// enum for source
#define  SOURCE_MASK        0x01F00000
#define  SOURCE_HDMI        0x00100000
#define  SOURCE_AV          0x00200000
#define  SOURCE_LVDS        0x00300000
#define  SOURCE_DTV         0x00400000
#define  SOURCE_SCART       0x00500000
#define  SOURCE_PC          0x00600000
#define  SOURCE_DVI         0x00700000
#define  SOURCE_MM_PHOTO    0x00800000
#define  SOURCE_MM_ONLINE   0x00900000
#define  SOURCE_MM_VIDEO    0x00a00000

#define  TIMING_MASK        0x0000000f
#define  TIMING_480         0x00000001
#define  TIMING_576         0x00000002
#define  TIMING_720         0x00000003
#define  TIMING_1080        0x00000004
#define  TIMING_FHD         0x00000005
#define  TIMING_4K2K        0x00000006
#define  TIMING_8K2K        0x00000007

#define  INTERLACED_MASK    0x000c0000
#define  INTERLACED_P       0x00040000
#define  INTERLACED_I       0x00080000

#define  FRAME_RATE_MASK    0x3e000000
#define  FRAME_RATE_24      0x02000000
#define  FRAME_RATE_30      0x04000000
#define  FRAME_RATE_50      0x06000000
#define  FRAME_RATE_60      0x08000000
#define  FRAME_RATE_120     0x0a000000
#define  FRAME_RATE_144     0x0c000000
#define  FRAME_RATE_240     0x0e000000

#define  SCALING_MASK       0x0000F000
#define  SCALING_00x        0x00001000
#define  SCALING_01x        0x00002000
#define  SCALING_02x        0x00003000
#define  SCALING_03x        0x00004000
#define  SCALING_04x        0x00005000

#define  YUVRGB_MASK        0x0000C000
#define  YUVRGB_YUV         0x00004000
#define  YUVRGB_RGB         0x00008000

#define  GRULE_MASK         0x007C0000
#define  GRULE_TEST_OFF     0x00040000
#define  GRULE_TEST_BILINEAR   0x00080000

// enum for bin forest
#define  TEST_MODEL         0x0
#define  COLOR_BIN          0x0
#define  MAIN_BIN           0x1
#define  LEN_CONFIG         2

// idx for grule
#define  RULE_TEST          0
#define  XRULE_VSDRule      1

// macro for setup
#define  PQ_SET_SOURCE(config, filter)  do{\
         config[0] |= filter;\
} while(0)

#define  PQ_SET_TIMING(config, filter)  do{\
         config[0] |= filter;\
} while(0)

#define  PQ_SET_INTERLACED(config, filter) do{\
         config[0] |= filter;\
} while(0)

#define  PQ_SET_SCALING(config, filter) do{\
         config[0] |= filter;\
} while(0)

#define  PQ_SET_YUVRGB(config, filter) do{\
         config[0] |= filter;\
} while(0)

#define  PQ_SET_GRULE_IDX(config, filter) do{\
         config[0] |= filter;\
} while(0)

#define  COMPARE(label, rule) ( (( rule.pu32Mask[0] & label[0] ) == rule.pu32Label[0]) && (( rule.pu32Mask[1] & label[1] ) == rule.pu32Label[1]))

#define HSY_BIN_ID_OFFSET (1)
#endif

//-------------------------------------------------------------------------------------------------
//  enum
//-------------------------------------------------------------------------------------------------
#if (ENABLE_PQ_BIN_HSY)
typedef enum
{
    E_HSY_BIN_ID_HSY,           // [0] + HSY_BIN_ID_OFFSET = REAL HSY_ID
    E_HSY_BIN_ID_PQGAMMA,       // [1] + HSY_BIN_ID_OFFSET = REAL PQGAMMA_ID
    E_HSY_BIN_ID_OSDGAMMA,      // [2] + HSY_BIN_ID_OFFSET = REAL OSDGAMMA_ID
    E_HSY_BIN_ID_PANELGAMMA,    // [3] + HSY_BIN_ID_OFFSET = REAL PANELGAMMA_ID
    E_HSY_BIN_ID_LINEARRGB,     // [4] + HSY_BIN_ID_OFFSET = REAL LINEARRGB_ID
    E_HSY_BIN_ID_THREEDLUT,     // [5] + HSY_BIN_ID_OFFSET = REAL THREEDLUT_ID
    E_HSY_BIN_ID_HDR,           // [6] + HSY_BIN_ID_OFFSET = REAL HDR_ID
    E_HSY_BIN_ID_HDR_THREEDLUT, // [7] + HSY_BIN_ID_OFFSET = REAL HDR_THREEDLUT_ID
    E_HSY_BIN_ID_HSY2,          // [8] + HSY_BIN_ID_OFFSET = REAL HSY2_ID
    E_HSY_BIN_ID_MAX,
}EN_HSY_BIN_ID_TYPE;
#endif

typedef enum
{
    PQ_BIN_DISPLAY_ONE = 0,
    PQ_BIN_DISPLAY_PIP,
    PQ_BIN_DISPLAY_POP,
    PQ_BIN_DISPLAY_NUM,
}PQ_BIN_DISPLAY_TYPE;

//Local Fuction use
//MS_U16 _MDrv_PQ_PictureModeIdxMapping(EN_HSY_GRULE_IDX enGRuleTable)
typedef enum
{
    /// PICTUREMODE
   E_GRULE_IDX_PICTUREMODE_VIVID = 0,
   E_GRULE_IDX_PICTUREMODE_STANDARD,
   E_GRULE_IDX_PICTUREMODE_AUTO,
   E_GRULE_IDX_PICTUREMODE_MOVIE,
   E_GRULE_IDX_PICTUREMODE_PC,
   E_GRULE_IDX_PICTUREMODE_USER,
   E_GRULE_IDX_PICTUREMODE_GAME,
   E_GRULE_IDX_PICTUREMODE_OFF,
   /// THE MAX SUPPORT NUMBER OF PICTUREMODE GRULE INDEX
   E_GRULE_IDX_PICTUREMODE_MAX,
}EN_PQ_PICTUREMODE_GRULE_IDX;

//-------------------------------------------------------------------------------------------------
//  Type and Structure
//-------------------------------------------------------------------------------------------------

typedef struct
{
    MS_U16 u16IPNum;
    MS_U16 u16SourceNum;
    MS_U16 u16GroupNum;
    MS_U32 u32IPOffset;
    MS_U32 u32RuleOffset;
}MS_PQBin_GRule_Info;

typedef struct
{
    MS_U16 u16LvlNum;
    MS_U32 u32Offset;
}MS_PQBin_GRule_Level_Info;

typedef struct
{
    MS_U16 u16IPNum;
    MS_U16 u16GroupNum;
    MS_U32 u32IPOffset;
    MS_U32 u32GroupOffset;
}MS_PQBin_XRule_Info;


typedef struct
{
    MS_U16 u16IPNum;
    MS_U32 u32Offset;
}MS_PQBin_SkipRule_Info;


typedef struct
{
    MS_U16 u16SourceNum;
    MS_U16 u16IPNum;
    MS_U32 u32Offset;
}MS_PQBin_SourceLUT_Info;


typedef struct
{
    MS_U16 u16TableType;
    MS_U16 u16CommRegNum;
    MS_U16 u16IPRegNum;
    MS_U16 u16IPGroupNum;
    MS_U32 u32CommOffset;
    MS_U32 u32IPOffset;
}MS_PQBin_IP_Common_Info;

typedef struct
{
    MS_U8 u8BinID;
    MS_PHYADDR u32BinStartAddress;
    char  u8Header[PQ_BIN_HEADER_LEN];
    char  u8Version[PQ_BIN_VERSION_LEN];
    MS_U32 u32StartOffset;
    MS_U32 u32EndOffset;
    MS_U8  u8Dummy[PQ_BIN_DUMMY];

    // GRule
    char   u8GRuleHader[PQ_BIN_HEADER_LEN];
    MS_U16 u16GRule_RuleNum;
    MS_U16 u16GRule_PnlNum;
    MS_U32 u32GRule_Offset;
    MS_U32 u32GRule_Pitch;
    MS_U32 u32GRule_Lvl_Offset;
    MS_U32 u32GRule_Lvl_Pitch;

    // XRule
    char   u8XRuleHader[PQ_BIN_HEADER_LEN];
    MS_U16 u16XRuleNum;
    MS_U32 u32XRule_Offset;
    MS_U32 u32XRUle_Pitch;

    // SkipRule
    char   u8SkipRuleHader[PQ_BIN_HEADER_LEN];
    MS_U16 u16SkipRule_IPNum;
    MS_U32 u32SkipRule_Offset;

    // IP & Common
    char   u8IP_Comm_Header[PQ_BIN_HEADER_LEN];
    MS_U16 u16IP_Comm_Num;
    MS_U32 u32IP_Comm_Offset;
    MS_U32 u32IP_Comm_Pitch;


    // SourceLUT
    MS_U8  u8SourceLUT_Header[PQ_BIN_HEADER_LEN];
    MS_U16 u16SourceLUT_PnlNum;
    MS_U32 u32SourceLUT_Offset;
    MS_U32 u32SourceLUT_Pitch;
}MS_PQBin_Header_Info;

typedef struct
{
    MS_U8 u8BinID;
    MS_PHYADDR u32BinStartAddress;
    char  u8Header[PQ_TEXT_BIN_HEADER_LEN];
    char  u8Version[PQ_TEXT_BIN_HEADER_LEN];
    MS_U32 u32StartOffset;
    MS_U32 u32EndOffset;
    MS_U8  u8Dummy[PQ_TEXT_BIN_HEADER_LEN];

    //SourceType
    char   u8SrcTypeHeader[PQ_TEXT_BIN_HEADER_LEN];
    MS_U16 u16SrcNum;
    MS_U32 u32SrcType_Offset;
    MS_U32 u32SrcType_Pitch;

    //IPName
    char   u8IPNameHeader[PQ_TEXT_BIN_HEADER_LEN];
    MS_U16 u16IPNum;
    MS_U32 u32IPName_Offset;
    MS_U32 u32IPName_Pitch;

    //GroupName
    char   u8GroupNameHeader[PQ_TEXT_BIN_HEADER_LEN];
    MS_U16 u16GroupIPNum;
    MS_U32 u32IP_Group_Offset;
    MS_U32 u32IP_Group_pitch;
}MS_PQTextBin_Header_Info;

typedef struct
{
    MS_PHYADDR u32TabOffset;
    MS_U16 u16RegNum;
    MS_U16 u16TableType;
    MS_U16 u16GroupNum;
    MS_U16 u16GroupIdx;
}MS_PQBin_IP_Table_Info;

typedef struct
{
    MS_U32   u32Addr;
    MS_U32   u32Mask;
    MS_U32   u32Value;
}MS_RIU_UNIT;

#if (ENABLE_PQ_BIN_HSY)
typedef struct {
    MS_U32 u32Bank;
    MS_U32 u32RegNum;
    MS_RIU_UNIT pReg[];
}MS_RIU;


typedef struct {
    MS_U16  u16RegNum;
    MS_U64  pReg[];
}MS_DS;

typedef struct {
    MS_U16  u16ClientIdentifier;
    MS_U16  u16Version;
    MS_U32  u32DstAddress;
    MS_U8   pValue[];
}MS_ADL;

typedef struct ST_BIN_FOREST_HEADER_ {
    char   u8Description[40];
    MS_U8  u8Empty[4];
    MS_U32 u32Version;
    MS_U32 u32Size;
    MS_U16 u16ModelNum;
    MS_U16 u16MaxBinPerModel;
    // for the rest of the fields, we need to sequentially parse them
}MS_BIN_FOREST_HEADER;

typedef struct ST_BIN_HEADER_ {
    MS_U32 u32BinSize;
    MS_U32 u32CrcChecksum;
    MS_U8  u8Description[62];
    MS_U16 u16ChipId;
    MS_U32 u32RuleSectionOffset;
    MS_U32 u32IndexNum;
    MS_U32 u32ExtRuleNum;
}MS_BIN_HEADER;

typedef struct ST_RULE_HEADER_ {
    MS_U8  u8HeaderType;
    MS_U8  u8LabelSize;
    MS_U16 u16EntryNum;
    MS_U8  u8RuleName[16];
    MS_U32 u32EntryIndexMaxNum;
    MS_U32 u32MappingTableOffset;
}MS_RULE_HEADER;

typedef struct ST_RULE_HEADER_DETAILED_ {
    MS_U16 u16SkipRuleNum;
    MS_U16 u16IpNameSize;
    MS_U32 u32SkipRuleOffset;
    MS_U8  u8IpName[];
}MS_RULE_HEADER_DETAILED;

typedef struct ST_EXTRA_OPTION_{
    MS_U8  u8OptionName[31];
    MS_U8  u8MaskOffset;
    MS_U32 u32OptionLabel;
}MS_EXTRA_OPTION;

typedef struct ST_RULE_HEADER_EXTRA_OPTION_{
    MS_U32 u32ExtraOptionNum;
    MS_EXTRA_OPTION pOption[];
}MS_RULE_HEADER_EXTRA_OPTION;

typedef struct ST_CONTAINER_ {
    MS_U32 u32Type;
    MS_U8  u8Config[];
}MS_CONTAINER;

typedef struct ST_HETERO_CONFIG_ {
    MS_U32   u32Size;
    MS_U16   u16ContainerNum;
    MS_U16   u16ChipId;
    MS_U8    u8ConfigDescript[32];
    MS_CONTAINER* pContainers[];
}MS_HETERO_CONFIG;

/***********************************************
*
*¡ô STRUCT for parsing
*¡õ STRUCT for storage
*
***********************************************/
typedef struct ST_RULE_ {
    MS_U32* pu32Label;
    MS_U32* pu32Mask;
    MS_U32* pu32Idx;
}MS_RULE;

typedef MS_U8* ST_SKIPRULE;

typedef struct ST_RULE_SECTION_ {
    MS_RULE_HEADER *pRuleHeader;
    MS_RULE *pRule;
    MS_RULE_HEADER_DETAILED *pRuleHeaderDetailed;
    MS_RULE_HEADER_EXTRA_OPTION *pRuleHeaderExtraOption;
    ST_SKIPRULE  *pSkipRule;
}MS_RULE_SECTION;

typedef struct ST_BIN_MAP_{
    MS_U8* pu8Cache;
    MS_U8* pu8CacheCurr;
    MS_U8* pu8CacheEnd;
    MS_BIN_HEADER* pBinHeader;
    MS_RULE_SECTION* pRuleSection;
    MS_RULE_SECTION** pExtRuleSection;
    MS_HETERO_CONFIG** pIndexedConfig;
}MS_BIN_MAP;

typedef struct ST_FOREST_BIN_INFO_
{
    MS_U32 u32Offset;
    MS_U32 u32Id;
}MS_FOREST_BIN_INFO;

typedef struct ST_BIN_FOREST_MODEL_HEADER_ {
    MS_U32  u32ModelId;
    MS_U16  u16BinNum;
    MS_FOREST_BIN_INFO stBinInfo[];
} MS_FOREST_MODEL_HEADER;

typedef struct MS_FOREST_MAP_ {
    MS_BIN_FOREST_HEADER  stForestHeader;
    MS_FOREST_MODEL_HEADER  **pForestModelHeader;
}MS_FOREST_MAP;


typedef enum
{
    E_COLOR_SUCCESS,
    E_COLOR_IO_ERROR,
    E_COLOR_MEM_ERROR,
    E_COLOR_FMT_ERROR,
    E_COLOR_RESULT_MAX,
}MS_COLOR_RETURN_VALUE;
#endif

typedef enum
{
    E_DS_MODULE_IP,
    E_DS_MODULE_OP,
    E_DS_MODULE_FRC_IP,
    E_DS_MODULE_FRC_OP,
    E_DS_MODULE_UCMR,
    E_DS_MODULE_MAX,
}EN_DS_MODULE_SEL;
//cfd ctrl: set bin data

typedef enum
{
    E_PQ_DUMPGENERALBANK_VR,
    E_PQ_DUMPGENERALBANK_SC,
    E_PQ_DUMPGENERALBANK_MAX,
}EN_PQ_DUMPGENERALBANK_TYPE;


typedef struct
{
    MS_U32 u32Bank;
    MS_U32 u32RegNum;
    MS_U32 u32Offset;
} ST_RIU_ENTRY;

typedef struct __attribute__((packed))
{
    /// Structure version
    MS_U32 u32Version;
    /// Structure length
    MS_U16 u16Length;

    MS_U32  pu32Label[LEN_HSY_CONFIG];
    MS_U32  pu32Mask[LEN_HSY_CONFIG];

    //RIU
    ST_RIU_ENTRY  *pstRIU;
    MS_U16 u16RIURegGroupNum;

    //DS
    MS_U16  u16RegNum;
    MS_U32  u32DsScriptOffset;

    //ADL
    MS_U16  u16ClientIdentifier;
    MS_U32  u32Size;
    MS_U32  u32AdlOffset;
} ST_XC_PQ_HSY_TABLE_ENTRY;

typedef struct __attribute__((packed))
{
    /// Structure version
    MS_U32 u32Version;
    /// Structure length
    MS_U16 u16Length;
    //
    ST_XC_PQ_HSY_TABLE_ENTRY *pstTableEntry;
#if !defined (__aarch64__)
    /// Dummy parameter
    void *pDummy0;
#endif
    ST_XC_PQ_HSY_TABLE_ENTRY **pstGruleTableEntry;
#if !defined (__aarch64__)
    /// Dummy parameter
    void *pDummy1;
#endif
    MS_U8 *pu8Config;
#if !defined (__aarch64__)
    /// Dummy parameter
    void *pDummy2;
#endif
    MS_U32 u32ConfigSize;
    MS_U16 u16EntryNum;
    MS_U32 u32GRuleIPNum;
    MS_U16 *pu16EntryNumGRule;
#if !defined (__aarch64__)
    /// Dummy parameter
    void *pDummy3;
#endif

} ST_XC_PQ_HSY_TABLE;

//cfd ctrl: set HSY
typedef struct __attribute__((packed))
{
    /// Structure version
    MS_U32 u32Version;
    /// Structure length
    MS_U16 u16Length;
    /// Window (main or sub window)
    MS_U8 u8Win;
    /// HSY Type
    MS_U32 *pu32PQSrcTypeHSY;
#if !defined (__aarch64__)
    void    *pDummy0;
#endif
} ST_XC_CFD_HSY;

void MDrv_PQBin_Parsing(void *pInstance,MS_PQBin_Header_Info *pPQBinHeader);
void MDrv_PQBin_LoadCommTable(void *pInstance,MS_U16 u16PnlIdx, MS_PQBin_Header_Info *pPQBinHeader);
MS_U16 MDrv_PQBin_GetIPNum(void *pInstance,const MS_PQBin_Header_Info* const pPQBinHeader);
MS_U16 MDrv_PQBin_GetCurrentTableIndex(void *pInstance,MS_U16 u16PnlIdx, MS_U16 u16PQIPIdx);
MS_U16 MDrv_PQBin_GetTableNum(void *pInstance,MS_U16 u16PQIPIdx, const MS_PQBin_Header_Info* const pPQBinHeader);
void MDrv_PQBin_LoadTableData(void *pInstance,MS_U16 u16PnlIdx, MS_U16 u16TabIdx, MS_U16 u16PQIPIdx, MS_PQBin_Header_Info *pPQBinHeader, MS_U8 *pTable, MS_U16 u16TableSize);
void MDrv_PQBin_LoadTable(void *pInstance,MS_U16 u16PnlIdx, MS_U16 u16TabIdx, MS_U16 u16PQIPIdx, MS_PQBin_Header_Info *pPQBinHeader,PQ_WIN eWindow);
void MDrv_PQBin_CloneTable(void *pInstance,MS_U16 u16PnlIdx, MS_U16 u16TabIdx, MS_U16 u16PQMainIPIdx, MS_U16 u16PQSubIPIdx, MS_PQBin_Header_Info *pPQBinHeader);
void MDrv_PQBin_GetTable(void *pInstance,MS_U16 u16PnlIdx, MS_U16 u16TabIdx, MS_U16 u16PQIPIdx, MS_PQBin_IP_Table_Info *pTableInfo, const MS_PQBin_Header_Info* const pPQBinHeader);
void MDrv_PQBin_DumpTable(void *pInstance,MS_PQBin_IP_Table_Info* pTabInfo);
void MDrv_PQBin_DumpTable_Main2(void *pInstance,MS_PQBin_IP_Table_Info* pTabInfo);
MS_U16 MDrv_PQBin_GetTableIndex(void *pInstance,MS_U16 u16PQSrcType, MS_U16 u16PQIPIdx, MS_U16 u16PQPnlIdx, const MS_PQBin_Header_Info* const pPQBinHeader);
MS_U16 MDrv_PQBin_GetSkipRule(void *pInstance,MS_U16 u16PQIPIdx, const MS_PQBin_Header_Info* const pPQBinHeader);
void MDrv_PQBin_LoadTableBySrcType(void *pInstance,MS_U16 u16PQSrcType, MS_U16 u16PQIPIdx, MS_U16 u16PQPnlIdx, MS_PQBin_Header_Info *pPQBinHeader, PQ_WIN eWindow);


MS_U16 MDrv_PQBin_GetXRuleTableIndex(void *pInstance,MS_U16 u16XRuleType, MS_U16 u16XRuleIdx, MS_U16 u16XRuleIP, const MS_PQBin_Header_Info* const pPQBinHeader);
MS_U16 MDrv_PQBin_GetXRuleIPIndex(void *pInstance,MS_U16 u16XRuleType, MS_U16 u16XRuleIP, const MS_PQBin_Header_Info* const pPQBinHeader);
MS_U16 MDrv_PQBin_GetXRuleIPNum(void *pInstance,MS_U16 u16XRuleType, const MS_PQBin_Header_Info* const pPQBinHeader);


MS_U16 MDrv_PQBin_GetGRule_LevelIndex(void *pInstance,MS_U16 u16PnlIdx, MS_U16 u16GRuleType, MS_U16 u16GRuleLvlIndex, const MS_PQBin_Header_Info* const pPQBinHeader);
MS_U16 MDrv_PQBin_GetGRule_IPIndex(void *pInstance,MS_U16 u16GRuleType, MS_U16 u16GRuleIPIndex, const MS_PQBin_Header_Info* const pPQBinHeader);
MS_U16 MDrv_PQBin_GetGRule_TableIndex(void *pInstance,MS_U16 u16GRuleType, MS_U16 u16PQSrcType, MS_U16 u16PQ_NRIdx, MS_U16 u16GRuleIPIndex, const MS_PQBin_Header_Info* const pPQBinHeader);
MS_U16 MDrv_PQBin_GetGRule_GroupNum(void *pInstance,MS_U16 u16GRuleType, const MS_PQBin_Header_Info* const pPQBinHeader);
MS_U16 MDrv_PQBin_GetGRule_IPNum(void *pInstance,MS_U16 u16GRuleType, const MS_PQBin_Header_Info* const pPQBinHeader);
MS_U16 MDrv_PQBin_GetGRule_LvlNum(void *pInstance,    MS_U16 u16PnlIdx,    MS_U16 u16GRuleType,    const MS_PQBin_Header_Info* const pPQBinHeader);
void MDrv_PQBin_DumpVirtualRegTable(void *pInstance,const MS_PQBin_IP_Table_Info* const pTabInfo);
MS_BOOL MDrv_PQBin_SetVRAddress(void *pInstance,MS_PHYADDR u32VRAddress, MS_PHYADDR u32VRSize);

void MDrv_PQBin_SetDisplayType(void *pInstance,PQ_BIN_DISPLAY_TYPE enDisplayType);
void MDrv_PQBin_CheckGRule(MS_BOOL bCheckGRule);
PQ_BIN_DISPLAY_TYPE MDrv_PQBin_GetDisplayType(void *pInstance);
MS_BOOL MDrv_PQBin_VR_WriteByte(MS_U32 u32AddrOffset, MS_U8 u8Value);
MS_U8 MDrv_PQBin_VR_ReadByte(MS_U32 u32AddrOffset);
MS_U32 MDrv_PQBin_VR_Read4Byte(MS_U32 u32AddrOffset);
MS_U16 MDrv_PQBin_VR_Read2Byte(MS_U32 u32AddrOffset);
void  MDrv_PQBin_SetPanelID(void *pInstance,MS_U8 u8PnlID);
MS_U8 MDrv_PQBin_GetPanelIdx(void *pInstance);
MS_U8 MDrv_PQBin_GetPanelIdx_UFSC(void *pInstance);

void MDrv_PQBin_Set_MLoadEn(void *pInstance,MS_BOOL bEn);
void MDrv_PQBin_DS_GetTable(void* pInstance,PQ_WIN eWindow, MS_U16 u16TabIdx, MS_U16 u16PQIPIdx, PQ_ENGINE_TYPE ePQEngineType, EN_DS_MODULE_SEL eDS_SEL,MS_BOOL bCheckGRule);
void MDrv_PQBin_CheckCommTable(void *pInstance,MS_U16 u16PnlIdx, MS_PQBin_Header_Info * pPQBinHeader);
void MDrv_PQBin_CheckTableBySrcType(void *pInstance,MS_U16 u16PQSrcType, MS_U16 u16PQIPIdx, MS_U16 u16PQPnlIdx, MS_PQBin_Header_Info * pPQBinHeader, PQ_WIN eWindow);


void MDrv_PQTextBin_Parsing(void *pInstance,MS_PQTextBin_Header_Info *pPQTextBinHeader);
MS_U32 MDrv_PQTextBin_GetSrcType(void *pInstance,MS_U16 u16SrcIdx, const MS_PQTextBin_Header_Info* const pPQTextBinHeader);
MS_U32 MDrv_PQTextBin_GetIPName(void *pInstance,MS_U16 u16PQIPIdx, const MS_PQTextBin_Header_Info* const pPQTextBinHeader);
MS_U32 MDrv_PQTextBin_GetTableName(void *pInstance,MS_U16 u16PQIPIdx, MS_U16 u16TabIdx, const MS_PQTextBin_Header_Info* const pPQTextBinHeader);
MS_BOOL MDrv_PQBin_Enable8LBMode(void *pInstance,MS_BOOL bEnable);

void MDrv_PQBin_DumpUFSCScalerRegTable(void *pInstance, const MS_PQBin_IP_Table_Info* const pTabInfo);
void MDrv_PQBin_DumpUFSCFilterTable(void *pInstance, MS_PQBin_IP_Table_Info *pTabInfo);

#if (ENABLE_PQ_BIN_HSY)
MS_BOOL MDrv_HSYBin_GetGRuleParse(void* pInstance, MS_BIN_MAP* pbinMap, MS_U32 u32GRuleIP, MS_U32 u32label[LEN_CONFIG]);
MS_COLOR_RETURN_VALUE MDrv_ColorBin_Parse(MS_U8* pu8BinAddr, MS_FOREST_MAP* pForestMap);
MS_COLOR_RETURN_VALUE MDrv_ColorBin_ParseBin(MS_U8* pu8BinAddr, MS_BIN_MAP* binMap);
MS_U16 MDrv_HSYBin_GetBinID(EN_HSY_BIN_ID_TYPE u16HSYBinID);
MS_BOOL MDrv_PQBin_SearchRule(void* pInstance, MS_U32 u32label[LEN_CONFIG] ,MS_RULE_SECTION *pRuleSection, MS_BIN_MAP *pMap);
void MDrv_HSYBin_Parse(MS_U8* pu8BinAddr,MS_FOREST_MAP* pForestMap,MS_BIN_MAP* pbinMap);
MS_BOOL MDrv_HSYBin_ExtractRule( MS_RULE_SECTION *pstRuleSection, MS_BIN_MAP *pstMap, ST_XC_PQ_HSY_TABLE_ENTRY *pstHSYTableEntry);
MS_BOOL MDrv_HSYBin_FREE_HSYTable(MS_RULE_SECTION *pstRuleSection, MS_BIN_MAP *pstMap, ST_XC_PQ_HSY_TABLE_ENTRY *pstHSYTableEntry);
#endif
MS_BOOL MDrv_PQBin_DumpGeneralBankRegTable(void * pInstance,const MS_PQBin_IP_Table_Info* const pTabInfo);

MS_BOOL MDrv_PQBin_GetGRule_GroupIPNum(void *pInstance, PQ_WIN eWindow,MS_U16 u16GRuleType,PQ_BIN_TYPE enPQBinType ,MS_U16* u16GroupNum,MS_U16* u16IPNum ,const MS_PQBin_Header_Info* const pPQBinHeader);

void MDrv_PQBin_GetVirtualRegValue(void *pInstance, const MS_PQBin_IP_Table_Info* const pTabInfo, MS_RIU_UNIT *stTableInfo);
void MDrv_PQBin_GetTableValue(void *pInstance,MS_U16 u16PnlIdx,MS_U16 u16TabIdx,MS_U16 u16PQIPIdx,MS_PQBin_Header_Info *pPQBinHeader,MS_RIU_UNIT *pstTableInfo, PQ_WIN eWindow);
MS_BOOL MDrv_PQBin_GeneralSetPanelIdx(void *pInstance,PQ_BIN_TYPE enPQBinType, MS_U16 u16PnlIdx);
MS_U16 MDrv_PQBin_GeneralGetPanelIdx(void *pInstance,PQ_BIN_TYPE enPQBinType);
void MDrv_PQBin_GetGeneralBankRegTable(void *pInstance,const MS_PQBin_IP_Table_Info* const pTabInfo,MS_RIU_UNIT *pstTableInfo);
void MDrv_PQ_ResetFilterPara(void);
#endif
