device property: Implement fwnode_graph_get_endpoint_count()
authorSakari Ailus <sakari.ailus@linux.intel.com>
Wed, 1 Dec 2021 12:59:33 +0000 (14:59 +0200)
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>
Fri, 17 Dec 2021 17:41:29 +0000 (18:41 +0100)
Add fwnode_graph_get_endpoint_count() function to provide generic
implementation of of_graph_get_endpoint_count(). The former by default
only counts endpoints to available devices which is consistent with the
rest of the fwnode graph API. By providing FWNODE_GRAPH_DEVICE_DISABLED
flag, also unconnected endpoints and endpoints to disabled devices are
counted.

Signed-off-by: Sakari Ailus <sakari.ailus@linux.intel.com>
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
drivers/base/property.c
include/linux/property.h

index e2860ab..39435ba 100644 (file)
@@ -1049,6 +1049,18 @@ fwnode_graph_get_remote_node(const struct fwnode_handle *fwnode, u32 port_id,
 }
 EXPORT_SYMBOL_GPL(fwnode_graph_get_remote_node);
 
+static bool fwnode_graph_remote_available(struct fwnode_handle *ep)
+{
+       struct fwnode_handle *dev_node;
+       bool available;
+
+       dev_node = fwnode_graph_get_remote_port_parent(ep);
+       available = fwnode_device_is_available(dev_node);
+       fwnode_handle_put(dev_node);
+
+       return available;
+}
+
 /**
  * fwnode_graph_get_endpoint_by_id - get endpoint by port and endpoint numbers
  * @fwnode: parent fwnode_handle containing the graph
@@ -1082,16 +1094,8 @@ fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
                struct fwnode_endpoint fwnode_ep = { 0 };
                int ret;
 
-               if (enabled_only) {
-                       struct fwnode_handle *dev_node;
-                       bool available;
-
-                       dev_node = fwnode_graph_get_remote_port_parent(ep);
-                       available = fwnode_device_is_available(dev_node);
-                       fwnode_handle_put(dev_node);
-                       if (!available)
-                               continue;
-               }
+               if (enabled_only && !fwnode_graph_remote_available(ep))
+                       continue;
 
                ret = fwnode_graph_parse_endpoint(ep, &fwnode_ep);
                if (ret < 0)
@@ -1125,6 +1129,31 @@ fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
 EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_by_id);
 
 /**
+ * fwnode_graph_get_endpoint_count - Count endpoints on a device node
+ * @fwnode: The node related to a device
+ * @flags: fwnode lookup flags
+ * Count endpoints in a device node.
+ *
+ * If FWNODE_GRAPH_DEVICE_DISABLED flag is specified, also unconnected endpoints
+ * and endpoints connected to disabled devices are counted.
+ */
+unsigned int fwnode_graph_get_endpoint_count(struct fwnode_handle *fwnode,
+                                            unsigned long flags)
+{
+       struct fwnode_handle *ep;
+       unsigned int count = 0;
+
+       fwnode_graph_for_each_endpoint(fwnode, ep) {
+               if (flags & FWNODE_GRAPH_DEVICE_DISABLED ||
+                   fwnode_graph_remote_available(ep))
+                       count++;
+       }
+
+       return count;
+}
+EXPORT_SYMBOL_GPL(fwnode_graph_get_endpoint_count);
+
+/**
  * fwnode_graph_parse_endpoint - parse common endpoint node properties
  * @fwnode: pointer to endpoint fwnode_handle
  * @endpoint: pointer to the fwnode endpoint data structure
index 7a2df45..8c01048 100644 (file)
@@ -423,6 +423,8 @@ static inline bool fwnode_graph_is_endpoint(struct fwnode_handle *fwnode)
 struct fwnode_handle *
 fwnode_graph_get_endpoint_by_id(const struct fwnode_handle *fwnode,
                                u32 port, u32 endpoint, unsigned long flags);
+unsigned int fwnode_graph_get_endpoint_count(struct fwnode_handle *fwnode,
+                                            unsigned long flags);
 
 #define fwnode_graph_for_each_endpoint(fwnode, child)                  \
        for (child = NULL;                                              \