4 * Dong Aisheng <aisheng.dong@nxp.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <linux/clk.h>
20 #include <linux/clk-provider.h>
21 #include <linux/device.h>
22 #include <linux/export.h>
24 #include <linux/slab.h>
26 static int __must_check
of_clk_bulk_get(struct device_node
*np
, int num_clks
,
27 struct clk_bulk_data
*clks
)
32 for (i
= 0; i
< num_clks
; i
++)
35 for (i
= 0; i
< num_clks
; i
++) {
36 clks
[i
].clk
= of_clk_get(np
, i
);
37 if (IS_ERR(clks
[i
].clk
)) {
38 ret
= PTR_ERR(clks
[i
].clk
);
39 pr_err("%pOF: Failed to get clk index: %d ret: %d\n",
49 clk_bulk_put(i
, clks
);
54 static int __must_check
of_clk_bulk_get_all(struct device_node
*np
,
55 struct clk_bulk_data
**clks
)
57 struct clk_bulk_data
*clk_bulk
;
61 num_clks
= of_clk_get_parent_count(np
);
65 clk_bulk
= kmalloc_array(num_clks
, sizeof(*clk_bulk
), GFP_KERNEL
);
69 ret
= of_clk_bulk_get(np
, num_clks
, clk_bulk
);
80 void clk_bulk_put(int num_clks
, struct clk_bulk_data
*clks
)
82 while (--num_clks
>= 0) {
83 clk_put(clks
[num_clks
].clk
);
84 clks
[num_clks
].clk
= NULL
;
87 EXPORT_SYMBOL_GPL(clk_bulk_put
);
89 int __must_check
clk_bulk_get(struct device
*dev
, int num_clks
,
90 struct clk_bulk_data
*clks
)
95 for (i
= 0; i
< num_clks
; i
++)
98 for (i
= 0; i
< num_clks
; i
++) {
99 clks
[i
].clk
= clk_get(dev
, clks
[i
].id
);
100 if (IS_ERR(clks
[i
].clk
)) {
101 ret
= PTR_ERR(clks
[i
].clk
);
102 if (ret
!= -EPROBE_DEFER
)
103 dev_err(dev
, "Failed to get clk '%s': %d\n",
113 clk_bulk_put(i
, clks
);
117 EXPORT_SYMBOL(clk_bulk_get
);
119 void clk_bulk_put_all(int num_clks
, struct clk_bulk_data
*clks
)
121 if (IS_ERR_OR_NULL(clks
))
124 clk_bulk_put(num_clks
, clks
);
128 EXPORT_SYMBOL(clk_bulk_put_all
);
130 int __must_check
clk_bulk_get_all(struct device
*dev
,
131 struct clk_bulk_data
**clks
)
133 struct device_node
*np
= dev_of_node(dev
);
138 return of_clk_bulk_get_all(np
, clks
);
140 EXPORT_SYMBOL(clk_bulk_get_all
);
142 #ifdef CONFIG_HAVE_CLK_PREPARE
145 * clk_bulk_unprepare - undo preparation of a set of clock sources
146 * @num_clks: the number of clk_bulk_data
147 * @clks: the clk_bulk_data table being unprepared
149 * clk_bulk_unprepare may sleep, which differentiates it from clk_bulk_disable.
150 * Returns 0 on success, -EERROR otherwise.
152 void clk_bulk_unprepare(int num_clks
, const struct clk_bulk_data
*clks
)
154 while (--num_clks
>= 0)
155 clk_unprepare(clks
[num_clks
].clk
);
157 EXPORT_SYMBOL_GPL(clk_bulk_unprepare
);
160 * clk_bulk_prepare - prepare a set of clocks
161 * @num_clks: the number of clk_bulk_data
162 * @clks: the clk_bulk_data table being prepared
164 * clk_bulk_prepare may sleep, which differentiates it from clk_bulk_enable.
165 * Returns 0 on success, -EERROR otherwise.
167 int __must_check
clk_bulk_prepare(int num_clks
,
168 const struct clk_bulk_data
*clks
)
173 for (i
= 0; i
< num_clks
; i
++) {
174 ret
= clk_prepare(clks
[i
].clk
);
176 pr_err("Failed to prepare clk '%s': %d\n",
185 clk_bulk_unprepare(i
, clks
);
189 EXPORT_SYMBOL_GPL(clk_bulk_prepare
);
191 #endif /* CONFIG_HAVE_CLK_PREPARE */
194 * clk_bulk_disable - gate a set of clocks
195 * @num_clks: the number of clk_bulk_data
196 * @clks: the clk_bulk_data table being gated
198 * clk_bulk_disable must not sleep, which differentiates it from
199 * clk_bulk_unprepare. clk_bulk_disable must be called before
200 * clk_bulk_unprepare.
202 void clk_bulk_disable(int num_clks
, const struct clk_bulk_data
*clks
)
205 while (--num_clks
>= 0)
206 clk_disable(clks
[num_clks
].clk
);
208 EXPORT_SYMBOL_GPL(clk_bulk_disable
);
211 * clk_bulk_enable - ungate a set of clocks
212 * @num_clks: the number of clk_bulk_data
213 * @clks: the clk_bulk_data table being ungated
215 * clk_bulk_enable must not sleep
216 * Returns 0 on success, -EERROR otherwise.
218 int __must_check
clk_bulk_enable(int num_clks
, const struct clk_bulk_data
*clks
)
223 for (i
= 0; i
< num_clks
; i
++) {
224 ret
= clk_enable(clks
[i
].clk
);
226 pr_err("Failed to enable clk '%s': %d\n",
235 clk_bulk_disable(i
, clks
);
239 EXPORT_SYMBOL_GPL(clk_bulk_enable
);