1 // SPDX-License-Identifier: GPL-2.0+
6 #include <dt-bindings/clock/imx8-clock.h>
7 #include <linux/clk-provider.h>
8 #include <linux/device.h>
11 #include <linux/module.h>
13 #include <linux/of_device.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_domain.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/slab.h>
22 * struct clk_imx_acm_pm_domains - structure for multi power domain
23 * @pd_dev: power domain device
24 * @pd_dev_link: power domain device link
25 * @num_domains: power domain nummber
27 struct clk_imx_acm_pm_domains {
28 struct device **pd_dev;
29 struct device_link **pd_dev_link;
34 * struct clk_imx8_acm_sel - for clock mux
37 * @parents: clock parents
38 * @num_parents: clock parents number
39 * @reg: register offset
40 * @shift: bit shift in register
43 struct clk_imx8_acm_sel {
46 const struct clk_parent_data *parents; /* For mux */
54 * struct imx8_acm_soc_data - soc specific data
55 * @sels: pointer to struct clk_imx8_acm_sel
56 * @num_sels: numbers of items
58 struct imx8_acm_soc_data {
59 struct clk_imx8_acm_sel *sels;
60 unsigned int num_sels;
64 * struct imx8_acm_priv - private structure
65 * @dev_pm: multi power domain
66 * @soc_data: pointer to soc data
67 * @reg: base address of registers
68 * @regs: save registers for suspend
70 struct imx8_acm_priv {
71 struct clk_imx_acm_pm_domains dev_pm;
72 const struct imx8_acm_soc_data *soc_data;
74 u32 regs[IMX_ADMA_ACM_CLK_END];
77 static const struct clk_parent_data imx8qm_aud_clk_sels[] = {
78 { .fw_name = "aud_rec_clk0_lpcg_clk" },
79 { .fw_name = "aud_rec_clk1_lpcg_clk" },
80 { .fw_name = "mlb_clk" },
81 { .fw_name = "hdmi_rx_mclk" },
82 { .fw_name = "ext_aud_mclk0" },
83 { .fw_name = "ext_aud_mclk1" },
84 { .fw_name = "esai0_rx_clk" },
85 { .fw_name = "esai0_rx_hf_clk" },
86 { .fw_name = "esai0_tx_clk" },
87 { .fw_name = "esai0_tx_hf_clk" },
88 { .fw_name = "esai1_rx_clk" },
89 { .fw_name = "esai1_rx_hf_clk" },
90 { .fw_name = "esai1_tx_clk" },
91 { .fw_name = "esai1_tx_hf_clk" },
92 { .fw_name = "spdif0_rx" },
93 { .fw_name = "spdif1_rx" },
94 { .fw_name = "sai0_rx_bclk" },
95 { .fw_name = "sai0_tx_bclk" },
96 { .fw_name = "sai1_rx_bclk" },
97 { .fw_name = "sai1_tx_bclk" },
98 { .fw_name = "sai2_rx_bclk" },
99 { .fw_name = "sai3_rx_bclk" },
100 { .fw_name = "sai4_rx_bclk" },
103 static const struct clk_parent_data imx8qm_mclk_out_sels[] = {
104 { .fw_name = "aud_rec_clk0_lpcg_clk" },
105 { .fw_name = "aud_rec_clk1_lpcg_clk" },
106 { .fw_name = "mlb_clk" },
107 { .fw_name = "hdmi_rx_mclk" },
108 { .fw_name = "spdif0_rx" },
109 { .fw_name = "spdif1_rx" },
110 { .fw_name = "sai4_rx_bclk" },
111 { .fw_name = "sai6_rx_bclk" },
114 static const struct clk_parent_data imx8qm_mclk_sels[] = {
115 { .fw_name = "aud_pll_div_clk0_lpcg_clk" },
116 { .fw_name = "aud_pll_div_clk1_lpcg_clk" },
117 { .fw_name = "acm_aud_clk0_sel" },
118 { .fw_name = "acm_aud_clk1_sel" },
121 static const struct clk_parent_data imx8qm_asrc_mux_clk_sels[] = {
122 { .fw_name = "sai4_rx_bclk" },
123 { .fw_name = "sai5_tx_bclk" },
125 { .fw_name = "mlb_clk" },
129 static struct clk_imx8_acm_sel imx8qm_sels[] = {
130 { "acm_aud_clk0_sel", IMX_ADMA_ACM_AUD_CLK0_SEL, imx8qm_aud_clk_sels, ARRAY_SIZE(imx8qm_aud_clk_sels), 0x000000, 0, 5 },
131 { "acm_aud_clk1_sel", IMX_ADMA_ACM_AUD_CLK1_SEL, imx8qm_aud_clk_sels, ARRAY_SIZE(imx8qm_aud_clk_sels), 0x010000, 0, 5 },
132 { "acm_mclkout0_sel", IMX_ADMA_ACM_MCLKOUT0_SEL, imx8qm_mclk_out_sels, ARRAY_SIZE(imx8qm_mclk_out_sels), 0x020000, 0, 3 },
133 { "acm_mclkout1_sel", IMX_ADMA_ACM_MCLKOUT1_SEL, imx8qm_mclk_out_sels, ARRAY_SIZE(imx8qm_mclk_out_sels), 0x030000, 0, 3 },
134 { "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 },
135 { "acm_esai0_mclk_sel", IMX_ADMA_ACM_ESAI0_MCLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x060000, 0, 2 },
136 { "acm_esai1_mclk_sel", IMX_ADMA_ACM_ESAI1_MCLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x070000, 0, 2 },
137 { "acm_sai0_mclk_sel", IMX_ADMA_ACM_SAI0_MCLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x0E0000, 0, 2 },
138 { "acm_sai1_mclk_sel", IMX_ADMA_ACM_SAI1_MCLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x0F0000, 0, 2 },
139 { "acm_sai2_mclk_sel", IMX_ADMA_ACM_SAI2_MCLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x100000, 0, 2 },
140 { "acm_sai3_mclk_sel", IMX_ADMA_ACM_SAI3_MCLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x110000, 0, 2 },
141 { "acm_sai4_mclk_sel", IMX_ADMA_ACM_SAI4_MCLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x120000, 0, 2 },
142 { "acm_sai5_mclk_sel", IMX_ADMA_ACM_SAI5_MCLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x130000, 0, 2 },
143 { "acm_sai6_mclk_sel", IMX_ADMA_ACM_SAI6_MCLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x140000, 0, 2 },
144 { "acm_sai7_mclk_sel", IMX_ADMA_ACM_SAI7_MCLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x150000, 0, 2 },
145 { "acm_spdif0_mclk_sel", IMX_ADMA_ACM_SPDIF0_TX_CLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x1A0000, 0, 2 },
146 { "acm_spdif1_mclk_sel", IMX_ADMA_ACM_SPDIF1_TX_CLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x1B0000, 0, 2 },
147 { "acm_mqs_mclk_sel", IMX_ADMA_ACM_MQS_TX_CLK_SEL, imx8qm_mclk_sels, ARRAY_SIZE(imx8qm_mclk_sels), 0x1C0000, 0, 2 },
150 static const struct clk_parent_data imx8qxp_aud_clk_sels[] = {
151 { .fw_name = "aud_rec_clk0_lpcg_clk" },
152 { .fw_name = "aud_rec_clk1_lpcg_clk" },
153 { .fw_name = "ext_aud_mclk0" },
154 { .fw_name = "ext_aud_mclk1" },
155 { .fw_name = "esai0_rx_clk" },
156 { .fw_name = "esai0_rx_hf_clk" },
157 { .fw_name = "esai0_tx_clk" },
158 { .fw_name = "esai0_tx_hf_clk" },
159 { .fw_name = "spdif0_rx" },
160 { .fw_name = "sai0_rx_bclk" },
161 { .fw_name = "sai0_tx_bclk" },
162 { .fw_name = "sai1_rx_bclk" },
163 { .fw_name = "sai1_tx_bclk" },
164 { .fw_name = "sai2_rx_bclk" },
165 { .fw_name = "sai3_rx_bclk" },
168 static const struct clk_parent_data imx8qxp_mclk_out_sels[] = {
169 { .fw_name = "aud_rec_clk0_lpcg_clk" },
170 { .fw_name = "aud_rec_clk1_lpcg_clk" },
173 { .fw_name = "spdif0_rx" },
176 { .fw_name = "sai4_rx_bclk" },
179 static const struct clk_parent_data imx8qxp_mclk_sels[] = {
180 { .fw_name = "aud_pll_div_clk0_lpcg_clk" },
181 { .fw_name = "aud_pll_div_clk1_lpcg_clk" },
182 { .fw_name = "acm_aud_clk0_sel" },
183 { .fw_name = "acm_aud_clk1_sel" },
186 static struct clk_imx8_acm_sel imx8qxp_sels[] = {
187 { "acm_aud_clk0_sel", IMX_ADMA_ACM_AUD_CLK0_SEL, imx8qxp_aud_clk_sels, ARRAY_SIZE(imx8qxp_aud_clk_sels), 0x000000, 0, 5 },
188 { "acm_aud_clk1_sel", IMX_ADMA_ACM_AUD_CLK1_SEL, imx8qxp_aud_clk_sels, ARRAY_SIZE(imx8qxp_aud_clk_sels), 0x010000, 0, 5 },
189 { "acm_mclkout0_sel", IMX_ADMA_ACM_MCLKOUT0_SEL, imx8qxp_mclk_out_sels, ARRAY_SIZE(imx8qxp_mclk_out_sels), 0x020000, 0, 3 },
190 { "acm_mclkout1_sel", IMX_ADMA_ACM_MCLKOUT1_SEL, imx8qxp_mclk_out_sels, ARRAY_SIZE(imx8qxp_mclk_out_sels), 0x030000, 0, 3 },
191 { "acm_esai0_mclk_sel", IMX_ADMA_ACM_ESAI0_MCLK_SEL, imx8qxp_mclk_sels, ARRAY_SIZE(imx8qxp_mclk_sels), 0x060000, 0, 2 },
192 { "acm_sai0_mclk_sel", IMX_ADMA_ACM_SAI0_MCLK_SEL, imx8qxp_mclk_sels, ARRAY_SIZE(imx8qxp_mclk_sels), 0x0E0000, 0, 2 },
193 { "acm_sai1_mclk_sel", IMX_ADMA_ACM_SAI1_MCLK_SEL, imx8qxp_mclk_sels, ARRAY_SIZE(imx8qxp_mclk_sels), 0x0F0000, 0, 2 },
194 { "acm_sai2_mclk_sel", IMX_ADMA_ACM_SAI2_MCLK_SEL, imx8qxp_mclk_sels, ARRAY_SIZE(imx8qxp_mclk_sels), 0x100000, 0, 2 },
195 { "acm_sai3_mclk_sel", IMX_ADMA_ACM_SAI3_MCLK_SEL, imx8qxp_mclk_sels, ARRAY_SIZE(imx8qxp_mclk_sels), 0x110000, 0, 2 },
196 { "acm_sai4_mclk_sel", IMX_ADMA_ACM_SAI4_MCLK_SEL, imx8qxp_mclk_sels, ARRAY_SIZE(imx8qxp_mclk_sels), 0x140000, 0, 2 },
197 { "acm_sai5_mclk_sel", IMX_ADMA_ACM_SAI5_MCLK_SEL, imx8qxp_mclk_sels, ARRAY_SIZE(imx8qxp_mclk_sels), 0x150000, 0, 2 },
198 { "acm_spdif0_mclk_sel", IMX_ADMA_ACM_SPDIF0_TX_CLK_SEL, imx8qxp_mclk_sels, ARRAY_SIZE(imx8qxp_mclk_sels), 0x1A0000, 0, 2 },
199 { "acm_mqs_mclk_sel", IMX_ADMA_ACM_MQS_TX_CLK_SEL, imx8qxp_mclk_sels, ARRAY_SIZE(imx8qxp_mclk_sels), 0x1C0000, 0, 2 },
202 static const struct clk_parent_data imx8dxl_aud_clk_sels[] = {
203 { .fw_name = "aud_rec_clk0_lpcg_clk" },
204 { .fw_name = "aud_rec_clk1_lpcg_clk" },
205 { .fw_name = "ext_aud_mclk0" },
206 { .fw_name = "ext_aud_mclk1" },
211 { .fw_name = "spdif0_rx" },
212 { .fw_name = "sai0_rx_bclk" },
213 { .fw_name = "sai0_tx_bclk" },
214 { .fw_name = "sai1_rx_bclk" },
215 { .fw_name = "sai1_tx_bclk" },
216 { .fw_name = "sai2_rx_bclk" },
217 { .fw_name = "sai3_rx_bclk" },
220 static const struct clk_parent_data imx8dxl_mclk_out_sels[] = {
221 { .fw_name = "aud_rec_clk0_lpcg_clk" },
222 { .fw_name = "aud_rec_clk1_lpcg_clk" },
225 { .fw_name = "spdif0_rx" },
231 static const struct clk_parent_data imx8dxl_mclk_sels[] = {
232 { .fw_name = "aud_pll_div_clk0_lpcg_clk" },
233 { .fw_name = "aud_pll_div_clk1_lpcg_clk" },
234 { .fw_name = "acm_aud_clk0_sel" },
235 { .fw_name = "acm_aud_clk1_sel" },
238 static struct clk_imx8_acm_sel imx8dxl_sels[] = {
239 { "acm_aud_clk0_sel", IMX_ADMA_ACM_AUD_CLK0_SEL, imx8dxl_aud_clk_sels, ARRAY_SIZE(imx8dxl_aud_clk_sels), 0x000000, 0, 5 },
240 { "acm_aud_clk1_sel", IMX_ADMA_ACM_AUD_CLK1_SEL, imx8dxl_aud_clk_sels, ARRAY_SIZE(imx8dxl_aud_clk_sels), 0x010000, 0, 5 },
241 { "acm_mclkout0_sel", IMX_ADMA_ACM_MCLKOUT0_SEL, imx8dxl_mclk_out_sels, ARRAY_SIZE(imx8dxl_mclk_out_sels), 0x020000, 0, 3 },
242 { "acm_mclkout1_sel", IMX_ADMA_ACM_MCLKOUT1_SEL, imx8dxl_mclk_out_sels, ARRAY_SIZE(imx8dxl_mclk_out_sels), 0x030000, 0, 3 },
243 { "acm_sai0_mclk_sel", IMX_ADMA_ACM_SAI0_MCLK_SEL, imx8dxl_mclk_sels, ARRAY_SIZE(imx8dxl_mclk_sels), 0x0E0000, 0, 2 },
244 { "acm_sai1_mclk_sel", IMX_ADMA_ACM_SAI1_MCLK_SEL, imx8dxl_mclk_sels, ARRAY_SIZE(imx8dxl_mclk_sels), 0x0F0000, 0, 2 },
245 { "acm_sai2_mclk_sel", IMX_ADMA_ACM_SAI2_MCLK_SEL, imx8dxl_mclk_sels, ARRAY_SIZE(imx8dxl_mclk_sels), 0x100000, 0, 2 },
246 { "acm_sai3_mclk_sel", IMX_ADMA_ACM_SAI3_MCLK_SEL, imx8dxl_mclk_sels, ARRAY_SIZE(imx8dxl_mclk_sels), 0x110000, 0, 2 },
247 { "acm_spdif0_mclk_sel", IMX_ADMA_ACM_SPDIF0_TX_CLK_SEL, imx8dxl_mclk_sels, ARRAY_SIZE(imx8dxl_mclk_sels), 0x1A0000, 0, 2 },
248 { "acm_mqs_mclk_sel", IMX_ADMA_ACM_MQS_TX_CLK_SEL, imx8dxl_mclk_sels, ARRAY_SIZE(imx8dxl_mclk_sels), 0x1C0000, 0, 2 },
252 * clk_imx_acm_attach_pm_domains: attach multi power domains
253 * @dev: device pointer
254 * @dev_pm: power domains for device
256 static int clk_imx_acm_attach_pm_domains(struct device *dev,
257 struct clk_imx_acm_pm_domains *dev_pm)
262 dev_pm->num_domains = of_count_phandle_with_args(dev->of_node, "power-domains",
263 "#power-domain-cells");
264 if (dev_pm->num_domains <= 1)
267 dev_pm->pd_dev = devm_kmalloc_array(dev, dev_pm->num_domains,
268 sizeof(*dev_pm->pd_dev),
273 dev_pm->pd_dev_link = devm_kmalloc_array(dev,
275 sizeof(*dev_pm->pd_dev_link),
277 if (!dev_pm->pd_dev_link)
280 for (i = 0; i < dev_pm->num_domains; i++) {
281 dev_pm->pd_dev[i] = dev_pm_domain_attach_by_id(dev, i);
282 if (IS_ERR(dev_pm->pd_dev[i]))
283 return PTR_ERR(dev_pm->pd_dev[i]);
285 dev_pm->pd_dev_link[i] = device_link_add(dev,
290 if (IS_ERR(dev_pm->pd_dev_link[i])) {
291 dev_pm_domain_detach(dev_pm->pd_dev[i], false);
292 ret = PTR_ERR(dev_pm->pd_dev_link[i]);
300 device_link_del(dev_pm->pd_dev_link[i]);
301 dev_pm_domain_detach(dev_pm->pd_dev[i], false);
307 * clk_imx_acm_detach_pm_domains: detach multi power domains
308 * @dev: deivice pointer
309 * @dev_pm: multi power domain for device
311 static int clk_imx_acm_detach_pm_domains(struct device *dev,
312 struct clk_imx_acm_pm_domains *dev_pm)
316 if (dev_pm->num_domains <= 1)
319 for (i = 0; i < dev_pm->num_domains; i++) {
320 device_link_del(dev_pm->pd_dev_link[i]);
321 dev_pm_domain_detach(dev_pm->pd_dev[i], false);
327 static int imx8_acm_clk_probe(struct platform_device *pdev)
329 struct clk_hw_onecell_data *clk_hw_data;
330 struct device *dev = &pdev->dev;
331 struct clk_imx8_acm_sel *sels;
332 struct imx8_acm_priv *priv;
338 base = devm_of_iomap(dev, dev->of_node, 0, NULL);
339 if (WARN_ON(IS_ERR(base)))
340 return PTR_ERR(base);
342 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
347 priv->soc_data = of_device_get_match_data(dev);
348 platform_set_drvdata(pdev, priv);
350 clk_hw_data = devm_kzalloc(&pdev->dev, struct_size(clk_hw_data, hws, IMX_ADMA_ACM_CLK_END),
355 clk_hw_data->num = IMX_ADMA_ACM_CLK_END;
356 hws = clk_hw_data->hws;
358 ret = clk_imx_acm_attach_pm_domains(&pdev->dev, &priv->dev_pm);
362 pm_runtime_enable(&pdev->dev);
363 pm_runtime_get_sync(&pdev->dev);
365 sels = priv->soc_data->sels;
366 for (i = 0; i < priv->soc_data->num_sels; i++) {
367 hws[sels[i].clkid] = devm_clk_hw_register_mux_parent_data_table(dev,
368 sels[i].name, sels[i].parents,
369 sels[i].num_parents, 0,
371 sels[i].shift, sels[i].width,
373 if (IS_ERR(hws[sels[i].clkid])) {
374 pm_runtime_disable(&pdev->dev);
375 goto err_clk_register;
379 imx_check_clk_hws(hws, IMX_ADMA_ACM_CLK_END);
381 ret = devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, clk_hw_data);
383 dev_err(dev, "failed to register hws for ACM\n");
384 pm_runtime_disable(&pdev->dev);
389 pm_runtime_put_sync(&pdev->dev);
394 static int imx8_acm_clk_remove(struct platform_device *pdev)
396 struct imx8_acm_priv *priv = dev_get_drvdata(&pdev->dev);
398 pm_runtime_disable(&pdev->dev);
400 clk_imx_acm_detach_pm_domains(&pdev->dev, &priv->dev_pm);
405 static const struct imx8_acm_soc_data imx8qm_acm_data = {
407 .num_sels = ARRAY_SIZE(imx8qm_sels),
410 static const struct imx8_acm_soc_data imx8qxp_acm_data = {
411 .sels = imx8qxp_sels,
412 .num_sels = ARRAY_SIZE(imx8qxp_sels),
415 static const struct imx8_acm_soc_data imx8dxl_acm_data = {
416 .sels = imx8dxl_sels,
417 .num_sels = ARRAY_SIZE(imx8dxl_sels),
420 static const struct of_device_id imx8_acm_match[] = {
421 { .compatible = "fsl,imx8qm-acm", .data = &imx8qm_acm_data },
422 { .compatible = "fsl,imx8qxp-acm", .data = &imx8qxp_acm_data },
423 { .compatible = "fsl,imx8dxl-acm", .data = &imx8dxl_acm_data },
426 MODULE_DEVICE_TABLE(of, imx8_acm_match);
428 static int __maybe_unused imx8_acm_runtime_suspend(struct device *dev)
430 struct imx8_acm_priv *priv = dev_get_drvdata(dev);
431 struct clk_imx8_acm_sel *sels;
434 sels = priv->soc_data->sels;
436 for (i = 0; i < priv->soc_data->num_sels; i++)
437 priv->regs[i] = readl_relaxed(priv->reg + sels[i].reg);
442 static int __maybe_unused imx8_acm_runtime_resume(struct device *dev)
444 struct imx8_acm_priv *priv = dev_get_drvdata(dev);
445 struct clk_imx8_acm_sel *sels;
448 sels = priv->soc_data->sels;
450 for (i = 0; i < priv->soc_data->num_sels; i++)
451 writel_relaxed(priv->regs[i], priv->reg + sels[i].reg);
456 static const struct dev_pm_ops imx8_acm_pm_ops = {
457 SET_RUNTIME_PM_OPS(imx8_acm_runtime_suspend,
458 imx8_acm_runtime_resume, NULL)
459 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
460 pm_runtime_force_resume)
463 static struct platform_driver imx8_acm_clk_driver = {
466 .of_match_table = imx8_acm_match,
467 .pm = &imx8_acm_pm_ops,
469 .probe = imx8_acm_clk_probe,
470 .remove = imx8_acm_clk_remove,
472 module_platform_driver(imx8_acm_clk_driver);
474 MODULE_AUTHOR("Shengjiu Wang <shengjiu.wang@nxp.com>");
475 MODULE_DESCRIPTION("Freescale i.MX8 Audio Clock Mux driver");
476 MODULE_LICENSE("GPL");