[PATCH v3] docs: dt-bindings: add DTS Coding Style document

2023-11-25 Thread Krzysztof Kozlowski
Document preferred coding style for Devicetree sources (DTS and DTSI),
to bring consistency among all (sub)architectures and ease in reviews.

Cc: Andrew Davis 
cc: Andrew Lunn 
Cc: AngeloGioacchino Del Regno 
Cc: Arnd Bergmann 
Cc: Bjorn Andersson 
Cc: Chen-Yu Tsai 
Cc: Dmitry Baryshkov 
Cc: Geert Uytterhoeven 
Cc: Heiko Stuebner 
Cc: Jonathan Corbet 
Cc: Konrad Dybcio 
Cc: Matthias Brugger 
Cc: Michal Simek 
Cc: Neil Armstrong 
Cc: Nishanth Menon 
Cc: Olof Johansson 
Cc: Rafał Miłecki 
Acked-by: Neil Armstrong 
Acked-by: Heiko Stuebner 
Signed-off-by: Krzysztof Kozlowski 

---

Merging idea: Rob/DT bindings

Changes in v3
=
1. should->shall (Angelo)
2. Comments // -> /* (Angelo, Michal)
3. Use imaginary example in "Order of Properties in Device Node"
   (Angelo)
4. Added paragraphs for three sections with justifications of chosen
   style.
5. Allow two style of ordering overrides in board DTS: alphabetically or
   by order of DTSI (Rob).
6. I did not incorporate feedback about, due to lack of consensus and my
   disagreement:
   a. SoM being DTS without DTSI in "Organizing DTSI and DTS"

Changes in v2
=
1. Hopefully incorporate entire feedback from comments:
a. Fix \ { => / { (Rob)
b. Name: dts-coding-style (Rob)
c. Exceptions for ordering nodes by name for Renesas and pinctrl (Geert,
   Konrad)
d. Ordering properties by common/vendor (Rob)
e. Array entries in <> (Rob)

2. New chapter: Organizing DTSI and DTS

3. Several grammar fixes (missing articles)

Cc: linux-rockc...@lists.infradead.org
Cc: linux-media...@lists.infradead.org
Cc: linux-samsung-...@vger.kernel.org
Cc: linux-amlo...@lists.infradead.org
Cc: linux-arm-ker...@lists.infradead.org
Cc: linux-arm-...@vger.kernel.org
Cc: workfl...@vger.kernel.org
Cc: linux-doc@vger.kernel.org
---
 .../devicetree/bindings/dts-coding-style.rst  | 194 ++
 Documentation/devicetree/bindings/index.rst   |   1 +
 2 files changed, 195 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/dts-coding-style.rst

diff --git a/Documentation/devicetree/bindings/dts-coding-style.rst 
b/Documentation/devicetree/bindings/dts-coding-style.rst
new file mode 100644
index ..e374bec0f555
--- /dev/null
+++ b/Documentation/devicetree/bindings/dts-coding-style.rst
@@ -0,0 +1,194 @@
+.. SPDX-License-Identifier: GPL-2.0
+.. _dtscodingstyle:
+
+=
+Devicetree Sources (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
+---
+
+Devicetree specification allows broader range of characters in node and
+property names, but for code readability the choice shall be narrowed.
+
+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 shall use lowercase hex, without leading zeros (padding).
+
+4. Hex values in properties, e.g. "reg", shall use lowercase hex.  The address
+   part can be padded with leading zeros.
+
+Example::
+
+   gpi_dma2: dma-controller@80 {
+   compatible = "qcom,sm8550-gpi-dma", "qcom,sm6350-gpi-dma";
+   reg = <0x0 0x0080 0x0 0x6>;
+   }
+
+Order of Nodes
+--
+
+1. Nodes within any bus, thus using unit addresses for children, shall be
+   ordered incrementally by unit address.
+   Alternatively for some sub-architectures, nodes of the same type can be
+   grouped together (e.g. all I2C controllers one after another even if this
+   breaks unit address ordering).
+
+2. Nodes without unit addresses shall be ordered alpha-numerically by the node
+   name.  For a few types of nodes, they can be ordered by the main property
+   (e.g. pin configuration states ordered by value of "pins" property).
+
+3. When extending nodes in the board DTS via &label, the entries shall be
+   ordered either alpha-numerically or by keeping the order from DTSI (choice
+   depending on sub-architecture).
+
+Above ordering rules are easy to enforce during review, reduce chances of
+conflicts for simultaneous additions (new nodes) to a file and help in
+navigating through the DTS source.
+
+Example::
+
+   /* SoC DTSI */
+
+   / {
+   cpus {
+   /* ... */
+   };
+
+   psci {
+   /* ... */
+   };
+
+   soc@ {
+   dma: dma-controller@1 {
+   /* ... */
+   };
+
+   clk: clo

Re: [PATCH v3] docs: dt-bindings: add DTS Coding Style document

2023-11-25 Thread Jonathan Corbet
Krzysztof Kozlowski  writes:

> Document preferred coding style for Devicetree sources (DTS and DTSI),
> to bring consistency among all (sub)architectures and ease in reviews.

One little nit:

> diff --git a/Documentation/devicetree/bindings/dts-coding-style.rst 
> b/Documentation/devicetree/bindings/dts-coding-style.rst
> new file mode 100644
> index ..e374bec0f555
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/dts-coding-style.rst
> @@ -0,0 +1,194 @@
> +.. SPDX-License-Identifier: GPL-2.0
> +.. _dtscodingstyle:

There is no need to put a label at the top of a document like that, I'd
just take it out.

Thanks,

jon



Re: [PATCH v3] docs: dt-bindings: add DTS Coding Style document

2023-11-25 Thread Laurent Pinchart
Hi Krzysztof,

Thank you for the patch.

On Sat, Nov 25, 2023 at 07:44:22PM +0100, 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: Andrew Davis 
> cc: Andrew Lunn 
> Cc: AngeloGioacchino Del Regno 
> Cc: Arnd Bergmann 
> Cc: Bjorn Andersson 
> Cc: Chen-Yu Tsai 
> Cc: Dmitry Baryshkov 
> Cc: Geert Uytterhoeven 
> Cc: Heiko Stuebner 
> Cc: Jonathan Corbet 
> Cc: Konrad Dybcio 
> Cc: Matthias Brugger 
> Cc: Michal Simek 
> Cc: Neil Armstrong 
> Cc: Nishanth Menon 
> Cc: Olof Johansson 
> Cc: Rafał Miłecki 
> Acked-by: Neil Armstrong 
> Acked-by: Heiko Stuebner 
> Signed-off-by: Krzysztof Kozlowski 
> 
> ---
> 
> Merging idea: Rob/DT bindings
> 
> Changes in v3
> =
> 1. should->shall (Angelo)
> 2. Comments // -> /* (Angelo, Michal)
> 3. Use imaginary example in "Order of Properties in Device Node"
>(Angelo)
> 4. Added paragraphs for three sections with justifications of chosen
>style.
> 5. Allow two style of ordering overrides in board DTS: alphabetically or
>by order of DTSI (Rob).
> 6. I did not incorporate feedback about, due to lack of consensus and my
>disagreement:
>a. SoM being DTS without DTSI in "Organizing DTSI and DTS"
> 
> Changes in v2
> =
> 1. Hopefully incorporate entire feedback from comments:
> a. Fix \ { => / { (Rob)
> b. Name: dts-coding-style (Rob)
> c. Exceptions for ordering nodes by name for Renesas and pinctrl (Geert,
>Konrad)
> d. Ordering properties by common/vendor (Rob)
> e. Array entries in <> (Rob)
> 
> 2. New chapter: Organizing DTSI and DTS
> 
> 3. Several grammar fixes (missing articles)
> 
> Cc: linux-rockc...@lists.infradead.org
> Cc: linux-media...@lists.infradead.org
> Cc: linux-samsung-...@vger.kernel.org
> Cc: linux-amlo...@lists.infradead.org
> Cc: linux-arm-ker...@lists.infradead.org
> Cc: linux-arm-...@vger.kernel.org
> Cc: workfl...@vger.kernel.org
> Cc: linux-doc@vger.kernel.org
> ---
>  .../devicetree/bindings/dts-coding-style.rst  | 194 ++
>  Documentation/devicetree/bindings/index.rst   |   1 +
>  2 files changed, 195 insertions(+)
>  create mode 100644 Documentation/devicetree/bindings/dts-coding-style.rst
> 
> diff --git a/Documentation/devicetree/bindings/dts-coding-style.rst 
> b/Documentation/devicetree/bindings/dts-coding-style.rst
> new file mode 100644
> index ..e374bec0f555
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/dts-coding-style.rst
> @@ -0,0 +1,194 @@
> +.. SPDX-License-Identifier: GPL-2.0
> +.. _dtscodingstyle:
> +
> +=
> +Devicetree Sources (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
> +---
> +
> +Devicetree specification allows broader range of characters in node and

s/Devicetree specification/The Devicetree specification/
s/broader range/a broad range/

> +property names, but for code readability the choice shall be narrowed.
> +
> +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 shall use lowercase hex, without leading zeros (padding).

I'm curious, what's the reason for this ? I think it makes the sources
less readable. If the rule is "just" because that's how DT sources are
written today and it would be too complicated to change that, that's
fine with me.

> +
> +4. Hex values in properties, e.g. "reg", shall use lowercase hex.  The 
> address
> +   part can be padded with leading zeros.
> +
> +Example::
> +
> + gpi_dma2: dma-controller@80 {
> + compatible = "qcom,sm8550-gpi-dma", "qcom,sm6350-gpi-dma";
> + reg = <0x0 0x0080 0x0 0x6>;
> + }
> +
> +Order of Nodes
> +--
> +
> +1. Nodes within any bus, thus using unit addresses for children, shall be
> +   ordered incrementally by unit address.
> +   Alternatively for some sub-architectures, nodes of the same type can be
> +   grouped together (e.g. all I2C controllers one after another even if this
> +   breaks unit address ordering).
> +
> +2. Nodes without unit addresses shall be ordered alpha-numerically by the 
> node
> +   name.  For a few types of nodes, they can be ordered by the main property
> +   (e.g. pin configuration states ordered by value of "pins" property).
> +
> +3. When extending nodes in the board DTS via &label, the en

Re: [PATCH v3] docs: dt-bindings: add DTS Coding Style document

2023-11-25 Thread Andrew Lunn
> +=
> +Devicetree Sources (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.

It would be nice to add a pointer where such rules are documented.

> +Naming and Valid Characters
> +---
> +
> +Devicetree specification allows broader range of characters in node and
> +property names, but for code readability the choice shall be narrowed.
> +
> +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 shall use lowercase hex, without leading zeros (padding).
> +
> +4. Hex values in properties, e.g. "reg", shall use lowercase hex.  The 
> address
> +   part can be padded with leading zeros.
> +
> +Example::
> +
> + gpi_dma2: dma-controller@80 {

Not the best of example. Upper case 8 does not exist, as far as i
known. 

> + compatible = "qcom,sm8550-gpi-dma", "qcom,sm6350-gpi-dma";
> + reg = <0x0 0x0080 0x0 0x6>;

Maybe introduce some [a-f] in the example reg?

> +Order of Nodes
> +--
> +
> +1. Nodes within any bus, thus using unit addresses for children, shall be
> +   ordered incrementally by unit address.
> +   Alternatively for some sub-architectures, nodes of the same type can be
> +   grouped together (e.g. all I2C controllers one after another even if this
> +   breaks unit address ordering).
> +
> +2. Nodes without unit addresses shall be ordered alpha-numerically by the 
> node
> +   name.  For a few types of nodes, they can be ordered by the main property
> +   (e.g. pin configuration states ordered by value of "pins" property).
> +
> +3. When extending nodes in the board DTS via &label, the entries shall be
> +   ordered either alpha-numerically or by keeping the order from DTSI (choice
> +   depending on sub-architecture).

Are these sub-architecture choices documented somewhere? Can you
include a hint which they are?

> +Example::
> +
> + /* SoC DTSI */
> +
> + / {

Dumb question. Does this open { indicate the start of a bus?

> + cpus {
> + /* ... */
> + };
> +
> + psci {
> + /* ... */
> + };

If that does indicate a bus, the nodes above are ordered
alpha-numerically, according to 2).

> +
> + soc@ {

This has a unit address, even if its missing, so should be sorted by
1).

Should there be something in the coding style that 2) comes before 1)
on the bus? And if that is true, don't you think it would make sense
to swap 1) and 2) in the description above?

> + dma: dma-controller@1 {
> + /* ... */
> + };
> +
> + clk: clock-controller@8 {
> + /* ... */
> + };
> + };
> + };
> +
> + /* Board DTS - alphabetical order */
> +
> + &clk {
> + /* ... */
> + };
> +
> + &dma {
> + /* ... */
> + };
> +
> + /* Board DTS - alternative order, keep as DTSI */
> +
> + &dma {
> + /* ... */
> + };
> +
> + &clk {
> + /* ... */
> + };

Do you imaging there will ever be a checkpatch for DT files? The
second alternative seems pretty difficult to check for with tools. You
need to include all the .dtsi files to determine the ordered tree,
then flatten it to get the properties order. Should we discourage this
alternative?

> +Indentation
> +---
> +
> +1. Use indentation according to :ref:`codingstyle`.
> +2. For arrays spanning across lines, it is preferred to align the continued
> +   entries with opening < from the first line.
> +3. Each entry in arrays with multiple cells (e.g. "reg" with two IO 
> addresses)
> +   shall be enclosed in <>.
> +
> +Example::
> +
> + thermal-sensor@c271000 {
> + compatible = "qcom,sm8550-tsens", "qcom,tsens-v2";
> + reg = <0x0 0x0c271000 0x0 0x1000>,
> +   <0x0 0x0c222000 0x0 0x1000>;
> + };

I'm not sure i understand this. Is this example correct?

gpio-fan,speed-map = <00
  3000 1
  6000 2>;

It exists a lot in todays files.


> +The DTSI and DTS files shall be organized in a way representing the common
> +(and re-usable) parts of the hardware.  Typically this means organizing DTSI
> +and DTS files into several fil

Re: [PATCH v3] docs: dt-bindings: add DTS Coding Style document

2023-11-25 Thread Konrad Dybcio
[...]
>> +
>> +3. Unit addresses shall use lowercase hex, without leading zeros (padding).
> 
> I'm curious, what's the reason for this ? I think it makes the sources
> less readable. If the rule is "just" because that's how DT sources are
> written today and it would be too complicated to change that, that's
> fine with me.
One more thing not mentioned is "no 0x prefix" (the unit address is *always*
interpreted as hex).

Lowercase hex seems to be (in my experience?) the consensus for everything
except preprocessor defines across the spectrum

No leading zeroes.. I guess it was just eye-pleasing for people that have
been doing devicetree to date, myself included.

Konrad



Re: [PATCH v3] docs: dt-bindings: add DTS Coding Style document

2023-11-25 Thread Konrad Dybcio
On 25.11.2023 19:44, 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: Andrew Davis 
> cc: Andrew Lunn 
> Cc: AngeloGioacchino Del Regno 
> Cc: Arnd Bergmann 
> Cc: Bjorn Andersson 
> Cc: Chen-Yu Tsai 
> Cc: Dmitry Baryshkov 
> Cc: Geert Uytterhoeven 
> Cc: Heiko Stuebner 
> Cc: Jonathan Corbet 
> Cc: Konrad Dybcio 
> Cc: Matthias Brugger 
> Cc: Michal Simek 
> Cc: Neil Armstrong 
> Cc: Nishanth Menon 
> Cc: Olof Johansson 
> Cc: Rafał Miłecki 
> Acked-by: Neil Armstrong 
> Acked-by: Heiko Stuebner 
> Signed-off-by: Krzysztof Kozlowski 
> 
> ---
There's still a couple of comments being resolved, but even as-is, I
agree with the contents and want to thank you for doing this.

Acked-by: Konrad Dybcio 

Konrad