devicetree: add DT_ENUM_HAS_VALUE

Add a macro for checking if an enumeration matches a given value.
This enables code to directly check whether a string enumeration is a
specific value, without needing to construct an intermediate variable,
and without checking against a index value which may change.

```
/* Enables this */
if (DT_INST_ENUM_HAS_VALUE(0, power_amplifier_output, rfo_hp)) {}
/* Instead of this */
if (DT_INST_ENUM_IDX(0, power_amplifier_output) == 0) {}
```

Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
This commit is contained in:
Jordan Yates 2023-04-15 11:35:40 +10:00 committed by Marti Bolivar
parent 59167e1888
commit b6fb701f89

View file

@ -39,6 +39,7 @@
* are missing from this list, please add them. It should be complete.
*
* _ENUM_IDX: property's value as an index into bindings enum
* _ENUM_VAL_<val>_EXISTS property's value as a token exists
* _ENUM_TOKEN: property's value as a token into bindings enum (string
* enum values are identifiers) [deprecated, use _STRING_TOKEN]
* _ENUM_UPPER_TOKEN: like _ENUM_TOKEN, but uppercased [deprecated, use
@ -827,6 +828,17 @@
COND_CODE_1(DT_NODE_HAS_PROP(node_id, prop), \
(DT_ENUM_IDX(node_id, prop)), (default_idx_value))
/**
* @brief Does a node enumeration property have a given value?
*
* @param node_id node identifier
* @param prop lowercase-and-underscores property name
* @param value lowercase-and-underscores enumeration value
* @return 1 if the node property has the value @a value, 0 otherwise.
*/
#define DT_ENUM_HAS_VALUE(node_id, prop, value) \
IS_ENABLED(DT_CAT6(node_id, _P_, prop, _ENUM_VAL_, value, _EXISTS))
/**
* @brief Get a string property's value as a token.
*
@ -3336,6 +3348,17 @@
#define DT_INST_ENUM_IDX_OR(inst, prop, default_idx_value) \
DT_ENUM_IDX_OR(DT_DRV_INST(inst), prop, default_idx_value)
/**
* @brief Does a `DT_DRV_COMPAT` enumeration property have a given value?
*
* @param inst instance number
* @param prop lowercase-and-underscores property name
* @param value lowercase-and-underscores enumeration value
* @return 1 if the node property has the value @a value, 0 otherwise.
*/
#define DT_INST_ENUM_HAS_VALUE(inst, prop, value) \
DT_ENUM_HAS_VALUE(DT_DRV_INST(inst), prop, value)
/**
* @brief Get a `DT_DRV_COMPAT` instance property
* @param inst instance number