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