blob: 8b55b6c5a1f297aabdf940bfa48bbc36a1724dac [file] [log] [blame]
b.liu68a94c92025-05-24 12:53:41 +08001#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.liucd370792025-05-28 06:29:19 -070010#include "gsw_hwpin_interface.h"
hong.liud2417072025-06-27 07:10:37 -070011#include "gsw_hal_errcode.h"
xk.li8a2e94a2025-07-21 19:47:08 -070012#include "gsw_log_interface.h"
b.liu68a94c92025-05-24 12:53:41 +080013
b.liu68a94c92025-05-24 12:53:41 +080014
l.yang6a42e4d2025-05-28 01:04:20 -070015#define GSW_GPIO "[HAL][GSW_GPIO]"
16
b.liu68a94c92025-05-24 12:53:41 +080017
b.liu68a94c92025-05-24 12:53:41 +080018#define wakeout 117
19
hong.liucd370792025-05-28 06:29:19 -070020typedef enum{
21 GSW_HAL_PINDIR_IN,
22 GSW_HAL_PINDIR_OUT,
23}gsw_hal_pinDirection;
24
25typedef enum{
26 GSW_HAL_LEVEL_LOW,
27 GSW_HAL_LEVEL_HIGH,
28}gsw_hal_pinLevel;
29
30typedef enum{
31 GSW_HAL_PULL_NULL,
32 GSW_HAL_PULL_DOWN,
33 GSW_HAL_PULL_UP,
34}gsw_hal_pinPullSel;
35
36typedef enum{
37 GSW_HAL_USB_DISCONNECTED,
38 GSW_HAL_USB_CONNECTED,
39}gsw_hal_usbStatus;
xk.li8a2e94a2025-07-21 19:47:08 -070040//typedef void (*mbtk_log)(int level, const char *format,...);
41//static mbtk_log fun_ptr_log = NULL;
42
b.liu68a94c92025-05-24 12:53:41 +080043
44int 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.liu68a94c92025-05-24 12:53:41 +080052
53static 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.li8a2e94a2025-07-21 19:47:08 -070061 LOGE(GSW_GPIO, "export has been add\n");
b.liu68a94c92025-05-24 12:53:41 +080062 return 0;
63 }
64
65 file = open("/sys/class/gpio/export",O_WRONLY);
66 if(file == -1)
67 {
xk.li8a2e94a2025-07-21 19:47:08 -070068 LOGE(GSW_GPIO, "Open gpio export file fail.");
b.liu68a94c92025-05-24 12:53:41 +080069 return -1;
70 }
71
b.liu68a94c92025-05-24 12:53:41 +080072 sprintf(pin_index_buffer,"%d", gpio);
73 result = write(file,pin_index_buffer,strlen(pin_index_buffer));
74 if(result < 0)
75 {
xk.li8a2e94a2025-07-21 19:47:08 -070076 LOGE(GSW_GPIO, "Gpio[%d] export fail.", gpio);
b.liu68a94c92025-05-24 12:53:41 +080077 close(file);
78 return -1;
79 }
80 close(file);
81
82 return 0;
83}
84
85static 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.li8a2e94a2025-07-21 19:47:08 -070092 LOGE(GSW_GPIO, "export has been not add\n");
b.liu68a94c92025-05-24 12:53:41 +080093 return -1;
94 }
95
96 file = open("/sys/class/gpio/unexport",O_WRONLY);
97 if(file == -1)
98 {
xk.li8a2e94a2025-07-21 19:47:08 -070099 LOGE(GSW_GPIO, "Open gpio unexport file fail.");
b.liu68a94c92025-05-24 12:53:41 +0800100 return -1;
101 }
102
b.liu68a94c92025-05-24 12:53:41 +0800103 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.li8a2e94a2025-07-21 19:47:08 -0700108 LOGE(GSW_GPIO, "Gpio[%d] unexport fail.", gpio);
b.liu68a94c92025-05-24 12:53:41 +0800109 return -1;
110 }
111 close(file);
112
113 return 0;
114}
115
116int32_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.li8a2e94a2025-07-21 19:47:08 -0700123
b.liu68a94c92025-05-24 12:53:41 +0800124 if (gpio_check(gpioNum) == -1)
125 {
xk.li8a2e94a2025-07-21 19:47:08 -0700126 LOGD(GSW_GPIO, "in dir export has been not add\n");
b.liu68a94c92025-05-24 12:53:41 +0800127 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.li8a2e94a2025-07-21 19:47:08 -0700135 LOGE(GSW_GPIO, "Open gpio[%d] direct fail.", gpioNum);
b.liu68a94c92025-05-24 12:53:41 +0800136 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.li8a2e94a2025-07-21 19:47:08 -0700146 LOGE(GSW_GPIO, "Set gpio[%d] direct fail.", gpioNum);
b.liu68a94c92025-05-24 12:53:41 +0800147 close(file);
148 gpio_unexport(gpioNum);
hong.liucd370792025-05-28 06:29:19 -0700149 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800150 }
151 close(file);
152 gpio_unexport(gpioNum);
153 return GSW_HAL_SUCCESS;
154}
155
156gsw_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.liud2417072025-06-27 07:10:37 -0700162 memset(path,0,sizeof(path));
163 memset(buffer,0,sizeof(buffer));
b.liu68a94c92025-05-24 12:53:41 +0800164
165 if (gpio_check(gpioNum) == -1)
166 {
xk.li8a2e94a2025-07-21 19:47:08 -0700167 LOGD(GSW_GPIO, "in get value export has been not add\n");
b.liu68a94c92025-05-24 12:53:41 +0800168 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.li8a2e94a2025-07-21 19:47:08 -0700174 LOGE(GSW_GPIO, "Open gpio[%d] fail.", gpioNum);
b.liu68a94c92025-05-24 12:53:41 +0800175 return -1;
176 }
177 result = read(file,buffer,50);
178 if(result <= 0)
179 {
xk.li8a2e94a2025-07-21 19:47:08 -0700180 LOGE(GSW_GPIO, "Get gpio[%d] value fail", gpioNum);
b.liu68a94c92025-05-24 12:53:41 +0800181 close(file);
182 gpio_unexport(gpioNum);
hong.liucd370792025-05-28 06:29:19 -0700183 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800184 }
185 close(file);
186 gpio_unexport(gpioNum);
187 return atoi(buffer);
188}
189
190int32_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.liu68a94c92025-05-24 12:53:41 +0800196
197 if (gpio_check(gpioNum) == -1)
198 {
xk.li8a2e94a2025-07-21 19:47:08 -0700199 LOGD(GSW_GPIO, "in set value export has been not add\n");
b.liu68a94c92025-05-24 12:53:41 +0800200 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.li8a2e94a2025-07-21 19:47:08 -0700207 LOGE(GSW_GPIO, "Open gpio[%d] value fail.", gpioNum);
b.liu68a94c92025-05-24 12:53:41 +0800208 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.li8a2e94a2025-07-21 19:47:08 -0700217 LOGE(GSW_GPIO, "Set gpio[%d] value fail err =%d.", gpioNum, errno);
b.liu68a94c92025-05-24 12:53:41 +0800218 close(file);
219 gpio_unexport(gpioNum);
hong.liucd370792025-05-28 06:29:19 -0700220 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800221 }
222 close(file);
223 gpio_unexport(gpioNum);
224 return GSW_HAL_SUCCESS;
225}
226
227
228int32_t gsw_gpio_Init(uint32_t gpioNum, gsw_hal_pinDirection dir, gsw_hal_pinLevel level, gsw_hal_pinPullSel pullsel)
229{
b.liu68a94c92025-05-24 12:53:41 +0800230 if (dir != 1 && dir != 0)
231 {
xk.li8a2e94a2025-07-21 19:47:08 -0700232 LOGE(GSW_GPIO, "[lynq_gpio_init] direction fail.");
b.liu68a94c92025-05-24 12:53:41 +0800233 return -1;
234 }
235
236 if (level != 1 && level != 0)
237 {
xk.li8a2e94a2025-07-21 19:47:08 -0700238 LOGE(GSW_GPIO, "[lynq_gpio_init] value fail.");
hong.liucd370792025-05-28 06:29:19 -0700239 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800240 }
241
242 if(gpio_export(gpioNum))
243 {
xk.li8a2e94a2025-07-21 19:47:08 -0700244 LOGE(GSW_GPIO, "[lynq_gpio_init]gpio_export fail.");
hong.liucd370792025-05-28 06:29:19 -0700245 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800246 }
247
248 if(gsw_gpio_SetDirection(gpioNum, dir))
249 {
xk.li8a2e94a2025-07-21 19:47:08 -0700250 LOGE(GSW_GPIO, "[lynq_gpio_init]gpio_direct_set fail.");
hong.liucd370792025-05-28 06:29:19 -0700251 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800252 }
253
254 if(dir == 1 && (gsw_gpio_SetLevel(gpioNum, level) != 0))
255 {
xk.li8a2e94a2025-07-21 19:47:08 -0700256 LOGE(GSW_GPIO, "[lynq_gpio_init]gpio_value_set fail.");
hong.liucd370792025-05-28 06:29:19 -0700257 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800258 }
259 return GSW_HAL_SUCCESS;
260}
hong.liucd370792025-05-28 06:29:19 -0700261int gsw_hwpin_is_usb_connected(void)
b.liu68a94c92025-05-24 12:53:41 +0800262{
263 FILE *fp;
264 char cmd[128] = {0};
265 char tmp_rsp[20] = {0};
266 //char *CONNECTED = "configured";
267 char *DISCONNECTED = "not attached";
b.liu68a94c92025-05-24 12:53:41 +0800268
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.li8a2e94a2025-07-21 19:47:08 -0700272 LOGE(GSW_GPIO, "gsw_hwpin_is_usb_connected fail.");
b.liu68a94c92025-05-24 12:53:41 +0800273 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
280int32_t gsw_setWakeupLevel(gsw_hal_pinLevel level)
281{
hong.liucd370792025-05-28 06:29:19 -0700282 if (gsw_gpio_SetLevel(wakeout, level) == GSW_HAL_NORMAL_FAIL)
b.liu68a94c92025-05-24 12:53:41 +0800283 {
hong.liucd370792025-05-28 06:29:19 -0700284 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800285 }
286 return GSW_HAL_SUCCESS;
287}
hong.liud2417072025-06-27 07:10:37 -0700288
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
299int 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.li8a2e94a2025-07-21 19:47:08 -0700303 LOGE(GSW_GPIO, "init GPIO %d fail!", MDM_RESET_MCU);
hong.liud2417072025-06-27 07:10:37 -0700304 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.li8a2e94a2025-07-21 19:47:08 -0700310 LOGE(GSW_GPIO, "set GPIO %d : %d fail!", MDM_RESET_MCU, GSW_HAL_LEVEL_HIGH);
hong.liud2417072025-06-27 07:10:37 -0700311 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.li8a2e94a2025-07-21 19:47:08 -0700317 LOGE(GSW_GPIO, "set GPIO %d : %d fail!", MDM_RESET_MCU, GSW_HAL_LEVEL_LOW);
hong.liud2417072025-06-27 07:10:37 -0700318 return GSW_HAL_NORMAL_FAIL;
319 }
320
321 usleep(100000);
322 return GSW_HAL_SUCCESS;
323}
324
325int 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.li8a2e94a2025-07-21 19:47:08 -0700329 LOGE(GSW_GPIO, "init GPIO %d fail!", MDM_BOOT_MCU_CTRL);
hong.liud2417072025-06-27 07:10:37 -0700330 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.li8a2e94a2025-07-21 19:47:08 -0700336 LOGE(GSW_GPIO, "set GPIO %d : %d fail!", MDM_BOOT_MCU_CTRL, GSW_HAL_LEVEL_HIGH);
hong.liud2417072025-06-27 07:10:37 -0700337 return GSW_HAL_NORMAL_FAIL;
338 }
339
340 usleep(100000);
341 return gsw_hwpin_mcu_reset();
342}
343
344int 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.li8a2e94a2025-07-21 19:47:08 -0700348 LOGE(GSW_GPIO, "init GPIO %d fail!", MDM_BOOT_MCU_CTRL);
hong.liud2417072025-06-27 07:10:37 -0700349 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.li8a2e94a2025-07-21 19:47:08 -0700355 LOGE(GSW_GPIO, "set GPIO %d : %d fail!", MDM_BOOT_MCU_CTRL, GSW_HAL_LEVEL_LOW);
hong.liud2417072025-06-27 07:10:37 -0700356 return GSW_HAL_NORMAL_FAIL;
357 }
358
359 usleep(100000);
360 return gsw_hwpin_mcu_reset();
361}
362
363int gsw_hwpin_soc_power_keep(void)
364{
365 return 0;
366}
367
368int gsw_hwpin_soc_power_release(void)
369{
370 return 0;
371}
372
373static 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.li8a2e94a2025-07-21 19:47:08 -0700377 LOGE(GSW_GPIO, "init GPIO %d fail!", MDM_BOOT_MCU_CTRL);
hong.liud2417072025-06-27 07:10:37 -0700378 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.li8a2e94a2025-07-21 19:47:08 -0700384 LOGE(GSW_GPIO, "set GPIO %d : %d fail!", MDM_PWRMDM_EN, GSW_HAL_LEVEL_HIGH);
hong.liud2417072025-06-27 07:10:37 -0700385 return GSW_HAL_NORMAL_FAIL;
386 }
387
388 return GSW_HAL_SUCCESS;
389}
390
391int 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
399int gsw_hwpin_soc_wakeup_mcu(void)
400{
401 if (0 != gsw_setWakeupLevel(GSW_HAL_LEVEL_HIGH))
402 {
xk.li8a2e94a2025-07-21 19:47:08 -0700403 LOGE(GSW_GPIO, "wakeup mcu fail!");
hong.liud2417072025-06-27 07:10:37 -0700404 return GSW_HAL_NORMAL_FAIL;
405 }
406
407 return GSW_HAL_SUCCESS;
408}
409
410int gsw_hwpin_soc_wakeup_mcu_gpio_set_low(void)
411{
412 if (0 != gsw_setWakeupLevel(GSW_HAL_LEVEL_LOW))
413 {
xk.li8a2e94a2025-07-21 19:47:08 -0700414 LOGE(GSW_GPIO, "wakeup mcu set low fail!");
hong.liud2417072025-06-27 07:10:37 -0700415 return GSW_HAL_NORMAL_FAIL;
416 }
417
418 return GSW_HAL_SUCCESS;
419}
420
421static 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.li8a2e94a2025-07-21 19:47:08 -0700427 LOGE(GSW_GPIO, "[hwpin] open file error %d,%s", fd, file);
hong.liud2417072025-06-27 07:10:37 -0700428 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.li8a2e94a2025-07-21 19:47:08 -0700435 LOGE(GSW_GPIO, "[hwpin] read file error %d,%s",ret, file);
hong.liud2417072025-06-27 07:10:37 -0700436 }
437 return ret;
438}
439
440static 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.li8a2e94a2025-07-21 19:47:08 -0700446 LOGE(GSW_GPIO, "[hwpin] open file error %d,%s", fd, file);
hong.liud2417072025-06-27 07:10:37 -0700447 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.li8a2e94a2025-07-21 19:47:08 -0700454 LOGE(GSW_GPIO, "[hwpin] write file error %d,%s",ret, file);
hong.liud2417072025-06-27 07:10:37 -0700455 }
456 return ret;
457}
458
459int gsw_hwpin_led_red_on(void)
460{
461 return GSW_HAL_SUCCESS;
462}
463
464int gsw_hwpin_led_red_off(void)
465{
466 return GSW_HAL_SUCCESS;
467}
468
469int gsw_hwpin_led_red_blink(int delay_on, int delay_off)
470{
471 return GSW_HAL_SUCCESS;
472}
473
474static bool g_aging_flag = false;
475int gsw_hwpin_led_green_on(void)
476{
477 if (true == g_aging_flag)
478 {
xk.li8a2e94a2025-07-21 19:47:08 -0700479 LOGI(GSW_GPIO, "green on LED file fail g_aging_flag = %d", g_aging_flag);
hong.liud2417072025-06-27 07:10:37 -0700480 return GSW_HAL_SUCCESS;
481 }
482
483 if (access(SYS_LED_DEV_TRIGGER_NODE, W_OK) != 0)
484 {
xk.li8a2e94a2025-07-21 19:47:08 -0700485 LOGE(GSW_GPIO, "green_on LED file fail %s", SYS_LED_DEV_TRIGGER_NODE);
hong.liud2417072025-06-27 07:10:37 -0700486 return GSW_HAL_NORMAL_FAIL;
487 }
488
489 if (_file_write(SYS_LED_DEV_TRIGGER_NODE, "none") <= 0)
490 {
xk.li8a2e94a2025-07-21 19:47:08 -0700491 LOGE(GSW_GPIO, "green_on LED write fail %s", SYS_LED_DEV_TRIGGER_NODE);
hong.liud2417072025-06-27 07:10:37 -0700492 return GSW_HAL_NORMAL_FAIL;
493 }
494
495 if (access(SYS_LED_DEV_BRIGHTNES_NODE, W_OK) != 0)
496 {
xk.li8a2e94a2025-07-21 19:47:08 -0700497 LOGE(GSW_GPIO, "green_on LED file fail %s", SYS_LED_DEV_BRIGHTNES_NODE);
hong.liud2417072025-06-27 07:10:37 -0700498 return GSW_HAL_NORMAL_FAIL;
499 }
500
501 if (_file_write(SYS_LED_DEV_BRIGHTNES_NODE, "1") <= 0)
502 {
xk.li8a2e94a2025-07-21 19:47:08 -0700503 LOGE(GSW_GPIO, "green_on LED write fail %s", SYS_LED_DEV_BRIGHTNES_NODE);
hong.liud2417072025-06-27 07:10:37 -0700504 return GSW_HAL_NORMAL_FAIL;
505 }
506 return GSW_HAL_SUCCESS;
507}
508
509int gsw_hwpin_led_green_off(void)
510{
511 if (true == g_aging_flag)
512 {
xk.li8a2e94a2025-07-21 19:47:08 -0700513 LOGI(GSW_GPIO, "green off LED file fail g_aging_flag = %d", g_aging_flag);
hong.liud2417072025-06-27 07:10:37 -0700514 return GSW_HAL_SUCCESS;
515 }
516
517 if (access(SYS_LED_DEV_TRIGGER_NODE, W_OK) != 0)
518 {
xk.li8a2e94a2025-07-21 19:47:08 -0700519 LOGE(GSW_GPIO, "green_on LED file fail %s", SYS_LED_DEV_TRIGGER_NODE);
hong.liud2417072025-06-27 07:10:37 -0700520 return GSW_HAL_NORMAL_FAIL;
521 }
522
523 if (_file_write(SYS_LED_DEV_TRIGGER_NODE, "none") <= 0)
524 {
xk.li8a2e94a2025-07-21 19:47:08 -0700525 LOGE(GSW_GPIO, "green_on LED write fail %s", SYS_LED_DEV_TRIGGER_NODE);
hong.liud2417072025-06-27 07:10:37 -0700526 return GSW_HAL_NORMAL_FAIL;
527 }
528
529 if (access(SYS_LED_DEV_BRIGHTNES_NODE, W_OK) != 0)
530 {
xk.li8a2e94a2025-07-21 19:47:08 -0700531 LOGE(GSW_GPIO, "green_on LED file fail %s", SYS_LED_DEV_BRIGHTNES_NODE);
hong.liud2417072025-06-27 07:10:37 -0700532 return GSW_HAL_NORMAL_FAIL;
533 }
534
535 if (_file_write(SYS_LED_DEV_BRIGHTNES_NODE, "0") <= 0)
536 {
xk.li8a2e94a2025-07-21 19:47:08 -0700537 LOGE(GSW_GPIO, "green_on LED write fail %s", SYS_LED_DEV_BRIGHTNES_NODE);
hong.liud2417072025-06-27 07:10:37 -0700538 return GSW_HAL_NORMAL_FAIL;
539 }
540
541 return GSW_HAL_SUCCESS;
542}
543
544int 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.li8a2e94a2025-07-21 19:47:08 -0700550 LOGI(GSW_GPIO, "green blink LED file fail g_aging_flag = %d", g_aging_flag);
hong.liud2417072025-06-27 07:10:37 -0700551 return GSW_HAL_SUCCESS;
552 }
553
554 if (access(SYS_LED_DEV_TRIGGER_NODE, W_OK) != 0)
555 {
xk.li8a2e94a2025-07-21 19:47:08 -0700556 LOGE(GSW_GPIO, "green_on LED file fail %s", SYS_LED_DEV_TRIGGER_NODE);
hong.liud2417072025-06-27 07:10:37 -0700557 return GSW_HAL_NORMAL_FAIL;
558 }
559
560 if (_file_write(SYS_LED_DEV_TRIGGER_NODE, "timer") <= 0)
561 {
xk.li8a2e94a2025-07-21 19:47:08 -0700562 LOGE(GSW_GPIO, "green_on LED write fail %s", SYS_LED_DEV_TRIGGER_NODE);
hong.liud2417072025-06-27 07:10:37 -0700563 return GSW_HAL_NORMAL_FAIL;
564 }
565
566 if (access(SYS_LED_DEV_DELAY_OFF_NODE, W_OK) != 0)
567 {
xk.li8a2e94a2025-07-21 19:47:08 -0700568 LOGE(GSW_GPIO, "green_on LED file fail %s", SYS_LED_DEV_DELAY_OFF_NODE);
hong.liud2417072025-06-27 07:10:37 -0700569 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.li8a2e94a2025-07-21 19:47:08 -0700575 LOGE(GSW_GPIO, "green_on LED write fail %s", SYS_LED_DEV_DELAY_OFF_NODE);
hong.liud2417072025-06-27 07:10:37 -0700576 return GSW_HAL_NORMAL_FAIL;
577 }
578
579 if (access(SYS_LED_DEV_DELAY_ON_NODE, W_OK) != 0)
580 {
xk.li8a2e94a2025-07-21 19:47:08 -0700581 LOGE(GSW_GPIO, "green_on LED file fail %s", SYS_LED_DEV_DELAY_ON_NODE);
hong.liud2417072025-06-27 07:10:37 -0700582 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.li8a2e94a2025-07-21 19:47:08 -0700588 LOGE(GSW_GPIO, "green_on LED write fail %s", SYS_LED_DEV_DELAY_ON_NODE);
hong.liud2417072025-06-27 07:10:37 -0700589 return GSW_HAL_NORMAL_FAIL;
590 }
591
592 return GSW_HAL_SUCCESS;
593}
594
595int 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
628bool gsw_hwpin_check_emmc_readonly(void)
629{
630 return false;//defualt return normal
631}
632
633int gsw_hwpin_factorytest_ctrl_led(bool aging_flag, int delay_on, int delay_off)
634{
xk.li8a2e94a2025-07-21 19:47:08 -0700635 LOGI(GSW_GPIO, "[hwpin] factory test ctrl led entry");
hong.liud2417072025-06-27 07:10:37 -0700636 if (false == g_aging_flag)
637 {
638 if (false == aging_flag)
639 {
xk.li8a2e94a2025-07-21 19:47:08 -0700640 LOGI(GSW_GPIO, "[hwpin] factory test ctrl led aging_flag = %d", aging_flag);
hong.liud2417072025-06-27 07:10:37 -0700641 return GSW_HAL_SUCCESS;
642 }
643 g_aging_flag = aging_flag;
xk.li8a2e94a2025-07-21 19:47:08 -0700644 LOGI(GSW_GPIO, "g_aging_flag 1 value %d", g_aging_flag);
hong.liud2417072025-06-27 07:10:37 -0700645 }
646 else if (false == aging_flag)
647 {
648 g_aging_flag = aging_flag;
xk.li8a2e94a2025-07-21 19:47:08 -0700649 LOGI(GSW_GPIO, "g_aging_flag 2 value %d", g_aging_flag);
hong.liud2417072025-06-27 07:10:37 -0700650 }
651
652 gsw_hwpin_led_green_blink(delay_on, delay_off);
653
xk.li8a2e94a2025-07-21 19:47:08 -0700654 LOGI(GSW_GPIO, "[hwpin] factory test ctrl led exit");
hong.liud2417072025-06-27 07:10:37 -0700655 return GSW_HAL_SUCCESS;
656}
657
658int gsw_hwpin_mute_test(int value)
659{
660 return GSW_HAL_SUCCESS;
661}
662
663int gsw_hwpin_fstrim_emmc(const char *emmc_path)
664{
xk.lic67ebf52025-07-19 02:40:18 -0700665 char cmd[256] = {0};
666 int ret = 0;
667
668 if(!emmc_path || access(emmc_path, F_OK) != 0)
669 {
xk.li8a2e94a2025-07-21 19:47:08 -0700670 LOGE(GSW_GPIO, "Invalid path : %s", emmc_path);
xk.lic67ebf52025-07-19 02:40:18 -0700671 return ret;
672 }
673
674 sprintf(cmd, "fstrim -v %s", emmc_path);
675 ret = system(cmd);
676
xk.li8a2e94a2025-07-21 19:47:08 -0700677 LOGI(GSW_GPIO, "command %s, ret : %d", cmd, ret);
xk.lic67ebf52025-07-19 02:40:18 -0700678
hong.liud2417072025-06-27 07:10:37 -0700679 return GSW_HAL_SUCCESS;
680}