GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / clk / clk-conf.c
1 /*
2  * Copyright (C) 2014 Samsung Electronics Co., Ltd.
3  * Sylwester Nawrocki <s.nawrocki@samsung.com>
4  *
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.
8  */
9
10 #include <linux/clk.h>
11 #include <linux/clk-provider.h>
12 #include <linux/clk/clk-conf.h>
13 #include <linux/device.h>
14 #include <linux/of.h>
15 #include <linux/printk.h>
16
17 static int __set_clk_parents(struct device_node *node, bool clk_supplier)
18 {
19         struct of_phandle_args clkspec;
20         int index, rc, num_parents;
21         struct clk *clk, *pclk;
22
23         num_parents = of_count_phandle_with_args(node, "assigned-clock-parents",
24                                                  "#clock-cells");
25         if (num_parents == -EINVAL)
26                 pr_err("clk: invalid value of clock-parents property at %pOF\n",
27                        node);
28
29         for (index = 0; index < num_parents; index++) {
30                 rc = of_parse_phandle_with_args(node, "assigned-clock-parents",
31                                         "#clock-cells", index, &clkspec);
32                 if (rc < 0) {
33                         /* skip empty (null) phandles */
34                         if (rc == -ENOENT)
35                                 continue;
36                         else
37                                 return rc;
38                 }
39                 if (clkspec.np == node && !clk_supplier) {
40                         of_node_put(clkspec.np);
41                         return 0;
42                 }
43                 pclk = of_clk_get_from_provider(&clkspec);
44                 of_node_put(clkspec.np);
45                 if (IS_ERR(pclk)) {
46                         if (PTR_ERR(pclk) != -EPROBE_DEFER)
47                                 pr_warn("clk: couldn't get parent clock %d for %pOF\n",
48                                         index, node);
49                         return PTR_ERR(pclk);
50                 }
51
52                 rc = of_parse_phandle_with_args(node, "assigned-clocks",
53                                         "#clock-cells", index, &clkspec);
54                 if (rc < 0)
55                         goto err;
56                 if (clkspec.np == node && !clk_supplier) {
57                         of_node_put(clkspec.np);
58                         rc = 0;
59                         goto err;
60                 }
61                 clk = of_clk_get_from_provider(&clkspec);
62                 of_node_put(clkspec.np);
63                 if (IS_ERR(clk)) {
64                         if (PTR_ERR(clk) != -EPROBE_DEFER)
65                                 pr_warn("clk: couldn't get assigned clock %d for %pOF\n",
66                                         index, node);
67                         rc = PTR_ERR(clk);
68                         goto err;
69                 }
70
71                 rc = clk_set_parent(clk, pclk);
72                 if (rc < 0)
73                         pr_err("clk: failed to reparent %s to %s: %d\n",
74                                __clk_get_name(clk), __clk_get_name(pclk), rc);
75                 clk_put(clk);
76                 clk_put(pclk);
77         }
78         return 0;
79 err:
80         clk_put(pclk);
81         return rc;
82 }
83
84 static int __set_clk_rates(struct device_node *node, bool clk_supplier)
85 {
86         struct of_phandle_args clkspec;
87         struct property *prop;
88         const __be32 *cur;
89         int rc, index = 0;
90         struct clk *clk;
91         u32 rate;
92
93         of_property_for_each_u32(node, "assigned-clock-rates", prop, cur, rate) {
94                 if (rate) {
95                         rc = of_parse_phandle_with_args(node, "assigned-clocks",
96                                         "#clock-cells", index, &clkspec);
97                         if (rc < 0) {
98                                 /* skip empty (null) phandles */
99                                 if (rc == -ENOENT)
100                                         continue;
101                                 else
102                                         return rc;
103                         }
104                         if (clkspec.np == node && !clk_supplier) {
105                                 of_node_put(clkspec.np);
106                                 return 0;
107                         }
108
109                         clk = of_clk_get_from_provider(&clkspec);
110                         of_node_put(clkspec.np);
111                         if (IS_ERR(clk)) {
112                                 if (PTR_ERR(clk) != -EPROBE_DEFER)
113                                         pr_warn("clk: couldn't get clock %d for %pOF\n",
114                                                 index, node);
115                                 return PTR_ERR(clk);
116                         }
117
118                         rc = clk_set_rate(clk, rate);
119                         if (rc < 0)
120                                 pr_err("clk: couldn't set %s clk rate to %u (%d), current rate: %lu\n",
121                                        __clk_get_name(clk), rate, rc,
122                                        clk_get_rate(clk));
123                         clk_put(clk);
124                 }
125                 index++;
126         }
127         return 0;
128 }
129
130 /**
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
134  *
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.
141  */
142 int of_clk_set_defaults(struct device_node *node, bool clk_supplier)
143 {
144         int rc;
145
146         if (!node)
147                 return 0;
148
149         rc = __set_clk_parents(node, clk_supplier);
150         if (rc < 0)
151                 return rc;
152
153         return __set_clk_rates(node, clk_supplier);
154 }
155 EXPORT_SYMBOL_GPL(of_clk_set_defaults);