omap: Fix access to already released memory in clk_debugfs_register_one()
[safe/jmp/linux-2.6] / arch / arm / plat-omap / clock.c
index df58f5d..4becbdd 100644 (file)
@@ -24,7 +24,7 @@
 #include <linux/debugfs.h>
 #include <linux/io.h>
 
-#include <mach/clock.h>
+#include <plat/clock.h>
 
 static LIST_HEAD(clocks);
 static DEFINE_MUTEX(clocks_mutex);
@@ -36,43 +36,6 @@ static struct clk_functions *arch_clock;
  * Standard clock functions defined in include/linux/clk.h
  *-------------------------------------------------------------------------*/
 
-/*
- * Returns a clock. Note that we first try to use device id on the bus
- * and clock name. If this fails, we try to use clock name only.
- */
-struct clk * clk_get(struct device *dev, const char *id)
-{
-       struct clk *p, *clk = ERR_PTR(-ENOENT);
-       int idno;
-
-       if (dev == NULL || dev->bus != &platform_bus_type)
-               idno = -1;
-       else
-               idno = to_platform_device(dev)->id;
-
-       mutex_lock(&clocks_mutex);
-
-       list_for_each_entry(p, &clocks, node) {
-               if (p->id == idno && strcmp(id, p->name) == 0) {
-                       clk = p;
-                       goto found;
-               }
-       }
-
-       list_for_each_entry(p, &clocks, node) {
-               if (strcmp(id, p->name) == 0) {
-                       clk = p;
-                       break;
-               }
-       }
-
-found:
-       mutex_unlock(&clocks_mutex);
-
-       return clk;
-}
-EXPORT_SYMBOL(clk_get);
-
 int clk_enable(struct clk *clk)
 {
        unsigned long flags;
@@ -113,22 +76,6 @@ out:
 }
 EXPORT_SYMBOL(clk_disable);
 
-int clk_get_usecount(struct clk *clk)
-{
-       unsigned long flags;
-       int ret = 0;
-
-       if (clk == NULL || IS_ERR(clk))
-               return 0;
-
-       spin_lock_irqsave(&clockfw_lock, flags);
-       ret = clk->usecount;
-       spin_unlock_irqrestore(&clockfw_lock, flags);
-
-       return ret;
-}
-EXPORT_SYMBOL(clk_get_usecount);
-
 unsigned long clk_get_rate(struct clk *clk)
 {
        unsigned long flags;
@@ -145,11 +92,6 @@ unsigned long clk_get_rate(struct clk *clk)
 }
 EXPORT_SYMBOL(clk_get_rate);
 
-void clk_put(struct clk *clk)
-{
-}
-EXPORT_SYMBOL(clk_put);
-
 /*-------------------------------------------------------------------------
  * Optional clock functions defined in include/linux/clk.h
  *-------------------------------------------------------------------------*/
@@ -182,8 +124,11 @@ int clk_set_rate(struct clk *clk, unsigned long rate)
        spin_lock_irqsave(&clockfw_lock, flags);
        if (arch_clock->clk_set_rate)
                ret = arch_clock->clk_set_rate(clk, rate);
-       if (ret == 0 && (clk->flags & RATE_PROPAGATES))
+       if (ret == 0) {
+               if (clk->recalc)
+                       clk->rate = clk->recalc(clk);
                propagate_rate(clk);
+       }
        spin_unlock_irqrestore(&clockfw_lock, flags);
 
        return ret;
@@ -195,14 +140,23 @@ int clk_set_parent(struct clk *clk, struct clk *parent)
        unsigned long flags;
        int ret = -EINVAL;
 
+       if (cpu_is_omap44xx())
+       /* OMAP4 clk framework not supported yet */
+               return 0;
        if (clk == NULL || IS_ERR(clk) || parent == NULL || IS_ERR(parent))
                return ret;
 
        spin_lock_irqsave(&clockfw_lock, flags);
-       if (arch_clock->clk_set_parent)
-               ret = arch_clock->clk_set_parent(clk, parent);
-       if (ret == 0 && (clk->flags & RATE_PROPAGATES))
-               propagate_rate(clk);
+       if (clk->usecount == 0) {
+               if (arch_clock->clk_set_parent)
+                       ret = arch_clock->clk_set_parent(clk, parent);
+               if (ret == 0) {
+                       if (clk->recalc)
+                               clk->rate = clk->recalc(clk);
+                       propagate_rate(clk);
+               }
+       } else
+               ret = -EBUSY;
        spin_unlock_irqrestore(&clockfw_lock, flags);
 
        return ret;
@@ -240,12 +194,20 @@ static int __init omap_clk_setup(char *str)
 __setup("mpurate=", omap_clk_setup);
 
 /* Used for clocks that always have same value as the parent clock */
-void followparent_recalc(struct clk *clk)
+unsigned long followparent_recalc(struct clk *clk)
 {
-       if (clk == NULL || IS_ERR(clk))
-               return;
+       return clk->parent->rate;
+}
+
+void clk_reparent(struct clk *child, struct clk *parent)
+{
+       list_del_init(&child->sibling);
+       if (parent)
+               list_add(&child->sibling, &parent->children);
+       child->parent = parent;
 
-       clk->rate = clk->parent->rate;
+       /* now do the debugfs renaming to reattach the child
+          to the proper parent */
 }
 
 /* Propagate rate to children */
@@ -253,19 +215,15 @@ void propagate_rate(struct clk * tclk)
 {
        struct clk *clkp;
 
-       if (tclk == NULL || IS_ERR(tclk))
-               return;
-
-       list_for_each_entry(clkp, &clocks, node) {
-               if (likely(clkp->parent != tclk))
-                       continue;
+       list_for_each_entry(clkp, &tclk->children, sibling) {
                if (clkp->recalc)
-                       clkp->recalc(clkp);
-               if (clkp->flags & RATE_PROPAGATES)
-                       propagate_rate(clkp);
+                       clkp->rate = clkp->recalc(clkp);
+               propagate_rate(clkp);
        }
 }
 
+static LIST_HEAD(root_clks);
+
 /**
  * recalculate_root_clocks - recalculate and propagate all root clocks
  *
@@ -277,22 +235,42 @@ void recalculate_root_clocks(void)
 {
        struct clk *clkp;
 
-       list_for_each_entry(clkp, &clocks, node) {
-               if (!clkp->parent) {
-                       if (clkp->recalc)
-                               clkp->recalc(clkp);
-                       if (clkp->flags & RATE_PROPAGATES)
-                               propagate_rate(clkp);
-               }
+       list_for_each_entry(clkp, &root_clks, sibling) {
+               if (clkp->recalc)
+                       clkp->rate = clkp->recalc(clkp);
+               propagate_rate(clkp);
        }
 }
 
+/**
+ * clk_preinit - initialize any fields in the struct clk before clk init
+ * @clk: struct clk * to initialize
+ *
+ * Initialize any struct clk fields needed before normal clk initialization
+ * can run.  No return value.
+ */
+void clk_preinit(struct clk *clk)
+{
+       INIT_LIST_HEAD(&clk->children);
+}
+
 int clk_register(struct clk *clk)
 {
        if (clk == NULL || IS_ERR(clk))
                return -EINVAL;
 
+       /*
+        * trap out already registered clocks
+        */
+       if (clk->node.next || clk->node.prev)
+               return 0;
+
        mutex_lock(&clocks_mutex);
+       if (clk->parent)
+               list_add(&clk->sibling, &clk->parent->children);
+       else
+               list_add(&clk->sibling, &root_clks);
+
        list_add(&clk->node, &clocks);
        if (clk->init)
                clk->init(clk);
@@ -308,6 +286,7 @@ void clk_unregister(struct clk *clk)
                return;
 
        mutex_lock(&clocks_mutex);
+       list_del(&clk->sibling);
        list_del(&clk->node);
        mutex_unlock(&clocks_mutex);
 }
@@ -322,7 +301,6 @@ void clk_enable_init_clocks(void)
                        clk_enable(clkp);
        }
 }
-EXPORT_SYMBOL(clk_enable_init_clocks);
 
 /*
  * Low level helpers
@@ -351,7 +329,16 @@ void clk_init_cpufreq_table(struct cpufreq_frequency_table **table)
                arch_clock->clk_init_cpufreq_table(table);
        spin_unlock_irqrestore(&clockfw_lock, flags);
 }
-EXPORT_SYMBOL(clk_init_cpufreq_table);
+
+void clk_exit_cpufreq_table(struct cpufreq_frequency_table **table)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&clockfw_lock, flags);
+       if (arch_clock->clk_exit_cpufreq_table)
+               arch_clock->clk_exit_cpufreq_table(table);
+       spin_unlock_irqrestore(&clockfw_lock, flags);
+}
 #endif
 
 /*-------------------------------------------------------------------------*/
@@ -404,7 +391,7 @@ static struct dentry *clk_debugfs_root;
 static int clk_debugfs_register_one(struct clk *c)
 {
        int err;
-       struct dentry *d, *child;
+       struct dentry *d, *child, *child_tmp;
        struct clk *pa = c->parent;
        char s[255];
        char *p = s;
@@ -436,7 +423,7 @@ static int clk_debugfs_register_one(struct clk *c)
 
 err_out:
        d = c->dent;
-       list_for_each_entry(child, &d->d_subdirs, d_u.d_child)
+       list_for_each_entry_safe(child, child_tmp, &d->d_subdirs, d_u.d_child)
                debugfs_remove(child);
        debugfs_remove(c->dent);
        return err;
@@ -479,7 +466,7 @@ static int __init clk_debugfs_init(void)
        }
        return 0;
 err_out:
-       debugfs_remove(clk_debugfs_root); /* REVISIT: Cleanup correctly */
+       debugfs_remove_recursive(clk_debugfs_root);
        return err;
 }
 late_initcall(clk_debugfs_init);