Branch data Line data Source code
1 : : /*
2 : : * Copyright (c) 2021 Nordic Semiconductor ASA
3 : : *
4 : : * SPDX-License-Identifier: Apache-2.0
5 : : */
6 : :
7 : : #include <drivers/pinctrl.h>
8 : :
9 : : #include <hal/nrf_gpio.h>
10 : :
11 : : BUILD_ASSERT(((NRF_PULL_NONE == NRF_GPIO_PIN_NOPULL) &&
12 : : (NRF_PULL_DOWN == NRF_GPIO_PIN_PULLDOWN) &&
13 : : (NRF_PULL_UP == NRF_GPIO_PIN_PULLUP)),
14 : : "nRF pinctrl pull settings do not match HAL values");
15 : :
16 : : BUILD_ASSERT(((NRF_DRIVE_S0S1 == NRF_GPIO_PIN_S0S1) &&
17 : : (NRF_DRIVE_H0S1 == NRF_GPIO_PIN_H0S1) &&
18 : : (NRF_DRIVE_S0H1 == NRF_GPIO_PIN_S0H1) &&
19 : : (NRF_DRIVE_H0H1 == NRF_GPIO_PIN_H0H1) &&
20 : : (NRF_DRIVE_D0S1 == NRF_GPIO_PIN_D0S1) &&
21 : : (NRF_DRIVE_D0H1 == NRF_GPIO_PIN_D0H1) &&
22 : : (NRF_DRIVE_S0D1 == NRF_GPIO_PIN_S0D1) &&
23 : : (NRF_DRIVE_H0D1 == NRF_GPIO_PIN_H0D1) &&
24 : : #if defined(GPIO_PIN_CNF_DRIVE_E0E1)
25 : : (NRF_DRIVE_E0E1 == NRF_GPIO_PIN_E0E1) &&
26 : : #endif /* defined(GPIO_PIN_CNF_DRIVE_E0E1) */
27 : : (1U)),
28 : : "nRF pinctrl drive settings do not match HAL values");
29 : :
30 : : #if DT_HAS_COMPAT_STATUS_OKAY(nordic_nrf_uart)
31 : : #define NRF_PSEL_UART(reg, line) ((NRF_UART_Type *)reg)->PSEL##line
32 : : #elif DT_HAS_COMPAT_STATUS_OKAY(nordic_nrf_uarte)
33 : : #define NRF_PSEL_UART(reg, line) ((NRF_UARTE_Type *)reg)->PSEL.line
34 : : #endif
35 : :
36 : : #if DT_HAS_COMPAT_STATUS_OKAY(nordic_nrf_spi)
37 : : #define NRF_PSEL_SPIM(reg, line) ((NRF_SPI_Type *)reg)->PSEL##line
38 : : #elif DT_HAS_COMPAT_STATUS_OKAY(nordic_nrf_spim)
39 : : #define NRF_PSEL_SPIM(reg, line) ((NRF_SPIM_Type *)reg)->PSEL.line
40 : : #endif
41 : :
42 : : #if DT_HAS_COMPAT_STATUS_OKAY(nordic_nrf_spis)
43 : : #if defined(NRF51)
44 : : #define NRF_PSEL_SPIS(reg, line) ((NRF_SPIS_Type *)reg)->PSEL##line
45 : : #else
46 : : #define NRF_PSEL_SPIS(reg, line) ((NRF_SPIS_Type *)reg)->PSEL.line
47 : : #endif
48 : : #endif /* DT_HAS_COMPAT_STATUS_OKAY(nordic_nrf_spis) */
49 : :
50 : : #if DT_HAS_COMPAT_STATUS_OKAY(nordic_nrf_twi)
51 : : #if !defined(TWI_PSEL_SCL_CONNECT_Pos)
52 : : #define NRF_PSEL_TWIM(reg, line) ((NRF_TWI_Type *)reg)->PSEL##line
53 : : #else
54 : : #define NRF_PSEL_TWIM(reg, line) ((NRF_TWI_Type *)reg)->PSEL.line
55 : : #endif
56 : : #elif DT_HAS_COMPAT_STATUS_OKAY(nordic_nrf_twim)
57 : : #define NRF_PSEL_TWIM(reg, line) ((NRF_TWIM_Type *)reg)->PSEL.line
58 : : #endif
59 : :
60 : : #if DT_HAS_COMPAT_STATUS_OKAY(nordic_nrf_i2s)
61 : : #define NRF_PSEL_I2S(reg, line) ((NRF_I2S_Type *)reg)->PSEL.line
62 : : #endif
63 : :
64 : : #if DT_HAS_COMPAT_STATUS_OKAY(nordic_nrf_pdm)
65 : : #define NRF_PSEL_PDM(reg, line) ((NRF_PDM_Type *)reg)->PSEL.line
66 : : #endif
67 : :
68 : : #if DT_HAS_COMPAT_STATUS_OKAY(nordic_nrf_pwm)
69 : : #define NRF_PSEL_PWM(reg, line) ((NRF_PWM_Type *)reg)->PSEL.line
70 : : #endif
71 : :
72 : : #if DT_HAS_COMPAT_STATUS_OKAY(nordic_nrf_qdec)
73 : : #define NRF_PSEL_QDEC(reg, line) ((NRF_QDEC_Type *)reg)->PSEL.line
74 : : #endif
75 : :
76 : : #if DT_HAS_COMPAT_STATUS_OKAY(nordic_nrf_qspi)
77 : : #define NRF_PSEL_QSPI(reg, line) ((NRF_QSPI_Type *)reg)->PSEL.line
78 : : #endif
79 : :
80 : : /**
81 : : * @brief Configure pin settings.
82 : : *
83 : : * @param pin Pin configuration.
84 : : * @param dir Pin direction.
85 : : * @param input Pin input buffer connection.
86 : : */
87 : 4 : __unused static void nrf_pin_configure(pinctrl_soc_pin_t pin,
88 : : nrf_gpio_pin_dir_t dir,
89 : : nrf_gpio_pin_input_t input)
90 : : {
91 : : /* force input direction and disconnected buffer for low power */
92 [ - + ]: 4 : if (NRF_GET_LP(pin) == NRF_LP_ENABLE) {
93 : 0 : dir = NRF_GPIO_PIN_DIR_INPUT;
94 : 0 : input = NRF_GPIO_PIN_INPUT_DISCONNECT;
95 : : }
96 : :
97 : 4 : nrf_gpio_cfg(NRF_GET_PIN(pin), dir, input, NRF_GET_PULL(pin),
98 : 4 : NRF_GET_DRIVE(pin), NRF_GPIO_PIN_NOSENSE);
99 : 4 : }
100 : :
101 : 1 : int pinctrl_configure_pins(const pinctrl_soc_pin_t *pins, uint8_t pin_cnt,
102 : : uintptr_t reg)
103 : : {
104 [ + + ]: 5 : for (uint8_t i = 0U; i < pin_cnt; i++) {
105 [ + + + + : 4 : switch (NRF_GET_FUN(pins[i])) {
- - - - -
- - - - -
- - - - -
- ]
106 : : #if defined(NRF_PSEL_UART)
107 : 1 : case NRF_FUN_UART_TX:
108 : 1 : NRF_PSEL_UART(reg, TXD) = NRF_GET_PIN(pins[i]);
109 : 1 : nrf_gpio_pin_write(NRF_GET_PIN(pins[i]), 1);
110 : 1 : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_OUTPUT,
111 : : NRF_GPIO_PIN_INPUT_DISCONNECT);
112 : 1 : break;
113 : 1 : case NRF_FUN_UART_RX:
114 : 1 : NRF_PSEL_UART(reg, RXD) = NRF_GET_PIN(pins[i]);
115 : 1 : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
116 : : NRF_GPIO_PIN_INPUT_CONNECT);
117 : 1 : break;
118 : 1 : case NRF_FUN_UART_RTS:
119 : 1 : NRF_PSEL_UART(reg, RTS) = NRF_GET_PIN(pins[i]);
120 : 1 : nrf_gpio_pin_write(NRF_GET_PIN(pins[i]), 1);
121 : 1 : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_OUTPUT,
122 : : NRF_GPIO_PIN_INPUT_DISCONNECT);
123 : 1 : break;
124 : 1 : case NRF_FUN_UART_CTS:
125 : 1 : NRF_PSEL_UART(reg, CTS) = NRF_GET_PIN(pins[i]);
126 : 1 : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
127 : : NRF_GPIO_PIN_INPUT_CONNECT);
128 : 1 : break;
129 : : #endif /* defined(NRF_PSEL_UART) */
130 : : #if defined(NRF_PSEL_SPIM)
131 : 0 : case NRF_FUN_SPIM_SCK:
132 : 0 : NRF_PSEL_SPIM(reg, SCK) = NRF_GET_PIN(pins[i]);
133 : 0 : nrf_gpio_pin_write(NRF_GET_PIN(pins[i]), 0);
134 : 0 : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_OUTPUT,
135 : : NRF_GPIO_PIN_INPUT_CONNECT);
136 : 0 : break;
137 : 0 : case NRF_FUN_SPIM_MOSI:
138 : 0 : NRF_PSEL_SPIM(reg, MOSI) = NRF_GET_PIN(pins[i]);
139 : 0 : nrf_gpio_pin_write(NRF_GET_PIN(pins[i]), 0);
140 : 0 : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_OUTPUT,
141 : : NRF_GPIO_PIN_INPUT_DISCONNECT);
142 : 0 : break;
143 : 0 : case NRF_FUN_SPIM_MISO:
144 : 0 : NRF_PSEL_SPIM(reg, MISO) = NRF_GET_PIN(pins[i]);
145 : 0 : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
146 : : NRF_GPIO_PIN_INPUT_CONNECT);
147 : 0 : break;
148 : : #endif /* defined(NRF_PSEL_SPIM) */
149 : : #if defined(NRF_PSEL_SPIS)
150 : : case NRF_FUN_SPIS_SCK:
151 : : NRF_PSEL_SPIS(reg, SCK) = NRF_GET_PIN(pins[i]);
152 : : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
153 : : NRF_GPIO_PIN_INPUT_CONNECT);
154 : : break;
155 : : case NRF_FUN_SPIS_MOSI:
156 : : NRF_PSEL_SPIS(reg, MOSI) = NRF_GET_PIN(pins[i]);
157 : : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
158 : : NRF_GPIO_PIN_INPUT_CONNECT);
159 : : break;
160 : : case NRF_FUN_SPIS_MISO:
161 : : NRF_PSEL_SPIS(reg, MISO) = NRF_GET_PIN(pins[i]);
162 : : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
163 : : NRF_GPIO_PIN_INPUT_DISCONNECT);
164 : : break;
165 : : case NRF_FUN_SPIS_CSN:
166 : : NRF_PSEL_SPIS(reg, CSN) = NRF_GET_PIN(pins[i]);
167 : : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
168 : : NRF_GPIO_PIN_INPUT_CONNECT);
169 : : break;
170 : : #endif /* defined(NRF_PSEL_SPIS) */
171 : : #if defined(NRF_PSEL_TWIM)
172 : 0 : case NRF_FUN_TWIM_SCL:
173 : 0 : NRF_PSEL_TWIM(reg, SCL) = NRF_GET_PIN(pins[i]);
174 : 0 : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
175 : : NRF_GPIO_PIN_INPUT_CONNECT);
176 : 0 : break;
177 : 0 : case NRF_FUN_TWIM_SDA:
178 : 0 : NRF_PSEL_TWIM(reg, SDA) = NRF_GET_PIN(pins[i]);
179 : 0 : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
180 : : NRF_GPIO_PIN_INPUT_CONNECT);
181 : 0 : break;
182 : : #endif /* defined(NRF_PSEL_TWIM) */
183 : : #if defined(NRF_PSEL_I2S)
184 : : case NRF_FUN_I2S_SCK_M:
185 : : NRF_PSEL_I2S(reg, SCK) = NRF_GET_PIN(pins[i]);
186 : : nrf_gpio_pin_write(NRF_GET_PIN(pins[i]), 0);
187 : : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_OUTPUT,
188 : : NRF_GPIO_PIN_INPUT_DISCONNECT);
189 : : break;
190 : : case NRF_FUN_I2S_SCK_S:
191 : : NRF_PSEL_I2S(reg, SCK) = NRF_GET_PIN(pins[i]);
192 : : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
193 : : NRF_GPIO_PIN_INPUT_CONNECT);
194 : : break;
195 : : case NRF_FUN_I2S_LRCK_M:
196 : : NRF_PSEL_I2S(reg, LRCK) = NRF_GET_PIN(pins[i]);
197 : : nrf_gpio_pin_write(NRF_GET_PIN(pins[i]), 0);
198 : : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_OUTPUT,
199 : : NRF_GPIO_PIN_INPUT_DISCONNECT);
200 : : break;
201 : : case NRF_FUN_I2S_LRCK_S:
202 : : NRF_PSEL_I2S(reg, LRCK) = NRF_GET_PIN(pins[i]);
203 : : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
204 : : NRF_GPIO_PIN_INPUT_CONNECT);
205 : : break;
206 : : case NRF_FUN_I2S_SDIN:
207 : : NRF_PSEL_I2S(reg, SDIN) = NRF_GET_PIN(pins[i]);
208 : : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
209 : : NRF_GPIO_PIN_INPUT_CONNECT);
210 : : break;
211 : : case NRF_FUN_I2S_SDOUT:
212 : : NRF_PSEL_I2S(reg, SDOUT) = NRF_GET_PIN(pins[i]);
213 : : nrf_gpio_pin_write(NRF_GET_PIN(pins[i]), 0);
214 : : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_OUTPUT,
215 : : NRF_GPIO_PIN_INPUT_DISCONNECT);
216 : : break;
217 : : case NRF_FUN_I2S_MCK:
218 : : NRF_PSEL_I2S(reg, MCK) = NRF_GET_PIN(pins[i]);
219 : : nrf_gpio_pin_write(NRF_GET_PIN(pins[i]), 0);
220 : : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_OUTPUT,
221 : : NRF_GPIO_PIN_INPUT_DISCONNECT);
222 : : break;
223 : : #endif /* defined(NRF_PSEL_I2S) */
224 : : #if defined(NRF_PSEL_PDM)
225 : : case NRF_FUN_PDM_CLK:
226 : : NRF_PSEL_PDM(reg, CLK) = NRF_GET_PIN(pins[i]);
227 : : nrf_gpio_pin_write(NRF_GET_PIN(pins[i]), 0);
228 : : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_OUTPUT,
229 : : NRF_GPIO_PIN_INPUT_DISCONNECT);
230 : : break;
231 : : case NRF_FUN_PDM_DIN:
232 : : NRF_PSEL_PDM(reg, DIN) = NRF_GET_PIN(pins[i]);
233 : : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
234 : : NRF_GPIO_PIN_INPUT_CONNECT);
235 : : break;
236 : : #endif /* defined(NRF_PSEL_PDM) */
237 : : #if defined(NRF_PSEL_PWM)
238 : 0 : case NRF_FUN_PWM_OUT0:
239 : 0 : NRF_PSEL_PWM(reg, OUT[0]) = NRF_GET_PIN(pins[i]);
240 : 0 : nrf_gpio_pin_write(NRF_GET_PIN(pins[i]),
241 : 0 : NRF_GET_INVERT(pins[i]));
242 : 0 : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_OUTPUT,
243 : : NRF_GPIO_PIN_INPUT_DISCONNECT);
244 : 0 : break;
245 : 0 : case NRF_FUN_PWM_OUT1:
246 : 0 : NRF_PSEL_PWM(reg, OUT[1]) = NRF_GET_PIN(pins[i]);
247 : 0 : nrf_gpio_pin_write(NRF_GET_PIN(pins[i]),
248 : 0 : NRF_GET_INVERT(pins[i]));
249 : 0 : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_OUTPUT,
250 : : NRF_GPIO_PIN_INPUT_DISCONNECT);
251 : 0 : break;
252 : 0 : case NRF_FUN_PWM_OUT2:
253 : 0 : NRF_PSEL_PWM(reg, OUT[2]) = NRF_GET_PIN(pins[i]);
254 : 0 : nrf_gpio_pin_write(NRF_GET_PIN(pins[i]),
255 : 0 : NRF_GET_INVERT(pins[i]));
256 : 0 : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_OUTPUT,
257 : : NRF_GPIO_PIN_INPUT_DISCONNECT);
258 : 0 : break;
259 : 0 : case NRF_FUN_PWM_OUT3:
260 : 0 : NRF_PSEL_PWM(reg, OUT[3]) = NRF_GET_PIN(pins[i]);
261 : 0 : nrf_gpio_pin_write(NRF_GET_PIN(pins[i]),
262 : 0 : NRF_GET_INVERT(pins[i]));
263 : 0 : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_OUTPUT,
264 : : NRF_GPIO_PIN_INPUT_DISCONNECT);
265 : 0 : break;
266 : : #endif /* defined(NRF_PSEL_PWM) */
267 : : #if defined(NRF_PSEL_QDEC)
268 : : case NRF_FUN_QDEC_A:
269 : : NRF_PSEL_QDEC(reg, A) = NRF_GET_PIN(pins[i]);
270 : : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
271 : : NRF_GPIO_PIN_INPUT_CONNECT);
272 : : break;
273 : : case NRF_FUN_QDEC_B:
274 : : NRF_PSEL_QDEC(reg, B) = NRF_GET_PIN(pins[i]);
275 : : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
276 : : NRF_GPIO_PIN_INPUT_CONNECT);
277 : : break;
278 : : case NRF_FUN_QDEC_LED:
279 : : NRF_PSEL_QDEC(reg, LED) = NRF_GET_PIN(pins[i]);
280 : : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
281 : : NRF_GPIO_PIN_INPUT_CONNECT);
282 : : break;
283 : : #endif /* defined(NRF_PSEL_QDEC) */
284 : : #if defined(NRF_PSEL_QSPI)
285 : 0 : case NRF_FUN_QSPI_SCK:
286 : 0 : NRF_PSEL_QSPI(reg, SCK) = NRF_GET_PIN(pins[i]);
287 : 0 : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
288 : : NRF_GPIO_PIN_INPUT_DISCONNECT);
289 : 0 : break;
290 : 0 : case NRF_FUN_QSPI_CSN:
291 : 0 : NRF_PSEL_QSPI(reg, CSN) = NRF_GET_PIN(pins[i]);
292 : 0 : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
293 : : NRF_GPIO_PIN_INPUT_DISCONNECT);
294 : 0 : break;
295 : 0 : case NRF_FUN_QSPI_IO0:
296 : 0 : NRF_PSEL_QSPI(reg, IO0) = NRF_GET_PIN(pins[i]);
297 : 0 : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
298 : : NRF_GPIO_PIN_INPUT_DISCONNECT);
299 : 0 : break;
300 : 0 : case NRF_FUN_QSPI_IO1:
301 : 0 : NRF_PSEL_QSPI(reg, IO1) = NRF_GET_PIN(pins[i]);
302 : 0 : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
303 : : NRF_GPIO_PIN_INPUT_DISCONNECT);
304 : 0 : break;
305 : 0 : case NRF_FUN_QSPI_IO2:
306 : 0 : NRF_PSEL_QSPI(reg, IO2) = NRF_GET_PIN(pins[i]);
307 : 0 : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
308 : : NRF_GPIO_PIN_INPUT_DISCONNECT);
309 : 0 : break;
310 : 0 : case NRF_FUN_QSPI_IO3:
311 : 0 : NRF_PSEL_QSPI(reg, IO3) = NRF_GET_PIN(pins[i]);
312 : 0 : nrf_pin_configure(pins[i], NRF_GPIO_PIN_DIR_INPUT,
313 : : NRF_GPIO_PIN_INPUT_DISCONNECT);
314 : 0 : break;
315 : : #endif /* defined(NRF_PSEL_QSPI) */
316 : 0 : default:
317 : 0 : return -ENOTSUP;
318 : : }
319 : : }
320 : :
321 : 1 : return 0;
322 : : }
|