]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - arch/arm/plat-omap/clock.c
ARM: OMAP: Fix clk_get() when using id and name
[linux-2.6-omap-h63xx.git] / arch / arm / plat-omap / clock.c
index 7ce39b986e23348622092e3ca8d5627ad0b1af3e..3d47bdcfb25ca42f9103602685de8e31c16151a8 100644 (file)
 #include <linux/errno.h>
 #include <linux/err.h>
 #include <linux/string.h>
+#include <linux/clk.h>
+#include <linux/mutex.h>
+#include <linux/platform_device.h>
 
 #include <asm/io.h>
 #include <asm/semaphore.h>
-#include <asm/hardware/clock.h>
 
 #include <asm/arch/clock.h>
 
 LIST_HEAD(clocks);
-static DECLARE_MUTEX(clocks_sem);
+static DEFINE_MUTEX(clocks_mutex);
 DEFINE_SPINLOCK(clockfw_lock);
 
 static struct clk_functions *arch_clock;
 
 /*-------------------------------------------------------------------------
- * Standard clock functions defined in asm/hardware/clock.h
+ * 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 && try_module_get(p->owner)) {
+                       clk = p;
+                       goto found;
+               }
+       }
 
-       down(&clocks_sem);
        list_for_each_entry(p, &clocks, node) {
                if (strcmp(id, p->name) == 0 && try_module_get(p->owner)) {
                        clk = p;
                        break;
                }
        }
-       up(&clocks_sem);
+
+found:
+       mutex_unlock(&clocks_mutex);
 
        return clk;
 }
@@ -58,13 +81,12 @@ int clk_enable(struct clk *clk)
        unsigned long flags;
        int ret = 0;
 
+       if (clk == NULL || IS_ERR(clk))
+               return -EINVAL;
+
        spin_lock_irqsave(&clockfw_lock, flags);
-       if (clk->enable)
-               ret = clk->enable(clk);
-       else if (arch_clock->clk_enable)
+       if (arch_clock->clk_enable)
                ret = arch_clock->clk_enable(clk);
-       else
-               printk(KERN_ERR "Could not enable clock %s\n", clk->name);
        spin_unlock_irqrestore(&clockfw_lock, flags);
 
        return ret;
@@ -75,47 +97,24 @@ void clk_disable(struct clk *clk)
 {
        unsigned long flags;
 
+       if (clk == NULL || IS_ERR(clk))
+               return;
+
        spin_lock_irqsave(&clockfw_lock, flags);
-       if (clk->disable)
-               clk->disable(clk);
-       else if (arch_clock->clk_disable)
+       if (arch_clock->clk_disable)
                arch_clock->clk_disable(clk);
-       else
-               printk(KERN_ERR "Could not disable clock %s\n", clk->name);
        spin_unlock_irqrestore(&clockfw_lock, flags);
 }
 EXPORT_SYMBOL(clk_disable);
 
-int clk_use(struct clk *clk)
-{
-       unsigned long flags;
-       int ret = 0;
-
-       spin_lock_irqsave(&clockfw_lock, flags);
-       if (arch_clock->clk_use)
-               ret = arch_clock->clk_use(clk);
-       spin_unlock_irqrestore(&clockfw_lock, flags);
-
-       return ret;
-}
-EXPORT_SYMBOL(clk_use);
-
-void clk_unuse(struct clk *clk)
-{
-       unsigned long flags;
-
-       spin_lock_irqsave(&clockfw_lock, flags);
-       if (arch_clock->clk_unuse)
-               arch_clock->clk_unuse(clk);
-       spin_unlock_irqrestore(&clockfw_lock, flags);
-}
-EXPORT_SYMBOL(clk_unuse);
-
 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);
@@ -129,6 +128,9 @@ unsigned long clk_get_rate(struct clk *clk)
        unsigned long flags;
        unsigned long ret = 0;
 
+       if (clk == NULL || IS_ERR(clk))
+               return 0;
+
        spin_lock_irqsave(&clockfw_lock, flags);
        ret = clk->rate;
        spin_unlock_irqrestore(&clockfw_lock, flags);
@@ -145,7 +147,7 @@ void clk_put(struct clk *clk)
 EXPORT_SYMBOL(clk_put);
 
 /*-------------------------------------------------------------------------
- * Optional clock functions defined in asm/hardware/clock.h
+ * Optional clock functions defined in include/linux/clk.h
  *-------------------------------------------------------------------------*/
 
 long clk_round_rate(struct clk *clk, unsigned long rate)
@@ -153,6 +155,9 @@ long clk_round_rate(struct clk *clk, unsigned long rate)
        unsigned long flags;
        long ret = 0;
 
+       if (clk == NULL || IS_ERR(clk))
+               return ret;
+
        spin_lock_irqsave(&clockfw_lock, flags);
        if (arch_clock->clk_round_rate)
                ret = arch_clock->clk_round_rate(clk, rate);
@@ -165,7 +170,10 @@ EXPORT_SYMBOL(clk_round_rate);
 int clk_set_rate(struct clk *clk, unsigned long rate)
 {
        unsigned long flags;
-       int ret = 0;
+       int ret = -EINVAL;
+
+       if (clk == NULL || IS_ERR(clk))
+               return ret;
 
        spin_lock_irqsave(&clockfw_lock, flags);
        if (arch_clock->clk_set_rate)
@@ -179,7 +187,10 @@ EXPORT_SYMBOL(clk_set_rate);
 int clk_set_parent(struct clk *clk, struct clk *parent)
 {
        unsigned long flags;
-       int ret = 0;
+       int ret = -EINVAL;
+
+       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)
@@ -195,6 +206,9 @@ struct clk *clk_get_parent(struct clk *clk)
        unsigned long flags;
        struct clk * ret = NULL;
 
+       if (clk == NULL || IS_ERR(clk))
+               return ret;
+
        spin_lock_irqsave(&clockfw_lock, flags);
        if (arch_clock->clk_get_parent)
                ret = arch_clock->clk_get_parent(clk);
@@ -231,6 +245,9 @@ __setup("mpurate=", omap_clk_setup);
 /* Used for clocks that always have same value as the parent clock */
 void followparent_recalc(struct clk *clk)
 {
+       if (clk == NULL || IS_ERR(clk))
+               return;
+
        clk->rate = clk->parent->rate;
 }
 
@@ -239,6 +256,9 @@ 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;
@@ -249,11 +269,14 @@ void propagate_rate(struct clk * tclk)
 
 int clk_register(struct clk *clk)
 {
-       down(&clocks_sem);
+       if (clk == NULL || IS_ERR(clk))
+               return -EINVAL;
+
+       mutex_lock(&clocks_mutex);
        list_add(&clk->node, &clocks);
        if (clk->init)
                clk->init(clk);
-       up(&clocks_sem);
+       mutex_unlock(&clocks_mutex);
 
        return 0;
 }
@@ -261,9 +284,12 @@ EXPORT_SYMBOL(clk_register);
 
 void clk_unregister(struct clk *clk)
 {
-       down(&clocks_sem);
+       if (clk == NULL || IS_ERR(clk))
+               return;
+
+       mutex_lock(&clocks_mutex);
        list_del(&clk->node);
-       up(&clocks_sem);
+       mutex_unlock(&clocks_mutex);
 }
 EXPORT_SYMBOL(clk_unregister);
 
@@ -271,6 +297,9 @@ void clk_deny_idle(struct clk *clk)
 {
        unsigned long flags;
 
+       if (clk == NULL || IS_ERR(clk))
+               return;
+
        spin_lock_irqsave(&clockfw_lock, flags);
        if (arch_clock->clk_deny_idle)
                arch_clock->clk_deny_idle(clk);
@@ -282,6 +311,9 @@ void clk_allow_idle(struct clk *clk)
 {
        unsigned long flags;
 
+       if (clk == NULL || IS_ERR(clk))
+               return;
+
        spin_lock_irqsave(&clockfw_lock, flags);
        if (arch_clock->clk_allow_idle)
                arch_clock->clk_allow_idle(clk);
@@ -302,3 +334,65 @@ int __init clk_init(struct clk_functions * custom_clocks)
 
        return 0;
 }
+
+#ifdef CONFIG_PROC_FS
+#include <linux/proc_fs.h>
+#include <linux/seq_file.h>
+
+static void *omap_ck_start(struct seq_file *m, loff_t *pos)
+{
+       return *pos < 1 ? (void *)1 : NULL;
+}
+
+static void *omap_ck_next(struct seq_file *m, void *v, loff_t *pos)
+{
+       ++*pos;
+       return NULL;
+}
+
+static void omap_ck_stop(struct seq_file *m, void *v)
+{
+}
+
+int omap_ck_show(struct seq_file *m, void *v)
+{
+       struct clk *cp;
+
+       list_for_each_entry(cp, &clocks, node)
+               seq_printf(m,"%s %ld %d\n", cp->name, cp->rate, cp->usecount);
+
+       return 0;
+}
+
+static struct seq_operations omap_ck_op = {
+       .start =        omap_ck_start,
+       .next =         omap_ck_next,
+       .stop =         omap_ck_stop,
+       .show =         omap_ck_show
+};
+
+static int omap_ck_open(struct inode *inode, struct file *file)
+{
+       return seq_open(file, &omap_ck_op);
+}
+
+static struct file_operations proc_omap_ck_operations = {
+       .open           = omap_ck_open,
+       .read           = seq_read,
+       .llseek         = seq_lseek,
+       .release        = seq_release,
+};
+
+int __init omap_ck_init(void)
+{
+    struct proc_dir_entry *entry;
+
+       entry = create_proc_entry("omap_clocks", 0, NULL);
+       if (entry)
+               entry->proc_fops = &proc_omap_ck_operations;
+       return 0;
+
+}
+__initcall(omap_ck_init);
+#endif /* CONFIG_DEBUG_PROC_FS */
+