Zephyr API Documentation  3.7.0-rc3
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
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
23#include <errno.h>
24
25#include <zephyr/device.h>
26#include <zephyr/devicetree.h>
28#include <pinctrl_soc.h>
29#include <zephyr/sys/util.h>
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
42#define PINCTRL_STATE_DEFAULT 0U
44#define PINCTRL_STATE_SLEEP 1U
45
47#define PINCTRL_STATE_PRIV_START 2U
48
59};
60
63#if defined(CONFIG_PINCTRL_STORE_REG) || defined(__DOXYGEN__)
69#endif /* defined(CONFIG_PINCTRL_STORE_REG) || defined(__DOXYGEN__) */
71 const struct pinctrl_state *states;
74};
75
77#define PINCTRL_REG_NONE 0U
78
81#if !defined(CONFIG_PM) && !defined(CONFIG_PM_DEVICE)
83#define PINCTRL_SKIP_SLEEP 1
84#endif
85
92#define Z_PINCTRL_STATE_ID(state_idx, node_id) \
93 _CONCAT(PINCTRL_STATE_, \
94 DT_PINCTRL_IDX_TO_NAME_UPPER_TOKEN(node_id, state_idx))
95
102#define Z_PINCTRL_DEV_CONFIG_NAME(node_id) \
103 _CONCAT(__pinctrl_dev_config, DEVICE_DT_NAME_GET(node_id))
104
111#define Z_PINCTRL_STATES_NAME(node_id) \
112 _CONCAT(__pinctrl_states, DEVICE_DT_NAME_GET(node_id))
113
121#define Z_PINCTRL_STATE_PINS_NAME(state_idx, node_id) \
122 _CONCAT(__pinctrl_state_pins_ ## state_idx, DEVICE_DT_NAME_GET(node_id))
123
135#define Z_PINCTRL_SKIP_STATE(state_idx, node_id) \
136 _CONCAT(PINCTRL_SKIP_, \
137 DT_PINCTRL_IDX_TO_NAME_UPPER_TOKEN(node_id, state_idx))
138
145#define Z_PINCTRL_STATE_PINS_DEFINE(state_idx, node_id) \
146 COND_CODE_1(Z_PINCTRL_SKIP_STATE(state_idx, node_id), (), \
147 (static const pinctrl_soc_pin_t \
148 Z_PINCTRL_STATE_PINS_NAME(state_idx, node_id)[] = \
149 Z_PINCTRL_STATE_PINS_INIT(node_id, pinctrl_ ## state_idx)))
150
157#define Z_PINCTRL_STATE_INIT(state_idx, node_id) \
158 COND_CODE_1(Z_PINCTRL_SKIP_STATE(state_idx, node_id), (), \
159 ({ \
160 .id = Z_PINCTRL_STATE_ID(state_idx, node_id), \
161 .pins = Z_PINCTRL_STATE_PINS_NAME(state_idx, node_id), \
162 .pin_cnt = ARRAY_SIZE(Z_PINCTRL_STATE_PINS_NAME(state_idx, \
163 node_id)) \
164 }))
165
171#define Z_PINCTRL_STATES_DEFINE(node_id) \
172 static const struct pinctrl_state \
173 Z_PINCTRL_STATES_NAME(node_id)[] = { \
174 LISTIFY(DT_NUM_PINCTRL_STATES(node_id), \
175 Z_PINCTRL_STATE_INIT, (,), node_id) \
176 };
177
178#ifdef CONFIG_PINCTRL_STORE_REG
184#define Z_PINCTRL_DEV_CONFIG_INIT(node_id) \
185 { \
186 .reg = DT_REG_ADDR(node_id), \
187 .states = Z_PINCTRL_STATES_NAME(node_id), \
188 .state_cnt = ARRAY_SIZE(Z_PINCTRL_STATES_NAME(node_id)), \
189 }
190#else
191#define Z_PINCTRL_DEV_CONFIG_INIT(node_id) \
192 { \
193 .states = Z_PINCTRL_STATES_NAME(node_id), \
194 .state_cnt = ARRAY_SIZE(Z_PINCTRL_STATES_NAME(node_id)), \
195 }
196#endif
197
198#ifdef CONFIG_PINCTRL_NON_STATIC
199#define Z_PINCTRL_DEV_CONFIG_STATIC
200#else
201#define Z_PINCTRL_DEV_CONFIG_STATIC static
202#endif
203
204#ifdef CONFIG_PINCTRL_DYNAMIC
205#define Z_PINCTRL_DEV_CONFIG_CONST
206#else
207#define Z_PINCTRL_DEV_CONFIG_CONST const
208#endif
209
212#if defined(CONFIG_PINCTRL_NON_STATIC) || defined(__DOXYGEN__)
225#define PINCTRL_DT_DEV_CONFIG_DECLARE(node_id) \
226 extern Z_PINCTRL_DEV_CONFIG_CONST struct pinctrl_dev_config \
227 Z_PINCTRL_DEV_CONFIG_NAME(node_id)
228#endif /* defined(CONFIG_PINCTRL_NON_STATIC) || defined(__DOXYGEN__) */
229
241#define PINCTRL_DT_DEFINE(node_id) \
242 LISTIFY(DT_NUM_PINCTRL_STATES(node_id), \
243 Z_PINCTRL_STATE_PINS_DEFINE, (;), node_id); \
244 Z_PINCTRL_STATES_DEFINE(node_id) \
245 Z_PINCTRL_DEV_CONFIG_STATIC Z_PINCTRL_DEV_CONFIG_CONST \
246 struct pinctrl_dev_config Z_PINCTRL_DEV_CONFIG_NAME(node_id) = \
247 Z_PINCTRL_DEV_CONFIG_INIT(node_id)
248
256#define PINCTRL_DT_INST_DEFINE(inst) PINCTRL_DT_DEFINE(DT_DRV_INST(inst))
257
264#define PINCTRL_DT_DEV_CONFIG_GET(node_id) &Z_PINCTRL_DEV_CONFIG_NAME(node_id)
265
274#define PINCTRL_DT_INST_DEV_CONFIG_GET(inst) \
275 PINCTRL_DT_DEV_CONFIG_GET(DT_DRV_INST(inst))
276
288 const struct pinctrl_state **state);
289
307 uintptr_t reg);
308
319 const struct pinctrl_dev_config *config,
320 const struct pinctrl_state *state)
321{
322 uintptr_t reg;
323
324#ifdef CONFIG_PINCTRL_STORE_REG
325 reg = config->reg;
326#else
327 ARG_UNUSED(config);
328 reg = PINCTRL_REG_NONE;
329#endif
330
331 return pinctrl_configure_pins(state->pins, state->pin_cnt, reg);
332}
333
344static inline int pinctrl_apply_state(const struct pinctrl_dev_config *config,
345 uint8_t id)
346{
347 int ret;
348 const struct pinctrl_state *state;
349
350 ret = pinctrl_lookup_state(config, id, &state);
351 if (ret < 0) {
352 return ret;
353 }
354
355 return pinctrl_apply_state_direct(config, state);
356}
357
358#if defined(CONFIG_PINCTRL_DYNAMIC) || defined(__DOXYGEN__)
376#define PINCTRL_DT_STATE_PINS_DEFINE(node_id, prop) \
377 static const pinctrl_soc_pin_t prop ## _pins[] = \
378 Z_PINCTRL_STATE_PINS_INIT(node_id, prop); \
379
415#define PINCTRL_DT_STATE_INIT(prop, state) \
416 { \
417 .id = state, \
418 .pins = prop ## _pins, \
419 .pin_cnt = ARRAY_SIZE(prop ## _pins) \
420 }
421
440 const struct pinctrl_state *states,
441 uint8_t state_cnt);
442
444#else
445static inline int pinctrl_update_states(
446 struct pinctrl_dev_config *config,
447 const struct pinctrl_state *states, uint8_t state_cnt)
448{
449 ARG_UNUSED(config);
450 ARG_UNUSED(states);
451 ARG_UNUSED(state_cnt);
452 return -ENOSYS;
453}
454#endif /* defined(CONFIG_PINCTRL_DYNAMIC) || defined(__DOXYGEN__) */
455
456#ifdef __cplusplus
457}
458#endif
459
464#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:344
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:318
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:77
#define ENOSYS
Function not implemented.
Definition: errno.h:82
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:62
uint8_t state_cnt
Number of state configurations.
Definition: pinctrl.h:73
const struct pinctrl_state * states
List of state configurations.
Definition: pinctrl.h:71
uintptr_t reg
Device address (only available if <code>CONFIG_PINCTRL_STORE_REG</code> <verbatim>embed:rst:inline...
Definition: pinctrl.h:68
Type for R-Car pin.
Definition: pinctrl_rcar_common.h:38
Pin control state configuration.
Definition: pinctrl.h:52
const pinctrl_soc_pin_t * pins
Pin configurations.
Definition: pinctrl.h:54
uint8_t pin_cnt
Number of pin configurations.
Definition: pinctrl.h:56
uint8_t id
State identifier (see PINCTRL_STATES).
Definition: pinctrl.h:58
Misc utilities.