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/clkdev.h>
26 static LIST_HEAD(clocks
);
27 static DEFINE_MUTEX(clocks_mutex
);
29 #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK)
32 * of_clk_get_by_clkspec() - Lookup a clock form a clock provider
33 * @clkspec: pointer to a clock specifier data structure
35 * This function looks up a struct clk from the registered list of clock
36 * providers, an input is a clock specifier data structure as returned
37 * from the of_parse_phandle_with_args() function call.
39 struct clk
*of_clk_get_by_clkspec(struct of_phandle_args
*clkspec
)
44 return ERR_PTR(-EINVAL
);
47 clk
= __of_clk_get_from_provider(clkspec
);
49 if (!IS_ERR(clk
) && !__clk_get(clk
))
50 clk
= ERR_PTR(-ENOENT
);
56 struct clk
*of_clk_get(struct device_node
*np
, int index
)
58 struct of_phandle_args clkspec
;
63 return ERR_PTR(-EINVAL
);
65 rc
= of_parse_phandle_with_args(np
, "clocks", "#clock-cells", index
,
70 clk
= of_clk_get_by_clkspec(&clkspec
);
71 of_node_put(clkspec
.np
);
74 EXPORT_SYMBOL(of_clk_get
);
77 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
78 * @np: pointer to clock consumer node
79 * @name: name of consumer's clock input, or NULL for the first clock reference
81 * This function parses the clocks and clock-names properties,
82 * and uses them to look up the struct clk from the registered list of clock
85 struct clk
*of_clk_get_by_name(struct device_node
*np
, const char *name
)
87 struct clk
*clk
= ERR_PTR(-ENOENT
);
89 /* Walk up the tree of devices looking for a clock that matches */
94 * For named clocks, first look up the name in the
95 * "clock-names" property. If it cannot be found, then
96 * index will be an error code, and of_clk_get() will fail.
99 index
= of_property_match_string(np
, "clock-names", name
);
100 clk
= of_clk_get(np
, index
);
103 else if (name
&& index
>= 0) {
104 pr_err("ERROR: could not get clock %s:%s(%i)\n",
105 np
->full_name
, name
? name
: "", index
);
110 * No matching clock found on this node. If the parent node
111 * has a "clock-ranges" property, then we can try one of its
115 if (np
&& !of_get_property(np
, "clock-ranges", NULL
))
121 EXPORT_SYMBOL(of_clk_get_by_name
);
125 * Find the correct struct clk for the device and connection ID.
126 * We do slightly fuzzy matching here:
127 * An entry with a NULL ID is assumed to be a wildcard.
128 * If an entry has a device ID, it must match
129 * If an entry has a connection ID, it must match
130 * Then we take the most specific entry - with the following
131 * order of precedence: dev+con > dev only > con only.
133 static struct clk_lookup
*clk_find(const char *dev_id
, const char *con_id
)
135 struct clk_lookup
*p
, *cl
= NULL
;
136 int match
, best_found
= 0, best_possible
= 0;
143 list_for_each_entry(p
, &clocks
, node
) {
146 if (!dev_id
|| strcmp(p
->dev_id
, dev_id
))
151 if (!con_id
|| strcmp(p
->con_id
, con_id
))
156 if (match
> best_found
) {
158 if (match
!= best_possible
)
167 struct clk
*clk_get_sys(const char *dev_id
, const char *con_id
)
169 struct clk_lookup
*cl
;
171 mutex_lock(&clocks_mutex
);
172 cl
= clk_find(dev_id
, con_id
);
173 if (cl
&& !__clk_get(cl
->clk
))
175 mutex_unlock(&clocks_mutex
);
177 return cl
? cl
->clk
: ERR_PTR(-ENOENT
);
179 EXPORT_SYMBOL(clk_get_sys
);
181 struct clk
*clk_get(struct device
*dev
, const char *con_id
)
183 const char *dev_id
= dev
? dev_name(dev
) : NULL
;
187 clk
= of_clk_get_by_name(dev
->of_node
, con_id
);
190 if (PTR_ERR(clk
) == -EPROBE_DEFER
)
194 return clk_get_sys(dev_id
, con_id
);
196 EXPORT_SYMBOL(clk_get
);
198 void clk_put(struct clk
*clk
)
202 EXPORT_SYMBOL(clk_put
);
204 void clkdev_add(struct clk_lookup
*cl
)
206 mutex_lock(&clocks_mutex
);
207 list_add_tail(&cl
->node
, &clocks
);
208 mutex_unlock(&clocks_mutex
);
210 EXPORT_SYMBOL(clkdev_add
);
212 void __init
clkdev_add_table(struct clk_lookup
*cl
, size_t num
)
214 mutex_lock(&clocks_mutex
);
216 list_add_tail(&cl
->node
, &clocks
);
219 mutex_unlock(&clocks_mutex
);
222 #define MAX_DEV_ID 20
223 #define MAX_CON_ID 16
225 struct clk_lookup_alloc
{
226 struct clk_lookup cl
;
227 char dev_id
[MAX_DEV_ID
];
228 char con_id
[MAX_CON_ID
];
231 static struct clk_lookup
* __init_refok
232 vclkdev_alloc(struct clk
*clk
, const char *con_id
, const char *dev_fmt
,
235 struct clk_lookup_alloc
*cla
;
237 cla
= __clkdev_alloc(sizeof(*cla
));
243 strlcpy(cla
->con_id
, con_id
, sizeof(cla
->con_id
));
244 cla
->cl
.con_id
= cla
->con_id
;
248 vscnprintf(cla
->dev_id
, sizeof(cla
->dev_id
), dev_fmt
, ap
);
249 cla
->cl
.dev_id
= cla
->dev_id
;
255 struct clk_lookup
* __init_refok
256 clkdev_alloc(struct clk
*clk
, const char *con_id
, const char *dev_fmt
, ...)
258 struct clk_lookup
*cl
;
261 va_start(ap
, dev_fmt
);
262 cl
= vclkdev_alloc(clk
, con_id
, dev_fmt
, ap
);
267 EXPORT_SYMBOL(clkdev_alloc
);
269 int clk_add_alias(const char *alias
, const char *alias_dev_name
, char *id
,
272 struct clk
*r
= clk_get(dev
, id
);
273 struct clk_lookup
*l
;
278 l
= clkdev_alloc(r
, alias
, alias_dev_name
);
285 EXPORT_SYMBOL(clk_add_alias
);
288 * clkdev_drop - remove a clock dynamically allocated
290 void clkdev_drop(struct clk_lookup
*cl
)
292 mutex_lock(&clocks_mutex
);
294 mutex_unlock(&clocks_mutex
);
297 EXPORT_SYMBOL(clkdev_drop
);
300 * clk_register_clkdev - register one clock lookup for a struct clk
301 * @clk: struct clk to associate with all clk_lookups
302 * @con_id: connection ID string on device
303 * @dev_id: format string describing device name
305 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
308 * To make things easier for mass registration, we detect error clks
309 * from a previous clk_register() call, and return the error code for
310 * those. This is to permit this function to be called immediately
311 * after clk_register().
313 int clk_register_clkdev(struct clk
*clk
, const char *con_id
,
314 const char *dev_fmt
, ...)
316 struct clk_lookup
*cl
;
322 va_start(ap
, dev_fmt
);
323 cl
= vclkdev_alloc(clk
, con_id
, dev_fmt
, ap
);
335 * clk_register_clkdevs - register a set of clk_lookup for a struct clk
336 * @clk: struct clk to associate with all clk_lookups
337 * @cl: array of clk_lookup structures with con_id and dev_id pre-initialized
338 * @num: number of clk_lookup structures to register
340 * To make things easier for mass registration, we detect error clks
341 * from a previous clk_register() call, and return the error code for
342 * those. This is to permit this function to be called immediately
343 * after clk_register().
345 int clk_register_clkdevs(struct clk
*clk
, struct clk_lookup
*cl
, size_t num
)
352 for (i
= 0; i
< num
; i
++, cl
++) {
359 EXPORT_SYMBOL(clk_register_clkdevs
);