mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] clk: Fix return value of of_clk_get_by_name()
@ 2023-10-06  5:28 Alexander Shiyan
  2023-10-06  6:49 ` Ahmad Fatoum
  0 siblings, 1 reply; 3+ messages in thread
From: Alexander Shiyan @ 2023-10-06  5:28 UTC (permalink / raw)
  To: barebox; +Cc: Alexander Shiyan

of_clk_get_by_name() is used by clk_bulk_get_optional() => clk_get() function,
so in case some clocks are missing we should return -ENOENT error code,
as this is normal for optional clocks.
The current implementation of of_clk_get_by_name() returns the -EINVAL error
that comes from of_clk_get() with index <0 if the clock is not described
in the device tree.

Signed-off-by: Alexander Shiyan <eagle.alexander923@gmail.com>
---
 drivers/clk/clkdev.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/clk/clkdev.c b/drivers/clk/clkdev.c
index dbe998b6af..f6c2fcdf10 100644
--- a/drivers/clk/clkdev.c
+++ b/drivers/clk/clkdev.c
@@ -52,10 +52,9 @@ EXPORT_SYMBOL(of_clk_get);
  */
 struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
 {
-	struct clk *clk = ERR_PTR(-ENOENT);
-
 	/* Walk up the tree of devices looking for a clock that matches */
 	while (np) {
+		struct clk *clk;
 		int index = 0;
 
 		/*
@@ -68,11 +67,12 @@ struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
 							 name);
 		clk = of_clk_get(np, index);
 		if (!IS_ERR(clk))
-			break;
-		else if (name && index >= 0) {
+			return clk;
+
+		if (name && index >= 0) {
 			pr_err("ERROR: could not get clock %pOF:%s(%i)\n",
 				np, name ? name : "", index);
-			return clk;
+			return ERR_PTR(-ENOENT);
 		}
 
 		/*
@@ -85,7 +85,7 @@ struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
 			break;
 	}
 
-	return clk;
+	return ERR_PTR(-ENOENT);
 }
 EXPORT_SYMBOL(of_clk_get_by_name);
 #endif
-- 
2.39.1




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] clk: Fix return value of of_clk_get_by_name()
  2023-10-06  5:28 [PATCH] clk: Fix return value of of_clk_get_by_name() Alexander Shiyan
@ 2023-10-06  6:49 ` Ahmad Fatoum
  2023-10-06 10:00   ` Alexander Shiyan
  0 siblings, 1 reply; 3+ messages in thread
From: Ahmad Fatoum @ 2023-10-06  6:49 UTC (permalink / raw)
  To: Alexander Shiyan, barebox

Hi Alexander,

On 06.10.23 07:28, Alexander Shiyan wrote:
> of_clk_get_by_name() is used by clk_bulk_get_optional() => clk_get() function,
> so in case some clocks are missing we should return -ENOENT error code,
> as this is normal for optional clocks.
> The current implementation of of_clk_get_by_name() returns the -EINVAL error
> that comes from of_clk_get() with index <0 if the clock is not described
> in the device tree.

I stumbled upon the same problem and found that of_clk_get_by_name() returning
ERR_PTR(-EINVAL) in this case is documented Linux behavior (for of_parse_clkspec()).

The correct solution is thus to leave of_clk_get_by_name() alone and align
clk_get return values with Linux.

I did this in
https://lore.barebox.org/barebox/20230921080236.GX637806@pengutronix.de/T/#t

Can you test with that and see if it solves your issue?

Cheers,
Ahmad 

> 
> Signed-off-by: Alexander Shiyan <eagle.alexander923@gmail.com>
> ---
>  drivers/clk/clkdev.c | 12 ++++++------
>  1 file changed, 6 insertions(+), 6 deletions(-)
> 
> diff --git a/drivers/clk/clkdev.c b/drivers/clk/clkdev.c
> index dbe998b6af..f6c2fcdf10 100644
> --- a/drivers/clk/clkdev.c
> +++ b/drivers/clk/clkdev.c
> @@ -52,10 +52,9 @@ EXPORT_SYMBOL(of_clk_get);
>   */
>  struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
>  {
> -	struct clk *clk = ERR_PTR(-ENOENT);
> -
>  	/* Walk up the tree of devices looking for a clock that matches */
>  	while (np) {
> +		struct clk *clk;
>  		int index = 0;
>  
>  		/*
> @@ -68,11 +67,12 @@ struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
>  							 name);
>  		clk = of_clk_get(np, index);
>  		if (!IS_ERR(clk))
> -			break;
> -		else if (name && index >= 0) {
> +			return clk;
> +
> +		if (name && index >= 0) {
>  			pr_err("ERROR: could not get clock %pOF:%s(%i)\n",
>  				np, name ? name : "", index);
> -			return clk;
> +			return ERR_PTR(-ENOENT);
>  		}
>  
>  		/*
> @@ -85,7 +85,7 @@ struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
>  			break;
>  	}
>  
> -	return clk;
> +	return ERR_PTR(-ENOENT);
>  }
>  EXPORT_SYMBOL(of_clk_get_by_name);
>  #endif

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] clk: Fix return value of of_clk_get_by_name()
  2023-10-06  6:49 ` Ahmad Fatoum
@ 2023-10-06 10:00   ` Alexander Shiyan
  0 siblings, 0 replies; 3+ messages in thread
From: Alexander Shiyan @ 2023-10-06 10:00 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

> On 06.10.23 07:28, Alexander Shiyan wrote:
> > of_clk_get_by_name() is used by clk_bulk_get_optional() => clk_get() function,
> > so in case some clocks are missing we should return -ENOENT error code,
> > as this is normal for optional clocks.
> > The current implementation of of_clk_get_by_name() returns the -EINVAL error
> > that comes from of_clk_get() with index <0 if the clock is not described
> > in the device tree.
>
> I stumbled upon the same problem and found that of_clk_get_by_name() returning
> ERR_PTR(-EINVAL) in this case is documented Linux behavior (for of_parse_clkspec()).
>
> The correct solution is thus to leave of_clk_get_by_name() alone and align
> clk_get return values with Linux.
>
> I did this in
> https://lore.barebox.org/barebox/20230921080236.GX637806@pengutronix.de/T/#t
>
> Can you test with that and see if it solves your issue?

Hello Ahmad.
Yes, this patch works. Thanks!



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-10-06 10:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-10-06  5:28 [PATCH] clk: Fix return value of of_clk_get_by_name() Alexander Shiyan
2023-10-06  6:49 ` Ahmad Fatoum
2023-10-06 10:00   ` Alexander Shiyan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox