1 /*
2  * Copyright (c) 2023 Tokita, Hiroshi <tokita.hiroshi@fujitsu.com>
3  * Copyright (c) 2023 Yonatan Schachter
4  * Copyright (c) 2023 Ionut Pavel <iocapa@iocapa.com>
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #ifndef ZEPHYR_DRIVERS_MISC_PIO_PICO_RPI_PIO_PICO_RPI_H_
10 #define ZEPHYR_DRIVERS_MISC_PIO_PICO_RPI_PIO_PICO_RPI_H_
11 
12 #include <zephyr/devicetree/gpio.h>
13 
14 #include <hardware/pio.h>
15 
16 /**
17  * @brief Utility macro to define a PIO program. The program is a list
18  *        of 16 bit instructions, generated by the pioasm tool.
19  *
20  * @param name Name of the program.
21  * @param wrap_target Wrap target as specified by the PIO program.
22  * @param wrap Wrap source as specified by the PIO program.
23  * @param ... Comma separated list of PIO instructions.
24  */
25 #define RPI_PICO_PIO_DEFINE_PROGRAM(name, wrap_target, wrap, ...)	\
26 	static const uint32_t name ## _wrap_target = wrap_target;	\
27 	static const uint32_t name ## _wrap = wrap;			\
28 	static const uint16_t name ## _program_instructions[] = {	\
29 		__VA_ARGS__						\
30 	};								\
31 	static const struct pio_program name ## _program = {		\
32 		.instructions = name ## _program_instructions,		\
33 		.length = ARRAY_SIZE(name ## _program_instructions),	\
34 		.origin = -1,						\
35 	}
36 
37 /**
38  * @brief Utility macro to get the wrap target of a program.
39  *
40  * @param name Name of the program.
41  */
42 #define RPI_PICO_PIO_GET_WRAP_TARGET(name) name ## _wrap_target
43 
44 /**
45  * @brief Utility macro to get the wrap source of a program.
46  *
47  * @param name Name of the program.
48  */
49 #define RPI_PICO_PIO_GET_WRAP(name) name ## _wrap
50 
51 /**
52  * @brief Utility macro to get a pointer to a PIO program.
53  *
54  * @param name Name of the program.
55  */
56 #define RPI_PICO_PIO_GET_PROGRAM(name) &name ## _program
57 
58 /**
59  * @brief Get a pin number from a pinctrl / group name and index
60  *
61  * Example devicetree fragment(s):
62  *
63  * @code{.dts}
64  *	pinctrl {
65  *		pio_child_default: pio_child_default {
66  *			tx_gpio {
67  *				pinmux = <PIO0_P0>, <PIO0_P2>;
68  *			};
69  *
70  *			rx_gpio {
71  *				pinmux = <PIO0_P1>;
72  *				input-enable;
73  *			};
74  *		};
75  *	};
76  * @endcode
77  *
78  * @code{.dts}
79  *	pio {
80  *		status = "okay";
81  *		c: child {
82  *			pinctrl-0 = <&pio_child_default>;
83  *			pinctrl-names = "default";
84  *		};
85  *	};
86  * @endcode
87  *
88  * Example usage:
89  *
90  * @code{.c}
91  *	DT_RPI_PICO_PIO_PIN_BY_NAME(node, default, 0, tx_gpio, 0) // 0
92  *	DT_RPI_PICO_PIO_PIN_BY_NAME(node, default, 0, tx_gpio, 1) // 2
93  *	DT_RPI_PICO_PIO_PIN_BY_NAME(node, default, 0, rx_gpio, 0) // 1
94  * @endcode
95  *
96  * @param node_id node identifier
97  * @param p_name pinctrl name
98  * @param p_idx pinctrl index
99  * @param g_name group name
100  * @param g_idx group index
101  * @return pin number
102  */
103 #define DT_RPI_PICO_PIO_PIN_BY_NAME(node_id, p_name, p_idx, g_name, g_idx)			\
104 	RP2_GET_PIN_NUM(DT_PROP_BY_IDX(								\
105 		DT_CHILD(DT_PINCTRL_BY_NAME(node_id, p_name, p_idx), g_name), pinmux, g_idx))
106 
107 /**
108  * @brief Get a pin number from a pinctrl / group name and index
109  *
110  * @param inst instance number
111  * @param p_name pinctrl name
112  * @param p_idx pinctrl index
113  * @param g_name group name
114  * @param g_idx group index
115  * @return pin number
116  *
117  * @see DT_RPI_PICO_PIO_PIN_BY_NAME
118  */
119 #define DT_INST_RPI_PICO_PIO_PIN_BY_NAME(inst, p_name, p_idx, g_name, g_idx)			\
120 	DT_RPI_PICO_PIO_PIN_BY_NAME(DT_DRV_INST(inst), p_name, p_idx, g_name, g_idx)
121 
122 /**
123  * @brief Get the pin number of a pin by its name.
124  *
125  * @param inst instance number
126  * @param name name of the pin (e.g. tx, rx, sck).
127  */
128 #define DT_INST_PIO_PIN_BY_NAME(inst, name) \
129 	DT_PIO_PIN_BY_NAME(DT_DRV_INST(inst), name)
130 
131 /**
132  * Get PIO object
133  *
134  * @param dev Pointer to device structure for rpi_pio device instance
135  * @return PIO object
136  */
137 PIO pio_rpi_pico_get_pio(const struct device *dev);
138 
139 /**
140  * Allocate a state machine.
141  *
142  * @param dev Pointer to device structure for rpi_pio device instance
143  * @param sm Pointer to store allocated state machine
144  * @retval 0 on success
145  * @retval -EBUSY if no state machines were available
146  */
147 int pio_rpi_pico_allocate_sm(const struct device *dev, size_t *sm);
148 
149 #endif /* ZEPHYR_DRIVERS_MISC_PIO_PICO_RPI_PIO_PICO_RPI_H_ */
150