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 <asm/clock.h>
24 #include <asm/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 precidence: 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 mutex_unlock(&clocks_mutex);
75 return clk ? clk : ERR_PTR(-ENOENT);
77 EXPORT_SYMBOL(clk_get_sys);
79 void clkdev_add(struct clk_lookup *cl)
81 mutex_lock(&clocks_mutex);
82 list_add_tail(&cl->node, &clocks);
83 mutex_unlock(&clocks_mutex);
85 EXPORT_SYMBOL(clkdev_add);
87 void __init clkdev_add_table(struct clk_lookup *cl, size_t num)
89 mutex_lock(&clocks_mutex);
91 list_add_tail(&cl->node, &clocks);
94 mutex_unlock(&clocks_mutex);
100 struct clk_lookup_alloc {
101 struct clk_lookup cl;
102 char dev_id[MAX_DEV_ID];
103 char con_id[MAX_CON_ID];
106 struct clk_lookup *clkdev_alloc(struct clk *clk, const char *con_id,
107 const char *dev_fmt, ...)
109 struct clk_lookup_alloc *cla;
111 cla = kzalloc(sizeof(*cla), GFP_KERNEL);
117 strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
118 cla->cl.con_id = cla->con_id;
124 va_start(ap, dev_fmt);
125 vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
126 cla->cl.dev_id = cla->dev_id;
132 EXPORT_SYMBOL(clkdev_alloc);
134 int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
137 struct clk *r = clk_get(dev, id);
138 struct clk_lookup *l;
143 l = clkdev_alloc(r, alias, alias_dev_name);
150 EXPORT_SYMBOL(clk_add_alias);
153 * clkdev_drop - remove a clock dynamically allocated
155 void clkdev_drop(struct clk_lookup *cl)
157 mutex_lock(&clocks_mutex);
159 mutex_unlock(&clocks_mutex);
162 EXPORT_SYMBOL(clkdev_drop);