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 4032226916SJiancheng Xue struct hisi_clock_data *hisi_clk_alloc(struct platform_device *pdev, 4132226916SJiancheng Xue int nr_clks) 4232226916SJiancheng Xue { 4332226916SJiancheng Xue struct hisi_clock_data *clk_data; 4432226916SJiancheng Xue struct resource *res; 4532226916SJiancheng Xue struct clk **clk_table; 4632226916SJiancheng Xue 4732226916SJiancheng Xue clk_data = devm_kmalloc(&pdev->dev, sizeof(*clk_data), GFP_KERNEL); 4832226916SJiancheng Xue if (!clk_data) 4932226916SJiancheng Xue return NULL; 5032226916SJiancheng Xue 5132226916SJiancheng Xue res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 5232226916SJiancheng Xue clk_data->base = devm_ioremap(&pdev->dev, 5332226916SJiancheng Xue res->start, resource_size(res)); 5432226916SJiancheng Xue if (!clk_data->base) 5532226916SJiancheng Xue return NULL; 5632226916SJiancheng Xue 5732226916SJiancheng Xue clk_table = devm_kmalloc(&pdev->dev, sizeof(struct clk *) * nr_clks, 5832226916SJiancheng Xue GFP_KERNEL); 5932226916SJiancheng Xue if (!clk_table) 6032226916SJiancheng Xue return NULL; 6132226916SJiancheng Xue 6232226916SJiancheng Xue clk_data->clk_data.clks = clk_table; 6332226916SJiancheng Xue clk_data->clk_data.clk_num = nr_clks; 6432226916SJiancheng Xue 6532226916SJiancheng Xue return clk_data; 6632226916SJiancheng Xue } 6732226916SJiancheng Xue EXPORT_SYMBOL_GPL(hisi_clk_alloc); 6832226916SJiancheng Xue 69f6ff57c8SJiancheng Xue struct hisi_clock_data *hisi_clk_init(struct device_node *np, 7075af25f5SHaojian Zhuang int nr_clks) 710aa0c95fSHaojian Zhuang { 7275af25f5SHaojian Zhuang struct hisi_clock_data *clk_data; 7375af25f5SHaojian Zhuang struct clk **clk_table; 7475af25f5SHaojian Zhuang void __iomem *base; 7575af25f5SHaojian Zhuang 7675af25f5SHaojian Zhuang base = of_iomap(np, 0); 7775af25f5SHaojian Zhuang if (!base) { 781fb6dd9dSLeo Yan pr_err("%s: failed to map clock registers\n", __func__); 7975af25f5SHaojian Zhuang goto err; 8075af25f5SHaojian Zhuang } 8175af25f5SHaojian Zhuang 8275af25f5SHaojian Zhuang clk_data = kzalloc(sizeof(*clk_data), GFP_KERNEL); 8375af25f5SHaojian Zhuang if (!clk_data) { 8475af25f5SHaojian Zhuang pr_err("%s: could not allocate clock data\n", __func__); 8575af25f5SHaojian Zhuang goto err; 8675af25f5SHaojian Zhuang } 8775af25f5SHaojian Zhuang clk_data->base = base; 88*7b9bae17SMarkus Elfring clk_table = kcalloc(nr_clks, sizeof(*clk_table), GFP_KERNEL); 890aa0c95fSHaojian Zhuang if (!clk_table) { 900aa0c95fSHaojian Zhuang pr_err("%s: could not allocate clock lookup table\n", __func__); 9175af25f5SHaojian Zhuang goto err_data; 920aa0c95fSHaojian Zhuang } 9375af25f5SHaojian Zhuang clk_data->clk_data.clks = clk_table; 9475af25f5SHaojian Zhuang clk_data->clk_data.clk_num = nr_clks; 9575af25f5SHaojian Zhuang of_clk_add_provider(np, of_clk_src_onecell_get, &clk_data->clk_data); 9675af25f5SHaojian Zhuang return clk_data; 9775af25f5SHaojian Zhuang err_data: 9875af25f5SHaojian Zhuang kfree(clk_data); 9975af25f5SHaojian Zhuang err: 10075af25f5SHaojian Zhuang return NULL; 1010aa0c95fSHaojian Zhuang } 102f6ff57c8SJiancheng Xue EXPORT_SYMBOL_GPL(hisi_clk_init); 1030aa0c95fSHaojian Zhuang 1045497f668SJiancheng Xue int hisi_clk_register_fixed_rate(const struct hisi_fixed_rate_clock *clks, 10575af25f5SHaojian Zhuang int nums, struct hisi_clock_data *data) 1060aa0c95fSHaojian Zhuang { 1070aa0c95fSHaojian Zhuang struct clk *clk; 1080aa0c95fSHaojian Zhuang int i; 1090aa0c95fSHaojian Zhuang 1100aa0c95fSHaojian Zhuang for (i = 0; i < nums; i++) { 1110aa0c95fSHaojian Zhuang clk = clk_register_fixed_rate(NULL, clks[i].name, 1120aa0c95fSHaojian Zhuang clks[i].parent_name, 1130aa0c95fSHaojian Zhuang clks[i].flags, 1140aa0c95fSHaojian Zhuang clks[i].fixed_rate); 1150aa0c95fSHaojian Zhuang if (IS_ERR(clk)) { 1160aa0c95fSHaojian Zhuang pr_err("%s: failed to register clock %s\n", 1170aa0c95fSHaojian Zhuang __func__, clks[i].name); 1185497f668SJiancheng Xue goto err; 1190aa0c95fSHaojian Zhuang } 12075af25f5SHaojian Zhuang data->clk_data.clks[clks[i].id] = clk; 1210aa0c95fSHaojian Zhuang } 1225497f668SJiancheng Xue 1235497f668SJiancheng Xue return 0; 1245497f668SJiancheng Xue 1255497f668SJiancheng Xue err: 1265497f668SJiancheng Xue while (i--) 1275497f668SJiancheng Xue clk_unregister_fixed_rate(data->clk_data.clks[clks[i].id]); 1285497f668SJiancheng Xue 1295497f668SJiancheng Xue return PTR_ERR(clk); 1300aa0c95fSHaojian Zhuang } 131f6ff57c8SJiancheng Xue EXPORT_SYMBOL_GPL(hisi_clk_register_fixed_rate); 1320aa0c95fSHaojian Zhuang 1335497f668SJiancheng Xue int hisi_clk_register_fixed_factor(const struct hisi_fixed_factor_clock *clks, 13475af25f5SHaojian Zhuang int nums, 13575af25f5SHaojian Zhuang struct hisi_clock_data *data) 1360aa0c95fSHaojian Zhuang { 1370aa0c95fSHaojian Zhuang struct clk *clk; 1380aa0c95fSHaojian Zhuang int i; 1390aa0c95fSHaojian Zhuang 1400aa0c95fSHaojian Zhuang for (i = 0; i < nums; i++) { 1410aa0c95fSHaojian Zhuang clk = clk_register_fixed_factor(NULL, clks[i].name, 1420aa0c95fSHaojian Zhuang clks[i].parent_name, 1430aa0c95fSHaojian Zhuang clks[i].flags, clks[i].mult, 1440aa0c95fSHaojian Zhuang clks[i].div); 1450aa0c95fSHaojian Zhuang if (IS_ERR(clk)) { 1460aa0c95fSHaojian Zhuang pr_err("%s: failed to register clock %s\n", 1470aa0c95fSHaojian Zhuang __func__, clks[i].name); 1485497f668SJiancheng Xue goto err; 1490aa0c95fSHaojian Zhuang } 15075af25f5SHaojian Zhuang data->clk_data.clks[clks[i].id] = clk; 1510aa0c95fSHaojian Zhuang } 1525497f668SJiancheng Xue 1535497f668SJiancheng Xue return 0; 1545497f668SJiancheng Xue 1555497f668SJiancheng Xue err: 1565497f668SJiancheng Xue while (i--) 1575497f668SJiancheng Xue clk_unregister_fixed_factor(data->clk_data.clks[clks[i].id]); 1585497f668SJiancheng Xue 1595497f668SJiancheng Xue return PTR_ERR(clk); 1600aa0c95fSHaojian Zhuang } 161f6ff57c8SJiancheng Xue EXPORT_SYMBOL_GPL(hisi_clk_register_fixed_factor); 1620aa0c95fSHaojian Zhuang 1635497f668SJiancheng Xue int hisi_clk_register_mux(const struct hisi_mux_clock *clks, 16475af25f5SHaojian Zhuang int nums, struct hisi_clock_data *data) 1650aa0c95fSHaojian Zhuang { 1660aa0c95fSHaojian Zhuang struct clk *clk; 16775af25f5SHaojian Zhuang void __iomem *base = data->base; 1680aa0c95fSHaojian Zhuang int i; 1690aa0c95fSHaojian Zhuang 1700aa0c95fSHaojian Zhuang for (i = 0; i < nums; i++) { 171156342a1SZhangfei Gao u32 mask = BIT(clks[i].width) - 1; 172156342a1SZhangfei Gao 173156342a1SZhangfei Gao clk = clk_register_mux_table(NULL, clks[i].name, 174156342a1SZhangfei Gao clks[i].parent_names, 1750aa0c95fSHaojian Zhuang clks[i].num_parents, clks[i].flags, 1760aa0c95fSHaojian Zhuang base + clks[i].offset, clks[i].shift, 177156342a1SZhangfei Gao mask, clks[i].mux_flags, 178156342a1SZhangfei Gao clks[i].table, &hisi_clk_lock); 1790aa0c95fSHaojian Zhuang if (IS_ERR(clk)) { 1800aa0c95fSHaojian Zhuang pr_err("%s: failed to register clock %s\n", 1810aa0c95fSHaojian Zhuang __func__, clks[i].name); 1825497f668SJiancheng Xue goto err; 1830aa0c95fSHaojian Zhuang } 1840aa0c95fSHaojian Zhuang 1850aa0c95fSHaojian Zhuang if (clks[i].alias) 1860aa0c95fSHaojian Zhuang clk_register_clkdev(clk, clks[i].alias, NULL); 1870aa0c95fSHaojian Zhuang 18875af25f5SHaojian Zhuang data->clk_data.clks[clks[i].id] = clk; 1890aa0c95fSHaojian Zhuang } 1905497f668SJiancheng Xue 1915497f668SJiancheng Xue return 0; 1925497f668SJiancheng Xue 1935497f668SJiancheng Xue err: 1945497f668SJiancheng Xue while (i--) 1955497f668SJiancheng Xue clk_unregister_mux(data->clk_data.clks[clks[i].id]); 1965497f668SJiancheng Xue 1975497f668SJiancheng Xue return PTR_ERR(clk); 1980aa0c95fSHaojian Zhuang } 199f6ff57c8SJiancheng Xue EXPORT_SYMBOL_GPL(hisi_clk_register_mux); 2000aa0c95fSHaojian Zhuang 2015497f668SJiancheng Xue int hisi_clk_register_divider(const struct hisi_divider_clock *clks, 20275af25f5SHaojian Zhuang int nums, struct hisi_clock_data *data) 2030aa0c95fSHaojian Zhuang { 2040aa0c95fSHaojian Zhuang struct clk *clk; 20575af25f5SHaojian Zhuang void __iomem *base = data->base; 2060aa0c95fSHaojian Zhuang int i; 2070aa0c95fSHaojian Zhuang 2080aa0c95fSHaojian Zhuang for (i = 0; i < nums; i++) { 2090aa0c95fSHaojian Zhuang clk = clk_register_divider_table(NULL, clks[i].name, 2100aa0c95fSHaojian Zhuang clks[i].parent_name, 2110aa0c95fSHaojian Zhuang clks[i].flags, 2120aa0c95fSHaojian Zhuang base + clks[i].offset, 2130aa0c95fSHaojian Zhuang clks[i].shift, clks[i].width, 2140aa0c95fSHaojian Zhuang clks[i].div_flags, 2150aa0c95fSHaojian Zhuang clks[i].table, 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); 2205497f668SJiancheng Xue goto err; 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 } 2285497f668SJiancheng Xue 2295497f668SJiancheng Xue return 0; 2305497f668SJiancheng Xue 2315497f668SJiancheng Xue err: 2325497f668SJiancheng Xue while (i--) 2335497f668SJiancheng Xue clk_unregister_divider(data->clk_data.clks[clks[i].id]); 2345497f668SJiancheng Xue 2355497f668SJiancheng Xue return PTR_ERR(clk); 2360aa0c95fSHaojian Zhuang } 237f6ff57c8SJiancheng Xue EXPORT_SYMBOL_GPL(hisi_clk_register_divider); 2380aa0c95fSHaojian Zhuang 2395497f668SJiancheng Xue int hisi_clk_register_gate(const struct hisi_gate_clock *clks, 2408b9dcb6cSZhangfei Gao int nums, struct hisi_clock_data *data) 2418b9dcb6cSZhangfei Gao { 2428b9dcb6cSZhangfei Gao struct clk *clk; 2438b9dcb6cSZhangfei Gao void __iomem *base = data->base; 2448b9dcb6cSZhangfei Gao int i; 2458b9dcb6cSZhangfei Gao 2468b9dcb6cSZhangfei Gao for (i = 0; i < nums; i++) { 2478b9dcb6cSZhangfei Gao clk = clk_register_gate(NULL, clks[i].name, 2488b9dcb6cSZhangfei Gao clks[i].parent_name, 2498b9dcb6cSZhangfei Gao clks[i].flags, 2508b9dcb6cSZhangfei Gao base + clks[i].offset, 2518b9dcb6cSZhangfei Gao clks[i].bit_idx, 2528b9dcb6cSZhangfei Gao clks[i].gate_flags, 2538b9dcb6cSZhangfei Gao &hisi_clk_lock); 2548b9dcb6cSZhangfei Gao if (IS_ERR(clk)) { 2558b9dcb6cSZhangfei Gao pr_err("%s: failed to register clock %s\n", 2568b9dcb6cSZhangfei Gao __func__, clks[i].name); 2575497f668SJiancheng Xue goto err; 2588b9dcb6cSZhangfei Gao } 2598b9dcb6cSZhangfei Gao 2608b9dcb6cSZhangfei Gao if (clks[i].alias) 2618b9dcb6cSZhangfei Gao clk_register_clkdev(clk, clks[i].alias, NULL); 2628b9dcb6cSZhangfei Gao 2638b9dcb6cSZhangfei Gao data->clk_data.clks[clks[i].id] = clk; 2648b9dcb6cSZhangfei Gao } 2655497f668SJiancheng Xue 2665497f668SJiancheng Xue return 0; 2675497f668SJiancheng Xue 2685497f668SJiancheng Xue err: 2695497f668SJiancheng Xue while (i--) 2705497f668SJiancheng Xue clk_unregister_gate(data->clk_data.clks[clks[i].id]); 2715497f668SJiancheng Xue 2725497f668SJiancheng Xue return PTR_ERR(clk); 2738b9dcb6cSZhangfei Gao } 274f6ff57c8SJiancheng Xue EXPORT_SYMBOL_GPL(hisi_clk_register_gate); 2758b9dcb6cSZhangfei Gao 276f6ff57c8SJiancheng Xue void hisi_clk_register_gate_sep(const struct hisi_gate_clock *clks, 27775af25f5SHaojian Zhuang int nums, struct hisi_clock_data *data) 2780aa0c95fSHaojian Zhuang { 2790aa0c95fSHaojian Zhuang struct clk *clk; 28075af25f5SHaojian Zhuang void __iomem *base = data->base; 2810aa0c95fSHaojian Zhuang int i; 2820aa0c95fSHaojian Zhuang 2830aa0c95fSHaojian Zhuang for (i = 0; i < nums; i++) { 2840aa0c95fSHaojian Zhuang clk = hisi_register_clkgate_sep(NULL, clks[i].name, 2850aa0c95fSHaojian Zhuang clks[i].parent_name, 2860aa0c95fSHaojian Zhuang clks[i].flags, 2870aa0c95fSHaojian Zhuang base + clks[i].offset, 2880aa0c95fSHaojian Zhuang clks[i].bit_idx, 2890aa0c95fSHaojian Zhuang clks[i].gate_flags, 2900aa0c95fSHaojian Zhuang &hisi_clk_lock); 2910aa0c95fSHaojian Zhuang if (IS_ERR(clk)) { 2920aa0c95fSHaojian Zhuang pr_err("%s: failed to register clock %s\n", 2930aa0c95fSHaojian Zhuang __func__, clks[i].name); 2940aa0c95fSHaojian Zhuang continue; 2950aa0c95fSHaojian Zhuang } 2960aa0c95fSHaojian Zhuang 2970aa0c95fSHaojian Zhuang if (clks[i].alias) 2980aa0c95fSHaojian Zhuang clk_register_clkdev(clk, clks[i].alias, NULL); 2990aa0c95fSHaojian Zhuang 30075af25f5SHaojian Zhuang data->clk_data.clks[clks[i].id] = clk; 3010aa0c95fSHaojian Zhuang } 3020aa0c95fSHaojian Zhuang } 303f6ff57c8SJiancheng Xue EXPORT_SYMBOL_GPL(hisi_clk_register_gate_sep); 30472ea4861SBintian Wang 305f6ff57c8SJiancheng Xue void __init hi6220_clk_register_divider(const struct hi6220_divider_clock *clks, 30672ea4861SBintian Wang int nums, struct hisi_clock_data *data) 30772ea4861SBintian Wang { 30872ea4861SBintian Wang struct clk *clk; 30972ea4861SBintian Wang void __iomem *base = data->base; 31072ea4861SBintian Wang int i; 31172ea4861SBintian Wang 31272ea4861SBintian Wang for (i = 0; i < nums; i++) { 31372ea4861SBintian Wang clk = hi6220_register_clkdiv(NULL, clks[i].name, 31472ea4861SBintian Wang clks[i].parent_name, 31572ea4861SBintian Wang clks[i].flags, 31672ea4861SBintian Wang base + clks[i].offset, 31772ea4861SBintian Wang clks[i].shift, 31872ea4861SBintian Wang clks[i].width, 31972ea4861SBintian Wang clks[i].mask_bit, 32072ea4861SBintian Wang &hisi_clk_lock); 32172ea4861SBintian Wang if (IS_ERR(clk)) { 32272ea4861SBintian Wang pr_err("%s: failed to register clock %s\n", 32372ea4861SBintian Wang __func__, clks[i].name); 32472ea4861SBintian Wang continue; 32572ea4861SBintian Wang } 32672ea4861SBintian Wang 32772ea4861SBintian Wang if (clks[i].alias) 32872ea4861SBintian Wang clk_register_clkdev(clk, clks[i].alias, NULL); 32972ea4861SBintian Wang 33072ea4861SBintian Wang data->clk_data.clks[clks[i].id] = clk; 33172ea4861SBintian Wang } 33272ea4861SBintian Wang } 333