clk: sunxi-ng: mux: Re-adjust parent rate
authorMaxime Ripard <maxime.ripard@free-electrons.com>
Wed, 17 May 2017 07:40:36 +0000 (09:40 +0200)
committerMaxime Ripard <maxime.ripard@free-electrons.com>
Wed, 7 Jun 2017 13:32:14 +0000 (15:32 +0200)
Currently, the parent rate given back to the clock framework in our
request is the original parent rate we calculated before trying to round
the rate of our clock.

This works fine unless our clock also changes its parent rate, in which
case we will simply ignore that change and still use the previous parent
rate.

Create a new function to re-adjust the parent rate to take the pre-dividers
into account, and give that back to the clock framework.

Signed-off-by: Maxime Ripard <maxime.ripard@free-electrons.com>
Signed-off-by: Chen-Yu Tsai <wens@csie.org>
drivers/clk/sunxi-ng/ccu_mux.c

index c332109725814989986ffe54d630f60645611cc7..748b172f91936d5dc0d16d83f3d2b7d033a700ca 100644 (file)
@@ -64,6 +64,14 @@ unsigned long ccu_mux_helper_apply_prediv(struct ccu_common *common,
        return parent_rate / ccu_mux_get_prediv(common, cm, parent_index);
 }
 
+unsigned long ccu_mux_helper_unapply_prediv(struct ccu_common *common,
+                                           struct ccu_mux_internal *cm,
+                                           int parent_index,
+                                           unsigned long parent_rate)
+{
+       return parent_rate * ccu_mux_get_prediv(common, cm, parent_index);
+}
+
 int ccu_mux_helper_determine_rate(struct ccu_common *common,
                                  struct ccu_mux_internal *cm,
                                  struct clk_rate_request *req,
@@ -89,22 +97,37 @@ int ccu_mux_helper_determine_rate(struct ccu_common *common,
                best_rate = round(cm, best_parent, &adj_parent_rate,
                                  req->rate, data);
 
+               /*
+                * adj_parent_rate might have been modified by our clock.
+                * Unapply the pre-divider if there's one, and give
+                * the actual frequency the parent needs to run at.
+                */
+               best_parent_rate = ccu_mux_helper_unapply_prediv(common, cm, -1,
+                                                                adj_parent_rate);
+
                goto out;
        }
 
        for (i = 0; i < clk_hw_get_num_parents(hw); i++) {
-               unsigned long tmp_rate, parent_rate, adj_parent_rate;
+               unsigned long tmp_rate, parent_rate;
                struct clk_hw *parent;
 
                parent = clk_hw_get_parent_by_index(hw, i);
                if (!parent)
                        continue;
 
-               parent_rate = clk_hw_get_rate(parent);
-               adj_parent_rate = ccu_mux_helper_apply_prediv(common, cm, i,
-                                                             parent_rate);
+               parent_rate = ccu_mux_helper_apply_prediv(common, cm, i,
+                                                         clk_hw_get_rate(parent));
+
+               tmp_rate = round(cm, parent, &parent_rate, req->rate, data);
 
-               tmp_rate = round(cm, parent, &adj_parent_rate, req->rate, data);
+               /*
+                * parent_rate might have been modified by our clock.
+                * Unapply the pre-divider if there's one, and give
+                * the actual frequency the parent needs to run at.
+                */
+               parent_rate = ccu_mux_helper_unapply_prediv(common, cm, i,
+                                                           parent_rate);
                if (tmp_rate == req->rate) {
                        best_parent = parent;
                        best_parent_rate = parent_rate;