]> Git Repo - linux.git/blame - drivers/clk/clkdev.c
clk: shmobile: div6: Avoid division by zero in .round_rate()
[linux.git] / drivers / clk / clkdev.c
CommitLineData
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
27static LIST_HEAD(clocks);
28static DEFINE_MUTEX(clocks_mutex);
29
137f8a72 30#if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK)
7f05e28f
SN
31
32/**
33 * of_clk_get_by_clkspec() - Lookup a clock form a clock provider
34 * @clkspec: pointer to a clock specifier data structure
35 *
36 * This function looks up a struct clk from the registered list of clock
37 * providers, an input is a clock specifier data structure as returned
38 * from the of_parse_phandle_with_args() function call.
39 */
40struct clk *of_clk_get_by_clkspec(struct of_phandle_args *clkspec)
41{
42 struct clk *clk;
43
44 if (!clkspec)
45 return ERR_PTR(-EINVAL);
46
47 of_clk_lock();
48 clk = __of_clk_get_from_provider(clkspec);
49
50 if (!IS_ERR(clk) && !__clk_get(clk))
51 clk = ERR_PTR(-ENOENT);
52
53 of_clk_unlock();
54 return clk;
55}
56
035a61c3 57static struct clk *__of_clk_get(struct device_node *np, int index)
766e6a4e
GL
58{
59 struct of_phandle_args clkspec;
60 struct clk *clk;
61 int rc;
62
63 if (index < 0)
64 return ERR_PTR(-EINVAL);
65
66 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
67 &clkspec);
68 if (rc)
69 return ERR_PTR(rc);
70
7f05e28f 71 clk = of_clk_get_by_clkspec(&clkspec);
766e6a4e 72 of_node_put(clkspec.np);
035a61c3
TV
73
74 return clk;
75}
76
77struct clk *of_clk_get(struct device_node *np, int index)
78{
79 struct clk *clk = __of_clk_get(np, index);
80
81 if (!IS_ERR(clk))
82 clk = __clk_create_clk(__clk_get_hw(clk), np->full_name, NULL);
83
766e6a4e
GL
84 return clk;
85}
86EXPORT_SYMBOL(of_clk_get);
87
035a61c3
TV
88static struct clk *__of_clk_get_by_name(struct device_node *np,
89 const char *dev_id,
90 const char *name)
766e6a4e
GL
91{
92 struct clk *clk = ERR_PTR(-ENOENT);
93
94 /* Walk up the tree of devices looking for a clock that matches */
95 while (np) {
96 int index = 0;
97
98 /*
99 * For named clocks, first look up the name in the
100 * "clock-names" property. If it cannot be found, then
101 * index will be an error code, and of_clk_get() will fail.
102 */
103 if (name)
104 index = of_property_match_string(np, "clock-names", name);
035a61c3
TV
105 clk = __of_clk_get(np, index);
106 if (!IS_ERR(clk)) {
107 clk = __clk_create_clk(__clk_get_hw(clk), dev_id, name);
766e6a4e 108 break;
035a61c3 109 }
766e6a4e 110 else if (name && index >= 0) {
d8e53c3d
SB
111 if (PTR_ERR(clk) != -EPROBE_DEFER)
112 pr_err("ERROR: could not get clock %s:%s(%i)\n",
113 np->full_name, name ? name : "", index);
766e6a4e
GL
114 return clk;
115 }
116
117 /*
118 * No matching clock found on this node. If the parent node
119 * has a "clock-ranges" property, then we can try one of its
120 * clocks.
121 */
122 np = np->parent;
123 if (np && !of_get_property(np, "clock-ranges", NULL))
124 break;
125 }
126
127 return clk;
128}
035a61c3
TV
129
130/**
131 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
132 * @np: pointer to clock consumer node
133 * @name: name of consumer's clock input, or NULL for the first clock reference
134 *
135 * This function parses the clocks and clock-names properties,
136 * and uses them to look up the struct clk from the registered list of clock
137 * providers.
138 */
139struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
140{
141 if (!np)
142 return ERR_PTR(-ENOENT);
143
144 return __of_clk_get_by_name(np, np->full_name, name);
145}
766e6a4e 146EXPORT_SYMBOL(of_clk_get_by_name);
035a61c3
TV
147
148#else /* defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) */
149
150static struct clk *__of_clk_get_by_name(struct device_node *np,
151 const char *dev_id,
152 const char *name)
153{
154 return ERR_PTR(-ENOENT);
155}
766e6a4e
GL
156#endif
157
409dc360
RK
158/*
159 * Find the correct struct clk for the device and connection ID.
160 * We do slightly fuzzy matching here:
161 * An entry with a NULL ID is assumed to be a wildcard.
162 * If an entry has a device ID, it must match
163 * If an entry has a connection ID, it must match
164 * Then we take the most specific entry - with the following
659431fc 165 * order of precedence: dev+con > dev only > con only.
409dc360 166 */
e8bf8df9 167static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
0318e693 168{
e8bf8df9 169 struct clk_lookup *p, *cl = NULL;
67b50871
VK
170 int match, best_found = 0, best_possible = 0;
171
172 if (dev_id)
173 best_possible += 2;
174 if (con_id)
175 best_possible += 1;
0318e693
RK
176
177 list_for_each_entry(p, &clocks, node) {
0318e693 178 match = 0;
409dc360
RK
179 if (p->dev_id) {
180 if (!dev_id || strcmp(p->dev_id, dev_id))
181 continue;
182 match += 2;
183 }
184 if (p->con_id) {
185 if (!con_id || strcmp(p->con_id, con_id))
186 continue;
187 match += 1;
188 }
0318e693 189
67b50871 190 if (match > best_found) {
e8bf8df9 191 cl = p;
67b50871
VK
192 if (match != best_possible)
193 best_found = match;
e4bf5bec
VK
194 else
195 break;
0318e693
RK
196 }
197 }
e8bf8df9 198 return cl;
0318e693
RK
199}
200
05fd8e73 201struct clk *clk_get_sys(const char *dev_id, const char *con_id)
0318e693 202{
e8bf8df9 203 struct clk_lookup *cl;
035a61c3 204 struct clk *clk = NULL;
0318e693
RK
205
206 mutex_lock(&clocks_mutex);
035a61c3 207
e8bf8df9 208 cl = clk_find(dev_id, con_id);
035a61c3
TV
209 if (!cl)
210 goto out;
211
212 if (!__clk_get(cl->clk)) {
e8bf8df9 213 cl = NULL;
035a61c3
TV
214 goto out;
215 }
216
217#if defined(CONFIG_COMMON_CLK)
218 clk = __clk_create_clk(__clk_get_hw(cl->clk), dev_id, con_id);
219#else
220 clk = cl->clk;
221#endif
222
223out:
0318e693
RK
224 mutex_unlock(&clocks_mutex);
225
035a61c3 226 return cl ? clk : ERR_PTR(-ENOENT);
0318e693 227}
05fd8e73
SH
228EXPORT_SYMBOL(clk_get_sys);
229
230struct clk *clk_get(struct device *dev, const char *con_id)
231{
232 const char *dev_id = dev ? dev_name(dev) : NULL;
766e6a4e
GL
233 struct clk *clk;
234
235 if (dev) {
035a61c3
TV
236 clk = __of_clk_get_by_name(dev->of_node, dev_id, con_id);
237 if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER)
a34cd466 238 return clk;
766e6a4e 239 }
05fd8e73
SH
240
241 return clk_get_sys(dev_id, con_id);
242}
0318e693
RK
243EXPORT_SYMBOL(clk_get);
244
245void clk_put(struct clk *clk)
246{
247 __clk_put(clk);
248}
249EXPORT_SYMBOL(clk_put);
250
251void clkdev_add(struct clk_lookup *cl)
252{
253 mutex_lock(&clocks_mutex);
254 list_add_tail(&cl->node, &clocks);
255 mutex_unlock(&clocks_mutex);
256}
257EXPORT_SYMBOL(clkdev_add);
258
0a0300dc
RK
259void __init clkdev_add_table(struct clk_lookup *cl, size_t num)
260{
261 mutex_lock(&clocks_mutex);
262 while (num--) {
263 list_add_tail(&cl->node, &clocks);
264 cl++;
265 }
266 mutex_unlock(&clocks_mutex);
267}
268
0318e693
RK
269#define MAX_DEV_ID 20
270#define MAX_CON_ID 16
271
272struct clk_lookup_alloc {
273 struct clk_lookup cl;
274 char dev_id[MAX_DEV_ID];
275 char con_id[MAX_CON_ID];
276};
277
e9d7f406
RK
278static struct clk_lookup * __init_refok
279vclkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt,
280 va_list ap)
0318e693
RK
281{
282 struct clk_lookup_alloc *cla;
283
6d803ba7 284 cla = __clkdev_alloc(sizeof(*cla));
0318e693
RK
285 if (!cla)
286 return NULL;
287
288 cla->cl.clk = clk;
289 if (con_id) {
290 strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
291 cla->cl.con_id = cla->con_id;
292 }
293
294 if (dev_fmt) {
0318e693
RK
295 vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
296 cla->cl.dev_id = cla->dev_id;
0318e693
RK
297 }
298
299 return &cla->cl;
300}
e9d7f406
RK
301
302struct clk_lookup * __init_refok
303clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
304{
305 struct clk_lookup *cl;
306 va_list ap;
307
308 va_start(ap, dev_fmt);
309 cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
310 va_end(ap);
311
312 return cl;
313}
0318e693
RK
314EXPORT_SYMBOL(clkdev_alloc);
315
c0683039
TL
316int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
317 struct device *dev)
318{
319 struct clk *r = clk_get(dev, id);
320 struct clk_lookup *l;
321
322 if (IS_ERR(r))
323 return PTR_ERR(r);
324
325 l = clkdev_alloc(r, alias, alias_dev_name);
326 clk_put(r);
327 if (!l)
328 return -ENODEV;
329 clkdev_add(l);
330 return 0;
331}
332EXPORT_SYMBOL(clk_add_alias);
333
0318e693
RK
334/*
335 * clkdev_drop - remove a clock dynamically allocated
336 */
337void clkdev_drop(struct clk_lookup *cl)
338{
339 mutex_lock(&clocks_mutex);
340 list_del(&cl->node);
341 mutex_unlock(&clocks_mutex);
342 kfree(cl);
343}
344EXPORT_SYMBOL(clkdev_drop);
e9d7f406
RK
345
346/**
347 * clk_register_clkdev - register one clock lookup for a struct clk
348 * @clk: struct clk to associate with all clk_lookups
349 * @con_id: connection ID string on device
350 * @dev_id: format string describing device name
351 *
352 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
353 * clkdev.
354 *
355 * To make things easier for mass registration, we detect error clks
356 * from a previous clk_register() call, and return the error code for
357 * those. This is to permit this function to be called immediately
358 * after clk_register().
359 */
360int clk_register_clkdev(struct clk *clk, const char *con_id,
361 const char *dev_fmt, ...)
362{
363 struct clk_lookup *cl;
364 va_list ap;
365
366 if (IS_ERR(clk))
367 return PTR_ERR(clk);
368
369 va_start(ap, dev_fmt);
370 cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
371 va_end(ap);
372
373 if (!cl)
374 return -ENOMEM;
375
376 clkdev_add(cl);
377
378 return 0;
379}
a251361a 380EXPORT_SYMBOL(clk_register_clkdev);
e9d7f406
RK
381
382/**
383 * clk_register_clkdevs - register a set of clk_lookup for a struct clk
384 * @clk: struct clk to associate with all clk_lookups
385 * @cl: array of clk_lookup structures with con_id and dev_id pre-initialized
386 * @num: number of clk_lookup structures to register
387 *
388 * To make things easier for mass registration, we detect error clks
389 * from a previous clk_register() call, and return the error code for
390 * those. This is to permit this function to be called immediately
391 * after clk_register().
392 */
393int clk_register_clkdevs(struct clk *clk, struct clk_lookup *cl, size_t num)
394{
395 unsigned i;
396
397 if (IS_ERR(clk))
398 return PTR_ERR(clk);
399
400 for (i = 0; i < num; i++, cl++) {
401 cl->clk = clk;
402 clkdev_add(cl);
403 }
404
405 return 0;
406}
407EXPORT_SYMBOL(clk_register_clkdevs);
This page took 0.430344 seconds and 4 git commands to generate.