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>
14 #include <dt-structs.h>
16 #include <linux/clk-provider.h>
18 static inline const struct clk_ops *clk_dev_ops(struct udevice *dev)
20 return (const struct clk_ops *)dev->driver->ops;
23 #if CONFIG_IS_ENABLED(OF_CONTROL)
24 # if CONFIG_IS_ENABLED(OF_PLATDATA)
25 int clk_get_by_index_platdata(struct udevice *dev, int index,
26 struct phandle_1_arg *cells, struct clk *clk)
32 ret = uclass_get_device(UCLASS_CLK, 0, &clk->dev);
35 clk->id = cells[0].arg[0];
40 static int clk_of_xlate_default(struct clk *clk,
41 struct ofnode_phandle_args *args)
43 debug("%s(clk=%p)\n", __func__, clk);
45 if (args->args_count > 1) {
46 debug("Invaild args_count: %d\n", args->args_count);
51 clk->id = args->args[0];
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,
65 struct udevice *dev_clk;
66 const struct clk_ops *ops;
73 ret = uclass_get_device_by_ofnode(UCLASS_CLK, args->node, &dev_clk);
75 debug("%s: uclass_get_device_by_of_offset failed: err=%d\n",
82 ops = clk_dev_ops(dev_clk);
85 ret = ops->of_xlate(clk, args);
87 ret = clk_of_xlate_default(clk, args);
89 debug("of_xlate() failed: %d\n", ret);
93 return clk_request(dev_clk, clk);
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);
100 static int clk_get_by_indexed_prop(struct udevice *dev, const char *prop_name,
101 int index, struct clk *clk)
104 struct ofnode_phandle_args args;
106 debug("%s(dev=%p, index=%d, clk=%p)\n", __func__, dev, index, clk);
111 ret = dev_read_phandle_with_args(dev, prop_name, "#clock-cells", 0,
114 debug("%s: fdtdec_parse_phandle_with_args failed: err=%d\n",
120 return clk_get_by_index_tail(ret, dev_ofnode(dev), &args, "clocks",
124 int clk_get_by_index(struct udevice *dev, int index, struct clk *clk)
126 struct ofnode_phandle_args args;
129 ret = dev_read_phandle_with_args(dev, "clocks", "#clock-cells", 0,
132 return clk_get_by_index_tail(ret, dev_ofnode(dev), &args, "clocks",
136 int clk_get_by_index_nodev(ofnode node, int index, struct clk *clk)
138 struct ofnode_phandle_args args;
141 ret = ofnode_parse_phandle_with_args(node, "clocks", "#clock-cells", 0,
144 return clk_get_by_index_tail(ret, node, &args, "clocks",
148 int clk_get_bulk(struct udevice *dev, struct clk_bulk *bulk)
150 int i, ret, err, count;
154 count = dev_count_phandle_with_args(dev, "clocks", "#clock-cells");
158 bulk->clks = devm_kcalloc(dev, count, sizeof(struct clk), GFP_KERNEL);
162 for (i = 0; i < count; i++) {
163 ret = clk_get_by_index(dev, i, &bulk->clks[i]);
173 err = clk_release_all(bulk->clks, bulk->count);
175 debug("%s: could release all clocks for %p\n",
181 static int clk_set_default_parents(struct udevice *dev, int stage)
183 struct clk clk, parent_clk;
188 num_parents = dev_count_phandle_with_args(dev, "assigned-clock-parents",
190 if (num_parents < 0) {
191 debug("%s: could not read assigned-clock-parents for %p\n",
196 for (index = 0; index < num_parents; index++) {
197 ret = clk_get_by_indexed_prop(dev, "assigned-clock-parents",
199 /* If -ENOENT, this is a no-op entry */
204 debug("%s: could not get parent clock %d for %s\n",
205 __func__, index, dev_read_name(dev));
209 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
212 debug("%s: could not get assigned clock %d for %s\n",
213 __func__, index, dev_read_name(dev));
217 /* This is clk provider device trying to reparent itself
218 * It cannot be done right now but need to wait after the
221 if (stage == 0 && clk.dev == dev)
224 if (stage > 0 && clk.dev != dev)
225 /* do not setup twice the parent clocks */
228 ret = clk_set_parent(&clk, &parent_clk);
230 * Not all drivers may support clock-reparenting (as of now).
231 * Ignore errors due to this.
237 debug("%s: failed to reparent clock %d for %s\n",
238 __func__, index, dev_read_name(dev));
246 static int clk_set_default_rates(struct udevice *dev, int stage)
255 size = dev_read_size(dev, "assigned-clock-rates");
259 num_rates = size / sizeof(u32);
260 rates = calloc(num_rates, sizeof(u32));
264 ret = dev_read_u32_array(dev, "assigned-clock-rates", rates, num_rates);
268 for (index = 0; index < num_rates; index++) {
269 /* If 0 is passed, this is a no-op */
273 ret = clk_get_by_indexed_prop(dev, "assigned-clocks",
276 debug("%s: could not get assigned clock %d for %s\n",
277 __func__, index, dev_read_name(dev));
281 /* This is clk provider device trying to program itself
282 * It cannot be done right now but need to wait after the
285 if (stage == 0 && clk.dev == dev)
288 if (stage > 0 && clk.dev != dev)
289 /* do not setup twice the parent clocks */
292 ret = clk_set_rate(&clk, rates[index]);
295 debug("%s: failed to set rate on clock index %d (%ld) for %s\n",
296 __func__, index, clk.id, dev_read_name(dev));
306 int clk_set_defaults(struct udevice *dev, int stage)
310 if (!dev_of_valid(dev))
313 /* If this not in SPL and pre-reloc state, don't take any action. */
314 if (!(IS_ENABLED(CONFIG_SPL_BUILD) || (gd->flags & GD_FLG_RELOC)))
317 debug("%s(%s)\n", __func__, dev_read_name(dev));
319 ret = clk_set_default_parents(dev, stage);
323 ret = clk_set_default_rates(dev, stage);
329 # endif /* OF_PLATDATA */
331 int clk_get_by_name(struct udevice *dev, const char *name, struct clk *clk)
335 debug("%s(dev=%p, name=%s, clk=%p)\n", __func__, dev, name, clk);
338 index = dev_read_stringlist_search(dev, "clock-names", name);
340 debug("fdt_stringlist_search() failed: %d\n", index);
344 return clk_get_by_index(dev, index, clk);
347 int clk_get_by_name_nodev(ofnode node, const char *name, struct clk *clk)
351 debug("%s(node=%p, name=%s, clk=%p)\n", __func__,
352 ofnode_get_name(node), name, clk);
355 index = ofnode_stringlist_search(node, "clock-names", name);
357 debug("fdt_stringlist_search() failed: %d\n", index);
361 return clk_get_by_index_nodev(node, index, clk);
364 int clk_get_optional_nodev(ofnode node, const char *name, struct clk *clk)
368 ret = clk_get_by_name_nodev(node, name, clk);
375 int clk_release_all(struct clk *clk, int count)
379 for (i = 0; i < count; i++) {
380 debug("%s(clk[%d]=%p)\n", __func__, i, &clk[i]);
382 /* check if clock has been previously requested */
386 ret = clk_disable(&clk[i]);
387 if (ret && ret != -ENOSYS)
390 ret = clk_free(&clk[i]);
391 if (ret && ret != -ENOSYS)
398 #endif /* OF_CONTROL */
400 int clk_request(struct udevice *dev, struct clk *clk)
402 const struct clk_ops *ops;
404 debug("%s(dev=%p, clk=%p)\n", __func__, dev, clk);
407 ops = clk_dev_ops(dev);
414 return ops->request(clk);
417 int clk_free(struct clk *clk)
419 const struct clk_ops *ops;
421 debug("%s(clk=%p)\n", __func__, clk);
424 ops = clk_dev_ops(clk->dev);
429 return ops->free(clk);
432 ulong clk_get_rate(struct clk *clk)
434 const struct clk_ops *ops;
436 debug("%s(clk=%p)\n", __func__, clk);
439 ops = clk_dev_ops(clk->dev);
444 return ops->get_rate(clk);
447 struct clk *clk_get_parent(struct clk *clk)
449 struct udevice *pdev;
452 debug("%s(clk=%p)\n", __func__, clk);
456 pdev = dev_get_parent(clk->dev);
457 pclk = dev_get_clk_ptr(pdev);
459 return ERR_PTR(-ENODEV);
464 long long clk_get_parent_rate(struct clk *clk)
466 const struct clk_ops *ops;
469 debug("%s(clk=%p)\n", __func__, clk);
473 pclk = clk_get_parent(clk);
477 ops = clk_dev_ops(pclk->dev);
481 /* Read the 'rate' if not already set or if proper flag set*/
482 if (!pclk->rate || pclk->flags & CLK_GET_RATE_NOCACHE)
483 pclk->rate = clk_get_rate(pclk);
488 ulong clk_set_rate(struct clk *clk, ulong rate)
490 const struct clk_ops *ops;
492 debug("%s(clk=%p, rate=%lu)\n", __func__, clk, rate);
495 ops = clk_dev_ops(clk->dev);
500 return ops->set_rate(clk, rate);
503 int clk_set_parent(struct clk *clk, struct clk *parent)
505 const struct clk_ops *ops;
507 debug("%s(clk=%p, parent=%p)\n", __func__, clk, parent);
510 ops = clk_dev_ops(clk->dev);
512 if (!ops->set_parent)
515 return ops->set_parent(clk, parent);
518 int clk_enable(struct clk *clk)
520 const struct clk_ops *ops;
521 struct clk *clkp = NULL;
524 debug("%s(clk=%p)\n", __func__, clk);
527 ops = clk_dev_ops(clk->dev);
529 if (CONFIG_IS_ENABLED(CLK_CCF)) {
530 /* Take id 0 as a non-valid clk, such as dummy */
531 if (clk->id && !clk_get_by_id(clk->id, &clkp)) {
532 if (clkp->enable_count) {
533 clkp->enable_count++;
536 if (clkp->dev->parent &&
537 device_get_uclass_id(clkp->dev) == UCLASS_CLK) {
538 ret = clk_enable(dev_get_clk_ptr(clkp->dev->parent));
540 printf("Enable %s failed\n",
541 clkp->dev->parent->name);
548 ret = ops->enable(clk);
550 printf("Enable %s failed\n", clk->dev->name);
555 clkp->enable_count++;
559 return ops->enable(clk);
565 int clk_enable_bulk(struct clk_bulk *bulk)
569 for (i = 0; i < bulk->count; i++) {
570 ret = clk_enable(&bulk->clks[i]);
571 if (ret < 0 && ret != -ENOSYS)
578 int clk_disable(struct clk *clk)
580 const struct clk_ops *ops;
581 struct clk *clkp = NULL;
584 debug("%s(clk=%p)\n", __func__, clk);
587 ops = clk_dev_ops(clk->dev);
589 if (CONFIG_IS_ENABLED(CLK_CCF)) {
590 if (clk->id && !clk_get_by_id(clk->id, &clkp)) {
591 if (clkp->enable_count == 0) {
592 printf("clk %s already disabled\n",
597 if (--clkp->enable_count > 0)
602 ret = ops->disable(clk);
607 if (clkp && clkp->dev->parent &&
608 device_get_uclass_id(clkp->dev) == UCLASS_CLK) {
609 ret = clk_disable(dev_get_clk_ptr(clkp->dev->parent));
611 printf("Disable %s failed\n",
612 clkp->dev->parent->name);
620 return ops->disable(clk);
626 int clk_disable_bulk(struct clk_bulk *bulk)
630 for (i = 0; i < bulk->count; i++) {
631 ret = clk_disable(&bulk->clks[i]);
632 if (ret < 0 && ret != -ENOSYS)
639 int clk_get_by_id(ulong id, struct clk **clkp)
645 ret = uclass_get(UCLASS_CLK, &uc);
649 uclass_foreach_dev(dev, uc) {
650 struct clk *clk = dev_get_clk_ptr(dev);
652 if (clk && clk->id == id) {
661 bool clk_is_match(const struct clk *p, const struct clk *q)
663 /* trivial case: identical struct clk's or both NULL */
667 /* trivial case #2: on the clk pointer is NULL */
671 /* same device, id and data */
672 if (p->dev == q->dev && p->id == q->id && p->data == q->data)
678 static void devm_clk_release(struct udevice *dev, void *res)
683 static int devm_clk_match(struct udevice *dev, void *res, void *data)
688 struct clk *devm_clk_get(struct udevice *dev, const char *id)
693 clk = devres_alloc(devm_clk_release, sizeof(struct clk), __GFP_ZERO);
695 return ERR_PTR(-ENOMEM);
697 rc = clk_get_by_name(dev, id, clk);
701 devres_add(dev, clk);
705 struct clk *devm_clk_get_optional(struct udevice *dev, const char *id)
707 struct clk *clk = devm_clk_get(dev, id);
709 if (PTR_ERR(clk) == -ENODATA)
715 void devm_clk_put(struct udevice *dev, struct clk *clk)
722 rc = devres_release(dev, devm_clk_release, devm_clk_match, clk);
726 int clk_uclass_post_probe(struct udevice *dev)
729 * when a clock provider is probed. Call clk_set_defaults()
730 * also after the device is probed. This takes care of cases
731 * where the DT is used to setup default parents and rates
732 * using assigned-clocks
734 clk_set_defaults(dev, 1);
739 UCLASS_DRIVER(clk) = {
742 .post_probe = clk_uclass_post_probe,