Skip to contentSkip to conversation

clk: A few bugfixes/enhancements for CCF

PATCH RESEND Applied Yang Xiwen 2 participants 19 messages Aug 17, 2023 – Nov 4, 2023 1 message not in the archive Common clk

v1 of 2 Applied Latest is v2 →

lore
Share & tools
lore

Yang Xiwen

author
#0 lore raw

cover clk: A few bugfixes/enhancements for CCF

They are found during my development for HiSilicon clock driver. Details are in commit logs.

---
Yang Xiwen (5):
clk: export clk_register_mux_table()
clk: call log_debug() instead to avoid console log printing
clk: also handle ENOENT in *_optional functions
clk: promote clk_dev_ops to linux/clk-provider.h
clk: ccf: call clock provided ops directly for endisable()

 drivers/clk/clk-uclass.c     |  5 -----
 drivers/clk/clk.c            | 16 +++++++++++++---
 include/clk.h                |  8 +++++---
 include/linux/clk-provider.h | 11 +++++++++++
 4 files changed, 29 insertions(+), 11 deletions(-)

---
base-commit: 580eb31199be8a822e62f20965854a242f895d03
change-id: 20230807-clk-fix-17e895f79817

Best regards,

Signature · 1 line
Yang Xiwen <[email protected]>
author
#1 lore raw

PATCH RESEND 1/5 clk: export clk_register_mux_table()

It's already implemented in clk-mux.c, export it in the header file.

---
 include/linux/clk-provider.h | 6 ++++++
 1 file changed, 6 insertions(+)
include/linux/clk-provider.h +6 -0
index b8acacd49e..801404480b 100644
@@ -247,6 +247,12 @@ struct clk *clk_register_mux(struct device *dev, const char *name,
 		void __iomem *reg, u8 shift, u8 width,
 		u8 clk_mux_flags);
 
+struct clk *clk_register_mux_table(struct device *dev, const char *name,
+				   const char * const *parent_names, u8 num_parents,
+				   unsigned long flags,
+				   void __iomem *reg, u8 shift, u32 mask,
+				   u8 clk_mux_flags, u32 *table);
+
 struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
 				    ulong rate);
 
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH RESEND 1/5] clk: export clk_register_mux_table()

Quoted 17 lines · On 8/17/23 13:04, Yang Xiwen via B4 Relay wrote:

From: Yang Xiwen <[email protected]>

It's already implemented in clk-mux.c, export it in the header file.

---
  include/linux/clk-provider.h | 6 ++++++
  1 file changed, 6 insertions(+)
include/linux/clk-provider.h +0 -0
index b8acacd49e..801404480b 100644
@@ -247,6 +247,12 @@ struct clk *clk_register_mux(struct device *dev, const char *name,
  		void __iomem *reg, u8 shift, u8 width,
  		u8 clk_mux_flags);

include/linux/clk-provider.h struct clk *clk_register_mux(struct device *dev, const char *name, · line 258

Quoted 10 lines
include/linux/clk-provider.h +6 -0
 
+struct clk *clk_register_mux_table(struct device *dev, const char *name,
+				   const char * const *parent_names, u8 num_parents,
+				   unsigned long flags,
+				   void __iomem *reg, u8 shift, u32 mask,
+				   u8 clk_mux_flags, u32 *table);
+
  struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
  				    ulong rate);
 

Why do you want to export this? None of your other patches use it.

--Sean

author
#3 lore raw

Re: [PATCH RESEND 1/5] clk: export clk_register_mux_table()

Quoted 23 lines · Sean Anderson
Quoted 22 lines · On 8/17/23 13:04, Yang Xiwen via B4 Relay wrote:

From: Yang Xiwen <[email protected]>

It's already implemented in clk-mux.c, export it in the header file.

---
  include/linux/clk-provider.h | 6 ++++++

  1 file changed, 6 insertions(+)
include/linux/clk-provider.h +0 -0
index b8acacd49e..801404480b 100644
@@ -247,6 +247,12 @@ struct clk *clk_register_mux(struct device *dev,
 const char *name,
           void __iomem *reg, u8 shift, u8 width,
           u8 clk_mux_flags);

  +struct clk *clk_register_mux_table(struct device *dev, const char *name,
+                   const char * const *parent_names, u8 num_parents, +                   unsigned long flags,
+                   void __iomem *reg, u8 shift, u32 mask,

Quoted 8 lines
Quoted 6 lines

+                   u8 clk_mux_flags, u32 *table);
+
  struct clk *clk_register_fixed_rate(struct device *dev, const char *name,
                      ulong rate);

Why do you want to export this? None of your other patches use it.

It will be used in HiSilicon clk framework driver which i will send after this series is applied. And this function is exported in Linux kernel CCF. So i think it's fine to export it in U-Boot as well.

Quoted 2 lines

--Sean

Signature · 2 lines
Best regards,
Yang Xiwen
#4 lore raw

Re: [PATCH RESEND 1/5] clk: export clk_register_mux_table()

Quoted 32 lines · Yang Xiwen
Quoted 23 lines · Sean Anderson
Quoted 22 lines · On 8/17/23 13:04, Yang Xiwen via B4 Relay wrote:

From: Yang Xiwen <[email protected]>

It's already implemented in clk-mux.c, export it in the header file.

---
   include/linux/clk-provider.h | 6 ++++++
   1 file changed, 6 insertions(+)
include/linux/clk-provider.h +3 -0
index b8acacd49e..801404480b 100644
@@ -247,6 +247,12 @@ struct clk *clk_register_mux(struct device *dev,
 const char *name,
           void __iomem *reg, u8 shift, u8 width,
           u8 clk_mux_flags);
   +struct clk *clk_register_mux_table(struct device *dev, const char
 *name,
+                   const char * const *parent_names, u8 num_parents,
+                   unsigned long flags,
+                   void __iomem *reg, u8 shift, u32 mask,
Quoted 8 lines
Quoted 6 lines
include/linux/clk-provider.h +2 -0
+                   u8 clk_mux_flags, u32 *table);
+
   struct clk *clk_register_fixed_rate(struct device *dev, const char

*name,
                      ulong rate);

Why do you want to export this? None of your other patches use it.

Quoted 3 lines

It will be used in HiSilicon clk framework driver which i will send after this series is applied. And this function is exported in Linux kernel CCF. So i think it's fine to export it in U-Boot as well.

Please just send it along with that series then.

--Sean

author
#5 lore raw

PATCH RESEND 2/5 clk: call log_debug() instead to avoid console log printing

In reply to Yang Xiwen

it's a very common case to register a clock without a parent, such as clk_register_fixed_rate(). Replace log_error() with log_debug() to avoid useless console log if not debugging.

---
 drivers/clk/clk.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/clk/clk.c +2 -2
index a5a3461b66..a38daaac0c 100644
@@ -24,8 +24,8 @@ int clk_register(struct clk *clk, const char *drv_name,
 
 	ret = uclass_get_device_by_name(UCLASS_CLK, parent_name, &parent);
 	if (ret) {
-		log_err("%s: failed to get %s device (parent of %s)\n",
-			__func__, parent_name, name);
+		log_debug("%s: failed to get %s device (parent of %s)\n",
+			  __func__, parent_name, name);
 	} else {
 		log_debug("%s: name: %s parent: %s [0x%p]\n", __func__, name,
 			  parent->name, parent);
Signature · 1 line
2.34.1
#6 lore raw

Re: [PATCH RESEND 2/5] clk: call log_debug() instead to avoid console log printing

Quoted 4 lines · On 8/17/23 13:04, Yang Xiwen via B4 Relay wrote:

From: Yang Xiwen <[email protected]>

it's a very common case to register a clock without a parent, such as clk_register_fixed_rate().

Actually, that seems like the only place this is done.

Quoted 14 lines

Replace log_error() with log_debug() to avoid
useless console log if not debugging.

---
  drivers/clk/clk.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/clk/clk.c +0 -0
index a5a3461b66..a38daaac0c 100644

drivers/clk/clk.c int clk_register(struct clk *clk, const char *drv_name, · line 31

Quoted 10 lines
drivers/clk/clk.c +2 -2
@@ -24,8 +24,8 @@ int clk_register(struct clk *clk, const char *drv_name,
 
  	ret = uclass_get_device_by_name(UCLASS_CLK, parent_name, &parent);
  	if (ret) {
-		log_err("%s: failed to get %s device (parent of %s)\n",
-			__func__, parent_name, name);
+		log_debug("%s: failed to get %s device (parent of %s)\n",
+			  __func__, parent_name, name);
  	} else {
  		log_debug("%s: name: %s parent: %s [0x%p]\n", __func__, name,
  			  parent->name, parent);

I think a correct fix would be

drivers/clk/clk.c +10 -8
index a5a3461b66c..cb333c83f66 100644
@@ -18,17 +18,19 @@
 int clk_register(struct clk *clk, const char *drv_name,
                 const char *name, const char *parent_name)
 {
-       struct udevice *parent;
+       struct udevice *parent = NULL;
        struct driver *drv;
        int ret;
 
-       ret = uclass_get_device_by_name(UCLASS_CLK, parent_name, &parent);
-       if (ret) {
-               log_err("%s: failed to get %s device (parent of %s)\n",
-                       __func__, parent_name, name);
-       } else {
-               log_debug("%s: name: %s parent: %s [0x%p]\n", __func__, name,
-                         parent->name, parent);
+       if (parent_name) {
+               ret = uclass_get_device_by_name(UCLASS_CLK, parent_name,
+                                               &parent);
+               if (ret)
+                       log_err("%s: failed to get %s device (parent of %s)\n",
+                               __func__, parent_name, name);
+               else
+                       log_debug("%s: name: %s parent: %s [0x%p]\n", __func__,
+                                 name, parent->name, parent);
        }
 
        drv = lists_driver_lookup_name(drv_name);

--Sean

#7 lore raw

Re: [PATCH RESEND 2/5] clk: call log_debug() instead to avoid console log printing

Quoted 58 lines · Sean Anderson
Quoted 4 lines · On 8/17/23 13:04, Yang Xiwen via B4 Relay wrote:

From: Yang Xiwen <[email protected]>

it's a very common case to register a clock without a parent, such as clk_register_fixed_rate().

Actually, that seems like the only place this is done.

Quoted 13 lines

Replace log_error() with log_debug() to avoid
useless console log if not debugging.

---
  drivers/clk/clk.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/clk/clk.c +0 -0
index a5a3461b66..a38daaac0c 100644

drivers/clk/clk.c int clk_register(struct clk *clk, const char *drv_name, · line 31

Quoted 10 lines
drivers/clk/clk.c +2 -2
@@ -24,8 +24,8 @@ int clk_register(struct clk *clk, const char *drv_name,
       ret = uclass_get_device_by_name(UCLASS_CLK, parent_name, &parent);
       if (ret) {
-        log_err("%s: failed to get %s device (parent of %s)\n",
-            __func__, parent_name, name);
+        log_debug("%s: failed to get %s device (parent of %s)\n",
+              __func__, parent_name, name);
       } else {
           log_debug("%s: name: %s parent: %s [0x%p]\n", __func__, name,
                 parent->name, parent);
 

I think a correct fix would be

drivers/clk/clk.c +5 -8
index a5a3461b66c..cb333c83f66 100644
@@ -18,17 +18,19 @@
  int clk_register(struct clk *clk, const char *drv_name,
                  const char *name, const char *parent_name)
  {
-       struct udevice *parent;
+       struct udevice *parent = NULL;
         struct driver *drv;
         int ret;
 
-       ret = uclass_get_device_by_name(UCLASS_CLK, parent_name, &parent);
-       if (ret) {
-               log_err("%s: failed to get %s device (parent of %s)\n",
-                       __func__, parent_name, name);
-       } else {
-               log_debug("%s: name: %s parent: %s [0x%p]\n", __func__, name,
-                         parent->name, parent);
+       if (parent_name) {
+               ret = uclass_get_device_by_name(UCLASS_CLK, parent_name,
+                                               &parent);
+               if (ret)

drivers/clk/clk.c

Quoted 10 lines
drivers/clk/clk.c +5 -1
+                       log_err("%s: failed to get %s device (parent of %s)\n",
+                               __func__, parent_name, name);
+               else
+                       log_debug("%s: name: %s parent: %s [0x%p]\n", __func__,
+                                 name, parent->name, parent);
         }
 
         drv = lists_driver_lookup_name(drv_name);
 
--Sean

or you could modify the condition to be if (ret && parent_name) with appropriate modification of the second debug message.

--Sean

author
#8 lore raw

Re: [PATCH RESEND 2/5] clk: call log_debug() instead to avoid console log printing

Quoted 71 lines · Sean Anderson
Quoted 70 lines · Sean Anderson
Quoted 4 lines · On 8/17/23 13:04, Yang Xiwen via B4 Relay wrote:

From: Yang Xiwen <[email protected]>

it's a very common case to register a clock without a parent, such as clk_register_fixed_rate().

Actually, that seems like the only place this is done.

Quoted 14 lines

Replace log_error() with log_debug() to avoid
useless console log if not debugging.

---
  drivers/clk/clk.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/clk/clk.c +0 -0
index a5a3461b66..a38daaac0c 100644

drivers/clk/clk.c int clk_register(struct clk *clk, const char · line 31

Quoted 10 lines
drivers/clk/clk.c +2 -2
@@ -24,8 +24,8 @@ int clk_register(struct clk *clk, const char
 *drv_name,
       ret = uclass_get_device_by_name(UCLASS_CLK, parent_name, &parent);
       if (ret) {
-        log_err("%s: failed to get %s device (parent of %s)\n",
-            __func__, parent_name, name);
+        log_debug("%s: failed to get %s device (parent of %s)\n",
+              __func__, parent_name, name);
       } else {
           log_debug("%s: name: %s parent: %s [0x%p]\n", __func__, name,
                 parent->name, parent);

I think a correct fix would be

drivers/clk/clk.c +9 -8
index a5a3461b66c..cb333c83f66 100644
@@ -18,17 +18,19 @@
   int clk_register(struct clk *clk, const char *drv_name,
                   const char *name, const char *parent_name)
   {
-       struct udevice *parent;
+       struct udevice *parent = NULL;
          struct driver *drv;
          int ret;
 
-       ret = uclass_get_device_by_name(UCLASS_CLK, parent_name,
 &parent);
-       if (ret) {
-               log_err("%s: failed to get %s device (parent of %s)\n",
-                       __func__, parent_name, name);
-       } else {
-               log_debug("%s: name: %s parent: %s [0x%p]\n",
 __func__, name,
-                         parent->name, parent);
+       if (parent_name) {
+               ret = uclass_get_device_by_name(UCLASS_CLK, parent_name,
+                                               &parent);
+               if (ret)
+                       log_err("%s: failed to get %s device (parent
 of %s)\n",
+                               __func__, parent_name, name);
+               else
+                       log_debug("%s: name: %s parent: %s [0x%p]\n",

__func__,
+                                 name, parent->name, parent); }

Quoted 8 lines
Quoted 3 lines

         drv = lists_driver_lookup_name(drv_name);

--Sean

or you could modify the condition to be if (ret && parent_name) with appropriate
modification of the second debug message.

Thanks for your review. I'll use your patch with your SoB and drop mine in next release.

Quoted 1 line

--Sean

Signature · 2 lines
Best regards,
Yang Xiwen
author
#9 lore raw

PATCH RESEND 3/5 clk: also handle ENOENT in *_optional functions

In reply to Yang Xiwen

If the device does not specify any clocks in device tree, these functions will return PTR_ERR(-ENOENT). This is not the intended behavior and does not comply with linux kernel CCF. Fix that by returning NULL under such circumstances instead.

---
 include/clk.h | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
include/clk.h +5 -3
index d91285235f..f95da0838d 100644
@@ -223,9 +223,11 @@ struct clk *devm_clk_get(struct udevice *dev, const char *id);
 static inline struct clk *devm_clk_get_optional(struct udevice *dev,
 						const char *id)
 {
+	int ret;
 	struct clk *clk = devm_clk_get(dev, id);
 
-	if (PTR_ERR(clk) == -ENODATA)
+	ret = PTR_ERR(clk);
+	if (ret == -ENODATA || ret == -ENOENT)
 		return NULL;
 
 	return clk;
@@ -335,7 +337,7 @@ static inline int clk_get_by_name_optional(struct udevice *dev,
 	int ret;
 
 	ret = clk_get_by_name(dev, name, clk);
-	if (ret == -ENODATA)
+	if (ret == -ENODATA || ret == -ENOENT)
 		return 0;
 
 	return ret;
@@ -359,7 +361,7 @@ static inline int clk_get_by_name_nodev_optional(ofnode node, const char *name,
 	int ret;
 
 	ret = clk_get_by_name_nodev(node, name, clk);
-	if (ret == -ENODATA)
+	if (ret == -ENODATA || ret == -ENOENT)
 		return 0;
 
 	return ret;
Signature · 1 line
2.34.1
Reviewed-by on v1 3/5
#10 lore raw

Re: [PATCH RESEND 3/5] clk: also handle ENOENT in *_optional functions

Quoted 40 lines · On 8/17/23 13:04, Yang Xiwen via B4 Relay wrote:

From: Yang Xiwen <[email protected]>

If the device does not specify any clocks in device tree, these functions will return PTR_ERR(-ENOENT). This is not the intended behavior and does not comply with linux kernel CCF. Fix that by returning NULL under such circumstances instead.

---
  include/clk.h | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
include/clk.h +4 -2
index d91285235f..f95da0838d 100644
@@ -223,9 +223,11 @@ struct clk *devm_clk_get(struct udevice *dev, const char *id);
  static inline struct clk *devm_clk_get_optional(struct udevice *dev,
  						const char *id)
  {
+	int ret;
  	struct clk *clk = devm_clk_get(dev, id);
 
-	if (PTR_ERR(clk) == -ENODATA)
+	ret = PTR_ERR(clk);
+	if (ret == -ENODATA || ret == -ENOENT)
  		return NULL;
 
  	return clk;
@@ -335,7 +337,7 @@ static inline int clk_get_by_name_optional(struct udevice *dev,
  	int ret;
 
  	ret = clk_get_by_name(dev, name, clk);
-	if (ret == -ENODATA)
+	if (ret == -ENODATA || ret == -ENOENT)
  		return 0;
 
  	return ret;

include/clk.h static inline int clk_get_by_name_nodev_optional(ofnode node, const char *name, · line 367

Quoted 8 lines
include/clk.h +1 -1
@@ -359,7 +361,7 @@ static inline int clk_get_by_name_nodev_optional(ofnode node, const char *name,
  	int ret;
 
  	ret = clk_get_by_name_nodev(node, name, clk);
-	if (ret == -ENODATA)
+	if (ret == -ENODATA || ret == -ENOENT)
  		return 0;
 
  	return ret;
author
#11 lore raw

PATCH RESEND 4/5 clk: promote clk_dev_ops to linux/clk-provider.h

In reply to Yang Xiwen

So that it can be used by others.

---
 drivers/clk/clk-uclass.c     | 5 -----
 include/linux/clk-provider.h | 5 +++++
 2 files changed, 5 insertions(+), 5 deletions(-)
drivers/clk/clk-uclass.c +0 -5
index dc3e9d6a26..5cc80e5e39 100644
@@ -25,11 +25,6 @@
 #include <linux/clk-provider.h>
 #include <linux/err.h>
 
-static inline const struct clk_ops *clk_dev_ops(struct udevice *dev)
-{
-	return (const struct clk_ops *)dev->driver->ops;
-}
-
 struct clk *dev_get_clk_ptr(struct udevice *dev)
 {
 	return (struct clk *)dev_get_uclass_priv(dev);
include/linux/clk-provider.h +5 -0
index 801404480b..dfafb4cc9d 100644
@@ -21,6 +21,11 @@ static inline void clk_dm(ulong id, struct clk *clk)
 		clk->id = id;
 }
 
+static inline const struct clk_ops *clk_dev_ops(struct udevice *dev)
+{
+	return (const struct clk_ops *)dev->driver->ops;
+}
+
 /*
  * flags used across common struct clk.  these flags should only affect the
  * top-level framework.  custom flags for dealing with hardware specifics
Signature · 1 line
2.34.1
author
#12 lore raw

PATCH RESEND 5/5 clk: ccf: call clock provided ops directly for endisable()

In reply to Yang Xiwen

Calling into CCF framework will cause a clock being enabled twice instead of once (clk->enable_count becomes 2 rather than 1), thus making it hard to disable (needs to call clk_disable() twice).
Fix that by calling clock provided ops directly.

---
 drivers/clk/clk.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/clk/clk.c +11 -1
index a38daaac0c..00d082c46f 100644
@@ -14,6 +14,7 @@
 #include <dm/uclass.h>
 #include <dm/lists.h>
 #include <dm/device-internal.h>
+#include <linux/clk-provider.h>
 
 int clk_register(struct clk *clk, const char *drv_name,
 		 const char *name, const char *parent_name)
@@ -115,11 +116,20 @@ int ccf_clk_set_parent(struct clk *clk, struct clk *parent)
 static int ccf_clk_endisable(struct clk *clk, bool enable)
 {
 	struct clk *c;
+	const struct clk_ops *ops;
 	int err = clk_get_by_id(clk->id, &c);
 
 	if (err)
 		return err;
-	return enable ? clk_enable(c) : clk_disable(c);
+	else
+		ops = clk_dev_ops(c->dev);
+
+	if (enable && ops->enable)
+		return ops->enable(c);
+	else if (!enable && ops->disable)
+		return ops->disable(c);
+
+	return -ENOSYS;
 }
 
 int ccf_clk_enable(struct clk *clk)
Signature · 1 line
2.34.1
#13 lore raw

Re: [PATCH RESEND 5/5] clk: ccf: call clock provided ops directly for endisable()

Quoted 6 lines · On 8/17/23 13:04, Yang Xiwen via B4 Relay wrote:

From: Yang Xiwen <[email protected]>

Calling into CCF framework will cause a clock being enabled twice instead of once (clk->enable_count becomes 2 rather than 1), thus making it hard to disable (needs to call clk_disable() twice).
Fix that by calling clock provided ops directly.

Can you describe this scenario more? From what I can tell, clk_enable doesn't increment enable_count for CCF clocks.

--Sean

Quoted 40 lines
---
  drivers/clk/clk.c | 12 +++++++++++-
  1 file changed, 11 insertions(+), 1 deletion(-)
drivers/clk/clk.c +11 -1
index a38daaac0c..00d082c46f 100644
@@ -14,6 +14,7 @@
  #include <dm/uclass.h>
  #include <dm/lists.h>
  #include <dm/device-internal.h>
+#include <linux/clk-provider.h>
 
  int clk_register(struct clk *clk, const char *drv_name,
  		 const char *name, const char *parent_name)
@@ -115,11 +116,20 @@ int ccf_clk_set_parent(struct clk *clk, struct clk *parent)
  static int ccf_clk_endisable(struct clk *clk, bool enable)
  {
  	struct clk *c;
+	const struct clk_ops *ops;
  	int err = clk_get_by_id(clk->id, &c);
 
  	if (err)
  		return err;
-	return enable ? clk_enable(c) : clk_disable(c);
+	else
+		ops = clk_dev_ops(c->dev);
+
+	if (enable && ops->enable)
+		return ops->enable(c);
+	else if (!enable && ops->disable)
+		return ops->disable(c);
+
+	return -ENOSYS;
  }
 
  int ccf_clk_enable(struct clk *clk)
author
#14 lore raw

Re: [PATCH RESEND 5/5] clk: ccf: call clock provided ops directly for endisable()

Quoted 12 lines · Sean Anderson
Quoted 6 lines · On 8/17/23 13:04, Yang Xiwen via B4 Relay wrote:

From: Yang Xiwen <[email protected]>

Calling into CCF framework will cause a clock being enabled twice instead of once (clk->enable_count becomes 2 rather than 1), thus making it hard to disable (needs to call clk_disable() twice).
Fix that by calling clock provided ops directly.

Can you describe this scenario more? From what I can tell, clk_enable doesn't
increment enable_count for CCF clocks.

Well, it's hard to describe clearly. But I can only tell this patch fixed the issue when i was trying to write an Ethernet driver[1] which calls clk_disable() and expects the clock to be disabled after that. Also I found that CCF driver does not have a corresponding test file. I will try to write a test for that in next release.

Quoted 32 lines

--Sean

Quoted 30 lines

---
  drivers/clk/clk.c | 12 +++++++++++-

  1 file changed, 11 insertions(+), 1 deletion(-)
drivers/clk/clk.c +6 -1
index a38daaac0c..00d082c46f 100644
@@ -14,6 +14,7 @@
   #include <dm/uclass.h>
   #include <dm/lists.h>
   #include <dm/device-internal.h>
+#include <linux/clk-provider.h>
     int clk_register(struct clk *clk, const char *drv_name,
            const char *name, const char *parent_name)
@@ -115,11 +116,20 @@ int ccf_clk_set_parent(struct clk *clk, struct
 clk *parent)
   static int ccf_clk_endisable(struct clk *clk, bool enable)
   {
       struct clk *c;
+    const struct clk_ops *ops;
       int err = clk_get_by_id(clk->id, &c);
         if (err)
           return err;
-    return enable ? clk_enable(c) : clk_disable(c);
+    else
+        ops = clk_dev_ops(c->dev);
+
+    if (enable && ops->enable)
Quoted 9 lines

drivers/clk/clk.c int ccf_clk_set_parent(struct clk *clk, struct clk *parent)

Quoted 8 lines
drivers/clk/clk.c +5 -0
+        return ops->enable(c);
+    else if (!enable && ops->disable)
+        return ops->disable(c);
+
+    return -ENOSYS;
   }
     int ccf_clk_enable(struct clk *clk)
 

[1]
https://lore.kernel.org/all/[email protected]/

Signature · 2 lines
Best regards,
Yang Xiwen

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#16 lore raw

Re: [PATCH RESEND 5/5] clk: ccf: call clock provided ops directly for endisable()

Quoted 13 lines · Yang Xiwen
Quoted 12 lines · Sean Anderson
Quoted 6 lines · On 8/17/23 13:04, Yang Xiwen via B4 Relay wrote:

From: Yang Xiwen <[email protected]>

Calling into CCF framework will cause a clock being enabled twice instead of once (clk->enable_count becomes 2 rather than 1), thus making it hard to disable (needs to call clk_disable() twice).
Fix that by calling clock provided ops directly.

Can you describe this scenario more? From what I can tell, clk_enable doesn't
increment enable_count for CCF clocks.

Quoted 5 lines

Well, it's hard to describe clearly. But I can only tell this patch fixed the issue when i was trying to write an Ethernet driver[1] which calls clk_disable() and expects the clock to be disabled after that. Also I found that CCF driver does not have a corresponding test file. I will try to write a test for that in next release.

Okay, fine. I read the source again and let me try to explain the whole thing to you briefly. Let's see what happens when we are calling clk_enable(gate).

The source of clk.c is listed below and labeled for clarity:

```
1	if (CONFIG_IS_ENABLED(CLK_CCF)) {
2		/* Take id 0 as a non-valid clk, such as dummy */
3		if (clk->id && !clk_get_by_id(clk->id, &clkp)) {
4			if (clkp->enable_count) {
5				clkp->enable_count++;
6				return 0;
7			}
8			if (clkp->dev->parent &&
9			    device_get_uclass_id(clkp->dev->parent) == UCLASS_CLK) {
10				ret = clk_enable(dev_get_clk_ptr(clkp->dev->parent));
11				if (ret) {
12					printf("Enable %s failed\n",
13					       clkp->dev->parent->name);
14					return ret;
15				}
16			}
17		}
18
19		if (ops->enable) {
20			ret = ops->enable(clk);
21			if (ret) {
22				printf("Enable %s failed\n", clk->dev->name);
23				return ret;
24			}
25		}
26		if (clkp)
27			clkp->enable_count++;
28	} else {
29		if (!ops->enable)
30			return -ENOSYS;
31		return ops->enable(clk);
```

The following steps are executed:

1. Actually, a "fake" clk is passed to clk_enable() and only clk->id is valid. The actual clk is "clkp";
2. Initially, we runs till ret = ops->enable(clk)(line 20), i.e. ccf_clk_enable(clk);
3. Thankfully, ccf_clk_enable() calls clk_get_by_id() to get the real clk and call clk_enable(clkp) again so we won't have an endless loop here.
4. So ops->enable(clk) actually equals to clk_enable(clkp). It's obvious that there is a clkp->enable_count++ inside the nested function call since it's still 0. Now it becomes 1;
5. The nested clk_enable(clkp) now returns to the outer clk_enable(clk);
6. Unfortunately, there is a if (clkp) clkp->enable_count++;(line 26) afterwards. Now it becomes 2.
7. Finally, we got a clk being enabled twice. "clkp->enable_count" is 2 now.

Obviously it's not the intended behavior. We can either fix clk_enable() or ccf_clk_endisable() to resolve this. But I choose to touch ccf_clk_endisable() since it's less commonly used.

Quoted 44 lines
Quoted 32 lines

--Sean

Quoted 30 lines
---
  drivers/clk/clk.c | 12 +++++++++++-
  1 file changed, 11 insertions(+), 1 deletion(-)
drivers/clk/clk.c +6 -1
index a38daaac0c..00d082c46f 100644
@@ -14,6 +14,7 @@
  #include <dm/uclass.h>
  #include <dm/lists.h>
  #include <dm/device-internal.h>
+#include <linux/clk-provider.h>
    int clk_register(struct clk *clk, const char *drv_name,
           const char *name, const char *parent_name)
@@ -115,11 +116,20 @@ int ccf_clk_set_parent(struct clk *clk, struct
 clk *parent)
  static int ccf_clk_endisable(struct clk *clk, bool enable)
  {
      struct clk *c;
+    const struct clk_ops *ops;
      int err = clk_get_by_id(clk->id, &c);
        if (err)
          return err;
-    return enable ? clk_enable(c) : clk_disable(c);
+    else
+        ops = clk_dev_ops(c->dev);
+
+    if (enable && ops->enable)
Quoted 9 lines

drivers/clk/clk.c int ccf_clk_set_parent(struct clk *clk, struct clk *parent)

Quoted 8 lines
drivers/clk/clk.c +5 -0
+        return ops->enable(c);
+    else if (!enable && ops->disable)
+        return ops->disable(c);
+
+    return -ENOSYS;
  }
    int ccf_clk_enable(struct clk *clk)
 

[1]
https://lore.kernel.org/all/[email protected]/

Signature · 2 lines
Best regards,
Yang Xiwen
#17 lore raw

Re: [PATCH RESEND 5/5] clk: ccf: call clock provided ops directly for endisable()

Quoted 61 lines · Yang Xiwen
Quoted 13 lines · Yang Xiwen
Quoted 12 lines · Sean Anderson
Quoted 6 lines · On 8/17/23 13:04, Yang Xiwen via B4 Relay wrote:

From: Yang Xiwen <[email protected]>

Calling into CCF framework will cause a clock being enabled twice instead of once (clk->enable_count becomes 2 rather than 1), thus making it hard to disable (needs to call clk_disable() twice).
Fix that by calling clock provided ops directly.

Can you describe this scenario more? From what I can tell, clk_enable doesn't
increment enable_count for CCF clocks.

Quoted 5 lines

Well, it's hard to describe clearly. But I can only tell this patch fixed the issue when i was trying to write an Ethernet driver[1] which calls clk_disable() and expects the clock to be disabled after that. Also I found that CCF driver does not have a corresponding test file. I will try to write a test for that in next release.

Okay, fine. I read the source again and let me try to explain the whole thing to you briefly. Let's see what happens when we are calling clk_enable(gate).

The source of clk.c is listed below and labeled for clarity:

```
1	if (CONFIG_IS_ENABLED(CLK_CCF)) {
2		/* Take id 0 as a non-valid clk, such as dummy */
3		if (clk->id && !clk_get_by_id(clk->id, &clkp)) {
4			if (clkp->enable_count) {
5				clkp->enable_count++;
6				return 0;
7			}
8			if (clkp->dev->parent &&
9			    device_get_uclass_id(clkp->dev->parent) == UCLASS_CLK) {
10				ret = clk_enable(dev_get_clk_ptr(clkp->dev->parent));
11				if (ret) {
12					printf("Enable %s failed\n",
13					       clkp->dev->parent->name);
14					return ret;
15				}
16			}
17		}
18
19		if (ops->enable) {
20			ret = ops->enable(clk);
21			if (ret) {
22				printf("Enable %s failed\n", clk->dev->name);
23				return ret;
24			}
25		}
26		if (clkp)
27			clkp->enable_count++;
28	} else {
29		if (!ops->enable)
30			return -ENOSYS;
31		return ops->enable(clk);
```

The following steps are executed:

Quoted 13 lines

1. Actually, a "fake" clk is passed to clk_enable() and only clk->id is valid. The actual clk is "clkp";
2. Initially, we runs till ret = ops->enable(clk)(line 20), i.e. ccf_clk_enable(clk);
3. Thankfully, ccf_clk_enable() calls clk_get_by_id() to get the real clk and call clk_enable(clkp) again so we won't have an endless loop here.
4. So ops->enable(clk) actually equals to clk_enable(clkp). It's obvious that there is a clkp->enable_count++ inside the nested function call since it's still 0. Now it becomes 1;
5. The nested clk_enable(clkp) now returns to the outer clk_enable(clk);
6. Unfortunately, there is a if (clkp) clkp->enable_count++;(line 26) afterwards. Now it becomes 2.
7. Finally, we got a clk being enabled twice. "clkp->enable_count" is 2 now.

OK, thank you for writing this up; it is clearer now. Please include this in your commit message.

Quoted 3 lines

Obviously it's not the intended behavior. We can either fix clk_enable() or ccf_clk_endisable() to resolve this. But I choose to touch ccf_clk_endisable() since it's less commonly used.

Hm, what if we added something like clk_raw_enable, which just did

Quoted 7 lines

19 if (ops->enable) {
20 ret = ops->enable(clk);
21 if (ret) {
22 printf("Enable %s failed\n", clk->dev->name);
23 return ret;
24 }
25 }

and the same thing for disable.

--Sean

author
#18 lore raw
In reply to Yang Xiwen

Why is this patchset completely ignored for more than half a month already? I have some other patches pending because of this one. Please tell me what's wrong with this patchset so that i can fix them.

Signature · 2 lines
Regards,
Yang Xiwen
#19 lore raw

Re: (subset) [PATCH RESEND 0/5] clk: A few bugfixes/enhancements for CCF

In reply to Yang Xiwen
Quoted 4 lines · Yang Xiwen

They are found during my development for HiSilicon clock driver. Details are in commit logs.

Applied, thanks!

[3/5] clk: also handle ENOENT in *_optional functions

      commit: c4b52fda6924e92c6745351f32c4cafc36034170

Best regards,

Signature · 1 line
Sean Anderson <[email protected]>