1.. _devicetree_api: 2 3Devicetree API 4############## 5 6This is a reference page for the ``<zephyr/devicetree.h>`` API. The API is macro 7based. Use of these macros has no impact on scheduling. They can be used from 8any calling context and at file scope. 9 10Some of these -- the ones beginning with ``DT_INST_`` -- require a special 11macro named ``DT_DRV_COMPAT`` to be defined before they can be used; these are 12discussed individually below. These macros are generally meant for use within 13:ref:`device drivers <device_model_api>`, though they can be used outside of 14drivers with appropriate care. 15 16.. contents:: Contents 17 :local: 18 19.. _devicetree-generic-apis: 20 21Generic APIs 22************ 23 24The APIs in this section can be used anywhere and do not require 25``DT_DRV_COMPAT`` to be defined. 26 27Node identifiers and helpers 28============================ 29 30A *node identifier* is a way to refer to a devicetree node at C preprocessor 31time. While node identifiers are not C values, you can use them to access 32devicetree data in C rvalue form using, for example, the 33:ref:`devicetree-property-access` API. 34 35The root node ``/`` has node identifier ``DT_ROOT``. You can create node 36identifiers for other devicetree nodes using :c:func:`DT_PATH`, 37:c:func:`DT_NODELABEL`, :c:func:`DT_ALIAS`, and :c:func:`DT_INST`. 38 39There are also :c:func:`DT_PARENT` and :c:func:`DT_CHILD` macros which can be 40used to create node identifiers for a given node's parent node or a particular 41child node, respectively. 42 43The following macros create or operate on node identifiers. 44 45.. doxygengroup:: devicetree-generic-id 46 47.. _devicetree-property-access: 48 49Property access 50=============== 51 52The following general-purpose macros can be used to access node properties. 53There are special-purpose APIs for accessing the :ref:`devicetree-ranges-property`, 54:ref:`devicetree-reg-property` and :ref:`devicetree-interrupts-property`. 55 56Property values can be read using these macros even if the node is disabled, 57as long as it has a matching binding. 58 59.. doxygengroup:: devicetree-generic-prop 60 61.. _devicetree-ranges-property: 62 63``ranges`` property 64=================== 65 66Use these APIs instead of :ref:`devicetree-property-access` to access the 67``ranges`` property. Because this property's semantics are defined by the 68devicetree specification, these macros can be used even for nodes without 69matching bindings. However, they take on special semantics when the node's 70binding indicates it is a PCIe bus node, as defined in the 71`PCI Bus Binding to: IEEE Std 1275-1994 Standard for Boot (Initialization Configuration) Firmware`_ 72 73.. _PCI Bus Binding to\: IEEE Std 1275-1994 Standard for Boot (Initialization Configuration) Firmware: 74 https://www.openfirmware.info/data/docs/bus.pci.pdf 75 76.. doxygengroup:: devicetree-ranges-prop 77 78.. _devicetree-reg-property: 79 80``reg`` property 81================ 82 83Use these APIs instead of :ref:`devicetree-property-access` to access the 84``reg`` property. Because this property's semantics are defined by the 85devicetree specification, these macros can be used even for nodes without 86matching bindings. 87 88.. doxygengroup:: devicetree-reg-prop 89 90.. _devicetree-interrupts-property: 91 92``interrupts`` property 93======================= 94 95Use these APIs instead of :ref:`devicetree-property-access` to access the 96``interrupts`` property. 97 98Because this property's semantics are defined by the devicetree specification, 99some of these macros can be used even for nodes without matching bindings. This 100does not apply to macros which take cell names as arguments. 101 102.. doxygengroup:: devicetree-interrupts-prop 103 104For-each macros 105=============== 106 107There is currently only one "generic" for-each macro, 108:c:func:`DT_FOREACH_CHILD`, which allows iterating over the children of a 109devicetree node. 110 111There are special-purpose for-each macros, like 112:c:func:`DT_INST_FOREACH_STATUS_OKAY`, but these require ``DT_DRV_COMPAT`` to 113be defined before use. 114 115.. doxygengroup:: devicetree-generic-foreach 116 117Existence checks 118================ 119 120This section documents miscellaneous macros that can be used to test if a node 121exists, how many nodes of a certain type exist, whether a node has certain 122properties, etc. Some macros used for special purposes (such as 123:c:func:`DT_IRQ_HAS_IDX` and all macros which require ``DT_DRV_COMPAT``) are 124documented elsewhere on this page. 125 126.. doxygengroup:: devicetree-generic-exist 127 128.. _devicetree-dep-ord: 129 130Inter-node dependencies 131======================= 132 133The ``devicetree.h`` API has some support for tracking dependencies between 134nodes. Dependency tracking relies on a binary "depends on" relation between 135devicetree nodes, which is defined as the `transitive closure 136<https://en.wikipedia.org/wiki/Transitive_closure>`_ of the following "directly 137depends on" relation: 138 139- every non-root node directly depends on its parent node 140- a node directly depends on any nodes its properties refer to by phandle 141- a node directly depends on its ``interrupt-parent`` if it has an 142 ``interrupts`` property 143- a parent node inherits all dependencies from its child nodes 144 145A *dependency ordering* of a devicetree is a list of its nodes, where each node 146``n`` appears earlier in the list than any nodes that depend on ``n``. A node's 147*dependency ordinal* is then its zero-based index in that list. Thus, for two 148distinct devicetree nodes ``n1`` and ``n2`` with dependency ordinals ``d1`` and 149``d2``, we have: 150 151- ``d1 != d2`` 152- if ``n1`` depends on ``n2``, then ``d1 > d2`` 153- ``d1 > d2`` does **not** necessarily imply that ``n1`` depends on ``n2`` 154 155The Zephyr build system chooses a dependency ordering of the final devicetree 156and assigns a dependency ordinal to each node. Dependency related information 157can be accessed using the following macros. The exact dependency ordering 158chosen is an implementation detail, but cyclic dependencies are detected and 159cause errors, so it's safe to assume there are none when using these macros. 160 161There are instance number-based conveniences as well; see 162:c:func:`DT_INST_DEP_ORD` and subsequent documentation. 163 164.. doxygengroup:: devicetree-dep-ord 165 166Bus helpers 167=========== 168 169Zephyr's devicetree bindings language supports a ``bus:`` key which allows 170bindings to declare that nodes with a given compatible describe system buses. 171In this case, child nodes are considered to be on a bus of the given type, and 172the following APIs may be used. 173 174.. doxygengroup:: devicetree-generic-bus 175 176.. _devicetree-inst-apis: 177 178Instance-based APIs 179******************* 180 181These are recommended for use within device drivers. To use them, define 182``DT_DRV_COMPAT`` to the lowercase-and-underscores compatible the device driver 183implements support for. Here is an example devicetree fragment: 184 185.. code-block:: devicetree 186 187 serial@40001000 { 188 compatible = "vnd,serial"; 189 status = "okay"; 190 current-speed = <115200>; 191 }; 192 193Example usage, assuming ``serial@40001000`` is the only enabled node 194with compatible ``vnd,serial``: 195 196.. code-block:: c 197 198 #define DT_DRV_COMPAT vnd_serial 199 DT_DRV_INST(0) // node identifier for serial@40001000 200 DT_INST_PROP(0, current_speed) // 115200 201 202.. warning:: 203 204 Be careful making assumptions about instance numbers. See :c:func:`DT_INST` 205 for the API guarantees. 206 207As shown above, the ``DT_INST_*`` APIs are conveniences for addressing nodes by 208instance number. They are almost all defined in terms of one of the 209:ref:`devicetree-generic-apis`. The equivalent generic API can be found by 210removing ``INST_`` from the macro name. For example, ``DT_INST_PROP(inst, 211prop)`` is equivalent to ``DT_PROP(DT_DRV_INST(inst), prop)``. Similarly, 212``DT_INST_REG_ADDR(inst)`` is equivalent to ``DT_REG_ADDR(DT_DRV_INST(inst))``, 213and so on. There are some exceptions: :c:func:`DT_ANY_INST_ON_BUS_STATUS_OKAY` 214and :c:func:`DT_INST_FOREACH_STATUS_OKAY` are special-purpose helpers without 215straightforward generic equivalents. 216 217Since ``DT_DRV_INST()`` requires ``DT_DRV_COMPAT`` to be defined, it's an error 218to use any of these without that macro defined. 219 220Note that there are also helpers available for 221specific hardware; these are documented in :ref:`devicetree-hw-api`. 222 223.. doxygengroup:: devicetree-inst 224 225.. _devicetree-hw-api: 226 227Hardware specific APIs 228********************** 229 230The following APIs can also be used by including ``<devicetree.h>``; 231no additional include is needed. 232 233.. _devicetree-can-api: 234 235CAN 236=== 237 238These conveniences may be used for nodes which describe CAN 239controllers/transceivers, and properties related to them. 240 241.. doxygengroup:: devicetree-can 242 243Clocks 244====== 245 246These conveniences may be used for nodes which describe clock sources, and 247properties related to them. 248 249.. doxygengroup:: devicetree-clocks 250 251DMA 252=== 253 254These conveniences may be used for nodes which describe direct memory access 255controllers or channels, and properties related to them. 256 257.. doxygengroup:: devicetree-dmas 258 259.. _devicetree-flash-api: 260 261Fixed flash partitions 262====================== 263 264These conveniences may be used for the special-purpose ``fixed-partitions`` 265compatible used to encode information about flash memory partitions in the 266device tree. See See :dtcompatible:`fixed-partition` for more details. 267 268.. doxygengroup:: devicetree-fixed-partition 269 270.. _devicetree-gpio-api: 271 272GPIO 273==== 274 275These conveniences may be used for nodes which describe GPIO controllers/pins, 276and properties related to them. 277 278.. doxygengroup:: devicetree-gpio 279 280IO channels 281=========== 282 283These are commonly used by device drivers which need to use IO 284channels (e.g. ADC or DAC channels) for conversion. 285 286.. doxygengroup:: devicetree-io-channels 287 288.. _devicetree-mbox-api: 289 290MBOX 291==== 292 293These conveniences may be used for nodes which describe MBOX controllers/users, 294and properties related to them. 295 296.. doxygengroup:: devicetree-mbox 297 298.. _devicetree-pinctrl-api: 299 300Pinctrl (pin control) 301===================== 302 303These are used to access pin control properties by name or index. 304 305Devicetree nodes may have properties which specify pin control (sometimes known 306as pin mux) settings. These are expressed using ``pinctrl-<index>`` properties 307within the node, where the ``<index>`` values are contiguous integers starting 308from 0. These may also be named using the ``pinctrl-names`` property. 309 310Here is an example: 311 312.. code-block:: DTS 313 314 node { 315 ... 316 pinctrl-0 = <&foo &bar ...>; 317 pinctrl-1 = <&baz ...>; 318 pinctrl-names = "default", "sleep"; 319 }; 320 321Above, ``pinctrl-0`` has name ``"default"``, and ``pinctrl-1`` has name 322``"sleep"``. The ``pinctrl-<index>`` property values contain phandles. The 323``&foo``, ``&bar``, etc. phandles within the properties point to nodes whose 324contents vary by platform, and which describe a pin configuration for the node. 325 326.. doxygengroup:: devicetree-pinctrl 327 328PWM 329=== 330 331These conveniences may be used for nodes which describe PWM controllers and 332properties related to them. 333 334.. doxygengroup:: devicetree-pwms 335 336Reset Controller 337================ 338 339These conveniences may be used for nodes which describe reset controllers and 340properties related to them. 341 342.. doxygengroup:: devicetree-reset-controller 343 344SPI 345=== 346 347These conveniences may be used for nodes which describe either SPI controllers 348or devices, depending on the case. 349 350.. doxygengroup:: devicetree-spi 351 352.. _devicetree-chosen-nodes: 353 354Chosen nodes 355************ 356 357The special ``/chosen`` node contains properties whose values describe 358system-wide settings. The :c:func:`DT_CHOSEN()` macro can be used to get a node 359identifier for a chosen node. 360 361.. doxygengroup:: devicetree-generic-chosen 362 :project: Zephyr 363 364Zephyr-specific chosen nodes 365**************************** 366 367The following table documents some commonly used Zephyr-specific chosen nodes. 368 369Sometimes, a chosen node's label property will be used to set the default value 370of a Kconfig option which in turn configures a hardware-specific device. This 371is usually for backwards compatibility in cases when the Kconfig option 372predates devicetree support in Zephyr. In other cases, there is no Kconfig 373option, and the devicetree node is used directly in the source code to select a 374device. 375 376.. Documentation maintainers: please keep this sorted by property name 377 378.. list-table:: Zephyr-specific chosen properties 379 :header-rows: 1 380 :widths: 25 75 381 382 * - Property 383 - Purpose 384 * - zephyr,bt-c2h-uart 385 - Selects the UART used for host communication in the 386 :ref:`bluetooth-hci-uart-sample` 387 * - zephyr,bt-mon-uart 388 - Sets UART device used for the Bluetooth monitor logging 389 * - zephyr,bt-uart 390 - Sets UART device used by Bluetooth 391 * - zephyr,canbus 392 - Sets the default CAN controller 393 * - zephyr,ccm 394 - Core-Coupled Memory node on some STM32 SoCs 395 * - zephyr,code-partition 396 - Flash partition that the Zephyr image's text section should be linked 397 into 398 * - zephyr,console 399 - Sets UART device used by console driver 400 * - zephyr,display 401 - Sets the default display controller 402 * - zephyr,keyboard-scan 403 - Sets the default keyboard scan controller 404 * - zephyr,dtcm 405 - Data Tightly Coupled Memory node on some Arm SoCs 406 * - zephyr,entropy 407 - A device which can be used as a system-wide entropy source 408 * - zephyr,flash 409 - A node whose ``reg`` is sometimes used to set the defaults for 410 :kconfig:option:`CONFIG_FLASH_BASE_ADDRESS` and :kconfig:option:`CONFIG_FLASH_SIZE` 411 * - zephyr,flash-controller 412 - The node corresponding to the flash controller device for 413 the ``zephyr,flash`` node 414 * - zephyr,gdbstub-uart 415 - Sets UART device used by the :ref:`gdbstub` subsystem 416 * - zephyr,ieee802154 417 - Used by the networking subsystem to set the IEEE 802.15.4 device 418 * - zephyr,ipc 419 - Used by the OpenAMP subsystem to specify the inter-process communication 420 (IPC) device 421 * - zephyr,ipc_shm 422 - A node whose ``reg`` is used by the OpenAMP subsystem to determine the 423 base address and size of the shared memory (SHM) usable for 424 interprocess-communication (IPC) 425 * - zephyr,itcm 426 - Instruction Tightly Coupled Memory node on some Arm SoCs 427 * - zephyr,log-uart 428 - Sets the UART device(s) used by the logging subsystem's UART backend. 429 If defined, the UART log backend would output to the devices listed in this node. 430 * - zephyr,ocm 431 - On-chip memory node on Xilinx Zynq-7000 and ZynqMP SoCs 432 * - zephyr,osdp-uart 433 - Sets UART device used by OSDP subsystem 434 * - zephyr,ot-uart 435 - Used by the OpenThread to specify UART device for Spinel protocol 436 * - zephyr,pcie-controller 437 - The node corresponding to the PCIe Controller 438 * - zephyr,ppp-uart 439 - Sets UART device used by PPP 440 * - zephyr,settings-partition 441 - Fixed partition node. If defined this selects the partition used 442 by the NVS and FCB settings backends. 443 * - zephyr,shell-uart 444 - Sets UART device used by serial shell backend 445 * - zephyr,sram 446 - A node whose ``reg`` sets the base address and size of SRAM memory 447 available to the Zephyr image, used during linking 448 * - zephyr,tracing-uart 449 - Sets UART device used by tracing subsystem 450 * - zephyr,uart-mcumgr 451 - UART used for :ref:`device_mgmt` 452 * - zephyr,uart-pipe 453 - Sets UART device used by serial pipe driver 454 * - zephyr,usb-device 455 - USB device node. If defined and has a ``vbus-gpios`` property, these 456 will be used by the USB subsystem to enable/disable VBUS 457