1.. SPDX-License-Identifier: GPL-2.0
2
3Writing Devicetree Bindings in json-schema
4==========================================
5
6Devicetree bindings are written using json-schema vocabulary. Schema files are
7written in a JSON compatible subset of YAML. YAML is used instead of JSON as it
8is considered more human readable and has some advantages such as allowing
9comments (Prefixed with '#').
10
11Also see :ref:`example-schema`.
12
13Schema Contents
14---------------
15
16Each schema doc is a structured json-schema which is defined by a set of
17top-level properties. Generally, there is one binding defined per file. The
18top-level json-schema properties used are:
19
20$id
21  A json-schema unique identifier string. The string must be a valid
22  URI typically containing the binding's filename and path. For DT schema, it must
23  begin with "http://devicetree.org/schemas/". The URL is used in constructing
24  references to other files specified in schema "$ref" properties. A $ref value
25  with a leading '/' will have the hostname prepended. A $ref value a relative
26  path or filename only will be prepended with the hostname and path components
27  of the current schema file's '$id' value. A URL is used even for local files,
28  but there may not actually be files present at those locations.
29
30$schema
31  Indicates the meta-schema the schema file adheres to.
32
33title
34  A one line description on the contents of the binding schema.
35
36maintainers
37  A DT specific property. Contains a list of email address(es)
38  for maintainers of this binding.
39
40description
41  Optional. A multi-line text block containing any detailed
42  information about this binding. It should contain things such as what the block
43  or device does, standards the device conforms to, and links to datasheets for
44  more information.
45
46select
47  Optional. A json-schema used to match nodes for applying the
48  schema. By default without 'select', nodes are matched against their possible
49  compatible string values or node name. Most bindings should not need select.
50
51allOf
52  Optional. A list of other schemas to include. This is used to
53  include other schemas the binding conforms to. This may be schemas for a
54  particular class of devices such as I2C or SPI controllers.
55
56properties
57  A set of sub-schema defining all the DT properties for the
58  binding. The exact schema syntax depends on whether properties are known,
59  common properties (e.g. 'interrupts') or are binding/vendor specific properties.
60
61A property can also define a child DT node with child properties defined
62under it.
63
64For more details on properties sections, see 'Property Schema' section.
65
66patternProperties
67  Optional. Similar to 'properties', but names are regex.
68
69required
70  A list of DT properties from the 'properties' section that
71  must always be present.
72
73examples
74  Optional. A list of one or more DTS hunks implementing the
75  binding. Note: YAML doesn't allow leading tabs, so spaces must be used instead.
76
77Unless noted otherwise, all properties are required.
78
79Property Schema
80---------------
81
82The 'properties' section of the schema contains all the DT properties for a
83binding. Each property contains a set of constraints using json-schema
84vocabulary for that property. The properties schemas are what is used for
85validation of DT files.
86
87For common properties, only additional constraints not covered by the common
88binding schema need to be defined such as how many values are valid or what
89possible values are valid.
90
91Vendor specific properties will typically need more detailed schema. With the
92exception of boolean properties, they should have a reference to a type in
93schemas/types.yaml. A "description" property is always required.
94
95The Devicetree schemas don't exactly match the YAML encoded DT data produced by
96dtc. They are simplified to make them more compact and avoid a bunch of
97boilerplate. The tools process the schema files to produce the final schema for
98validation. There are currently 2 transformations the tools perform.
99
100The default for arrays in json-schema is they are variable sized and allow more
101entries than explicitly defined. This can be restricted by defining 'minItems',
102'maxItems', and 'additionalItems'. However, for DeviceTree Schemas, a fixed
103size is desired in most cases, so these properties are added based on the
104number of entries in an 'items' list.
105
106The YAML Devicetree format also makes all string values an array and scalar
107values a matrix (in order to define groupings) even when only a single value
108is present. Single entries in schemas are fixed up to match this encoding.
109
110Testing
111-------
112
113Dependencies
114~~~~~~~~~~~~
115
116The DT schema project must be installed in order to validate the DT schema
117binding documents and validate DTS files using the DT schema. The DT schema
118project can be installed with pip::
119
120    pip3 install git+https://github.com/devicetree-org/dt-schema.git@master
121
122Several executables (dt-doc-validate, dt-mk-schema, dt-validate) will be
123installed. Ensure they are in your PATH (~/.local/bin by default).
124
125dtc must also be built with YAML output support enabled. This requires that
126libyaml and its headers be installed on the host system. For some distributions
127that involves installing the development package, such as:
128
129Debian::
130
131  apt-get install libyaml-dev
132
133Fedora::
134
135  dnf -y install libyaml-devel
136
137Running checks
138~~~~~~~~~~~~~~
139
140The DT schema binding documents must be validated using the meta-schema (the
141schema for the schema) to ensure they are both valid json-schema and valid
142binding schema. All of the DT binding documents can be validated using the
143``dt_binding_check`` target::
144
145    make dt_binding_check
146
147In order to perform validation of DT source files, use the ``dtbs_check`` target::
148
149    make dtbs_check
150
151Note that ``dtbs_check`` will skip any binding schema files with errors. It is
152necessary to use ``dt_binding_check`` to get all the validation errors in the
153binding schema files.
154
155It is possible to run both in a single command::
156
157    make dt_binding_check dtbs_check
158
159It is also possible to run checks with a single schema file by setting the
160``DT_SCHEMA_FILES`` variable to a specific schema file.
161
162::
163
164    make dt_binding_check DT_SCHEMA_FILES=Documentation/devicetree/bindings/trivial-devices.yaml
165    make dtbs_check DT_SCHEMA_FILES=Documentation/devicetree/bindings/trivial-devices.yaml
166
167
168json-schema Resources
169---------------------
170
171
172`JSON-Schema Specifications <http://json-schema.org/>`_
173
174`Using JSON Schema Book <http://usingjsonschema.com/>`_
175
176.. _example-schema:
177
178Annotated Example Schema
179------------------------
180
181Also available as a separate file: :download:`example-schema.yaml`
182
183.. literalinclude:: example-schema.yaml
184