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
11 #include <clk-uclass.h>
13 #include <dt-structs.h>
17 #include <dm/devres.h>
19 #include <linux/bug.h>
20 #include <linux/clk-provider.h>
21 #include <linux/err.h>
23 static inline const struct clk_ops *clk_dev_ops(struct udevice *dev)
25 return (const struct clk_ops *)dev->driver->ops;
28 struct clk *dev_get_clk_ptr(struct udevice *dev)
30 return (struct clk *)dev_get_uclass_priv(dev);
33 #if CONFIG_IS_ENABLED(OF_CONTROL)
34 # if CONFIG_IS_ENABLED(OF_PLATDATA)
35 int clk_get_by_driver_info(struct udevice *dev, struct phandle_1_arg *cells,
40 ret = device_get_by_driver_info((struct driver_info *)cells->node,
44 clk->id = cells->arg[0];
49 static int clk_of_xlate_default(struct clk *clk,
50 struct ofnode_phandle_args *args)
52 debug("%s(clk=%p)\n", __func__, clk);
54 if (args->args_count > 1) {
55 debug("Invaild args_count: %d\n", args->args_count);
60 clk->id = args->args[0];
69 static int clk_get_by_index_tail(int ret, ofnode node,
70 struct ofnode_phandle_args *args,
71 const char *list_name, int index,
74 struct udevice *dev_clk;
75 const struct clk_ops *ops;
82 ret = uclass_get_device_by_ofnode(UCLASS_CLK, args->node, &dev_clk);
84 debug("%s: uclass_get_device_by_of_offset failed: err=%d\n",
91 ops = clk_dev_ops(dev_clk);
94 ret = ops->of_xlate(clk, args);
96 ret = clk_of_xlate_default(clk, args);
98 debug("of_xlate() failed: %d\n", ret);
102 return clk_request(dev_clk, clk);
104 debug("%s: Node '%s', property '%s', failed to request CLK index %d: %d\n",
105 __func__, ofnode_get_name(node), list_name, index, ret);
109 static int clk_get_by_indexed_prop(struct udevice *dev, const char *prop_name,
110 int index, struct clk *clk)
113 struct ofnode_phandle_args args;
115 debug("%s(dev=%p, index=%d, clk=%p)\n", __func__, dev, index, clk);
120 ret = dev_read_phandle_with_args(dev, prop_name, "#clock-cells", 0,
123 debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
129 return clk_get_by_index_tail(ret, dev_ofnode(dev), &args, "clocks",
133 int clk_get_by_index(struct udevice *dev, int index, struct clk *clk)
135 struct ofnode_phandle_args args;
138 ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0,
141 return clk_get_by_index_tail(ret, dev_ofnode(dev), &args, "clocks",
145 int clk_get_by_index_nodev(ofnode node, int index, struct clk *clk)
147 struct ofnode_phandle_args args;
150 ret = ofnode_parse_phandle_with_args(node, "clocks", "#clock-cells", 0,
153 return clk_get_by_index_tail(ret, node, &args, "clocks",
157 int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
159 int i, ret, err, count;
163 count = dev_count_phandle_with_args(dev, "clocks", "#clock-cells");
167 bulk->clks = devm_kcalloc(dev, count, sizeof(struct clk), GFP_KERNEL);
171 for (i = 0; i < count; i++) {
172 ret = clk_get_by_index(dev, i, &bulk->clks[i]);
182 err = clk_release_all(bulk->clks, bulk->count);
184 debug("%s: could release all clocks for %p\n",
190 static int clk_set_default_parents(struct udevice *dev, int stage)
192 struct clk clk, parent_clk;
197 num_parents = dev_count_phandle_with_args(dev, "assigned-clock-parents",
199 if (num_parents < 0) {
200 debug("%s: could not read assigned-clock-parents for %p\n",
205 for (index = 0; index < num_parents; index++) {
206 ret = clk_get_by_indexed_prop(dev, "assigned-clock-parents",
208 /* If -ENOENT, this is a no-op entry */
213 debug("%s: could not get parent clock %d for %s\n",
214 __func__, index, dev_read_name(dev));
218 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
221 debug("%s: could not get assigned clock %d for %s\n",
222 __func__, index, dev_read_name(dev));
226 /* This is clk provider device trying to reparent itself
227 * It cannot be done right now but need to wait after the
230 if (stage == 0 && clk.dev == dev)
233 if (stage > 0 && clk.dev != dev)
234 /* do not setup twice the parent clocks */
237 ret = clk_set_parent(&clk, &parent_clk);
239 * Not all drivers may support clock-reparenting (as of now).
240 * Ignore errors due to this.
246 debug("%s: failed to reparent clock %d for %s\n",
247 __func__, index, dev_read_name(dev));
255 static int clk_set_default_rates(struct udevice *dev, int stage)
264 size = dev_read_size(dev, "assigned-clock-rates");
268 num_rates = size / sizeof(u32);
269 rates = calloc(num_rates, sizeof(u32));
273 ret = dev_read_u32_array(dev, "assigned-clock-rates", rates, num_rates);
277 for (index = 0; index < num_rates; index++) {
278 /* If 0 is passed, this is a no-op */
282 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
285 debug("%s: could not get assigned clock %d for %s\n",
286 __func__, index, dev_read_name(dev));
290 /* This is clk provider device trying to program itself
291 * It cannot be done right now but need to wait after the
294 if (stage == 0 && clk.dev == dev)
297 if (stage > 0 && clk.dev != dev)
298 /* do not setup twice the parent clocks */
301 ret = clk_set_rate(&clk, rates[index]);
304 debug("%s: failed to set rate on clock index %d (%ld) for %s\n",
305 __func__, index, clk.id, dev_read_name(dev));
315 int clk_set_defaults(struct udevice *dev, int stage)
319 if (!dev_of_valid(dev))
322 /* If this not in SPL and pre-reloc state, don't take any action. */
323 if (!(IS_ENABLED(CONFIG_SPL_BUILD) || (gd->flags & GD_FLG_RELOC)))
326 debug("%s(%s)\n", __func__, dev_read_name(dev));
328 ret = clk_set_default_parents(dev, stage);
332 ret = clk_set_default_rates(dev, stage);
339 int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk)
343 debug("%s(dev=%p, name=%s, clk=%p)\n", __func__, dev, name, clk);
346 index = dev_read_stringlist_search(dev, "clock-names", name);
348 debug("fdt_stringlist_search() failed: %d\n", index);
352 return clk_get_by_index(dev, index, clk);
354 # endif /* OF_PLATDATA */
356 int clk_get_by_name_nodev(ofnode node, const char *name, struct clk *clk)
360 debug("%s(node=%p, name=%s, clk=%p)\n", __func__,
361 ofnode_get_name(node), name, clk);
364 index = ofnode_stringlist_search(node, "clock-names", name);
366 debug("fdt_stringlist_search() failed: %d\n", index);
370 return clk_get_by_index_nodev(node, index, clk);
373 int clk_get_optional_nodev(ofnode node, const char *name, struct clk *clk)
377 ret = clk_get_by_name_nodev(node, name, clk);
384 int clk_release_all(struct clk *clk, int count)
388 for (i = 0; i < count; i++) {
389 debug("%s(clk[%d]=%p)\n", __func__, i, &clk[i]);
391 /* check if clock has been previously requested */
395 ret = clk_disable(&clk[i]);
396 if (ret && ret != -ENOSYS)
399 ret = clk_free(&clk[i]);
400 if (ret && ret != -ENOSYS)
407 #endif /* OF_CONTROL */
409 int clk_request(struct udevice *dev, struct clk *clk)
411 const struct clk_ops *ops;
413 debug("%s(dev=%p, clk=%p)\n", __func__, dev, clk);
416 ops = clk_dev_ops(dev);
423 return ops->request(clk);
426 int clk_free(struct clk *clk)
428 const struct clk_ops *ops;
430 debug("%s(clk=%p)\n", __func__, clk);
433 ops = clk_dev_ops(clk->dev);
438 return ops->rfree(clk);
441 ulong clk_get_rate(struct clk *clk)
443 const struct clk_ops *ops;
445 debug("%s(clk=%p)\n", __func__, clk);
448 ops = clk_dev_ops(clk->dev);
453 return ops->get_rate(clk);
456 struct clk *clk_get_parent(struct clk *clk)
458 struct udevice *pdev;
461 debug("%s(clk=%p)\n", __func__, clk);
465 pdev = dev_get_parent(clk->dev);
466 pclk = dev_get_clk_ptr(pdev);
468 return ERR_PTR(-ENODEV);
473 long long clk_get_parent_rate(struct clk *clk)
475 const struct clk_ops *ops;
478 debug("%s(clk=%p)\n", __func__, clk);
482 pclk = clk_get_parent(clk);
486 ops = clk_dev_ops(pclk->dev);
490 /* Read the 'rate' if not already set or if proper flag set*/
491 if (!pclk->rate || pclk->flags & CLK_GET_RATE_NOCACHE)
492 pclk->rate = clk_get_rate(pclk);
497 ulong clk_set_rate(struct clk *clk, ulong rate)
499 const struct clk_ops *ops;
501 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
504 ops = clk_dev_ops(clk->dev);
509 return ops->set_rate(clk, rate);
512 int clk_set_parent(struct clk *clk, struct clk *parent)
514 const struct clk_ops *ops;
516 debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
519 ops = clk_dev_ops(clk->dev);
521 if (!ops->set_parent)
524 return ops->set_parent(clk, parent);
527 int clk_enable(struct clk *clk)
529 const struct clk_ops *ops;
530 struct clk *clkp = NULL;
533 debug("%s(clk=%p)\n", __func__, clk);
536 ops = clk_dev_ops(clk->dev);
538 if (CONFIG_IS_ENABLED(CLK_CCF)) {
539 /* Take id 0 as a non-valid clk, such as dummy */
540 if (clk->id && !clk_get_by_id(clk->id, &clkp)) {
541 if (clkp->enable_count) {
542 clkp->enable_count++;
545 if (clkp->dev->parent &&
546 device_get_uclass_id(clkp->dev) == UCLASS_CLK) {
547 ret = clk_enable(dev_get_clk_ptr(clkp->dev->parent));
549 printf("Enable %s failed\n",
550 clkp->dev->parent->name);
557 ret = ops->enable(clk);
559 printf("Enable %s failed\n", clk->dev->name);
564 clkp->enable_count++;
568 return ops->enable(clk);
574 int clk_enable_bulk(struct clk_bulk *bulk)
578 for (i = 0; i < bulk->count; i++) {
579 ret = clk_enable(&bulk->clks[i]);
580 if (ret < 0 && ret != -ENOSYS)
587 int clk_disable(struct clk *clk)
589 const struct clk_ops *ops;
590 struct clk *clkp = NULL;
593 debug("%s(clk=%p)\n", __func__, clk);
596 ops = clk_dev_ops(clk->dev);
598 if (CONFIG_IS_ENABLED(CLK_CCF)) {
599 if (clk->id && !clk_get_by_id(clk->id, &clkp)) {
600 if (clkp->enable_count == 0) {
601 printf("clk %s already disabled\n",
606 if (--clkp->enable_count > 0)
611 ret = ops->disable(clk);
616 if (clkp && clkp->dev->parent &&
617 device_get_uclass_id(clkp->dev) == UCLASS_CLK) {
618 ret = clk_disable(dev_get_clk_ptr(clkp->dev->parent));
620 printf("Disable %s failed\n",
621 clkp->dev->parent->name);
629 return ops->disable(clk);
635 int clk_disable_bulk(struct clk_bulk *bulk)
639 for (i = 0; i < bulk->count; i++) {
640 ret = clk_disable(&bulk->clks[i]);
641 if (ret < 0 && ret != -ENOSYS)
648 int clk_get_by_id(ulong id, struct clk **clkp)
654 ret = uclass_get(UCLASS_CLK, &uc);
658 uclass_foreach_dev(dev, uc) {
659 struct clk *clk = dev_get_clk_ptr(dev);
661 if (clk && clk->id == id) {
670 bool clk_is_match(const struct clk *p, const struct clk *q)
672 /* trivial case: identical struct clk's or both NULL */
676 /* trivial case #2: on the clk pointer is NULL */
680 /* same device, id and data */
681 if (p->dev == q->dev && p->id == q->id && p->data == q->data)
687 static void devm_clk_release(struct udevice *dev, void *res)
692 static int devm_clk_match(struct udevice *dev, void *res, void *data)
697 struct clk *devm_clk_get(struct udevice *dev, const char *id)
702 clk = devres_alloc(devm_clk_release, sizeof(struct clk), __GFP_ZERO);
704 return ERR_PTR(-ENOMEM);
706 rc = clk_get_by_name(dev, id, clk);
710 devres_add(dev, clk);
714 struct clk *devm_clk_get_optional(struct udevice *dev, const char *id)
716 struct clk *clk = devm_clk_get(dev, id);
718 if (PTR_ERR(clk) == -ENODATA)
724 void devm_clk_put(struct udevice *dev, struct clk *clk)
731 rc = devres_release(dev, devm_clk_release, devm_clk_match, clk);
735 int clk_uclass_post_probe(struct udevice *dev)
738 * when a clock provider is probed. Call clk_set_defaults()
739 * also after the device is probed. This takes care of cases
740 * where the DT is used to setup default parents and rates
741 * using assigned-clocks
743 clk_set_defaults(dev, 1);
748 UCLASS_DRIVER(clk) = {
751 .post_probe = clk_uclass_post_probe,