Skip to contentSkip to conversation

Fix clk-gpio driver

PATCH Superseded Svyatoslav Ryhel 2 participants 7 messages Nov 17, 2023 – Dec 15, 2023 Common clk

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover Fix clk-gpio driver

Existing gpio-gate-clock driver acts like a simple GPIO switch without any effect on gated clock. Add actual clock actions into enable/disable ops and implement get_rate op by passing gated clock if it is enabled.

Testing current driver implementation shows that it is not fully capable and lacks gated clock manipulations.

Svyatoslav Ryhel (1):
clk: clk-gpio: add actual gated clock

 drivers/clk/clk-gpio.c | 47 +++++++++++++++++++++++++++++++++++-------
 1 file changed, 40 insertions(+), 7 deletions(-)
Signature · 1 line
2.40.1
#1 lore raw

PATCH 1/1 clk: clk-gpio: add actual gated clock

Existing gpio-gate-clock driver acts like a simple GPIO switch without any effect on gated clock. Add actual clock actions into enable/disable ops and implement get_rate op by passing gated clock if it is enabled.

---
 drivers/clk/clk-gpio.c | 47 +++++++++++++++++++++++++++++++++++-------
 1 file changed, 40 insertions(+), 7 deletions(-)
drivers/clk/clk-gpio.c +40 -7
index 26d795b978..a55470cfbf 100644
@@ -3,19 +3,23 @@
  * Copyright (C) 2023 Marek Vasut <[email protected]>
  */
 
-#include <asm/gpio.h>
-#include <common.h>
-#include <clk-uclass.h>
+#include <clk.h>
 #include <dm.h>
+#include <clk-uclass.h>
+#include <linux/clk-provider.h>
+
+#include <asm/gpio.h>
 
 struct clk_gpio_priv {
 	struct gpio_desc	enable;
+	struct clk		*clk;
 };
 
 static int clk_gpio_enable(struct clk *clk)
 {
 	struct clk_gpio_priv *priv = dev_get_priv(clk->dev);
 
+	clk_prepare_enable(priv->clk);
 	dm_gpio_set_value(&priv->enable, 1);
 
 	return 0;
@@ -26,21 +30,50 @@ static int clk_gpio_disable(struct clk *clk)
 	struct clk_gpio_priv *priv = dev_get_priv(clk->dev);
 
 	dm_gpio_set_value(&priv->enable, 0);
+	clk_disable_unprepare(priv->clk);
 
 	return 0;
 }
 
+static ulong clk_gpio_get_rate(struct clk *clk)
+{
+	struct clk_gpio_priv *priv = dev_get_priv(clk->dev);
+	int ret;
+
+	ret = dm_gpio_get_value(&priv->enable);
+	if (ret)
+		return clk_get_rate(priv->clk);
+	else
+		return -EINVAL;
+}
+
 const struct clk_ops clk_gpio_ops = {
 	.enable		= clk_gpio_enable,
 	.disable	= clk_gpio_disable,
+	.get_rate	= clk_gpio_get_rate,
 };
 
-static int clk_gpio_probe(struct udevice *dev)
+static int clk_gpio_of_to_plat(struct udevice *dev)
 {
 	struct clk_gpio_priv *priv = dev_get_priv(dev);
+	int ret;
 
-	return gpio_request_by_name(dev, "enable-gpios", 0,
-				    &priv->enable, GPIOD_IS_OUT);
+	priv->clk = devm_clk_get(dev, NULL);
+	if (IS_ERR(priv->clk)) {
+		log_err("%s: Could not get gated clock: %ld\n",
+			__func__, PTR_ERR(priv->clk));
+		return PTR_ERR(priv->clk);
+	}
+
+	ret = gpio_request_by_name(dev, "enable-gpios", 0,
+				   &priv->enable, GPIOD_IS_OUT);
+	if (ret) {
+		log_err("%s: Could not decode enable-gpios (%d)\n",
+			__func__, ret);
+		return ret;
+	}
+
+	return 0;
 }
 
 /*
@@ -59,7 +92,7 @@ U_BOOT_DRIVER(gpio_gate_clock) = {
 	.name		= "gpio_clock",
 	.id		= UCLASS_CLK,
 	.of_match	= clk_gpio_match,
-	.probe		= clk_gpio_probe,
+	.of_to_plat	= clk_gpio_of_to_plat,
 	.priv_auto	= sizeof(struct clk_gpio_priv),
 	.ops		= &clk_gpio_ops,
 	.flags		= DM_FLAG_PRE_RELOC,
Signature · 1 line
2.40.1
#2 lore raw

Re: [PATCH v1 1/1] clk: clk-gpio: add actual gated clock

Quoted 20 lines · Svyatoslav Ryhel

Existing gpio-gate-clock driver acts like a simple GPIO switch without any effect on gated clock. Add actual clock actions into enable/disable ops and implement get_rate op by passing gated clock if it is enabled.

---
  drivers/clk/clk-gpio.c | 47 +++++++++++++++++++++++++++++++++++-------
  1 file changed, 40 insertions(+), 7 deletions(-)
drivers/clk/clk-gpio.c +0 -3
index 26d795b978..a55470cfbf 100644
@@ -3,19 +3,23 @@
   * Copyright (C) 2023 Marek Vasut <[email protected]>
   */
 
-#include <asm/gpio.h>
-#include <common.h>
-#include <clk-uclass.h>

drivers/clk/clk-gpio.c line 15

Quoted 10 lines
drivers/clk/clk-gpio.c +6 -0
+#include <clk.h>
  #include <dm.h>
+#include <clk-uclass.h>
+#include <linux/clk-provider.h>
+
+#include <asm/gpio.h>
 
  struct clk_gpio_priv {
  	struct gpio_desc	enable;
+	struct clk		*clk;

Please name this "parent".

drivers/clk/clk-gpio.c line 22

Quoted 7 lines
drivers/clk/clk-gpio.c +1 -0
  };
 
  static int clk_gpio_enable(struct clk *clk)
  {
  	struct clk_gpio_priv *priv = dev_get_priv(clk->dev);
 
+	clk_prepare_enable(priv->clk);

Just clk_enable is fine. We don't have a separate prepare stage line Linux.

Quoted 12 lines
drivers/clk/clk-gpio.c +1 -0
  	dm_gpio_set_value(&priv->enable, 1);
 
  	return 0;
@@ -26,21 +30,50 @@ static int clk_gpio_disable(struct clk *clk)
  	struct clk_gpio_priv *priv = dev_get_priv(clk->dev);
 
  	dm_gpio_set_value(&priv->enable, 0);
+	clk_disable_unprepare(priv->clk);
 
  	return 0;
  }
 

drivers/clk/clk-gpio.c static int clk_gpio_disable(struct clk *clk) · line 47

Quoted 10 lines
drivers/clk/clk-gpio.c +10 -0
+static ulong clk_gpio_get_rate(struct clk *clk)
+{
+	struct clk_gpio_priv *priv = dev_get_priv(clk->dev);
+	int ret;
+
+	ret = dm_gpio_get_value(&priv->enable);
+	if (ret)
+		return clk_get_rate(priv->clk);
+	else
+		return -EINVAL;

This should always return the parent rate.

drivers/clk/clk-gpio.c static int clk_gpio_disable(struct clk *clk) · line 56

Quoted 10 lines
drivers/clk/clk-gpio.c +4 -1
+}
+
  const struct clk_ops clk_gpio_ops = {
  	.enable		= clk_gpio_enable,
  	.disable	= clk_gpio_disable,
+	.get_rate	= clk_gpio_get_rate,
  };
 
-static int clk_gpio_probe(struct udevice *dev)
+static int clk_gpio_of_to_plat(struct udevice *dev)

This change should be a separate commit.

Quoted 8 lines
drivers/clk/clk-gpio.c +3 -2
  {
  	struct clk_gpio_priv *priv = dev_get_priv(dev);
+	int ret;
 
-	return gpio_request_by_name(dev, "enable-gpios", 0,
-				    &priv->enable, GPIOD_IS_OUT);
+	priv->clk = devm_clk_get(dev, NULL);
+	if (IS_ERR(priv->clk)) {

drivers/clk/clk-gpio.c static int clk_gpio_disable(struct clk *clk) · line 72

Quoted 10 lines
drivers/clk/clk-gpio.c +10 -0
+		log_err("%s: Could not get gated clock: %ld\n",
+			__func__, PTR_ERR(priv->clk));
+		return PTR_ERR(priv->clk);
+	}
+
+	ret = gpio_request_by_name(dev, "enable-gpios", 0,
+				   &priv->enable, GPIOD_IS_OUT);
+	if (ret) {
+		log_err("%s: Could not decode enable-gpios (%d)\n",
+			__func__, ret);

Use dev_dbg for probe errors to reduce binary size.

Quoted 8 lines
drivers/clk/clk-gpio.c +4 -0
+		return ret;
+	}
+
+	return 0;
  }
 
  /*

drivers/clk/clk-gpio.c U_BOOT_DRIVER(gpio_gate_clock) = { · line 98

Quoted 8 lines
drivers/clk/clk-gpio.c +1 -1
@@ -59,7 +92,7 @@ U_BOOT_DRIVER(gpio_gate_clock) = {
  	.name		= "gpio_clock",
  	.id		= UCLASS_CLK,
  	.of_match	= clk_gpio_match,
-	.probe		= clk_gpio_probe,
+	.of_to_plat	= clk_gpio_of_to_plat,
  	.priv_auto	= sizeof(struct clk_gpio_priv),
  	.ops		= &clk_gpio_ops,
  	.flags		= DM_FLAG_PRE_RELOC,

--Sean

#3 lore raw

Re: [PATCH v1 1/1] clk: clk-gpio: add actual gated clock

пт, 15 груд. 2023 р. о 19:25 Sean Anderson <[email protected]> пише:

Quoted 29 lines · Svyatoslav Ryhel
Quoted 27 lines

Existing gpio-gate-clock driver acts like a simple GPIO switch without any effect on gated clock. Add actual clock actions into enable/disable ops and implement get_rate op by passing gated clock if it is enabled.

---
  drivers/clk/clk-gpio.c | 47 +++++++++++++++++++++++++++++++++++-------
  1 file changed, 40 insertions(+), 7 deletions(-)
drivers/clk/clk-gpio.c +5 -3
index 26d795b978..a55470cfbf 100644
@@ -3,19 +3,23 @@
   * Copyright (C) 2023 Marek Vasut <[email protected]>
   */
 
-#include <asm/gpio.h>
-#include <common.h>
-#include <clk-uclass.h>
+#include <clk.h>
  #include <dm.h>
+#include <clk-uclass.h>
+#include <linux/clk-provider.h>
+
+#include <asm/gpio.h>
 
Quoted 6 lines

drivers/clk/clk-gpio.c line 15

Quoted 3 lines
drivers/clk/clk-gpio.c +1 -0
  struct clk_gpio_priv {
      struct gpio_desc        enable;
+     struct clk              *clk;

Please name this "parent".

I see no need in this since clk is generic name and this driver can accept only one clock with any name.

Quoted 10 lines

drivers/clk/clk-gpio.c line 22

Quoted 7 lines
drivers/clk/clk-gpio.c +1 -0
  };
 
  static int clk_gpio_enable(struct clk *clk)
  {
      struct clk_gpio_priv *priv = dev_get_priv(clk->dev);
 
+     clk_prepare_enable(priv->clk);

Just clk_enable is fine. We don't have a separate prepare stage line Linux.

U-Boot supports clk_prepare_enable and since this support is present I see no contradiction in using it.

Quoted 12 lines
Quoted 12 lines
drivers/clk/clk-gpio.c +1 -0
      dm_gpio_set_value(&priv->enable, 1);
 
      return 0;
@@ -26,21 +30,50 @@ static int clk_gpio_disable(struct clk *clk)
      struct clk_gpio_priv *priv = dev_get_priv(clk->dev);
 
      dm_gpio_set_value(&priv->enable, 0);
+     clk_disable_unprepare(priv->clk);
 
      return 0;
  }
 
Quoted 13 lines

drivers/clk/clk-gpio.c static int clk_gpio_disable(struct clk *clk) · line 47

Quoted 10 lines
drivers/clk/clk-gpio.c +10 -0
+static ulong clk_gpio_get_rate(struct clk *clk)
+{
+     struct clk_gpio_priv *priv = dev_get_priv(clk->dev);
+     int ret;
+
+     ret = dm_gpio_get_value(&priv->enable);
+     if (ret)
+             return clk_get_rate(priv->clk);
+     else
+             return -EINVAL;

This should always return the parent rate.

How can you return clock rate if the gate is disabled?

Quoted 8 lines
Quoted 8 lines
drivers/clk/clk-gpio.c +3 -0
+}
+
  const struct clk_ops clk_gpio_ops = {
      .enable         = clk_gpio_enable,
      .disable        = clk_gpio_disable,
+     .get_rate       = clk_gpio_get_rate,
  };
 
Quoted 5 lines

drivers/clk/clk-gpio.c static int clk_gpio_disable(struct clk *clk) · line 56

Quoted 2 lines
drivers/clk/clk-gpio.c +1 -1
-static int clk_gpio_probe(struct udevice *dev)
+static int clk_gpio_of_to_plat(struct udevice *dev)

This change should be a separate commit.

This actually should not be accepted first place

.of_to_plat - convert device tree data to plat
.probe - make a device ready for use

https://github.com/u-boot/u-boot/blob/master/doc/develop/driver-model/design.rst

Quoted 19 lines
Quoted 8 lines
drivers/clk/clk-gpio.c +3 -2
  {
      struct clk_gpio_priv *priv = dev_get_priv(dev);
+     int ret;
 
-     return gpio_request_by_name(dev, "enable-gpios", 0,
-                                 &priv->enable, GPIOD_IS_OUT);
+     priv->clk = devm_clk_get(dev, NULL);
+     if (IS_ERR(priv->clk)) {

drivers/clk/clk-gpio.c static int clk_gpio_disable(struct clk *clk) · line 72

Quoted 10 lines
drivers/clk/clk-gpio.c +10 -0
+             log_err("%s: Could not get gated clock: %ld\n",
+                     __func__, PTR_ERR(priv->clk));
+             return PTR_ERR(priv->clk);
+     }
+
+     ret = gpio_request_by_name(dev, "enable-gpios", 0,
+                                &priv->enable, GPIOD_IS_OUT);
+     if (ret) {
+             log_err("%s: Could not decode enable-gpios (%d)\n",
+                     __func__, ret);
Quoted 2 lines

Use dev_dbg for probe errors to reduce binary size.

These errors provide intel about probing 2 key parts of the driver. How would you know it fails in case you have no devices which face this error? Iis there any convention or restriction which prohibits this? If yes, then provide a document please.

All other general clock drivers use log_err/dev_err versions btw.

Quoted 18 lines
Quoted 8 lines
drivers/clk/clk-gpio.c +4 -0
+             return ret;
+     }
+
+     return 0;
  }
 
  /*

drivers/clk/clk-gpio.c U_BOOT_DRIVER(gpio_gate_clock) = { · line 98

Quoted 8 lines
drivers/clk/clk-gpio.c +1 -1
@@ -59,7 +92,7 @@ U_BOOT_DRIVER(gpio_gate_clock) = {
      .name           = "gpio_clock",
      .id             = UCLASS_CLK,
      .of_match       = clk_gpio_match,
-     .probe          = clk_gpio_probe,
+     .of_to_plat     = clk_gpio_of_to_plat,
      .priv_auto      = sizeof(struct clk_gpio_priv),
      .ops            = &clk_gpio_ops,
      .flags          = DM_FLAG_PRE_RELOC,

--Sean

#4 lore raw

Re: [PATCH v1 1/1] clk: clk-gpio: add actual gated clock

Quoted 30 lines · Svyatoslav Ryhel

пт, 15 груд. 2023 р. о 19:25 Sean Anderson <[email protected]> пише:

Quoted 29 lines · Svyatoslav Ryhel
Quoted 27 lines

Existing gpio-gate-clock driver acts like a simple GPIO switch without any effect on gated clock. Add actual clock actions into enable/disable ops and implement get_rate op by passing gated clock if it is enabled.

---
   drivers/clk/clk-gpio.c | 47 +++++++++++++++++++++++++++++++++++-------
   1 file changed, 40 insertions(+), 7 deletions(-)
drivers/clk/clk-gpio.c +5 -3
index 26d795b978..a55470cfbf 100644
@@ -3,19 +3,23 @@
    * Copyright (C) 2023 Marek Vasut <[email protected]>
    */
 
-#include <asm/gpio.h>
-#include <common.h>
-#include <clk-uclass.h>
+#include <clk.h>
   #include <dm.h>
+#include <clk-uclass.h>
+#include <linux/clk-provider.h>
+
+#include <asm/gpio.h>
 
Quoted 8 lines
Quoted 6 lines

drivers/clk/clk-gpio.c line 15

Quoted 3 lines
drivers/clk/clk-gpio.c +1 -0
   struct clk_gpio_priv {
       struct gpio_desc        enable;
+     struct clk              *clk;

Please name this "parent".

I see no need in this since clk is generic name and this driver can accept only one clock with any name.

It's good documentation. It makes it clear what the purpose of the member is.

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
drivers/clk/clk-gpio.c +0 -0
   };
 
   static int clk_gpio_enable(struct clk *clk)
   {
       struct clk_gpio_priv *priv = dev_get_priv(clk->dev);
 
Quoted 6 lines
Quoted 4 lines

drivers/clk/clk-gpio.c line 22

Quoted 1 line
drivers/clk/clk-gpio.c +1 -0
+     clk_prepare_enable(priv->clk);

Just clk_enable is fine. We don't have a separate prepare stage line Linux.

U-Boot supports clk_prepare_enable and since this support is present I see no contradiction in using it.

These functions are only there for to make it easier to port code from Linux. Since this code was written for U-Boot, just use the actual function.

Quoted 25 lines
Quoted 12 lines
Quoted 12 lines
drivers/clk/clk-gpio.c +1 -0
       dm_gpio_set_value(&priv->enable, 1);
 
       return 0;
@@ -26,21 +30,50 @@ static int clk_gpio_disable(struct clk *clk)
       struct clk_gpio_priv *priv = dev_get_priv(clk->dev);
 
       dm_gpio_set_value(&priv->enable, 0);
+     clk_disable_unprepare(priv->clk);
 
       return 0;
   }
 
Quoted 13 lines

drivers/clk/clk-gpio.c static int clk_gpio_disable(struct clk *clk) · line 47

Quoted 10 lines
drivers/clk/clk-gpio.c +10 -0
+static ulong clk_gpio_get_rate(struct clk *clk)
+{
+     struct clk_gpio_priv *priv = dev_get_priv(clk->dev);
+     int ret;
+
+     ret = dm_gpio_get_value(&priv->enable);
+     if (ret)
+             return clk_get_rate(priv->clk);
+     else
+             return -EINVAL;

This should always return the parent rate.

Quoted 1 line

How can you return clock rate if the gate is disabled?

This allows consumers to know what the rate will be before they enable the clock.

Quoted 13 lines
Quoted 8 lines
Quoted 8 lines
drivers/clk/clk-gpio.c +3 -0
+}
+
   const struct clk_ops clk_gpio_ops = {
       .enable         = clk_gpio_enable,
       .disable        = clk_gpio_disable,
+     .get_rate       = clk_gpio_get_rate,
   };
 
Quoted 5 lines

drivers/clk/clk-gpio.c static int clk_gpio_disable(struct clk *clk) · line 56

Quoted 2 lines
drivers/clk/clk-gpio.c +1 -1
-static int clk_gpio_probe(struct udevice *dev)
+static int clk_gpio_of_to_plat(struct udevice *dev)

This change should be a separate commit.

Quoted 6 lines

This actually should not be accepted first place

.of_to_plat - convert device tree data to plat
.probe - make a device ready for use

https://github.com/u-boot/u-boot/blob/master/doc/develop/driver-model/design.rst

Yes, this is why I was a bit confused :)

Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
drivers/clk/clk-gpio.c +13 -2
   {
       struct clk_gpio_priv *priv = dev_get_priv(dev);
+     int ret;
 
-     return gpio_request_by_name(dev, "enable-gpios", 0,
-                                 &priv->enable, GPIOD_IS_OUT);
+     priv->clk = devm_clk_get(dev, NULL);
+     if (IS_ERR(priv->clk)) {
+             log_err("%s: Could not get gated clock: %ld\n",
+                     __func__, PTR_ERR(priv->clk));
+             return PTR_ERR(priv->clk);
+     }
+
+     ret = gpio_request_by_name(dev, "enable-gpios", 0,
+                                &priv->enable, GPIOD_IS_OUT);
+     if (ret) {
+             log_err("%s: Could not decode enable-gpios (%d)\n",
+                     __func__, ret);
Quoted 6 lines
Quoted 2 lines

Use dev_dbg for probe errors to reduce binary size.

These errors provide intel about probing 2 key parts of the driver. How would you know it fails in case you have no devices which face this error? Iis there any convention or restriction which prohibits this? If yes, then provide a document please.

https://docs.u-boot.org/en/latest/develop/driver-model/design.html#error-codes

| Ideally, messages in drivers should only be displayed when debugging,
| e.g. by using log_debug() although in extreme cases log_warning() or
| log_error() may be used.
Quoted 1 line

All other general clock drivers use log_err/dev_err versions btw.

Yeah, it's something we have not been so consistent with enforcing.

Quoted 18 lines
Quoted 18 lines
Quoted 8 lines
drivers/clk/clk-gpio.c +4 -0
+             return ret;
+     }
+
+     return 0;
   }
 
   /*

drivers/clk/clk-gpio.c U_BOOT_DRIVER(gpio_gate_clock) = { · line 98

Quoted 8 lines
drivers/clk/clk-gpio.c +1 -1
@@ -59,7 +92,7 @@ U_BOOT_DRIVER(gpio_gate_clock) = {
       .name           = "gpio_clock",
       .id             = UCLASS_CLK,
       .of_match       = clk_gpio_match,
-     .probe          = clk_gpio_probe,
+     .of_to_plat     = clk_gpio_of_to_plat,
       .priv_auto      = sizeof(struct clk_gpio_priv),
       .ops            = &clk_gpio_ops,
       .flags          = DM_FLAG_PRE_RELOC,

--Sean

#5 lore raw

Re: [PATCH v1 1/1] clk: clk-gpio: add actual gated clock

пт, 15 груд. 2023 р. о 20:45 Sean Anderson <[email protected]> пише:

Quoted 38 lines · Svyatoslav Ryhel
Quoted 36 lines

пт, 15 груд. 2023 р. о 19:25 Sean Anderson <[email protected]> пише:

Quoted 35 lines · Svyatoslav Ryhel
Quoted 20 lines

Existing gpio-gate-clock driver acts like a simple GPIO switch without any effect on gated clock. Add actual clock actions into enable/disable ops and implement get_rate op by passing gated clock if it is enabled.

---
   drivers/clk/clk-gpio.c | 47 +++++++++++++++++++++++++++++++++++-------
   1 file changed, 40 insertions(+), 7 deletions(-)
drivers/clk/clk-gpio.c +0 -3
index 26d795b978..a55470cfbf 100644
@@ -3,19 +3,23 @@
    * Copyright (C) 2023 Marek Vasut <[email protected]>
    */
 
-#include <asm/gpio.h>
-#include <common.h>
-#include <clk-uclass.h>

drivers/clk/clk-gpio.c line 15

Quoted 10 lines
drivers/clk/clk-gpio.c +6 -0
+#include <clk.h>
   #include <dm.h>
+#include <clk-uclass.h>
+#include <linux/clk-provider.h>
+
+#include <asm/gpio.h>
 
   struct clk_gpio_priv {
       struct gpio_desc        enable;
+     struct clk              *clk;

Please name this "parent".

Quoted 5 lines
Quoted 2 lines

I see no need in this since clk is generic name and this driver can accept only one clock with any name.

It's good documentation. It makes it clear what the purpose of the member is.

This driver uses a single (1) clock with a single purpose.

Why do you think you need a clock in a clock-gpio driver? Hm, I don't know. This will remain a mystery for future archeo-coders yeah.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
drivers/clk/clk-gpio.c +1 -0
   };
 
   static int clk_gpio_enable(struct clk *clk)
   {
       struct clk_gpio_priv *priv = dev_get_priv(clk->dev);
 
+     clk_prepare_enable(priv->clk);
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

Just clk_enable is fine. We don't have a separate prepare stage line Linux.

U-Boot supports clk_prepare_enable and since this support is present I see no contradiction in using it.

These functions are only there for to make it easier to port code from Linux. Since this code was written for U-Boot, just use the actual function.

Then document that its use is discouraged. Until then it is not restricted.

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 12 lines
drivers/clk/clk-gpio.c +1 -0
       dm_gpio_set_value(&priv->enable, 1);
 
       return 0;
@@ -26,21 +30,50 @@ static int clk_gpio_disable(struct clk *clk)
       struct clk_gpio_priv *priv = dev_get_priv(clk->dev);
 
       dm_gpio_set_value(&priv->enable, 0);
+     clk_disable_unprepare(priv->clk);
 
       return 0;
   }
 

drivers/clk/clk-gpio.c static int clk_gpio_disable(struct clk *clk) · line 47

Quoted 10 lines
drivers/clk/clk-gpio.c +10 -0
+static ulong clk_gpio_get_rate(struct clk *clk)
+{
+     struct clk_gpio_priv *priv = dev_get_priv(clk->dev);
+     int ret;
+
+     ret = dm_gpio_get_value(&priv->enable);
+     if (ret)
+             return clk_get_rate(priv->clk);
+     else
+             return -EINVAL;

This should always return the parent rate.

Quoted 5 lines
Quoted 1 line

How can you return clock rate if the gate is disabled?

This allows consumers to know what the rate will be before they enable the clock.

I don't quite understand the logic behind this but ok.

Quoted 50 lines
Quoted 13 lines
Quoted 8 lines
Quoted 8 lines
drivers/clk/clk-gpio.c +3 -0
+}
+
   const struct clk_ops clk_gpio_ops = {
       .enable         = clk_gpio_enable,
       .disable        = clk_gpio_disable,
+     .get_rate       = clk_gpio_get_rate,
   };
 
Quoted 5 lines

drivers/clk/clk-gpio.c static int clk_gpio_disable(struct clk *clk) · line 56

Quoted 2 lines
drivers/clk/clk-gpio.c +1 -1
-static int clk_gpio_probe(struct udevice *dev)
+static int clk_gpio_of_to_plat(struct udevice *dev)

This change should be a separate commit.

Quoted 6 lines

This actually should not be accepted first place

.of_to_plat - convert device tree data to plat
.probe - make a device ready for use

https://github.com/u-boot/u-boot/blob/master/doc/develop/driver-model/design.rst

Yes, this is why I was a bit confused :)

Quoted 19 lines
Quoted 19 lines
Quoted 18 lines
drivers/clk/clk-gpio.c +13 -2
   {
       struct clk_gpio_priv *priv = dev_get_priv(dev);
+     int ret;
 
-     return gpio_request_by_name(dev, "enable-gpios", 0,
-                                 &priv->enable, GPIOD_IS_OUT);
+     priv->clk = devm_clk_get(dev, NULL);
+     if (IS_ERR(priv->clk)) {
+             log_err("%s: Could not get gated clock: %ld\n",
+                     __func__, PTR_ERR(priv->clk));
+             return PTR_ERR(priv->clk);
+     }
+
+     ret = gpio_request_by_name(dev, "enable-gpios", 0,
+                                &priv->enable, GPIOD_IS_OUT);
+     if (ret) {
+             log_err("%s: Could not decode enable-gpios (%d)\n",
+                     __func__, ret);
Quoted 6 lines
Quoted 2 lines

Use dev_dbg for probe errors to reduce binary size.

These errors provide intel about probing 2 key parts of the driver. How would you know it fails in case you have no devices which face this error? Iis there any convention or restriction which prohibits this? If yes, then provide a document please.

https://docs.u-boot.org/en/latest/develop/driver-model/design.html#error-codes

Quoted 8 lines
| Ideally, messages in drivers should only be displayed when debugging,
| e.g. by using log_debug() although in extreme cases log_warning() or
| log_error() may be used.
Quoted 1 line

All other general clock drivers use log_err/dev_err versions btw.

Yeah, it's something we have not been so consistent with enforcing.

Alright

Quoted 19 lines
Quoted 18 lines
Quoted 18 lines
Quoted 8 lines
drivers/clk/clk-gpio.c +4 -0
+             return ret;
+     }
+
+     return 0;
   }
 
   /*

drivers/clk/clk-gpio.c U_BOOT_DRIVER(gpio_gate_clock) = { · line 98

Quoted 8 lines
drivers/clk/clk-gpio.c +1 -1
@@ -59,7 +92,7 @@ U_BOOT_DRIVER(gpio_gate_clock) = {
       .name           = "gpio_clock",
       .id             = UCLASS_CLK,
       .of_match       = clk_gpio_match,
-     .probe          = clk_gpio_probe,
+     .of_to_plat     = clk_gpio_of_to_plat,
       .priv_auto      = sizeof(struct clk_gpio_priv),
       .ops            = &clk_gpio_ops,
       .flags          = DM_FLAG_PRE_RELOC,

--Sean

#6 lore raw

Re: [PATCH v1 1/1] clk: clk-gpio: add actual gated clock

Quoted 42 lines · Svyatoslav Ryhel

пт, 15 груд. 2023 р. о 20:45 Sean Anderson <[email protected]> пише:

Quoted 41 lines · Svyatoslav Ryhel
Quoted 38 lines

пт, 15 груд. 2023 р. о 19:25 Sean Anderson <[email protected]> пише:

Quoted 29 lines · Svyatoslav Ryhel
Quoted 27 lines

Existing gpio-gate-clock driver acts like a simple GPIO switch without any effect on gated clock. Add actual clock actions into enable/disable ops and implement get_rate op by passing gated clock if it is enabled.

---
    drivers/clk/clk-gpio.c | 47 +++++++++++++++++++++++++++++++++++-------
    1 file changed, 40 insertions(+), 7 deletions(-)
drivers/clk/clk-gpio.c +5 -3
index 26d795b978..a55470cfbf 100644
@@ -3,19 +3,23 @@
     * Copyright (C) 2023 Marek Vasut <[email protected]>
     */
 
-#include <asm/gpio.h>
-#include <common.h>
-#include <clk-uclass.h>
+#include <clk.h>
    #include <dm.h>
+#include <clk-uclass.h>
+#include <linux/clk-provider.h>
+
+#include <asm/gpio.h>
 
Quoted 6 lines

drivers/clk/clk-gpio.c line 15

Quoted 3 lines
drivers/clk/clk-gpio.c +1 -0
    struct clk_gpio_priv {
        struct gpio_desc        enable;
+     struct clk              *clk;

Please name this "parent".

I see no need in this since clk is generic name and this driver can accept only one clock with any name.

Quoted 6 lines
Quoted 2 lines

It's good documentation. It makes it clear what the purpose of the member is.

This driver uses a single (1) clock with a single purpose.

Why do you think you need a clock in a clock-gpio driver? Hm, I don't know. This will remain a mystery for future archeo-coders yeah.

If you don't want to change the name, then add a doc comment.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines

drivers/clk/clk-gpio.c line 22

Quoted 7 lines
drivers/clk/clk-gpio.c +1 -0
    };
 
    static int clk_gpio_enable(struct clk *clk)
    {
        struct clk_gpio_priv *priv = dev_get_priv(clk->dev);
 
+     clk_prepare_enable(priv->clk);

Just clk_enable is fine. We don't have a separate prepare stage line Linux.

Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

U-Boot supports clk_prepare_enable and since this support is present I see no contradiction in using it.

These functions are only there for to make it easier to port code from Linux. Since this code was written for U-Boot, just use the actual function.

Then document that its use is discouraged. Until then it is not restricted.

There are many functions like this in U-Boot (e.g. kmalloc which takes an unused flags argument, or all the pr_* functions which are almost-but-not-quite the same as log_*). Not all of them are explicitly document as such, but generally you should use the native functions where it is not inconvenient.

--Sean