blob: 8b55b6c5a1f297aabdf940bfa48bbc36a1724dac [file] [log] [blame]
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <stdint.h>
#include <string.h>
#include <dlfcn.h>
#include "gsw_hwpin_interface.h"
#include "gsw_hal_errcode.h"
#include "gsw_log_interface.h"
#define GSW_GPIO "[HAL][GSW_GPIO]"
#define wakeout 117
typedef enum{
GSW_HAL_PINDIR_IN,
GSW_HAL_PINDIR_OUT,
}gsw_hal_pinDirection;
typedef enum{
GSW_HAL_LEVEL_LOW,
GSW_HAL_LEVEL_HIGH,
}gsw_hal_pinLevel;
typedef enum{
GSW_HAL_PULL_NULL,
GSW_HAL_PULL_DOWN,
GSW_HAL_PULL_UP,
}gsw_hal_pinPullSel;
typedef enum{
GSW_HAL_USB_DISCONNECTED,
GSW_HAL_USB_CONNECTED,
}gsw_hal_usbStatus;
//typedef void (*mbtk_log)(int level, const char *format,...);
//static mbtk_log fun_ptr_log = NULL;
int gpio_check(int gpio)
{
char buffer[128];
memset(buffer,0,128);
sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio);
return access(buffer , F_OK);
}
static int gpio_export(int gpio)
{
int file=-1;
int result =-1;
char pin_index_buffer[5]= {0};
if (gpio_check(gpio) == 0)
{
LOGE(GSW_GPIO, "export has been add\n");
return 0;
}
file = open("/sys/class/gpio/export",O_WRONLY);
if(file == -1)
{
LOGE(GSW_GPIO, "Open gpio export file fail.");
return -1;
}
sprintf(pin_index_buffer,"%d", gpio);
result = write(file,pin_index_buffer,strlen(pin_index_buffer));
if(result < 0)
{
LOGE(GSW_GPIO, "Gpio[%d] export fail.", gpio);
close(file);
return -1;
}
close(file);
return 0;
}
static int gpio_unexport(int gpio)
{
int file=-1;
int result =-1;
char pin_index_buffer[5]= {0};
if (gpio_check(gpio) == -1)
{
LOGE(GSW_GPIO, "export has been not add\n");
return -1;
}
file = open("/sys/class/gpio/unexport",O_WRONLY);
if(file == -1)
{
LOGE(GSW_GPIO, "Open gpio unexport file fail.");
return -1;
}
sprintf(pin_index_buffer,"%d", gpio);
result=write(file,pin_index_buffer,strlen(pin_index_buffer));
if(result < 0)
{
close(file);
LOGE(GSW_GPIO, "Gpio[%d] unexport fail.", gpio);
return -1;
}
close(file);
return 0;
}
int32_t gsw_gpio_SetDirection(uint32_t gpioNum, gsw_hal_pinDirection dir)
{
char buffer[128]= {0};
int file =-1;
int result =-1;
char direction[10] = {0};
memset(buffer,0,128);
if (gpio_check(gpioNum) == -1)
{
LOGD(GSW_GPIO, "in dir export has been not add\n");
gpio_export(gpioNum);
}
sprintf(buffer,"/sys/class/gpio/gpio%u/direction", gpioNum);
file = open(buffer, O_WRONLY);
if(file == -1)
{
LOGE(GSW_GPIO, "Open gpio[%d] direct fail.", gpioNum);
return -1;
}
if (dir == GSW_HAL_PINDIR_IN)
sprintf(direction,"in");
else if (dir == GSW_HAL_PINDIR_OUT)
sprintf(direction,"out");
result = write(file,direction,strlen(direction));
if(result != strlen(direction))
{
LOGE(GSW_GPIO, "Set gpio[%d] direct fail.", gpioNum);
close(file);
gpio_unexport(gpioNum);
return GSW_HAL_NORMAL_FAIL;
}
close(file);
gpio_unexport(gpioNum);
return GSW_HAL_SUCCESS;
}
gsw_hal_pinLevel gsw_gpio_GetLevel(uint32_t gpioNum)
{
char buffer[50];
char path[128];
int file =-1;
int result =-1;
memset(path,0,sizeof(path));
memset(buffer,0,sizeof(buffer));
if (gpio_check(gpioNum) == -1)
{
LOGD(GSW_GPIO, "in get value export has been not add\n");
gpio_export(gpioNum);
}
sprintf(path,"/sys/class/gpio/gpio%u/value", gpioNum);
file = open(path,O_RDONLY);
if(file == -1)
{
LOGE(GSW_GPIO, "Open gpio[%d] fail.", gpioNum);
return -1;
}
result = read(file,buffer,50);
if(result <= 0)
{
LOGE(GSW_GPIO, "Get gpio[%d] value fail", gpioNum);
close(file);
gpio_unexport(gpioNum);
return GSW_HAL_NORMAL_FAIL;
}
close(file);
gpio_unexport(gpioNum);
return atoi(buffer);
}
int32_t gsw_gpio_SetLevel(uint32_t gpioNum, gsw_hal_pinLevel level)
{
char buffer[128]= {0};
int file =-1;
int result =-1;
memset(buffer,0,50);
if (gpio_check(gpioNum) == -1)
{
LOGD(GSW_GPIO, "in set value export has been not add\n");
gpio_export(gpioNum);
}
sprintf(buffer,"/sys/class/gpio/gpio%u/value", gpioNum);
file = open(buffer,O_WRONLY);
if(file == -1)
{
LOGE(GSW_GPIO, "Open gpio[%d] value fail.", gpioNum);
return -1;
}
if(level == GSW_HAL_LEVEL_LOW) {
result = write(file,"0",1);
} else {
result = write(file,"1",1);
}
if(result != 1)
{
LOGE(GSW_GPIO, "Set gpio[%d] value fail err =%d.", gpioNum, errno);
close(file);
gpio_unexport(gpioNum);
return GSW_HAL_NORMAL_FAIL;
}
close(file);
gpio_unexport(gpioNum);
return GSW_HAL_SUCCESS;
}
int32_t gsw_gpio_Init(uint32_t gpioNum, gsw_hal_pinDirection dir, gsw_hal_pinLevel level, gsw_hal_pinPullSel pullsel)
{
if (dir != 1 && dir != 0)
{
LOGE(GSW_GPIO, "[lynq_gpio_init] direction fail.");
return -1;
}
if (level != 1 && level != 0)
{
LOGE(GSW_GPIO, "[lynq_gpio_init] value fail.");
return GSW_HAL_NORMAL_FAIL;
}
if(gpio_export(gpioNum))
{
LOGE(GSW_GPIO, "[lynq_gpio_init]gpio_export fail.");
return GSW_HAL_NORMAL_FAIL;
}
if(gsw_gpio_SetDirection(gpioNum, dir))
{
LOGE(GSW_GPIO, "[lynq_gpio_init]gpio_direct_set fail.");
return GSW_HAL_NORMAL_FAIL;
}
if(dir == 1 && (gsw_gpio_SetLevel(gpioNum, level) != 0))
{
LOGE(GSW_GPIO, "[lynq_gpio_init]gpio_value_set fail.");
return GSW_HAL_NORMAL_FAIL;
}
return GSW_HAL_SUCCESS;
}
int gsw_hwpin_is_usb_connected(void)
{
FILE *fp;
char cmd[128] = {0};
char tmp_rsp[20] = {0};
//char *CONNECTED = "configured";
char *DISCONNECTED = "not attached";
sprintf(cmd,"cat /sys/devices/platform/soc/d4200000.axi/c0000000.usb/udc/c0000000.usb/state");
fp=popen(cmd, "r");
if (fgets(tmp_rsp,sizeof(tmp_rsp),fp) == NULL)
LOGE(GSW_GPIO, "gsw_hwpin_is_usb_connected fail.");
pclose(fp);
if (strncmp(tmp_rsp,DISCONNECTED,strlen(DISCONNECTED)) == 0)
return GSW_HAL_USB_DISCONNECTED;
else
return GSW_HAL_USB_CONNECTED;
}
int32_t gsw_setWakeupLevel(gsw_hal_pinLevel level)
{
if (gsw_gpio_SetLevel(wakeout, level) == GSW_HAL_NORMAL_FAIL)
{
return GSW_HAL_NORMAL_FAIL;
}
return GSW_HAL_SUCCESS;
}
#define SYS_LED_DEV_TRIGGER_NODE "/sys/class/leds/red/trigger"
#define SYS_LED_DEV_BRIGHTNES_NODE "/sys/class/leds/red/brightness"
#define SYS_LED_DEV_DELAY_OFF_NODE "/sys/class/leds/red/delay_off"
#define SYS_LED_DEV_DELAY_ON_NODE "/sys/class/leds/red/delay_on"
#define EMMC_HEALTH_FILE "/sys/kernel/debug/mmc0/mmc0:0001/health"
#define MDM_RESET_MCU 19
#define MDM_BOOT_MCU_CTRL 31
#define MDM_PWRMDM_EN 116
#define MDM_WAKEUP_MCU 117
int gsw_hwpin_mcu_reset(void)
{
if (0 != gsw_gpio_Init(MDM_RESET_MCU, GSW_HAL_PINDIR_OUT, GSW_HAL_LEVEL_LOW, GSW_HAL_PULL_DOWN))
{
LOGE(GSW_GPIO, "init GPIO %d fail!", MDM_RESET_MCU);
return GSW_HAL_NORMAL_FAIL;
}
usleep(1000);
if (0 != gsw_gpio_SetLevel(MDM_RESET_MCU, GSW_HAL_LEVEL_HIGH))
{
LOGE(GSW_GPIO, "set GPIO %d : %d fail!", MDM_RESET_MCU, GSW_HAL_LEVEL_HIGH);
return GSW_HAL_NORMAL_FAIL;
}
usleep(100000);
if (0 != gsw_gpio_SetLevel(MDM_RESET_MCU, GSW_HAL_LEVEL_LOW))
{
LOGE(GSW_GPIO, "set GPIO %d : %d fail!", MDM_RESET_MCU, GSW_HAL_LEVEL_LOW);
return GSW_HAL_NORMAL_FAIL;
}
usleep(100000);
return GSW_HAL_SUCCESS;
}
int gsw_hwpin_mcu_enter_bootmode(void)
{
if (0 != gsw_gpio_Init(MDM_BOOT_MCU_CTRL, GSW_HAL_PINDIR_OUT, GSW_HAL_LEVEL_LOW, GSW_HAL_PULL_DOWN))
{
LOGE(GSW_GPIO, "init GPIO %d fail!", MDM_BOOT_MCU_CTRL);
return GSW_HAL_NORMAL_FAIL;
}
usleep(1000);
if (0 != gsw_gpio_SetLevel(MDM_BOOT_MCU_CTRL, GSW_HAL_LEVEL_HIGH))
{
LOGE(GSW_GPIO, "set GPIO %d : %d fail!", MDM_BOOT_MCU_CTRL, GSW_HAL_LEVEL_HIGH);
return GSW_HAL_NORMAL_FAIL;
}
usleep(100000);
return gsw_hwpin_mcu_reset();
}
int gsw_hwpin_mcu_exit_bootmode(void)
{
if (0 != gsw_gpio_Init(MDM_BOOT_MCU_CTRL, GSW_HAL_PINDIR_OUT, GSW_HAL_LEVEL_LOW, GSW_HAL_PULL_DOWN))
{
LOGE(GSW_GPIO, "init GPIO %d fail!", MDM_BOOT_MCU_CTRL);
return GSW_HAL_NORMAL_FAIL;
}
usleep(1000);
if (0 != gsw_gpio_SetLevel(MDM_BOOT_MCU_CTRL, GSW_HAL_LEVEL_LOW))
{
LOGE(GSW_GPIO, "set GPIO %d : %d fail!", MDM_BOOT_MCU_CTRL, GSW_HAL_LEVEL_LOW);
return GSW_HAL_NORMAL_FAIL;
}
usleep(100000);
return gsw_hwpin_mcu_reset();
}
int gsw_hwpin_soc_power_keep(void)
{
return 0;
}
int gsw_hwpin_soc_power_release(void)
{
return 0;
}
static int _gsw_hwpin_soc_power_down(void)
{
if (0 != gsw_gpio_Init(MDM_PWRMDM_EN, GSW_HAL_PINDIR_OUT, GSW_HAL_LEVEL_LOW, GSW_HAL_PULL_DOWN))
{
LOGE(GSW_GPIO, "init GPIO %d fail!", MDM_BOOT_MCU_CTRL);
return GSW_HAL_NORMAL_FAIL;
}
usleep(1000);
if (0 != gsw_gpio_SetLevel(MDM_PWRMDM_EN, GSW_HAL_LEVEL_HIGH))
{
LOGE(GSW_GPIO, "set GPIO %d : %d fail!", MDM_PWRMDM_EN, GSW_HAL_LEVEL_HIGH);
return GSW_HAL_NORMAL_FAIL;
}
return GSW_HAL_SUCCESS;
}
int gsw_hwpin_tcam_reset(void)
{
if (gsw_hwpin_mcu_reset() < 0)
return GSW_HAL_NORMAL_FAIL;
usleep(10000);
return _gsw_hwpin_soc_power_down();
}
int gsw_hwpin_soc_wakeup_mcu(void)
{
if (0 != gsw_setWakeupLevel(GSW_HAL_LEVEL_HIGH))
{
LOGE(GSW_GPIO, "wakeup mcu fail!");
return GSW_HAL_NORMAL_FAIL;
}
return GSW_HAL_SUCCESS;
}
int gsw_hwpin_soc_wakeup_mcu_gpio_set_low(void)
{
if (0 != gsw_setWakeupLevel(GSW_HAL_LEVEL_LOW))
{
LOGE(GSW_GPIO, "wakeup mcu set low fail!");
return GSW_HAL_NORMAL_FAIL;
}
return GSW_HAL_SUCCESS;
}
static int _file_read(const char *file, char *value, unsigned int len)
{
int fd = 0;
int ret = 0;
fd = open(file, O_RDONLY);
if(fd <= 0) {
LOGE(GSW_GPIO, "[hwpin] open file error %d,%s", fd, file);
return GSW_HAL_ERROR_FILE_OPEN;
}
if(value && len > 0) {
ret = read(fd, value, len);
}
close(fd);
if(ret < 0) {
LOGE(GSW_GPIO, "[hwpin] read file error %d,%s",ret, file);
}
return ret;
}
static int _file_write(const char *file, char *value)
{
int fd = 0;
int ret = 0;
fd = open(file, O_WRONLY);
if(fd <= 0) {
LOGE(GSW_GPIO, "[hwpin] open file error %d,%s", fd, file);
return GSW_HAL_ERROR_FILE_OPEN;
}
if(value) {
ret = write(fd, value, strlen(value));
}
close(fd);
if(ret < 0) {
LOGE(GSW_GPIO, "[hwpin] write file error %d,%s",ret, file);
}
return ret;
}
int gsw_hwpin_led_red_on(void)
{
return GSW_HAL_SUCCESS;
}
int gsw_hwpin_led_red_off(void)
{
return GSW_HAL_SUCCESS;
}
int gsw_hwpin_led_red_blink(int delay_on, int delay_off)
{
return GSW_HAL_SUCCESS;
}
static bool g_aging_flag = false;
int gsw_hwpin_led_green_on(void)
{
if (true == g_aging_flag)
{
LOGI(GSW_GPIO, "green on LED file fail g_aging_flag = %d", g_aging_flag);
return GSW_HAL_SUCCESS;
}
if (access(SYS_LED_DEV_TRIGGER_NODE, W_OK) != 0)
{
LOGE(GSW_GPIO, "green_on LED file fail %s", SYS_LED_DEV_TRIGGER_NODE);
return GSW_HAL_NORMAL_FAIL;
}
if (_file_write(SYS_LED_DEV_TRIGGER_NODE, "none") <= 0)
{
LOGE(GSW_GPIO, "green_on LED write fail %s", SYS_LED_DEV_TRIGGER_NODE);
return GSW_HAL_NORMAL_FAIL;
}
if (access(SYS_LED_DEV_BRIGHTNES_NODE, W_OK) != 0)
{
LOGE(GSW_GPIO, "green_on LED file fail %s", SYS_LED_DEV_BRIGHTNES_NODE);
return GSW_HAL_NORMAL_FAIL;
}
if (_file_write(SYS_LED_DEV_BRIGHTNES_NODE, "1") <= 0)
{
LOGE(GSW_GPIO, "green_on LED write fail %s", SYS_LED_DEV_BRIGHTNES_NODE);
return GSW_HAL_NORMAL_FAIL;
}
return GSW_HAL_SUCCESS;
}
int gsw_hwpin_led_green_off(void)
{
if (true == g_aging_flag)
{
LOGI(GSW_GPIO, "green off LED file fail g_aging_flag = %d", g_aging_flag);
return GSW_HAL_SUCCESS;
}
if (access(SYS_LED_DEV_TRIGGER_NODE, W_OK) != 0)
{
LOGE(GSW_GPIO, "green_on LED file fail %s", SYS_LED_DEV_TRIGGER_NODE);
return GSW_HAL_NORMAL_FAIL;
}
if (_file_write(SYS_LED_DEV_TRIGGER_NODE, "none") <= 0)
{
LOGE(GSW_GPIO, "green_on LED write fail %s", SYS_LED_DEV_TRIGGER_NODE);
return GSW_HAL_NORMAL_FAIL;
}
if (access(SYS_LED_DEV_BRIGHTNES_NODE, W_OK) != 0)
{
LOGE(GSW_GPIO, "green_on LED file fail %s", SYS_LED_DEV_BRIGHTNES_NODE);
return GSW_HAL_NORMAL_FAIL;
}
if (_file_write(SYS_LED_DEV_BRIGHTNES_NODE, "0") <= 0)
{
LOGE(GSW_GPIO, "green_on LED write fail %s", SYS_LED_DEV_BRIGHTNES_NODE);
return GSW_HAL_NORMAL_FAIL;
}
return GSW_HAL_SUCCESS;
}
int gsw_hwpin_led_green_blink(int delay_on, int delay_off)
{
char buf[32] = {0};
if (true == g_aging_flag)
{
LOGI(GSW_GPIO, "green blink LED file fail g_aging_flag = %d", g_aging_flag);
return GSW_HAL_SUCCESS;
}
if (access(SYS_LED_DEV_TRIGGER_NODE, W_OK) != 0)
{
LOGE(GSW_GPIO, "green_on LED file fail %s", SYS_LED_DEV_TRIGGER_NODE);
return GSW_HAL_NORMAL_FAIL;
}
if (_file_write(SYS_LED_DEV_TRIGGER_NODE, "timer") <= 0)
{
LOGE(GSW_GPIO, "green_on LED write fail %s", SYS_LED_DEV_TRIGGER_NODE);
return GSW_HAL_NORMAL_FAIL;
}
if (access(SYS_LED_DEV_DELAY_OFF_NODE, W_OK) != 0)
{
LOGE(GSW_GPIO, "green_on LED file fail %s", SYS_LED_DEV_DELAY_OFF_NODE);
return GSW_HAL_NORMAL_FAIL;
}
sprintf(buf, "%d", delay_off);
if (_file_write(SYS_LED_DEV_DELAY_OFF_NODE, buf) <= 0)
{
LOGE(GSW_GPIO, "green_on LED write fail %s", SYS_LED_DEV_DELAY_OFF_NODE);
return GSW_HAL_NORMAL_FAIL;
}
if (access(SYS_LED_DEV_DELAY_ON_NODE, W_OK) != 0)
{
LOGE(GSW_GPIO, "green_on LED file fail %s", SYS_LED_DEV_DELAY_ON_NODE);
return GSW_HAL_NORMAL_FAIL;
}
sprintf(buf, "%d", delay_on);
if (_file_write(SYS_LED_DEV_DELAY_ON_NODE, buf) <= 0)
{
LOGE(GSW_GPIO, "green_on LED write fail %s", SYS_LED_DEV_DELAY_ON_NODE);
return GSW_HAL_NORMAL_FAIL;
}
return GSW_HAL_SUCCESS;
}
int gsw_hwpin_get_emmc_health(int *written_mbytes, int *health)
{
char buffer[32] = {0};
char *p = buffer;
char *written_str = buffer;
char *health_str = NULL;
if (!written_mbytes && !health)
return GSW_HAL_NORMAL_FAIL;
if (_file_read(EMMC_HEALTH_FILE, buffer, sizeof(buffer)) <= 0)
return -1;
/* [0000017500-003], all 17500MB has be writtend, 003 is health value(smaller is better) */
while (*p != '\0')
{
if ('-' == *p)
{
health_str = p + 1;
*p = '\0';
break;
}
p++;
}
if (written_mbytes)
sscanf(written_str, "%10d", written_mbytes);
if (health)
sscanf(health_str, "%10d", health);
return GSW_HAL_SUCCESS;
}
bool gsw_hwpin_check_emmc_readonly(void)
{
return false;//defualt return normal
}
int gsw_hwpin_factorytest_ctrl_led(bool aging_flag, int delay_on, int delay_off)
{
LOGI(GSW_GPIO, "[hwpin] factory test ctrl led entry");
if (false == g_aging_flag)
{
if (false == aging_flag)
{
LOGI(GSW_GPIO, "[hwpin] factory test ctrl led aging_flag = %d", aging_flag);
return GSW_HAL_SUCCESS;
}
g_aging_flag = aging_flag;
LOGI(GSW_GPIO, "g_aging_flag 1 value %d", g_aging_flag);
}
else if (false == aging_flag)
{
g_aging_flag = aging_flag;
LOGI(GSW_GPIO, "g_aging_flag 2 value %d", g_aging_flag);
}
gsw_hwpin_led_green_blink(delay_on, delay_off);
LOGI(GSW_GPIO, "[hwpin] factory test ctrl led exit");
return GSW_HAL_SUCCESS;
}
int gsw_hwpin_mute_test(int value)
{
return GSW_HAL_SUCCESS;
}
int gsw_hwpin_fstrim_emmc(const char *emmc_path)
{
char cmd[256] = {0};
int ret = 0;
if(!emmc_path || access(emmc_path, F_OK) != 0)
{
LOGE(GSW_GPIO, "Invalid path : %s", emmc_path);
return ret;
}
sprintf(cmd, "fstrim -v %s", emmc_path);
ret = system(cmd);
LOGI(GSW_GPIO, "command %s, ret : %d", cmd, ret);
return GSW_HAL_SUCCESS;
}