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