11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
26c9da387SJiancheng Xue /*
36c9da387SJiancheng Xue * Hi3519 Clock Driver
46c9da387SJiancheng Xue *
56c9da387SJiancheng Xue * Copyright (c) 2015-2016 HiSilicon Technologies Co., Ltd.
66c9da387SJiancheng Xue */
76c9da387SJiancheng Xue
86c9da387SJiancheng Xue #include <dt-bindings/clock/hi3519-clock.h>
96c9da387SJiancheng Xue #include <linux/clk-provider.h>
106c9da387SJiancheng Xue #include <linux/module.h>
116c9da387SJiancheng Xue #include <linux/platform_device.h>
126c9da387SJiancheng Xue #include "clk.h"
136c9da387SJiancheng Xue #include "reset.h"
146c9da387SJiancheng Xue
156c9da387SJiancheng Xue #define HI3519_INNER_CLK_OFFSET 64
166c9da387SJiancheng Xue #define HI3519_FIXED_24M 65
176c9da387SJiancheng Xue #define HI3519_FIXED_50M 66
186c9da387SJiancheng Xue #define HI3519_FIXED_75M 67
196c9da387SJiancheng Xue #define HI3519_FIXED_125M 68
206c9da387SJiancheng Xue #define HI3519_FIXED_150M 69
216c9da387SJiancheng Xue #define HI3519_FIXED_200M 70
226c9da387SJiancheng Xue #define HI3519_FIXED_250M 71
236c9da387SJiancheng Xue #define HI3519_FIXED_300M 72
246c9da387SJiancheng Xue #define HI3519_FIXED_400M 73
256c9da387SJiancheng Xue #define HI3519_FMC_MUX 74
266c9da387SJiancheng Xue
276c9da387SJiancheng Xue #define HI3519_NR_CLKS 128
286c9da387SJiancheng Xue
29224b3b26SJiancheng Xue struct hi3519_crg_data {
30224b3b26SJiancheng Xue struct hisi_clock_data *clk_data;
31224b3b26SJiancheng Xue struct hisi_reset_controller *rstc;
32224b3b26SJiancheng Xue };
33224b3b26SJiancheng Xue
346c9da387SJiancheng Xue static const struct hisi_fixed_rate_clock hi3519_fixed_rate_clks[] = {
356c9da387SJiancheng Xue { HI3519_FIXED_24M, "24m", NULL, 0, 24000000, },
366c9da387SJiancheng Xue { HI3519_FIXED_50M, "50m", NULL, 0, 50000000, },
376c9da387SJiancheng Xue { HI3519_FIXED_75M, "75m", NULL, 0, 75000000, },
386c9da387SJiancheng Xue { HI3519_FIXED_125M, "125m", NULL, 0, 125000000, },
396c9da387SJiancheng Xue { HI3519_FIXED_150M, "150m", NULL, 0, 150000000, },
406c9da387SJiancheng Xue { HI3519_FIXED_200M, "200m", NULL, 0, 200000000, },
416c9da387SJiancheng Xue { HI3519_FIXED_250M, "250m", NULL, 0, 250000000, },
426c9da387SJiancheng Xue { HI3519_FIXED_300M, "300m", NULL, 0, 300000000, },
436c9da387SJiancheng Xue { HI3519_FIXED_400M, "400m", NULL, 0, 400000000, },
446c9da387SJiancheng Xue };
456c9da387SJiancheng Xue
466c9da387SJiancheng Xue static const char *const fmc_mux_p[] = {
476c9da387SJiancheng Xue "24m", "75m", "125m", "150m", "200m", "250m", "300m", "400m", };
486c9da387SJiancheng Xue static u32 fmc_mux_table[] = {0, 1, 2, 3, 4, 5, 6, 7};
496c9da387SJiancheng Xue
506c9da387SJiancheng Xue static const struct hisi_mux_clock hi3519_mux_clks[] = {
516c9da387SJiancheng Xue { HI3519_FMC_MUX, "fmc_mux", fmc_mux_p, ARRAY_SIZE(fmc_mux_p),
526c9da387SJiancheng Xue CLK_SET_RATE_PARENT, 0xc0, 2, 3, 0, fmc_mux_table, },
536c9da387SJiancheng Xue };
546c9da387SJiancheng Xue
556c9da387SJiancheng Xue static const struct hisi_gate_clock hi3519_gate_clks[] = {
566c9da387SJiancheng Xue { HI3519_FMC_CLK, "clk_fmc", "fmc_mux",
576c9da387SJiancheng Xue CLK_SET_RATE_PARENT, 0xc0, 1, 0, },
586c9da387SJiancheng Xue { HI3519_UART0_CLK, "clk_uart0", "24m",
596c9da387SJiancheng Xue CLK_SET_RATE_PARENT, 0xe4, 20, 0, },
606c9da387SJiancheng Xue { HI3519_UART1_CLK, "clk_uart1", "24m",
616c9da387SJiancheng Xue CLK_SET_RATE_PARENT, 0xe4, 21, 0, },
626c9da387SJiancheng Xue { HI3519_UART2_CLK, "clk_uart2", "24m",
636c9da387SJiancheng Xue CLK_SET_RATE_PARENT, 0xe4, 22, 0, },
646c9da387SJiancheng Xue { HI3519_UART3_CLK, "clk_uart3", "24m",
656c9da387SJiancheng Xue CLK_SET_RATE_PARENT, 0xe4, 23, 0, },
666c9da387SJiancheng Xue { HI3519_UART4_CLK, "clk_uart4", "24m",
676c9da387SJiancheng Xue CLK_SET_RATE_PARENT, 0xe4, 24, 0, },
686c9da387SJiancheng Xue { HI3519_SPI0_CLK, "clk_spi0", "50m",
696c9da387SJiancheng Xue CLK_SET_RATE_PARENT, 0xe4, 16, 0, },
706c9da387SJiancheng Xue { HI3519_SPI1_CLK, "clk_spi1", "50m",
716c9da387SJiancheng Xue CLK_SET_RATE_PARENT, 0xe4, 17, 0, },
726c9da387SJiancheng Xue { HI3519_SPI2_CLK, "clk_spi2", "50m",
736c9da387SJiancheng Xue CLK_SET_RATE_PARENT, 0xe4, 18, 0, },
746c9da387SJiancheng Xue };
756c9da387SJiancheng Xue
hi3519_clk_register(struct platform_device * pdev)76224b3b26SJiancheng Xue static struct hisi_clock_data *hi3519_clk_register(struct platform_device *pdev)
776c9da387SJiancheng Xue {
786c9da387SJiancheng Xue struct hisi_clock_data *clk_data;
79224b3b26SJiancheng Xue int ret;
806c9da387SJiancheng Xue
81224b3b26SJiancheng Xue clk_data = hisi_clk_alloc(pdev, HI3519_NR_CLKS);
82224b3b26SJiancheng Xue if (!clk_data)
83224b3b26SJiancheng Xue return ERR_PTR(-ENOMEM);
846c9da387SJiancheng Xue
85224b3b26SJiancheng Xue ret = hisi_clk_register_fixed_rate(hi3519_fixed_rate_clks,
866c9da387SJiancheng Xue ARRAY_SIZE(hi3519_fixed_rate_clks),
876c9da387SJiancheng Xue clk_data);
88224b3b26SJiancheng Xue if (ret)
89224b3b26SJiancheng Xue return ERR_PTR(ret);
906c9da387SJiancheng Xue
91224b3b26SJiancheng Xue ret = hisi_clk_register_mux(hi3519_mux_clks,
92224b3b26SJiancheng Xue ARRAY_SIZE(hi3519_mux_clks),
93224b3b26SJiancheng Xue clk_data);
94224b3b26SJiancheng Xue if (ret)
95224b3b26SJiancheng Xue goto unregister_fixed_rate;
96224b3b26SJiancheng Xue
97224b3b26SJiancheng Xue ret = hisi_clk_register_gate(hi3519_gate_clks,
98224b3b26SJiancheng Xue ARRAY_SIZE(hi3519_gate_clks),
99224b3b26SJiancheng Xue clk_data);
100224b3b26SJiancheng Xue if (ret)
101224b3b26SJiancheng Xue goto unregister_mux;
102224b3b26SJiancheng Xue
103224b3b26SJiancheng Xue ret = of_clk_add_provider(pdev->dev.of_node,
104224b3b26SJiancheng Xue of_clk_src_onecell_get, &clk_data->clk_data);
105224b3b26SJiancheng Xue if (ret)
106224b3b26SJiancheng Xue goto unregister_gate;
107224b3b26SJiancheng Xue
108224b3b26SJiancheng Xue return clk_data;
109224b3b26SJiancheng Xue
110224b3b26SJiancheng Xue unregister_fixed_rate:
111224b3b26SJiancheng Xue hisi_clk_unregister_fixed_rate(hi3519_fixed_rate_clks,
112224b3b26SJiancheng Xue ARRAY_SIZE(hi3519_fixed_rate_clks),
113224b3b26SJiancheng Xue clk_data);
114224b3b26SJiancheng Xue
115224b3b26SJiancheng Xue unregister_mux:
116224b3b26SJiancheng Xue hisi_clk_unregister_mux(hi3519_mux_clks,
117224b3b26SJiancheng Xue ARRAY_SIZE(hi3519_mux_clks),
118224b3b26SJiancheng Xue clk_data);
119224b3b26SJiancheng Xue unregister_gate:
120224b3b26SJiancheng Xue hisi_clk_unregister_gate(hi3519_gate_clks,
121224b3b26SJiancheng Xue ARRAY_SIZE(hi3519_gate_clks),
122224b3b26SJiancheng Xue clk_data);
123224b3b26SJiancheng Xue return ERR_PTR(ret);
124224b3b26SJiancheng Xue }
125224b3b26SJiancheng Xue
hi3519_clk_unregister(struct platform_device * pdev)126224b3b26SJiancheng Xue static void hi3519_clk_unregister(struct platform_device *pdev)
127224b3b26SJiancheng Xue {
128224b3b26SJiancheng Xue struct hi3519_crg_data *crg = platform_get_drvdata(pdev);
129224b3b26SJiancheng Xue
130224b3b26SJiancheng Xue of_clk_del_provider(pdev->dev.of_node);
131224b3b26SJiancheng Xue
132224b3b26SJiancheng Xue hisi_clk_unregister_gate(hi3519_gate_clks,
13374e39f52SChristophe JAILLET ARRAY_SIZE(hi3519_gate_clks),
134224b3b26SJiancheng Xue crg->clk_data);
135224b3b26SJiancheng Xue hisi_clk_unregister_mux(hi3519_mux_clks,
136224b3b26SJiancheng Xue ARRAY_SIZE(hi3519_mux_clks),
137224b3b26SJiancheng Xue crg->clk_data);
138224b3b26SJiancheng Xue hisi_clk_unregister_fixed_rate(hi3519_fixed_rate_clks,
139224b3b26SJiancheng Xue ARRAY_SIZE(hi3519_fixed_rate_clks),
140224b3b26SJiancheng Xue crg->clk_data);
141224b3b26SJiancheng Xue }
142224b3b26SJiancheng Xue
hi3519_clk_probe(struct platform_device * pdev)143224b3b26SJiancheng Xue static int hi3519_clk_probe(struct platform_device *pdev)
144224b3b26SJiancheng Xue {
145224b3b26SJiancheng Xue struct hi3519_crg_data *crg;
146224b3b26SJiancheng Xue
147224b3b26SJiancheng Xue crg = devm_kmalloc(&pdev->dev, sizeof(*crg), GFP_KERNEL);
148224b3b26SJiancheng Xue if (!crg)
149224b3b26SJiancheng Xue return -ENOMEM;
150224b3b26SJiancheng Xue
151224b3b26SJiancheng Xue crg->rstc = hisi_reset_init(pdev);
152224b3b26SJiancheng Xue if (!crg->rstc)
153224b3b26SJiancheng Xue return -ENOMEM;
154224b3b26SJiancheng Xue
155224b3b26SJiancheng Xue crg->clk_data = hi3519_clk_register(pdev);
156224b3b26SJiancheng Xue if (IS_ERR(crg->clk_data)) {
157224b3b26SJiancheng Xue hisi_reset_exit(crg->rstc);
158224b3b26SJiancheng Xue return PTR_ERR(crg->clk_data);
159224b3b26SJiancheng Xue }
160224b3b26SJiancheng Xue
161224b3b26SJiancheng Xue platform_set_drvdata(pdev, crg);
1626c9da387SJiancheng Xue return 0;
1636c9da387SJiancheng Xue }
1646c9da387SJiancheng Xue
hi3519_clk_remove(struct platform_device * pdev)165bfa8370bSUwe Kleine-König static void hi3519_clk_remove(struct platform_device *pdev)
166224b3b26SJiancheng Xue {
167224b3b26SJiancheng Xue struct hi3519_crg_data *crg = platform_get_drvdata(pdev);
168224b3b26SJiancheng Xue
169224b3b26SJiancheng Xue hisi_reset_exit(crg->rstc);
170224b3b26SJiancheng Xue hi3519_clk_unregister(pdev);
171224b3b26SJiancheng Xue }
172224b3b26SJiancheng Xue
173224b3b26SJiancheng Xue
1746c9da387SJiancheng Xue static const struct of_device_id hi3519_clk_match_table[] = {
1756c9da387SJiancheng Xue { .compatible = "hisilicon,hi3519-crg" },
1766c9da387SJiancheng Xue { }
1776c9da387SJiancheng Xue };
1786c9da387SJiancheng Xue MODULE_DEVICE_TABLE(of, hi3519_clk_match_table);
1796c9da387SJiancheng Xue
1806c9da387SJiancheng Xue static struct platform_driver hi3519_clk_driver = {
1816c9da387SJiancheng Xue .probe = hi3519_clk_probe,
182*f00b45dbSUwe Kleine-König .remove = hi3519_clk_remove,
1836c9da387SJiancheng Xue .driver = {
1846c9da387SJiancheng Xue .name = "hi3519-clk",
1856c9da387SJiancheng Xue .of_match_table = hi3519_clk_match_table,
1866c9da387SJiancheng Xue },
1876c9da387SJiancheng Xue };
1886c9da387SJiancheng Xue
hi3519_clk_init(void)1896c9da387SJiancheng Xue static int __init hi3519_clk_init(void)
1906c9da387SJiancheng Xue {
1916c9da387SJiancheng Xue return platform_driver_register(&hi3519_clk_driver);
1926c9da387SJiancheng Xue }
1936c9da387SJiancheng Xue core_initcall(hi3519_clk_init);
1946c9da387SJiancheng Xue
hi3519_clk_exit(void)195224b3b26SJiancheng Xue static void __exit hi3519_clk_exit(void)
196224b3b26SJiancheng Xue {
197224b3b26SJiancheng Xue platform_driver_unregister(&hi3519_clk_driver);
198224b3b26SJiancheng Xue }
199224b3b26SJiancheng Xue module_exit(hi3519_clk_exit);
200224b3b26SJiancheng Xue
2016c9da387SJiancheng Xue MODULE_LICENSE("GPL v2");
2026c9da387SJiancheng Xue MODULE_DESCRIPTION("HiSilicon Hi3519 Clock Driver");
203