ipq: more v4.9 fixes
[openwrt/staging/blogic.git] / target / linux / ipq806x / patches-4.9 / 0037-clk-Add-safe-switch-hook.patch
1 From a1adfb782789ae9b25c928dfe3d639288563a86c Mon Sep 17 00:00:00 2001
2 From: Stephen Boyd <sboyd@codeaurora.org>
3 Date: Fri, 20 Mar 2015 23:45:23 -0700
4 Subject: [PATCH 37/69] clk: Add safe switch hook
5
6 Sometimes clocks can't accept their parent source turning off
7 while the source is reprogrammed to a different rate. Most
8 notably CPU clocks require a way to switch away from the current
9 PLL they're running on, reprogram that PLL to a new rate, and
10 then switch back to the PLL with the new rate once they're done.
11 Add a hook that drivers can implement allowing them to return a
12 'safe parent' and 'safe frequency' that they can switch their
13 parent to while the upstream source is reprogrammed to support
14 this.
15
16 Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>
17 Signed-off-by: Georgi Djakov <georgi.djakov@linaro.org>
18 ---
19 drivers/clk/clk.c | 73 +++++++++++++++++++++++++++++++++++++++++---
20 include/linux/clk-provider.h | 2 ++
21 2 files changed, 70 insertions(+), 5 deletions(-)
22
23 diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
24 index b08404e07984..ece4fa171d46 100644
25 --- a/drivers/clk/clk.c
26 +++ b/drivers/clk/clk.c
27 @@ -51,9 +51,13 @@ struct clk_core {
28 struct clk_core **parents;
29 u8 num_parents;
30 u8 new_parent_index;
31 + u8 safe_parent_index;
32 unsigned long rate;
33 unsigned long req_rate;
34 + unsigned long old_rate;
35 unsigned long new_rate;
36 + unsigned long safe_freq;
37 + struct clk_core *safe_parent;
38 struct clk_core *new_parent;
39 struct clk_core *new_child;
40 unsigned long flags;
41 @@ -1310,7 +1314,9 @@ static int __clk_speculate_rates(struct clk_core *core,
42 static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
43 struct clk_core *new_parent, u8 p_index)
44 {
45 - struct clk_core *child;
46 + struct clk_core *child, *parent;
47 + struct clk_hw *parent_hw;
48 + unsigned long safe_freq = 0;
49
50 core->new_rate = new_rate;
51 core->new_parent = new_parent;
52 @@ -1320,6 +1326,23 @@ static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
53 if (new_parent && new_parent != core->parent)
54 new_parent->new_child = core;
55
56 + if (core->ops->get_safe_parent) {
57 + parent_hw = core->ops->get_safe_parent(core->hw, &safe_freq);
58 + if (parent_hw) {
59 + parent = parent_hw->core;
60 + p_index = clk_fetch_parent_index(core, parent);
61 + core->safe_parent_index = p_index;
62 + core->safe_parent = parent;
63 + if (safe_freq)
64 + core->safe_freq = safe_freq;
65 + else
66 + core->safe_freq = 0;
67 + }
68 + } else {
69 + core->safe_parent = NULL;
70 + core->safe_freq = 0;
71 + }
72 +
73 hlist_for_each_entry(child, &core->children, child_node) {
74 child->new_rate = clk_recalc(child, new_rate);
75 clk_calc_subtree(child, child->new_rate, NULL, 0);
76 @@ -1432,14 +1455,51 @@ static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
77 unsigned long event)
78 {
79 struct clk_core *child, *tmp_clk, *fail_clk = NULL;
80 + struct clk_core *old_parent;
81 int ret = NOTIFY_DONE;
82
83 - if (core->rate == core->new_rate)
84 + if (core->rate == core->new_rate && event != POST_RATE_CHANGE)
85 return NULL;
86
87 + switch (event) {
88 + case PRE_RATE_CHANGE:
89 + if (core->safe_parent) {
90 + if (core->safe_freq)
91 + core->ops->set_rate_and_parent(core->hw,
92 + core->safe_freq,
93 + core->safe_parent->rate,
94 + core->safe_parent_index);
95 + else
96 + core->ops->set_parent(core->hw,
97 + core->safe_parent_index);
98 + }
99 + core->old_rate = core->rate;
100 + break;
101 + case POST_RATE_CHANGE:
102 + if (core->safe_parent) {
103 + old_parent = __clk_set_parent_before(core,
104 + core->new_parent);
105 + if (core->ops->set_rate_and_parent) {
106 + core->ops->set_rate_and_parent(core->hw,
107 + core->new_rate,
108 + core->new_parent ?
109 + core->new_parent->rate : 0,
110 + core->new_parent_index);
111 + } else if (core->ops->set_parent) {
112 + core->ops->set_parent(core->hw,
113 + core->new_parent_index);
114 + }
115 + __clk_set_parent_after(core, core->new_parent,
116 + old_parent);
117 + }
118 + break;
119 + }
120 +
121 if (core->notifier_count) {
122 - ret = __clk_notify(core, event, core->rate, core->new_rate);
123 - if (ret & NOTIFY_STOP_MASK)
124 + if (event != POST_RATE_CHANGE || core->old_rate != core->rate)
125 + ret = __clk_notify(core, event, core->old_rate,
126 + core->new_rate);
127 + if (ret & NOTIFY_STOP_MASK && event != POST_RATE_CHANGE)
128 fail_clk = core;
129 }
130
131 @@ -1495,7 +1555,8 @@ clk_change_rate(struct clk_core *core, unsigned long best_parent_rate)
132 clk_enable_unlock(flags);
133 }
134
135 - if (core->new_parent && core->new_parent != core->parent) {
136 + if (core->new_parent && core->new_parent != core->parent &&
137 + !core->safe_parent) {
138 old_parent = __clk_set_parent_before(core, core->new_parent);
139 trace_clk_set_parent(core, core->new_parent);
140
141 @@ -1601,6 +1662,8 @@ static int clk_core_set_rate_nolock(struct clk_core *core,
142
143 core->req_rate = req_rate;
144
145 + clk_propagate_rate_change(top, POST_RATE_CHANGE);
146 +
147 return 0;
148 }
149
150 diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h
151 index 9c15cd478876..31fb4eb4294a 100644
152 --- a/include/linux/clk-provider.h
153 +++ b/include/linux/clk-provider.h
154 @@ -206,6 +206,8 @@ struct clk_ops {
155 struct clk_rate_request *req);
156 int (*set_parent)(struct clk_hw *hw, u8 index);
157 u8 (*get_parent)(struct clk_hw *hw);
158 + struct clk_hw *(*get_safe_parent)(struct clk_hw *hw,
159 + unsigned long *safe_freq);
160 int (*set_rate)(struct clk_hw *hw, unsigned long rate,
161 unsigned long parent_rate);
162 int (*set_rate_and_parent)(struct clk_hw *hw,
163 --
164 2.11.0
165