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> |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 22 | #include <linux/clk-provider.h> |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 23 | #include <linux/of.h> |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 24 | |
Sylwester Nawrocki | 3a3d2b0 | 2013-08-23 17:03:44 +0200 | [diff] [blame] | 25 | #include "clk.h" |
| 26 | |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 27 | static LIST_HEAD(clocks); |
| 28 | static DEFINE_MUTEX(clocks_mutex); |
| 29 | |
Shefali Jain | 0dc6e78 | 2017-11-27 13:06:27 +0530 | [diff] [blame^] | 30 | #if defined(CONFIG_OF) |
Stephen Boyd | 73e0e49 | 2015-02-06 11:42:43 -0800 | [diff] [blame] | 31 | static struct clk *__of_clk_get(struct device_node *np, int index, |
| 32 | const char *dev_id, const char *con_id) |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 33 | { |
| 34 | struct of_phandle_args clkspec; |
| 35 | struct clk *clk; |
| 36 | int rc; |
| 37 | |
| 38 | if (index < 0) |
| 39 | return ERR_PTR(-EINVAL); |
| 40 | |
| 41 | rc = of_parse_phandle_with_args(np, "clocks", "#clock-cells", index, |
| 42 | &clkspec); |
| 43 | if (rc) |
| 44 | return ERR_PTR(rc); |
| 45 | |
Stephen Boyd | 306c342 | 2015-02-05 15:39:11 -0800 | [diff] [blame] | 46 | clk = __of_clk_get_from_provider(&clkspec, dev_id, con_id); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 47 | of_node_put(clkspec.np); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 48 | |
| 49 | return clk; |
| 50 | } |
| 51 | |
| 52 | struct clk *of_clk_get(struct device_node *np, int index) |
| 53 | { |
Stephen Boyd | 73e0e49 | 2015-02-06 11:42:43 -0800 | [diff] [blame] | 54 | return __of_clk_get(np, index, np->full_name, NULL); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 55 | } |
| 56 | EXPORT_SYMBOL(of_clk_get); |
| 57 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 58 | static struct clk *__of_clk_get_by_name(struct device_node *np, |
| 59 | const char *dev_id, |
| 60 | const char *name) |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 61 | { |
| 62 | struct clk *clk = ERR_PTR(-ENOENT); |
| 63 | |
| 64 | /* Walk up the tree of devices looking for a clock that matches */ |
| 65 | while (np) { |
| 66 | int index = 0; |
| 67 | |
| 68 | /* |
| 69 | * For named clocks, first look up the name in the |
| 70 | * "clock-names" property. If it cannot be found, then |
| 71 | * index will be an error code, and of_clk_get() will fail. |
| 72 | */ |
| 73 | if (name) |
| 74 | index = of_property_match_string(np, "clock-names", name); |
Stephen Boyd | 73e0e49 | 2015-02-06 11:42:43 -0800 | [diff] [blame] | 75 | clk = __of_clk_get(np, index, dev_id, name); |
Shefali Jain | 0dc6e78 | 2017-11-27 13:06:27 +0530 | [diff] [blame^] | 76 | if (!IS_ERR(clk)) |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 77 | break; |
Shefali Jain | 0dc6e78 | 2017-11-27 13:06:27 +0530 | [diff] [blame^] | 78 | else if (name && index >= 0) |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 79 | return clk; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 80 | |
| 81 | /* |
| 82 | * No matching clock found on this node. If the parent node |
| 83 | * has a "clock-ranges" property, then we can try one of its |
| 84 | * clocks. |
| 85 | */ |
| 86 | np = np->parent; |
| 87 | if (np && !of_get_property(np, "clock-ranges", NULL)) |
| 88 | break; |
| 89 | } |
| 90 | |
| 91 | return clk; |
| 92 | } |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node |
| 96 | * @np: pointer to clock consumer node |
| 97 | * @name: name of consumer's clock input, or NULL for the first clock reference |
| 98 | * |
| 99 | * This function parses the clocks and clock-names properties, |
| 100 | * and uses them to look up the struct clk from the registered list of clock |
| 101 | * providers. |
| 102 | */ |
| 103 | struct clk *of_clk_get_by_name(struct device_node *np, const char *name) |
| 104 | { |
| 105 | if (!np) |
| 106 | return ERR_PTR(-ENOENT); |
| 107 | |
| 108 | return __of_clk_get_by_name(np, np->full_name, name); |
| 109 | } |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 110 | EXPORT_SYMBOL(of_clk_get_by_name); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 111 | |
| 112 | #else /* defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) */ |
| 113 | |
| 114 | static struct clk *__of_clk_get_by_name(struct device_node *np, |
| 115 | const char *dev_id, |
| 116 | const char *name) |
| 117 | { |
| 118 | return ERR_PTR(-ENOENT); |
| 119 | } |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 120 | #endif |
| 121 | |
Russell King | 409dc36 | 2009-01-24 10:14:37 +0000 | [diff] [blame] | 122 | /* |
| 123 | * Find the correct struct clk for the device and connection ID. |
| 124 | * We do slightly fuzzy matching here: |
| 125 | * An entry with a NULL ID is assumed to be a wildcard. |
| 126 | * If an entry has a device ID, it must match |
| 127 | * If an entry has a connection ID, it must match |
| 128 | * Then we take the most specific entry - with the following |
Uwe Kleine-König | 659431f | 2010-01-18 16:02:48 +0100 | [diff] [blame] | 129 | * order of precedence: dev+con > dev only > con only. |
Russell King | 409dc36 | 2009-01-24 10:14:37 +0000 | [diff] [blame] | 130 | */ |
Russell King | e8bf8df | 2011-04-30 10:14:08 +0100 | [diff] [blame] | 131 | 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] | 132 | { |
Russell King | e8bf8df | 2011-04-30 10:14:08 +0100 | [diff] [blame] | 133 | struct clk_lookup *p, *cl = NULL; |
viresh kumar | 67b5087 | 2012-04-19 04:23:25 +0100 | [diff] [blame] | 134 | int match, best_found = 0, best_possible = 0; |
| 135 | |
| 136 | if (dev_id) |
| 137 | best_possible += 2; |
| 138 | if (con_id) |
| 139 | best_possible += 1; |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 140 | |
| 141 | list_for_each_entry(p, &clocks, node) { |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 142 | match = 0; |
Russell King | 409dc36 | 2009-01-24 10:14:37 +0000 | [diff] [blame] | 143 | if (p->dev_id) { |
| 144 | if (!dev_id || strcmp(p->dev_id, dev_id)) |
| 145 | continue; |
| 146 | match += 2; |
| 147 | } |
| 148 | if (p->con_id) { |
| 149 | if (!con_id || strcmp(p->con_id, con_id)) |
| 150 | continue; |
| 151 | match += 1; |
| 152 | } |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 153 | |
viresh kumar | 67b5087 | 2012-04-19 04:23:25 +0100 | [diff] [blame] | 154 | if (match > best_found) { |
Russell King | e8bf8df | 2011-04-30 10:14:08 +0100 | [diff] [blame] | 155 | cl = p; |
viresh kumar | 67b5087 | 2012-04-19 04:23:25 +0100 | [diff] [blame] | 156 | if (match != best_possible) |
| 157 | best_found = match; |
viresh kumar | e4bf5be | 2010-03-09 11:54:30 +0100 | [diff] [blame] | 158 | else |
| 159 | break; |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
Russell King | e8bf8df | 2011-04-30 10:14:08 +0100 | [diff] [blame] | 162 | return cl; |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Sascha Hauer | 05fd8e7 | 2009-03-07 12:55:49 +0100 | [diff] [blame] | 165 | 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] | 166 | { |
Russell King | e8bf8df | 2011-04-30 10:14:08 +0100 | [diff] [blame] | 167 | struct clk_lookup *cl; |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 168 | struct clk *clk = NULL; |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 169 | |
| 170 | mutex_lock(&clocks_mutex); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 171 | |
Russell King | e8bf8df | 2011-04-30 10:14:08 +0100 | [diff] [blame] | 172 | cl = clk_find(dev_id, con_id); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 173 | if (!cl) |
| 174 | goto out; |
| 175 | |
Russell King | d5622a9 | 2015-03-02 15:45:41 +0000 | [diff] [blame] | 176 | clk = __clk_create_clk(cl->clk_hw, dev_id, con_id); |
Stephen Boyd | 73e0e49 | 2015-02-06 11:42:43 -0800 | [diff] [blame] | 177 | if (IS_ERR(clk)) |
| 178 | goto out; |
| 179 | |
| 180 | if (!__clk_get(clk)) { |
| 181 | __clk_free_clk(clk); |
Russell King | e8bf8df | 2011-04-30 10:14:08 +0100 | [diff] [blame] | 182 | cl = NULL; |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 183 | goto out; |
| 184 | } |
| 185 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 186 | out: |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 187 | mutex_unlock(&clocks_mutex); |
| 188 | |
Shefali Jain | 0dc6e78 | 2017-11-27 13:06:27 +0530 | [diff] [blame^] | 189 | return cl ? cl->clk : ERR_PTR(-ENOENT); |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 190 | } |
Sascha Hauer | 05fd8e7 | 2009-03-07 12:55:49 +0100 | [diff] [blame] | 191 | EXPORT_SYMBOL(clk_get_sys); |
| 192 | |
| 193 | struct clk *clk_get(struct device *dev, const char *con_id) |
| 194 | { |
| 195 | const char *dev_id = dev ? dev_name(dev) : NULL; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 196 | struct clk *clk; |
| 197 | |
| 198 | if (dev) { |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 199 | clk = __of_clk_get_by_name(dev->of_node, dev_id, con_id); |
| 200 | if (!IS_ERR(clk) || PTR_ERR(clk) == -EPROBE_DEFER) |
Jean-Francois Moine | a34cd46 | 2013-11-25 19:47:04 +0100 | [diff] [blame] | 201 | return clk; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 202 | } |
Sascha Hauer | 05fd8e7 | 2009-03-07 12:55:49 +0100 | [diff] [blame] | 203 | |
| 204 | return clk_get_sys(dev_id, con_id); |
| 205 | } |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 206 | EXPORT_SYMBOL(clk_get); |
| 207 | |
| 208 | void clk_put(struct clk *clk) |
| 209 | { |
| 210 | __clk_put(clk); |
| 211 | } |
| 212 | EXPORT_SYMBOL(clk_put); |
| 213 | |
Russell King | d5622a9 | 2015-03-02 15:45:41 +0000 | [diff] [blame] | 214 | static void __clkdev_add(struct clk_lookup *cl) |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 215 | { |
| 216 | mutex_lock(&clocks_mutex); |
| 217 | list_add_tail(&cl->node, &clocks); |
| 218 | mutex_unlock(&clocks_mutex); |
| 219 | } |
Russell King | d5622a9 | 2015-03-02 15:45:41 +0000 | [diff] [blame] | 220 | |
| 221 | void clkdev_add(struct clk_lookup *cl) |
| 222 | { |
| 223 | if (!cl->clk_hw) |
| 224 | cl->clk_hw = __clk_get_hw(cl->clk); |
| 225 | __clkdev_add(cl); |
| 226 | } |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 227 | EXPORT_SYMBOL(clkdev_add); |
| 228 | |
Russell King | fba3acd | 2015-03-10 14:34:00 +0000 | [diff] [blame] | 229 | void clkdev_add_table(struct clk_lookup *cl, size_t num) |
Russell King | 0a0300d | 2010-01-12 12:28:00 +0000 | [diff] [blame] | 230 | { |
| 231 | mutex_lock(&clocks_mutex); |
| 232 | while (num--) { |
Russell King | d5622a9 | 2015-03-02 15:45:41 +0000 | [diff] [blame] | 233 | cl->clk_hw = __clk_get_hw(cl->clk); |
Russell King | 0a0300d | 2010-01-12 12:28:00 +0000 | [diff] [blame] | 234 | list_add_tail(&cl->node, &clocks); |
| 235 | cl++; |
| 236 | } |
| 237 | mutex_unlock(&clocks_mutex); |
| 238 | } |
| 239 | |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 240 | #define MAX_DEV_ID 20 |
| 241 | #define MAX_CON_ID 16 |
| 242 | |
| 243 | struct clk_lookup_alloc { |
| 244 | struct clk_lookup cl; |
| 245 | char dev_id[MAX_DEV_ID]; |
| 246 | char con_id[MAX_CON_ID]; |
| 247 | }; |
| 248 | |
Fabian Frederick | bd721ea | 2016-08-02 14:03:33 -0700 | [diff] [blame] | 249 | static struct clk_lookup * __ref |
Russell King | d5622a9 | 2015-03-02 15:45:41 +0000 | [diff] [blame] | 250 | vclkdev_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt, |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 251 | va_list ap) |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 252 | { |
| 253 | struct clk_lookup_alloc *cla; |
| 254 | |
Jean-Christop PLAGNIOL-VILLARD | 6d803ba | 2010-11-17 10:04:33 +0100 | [diff] [blame] | 255 | cla = __clkdev_alloc(sizeof(*cla)); |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 256 | if (!cla) |
| 257 | return NULL; |
| 258 | |
Russell King | d5622a9 | 2015-03-02 15:45:41 +0000 | [diff] [blame] | 259 | cla->cl.clk_hw = hw; |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 260 | if (con_id) { |
| 261 | strlcpy(cla->con_id, con_id, sizeof(cla->con_id)); |
| 262 | cla->cl.con_id = cla->con_id; |
| 263 | } |
| 264 | |
| 265 | if (dev_fmt) { |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 266 | vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap); |
| 267 | cla->cl.dev_id = cla->dev_id; |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | return &cla->cl; |
| 271 | } |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 272 | |
Russell King | 2568999 | 2015-03-02 15:40:29 +0000 | [diff] [blame] | 273 | static struct clk_lookup * |
| 274 | vclkdev_create(struct clk_hw *hw, const char *con_id, const char *dev_fmt, |
| 275 | va_list ap) |
| 276 | { |
| 277 | struct clk_lookup *cl; |
| 278 | |
| 279 | cl = vclkdev_alloc(hw, con_id, dev_fmt, ap); |
| 280 | if (cl) |
| 281 | __clkdev_add(cl); |
| 282 | |
| 283 | return cl; |
| 284 | } |
| 285 | |
Fabian Frederick | bd721ea | 2016-08-02 14:03:33 -0700 | [diff] [blame] | 286 | struct clk_lookup * __ref |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 287 | clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...) |
| 288 | { |
| 289 | struct clk_lookup *cl; |
| 290 | va_list ap; |
| 291 | |
| 292 | va_start(ap, dev_fmt); |
Russell King | d5622a9 | 2015-03-02 15:45:41 +0000 | [diff] [blame] | 293 | cl = vclkdev_alloc(__clk_get_hw(clk), con_id, dev_fmt, ap); |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 294 | va_end(ap); |
| 295 | |
| 296 | return cl; |
| 297 | } |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 298 | EXPORT_SYMBOL(clkdev_alloc); |
| 299 | |
Stephen Boyd | e4f1b49 | 2016-02-08 14:59:49 -0800 | [diff] [blame] | 300 | struct clk_lookup * |
| 301 | clkdev_hw_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt, ...) |
| 302 | { |
| 303 | struct clk_lookup *cl; |
| 304 | va_list ap; |
| 305 | |
| 306 | va_start(ap, dev_fmt); |
| 307 | cl = vclkdev_alloc(hw, con_id, dev_fmt, ap); |
| 308 | va_end(ap); |
| 309 | |
| 310 | return cl; |
| 311 | } |
| 312 | EXPORT_SYMBOL(clkdev_hw_alloc); |
| 313 | |
Russell King | 2568999 | 2015-03-02 15:40:29 +0000 | [diff] [blame] | 314 | /** |
| 315 | * clkdev_create - allocate and add a clkdev lookup structure |
| 316 | * @clk: struct clk to associate with all clk_lookups |
| 317 | * @con_id: connection ID string on device |
| 318 | * @dev_fmt: format string describing device name |
| 319 | * |
| 320 | * Returns a clk_lookup structure, which can be later unregistered and |
| 321 | * freed. |
| 322 | */ |
| 323 | struct clk_lookup *clkdev_create(struct clk *clk, const char *con_id, |
| 324 | const char *dev_fmt, ...) |
| 325 | { |
| 326 | struct clk_lookup *cl; |
| 327 | va_list ap; |
| 328 | |
| 329 | va_start(ap, dev_fmt); |
| 330 | cl = vclkdev_create(__clk_get_hw(clk), con_id, dev_fmt, ap); |
| 331 | va_end(ap); |
| 332 | |
| 333 | return cl; |
| 334 | } |
| 335 | EXPORT_SYMBOL_GPL(clkdev_create); |
| 336 | |
Stephen Boyd | e4f1b49 | 2016-02-08 14:59:49 -0800 | [diff] [blame] | 337 | /** |
| 338 | * clkdev_hw_create - allocate and add a clkdev lookup structure |
| 339 | * @hw: struct clk_hw to associate with all clk_lookups |
| 340 | * @con_id: connection ID string on device |
| 341 | * @dev_fmt: format string describing device name |
| 342 | * |
| 343 | * Returns a clk_lookup structure, which can be later unregistered and |
| 344 | * freed. |
| 345 | */ |
| 346 | struct clk_lookup *clkdev_hw_create(struct clk_hw *hw, const char *con_id, |
| 347 | const char *dev_fmt, ...) |
| 348 | { |
| 349 | struct clk_lookup *cl; |
| 350 | va_list ap; |
| 351 | |
| 352 | va_start(ap, dev_fmt); |
| 353 | cl = vclkdev_create(hw, con_id, dev_fmt, ap); |
| 354 | va_end(ap); |
| 355 | |
| 356 | return cl; |
| 357 | } |
| 358 | EXPORT_SYMBOL_GPL(clkdev_hw_create); |
| 359 | |
Russell King | b3d8d7e | 2015-03-09 10:43:04 +0000 | [diff] [blame] | 360 | int clk_add_alias(const char *alias, const char *alias_dev_name, |
| 361 | const char *con_id, struct device *dev) |
Tony Lindgren | c068303 | 2009-06-03 17:43:14 +0100 | [diff] [blame] | 362 | { |
Russell King | b3d8d7e | 2015-03-09 10:43:04 +0000 | [diff] [blame] | 363 | struct clk *r = clk_get(dev, con_id); |
Tony Lindgren | c068303 | 2009-06-03 17:43:14 +0100 | [diff] [blame] | 364 | struct clk_lookup *l; |
| 365 | |
| 366 | if (IS_ERR(r)) |
| 367 | return PTR_ERR(r); |
| 368 | |
Russell King | 625faa6 | 2015-10-20 11:49:44 +0100 | [diff] [blame] | 369 | l = clkdev_create(r, alias, alias_dev_name ? "%s" : NULL, |
| 370 | alias_dev_name); |
Tony Lindgren | c068303 | 2009-06-03 17:43:14 +0100 | [diff] [blame] | 371 | clk_put(r); |
Russell King | 2568999 | 2015-03-02 15:40:29 +0000 | [diff] [blame] | 372 | |
| 373 | return l ? 0 : -ENODEV; |
Tony Lindgren | c068303 | 2009-06-03 17:43:14 +0100 | [diff] [blame] | 374 | } |
| 375 | EXPORT_SYMBOL(clk_add_alias); |
| 376 | |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 377 | /* |
| 378 | * clkdev_drop - remove a clock dynamically allocated |
| 379 | */ |
| 380 | void clkdev_drop(struct clk_lookup *cl) |
| 381 | { |
| 382 | mutex_lock(&clocks_mutex); |
| 383 | list_del(&cl->node); |
| 384 | mutex_unlock(&clocks_mutex); |
| 385 | kfree(cl); |
| 386 | } |
| 387 | EXPORT_SYMBOL(clkdev_drop); |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 388 | |
Kees Cook | 416dd13 | 2016-01-26 01:21:26 +0100 | [diff] [blame] | 389 | static struct clk_lookup *__clk_register_clkdev(struct clk_hw *hw, |
| 390 | const char *con_id, |
| 391 | const char *dev_id, ...) |
| 392 | { |
| 393 | struct clk_lookup *cl; |
| 394 | va_list ap; |
| 395 | |
| 396 | va_start(ap, dev_id); |
| 397 | cl = vclkdev_create(hw, con_id, dev_id, ap); |
| 398 | va_end(ap); |
| 399 | |
| 400 | return cl; |
| 401 | } |
| 402 | |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 403 | /** |
| 404 | * clk_register_clkdev - register one clock lookup for a struct clk |
| 405 | * @clk: struct clk to associate with all clk_lookups |
| 406 | * @con_id: connection ID string on device |
Kees Cook | 416dd13 | 2016-01-26 01:21:26 +0100 | [diff] [blame] | 407 | * @dev_id: string describing device name |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 408 | * |
| 409 | * con_id or dev_id may be NULL as a wildcard, just as in the rest of |
| 410 | * clkdev. |
| 411 | * |
| 412 | * To make things easier for mass registration, we detect error clks |
| 413 | * from a previous clk_register() call, and return the error code for |
| 414 | * those. This is to permit this function to be called immediately |
| 415 | * after clk_register(). |
| 416 | */ |
| 417 | int clk_register_clkdev(struct clk *clk, const char *con_id, |
Kees Cook | 416dd13 | 2016-01-26 01:21:26 +0100 | [diff] [blame] | 418 | const char *dev_id) |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 419 | { |
| 420 | struct clk_lookup *cl; |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 421 | |
| 422 | if (IS_ERR(clk)) |
| 423 | return PTR_ERR(clk); |
| 424 | |
Kees Cook | 416dd13 | 2016-01-26 01:21:26 +0100 | [diff] [blame] | 425 | /* |
| 426 | * Since dev_id can be NULL, and NULL is handled specially, we must |
| 427 | * pass it as either a NULL format string, or with "%s". |
| 428 | */ |
| 429 | if (dev_id) |
| 430 | cl = __clk_register_clkdev(__clk_get_hw(clk), con_id, "%s", |
| 431 | dev_id); |
| 432 | else |
| 433 | cl = __clk_register_clkdev(__clk_get_hw(clk), con_id, NULL); |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 434 | |
Russell King | 2568999 | 2015-03-02 15:40:29 +0000 | [diff] [blame] | 435 | return cl ? 0 : -ENOMEM; |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 436 | } |
Tomeu Vizoso | a251361a | 2015-01-23 12:03:32 +0100 | [diff] [blame] | 437 | EXPORT_SYMBOL(clk_register_clkdev); |
Stephen Boyd | e4f1b49 | 2016-02-08 14:59:49 -0800 | [diff] [blame] | 438 | |
| 439 | /** |
| 440 | * clk_hw_register_clkdev - register one clock lookup for a struct clk_hw |
| 441 | * @hw: struct clk_hw to associate with all clk_lookups |
| 442 | * @con_id: connection ID string on device |
| 443 | * @dev_id: format string describing device name |
| 444 | * |
| 445 | * con_id or dev_id may be NULL as a wildcard, just as in the rest of |
| 446 | * clkdev. |
Geert Uytterhoeven | b8ba5fa | 2016-11-22 12:33:11 +0100 | [diff] [blame] | 447 | * |
| 448 | * To make things easier for mass registration, we detect error clk_hws |
| 449 | * from a previous clk_hw_register_*() call, and return the error code for |
| 450 | * those. This is to permit this function to be called immediately |
| 451 | * after clk_hw_register_*(). |
Stephen Boyd | e4f1b49 | 2016-02-08 14:59:49 -0800 | [diff] [blame] | 452 | */ |
| 453 | int clk_hw_register_clkdev(struct clk_hw *hw, const char *con_id, |
| 454 | const char *dev_id) |
| 455 | { |
| 456 | struct clk_lookup *cl; |
| 457 | |
Geert Uytterhoeven | b8ba5fa | 2016-11-22 12:33:11 +0100 | [diff] [blame] | 458 | if (IS_ERR(hw)) |
| 459 | return PTR_ERR(hw); |
| 460 | |
Stephen Boyd | e4f1b49 | 2016-02-08 14:59:49 -0800 | [diff] [blame] | 461 | /* |
| 462 | * Since dev_id can be NULL, and NULL is handled specially, we must |
| 463 | * pass it as either a NULL format string, or with "%s". |
| 464 | */ |
| 465 | if (dev_id) |
| 466 | cl = __clk_register_clkdev(hw, con_id, "%s", dev_id); |
| 467 | else |
| 468 | cl = __clk_register_clkdev(hw, con_id, NULL); |
| 469 | |
| 470 | return cl ? 0 : -ENOMEM; |
| 471 | } |
| 472 | EXPORT_SYMBOL(clk_hw_register_clkdev); |