blob: f890b901c6bc62f9d38ea7b16ae3d05fd3fb1df5 [file] [log] [blame]
Russell King0318e692008-11-09 16:32:46 +00001/*
Jean-Christop PLAGNIOL-VILLARD6d803ba2010-11-17 10:04:33 +01002 * drivers/clk/clkdev.c
Russell King0318e692008-11-09 16:32:46 +00003 *
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>
Hartley Sweetenc0c60c42009-08-04 23:38:06 +010020#include <linux/clk.h>
Jean-Christop PLAGNIOL-VILLARD6d803ba2010-11-17 10:04:33 +010021#include <linux/clkdev.h>
Grant Likely766e6a42012-04-09 14:50:06 -050022#include <linux/of.h>
Russell King0318e692008-11-09 16:32:46 +000023
Sylwester Nawrocki3a3d2b02013-08-23 17:03:44 +020024#include "clk.h"
25
Russell King0318e692008-11-09 16:32:46 +000026static LIST_HEAD(clocks);
27static DEFINE_MUTEX(clocks_mutex);
28
Rob Herring137f8a72012-07-18 11:52:23 +080029#if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK)
Sylwester Nawrocki7f05e282014-05-19 19:22:50 +020030
31/**
32 * of_clk_get_by_clkspec() - Lookup a clock form a clock provider
33 * @clkspec: pointer to a clock specifier data structure
34 *
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.
38 */
39struct clk *of_clk_get_by_clkspec(struct of_phandle_args *clkspec)
40{
41 struct clk *clk;
42
43 if (!clkspec)
44 return ERR_PTR(-EINVAL);
45
46 of_clk_lock();
47 clk = __of_clk_get_from_provider(clkspec);
48
49 if (!IS_ERR(clk) && !__clk_get(clk))
50 clk = ERR_PTR(-ENOENT);
51
52 of_clk_unlock();
53 return clk;
54}
55
Grant Likely766e6a42012-04-09 14:50:06 -050056struct clk *of_clk_get(struct device_node *np, int index)
57{
58 struct of_phandle_args clkspec;
59 struct clk *clk;
60 int rc;
61
62 if (index < 0)
63 return ERR_PTR(-EINVAL);
64
65 rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index,
66 &clkspec);
67 if (rc)
68 return ERR_PTR(rc);
69
Sylwester Nawrocki7f05e282014-05-19 19:22:50 +020070 clk = of_clk_get_by_clkspec(&clkspec);
Grant Likely766e6a42012-04-09 14:50:06 -050071 of_node_put(clkspec.np);
72 return clk;
73}
74EXPORT_SYMBOL(of_clk_get);
75
76/**
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
80 *
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
83 * providers.
84 */
85struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
86{
87 struct clk *clk = ERR_PTR(-ENOENT);
88
89 /* Walk up the tree of devices looking for a clock that matches */
90 while (np) {
91 int index = 0;
92
93 /*
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.
97 */
98 if (name)
99 index = of_property_match_string(np, "clock-names", name);
100 clk = of_clk_get(np, index);
101 if (!IS_ERR(clk))
102 break;
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);
106 return clk;
107 }
108
109 /*
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
112 * clocks.
113 */
114 np = np->parent;
115 if (np && !of_get_property(np, "clock-ranges", NULL))
116 break;
117 }
118
119 return clk;
120}
121EXPORT_SYMBOL(of_clk_get_by_name);
122#endif
123
Russell King409dc362009-01-24 10:14:37 +0000124/*
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
Uwe Kleine-König659431f2010-01-18 16:02:48 +0100131 * order of precedence: dev+con > dev only > con only.
Russell King409dc362009-01-24 10:14:37 +0000132 */
Russell Kinge8bf8df2011-04-30 10:14:08 +0100133static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
Russell King0318e692008-11-09 16:32:46 +0000134{
Russell Kinge8bf8df2011-04-30 10:14:08 +0100135 struct clk_lookup *p, *cl = NULL;
viresh kumar67b50872012-04-19 04:23:25 +0100136 int match, best_found = 0, best_possible = 0;
137
138 if (dev_id)
139 best_possible += 2;
140 if (con_id)
141 best_possible += 1;
Russell King0318e692008-11-09 16:32:46 +0000142
143 list_for_each_entry(p, &clocks, node) {
Russell King0318e692008-11-09 16:32:46 +0000144 match = 0;
Russell King409dc362009-01-24 10:14:37 +0000145 if (p->dev_id) {
146 if (!dev_id || strcmp(p->dev_id, dev_id))
147 continue;
148 match += 2;
149 }
150 if (p->con_id) {
151 if (!con_id || strcmp(p->con_id, con_id))
152 continue;
153 match += 1;
154 }
Russell King0318e692008-11-09 16:32:46 +0000155
viresh kumar67b50872012-04-19 04:23:25 +0100156 if (match > best_found) {
Russell Kinge8bf8df2011-04-30 10:14:08 +0100157 cl = p;
viresh kumar67b50872012-04-19 04:23:25 +0100158 if (match != best_possible)
159 best_found = match;
viresh kumare4bf5be2010-03-09 11:54:30 +0100160 else
161 break;
Russell King0318e692008-11-09 16:32:46 +0000162 }
163 }
Russell Kinge8bf8df2011-04-30 10:14:08 +0100164 return cl;
Russell King0318e692008-11-09 16:32:46 +0000165}
166
Sascha Hauer05fd8e72009-03-07 12:55:49 +0100167struct clk *clk_get_sys(const char *dev_id, const char *con_id)
Russell King0318e692008-11-09 16:32:46 +0000168{
Russell Kinge8bf8df2011-04-30 10:14:08 +0100169 struct clk_lookup *cl;
Russell King0318e692008-11-09 16:32:46 +0000170
171 mutex_lock(&clocks_mutex);
Russell Kinge8bf8df2011-04-30 10:14:08 +0100172 cl = clk_find(dev_id, con_id);
173 if (cl && !__clk_get(cl->clk))
174 cl = NULL;
Russell King0318e692008-11-09 16:32:46 +0000175 mutex_unlock(&clocks_mutex);
176
Russell Kinge8bf8df2011-04-30 10:14:08 +0100177 return cl ? cl->clk : ERR_PTR(-ENOENT);
Russell King0318e692008-11-09 16:32:46 +0000178}
Sascha Hauer05fd8e72009-03-07 12:55:49 +0100179EXPORT_SYMBOL(clk_get_sys);
180
181struct clk *clk_get(struct device *dev, const char *con_id)
182{
183 const char *dev_id = dev ? dev_name(dev) : NULL;
Grant Likely766e6a42012-04-09 14:50:06 -0500184 struct clk *clk;
185
186 if (dev) {
187 clk = of_clk_get_by_name(dev->of_node, con_id);
Sylwester Nawrocki3a3d2b02013-08-23 17:03:44 +0200188 if (!IS_ERR(clk))
Grant Likely766e6a42012-04-09 14:50:06 -0500189 return clk;
Jean-Francois Moinea34cd462013-11-25 19:47:04 +0100190 if (PTR_ERR(clk) == -EPROBE_DEFER)
191 return clk;
Grant Likely766e6a42012-04-09 14:50:06 -0500192 }
Sascha Hauer05fd8e72009-03-07 12:55:49 +0100193
194 return clk_get_sys(dev_id, con_id);
195}
Russell King0318e692008-11-09 16:32:46 +0000196EXPORT_SYMBOL(clk_get);
197
198void clk_put(struct clk *clk)
199{
200 __clk_put(clk);
201}
202EXPORT_SYMBOL(clk_put);
203
204void clkdev_add(struct clk_lookup *cl)
205{
206 mutex_lock(&clocks_mutex);
207 list_add_tail(&cl->node, &clocks);
208 mutex_unlock(&clocks_mutex);
209}
210EXPORT_SYMBOL(clkdev_add);
211
Russell King0a0300d2010-01-12 12:28:00 +0000212void __init clkdev_add_table(struct clk_lookup *cl, size_t num)
213{
214 mutex_lock(&clocks_mutex);
215 while (num--) {
216 list_add_tail(&cl->node, &clocks);
217 cl++;
218 }
219 mutex_unlock(&clocks_mutex);
220}
221
Russell King0318e692008-11-09 16:32:46 +0000222#define MAX_DEV_ID 20
223#define MAX_CON_ID 16
224
225struct clk_lookup_alloc {
226 struct clk_lookup cl;
227 char dev_id[MAX_DEV_ID];
228 char con_id[MAX_CON_ID];
229};
230
Russell Kinge9d7f402012-05-02 09:30:32 +0100231static struct clk_lookup * __init_refok
232vclkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt,
233 va_list ap)
Russell King0318e692008-11-09 16:32:46 +0000234{
235 struct clk_lookup_alloc *cla;
236
Jean-Christop PLAGNIOL-VILLARD6d803ba2010-11-17 10:04:33 +0100237 cla = __clkdev_alloc(sizeof(*cla));
Russell King0318e692008-11-09 16:32:46 +0000238 if (!cla)
239 return NULL;
240
241 cla->cl.clk = clk;
242 if (con_id) {
243 strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
244 cla->cl.con_id = cla->con_id;
245 }
246
247 if (dev_fmt) {
Russell King0318e692008-11-09 16:32:46 +0000248 vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
249 cla->cl.dev_id = cla->dev_id;
Russell King0318e692008-11-09 16:32:46 +0000250 }
251
252 return &cla->cl;
253}
Russell Kinge9d7f402012-05-02 09:30:32 +0100254
255struct clk_lookup * __init_refok
256clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
257{
258 struct clk_lookup *cl;
259 va_list ap;
260
261 va_start(ap, dev_fmt);
262 cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
263 va_end(ap);
264
265 return cl;
266}
Russell King0318e692008-11-09 16:32:46 +0000267EXPORT_SYMBOL(clkdev_alloc);
268
Tony Lindgrenc0683032009-06-03 17:43:14 +0100269int clk_add_alias(const char *alias, const char *alias_dev_name, char *id,
270 struct device *dev)
271{
272 struct clk *r = clk_get(dev, id);
273 struct clk_lookup *l;
274
275 if (IS_ERR(r))
276 return PTR_ERR(r);
277
278 l = clkdev_alloc(r, alias, alias_dev_name);
279 clk_put(r);
280 if (!l)
281 return -ENODEV;
282 clkdev_add(l);
283 return 0;
284}
285EXPORT_SYMBOL(clk_add_alias);
286
Russell King0318e692008-11-09 16:32:46 +0000287/*
288 * clkdev_drop - remove a clock dynamically allocated
289 */
290void clkdev_drop(struct clk_lookup *cl)
291{
292 mutex_lock(&clocks_mutex);
293 list_del(&cl->node);
294 mutex_unlock(&clocks_mutex);
295 kfree(cl);
296}
297EXPORT_SYMBOL(clkdev_drop);
Russell Kinge9d7f402012-05-02 09:30:32 +0100298
299/**
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
304 *
305 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
306 * clkdev.
307 *
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().
312 */
313int clk_register_clkdev(struct clk *clk, const char *con_id,
314 const char *dev_fmt, ...)
315{
316 struct clk_lookup *cl;
317 va_list ap;
318
319 if (IS_ERR(clk))
320 return PTR_ERR(clk);
321
322 va_start(ap, dev_fmt);
323 cl = vclkdev_alloc(clk, con_id, dev_fmt, ap);
324 va_end(ap);
325
326 if (!cl)
327 return -ENOMEM;
328
329 clkdev_add(cl);
330
331 return 0;
332}
333
334/**
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
339 *
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().
344 */
345int clk_register_clkdevs(struct clk *clk, struct clk_lookup *cl, size_t num)
346{
347 unsigned i;
348
349 if (IS_ERR(clk))
350 return PTR_ERR(clk);
351
352 for (i = 0; i < num; i++, cl++) {
353 cl->clk = clk;
354 clkdev_add(cl);
355 }
356
357 return 0;
358}
359EXPORT_SYMBOL(clk_register_clkdevs);