dm: core: Add a way to count the devices in a uclass
authorSimon Glass <sjg@chromium.org>
Sat, 23 Oct 2021 23:26:09 +0000 (17:26 -0600)
committerSimon Glass <sjg@chromium.org>
Sun, 28 Nov 2021 23:51:51 +0000 (16:51 -0700)
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>
drivers/core/uclass.c
include/dm/uclass.h

index 3de5f27..2aa2143 100644 (file)
@@ -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",
index aea2f34..f1fd2ba 100644 (file)
@@ -426,6 +426,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
  *
  * This creates a for() loop which works through the available devices in