1 /**
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 * Description: Provides pinctrl driver source \n
16 *
17 * History: \n
18 * 2022-08-25, Create file. \n
19 */
20 #include <stdbool.h>
21 #include "common_def.h"
22 #include "soc_osal.h"
23 #include "hal_pinctrl.h"
24 #include "pinctrl.h"
25
uapi_pin_init(void)26 void uapi_pin_init(void)
27 {
28 pin_port_register_hal_funcs();
29 }
30
uapi_pin_deinit(void)31 void uapi_pin_deinit(void)
32 {
33 pin_port_unregister_hal_funcs();
34 }
35
uapi_pin_set_mode(pin_t pin,pin_mode_t mode)36 errcode_t uapi_pin_set_mode(pin_t pin, pin_mode_t mode)
37 {
38 if (unlikely((pin >= PIN_MAX_NUMBER) || (mode >= PIN_MODE_MAX))) {
39 return ERRCODE_PIN_INVALID_PARAMETER;
40 }
41 if (unlikely(pin_check_mode_is_valid(pin, mode) == false)) {
42 return ERRCODE_PIN_MODE_NO_FUNC;
43 }
44 hal_pin_funcs_t *pin_funcs = hal_pin_get_funcs();
45 if (unlikely((pin_funcs == NULL) || (pin_funcs->set_mode == NULL))) {
46 return ERRCODE_PIN_NOT_INIT;
47 }
48
49 uint32_t irq_sts = osal_irq_lock();
50 errcode_t ret = pin_funcs->set_mode(pin, mode);
51 osal_irq_restore(irq_sts);
52
53 return ret;
54 }
55
uapi_pin_get_mode(pin_t pin)56 pin_mode_t uapi_pin_get_mode(pin_t pin)
57 {
58 if (unlikely(pin >= PIN_MAX_NUMBER)) {
59 return PIN_MODE_MAX;
60 }
61 pin_mode_t mode = PIN_MODE_MAX;
62 hal_pin_funcs_t *pin_funcs = hal_pin_get_funcs();
63 if (likely((pin_funcs != NULL) && (pin_funcs->get_mode != NULL))) {
64 uint32_t irq_sts = osal_irq_lock();
65 mode = pin_funcs->get_mode(pin);
66 osal_irq_restore(irq_sts);
67 }
68 return mode;
69 }
70
uapi_pin_set_ds(pin_t pin,pin_drive_strength_t ds)71 errcode_t uapi_pin_set_ds(pin_t pin, pin_drive_strength_t ds)
72 {
73 if (unlikely((pin >= PIN_MAX_NUMBER) || (ds >= PIN_DS_MAX))) {
74 return ERRCODE_PIN_INVALID_PARAMETER;
75 }
76 hal_pin_funcs_t *pin_funcs = hal_pin_get_funcs();
77 if (unlikely((pin_funcs == NULL) || (pin_funcs->set_ds == NULL))) {
78 return ERRCODE_PIN_NOT_INIT;
79 }
80
81 uint32_t irq_sts = osal_irq_lock();
82 errcode_t ret = pin_funcs->set_ds(pin, ds);
83 osal_irq_restore(irq_sts);
84
85 return ret;
86 }
87
uapi_pin_get_ds(pin_t pin)88 pin_drive_strength_t uapi_pin_get_ds(pin_t pin)
89 {
90 if (unlikely(pin >= PIN_MAX_NUMBER)) {
91 return PIN_DS_MAX;
92 }
93 pin_drive_strength_t ds = PIN_DS_MAX;
94 hal_pin_funcs_t *pin_funcs = hal_pin_get_funcs();
95 if (likely((pin_funcs != NULL) && (pin_funcs->get_ds != NULL))) {
96 uint32_t irq_sts = osal_irq_lock();
97 ds = pin_funcs->get_ds(pin);
98 osal_irq_restore(irq_sts);
99 }
100 return ds;
101 }
102
uapi_pin_set_pull(pin_t pin,pin_pull_t pull_type)103 errcode_t uapi_pin_set_pull(pin_t pin, pin_pull_t pull_type)
104 {
105 if (unlikely((pin >= PIN_MAX_NUMBER) || (pull_type >= PIN_PULL_MAX))) {
106 return ERRCODE_PIN_INVALID_PARAMETER;
107 }
108 hal_pin_funcs_t *pin_funcs = hal_pin_get_funcs();
109 if (unlikely((pin_funcs == NULL) || (pin_funcs->set_pull == NULL))) {
110 return ERRCODE_PIN_NOT_INIT;
111 }
112
113 uint32_t irq_sts = osal_irq_lock();
114 errcode_t ret = pin_funcs->set_pull(pin, pull_type);
115 osal_irq_restore(irq_sts);
116
117 return ret;
118 }
119
uapi_pin_get_pull(pin_t pin)120 pin_pull_t uapi_pin_get_pull(pin_t pin)
121 {
122 if (unlikely(pin >= PIN_MAX_NUMBER)) {
123 return PIN_PULL_MAX;
124 }
125 pin_pull_t pull_type = PIN_PULL_MAX;
126 hal_pin_funcs_t *pin_funcs = hal_pin_get_funcs();
127 if (likely((pin_funcs != NULL) && (pin_funcs->get_pull != NULL))) {
128 uint32_t irq_sts = osal_irq_lock();
129 pull_type = pin_funcs->get_pull(pin);
130 osal_irq_restore(irq_sts);
131 }
132 return pull_type;
133 }
134
135 #if defined(CONFIG_PINCTRL_SUPPORT_IE)
uapi_pin_set_ie(pin_t pin,pin_input_enable_t ie)136 errcode_t uapi_pin_set_ie(pin_t pin, pin_input_enable_t ie)
137 {
138 if (unlikely((pin >= PIN_MAX_NUMBER) || (ie >= PIN_IE_MAX))) {
139 return ERRCODE_PIN_INVALID_PARAMETER;
140 }
141 hal_pin_funcs_t *pin_funcs = hal_pin_get_funcs();
142 if (unlikely((pin_funcs == NULL) || (pin_funcs->set_ie == NULL))) {
143 return ERRCODE_PIN_NOT_INIT;
144 }
145
146 uint32_t irq_sts = osal_irq_lock();
147 errcode_t ret = pin_funcs->set_ie(pin, ie);
148 osal_irq_restore(irq_sts);
149
150 return ret;
151 }
152
uapi_pin_get_ie(pin_t pin)153 pin_input_enable_t uapi_pin_get_ie(pin_t pin)
154 {
155 if (unlikely(pin >= PIN_MAX_NUMBER)) {
156 return PIN_IE_MAX;
157 }
158 pin_input_enable_t ie = PIN_IE_MAX;
159 hal_pin_funcs_t *pin_funcs = hal_pin_get_funcs();
160 if (likely((pin_funcs != NULL) && (pin_funcs->get_ie != NULL))) {
161 uint32_t irq_sts = osal_irq_lock();
162 ie = pin_funcs->get_ie(pin);
163 osal_irq_restore(irq_sts);
164 }
165 return ie;
166 }
167 #endif /* CONFIG_PINCTRL_SUPPORT_IE */
168
169 #if defined(CONFIG_PINCTRL_SUPPORT_ST)
uapi_pin_set_st(pin_t pin,pin_schmitt_trigger_t st)170 errcode_t uapi_pin_set_st(pin_t pin, pin_schmitt_trigger_t st)
171 {
172 if (unlikely((pin >= PIN_MAX_NUMBER) || (st >= PIN_ST_MAX))) {
173 return ERRCODE_PIN_INVALID_PARAMETER;
174 }
175 hal_pin_funcs_t *pin_funcs = hal_pin_get_funcs();
176 if (unlikely((pin_funcs == NULL) || (pin_funcs->set_st == NULL))) {
177 return ERRCODE_PIN_NOT_INIT;
178 }
179
180 uint32_t irq_sts = osal_irq_lock();
181 errcode_t ret = pin_funcs->set_st(pin, st);
182 osal_irq_restore(irq_sts);
183
184 return ret;
185 }
186
uapi_pin_get_st(pin_t pin)187 pin_schmitt_trigger_t uapi_pin_get_st(pin_t pin)
188 {
189 if (unlikely(pin >= PIN_MAX_NUMBER)) {
190 return PIN_ST_MAX;
191 }
192 pin_schmitt_trigger_t st = PIN_ST_MAX;
193 hal_pin_funcs_t *pin_funcs = hal_pin_get_funcs();
194 if (likely((pin_funcs != NULL) && (pin_funcs->get_st != NULL))) {
195 uint32_t irq_sts = osal_irq_lock();
196 st = pin_funcs->get_st(pin);
197 osal_irq_restore(irq_sts);
198 }
199 return st;
200 }
201 #endif /* CONFIG_PINCTRL_SUPPORT_ST */
202
203 #if defined(CONFIG_PINCTRL_SUPPORT_LPM)
uapi_pin_suspend(uintptr_t arg)204 errcode_t uapi_pin_suspend(uintptr_t arg)
205 {
206 unused(arg);
207 return ERRCODE_SUCC;
208 }
209
uapi_pin_resume(uintptr_t arg)210 errcode_t uapi_pin_resume(uintptr_t arg)
211 {
212 unused(arg);
213 return ERRCODE_SUCC;
214 }
215 #endif /* CONFIG_PINCTRL_SUPPORT_LPM */