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