1*8d13bc63SEmmanuel Vadot.. SPDX-License-Identifier: GPL-2.0 2*8d13bc63SEmmanuel Vadot 3*8d13bc63SEmmanuel Vadot===================================== 4*8d13bc63SEmmanuel VadotDevicetree Sources (DTS) Coding Style 5*8d13bc63SEmmanuel Vadot===================================== 6*8d13bc63SEmmanuel Vadot 7*8d13bc63SEmmanuel VadotWhen writing Devicetree Sources (DTS) please observe below guidelines. They 8*8d13bc63SEmmanuel Vadotshould be considered complementary to any rules expressed already in 9*8d13bc63SEmmanuel Vadotthe Devicetree Specification and the dtc compiler (including W=1 and W=2 10*8d13bc63SEmmanuel Vadotbuilds). 11*8d13bc63SEmmanuel Vadot 12*8d13bc63SEmmanuel VadotIndividual architectures and subarchitectures can define additional rules, 13*8d13bc63SEmmanuel Vadotmaking the coding style stricter. 14*8d13bc63SEmmanuel Vadot 15*8d13bc63SEmmanuel VadotNaming and Valid Characters 16*8d13bc63SEmmanuel Vadot--------------------------- 17*8d13bc63SEmmanuel Vadot 18*8d13bc63SEmmanuel VadotThe Devicetree Specification allows a broad range of characters in node 19*8d13bc63SEmmanuel Vadotand property names, but this coding style narrows the range down to achieve 20*8d13bc63SEmmanuel Vadotbetter code readability. 21*8d13bc63SEmmanuel Vadot 22*8d13bc63SEmmanuel Vadot1. Node and property names can use only the following characters: 23*8d13bc63SEmmanuel Vadot 24*8d13bc63SEmmanuel Vadot * Lowercase characters: [a-z] 25*8d13bc63SEmmanuel Vadot * Digits: [0-9] 26*8d13bc63SEmmanuel Vadot * Dash: - 27*8d13bc63SEmmanuel Vadot 28*8d13bc63SEmmanuel Vadot2. Labels can use only the following characters: 29*8d13bc63SEmmanuel Vadot 30*8d13bc63SEmmanuel Vadot * Lowercase characters: [a-z] 31*8d13bc63SEmmanuel Vadot * Digits: [0-9] 32*8d13bc63SEmmanuel Vadot * Underscore: _ 33*8d13bc63SEmmanuel Vadot 34*8d13bc63SEmmanuel Vadot3. Unless a bus defines differently, unit addresses shall use lowercase 35*8d13bc63SEmmanuel Vadot hexadecimal digits, without leading zeros (padding). 36*8d13bc63SEmmanuel Vadot 37*8d13bc63SEmmanuel Vadot4. Hex values in properties, e.g. "reg", shall use lowercase hex. The address 38*8d13bc63SEmmanuel Vadot part can be padded with leading zeros. 39*8d13bc63SEmmanuel Vadot 40*8d13bc63SEmmanuel VadotExample:: 41*8d13bc63SEmmanuel Vadot 42*8d13bc63SEmmanuel Vadot gpi_dma2: dma-controller@a00000 { 43*8d13bc63SEmmanuel Vadot compatible = "qcom,sm8550-gpi-dma", "qcom,sm6350-gpi-dma"; 44*8d13bc63SEmmanuel Vadot reg = <0x0 0x00a00000 0x0 0x60000>; 45*8d13bc63SEmmanuel Vadot } 46*8d13bc63SEmmanuel Vadot 47*8d13bc63SEmmanuel VadotOrder of Nodes 48*8d13bc63SEmmanuel Vadot-------------- 49*8d13bc63SEmmanuel Vadot 50*8d13bc63SEmmanuel Vadot1. Nodes on any bus, thus using unit addresses for children, shall be 51*8d13bc63SEmmanuel Vadot ordered by unit address in ascending order. 52*8d13bc63SEmmanuel Vadot Alternatively for some subarchitectures, nodes of the same type can be 53*8d13bc63SEmmanuel Vadot grouped together, e.g. all I2C controllers one after another even if this 54*8d13bc63SEmmanuel Vadot breaks unit address ordering. 55*8d13bc63SEmmanuel Vadot 56*8d13bc63SEmmanuel Vadot2. Nodes without unit addresses shall be ordered alpha-numerically by the node 57*8d13bc63SEmmanuel Vadot name. For a few node types, they can be ordered by the main property, e.g. 58*8d13bc63SEmmanuel Vadot pin configuration states ordered by value of "pins" property. 59*8d13bc63SEmmanuel Vadot 60*8d13bc63SEmmanuel Vadot3. When extending nodes in the board DTS via &label, the entries shall be 61*8d13bc63SEmmanuel Vadot ordered either alpha-numerically or by keeping the order from DTSI, where 62*8d13bc63SEmmanuel Vadot the choice depends on the subarchitecture. 63*8d13bc63SEmmanuel Vadot 64*8d13bc63SEmmanuel VadotThe above-described ordering rules are easy to enforce during review, reduce 65*8d13bc63SEmmanuel Vadotchances of conflicts for simultaneous additions of new nodes to a file and help 66*8d13bc63SEmmanuel Vadotin navigating through the DTS source. 67*8d13bc63SEmmanuel Vadot 68*8d13bc63SEmmanuel VadotExample:: 69*8d13bc63SEmmanuel Vadot 70*8d13bc63SEmmanuel Vadot /* SoC DTSI */ 71*8d13bc63SEmmanuel Vadot 72*8d13bc63SEmmanuel Vadot / { 73*8d13bc63SEmmanuel Vadot cpus { 74*8d13bc63SEmmanuel Vadot /* ... */ 75*8d13bc63SEmmanuel Vadot }; 76*8d13bc63SEmmanuel Vadot 77*8d13bc63SEmmanuel Vadot psci { 78*8d13bc63SEmmanuel Vadot /* ... */ 79*8d13bc63SEmmanuel Vadot }; 80*8d13bc63SEmmanuel Vadot 81*8d13bc63SEmmanuel Vadot soc@0 { 82*8d13bc63SEmmanuel Vadot dma: dma-controller@10000 { 83*8d13bc63SEmmanuel Vadot /* ... */ 84*8d13bc63SEmmanuel Vadot }; 85*8d13bc63SEmmanuel Vadot 86*8d13bc63SEmmanuel Vadot clk: clock-controller@80000 { 87*8d13bc63SEmmanuel Vadot /* ... */ 88*8d13bc63SEmmanuel Vadot }; 89*8d13bc63SEmmanuel Vadot }; 90*8d13bc63SEmmanuel Vadot }; 91*8d13bc63SEmmanuel Vadot 92*8d13bc63SEmmanuel Vadot /* Board DTS - alphabetical order */ 93*8d13bc63SEmmanuel Vadot 94*8d13bc63SEmmanuel Vadot &clk { 95*8d13bc63SEmmanuel Vadot /* ... */ 96*8d13bc63SEmmanuel Vadot }; 97*8d13bc63SEmmanuel Vadot 98*8d13bc63SEmmanuel Vadot &dma { 99*8d13bc63SEmmanuel Vadot /* ... */ 100*8d13bc63SEmmanuel Vadot }; 101*8d13bc63SEmmanuel Vadot 102*8d13bc63SEmmanuel Vadot /* Board DTS - alternative order, keep as DTSI */ 103*8d13bc63SEmmanuel Vadot 104*8d13bc63SEmmanuel Vadot &dma { 105*8d13bc63SEmmanuel Vadot /* ... */ 106*8d13bc63SEmmanuel Vadot }; 107*8d13bc63SEmmanuel Vadot 108*8d13bc63SEmmanuel Vadot &clk { 109*8d13bc63SEmmanuel Vadot /* ... */ 110*8d13bc63SEmmanuel Vadot }; 111*8d13bc63SEmmanuel Vadot 112*8d13bc63SEmmanuel VadotOrder of Properties in Device Node 113*8d13bc63SEmmanuel Vadot---------------------------------- 114*8d13bc63SEmmanuel Vadot 115*8d13bc63SEmmanuel VadotThe following order of properties in device nodes is preferred: 116*8d13bc63SEmmanuel Vadot 117*8d13bc63SEmmanuel Vadot1. "compatible" 118*8d13bc63SEmmanuel Vadot2. "reg" 119*8d13bc63SEmmanuel Vadot3. "ranges" 120*8d13bc63SEmmanuel Vadot4. Standard/common properties (defined by common bindings, e.g. without 121*8d13bc63SEmmanuel Vadot vendor-prefixes) 122*8d13bc63SEmmanuel Vadot5. Vendor-specific properties 123*8d13bc63SEmmanuel Vadot6. "status" (if applicable) 124*8d13bc63SEmmanuel Vadot7. Child nodes, where each node is preceded with a blank line 125*8d13bc63SEmmanuel Vadot 126*8d13bc63SEmmanuel VadotThe "status" property is by default "okay", thus it can be omitted. 127*8d13bc63SEmmanuel Vadot 128*8d13bc63SEmmanuel VadotThe above-described ordering follows this approach: 129*8d13bc63SEmmanuel Vadot 130*8d13bc63SEmmanuel Vadot1. Most important properties start the node: compatible then bus addressing to 131*8d13bc63SEmmanuel Vadot match unit address. 132*8d13bc63SEmmanuel Vadot2. Each node will have common properties in similar place. 133*8d13bc63SEmmanuel Vadot3. Status is the last information to annotate that device node is or is not 134*8d13bc63SEmmanuel Vadot finished (board resources are needed). 135*8d13bc63SEmmanuel Vadot 136*8d13bc63SEmmanuel VadotExample:: 137*8d13bc63SEmmanuel Vadot 138*8d13bc63SEmmanuel Vadot /* SoC DTSI */ 139*8d13bc63SEmmanuel Vadot 140*8d13bc63SEmmanuel Vadot device_node: device-class@6789abc { 141*8d13bc63SEmmanuel Vadot compatible = "vendor,device"; 142*8d13bc63SEmmanuel Vadot reg = <0x0 0x06789abc 0x0 0xa123>; 143*8d13bc63SEmmanuel Vadot ranges = <0x0 0x0 0x06789abc 0x1000>; 144*8d13bc63SEmmanuel Vadot #dma-cells = <1>; 145*8d13bc63SEmmanuel Vadot clocks = <&clock_controller 0>, <&clock_controller 1>; 146*8d13bc63SEmmanuel Vadot clock-names = "bus", "host"; 147*8d13bc63SEmmanuel Vadot vendor,custom-property = <2>; 148*8d13bc63SEmmanuel Vadot status = "disabled"; 149*8d13bc63SEmmanuel Vadot 150*8d13bc63SEmmanuel Vadot child_node: child-class@100 { 151*8d13bc63SEmmanuel Vadot reg = <0x100 0x200>; 152*8d13bc63SEmmanuel Vadot /* ... */ 153*8d13bc63SEmmanuel Vadot }; 154*8d13bc63SEmmanuel Vadot }; 155*8d13bc63SEmmanuel Vadot 156*8d13bc63SEmmanuel Vadot /* Board DTS */ 157*8d13bc63SEmmanuel Vadot 158*8d13bc63SEmmanuel Vadot &device_node { 159*8d13bc63SEmmanuel Vadot vdd-supply = <&board_vreg1>; 160*8d13bc63SEmmanuel Vadot status = "okay"; 161*8d13bc63SEmmanuel Vadot } 162*8d13bc63SEmmanuel Vadot 163*8d13bc63SEmmanuel VadotIndentation 164*8d13bc63SEmmanuel Vadot----------- 165*8d13bc63SEmmanuel Vadot 166*8d13bc63SEmmanuel Vadot1. Use indentation according to Documentation/process/coding-style.rst. 167*8d13bc63SEmmanuel Vadot2. Each entry in arrays with multiple cells, e.g. "reg" with two IO addresses, 168*8d13bc63SEmmanuel Vadot shall be enclosed in <>. 169*8d13bc63SEmmanuel Vadot3. For arrays spanning across lines, it is preferred to align the continued 170*8d13bc63SEmmanuel Vadot entries with opening < from the first line. 171*8d13bc63SEmmanuel Vadot 172*8d13bc63SEmmanuel VadotExample:: 173*8d13bc63SEmmanuel Vadot 174*8d13bc63SEmmanuel Vadot thermal-sensor@c271000 { 175*8d13bc63SEmmanuel Vadot compatible = "qcom,sm8550-tsens", "qcom,tsens-v2"; 176*8d13bc63SEmmanuel Vadot reg = <0x0 0x0c271000 0x0 0x1000>, 177*8d13bc63SEmmanuel Vadot <0x0 0x0c222000 0x0 0x1000>; 178*8d13bc63SEmmanuel Vadot }; 179*8d13bc63SEmmanuel Vadot 180*8d13bc63SEmmanuel VadotOrganizing DTSI and DTS 181*8d13bc63SEmmanuel Vadot----------------------- 182*8d13bc63SEmmanuel Vadot 183*8d13bc63SEmmanuel VadotThe DTSI and DTS files shall be organized in a way representing the common, 184*8d13bc63SEmmanuel Vadotreusable parts of hardware. Typically, this means organizing DTSI and DTS files 185*8d13bc63SEmmanuel Vadotinto several files: 186*8d13bc63SEmmanuel Vadot 187*8d13bc63SEmmanuel Vadot1. DTSI with contents of the entire SoC, without nodes for hardware not present 188*8d13bc63SEmmanuel Vadot on the SoC. 189*8d13bc63SEmmanuel Vadot2. If applicable: DTSI with common or re-usable parts of the hardware, e.g. 190*8d13bc63SEmmanuel Vadot entire System-on-Module. 191*8d13bc63SEmmanuel Vadot3. DTS representing the board. 192*8d13bc63SEmmanuel Vadot 193*8d13bc63SEmmanuel VadotHardware components that are present on the board shall be placed in the 194*8d13bc63SEmmanuel Vadotboard DTS, not in the SoC or SoM DTSI. A partial exception is a common 195*8d13bc63SEmmanuel Vadotexternal reference SoC input clock, which could be coded as a fixed-clock in 196*8d13bc63SEmmanuel Vadotthe SoC DTSI with its frequency provided by each board DTS. 197