xref: /openbmc/linux/drivers/clk/hisilicon/clk.c (revision f6ff57c8abde68c0bb8e0d4539f4204fc5ba45ae)
10aa0c95fSHaojian Zhuang /*
20aa0c95fSHaojian Zhuang  * Hisilicon clock driver
30aa0c95fSHaojian Zhuang  *
40aa0c95fSHaojian Zhuang  * Copyright (c) 2012-2013 Hisilicon Limited.
50aa0c95fSHaojian Zhuang  * Copyright (c) 2012-2013 Linaro Limited.
60aa0c95fSHaojian Zhuang  *
70aa0c95fSHaojian Zhuang  * Author: Haojian Zhuang <haojian.zhuang@linaro.org>
80aa0c95fSHaojian Zhuang  *	   Xin Li <li.xin@linaro.org>
90aa0c95fSHaojian Zhuang  *
100aa0c95fSHaojian Zhuang  * This program is free software; you can redistribute it and/or modify
110aa0c95fSHaojian Zhuang  * it under the terms of the GNU General Public License as published by
120aa0c95fSHaojian Zhuang  * the Free Software Foundation; either version 2 of the License, or
130aa0c95fSHaojian Zhuang  * (at your option) any later version.
140aa0c95fSHaojian Zhuang  *
150aa0c95fSHaojian Zhuang  * This program is distributed in the hope that it will be useful,
160aa0c95fSHaojian Zhuang  * but WITHOUT ANY WARRANTY; without even the implied warranty of
170aa0c95fSHaojian Zhuang  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
180aa0c95fSHaojian Zhuang  * GNU General Public License for more details.
190aa0c95fSHaojian Zhuang  *
200aa0c95fSHaojian Zhuang  * You should have received a copy of the GNU General Public License along
210aa0c95fSHaojian Zhuang  * with this program; if not, write to the Free Software Foundation, Inc.,
220aa0c95fSHaojian Zhuang  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
230aa0c95fSHaojian Zhuang  *
240aa0c95fSHaojian Zhuang  */
250aa0c95fSHaojian Zhuang 
260aa0c95fSHaojian Zhuang #include <linux/kernel.h>
270aa0c95fSHaojian Zhuang #include <linux/clkdev.h>
28593438e4SStephen Boyd #include <linux/clk-provider.h>
290aa0c95fSHaojian Zhuang #include <linux/delay.h>
300aa0c95fSHaojian Zhuang #include <linux/io.h>
310aa0c95fSHaojian Zhuang #include <linux/of.h>
320aa0c95fSHaojian Zhuang #include <linux/of_address.h>
330aa0c95fSHaojian Zhuang #include <linux/of_device.h>
340aa0c95fSHaojian Zhuang #include <linux/slab.h>
350aa0c95fSHaojian Zhuang 
360aa0c95fSHaojian Zhuang #include "clk.h"
370aa0c95fSHaojian Zhuang 
380aa0c95fSHaojian Zhuang static DEFINE_SPINLOCK(hisi_clk_lock);
390aa0c95fSHaojian Zhuang 
40*f6ff57c8SJiancheng Xue struct hisi_clock_data *hisi_clk_init(struct device_node *np,
4175af25f5SHaojian Zhuang 					     int nr_clks)
420aa0c95fSHaojian Zhuang {
4375af25f5SHaojian Zhuang 	struct hisi_clock_data *clk_data;
4475af25f5SHaojian Zhuang 	struct clk **clk_table;
4575af25f5SHaojian Zhuang 	void __iomem *base;
4675af25f5SHaojian Zhuang 
4775af25f5SHaojian Zhuang 	base = of_iomap(np, 0);
4875af25f5SHaojian Zhuang 	if (!base) {
491fb6dd9dSLeo Yan 		pr_err("%s: failed to map clock registers\n", __func__);
5075af25f5SHaojian Zhuang 		goto err;
5175af25f5SHaojian Zhuang 	}
5275af25f5SHaojian Zhuang 
5375af25f5SHaojian Zhuang 	clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL);
5475af25f5SHaojian Zhuang 	if (!clk_data) {
5575af25f5SHaojian Zhuang 		pr_err("%s: could not allocate clock data\n", __func__);
5675af25f5SHaojian Zhuang 		goto err;
5775af25f5SHaojian Zhuang 	}
5875af25f5SHaojian Zhuang 	clk_data->base = base;
5975af25f5SHaojian Zhuang 
600aa0c95fSHaojian Zhuang 	clk_table = kzalloc(sizeof(struct clk *) * nr_clks, GFP_KERNEL);
610aa0c95fSHaojian Zhuang 	if (!clk_table) {
620aa0c95fSHaojian Zhuang 		pr_err("%s: could not allocate clock lookup table\n", __func__);
6375af25f5SHaojian Zhuang 		goto err_data;
640aa0c95fSHaojian Zhuang 	}
6575af25f5SHaojian Zhuang 	clk_data->clk_data.clks = clk_table;
6675af25f5SHaojian Zhuang 	clk_data->clk_data.clk_num = nr_clks;
6775af25f5SHaojian Zhuang 	of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data->clk_data);
6875af25f5SHaojian Zhuang 	return clk_data;
6975af25f5SHaojian Zhuang err_data:
7075af25f5SHaojian Zhuang 	kfree(clk_data);
7175af25f5SHaojian Zhuang err:
7275af25f5SHaojian Zhuang 	return NULL;
730aa0c95fSHaojian Zhuang }
74*f6ff57c8SJiancheng Xue EXPORT_SYMBOL_GPL(hisi_clk_init);
750aa0c95fSHaojian Zhuang 
76*f6ff57c8SJiancheng Xue void hisi_clk_register_fixed_rate(const struct hisi_fixed_rate_clock *clks,
7775af25f5SHaojian Zhuang 					 int nums, struct hisi_clock_data *data)
780aa0c95fSHaojian Zhuang {
790aa0c95fSHaojian Zhuang 	struct clk *clk;
800aa0c95fSHaojian Zhuang 	int i;
810aa0c95fSHaojian Zhuang 
820aa0c95fSHaojian Zhuang 	for (i = 0; i < nums; i++) {
830aa0c95fSHaojian Zhuang 		clk = clk_register_fixed_rate(NULL, clks[i].name,
840aa0c95fSHaojian Zhuang 					      clks[i].parent_name,
850aa0c95fSHaojian Zhuang 					      clks[i].flags,
860aa0c95fSHaojian Zhuang 					      clks[i].fixed_rate);
870aa0c95fSHaojian Zhuang 		if (IS_ERR(clk)) {
880aa0c95fSHaojian Zhuang 			pr_err("%s: failed to register clock %s\n",
890aa0c95fSHaojian Zhuang 			       __func__, clks[i].name);
900aa0c95fSHaojian Zhuang 			continue;
910aa0c95fSHaojian Zhuang 		}
9275af25f5SHaojian Zhuang 		data->clk_data.clks[clks[i].id] = clk;
930aa0c95fSHaojian Zhuang 	}
940aa0c95fSHaojian Zhuang }
95*f6ff57c8SJiancheng Xue EXPORT_SYMBOL_GPL(hisi_clk_register_fixed_rate);
960aa0c95fSHaojian Zhuang 
97*f6ff57c8SJiancheng Xue void hisi_clk_register_fixed_factor(const struct hisi_fixed_factor_clock *clks,
9875af25f5SHaojian Zhuang 					   int nums,
9975af25f5SHaojian Zhuang 					   struct hisi_clock_data *data)
1000aa0c95fSHaojian Zhuang {
1010aa0c95fSHaojian Zhuang 	struct clk *clk;
1020aa0c95fSHaojian Zhuang 	int i;
1030aa0c95fSHaojian Zhuang 
1040aa0c95fSHaojian Zhuang 	for (i = 0; i < nums; i++) {
1050aa0c95fSHaojian Zhuang 		clk = clk_register_fixed_factor(NULL, clks[i].name,
1060aa0c95fSHaojian Zhuang 						clks[i].parent_name,
1070aa0c95fSHaojian Zhuang 						clks[i].flags, clks[i].mult,
1080aa0c95fSHaojian Zhuang 						clks[i].div);
1090aa0c95fSHaojian Zhuang 		if (IS_ERR(clk)) {
1100aa0c95fSHaojian Zhuang 			pr_err("%s: failed to register clock %s\n",
1110aa0c95fSHaojian Zhuang 			       __func__, clks[i].name);
1120aa0c95fSHaojian Zhuang 			continue;
1130aa0c95fSHaojian Zhuang 		}
11475af25f5SHaojian Zhuang 		data->clk_data.clks[clks[i].id] = clk;
1150aa0c95fSHaojian Zhuang 	}
1160aa0c95fSHaojian Zhuang }
117*f6ff57c8SJiancheng Xue EXPORT_SYMBOL_GPL(hisi_clk_register_fixed_factor);
1180aa0c95fSHaojian Zhuang 
119*f6ff57c8SJiancheng Xue void hisi_clk_register_mux(const struct hisi_mux_clock *clks,
12075af25f5SHaojian Zhuang 				  int nums, struct hisi_clock_data *data)
1210aa0c95fSHaojian Zhuang {
1220aa0c95fSHaojian Zhuang 	struct clk *clk;
12375af25f5SHaojian Zhuang 	void __iomem *base = data->base;
1240aa0c95fSHaojian Zhuang 	int i;
1250aa0c95fSHaojian Zhuang 
1260aa0c95fSHaojian Zhuang 	for (i = 0; i < nums; i++) {
127156342a1SZhangfei Gao 		u32 mask = BIT(clks[i].width) - 1;
128156342a1SZhangfei Gao 
129156342a1SZhangfei Gao 		clk = clk_register_mux_table(NULL, clks[i].name,
130156342a1SZhangfei Gao 					clks[i].parent_names,
1310aa0c95fSHaojian Zhuang 					clks[i].num_parents, clks[i].flags,
1320aa0c95fSHaojian Zhuang 					base + clks[i].offset, clks[i].shift,
133156342a1SZhangfei Gao 					mask, clks[i].mux_flags,
134156342a1SZhangfei Gao 					clks[i].table, &hisi_clk_lock);
1350aa0c95fSHaojian Zhuang 		if (IS_ERR(clk)) {
1360aa0c95fSHaojian Zhuang 			pr_err("%s: failed to register clock %s\n",
1370aa0c95fSHaojian Zhuang 			       __func__, clks[i].name);
1380aa0c95fSHaojian Zhuang 			continue;
1390aa0c95fSHaojian Zhuang 		}
1400aa0c95fSHaojian Zhuang 
1410aa0c95fSHaojian Zhuang 		if (clks[i].alias)
1420aa0c95fSHaojian Zhuang 			clk_register_clkdev(clk, clks[i].alias, NULL);
1430aa0c95fSHaojian Zhuang 
14475af25f5SHaojian Zhuang 		data->clk_data.clks[clks[i].id] = clk;
1450aa0c95fSHaojian Zhuang 	}
1460aa0c95fSHaojian Zhuang }
147*f6ff57c8SJiancheng Xue EXPORT_SYMBOL_GPL(hisi_clk_register_mux);
1480aa0c95fSHaojian Zhuang 
149*f6ff57c8SJiancheng Xue void hisi_clk_register_divider(const struct hisi_divider_clock *clks,
15075af25f5SHaojian Zhuang 				      int nums, struct hisi_clock_data *data)
1510aa0c95fSHaojian Zhuang {
1520aa0c95fSHaojian Zhuang 	struct clk *clk;
15375af25f5SHaojian Zhuang 	void __iomem *base = data->base;
1540aa0c95fSHaojian Zhuang 	int i;
1550aa0c95fSHaojian Zhuang 
1560aa0c95fSHaojian Zhuang 	for (i = 0; i < nums; i++) {
1570aa0c95fSHaojian Zhuang 		clk = clk_register_divider_table(NULL, clks[i].name,
1580aa0c95fSHaojian Zhuang 						 clks[i].parent_name,
1590aa0c95fSHaojian Zhuang 						 clks[i].flags,
1600aa0c95fSHaojian Zhuang 						 base + clks[i].offset,
1610aa0c95fSHaojian Zhuang 						 clks[i].shift, clks[i].width,
1620aa0c95fSHaojian Zhuang 						 clks[i].div_flags,
1630aa0c95fSHaojian Zhuang 						 clks[i].table,
1640aa0c95fSHaojian Zhuang 						 &hisi_clk_lock);
1650aa0c95fSHaojian Zhuang 		if (IS_ERR(clk)) {
1660aa0c95fSHaojian Zhuang 			pr_err("%s: failed to register clock %s\n",
1670aa0c95fSHaojian Zhuang 			       __func__, clks[i].name);
1680aa0c95fSHaojian Zhuang 			continue;
1690aa0c95fSHaojian Zhuang 		}
1700aa0c95fSHaojian Zhuang 
1710aa0c95fSHaojian Zhuang 		if (clks[i].alias)
1720aa0c95fSHaojian Zhuang 			clk_register_clkdev(clk, clks[i].alias, NULL);
1730aa0c95fSHaojian Zhuang 
17475af25f5SHaojian Zhuang 		data->clk_data.clks[clks[i].id] = clk;
1750aa0c95fSHaojian Zhuang 	}
1760aa0c95fSHaojian Zhuang }
177*f6ff57c8SJiancheng Xue EXPORT_SYMBOL_GPL(hisi_clk_register_divider);
1780aa0c95fSHaojian Zhuang 
179*f6ff57c8SJiancheng Xue void hisi_clk_register_gate(const struct hisi_gate_clock *clks,
1808b9dcb6cSZhangfei Gao 				       int nums, struct hisi_clock_data *data)
1818b9dcb6cSZhangfei Gao {
1828b9dcb6cSZhangfei Gao 	struct clk *clk;
1838b9dcb6cSZhangfei Gao 	void __iomem *base = data->base;
1848b9dcb6cSZhangfei Gao 	int i;
1858b9dcb6cSZhangfei Gao 
1868b9dcb6cSZhangfei Gao 	for (i = 0; i < nums; i++) {
1878b9dcb6cSZhangfei Gao 		clk = clk_register_gate(NULL, clks[i].name,
1888b9dcb6cSZhangfei Gao 						clks[i].parent_name,
1898b9dcb6cSZhangfei Gao 						clks[i].flags,
1908b9dcb6cSZhangfei Gao 						base + clks[i].offset,
1918b9dcb6cSZhangfei Gao 						clks[i].bit_idx,
1928b9dcb6cSZhangfei Gao 						clks[i].gate_flags,
1938b9dcb6cSZhangfei Gao 						&hisi_clk_lock);
1948b9dcb6cSZhangfei Gao 		if (IS_ERR(clk)) {
1958b9dcb6cSZhangfei Gao 			pr_err("%s: failed to register clock %s\n",
1968b9dcb6cSZhangfei Gao 			       __func__, clks[i].name);
1978b9dcb6cSZhangfei Gao 			continue;
1988b9dcb6cSZhangfei Gao 		}
1998b9dcb6cSZhangfei Gao 
2008b9dcb6cSZhangfei Gao 		if (clks[i].alias)
2018b9dcb6cSZhangfei Gao 			clk_register_clkdev(clk, clks[i].alias, NULL);
2028b9dcb6cSZhangfei Gao 
2038b9dcb6cSZhangfei Gao 		data->clk_data.clks[clks[i].id] = clk;
2048b9dcb6cSZhangfei Gao 	}
2058b9dcb6cSZhangfei Gao }
206*f6ff57c8SJiancheng Xue EXPORT_SYMBOL_GPL(hisi_clk_register_gate);
2078b9dcb6cSZhangfei Gao 
208*f6ff57c8SJiancheng Xue void hisi_clk_register_gate_sep(const struct hisi_gate_clock *clks,
20975af25f5SHaojian Zhuang 				       int nums, struct hisi_clock_data *data)
2100aa0c95fSHaojian Zhuang {
2110aa0c95fSHaojian Zhuang 	struct clk *clk;
21275af25f5SHaojian Zhuang 	void __iomem *base = data->base;
2130aa0c95fSHaojian Zhuang 	int i;
2140aa0c95fSHaojian Zhuang 
2150aa0c95fSHaojian Zhuang 	for (i = 0; i < nums; i++) {
2160aa0c95fSHaojian Zhuang 		clk = hisi_register_clkgate_sep(NULL, clks[i].name,
2170aa0c95fSHaojian Zhuang 						clks[i].parent_name,
2180aa0c95fSHaojian Zhuang 						clks[i].flags,
2190aa0c95fSHaojian Zhuang 						base + clks[i].offset,
2200aa0c95fSHaojian Zhuang 						clks[i].bit_idx,
2210aa0c95fSHaojian Zhuang 						clks[i].gate_flags,
2220aa0c95fSHaojian Zhuang 						&hisi_clk_lock);
2230aa0c95fSHaojian Zhuang 		if (IS_ERR(clk)) {
2240aa0c95fSHaojian Zhuang 			pr_err("%s: failed to register clock %s\n",
2250aa0c95fSHaojian Zhuang 			       __func__, clks[i].name);
2260aa0c95fSHaojian Zhuang 			continue;
2270aa0c95fSHaojian Zhuang 		}
2280aa0c95fSHaojian Zhuang 
2290aa0c95fSHaojian Zhuang 		if (clks[i].alias)
2300aa0c95fSHaojian Zhuang 			clk_register_clkdev(clk, clks[i].alias, NULL);
2310aa0c95fSHaojian Zhuang 
23275af25f5SHaojian Zhuang 		data->clk_data.clks[clks[i].id] = clk;
2330aa0c95fSHaojian Zhuang 	}
2340aa0c95fSHaojian Zhuang }
235*f6ff57c8SJiancheng Xue EXPORT_SYMBOL_GPL(hisi_clk_register_gate_sep);
23672ea4861SBintian Wang 
237*f6ff57c8SJiancheng Xue void __init hi6220_clk_register_divider(const struct hi6220_divider_clock *clks,
23872ea4861SBintian Wang 					int nums, struct hisi_clock_data *data)
23972ea4861SBintian Wang {
24072ea4861SBintian Wang 	struct clk *clk;
24172ea4861SBintian Wang 	void __iomem *base = data->base;
24272ea4861SBintian Wang 	int i;
24372ea4861SBintian Wang 
24472ea4861SBintian Wang 	for (i = 0; i < nums; i++) {
24572ea4861SBintian Wang 		clk = hi6220_register_clkdiv(NULL, clks[i].name,
24672ea4861SBintian Wang 						clks[i].parent_name,
24772ea4861SBintian Wang 						clks[i].flags,
24872ea4861SBintian Wang 						base + clks[i].offset,
24972ea4861SBintian Wang 						clks[i].shift,
25072ea4861SBintian Wang 						clks[i].width,
25172ea4861SBintian Wang 						clks[i].mask_bit,
25272ea4861SBintian Wang 						&hisi_clk_lock);
25372ea4861SBintian Wang 		if (IS_ERR(clk)) {
25472ea4861SBintian Wang 			pr_err("%s: failed to register clock %s\n",
25572ea4861SBintian Wang 			       __func__, clks[i].name);
25672ea4861SBintian Wang 			continue;
25772ea4861SBintian Wang 		}
25872ea4861SBintian Wang 
25972ea4861SBintian Wang 		if (clks[i].alias)
26072ea4861SBintian Wang 			clk_register_clkdev(clk, clks[i].alias, NULL);
26172ea4861SBintian Wang 
26272ea4861SBintian Wang 		data->clk_data.clks[clks[i].id] = clk;
26372ea4861SBintian Wang 	}
26472ea4861SBintian Wang }
265