blob: 231ddd3789fc9e089cc8a2d303ba6a69c61ad52a [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
10#include "gsw_gpio.h"
11
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
101#define GSW_HAL_SUCCESS 0
102#define GSW_HAL_FAIL -1 //表示失败(通用性)
103#define GSW_HAL_MEM_INVAILD -2 //表示入参地址为NULL
104
105#define wakeout 117
106
107typedef void (*mbtk_log)(int level, const char *format,...);
108static mbtk_log fun_ptr_log = NULL;
109void *dlHandle_gpio = NULL;
110char *lynqLib_gpio = "/lib/libmbtk_lib.so";
111
112int gpio_check(int gpio)
113{
114 char buffer[128];
115 memset(buffer,0,128);
116 sprintf(buffer,"/sys/class/gpio/gpio%d/direction", gpio);
117 return access(buffer , F_OK);
118}
119
120static int handle()
121{
122 if(dlHandle_gpio == NULL || fun_ptr_log == NULL)
123 {
124 dlHandle_gpio = dlopen(lynqLib_gpio, RTLD_NOW);
125 fun_ptr_log = (mbtk_log)dlsym(dlHandle_gpio, "mbtk_log");
126 if(fun_ptr_log == NULL || dlHandle_gpio == NULL)
127 {
128 return GSW_HAL_FAIL;
129 }
130 }
131 return GSW_HAL_SUCCESS;
132}
133
134static int gpio_export(int gpio)
135{
136 int file=-1;
137 int result =-1;
138 char pin_index_buffer[5]= {0};
139
140 if (gpio_check(gpio) == 0)
141 {
142 LOGE("export has been add\n");
143 return 0;
144 }
145
146 file = open("/sys/class/gpio/export",O_WRONLY);
147 if(file == -1)
148 {
149 LOGE("Open gpio export file fail.");
150 return -1;
151 }
152
153 memset(pin_index_buffer,0,5);
154 sprintf(pin_index_buffer,"%d", gpio);
155 result = write(file,pin_index_buffer,strlen(pin_index_buffer));
156 if(result < 0)
157 {
158 LOGE("Gpio[%d] export fail.", gpio);
159 close(file);
160 return -1;
161 }
162 close(file);
163
164 return 0;
165}
166
167static int gpio_unexport(int gpio)
168{
169 int file=-1;
170 int result =-1;
171 char pin_index_buffer[5]= {0};
172 if (gpio_check(gpio) == -1)
173 {
174 LOGE("export has been not add\n");
175 return -1;
176 }
177
178 file = open("/sys/class/gpio/unexport",O_WRONLY);
179 if(file == -1)
180 {
181 LOGE("Open gpio unexport file fail.");
182 return -1;
183 }
184
185 memset(pin_index_buffer,0,5);
186 sprintf(pin_index_buffer,"%d", gpio);
187 result=write(file,pin_index_buffer,strlen(pin_index_buffer));
188 if(result < 0)
189 {
190 close(file);
191 LOGE("Gpio[%d] unexport fail.", gpio);
192 return -1;
193 }
194 close(file);
195
196 return 0;
197}
198
199int32_t gsw_gpio_SetDirection(uint32_t gpioNum, gsw_hal_pinDirection dir)
200{
201 char buffer[128]= {0};
202 int file =-1;
203 int result =-1;
204 char direction[10] = {0};
205 memset(buffer,0,128);
206 if (handle())
207 return GSW_HAL_FAIL;
208
209 if (gpio_check(gpioNum) == -1)
210 {
211 LOGD("in dir export has been not add\n");
212 gpio_export(gpioNum);
213 }
214
215
216 sprintf(buffer,"/sys/class/gpio/gpio%u/direction", gpioNum);
217 file = open(buffer, O_WRONLY);
218 if(file == -1)
219 {
220 LOGE("Open gpio[%d] direct fail.", gpioNum);
221 return -1;
222 }
223 if (dir == GSW_HAL_PINDIR_IN)
224 sprintf(direction,"in");
225 else if (dir == GSW_HAL_PINDIR_OUT)
226 sprintf(direction,"out");
227
228 result = write(file,direction,strlen(direction));
229 if(result != strlen(direction))
230 {
231 LOGE("Set gpio[%d] direct fail.", gpioNum);
232 close(file);
233 gpio_unexport(gpioNum);
234 return GSW_HAL_FAIL;
235 }
236 close(file);
237 gpio_unexport(gpioNum);
238 return GSW_HAL_SUCCESS;
239}
240
241gsw_hal_pinLevel gsw_gpio_GetLevel(uint32_t gpioNum)
242{
243 char buffer[50];
244 char path[128];
245 int file =-1;
246 int result =-1;
247 memset(path,0,128);
248 memset(buffer,0,50);
249 if (handle())
250 return GSW_HAL_FAIL;
251
252 if (gpio_check(gpioNum) == -1)
253 {
254 LOGD("in get value export has been not add\n");
255 gpio_export(gpioNum);
256 }
257 sprintf(path,"/sys/class/gpio/gpio%u/value", gpioNum);
258 file = open(path,O_RDONLY);
259 if(file == -1)
260 {
261 LOGE("Open gpio[%d] fail.", gpioNum);
262 return -1;
263 }
264 result = read(file,buffer,50);
265 if(result <= 0)
266 {
267 LOGE("Get gpio[%d] value fail", gpioNum);
268 close(file);
269 gpio_unexport(gpioNum);
270 return GSW_HAL_FAIL;
271 }
272 close(file);
273 gpio_unexport(gpioNum);
274 return atoi(buffer);
275}
276
277int32_t gsw_gpio_SetLevel(uint32_t gpioNum, gsw_hal_pinLevel level)
278{
279 char buffer[128]= {0};
280 int file =-1;
281 int result =-1;
282 memset(buffer,0,50);
283 if (handle())
284 return GSW_HAL_FAIL;
285
286 if (gpio_check(gpioNum) == -1)
287 {
288 LOGD("in set value export has been not add\n");
289 gpio_export(gpioNum);
290 }
291
292 sprintf(buffer,"/sys/class/gpio/gpio%u/value", gpioNum);
293 file = open(buffer,O_WRONLY);
294 if(file == -1)
295 {
296 LOGE("Open gpio[%d] value fail.", gpioNum);
297 return -1;
298 }
299 if(level == GSW_HAL_LEVEL_LOW) {
300 result = write(file,"0",1);
301 } else {
302 result = write(file,"1",1);
303 }
304 if(result != 1)
305 {
306 LOGE("Set gpio[%d] value fail err =%d.", gpioNum, errno);
307 close(file);
308 gpio_unexport(gpioNum);
309 return GSW_HAL_FAIL;
310 }
311 close(file);
312 gpio_unexport(gpioNum);
313 return GSW_HAL_SUCCESS;
314}
315
316
317int32_t gsw_gpio_Init(uint32_t gpioNum, gsw_hal_pinDirection dir, gsw_hal_pinLevel level, gsw_hal_pinPullSel pullsel)
318{
319 if (handle())
320 return GSW_HAL_FAIL;
321
322 if (dir != 1 && dir != 0)
323 {
324 LOGE("[lynq_gpio_init] direction fail.");
325 return -1;
326 }
327
328 if (level != 1 && level != 0)
329 {
330 LOGE("[lynq_gpio_init] value fail.");
331 return GSW_HAL_FAIL;
332 }
333
334 if(gpio_export(gpioNum))
335 {
336 LOGE("[lynq_gpio_init]gpio_export fail.");
337 return GSW_HAL_FAIL;
338 }
339
340 if(gsw_gpio_SetDirection(gpioNum, dir))
341 {
342 LOGE("[lynq_gpio_init]gpio_direct_set fail.");
343 return GSW_HAL_FAIL;
344 }
345
346 if(dir == 1 && (gsw_gpio_SetLevel(gpioNum, level) != 0))
347 {
348 LOGE("[lynq_gpio_init]gpio_value_set fail.");
349 return GSW_HAL_FAIL;
350 }
351 return GSW_HAL_SUCCESS;
352}
353gsw_hal_usbStatus gsw_hwpin_is_usb_connected(void)
354{
355 FILE *fp;
356 char cmd[128] = {0};
357 char tmp_rsp[20] = {0};
358 //char *CONNECTED = "configured";
359 char *DISCONNECTED = "not attached";
360 if (handle())
361 return GSW_HAL_FAIL;
362
363 sprintf(cmd,"cat /sys/devices/platform/soc/d4200000.axi/c0000000.usb/udc/c0000000.usb/state");
364 fp=popen(cmd, "r");
365 if (fgets(tmp_rsp,sizeof(tmp_rsp),fp) == NULL)
366 LOGE("gsw_hwpin_is_usb_connected fail.");
367 pclose(fp);
368 if (strncmp(tmp_rsp,DISCONNECTED,strlen(DISCONNECTED)) == 0)
369 return GSW_HAL_USB_DISCONNECTED;
370 else
371 return GSW_HAL_USB_CONNECTED;
372}
373
374int32_t gsw_setWakeupLevel(gsw_hal_pinLevel level)
375{
376 if (gsw_gpio_SetLevel(wakeout, level) == GSW_HAL_FAIL)
377 {
378 return GSW_HAL_FAIL;
379 }
380 return GSW_HAL_SUCCESS;
381}