18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Hi3519 Clock Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2015-2016 HiSilicon Technologies Co., Ltd.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <dt-bindings/clock/hi3519-clock.h>
98c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
128c2ecf20Sopenharmony_ci#include "clk.h"
138c2ecf20Sopenharmony_ci#include "reset.h"
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#define HI3519_INNER_CLK_OFFSET	64
168c2ecf20Sopenharmony_ci#define HI3519_FIXED_24M	65
178c2ecf20Sopenharmony_ci#define HI3519_FIXED_50M	66
188c2ecf20Sopenharmony_ci#define HI3519_FIXED_75M	67
198c2ecf20Sopenharmony_ci#define HI3519_FIXED_125M	68
208c2ecf20Sopenharmony_ci#define HI3519_FIXED_150M	69
218c2ecf20Sopenharmony_ci#define HI3519_FIXED_200M	70
228c2ecf20Sopenharmony_ci#define HI3519_FIXED_250M	71
238c2ecf20Sopenharmony_ci#define HI3519_FIXED_300M	72
248c2ecf20Sopenharmony_ci#define HI3519_FIXED_400M	73
258c2ecf20Sopenharmony_ci#define HI3519_FMC_MUX		74
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define HI3519_NR_CLKS		128
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistruct hi3519_crg_data {
308c2ecf20Sopenharmony_ci	struct hisi_clock_data *clk_data;
318c2ecf20Sopenharmony_ci	struct hisi_reset_controller *rstc;
328c2ecf20Sopenharmony_ci};
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cistatic const struct hisi_fixed_rate_clock hi3519_fixed_rate_clks[] = {
358c2ecf20Sopenharmony_ci	{ HI3519_FIXED_24M, "24m", NULL, 0, 24000000, },
368c2ecf20Sopenharmony_ci	{ HI3519_FIXED_50M, "50m", NULL, 0, 50000000, },
378c2ecf20Sopenharmony_ci	{ HI3519_FIXED_75M, "75m", NULL, 0, 75000000, },
388c2ecf20Sopenharmony_ci	{ HI3519_FIXED_125M, "125m", NULL, 0, 125000000, },
398c2ecf20Sopenharmony_ci	{ HI3519_FIXED_150M, "150m", NULL, 0, 150000000, },
408c2ecf20Sopenharmony_ci	{ HI3519_FIXED_200M, "200m", NULL, 0, 200000000, },
418c2ecf20Sopenharmony_ci	{ HI3519_FIXED_250M, "250m", NULL, 0, 250000000, },
428c2ecf20Sopenharmony_ci	{ HI3519_FIXED_300M, "300m", NULL, 0, 300000000, },
438c2ecf20Sopenharmony_ci	{ HI3519_FIXED_400M, "400m", NULL, 0, 400000000, },
448c2ecf20Sopenharmony_ci};
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic const char *const fmc_mux_p[] = {
478c2ecf20Sopenharmony_ci		"24m", "75m", "125m", "150m", "200m", "250m", "300m", "400m", };
488c2ecf20Sopenharmony_cistatic u32 fmc_mux_table[] = {0, 1, 2, 3, 4, 5, 6, 7};
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_cistatic const struct hisi_mux_clock hi3519_mux_clks[] = {
518c2ecf20Sopenharmony_ci	{ HI3519_FMC_MUX, "fmc_mux", fmc_mux_p, ARRAY_SIZE(fmc_mux_p),
528c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xc0, 2, 3, 0, fmc_mux_table, },
538c2ecf20Sopenharmony_ci};
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_cistatic const struct hisi_gate_clock hi3519_gate_clks[] = {
568c2ecf20Sopenharmony_ci	{ HI3519_FMC_CLK, "clk_fmc", "fmc_mux",
578c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xc0, 1, 0, },
588c2ecf20Sopenharmony_ci	{ HI3519_UART0_CLK, "clk_uart0", "24m",
598c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xe4, 20, 0, },
608c2ecf20Sopenharmony_ci	{ HI3519_UART1_CLK, "clk_uart1", "24m",
618c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xe4, 21, 0, },
628c2ecf20Sopenharmony_ci	{ HI3519_UART2_CLK, "clk_uart2", "24m",
638c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xe4, 22, 0, },
648c2ecf20Sopenharmony_ci	{ HI3519_UART3_CLK, "clk_uart3", "24m",
658c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xe4, 23, 0, },
668c2ecf20Sopenharmony_ci	{ HI3519_UART4_CLK, "clk_uart4", "24m",
678c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xe4, 24, 0, },
688c2ecf20Sopenharmony_ci	{ HI3519_SPI0_CLK, "clk_spi0", "50m",
698c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xe4, 16, 0, },
708c2ecf20Sopenharmony_ci	{ HI3519_SPI1_CLK, "clk_spi1", "50m",
718c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xe4, 17, 0, },
728c2ecf20Sopenharmony_ci	{ HI3519_SPI2_CLK, "clk_spi2", "50m",
738c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xe4, 18, 0, },
748c2ecf20Sopenharmony_ci};
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic struct hisi_clock_data *hi3519_clk_register(struct platform_device *pdev)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	struct hisi_clock_data *clk_data;
798c2ecf20Sopenharmony_ci	int ret;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	clk_data = hisi_clk_alloc(pdev, HI3519_NR_CLKS);
828c2ecf20Sopenharmony_ci	if (!clk_data)
838c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	ret = hisi_clk_register_fixed_rate(hi3519_fixed_rate_clks,
868c2ecf20Sopenharmony_ci				     ARRAY_SIZE(hi3519_fixed_rate_clks),
878c2ecf20Sopenharmony_ci				     clk_data);
888c2ecf20Sopenharmony_ci	if (ret)
898c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	ret = hisi_clk_register_mux(hi3519_mux_clks,
928c2ecf20Sopenharmony_ci				ARRAY_SIZE(hi3519_mux_clks),
938c2ecf20Sopenharmony_ci				clk_data);
948c2ecf20Sopenharmony_ci	if (ret)
958c2ecf20Sopenharmony_ci		goto unregister_fixed_rate;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	ret = hisi_clk_register_gate(hi3519_gate_clks,
988c2ecf20Sopenharmony_ci				ARRAY_SIZE(hi3519_gate_clks),
998c2ecf20Sopenharmony_ci				clk_data);
1008c2ecf20Sopenharmony_ci	if (ret)
1018c2ecf20Sopenharmony_ci		goto unregister_mux;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	ret = of_clk_add_provider(pdev->dev.of_node,
1048c2ecf20Sopenharmony_ci			of_clk_src_onecell_get, &clk_data->clk_data);
1058c2ecf20Sopenharmony_ci	if (ret)
1068c2ecf20Sopenharmony_ci		goto unregister_gate;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	return clk_data;
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ciunregister_fixed_rate:
1118c2ecf20Sopenharmony_ci	hisi_clk_unregister_fixed_rate(hi3519_fixed_rate_clks,
1128c2ecf20Sopenharmony_ci				ARRAY_SIZE(hi3519_fixed_rate_clks),
1138c2ecf20Sopenharmony_ci				clk_data);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ciunregister_mux:
1168c2ecf20Sopenharmony_ci	hisi_clk_unregister_mux(hi3519_mux_clks,
1178c2ecf20Sopenharmony_ci				ARRAY_SIZE(hi3519_mux_clks),
1188c2ecf20Sopenharmony_ci				clk_data);
1198c2ecf20Sopenharmony_ciunregister_gate:
1208c2ecf20Sopenharmony_ci	hisi_clk_unregister_gate(hi3519_gate_clks,
1218c2ecf20Sopenharmony_ci				ARRAY_SIZE(hi3519_gate_clks),
1228c2ecf20Sopenharmony_ci				clk_data);
1238c2ecf20Sopenharmony_ci	return ERR_PTR(ret);
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic void hi3519_clk_unregister(struct platform_device *pdev)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	struct hi3519_crg_data *crg = platform_get_drvdata(pdev);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	of_clk_del_provider(pdev->dev.of_node);
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	hisi_clk_unregister_gate(hi3519_gate_clks,
1338c2ecf20Sopenharmony_ci				ARRAY_SIZE(hi3519_mux_clks),
1348c2ecf20Sopenharmony_ci				crg->clk_data);
1358c2ecf20Sopenharmony_ci	hisi_clk_unregister_mux(hi3519_mux_clks,
1368c2ecf20Sopenharmony_ci				ARRAY_SIZE(hi3519_mux_clks),
1378c2ecf20Sopenharmony_ci				crg->clk_data);
1388c2ecf20Sopenharmony_ci	hisi_clk_unregister_fixed_rate(hi3519_fixed_rate_clks,
1398c2ecf20Sopenharmony_ci				ARRAY_SIZE(hi3519_fixed_rate_clks),
1408c2ecf20Sopenharmony_ci				crg->clk_data);
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic int hi3519_clk_probe(struct platform_device *pdev)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	struct hi3519_crg_data *crg;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	crg = devm_kmalloc(&pdev->dev, sizeof(*crg), GFP_KERNEL);
1488c2ecf20Sopenharmony_ci	if (!crg)
1498c2ecf20Sopenharmony_ci		return -ENOMEM;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	crg->rstc = hisi_reset_init(pdev);
1528c2ecf20Sopenharmony_ci	if (!crg->rstc)
1538c2ecf20Sopenharmony_ci		return -ENOMEM;
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	crg->clk_data = hi3519_clk_register(pdev);
1568c2ecf20Sopenharmony_ci	if (IS_ERR(crg->clk_data)) {
1578c2ecf20Sopenharmony_ci		hisi_reset_exit(crg->rstc);
1588c2ecf20Sopenharmony_ci		return PTR_ERR(crg->clk_data);
1598c2ecf20Sopenharmony_ci	}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, crg);
1628c2ecf20Sopenharmony_ci	return 0;
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic int hi3519_clk_remove(struct platform_device *pdev)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	struct hi3519_crg_data *crg = platform_get_drvdata(pdev);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	hisi_reset_exit(crg->rstc);
1708c2ecf20Sopenharmony_ci	hi3519_clk_unregister(pdev);
1718c2ecf20Sopenharmony_ci	return 0;
1728c2ecf20Sopenharmony_ci}
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic const struct of_device_id hi3519_clk_match_table[] = {
1768c2ecf20Sopenharmony_ci	{ .compatible = "hisilicon,hi3519-crg" },
1778c2ecf20Sopenharmony_ci	{ }
1788c2ecf20Sopenharmony_ci};
1798c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, hi3519_clk_match_table);
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistatic struct platform_driver hi3519_clk_driver = {
1828c2ecf20Sopenharmony_ci	.probe          = hi3519_clk_probe,
1838c2ecf20Sopenharmony_ci	.remove		= hi3519_clk_remove,
1848c2ecf20Sopenharmony_ci	.driver         = {
1858c2ecf20Sopenharmony_ci		.name   = "hi3519-clk",
1868c2ecf20Sopenharmony_ci		.of_match_table = hi3519_clk_match_table,
1878c2ecf20Sopenharmony_ci	},
1888c2ecf20Sopenharmony_ci};
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cistatic int __init hi3519_clk_init(void)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	return platform_driver_register(&hi3519_clk_driver);
1938c2ecf20Sopenharmony_ci}
1948c2ecf20Sopenharmony_cicore_initcall(hi3519_clk_init);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistatic void __exit hi3519_clk_exit(void)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	platform_driver_unregister(&hi3519_clk_driver);
1998c2ecf20Sopenharmony_ci}
2008c2ecf20Sopenharmony_cimodule_exit(hi3519_clk_exit);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
2038c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("HiSilicon Hi3519 Clock Driver");
204