From: Simon Glass <sjg@chromium.org>
Date: Sat, 23 Oct 2021 23:26:09 +0000 (-0600)
Subject: dm: core: Add a way to count the devices in a uclass
X-Git-Tag: v2025.01-rc5-pxa1908~1591^2~18^2~3
X-Git-Url: http://git.dujemihanovic.xyz/img/html/static/git-logo.png?a=commitdiff_plain;h=29fe555dec4c18096ab1ddd51398317160359ba1;p=u-boot.git

dm: core: Add a way to count the devices in a uclass

Add a function that returns the number of devices in a uclass. This can be
helpful in sizing an array that needs to hold a list of them.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

diff --git a/drivers/core/uclass.c b/drivers/core/uclass.c
index 3de5f27fe4..2aa2143077 100644
--- a/drivers/core/uclass.c
+++ b/drivers/core/uclass.c
@@ -794,6 +794,18 @@ int uclass_probe_all(enum uclass_id id)
 	return 0;
 }
 
+int uclass_id_count(enum uclass_id id)
+{
+	struct udevice *dev;
+	struct uclass *uc;
+	int count = 0;
+
+	uclass_id_foreach_dev(id, dev, uc)
+		count++;
+
+	return count;
+}
+
 UCLASS_DRIVER(nop) = {
 	.id		= UCLASS_NOP,
 	.name		= "nop",
diff --git a/include/dm/uclass.h b/include/dm/uclass.h
index aea2f34fce..f1fd2ba246 100644
--- a/include/dm/uclass.h
+++ b/include/dm/uclass.h
@@ -425,6 +425,14 @@ int uclass_first_device_drvdata(enum uclass_id id, ulong driver_data,
  */
 int uclass_probe_all(enum uclass_id id);
 
+/**
+ * uclass_id_count() - Count the number of devices in a uclass
+ *
+ * @id: uclass ID to look up
+ * @return number of devices in that uclass (0 if none)
+ */
+int uclass_id_count(enum uclass_id id);
+
 /**
  * uclass_id_foreach_dev() - Helper function to iteration through devices
  *