1 // SPDX-License-Identifier: GPL-2.0
3 // ALSA SoC IMX MQS driver
5 // Copyright (C) 2014-2015 Freescale Semiconductor, Inc.
9 #include <linux/module.h>
10 #include <linux/moduleparam.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
13 #include <linux/pm_runtime.h>
16 #include <linux/slab.h>
17 #include <sound/soc.h>
18 #include <sound/pcm.h>
19 #include <sound/initval.h>
21 #define REG_MQS_CTRL 0x00
23 #define MQS_EN_MASK (0x1 << 28)
24 #define MQS_EN_SHIFT (28)
25 #define MQS_SW_RST_MASK (0x1 << 24)
26 #define MQS_SW_RST_SHIFT (24)
27 #define MQS_OVERSAMPLE_MASK (0x1 << 20)
28 #define MQS_OVERSAMPLE_SHIFT (20)
29 #define MQS_CLK_DIV_MASK (0xFF << 0)
30 #define MQS_CLK_DIV_SHIFT (0)
32 /* codec private data */
34 struct regmap
*regmap
;
38 unsigned int reg_iomuxc_gpr2
;
39 unsigned int reg_mqs_ctrl
;
43 #define FSL_MQS_RATES (SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
44 #define FSL_MQS_FORMATS SNDRV_PCM_FMTBIT_S16_LE
46 static int fsl_mqs_hw_params(struct snd_pcm_substream
*substream
,
47 struct snd_pcm_hw_params
*params
,
48 struct snd_soc_dai
*dai
)
50 struct snd_soc_component
*component
= dai
->component
;
51 struct fsl_mqs
*mqs_priv
= snd_soc_component_get_drvdata(component
);
52 unsigned long mclk_rate
;
56 mclk_rate
= clk_get_rate(mqs_priv
->mclk
);
57 lrclk
= params_rate(params
);
60 * mclk_rate / (oversample(32,64) * FS * 2 * divider ) = repeat_rate;
61 * if repeat_rate is 8, mqs can achieve better quality.
62 * oversample rate is fix to 32 currently.
64 div
= mclk_rate
/ (32 * lrclk
* 2 * 8);
65 res
= mclk_rate
% (32 * lrclk
* 2 * 8);
67 if (res
== 0 && div
> 0 && div
<= 256) {
68 if (mqs_priv
->use_gpr
) {
69 regmap_update_bits(mqs_priv
->regmap
, IOMUXC_GPR2
,
70 IMX6SX_GPR2_MQS_CLK_DIV_MASK
,
71 (div
- 1) << IMX6SX_GPR2_MQS_CLK_DIV_SHIFT
);
72 regmap_update_bits(mqs_priv
->regmap
, IOMUXC_GPR2
,
73 IMX6SX_GPR2_MQS_OVERSAMPLE_MASK
, 0);
75 regmap_update_bits(mqs_priv
->regmap
, REG_MQS_CTRL
,
77 (div
- 1) << MQS_CLK_DIV_SHIFT
);
78 regmap_update_bits(mqs_priv
->regmap
, REG_MQS_CTRL
,
79 MQS_OVERSAMPLE_MASK
, 0);
82 dev_err(component
->dev
, "can't get proper divider\n");
88 static int fsl_mqs_set_dai_fmt(struct snd_soc_dai
*dai
, unsigned int fmt
)
90 /* Only LEFT_J & SLAVE mode is supported. */
91 switch (fmt
& SND_SOC_DAIFMT_FORMAT_MASK
) {
92 case SND_SOC_DAIFMT_LEFT_J
:
98 switch (fmt
& SND_SOC_DAIFMT_INV_MASK
) {
99 case SND_SOC_DAIFMT_NB_NF
:
105 switch (fmt
& SND_SOC_DAIFMT_MASTER_MASK
) {
106 case SND_SOC_DAIFMT_CBS_CFS
:
115 static int fsl_mqs_startup(struct snd_pcm_substream
*substream
,
116 struct snd_soc_dai
*dai
)
118 struct snd_soc_component
*component
= dai
->component
;
119 struct fsl_mqs
*mqs_priv
= snd_soc_component_get_drvdata(component
);
121 if (mqs_priv
->use_gpr
)
122 regmap_update_bits(mqs_priv
->regmap
, IOMUXC_GPR2
,
123 IMX6SX_GPR2_MQS_EN_MASK
,
124 1 << IMX6SX_GPR2_MQS_EN_SHIFT
);
126 regmap_update_bits(mqs_priv
->regmap
, REG_MQS_CTRL
,
132 static void fsl_mqs_shutdown(struct snd_pcm_substream
*substream
,
133 struct snd_soc_dai
*dai
)
135 struct snd_soc_component
*component
= dai
->component
;
136 struct fsl_mqs
*mqs_priv
= snd_soc_component_get_drvdata(component
);
138 if (mqs_priv
->use_gpr
)
139 regmap_update_bits(mqs_priv
->regmap
, IOMUXC_GPR2
,
140 IMX6SX_GPR2_MQS_EN_MASK
, 0);
142 regmap_update_bits(mqs_priv
->regmap
, REG_MQS_CTRL
,
146 static const struct snd_soc_component_driver soc_codec_fsl_mqs
= {
148 .non_legacy_dai_naming
= 1,
151 static const struct snd_soc_dai_ops fsl_mqs_dai_ops
= {
152 .startup
= fsl_mqs_startup
,
153 .shutdown
= fsl_mqs_shutdown
,
154 .hw_params
= fsl_mqs_hw_params
,
155 .set_fmt
= fsl_mqs_set_dai_fmt
,
158 static struct snd_soc_dai_driver fsl_mqs_dai
= {
159 .name
= "fsl-mqs-dai",
161 .stream_name
= "Playback",
164 .rates
= FSL_MQS_RATES
,
165 .formats
= FSL_MQS_FORMATS
,
167 .ops
= &fsl_mqs_dai_ops
,
170 static const struct regmap_config fsl_mqs_regmap_config
= {
174 .max_register
= REG_MQS_CTRL
,
175 .cache_type
= REGCACHE_NONE
,
178 static int fsl_mqs_probe(struct platform_device
*pdev
)
180 struct device_node
*np
= pdev
->dev
.of_node
;
181 struct device_node
*gpr_np
= NULL
;
182 struct fsl_mqs
*mqs_priv
;
186 mqs_priv
= devm_kzalloc(&pdev
->dev
, sizeof(*mqs_priv
), GFP_KERNEL
);
190 /* On i.MX6sx the MQS control register is in GPR domain
191 * But in i.MX8QM/i.MX8QXP the control register is moved
194 if (of_device_is_compatible(np
, "fsl,imx8qm-mqs"))
195 mqs_priv
->use_gpr
= false;
197 mqs_priv
->use_gpr
= true;
199 if (mqs_priv
->use_gpr
) {
200 gpr_np
= of_parse_phandle(np
, "gpr", 0);
202 dev_err(&pdev
->dev
, "failed to get gpr node by phandle\n");
206 mqs_priv
->regmap
= syscon_node_to_regmap(gpr_np
);
207 if (IS_ERR(mqs_priv
->regmap
)) {
208 dev_err(&pdev
->dev
, "failed to get gpr regmap\n");
209 ret
= PTR_ERR(mqs_priv
->regmap
);
210 goto err_free_gpr_np
;
213 regs
= devm_platform_ioremap_resource(pdev
, 0);
215 return PTR_ERR(regs
);
217 mqs_priv
->regmap
= devm_regmap_init_mmio_clk(&pdev
->dev
,
220 &fsl_mqs_regmap_config
);
221 if (IS_ERR(mqs_priv
->regmap
)) {
222 dev_err(&pdev
->dev
, "failed to init regmap: %ld\n",
223 PTR_ERR(mqs_priv
->regmap
));
224 return PTR_ERR(mqs_priv
->regmap
);
227 mqs_priv
->ipg
= devm_clk_get(&pdev
->dev
, "core");
228 if (IS_ERR(mqs_priv
->ipg
)) {
229 dev_err(&pdev
->dev
, "failed to get the clock: %ld\n",
230 PTR_ERR(mqs_priv
->ipg
));
231 return PTR_ERR(mqs_priv
->ipg
);
235 mqs_priv
->mclk
= devm_clk_get(&pdev
->dev
, "mclk");
236 if (IS_ERR(mqs_priv
->mclk
)) {
237 dev_err(&pdev
->dev
, "failed to get the clock: %ld\n",
238 PTR_ERR(mqs_priv
->mclk
));
239 ret
= PTR_ERR(mqs_priv
->mclk
);
240 goto err_free_gpr_np
;
243 dev_set_drvdata(&pdev
->dev
, mqs_priv
);
244 pm_runtime_enable(&pdev
->dev
);
246 ret
= devm_snd_soc_register_component(&pdev
->dev
, &soc_codec_fsl_mqs
,
249 goto err_free_gpr_np
;
258 static int fsl_mqs_remove(struct platform_device
*pdev
)
260 pm_runtime_disable(&pdev
->dev
);
265 static int fsl_mqs_runtime_resume(struct device
*dev
)
267 struct fsl_mqs
*mqs_priv
= dev_get_drvdata(dev
);
270 clk_prepare_enable(mqs_priv
->ipg
);
273 clk_prepare_enable(mqs_priv
->mclk
);
275 if (mqs_priv
->use_gpr
)
276 regmap_write(mqs_priv
->regmap
, IOMUXC_GPR2
,
277 mqs_priv
->reg_iomuxc_gpr2
);
279 regmap_write(mqs_priv
->regmap
, REG_MQS_CTRL
,
280 mqs_priv
->reg_mqs_ctrl
);
284 static int fsl_mqs_runtime_suspend(struct device
*dev
)
286 struct fsl_mqs
*mqs_priv
= dev_get_drvdata(dev
);
288 if (mqs_priv
->use_gpr
)
289 regmap_read(mqs_priv
->regmap
, IOMUXC_GPR2
,
290 &mqs_priv
->reg_iomuxc_gpr2
);
292 regmap_read(mqs_priv
->regmap
, REG_MQS_CTRL
,
293 &mqs_priv
->reg_mqs_ctrl
);
296 clk_disable_unprepare(mqs_priv
->mclk
);
299 clk_disable_unprepare(mqs_priv
->ipg
);
305 static const struct dev_pm_ops fsl_mqs_pm_ops
= {
306 SET_RUNTIME_PM_OPS(fsl_mqs_runtime_suspend
,
307 fsl_mqs_runtime_resume
,
309 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend
,
310 pm_runtime_force_resume
)
313 static const struct of_device_id fsl_mqs_dt_ids
[] = {
314 { .compatible
= "fsl,imx8qm-mqs", },
315 { .compatible
= "fsl,imx6sx-mqs", },
318 MODULE_DEVICE_TABLE(of
, fsl_mqs_dt_ids
);
320 static struct platform_driver fsl_mqs_driver
= {
321 .probe
= fsl_mqs_probe
,
322 .remove
= fsl_mqs_remove
,
325 .of_match_table
= fsl_mqs_dt_ids
,
326 .pm
= &fsl_mqs_pm_ops
,
330 module_platform_driver(fsl_mqs_driver
);
332 MODULE_AUTHOR("Shengjiu Wang <Shengjiu.Wang@nxp.com>");
333 MODULE_DESCRIPTION("MQS codec driver");
334 MODULE_LICENSE("GPL v2");
335 MODULE_ALIAS("platform: fsl-mqs");