Skip to content

Commit 58fe47d

Browse files
morimotorobherring
authored andcommitted
of: property: add of_graph_get_next_port_endpoint()
We already have of_graph_get_next_endpoint(), but it is not intuitive to use in some case. (X) node { (Y) ports { (P0) port@0 { endpoint { remote-endpoint = ...; };}; (P10) port@1 { endpoint { remote-endpoint = ...; }; (P11) endpoint { remote-endpoint = ...; };}; (P2) port@2 { endpoint { remote-endpoint = ...; };}; }; }; For example, if I want to handle port@1's 2 endpoints (= P10, P11), I want to use like below P10 = of_graph_get_next_endpoint(port1, NULL); P11 = of_graph_get_next_endpoint(port1, P10); But 1st one will be error, because of_graph_get_next_endpoint() requested 1st parameter is "node" (X) or "ports" (Y), not but "port". Below works well, but it will get P0 P0 = of_graph_get_next_endpoint(node, NULL); P0 = of_graph_get_next_endpoint(ports, NULL); In other words, we can't handle P10/P11 directly via of_graph_get_next_endpoint(). There is another non intuitive behavior on of_graph_get_next_endpoint(). In case of if I could get P10 pointer for some way, and if I want to handle port@1 things by loop, I would like use it like below /* * "ep" is now P10, and handle port1 things here, * but we don't know how many endpoints port1 have. * * Because "ep" is non NULL now, we can use port1 * as of_graph_get_next_endpoint(port1, xxx) */ do { /* do something for port1 specific things here */ } while (ep = of_graph_get_next_endpoint(port1, ep)) But it also not worked as I expected. I expect it will be P10 -> P11 -> NULL, but it will be P10 -> P11 -> P2, because of_graph_get_next_endpoint() will fetch "endpoint" beyond the "port". It is not useful for generic driver. To handle endpoint more intuitive, create of_graph_get_next_port_endpoint() of_graph_get_next_port_endpoint(port1, NULL); // P10 of_graph_get_next_port_endpoint(port1, P10); // P11 of_graph_get_next_port_endpoint(port1, P11); // NULL Signed-off-by: Kuninori Morimoto <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Rob Herring (Arm) <[email protected]>
1 parent 02ac5f9 commit 58fe47d

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

drivers/of/property.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,33 @@ struct device_node *of_graph_get_next_port(const struct device_node *parent,
667667
}
668668
EXPORT_SYMBOL(of_graph_get_next_port);
669669

670+
/**
671+
* of_graph_get_next_port_endpoint() - get next endpoint node in port.
672+
* If it reached to end of the port, it will return NULL.
673+
* @port: pointer to the target port node
674+
* @prev: previous endpoint node, or NULL to get first
675+
*
676+
* Return: An 'endpoint' node pointer with refcount incremented. Refcount
677+
* of the passed @prev node is decremented.
678+
*/
679+
struct device_node *of_graph_get_next_port_endpoint(const struct device_node *port,
680+
struct device_node *prev)
681+
{
682+
while (1) {
683+
prev = of_get_next_child(port, prev);
684+
if (!prev)
685+
break;
686+
if (WARN(!of_node_name_eq(prev, "endpoint"),
687+
"non endpoint node is used (%pOF)", prev))
688+
continue;
689+
690+
break;
691+
}
692+
693+
return prev;
694+
}
695+
EXPORT_SYMBOL(of_graph_get_next_port_endpoint);
696+
670697
/**
671698
* of_graph_get_next_endpoint() - get next endpoint node
672699
* @parent: pointer to the parent device node

include/linux/of_graph.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ struct of_endpoint {
5050
for (struct device_node *child __free(device_node) = of_graph_get_next_port(parent, NULL);\
5151
child != NULL; child = of_graph_get_next_port(parent, child))
5252

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+
5365
#ifdef CONFIG_OF
5466
bool of_graph_is_present(const struct device_node *node);
5567
int of_graph_parse_endpoint(const struct device_node *node,
@@ -61,6 +73,8 @@ struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
6173
struct device_node *previous);
6274
struct device_node *of_graph_get_next_port(const struct device_node *parent,
6375
struct device_node *port);
76+
struct device_node *of_graph_get_next_port_endpoint(const struct device_node *port,
77+
struct device_node *prev);
6478
struct device_node *of_graph_get_endpoint_by_regs(
6579
const struct device_node *parent, int port_reg, int reg);
6680
struct device_node *of_graph_get_remote_endpoint(
@@ -114,6 +128,13 @@ static inline struct device_node *of_graph_get_next_port(
114128
return NULL;
115129
}
116130

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+
117138
static inline struct device_node *of_graph_get_endpoint_by_regs(
118139
const struct device_node *parent, int port_reg, int reg)
119140
{

0 commit comments

Comments
 (0)