blob: 9837b975159fc218478e452aa9b38a83b56ad491 [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"
b.liu68a94c92025-05-24 12:53:41 +080011
12#ifndef LOG_ERR_LEVEL
13#define LOG_ERR_LEVEL 3 /* error conditions */
14#endif
15#ifndef LOG_WARN_LEVEL
16#define LOG_WARN_LEVEL 4 /* warning conditions */
17#endif
18#ifndef LOG_INFO_LEVEL
19#define LOG_INFO_LEVEL 6 /* informational */
20#endif
21#ifndef LOG_DEBUG_LEVEL
22#define LOG_DEBUG_LEVEL 7 /* debug-level messages */
23#endif
24#ifndef LOG_VERBOSE_LEVEL
25#define LOG_VERBOSE_LEVEL 8
26#endif
27
l.yang6a42e4d2025-05-28 01:04:20 -070028#define GSW_GPIO "[HAL][GSW_GPIO]"
29
b.liu68a94c92025-05-24 12:53:41 +080030#define LOGV(fmt, args ...) \
31 do{ \
32 char *file_ptr_1001 = __FILE__; \
33 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
34 char line_1001[10] = {0}; \
35 sprintf(line_1001, "%d", __LINE__); \
36 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
37 if(*ptr_1001 == '/') \
38 break; \
39 ptr_1001--; \
40 } \
l.yang6a42e4d2025-05-28 01:04:20 -070041 fun_ptr_log(LOG_VERBOSE_LEVEL, "%s#%s: "GSW_GPIO"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080042 } while(0)
43
44#define LOGI(fmt, args...) \
45 do{ \
46 char *file_ptr_1001 = __FILE__; \
47 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
48 char line_1001[10] = {0}; \
49 sprintf(line_1001, "%d", __LINE__); \
50 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
51 if(*ptr_1001 == '/') \
52 break; \
53 ptr_1001--; \
54 } \
l.yang6a42e4d2025-05-28 01:04:20 -070055 fun_ptr_log(LOG_INFO_LEVEL, "%s#%s: "GSW_GPIO"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080056 } while(0)
57
58#define LOGD(fmt, args...) \
59 do{ \
60 char *file_ptr_1001 = __FILE__; \
61 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
62 char line_1001[10] = {0}; \
63 sprintf(line_1001, "%d", __LINE__); \
64 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
65 if(*ptr_1001 == '/') \
66 break; \
67 ptr_1001--; \
68 } \
l.yang6a42e4d2025-05-28 01:04:20 -070069 fun_ptr_log(LOG_DEBUG_LEVEL, "%s#%s: "GSW_GPIO"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080070 } while(0)
71
72#define LOGW(fmt, args...) \
73 do{ \
74 char *file_ptr_1001 = __FILE__; \
75 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
76 char line_1001[10] = {0}; \
77 sprintf(line_1001, "%d", __LINE__); \
78 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
79 if(*ptr_1001 == '/') \
80 break; \
81 ptr_1001--; \
82 } \
l.yang6a42e4d2025-05-28 01:04:20 -070083 fun_ptr_log(LOG_WARN_LEVEL, "%s#%s: "GSW_GPIO"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080084 } while(0)
85
86#define LOGE(fmt, args...) \
87 do{ \
88 char *file_ptr_1001 = __FILE__; \
89 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
90 char line_1001[10] = {0}; \
91 sprintf(line_1001, "%d", __LINE__); \
92 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
93 if(*ptr_1001 == '/') \
94 break; \
95 ptr_1001--; \
96 } \
l.yang6a42e4d2025-05-28 01:04:20 -070097 fun_ptr_log(LOG_ERR_LEVEL, "%s#%s: "GSW_GPIO"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080098 } while(0)
99
100
b.liu68a94c92025-05-24 12:53:41 +0800101#define GSW_HAL_MEM_INVAILD -2 //表示入参地址为NULL
b.liu68a94c92025-05-24 12:53:41 +0800102#define wakeout 117
103
hong.liucd370792025-05-28 06:29:19 -0700104typedef enum{
105 GSW_HAL_PINDIR_IN,
106 GSW_HAL_PINDIR_OUT,
107}gsw_hal_pinDirection;
108
109typedef enum{
110 GSW_HAL_LEVEL_LOW,
111 GSW_HAL_LEVEL_HIGH,
112}gsw_hal_pinLevel;
113
114typedef enum{
115 GSW_HAL_PULL_NULL,
116 GSW_HAL_PULL_DOWN,
117 GSW_HAL_PULL_UP,
118}gsw_hal_pinPullSel;
119
120typedef enum{
121 GSW_HAL_USB_DISCONNECTED,
122 GSW_HAL_USB_CONNECTED,
123}gsw_hal_usbStatus;
b.liu68a94c92025-05-24 12:53:41 +0800124typedef void (*mbtk_log)(int level, const char *format,...);
125static mbtk_log fun_ptr_log = NULL;
126void *dlHandle_gpio = NULL;
127char *lynqLib_gpio = "/lib/libmbtk_lib.so";
128
129int gpio_check(int gpio)
130{
131 char buffer[128];
132 memset(buffer,0,128);
133 sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio);
134 return access(buffer , F_OK);
135}
136
137static int handle()
138{
139 if(dlHandle_gpio == NULL || fun_ptr_log == NULL)
140 {
141 dlHandle_gpio = dlopen(lynqLib_gpio, RTLD_NOW);
142 fun_ptr_log = (mbtk_log)dlsym(dlHandle_gpio, "mbtk_log");
143 if(fun_ptr_log == NULL || dlHandle_gpio == NULL)
144 {
hong.liucd370792025-05-28 06:29:19 -0700145 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800146 }
147 }
148 return GSW_HAL_SUCCESS;
149}
150
151static int gpio_export(int gpio)
152{
153 int file=-1;
154 int result =-1;
155 char pin_index_buffer[5]= {0};
156
157 if (gpio_check(gpio) == 0)
158 {
159 LOGE("export has been add\n");
160 return 0;
161 }
162
163 file = open("/sys/class/gpio/export",O_WRONLY);
164 if(file == -1)
165 {
166 LOGE("Open gpio export file fail.");
167 return -1;
168 }
169
170 memset(pin_index_buffer,0,5);
171 sprintf(pin_index_buffer,"%d", gpio);
172 result = write(file,pin_index_buffer,strlen(pin_index_buffer));
173 if(result < 0)
174 {
175 LOGE("Gpio[%d] export fail.", gpio);
176 close(file);
177 return -1;
178 }
179 close(file);
180
181 return 0;
182}
183
184static int gpio_unexport(int gpio)
185{
186 int file=-1;
187 int result =-1;
188 char pin_index_buffer[5]= {0};
189 if (gpio_check(gpio) == -1)
190 {
191 LOGE("export has been not add\n");
192 return -1;
193 }
194
195 file = open("/sys/class/gpio/unexport",O_WRONLY);
196 if(file == -1)
197 {
198 LOGE("Open gpio unexport file fail.");
199 return -1;
200 }
201
202 memset(pin_index_buffer,0,5);
203 sprintf(pin_index_buffer,"%d", gpio);
204 result=write(file,pin_index_buffer,strlen(pin_index_buffer));
205 if(result < 0)
206 {
207 close(file);
208 LOGE("Gpio[%d] unexport fail.", gpio);
209 return -1;
210 }
211 close(file);
212
213 return 0;
214}
215
216int32_t gsw_gpio_SetDirection(uint32_t gpioNum, gsw_hal_pinDirection dir)
217{
218 char buffer[128]= {0};
219 int file =-1;
220 int result =-1;
221 char direction[10] = {0};
222 memset(buffer,0,128);
223 if (handle())
hong.liucd370792025-05-28 06:29:19 -0700224 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800225
226 if (gpio_check(gpioNum) == -1)
227 {
228 LOGD("in dir export has been not add\n");
229 gpio_export(gpioNum);
230 }
231
232
233 sprintf(buffer,"/sys/class/gpio/gpio%u/direction", gpioNum);
234 file = open(buffer, O_WRONLY);
235 if(file == -1)
236 {
237 LOGE("Open gpio[%d] direct fail.", gpioNum);
238 return -1;
239 }
240 if (dir == GSW_HAL_PINDIR_IN)
241 sprintf(direction,"in");
242 else if (dir == GSW_HAL_PINDIR_OUT)
243 sprintf(direction,"out");
244
245 result = write(file,direction,strlen(direction));
246 if(result != strlen(direction))
247 {
248 LOGE("Set gpio[%d] direct fail.", gpioNum);
249 close(file);
250 gpio_unexport(gpioNum);
hong.liucd370792025-05-28 06:29:19 -0700251 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800252 }
253 close(file);
254 gpio_unexport(gpioNum);
255 return GSW_HAL_SUCCESS;
256}
257
258gsw_hal_pinLevel gsw_gpio_GetLevel(uint32_t gpioNum)
259{
260 char buffer[50];
261 char path[128];
262 int file =-1;
263 int result =-1;
264 memset(path,0,128);
265 memset(buffer,0,50);
266 if (handle())
hong.liucd370792025-05-28 06:29:19 -0700267 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800268
269 if (gpio_check(gpioNum) == -1)
270 {
271 LOGD("in get value export has been not add\n");
272 gpio_export(gpioNum);
273 }
274 sprintf(path,"/sys/class/gpio/gpio%u/value", gpioNum);
275 file = open(path,O_RDONLY);
276 if(file == -1)
277 {
278 LOGE("Open gpio[%d] fail.", gpioNum);
279 return -1;
280 }
281 result = read(file,buffer,50);
282 if(result <= 0)
283 {
284 LOGE("Get gpio[%d] value fail", gpioNum);
285 close(file);
286 gpio_unexport(gpioNum);
hong.liucd370792025-05-28 06:29:19 -0700287 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800288 }
289 close(file);
290 gpio_unexport(gpioNum);
291 return atoi(buffer);
292}
293
294int32_t gsw_gpio_SetLevel(uint32_t gpioNum, gsw_hal_pinLevel level)
295{
296 char buffer[128]= {0};
297 int file =-1;
298 int result =-1;
299 memset(buffer,0,50);
300 if (handle())
hong.liucd370792025-05-28 06:29:19 -0700301 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800302
303 if (gpio_check(gpioNum) == -1)
304 {
305 LOGD("in set value export has been not add\n");
306 gpio_export(gpioNum);
307 }
308
309 sprintf(buffer,"/sys/class/gpio/gpio%u/value", gpioNum);
310 file = open(buffer,O_WRONLY);
311 if(file == -1)
312 {
313 LOGE("Open gpio[%d] value fail.", gpioNum);
314 return -1;
315 }
316 if(level == GSW_HAL_LEVEL_LOW) {
317 result = write(file,"0",1);
318 } else {
319 result = write(file,"1",1);
320 }
321 if(result != 1)
322 {
323 LOGE("Set gpio[%d] value fail err =%d.", gpioNum, errno);
324 close(file);
325 gpio_unexport(gpioNum);
hong.liucd370792025-05-28 06:29:19 -0700326 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800327 }
328 close(file);
329 gpio_unexport(gpioNum);
330 return GSW_HAL_SUCCESS;
331}
332
333
334int32_t gsw_gpio_Init(uint32_t gpioNum, gsw_hal_pinDirection dir, gsw_hal_pinLevel level, gsw_hal_pinPullSel pullsel)
335{
336 if (handle())
hong.liucd370792025-05-28 06:29:19 -0700337 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800338
339 if (dir != 1 && dir != 0)
340 {
341 LOGE("[lynq_gpio_init] direction fail.");
342 return -1;
343 }
344
345 if (level != 1 && level != 0)
346 {
347 LOGE("[lynq_gpio_init] value fail.");
hong.liucd370792025-05-28 06:29:19 -0700348 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800349 }
350
351 if(gpio_export(gpioNum))
352 {
353 LOGE("[lynq_gpio_init]gpio_export fail.");
hong.liucd370792025-05-28 06:29:19 -0700354 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800355 }
356
357 if(gsw_gpio_SetDirection(gpioNum, dir))
358 {
359 LOGE("[lynq_gpio_init]gpio_direct_set fail.");
hong.liucd370792025-05-28 06:29:19 -0700360 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800361 }
362
363 if(dir == 1 && (gsw_gpio_SetLevel(gpioNum, level) != 0))
364 {
365 LOGE("[lynq_gpio_init]gpio_value_set fail.");
hong.liucd370792025-05-28 06:29:19 -0700366 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800367 }
368 return GSW_HAL_SUCCESS;
369}
hong.liucd370792025-05-28 06:29:19 -0700370int gsw_hwpin_is_usb_connected(void)
b.liu68a94c92025-05-24 12:53:41 +0800371{
372 FILE *fp;
373 char cmd[128] = {0};
374 char tmp_rsp[20] = {0};
375 //char *CONNECTED = "configured";
376 char *DISCONNECTED = "not attached";
377 if (handle())
hong.liucd370792025-05-28 06:29:19 -0700378 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800379
380 sprintf(cmd,"cat /sys/devices/platform/soc/d4200000.axi/c0000000.usb/udc/c0000000.usb/state");
381 fp=popen(cmd, "r");
382 if (fgets(tmp_rsp,sizeof(tmp_rsp),fp) == NULL)
383 LOGE("gsw_hwpin_is_usb_connected fail.");
384 pclose(fp);
385 if (strncmp(tmp_rsp,DISCONNECTED,strlen(DISCONNECTED)) == 0)
386 return GSW_HAL_USB_DISCONNECTED;
387 else
388 return GSW_HAL_USB_CONNECTED;
389}
390
391int32_t gsw_setWakeupLevel(gsw_hal_pinLevel level)
392{
hong.liucd370792025-05-28 06:29:19 -0700393 if (gsw_gpio_SetLevel(wakeout, level) == GSW_HAL_NORMAL_FAIL)
b.liu68a94c92025-05-24 12:53:41 +0800394 {
hong.liucd370792025-05-28 06:29:19 -0700395 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800396 }
397 return GSW_HAL_SUCCESS;
398}