Skip to content

Commit f594580

Browse files
committed
devicetree.h: Add DT_NODE_HAS_STATUS()
This macro takes a node identifier and status value and returns 0 or 1 if the status property on the node referred to by the node identifier matches the status value passed. We also update DT_HAS_NODE_STATUS_OKAY to utilize DT_NODE_HAS_STATUS Signed-off-by: Kumar Gala <[email protected]>
1 parent ac2c66e commit f594580

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

doc/guides/dts/macros.bnf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ node-macro =/ %s"DT_N" path-id %s"_IRQ_NAME_" dt-name
4343
node-macro =/ %s"DT_N" path-id %s"_COMPAT_MATCHES_" dt-name
4444
; The node identifier for the node's parent in the devicetree.
4545
node-macro =/ %s"DT_N" path-id %s"_PARENT"
46+
; The node identifier for the node's status
47+
node-macro =/ %s"DT_N" path-id %s"_STATUS_" status
4648

4749
; --------------------------------------------------------------------
4850
; property-macro: a macro related to a node property

include/devicetree.h

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,38 @@
880880
* @{
881881
*/
882882

883+
/** @internal helper for DT_NODE_HAS_STATUS so we can additional
884+
* macro expansion
885+
*/
886+
#define DT_NODE_HAS_STATUS_(node_id, status) \
887+
IS_ENABLED(DT_CAT(node_id, _STATUS_##status))
888+
889+
/**
890+
* @brief Does a devicetree node match a status?
891+
*
892+
* Example devicetree fragment:
893+
*
894+
* n: node {
895+
* compatible = "generic-device";
896+
* status = "okay";
897+
* }
898+
*
899+
* Example usages which evaluate to 1:
900+
*
901+
* DT_NODE_HAS_STATUS(DT_NODELABEL(n), okay)
902+
*
903+
* Example usages which evaluate to 0:
904+
*
905+
* DT_NODE_HAS_STATUS(DT_NODELABEL(n), disabled)
906+
*
907+
* @param node_id node identifier
908+
* @param status status value to check for
909+
* @return 1 if the node's status property matches status,
910+
* 0 otherwise.
911+
*/
912+
#define DT_NODE_HAS_STATUS(node_id, status) \
913+
DT_NODE_HAS_STATUS_(node_id, status)
914+
883915
/**
884916
* @brief Does a node identifier refer to a usable node?
885917
*
@@ -898,7 +930,7 @@
898930
* @return 1 if the node identifier refers to a usable node,
899931
* 0 otherwise.
900932
*/
901-
#define DT_HAS_NODE_STATUS_OKAY(node_id) IS_ENABLED(DT_CAT(node_id, _EXISTS))
933+
#define DT_HAS_NODE_STATUS_OKAY(node_id) DT_NODE_HAS_STATUS(node_id, okay)
902934

903935
/**
904936
* @brief Does the devicetree have any usable nodes with a compatible?

scripts/dts/gen_defines.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ def write_special_props(node):
241241
write_regs(node)
242242
write_interrupts(node)
243243
write_compatibles(node)
244+
write_status(node)
244245

245246

246247
def write_regs(node):
@@ -356,6 +357,21 @@ def write_compatibles(node):
356357
f"{node.z_path_id}_COMPAT_MATCHES_{str2ident(compat)}", 1)
357358

358359

360+
def write_status(node):
361+
# Writes a macro that we can utilize to test the "status" of a node
362+
# We normalize status = "ok" to "okay", and if there is no "status"
363+
# property we treat that as "okay"
364+
365+
if "status" in node.props:
366+
status = node.props["status"].val
367+
if status == "ok":
368+
status = "okay"
369+
else:
370+
status = "okay"
371+
372+
out_dt_define(f"{node.z_path_id}_STATUS_{status}", 1)
373+
374+
359375
def write_vanilla_props(node):
360376
# Writes macros for any and all properties defined in the
361377
# "properties" section of the binding for the node.

tests/lib/devicetree/app.overlay

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@
7575
status = "disabled";
7676
};
7777

78+
test_no_status: intc_no_status@0 {
79+
compatible = "vnd,intc";
80+
reg = <0x0 0x1000>;
81+
interrupt-controller;
82+
#interrupt-cells = <2>;
83+
};
84+
7885
test_nodelabel: TEST_NODELABEL_ALLCAPS: test_gpio_1: gpio@deadbeef {
7986
compatible = "vnd,gpio";
8087
gpio-controller;

tests/lib/devicetree/src/main.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,27 @@ static void test_has_compat(void)
200200
zassert_equal(compats, 0x3, "as bit array");
201201
}
202202

203+
static void test_has_status(void)
204+
{
205+
zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_gpio_1), okay),
206+
1, "vnd,gpio okay");
207+
zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_gpio_1), disabled),
208+
0, "vnd,gpio not disabled");
209+
210+
zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_no_status), okay),
211+
1, "vnd,gpio okay");
212+
zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(test_no_status), disabled),
213+
0, "vnd,gpio not disabled");
214+
215+
/* Disabled for now until we generate something for non-"enabled" nodes */
216+
#if 0
217+
zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(disabled_gpio), disabled),
218+
1, "vnd,disabled-compat disabled");
219+
zassert_equal(DT_NODE_HAS_STATUS(DT_NODELABEL(disabled_gpio), okay),
220+
0, "vnd,disabled-compat not okay");
221+
#endif
222+
}
223+
203224
#define TEST_I2C_DEV DT_PATH(test, i2c_11112222, test_i2c_dev_10)
204225
#define TEST_I2C_BUS DT_BUS(TEST_I2C_DEV)
205226

@@ -1381,6 +1402,7 @@ void test_main(void)
13811402
ztest_unit_test(test_inst_checks),
13821403
ztest_unit_test(test_has_nodelabel),
13831404
ztest_unit_test(test_has_compat),
1405+
ztest_unit_test(test_has_status),
13841406
ztest_unit_test(test_bus),
13851407
ztest_unit_test(test_reg),
13861408
ztest_unit_test(test_irq),

0 commit comments

Comments
 (0)