2 * arch/arm/common/clkdev.c
4 * Copyright (C) 2008 Russell King.
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.
10 * Helper for the clk API to assist looking up a struct clk.
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/device.h>
15 #include <linux/list.h>
16 #include <linux/errno.h>
17 #include <linux/err.h>
18 #include <linux/string.h>
19 #include <linux/mutex.h>
20 #include <linux/clk.h>
21 #include <linux/slab.h>
23 #include <asm/clkdev.h>
24 #include <mach/clkdev.h>
26 static LIST_HEAD(clocks);
27 static DEFINE_MUTEX(clocks_mutex);
30 * Find the correct struct clk for the device and connection ID.
31 * We do slightly fuzzy matching here:
32 * An entry with a NULL ID is assumed to be a wildcard.
33 * If an entry has a device ID, it must match
34 * If an entry has a connection ID, it must match
35 * Then we take the most specific entry - with the following
36 * order of precedence: dev+con > dev only > con only.
38 static struct clk *clk_find(const char *dev_id, const char *con_id)
41 struct clk *clk = NULL;
44 list_for_each_entry(p, &clocks, node) {
47 if (!dev_id || strcmp(p->dev_id, dev_id))
52 if (!con_id || strcmp(p->con_id, con_id))
67 struct clk *clk_get_sys(const char *dev_id, const char *con_id)
71 mutex_lock(&clocks_mutex);
72 clk = clk_find(dev_id, con_id);
73 if (clk && !__clk_get(clk))
75 mutex_unlock(&clocks_mutex);
77 return clk ? clk : ERR_PTR(-ENOENT);
79 EXPORT_SYMBOL(clk_get_sys);
81 struct clk *clk_get(struct device *dev, const char *con_id)
83 const char *dev_id = dev ? dev_name(dev) : NULL;
85 return clk_get_sys(dev_id, con_id);
87 EXPORT_SYMBOL(clk_get);
89 void clk_put(struct clk *clk)
93 EXPORT_SYMBOL(clk_put);
95 void clkdev_add(struct clk_lookup *cl)
97 mutex_lock(&clocks_mutex);
98 list_add_tail(&cl->node, &clocks);
99 mutex_unlock(&clocks_mutex);
101 EXPORT_SYMBOL(clkdev_add);
103 void __init clkdev_add_table(struct clk_lookup *cl, size_t num)
105 mutex_lock(&clocks_mutex);
107 list_add_tail(&cl->node, &clocks);
110 mutex_unlock(&clocks_mutex);
113 #define MAX_DEV_ID 20
114 #define MAX_CON_ID 16
116 struct clk_lookup_alloc {
117 struct clk_lookup cl;
118 char dev_id[MAX_DEV_ID];
119 char con_id[MAX_CON_ID];
122 struct clk_lookup *clkdev_alloc(struct clk *clk, const char *con_id,
123 const char *dev_fmt, ...)
125 struct clk_lookup_alloc *cla;
127 cla = kzalloc(sizeof(*cla), GFP_KERNEL);
133 strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
134 cla->cl.con_id = cla->con_id;
140 va_start(ap, dev_fmt);
141 vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
142 cla->cl.dev_id = cla->dev_id;
148 EXPORT_SYMBOL(clkdev_alloc);
150 int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
153 struct clk *r = clk_get(dev, id);
154 struct clk_lookup *l;
159 l = clkdev_alloc(r, alias, alias_dev_name);
166 EXPORT_SYMBOL(clk_add_alias);
169 * clkdev_drop - remove a clock dynamically allocated
171 void clkdev_drop(struct clk_lookup *cl)
173 mutex_lock(&clocks_mutex);
175 mutex_unlock(&clocks_mutex);
178 EXPORT_SYMBOL(clkdev_drop);