/*
 * @file hv_cal_Gpio.h
 * @brief Header file of GPIO module.
 *
 * @verbatim
 * ==============================================================================
 *                        ##### How to use #####
 * ==============================================================================
 * (+) Use ()
 * @endverbatim
 * @author HiView SoC Software Team
 * @version 1.0.0
 * @date 2023-05-30
 */

#ifndef _HV_CAL_GPIO_H
#define _HV_CAL_GPIO_H

#include "Common/hv_comm_DataType.h"

/*
  88 I/O register layout
  gpio0     : group0[19:0],group1[39:20]
  gpio0_01: group0[19:0],group1[39:20]
  gpio0_02: group0[1:0],group1[1:0]
  gpio0_03: group0[1:0],group1[1:0]
*/

typedef enum _GpioPull
{
    GPIO_NOPULL,
    GPIO_PULLUP,
    GPIO_PULLDOWN,
} GpioPull;

typedef enum _GPioDir
{
    GPIO_DIR_INPUT    = 0,
    GPIO_DIR_OUTPUT   = 1,
    GPIO_DIR_NONE
} GpioDir;

typedef enum _GpioLevel
{
    GPIO_LEVEL_LOW = 0,
    GPIO_LEVEL_HIGH,
    GPIO_LEVEL_INVALID
} GpioLevel;

typedef enum _GpioDomain
{
    GPIO_DOMAIN_SYS = 0, //system
    GPIO_DOMAIN_SDBY = 1,//standby
} GpioDomain;

typedef enum _GpioIndex
{
    //port0
    GPIO_IDX0 = 0,
    GPIO_IDX1,
    GPIO_IDX2,
    GPIO_IDX3,
    GPIO_IDX4,
    GPIO_IDX5,
    GPIO_IDX6,
    GPIO_IDX7,
    GPIO_IDX8,
    GPIO_IDX9,
    GPIO_IDX10,
    GPIO_IDX11,
    GPIO_IDX12,
    GPIO_IDX13,
    GPIO_IDX14,
    GPIO_IDX15,
    GPIO_IDX16,
    GPIO_IDX17,
    GPIO_IDX18,
    GPIO_IDX19,
    GPIO_IDX20,
    GPIO_IDX21,
    GPIO_IDX22,
    GPIO_IDX23,
    GPIO_IDX24,
    GPIO_IDX25,
    GPIO_IDX26,
    GPIO_IDX27,
    GPIO_IDX28,
    GPIO_IDX29,
    GPIO_IDX30,
    GPIO_IDX31,
    GPIO_IDX32,
    GPIO_IDX33,
    GPIO_IDX34,
    GPIO_IDX35,
    GPIO_IDX36,
    GPIO_IDX37,
    GPIO_IDX38,
    GPIO_IDX39,
    //port1
    GPIO_IDX_40,
    GPIO_IDX_41,
    GPIO_IDX_COUNT
}GpioIndex;

/*
 HT7315 has 42 gpios,
 0-39   @page_gpio0:0x60
 40-41 @page  gpio0_1:0x61
*/
#define HV_GPIO_MAX_NUM                   (GPIO_IDX_COUNT)

/*
    8 I/O standby domain
*/
typedef enum _GpioSdbyPinIndex
{
    GPIO_SDBY_PIN_0,
    GPIO_SDBY_PIN_1,
    GPIO_SDBY_PIN_2,
    GPIO_SDBY_PIN_3,
    GPIO_SDBY_PIN_4,
    GPIO_SDBY_PIN_5,
    GPIO_SDBY_PIN_6,
    GPIO_SDBY_PIN_7,
    GPIO_SDBY_PIN_MAX
}GpioSdbyPinIndex;

#define HV_GPIO_STBY_PIN_MAX_NUM           (GPIO_SDBY_PIN_MAX)

typedef enum _GpioPort
{
    GPIO_PORT0 = 0,//gpio0
    GPIO_PORT1,    //gpio1
    GPIO_PORT_COUNT,
} GpioPort;

#define HV_GPIO_PIN_INT_STATE_COUNT_MAX    (40)

typedef enum _GpioIntTrigMode
{
    GPIO_INT_NONE,
    GPIO_INT_HIGHLEVEL,
    GPIO_INT_LOWLEVEL,
    GPIO_INT_RISING,
    GPIO_INT_FALLING,
    GPIO_INT_RISING_FALLING,
} GpioIntTrigMode;

#define HV_GPIO_GENERAL_INT_REG_MASK       (0xFFFFF)//20bits
#define HV_GPIO_SPECIAL_INT_REG_MASK       (0x3)//2bits

/* 6 irq num for 6 gpio groups
HT7315 user gpio0 and gpio1
[0]:gpio0[39:0];
[1]:gpio1[39:0];
[2]:gpio2[1:0];
[3]:gpio3[1:0];
[4]:gpio4[1:0];
[5]:gpio5[1:0];
*/
typedef enum _GpioIntHandleIndex
{
    GPIO_INT_HANDLER0,     //gpio0[39:0]
    GPIO_INT_HANDLER1,     //gpio1[39:0]
    GPIO_INT_HANDLER_MAX,
} GpioIntHandleIndex;

/*
40 gpios in group gpio0&gpio1,every group use 2 members,every element used 20 bits
[0]:gpio0[19:0];
[1]:gpio0[39:20];
[2]:gpio1[19:0];
[3]:gpio1[39:20];
[4]:gpio2[1:0];
[5]:gpio3[1:0];
[6]:gpio4[1:0];
[7]:gpio5[1:0];
*/
typedef enum _GPioIntStateIndex
{
    GPIO_INT_STATE_0,//gpio0[19:0]
    GPIO_INT_STATE_1,//gpio0[39:20]
    GPIO_INT_STATE_2,//gpio1[19:0]
    GPIO_INT_STATE_3,//gpio1[39:20]
    GPIO_INT_STATE_COUNT,
}GPioIntStateIndex;

typedef struct _GpioIntState
{
    UINT32 uiIntState[GPIO_INT_STATE_COUNT];
} GpioIntState;

typedef VOID (*GpioIntCallBack)(const GpioIntState *stIntState, VOID *pArg);

/** Enable the designated gpio interrupt
 *  @param[in] ucPinIdx Specified GPIO pin
 */
VOID Hv_Cal_Gpio_EnableInt(UCHAR8 ucPinIdx);

/** Disable the designated gpio interrupt
 *  @param[in] ucPinIdx Specified GPIO pin
 */
VOID Hv_Cal_Gpio_DisableInt(UCHAR8 ucPinIdx);

/** Set gpio pin interrupt trigger type
 *  @param[in] ucPinIdx Specified GPIO pin, enPull Specified pull acion @ref GpioIntTrigMode
 */
VOID Hv_Cal_Gpio_SetIntTriggerMode(UCHAR8 ucPinIdx, GpioIntTrigMode enIntTrigMode);

/** Set gpio pin interrupt callback
 *  @param ucPinIdx Specified GPIO pin, pfCallBack Specified callback @ref GpioIntCallBack
 */
VOID Hv_Cal_Gpio_SetIntCallBack(UCHAR8 ucPinIdx, GpioIntCallBack pfCallBack);
/**
   De-initializes the gpio module interrupts
 */
VOID Hv_Cal_Gpio_CleanupInt(VOID);

/** Get interrupt state of designated gpio
 *  @param[in] ucPinIdx Specified GPIO pin
 */
BOOL Hv_Cal_Gpio_GetIntState(UCHAR8 ucPinIdx);

/** Clear the gpio interrupt status
 *  @param[in]: ucPinIdx Specified GPIO pin
 */
VOID Hv_Cal_Gpio_ClrIntState(UCHAR8 ucPinIdx);

/** Set gpio pin direction
 *  @param[in] ucPinIdx Specified GPIO pin, enPull Specified pull acion @ref GpioDir
 */
VOID Hv_Cal_Gpio_SetDirection(UCHAR8 ucPinIdx, GpioDir dir);

/**@brief Get gpio pin direction
 *  @param[in] ucPinIdx Specified GPIO pin
 *  @param[out]: dir output/input
 */
GpioDir Hv_Cal_Gpio_GetDirection(UCHAR8 ucPinIdx);

/** Set gpio pin level
 *  @param ucPinIdx Specified GPIO pin
 *  @param GpioLevel high(1) or low(0) level
 */
VOID Hv_Cal_Gpio_SetPinLevel(UCHAR8 ucPinIdx, GpioLevel enLevel);


/** Get gpio pin out level
 *  @param[in]  :ucPinIdx Specified GPIO pin
 *  @param[out]: state high(1)/low(0)/invalid(2)
 */
GpioLevel Hv_Cal_Gpio_GetPinLevel(UCHAR8 ucPinIdx);

/** @} */

#endif//_HV_CAL_GPIO_H

