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