2 * Copyright (C) 2014 Samsung Electronics Co., Ltd.
3 * Sylwester Nawrocki <s.nawrocki@samsung.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/clk.h>
11 #include <linux/clk-provider.h>
12 #include <linux/clk/clk-conf.h>
13 #include <linux/device.h>
15 #include <linux/printk.h>
17 static int __set_clk_parents(struct device_node *node, bool clk_supplier)
19 struct of_phandle_args clkspec;
20 int index, rc, num_parents;
21 struct clk *clk, *pclk;
23 num_parents = of_count_phandle_with_args(node, "assigned-clock-parents",
25 if (num_parents == -EINVAL)
26 pr_err("clk: invalid value of clock-parents property at %pOF\n",
29 for (index = 0; index < num_parents; index++) {
30 rc = of_parse_phandle_with_args(node, "assigned-clock-parents",
31 "#clock-cells", index, &clkspec);
33 /* skip empty (null) phandles */
39 if (clkspec.np == node && !clk_supplier) {
40 of_node_put(clkspec.np);
43 pclk = of_clk_get_from_provider(&clkspec);
44 of_node_put(clkspec.np);
46 if (PTR_ERR(pclk) != -EPROBE_DEFER)
47 pr_warn("clk: couldn't get parent clock %d for %pOF\n",
52 rc = of_parse_phandle_with_args(node, "assigned-clocks",
53 "#clock-cells", index, &clkspec);
56 if (clkspec.np == node && !clk_supplier) {
57 of_node_put(clkspec.np);
61 clk = of_clk_get_from_provider(&clkspec);
62 of_node_put(clkspec.np);
64 if (PTR_ERR(clk) != -EPROBE_DEFER)
65 pr_warn("clk: couldn't get assigned clock %d for %pOF\n",
71 rc = clk_set_parent(clk, pclk);
73 pr_err("clk: failed to reparent %s to %s: %d\n",
74 __clk_get_name(clk), __clk_get_name(pclk), rc);
84 static int __set_clk_rates(struct device_node *node, bool clk_supplier)
86 struct of_phandle_args clkspec;
87 struct property *prop;
93 of_property_for_each_u32(node, "assigned-clock-rates", prop, cur, rate) {
95 rc = of_parse_phandle_with_args(node, "assigned-clocks",
96 "#clock-cells", index, &clkspec);
98 /* skip empty (null) phandles */
104 if (clkspec.np == node && !clk_supplier) {
105 of_node_put(clkspec.np);
109 clk = of_clk_get_from_provider(&clkspec);
110 of_node_put(clkspec.np);
112 if (PTR_ERR(clk) != -EPROBE_DEFER)
113 pr_warn("clk: couldn't get clock %d for %pOF\n",
118 rc = clk_set_rate(clk, rate);
120 pr_err("clk: couldn't set %s clk rate to %u (%d), current rate: %lu\n",
121 __clk_get_name(clk), rate, rc,
131 * of_clk_set_defaults() - parse and set assigned clocks configuration
132 * @node: device node to apply clock settings for
133 * @clk_supplier: true if clocks supplied by @node should also be considered
135 * This function parses 'assigned-{clocks/clock-parents/clock-rates}' properties
136 * and sets any specified clock parents and rates. The @clk_supplier argument
137 * should be set to true if @node may be also a clock supplier of any clock
138 * listed in its 'assigned-clocks' or 'assigned-clock-parents' properties.
139 * If @clk_supplier is false the function exits returning 0 as soon as it
140 * determines the @node is also a supplier of any of the clocks.
142 int of_clk_set_defaults(struct device_node *node, bool clk_supplier)
149 rc = __set_clk_parents(node, clk_supplier);
153 return __set_clk_rates(node, clk_supplier);
155 EXPORT_SYMBOL_GPL(of_clk_set_defaults);