diff mbox series

docs: dt-bindings: add DTS Coding Style document

Message ID 20231116181218.18886-1-krzysztof.kozlowski@linaro.org
State New
Headers show
Series docs: dt-bindings: add DTS Coding Style document | expand

Commit Message

Krzysztof Kozlowski Nov. 16, 2023, 6:12 p.m. UTC
Document preferred coding style for Devicetree sources (DTS and DTSI),
to bring consistency among all (sub)architectures and ease in reviews.

Cc: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
Cc: Arnd Bergmann <arnd@arndb.de>
Cc: Bjorn Andersson <andersson@kernel.org>
Cc: Geert Uytterhoeven <geert+renesas@glider.be>
Cc: Heiko Stuebner <heiko@sntech.de>
Cc: Konrad Dybcio <konrad.dybcio@linaro.org>
Cc: Matthias Brugger <matthias.bgg@gmail.com>
Cc: Michal Simek <michal.simek@amd.com>
Cc: Neil Armstrong <neil.armstrong@linaro.org>
Cc: Nishanth Menon <nm@ti.com>
Cc: Olof Johansson <olof@lixom.net>
Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>

---

Merging idea: Rob/DT bindings
---
 Documentation/devicetree/bindings/index.rst   |   1 +
 .../devicetree/bindings/writing-dts.rst       | 137 ++++++++++++++++++
 2 files changed, 138 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/writing-dts.rst

Comments

Andrew Davis Nov. 17, 2023, 2:03 p.m. UTC | #1
On 11/16/23 2:33 PM, Heiko Stuebner wrote:
> Am Donnerstag, 16. November 2023, 21:23:20 CET schrieb Krzysztof Kozlowski:
>> On 16/11/2023 21:03, Heiko Stuebner wrote:
>>
>>>>> I guess the only thing I do have questions about is the part
>>>>>
>>>>>> +4. All properties with values
>>>>>> +5. Boolean properties
>>>>>
>>>>> Is there a rationale for it? Because with it things like regulator-*
>>>>> properties then end up in two different blocks.
>>>>
>>>> Good point. It is only a matter of style that this:
>>>>
>>>> foo {
>>>> 	compatible = "foo";
>>>> 	reg = <0x1>;
>>>> 	clocks = <&clk>;
>>>> 	wakeup-source;
>>>> 	key-autorepeat;
>>>> }
>>>>
>>>> looks better to me than:
>>>>
>>>>
>>>> foo {
>>>> 	compatible = "foo";
>>>> 	reg = <0x1>;
>>>> 	key-autorepeat;
>>>> 	wakeup-source;
>>>> 	clocks = <&clk>;
>>>> }
>>>>
>>>> But you have good point that similar properties should be usually
>>>> grouped together.
>>>>
>>>> About which regulator properties are you thinking now? You mean the
>>>> supplies or the provider?
>>>
>>> I was thinking about the provider. There are
>>> 	regulator-min-microvolt = <>;
>>> and friends, but also
>>> 	regulator-boot-on;
>>
>> These are in regulator provider nodes and above guideline would keep
>> logical order:
>>
>> 	regulator-name = "vdd_kfc";
>> 	regulator-min-microvolt = <800000>;
>> 	regulator-max-microvolt = <1500000>;
>> 	regulator-always-on;
>> 	regulator-boot-on;
>>
>> 	regulator-state-mem {
>> 		regulator-off-in-suspend;
>> 	};
>>
>> What exactly would be here misordered?
> 
> going with the vcc5v0_host regulator of the rk3588-quartzpro64 and
> 
> +1. compatible
> +2. reg
> +3. ranges
> +4. All properties with values
> +5. Boolean properties
> +6. status (if applicable)
> +7. Child nodes
> 
> we'd end up with
> 
>          vcc5v0_host: vcc5v0-host-regulator {
> /* 1. */        compatible = "regulator-fixed";
> /* 4. */        gpio = <&gpio4 RK_PB0 GPIO_ACTIVE_HIGH>;
>                  pinctrl-names = "default";
>                  pinctrl-0 = <&vcc5v0_host_en>;
>                  regulator-min-microvolt = <5000000>;
>                  regulator-max-microvolt = <5000000>;
>                  regulator-name = "vcc5v0_host";
>                  vin-supply = <&vcc5v0_usb>;
> /* 5. */        enable-active-high;
>                  regulator-always-on;
>                  regulator-boot-on;
>          };
> 

How about grouping like properties (defined in the same schema),
then sorting within that group. Would also allow for defining
where to add spacing.

1. compatible
2. reg
3. ranges
4. All property groups
   4.1 Properties with values
   4.2 Boolean properties
   4.3 Separating space
6. status (if applicable)
7. Child nodes

Your node then would look like we expect:

vcc5v0_host: vcc5v0-host-regulator {
/* 1   */   compatible = "regulator-fixed";

/* 4.1 */   pinctrl-names = "default";
/* 4.1 */   pinctrl-0 = <&vcc5v0_host_en>;
/* 4.3 */
/* 4.1 */   regulator-min-microvolt = <5000000>;
/* 4.1 */   regulator-max-microvolt = <5000000>;
/* 4.1 */   regulator-name = "vcc5v0_host";
/* 4.2 */   regulator-always-on;
/* 4.2 */   regulator-boot-on;
/* 4.2 */   enable-active-high;
/* 4.3 */
/* 4.1 */   gpio = <&gpio4 RK_PB0 GPIO_ACTIVE_HIGH>;
...
};


Andrew


> which I find somewhat counter-intuitive ;-) .
> 
> 
> Heiko
> 
>
Heiko Stuebner Nov. 17, 2023, 7:38 p.m. UTC | #2
Am Freitag, 17. November 2023, 15:03:38 CET schrieb Andrew Davis:
> On 11/16/23 2:33 PM, Heiko Stuebner wrote:
> > Am Donnerstag, 16. November 2023, 21:23:20 CET schrieb Krzysztof Kozlowski:
> >> On 16/11/2023 21:03, Heiko Stuebner wrote:
> > going with the vcc5v0_host regulator of the rk3588-quartzpro64 and
> > 
> > +1. compatible
> > +2. reg
> > +3. ranges
> > +4. All properties with values
> > +5. Boolean properties
> > +6. status (if applicable)
> > +7. Child nodes
> > 
> > we'd end up with
> > 
> >          vcc5v0_host: vcc5v0-host-regulator {
> > /* 1. */        compatible = "regulator-fixed";
> > /* 4. */        gpio = <&gpio4 RK_PB0 GPIO_ACTIVE_HIGH>;
> >                  pinctrl-names = "default";
> >                  pinctrl-0 = <&vcc5v0_host_en>;
> >                  regulator-min-microvolt = <5000000>;
> >                  regulator-max-microvolt = <5000000>;
> >                  regulator-name = "vcc5v0_host";
> >                  vin-supply = <&vcc5v0_usb>;
> > /* 5. */        enable-active-high;
> >                  regulator-always-on;
> >                  regulator-boot-on;
> >          };
> > 
> 
> How about grouping like properties (defined in the same schema),
> then sorting within that group. Would also allow for defining
> where to add spacing.
> 
> 1. compatible
> 2. reg
> 3. ranges
> 4. All property groups
>    4.1 Properties with values
>    4.2 Boolean properties
>    4.3 Separating space
> 6. status (if applicable)
> 7. Child nodes
> 
> Your node then would look like we expect:
> 
> vcc5v0_host: vcc5v0-host-regulator {
> /* 1   */   compatible = "regulator-fixed";
> 
> /* 4.1 */   pinctrl-names = "default";
> /* 4.1 */   pinctrl-0 = <&vcc5v0_host_en>;
> /* 4.3 */
> /* 4.1 */   regulator-min-microvolt = <5000000>;
> /* 4.1 */   regulator-max-microvolt = <5000000>;
> /* 4.1 */   regulator-name = "vcc5v0_host";
> /* 4.2 */   regulator-always-on;
> /* 4.2 */   regulator-boot-on;
> /* 4.2 */   enable-active-high;
> /* 4.3 */
> /* 4.1 */   gpio = <&gpio4 RK_PB0 GPIO_ACTIVE_HIGH>;
> ...
> };

I'm really not sure about adding big sets of rules.
In the above example you'd also need to define which schema has a higher
priority? ;-)


When I started with Rockchip stuff, I also had some fancy way of sorting
elements in mind that was really intuitive to myself :-) .
Over time I realized that it was quite complex - especially when I had to
explain it to people.

There are definite advantages for having compatible + reg + status in
fixed positions, as it helps going over a whole dt to spot the huge
mistakes (accidentially disabled, wrong address), but for the rest a
simple alphabetical sorting is easiest to explain to people :-) .

And alphabetic elements are also easier on my eyes.


I just think having a short clean set of rules like Krzysztof proposed,
is easier to follow and "enforce" and also most likely doesn't deter
people from contributing, if mainline work is not their main occupation.


Heiko
Andrew Davis Nov. 17, 2023, 7:54 p.m. UTC | #3
On 11/17/23 1:38 PM, Heiko Stübner wrote:
> Am Freitag, 17. November 2023, 15:03:38 CET schrieb Andrew Davis:
>> On 11/16/23 2:33 PM, Heiko Stuebner wrote:
>>> Am Donnerstag, 16. November 2023, 21:23:20 CET schrieb Krzysztof Kozlowski:
>>>> On 16/11/2023 21:03, Heiko Stuebner wrote:
>>> going with the vcc5v0_host regulator of the rk3588-quartzpro64 and
>>>
>>> +1. compatible
>>> +2. reg
>>> +3. ranges
>>> +4. All properties with values
>>> +5. Boolean properties
>>> +6. status (if applicable)
>>> +7. Child nodes
>>>
>>> we'd end up with
>>>
>>>           vcc5v0_host: vcc5v0-host-regulator {
>>> /* 1. */        compatible = "regulator-fixed";
>>> /* 4. */        gpio = <&gpio4 RK_PB0 GPIO_ACTIVE_HIGH>;
>>>                   pinctrl-names = "default";
>>>                   pinctrl-0 = <&vcc5v0_host_en>;
>>>                   regulator-min-microvolt = <5000000>;
>>>                   regulator-max-microvolt = <5000000>;
>>>                   regulator-name = "vcc5v0_host";
>>>                   vin-supply = <&vcc5v0_usb>;
>>> /* 5. */        enable-active-high;
>>>                   regulator-always-on;
>>>                   regulator-boot-on;
>>>           };
>>>
>>
>> How about grouping like properties (defined in the same schema),
>> then sorting within that group. Would also allow for defining
>> where to add spacing.
>>
>> 1. compatible
>> 2. reg
>> 3. ranges
>> 4. All property groups
>>     4.1 Properties with values
>>     4.2 Boolean properties
>>     4.3 Separating space
>> 6. status (if applicable)
>> 7. Child nodes
>>
>> Your node then would look like we expect:
>>
>> vcc5v0_host: vcc5v0-host-regulator {
>> /* 1   */   compatible = "regulator-fixed";
>>
>> /* 4.1 */   pinctrl-names = "default";
>> /* 4.1 */   pinctrl-0 = <&vcc5v0_host_en>;
>> /* 4.3 */
>> /* 4.1 */   regulator-min-microvolt = <5000000>;
>> /* 4.1 */   regulator-max-microvolt = <5000000>;
>> /* 4.1 */   regulator-name = "vcc5v0_host";
>> /* 4.2 */   regulator-always-on;
>> /* 4.2 */   regulator-boot-on;
>> /* 4.2 */   enable-active-high;
>> /* 4.3 */
>> /* 4.1 */   gpio = <&gpio4 RK_PB0 GPIO_ACTIVE_HIGH>;
>> ...
>> };
> 
> I'm really not sure about adding big sets of rules.
> In the above example you'd also need to define which schema has a higher
> priority? ;-)
> 
> 
> When I started with Rockchip stuff, I also had some fancy way of sorting
> elements in mind that was really intuitive to myself :-) .
> Over time I realized that it was quite complex - especially when I had to
> explain it to people.
> 
> There are definite advantages for having compatible + reg + status in
> fixed positions, as it helps going over a whole dt to spot the huge
> mistakes (accidentially disabled, wrong address), but for the rest a
> simple alphabetical sorting is easiest to explain to people :-) .
> 
> And alphabetic elements are also easier on my eyes.
> 

+1 for starting with compatible/reg/status that we would like to see
in the same spot in each node.

Not so sure on plain alphabetical. That has the same issue you pointed out
with splitting value vs boolean properties, related properties would end up
not grouped. Some like regulator- with the same prefix will, but think -gpios
that is a postfix, they would be scattered about.

How about just enforcing ordering on the couple common property we care about
seeing and everything else left free-hand as it today?

Andrew

> 
> I just think having a short clean set of rules like Krzysztof proposed,
> is easier to follow and "enforce" and also most likely doesn't deter
> people from contributing, if mainline work is not their main occupation.
> 
> 
> Heiko
> 
> 
>
Heiko Stuebner Nov. 17, 2023, 8:04 p.m. UTC | #4
Am Freitag, 17. November 2023, 20:54:05 CET schrieb Andrew Davis:
> On 11/17/23 1:38 PM, Heiko Stübner wrote:
> > Am Freitag, 17. November 2023, 15:03:38 CET schrieb Andrew Davis:
> >> On 11/16/23 2:33 PM, Heiko Stuebner wrote:
> >>> Am Donnerstag, 16. November 2023, 21:23:20 CET schrieb Krzysztof Kozlowski:
> >>>> On 16/11/2023 21:03, Heiko Stuebner wrote:
> >>> going with the vcc5v0_host regulator of the rk3588-quartzpro64 and
> >>>
> >>> +1. compatible
> >>> +2. reg
> >>> +3. ranges
> >>> +4. All properties with values
> >>> +5. Boolean properties
> >>> +6. status (if applicable)
> >>> +7. Child nodes
> >>>
> >>> we'd end up with
> >>>
> >>>           vcc5v0_host: vcc5v0-host-regulator {
> >>> /* 1. */        compatible = "regulator-fixed";
> >>> /* 4. */        gpio = <&gpio4 RK_PB0 GPIO_ACTIVE_HIGH>;
> >>>                   pinctrl-names = "default";
> >>>                   pinctrl-0 = <&vcc5v0_host_en>;
> >>>                   regulator-min-microvolt = <5000000>;
> >>>                   regulator-max-microvolt = <5000000>;
> >>>                   regulator-name = "vcc5v0_host";
> >>>                   vin-supply = <&vcc5v0_usb>;
> >>> /* 5. */        enable-active-high;
> >>>                   regulator-always-on;
> >>>                   regulator-boot-on;
> >>>           };
> >>>
> >>
> >> How about grouping like properties (defined in the same schema),
> >> then sorting within that group. Would also allow for defining
> >> where to add spacing.
> >>
> >> 1. compatible
> >> 2. reg
> >> 3. ranges
> >> 4. All property groups
> >>     4.1 Properties with values
> >>     4.2 Boolean properties
> >>     4.3 Separating space
> >> 6. status (if applicable)
> >> 7. Child nodes
> >>
> >> Your node then would look like we expect:
> >>
> >> vcc5v0_host: vcc5v0-host-regulator {
> >> /* 1   */   compatible = "regulator-fixed";
> >>
> >> /* 4.1 */   pinctrl-names = "default";
> >> /* 4.1 */   pinctrl-0 = <&vcc5v0_host_en>;
> >> /* 4.3 */
> >> /* 4.1 */   regulator-min-microvolt = <5000000>;
> >> /* 4.1 */   regulator-max-microvolt = <5000000>;
> >> /* 4.1 */   regulator-name = "vcc5v0_host";
> >> /* 4.2 */   regulator-always-on;
> >> /* 4.2 */   regulator-boot-on;
> >> /* 4.2 */   enable-active-high;
> >> /* 4.3 */
> >> /* 4.1 */   gpio = <&gpio4 RK_PB0 GPIO_ACTIVE_HIGH>;
> >> ...
> >> };
> > 
> > I'm really not sure about adding big sets of rules.
> > In the above example you'd also need to define which schema has a higher
> > priority? ;-)
> > 
> > 
> > When I started with Rockchip stuff, I also had some fancy way of sorting
> > elements in mind that was really intuitive to myself :-) .
> > Over time I realized that it was quite complex - especially when I had to
> > explain it to people.
> > 
> > There are definite advantages for having compatible + reg + status in
> > fixed positions, as it helps going over a whole dt to spot the huge
> > mistakes (accidentially disabled, wrong address), but for the rest a
> > simple alphabetical sorting is easiest to explain to people :-) .
> > 
> > And alphabetic elements are also easier on my eyes.
> > 
> 
> +1 for starting with compatible/reg/status that we would like to see
> in the same spot in each node.
> 
> Not so sure on plain alphabetical. That has the same issue you pointed out
> with splitting value vs boolean properties, related properties would end up
> not grouped. Some like regulator- with the same prefix will, but think -gpios
> that is a postfix, they would be scattered about.
> 
> How about just enforcing ordering on the couple common property we care about
> seeing and everything else left free-hand as it today?

Sounds like a very sensible idea :-) .

Especially as the sorting of individual properties is just a tiny part of
Krzysztof's document, and all the other parts in it are way more
important anyway.


Heiko
Konrad Dybcio Nov. 17, 2023, 11:53 p.m. UTC | #5
On 16.11.2023 19:12, Krzysztof Kozlowski wrote:
> Document preferred coding style for Devicetree sources (DTS and DTSI),
> to bring consistency among all (sub)architectures and ease in reviews.
> 
> Cc: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
> Cc: Arnd Bergmann <arnd@arndb.de>
> Cc: Bjorn Andersson <andersson@kernel.org>
> Cc: Geert Uytterhoeven <geert+renesas@glider.be>
> Cc: Heiko Stuebner <heiko@sntech.de>
> Cc: Konrad Dybcio <konrad.dybcio@linaro.org>
> Cc: Matthias Brugger <matthias.bgg@gmail.com>
> Cc: Michal Simek <michal.simek@amd.com>
> Cc: Neil Armstrong <neil.armstrong@linaro.org>
> Cc: Nishanth Menon <nm@ti.com>
> Cc: Olof Johansson <olof@lixom.net>
> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
> 
> ---
[...]

> +Order of Nodes
> +--------------
> +
> +1. Nodes within any bus, thus using unit addresses for children, shall be
> +   ordered incrementally by unit address.
> +
> +2. Nodes without unit addresses should be ordered alpha-numerically.
I largely agree with all this given our fight for some level of
tidyness across linux-msm, but there's one specific case where I think
it would make sense to break this ordering, and that's GPIO states:


&pinctrl {
	xyz-active-state {
		pins ="gpio0";
		...
	};

	abc-active-state {
		pins ="gpio1";
		...
	};

	qwe-active-state {
		pins ="gpio2";
		...
	};
};

looks so much more readable to me than

&pinctrl {
	abc-active-state {
		pins ="gpio1";
		...
	};

	qwe-active-state {
		pins ="gpio2";
		...
	};

	xyz-active-state {
		pins ="gpio0";
		...
	};
};

Konrad
Konrad Dybcio Nov. 17, 2023, 11:55 p.m. UTC | #6
On 16.11.2023 21:44, Rob Herring wrote:
> On Thu, Nov 16, 2023 at 12:12 PM Krzysztof Kozlowski
> <krzysztof.kozlowski@linaro.org> wrote:
>>
>> Document preferred coding style for Devicetree sources (DTS and DTSI),
>> to bring consistency among all (sub)architectures and ease in reviews.
> 
> Thanks for doing this.
> 
>>
>> Cc: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
>> Cc: Arnd Bergmann <arnd@arndb.de>
>> Cc: Bjorn Andersson <andersson@kernel.org>
>> Cc: Geert Uytterhoeven <geert+renesas@glider.be>
>> Cc: Heiko Stuebner <heiko@sntech.de>
>> Cc: Konrad Dybcio <konrad.dybcio@linaro.org>
>> Cc: Matthias Brugger <matthias.bgg@gmail.com>
>> Cc: Michal Simek <michal.simek@amd.com>
>> Cc: Neil Armstrong <neil.armstrong@linaro.org>
>> Cc: Nishanth Menon <nm@ti.com>
>> Cc: Olof Johansson <olof@lixom.net>
>> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
>>
>> ---
[...]

>> +Example::
>> +
>> +       thermal-sensor@c271000 {
>> +               compatible = "qcom,sm8550-tsens", "qcom,tsens-v2";
>> +               reg = <0x0 0x0c271000 0x0 0x1000>,
>> +                     <0x0 0x0c222000 0x0 0x1000>;
> 
> You should cover the <> style too, meaning <> around each logical entry.
FWIW this matters because e.g.

https://github.com/devicetree-org/devicetree-specification/issues/66

Konrad
Konrad Dybcio Nov. 17, 2023, 11:58 p.m. UTC | #7
On 16.11.2023 19:12, Krzysztof Kozlowski wrote:
> Document preferred coding style for Devicetree sources (DTS and DTSI),
> to bring consistency among all (sub)architectures and ease in reviews.
> 
> Cc: AngeloGioacchino Del Regno <angelogioacchino.delregno@collabora.com>
> Cc: Arnd Bergmann <arnd@arndb.de>
> Cc: Bjorn Andersson <andersson@kernel.org>
> Cc: Geert Uytterhoeven <geert+renesas@glider.be>
> Cc: Heiko Stuebner <heiko@sntech.de>
> Cc: Konrad Dybcio <konrad.dybcio@linaro.org>
> Cc: Matthias Brugger <matthias.bgg@gmail.com>
> Cc: Michal Simek <michal.simek@amd.com>
> Cc: Neil Armstrong <neil.armstrong@linaro.org>
> Cc: Nishanth Menon <nm@ti.com>
> Cc: Olof Johansson <olof@lixom.net>
> Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
> 
> ---
Another thing that was suggested by at least one person to me is that
with a formalized ordering system in place AND using dt-bindings,
devicetrees could largely be reduced to a set of structs thrown into
some sort of an ASLC-alike (meaning: you fill out structs with the
necessary data, like reg, name etc. and the computer infers the rest
and creates a nice & stylish output for you).

Konrad
Geert Uytterhoeven Nov. 18, 2023, 11:25 a.m. UTC | #8
Hi Heiko,

On Fri, Nov 17, 2023 at 8:38 PM Heiko Stübner <heiko@sntech.de> wrote:
> Am Freitag, 17. November 2023, 15:03:38 CET schrieb Andrew Davis:
> > On 11/16/23 2:33 PM, Heiko Stuebner wrote:
> > > Am Donnerstag, 16. November 2023, 21:23:20 CET schrieb Krzysztof Kozlowski:
> > >> On 16/11/2023 21:03, Heiko Stuebner wrote:
> > > going with the vcc5v0_host regulator of the rk3588-quartzpro64 and
> > >
> > > +1. compatible
> > > +2. reg
> > > +3. ranges
> > > +4. All properties with values
> > > +5. Boolean properties
> > > +6. status (if applicable)
> > > +7. Child nodes
> > >
> > > we'd end up with
> > >
> > >          vcc5v0_host: vcc5v0-host-regulator {
> > > /* 1. */        compatible = "regulator-fixed";
> > > /* 4. */        gpio = <&gpio4 RK_PB0 GPIO_ACTIVE_HIGH>;
> > >                  pinctrl-names = "default";
> > >                  pinctrl-0 = <&vcc5v0_host_en>;
> > >                  regulator-min-microvolt = <5000000>;
> > >                  regulator-max-microvolt = <5000000>;
> > >                  regulator-name = "vcc5v0_host";
> > >                  vin-supply = <&vcc5v0_usb>;
> > > /* 5. */        enable-active-high;
> > >                  regulator-always-on;
> > >                  regulator-boot-on;
> > >          };
> > >
> >
> > How about grouping like properties (defined in the same schema),
> > then sorting within that group. Would also allow for defining
> > where to add spacing.
> >
> > 1. compatible
> > 2. reg
> > 3. ranges
> > 4. All property groups
> >    4.1 Properties with values
> >    4.2 Boolean properties
> >    4.3 Separating space
> > 6. status (if applicable)
> > 7. Child nodes
> >
> > Your node then would look like we expect:
> >
> > vcc5v0_host: vcc5v0-host-regulator {
> > /* 1   */   compatible = "regulator-fixed";
> >
> > /* 4.1 */   pinctrl-names = "default";
> > /* 4.1 */   pinctrl-0 = <&vcc5v0_host_en>;
> > /* 4.3 */
> > /* 4.1 */   regulator-min-microvolt = <5000000>;
> > /* 4.1 */   regulator-max-microvolt = <5000000>;
> > /* 4.1 */   regulator-name = "vcc5v0_host";
> > /* 4.2 */   regulator-always-on;
> > /* 4.2 */   regulator-boot-on;
> > /* 4.2 */   enable-active-high;
> > /* 4.3 */
> > /* 4.1 */   gpio = <&gpio4 RK_PB0 GPIO_ACTIVE_HIGH>;
> > ...
> > };
>
> I'm really not sure about adding big sets of rules.
> In the above example you'd also need to define which schema has a higher
> priority? ;-)
>
>
> When I started with Rockchip stuff, I also had some fancy way of sorting
> elements in mind that was really intuitive to myself :-) .
> Over time I realized that it was quite complex - especially when I had to
> explain it to people.

Feel familiar.

> There are definite advantages for having compatible + reg + status in
> fixed positions, as it helps going over a whole dt to spot the huge
> mistakes (accidentially disabled, wrong address), but for the rest a
> simple alphabetical sorting is easiest to explain to people :-) .
>
> And alphabetic elements are also easier on my eyes.

Alphabetical does break logical ordering and grouping.

Apart from compatible/reg order, on Renesas SoCs we usually
also have the clocks/resets/power-domains block.

> I just think having a short clean set of rules like Krzysztof proposed,
> is easier to follow and "enforce" and also most likely doesn't deter
> people from contributing, if mainline work is not their main occupation.

And in reality, most of this is created by copy-and-corrupt^Wmodify...

Gr{oetje,eeting}s,

                        Geert
diff mbox series

Patch

diff --git a/Documentation/devicetree/bindings/index.rst b/Documentation/devicetree/bindings/index.rst
index d9002a3a0abb..975449be4862 100644
--- a/Documentation/devicetree/bindings/index.rst
+++ b/Documentation/devicetree/bindings/index.rst
@@ -5,5 +5,6 @@ 
 
    ABI
    writing-bindings
+   writing-dts
    writing-schema
    submitting-patches
diff --git a/Documentation/devicetree/bindings/writing-dts.rst b/Documentation/devicetree/bindings/writing-dts.rst
new file mode 100644
index 000000000000..10c477ec1eed
--- /dev/null
+++ b/Documentation/devicetree/bindings/writing-dts.rst
@@ -0,0 +1,137 @@ 
+.. SPDX-License-Identifier: GPL-2.0
+.. _writingdts:
+
+===================================================
+Writing Devicetree Sources (DTS) - DTS Coding Style
+===================================================
+
+When writing Devicetree Sources (DTS) please observe below guidelines.  They
+should be considered complementary to any rules expressed already in Devicetree
+Specification and dtc compiler (including W=1 and W=2 builds).
+
+Individual architectures and sub-architectures can add additional rules, making
+the style stricter.
+
+Naming and Valid Characters
+---------------------------
+
+1. Node and property names are allowed to use only:
+
+   * lowercase characters:: [a-z]
+   * digits:: [0-9]
+   * dash:: -
+
+2. Labels are allowed to use only:
+
+   * lowercase characters:: [a-z]
+   * digits:: [0-9]
+   * underscore:: _
+
+3. Unit addresses should use lowercase hex, without leading zeros (padding).
+
+4. Hex values in properties, e.g. "reg", should use lowercase hex.  Any address
+   part can be padded with leading zeros.
+
+Example::
+
+	gpi_dma2: dma-controller@800000 {
+		compatible = "qcom,sm8550-gpi-dma", "qcom,sm6350-gpi-dma";
+		reg = <0x0 0x00800000 0x0 0x60000>;
+	}
+
+Order of Nodes
+--------------
+
+1. Nodes within any bus, thus using unit addresses for children, shall be
+   ordered incrementally by unit address.
+
+2. Nodes without unit addresses should be ordered alpha-numerically.
+
+3. When extending nodes in board DTS via &label, the entries should be ordered
+   alpha-numerically.
+
+Example::
+
+	// SoC DTSI
+
+	\ {
+		cpus {
+			// ...
+		};
+
+		psci {
+			// ...
+		};
+
+		soc@ {
+			dma: dma-controller@10000 {
+				// ...
+			};
+
+			clk: clock-controller@80000 {
+				// ...
+			};
+		};
+	};
+
+	// Board DTS
+
+	&clk {
+		// ...
+	};
+
+	&dma {
+		// ...
+	};
+
+
+Order of Properties in Device Node
+----------------------------------
+
+Following order of properties in device nodes is preferred:
+
+1. compatible
+2. reg
+3. ranges
+4. All properties with values
+5. Boolean properties
+6. status (if applicable)
+7. Child nodes
+
+The "status" property is by default "okay", thus it can be omitted.
+
+Example::
+
+	// SoC DTSI
+
+	usb_1_hsphy: phy@88e3000 {
+		compatible = "qcom,sm8550-snps-eusb2-phy";
+		reg = <0x0 0x088e3000 0x0 0x154>;
+		#phy-cells = <0>;
+		resets = <&gcc GCC_QUSB2PHY_PRIM_BCR>;
+		status = "disabled";
+	};
+
+	// Board DTS
+
+	&usb_1_hsphy {
+		clocks = <&tcsr TCSR_USB2_CLKREF_EN>;
+		clock-names = "ref";
+		status = "okay";
+	};
+
+
+Indentation
+-----------
+
+1. Use indentation according to :ref:`codingstyle`.
+2. For arrays spanning across lines, preferred is to align the continued
+   entries with opening < from first line.
+
+Example::
+
+	thermal-sensor@c271000 {
+		compatible = "qcom,sm8550-tsens", "qcom,tsens-v2";
+		reg = <0x0 0x0c271000 0x0 0x1000>,
+		      <0x0 0x0c222000 0x0 0x1000>;
+	};