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

/** @file x_gpio.h
 *  x_gpio.h declare all pinmux/gpio related function and parameters.
 */


#ifndef X_GPIO_H
#define X_GPIO_H


//-----------------------------------------------------------------------------
// Configuration
//-----------------------------------------------------------------------------

#define GPIO_THREAD_ENABLE      0


//-----------------------------------------------------------------------------
// Constant definitions
//-----------------------------------------------------------------------------

#define NORMAL_PINMUX_BITS      160
#define TOTAL_PINMUX_BITS       (NORMAL_PINMUX_BITS + 32 + 1)

#define NORMAL_GPIO_NUM         192
#define TOTAL_OPCTRL_NUM        13
#define TOTAL_GPIO_NUM          (NORMAL_GPIO_NUM + 8 + TOTAL_OPCTRL_NUM)
#define TOTAL_GPIO_IDX          ((NORMAL_GPIO_NUM + 31) >> 5)
#define GPIO_INDEX_MASK         ((1 << 5) - 1)
#define GPIO_TO_INDEX(gpio)     (((UINT32)(gpio)) >> 5)

#define GPIO_EDGE_INTR_NUM      27
#define TOTAL_EDGE_IDX          ((GPIO_EDGE_INTR_NUM + 31) >> 5)
#define EDGE_INDEX_MASK         ((1 << 5) - 1)


//-----------------------------------------------------------------------------
// Type definitions
//-----------------------------------------------------------------------------

typedef void (* PFN_GPIO_CALLBACK)(INT32 i4Gpio, BOOL fgStatus);

typedef enum {
    GPIO_TYPE_NONE = 0,
    GPIO_TYPE_INTR_FALL,        /* Only GPIO 0 ~ 23 */
    GPIO_TYPE_INTR_RISE,        /* Only GPIO 0 ~ 23 */
    GPIO_TYPE_INTR_BOTH,        /* Only GPIO 0 ~ 23 */
    GPIO_TYPE_INTR_LEVEL,       /* Polarity only for gpio 0 ~ 158 */
#if GPIO_THREAD_ENABLE
    GPIO_TYPE_POLL_FALL,
    GPIO_TYPE_POLL_RISE,
    GPIO_TYPE_POLL_BOTH,
#endif /* GPIO_THREAD_ENABLE */
    GPIO_TYPE_LAST
} GPIO_TYPE;


//-----------------------------------------------------------------------------
// Public function declaration
//-----------------------------------------------------------------------------

/* Check/Set Gpio pinmux. */
EXTERN INT32 BSP_PinGpioSel(INT32 i4Gpio);
EXTERN INT32 BSP_PinGpioGet(INT32 i4Gpio, INT32 *pi4PinSel);

/* Register query/set functions */
EXTERN INT32 GPIO_Output(INT32 i4GpioNum, const INT32 *pfgSet);
EXTERN INT32 GPIO_Enable(INT32 i4GpioNum, const INT32 *pfgSet);
EXTERN INT32 GPIO_Input(INT32 i4GpioNum);
EXTERN INT32 GPIO_Intrq(INT32 i4GpioNum, const INT32 *pfgSet);

/* GPIO functions. */
EXTERN INT32 GPIO_Query(INT32 i4Gpio, INT32 *pi4Intr, INT32 *pi4Fall, INT32 *pi4Rise);
EXTERN INT32 GPIO_Reg(INT32 i4Gpio, GPIO_TYPE eType, PFN_GPIO_CALLBACK pfnCallback);
EXTERN INT32 GPIO_SetOut(INT32 i4GpioNum, INT32 i4Val);
EXTERN INT32 GPIO_GetIn(INT32 i4GpioNum);

#endif //X_GPIO_H
