]>
Commit | Line | Data |
---|---|---|
0318e693 | 1 | /* |
6d803ba7 | 2 | * drivers/clk/clkdev.c |
0318e693 RK |
3 | * |
4 | * Copyright (C) 2008 Russell King. | |
5 | * | |
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. | |
9 | * | |
10 | * Helper for the clk API to assist looking up a struct clk. | |
11 | */ | |
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> | |
c0c60c4b | 20 | #include <linux/clk.h> |
6d803ba7 | 21 | #include <linux/clkdev.h> |
035a61c3 | 22 | #include <linux/clk-provider.h> |
766e6a4e | 23 | #include <linux/of.h> |
0318e693 | 24 | |
3a3d2b05 SN |
25 | #include "clk.h" |
26 | ||
0318e693 RK |
27 | static LIST_HEAD(clocks); |
28 | static DEFINE_MUTEX(clocks_mutex); | |
29 | ||
137f8a72 | 30 | #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) |
73e0e496 SB |
31 | static struct clk *__of_clk_get(struct device_node *np, int index, |
32 | const char *dev_id, const char *con_id) | |
766e6a4e GL |
33 | { |
34 | struct of_phandle_args clkspec; | |
35 | struct clk *clk; | |
36 | int rc; | |
37 | ||
766e6a4e GL |
38 | rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index, |
39 | &clkspec); | |
40 | if (rc) | |
41 | return ERR_PTR(rc); | |
42 | ||
306c342f | 43 | clk = __of_clk_get_from_provider(&clkspec, dev_id, con_id); |
766e6a4e | 44 | of_node_put(clkspec.np); |
035a61c3 TV |
45 | |
46 | return clk; | |
47 | } | |
48 | ||
49 | struct clk *of_clk_get(struct device_node *np, int index) | |
50 | { | |
73e0e496 | 51 | return __of_clk_get(np, index, np->full_name, NULL); |
766e6a4e GL |
52 | } |
53 | EXPORT_SYMBOL(of_clk_get); | |
54 | ||
035a61c3 TV |
55 | static struct clk *__of_clk_get_by_name(struct device_node *np, |
56 | const char *dev_id, | |
57 | const char *name) | |
766e6a4e GL |
58 | { |
59 | struct clk *clk = ERR_PTR(-ENOENT); | |
60 | ||
61 | /* Walk up the tree of devices looking for a clock that matches */ | |
62 | while (np) { | |
63 | int index = 0; | |
64 | ||
65 | /* | |
66 | * For named clocks, first look up the name in the | |
67 | * "clock-names" property. If it cannot be found, then | |
68 | * index will be an error code, and of_clk_get() will fail. | |
69 | */ | |
70 | if (name) | |
71 | index = of_property_match_string(np, "clock-names", name); | |
73e0e496 | 72 | clk = __of_clk_get(np, index, dev_id, name); |
035a61c3 | 73 | if (!IS_ERR(clk)) { |
766e6a4e | 74 | break; |
73e0e496 | 75 | } else if (name && index >= 0) { |
d8e53c3d | 76 | if (PTR_ERR(clk) != -EPROBE_DEFER) |
16673931 RH |
77 | pr_err("ERROR: could not get clock %pOF:%s(%i)\n", |
78 | np, name ? name : "", index); | |
766e6a4e GL |
79 | return clk; |
80 | } | |
81 | ||
82 | /* | |
83 | * No matching clock found on this node. If the parent node | |
84 | * has a "clock-ranges" property, then we can try one of its | |
85 | * clocks. | |
86 | */ | |
87 | np = np->parent; | |
88 | if (np && !of_get_property(np, "clock-ranges", NULL)) | |
89 | break; | |
90 | } | |
91 | ||
92 | return clk; | |
93 | } | |
035a61c3 TV |
94 | |
95 | /** | |
96 | * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node | |
97 | * @np: pointer to clock consumer node | |
98 | * @name: name of consumer's clock input, or NULL for the first clock reference | |
99 | * | |
100 | * This function parses the clocks and clock-names properties, | |
101 | * and uses them to look up the struct clk from the registered list of clock | |
102 | * providers. | |
103 | */ | |
104 | struct clk *of_clk_get_by_name(struct device_node *np, const char *name) | |
105 | { | |
106 | if (!np) | |
107 | return ERR_PTR(-ENOENT); | |
108 | ||
109 | return __of_clk_get_by_name(np, np->full_name, name); | |
110 | } | |
766e6a4e | 111 | EXPORT_SYMBOL(of_clk_get_by_name); |
035a61c3 TV |
112 | |
113 | #else /* defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) */ | |
114 | ||
115 | static struct clk *__of_clk_get_by_name(struct device_node *np, | |
116 | const char *dev_id, | |
117 | const char *name) | |
118 | { | |
119 | return ERR_PTR(-ENOENT); | |
120 | } | |
766e6a4e GL |
121 | #endif |
122 | ||
409dc360 RK |
123 | /* |
124 | * Find the correct struct clk for the device and connection ID. | |
125 | * We do slightly fuzzy matching here: | |
126 | * An entry with a NULL ID is assumed to be a wildcard. | |
127 | * If an entry has a device ID, it must match | |
128 | * If an entry has a connection ID, it must match | |
129 | * Then we take the most specific entry - with the following | |
659431fc | 130 | * order of precedence: dev+con > dev only > con only. |
409dc360 | 131 | */ |
e8bf8df9 | 132 | static struct clk_lookup *clk_find(const char *dev_id, const char *con_id) |
0318e693 | 133 | { |
e8bf8df9 | 134 | struct clk_lookup *p, *cl = NULL; |
67b50871 VK |
135 | int match, best_found = 0, best_possible = 0; |
136 | ||
137 | if (dev_id) | |
138 | best_possible += 2; | |
139 | if (con_id) | |
140 | best_possible += 1; | |
0318e693 RK |
141 | |
142 | list_for_each_entry(p, &clocks, node) { | |
0318e693 | 143 | match = 0; |
409dc360 RK |
144 | if (p->dev_id) { |
145 | if (!dev_id || strcmp(p->dev_id, dev_id)) | |
146 | continue; | |
147 | match += 2; | |
148 | } | |
149 | if (p->con_id) { | |
150 | if (!con_id || strcmp(p->con_id, con_id)) | |
151 | continue; | |
152 | match += 1; | |
153 | } | |
0318e693 | 154 | |
67b50871 | 155 | if (match > best_found) { |
e8bf8df9 | 156 | cl = p; |
67b50871 VK |
157 | if (match != best_possible) |
158 | best_found = match; | |
e4bf5bec VK |
159 | else |
160 | break; | |
0318e693 RK |
161 | } |
162 | } | |
e8bf8df9 | 163 | return cl; |
0318e693 RK |
164 | } |
165 | ||
05fd8e73 | 166 | struct clk *clk_get_sys(const char *dev_id, const char *con_id) |
0318e693 | 167 | { |
e8bf8df9 | 168 | struct clk_lookup *cl; |
035a61c3 | 169 | struct clk *clk = NULL; |
0318e693 RK |
170 | |
171 | mutex_lock(&clocks_mutex); | |
035a61c3 | 172 | |
e8bf8df9 | 173 | cl = clk_find(dev_id, con_id); |
035a61c3 TV |
174 | if (!cl) |
175 | goto out; | |
176 | ||
d5622a9c | 177 | clk = __clk_create_clk(cl->clk_hw, dev_id, con_id); |
73e0e496 SB |
178 | if (IS_ERR(clk)) |
179 | goto out; | |
180 | ||
181 | if (!__clk_get(clk)) { | |
182 | __clk_free_clk(clk); | |
e8bf8df9 | 183 | cl = NULL; |
035a61c3 TV |
184 | goto out; |
185 | } | |
186 | ||
035a61c3 | 187 | out: |
0318e693 RK |
188 | mutex_unlock(&clocks_mutex); |
189 | ||
035a61c3 | 190 | return cl ? clk : ERR_PTR(-ENOENT); |
0318e693 | 191 | } |
05fd8e73 SH |
192 | EXPORT_SYMBOL(clk_get_sys); |
193 | ||
194 | struct clk *clk_get(struct device *dev, const char *con_id) | |
195 | { | |
196 | const char *dev_id = dev ? dev_name(dev) : NULL; | |
766e6a4e GL |
197 | struct clk *clk; |
198 | ||
53ccb22b | 199 | if (dev && dev->of_node) { |
035a61c3 TV |
200 | clk = __of_clk_get_by_name(dev->of_node, dev_id, con_id); |
201 | if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER) | |
a34cd466 | 202 | return clk; |
766e6a4e | 203 | } |
05fd8e73 SH |
204 | |
205 | return clk_get_sys(dev_id, con_id); | |
206 | } | |
0318e693 RK |
207 | EXPORT_SYMBOL(clk_get); |
208 | ||
209 | void clk_put(struct clk *clk) | |
210 | { | |
211 | __clk_put(clk); | |
212 | } | |
213 | EXPORT_SYMBOL(clk_put); | |
214 | ||
d5622a9c | 215 | static void __clkdev_add(struct clk_lookup *cl) |
0318e693 RK |
216 | { |
217 | mutex_lock(&clocks_mutex); | |
218 | list_add_tail(&cl->node, &clocks); | |
219 | mutex_unlock(&clocks_mutex); | |
220 | } | |
d5622a9c RK |
221 | |
222 | void clkdev_add(struct clk_lookup *cl) | |
223 | { | |
224 | if (!cl->clk_hw) | |
225 | cl->clk_hw = __clk_get_hw(cl->clk); | |
226 | __clkdev_add(cl); | |
227 | } | |
0318e693 RK |
228 | EXPORT_SYMBOL(clkdev_add); |
229 | ||
fba3acd9 | 230 | void clkdev_add_table(struct clk_lookup *cl, size_t num) |
0a0300dc RK |
231 | { |
232 | mutex_lock(&clocks_mutex); | |
233 | while (num--) { | |
d5622a9c | 234 | cl->clk_hw = __clk_get_hw(cl->clk); |
0a0300dc RK |
235 | list_add_tail(&cl->node, &clocks); |
236 | cl++; | |
237 | } | |
238 | mutex_unlock(&clocks_mutex); | |
239 | } | |
240 | ||
0318e693 RK |
241 | #define MAX_DEV_ID 20 |
242 | #define MAX_CON_ID 16 | |
243 | ||
244 | struct clk_lookup_alloc { | |
245 | struct clk_lookup cl; | |
246 | char dev_id[MAX_DEV_ID]; | |
247 | char con_id[MAX_CON_ID]; | |
248 | }; | |
249 | ||
bd721ea7 | 250 | static struct clk_lookup * __ref |
d5622a9c | 251 | vclkdev_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt, |
e9d7f406 | 252 | va_list ap) |
0318e693 RK |
253 | { |
254 | struct clk_lookup_alloc *cla; | |
255 | ||
0d4e3d00 | 256 | cla = kzalloc(sizeof(*cla), GFP_KERNEL); |
0318e693 RK |
257 | if (!cla) |
258 | return NULL; | |
259 | ||
d5622a9c | 260 | cla->cl.clk_hw = hw; |
0318e693 RK |
261 | if (con_id) { |
262 | strlcpy(cla->con_id, con_id, sizeof(cla->con_id)); | |
263 | cla->cl.con_id = cla->con_id; | |
264 | } | |
265 | ||
266 | if (dev_fmt) { | |
0318e693 RK |
267 | vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap); |
268 | cla->cl.dev_id = cla->dev_id; | |
0318e693 RK |
269 | } |
270 | ||
271 | return &cla->cl; | |
272 | } | |
e9d7f406 | 273 | |
25689998 RK |
274 | static struct clk_lookup * |
275 | vclkdev_create(struct clk_hw *hw, const char *con_id, const char *dev_fmt, | |
276 | va_list ap) | |
277 | { | |
278 | struct clk_lookup *cl; | |
279 | ||
280 | cl = vclkdev_alloc(hw, con_id, dev_fmt, ap); | |
281 | if (cl) | |
282 | __clkdev_add(cl); | |
283 | ||
284 | return cl; | |
285 | } | |
286 | ||
bd721ea7 | 287 | struct clk_lookup * __ref |
e9d7f406 RK |
288 | clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...) |
289 | { | |
290 | struct clk_lookup *cl; | |
291 | va_list ap; | |
292 | ||
293 | va_start(ap, dev_fmt); | |
d5622a9c | 294 | cl = vclkdev_alloc(__clk_get_hw(clk), con_id, dev_fmt, ap); |
e9d7f406 RK |
295 | va_end(ap); |
296 | ||
297 | return cl; | |
298 | } | |
0318e693 RK |
299 | EXPORT_SYMBOL(clkdev_alloc); |
300 | ||
e4f1b49b SB |
301 | struct clk_lookup * |
302 | clkdev_hw_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt, ...) | |
303 | { | |
304 | struct clk_lookup *cl; | |
305 | va_list ap; | |
306 | ||
307 | va_start(ap, dev_fmt); | |
308 | cl = vclkdev_alloc(hw, con_id, dev_fmt, ap); | |
309 | va_end(ap); | |
310 | ||
311 | return cl; | |
312 | } | |
313 | EXPORT_SYMBOL(clkdev_hw_alloc); | |
314 | ||
25689998 RK |
315 | /** |
316 | * clkdev_create - allocate and add a clkdev lookup structure | |
317 | * @clk: struct clk to associate with all clk_lookups | |
318 | * @con_id: connection ID string on device | |
319 | * @dev_fmt: format string describing device name | |
320 | * | |
321 | * Returns a clk_lookup structure, which can be later unregistered and | |
322 | * freed. | |
323 | */ | |
324 | struct clk_lookup *clkdev_create(struct clk *clk, const char *con_id, | |
325 | const char *dev_fmt, ...) | |
326 | { | |
327 | struct clk_lookup *cl; | |
328 | va_list ap; | |
329 | ||
330 | va_start(ap, dev_fmt); | |
331 | cl = vclkdev_create(__clk_get_hw(clk), con_id, dev_fmt, ap); | |
332 | va_end(ap); | |
333 | ||
334 | return cl; | |
335 | } | |
336 | EXPORT_SYMBOL_GPL(clkdev_create); | |
337 | ||
e4f1b49b SB |
338 | /** |
339 | * clkdev_hw_create - allocate and add a clkdev lookup structure | |
340 | * @hw: struct clk_hw to associate with all clk_lookups | |
341 | * @con_id: connection ID string on device | |
342 | * @dev_fmt: format string describing device name | |
343 | * | |
344 | * Returns a clk_lookup structure, which can be later unregistered and | |
345 | * freed. | |
346 | */ | |
347 | struct clk_lookup *clkdev_hw_create(struct clk_hw *hw, const char *con_id, | |
348 | const char *dev_fmt, ...) | |
349 | { | |
350 | struct clk_lookup *cl; | |
351 | va_list ap; | |
352 | ||
353 | va_start(ap, dev_fmt); | |
354 | cl = vclkdev_create(hw, con_id, dev_fmt, ap); | |
355 | va_end(ap); | |
356 | ||
357 | return cl; | |
358 | } | |
359 | EXPORT_SYMBOL_GPL(clkdev_hw_create); | |
360 | ||
b3d8d7e8 RK |
361 | int clk_add_alias(const char *alias, const char *alias_dev_name, |
362 | const char *con_id, struct device *dev) | |
c0683039 | 363 | { |
b3d8d7e8 | 364 | struct clk *r = clk_get(dev, con_id); |
c0683039 TL |
365 | struct clk_lookup *l; |
366 | ||
367 | if (IS_ERR(r)) | |
368 | return PTR_ERR(r); | |
369 | ||
625faa6a RK |
370 | l = clkdev_create(r, alias, alias_dev_name ? "%s" : NULL, |
371 | alias_dev_name); | |
c0683039 | 372 | clk_put(r); |
25689998 RK |
373 | |
374 | return l ? 0 : -ENODEV; | |
c0683039 TL |
375 | } |
376 | EXPORT_SYMBOL(clk_add_alias); | |
377 | ||
0318e693 RK |
378 | /* |
379 | * clkdev_drop - remove a clock dynamically allocated | |
380 | */ | |
381 | void clkdev_drop(struct clk_lookup *cl) | |
382 | { | |
383 | mutex_lock(&clocks_mutex); | |
384 | list_del(&cl->node); | |
385 | mutex_unlock(&clocks_mutex); | |
386 | kfree(cl); | |
387 | } | |
388 | EXPORT_SYMBOL(clkdev_drop); | |
e9d7f406 | 389 | |
416dd13a KC |
390 | static struct clk_lookup *__clk_register_clkdev(struct clk_hw *hw, |
391 | const char *con_id, | |
392 | const char *dev_id, ...) | |
393 | { | |
394 | struct clk_lookup *cl; | |
395 | va_list ap; | |
396 | ||
397 | va_start(ap, dev_id); | |
398 | cl = vclkdev_create(hw, con_id, dev_id, ap); | |
399 | va_end(ap); | |
400 | ||
401 | return cl; | |
402 | } | |
403 | ||
e9d7f406 RK |
404 | /** |
405 | * clk_register_clkdev - register one clock lookup for a struct clk | |
406 | * @clk: struct clk to associate with all clk_lookups | |
407 | * @con_id: connection ID string on device | |
416dd13a | 408 | * @dev_id: string describing device name |
e9d7f406 RK |
409 | * |
410 | * con_id or dev_id may be NULL as a wildcard, just as in the rest of | |
411 | * clkdev. | |
412 | * | |
413 | * To make things easier for mass registration, we detect error clks | |
414 | * from a previous clk_register() call, and return the error code for | |
415 | * those. This is to permit this function to be called immediately | |
416 | * after clk_register(). | |
417 | */ | |
418 | int clk_register_clkdev(struct clk *clk, const char *con_id, | |
416dd13a | 419 | const char *dev_id) |
e9d7f406 RK |
420 | { |
421 | struct clk_lookup *cl; | |
e9d7f406 RK |
422 | |
423 | if (IS_ERR(clk)) | |
424 | return PTR_ERR(clk); | |
425 | ||
416dd13a KC |
426 | /* |
427 | * Since dev_id can be NULL, and NULL is handled specially, we must | |
428 | * pass it as either a NULL format string, or with "%s". | |
429 | */ | |
430 | if (dev_id) | |
431 | cl = __clk_register_clkdev(__clk_get_hw(clk), con_id, "%s", | |
432 | dev_id); | |
433 | else | |
434 | cl = __clk_register_clkdev(__clk_get_hw(clk), con_id, NULL); | |
e9d7f406 | 435 | |
25689998 | 436 | return cl ? 0 : -ENOMEM; |
e9d7f406 | 437 | } |
a251361a | 438 | EXPORT_SYMBOL(clk_register_clkdev); |
e4f1b49b SB |
439 | |
440 | /** | |
441 | * clk_hw_register_clkdev - register one clock lookup for a struct clk_hw | |
442 | * @hw: struct clk_hw to associate with all clk_lookups | |
443 | * @con_id: connection ID string on device | |
444 | * @dev_id: format string describing device name | |
445 | * | |
446 | * con_id or dev_id may be NULL as a wildcard, just as in the rest of | |
447 | * clkdev. | |
9388093d GU |
448 | * |
449 | * To make things easier for mass registration, we detect error clk_hws | |
450 | * from a previous clk_hw_register_*() call, and return the error code for | |
451 | * those. This is to permit this function to be called immediately | |
452 | * after clk_hw_register_*(). | |
e4f1b49b SB |
453 | */ |
454 | int clk_hw_register_clkdev(struct clk_hw *hw, const char *con_id, | |
455 | const char *dev_id) | |
456 | { | |
457 | struct clk_lookup *cl; | |
458 | ||
9388093d GU |
459 | if (IS_ERR(hw)) |
460 | return PTR_ERR(hw); | |
461 | ||
e4f1b49b SB |
462 | /* |
463 | * Since dev_id can be NULL, and NULL is handled specially, we must | |
464 | * pass it as either a NULL format string, or with "%s". | |
465 | */ | |
466 | if (dev_id) | |
467 | cl = __clk_register_clkdev(hw, con_id, "%s", dev_id); | |
468 | else | |
469 | cl = __clk_register_clkdev(hw, con_id, NULL); | |
470 | ||
471 | return cl ? 0 : -ENOMEM; | |
472 | } | |
473 | EXPORT_SYMBOL(clk_hw_register_clkdev); |