diff mbox series

[v6,1/6] clk: Add of_clk_get_by_name_optional() function

Message ID 20181116145937.27660-2-phil.edworthy@renesas.com (mailing list archive)
State Superseded, archived
Headers show
Series clk: Add functions to get optional clocks | expand

Commit Message

Phil Edworthy Nov. 16, 2018, 2:59 p.m. UTC
Quite a few drivers get an optional clock, e.g. a clock required to
access a peripheral's registers that is always enabled on some devices.
This adds the of_clk_get_by_name_optional() function for this purpose.

This function behaves the same as of_clk_get_by_name() except that it
will return NULL instead of -ENOENT. This allows for simpler error
handling in the callers.

Signed-off-by: Phil Edworthy <phil.edworthy@renesas.com>
---
*Warning*
This changes the return values for of_clk_get_by_name() in some cases.
If the name arg is non-NULL, and the "clock-names" OF property can't be
found or the name is not in that prop, the code used to return -EINVAL,
but will now return -ENOENT.
Note that before and after this patch, if name=NULL and no "clocks" OF
property has been found, of_clk_get_by_name() returns -ENOENT.

I believe the new behaviour is correct. I cannot find any callers to
of_clk_get_by_name() that explicitly check for -EINVAL or -ENOENT, but
there is the possibility that something will break at runtime with this
change.

v6:
 - Rework the __of_clk_get_by_name() logic so as to avoid duplicate tests.
v5:
 - Simplified the code by handling all the error conditions on exit
v4:
 - For optional named clocks of_property_match_string() will return
   -EINVAL if the "clock-names" property is missing, or -ENODATA if
   the specified clock name in the "clock-names" property is missing.
   If we then call __of_clk_get() with these errors as the index, we
   get clk = -EINVAL. This is then filtered later on so users don't
   see it. However, if the clock is not named, __of_clk_get() will
   return -ENOENT is the clock provide is not there.
   So for optional named clocks, use index to determine if the clock
   provider is there or not, and for unnamed clocks, simply check if
   __of_clk_get() returns -ENOENT.

v3:
 - Fix check for clock not present. __of_clk_get() returns -EINVAL
   if it's not there. Cover case of when there is no clock name.
 - of_clk_get_by_name_optional() should return NULL if !np.
 - Add dummy version of of_clk_get_by_name_optional() for the
   !OF || !COMMON_CLK case.
---
 drivers/clk/clkdev.c | 76 ++++++++++++++++++++++++++++++++++++--------
 include/linux/clk.h  |  6 ++++
 2 files changed, 69 insertions(+), 13 deletions(-)

Comments

Uwe Kleine-König Nov. 16, 2018, 4:01 p.m. UTC | #1
On Fri, Nov 16, 2018 at 02:59:32PM +0000, Phil Edworthy wrote:
> Quite a few drivers get an optional clock, e.g. a clock required to
> access a peripheral's registers that is always enabled on some devices.
> This adds the of_clk_get_by_name_optional() function for this purpose.
> 
> This function behaves the same as of_clk_get_by_name() except that it
> will return NULL instead of -ENOENT. This allows for simpler error
> handling in the callers.
> 
> Signed-off-by: Phil Edworthy <phil.edworthy@renesas.com>
> ---
> *Warning*
> This changes the return values for of_clk_get_by_name() in some cases.
> If the name arg is non-NULL, and the "clock-names" OF property can't be
> found or the name is not in that prop, the code used to return -EINVAL,
> but will now return -ENOENT.
> Note that before and after this patch, if name=NULL and no "clocks" OF
> property has been found, of_clk_get_by_name() returns -ENOENT.
> 
> I believe the new behaviour is correct. I cannot find any callers to
> of_clk_get_by_name() that explicitly check for -EINVAL or -ENOENT, but
> there is the possibility that something will break at runtime with this
> change.
> 
> v6:
>  - Rework the __of_clk_get_by_name() logic so as to avoid duplicate tests.
> v5:
>  - Simplified the code by handling all the error conditions on exit
> v4:
>  - For optional named clocks of_property_match_string() will return
>    -EINVAL if the "clock-names" property is missing, or -ENODATA if
>    the specified clock name in the "clock-names" property is missing.
>    If we then call __of_clk_get() with these errors as the index, we
>    get clk = -EINVAL. This is then filtered later on so users don't
>    see it. However, if the clock is not named, __of_clk_get() will
>    return -ENOENT is the clock provide is not there.
>    So for optional named clocks, use index to determine if the clock
>    provider is there or not, and for unnamed clocks, simply check if
>    __of_clk_get() returns -ENOENT.
> 
> v3:
>  - Fix check for clock not present. __of_clk_get() returns -EINVAL
>    if it's not there. Cover case of when there is no clock name.
>  - of_clk_get_by_name_optional() should return NULL if !np.
>  - Add dummy version of of_clk_get_by_name_optional() for the
>    !OF || !COMMON_CLK case.
> ---
>  drivers/clk/clkdev.c | 76 ++++++++++++++++++++++++++++++++++++--------
>  include/linux/clk.h  |  6 ++++
>  2 files changed, 69 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/clk/clkdev.c b/drivers/clk/clkdev.c
> index 9ab3db8b3988..0c655d1ba1d9 100644
> --- a/drivers/clk/clkdev.c
> +++ b/drivers/clk/clkdev.c
> @@ -52,9 +52,19 @@ struct clk *of_clk_get(struct device_node *np, int index)
>  }
>  EXPORT_SYMBOL(of_clk_get);
>  
> +/*
> + * This function tries to find a clock provider.
> + * If a name is provided, the function looks for a clock with that name in the
> + * OF node's "clock-names" property. If not found, the function will try the
> + * parent node and so on until a matching property is found or we reach the
> + * top of the tree.

I think the comment here could be improved. I think if you replace the
last sentence by:

	If not found (i.e. either there is no "clock-names" property or
	the "clock-names" property doesn't include the name to look up),
	the function will try the parent node and so on until ...

Other than that I think the patch is fine, but maybe it would be easier
to review if you split it in two patches. The first patch to implement
the changed behaviour you mention in the warning above and only then the
addition of the optional handling.

Best regards
Uwe
Uwe Kleine-König Nov. 16, 2018, 4:11 p.m. UTC | #2
On Fri, Nov 16, 2018 at 05:01:28PM +0100, Uwe Kleine-König wrote:
> Other than that I think the patch is fine

Thinking again, I wonder why not just do:

static inline struct clk *clk_get_optional(struct device *dev, const char *id)
{
	struct clk *c = clk_get(dev, id);

	if (c == ERR_PTR(-ENOENT))
		return NULL;
	else
		return c;
}

Best regards
Uwe
Phil Edworthy Nov. 19, 2018, 10:41 a.m. UTC | #3
Hi Uwe,

On 16 November 2018 16:11 Uwe Kleine-König wrote:
> On Fri, Nov 16, 2018 at 05:01:28PM +0100, Uwe Kleine-König wrote:
> > Other than that I think the patch is fine
> 
> Thinking again, I wonder why not just do:
> 
> static inline struct clk *clk_get_optional(struct device *dev, const char *id) {
> 	struct clk *c = clk_get(dev, id);
> 
> 	if (c == ERR_PTR(-ENOENT))
> 		return NULL;
> 	else
> 		return c;
> }

Unfortunately, underneath this __of_clk_get_by_name() returns -EINVAL
when looking for a named clock, and the "clock-names" OF property can't
be found or the name is not in that prop. This is because the index
returned by of_property_match_string() will be an error code and is then
currently always passed to __of_clk_get().

If, as you said, I split the patches into one that fixes the error code, and then
adds clk_get_optional() like above, it will make more sense.

btw, do we need to add of_clk_get_by_name_optional()? I only added it as a
counterpart to of_clk_get_by_name(), but it may not be needed.

Thanks
Phil
Uwe Kleine-König Nov. 19, 2018, 10:46 a.m. UTC | #4
Hello Phil,

On Mon, Nov 19, 2018 at 10:41:42AM +0000, Phil Edworthy wrote:
> On 16 November 2018 16:11 Uwe Kleine-König wrote:
> > On Fri, Nov 16, 2018 at 05:01:28PM +0100, Uwe Kleine-König wrote:
> > > Other than that I think the patch is fine
> > 
> > Thinking again, I wonder why not just do:
> > 
> > static inline struct clk *clk_get_optional(struct device *dev, const char *id) {
> > 	struct clk *c = clk_get(dev, id);
> > 
> > 	if (c == ERR_PTR(-ENOENT))
> > 		return NULL;
> > 	else
> > 		return c;
> > }
> 
> Unfortunately, underneath this __of_clk_get_by_name() returns -EINVAL
> when looking for a named clock, and the "clock-names" OF property can't
> be found or the name is not in that prop. This is because the index
> returned by of_property_match_string() will be an error code and is then
> currently always passed to __of_clk_get().
> 
> If, as you said, I split the patches into one that fixes the error code, and then
> adds clk_get_optional() like above, it will make more sense.

Sounds like a good plan.

> btw, do we need to add of_clk_get_by_name_optional()? I only added it as a
> counterpart to of_clk_get_by_name(), but it may not be needed.

I don't need it. Given that it is easy to add when someone has a need,
I'd say, skip it for now.

Best regards
Uwe
Phil Edworthy Nov. 19, 2018, 12:53 p.m. UTC | #5
Hi Uwe,

On 19 November 2018 10:46 Uwe Kleine-König wrote:
> On Mon, Nov 19, 2018 at 10:41:42AM +0000, Phil Edworthy wrote:
> > On 16 November 2018 16:11 Uwe Kleine-König wrote:
> > > On Fri, Nov 16, 2018 at 05:01:28PM +0100, Uwe Kleine-König wrote:
> > > > Other than that I think the patch is fine
> > >
> > > Thinking again, I wonder why not just do:
> > >
> > > static inline struct clk *clk_get_optional(struct device *dev, const char
> *id) {
> > > 	struct clk *c = clk_get(dev, id);
> > >
> > > 	if (c == ERR_PTR(-ENOENT))
> > > 		return NULL;
> > > 	else
> > > 		return c;
> > > }
> >
> > Unfortunately, underneath this __of_clk_get_by_name() returns -EINVAL
> > when looking for a named clock, and the "clock-names" OF property
> > can't be found or the name is not in that prop. This is because the
> > index returned by of_property_match_string() will be an error code and
> > is then currently always passed to __of_clk_get().
> >
> > If, as you said, I split the patches into one that fixes the error
> > code, and then adds clk_get_optional() like above, it will make more sense.
> 
> Sounds like a good plan.
Now that I have removed of_clk_get_by_name_optional(), I see that clk_get()
deals with __of_clk_get_by_name() returning -EINVAL and -ENOENT the same
way. In both cases, clk_get_sys() will return -ENOENT... i.e. I no longer need to
modify __of_clk_get_by_name().
All I need is a simple wrapper just as you have outlined above.

> > btw, do we need to add of_clk_get_by_name_optional()? I only added it
> > as a counterpart to of_clk_get_by_name(), but it may not be needed.
> 
> I don't need it. Given that it is easy to add when someone has a need, I'd say,
> skip it for now.
I'm wondering if we actually need clk_get_optional(). For me at least, I just
want devm_clk_get_optional(). That would get rid of the arch patches.

Thanks
Phil
Uwe Kleine-König Nov. 19, 2018, 12:58 p.m. UTC | #6
Hello Phil,

On Mon, Nov 19, 2018 at 12:53:46PM +0000, Phil Edworthy wrote:
> On 19 November 2018 10:46 Uwe Kleine-König wrote:
> > On Mon, Nov 19, 2018 at 10:41:42AM +0000, Phil Edworthy wrote:
> > > btw, do we need to add of_clk_get_by_name_optional()? I only added it
> > > as a counterpart to of_clk_get_by_name(), but it may not be needed.
> > 
> > I don't need it. Given that it is easy to add when someone has a need, I'd say,
> > skip it for now.
> 
> I'm wondering if we actually need clk_get_optional(). For me at least, I just
> want devm_clk_get_optional(). That would get rid of the arch patches.

Given that clk_get_optional will be that simple, it can live in
linux/clk.h for all implementors of the clk API, then you don't have to
care about different archs. (Unless I'm missing something.)

I don't think it's a good idea to drop clk_get_optional even if you'd
have to provide arch-specific stuff.

Best regards
Uwe
Phil Edworthy Nov. 19, 2018, 1:41 p.m. UTC | #7
Hi Uwe,

On 19 November 2018 12:58 Uwe Kleine-König wrote:
> On Mon, Nov 19, 2018 at 12:53:46PM +0000, Phil Edworthy wrote:
> > On 19 November 2018 10:46 Uwe Kleine-König wrote:
> > > On Mon, Nov 19, 2018 at 10:41:42AM +0000, Phil Edworthy wrote:
> > > > btw, do we need to add of_clk_get_by_name_optional()? I only added
> it
> > > > as a counterpart to of_clk_get_by_name(), but it may not be needed.
> > >
> > > I don't need it. Given that it is easy to add when someone has a need, I'd
> say,
> > > skip it for now.
> >
> > I'm wondering if we actually need clk_get_optional(). For me at least, I just
> > want devm_clk_get_optional(). That would get rid of the arch patches.
> 
> Given that clk_get_optional will be that simple, it can live in
> linux/clk.h for all implementors of the clk API, then you don't have to
> care about different archs. (Unless I'm missing something.)
You are absolutely right, I'm such a clutz sometimes!

Thanks
Phil

> I don't think it's a good idea to drop clk_get_optional even if you'd
> have to provide arch-specific stuff.
> 
> Best regards
> Uwe
> 
> --
> Pengutronix e.K.                           | Uwe Kleine-König            |
> Industrial Linux Solutions                 | http://www.pengutronix.de/  |
diff mbox series

Patch

diff --git a/drivers/clk/clkdev.c b/drivers/clk/clkdev.c
index 9ab3db8b3988..0c655d1ba1d9 100644
--- a/drivers/clk/clkdev.c
+++ b/drivers/clk/clkdev.c
@@ -52,9 +52,19 @@  struct clk *of_clk_get(struct device_node *np, int index)
 }
 EXPORT_SYMBOL(of_clk_get);
 
+/*
+ * This function tries to find a clock provider.
+ * If a name is provided, the function looks for a clock with that name in the
+ * OF node's "clock-names" property. If not found, the function will try the
+ * parent node and so on until a matching property is found or we reach the
+ * top of the tree.
+ * When no clock provider is found, if optional is true, the function will
+ * return NULL, otherwise return -ENOENT.
+ */
 static struct clk *__of_clk_get_by_name(struct device_node *np,
 					const char *dev_id,
-					const char *name)
+					const char *name,
+					bool optional)
 {
 	struct clk *clk = ERR_PTR(-ENOENT);
 
@@ -65,18 +75,33 @@  static struct clk *__of_clk_get_by_name(struct device_node *np,
 		/*
 		 * For named clocks, first look up the name in the
 		 * "clock-names" property.  If it cannot be found, then
-		 * index will be an error code, and of_clk_get() will fail.
+		 * index will be an error code.
 		 */
 		if (name)
 			index = of_property_match_string(np, "clock-names", name);
-		clk = __of_clk_get(np, index, dev_id, name);
-		if (!IS_ERR(clk)) {
-			break;
-		} else if (name && index >= 0) {
-			if (PTR_ERR(clk) != -EPROBE_DEFER)
-				pr_err("ERROR: could not get clock %pOF:%s(%i)\n",
-					np, name ? name : "", index);
-			return clk;
+
+		/*
+		 * If we are looking for an unnamed clock, or we have found the
+		 * named clock in the node, try to get the clock provider.
+		 */
+		if (index >= 0) {
+			clk = __of_clk_get(np, index, dev_id, name);
+			if (!IS_ERR(clk))
+				return clk;
+
+			/*
+			 * If the node specifies the clock name, do not walk up
+			 * the tree looking in parent nodes.
+			 */
+			if (name) {
+				/* Optional clock that's not there? */
+				if (optional && PTR_ERR(clk) == -ENOENT)
+					clk = NULL;
+				else if (PTR_ERR(clk) != -EPROBE_DEFER)
+					pr_err("ERROR: could not get clock %pOF:%s(%i)\n",
+					       np, name, index);
+				return clk;
+			}
 		}
 
 		/*
@@ -89,6 +114,9 @@  static struct clk *__of_clk_get_by_name(struct device_node *np,
 			break;
 	}
 
+	if (optional && PTR_ERR(clk) == -ENOENT)
+		clk = NULL;
+
 	return clk;
 }
 
@@ -106,15 +134,37 @@  struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
 	if (!np)
 		return ERR_PTR(-ENOENT);
 
-	return __of_clk_get_by_name(np, np->full_name, name);
+	return __of_clk_get_by_name(np, np->full_name, name, false);
 }
 EXPORT_SYMBOL(of_clk_get_by_name);
 
+/**
+ * of_clk_get_by_name_optional() - Parse and lookup an optional clock referenced
+ * by a device node
+ * @np: pointer to clock consumer node
+ * @name: name of consumer's clock input, or NULL for the first clock reference
+ *
+ * This function parses the clocks and clock-names properties, and uses them to
+ * look up the struct clk from the registered list of clock providers.
+ * It behaves the same as of_clk_get_by_name(), except when np is NULL or no
+ * clock provider is found, when it then returns NULL.
+ */
+struct clk *of_clk_get_by_name_optional(struct device_node *np,
+					const char *name)
+{
+	if (!np)
+		return NULL;
+
+	return __of_clk_get_by_name(np, np->full_name, name, true);
+}
+EXPORT_SYMBOL(of_clk_get_by_name_optional);
+
 #else /* defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) */
 
 static struct clk *__of_clk_get_by_name(struct device_node *np,
 					const char *dev_id,
-					const char *name)
+					const char *name,
+					bool optional)
 {
 	return ERR_PTR(-ENOENT);
 }
@@ -197,7 +247,7 @@  struct clk *clk_get(struct device *dev, const char *con_id)
 	struct clk *clk;
 
 	if (dev && dev->of_node) {
-		clk = __of_clk_get_by_name(dev->of_node, dev_id, con_id);
+		clk = __of_clk_get_by_name(dev->of_node, dev_id, con_id, false);
 		if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
 			return clk;
 	}
diff --git a/include/linux/clk.h b/include/linux/clk.h
index a7773b5c0b9f..84512b3ecf5c 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -865,6 +865,7 @@  static inline void clk_bulk_disable_unprepare(int num_clks,
 #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK)
 struct clk *of_clk_get(struct device_node *np, int index);
 struct clk *of_clk_get_by_name(struct device_node *np, const char *name);
+struct clk *of_clk_get_by_name_optional(struct device_node *np, const char *name);
 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec);
 #else
 static inline struct clk *of_clk_get(struct device_node *np, int index)
@@ -876,6 +877,11 @@  static inline struct clk *of_clk_get_by_name(struct device_node *np,
 {
 	return ERR_PTR(-ENOENT);
 }
+static inline struct clk *of_clk_get_by_name_optional(struct device_node *np,
+						      const char *name)
+{
+	return NULL;
+}
 static inline struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
 {
 	return ERR_PTR(-ENOENT);