Skip to contentSkip to conversation

serial: 8250_of: support an optional bus clock

PATCH Superseded Alex Elder 2 participants 10 messages Apr 11, 2025

Subsystems (3)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Alex Elder

author
#0 lore raw

v3 cover serial: 8250_of: support an optional bus clock

The SpacemiT UART hardware requires a bus clock to be enabled in addition to the primary function clock.

This series makes it possible to specify both clocks via DTS. If a bus clock is required, it and the primary clock are fetched by name.

Changes between v2 and v3:
- A third patch was added to disable the bus clock on suspend and enable it again on resume, as requested by Yixun Lan
- Rob's Reviewed-by tag has been added to patch 1
- The first two patches are otherwise identical to what was in v2

Here is version 2 of this series:
https://lore.kernel.org/lkml/[email protected]/

Changes between v1 and v2:
- The DT binding was fixed to properly define the number of clocks and clock names based on the compatible string, as suggested by Rob Herring
- Logic to look up the optional bus clock was changed as requested by Andy Shevchenko
- The bus clock pointer (which was never used after it was enabled) was renmoved from the of_serial_info structure

Here is the first version of this series:
https://lore.kernel.org/lkml/[email protected]/

-Alex
Alex Elder (3):
dt-bindings: serial: 8250: support an optional second clock
serial: 8250_of: add support for an optional bus clock
serial: 8250_of: manage bus clock in suspend/resume

 .../devicetree/bindings/serial/8250.yaml      | 30 ++++++++++++++++++-
 drivers/tty/serial/8250/8250_of.c             | 13 +++++++-
 2 files changed, 41 insertions(+), 2 deletions(-)

base-commit: 0af2f6be1b4281385b618cb86ad946eded089ac8

Signature · 1 line
2.45.2
author
#1 lore raw

PATCH v3 1/3 dt-bindings: serial: 8250: support an optional second clock

The SpacemiT UART driver requires a bus clock to be enabled in addition to the primary function clock. Add the option to specify two clocks for an 8250-compatible UART, named "core" and "bus". If both are needed, require them to be named.

---
v3: Added Rob's Reviewed-by tag

 .../devicetree/bindings/serial/8250.yaml      | 30 ++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/serial/8250.yaml +29 -1
index dc0d52920575f..33d2016b65090 100644
@@ -135,7 +135,16 @@ properties:
   clock-frequency: true
 
   clocks:
-    maxItems: 1
+    minItems: 1
+    items:
+      - description: The core function clock
+      - description: An optional bus clock
+
+  clock-names:
+    minItems: 1
+    items:
+      - const: core
+      - const: bus
 
   resets:
     maxItems: 1
@@ -224,6 +233,25 @@ required:
   - reg
   - interrupts
 
+if:
+  properties:
+    compatible:
+      contains:
+        const: spacemit,k1-uart
+then:
+  required: [clock-names]
+  properties:
+    clocks:
+      minItems: 2
+    clock-names:
+      minItems: 2
+else:
+  properties:
+    clocks:
+      maxItems: 1
+    clock-names:
+      maxItems: 1
+
 unevaluatedProperties: false
 
 examples:
Signature · 1 line
2.45.2
author
#2 lore raw

PATCH v3 2/3 serial: 8250_of: add support for an optional bus clock

In reply to Alex Elder

The SpacemiT UART requires a bus clock to be enabled, in addition to it's "normal" core clock. Look up the optional bus clock by name, and if that's found, look up the core clock using the name "core".

Supplying a bus clock is optional. If no bus clock is needed, the the first/only clock is used for the core clock.

---
 drivers/tty/serial/8250/8250_of.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
drivers/tty/serial/8250/8250_of.c +10 -1
index 11c860ea80f60..a90a5462aa72a 100644
@@ -123,7 +123,16 @@ static int of_platform_serial_setup(struct platform_device *ofdev,
 
 	/* Get clk rate through clk driver if present */
 	if (!port->uartclk) {
-		info->clk = devm_clk_get_enabled(dev, NULL);
+		struct clk *bus_clk;
+
+		bus_clk = devm_clk_get_optional_enabled(dev, "bus");
+		if (IS_ERR(bus_clk)) {
+			ret = dev_err_probe(dev, PTR_ERR(bus_clk), "failed to get bus clock\n");
+			goto err_pmruntime;
+		}
+
+		/* If the bus clock is required, core clock must be named */
+		info->clk = devm_clk_get_enabled(dev, bus_clk ? "core" : NULL);
 		if (IS_ERR(info->clk)) {
 			ret = dev_err_probe(dev, PTR_ERR(info->clk), "failed to get clock\n");
 			goto err_pmruntime;
Signature · 1 line
2.45.2
Reviewed-by on v3 2/3
#3 lore raw

Re: [PATCH v3 2/3] serial: 8250_of: add support for an optional bus clock

Fri, Apr 11, 2025 at 10:44:17AM -0500, Alex Elder kirjoitti:

Quoted 6 lines

The SpacemiT UART requires a bus clock to be enabled, in addition to it's "normal" core clock. Look up the optional bus clock by name, and if that's found, look up the core clock using the name "core".

Supplying a bus clock is optional. If no bus clock is needed, the the first/only clock is used for the core clock.

Okay, it's slightly different to what we discussed, but at least it looks much better than previous version and acceptable in my point of view.gq

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#4 lore raw

PATCH v3 3/3 serial: 8250_of: manage bus clock in suspend/resume

In reply to Alex Elder

Save the bus clock pointer in the of_serial_info structure, and use that to disable the bus clock on suspend and re-enable it on resume.

---
v3: New patch, managing the bus clock during suspend and resume

 drivers/tty/serial/8250/8250_of.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
drivers/tty/serial/8250/8250_of.c +8 -6
index a90a5462aa72a..8f2529f699e0a 100644
@@ -24,6 +24,7 @@
 
 struct of_serial_info {
 	struct clk *clk;
+	struct clk *bus_clk;
 	struct reset_control *rst;
 	int type;
 	int line;
@@ -123,16 +124,15 @@ static int of_platform_serial_setup(struct platform_device *ofdev,
 
 	/* Get clk rate through clk driver if present */
 	if (!port->uartclk) {
-		struct clk *bus_clk;
-
-		bus_clk = devm_clk_get_optional_enabled(dev, "bus");
-		if (IS_ERR(bus_clk)) {
-			ret = dev_err_probe(dev, PTR_ERR(bus_clk), "failed to get bus clock\n");
+		info->bus_clk = devm_clk_get_optional_enabled(dev, "bus");
+		if (IS_ERR(info->bus_clk)) {
+			ret = dev_err_probe(dev, PTR_ERR(info->bus_clk),
+					    "failed to get bus clock\n");
 			goto err_pmruntime;
 		}
 
 		/* If the bus clock is required, core clock must be named */
-		info->clk = devm_clk_get_enabled(dev, bus_clk ? "core" : NULL);
+		info->clk = devm_clk_get_enabled(dev, info->bus_clk ? "core" : NULL);
 		if (IS_ERR(info->clk)) {
 			ret = dev_err_probe(dev, PTR_ERR(info->clk), "failed to get clock\n");
 			goto err_pmruntime;
@@ -299,6 +299,7 @@ static int of_serial_suspend(struct device *dev)
 	if (!uart_console(port) || console_suspend_enabled) {
 		pm_runtime_put_sync(dev);
 		clk_disable_unprepare(info->clk);
+		clk_disable_unprepare(info->bus_clk);
 	}
 	return 0;
 }
@@ -311,6 +312,7 @@ static int of_serial_resume(struct device *dev)
 
 	if (!uart_console(port) || console_suspend_enabled) {
 		pm_runtime_get_sync(dev);
+		clk_prepare_enable(info->bus_clk);
 		clk_prepare_enable(info->clk);
 	}
 
Signature · 1 line
2.45.2
#5 lore raw

Re: [PATCH v3 3/3] serial: 8250_of: manage bus clock in suspend/resume

Fri, Apr 11, 2025 at 10:44:18AM -0500, Alex Elder kirjoitti:

Quoted 2 lines

Save the bus clock pointer in the of_serial_info structure, and use that to disable the bus clock on suspend and re-enable it on resume.

...

Quoted 8 lines
drivers/tty/serial/8250/8250_of.c +2 -5
 	if (!port->uartclk) {
-		struct clk *bus_clk;
-
-		bus_clk = devm_clk_get_optional_enabled(dev, "bus");
-		if (IS_ERR(bus_clk)) {
-			ret = dev_err_probe(dev, PTR_ERR(bus_clk), "failed to get bus clock\n");
+		info->bus_clk = devm_clk_get_optional_enabled(dev, "bus");
+		if (IS_ERR(info->bus_clk)) {

drivers/tty/serial/8250/8250_of.c static int of_platform_serial_setup(struct platform_device *ofdev, · line 137

Quoted 10 lines
drivers/tty/serial/8250/8250_of.c +3 -1
+			ret = dev_err_probe(dev, PTR_ERR(info->bus_clk),
+					    "failed to get bus clock\n");
 			goto err_pmruntime;
 		}
 
 		/* If the bus clock is required, core clock must be named */
-		info->clk = devm_clk_get_enabled(dev, bus_clk ? "core" : NULL);
+		info->clk = devm_clk_get_enabled(dev, info->bus_clk ? "core" : NULL);
 		if (IS_ERR(info->clk)) {
 			ret = dev_err_probe(dev, PTR_ERR(info->clk), "failed to get clock\n");

While the first patch against this file looks okay now, this one inherits the same problem (seems like not enought thinking about the code representation).

Instead of rewritting half of the lines you just introduced (which is also a bad practice), add a one-liner that assigns a field to the local variable.

Quoted 1 line

goto err_pmruntime;

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#6 lore raw

Re: [PATCH v3 3/3] serial: 8250_of: manage bus clock in suspend/resume

Quoted 28 lines · Andy Shevchenko

Fri, Apr 11, 2025 at 10:44:18AM -0500, Alex Elder kirjoitti:

Quoted 2 lines

Save the bus clock pointer in the of_serial_info structure, and use that to disable the bus clock on suspend and re-enable it on resume.

...

Quoted 8 lines
  	if (!port->uartclk) {
drivers/tty/serial/8250/8250_of.c +2 -5
-		struct clk *bus_clk;
-
-		bus_clk = devm_clk_get_optional_enabled(dev, "bus");
-		if (IS_ERR(bus_clk)) {
-			ret = dev_err_probe(dev, PTR_ERR(bus_clk), "failed to get bus clock\n");
+		info->bus_clk = devm_clk_get_optional_enabled(dev, "bus");
+		if (IS_ERR(info->bus_clk)) {

drivers/tty/serial/8250/8250_of.c static int of_platform_serial_setup(struct platform_device *ofdev, · line 137

Quoted 10 lines
drivers/tty/serial/8250/8250_of.c +3 -1
+			ret = dev_err_probe(dev, PTR_ERR(info->bus_clk),
+					    "failed to get bus clock\n");
  			goto err_pmruntime;
  		}
 
  		/* If the bus clock is required, core clock must be named */
-		info->clk = devm_clk_get_enabled(dev, bus_clk ? "core" : NULL);
+		info->clk = devm_clk_get_enabled(dev, info->bus_clk ? "core" : NULL);
  		if (IS_ERR(info->clk)) {
  			ret = dev_err_probe(dev, PTR_ERR(info->clk), "failed to get clock\n");

While the first patch against this file looks okay now, this one inherits the same problem (seems like not enought thinking about the code representation).

Quoted 2 lines

Instead of rewritting half of the lines you just introduced (which is also a bad practice), add a one-liner that assigns a field to the local variable.

So you want me to re-spin this again so that I use the local variable?

I understand what you're saying based on ease of review, but this is a simple patch and the change is very understandable, and the code is no more or less clear when using the local variable.

-Alex

Quoted 4 lines
Quoted 2 lines

goto err_pmruntime;

#7 lore raw

Re: [PATCH v3 3/3] serial: 8250_of: manage bus clock in suspend/resume

Quoted 4 lines · Alex Elder
Quoted 3 lines · Andy Shevchenko

Fri, Apr 11, 2025 at 10:44:18AM -0500, Alex Elder kirjoitti:

Quoted 2 lines

Save the bus clock pointer in the of_serial_info structure, and use that to disable the bus clock on suspend and re-enable it on resume.

...

Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
     if (!port->uartclk) {
drivers/tty/serial/8250/8250_of.c +5 -6
-            struct clk *bus_clk;
-
-            bus_clk = devm_clk_get_optional_enabled(dev, "bus");
-            if (IS_ERR(bus_clk)) {
-                    ret = dev_err_probe(dev, PTR_ERR(bus_clk), "failed to get bus clock\n");
+            info->bus_clk = devm_clk_get_optional_enabled(dev, "bus");
+            if (IS_ERR(info->bus_clk)) {
+                    ret = dev_err_probe(dev, PTR_ERR(info->bus_clk),
+                                        "failed to get bus clock\n");
                     goto err_pmruntime;
             }
 
             /* If the bus clock is required, core clock must be named */
-            info->clk = devm_clk_get_enabled(dev, bus_clk ? "core" : NULL);
+            info->clk = devm_clk_get_enabled(dev, info->bus_clk ? "core" : NULL);
             if (IS_ERR(info->clk)) {
                     ret = dev_err_probe(dev, PTR_ERR(info->clk), "failed to get clock\n");
Quoted 7 lines
Quoted 5 lines

While the first patch against this file looks okay now, this one inherits the same problem (seems like not enought thinking about the code representation).

Instead of rewritting half of the lines you just introduced (which is also a bad practice), add a one-liner that assigns a field to the local variable.

So you want me to re-spin this again so that I use the local variable?

Yes.

Quoted 1 line

I understand what you're saying based on ease of review,

No, not only review. Here the main issue is ping-pong between patches. If you know ahead that these lines should be changed, do it from the start. But I understand the need to have separate changes for logically different pieces. That's why

Quoted 2 lines

but this
is a simple patch

It doesn't matter how simple it is.

Quoted 2 lines

and the change is very understandable, and the code is no more or less clear when using the local variable.

See above.

Quoted 1 line
Quoted 1 line
Quoted 1 line
                     goto err_pmruntime;
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#8 lore raw

Re: [PATCH v3 3/3] serial: 8250_of: manage bus clock in suspend/resume

Quoted 35 lines · Andy Shevchenko
Quoted 4 lines · Alex Elder
Quoted 3 lines · Andy Shevchenko

Fri, Apr 11, 2025 at 10:44:18AM -0500, Alex Elder kirjoitti:

Quoted 2 lines

Save the bus clock pointer in the of_serial_info structure, and use that to disable the bus clock on suspend and re-enable it on resume.

...

Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
      if (!port->uartclk) {
drivers/tty/serial/8250/8250_of.c +5 -6
-            struct clk *bus_clk;
-
-            bus_clk = devm_clk_get_optional_enabled(dev, "bus");
-            if (IS_ERR(bus_clk)) {
-                    ret = dev_err_probe(dev, PTR_ERR(bus_clk), "failed to get bus clock\n");
+            info->bus_clk = devm_clk_get_optional_enabled(dev, "bus");
+            if (IS_ERR(info->bus_clk)) {
+                    ret = dev_err_probe(dev, PTR_ERR(info->bus_clk),
+                                        "failed to get bus clock\n");
                      goto err_pmruntime;
              }
 
              /* If the bus clock is required, core clock must be named */
-            info->clk = devm_clk_get_enabled(dev, bus_clk ? "core" : NULL);
+            info->clk = devm_clk_get_enabled(dev, info->bus_clk ? "core" : NULL);
              if (IS_ERR(info->clk)) {
                      ret = dev_err_probe(dev, PTR_ERR(info->clk), "failed to get clock\n");
Quoted 7 lines
Quoted 5 lines

While the first patch against this file looks okay now, this one inherits the same problem (seems like not enought thinking about the code representation).

Instead of rewritting half of the lines you just introduced (which is also a bad practice), add a one-liner that assigns a field to the local variable.

So you want me to re-spin this again so that I use the local variable?

Quoted 8 lines

Yes.

Quoted 1 line

I understand what you're saying based on ease of review,

No, not only review. Here the main issue is ping-pong between patches. If you know ahead that these lines should be changed, do it from the start. But I understand the need to have separate changes for logically different pieces. That's why

I did do it from the start (v1 of the series). Then I reworked it based on your feedback and concluded there was no need for keeping a copy of the bus clock in the of_serial_info structure. In the interest of keeping the code simple I dropped it.

Yixun asked me to disable/enable the bus clock in suspend/resume, so I was preparing the patches for that, when Greg informed me he had already accepted the first two patches. So I added this new feature on the end. This is the reason for this "ping pong." I would have done it from the start in this series otherwise in v3.

Anyway, I'm sending v4 shortly. I just have to run it through another test. It is indeed a much simpler change.

-Alex

Quoted 13 lines
Quoted 2 lines

but this
is a simple patch

It doesn't matter how simple it is.

Quoted 2 lines

and the change is very understandable, and the code is no more or less clear when using the local variable.

See above.

Quoted 1 line
Quoted 1 line
Quoted 1 line
                      goto err_pmruntime;
#9 lore raw

Re: [PATCH v3 3/3] serial: 8250_of: manage bus clock in suspend/resume

Quoted 1 line · Alex Elder

On 4/11/25 2:44 PM, Andy Shevchenko wrote:

...

Quoted 2 lines

when Greg informed me
he had already accepted the first two patches.

Indeed. Just send a last patch in a simplified form as I suggested here.

Signature · 2 lines
With Best Regards,
Andy Shevchenko