1d3a0946dSShengjiu Wang // SPDX-License-Identifier: GPL-2.0+
2d3a0946dSShengjiu Wang //
3d3a0946dSShengjiu Wang // Copyright 2023 NXP
4d3a0946dSShengjiu Wang //
5d3a0946dSShengjiu Wang
6d3a0946dSShengjiu Wang #include <dt-bindings/clock/imx8-clock.h>
7d3a0946dSShengjiu Wang #include <linux/clk-provider.h>
8d3a0946dSShengjiu Wang #include <linux/device.h>
9d3a0946dSShengjiu Wang #include <linux/err.h>
10d3a0946dSShengjiu Wang #include <linux/io.h>
11d3a0946dSShengjiu Wang #include <linux/module.h>
12d3a0946dSShengjiu Wang #include <linux/of.h>
13d3a0946dSShengjiu Wang #include <linux/of_device.h>
14d3a0946dSShengjiu Wang #include <linux/platform_device.h>
15d3a0946dSShengjiu Wang #include <linux/pm_domain.h>
16d3a0946dSShengjiu Wang #include <linux/pm_runtime.h>
17d3a0946dSShengjiu Wang #include <linux/slab.h>
18d3a0946dSShengjiu Wang
19d3a0946dSShengjiu Wang #include "clk.h"
20d3a0946dSShengjiu Wang
21d3a0946dSShengjiu Wang /**
22d3a0946dSShengjiu Wang * struct clk_imx_acm_pm_domains - structure for multi power domain
23d3a0946dSShengjiu Wang * @pd_dev: power domain device
24d3a0946dSShengjiu Wang * @pd_dev_link: power domain device link
25d3a0946dSShengjiu Wang * @num_domains: power domain nummber
26d3a0946dSShengjiu Wang */
27d3a0946dSShengjiu Wang struct clk_imx_acm_pm_domains {
28d3a0946dSShengjiu Wang struct device **pd_dev;
29d3a0946dSShengjiu Wang struct device_link **pd_dev_link;
30d3a0946dSShengjiu Wang int num_domains;
31d3a0946dSShengjiu Wang };
32d3a0946dSShengjiu Wang
33d3a0946dSShengjiu Wang /**
34d3a0946dSShengjiu Wang * struct clk_imx8_acm_sel - for clock mux
35d3a0946dSShengjiu Wang * @name: clock name
36d3a0946dSShengjiu Wang * @clkid: clock id
37d3a0946dSShengjiu Wang * @parents: clock parents
38d3a0946dSShengjiu Wang * @num_parents: clock parents number
39d3a0946dSShengjiu Wang * @reg: register offset
40d3a0946dSShengjiu Wang * @shift: bit shift in register
41d3a0946dSShengjiu Wang * @width: bits width
42d3a0946dSShengjiu Wang */
43d3a0946dSShengjiu Wang struct clk_imx8_acm_sel {
44d3a0946dSShengjiu Wang const char *name;
45d3a0946dSShengjiu Wang int clkid;
46d3a0946dSShengjiu Wang const struct clk_parent_data *parents; /* For mux */
47d3a0946dSShengjiu Wang int num_parents;
48d3a0946dSShengjiu Wang u32 reg;
49d3a0946dSShengjiu Wang u8 shift;
50d3a0946dSShengjiu Wang u8 width;
51d3a0946dSShengjiu Wang };
52d3a0946dSShengjiu Wang
53d3a0946dSShengjiu Wang /**
54d3a0946dSShengjiu Wang * struct imx8_acm_soc_data - soc specific data
55d3a0946dSShengjiu Wang * @sels: pointer to struct clk_imx8_acm_sel
56d3a0946dSShengjiu Wang * @num_sels: numbers of items
5735121e9dSShengjiu Wang * @mclk_sels: pointer to imx8qm/qxp/dxl_mclk_sels
58d3a0946dSShengjiu Wang */
59d3a0946dSShengjiu Wang struct imx8_acm_soc_data {
60d3a0946dSShengjiu Wang struct clk_imx8_acm_sel *sels;
61d3a0946dSShengjiu Wang unsigned int num_sels;
6235121e9dSShengjiu Wang struct clk_parent_data *mclk_sels;
63d3a0946dSShengjiu Wang };
64d3a0946dSShengjiu Wang
65d3a0946dSShengjiu Wang /**
66d3a0946dSShengjiu Wang * struct imx8_acm_priv - private structure
67d3a0946dSShengjiu Wang * @dev_pm: multi power domain
68d3a0946dSShengjiu Wang * @soc_data: pointer to soc data
69d3a0946dSShengjiu Wang * @reg: base address of registers
70d3a0946dSShengjiu Wang * @regs: save registers for suspend
71d3a0946dSShengjiu Wang */
72d3a0946dSShengjiu Wang struct imx8_acm_priv {
73d3a0946dSShengjiu Wang struct clk_imx_acm_pm_domains dev_pm;
74d3a0946dSShengjiu Wang const struct imx8_acm_soc_data *soc_data;
75d3a0946dSShengjiu Wang void __iomem *reg;
76d3a0946dSShengjiu Wang u32 regs[IMX_ADMA_ACM_CLK_END];
77d3a0946dSShengjiu Wang };
78d3a0946dSShengjiu Wang
79d3a0946dSShengjiu Wang static const struct clk_parent_data imx8qm_aud_clk_sels[] = {
80d3a0946dSShengjiu Wang { .fw_name = "aud_rec_clk0_lpcg_clk" },
81d3a0946dSShengjiu Wang { .fw_name = "aud_rec_clk1_lpcg_clk" },
82a699148bSPeng Fan { .fw_name = "dummy" },
83d3a0946dSShengjiu Wang { .fw_name = "hdmi_rx_mclk" },
84d3a0946dSShengjiu Wang { .fw_name = "ext_aud_mclk0" },
85d3a0946dSShengjiu Wang { .fw_name = "ext_aud_mclk1" },
86d3a0946dSShengjiu Wang { .fw_name = "esai0_rx_clk" },
87d3a0946dSShengjiu Wang { .fw_name = "esai0_rx_hf_clk" },
88d3a0946dSShengjiu Wang { .fw_name = "esai0_tx_clk" },
89d3a0946dSShengjiu Wang { .fw_name = "esai0_tx_hf_clk" },
90d3a0946dSShengjiu Wang { .fw_name = "esai1_rx_clk" },
91d3a0946dSShengjiu Wang { .fw_name = "esai1_rx_hf_clk" },
92d3a0946dSShengjiu Wang { .fw_name = "esai1_tx_clk" },
93d3a0946dSShengjiu Wang { .fw_name = "esai1_tx_hf_clk" },
94d3a0946dSShengjiu Wang { .fw_name = "spdif0_rx" },
95d3a0946dSShengjiu Wang { .fw_name = "spdif1_rx" },
96d3a0946dSShengjiu Wang { .fw_name = "sai0_rx_bclk" },
97d3a0946dSShengjiu Wang { .fw_name = "sai0_tx_bclk" },
98d3a0946dSShengjiu Wang { .fw_name = "sai1_rx_bclk" },
99d3a0946dSShengjiu Wang { .fw_name = "sai1_tx_bclk" },
100d3a0946dSShengjiu Wang { .fw_name = "sai2_rx_bclk" },
101d3a0946dSShengjiu Wang { .fw_name = "sai3_rx_bclk" },
102d3a0946dSShengjiu Wang { .fw_name = "sai4_rx_bclk" },
103d3a0946dSShengjiu Wang };
104d3a0946dSShengjiu Wang
105d3a0946dSShengjiu Wang static const struct clk_parent_data imx8qm_mclk_out_sels[] = {
106d3a0946dSShengjiu Wang { .fw_name = "aud_rec_clk0_lpcg_clk" },
107d3a0946dSShengjiu Wang { .fw_name = "aud_rec_clk1_lpcg_clk" },
108a699148bSPeng Fan { .fw_name = "dummy" },
109d3a0946dSShengjiu Wang { .fw_name = "hdmi_rx_mclk" },
110d3a0946dSShengjiu Wang { .fw_name = "spdif0_rx" },
111d3a0946dSShengjiu Wang { .fw_name = "spdif1_rx" },
112d3a0946dSShengjiu Wang { .fw_name = "sai4_rx_bclk" },
113d3a0946dSShengjiu Wang { .fw_name = "sai6_rx_bclk" },
114d3a0946dSShengjiu Wang };
115d3a0946dSShengjiu Wang
11635121e9dSShengjiu Wang #define ACM_AUD_CLK0_SEL_INDEX 2
11735121e9dSShengjiu Wang #define ACM_AUD_CLK1_SEL_INDEX 3
11835121e9dSShengjiu Wang
11935121e9dSShengjiu Wang static struct clk_parent_data imx8qm_mclk_sels[] = {
120d3a0946dSShengjiu Wang { .fw_name = "aud_pll_div_clk0_lpcg_clk" },
121d3a0946dSShengjiu Wang { .fw_name = "aud_pll_div_clk1_lpcg_clk" },
12235121e9dSShengjiu Wang { }, /* clk_hw pointer of "acm_aud_clk0_sel" */
12335121e9dSShengjiu Wang { }, /* clk_hw pointer of "acm_aud_clk1_sel" */
124d3a0946dSShengjiu Wang };
125d3a0946dSShengjiu Wang
126d3a0946dSShengjiu Wang static const struct clk_parent_data imx8qm_asrc_mux_clk_sels[] = {
127d3a0946dSShengjiu Wang { .fw_name = "sai4_rx_bclk" },
128d3a0946dSShengjiu Wang { .fw_name = "sai5_tx_bclk" },
129d3a0946dSShengjiu Wang { .index = -1 },
130a699148bSPeng Fan { .fw_name = "dummy" },
131d3a0946dSShengjiu Wang
132d3a0946dSShengjiu Wang };
133d3a0946dSShengjiu Wang
134d3a0946dSShengjiu Wang static struct clk_imx8_acm_sel imx8qm_sels[] = {
135d3a0946dSShengjiu Wang { "acm_aud_clk0_sel", IMX_ADMA_ACM_AUD_CLK0_SEL, imx8qm_aud_clk_sels, ARRAY_SIZE(imx8qm_aud_clk_sels), 0x000000, 0, 5 },
136d3a0946dSShengjiu Wang { "acm_aud_clk1_sel", IMX_ADMA_ACM_AUD_CLK1_SEL, imx8qm_aud_clk_sels, ARRAY_SIZE(imx8qm_aud_clk_sels), 0x010000, 0, 5 },
137d3a0946dSShengjiu Wang { "acm_mclkout0_sel", IMX_ADMA_ACM_MCLKOUT0_SEL, imx8qm_mclk_out_sels, ARRAY_SIZE(imx8qm_mclk_out_sels), 0x020000, 0, 3 },
138d3a0946dSShengjiu Wang { "acm_mclkout1_sel", IMX_ADMA_ACM_MCLKOUT1_SEL, imx8qm_mclk_out_sels, ARRAY_SIZE(imx8qm_mclk_out_sels), 0x030000, 0, 3 },
139d3a0946dSShengjiu Wang { "acm_asrc0_mclk_sel", IMX_ADMA_ACM_ASRC0_MUX_CLK_SEL, imx8qm_asrc_mux_clk_sels, ARRAY_SIZE(imx8qm_asrc_mux_clk_sels), 0x040000, 0, 2 },
140d3a0946dSShengjiu Wang { "acm_esai0_mclk_sel", IMX_ADMA_ACM_ESAI0_MCLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x060000, 0, 2 },
141d3a0946dSShengjiu Wang { "acm_esai1_mclk_sel", IMX_ADMA_ACM_ESAI1_MCLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x070000, 0, 2 },
142d3a0946dSShengjiu Wang { "acm_sai0_mclk_sel", IMX_ADMA_ACM_SAI0_MCLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x0E0000, 0, 2 },
143d3a0946dSShengjiu Wang { "acm_sai1_mclk_sel", IMX_ADMA_ACM_SAI1_MCLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x0F0000, 0, 2 },
144d3a0946dSShengjiu Wang { "acm_sai2_mclk_sel", IMX_ADMA_ACM_SAI2_MCLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x100000, 0, 2 },
145d3a0946dSShengjiu Wang { "acm_sai3_mclk_sel", IMX_ADMA_ACM_SAI3_MCLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x110000, 0, 2 },
146d3a0946dSShengjiu Wang { "acm_sai4_mclk_sel", IMX_ADMA_ACM_SAI4_MCLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x120000, 0, 2 },
147d3a0946dSShengjiu Wang { "acm_sai5_mclk_sel", IMX_ADMA_ACM_SAI5_MCLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x130000, 0, 2 },
148d3a0946dSShengjiu Wang { "acm_sai6_mclk_sel", IMX_ADMA_ACM_SAI6_MCLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x140000, 0, 2 },
149d3a0946dSShengjiu Wang { "acm_sai7_mclk_sel", IMX_ADMA_ACM_SAI7_MCLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x150000, 0, 2 },
150d3a0946dSShengjiu Wang { "acm_spdif0_mclk_sel", IMX_ADMA_ACM_SPDIF0_TX_CLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x1A0000, 0, 2 },
151d3a0946dSShengjiu Wang { "acm_spdif1_mclk_sel", IMX_ADMA_ACM_SPDIF1_TX_CLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x1B0000, 0, 2 },
152d3a0946dSShengjiu Wang { "acm_mqs_mclk_sel", IMX_ADMA_ACM_MQS_TX_CLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x1C0000, 0, 2 },
153d3a0946dSShengjiu Wang };
154d3a0946dSShengjiu Wang
155d3a0946dSShengjiu Wang static const struct clk_parent_data imx8qxp_aud_clk_sels[] = {
156d3a0946dSShengjiu Wang { .fw_name = "aud_rec_clk0_lpcg_clk" },
157d3a0946dSShengjiu Wang { .fw_name = "aud_rec_clk1_lpcg_clk" },
158d3a0946dSShengjiu Wang { .fw_name = "ext_aud_mclk0" },
159d3a0946dSShengjiu Wang { .fw_name = "ext_aud_mclk1" },
160d3a0946dSShengjiu Wang { .fw_name = "esai0_rx_clk" },
161d3a0946dSShengjiu Wang { .fw_name = "esai0_rx_hf_clk" },
162d3a0946dSShengjiu Wang { .fw_name = "esai0_tx_clk" },
163d3a0946dSShengjiu Wang { .fw_name = "esai0_tx_hf_clk" },
164d3a0946dSShengjiu Wang { .fw_name = "spdif0_rx" },
165d3a0946dSShengjiu Wang { .fw_name = "sai0_rx_bclk" },
166d3a0946dSShengjiu Wang { .fw_name = "sai0_tx_bclk" },
167d3a0946dSShengjiu Wang { .fw_name = "sai1_rx_bclk" },
168d3a0946dSShengjiu Wang { .fw_name = "sai1_tx_bclk" },
169d3a0946dSShengjiu Wang { .fw_name = "sai2_rx_bclk" },
170d3a0946dSShengjiu Wang { .fw_name = "sai3_rx_bclk" },
171d3a0946dSShengjiu Wang };
172d3a0946dSShengjiu Wang
173d3a0946dSShengjiu Wang static const struct clk_parent_data imx8qxp_mclk_out_sels[] = {
174d3a0946dSShengjiu Wang { .fw_name = "aud_rec_clk0_lpcg_clk" },
175d3a0946dSShengjiu Wang { .fw_name = "aud_rec_clk1_lpcg_clk" },
176d3a0946dSShengjiu Wang { .index = -1 },
177d3a0946dSShengjiu Wang { .index = -1 },
178d3a0946dSShengjiu Wang { .fw_name = "spdif0_rx" },
179d3a0946dSShengjiu Wang { .index = -1 },
180d3a0946dSShengjiu Wang { .index = -1 },
181d3a0946dSShengjiu Wang { .fw_name = "sai4_rx_bclk" },
182d3a0946dSShengjiu Wang };
183d3a0946dSShengjiu Wang
18435121e9dSShengjiu Wang static struct clk_parent_data imx8qxp_mclk_sels[] = {
185d3a0946dSShengjiu Wang { .fw_name = "aud_pll_div_clk0_lpcg_clk" },
186d3a0946dSShengjiu Wang { .fw_name = "aud_pll_div_clk1_lpcg_clk" },
18735121e9dSShengjiu Wang { }, /* clk_hw pointer of "acm_aud_clk0_sel" */
18835121e9dSShengjiu Wang { }, /* clk_hw pointer of "acm_aud_clk1_sel" */
189d3a0946dSShengjiu Wang };
190d3a0946dSShengjiu Wang
191d3a0946dSShengjiu Wang static struct clk_imx8_acm_sel imx8qxp_sels[] = {
192d3a0946dSShengjiu Wang { "acm_aud_clk0_sel", IMX_ADMA_ACM_AUD_CLK0_SEL, imx8qxp_aud_clk_sels, ARRAY_SIZE(imx8qxp_aud_clk_sels), 0x000000, 0, 5 },
193d3a0946dSShengjiu Wang { "acm_aud_clk1_sel", IMX_ADMA_ACM_AUD_CLK1_SEL, imx8qxp_aud_clk_sels, ARRAY_SIZE(imx8qxp_aud_clk_sels), 0x010000, 0, 5 },
194d3a0946dSShengjiu Wang { "acm_mclkout0_sel", IMX_ADMA_ACM_MCLKOUT0_SEL, imx8qxp_mclk_out_sels, ARRAY_SIZE(imx8qxp_mclk_out_sels), 0x020000, 0, 3 },
195d3a0946dSShengjiu Wang { "acm_mclkout1_sel", IMX_ADMA_ACM_MCLKOUT1_SEL, imx8qxp_mclk_out_sels, ARRAY_SIZE(imx8qxp_mclk_out_sels), 0x030000, 0, 3 },
196d3a0946dSShengjiu Wang { "acm_esai0_mclk_sel", IMX_ADMA_ACM_ESAI0_MCLK_SEL, imx8qxp_mclk_sels, ARRAY_SIZE(imx8qxp_mclk_sels), 0x060000, 0, 2 },
197d3a0946dSShengjiu Wang { "acm_sai0_mclk_sel", IMX_ADMA_ACM_SAI0_MCLK_SEL, imx8qxp_mclk_sels, ARRAY_SIZE(imx8qxp_mclk_sels), 0x0E0000, 0, 2 },
198d3a0946dSShengjiu Wang { "acm_sai1_mclk_sel", IMX_ADMA_ACM_SAI1_MCLK_SEL, imx8qxp_mclk_sels, ARRAY_SIZE(imx8qxp_mclk_sels), 0x0F0000, 0, 2 },
199d3a0946dSShengjiu Wang { "acm_sai2_mclk_sel", IMX_ADMA_ACM_SAI2_MCLK_SEL, imx8qxp_mclk_sels, ARRAY_SIZE(imx8qxp_mclk_sels), 0x100000, 0, 2 },
200d3a0946dSShengjiu Wang { "acm_sai3_mclk_sel", IMX_ADMA_ACM_SAI3_MCLK_SEL, imx8qxp_mclk_sels, ARRAY_SIZE(imx8qxp_mclk_sels), 0x110000, 0, 2 },
201d3a0946dSShengjiu Wang { "acm_sai4_mclk_sel", IMX_ADMA_ACM_SAI4_MCLK_SEL, imx8qxp_mclk_sels, ARRAY_SIZE(imx8qxp_mclk_sels), 0x140000, 0, 2 },
202d3a0946dSShengjiu Wang { "acm_sai5_mclk_sel", IMX_ADMA_ACM_SAI5_MCLK_SEL, imx8qxp_mclk_sels, ARRAY_SIZE(imx8qxp_mclk_sels), 0x150000, 0, 2 },
203d3a0946dSShengjiu Wang { "acm_spdif0_mclk_sel", IMX_ADMA_ACM_SPDIF0_TX_CLK_SEL, imx8qxp_mclk_sels, ARRAY_SIZE(imx8qxp_mclk_sels), 0x1A0000, 0, 2 },
204d3a0946dSShengjiu Wang { "acm_mqs_mclk_sel", IMX_ADMA_ACM_MQS_TX_CLK_SEL, imx8qxp_mclk_sels, ARRAY_SIZE(imx8qxp_mclk_sels), 0x1C0000, 0, 2 },
205d3a0946dSShengjiu Wang };
206d3a0946dSShengjiu Wang
207d3a0946dSShengjiu Wang static const struct clk_parent_data imx8dxl_aud_clk_sels[] = {
208d3a0946dSShengjiu Wang { .fw_name = "aud_rec_clk0_lpcg_clk" },
209d3a0946dSShengjiu Wang { .fw_name = "aud_rec_clk1_lpcg_clk" },
210d3a0946dSShengjiu Wang { .fw_name = "ext_aud_mclk0" },
211d3a0946dSShengjiu Wang { .fw_name = "ext_aud_mclk1" },
212d3a0946dSShengjiu Wang { .index = -1 },
213d3a0946dSShengjiu Wang { .index = -1 },
214d3a0946dSShengjiu Wang { .index = -1 },
215d3a0946dSShengjiu Wang { .index = -1 },
216d3a0946dSShengjiu Wang { .fw_name = "spdif0_rx" },
217d3a0946dSShengjiu Wang { .fw_name = "sai0_rx_bclk" },
218d3a0946dSShengjiu Wang { .fw_name = "sai0_tx_bclk" },
219d3a0946dSShengjiu Wang { .fw_name = "sai1_rx_bclk" },
220d3a0946dSShengjiu Wang { .fw_name = "sai1_tx_bclk" },
221d3a0946dSShengjiu Wang { .fw_name = "sai2_rx_bclk" },
222d3a0946dSShengjiu Wang { .fw_name = "sai3_rx_bclk" },
223d3a0946dSShengjiu Wang };
224d3a0946dSShengjiu Wang
225d3a0946dSShengjiu Wang static const struct clk_parent_data imx8dxl_mclk_out_sels[] = {
226d3a0946dSShengjiu Wang { .fw_name = "aud_rec_clk0_lpcg_clk" },
227d3a0946dSShengjiu Wang { .fw_name = "aud_rec_clk1_lpcg_clk" },
228d3a0946dSShengjiu Wang { .index = -1 },
229d3a0946dSShengjiu Wang { .index = -1 },
230d3a0946dSShengjiu Wang { .fw_name = "spdif0_rx" },
231d3a0946dSShengjiu Wang { .index = -1 },
232d3a0946dSShengjiu Wang { .index = -1 },
233d3a0946dSShengjiu Wang { .index = -1 },
234d3a0946dSShengjiu Wang };
235d3a0946dSShengjiu Wang
23635121e9dSShengjiu Wang static struct clk_parent_data imx8dxl_mclk_sels[] = {
237d3a0946dSShengjiu Wang { .fw_name = "aud_pll_div_clk0_lpcg_clk" },
238d3a0946dSShengjiu Wang { .fw_name = "aud_pll_div_clk1_lpcg_clk" },
23935121e9dSShengjiu Wang { }, /* clk_hw pointer of "acm_aud_clk0_sel" */
24035121e9dSShengjiu Wang { }, /* clk_hw pointer of "acm_aud_clk1_sel" */
241d3a0946dSShengjiu Wang };
242d3a0946dSShengjiu Wang
243d3a0946dSShengjiu Wang static struct clk_imx8_acm_sel imx8dxl_sels[] = {
244d3a0946dSShengjiu Wang { "acm_aud_clk0_sel", IMX_ADMA_ACM_AUD_CLK0_SEL, imx8dxl_aud_clk_sels, ARRAY_SIZE(imx8dxl_aud_clk_sels), 0x000000, 0, 5 },
245d3a0946dSShengjiu Wang { "acm_aud_clk1_sel", IMX_ADMA_ACM_AUD_CLK1_SEL, imx8dxl_aud_clk_sels, ARRAY_SIZE(imx8dxl_aud_clk_sels), 0x010000, 0, 5 },
246d3a0946dSShengjiu Wang { "acm_mclkout0_sel", IMX_ADMA_ACM_MCLKOUT0_SEL, imx8dxl_mclk_out_sels, ARRAY_SIZE(imx8dxl_mclk_out_sels), 0x020000, 0, 3 },
247d3a0946dSShengjiu Wang { "acm_mclkout1_sel", IMX_ADMA_ACM_MCLKOUT1_SEL, imx8dxl_mclk_out_sels, ARRAY_SIZE(imx8dxl_mclk_out_sels), 0x030000, 0, 3 },
248d3a0946dSShengjiu Wang { "acm_sai0_mclk_sel", IMX_ADMA_ACM_SAI0_MCLK_SEL, imx8dxl_mclk_sels, ARRAY_SIZE(imx8dxl_mclk_sels), 0x0E0000, 0, 2 },
249d3a0946dSShengjiu Wang { "acm_sai1_mclk_sel", IMX_ADMA_ACM_SAI1_MCLK_SEL, imx8dxl_mclk_sels, ARRAY_SIZE(imx8dxl_mclk_sels), 0x0F0000, 0, 2 },
250d3a0946dSShengjiu Wang { "acm_sai2_mclk_sel", IMX_ADMA_ACM_SAI2_MCLK_SEL, imx8dxl_mclk_sels, ARRAY_SIZE(imx8dxl_mclk_sels), 0x100000, 0, 2 },
251d3a0946dSShengjiu Wang { "acm_sai3_mclk_sel", IMX_ADMA_ACM_SAI3_MCLK_SEL, imx8dxl_mclk_sels, ARRAY_SIZE(imx8dxl_mclk_sels), 0x110000, 0, 2 },
252d3a0946dSShengjiu Wang { "acm_spdif0_mclk_sel", IMX_ADMA_ACM_SPDIF0_TX_CLK_SEL, imx8dxl_mclk_sels, ARRAY_SIZE(imx8dxl_mclk_sels), 0x1A0000, 0, 2 },
253d3a0946dSShengjiu Wang { "acm_mqs_mclk_sel", IMX_ADMA_ACM_MQS_TX_CLK_SEL, imx8dxl_mclk_sels, ARRAY_SIZE(imx8dxl_mclk_sels), 0x1C0000, 0, 2 },
254d3a0946dSShengjiu Wang };
255d3a0946dSShengjiu Wang
256d3a0946dSShengjiu Wang /**
257d3a0946dSShengjiu Wang * clk_imx_acm_attach_pm_domains: attach multi power domains
258d3a0946dSShengjiu Wang * @dev: device pointer
259d3a0946dSShengjiu Wang * @dev_pm: power domains for device
260d3a0946dSShengjiu Wang */
clk_imx_acm_attach_pm_domains(struct device * dev,struct clk_imx_acm_pm_domains * dev_pm)261d3a0946dSShengjiu Wang static int clk_imx_acm_attach_pm_domains(struct device *dev,
262d3a0946dSShengjiu Wang struct clk_imx_acm_pm_domains *dev_pm)
263d3a0946dSShengjiu Wang {
264d3a0946dSShengjiu Wang int ret;
265d3a0946dSShengjiu Wang int i;
266d3a0946dSShengjiu Wang
267d3a0946dSShengjiu Wang dev_pm->num_domains = of_count_phandle_with_args(dev->of_node, "power-domains",
268d3a0946dSShengjiu Wang "#power-domain-cells");
269d3a0946dSShengjiu Wang if (dev_pm->num_domains <= 1)
270d3a0946dSShengjiu Wang return 0;
271d3a0946dSShengjiu Wang
272d3a0946dSShengjiu Wang dev_pm->pd_dev = devm_kmalloc_array(dev, dev_pm->num_domains,
273d3a0946dSShengjiu Wang sizeof(*dev_pm->pd_dev),
274d3a0946dSShengjiu Wang GFP_KERNEL);
275d3a0946dSShengjiu Wang if (!dev_pm->pd_dev)
276d3a0946dSShengjiu Wang return -ENOMEM;
277d3a0946dSShengjiu Wang
278d3a0946dSShengjiu Wang dev_pm->pd_dev_link = devm_kmalloc_array(dev,
279d3a0946dSShengjiu Wang dev_pm->num_domains,
280d3a0946dSShengjiu Wang sizeof(*dev_pm->pd_dev_link),
281d3a0946dSShengjiu Wang GFP_KERNEL);
282d3a0946dSShengjiu Wang if (!dev_pm->pd_dev_link)
283d3a0946dSShengjiu Wang return -ENOMEM;
284d3a0946dSShengjiu Wang
285d3a0946dSShengjiu Wang for (i = 0; i < dev_pm->num_domains; i++) {
286d3a0946dSShengjiu Wang dev_pm->pd_dev[i] = dev_pm_domain_attach_by_id(dev, i);
287156624e2SChristophe JAILLET if (IS_ERR(dev_pm->pd_dev[i])) {
288156624e2SChristophe JAILLET ret = PTR_ERR(dev_pm->pd_dev[i]);
289156624e2SChristophe JAILLET goto detach_pm;
290156624e2SChristophe JAILLET }
291d3a0946dSShengjiu Wang
292d3a0946dSShengjiu Wang dev_pm->pd_dev_link[i] = device_link_add(dev,
293d3a0946dSShengjiu Wang dev_pm->pd_dev[i],
294d3a0946dSShengjiu Wang DL_FLAG_STATELESS |
295d3a0946dSShengjiu Wang DL_FLAG_PM_RUNTIME |
296d3a0946dSShengjiu Wang DL_FLAG_RPM_ACTIVE);
297d3a0946dSShengjiu Wang if (IS_ERR(dev_pm->pd_dev_link[i])) {
298d3a0946dSShengjiu Wang dev_pm_domain_detach(dev_pm->pd_dev[i], false);
299d3a0946dSShengjiu Wang ret = PTR_ERR(dev_pm->pd_dev_link[i]);
300d3a0946dSShengjiu Wang goto detach_pm;
301d3a0946dSShengjiu Wang }
302d3a0946dSShengjiu Wang }
303d3a0946dSShengjiu Wang return 0;
304d3a0946dSShengjiu Wang
305d3a0946dSShengjiu Wang detach_pm:
306d3a0946dSShengjiu Wang while (--i >= 0) {
307d3a0946dSShengjiu Wang device_link_del(dev_pm->pd_dev_link[i]);
308d3a0946dSShengjiu Wang dev_pm_domain_detach(dev_pm->pd_dev[i], false);
309d3a0946dSShengjiu Wang }
310d3a0946dSShengjiu Wang return ret;
311d3a0946dSShengjiu Wang }
312d3a0946dSShengjiu Wang
313d3a0946dSShengjiu Wang /**
314d3a0946dSShengjiu Wang * clk_imx_acm_detach_pm_domains: detach multi power domains
315d3a0946dSShengjiu Wang * @dev: deivice pointer
316d3a0946dSShengjiu Wang * @dev_pm: multi power domain for device
317d3a0946dSShengjiu Wang */
clk_imx_acm_detach_pm_domains(struct device * dev,struct clk_imx_acm_pm_domains * dev_pm)3183af29a89SChristophe JAILLET static void clk_imx_acm_detach_pm_domains(struct device *dev,
319d3a0946dSShengjiu Wang struct clk_imx_acm_pm_domains *dev_pm)
320d3a0946dSShengjiu Wang {
321d3a0946dSShengjiu Wang int i;
322d3a0946dSShengjiu Wang
323d3a0946dSShengjiu Wang if (dev_pm->num_domains <= 1)
3243af29a89SChristophe JAILLET return;
325d3a0946dSShengjiu Wang
326d3a0946dSShengjiu Wang for (i = 0; i < dev_pm->num_domains; i++) {
327d3a0946dSShengjiu Wang device_link_del(dev_pm->pd_dev_link[i]);
328d3a0946dSShengjiu Wang dev_pm_domain_detach(dev_pm->pd_dev[i], false);
329d3a0946dSShengjiu Wang }
330d3a0946dSShengjiu Wang }
331d3a0946dSShengjiu Wang
imx8_acm_clk_probe(struct platform_device * pdev)332d3a0946dSShengjiu Wang static int imx8_acm_clk_probe(struct platform_device *pdev)
333d3a0946dSShengjiu Wang {
334d3a0946dSShengjiu Wang struct clk_hw_onecell_data *clk_hw_data;
335d3a0946dSShengjiu Wang struct device *dev = &pdev->dev;
336d3a0946dSShengjiu Wang struct clk_imx8_acm_sel *sels;
337d3a0946dSShengjiu Wang struct imx8_acm_priv *priv;
338d3a0946dSShengjiu Wang struct clk_hw **hws;
339d3a0946dSShengjiu Wang void __iomem *base;
340d3a0946dSShengjiu Wang int ret;
341d3a0946dSShengjiu Wang int i;
342d3a0946dSShengjiu Wang
343d3a0946dSShengjiu Wang base = devm_of_iomap(dev, dev->of_node, 0, NULL);
344d3a0946dSShengjiu Wang if (WARN_ON(IS_ERR(base)))
345d3a0946dSShengjiu Wang return PTR_ERR(base);
346d3a0946dSShengjiu Wang
347d3a0946dSShengjiu Wang priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
348d3a0946dSShengjiu Wang if (!priv)
349d3a0946dSShengjiu Wang return -ENOMEM;
350d3a0946dSShengjiu Wang
351d3a0946dSShengjiu Wang priv->reg = base;
352d3a0946dSShengjiu Wang priv->soc_data = of_device_get_match_data(dev);
353d3a0946dSShengjiu Wang platform_set_drvdata(pdev, priv);
354d3a0946dSShengjiu Wang
355d3a0946dSShengjiu Wang clk_hw_data = devm_kzalloc(&pdev->dev, struct_size(clk_hw_data, hws, IMX_ADMA_ACM_CLK_END),
356d3a0946dSShengjiu Wang GFP_KERNEL);
357d3a0946dSShengjiu Wang if (!clk_hw_data)
358d3a0946dSShengjiu Wang return -ENOMEM;
359d3a0946dSShengjiu Wang
360d3a0946dSShengjiu Wang clk_hw_data->num = IMX_ADMA_ACM_CLK_END;
361d3a0946dSShengjiu Wang hws = clk_hw_data->hws;
362d3a0946dSShengjiu Wang
363d3a0946dSShengjiu Wang ret = clk_imx_acm_attach_pm_domains(&pdev->dev, &priv->dev_pm);
364d3a0946dSShengjiu Wang if (ret)
365d3a0946dSShengjiu Wang return ret;
366d3a0946dSShengjiu Wang
367d3a0946dSShengjiu Wang pm_runtime_enable(&pdev->dev);
368d3a0946dSShengjiu Wang pm_runtime_get_sync(&pdev->dev);
369d3a0946dSShengjiu Wang
370d3a0946dSShengjiu Wang sels = priv->soc_data->sels;
371d3a0946dSShengjiu Wang for (i = 0; i < priv->soc_data->num_sels; i++) {
372d3a0946dSShengjiu Wang hws[sels[i].clkid] = devm_clk_hw_register_mux_parent_data_table(dev,
373d3a0946dSShengjiu Wang sels[i].name, sels[i].parents,
374d3a0946dSShengjiu Wang sels[i].num_parents, 0,
375d3a0946dSShengjiu Wang base + sels[i].reg,
376d3a0946dSShengjiu Wang sels[i].shift, sels[i].width,
377d3a0946dSShengjiu Wang 0, NULL, NULL);
378d3a0946dSShengjiu Wang if (IS_ERR(hws[sels[i].clkid])) {
3799a0108acSChristophe JAILLET ret = PTR_ERR(hws[sels[i].clkid]);
380ef23d44bSChristophe JAILLET imx_check_clk_hws(hws, IMX_ADMA_ACM_CLK_END);
381d3a0946dSShengjiu Wang goto err_clk_register;
382d3a0946dSShengjiu Wang }
38335121e9dSShengjiu Wang
38435121e9dSShengjiu Wang /*
38535121e9dSShengjiu Wang * The IMX_ADMA_ACM_AUD_CLK0_SEL and IMX_ADMA_ACM_AUD_CLK1_SEL are
38635121e9dSShengjiu Wang * registered first. After registration, update the clk_hw pointer
38735121e9dSShengjiu Wang * to imx8qm/qxp/dxl_mclk_sels structures.
38835121e9dSShengjiu Wang */
38935121e9dSShengjiu Wang if (sels[i].clkid == IMX_ADMA_ACM_AUD_CLK0_SEL)
39035121e9dSShengjiu Wang priv->soc_data->mclk_sels[ACM_AUD_CLK0_SEL_INDEX].hw =
39135121e9dSShengjiu Wang hws[IMX_ADMA_ACM_AUD_CLK0_SEL];
39235121e9dSShengjiu Wang if (sels[i].clkid == IMX_ADMA_ACM_AUD_CLK1_SEL)
39335121e9dSShengjiu Wang priv->soc_data->mclk_sels[ACM_AUD_CLK1_SEL_INDEX].hw =
39435121e9dSShengjiu Wang hws[IMX_ADMA_ACM_AUD_CLK1_SEL];
395d3a0946dSShengjiu Wang }
396d3a0946dSShengjiu Wang
397d3a0946dSShengjiu Wang ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_hw_data);
398d3a0946dSShengjiu Wang if (ret < 0) {
399d3a0946dSShengjiu Wang dev_err(dev, "failed to register hws for ACM\n");
400e9a164e3SChristophe JAILLET goto err_clk_register;
401d3a0946dSShengjiu Wang }
402d3a0946dSShengjiu Wang
403d3a0946dSShengjiu Wang pm_runtime_put_sync(&pdev->dev);
404e9a164e3SChristophe JAILLET return 0;
405e9a164e3SChristophe JAILLET
406e9a164e3SChristophe JAILLET err_clk_register:
407e9a164e3SChristophe JAILLET pm_runtime_put_sync(&pdev->dev);
408e9a164e3SChristophe JAILLET pm_runtime_disable(&pdev->dev);
409e9a164e3SChristophe JAILLET clk_imx_acm_detach_pm_domains(&pdev->dev, &priv->dev_pm);
410d3a0946dSShengjiu Wang
411d3a0946dSShengjiu Wang return ret;
412d3a0946dSShengjiu Wang }
413d3a0946dSShengjiu Wang
imx8_acm_clk_remove(struct platform_device * pdev)4146995c4f5SUwe Kleine-König static void imx8_acm_clk_remove(struct platform_device *pdev)
415d3a0946dSShengjiu Wang {
416d3a0946dSShengjiu Wang struct imx8_acm_priv *priv = dev_get_drvdata(&pdev->dev);
417d3a0946dSShengjiu Wang
418d3a0946dSShengjiu Wang pm_runtime_disable(&pdev->dev);
419d3a0946dSShengjiu Wang
420d3a0946dSShengjiu Wang clk_imx_acm_detach_pm_domains(&pdev->dev, &priv->dev_pm);
421d3a0946dSShengjiu Wang }
422d3a0946dSShengjiu Wang
423d3a0946dSShengjiu Wang static const struct imx8_acm_soc_data imx8qm_acm_data = {
424d3a0946dSShengjiu Wang .sels = imx8qm_sels,
425d3a0946dSShengjiu Wang .num_sels = ARRAY_SIZE(imx8qm_sels),
42635121e9dSShengjiu Wang .mclk_sels = imx8qm_mclk_sels,
427d3a0946dSShengjiu Wang };
428d3a0946dSShengjiu Wang
429d3a0946dSShengjiu Wang static const struct imx8_acm_soc_data imx8qxp_acm_data = {
430d3a0946dSShengjiu Wang .sels = imx8qxp_sels,
431d3a0946dSShengjiu Wang .num_sels = ARRAY_SIZE(imx8qxp_sels),
43235121e9dSShengjiu Wang .mclk_sels = imx8qxp_mclk_sels,
433d3a0946dSShengjiu Wang };
434d3a0946dSShengjiu Wang
435d3a0946dSShengjiu Wang static const struct imx8_acm_soc_data imx8dxl_acm_data = {
436d3a0946dSShengjiu Wang .sels = imx8dxl_sels,
437d3a0946dSShengjiu Wang .num_sels = ARRAY_SIZE(imx8dxl_sels),
43835121e9dSShengjiu Wang .mclk_sels = imx8dxl_mclk_sels,
439d3a0946dSShengjiu Wang };
440d3a0946dSShengjiu Wang
441d3a0946dSShengjiu Wang static const struct of_device_id imx8_acm_match[] = {
442d3a0946dSShengjiu Wang { .compatible = "fsl,imx8qm-acm", .data = &imx8qm_acm_data },
443d3a0946dSShengjiu Wang { .compatible = "fsl,imx8qxp-acm", .data = &imx8qxp_acm_data },
444d3a0946dSShengjiu Wang { .compatible = "fsl,imx8dxl-acm", .data = &imx8dxl_acm_data },
445d3a0946dSShengjiu Wang { /* sentinel */ }
446d3a0946dSShengjiu Wang };
447d3a0946dSShengjiu Wang MODULE_DEVICE_TABLE(of, imx8_acm_match);
448d3a0946dSShengjiu Wang
imx8_acm_runtime_suspend(struct device * dev)449d3a0946dSShengjiu Wang static int __maybe_unused imx8_acm_runtime_suspend(struct device *dev)
450d3a0946dSShengjiu Wang {
451d3a0946dSShengjiu Wang struct imx8_acm_priv *priv = dev_get_drvdata(dev);
452d3a0946dSShengjiu Wang struct clk_imx8_acm_sel *sels;
453d3a0946dSShengjiu Wang int i;
454d3a0946dSShengjiu Wang
455d3a0946dSShengjiu Wang sels = priv->soc_data->sels;
456d3a0946dSShengjiu Wang
457d3a0946dSShengjiu Wang for (i = 0; i < priv->soc_data->num_sels; i++)
458d3a0946dSShengjiu Wang priv->regs[i] = readl_relaxed(priv->reg + sels[i].reg);
459d3a0946dSShengjiu Wang
460d3a0946dSShengjiu Wang return 0;
461d3a0946dSShengjiu Wang }
462d3a0946dSShengjiu Wang
imx8_acm_runtime_resume(struct device * dev)463d3a0946dSShengjiu Wang static int __maybe_unused imx8_acm_runtime_resume(struct device *dev)
464d3a0946dSShengjiu Wang {
465d3a0946dSShengjiu Wang struct imx8_acm_priv *priv = dev_get_drvdata(dev);
466d3a0946dSShengjiu Wang struct clk_imx8_acm_sel *sels;
467d3a0946dSShengjiu Wang int i;
468d3a0946dSShengjiu Wang
469d3a0946dSShengjiu Wang sels = priv->soc_data->sels;
470d3a0946dSShengjiu Wang
471d3a0946dSShengjiu Wang for (i = 0; i < priv->soc_data->num_sels; i++)
472d3a0946dSShengjiu Wang writel_relaxed(priv->regs[i], priv->reg + sels[i].reg);
473d3a0946dSShengjiu Wang
474d3a0946dSShengjiu Wang return 0;
475d3a0946dSShengjiu Wang }
476d3a0946dSShengjiu Wang
477d3a0946dSShengjiu Wang static const struct dev_pm_ops imx8_acm_pm_ops = {
478d3a0946dSShengjiu Wang SET_RUNTIME_PM_OPS(imx8_acm_runtime_suspend,
479d3a0946dSShengjiu Wang imx8_acm_runtime_resume, NULL)
480d3a0946dSShengjiu Wang SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
481d3a0946dSShengjiu Wang pm_runtime_force_resume)
482d3a0946dSShengjiu Wang };
483d3a0946dSShengjiu Wang
484d3a0946dSShengjiu Wang static struct platform_driver imx8_acm_clk_driver = {
485d3a0946dSShengjiu Wang .driver = {
486d3a0946dSShengjiu Wang .name = "imx8-acm",
487d3a0946dSShengjiu Wang .of_match_table = imx8_acm_match,
488d3a0946dSShengjiu Wang .pm = &imx8_acm_pm_ops,
489d3a0946dSShengjiu Wang },
490d3a0946dSShengjiu Wang .probe = imx8_acm_clk_probe,
491*f00b45dbSUwe Kleine-König .remove = imx8_acm_clk_remove,
492d3a0946dSShengjiu Wang };
493d3a0946dSShengjiu Wang module_platform_driver(imx8_acm_clk_driver);
494d3a0946dSShengjiu Wang
495d3a0946dSShengjiu Wang MODULE_AUTHOR("Shengjiu Wang <shengjiu.wang@nxp.com>");
496d3a0946dSShengjiu Wang MODULE_DESCRIPTION("Freescale i.MX8 Audio Clock Mux driver");
497d3a0946dSShengjiu Wang MODULE_LICENSE("GPL");
498