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 4075af25f5SHaojian Zhuang struct hisi_clock_data __init *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) { 49*1fb6dd9dSLeo 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 } 740aa0c95fSHaojian Zhuang 750aa0c95fSHaojian Zhuang void __init hisi_clk_register_fixed_rate(struct hisi_fixed_rate_clock *clks, 7675af25f5SHaojian Zhuang int nums, struct hisi_clock_data *data) 770aa0c95fSHaojian Zhuang { 780aa0c95fSHaojian Zhuang struct clk *clk; 790aa0c95fSHaojian Zhuang int i; 800aa0c95fSHaojian Zhuang 810aa0c95fSHaojian Zhuang for (i = 0; i < nums; i++) { 820aa0c95fSHaojian Zhuang clk = clk_register_fixed_rate(NULL, clks[i].name, 830aa0c95fSHaojian Zhuang clks[i].parent_name, 840aa0c95fSHaojian Zhuang clks[i].flags, 850aa0c95fSHaojian Zhuang clks[i].fixed_rate); 860aa0c95fSHaojian Zhuang if (IS_ERR(clk)) { 870aa0c95fSHaojian Zhuang pr_err("%s: failed to register clock %s\n", 880aa0c95fSHaojian Zhuang __func__, clks[i].name); 890aa0c95fSHaojian Zhuang continue; 900aa0c95fSHaojian Zhuang } 9175af25f5SHaojian Zhuang data->clk_data.clks[clks[i].id] = clk; 920aa0c95fSHaojian Zhuang } 930aa0c95fSHaojian Zhuang } 940aa0c95fSHaojian Zhuang 950aa0c95fSHaojian Zhuang void __init hisi_clk_register_fixed_factor(struct hisi_fixed_factor_clock *clks, 9675af25f5SHaojian Zhuang int nums, 9775af25f5SHaojian Zhuang struct hisi_clock_data *data) 980aa0c95fSHaojian Zhuang { 990aa0c95fSHaojian Zhuang struct clk *clk; 1000aa0c95fSHaojian Zhuang int i; 1010aa0c95fSHaojian Zhuang 1020aa0c95fSHaojian Zhuang for (i = 0; i < nums; i++) { 1030aa0c95fSHaojian Zhuang clk = clk_register_fixed_factor(NULL, clks[i].name, 1040aa0c95fSHaojian Zhuang clks[i].parent_name, 1050aa0c95fSHaojian Zhuang clks[i].flags, clks[i].mult, 1060aa0c95fSHaojian Zhuang clks[i].div); 1070aa0c95fSHaojian Zhuang if (IS_ERR(clk)) { 1080aa0c95fSHaojian Zhuang pr_err("%s: failed to register clock %s\n", 1090aa0c95fSHaojian Zhuang __func__, clks[i].name); 1100aa0c95fSHaojian Zhuang continue; 1110aa0c95fSHaojian Zhuang } 11275af25f5SHaojian Zhuang data->clk_data.clks[clks[i].id] = clk; 1130aa0c95fSHaojian Zhuang } 1140aa0c95fSHaojian Zhuang } 1150aa0c95fSHaojian Zhuang 1160aa0c95fSHaojian Zhuang void __init hisi_clk_register_mux(struct hisi_mux_clock *clks, 11775af25f5SHaojian Zhuang int nums, struct hisi_clock_data *data) 1180aa0c95fSHaojian Zhuang { 1190aa0c95fSHaojian Zhuang struct clk *clk; 12075af25f5SHaojian Zhuang void __iomem *base = data->base; 1210aa0c95fSHaojian Zhuang int i; 1220aa0c95fSHaojian Zhuang 1230aa0c95fSHaojian Zhuang for (i = 0; i < nums; i++) { 124156342a1SZhangfei Gao u32 mask = BIT(clks[i].width) - 1; 125156342a1SZhangfei Gao 126156342a1SZhangfei Gao clk = clk_register_mux_table(NULL, clks[i].name, 127156342a1SZhangfei Gao clks[i].parent_names, 1280aa0c95fSHaojian Zhuang clks[i].num_parents, clks[i].flags, 1290aa0c95fSHaojian Zhuang base + clks[i].offset, clks[i].shift, 130156342a1SZhangfei Gao mask, clks[i].mux_flags, 131156342a1SZhangfei Gao clks[i].table, &hisi_clk_lock); 1320aa0c95fSHaojian Zhuang if (IS_ERR(clk)) { 1330aa0c95fSHaojian Zhuang pr_err("%s: failed to register clock %s\n", 1340aa0c95fSHaojian Zhuang __func__, clks[i].name); 1350aa0c95fSHaojian Zhuang continue; 1360aa0c95fSHaojian Zhuang } 1370aa0c95fSHaojian Zhuang 1380aa0c95fSHaojian Zhuang if (clks[i].alias) 1390aa0c95fSHaojian Zhuang clk_register_clkdev(clk, clks[i].alias, NULL); 1400aa0c95fSHaojian Zhuang 14175af25f5SHaojian Zhuang data->clk_data.clks[clks[i].id] = clk; 1420aa0c95fSHaojian Zhuang } 1430aa0c95fSHaojian Zhuang } 1440aa0c95fSHaojian Zhuang 1450aa0c95fSHaojian Zhuang void __init hisi_clk_register_divider(struct hisi_divider_clock *clks, 14675af25f5SHaojian Zhuang int nums, struct hisi_clock_data *data) 1470aa0c95fSHaojian Zhuang { 1480aa0c95fSHaojian Zhuang struct clk *clk; 14975af25f5SHaojian Zhuang void __iomem *base = data->base; 1500aa0c95fSHaojian Zhuang int i; 1510aa0c95fSHaojian Zhuang 1520aa0c95fSHaojian Zhuang for (i = 0; i < nums; i++) { 1530aa0c95fSHaojian Zhuang clk = clk_register_divider_table(NULL, clks[i].name, 1540aa0c95fSHaojian Zhuang clks[i].parent_name, 1550aa0c95fSHaojian Zhuang clks[i].flags, 1560aa0c95fSHaojian Zhuang base + clks[i].offset, 1570aa0c95fSHaojian Zhuang clks[i].shift, clks[i].width, 1580aa0c95fSHaojian Zhuang clks[i].div_flags, 1590aa0c95fSHaojian Zhuang clks[i].table, 1600aa0c95fSHaojian Zhuang &hisi_clk_lock); 1610aa0c95fSHaojian Zhuang if (IS_ERR(clk)) { 1620aa0c95fSHaojian Zhuang pr_err("%s: failed to register clock %s\n", 1630aa0c95fSHaojian Zhuang __func__, clks[i].name); 1640aa0c95fSHaojian Zhuang continue; 1650aa0c95fSHaojian Zhuang } 1660aa0c95fSHaojian Zhuang 1670aa0c95fSHaojian Zhuang if (clks[i].alias) 1680aa0c95fSHaojian Zhuang clk_register_clkdev(clk, clks[i].alias, NULL); 1690aa0c95fSHaojian Zhuang 17075af25f5SHaojian Zhuang data->clk_data.clks[clks[i].id] = clk; 1710aa0c95fSHaojian Zhuang } 1720aa0c95fSHaojian Zhuang } 1730aa0c95fSHaojian Zhuang 1748b9dcb6cSZhangfei Gao void __init hisi_clk_register_gate(struct hisi_gate_clock *clks, 1758b9dcb6cSZhangfei Gao int nums, struct hisi_clock_data *data) 1768b9dcb6cSZhangfei Gao { 1778b9dcb6cSZhangfei Gao struct clk *clk; 1788b9dcb6cSZhangfei Gao void __iomem *base = data->base; 1798b9dcb6cSZhangfei Gao int i; 1808b9dcb6cSZhangfei Gao 1818b9dcb6cSZhangfei Gao for (i = 0; i < nums; i++) { 1828b9dcb6cSZhangfei Gao clk = clk_register_gate(NULL, clks[i].name, 1838b9dcb6cSZhangfei Gao clks[i].parent_name, 1848b9dcb6cSZhangfei Gao clks[i].flags, 1858b9dcb6cSZhangfei Gao base + clks[i].offset, 1868b9dcb6cSZhangfei Gao clks[i].bit_idx, 1878b9dcb6cSZhangfei Gao clks[i].gate_flags, 1888b9dcb6cSZhangfei Gao &hisi_clk_lock); 1898b9dcb6cSZhangfei Gao if (IS_ERR(clk)) { 1908b9dcb6cSZhangfei Gao pr_err("%s: failed to register clock %s\n", 1918b9dcb6cSZhangfei Gao __func__, clks[i].name); 1928b9dcb6cSZhangfei Gao continue; 1938b9dcb6cSZhangfei Gao } 1948b9dcb6cSZhangfei Gao 1958b9dcb6cSZhangfei Gao if (clks[i].alias) 1968b9dcb6cSZhangfei Gao clk_register_clkdev(clk, clks[i].alias, NULL); 1978b9dcb6cSZhangfei Gao 1988b9dcb6cSZhangfei Gao data->clk_data.clks[clks[i].id] = clk; 1998b9dcb6cSZhangfei Gao } 2008b9dcb6cSZhangfei Gao } 2018b9dcb6cSZhangfei Gao 2020aa0c95fSHaojian Zhuang void __init hisi_clk_register_gate_sep(struct hisi_gate_clock *clks, 20375af25f5SHaojian Zhuang int nums, struct hisi_clock_data *data) 2040aa0c95fSHaojian Zhuang { 2050aa0c95fSHaojian Zhuang struct clk *clk; 20675af25f5SHaojian Zhuang void __iomem *base = data->base; 2070aa0c95fSHaojian Zhuang int i; 2080aa0c95fSHaojian Zhuang 2090aa0c95fSHaojian Zhuang for (i = 0; i < nums; i++) { 2100aa0c95fSHaojian Zhuang clk = hisi_register_clkgate_sep(NULL, clks[i].name, 2110aa0c95fSHaojian Zhuang clks[i].parent_name, 2120aa0c95fSHaojian Zhuang clks[i].flags, 2130aa0c95fSHaojian Zhuang base + clks[i].offset, 2140aa0c95fSHaojian Zhuang clks[i].bit_idx, 2150aa0c95fSHaojian Zhuang clks[i].gate_flags, 2160aa0c95fSHaojian Zhuang &hisi_clk_lock); 2170aa0c95fSHaojian Zhuang if (IS_ERR(clk)) { 2180aa0c95fSHaojian Zhuang pr_err("%s: failed to register clock %s\n", 2190aa0c95fSHaojian Zhuang __func__, clks[i].name); 2200aa0c95fSHaojian Zhuang continue; 2210aa0c95fSHaojian Zhuang } 2220aa0c95fSHaojian Zhuang 2230aa0c95fSHaojian Zhuang if (clks[i].alias) 2240aa0c95fSHaojian Zhuang clk_register_clkdev(clk, clks[i].alias, NULL); 2250aa0c95fSHaojian Zhuang 22675af25f5SHaojian Zhuang data->clk_data.clks[clks[i].id] = clk; 2270aa0c95fSHaojian Zhuang } 2280aa0c95fSHaojian Zhuang } 22972ea4861SBintian Wang 23072ea4861SBintian Wang void __init hi6220_clk_register_divider(struct hi6220_divider_clock *clks, 23172ea4861SBintian Wang int nums, struct hisi_clock_data *data) 23272ea4861SBintian Wang { 23372ea4861SBintian Wang struct clk *clk; 23472ea4861SBintian Wang void __iomem *base = data->base; 23572ea4861SBintian Wang int i; 23672ea4861SBintian Wang 23772ea4861SBintian Wang for (i = 0; i < nums; i++) { 23872ea4861SBintian Wang clk = hi6220_register_clkdiv(NULL, clks[i].name, 23972ea4861SBintian Wang clks[i].parent_name, 24072ea4861SBintian Wang clks[i].flags, 24172ea4861SBintian Wang base + clks[i].offset, 24272ea4861SBintian Wang clks[i].shift, 24372ea4861SBintian Wang clks[i].width, 24472ea4861SBintian Wang clks[i].mask_bit, 24572ea4861SBintian Wang &hisi_clk_lock); 24672ea4861SBintian Wang if (IS_ERR(clk)) { 24772ea4861SBintian Wang pr_err("%s: failed to register clock %s\n", 24872ea4861SBintian Wang __func__, clks[i].name); 24972ea4861SBintian Wang continue; 25072ea4861SBintian Wang } 25172ea4861SBintian Wang 25272ea4861SBintian Wang if (clks[i].alias) 25372ea4861SBintian Wang clk_register_clkdev(clk, clks[i].alias, NULL); 25472ea4861SBintian Wang 25572ea4861SBintian Wang data->clk_data.clks[clks[i].id] = clk; 25672ea4861SBintian Wang } 25772ea4861SBintian Wang } 258