2016-03-03 15:33:15 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Open-RnD Sp. z o.o.
|
2021-02-17 11:17:01 +01:00
|
|
|
* Copyright (c) 2021 Linaro Limited
|
2016-03-03 15:33:15 +01:00
|
|
|
*
|
2017-01-18 17:01:01 -08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-03-03 15:33:15 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief
|
|
|
|
*
|
2021-02-17 11:17:01 +01:00
|
|
|
* A common driver for STM32 pinmux.
|
2016-03-03 15:33:15 +01:00
|
|
|
*/
|
|
|
|
|
2016-04-26 18:20:17 -03:00
|
|
|
#include <errno.h>
|
|
|
|
|
2016-12-04 14:59:37 -06:00
|
|
|
#include <kernel.h>
|
2016-03-03 15:33:15 +01:00
|
|
|
#include <device.h>
|
|
|
|
#include <soc.h>
|
2020-11-20 16:41:20 +01:00
|
|
|
#include <stm32_ll_bus.h>
|
|
|
|
#include <stm32_ll_gpio.h>
|
2021-10-04 17:04:09 +02:00
|
|
|
#include <stm32_ll_system.h>
|
2019-06-25 15:53:57 -04:00
|
|
|
#include <drivers/pinmux.h>
|
2016-03-21 17:14:15 -03:00
|
|
|
#include <gpio/gpio_stm32.h>
|
2020-01-25 05:34:53 -06:00
|
|
|
#include <drivers/clock_control/stm32_clock_control.h>
|
2021-06-10 15:55:31 +02:00
|
|
|
#include <pinmux/pinmux_stm32.h>
|
2016-03-03 15:33:15 +01:00
|
|
|
|
2021-02-17 11:13:09 +01:00
|
|
|
const struct device * const gpio_ports[STM32_PORTS_MAX] = {
|
2021-09-06 15:46:57 +02:00
|
|
|
DEVICE_DT_GET_OR_NULL(DT_NODELABEL(gpioa)),
|
|
|
|
DEVICE_DT_GET_OR_NULL(DT_NODELABEL(gpiob)),
|
|
|
|
DEVICE_DT_GET_OR_NULL(DT_NODELABEL(gpioc)),
|
|
|
|
DEVICE_DT_GET_OR_NULL(DT_NODELABEL(gpiod)),
|
|
|
|
DEVICE_DT_GET_OR_NULL(DT_NODELABEL(gpioe)),
|
|
|
|
DEVICE_DT_GET_OR_NULL(DT_NODELABEL(gpiof)),
|
|
|
|
DEVICE_DT_GET_OR_NULL(DT_NODELABEL(gpiog)),
|
|
|
|
DEVICE_DT_GET_OR_NULL(DT_NODELABEL(gpioh)),
|
|
|
|
DEVICE_DT_GET_OR_NULL(DT_NODELABEL(gpioi)),
|
|
|
|
DEVICE_DT_GET_OR_NULL(DT_NODELABEL(gpioj)),
|
|
|
|
DEVICE_DT_GET_OR_NULL(DT_NODELABEL(gpiok)),
|
2017-01-23 17:54:57 +01:00
|
|
|
};
|
2016-08-10 18:28:32 -03:00
|
|
|
|
2021-10-04 17:04:09 +02:00
|
|
|
#if DT_NODE_HAS_PROP(DT_NODELABEL(pinctrl), remap_pa11)
|
|
|
|
#define REMAP_PA11 DT_PROP(DT_NODELABEL(pinctrl), remap_pa11)
|
|
|
|
#endif
|
|
|
|
#if DT_NODE_HAS_PROP(DT_NODELABEL(pinctrl), remap_pa12)
|
|
|
|
#define REMAP_PA12 DT_PROP(DT_NODELABEL(pinctrl), remap_pa12)
|
|
|
|
#endif
|
|
|
|
#if DT_NODE_HAS_PROP(DT_NODELABEL(pinctrl), remap_pa11_pa12)
|
|
|
|
#define REMAP_PA11_PA12 DT_PROP(DT_NODELABEL(pinctrl), remap_pa11_pa12)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if REMAP_PA11 || REMAP_PA12 || REMAP_PA11_PA12
|
|
|
|
|
|
|
|
int stm32_pinmux_init_remap(const struct device *dev)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
#if REMAP_PA11 || REMAP_PA12
|
|
|
|
|
|
|
|
#if !defined(CONFIG_SOC_SERIES_STM32G0X)
|
|
|
|
#error "Pin remap property available only on STM32G0 SoC series"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
|
|
|
|
#if REMAP_PA11
|
|
|
|
LL_SYSCFG_EnablePinRemap(LL_SYSCFG_PIN_RMP_PA11);
|
|
|
|
#endif
|
|
|
|
#if REMAP_PA12
|
|
|
|
LL_SYSCFG_EnablePinRemap(LL_SYSCFG_PIN_RMP_PA12);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#elif REMAP_PA11_PA12
|
|
|
|
|
|
|
|
#if !defined(SYSCFG_CFGR1_PA11_PA12_RMP)
|
|
|
|
#error "Pin remap property available only on STM32F070x SoC series"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_SYSCFG);
|
|
|
|
LL_SYSCFG_EnablePinRemap();
|
|
|
|
|
|
|
|
#endif /* (REMAP_PA11 || REMAP_PA12) || REMAP_PA11_PA12 */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
SYS_INIT(stm32_pinmux_init_remap, PRE_KERNEL_1,
|
|
|
|
CONFIG_PINMUX_STM32_REMAP_INIT_PRIORITY);
|
|
|
|
|
|
|
|
#endif /* REMAP_PA11 || REMAP_PA12 || REMAP_PA11_PA12 */
|
|
|
|
|
|
|
|
|
2020-06-04 15:48:48 +02:00
|
|
|
static int stm32_pin_configure(uint32_t pin, uint32_t func, uint32_t altf)
|
2016-03-21 17:14:15 -03:00
|
|
|
{
|
2021-07-15 17:19:27 +02:00
|
|
|
const struct device *port_device;
|
2016-03-21 17:14:15 -03:00
|
|
|
|
2021-07-15 17:19:27 +02:00
|
|
|
if (STM32_PORT(pin) >= STM32_PORTS_MAX) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
port_device = gpio_ports[STM32_PORT(pin)];
|
|
|
|
|
|
|
|
if ((port_device == NULL) || (!device_is_ready(port_device))) {
|
2021-02-17 11:17:01 +01:00
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2021-11-25 12:04:17 +01:00
|
|
|
return gpio_stm32_configure(port_device, STM32_PIN(pin), func, altf);
|
2016-03-21 17:14:15 -03:00
|
|
|
}
|
|
|
|
|
2020-06-05 10:55:20 +02:00
|
|
|
/**
|
|
|
|
* @brief helper for converting dt stm32 pinctrl format to existing pin config
|
|
|
|
* format
|
|
|
|
*
|
|
|
|
* @param *pinctrl pointer to soc_gpio_pinctrl list
|
|
|
|
* @param list_size list size
|
2020-10-16 17:20:00 +02:00
|
|
|
* @param base device base register value
|
|
|
|
*
|
|
|
|
* @return 0 on success, -EINVAL otherwise
|
2020-06-05 10:55:20 +02:00
|
|
|
*/
|
2020-10-16 17:20:00 +02:00
|
|
|
int stm32_dt_pinctrl_configure(const struct soc_gpio_pinctrl *pinctrl,
|
|
|
|
size_t list_size, uint32_t base)
|
2020-06-05 10:55:20 +02:00
|
|
|
{
|
|
|
|
uint32_t pin, mux;
|
|
|
|
uint32_t func = 0;
|
2021-02-17 11:13:09 +01:00
|
|
|
int ret = 0;
|
2020-06-05 10:55:20 +02:00
|
|
|
|
2020-10-16 17:20:00 +02:00
|
|
|
if (!list_size) {
|
|
|
|
/* Empty pinctrl. Exit */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32f1_pinctrl)
|
2021-11-19 15:34:39 +01:00
|
|
|
if (stm32_dt_pinctrl_remap(pinctrl, list_size)) {
|
2020-10-16 17:20:00 +02:00
|
|
|
/* Wrong remap config. Exit */
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
ARG_UNUSED(base);
|
|
|
|
#endif /* DT_HAS_COMPAT_STATUS_OKAY(st_stm32f1_pinctrl) */
|
|
|
|
|
2020-06-05 10:55:20 +02:00
|
|
|
for (int i = 0; i < list_size; i++) {
|
|
|
|
mux = pinctrl[i].pinmux;
|
|
|
|
|
2020-08-12 13:26:06 +02:00
|
|
|
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32f1_pinctrl)
|
|
|
|
uint32_t pupd;
|
|
|
|
|
|
|
|
if (STM32_DT_PINMUX_FUNC(mux) == ALTERNATE) {
|
|
|
|
func = pinctrl[i].pincfg | STM32_MODE_OUTPUT |
|
|
|
|
STM32_CNF_ALT_FUNC;
|
|
|
|
} else if (STM32_DT_PINMUX_FUNC(mux) == ANALOG) {
|
|
|
|
func = pinctrl[i].pincfg | STM32_MODE_INPUT |
|
|
|
|
STM32_CNF_IN_ANALOG;
|
|
|
|
} else if (STM32_DT_PINMUX_FUNC(mux) == GPIO_IN) {
|
|
|
|
func = pinctrl[i].pincfg | STM32_MODE_INPUT;
|
|
|
|
pupd = func & (STM32_PUPD_MASK << STM32_PUPD_SHIFT);
|
|
|
|
if (pupd == STM32_PUPD_NO_PULL) {
|
|
|
|
func = func | STM32_CNF_IN_FLOAT;
|
|
|
|
} else {
|
|
|
|
func = func | STM32_CNF_IN_PUPD;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Not supported */
|
|
|
|
__ASSERT_NO_MSG(STM32_DT_PINMUX_FUNC(mux));
|
|
|
|
}
|
|
|
|
#else
|
2021-07-09 18:05:16 +02:00
|
|
|
if (STM32_DT_PINMUX_FUNC(mux) < STM32_ANALOG) {
|
2020-06-05 10:55:20 +02:00
|
|
|
func = pinctrl[i].pincfg | STM32_MODER_ALT_MODE;
|
2021-07-09 18:05:16 +02:00
|
|
|
} else if (STM32_DT_PINMUX_FUNC(mux) == STM32_ANALOG) {
|
2020-06-05 10:55:20 +02:00
|
|
|
func = STM32_MODER_ANALOG_MODE;
|
|
|
|
} else {
|
|
|
|
/* Not supported */
|
|
|
|
__ASSERT_NO_MSG(STM32_DT_PINMUX_FUNC(mux));
|
|
|
|
}
|
2020-08-12 13:26:06 +02:00
|
|
|
#endif /* DT_HAS_COMPAT_STATUS_OKAY(st_stm32f1_pinctrl) */
|
2020-06-05 10:55:20 +02:00
|
|
|
|
|
|
|
pin = STM32PIN(STM32_DT_PINMUX_PORT(mux),
|
|
|
|
STM32_DT_PINMUX_LINE(mux));
|
2021-03-25 17:29:30 +01:00
|
|
|
|
2021-07-15 17:19:27 +02:00
|
|
|
ret = stm32_pin_configure(pin, func, STM32_DT_PINMUX_FUNC(mux));
|
2021-02-17 11:13:09 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2020-06-05 10:55:20 +02:00
|
|
|
}
|
2020-08-12 13:26:06 +02:00
|
|
|
|
2021-07-15 17:19:27 +02:00
|
|
|
return 0;
|
2020-08-12 13:26:06 +02:00
|
|
|
}
|
|
|
|
|
2020-10-16 17:20:00 +02:00
|
|
|
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32f1_pinctrl)
|
2020-08-12 13:26:06 +02:00
|
|
|
/**
|
2020-10-16 17:20:00 +02:00
|
|
|
* @brief Helper function to check and apply provided pinctrl remap
|
|
|
|
* configuration
|
|
|
|
*
|
|
|
|
* Check operation verifies that pin remapping configuration is the same on all
|
|
|
|
* pins. If configuration is valid AFIO clock is enabled and remap is applied
|
2020-08-12 13:26:06 +02:00
|
|
|
*
|
|
|
|
* @param *pinctrl pointer to soc_gpio_pinctrl list
|
|
|
|
* @param list_size list size
|
|
|
|
*
|
2020-10-16 17:20:00 +02:00
|
|
|
* @return 0 on success, -EINVAL otherwise
|
2020-08-12 13:26:06 +02:00
|
|
|
*/
|
2020-10-16 17:20:00 +02:00
|
|
|
int stm32_dt_pinctrl_remap(const struct soc_gpio_pinctrl *pinctrl,
|
2021-11-19 15:34:39 +01:00
|
|
|
size_t list_size)
|
2020-08-12 13:26:06 +02:00
|
|
|
{
|
2021-11-19 15:34:39 +01:00
|
|
|
uint8_t pos;
|
|
|
|
uint32_t reg_val;
|
|
|
|
volatile uint32_t *reg;
|
|
|
|
uint16_t remap;
|
2020-08-12 13:26:06 +02:00
|
|
|
|
2021-10-13 17:08:11 +02:00
|
|
|
remap = (uint16_t)STM32_DT_PINMUX_REMAP(pinctrl[0].pinmux);
|
2020-08-12 13:26:06 +02:00
|
|
|
|
2021-11-19 15:34:39 +01:00
|
|
|
/* not remappable */
|
|
|
|
if (remap == NO_REMAP) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-06 14:37:05 +02:00
|
|
|
for (size_t i = 1U; i < list_size; i++) {
|
|
|
|
if (STM32_DT_PINMUX_REMAP(pinctrl[i].pinmux) != remap) {
|
2020-10-16 17:20:00 +02:00
|
|
|
return -EINVAL;
|
2020-08-12 13:26:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 17:20:00 +02:00
|
|
|
/* A valid remapping configuration is available */
|
2020-10-13 16:44:13 +02:00
|
|
|
/* Apply remapping before proceeding with pin configuration */
|
|
|
|
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_AFIO);
|
|
|
|
|
2021-11-19 15:34:39 +01:00
|
|
|
if (STM32_REMAP_REG_GET(remap) == 0U) {
|
|
|
|
reg = &AFIO->MAPR;
|
|
|
|
} else {
|
|
|
|
reg = &AFIO->MAPR2;
|
2020-10-13 16:44:13 +02:00
|
|
|
}
|
|
|
|
|
2021-11-19 15:34:39 +01:00
|
|
|
pos = STM32_REMAP_SHIFT_GET(remap);
|
|
|
|
|
|
|
|
reg_val = *reg;
|
|
|
|
reg_val &= ~(STM32_REMAP_MASK_GET(remap) << pos);
|
|
|
|
reg_val |= STM32_REMAP_VAL_GET(remap) << pos;
|
|
|
|
*reg = reg_val;
|
|
|
|
|
2020-10-16 17:20:00 +02:00
|
|
|
return 0;
|
2020-10-16 14:28:48 +02:00
|
|
|
}
|
2021-11-19 15:34:39 +01:00
|
|
|
|
2020-10-16 17:20:00 +02:00
|
|
|
#endif /* DT_HAS_COMPAT_STATUS_OKAY(st_stm32f1_pinctrl) */
|