212 lines · plain
1.. SPDX-License-Identifier: GPL-2.02 3Writing Devicetree Bindings in json-schema4==========================================5 6Devicetree bindings are written using json-schema vocabulary. Schema files are7written in a JSON-compatible subset of YAML. YAML is used instead of JSON as it8is considered more human readable and has some advantages such as allowing9comments (Prefixed with '#').10 11Also see :ref:`example-schema`.12 13Schema Contents14---------------15 16Each schema doc is a structured json-schema which is defined by a set of17top-level properties. Generally, there is one binding defined per file. The18top-level json-schema properties used are:19 20$id21 A json-schema unique identifier string. The string must be a valid22 URI typically containing the binding's filename and path. For DT schema, it must23 begin with "http://devicetree.org/schemas/". The URL is used in constructing24 references to other files specified in schema "$ref" properties. A $ref value25 with a leading '/' will have the hostname prepended. A $ref value with only a26 relative path or filename will be prepended with the hostname and path27 components of the current schema file's '$id' value. A URL is used even for28 local files, but there may not actually be files present at those locations.29 30$schema31 Indicates the meta-schema the schema file adheres to.32 33title34 A one-line description of the hardware being described in the binding schema.35 36maintainers37 A DT specific property. Contains a list of email address(es)38 for maintainers of this binding.39 40description41 Optional. A multi-line text block containing any detailed42 information about this hardware. It should contain things such as what the block43 or device does, standards the device conforms to, and links to datasheets for44 more information.45 46select47 Optional. A json-schema used to match nodes for applying the48 schema. By default, without 'select', nodes are matched against their possible49 compatible-string values or node name. Most bindings should not need select.50 51allOf52 Optional. A list of other schemas to include. This is used to53 include other schemas the binding conforms to. This may be schemas for a54 particular class of devices such as I2C or SPI controllers.55 56properties57 A set of sub-schema defining all the DT properties for the58 binding. The exact schema syntax depends on whether properties are known,59 common properties (e.g. 'interrupts') or are binding/vendor-specific60 properties.61 62A property can also define a child DT node with child properties defined63under it.64 65For more details on properties sections, see 'Property Schema' section.66 67patternProperties68 Optional. Similar to 'properties', but names are regex.69 70required71 A list of DT properties from the 'properties' section that72 must always be present.73 74additionalProperties / unevaluatedProperties75 Keywords controlling how schema will validate properties not matched by this76 schema's 'properties' or 'patternProperties'. Each schema is supposed to77 have exactly one of these keywords in top-level part, so either78 additionalProperties or unevaluatedProperties. Nested nodes, so properties79 being objects, are supposed to have one as well.80 81 * additionalProperties: false82 Most common case, where no additional schema is referenced or if this83 binding allows subset of properties from other referenced schemas.84 85 * unevaluatedProperties: false86 Used when this binding references other schema whose all properties87 should be allowed.88 89 * additionalProperties: true90 Rare case, used for schemas implementing common set of properties. Such91 schemas are supposed to be referenced by other schemas, which then use92 'unevaluatedProperties: false'. Typically bus or common-part schemas.93 94examples95 Optional. A list of one or more DTS hunks implementing this binding only.96 Example should not contain unrelated device nodes, e.g. consumer nodes in a97 provider binding, other nodes referenced by phandle.98 Note: YAML doesn't allow leading tabs, so spaces must be used instead.99 100Unless noted otherwise, all properties are required.101 102Property Schema103---------------104 105The 'properties' section of the schema contains all the DT properties for a106binding. Each property contains a set of constraints using json-schema107vocabulary for that property. The properties schemas are what are used for108validation of DT files.109 110For common properties, only additional constraints not covered by the common,111binding schema need to be defined such as how many values are valid or what112possible values are valid.113 114Vendor-specific properties will typically need more detailed schema. With the115exception of boolean properties, they should have a reference to a type in116schemas/types.yaml. A "description" property is always required.117 118The Devicetree schemas don't exactly match the YAML-encoded DT data produced by119dtc. They are simplified to make them more compact and avoid a bunch of120boilerplate. The tools process the schema files to produce the final schema for121validation. There are currently 2 transformations the tools perform.122 123The default for arrays in json-schema is they are variable-sized and allow more124entries than explicitly defined. This can be restricted by defining 'minItems',125'maxItems', and 'additionalItems'. However, for DeviceTree Schemas, a fixed126size is desired in most cases, so these properties are added based on the127number of entries in an 'items' list.128 129The YAML Devicetree format also makes all string values an array and scalar130values a matrix (in order to define groupings) even when only a single value131is present. Single entries in schemas are fixed up to match this encoding.132 133Coding style134------------135 136Use YAML coding style (two-space indentation). For DTS examples in the schema,137preferred is four-space indentation.138 139Testing140-------141 142Dependencies143~~~~~~~~~~~~144 145The DT schema project must be installed in order to validate the DT schema146binding documents and validate DTS files using the DT schema. The DT schema147project can be installed with pip::148 149 pip3 install dtschema150 151Note that 'dtschema' installation requires 'swig' and Python development files152installed first. On Debian/Ubuntu systems::153 154 apt install swig python3-dev155 156Several executables (dt-doc-validate, dt-mk-schema, dt-validate) will be157installed. Ensure they are in your PATH (~/.local/bin by default).158 159Recommended is also to install yamllint (used by dtschema when present).160 161Running checks162~~~~~~~~~~~~~~163 164The DT schema binding documents must be validated using the meta-schema (the165schema for the schema) to ensure they are both valid json-schema and valid166binding schema. All of the DT binding documents can be validated using the167``dt_binding_check`` target::168 169 make dt_binding_check170 171In order to perform validation of DT source files, use the ``dtbs_check`` target::172 173 make dtbs_check174 175Note that ``dtbs_check`` will skip any binding schema files with errors. It is176necessary to use ``dt_binding_check`` to get all the validation errors in the177binding schema files.178 179It is possible to run both in a single command::180 181 make dt_binding_check dtbs_check182 183It is also possible to run checks with a subset of matching schema files by184setting the ``DT_SCHEMA_FILES`` variable to 1 or more specific schema files or185patterns (partial match of a fixed string). Each file or pattern should be186separated by ':'.187 188::189 190 make dt_binding_check DT_SCHEMA_FILES=trivial-devices.yaml191 make dt_binding_check DT_SCHEMA_FILES=trivial-devices.yaml:rtc.yaml192 make dt_binding_check DT_SCHEMA_FILES=/gpio/193 make dtbs_check DT_SCHEMA_FILES=trivial-devices.yaml194 195 196json-schema Resources197---------------------198 199 200`JSON-Schema Specifications <http://json-schema.org/>`_201 202`Using JSON Schema Book <http://usingjsonschema.com/>`_203 204.. _example-schema:205 206Annotated Example Schema207------------------------208 209Also available as a separate file: :download:`example-schema.yaml`210 211.. literalinclude:: example-schema.yaml212