From e425168b7c0a6b279edae9f3225d2604fd401ba7 Mon Sep 17 00:00:00 2001 From: Gerard Marull-Paretas Date: Wed, 17 Nov 2021 13:49:25 +0100 Subject: [PATCH] devicetree: add instance based macros for DT_ENUM_IDX(_OR) The DT_ENUM_IDX and DT_ENUM_IDX_OR macros did not have the instance based equivalents. With them many drivers can be simplified. Signed-off-by: Gerard Marull-Paretas --- include/devicetree.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/include/devicetree.h b/include/devicetree.h index ed28e4fdacb..62a3a56796a 100644 --- a/include/devicetree.h +++ b/include/devicetree.h @@ -2456,6 +2456,26 @@ #define DT_INST_FOREACH_CHILD_VARGS(inst, fn, ...) \ DT_FOREACH_CHILD_VARGS(DT_DRV_INST(inst), fn, __VA_ARGS__) +/** + * @brief Get a DT_DRV_COMPAT value's index into its enumeration values + * @param inst instance number + * @param prop lowercase-and-underscores property name + * @return zero-based index of the property's value in its enum: list + */ +#define DT_INST_ENUM_IDX(inst, prop) \ + DT_ENUM_IDX(DT_DRV_INST(inst), prop) + +/** + * @brief Like DT_INST_ENUM_IDX(), but with a fallback to a default enum index + * @param inst instance number + * @param prop lowercase-and-underscores property name + * @param default_idx_value a fallback index value to expand to + * @return zero-based index of the property's value in its enum if present, + * default_idx_value ohterwise + */ +#define DT_INST_ENUM_IDX_OR(inst, prop, default_idx_value) \ + DT_ENUM_IDX_OR(DT_DRV_INST(inst), prop, default_idx_value) + /** * @brief Get a DT_DRV_COMPAT instance property * @param inst instance number