162306a36Sopenharmony_ci// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2021
462306a36Sopenharmony_ci * Author(s):
562306a36Sopenharmony_ci * Jesse Taube <Mr.Bossman075@gmail.com>
662306a36Sopenharmony_ci * Giulio Benetti <giulio.benetti@benettiengineering.com>
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci#include <linux/clk.h>
962306a36Sopenharmony_ci#include <linux/of_address.h>
1062306a36Sopenharmony_ci#include <linux/of_irq.h>
1162306a36Sopenharmony_ci#include <linux/platform_device.h>
1262306a36Sopenharmony_ci#include <dt-bindings/clock/imxrt1050-clock.h>
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include "clk.h"
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_cistatic const char * const pll_ref_sels[] = {"osc", "dummy", };
1762306a36Sopenharmony_cistatic const char * const per_sels[] = {"ipg_pdof", "osc", };
1862306a36Sopenharmony_cistatic const char * const pll1_bypass_sels[] = {"pll1_arm", "pll1_arm_ref_sel", };
1962306a36Sopenharmony_cistatic const char * const pll2_bypass_sels[] = {"pll2_sys", "pll2_sys_ref_sel", };
2062306a36Sopenharmony_cistatic const char * const pll3_bypass_sels[] = {"pll3_usb_otg", "pll3_usb_otg_ref_sel", };
2162306a36Sopenharmony_cistatic const char * const pll5_bypass_sels[] = {"pll5_video", "pll5_video_ref_sel", };
2262306a36Sopenharmony_cistatic const char *const pre_periph_sels[] = {
2362306a36Sopenharmony_ci	"pll2_sys", "pll2_pfd2_396m", "pll2_pfd0_352m", "arm_podf", };
2462306a36Sopenharmony_cistatic const char *const periph_sels[] = { "pre_periph_sel", "todo", };
2562306a36Sopenharmony_cistatic const char *const usdhc_sels[] = { "pll2_pfd2_396m", "pll2_pfd0_352m", };
2662306a36Sopenharmony_cistatic const char *const lpuart_sels[] = { "pll3_80m", "osc", };
2762306a36Sopenharmony_cistatic const char *const lcdif_sels[] = {
2862306a36Sopenharmony_ci	"pll2_sys", "pll3_pfd3_454_74m", "pll5_video", "pll2_pfd0_352m",
2962306a36Sopenharmony_ci	"pll2_pfd1_594m", "pll3_pfd1_664_62m", };
3062306a36Sopenharmony_cistatic const char *const semc_alt_sels[] = { "pll2_pfd2_396m", "pll3_pfd1_664_62m", };
3162306a36Sopenharmony_cistatic const char *const semc_sels[] = { "periph_sel", "semc_alt_sel", };
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_cistatic struct clk_hw **hws;
3462306a36Sopenharmony_cistatic struct clk_hw_onecell_data *clk_hw_data;
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_cistatic int imxrt1050_clocks_probe(struct platform_device *pdev)
3762306a36Sopenharmony_ci{
3862306a36Sopenharmony_ci	void __iomem *ccm_base;
3962306a36Sopenharmony_ci	void __iomem *pll_base;
4062306a36Sopenharmony_ci	struct device *dev = &pdev->dev;
4162306a36Sopenharmony_ci	struct device_node *np = dev->of_node;
4262306a36Sopenharmony_ci	struct device_node *anp;
4362306a36Sopenharmony_ci	int ret;
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci	clk_hw_data = devm_kzalloc(dev, struct_size(clk_hw_data, hws,
4662306a36Sopenharmony_ci					  IMXRT1050_CLK_END), GFP_KERNEL);
4762306a36Sopenharmony_ci	if (WARN_ON(!clk_hw_data))
4862306a36Sopenharmony_ci		return -ENOMEM;
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci	clk_hw_data->num = IMXRT1050_CLK_END;
5162306a36Sopenharmony_ci	hws = clk_hw_data->hws;
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	hws[IMXRT1050_CLK_OSC] = imx_get_clk_hw_by_name(np, "osc");
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	anp = of_find_compatible_node(NULL, NULL, "fsl,imxrt-anatop");
5662306a36Sopenharmony_ci	pll_base = devm_of_iomap(dev, anp, 0, NULL);
5762306a36Sopenharmony_ci	of_node_put(anp);
5862306a36Sopenharmony_ci	if (WARN_ON(IS_ERR(pll_base))) {
5962306a36Sopenharmony_ci		ret = PTR_ERR(pll_base);
6062306a36Sopenharmony_ci		goto unregister_hws;
6162306a36Sopenharmony_ci	}
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	/* Anatop clocks */
6462306a36Sopenharmony_ci	hws[IMXRT1050_CLK_DUMMY] = imx_clk_hw_fixed("dummy", 0UL);
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PLL1_REF_SEL] = imx_clk_hw_mux("pll1_arm_ref_sel",
6762306a36Sopenharmony_ci		pll_base + 0x0, 14, 2, pll_ref_sels, ARRAY_SIZE(pll_ref_sels));
6862306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PLL2_REF_SEL] = imx_clk_hw_mux("pll2_sys_ref_sel",
6962306a36Sopenharmony_ci		pll_base + 0x30, 14, 2, pll_ref_sels, ARRAY_SIZE(pll_ref_sels));
7062306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PLL3_REF_SEL] = imx_clk_hw_mux("pll3_usb_otg_ref_sel",
7162306a36Sopenharmony_ci		pll_base + 0x10, 14, 2, pll_ref_sels, ARRAY_SIZE(pll_ref_sels));
7262306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PLL5_REF_SEL] = imx_clk_hw_mux("pll5_video_ref_sel",
7362306a36Sopenharmony_ci		pll_base + 0xa0, 14, 2, pll_ref_sels, ARRAY_SIZE(pll_ref_sels));
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PLL1_ARM] = imx_clk_hw_pllv3(IMX_PLLV3_SYS, "pll1_arm",
7662306a36Sopenharmony_ci		"pll1_arm_ref_sel", pll_base + 0x0, 0x7f);
7762306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PLL2_SYS] = imx_clk_hw_pllv3(IMX_PLLV3_GENERIC, "pll2_sys",
7862306a36Sopenharmony_ci		"pll2_sys_ref_sel", pll_base + 0x30, 0x1);
7962306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PLL3_USB_OTG] = imx_clk_hw_pllv3(IMX_PLLV3_USB, "pll3_usb_otg",
8062306a36Sopenharmony_ci		"pll3_usb_otg_ref_sel", pll_base + 0x10, 0x1);
8162306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PLL5_VIDEO] = imx_clk_hw_pllv3(IMX_PLLV3_AV, "pll5_video",
8262306a36Sopenharmony_ci		"pll5_video_ref_sel", pll_base + 0xa0, 0x7f);
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	/* PLL bypass out */
8562306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PLL1_BYPASS] = imx_clk_hw_mux_flags("pll1_bypass", pll_base + 0x0, 16, 1,
8662306a36Sopenharmony_ci		pll1_bypass_sels, ARRAY_SIZE(pll1_bypass_sels), CLK_SET_RATE_PARENT);
8762306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PLL2_BYPASS] = imx_clk_hw_mux_flags("pll2_bypass", pll_base + 0x30, 16, 1,
8862306a36Sopenharmony_ci		pll2_bypass_sels, ARRAY_SIZE(pll2_bypass_sels), CLK_SET_RATE_PARENT);
8962306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PLL3_BYPASS] = imx_clk_hw_mux_flags("pll3_bypass", pll_base + 0x10, 16, 1,
9062306a36Sopenharmony_ci		pll3_bypass_sels, ARRAY_SIZE(pll3_bypass_sels), CLK_SET_RATE_PARENT);
9162306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PLL5_BYPASS] = imx_clk_hw_mux_flags("pll5_bypass", pll_base + 0xa0, 16, 1,
9262306a36Sopenharmony_ci		pll5_bypass_sels, ARRAY_SIZE(pll5_bypass_sels), CLK_SET_RATE_PARENT);
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	hws[IMXRT1050_CLK_VIDEO_POST_DIV_SEL] = imx_clk_hw_divider("video_post_div_sel",
9562306a36Sopenharmony_ci		"pll5_video", pll_base + 0xa0, 19, 2);
9662306a36Sopenharmony_ci	hws[IMXRT1050_CLK_VIDEO_DIV] = imx_clk_hw_divider("video_div",
9762306a36Sopenharmony_ci		"video_post_div_sel", pll_base + 0x170, 30, 2);
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PLL3_80M] = imx_clk_hw_fixed_factor("pll3_80m",  "pll3_usb_otg", 1, 6);
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PLL2_PFD0_352M] = imx_clk_hw_pfd("pll2_pfd0_352m", "pll2_sys", pll_base + 0x100, 0);
10262306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PLL2_PFD1_594M] = imx_clk_hw_pfd("pll2_pfd1_594m", "pll2_sys", pll_base + 0x100, 1);
10362306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PLL2_PFD2_396M] = imx_clk_hw_pfd("pll2_pfd2_396m", "pll2_sys", pll_base + 0x100, 2);
10462306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PLL3_PFD1_664_62M] = imx_clk_hw_pfd("pll3_pfd1_664_62m", "pll3_usb_otg", pll_base + 0xf0, 1);
10562306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PLL3_PFD3_454_74M] = imx_clk_hw_pfd("pll3_pfd3_454_74m", "pll3_usb_otg", pll_base + 0xf0, 3);
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci	/* CCM clocks */
10862306a36Sopenharmony_ci	ccm_base = devm_platform_ioremap_resource(pdev, 0);
10962306a36Sopenharmony_ci	if (WARN_ON(IS_ERR(ccm_base))) {
11062306a36Sopenharmony_ci		ret = PTR_ERR(ccm_base);
11162306a36Sopenharmony_ci		goto unregister_hws;
11262306a36Sopenharmony_ci	}
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ci	hws[IMXRT1050_CLK_ARM_PODF] = imx_clk_hw_divider("arm_podf", "pll1_arm", ccm_base + 0x10, 0, 3);
11562306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PRE_PERIPH_SEL] = imx_clk_hw_mux("pre_periph_sel", ccm_base + 0x18, 18, 2,
11662306a36Sopenharmony_ci		pre_periph_sels, ARRAY_SIZE(pre_periph_sels));
11762306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PERIPH_SEL] = imx_clk_hw_mux("periph_sel", ccm_base + 0x14, 25, 1,
11862306a36Sopenharmony_ci		periph_sels, ARRAY_SIZE(periph_sels));
11962306a36Sopenharmony_ci	hws[IMXRT1050_CLK_USDHC1_SEL] = imx_clk_hw_mux("usdhc1_sel", ccm_base + 0x1c, 16, 1,
12062306a36Sopenharmony_ci		usdhc_sels, ARRAY_SIZE(usdhc_sels));
12162306a36Sopenharmony_ci	hws[IMXRT1050_CLK_USDHC2_SEL] = imx_clk_hw_mux("usdhc2_sel", ccm_base + 0x1c, 17, 1,
12262306a36Sopenharmony_ci		usdhc_sels, ARRAY_SIZE(usdhc_sels));
12362306a36Sopenharmony_ci	hws[IMXRT1050_CLK_LPUART_SEL] = imx_clk_hw_mux("lpuart_sel", ccm_base + 0x24, 6, 1,
12462306a36Sopenharmony_ci		lpuart_sels, ARRAY_SIZE(lpuart_sels));
12562306a36Sopenharmony_ci	hws[IMXRT1050_CLK_LCDIF_SEL] = imx_clk_hw_mux("lcdif_sel", ccm_base + 0x38, 15, 3,
12662306a36Sopenharmony_ci		lcdif_sels, ARRAY_SIZE(lcdif_sels));
12762306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PER_CLK_SEL] = imx_clk_hw_mux("per_sel", ccm_base + 0x1C, 6, 1,
12862306a36Sopenharmony_ci		per_sels, ARRAY_SIZE(per_sels));
12962306a36Sopenharmony_ci	hws[IMXRT1050_CLK_SEMC_ALT_SEL] = imx_clk_hw_mux("semc_alt_sel", ccm_base + 0x14, 7, 1,
13062306a36Sopenharmony_ci		semc_alt_sels, ARRAY_SIZE(semc_alt_sels));
13162306a36Sopenharmony_ci	hws[IMXRT1050_CLK_SEMC_SEL] = imx_clk_hw_mux_flags("semc_sel", ccm_base + 0x14, 6, 1,
13262306a36Sopenharmony_ci		semc_sels, ARRAY_SIZE(semc_sels), CLK_IS_CRITICAL);
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	hws[IMXRT1050_CLK_AHB_PODF] = imx_clk_hw_divider("ahb", "periph_sel", ccm_base + 0x14, 10, 3);
13562306a36Sopenharmony_ci	hws[IMXRT1050_CLK_IPG_PDOF] = imx_clk_hw_divider("ipg", "ahb", ccm_base + 0x14, 8, 2);
13662306a36Sopenharmony_ci	hws[IMXRT1050_CLK_PER_PDOF] = imx_clk_hw_divider("per", "per_sel", ccm_base + 0x1C, 0, 5);
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci	hws[IMXRT1050_CLK_USDHC1_PODF] = imx_clk_hw_divider("usdhc1_podf", "usdhc1_sel", ccm_base + 0x24, 11, 3);
13962306a36Sopenharmony_ci	hws[IMXRT1050_CLK_USDHC2_PODF] = imx_clk_hw_divider("usdhc2_podf", "usdhc2_sel", ccm_base + 0x24, 16, 3);
14062306a36Sopenharmony_ci	hws[IMXRT1050_CLK_LPUART_PODF] = imx_clk_hw_divider("lpuart_podf", "lpuart_sel", ccm_base + 0x24, 0, 6);
14162306a36Sopenharmony_ci	hws[IMXRT1050_CLK_LCDIF_PRED] = imx_clk_hw_divider("lcdif_pred", "lcdif_sel", ccm_base + 0x38, 12, 3);
14262306a36Sopenharmony_ci	hws[IMXRT1050_CLK_LCDIF_PODF] = imx_clk_hw_divider("lcdif_podf", "lcdif_pred", ccm_base + 0x18, 23, 3);
14362306a36Sopenharmony_ci
14462306a36Sopenharmony_ci	hws[IMXRT1050_CLK_USDHC1] = imx_clk_hw_gate2("usdhc1", "usdhc1_podf", ccm_base + 0x80, 2);
14562306a36Sopenharmony_ci	hws[IMXRT1050_CLK_USDHC2] = imx_clk_hw_gate2("usdhc2", "usdhc2_podf", ccm_base + 0x80, 4);
14662306a36Sopenharmony_ci	hws[IMXRT1050_CLK_LPUART1] = imx_clk_hw_gate2("lpuart1", "lpuart_podf", ccm_base + 0x7c, 24);
14762306a36Sopenharmony_ci	hws[IMXRT1050_CLK_LCDIF_APB] = imx_clk_hw_gate2("lcdif", "lcdif_podf", ccm_base + 0x70, 28);
14862306a36Sopenharmony_ci	hws[IMXRT1050_CLK_LCDIF_PIX] = imx_clk_hw_gate2("lcdif_pix", "lcdif", ccm_base + 0x74, 10);
14962306a36Sopenharmony_ci	hws[IMXRT1050_CLK_DMA] = imx_clk_hw_gate("dma", "ipg", ccm_base + 0x7C, 6);
15062306a36Sopenharmony_ci	hws[IMXRT1050_CLK_DMA_MUX] = imx_clk_hw_gate("dmamux0", "ipg", ccm_base + 0x7C, 7);
15162306a36Sopenharmony_ci	imx_check_clk_hws(hws, IMXRT1050_CLK_END);
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_hw_data);
15462306a36Sopenharmony_ci	if (ret < 0) {
15562306a36Sopenharmony_ci		dev_err(dev, "Failed to register clks for i.MXRT1050.\n");
15662306a36Sopenharmony_ci		goto unregister_hws;
15762306a36Sopenharmony_ci	}
15862306a36Sopenharmony_ci	return 0;
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ciunregister_hws:
16162306a36Sopenharmony_ci	imx_unregister_hw_clocks(hws, IMXRT1050_CLK_END);
16262306a36Sopenharmony_ci	return ret;
16362306a36Sopenharmony_ci}
16462306a36Sopenharmony_cistatic const struct of_device_id imxrt1050_clk_of_match[] = {
16562306a36Sopenharmony_ci	{ .compatible = "fsl,imxrt1050-ccm" },
16662306a36Sopenharmony_ci	{ /* Sentinel */ }
16762306a36Sopenharmony_ci};
16862306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, imxrt1050_clk_of_match);
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_cistatic struct platform_driver imxrt1050_clk_driver = {
17162306a36Sopenharmony_ci	.probe = imxrt1050_clocks_probe,
17262306a36Sopenharmony_ci	.driver = {
17362306a36Sopenharmony_ci		.name = "imxrt1050-ccm",
17462306a36Sopenharmony_ci		.of_match_table = imxrt1050_clk_of_match,
17562306a36Sopenharmony_ci	},
17662306a36Sopenharmony_ci};
17762306a36Sopenharmony_cimodule_platform_driver(imxrt1050_clk_driver);
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL");
18062306a36Sopenharmony_ciMODULE_AUTHOR("Jesse Taube <Mr.Bossman075@gmail.com>");
18162306a36Sopenharmony_ciMODULE_AUTHOR("Giulio Benetti <giulio.benetti@benettiengineering.com>");
182