Back to home page

OSCL-LXR

 
 

    


0001 .. SPDX-License-Identifier: GPL-2.0
0002 
0003 Writing Devicetree Bindings in json-schema
0004 ==========================================
0005 
0006 Devicetree bindings are written using json-schema vocabulary. Schema files are
0007 written in a JSON-compatible subset of YAML. YAML is used instead of JSON as it
0008 is considered more human readable and has some advantages such as allowing
0009 comments (Prefixed with '#').
0010 
0011 Also see :ref:`example-schema`.
0012 
0013 Schema Contents
0014 ---------------
0015 
0016 Each schema doc is a structured json-schema which is defined by a set of
0017 top-level properties. Generally, there is one binding defined per file. The
0018 top-level json-schema properties used are:
0019 
0020 $id
0021   A json-schema unique identifier string. The string must be a valid
0022   URI typically containing the binding's filename and path. For DT schema, it must
0023   begin with "http://devicetree.org/schemas/". The URL is used in constructing
0024   references to other files specified in schema "$ref" properties. A $ref value
0025   with a leading '/' will have the hostname prepended. A $ref value with only a
0026   relative path or filename will be prepended with the hostname and path
0027   components of the current schema file's '$id' value. A URL is used even for
0028   local files, but there may not actually be files present at those locations.
0029 
0030 $schema
0031   Indicates the meta-schema the schema file adheres to.
0032 
0033 title
0034   A one-line description on the contents of the binding schema.
0035 
0036 maintainers
0037   A DT specific property. Contains a list of email address(es)
0038   for maintainers of this binding.
0039 
0040 description
0041   Optional. A multi-line text block containing any detailed
0042   information about this binding. It should contain things such as what the block
0043   or device does, standards the device conforms to, and links to datasheets for
0044   more information.
0045 
0046 select
0047   Optional. A json-schema used to match nodes for applying the
0048   schema. By default, without 'select', nodes are matched against their possible
0049   compatible-string values or node name. Most bindings should not need select.
0050 
0051 allOf
0052   Optional. A list of other schemas to include. This is used to
0053   include other schemas the binding conforms to. This may be schemas for a
0054   particular class of devices such as I2C or SPI controllers.
0055 
0056 properties
0057   A set of sub-schema defining all the DT properties for the
0058   binding. The exact schema syntax depends on whether properties are known,
0059   common properties (e.g. 'interrupts') or are binding/vendor-specific
0060   properties.
0061 
0062 A property can also define a child DT node with child properties defined
0063 under it.
0064 
0065 For more details on properties sections, see 'Property Schema' section.
0066 
0067 patternProperties
0068   Optional. Similar to 'properties', but names are regex.
0069 
0070 required
0071   A list of DT properties from the 'properties' section that
0072   must always be present.
0073 
0074 examples
0075   Optional. A list of one or more DTS hunks implementing the
0076   binding. Note: YAML doesn't allow leading tabs, so spaces must be used instead.
0077 
0078 Unless noted otherwise, all properties are required.
0079 
0080 Property Schema
0081 ---------------
0082 
0083 The 'properties' section of the schema contains all the DT properties for a
0084 binding. Each property contains a set of constraints using json-schema
0085 vocabulary for that property. The properties schemas are what are used for
0086 validation of DT files.
0087 
0088 For common properties, only additional constraints not covered by the common,
0089 binding schema need to be defined such as how many values are valid or what
0090 possible values are valid.
0091 
0092 Vendor-specific properties will typically need more detailed schema. With the
0093 exception of boolean properties, they should have a reference to a type in
0094 schemas/types.yaml. A "description" property is always required.
0095 
0096 The Devicetree schemas don't exactly match the YAML-encoded DT data produced by
0097 dtc. They are simplified to make them more compact and avoid a bunch of
0098 boilerplate. The tools process the schema files to produce the final schema for
0099 validation. There are currently 2 transformations the tools perform.
0100 
0101 The default for arrays in json-schema is they are variable-sized and allow more
0102 entries than explicitly defined. This can be restricted by defining 'minItems',
0103 'maxItems', and 'additionalItems'. However, for DeviceTree Schemas, a fixed
0104 size is desired in most cases, so these properties are added based on the
0105 number of entries in an 'items' list.
0106 
0107 The YAML Devicetree format also makes all string values an array and scalar
0108 values a matrix (in order to define groupings) even when only a single value
0109 is present. Single entries in schemas are fixed up to match this encoding.
0110 
0111 Coding style
0112 ------------
0113 
0114 Use YAML coding style (two-space indentation). For DTS examples in the schema,
0115 preferred is four-space indentation.
0116 
0117 Testing
0118 -------
0119 
0120 Dependencies
0121 ~~~~~~~~~~~~
0122 
0123 The DT schema project must be installed in order to validate the DT schema
0124 binding documents and validate DTS files using the DT schema. The DT schema
0125 project can be installed with pip::
0126 
0127     pip3 install dtschema
0128 
0129 Note that 'dtschema' installation requires 'swig' and Python development files
0130 installed first. On Debian/Ubuntu systems::
0131 
0132     apt install swig python3-dev
0133 
0134 Several executables (dt-doc-validate, dt-mk-schema, dt-validate) will be
0135 installed. Ensure they are in your PATH (~/.local/bin by default).
0136 
0137 Recommended is also to install yamllint (used by dtschema when present).
0138 
0139 Running checks
0140 ~~~~~~~~~~~~~~
0141 
0142 The DT schema binding documents must be validated using the meta-schema (the
0143 schema for the schema) to ensure they are both valid json-schema and valid
0144 binding schema. All of the DT binding documents can be validated using the
0145 ``dt_binding_check`` target::
0146 
0147     make dt_binding_check
0148 
0149 In order to perform validation of DT source files, use the ``dtbs_check`` target::
0150 
0151     make dtbs_check
0152 
0153 Note that ``dtbs_check`` will skip any binding schema files with errors. It is
0154 necessary to use ``dt_binding_check`` to get all the validation errors in the
0155 binding schema files.
0156 
0157 It is possible to run both in a single command::
0158 
0159     make dt_binding_check dtbs_check
0160 
0161 It is also possible to run checks with a subset of matching schema files by
0162 setting the ``DT_SCHEMA_FILES`` variable to a specific schema file or pattern.
0163 
0164 ::
0165 
0166     make dt_binding_check DT_SCHEMA_FILES=trivial-devices.yaml
0167     make dt_binding_check DT_SCHEMA_FILES=/gpio/
0168     make dtbs_check DT_SCHEMA_FILES=trivial-devices.yaml
0169 
0170 
0171 json-schema Resources
0172 ---------------------
0173 
0174 
0175 `JSON-Schema Specifications <http://json-schema.org/>`_
0176 
0177 `Using JSON Schema Book <http://usingjsonschema.com/>`_
0178 
0179 .. _example-schema:
0180 
0181 Annotated Example Schema
0182 ------------------------
0183 
0184 Also available as a separate file: :download:`example-schema.yaml`
0185 
0186 .. literalinclude:: example-schema.yaml