devicetree: implement DT_INST_NODE_HAS_COMPAT

Implement `DT_INST_NODE_HAS_COMPAT` to check if a node has a
compatible. This is helpful when a node has more than one
compatible, which can be used to enable additional features
in the driver.

Updated the devicetree test to play with the new API.

Signed-off-by: Yong Cong Sin <ycsin@meta.com>
This commit is contained in:
Yong Cong Sin 2024-04-24 22:27:28 +08:00 committed by Fabio Baltieri
parent a1916b0121
commit 9b7638f049
2 changed files with 13 additions and 0 deletions

View file

@ -4357,6 +4357,15 @@
#define DT_INST_NODE_HAS_PROP(inst, prop) \
DT_NODE_HAS_PROP(DT_DRV_INST(inst), prop)
/**
* @brief Does a DT_DRV_COMPAT instance have the compatible?
* @param inst instance number
* @param compat lowercase-and-underscores compatible, without quotes
* @return 1 if the instance matches the compatible, 0 otherwise.
*/
#define DT_INST_NODE_HAS_COMPAT(inst, compat) \
DT_NODE_HAS_COMPAT(DT_DRV_INST(inst), compat)
/**
* @brief Does a phandle array have a named cell specifier at an index
* for a `DT_DRV_COMPAT` instance?

View file

@ -319,6 +319,10 @@ ZTEST(devicetree_api, test_has_compat)
(TA_HAS_COMPAT(vnd_undefined_compat) << 1) |
(TA_HAS_COMPAT(vnd_not_a_test_array_compat) << 2));
zassert_equal(compats, 0x3, "");
#undef DT_DRV_COMPAT
#define DT_DRV_COMPAT vnd_model1
zassert_true(DT_INST_NODE_HAS_COMPAT(0, zephyr_model2));
}
ZTEST(devicetree_api, test_has_status)