1 /*
2 * Copyright (c) 2017, Christian Taedcke
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #define DT_DRV_COMPAT silabs_gecko_gpio_port
8
9 #include <errno.h>
10 #include <zephyr/drivers/gpio.h>
11 #include <zephyr/irq.h>
12 #include <soc.h>
13 #include <em_gpio.h>
14 #ifdef CONFIG_SOC_GECKO_DEV_INIT
15 #include <em_cmu.h>
16 #endif
17
18 #include <zephyr/drivers/gpio/gpio_utils.h>
19
20 #if CONFIG_GPIO_GECKO_COMMON_INIT_PRIORITY >= CONFIG_GPIO_INIT_PRIORITY
21 #error CONFIG_GPIO_GECKO_COMMON_INIT_PRIORITY must be less than \
22 CONFIG_GPIO_INIT_PRIORITY.
23 #endif
24
25 #if DT_NODE_HAS_PROP(id, peripheral_id)
26 #define GET_GECKO_GPIO_INDEX(id) DT_INST_PROP(id, peripheral_id)
27 #else
28 #if defined(CONFIG_SOC_SERIES_EFR32BG22) || \
29 defined(CONFIG_SOC_SERIES_EFR32BG27) || \
30 defined(CONFIG_SOC_SERIES_EFR32MG21) || \
31 defined(CONFIG_SOC_SERIES_EFR32MG24)
32 #define GECKO_GPIO_PORT_ADDR_SPACE_SIZE sizeof(GPIO_PORT_TypeDef)
33 #else
34 #define GECKO_GPIO_PORT_ADDR_SPACE_SIZE sizeof(GPIO_P_TypeDef)
35 #endif
36 /* Assumption for calculating gpio index:
37 * 1. Address space of the first GPIO port is the address space for GPIO port A
38 */
39 #define GET_GECKO_GPIO_INDEX(id) (DT_INST_REG_ADDR(id) - DT_REG_ADDR(DT_NODELABEL(gpioa))) \
40 / GECKO_GPIO_PORT_ADDR_SPACE_SIZE
41 #endif /* DT_NODE_HAS_PROP(id, peripheral_id) */
42
43 /*
44 * Macros to set the GPIO MODE registers
45 *
46 * See https://www.silabs.com/documents/public/reference-manuals/EFM32WG-RM.pdf
47 * pages 972 and 982.
48 */
49 /**
50 * @brief Create the value to set the GPIO MODEL register
51 * @param[in] pin The index of the pin. Valid values are 0..7.
52 * @param[in] mode The mode that should be set.
53 * @return The value that can be set into the GPIO MODEL register.
54 */
55 #define GECKO_GPIO_MODEL(pin, mode) (mode << (pin * 4))
56
57 /**
58 * @brief Create the value to set the GPIO MODEH register
59 * @param[in] pin The index of the pin. Valid values are 8..15.
60 * @param[in] mode The mode that should be set.
61 * @return The value that can be set into the GPIO MODEH register.
62 */
63 #define GECKO_GPIO_MODEH(pin, mode) (mode << ((pin - 8) * 4))
64
65
66 #define member_size(type, member) sizeof(((type *)0)->member)
67 #define NUMBER_OF_PORTS (member_size(GPIO_TypeDef, P) / \
68 member_size(GPIO_TypeDef, P[0]))
69
70 struct gpio_gecko_common_config {
71 };
72
73 struct gpio_gecko_common_data {
74 /* a list of all ports */
75 const struct device *ports[NUMBER_OF_PORTS];
76 size_t count;
77 };
78
79 struct gpio_gecko_config {
80 /* gpio_driver_config needs to be first */
81 struct gpio_driver_config common;
82 GPIO_Port_TypeDef gpio_index;
83 };
84
85 struct gpio_gecko_data {
86 /* gpio_driver_data needs to be first */
87 struct gpio_driver_data common;
88 /* port ISR callback routine address */
89 sys_slist_t callbacks;
90 /* mask of pins on which interrupt is enabled */
91 uint32_t int_enabled_mask;
92 };
93
gpio_gecko_add_port(struct gpio_gecko_common_data * data,const struct device * dev)94 static inline void gpio_gecko_add_port(struct gpio_gecko_common_data *data,
95 const struct device *dev)
96 {
97 __ASSERT(dev, "No port device!");
98 data->ports[data->count++] = dev;
99 }
100
gpio_gecko_configure(const struct device * dev,gpio_pin_t pin,gpio_flags_t flags)101 static int gpio_gecko_configure(const struct device *dev,
102 gpio_pin_t pin,
103 gpio_flags_t flags)
104 {
105 const struct gpio_gecko_config *config = dev->config;
106 GPIO_Port_TypeDef gpio_index = config->gpio_index;
107 GPIO_Mode_TypeDef mode;
108 unsigned int out = 0U;
109
110 if (flags & GPIO_OUTPUT) {
111 /* Following modes enable both output and input */
112 if (flags & GPIO_SINGLE_ENDED) {
113 if (flags & GPIO_LINE_OPEN_DRAIN) {
114 mode = gpioModeWiredAnd;
115 } else {
116 mode = gpioModeWiredOr;
117 }
118 } else {
119 mode = gpioModePushPull;
120 }
121 if (flags & GPIO_OUTPUT_INIT_HIGH) {
122 out = 1U;
123 } else if (flags & GPIO_OUTPUT_INIT_LOW) {
124 out = 0U;
125 } else {
126 out = GPIO_PinOutGet(gpio_index, pin);
127 }
128 } else if (flags & GPIO_INPUT) {
129 if (flags & GPIO_PULL_UP) {
130 mode = gpioModeInputPull;
131 out = 1U; /* pull-up*/
132 } else if (flags & GPIO_PULL_DOWN) {
133 mode = gpioModeInputPull;
134 /* out = 0 means pull-down*/
135 } else {
136 mode = gpioModeInput;
137 }
138 } else {
139 /* Neither input nor output mode is selected */
140 mode = gpioModeDisabled;
141 }
142 /* The flags contain options that require touching registers in the
143 * GPIO module and the corresponding PORT module.
144 *
145 * Start with the GPIO module and set up the pin direction register.
146 * 0 - pin is input, 1 - pin is output
147 */
148
149 GPIO_PinModeSet(gpio_index, pin, mode, out);
150
151 return 0;
152 }
153
154 #ifdef CONFIG_GPIO_GET_CONFIG
gpio_gecko_get_config(const struct device * dev,gpio_pin_t pin,gpio_flags_t * out_flags)155 static int gpio_gecko_get_config(const struct device *dev,
156 gpio_pin_t pin,
157 gpio_flags_t *out_flags)
158 {
159 const struct gpio_gecko_config *config = dev->config;
160 GPIO_Port_TypeDef gpio_index = config->gpio_index;
161 GPIO_Mode_TypeDef mode;
162 unsigned int out;
163 gpio_flags_t flags = 0;
164
165 mode = GPIO_PinModeGet(gpio_index, pin);
166 out = GPIO_PinOutGet(gpio_index, pin);
167
168 switch (mode) {
169 case gpioModeWiredAnd:
170 flags = GPIO_OUTPUT | GPIO_OPEN_DRAIN;
171
172 if (out) {
173 flags |= GPIO_OUTPUT_HIGH;
174 } else {
175 flags |= GPIO_OUTPUT_LOW;
176 }
177
178 break;
179 case gpioModeWiredOr:
180 flags = GPIO_OUTPUT | GPIO_OPEN_SOURCE;
181
182 if (out) {
183 flags |= GPIO_OUTPUT_HIGH;
184 } else {
185 flags |= GPIO_OUTPUT_LOW;
186 }
187
188 break;
189 case gpioModePushPull:
190 flags = GPIO_OUTPUT | GPIO_PUSH_PULL;
191
192 if (out) {
193 flags |= GPIO_OUTPUT_HIGH;
194 } else {
195 flags |= GPIO_OUTPUT_LOW;
196 }
197
198 break;
199 case gpioModeInputPull:
200 flags = GPIO_INPUT;
201
202 if (out) {
203 flags |= GPIO_PULL_UP;
204 } else {
205 flags |= GPIO_PULL_DOWN;
206 }
207
208 break;
209 case gpioModeInput:
210 flags = GPIO_INPUT;
211 break;
212 case gpioModeDisabled:
213 flags = GPIO_DISCONNECTED;
214 break;
215 default:
216 break;
217 }
218
219 *out_flags = flags;
220
221 return 0;
222 }
223 #endif
224
gpio_gecko_port_get_raw(const struct device * dev,uint32_t * value)225 static int gpio_gecko_port_get_raw(const struct device *dev, uint32_t *value)
226 {
227 const struct gpio_gecko_config *config = dev->config;
228 GPIO_Port_TypeDef gpio_index = config->gpio_index;
229
230 *value = GPIO_PortInGet(gpio_index);
231
232 return 0;
233 }
234
gpio_gecko_port_set_masked_raw(const struct device * dev,uint32_t mask,uint32_t value)235 static int gpio_gecko_port_set_masked_raw(const struct device *dev,
236 uint32_t mask,
237 uint32_t value)
238 {
239 const struct gpio_gecko_config *config = dev->config;
240 GPIO_Port_TypeDef gpio_index = config->gpio_index;
241
242 GPIO_PortOutSetVal(gpio_index, value, mask);
243
244 return 0;
245 }
246
gpio_gecko_port_set_bits_raw(const struct device * dev,uint32_t mask)247 static int gpio_gecko_port_set_bits_raw(const struct device *dev,
248 uint32_t mask)
249 {
250 const struct gpio_gecko_config *config = dev->config;
251 GPIO_Port_TypeDef gpio_index = config->gpio_index;
252
253 GPIO_PortOutSet(gpio_index, mask);
254
255 return 0;
256 }
257
gpio_gecko_port_clear_bits_raw(const struct device * dev,uint32_t mask)258 static int gpio_gecko_port_clear_bits_raw(const struct device *dev,
259 uint32_t mask)
260 {
261 const struct gpio_gecko_config *config = dev->config;
262 GPIO_Port_TypeDef gpio_index = config->gpio_index;
263
264 GPIO_PortOutClear(gpio_index, mask);
265
266 return 0;
267 }
268
gpio_gecko_port_toggle_bits(const struct device * dev,uint32_t mask)269 static int gpio_gecko_port_toggle_bits(const struct device *dev,
270 uint32_t mask)
271 {
272 const struct gpio_gecko_config *config = dev->config;
273 GPIO_Port_TypeDef gpio_index = config->gpio_index;
274
275 GPIO_PortOutToggle(gpio_index, mask);
276
277 return 0;
278 }
279
gpio_gecko_pin_interrupt_configure(const struct device * dev,gpio_pin_t pin,enum gpio_int_mode mode,enum gpio_int_trig trig)280 static int gpio_gecko_pin_interrupt_configure(const struct device *dev,
281 gpio_pin_t pin,
282 enum gpio_int_mode mode,
283 enum gpio_int_trig trig)
284 {
285 const struct gpio_gecko_config *config = dev->config;
286 struct gpio_gecko_data *data = dev->data;
287
288 /* Interrupt on static level is not supported by the hardware */
289 if (mode == GPIO_INT_MODE_LEVEL) {
290 return -ENOTSUP;
291 }
292
293 if (mode == GPIO_INT_MODE_DISABLED) {
294 GPIO_IntDisable(BIT(pin));
295 } else {
296 /* Interrupt line is already in use */
297 if ((GPIO->IEN & BIT(pin)) != 0) {
298 /* TODO: Return an error only if request is done for
299 * a pin from a different port.
300 */
301 return -EBUSY;
302 }
303
304 bool rising_edge = true;
305 bool falling_edge = true;
306
307 if (trig == GPIO_INT_TRIG_LOW) {
308 rising_edge = false;
309 falling_edge = true;
310 } else if (trig == GPIO_INT_TRIG_HIGH) {
311 rising_edge = true;
312 falling_edge = false;
313 } /* default is GPIO_INT_TRIG_BOTH */
314
315 GPIO_ExtIntConfig(config->gpio_index, pin, pin,
316 rising_edge, falling_edge, true);
317 }
318
319 WRITE_BIT(data->int_enabled_mask, pin, mode != GPIO_INT_DISABLE);
320
321 return 0;
322 }
323
gpio_gecko_manage_callback(const struct device * dev,struct gpio_callback * callback,bool set)324 static int gpio_gecko_manage_callback(const struct device *dev,
325 struct gpio_callback *callback, bool set)
326 {
327 struct gpio_gecko_data *data = dev->data;
328
329 return gpio_manage_callback(&data->callbacks, callback, set);
330 }
331
332 /**
333 * Handler for both odd and even pin interrupts
334 */
gpio_gecko_common_isr(const struct device * dev)335 static void gpio_gecko_common_isr(const struct device *dev)
336 {
337 struct gpio_gecko_common_data *data = dev->data;
338 uint32_t enabled_int, int_status;
339 const struct device *port_dev;
340 struct gpio_gecko_data *port_data;
341
342 int_status = GPIO->IF;
343
344 for (unsigned int i = 0; int_status && (i < data->count); i++) {
345 port_dev = data->ports[i];
346 port_data = port_dev->data;
347 enabled_int = int_status & port_data->int_enabled_mask;
348 if (enabled_int != 0) {
349 int_status &= ~enabled_int;
350 #if defined(_SILICON_LABS_32B_SERIES_2)
351 GPIO->IF_CLR = enabled_int;
352 #else
353 GPIO->IFC = enabled_int;
354 #endif
355 gpio_fire_callbacks(&port_data->callbacks, port_dev,
356 enabled_int);
357 }
358 }
359 }
360
361 static const struct gpio_driver_api gpio_gecko_driver_api = {
362 .pin_configure = gpio_gecko_configure,
363 #ifdef CONFIG_GPIO_GET_CONFIG
364 .pin_get_config = gpio_gecko_get_config,
365 #endif
366 .port_get_raw = gpio_gecko_port_get_raw,
367 .port_set_masked_raw = gpio_gecko_port_set_masked_raw,
368 .port_set_bits_raw = gpio_gecko_port_set_bits_raw,
369 .port_clear_bits_raw = gpio_gecko_port_clear_bits_raw,
370 .port_toggle_bits = gpio_gecko_port_toggle_bits,
371 .pin_interrupt_configure = gpio_gecko_pin_interrupt_configure,
372 .manage_callback = gpio_gecko_manage_callback,
373 };
374
375 static const struct gpio_driver_api gpio_gecko_common_driver_api = {
376 .manage_callback = gpio_gecko_manage_callback,
377 };
378
379 static int gpio_gecko_common_init(const struct device *dev);
380
381 static const struct gpio_gecko_common_config gpio_gecko_common_config = {
382 };
383
384 static struct gpio_gecko_common_data gpio_gecko_common_data;
385
386 DEVICE_DT_DEFINE(DT_INST(0, silabs_gecko_gpio),
387 gpio_gecko_common_init,
388 NULL,
389 &gpio_gecko_common_data, &gpio_gecko_common_config,
390 PRE_KERNEL_1, CONFIG_GPIO_GECKO_COMMON_INIT_PRIORITY,
391 &gpio_gecko_common_driver_api);
392
gpio_gecko_common_init(const struct device * dev)393 static int gpio_gecko_common_init(const struct device *dev)
394 {
395 #ifdef CONFIG_SOC_GECKO_DEV_INIT
396 CMU_ClockEnable(cmuClock_GPIO, true);
397 #endif
398 gpio_gecko_common_data.count = 0;
399 IRQ_CONNECT(GPIO_EVEN_IRQn,
400 DT_IRQ_BY_NAME(DT_INST(0, silabs_gecko_gpio), gpio_even, priority),
401 gpio_gecko_common_isr,
402 DEVICE_DT_GET(DT_INST(0, silabs_gecko_gpio)), 0);
403
404 IRQ_CONNECT(GPIO_ODD_IRQn,
405 DT_IRQ_BY_NAME(DT_INST(0, silabs_gecko_gpio), gpio_odd, priority),
406 gpio_gecko_common_isr,
407 DEVICE_DT_GET(DT_INST(0, silabs_gecko_gpio)), 0);
408
409 irq_enable(GPIO_EVEN_IRQn);
410 irq_enable(GPIO_ODD_IRQn);
411
412 return 0;
413 }
414
415 #define GPIO_PORT_INIT(idx) \
416 static int gpio_gecko_port##idx##_init(const struct device *dev); \
417 \
418 static const struct gpio_gecko_config gpio_gecko_port##idx##_config = { \
419 .common = { \
420 .port_pin_mask = (gpio_port_pins_t)(-1), \
421 }, \
422 .gpio_index = GET_GECKO_GPIO_INDEX(idx), \
423 }; \
424 \
425 static struct gpio_gecko_data gpio_gecko_port##idx##_data; \
426 \
427 DEVICE_DT_INST_DEFINE(idx, \
428 gpio_gecko_port##idx##_init, \
429 NULL, \
430 &gpio_gecko_port##idx##_data, \
431 &gpio_gecko_port##idx##_config, \
432 POST_KERNEL, CONFIG_GPIO_INIT_PRIORITY, \
433 &gpio_gecko_driver_api); \
434 \
435 static int gpio_gecko_port##idx##_init(const struct device *dev) \
436 { \
437 gpio_gecko_add_port(&gpio_gecko_common_data, dev); \
438 return 0; \
439 }
440
441 DT_INST_FOREACH_STATUS_OKAY(GPIO_PORT_INIT)
442