1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2014 Samsung Electronics Co., Ltd.
8 #include <linux/clk-provider.h>
9 #include <linux/clk/clk-conf.h>
10 #include <linux/device.h>
12 #include <linux/printk.h>
13 #include <linux/slab.h>
15 static int __set_clk_parents(struct device_node *node, bool clk_supplier)
17 struct of_phandle_args clkspec;
18 int index, rc, num_parents;
19 struct clk *clk, *pclk;
21 num_parents = of_count_phandle_with_args(node, "assigned-clock-parents",
23 if (num_parents == -EINVAL)
24 pr_err("clk: invalid value of clock-parents property at %pOF\n",
27 for (index = 0; index < num_parents; index++) {
28 rc = of_parse_phandle_with_args(node, "assigned-clock-parents",
29 "#clock-cells", index, &clkspec);
31 /* skip empty (null) phandles */
37 if (clkspec.np == node && !clk_supplier) {
38 of_node_put(clkspec.np);
41 pclk = of_clk_get_from_provider(&clkspec);
42 of_node_put(clkspec.np);
44 if (PTR_ERR(pclk) != -EPROBE_DEFER)
45 pr_warn("clk: couldn't get parent clock %d for %pOF\n",
50 rc = of_parse_phandle_with_args(node, "assigned-clocks",
51 "#clock-cells", index, &clkspec);
54 if (clkspec.np == node && !clk_supplier) {
55 of_node_put(clkspec.np);
59 clk = of_clk_get_from_provider(&clkspec);
60 of_node_put(clkspec.np);
62 if (PTR_ERR(clk) != -EPROBE_DEFER)
63 pr_warn("clk: couldn't get assigned clock %d for %pOF\n",
69 rc = clk_set_parent(clk, pclk);
71 pr_err("clk: failed to reparent %s to %s: %d\n",
72 __clk_get_name(clk), __clk_get_name(pclk), rc);
82 static int __set_clk_rates(struct device_node *node, bool clk_supplier)
84 struct of_phandle_args clkspec;
85 int rc, count, count_64, index;
87 u64 *rates_64 __free(kfree) = NULL;
88 u32 *rates __free(kfree) = NULL;
90 count = of_property_count_u32_elems(node, "assigned-clock-rates");
91 count_64 = of_property_count_u64_elems(node, "assigned-clock-rates-u64");
94 rates_64 = kcalloc(count, sizeof(*rates_64), GFP_KERNEL);
98 rc = of_property_read_u64_array(node,
99 "assigned-clock-rates-u64",
101 } else if (count > 0) {
102 rates = kcalloc(count, sizeof(*rates), GFP_KERNEL);
106 rc = of_property_read_u32_array(node, "assigned-clock-rates",
115 for (index = 0; index < count; index++) {
119 rate = rates_64[index];
124 rc = of_parse_phandle_with_args(node, "assigned-clocks",
125 "#clock-cells", index, &clkspec);
127 /* skip empty (null) phandles */
133 if (clkspec.np == node && !clk_supplier) {
134 of_node_put(clkspec.np);
138 clk = of_clk_get_from_provider(&clkspec);
139 of_node_put(clkspec.np);
141 if (PTR_ERR(clk) != -EPROBE_DEFER)
142 pr_warn("clk: couldn't get clock %d for %pOF\n",
147 rc = clk_set_rate(clk, rate);
149 pr_err("clk: couldn't set %s clk rate to %lu (%d), current rate: %lu\n",
150 __clk_get_name(clk), rate, rc,
159 * of_clk_set_defaults() - parse and set assigned clocks configuration
160 * @node: device node to apply clock settings for
161 * @clk_supplier: true if clocks supplied by @node should also be considered
163 * This function parses 'assigned-{clocks/clock-parents/clock-rates}' properties
164 * and sets any specified clock parents and rates. The @clk_supplier argument
165 * should be set to true if @node may be also a clock supplier of any clock
166 * listed in its 'assigned-clocks' or 'assigned-clock-parents' properties.
167 * If @clk_supplier is false the function exits returning 0 as soon as it
168 * determines the @node is also a supplier of any of the clocks.
170 int of_clk_set_defaults(struct device_node *node, bool clk_supplier)
177 rc = __set_clk_parents(node, clk_supplier);
181 return __set_clk_rates(node, clk_supplier);
183 EXPORT_SYMBOL_GPL(of_clk_set_defaults);