1 /*
2  * Copyright (c) 2021 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * Testing specific helpers for pinctrl driver.
10  */
11 
12 #ifndef ZEPHYR_TESTS_DRIVERS_PINCTRL_API_SRC_PINCTRL_SOC_H_
13 #define ZEPHYR_TESTS_DRIVERS_PINCTRL_API_SRC_PINCTRL_SOC_H_
14 
15 #include <zephyr/devicetree.h>
16 #include <zephyr/types.h>
17 
18 /**
19  * @name Test pin configuration bit field positions and masks.
20  *
21  * Bits 31, 30: Pull config.
22  * Bits 29..0: Pin.
23  *
24  * @{
25  */
26 
27 /** Position of the pull field. */
28 #define TEST_PULL_POS 30U
29 /** Mask of the pull field. */
30 #define TEST_PULL_MSK 0x3U
31 /** Position of the pin field. */
32 #define TEST_PIN_POS 0U
33 /** Mask for the pin field. */
34 #define TEST_PIN_MSK 0x3FFFFFFFU
35 
36 /** @} */
37 
38 /**
39  * @name Test pinctrl pull-up/down.
40  * @{
41  */
42 
43 /** Pull-up disabled. */
44 #define TEST_PULL_DISABLE 0U
45 /** Pull-down enabled. */
46 #define TEST_PULL_DOWN 1U
47 /** Pull-up enabled. */
48 #define TEST_PULL_UP 2U
49 
50 /** @} */
51 
52 /**
53  * @brief Utility macro to obtain pin pull configuration.
54  *
55  * @param pincfg Pin configuration bit field.
56  */
57 #define TEST_GET_PULL(pincfg) (((pincfg) >> TEST_PULL_POS) & TEST_PULL_MSK)
58 
59 /**
60  * @brief Utility macro to obtain port and pin combination.
61  *
62  * @param pincfg Pin configuration bit field.
63  */
64 #define TEST_GET_PIN(pincfg) (((pincfg) >> TEST_PIN_POS) & TEST_PIN_MSK)
65 
66 /** @cond INTERNAL_HIDDEN */
67 
68 /** Test pin type */
69 typedef uint32_t pinctrl_soc_pin_t;
70 
71 /**
72  * @brief Utility macro to initialize each pin.
73  *
74  * @param node_id Node identifier.
75  * @param prop Property name.
76  * @param idx Property entry index.
77  */
78 #define Z_PINCTRL_STATE_PIN_INIT(node_id, prop, idx)			       \
79 	(((DT_PROP_BY_IDX(node_id, prop, idx) << TEST_PIN_POS)		       \
80 	  & TEST_PIN_MSK) |						       \
81 	 ((TEST_PULL_UP * DT_PROP(node_id, bias_pull_up))		       \
82 	  << TEST_PULL_POS) |						       \
83 	 ((TEST_PULL_DOWN * DT_PROP(node_id, bias_pull_down))		       \
84 	  << TEST_PULL_POS)						       \
85 	),
86 
87 /**
88  * @brief Utility macro to initialize state pins contained in a given property.
89  *
90  * @param node_id Node identifier.
91  * @param prop Property name describing state pins.
92  */
93 #define Z_PINCTRL_STATE_PINS_INIT(node_id, prop)			       \
94 	{DT_FOREACH_CHILD_VARGS(DT_PROP_BY_IDX(node_id, prop, 0),	       \
95 				DT_FOREACH_PROP_ELEM, pins,		       \
96 				Z_PINCTRL_STATE_PIN_INIT)}
97 
98 /** @endcond */
99 
100 #endif /* ZEPHYR_TESTS_DRIVERS_PINCTRL_API_SRC_PINCTRL_SOC_H_ */
101