Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 36ad234 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 5 | * Copyright (c) 2016, NVIDIA CORPORATION. |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 6 | * Copyright (c) 2018, Theobroma Systems Design und Consulting GmbH |
Simon Glass | 36ad234 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <clk.h> |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 11 | #include <clk-uclass.h> |
Simon Glass | 36ad234 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 12 | #include <dm.h> |
Simon Glass | 589d915 | 2016-07-04 11:58:03 -0600 | [diff] [blame] | 13 | #include <dt-structs.h> |
Simon Glass | 36ad234 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 14 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <malloc.h> |
Claudiu Beznea | c8c1600 | 2020-09-07 17:46:34 +0300 | [diff] [blame] | 17 | #include <dm/device-internal.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 18 | #include <dm/devres.h> |
| 19 | #include <dm/read.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 20 | #include <linux/bug.h> |
Lukasz Majewski | 9e38dc3 | 2019-06-24 15:50:42 +0200 | [diff] [blame] | 21 | #include <linux/clk-provider.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 22 | #include <linux/err.h> |
Simon Glass | 36ad234 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 23 | |
Mario Six | 799fe56 | 2018-01-15 11:06:51 +0100 | [diff] [blame] | 24 | static inline const struct clk_ops *clk_dev_ops(struct udevice *dev) |
Simon Glass | 36ad234 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 25 | { |
Mario Six | 799fe56 | 2018-01-15 11:06:51 +0100 | [diff] [blame] | 26 | return (const struct clk_ops *)dev->driver->ops; |
Simon Glass | 36ad234 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 27 | } |
| 28 | |
Simon Glass | 4303396 | 2020-07-19 10:15:56 -0600 | [diff] [blame] | 29 | struct clk *dev_get_clk_ptr(struct udevice *dev) |
| 30 | { |
| 31 | return (struct clk *)dev_get_uclass_priv(dev); |
| 32 | } |
| 33 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 34 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Simon Glass | 589d915 | 2016-07-04 11:58:03 -0600 | [diff] [blame] | 35 | # if CONFIG_IS_ENABLED(OF_PLATDATA) |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 36 | int clk_get_by_driver_info(struct udevice *dev, struct phandle_1_arg *cells, |
| 37 | struct clk *clk) |
Simon Glass | 589d915 | 2016-07-04 11:58:03 -0600 | [diff] [blame] | 38 | { |
| 39 | int ret; |
| 40 | |
Simon Glass | 5792f4b | 2020-10-03 11:31:40 -0600 | [diff] [blame] | 41 | ret = device_get_by_driver_info_idx(cells->idx, &clk->dev); |
Simon Glass | 589d915 | 2016-07-04 11:58:03 -0600 | [diff] [blame] | 42 | if (ret) |
| 43 | return ret; |
Walter Lozano | dc5b437 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 44 | clk->id = cells->arg[0]; |
Simon Glass | 589d915 | 2016-07-04 11:58:03 -0600 | [diff] [blame] | 45 | |
| 46 | return 0; |
| 47 | } |
| 48 | # else |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 49 | static int clk_of_xlate_default(struct clk *clk, |
Simon Glass | b7ae277 | 2017-05-18 20:09:40 -0600 | [diff] [blame] | 50 | struct ofnode_phandle_args *args) |
Simon Glass | 36ad234 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 51 | { |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 52 | debug("%s(clk=%p)\n", __func__, clk); |
Simon Glass | 36ad234 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 53 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 54 | if (args->args_count > 1) { |
| 55 | debug("Invaild args_count: %d\n", args->args_count); |
| 56 | return -EINVAL; |
| 57 | } |
Simon Glass | 36ad234 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 58 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 59 | if (args->args_count) |
| 60 | clk->id = args->args[0]; |
| 61 | else |
| 62 | clk->id = 0; |
Simon Glass | 36ad234 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 63 | |
Sekhar Nori | 3d23abd | 2019-07-11 14:30:24 +0530 | [diff] [blame] | 64 | clk->data = 0; |
| 65 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 66 | return 0; |
Simon Glass | 36ad234 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 67 | } |
Simon Glass | 0342bd2 | 2016-01-20 19:43:02 -0700 | [diff] [blame] | 68 | |
Jagan Teki | fc7c7ce | 2019-02-28 00:26:52 +0530 | [diff] [blame] | 69 | static int clk_get_by_index_tail(int ret, ofnode node, |
| 70 | struct ofnode_phandle_args *args, |
| 71 | const char *list_name, int index, |
| 72 | struct clk *clk) |
| 73 | { |
| 74 | struct udevice *dev_clk; |
| 75 | const struct clk_ops *ops; |
| 76 | |
| 77 | assert(clk); |
| 78 | clk->dev = NULL; |
| 79 | if (ret) |
| 80 | goto err; |
| 81 | |
| 82 | ret = uclass_get_device_by_ofnode(UCLASS_CLK, args->node, &dev_clk); |
| 83 | if (ret) { |
| 84 | debug("%s: uclass_get_device_by_of_offset failed: err=%d\n", |
| 85 | __func__, ret); |
| 86 | return ret; |
| 87 | } |
| 88 | |
| 89 | clk->dev = dev_clk; |
| 90 | |
| 91 | ops = clk_dev_ops(dev_clk); |
| 92 | |
| 93 | if (ops->of_xlate) |
| 94 | ret = ops->of_xlate(clk, args); |
| 95 | else |
| 96 | ret = clk_of_xlate_default(clk, args); |
| 97 | if (ret) { |
| 98 | debug("of_xlate() failed: %d\n", ret); |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | return clk_request(dev_clk, clk); |
| 103 | err: |
| 104 | debug("%s: Node '%s', property '%s', failed to request CLK index %d: %d\n", |
| 105 | __func__, ofnode_get_name(node), list_name, index, ret); |
| 106 | return ret; |
| 107 | } |
| 108 | |
Philipp Tomsich | f760434 | 2018-01-08 11:18:18 +0100 | [diff] [blame] | 109 | static int clk_get_by_indexed_prop(struct udevice *dev, const char *prop_name, |
| 110 | int index, struct clk *clk) |
Simon Glass | 0342bd2 | 2016-01-20 19:43:02 -0700 | [diff] [blame] | 111 | { |
Simon Glass | 0342bd2 | 2016-01-20 19:43:02 -0700 | [diff] [blame] | 112 | int ret; |
Simon Glass | 2558bff | 2017-05-30 21:47:29 -0600 | [diff] [blame] | 113 | struct ofnode_phandle_args args; |
Simon Glass | 0342bd2 | 2016-01-20 19:43:02 -0700 | [diff] [blame] | 114 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 115 | debug("%s(dev=%p, index=%d, clk=%p)\n", __func__, dev, index, clk); |
| 116 | |
| 117 | assert(clk); |
Patrice Chotard | 96fc03d | 2017-07-18 11:57:07 +0200 | [diff] [blame] | 118 | clk->dev = NULL; |
| 119 | |
Philipp Tomsich | f760434 | 2018-01-08 11:18:18 +0100 | [diff] [blame] | 120 | ret = dev_read_phandle_with_args(dev, prop_name, "#clock-cells", 0, |
Mario Six | 799fe56 | 2018-01-15 11:06:51 +0100 | [diff] [blame] | 121 | index, &args); |
Simon Glass | 0342bd2 | 2016-01-20 19:43:02 -0700 | [diff] [blame] | 122 | if (ret) { |
| 123 | debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n", |
| 124 | __func__, ret); |
| 125 | return ret; |
| 126 | } |
| 127 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 128 | |
Jagan Teki | a77add3 | 2019-02-28 00:26:53 +0530 | [diff] [blame] | 129 | return clk_get_by_index_tail(ret, dev_ofnode(dev), &args, "clocks", |
Sean Anderson | f0d5a6b | 2020-06-24 06:41:08 -0400 | [diff] [blame] | 130 | index, clk); |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 131 | } |
Philipp Tomsich | f760434 | 2018-01-08 11:18:18 +0100 | [diff] [blame] | 132 | |
| 133 | int clk_get_by_index(struct udevice *dev, int index, struct clk *clk) |
| 134 | { |
Jagan Teki | fc7c7ce | 2019-02-28 00:26:52 +0530 | [diff] [blame] | 135 | struct ofnode_phandle_args args; |
| 136 | int ret; |
| 137 | |
| 138 | ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0, |
| 139 | index, &args); |
| 140 | |
| 141 | return clk_get_by_index_tail(ret, dev_ofnode(dev), &args, "clocks", |
Sean Anderson | f0d5a6b | 2020-06-24 06:41:08 -0400 | [diff] [blame] | 142 | index, clk); |
Jagan Teki | fc7c7ce | 2019-02-28 00:26:52 +0530 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | int clk_get_by_index_nodev(ofnode node, int index, struct clk *clk) |
| 146 | { |
| 147 | struct ofnode_phandle_args args; |
| 148 | int ret; |
| 149 | |
| 150 | ret = ofnode_parse_phandle_with_args(node, "clocks", "#clock-cells", 0, |
Sean Anderson | f0d5a6b | 2020-06-24 06:41:08 -0400 | [diff] [blame] | 151 | index, &args); |
Jagan Teki | fc7c7ce | 2019-02-28 00:26:52 +0530 | [diff] [blame] | 152 | |
| 153 | return clk_get_by_index_tail(ret, node, &args, "clocks", |
Sean Anderson | f0d5a6b | 2020-06-24 06:41:08 -0400 | [diff] [blame] | 154 | index, clk); |
Philipp Tomsich | f760434 | 2018-01-08 11:18:18 +0100 | [diff] [blame] | 155 | } |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 156 | |
Neil Armstrong | 8a275a0 | 2018-04-03 11:44:18 +0200 | [diff] [blame] | 157 | int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk) |
| 158 | { |
| 159 | int i, ret, err, count; |
| 160 | |
| 161 | bulk->count = 0; |
| 162 | |
Patrick Delaunay | d776a84 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 163 | count = dev_count_phandle_with_args(dev, "clocks", "#clock-cells", 0); |
Neil Armstrong | 52b26d9 | 2018-04-17 11:30:31 +0200 | [diff] [blame] | 164 | if (count < 1) |
| 165 | return count; |
Neil Armstrong | 8a275a0 | 2018-04-03 11:44:18 +0200 | [diff] [blame] | 166 | |
| 167 | bulk->clks = devm_kcalloc(dev, count, sizeof(struct clk), GFP_KERNEL); |
| 168 | if (!bulk->clks) |
| 169 | return -ENOMEM; |
| 170 | |
| 171 | for (i = 0; i < count; i++) { |
| 172 | ret = clk_get_by_index(dev, i, &bulk->clks[i]); |
| 173 | if (ret < 0) |
| 174 | goto bulk_get_err; |
| 175 | |
| 176 | ++bulk->count; |
| 177 | } |
| 178 | |
| 179 | return 0; |
| 180 | |
| 181 | bulk_get_err: |
| 182 | err = clk_release_all(bulk->clks, bulk->count); |
| 183 | if (err) |
| 184 | debug("%s: could release all clocks for %p\n", |
| 185 | __func__, dev); |
| 186 | |
| 187 | return ret; |
| 188 | } |
| 189 | |
Claudiu Beznea | b91eee6 | 2020-09-07 17:46:36 +0300 | [diff] [blame] | 190 | static struct clk *clk_set_default_get_by_id(struct clk *clk) |
| 191 | { |
| 192 | struct clk *c = clk; |
| 193 | |
| 194 | if (CONFIG_IS_ENABLED(CLK_CCF)) { |
| 195 | int ret = clk_get_by_id(clk->id, &c); |
| 196 | |
| 197 | if (ret) { |
| 198 | debug("%s(): could not get parent clock pointer, id %lu\n", |
| 199 | __func__, clk->id); |
| 200 | ERR_PTR(ret); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | return c; |
| 205 | } |
| 206 | |
Jean-Jacques Hiblot | 9601f32 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 207 | static int clk_set_default_parents(struct udevice *dev, int stage) |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 208 | { |
Claudiu Beznea | b91eee6 | 2020-09-07 17:46:36 +0300 | [diff] [blame] | 209 | struct clk clk, parent_clk, *c, *p; |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 210 | int index; |
| 211 | int num_parents; |
| 212 | int ret; |
| 213 | |
| 214 | num_parents = dev_count_phandle_with_args(dev, "assigned-clock-parents", |
Patrick Delaunay | d776a84 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 215 | "#clock-cells", 0); |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 216 | if (num_parents < 0) { |
| 217 | debug("%s: could not read assigned-clock-parents for %p\n", |
| 218 | __func__, dev); |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | for (index = 0; index < num_parents; index++) { |
| 223 | ret = clk_get_by_indexed_prop(dev, "assigned-clock-parents", |
| 224 | index, &parent_clk); |
Neil Armstrong | f3cc631 | 2018-07-26 15:19:32 +0200 | [diff] [blame] | 225 | /* If -ENOENT, this is a no-op entry */ |
| 226 | if (ret == -ENOENT) |
| 227 | continue; |
| 228 | |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 229 | if (ret) { |
| 230 | debug("%s: could not get parent clock %d for %s\n", |
| 231 | __func__, index, dev_read_name(dev)); |
| 232 | return ret; |
| 233 | } |
| 234 | |
Claudiu Beznea | b91eee6 | 2020-09-07 17:46:36 +0300 | [diff] [blame] | 235 | p = clk_set_default_get_by_id(&parent_clk); |
| 236 | if (IS_ERR(p)) |
| 237 | return PTR_ERR(p); |
| 238 | |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 239 | ret = clk_get_by_indexed_prop(dev, "assigned-clocks", |
| 240 | index, &clk); |
| 241 | if (ret) { |
| 242 | debug("%s: could not get assigned clock %d for %s\n", |
| 243 | __func__, index, dev_read_name(dev)); |
| 244 | return ret; |
| 245 | } |
| 246 | |
Jean-Jacques Hiblot | 9601f32 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 247 | /* This is clk provider device trying to reparent itself |
| 248 | * It cannot be done right now but need to wait after the |
| 249 | * device is probed |
| 250 | */ |
| 251 | if (stage == 0 && clk.dev == dev) |
| 252 | continue; |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 253 | |
Jean-Jacques Hiblot | 9601f32 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 254 | if (stage > 0 && clk.dev != dev) |
| 255 | /* do not setup twice the parent clocks */ |
| 256 | continue; |
| 257 | |
Claudiu Beznea | b91eee6 | 2020-09-07 17:46:36 +0300 | [diff] [blame] | 258 | c = clk_set_default_get_by_id(&clk); |
| 259 | if (IS_ERR(c)) |
| 260 | return PTR_ERR(c); |
| 261 | |
| 262 | ret = clk_set_parent(c, p); |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 263 | /* |
| 264 | * Not all drivers may support clock-reparenting (as of now). |
| 265 | * Ignore errors due to this. |
| 266 | */ |
| 267 | if (ret == -ENOSYS) |
| 268 | continue; |
| 269 | |
Jean-Jacques Hiblot | b232081 | 2019-09-26 15:42:42 +0200 | [diff] [blame] | 270 | if (ret < 0) { |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 271 | debug("%s: failed to reparent clock %d for %s\n", |
| 272 | __func__, index, dev_read_name(dev)); |
| 273 | return ret; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
Jean-Jacques Hiblot | 9601f32 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 280 | static int clk_set_default_rates(struct udevice *dev, int stage) |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 281 | { |
Claudiu Beznea | b91eee6 | 2020-09-07 17:46:36 +0300 | [diff] [blame] | 282 | struct clk clk, *c; |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 283 | int index; |
| 284 | int num_rates; |
| 285 | int size; |
| 286 | int ret = 0; |
| 287 | u32 *rates = NULL; |
| 288 | |
| 289 | size = dev_read_size(dev, "assigned-clock-rates"); |
| 290 | if (size < 0) |
| 291 | return 0; |
| 292 | |
| 293 | num_rates = size / sizeof(u32); |
| 294 | rates = calloc(num_rates, sizeof(u32)); |
| 295 | if (!rates) |
| 296 | return -ENOMEM; |
| 297 | |
| 298 | ret = dev_read_u32_array(dev, "assigned-clock-rates", rates, num_rates); |
| 299 | if (ret) |
| 300 | goto fail; |
| 301 | |
| 302 | for (index = 0; index < num_rates; index++) { |
Neil Armstrong | f3cc631 | 2018-07-26 15:19:32 +0200 | [diff] [blame] | 303 | /* If 0 is passed, this is a no-op */ |
| 304 | if (!rates[index]) |
| 305 | continue; |
| 306 | |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 307 | ret = clk_get_by_indexed_prop(dev, "assigned-clocks", |
| 308 | index, &clk); |
| 309 | if (ret) { |
| 310 | debug("%s: could not get assigned clock %d for %s\n", |
| 311 | __func__, index, dev_read_name(dev)); |
| 312 | continue; |
| 313 | } |
| 314 | |
Jean-Jacques Hiblot | 9601f32 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 315 | /* This is clk provider device trying to program itself |
| 316 | * It cannot be done right now but need to wait after the |
| 317 | * device is probed |
| 318 | */ |
| 319 | if (stage == 0 && clk.dev == dev) |
| 320 | continue; |
| 321 | |
| 322 | if (stage > 0 && clk.dev != dev) |
| 323 | /* do not setup twice the parent clocks */ |
| 324 | continue; |
| 325 | |
Claudiu Beznea | b91eee6 | 2020-09-07 17:46:36 +0300 | [diff] [blame] | 326 | c = clk_set_default_get_by_id(&clk); |
| 327 | if (IS_ERR(c)) |
| 328 | return PTR_ERR(c); |
| 329 | |
| 330 | ret = clk_set_rate(c, rates[index]); |
Jean-Jacques Hiblot | 9601f32 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 331 | |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 332 | if (ret < 0) { |
Simon Glass | 3336373 | 2019-01-21 14:53:19 -0700 | [diff] [blame] | 333 | debug("%s: failed to set rate on clock index %d (%ld) for %s\n", |
| 334 | __func__, index, clk.id, dev_read_name(dev)); |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 335 | break; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | fail: |
| 340 | free(rates); |
| 341 | return ret; |
| 342 | } |
| 343 | |
Jean-Jacques Hiblot | 9601f32 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 344 | int clk_set_defaults(struct udevice *dev, int stage) |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 345 | { |
| 346 | int ret; |
| 347 | |
Peng Fan | 40ec4e4 | 2019-07-31 07:01:49 +0000 | [diff] [blame] | 348 | if (!dev_of_valid(dev)) |
| 349 | return 0; |
| 350 | |
Philipp Tomsich | e546ec8 | 2018-11-26 20:20:19 +0100 | [diff] [blame] | 351 | /* If this not in SPL and pre-reloc state, don't take any action. */ |
| 352 | if (!(IS_ENABLED(CONFIG_SPL_BUILD) || (gd->flags & GD_FLG_RELOC))) |
| 353 | return 0; |
| 354 | |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 355 | debug("%s(%s)\n", __func__, dev_read_name(dev)); |
| 356 | |
Jean-Jacques Hiblot | 9601f32 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 357 | ret = clk_set_default_parents(dev, stage); |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 358 | if (ret) |
| 359 | return ret; |
| 360 | |
Jean-Jacques Hiblot | 9601f32 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 361 | ret = clk_set_default_rates(dev, stage); |
Philipp Tomsich | 9cf03b0 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 362 | if (ret < 0) |
| 363 | return ret; |
| 364 | |
| 365 | return 0; |
| 366 | } |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 367 | |
| 368 | int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk) |
| 369 | { |
| 370 | int index; |
| 371 | |
| 372 | debug("%s(dev=%p, name=%s, clk=%p)\n", __func__, dev, name, clk); |
Patrice Chotard | 96fc03d | 2017-07-18 11:57:07 +0200 | [diff] [blame] | 373 | clk->dev = NULL; |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 374 | |
Simon Glass | 2558bff | 2017-05-30 21:47:29 -0600 | [diff] [blame] | 375 | index = dev_read_stringlist_search(dev, "clock-names", name); |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 376 | if (index < 0) { |
Simon Glass | b0ea740 | 2016-10-02 17:59:28 -0600 | [diff] [blame] | 377 | debug("fdt_stringlist_search() failed: %d\n", index); |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 378 | return index; |
| 379 | } |
| 380 | |
| 381 | return clk_get_by_index(dev, index, clk); |
Simon Glass | 0342bd2 | 2016-01-20 19:43:02 -0700 | [diff] [blame] | 382 | } |
Giulio Benetti | 6c91087 | 2019-12-12 23:53:19 +0100 | [diff] [blame] | 383 | # endif /* OF_PLATDATA */ |
Patrice Chotard | cafc341 | 2017-07-25 13:24:45 +0200 | [diff] [blame] | 384 | |
developer | bdc786d | 2020-01-09 11:35:07 +0800 | [diff] [blame] | 385 | int clk_get_by_name_nodev(ofnode node, const char *name, struct clk *clk) |
| 386 | { |
| 387 | int index; |
| 388 | |
| 389 | debug("%s(node=%p, name=%s, clk=%p)\n", __func__, |
| 390 | ofnode_get_name(node), name, clk); |
| 391 | clk->dev = NULL; |
| 392 | |
| 393 | index = ofnode_stringlist_search(node, "clock-names", name); |
| 394 | if (index < 0) { |
| 395 | debug("fdt_stringlist_search() failed: %d\n", index); |
| 396 | return index; |
| 397 | } |
| 398 | |
| 399 | return clk_get_by_index_nodev(node, index, clk); |
| 400 | } |
| 401 | |
| 402 | int clk_get_optional_nodev(ofnode node, const char *name, struct clk *clk) |
| 403 | { |
| 404 | int ret; |
| 405 | |
| 406 | ret = clk_get_by_name_nodev(node, name, clk); |
| 407 | if (ret == -ENODATA) |
| 408 | return 0; |
| 409 | |
| 410 | return ret; |
| 411 | } |
| 412 | |
Patrice Chotard | cafc341 | 2017-07-25 13:24:45 +0200 | [diff] [blame] | 413 | int clk_release_all(struct clk *clk, int count) |
| 414 | { |
| 415 | int i, ret; |
| 416 | |
| 417 | for (i = 0; i < count; i++) { |
| 418 | debug("%s(clk[%d]=%p)\n", __func__, i, &clk[i]); |
| 419 | |
| 420 | /* check if clock has been previously requested */ |
| 421 | if (!clk[i].dev) |
| 422 | continue; |
| 423 | |
| 424 | ret = clk_disable(&clk[i]); |
| 425 | if (ret && ret != -ENOSYS) |
| 426 | return ret; |
| 427 | |
| 428 | ret = clk_free(&clk[i]); |
| 429 | if (ret && ret != -ENOSYS) |
| 430 | return ret; |
| 431 | } |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
Simon Glass | 589d915 | 2016-07-04 11:58:03 -0600 | [diff] [blame] | 436 | #endif /* OF_CONTROL */ |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 437 | |
| 438 | int clk_request(struct udevice *dev, struct clk *clk) |
| 439 | { |
Jean-Jacques Hiblot | 718039b | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 440 | const struct clk_ops *ops; |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 441 | |
| 442 | debug("%s(dev=%p, clk=%p)\n", __func__, dev, clk); |
Jean-Jacques Hiblot | 718039b | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 443 | if (!clk) |
| 444 | return 0; |
| 445 | ops = clk_dev_ops(dev); |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 446 | |
| 447 | clk->dev = dev; |
| 448 | |
| 449 | if (!ops->request) |
| 450 | return 0; |
| 451 | |
| 452 | return ops->request(clk); |
| 453 | } |
| 454 | |
| 455 | int clk_free(struct clk *clk) |
| 456 | { |
Jean-Jacques Hiblot | 718039b | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 457 | const struct clk_ops *ops; |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 458 | |
| 459 | debug("%s(clk=%p)\n", __func__, clk); |
developer | dc338d3 | 2020-01-09 11:35:06 +0800 | [diff] [blame] | 460 | if (!clk_valid(clk)) |
Jean-Jacques Hiblot | 718039b | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 461 | return 0; |
| 462 | ops = clk_dev_ops(clk->dev); |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 463 | |
Simon Glass | 2cdd3f4 | 2020-02-03 07:35:54 -0700 | [diff] [blame] | 464 | if (!ops->rfree) |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 465 | return 0; |
| 466 | |
Simon Glass | 2cdd3f4 | 2020-02-03 07:35:54 -0700 | [diff] [blame] | 467 | return ops->rfree(clk); |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | ulong clk_get_rate(struct clk *clk) |
| 471 | { |
Jean-Jacques Hiblot | 718039b | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 472 | const struct clk_ops *ops; |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 473 | |
| 474 | debug("%s(clk=%p)\n", __func__, clk); |
developer | dc338d3 | 2020-01-09 11:35:06 +0800 | [diff] [blame] | 475 | if (!clk_valid(clk)) |
Jean-Jacques Hiblot | 718039b | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 476 | return 0; |
| 477 | ops = clk_dev_ops(clk->dev); |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 478 | |
| 479 | if (!ops->get_rate) |
| 480 | return -ENOSYS; |
| 481 | |
| 482 | return ops->get_rate(clk); |
| 483 | } |
| 484 | |
Lukasz Majewski | 9e38dc3 | 2019-06-24 15:50:42 +0200 | [diff] [blame] | 485 | struct clk *clk_get_parent(struct clk *clk) |
| 486 | { |
| 487 | struct udevice *pdev; |
| 488 | struct clk *pclk; |
| 489 | |
| 490 | debug("%s(clk=%p)\n", __func__, clk); |
developer | dc338d3 | 2020-01-09 11:35:06 +0800 | [diff] [blame] | 491 | if (!clk_valid(clk)) |
Jean-Jacques Hiblot | 718039b | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 492 | return NULL; |
Lukasz Majewski | 9e38dc3 | 2019-06-24 15:50:42 +0200 | [diff] [blame] | 493 | |
| 494 | pdev = dev_get_parent(clk->dev); |
| 495 | pclk = dev_get_clk_ptr(pdev); |
| 496 | if (!pclk) |
| 497 | return ERR_PTR(-ENODEV); |
| 498 | |
| 499 | return pclk; |
| 500 | } |
| 501 | |
Lukasz Majewski | 53155da | 2019-06-24 15:50:43 +0200 | [diff] [blame] | 502 | long long clk_get_parent_rate(struct clk *clk) |
| 503 | { |
| 504 | const struct clk_ops *ops; |
| 505 | struct clk *pclk; |
| 506 | |
| 507 | debug("%s(clk=%p)\n", __func__, clk); |
developer | dc338d3 | 2020-01-09 11:35:06 +0800 | [diff] [blame] | 508 | if (!clk_valid(clk)) |
Jean-Jacques Hiblot | 718039b | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 509 | return 0; |
Lukasz Majewski | 53155da | 2019-06-24 15:50:43 +0200 | [diff] [blame] | 510 | |
| 511 | pclk = clk_get_parent(clk); |
| 512 | if (IS_ERR(pclk)) |
| 513 | return -ENODEV; |
| 514 | |
| 515 | ops = clk_dev_ops(pclk->dev); |
| 516 | if (!ops->get_rate) |
| 517 | return -ENOSYS; |
| 518 | |
Lukasz Majewski | 4ef3217 | 2019-06-24 15:50:46 +0200 | [diff] [blame] | 519 | /* Read the 'rate' if not already set or if proper flag set*/ |
| 520 | if (!pclk->rate || pclk->flags & CLK_GET_RATE_NOCACHE) |
Lukasz Majewski | 53155da | 2019-06-24 15:50:43 +0200 | [diff] [blame] | 521 | pclk->rate = clk_get_rate(pclk); |
| 522 | |
| 523 | return pclk->rate; |
| 524 | } |
| 525 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 526 | ulong clk_set_rate(struct clk *clk, ulong rate) |
| 527 | { |
Jean-Jacques Hiblot | 718039b | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 528 | const struct clk_ops *ops; |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 529 | |
| 530 | debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate); |
developer | dc338d3 | 2020-01-09 11:35:06 +0800 | [diff] [blame] | 531 | if (!clk_valid(clk)) |
Jean-Jacques Hiblot | 718039b | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 532 | return 0; |
| 533 | ops = clk_dev_ops(clk->dev); |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 534 | |
| 535 | if (!ops->set_rate) |
| 536 | return -ENOSYS; |
| 537 | |
| 538 | return ops->set_rate(clk, rate); |
| 539 | } |
| 540 | |
Philipp Tomsich | f8e02b2 | 2018-01-08 11:15:08 +0100 | [diff] [blame] | 541 | int clk_set_parent(struct clk *clk, struct clk *parent) |
| 542 | { |
Jean-Jacques Hiblot | 718039b | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 543 | const struct clk_ops *ops; |
Claudiu Beznea | c8c1600 | 2020-09-07 17:46:34 +0300 | [diff] [blame] | 544 | int ret; |
Philipp Tomsich | f8e02b2 | 2018-01-08 11:15:08 +0100 | [diff] [blame] | 545 | |
| 546 | debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent); |
developer | dc338d3 | 2020-01-09 11:35:06 +0800 | [diff] [blame] | 547 | if (!clk_valid(clk)) |
Jean-Jacques Hiblot | 718039b | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 548 | return 0; |
| 549 | ops = clk_dev_ops(clk->dev); |
Philipp Tomsich | f8e02b2 | 2018-01-08 11:15:08 +0100 | [diff] [blame] | 550 | |
| 551 | if (!ops->set_parent) |
| 552 | return -ENOSYS; |
| 553 | |
Claudiu Beznea | c8c1600 | 2020-09-07 17:46:34 +0300 | [diff] [blame] | 554 | ret = ops->set_parent(clk, parent); |
| 555 | if (ret) |
| 556 | return ret; |
| 557 | |
| 558 | if (CONFIG_IS_ENABLED(CLK_CCF)) |
| 559 | ret = device_reparent(clk->dev, parent->dev); |
| 560 | |
| 561 | return ret; |
Philipp Tomsich | f8e02b2 | 2018-01-08 11:15:08 +0100 | [diff] [blame] | 562 | } |
| 563 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 564 | int clk_enable(struct clk *clk) |
| 565 | { |
Jean-Jacques Hiblot | 718039b | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 566 | const struct clk_ops *ops; |
Peng Fan | 82628e2 | 2019-08-21 13:35:09 +0000 | [diff] [blame] | 567 | struct clk *clkp = NULL; |
| 568 | int ret; |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 569 | |
| 570 | debug("%s(clk=%p)\n", __func__, clk); |
developer | dc338d3 | 2020-01-09 11:35:06 +0800 | [diff] [blame] | 571 | if (!clk_valid(clk)) |
Jean-Jacques Hiblot | 718039b | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 572 | return 0; |
| 573 | ops = clk_dev_ops(clk->dev); |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 574 | |
Peng Fan | 82628e2 | 2019-08-21 13:35:09 +0000 | [diff] [blame] | 575 | if (CONFIG_IS_ENABLED(CLK_CCF)) { |
| 576 | /* Take id 0 as a non-valid clk, such as dummy */ |
| 577 | if (clk->id && !clk_get_by_id(clk->id, &clkp)) { |
| 578 | if (clkp->enable_count) { |
| 579 | clkp->enable_count++; |
| 580 | return 0; |
| 581 | } |
| 582 | if (clkp->dev->parent && |
| 583 | device_get_uclass_id(clkp->dev) == UCLASS_CLK) { |
| 584 | ret = clk_enable(dev_get_clk_ptr(clkp->dev->parent)); |
| 585 | if (ret) { |
| 586 | printf("Enable %s failed\n", |
| 587 | clkp->dev->parent->name); |
| 588 | return ret; |
| 589 | } |
| 590 | } |
| 591 | } |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 592 | |
Peng Fan | 82628e2 | 2019-08-21 13:35:09 +0000 | [diff] [blame] | 593 | if (ops->enable) { |
| 594 | ret = ops->enable(clk); |
| 595 | if (ret) { |
| 596 | printf("Enable %s failed\n", clk->dev->name); |
| 597 | return ret; |
| 598 | } |
| 599 | } |
| 600 | if (clkp) |
| 601 | clkp->enable_count++; |
| 602 | } else { |
| 603 | if (!ops->enable) |
| 604 | return -ENOSYS; |
| 605 | return ops->enable(clk); |
| 606 | } |
| 607 | |
| 608 | return 0; |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 609 | } |
| 610 | |
Neil Armstrong | 8a275a0 | 2018-04-03 11:44:18 +0200 | [diff] [blame] | 611 | int clk_enable_bulk(struct clk_bulk *bulk) |
| 612 | { |
| 613 | int i, ret; |
| 614 | |
| 615 | for (i = 0; i < bulk->count; i++) { |
| 616 | ret = clk_enable(&bulk->clks[i]); |
| 617 | if (ret < 0 && ret != -ENOSYS) |
| 618 | return ret; |
| 619 | } |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 624 | int clk_disable(struct clk *clk) |
| 625 | { |
Jean-Jacques Hiblot | 718039b | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 626 | const struct clk_ops *ops; |
Peng Fan | 82628e2 | 2019-08-21 13:35:09 +0000 | [diff] [blame] | 627 | struct clk *clkp = NULL; |
| 628 | int ret; |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 629 | |
| 630 | debug("%s(clk=%p)\n", __func__, clk); |
developer | dc338d3 | 2020-01-09 11:35:06 +0800 | [diff] [blame] | 631 | if (!clk_valid(clk)) |
Jean-Jacques Hiblot | 718039b | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 632 | return 0; |
| 633 | ops = clk_dev_ops(clk->dev); |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 634 | |
Peng Fan | 82628e2 | 2019-08-21 13:35:09 +0000 | [diff] [blame] | 635 | if (CONFIG_IS_ENABLED(CLK_CCF)) { |
| 636 | if (clk->id && !clk_get_by_id(clk->id, &clkp)) { |
Claudiu Beznea | b02e8dd | 2020-09-07 17:46:35 +0300 | [diff] [blame] | 637 | if (clkp->flags & CLK_IS_CRITICAL) |
| 638 | return 0; |
| 639 | |
Peng Fan | 82628e2 | 2019-08-21 13:35:09 +0000 | [diff] [blame] | 640 | if (clkp->enable_count == 0) { |
| 641 | printf("clk %s already disabled\n", |
| 642 | clkp->dev->name); |
| 643 | return 0; |
| 644 | } |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 645 | |
Peng Fan | 82628e2 | 2019-08-21 13:35:09 +0000 | [diff] [blame] | 646 | if (--clkp->enable_count > 0) |
| 647 | return 0; |
| 648 | } |
| 649 | |
| 650 | if (ops->disable) { |
| 651 | ret = ops->disable(clk); |
| 652 | if (ret) |
| 653 | return ret; |
| 654 | } |
| 655 | |
| 656 | if (clkp && clkp->dev->parent && |
| 657 | device_get_uclass_id(clkp->dev) == UCLASS_CLK) { |
| 658 | ret = clk_disable(dev_get_clk_ptr(clkp->dev->parent)); |
| 659 | if (ret) { |
| 660 | printf("Disable %s failed\n", |
| 661 | clkp->dev->parent->name); |
| 662 | return ret; |
| 663 | } |
| 664 | } |
| 665 | } else { |
| 666 | if (!ops->disable) |
| 667 | return -ENOSYS; |
| 668 | |
| 669 | return ops->disable(clk); |
| 670 | } |
| 671 | |
| 672 | return 0; |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 673 | } |
Simon Glass | 36ad234 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 674 | |
Neil Armstrong | 8a275a0 | 2018-04-03 11:44:18 +0200 | [diff] [blame] | 675 | int clk_disable_bulk(struct clk_bulk *bulk) |
| 676 | { |
| 677 | int i, ret; |
| 678 | |
| 679 | for (i = 0; i < bulk->count; i++) { |
| 680 | ret = clk_disable(&bulk->clks[i]); |
| 681 | if (ret < 0 && ret != -ENOSYS) |
| 682 | return ret; |
| 683 | } |
| 684 | |
| 685 | return 0; |
| 686 | } |
| 687 | |
Lukasz Majewski | 12014be | 2019-06-24 15:50:44 +0200 | [diff] [blame] | 688 | int clk_get_by_id(ulong id, struct clk **clkp) |
| 689 | { |
| 690 | struct udevice *dev; |
| 691 | struct uclass *uc; |
| 692 | int ret; |
| 693 | |
| 694 | ret = uclass_get(UCLASS_CLK, &uc); |
| 695 | if (ret) |
| 696 | return ret; |
| 697 | |
| 698 | uclass_foreach_dev(dev, uc) { |
| 699 | struct clk *clk = dev_get_clk_ptr(dev); |
| 700 | |
| 701 | if (clk && clk->id == id) { |
| 702 | *clkp = clk; |
| 703 | return 0; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | return -ENOENT; |
| 708 | } |
| 709 | |
Sekhar Nori | cf3119d | 2019-08-01 19:12:55 +0530 | [diff] [blame] | 710 | bool clk_is_match(const struct clk *p, const struct clk *q) |
| 711 | { |
| 712 | /* trivial case: identical struct clk's or both NULL */ |
| 713 | if (p == q) |
| 714 | return true; |
| 715 | |
Jean-Jacques Hiblot | 718039b | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 716 | /* trivial case #2: on the clk pointer is NULL */ |
| 717 | if (!p || !q) |
| 718 | return false; |
| 719 | |
Sekhar Nori | cf3119d | 2019-08-01 19:12:55 +0530 | [diff] [blame] | 720 | /* same device, id and data */ |
| 721 | if (p->dev == q->dev && p->id == q->id && p->data == q->data) |
| 722 | return true; |
| 723 | |
| 724 | return false; |
| 725 | } |
| 726 | |
Jean-Jacques Hiblot | 6e66b2d | 2019-10-22 14:00:04 +0200 | [diff] [blame] | 727 | static void devm_clk_release(struct udevice *dev, void *res) |
| 728 | { |
| 729 | clk_free(res); |
| 730 | } |
| 731 | |
| 732 | static int devm_clk_match(struct udevice *dev, void *res, void *data) |
| 733 | { |
| 734 | return res == data; |
| 735 | } |
| 736 | |
| 737 | struct clk *devm_clk_get(struct udevice *dev, const char *id) |
| 738 | { |
| 739 | int rc; |
| 740 | struct clk *clk; |
| 741 | |
| 742 | clk = devres_alloc(devm_clk_release, sizeof(struct clk), __GFP_ZERO); |
| 743 | if (unlikely(!clk)) |
| 744 | return ERR_PTR(-ENOMEM); |
| 745 | |
| 746 | rc = clk_get_by_name(dev, id, clk); |
| 747 | if (rc) |
| 748 | return ERR_PTR(rc); |
| 749 | |
| 750 | devres_add(dev, clk); |
| 751 | return clk; |
| 752 | } |
| 753 | |
| 754 | struct clk *devm_clk_get_optional(struct udevice *dev, const char *id) |
| 755 | { |
| 756 | struct clk *clk = devm_clk_get(dev, id); |
| 757 | |
developer | 5e108fb | 2020-01-09 11:35:05 +0800 | [diff] [blame] | 758 | if (PTR_ERR(clk) == -ENODATA) |
Jean-Jacques Hiblot | 6e66b2d | 2019-10-22 14:00:04 +0200 | [diff] [blame] | 759 | return NULL; |
| 760 | |
| 761 | return clk; |
| 762 | } |
| 763 | |
| 764 | void devm_clk_put(struct udevice *dev, struct clk *clk) |
| 765 | { |
| 766 | int rc; |
| 767 | |
| 768 | if (!clk) |
| 769 | return; |
| 770 | |
| 771 | rc = devres_release(dev, devm_clk_release, devm_clk_match, clk); |
| 772 | WARN_ON(rc); |
| 773 | } |
| 774 | |
Jean-Jacques Hiblot | 9601f32 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 775 | int clk_uclass_post_probe(struct udevice *dev) |
| 776 | { |
| 777 | /* |
| 778 | * when a clock provider is probed. Call clk_set_defaults() |
| 779 | * also after the device is probed. This takes care of cases |
| 780 | * where the DT is used to setup default parents and rates |
| 781 | * using assigned-clocks |
| 782 | */ |
| 783 | clk_set_defaults(dev, 1); |
| 784 | |
| 785 | return 0; |
| 786 | } |
| 787 | |
Simon Glass | 36ad234 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 788 | UCLASS_DRIVER(clk) = { |
| 789 | .id = UCLASS_CLK, |
| 790 | .name = "clk", |
Jean-Jacques Hiblot | 9601f32 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 791 | .post_probe = clk_uclass_post_probe, |
Simon Glass | 36ad234 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 792 | }; |