/* 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.
 *
 *****************************************************************************/

#ifndef CUSTOM_GPIO_H
#define CUSTOM_GPIO_H

#include "x_pdwnc.h"
#include "drv_pwm.h"
#ifndef CC_UBOOT
#include "CustomCtrl.h"
#else
#ifdef TARGET_SPEC_DRV_CUST_CUSTOMIZED_HDR_FILE
#include TARGET_SPEC_DRV_CUST_CUSTOMIZED_HDR_FILE
#endif
#endif
#ifdef CC_SAKURA_LED_ILLUMINATION
#include "pdwnc_if.h"
#endif
//---------------------------------------------------------------------------
// Customer function definitions
//---------------------------------------------------------------------------
#ifndef CC_UBOOT
#define GPIO_POWER_ON_FUNC      (GPIO_CustomPowerOnFunc)
#define GPIO_POWER_OFF_FUNC     (GPIO_CustomPowerOffFunc)
#define GPIO_LED_CTRL_FUNC      (GPIO_CustomLedCtrlFunc)
#else
#define GPIO_POWER_ON_FUNC      (NULL)
#define GPIO_POWER_OFF_FUNC     (NULL)
#define GPIO_LED_CTRL_FUNC      (NULL)
#define LOADER_ENV_INIT_FUNC    (NULL)
#endif

#ifdef CC_MTK_LOADER
//#define LOADER_UPGRADE_VER_CMP_FUNC             (NULL)
#ifndef CC_UBOOT
#define LOADER_UPGRADE_DLM_CB_FUNC              (_LdrUpgradeDlmCbFunc)
#define LOADER_UPGRADE_DLM_GET_TAG_VER_CB_FUNC  (_LdrUpgradeDlmGetTagVerCbFunc)
#define LOADER_UPGRADE_DLM_CMP_TAG_VER_CB_FUNC  (_LdrUpgradeDlmCmpTagVerCbFunc)
#else
#define LOADER_UPGRADE_DLM_CB_FUNC              (NULL)
#define LOADER_UPGRADE_DLM_GET_TAG_VER_CB_FUNC  (NULL)
#define LOADER_UPGRADE_DLM_CMP_TAG_VER_CB_FUNC  (NULL)
#endif
//#define LOADER_UPGRADE_BREAK_HANDLE_FUNC        (_LdrUpgradeBreakFunc)
#else
#define LOGO_NOTIFY_FUNC                        (CustomLogoNotifyFunc)
#define CUSTOMER_LOGO_DISPLAY_FUNC              (CustomDisplayLogo)
#ifdef CC_PAA_GET_COUNTRY_SUPPORT
#ifndef CC_UBOOT
#define APP_COUNTRY_SET_FUCTION                 (CustomSetAPPCountry)
#define APP_COUNTRY_GET_FUCTION                 (CustomGetAPPCountry)
#endif
#endif

#endif /* CC_MTK_LOADER */

#ifdef SONY_LOADER_POWER_SEQ
#ifndef CC_UBOOT
#define CUSTOMER_POWER_ON_SEQUENCE              (CustomPowerOnSequence)
#define CUSTOMER_POWER_OFF_SEQUENCE             (CustomPowerOffSequence)
#endif
#else
#define CUSTOMER_POWER_ON_SEQUENCE              (NULL)
#define CUSTOMER_POWER_OFF_SEQUENCE             (NULL)
#endif

#ifdef CUSTOM_POWER_STATE_CTRL_ENABLE
#ifndef CC_UBOOT
#define LOADER_CEC_CONFIG_QUERY_FUNC            (CustomCheckCECCtrlState)
#define POWER_STATE_QUERY_FUNC                  (PDWNC_Custom_CheckPowerState)
#endif
#endif /* CUSTOM_POWER_STATE_CTRL_ENABLE */

//---------------------------------------------------------------------------
// Constant definitions
//---------------------------------------------------------------------------
#define CUSTOM_IR_LED_BLINK_PERIOD              150         // 150ms

#define WIFI_MODUDLE_RST_GPIO        (243)
#define WIFI_MODUDLE_RST_POLARITY    (0)

// Strap GPIO definitaion on PCB
#define CUSTOM_GPIO_PANEL_NVM_WP                GPIO(85)    // GPIO 15
#define CUSTOM_GPIO_PANEL_ID_PIN_0              GPIO(0xffffffff)
#define CUSTOM_GPIO_PANEL_DET_PIN               GPIO(273)
#define CUSTOM_GPIO_PANELLESS_PIN               GPIO(63)
#define CUSTOM_PCB_TYPE_1_WXGA                  0x00
#define CUSTOM_PCB_TYPE_1_FHD                   0x01
#define CUSTOM_PCB_TYPE_2                       0x02
#define CUSTOM_PCB_TYPE_3                       0x03
#define CUSTOM_PCB_TYPE_5                       0x05

#define CUSTOM_GPIO_PANEL_NONTCON               3
#define CUSTOM_GPIO_PANEL_OTHER                 2
#define CUSTOM_GPIO_PANEL_WXGA                  1
#define CUSTOM_GPIO_PANEL_FHD                   0
#define CUSTOM_GPIO_LVDS_OTHER_BIT              2
#define CUSTOM_GPIO_LVDS_10BIT                  1
#define CUSTOM_GPIO_LVDS_8BIT                   0
#define CUSTOM_GPIO_FACTORY_SONY                0
#define CUSTOM_GPIO_FACTORY_WISTRON             1
#define CUSTOM_GPIO_DVD_NA                      0
#define CUSTOM_GPIO_DVD_INCLUDE                 1
#define CUSTOM_GPIO_FHD_CMO_37                  0
#define CUSTOM_GPIO_FHD_OTHER                   1
#define CUSTOM_GPIO_AUD_NORMAL                  0
#define CUSTOM_GPIO_AUD_WOOFER                  1
#define CUSTOM_GPIO_LIGHT_SENSOR_INCLUDE        0
#define CUSTOM_GPIO_LIGHT_SENSOR_NA             1
#define CUSTOM_GPIO_THERMAL_SENSOR_NA           0
#define CUSTOM_GPIO_THERMAL_SENSOR_INCLUDE      1
#define CUSTOM_PCB_TYPE_1H						0
#define CUSTOM_PCB_TYPE_2H						1

#define CUSTOM_PANEL_WIDTH_FHD                  1920
#define CUSTOM_PANEL_WIDTH_WXGA                 1366
#if 1
// LED GPIO 
#define CUSTOM_GPIO_LED_STANDBY                 0xffffffff//GPIO(206)

#ifdef CC_SAKURA_LED_ILLUMINATION
#define CUSTOM_GPIO_LED_RED                     GPIO(254)
#define CUSTOM_GPIO_LED_GREEN                   GPIO(255)
#define CUSTOM_GPIO_LED_BLUE                    GPIO(256)
#define CUSTOM_GPIO_LED_WHITE                   GPIO(257)
#define CUSTOM_GPIO_LED_AMBER                   GPIO(201)
#define CUSTOM_GPIO_LED_PWM_FREQ              (2500)  // request cycle = 400usec = 1sec / 2500

#define CUSTOM_SET_LED_RED_PWM(u4DutyCycle)      PDWNC_PwmSetPwm(0,1,CUSTOM_GPIO_LED_PWM_FREQ,u4DutyCycle)
#define CUSTOM_SET_LED_GREEN_PWM(u4DutyCycle)      PDWNC_PwmSetPwm(1,1,CUSTOM_GPIO_LED_PWM_FREQ,u4DutyCycle)
#define CUSTOM_SET_LED_BLUE_PWM(u4DutyCycle)      PDWNC_PwmSetPwm(2,1,CUSTOM_GPIO_LED_PWM_FREQ,u4DutyCycle)
#define CUSTOM_SET_LED_WHITE_PWM(u4DutyCycle)      PDWNC_PwmSetPwm(3,1,CUSTOM_GPIO_LED_PWM_FREQ,u4DutyCycle)

#define LED_BETA                     (0)
#define ENTRY_CNT_W             (1)
#define LOOP_CNT_W               (2)
#define ENTRY_CNT_RGB          (3)
#define LOOP_CNT_RGB            (4)
#define LED_DUTY_W0              (5)
#define LED_DURATION_W0      (17)
#define LED_DUTY_R0               (29)
#define LED_DUTY_G0               (41)
#define LED_DUTY_B0               (53)
#define LED_DURATION_RGB0   (65)

//#define LED_ANIMA_W_FULLTYPE            (77)
//#define LED_ANIMA_W_ONESHOT_TYPE   (78)
//#define LED_ANIMA_RGB_FULLTYPE            (79)
//#define LED_ANIMA_RGB_ONESHOT_TYPE   (80)

#define LED_ANIMA_PARA_TOTAL                 (77)
#define LED_ANIMA_POINT_NUM                  (12)
#define LED_ANIMA_DURATION_MS                (50)
#define LED_ANIMA_UPDATE_FREQ                (10)  // in ms
#define LED_ANIMA_REAL_DELAY_TIME         (8)  //10 to 8

#define LED_EVENT_ARRAY_TOTAL                (20)

#endif

#ifdef CC_LED_PWM_MODE
#define CUSTOM_RED_PWM_PIN                      (0)         //LED_PWM_0
#define CUSTOM_GREEN_PWM_PIN                    (1)         //LED_PWM_1
#define CUSTOM_BLUE_PWM_PIN                     (2)         //LED_PWM_2
#define CUSTOM_WHITE_PWM_PIN                    (3)         //LED_PWM_3
#define CUSTOM_ERROR_LED_PIN                    (CUSTOM_RED_PWM_PIN)
#define CUSTOM_LED_POWER_ON_VAL                 1
#define CUSTOM_LED_STANDBY_ON_VAL               1
#define CUSTOM_LED_TIMER_ON_VAL                 1

#define CUSTOM_PWM_FREQ                         (50)
#define CUSTOM_PWM_ON_DUTY                      (6)
#define CUSTOM_PWM_OFF_DUTY                     (0)
#else
#define CUSTOM_GPIO_LED_POWER                   0xffffffff//GPIO(207)
#define CUSTOM_GPIO_LED_POWER_SECONDARY         0xffffffff//GPIO(206)
#define CUSTOM_GPIO_LED_TIMER_AMBER             0xffffffff  //OPCTRL(5)     // ADIN5
#define CUSTOM_GPIO_LED_TIMER_GREEN             0xffffffff  // GPIO 26
#define CUSTOM_LED_POWER_ON_VAL                 1
#define CUSTOM_LED_STANDBY_ON_VAL               1
#define CUSTOM_LED_TIMER_ON_VAL                 1
#endif
#define T8032_CTRL_ERR_LED                      CUSTOM_GPIO_LED_STANDBY
#define T8032_CTRL_ERR_LED_ON_VAL               CUSTOM_LED_STANDBY_ON_VAL

// Power Key
#define BUTTON_POWER_KEY                        OPCTRL(5)
//#define BUTTON_POWER_KEY_SRV_NUM                SERVO(3)
//#define POWER_KEY_SRV_AD_HI_THR                 240
//#define POWER_KEY_SRV_AD_LOW_THR                210

// Shutdown status
#define CUSTOM_SHUTDOWN_ERROR                   (1 << 0)
#define CUSTOM_SHUTDOWN_AC_DET                  (1 << 1)
#define CUSTOM_SHUTDOWN_DISABLE_STANDBY_LED     (1 << 2)

#define CUSTOM_GPIO_HOTEL_MODE                  OPCTRL4//GPIO(44)       
#define CUSTOM_HOTEL_MODE_ON_VAL                 0
#define CUSTOM_HOTEL_MODE_OFF_VAL                 (1-CUSTOM_HOTEL_MODE_ON_VAL)

// DVD GPIO
#define CUSTOM_GPIO_DVD_RESET                   0xffffffff//GPIO(12)
#define CUSTOM_DVD_POWER_ON                     1
#define CUSTOM_DVD_POWER_OFF                    0
#endif
//-----------------------------------------------------------------------------
// Macros
//-----------------------------------------------------------------------------

#define INIT_SET_GPIO(gpio, onval)                                  \
    if (DRVCUST_InitQuery(gpio, &i)==0)                             \
    {                                                               \
        if (DRVCUST_InitQuery(onval, (UINT32 *)&i4Val)==0)          \
        {                                                           \
            VERIFY(GPIO_Output((INT32)i, &i4Val) == i4Val);         \
        }                                                           \
    }

#define DOWN_SET_GPIO(gpio, onval)                                  \
    if (DRVCUST_InitQuery(gpio, &i)==0)                             \
    {                                                               \
        if (DRVCUST_InitQuery(onval, (UINT32 *)&i4Val)==0)          \
        {                                                           \
            i4Val = !i4Val;                                         \
            VERIFY(GPIO_Output((INT32)i, &i4Val)== i4Val);          \
        }                                                           \
    }

#define UART_SELECTION_IS_TTY_USB_LOG() \
( \
{ \
    UINT32 uart_sel=0;\
    EEPROM_Read(CUSTOM_UART_SELECTION_ADDR, (UINT32)&uart_sel, 1);\
    ((uart_sel==UART_SELECTION_TTY_USB_LOG)); \
} \
)
#define _IS_USB_LOG_MODE UART_SELECTION_IS_TTY_USB_LOG()

//-----------------------------------------------------------------------------
// Structure definition
//-----------------------------------------------------------------------------
#ifdef CC_SAKURA_LED_ILLUMINATION

typedef enum
{
    eLedCenter = 0,
    eLedSide,
    eLedTypeTotal
}LED_TYPE_T;


typedef enum
{
    eLedWhite = 0,
    eLedRed,
    eLedGreen,
    eLedBlue,
    eLedColorTotal
}LED_COLOR_T;

typedef enum
{
    eLedOff,
    eLedAnimeLit,
    eLedAnimeLoop,
    eLedAnimeBlink,
    eLedAnimeOneShot,
    eLedAnimeOneShotLit,
    eLedAnimeOneShotLoop 
}LED_ANIMATION_T;


/*
typedef struct _LedAnimaParameter
{
    UINT8 u1LedBeta;   //£]¡× ([LED_BETA] + 1) / 256 [0-255]
    UINT8 u1EntryCnt_W;  //number of Entry Action (One Shot) [0-12]
    UINT8 u1LoopCnt_W;   //number of loop Count [0-12]
    UINT8 u1EntryCnt_RGB;  //number of Entry Action (One Shot) [0-12]
    UINT8 u1LoopCnt_RGB;   //number of loop Count [0-12]
    UINT8 aru1LedDuty_W[12];         // PWM duty [0-255]
    UINT8 aru1LedDuration_W[12];   //PWM duratoin [0-255] * 50msec
    UINT8 aru1LedDuty_R[12];          // PWM duty [0-255]
    UINT8 aru1LedDuty_G[12];          // PWM duty [0-255]
    UINT8 aru1LedDuty_B[12];          // PWM duty [0-255]
    UINT8 aru1LedDuration_RGB[12];   //PWM duratoin [0-255] * 50msec

} LED_ANIMA_PARAMETER;
*/

typedef struct _LedEvent
{
    LED_PATTERN_IDX_T eLedPatternIdx;
    LED_ANIMATION_T eLedAnimeType[eLedTypeTotal];
    BOOL fgNewEvent[eLedTypeTotal];
    BOOL fgEventIsDone[eLedTypeTotal];
    UINT32 u4CurrTimePt[eLedTypeTotal];

} LED_EVENT_INFO;

//UINT8 u1EntryCnt;  //number of Entry Action (One Shot) [0-12]
//UINT8 u1LoopCnt;   //number of loop Count [0-12]
//UINT8 aru1LedDuty[12];	 // PWM duty [0-255]
//UINT8 aru1LedDuration[12];	//PWM duratoin [0-255] * 50msec
/*
typedef struct _LedAnima
{
    UINT32 u4LedBeta;        //£]¡× ([LED_BETA] + 1) / 256 [0-255]
    UINT8 u1EntryCnt_W;  //number of Entry Action (One Shot) [0-12]
    UINT8 u1LoopCnt_W;   //number of loop Count [0-12]
    UINT8 u1EntryCnt_RGB;  //number of Entry Action (One Shot) [0-12]
    UINT8 u1LoopCnt_RGB;   //number of loop Count [0-12]
    UINT8 aru1LedDuty_W[LED_ANIMA_POINT_NUM];         // PWM duty [0-255]
    UINT8 aru1LedDuration_W[LED_ANIMA_POINT_NUM];   //PWM duratoin [0-255] * 50msec
    UINT8 aru1LedDuty_R[LED_ANIMA_POINT_NUM];          // PWM duty [0-255]
    UINT8 aru1LedDuty_G[LED_ANIMA_POINT_NUM];          // PWM duty [0-255]
    UINT8 aru1LedDuty_B[LED_ANIMA_POINT_NUM];          // PWM duty [0-255]
    UINT8 aru1LedDuration_RGB[LED_ANIMA_POINT_NUM];   //PWM duratoin [0-255] * 50msec
    LED_ANIMATION_T eLedAnimaType_W;
    LED_ANIMATION_T eLedAnimaType_RGB;

} LED_ANIMATION_STATE;
*/


#else
typedef struct _LedCtrl
{
    D_LED_TYPE eLedType;
    UINT32 u4LedState;
} LED_CTRL_STATE;
#endif
//-----------------------------------------------------------------------------
// Prototypes
//-----------------------------------------------------------------------------
void GPIO_CustomPowerOnFunc(void);
void GPIO_CustomPowerOffFunc(UINT32 u4PowerDownEn);
#if (defined(CC_SAKURA_LED_ILLUMINATION) && !(defined(CC_MTK_LOADER) || defined(CC_MTK_PRELOADER)))
void GPIO_CustomLedCtrlFunc(LED_PATTERN_IDX_T eLedPatternIdx);
#else
void GPIO_CustomLedCtrlFunc(LED_STATUS_T eLedStatus);
#endif
void CustomPowerOnSequence(void);
void CustomPowerOffSequence(void);
UINT32 CustomModelQueryFunc(VOID);
void _LdrUpgradeBreakFunc(void);
void _LdrUpgradeDlmCbFunc(UINT32 u4BufSize);
void _LdrUpgradeDlmGetTagVerCbFunc(UCHAR* ucTagName, CHAR *szCurVer);
INT32 _LdrUpgradeDlmCmpTagVerCbFunc(UCHAR* ucTagName, CHAR *szCurVer, CHAR *szUpgVer);
#endif /* CUSTOM_GPIO_H*/

