From 37a751ca39f479d25c42d81614849195872ebc86 Mon Sep 17 00:00:00 2001 From: Tomasz Bursztyka Date: Mon, 12 May 2025 15:49:07 +0200 Subject: [PATCH] device: Add an option to generate the device name from the node-label The default device name might be less human readable. A label property can be created instead but is marked as deprecated and should no longer be used. DTS provide a way, internaly, to alias a node with a more human readable name: the node label. So let's enable the possibility to use it as a device name. As there may be more than one node-label, the first one is arbitrarly chosen to be the "main" node-label, which in turn will be used as the device name if CONFIG_DEVICE_DT_NAME_FROM_NODE_LABEL is enabled. Signed-off-by: Tomasz Bursztyka --- include/zephyr/device.h | 10 +++++++++- include/zephyr/devicetree.h | 28 ++++++++++++++++++++++++++++ kernel/Kconfig.device | 13 +++++++++++++ scripts/dts/gen_defines.py | 3 +++ 4 files changed, 53 insertions(+), 1 deletion(-) diff --git a/include/zephyr/device.h b/include/zephyr/device.h index df2d47250c71c..96a7c9a1c6fb1 100644 --- a/include/zephyr/device.h +++ b/include/zephyr/device.h @@ -195,9 +195,17 @@ typedef int16_t device_handle_t; * * @return The value of the node's `label` property, if it has one. * Otherwise, the node's full name in `node-name@unit-address` form. + * If CONFIG_DEVICE_DT_NAME_FROM_NODE_LABEL is enabled, it will use the + * node-label in priority over the full name, if the `label` property is not + * present. Note that, in this case, the first node-label is always used. */ #define DEVICE_DT_NAME(node_id) \ - DT_PROP_OR(node_id, label, DT_NODE_FULL_NAME(node_id)) + DT_PROP_OR(node_id, label, \ + COND_CODE_1(CONFIG_DEVICE_DT_NAME_FROM_NODE_LABEL, \ + (COND_CODE_1(DT_HAS_NODELABEL_MAIN(node_id), \ + (DT_NODELABEL_MAIN_STR(node_id)), \ + (DT_NODE_FULL_NAME(node_id)))), \ + (DT_NODE_FULL_NAME(node_id)))) /** * @brief Create a device object from a devicetree node identifier and set it up diff --git a/include/zephyr/devicetree.h b/include/zephyr/devicetree.h index 665c10c68054b..b7c2453d21ac8 100644 --- a/include/zephyr/devicetree.h +++ b/include/zephyr/devicetree.h @@ -195,6 +195,34 @@ */ #define DT_NODELABEL(label) DT_CAT(DT_N_NODELABEL_, label) +/** + * @brief Check if a node has a main node-label + * + * @param node_id node identifier + * @return 1 if such macro exists, 0 otherwise + */ +#define DT_HAS_NODELABEL_MAIN(node_id) \ + IS_ENABLED(DT_CAT(node_id, _NODELABEL_MAIN_EXISTS)) + +/** + * @brief Get the main node label macro from a node identifier + * + * @param node_id node identifier + * @return the macro which, if it exists, will give the main node label + */ +#define DT_NODELABEL_MAIN(node_id) DT_CAT(node_id, _NODELABEL_MAIN) + +/** + * @brief Get the main node label as a string + * + * Note: Prior to calling this macro, one should ensure that DT_NODELABEL_MAIN + * actually exists. + * + * @param node_id node identifier + * @return node's main label as a string + */ +#define DT_NODELABEL_MAIN_STR(node_id) STRINGIFY(DT_NODELABEL_MAIN(node_id)) + /** * @brief Get a node identifier from /aliases * diff --git a/kernel/Kconfig.device b/kernel/Kconfig.device index 5692236a0062f..ef66593351746 100644 --- a/kernel/Kconfig.device +++ b/kernel/Kconfig.device @@ -33,6 +33,19 @@ config DEVICE_DT_METADATA each device. This allows you to use device_get_by_dt_nodelabel(), device_get_dt_metadata(), etc. +config DEVICE_DT_NAME_FROM_NODE_LABEL + bool "Use DTS (first) node label as device name" + help + In DTS you can provide a 'label' property to define a more human + readable name. However, this is nowadays a deprecated property, + letting the node full name as the default. + But it is less human readable, and can be confusing in some + cases (i2c@0f00bar0 can be less readable than i2c1 for instance). + DTS provides an other way to get a unique identifier, + more human readable: the node-label. Let's use it instead. + Note that in case there are 2+ node-labels define for a node, + the first one will be used. + endmenu menu "Initialization Priorities" diff --git a/scripts/dts/gen_defines.py b/scripts/dts/gen_defines.py index 65ae177b63c3c..f499f75c3829c 100755 --- a/scripts/dts/gen_defines.py +++ b/scripts/dts/gen_defines.py @@ -92,6 +92,9 @@ def main(): out_comment("Helpers for dealing with node labels:") out_dt_define(f"{node.z_path_id}_NODELABEL_NUM", len(node.labels)) + if len(node.labels) >= 1: + out_dt_define(f"{node.z_path_id}_NODELABEL_MAIN", node.labels[0]) + out_dt_define(f"{node.z_path_id}_NODELABEL_MAIN_EXISTS", 1) out_dt_define(f"{node.z_path_id}_FOREACH_NODELABEL(fn)", " ".join(f"fn({nodelabel})" for nodelabel in node.labels)) out_dt_define(f"{node.z_path_id}_FOREACH_NODELABEL_VARGS(fn, ...)",