kernel: Move append-dtb to common image-commands
[librecmc/librecmc.git] / target / linux / ipq806x / patches-4.4 / 136-clk-Add-safe-switch-hook.patch
1 From f7a00ea959be31f9b742042294a359d508edce94 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] 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' that they can switch their parent to while the
13 upstream source is reprogrammed to support this.
14
15 Signed-off-by: Stephen Boyd <sboyd@codeaurora.org>
16 Signed-off-by: Ram Chandra Jangir <rjangi@codeaurora.org>
17 ---
18  drivers/clk/clk.c            | 61 ++++++++++++++++++++++++++++++++++++++------
19  include/linux/clk-provider.h |  1 +
20  2 files changed, 54 insertions(+), 8 deletions(-)
21
22 diff --git a/drivers/clk/clk.c b/drivers/clk/clk.c
23 index 8404c3c..a29319a 100644
24 --- a/drivers/clk/clk.c
25 +++ b/drivers/clk/clk.c
26 @@ -51,9 +51,12 @@ struct clk_core {
27         struct clk_core         **parents;
28         u8                      num_parents;
29         u8                      new_parent_index;
30 +       u8                      safe_parent_index;
31         unsigned long           rate;
32         unsigned long           req_rate;
33 +       unsigned long           old_rate;
34         unsigned long           new_rate;
35 +       struct clk_core         *safe_parent;
36         struct clk_core         *new_parent;
37         struct clk_core         *new_child;
38         unsigned long           flags;
39 @@ -1271,7 +1274,8 @@ out:
40  static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
41                              struct clk_core *new_parent, u8 p_index)
42  {
43 -       struct clk_core *child;
44 +       struct clk_core *child, *parent;
45 +       struct clk_hw *parent_hw;
46  
47         core->new_rate = new_rate;
48         core->new_parent = new_parent;
49 @@ -1281,6 +1285,18 @@ static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
50         if (new_parent && new_parent != core->parent)
51                 new_parent->new_child = core;
52  
53 +       if (core->ops->get_safe_parent) {
54 +               parent_hw = core->ops->get_safe_parent(core->hw);
55 +               if (parent_hw) {
56 +                       parent = parent_hw->core;
57 +                       p_index = clk_fetch_parent_index(core, parent);
58 +                       core->safe_parent_index = p_index;
59 +                       core->safe_parent = parent;
60 +               }
61 +       } else {
62 +               core->safe_parent = NULL;
63 +       }
64 +
65         hlist_for_each_entry(child, &core->children, child_node) {
66                 child->new_rate = clk_recalc(child, new_rate);
67                 clk_calc_subtree(child, child->new_rate, NULL, 0);
68 @@ -1393,14 +1409,43 @@ static struct clk_core *clk_propagate_rate_change(struct clk_core *core,
69                                                   unsigned long event)
70  {
71         struct clk_core *child, *tmp_clk, *fail_clk = NULL;
72 +       struct clk_core *old_parent;
73         int ret = NOTIFY_DONE;
74  
75 -       if (core->rate == core->new_rate)
76 +       if (core->rate == core->new_rate && event != POST_RATE_CHANGE)
77                 return NULL;
78  
79 +       switch (event) {
80 +       case PRE_RATE_CHANGE:
81 +               if (core->safe_parent)
82 +                       core->ops->set_parent(core->hw, core->safe_parent_index);
83 +               core->old_rate = core->rate;
84 +               break;
85 +       case POST_RATE_CHANGE:
86 +               if (core->safe_parent) {
87 +                       old_parent = __clk_set_parent_before(core,
88 +                                                           core->new_parent);
89 +                       if (core->ops->set_rate_and_parent) {
90 +                               core->ops->set_rate_and_parent(core->hw,
91 +                                               core->new_rate,
92 +                                               core->new_parent ?
93 +                                               core->new_parent->rate : 0,
94 +                                               core->new_parent_index);
95 +                       } else if (core->ops->set_parent) {
96 +                               core->ops->set_parent(core->hw,
97 +                                               core->new_parent_index);
98 +                       }
99 +                       __clk_set_parent_after(core, core->new_parent,
100 +                                               old_parent);
101 +               }
102 +               break;
103 +       }
104 +
105         if (core->notifier_count) {
106 -               ret = __clk_notify(core, event, core->rate, core->new_rate);
107 -               if (ret & NOTIFY_STOP_MASK)
108 +               if (event != POST_RATE_CHANGE || core->old_rate != core->rate)
109 +                       ret = __clk_notify(core, event, core->old_rate,
110 +                                          core->new_rate);
111 +               if (ret & NOTIFY_STOP_MASK && event != POST_RATE_CHANGE)
112                         fail_clk = core;
113         }
114  
115 @@ -1446,7 +1491,8 @@ clk_change_rate(struct clk_core *core, unsigned long best_parent_rate)
116  
117         old_rate = core->rate;
118  
119 -       if (core->new_parent && core->new_parent != core->parent) {
120 +       if (core->new_parent && core->new_parent != core->parent &&
121 +                       !core->safe_parent) {
122                 old_parent = __clk_set_parent_before(core, core->new_parent);
123                 trace_clk_set_parent(core, core->new_parent);
124  
125 @@ -1472,9 +1518,6 @@ clk_change_rate(struct clk_core *core, unsigned long best_parent_rate)
126  
127         core->rate = core->new_rate;
128  
129 -       if (core->notifier_count && old_rate != core->rate)
130 -               __clk_notify(core, POST_RATE_CHANGE, old_rate, core->rate);
131 -
132         if (core->flags & CLK_RECALC_NEW_RATES)
133                 (void)clk_calc_new_rates(core, core->new_rate);
134  
135 @@ -1537,6 +1580,8 @@ static int clk_core_set_rate_nolock(struct clk_core *core,
136  
137         core->req_rate = req_rate;
138  
139 +       clk_propagate_rate_change(top, POST_RATE_CHANGE);
140 +
141         return ret;
142  }
143  
144 diff --git a/include/linux/clk-provider.h b/include/linux/clk-provider.h
145 index b6b17b5..5d49262 100644
146 --- a/include/linux/clk-provider.h
147 +++ b/include/linux/clk-provider.h
148 @@ -202,6 +202,7 @@ struct clk_ops {
149                                           struct clk_rate_request *req);
150         int             (*set_parent)(struct clk_hw *hw, u8 index);
151         u8              (*get_parent)(struct clk_hw *hw);
152 +       struct clk_hw   *(*get_safe_parent)(struct clk_hw *hw);
153         int             (*set_rate)(struct clk_hw *hw, unsigned long rate,
154                                     unsigned long parent_rate);
155         int             (*set_rate_and_parent)(struct clk_hw *hw,
156 -- 
157 2.7.2
158