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/clk-provider.h> 280aa0c95fSHaojian Zhuang #include <linux/clkdev.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 #include <linux/clk.h> 360aa0c95fSHaojian Zhuang 370aa0c95fSHaojian Zhuang #include "clk.h" 380aa0c95fSHaojian Zhuang 390aa0c95fSHaojian Zhuang static DEFINE_SPINLOCK(hisi_clk_lock); 400aa0c95fSHaojian Zhuang 41*75af25f5SHaojian Zhuang struct hisi_clock_data __init *hisi_clk_init(struct device_node *np, 42*75af25f5SHaojian Zhuang int nr_clks) 430aa0c95fSHaojian Zhuang { 44*75af25f5SHaojian Zhuang struct hisi_clock_data *clk_data; 45*75af25f5SHaojian Zhuang struct clk **clk_table; 46*75af25f5SHaojian Zhuang void __iomem *base; 47*75af25f5SHaojian Zhuang 48*75af25f5SHaojian Zhuang if (np) { 49*75af25f5SHaojian Zhuang base = of_iomap(np, 0); 50*75af25f5SHaojian Zhuang if (!base) { 51*75af25f5SHaojian Zhuang pr_err("failed to map Hisilicon clock registers\n"); 52*75af25f5SHaojian Zhuang goto err; 53*75af25f5SHaojian Zhuang } 54*75af25f5SHaojian Zhuang } else { 55*75af25f5SHaojian Zhuang pr_err("failed to find Hisilicon clock node in DTS\n"); 56*75af25f5SHaojian Zhuang goto err; 57*75af25f5SHaojian Zhuang } 58*75af25f5SHaojian Zhuang 59*75af25f5SHaojian Zhuang clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL); 60*75af25f5SHaojian Zhuang if (!clk_data) { 61*75af25f5SHaojian Zhuang pr_err("%s: could not allocate clock data\n", __func__); 62*75af25f5SHaojian Zhuang goto err; 63*75af25f5SHaojian Zhuang } 64*75af25f5SHaojian Zhuang clk_data->base = base; 65*75af25f5SHaojian Zhuang 660aa0c95fSHaojian Zhuang clk_table = kzalloc(sizeof(struct clk *) * nr_clks, GFP_KERNEL); 670aa0c95fSHaojian Zhuang if (!clk_table) { 680aa0c95fSHaojian Zhuang pr_err("%s: could not allocate clock lookup table\n", __func__); 69*75af25f5SHaojian Zhuang goto err_data; 700aa0c95fSHaojian Zhuang } 71*75af25f5SHaojian Zhuang clk_data->clk_data.clks = clk_table; 72*75af25f5SHaojian Zhuang clk_data->clk_data.clk_num = nr_clks; 73*75af25f5SHaojian Zhuang of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data->clk_data); 74*75af25f5SHaojian Zhuang return clk_data; 75*75af25f5SHaojian Zhuang err_data: 76*75af25f5SHaojian Zhuang kfree(clk_data); 77*75af25f5SHaojian Zhuang err: 78*75af25f5SHaojian Zhuang return NULL; 790aa0c95fSHaojian Zhuang } 800aa0c95fSHaojian Zhuang 810aa0c95fSHaojian Zhuang void __init hisi_clk_register_fixed_rate(struct hisi_fixed_rate_clock *clks, 82*75af25f5SHaojian Zhuang int nums, struct hisi_clock_data *data) 830aa0c95fSHaojian Zhuang { 840aa0c95fSHaojian Zhuang struct clk *clk; 850aa0c95fSHaojian Zhuang int i; 860aa0c95fSHaojian Zhuang 870aa0c95fSHaojian Zhuang for (i = 0; i < nums; i++) { 880aa0c95fSHaojian Zhuang clk = clk_register_fixed_rate(NULL, clks[i].name, 890aa0c95fSHaojian Zhuang clks[i].parent_name, 900aa0c95fSHaojian Zhuang clks[i].flags, 910aa0c95fSHaojian Zhuang clks[i].fixed_rate); 920aa0c95fSHaojian Zhuang if (IS_ERR(clk)) { 930aa0c95fSHaojian Zhuang pr_err("%s: failed to register clock %s\n", 940aa0c95fSHaojian Zhuang __func__, clks[i].name); 950aa0c95fSHaojian Zhuang continue; 960aa0c95fSHaojian Zhuang } 97*75af25f5SHaojian Zhuang data->clk_data.clks[clks[i].id] = clk; 980aa0c95fSHaojian Zhuang } 990aa0c95fSHaojian Zhuang } 1000aa0c95fSHaojian Zhuang 1010aa0c95fSHaojian Zhuang void __init hisi_clk_register_fixed_factor(struct hisi_fixed_factor_clock *clks, 102*75af25f5SHaojian Zhuang int nums, 103*75af25f5SHaojian Zhuang struct hisi_clock_data *data) 1040aa0c95fSHaojian Zhuang { 1050aa0c95fSHaojian Zhuang struct clk *clk; 1060aa0c95fSHaojian Zhuang int i; 1070aa0c95fSHaojian Zhuang 1080aa0c95fSHaojian Zhuang for (i = 0; i < nums; i++) { 1090aa0c95fSHaojian Zhuang clk = clk_register_fixed_factor(NULL, clks[i].name, 1100aa0c95fSHaojian Zhuang clks[i].parent_name, 1110aa0c95fSHaojian Zhuang clks[i].flags, clks[i].mult, 1120aa0c95fSHaojian Zhuang clks[i].div); 1130aa0c95fSHaojian Zhuang if (IS_ERR(clk)) { 1140aa0c95fSHaojian Zhuang pr_err("%s: failed to register clock %s\n", 1150aa0c95fSHaojian Zhuang __func__, clks[i].name); 1160aa0c95fSHaojian Zhuang continue; 1170aa0c95fSHaojian Zhuang } 118*75af25f5SHaojian Zhuang data->clk_data.clks[clks[i].id] = clk; 1190aa0c95fSHaojian Zhuang } 1200aa0c95fSHaojian Zhuang } 1210aa0c95fSHaojian Zhuang 1220aa0c95fSHaojian Zhuang void __init hisi_clk_register_mux(struct hisi_mux_clock *clks, 123*75af25f5SHaojian Zhuang int nums, struct hisi_clock_data *data) 1240aa0c95fSHaojian Zhuang { 1250aa0c95fSHaojian Zhuang struct clk *clk; 126*75af25f5SHaojian Zhuang void __iomem *base = data->base; 1270aa0c95fSHaojian Zhuang int i; 1280aa0c95fSHaojian Zhuang 1290aa0c95fSHaojian Zhuang for (i = 0; i < nums; i++) { 1300aa0c95fSHaojian Zhuang clk = clk_register_mux(NULL, clks[i].name, clks[i].parent_names, 1310aa0c95fSHaojian Zhuang clks[i].num_parents, clks[i].flags, 1320aa0c95fSHaojian Zhuang base + clks[i].offset, clks[i].shift, 1330aa0c95fSHaojian Zhuang clks[i].width, clks[i].mux_flags, 1340aa0c95fSHaojian Zhuang &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 144*75af25f5SHaojian Zhuang data->clk_data.clks[clks[i].id] = clk; 1450aa0c95fSHaojian Zhuang } 1460aa0c95fSHaojian Zhuang } 1470aa0c95fSHaojian Zhuang 1480aa0c95fSHaojian Zhuang void __init hisi_clk_register_divider(struct hisi_divider_clock *clks, 149*75af25f5SHaojian Zhuang int nums, struct hisi_clock_data *data) 1500aa0c95fSHaojian Zhuang { 1510aa0c95fSHaojian Zhuang struct clk *clk; 152*75af25f5SHaojian Zhuang void __iomem *base = data->base; 1530aa0c95fSHaojian Zhuang int i; 1540aa0c95fSHaojian Zhuang 1550aa0c95fSHaojian Zhuang for (i = 0; i < nums; i++) { 1560aa0c95fSHaojian Zhuang clk = clk_register_divider_table(NULL, clks[i].name, 1570aa0c95fSHaojian Zhuang clks[i].parent_name, 1580aa0c95fSHaojian Zhuang clks[i].flags, 1590aa0c95fSHaojian Zhuang base + clks[i].offset, 1600aa0c95fSHaojian Zhuang clks[i].shift, clks[i].width, 1610aa0c95fSHaojian Zhuang clks[i].div_flags, 1620aa0c95fSHaojian Zhuang clks[i].table, 1630aa0c95fSHaojian Zhuang &hisi_clk_lock); 1640aa0c95fSHaojian Zhuang if (IS_ERR(clk)) { 1650aa0c95fSHaojian Zhuang pr_err("%s: failed to register clock %s\n", 1660aa0c95fSHaojian Zhuang __func__, clks[i].name); 1670aa0c95fSHaojian Zhuang continue; 1680aa0c95fSHaojian Zhuang } 1690aa0c95fSHaojian Zhuang 1700aa0c95fSHaojian Zhuang if (clks[i].alias) 1710aa0c95fSHaojian Zhuang clk_register_clkdev(clk, clks[i].alias, NULL); 1720aa0c95fSHaojian Zhuang 173*75af25f5SHaojian Zhuang data->clk_data.clks[clks[i].id] = clk; 1740aa0c95fSHaojian Zhuang } 1750aa0c95fSHaojian Zhuang } 1760aa0c95fSHaojian Zhuang 1770aa0c95fSHaojian Zhuang void __init hisi_clk_register_gate_sep(struct hisi_gate_clock *clks, 178*75af25f5SHaojian Zhuang int nums, struct hisi_clock_data *data) 1790aa0c95fSHaojian Zhuang { 1800aa0c95fSHaojian Zhuang struct clk *clk; 181*75af25f5SHaojian Zhuang void __iomem *base = data->base; 1820aa0c95fSHaojian Zhuang int i; 1830aa0c95fSHaojian Zhuang 1840aa0c95fSHaojian Zhuang for (i = 0; i < nums; i++) { 1850aa0c95fSHaojian Zhuang clk = hisi_register_clkgate_sep(NULL, clks[i].name, 1860aa0c95fSHaojian Zhuang clks[i].parent_name, 1870aa0c95fSHaojian Zhuang clks[i].flags, 1880aa0c95fSHaojian Zhuang base + clks[i].offset, 1890aa0c95fSHaojian Zhuang clks[i].bit_idx, 1900aa0c95fSHaojian Zhuang clks[i].gate_flags, 1910aa0c95fSHaojian Zhuang &hisi_clk_lock); 1920aa0c95fSHaojian Zhuang if (IS_ERR(clk)) { 1930aa0c95fSHaojian Zhuang pr_err("%s: failed to register clock %s\n", 1940aa0c95fSHaojian Zhuang __func__, clks[i].name); 1950aa0c95fSHaojian Zhuang continue; 1960aa0c95fSHaojian Zhuang } 1970aa0c95fSHaojian Zhuang 1980aa0c95fSHaojian Zhuang if (clks[i].alias) 1990aa0c95fSHaojian Zhuang clk_register_clkdev(clk, clks[i].alias, NULL); 2000aa0c95fSHaojian Zhuang 201*75af25f5SHaojian Zhuang data->clk_data.clks[clks[i].id] = clk; 2020aa0c95fSHaojian Zhuang } 2030aa0c95fSHaojian Zhuang } 204