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 | |
Rob Herring | 137f8a7 | 2012-07-18 11:52:23 +0800 | [diff] [blame] | 30 | #if defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) |
Stephen Boyd | 4472287 | 2018-12-19 10:59:55 -0800 | [diff] [blame^] | 31 | static struct clk_hw *of_clk_get_hw(struct device_node *np, |
| 32 | int index, const char *con_id) |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 33 | { |
Stephen Boyd | 4472287 | 2018-12-19 10:59:55 -0800 | [diff] [blame^] | 34 | int ret; |
| 35 | struct clk_hw *hw; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 36 | struct of_phandle_args clkspec; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 37 | |
Stephen Boyd | 4472287 | 2018-12-19 10:59:55 -0800 | [diff] [blame^] | 38 | ret = of_parse_clkspec(np, index, con_id, &clkspec); |
| 39 | if (ret) |
| 40 | return ERR_PTR(ret); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 41 | |
Stephen Boyd | 4472287 | 2018-12-19 10:59:55 -0800 | [diff] [blame^] | 42 | hw = of_clk_get_hw_from_clkspec(&clkspec); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 43 | of_node_put(clkspec.np); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 44 | |
Stephen Boyd | 4472287 | 2018-12-19 10:59:55 -0800 | [diff] [blame^] | 45 | return hw; |
| 46 | } |
| 47 | |
| 48 | static 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 | |
| 54 | return clk_hw_create_clk(hw, dev_id, con_id); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | struct clk *of_clk_get(struct device_node *np, int index) |
| 58 | { |
Stephen Boyd | 73e0e49 | 2015-02-06 11:42:43 -0800 | [diff] [blame] | 59 | return __of_clk_get(np, index, np->full_name, NULL); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 60 | } |
| 61 | EXPORT_SYMBOL(of_clk_get); |
| 62 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 63 | /** |
| 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 | */ |
| 72 | struct clk *of_clk_get_by_name(struct device_node *np, const char *name) |
| 73 | { |
| 74 | if (!np) |
| 75 | return ERR_PTR(-ENOENT); |
| 76 | |
Stephen Boyd | 4472287 | 2018-12-19 10:59:55 -0800 | [diff] [blame^] | 77 | return __of_clk_get(np, -1, np->full_name, name); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 78 | } |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 79 | EXPORT_SYMBOL(of_clk_get_by_name); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 80 | |
| 81 | #else /* defined(CONFIG_OF) && defined(CONFIG_COMMON_CLK) */ |
| 82 | |
Stephen Boyd | 4472287 | 2018-12-19 10:59:55 -0800 | [diff] [blame^] | 83 | static struct clk_hw *of_clk_get_hw(struct device_node *np, |
| 84 | int index, const char *con_id) |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 85 | { |
| 86 | return ERR_PTR(-ENOENT); |
| 87 | } |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 88 | #endif |
| 89 | |
Russell King | 409dc36 | 2009-01-24 10:14:37 +0000 | [diff] [blame] | 90 | /* |
| 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önig | 659431f | 2010-01-18 16:02:48 +0100 | [diff] [blame] | 97 | * order of precedence: dev+con > dev only > con only. |
Russell King | 409dc36 | 2009-01-24 10:14:37 +0000 | [diff] [blame] | 98 | */ |
Russell King | e8bf8df | 2011-04-30 10:14:08 +0100 | [diff] [blame] | 99 | 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] | 100 | { |
Russell King | e8bf8df | 2011-04-30 10:14:08 +0100 | [diff] [blame] | 101 | struct clk_lookup *p, *cl = NULL; |
viresh kumar | 67b5087 | 2012-04-19 04:23:25 +0100 | [diff] [blame] | 102 | 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 King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 108 | |
| 109 | list_for_each_entry(p, &clocks, node) { |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 110 | match = 0; |
Russell King | 409dc36 | 2009-01-24 10:14:37 +0000 | [diff] [blame] | 111 | 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 King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 121 | |
viresh kumar | 67b5087 | 2012-04-19 04:23:25 +0100 | [diff] [blame] | 122 | if (match > best_found) { |
Russell King | e8bf8df | 2011-04-30 10:14:08 +0100 | [diff] [blame] | 123 | cl = p; |
viresh kumar | 67b5087 | 2012-04-19 04:23:25 +0100 | [diff] [blame] | 124 | if (match != best_possible) |
| 125 | best_found = match; |
viresh kumar | e4bf5be | 2010-03-09 11:54:30 +0100 | [diff] [blame] | 126 | else |
| 127 | break; |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 128 | } |
| 129 | } |
Russell King | e8bf8df | 2011-04-30 10:14:08 +0100 | [diff] [blame] | 130 | return cl; |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Sascha Hauer | 05fd8e73 | 2009-03-07 12:55:49 +0100 | [diff] [blame] | 133 | 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] | 134 | { |
Russell King | e8bf8df | 2011-04-30 10:14:08 +0100 | [diff] [blame] | 135 | struct clk_lookup *cl; |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 136 | struct clk *clk = NULL; |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 137 | |
| 138 | mutex_lock(&clocks_mutex); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 139 | |
Russell King | e8bf8df | 2011-04-30 10:14:08 +0100 | [diff] [blame] | 140 | cl = clk_find(dev_id, con_id); |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 141 | if (!cl) |
| 142 | goto out; |
| 143 | |
Stephen Boyd | 1df4046 | 2018-12-11 08:32:04 -0800 | [diff] [blame] | 144 | clk = clk_hw_create_clk(cl->clk_hw, dev_id, con_id); |
Stephen Boyd | 73e0e49 | 2015-02-06 11:42:43 -0800 | [diff] [blame] | 145 | if (IS_ERR(clk)) |
Russell King | e8bf8df | 2011-04-30 10:14:08 +0100 | [diff] [blame] | 146 | cl = NULL; |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 147 | out: |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 148 | mutex_unlock(&clocks_mutex); |
| 149 | |
Tomeu Vizoso | 035a61c | 2015-01-23 12:03:30 +0100 | [diff] [blame] | 150 | return cl ? clk : ERR_PTR(-ENOENT); |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 151 | } |
Sascha Hauer | 05fd8e73 | 2009-03-07 12:55:49 +0100 | [diff] [blame] | 152 | EXPORT_SYMBOL(clk_get_sys); |
| 153 | |
| 154 | struct clk *clk_get(struct device *dev, const char *con_id) |
| 155 | { |
| 156 | const char *dev_id = dev ? dev_name(dev) : NULL; |
Stephen Boyd | 4472287 | 2018-12-19 10:59:55 -0800 | [diff] [blame^] | 157 | struct clk_hw *hw; |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 158 | |
Bartosz Golaszewski | 53ccb22 | 2018-06-28 15:42:20 +0100 | [diff] [blame] | 159 | if (dev && dev->of_node) { |
Stephen Boyd | 4472287 | 2018-12-19 10:59:55 -0800 | [diff] [blame^] | 160 | hw = of_clk_get_hw(dev->of_node, 0, con_id); |
| 161 | if (!IS_ERR(hw) || PTR_ERR(hw) == -EPROBE_DEFER) |
| 162 | return clk_hw_create_clk(hw, dev_id, con_id); |
Grant Likely | 766e6a4 | 2012-04-09 14:50:06 -0500 | [diff] [blame] | 163 | } |
Sascha Hauer | 05fd8e73 | 2009-03-07 12:55:49 +0100 | [diff] [blame] | 164 | |
| 165 | return clk_get_sys(dev_id, con_id); |
| 166 | } |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 167 | EXPORT_SYMBOL(clk_get); |
| 168 | |
| 169 | void clk_put(struct clk *clk) |
| 170 | { |
| 171 | __clk_put(clk); |
| 172 | } |
| 173 | EXPORT_SYMBOL(clk_put); |
| 174 | |
Russell King | d5622a9 | 2015-03-02 15:45:41 +0000 | [diff] [blame] | 175 | static void __clkdev_add(struct clk_lookup *cl) |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 176 | { |
| 177 | mutex_lock(&clocks_mutex); |
| 178 | list_add_tail(&cl->node, &clocks); |
| 179 | mutex_unlock(&clocks_mutex); |
| 180 | } |
Russell King | d5622a9 | 2015-03-02 15:45:41 +0000 | [diff] [blame] | 181 | |
| 182 | void clkdev_add(struct clk_lookup *cl) |
| 183 | { |
| 184 | if (!cl->clk_hw) |
| 185 | cl->clk_hw = __clk_get_hw(cl->clk); |
| 186 | __clkdev_add(cl); |
| 187 | } |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 188 | EXPORT_SYMBOL(clkdev_add); |
| 189 | |
Russell King | fba3acd | 2015-03-10 14:34:00 +0000 | [diff] [blame] | 190 | void clkdev_add_table(struct clk_lookup *cl, size_t num) |
Russell King | 0a0300d | 2010-01-12 12:28:00 +0000 | [diff] [blame] | 191 | { |
| 192 | mutex_lock(&clocks_mutex); |
| 193 | while (num--) { |
Russell King | d5622a9 | 2015-03-02 15:45:41 +0000 | [diff] [blame] | 194 | cl->clk_hw = __clk_get_hw(cl->clk); |
Russell King | 0a0300d | 2010-01-12 12:28:00 +0000 | [diff] [blame] | 195 | list_add_tail(&cl->node, &clocks); |
| 196 | cl++; |
| 197 | } |
| 198 | mutex_unlock(&clocks_mutex); |
| 199 | } |
| 200 | |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 201 | #define MAX_DEV_ID 20 |
| 202 | #define MAX_CON_ID 16 |
| 203 | |
| 204 | struct clk_lookup_alloc { |
| 205 | struct clk_lookup cl; |
| 206 | char dev_id[MAX_DEV_ID]; |
| 207 | char con_id[MAX_CON_ID]; |
| 208 | }; |
| 209 | |
Fabian Frederick | bd721ea | 2016-08-02 14:03:33 -0700 | [diff] [blame] | 210 | static struct clk_lookup * __ref |
Russell King | d5622a9 | 2015-03-02 15:45:41 +0000 | [diff] [blame] | 211 | 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] | 212 | va_list ap) |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 213 | { |
| 214 | struct clk_lookup_alloc *cla; |
| 215 | |
Stephen Boyd | 0d4e3d00 | 2018-01-02 15:47:07 -0800 | [diff] [blame] | 216 | cla = kzalloc(sizeof(*cla), GFP_KERNEL); |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 217 | if (!cla) |
| 218 | return NULL; |
| 219 | |
Russell King | d5622a9 | 2015-03-02 15:45:41 +0000 | [diff] [blame] | 220 | cla->cl.clk_hw = hw; |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 221 | if (con_id) { |
| 222 | strlcpy(cla->con_id, con_id, sizeof(cla->con_id)); |
| 223 | cla->cl.con_id = cla->con_id; |
| 224 | } |
| 225 | |
| 226 | if (dev_fmt) { |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 227 | vscnprintf(cla->dev_id, sizeof(cla->dev_id), dev_fmt, ap); |
| 228 | cla->cl.dev_id = cla->dev_id; |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return &cla->cl; |
| 232 | } |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 233 | |
Russell King | 2568999 | 2015-03-02 15:40:29 +0000 | [diff] [blame] | 234 | static struct clk_lookup * |
| 235 | vclkdev_create(struct clk_hw *hw, const char *con_id, const char *dev_fmt, |
| 236 | va_list ap) |
| 237 | { |
| 238 | struct clk_lookup *cl; |
| 239 | |
| 240 | cl = vclkdev_alloc(hw, con_id, dev_fmt, ap); |
| 241 | if (cl) |
| 242 | __clkdev_add(cl); |
| 243 | |
| 244 | return cl; |
| 245 | } |
| 246 | |
Fabian Frederick | bd721ea | 2016-08-02 14:03:33 -0700 | [diff] [blame] | 247 | struct clk_lookup * __ref |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 248 | clkdev_alloc(struct clk *clk, const char *con_id, const char *dev_fmt, ...) |
| 249 | { |
| 250 | struct clk_lookup *cl; |
| 251 | va_list ap; |
| 252 | |
| 253 | va_start(ap, dev_fmt); |
Russell King | d5622a9 | 2015-03-02 15:45:41 +0000 | [diff] [blame] | 254 | cl = vclkdev_alloc(__clk_get_hw(clk), con_id, dev_fmt, ap); |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 255 | va_end(ap); |
| 256 | |
| 257 | return cl; |
| 258 | } |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 259 | EXPORT_SYMBOL(clkdev_alloc); |
| 260 | |
Stephen Boyd | e4f1b49 | 2016-02-08 14:59:49 -0800 | [diff] [blame] | 261 | struct clk_lookup * |
| 262 | clkdev_hw_alloc(struct clk_hw *hw, const char *con_id, const char *dev_fmt, ...) |
| 263 | { |
| 264 | struct clk_lookup *cl; |
| 265 | va_list ap; |
| 266 | |
| 267 | va_start(ap, dev_fmt); |
| 268 | cl = vclkdev_alloc(hw, con_id, dev_fmt, ap); |
| 269 | va_end(ap); |
| 270 | |
| 271 | return cl; |
| 272 | } |
| 273 | EXPORT_SYMBOL(clkdev_hw_alloc); |
| 274 | |
Russell King | 2568999 | 2015-03-02 15:40:29 +0000 | [diff] [blame] | 275 | /** |
| 276 | * clkdev_create - allocate and add a clkdev lookup structure |
| 277 | * @clk: struct clk to associate with all clk_lookups |
| 278 | * @con_id: connection ID string on device |
| 279 | * @dev_fmt: format string describing device name |
| 280 | * |
| 281 | * Returns a clk_lookup structure, which can be later unregistered and |
| 282 | * freed. |
| 283 | */ |
| 284 | struct clk_lookup *clkdev_create(struct clk *clk, const char *con_id, |
| 285 | const char *dev_fmt, ...) |
| 286 | { |
| 287 | struct clk_lookup *cl; |
| 288 | va_list ap; |
| 289 | |
| 290 | va_start(ap, dev_fmt); |
| 291 | cl = vclkdev_create(__clk_get_hw(clk), con_id, dev_fmt, ap); |
| 292 | va_end(ap); |
| 293 | |
| 294 | return cl; |
| 295 | } |
| 296 | EXPORT_SYMBOL_GPL(clkdev_create); |
| 297 | |
Stephen Boyd | e4f1b49 | 2016-02-08 14:59:49 -0800 | [diff] [blame] | 298 | /** |
| 299 | * clkdev_hw_create - allocate and add a clkdev lookup structure |
| 300 | * @hw: struct clk_hw to associate with all clk_lookups |
| 301 | * @con_id: connection ID string on device |
| 302 | * @dev_fmt: format string describing device name |
| 303 | * |
| 304 | * Returns a clk_lookup structure, which can be later unregistered and |
| 305 | * freed. |
| 306 | */ |
| 307 | struct clk_lookup *clkdev_hw_create(struct clk_hw *hw, const char *con_id, |
| 308 | const char *dev_fmt, ...) |
| 309 | { |
| 310 | struct clk_lookup *cl; |
| 311 | va_list ap; |
| 312 | |
| 313 | va_start(ap, dev_fmt); |
| 314 | cl = vclkdev_create(hw, con_id, dev_fmt, ap); |
| 315 | va_end(ap); |
| 316 | |
| 317 | return cl; |
| 318 | } |
| 319 | EXPORT_SYMBOL_GPL(clkdev_hw_create); |
| 320 | |
Russell King | b3d8d7e | 2015-03-09 10:43:04 +0000 | [diff] [blame] | 321 | int clk_add_alias(const char *alias, const char *alias_dev_name, |
| 322 | const char *con_id, struct device *dev) |
Tony Lindgren | c068303 | 2009-06-03 17:43:14 +0100 | [diff] [blame] | 323 | { |
Russell King | b3d8d7e | 2015-03-09 10:43:04 +0000 | [diff] [blame] | 324 | struct clk *r = clk_get(dev, con_id); |
Tony Lindgren | c068303 | 2009-06-03 17:43:14 +0100 | [diff] [blame] | 325 | struct clk_lookup *l; |
| 326 | |
| 327 | if (IS_ERR(r)) |
| 328 | return PTR_ERR(r); |
| 329 | |
Russell King | 625faa6 | 2015-10-20 11:49:44 +0100 | [diff] [blame] | 330 | l = clkdev_create(r, alias, alias_dev_name ? "%s" : NULL, |
| 331 | alias_dev_name); |
Tony Lindgren | c068303 | 2009-06-03 17:43:14 +0100 | [diff] [blame] | 332 | clk_put(r); |
Russell King | 2568999 | 2015-03-02 15:40:29 +0000 | [diff] [blame] | 333 | |
| 334 | return l ? 0 : -ENODEV; |
Tony Lindgren | c068303 | 2009-06-03 17:43:14 +0100 | [diff] [blame] | 335 | } |
| 336 | EXPORT_SYMBOL(clk_add_alias); |
| 337 | |
Russell King | 0318e69 | 2008-11-09 16:32:46 +0000 | [diff] [blame] | 338 | /* |
| 339 | * clkdev_drop - remove a clock dynamically allocated |
| 340 | */ |
| 341 | void clkdev_drop(struct clk_lookup *cl) |
| 342 | { |
| 343 | mutex_lock(&clocks_mutex); |
| 344 | list_del(&cl->node); |
| 345 | mutex_unlock(&clocks_mutex); |
| 346 | kfree(cl); |
| 347 | } |
| 348 | EXPORT_SYMBOL(clkdev_drop); |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 349 | |
Kees Cook | 416dd13 | 2016-01-26 01:21:26 +0100 | [diff] [blame] | 350 | static struct clk_lookup *__clk_register_clkdev(struct clk_hw *hw, |
| 351 | const char *con_id, |
| 352 | const char *dev_id, ...) |
| 353 | { |
| 354 | struct clk_lookup *cl; |
| 355 | va_list ap; |
| 356 | |
| 357 | va_start(ap, dev_id); |
| 358 | cl = vclkdev_create(hw, con_id, dev_id, ap); |
| 359 | va_end(ap); |
| 360 | |
| 361 | return cl; |
| 362 | } |
| 363 | |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 364 | /** |
| 365 | * clk_register_clkdev - register one clock lookup for a struct clk |
| 366 | * @clk: struct clk to associate with all clk_lookups |
| 367 | * @con_id: connection ID string on device |
Kees Cook | 416dd13 | 2016-01-26 01:21:26 +0100 | [diff] [blame] | 368 | * @dev_id: string describing device name |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 369 | * |
| 370 | * con_id or dev_id may be NULL as a wildcard, just as in the rest of |
| 371 | * clkdev. |
| 372 | * |
| 373 | * To make things easier for mass registration, we detect error clks |
| 374 | * from a previous clk_register() call, and return the error code for |
| 375 | * those. This is to permit this function to be called immediately |
| 376 | * after clk_register(). |
| 377 | */ |
| 378 | int clk_register_clkdev(struct clk *clk, const char *con_id, |
Kees Cook | 416dd13 | 2016-01-26 01:21:26 +0100 | [diff] [blame] | 379 | const char *dev_id) |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 380 | { |
| 381 | struct clk_lookup *cl; |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 382 | |
| 383 | if (IS_ERR(clk)) |
| 384 | return PTR_ERR(clk); |
| 385 | |
Kees Cook | 416dd13 | 2016-01-26 01:21:26 +0100 | [diff] [blame] | 386 | /* |
| 387 | * Since dev_id can be NULL, and NULL is handled specially, we must |
| 388 | * pass it as either a NULL format string, or with "%s". |
| 389 | */ |
| 390 | if (dev_id) |
| 391 | cl = __clk_register_clkdev(__clk_get_hw(clk), con_id, "%s", |
| 392 | dev_id); |
| 393 | else |
| 394 | cl = __clk_register_clkdev(__clk_get_hw(clk), con_id, NULL); |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 395 | |
Russell King | 2568999 | 2015-03-02 15:40:29 +0000 | [diff] [blame] | 396 | return cl ? 0 : -ENOMEM; |
Russell King | e9d7f40 | 2012-05-02 09:30:32 +0100 | [diff] [blame] | 397 | } |
Tomeu Vizoso | a251361a | 2015-01-23 12:03:32 +0100 | [diff] [blame] | 398 | EXPORT_SYMBOL(clk_register_clkdev); |
Stephen Boyd | e4f1b49 | 2016-02-08 14:59:49 -0800 | [diff] [blame] | 399 | |
| 400 | /** |
| 401 | * clk_hw_register_clkdev - register one clock lookup for a struct clk_hw |
| 402 | * @hw: struct clk_hw to associate with all clk_lookups |
| 403 | * @con_id: connection ID string on device |
| 404 | * @dev_id: format string describing device name |
| 405 | * |
| 406 | * con_id or dev_id may be NULL as a wildcard, just as in the rest of |
| 407 | * clkdev. |
Geert Uytterhoeven | 9388093 | 2016-11-22 12:33:11 +0100 | [diff] [blame] | 408 | * |
| 409 | * To make things easier for mass registration, we detect error clk_hws |
| 410 | * from a previous clk_hw_register_*() call, and return the error code for |
| 411 | * those. This is to permit this function to be called immediately |
| 412 | * after clk_hw_register_*(). |
Stephen Boyd | e4f1b49 | 2016-02-08 14:59:49 -0800 | [diff] [blame] | 413 | */ |
| 414 | int clk_hw_register_clkdev(struct clk_hw *hw, const char *con_id, |
| 415 | const char *dev_id) |
| 416 | { |
| 417 | struct clk_lookup *cl; |
| 418 | |
Geert Uytterhoeven | 9388093 | 2016-11-22 12:33:11 +0100 | [diff] [blame] | 419 | if (IS_ERR(hw)) |
| 420 | return PTR_ERR(hw); |
| 421 | |
Stephen Boyd | e4f1b49 | 2016-02-08 14:59:49 -0800 | [diff] [blame] | 422 | /* |
| 423 | * Since dev_id can be NULL, and NULL is handled specially, we must |
| 424 | * pass it as either a NULL format string, or with "%s". |
| 425 | */ |
| 426 | if (dev_id) |
| 427 | cl = __clk_register_clkdev(hw, con_id, "%s", dev_id); |
| 428 | else |
| 429 | cl = __clk_register_clkdev(hw, con_id, NULL); |
| 430 | |
| 431 | return cl ? 0 : -ENOMEM; |
| 432 | } |
| 433 | EXPORT_SYMBOL(clk_hw_register_clkdev); |