writing-schema.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. The YAML format has several options for defining the formatting of the text
  36. block. The options are controlled with indicator characters following the key
  37. (e.g. "description: \|"). The minimum formatting needed for a block should be
  38. used. The formatting controls can not only affect whether the YAML can be
  39. parsed correctly, but are important when the text blocks are rendered to
  40. another form. The options are as follows.
  41. The default without any indicators is flowed, plain scalar style where single
  42. line breaks and leading whitespace are stripped. Paragraphs are delimited by
  43. blank lines (i.e. double line break). This style cannot contain ": " in it as
  44. it will be interpreted as a key. Any " #" sequence will be interpreted as
  45. a comment. There's other restrictions on characters as well. Most
  46. restrictions are on what the first character can be.
  47. The second style is folded which is indicated by ">" character. In addition
  48. to maintaining line breaks on double line breaks, the folded style also
  49. maintains leading whitespace beyond indentation of the first line. The line
  50. breaks on indented lines are also maintained.
  51. The third style is literal which is indicated by "\|" character. The literal
  52. style maintains all line breaks and whitespace (beyond indentation of the
  53. first line).
  54. The above is not a complete description of YAML text blocks. More details on
  55. multi-line YAML text blocks can be found online:
  56. https://yaml-multiline.info/
  57. https://www.yaml.info/learn/quote.html
  58. select
  59. Optional. A json-schema used to match nodes for applying the
  60. schema. By default, without 'select', nodes are matched against their possible
  61. compatible-string values or node name. Most bindings should not need select.
  62. allOf
  63. Optional. A list of other schemas to include. This is used to
  64. include other schemas the binding conforms to. This may be schemas for a
  65. particular class of devices such as I2C or SPI controllers.
  66. properties
  67. A set of sub-schema defining all the DT properties for the
  68. binding. The exact schema syntax depends on whether properties are known,
  69. common properties (e.g. 'interrupts') or are binding/vendor-specific
  70. properties.
  71. A property can also define a child DT node with child properties defined
  72. under it.
  73. For more details on properties sections, see 'Property Schema' section.
  74. patternProperties
  75. Optional. Similar to 'properties', but names are regex.
  76. required
  77. A list of DT properties from the 'properties' section that
  78. must always be present.
  79. additionalProperties / unevaluatedProperties
  80. Keywords controlling how schema will validate properties not matched by this
  81. schema's 'properties' or 'patternProperties'. Each schema is supposed to
  82. have exactly one of these keywords in top-level part, so either
  83. additionalProperties or unevaluatedProperties. Nested nodes, so properties
  84. being objects, are supposed to have one as well.
  85. * additionalProperties: false
  86. Most common case, where no additional schema is referenced or if this
  87. binding allows subset of properties from other referenced schemas.
  88. * unevaluatedProperties: false
  89. Used when this binding references other schema whose all properties
  90. should be allowed.
  91. * additionalProperties: true
  92. - Top-level part:
  93. Rare case, used for schemas implementing common set of properties. Such
  94. schemas are supposed to be referenced by other schemas, which then use
  95. 'unevaluatedProperties: false'. Typically bus or common-part schemas.
  96. - Nested node:
  97. When listing only the expected compatible of the nested node and there
  98. is an another schema matching that compatible which ends with one of
  99. two above cases ('false').
  100. examples
  101. Optional. A list of one or more DTS hunks implementing this binding only.
  102. Example should not contain unrelated device nodes, e.g. consumer nodes in a
  103. provider binding, other nodes referenced by phandle.
  104. Note: YAML doesn't allow leading tabs, so spaces must be used instead.
  105. Unless noted otherwise, all properties are required.
  106. Property Schema
  107. ---------------
  108. The 'properties' section of the schema contains all the DT properties for a
  109. binding. Each property contains a set of constraints using json-schema
  110. vocabulary for that property. The properties schemas are what are used for
  111. validation of DT files.
  112. For common properties, only additional constraints not covered by the common,
  113. binding schema need to be defined such as how many values are valid or what
  114. possible values are valid.
  115. Vendor-specific properties will typically need more detailed schema. With the
  116. exception of boolean properties, they should have a reference to a type in
  117. schemas/types.yaml. A "description" property is always required.
  118. The Devicetree schemas don't exactly match the YAML-encoded DT data produced by
  119. dtc. They are simplified to make them more compact and avoid a bunch of
  120. boilerplate. The tools process the schema files to produce the final schema for
  121. validation. There are currently 2 transformations the tools perform.
  122. The default for arrays in json-schema is they are variable-sized and allow more
  123. entries than explicitly defined. This can be restricted by defining 'minItems',
  124. 'maxItems', and 'additionalItems'. However, for DeviceTree Schemas, a fixed
  125. size is desired in most cases, so these properties are added based on the
  126. number of entries in an 'items' list.
  127. The YAML Devicetree format also makes all string values an array and scalar
  128. values a matrix (in order to define groupings) even when only a single value
  129. is present. Single entries in schemas are fixed up to match this encoding.
  130. When bindings cover multiple similar devices that differ in some properties,
  131. those properties should be constrained for each device. This usually means:
  132. * In top level 'properties' define the property with the broadest constraints.
  133. * In 'if:then:' blocks, further narrow the constraints for those properties.
  134. * Do not define the properties within an 'if:then:' block (note that
  135. 'additionalItems' also won't allow that).
  136. Coding style
  137. ------------
  138. Use YAML coding style (two-space indentation). For DTS examples in the schema,
  139. preferred is four-space indentation.
  140. Place entries in 'properties' and 'required' sections in the same order, using
  141. style from Documentation/devicetree/bindings/dts-coding-style.rst.
  142. Testing
  143. -------
  144. Dependencies
  145. ~~~~~~~~~~~~
  146. The DT schema project must be installed in order to validate the DT schema
  147. binding documents and validate DTS files using the DT schema. The DT schema
  148. project can be installed with pip::
  149. pip3 install dtschema
  150. Note that 'dtschema' installation requires 'swig' and Python development files
  151. installed first. On Debian/Ubuntu systems::
  152. apt install swig python3-dev
  153. Several executables (dt-doc-validate, dt-mk-schema, dt-validate) will be
  154. installed. Ensure they are in your PATH (~/.local/bin by default).
  155. Recommended is also to install yamllint (used by dtschema when present).
  156. Running checks
  157. ~~~~~~~~~~~~~~
  158. The DT schema binding documents must be validated using the meta-schema (the
  159. schema for the schema) to ensure they are both valid json-schema and valid
  160. binding schema. All of the DT binding documents can be validated using the
  161. ``dt_binding_check`` target::
  162. make dt_binding_check
  163. Or to validate a single schema and its example::
  164. make sram/sram.yaml
  165. In order to perform validation of DT source files, use the ``dtbs_check`` target::
  166. make dtbs_check
  167. Note that ``dtbs_check`` will skip any binding schema files with errors. It is
  168. necessary to use ``dt_binding_check`` to get all the validation errors in the
  169. binding schema files.
  170. It is possible to run both in a single command::
  171. make dt_binding_check dtbs_check
  172. It is also possible to combine running the above commands with a subset of
  173. matching schema files by setting the ``DT_SCHEMA_FILES`` variable to 1 or more
  174. specific schema files or patterns (partial match of a fixed string). Each file
  175. or pattern should be separated by ':'.
  176. ::
  177. make dt_binding_check DT_SCHEMA_FILES=trivial-devices.yaml
  178. make dt_binding_check DT_SCHEMA_FILES=trivial-devices.yaml:rtc.yaml
  179. make dt_binding_check DT_SCHEMA_FILES=/gpio/
  180. make dtbs_check DT_SCHEMA_FILES=trivial-devices.yaml
  181. json-schema Resources
  182. ---------------------
  183. `JSON-Schema Specifications <http://json-schema.org/>`_
  184. `Using JSON Schema Book <http://usingjsonschema.com/>`_
  185. .. _example-schema:
  186. Annotated Example Schema
  187. ------------------------
  188. Also available as a separate file: :download:`example-schema.yaml`
  189. .. literalinclude:: example-schema.yaml