Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 1 | /* |
Jean-Christop PLAGNIOL-VILLARD | 6d803ba | 2010-11-17 10:04:33 +0100 | [diff] [blame] | 2 | * drivers/clk/clkdev.c |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 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> |
Hartley Sweeten | c0c60c4 | 2009-08-04 23:38:06 +0100 | [diff] [blame] | 20 | #include <linux/clk.h> |
Jean-Christop PLAGNIOL-VILLARD | 6d803ba | 2010-11-17 10:04:33 +0100 | [diff] [blame] | 21 | #include <linux/clkdev.h> |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 22 | #include <linux/of.h> |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 23 | |
Sylwester Nawrocki | 3a3d2b0 | 2013-08-23 17:03:44 +0200 | [diff] [blame] | 24 | #include "clk.h" |
| 25 | |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 26 | static LIST_HEAD(clocks); |
| 27 | static DEFINE_MUTEX(clocks_mutex); |
| 28 | |
Rob Herring | 137f8a7 | 2012-07-18 11:52:23 +0800 | [diff] [blame] | 29 | #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) |
Sylwester Nawrocki | 7f05e28 | 2014-05-19 19:22:50 +0200 | [diff] [blame] | 30 | |
| 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 | */ |
| 39 | struct 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 Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 56 | struct 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 Nawrocki | 7f05e28 | 2014-05-19 19:22:50 +0200 | [diff] [blame] | 70 | clk = of_clk_get_by_clkspec(&clkspec); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 71 | of_node_put(clkspec.np); |
| 72 | return clk; |
| 73 | } |
| 74 | EXPORT_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 | */ |
| 85 | struct 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) { |
Stephen Boyd | d8e53c3 | 2014-06-13 16:36:31 -0700 | [diff] [blame] | 104 | if (PTR_ERR(clk) != -EPROBE_DEFER) |
| 105 | pr_err("ERROR: could not get clock %s:%s(%i)\n", |
| 106 | np->full_name, name ? name : "", index); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 107 | return clk; |
| 108 | } |
| 109 | |
| 110 | /* |
| 111 | * No matching clock found on this node. If the parent node |
| 112 | * has a "clock-ranges" property, then we can try one of its |
| 113 | * clocks. |
| 114 | */ |
| 115 | np = np->parent; |
| 116 | if (np && !of_get_property(np, "clock-ranges", NULL)) |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | return clk; |
| 121 | } |
| 122 | EXPORT_SYMBOL(of_clk_get_by_name); |
| 123 | #endif |
| 124 | |
Russell King | 409dc36 | 2009-01-24 10:14:37 +0000 | [diff] [blame] | 125 | /* |
| 126 | * Find the correct struct clk for the device and connection ID. |
| 127 | * We do slightly fuzzy matching here: |
| 128 | * An entry with a NULL ID is assumed to be a wildcard. |
| 129 | * If an entry has a device ID, it must match |
| 130 | * If an entry has a connection ID, it must match |
| 131 | * Then we take the most specific entry - with the following |
Uwe Kleine-König | 659431f | 2010-01-18 16:02:48 +0100 | [diff] [blame] | 132 | * order of precedence: dev+con > dev only > con only. |
Russell King | 409dc36 | 2009-01-24 10:14:37 +0000 | [diff] [blame] | 133 | */ |
Russell King | e8bf8df | 2011-04-30 10:14:08 +0100 | [diff] [blame] | 134 | static struct clk_lookup *clk_find(const char *dev_id, const char *con_id) |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 135 | { |
Russell King | e8bf8df | 2011-04-30 10:14:08 +0100 | [diff] [blame] | 136 | struct clk_lookup *p, *cl = NULL; |
viresh kumar | 67b5087 | 2012-04-19 04:23:25 +0100 | [diff] [blame] | 137 | int match, best_found = 0, best_possible = 0; |
| 138 | |
| 139 | if (dev_id) |
| 140 | best_possible += 2; |
| 141 | if (con_id) |
| 142 | best_possible += 1; |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 143 | |
| 144 | list_for_each_entry(p, &clocks, node) { |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 145 | match = 0; |
Russell King | 409dc36 | 2009-01-24 10:14:37 +0000 | [diff] [blame] | 146 | if (p->dev_id) { |
| 147 | if (!dev_id || strcmp(p->dev_id, dev_id)) |
| 148 | continue; |
| 149 | match += 2; |
| 150 | } |
| 151 | if (p->con_id) { |
| 152 | if (!con_id || strcmp(p->con_id, con_id)) |
| 153 | continue; |
| 154 | match += 1; |
| 155 | } |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 156 | |
viresh kumar | 67b5087 | 2012-04-19 04:23:25 +0100 | [diff] [blame] | 157 | if (match > best_found) { |
Russell King | e8bf8df | 2011-04-30 10:14:08 +0100 | [diff] [blame] | 158 | cl = p; |
viresh kumar | 67b5087 | 2012-04-19 04:23:25 +0100 | [diff] [blame] | 159 | if (match != best_possible) |
| 160 | best_found = match; |
viresh kumar | e4bf5be | 2010-03-09 11:54:30 +0100 | [diff] [blame] | 161 | else |
| 162 | break; |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
Russell King | e8bf8df | 2011-04-30 10:14:08 +0100 | [diff] [blame] | 165 | return cl; |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Sascha Hauer | 05fd8e73 | 2009-03-07 12:55:49 +0100 | [diff] [blame] | 168 | struct clk *clk_get_sys(const char *dev_id, const char *con_id) |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 169 | { |
Russell King | e8bf8df | 2011-04-30 10:14:08 +0100 | [diff] [blame] | 170 | struct clk_lookup *cl; |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 171 | |
| 172 | mutex_lock(&clocks_mutex); |
Russell King | e8bf8df | 2011-04-30 10:14:08 +0100 | [diff] [blame] | 173 | cl = clk_find(dev_id, con_id); |
| 174 | if (cl && !__clk_get(cl->clk)) |
| 175 | cl = NULL; |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 176 | mutex_unlock(&clocks_mutex); |
| 177 | |
Russell King | e8bf8df | 2011-04-30 10:14:08 +0100 | [diff] [blame] | 178 | return cl ? cl->clk : ERR_PTR(-ENOENT); |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 179 | } |
Sascha Hauer | 05fd8e73 | 2009-03-07 12:55:49 +0100 | [diff] [blame] | 180 | EXPORT_SYMBOL(clk_get_sys); |
| 181 | |
| 182 | struct clk *clk_get(struct device *dev, const char *con_id) |
| 183 | { |
| 184 | const char *dev_id = dev ? dev_name(dev) : NULL; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 185 | struct clk *clk; |
| 186 | |
| 187 | if (dev) { |
| 188 | clk = of_clk_get_by_name(dev->of_node, con_id); |
Sylwester Nawrocki | 3a3d2b0 | 2013-08-23 17:03:44 +0200 | [diff] [blame] | 189 | if (!IS_ERR(clk)) |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 190 | return clk; |
Jean-Francois Moine | a34cd46 | 2013-11-25 19:47:04 +0100 | [diff] [blame] | 191 | if (PTR_ERR(clk) == -EPROBE_DEFER) |
| 192 | return clk; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 193 | } |
Sascha Hauer | 05fd8e73 | 2009-03-07 12:55:49 +0100 | [diff] [blame] | 194 | |
| 195 | return clk_get_sys(dev_id, con_id); |
| 196 | } |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 197 | EXPORT_SYMBOL(clk_get); |
| 198 | |
| 199 | void clk_put(struct clk *clk) |
| 200 | { |
| 201 | __clk_put(clk); |
| 202 | } |
| 203 | EXPORT_SYMBOL(clk_put); |
| 204 | |
| 205 | void clkdev_add(struct clk_lookup *cl) |
| 206 | { |
| 207 | mutex_lock(&clocks_mutex); |
| 208 | list_add_tail(&cl->node, &clocks); |
| 209 | mutex_unlock(&clocks_mutex); |
| 210 | } |
| 211 | EXPORT_SYMBOL(clkdev_add); |
| 212 | |
Russell King | 0a0300d | 2010-01-12 12:28:00 +0000 | [diff] [blame] | 213 | void __init clkdev_add_table(struct clk_lookup *cl, size_t num) |
| 214 | { |
| 215 | mutex_lock(&clocks_mutex); |
| 216 | while (num--) { |
| 217 | list_add_tail(&cl->node, &clocks); |
| 218 | cl++; |
| 219 | } |
| 220 | mutex_unlock(&clocks_mutex); |
| 221 | } |
| 222 | |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 223 | #define MAX_DEV_ID 20 |
| 224 | #define MAX_CON_ID 16 |
| 225 | |
| 226 | struct clk_lookup_alloc { |
| 227 | struct clk_lookup cl; |
| 228 | char dev_id[MAX_DEV_ID]; |
| 229 | char con_id[MAX_CON_ID]; |
| 230 | }; |
| 231 | |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 232 | static struct clk_lookup * __init_refok |
| 233 | vclkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, |
| 234 | va_list ap) |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 235 | { |
| 236 | struct clk_lookup_alloc *cla; |
| 237 | |
Jean-Christop PLAGNIOL-VILLARD | 6d803ba | 2010-11-17 10:04:33 +0100 | [diff] [blame] | 238 | cla = __clkdev_alloc(sizeof(*cla)); |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 239 | if (!cla) |
| 240 | return NULL; |
| 241 | |
| 242 | cla->cl.clk = clk; |
| 243 | if (con_id) { |
| 244 | strlcpy(cla->con_id, con_id, sizeof(cla->con_id)); |
| 245 | cla->cl.con_id = cla->con_id; |
| 246 | } |
| 247 | |
| 248 | if (dev_fmt) { |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 249 | vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap); |
| 250 | cla->cl.dev_id = cla->dev_id; |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | return &cla->cl; |
| 254 | } |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 255 | |
| 256 | struct clk_lookup * __init_refok |
| 257 | clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...) |
| 258 | { |
| 259 | struct clk_lookup *cl; |
| 260 | va_list ap; |
| 261 | |
| 262 | va_start(ap, dev_fmt); |
| 263 | cl = vclkdev_alloc(clk, con_id, dev_fmt, ap); |
| 264 | va_end(ap); |
| 265 | |
| 266 | return cl; |
| 267 | } |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 268 | EXPORT_SYMBOL(clkdev_alloc); |
| 269 | |
Tony Lindgren | c068303 | 2009-06-03 17:43:14 +0100 | [diff] [blame] | 270 | int clk_add_alias(const char *alias, const char *alias_dev_name, char *id, |
| 271 | struct device *dev) |
| 272 | { |
| 273 | struct clk *r = clk_get(dev, id); |
| 274 | struct clk_lookup *l; |
| 275 | |
| 276 | if (IS_ERR(r)) |
| 277 | return PTR_ERR(r); |
| 278 | |
| 279 | l = clkdev_alloc(r, alias, alias_dev_name); |
| 280 | clk_put(r); |
| 281 | if (!l) |
| 282 | return -ENODEV; |
| 283 | clkdev_add(l); |
| 284 | return 0; |
| 285 | } |
| 286 | EXPORT_SYMBOL(clk_add_alias); |
| 287 | |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 288 | /* |
| 289 | * clkdev_drop - remove a clock dynamically allocated |
| 290 | */ |
| 291 | void clkdev_drop(struct clk_lookup *cl) |
| 292 | { |
| 293 | mutex_lock(&clocks_mutex); |
| 294 | list_del(&cl->node); |
| 295 | mutex_unlock(&clocks_mutex); |
| 296 | kfree(cl); |
| 297 | } |
| 298 | EXPORT_SYMBOL(clkdev_drop); |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 299 | |
| 300 | /** |
| 301 | * clk_register_clkdev - register one clock lookup for a struct clk |
| 302 | * @clk: struct clk to associate with all clk_lookups |
| 303 | * @con_id: connection ID string on device |
| 304 | * @dev_id: format string describing device name |
| 305 | * |
| 306 | * con_id or dev_id may be NULL as a wildcard, just as in the rest of |
| 307 | * clkdev. |
| 308 | * |
| 309 | * To make things easier for mass registration, we detect error clks |
| 310 | * from a previous clk_register() call, and return the error code for |
| 311 | * those. This is to permit this function to be called immediately |
| 312 | * after clk_register(). |
| 313 | */ |
| 314 | int clk_register_clkdev(struct clk *clk, const char *con_id, |
| 315 | const char *dev_fmt, ...) |
| 316 | { |
| 317 | struct clk_lookup *cl; |
| 318 | va_list ap; |
| 319 | |
| 320 | if (IS_ERR(clk)) |
| 321 | return PTR_ERR(clk); |
| 322 | |
| 323 | va_start(ap, dev_fmt); |
| 324 | cl = vclkdev_alloc(clk, con_id, dev_fmt, ap); |
| 325 | va_end(ap); |
| 326 | |
| 327 | if (!cl) |
| 328 | return -ENOMEM; |
| 329 | |
| 330 | clkdev_add(cl); |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * clk_register_clkdevs - register a set of clk_lookup for a struct clk |
| 337 | * @clk: struct clk to associate with all clk_lookups |
| 338 | * @cl: array of clk_lookup structures with con_id and dev_id pre-initialized |
| 339 | * @num: number of clk_lookup structures to register |
| 340 | * |
| 341 | * To make things easier for mass registration, we detect error clks |
| 342 | * from a previous clk_register() call, and return the error code for |
| 343 | * those. This is to permit this function to be called immediately |
| 344 | * after clk_register(). |
| 345 | */ |
| 346 | int clk_register_clkdevs(struct clk *clk, struct clk_lookup *cl, size_t num) |
| 347 | { |
| 348 | unsigned i; |
| 349 | |
| 350 | if (IS_ERR(clk)) |
| 351 | return PTR_ERR(clk); |
| 352 | |
| 353 | for (i = 0; i < num; i++, cl++) { |
| 354 | cl->clk = clk; |
| 355 | clkdev_add(cl); |
| 356 | } |
| 357 | |
| 358 | return 0; |
| 359 | } |
| 360 | EXPORT_SYMBOL(clk_register_clkdevs); |