162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright 2018 NXP
462306a36Sopenharmony_ci *	Dong Aisheng <aisheng.dong@nxp.com>
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#include <linux/clk-provider.h>
862306a36Sopenharmony_ci#include <linux/err.h>
962306a36Sopenharmony_ci#include <linux/io.h>
1062306a36Sopenharmony_ci#include <linux/module.h>
1162306a36Sopenharmony_ci#include <linux/of.h>
1262306a36Sopenharmony_ci#include <linux/platform_device.h>
1362306a36Sopenharmony_ci#include <linux/pm_runtime.h>
1462306a36Sopenharmony_ci#include <linux/slab.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#include "clk-scu.h"
1762306a36Sopenharmony_ci#include "clk-imx8qxp-lpcg.h"
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#include <dt-bindings/clock/imx8-clock.h>
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_ci/*
2262306a36Sopenharmony_ci * struct imx8qxp_lpcg_data - Description of one LPCG clock
2362306a36Sopenharmony_ci * @id: clock ID
2462306a36Sopenharmony_ci * @name: clock name
2562306a36Sopenharmony_ci * @parent: parent clock name
2662306a36Sopenharmony_ci * @flags: common clock flags
2762306a36Sopenharmony_ci * @offset: offset of this LPCG clock
2862306a36Sopenharmony_ci * @bit_idx: bit index of this LPCG clock
2962306a36Sopenharmony_ci * @hw_gate: whether supports HW autogate
3062306a36Sopenharmony_ci *
3162306a36Sopenharmony_ci * This structure describes one LPCG clock
3262306a36Sopenharmony_ci */
3362306a36Sopenharmony_cistruct imx8qxp_lpcg_data {
3462306a36Sopenharmony_ci	int id;
3562306a36Sopenharmony_ci	char *name;
3662306a36Sopenharmony_ci	char *parent;
3762306a36Sopenharmony_ci	unsigned long flags;
3862306a36Sopenharmony_ci	u32 offset;
3962306a36Sopenharmony_ci	u8 bit_idx;
4062306a36Sopenharmony_ci	bool hw_gate;
4162306a36Sopenharmony_ci};
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci/*
4462306a36Sopenharmony_ci * struct imx8qxp_ss_lpcg - Description of one subsystem LPCG clocks
4562306a36Sopenharmony_ci * @lpcg: LPCG clocks array of one subsystem
4662306a36Sopenharmony_ci * @num_lpcg: the number of LPCG clocks
4762306a36Sopenharmony_ci * @num_max: the maximum number of LPCG clocks
4862306a36Sopenharmony_ci *
4962306a36Sopenharmony_ci * This structure describes each subsystem LPCG clocks information
5062306a36Sopenharmony_ci * which then will be used to create respective LPCGs clocks
5162306a36Sopenharmony_ci */
5262306a36Sopenharmony_cistruct imx8qxp_ss_lpcg {
5362306a36Sopenharmony_ci	const struct imx8qxp_lpcg_data *lpcg;
5462306a36Sopenharmony_ci	u8 num_lpcg;
5562306a36Sopenharmony_ci	u8 num_max;
5662306a36Sopenharmony_ci};
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_cistatic const struct imx8qxp_lpcg_data imx8qxp_lpcg_adma[] = {
5962306a36Sopenharmony_ci	{ IMX_ADMA_LPCG_UART0_IPG_CLK, "uart0_lpcg_ipg_clk", "dma_ipg_clk_root", 0, ADMA_LPUART_0_LPCG, 16, 0, },
6062306a36Sopenharmony_ci	{ IMX_ADMA_LPCG_UART0_BAUD_CLK, "uart0_lpcg_baud_clk", "uart0_clk", 0, ADMA_LPUART_0_LPCG, 0, 0, },
6162306a36Sopenharmony_ci	{ IMX_ADMA_LPCG_UART1_IPG_CLK, "uart1_lpcg_ipg_clk", "dma_ipg_clk_root", 0, ADMA_LPUART_1_LPCG, 16, 0, },
6262306a36Sopenharmony_ci	{ IMX_ADMA_LPCG_UART1_BAUD_CLK, "uart1_lpcg_baud_clk", "uart1_clk", 0, ADMA_LPUART_1_LPCG, 0, 0, },
6362306a36Sopenharmony_ci	{ IMX_ADMA_LPCG_UART2_IPG_CLK, "uart2_lpcg_ipg_clk", "dma_ipg_clk_root", 0, ADMA_LPUART_2_LPCG, 16, 0, },
6462306a36Sopenharmony_ci	{ IMX_ADMA_LPCG_UART2_BAUD_CLK, "uart2_lpcg_baud_clk", "uart2_clk", 0, ADMA_LPUART_2_LPCG, 0, 0, },
6562306a36Sopenharmony_ci	{ IMX_ADMA_LPCG_UART3_IPG_CLK, "uart3_lpcg_ipg_clk", "dma_ipg_clk_root", 0, ADMA_LPUART_3_LPCG, 16, 0, },
6662306a36Sopenharmony_ci	{ IMX_ADMA_LPCG_UART3_BAUD_CLK, "uart3_lpcg_baud_clk", "uart3_clk", 0, ADMA_LPUART_3_LPCG, 0, 0, },
6762306a36Sopenharmony_ci	{ IMX_ADMA_LPCG_I2C0_IPG_CLK, "i2c0_lpcg_ipg_clk", "dma_ipg_clk_root", 0, ADMA_LPI2C_0_LPCG, 16, 0, },
6862306a36Sopenharmony_ci	{ IMX_ADMA_LPCG_I2C0_CLK, "i2c0_lpcg_clk", "i2c0_clk", 0, ADMA_LPI2C_0_LPCG, 0, 0, },
6962306a36Sopenharmony_ci	{ IMX_ADMA_LPCG_I2C1_IPG_CLK, "i2c1_lpcg_ipg_clk", "dma_ipg_clk_root", 0, ADMA_LPI2C_1_LPCG, 16, 0, },
7062306a36Sopenharmony_ci	{ IMX_ADMA_LPCG_I2C1_CLK, "i2c1_lpcg_clk", "i2c1_clk", 0, ADMA_LPI2C_1_LPCG, 0, 0, },
7162306a36Sopenharmony_ci	{ IMX_ADMA_LPCG_I2C2_IPG_CLK, "i2c2_lpcg_ipg_clk", "dma_ipg_clk_root", 0, ADMA_LPI2C_2_LPCG, 16, 0, },
7262306a36Sopenharmony_ci	{ IMX_ADMA_LPCG_I2C2_CLK, "i2c2_lpcg_clk", "i2c2_clk", 0, ADMA_LPI2C_2_LPCG, 0, 0, },
7362306a36Sopenharmony_ci	{ IMX_ADMA_LPCG_I2C3_IPG_CLK, "i2c3_lpcg_ipg_clk", "dma_ipg_clk_root", 0, ADMA_LPI2C_3_LPCG, 16, 0, },
7462306a36Sopenharmony_ci	{ IMX_ADMA_LPCG_I2C3_CLK, "i2c3_lpcg_clk", "i2c3_clk", 0, ADMA_LPI2C_3_LPCG, 0, 0, },
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	{ IMX_ADMA_LPCG_DSP_CORE_CLK, "dsp_lpcg_core_clk", "dma_ipg_clk_root", 0, ADMA_HIFI_LPCG, 28, 0, },
7762306a36Sopenharmony_ci	{ IMX_ADMA_LPCG_DSP_IPG_CLK, "dsp_lpcg_ipg_clk", "dma_ipg_clk_root", 0, ADMA_HIFI_LPCG, 20, 0, },
7862306a36Sopenharmony_ci	{ IMX_ADMA_LPCG_DSP_ADB_CLK, "dsp_lpcg_adb_clk", "dma_ipg_clk_root", 0, ADMA_HIFI_LPCG, 16, 0, },
7962306a36Sopenharmony_ci	{ IMX_ADMA_LPCG_OCRAM_IPG_CLK, "ocram_lpcg_ipg_clk", "dma_ipg_clk_root", 0, ADMA_OCRAM_LPCG, 16, 0, },
8062306a36Sopenharmony_ci};
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_cistatic const struct imx8qxp_ss_lpcg imx8qxp_ss_adma = {
8362306a36Sopenharmony_ci	.lpcg = imx8qxp_lpcg_adma,
8462306a36Sopenharmony_ci	.num_lpcg = ARRAY_SIZE(imx8qxp_lpcg_adma),
8562306a36Sopenharmony_ci	.num_max = IMX_ADMA_LPCG_CLK_END,
8662306a36Sopenharmony_ci};
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_cistatic const struct imx8qxp_lpcg_data imx8qxp_lpcg_conn[] = {
8962306a36Sopenharmony_ci	{ IMX_CONN_LPCG_SDHC0_PER_CLK, "sdhc0_lpcg_per_clk", "sdhc0_clk", 0, CONN_USDHC_0_LPCG, 0, 0, },
9062306a36Sopenharmony_ci	{ IMX_CONN_LPCG_SDHC0_IPG_CLK, "sdhc0_lpcg_ipg_clk", "conn_ipg_clk_root", 0, CONN_USDHC_0_LPCG, 16, 0, },
9162306a36Sopenharmony_ci	{ IMX_CONN_LPCG_SDHC0_HCLK, "sdhc0_lpcg_ahb_clk", "conn_axi_clk_root", 0, CONN_USDHC_0_LPCG, 20, 0, },
9262306a36Sopenharmony_ci	{ IMX_CONN_LPCG_SDHC1_PER_CLK, "sdhc1_lpcg_per_clk", "sdhc1_clk", 0, CONN_USDHC_1_LPCG, 0, 0, },
9362306a36Sopenharmony_ci	{ IMX_CONN_LPCG_SDHC1_IPG_CLK, "sdhc1_lpcg_ipg_clk", "conn_ipg_clk_root", 0, CONN_USDHC_1_LPCG, 16, 0, },
9462306a36Sopenharmony_ci	{ IMX_CONN_LPCG_SDHC1_HCLK, "sdhc1_lpcg_ahb_clk", "conn_axi_clk_root", 0, CONN_USDHC_1_LPCG, 20, 0, },
9562306a36Sopenharmony_ci	{ IMX_CONN_LPCG_SDHC2_PER_CLK, "sdhc2_lpcg_per_clk", "sdhc2_clk", 0, CONN_USDHC_2_LPCG, 0, 0, },
9662306a36Sopenharmony_ci	{ IMX_CONN_LPCG_SDHC2_IPG_CLK, "sdhc2_lpcg_ipg_clk", "conn_ipg_clk_root", 0, CONN_USDHC_2_LPCG, 16, 0, },
9762306a36Sopenharmony_ci	{ IMX_CONN_LPCG_SDHC2_HCLK, "sdhc2_lpcg_ahb_clk", "conn_axi_clk_root", 0, CONN_USDHC_2_LPCG, 20, 0, },
9862306a36Sopenharmony_ci	{ IMX_CONN_LPCG_ENET0_ROOT_CLK, "enet0_ipg_root_clk", "enet0_clk", 0, CONN_ENET_0_LPCG, 0, 0, },
9962306a36Sopenharmony_ci	{ IMX_CONN_LPCG_ENET0_TX_CLK, "enet0_tx_clk", "enet0_clk", 0, CONN_ENET_0_LPCG, 4, 0, },
10062306a36Sopenharmony_ci	{ IMX_CONN_LPCG_ENET0_AHB_CLK, "enet0_ahb_clk", "conn_axi_clk_root", 0, CONN_ENET_0_LPCG, 8, 0, },
10162306a36Sopenharmony_ci	{ IMX_CONN_LPCG_ENET0_IPG_S_CLK, "enet0_ipg_s_clk", "conn_ipg_clk_root", 0, CONN_ENET_0_LPCG, 20, 0, },
10262306a36Sopenharmony_ci	{ IMX_CONN_LPCG_ENET0_IPG_CLK, "enet0_ipg_clk", "enet0_ipg_s_clk", 0, CONN_ENET_0_LPCG, 16, 0, },
10362306a36Sopenharmony_ci	{ IMX_CONN_LPCG_ENET1_ROOT_CLK, "enet1_ipg_root_clk", "enet1_clk", 0, CONN_ENET_1_LPCG, 0, 0, },
10462306a36Sopenharmony_ci	{ IMX_CONN_LPCG_ENET1_TX_CLK, "enet1_tx_clk", "enet1_clk", 0, CONN_ENET_1_LPCG, 4, 0, },
10562306a36Sopenharmony_ci	{ IMX_CONN_LPCG_ENET1_AHB_CLK, "enet1_ahb_clk", "conn_axi_clk_root", 0, CONN_ENET_1_LPCG, 8, 0, },
10662306a36Sopenharmony_ci	{ IMX_CONN_LPCG_ENET1_IPG_S_CLK, "enet1_ipg_s_clk", "conn_ipg_clk_root", 0, CONN_ENET_1_LPCG, 20, 0, },
10762306a36Sopenharmony_ci	{ IMX_CONN_LPCG_ENET1_IPG_CLK, "enet1_ipg_clk", "enet0_ipg_s_clk", 0, CONN_ENET_1_LPCG, 16, 0, },
10862306a36Sopenharmony_ci};
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_cistatic const struct imx8qxp_ss_lpcg imx8qxp_ss_conn = {
11162306a36Sopenharmony_ci	.lpcg = imx8qxp_lpcg_conn,
11262306a36Sopenharmony_ci	.num_lpcg = ARRAY_SIZE(imx8qxp_lpcg_conn),
11362306a36Sopenharmony_ci	.num_max = IMX_CONN_LPCG_CLK_END,
11462306a36Sopenharmony_ci};
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_cistatic const struct imx8qxp_lpcg_data imx8qxp_lpcg_lsio[] = {
11762306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM0_IPG_CLK, "pwm0_lpcg_ipg_clk", "pwm0_clk", 0, LSIO_PWM_0_LPCG, 0, 0, },
11862306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM0_IPG_HF_CLK, "pwm0_lpcg_ipg_hf_clk", "pwm0_clk", 0, LSIO_PWM_0_LPCG, 4, 0, },
11962306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM0_IPG_S_CLK, "pwm0_lpcg_ipg_s_clk", "pwm0_clk", 0, LSIO_PWM_0_LPCG, 16, 0, },
12062306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM0_IPG_SLV_CLK, "pwm0_lpcg_ipg_slv_clk", "lsio_bus_clk_root", 0, LSIO_PWM_0_LPCG, 20, 0, },
12162306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM0_IPG_MSTR_CLK, "pwm0_lpcg_ipg_mstr_clk", "pwm0_clk", 0, LSIO_PWM_0_LPCG, 24, 0, },
12262306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM1_IPG_CLK, "pwm1_lpcg_ipg_clk", "pwm1_clk", 0, LSIO_PWM_1_LPCG, 0, 0, },
12362306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM1_IPG_HF_CLK, "pwm1_lpcg_ipg_hf_clk", "pwm1_clk", 0, LSIO_PWM_1_LPCG, 4, 0, },
12462306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM1_IPG_S_CLK, "pwm1_lpcg_ipg_s_clk", "pwm1_clk", 0, LSIO_PWM_1_LPCG, 16, 0, },
12562306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM1_IPG_SLV_CLK, "pwm1_lpcg_ipg_slv_clk", "lsio_bus_clk_root", 0, LSIO_PWM_1_LPCG, 20, 0, },
12662306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM1_IPG_MSTR_CLK, "pwm1_lpcg_ipg_mstr_clk", "pwm1_clk", 0, LSIO_PWM_1_LPCG, 24, 0, },
12762306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM2_IPG_CLK, "pwm2_lpcg_ipg_clk", "pwm2_clk", 0, LSIO_PWM_2_LPCG, 0, 0, },
12862306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM2_IPG_HF_CLK, "pwm2_lpcg_ipg_hf_clk", "pwm2_clk", 0, LSIO_PWM_2_LPCG, 4, 0, },
12962306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM2_IPG_S_CLK, "pwm2_lpcg_ipg_s_clk", "pwm2_clk", 0, LSIO_PWM_2_LPCG, 16, 0, },
13062306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM2_IPG_SLV_CLK, "pwm2_lpcg_ipg_slv_clk", "lsio_bus_clk_root", 0, LSIO_PWM_2_LPCG, 20, 0, },
13162306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM2_IPG_MSTR_CLK, "pwm2_lpcg_ipg_mstr_clk", "pwm2_clk", 0, LSIO_PWM_2_LPCG, 24, 0, },
13262306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM3_IPG_CLK, "pwm3_lpcg_ipg_clk", "pwm3_clk", 0, LSIO_PWM_3_LPCG, 0, 0, },
13362306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM3_IPG_HF_CLK, "pwm3_lpcg_ipg_hf_clk", "pwm3_clk", 0, LSIO_PWM_3_LPCG, 4, 0, },
13462306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM3_IPG_S_CLK, "pwm3_lpcg_ipg_s_clk", "pwm3_clk", 0, LSIO_PWM_3_LPCG, 16, 0, },
13562306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM3_IPG_SLV_CLK, "pwm3_lpcg_ipg_slv_clk", "lsio_bus_clk_root", 0, LSIO_PWM_3_LPCG, 20, 0, },
13662306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM3_IPG_MSTR_CLK, "pwm3_lpcg_ipg_mstr_clk", "pwm3_clk", 0, LSIO_PWM_3_LPCG, 24, 0, },
13762306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM4_IPG_CLK, "pwm4_lpcg_ipg_clk", "pwm4_clk", 0, LSIO_PWM_4_LPCG, 0, 0, },
13862306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM4_IPG_HF_CLK, "pwm4_lpcg_ipg_hf_clk", "pwm4_clk", 0, LSIO_PWM_4_LPCG, 4, 0, },
13962306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM4_IPG_S_CLK, "pwm4_lpcg_ipg_s_clk", "pwm4_clk", 0, LSIO_PWM_4_LPCG, 16, 0, },
14062306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM4_IPG_SLV_CLK, "pwm4_lpcg_ipg_slv_clk", "lsio_bus_clk_root", 0, LSIO_PWM_4_LPCG, 20, 0, },
14162306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM4_IPG_MSTR_CLK, "pwm4_lpcg_ipg_mstr_clk", "pwm4_clk", 0, LSIO_PWM_4_LPCG, 24, 0, },
14262306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM5_IPG_CLK, "pwm5_lpcg_ipg_clk", "pwm5_clk", 0, LSIO_PWM_5_LPCG, 0, 0, },
14362306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM5_IPG_HF_CLK, "pwm5_lpcg_ipg_hf_clk", "pwm5_clk", 0, LSIO_PWM_5_LPCG, 4, 0, },
14462306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM5_IPG_S_CLK, "pwm5_lpcg_ipg_s_clk", "pwm5_clk", 0, LSIO_PWM_5_LPCG, 16, 0, },
14562306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM5_IPG_SLV_CLK, "pwm5_lpcg_ipg_slv_clk", "lsio_bus_clk_root", 0, LSIO_PWM_5_LPCG, 20, 0, },
14662306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM5_IPG_MSTR_CLK, "pwm5_lpcg_ipg_mstr_clk", "pwm5_clk", 0, LSIO_PWM_5_LPCG, 24, 0, },
14762306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM6_IPG_CLK, "pwm6_lpcg_ipg_clk", "pwm6_clk", 0, LSIO_PWM_6_LPCG, 0, 0, },
14862306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM6_IPG_HF_CLK, "pwm6_lpcg_ipg_hf_clk", "pwm6_clk", 0, LSIO_PWM_6_LPCG, 4, 0, },
14962306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM6_IPG_S_CLK, "pwm6_lpcg_ipg_s_clk", "pwm6_clk", 0, LSIO_PWM_6_LPCG, 16, 0, },
15062306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM6_IPG_SLV_CLK, "pwm6_lpcg_ipg_slv_clk", "lsio_bus_clk_root", 0, LSIO_PWM_6_LPCG, 20, 0, },
15162306a36Sopenharmony_ci	{ IMX_LSIO_LPCG_PWM6_IPG_MSTR_CLK, "pwm6_lpcg_ipg_mstr_clk", "pwm6_clk", 0, LSIO_PWM_6_LPCG, 24, 0, },
15262306a36Sopenharmony_ci};
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_cistatic const struct imx8qxp_ss_lpcg imx8qxp_ss_lsio = {
15562306a36Sopenharmony_ci	.lpcg = imx8qxp_lpcg_lsio,
15662306a36Sopenharmony_ci	.num_lpcg = ARRAY_SIZE(imx8qxp_lpcg_lsio),
15762306a36Sopenharmony_ci	.num_max = IMX_LSIO_LPCG_CLK_END,
15862306a36Sopenharmony_ci};
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci#define IMX_LPCG_MAX_CLKS	8
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_cistatic struct clk_hw *imx_lpcg_of_clk_src_get(struct of_phandle_args *clkspec,
16362306a36Sopenharmony_ci					      void *data)
16462306a36Sopenharmony_ci{
16562306a36Sopenharmony_ci	struct clk_hw_onecell_data *hw_data = data;
16662306a36Sopenharmony_ci	unsigned int idx = clkspec->args[0] / 4;
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci	if (idx >= hw_data->num) {
16962306a36Sopenharmony_ci		pr_err("%s: invalid index %u\n", __func__, idx);
17062306a36Sopenharmony_ci		return ERR_PTR(-EINVAL);
17162306a36Sopenharmony_ci	}
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ci	return hw_data->hws[idx];
17462306a36Sopenharmony_ci}
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_cistatic int imx_lpcg_parse_clks_from_dt(struct platform_device *pdev,
17762306a36Sopenharmony_ci				       struct device_node *np)
17862306a36Sopenharmony_ci{
17962306a36Sopenharmony_ci	const char *output_names[IMX_LPCG_MAX_CLKS];
18062306a36Sopenharmony_ci	const char *parent_names[IMX_LPCG_MAX_CLKS];
18162306a36Sopenharmony_ci	unsigned int bit_offset[IMX_LPCG_MAX_CLKS];
18262306a36Sopenharmony_ci	struct clk_hw_onecell_data *clk_data;
18362306a36Sopenharmony_ci	struct clk_hw **clk_hws;
18462306a36Sopenharmony_ci	void __iomem *base;
18562306a36Sopenharmony_ci	int count;
18662306a36Sopenharmony_ci	int idx;
18762306a36Sopenharmony_ci	int ret;
18862306a36Sopenharmony_ci	int i;
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci	if (!of_device_is_compatible(np, "fsl,imx8qxp-lpcg"))
19162306a36Sopenharmony_ci		return -EINVAL;
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ci	base = devm_platform_ioremap_resource(pdev, 0);
19462306a36Sopenharmony_ci	if (IS_ERR(base))
19562306a36Sopenharmony_ci		return PTR_ERR(base);
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci	count = of_property_count_u32_elems(np, "clock-indices");
19862306a36Sopenharmony_ci	if (count < 0) {
19962306a36Sopenharmony_ci		dev_err(&pdev->dev, "failed to count clocks\n");
20062306a36Sopenharmony_ci		return -EINVAL;
20162306a36Sopenharmony_ci	}
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci	/*
20462306a36Sopenharmony_ci	 * A trick here is that we set the num of clks to the MAX instead
20562306a36Sopenharmony_ci	 * of the count from clock-indices because one LPCG supports up to
20662306a36Sopenharmony_ci	 * 8 clock outputs which each of them is fixed to 4 bits. Then we can
20762306a36Sopenharmony_ci	 * easily get the clock by clk-indices (bit-offset) / 4.
20862306a36Sopenharmony_ci	 * And the cost is very limited few pointers.
20962306a36Sopenharmony_ci	 */
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci	clk_data = devm_kzalloc(&pdev->dev, struct_size(clk_data, hws,
21262306a36Sopenharmony_ci				IMX_LPCG_MAX_CLKS), GFP_KERNEL);
21362306a36Sopenharmony_ci	if (!clk_data)
21462306a36Sopenharmony_ci		return -ENOMEM;
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci	clk_data->num = IMX_LPCG_MAX_CLKS;
21762306a36Sopenharmony_ci	clk_hws = clk_data->hws;
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci	ret = of_property_read_u32_array(np, "clock-indices", bit_offset,
22062306a36Sopenharmony_ci					 count);
22162306a36Sopenharmony_ci	if (ret < 0) {
22262306a36Sopenharmony_ci		dev_err(&pdev->dev, "failed to read clock-indices\n");
22362306a36Sopenharmony_ci		return -EINVAL;
22462306a36Sopenharmony_ci	}
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci	ret = of_clk_parent_fill(np, parent_names, count);
22762306a36Sopenharmony_ci	if (ret != count) {
22862306a36Sopenharmony_ci		dev_err(&pdev->dev, "failed to get clock parent names\n");
22962306a36Sopenharmony_ci		return count;
23062306a36Sopenharmony_ci	}
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ci	ret = of_property_read_string_array(np, "clock-output-names",
23362306a36Sopenharmony_ci					    output_names, count);
23462306a36Sopenharmony_ci	if (ret != count) {
23562306a36Sopenharmony_ci		dev_err(&pdev->dev, "failed to read clock-output-names\n");
23662306a36Sopenharmony_ci		return -EINVAL;
23762306a36Sopenharmony_ci	}
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	pm_runtime_get_noresume(&pdev->dev);
24062306a36Sopenharmony_ci	pm_runtime_set_active(&pdev->dev);
24162306a36Sopenharmony_ci	pm_runtime_set_autosuspend_delay(&pdev->dev, 500);
24262306a36Sopenharmony_ci	pm_runtime_use_autosuspend(&pdev->dev);
24362306a36Sopenharmony_ci	pm_runtime_enable(&pdev->dev);
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci	for (i = 0; i < count; i++) {
24662306a36Sopenharmony_ci		idx = bit_offset[i] / 4;
24762306a36Sopenharmony_ci		if (idx >= IMX_LPCG_MAX_CLKS) {
24862306a36Sopenharmony_ci			dev_warn(&pdev->dev, "invalid bit offset of clock %d\n",
24962306a36Sopenharmony_ci				 i);
25062306a36Sopenharmony_ci			ret = -EINVAL;
25162306a36Sopenharmony_ci			goto unreg;
25262306a36Sopenharmony_ci		}
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci		clk_hws[idx] = imx_clk_lpcg_scu_dev(&pdev->dev, output_names[i],
25562306a36Sopenharmony_ci						    parent_names[i], 0, base,
25662306a36Sopenharmony_ci						    bit_offset[i], false);
25762306a36Sopenharmony_ci		if (IS_ERR(clk_hws[idx])) {
25862306a36Sopenharmony_ci			dev_warn(&pdev->dev, "failed to register clock %d\n",
25962306a36Sopenharmony_ci				 idx);
26062306a36Sopenharmony_ci			ret = PTR_ERR(clk_hws[idx]);
26162306a36Sopenharmony_ci			goto unreg;
26262306a36Sopenharmony_ci		}
26362306a36Sopenharmony_ci	}
26462306a36Sopenharmony_ci
26562306a36Sopenharmony_ci	ret = devm_of_clk_add_hw_provider(&pdev->dev, imx_lpcg_of_clk_src_get,
26662306a36Sopenharmony_ci					  clk_data);
26762306a36Sopenharmony_ci	if (ret)
26862306a36Sopenharmony_ci		goto unreg;
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci	pm_runtime_mark_last_busy(&pdev->dev);
27162306a36Sopenharmony_ci	pm_runtime_put_autosuspend(&pdev->dev);
27262306a36Sopenharmony_ci
27362306a36Sopenharmony_ci	return 0;
27462306a36Sopenharmony_ci
27562306a36Sopenharmony_ciunreg:
27662306a36Sopenharmony_ci	while (--i >= 0) {
27762306a36Sopenharmony_ci		idx = bit_offset[i] / 4;
27862306a36Sopenharmony_ci		if (clk_hws[idx])
27962306a36Sopenharmony_ci			imx_clk_lpcg_scu_unregister(clk_hws[idx]);
28062306a36Sopenharmony_ci	}
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci	pm_runtime_disable(&pdev->dev);
28362306a36Sopenharmony_ci
28462306a36Sopenharmony_ci	return ret;
28562306a36Sopenharmony_ci}
28662306a36Sopenharmony_ci
28762306a36Sopenharmony_cistatic int imx8qxp_lpcg_clk_probe(struct platform_device *pdev)
28862306a36Sopenharmony_ci{
28962306a36Sopenharmony_ci	struct device *dev = &pdev->dev;
29062306a36Sopenharmony_ci	struct device_node *np = dev->of_node;
29162306a36Sopenharmony_ci	struct clk_hw_onecell_data *clk_data;
29262306a36Sopenharmony_ci	const struct imx8qxp_ss_lpcg *ss_lpcg;
29362306a36Sopenharmony_ci	const struct imx8qxp_lpcg_data *lpcg;
29462306a36Sopenharmony_ci	struct resource *res;
29562306a36Sopenharmony_ci	struct clk_hw **clks;
29662306a36Sopenharmony_ci	void __iomem *base;
29762306a36Sopenharmony_ci	int ret;
29862306a36Sopenharmony_ci	int i;
29962306a36Sopenharmony_ci
30062306a36Sopenharmony_ci	/* try new binding to parse clocks from device tree first */
30162306a36Sopenharmony_ci	ret = imx_lpcg_parse_clks_from_dt(pdev, np);
30262306a36Sopenharmony_ci	if (!ret)
30362306a36Sopenharmony_ci		return 0;
30462306a36Sopenharmony_ci
30562306a36Sopenharmony_ci	ss_lpcg = of_device_get_match_data(dev);
30662306a36Sopenharmony_ci	if (!ss_lpcg)
30762306a36Sopenharmony_ci		return -ENODEV;
30862306a36Sopenharmony_ci
30962306a36Sopenharmony_ci	/*
31062306a36Sopenharmony_ci	 * Please don't replace this with devm_platform_ioremap_resource.
31162306a36Sopenharmony_ci	 *
31262306a36Sopenharmony_ci	 * devm_platform_ioremap_resource calls devm_ioremap_resource which
31362306a36Sopenharmony_ci	 * differs from devm_ioremap by also calling devm_request_mem_region
31462306a36Sopenharmony_ci	 * and preventing other mappings in the same area.
31562306a36Sopenharmony_ci	 *
31662306a36Sopenharmony_ci	 * On imx8 the LPCG nodes map entire subsystems and overlap
31762306a36Sopenharmony_ci	 * peripherals, this means that using devm_platform_ioremap_resource
31862306a36Sopenharmony_ci	 * will cause many devices to fail to probe including serial ports.
31962306a36Sopenharmony_ci	 */
32062306a36Sopenharmony_ci	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
32162306a36Sopenharmony_ci	if (!res)
32262306a36Sopenharmony_ci		return -EINVAL;
32362306a36Sopenharmony_ci	base = devm_ioremap(dev, res->start, resource_size(res));
32462306a36Sopenharmony_ci	if (!base)
32562306a36Sopenharmony_ci		return -ENOMEM;
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci	clk_data = devm_kzalloc(&pdev->dev, struct_size(clk_data, hws,
32862306a36Sopenharmony_ci				ss_lpcg->num_max), GFP_KERNEL);
32962306a36Sopenharmony_ci	if (!clk_data)
33062306a36Sopenharmony_ci		return -ENOMEM;
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci	clk_data->num = ss_lpcg->num_max;
33362306a36Sopenharmony_ci	clks = clk_data->hws;
33462306a36Sopenharmony_ci
33562306a36Sopenharmony_ci	for (i = 0; i < ss_lpcg->num_lpcg; i++) {
33662306a36Sopenharmony_ci		lpcg = ss_lpcg->lpcg + i;
33762306a36Sopenharmony_ci		clks[lpcg->id] = imx_clk_lpcg_scu(lpcg->name, lpcg->parent,
33862306a36Sopenharmony_ci						  lpcg->flags, base + lpcg->offset,
33962306a36Sopenharmony_ci						  lpcg->bit_idx, lpcg->hw_gate);
34062306a36Sopenharmony_ci	}
34162306a36Sopenharmony_ci
34262306a36Sopenharmony_ci	for (i = 0; i < clk_data->num; i++) {
34362306a36Sopenharmony_ci		if (IS_ERR(clks[i]))
34462306a36Sopenharmony_ci			pr_warn("i.MX clk %u: register failed with %ld\n",
34562306a36Sopenharmony_ci				i, PTR_ERR(clks[i]));
34662306a36Sopenharmony_ci	}
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ci	return of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_data);
34962306a36Sopenharmony_ci}
35062306a36Sopenharmony_ci
35162306a36Sopenharmony_cistatic const struct of_device_id imx8qxp_lpcg_match[] = {
35262306a36Sopenharmony_ci	{ .compatible = "fsl,imx8qxp-lpcg-adma", &imx8qxp_ss_adma, },
35362306a36Sopenharmony_ci	{ .compatible = "fsl,imx8qxp-lpcg-conn", &imx8qxp_ss_conn, },
35462306a36Sopenharmony_ci	{ .compatible = "fsl,imx8qxp-lpcg-lsio", &imx8qxp_ss_lsio, },
35562306a36Sopenharmony_ci	{ .compatible = "fsl,imx8qxp-lpcg", NULL },
35662306a36Sopenharmony_ci	{ /* sentinel */ }
35762306a36Sopenharmony_ci};
35862306a36Sopenharmony_ci
35962306a36Sopenharmony_cistatic struct platform_driver imx8qxp_lpcg_clk_driver = {
36062306a36Sopenharmony_ci	.driver = {
36162306a36Sopenharmony_ci		.name = "imx8qxp-lpcg-clk",
36262306a36Sopenharmony_ci		.of_match_table = imx8qxp_lpcg_match,
36362306a36Sopenharmony_ci		.pm = &imx_clk_lpcg_scu_pm_ops,
36462306a36Sopenharmony_ci		.suppress_bind_attrs = true,
36562306a36Sopenharmony_ci	},
36662306a36Sopenharmony_ci	.probe = imx8qxp_lpcg_clk_probe,
36762306a36Sopenharmony_ci};
36862306a36Sopenharmony_ci
36962306a36Sopenharmony_cimodule_platform_driver(imx8qxp_lpcg_clk_driver);
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ciMODULE_AUTHOR("Aisheng Dong <aisheng.dong@nxp.com>");
37262306a36Sopenharmony_ciMODULE_DESCRIPTION("NXP i.MX8QXP LPCG clock driver");
37362306a36Sopenharmony_ciMODULE_LICENSE("GPL v2");
374