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