* [PATCH 1/2] clk: Add clk_set_rate_safe() API support
@ 2018-04-28 9:16 Shawn Lin
2018-04-28 9:16 ` [PATCH 2/2] clk: clk-conf: Convert to use clk_set_rate_safe() Shawn Lin
2018-06-02 6:43 ` [PATCH 1/2] clk: Add clk_set_rate_safe() API support Stephen Boyd
0 siblings, 2 replies; 4+ messages in thread
From: Shawn Lin @ 2018-04-28 9:16 UTC (permalink / raw)
To: Michael Turquette, Stephen Boyd
Cc: Heiko Stuebner, linux-clk, Elaine Zhang, Shawn Lin
From: Elaine Zhang <zhangqing@rock-chips.com>
Compared with clk_set_rate(), this API would maintain the clock rate
of its children to avoid exception if the new rate is higher than
the old rate.
Signed-off-by: Elaine Zhang <zhangqing@rock-chips.com>
Signed-off-by: Shawn Lin <shawn.lin@rock-chips.com>
---
drivers/clk/clk.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
include/linux/clk.h | 15 +++++++++++++++
2 files changed, 66 insertions(+)
diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
index 9ae92aa..7dec6a8 100644
--- a/drivers/clk/clk.c
+++ b/drivers/clk/clk.c
@@ -2013,6 +2013,57 @@ int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
/**
+ * clk_set_rate_safe - specify a new rate for clk
+ * @clk: the clk whose rate is being changed
+ * @new_rate: the new rate for clk
+ *
+ * Compared with clk_set_rate, this API would also make sure children
+ * of @clk are in the safe clock rate range by maintaining the original
+ * clock rate.
+ *
+ * When changing the parent's clock rate in some cases, we should make
+ * sure the children's clock rate are still in the safe range, namely
+ * [clk->min_rate, clk->max_rate], which is especially important
+ * if its child is a system bus. In general, the lower clock rate the
+ * safer, so simply walk through the parent's clock chain to maintain
+ * the children's clock rate if the new clock rate is higher than the
+ * older one.
+ *
+ * Returns 0 on success, -EERROR otherwise.
+ */
+int clk_set_rate_safe(struct clk *clk, unsigned long new_rate)
+{
+ unsigned long old_rate, child_rate;
+ struct hlist_node *hn;
+ struct clk_core *core;
+ int div, ret;
+
+ if (!clk)
+ return 0;
+
+ old_rate = clk_get_rate(clk);
+
+ if (new_rate <= old_rate)
+ goto set_rate;
+
+ hlist_for_each_entry_safe(core, hn, &clk->core->children,
+ child_node) {
+ if (strcmp(core->parent->name, clk->core->name))
+ continue;
+
+ child_rate = core->rate;
+ div = DIV_ROUND_UP_ULL((u64)new_rate, old_rate);
+ core->rate = child_rate / div;
+ clk_change_rate(core);
+ }
+
+set_rate:
+ ret = clk_set_rate(clk, new_rate);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(clk_set_rate_safe);
+
+/**
* clk_set_rate_range - set a rate range for a clock source
* @clk: clock source
* @min: desired minimum clock rate in Hz, inclusive
diff --git a/include/linux/clk.h b/include/linux/clk.h
index 0dbd088..244d030 100644
--- a/include/linux/clk.h
+++ b/include/linux/clk.h
@@ -505,6 +505,16 @@ int __must_check clk_bulk_enable(int num_clks,
int clk_set_rate(struct clk *clk, unsigned long rate);
/**
+ * clk_set_rate_safe - set the clock rate for a clock source and make sure
+ * its children still maintain the original rate
+ * @clk: clock source
+ * @rate: desired clock rate in Hz
+ *
+ * Returns success (0) or negative errno.
+ */
+int clk_set_rate_safe(struct clk *clk, unsigned long rate);
+
+/**
* clk_set_rate_exclusive- set the clock rate and claim exclusivity over
* clock source
* @clk: clock source
@@ -671,6 +681,11 @@ static inline int clk_set_rate_exclusive(struct clk *clk, unsigned long rate)
return 0;
}
+static inline int clk_set_rate_safe(struct clk *clk, unsigned long rate)
+{
+ return 0;
+}
+
static inline long clk_round_rate(struct clk *clk, unsigned long rate)
{
return 0;
--
1.9.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* [PATCH 2/2] clk: clk-conf: Convert to use clk_set_rate_safe()
2018-04-28 9:16 [PATCH 1/2] clk: Add clk_set_rate_safe() API support Shawn Lin
@ 2018-04-28 9:16 ` Shawn Lin
2018-06-02 6:43 ` [PATCH 1/2] clk: Add clk_set_rate_safe() API support Stephen Boyd
1 sibling, 0 replies; 4+ messages in thread
From: Shawn Lin @ 2018-04-28 9:16 UTC (permalink / raw)
To: Michael Turquette, Stephen Boyd
Cc: Heiko Stuebner, linux-clk, Elaine Zhang, Shawn Lin
If assigning clock rate from firmware, make sure any assignment
is safety for the its children.
Signed-off-by: Shawn Lin <shawn.lin@rock-chips.com>
---
drivers/clk/clk-conf.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/clk/clk-conf.c b/drivers/clk/clk-conf.c
index 49819b5..2dd0305 100644
--- a/drivers/clk/clk-conf.c
+++ b/drivers/clk/clk-conf.c
@@ -107,7 +107,7 @@ static int __set_clk_rates(struct device_node *node, bool clk_supplier)
return PTR_ERR(clk);
}
- rc = clk_set_rate(clk, rate);
+ rc = clk_set_rate_safe(clk, rate);
if (rc < 0)
pr_err("clk: couldn't set %s clk rate to %u (%d), current rate: %lu\n",
__clk_get_name(clk), rate, rc,
--
1.9.1
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH 1/2] clk: Add clk_set_rate_safe() API support
2018-04-28 9:16 [PATCH 1/2] clk: Add clk_set_rate_safe() API support Shawn Lin
2018-04-28 9:16 ` [PATCH 2/2] clk: clk-conf: Convert to use clk_set_rate_safe() Shawn Lin
@ 2018-06-02 6:43 ` Stephen Boyd
2018-06-07 2:11 ` Shawn Lin
1 sibling, 1 reply; 4+ messages in thread
From: Stephen Boyd @ 2018-06-02 6:43 UTC (permalink / raw)
To: Michael Turquette, Shawn Lin
Cc: Heiko Stuebner, linux-clk, Elaine Zhang, Shawn Lin
Quoting Shawn Lin (2018-04-28 02:16:57)
> From: Elaine Zhang <zhangqing@rock-chips.com>
> =
> Compared with clk_set_rate(), this API would maintain the clock rate
> of its children to avoid exception if the new rate is higher than
> the old rate.
> =
> Signed-off-by: Elaine Zhang <zhangqing@rock-chips.com>
> Signed-off-by: Shawn Lin <shawn.lin@rock-chips.com>
> ---
There's no cover letter, and the length and quality of the commit
message leaves me with more questions than answers. Please resend with a
proper argument for this patch set.
My first question is why can't we fix clk_set_rate() or
clk_set_rate_range() APIs to do what you want?
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH 1/2] clk: Add clk_set_rate_safe() API support
2018-06-02 6:43 ` [PATCH 1/2] clk: Add clk_set_rate_safe() API support Stephen Boyd
@ 2018-06-07 2:11 ` Shawn Lin
0 siblings, 0 replies; 4+ messages in thread
From: Shawn Lin @ 2018-06-07 2:11 UTC (permalink / raw)
To: Stephen Boyd, Michael Turquette
Cc: shawn.lin, Heiko Stuebner, linux-clk, Elaine Zhang
Hi Stephen,
On 2018/6/2 14:43, Stephen Boyd wrote:
> Quoting Shawn Lin (2018-04-28 02:16:57)
>> From: Elaine Zhang <zhangqing@rock-chips.com>
>>
>> Compared with clk_set_rate(), this API would maintain the clock rate
>> of its children to avoid exception if the new rate is higher than
>> the old rate.
>>
>> Signed-off-by: Elaine Zhang <zhangqing@rock-chips.com>
>> Signed-off-by: Shawn Lin <shawn.lin@rock-chips.com>
>> ---
>
> There's no cover letter, and the length and quality of the commit
> message leaves me with more questions than answers. Please resend with a
> proper argument for this patch set.
Sure.
>
> My first question is why can't we fix clk_set_rate() or
> clk_set_rate_range() APIs to do what you want?
Yep, I was thinking if there are any drivers/instances implicitly
set child's rate depending on seting a higher rate for its
parent?
But now I think if that happens, we need fixes for the
drivers/instances, as clk_set_rate*() didn't guarantee this. So we could
do the right constraint for clk_set_rate() or clk_set_rate_range().
Will update V2.
>
>
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2018-06-07 2:11 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-28 9:16 [PATCH 1/2] clk: Add clk_set_rate_safe() API support Shawn Lin
2018-04-28 9:16 ` [PATCH 2/2] clk: clk-conf: Convert to use clk_set_rate_safe() Shawn Lin
2018-06-02 6:43 ` [PATCH 1/2] clk: Add clk_set_rate_safe() API support Stephen Boyd
2018-06-07 2:11 ` Shawn Lin
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.