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))
68 struct clk *clk_get_sys(const char *dev_id, const char *con_id)
72 mutex_lock(&clocks_mutex);
73 clk = clk_find(dev_id, con_id);
74 if (clk && !__clk_get(clk))
76 mutex_unlock(&clocks_mutex);
78 return clk ? clk : ERR_PTR(-ENOENT);
80 EXPORT_SYMBOL(clk_get_sys);
82 struct clk *clk_get(struct device *dev, const char *con_id)
84 const char *dev_id = dev ? dev_name(dev) : NULL;
86 return clk_get_sys(dev_id, con_id);
88 EXPORT_SYMBOL(clk_get);
90 void clk_put(struct clk *clk)
94 EXPORT_SYMBOL(clk_put);
96 void clkdev_add(struct clk_lookup *cl)
98 mutex_lock(&clocks_mutex);
99 list_add_tail(&cl->node, &clocks);
100 mutex_unlock(&clocks_mutex);
102 EXPORT_SYMBOL(clkdev_add);
104 void __init clkdev_add_table(struct clk_lookup *cl, size_t num)
106 mutex_lock(&clocks_mutex);
108 list_add_tail(&cl->node, &clocks);
111 mutex_unlock(&clocks_mutex);
114 #define MAX_DEV_ID 20
115 #define MAX_CON_ID 16
117 struct clk_lookup_alloc {
118 struct clk_lookup cl;
119 char dev_id[MAX_DEV_ID];
120 char con_id[MAX_CON_ID];
123 struct clk_lookup *clkdev_alloc(struct clk *clk, const char *con_id,
124 const char *dev_fmt, ...)
126 struct clk_lookup_alloc *cla;
128 cla = kzalloc(sizeof(*cla), GFP_KERNEL);
134 strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
135 cla->cl.con_id = cla->con_id;
141 va_start(ap, dev_fmt);
142 vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
143 cla->cl.dev_id = cla->dev_id;
149 EXPORT_SYMBOL(clkdev_alloc);
151 int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
154 struct clk *r = clk_get(dev, id);
155 struct clk_lookup *l;
160 l = clkdev_alloc(r, alias, alias_dev_name);
167 EXPORT_SYMBOL(clk_add_alias);
170 * clkdev_drop - remove a clock dynamically allocated
172 void clkdev_drop(struct clk_lookup *cl)
174 mutex_lock(&clocks_mutex);
176 mutex_unlock(&clocks_mutex);
179 EXPORT_SYMBOL(clkdev_drop);