Zephyr API Documentation  3.5.0
A Scalable Open Source RTOS
3.5.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
pinctrl.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 Nordic Semiconductor ASA
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
11#ifndef ZEPHYR_INCLUDE_DRIVERS_PINCTRL_H_
12#define ZEPHYR_INCLUDE_DRIVERS_PINCTRL_H_
13
21#include <errno.h>
22
23#include <zephyr/device.h>
24#include <zephyr/devicetree.h>
26#include <pinctrl_soc.h>
27#include <zephyr/sys/util.h>
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
40#define PINCTRL_STATE_DEFAULT 0U
42#define PINCTRL_STATE_SLEEP 1U
43
45#define PINCTRL_STATE_PRIV_START 2U
46
52 const pinctrl_soc_pin_t *pins;
57};
58
61#if defined(CONFIG_PINCTRL_STORE_REG) || defined(__DOXYGEN__)
67#endif /* defined(CONFIG_PINCTRL_STORE_REG) || defined(__DOXYGEN__) */
69 const struct pinctrl_state *states;
72};
73
75#define PINCTRL_REG_NONE 0U
76
79#ifndef CONFIG_PM_DEVICE
81#define PINCTRL_SKIP_SLEEP 1
82#endif
83
90#define Z_PINCTRL_STATE_ID(state_idx, node_id) \
91 _CONCAT(PINCTRL_STATE_, \
92 DT_PINCTRL_IDX_TO_NAME_UPPER_TOKEN(node_id, state_idx))
93
100#define Z_PINCTRL_DEV_CONFIG_NAME(node_id) \
101 _CONCAT(__pinctrl_dev_config, DEVICE_DT_NAME_GET(node_id))
102
109#define Z_PINCTRL_STATES_NAME(node_id) \
110 _CONCAT(__pinctrl_states, DEVICE_DT_NAME_GET(node_id))
111
119#define Z_PINCTRL_STATE_PINS_NAME(state_idx, node_id) \
120 _CONCAT(__pinctrl_state_pins_ ## state_idx, DEVICE_DT_NAME_GET(node_id))
121
133#define Z_PINCTRL_SKIP_STATE(state_idx, node_id) \
134 _CONCAT(PINCTRL_SKIP_, \
135 DT_PINCTRL_IDX_TO_NAME_UPPER_TOKEN(node_id, state_idx))
136
143#define Z_PINCTRL_STATE_PINS_DEFINE(state_idx, node_id) \
144 COND_CODE_1(Z_PINCTRL_SKIP_STATE(state_idx, node_id), (), \
145 (static const pinctrl_soc_pin_t \
146 Z_PINCTRL_STATE_PINS_NAME(state_idx, node_id)[] = \
147 Z_PINCTRL_STATE_PINS_INIT(node_id, pinctrl_ ## state_idx)))
148
155#define Z_PINCTRL_STATE_INIT(state_idx, node_id) \
156 COND_CODE_1(Z_PINCTRL_SKIP_STATE(state_idx, node_id), (), \
157 ({ \
158 .id = Z_PINCTRL_STATE_ID(state_idx, node_id), \
159 .pins = Z_PINCTRL_STATE_PINS_NAME(state_idx, node_id), \
160 .pin_cnt = ARRAY_SIZE(Z_PINCTRL_STATE_PINS_NAME(state_idx, \
161 node_id)) \
162 }))
163
169#define Z_PINCTRL_STATES_DEFINE(node_id) \
170 static const struct pinctrl_state \
171 Z_PINCTRL_STATES_NAME(node_id)[] = { \
172 LISTIFY(DT_NUM_PINCTRL_STATES(node_id), \
173 Z_PINCTRL_STATE_INIT, (,), node_id) \
174 };
175
176#ifdef CONFIG_PINCTRL_STORE_REG
182#define Z_PINCTRL_DEV_CONFIG_INIT(node_id) \
183 { \
184 .reg = DT_REG_ADDR(node_id), \
185 .states = Z_PINCTRL_STATES_NAME(node_id), \
186 .state_cnt = ARRAY_SIZE(Z_PINCTRL_STATES_NAME(node_id)), \
187 }
188#else
189#define Z_PINCTRL_DEV_CONFIG_INIT(node_id) \
190 { \
191 .states = Z_PINCTRL_STATES_NAME(node_id), \
192 .state_cnt = ARRAY_SIZE(Z_PINCTRL_STATES_NAME(node_id)), \
193 }
194#endif
195
196#ifdef CONFIG_PINCTRL_NON_STATIC
197#define Z_PINCTRL_DEV_CONFIG_STATIC
198#else
199#define Z_PINCTRL_DEV_CONFIG_STATIC static
200#endif
201
202#ifdef CONFIG_PINCTRL_DYNAMIC
203#define Z_PINCTRL_DEV_CONFIG_CONST
204#else
205#define Z_PINCTRL_DEV_CONFIG_CONST const
206#endif
207
210#if defined(CONFIG_PINCTRL_NON_STATIC) || defined(__DOXYGEN__)
223#define PINCTRL_DT_DEV_CONFIG_DECLARE(node_id) \
224 extern Z_PINCTRL_DEV_CONFIG_CONST struct pinctrl_dev_config \
225 Z_PINCTRL_DEV_CONFIG_NAME(node_id)
226#endif /* defined(CONFIG_PINCTRL_NON_STATIC) || defined(__DOXYGEN__) */
227
239#define PINCTRL_DT_DEFINE(node_id) \
240 LISTIFY(DT_NUM_PINCTRL_STATES(node_id), \
241 Z_PINCTRL_STATE_PINS_DEFINE, (;), node_id); \
242 Z_PINCTRL_STATES_DEFINE(node_id) \
243 Z_PINCTRL_DEV_CONFIG_STATIC Z_PINCTRL_DEV_CONFIG_CONST \
244 struct pinctrl_dev_config Z_PINCTRL_DEV_CONFIG_NAME(node_id) = \
245 Z_PINCTRL_DEV_CONFIG_INIT(node_id)
246
254#define PINCTRL_DT_INST_DEFINE(inst) PINCTRL_DT_DEFINE(DT_DRV_INST(inst))
255
262#define PINCTRL_DT_DEV_CONFIG_GET(node_id) &Z_PINCTRL_DEV_CONFIG_NAME(node_id)
263
272#define PINCTRL_DT_INST_DEV_CONFIG_GET(inst) \
273 PINCTRL_DT_DEV_CONFIG_GET(DT_DRV_INST(inst))
274
286 const struct pinctrl_state **state);
287
304int pinctrl_configure_pins(const pinctrl_soc_pin_t *pins, uint8_t pin_cnt,
305 uintptr_t reg);
306
317 const struct pinctrl_dev_config *config,
318 const struct pinctrl_state *state)
319{
320 uintptr_t reg;
321
322#ifdef CONFIG_PINCTRL_STORE_REG
323 reg = config->reg;
324#else
325 ARG_UNUSED(config);
326 reg = PINCTRL_REG_NONE;
327#endif
328
329 return pinctrl_configure_pins(state->pins, state->pin_cnt, reg);
330}
331
342static inline int pinctrl_apply_state(const struct pinctrl_dev_config *config,
343 uint8_t id)
344{
345 int ret;
346 const struct pinctrl_state *state;
347
348 ret = pinctrl_lookup_state(config, id, &state);
349 if (ret < 0) {
350 return ret;
351 }
352
353 return pinctrl_apply_state_direct(config, state);
354}
355
356#if defined(CONFIG_PINCTRL_DYNAMIC) || defined(__DOXYGEN__)
374#define PINCTRL_DT_STATE_PINS_DEFINE(node_id, prop) \
375 static const pinctrl_soc_pin_t prop ## _pins[] = \
376 Z_PINCTRL_STATE_PINS_INIT(node_id, prop); \
377
413#define PINCTRL_DT_STATE_INIT(prop, state) \
414 { \
415 .id = state, \
416 .pins = prop ## _pins, \
417 .pin_cnt = ARRAY_SIZE(prop ## _pins) \
418 }
419
438 const struct pinctrl_state *states,
439 uint8_t state_cnt);
440
442#else
443static inline int pinctrl_update_states(
444 struct pinctrl_dev_config *config,
445 const struct pinctrl_state *states, uint8_t state_cnt)
446{
447 ARG_UNUSED(config);
448 ARG_UNUSED(states);
449 ARG_UNUSED(state_cnt);
450 return -ENOSYS;
451}
452#endif /* defined(CONFIG_PINCTRL_DYNAMIC) || defined(__DOXYGEN__) */
453
454#ifdef __cplusplus
455}
456#endif
457
462#endif /* ZEPHYR_INCLUDE_DRIVERS_PINCTRL_H_ */
Devicetree pin control helpers.
Devicetree main header.
System error numbers.
int pinctrl_update_states(struct pinctrl_dev_config *config, const struct pinctrl_state *states, uint8_t state_cnt)
Update states with a new set.
static int pinctrl_apply_state(const struct pinctrl_dev_config *config, uint8_t id)
Apply a state from the given device configuration.
Definition: pinctrl.h:342
int pinctrl_configure_pins(const pinctrl_soc_pin_t *pins, uint8_t pin_cnt, uintptr_t reg)
Configure a set of pins.
static int pinctrl_apply_state_direct(const struct pinctrl_dev_config *config, const struct pinctrl_state *state)
Apply a state directly from the provided state configuration.
Definition: pinctrl.h:316
int pinctrl_lookup_state(const struct pinctrl_dev_config *config, uint8_t id, const struct pinctrl_state **state)
Find the state configuration for the given state id.
#define PINCTRL_REG_NONE
Utility macro to indicate no register is used.
Definition: pinctrl.h:75
#define ENOSYS
Function not implemented.
Definition: errno.h:83
state
Definition: parser_state.h:29
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
__UINTPTR_TYPE__ uintptr_t
Definition: stdint.h:105
Pin controller configuration for a given device.
Definition: pinctrl.h:60
uint8_t state_cnt
Number of state configurations.
Definition: pinctrl.h:71
const struct pinctrl_state * states
List of state configurations.
Definition: pinctrl.h:69
uintptr_t reg
Device address (only available if <code>CONFIG_PINCTRL_STORE_REG</code> <verbatim>embed:rst:inline...
Definition: pinctrl.h:66
Pin control state configuration.
Definition: pinctrl.h:50
const pinctrl_soc_pin_t * pins
Pin configurations.
Definition: pinctrl.h:52
uint8_t pin_cnt
Number of pin configurations.
Definition: pinctrl.h:54
uint8_t id
State identifier (see PINCTRL_STATES).
Definition: pinctrl.h:56
Misc utilities.