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
144A *dependency ordering* of a devicetree is a list of its nodes, where each node
145``n`` appears earlier in the list than any nodes that depend on ``n``. A node's
146*dependency ordinal* is then its zero-based index in that list. Thus, for two
147distinct devicetree nodes ``n1`` and ``n2`` with dependency ordinals ``d1`` and
148``d2``, we have:
149
150- ``d1 != d2``
151- if ``n1`` depends on ``n2``, then ``d1 > d2``
152- ``d1 > d2`` does **not** necessarily imply that ``n1`` depends on ``n2``
153
154The Zephyr build system chooses a dependency ordering of the final devicetree
155and assigns a dependency ordinal to each node. Dependency related information
156can be accessed using the following macros. The exact dependency ordering
157chosen is an implementation detail, but cyclic dependencies are detected and
158cause errors, so it's safe to assume there are none when using these macros.
159
160There are instance number-based conveniences as well; see
161:c:func:`DT_INST_DEP_ORD` and subsequent documentation.
162
163.. doxygengroup:: devicetree-dep-ord
164
165Bus helpers
166===========
167
168Zephyr's devicetree bindings language supports a ``bus:`` key which allows
169bindings to declare that nodes with a given compatible describe system buses.
170In this case, child nodes are considered to be on a bus of the given type, and
171the following APIs may be used.
172
173.. doxygengroup:: devicetree-generic-bus
174
175.. _devicetree-inst-apis:
176
177Instance-based APIs
178*******************
179
180These are recommended for use within device drivers. To use them, define
181``DT_DRV_COMPAT`` to the lowercase-and-underscores compatible the device driver
182implements support for. Here is an example devicetree fragment:
183
184.. code-block:: devicetree
185
186   serial@40001000 {
187           compatible = "vnd,serial";
188           status = "okay";
189           current-speed = <115200>;
190   };
191
192Example usage, assuming ``serial@40001000`` is the only enabled node
193with compatible ``vnd,serial``:
194
195.. code-block:: c
196
197   #define DT_DRV_COMPAT vnd_serial
198   DT_DRV_INST(0)                  // node identifier for serial@40001000
199   DT_INST_PROP(0, current_speed)  // 115200
200
201.. warning::
202
203   Be careful making assumptions about instance numbers. See :c:func:`DT_INST`
204   for the API guarantees.
205
206As shown above, the ``DT_INST_*`` APIs are conveniences for addressing nodes by
207instance number. They are almost all defined in terms of one of the
208:ref:`devicetree-generic-apis`. The equivalent generic API can be found by
209removing ``INST_`` from the macro name. For example, ``DT_INST_PROP(inst,
210prop)`` is equivalent to ``DT_PROP(DT_DRV_INST(inst), prop)``. Similarly,
211``DT_INST_REG_ADDR(inst)`` is equivalent to ``DT_REG_ADDR(DT_DRV_INST(inst))``,
212and so on. There are some exceptions: :c:func:`DT_ANY_INST_ON_BUS_STATUS_OKAY`
213and :c:func:`DT_INST_FOREACH_STATUS_OKAY` are special-purpose helpers without
214straightforward generic equivalents.
215
216Since ``DT_DRV_INST()`` requires ``DT_DRV_COMPAT`` to be defined, it's an error
217to use any of these without that macro defined.
218
219Note that there are also helpers available for
220specific hardware; these are documented in :ref:`devicetree-hw-api`.
221
222.. doxygengroup:: devicetree-inst
223
224.. _devicetree-hw-api:
225
226Hardware specific APIs
227**********************
228
229The following APIs can also be used by including ``<devicetree.h>``;
230no additional include is needed.
231
232.. _devicetree-can-api:
233
234CAN
235===
236
237These conveniences may be used for nodes which describe CAN
238controllers/transceivers, and properties related to them.
239
240.. doxygengroup:: devicetree-can
241
242Clocks
243======
244
245These conveniences may be used for nodes which describe clock sources, and
246properties related to them.
247
248.. doxygengroup:: devicetree-clocks
249
250DMA
251===
252
253These conveniences may be used for nodes which describe direct memory access
254controllers or channels, and properties related to them.
255
256.. doxygengroup:: devicetree-dmas
257
258.. _devicetree-flash-api:
259
260Fixed flash partitions
261======================
262
263These conveniences may be used for the special-purpose ``fixed-partitions``
264compatible used to encode information about flash memory partitions in the
265device tree. See See :dtcompatible:`fixed-partition` for more details.
266
267.. doxygengroup:: devicetree-fixed-partition
268
269.. _devicetree-gpio-api:
270
271GPIO
272====
273
274These conveniences may be used for nodes which describe GPIO controllers/pins,
275and properties related to them.
276
277.. doxygengroup:: devicetree-gpio
278
279IO channels
280===========
281
282These are commonly used by device drivers which need to use IO
283channels (e.g. ADC or DAC channels) for conversion.
284
285.. doxygengroup:: devicetree-io-channels
286
287.. _devicetree-mbox-api:
288
289MBOX
290====
291
292These conveniences may be used for nodes which describe MBOX controllers/users,
293and properties related to them.
294
295.. doxygengroup:: devicetree-mbox
296
297.. _devicetree-pinctrl-api:
298
299Pinctrl (pin control)
300=====================
301
302These are used to access pin control properties by name or index.
303
304Devicetree nodes may have properties which specify pin control (sometimes known
305as pin mux) settings. These are expressed using ``pinctrl-<index>`` properties
306within the node, where the ``<index>`` values are contiguous integers starting
307from 0. These may also be named using the ``pinctrl-names`` property.
308
309Here is an example:
310
311.. code-block:: DTS
312
313   node {
314       ...
315       pinctrl-0 = <&foo &bar ...>;
316       pinctrl-1 = <&baz ...>;
317       pinctrl-names = "default", "sleep";
318   };
319
320Above, ``pinctrl-0`` has name ``"default"``, and ``pinctrl-1`` has name
321``"sleep"``. The ``pinctrl-<index>`` property values contain phandles. The
322``&foo``, ``&bar``, etc. phandles within the properties point to nodes whose
323contents vary by platform, and which describe a pin configuration for the node.
324
325.. doxygengroup:: devicetree-pinctrl
326
327PWM
328===
329
330These conveniences may be used for nodes which describe PWM controllers and
331properties related to them.
332
333.. doxygengroup:: devicetree-pwms
334
335Reset Controller
336================
337
338These conveniences may be used for nodes which describe reset controllers and
339properties related to them.
340
341.. doxygengroup:: devicetree-reset-controller
342
343SPI
344===
345
346These conveniences may be used for nodes which describe either SPI controllers
347or devices, depending on the case.
348
349.. doxygengroup:: devicetree-spi
350
351.. _devicetree-chosen-nodes:
352
353Chosen nodes
354************
355
356The special ``/chosen`` node contains properties whose values describe
357system-wide settings. The :c:func:`DT_CHOSEN()` macro can be used to get a node
358identifier for a chosen node.
359
360.. doxygengroup:: devicetree-generic-chosen
361   :project: Zephyr
362
363Zephyr-specific chosen nodes
364****************************
365
366The following table documents some commonly used Zephyr-specific chosen nodes.
367
368Sometimes, a chosen node's label property will be used to set the default value
369of a Kconfig option which in turn configures a hardware-specific device. This
370is usually for backwards compatibility in cases when the Kconfig option
371predates devicetree support in Zephyr. In other cases, there is no Kconfig
372option, and the devicetree node is used directly in the source code to select a
373device.
374
375.. Documentation maintainers: please keep this sorted by property name
376
377.. list-table:: Zephyr-specific chosen properties
378   :header-rows: 1
379   :widths: 25 75
380
381   * - Property
382     - Purpose
383   * - zephyr,bt-c2h-uart
384     - Selects the UART used for host communication in the
385       :ref:`bluetooth-hci-uart-sample`
386   * - zephyr,bt-mon-uart
387     - Sets UART device used for the Bluetooth monitor logging
388   * - zephyr,bt-uart
389     - Sets UART device used by Bluetooth
390   * - zephyr,canbus
391     - Sets the default CAN controller
392   * - zephyr,ccm
393     - Core-Coupled Memory node on some STM32 SoCs
394   * - zephyr,code-partition
395     - Flash partition that the Zephyr image's text section should be linked
396       into
397   * - zephyr,console
398     - Sets UART device used by console driver
399   * - zephyr,display
400     - Sets the default display controller
401   * - zephyr,keyboard-scan
402     - Sets the default keyboard scan controller
403   * - zephyr,dtcm
404     - Data Tightly Coupled Memory node on some Arm SoCs
405   * - zephyr,entropy
406     - A device which can be used as a system-wide entropy source
407   * - zephyr,flash
408     - A node whose ``reg`` is sometimes used to set the defaults for
409       :kconfig:option:`CONFIG_FLASH_BASE_ADDRESS` and :kconfig:option:`CONFIG_FLASH_SIZE`
410   * - zephyr,flash-controller
411     - The node corresponding to the flash controller device for
412       the ``zephyr,flash`` node
413   * - zephyr,gdbstub-uart
414     - Sets UART device used by the :ref:`gdbstub` subsystem
415   * - zephyr,ieee802154
416     - Used by the networking subsystem to set the IEEE 802.15.4 device
417   * - zephyr,ipc
418     - Used by the OpenAMP subsystem to specify the inter-process communication
419       (IPC) device
420   * - zephyr,ipc_shm
421     - A node whose ``reg`` is used by the OpenAMP subsystem to determine the
422       base address and size of the shared memory (SHM) usable for
423       interprocess-communication (IPC)
424   * - zephyr,itcm
425     - Instruction Tightly Coupled Memory node on some Arm SoCs
426   * - zephyr,ocm
427     - On-chip memory node on Xilinx Zynq-7000 and ZynqMP SoCs
428   * - zephyr,osdp-uart
429     - Sets UART device used by OSDP subsystem
430   * - zephyr,ot-uart
431     - Used by the OpenThread to specify UART device for Spinel protocol
432   * - zephyr,pcie-controller
433     - The node corresponding to the PCIe Controller
434   * - zephyr,ppp-uart
435     - Sets UART device used by PPP
436   * - zephyr,settings-partition
437     - Fixed partition node. If defined this selects the partition used
438       by the NVS and FCB settings backends.
439   * - zephyr,shell-uart
440     - Sets UART device used by serial shell backend
441   * - zephyr,sram
442     - A node whose ``reg`` sets the base address and size of SRAM memory
443       available to the Zephyr image, used during linking
444   * - zephyr,tracing-uart
445     - Sets UART device used by tracing subsystem
446   * - zephyr,uart-mcumgr
447     - UART used for :ref:`device_mgmt`
448   * - zephyr,uart-pipe
449     - Sets UART device used by serial pipe driver
450   * - zephyr,usb-device
451     - USB device node. If defined and has a ``vbus-gpios`` property, these
452       will be used by the USB subsystem to enable/disable VBUS
453