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