2 * Hisilicon clock driver
4 * Copyright (c) 2012-2013 Hisilicon Limited.
5 * Copyright (c) 2012-2013 Linaro Limited.
7 * Author: Haojian Zhuang <haojian.zhuang@linaro.org>
8 * Xin Li <li.xin@linaro.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <linux/kernel.h>
27 #include <linux/clkdev.h>
28 #include <linux/clk-provider.h>
29 #include <linux/delay.h>
32 #include <linux/of_address.h>
33 #include <linux/of_device.h>
34 #include <linux/slab.h>
38 static DEFINE_SPINLOCK(hisi_clk_lock
);
40 struct hisi_clock_data
*hisi_clk_alloc(struct platform_device
*pdev
,
43 struct hisi_clock_data
*clk_data
;
45 struct clk
**clk_table
;
47 clk_data
= devm_kmalloc(&pdev
->dev
, sizeof(*clk_data
), GFP_KERNEL
);
51 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
52 clk_data
->base
= devm_ioremap(&pdev
->dev
,
53 res
->start
, resource_size(res
));
57 clk_table
= devm_kmalloc_array(&pdev
->dev
, nr_clks
,
63 clk_data
->clk_data
.clks
= clk_table
;
64 clk_data
->clk_data
.clk_num
= nr_clks
;
68 EXPORT_SYMBOL_GPL(hisi_clk_alloc
);
70 struct hisi_clock_data
*hisi_clk_init(struct device_node
*np
,
73 struct hisi_clock_data
*clk_data
;
74 struct clk
**clk_table
;
77 base
= of_iomap(np
, 0);
79 pr_err("%s: failed to map clock registers\n", __func__
);
83 clk_data
= kzalloc(sizeof(*clk_data
), GFP_KERNEL
);
87 clk_data
->base
= base
;
88 clk_table
= kcalloc(nr_clks
, sizeof(*clk_table
), GFP_KERNEL
);
92 clk_data
->clk_data
.clks
= clk_table
;
93 clk_data
->clk_data
.clk_num
= nr_clks
;
94 of_clk_add_provider(np
, of_clk_src_onecell_get
, &clk_data
->clk_data
);
101 EXPORT_SYMBOL_GPL(hisi_clk_init
);
103 int hisi_clk_register_fixed_rate(const struct hisi_fixed_rate_clock
*clks
,
104 int nums
, struct hisi_clock_data
*data
)
109 for (i
= 0; i
< nums
; i
++) {
110 clk
= clk_register_fixed_rate(NULL
, clks
[i
].name
,
115 pr_err("%s: failed to register clock %s\n",
116 __func__
, clks
[i
].name
);
119 data
->clk_data
.clks
[clks
[i
].id
] = clk
;
126 clk_unregister_fixed_rate(data
->clk_data
.clks
[clks
[i
].id
]);
130 EXPORT_SYMBOL_GPL(hisi_clk_register_fixed_rate
);
132 int hisi_clk_register_fixed_factor(const struct hisi_fixed_factor_clock
*clks
,
134 struct hisi_clock_data
*data
)
139 for (i
= 0; i
< nums
; i
++) {
140 clk
= clk_register_fixed_factor(NULL
, clks
[i
].name
,
142 clks
[i
].flags
, clks
[i
].mult
,
145 pr_err("%s: failed to register clock %s\n",
146 __func__
, clks
[i
].name
);
149 data
->clk_data
.clks
[clks
[i
].id
] = clk
;
156 clk_unregister_fixed_factor(data
->clk_data
.clks
[clks
[i
].id
]);
160 EXPORT_SYMBOL_GPL(hisi_clk_register_fixed_factor
);
162 int hisi_clk_register_mux(const struct hisi_mux_clock
*clks
,
163 int nums
, struct hisi_clock_data
*data
)
166 void __iomem
*base
= data
->base
;
169 for (i
= 0; i
< nums
; i
++) {
170 u32 mask
= BIT(clks
[i
].width
) - 1;
172 clk
= clk_register_mux_table(NULL
, clks
[i
].name
,
173 clks
[i
].parent_names
,
174 clks
[i
].num_parents
, clks
[i
].flags
,
175 base
+ clks
[i
].offset
, clks
[i
].shift
,
176 mask
, clks
[i
].mux_flags
,
177 clks
[i
].table
, &hisi_clk_lock
);
179 pr_err("%s: failed to register clock %s\n",
180 __func__
, clks
[i
].name
);
185 clk_register_clkdev(clk
, clks
[i
].alias
, NULL
);
187 data
->clk_data
.clks
[clks
[i
].id
] = clk
;
194 clk_unregister_mux(data
->clk_data
.clks
[clks
[i
].id
]);
198 EXPORT_SYMBOL_GPL(hisi_clk_register_mux
);
200 int hisi_clk_register_divider(const struct hisi_divider_clock
*clks
,
201 int nums
, struct hisi_clock_data
*data
)
204 void __iomem
*base
= data
->base
;
207 for (i
= 0; i
< nums
; i
++) {
208 clk
= clk_register_divider_table(NULL
, clks
[i
].name
,
211 base
+ clks
[i
].offset
,
212 clks
[i
].shift
, clks
[i
].width
,
217 pr_err("%s: failed to register clock %s\n",
218 __func__
, clks
[i
].name
);
223 clk_register_clkdev(clk
, clks
[i
].alias
, NULL
);
225 data
->clk_data
.clks
[clks
[i
].id
] = clk
;
232 clk_unregister_divider(data
->clk_data
.clks
[clks
[i
].id
]);
236 EXPORT_SYMBOL_GPL(hisi_clk_register_divider
);
238 int hisi_clk_register_gate(const struct hisi_gate_clock
*clks
,
239 int nums
, struct hisi_clock_data
*data
)
242 void __iomem
*base
= data
->base
;
245 for (i
= 0; i
< nums
; i
++) {
246 clk
= clk_register_gate(NULL
, clks
[i
].name
,
249 base
+ clks
[i
].offset
,
254 pr_err("%s: failed to register clock %s\n",
255 __func__
, clks
[i
].name
);
260 clk_register_clkdev(clk
, clks
[i
].alias
, NULL
);
262 data
->clk_data
.clks
[clks
[i
].id
] = clk
;
269 clk_unregister_gate(data
->clk_data
.clks
[clks
[i
].id
]);
273 EXPORT_SYMBOL_GPL(hisi_clk_register_gate
);
275 void hisi_clk_register_gate_sep(const struct hisi_gate_clock
*clks
,
276 int nums
, struct hisi_clock_data
*data
)
279 void __iomem
*base
= data
->base
;
282 for (i
= 0; i
< nums
; i
++) {
283 clk
= hisi_register_clkgate_sep(NULL
, clks
[i
].name
,
286 base
+ clks
[i
].offset
,
291 pr_err("%s: failed to register clock %s\n",
292 __func__
, clks
[i
].name
);
297 clk_register_clkdev(clk
, clks
[i
].alias
, NULL
);
299 data
->clk_data
.clks
[clks
[i
].id
] = clk
;
302 EXPORT_SYMBOL_GPL(hisi_clk_register_gate_sep
);
304 void __init
hi6220_clk_register_divider(const struct hi6220_divider_clock
*clks
,
305 int nums
, struct hisi_clock_data
*data
)
308 void __iomem
*base
= data
->base
;
311 for (i
= 0; i
< nums
; i
++) {
312 clk
= hi6220_register_clkdiv(NULL
, clks
[i
].name
,
315 base
+ clks
[i
].offset
,
321 pr_err("%s: failed to register clock %s\n",
322 __func__
, clks
[i
].name
);
327 clk_register_clkdev(clk
, clks
[i
].alias
, NULL
);
329 data
->clk_data
.clks
[clks
[i
].id
] = clk
;