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
)
41 pclk
= of_clk_get_from_provider(&clkspec
);
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
) {
57 clk
= of_clk_get_from_provider(&clkspec
);
59 if (PTR_ERR(clk
) != -EPROBE_DEFER
)
60 pr_warn("clk: couldn't get assigned clock %d for %pOF\n",
66 rc
= clk_set_parent(clk
, pclk
);
68 pr_err("clk: failed to reparent %s to %s: %d\n",
69 __clk_get_name(clk
), __clk_get_name(pclk
), rc
);
79 static int __set_clk_rates(struct device_node
*node
, bool clk_supplier
)
81 struct of_phandle_args clkspec
;
82 struct property
*prop
;
88 of_property_for_each_u32(node
, "assigned-clock-rates", prop
, cur
, rate
) {
90 rc
= of_parse_phandle_with_args(node
, "assigned-clocks",
91 "#clock-cells", index
, &clkspec
);
93 /* skip empty (null) phandles */
99 if (clkspec
.np
== node
&& !clk_supplier
)
102 clk
= of_clk_get_from_provider(&clkspec
);
104 if (PTR_ERR(clk
) != -EPROBE_DEFER
)
105 pr_warn("clk: couldn't get clock %d for %pOF\n",
110 rc
= clk_set_rate(clk
, rate
);
112 pr_err("clk: couldn't set %s clk rate to %u (%d), current rate: %lu\n",
113 __clk_get_name(clk
), rate
, rc
,
123 * of_clk_set_defaults() - parse and set assigned clocks configuration
124 * @node: device node to apply clock settings for
125 * @clk_supplier: true if clocks supplied by @node should also be considered
127 * This function parses 'assigned-{clocks/clock-parents/clock-rates}' properties
128 * and sets any specified clock parents and rates. The @clk_supplier argument
129 * should be set to true if @node may be also a clock supplier of any clock
130 * listed in its 'assigned-clocks' or 'assigned-clock-parents' properties.
131 * If @clk_supplier is false the function exits returning 0 as soon as it
132 * determines the @node is also a supplier of any of the clocks.
134 int of_clk_set_defaults(struct device_node
*node
, bool clk_supplier
)
141 rc
= __set_clk_parents(node
, clk_supplier
);
145 return __set_clk_rates(node
, clk_supplier
);
147 EXPORT_SYMBOL_GPL(of_clk_set_defaults
);