xref: /openbmc/linux/Documentation/devicetree/bindings/writing-schema.rst (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1b83db5b8SRob Herring.. SPDX-License-Identifier: GPL-2.0
2b83db5b8SRob Herring
3b83db5b8SRob HerringWriting Devicetree Bindings in json-schema
4b83db5b8SRob Herring==========================================
5b83db5b8SRob Herring
6b83db5b8SRob HerringDevicetree bindings are written using json-schema vocabulary. Schema files are
737ef2c34SSimon Glasswritten in a JSON-compatible subset of YAML. YAML is used instead of JSON as it
8b83db5b8SRob Herringis considered more human readable and has some advantages such as allowing
9b83db5b8SRob Herringcomments (Prefixed with '#').
10b83db5b8SRob Herring
11b83db5b8SRob HerringAlso see :ref:`example-schema`.
12b83db5b8SRob Herring
13b83db5b8SRob HerringSchema Contents
14b83db5b8SRob Herring---------------
15b83db5b8SRob Herring
16b83db5b8SRob HerringEach schema doc is a structured json-schema which is defined by a set of
17b83db5b8SRob Herringtop-level properties. Generally, there is one binding defined per file. The
18b83db5b8SRob Herringtop-level json-schema properties used are:
19b83db5b8SRob Herring
20b83db5b8SRob Herring$id
21b83db5b8SRob Herring  A json-schema unique identifier string. The string must be a valid
22b83db5b8SRob Herring  URI typically containing the binding's filename and path. For DT schema, it must
23b83db5b8SRob Herring  begin with "http://devicetree.org/schemas/". The URL is used in constructing
24b83db5b8SRob Herring  references to other files specified in schema "$ref" properties. A $ref value
2537ef2c34SSimon Glass  with a leading '/' will have the hostname prepended. A $ref value with only a
2637ef2c34SSimon Glass  relative path or filename will be prepended with the hostname and path
2737ef2c34SSimon Glass  components of the current schema file's '$id' value. A URL is used even for
2837ef2c34SSimon Glass  local files, but there may not actually be files present at those locations.
29b83db5b8SRob Herring
30b83db5b8SRob Herring$schema
31b83db5b8SRob Herring  Indicates the meta-schema the schema file adheres to.
32b83db5b8SRob Herring
33b83db5b8SRob Herringtitle
3437ef2c34SSimon Glass  A one-line description on the contents of the binding schema.
35b83db5b8SRob Herring
36b83db5b8SRob Herringmaintainers
37b83db5b8SRob Herring  A DT specific property. Contains a list of email address(es)
38b83db5b8SRob Herring  for maintainers of this binding.
39b83db5b8SRob Herring
40b83db5b8SRob Herringdescription
41b83db5b8SRob Herring  Optional. A multi-line text block containing any detailed
42b83db5b8SRob Herring  information about this binding. It should contain things such as what the block
43b83db5b8SRob Herring  or device does, standards the device conforms to, and links to datasheets for
44b83db5b8SRob Herring  more information.
45b83db5b8SRob Herring
46b83db5b8SRob Herringselect
47b83db5b8SRob Herring  Optional. A json-schema used to match nodes for applying the
4837ef2c34SSimon Glass  schema. By default, without 'select', nodes are matched against their possible
4937ef2c34SSimon Glass  compatible-string values or node name. Most bindings should not need select.
50b83db5b8SRob Herring
51b83db5b8SRob HerringallOf
52b83db5b8SRob Herring  Optional. A list of other schemas to include. This is used to
53b83db5b8SRob Herring  include other schemas the binding conforms to. This may be schemas for a
54b83db5b8SRob Herring  particular class of devices such as I2C or SPI controllers.
55b83db5b8SRob Herring
56b83db5b8SRob Herringproperties
57b83db5b8SRob Herring  A set of sub-schema defining all the DT properties for the
58b83db5b8SRob Herring  binding. The exact schema syntax depends on whether properties are known,
5937ef2c34SSimon Glass  common properties (e.g. 'interrupts') or are binding/vendor-specific
6037ef2c34SSimon Glass  properties.
61b83db5b8SRob Herring
62b83db5b8SRob HerringA property can also define a child DT node with child properties defined
63b83db5b8SRob Herringunder it.
64b83db5b8SRob Herring
65b83db5b8SRob HerringFor more details on properties sections, see 'Property Schema' section.
66b83db5b8SRob Herring
67b83db5b8SRob HerringpatternProperties
68b83db5b8SRob Herring  Optional. Similar to 'properties', but names are regex.
69b83db5b8SRob Herring
70b83db5b8SRob Herringrequired
71b83db5b8SRob Herring  A list of DT properties from the 'properties' section that
72b83db5b8SRob Herring  must always be present.
73b83db5b8SRob Herring
74b83db5b8SRob Herringexamples
75b83db5b8SRob Herring  Optional. A list of one or more DTS hunks implementing the
76b83db5b8SRob Herring  binding. Note: YAML doesn't allow leading tabs, so spaces must be used instead.
77b83db5b8SRob Herring
78b83db5b8SRob HerringUnless noted otherwise, all properties are required.
79b83db5b8SRob Herring
80b83db5b8SRob HerringProperty Schema
81b83db5b8SRob Herring---------------
82b83db5b8SRob Herring
83b83db5b8SRob HerringThe 'properties' section of the schema contains all the DT properties for a
84b83db5b8SRob Herringbinding. Each property contains a set of constraints using json-schema
8537ef2c34SSimon Glassvocabulary for that property. The properties schemas are what are used for
86b83db5b8SRob Herringvalidation of DT files.
87b83db5b8SRob Herring
8837ef2c34SSimon GlassFor common properties, only additional constraints not covered by the common,
89b83db5b8SRob Herringbinding schema need to be defined such as how many values are valid or what
90b83db5b8SRob Herringpossible values are valid.
91b83db5b8SRob Herring
9237ef2c34SSimon GlassVendor-specific properties will typically need more detailed schema. With the
93b83db5b8SRob Herringexception of boolean properties, they should have a reference to a type in
94b83db5b8SRob Herringschemas/types.yaml. A "description" property is always required.
95b83db5b8SRob Herring
9637ef2c34SSimon GlassThe Devicetree schemas don't exactly match the YAML-encoded DT data produced by
97b83db5b8SRob Herringdtc. They are simplified to make them more compact and avoid a bunch of
98b83db5b8SRob Herringboilerplate. The tools process the schema files to produce the final schema for
99b83db5b8SRob Herringvalidation. There are currently 2 transformations the tools perform.
100b83db5b8SRob Herring
10137ef2c34SSimon GlassThe default for arrays in json-schema is they are variable-sized and allow more
102b83db5b8SRob Herringentries than explicitly defined. This can be restricted by defining 'minItems',
103b83db5b8SRob Herring'maxItems', and 'additionalItems'. However, for DeviceTree Schemas, a fixed
104b83db5b8SRob Herringsize is desired in most cases, so these properties are added based on the
105b83db5b8SRob Herringnumber of entries in an 'items' list.
106b83db5b8SRob Herring
107b83db5b8SRob HerringThe YAML Devicetree format also makes all string values an array and scalar
108b83db5b8SRob Herringvalues a matrix (in order to define groupings) even when only a single value
109b83db5b8SRob Herringis present. Single entries in schemas are fixed up to match this encoding.
110b83db5b8SRob Herring
1117a150b0dSKrzysztof KozlowskiCoding style
1127a150b0dSKrzysztof Kozlowski------------
1137a150b0dSKrzysztof Kozlowski
1147a150b0dSKrzysztof KozlowskiUse YAML coding style (two-space indentation). For DTS examples in the schema,
1157a150b0dSKrzysztof Kozlowskipreferred is four-space indentation.
1167a150b0dSKrzysztof Kozlowski
117b83db5b8SRob HerringTesting
118b83db5b8SRob Herring-------
119b83db5b8SRob Herring
120b83db5b8SRob HerringDependencies
121b83db5b8SRob Herring~~~~~~~~~~~~
122b83db5b8SRob Herring
123b83db5b8SRob HerringThe DT schema project must be installed in order to validate the DT schema
124b83db5b8SRob Herringbinding documents and validate DTS files using the DT schema. The DT schema
125b83db5b8SRob Herringproject can be installed with pip::
126b83db5b8SRob Herring
1272b709a72SKrzysztof Kozlowski    pip3 install dtschema
128b83db5b8SRob Herring
129ef8795f3SRob HerringNote that 'dtschema' installation requires 'swig' and Python development files
130ef8795f3SRob Herringinstalled first. On Debian/Ubuntu systems::
131ef8795f3SRob Herring
132ef8795f3SRob Herring    apt install swig python3-dev
133ef8795f3SRob Herring
134b83db5b8SRob HerringSeveral executables (dt-doc-validate, dt-mk-schema, dt-validate) will be
135b83db5b8SRob Herringinstalled. Ensure they are in your PATH (~/.local/bin by default).
136b83db5b8SRob Herring
137*fe7ff911SKrzysztof KozlowskiRecommended is also to install yamllint (used by dtschema when present).
138*fe7ff911SKrzysztof Kozlowski
139b83db5b8SRob HerringRunning checks
140b83db5b8SRob Herring~~~~~~~~~~~~~~
141b83db5b8SRob Herring
142b83db5b8SRob HerringThe DT schema binding documents must be validated using the meta-schema (the
143b83db5b8SRob Herringschema for the schema) to ensure they are both valid json-schema and valid
144b83db5b8SRob Herringbinding schema. All of the DT binding documents can be validated using the
145b83db5b8SRob Herring``dt_binding_check`` target::
146b83db5b8SRob Herring
147b83db5b8SRob Herring    make dt_binding_check
148b83db5b8SRob Herring
149b83db5b8SRob HerringIn order to perform validation of DT source files, use the ``dtbs_check`` target::
150b83db5b8SRob Herring
151b83db5b8SRob Herring    make dtbs_check
152b83db5b8SRob Herring
153b83db5b8SRob HerringNote that ``dtbs_check`` will skip any binding schema files with errors. It is
154b83db5b8SRob Herringnecessary to use ``dt_binding_check`` to get all the validation errors in the
155b83db5b8SRob Herringbinding schema files.
156b83db5b8SRob Herring
157b83db5b8SRob HerringIt is possible to run both in a single command::
158b83db5b8SRob Herring
159b83db5b8SRob Herring    make dt_binding_check dtbs_check
160b83db5b8SRob Herring
161309d9559SRob HerringIt is also possible to run checks with a subset of matching schema files by
162309d9559SRob Herringsetting the ``DT_SCHEMA_FILES`` variable to a specific schema file or pattern.
163b83db5b8SRob Herring
164b83db5b8SRob Herring::
165b83db5b8SRob Herring
166309d9559SRob Herring    make dt_binding_check DT_SCHEMA_FILES=trivial-devices.yaml
167309d9559SRob Herring    make dt_binding_check DT_SCHEMA_FILES=/gpio/
168309d9559SRob Herring    make dtbs_check DT_SCHEMA_FILES=trivial-devices.yaml
169b83db5b8SRob Herring
170b83db5b8SRob Herring
171b83db5b8SRob Herringjson-schema Resources
172b83db5b8SRob Herring---------------------
173b83db5b8SRob Herring
174b83db5b8SRob Herring
175b83db5b8SRob Herring`JSON-Schema Specifications <http://json-schema.org/>`_
176b83db5b8SRob Herring
177b83db5b8SRob Herring`Using JSON Schema Book <http://usingjsonschema.com/>`_
178b83db5b8SRob Herring
179b83db5b8SRob Herring.. _example-schema:
180b83db5b8SRob Herring
181b83db5b8SRob HerringAnnotated Example Schema
182b83db5b8SRob Herring------------------------
183b83db5b8SRob Herring
184b83db5b8SRob HerringAlso available as a separate file: :download:`example-schema.yaml`
185b83db5b8SRob Herring
186b83db5b8SRob Herring.. literalinclude:: example-schema.yaml
187