2018-11-16 15:02:05

by Phil Edworthy

[permalink] [raw]
Subject: [PATCH v6 0/6] clk: Add functions to get optional clocks

Quite a few drivers get an optional clock, e.g. a bus clock required to
access peripheral's registers that is always enabled on some devices. This
series adds of_clk_get_by_name_optional(), clk_get_optional() and
devm_clk_get_optional() functions for this purpose.

The functions behave the same as of_clk_get_by_name(), clk_get() and
devm_clk_get() except that they will return NULL instead of -ENOENT. This
allows for simpler error handling in the callers.

*Note*
This changes the return values for of_clk_get_by_name() in some cases, see
[PATCH 1/6] clk: Add of_clk_get_by_name_optional() function.

v6:
- Rework the __of_clk_get_by_name() logic so as to avoid duplicate tests.
- Add doxygen style comment for devm_clk_get_optional() args
- Add clk_get_optional() to archs that don't use the commom clk framework.

Phil Edworthy (6):
clk: Add of_clk_get_by_name_optional() function
clk: Add functions to get optional clocks
m68k: coldfire: Add clk_get_optional() function
MIPS: AR7: Add clk_get_optional() function
MIPS: Loongson 2F: Add clk_get_optional() function
arch/unicore32/kernel/clock.c: Add clk_get_optional() function

arch/m68k/coldfire/clk.c | 11 ++++
arch/mips/ar7/clock.c | 11 ++++
arch/mips/loongson64/lemote-2f/clock.c | 11 ++++
arch/unicore32/kernel/clock.c | 11 ++++
drivers/clk/clk-devres.c | 18 ++++-
drivers/clk/clkdev.c | 91 ++++++++++++++++++++++----
include/linux/clk.h | 38 +++++++++++
7 files changed, 175 insertions(+), 16 deletions(-)

--
2.17.1



2018-11-16 15:01:56

by Phil Edworthy

[permalink] [raw]
Subject: [PATCH v6 4/6] MIPS: AR7: Add clk_get_optional() function

clk_get_optional() returns NULL if not found instead of -ENOENT,
otherwise the behaviour is the same as clk_get().

Signed-off-by: Phil Edworthy <[email protected]>
---
arch/mips/ar7/clock.c | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/arch/mips/ar7/clock.c b/arch/mips/ar7/clock.c
index 6b64fd96dba8..b13f763948b1 100644
--- a/arch/mips/ar7/clock.c
+++ b/arch/mips/ar7/clock.c
@@ -454,6 +454,17 @@ struct clk *clk_get(struct device *dev, const char *id)
}
EXPORT_SYMBOL(clk_get);

+struct clk *clk_get_optional(struct device *dev, const char *id)
+{
+ struct clk *clk = clk_get(dev, id);
+
+ if (clk == ERR_PTR(-ENOENT))
+ clk = NULL;
+
+ return clk;
+}
+EXPORT_SYMBOL(clk_get_optional);
+
void clk_put(struct clk *clk)
{
}
--
2.17.1


2018-11-16 15:02:14

by Phil Edworthy

[permalink] [raw]
Subject: [PATCH v6 5/6] MIPS: Loongson 2F: Add clk_get_optional() function

clk_get_optional() returns NULL if not found instead of -ENOENT,
otherwise the behaviour is the same as clk_get().

Signed-off-by: Phil Edworthy <[email protected]>
---
arch/mips/loongson64/lemote-2f/clock.c | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/arch/mips/loongson64/lemote-2f/clock.c b/arch/mips/loongson64/lemote-2f/clock.c
index 8281334df9c8..abbade58b635 100644
--- a/arch/mips/loongson64/lemote-2f/clock.c
+++ b/arch/mips/loongson64/lemote-2f/clock.c
@@ -53,6 +53,17 @@ struct clk *clk_get(struct device *dev, const char *id)
}
EXPORT_SYMBOL(clk_get);

+struct clk *clk_get_optional(struct device *dev, const char *id)
+{
+ struct clk *clk = clk_get(dev, id);
+
+ if (clk == ERR_PTR(-ENOENT))
+ clk = NULL;
+
+ return clk;
+}
+EXPORT_SYMBOL(clk_get_optional);
+
static void propagate_rate(struct clk *clk)
{
struct clk *clkp;
--
2.17.1


2018-11-16 15:02:19

by Phil Edworthy

[permalink] [raw]
Subject: [PATCH v6 1/6] clk: Add of_clk_get_by_name_optional() function

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 <[email protected]>
---
*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.
+ * 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);
--
2.17.1


2018-11-16 15:02:33

by Phil Edworthy

[permalink] [raw]
Subject: [PATCH v6 6/6] arch/unicore32/kernel/clock.c: Add clk_get_optional() function

clk_get_optional() returns NULL if not found instead of -ENOENT,
otherwise the behaviour is the same as clk_get().

Signed-off-by: Phil Edworthy <[email protected]>
---
arch/unicore32/kernel/clock.c | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/arch/unicore32/kernel/clock.c b/arch/unicore32/kernel/clock.c
index b1ca775f6f6e..03def0b43192 100644
--- a/arch/unicore32/kernel/clock.c
+++ b/arch/unicore32/kernel/clock.c
@@ -74,6 +74,17 @@ struct clk *clk_get(struct device *dev, const char *id)
}
EXPORT_SYMBOL(clk_get);

+struct clk *clk_get_optional(struct device *dev, const char *id)
+{
+ struct clk *clk = clk_get(dev, id);
+
+ if (clk == ERR_PTR(-ENOENT))
+ clk = NULL;
+
+ return clk;
+}
+EXPORT_SYMBOL(clk_get_optional);
+
void clk_put(struct clk *clk)
{
}
--
2.17.1


2018-11-16 15:02:40

by Phil Edworthy

[permalink] [raw]
Subject: [PATCH v6 2/6] clk: Add (devm_)clk_get_optional() functions

This adds clk_get_optional() and devm_clk_get_optional() functions to get
optional clocks.
They behave the same as (devm_)clk_get except where there is no clock
producer. In this case, instead of returning -ENOENT, the function
returns NULL. This makes error checking simpler and allows
clk_prepare_enable, etc to be called on the returned reference
without additional checks.

Signed-off-by: Phil Edworthy <[email protected]>
---
v6:
- Add doxygen style comment for devm_clk_get_optional() args
v5:
- No changes.
v4:
- No changes.
v3:
- No changes.
---
drivers/clk/clk-devres.c | 18 ++++++++++++++++--
drivers/clk/clkdev.c | 17 +++++++++++++++--
include/linux/clk.h | 32 ++++++++++++++++++++++++++++++++
3 files changed, 63 insertions(+), 4 deletions(-)

diff --git a/drivers/clk/clk-devres.c b/drivers/clk/clk-devres.c
index 12c87457eca1..be07536725a2 100644
--- a/drivers/clk/clk-devres.c
+++ b/drivers/clk/clk-devres.c
@@ -14,7 +14,7 @@ static void devm_clk_release(struct device *dev, void *res)
clk_put(*(struct clk **)res);
}

-struct clk *devm_clk_get(struct device *dev, const char *id)
+static struct clk *__devm_clk_get(struct device *dev, const char *id, bool optional)
{
struct clk **ptr, *clk;

@@ -22,7 +22,10 @@ struct clk *devm_clk_get(struct device *dev, const char *id)
if (!ptr)
return ERR_PTR(-ENOMEM);

- clk = clk_get(dev, id);
+ if (!optional)
+ clk = clk_get(dev, id);
+ else
+ clk = clk_get_optional(dev, id);
if (!IS_ERR(clk)) {
*ptr = clk;
devres_add(dev, ptr);
@@ -32,8 +35,19 @@ struct clk *devm_clk_get(struct device *dev, const char *id)

return clk;
}
+
+struct clk *devm_clk_get(struct device *dev, const char *id)
+{
+ return __devm_clk_get(dev, id, false);
+}
EXPORT_SYMBOL(devm_clk_get);

+struct clk *devm_clk_get_optional(struct device *dev, const char *id)
+{
+ return __devm_clk_get(dev, id, true);
+}
+EXPORT_SYMBOL(devm_clk_get_optional);
+
struct clk_bulk_devres {
struct clk_bulk_data *clks;
int num_clks;
diff --git a/drivers/clk/clkdev.c b/drivers/clk/clkdev.c
index 0c655d1ba1d9..ebf3afef4371 100644
--- a/drivers/clk/clkdev.c
+++ b/drivers/clk/clkdev.c
@@ -241,21 +241,34 @@ struct clk *clk_get_sys(const char *dev_id, const char *con_id)
}
EXPORT_SYMBOL(clk_get_sys);

-struct clk *clk_get(struct device *dev, const char *con_id)
+static struct clk *internal_clk_get(struct device *dev, const char *con_id,
+ bool optional)
{
const char *dev_id = dev ? dev_name(dev) : NULL;
struct clk *clk;

if (dev && dev->of_node) {
- clk = __of_clk_get_by_name(dev->of_node, dev_id, con_id, false);
+ clk = __of_clk_get_by_name(dev->of_node, dev_id, con_id,
+ optional);
if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
return clk;
}

return clk_get_sys(dev_id, con_id);
}
+
+struct clk *clk_get(struct device *dev, const char *con_id)
+{
+ return internal_clk_get(dev, con_id, false);
+}
EXPORT_SYMBOL(clk_get);

+struct clk *clk_get_optional(struct device *dev, const char *con_id)
+{
+ return internal_clk_get(dev, con_id, true);
+}
+EXPORT_SYMBOL(clk_get_optional);
+
void clk_put(struct clk *clk)
{
__clk_put(clk);
diff --git a/include/linux/clk.h b/include/linux/clk.h
index 84512b3ecf5c..58bebc32133e 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -290,6 +290,16 @@ static inline void clk_bulk_unprepare(int num_clks, struct clk_bulk_data *clks)
*/
struct clk *clk_get(struct device *dev, const char *id);

+/**
+ * clk_get_optional - lookup and obtain a reference to optional clock producer.
+ * @dev: device for clock "consumer"
+ * @id: clock consumer ID
+ *
+ * Behaves the same as clk_get except where there is no clock producer. In this
+ * case, instead of returning -ENOENT, the function returns NULL.
+ */
+struct clk *clk_get_optional(struct device *dev, const char *id);
+
/**
* clk_bulk_get - lookup and obtain a number of references to clock producer.
* @dev: device for clock "consumer"
@@ -383,6 +393,17 @@ int __must_check devm_clk_bulk_get_all(struct device *dev,
*/
struct clk *devm_clk_get(struct device *dev, const char *id);

+/**
+ * devm_clk_get_optional - lookup and obtain a managed reference to an optional
+ * clock producer.
+ * @dev: device for clock "consumer"
+ * @id: clock consumer ID
+ *
+ * Behaves the same as devm_clk_get except where there is no clock producer. In
+ * this case, instead of returning -ENOENT, the function returns NULL.
+ */
+struct clk *devm_clk_get_optional(struct device *dev, const char *id);
+
/**
* devm_get_clk_from_child - lookup and obtain a managed reference to a
* clock producer from child node.
@@ -701,6 +722,11 @@ static inline struct clk *clk_get(struct device *dev, const char *id)
return NULL;
}

+static inline struct clk *clk_get_optional(struct device *dev, const char *id)
+{
+ return NULL;
+}
+
static inline int __must_check clk_bulk_get(struct device *dev, int num_clks,
struct clk_bulk_data *clks)
{
@@ -718,6 +744,12 @@ static inline struct clk *devm_clk_get(struct device *dev, const char *id)
return NULL;
}

+static inline struct clk *devm_clk_get_optional(struct device *dev,
+ const char *id)
+{
+ return NULL;
+}
+
static inline int __must_check devm_clk_bulk_get(struct device *dev, int num_clks,
struct clk_bulk_data *clks)
{
--
2.17.1


2018-11-16 15:02:54

by Phil Edworthy

[permalink] [raw]
Subject: [PATCH v6 3/6] m68k: coldfire: Add clk_get_optional() function

clk_get_optional() returns NULL if not found instead of -ENOENT,
otherwise the behaviour is the same as clk_get().

Signed-off-by: Phil Edworthy <[email protected]>
---
arch/m68k/coldfire/clk.c | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/arch/m68k/coldfire/clk.c b/arch/m68k/coldfire/clk.c
index 7bc666e482eb..b221cabc7f54 100644
--- a/arch/m68k/coldfire/clk.c
+++ b/arch/m68k/coldfire/clk.c
@@ -87,6 +87,17 @@ struct clk *clk_get(struct device *dev, const char *id)
}
EXPORT_SYMBOL(clk_get);

+struct clk *clk_get_optional(struct device *dev, const char *id)
+{
+ struct clk *clk = clk_get(dev, id);
+
+ if (clk == ERR_PTR(-ENOENT))
+ clk = NULL;
+
+ return clk;
+}
+EXPORT_SYMBOL(clk_get_optional);
+
int clk_enable(struct clk *clk)
{
unsigned long flags;
--
2.17.1


2018-11-16 16:04:12

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH v6 1/6] clk: Add of_clk_get_by_name_optional() function

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 <[email protected]>
> ---
> *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

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | http://www.pengutronix.de/ |

2018-11-16 16:12:35

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH v6 1/6] clk: Add of_clk_get_by_name_optional() function

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

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | http://www.pengutronix.de/ |

2018-11-19 10:43:03

by Phil Edworthy

[permalink] [raw]
Subject: RE: [PATCH v6 1/6] clk: Add of_clk_get_by_name_optional() function

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

2018-11-19 10:48:22

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH v6 1/6] clk: Add of_clk_get_by_name_optional() function

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

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | http://www.pengutronix.de/ |

2018-11-19 12:55:10

by Phil Edworthy

[permalink] [raw]
Subject: RE: [PATCH v6 1/6] clk: Add of_clk_get_by_name_optional() function

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

2018-11-19 12:59:16

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH v6 1/6] clk: Add of_clk_get_by_name_optional() function

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

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | http://www.pengutronix.de/ |

2018-11-19 13:42:31

by Phil Edworthy

[permalink] [raw]
Subject: RE: [PATCH v6 1/6] clk: Add of_clk_get_by_name_optional() function

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/ |

2018-11-29 12:04:06

by Phil Edworthy

[permalink] [raw]
Subject: RE: [PATCH v6 3/6] m68k: coldfire: Add clk_get_optional() function

Hi Greq,

On 29 November 2018 11:55, Greg Ungerer wrote:
> On 17/11/18 12:59 am, Phil Edworthy wrote:
> > clk_get_optional() returns NULL if not found instead of -ENOENT,
> > otherwise the behaviour is the same as clk_get().
> >
> > Signed-off-by: Phil Edworthy <[email protected]>
>
> Acked-by: Greg Ungerer <gerg@@linux-m68k.org>
>
> Looks good. Do you want me to take this in the m68knommu git tree?
> Or is the whole series going through some other tree?
This patch is no longer needed as I found a better way to implement this:
[PATCH v8] clk: Add (devm_)clk_get_optional() functions
https://patchwork.kernel.org/patch/10690437/

Apologies for any confusion,
Phil

> Regards
> Greg
>
>
>
> > ---
> > arch/m68k/coldfire/clk.c | 11 +++++++++++
> > 1 file changed, 11 insertions(+)
> >
> > diff --git a/arch/m68k/coldfire/clk.c b/arch/m68k/coldfire/clk.c index
> > 7bc666e482eb..b221cabc7f54 100644
> > --- a/arch/m68k/coldfire/clk.c
> > +++ b/arch/m68k/coldfire/clk.c
> > @@ -87,6 +87,17 @@ struct clk *clk_get(struct device *dev, const char *id)
> > }
> > EXPORT_SYMBOL(clk_get);
> >
> > +struct clk *clk_get_optional(struct device *dev, const char *id) {
> > + struct clk *clk = clk_get(dev, id);
> > +
> > + if (clk == ERR_PTR(-ENOENT))
> > + clk = NULL;
> > +
> > + return clk;
> > +}
> > +EXPORT_SYMBOL(clk_get_optional);
> > +
> > int clk_enable(struct clk *clk)
> > {
> > unsigned long flags;
> >

2018-11-29 12:06:38

by Greg Ungerer

[permalink] [raw]
Subject: Re: [PATCH v6 3/6] m68k: coldfire: Add clk_get_optional() function

Hi Phil,

On 17/11/18 12:59 am, Phil Edworthy wrote:
> clk_get_optional() returns NULL if not found instead of -ENOENT,
> otherwise the behaviour is the same as clk_get().
>
> Signed-off-by: Phil Edworthy <[email protected]>

Acked-by: Greg Ungerer <gerg@@linux-m68k.org>

Looks good. Do you want me to take this in the m68knommu git tree?
Or is the whole series going through some other tree?

Regards
Greg



> ---
> arch/m68k/coldfire/clk.c | 11 +++++++++++
> 1 file changed, 11 insertions(+)
>
> diff --git a/arch/m68k/coldfire/clk.c b/arch/m68k/coldfire/clk.c
> index 7bc666e482eb..b221cabc7f54 100644
> --- a/arch/m68k/coldfire/clk.c
> +++ b/arch/m68k/coldfire/clk.c
> @@ -87,6 +87,17 @@ struct clk *clk_get(struct device *dev, const char *id)
> }
> EXPORT_SYMBOL(clk_get);
>
> +struct clk *clk_get_optional(struct device *dev, const char *id)
> +{
> + struct clk *clk = clk_get(dev, id);
> +
> + if (clk == ERR_PTR(-ENOENT))
> + clk = NULL;
> +
> + return clk;
> +}
> +EXPORT_SYMBOL(clk_get_optional);
> +
> int clk_enable(struct clk *clk)
> {
> unsigned long flags;
>

2018-11-29 12:11:49

by Greg Ungerer

[permalink] [raw]
Subject: Re: [PATCH v6 3/6] m68k: coldfire: Add clk_get_optional() function

Hi Phil,

On 29/11/18 10:02 pm, Phil Edworthy wrote:
> On 29 November 2018 11:55, Greg Ungerer wrote:
>> On 17/11/18 12:59 am, Phil Edworthy wrote:
>>> clk_get_optional() returns NULL if not found instead of -ENOENT,
>>> otherwise the behaviour is the same as clk_get().
>>>
>>> Signed-off-by: Phil Edworthy <[email protected]>
>>
>> Acked-by: Greg Ungerer <gerg@@linux-m68k.org>
>>
>> Looks good. Do you want me to take this in the m68knommu git tree?
>> Or is the whole series going through some other tree?
> This patch is no longer needed as I found a better way to implement this:
> [PATCH v8] clk: Add (devm_)clk_get_optional() functions
> https://patchwork.kernel.org/patch/10690437/
>
> Apologies for any confusion,

No problem, I'll just drop it then.

Regards
Greg


>>> ---
>>> arch/m68k/coldfire/clk.c | 11 +++++++++++
>>> 1 file changed, 11 insertions(+)
>>>
>>> diff --git a/arch/m68k/coldfire/clk.c b/arch/m68k/coldfire/clk.c index
>>> 7bc666e482eb..b221cabc7f54 100644
>>> --- a/arch/m68k/coldfire/clk.c
>>> +++ b/arch/m68k/coldfire/clk.c
>>> @@ -87,6 +87,17 @@ struct clk *clk_get(struct device *dev, const char *id)
>>> }
>>> EXPORT_SYMBOL(clk_get);
>>>
>>> +struct clk *clk_get_optional(struct device *dev, const char *id) {
>>> + struct clk *clk = clk_get(dev, id);
>>> +
>>> + if (clk == ERR_PTR(-ENOENT))
>>> + clk = NULL;
>>> +
>>> + return clk;
>>> +}
>>> +EXPORT_SYMBOL(clk_get_optional);
>>> +
>>> int clk_enable(struct clk *clk)
>>> {
>>> unsigned long flags;
>>>

2018-11-29 16:35:39

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH v6 3/6] m68k: coldfire: Add clk_get_optional() function

On Thu, Nov 29, 2018 at 09:54:37PM +1000, Greg Ungerer wrote:
> Hi Phil,
>
> On 17/11/18 12:59 am, Phil Edworthy wrote:
> > clk_get_optional() returns NULL if not found instead of -ENOENT,
> > otherwise the behaviour is the same as clk_get().
> >
> > Signed-off-by: Phil Edworthy <[email protected]>
>
> Acked-by: Greg Ungerer <gerg@@linux-m68k.org>
>
> Looks good. Do you want me to take this in the m68knommu git tree?
> Or is the whole series going through some other tree?

Any chance we could just get coldfire moved over to the common clock
framework?

2018-12-03 12:30:29

by Greg Ungerer

[permalink] [raw]
Subject: Re: [PATCH v6 3/6] m68k: coldfire: Add clk_get_optional() function

Hi Christoph,

On 30/11/18 2:32 am, Christoph Hellwig wrote:
> On Thu, Nov 29, 2018 at 09:54:37PM +1000, Greg Ungerer wrote:
>> Hi Phil,
>>
>> On 17/11/18 12:59 am, Phil Edworthy wrote:
>>> clk_get_optional() returns NULL if not found instead of -ENOENT,
>>> otherwise the behaviour is the same as clk_get().
>>>
>>> Signed-off-by: Phil Edworthy <[email protected]>
>>
>> Acked-by: Greg Ungerer <gerg@@linux-m68k.org>
>>
>> Looks good. Do you want me to take this in the m68knommu git tree?
>> Or is the whole series going through some other tree?
>
> Any chance we could just get coldfire moved over to the common clock
> framework?

Sure, I will have a look at it.

Regards
Greg