b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 1 | #include <stdio.h>
|
| 2 | #include <stdlib.h>
|
| 3 | #include <unistd.h>
|
| 4 | #include <fcntl.h>
|
| 5 | #include <errno.h>
|
| 6 | #include <stdint.h>
|
| 7 | #include <string.h>
|
| 8 | #include <dlfcn.h>
|
| 9 |
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 10 | #include "gsw_hwpin_interface.h"
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 11 | #include "gsw_hal_errcode.h"
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 12 | #include "gsw_log_interface.h"
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 13 |
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 14 |
|
l.yang | 6a42e4d | 2025-05-28 01:04:20 -0700 | [diff] [blame] | 15 | #define GSW_GPIO "[HAL][GSW_GPIO]"
|
| 16 |
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 17 |
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 18 | #define wakeout 117
|
| 19 |
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 20 | typedef enum{
|
| 21 | GSW_HAL_PINDIR_IN,
|
| 22 | GSW_HAL_PINDIR_OUT,
|
| 23 | }gsw_hal_pinDirection;
|
| 24 |
|
| 25 | typedef enum{
|
| 26 | GSW_HAL_LEVEL_LOW,
|
| 27 | GSW_HAL_LEVEL_HIGH,
|
| 28 | }gsw_hal_pinLevel;
|
| 29 |
|
| 30 | typedef enum{
|
| 31 | GSW_HAL_PULL_NULL,
|
| 32 | GSW_HAL_PULL_DOWN,
|
| 33 | GSW_HAL_PULL_UP,
|
| 34 | }gsw_hal_pinPullSel;
|
| 35 |
|
| 36 | typedef enum{
|
| 37 | GSW_HAL_USB_DISCONNECTED,
|
| 38 | GSW_HAL_USB_CONNECTED,
|
| 39 | }gsw_hal_usbStatus;
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 40 | //typedef void (*mbtk_log)(int level, const char *format,...);
|
| 41 | //static mbtk_log fun_ptr_log = NULL;
|
| 42 |
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 43 |
|
| 44 | int gpio_check(int gpio)
|
| 45 | {
|
| 46 | char buffer[128];
|
| 47 | memset(buffer,0,128);
|
| 48 | sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio);
|
| 49 | return access(buffer , F_OK);
|
| 50 | }
|
| 51 |
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 52 |
|
| 53 | static int gpio_export(int gpio)
|
| 54 | {
|
| 55 | int file=-1;
|
| 56 | int result =-1;
|
| 57 | char pin_index_buffer[5]= {0};
|
| 58 |
|
| 59 | if (gpio_check(gpio) == 0)
|
| 60 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 61 | LOGE(GSW_GPIO, "export has been add\n");
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 62 | return 0;
|
| 63 | }
|
| 64 |
|
| 65 | file = open("/sys/class/gpio/export",O_WRONLY);
|
| 66 | if(file == -1)
|
| 67 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 68 | LOGE(GSW_GPIO, "Open gpio export file fail.");
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 69 | return -1;
|
| 70 | }
|
| 71 |
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 72 | sprintf(pin_index_buffer,"%d", gpio);
|
| 73 | result = write(file,pin_index_buffer,strlen(pin_index_buffer));
|
| 74 | if(result < 0)
|
| 75 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 76 | LOGE(GSW_GPIO, "Gpio[%d] export fail.", gpio);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 77 | close(file);
|
| 78 | return -1;
|
| 79 | }
|
| 80 | close(file);
|
| 81 |
|
| 82 | return 0;
|
| 83 | }
|
| 84 |
|
| 85 | static int gpio_unexport(int gpio)
|
| 86 | {
|
| 87 | int file=-1;
|
| 88 | int result =-1;
|
| 89 | char pin_index_buffer[5]= {0};
|
| 90 | if (gpio_check(gpio) == -1)
|
| 91 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 92 | LOGE(GSW_GPIO, "export has been not add\n");
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 93 | return -1;
|
| 94 | }
|
| 95 |
|
| 96 | file = open("/sys/class/gpio/unexport",O_WRONLY);
|
| 97 | if(file == -1)
|
| 98 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 99 | LOGE(GSW_GPIO, "Open gpio unexport file fail.");
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 100 | return -1;
|
| 101 | }
|
| 102 |
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 103 | sprintf(pin_index_buffer,"%d", gpio);
|
| 104 | result=write(file,pin_index_buffer,strlen(pin_index_buffer));
|
| 105 | if(result < 0)
|
| 106 | {
|
| 107 | close(file);
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 108 | LOGE(GSW_GPIO, "Gpio[%d] unexport fail.", gpio);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 109 | return -1;
|
| 110 | }
|
| 111 | close(file);
|
| 112 |
|
| 113 | return 0;
|
| 114 | }
|
| 115 |
|
| 116 | int32_t gsw_gpio_SetDirection(uint32_t gpioNum, gsw_hal_pinDirection dir)
|
| 117 | {
|
| 118 | char buffer[128]= {0};
|
| 119 | int file =-1;
|
| 120 | int result =-1;
|
| 121 | char direction[10] = {0};
|
| 122 | memset(buffer,0,128);
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 123 |
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 124 | if (gpio_check(gpioNum) == -1)
|
| 125 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 126 | LOGD(GSW_GPIO, "in dir export has been not add\n");
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 127 | gpio_export(gpioNum);
|
| 128 | }
|
| 129 |
|
| 130 |
|
| 131 | sprintf(buffer,"/sys/class/gpio/gpio%u/direction", gpioNum);
|
| 132 | file = open(buffer, O_WRONLY);
|
| 133 | if(file == -1)
|
| 134 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 135 | LOGE(GSW_GPIO, "Open gpio[%d] direct fail.", gpioNum);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 136 | return -1;
|
| 137 | }
|
| 138 | if (dir == GSW_HAL_PINDIR_IN)
|
| 139 | sprintf(direction,"in");
|
| 140 | else if (dir == GSW_HAL_PINDIR_OUT)
|
| 141 | sprintf(direction,"out");
|
| 142 |
|
| 143 | result = write(file,direction,strlen(direction));
|
| 144 | if(result != strlen(direction))
|
| 145 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 146 | LOGE(GSW_GPIO, "Set gpio[%d] direct fail.", gpioNum);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 147 | close(file);
|
| 148 | gpio_unexport(gpioNum);
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 149 | return GSW_HAL_NORMAL_FAIL;
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 150 | }
|
| 151 | close(file);
|
| 152 | gpio_unexport(gpioNum);
|
| 153 | return GSW_HAL_SUCCESS;
|
| 154 | }
|
| 155 |
|
| 156 | gsw_hal_pinLevel gsw_gpio_GetLevel(uint32_t gpioNum)
|
| 157 | {
|
| 158 | char buffer[50];
|
| 159 | char path[128];
|
| 160 | int file =-1;
|
| 161 | int result =-1;
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 162 | memset(path,0,sizeof(path));
|
| 163 | memset(buffer,0,sizeof(buffer));
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 164 |
|
| 165 | if (gpio_check(gpioNum) == -1)
|
| 166 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 167 | LOGD(GSW_GPIO, "in get value export has been not add\n");
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 168 | gpio_export(gpioNum);
|
| 169 | }
|
| 170 | sprintf(path,"/sys/class/gpio/gpio%u/value", gpioNum);
|
| 171 | file = open(path,O_RDONLY);
|
| 172 | if(file == -1)
|
| 173 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 174 | LOGE(GSW_GPIO, "Open gpio[%d] fail.", gpioNum);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 175 | return -1;
|
| 176 | }
|
| 177 | result = read(file,buffer,50);
|
| 178 | if(result <= 0)
|
| 179 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 180 | LOGE(GSW_GPIO, "Get gpio[%d] value fail", gpioNum);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 181 | close(file);
|
| 182 | gpio_unexport(gpioNum);
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 183 | return GSW_HAL_NORMAL_FAIL;
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 184 | }
|
| 185 | close(file);
|
| 186 | gpio_unexport(gpioNum);
|
| 187 | return atoi(buffer);
|
| 188 | }
|
| 189 |
|
| 190 | int32_t gsw_gpio_SetLevel(uint32_t gpioNum, gsw_hal_pinLevel level)
|
| 191 | {
|
| 192 | char buffer[128]= {0};
|
| 193 | int file =-1;
|
| 194 | int result =-1;
|
| 195 | memset(buffer,0,50);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 196 |
|
| 197 | if (gpio_check(gpioNum) == -1)
|
| 198 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 199 | LOGD(GSW_GPIO, "in set value export has been not add\n");
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 200 | gpio_export(gpioNum);
|
| 201 | }
|
| 202 |
|
| 203 | sprintf(buffer,"/sys/class/gpio/gpio%u/value", gpioNum);
|
| 204 | file = open(buffer,O_WRONLY);
|
| 205 | if(file == -1)
|
| 206 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 207 | LOGE(GSW_GPIO, "Open gpio[%d] value fail.", gpioNum);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 208 | return -1;
|
| 209 | }
|
| 210 | if(level == GSW_HAL_LEVEL_LOW) {
|
| 211 | result = write(file,"0",1);
|
| 212 | } else {
|
| 213 | result = write(file,"1",1);
|
| 214 | }
|
| 215 | if(result != 1)
|
| 216 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 217 | LOGE(GSW_GPIO, "Set gpio[%d] value fail err =%d.", gpioNum, errno);
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 218 | close(file);
|
| 219 | gpio_unexport(gpioNum);
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 220 | return GSW_HAL_NORMAL_FAIL;
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 221 | }
|
| 222 | close(file);
|
| 223 | gpio_unexport(gpioNum);
|
| 224 | return GSW_HAL_SUCCESS;
|
| 225 | }
|
| 226 |
|
| 227 |
|
| 228 | int32_t gsw_gpio_Init(uint32_t gpioNum, gsw_hal_pinDirection dir, gsw_hal_pinLevel level, gsw_hal_pinPullSel pullsel)
|
| 229 | {
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 230 | if (dir != 1 && dir != 0)
|
| 231 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 232 | LOGE(GSW_GPIO, "[lynq_gpio_init] direction fail.");
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 233 | return -1;
|
| 234 | }
|
| 235 |
|
| 236 | if (level != 1 && level != 0)
|
| 237 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 238 | LOGE(GSW_GPIO, "[lynq_gpio_init] value fail.");
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 239 | return GSW_HAL_NORMAL_FAIL;
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 240 | }
|
| 241 |
|
| 242 | if(gpio_export(gpioNum))
|
| 243 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 244 | LOGE(GSW_GPIO, "[lynq_gpio_init]gpio_export fail.");
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 245 | return GSW_HAL_NORMAL_FAIL;
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 246 | }
|
| 247 |
|
| 248 | if(gsw_gpio_SetDirection(gpioNum, dir))
|
| 249 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 250 | LOGE(GSW_GPIO, "[lynq_gpio_init]gpio_direct_set fail.");
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 251 | return GSW_HAL_NORMAL_FAIL;
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 252 | }
|
| 253 |
|
| 254 | if(dir == 1 && (gsw_gpio_SetLevel(gpioNum, level) != 0))
|
| 255 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 256 | LOGE(GSW_GPIO, "[lynq_gpio_init]gpio_value_set fail.");
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 257 | return GSW_HAL_NORMAL_FAIL;
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 258 | }
|
| 259 | return GSW_HAL_SUCCESS;
|
| 260 | }
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 261 | int gsw_hwpin_is_usb_connected(void)
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 262 | {
|
| 263 | FILE *fp;
|
| 264 | char cmd[128] = {0};
|
| 265 | char tmp_rsp[20] = {0};
|
| 266 | //char *CONNECTED = "configured";
|
| 267 | char *DISCONNECTED = "not attached";
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 268 |
|
| 269 | sprintf(cmd,"cat /sys/devices/platform/soc/d4200000.axi/c0000000.usb/udc/c0000000.usb/state");
|
| 270 | fp=popen(cmd, "r");
|
| 271 | if (fgets(tmp_rsp,sizeof(tmp_rsp),fp) == NULL)
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 272 | LOGE(GSW_GPIO, "gsw_hwpin_is_usb_connected fail.");
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 273 | pclose(fp);
|
| 274 | if (strncmp(tmp_rsp,DISCONNECTED,strlen(DISCONNECTED)) == 0)
|
| 275 | return GSW_HAL_USB_DISCONNECTED;
|
| 276 | else
|
| 277 | return GSW_HAL_USB_CONNECTED;
|
| 278 | }
|
| 279 |
|
| 280 | int32_t gsw_setWakeupLevel(gsw_hal_pinLevel level)
|
| 281 | {
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 282 | if (gsw_gpio_SetLevel(wakeout, level) == GSW_HAL_NORMAL_FAIL)
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 283 | {
|
hong.liu | cd37079 | 2025-05-28 06:29:19 -0700 | [diff] [blame] | 284 | return GSW_HAL_NORMAL_FAIL;
|
b.liu | 68a94c9 | 2025-05-24 12:53:41 +0800 | [diff] [blame] | 285 | }
|
| 286 | return GSW_HAL_SUCCESS;
|
| 287 | }
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 288 |
|
| 289 | #define SYS_LED_DEV_TRIGGER_NODE "/sys/class/leds/red/trigger"
|
| 290 | #define SYS_LED_DEV_BRIGHTNES_NODE "/sys/class/leds/red/brightness"
|
| 291 | #define SYS_LED_DEV_DELAY_OFF_NODE "/sys/class/leds/red/delay_off"
|
| 292 | #define SYS_LED_DEV_DELAY_ON_NODE "/sys/class/leds/red/delay_on"
|
| 293 | #define EMMC_HEALTH_FILE "/sys/kernel/debug/mmc0/mmc0:0001/health"
|
| 294 | #define MDM_RESET_MCU 19
|
| 295 | #define MDM_BOOT_MCU_CTRL 31
|
| 296 | #define MDM_PWRMDM_EN 116
|
| 297 | #define MDM_WAKEUP_MCU 117
|
| 298 |
|
| 299 | int gsw_hwpin_mcu_reset(void)
|
| 300 | {
|
| 301 | if (0 != gsw_gpio_Init(MDM_RESET_MCU, GSW_HAL_PINDIR_OUT, GSW_HAL_LEVEL_LOW, GSW_HAL_PULL_DOWN))
|
| 302 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 303 | LOGE(GSW_GPIO, "init GPIO %d fail!", MDM_RESET_MCU);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 304 | return GSW_HAL_NORMAL_FAIL;
|
| 305 | }
|
| 306 |
|
| 307 | usleep(1000);
|
| 308 | if (0 != gsw_gpio_SetLevel(MDM_RESET_MCU, GSW_HAL_LEVEL_HIGH))
|
| 309 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 310 | LOGE(GSW_GPIO, "set GPIO %d : %d fail!", MDM_RESET_MCU, GSW_HAL_LEVEL_HIGH);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 311 | return GSW_HAL_NORMAL_FAIL;
|
| 312 | }
|
| 313 |
|
| 314 | usleep(100000);
|
| 315 | if (0 != gsw_gpio_SetLevel(MDM_RESET_MCU, GSW_HAL_LEVEL_LOW))
|
| 316 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 317 | LOGE(GSW_GPIO, "set GPIO %d : %d fail!", MDM_RESET_MCU, GSW_HAL_LEVEL_LOW);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 318 | return GSW_HAL_NORMAL_FAIL;
|
| 319 | }
|
| 320 |
|
| 321 | usleep(100000);
|
| 322 | return GSW_HAL_SUCCESS;
|
| 323 | }
|
| 324 |
|
| 325 | int gsw_hwpin_mcu_enter_bootmode(void)
|
| 326 | {
|
| 327 | if (0 != gsw_gpio_Init(MDM_BOOT_MCU_CTRL, GSW_HAL_PINDIR_OUT, GSW_HAL_LEVEL_LOW, GSW_HAL_PULL_DOWN))
|
| 328 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 329 | LOGE(GSW_GPIO, "init GPIO %d fail!", MDM_BOOT_MCU_CTRL);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 330 | return GSW_HAL_NORMAL_FAIL;
|
| 331 | }
|
| 332 |
|
| 333 | usleep(1000);
|
| 334 | if (0 != gsw_gpio_SetLevel(MDM_BOOT_MCU_CTRL, GSW_HAL_LEVEL_HIGH))
|
| 335 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 336 | LOGE(GSW_GPIO, "set GPIO %d : %d fail!", MDM_BOOT_MCU_CTRL, GSW_HAL_LEVEL_HIGH);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 337 | return GSW_HAL_NORMAL_FAIL;
|
| 338 | }
|
| 339 |
|
| 340 | usleep(100000);
|
| 341 | return gsw_hwpin_mcu_reset();
|
| 342 | }
|
| 343 |
|
| 344 | int gsw_hwpin_mcu_exit_bootmode(void)
|
| 345 | {
|
| 346 | if (0 != gsw_gpio_Init(MDM_BOOT_MCU_CTRL, GSW_HAL_PINDIR_OUT, GSW_HAL_LEVEL_LOW, GSW_HAL_PULL_DOWN))
|
| 347 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 348 | LOGE(GSW_GPIO, "init GPIO %d fail!", MDM_BOOT_MCU_CTRL);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 349 | return GSW_HAL_NORMAL_FAIL;
|
| 350 | }
|
| 351 |
|
| 352 | usleep(1000);
|
| 353 | if (0 != gsw_gpio_SetLevel(MDM_BOOT_MCU_CTRL, GSW_HAL_LEVEL_LOW))
|
| 354 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 355 | LOGE(GSW_GPIO, "set GPIO %d : %d fail!", MDM_BOOT_MCU_CTRL, GSW_HAL_LEVEL_LOW);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 356 | return GSW_HAL_NORMAL_FAIL;
|
| 357 | }
|
| 358 |
|
| 359 | usleep(100000);
|
| 360 | return gsw_hwpin_mcu_reset();
|
| 361 | }
|
| 362 |
|
| 363 | int gsw_hwpin_soc_power_keep(void)
|
| 364 | {
|
| 365 | return 0;
|
| 366 | }
|
| 367 |
|
| 368 | int gsw_hwpin_soc_power_release(void)
|
| 369 | {
|
| 370 | return 0;
|
| 371 | }
|
| 372 |
|
| 373 | static int _gsw_hwpin_soc_power_down(void)
|
| 374 | {
|
| 375 | if (0 != gsw_gpio_Init(MDM_PWRMDM_EN, GSW_HAL_PINDIR_OUT, GSW_HAL_LEVEL_LOW, GSW_HAL_PULL_DOWN))
|
| 376 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 377 | LOGE(GSW_GPIO, "init GPIO %d fail!", MDM_BOOT_MCU_CTRL);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 378 | return GSW_HAL_NORMAL_FAIL;
|
| 379 | }
|
| 380 |
|
| 381 | usleep(1000);
|
| 382 | if (0 != gsw_gpio_SetLevel(MDM_PWRMDM_EN, GSW_HAL_LEVEL_HIGH))
|
| 383 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 384 | LOGE(GSW_GPIO, "set GPIO %d : %d fail!", MDM_PWRMDM_EN, GSW_HAL_LEVEL_HIGH);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 385 | return GSW_HAL_NORMAL_FAIL;
|
| 386 | }
|
| 387 |
|
| 388 | return GSW_HAL_SUCCESS;
|
| 389 | }
|
| 390 |
|
| 391 | int gsw_hwpin_tcam_reset(void)
|
| 392 | {
|
| 393 | if (gsw_hwpin_mcu_reset() < 0)
|
| 394 | return GSW_HAL_NORMAL_FAIL;
|
| 395 | usleep(10000);
|
| 396 | return _gsw_hwpin_soc_power_down();
|
| 397 | }
|
| 398 |
|
| 399 | int gsw_hwpin_soc_wakeup_mcu(void)
|
| 400 | {
|
| 401 | if (0 != gsw_setWakeupLevel(GSW_HAL_LEVEL_HIGH))
|
| 402 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 403 | LOGE(GSW_GPIO, "wakeup mcu fail!");
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 404 | return GSW_HAL_NORMAL_FAIL;
|
| 405 | }
|
| 406 |
|
| 407 | return GSW_HAL_SUCCESS;
|
| 408 | }
|
| 409 |
|
| 410 | int gsw_hwpin_soc_wakeup_mcu_gpio_set_low(void)
|
| 411 | {
|
| 412 | if (0 != gsw_setWakeupLevel(GSW_HAL_LEVEL_LOW))
|
| 413 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 414 | LOGE(GSW_GPIO, "wakeup mcu set low fail!");
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 415 | return GSW_HAL_NORMAL_FAIL;
|
| 416 | }
|
| 417 |
|
| 418 | return GSW_HAL_SUCCESS;
|
| 419 | }
|
| 420 |
|
| 421 | static int _file_read(const char *file, char *value, unsigned int len)
|
| 422 | {
|
| 423 | int fd = 0;
|
| 424 | int ret = 0;
|
| 425 | fd = open(file, O_RDONLY);
|
| 426 | if(fd <= 0) {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 427 | LOGE(GSW_GPIO, "[hwpin] open file error %d,%s", fd, file);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 428 | return GSW_HAL_ERROR_FILE_OPEN;
|
| 429 | }
|
| 430 | if(value && len > 0) {
|
| 431 | ret = read(fd, value, len);
|
| 432 | }
|
| 433 | close(fd);
|
| 434 | if(ret < 0) {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 435 | LOGE(GSW_GPIO, "[hwpin] read file error %d,%s",ret, file);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 436 | }
|
| 437 | return ret;
|
| 438 | }
|
| 439 |
|
| 440 | static int _file_write(const char *file, char *value)
|
| 441 | {
|
| 442 | int fd = 0;
|
| 443 | int ret = 0;
|
| 444 | fd = open(file, O_WRONLY);
|
| 445 | if(fd <= 0) {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 446 | LOGE(GSW_GPIO, "[hwpin] open file error %d,%s", fd, file);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 447 | return GSW_HAL_ERROR_FILE_OPEN;
|
| 448 | }
|
| 449 | if(value) {
|
| 450 | ret = write(fd, value, strlen(value));
|
| 451 | }
|
| 452 | close(fd);
|
| 453 | if(ret < 0) {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 454 | LOGE(GSW_GPIO, "[hwpin] write file error %d,%s",ret, file);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 455 | }
|
| 456 | return ret;
|
| 457 | }
|
| 458 |
|
| 459 | int gsw_hwpin_led_red_on(void)
|
| 460 | {
|
| 461 | return GSW_HAL_SUCCESS;
|
| 462 | }
|
| 463 |
|
| 464 | int gsw_hwpin_led_red_off(void)
|
| 465 | {
|
| 466 | return GSW_HAL_SUCCESS;
|
| 467 | }
|
| 468 |
|
| 469 | int gsw_hwpin_led_red_blink(int delay_on, int delay_off)
|
| 470 | {
|
| 471 | return GSW_HAL_SUCCESS;
|
| 472 | }
|
| 473 |
|
| 474 | static bool g_aging_flag = false;
|
| 475 | int gsw_hwpin_led_green_on(void)
|
| 476 | {
|
| 477 | if (true == g_aging_flag)
|
| 478 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 479 | LOGI(GSW_GPIO, "green on LED file fail g_aging_flag = %d", g_aging_flag);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 480 | return GSW_HAL_SUCCESS;
|
| 481 | }
|
| 482 |
|
| 483 | if (access(SYS_LED_DEV_TRIGGER_NODE, W_OK) != 0)
|
| 484 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 485 | LOGE(GSW_GPIO, "green_on LED file fail %s", SYS_LED_DEV_TRIGGER_NODE);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 486 | return GSW_HAL_NORMAL_FAIL;
|
| 487 | }
|
| 488 |
|
| 489 | if (_file_write(SYS_LED_DEV_TRIGGER_NODE, "none") <= 0)
|
| 490 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 491 | LOGE(GSW_GPIO, "green_on LED write fail %s", SYS_LED_DEV_TRIGGER_NODE);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 492 | return GSW_HAL_NORMAL_FAIL;
|
| 493 | }
|
| 494 |
|
| 495 | if (access(SYS_LED_DEV_BRIGHTNES_NODE, W_OK) != 0)
|
| 496 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 497 | LOGE(GSW_GPIO, "green_on LED file fail %s", SYS_LED_DEV_BRIGHTNES_NODE);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 498 | return GSW_HAL_NORMAL_FAIL;
|
| 499 | }
|
| 500 |
|
| 501 | if (_file_write(SYS_LED_DEV_BRIGHTNES_NODE, "1") <= 0)
|
| 502 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 503 | LOGE(GSW_GPIO, "green_on LED write fail %s", SYS_LED_DEV_BRIGHTNES_NODE);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 504 | return GSW_HAL_NORMAL_FAIL;
|
| 505 | }
|
| 506 | return GSW_HAL_SUCCESS;
|
| 507 | }
|
| 508 |
|
| 509 | int gsw_hwpin_led_green_off(void)
|
| 510 | {
|
| 511 | if (true == g_aging_flag)
|
| 512 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 513 | LOGI(GSW_GPIO, "green off LED file fail g_aging_flag = %d", g_aging_flag);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 514 | return GSW_HAL_SUCCESS;
|
| 515 | }
|
| 516 |
|
| 517 | if (access(SYS_LED_DEV_TRIGGER_NODE, W_OK) != 0)
|
| 518 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 519 | LOGE(GSW_GPIO, "green_on LED file fail %s", SYS_LED_DEV_TRIGGER_NODE);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 520 | return GSW_HAL_NORMAL_FAIL;
|
| 521 | }
|
| 522 |
|
| 523 | if (_file_write(SYS_LED_DEV_TRIGGER_NODE, "none") <= 0)
|
| 524 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 525 | LOGE(GSW_GPIO, "green_on LED write fail %s", SYS_LED_DEV_TRIGGER_NODE);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 526 | return GSW_HAL_NORMAL_FAIL;
|
| 527 | }
|
| 528 |
|
| 529 | if (access(SYS_LED_DEV_BRIGHTNES_NODE, W_OK) != 0)
|
| 530 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 531 | LOGE(GSW_GPIO, "green_on LED file fail %s", SYS_LED_DEV_BRIGHTNES_NODE);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 532 | return GSW_HAL_NORMAL_FAIL;
|
| 533 | }
|
| 534 |
|
| 535 | if (_file_write(SYS_LED_DEV_BRIGHTNES_NODE, "0") <= 0)
|
| 536 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 537 | LOGE(GSW_GPIO, "green_on LED write fail %s", SYS_LED_DEV_BRIGHTNES_NODE);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 538 | return GSW_HAL_NORMAL_FAIL;
|
| 539 | }
|
| 540 |
|
| 541 | return GSW_HAL_SUCCESS;
|
| 542 | }
|
| 543 |
|
| 544 | int gsw_hwpin_led_green_blink(int delay_on, int delay_off)
|
| 545 | {
|
| 546 | char buf[32] = {0};
|
| 547 |
|
| 548 | if (true == g_aging_flag)
|
| 549 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 550 | LOGI(GSW_GPIO, "green blink LED file fail g_aging_flag = %d", g_aging_flag);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 551 | return GSW_HAL_SUCCESS;
|
| 552 | }
|
| 553 |
|
| 554 | if (access(SYS_LED_DEV_TRIGGER_NODE, W_OK) != 0)
|
| 555 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 556 | LOGE(GSW_GPIO, "green_on LED file fail %s", SYS_LED_DEV_TRIGGER_NODE);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 557 | return GSW_HAL_NORMAL_FAIL;
|
| 558 | }
|
| 559 |
|
| 560 | if (_file_write(SYS_LED_DEV_TRIGGER_NODE, "timer") <= 0)
|
| 561 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 562 | LOGE(GSW_GPIO, "green_on LED write fail %s", SYS_LED_DEV_TRIGGER_NODE);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 563 | return GSW_HAL_NORMAL_FAIL;
|
| 564 | }
|
| 565 |
|
| 566 | if (access(SYS_LED_DEV_DELAY_OFF_NODE, W_OK) != 0)
|
| 567 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 568 | LOGE(GSW_GPIO, "green_on LED file fail %s", SYS_LED_DEV_DELAY_OFF_NODE);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 569 | return GSW_HAL_NORMAL_FAIL;
|
| 570 | }
|
| 571 |
|
| 572 | sprintf(buf, "%d", delay_off);
|
| 573 | if (_file_write(SYS_LED_DEV_DELAY_OFF_NODE, buf) <= 0)
|
| 574 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 575 | LOGE(GSW_GPIO, "green_on LED write fail %s", SYS_LED_DEV_DELAY_OFF_NODE);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 576 | return GSW_HAL_NORMAL_FAIL;
|
| 577 | }
|
| 578 |
|
| 579 | if (access(SYS_LED_DEV_DELAY_ON_NODE, W_OK) != 0)
|
| 580 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 581 | LOGE(GSW_GPIO, "green_on LED file fail %s", SYS_LED_DEV_DELAY_ON_NODE);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 582 | return GSW_HAL_NORMAL_FAIL;
|
| 583 | }
|
| 584 |
|
| 585 | sprintf(buf, "%d", delay_on);
|
| 586 | if (_file_write(SYS_LED_DEV_DELAY_ON_NODE, buf) <= 0)
|
| 587 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 588 | LOGE(GSW_GPIO, "green_on LED write fail %s", SYS_LED_DEV_DELAY_ON_NODE);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 589 | return GSW_HAL_NORMAL_FAIL;
|
| 590 | }
|
| 591 |
|
| 592 | return GSW_HAL_SUCCESS;
|
| 593 | }
|
| 594 |
|
| 595 | int gsw_hwpin_get_emmc_health(int *written_mbytes, int *health)
|
| 596 | {
|
| 597 | char buffer[32] = {0};
|
| 598 | char *p = buffer;
|
| 599 | char *written_str = buffer;
|
| 600 | char *health_str = NULL;
|
| 601 |
|
| 602 | if (!written_mbytes && !health)
|
| 603 | return GSW_HAL_NORMAL_FAIL;
|
| 604 |
|
| 605 | if (_file_read(EMMC_HEALTH_FILE, buffer, sizeof(buffer)) <= 0)
|
| 606 | return -1;
|
| 607 |
|
| 608 | /* [0000017500-003], all 17500MB has be writtend, 003 is health value(smaller is better) */
|
| 609 | while (*p != '\0')
|
| 610 | {
|
| 611 | if ('-' == *p)
|
| 612 | {
|
| 613 | health_str = p + 1;
|
| 614 | *p = '\0';
|
| 615 | break;
|
| 616 | }
|
| 617 | p++;
|
| 618 | }
|
| 619 |
|
| 620 | if (written_mbytes)
|
| 621 | sscanf(written_str, "%10d", written_mbytes);
|
| 622 | if (health)
|
| 623 | sscanf(health_str, "%10d", health);
|
| 624 |
|
| 625 | return GSW_HAL_SUCCESS;
|
| 626 | }
|
| 627 |
|
| 628 | bool gsw_hwpin_check_emmc_readonly(void)
|
| 629 | {
|
| 630 | return false;//defualt return normal
|
| 631 | }
|
| 632 |
|
| 633 | int gsw_hwpin_factorytest_ctrl_led(bool aging_flag, int delay_on, int delay_off)
|
| 634 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 635 | LOGI(GSW_GPIO, "[hwpin] factory test ctrl led entry");
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 636 | if (false == g_aging_flag)
|
| 637 | {
|
| 638 | if (false == aging_flag)
|
| 639 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 640 | LOGI(GSW_GPIO, "[hwpin] factory test ctrl led aging_flag = %d", aging_flag);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 641 | return GSW_HAL_SUCCESS;
|
| 642 | }
|
| 643 | g_aging_flag = aging_flag;
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 644 | LOGI(GSW_GPIO, "g_aging_flag 1 value %d", g_aging_flag);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 645 | }
|
| 646 | else if (false == aging_flag)
|
| 647 | {
|
| 648 | g_aging_flag = aging_flag;
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 649 | LOGI(GSW_GPIO, "g_aging_flag 2 value %d", g_aging_flag);
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 650 | }
|
| 651 |
|
| 652 | gsw_hwpin_led_green_blink(delay_on, delay_off);
|
| 653 |
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 654 | LOGI(GSW_GPIO, "[hwpin] factory test ctrl led exit");
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 655 | return GSW_HAL_SUCCESS;
|
| 656 | }
|
| 657 |
|
| 658 | int gsw_hwpin_mute_test(int value)
|
| 659 | {
|
| 660 | return GSW_HAL_SUCCESS;
|
| 661 | }
|
| 662 |
|
| 663 | int gsw_hwpin_fstrim_emmc(const char *emmc_path)
|
| 664 | {
|
xk.li | c67ebf5 | 2025-07-19 02:40:18 -0700 | [diff] [blame] | 665 | char cmd[256] = {0};
|
| 666 | int ret = 0;
|
| 667 |
|
| 668 | if(!emmc_path || access(emmc_path, F_OK) != 0)
|
| 669 | {
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 670 | LOGE(GSW_GPIO, "Invalid path : %s", emmc_path);
|
xk.li | c67ebf5 | 2025-07-19 02:40:18 -0700 | [diff] [blame] | 671 | return ret;
|
| 672 | }
|
| 673 |
|
| 674 | sprintf(cmd, "fstrim -v %s", emmc_path);
|
| 675 | ret = system(cmd);
|
| 676 |
|
xk.li | 8a2e94a | 2025-07-21 19:47:08 -0700 | [diff] [blame] | 677 | LOGI(GSW_GPIO, "command %s, ret : %d", cmd, ret);
|
xk.li | c67ebf5 | 2025-07-19 02:40:18 -0700 | [diff] [blame] | 678 |
|
hong.liu | d241707 | 2025-06-27 07:10:37 -0700 | [diff] [blame] | 679 | return GSW_HAL_SUCCESS;
|
| 680 | } |