1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
5 * Copyright (c) 2016, NVIDIA CORPORATION.
6 * Copyright (c) 2018, Theobroma Systems Design und Consulting GmbH
9 #define LOG_CATEGORY UCLASS_CLK
13 #include <clk-uclass.h>
15 #include <dt-structs.h>
19 #include <asm/global_data.h>
20 #include <dm/device_compat.h>
21 #include <dm/device-internal.h>
22 #include <dm/devres.h>
24 #include <linux/bug.h>
25 #include <linux/clk-provider.h>
26 #include <linux/err.h>
28 static inline const struct clk_ops *clk_dev_ops(struct udevice *dev)
30 return (const struct clk_ops *)dev->driver->ops;
33 struct clk *dev_get_clk_ptr(struct udevice *dev)
35 return (struct clk *)dev_get_uclass_priv(dev);
38 #if CONFIG_IS_ENABLED(OF_PLATDATA)
39 int clk_get_by_phandle(struct udevice *dev, const struct phandle_1_arg *cells,
44 ret = device_get_by_ofplat_idx(cells->idx, &clk->dev);
47 clk->id = cells->arg[0];
53 #if CONFIG_IS_ENABLED(OF_REAL)
54 static int clk_of_xlate_default(struct clk *clk,
55 struct ofnode_phandle_args *args)
57 debug("%s(clk=%p)\n", __func__, clk);
59 if (args->args_count > 1) {
60 debug("Invalid args_count: %d\n", args->args_count);
65 clk->id = args->args[0];
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,
79 struct udevice *dev_clk;
80 const struct clk_ops *ops;
87 ret = uclass_get_device_by_ofnode(UCLASS_CLK, args->node, &dev_clk);
89 debug("%s: uclass_get_device_by_of_offset failed: err=%d\n",
91 return log_msg_ret("get", ret);
96 ops = clk_dev_ops(dev_clk);
99 ret = ops->of_xlate(clk, args);
101 ret = clk_of_xlate_default(clk, args);
103 debug("of_xlate() failed: %d\n", ret);
104 return log_msg_ret("xlate", ret);
107 return clk_request(dev_clk, clk);
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);
112 return log_msg_ret("prop", ret);
115 static int clk_get_by_indexed_prop(struct udevice *dev, const char *prop_name,
116 int index, struct clk *clk)
119 struct ofnode_phandle_args args;
121 debug("%s(dev=%p, index=%d, clk=%p)\n", __func__, dev, index, clk);
126 ret = dev_read_phandle_with_args(dev, prop_name, "#clock-cells", 0,
129 debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
135 return clk_get_by_index_tail(ret, dev_ofnode(dev), &args, "clocks",
139 int clk_get_by_index(struct udevice *dev, int index, struct clk *clk)
141 struct ofnode_phandle_args args;
144 ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0,
147 return clk_get_by_index_tail(ret, dev_ofnode(dev), &args, "clocks",
151 int clk_get_by_index_nodev(ofnode node, int index, struct clk *clk)
153 struct ofnode_phandle_args args;
156 ret = ofnode_parse_phandle_with_args(node, "clocks", "#clock-cells", 0,
159 return clk_get_by_index_tail(ret, node, &args, "clocks",
163 int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
165 int i, ret, err, count;
169 count = dev_count_phandle_with_args(dev, "clocks", "#clock-cells", 0);
173 bulk->clks = devm_kcalloc(dev, count, sizeof(struct clk), GFP_KERNEL);
177 for (i = 0; i < count; i++) {
178 ret = clk_get_by_index(dev, i, &bulk->clks[i]);
188 err = clk_release_all(bulk->clks, bulk->count);
190 debug("%s: could release all clocks for %p\n",
196 static struct clk *clk_set_default_get_by_id(struct clk *clk)
200 if (CONFIG_IS_ENABLED(CLK_CCF)) {
201 int ret = clk_get_by_id(clk->id, &c);
204 debug("%s(): could not get parent clock pointer, id %lu\n",
213 static int clk_set_default_parents(struct udevice *dev,
214 enum clk_defaults_stage stage)
216 struct clk clk, parent_clk, *c, *p;
221 num_parents = dev_count_phandle_with_args(dev, "assigned-clock-parents",
223 if (num_parents < 0) {
224 debug("%s: could not read assigned-clock-parents for %p\n",
229 for (index = 0; index < num_parents; index++) {
230 ret = clk_get_by_indexed_prop(dev, "assigned-clock-parents",
232 /* If -ENOENT, this is a no-op entry */
237 debug("%s: could not get parent clock %d for %s\n",
238 __func__, index, dev_read_name(dev));
242 p = clk_set_default_get_by_id(&parent_clk);
246 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
249 * If the clock provider is not ready yet, let it handle
250 * the re-programming later.
252 if (ret == -EPROBE_DEFER) {
258 debug("%s: could not get assigned clock %d for %s\n",
259 __func__, index, dev_read_name(dev));
263 /* This is clk provider device trying to reparent itself
264 * It cannot be done right now but need to wait after the
267 if (stage == CLK_DEFAULTS_PRE && clk.dev == dev)
270 if (stage != CLK_DEFAULTS_PRE && clk.dev != dev)
271 /* do not setup twice the parent clocks */
274 c = clk_set_default_get_by_id(&clk);
278 ret = clk_set_parent(c, p);
280 * Not all drivers may support clock-reparenting (as of now).
281 * Ignore errors due to this.
287 debug("%s: failed to reparent clock %d for %s\n",
288 __func__, index, dev_read_name(dev));
296 static int clk_set_default_rates(struct udevice *dev,
297 enum clk_defaults_stage stage)
306 size = dev_read_size(dev, "assigned-clock-rates");
310 num_rates = size / sizeof(u32);
311 rates = calloc(num_rates, sizeof(u32));
315 ret = dev_read_u32_array(dev, "assigned-clock-rates", rates, num_rates);
319 for (index = 0; index < num_rates; index++) {
320 /* If 0 is passed, this is a no-op */
324 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
327 * If the clock provider is not ready yet, let it handle
328 * the re-programming later.
330 if (ret == -EPROBE_DEFER) {
337 "could not get assigned clock %d (err = %d)\n",
342 /* This is clk provider device trying to program itself
343 * It cannot be done right now but need to wait after the
346 if (stage == CLK_DEFAULTS_PRE && clk.dev == dev)
349 if (stage != CLK_DEFAULTS_PRE && clk.dev != dev)
350 /* do not setup twice the parent clocks */
353 c = clk_set_default_get_by_id(&clk);
357 ret = clk_set_rate(c, rates[index]);
361 "failed to set rate on clock index %d (%ld) (error = %d)\n",
372 int clk_set_defaults(struct udevice *dev, enum clk_defaults_stage stage)
376 if (!dev_has_ofnode(dev))
380 * To avoid setting defaults twice, don't set them before relocation.
381 * However, still set them for SPL. And still set them if explicitly
384 if (!(IS_ENABLED(CONFIG_SPL_BUILD) || (gd->flags & GD_FLG_RELOC)))
385 if (stage != CLK_DEFAULTS_POST_FORCE)
388 debug("%s(%s)\n", __func__, dev_read_name(dev));
390 ret = clk_set_default_parents(dev, stage);
394 ret = clk_set_default_rates(dev, stage);
401 int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk)
405 debug("%s(dev=%p, name=%s, clk=%p)\n", __func__, dev, name, clk);
408 index = dev_read_stringlist_search(dev, "clock-names", name);
410 debug("fdt_stringlist_search() failed: %d\n", index);
414 return clk_get_by_index(dev, index, clk);
418 int clk_get_by_name_nodev(ofnode node, const char *name, struct clk *clk)
422 debug("%s(node=%p, name=%s, clk=%p)\n", __func__,
423 ofnode_get_name(node), name, clk);
426 index = ofnode_stringlist_search(node, "clock-names", name);
428 debug("fdt_stringlist_search() failed: %d\n", index);
432 return clk_get_by_index_nodev(node, index, clk);
435 int clk_release_all(struct clk *clk, int count)
439 for (i = 0; i < count; i++) {
440 debug("%s(clk[%d]=%p)\n", __func__, i, &clk[i]);
442 /* check if clock has been previously requested */
446 ret = clk_disable(&clk[i]);
447 if (ret && ret != -ENOSYS)
450 ret = clk_free(&clk[i]);
451 if (ret && ret != -ENOSYS)
458 int clk_request(struct udevice *dev, struct clk *clk)
460 const struct clk_ops *ops;
462 debug("%s(dev=%p, clk=%p)\n", __func__, dev, clk);
465 ops = clk_dev_ops(dev);
472 return ops->request(clk);
475 int clk_free(struct clk *clk)
477 const struct clk_ops *ops;
479 debug("%s(clk=%p)\n", __func__, clk);
482 ops = clk_dev_ops(clk->dev);
487 return ops->rfree(clk);
490 ulong clk_get_rate(struct clk *clk)
492 const struct clk_ops *ops;
495 debug("%s(clk=%p)\n", __func__, clk);
498 ops = clk_dev_ops(clk->dev);
503 ret = ops->get_rate(clk);
510 struct clk *clk_get_parent(struct clk *clk)
512 struct udevice *pdev;
515 debug("%s(clk=%p)\n", __func__, clk);
519 pdev = dev_get_parent(clk->dev);
521 return ERR_PTR(-ENODEV);
522 pclk = dev_get_clk_ptr(pdev);
524 return ERR_PTR(-ENODEV);
529 long long clk_get_parent_rate(struct clk *clk)
531 const struct clk_ops *ops;
534 debug("%s(clk=%p)\n", __func__, clk);
538 pclk = clk_get_parent(clk);
542 ops = clk_dev_ops(pclk->dev);
546 /* Read the 'rate' if not already set or if proper flag set*/
547 if (!pclk->rate || pclk->flags & CLK_GET_RATE_NOCACHE)
548 pclk->rate = clk_get_rate(pclk);
553 ulong clk_round_rate(struct clk *clk, ulong rate)
555 const struct clk_ops *ops;
557 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
561 ops = clk_dev_ops(clk->dev);
562 if (!ops->round_rate)
565 return ops->round_rate(clk, rate);
568 static void clk_clean_rate_cache(struct clk *clk)
570 struct udevice *child_dev;
578 list_for_each_entry(child_dev, &clk->dev->child_head, sibling_node) {
579 clkp = dev_get_clk_ptr(child_dev);
580 clk_clean_rate_cache(clkp);
584 ulong clk_set_rate(struct clk *clk, ulong rate)
586 const struct clk_ops *ops;
588 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
591 ops = clk_dev_ops(clk->dev);
596 /* Clean up cached rates for us and all child clocks */
597 clk_clean_rate_cache(clk);
599 return ops->set_rate(clk, rate);
602 int clk_set_parent(struct clk *clk, struct clk *parent)
604 const struct clk_ops *ops;
607 debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
610 ops = clk_dev_ops(clk->dev);
612 if (!ops->set_parent)
615 ret = ops->set_parent(clk, parent);
619 if (CONFIG_IS_ENABLED(CLK_CCF))
620 ret = device_reparent(clk->dev, parent->dev);
625 int clk_enable(struct clk *clk)
627 const struct clk_ops *ops;
628 struct clk *clkp = NULL;
631 debug("%s(clk=%p)\n", __func__, clk);
634 ops = clk_dev_ops(clk->dev);
636 if (CONFIG_IS_ENABLED(CLK_CCF)) {
637 /* Take id 0 as a non-valid clk, such as dummy */
638 if (clk->id && !clk_get_by_id(clk->id, &clkp)) {
639 if (clkp->enable_count) {
640 clkp->enable_count++;
643 if (clkp->dev->parent &&
644 device_get_uclass_id(clkp->dev->parent) == UCLASS_CLK) {
645 ret = clk_enable(dev_get_clk_ptr(clkp->dev->parent));
647 printf("Enable %s failed\n",
648 clkp->dev->parent->name);
655 ret = ops->enable(clk);
657 printf("Enable %s failed\n", clk->dev->name);
662 clkp->enable_count++;
666 return ops->enable(clk);
672 int clk_enable_bulk(struct clk_bulk *bulk)
676 for (i = 0; i < bulk->count; i++) {
677 ret = clk_enable(&bulk->clks[i]);
678 if (ret < 0 && ret != -ENOSYS)
685 int clk_disable(struct clk *clk)
687 const struct clk_ops *ops;
688 struct clk *clkp = NULL;
691 debug("%s(clk=%p)\n", __func__, clk);
694 ops = clk_dev_ops(clk->dev);
696 if (CONFIG_IS_ENABLED(CLK_CCF)) {
697 if (clk->id && !clk_get_by_id(clk->id, &clkp)) {
698 if (clkp->flags & CLK_IS_CRITICAL)
701 if (clkp->enable_count == 0) {
702 printf("clk %s already disabled\n",
707 if (--clkp->enable_count > 0)
712 ret = ops->disable(clk);
717 if (clkp && clkp->dev->parent &&
718 device_get_uclass_id(clkp->dev->parent) == UCLASS_CLK) {
719 ret = clk_disable(dev_get_clk_ptr(clkp->dev->parent));
721 printf("Disable %s failed\n",
722 clkp->dev->parent->name);
730 return ops->disable(clk);
736 int clk_disable_bulk(struct clk_bulk *bulk)
740 for (i = 0; i < bulk->count; i++) {
741 ret = clk_disable(&bulk->clks[i]);
742 if (ret < 0 && ret != -ENOSYS)
749 int clk_get_by_id(ulong id, struct clk **clkp)
755 ret = uclass_get(UCLASS_CLK, &uc);
759 uclass_foreach_dev(dev, uc) {
760 struct clk *clk = dev_get_clk_ptr(dev);
762 if (clk && clk->id == id) {
771 bool clk_is_match(const struct clk *p, const struct clk *q)
773 /* trivial case: identical struct clk's or both NULL */
777 /* trivial case #2: on the clk pointer is NULL */
781 /* same device, id and data */
782 if (p->dev == q->dev && p->id == q->id && p->data == q->data)
788 static void devm_clk_release(struct udevice *dev, void *res)
793 static int devm_clk_match(struct udevice *dev, void *res, void *data)
798 struct clk *devm_clk_get(struct udevice *dev, const char *id)
803 clk = devres_alloc(devm_clk_release, sizeof(struct clk), __GFP_ZERO);
805 return ERR_PTR(-ENOMEM);
807 rc = clk_get_by_name(dev, id, clk);
811 devres_add(dev, clk);
815 void devm_clk_put(struct udevice *dev, struct clk *clk)
822 rc = devres_release(dev, devm_clk_release, devm_clk_match, clk);
826 int clk_uclass_post_probe(struct udevice *dev)
829 * when a clock provider is probed. Call clk_set_defaults()
830 * also after the device is probed. This takes care of cases
831 * where the DT is used to setup default parents and rates
832 * using assigned-clocks
834 clk_set_defaults(dev, CLK_DEFAULTS_POST);
839 UCLASS_DRIVER(clk) = {
842 .post_probe = clk_uclass_post_probe,