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(&pdev
->dev
, sizeof(struct clk
*) * nr_clks
,
62 clk_data
->clk_data
.clks
= clk_table
;
63 clk_data
->clk_data
.clk_num
= nr_clks
;
67 EXPORT_SYMBOL_GPL(hisi_clk_alloc
);
69 struct hisi_clock_data
*hisi_clk_init(struct device_node
*np
,
72 struct hisi_clock_data
*clk_data
;
73 struct clk
**clk_table
;
76 base
= of_iomap(np
, 0);
78 pr_err("%s: failed to map clock registers\n", __func__
);
82 clk_data
= kzalloc(sizeof(*clk_data
), GFP_KERNEL
);
84 pr_err("%s: could not allocate clock data\n", __func__
);
87 clk_data
->base
= base
;
89 clk_table
= kzalloc(sizeof(struct clk
*) * nr_clks
, GFP_KERNEL
);
91 pr_err("%s: could not allocate clock lookup table\n", __func__
);
94 clk_data
->clk_data
.clks
= clk_table
;
95 clk_data
->clk_data
.clk_num
= nr_clks
;
96 of_clk_add_provider(np
, of_clk_src_onecell_get
, &clk_data
->clk_data
);
103 EXPORT_SYMBOL_GPL(hisi_clk_init
);
105 int hisi_clk_register_fixed_rate(const struct hisi_fixed_rate_clock
*clks
,
106 int nums
, struct hisi_clock_data
*data
)
111 for (i
= 0; i
< nums
; i
++) {
112 clk
= clk_register_fixed_rate(NULL
, clks
[i
].name
,
117 pr_err("%s: failed to register clock %s\n",
118 __func__
, clks
[i
].name
);
121 data
->clk_data
.clks
[clks
[i
].id
] = clk
;
128 clk_unregister_fixed_rate(data
->clk_data
.clks
[clks
[i
].id
]);
132 EXPORT_SYMBOL_GPL(hisi_clk_register_fixed_rate
);
134 int hisi_clk_register_fixed_factor(const struct hisi_fixed_factor_clock
*clks
,
136 struct hisi_clock_data
*data
)
141 for (i
= 0; i
< nums
; i
++) {
142 clk
= clk_register_fixed_factor(NULL
, clks
[i
].name
,
144 clks
[i
].flags
, clks
[i
].mult
,
147 pr_err("%s: failed to register clock %s\n",
148 __func__
, clks
[i
].name
);
151 data
->clk_data
.clks
[clks
[i
].id
] = clk
;
158 clk_unregister_fixed_factor(data
->clk_data
.clks
[clks
[i
].id
]);
162 EXPORT_SYMBOL_GPL(hisi_clk_register_fixed_factor
);
164 int hisi_clk_register_mux(const struct hisi_mux_clock
*clks
,
165 int nums
, struct hisi_clock_data
*data
)
168 void __iomem
*base
= data
->base
;
171 for (i
= 0; i
< nums
; i
++) {
172 u32 mask
= BIT(clks
[i
].width
) - 1;
174 clk
= clk_register_mux_table(NULL
, clks
[i
].name
,
175 clks
[i
].parent_names
,
176 clks
[i
].num_parents
, clks
[i
].flags
,
177 base
+ clks
[i
].offset
, clks
[i
].shift
,
178 mask
, clks
[i
].mux_flags
,
179 clks
[i
].table
, &hisi_clk_lock
);
181 pr_err("%s: failed to register clock %s\n",
182 __func__
, clks
[i
].name
);
187 clk_register_clkdev(clk
, clks
[i
].alias
, NULL
);
189 data
->clk_data
.clks
[clks
[i
].id
] = clk
;
196 clk_unregister_mux(data
->clk_data
.clks
[clks
[i
].id
]);
200 EXPORT_SYMBOL_GPL(hisi_clk_register_mux
);
202 int hisi_clk_register_divider(const struct hisi_divider_clock
*clks
,
203 int nums
, struct hisi_clock_data
*data
)
206 void __iomem
*base
= data
->base
;
209 for (i
= 0; i
< nums
; i
++) {
210 clk
= clk_register_divider_table(NULL
, clks
[i
].name
,
213 base
+ clks
[i
].offset
,
214 clks
[i
].shift
, clks
[i
].width
,
219 pr_err("%s: failed to register clock %s\n",
220 __func__
, clks
[i
].name
);
225 clk_register_clkdev(clk
, clks
[i
].alias
, NULL
);
227 data
->clk_data
.clks
[clks
[i
].id
] = clk
;
234 clk_unregister_divider(data
->clk_data
.clks
[clks
[i
].id
]);
238 EXPORT_SYMBOL_GPL(hisi_clk_register_divider
);
240 int hisi_clk_register_gate(const struct hisi_gate_clock
*clks
,
241 int nums
, struct hisi_clock_data
*data
)
244 void __iomem
*base
= data
->base
;
247 for (i
= 0; i
< nums
; i
++) {
248 clk
= clk_register_gate(NULL
, clks
[i
].name
,
251 base
+ clks
[i
].offset
,
256 pr_err("%s: failed to register clock %s\n",
257 __func__
, clks
[i
].name
);
262 clk_register_clkdev(clk
, clks
[i
].alias
, NULL
);
264 data
->clk_data
.clks
[clks
[i
].id
] = clk
;
271 clk_unregister_gate(data
->clk_data
.clks
[clks
[i
].id
]);
275 EXPORT_SYMBOL_GPL(hisi_clk_register_gate
);
277 void hisi_clk_register_gate_sep(const struct hisi_gate_clock
*clks
,
278 int nums
, struct hisi_clock_data
*data
)
281 void __iomem
*base
= data
->base
;
284 for (i
= 0; i
< nums
; i
++) {
285 clk
= hisi_register_clkgate_sep(NULL
, clks
[i
].name
,
288 base
+ clks
[i
].offset
,
293 pr_err("%s: failed to register clock %s\n",
294 __func__
, clks
[i
].name
);
299 clk_register_clkdev(clk
, clks
[i
].alias
, NULL
);
301 data
->clk_data
.clks
[clks
[i
].id
] = clk
;
304 EXPORT_SYMBOL_GPL(hisi_clk_register_gate_sep
);
306 void __init
hi6220_clk_register_divider(const struct hi6220_divider_clock
*clks
,
307 int nums
, struct hisi_clock_data
*data
)
310 void __iomem
*base
= data
->base
;
313 for (i
= 0; i
< nums
; i
++) {
314 clk
= hi6220_register_clkdiv(NULL
, clks
[i
].name
,
317 base
+ clks
[i
].offset
,
323 pr_err("%s: failed to register clock %s\n",
324 __func__
, clks
[i
].name
);
329 clk_register_clkdev(clk
, clks
[i
].alias
, NULL
);
331 data
->clk_data
.clks
[clks
[i
].id
] = clk
;