···11+/*22+ * arch/arm/common/clkdev.c33+ *44+ * Copyright (C) 2008 Russell King.55+ *66+ * This program is free software; you can redistribute it and/or modify77+ * it under the terms of the GNU General Public License version 2 as88+ * published by the Free Software Foundation.99+ *1010+ * Helper for the clk API to assist looking up a struct clk.1111+ */1212+#include <linux/module.h>1313+#include <linux/kernel.h>1414+#include <linux/device.h>1515+#include <linux/list.h>1616+#include <linux/errno.h>1717+#include <linux/err.h>1818+#include <linux/string.h>1919+#include <linux/mutex.h>2020+2121+#include <asm/clkdev.h>2222+#include <mach/clkdev.h>2323+2424+static LIST_HEAD(clocks);2525+static DEFINE_MUTEX(clocks_mutex);2626+2727+static struct clk *clk_find(const char *dev_id, const char *con_id)2828+{2929+ struct clk_lookup *p;3030+ struct clk *clk = NULL;3131+ int match, best = 0;3232+3333+ list_for_each_entry(p, &clocks, node) {3434+ if ((p->dev_id && !dev_id) || (p->con_id && !con_id))3535+ continue;3636+ match = 0;3737+ if (p->dev_id)3838+ match += 2 * (strcmp(p->dev_id, dev_id) == 0);3939+ if (p->con_id)4040+ match += 1 * (strcmp(p->con_id, con_id) == 0);4141+ if (match == 0)4242+ continue;4343+4444+ if (match > best) {4545+ clk = p->clk;4646+ best = match;4747+ }4848+ }4949+ return clk;5050+}5151+5252+struct clk *clk_get(struct device *dev, const char *con_id)5353+{5454+ const char *dev_id = dev ? dev_name(dev) : NULL;5555+ struct clk *clk;5656+5757+ mutex_lock(&clocks_mutex);5858+ clk = clk_find(dev_id, con_id);5959+ if (clk && !__clk_get(clk))6060+ clk = NULL;6161+ mutex_unlock(&clocks_mutex);6262+6363+ return clk ? clk : ERR_PTR(-ENOENT);6464+}6565+EXPORT_SYMBOL(clk_get);6666+6767+void clk_put(struct clk *clk)6868+{6969+ __clk_put(clk);7070+}7171+EXPORT_SYMBOL(clk_put);7272+7373+void clkdev_add(struct clk_lookup *cl)7474+{7575+ mutex_lock(&clocks_mutex);7676+ list_add_tail(&cl->node, &clocks);7777+ mutex_unlock(&clocks_mutex);7878+}7979+EXPORT_SYMBOL(clkdev_add);8080+8181+#define MAX_DEV_ID 208282+#define MAX_CON_ID 168383+8484+struct clk_lookup_alloc {8585+ struct clk_lookup cl;8686+ char dev_id[MAX_DEV_ID];8787+ char con_id[MAX_CON_ID];8888+};8989+9090+struct clk_lookup *clkdev_alloc(struct clk *clk, const char *con_id,9191+ const char *dev_fmt, ...)9292+{9393+ struct clk_lookup_alloc *cla;9494+9595+ cla = kzalloc(sizeof(*cla), GFP_KERNEL);9696+ if (!cla)9797+ return NULL;9898+9999+ cla->cl.clk = clk;100100+ if (con_id) {101101+ strlcpy(cla->con_id, con_id, sizeof(cla->con_id));102102+ cla->cl.con_id = cla->con_id;103103+ }104104+105105+ if (dev_fmt) {106106+ va_list ap;107107+108108+ va_start(ap, dev_fmt);109109+ vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);110110+ cla->cl.dev_id = cla->dev_id;111111+ va_end(ap);112112+ }113113+114114+ return &cla->cl;115115+}116116+EXPORT_SYMBOL(clkdev_alloc);117117+118118+/*119119+ * clkdev_drop - remove a clock dynamically allocated120120+ */121121+void clkdev_drop(struct clk_lookup *cl)122122+{123123+ mutex_lock(&clocks_mutex);124124+ list_del(&cl->node);125125+ mutex_unlock(&clocks_mutex);126126+ kfree(cl);127127+}128128+EXPORT_SYMBOL(clkdev_drop);
+30
arch/arm/include/asm/clkdev.h
···11+/*22+ * arch/arm/include/asm/clkdev.h33+ *44+ * Copyright (C) 2008 Russell King.55+ *66+ * This program is free software; you can redistribute it and/or modify77+ * it under the terms of the GNU General Public License version 2 as88+ * published by the Free Software Foundation.99+ *1010+ * Helper for the clk API to assist looking up a struct clk.1111+ */1212+#ifndef __ASM_CLKDEV_H1313+#define __ASM_CLKDEV_H1414+1515+struct clk;1616+1717+struct clk_lookup {1818+ struct list_head node;1919+ const char *dev_id;2020+ const char *con_id;2121+ struct clk *clk;2222+};2323+2424+struct clk_lookup *clkdev_alloc(struct clk *clk, const char *con_id,2525+ const char *dev_fmt, ...);2626+2727+void clkdev_add(struct clk_lookup *cl);2828+void clkdev_drop(struct clk_lookup *cl);2929+3030+#endif