xref: /linux/Documentation/devicetree/bindings/writing-schema.rst (revision b83db5b8490073dfd27f4fd478b478f34e51bb36)
1d0793c3cSMauro Carvalho Chehab.. SPDX-License-Identifier: GPL-2.0
2cdea0121SMauro Carvalho Chehab
3*7248213cSRob HerringWriting Devicetree Bindings in json-schema
4cdea0121SMauro Carvalho Chehab==========================================
5cdea0121SMauro Carvalho Chehab
6cdea0121SMauro Carvalho ChehabDevicetree bindings are written using json-schema vocabulary. Schema files are
7cdea0121SMauro Carvalho Chehabwritten in a JSON compatible subset of YAML. YAML is used instead of JSON as it
843647929SGeert Uytterhoevenis considered more human readable and has some advantages such as allowing
9cdea0121SMauro Carvalho Chehabcomments (Prefixed with '#').
10cdea0121SMauro Carvalho Chehab
119be21f73SRob HerringAlso see :ref:`example-schema`.
129be21f73SRob Herring
13cdea0121SMauro Carvalho ChehabSchema Contents
14cdea0121SMauro Carvalho Chehab---------------
15cdea0121SMauro Carvalho Chehab
16cdea0121SMauro Carvalho ChehabEach schema doc is a structured json-schema which is defined by a set of
17cdea0121SMauro Carvalho Chehabtop-level properties. Generally, there is one binding defined per file. The
18cdea0121SMauro Carvalho Chehabtop-level json-schema properties used are:
19cdea0121SMauro Carvalho Chehab
20cdea0121SMauro Carvalho Chehab$id
21cdea0121SMauro Carvalho Chehab  A json-schema unique identifier string. The string must be a valid
22cdea0121SMauro Carvalho Chehab  URI typically containing the binding's filename and path. For DT schema, it must
23cdea0121SMauro Carvalho Chehab  begin with "http://devicetree.org/schemas/". The URL is used in constructing
2443647929SGeert Uytterhoeven  references to other files specified in schema "$ref" properties. A $ref value
25cdea0121SMauro Carvalho Chehab  with a leading '/' will have the hostname prepended. A $ref value a relative
26cdea0121SMauro Carvalho Chehab  path or filename only will be prepended with the hostname and path components
27cdea0121SMauro Carvalho Chehab  of the current schema file's '$id' value. A URL is used even for local files,
28cdea0121SMauro Carvalho Chehab  but there may not actually be files present at those locations.
29cdea0121SMauro Carvalho Chehab
30cdea0121SMauro Carvalho Chehab$schema
31cdea0121SMauro Carvalho Chehab  Indicates the meta-schema the schema file adheres to.
32cdea0121SMauro Carvalho Chehab
33cdea0121SMauro Carvalho Chehabtitle
34cdea0121SMauro Carvalho Chehab  A one line description on the contents of the binding schema.
35cdea0121SMauro Carvalho Chehab
36cdea0121SMauro Carvalho Chehabmaintainers
37cdea0121SMauro Carvalho Chehab  A DT specific property. Contains a list of email address(es)
38cdea0121SMauro Carvalho Chehab  for maintainers of this binding.
39cdea0121SMauro Carvalho Chehab
40cdea0121SMauro Carvalho Chehabdescription
41cdea0121SMauro Carvalho Chehab  Optional. A multi-line text block containing any detailed
42cdea0121SMauro Carvalho Chehab  information about this binding. It should contain things such as what the block
43cdea0121SMauro Carvalho Chehab  or device does, standards the device conforms to, and links to datasheets for
44cdea0121SMauro Carvalho Chehab  more information.
45cdea0121SMauro Carvalho Chehab
46cdea0121SMauro Carvalho Chehabselect
47cdea0121SMauro Carvalho Chehab  Optional. A json-schema used to match nodes for applying the
48cdea0121SMauro Carvalho Chehab  schema. By default without 'select', nodes are matched against their possible
49cdea0121SMauro Carvalho Chehab  compatible string values or node name. Most bindings should not need select.
50cdea0121SMauro Carvalho Chehab
51cdea0121SMauro Carvalho ChehaballOf
52cdea0121SMauro Carvalho Chehab  Optional. A list of other schemas to include. This is used to
53cdea0121SMauro Carvalho Chehab  include other schemas the binding conforms to. This may be schemas for a
54cdea0121SMauro Carvalho Chehab  particular class of devices such as I2C or SPI controllers.
55cdea0121SMauro Carvalho Chehab
56cdea0121SMauro Carvalho Chehabproperties
57cdea0121SMauro Carvalho Chehab  A set of sub-schema defining all the DT properties for the
58cdea0121SMauro Carvalho Chehab  binding. The exact schema syntax depends on whether properties are known,
59cdea0121SMauro Carvalho Chehab  common properties (e.g. 'interrupts') or are binding/vendor specific properties.
60cdea0121SMauro Carvalho Chehab
61cdea0121SMauro Carvalho ChehabA property can also define a child DT node with child properties defined
62cdea0121SMauro Carvalho Chehabunder it.
63cdea0121SMauro Carvalho Chehab
64cdea0121SMauro Carvalho ChehabFor more details on properties sections, see 'Property Schema' section.
65cdea0121SMauro Carvalho Chehab
66cdea0121SMauro Carvalho ChehabpatternProperties
67cdea0121SMauro Carvalho Chehab  Optional. Similar to 'properties', but names are regex.
68cdea0121SMauro Carvalho Chehab
69cdea0121SMauro Carvalho Chehabrequired
70cdea0121SMauro Carvalho Chehab  A list of DT properties from the 'properties' section that
71cdea0121SMauro Carvalho Chehab  must always be present.
72cdea0121SMauro Carvalho Chehab
73cdea0121SMauro Carvalho Chehabexamples
74cdea0121SMauro Carvalho Chehab  Optional. A list of one or more DTS hunks implementing the
75cdea0121SMauro Carvalho Chehab  binding. Note: YAML doesn't allow leading tabs, so spaces must be used instead.
76cdea0121SMauro Carvalho Chehab
77cdea0121SMauro Carvalho ChehabUnless noted otherwise, all properties are required.
78cdea0121SMauro Carvalho Chehab
79cdea0121SMauro Carvalho ChehabProperty Schema
80cdea0121SMauro Carvalho Chehab---------------
81cdea0121SMauro Carvalho Chehab
82cdea0121SMauro Carvalho ChehabThe 'properties' section of the schema contains all the DT properties for a
83cdea0121SMauro Carvalho Chehabbinding. Each property contains a set of constraints using json-schema
84cdea0121SMauro Carvalho Chehabvocabulary for that property. The properties schemas are what is used for
85cdea0121SMauro Carvalho Chehabvalidation of DT files.
86cdea0121SMauro Carvalho Chehab
87cdea0121SMauro Carvalho ChehabFor common properties, only additional constraints not covered by the common
88cdea0121SMauro Carvalho Chehabbinding schema need to be defined such as how many values are valid or what
89cdea0121SMauro Carvalho Chehabpossible values are valid.
90cdea0121SMauro Carvalho Chehab
91cdea0121SMauro Carvalho ChehabVendor specific properties will typically need more detailed schema. With the
92cdea0121SMauro Carvalho Chehabexception of boolean properties, they should have a reference to a type in
93cdea0121SMauro Carvalho Chehabschemas/types.yaml. A "description" property is always required.
94cdea0121SMauro Carvalho Chehab
95cdea0121SMauro Carvalho ChehabThe Devicetree schemas don't exactly match the YAML encoded DT data produced by
96cdea0121SMauro Carvalho Chehabdtc. They are simplified to make them more compact and avoid a bunch of
97cdea0121SMauro Carvalho Chehabboilerplate. The tools process the schema files to produce the final schema for
98cdea0121SMauro Carvalho Chehabvalidation. There are currently 2 transformations the tools perform.
99cdea0121SMauro Carvalho Chehab
100cdea0121SMauro Carvalho ChehabThe default for arrays in json-schema is they are variable sized and allow more
101cdea0121SMauro Carvalho Chehabentries than explicitly defined. This can be restricted by defining 'minItems',
102cdea0121SMauro Carvalho Chehab'maxItems', and 'additionalItems'. However, for DeviceTree Schemas, a fixed
103cdea0121SMauro Carvalho Chehabsize is desired in most cases, so these properties are added based on the
104cdea0121SMauro Carvalho Chehabnumber of entries in an 'items' list.
105cdea0121SMauro Carvalho Chehab
106cdea0121SMauro Carvalho ChehabThe YAML Devicetree format also makes all string values an array and scalar
107cdea0121SMauro Carvalho Chehabvalues a matrix (in order to define groupings) even when only a single value
108cdea0121SMauro Carvalho Chehabis present. Single entries in schemas are fixed up to match this encoding.
109cdea0121SMauro Carvalho Chehab
110cdea0121SMauro Carvalho ChehabTesting
111cdea0121SMauro Carvalho Chehab-------
112cdea0121SMauro Carvalho Chehab
113cdea0121SMauro Carvalho ChehabDependencies
114cdea0121SMauro Carvalho Chehab~~~~~~~~~~~~
115cdea0121SMauro Carvalho Chehab
116cdea0121SMauro Carvalho ChehabThe DT schema project must be installed in order to validate the DT schema
117cdea0121SMauro Carvalho Chehabbinding documents and validate DTS files using the DT schema. The DT schema
118cdea0121SMauro Carvalho Chehabproject can be installed with pip::
119cdea0121SMauro Carvalho Chehab
120cdea0121SMauro Carvalho Chehab    pip3 install git+https://github.com/devicetree-org/dt-schema.git@master
121cdea0121SMauro Carvalho Chehab
1227054c207SRob HerringSeveral executables (dt-doc-validate, dt-mk-schema, dt-validate) will be
1237054c207SRob Herringinstalled. Ensure they are in your PATH (~/.local/bin by default).
1247054c207SRob Herring
125cdea0121SMauro Carvalho Chehabdtc must also be built with YAML output support enabled. This requires that
1267dcde0f3SLinus Walleijlibyaml and its headers be installed on the host system. For some distributions
1277dcde0f3SLinus Walleijthat involves installing the development package, such as:
1287dcde0f3SLinus Walleij
129d0793c3cSMauro Carvalho ChehabDebian::
130d0793c3cSMauro Carvalho Chehab
1317dcde0f3SLinus Walleij  apt-get install libyaml-dev
132d0793c3cSMauro Carvalho Chehab
133d0793c3cSMauro Carvalho ChehabFedora::
134d0793c3cSMauro Carvalho Chehab
1357dcde0f3SLinus Walleij  dnf -y install libyaml-devel
136cdea0121SMauro Carvalho Chehab
137cdea0121SMauro Carvalho ChehabRunning checks
138cdea0121SMauro Carvalho Chehab~~~~~~~~~~~~~~
139cdea0121SMauro Carvalho Chehab
140cdea0121SMauro Carvalho ChehabThe DT schema binding documents must be validated using the meta-schema (the
141cdea0121SMauro Carvalho Chehabschema for the schema) to ensure they are both valid json-schema and valid
142cdea0121SMauro Carvalho Chehabbinding schema. All of the DT binding documents can be validated using the
143cdea0121SMauro Carvalho Chehab``dt_binding_check`` target::
144cdea0121SMauro Carvalho Chehab
145cdea0121SMauro Carvalho Chehab    make dt_binding_check
146cdea0121SMauro Carvalho Chehab
14793512dadSRob HerringIn order to perform validation of DT source files, use the ``dtbs_check`` target::
148cdea0121SMauro Carvalho Chehab
149cdea0121SMauro Carvalho Chehab    make dtbs_check
150cdea0121SMauro Carvalho Chehab
15193512dadSRob HerringNote that ``dtbs_check`` will skip any binding schema files with errors. It is
15293512dadSRob Herringnecessary to use ``dt_binding_check`` to get all the validation errors in the
15393512dadSRob Herringbinding schema files.
154cdea0121SMauro Carvalho Chehab
155e10c4321SMasahiro YamadaIt is possible to run both in a single command::
156e10c4321SMasahiro Yamada
157e10c4321SMasahiro Yamada    make dt_binding_check dtbs_check
158e10c4321SMasahiro Yamada
159cdea0121SMauro Carvalho ChehabIt is also possible to run checks with a single schema file by setting the
160cdea0121SMauro Carvalho Chehab``DT_SCHEMA_FILES`` variable to a specific schema file.
161cdea0121SMauro Carvalho Chehab
162cdea0121SMauro Carvalho Chehab::
163cdea0121SMauro Carvalho Chehab
1647aa8dd91SStephen Boyd    make dt_binding_check DT_SCHEMA_FILES=Documentation/devicetree/bindings/trivial-devices.yaml
165cdea0121SMauro Carvalho Chehab    make dtbs_check DT_SCHEMA_FILES=Documentation/devicetree/bindings/trivial-devices.yaml
166cdea0121SMauro Carvalho Chehab
167cdea0121SMauro Carvalho Chehab
168cdea0121SMauro Carvalho Chehabjson-schema Resources
169cdea0121SMauro Carvalho Chehab---------------------
170cdea0121SMauro Carvalho Chehab
171cdea0121SMauro Carvalho Chehab
172cdea0121SMauro Carvalho Chehab`JSON-Schema Specifications <http://json-schema.org/>`_
173cdea0121SMauro Carvalho Chehab
174cdea0121SMauro Carvalho Chehab`Using JSON Schema Book <http://usingjsonschema.com/>`_
1759be21f73SRob Herring
1769be21f73SRob Herring.. _example-schema:
1779be21f73SRob Herring
1789be21f73SRob HerringAnnotated Example Schema
1799be21f73SRob Herring------------------------
1809be21f73SRob Herring
1819be21f73SRob HerringAlso available as a separate file: :download:`example-schema.yaml`
1829be21f73SRob Herring
1839be21f73SRob Herring.. literalinclude:: example-schema.yaml
184