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