← Documents Documentation/devicetree/bindings/dts-coding-style.rst GitHub 원문 ↗

Linux 6.18.37 · Devicetree Bindings

Devicetree Sources Coding Style

DTS 이름, node/property 순서, 줄바꿈과 DTSI/DTS 구성 규칙입니다.

Source pathDocumentation/devicetree/bindings/dts-coding-style.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약과 해설

dts-coding-style.rst:1-210

Review conflict를 줄이고 일관된 DTS source를 만드는 coding style을 설명합니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

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

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

DTS 이름과 허용 문자

1-46

Devicetree Source(DTS)를 작성할 때 이 지침을 따라야 합니다. 이 규칙은 Devicetree Specification과 `dtc` compiler가 이미 표현하는 규칙, 즉 `W=1` 및 `W=2` build를 포함한 규칙을 보완합니다. 개별 architecture와 subarchitecture는 더 엄격한 추가 규칙을 정의할 수 있습니다.

Devicetree Specification은 node와 property 이름에 넓은 문자 범위를 허용하지만, 이 coding style은 code 가독성을 높이기 위해 범위를 좁힙니다. Node와 property 이름에는 소문자 `[a-z]`, 숫자 `[0-9]`, dash `-`만 사용할 수 있습니다. Label에는 소문자 `[a-z]`, 숫자 `[0-9]`, underscore `_`만 사용할 수 있습니다.

Bus가 다르게 정의하지 않는 한 unit address는 leading zero padding 없이 소문자 hexadecimal digit을 사용해야 합니다. `reg` 같은 property의 hex 값도 소문자 hex를 사용하며, 이때 address 부분은 leading zero로 padding할 수 있습니다.

DTS 식별자 문자 규칙
대상허용 문자구분자
Node/property name[a-z], [0-9]dash (-)
Label[a-z], [0-9]underscore (_)
Unit addresslowercase hexadecimalleading zero 금지
Property hex valuelowercase hexadecimaladdress padding 허용

Node/property와 label에서 허용되는 문자를 구분합니다.

gpi_dma2: dma-controller@a00000 {
        compatible = "qcom,sm8550-gpi-dma", "qcom,sm6350-gpi-dma";
        reg = <0x0 0x00a00000 0x0 0x60000>;
}

Node 순서

47-111

Child에 unit address를 사용하는 모든 bus의 node는 unit address 오름차순으로 정렬해야 합니다. 일부 subarchitecture에서는 이 순서를 깨더라도 같은 종류의 node를 한데 묶을 수 있습니다. 예를 들어 모든 I2C controller를 연속해서 둘 수 있습니다.

Unit address가 없는 node는 node 이름의 영숫자 순으로 정렬해야 합니다. 일부 node 종류는 주 property를 기준으로 정렬할 수 있습니다. 예를 들어 pin configuration state는 `pins` property 값 순으로 둘 수 있습니다.

Board DTS에서 `&label`로 node를 확장할 때 entry는 영숫자 순으로 정렬하거나 DTSI의 순서를 유지해야 하며, 어느 방식을 쓸지는 subarchitecture에 따릅니다. 이러한 규칙은 review에서 쉽게 강제할 수 있고, 같은 파일에 node를 동시에 추가할 때 conflict 가능성을 줄이며 DTS source 탐색을 돕습니다.

DTS node 정렬 결정
Bus child nodeUnit address 오름차순
Same-type exceptionSubarchitecture가 허용하면 종류별 그룹
No unit addressNode name 영숫자 순
Board &label extension영숫자 순 또는 DTSI 순서

Node의 unit address 유무와 board 확장 여부에 따라 적용할 순서를 나타냅니다.

/* SoC DTSI */

/ {
        cpus {
                /* ... */
        };

        psci {
                /* ... */
        };

        soc@0 {
                dma: dma-controller@10000 {
                        /* ... */
                };

                clk: clock-controller@80000 {
                        /* ... */
                };
        };
};

/* Board DTS - alphabetical order */

&clk {
        /* ... */
};

&dma {
        /* ... */
};

/* Board DTS - alternative order, keep as DTSI */

&dma {
        /* ... */
};

&clk {
        /* ... */
};

Device node의 property 순서

112-170

Device node property의 권장 순서는 `compatible`, `reg`, `ranges`, 표준/common property, vendor-specific property, 해당하는 경우 `status`, 그리고 각 node 앞에 blank line을 둔 child node입니다. 표준/common property는 vendor prefix 없이 common binding에서 정의된 속성을 뜻합니다.

`status`의 기본값은 `okay`이므로 생략할 수 있습니다. 이 순서는 가장 중요한 `compatible`과 unit address에 맞추는 bus addressing을 node 앞에 두고, 모든 node에서 common property를 비슷한 위치에 배치하며, board resource가 필요한지를 표시하는 최종 정보인 `status`를 마지막에 두려는 방식입니다.

각 group 안의 개별 property는 property 이름의 natural sort order를 따라야 합니다.

Device node property 순서
순서Group
1Identitycompatible
2Bus addressingreg, ranges
3Standard/commonclocks, #address-cells
4Vendor-specificvendor,custom-property
5Availabilitystatus
6ChildrenBlank line 뒤 child node

권장 property group 순서를 review 가능한 단계로 정리합니다.

/* SoC DTSI */

device_node: device-class@6789abc {
        compatible = "vendor,device";
        reg = <0x0 0x06789abc 0x0 0xa123>;
        ranges = <0x0 0x0 0x06789abc 0x1000>;
        #dma-cells = <1>;
        clocks = <&clock_controller 0>, <&clock_controller 1>;
        clock-names = "bus", "host";
        #address-cells = <1>;
        #size-cells = <1>;
        vendor,custom-property = <2>;
        status = "disabled";

        child_node: child-class@100 {
                reg = <0x100 0x200>;
                /* ... */
        };
};

/* Board DTS */

&device_node {
        vdd-0v9-supply = <&board_vreg1>;
        vdd-1v8-supply = <&board_vreg4>;
        vdd-3v3-supply = <&board_vreg2>;
        vdd-12v-supply = <&board_vreg3>;
        status = "okay";
}

들여쓰기와 줄바꿈

171-193

들여쓰기와 줄바꿈은 `Documentation/process/coding-style.rst`를 따릅니다. 두 IO address를 담는 `reg`처럼 여러 cell을 가진 array에서는 각 entry를 `<>`로 감싸야 합니다.

여러 줄에 걸친 array는 item 경계에서 나누고 이어지는 entry를 첫 줄의 여는 `<`에 맞추는 방식을 권장합니다. 개별 item이 line wrap 제한을 크게 넘지 않는 한 item 자체를 나누는 것은 피합니다.

thermal-sensor@c271000 {
        compatible = "qcom,sm8550-tsens", "qcom,tsens-v2";
        reg = <0x0 0x0c271000 0x0 0x1000>,
              <0x0 0x0c222000 0x0 0x1000>;
        /* Lines exceeding coding style line wrap limit: */
        interconnects = <&aggre1_noc MASTER_USB3_0 0 &mc_virt SLAVE_EBI1 0>,
                        <&gem_noc MASTER_APPSS_PROC 0 &config_noc SLAVE_USB3_0 0>;
};

DTSI와 DTS 구성

194-210

DTSI와 DTS 파일은 hardware의 공통되고 재사용 가능한 부분을 나타내도록 구성해야 합니다. 일반적으로 전체 SoC 내용을 담되 SoC에 없는 hardware node는 포함하지 않는 DTSI, 해당하는 경우 System-on-Module 전체처럼 공통 또는 재사용 가능한 hardware 부분을 담는 DTSI, 그리고 board를 나타내는 DTS로 나눕니다.

Board에 존재하는 hardware component는 SoC나 SoM DTSI가 아니라 board DTS에 두어야 합니다. 부분적인 예외는 공통 external reference SoC input clock입니다. 이를 SoC DTSI에 `fixed-clock`으로 작성하고 각 board DTS가 frequency를 제공할 수 있습니다.

Devicetree file 계층
SoC DTSISoC에 실제 존재하는 전체 hardware
SoM/common DTSI공통 또는 재사용 가능한 module hardware
Board DTSBoard에 실장된 component와 resource
Reference input clock exceptionSoC fixed-clock + board frequency

공통 hardware에서 구체적인 board 구성으로 내려가는 재사용 계층을 나타냅니다.