1 /*
2  * Copyright (c) 2024 Renesas Electronics Corporation
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #ifndef ZEPHYR_SOC_RENESAS_RZ_COMMON_PINCTRL_RZG_H_
6 #define ZEPHYR_SOC_RENESAS_RZ_COMMON_PINCTRL_RZG_H_
7 
8 #include <zephyr/devicetree.h>
9 #include <zephyr/sys/util.h>
10 #include <zephyr/types.h>
11 #include "r_ioport.h"
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 /*Porting*/
18 typedef struct pinctrl_cfg_data_t {
19 	uint32_t reserved: 4;
20 	uint32_t pupd_reg: 6;
21 	uint32_t iolh_reg: 6;
22 	uint32_t pmc_reg: 2;
23 	uint32_t ien_reg: 1;
24 	uint32_t filonoff_reg: 1;
25 	uint32_t filnum_reg: 2;
26 	uint32_t filclksel_reg: 2;
27 	uint32_t pfc_reg: 3;
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 /* Iterate over each pinctrl-n phandle child */
36 #define Z_PINCTRL_STATE_PIN_INIT(node_id, state_prop, idx)                                         \
37 	DT_FOREACH_CHILD(DT_PHANDLE_BY_IDX(node_id, state_prop, idx),                              \
38 			 Z_PINCTRL_STATE_PIN_CHILD_INIT)
39 
40 /* Iterate over each pinctrl-n phandle child */
41 #define Z_PINCTRL_STATE_PINS_INIT(node_id, prop)                                                   \
42 	{DT_FOREACH_PROP_ELEM_SEP(node_id, prop, Z_PINCTRL_STATE_PIN_INIT, ())};
43 
44 #define Z_PINCTRL_STATE_PIN_CHILD_INIT(node_id)                                                    \
45 	COND_CODE_1(DT_NODE_HAS_PROP(node_id, pinmux), \
46 		    (DT_FOREACH_PROP_ELEM(node_id, pinmux, Z_PINCTRL_PINMUX_INIT)), \
47 		    ())                                           \
48 	COND_CODE_1(DT_NODE_HAS_PROP(node_id, pins), \
49 		    (DT_FOREACH_PROP_ELEM(node_id, pins, Z_PINCTRL_SPECIAL_PINS_INIT)), \
50 		    ())
51 
52 #define RZG_GET_PORT_PIN(pinmux) (pinmux & ~(0xF << 4))
53 #define RZG_GET_FUNC(pinmux)     ((pinmux & 0xF0) >> 4)
54 
55 #define RZG_GET_PU_PD(node_id)                                                                     \
56 	DT_PROP(node_id, bias_pull_up) == 1 ? 1U : (DT_PROP(node_id, bias_pull_down) == 1 ? 2U : 0U)
57 
58 #define RZG_GET_FILNUM(node_id) ((DT_PROP(node_id, renesas_filter) >> 2) & 0x3)
59 
60 #define RZG_GET_FILCLKSEL(node_id) (DT_PROP(node_id, renesas_filter) & 0x3)
61 
62 #define RZG_FILTER_ON_OFF(node_id) COND_CODE_0(DT_PROP(node_id, renesas_filter), (0), (1))
63 
64 /* Process pinmux cfg */
65 #define Z_PINCTRL_PINMUX_INIT(node_id, state_prop, idx)                                            \
66 	{                                                                                          \
67 		.port_pin = RZG_GET_PORT_PIN(DT_PROP_BY_IDX(node_id, state_prop, idx)),            \
68 		.config =                                                                          \
69 			{                                                                          \
70 				.reserved = 0,                                                     \
71 				.pupd_reg = RZG_GET_PU_PD(node_id),                                \
72 				.iolh_reg = DT_PROP(node_id, drive_strength),                      \
73 				.pmc_reg = 1,                                                      \
74 				.ien_reg = DT_PROP(node_id, input_enable),                         \
75 				.filonoff_reg = RZG_FILTER_ON_OFF(node_id),                        \
76 				.filnum_reg = RZG_GET_FILNUM(node_id),                             \
77 				.filclksel_reg = RZG_GET_FILCLKSEL(node_id),                       \
78 				.pfc_reg =                                                         \
79 					(RZG_GET_FUNC(DT_PROP_BY_IDX(node_id, state_prop, idx)) -  \
80 					 1),                                                       \
81 			},                                                                         \
82 	},
83 
84 #define Z_PINCTRL_SPECIAL_PINS_INIT(node_id, state_prop, idx)                                      \
85 	{                                                                                          \
86 		.port_pin = DT_PROP_BY_IDX(node_id, state_prop, idx),                              \
87 		.config =                                                                          \
88 			{                                                                          \
89 				.reserved = 0,                                                     \
90 				.pupd_reg = RZG_GET_PU_PD(node_id),                                \
91 				.iolh_reg = DT_PROP(node_id, drive_strength),                      \
92 				.pmc_reg = 0,                                                      \
93 				.ien_reg = DT_PROP(node_id, input_enable),                         \
94 				.filonoff_reg = RZG_FILTER_ON_OFF(node_id),                        \
95 				.filnum_reg = RZG_GET_FILNUM(node_id),                             \
96 				.filclksel_reg = RZG_GET_FILCLKSEL(node_id),                       \
97 				.pfc_reg = 0,                                                      \
98 			},                                                                         \
99 	},
100 
101 #ifdef __cplusplus
102 }
103 #endif
104 #endif /*ZEPHYR_SOC_RENESAS_RZ_COMMON_PINCTRL_RZG_H_*/
105