1 /******************************************************************************
2  *
3  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
4  * Analog Devices, Inc.),
5  * Copyright (C) 2023-2024 Analog Devices, Inc.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************************/
20 
21 /* **** Includes **** */
22 #include <stddef.h>
23 #include "mxc_device.h"
24 #include "mxc_assert.h"
25 #include "gpio.h"
26 #include "gpio_reva.h"
27 #include "gpio_common.h"
28 #include "mxc_sys.h"
29 #include "mxc_errors.h"
30 
31 /* **** Functions **** */
32 
MXC_GPIO_Init(uint32_t portmask)33 int MXC_GPIO_Init(uint32_t portmask)
34 {
35     if (portmask & MXC_GPIO_PORT_0) {
36         MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_GPIO0);
37     }
38 
39     if (portmask & MXC_GPIO_PORT_1) {
40         MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_GPIO1);
41     }
42 
43     if (portmask & MXC_GPIO_PORT_2) {
44         MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_GPIO2);
45     }
46 
47     if (portmask & MXC_GPIO_PORT_3) {
48         MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_GPIO3);
49     }
50 
51     return MXC_GPIO_Common_Init(portmask);
52 }
53 
MXC_GPIO_Shutdown(uint32_t portmask)54 int MXC_GPIO_Shutdown(uint32_t portmask)
55 {
56     if (portmask & MXC_GPIO_PORT_0) {
57         MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_GPIO0);
58     }
59 
60     if (portmask & MXC_GPIO_PORT_1) {
61         MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_GPIO1);
62     }
63 
64     if (portmask & MXC_GPIO_PORT_2) {
65         MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_GPIO2);
66     }
67 
68     if (portmask & MXC_GPIO_PORT_3) {
69         MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_GPIO3);
70     }
71 
72     return E_NO_ERROR;
73 }
74 
MXC_GPIO_Reset(uint32_t portmask)75 int MXC_GPIO_Reset(uint32_t portmask)
76 {
77     if (portmask & MXC_GPIO_PORT_0) {
78         MXC_SYS_Reset_Periph(MXC_SYS_RESET0_GPIO0);
79     }
80 
81     if (portmask & MXC_GPIO_PORT_1) {
82         MXC_SYS_Reset_Periph(MXC_SYS_RESET0_GPIO1);
83     }
84 
85     if (portmask & MXC_GPIO_PORT_2) {
86         MXC_SYS_Reset_Periph(MXC_SYS_RESET0_GPIO2);
87     }
88 
89     if (portmask & MXC_GPIO_PORT_3) {
90         MXC_SYS_Reset_Periph(MXC_SYS_RESET1_GPIO3);
91     }
92 
93     return E_NO_ERROR;
94 }
95 
MXC_GPIO_Config(const mxc_gpio_cfg_t * cfg)96 int MXC_GPIO_Config(const mxc_gpio_cfg_t *cfg)
97 {
98     int error;
99     mxc_gpio_regs_t *gpio = cfg->port;
100 
101     // Configure the vssel
102     error = MXC_GPIO_SetVSSEL(gpio, cfg->vssel, cfg->mask);
103     if (error != E_NO_ERROR) {
104         return error;
105     }
106 
107     // Configure alternate function
108     error = MXC_GPIO_RevA_SetAF((mxc_gpio_reva_regs_t *)gpio, cfg->func, cfg->mask);
109 
110     if (error != E_NO_ERROR) {
111         return error;
112     }
113 
114     // Configure the pad
115     switch (cfg->pad) {
116     case MXC_GPIO_PAD_NONE:
117         gpio->pad_cfg1 &= ~cfg->mask;
118         gpio->pad_cfg2 &= ~cfg->mask;
119         break;
120 
121     case MXC_GPIO_PAD_WEAK_PULL_UP:
122         gpio->pad_cfg1 |= cfg->mask;
123         gpio->pad_cfg2 &= ~cfg->mask;
124         gpio->ps &= ~cfg->mask;
125         break;
126 
127     case MXC_GPIO_PAD_PULL_UP:
128         gpio->pad_cfg1 |= cfg->mask;
129         gpio->pad_cfg2 &= ~cfg->mask;
130         gpio->ps |= cfg->mask;
131         break;
132 
133     case MXC_GPIO_PAD_WEAK_PULL_DOWN:
134         gpio->pad_cfg1 &= ~cfg->mask;
135         gpio->pad_cfg2 |= cfg->mask;
136         gpio->ps &= ~cfg->mask;
137         break;
138 
139     case MXC_GPIO_PAD_PULL_DOWN:
140         gpio->pad_cfg1 &= ~cfg->mask;
141         gpio->pad_cfg2 |= cfg->mask;
142         gpio->ps |= cfg->mask;
143         break;
144 
145     default:
146         return E_BAD_PARAM;
147     }
148 
149     // Configure the drive strength
150     if (cfg->func == MXC_GPIO_FUNC_IN) {
151         return E_NO_ERROR;
152     } else {
153         return MXC_GPIO_SetDriveStrength(gpio, cfg->drvstr, cfg->mask);
154     }
155 }
156 
MXC_GPIO_InGet(mxc_gpio_regs_t * port,uint32_t mask)157 uint32_t MXC_GPIO_InGet(mxc_gpio_regs_t *port, uint32_t mask)
158 {
159     return MXC_GPIO_RevA_InGet((mxc_gpio_reva_regs_t *)port, mask);
160 }
161 
MXC_GPIO_OutSet(mxc_gpio_regs_t * port,uint32_t mask)162 void MXC_GPIO_OutSet(mxc_gpio_regs_t *port, uint32_t mask)
163 {
164     MXC_GPIO_RevA_OutSet((mxc_gpio_reva_regs_t *)port, mask);
165 }
166 
MXC_GPIO_OutClr(mxc_gpio_regs_t * port,uint32_t mask)167 void MXC_GPIO_OutClr(mxc_gpio_regs_t *port, uint32_t mask)
168 {
169     MXC_GPIO_RevA_OutClr((mxc_gpio_reva_regs_t *)port, mask);
170 }
171 
MXC_GPIO_OutGet(mxc_gpio_regs_t * port,uint32_t mask)172 uint32_t MXC_GPIO_OutGet(mxc_gpio_regs_t *port, uint32_t mask)
173 {
174     return MXC_GPIO_RevA_OutGet((mxc_gpio_reva_regs_t *)port, mask);
175 }
176 
MXC_GPIO_OutPut(mxc_gpio_regs_t * port,uint32_t mask,uint32_t val)177 void MXC_GPIO_OutPut(mxc_gpio_regs_t *port, uint32_t mask, uint32_t val)
178 {
179     MXC_GPIO_RevA_OutPut((mxc_gpio_reva_regs_t *)port, mask, val);
180 }
181 
MXC_GPIO_OutToggle(mxc_gpio_regs_t * port,uint32_t mask)182 void MXC_GPIO_OutToggle(mxc_gpio_regs_t *port, uint32_t mask)
183 {
184     MXC_GPIO_RevA_OutToggle((mxc_gpio_reva_regs_t *)port, mask);
185 }
186 
MXC_GPIO_IntConfig(const mxc_gpio_cfg_t * cfg,mxc_gpio_int_pol_t pol)187 int MXC_GPIO_IntConfig(const mxc_gpio_cfg_t *cfg, mxc_gpio_int_pol_t pol)
188 {
189     return MXC_GPIO_RevA_IntConfig(cfg, pol);
190 }
191 
MXC_GPIO_EnableInt(mxc_gpio_regs_t * port,uint32_t mask)192 void MXC_GPIO_EnableInt(mxc_gpio_regs_t *port, uint32_t mask)
193 {
194     MXC_GPIO_RevA_EnableInt((mxc_gpio_reva_regs_t *)port, mask);
195 }
196 
MXC_GPIO_DisableInt(mxc_gpio_regs_t * port,uint32_t mask)197 void MXC_GPIO_DisableInt(mxc_gpio_regs_t *port, uint32_t mask)
198 {
199     MXC_GPIO_RevA_DisableInt((mxc_gpio_reva_regs_t *)port, mask);
200 }
201 
MXC_GPIO_RegisterCallback(const mxc_gpio_cfg_t * cfg,mxc_gpio_callback_fn func,void * cbdata)202 void MXC_GPIO_RegisterCallback(const mxc_gpio_cfg_t *cfg, mxc_gpio_callback_fn func, void *cbdata)
203 {
204     MXC_GPIO_Common_RegisterCallback(cfg, func, cbdata);
205 }
206 
MXC_GPIO_Handler(unsigned int port)207 void MXC_GPIO_Handler(unsigned int port)
208 {
209     MXC_GPIO_Common_Handler(port);
210 }
211 
MXC_GPIO_ClearFlags(mxc_gpio_regs_t * port,uint32_t flags)212 void MXC_GPIO_ClearFlags(mxc_gpio_regs_t *port, uint32_t flags)
213 {
214     MXC_GPIO_RevA_ClearFlags((mxc_gpio_reva_regs_t *)port, flags);
215 }
216 
MXC_GPIO_GetFlags(mxc_gpio_regs_t * port)217 uint32_t MXC_GPIO_GetFlags(mxc_gpio_regs_t *port)
218 {
219     return MXC_GPIO_RevA_GetFlags((mxc_gpio_reva_regs_t *)port);
220 }
221 
MXC_GPIO_SetVSSEL(mxc_gpio_regs_t * port,mxc_gpio_vssel_t vssel,uint32_t mask)222 int MXC_GPIO_SetVSSEL(mxc_gpio_regs_t *port, mxc_gpio_vssel_t vssel, uint32_t mask)
223 {
224     return MXC_GPIO_RevA_SetVSSEL((mxc_gpio_reva_regs_t *)port, vssel, mask);
225 }
226 
MXC_GPIO_SetWakeEn(mxc_gpio_regs_t * port,uint32_t mask)227 void MXC_GPIO_SetWakeEn(mxc_gpio_regs_t *port, uint32_t mask)
228 {
229     MXC_GPIO_RevA_SetWakeEn((mxc_gpio_reva_regs_t *)port, mask);
230 }
231 
MXC_GPIO_ClearWakeEn(mxc_gpio_regs_t * port,uint32_t mask)232 void MXC_GPIO_ClearWakeEn(mxc_gpio_regs_t *port, uint32_t mask)
233 {
234     MXC_GPIO_RevA_ClearWakeEn((mxc_gpio_reva_regs_t *)port, mask);
235 }
236 
MXC_GPIO_GetWakeEn(mxc_gpio_regs_t * port)237 uint32_t MXC_GPIO_GetWakeEn(mxc_gpio_regs_t *port)
238 {
239     return MXC_GPIO_RevA_GetWakeEn((mxc_gpio_reva_regs_t *)port);
240 }
241 
MXC_GPIO_SetDriveStrength(mxc_gpio_regs_t * port,mxc_gpio_drvstr_t drvstr,uint32_t mask)242 int MXC_GPIO_SetDriveStrength(mxc_gpio_regs_t *port, mxc_gpio_drvstr_t drvstr, uint32_t mask)
243 {
244     return MXC_GPIO_RevA_SetDriveStrength((mxc_gpio_reva_regs_t *)port, drvstr, mask);
245 }
246