1 /*
2  * Copyright (c) 2022 Espressif Systems (Shanghai) Co., Ltd.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * ESP32 SoC specific helpers for pinctrl driver
10  */
11 
12 #ifndef ZEPHYR_SOC_XTENSA_ESP32_PINCTRL_SOC_H_
13 #define ZEPHYR_SOC_XTENSA_ESP32_PINCTRL_SOC_H_
14 
15 #include <zephyr/devicetree.h>
16 #include <zephyr/types.h>
17 
18 #include <zephyr/dt-bindings/pinctrl/esp-pinctrl-common.h>
19 
20 /** @cond INTERNAL_HIDDEN */
21 
22 /** Type for ESP32 pin. */
23 typedef struct pinctrl_soc_pin {
24 	/** Pinmux settings (pin, direction and signal). */
25 	uint32_t pinmux;
26 	/** Pincfg settings (bias). */
27 	uint32_t pincfg;
28 } pinctrl_soc_pin_t;
29 
30 /**
31  * @brief Utility macro to initialize pinmux field in #pinctrl_pin_t.
32  *
33  * @param node_id Node identifier.
34  */
35 #define Z_PINCTRL_ESP32_PINMUX_INIT(node_id, prop, idx) DT_PROP_BY_IDX(node_id, prop, idx)
36 
37 /**
38  * @brief Utility macro to initialize pincfg field in #pinctrl_pin_t.
39  *
40  * @param node_id Node identifier.
41  */
42 #define Z_PINCTRL_ESP32_PINCFG_INIT(node_id)                                                       \
43 	(((ESP32_NO_PULL * DT_PROP(node_id, bias_disable)) << ESP32_PIN_BIAS_SHIFT) |              \
44 	 ((ESP32_PULL_UP * DT_PROP(node_id, bias_pull_up)) << ESP32_PIN_BIAS_SHIFT) |              \
45 	 ((ESP32_PULL_DOWN * DT_PROP(node_id, bias_pull_down)) << ESP32_PIN_BIAS_SHIFT) |          \
46 	 ((ESP32_PUSH_PULL * DT_PROP(node_id, drive_push_pull)) << ESP32_PIN_DRV_SHIFT) |          \
47 	 ((ESP32_OPEN_DRAIN * DT_PROP(node_id, drive_open_drain)) << ESP32_PIN_DRV_SHIFT) |        \
48 	 ((ESP32_PIN_OUT_HIGH * DT_PROP(node_id, output_high)) << ESP32_PIN_OUT_SHIFT) |           \
49 	 ((ESP32_PIN_OUT_LOW * DT_PROP(node_id, output_low)) << ESP32_PIN_OUT_SHIFT) |             \
50 	 ((ESP32_PIN_OUT_EN * DT_PROP(node_id, output_enable)) << ESP32_PIN_EN_DIR_SHIFT) |        \
51 	 ((ESP32_PIN_IN_EN * DT_PROP(node_id, input_enable)) << ESP32_PIN_EN_DIR_SHIFT))
52 
53 /**
54  * @brief Utility macro to initialize each pin.
55  *
56  * @param node_id Node identifier.
57  * @param prop Property name.
58  * @param idx Property entry index.
59  */
60 #define Z_PINCTRL_STATE_PIN_INIT(node_id, prop, idx)                                               \
61 	{.pinmux = Z_PINCTRL_ESP32_PINMUX_INIT(node_id, prop, idx),                                \
62 	 .pincfg = Z_PINCTRL_ESP32_PINCFG_INIT(node_id)},
63 
64 /**
65  * @brief Utility macro to initialize state pins contained in a given property.
66  *
67  * @param node_id Node identifier.
68  * @param prop Property name describing state pins.
69  */
70 #define Z_PINCTRL_STATE_PINS_INIT(node_id, prop)                                                   \
71 	{                                                                                          \
72 		DT_FOREACH_CHILD_VARGS(DT_PHANDLE(node_id, prop), DT_FOREACH_PROP_ELEM, pinmux,    \
73 				       Z_PINCTRL_STATE_PIN_INIT)                                   \
74 	}
75 
76 /** @endcond */
77 
78 #endif /* ZEPHYR_SOC_XTENSA_ESP32_PINCTRL_SOC_H_ */
79