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 	/** IOM interrupt direction */
41 	uint32_t interrupt_direction: 2;
42 };
43 
44 typedef struct apollo4_pinctrl_soc_pin pinctrl_soc_pin_t;
45 
46 /**
47  * @brief Utility macro to initialize each pin.
48  *
49  * @param node_id Node identifier.
50  * @param prop Property name.
51  * @param idx Property entry index.
52  */
53 #define Z_PINCTRL_STATE_PIN_INIT(node_id, prop, idx)                                               \
54 	{                                                                                          \
55 		APOLLO4_GET_PIN_NUM(DT_PROP_BY_IDX(node_id, prop, idx)),                           \
56 		APOLLO4_GET_PIN_ALT_FUNC(DT_PROP_BY_IDX(node_id, prop, idx)),                      \
57 		DT_PROP(node_id, input_enable),                                                    \
58 		DT_ENUM_IDX(node_id, drive_strength),                                              \
59 		DT_ENUM_IDX(node_id, slew_rate),                                                   \
60 		DT_PROP(node_id, drive_push_pull),                                                 \
61 		DT_PROP(node_id, drive_open_drain),                                                \
62 		DT_PROP(node_id, bias_high_impedance),                                             \
63 		DT_PROP(node_id, bias_pull_up),                                                    \
64 		DT_PROP(node_id, bias_pull_down),                                                  \
65 		DT_ENUM_IDX(node_id, ambiq_pull_up_ohms),                                          \
66 		DT_PROP(node_id, ambiq_iom_nce_module),                                            \
67 		DT_PROP(node_id, ambiq_interrupt_direction),                                       \
68 	},
69 
70 /**
71  * @brief Utility macro to initialize state pins contained in a given property.
72  *
73  * @param node_id Node identifier.
74  * @param prop Property name describing state pins.
75  */
76 #define Z_PINCTRL_STATE_PINS_INIT(node_id, prop)				\
77 	{DT_FOREACH_CHILD_VARGS(DT_PHANDLE(node_id, prop),			\
78 				DT_FOREACH_PROP_ELEM, pinmux,			\
79 				Z_PINCTRL_STATE_PIN_INIT)}
80 
81 #define APOLLO4_GET_PIN_NUM(pinctrl) \
82 	(((pinctrl) >> APOLLO4_PIN_NUM_POS) & APOLLO4_PIN_NUM_MASK)
83 #define APOLLO4_GET_PIN_ALT_FUNC(pinctrl) \
84 	(((pinctrl) >> APOLLO4_ALT_FUNC_POS) & APOLLO4_ALT_FUNC_MASK)
85 
86 #endif /* ZEPHYR_SOC_ARM_AMBIQ_APOLLO4_PINCTRL_SOC_H_ */
87