1 /*
2  * Copyright (c) 2023 Antmicro <www.antmicro.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_SOC_ARM_AMBIQ_APOLLO4_PINCTRL_SOC_H_
8 #define ZEPHYR_SOC_ARM_AMBIQ_APOLLO4_PINCTRL_SOC_H_
9 
10 #include <zephyr/dt-bindings/pinctrl/ambiq-apollo4-pinctrl.h>
11 
12 /**
13  * @brief Type to hold a pin's pinctrl configuration.
14  */
15 struct apollo4_pinctrl_soc_pin {
16 	/** Pin number 0..128 */
17 	uint32_t pin_num : 7;
18 	/** Alternative function (UART, SPI, etc.) */
19 	uint32_t alt_func : 4;
20 	/** Enable the pin as an input */
21 	uint32_t input_enable : 1;
22 	/** Drive strength, relative to full-driver strength */
23 	uint32_t drive_strength : 2;
24 	/** Slew rate, may be either false (slow) or true (fast) */
25 	uint32_t slew_rate : 1;
26 	/** Drive actively high or low */
27 	uint32_t push_pull : 1;
28 	/** Drive with open drain */
29 	uint32_t open_drain : 1;
30 	/** High impedance mode */
31 	uint32_t tristate : 1;
32 	/** Enable the internal pull up resistor */
33 	uint32_t bias_pull_up : 1;
34 	/** Enable the internal pull down resistor */
35 	uint32_t bias_pull_down : 1;
36 	/** pullup resistor value */
37 	uint32_t ambiq_pull_up_ohms : 3;
38 	/** IOM nCE module select */
39 	uint32_t iom_nce : 6;
40 };
41 
42 typedef struct apollo4_pinctrl_soc_pin pinctrl_soc_pin_t;
43 
44 /**
45  * @brief Utility macro to initialize each pin.
46  *
47  * @param node_id Node identifier.
48  * @param prop Property name.
49  * @param idx Property entry index.
50  */
51 #define Z_PINCTRL_STATE_PIN_INIT(node_id, prop, idx)				\
52 	{									\
53 		APOLLO4_GET_PIN_NUM(DT_PROP_BY_IDX(node_id, prop, idx)),	\
54 		APOLLO4_GET_PIN_ALT_FUNC(DT_PROP_BY_IDX(node_id, prop, idx)),	\
55 		DT_PROP(node_id, input_enable),					\
56 		DT_ENUM_IDX(node_id, drive_strength),				\
57 		DT_ENUM_IDX(node_id, slew_rate),				\
58 		DT_PROP(node_id, drive_push_pull),				\
59 		DT_PROP(node_id, drive_open_drain),				\
60 		DT_PROP(node_id, bias_high_impedance),				\
61 		DT_PROP(node_id, bias_pull_up),					\
62 		DT_PROP(node_id, bias_pull_down),				\
63 		DT_ENUM_IDX(node_id, ambiq_pull_up_ohms),			\
64 		DT_PROP(node_id, ambiq_iom_nce_module),				\
65 	},
66 
67 /**
68  * @brief Utility macro to initialize state pins contained in a given property.
69  *
70  * @param node_id Node identifier.
71  * @param prop Property name describing state pins.
72  */
73 #define Z_PINCTRL_STATE_PINS_INIT(node_id, prop)				\
74 	{DT_FOREACH_CHILD_VARGS(DT_PHANDLE(node_id, prop),			\
75 				DT_FOREACH_PROP_ELEM, pinmux,			\
76 				Z_PINCTRL_STATE_PIN_INIT)}
77 
78 #define APOLLO4_GET_PIN_NUM(pinctrl) \
79 	(((pinctrl) >> APOLLO4_PIN_NUM_POS) & APOLLO4_PIN_NUM_MASK)
80 #define APOLLO4_GET_PIN_ALT_FUNC(pinctrl) \
81 	(((pinctrl) >> APOLLO4_ALT_FUNC_POS) & APOLLO4_ALT_FUNC_MASK)
82 
83 #endif /* ZEPHYR_SOC_ARM_AMBIQ_APOLLO4_PINCTRL_SOC_H_ */
84