tizen 2.4 release
[kernel/u-boot-tm1.git] / drivers / clk / sprd_clk.c
1 /*
2  * drivers/clk/clkdev.c
3  *
4  *  Copyright (C) 2008 Russell King.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Helper for the clk API to assist looking up a struct clk.
11  */
12 #ifdef CONFIG_NKERNEL
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/device.h>
16 #include <linux/list.h>
17 #include <linux/errno.h>
18 #include <linux/err.h>
19 #include <linux/string.h>
20 #include <linux/mutex.h>
21 #include <linux/clk.h>
22 #include <linux/clkdev.h>
23 #else
24 #include <ubi_uboot.h>
25 #include <asm/arch/clock.h>
26 #endif
27 static LIST_HEAD(clocks);
28 static DEFINE_MUTEX(clocks_mutex);
29
30 /*
31  * Find the correct struct clk for the device and connection ID.
32  * We do slightly fuzzy matching here:
33  *  An entry with a NULL ID is assumed to be a wildcard.
34  *  If an entry has a device ID, it must match
35  *  If an entry has a connection ID, it must match
36  * Then we take the most specific entry - with the following
37  * order of precedence: dev+con > dev only > con only.
38  */
39 static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
40 {
41         struct clk_lookup *p, *cl = NULL;
42         int match, best = 0;
43
44         list_for_each_entry(p, &clocks, node) {
45                 match = 0;
46                 if (p->dev_id) {
47                         if (!dev_id || strcmp(p->dev_id, dev_id))
48                                 continue;
49                         match += 2;
50                 }
51                 if (p->con_id) {
52                         if (!con_id || strcmp(p->con_id, con_id))
53                                 continue;
54                         match += 1;
55                 }
56
57                 if (match > best) {
58                         cl = p;
59                         if (match != 3)
60                                 best = match;
61                         else
62                                 break;
63                 }
64         }
65         return cl;
66 }
67
68 struct clk *clk_get_sys(const char *dev_id, const char *con_id)
69 {
70         struct clk_lookup *cl;
71
72         mutex_lock(&clocks_mutex);
73         cl = clk_find(dev_id, con_id);
74         //if (cl && !__clk_get(cl->clk))
75         //      cl = NULL;
76         mutex_unlock(&clocks_mutex);
77
78         return cl ? cl->clk : ERR_PTR(-ENOENT);
79 }
80 EXPORT_SYMBOL(clk_get_sys);
81
82 struct clk *clk_get(struct device *dev, const char *con_id)
83 {
84         //const char *dev_id = dev ? dev_name(dev) : NULL;
85
86         return clk_get_sys(NULL, con_id);
87 }
88 EXPORT_SYMBOL(clk_get);
89
90 void clk_put(struct clk *clk)
91 {
92 //      __clk_put(clk);
93 }
94 EXPORT_SYMBOL(clk_put);
95
96 void clkdev_add(struct clk_lookup *cl)
97 {
98         mutex_lock(&clocks_mutex);
99         list_add_tail(&cl->node, &clocks);
100         mutex_unlock(&clocks_mutex);
101 }
102 EXPORT_SYMBOL(clkdev_add);
103