16c9da387SJiancheng Xue /* 26c9da387SJiancheng Xue * Hi3519 Clock Driver 36c9da387SJiancheng Xue * 46c9da387SJiancheng Xue * Copyright (c) 2015-2016 HiSilicon Technologies Co., Ltd. 56c9da387SJiancheng Xue * 66c9da387SJiancheng Xue * This program is free software; you can redistribute it and/or modify 76c9da387SJiancheng Xue * it under the terms of the GNU General Public License as published by 86c9da387SJiancheng Xue * the Free Software Foundation; either version 2 of the License, or 96c9da387SJiancheng Xue * (at your option) any later version. 106c9da387SJiancheng Xue * 116c9da387SJiancheng Xue * This program is distributed in the hope that it will be useful, 126c9da387SJiancheng Xue * but WITHOUT ANY WARRANTY; without even the implied warranty of 136c9da387SJiancheng Xue * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 146c9da387SJiancheng Xue * GNU General Public License for more details. 156c9da387SJiancheng Xue * 166c9da387SJiancheng Xue * You should have received a copy of the GNU General Public License 176c9da387SJiancheng Xue * along with this program. If not, see <http://www.gnu.org/licenses/>. 186c9da387SJiancheng Xue */ 196c9da387SJiancheng Xue 206c9da387SJiancheng Xue #include <dt-bindings/clock/hi3519-clock.h> 216c9da387SJiancheng Xue #include <linux/clk-provider.h> 226c9da387SJiancheng Xue #include <linux/module.h> 236c9da387SJiancheng Xue #include <linux/platform_device.h> 246c9da387SJiancheng Xue #include "clk.h" 256c9da387SJiancheng Xue #include "reset.h" 266c9da387SJiancheng Xue 276c9da387SJiancheng Xue #define HI3519_INNER_CLK_OFFSET 64 286c9da387SJiancheng Xue #define HI3519_FIXED_24M 65 296c9da387SJiancheng Xue #define HI3519_FIXED_50M 66 306c9da387SJiancheng Xue #define HI3519_FIXED_75M 67 316c9da387SJiancheng Xue #define HI3519_FIXED_125M 68 326c9da387SJiancheng Xue #define HI3519_FIXED_150M 69 336c9da387SJiancheng Xue #define HI3519_FIXED_200M 70 346c9da387SJiancheng Xue #define HI3519_FIXED_250M 71 356c9da387SJiancheng Xue #define HI3519_FIXED_300M 72 366c9da387SJiancheng Xue #define HI3519_FIXED_400M 73 376c9da387SJiancheng Xue #define HI3519_FMC_MUX 74 386c9da387SJiancheng Xue 396c9da387SJiancheng Xue #define HI3519_NR_CLKS 128 406c9da387SJiancheng Xue 41*224b3b26SJiancheng Xue struct hi3519_crg_data { 42*224b3b26SJiancheng Xue struct hisi_clock_data *clk_data; 43*224b3b26SJiancheng Xue struct hisi_reset_controller *rstc; 44*224b3b26SJiancheng Xue }; 45*224b3b26SJiancheng Xue 466c9da387SJiancheng Xue static const struct hisi_fixed_rate_clock hi3519_fixed_rate_clks[] = { 476c9da387SJiancheng Xue { HI3519_FIXED_24M, "24m", NULL, 0, 24000000, }, 486c9da387SJiancheng Xue { HI3519_FIXED_50M, "50m", NULL, 0, 50000000, }, 496c9da387SJiancheng Xue { HI3519_FIXED_75M, "75m", NULL, 0, 75000000, }, 506c9da387SJiancheng Xue { HI3519_FIXED_125M, "125m", NULL, 0, 125000000, }, 516c9da387SJiancheng Xue { HI3519_FIXED_150M, "150m", NULL, 0, 150000000, }, 526c9da387SJiancheng Xue { HI3519_FIXED_200M, "200m", NULL, 0, 200000000, }, 536c9da387SJiancheng Xue { HI3519_FIXED_250M, "250m", NULL, 0, 250000000, }, 546c9da387SJiancheng Xue { HI3519_FIXED_300M, "300m", NULL, 0, 300000000, }, 556c9da387SJiancheng Xue { HI3519_FIXED_400M, "400m", NULL, 0, 400000000, }, 566c9da387SJiancheng Xue }; 576c9da387SJiancheng Xue 586c9da387SJiancheng Xue static const char *const fmc_mux_p[] = { 596c9da387SJiancheng Xue "24m", "75m", "125m", "150m", "200m", "250m", "300m", "400m", }; 606c9da387SJiancheng Xue static u32 fmc_mux_table[] = {0, 1, 2, 3, 4, 5, 6, 7}; 616c9da387SJiancheng Xue 626c9da387SJiancheng Xue static const struct hisi_mux_clock hi3519_mux_clks[] = { 636c9da387SJiancheng Xue { HI3519_FMC_MUX, "fmc_mux", fmc_mux_p, ARRAY_SIZE(fmc_mux_p), 646c9da387SJiancheng Xue CLK_SET_RATE_PARENT, 0xc0, 2, 3, 0, fmc_mux_table, }, 656c9da387SJiancheng Xue }; 666c9da387SJiancheng Xue 676c9da387SJiancheng Xue static const struct hisi_gate_clock hi3519_gate_clks[] = { 686c9da387SJiancheng Xue { HI3519_FMC_CLK, "clk_fmc", "fmc_mux", 696c9da387SJiancheng Xue CLK_SET_RATE_PARENT, 0xc0, 1, 0, }, 706c9da387SJiancheng Xue { HI3519_UART0_CLK, "clk_uart0", "24m", 716c9da387SJiancheng Xue CLK_SET_RATE_PARENT, 0xe4, 20, 0, }, 726c9da387SJiancheng Xue { HI3519_UART1_CLK, "clk_uart1", "24m", 736c9da387SJiancheng Xue CLK_SET_RATE_PARENT, 0xe4, 21, 0, }, 746c9da387SJiancheng Xue { HI3519_UART2_CLK, "clk_uart2", "24m", 756c9da387SJiancheng Xue CLK_SET_RATE_PARENT, 0xe4, 22, 0, }, 766c9da387SJiancheng Xue { HI3519_UART3_CLK, "clk_uart3", "24m", 776c9da387SJiancheng Xue CLK_SET_RATE_PARENT, 0xe4, 23, 0, }, 786c9da387SJiancheng Xue { HI3519_UART4_CLK, "clk_uart4", "24m", 796c9da387SJiancheng Xue CLK_SET_RATE_PARENT, 0xe4, 24, 0, }, 806c9da387SJiancheng Xue { HI3519_SPI0_CLK, "clk_spi0", "50m", 816c9da387SJiancheng Xue CLK_SET_RATE_PARENT, 0xe4, 16, 0, }, 826c9da387SJiancheng Xue { HI3519_SPI1_CLK, "clk_spi1", "50m", 836c9da387SJiancheng Xue CLK_SET_RATE_PARENT, 0xe4, 17, 0, }, 846c9da387SJiancheng Xue { HI3519_SPI2_CLK, "clk_spi2", "50m", 856c9da387SJiancheng Xue CLK_SET_RATE_PARENT, 0xe4, 18, 0, }, 866c9da387SJiancheng Xue }; 876c9da387SJiancheng Xue 88*224b3b26SJiancheng Xue static struct hisi_clock_data *hi3519_clk_register(struct platform_device *pdev) 896c9da387SJiancheng Xue { 906c9da387SJiancheng Xue struct hisi_clock_data *clk_data; 91*224b3b26SJiancheng Xue int ret; 926c9da387SJiancheng Xue 93*224b3b26SJiancheng Xue clk_data = hisi_clk_alloc(pdev, HI3519_NR_CLKS); 94*224b3b26SJiancheng Xue if (!clk_data) 95*224b3b26SJiancheng Xue return ERR_PTR(-ENOMEM); 966c9da387SJiancheng Xue 97*224b3b26SJiancheng Xue ret = hisi_clk_register_fixed_rate(hi3519_fixed_rate_clks, 986c9da387SJiancheng Xue ARRAY_SIZE(hi3519_fixed_rate_clks), 996c9da387SJiancheng Xue clk_data); 100*224b3b26SJiancheng Xue if (ret) 101*224b3b26SJiancheng Xue return ERR_PTR(ret); 1026c9da387SJiancheng Xue 103*224b3b26SJiancheng Xue ret = hisi_clk_register_mux(hi3519_mux_clks, 104*224b3b26SJiancheng Xue ARRAY_SIZE(hi3519_mux_clks), 105*224b3b26SJiancheng Xue clk_data); 106*224b3b26SJiancheng Xue if (ret) 107*224b3b26SJiancheng Xue goto unregister_fixed_rate; 108*224b3b26SJiancheng Xue 109*224b3b26SJiancheng Xue ret = hisi_clk_register_gate(hi3519_gate_clks, 110*224b3b26SJiancheng Xue ARRAY_SIZE(hi3519_gate_clks), 111*224b3b26SJiancheng Xue clk_data); 112*224b3b26SJiancheng Xue if (ret) 113*224b3b26SJiancheng Xue goto unregister_mux; 114*224b3b26SJiancheng Xue 115*224b3b26SJiancheng Xue ret = of_clk_add_provider(pdev->dev.of_node, 116*224b3b26SJiancheng Xue of_clk_src_onecell_get, &clk_data->clk_data); 117*224b3b26SJiancheng Xue if (ret) 118*224b3b26SJiancheng Xue goto unregister_gate; 119*224b3b26SJiancheng Xue 120*224b3b26SJiancheng Xue return clk_data; 121*224b3b26SJiancheng Xue 122*224b3b26SJiancheng Xue unregister_fixed_rate: 123*224b3b26SJiancheng Xue hisi_clk_unregister_fixed_rate(hi3519_fixed_rate_clks, 124*224b3b26SJiancheng Xue ARRAY_SIZE(hi3519_fixed_rate_clks), 125*224b3b26SJiancheng Xue clk_data); 126*224b3b26SJiancheng Xue 127*224b3b26SJiancheng Xue unregister_mux: 128*224b3b26SJiancheng Xue hisi_clk_unregister_mux(hi3519_mux_clks, 129*224b3b26SJiancheng Xue ARRAY_SIZE(hi3519_mux_clks), 130*224b3b26SJiancheng Xue clk_data); 131*224b3b26SJiancheng Xue unregister_gate: 132*224b3b26SJiancheng Xue hisi_clk_unregister_gate(hi3519_gate_clks, 133*224b3b26SJiancheng Xue ARRAY_SIZE(hi3519_gate_clks), 134*224b3b26SJiancheng Xue clk_data); 135*224b3b26SJiancheng Xue return ERR_PTR(ret); 136*224b3b26SJiancheng Xue } 137*224b3b26SJiancheng Xue 138*224b3b26SJiancheng Xue static void hi3519_clk_unregister(struct platform_device *pdev) 139*224b3b26SJiancheng Xue { 140*224b3b26SJiancheng Xue struct hi3519_crg_data *crg = platform_get_drvdata(pdev); 141*224b3b26SJiancheng Xue 142*224b3b26SJiancheng Xue of_clk_del_provider(pdev->dev.of_node); 143*224b3b26SJiancheng Xue 144*224b3b26SJiancheng Xue hisi_clk_unregister_gate(hi3519_gate_clks, 145*224b3b26SJiancheng Xue ARRAY_SIZE(hi3519_mux_clks), 146*224b3b26SJiancheng Xue crg->clk_data); 147*224b3b26SJiancheng Xue hisi_clk_unregister_mux(hi3519_mux_clks, 148*224b3b26SJiancheng Xue ARRAY_SIZE(hi3519_mux_clks), 149*224b3b26SJiancheng Xue crg->clk_data); 150*224b3b26SJiancheng Xue hisi_clk_unregister_fixed_rate(hi3519_fixed_rate_clks, 151*224b3b26SJiancheng Xue ARRAY_SIZE(hi3519_fixed_rate_clks), 152*224b3b26SJiancheng Xue crg->clk_data); 153*224b3b26SJiancheng Xue } 154*224b3b26SJiancheng Xue 155*224b3b26SJiancheng Xue static int hi3519_clk_probe(struct platform_device *pdev) 156*224b3b26SJiancheng Xue { 157*224b3b26SJiancheng Xue struct hi3519_crg_data *crg; 158*224b3b26SJiancheng Xue 159*224b3b26SJiancheng Xue crg = devm_kmalloc(&pdev->dev, sizeof(*crg), GFP_KERNEL); 160*224b3b26SJiancheng Xue if (!crg) 161*224b3b26SJiancheng Xue return -ENOMEM; 162*224b3b26SJiancheng Xue 163*224b3b26SJiancheng Xue crg->rstc = hisi_reset_init(pdev); 164*224b3b26SJiancheng Xue if (!crg->rstc) 165*224b3b26SJiancheng Xue return -ENOMEM; 166*224b3b26SJiancheng Xue 167*224b3b26SJiancheng Xue crg->clk_data = hi3519_clk_register(pdev); 168*224b3b26SJiancheng Xue if (IS_ERR(crg->clk_data)) { 169*224b3b26SJiancheng Xue hisi_reset_exit(crg->rstc); 170*224b3b26SJiancheng Xue return PTR_ERR(crg->clk_data); 171*224b3b26SJiancheng Xue } 172*224b3b26SJiancheng Xue 173*224b3b26SJiancheng Xue platform_set_drvdata(pdev, crg); 1746c9da387SJiancheng Xue return 0; 1756c9da387SJiancheng Xue } 1766c9da387SJiancheng Xue 177*224b3b26SJiancheng Xue static int hi3519_clk_remove(struct platform_device *pdev) 178*224b3b26SJiancheng Xue { 179*224b3b26SJiancheng Xue struct hi3519_crg_data *crg = platform_get_drvdata(pdev); 180*224b3b26SJiancheng Xue 181*224b3b26SJiancheng Xue hisi_reset_exit(crg->rstc); 182*224b3b26SJiancheng Xue hi3519_clk_unregister(pdev); 183*224b3b26SJiancheng Xue return 0; 184*224b3b26SJiancheng Xue } 185*224b3b26SJiancheng Xue 186*224b3b26SJiancheng Xue 1876c9da387SJiancheng Xue static const struct of_device_id hi3519_clk_match_table[] = { 1886c9da387SJiancheng Xue { .compatible = "hisilicon,hi3519-crg" }, 1896c9da387SJiancheng Xue { } 1906c9da387SJiancheng Xue }; 1916c9da387SJiancheng Xue MODULE_DEVICE_TABLE(of, hi3519_clk_match_table); 1926c9da387SJiancheng Xue 1936c9da387SJiancheng Xue static struct platform_driver hi3519_clk_driver = { 1946c9da387SJiancheng Xue .probe = hi3519_clk_probe, 195*224b3b26SJiancheng Xue .remove = hi3519_clk_remove, 1966c9da387SJiancheng Xue .driver = { 1976c9da387SJiancheng Xue .name = "hi3519-clk", 1986c9da387SJiancheng Xue .of_match_table = hi3519_clk_match_table, 1996c9da387SJiancheng Xue }, 2006c9da387SJiancheng Xue }; 2016c9da387SJiancheng Xue 2026c9da387SJiancheng Xue static int __init hi3519_clk_init(void) 2036c9da387SJiancheng Xue { 2046c9da387SJiancheng Xue return platform_driver_register(&hi3519_clk_driver); 2056c9da387SJiancheng Xue } 2066c9da387SJiancheng Xue core_initcall(hi3519_clk_init); 2076c9da387SJiancheng Xue 208*224b3b26SJiancheng Xue static void __exit hi3519_clk_exit(void) 209*224b3b26SJiancheng Xue { 210*224b3b26SJiancheng Xue platform_driver_unregister(&hi3519_clk_driver); 211*224b3b26SJiancheng Xue } 212*224b3b26SJiancheng Xue module_exit(hi3519_clk_exit); 213*224b3b26SJiancheng Xue 2146c9da387SJiancheng Xue MODULE_LICENSE("GPL v2"); 2156c9da387SJiancheng Xue MODULE_DESCRIPTION("HiSilicon Hi3519 Clock Driver"); 216