1 /*
2  * Copyright (c) 2025 Renesas Electronics Corporation
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #ifndef ZEPHYR_SOC_RENESAS_RZ_COMMON_PINCTRL_RZT_H_
6 #define ZEPHYR_SOC_RENESAS_RZ_COMMON_PINCTRL_RZT_H_
7 
8 #include <zephyr/types.h>
9 #include <zephyr/devicetree.h>
10 #include "r_ioport.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 #define RZT_GET_PORT_PIN(pinmux) (pinmux & ~(0xF << 4))
17 #define RZT_GET_FUNC(pinmux)     ((pinmux & 0xF0) >> 4)
18 
19 /*Porting*/
20 typedef struct pinctrl_cfg_data_t {
21 	uint32_t p_reg: 1;
22 	uint32_t pm_reg: 2;
23 	uint32_t pmc_reg: 1;
24 	uint32_t pfc_reg: 4;
25 	uint32_t drct_reg: 6;
26 	uint32_t rsel_reg: 1;
27 	uint32_t reserved: 17;
28 } pinctrl_cfg_data_t;
29 
30 typedef struct pinctrl_soc_pin_t {
31 	bsp_io_port_pin_t port_pin;
32 	pinctrl_cfg_data_t config;
33 } pinctrl_soc_pin_t;
34 
35 #define Z_PINCTRL_STATE_PIN_INIT(node_id, prop, idx)                                               \
36 	{                                                                                          \
37 		.port_pin = RZT_GET_PORT_PIN(DT_PROP_BY_IDX(node_id, prop, idx)),                  \
38 		.config =                                                                          \
39 			{                                                                          \
40 				.p_reg = DT_PROP(node_id, output_high),                            \
41 				.pm_reg = DT_PROP(node_id, input_enable) == 1                      \
42 						  ? 1U                                             \
43 						  : (DT_PROP(node_id, output_enable) == 1 ? 2U     \
44 											  : 0U),   \
45 				.pmc_reg = 1,                                                      \
46 				.pfc_reg = RZT_GET_FUNC(DT_PROP_BY_IDX(node_id, prop, idx)),       \
47 				.drct_reg =                                                        \
48 					(DT_ENUM_IDX(node_id, drive_strength)) |                   \
49 					((DT_PROP(node_id, bias_pull_up) == 1                      \
50 						  ? 1U                                             \
51 						  : (DT_PROP(node_id, bias_pull_down) == 1 ? 2U    \
52 											   : 0))   \
53 					 << 2) |                                                   \
54 					(DT_PROP(node_id, input_schmitt_enable) << 4) |            \
55 					(DT_ENUM_IDX(node_id, slew_rate) << 5),                    \
56 				.rsel_reg = 1,                                                     \
57 				.reserved = 0,                                                     \
58 			},                                                                         \
59 	},
60 #define Z_PINCTRL_STATE_PINS_INIT(node_id, prop)                                                   \
61 	{DT_FOREACH_CHILD_VARGS(DT_PHANDLE(node_id, prop), DT_FOREACH_PROP_ELEM, pinmux,           \
62 				Z_PINCTRL_STATE_PIN_INIT)}
63 
64 #ifdef __cplusplus
65 }
66 #endif
67 
68 #endif /*ZEPHYR_SOC_RENESAS_RZ_COMMON_PINCTRL_RZT_H_*/
69