11e3121bfSAisheng Dong // SPDX-License-Identifier: GPL-2.0+
21e3121bfSAisheng Dong /*
31e3121bfSAisheng Dong * Copyright 2018 NXP
41e3121bfSAisheng Dong * Dong Aisheng <aisheng.dong@nxp.com>
51e3121bfSAisheng Dong */
61e3121bfSAisheng Dong
71e3121bfSAisheng Dong #include <linux/clk-provider.h>
81e3121bfSAisheng Dong #include <linux/err.h>
91e3121bfSAisheng Dong #include <linux/io.h>
101e3121bfSAisheng Dong #include <linux/module.h>
111e3121bfSAisheng Dong #include <linux/of.h>
121e3121bfSAisheng Dong #include <linux/platform_device.h>
1318cdbad4SDong Aisheng #include <linux/pm_runtime.h>
141e3121bfSAisheng Dong #include <linux/slab.h>
151e3121bfSAisheng Dong
161e3121bfSAisheng Dong #include "clk-scu.h"
171e3121bfSAisheng Dong #include "clk-imx8qxp-lpcg.h"
181e3121bfSAisheng Dong
1908972760SAisheng Dong #include <dt-bindings/clock/imx8-clock.h>
201e3121bfSAisheng Dong
211e3121bfSAisheng Dong /*
221e3121bfSAisheng Dong * struct imx8qxp_lpcg_data - Description of one LPCG clock
231e3121bfSAisheng Dong * @id: clock ID
241e3121bfSAisheng Dong * @name: clock name
251e3121bfSAisheng Dong * @parent: parent clock name
261e3121bfSAisheng Dong * @flags: common clock flags
271e3121bfSAisheng Dong * @offset: offset of this LPCG clock
281e3121bfSAisheng Dong * @bit_idx: bit index of this LPCG clock
291e3121bfSAisheng Dong * @hw_gate: whether supports HW autogate
301e3121bfSAisheng Dong *
311e3121bfSAisheng Dong * This structure describes one LPCG clock
321e3121bfSAisheng Dong */
331e3121bfSAisheng Dong struct imx8qxp_lpcg_data {
341e3121bfSAisheng Dong int id;
351e3121bfSAisheng Dong char *name;
361e3121bfSAisheng Dong char *parent;
371e3121bfSAisheng Dong unsigned long flags;
381e3121bfSAisheng Dong u32 offset;
391e3121bfSAisheng Dong u8 bit_idx;
401e3121bfSAisheng Dong bool hw_gate;
411e3121bfSAisheng Dong };
421e3121bfSAisheng Dong
431e3121bfSAisheng Dong /*
441e3121bfSAisheng Dong * struct imx8qxp_ss_lpcg - Description of one subsystem LPCG clocks
451e3121bfSAisheng Dong * @lpcg: LPCG clocks array of one subsystem
461e3121bfSAisheng Dong * @num_lpcg: the number of LPCG clocks
471e3121bfSAisheng Dong * @num_max: the maximum number of LPCG clocks
481e3121bfSAisheng Dong *
491e3121bfSAisheng Dong * This structure describes each subsystem LPCG clocks information
501e3121bfSAisheng Dong * which then will be used to create respective LPCGs clocks
511e3121bfSAisheng Dong */
521e3121bfSAisheng Dong struct imx8qxp_ss_lpcg {
531e3121bfSAisheng Dong const struct imx8qxp_lpcg_data *lpcg;
541e3121bfSAisheng Dong u8 num_lpcg;
551e3121bfSAisheng Dong u8 num_max;
561e3121bfSAisheng Dong };
571e3121bfSAisheng Dong
581e3121bfSAisheng Dong static const struct imx8qxp_lpcg_data imx8qxp_lpcg_adma[] = {
5908972760SAisheng Dong { IMX_ADMA_LPCG_UART0_IPG_CLK, "uart0_lpcg_ipg_clk", "dma_ipg_clk_root", 0, ADMA_LPUART_0_LPCG, 16, 0, },
6008972760SAisheng Dong { IMX_ADMA_LPCG_UART0_BAUD_CLK, "uart0_lpcg_baud_clk", "uart0_clk", 0, ADMA_LPUART_0_LPCG, 0, 0, },
6108972760SAisheng Dong { IMX_ADMA_LPCG_UART1_IPG_CLK, "uart1_lpcg_ipg_clk", "dma_ipg_clk_root", 0, ADMA_LPUART_1_LPCG, 16, 0, },
6208972760SAisheng Dong { IMX_ADMA_LPCG_UART1_BAUD_CLK, "uart1_lpcg_baud_clk", "uart1_clk", 0, ADMA_LPUART_1_LPCG, 0, 0, },
6308972760SAisheng Dong { IMX_ADMA_LPCG_UART2_IPG_CLK, "uart2_lpcg_ipg_clk", "dma_ipg_clk_root", 0, ADMA_LPUART_2_LPCG, 16, 0, },
6408972760SAisheng Dong { IMX_ADMA_LPCG_UART2_BAUD_CLK, "uart2_lpcg_baud_clk", "uart2_clk", 0, ADMA_LPUART_2_LPCG, 0, 0, },
6508972760SAisheng Dong { IMX_ADMA_LPCG_UART3_IPG_CLK, "uart3_lpcg_ipg_clk", "dma_ipg_clk_root", 0, ADMA_LPUART_3_LPCG, 16, 0, },
6608972760SAisheng Dong { IMX_ADMA_LPCG_UART3_BAUD_CLK, "uart3_lpcg_baud_clk", "uart3_clk", 0, ADMA_LPUART_3_LPCG, 0, 0, },
6708972760SAisheng Dong { IMX_ADMA_LPCG_I2C0_IPG_CLK, "i2c0_lpcg_ipg_clk", "dma_ipg_clk_root", 0, ADMA_LPI2C_0_LPCG, 16, 0, },
6808972760SAisheng Dong { IMX_ADMA_LPCG_I2C0_CLK, "i2c0_lpcg_clk", "i2c0_clk", 0, ADMA_LPI2C_0_LPCG, 0, 0, },
6908972760SAisheng Dong { IMX_ADMA_LPCG_I2C1_IPG_CLK, "i2c1_lpcg_ipg_clk", "dma_ipg_clk_root", 0, ADMA_LPI2C_1_LPCG, 16, 0, },
7008972760SAisheng Dong { IMX_ADMA_LPCG_I2C1_CLK, "i2c1_lpcg_clk", "i2c1_clk", 0, ADMA_LPI2C_1_LPCG, 0, 0, },
7108972760SAisheng Dong { IMX_ADMA_LPCG_I2C2_IPG_CLK, "i2c2_lpcg_ipg_clk", "dma_ipg_clk_root", 0, ADMA_LPI2C_2_LPCG, 16, 0, },
7208972760SAisheng Dong { IMX_ADMA_LPCG_I2C2_CLK, "i2c2_lpcg_clk", "i2c2_clk", 0, ADMA_LPI2C_2_LPCG, 0, 0, },
7308972760SAisheng Dong { IMX_ADMA_LPCG_I2C3_IPG_CLK, "i2c3_lpcg_ipg_clk", "dma_ipg_clk_root", 0, ADMA_LPI2C_3_LPCG, 16, 0, },
7408972760SAisheng Dong { IMX_ADMA_LPCG_I2C3_CLK, "i2c3_lpcg_clk", "i2c3_clk", 0, ADMA_LPI2C_3_LPCG, 0, 0, },
756ad7cb71SDaniel Baluta
766ad7cb71SDaniel Baluta { IMX_ADMA_LPCG_DSP_CORE_CLK, "dsp_lpcg_core_clk", "dma_ipg_clk_root", 0, ADMA_HIFI_LPCG, 28, 0, },
776ad7cb71SDaniel Baluta { IMX_ADMA_LPCG_DSP_IPG_CLK, "dsp_lpcg_ipg_clk", "dma_ipg_clk_root", 0, ADMA_HIFI_LPCG, 20, 0, },
786ad7cb71SDaniel Baluta { IMX_ADMA_LPCG_DSP_ADB_CLK, "dsp_lpcg_adb_clk", "dma_ipg_clk_root", 0, ADMA_HIFI_LPCG, 16, 0, },
796ad7cb71SDaniel Baluta { IMX_ADMA_LPCG_OCRAM_IPG_CLK, "ocram_lpcg_ipg_clk", "dma_ipg_clk_root", 0, ADMA_OCRAM_LPCG, 16, 0, },
801e3121bfSAisheng Dong };
811e3121bfSAisheng Dong
821e3121bfSAisheng Dong static const struct imx8qxp_ss_lpcg imx8qxp_ss_adma = {
831e3121bfSAisheng Dong .lpcg = imx8qxp_lpcg_adma,
841e3121bfSAisheng Dong .num_lpcg = ARRAY_SIZE(imx8qxp_lpcg_adma),
8508972760SAisheng Dong .num_max = IMX_ADMA_LPCG_CLK_END,
861e3121bfSAisheng Dong };
871e3121bfSAisheng Dong
881e3121bfSAisheng Dong static const struct imx8qxp_lpcg_data imx8qxp_lpcg_conn[] = {
8908972760SAisheng Dong { IMX_CONN_LPCG_SDHC0_PER_CLK, "sdhc0_lpcg_per_clk", "sdhc0_clk", 0, CONN_USDHC_0_LPCG, 0, 0, },
9008972760SAisheng Dong { IMX_CONN_LPCG_SDHC0_IPG_CLK, "sdhc0_lpcg_ipg_clk", "conn_ipg_clk_root", 0, CONN_USDHC_0_LPCG, 16, 0, },
9108972760SAisheng Dong { IMX_CONN_LPCG_SDHC0_HCLK, "sdhc0_lpcg_ahb_clk", "conn_axi_clk_root", 0, CONN_USDHC_0_LPCG, 20, 0, },
9208972760SAisheng Dong { IMX_CONN_LPCG_SDHC1_PER_CLK, "sdhc1_lpcg_per_clk", "sdhc1_clk", 0, CONN_USDHC_1_LPCG, 0, 0, },
9308972760SAisheng Dong { IMX_CONN_LPCG_SDHC1_IPG_CLK, "sdhc1_lpcg_ipg_clk", "conn_ipg_clk_root", 0, CONN_USDHC_1_LPCG, 16, 0, },
9408972760SAisheng Dong { IMX_CONN_LPCG_SDHC1_HCLK, "sdhc1_lpcg_ahb_clk", "conn_axi_clk_root", 0, CONN_USDHC_1_LPCG, 20, 0, },
9508972760SAisheng Dong { IMX_CONN_LPCG_SDHC2_PER_CLK, "sdhc2_lpcg_per_clk", "sdhc2_clk", 0, CONN_USDHC_2_LPCG, 0, 0, },
9608972760SAisheng Dong { IMX_CONN_LPCG_SDHC2_IPG_CLK, "sdhc2_lpcg_ipg_clk", "conn_ipg_clk_root", 0, CONN_USDHC_2_LPCG, 16, 0, },
9708972760SAisheng Dong { IMX_CONN_LPCG_SDHC2_HCLK, "sdhc2_lpcg_ahb_clk", "conn_axi_clk_root", 0, CONN_USDHC_2_LPCG, 20, 0, },
9808972760SAisheng Dong { IMX_CONN_LPCG_ENET0_ROOT_CLK, "enet0_ipg_root_clk", "enet0_clk", 0, CONN_ENET_0_LPCG, 0, 0, },
9908972760SAisheng Dong { IMX_CONN_LPCG_ENET0_TX_CLK, "enet0_tx_clk", "enet0_clk", 0, CONN_ENET_0_LPCG, 4, 0, },
10008972760SAisheng Dong { IMX_CONN_LPCG_ENET0_AHB_CLK, "enet0_ahb_clk", "conn_axi_clk_root", 0, CONN_ENET_0_LPCG, 8, 0, },
10108972760SAisheng Dong { IMX_CONN_LPCG_ENET0_IPG_S_CLK, "enet0_ipg_s_clk", "conn_ipg_clk_root", 0, CONN_ENET_0_LPCG, 20, 0, },
10208972760SAisheng Dong { IMX_CONN_LPCG_ENET0_IPG_CLK, "enet0_ipg_clk", "enet0_ipg_s_clk", 0, CONN_ENET_0_LPCG, 16, 0, },
10308972760SAisheng Dong { IMX_CONN_LPCG_ENET1_ROOT_CLK, "enet1_ipg_root_clk", "enet1_clk", 0, CONN_ENET_1_LPCG, 0, 0, },
10408972760SAisheng Dong { IMX_CONN_LPCG_ENET1_TX_CLK, "enet1_tx_clk", "enet1_clk", 0, CONN_ENET_1_LPCG, 4, 0, },
10508972760SAisheng Dong { IMX_CONN_LPCG_ENET1_AHB_CLK, "enet1_ahb_clk", "conn_axi_clk_root", 0, CONN_ENET_1_LPCG, 8, 0, },
10608972760SAisheng Dong { IMX_CONN_LPCG_ENET1_IPG_S_CLK, "enet1_ipg_s_clk", "conn_ipg_clk_root", 0, CONN_ENET_1_LPCG, 20, 0, },
10708972760SAisheng Dong { IMX_CONN_LPCG_ENET1_IPG_CLK, "enet1_ipg_clk", "enet0_ipg_s_clk", 0, CONN_ENET_1_LPCG, 16, 0, },
1081e3121bfSAisheng Dong };
1091e3121bfSAisheng Dong
1101e3121bfSAisheng Dong static const struct imx8qxp_ss_lpcg imx8qxp_ss_conn = {
1111e3121bfSAisheng Dong .lpcg = imx8qxp_lpcg_conn,
1121e3121bfSAisheng Dong .num_lpcg = ARRAY_SIZE(imx8qxp_lpcg_conn),
11308972760SAisheng Dong .num_max = IMX_CONN_LPCG_CLK_END,
1141e3121bfSAisheng Dong };
1151e3121bfSAisheng Dong
1161e3121bfSAisheng Dong static const struct imx8qxp_lpcg_data imx8qxp_lpcg_lsio[] = {
11708972760SAisheng Dong { IMX_LSIO_LPCG_PWM0_IPG_CLK, "pwm0_lpcg_ipg_clk", "pwm0_clk", 0, LSIO_PWM_0_LPCG, 0, 0, },
11808972760SAisheng Dong { IMX_LSIO_LPCG_PWM0_IPG_HF_CLK, "pwm0_lpcg_ipg_hf_clk", "pwm0_clk", 0, LSIO_PWM_0_LPCG, 4, 0, },
11908972760SAisheng Dong { IMX_LSIO_LPCG_PWM0_IPG_S_CLK, "pwm0_lpcg_ipg_s_clk", "pwm0_clk", 0, LSIO_PWM_0_LPCG, 16, 0, },
12008972760SAisheng Dong { IMX_LSIO_LPCG_PWM0_IPG_SLV_CLK, "pwm0_lpcg_ipg_slv_clk", "lsio_bus_clk_root", 0, LSIO_PWM_0_LPCG, 20, 0, },
12108972760SAisheng Dong { IMX_LSIO_LPCG_PWM0_IPG_MSTR_CLK, "pwm0_lpcg_ipg_mstr_clk", "pwm0_clk", 0, LSIO_PWM_0_LPCG, 24, 0, },
12208972760SAisheng Dong { IMX_LSIO_LPCG_PWM1_IPG_CLK, "pwm1_lpcg_ipg_clk", "pwm1_clk", 0, LSIO_PWM_1_LPCG, 0, 0, },
12308972760SAisheng Dong { IMX_LSIO_LPCG_PWM1_IPG_HF_CLK, "pwm1_lpcg_ipg_hf_clk", "pwm1_clk", 0, LSIO_PWM_1_LPCG, 4, 0, },
12408972760SAisheng Dong { IMX_LSIO_LPCG_PWM1_IPG_S_CLK, "pwm1_lpcg_ipg_s_clk", "pwm1_clk", 0, LSIO_PWM_1_LPCG, 16, 0, },
12508972760SAisheng Dong { IMX_LSIO_LPCG_PWM1_IPG_SLV_CLK, "pwm1_lpcg_ipg_slv_clk", "lsio_bus_clk_root", 0, LSIO_PWM_1_LPCG, 20, 0, },
12608972760SAisheng Dong { IMX_LSIO_LPCG_PWM1_IPG_MSTR_CLK, "pwm1_lpcg_ipg_mstr_clk", "pwm1_clk", 0, LSIO_PWM_1_LPCG, 24, 0, },
12708972760SAisheng Dong { IMX_LSIO_LPCG_PWM2_IPG_CLK, "pwm2_lpcg_ipg_clk", "pwm2_clk", 0, LSIO_PWM_2_LPCG, 0, 0, },
12808972760SAisheng Dong { IMX_LSIO_LPCG_PWM2_IPG_HF_CLK, "pwm2_lpcg_ipg_hf_clk", "pwm2_clk", 0, LSIO_PWM_2_LPCG, 4, 0, },
12908972760SAisheng Dong { IMX_LSIO_LPCG_PWM2_IPG_S_CLK, "pwm2_lpcg_ipg_s_clk", "pwm2_clk", 0, LSIO_PWM_2_LPCG, 16, 0, },
13008972760SAisheng Dong { IMX_LSIO_LPCG_PWM2_IPG_SLV_CLK, "pwm2_lpcg_ipg_slv_clk", "lsio_bus_clk_root", 0, LSIO_PWM_2_LPCG, 20, 0, },
13108972760SAisheng Dong { IMX_LSIO_LPCG_PWM2_IPG_MSTR_CLK, "pwm2_lpcg_ipg_mstr_clk", "pwm2_clk", 0, LSIO_PWM_2_LPCG, 24, 0, },
13208972760SAisheng Dong { IMX_LSIO_LPCG_PWM3_IPG_CLK, "pwm3_lpcg_ipg_clk", "pwm3_clk", 0, LSIO_PWM_3_LPCG, 0, 0, },
13308972760SAisheng Dong { IMX_LSIO_LPCG_PWM3_IPG_HF_CLK, "pwm3_lpcg_ipg_hf_clk", "pwm3_clk", 0, LSIO_PWM_3_LPCG, 4, 0, },
13408972760SAisheng Dong { IMX_LSIO_LPCG_PWM3_IPG_S_CLK, "pwm3_lpcg_ipg_s_clk", "pwm3_clk", 0, LSIO_PWM_3_LPCG, 16, 0, },
13508972760SAisheng Dong { IMX_LSIO_LPCG_PWM3_IPG_SLV_CLK, "pwm3_lpcg_ipg_slv_clk", "lsio_bus_clk_root", 0, LSIO_PWM_3_LPCG, 20, 0, },
13608972760SAisheng Dong { IMX_LSIO_LPCG_PWM3_IPG_MSTR_CLK, "pwm3_lpcg_ipg_mstr_clk", "pwm3_clk", 0, LSIO_PWM_3_LPCG, 24, 0, },
13708972760SAisheng Dong { IMX_LSIO_LPCG_PWM4_IPG_CLK, "pwm4_lpcg_ipg_clk", "pwm4_clk", 0, LSIO_PWM_4_LPCG, 0, 0, },
13808972760SAisheng Dong { IMX_LSIO_LPCG_PWM4_IPG_HF_CLK, "pwm4_lpcg_ipg_hf_clk", "pwm4_clk", 0, LSIO_PWM_4_LPCG, 4, 0, },
13908972760SAisheng Dong { IMX_LSIO_LPCG_PWM4_IPG_S_CLK, "pwm4_lpcg_ipg_s_clk", "pwm4_clk", 0, LSIO_PWM_4_LPCG, 16, 0, },
14008972760SAisheng Dong { IMX_LSIO_LPCG_PWM4_IPG_SLV_CLK, "pwm4_lpcg_ipg_slv_clk", "lsio_bus_clk_root", 0, LSIO_PWM_4_LPCG, 20, 0, },
14108972760SAisheng Dong { IMX_LSIO_LPCG_PWM4_IPG_MSTR_CLK, "pwm4_lpcg_ipg_mstr_clk", "pwm4_clk", 0, LSIO_PWM_4_LPCG, 24, 0, },
14208972760SAisheng Dong { IMX_LSIO_LPCG_PWM5_IPG_CLK, "pwm5_lpcg_ipg_clk", "pwm5_clk", 0, LSIO_PWM_5_LPCG, 0, 0, },
14308972760SAisheng Dong { IMX_LSIO_LPCG_PWM5_IPG_HF_CLK, "pwm5_lpcg_ipg_hf_clk", "pwm5_clk", 0, LSIO_PWM_5_LPCG, 4, 0, },
14408972760SAisheng Dong { IMX_LSIO_LPCG_PWM5_IPG_S_CLK, "pwm5_lpcg_ipg_s_clk", "pwm5_clk", 0, LSIO_PWM_5_LPCG, 16, 0, },
14508972760SAisheng Dong { IMX_LSIO_LPCG_PWM5_IPG_SLV_CLK, "pwm5_lpcg_ipg_slv_clk", "lsio_bus_clk_root", 0, LSIO_PWM_5_LPCG, 20, 0, },
14608972760SAisheng Dong { IMX_LSIO_LPCG_PWM5_IPG_MSTR_CLK, "pwm5_lpcg_ipg_mstr_clk", "pwm5_clk", 0, LSIO_PWM_5_LPCG, 24, 0, },
14708972760SAisheng Dong { IMX_LSIO_LPCG_PWM6_IPG_CLK, "pwm6_lpcg_ipg_clk", "pwm6_clk", 0, LSIO_PWM_6_LPCG, 0, 0, },
14808972760SAisheng Dong { IMX_LSIO_LPCG_PWM6_IPG_HF_CLK, "pwm6_lpcg_ipg_hf_clk", "pwm6_clk", 0, LSIO_PWM_6_LPCG, 4, 0, },
14908972760SAisheng Dong { IMX_LSIO_LPCG_PWM6_IPG_S_CLK, "pwm6_lpcg_ipg_s_clk", "pwm6_clk", 0, LSIO_PWM_6_LPCG, 16, 0, },
15008972760SAisheng Dong { IMX_LSIO_LPCG_PWM6_IPG_SLV_CLK, "pwm6_lpcg_ipg_slv_clk", "lsio_bus_clk_root", 0, LSIO_PWM_6_LPCG, 20, 0, },
15108972760SAisheng Dong { IMX_LSIO_LPCG_PWM6_IPG_MSTR_CLK, "pwm6_lpcg_ipg_mstr_clk", "pwm6_clk", 0, LSIO_PWM_6_LPCG, 24, 0, },
1521e3121bfSAisheng Dong };
1531e3121bfSAisheng Dong
1541e3121bfSAisheng Dong static const struct imx8qxp_ss_lpcg imx8qxp_ss_lsio = {
1551e3121bfSAisheng Dong .lpcg = imx8qxp_lpcg_lsio,
1561e3121bfSAisheng Dong .num_lpcg = ARRAY_SIZE(imx8qxp_lpcg_lsio),
15708972760SAisheng Dong .num_max = IMX_LSIO_LPCG_CLK_END,
1581e3121bfSAisheng Dong };
1591e3121bfSAisheng Dong
160d5f1e6a2SDong Aisheng #define IMX_LPCG_MAX_CLKS 8
161d5f1e6a2SDong Aisheng
imx_lpcg_of_clk_src_get(struct of_phandle_args * clkspec,void * data)162d5f1e6a2SDong Aisheng static struct clk_hw *imx_lpcg_of_clk_src_get(struct of_phandle_args *clkspec,
163d5f1e6a2SDong Aisheng void *data)
164d5f1e6a2SDong Aisheng {
165d5f1e6a2SDong Aisheng struct clk_hw_onecell_data *hw_data = data;
166d5f1e6a2SDong Aisheng unsigned int idx = clkspec->args[0] / 4;
167d5f1e6a2SDong Aisheng
168d5f1e6a2SDong Aisheng if (idx >= hw_data->num) {
169d5f1e6a2SDong Aisheng pr_err("%s: invalid index %u\n", __func__, idx);
170d5f1e6a2SDong Aisheng return ERR_PTR(-EINVAL);
171d5f1e6a2SDong Aisheng }
172d5f1e6a2SDong Aisheng
173d5f1e6a2SDong Aisheng return hw_data->hws[idx];
174d5f1e6a2SDong Aisheng }
175d5f1e6a2SDong Aisheng
imx_lpcg_parse_clks_from_dt(struct platform_device * pdev,struct device_node * np)176d5f1e6a2SDong Aisheng static int imx_lpcg_parse_clks_from_dt(struct platform_device *pdev,
177d5f1e6a2SDong Aisheng struct device_node *np)
178d5f1e6a2SDong Aisheng {
179d5f1e6a2SDong Aisheng const char *output_names[IMX_LPCG_MAX_CLKS];
180d5f1e6a2SDong Aisheng const char *parent_names[IMX_LPCG_MAX_CLKS];
181d5f1e6a2SDong Aisheng unsigned int bit_offset[IMX_LPCG_MAX_CLKS];
182d5f1e6a2SDong Aisheng struct clk_hw_onecell_data *clk_data;
183d5f1e6a2SDong Aisheng struct clk_hw **clk_hws;
184d5f1e6a2SDong Aisheng void __iomem *base;
185d5f1e6a2SDong Aisheng int count;
186d5f1e6a2SDong Aisheng int idx;
187d5f1e6a2SDong Aisheng int ret;
188d5f1e6a2SDong Aisheng int i;
189d5f1e6a2SDong Aisheng
190d5f1e6a2SDong Aisheng if (!of_device_is_compatible(np, "fsl,imx8qxp-lpcg"))
191d5f1e6a2SDong Aisheng return -EINVAL;
192d5f1e6a2SDong Aisheng
193*94945b23SYangtao Li base = devm_platform_ioremap_resource(pdev, 0);
194d5f1e6a2SDong Aisheng if (IS_ERR(base))
195d5f1e6a2SDong Aisheng return PTR_ERR(base);
196d5f1e6a2SDong Aisheng
197d5f1e6a2SDong Aisheng count = of_property_count_u32_elems(np, "clock-indices");
198d5f1e6a2SDong Aisheng if (count < 0) {
199d5f1e6a2SDong Aisheng dev_err(&pdev->dev, "failed to count clocks\n");
200d5f1e6a2SDong Aisheng return -EINVAL;
201d5f1e6a2SDong Aisheng }
202d5f1e6a2SDong Aisheng
203d5f1e6a2SDong Aisheng /*
204d5f1e6a2SDong Aisheng * A trick here is that we set the num of clks to the MAX instead
205d5f1e6a2SDong Aisheng * of the count from clock-indices because one LPCG supports up to
206d5f1e6a2SDong Aisheng * 8 clock outputs which each of them is fixed to 4 bits. Then we can
207d5f1e6a2SDong Aisheng * easily get the clock by clk-indices (bit-offset) / 4.
208d5f1e6a2SDong Aisheng * And the cost is very limited few pointers.
209d5f1e6a2SDong Aisheng */
210d5f1e6a2SDong Aisheng
211d5f1e6a2SDong Aisheng clk_data = devm_kzalloc(&pdev->dev, struct_size(clk_data, hws,
212d5f1e6a2SDong Aisheng IMX_LPCG_MAX_CLKS), GFP_KERNEL);
213d5f1e6a2SDong Aisheng if (!clk_data)
214d5f1e6a2SDong Aisheng return -ENOMEM;
215d5f1e6a2SDong Aisheng
216d5f1e6a2SDong Aisheng clk_data->num = IMX_LPCG_MAX_CLKS;
217d5f1e6a2SDong Aisheng clk_hws = clk_data->hws;
218d5f1e6a2SDong Aisheng
219d5f1e6a2SDong Aisheng ret = of_property_read_u32_array(np, "clock-indices", bit_offset,
220d5f1e6a2SDong Aisheng count);
221d5f1e6a2SDong Aisheng if (ret < 0) {
222d5f1e6a2SDong Aisheng dev_err(&pdev->dev, "failed to read clock-indices\n");
223d5f1e6a2SDong Aisheng return -EINVAL;
224d5f1e6a2SDong Aisheng }
225d5f1e6a2SDong Aisheng
226d5f1e6a2SDong Aisheng ret = of_clk_parent_fill(np, parent_names, count);
227d5f1e6a2SDong Aisheng if (ret != count) {
228d5f1e6a2SDong Aisheng dev_err(&pdev->dev, "failed to get clock parent names\n");
229d5f1e6a2SDong Aisheng return count;
230d5f1e6a2SDong Aisheng }
231d5f1e6a2SDong Aisheng
232d5f1e6a2SDong Aisheng ret = of_property_read_string_array(np, "clock-output-names",
233d5f1e6a2SDong Aisheng output_names, count);
234d5f1e6a2SDong Aisheng if (ret != count) {
235d5f1e6a2SDong Aisheng dev_err(&pdev->dev, "failed to read clock-output-names\n");
236d5f1e6a2SDong Aisheng return -EINVAL;
237d5f1e6a2SDong Aisheng }
238d5f1e6a2SDong Aisheng
23918cdbad4SDong Aisheng pm_runtime_get_noresume(&pdev->dev);
24018cdbad4SDong Aisheng pm_runtime_set_active(&pdev->dev);
24118cdbad4SDong Aisheng pm_runtime_set_autosuspend_delay(&pdev->dev, 500);
24218cdbad4SDong Aisheng pm_runtime_use_autosuspend(&pdev->dev);
24318cdbad4SDong Aisheng pm_runtime_enable(&pdev->dev);
24418cdbad4SDong Aisheng
245d5f1e6a2SDong Aisheng for (i = 0; i < count; i++) {
246d5f1e6a2SDong Aisheng idx = bit_offset[i] / 4;
247135efc3aSDan Carpenter if (idx >= IMX_LPCG_MAX_CLKS) {
248d5f1e6a2SDong Aisheng dev_warn(&pdev->dev, "invalid bit offset of clock %d\n",
249d5f1e6a2SDong Aisheng i);
250d5f1e6a2SDong Aisheng ret = -EINVAL;
251d5f1e6a2SDong Aisheng goto unreg;
252d5f1e6a2SDong Aisheng }
253d5f1e6a2SDong Aisheng
25418cdbad4SDong Aisheng clk_hws[idx] = imx_clk_lpcg_scu_dev(&pdev->dev, output_names[i],
255d5f1e6a2SDong Aisheng parent_names[i], 0, base,
256d5f1e6a2SDong Aisheng bit_offset[i], false);
257d5f1e6a2SDong Aisheng if (IS_ERR(clk_hws[idx])) {
258d5f1e6a2SDong Aisheng dev_warn(&pdev->dev, "failed to register clock %d\n",
259d5f1e6a2SDong Aisheng idx);
260d5f1e6a2SDong Aisheng ret = PTR_ERR(clk_hws[idx]);
261d5f1e6a2SDong Aisheng goto unreg;
262d5f1e6a2SDong Aisheng }
263d5f1e6a2SDong Aisheng }
264d5f1e6a2SDong Aisheng
265d5f1e6a2SDong Aisheng ret = devm_of_clk_add_hw_provider(&pdev->dev, imx_lpcg_of_clk_src_get,
266d5f1e6a2SDong Aisheng clk_data);
26718cdbad4SDong Aisheng if (ret)
26818cdbad4SDong Aisheng goto unreg;
26918cdbad4SDong Aisheng
27018cdbad4SDong Aisheng pm_runtime_mark_last_busy(&pdev->dev);
27118cdbad4SDong Aisheng pm_runtime_put_autosuspend(&pdev->dev);
27218cdbad4SDong Aisheng
273d5f1e6a2SDong Aisheng return 0;
274d5f1e6a2SDong Aisheng
275d5f1e6a2SDong Aisheng unreg:
276d5f1e6a2SDong Aisheng while (--i >= 0) {
277d5f1e6a2SDong Aisheng idx = bit_offset[i] / 4;
278d5f1e6a2SDong Aisheng if (clk_hws[idx])
279d5f1e6a2SDong Aisheng imx_clk_lpcg_scu_unregister(clk_hws[idx]);
280d5f1e6a2SDong Aisheng }
281d5f1e6a2SDong Aisheng
28218cdbad4SDong Aisheng pm_runtime_disable(&pdev->dev);
28318cdbad4SDong Aisheng
284d5f1e6a2SDong Aisheng return ret;
285d5f1e6a2SDong Aisheng }
286d5f1e6a2SDong Aisheng
imx8qxp_lpcg_clk_probe(struct platform_device * pdev)2871e3121bfSAisheng Dong static int imx8qxp_lpcg_clk_probe(struct platform_device *pdev)
2881e3121bfSAisheng Dong {
2891e3121bfSAisheng Dong struct device *dev = &pdev->dev;
2901e3121bfSAisheng Dong struct device_node *np = dev->of_node;
2911e3121bfSAisheng Dong struct clk_hw_onecell_data *clk_data;
2921e3121bfSAisheng Dong const struct imx8qxp_ss_lpcg *ss_lpcg;
2931e3121bfSAisheng Dong const struct imx8qxp_lpcg_data *lpcg;
2941e3121bfSAisheng Dong struct resource *res;
2951e3121bfSAisheng Dong struct clk_hw **clks;
2961e3121bfSAisheng Dong void __iomem *base;
297d5f1e6a2SDong Aisheng int ret;
2981e3121bfSAisheng Dong int i;
2991e3121bfSAisheng Dong
300d5f1e6a2SDong Aisheng /* try new binding to parse clocks from device tree first */
301d5f1e6a2SDong Aisheng ret = imx_lpcg_parse_clks_from_dt(pdev, np);
302d5f1e6a2SDong Aisheng if (!ret)
303d5f1e6a2SDong Aisheng return 0;
304d5f1e6a2SDong Aisheng
3051e3121bfSAisheng Dong ss_lpcg = of_device_get_match_data(dev);
3061e3121bfSAisheng Dong if (!ss_lpcg)
3071e3121bfSAisheng Dong return -ENODEV;
3081e3121bfSAisheng Dong
309249fce6fSLeonard Crestez /*
310249fce6fSLeonard Crestez * Please don't replace this with devm_platform_ioremap_resource.
311249fce6fSLeonard Crestez *
312249fce6fSLeonard Crestez * devm_platform_ioremap_resource calls devm_ioremap_resource which
313249fce6fSLeonard Crestez * differs from devm_ioremap by also calling devm_request_mem_region
314249fce6fSLeonard Crestez * and preventing other mappings in the same area.
315249fce6fSLeonard Crestez *
316249fce6fSLeonard Crestez * On imx8 the LPCG nodes map entire subsystems and overlap
317249fce6fSLeonard Crestez * peripherals, this means that using devm_platform_ioremap_resource
318249fce6fSLeonard Crestez * will cause many devices to fail to probe including serial ports.
319249fce6fSLeonard Crestez */
3201e3121bfSAisheng Dong res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
32148504619SWei Yongjun if (!res)
32248504619SWei Yongjun return -EINVAL;
3231e3121bfSAisheng Dong base = devm_ioremap(dev, res->start, resource_size(res));
3241e3121bfSAisheng Dong if (!base)
3251e3121bfSAisheng Dong return -ENOMEM;
3261e3121bfSAisheng Dong
3271e3121bfSAisheng Dong clk_data = devm_kzalloc(&pdev->dev, struct_size(clk_data, hws,
3281e3121bfSAisheng Dong ss_lpcg->num_max), GFP_KERNEL);
3291e3121bfSAisheng Dong if (!clk_data)
3301e3121bfSAisheng Dong return -ENOMEM;
3311e3121bfSAisheng Dong
3321e3121bfSAisheng Dong clk_data->num = ss_lpcg->num_max;
3331e3121bfSAisheng Dong clks = clk_data->hws;
3341e3121bfSAisheng Dong
3351e3121bfSAisheng Dong for (i = 0; i < ss_lpcg->num_lpcg; i++) {
3361e3121bfSAisheng Dong lpcg = ss_lpcg->lpcg + i;
3371e3121bfSAisheng Dong clks[lpcg->id] = imx_clk_lpcg_scu(lpcg->name, lpcg->parent,
3381e3121bfSAisheng Dong lpcg->flags, base + lpcg->offset,
3391e3121bfSAisheng Dong lpcg->bit_idx, lpcg->hw_gate);
3401e3121bfSAisheng Dong }
3411e3121bfSAisheng Dong
3421e3121bfSAisheng Dong for (i = 0; i < clk_data->num; i++) {
3431e3121bfSAisheng Dong if (IS_ERR(clks[i]))
3441e3121bfSAisheng Dong pr_warn("i.MX clk %u: register failed with %ld\n",
3451e3121bfSAisheng Dong i, PTR_ERR(clks[i]));
3461e3121bfSAisheng Dong }
3471e3121bfSAisheng Dong
3481e3121bfSAisheng Dong return of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_data);
3491e3121bfSAisheng Dong }
3501e3121bfSAisheng Dong
3511e3121bfSAisheng Dong static const struct of_device_id imx8qxp_lpcg_match[] = {
3521e3121bfSAisheng Dong { .compatible = "fsl,imx8qxp-lpcg-adma", &imx8qxp_ss_adma, },
3531e3121bfSAisheng Dong { .compatible = "fsl,imx8qxp-lpcg-conn", &imx8qxp_ss_conn, },
3541e3121bfSAisheng Dong { .compatible = "fsl,imx8qxp-lpcg-lsio", &imx8qxp_ss_lsio, },
355d5f1e6a2SDong Aisheng { .compatible = "fsl,imx8qxp-lpcg", NULL },
3561e3121bfSAisheng Dong { /* sentinel */ }
3571e3121bfSAisheng Dong };
3581e3121bfSAisheng Dong
3591e3121bfSAisheng Dong static struct platform_driver imx8qxp_lpcg_clk_driver = {
3601e3121bfSAisheng Dong .driver = {
3611e3121bfSAisheng Dong .name = "imx8qxp-lpcg-clk",
3621e3121bfSAisheng Dong .of_match_table = imx8qxp_lpcg_match,
363ea0c5cbaSDong Aisheng .pm = &imx_clk_lpcg_scu_pm_ops,
3641e3121bfSAisheng Dong .suppress_bind_attrs = true,
3651e3121bfSAisheng Dong },
3661e3121bfSAisheng Dong .probe = imx8qxp_lpcg_clk_probe,
3671e3121bfSAisheng Dong };
3681e3121bfSAisheng Dong
369eee377b8SMiles Chen module_platform_driver(imx8qxp_lpcg_clk_driver);
370e0d0d4d8SAnson Huang
371e0d0d4d8SAnson Huang MODULE_AUTHOR("Aisheng Dong <aisheng.dong@nxp.com>");
372e0d0d4d8SAnson Huang MODULE_DESCRIPTION("NXP i.MX8QXP LPCG clock driver");
373e0d0d4d8SAnson Huang MODULE_LICENSE("GPL v2");
374