1 /*
2  * Copyright (c) 2016 Open-RnD Sp. z o.o.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_GPIO_GPIO_STM32_H_
8 #define ZEPHYR_DRIVERS_GPIO_GPIO_STM32_H_
9 
10 /**
11  * @file header for STM32 GPIO
12  */
13 
14 #include <zephyr/drivers/clock_control/stm32_clock_control.h>
15 #include <zephyr/drivers/gpio.h>
16 #if DT_HAS_COMPAT_STATUS_OKAY(st_stm32f1_pinctrl)
17 #include <zephyr/dt-bindings/pinctrl/stm32f1-pinctrl.h>
18 #else
19 #include <zephyr/dt-bindings/pinctrl/stm32-pinctrl.h>
20 #endif /* DT_HAS_COMPAT_STATUS_OKAY(st_stm32f1_pinctrl) */
21 
22 /* GPIO buses definitions */
23 
24 #define STM32_PORT_NOT_AVAILABLE 0xFFFFFFFF
25 
26 #ifdef CONFIG_SOC_SERIES_STM32F0X
27 #define STM32_CLOCK_BUS_GPIO STM32_CLOCK_BUS_AHB1
28 #define STM32_PERIPH_GPIOA LL_AHB1_GRP1_PERIPH_GPIOA
29 #define STM32_PERIPH_GPIOB LL_AHB1_GRP1_PERIPH_GPIOB
30 #define STM32_PERIPH_GPIOC LL_AHB1_GRP1_PERIPH_GPIOC
31 #define STM32_PERIPH_GPIOD LL_AHB1_GRP1_PERIPH_GPIOD
32 #define STM32_PERIPH_GPIOE LL_AHB1_GRP1_PERIPH_GPIOE
33 #define STM32_PERIPH_GPIOF LL_AHB1_GRP1_PERIPH_GPIOF
34 #elif CONFIG_SOC_SERIES_STM32F1X
35 #define STM32_CLOCK_BUS_GPIO STM32_CLOCK_BUS_APB2
36 #define STM32_PERIPH_GPIOA LL_APB2_GRP1_PERIPH_GPIOA
37 #define STM32_PERIPH_GPIOB LL_APB2_GRP1_PERIPH_GPIOB
38 #define STM32_PERIPH_GPIOC LL_APB2_GRP1_PERIPH_GPIOC
39 #define STM32_PERIPH_GPIOD LL_APB2_GRP1_PERIPH_GPIOD
40 #define STM32_PERIPH_GPIOE LL_APB2_GRP1_PERIPH_GPIOE
41 #define STM32_PERIPH_GPIOF LL_APB2_GRP1_PERIPH_GPIOF
42 #define STM32_PERIPH_GPIOG LL_APB2_GRP1_PERIPH_GPIOG
43 #elif CONFIG_SOC_SERIES_STM32F2X
44 #define STM32_CLOCK_BUS_GPIO STM32_CLOCK_BUS_AHB1
45 #define STM32_PERIPH_GPIOA LL_AHB1_GRP1_PERIPH_GPIOA
46 #define STM32_PERIPH_GPIOB LL_AHB1_GRP1_PERIPH_GPIOB
47 #define STM32_PERIPH_GPIOC LL_AHB1_GRP1_PERIPH_GPIOC
48 #define STM32_PERIPH_GPIOD LL_AHB1_GRP1_PERIPH_GPIOD
49 #define STM32_PERIPH_GPIOE LL_AHB1_GRP1_PERIPH_GPIOE
50 #define STM32_PERIPH_GPIOF LL_AHB1_GRP1_PERIPH_GPIOF
51 #define STM32_PERIPH_GPIOG LL_AHB1_GRP1_PERIPH_GPIOG
52 #define STM32_PERIPH_GPIOH LL_AHB1_GRP1_PERIPH_GPIOH
53 #define STM32_PERIPH_GPIOI LL_AHB1_GRP1_PERIPH_GPIOI
54 #elif CONFIG_SOC_SERIES_STM32F3X
55 #define STM32_CLOCK_BUS_GPIO STM32_CLOCK_BUS_AHB1
56 #define STM32_PERIPH_GPIOA LL_AHB1_GRP1_PERIPH_GPIOA
57 #define STM32_PERIPH_GPIOB LL_AHB1_GRP1_PERIPH_GPIOB
58 #define STM32_PERIPH_GPIOC LL_AHB1_GRP1_PERIPH_GPIOC
59 #define STM32_PERIPH_GPIOD LL_AHB1_GRP1_PERIPH_GPIOD
60 #define STM32_PERIPH_GPIOE LL_AHB1_GRP1_PERIPH_GPIOE
61 #define STM32_PERIPH_GPIOF LL_AHB1_GRP1_PERIPH_GPIOF
62 #define STM32_PERIPH_GPIOG LL_AHB1_GRP1_PERIPH_GPIOG
63 #define STM32_PERIPH_GPIOH LL_AHB1_GRP1_PERIPH_GPIOH
64 #elif CONFIG_SOC_SERIES_STM32F4X
65 #define STM32_CLOCK_BUS_GPIO STM32_CLOCK_BUS_AHB1
66 #define STM32_PERIPH_GPIOA LL_AHB1_GRP1_PERIPH_GPIOA
67 #define STM32_PERIPH_GPIOB LL_AHB1_GRP1_PERIPH_GPIOB
68 #define STM32_PERIPH_GPIOC LL_AHB1_GRP1_PERIPH_GPIOC
69 #define STM32_PERIPH_GPIOD LL_AHB1_GRP1_PERIPH_GPIOD
70 #define STM32_PERIPH_GPIOE LL_AHB1_GRP1_PERIPH_GPIOE
71 #define STM32_PERIPH_GPIOF LL_AHB1_GRP1_PERIPH_GPIOF
72 #define STM32_PERIPH_GPIOG LL_AHB1_GRP1_PERIPH_GPIOG
73 #define STM32_PERIPH_GPIOH LL_AHB1_GRP1_PERIPH_GPIOH
74 #define STM32_PERIPH_GPIOI LL_AHB1_GRP1_PERIPH_GPIOI
75 #define STM32_PERIPH_GPIOJ LL_AHB1_GRP1_PERIPH_GPIOJ
76 #define STM32_PERIPH_GPIOK LL_AHB1_GRP1_PERIPH_GPIOK
77 #elif CONFIG_SOC_SERIES_STM32F7X
78 #define STM32_CLOCK_BUS_GPIO STM32_CLOCK_BUS_AHB1
79 #define STM32_PERIPH_GPIOA LL_AHB1_GRP1_PERIPH_GPIOA
80 #define STM32_PERIPH_GPIOB LL_AHB1_GRP1_PERIPH_GPIOB
81 #define STM32_PERIPH_GPIOC LL_AHB1_GRP1_PERIPH_GPIOC
82 #define STM32_PERIPH_GPIOD LL_AHB1_GRP1_PERIPH_GPIOD
83 #define STM32_PERIPH_GPIOE LL_AHB1_GRP1_PERIPH_GPIOE
84 #define STM32_PERIPH_GPIOF LL_AHB1_GRP1_PERIPH_GPIOF
85 #define STM32_PERIPH_GPIOG LL_AHB1_GRP1_PERIPH_GPIOG
86 #define STM32_PERIPH_GPIOH LL_AHB1_GRP1_PERIPH_GPIOH
87 #define STM32_PERIPH_GPIOI LL_AHB1_GRP1_PERIPH_GPIOI
88 #define STM32_PERIPH_GPIOJ LL_AHB1_GRP1_PERIPH_GPIOJ
89 #define STM32_PERIPH_GPIOK LL_AHB1_GRP1_PERIPH_GPIOK
90 #elif CONFIG_SOC_SERIES_STM32H7X
91 #define STM32_CLOCK_BUS_GPIO STM32_CLOCK_BUS_AHB4
92 #define STM32_PERIPH_GPIOA LL_AHB4_GRP1_PERIPH_GPIOA
93 #define STM32_PERIPH_GPIOB LL_AHB4_GRP1_PERIPH_GPIOB
94 #define STM32_PERIPH_GPIOC LL_AHB4_GRP1_PERIPH_GPIOC
95 #define STM32_PERIPH_GPIOD LL_AHB4_GRP1_PERIPH_GPIOD
96 #define STM32_PERIPH_GPIOE LL_AHB4_GRP1_PERIPH_GPIOE
97 #define STM32_PERIPH_GPIOF LL_AHB4_GRP1_PERIPH_GPIOF
98 #define STM32_PERIPH_GPIOG LL_AHB4_GRP1_PERIPH_GPIOG
99 #define STM32_PERIPH_GPIOH LL_AHB4_GRP1_PERIPH_GPIOH
100 #define STM32_PERIPH_GPIOI LL_AHB4_GRP1_PERIPH_GPIOI
101 #define STM32_PERIPH_GPIOJ LL_AHB4_GRP1_PERIPH_GPIOJ
102 #define STM32_PERIPH_GPIOK LL_AHB4_GRP1_PERIPH_GPIOK
103 #elif CONFIG_SOC_SERIES_STM32G0X
104 #define STM32_CLOCK_BUS_GPIO STM32_CLOCK_BUS_IOP
105 #define STM32_PERIPH_GPIOA LL_IOP_GRP1_PERIPH_GPIOA
106 #define STM32_PERIPH_GPIOB LL_IOP_GRP1_PERIPH_GPIOB
107 #define STM32_PERIPH_GPIOC LL_IOP_GRP1_PERIPH_GPIOC
108 #define STM32_PERIPH_GPIOD LL_IOP_GRP1_PERIPH_GPIOD
109 #define STM32_PERIPH_GPIOE LL_IOP_GRP1_PERIPH_GPIOE
110 #define STM32_PERIPH_GPIOF LL_IOP_GRP1_PERIPH_GPIOF
111 #elif CONFIG_SOC_SERIES_STM32L0X
112 #define STM32_CLOCK_BUS_GPIO STM32_CLOCK_BUS_IOP
113 #define STM32_PERIPH_GPIOA LL_IOP_GRP1_PERIPH_GPIOA
114 #define STM32_PERIPH_GPIOB LL_IOP_GRP1_PERIPH_GPIOB
115 #define STM32_PERIPH_GPIOC LL_IOP_GRP1_PERIPH_GPIOC
116 #define STM32_PERIPH_GPIOD LL_IOP_GRP1_PERIPH_GPIOD
117 #define STM32_PERIPH_GPIOE LL_IOP_GRP1_PERIPH_GPIOE
118 #define STM32_PERIPH_GPIOH LL_IOP_GRP1_PERIPH_GPIOH
119 #elif CONFIG_SOC_SERIES_STM32L1X
120 #define STM32_CLOCK_BUS_GPIO STM32_CLOCK_BUS_AHB1
121 #define STM32_PERIPH_GPIOA LL_AHB1_GRP1_PERIPH_GPIOA
122 #define STM32_PERIPH_GPIOB LL_AHB1_GRP1_PERIPH_GPIOB
123 #define STM32_PERIPH_GPIOC LL_AHB1_GRP1_PERIPH_GPIOC
124 #define STM32_PERIPH_GPIOD LL_AHB1_GRP1_PERIPH_GPIOD
125 #define STM32_PERIPH_GPIOE LL_AHB1_GRP1_PERIPH_GPIOE
126 #define STM32_PERIPH_GPIOF LL_AHB1_GRP1_PERIPH_GPIOF
127 #define STM32_PERIPH_GPIOG LL_AHB1_GRP1_PERIPH_GPIOG
128 #define STM32_PERIPH_GPIOH LL_AHB1_GRP1_PERIPH_GPIOH
129 #elif CONFIG_SOC_SERIES_STM32L4X
130 #define STM32_CLOCK_BUS_GPIO STM32_CLOCK_BUS_AHB2
131 #define STM32_PERIPH_GPIOA LL_AHB2_GRP1_PERIPH_GPIOA
132 #define STM32_PERIPH_GPIOB LL_AHB2_GRP1_PERIPH_GPIOB
133 #define STM32_PERIPH_GPIOC LL_AHB2_GRP1_PERIPH_GPIOC
134 #define STM32_PERIPH_GPIOD LL_AHB2_GRP1_PERIPH_GPIOD
135 #define STM32_PERIPH_GPIOE LL_AHB2_GRP1_PERIPH_GPIOE
136 #define STM32_PERIPH_GPIOF LL_AHB2_GRP1_PERIPH_GPIOF
137 #define STM32_PERIPH_GPIOG LL_AHB2_GRP1_PERIPH_GPIOG
138 #define STM32_PERIPH_GPIOH LL_AHB2_GRP1_PERIPH_GPIOH
139 #define STM32_PERIPH_GPIOI LL_AHB2_GRP1_PERIPH_GPIOI
140 #elif CONFIG_SOC_SERIES_STM32L5X
141 #define STM32_CLOCK_BUS_GPIO STM32_CLOCK_BUS_AHB2
142 #define STM32_PERIPH_GPIOA LL_AHB2_GRP1_PERIPH_GPIOA
143 #define STM32_PERIPH_GPIOB LL_AHB2_GRP1_PERIPH_GPIOB
144 #define STM32_PERIPH_GPIOC LL_AHB2_GRP1_PERIPH_GPIOC
145 #define STM32_PERIPH_GPIOD LL_AHB2_GRP1_PERIPH_GPIOD
146 #define STM32_PERIPH_GPIOE LL_AHB2_GRP1_PERIPH_GPIOE
147 #define STM32_PERIPH_GPIOF LL_AHB2_GRP1_PERIPH_GPIOF
148 #define STM32_PERIPH_GPIOG LL_AHB2_GRP1_PERIPH_GPIOG
149 #define STM32_PERIPH_GPIOH LL_AHB2_GRP1_PERIPH_GPIOH
150 #elif CONFIG_SOC_SERIES_STM32MP1X
151 #define STM32_CLOCK_BUS_GPIO STM32_CLOCK_BUS_AHB4
152 #define STM32_PERIPH_GPIOA LL_AHB4_GRP1_PERIPH_GPIOA
153 #define STM32_PERIPH_GPIOB LL_AHB4_GRP1_PERIPH_GPIOB
154 #define STM32_PERIPH_GPIOC LL_AHB4_GRP1_PERIPH_GPIOC
155 #define STM32_PERIPH_GPIOD LL_AHB4_GRP1_PERIPH_GPIOD
156 #define STM32_PERIPH_GPIOE LL_AHB4_GRP1_PERIPH_GPIOE
157 #define STM32_PERIPH_GPIOF LL_AHB4_GRP1_PERIPH_GPIOF
158 #define STM32_PERIPH_GPIOG LL_AHB4_GRP1_PERIPH_GPIOG
159 #define STM32_PERIPH_GPIOH LL_AHB4_GRP1_PERIPH_GPIOH
160 #define STM32_PERIPH_GPIOI LL_AHB4_GRP1_PERIPH_GPIOI
161 #define STM32_PERIPH_GPIOJ LL_AHB4_GRP1_PERIPH_GPIOJ
162 #define STM32_PERIPH_GPIOK LL_AHB4_GRP1_PERIPH_GPIOK
163 #elif CONFIG_SOC_SERIES_STM32WBX
164 #define STM32_CLOCK_BUS_GPIO STM32_CLOCK_BUS_AHB2
165 #define STM32_PERIPH_GPIOA LL_AHB2_GRP1_PERIPH_GPIOA
166 #define STM32_PERIPH_GPIOB LL_AHB2_GRP1_PERIPH_GPIOB
167 #define STM32_PERIPH_GPIOC LL_AHB2_GRP1_PERIPH_GPIOC
168 #define STM32_PERIPH_GPIOD LL_AHB2_GRP1_PERIPH_GPIOD
169 #define STM32_PERIPH_GPIOE LL_AHB2_GRP1_PERIPH_GPIOE
170 #define STM32_PERIPH_GPIOH LL_AHB2_GRP1_PERIPH_GPIOH
171 #elif CONFIG_SOC_SERIES_STM32G4X
172 #define STM32_CLOCK_BUS_GPIO STM32_CLOCK_BUS_AHB2
173 #define STM32_PERIPH_GPIOA LL_AHB2_GRP1_PERIPH_GPIOA
174 #define STM32_PERIPH_GPIOB LL_AHB2_GRP1_PERIPH_GPIOB
175 #define STM32_PERIPH_GPIOC LL_AHB2_GRP1_PERIPH_GPIOC
176 #define STM32_PERIPH_GPIOD LL_AHB2_GRP1_PERIPH_GPIOD
177 #define STM32_PERIPH_GPIOE LL_AHB2_GRP1_PERIPH_GPIOE
178 #define STM32_PERIPH_GPIOF LL_AHB2_GRP1_PERIPH_GPIOF
179 #define STM32_PERIPH_GPIOG LL_AHB2_GRP1_PERIPH_GPIOG
180 #elif CONFIG_SOC_SERIES_STM32WLX
181 #define STM32_CLOCK_BUS_GPIO STM32_CLOCK_BUS_AHB2
182 #define STM32_PERIPH_GPIOA LL_AHB2_GRP1_PERIPH_GPIOA
183 #define STM32_PERIPH_GPIOB LL_AHB2_GRP1_PERIPH_GPIOB
184 #define STM32_PERIPH_GPIOC LL_AHB2_GRP1_PERIPH_GPIOC
185 #define STM32_PERIPH_GPIOH LL_AHB2_GRP1_PERIPH_GPIOH
186 #endif /* CONFIG_SOC_SERIES_* */
187 
188 #ifdef CONFIG_SOC_SERIES_STM32F1X
189 #define STM32_PINCFG_MODE_OUTPUT        (STM32_MODE_OUTPUT     \
190 					 | STM32_CNF_GP_OUTPUT \
191 					 | STM32_CNF_PUSH_PULL)
192 #define STM32_PINCFG_MODE_INPUT         STM32_MODE_INPUT
193 #define STM32_PINCFG_MODE_ANALOG        (STM32_MODE_INPUT	\
194 					 | STM32_CNF_IN_ANALOG)
195 #define STM32_PINCFG_PUSH_PULL          STM32_CNF_PUSH_PULL
196 #define STM32_PINCFG_OPEN_DRAIN         STM32_CNF_OPEN_DRAIN
197 #define STM32_PINCFG_PULL_UP            (STM32_CNF_IN_PUPD | STM32_PUPD_PULL_UP)
198 #define STM32_PINCFG_PULL_DOWN          (STM32_CNF_IN_PUPD | \
199 					STM32_PUPD_PULL_DOWN)
200 #define STM32_PINCFG_FLOATING           (STM32_CNF_IN_FLOAT | \
201 					STM32_PUPD_NO_PULL)
202 #else
203 #define STM32_PINCFG_MODE_OUTPUT        STM32_MODER_OUTPUT_MODE
204 #define STM32_PINCFG_MODE_INPUT         STM32_MODER_INPUT_MODE
205 #define STM32_PINCFG_MODE_ANALOG        STM32_MODER_ANALOG_MODE
206 #define STM32_PINCFG_PUSH_PULL          STM32_OTYPER_PUSH_PULL
207 #define STM32_PINCFG_OPEN_DRAIN         STM32_OTYPER_OPEN_DRAIN
208 #define STM32_PINCFG_PULL_UP            STM32_PUPDR_PULL_UP
209 #define STM32_PINCFG_PULL_DOWN          STM32_PUPDR_PULL_DOWN
210 #define STM32_PINCFG_FLOATING           STM32_PUPDR_NO_PULL
211 #endif /* CONFIG_SOC_SERIES_STM32F1X */
212 
213 #if defined(CONFIG_GPIO_GET_CONFIG) && !defined(CONFIG_SOC_SERIES_STM32F1X)
214 /**
215  * @brief structure of a GPIO pin (stm32 LL values) use to get the configuration
216  */
217 struct gpio_stm32_pin {
218 	unsigned int type; /* LL_GPIO_OUTPUT_PUSHPULL or LL_GPIO_OUTPUT_OPENDRAIN */
219 	unsigned int pupd; /* LL_GPIO_PULL_NO or LL_GPIO_PULL_UP or LL_GPIO_PULL_DOWN */
220 	unsigned int mode; /* LL_GPIO_MODE_INPUT or LL_GPIO_MODE_OUTPUT or other */
221 	unsigned int out_state; /* 1 (high level) or 0 (low level) */
222 };
223 #endif /* CONFIG_GPIO_GET_CONFIG */
224 
225 /**
226  * @brief configuration of GPIO device
227  */
228 struct gpio_stm32_config {
229 	/* gpio_driver_config needs to be first */
230 	struct gpio_driver_config common;
231 	/* port base address */
232 	uint32_t *base;
233 	/* IO port */
234 	int port;
235 	struct stm32_pclken pclken;
236 };
237 
238 /**
239  * @brief driver data
240  */
241 struct gpio_stm32_data {
242 	/* gpio_driver_data needs to be first */
243 	struct gpio_driver_data common;
244 	/* device's owner of this data */
245 	const struct device *dev;
246 	/* user ISR cb */
247 	sys_slist_t cb;
248 };
249 
250 /**
251  * @brief helper for configuration of GPIO pin
252  *
253  * @param dev GPIO port device pointer
254  * @param pin IO pin
255  * @param conf GPIO mode
256  * @param func Pin function
257  *
258  * @return 0 on success, negative errno code on failure
259  */
260 int gpio_stm32_configure(const struct device *dev, int pin, int conf, int func);
261 
262 #endif /* ZEPHYR_DRIVERS_GPIO_GPIO_STM32_H_ */
263