/*
 * @file hv_app_DdcciFactory.h
 * @brief Header file of hisense ddcci factory.
 *
 * @verbatim
 * ==============================================================================
 *                        ##### How to use #####
 * ==============================================================================
 * (+) Use 
 *
 * @author HiView SoC Software Team
 * @version 1.0.0
 * @date 2023-12-20
 */



#ifndef _HV_APP_DDCCI_HISENSE_FACTORY_H
#define _HV_APP_DDCCI_HISENSE_FACTORY_H

#include "hv_comm_DataType.h"
#include "hv_vos_Comm.h"
#include "hv_comm_Define.h"


/*
Standard Examples
Read:
ddc cmd: [7] = 51 84 01 f8 e1 54 f7 
ddc send long [11] = 6e 88 02 00 f8 00 00 00 00 00 4c 

Read:
ddc cmd: [6] = 51 83 f3 01 04 4a 
ddc send long [32] = 
    00000000: 6e 9d e3 01 04 20 38 30 20 38 31 20 38 32 20 38 
    00000010: 33 20 38 34 20 38 35 20 38 36 20 38 37 20 38 5d

Set:
ddc cmd: [9] = 51 86 03 f8 e1 59 00 01 fb 
*/


//Read Reg: 51 CmdType CmdH CmdL LenH LenL AddrMsb Addr2nd Addr3rd AddrLsb StartBit StopBit Crc  (LenH LenL: 00 09)
//response: 6E CmdType CmdH CmdL LenH LenL AddrMsb Addr2nd Addr3rd AddrLsb StartBit StopBit ValMsb Val2nd Val3rd ValLsb Crc  (LenH LenL: 00 0D)

//Write Reg: 51 CmdType CmdH CmdL LenH LenL AddrMsb Addr2nd Addr3rd AddrLsb StartBit StopBit ValMsb Val2nd Val3rd ValLsb Crc  (LenH LenL: 00 0D)
//response: 6E CmdType CmdH CmdL LenH LenL AddrMsb Addr2nd Addr3rd AddrLsb StartBit StopBit RC Crc  (LenH LenL: 00 0A)

//Read DDR: 51 CmdType CmdH CmdL LenH LenL AddrMsb Addr2nd Addr3rd AddrLsb ReadLenH ReadLenL Crc  (LenH LenL: 00 09)
//response: 6E CmdType CmdH CmdL LenH LenL AddrMsb Addr2nd Addr3rd AddrLsb ReadLenH ReadLenL Data[ReadLen] Crc  (LenH LenL: ReadLen + 9, Total:ReadLen + 9 + 4)

//Write DDR: 51 CmdType CmdH CmdL LenH LenL AddrMsb Addr2nd Addr3rd AddrLsb Data[WriteLen] Crc  (LenH LenL: WriteLen + 7 + 4)
//response: 6E CmdType CmdH CmdL LenH LenL AddrMsb Addr2nd Addr3rd AddrLsb RC Crc  (LenH LenL: 00 08)

//PQ CMD Read: 51 CmdType CmdH CmdL LenH LenL Crc  (LenH LenL: 00 03)
//response: 6E CmdType CmdH CmdL LenH LenL ValMsb Val2nd Val3rd ValLsb Crc  (LenH LenL: 00 07)

//PQ CMD Write: 51 CmdType CmdH CmdL LenH LenL ValMsb Val2nd Val3rd ValLsb Crc
//response: 6E CmdType CmdH CmdL LenH LenL RC Crc  (LenH LenL: 00 04)

//PQ CMD Write Long: 51 CmdType CmdH CmdL LenH LenL Data[WriteLen] Crc
//response: 6E CmdType CmdH CmdL LenH LenL RC Crc  (LenH LenL: 00 04)

//Write GammaLut: 51 CmdType CmdH CmdL LenH LenL Data[3 * 1025] Crc  (LenH LenL: 3 * 1025 * 4 + 3)
//response: 6E CmdType CmdH CmdL LenH LenL RC Crc  (LenH LenL: 00 04)

//Write Gamma Unit: 51 CmdType CmdH CmdL LenH LenL Val0 Val1 Val2 Val3 Row ColH ColL Crc
//response: 6E CmdType CmdH CmdL LenH LenL RC Crc  (LenH LenL: 00 04)

#define DDCCI_FAC_RC_SUCCESS                    0xE0
#define DDCCI_FAC_RC_FAIL                       0xCC

#define DDCCI_FAC_DDC_HEADER_LEN                4       // 51 LenH LenL + CRC
#define DDCCI_FAC_DDC_CMD_LEN                   3       // CmdType CmdH CmdL
#define DDCCI_FAC_DDC_CMD_FIX_LEN               7       // CmdType CmdH CmdL AddrMsb Addr2nd Addr3rd AddrLsb

#define DDCCI_FAC_READ_REG_RSP_LEN              17
#define DDCCI_FAC_WRITE_REG_RSP_LEN             14
#define DDCCI_FAC_WRITE_DDR_RSP_LEN             12
#define DDCCI_FAC_READ_PQ_CMD_RSP_LEN           11
#define DDCCI_FAC_WRITE_PQ_CMD_RSP_LEN          8

#define DDCCI_FAC_OFFSET_SRC                    0       // 0x51
#define DDCCI_FAC_OFFSET_CMD_TYPE               1
#define DDCCI_FAC_OFFSET_CMD_HIGH               2
#define DDCCI_FAC_OFFSET_CMD_LOW                3

#define DDCCI_FAC_OFFSET_ADDR_MSB               6
#define DDCCI_FAC_OFFSET_ADDR_2ND               7
#define DDCCI_FAC_OFFSET_ADDR_3RD               8
#define DDCCI_FAC_OFFSET_ADDR_LSB               9

#define DDCCI_FAC_OFFSET_W_LONG_DATA            6

#define DDCCI_FAC_OFFSET_W_DDR_DATA_START       10
#define DDCCI_FAC_OFFSET_READ_DDR_LEN_HIGH      10
#define DDCCI_FAC_OFFSET_READ_DDR_LEN_LOW       11
#define DDCCI_FAC_OFFSET_R_DDR_RSP_DATA_START   12

#define DDCCI_FAC_OFFSET_REG_START_BIT          10
#define DDCCI_FAC_OFFSET_REG_STOP_BIT           11

#define DDCCI_FAC_OFFSET_REG_VALUE_MSB          12
#define DDCCI_FAC_OFFSET_REG_VALUE_2ND          13
#define DDCCI_FAC_OFFSET_REG_VALUE_3RD          14
#define DDCCI_FAC_OFFSET_REG_VALUE_LSB          15

#define DDCCI_FAC_OFFSET_PQ_CMD_VALUE_MSB       6
#define DDCCI_FAC_OFFSET_PQ_CMD_VALUE_2ND       7
#define DDCCI_FAC_OFFSET_PQ_CMD_VALUE_3RD       8
#define DDCCI_FAC_OFFSET_PQ_CMD_VALUE_LSB       9
#define DDCCI_FAC_OFFSET_PQ_CMD_RC              6

//PQ CMD Write: 51 LenH LenL CmdType CmdH CmdL Val Crc
//response: 6E LenH LenL CmdType CmdH CmdL RC Crc  (LenH LenL: 00 04)
#define DDCCI_FAC_CMD_PQ_PREPARE                1
#define DDCCI_FAC_CMD_PQ_FINISH                 0

typedef enum
{
    DDCCI_FAC_CMD_GAMMAT               = 0x00,
    DDCCI_FAC_CMD_HDCP                 = 0x01,
    DDCCI_FAC_CMD_SN                   = 0x02,
    DDCCI_FAC_CMD_FACWEEK              = 0x03,
    DDCCI_FAC_CMD_FACYEAR              = 0x04,
    DDCCI_FAC_CMD_HDMIEDID             = 0x05,
    DDCCI_FAC_CMD_HDMI1EDID            = 0x06,
    DDCCI_FAC_CMD_DPEDID               = 0x07,
    DDCCI_FAC_CMD_DP1EDID              = 0x08,
    DDCCI_FAC_CMD_COLORTEMP_COOL       = 0x09,
    DDCCI_FAC_CMD_COLORTEMP_WARM       = 0x0A,
    DDCCI_FAC_CMD_COLORTEMP_STANDARD   = 0x0B,
    DDCCI_FAC_CMD_COLORTEMP_P3         = 0x0C,
    DDCCI_FAC_CMD_COLORTEMP_SRGB       = 0x0D,
    DDCCI_FAC_CMD_COLORTEMP_EYECARE    = 0x0E,
    DDCCI_FAC_CMD_GAMUT_NATIVE         = 0x0F,
    DDCCI_FAC_CMD_GAMUT_SRGB           = 0x10,
    DDCCI_FAC_CMD_GAMUT_709            = 0x11,
    DDCCI_FAC_CMD_GAMUT_P3             = 0x12,
    DDCCI_FAC_CMD_GAMUT_HDR            = 0x13,
    DDCCI_FAC_CMD_GAMUT_ADOBE          = 0x14,
    DDCCI_FAC_CMD_FACBRIGHTNESS        = 0x15,
    DDCCI_FAC_CMD_FACCONTRAST          = 0x16,
    DDCCI_FAC_CMD_ENERGY               = 0x17,
    DDCCI_FAC_CMD_DEFAULT_LANG         = 0x18,
    DDCCI_FAC_CMD_SHARPNESS            = 0x19,
    DDCCI_FAC_CMD_DATABASE_SYNC        = 0x1A,
    DDCCI_FAC_CMD_CM64                 = 0x1B,
    DDCCI_FAC_CMD_GMACOMPRESS          = 0x1C,
    DDCCI_FAC_CMD_HDCP_ALL             = 0x1D,
    DDCCI_FAC_CMD_GAMUT_DISPLAY_P3     = 0x1E,
    DDCCI_FAC_CMD_GAMUT_USER0          = 0x1F,
    DDCCI_FAC_CMD_GAMUT_USER1          = 0x20,
    DDCCI_FAC_CMD_GAMUT_USER2          = 0x21,

    DDCCI_FAC_CMD_HUE                  = 0x200,
    DDCCI_FAC_CMD_SATURATION           = 0x201,
    DDCCI_FAC_CMD_CONTRAST             = 0x202,
    DDCCI_FAC_CMD_DLC                  = 0x203,
    DDCCI_FAC_CMD_DCR                  = 0x204,
    DDCCI_FAC_CMD_COLORSPACE           = 0x205,
    DDCCI_FAC_CMD_RGAIN                = 0x206,
    DDCCI_FAC_CMD_GGAIN                = 0x207,
    DDCCI_FAC_CMD_BGAIN                = 0x208,
    DDCCI_FAC_CMD_ROFFSET              = 0x209,
    DDCCI_FAC_CMD_GOFFSET              = 0x20A,
    DDCCI_FAC_CMD_BOFFSET              = 0x20B,
    DDCCI_FAC_CMD_RED_HUE              = 0x20C,
    DDCCI_FAC_CMD_RED_SAT              = 0x20D,
    DDCCI_FAC_CMD_RED_LUM              = 0x20E,
    DDCCI_FAC_CMD_GREEN_HUE            = 0x20F,
    DDCCI_FAC_CMD_GREEN_SAT            = 0x210,
    DDCCI_FAC_CMD_GREEN_LUM            = 0x211,
    DDCCI_FAC_CMD_BLUE_HUE             = 0x212,
    DDCCI_FAC_CMD_BLUE_SAT             = 0x213,
    DDCCI_FAC_CMD_BLUE_LUM             = 0x214,
    DDCCI_FAC_CMD_CYAN_HUE             = 0x215,
    DDCCI_FAC_CMD_CYAN_SAT             = 0x216,
    DDCCI_FAC_CMD_CYAN_LUM             = 0x217,
    DDCCI_FAC_CMD_YELLOW_HUE           = 0x218,
    DDCCI_FAC_CMD_YELLOW_SAT           = 0x219,
    DDCCI_FAC_CMD_YELLOW_LUM           = 0x21A,
    DDCCI_FAC_CMD_MAGENTA_HUE          = 0x21B,
    DDCCI_FAC_CMD_MAGENTA_SAT          = 0x21C,
    DDCCI_FAC_CMD_MAGENTA_LUM          = 0x21D,
    DDCCI_FAC_CMD_PWM                  = 0x21E,
    DDCCI_FAC_CMD_SET_FR               = 0x21F,
    DDCCI_FAC_CMD_SIMKEY0              = 0x220,
    DDCCI_FAC_CMD_SIMKEY1              = 0x221,
    DDCCI_FAC_CMD_SIMKEY2              = 0x222,
    DDCCI_FAC_CMD_SIMKEY3              = 0x223,
    DDCCI_FAC_CMD_SIMKEY4              = 0x224,
    DDCCI_FAC_CMD_GAMMA_MEA            = 0x225,
    DDCCI_FAC_CMD_DELTAE_MEA           = 0x226,
    DDCCI_FAC_CMD_DYNAMICPANEL         = 0x227,
    DDCCI_FAC_CMD_FACTORYMODE          = 0x228,
    DDCCI_FAC_CMD_LUTINIT              = 0x229,
    DDCCI_FAC_CMD_BRIGHTNESS           = 0x22A,
    DDCCI_FAC_CMD_BRIGHTNESS_NIT       = 0x22B,
    DDCCI_FAC_CMD_WCG_3D_LUT           = 0x22C,
    DDCCI_FAC_CMD_SETBURNOSD           = 0x22D,
    DDCCI_FAC_CMD_SETEDIDSN            = 0x22E,

    DDCCI_FAC_CMD_SWITCH_GAMMA         = 0x239,

    DDCCI_FAC_CMD_REGISTER             = 0x8881,
    DDCCI_FAC_CMD_DDR                  = 0x8883,
    DDCCI_FAC_CMD_GAMMA_LUT            = 0x8884,
    DDCCI_FAC_CMD_GAMMA_UNIT_WRITE     = 0x8885,
    DDCCI_FAC_CMD_GAMMA_ACTION         = 0x8886,  //0xEE,0x88,0x86+0x01:Prepare, 0x00:Finish
    DDCCI_FAC_CMD_COLORSPACE_ACTION    = 0x8887,  //0xEE,0x88,0x87+0x01:Prepare, 0x00:Finish
    DDCCI_FAC_CMD_WCG_ACTION           = 0x8889,  //0xEE,0x88,0x89+0x01:On, 0x00:Off
    DDCCI_FAC_CMD_COLORTEMP_ACTION     = 0x8890,  //0xEE,0x88,0x90+0x01:Prepare, 0x00:Finish
    DDCCI_FAC_CMD_PATTERN_ACTION       = 0x8891,  //0xEE,0x88,0x91+0x01:Prepare, 0x00:Finish
    DDCCI_FAC_CMD_COLORSPACE_DATA1     = 0x8892,  //0xEE,0x88,0x92+18个16bit数值，高位在前
    DDCCI_FAC_CMD_COLORSPACE_DATA2     = 0x8893,  //0xEE,0x88,0x93+18个16bit数值，高位在前
    DDCCI_FAC_CMD_COLORSPACE_DATA3     = 0x8894,  //0xEE,0x88,0x94+18个16bit数值，高位在前
    DDCCI_FAC_CMD_COLORSPACE_DATA4     = 0x8895,  //0xEE,0x88,0x95+18个16bit数值，高位在前
    DDCCI_FAC_CMD_COLORSPACE_DATA5     = 0x8896,  //0xEE,0x88,0x96+18个16bit数值，高位在前
    DDCCI_FAC_CMD_PQ_TRAINING_START    = 0x88A0,  //0xEE,0x88,0xA0+0x01, training start; 0x00-training stop
    DDCCI_FAC_CMD_PQ_RGB_COEF          = 0x88A1,  //0xEE,0x88,0xA1+RHigh RLow GHigh GLow BHigh BLow
    DDCCI_FAC_CMD_PQ_WEEK_YEAR         = 0x88A2,  //0xEE,0x88,0xA2+week+year
    
    DDCCI_FAC_CMD_START_OR_END         = 0x88F0,
} DdcciFacCmdItem;

VOID Hv_App_Ddcci_ProcFactoryMsg(UCHAR8 ucSource, UCHAR8 *pucData, USHORT16 usLen);

UCHAR8 Hv_App_Ddcci_VerifyFactoryCheckSum(UCHAR8 *pucData);

VOID Hv_App_GmaCompress(UINT32 *puiInput, UCHAR8 *pucOut);
#endif

