Merge branch 'clk-has-parent' into clk-next
[sfrench/cifs-2.6.git] / drivers / clk / clk.c
index 4896ae9e23da0c23ed52872cf1992f95609e1b59..7ba02e52c5549569fe4e852ff618e37543fe595d 100644 (file)
@@ -240,7 +240,6 @@ static const struct file_operations clk_dump_fops = {
        .release        = single_release,
 };
 
-/* caller must hold prepare_lock */
 static int clk_debug_create_one(struct clk *clk, struct dentry *pdentry)
 {
        struct dentry *d;
@@ -354,13 +353,13 @@ out:
        mutex_unlock(&clk_debug_lock);
 }
 
-struct dentry *clk_debugfs_add_file(struct clk *clk, char *name, umode_t mode,
+struct dentry *clk_debugfs_add_file(struct clk_hw *hw, char *name, umode_t mode,
                                void *data, const struct file_operations *fops)
 {
        struct dentry *d = NULL;
 
-       if (clk->dentry)
-               d = debugfs_create_file(name, mode, clk->dentry, data, fops);
+       if (hw->clk->dentry)
+               d = debugfs_create_file(name, mode, hw->clk->dentry, data, fops);
 
        return d;
 }
@@ -574,11 +573,6 @@ unsigned int __clk_get_enable_count(struct clk *clk)
        return !clk ? 0 : clk->enable_count;
 }
 
-unsigned int __clk_get_prepare_count(struct clk *clk)
-{
-       return !clk ? 0 : clk->prepare_count;
-}
-
 unsigned long __clk_get_rate(struct clk *clk)
 {
        unsigned long ret;
@@ -601,7 +595,7 @@ out:
 }
 EXPORT_SYMBOL_GPL(__clk_get_rate);
 
-unsigned long __clk_get_accuracy(struct clk *clk)
+static unsigned long __clk_get_accuracy(struct clk *clk)
 {
        if (!clk)
                return 0;
@@ -707,7 +701,7 @@ struct clk *__clk_lookup(const char *name)
  */
 long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
                              unsigned long *best_parent_rate,
-                             struct clk **best_parent_p)
+                             struct clk_hw **best_parent_p)
 {
        struct clk *clk = hw->clk, *parent, *best_parent = NULL;
        int i, num_parents;
@@ -743,7 +737,7 @@ long __clk_mux_determine_rate(struct clk_hw *hw, unsigned long rate,
 
 out:
        if (best_parent)
-               *best_parent_p = best_parent;
+               *best_parent_p = best_parent->hw;
        *best_parent_rate = best;
 
        return best;
@@ -951,6 +945,7 @@ unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
 {
        unsigned long parent_rate = 0;
        struct clk *parent;
+       struct clk_hw *parent_hw;
 
        if (!clk)
                return 0;
@@ -959,10 +954,11 @@ unsigned long __clk_round_rate(struct clk *clk, unsigned long rate)
        if (parent)
                parent_rate = parent->rate;
 
-       if (clk->ops->determine_rate)
+       if (clk->ops->determine_rate) {
+               parent_hw = parent ? parent->hw : NULL;
                return clk->ops->determine_rate(clk->hw, rate, &parent_rate,
-                                               &parent);
-       else if (clk->ops->round_rate)
+                                               &parent_hw);
+       else if (clk->ops->round_rate)
                return clk->ops->round_rate(clk->hw, rate, &parent_rate);
        else if (clk->flags & CLK_SET_RATE_PARENT)
                return __clk_round_rate(clk->parent, rate);
@@ -1350,6 +1346,7 @@ static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate)
 {
        struct clk *top = clk;
        struct clk *old_parent, *parent;
+       struct clk_hw *parent_hw;
        unsigned long best_parent_rate = 0;
        unsigned long new_rate;
        int p_index = 0;
@@ -1365,9 +1362,11 @@ static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate)
 
        /* find the closest rate and parent clk/rate */
        if (clk->ops->determine_rate) {
+               parent_hw = parent ? parent->hw : NULL;
                new_rate = clk->ops->determine_rate(clk->hw, rate,
                                                    &best_parent_rate,
-                                                   &parent);
+                                                   &parent_hw);
+               parent = parent_hw ? parent_hw->clk : NULL;
        } else if (clk->ops->round_rate) {
                new_rate = clk->ops->round_rate(clk->hw, rate,
                                                &best_parent_rate);
@@ -1391,7 +1390,7 @@ static struct clk *clk_calc_new_rates(struct clk *clk, unsigned long rate)
        }
 
        /* try finding the new parent index */
-       if (parent) {
+       if (parent && clk->num_parents > 1) {
                p_index = clk_fetch_parent_index(clk, parent);
                if (p_index < 0) {
                        pr_debug("%s: clk %s can not be parent of clk %s\n",
@@ -1614,7 +1613,7 @@ static struct clk *__clk_init_parent(struct clk *clk)
 
        if (clk->num_parents == 1) {
                if (IS_ERR_OR_NULL(clk->parent))
-                       ret = clk->parent = __clk_lookup(clk->parent_names[0]);
+                       clk->parent = __clk_lookup(clk->parent_names[0]);
                ret = clk->parent;
                goto out;
        }
@@ -1652,6 +1651,36 @@ void __clk_reparent(struct clk *clk, struct clk *new_parent)
        __clk_recalc_rates(clk, POST_RATE_CHANGE);
 }
 
+/**
+ * clk_has_parent - check if a clock is a possible parent for another
+ * @clk: clock source
+ * @parent: parent clock source
+ *
+ * This function can be used in drivers that need to check that a clock can be
+ * the parent of another without actually changing the parent.
+ *
+ * Returns true if @parent is a possible parent for @clk, false otherwise.
+ */
+bool clk_has_parent(struct clk *clk, struct clk *parent)
+{
+       unsigned int i;
+
+       /* NULL clocks should be nops, so return success if either is NULL. */
+       if (!clk || !parent)
+               return true;
+
+       /* Optimize for the case where the parent is already the parent. */
+       if (clk->parent == parent)
+               return true;
+
+       for (i = 0; i < clk->num_parents; i++)
+               if (strcmp(clk->parent_names[i], parent->name) == 0)
+                       return true;
+
+       return false;
+}
+EXPORT_SYMBOL_GPL(clk_has_parent);
+
 /**
  * clk_set_parent - switch the parent of a mux clk
  * @clk: the mux clk whose input we are switching
@@ -1944,7 +1973,6 @@ int __clk_init(struct device *dev, struct clk *clk)
        else
                clk->rate = 0;
 
-       clk_debug_register(clk);
        /*
         * walk the list of orphan clocks and reparent any that are children of
         * this clock
@@ -1979,6 +2007,9 @@ int __clk_init(struct device *dev, struct clk *clk)
 out:
        clk_prepare_unlock();
 
+       if (!ret)
+               clk_debug_register(clk);
+
        return ret;
 }
 
@@ -2273,14 +2304,17 @@ int __clk_get(struct clk *clk)
 
 void __clk_put(struct clk *clk)
 {
+       struct module *owner;
+
        if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
                return;
 
        clk_prepare_lock();
+       owner = clk->owner;
        kref_put(&clk->ref, __clk_release);
        clk_prepare_unlock();
 
-       module_put(clk->owner);
+       module_put(owner);
 }
 
 /***        clk rate change notifiers        ***/