1 /*
2  * SPDX-License-Identifier: Apache-2.0
3  * Copyright (c) 2024 sensry.io
4  */
5 
6 #ifndef GANYMED_SY1XX_PINCTRL_SOC_H
7 #define GANYMED_SY1XX_PINCTRL_SOC_H
8 
9 #include <stdint.h>
10 
11 #include <zephyr/devicetree.h>
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 #define SY1XX_SCHMITT_ENABLE   1U
18 #define SY1XX_PULL_UP_ENABLE   1U
19 #define SY1XX_PULL_DOWN_ENABLE 1U
20 #define SY1XX_TRISTATE_ENABLE  1U
21 #define SY1XX_OUTPUT_ENABLE    1U
22 
23 #define SY1XX_PAD_SCHMITT_OFFS   7
24 #define SY1XX_PAD_PULL_UP_OFFS   5
25 #define SY1XX_PAD_PULL_DOWN_OFFS 4
26 #define SY1XX_PAD_DRIVE_OFFS     2
27 #define SY1XX_PAD_TRISTATE_OFFS  1
28 #define SY1XX_PAD_DIR_OFFS       0
29 
30 /** Type for SY1XX pin. */
31 typedef struct {
32 	/** address of pin config register */
33 	uint32_t addr;
34 	/** intra register offset (8bit cfg per pin) */
35 	uint32_t iro;
36 	/** config for pin (8bit), describes pull-up/down, ... */
37 	uint32_t cfg;
38 } pinctrl_soc_pin_t;
39 
40 #define Z_PINCTRL_CFG(node)                                                                        \
41 	(                                                                                          \
42                                                                                                    \
43 		(SY1XX_SCHMITT_ENABLE * DT_PROP(node, input_schmitt_enable))                       \
44 			<< SY1XX_PAD_SCHMITT_OFFS |                                                \
45 		(SY1XX_PULL_UP_ENABLE * DT_PROP(node, bias_pull_up)) << SY1XX_PAD_PULL_UP_OFFS |   \
46 		(SY1XX_PULL_DOWN_ENABLE * DT_PROP(node, bias_pull_down))                           \
47 			<< SY1XX_PAD_PULL_DOWN_OFFS |                                              \
48 		(SY1XX_TRISTATE_ENABLE * DT_PROP(node, bias_high_impedance))                       \
49 			<< SY1XX_PAD_TRISTATE_OFFS |                                               \
50 		(SY1XX_OUTPUT_ENABLE & (1 - DT_PROP(node, input_enable))) << SY1XX_PAD_DIR_OFFS    \
51                                                                                                    \
52 	)
53 
54 #define Z_PINCTRL_STATE_PIN_INIT(node, pr, idx)                                                    \
55 	{                                                                                          \
56                                                                                                    \
57 		.addr = DT_PROP_BY_IDX(DT_PHANDLE_BY_IDX(node, pr, idx), pinmux, 0),               \
58 		.iro = DT_PROP_BY_IDX(DT_PHANDLE_BY_IDX(node, pr, idx), pinmux, 1),                \
59 		.cfg = Z_PINCTRL_CFG(DT_PHANDLE_BY_IDX(node, pr, idx))                             \
60                                                                                                    \
61 	},
62 
63 #define Z_PINCTRL_STATE_PINS_INIT(node_id, prop)                                                   \
64 	{                                                                                          \
65 		DT_FOREACH_PROP_ELEM(node_id, prop, Z_PINCTRL_STATE_PIN_INIT)                      \
66 	}
67 
68 #ifdef __cplusplus
69 }
70 #endif
71 
72 #endif /* GANYMED_SY1XX_PINCTRL_SOC_H */
73