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