blob: f2f4f2afd28c68ddaf903058d42ebf09ff3f0c33 [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>
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010022#include <linux/clk-provider.h>
Grant Likely766e6a42012-04-09 14:50:06 -050023#include <linux/of.h>
Russell King0318e692008-11-09 16:32:46 +000024
Sylwester Nawrocki3a3d2b02013-08-23 17:03:44 +020025#include "clk.h"
26
Russell King0318e692008-11-09 16:32:46 +000027static LIST_HEAD(clocks);
28static DEFINE_MUTEX(clocks_mutex);
29
Rob Herring137f8a72012-07-18 11:52:23 +080030#if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK)
Stephen Boyd44722872018-12-19 10:59:55 -080031static struct clk_hw *of_clk_get_hw(struct device_node *np,
32 int index, const char *con_id)
Grant Likely766e6a42012-04-09 14:50:06 -050033{
Stephen Boyd44722872018-12-19 10:59:55 -080034 int ret;
35 struct clk_hw *hw;
Grant Likely766e6a42012-04-09 14:50:06 -050036 struct of_phandle_args clkspec;
Grant Likely766e6a42012-04-09 14:50:06 -050037
Stephen Boyd44722872018-12-19 10:59:55 -080038 ret = of_parse_clkspec(np, index, con_id, &clkspec);
39 if (ret)
40 return ERR_PTR(ret);
Grant Likely766e6a42012-04-09 14:50:06 -050041
Stephen Boyd44722872018-12-19 10:59:55 -080042 hw = of_clk_get_hw_from_clkspec(&clkspec);
Grant Likely766e6a42012-04-09 14:50:06 -050043 of_node_put(clkspec.np);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010044
Stephen Boyd44722872018-12-19 10:59:55 -080045 return hw;
46}
47
48static struct clk *__of_clk_get(struct device_node *np,
49 int index, const char *dev_id,
50 const char *con_id)
51{
52 struct clk_hw *hw = of_clk_get_hw(np, index, con_id);
53
Stephen Boydefa85042018-12-11 08:34:16 -080054 return clk_hw_create_clk(NULL, hw, dev_id, con_id);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010055}
56
57struct clk *of_clk_get(struct device_node *np, int index)
58{
Stephen Boyd73e0e492015-02-06 11:42:43 -080059 return __of_clk_get(np, index, np->full_name, NULL);
Grant Likely766e6a42012-04-09 14:50:06 -050060}
61EXPORT_SYMBOL(of_clk_get);
62
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010063/**
64 * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
65 * @np: pointer to clock consumer node
66 * @name: name of consumer's clock input, or NULL for the first clock reference
67 *
68 * This function parses the clocks and clock-names properties,
69 * and uses them to look up the struct clk from the registered list of clock
70 * providers.
71 */
72struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
73{
74 if (!np)
75 return ERR_PTR(-ENOENT);
76
Stephen Boyd44722872018-12-19 10:59:55 -080077 return __of_clk_get(np, -1, np->full_name, name);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010078}
Grant Likely766e6a42012-04-09 14:50:06 -050079EXPORT_SYMBOL(of_clk_get_by_name);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010080
81#else /* defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) */
82
Stephen Boyd44722872018-12-19 10:59:55 -080083static struct clk_hw *of_clk_get_hw(struct device_node *np,
84 int index, const char *con_id)
Tomeu Vizoso035a61c2015-01-23 12:03:30 +010085{
86 return ERR_PTR(-ENOENT);
87}
Grant Likely766e6a42012-04-09 14:50:06 -050088#endif
89
Russell King409dc362009-01-24 10:14:37 +000090/*
91 * Find the correct struct clk for the device and connection ID.
92 * We do slightly fuzzy matching here:
93 * An entry with a NULL ID is assumed to be a wildcard.
94 * If an entry has a device ID, it must match
95 * If an entry has a connection ID, it must match
96 * Then we take the most specific entry - with the following
Uwe Kleine-König659431f2010-01-18 16:02:48 +010097 * order of precedence: dev+con > dev only > con only.
Russell King409dc362009-01-24 10:14:37 +000098 */
Russell Kinge8bf8df2011-04-30 10:14:08 +010099static struct clk_lookup *clk_find(const char *dev_id, const char *con_id)
Russell King0318e692008-11-09 16:32:46 +0000100{
Russell Kinge8bf8df2011-04-30 10:14:08 +0100101 struct clk_lookup *p, *cl = NULL;
viresh kumar67b50872012-04-19 04:23:25 +0100102 int match, best_found = 0, best_possible = 0;
103
104 if (dev_id)
105 best_possible += 2;
106 if (con_id)
107 best_possible += 1;
Russell King0318e692008-11-09 16:32:46 +0000108
109 list_for_each_entry(p, &clocks, node) {
Russell King0318e692008-11-09 16:32:46 +0000110 match = 0;
Russell King409dc362009-01-24 10:14:37 +0000111 if (p->dev_id) {
112 if (!dev_id || strcmp(p->dev_id, dev_id))
113 continue;
114 match += 2;
115 }
116 if (p->con_id) {
117 if (!con_id || strcmp(p->con_id, con_id))
118 continue;
119 match += 1;
120 }
Russell King0318e692008-11-09 16:32:46 +0000121
viresh kumar67b50872012-04-19 04:23:25 +0100122 if (match > best_found) {
Russell Kinge8bf8df2011-04-30 10:14:08 +0100123 cl = p;
viresh kumar67b50872012-04-19 04:23:25 +0100124 if (match != best_possible)
125 best_found = match;
viresh kumare4bf5be2010-03-09 11:54:30 +0100126 else
127 break;
Russell King0318e692008-11-09 16:32:46 +0000128 }
129 }
Russell Kinge8bf8df2011-04-30 10:14:08 +0100130 return cl;
Russell King0318e692008-11-09 16:32:46 +0000131}
132
Stephen Boydefa85042018-12-11 08:34:16 -0800133static struct clk *__clk_get_sys(struct device *dev, const char *dev_id,
134 const char *con_id)
Russell King0318e692008-11-09 16:32:46 +0000135{
Russell Kinge8bf8df2011-04-30 10:14:08 +0100136 struct clk_lookup *cl;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100137 struct clk *clk = NULL;
Russell King0318e692008-11-09 16:32:46 +0000138
139 mutex_lock(&clocks_mutex);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100140
Russell Kinge8bf8df2011-04-30 10:14:08 +0100141 cl = clk_find(dev_id, con_id);
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100142 if (!cl)
143 goto out;
144
Stephen Boydefa85042018-12-11 08:34:16 -0800145 clk = clk_hw_create_clk(dev, cl->clk_hw, dev_id, con_id);
Stephen Boyd73e0e492015-02-06 11:42:43 -0800146 if (IS_ERR(clk))
Russell Kinge8bf8df2011-04-30 10:14:08 +0100147 cl = NULL;
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100148out:
Russell King0318e692008-11-09 16:32:46 +0000149 mutex_unlock(&clocks_mutex);
150
Tomeu Vizoso035a61c2015-01-23 12:03:30 +0100151 return cl ? clk : ERR_PTR(-ENOENT);
Russell King0318e692008-11-09 16:32:46 +0000152}
Stephen Boydefa85042018-12-11 08:34:16 -0800153
154struct clk *clk_get_sys(const char *dev_id, const char *con_id)
155{
156 return __clk_get_sys(NULL, dev_id, con_id);
157}
Sascha Hauer05fd8e732009-03-07 12:55:49 +0100158EXPORT_SYMBOL(clk_get_sys);
159
160struct clk *clk_get(struct device *dev, const char *con_id)
161{
162 const char *dev_id = dev ? dev_name(dev) : NULL;
Stephen Boyd44722872018-12-19 10:59:55 -0800163 struct clk_hw *hw;
Grant Likely766e6a42012-04-09 14:50:06 -0500164
Bartosz Golaszewski53ccb222018-06-28 15:42:20 +0100165 if (dev && dev->of_node) {
Stephen Boyd44722872018-12-19 10:59:55 -0800166 hw = of_clk_get_hw(dev->of_node, 0, con_id);
167 if (!IS_ERR(hw) || PTR_ERR(hw) == -EPROBE_DEFER)
Stephen Boydefa85042018-12-11 08:34:16 -0800168 return clk_hw_create_clk(dev, hw, dev_id, con_id);
Grant Likely766e6a42012-04-09 14:50:06 -0500169 }
Sascha Hauer05fd8e732009-03-07 12:55:49 +0100170
Stephen Boydefa85042018-12-11 08:34:16 -0800171 return __clk_get_sys(dev, dev_id, con_id);
Sascha Hauer05fd8e732009-03-07 12:55:49 +0100172}
Russell King0318e692008-11-09 16:32:46 +0000173EXPORT_SYMBOL(clk_get);
174
175void clk_put(struct clk *clk)
176{
177 __clk_put(clk);
178}
179EXPORT_SYMBOL(clk_put);
180
Russell Kingd5622a92015-03-02 15:45:41 +0000181static void __clkdev_add(struct clk_lookup *cl)
Russell King0318e692008-11-09 16:32:46 +0000182{
183 mutex_lock(&clocks_mutex);
184 list_add_tail(&cl->node, &clocks);
185 mutex_unlock(&clocks_mutex);
186}
Russell Kingd5622a92015-03-02 15:45:41 +0000187
188void clkdev_add(struct clk_lookup *cl)
189{
190 if (!cl->clk_hw)
191 cl->clk_hw = __clk_get_hw(cl->clk);
192 __clkdev_add(cl);
193}
Russell King0318e692008-11-09 16:32:46 +0000194EXPORT_SYMBOL(clkdev_add);
195
Russell Kingfba3acd2015-03-10 14:34:00 +0000196void clkdev_add_table(struct clk_lookup *cl, size_t num)
Russell King0a0300d2010-01-12 12:28:00 +0000197{
198 mutex_lock(&clocks_mutex);
199 while (num--) {
Russell Kingd5622a92015-03-02 15:45:41 +0000200 cl->clk_hw = __clk_get_hw(cl->clk);
Russell King0a0300d2010-01-12 12:28:00 +0000201 list_add_tail(&cl->node, &clocks);
202 cl++;
203 }
204 mutex_unlock(&clocks_mutex);
205}
206
Russell King0318e692008-11-09 16:32:46 +0000207#define MAX_DEV_ID 20
208#define MAX_CON_ID 16
209
210struct clk_lookup_alloc {
211 struct clk_lookup cl;
212 char dev_id[MAX_DEV_ID];
213 char con_id[MAX_CON_ID];
214};
215
Fabian Frederickbd721ea2016-08-02 14:03:33 -0700216static struct clk_lookup * __ref
Russell Kingd5622a92015-03-02 15:45:41 +0000217vclkdev_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
Russell Kinge9d7f402012-05-02 09:30:32 +0100218 va_list ap)
Russell King0318e692008-11-09 16:32:46 +0000219{
220 struct clk_lookup_alloc *cla;
221
Stephen Boyd0d4e3d002018-01-02 15:47:07 -0800222 cla = kzalloc(sizeof(*cla), GFP_KERNEL);
Russell King0318e692008-11-09 16:32:46 +0000223 if (!cla)
224 return NULL;
225
Russell Kingd5622a92015-03-02 15:45:41 +0000226 cla->cl.clk_hw = hw;
Russell King0318e692008-11-09 16:32:46 +0000227 if (con_id) {
228 strlcpy(cla->con_id, con_id, sizeof(cla->con_id));
229 cla->cl.con_id = cla->con_id;
230 }
231
232 if (dev_fmt) {
Russell King0318e692008-11-09 16:32:46 +0000233 vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap);
234 cla->cl.dev_id = cla->dev_id;
Russell King0318e692008-11-09 16:32:46 +0000235 }
236
237 return &cla->cl;
238}
Russell Kinge9d7f402012-05-02 09:30:32 +0100239
Russell King25689992015-03-02 15:40:29 +0000240static struct clk_lookup *
241vclkdev_create(struct clk_hw *hw, const char *con_id, const char *dev_fmt,
242 va_list ap)
243{
244 struct clk_lookup *cl;
245
246 cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
247 if (cl)
248 __clkdev_add(cl);
249
250 return cl;
251}
252
Fabian Frederickbd721ea2016-08-02 14:03:33 -0700253struct clk_lookup * __ref
Russell Kinge9d7f402012-05-02 09:30:32 +0100254clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...)
255{
256 struct clk_lookup *cl;
257 va_list ap;
258
259 va_start(ap, dev_fmt);
Russell Kingd5622a92015-03-02 15:45:41 +0000260 cl = vclkdev_alloc(__clk_get_hw(clk), con_id, dev_fmt, ap);
Russell Kinge9d7f402012-05-02 09:30:32 +0100261 va_end(ap);
262
263 return cl;
264}
Russell King0318e692008-11-09 16:32:46 +0000265EXPORT_SYMBOL(clkdev_alloc);
266
Stephen Boyde4f1b492016-02-08 14:59:49 -0800267struct clk_lookup *
268clkdev_hw_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt, ...)
269{
270 struct clk_lookup *cl;
271 va_list ap;
272
273 va_start(ap, dev_fmt);
274 cl = vclkdev_alloc(hw, con_id, dev_fmt, ap);
275 va_end(ap);
276
277 return cl;
278}
279EXPORT_SYMBOL(clkdev_hw_alloc);
280
Russell King25689992015-03-02 15:40:29 +0000281/**
282 * clkdev_create - allocate and add a clkdev lookup structure
283 * @clk: struct clk to associate with all clk_lookups
284 * @con_id: connection ID string on device
285 * @dev_fmt: format string describing device name
286 *
287 * Returns a clk_lookup structure, which can be later unregistered and
288 * freed.
289 */
290struct clk_lookup *clkdev_create(struct clk *clk, const char *con_id,
291 const char *dev_fmt, ...)
292{
293 struct clk_lookup *cl;
294 va_list ap;
295
296 va_start(ap, dev_fmt);
297 cl = vclkdev_create(__clk_get_hw(clk), con_id, dev_fmt, ap);
298 va_end(ap);
299
300 return cl;
301}
302EXPORT_SYMBOL_GPL(clkdev_create);
303
Stephen Boyde4f1b492016-02-08 14:59:49 -0800304/**
305 * clkdev_hw_create - allocate and add a clkdev lookup structure
306 * @hw: struct clk_hw to associate with all clk_lookups
307 * @con_id: connection ID string on device
308 * @dev_fmt: format string describing device name
309 *
310 * Returns a clk_lookup structure, which can be later unregistered and
311 * freed.
312 */
313struct clk_lookup *clkdev_hw_create(struct clk_hw *hw, const char *con_id,
314 const char *dev_fmt, ...)
315{
316 struct clk_lookup *cl;
317 va_list ap;
318
319 va_start(ap, dev_fmt);
320 cl = vclkdev_create(hw, con_id, dev_fmt, ap);
321 va_end(ap);
322
323 return cl;
324}
325EXPORT_SYMBOL_GPL(clkdev_hw_create);
326
Russell Kingb3d8d7e2015-03-09 10:43:04 +0000327int clk_add_alias(const char *alias, const char *alias_dev_name,
328 const char *con_id, struct device *dev)
Tony Lindgrenc0683032009-06-03 17:43:14 +0100329{
Russell Kingb3d8d7e2015-03-09 10:43:04 +0000330 struct clk *r = clk_get(dev, con_id);
Tony Lindgrenc0683032009-06-03 17:43:14 +0100331 struct clk_lookup *l;
332
333 if (IS_ERR(r))
334 return PTR_ERR(r);
335
Russell King625faa62015-10-20 11:49:44 +0100336 l = clkdev_create(r, alias, alias_dev_name ? "%s" : NULL,
337 alias_dev_name);
Tony Lindgrenc0683032009-06-03 17:43:14 +0100338 clk_put(r);
Russell King25689992015-03-02 15:40:29 +0000339
340 return l ? 0 : -ENODEV;
Tony Lindgrenc0683032009-06-03 17:43:14 +0100341}
342EXPORT_SYMBOL(clk_add_alias);
343
Russell King0318e692008-11-09 16:32:46 +0000344/*
345 * clkdev_drop - remove a clock dynamically allocated
346 */
347void clkdev_drop(struct clk_lookup *cl)
348{
349 mutex_lock(&clocks_mutex);
350 list_del(&cl->node);
351 mutex_unlock(&clocks_mutex);
352 kfree(cl);
353}
354EXPORT_SYMBOL(clkdev_drop);
Russell Kinge9d7f402012-05-02 09:30:32 +0100355
Kees Cook416dd132016-01-26 01:21:26 +0100356static struct clk_lookup *__clk_register_clkdev(struct clk_hw *hw,
357 const char *con_id,
358 const char *dev_id, ...)
359{
360 struct clk_lookup *cl;
361 va_list ap;
362
363 va_start(ap, dev_id);
364 cl = vclkdev_create(hw, con_id, dev_id, ap);
365 va_end(ap);
366
367 return cl;
368}
369
Russell Kinge9d7f402012-05-02 09:30:32 +0100370/**
371 * clk_register_clkdev - register one clock lookup for a struct clk
372 * @clk: struct clk to associate with all clk_lookups
373 * @con_id: connection ID string on device
Kees Cook416dd132016-01-26 01:21:26 +0100374 * @dev_id: string describing device name
Russell Kinge9d7f402012-05-02 09:30:32 +0100375 *
376 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
377 * clkdev.
378 *
379 * To make things easier for mass registration, we detect error clks
380 * from a previous clk_register() call, and return the error code for
381 * those. This is to permit this function to be called immediately
382 * after clk_register().
383 */
384int clk_register_clkdev(struct clk *clk, const char *con_id,
Kees Cook416dd132016-01-26 01:21:26 +0100385 const char *dev_id)
Russell Kinge9d7f402012-05-02 09:30:32 +0100386{
387 struct clk_lookup *cl;
Russell Kinge9d7f402012-05-02 09:30:32 +0100388
389 if (IS_ERR(clk))
390 return PTR_ERR(clk);
391
Kees Cook416dd132016-01-26 01:21:26 +0100392 /*
393 * Since dev_id can be NULL, and NULL is handled specially, we must
394 * pass it as either a NULL format string, or with "%s".
395 */
396 if (dev_id)
397 cl = __clk_register_clkdev(__clk_get_hw(clk), con_id, "%s",
398 dev_id);
399 else
400 cl = __clk_register_clkdev(__clk_get_hw(clk), con_id, NULL);
Russell Kinge9d7f402012-05-02 09:30:32 +0100401
Russell King25689992015-03-02 15:40:29 +0000402 return cl ? 0 : -ENOMEM;
Russell Kinge9d7f402012-05-02 09:30:32 +0100403}
Tomeu Vizosoa251361a2015-01-23 12:03:32 +0100404EXPORT_SYMBOL(clk_register_clkdev);
Stephen Boyde4f1b492016-02-08 14:59:49 -0800405
406/**
407 * clk_hw_register_clkdev - register one clock lookup for a struct clk_hw
408 * @hw: struct clk_hw to associate with all clk_lookups
409 * @con_id: connection ID string on device
410 * @dev_id: format string describing device name
411 *
412 * con_id or dev_id may be NULL as a wildcard, just as in the rest of
413 * clkdev.
Geert Uytterhoeven93880932016-11-22 12:33:11 +0100414 *
415 * To make things easier for mass registration, we detect error clk_hws
416 * from a previous clk_hw_register_*() call, and return the error code for
417 * those. This is to permit this function to be called immediately
418 * after clk_hw_register_*().
Stephen Boyde4f1b492016-02-08 14:59:49 -0800419 */
420int clk_hw_register_clkdev(struct clk_hw *hw, const char *con_id,
421 const char *dev_id)
422{
423 struct clk_lookup *cl;
424
Geert Uytterhoeven93880932016-11-22 12:33:11 +0100425 if (IS_ERR(hw))
426 return PTR_ERR(hw);
427
Stephen Boyde4f1b492016-02-08 14:59:49 -0800428 /*
429 * Since dev_id can be NULL, and NULL is handled specially, we must
430 * pass it as either a NULL format string, or with "%s".
431 */
432 if (dev_id)
433 cl = __clk_register_clkdev(hw, con_id, "%s", dev_id);
434 else
435 cl = __clk_register_clkdev(hw, con_id, NULL);
436
437 return cl ? 0 : -ENOMEM;
438}
439EXPORT_SYMBOL(clk_hw_register_clkdev);