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 /**
54  * for_each_of_graph_port_endpoint - iterate over every endpoint in a port node
55  * @parent: parent port node
56  * @child: loop variable pointing to the current endpoint node
57  *
58  * When breaking out of the loop, and continue to use the @child, you need to
59  * use return_ptr(@child) or no_free_ptr(@child) not to call __free() for it.
60  */
61 #define for_each_of_graph_port_endpoint(parent, child)			\
62 	for (struct device_node *child __free(device_node) = of_graph_get_next_port_endpoint(parent, NULL);\
63 	     child != NULL; child = of_graph_get_next_port_endpoint(parent, child))
64 
65 #ifdef CONFIG_OF
66 bool of_graph_is_present(const struct device_node *node);
67 int of_graph_parse_endpoint(const struct device_node *node,
68 				struct of_endpoint *endpoint);
69 unsigned int of_graph_get_endpoint_count(const struct device_node *np);
70 unsigned int of_graph_get_port_count(struct device_node *np);
71 struct device_node *of_graph_get_port_by_id(struct device_node *node, u32 id);
72 struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
73 					struct device_node *previous);
74 struct device_node *of_graph_get_next_port(const struct device_node *parent,
75 					   struct device_node *port);
76 struct device_node *of_graph_get_next_port_endpoint(const struct device_node *port,
77 						    struct device_node *prev);
78 struct device_node *of_graph_get_endpoint_by_regs(
79 		const struct device_node *parent, int port_reg, int reg);
80 struct device_node *of_graph_get_remote_endpoint(
81 					const struct device_node *node);
82 struct device_node *of_graph_get_port_parent(struct device_node *node);
83 struct device_node *of_graph_get_remote_port_parent(
84 					const struct device_node *node);
85 struct device_node *of_graph_get_remote_port(const struct device_node *node);
86 struct device_node *of_graph_get_remote_node(const struct device_node *node,
87 					     u32 port, u32 endpoint);
88 #else
89 
of_graph_is_present(const struct device_node * node)90 static inline bool of_graph_is_present(const struct device_node *node)
91 {
92 	return false;
93 }
94 
of_graph_parse_endpoint(const struct device_node * node,struct of_endpoint * endpoint)95 static inline int of_graph_parse_endpoint(const struct device_node *node,
96 					struct of_endpoint *endpoint)
97 {
98 	return -ENOSYS;
99 }
100 
of_graph_get_endpoint_count(const struct device_node * np)101 static inline unsigned int of_graph_get_endpoint_count(const struct device_node *np)
102 {
103 	return 0;
104 }
105 
of_graph_get_port_count(struct device_node * np)106 static inline unsigned int of_graph_get_port_count(struct device_node *np)
107 {
108 	return 0;
109 }
110 
of_graph_get_port_by_id(struct device_node * node,u32 id)111 static inline struct device_node *of_graph_get_port_by_id(
112 					struct device_node *node, u32 id)
113 {
114 	return NULL;
115 }
116 
of_graph_get_next_endpoint(const struct device_node * parent,struct device_node * previous)117 static inline struct device_node *of_graph_get_next_endpoint(
118 					const struct device_node *parent,
119 					struct device_node *previous)
120 {
121 	return NULL;
122 }
123 
of_graph_get_next_port(const struct device_node * parent,struct device_node * previous)124 static inline struct device_node *of_graph_get_next_port(
125 					const struct device_node *parent,
126 					struct device_node *previous)
127 {
128 	return NULL;
129 }
130 
of_graph_get_next_port_endpoint(const struct device_node * parent,struct device_node * previous)131 static inline struct device_node *of_graph_get_next_port_endpoint(
132 					const struct device_node *parent,
133 					struct device_node *previous)
134 {
135 	return NULL;
136 }
137 
of_graph_get_endpoint_by_regs(const struct device_node * parent,int port_reg,int reg)138 static inline struct device_node *of_graph_get_endpoint_by_regs(
139 		const struct device_node *parent, int port_reg, int reg)
140 {
141 	return NULL;
142 }
143 
of_graph_get_remote_endpoint(const struct device_node * node)144 static inline struct device_node *of_graph_get_remote_endpoint(
145 					const struct device_node *node)
146 {
147 	return NULL;
148 }
149 
of_graph_get_port_parent(struct device_node * node)150 static inline struct device_node *of_graph_get_port_parent(
151 	struct device_node *node)
152 {
153 	return NULL;
154 }
155 
of_graph_get_remote_port_parent(const struct device_node * node)156 static inline struct device_node *of_graph_get_remote_port_parent(
157 					const struct device_node *node)
158 {
159 	return NULL;
160 }
161 
of_graph_get_remote_port(const struct device_node * node)162 static inline struct device_node *of_graph_get_remote_port(
163 					const struct device_node *node)
164 {
165 	return NULL;
166 }
of_graph_get_remote_node(const struct device_node * node,u32 port,u32 endpoint)167 static inline struct device_node *of_graph_get_remote_node(
168 					const struct device_node *node,
169 					u32 port, u32 endpoint)
170 {
171 	return NULL;
172 }
173 
174 #endif /* CONFIG_OF */
175 
176 #endif /* __LINUX_OF_GRAPH_H */
177