Skip to content

Commit 02ac5f9

Browse files
morimotorobherring
authored andcommitted
of: property: add of_graph_get_next_port()
We have endpoint base functions - of_graph_get_next_endpoint() - of_graph_get_endpoint_count() - for_each_endpoint_of_node() Here, for_each_endpoint_of_node() loop finds each endpoints ports { port@0 { (1) endpoint {...}; }; port@1 { (2) endpoint {...}; }; ... }; In above case, it finds endpoint as (1) -> (2) -> ... Basically, user/driver knows which port is used for what, but not in all cases. For example on flexible/generic driver case, how many ports are used is not fixed. For example Sound Generic Card driver which is very flexible/generic and used from many venders can't know how many ports are used, and used for what, because it depends on each vender SoC and/or its used board. And more, the port can have multi endpoints. For example Generic Sound Card case, it supports many type of connection between CPU / Codec, and some of them uses multi endpoint in one port. see below. ports { (A) port@0 { (1) endpoint@0 {...}; (2) endpoint@1 {...}; }; (B) port@1 { (3) endpoint {...}; }; ... }; Generic Sound Card want to handle each connection via "port" base instead of "endpoint" base. But, it is very difficult to handle each "port" via existing for_each_endpoint_of_node(). Because getting each "port" via of_get_parent() from each "endpoint" doesn't work. For example in above case, both (1) (2) endpoint has same "port" (= A). Add "port" base functions. 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 2e03091 commit 02ac5f9

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

drivers/of/property.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,43 @@ struct device_node *of_graph_get_port_by_id(struct device_node *parent, u32 id)
630630
}
631631
EXPORT_SYMBOL(of_graph_get_port_by_id);
632632

633+
/**
634+
* of_graph_get_next_port() - get next port node.
635+
* @parent: pointer to the parent device node, or parent ports node
636+
* @prev: previous port node, or NULL to get first
637+
*
638+
* Parent device node can be used as @parent whether device node has ports node
639+
* or not. It will work same as ports@0 node.
640+
*
641+
* Return: A 'port' node pointer with refcount incremented. Refcount
642+
* of the passed @prev node is decremented.
643+
*/
644+
struct device_node *of_graph_get_next_port(const struct device_node *parent,
645+
struct device_node *prev)
646+
{
647+
if (!parent)
648+
return NULL;
649+
650+
if (!prev) {
651+
struct device_node *node __free(device_node) =
652+
of_get_child_by_name(parent, "ports");
653+
654+
if (node)
655+
parent = node;
656+
657+
return of_get_child_by_name(parent, "port");
658+
}
659+
660+
do {
661+
prev = of_get_next_child(parent, prev);
662+
if (!prev)
663+
break;
664+
} while (!of_node_name_eq(prev, "port"));
665+
666+
return prev;
667+
}
668+
EXPORT_SYMBOL(of_graph_get_next_port);
669+
633670
/**
634671
* of_graph_get_next_endpoint() - get next endpoint node
635672
* @parent: pointer to the parent device node
@@ -823,6 +860,23 @@ unsigned int of_graph_get_endpoint_count(const struct device_node *np)
823860
}
824861
EXPORT_SYMBOL(of_graph_get_endpoint_count);
825862

863+
/**
864+
* of_graph_get_port_count() - get the number of port in a device or ports node
865+
* @np: pointer to the device or ports node
866+
*
867+
* Return: count of port of this device or ports node
868+
*/
869+
unsigned int of_graph_get_port_count(struct device_node *np)
870+
{
871+
unsigned int num = 0;
872+
873+
for_each_of_graph_port(np, port)
874+
num++;
875+
876+
return num;
877+
}
878+
EXPORT_SYMBOL(of_graph_get_port_count);
879+
826880
/**
827881
* of_graph_get_remote_node() - get remote parent device_node for given port/endpoint
828882
* @node: pointer to parent device_node containing graph port/endpoint

include/linux/of_graph.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#ifndef __LINUX_OF_GRAPH_H
1212
#define __LINUX_OF_GRAPH_H
1313

14+
#include <linux/cleanup.h>
1415
#include <linux/types.h>
1516
#include <linux/errno.h>
1617

@@ -37,14 +38,29 @@ struct of_endpoint {
3738
for (child = of_graph_get_next_endpoint(parent, NULL); child != NULL; \
3839
child = of_graph_get_next_endpoint(parent, child))
3940

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+
4053
#ifdef CONFIG_OF
4154
bool of_graph_is_present(const struct device_node *node);
4255
int of_graph_parse_endpoint(const struct device_node *node,
4356
struct of_endpoint *endpoint);
4457
unsigned int of_graph_get_endpoint_count(const struct device_node *np);
58+
unsigned int of_graph_get_port_count(struct device_node *np);
4559
struct device_node *of_graph_get_port_by_id(struct device_node *node, u32 id);
4660
struct device_node *of_graph_get_next_endpoint(const struct device_node *parent,
4761
struct device_node *previous);
62+
struct device_node *of_graph_get_next_port(const struct device_node *parent,
63+
struct device_node *port);
4864
struct device_node *of_graph_get_endpoint_by_regs(
4965
const struct device_node *parent, int port_reg, int reg);
5066
struct device_node *of_graph_get_remote_endpoint(
@@ -73,6 +89,11 @@ static inline unsigned int of_graph_get_endpoint_count(const struct device_node
7389
return 0;
7490
}
7591

92+
static inline unsigned int of_graph_get_port_count(struct device_node *np)
93+
{
94+
return 0;
95+
}
96+
7697
static inline struct device_node *of_graph_get_port_by_id(
7798
struct device_node *node, u32 id)
7899
{
@@ -86,6 +107,13 @@ static inline struct device_node *of_graph_get_next_endpoint(
86107
return NULL;
87108
}
88109

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

0 commit comments

Comments
 (0)