1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2014 Samsung Electronics Co., Ltd.
4 * Sylwester Nawrocki <s.nawrocki@samsung.com>
8 #include <linux/clk-provider.h>
9 #include <linux/clk/clk-conf.h>
10 #include <linux/device.h>
12 #include <linux/printk.h>
14 static int __set_clk_parents(struct device_node *node, bool clk_supplier)
16 struct of_phandle_args clkspec;
17 int index, rc, num_parents;
18 struct clk *clk, *pclk;
20 num_parents = of_count_phandle_with_args(node, "assigned-clock-parents",
22 if (num_parents == -EINVAL)
23 pr_err("clk: invalid value of clock-parents property at %pOF\n",
26 for (index = 0; index < num_parents; index++) {
27 rc = of_parse_phandle_with_args(node, "assigned-clock-parents",
28 "#clock-cells", index, &clkspec);
30 /* skip empty (null) phandles */
36 if (clkspec.np == node && !clk_supplier) {
37 of_node_put(clkspec.np);
40 pclk = of_clk_get_from_provider(&clkspec);
41 of_node_put(clkspec.np);
43 if (PTR_ERR(pclk) != -EPROBE_DEFER)
44 pr_warn("clk: couldn't get parent clock %d for %pOF\n",
49 rc = of_parse_phandle_with_args(node, "assigned-clocks",
50 "#clock-cells", index, &clkspec);
53 if (clkspec.np == node && !clk_supplier) {
54 of_node_put(clkspec.np);
58 clk = of_clk_get_from_provider(&clkspec);
59 of_node_put(clkspec.np);
61 if (PTR_ERR(clk) != -EPROBE_DEFER)
62 pr_warn("clk: couldn't get assigned clock %d for %pOF\n",
68 rc = clk_set_parent(clk, pclk);
70 pr_err("clk: failed to reparent %s to %s: %d\n",
71 __clk_get_name(clk), __clk_get_name(pclk), rc);
81 static int __set_clk_rates(struct device_node *node, bool clk_supplier)
83 struct of_phandle_args clkspec;
84 struct property *prop;
90 of_property_for_each_u32(node, "assigned-clock-rates", prop, cur, rate) {
92 rc = of_parse_phandle_with_args(node, "assigned-clocks",
93 "#clock-cells", index, &clkspec);
95 /* skip empty (null) phandles */
101 if (clkspec.np == node && !clk_supplier) {
102 of_node_put(clkspec.np);
106 clk = of_clk_get_from_provider(&clkspec);
107 of_node_put(clkspec.np);
109 if (PTR_ERR(clk) != -EPROBE_DEFER)
110 pr_warn("clk: couldn't get clock %d for %pOF\n",
115 rc = clk_set_rate(clk, rate);
117 pr_err("clk: couldn't set %s clk rate to %u (%d), current rate: %lu\n",
118 __clk_get_name(clk), rate, rc,
128 * of_clk_set_defaults() - parse and set assigned clocks configuration
129 * @node: device node to apply clock settings for
130 * @clk_supplier: true if clocks supplied by @node should also be considered
132 * This function parses 'assigned-{clocks/clock-parents/clock-rates}' properties
133 * and sets any specified clock parents and rates. The @clk_supplier argument
134 * should be set to true if @node may be also a clock supplier of any clock
135 * listed in its 'assigned-clocks' or 'assigned-clock-parents' properties.
136 * If @clk_supplier is false the function exits returning 0 as soon as it
137 * determines the @node is also a supplier of any of the clocks.
139 int of_clk_set_defaults(struct device_node *node, bool clk_supplier)
146 rc = __set_clk_parents(node, clk_supplier);
150 return __set_clk_rates(node, clk_supplier);
152 EXPORT_SYMBOL_GPL(of_clk_set_defaults);