1 /*
2  * Copyright (c) 2024 Renesas Electronics Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_SOC_RENESAS_RA_COMMON_PINCTRL_SOC_H_
8 #define ZEPHYR_SOC_RENESAS_RA_COMMON_PINCTRL_SOC_H_
9 
10 #include <zephyr/devicetree.h>
11 #include <zephyr/types.h>
12 
13 #include <zephyr/dt-bindings/pinctrl/renesas/pinctrl-ra.h>
14 
15 #define RA_PINCTRL_PORT_NUM ARRAY_SIZE(((R_PFS_Type *)0)->PORT)
16 #define RA_PINCTRL_PIN_NUM  ARRAY_SIZE(((R_PFS_PORT_Type *)0)->PIN)
17 /**
18  * @brief Type to hold a renesas ra pin's pinctrl configuration.
19  */
20 struct ra_pinctrl_soc_pin {
21 	/** Port number 0..9, A, B */
22 	uint32_t port_num: 4;
23 	/** Pin number 0..15 */
24 	uint32_t pin_num: 4;
25 	/** Register PFS cfg */
26 	uint32_t cfg;
27 };
28 
29 typedef struct ra_pinctrl_soc_pin pinctrl_soc_pin_t;
30 
31 /**
32  * @brief Utility macro to initialize each pin.
33  *
34  * @param node_id Node identifier.
35  * @param prop Property name.
36  * @param idx Property entry index.
37  */
38 #define Z_PINCTRL_STATE_PIN_INIT(node_id, prop, idx)                                               \
39 	{                                                                                          \
40 		.port_num = RA_GET_PORT_NUM(DT_PROP_BY_IDX(node_id, prop, idx)),                   \
41 		.pin_num = RA_GET_PIN_NUM(DT_PROP_BY_IDX(node_id, prop, idx)),                     \
42 		.cfg = (DT_PROP(node_id, bias_pull_up) << 4) |                                     \
43 		       (DT_PROP(node_id, drive_open_drain) << 6) |                                 \
44 		       (DT_PROP(node_id, renesas_analog_enable) << 15) |                           \
45 		       (DT_ENUM_IDX(node_id, drive_strength) << 10) |                              \
46 		       (RA_GET_MODE(DT_PROP_BY_IDX(node_id, prop, idx)) << 16) |                   \
47 		       (RA_GET_PSEL(DT_PROP_BY_IDX(node_id, prop, idx)) << 24),                    \
48 	},
49 
50 /**
51  * @brief Utility macro to initialize state pins contained in a given property.
52  *
53  * @param node_id Node identifier.
54  * @param prop Property name describing state pins.
55  */
56 #define Z_PINCTRL_STATE_PINS_INIT(node_id, prop)                                                   \
57 	{                                                                                          \
58 		DT_FOREACH_CHILD_VARGS(DT_PHANDLE(node_id, prop), DT_FOREACH_PROP_ELEM, psels,     \
59 				       Z_PINCTRL_STATE_PIN_INIT)                                   \
60 	}
61 
62 #define RA_GET_PORT_NUM(pinctrl) (((pinctrl) >> RA_PORT_NUM_POS) & RA_PORT_NUM_MASK)
63 #define RA_GET_PIN_NUM(pinctrl)  (((pinctrl) >> RA_PIN_NUM_POS) & RA_PIN_NUM_MASK)
64 
65 #define RA_GET_MODE(pinctrl) (((pinctrl) >> RA_MODE_POS) & RA_MODE_MASK)
66 #define RA_GET_PSEL(pinctrl) (((pinctrl) >> RA_PSEL_POS) & RA_PSEL_MASK)
67 
68 #endif /* ZEPHYR_SOC_RENESAS_RA_COMMON_PINCTRL_SOC_H_ */
69