writing-schema.rst 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. .. SPDX-License-Identifier: GPL-2.0
  2. Writing Devicetree Bindings in json-schema
  3. ==========================================
  4. Devicetree bindings are written using json-schema vocabulary. Schema files are
  5. written in a JSON-compatible subset of YAML. YAML is used instead of JSON as it
  6. is considered more human readable and has some advantages such as allowing
  7. comments (Prefixed with '#').
  8. Also see :ref:`example-schema`.
  9. Schema Contents
  10. ---------------
  11. Each schema doc is a structured json-schema which is defined by a set of
  12. top-level properties. Generally, there is one binding defined per file. The
  13. top-level json-schema properties used are:
  14. $id
  15. A json-schema unique identifier string. The string must be a valid
  16. URI typically containing the binding's filename and path. For DT schema, it must
  17. begin with "http://devicetree.org/schemas/". The URL is used in constructing
  18. references to other files specified in schema "$ref" properties. A $ref value
  19. with a leading '/' will have the hostname prepended. A $ref value with only a
  20. relative path or filename will be prepended with the hostname and path
  21. components of the current schema file's '$id' value. A URL is used even for
  22. local files, but there may not actually be files present at those locations.
  23. $schema
  24. Indicates the meta-schema the schema file adheres to.
  25. title
  26. A one-line description of the hardware being described in the binding schema.
  27. maintainers
  28. A DT specific property. Contains a list of email address(es)
  29. for maintainers of this binding.
  30. description
  31. Optional. A multi-line text block containing any detailed
  32. information about this hardware. It should contain things such as what the block
  33. or device does, standards the device conforms to, and links to datasheets for
  34. more information.
  35. select
  36. Optional. A json-schema used to match nodes for applying the
  37. schema. By default, without 'select', nodes are matched against their possible
  38. compatible-string values or node name. Most bindings should not need select.
  39. allOf
  40. Optional. A list of other schemas to include. This is used to
  41. include other schemas the binding conforms to. This may be schemas for a
  42. particular class of devices such as I2C or SPI controllers.
  43. properties
  44. A set of sub-schema defining all the DT properties for the
  45. binding. The exact schema syntax depends on whether properties are known,
  46. common properties (e.g. 'interrupts') or are binding/vendor-specific
  47. properties.
  48. A property can also define a child DT node with child properties defined
  49. under it.
  50. For more details on properties sections, see 'Property Schema' section.
  51. patternProperties
  52. Optional. Similar to 'properties', but names are regex.
  53. required
  54. A list of DT properties from the 'properties' section that
  55. must always be present.
  56. additionalProperties / unevaluatedProperties
  57. Keywords controlling how schema will validate properties not matched by this
  58. schema's 'properties' or 'patternProperties'. Each schema is supposed to
  59. have exactly one of these keywords in top-level part, so either
  60. additionalProperties or unevaluatedProperties. Nested nodes, so properties
  61. being objects, are supposed to have one as well.
  62. * additionalProperties: false
  63. Most common case, where no additional schema is referenced or if this
  64. binding allows subset of properties from other referenced schemas.
  65. * unevaluatedProperties: false
  66. Used when this binding references other schema whose all properties
  67. should be allowed.
  68. * additionalProperties: true
  69. Rare case, used for schemas implementing common set of properties. Such
  70. schemas are supposed to be referenced by other schemas, which then use
  71. 'unevaluatedProperties: false'. Typically bus or common-part schemas.
  72. examples
  73. Optional. A list of one or more DTS hunks implementing this binding only.
  74. Example should not contain unrelated device nodes, e.g. consumer nodes in a
  75. provider binding, other nodes referenced by phandle.
  76. Note: YAML doesn't allow leading tabs, so spaces must be used instead.
  77. Unless noted otherwise, all properties are required.
  78. Property Schema
  79. ---------------
  80. The 'properties' section of the schema contains all the DT properties for a
  81. binding. Each property contains a set of constraints using json-schema
  82. vocabulary for that property. The properties schemas are what are used for
  83. validation of DT files.
  84. For common properties, only additional constraints not covered by the common,
  85. binding schema need to be defined such as how many values are valid or what
  86. possible values are valid.
  87. Vendor-specific properties will typically need more detailed schema. With the
  88. exception of boolean properties, they should have a reference to a type in
  89. schemas/types.yaml. A "description" property is always required.
  90. The Devicetree schemas don't exactly match the YAML-encoded DT data produced by
  91. dtc. They are simplified to make them more compact and avoid a bunch of
  92. boilerplate. The tools process the schema files to produce the final schema for
  93. validation. There are currently 2 transformations the tools perform.
  94. The default for arrays in json-schema is they are variable-sized and allow more
  95. entries than explicitly defined. This can be restricted by defining 'minItems',
  96. 'maxItems', and 'additionalItems'. However, for DeviceTree Schemas, a fixed
  97. size is desired in most cases, so these properties are added based on the
  98. number of entries in an 'items' list.
  99. The YAML Devicetree format also makes all string values an array and scalar
  100. values a matrix (in order to define groupings) even when only a single value
  101. is present. Single entries in schemas are fixed up to match this encoding.
  102. Coding style
  103. ------------
  104. Use YAML coding style (two-space indentation). For DTS examples in the schema,
  105. preferred is four-space indentation.
  106. Testing
  107. -------
  108. Dependencies
  109. ~~~~~~~~~~~~
  110. The DT schema project must be installed in order to validate the DT schema
  111. binding documents and validate DTS files using the DT schema. The DT schema
  112. project can be installed with pip::
  113. pip3 install dtschema
  114. Note that 'dtschema' installation requires 'swig' and Python development files
  115. installed first. On Debian/Ubuntu systems::
  116. apt install swig python3-dev
  117. Several executables (dt-doc-validate, dt-mk-schema, dt-validate) will be
  118. installed. Ensure they are in your PATH (~/.local/bin by default).
  119. Recommended is also to install yamllint (used by dtschema when present).
  120. Running checks
  121. ~~~~~~~~~~~~~~
  122. The DT schema binding documents must be validated using the meta-schema (the
  123. schema for the schema) to ensure they are both valid json-schema and valid
  124. binding schema. All of the DT binding documents can be validated using the
  125. ``dt_binding_check`` target::
  126. make dt_binding_check
  127. In order to perform validation of DT source files, use the ``dtbs_check`` target::
  128. make dtbs_check
  129. Note that ``dtbs_check`` will skip any binding schema files with errors. It is
  130. necessary to use ``dt_binding_check`` to get all the validation errors in the
  131. binding schema files.
  132. It is possible to run both in a single command::
  133. make dt_binding_check dtbs_check
  134. It is also possible to run checks with a subset of matching schema files by
  135. setting the ``DT_SCHEMA_FILES`` variable to 1 or more specific schema files or
  136. patterns (partial match of a fixed string). Each file or pattern should be
  137. separated by ':'.
  138. ::
  139. make dt_binding_check DT_SCHEMA_FILES=trivial-devices.yaml
  140. make dt_binding_check DT_SCHEMA_FILES=trivial-devices.yaml:rtc.yaml
  141. make dt_binding_check DT_SCHEMA_FILES=/gpio/
  142. make dtbs_check DT_SCHEMA_FILES=trivial-devices.yaml
  143. json-schema Resources
  144. ---------------------
  145. `JSON-Schema Specifications <http://json-schema.org/>`_
  146. `Using JSON Schema Book <http://usingjsonschema.com/>`_
  147. .. _example-schema:
  148. Annotated Example Schema
  149. ------------------------
  150. Also available as a separate file: :download:`example-schema.yaml`
  151. .. literalinclude:: example-schema.yaml