summaryrefslogtreecommitdiff
path: root/arch/arm/soc/st_stm32/stm32l4/soc_pinmux.c
blob: 81a6106241286f0d50e225948acf3730f9391f46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright (c) 2016 Open-RnD Sp. z o.o.
 * Copyright (c) 2016 BayLibre, SAS
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <errno.h>

#include "soc.h"
#include "soc_pinmux.h"
#include <device.h>
#include <misc/util.h>
#include <pinmux/stm32/pinmux_stm32.h>
#include <drivers/clock_control/stm32_clock_control.h>

static const stm32_pin_func_t pin_pa9_funcs[] = {
	[STM32L4X_PINMUX_FUNC_PA9_USART1_TX - 1] =
		STM32L4X_PIN_CONFIG_PUSH_PULL,
};

static const stm32_pin_func_t pin_pa10_funcs[] = {
	[STM32L4X_PINMUX_FUNC_PA10_USART1_RX - 1] =
		STM32L4X_PIN_CONFIG_BIAS_HIGH_IMPEDANCE,
};

static const stm32_pin_func_t pin_pa2_funcs[] = {
	[STM32L4X_PINMUX_FUNC_PA2_USART2_TX - 1] =
		STM32L4X_PIN_CONFIG_PUSH_PULL,
};

static const stm32_pin_func_t pin_pa3_funcs[] = {
	[STM32L4X_PINMUX_FUNC_PA3_USART2_RX - 1] =
		STM32L4X_PIN_CONFIG_BIAS_HIGH_IMPEDANCE,
};

static const stm32_pin_func_t pin_pb6_funcs[] = {
	[STM32L4X_PINMUX_FUNC_PB6_I2C1_SCL - 1] =
		STM32L4X_PIN_CONFIG_OPEN_DRAIN_PULL_UP,
};

static const stm32_pin_func_t pin_pb7_funcs[] = {
	[STM32L4X_PINMUX_FUNC_PB7_I2C1_SDA - 1] =
		STM32L4X_PIN_CONFIG_OPEN_DRAIN_PULL_UP,
};

static const stm32_pin_func_t pin_pb10_funcs[] = {
	[STM32L4X_PINMUX_FUNC_PB10_USART3_TX - 1] =
		STM32L4X_PIN_CONFIG_PUSH_PULL,
};

static const stm32_pin_func_t pin_pb11_funcs[] = {
	[STM32L4X_PINMUX_FUNC_PB11_USART3_RX - 1] =
		STM32L4X_PIN_CONFIG_BIAS_HIGH_IMPEDANCE,
};

static const stm32_pin_func_t pin_pa0_funcs[] = {
	[STM32L4X_PINMUX_FUNC_PA0_PWM2_CH1 - 1] =
		STM32L4X_PIN_CONFIG_PUSH_PULL,
};

/**
 * @brief pin configuration
 */
static const struct stm32_pinmux_conf pins[] = {
	STM32_PIN_CONF(STM32_PIN_PA0, pin_pa0_funcs),
	STM32_PIN_CONF(STM32_PIN_PA2, pin_pa2_funcs),
	STM32_PIN_CONF(STM32_PIN_PA3, pin_pa3_funcs),
	STM32_PIN_CONF(STM32_PIN_PA9, pin_pa9_funcs),
	STM32_PIN_CONF(STM32_PIN_PA10, pin_pa10_funcs),
	STM32_PIN_CONF(STM32_PIN_PB6, pin_pb6_funcs),
	STM32_PIN_CONF(STM32_PIN_PB7, pin_pb7_funcs),
	STM32_PIN_CONF(STM32_PIN_PB10, pin_pb10_funcs),
	STM32_PIN_CONF(STM32_PIN_PB11, pin_pb11_funcs),
};

int stm32_get_pin_config(int pin, int func)
{
	/* GPIO function is always available, to save space it is not
	 * listed in alternate functions array
	 */
	if (func == STM32_PINMUX_FUNC_GPIO) {
		return STM32L4X_PIN_CONFIG_BIAS_HIGH_IMPEDANCE;
	}

	/* analog function is another 'known' setting */
	if (func == STM32_PINMUX_FUNC_ANALOG) {
		return STM32L4X_PIN_CONFIG_ANALOG;
	}

	for (int i = 0; i < ARRAY_SIZE(pins); i++) {
		if (pins[i].pin == pin) {
			if ((func - 1) >= pins[i].nfuncs) {
				return -EINVAL;
			}

			return pins[i].funcs[func - 1];
		}
	}
	return -EINVAL;
}

clock_control_subsys_t stm32_get_port_clock(int port)
{
	const clock_control_subsys_t ports_to_clock[STM32_PORTS_MAX] = {
		UINT_TO_POINTER(STM32L4X_CLOCK_SUBSYS_GPIOA),
		UINT_TO_POINTER(STM32L4X_CLOCK_SUBSYS_GPIOB),
		UINT_TO_POINTER(STM32L4X_CLOCK_SUBSYS_GPIOC),
		UINT_TO_POINTER(STM32L4X_CLOCK_SUBSYS_GPIOD),
		UINT_TO_POINTER(STM32L4X_CLOCK_SUBSYS_GPIOE),
		UINT_TO_POINTER(STM32L4X_CLOCK_SUBSYS_GPIOF),
		UINT_TO_POINTER(STM32L4X_CLOCK_SUBSYS_GPIOG),
		UINT_TO_POINTER(STM32L4X_CLOCK_SUBSYS_GPIOH),
	};

	if (port > STM32_PORTH) {
		return NULL;
	}

	return ports_to_clock[port];
}