1 /*
2  * Copyright (c) 2022 ITE Technology Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_SOC_RISCV_ITE_IT8XXX2_COMMON_PINCTRL_SOC_H_
8 #define ZEPHYR_SOC_RISCV_ITE_IT8XXX2_COMMON_PINCTRL_SOC_H_
9 
10 #include <zephyr/devicetree.h>
11 #include <zephyr/dt-bindings/pinctrl/it8xxx2-pinctrl.h>
12 #include <zephyr/types.h>
13 
14 /**
15  * @brief ITE IT8XXX2 pin type.
16  */
17 struct pinctrl_soc_pin {
18 	/* Pinmux control group */
19 	const struct device *pinctrls;
20 	/*
21 	 * Pin configuration
22 	 * kSI[7:0] and KSO[15:0] pins only support pull-up, push-pull/open-drain.
23 	 * GPIO group pinctrl pins (include KSO[17:16]) support impedance,
24 	 * pull-up/down, voltage selection, input.
25 	 */
26 	uint32_t pincfg;
27 	/* GPIO pin */
28 	uint8_t pin;
29 	/* Alternate function */
30 	uint8_t alt_func;
31 };
32 
33 typedef struct pinctrl_soc_pin pinctrl_soc_pin_t;
34 
35 /**
36  * @brief PIN configuration bitfield.
37  *
38  * Pin configuration is coded with the following
39  * bit fields.
40  *    Pin impedance config      [ 0 ]
41  *    Pin pull-up/down config   [ 4 : 5 ]
42  *    Pin voltage selection     [ 8 ]
43  *    Pin input enable config   [ 12 ]
44  *    Pin push-pull/open-drain  [ 16 ]
45  *    Pin drive current high/low[ 20 ]
46  *    Pin drive current default [ 21 ]
47  */
48 #define IT8XXX2_HIGH_IMPEDANCE     0x1U
49 #define IT8XXX2_PULL_PIN_DEFAULT   0x0U
50 #define IT8XXX2_PULL_UP            0x1U
51 #define IT8XXX2_PULL_DOWN          0x2U
52 #define IT8XXX2_VOLTAGE_3V3        0x0U
53 #define IT8XXX2_VOLTAGE_1V8        0x1U
54 #define IT8XXX2_INPUT_ENABLE       0x1U
55 #define IT8XXX2_PUSH_PULL          0x0U
56 #define IT8XXX2_OPEN_DRAIN         0x1U
57 #define IT8XXX2_DRIVE_STRENGTH     0x1U
58 #define IT8XXX2_DRIVE_DEFAULT      0x2U
59 
60 /* Pin tri-state mode. */
61 #define IT8XXX2_IMPEDANCE_SHIFT    0U
62 #define IT8XXX2_IMPEDANCE_MASK     0x1U
63 /* Pin pull-up or pull-down */
64 #define IT8XXX2_PUPDR_SHIFT        4U
65 #define IT8XXX2_PUPDR_MASK         0x3U
66 #define IT8XXX2_PULL_UP_MASK       BIT_MASK(1)
67 /* Pin 3.3V or 1.8V */
68 #define IT8XXX2_VOLTAGE_SHIFT      8U
69 #define IT8XXX2_VOLTAGE_MASK       0x1U
70 /* Pin INPUT enable or disable */
71 #define IT8XXX2_INPUT_SHIFT        12U
72 #define IT8XXX2_INPUT_MASK         0x1U
73 /* Pin push-pull/open-drain mode */
74 #define IT8XXX2_PP_OD_SHIFT        16U
75 #define IT8XXX2_PP_OD_MASK         BIT_MASK(1)
76 /* Pin driving select control */
77 #define IT8XXX2_PDSCX_SHIFT        20U
78 #define IT8XXX2_PDSCX_MASK         BIT_MASK(2)
79 
80 /**
81  * @brief Utility macro to obtain configuration of tri-state.
82  */
83 #define IT8XXX2_DT_PINCFG_IMPEDANCE(__mode) \
84 	(((__mode) >> IT8XXX2_IMPEDANCE_SHIFT) & IT8XXX2_IMPEDANCE_MASK)
85 
86 /**
87  * @brief Utility macro to obtain configuration of pull-up or pull-down.
88  */
89 #define IT8XXX2_DT_PINCFG_PUPDR(__mode)     \
90 	(((__mode) >> IT8XXX2_PUPDR_SHIFT) & IT8XXX2_PUPDR_MASK)
91 
92 /**
93  * @brief Utility macro to obtain input voltage selection.
94  */
95 #define IT8XXX2_DT_PINCFG_VOLTAGE(__mode)   \
96 	(((__mode) >> IT8XXX2_VOLTAGE_SHIFT) & IT8XXX2_VOLTAGE_MASK)
97 
98 /**
99  * @brief Utility macro to obtain input enable.
100  */
101 #define IT8XXX2_DT_PINCFG_INPUT(__mode)     \
102 	(((__mode) >> IT8XXX2_INPUT_SHIFT) & IT8XXX2_INPUT_MASK)
103 
104 /**
105  * @brief Utility macro to obtain configuration of pull-up or not.
106  */
107 #define IT8XXX2_DT_PINCFG_PULLUP(__mode) \
108 	(((__mode) >> IT8XXX2_PUPDR_SHIFT) & IT8XXX2_PULL_UP_MASK)
109 
110 /**
111  * @brief Utility macro to obtain configuration of push-pull/open-drain mode.
112  */
113 #define IT8XXX2_DT_PINCFG_PP_OD(__mode) \
114 	(((__mode) >> IT8XXX2_PP_OD_SHIFT) & IT8XXX2_PP_OD_MASK)
115 
116 /**
117  * @brief Utility macro to obtain configuration of driving current selection.
118  */
119 #define IT8XXX2_DT_PINCFG_DRIVE_CURRENT(__mode) \
120 	(((__mode) >> IT8XXX2_PDSCX_SHIFT) & IT8XXX2_PDSCX_MASK)
121 
122 /**
123  * @brief Utility macro to initialize pincfg field in #pinctrl_pin_t.
124  *
125  * @param node_id Node identifier.
126  */
127 #define Z_PINCTRL_IT8XXX2_PINCFG_INIT(node_id)                                 \
128 	(((IT8XXX2_HIGH_IMPEDANCE * DT_PROP(node_id, bias_high_impedance))     \
129 	 << IT8XXX2_IMPEDANCE_SHIFT) |                                         \
130 	 ((IT8XXX2_PULL_PIN_DEFAULT * DT_PROP(node_id, bias_pull_pin_default)) \
131 	 << IT8XXX2_PUPDR_SHIFT) |                                             \
132 	 ((IT8XXX2_PULL_UP * DT_PROP(node_id, bias_pull_up))                   \
133 	 << IT8XXX2_PUPDR_SHIFT) |                                             \
134 	 ((IT8XXX2_PULL_DOWN * DT_PROP(node_id, bias_pull_down))               \
135 	 << IT8XXX2_PUPDR_SHIFT) |                                             \
136 	 ((IT8XXX2_VOLTAGE_1V8 * DT_ENUM_IDX(node_id, gpio_voltage))           \
137 	 << IT8XXX2_VOLTAGE_SHIFT) |                                           \
138 	 ((IT8XXX2_INPUT_ENABLE * DT_PROP(node_id, input_enable))              \
139 	 << IT8XXX2_INPUT_SHIFT) |                                             \
140 	 ((IT8XXX2_OPEN_DRAIN * DT_PROP(node_id, drive_open_drain))            \
141 	 << IT8XXX2_PP_OD_SHIFT) |                                             \
142 	 ((IT8XXX2_DRIVE_STRENGTH *                                            \
143 	 DT_ENUM_IDX_OR(node_id, drive_strength, IT8XXX2_DRIVE_DEFAULT))       \
144 	 << IT8XXX2_PDSCX_SHIFT))
145 
146 /**
147  * @brief Utility macro to initialize pinctrls of pinmuxs field in #pinctrl_pin_t.
148  *
149  * @param node_id Node identifier.
150  */
151 #define Z_PINCTRL_IT8XXX2_PINCTRL_INIT(node_id)      \
152 	DEVICE_DT_GET(DT_PHANDLE(node_id, pinmuxs))
153 /**
154  * @brief Utility macro to initialize pin of pinmuxs field in #pinctrl_pin_t.
155  *
156  * @param node_id Node identifier.
157  */
158 #define Z_PINCTRL_IT8XXX2_PIN_INIT(node_id)          \
159 	DT_PHA(node_id, pinmuxs, pin)
160 /**
161  * @brief Utility macro to initialize alt_func of pinmuxs field in #pinctrl_pin_t.
162  *
163  * @param node_id Node identifier.
164  */
165 #define Z_PINCTRL_IT8XXX2_ALT_INIT(node_id)          \
166 	DT_PHA(node_id, pinmuxs, alt_func)
167 
168 /**
169  * @brief Utility macro to initialize each pin.
170  *
171  * @param node_id Node identifier.
172  * @param prop Property name.
173  * @param idx Property entry index.
174  */
175 #define Z_PINCTRL_STATE_PIN_INIT(node_id, prop, idx)          \
176 	{ .pinctrls = Z_PINCTRL_IT8XXX2_PINCTRL_INIT(         \
177 		DT_PROP_BY_IDX(node_id, prop, idx)),          \
178 	  .pincfg = Z_PINCTRL_IT8XXX2_PINCFG_INIT(            \
179 		DT_PROP_BY_IDX(node_id, prop, idx)),          \
180 	  .pin = Z_PINCTRL_IT8XXX2_PIN_INIT(                  \
181 		DT_PROP_BY_IDX(node_id, prop, idx)),          \
182 	  .alt_func = Z_PINCTRL_IT8XXX2_ALT_INIT(             \
183 		DT_PROP_BY_IDX(node_id, prop, idx)), },
184 
185 /**
186  * @brief Utility macro to initialize state pins contained in a given property.
187  *
188  * @param node_id Node identifier.
189  * @param prop Property name describing state pins.
190  */
191 #define Z_PINCTRL_STATE_PINS_INIT(node_id, prop)              \
192 	{DT_FOREACH_PROP_ELEM(node_id, prop, Z_PINCTRL_STATE_PIN_INIT)}
193 
194 #endif /* ZEPHYR_SOC_RISCV_ITE_IT8XXX2_COMMON_PINCTRL_SOC_H_ */
195