18d13bc63SEmmanuel Vadot.. SPDX-License-Identifier: GPL-2.0 28d13bc63SEmmanuel Vadot 38d13bc63SEmmanuel Vadot===================================== 48d13bc63SEmmanuel VadotDevicetree Sources (DTS) Coding Style 58d13bc63SEmmanuel Vadot===================================== 68d13bc63SEmmanuel Vadot 78d13bc63SEmmanuel VadotWhen writing Devicetree Sources (DTS) please observe below guidelines. They 88d13bc63SEmmanuel Vadotshould be considered complementary to any rules expressed already in 98d13bc63SEmmanuel Vadotthe Devicetree Specification and the dtc compiler (including W=1 and W=2 108d13bc63SEmmanuel Vadotbuilds). 118d13bc63SEmmanuel Vadot 128d13bc63SEmmanuel VadotIndividual architectures and subarchitectures can define additional rules, 138d13bc63SEmmanuel Vadotmaking the coding style stricter. 148d13bc63SEmmanuel Vadot 158d13bc63SEmmanuel VadotNaming and Valid Characters 168d13bc63SEmmanuel Vadot--------------------------- 178d13bc63SEmmanuel Vadot 188d13bc63SEmmanuel VadotThe Devicetree Specification allows a broad range of characters in node 198d13bc63SEmmanuel Vadotand property names, but this coding style narrows the range down to achieve 208d13bc63SEmmanuel Vadotbetter code readability. 218d13bc63SEmmanuel Vadot 228d13bc63SEmmanuel Vadot1. Node and property names can use only the following characters: 238d13bc63SEmmanuel Vadot 248d13bc63SEmmanuel Vadot * Lowercase characters: [a-z] 258d13bc63SEmmanuel Vadot * Digits: [0-9] 268d13bc63SEmmanuel Vadot * Dash: - 278d13bc63SEmmanuel Vadot 288d13bc63SEmmanuel Vadot2. Labels can use only the following characters: 298d13bc63SEmmanuel Vadot 308d13bc63SEmmanuel Vadot * Lowercase characters: [a-z] 318d13bc63SEmmanuel Vadot * Digits: [0-9] 328d13bc63SEmmanuel Vadot * Underscore: _ 338d13bc63SEmmanuel Vadot 348d13bc63SEmmanuel Vadot3. Unless a bus defines differently, unit addresses shall use lowercase 358d13bc63SEmmanuel Vadot hexadecimal digits, without leading zeros (padding). 368d13bc63SEmmanuel Vadot 378d13bc63SEmmanuel Vadot4. Hex values in properties, e.g. "reg", shall use lowercase hex. The address 388d13bc63SEmmanuel Vadot part can be padded with leading zeros. 398d13bc63SEmmanuel Vadot 408d13bc63SEmmanuel VadotExample:: 418d13bc63SEmmanuel Vadot 428d13bc63SEmmanuel Vadot gpi_dma2: dma-controller@a00000 { 438d13bc63SEmmanuel Vadot compatible = "qcom,sm8550-gpi-dma", "qcom,sm6350-gpi-dma"; 448d13bc63SEmmanuel Vadot reg = <0x0 0x00a00000 0x0 0x60000>; 458d13bc63SEmmanuel Vadot } 468d13bc63SEmmanuel Vadot 478d13bc63SEmmanuel VadotOrder of Nodes 488d13bc63SEmmanuel Vadot-------------- 498d13bc63SEmmanuel Vadot 508d13bc63SEmmanuel Vadot1. Nodes on any bus, thus using unit addresses for children, shall be 518d13bc63SEmmanuel Vadot ordered by unit address in ascending order. 528d13bc63SEmmanuel Vadot Alternatively for some subarchitectures, nodes of the same type can be 538d13bc63SEmmanuel Vadot grouped together, e.g. all I2C controllers one after another even if this 548d13bc63SEmmanuel Vadot breaks unit address ordering. 558d13bc63SEmmanuel Vadot 568d13bc63SEmmanuel Vadot2. Nodes without unit addresses shall be ordered alpha-numerically by the node 578d13bc63SEmmanuel Vadot name. For a few node types, they can be ordered by the main property, e.g. 588d13bc63SEmmanuel Vadot pin configuration states ordered by value of "pins" property. 598d13bc63SEmmanuel Vadot 608d13bc63SEmmanuel Vadot3. When extending nodes in the board DTS via &label, the entries shall be 618d13bc63SEmmanuel Vadot ordered either alpha-numerically or by keeping the order from DTSI, where 628d13bc63SEmmanuel Vadot the choice depends on the subarchitecture. 638d13bc63SEmmanuel Vadot 648d13bc63SEmmanuel VadotThe above-described ordering rules are easy to enforce during review, reduce 658d13bc63SEmmanuel Vadotchances of conflicts for simultaneous additions of new nodes to a file and help 668d13bc63SEmmanuel Vadotin navigating through the DTS source. 678d13bc63SEmmanuel Vadot 688d13bc63SEmmanuel VadotExample:: 698d13bc63SEmmanuel Vadot 708d13bc63SEmmanuel Vadot /* SoC DTSI */ 718d13bc63SEmmanuel Vadot 728d13bc63SEmmanuel Vadot / { 738d13bc63SEmmanuel Vadot cpus { 748d13bc63SEmmanuel Vadot /* ... */ 758d13bc63SEmmanuel Vadot }; 768d13bc63SEmmanuel Vadot 778d13bc63SEmmanuel Vadot psci { 788d13bc63SEmmanuel Vadot /* ... */ 798d13bc63SEmmanuel Vadot }; 808d13bc63SEmmanuel Vadot 818d13bc63SEmmanuel Vadot soc@0 { 828d13bc63SEmmanuel Vadot dma: dma-controller@10000 { 838d13bc63SEmmanuel Vadot /* ... */ 848d13bc63SEmmanuel Vadot }; 858d13bc63SEmmanuel Vadot 868d13bc63SEmmanuel Vadot clk: clock-controller@80000 { 878d13bc63SEmmanuel Vadot /* ... */ 888d13bc63SEmmanuel Vadot }; 898d13bc63SEmmanuel Vadot }; 908d13bc63SEmmanuel Vadot }; 918d13bc63SEmmanuel Vadot 928d13bc63SEmmanuel Vadot /* Board DTS - alphabetical order */ 938d13bc63SEmmanuel Vadot 948d13bc63SEmmanuel Vadot &clk { 958d13bc63SEmmanuel Vadot /* ... */ 968d13bc63SEmmanuel Vadot }; 978d13bc63SEmmanuel Vadot 988d13bc63SEmmanuel Vadot &dma { 998d13bc63SEmmanuel Vadot /* ... */ 1008d13bc63SEmmanuel Vadot }; 1018d13bc63SEmmanuel Vadot 1028d13bc63SEmmanuel Vadot /* Board DTS - alternative order, keep as DTSI */ 1038d13bc63SEmmanuel Vadot 1048d13bc63SEmmanuel Vadot &dma { 1058d13bc63SEmmanuel Vadot /* ... */ 1068d13bc63SEmmanuel Vadot }; 1078d13bc63SEmmanuel Vadot 1088d13bc63SEmmanuel Vadot &clk { 1098d13bc63SEmmanuel Vadot /* ... */ 1108d13bc63SEmmanuel Vadot }; 1118d13bc63SEmmanuel Vadot 1128d13bc63SEmmanuel VadotOrder of Properties in Device Node 1138d13bc63SEmmanuel Vadot---------------------------------- 1148d13bc63SEmmanuel Vadot 1158d13bc63SEmmanuel VadotThe following order of properties in device nodes is preferred: 1168d13bc63SEmmanuel Vadot 1178d13bc63SEmmanuel Vadot1. "compatible" 1188d13bc63SEmmanuel Vadot2. "reg" 1198d13bc63SEmmanuel Vadot3. "ranges" 1208d13bc63SEmmanuel Vadot4. Standard/common properties (defined by common bindings, e.g. without 1218d13bc63SEmmanuel Vadot vendor-prefixes) 1228d13bc63SEmmanuel Vadot5. Vendor-specific properties 1238d13bc63SEmmanuel Vadot6. "status" (if applicable) 1248d13bc63SEmmanuel Vadot7. Child nodes, where each node is preceded with a blank line 1258d13bc63SEmmanuel Vadot 1268d13bc63SEmmanuel VadotThe "status" property is by default "okay", thus it can be omitted. 1278d13bc63SEmmanuel Vadot 1288d13bc63SEmmanuel VadotThe above-described ordering follows this approach: 1298d13bc63SEmmanuel Vadot 1308d13bc63SEmmanuel Vadot1. Most important properties start the node: compatible then bus addressing to 1318d13bc63SEmmanuel Vadot match unit address. 1328d13bc63SEmmanuel Vadot2. Each node will have common properties in similar place. 1338d13bc63SEmmanuel Vadot3. Status is the last information to annotate that device node is or is not 1348d13bc63SEmmanuel Vadot finished (board resources are needed). 1358d13bc63SEmmanuel Vadot 1368d13bc63SEmmanuel VadotExample:: 1378d13bc63SEmmanuel Vadot 1388d13bc63SEmmanuel Vadot /* SoC DTSI */ 1398d13bc63SEmmanuel Vadot 1408d13bc63SEmmanuel Vadot device_node: device-class@6789abc { 1418d13bc63SEmmanuel Vadot compatible = "vendor,device"; 1428d13bc63SEmmanuel Vadot reg = <0x0 0x06789abc 0x0 0xa123>; 1438d13bc63SEmmanuel Vadot ranges = <0x0 0x0 0x06789abc 0x1000>; 1448d13bc63SEmmanuel Vadot #dma-cells = <1>; 1458d13bc63SEmmanuel Vadot clocks = <&clock_controller 0>, <&clock_controller 1>; 1468d13bc63SEmmanuel Vadot clock-names = "bus", "host"; 147*01950c46SEmmanuel Vadot #address-cells = <1>; 148*01950c46SEmmanuel Vadot #size-cells = <1>; 1498d13bc63SEmmanuel Vadot vendor,custom-property = <2>; 1508d13bc63SEmmanuel Vadot status = "disabled"; 1518d13bc63SEmmanuel Vadot 1528d13bc63SEmmanuel Vadot child_node: child-class@100 { 1538d13bc63SEmmanuel Vadot reg = <0x100 0x200>; 1548d13bc63SEmmanuel Vadot /* ... */ 1558d13bc63SEmmanuel Vadot }; 1568d13bc63SEmmanuel Vadot }; 1578d13bc63SEmmanuel Vadot 1588d13bc63SEmmanuel Vadot /* Board DTS */ 1598d13bc63SEmmanuel Vadot 1608d13bc63SEmmanuel Vadot &device_node { 1618d13bc63SEmmanuel Vadot vdd-supply = <&board_vreg1>; 1628d13bc63SEmmanuel Vadot status = "okay"; 1638d13bc63SEmmanuel Vadot } 1648d13bc63SEmmanuel Vadot 1658d13bc63SEmmanuel VadotIndentation 1668d13bc63SEmmanuel Vadot----------- 1678d13bc63SEmmanuel Vadot 1688d13bc63SEmmanuel Vadot1. Use indentation according to Documentation/process/coding-style.rst. 1698d13bc63SEmmanuel Vadot2. Each entry in arrays with multiple cells, e.g. "reg" with two IO addresses, 1708d13bc63SEmmanuel Vadot shall be enclosed in <>. 1718d13bc63SEmmanuel Vadot3. For arrays spanning across lines, it is preferred to align the continued 1728d13bc63SEmmanuel Vadot entries with opening < from the first line. 1738d13bc63SEmmanuel Vadot 1748d13bc63SEmmanuel VadotExample:: 1758d13bc63SEmmanuel Vadot 1768d13bc63SEmmanuel Vadot thermal-sensor@c271000 { 1778d13bc63SEmmanuel Vadot compatible = "qcom,sm8550-tsens", "qcom,tsens-v2"; 1788d13bc63SEmmanuel Vadot reg = <0x0 0x0c271000 0x0 0x1000>, 1798d13bc63SEmmanuel Vadot <0x0 0x0c222000 0x0 0x1000>; 1808d13bc63SEmmanuel Vadot }; 1818d13bc63SEmmanuel Vadot 1828d13bc63SEmmanuel VadotOrganizing DTSI and DTS 1838d13bc63SEmmanuel Vadot----------------------- 1848d13bc63SEmmanuel Vadot 1858d13bc63SEmmanuel VadotThe DTSI and DTS files shall be organized in a way representing the common, 1868d13bc63SEmmanuel Vadotreusable parts of hardware. Typically, this means organizing DTSI and DTS files 1878d13bc63SEmmanuel Vadotinto several files: 1888d13bc63SEmmanuel Vadot 1898d13bc63SEmmanuel Vadot1. DTSI with contents of the entire SoC, without nodes for hardware not present 1908d13bc63SEmmanuel Vadot on the SoC. 1918d13bc63SEmmanuel Vadot2. If applicable: DTSI with common or re-usable parts of the hardware, e.g. 1928d13bc63SEmmanuel Vadot entire System-on-Module. 1938d13bc63SEmmanuel Vadot3. DTS representing the board. 1948d13bc63SEmmanuel Vadot 1958d13bc63SEmmanuel VadotHardware components that are present on the board shall be placed in the 1968d13bc63SEmmanuel Vadotboard DTS, not in the SoC or SoM DTSI. A partial exception is a common 1978d13bc63SEmmanuel Vadotexternal reference SoC input clock, which could be coded as a fixed-clock in 1988d13bc63SEmmanuel Vadotthe SoC DTSI with its frequency provided by each board DTS. 199