Linux 4.16.11
[linux/fpc-iii.git] / drivers / clk / clk-bulk.c
blob4c10456f8a32923c0dade56265e4eeef714df641
1 /*
2 * Copyright 2017 NXP
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
13 * more details.
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/device.h>
21 #include <linux/export.h>
23 void clk_bulk_put(int num_clks, struct clk_bulk_data *clks)
25 while (--num_clks >= 0) {
26 clk_put(clks[num_clks].clk);
27 clks[num_clks].clk = NULL;
30 EXPORT_SYMBOL_GPL(clk_bulk_put);
32 int __must_check clk_bulk_get(struct device *dev, int num_clks,
33 struct clk_bulk_data *clks)
35 int ret;
36 int i;
38 for (i = 0; i < num_clks; i++)
39 clks[i].clk = NULL;
41 for (i = 0; i < num_clks; i++) {
42 clks[i].clk = clk_get(dev, clks[i].id);
43 if (IS_ERR(clks[i].clk)) {
44 ret = PTR_ERR(clks[i].clk);
45 dev_err(dev, "Failed to get clk '%s': %d\n",
46 clks[i].id, ret);
47 clks[i].clk = NULL;
48 goto err;
52 return 0;
54 err:
55 clk_bulk_put(i, clks);
57 return ret;
59 EXPORT_SYMBOL(clk_bulk_get);
61 #ifdef CONFIG_HAVE_CLK_PREPARE
63 /**
64 * clk_bulk_unprepare - undo preparation of a set of clock sources
65 * @num_clks: the number of clk_bulk_data
66 * @clks: the clk_bulk_data table being unprepared
68 * clk_bulk_unprepare may sleep, which differentiates it from clk_bulk_disable.
69 * Returns 0 on success, -EERROR otherwise.
71 void clk_bulk_unprepare(int num_clks, const struct clk_bulk_data *clks)
73 while (--num_clks >= 0)
74 clk_unprepare(clks[num_clks].clk);
76 EXPORT_SYMBOL_GPL(clk_bulk_unprepare);
78 /**
79 * clk_bulk_prepare - prepare a set of clocks
80 * @num_clks: the number of clk_bulk_data
81 * @clks: the clk_bulk_data table being prepared
83 * clk_bulk_prepare may sleep, which differentiates it from clk_bulk_enable.
84 * Returns 0 on success, -EERROR otherwise.
86 int __must_check clk_bulk_prepare(int num_clks,
87 const struct clk_bulk_data *clks)
89 int ret;
90 int i;
92 for (i = 0; i < num_clks; i++) {
93 ret = clk_prepare(clks[i].clk);
94 if (ret) {
95 pr_err("Failed to prepare clk '%s': %d\n",
96 clks[i].id, ret);
97 goto err;
101 return 0;
103 err:
104 clk_bulk_unprepare(i, clks);
106 return ret;
108 EXPORT_SYMBOL_GPL(clk_bulk_prepare);
110 #endif /* CONFIG_HAVE_CLK_PREPARE */
113 * clk_bulk_disable - gate a set of clocks
114 * @num_clks: the number of clk_bulk_data
115 * @clks: the clk_bulk_data table being gated
117 * clk_bulk_disable must not sleep, which differentiates it from
118 * clk_bulk_unprepare. clk_bulk_disable must be called before
119 * clk_bulk_unprepare.
121 void clk_bulk_disable(int num_clks, const struct clk_bulk_data *clks)
124 while (--num_clks >= 0)
125 clk_disable(clks[num_clks].clk);
127 EXPORT_SYMBOL_GPL(clk_bulk_disable);
130 * clk_bulk_enable - ungate a set of clocks
131 * @num_clks: the number of clk_bulk_data
132 * @clks: the clk_bulk_data table being ungated
134 * clk_bulk_enable must not sleep
135 * Returns 0 on success, -EERROR otherwise.
137 int __must_check clk_bulk_enable(int num_clks, const struct clk_bulk_data *clks)
139 int ret;
140 int i;
142 for (i = 0; i < num_clks; i++) {
143 ret = clk_enable(clks[i].clk);
144 if (ret) {
145 pr_err("Failed to enable clk '%s': %d\n",
146 clks[i].id, ret);
147 goto err;
151 return 0;
153 err:
154 clk_bulk_disable(i, clks);
156 return ret;
158 EXPORT_SYMBOL_GPL(clk_bulk_enable);