1 /*
2 * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #include "gpio_if.h"
10 #include "devsvc_manager_clnt.h"
11 #include "gpio/gpio_core.h"
12
13 #include "hdf_base.h"
14
15 #define HDF_LOG_TAG gpio_if
16
GpioRead(uint16_t gpio,uint16_t * val)17 int32_t GpioRead(uint16_t gpio, uint16_t *val)
18 {
19 int32_t ret;
20 struct GpioCntlr *cntlr = GpioCntlrGetByGpio(gpio);
21
22 ret = GpioCntlrRead(cntlr, GpioCntlrGetLocal(cntlr, gpio), val);
23
24 GpioCntlrPut(cntlr);
25 return ret;
26 }
27
GpioWrite(uint16_t gpio,uint16_t val)28 int32_t GpioWrite(uint16_t gpio, uint16_t val)
29 {
30 int32_t ret;
31 struct GpioCntlr *cntlr = GpioCntlrGetByGpio(gpio);
32
33 ret = GpioCntlrWrite(cntlr, GpioCntlrGetLocal(cntlr, gpio), val);
34
35 GpioCntlrPut(cntlr);
36 return ret;
37 }
38
GpioSetDir(uint16_t gpio,uint16_t dir)39 int32_t GpioSetDir(uint16_t gpio, uint16_t dir)
40 {
41 int32_t ret;
42 struct GpioCntlr *cntlr = GpioCntlrGetByGpio(gpio);
43
44 ret = GpioCntlrSetDir(cntlr, GpioCntlrGetLocal(cntlr, gpio), dir);
45
46 GpioCntlrPut(cntlr);
47 return ret;
48 }
49
GpioGetDir(uint16_t gpio,uint16_t * dir)50 int32_t GpioGetDir(uint16_t gpio, uint16_t *dir)
51 {
52 int32_t ret;
53 struct GpioCntlr *cntlr = GpioCntlrGetByGpio(gpio);
54
55 ret = GpioCntlrGetDir(cntlr, GpioCntlrGetLocal(cntlr, gpio), dir);
56
57 GpioCntlrPut(cntlr);
58 return ret;
59 }
60
GpioSetIrq(uint16_t gpio,uint16_t mode,GpioIrqFunc func,void * arg)61 int32_t GpioSetIrq(uint16_t gpio, uint16_t mode, GpioIrqFunc func, void *arg)
62 {
63 int32_t ret;
64 struct GpioCntlr *cntlr = GpioCntlrGetByGpio(gpio);
65
66 ret = GpioCntlrSetIrq(cntlr, GpioCntlrGetLocal(cntlr, gpio), mode, func, arg);
67
68 GpioCntlrPut(cntlr);
69 return ret;
70 }
71
GpioUnsetIrq(uint16_t gpio,void * arg)72 int32_t GpioUnsetIrq(uint16_t gpio, void *arg)
73 {
74 int32_t ret;
75 struct GpioCntlr *cntlr = GpioCntlrGetByGpio(gpio);
76
77 ret = GpioCntlrUnsetIrq(cntlr, GpioCntlrGetLocal(cntlr, gpio), arg);
78
79 GpioCntlrPut(cntlr);
80 return ret;
81 }
82
GpioEnableIrq(uint16_t gpio)83 int32_t GpioEnableIrq(uint16_t gpio)
84 {
85 int32_t ret;
86 struct GpioCntlr *cntlr = GpioCntlrGetByGpio(gpio);
87
88 ret = GpioCntlrEnableIrq(cntlr, GpioCntlrGetLocal(cntlr, gpio));
89
90 GpioCntlrPut(cntlr);
91 return ret;
92 }
93
GpioDisableIrq(uint16_t gpio)94 int32_t GpioDisableIrq(uint16_t gpio)
95 {
96 int32_t ret;
97 struct GpioCntlr *cntlr = GpioCntlrGetByGpio(gpio);
98
99 ret = GpioCntlrDisableIrq(cntlr, GpioCntlrGetLocal(cntlr, gpio));
100
101 GpioCntlrPut(cntlr);
102 return ret;
103 }
104
GpioGetByName(const char * gpioName)105 int32_t GpioGetByName(const char *gpioName)
106 {
107 int32_t ret;
108 struct GpioCntlr *cntlr = NULL;
109
110 if (gpioName == NULL || strlen(gpioName) > GPIO_NAME_LEN) {
111 HDF_LOGE("GpioGetByName: gpioName is null or gpioName len out of range!");
112 return HDF_ERR_INVALID_OBJECT;
113 }
114
115 cntlr = GpioCntlrGetByGpioName(gpioName);
116 if (cntlr == NULL) {
117 HDF_LOGE("GpioGetByName: cntlr is null!");
118 return HDF_ERR_INVALID_OBJECT;
119 }
120
121 ret = GpioCntlrGetNumByGpioName(cntlr, gpioName);
122
123 GpioCntlrPut(cntlr);
124 return ret;
125 }
126