18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci 38c2ecf20Sopenharmony_ciWriting DeviceTree Bindings in json-schema 48c2ecf20Sopenharmony_ci========================================== 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ciDevicetree bindings are written using json-schema vocabulary. Schema files are 78c2ecf20Sopenharmony_ciwritten in a JSON compatible subset of YAML. YAML is used instead of JSON as it 88c2ecf20Sopenharmony_ciis considered more human readable and has some advantages such as allowing 98c2ecf20Sopenharmony_cicomments (Prefixed with '#'). 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ciSchema Contents 128c2ecf20Sopenharmony_ci--------------- 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ciEach schema doc is a structured json-schema which is defined by a set of 158c2ecf20Sopenharmony_citop-level properties. Generally, there is one binding defined per file. The 168c2ecf20Sopenharmony_citop-level json-schema properties used are: 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci$id 198c2ecf20Sopenharmony_ci A json-schema unique identifier string. The string must be a valid 208c2ecf20Sopenharmony_ci URI typically containing the binding's filename and path. For DT schema, it must 218c2ecf20Sopenharmony_ci begin with "http://devicetree.org/schemas/". The URL is used in constructing 228c2ecf20Sopenharmony_ci references to other files specified in schema "$ref" properties. A $ref value 238c2ecf20Sopenharmony_ci with a leading '/' will have the hostname prepended. A $ref value a relative 248c2ecf20Sopenharmony_ci path or filename only will be prepended with the hostname and path components 258c2ecf20Sopenharmony_ci of the current schema file's '$id' value. A URL is used even for local files, 268c2ecf20Sopenharmony_ci but there may not actually be files present at those locations. 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci$schema 298c2ecf20Sopenharmony_ci Indicates the meta-schema the schema file adheres to. 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_cititle 328c2ecf20Sopenharmony_ci A one line description on the contents of the binding schema. 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cimaintainers 358c2ecf20Sopenharmony_ci A DT specific property. Contains a list of email address(es) 368c2ecf20Sopenharmony_ci for maintainers of this binding. 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_cidescription 398c2ecf20Sopenharmony_ci Optional. A multi-line text block containing any detailed 408c2ecf20Sopenharmony_ci information about this binding. It should contain things such as what the block 418c2ecf20Sopenharmony_ci or device does, standards the device conforms to, and links to datasheets for 428c2ecf20Sopenharmony_ci more information. 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ciselect 458c2ecf20Sopenharmony_ci Optional. A json-schema used to match nodes for applying the 468c2ecf20Sopenharmony_ci schema. By default without 'select', nodes are matched against their possible 478c2ecf20Sopenharmony_ci compatible string values or node name. Most bindings should not need select. 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci allOf 508c2ecf20Sopenharmony_ci Optional. A list of other schemas to include. This is used to 518c2ecf20Sopenharmony_ci include other schemas the binding conforms to. This may be schemas for a 528c2ecf20Sopenharmony_ci particular class of devices such as I2C or SPI controllers. 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci properties 558c2ecf20Sopenharmony_ci A set of sub-schema defining all the DT properties for the 568c2ecf20Sopenharmony_ci binding. The exact schema syntax depends on whether properties are known, 578c2ecf20Sopenharmony_ci common properties (e.g. 'interrupts') or are binding/vendor specific properties. 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ciA property can also define a child DT node with child properties defined 608c2ecf20Sopenharmony_ciunder it. 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ciFor more details on properties sections, see 'Property Schema' section. 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_cipatternProperties 658c2ecf20Sopenharmony_ci Optional. Similar to 'properties', but names are regex. 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_cirequired 688c2ecf20Sopenharmony_ci A list of DT properties from the 'properties' section that 698c2ecf20Sopenharmony_ci must always be present. 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ciexamples 728c2ecf20Sopenharmony_ci Optional. A list of one or more DTS hunks implementing the 738c2ecf20Sopenharmony_ci binding. Note: YAML doesn't allow leading tabs, so spaces must be used instead. 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ciUnless noted otherwise, all properties are required. 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ciProperty Schema 788c2ecf20Sopenharmony_ci--------------- 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ciThe 'properties' section of the schema contains all the DT properties for a 818c2ecf20Sopenharmony_cibinding. Each property contains a set of constraints using json-schema 828c2ecf20Sopenharmony_civocabulary for that property. The properties schemas are what is used for 838c2ecf20Sopenharmony_civalidation of DT files. 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ciFor common properties, only additional constraints not covered by the common 868c2ecf20Sopenharmony_cibinding schema need to be defined such as how many values are valid or what 878c2ecf20Sopenharmony_cipossible values are valid. 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ciVendor specific properties will typically need more detailed schema. With the 908c2ecf20Sopenharmony_ciexception of boolean properties, they should have a reference to a type in 918c2ecf20Sopenharmony_cischemas/types.yaml. A "description" property is always required. 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ciThe Devicetree schemas don't exactly match the YAML encoded DT data produced by 948c2ecf20Sopenharmony_cidtc. They are simplified to make them more compact and avoid a bunch of 958c2ecf20Sopenharmony_ciboilerplate. The tools process the schema files to produce the final schema for 968c2ecf20Sopenharmony_civalidation. There are currently 2 transformations the tools perform. 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ciThe default for arrays in json-schema is they are variable sized and allow more 998c2ecf20Sopenharmony_cientries than explicitly defined. This can be restricted by defining 'minItems', 1008c2ecf20Sopenharmony_ci'maxItems', and 'additionalItems'. However, for DeviceTree Schemas, a fixed 1018c2ecf20Sopenharmony_cisize is desired in most cases, so these properties are added based on the 1028c2ecf20Sopenharmony_cinumber of entries in an 'items' list. 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ciThe YAML Devicetree format also makes all string values an array and scalar 1058c2ecf20Sopenharmony_civalues a matrix (in order to define groupings) even when only a single value 1068c2ecf20Sopenharmony_ciis present. Single entries in schemas are fixed up to match this encoding. 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ciTesting 1098c2ecf20Sopenharmony_ci------- 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ciDependencies 1128c2ecf20Sopenharmony_ci~~~~~~~~~~~~ 1138c2ecf20Sopenharmony_ci 1148c2ecf20Sopenharmony_ciThe DT schema project must be installed in order to validate the DT schema 1158c2ecf20Sopenharmony_cibinding documents and validate DTS files using the DT schema. The DT schema 1168c2ecf20Sopenharmony_ciproject can be installed with pip:: 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci pip3 install git+https://github.com/devicetree-org/dt-schema.git@master 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ciSeveral executables (dt-doc-validate, dt-mk-schema, dt-validate) will be 1218c2ecf20Sopenharmony_ciinstalled. Ensure they are in your PATH (~/.local/bin by default). 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_cidtc must also be built with YAML output support enabled. This requires that 1248c2ecf20Sopenharmony_cilibyaml and its headers be installed on the host system. For some distributions 1258c2ecf20Sopenharmony_cithat involves installing the development package, such as: 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ciDebian:: 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci apt-get install libyaml-dev 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ciFedora:: 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci dnf -y install libyaml-devel 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ciRunning checks 1368c2ecf20Sopenharmony_ci~~~~~~~~~~~~~~ 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ciThe DT schema binding documents must be validated using the meta-schema (the 1398c2ecf20Sopenharmony_cischema for the schema) to ensure they are both valid json-schema and valid 1408c2ecf20Sopenharmony_cibinding schema. All of the DT binding documents can be validated using the 1418c2ecf20Sopenharmony_ci``dt_binding_check`` target:: 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci make dt_binding_check 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ciIn order to perform validation of DT source files, use the ``dtbs_check`` target:: 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci make dtbs_check 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ciNote that ``dtbs_check`` will skip any binding schema files with errors. It is 1508c2ecf20Sopenharmony_cinecessary to use ``dt_binding_check`` to get all the validation errors in the 1518c2ecf20Sopenharmony_cibinding schema files. 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ciIt is possible to run both in a single command:: 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci make dt_binding_check dtbs_check 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ciIt is also possible to run checks with a single schema file by setting the 1588c2ecf20Sopenharmony_ci``DT_SCHEMA_FILES`` variable to a specific schema file. 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci:: 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci make dt_binding_check DT_SCHEMA_FILES=Documentation/devicetree/bindings/trivial-devices.yaml 1638c2ecf20Sopenharmony_ci make dtbs_check DT_SCHEMA_FILES=Documentation/devicetree/bindings/trivial-devices.yaml 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_cijson-schema Resources 1678c2ecf20Sopenharmony_ci--------------------- 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci`JSON-Schema Specifications <http://json-schema.org/>`_ 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci`Using JSON Schema Book <http://usingjsonschema.com/>`_ 173