1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
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 <dm/device_compat.h>
20 #include <dm/device-internal.h>
21 #include <dm/devres.h>
23 #include <linux/bug.h>
24 #include <linux/clk-provider.h>
25 #include <linux/err.h>
26 #include <asm/global_data.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_CONTROL)
39 # if CONFIG_IS_ENABLED(OF_PLATDATA)
40 int clk_get_by_driver_info(struct udevice *dev, struct phandle_1_arg *cells,
45 ret = device_get_by_ofplat_idx(cells->idx, &clk->dev);
48 clk->id = cells->arg[0];
53 static int clk_of_xlate_default(struct clk *clk,
54 struct ofnode_phandle_args *args)
56 debug("%s(clk=%p)\n", __func__, clk);
58 if (args->args_count > 1) {
59 debug("Invaild args_count: %d\n", args->args_count);
64 clk->id = args->args[0];
73 static int clk_get_by_index_tail(int ret, ofnode node,
74 struct ofnode_phandle_args *args,
75 const char *list_name, int index,
78 struct udevice *dev_clk;
79 const struct clk_ops *ops;
86 ret = uclass_get_device_by_ofnode(UCLASS_CLK, args->node, &dev_clk);
88 debug("%s: uclass_get_device_by_of_offset failed: err=%d\n",
90 return log_msg_ret("get", ret);
95 ops = clk_dev_ops(dev_clk);
98 ret = ops->of_xlate(clk, args);
100 ret = clk_of_xlate_default(clk, args);
102 debug("of_xlate() failed: %d\n", ret);
103 return log_msg_ret("xlate", ret);
106 return clk_request(dev_clk, clk);
108 debug("%s: Node '%s', property '%s', failed to request CLK index %d: %d\n",
109 __func__, ofnode_get_name(node), list_name, index, ret);
111 return log_msg_ret("prop", ret);
114 static int clk_get_by_indexed_prop(struct udevice *dev, const char *prop_name,
115 int index, struct clk *clk)
118 struct ofnode_phandle_args args;
120 debug("%s(dev=%p, index=%d, clk=%p)\n", __func__, dev, index, clk);
125 ret = dev_read_phandle_with_args(dev, prop_name, "#clock-cells", 0,
128 debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
134 return clk_get_by_index_tail(ret, dev_ofnode(dev), &args, "clocks",
138 int clk_get_by_index(struct udevice *dev, int index, struct clk *clk)
140 struct ofnode_phandle_args args;
143 ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0,
146 return clk_get_by_index_tail(ret, dev_ofnode(dev), &args, "clocks",
150 int clk_get_by_index_nodev(ofnode node, int index, struct clk *clk)
152 struct ofnode_phandle_args args;
155 ret = ofnode_parse_phandle_with_args(node, "clocks", "#clock-cells", 0,
158 return clk_get_by_index_tail(ret, node, &args, "clocks",
162 int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
164 int i, ret, err, count;
168 count = dev_count_phandle_with_args(dev, "clocks", "#clock-cells", 0);
172 bulk->clks = devm_kcalloc(dev, count, sizeof(struct clk), GFP_KERNEL);
176 for (i = 0; i < count; i++) {
177 ret = clk_get_by_index(dev, i, &bulk->clks[i]);
187 err = clk_release_all(bulk->clks, bulk->count);
189 debug("%s: could release all clocks for %p\n",
195 static struct clk *clk_set_default_get_by_id(struct clk *clk)
199 if (CONFIG_IS_ENABLED(CLK_CCF)) {
200 int ret = clk_get_by_id(clk->id, &c);
203 debug("%s(): could not get parent clock pointer, id %lu\n",
212 static int clk_set_default_parents(struct udevice *dev,
213 enum clk_defaults_stage stage)
215 struct clk clk, parent_clk, *c, *p;
220 num_parents = dev_count_phandle_with_args(dev, "assigned-clock-parents",
222 if (num_parents < 0) {
223 debug("%s: could not read assigned-clock-parents for %p\n",
228 for (index = 0; index < num_parents; index++) {
229 ret = clk_get_by_indexed_prop(dev, "assigned-clock-parents",
231 /* If -ENOENT, this is a no-op entry */
236 debug("%s: could not get parent clock %d for %s\n",
237 __func__, index, dev_read_name(dev));
241 p = clk_set_default_get_by_id(&parent_clk);
245 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
248 * If the clock provider is not ready yet, let it handle
249 * the re-programming later.
251 if (ret == -EPROBE_DEFER) {
257 debug("%s: could not get assigned clock %d for %s\n",
258 __func__, index, dev_read_name(dev));
262 /* This is clk provider device trying to reparent itself
263 * It cannot be done right now but need to wait after the
266 if (stage == CLK_DEFAULTS_PRE && clk.dev == dev)
269 if (stage != CLK_DEFAULTS_PRE && clk.dev != dev)
270 /* do not setup twice the parent clocks */
273 c = clk_set_default_get_by_id(&clk);
277 ret = clk_set_parent(c, p);
279 * Not all drivers may support clock-reparenting (as of now).
280 * Ignore errors due to this.
286 debug("%s: failed to reparent clock %d for %s\n",
287 __func__, index, dev_read_name(dev));
295 static int clk_set_default_rates(struct udevice *dev,
296 enum clk_defaults_stage stage)
305 size = dev_read_size(dev, "assigned-clock-rates");
309 num_rates = size / sizeof(u32);
310 rates = calloc(num_rates, sizeof(u32));
314 ret = dev_read_u32_array(dev, "assigned-clock-rates", rates, num_rates);
318 for (index = 0; index < num_rates; index++) {
319 /* If 0 is passed, this is a no-op */
323 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
326 * If the clock provider is not ready yet, let it handle
327 * the re-programming later.
329 if (ret == -EPROBE_DEFER) {
336 "could not get assigned clock %d (err = %d)\n",
341 /* This is clk provider device trying to program itself
342 * It cannot be done right now but need to wait after the
345 if (stage == CLK_DEFAULTS_PRE && clk.dev == dev)
348 if (stage != CLK_DEFAULTS_PRE && clk.dev != dev)
349 /* do not setup twice the parent clocks */
352 c = clk_set_default_get_by_id(&clk);
356 ret = clk_set_rate(c, rates[index]);
360 "failed to set rate on clock index %d (%ld) (error = %d)\n",
371 int clk_set_defaults(struct udevice *dev, enum clk_defaults_stage stage)
375 if (!dev_has_ofnode(dev))
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
383 if (!(IS_ENABLED(CONFIG_SPL_BUILD) || (gd->flags & GD_FLG_RELOC)))
384 if (stage != CLK_DEFAULTS_POST_FORCE)
387 debug("%s(%s)\n", __func__, dev_read_name(dev));
389 ret = clk_set_default_parents(dev, stage);
393 ret = clk_set_default_rates(dev, stage);
400 int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk)
404 debug("%s(dev=%p, name=%s, clk=%p)\n", __func__, dev, name, clk);
407 index = dev_read_stringlist_search(dev, "clock-names", name);
409 debug("fdt_stringlist_search() failed: %d\n", index);
413 return clk_get_by_index(dev, index, clk);
415 # endif /* OF_PLATDATA */
417 int clk_get_by_name_nodev(ofnode node, const char *name, struct clk *clk)
421 debug("%s(node=%p, name=%s, clk=%p)\n", __func__,
422 ofnode_get_name(node), name, clk);
425 index = ofnode_stringlist_search(node, "clock-names", name);
427 debug("fdt_stringlist_search() failed: %d\n", index);
431 return clk_get_by_index_nodev(node, index, clk);
434 int clk_get_optional_nodev(ofnode node, const char *name, struct clk *clk)
438 ret = clk_get_by_name_nodev(node, name, clk);
445 int clk_release_all(struct clk *clk, int count)
449 for (i = 0; i < count; i++) {
450 debug("%s(clk[%d]=%p)\n", __func__, i, &clk[i]);
452 /* check if clock has been previously requested */
456 ret = clk_disable(&clk[i]);
457 if (ret && ret != -ENOSYS)
460 ret = clk_free(&clk[i]);
461 if (ret && ret != -ENOSYS)
468 #endif /* OF_CONTROL */
470 int clk_request(struct udevice *dev, struct clk *clk)
472 const struct clk_ops *ops;
474 debug("%s(dev=%p, clk=%p)\n", __func__, dev, clk);
477 ops = clk_dev_ops(dev);
484 return ops->request(clk);
487 int clk_free(struct clk *clk)
489 const struct clk_ops *ops;
491 debug("%s(clk=%p)\n", __func__, clk);
494 ops = clk_dev_ops(clk->dev);
499 return ops->rfree(clk);
502 ulong clk_get_rate(struct clk *clk)
504 const struct clk_ops *ops;
507 debug("%s(clk=%p)\n", __func__, clk);
510 ops = clk_dev_ops(clk->dev);
515 ret = ops->get_rate(clk);
522 struct clk *clk_get_parent(struct clk *clk)
524 struct udevice *pdev;
527 debug("%s(clk=%p)\n", __func__, clk);
531 pdev = dev_get_parent(clk->dev);
533 return ERR_PTR(-ENODEV);
534 pclk = dev_get_clk_ptr(pdev);
536 return ERR_PTR(-ENODEV);
541 long long clk_get_parent_rate(struct clk *clk)
543 const struct clk_ops *ops;
546 debug("%s(clk=%p)\n", __func__, clk);
550 pclk = clk_get_parent(clk);
554 ops = clk_dev_ops(pclk->dev);
558 /* Read the 'rate' if not already set or if proper flag set*/
559 if (!pclk->rate || pclk->flags & CLK_GET_RATE_NOCACHE)
560 pclk->rate = clk_get_rate(pclk);
565 ulong clk_round_rate(struct clk *clk, ulong rate)
567 const struct clk_ops *ops;
569 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
573 ops = clk_dev_ops(clk->dev);
574 if (!ops->round_rate)
577 return ops->round_rate(clk, rate);
580 static void clk_clean_rate_cache(struct clk *clk)
582 struct udevice *child_dev;
590 list_for_each_entry(child_dev, &clk->dev->child_head, sibling_node) {
591 clkp = dev_get_clk_ptr(child_dev);
592 clk_clean_rate_cache(clkp);
596 ulong clk_set_rate(struct clk *clk, ulong rate)
598 const struct clk_ops *ops;
600 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
603 ops = clk_dev_ops(clk->dev);
608 /* Clean up cached rates for us and all child clocks */
609 clk_clean_rate_cache(clk);
611 return ops->set_rate(clk, rate);
614 int clk_set_parent(struct clk *clk, struct clk *parent)
616 const struct clk_ops *ops;
619 debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
622 ops = clk_dev_ops(clk->dev);
624 if (!ops->set_parent)
627 ret = ops->set_parent(clk, parent);
631 if (CONFIG_IS_ENABLED(CLK_CCF))
632 ret = device_reparent(clk->dev, parent->dev);
637 int clk_enable(struct clk *clk)
639 const struct clk_ops *ops;
640 struct clk *clkp = NULL;
643 debug("%s(clk=%p)\n", __func__, clk);
646 ops = clk_dev_ops(clk->dev);
648 if (CONFIG_IS_ENABLED(CLK_CCF)) {
649 /* Take id 0 as a non-valid clk, such as dummy */
650 if (clk->id && !clk_get_by_id(clk->id, &clkp)) {
651 if (clkp->enable_count) {
652 clkp->enable_count++;
655 if (clkp->dev->parent &&
656 device_get_uclass_id(clkp->dev) == UCLASS_CLK) {
657 ret = clk_enable(dev_get_clk_ptr(clkp->dev->parent));
659 printf("Enable %s failed\n",
660 clkp->dev->parent->name);
667 ret = ops->enable(clk);
669 printf("Enable %s failed\n", clk->dev->name);
674 clkp->enable_count++;
678 return ops->enable(clk);
684 int clk_enable_bulk(struct clk_bulk *bulk)
688 for (i = 0; i < bulk->count; i++) {
689 ret = clk_enable(&bulk->clks[i]);
690 if (ret < 0 && ret != -ENOSYS)
697 int clk_disable(struct clk *clk)
699 const struct clk_ops *ops;
700 struct clk *clkp = NULL;
703 debug("%s(clk=%p)\n", __func__, clk);
706 ops = clk_dev_ops(clk->dev);
708 if (CONFIG_IS_ENABLED(CLK_CCF)) {
709 if (clk->id && !clk_get_by_id(clk->id, &clkp)) {
710 if (clkp->flags & CLK_IS_CRITICAL)
713 if (clkp->enable_count == 0) {
714 printf("clk %s already disabled\n",
719 if (--clkp->enable_count > 0)
724 ret = ops->disable(clk);
729 if (clkp && clkp->dev->parent &&
730 device_get_uclass_id(clkp->dev) == UCLASS_CLK) {
731 ret = clk_disable(dev_get_clk_ptr(clkp->dev->parent));
733 printf("Disable %s failed\n",
734 clkp->dev->parent->name);
742 return ops->disable(clk);
748 int clk_disable_bulk(struct clk_bulk *bulk)
752 for (i = 0; i < bulk->count; i++) {
753 ret = clk_disable(&bulk->clks[i]);
754 if (ret < 0 && ret != -ENOSYS)
761 int clk_get_by_id(ulong id, struct clk **clkp)
767 ret = uclass_get(UCLASS_CLK, &uc);
771 uclass_foreach_dev(dev, uc) {
772 struct clk *clk = dev_get_clk_ptr(dev);
774 if (clk && clk->id == id) {
783 bool clk_is_match(const struct clk *p, const struct clk *q)
785 /* trivial case: identical struct clk's or both NULL */
789 /* trivial case #2: on the clk pointer is NULL */
793 /* same device, id and data */
794 if (p->dev == q->dev && p->id == q->id && p->data == q->data)
800 static void devm_clk_release(struct udevice *dev, void *res)
805 static int devm_clk_match(struct udevice *dev, void *res, void *data)
810 struct clk *devm_clk_get(struct udevice *dev, const char *id)
815 clk = devres_alloc(devm_clk_release, sizeof(struct clk), __GFP_ZERO);
817 return ERR_PTR(-ENOMEM);
819 rc = clk_get_by_name(dev, id, clk);
823 devres_add(dev, clk);
827 struct clk *devm_clk_get_optional(struct udevice *dev, const char *id)
829 struct clk *clk = devm_clk_get(dev, id);
831 if (PTR_ERR(clk) == -ENODATA)
837 void devm_clk_put(struct udevice *dev, struct clk *clk)
844 rc = devres_release(dev, devm_clk_release, devm_clk_match, clk);
848 int clk_uclass_post_probe(struct udevice *dev)
851 * when a clock provider is probed. Call clk_set_defaults()
852 * also after the device is probed. This takes care of cases
853 * where the DT is used to setup default parents and rates
854 * using assigned-clocks
856 clk_set_defaults(dev, CLK_DEFAULTS_POST);
861 UCLASS_DRIVER(clk) = {
864 .post_probe = clk_uclass_post_probe,