xref: /freebsd-src/sys/contrib/device-tree/Bindings/writing-schema.rst (revision 01950c46b8155250f64374fb72fc11faa44bf099)
12eb4d8dcSEmmanuel Vadot.. SPDX-License-Identifier: GPL-2.0
22eb4d8dcSEmmanuel Vadot
32eb4d8dcSEmmanuel VadotWriting Devicetree Bindings in json-schema
42eb4d8dcSEmmanuel Vadot==========================================
52eb4d8dcSEmmanuel Vadot
62eb4d8dcSEmmanuel VadotDevicetree bindings are written using json-schema vocabulary. Schema files are
78cc087a1SEmmanuel Vadotwritten in a JSON-compatible subset of YAML. YAML is used instead of JSON as it
82eb4d8dcSEmmanuel Vadotis considered more human readable and has some advantages such as allowing
92eb4d8dcSEmmanuel Vadotcomments (Prefixed with '#').
102eb4d8dcSEmmanuel Vadot
112eb4d8dcSEmmanuel VadotAlso see :ref:`example-schema`.
122eb4d8dcSEmmanuel Vadot
132eb4d8dcSEmmanuel VadotSchema Contents
142eb4d8dcSEmmanuel Vadot---------------
152eb4d8dcSEmmanuel Vadot
162eb4d8dcSEmmanuel VadotEach schema doc is a structured json-schema which is defined by a set of
172eb4d8dcSEmmanuel Vadottop-level properties. Generally, there is one binding defined per file. The
182eb4d8dcSEmmanuel Vadottop-level json-schema properties used are:
192eb4d8dcSEmmanuel Vadot
202eb4d8dcSEmmanuel Vadot$id
212eb4d8dcSEmmanuel Vadot  A json-schema unique identifier string. The string must be a valid
222eb4d8dcSEmmanuel Vadot  URI typically containing the binding's filename and path. For DT schema, it must
232eb4d8dcSEmmanuel Vadot  begin with "http://devicetree.org/schemas/". The URL is used in constructing
242eb4d8dcSEmmanuel Vadot  references to other files specified in schema "$ref" properties. A $ref value
258cc087a1SEmmanuel Vadot  with a leading '/' will have the hostname prepended. A $ref value with only a
268cc087a1SEmmanuel Vadot  relative path or filename will be prepended with the hostname and path
278cc087a1SEmmanuel Vadot  components of the current schema file's '$id' value. A URL is used even for
288cc087a1SEmmanuel Vadot  local files, but there may not actually be files present at those locations.
292eb4d8dcSEmmanuel Vadot
302eb4d8dcSEmmanuel Vadot$schema
312eb4d8dcSEmmanuel Vadot  Indicates the meta-schema the schema file adheres to.
322eb4d8dcSEmmanuel Vadot
332eb4d8dcSEmmanuel Vadottitle
34*01950c46SEmmanuel Vadot  A one-line description of the hardware being described in the binding schema.
352eb4d8dcSEmmanuel Vadot
362eb4d8dcSEmmanuel Vadotmaintainers
372eb4d8dcSEmmanuel Vadot  A DT specific property. Contains a list of email address(es)
382eb4d8dcSEmmanuel Vadot  for maintainers of this binding.
392eb4d8dcSEmmanuel Vadot
402eb4d8dcSEmmanuel Vadotdescription
412eb4d8dcSEmmanuel Vadot  Optional. A multi-line text block containing any detailed
42*01950c46SEmmanuel Vadot  information about this hardware. It should contain things such as what the block
432eb4d8dcSEmmanuel Vadot  or device does, standards the device conforms to, and links to datasheets for
442eb4d8dcSEmmanuel Vadot  more information.
452eb4d8dcSEmmanuel Vadot
462eb4d8dcSEmmanuel Vadotselect
472eb4d8dcSEmmanuel Vadot  Optional. A json-schema used to match nodes for applying the
488cc087a1SEmmanuel Vadot  schema. By default, without 'select', nodes are matched against their possible
498cc087a1SEmmanuel Vadot  compatible-string values or node name. Most bindings should not need select.
502eb4d8dcSEmmanuel Vadot
512eb4d8dcSEmmanuel VadotallOf
522eb4d8dcSEmmanuel Vadot  Optional. A list of other schemas to include. This is used to
532eb4d8dcSEmmanuel Vadot  include other schemas the binding conforms to. This may be schemas for a
542eb4d8dcSEmmanuel Vadot  particular class of devices such as I2C or SPI controllers.
552eb4d8dcSEmmanuel Vadot
562eb4d8dcSEmmanuel Vadotproperties
572eb4d8dcSEmmanuel Vadot  A set of sub-schema defining all the DT properties for the
582eb4d8dcSEmmanuel Vadot  binding. The exact schema syntax depends on whether properties are known,
598cc087a1SEmmanuel Vadot  common properties (e.g. 'interrupts') or are binding/vendor-specific
608cc087a1SEmmanuel Vadot  properties.
612eb4d8dcSEmmanuel Vadot
622eb4d8dcSEmmanuel VadotA property can also define a child DT node with child properties defined
632eb4d8dcSEmmanuel Vadotunder it.
642eb4d8dcSEmmanuel Vadot
652eb4d8dcSEmmanuel VadotFor more details on properties sections, see 'Property Schema' section.
662eb4d8dcSEmmanuel Vadot
672eb4d8dcSEmmanuel VadotpatternProperties
682eb4d8dcSEmmanuel Vadot  Optional. Similar to 'properties', but names are regex.
692eb4d8dcSEmmanuel Vadot
702eb4d8dcSEmmanuel Vadotrequired
712eb4d8dcSEmmanuel Vadot  A list of DT properties from the 'properties' section that
722eb4d8dcSEmmanuel Vadot  must always be present.
732eb4d8dcSEmmanuel Vadot
74*01950c46SEmmanuel VadotadditionalProperties / unevaluatedProperties
75*01950c46SEmmanuel Vadot  Keywords controlling how schema will validate properties not matched by this
76*01950c46SEmmanuel Vadot  schema's 'properties' or 'patternProperties'. Each schema is supposed to
77*01950c46SEmmanuel Vadot  have exactly one of these keywords in top-level part, so either
78*01950c46SEmmanuel Vadot  additionalProperties or unevaluatedProperties. Nested nodes, so properties
79*01950c46SEmmanuel Vadot  being objects, are supposed to have one as well.
80*01950c46SEmmanuel Vadot
81*01950c46SEmmanuel Vadot  * additionalProperties: false
82*01950c46SEmmanuel Vadot      Most common case, where no additional schema is referenced or if this
83*01950c46SEmmanuel Vadot      binding allows subset of properties from other referenced schemas.
84*01950c46SEmmanuel Vadot
85*01950c46SEmmanuel Vadot  * unevaluatedProperties: false
86*01950c46SEmmanuel Vadot      Used when this binding references other schema whose all properties
87*01950c46SEmmanuel Vadot      should be allowed.
88*01950c46SEmmanuel Vadot
89*01950c46SEmmanuel Vadot  * additionalProperties: true
90*01950c46SEmmanuel Vadot      Rare case, used for schemas implementing common set of properties. Such
91*01950c46SEmmanuel Vadot      schemas are supposed to be referenced by other schemas, which then use
92*01950c46SEmmanuel Vadot      'unevaluatedProperties: false'.  Typically bus or common-part schemas.
93*01950c46SEmmanuel Vadot
942eb4d8dcSEmmanuel Vadotexamples
95*01950c46SEmmanuel Vadot  Optional. A list of one or more DTS hunks implementing this binding only.
96*01950c46SEmmanuel Vadot  Example should not contain unrelated device nodes, e.g. consumer nodes in a
97*01950c46SEmmanuel Vadot  provider binding, other nodes referenced by phandle.
98*01950c46SEmmanuel Vadot  Note: YAML doesn't allow leading tabs, so spaces must be used instead.
992eb4d8dcSEmmanuel Vadot
1002eb4d8dcSEmmanuel VadotUnless noted otherwise, all properties are required.
1012eb4d8dcSEmmanuel Vadot
1022eb4d8dcSEmmanuel VadotProperty Schema
1032eb4d8dcSEmmanuel Vadot---------------
1042eb4d8dcSEmmanuel Vadot
1052eb4d8dcSEmmanuel VadotThe 'properties' section of the schema contains all the DT properties for a
1062eb4d8dcSEmmanuel Vadotbinding. Each property contains a set of constraints using json-schema
1078cc087a1SEmmanuel Vadotvocabulary for that property. The properties schemas are what are used for
1082eb4d8dcSEmmanuel Vadotvalidation of DT files.
1092eb4d8dcSEmmanuel Vadot
1108cc087a1SEmmanuel VadotFor common properties, only additional constraints not covered by the common,
1112eb4d8dcSEmmanuel Vadotbinding schema need to be defined such as how many values are valid or what
1122eb4d8dcSEmmanuel Vadotpossible values are valid.
1132eb4d8dcSEmmanuel Vadot
1148cc087a1SEmmanuel VadotVendor-specific properties will typically need more detailed schema. With the
1152eb4d8dcSEmmanuel Vadotexception of boolean properties, they should have a reference to a type in
1162eb4d8dcSEmmanuel Vadotschemas/types.yaml. A "description" property is always required.
1172eb4d8dcSEmmanuel Vadot
1188cc087a1SEmmanuel VadotThe Devicetree schemas don't exactly match the YAML-encoded DT data produced by
1192eb4d8dcSEmmanuel Vadotdtc. They are simplified to make them more compact and avoid a bunch of
1202eb4d8dcSEmmanuel Vadotboilerplate. The tools process the schema files to produce the final schema for
1212eb4d8dcSEmmanuel Vadotvalidation. There are currently 2 transformations the tools perform.
1222eb4d8dcSEmmanuel Vadot
1238cc087a1SEmmanuel VadotThe default for arrays in json-schema is they are variable-sized and allow more
1242eb4d8dcSEmmanuel Vadotentries than explicitly defined. This can be restricted by defining 'minItems',
1252eb4d8dcSEmmanuel Vadot'maxItems', and 'additionalItems'. However, for DeviceTree Schemas, a fixed
1262eb4d8dcSEmmanuel Vadotsize is desired in most cases, so these properties are added based on the
1272eb4d8dcSEmmanuel Vadotnumber of entries in an 'items' list.
1282eb4d8dcSEmmanuel Vadot
1292eb4d8dcSEmmanuel VadotThe YAML Devicetree format also makes all string values an array and scalar
1302eb4d8dcSEmmanuel Vadotvalues a matrix (in order to define groupings) even when only a single value
1312eb4d8dcSEmmanuel Vadotis present. Single entries in schemas are fixed up to match this encoding.
1322eb4d8dcSEmmanuel Vadot
133d5b0e70fSEmmanuel VadotCoding style
134d5b0e70fSEmmanuel Vadot------------
135d5b0e70fSEmmanuel Vadot
136d5b0e70fSEmmanuel VadotUse YAML coding style (two-space indentation). For DTS examples in the schema,
137d5b0e70fSEmmanuel Vadotpreferred is four-space indentation.
138d5b0e70fSEmmanuel Vadot
1392eb4d8dcSEmmanuel VadotTesting
1402eb4d8dcSEmmanuel Vadot-------
1412eb4d8dcSEmmanuel Vadot
1422eb4d8dcSEmmanuel VadotDependencies
1432eb4d8dcSEmmanuel Vadot~~~~~~~~~~~~
1442eb4d8dcSEmmanuel Vadot
1452eb4d8dcSEmmanuel VadotThe DT schema project must be installed in order to validate the DT schema
1462eb4d8dcSEmmanuel Vadotbinding documents and validate DTS files using the DT schema. The DT schema
1472eb4d8dcSEmmanuel Vadotproject can be installed with pip::
1482eb4d8dcSEmmanuel Vadot
149c9ccf3a3SEmmanuel Vadot    pip3 install dtschema
150c9ccf3a3SEmmanuel Vadot
151c9ccf3a3SEmmanuel VadotNote that 'dtschema' installation requires 'swig' and Python development files
152c9ccf3a3SEmmanuel Vadotinstalled first. On Debian/Ubuntu systems::
153c9ccf3a3SEmmanuel Vadot
154c9ccf3a3SEmmanuel Vadot    apt install swig python3-dev
1552eb4d8dcSEmmanuel Vadot
1562eb4d8dcSEmmanuel VadotSeveral executables (dt-doc-validate, dt-mk-schema, dt-validate) will be
1572eb4d8dcSEmmanuel Vadotinstalled. Ensure they are in your PATH (~/.local/bin by default).
1582eb4d8dcSEmmanuel Vadot
159d5b0e70fSEmmanuel VadotRecommended is also to install yamllint (used by dtschema when present).
160d5b0e70fSEmmanuel Vadot
1612eb4d8dcSEmmanuel VadotRunning checks
1622eb4d8dcSEmmanuel Vadot~~~~~~~~~~~~~~
1632eb4d8dcSEmmanuel Vadot
1642eb4d8dcSEmmanuel VadotThe DT schema binding documents must be validated using the meta-schema (the
1652eb4d8dcSEmmanuel Vadotschema for the schema) to ensure they are both valid json-schema and valid
1662eb4d8dcSEmmanuel Vadotbinding schema. All of the DT binding documents can be validated using the
1672eb4d8dcSEmmanuel Vadot``dt_binding_check`` target::
1682eb4d8dcSEmmanuel Vadot
1692eb4d8dcSEmmanuel Vadot    make dt_binding_check
1702eb4d8dcSEmmanuel Vadot
1712eb4d8dcSEmmanuel VadotIn order to perform validation of DT source files, use the ``dtbs_check`` target::
1722eb4d8dcSEmmanuel Vadot
1732eb4d8dcSEmmanuel Vadot    make dtbs_check
1742eb4d8dcSEmmanuel Vadot
1752eb4d8dcSEmmanuel VadotNote that ``dtbs_check`` will skip any binding schema files with errors. It is
1762eb4d8dcSEmmanuel Vadotnecessary to use ``dt_binding_check`` to get all the validation errors in the
1772eb4d8dcSEmmanuel Vadotbinding schema files.
1782eb4d8dcSEmmanuel Vadot
1792eb4d8dcSEmmanuel VadotIt is possible to run both in a single command::
1802eb4d8dcSEmmanuel Vadot
1812eb4d8dcSEmmanuel Vadot    make dt_binding_check dtbs_check
1822eb4d8dcSEmmanuel Vadot
183c9ccf3a3SEmmanuel VadotIt is also possible to run checks with a subset of matching schema files by
18484943d6fSEmmanuel Vadotsetting the ``DT_SCHEMA_FILES`` variable to 1 or more specific schema files or
18584943d6fSEmmanuel Vadotpatterns (partial match of a fixed string). Each file or pattern should be
18684943d6fSEmmanuel Vadotseparated by ':'.
1872eb4d8dcSEmmanuel Vadot
1882eb4d8dcSEmmanuel Vadot::
1892eb4d8dcSEmmanuel Vadot
190c9ccf3a3SEmmanuel Vadot    make dt_binding_check DT_SCHEMA_FILES=trivial-devices.yaml
19184943d6fSEmmanuel Vadot    make dt_binding_check DT_SCHEMA_FILES=trivial-devices.yaml:rtc.yaml
192c9ccf3a3SEmmanuel Vadot    make dt_binding_check DT_SCHEMA_FILES=/gpio/
193c9ccf3a3SEmmanuel Vadot    make dtbs_check DT_SCHEMA_FILES=trivial-devices.yaml
1942eb4d8dcSEmmanuel Vadot
1952eb4d8dcSEmmanuel Vadot
1962eb4d8dcSEmmanuel Vadotjson-schema Resources
1972eb4d8dcSEmmanuel Vadot---------------------
1982eb4d8dcSEmmanuel Vadot
1992eb4d8dcSEmmanuel Vadot
2002eb4d8dcSEmmanuel Vadot`JSON-Schema Specifications <http://json-schema.org/>`_
2012eb4d8dcSEmmanuel Vadot
2022eb4d8dcSEmmanuel Vadot`Using JSON Schema Book <http://usingjsonschema.com/>`_
2032eb4d8dcSEmmanuel Vadot
2042eb4d8dcSEmmanuel Vadot.. _example-schema:
2052eb4d8dcSEmmanuel Vadot
2062eb4d8dcSEmmanuel VadotAnnotated Example Schema
2072eb4d8dcSEmmanuel Vadot------------------------
2082eb4d8dcSEmmanuel Vadot
2092eb4d8dcSEmmanuel VadotAlso available as a separate file: :download:`example-schema.yaml`
2102eb4d8dcSEmmanuel Vadot
2112eb4d8dcSEmmanuel Vadot.. literalinclude:: example-schema.yaml
212