]> Pileus Git - ~andy/linux/blobdiff - drivers/clk/clk.c
clk: tegra: Fix PLLP rate table
[~andy/linux] / drivers / clk / clk.c
index b7f6e99e61ebff9d49ba457923ad9f68c580d24c..5517944495d893cc3c8dd60569b58e67e9289765 100644 (file)
@@ -21,6 +21,8 @@
 #include <linux/init.h>
 #include <linux/sched.h>
 
+#include "clk.h"
+
 static DEFINE_SPINLOCK(enable_lock);
 static DEFINE_MUTEX(prepare_lock);
 
@@ -350,6 +352,21 @@ out:
        return ret;
 }
 
+ /**
+ * clk_debug_unregister - remove a clk node from the debugfs clk tree
+ * @clk: the clk being removed from the debugfs clk tree
+ *
+ * Dynamically removes a clk and all it's children clk nodes from the
+ * debugfs clk tree if clk->dentry points to debugfs created by
+ * clk_debug_register in __clk_init.
+ *
+ * Caller must hold prepare_lock.
+ */
+static void clk_debug_unregister(struct clk *clk)
+{
+       debugfs_remove_recursive(clk->dentry);
+}
+
 /**
  * clk_debug_reparent - reparent clk node in the debugfs clk tree
  * @clk: the clk being reparented
@@ -440,6 +457,9 @@ static inline int clk_debug_register(struct clk *clk) { return 0; }
 static inline void clk_debug_reparent(struct clk *clk, struct clk *new_parent)
 {
 }
+static inline void clk_debug_unregister(struct clk *clk)
+{
+}
 #endif
 
 /* caller must hold prepare_lock */
@@ -555,16 +575,19 @@ struct clk_hw *__clk_get_hw(struct clk *clk)
 {
        return !clk ? NULL : clk->hw;
 }
+EXPORT_SYMBOL_GPL(__clk_get_hw);
 
 u8 __clk_get_num_parents(struct clk *clk)
 {
        return !clk ? 0 : clk->num_parents;
 }
+EXPORT_SYMBOL_GPL(__clk_get_num_parents);
 
 struct clk *__clk_get_parent(struct clk *clk)
 {
        return !clk ? NULL : clk->parent;
 }
+EXPORT_SYMBOL_GPL(__clk_get_parent);
 
 struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
 {
@@ -578,6 +601,7 @@ struct clk *clk_get_parent_by_index(struct clk *clk, u8 index)
        else
                return clk->parents[index];
 }
+EXPORT_SYMBOL_GPL(clk_get_parent_by_index);
 
 unsigned int __clk_get_enable_count(struct clk *clk)
 {
@@ -609,6 +633,7 @@ unsigned long __clk_get_rate(struct clk *clk)
 out:
        return ret;
 }
+EXPORT_SYMBOL_GPL(__clk_get_rate);
 
 unsigned long __clk_get_accuracy(struct clk *clk)
 {
@@ -665,6 +690,7 @@ bool __clk_is_enabled(struct clk *clk)
 out:
        return !!ret;
 }
+EXPORT_SYMBOL_GPL(__clk_is_enabled);
 
 static struct clk *__clk_lookup_subtree(const char *name, struct clk *clk)
 {
@@ -756,6 +782,7 @@ out:
 
        return best;
 }
+EXPORT_SYMBOL_GPL(__clk_mux_determine_rate);
 
 /***        clk api        ***/
 
@@ -1198,10 +1225,9 @@ static void clk_reparent(struct clk *clk, struct clk *new_parent)
        clk->parent = new_parent;
 }
 
-static int __clk_set_parent(struct clk *clk, struct clk *parent, u8 p_index)
+static struct clk *__clk_set_parent_before(struct clk *clk, struct clk *parent)
 {
        unsigned long flags;
-       int ret = 0;
        struct clk *old_parent = clk->parent;
 
        /*
@@ -1232,6 +1258,34 @@ static int __clk_set_parent(struct clk *clk, struct clk *parent, u8 p_index)
        clk_reparent(clk, parent);
        clk_enable_unlock(flags);
 
+       return old_parent;
+}
+
+static void __clk_set_parent_after(struct clk *clk, struct clk *parent,
+               struct clk *old_parent)
+{
+       /*
+        * Finish the migration of prepare state and undo the changes done
+        * for preventing a race with clk_enable().
+        */
+       if (clk->prepare_count) {
+               clk_disable(clk);
+               clk_disable(old_parent);
+               __clk_unprepare(old_parent);
+       }
+
+       /* update debugfs with new clk tree topology */
+       clk_debug_reparent(clk, parent);
+}
+
+static int __clk_set_parent(struct clk *clk, struct clk *parent, u8 p_index)
+{
+       unsigned long flags;
+       int ret = 0;
+       struct clk *old_parent;
+
+       old_parent = __clk_set_parent_before(clk, parent);
+
        /* change clock input source */
        if (parent && clk->ops->set_parent)
                ret = clk->ops->set_parent(clk->hw, p_index);
@@ -1249,18 +1303,8 @@ static int __clk_set_parent(struct clk *clk, struct clk *parent, u8 p_index)
                return ret;
        }
 
-       /*
-        * Finish the migration of prepare state and undo the changes done
-        * for preventing a race with clk_enable().
-        */
-       if (clk->prepare_count) {
-               clk_disable(clk);
-               clk_disable(old_parent);
-               __clk_unprepare(old_parent);
-       }
+       __clk_set_parent_after(clk, parent, old_parent);
 
-       /* update debugfs with new clk tree topology */
-       clk_debug_reparent(clk, parent);
        return 0;
 }
 
@@ -1445,17 +1489,32 @@ static void clk_change_rate(struct clk *clk)
        struct clk *child;
        unsigned long old_rate;
        unsigned long best_parent_rate = 0;
+       bool skip_set_rate = false;
+       struct clk *old_parent;
 
        old_rate = clk->rate;
 
-       /* set parent */
-       if (clk->new_parent && clk->new_parent != clk->parent)
-               __clk_set_parent(clk, clk->new_parent, clk->new_parent_index);
-
-       if (clk->parent)
+       if (clk->new_parent)
+               best_parent_rate = clk->new_parent->rate;
+       else if (clk->parent)
                best_parent_rate = clk->parent->rate;
 
-       if (clk->ops->set_rate)
+       if (clk->new_parent && clk->new_parent != clk->parent) {
+               old_parent = __clk_set_parent_before(clk, clk->new_parent);
+
+               if (clk->ops->set_rate_and_parent) {
+                       skip_set_rate = true;
+                       clk->ops->set_rate_and_parent(clk->hw, clk->new_rate,
+                                       best_parent_rate,
+                                       clk->new_parent_index);
+               } else if (clk->ops->set_parent) {
+                       clk->ops->set_parent(clk->hw, clk->new_parent_index);
+               }
+
+               __clk_set_parent_after(clk, clk->new_parent, old_parent);
+       }
+
+       if (!skip_set_rate && clk->ops->set_rate)
                clk->ops->set_rate(clk->hw, clk->new_rate, best_parent_rate);
 
        if (clk->ops->recalc_rate)
@@ -1750,6 +1809,14 @@ int __clk_init(struct device *dev, struct clk *clk)
                goto out;
        }
 
+       if (clk->ops->set_rate_and_parent &&
+                       !(clk->ops->set_parent && clk->ops->set_rate)) {
+               pr_warn("%s: %s must implement .set_parent & .set_rate\n",
+                               __func__, clk->name);
+               ret = -EINVAL;
+               goto out;
+       }
+
        /* throw a WARN if any entries in parent_names are NULL */
        for (i = 0; i < clk->num_parents; i++)
                WARN(!clk->parent_names[i],
@@ -1861,6 +1928,7 @@ int __clk_init(struct device *dev, struct clk *clk)
        if (clk->ops->init)
                clk->ops->init(clk->hw);
 
+       kref_init(&clk->ref);
 out:
        clk_prepare_unlock();
 
@@ -1896,6 +1964,10 @@ struct clk *__clk_register(struct device *dev, struct clk_hw *hw)
        clk->flags = hw->init->flags;
        clk->parent_names = hw->init->parent_names;
        clk->num_parents = hw->init->num_parents;
+       if (dev && dev->driver)
+               clk->owner = dev->driver->owner;
+       else
+               clk->owner = NULL;
 
        ret = __clk_init(dev, clk);
        if (ret)
@@ -1916,6 +1988,8 @@ static int _clk_register(struct device *dev, struct clk_hw *hw, struct clk *clk)
                goto fail_name;
        }
        clk->ops = hw->init->ops;
+       if (dev && dev->driver)
+               clk->owner = dev->driver->owner;
        clk->hw = hw;
        clk->flags = hw->init->flags;
        clk->num_parents = hw->init->num_parents;
@@ -1990,13 +2064,104 @@ fail_out:
 }
 EXPORT_SYMBOL_GPL(clk_register);
 
+/*
+ * Free memory allocated for a clock.
+ * Caller must hold prepare_lock.
+ */
+static void __clk_release(struct kref *ref)
+{
+       struct clk *clk = container_of(ref, struct clk, ref);
+       int i = clk->num_parents;
+
+       kfree(clk->parents);
+       while (--i >= 0)
+               kfree(clk->parent_names[i]);
+
+       kfree(clk->parent_names);
+       kfree(clk->name);
+       kfree(clk);
+}
+
+/*
+ * Empty clk_ops for unregistered clocks. These are used temporarily
+ * after clk_unregister() was called on a clock and until last clock
+ * consumer calls clk_put() and the struct clk object is freed.
+ */
+static int clk_nodrv_prepare_enable(struct clk_hw *hw)
+{
+       return -ENXIO;
+}
+
+static void clk_nodrv_disable_unprepare(struct clk_hw *hw)
+{
+       WARN_ON_ONCE(1);
+}
+
+static int clk_nodrv_set_rate(struct clk_hw *hw, unsigned long rate,
+                                       unsigned long parent_rate)
+{
+       return -ENXIO;
+}
+
+static int clk_nodrv_set_parent(struct clk_hw *hw, u8 index)
+{
+       return -ENXIO;
+}
+
+static const struct clk_ops clk_nodrv_ops = {
+       .enable         = clk_nodrv_prepare_enable,
+       .disable        = clk_nodrv_disable_unprepare,
+       .prepare        = clk_nodrv_prepare_enable,
+       .unprepare      = clk_nodrv_disable_unprepare,
+       .set_rate       = clk_nodrv_set_rate,
+       .set_parent     = clk_nodrv_set_parent,
+};
+
 /**
  * clk_unregister - unregister a currently registered clock
  * @clk: clock to unregister
- *
- * Currently unimplemented.
  */
-void clk_unregister(struct clk *clk) {}
+void clk_unregister(struct clk *clk)
+{
+       unsigned long flags;
+
+       if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
+               return;
+
+       clk_prepare_lock();
+
+       if (clk->ops == &clk_nodrv_ops) {
+               pr_err("%s: unregistered clock: %s\n", __func__, clk->name);
+               goto out;
+       }
+       /*
+        * Assign empty clock ops for consumers that might still hold
+        * a reference to this clock.
+        */
+       flags = clk_enable_lock();
+       clk->ops = &clk_nodrv_ops;
+       clk_enable_unlock(flags);
+
+       if (!hlist_empty(&clk->children)) {
+               struct clk *child;
+
+               /* Reparent all children to the orphan list. */
+               hlist_for_each_entry(child, &clk->children, child_node)
+                       clk_set_parent(child, NULL);
+       }
+
+       clk_debug_unregister(clk);
+
+       hlist_del_init(&clk->child_node);
+
+       if (clk->prepare_count)
+               pr_warn("%s: unregistering prepared clock: %s\n",
+                                       __func__, clk->name);
+
+       kref_put(&clk->ref, __clk_release);
+out:
+       clk_prepare_unlock();
+}
 EXPORT_SYMBOL_GPL(clk_unregister);
 
 static void devm_clk_release(struct device *dev, void *res)
@@ -2056,6 +2221,31 @@ void devm_clk_unregister(struct device *dev, struct clk *clk)
 }
 EXPORT_SYMBOL_GPL(devm_clk_unregister);
 
+/*
+ * clkdev helpers
+ */
+int __clk_get(struct clk *clk)
+{
+       if (clk && !try_module_get(clk->owner))
+               return 0;
+
+       kref_get(&clk->ref);
+       return 1;
+}
+
+void __clk_put(struct clk *clk)
+{
+       if (WARN_ON_ONCE(IS_ERR(clk)))
+               return;
+
+       clk_prepare_lock();
+       kref_put(&clk->ref, __clk_release);
+       clk_prepare_unlock();
+
+       if (clk)
+               module_put(clk->owner);
+}
+
 /***        clk rate change notifiers        ***/
 
 /**
@@ -2190,13 +2380,22 @@ struct of_clk_provider {
        void *data;
 };
 
-extern struct of_device_id __clk_of_table[];
-
 static const struct of_device_id __clk_of_table_sentinel
        __used __section(__clk_of_table_end);
 
 static LIST_HEAD(of_clk_providers);
-static DEFINE_MUTEX(of_clk_lock);
+static DEFINE_MUTEX(of_clk_mutex);
+
+/* of_clk_provider list locking helpers */
+void of_clk_lock(void)
+{
+       mutex_lock(&of_clk_mutex);
+}
+
+void of_clk_unlock(void)
+{
+       mutex_unlock(&of_clk_mutex);
+}
 
 struct clk *of_clk_src_simple_get(struct of_phandle_args *clkspec,
                                     void *data)
@@ -2240,9 +2439,9 @@ int of_clk_add_provider(struct device_node *np,
        cp->data = data;
        cp->get = clk_src_get;
 
-       mutex_lock(&of_clk_lock);
+       mutex_lock(&of_clk_mutex);
        list_add(&cp->link, &of_clk_providers);
-       mutex_unlock(&of_clk_lock);
+       mutex_unlock(&of_clk_mutex);
        pr_debug("Added clock from %s\n", np->full_name);
 
        return 0;
@@ -2257,7 +2456,7 @@ void of_clk_del_provider(struct device_node *np)
 {
        struct of_clk_provider *cp;
 
-       mutex_lock(&of_clk_lock);
+       mutex_lock(&of_clk_mutex);
        list_for_each_entry(cp, &of_clk_providers, link) {
                if (cp->node == np) {
                        list_del(&cp->link);
@@ -2266,24 +2465,33 @@ void of_clk_del_provider(struct device_node *np)
                        break;
                }
        }
-       mutex_unlock(&of_clk_lock);
+       mutex_unlock(&of_clk_mutex);
 }
 EXPORT_SYMBOL_GPL(of_clk_del_provider);
 
-struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
+struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec)
 {
        struct of_clk_provider *provider;
        struct clk *clk = ERR_PTR(-ENOENT);
 
        /* Check if we have such a provider in our array */
-       mutex_lock(&of_clk_lock);
        list_for_each_entry(provider, &of_clk_providers, link) {
                if (provider->node == clkspec->np)
                        clk = provider->get(clkspec, provider->data);
                if (!IS_ERR(clk))
                        break;
        }
-       mutex_unlock(&of_clk_lock);
+
+       return clk;
+}
+
+struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
+{
+       struct clk *clk;
+
+       mutex_lock(&of_clk_mutex);
+       clk = __of_clk_get_from_provider(clkspec);
+       mutex_unlock(&of_clk_mutex);
 
        return clk;
 }
@@ -2331,7 +2539,7 @@ void __init of_clk_init(const struct of_device_id *matches)
        struct device_node *np;
 
        if (!matches)
-               matches = __clk_of_table;
+               matches = &__clk_of_table;
 
        for_each_matching_node_and_match(np, matches, &match) {
                of_clk_init_cb_t clk_init_cb = match->data;