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 /* **** Definitions **** */
32 
33 /* **** Globals **** */
34 
35 /* **** Functions **** */
36 
MXC_GPIO_Init(uint32_t portmask)37 int MXC_GPIO_Init(uint32_t portmask)
38 {
39     if (portmask & 0x1) {
40         MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_GPIO0);
41     }
42 
43     if (portmask & 0x2) {
44         MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_GPIO1);
45     }
46 
47     return MXC_GPIO_Common_Init(portmask);
48 }
49 
MXC_GPIO_Shutdown(uint32_t portmask)50 int MXC_GPIO_Shutdown(uint32_t portmask)
51 {
52     if (portmask & 0x1) {
53         MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_GPIO0);
54     }
55 
56     if (portmask & 0x2) {
57         MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_GPIO1);
58     }
59 
60     return E_NO_ERROR;
61 }
62 
MXC_GPIO_Reset(uint32_t portmask)63 int MXC_GPIO_Reset(uint32_t portmask)
64 {
65     if (portmask & 0x1) {
66         MXC_SYS_Reset_Periph(MXC_SYS_RESET_GPIO0);
67     }
68 
69     if (portmask & 0x2) {
70         MXC_SYS_Reset_Periph(MXC_SYS_RESET_GPIO1);
71     }
72 
73     return E_NO_ERROR;
74 }
75 
MXC_GPIO_Config(const mxc_gpio_cfg_t * cfg)76 int MXC_GPIO_Config(const mxc_gpio_cfg_t *cfg)
77 {
78     int error;
79     mxc_gpio_regs_t *gpio = cfg->port;
80 
81     // Configure the vssel
82     error = MXC_GPIO_SetVSSEL(gpio, cfg->vssel, cfg->mask);
83     if (error != E_NO_ERROR) {
84         return error;
85     }
86 
87     // Configure alternate function
88     error = MXC_GPIO_RevA_SetAF((mxc_gpio_reva_regs_t *)gpio, cfg->func, cfg->mask);
89 
90     if (error != E_NO_ERROR) {
91         return error;
92     }
93 
94     // Configure the pad
95     switch (cfg->pad) {
96     case MXC_GPIO_PAD_NONE:
97         gpio->pad_cfg1 &= ~cfg->mask;
98         gpio->pad_cfg2 &= ~cfg->mask;
99         break;
100 
101     case MXC_GPIO_PAD_WEAK_PULL_UP:
102         gpio->pad_cfg1 |= cfg->mask;
103         gpio->pad_cfg2 &= ~cfg->mask;
104         gpio->ps &= ~cfg->mask;
105         break;
106 
107     case MXC_GPIO_PAD_PULL_UP:
108         gpio->pad_cfg1 |= cfg->mask;
109         gpio->pad_cfg2 &= ~cfg->mask;
110         gpio->ps |= cfg->mask;
111         break;
112 
113     case MXC_GPIO_PAD_WEAK_PULL_DOWN:
114         gpio->pad_cfg1 &= ~cfg->mask;
115         gpio->pad_cfg2 |= cfg->mask;
116         gpio->ps &= ~cfg->mask;
117         break;
118 
119     case MXC_GPIO_PAD_PULL_DOWN:
120         gpio->pad_cfg1 &= ~cfg->mask;
121         gpio->pad_cfg2 |= cfg->mask;
122         gpio->ps |= cfg->mask;
123         break;
124 
125     default:
126         return E_BAD_PARAM;
127     }
128 
129     return E_NO_ERROR;
130 }
131 
132 /* ************************************************************************** */
MXC_GPIO_InGet(mxc_gpio_regs_t * port,uint32_t mask)133 uint32_t MXC_GPIO_InGet(mxc_gpio_regs_t *port, uint32_t mask)
134 {
135     return MXC_GPIO_RevA_InGet((mxc_gpio_reva_regs_t *)port, mask);
136 }
137 
138 /* ************************************************************************** */
MXC_GPIO_OutSet(mxc_gpio_regs_t * port,uint32_t mask)139 void MXC_GPIO_OutSet(mxc_gpio_regs_t *port, uint32_t mask)
140 {
141     MXC_GPIO_RevA_OutSet((mxc_gpio_reva_regs_t *)port, mask);
142 }
143 
144 /* ************************************************************************** */
MXC_GPIO_OutClr(mxc_gpio_regs_t * port,uint32_t mask)145 void MXC_GPIO_OutClr(mxc_gpio_regs_t *port, uint32_t mask)
146 {
147     MXC_GPIO_RevA_OutClr((mxc_gpio_reva_regs_t *)port, mask);
148 }
149 
150 /* ************************************************************************** */
MXC_GPIO_OutGet(mxc_gpio_regs_t * port,uint32_t mask)151 uint32_t MXC_GPIO_OutGet(mxc_gpio_regs_t *port, uint32_t mask)
152 {
153     return MXC_GPIO_RevA_OutGet((mxc_gpio_reva_regs_t *)port, mask);
154 }
155 
156 /* ************************************************************************** */
MXC_GPIO_OutPut(mxc_gpio_regs_t * port,uint32_t mask,uint32_t val)157 void MXC_GPIO_OutPut(mxc_gpio_regs_t *port, uint32_t mask, uint32_t val)
158 {
159     MXC_GPIO_RevA_OutPut((mxc_gpio_reva_regs_t *)port, mask, val);
160 }
161 
162 /* ************************************************************************** */
MXC_GPIO_OutToggle(mxc_gpio_regs_t * port,uint32_t mask)163 void MXC_GPIO_OutToggle(mxc_gpio_regs_t *port, uint32_t mask)
164 {
165     MXC_GPIO_RevA_OutToggle((mxc_gpio_reva_regs_t *)port, mask);
166 }
167 
168 /* ************************************************************************** */
MXC_GPIO_IntConfig(const mxc_gpio_cfg_t * cfg,mxc_gpio_int_pol_t pol)169 int MXC_GPIO_IntConfig(const mxc_gpio_cfg_t *cfg, mxc_gpio_int_pol_t pol)
170 {
171     return MXC_GPIO_RevA_IntConfig(cfg, pol);
172 }
173 
174 /* ************************************************************************** */
MXC_GPIO_EnableInt(mxc_gpio_regs_t * port,uint32_t mask)175 void MXC_GPIO_EnableInt(mxc_gpio_regs_t *port, uint32_t mask)
176 {
177     MXC_GPIO_RevA_EnableInt((mxc_gpio_reva_regs_t *)port, mask);
178 }
179 
180 /* ************************************************************************** */
MXC_GPIO_DisableInt(mxc_gpio_regs_t * port,uint32_t mask)181 void MXC_GPIO_DisableInt(mxc_gpio_regs_t *port, uint32_t mask)
182 {
183     MXC_GPIO_RevA_DisableInt((mxc_gpio_reva_regs_t *)port, mask);
184 }
185 
186 /* ************************************************************************** */
MXC_GPIO_RegisterCallback(const mxc_gpio_cfg_t * cfg,mxc_gpio_callback_fn func,void * cbdata)187 void MXC_GPIO_RegisterCallback(const mxc_gpio_cfg_t *cfg, mxc_gpio_callback_fn func, void *cbdata)
188 {
189     MXC_GPIO_Common_RegisterCallback(cfg, func, cbdata);
190 }
191 
192 /* ************************************************************************** */
MXC_GPIO_Handler(unsigned int port)193 void MXC_GPIO_Handler(unsigned int port)
194 {
195     MXC_GPIO_Common_Handler(port);
196 }
197 
198 /* ************************************************************************** */
MXC_GPIO_ClearFlags(mxc_gpio_regs_t * port,uint32_t flags)199 void MXC_GPIO_ClearFlags(mxc_gpio_regs_t *port, uint32_t flags)
200 {
201     MXC_GPIO_RevA_ClearFlags((mxc_gpio_reva_regs_t *)port, flags);
202 }
203 
204 /* ************************************************************************** */
MXC_GPIO_GetFlags(mxc_gpio_regs_t * port)205 uint32_t MXC_GPIO_GetFlags(mxc_gpio_regs_t *port)
206 {
207     return MXC_GPIO_RevA_GetFlags((mxc_gpio_reva_regs_t *)port);
208 }
209 
210 /* ************************************************************************** */
MXC_GPIO_SetVSSEL(mxc_gpio_regs_t * port,mxc_gpio_vssel_t vssel,uint32_t mask)211 int MXC_GPIO_SetVSSEL(mxc_gpio_regs_t *port, mxc_gpio_vssel_t vssel, uint32_t mask)
212 {
213     return MXC_GPIO_RevA_SetVSSEL((mxc_gpio_reva_regs_t *)port, vssel, mask);
214 }
215 
216 /* ************************************************************************** */
MXC_GPIO_SetWakeEn(mxc_gpio_regs_t * port,uint32_t mask)217 void MXC_GPIO_SetWakeEn(mxc_gpio_regs_t *port, uint32_t mask)
218 {
219     MXC_GPIO_RevA_SetWakeEn((mxc_gpio_reva_regs_t *)port, mask);
220 }
221 
222 /* ************************************************************************** */
MXC_GPIO_ClearWakeEn(mxc_gpio_regs_t * port,uint32_t mask)223 void MXC_GPIO_ClearWakeEn(mxc_gpio_regs_t *port, uint32_t mask)
224 {
225     MXC_GPIO_RevA_ClearWakeEn((mxc_gpio_reva_regs_t *)port, mask);
226 }
227 
228 /* ************************************************************************** */
MXC_GPIO_GetWakeEn(mxc_gpio_regs_t * port)229 uint32_t MXC_GPIO_GetWakeEn(mxc_gpio_regs_t *port)
230 {
231     return MXC_GPIO_RevA_GetWakeEn((mxc_gpio_reva_regs_t *)port);
232 }
233 
234 /* ************************************************************************** */
MXC_GPIO_SetDriveStrength(mxc_gpio_regs_t * port,mxc_gpio_drvstr_t drvstr,uint32_t mask)235 int MXC_GPIO_SetDriveStrength(mxc_gpio_regs_t *port, mxc_gpio_drvstr_t drvstr, uint32_t mask)
236 {
237     return MXC_GPIO_RevA_SetDriveStrength((mxc_gpio_reva_regs_t *)port, drvstr, mask);
238 }
239