18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Hi3798CV200 Clock and Reset Generator Driver
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2016 HiSilicon Technologies Co., Ltd.
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <dt-bindings/clock/histb-clock.h>
98c2ecf20Sopenharmony_ci#include <linux/clk-provider.h>
108c2ecf20Sopenharmony_ci#include <linux/module.h>
118c2ecf20Sopenharmony_ci#include <linux/of_device.h>
128c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
138c2ecf20Sopenharmony_ci#include "clk.h"
148c2ecf20Sopenharmony_ci#include "crg.h"
158c2ecf20Sopenharmony_ci#include "reset.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci/* hi3798CV200 core CRG */
188c2ecf20Sopenharmony_ci#define HI3798CV200_INNER_CLK_OFFSET		64
198c2ecf20Sopenharmony_ci#define HI3798CV200_FIXED_24M			65
208c2ecf20Sopenharmony_ci#define HI3798CV200_FIXED_25M			66
218c2ecf20Sopenharmony_ci#define HI3798CV200_FIXED_50M			67
228c2ecf20Sopenharmony_ci#define HI3798CV200_FIXED_75M			68
238c2ecf20Sopenharmony_ci#define HI3798CV200_FIXED_100M			69
248c2ecf20Sopenharmony_ci#define HI3798CV200_FIXED_150M			70
258c2ecf20Sopenharmony_ci#define HI3798CV200_FIXED_200M			71
268c2ecf20Sopenharmony_ci#define HI3798CV200_FIXED_250M			72
278c2ecf20Sopenharmony_ci#define HI3798CV200_FIXED_300M			73
288c2ecf20Sopenharmony_ci#define HI3798CV200_FIXED_400M			74
298c2ecf20Sopenharmony_ci#define HI3798CV200_MMC_MUX			75
308c2ecf20Sopenharmony_ci#define HI3798CV200_ETH_PUB_CLK			76
318c2ecf20Sopenharmony_ci#define HI3798CV200_ETH_BUS_CLK			77
328c2ecf20Sopenharmony_ci#define HI3798CV200_ETH_BUS0_CLK		78
338c2ecf20Sopenharmony_ci#define HI3798CV200_ETH_BUS1_CLK		79
348c2ecf20Sopenharmony_ci#define HI3798CV200_COMBPHY1_MUX		80
358c2ecf20Sopenharmony_ci#define HI3798CV200_FIXED_12M			81
368c2ecf20Sopenharmony_ci#define HI3798CV200_FIXED_48M			82
378c2ecf20Sopenharmony_ci#define HI3798CV200_FIXED_60M			83
388c2ecf20Sopenharmony_ci#define HI3798CV200_FIXED_166P5M		84
398c2ecf20Sopenharmony_ci#define HI3798CV200_SDIO0_MUX			85
408c2ecf20Sopenharmony_ci#define HI3798CV200_COMBPHY0_MUX		86
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define HI3798CV200_CRG_NR_CLKS			128
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistatic const struct hisi_fixed_rate_clock hi3798cv200_fixed_rate_clks[] = {
458c2ecf20Sopenharmony_ci	{ HISTB_OSC_CLK, "clk_osc", NULL, 0, 24000000, },
468c2ecf20Sopenharmony_ci	{ HISTB_APB_CLK, "clk_apb", NULL, 0, 100000000, },
478c2ecf20Sopenharmony_ci	{ HISTB_AHB_CLK, "clk_ahb", NULL, 0, 200000000, },
488c2ecf20Sopenharmony_ci	{ HI3798CV200_FIXED_12M, "12m", NULL, 0, 12000000, },
498c2ecf20Sopenharmony_ci	{ HI3798CV200_FIXED_24M, "24m", NULL, 0, 24000000, },
508c2ecf20Sopenharmony_ci	{ HI3798CV200_FIXED_25M, "25m", NULL, 0, 25000000, },
518c2ecf20Sopenharmony_ci	{ HI3798CV200_FIXED_48M, "48m", NULL, 0, 48000000, },
528c2ecf20Sopenharmony_ci	{ HI3798CV200_FIXED_50M, "50m", NULL, 0, 50000000, },
538c2ecf20Sopenharmony_ci	{ HI3798CV200_FIXED_60M, "60m", NULL, 0, 60000000, },
548c2ecf20Sopenharmony_ci	{ HI3798CV200_FIXED_75M, "75m", NULL, 0, 75000000, },
558c2ecf20Sopenharmony_ci	{ HI3798CV200_FIXED_100M, "100m", NULL, 0, 100000000, },
568c2ecf20Sopenharmony_ci	{ HI3798CV200_FIXED_150M, "150m", NULL, 0, 150000000, },
578c2ecf20Sopenharmony_ci	{ HI3798CV200_FIXED_166P5M, "166p5m", NULL, 0, 165000000, },
588c2ecf20Sopenharmony_ci	{ HI3798CV200_FIXED_200M, "200m", NULL, 0, 200000000, },
598c2ecf20Sopenharmony_ci	{ HI3798CV200_FIXED_250M, "250m", NULL, 0, 250000000, },
608c2ecf20Sopenharmony_ci};
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic const char *const mmc_mux_p[] = {
638c2ecf20Sopenharmony_ci		"100m", "50m", "25m", "200m", "150m" };
648c2ecf20Sopenharmony_cistatic u32 mmc_mux_table[] = {0, 1, 2, 3, 6};
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic const char *const comphy_mux_p[] = {
678c2ecf20Sopenharmony_ci		"100m", "25m"};
688c2ecf20Sopenharmony_cistatic u32 comphy_mux_table[] = {2, 3};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic const char *const sdio_mux_p[] = {
718c2ecf20Sopenharmony_ci		"100m", "50m", "150m", "166p5m" };
728c2ecf20Sopenharmony_cistatic u32 sdio_mux_table[] = {0, 1, 2, 3};
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic struct hisi_mux_clock hi3798cv200_mux_clks[] = {
758c2ecf20Sopenharmony_ci	{ HI3798CV200_MMC_MUX, "mmc_mux", mmc_mux_p, ARRAY_SIZE(mmc_mux_p),
768c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xa0, 8, 3, 0, mmc_mux_table, },
778c2ecf20Sopenharmony_ci	{ HI3798CV200_COMBPHY0_MUX, "combphy0_mux",
788c2ecf20Sopenharmony_ci		comphy_mux_p, ARRAY_SIZE(comphy_mux_p),
798c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0x188, 2, 2, 0, comphy_mux_table, },
808c2ecf20Sopenharmony_ci	{ HI3798CV200_COMBPHY1_MUX, "combphy1_mux",
818c2ecf20Sopenharmony_ci		comphy_mux_p, ARRAY_SIZE(comphy_mux_p),
828c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0x188, 10, 2, 0, comphy_mux_table, },
838c2ecf20Sopenharmony_ci	{ HI3798CV200_SDIO0_MUX, "sdio0_mux", sdio_mux_p,
848c2ecf20Sopenharmony_ci		ARRAY_SIZE(sdio_mux_p), CLK_SET_RATE_PARENT,
858c2ecf20Sopenharmony_ci		0x9c, 8, 2, 0, sdio_mux_table, },
868c2ecf20Sopenharmony_ci};
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_cistatic u32 mmc_phase_regvals[] = {0, 1, 2, 3, 4, 5, 6, 7};
898c2ecf20Sopenharmony_cistatic u32 mmc_phase_degrees[] = {0, 45, 90, 135, 180, 225, 270, 315};
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic struct hisi_phase_clock hi3798cv200_phase_clks[] = {
928c2ecf20Sopenharmony_ci	{ HISTB_MMC_SAMPLE_CLK, "mmc_sample", "clk_mmc_ciu",
938c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xa0, 12, 3, mmc_phase_degrees,
948c2ecf20Sopenharmony_ci		mmc_phase_regvals, ARRAY_SIZE(mmc_phase_regvals) },
958c2ecf20Sopenharmony_ci	{ HISTB_MMC_DRV_CLK, "mmc_drive", "clk_mmc_ciu",
968c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xa0, 16, 3, mmc_phase_degrees,
978c2ecf20Sopenharmony_ci		mmc_phase_regvals, ARRAY_SIZE(mmc_phase_regvals) },
988c2ecf20Sopenharmony_ci};
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_cistatic const struct hisi_gate_clock hi3798cv200_gate_clks[] = {
1018c2ecf20Sopenharmony_ci	/* UART */
1028c2ecf20Sopenharmony_ci	{ HISTB_UART2_CLK, "clk_uart2", "75m",
1038c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0x68, 4, 0, },
1048c2ecf20Sopenharmony_ci	/* I2C */
1058c2ecf20Sopenharmony_ci	{ HISTB_I2C0_CLK, "clk_i2c0", "clk_apb",
1068c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0x6C, 4, 0, },
1078c2ecf20Sopenharmony_ci	{ HISTB_I2C1_CLK, "clk_i2c1", "clk_apb",
1088c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0x6C, 8, 0, },
1098c2ecf20Sopenharmony_ci	{ HISTB_I2C2_CLK, "clk_i2c2", "clk_apb",
1108c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0x6C, 12, 0, },
1118c2ecf20Sopenharmony_ci	{ HISTB_I2C3_CLK, "clk_i2c3", "clk_apb",
1128c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0x6C, 16, 0, },
1138c2ecf20Sopenharmony_ci	{ HISTB_I2C4_CLK, "clk_i2c4", "clk_apb",
1148c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0x6C, 20, 0, },
1158c2ecf20Sopenharmony_ci	/* SPI */
1168c2ecf20Sopenharmony_ci	{ HISTB_SPI0_CLK, "clk_spi0", "clk_apb",
1178c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0x70, 0, 0, },
1188c2ecf20Sopenharmony_ci	/* SDIO */
1198c2ecf20Sopenharmony_ci	{ HISTB_SDIO0_BIU_CLK, "clk_sdio0_biu", "200m",
1208c2ecf20Sopenharmony_ci			CLK_SET_RATE_PARENT, 0x9c, 0, 0, },
1218c2ecf20Sopenharmony_ci	{ HISTB_SDIO0_CIU_CLK, "clk_sdio0_ciu", "sdio0_mux",
1228c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0x9c, 1, 0, },
1238c2ecf20Sopenharmony_ci	/* EMMC */
1248c2ecf20Sopenharmony_ci	{ HISTB_MMC_BIU_CLK, "clk_mmc_biu", "200m",
1258c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xa0, 0, 0, },
1268c2ecf20Sopenharmony_ci	{ HISTB_MMC_CIU_CLK, "clk_mmc_ciu", "mmc_mux",
1278c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xa0, 1, 0, },
1288c2ecf20Sopenharmony_ci	/* PCIE*/
1298c2ecf20Sopenharmony_ci	{ HISTB_PCIE_BUS_CLK, "clk_pcie_bus", "200m",
1308c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0x18c, 0, 0, },
1318c2ecf20Sopenharmony_ci	{ HISTB_PCIE_SYS_CLK, "clk_pcie_sys", "100m",
1328c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0x18c, 1, 0, },
1338c2ecf20Sopenharmony_ci	{ HISTB_PCIE_PIPE_CLK, "clk_pcie_pipe", "250m",
1348c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0x18c, 2, 0, },
1358c2ecf20Sopenharmony_ci	{ HISTB_PCIE_AUX_CLK, "clk_pcie_aux", "24m",
1368c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0x18c, 3, 0, },
1378c2ecf20Sopenharmony_ci	/* Ethernet */
1388c2ecf20Sopenharmony_ci	{ HI3798CV200_ETH_PUB_CLK, "clk_pub", NULL,
1398c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xcc, 5, 0, },
1408c2ecf20Sopenharmony_ci	{ HI3798CV200_ETH_BUS_CLK, "clk_bus", "clk_pub",
1418c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xcc, 0, 0, },
1428c2ecf20Sopenharmony_ci	{ HI3798CV200_ETH_BUS0_CLK, "clk_bus_m0", "clk_bus",
1438c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xcc, 1, 0, },
1448c2ecf20Sopenharmony_ci	{ HI3798CV200_ETH_BUS1_CLK, "clk_bus_m1", "clk_bus",
1458c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xcc, 2, 0, },
1468c2ecf20Sopenharmony_ci	{ HISTB_ETH0_MAC_CLK, "clk_mac0", "clk_bus_m0",
1478c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xcc, 3, 0, },
1488c2ecf20Sopenharmony_ci	{ HISTB_ETH0_MACIF_CLK, "clk_macif0", "clk_bus_m0",
1498c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xcc, 24, 0, },
1508c2ecf20Sopenharmony_ci	{ HISTB_ETH1_MAC_CLK, "clk_mac1", "clk_bus_m1",
1518c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xcc, 4, 0, },
1528c2ecf20Sopenharmony_ci	{ HISTB_ETH1_MACIF_CLK, "clk_macif1", "clk_bus_m1",
1538c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xcc, 25, 0, },
1548c2ecf20Sopenharmony_ci	/* COMBPHY0 */
1558c2ecf20Sopenharmony_ci	{ HISTB_COMBPHY0_CLK, "clk_combphy0", "combphy0_mux",
1568c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0x188, 0, 0, },
1578c2ecf20Sopenharmony_ci	/* COMBPHY1 */
1588c2ecf20Sopenharmony_ci	{ HISTB_COMBPHY1_CLK, "clk_combphy1", "combphy1_mux",
1598c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0x188, 8, 0, },
1608c2ecf20Sopenharmony_ci	/* USB2 */
1618c2ecf20Sopenharmony_ci	{ HISTB_USB2_BUS_CLK, "clk_u2_bus", "clk_ahb",
1628c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xb8, 0, 0, },
1638c2ecf20Sopenharmony_ci	{ HISTB_USB2_PHY_CLK, "clk_u2_phy", "60m",
1648c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xb8, 4, 0, },
1658c2ecf20Sopenharmony_ci	{ HISTB_USB2_12M_CLK, "clk_u2_12m", "12m",
1668c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xb8, 2, 0 },
1678c2ecf20Sopenharmony_ci	{ HISTB_USB2_48M_CLK, "clk_u2_48m", "48m",
1688c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xb8, 1, 0 },
1698c2ecf20Sopenharmony_ci	{ HISTB_USB2_UTMI_CLK, "clk_u2_utmi", "60m",
1708c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xb8, 5, 0 },
1718c2ecf20Sopenharmony_ci	{ HISTB_USB2_OTG_UTMI_CLK, "clk_u2_otg_utmi", "60m",
1728c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xb8, 3, 0 },
1738c2ecf20Sopenharmony_ci	{ HISTB_USB2_PHY1_REF_CLK, "clk_u2_phy1_ref", "24m",
1748c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xbc, 0, 0 },
1758c2ecf20Sopenharmony_ci	{ HISTB_USB2_PHY2_REF_CLK, "clk_u2_phy2_ref", "24m",
1768c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xbc, 2, 0 },
1778c2ecf20Sopenharmony_ci	/* USB3 */
1788c2ecf20Sopenharmony_ci	{ HISTB_USB3_BUS_CLK, "clk_u3_bus", NULL,
1798c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xb0, 0, 0 },
1808c2ecf20Sopenharmony_ci	{ HISTB_USB3_UTMI_CLK, "clk_u3_utmi", NULL,
1818c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xb0, 4, 0 },
1828c2ecf20Sopenharmony_ci	{ HISTB_USB3_PIPE_CLK, "clk_u3_pipe", NULL,
1838c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xb0, 3, 0 },
1848c2ecf20Sopenharmony_ci	{ HISTB_USB3_SUSPEND_CLK, "clk_u3_suspend", NULL,
1858c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xb0, 2, 0 },
1868c2ecf20Sopenharmony_ci	{ HISTB_USB3_BUS_CLK1, "clk_u3_bus1", NULL,
1878c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xb0, 16, 0 },
1888c2ecf20Sopenharmony_ci	{ HISTB_USB3_UTMI_CLK1, "clk_u3_utmi1", NULL,
1898c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xb0, 20, 0 },
1908c2ecf20Sopenharmony_ci	{ HISTB_USB3_PIPE_CLK1, "clk_u3_pipe1", NULL,
1918c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xb0, 19, 0 },
1928c2ecf20Sopenharmony_ci	{ HISTB_USB3_SUSPEND_CLK1, "clk_u3_suspend1", NULL,
1938c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0xb0, 18, 0 },
1948c2ecf20Sopenharmony_ci};
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistatic struct hisi_clock_data *hi3798cv200_clk_register(
1978c2ecf20Sopenharmony_ci				struct platform_device *pdev)
1988c2ecf20Sopenharmony_ci{
1998c2ecf20Sopenharmony_ci	struct hisi_clock_data *clk_data;
2008c2ecf20Sopenharmony_ci	int ret;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	clk_data = hisi_clk_alloc(pdev, HI3798CV200_CRG_NR_CLKS);
2038c2ecf20Sopenharmony_ci	if (!clk_data)
2048c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	/* hisi_phase_clock is resource managed */
2078c2ecf20Sopenharmony_ci	ret = hisi_clk_register_phase(&pdev->dev,
2088c2ecf20Sopenharmony_ci				hi3798cv200_phase_clks,
2098c2ecf20Sopenharmony_ci				ARRAY_SIZE(hi3798cv200_phase_clks),
2108c2ecf20Sopenharmony_ci				clk_data);
2118c2ecf20Sopenharmony_ci	if (ret)
2128c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	ret = hisi_clk_register_fixed_rate(hi3798cv200_fixed_rate_clks,
2158c2ecf20Sopenharmony_ci				     ARRAY_SIZE(hi3798cv200_fixed_rate_clks),
2168c2ecf20Sopenharmony_ci				     clk_data);
2178c2ecf20Sopenharmony_ci	if (ret)
2188c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	ret = hisi_clk_register_mux(hi3798cv200_mux_clks,
2218c2ecf20Sopenharmony_ci				ARRAY_SIZE(hi3798cv200_mux_clks),
2228c2ecf20Sopenharmony_ci				clk_data);
2238c2ecf20Sopenharmony_ci	if (ret)
2248c2ecf20Sopenharmony_ci		goto unregister_fixed_rate;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	ret = hisi_clk_register_gate(hi3798cv200_gate_clks,
2278c2ecf20Sopenharmony_ci				ARRAY_SIZE(hi3798cv200_gate_clks),
2288c2ecf20Sopenharmony_ci				clk_data);
2298c2ecf20Sopenharmony_ci	if (ret)
2308c2ecf20Sopenharmony_ci		goto unregister_mux;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	ret = of_clk_add_provider(pdev->dev.of_node,
2338c2ecf20Sopenharmony_ci			of_clk_src_onecell_get, &clk_data->clk_data);
2348c2ecf20Sopenharmony_ci	if (ret)
2358c2ecf20Sopenharmony_ci		goto unregister_gate;
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	return clk_data;
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ciunregister_gate:
2408c2ecf20Sopenharmony_ci	hisi_clk_unregister_gate(hi3798cv200_gate_clks,
2418c2ecf20Sopenharmony_ci				ARRAY_SIZE(hi3798cv200_gate_clks),
2428c2ecf20Sopenharmony_ci				clk_data);
2438c2ecf20Sopenharmony_ciunregister_mux:
2448c2ecf20Sopenharmony_ci	hisi_clk_unregister_mux(hi3798cv200_mux_clks,
2458c2ecf20Sopenharmony_ci				ARRAY_SIZE(hi3798cv200_mux_clks),
2468c2ecf20Sopenharmony_ci				clk_data);
2478c2ecf20Sopenharmony_ciunregister_fixed_rate:
2488c2ecf20Sopenharmony_ci	hisi_clk_unregister_fixed_rate(hi3798cv200_fixed_rate_clks,
2498c2ecf20Sopenharmony_ci				ARRAY_SIZE(hi3798cv200_fixed_rate_clks),
2508c2ecf20Sopenharmony_ci				clk_data);
2518c2ecf20Sopenharmony_ci	return ERR_PTR(ret);
2528c2ecf20Sopenharmony_ci}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_cistatic void hi3798cv200_clk_unregister(struct platform_device *pdev)
2558c2ecf20Sopenharmony_ci{
2568c2ecf20Sopenharmony_ci	struct hisi_crg_dev *crg = platform_get_drvdata(pdev);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	of_clk_del_provider(pdev->dev.of_node);
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	hisi_clk_unregister_gate(hi3798cv200_gate_clks,
2618c2ecf20Sopenharmony_ci				ARRAY_SIZE(hi3798cv200_gate_clks),
2628c2ecf20Sopenharmony_ci				crg->clk_data);
2638c2ecf20Sopenharmony_ci	hisi_clk_unregister_mux(hi3798cv200_mux_clks,
2648c2ecf20Sopenharmony_ci				ARRAY_SIZE(hi3798cv200_mux_clks),
2658c2ecf20Sopenharmony_ci				crg->clk_data);
2668c2ecf20Sopenharmony_ci	hisi_clk_unregister_fixed_rate(hi3798cv200_fixed_rate_clks,
2678c2ecf20Sopenharmony_ci				ARRAY_SIZE(hi3798cv200_fixed_rate_clks),
2688c2ecf20Sopenharmony_ci				crg->clk_data);
2698c2ecf20Sopenharmony_ci}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_cistatic const struct hisi_crg_funcs hi3798cv200_crg_funcs = {
2728c2ecf20Sopenharmony_ci	.register_clks = hi3798cv200_clk_register,
2738c2ecf20Sopenharmony_ci	.unregister_clks = hi3798cv200_clk_unregister,
2748c2ecf20Sopenharmony_ci};
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci/* hi3798CV200 sysctrl CRG */
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci#define HI3798CV200_SYSCTRL_NR_CLKS 16
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_cistatic const struct hisi_gate_clock hi3798cv200_sysctrl_gate_clks[] = {
2818c2ecf20Sopenharmony_ci	{ HISTB_IR_CLK, "clk_ir", "24m",
2828c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0x48, 4, 0, },
2838c2ecf20Sopenharmony_ci	{ HISTB_TIMER01_CLK, "clk_timer01", "24m",
2848c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0x48, 6, 0, },
2858c2ecf20Sopenharmony_ci	{ HISTB_UART0_CLK, "clk_uart0", "75m",
2868c2ecf20Sopenharmony_ci		CLK_SET_RATE_PARENT, 0x48, 10, 0, },
2878c2ecf20Sopenharmony_ci};
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_cistatic struct hisi_clock_data *hi3798cv200_sysctrl_clk_register(
2908c2ecf20Sopenharmony_ci					struct platform_device *pdev)
2918c2ecf20Sopenharmony_ci{
2928c2ecf20Sopenharmony_ci	struct hisi_clock_data *clk_data;
2938c2ecf20Sopenharmony_ci	int ret;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	clk_data = hisi_clk_alloc(pdev, HI3798CV200_SYSCTRL_NR_CLKS);
2968c2ecf20Sopenharmony_ci	if (!clk_data)
2978c2ecf20Sopenharmony_ci		return ERR_PTR(-ENOMEM);
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	ret = hisi_clk_register_gate(hi3798cv200_sysctrl_gate_clks,
3008c2ecf20Sopenharmony_ci				ARRAY_SIZE(hi3798cv200_sysctrl_gate_clks),
3018c2ecf20Sopenharmony_ci				clk_data);
3028c2ecf20Sopenharmony_ci	if (ret)
3038c2ecf20Sopenharmony_ci		return ERR_PTR(ret);
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	ret = of_clk_add_provider(pdev->dev.of_node,
3068c2ecf20Sopenharmony_ci			of_clk_src_onecell_get, &clk_data->clk_data);
3078c2ecf20Sopenharmony_ci	if (ret)
3088c2ecf20Sopenharmony_ci		goto unregister_gate;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	return clk_data;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ciunregister_gate:
3138c2ecf20Sopenharmony_ci	hisi_clk_unregister_gate(hi3798cv200_sysctrl_gate_clks,
3148c2ecf20Sopenharmony_ci				ARRAY_SIZE(hi3798cv200_sysctrl_gate_clks),
3158c2ecf20Sopenharmony_ci				clk_data);
3168c2ecf20Sopenharmony_ci	return ERR_PTR(ret);
3178c2ecf20Sopenharmony_ci}
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_cistatic void hi3798cv200_sysctrl_clk_unregister(struct platform_device *pdev)
3208c2ecf20Sopenharmony_ci{
3218c2ecf20Sopenharmony_ci	struct hisi_crg_dev *crg = platform_get_drvdata(pdev);
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	of_clk_del_provider(pdev->dev.of_node);
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	hisi_clk_unregister_gate(hi3798cv200_sysctrl_gate_clks,
3268c2ecf20Sopenharmony_ci				ARRAY_SIZE(hi3798cv200_sysctrl_gate_clks),
3278c2ecf20Sopenharmony_ci				crg->clk_data);
3288c2ecf20Sopenharmony_ci}
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_cistatic const struct hisi_crg_funcs hi3798cv200_sysctrl_funcs = {
3318c2ecf20Sopenharmony_ci	.register_clks = hi3798cv200_sysctrl_clk_register,
3328c2ecf20Sopenharmony_ci	.unregister_clks = hi3798cv200_sysctrl_clk_unregister,
3338c2ecf20Sopenharmony_ci};
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_cistatic const struct of_device_id hi3798cv200_crg_match_table[] = {
3368c2ecf20Sopenharmony_ci	{ .compatible = "hisilicon,hi3798cv200-crg",
3378c2ecf20Sopenharmony_ci		.data = &hi3798cv200_crg_funcs },
3388c2ecf20Sopenharmony_ci	{ .compatible = "hisilicon,hi3798cv200-sysctrl",
3398c2ecf20Sopenharmony_ci		.data = &hi3798cv200_sysctrl_funcs },
3408c2ecf20Sopenharmony_ci	{ }
3418c2ecf20Sopenharmony_ci};
3428c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, hi3798cv200_crg_match_table);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_cistatic int hi3798cv200_crg_probe(struct platform_device *pdev)
3458c2ecf20Sopenharmony_ci{
3468c2ecf20Sopenharmony_ci	struct hisi_crg_dev *crg;
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci	crg = devm_kmalloc(&pdev->dev, sizeof(*crg), GFP_KERNEL);
3498c2ecf20Sopenharmony_ci	if (!crg)
3508c2ecf20Sopenharmony_ci		return -ENOMEM;
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	crg->funcs = of_device_get_match_data(&pdev->dev);
3538c2ecf20Sopenharmony_ci	if (!crg->funcs)
3548c2ecf20Sopenharmony_ci		return -ENOENT;
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	crg->rstc = hisi_reset_init(pdev);
3578c2ecf20Sopenharmony_ci	if (!crg->rstc)
3588c2ecf20Sopenharmony_ci		return -ENOMEM;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	crg->clk_data = crg->funcs->register_clks(pdev);
3618c2ecf20Sopenharmony_ci	if (IS_ERR(crg->clk_data)) {
3628c2ecf20Sopenharmony_ci		hisi_reset_exit(crg->rstc);
3638c2ecf20Sopenharmony_ci		return PTR_ERR(crg->clk_data);
3648c2ecf20Sopenharmony_ci	}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, crg);
3678c2ecf20Sopenharmony_ci	return 0;
3688c2ecf20Sopenharmony_ci}
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_cistatic int hi3798cv200_crg_remove(struct platform_device *pdev)
3718c2ecf20Sopenharmony_ci{
3728c2ecf20Sopenharmony_ci	struct hisi_crg_dev *crg = platform_get_drvdata(pdev);
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	hisi_reset_exit(crg->rstc);
3758c2ecf20Sopenharmony_ci	crg->funcs->unregister_clks(pdev);
3768c2ecf20Sopenharmony_ci	return 0;
3778c2ecf20Sopenharmony_ci}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_cistatic struct platform_driver hi3798cv200_crg_driver = {
3808c2ecf20Sopenharmony_ci	.probe          = hi3798cv200_crg_probe,
3818c2ecf20Sopenharmony_ci	.remove		= hi3798cv200_crg_remove,
3828c2ecf20Sopenharmony_ci	.driver         = {
3838c2ecf20Sopenharmony_ci		.name   = "hi3798cv200-crg",
3848c2ecf20Sopenharmony_ci		.of_match_table = hi3798cv200_crg_match_table,
3858c2ecf20Sopenharmony_ci	},
3868c2ecf20Sopenharmony_ci};
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_cistatic int __init hi3798cv200_crg_init(void)
3898c2ecf20Sopenharmony_ci{
3908c2ecf20Sopenharmony_ci	return platform_driver_register(&hi3798cv200_crg_driver);
3918c2ecf20Sopenharmony_ci}
3928c2ecf20Sopenharmony_cicore_initcall(hi3798cv200_crg_init);
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_cistatic void __exit hi3798cv200_crg_exit(void)
3958c2ecf20Sopenharmony_ci{
3968c2ecf20Sopenharmony_ci	platform_driver_unregister(&hi3798cv200_crg_driver);
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_cimodule_exit(hi3798cv200_crg_exit);
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
4018c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("HiSilicon Hi3798CV200 CRG Driver");
402