Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | f26c8a8 | 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 | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 5 | * Copyright (c) 2016, NVIDIA CORPORATION. |
Philipp Tomsich | f4fcba5 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 6 | * Copyright (c) 2018, Theobroma Systems Design und Consulting GmbH |
Simon Glass | f26c8a8 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 7 | */ |
| 8 | |
Patrick Delaunay | b953ec2 | 2021-04-27 11:02:19 +0200 | [diff] [blame] | 9 | #define LOG_CATEGORY UCLASS_CLK |
| 10 | |
Simon Glass | f26c8a8 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 11 | #include <common.h> |
| 12 | #include <clk.h> |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 13 | #include <clk-uclass.h> |
Simon Glass | f26c8a8 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 14 | #include <dm.h> |
Simon Glass | 7423daa | 2016-07-04 11:58:03 -0600 | [diff] [blame] | 15 | #include <dt-structs.h> |
Simon Glass | f26c8a8 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 16 | #include <errno.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 17 | #include <log.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 18 | #include <malloc.h> |
Patrick Delaunay | 572c446 | 2021-11-19 15:12:06 +0100 | [diff] [blame] | 19 | #include <asm/global_data.h> |
Sean Anderson | 8c12cb3 | 2021-04-08 22:13:03 -0400 | [diff] [blame] | 20 | #include <dm/device_compat.h> |
Claudiu Beznea | 4d139f3 | 2020-09-07 17:46:34 +0300 | [diff] [blame] | 21 | #include <dm/device-internal.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 22 | #include <dm/devres.h> |
| 23 | #include <dm/read.h> |
Simon Glass | eb41d8a | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 24 | #include <linux/bug.h> |
Lukasz Majewski | 0c660c2 | 2019-06-24 15:50:42 +0200 | [diff] [blame] | 25 | #include <linux/clk-provider.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 26 | #include <linux/err.h> |
Simon Glass | f26c8a8 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 27 | |
Mario Six | 268453b | 2018-01-15 11:06:51 +0100 | [diff] [blame] | 28 | static inline const struct clk_ops *clk_dev_ops(struct udevice *dev) |
Simon Glass | f26c8a8 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 29 | { |
Mario Six | 268453b | 2018-01-15 11:06:51 +0100 | [diff] [blame] | 30 | return (const struct clk_ops *)dev->driver->ops; |
Simon Glass | f26c8a8 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 31 | } |
| 32 | |
Simon Glass | fb989e0 | 2020-07-19 10:15:56 -0600 | [diff] [blame] | 33 | struct clk *dev_get_clk_ptr(struct udevice *dev) |
| 34 | { |
| 35 | return (struct clk *)dev_get_uclass_priv(dev); |
| 36 | } |
| 37 | |
Simon Glass | 414cc15 | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 38 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
Simon Glass | f0ab8f9 | 2021-08-07 07:24:09 -0600 | [diff] [blame] | 39 | int clk_get_by_phandle(struct udevice *dev, const struct phandle_1_arg *cells, |
| 40 | struct clk *clk) |
Simon Glass | 7423daa | 2016-07-04 11:58:03 -0600 | [diff] [blame] | 41 | { |
| 42 | int ret; |
| 43 | |
Simon Glass | cc469b7 | 2021-03-15 17:25:28 +1300 | [diff] [blame] | 44 | ret = device_get_by_ofplat_idx(cells->idx, &clk->dev); |
Simon Glass | 7423daa | 2016-07-04 11:58:03 -0600 | [diff] [blame] | 45 | if (ret) |
| 46 | return ret; |
Walter Lozano | 51f1263 | 2020-06-25 01:10:13 -0300 | [diff] [blame] | 47 | clk->id = cells->arg[0]; |
Simon Glass | 7423daa | 2016-07-04 11:58:03 -0600 | [diff] [blame] | 48 | |
| 49 | return 0; |
| 50 | } |
Simon Glass | 414cc15 | 2021-08-07 07:24:03 -0600 | [diff] [blame] | 51 | #endif |
| 52 | |
| 53 | #if CONFIG_IS_ENABLED(OF_REAL) |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 54 | static int clk_of_xlate_default(struct clk *clk, |
Simon Glass | a4e0ef5 | 2017-05-18 20:09:40 -0600 | [diff] [blame] | 55 | struct ofnode_phandle_args *args) |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 56 | { |
| 57 | debug("%s(clk=%p)\n", __func__, clk); |
| 58 | |
| 59 | if (args->args_count > 1) { |
Sean Anderson | 46ad7ce | 2021-12-01 14:26:53 -0500 | [diff] [blame] | 60 | debug("Invalid args_count: %d\n", args->args_count); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 61 | return -EINVAL; |
| 62 | } |
| 63 | |
| 64 | if (args->args_count) |
| 65 | clk->id = args->args[0]; |
| 66 | else |
| 67 | clk->id = 0; |
| 68 | |
Sekhar Nori | e497fab | 2019-07-11 14:30:24 +0530 | [diff] [blame] | 69 | clk->data = 0; |
| 70 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 71 | return 0; |
| 72 | } |
| 73 | |
Jagan Teki | 75f9831 | 2019-02-28 00:26:52 +0530 | [diff] [blame] | 74 | static int clk_get_by_index_tail(int ret, ofnode node, |
| 75 | struct ofnode_phandle_args *args, |
| 76 | const char *list_name, int index, |
| 77 | struct clk *clk) |
| 78 | { |
| 79 | struct udevice *dev_clk; |
| 80 | const struct clk_ops *ops; |
| 81 | |
| 82 | assert(clk); |
| 83 | clk->dev = NULL; |
| 84 | if (ret) |
| 85 | goto err; |
| 86 | |
| 87 | ret = uclass_get_device_by_ofnode(UCLASS_CLK, args->node, &dev_clk); |
| 88 | if (ret) { |
| 89 | debug("%s: uclass_get_device_by_of_offset failed: err=%d\n", |
| 90 | __func__, ret); |
Simon Glass | 5c5992c | 2021-01-21 13:57:11 -0700 | [diff] [blame] | 91 | return log_msg_ret("get", ret); |
Jagan Teki | 75f9831 | 2019-02-28 00:26:52 +0530 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | clk->dev = dev_clk; |
| 95 | |
| 96 | ops = clk_dev_ops(dev_clk); |
| 97 | |
| 98 | if (ops->of_xlate) |
| 99 | ret = ops->of_xlate(clk, args); |
| 100 | else |
| 101 | ret = clk_of_xlate_default(clk, args); |
| 102 | if (ret) { |
| 103 | debug("of_xlate() failed: %d\n", ret); |
Simon Glass | 5c5992c | 2021-01-21 13:57:11 -0700 | [diff] [blame] | 104 | return log_msg_ret("xlate", ret); |
Jagan Teki | 75f9831 | 2019-02-28 00:26:52 +0530 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | return clk_request(dev_clk, clk); |
| 108 | err: |
| 109 | debug("%s: Node '%s', property '%s', failed to request CLK index %d: %d\n", |
| 110 | __func__, ofnode_get_name(node), list_name, index, ret); |
Simon Glass | 5c5992c | 2021-01-21 13:57:11 -0700 | [diff] [blame] | 111 | |
| 112 | return log_msg_ret("prop", ret); |
Jagan Teki | 75f9831 | 2019-02-28 00:26:52 +0530 | [diff] [blame] | 113 | } |
| 114 | |
Philipp Tomsich | 95f9a7e | 2018-01-08 11:18:18 +0100 | [diff] [blame] | 115 | static int clk_get_by_indexed_prop(struct udevice *dev, const char *prop_name, |
| 116 | int index, struct clk *clk) |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 117 | { |
| 118 | int ret; |
Simon Glass | aa9bb09 | 2017-05-30 21:47:29 -0600 | [diff] [blame] | 119 | struct ofnode_phandle_args args; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 120 | |
| 121 | debug("%s(dev=%p, index=%d, clk=%p)\n", __func__, dev, index, clk); |
| 122 | |
| 123 | assert(clk); |
Patrice Chotard | 82a8a66 | 2017-07-18 11:57:07 +0200 | [diff] [blame] | 124 | clk->dev = NULL; |
| 125 | |
Philipp Tomsich | 95f9a7e | 2018-01-08 11:18:18 +0100 | [diff] [blame] | 126 | ret = dev_read_phandle_with_args(dev, prop_name, "#clock-cells", 0, |
Mario Six | 268453b | 2018-01-15 11:06:51 +0100 | [diff] [blame] | 127 | index, &args); |
Simon Glass | e70cc43 | 2016-01-20 19:43:02 -0700 | [diff] [blame] | 128 | if (ret) { |
| 129 | debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n", |
| 130 | __func__, ret); |
Simon Glass | 5c5992c | 2021-01-21 13:57:11 -0700 | [diff] [blame] | 131 | return log_ret(ret); |
Simon Glass | e70cc43 | 2016-01-20 19:43:02 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Wenyou Yang | 3f56b13 | 2016-09-27 11:00:28 +0800 | [diff] [blame] | 134 | |
Jagan Teki | dcb63fc | 2019-02-28 00:26:53 +0530 | [diff] [blame] | 135 | return clk_get_by_index_tail(ret, dev_ofnode(dev), &args, "clocks", |
Sean Anderson | 675d790 | 2020-06-24 06:41:08 -0400 | [diff] [blame] | 136 | index, clk); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 137 | } |
Philipp Tomsich | 95f9a7e | 2018-01-08 11:18:18 +0100 | [diff] [blame] | 138 | |
| 139 | int clk_get_by_index(struct udevice *dev, int index, struct clk *clk) |
| 140 | { |
Sean Anderson | e7075ff | 2022-02-27 14:01:13 -0500 | [diff] [blame] | 141 | return clk_get_by_index_nodev(dev_ofnode(dev), index, clk); |
Jagan Teki | 75f9831 | 2019-02-28 00:26:52 +0530 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | int clk_get_by_index_nodev(ofnode node, int index, struct clk *clk) |
| 145 | { |
| 146 | struct ofnode_phandle_args args; |
| 147 | int ret; |
| 148 | |
| 149 | ret = ofnode_parse_phandle_with_args(node, "clocks", "#clock-cells", 0, |
Sean Anderson | 675d790 | 2020-06-24 06:41:08 -0400 | [diff] [blame] | 150 | index, &args); |
Jagan Teki | 75f9831 | 2019-02-28 00:26:52 +0530 | [diff] [blame] | 151 | |
| 152 | return clk_get_by_index_tail(ret, node, &args, "clocks", |
Sean Anderson | 675d790 | 2020-06-24 06:41:08 -0400 | [diff] [blame] | 153 | index, clk); |
Philipp Tomsich | 95f9a7e | 2018-01-08 11:18:18 +0100 | [diff] [blame] | 154 | } |
Philipp Tomsich | f4fcba5 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 155 | |
Neil Armstrong | a855be8 | 2018-04-03 11:44:18 +0200 | [diff] [blame] | 156 | int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk) |
| 157 | { |
| 158 | int i, ret, err, count; |
Patrick Delaunay | c262522 | 2021-04-27 10:57:54 +0200 | [diff] [blame] | 159 | |
Neil Armstrong | a855be8 | 2018-04-03 11:44:18 +0200 | [diff] [blame] | 160 | bulk->count = 0; |
| 161 | |
Patrick Delaunay | 89f6830 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 162 | count = dev_count_phandle_with_args(dev, "clocks", "#clock-cells", 0); |
Neil Armstrong | 721881c | 2018-04-17 11:30:31 +0200 | [diff] [blame] | 163 | if (count < 1) |
| 164 | return count; |
Neil Armstrong | a855be8 | 2018-04-03 11:44:18 +0200 | [diff] [blame] | 165 | |
| 166 | bulk->clks = devm_kcalloc(dev, count, sizeof(struct clk), GFP_KERNEL); |
| 167 | if (!bulk->clks) |
| 168 | return -ENOMEM; |
| 169 | |
| 170 | for (i = 0; i < count; i++) { |
| 171 | ret = clk_get_by_index(dev, i, &bulk->clks[i]); |
| 172 | if (ret < 0) |
| 173 | goto bulk_get_err; |
| 174 | |
| 175 | ++bulk->count; |
| 176 | } |
| 177 | |
| 178 | return 0; |
| 179 | |
| 180 | bulk_get_err: |
| 181 | err = clk_release_all(bulk->clks, bulk->count); |
| 182 | if (err) |
| 183 | debug("%s: could release all clocks for %p\n", |
| 184 | __func__, dev); |
| 185 | |
| 186 | return ret; |
| 187 | } |
| 188 | |
Claudiu Beznea | b364134 | 2020-09-07 17:46:36 +0300 | [diff] [blame] | 189 | static struct clk *clk_set_default_get_by_id(struct clk *clk) |
| 190 | { |
| 191 | struct clk *c = clk; |
| 192 | |
| 193 | if (CONFIG_IS_ENABLED(CLK_CCF)) { |
| 194 | int ret = clk_get_by_id(clk->id, &c); |
| 195 | |
| 196 | if (ret) { |
| 197 | debug("%s(): could not get parent clock pointer, id %lu\n", |
| 198 | __func__, clk->id); |
| 199 | ERR_PTR(ret); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return c; |
| 204 | } |
| 205 | |
Sean Anderson | 6e33eba | 2021-06-11 00:16:07 -0400 | [diff] [blame] | 206 | static int clk_set_default_parents(struct udevice *dev, |
| 207 | enum clk_defaults_stage stage) |
Philipp Tomsich | f4fcba5 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 208 | { |
Claudiu Beznea | b364134 | 2020-09-07 17:46:36 +0300 | [diff] [blame] | 209 | struct clk clk, parent_clk, *c, *p; |
Philipp Tomsich | f4fcba5 | 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 | 89f6830 | 2020-09-25 09:41:14 +0200 | [diff] [blame] | 215 | "#clock-cells", 0); |
Philipp Tomsich | f4fcba5 | 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 | d64caaf | 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 | f4fcba5 | 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 | b364134 | 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 | f4fcba5 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 239 | ret = clk_get_by_indexed_prop(dev, "assigned-clocks", |
| 240 | index, &clk); |
Tero Kristo | 1e1fab0 | 2021-06-11 11:45:11 +0300 | [diff] [blame] | 241 | /* |
| 242 | * If the clock provider is not ready yet, let it handle |
| 243 | * the re-programming later. |
| 244 | */ |
| 245 | if (ret == -EPROBE_DEFER) { |
| 246 | ret = 0; |
| 247 | continue; |
| 248 | } |
| 249 | |
Philipp Tomsich | f4fcba5 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 250 | if (ret) { |
| 251 | debug("%s: could not get assigned clock %d for %s\n", |
| 252 | __func__, index, dev_read_name(dev)); |
| 253 | return ret; |
| 254 | } |
| 255 | |
Jean-Jacques Hiblot | fd1ba29 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 256 | /* This is clk provider device trying to reparent itself |
| 257 | * It cannot be done right now but need to wait after the |
| 258 | * device is probed |
| 259 | */ |
Sean Anderson | 6e33eba | 2021-06-11 00:16:07 -0400 | [diff] [blame] | 260 | if (stage == CLK_DEFAULTS_PRE && clk.dev == dev) |
Jean-Jacques Hiblot | fd1ba29 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 261 | continue; |
Philipp Tomsich | f4fcba5 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 262 | |
Sean Anderson | 6e33eba | 2021-06-11 00:16:07 -0400 | [diff] [blame] | 263 | if (stage != CLK_DEFAULTS_PRE && clk.dev != dev) |
Jean-Jacques Hiblot | fd1ba29 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 264 | /* do not setup twice the parent clocks */ |
| 265 | continue; |
| 266 | |
Claudiu Beznea | b364134 | 2020-09-07 17:46:36 +0300 | [diff] [blame] | 267 | c = clk_set_default_get_by_id(&clk); |
| 268 | if (IS_ERR(c)) |
| 269 | return PTR_ERR(c); |
| 270 | |
| 271 | ret = clk_set_parent(c, p); |
Philipp Tomsich | f4fcba5 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 272 | /* |
| 273 | * Not all drivers may support clock-reparenting (as of now). |
| 274 | * Ignore errors due to this. |
| 275 | */ |
| 276 | if (ret == -ENOSYS) |
| 277 | continue; |
| 278 | |
Jean-Jacques Hiblot | 02e2a2a | 2019-09-26 15:42:42 +0200 | [diff] [blame] | 279 | if (ret < 0) { |
Philipp Tomsich | f4fcba5 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 280 | debug("%s: failed to reparent clock %d for %s\n", |
| 281 | __func__, index, dev_read_name(dev)); |
| 282 | return ret; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
Sean Anderson | 6e33eba | 2021-06-11 00:16:07 -0400 | [diff] [blame] | 289 | static int clk_set_default_rates(struct udevice *dev, |
| 290 | enum clk_defaults_stage stage) |
Philipp Tomsich | f4fcba5 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 291 | { |
Claudiu Beznea | b364134 | 2020-09-07 17:46:36 +0300 | [diff] [blame] | 292 | struct clk clk, *c; |
Philipp Tomsich | f4fcba5 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 293 | int index; |
| 294 | int num_rates; |
| 295 | int size; |
| 296 | int ret = 0; |
| 297 | u32 *rates = NULL; |
| 298 | |
| 299 | size = dev_read_size(dev, "assigned-clock-rates"); |
| 300 | if (size < 0) |
| 301 | return 0; |
| 302 | |
| 303 | num_rates = size / sizeof(u32); |
| 304 | rates = calloc(num_rates, sizeof(u32)); |
| 305 | if (!rates) |
| 306 | return -ENOMEM; |
| 307 | |
| 308 | ret = dev_read_u32_array(dev, "assigned-clock-rates", rates, num_rates); |
| 309 | if (ret) |
| 310 | goto fail; |
| 311 | |
| 312 | for (index = 0; index < num_rates; index++) { |
Neil Armstrong | d64caaf | 2018-07-26 15:19:32 +0200 | [diff] [blame] | 313 | /* If 0 is passed, this is a no-op */ |
| 314 | if (!rates[index]) |
| 315 | continue; |
| 316 | |
Philipp Tomsich | f4fcba5 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 317 | ret = clk_get_by_indexed_prop(dev, "assigned-clocks", |
| 318 | index, &clk); |
Tero Kristo | 1e1fab0 | 2021-06-11 11:45:11 +0300 | [diff] [blame] | 319 | /* |
| 320 | * If the clock provider is not ready yet, let it handle |
| 321 | * the re-programming later. |
| 322 | */ |
| 323 | if (ret == -EPROBE_DEFER) { |
| 324 | ret = 0; |
| 325 | continue; |
| 326 | } |
| 327 | |
Philipp Tomsich | f4fcba5 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 328 | if (ret) { |
Sean Anderson | 8c12cb3 | 2021-04-08 22:13:03 -0400 | [diff] [blame] | 329 | dev_dbg(dev, |
| 330 | "could not get assigned clock %d (err = %d)\n", |
| 331 | index, ret); |
Ashok Reddy Soma | 99b4647 | 2023-08-30 10:31:42 +0200 | [diff] [blame] | 332 | /* Skip if it is empty */ |
| 333 | if (ret == -ENOENT) { |
| 334 | ret = 0; |
| 335 | continue; |
| 336 | } |
| 337 | |
| 338 | return ret; |
Philipp Tomsich | f4fcba5 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 339 | } |
| 340 | |
Jean-Jacques Hiblot | fd1ba29 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 341 | /* This is clk provider device trying to program itself |
| 342 | * It cannot be done right now but need to wait after the |
| 343 | * device is probed |
| 344 | */ |
Sean Anderson | 6e33eba | 2021-06-11 00:16:07 -0400 | [diff] [blame] | 345 | if (stage == CLK_DEFAULTS_PRE && clk.dev == dev) |
Jean-Jacques Hiblot | fd1ba29 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 346 | continue; |
| 347 | |
Sean Anderson | 6e33eba | 2021-06-11 00:16:07 -0400 | [diff] [blame] | 348 | if (stage != CLK_DEFAULTS_PRE && clk.dev != dev) |
Jean-Jacques Hiblot | fd1ba29 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 349 | /* do not setup twice the parent clocks */ |
| 350 | continue; |
| 351 | |
Claudiu Beznea | b364134 | 2020-09-07 17:46:36 +0300 | [diff] [blame] | 352 | c = clk_set_default_get_by_id(&clk); |
| 353 | if (IS_ERR(c)) |
| 354 | return PTR_ERR(c); |
| 355 | |
| 356 | ret = clk_set_rate(c, rates[index]); |
Jean-Jacques Hiblot | fd1ba29 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 357 | |
Philipp Tomsich | f4fcba5 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 358 | if (ret < 0) { |
Sean Anderson | 8c12cb3 | 2021-04-08 22:13:03 -0400 | [diff] [blame] | 359 | dev_warn(dev, |
| 360 | "failed to set rate on clock index %d (%ld) (error = %d)\n", |
| 361 | index, clk.id, ret); |
Philipp Tomsich | f4fcba5 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 362 | break; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | fail: |
| 367 | free(rates); |
| 368 | return ret; |
| 369 | } |
| 370 | |
Sean Anderson | 6e33eba | 2021-06-11 00:16:07 -0400 | [diff] [blame] | 371 | int clk_set_defaults(struct udevice *dev, enum clk_defaults_stage stage) |
Philipp Tomsich | f4fcba5 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 372 | { |
| 373 | int ret; |
| 374 | |
Simon Glass | 7d14ee4 | 2020-12-19 10:40:13 -0700 | [diff] [blame] | 375 | if (!dev_has_ofnode(dev)) |
Peng Fan | 91944ef | 2019-07-31 07:01:49 +0000 | [diff] [blame] | 376 | return 0; |
| 377 | |
Sean Anderson | 6e33eba | 2021-06-11 00:16:07 -0400 | [diff] [blame] | 378 | /* |
| 379 | * To avoid setting defaults twice, don't set them before relocation. |
| 380 | * However, still set them for SPL. And still set them if explicitly |
| 381 | * asked. |
| 382 | */ |
Philipp Tomsich | 291da96 | 2018-11-26 20:20:19 +0100 | [diff] [blame] | 383 | if (!(IS_ENABLED(CONFIG_SPL_BUILD) || (gd->flags & GD_FLG_RELOC))) |
Sean Anderson | 6e33eba | 2021-06-11 00:16:07 -0400 | [diff] [blame] | 384 | if (stage != CLK_DEFAULTS_POST_FORCE) |
| 385 | return 0; |
Philipp Tomsich | 291da96 | 2018-11-26 20:20:19 +0100 | [diff] [blame] | 386 | |
Philipp Tomsich | f4fcba5 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 387 | debug("%s(%s)\n", __func__, dev_read_name(dev)); |
| 388 | |
Jean-Jacques Hiblot | fd1ba29 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 389 | ret = clk_set_default_parents(dev, stage); |
Philipp Tomsich | f4fcba5 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 390 | if (ret) |
| 391 | return ret; |
| 392 | |
Jean-Jacques Hiblot | fd1ba29 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 393 | ret = clk_set_default_rates(dev, stage); |
Philipp Tomsich | f4fcba5 | 2018-01-08 13:59:18 +0100 | [diff] [blame] | 394 | if (ret < 0) |
| 395 | return ret; |
| 396 | |
| 397 | return 0; |
| 398 | } |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 399 | |
| 400 | int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk) |
| 401 | { |
Sean Anderson | e7075ff | 2022-02-27 14:01:13 -0500 | [diff] [blame] | 402 | return clk_get_by_name_nodev(dev_ofnode(dev), name, clk); |
Simon Glass | e70cc43 | 2016-01-20 19:43:02 -0700 | [diff] [blame] | 403 | } |
Simon Glass | f0ab8f9 | 2021-08-07 07:24:09 -0600 | [diff] [blame] | 404 | #endif /* OF_REAL */ |
Patrice Chotard | b108d8a | 2017-07-25 13:24:45 +0200 | [diff] [blame] | 405 | |
Chunfeng Yun | d646420 | 2020-01-09 11:35:07 +0800 | [diff] [blame] | 406 | int clk_get_by_name_nodev(ofnode node, const char *name, struct clk *clk) |
| 407 | { |
Samuel Holland | 2050f82 | 2023-01-21 18:02:51 -0600 | [diff] [blame] | 408 | int index = 0; |
Chunfeng Yun | d646420 | 2020-01-09 11:35:07 +0800 | [diff] [blame] | 409 | |
| 410 | debug("%s(node=%p, name=%s, clk=%p)\n", __func__, |
| 411 | ofnode_get_name(node), name, clk); |
| 412 | clk->dev = NULL; |
| 413 | |
Samuel Holland | 2050f82 | 2023-01-21 18:02:51 -0600 | [diff] [blame] | 414 | if (name) { |
| 415 | index = ofnode_stringlist_search(node, "clock-names", name); |
| 416 | if (index < 0) { |
| 417 | debug("fdt_stringlist_search() failed: %d\n", index); |
| 418 | return index; |
| 419 | } |
Chunfeng Yun | d646420 | 2020-01-09 11:35:07 +0800 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | return clk_get_by_index_nodev(node, index, clk); |
| 423 | } |
| 424 | |
Eugen Hristev | b6a56f5 | 2023-06-19 13:47:52 +0300 | [diff] [blame] | 425 | int clk_release_all(struct clk *clk, unsigned int count) |
Patrice Chotard | b108d8a | 2017-07-25 13:24:45 +0200 | [diff] [blame] | 426 | { |
Eugen Hristev | b6a56f5 | 2023-06-19 13:47:52 +0300 | [diff] [blame] | 427 | unsigned int i; |
| 428 | int ret; |
Patrice Chotard | b108d8a | 2017-07-25 13:24:45 +0200 | [diff] [blame] | 429 | |
| 430 | for (i = 0; i < count; i++) { |
Eugen Hristev | b6a56f5 | 2023-06-19 13:47:52 +0300 | [diff] [blame] | 431 | debug("%s(clk[%u]=%p)\n", __func__, i, &clk[i]); |
Patrice Chotard | b108d8a | 2017-07-25 13:24:45 +0200 | [diff] [blame] | 432 | |
| 433 | /* check if clock has been previously requested */ |
| 434 | if (!clk[i].dev) |
| 435 | continue; |
| 436 | |
| 437 | ret = clk_disable(&clk[i]); |
| 438 | if (ret && ret != -ENOSYS) |
| 439 | return ret; |
| 440 | |
Sean Anderson | ac15e78 | 2022-01-15 17:25:04 -0500 | [diff] [blame] | 441 | clk_free(&clk[i]); |
Patrice Chotard | b108d8a | 2017-07-25 13:24:45 +0200 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | return 0; |
| 445 | } |
| 446 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 447 | int clk_request(struct udevice *dev, struct clk *clk) |
| 448 | { |
Jean-Jacques Hiblot | 8a1661f | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 449 | const struct clk_ops *ops; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 450 | |
| 451 | debug("%s(dev=%p, clk=%p)\n", __func__, dev, clk); |
Jean-Jacques Hiblot | 8a1661f | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 452 | if (!clk) |
| 453 | return 0; |
| 454 | ops = clk_dev_ops(dev); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 455 | |
| 456 | clk->dev = dev; |
| 457 | |
| 458 | if (!ops->request) |
| 459 | return 0; |
| 460 | |
| 461 | return ops->request(clk); |
| 462 | } |
| 463 | |
Sean Anderson | ac15e78 | 2022-01-15 17:25:04 -0500 | [diff] [blame] | 464 | void clk_free(struct clk *clk) |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 465 | { |
Jean-Jacques Hiblot | 8a1661f | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 466 | const struct clk_ops *ops; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 467 | |
| 468 | debug("%s(clk=%p)\n", __func__, clk); |
Chunfeng Yun | bd7c798 | 2020-01-09 11:35:06 +0800 | [diff] [blame] | 469 | if (!clk_valid(clk)) |
Sean Anderson | ac15e78 | 2022-01-15 17:25:04 -0500 | [diff] [blame] | 470 | return; |
Jean-Jacques Hiblot | 8a1661f | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 471 | ops = clk_dev_ops(clk->dev); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 472 | |
Sean Anderson | 276d446 | 2022-01-15 17:24:58 -0500 | [diff] [blame] | 473 | if (ops->rfree) |
| 474 | ops->rfree(clk); |
Sean Anderson | ac15e78 | 2022-01-15 17:25:04 -0500 | [diff] [blame] | 475 | return; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | ulong clk_get_rate(struct clk *clk) |
| 479 | { |
Jean-Jacques Hiblot | 8a1661f | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 480 | const struct clk_ops *ops; |
Nathan Barrett-Morrison | aed6480 | 2023-05-15 15:49:58 -0400 | [diff] [blame] | 481 | ulong ret; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 482 | |
| 483 | debug("%s(clk=%p)\n", __func__, clk); |
Chunfeng Yun | bd7c798 | 2020-01-09 11:35:06 +0800 | [diff] [blame] | 484 | if (!clk_valid(clk)) |
Jean-Jacques Hiblot | 8a1661f | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 485 | return 0; |
| 486 | ops = clk_dev_ops(clk->dev); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 487 | |
| 488 | if (!ops->get_rate) |
| 489 | return -ENOSYS; |
| 490 | |
Simon Glass | 5c5992c | 2021-01-21 13:57:11 -0700 | [diff] [blame] | 491 | ret = ops->get_rate(clk); |
| 492 | if (ret) |
| 493 | return log_ret(ret); |
| 494 | |
| 495 | return 0; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 496 | } |
| 497 | |
Lukasz Majewski | 0c660c2 | 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); |
Chunfeng Yun | bd7c798 | 2020-01-09 11:35:06 +0800 | [diff] [blame] | 504 | if (!clk_valid(clk)) |
Jean-Jacques Hiblot | 8a1661f | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 505 | return NULL; |
Lukasz Majewski | 0c660c2 | 2019-06-24 15:50:42 +0200 | [diff] [blame] | 506 | |
| 507 | pdev = dev_get_parent(clk->dev); |
Tero Kristo | 920ea5a | 2021-06-11 11:45:08 +0300 | [diff] [blame] | 508 | if (!pdev) |
| 509 | return ERR_PTR(-ENODEV); |
Lukasz Majewski | 0c660c2 | 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 | a1265cd | 2022-09-28 12:37:57 +0200 | [diff] [blame] | 517 | ulong clk_get_parent_rate(struct clk *clk) |
Lukasz Majewski | 4aa7830 | 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); |
Chunfeng Yun | bd7c798 | 2020-01-09 11:35:06 +0800 | [diff] [blame] | 523 | if (!clk_valid(clk)) |
Jean-Jacques Hiblot | 8a1661f | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 524 | return 0; |
Lukasz Majewski | 4aa7830 | 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 | 1a961c9 | 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 | 4aa7830 | 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 | 2983ad5 | 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 | 19fb40a | 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 | 6b7fd31 | 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) { |
| 580 | clkp = dev_get_clk_ptr(child_dev); |
| 581 | clk_clean_rate_cache(clkp); |
| 582 | } |
| 583 | } |
| 584 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 585 | ulong clk_set_rate(struct clk *clk, ulong rate) |
| 586 | { |
Jean-Jacques Hiblot | 8a1661f | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 587 | const struct clk_ops *ops; |
Patrick Delaunay | 19fb40a | 2022-06-20 15:37:25 +0200 | [diff] [blame] | 588 | struct clk *clkp; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 589 | |
| 590 | debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate); |
Chunfeng Yun | bd7c798 | 2020-01-09 11:35:06 +0800 | [diff] [blame] | 591 | if (!clk_valid(clk)) |
Jean-Jacques Hiblot | 8a1661f | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 592 | return 0; |
| 593 | ops = clk_dev_ops(clk->dev); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 594 | |
| 595 | if (!ops->set_rate) |
| 596 | return -ENOSYS; |
| 597 | |
Patrick Delaunay | 19fb40a | 2022-06-20 15:37:25 +0200 | [diff] [blame] | 598 | /* get private clock struct used for cache */ |
| 599 | clk_get_priv(clk, &clkp); |
Tero Kristo | 6b7fd31 | 2021-06-11 11:45:12 +0300 | [diff] [blame] | 600 | /* Clean up cached rates for us and all child clocks */ |
Patrick Delaunay | 19fb40a | 2022-06-20 15:37:25 +0200 | [diff] [blame] | 601 | clk_clean_rate_cache(clkp); |
Tero Kristo | 6b7fd31 | 2021-06-11 11:45:12 +0300 | [diff] [blame] | 602 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 603 | return ops->set_rate(clk, rate); |
| 604 | } |
| 605 | |
Philipp Tomsich | f7d1046 | 2018-01-08 11:15:08 +0100 | [diff] [blame] | 606 | int clk_set_parent(struct clk *clk, struct clk *parent) |
| 607 | { |
Jean-Jacques Hiblot | 8a1661f | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 608 | const struct clk_ops *ops; |
Claudiu Beznea | 4d139f3 | 2020-09-07 17:46:34 +0300 | [diff] [blame] | 609 | int ret; |
Philipp Tomsich | f7d1046 | 2018-01-08 11:15:08 +0100 | [diff] [blame] | 610 | |
| 611 | debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent); |
Chunfeng Yun | bd7c798 | 2020-01-09 11:35:06 +0800 | [diff] [blame] | 612 | if (!clk_valid(clk)) |
Jean-Jacques Hiblot | 8a1661f | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 613 | return 0; |
| 614 | ops = clk_dev_ops(clk->dev); |
Philipp Tomsich | f7d1046 | 2018-01-08 11:15:08 +0100 | [diff] [blame] | 615 | |
| 616 | if (!ops->set_parent) |
| 617 | return -ENOSYS; |
| 618 | |
Claudiu Beznea | 4d139f3 | 2020-09-07 17:46:34 +0300 | [diff] [blame] | 619 | ret = ops->set_parent(clk, parent); |
| 620 | if (ret) |
| 621 | return ret; |
| 622 | |
| 623 | if (CONFIG_IS_ENABLED(CLK_CCF)) |
| 624 | ret = device_reparent(clk->dev, parent->dev); |
| 625 | |
| 626 | return ret; |
Philipp Tomsich | f7d1046 | 2018-01-08 11:15:08 +0100 | [diff] [blame] | 627 | } |
| 628 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 629 | int clk_enable(struct clk *clk) |
| 630 | { |
Jean-Jacques Hiblot | 8a1661f | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 631 | const struct clk_ops *ops; |
Peng Fan | 0520be0 | 2019-08-21 13:35:09 +0000 | [diff] [blame] | 632 | struct clk *clkp = NULL; |
| 633 | int ret; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 634 | |
| 635 | debug("%s(clk=%p)\n", __func__, clk); |
Chunfeng Yun | bd7c798 | 2020-01-09 11:35:06 +0800 | [diff] [blame] | 636 | if (!clk_valid(clk)) |
Jean-Jacques Hiblot | 8a1661f | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 637 | return 0; |
| 638 | ops = clk_dev_ops(clk->dev); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 639 | |
Peng Fan | 0520be0 | 2019-08-21 13:35:09 +0000 | [diff] [blame] | 640 | if (CONFIG_IS_ENABLED(CLK_CCF)) { |
| 641 | /* Take id 0 as a non-valid clk, such as dummy */ |
| 642 | if (clk->id && !clk_get_by_id(clk->id, &clkp)) { |
Yang Xiwen | 3fb2d3d | 2023-11-19 06:10:06 +0800 | [diff] [blame^] | 643 | ops = clk_dev_ops(clkp->dev); |
Peng Fan | 0520be0 | 2019-08-21 13:35:09 +0000 | [diff] [blame] | 644 | if (clkp->enable_count) { |
| 645 | clkp->enable_count++; |
| 646 | return 0; |
| 647 | } |
| 648 | if (clkp->dev->parent && |
Patrick Delaunay | b0cdd82 | 2022-01-24 14:17:14 +0100 | [diff] [blame] | 649 | device_get_uclass_id(clkp->dev->parent) == UCLASS_CLK) { |
Peng Fan | 0520be0 | 2019-08-21 13:35:09 +0000 | [diff] [blame] | 650 | ret = clk_enable(dev_get_clk_ptr(clkp->dev->parent)); |
| 651 | if (ret) { |
| 652 | printf("Enable %s failed\n", |
| 653 | clkp->dev->parent->name); |
| 654 | return ret; |
| 655 | } |
| 656 | } |
| 657 | } |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 658 | |
Peng Fan | 0520be0 | 2019-08-21 13:35:09 +0000 | [diff] [blame] | 659 | if (ops->enable) { |
Maksim Kiselev | 0755db4 | 2023-09-06 01:16:49 +0300 | [diff] [blame] | 660 | ret = ops->enable(clkp ? clkp : clk); |
Peng Fan | 0520be0 | 2019-08-21 13:35:09 +0000 | [diff] [blame] | 661 | if (ret) { |
| 662 | printf("Enable %s failed\n", clk->dev->name); |
| 663 | return ret; |
| 664 | } |
| 665 | } |
| 666 | if (clkp) |
| 667 | clkp->enable_count++; |
| 668 | } else { |
| 669 | if (!ops->enable) |
| 670 | return -ENOSYS; |
| 671 | return ops->enable(clk); |
| 672 | } |
| 673 | |
| 674 | return 0; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 675 | } |
| 676 | |
Neil Armstrong | a855be8 | 2018-04-03 11:44:18 +0200 | [diff] [blame] | 677 | int clk_enable_bulk(struct clk_bulk *bulk) |
| 678 | { |
| 679 | int i, ret; |
| 680 | |
| 681 | for (i = 0; i < bulk->count; i++) { |
| 682 | ret = clk_enable(&bulk->clks[i]); |
| 683 | if (ret < 0 && ret != -ENOSYS) |
| 684 | return ret; |
| 685 | } |
| 686 | |
| 687 | return 0; |
| 688 | } |
| 689 | |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 690 | int clk_disable(struct clk *clk) |
| 691 | { |
Jean-Jacques Hiblot | 8a1661f | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 692 | const struct clk_ops *ops; |
Peng Fan | 0520be0 | 2019-08-21 13:35:09 +0000 | [diff] [blame] | 693 | struct clk *clkp = NULL; |
| 694 | int ret; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 695 | |
| 696 | debug("%s(clk=%p)\n", __func__, clk); |
Chunfeng Yun | bd7c798 | 2020-01-09 11:35:06 +0800 | [diff] [blame] | 697 | if (!clk_valid(clk)) |
Jean-Jacques Hiblot | 8a1661f | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 698 | return 0; |
| 699 | ops = clk_dev_ops(clk->dev); |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 700 | |
Peng Fan | 0520be0 | 2019-08-21 13:35:09 +0000 | [diff] [blame] | 701 | if (CONFIG_IS_ENABLED(CLK_CCF)) { |
| 702 | if (clk->id && !clk_get_by_id(clk->id, &clkp)) { |
Yang Xiwen | 3fb2d3d | 2023-11-19 06:10:06 +0800 | [diff] [blame^] | 703 | ops = clk_dev_ops(clkp->dev); |
Claudiu Beznea | 9a5d59d | 2020-09-07 17:46:35 +0300 | [diff] [blame] | 704 | if (clkp->flags & CLK_IS_CRITICAL) |
| 705 | return 0; |
| 706 | |
Peng Fan | 0520be0 | 2019-08-21 13:35:09 +0000 | [diff] [blame] | 707 | if (clkp->enable_count == 0) { |
| 708 | printf("clk %s already disabled\n", |
| 709 | clkp->dev->name); |
| 710 | return 0; |
| 711 | } |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 712 | |
Peng Fan | 0520be0 | 2019-08-21 13:35:09 +0000 | [diff] [blame] | 713 | if (--clkp->enable_count > 0) |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | if (ops->disable) { |
Maksim Kiselev | 0755db4 | 2023-09-06 01:16:49 +0300 | [diff] [blame] | 718 | ret = ops->disable(clkp ? clkp : clk); |
Peng Fan | 0520be0 | 2019-08-21 13:35:09 +0000 | [diff] [blame] | 719 | if (ret) |
| 720 | return ret; |
| 721 | } |
| 722 | |
| 723 | if (clkp && clkp->dev->parent && |
Patrick Delaunay | b0cdd82 | 2022-01-24 14:17:14 +0100 | [diff] [blame] | 724 | device_get_uclass_id(clkp->dev->parent) == UCLASS_CLK) { |
Peng Fan | 0520be0 | 2019-08-21 13:35:09 +0000 | [diff] [blame] | 725 | ret = clk_disable(dev_get_clk_ptr(clkp->dev->parent)); |
| 726 | if (ret) { |
| 727 | printf("Disable %s failed\n", |
| 728 | clkp->dev->parent->name); |
| 729 | return ret; |
| 730 | } |
| 731 | } |
| 732 | } else { |
| 733 | if (!ops->disable) |
| 734 | return -ENOSYS; |
| 735 | |
| 736 | return ops->disable(clk); |
| 737 | } |
| 738 | |
| 739 | return 0; |
Stephen Warren | 135aa95 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 740 | } |
Simon Glass | e70cc43 | 2016-01-20 19:43:02 -0700 | [diff] [blame] | 741 | |
Neil Armstrong | a855be8 | 2018-04-03 11:44:18 +0200 | [diff] [blame] | 742 | int clk_disable_bulk(struct clk_bulk *bulk) |
| 743 | { |
| 744 | int i, ret; |
| 745 | |
| 746 | for (i = 0; i < bulk->count; i++) { |
| 747 | ret = clk_disable(&bulk->clks[i]); |
| 748 | if (ret < 0 && ret != -ENOSYS) |
| 749 | return ret; |
| 750 | } |
| 751 | |
| 752 | return 0; |
| 753 | } |
| 754 | |
Lukasz Majewski | 2796af7 | 2019-06-24 15:50:44 +0200 | [diff] [blame] | 755 | int clk_get_by_id(ulong id, struct clk **clkp) |
| 756 | { |
| 757 | struct udevice *dev; |
| 758 | struct uclass *uc; |
| 759 | int ret; |
| 760 | |
| 761 | ret = uclass_get(UCLASS_CLK, &uc); |
| 762 | if (ret) |
| 763 | return ret; |
| 764 | |
| 765 | uclass_foreach_dev(dev, uc) { |
| 766 | struct clk *clk = dev_get_clk_ptr(dev); |
| 767 | |
| 768 | if (clk && clk->id == id) { |
| 769 | *clkp = clk; |
| 770 | return 0; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | return -ENOENT; |
| 775 | } |
| 776 | |
Sekhar Nori | acbb7cd | 2019-08-01 19:12:55 +0530 | [diff] [blame] | 777 | bool clk_is_match(const struct clk *p, const struct clk *q) |
| 778 | { |
| 779 | /* trivial case: identical struct clk's or both NULL */ |
| 780 | if (p == q) |
| 781 | return true; |
| 782 | |
Jean-Jacques Hiblot | 8a1661f | 2019-10-22 14:00:03 +0200 | [diff] [blame] | 783 | /* trivial case #2: on the clk pointer is NULL */ |
| 784 | if (!p || !q) |
| 785 | return false; |
| 786 | |
Sekhar Nori | acbb7cd | 2019-08-01 19:12:55 +0530 | [diff] [blame] | 787 | /* same device, id and data */ |
| 788 | if (p->dev == q->dev && p->id == q->id && p->data == q->data) |
| 789 | return true; |
| 790 | |
| 791 | return false; |
| 792 | } |
| 793 | |
Jean-Jacques Hiblot | 52720c5 | 2019-10-22 14:00:04 +0200 | [diff] [blame] | 794 | static void devm_clk_release(struct udevice *dev, void *res) |
| 795 | { |
| 796 | clk_free(res); |
| 797 | } |
| 798 | |
| 799 | static int devm_clk_match(struct udevice *dev, void *res, void *data) |
| 800 | { |
| 801 | return res == data; |
| 802 | } |
| 803 | |
| 804 | struct clk *devm_clk_get(struct udevice *dev, const char *id) |
| 805 | { |
| 806 | int rc; |
| 807 | struct clk *clk; |
| 808 | |
| 809 | clk = devres_alloc(devm_clk_release, sizeof(struct clk), __GFP_ZERO); |
| 810 | if (unlikely(!clk)) |
| 811 | return ERR_PTR(-ENOMEM); |
| 812 | |
| 813 | rc = clk_get_by_name(dev, id, clk); |
| 814 | if (rc) |
| 815 | return ERR_PTR(rc); |
| 816 | |
| 817 | devres_add(dev, clk); |
| 818 | return clk; |
| 819 | } |
| 820 | |
Jean-Jacques Hiblot | 52720c5 | 2019-10-22 14:00:04 +0200 | [diff] [blame] | 821 | void devm_clk_put(struct udevice *dev, struct clk *clk) |
| 822 | { |
| 823 | int rc; |
| 824 | |
| 825 | if (!clk) |
| 826 | return; |
| 827 | |
| 828 | rc = devres_release(dev, devm_clk_release, devm_clk_match, clk); |
| 829 | WARN_ON(rc); |
| 830 | } |
| 831 | |
Jean-Jacques Hiblot | fd1ba29 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 832 | int clk_uclass_post_probe(struct udevice *dev) |
| 833 | { |
| 834 | /* |
| 835 | * when a clock provider is probed. Call clk_set_defaults() |
| 836 | * also after the device is probed. This takes care of cases |
| 837 | * where the DT is used to setup default parents and rates |
| 838 | * using assigned-clocks |
| 839 | */ |
Marek Vasut | 75f080d | 2022-01-01 19:51:39 +0100 | [diff] [blame] | 840 | clk_set_defaults(dev, CLK_DEFAULTS_POST); |
Jean-Jacques Hiblot | fd1ba29 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 841 | |
| 842 | return 0; |
| 843 | } |
| 844 | |
Simon Glass | f26c8a8 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 845 | UCLASS_DRIVER(clk) = { |
| 846 | .id = UCLASS_CLK, |
| 847 | .name = "clk", |
Jean-Jacques Hiblot | fd1ba29 | 2019-10-22 14:00:06 +0200 | [diff] [blame] | 848 | .post_probe = clk_uclass_post_probe, |
Simon Glass | f26c8a8 | 2015-06-23 15:39:15 -0600 | [diff] [blame] | 849 | }; |