/*******************************************************************************
*
*
* File:         tau_dsi_datatype.h
* Description:  mipi dsi 通用头文件
* Version:      V0.1
* Date:         2021-01-13
* Author:       lzy
 *******************************************************************************/

#ifndef __MIPI_DSI_COMMON_H__
#define __MIPI_DSI_COMMON_H__

/*******************************************************************************
* 1.Included files
*******************************************************************************/
#include "tau_common.h"

/*******************************************************************************
* 2.Global constant and macro definitions using #define
*******************************************************************************/
#define DSC_PPS_SIZE 128

/*******************************************************************************
* 3.Global structures, unions and enumerations using typedef
*******************************************************************************/
/**
* @brief  Data Types for Peripheral-sourced Packets,From Mipi DSI Spec
*/
typedef enum
{
    DSI_ACK_DT_ERROR                 = 0x02,
    DSI_ACK_DT_EOTP                  = 0x08,
    DSI_ACK_DT_GEN_SHORT_RESPONSE_1B = 0x11,
    DSI_ACK_DT_GEN_SHORT_RESPONSE_2B = 0x12,
    DSI_ACK_DT_GEN_LONG_RESPONSE     = 0x1A,
    DSI_ACK_DT_DCS_LONG_RESPONSE     = 0x1C,
    DSI_ACK_DT_DCS_SHORT_RESPONSE_1B = 0x21,
    DSI_ACK_DT_DCS_SHORT_RESPONSE_2B = 0x22,
    DSI_ACK_DT_MAX
} dsi_ack_data_type_e;

/**
* @brief Software handle data types
*/
typedef enum
{
    DSI_RECV_DT_PPS                   = 0x0A,   //Picture Parameter Set
    DSI_RECV_DT_GEN_WRITE_1B          = 0x13,   //Generic Short WRITE, 1 parameter
    DSI_RECV_DT_GEN_WRITE_2B          = 0x23,   //Generic Short WRITE, 2 parameters
    DSI_RECV_DT_GEN_READ_0B           = 0x04,   //Generic READ, no parameters
    DSI_RECV_DT_GEN_READ_1B           = 0x14,   //Generic READ, 1 parameter
    DSI_RECV_DT_GEN_READ_2B           = 0x24,   //Generic READ, 2 parameters
    DSI_RECV_DT_DCS_WRITE_0B          = 0x05,   //DCS Short WRITE, no parameters
    DSI_RECV_DT_DCS_WRITE_1B          = 0x15,   //DCS Short WRITE, 1 parameter
    DSI_RECV_DT_DCS_READ_0B           = 0x06,   //DCS READ, no parameters
    DSI_RECV_DT_GEN_WRITE_LONG        = 0x29,   //Generic Long Write
    DSI_RECV_DT_DCS_WRITE_LONG        = 0x39,   //DCS Long Write/write_LUT Command Packet
    DSI_RECV_DT_MAX
} dsi_data_type_e;

typedef enum
{
    DCS_ENTER_IDLE_MODE       = 0x39,
    DCS_ENTER_INVERT_MODE     = 0x21,
    DCS_ENTER_NORMAL_MODE     = 0x13,
    DCS_ENTER_PARTIAL_MODE    = 0x12,
    DCS_ENTER_SLEEP_MODE      = 0x10,
    DCS_EXIT_IDLE_MODE        = 0x38,
    DCS_EXIT_INVERT_MODE      = 0x20,
    DCS_EXIT_SLEEP_MODE       = 0x11,
    DCS_GET_3D_CONTROL        = 0x3F,
    DCS_GET_ADDRESS_MODE      = 0x0B,
    DCS_GET_BLUE_CHANNEL      = 0x08,
    DCS_GET_COMPRESSION_MODE  = 0x03,
    DCS_GET_DIAGNOSTIC_RESULT = 0x0F,
    DCS_GET_DISPLAY_MODE      = 0x0D,
    DCS_GET_GREEN_CHANNEL     = 0x07,
    DCS_GET_PIXEL_FORMAT      = 0x0C,
    DCS_GET_POWER_MODE        = 0x0A,
    DCS_GET_RED_CHANNEL       = 0x06,
    DCS_GET_SCANLINE          = 0x45,
    DCS_GET_SIGNAL_MODE       = 0x0E,
    DCS_NOP                   = 0x00,
    DCS_READ_DDB_CONTINUE     = 0xA8,
    DCS_READ_DDB_START        = 0xA1,
    DCS_READ_MEMORY_CONTINUE  = 0x3E,
    DCS_READ_MEMORY_START     = 0x2E,
    DCS_SET_3D_CONTROL        = 0x3D,
    DCS_SET_ADDRESS_MODE      = 0x36,
    DCS_SET_COLUMN_ADDRESS    = 0x2A,
    DCS_SET_DISPLAY_OFF       = 0x28,
    DCS_SET_DISPLAY_ON        = 0x29,
    DCS_SET_GAMMA_CURVE       = 0x26,
    DCS_SET_PAGE_ADDRESS      = 0x2B,
    DCS_SET_PARTIAL_COLUMNS   = 0x31,
    DCS_SET_PARTIAL_ROWS      = 0x30,
    DCS_SET_PIXEL_FORMAT      = 0x3A,
    DCS_SET_SCROLL_AREA       = 0x33,
    DCS_SET_SCROLL_START      = 0x37,
    DCS_SET_TEAR_OFF          = 0x34,
    DCS_SET_TEAR_ON           = 0x35,
    DCS_SET_TEAR_SCANLINE     = 0x44,
    DCS_SET_VSYNC_TIMING      = 0x40,
    DCS_SOFT_RESET            = 0x01,
    DCS_WRITE_LUT             = 0x2D,
    DCS_WRITE_MEMORY_CONTINUE = 0x3C,
    DCS_WRITE_MEMORY_START    = 0x2C
} dsi_dcs_cmd_type_e;

/**
* @brief  video data transfer mode
*/
typedef enum
{
    DSI_DATA_VIDEO_MODE = 0,
    DSI_DATA_CMD_MODE   = 1,
    DSI_DATA_MODE_MAX
} dsi_video_data_mode_e;

/**
* @brief  dsi virtual channel
*/
typedef enum
{
    DSI_VC_0 = 0,
    DSI_VC_1 = 1,
    DSI_VC_2 = 2,
    DSI_VC_3 = 3,
    DSI_VC_MAX
} dsi_virtual_channel_e;

/**
* @brief  video data mode
*/
typedef enum
{
    DSI_FRAME_RATE_60HZ  = 0,
    DSI_FRAME_RATE_90HZ  = 1,
    DSI_FRAME_RATE_120HZ = 2,
    DSI_FRAME_RATE_144HZ = 3,
    DSI_FRAME_RATE_160HZ = 4,
    DSI_FRAME_RATE_MAX
} dsi_video_frame_rate_e;

/**
* @brief  dsi rx color coding
*/
typedef enum
{
    DSI_RGB565               = 1,
    DSI_RGB666               = 2,    /*!< 18 bbp(18bits per pixel) */
    DSI_RGB666_LOOSELY       = 3,    /*!< 24 bbp(24bits per pixel) */
    DSI_RGB888               = 4,    /*!< 24 bbp(24bits per pixel) */
    DSI_RGB10_10_10          = 5,
    DSI_RGB12_12_12          = 6,
    DSI_YCbCr422_16          = 7,
    DSI_PENTILE_16           = DSI_YCbCr422_16,
    DSI_YCbCr422_20_LOOSELY  = 8,
    DSI_YCbCr422_24          = 9,
    DSI_YCbCr420_12          = 10,
    DSI_DSC_STREAM           = 11,
    DSI_COLOR_CODE_MAX
} dsi_color_code_e;

/**
* @brief  dsi endianness type
*/
typedef enum
{
    DPI_ENDIAN_RGB = 0,
    DPI_ENDIAN_BGR
} dsi_endianness_e;

/**
* @brief  mipi lane number
*/
typedef enum
{
    DSI_LANE_1 = 1,
    DSI_LANE_2 = 2,
    DSI_LANE_3 = 3,
    DSI_LANE_4 = 4,
    DSI_LANE_NUME_MAX
} dsi_lane_nume_e;

/**
* @brief video mode
*/
typedef enum
{
    DSI_NONBURST_PULSE = 0,
    DSI_NONBURST_EVENT = 1,
    DSI_BURST_MODE     = 2,
    DSI_VIDEO_MODE_MAX
} dsi_video_mode_type_e;

/**
* @brief panel init cmd transfer type
*/
typedef enum
{
    DSI_CMD_TX_HS = 0,
    DSI_CMD_TX_LP = 1
} dsi_tx_cmd_tx_type_e;

/**
* @brief  angle of rotation
*/
typedef enum
{
    VIDOE_ROT_ANGLE_0    = 0, /* 不旋转       */
    VIDOE_ROT_ANGLE_90   = 1, /* 旋转90度     */
    VIDOE_ROT_ANGLE_180  = 2, /* 旋转180度 */
    VIDOE_ROT_ANGLE_270  = 3, /* 转转270度 */
    VIDOE_ROT_ANGLE_MAX
} video_rotate_angle_e;

/**
* @brief mipi rx lane swap
软件配置                        PIN28&PIN27         PIN26&PIN25         PIN24&PIN23             PIN22&PIN21         PIN20&PIN19
RX_LANE_SWAP_0123           D0P&D0N             D1P&D1N             CLKP&CLKN               D2P&D2N             D3P&D3N
RX_LANE_SWAP_3210           D3P&D3N             D2P&D2N             CLKP&CLKN               D1P&D1N             D0P&D0N
RX_LANE_SWAP_2103(default)  D2P&D2N             D1P&D1N             CLKP&CLKN               D0P&D0N             D3P&D3N
RX_LANE_SWAP_3012           D3P&D3N             D0P&D0N             CLKP&CLKN               D1P&D1N             D2P&D2N
*/
typedef enum
{
    RX_LANE_SWAP_0123  = 0x0,
    RX_LANE_SWAP_3210  = 0x1,
    RX_LANE_SWAP_2103  = 0x2,
    RX_LANE_SWAP_DEFAULT_ORDER = 0x2,   /* 默认原理图为2103顺序 */
    RX_LANE_SWAP_3012  = 0x3,
    RX_LANE_SWAP_MAX
} dsi_rx_lane_swap_e;

/**
* @brief mipi P/N lane swap flag
* eg: pn_swap = RX_LANE_0_PN_SWAP | RX_LANE_CLK_PN_SWAP;
* 表示 lane0 与 CLK 的P跟N交换，其他lane不变。
* 注意这里的lane表示的是进行完lane swap的lane,如lane swap配置RX_LANE_SWAP_3012,再配置RX_LANE_0_PN_SWAP则表示PIN26&PIN25进行PN交换,由D0P&D0N变成D0N&D0P
*/
typedef enum
{
    RX_LANE_0_PN_SWAP = 0x1,
    RX_LANE_1_PN_SWAP = 0x2,
    RX_LANE_2_PN_SWAP = 0x4,
    RX_LANE_3_PN_SWAP = 0x8,
    RX_LANE_CLK_PN_SWAP = 0x10
} dsi_rx_lane_pn_swap_e;

/**
* @brief error processing level
*/
typedef enum
{
    ERR_HANDLE_NONE  = 0,
    ERR_HANDLE_L1  = 1,
    ERR_HANDLE_L2  = 2,
    ERR_HANDLE_L3  = 3,
    ERR_HANDLE_MAX
} hal_err_handle_level_e;

/**
* @brief flow control mode
*/
typedef enum
{
    FC_AUTO_MODE        = 0,         /* 自动匹配模式,根据base info配置匹配1-4 */
    FC_V2V_NORMAL_MODE  = 1,         /* Video to Video 模式转换 */
    FC_V2C_NORMAL_MODE  = 2,         /* Video to Command 模式转换 */
    FC_C2V_NORMAL_MODE  = 3,         /* Command to Video 模式转换 */
    FC_C2C_NORMAL_MODE  = 4,         /* Command to Command 模式转换 */
    FC_PRO_MOTION_MODE  = 6,         /* 自适应帧率转换(LTPO模式) */
    FC_PRO_MOTION_MODE_2  = 5,         /* 自适应帧率转换(LTPO模式) */
    FC_PRO_MOTION_WITH_PU_MODE = 7,  /* 自适应帧率带PU */
    FC_V2V_AUTO_SYCN_MODE = 8,       /* Video to Video 软件同步 */
    FC_V2V_DIRECT_MODE   = 9,        /* Video to Video 直通模式 */
    FC_MODE_MAX
} flow_control_mode_e;

/**
* @brief  transform 基本信息
*/
typedef struct
{
    uint16_t top;
    uint16_t bottom;
    uint16_t left;
    uint16_t right;
    bool     enable;
} pic_edge_info_t;

/**
* @brief  transform 基本信息
*/
typedef struct
{
    uint32_t src_w;                     /* mipi rx 接收的 width        */
    uint32_t src_h;                     /* mipi rx 接收的 height       */
    uint32_t dst_w;                     /* mipi tx 发送的 width        */
    uint32_t dst_h;                     /* mipi tx 发送的 height       */
    dsi_video_frame_rate_e src_frate;   /* mipi rx 接收的frame rate */
    dsi_video_data_mode_e  src_mode;    /* mipi rx 接收video 数据传输模式(video/cmd mode) */
    dsi_video_data_mode_e  dst_mode;    /* mipi tx 输出video 数据传输模式(video/cmd mode) */
    uint16_t pn_swap;                   /* mipi P/N swap标志位, rx可配置/tx暂不支持   */
} dsi_base_trans_info_t;

/**
* @brief  transform 基本信息
*/
typedef struct
{
    bool     mirror_en;                       /* 对video 做水平镜像标志位 */
    bool     pu_optimize;                     /* 用于优化PU显示效果，默认为false;true:优化PU显示显示效果,高功耗;false:普通PU模式,低功耗 */
    video_rotate_angle_e   rot_angle;         /* 对video 做旋转的角度 */
    flow_control_mode_e    flow_control_mode; /* 图像数据流控制模式 */
    pic_edge_info_t        crop_info;         /* 图像边缘裁剪配置 not impletmented */
    pic_edge_info_t        blank_info;        /* 图像边缘补黑配置 not impletmented */
    bool                   bw_optimize;       /* 带宽自动检查,默认打开 */
    uint8_t                pq_type;           /* picture quality,参数为 pq_type_e */
} dsi_base_extra_info_t;

/**
* @brief  ccm系数
*/
typedef struct
{
    uint32_t coef_c00;
    uint32_t coef_c01;
    uint32_t coef_c02;
    uint32_t coef_c10;
    uint32_t coef_c11;
    uint32_t coef_c12;
    uint32_t coef_c20;
    uint32_t coef_c21;
    uint32_t coef_c22;
} ccm_coef_t;

/**
* @brief hight performan mode level
*/
typedef enum
{
    HIGHT_PERFORMAN_NONE  = 0,
    HIGHT_PERFORMAN_L1  = 1,
    HIGHT_PERFORMAN_L2  = 2,
    HIGHT_PERFORMAN_MAX
} hight_performan_mode_e;

/**
* @brief  TX False color去伪彩参数结构体
*/
typedef struct
{
    uint16_t edgemedslope;
    uint16_t desatslope;
} dsi_tx_fc_t;

/**
* @brief  TX 边缘增强参数结构体
*/
typedef struct
{
    bool     y_enh_method;
    uint8_t  enhance_str;
    uint16_t enhance_slope;
    uint16_t boundscale_low;
    uint16_t boundscale_high;
} dsi_tx_edge_enh_t;

/**
* @brief  TX 边缘检测参数结构体
*/
typedef struct
{
    uint8_t  edge_thr;
    bool     use_large_kernel;
} dsi_tx_edge_dect_t;

/**
* @brief  TX bcsa 参数结构体
*/
typedef struct
{
    int8_t   brightness; /* 亮度调整,   范围-127 - 127 */
    uint16_t contrast;   /* 对比度调整,范围0 - 4095 */
    uint16_t saturation; /* 饱和度调整,范围0 - 4095 */
} dsi_tx_bcs_t;

/**
* @brief  partial display 参数结构体
*/
typedef struct
{
    uint32_t    st_line;    /*部分显示起始行位置*/
    uint32_t    st_col;     /*部分显示起始列位置*/
    uint32_t    end_line;   /*部分显示结束行位置*/
    uint32_t    end_col;    /*部分显示结束列位置*/
    uint8_t     value_r;    /*部分显示背景色R值*/
    uint8_t     value_g;    /*部分显示背景色G值*/
    uint8_t     value_b;    /*部分显示背景色B值*/
} dsi_tx_par_dis_t;

#endif //__MIPI_DSI_COMMON_H__
