xref: /linux-6.15/include/linux/of_graph.h (revision 02ac5f9d)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * OF graph binding parsing helpers
4  *
5  * Copyright (C) 2012 - 2013 Samsung Electronics Co., Ltd.
6  * Author: Sylwester Nawrocki <[email protected]>
7  *
8  * Copyright (C) 2012 Renesas Electronics Corp.
9  * Author: Guennadi Liakhovetski <[email protected]>
10  */
11 #ifndef __LINUX_OF_GRAPH_H
12 #define __LINUX_OF_GRAPH_H
13 
14 #include <linux/cleanup.h>
15 #include <linux/types.h>
16 #include <linux/errno.h>
17 
18 /**
19  * struct of_endpoint - the OF graph endpoint data structure
20  * @port: identifier (value of reg property) of a port this endpoint belongs to
21  * @id: identifier (value of reg property) of this endpoint
22  * @local_node: pointer to device_node of this endpoint
23  */
24 struct of_endpoint {
25 	unsigned int port;
26 	unsigned int id;
27 	const struct device_node *local_node;
28 };
29 
30 /**
31  * for_each_endpoint_of_node - iterate over every endpoint in a device node
32  * @parent: parent device node containing ports and endpoints
33  * @child: loop variable pointing to the current endpoint node
34  *
35  * When breaking out of the loop, of_node_put(child) has to be called manually.
36  */
37 #define for_each_endpoint_of_node(parent, child) \
38 	for (child = of_graph_get_next_endpoint(parent, NULL); child != NULL; \
39 	     child = of_graph_get_next_endpoint(parent, child))
40 
41 /**
42  * for_each_of_graph_port - iterate over every port in a device or ports node
43  * @parent: parent device or ports node containing port
44  * @child: loop variable pointing to the current port node
45  *
46  * When breaking out of the loop, and continue to use the @child, you need to
47  * use return_ptr(@child) or no_free_ptr(@child) not to call __free() for it.
48  */
49 #define for_each_of_graph_port(parent, child)			\
50 	for (struct device_node *child __free(device_node) = of_graph_get_next_port(parent, NULL);\
51 	     child != NULL; child = of_graph_get_next_port(parent, child))
52 
53 #ifdef CONFIG_OF
54 bool of_graph_is_present(const struct device_node *node);
55 int of_graph_parse_endpoint(const struct device_node *node,
56 				struct of_endpoint *endpoint);
57 unsigned int of_graph_get_endpoint_count(const struct device_node *np);
58 unsigned int of_graph_get_port_count(struct device_node *np);
59 struct device_node *of_graph_get_port_by_id(struct device_node *node, u32 id);
60 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
61 					struct device_node *previous);
62 struct device_node *of_graph_get_next_port(const struct device_node *parent,
63 					   struct device_node *port);
64 struct device_node *of_graph_get_endpoint_by_regs(
65 		const struct device_node *parent, int port_reg, int reg);
66 struct device_node *of_graph_get_remote_endpoint(
67 					const struct device_node *node);
68 struct device_node *of_graph_get_port_parent(struct device_node *node);
69 struct device_node *of_graph_get_remote_port_parent(
70 					const struct device_node *node);
71 struct device_node *of_graph_get_remote_port(const struct device_node *node);
72 struct device_node *of_graph_get_remote_node(const struct device_node *node,
73 					     u32 port, u32 endpoint);
74 #else
75 
76 static inline bool of_graph_is_present(const struct device_node *node)
77 {
78 	return false;
79 }
80 
81 static inline int of_graph_parse_endpoint(const struct device_node *node,
82 					struct of_endpoint *endpoint)
83 {
84 	return -ENOSYS;
85 }
86 
87 static inline unsigned int of_graph_get_endpoint_count(const struct device_node *np)
88 {
89 	return 0;
90 }
91 
92 static inline unsigned int of_graph_get_port_count(struct device_node *np)
93 {
94 	return 0;
95 }
96 
97 static inline struct device_node *of_graph_get_port_by_id(
98 					struct device_node *node, u32 id)
99 {
100 	return NULL;
101 }
102 
103 static inline struct device_node *of_graph_get_next_endpoint(
104 					const struct device_node *parent,
105 					struct device_node *previous)
106 {
107 	return NULL;
108 }
109 
110 static inline struct device_node *of_graph_get_next_port(
111 					const struct device_node *parent,
112 					struct device_node *previous)
113 {
114 	return NULL;
115 }
116 
117 static inline struct device_node *of_graph_get_endpoint_by_regs(
118 		const struct device_node *parent, int port_reg, int reg)
119 {
120 	return NULL;
121 }
122 
123 static inline struct device_node *of_graph_get_remote_endpoint(
124 					const struct device_node *node)
125 {
126 	return NULL;
127 }
128 
129 static inline struct device_node *of_graph_get_port_parent(
130 	struct device_node *node)
131 {
132 	return NULL;
133 }
134 
135 static inline struct device_node *of_graph_get_remote_port_parent(
136 					const struct device_node *node)
137 {
138 	return NULL;
139 }
140 
141 static inline struct device_node *of_graph_get_remote_port(
142 					const struct device_node *node)
143 {
144 	return NULL;
145 }
146 static inline struct device_node *of_graph_get_remote_node(
147 					const struct device_node *node,
148 					u32 port, u32 endpoint)
149 {
150 	return NULL;
151 }
152 
153 #endif /* CONFIG_OF */
154 
155 #endif /* __LINUX_OF_GRAPH_H */
156