1 /*
2  * Copyright (c) 2022 Silicon Labs
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /**
8  * @file
9  * Silabs SoC specific helpers for pinctrl driver
10  */
11 
12 #ifndef ZEPHYR_SOC_ARM_SILABS_GECKO_COMMON_PINCTRL_SOC_H_
13 #define ZEPHYR_SOC_ARM_SILABS_GECKO_COMMON_PINCTRL_SOC_H_
14 
15 #include <stdint.h>
16 
17 #include <zephyr/devicetree.h>
18 #if CONFIG_PINCTRL_SILABS_DBUS
19 #include <zephyr/sys/util.h>
20 #include <zephyr/dt-bindings/pinctrl/silabs-pinctrl-dbus.h>
21 #elif CONFIG_SOC_FAMILY_SILABS_S1
22 #include <zephyr/dt-bindings/pinctrl/gecko-pinctrl-s1.h>
23 #else
24 #include <zephyr/dt-bindings/pinctrl/gecko-pinctrl.h>
25 #endif
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 /** @cond INTERNAL_HIDDEN */
32 
33 #if CONFIG_PINCTRL_SILABS_DBUS
34 
35 /** Type for Silabs pin using DBUS. */
36 typedef struct pinctrl_soc_pin {
37 	uint16_t base_offset;
38 	uint8_t port;
39 	uint8_t pin;
40 	uint8_t en_bit;
41 	uint8_t route_offset;
42 	uint8_t mode;
43 	uint8_t dout;
44 } pinctrl_soc_pin_t;
45 
46 #define Z_PINCTRL_SILABS_MODE_INIT(node)                                                           \
47 	(DT_PROP(node, drive_push_pull)     ? (4 + DT_PROP(node, silabs_alternate_port_control))   \
48 	 : DT_PROP(node, drive_open_source) ? (6 + DT_PROP(node, bias_pull_down))                  \
49 	 : DT_PROP(node, drive_open_drain)                                                         \
50 		 ? (8 + DT_PROP(node, silabs_input_filter) + 2 * DT_PROP(node, bias_pull_up) +     \
51 		    4 * DT_PROP(node, silabs_alternate_port_control))                              \
52 	 : DT_PROP(node, input_enable)                                                             \
53 		 ? ((DT_PROP(node, bias_pull_down) || DT_PROP(node, bias_pull_up))                 \
54 			    ? (2 + DT_PROP(node, silabs_input_filter))                             \
55 			    : 1)                                                                   \
56 		 : 0)
57 
58 #define Z_PINCTRL_SILABS_DOUT_INIT(node)                                                           \
59 	(DT_PROP(node, drive_push_pull)    ? DT_PROP(node, output_high)                            \
60 	 : DT_PROP(node, drive_open_drain) ? 1                                                     \
61 	 : DT_PROP(node, input_enable)                                                             \
62 		 ? ((DT_PROP(node, bias_pull_down) || DT_PROP(node, bias_pull_up))                 \
63 			    ? DT_PROP(node, bias_pull_up)                                          \
64 			    : DT_PROP(node, silabs_input_filter))                                  \
65 	 : DT_PROP(node, input_disable) ? DT_PROP(node, bias_pull_up)                              \
66 					: 0)
67 
68 #define Z_PINCTRL_STATE_PIN_INIT(node, prop, idx)                                                  \
69 	{.base_offset =                                                                            \
70 		 FIELD_GET(SILABS_PINCTRL_PERIPH_BASE_MASK, DT_PROP_BY_IDX(node, prop, idx)),      \
71 	 .port = FIELD_GET(SILABS_PINCTRL_GPIO_PORT_MASK, DT_PROP_BY_IDX(node, prop, idx)),        \
72 	 .pin = FIELD_GET(SILABS_PINCTRL_GPIO_PIN_MASK, DT_PROP_BY_IDX(node, prop, idx)),          \
73 	 .en_bit =                                                                                 \
74 		 (FIELD_GET(SILABS_PINCTRL_HAVE_EN_MASK, DT_PROP_BY_IDX(node, prop, idx))          \
75 			  ? FIELD_GET(SILABS_PINCTRL_EN_BIT_MASK, DT_PROP_BY_IDX(node, prop, idx)) \
76 			  : 0xFF),                                                                 \
77 	 .route_offset = FIELD_GET(SILABS_PINCTRL_ROUTE_MASK, DT_PROP_BY_IDX(node, prop, idx)),    \
78 	 .mode = Z_PINCTRL_SILABS_MODE_INIT(node),                                                 \
79 	 .dout = Z_PINCTRL_SILABS_DOUT_INIT(node)},
80 
81 #define Z_PINCTRL_STATE_PINS_INIT(node_id, prop)                                                   \
82 	{DT_FOREACH_CHILD_VARGS(DT_PHANDLE(node_id, prop), DT_FOREACH_PROP_ELEM, pins,             \
83 				Z_PINCTRL_STATE_PIN_INIT)}
84 
85 #else
86 
87 /** Type for gecko pin. */
88 typedef uint32_t pinctrl_soc_pin_t;
89 
90 /**
91  * @brief Utility macro to initialize each pin.
92  *
93  * @param node_id Node identifier.
94  * @param prop Property name.
95  * @param idx Property entry index.
96  */
97 #define Z_PINCTRL_STATE_PIN_INIT(node_id, prop, idx) (DT_PROP_BY_IDX(node_id, prop, idx)),
98 
99 /**
100  * @brief Utility macro to initialize state pins contained in a given property.
101  *
102  * @param node_id Node identifier.
103  * @param prop Property name describing state pins.
104  */
105 #define Z_PINCTRL_STATE_PINS_INIT(node_id, prop)                                                   \
106 	{                                                                                          \
107 		DT_FOREACH_CHILD_VARGS(DT_PHANDLE(node_id, prop), DT_FOREACH_PROP_ELEM, psels,     \
108 				       Z_PINCTRL_STATE_PIN_INIT)                                   \
109 	}
110 
111 /**
112  * @brief Utility macro to obtain pin function.
113  *
114  * @param pincfg Pin configuration bit field.
115  */
116 #define GECKO_GET_FUN(pincfg) (((pincfg) >> GECKO_FUN_POS) & GECKO_FUN_MSK)
117 
118 /**
119  * @brief Utility macro to obtain port configuration.
120  *
121  * @param pincfg port configuration bit field.
122  */
123 #define GECKO_GET_PORT(pincfg) (((pincfg) >> GECKO_PORT_POS) & GECKO_PORT_MSK)
124 
125 /**
126  * @brief Utility macro to obtain pin configuration.
127  *
128  * @param pincfg pin configuration bit field.
129  */
130 #define GECKO_GET_PIN(pincfg) (((pincfg) >> GECKO_PIN_POS) & GECKO_PIN_MSK)
131 
132 /**
133  * @brief Utility macro to obtain location configuration.
134  *
135  * @param pincfg Loc configuration bit field.
136  */
137 #define GECKO_GET_LOC(pincfg) (((pincfg) >> GECKO_LOC_POS) & GECKO_LOC_MSK)
138 
139 /**
140  * @brief Utility macro to obtain speed configuration.
141  *
142  * @param pincfg speed configuration bit field.
143  */
144 #define GECKO_GET_SPEED(pincfg) (((pincfg) >> GECKO_SPEED_POS) & GECKO_SPEED_MSK)
145 
146 #endif /* CONFIG_PINCTRL_SILABS_DBUS */
147 
148 /** @endcond */
149 
150 #ifdef __cplusplus
151 }
152 #endif
153 
154 #endif /* ZEPHYR_SOC_ARM_SILABS_GECKO_COMMON_PINCTRL_SOC_H_ */
155