From 7cb7dd277abdf84b9c9a3fbca196fcd39df14975 Mon Sep 17 00:00:00 2001 From: Ioannis Papamanoglou Date: Thu, 12 Mar 2020 10:44:42 +0100 Subject: [PATCH] devicetree.h: Add macro DT_INST_FOREACH With this macro device drivers can call macros and functions on every device instance compatible to that driver. This makes it possible to make drivers agnostic to the potential counts of instances. Sidenote: Introduces helper macro DT_CALL_WITH_ARG. Signed-off-by: Ioannis Papamanoglou --- include/devicetree.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/include/devicetree.h b/include/devicetree.h index 05d343a1f36..b4f08260a69 100644 --- a/include/devicetree.h +++ b/include/devicetree.h @@ -1331,6 +1331,22 @@ #define DT_ANY_INST_ON_BUS(bus) \ (UTIL_LISTIFY(DT_NUM_INST(DT_DRV_COMPAT), DT_INST_ON_BUS_OR, bus) 0) +/** + * @def DT_INST_FOREACH + * + * @brief Call specified macro for all nodes with compatible DT_DRV_COMPAT + * + * @details This macro will scan for all DT_INST_ device nodes for that driver. + * The macro then calls the supplied macro with the instance number. + * This macro can be used for example to call the init macro of a driver + * for each device specified in the device tree. + * + * @param inst_expr Macro or function that is called for each device node. + * Has to accept instance_number as only parameter. + */ +#define DT_INST_FOREACH(inst_expr) \ + UTIL_LISTIFY(DT_NUM_INST(DT_DRV_COMPAT), DT_CALL_WITH_ARG, inst_expr) + /** * @brief Does a DT_DRV_COMPAT instance have a property? * Equivalent to DT_NODE_HAS_PROP(DT_DRV_INST(inst), prop) @@ -1431,5 +1447,7 @@ #define DT_DASH_PREFIX(name) _##name /** @internal DT_ANY_INST_ON_BUS helper */ #define DT_INST_ON_BUS_OR(inst, bus) DT_ON_BUS(DT_DRV_INST(inst), bus) || +/** @internal DT_INST_FOREACH helper */ +#define DT_CALL_WITH_ARG(arg, expr) expr(arg); #endif /* DEVICETREE_H */