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:macro:`DT_PATH`,
37:c:macro:`DT_NODELABEL`, :c:macro:`DT_ALIAS`, and :c:macro:`DT_INST`.
38
39There are also :c:macro:`DT_PARENT` and :c:macro:`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
107The :c:macro:`DT_FOREACH_ANCESTOR` macro allows iterating over the ancestor node
108of a devicetree node.
109Additionally, the :c:macro:`DT_FOREACH_CHILD` macro allows iterating over the
110children of a devicetree node.
111
112There are special-purpose for-each macros, like
113:c:macro:`DT_INST_FOREACH_STATUS_OKAY`, but these require ``DT_DRV_COMPAT`` to
114be defined before use.
115
116.. doxygengroup:: devicetree-generic-foreach
117
118Existence checks
119================
120
121This section documents miscellaneous macros that can be used to test if a node
122exists, how many nodes of a certain type exist, whether a node has certain
123properties, etc. Some macros used for special purposes (such as
124:c:macro:`DT_IRQ_HAS_IDX` and all macros which require ``DT_DRV_COMPAT``) are
125documented elsewhere on this page.
126
127.. doxygengroup:: devicetree-generic-exist
128
129.. _devicetree-dep-ord:
130
131Inter-node dependencies
132=======================
133
134The ``devicetree.h`` API has some support for tracking dependencies between
135nodes. Dependency tracking relies on a binary "depends on" relation between
136devicetree nodes, which is defined as the `transitive closure
137<https://en.wikipedia.org/wiki/Transitive_closure>`_ of the following "directly
138depends on" relation:
139
140- every non-root node directly depends on its parent node
141- a node directly depends on any nodes its properties refer to by phandle
142- a node directly depends on its ``interrupt-parent`` if it has an
143  ``interrupts`` property
144- a parent node inherits all dependencies from its child nodes
145
146A *dependency ordering* of a devicetree is a list of its nodes, where each node
147``n`` appears earlier in the list than any nodes that depend on ``n``. A node's
148*dependency ordinal* is then its zero-based index in that list. Thus, for two
149distinct devicetree nodes ``n1`` and ``n2`` with dependency ordinals ``d1`` and
150``d2``, we have:
151
152- ``d1 != d2``
153- if ``n1`` depends on ``n2``, then ``d1 > d2``
154- ``d1 > d2`` does **not** necessarily imply that ``n1`` depends on ``n2``
155
156The Zephyr build system chooses a dependency ordering of the final devicetree
157and assigns a dependency ordinal to each node. Dependency related information
158can be accessed using the following macros. The exact dependency ordering
159chosen is an implementation detail, but cyclic dependencies are detected and
160cause errors, so it's safe to assume there are none when using these macros.
161
162There are instance number-based conveniences as well; see
163:c:macro:`DT_INST_DEP_ORD` and subsequent documentation.
164
165.. doxygengroup:: devicetree-dep-ord
166
167Bus helpers
168===========
169
170Zephyr's devicetree bindings language supports a ``bus:`` key which allows
171bindings to declare that nodes with a given compatible describe system buses.
172In this case, child nodes are considered to be on a bus of the given type, and
173the following APIs may be used.
174
175.. doxygengroup:: devicetree-generic-bus
176
177.. _devicetree-inst-apis:
178
179Instance-based APIs
180*******************
181
182These are recommended for use within device drivers. To use them, define
183``DT_DRV_COMPAT`` to the lowercase-and-underscores compatible the device driver
184implements support for. Here is an example devicetree fragment:
185
186.. code-block:: devicetree
187
188   serial@40001000 {
189           compatible = "vnd,serial";
190           status = "okay";
191           current-speed = <115200>;
192   };
193
194Example usage, assuming ``serial@40001000`` is the only enabled node
195with compatible ``vnd,serial``:
196
197.. code-block:: c
198
199   #define DT_DRV_COMPAT vnd_serial
200   DT_DRV_INST(0)                  // node identifier for serial@40001000
201   DT_INST_PROP(0, current_speed)  // 115200
202
203.. warning::
204
205   Be careful making assumptions about instance numbers. See :c:macro:`DT_INST`
206   for the API guarantees.
207
208As shown above, the ``DT_INST_*`` APIs are conveniences for addressing nodes by
209instance number. They are almost all defined in terms of one of the
210:ref:`devicetree-generic-apis`. The equivalent generic API can be found by
211removing ``INST_`` from the macro name. For example, ``DT_INST_PROP(inst,
212prop)`` is equivalent to ``DT_PROP(DT_DRV_INST(inst), prop)``. Similarly,
213``DT_INST_REG_ADDR(inst)`` is equivalent to ``DT_REG_ADDR(DT_DRV_INST(inst))``,
214and so on. There are some exceptions: :c:macro:`DT_ANY_INST_ON_BUS_STATUS_OKAY`
215and :c:macro:`DT_INST_FOREACH_STATUS_OKAY` are special-purpose helpers without
216straightforward generic equivalents.
217
218Since ``DT_DRV_INST()`` requires ``DT_DRV_COMPAT`` to be defined, it's an error
219to use any of these without that macro defined.
220
221Note that there are also helpers available for
222specific hardware; these are documented in :ref:`devicetree-hw-api`.
223
224.. doxygengroup:: devicetree-inst
225
226.. _devicetree-hw-api:
227
228Hardware specific APIs
229**********************
230
231The following APIs can also be used by including ``<devicetree.h>``;
232no additional include is needed.
233
234.. _devicetree-can-api:
235
236CAN
237===
238
239These conveniences may be used for nodes which describe CAN
240controllers/transceivers, and properties related to them.
241
242.. doxygengroup:: devicetree-can
243
244Clocks
245======
246
247These conveniences may be used for nodes which describe clock sources, and
248properties related to them.
249
250.. doxygengroup:: devicetree-clocks
251
252DMA
253===
254
255These conveniences may be used for nodes which describe direct memory access
256controllers or channels, and properties related to them.
257
258.. doxygengroup:: devicetree-dmas
259
260.. _devicetree-flash-api:
261
262Fixed flash partitions
263======================
264
265These conveniences may be used for the special-purpose ``fixed-partitions``
266compatible used to encode information about flash memory partitions in the
267device tree. See See :dtcompatible:`fixed-partition` for more details.
268
269.. doxygengroup:: devicetree-fixed-partition
270
271.. _devicetree-gpio-api:
272
273GPIO
274====
275
276These conveniences may be used for nodes which describe GPIO controllers/pins,
277and properties related to them.
278
279.. doxygengroup:: devicetree-gpio
280
281IO channels
282===========
283
284These are commonly used by device drivers which need to use IO
285channels (e.g. ADC or DAC channels) for conversion.
286
287.. doxygengroup:: devicetree-io-channels
288
289.. _devicetree-mbox-api:
290
291MBOX
292====
293
294These conveniences may be used for nodes which describe MBOX controllers/users,
295and properties related to them.
296
297.. doxygengroup:: devicetree-mbox
298
299.. _devicetree-pinctrl-api:
300
301Pinctrl (pin control)
302=====================
303
304These are used to access pin control properties by name or index.
305
306Devicetree nodes may have properties which specify pin control (sometimes known
307as pin mux) settings. These are expressed using ``pinctrl-<index>`` properties
308within the node, where the ``<index>`` values are contiguous integers starting
309from 0. These may also be named using the ``pinctrl-names`` property.
310
311Here is an example:
312
313.. code-block:: DTS
314
315   node {
316       ...
317       pinctrl-0 = <&foo &bar ...>;
318       pinctrl-1 = <&baz ...>;
319       pinctrl-names = "default", "sleep";
320   };
321
322Above, ``pinctrl-0`` has name ``"default"``, and ``pinctrl-1`` has name
323``"sleep"``. The ``pinctrl-<index>`` property values contain phandles. The
324``&foo``, ``&bar``, etc. phandles within the properties point to nodes whose
325contents vary by platform, and which describe a pin configuration for the node.
326
327.. doxygengroup:: devicetree-pinctrl
328
329PWM
330===
331
332These conveniences may be used for nodes which describe PWM controllers and
333properties related to them.
334
335.. doxygengroup:: devicetree-pwms
336
337Reset Controller
338================
339
340These conveniences may be used for nodes which describe reset controllers and
341properties related to them.
342
343.. doxygengroup:: devicetree-reset-controller
344
345SPI
346===
347
348These conveniences may be used for nodes which describe either SPI controllers
349or devices, depending on the case.
350
351.. doxygengroup:: devicetree-spi
352
353.. _devicetree-chosen-nodes:
354
355Chosen nodes
356************
357
358The special ``/chosen`` node contains properties whose values describe
359system-wide settings. The :c:macro:`DT_CHOSEN()` macro can be used to get a node
360identifier for a chosen node.
361
362.. doxygengroup:: devicetree-generic-chosen
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       :zephyr:code-sample:`bluetooth_hci_uart`
387   * - zephyr,bt-mon-uart
388     - Sets UART device used for the Bluetooth monitor logging
389   * - zephyr,bt-hci
390     - Selects the HCI device used by the Bluetooth host stack
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   * - zephyr,led-strip
458     - A LED-strip node which is used to determine the timings of the
459       WS2812 GPIO driver
460   * - zephyr,touch
461     - touchscreen controller device node.
462