2 * arch/sh/kernel/clkdev.c
4 * Cloned from arch/arm/common/clkdev.c:
6 * Copyright (C) 2008 Russell King.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 * Helper for the clk API to assist looking up a struct clk.
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/device.h>
17 #include <linux/list.h>
18 #include <linux/errno.h>
19 #include <linux/err.h>
20 #include <linux/string.h>
21 #include <linux/mutex.h>
22 #include <linux/clk.h>
23 #include <linux/slab.h>
24 #include <linux/bootmem.h>
26 #include <asm/clock.h>
27 #include <asm/clkdev.h>
29 static LIST_HEAD(clocks);
30 static DEFINE_MUTEX(clocks_mutex);
33 * Find the correct struct clk for the device and connection ID.
34 * We do slightly fuzzy matching here:
35 * An entry with a NULL ID is assumed to be a wildcard.
36 * If an entry has a device ID, it must match
37 * If an entry has a connection ID, it must match
38 * Then we take the most specific entry - with the following
39 * order of precedence: dev+con > dev only > con only.
41 static struct clk *clk_find(const char *dev_id, const char *con_id)
44 struct clk *clk = NULL;
47 list_for_each_entry(p, &clocks, node) {
50 if (!dev_id || strcmp(p->dev_id, dev_id))
55 if (!con_id || strcmp(p->con_id, con_id))
70 struct clk *clk_get_sys(const char *dev_id, const char *con_id)
74 mutex_lock(&clocks_mutex);
75 clk = clk_find(dev_id, con_id);
76 mutex_unlock(&clocks_mutex);
78 return clk ? clk : ERR_PTR(-ENOENT);
80 EXPORT_SYMBOL(clk_get_sys);
82 void clkdev_add(struct clk_lookup *cl)
84 mutex_lock(&clocks_mutex);
85 list_add_tail(&cl->node, &clocks);
86 mutex_unlock(&clocks_mutex);
88 EXPORT_SYMBOL(clkdev_add);
90 void __init clkdev_add_table(struct clk_lookup *cl, size_t num)
92 mutex_lock(&clocks_mutex);
94 list_add_tail(&cl->node, &clocks);
97 mutex_unlock(&clocks_mutex);
100 #define MAX_DEV_ID 20
101 #define MAX_CON_ID 16
103 struct clk_lookup_alloc {
104 struct clk_lookup cl;
105 char dev_id[MAX_DEV_ID];
106 char con_id[MAX_CON_ID];
109 struct clk_lookup * __init_refok
110 clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
112 struct clk_lookup_alloc *cla;
114 if (!slab_is_available())
115 cla = alloc_bootmem_low_pages(sizeof(*cla));
117 cla = kzalloc(sizeof(*cla), GFP_KERNEL);
124 strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
125 cla->cl.con_id = cla->con_id;
131 va_start(ap, dev_fmt);
132 vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
133 cla->cl.dev_id = cla->dev_id;
139 EXPORT_SYMBOL(clkdev_alloc);
141 int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
144 struct clk *r = clk_get(dev, id);
145 struct clk_lookup *l;
150 l = clkdev_alloc(r, alias, alias_dev_name);
157 EXPORT_SYMBOL(clk_add_alias);
160 * clkdev_drop - remove a clock dynamically allocated
162 void clkdev_drop(struct clk_lookup *cl)
164 struct clk_lookup_alloc *cla = container_of(cl, struct clk_lookup_alloc, cl);
166 mutex_lock(&clocks_mutex);
168 mutex_unlock(&clocks_mutex);
171 EXPORT_SYMBOL(clkdev_drop);