xref: /linux/sound/soc/fsl/fsl_mqs.c (revision e1c4c5436b4ad579762fbe78bfabc8aef59bd5b1)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // ALSA SoC IMX MQS driver
4 //
5 // Copyright (C) 2014-2015 Freescale Semiconductor, Inc.
6 // Copyright 2019 NXP
7 
8 #include <linux/clk.h>
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/of_device.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/of.h>
16 #include <linux/pm.h>
17 #include <linux/slab.h>
18 #include <sound/soc.h>
19 #include <sound/pcm.h>
20 #include <sound/initval.h>
21 
22 #define REG_MQS_CTRL		0x00
23 
24 #define MQS_EN_MASK			(0x1 << 28)
25 #define MQS_EN_SHIFT			(28)
26 #define MQS_SW_RST_MASK			(0x1 << 24)
27 #define MQS_SW_RST_SHIFT		(24)
28 #define MQS_OVERSAMPLE_MASK		(0x1 << 20)
29 #define MQS_OVERSAMPLE_SHIFT		(20)
30 #define MQS_CLK_DIV_MASK		(0xFF << 0)
31 #define MQS_CLK_DIV_SHIFT		(0)
32 
33 /**
34  * struct fsl_mqs_soc_data - soc specific data
35  *
36  * @use_gpr: control register is in General Purpose Register group
37  * @ctrl_off: control register offset
38  * @en_mask: enable bit mask
39  * @en_shift: enable bit shift
40  * @rst_mask: reset bit mask
41  * @rst_shift: reset bit shift
42  * @osr_mask: oversample bit mask
43  * @osr_shift: oversample bit shift
44  * @div_mask: clock divider mask
45  * @div_shift: clock divider bit shift
46  */
47 struct fsl_mqs_soc_data {
48 	bool use_gpr;
49 	int  ctrl_off;
50 	int  en_mask;
51 	int  en_shift;
52 	int  rst_mask;
53 	int  rst_shift;
54 	int  osr_mask;
55 	int  osr_shift;
56 	int  div_mask;
57 	int  div_shift;
58 };
59 
60 /* codec private data */
61 struct fsl_mqs {
62 	struct regmap *regmap;
63 	struct clk *mclk;
64 	struct clk *ipg;
65 	const struct fsl_mqs_soc_data *soc;
66 
67 	unsigned int reg_mqs_ctrl;
68 };
69 
70 #define FSL_MQS_RATES	(SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000)
71 #define FSL_MQS_FORMATS	SNDRV_PCM_FMTBIT_S16_LE
72 
73 static int fsl_mqs_hw_params(struct snd_pcm_substream *substream,
74 			     struct snd_pcm_hw_params *params,
75 			     struct snd_soc_dai *dai)
76 {
77 	struct snd_soc_component *component = dai->component;
78 	struct fsl_mqs *mqs_priv = snd_soc_component_get_drvdata(component);
79 	unsigned long mclk_rate;
80 	int div, res;
81 	int lrclk;
82 
83 	mclk_rate = clk_get_rate(mqs_priv->mclk);
84 	lrclk = params_rate(params);
85 
86 	/*
87 	 * mclk_rate / (oversample(32,64) * FS * 2 * divider ) = repeat_rate;
88 	 * if repeat_rate is 8, mqs can achieve better quality.
89 	 * oversample rate is fix to 32 currently.
90 	 */
91 	div = mclk_rate / (32 * lrclk * 2 * 8);
92 	res = mclk_rate % (32 * lrclk * 2 * 8);
93 
94 	if (res == 0 && div > 0 && div <= 256) {
95 		regmap_update_bits(mqs_priv->regmap, mqs_priv->soc->ctrl_off,
96 				   mqs_priv->soc->div_mask,
97 				   (div - 1) << mqs_priv->soc->div_shift);
98 		regmap_update_bits(mqs_priv->regmap, mqs_priv->soc->ctrl_off,
99 				   mqs_priv->soc->osr_mask, 0);
100 	} else {
101 		dev_err(component->dev, "can't get proper divider\n");
102 	}
103 
104 	return 0;
105 }
106 
107 static int fsl_mqs_set_dai_fmt(struct snd_soc_dai *dai, unsigned int fmt)
108 {
109 	/* Only LEFT_J & SLAVE mode is supported. */
110 	switch (fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
111 	case SND_SOC_DAIFMT_LEFT_J:
112 		break;
113 	default:
114 		return -EINVAL;
115 	}
116 
117 	switch (fmt & SND_SOC_DAIFMT_INV_MASK) {
118 	case SND_SOC_DAIFMT_NB_NF:
119 		break;
120 	default:
121 		return -EINVAL;
122 	}
123 
124 	switch (fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
125 	case SND_SOC_DAIFMT_CBC_CFC:
126 		break;
127 	default:
128 		return -EINVAL;
129 	}
130 
131 	return 0;
132 }
133 
134 static int fsl_mqs_startup(struct snd_pcm_substream *substream,
135 			   struct snd_soc_dai *dai)
136 {
137 	struct snd_soc_component *component = dai->component;
138 	struct fsl_mqs *mqs_priv = snd_soc_component_get_drvdata(component);
139 
140 	regmap_update_bits(mqs_priv->regmap, mqs_priv->soc->ctrl_off,
141 			   mqs_priv->soc->en_mask,
142 			   1 << mqs_priv->soc->en_shift);
143 	return 0;
144 }
145 
146 static void fsl_mqs_shutdown(struct snd_pcm_substream *substream,
147 			     struct snd_soc_dai *dai)
148 {
149 	struct snd_soc_component *component = dai->component;
150 	struct fsl_mqs *mqs_priv = snd_soc_component_get_drvdata(component);
151 
152 	regmap_update_bits(mqs_priv->regmap, mqs_priv->soc->ctrl_off,
153 			   mqs_priv->soc->en_mask, 0);
154 }
155 
156 static const struct snd_soc_component_driver soc_codec_fsl_mqs = {
157 	.idle_bias_on = 1,
158 };
159 
160 static const struct snd_soc_dai_ops fsl_mqs_dai_ops = {
161 	.startup = fsl_mqs_startup,
162 	.shutdown = fsl_mqs_shutdown,
163 	.hw_params = fsl_mqs_hw_params,
164 	.set_fmt = fsl_mqs_set_dai_fmt,
165 };
166 
167 static struct snd_soc_dai_driver fsl_mqs_dai = {
168 	.name		= "fsl-mqs-dai",
169 	.playback	= {
170 		.stream_name	= "Playback",
171 		.channels_min	= 2,
172 		.channels_max	= 2,
173 		.rates		= FSL_MQS_RATES,
174 		.formats	= FSL_MQS_FORMATS,
175 	},
176 	.ops = &fsl_mqs_dai_ops,
177 };
178 
179 static const struct regmap_config fsl_mqs_regmap_config = {
180 	.reg_bits = 32,
181 	.reg_stride = 4,
182 	.val_bits = 32,
183 	.max_register = REG_MQS_CTRL,
184 	.cache_type = REGCACHE_NONE,
185 };
186 
187 static int fsl_mqs_probe(struct platform_device *pdev)
188 {
189 	struct device_node *np = pdev->dev.of_node;
190 	struct device_node *gpr_np = NULL;
191 	struct fsl_mqs *mqs_priv;
192 	void __iomem *regs;
193 	int ret;
194 
195 	mqs_priv = devm_kzalloc(&pdev->dev, sizeof(*mqs_priv), GFP_KERNEL);
196 	if (!mqs_priv)
197 		return -ENOMEM;
198 
199 	/* On i.MX6sx the MQS control register is in GPR domain
200 	 * But in i.MX8QM/i.MX8QXP the control register is moved
201 	 * to its own domain.
202 	 */
203 	mqs_priv->soc = of_device_get_match_data(&pdev->dev);
204 
205 	if (mqs_priv->soc->use_gpr) {
206 		gpr_np = of_parse_phandle(np, "gpr", 0);
207 		if (!gpr_np) {
208 			dev_err(&pdev->dev, "failed to get gpr node by phandle\n");
209 			return -EINVAL;
210 		}
211 
212 		mqs_priv->regmap = syscon_node_to_regmap(gpr_np);
213 		if (IS_ERR(mqs_priv->regmap)) {
214 			dev_err(&pdev->dev, "failed to get gpr regmap\n");
215 			ret = PTR_ERR(mqs_priv->regmap);
216 			goto err_free_gpr_np;
217 		}
218 	} else {
219 		regs = devm_platform_ioremap_resource(pdev, 0);
220 		if (IS_ERR(regs))
221 			return PTR_ERR(regs);
222 
223 		mqs_priv->regmap = devm_regmap_init_mmio_clk(&pdev->dev,
224 							     "core",
225 							     regs,
226 							     &fsl_mqs_regmap_config);
227 		if (IS_ERR(mqs_priv->regmap)) {
228 			dev_err(&pdev->dev, "failed to init regmap: %ld\n",
229 				PTR_ERR(mqs_priv->regmap));
230 			return PTR_ERR(mqs_priv->regmap);
231 		}
232 
233 		mqs_priv->ipg = devm_clk_get(&pdev->dev, "core");
234 		if (IS_ERR(mqs_priv->ipg)) {
235 			dev_err(&pdev->dev, "failed to get the clock: %ld\n",
236 				PTR_ERR(mqs_priv->ipg));
237 			return PTR_ERR(mqs_priv->ipg);
238 		}
239 	}
240 
241 	mqs_priv->mclk = devm_clk_get(&pdev->dev, "mclk");
242 	if (IS_ERR(mqs_priv->mclk)) {
243 		dev_err(&pdev->dev, "failed to get the clock: %ld\n",
244 			PTR_ERR(mqs_priv->mclk));
245 		ret = PTR_ERR(mqs_priv->mclk);
246 		goto err_free_gpr_np;
247 	}
248 
249 	dev_set_drvdata(&pdev->dev, mqs_priv);
250 	pm_runtime_enable(&pdev->dev);
251 
252 	ret = devm_snd_soc_register_component(&pdev->dev, &soc_codec_fsl_mqs,
253 			&fsl_mqs_dai, 1);
254 	if (ret)
255 		goto err_free_gpr_np;
256 	return 0;
257 
258 err_free_gpr_np:
259 	of_node_put(gpr_np);
260 
261 	return ret;
262 }
263 
264 static int fsl_mqs_remove(struct platform_device *pdev)
265 {
266 	pm_runtime_disable(&pdev->dev);
267 	return 0;
268 }
269 
270 #ifdef CONFIG_PM
271 static int fsl_mqs_runtime_resume(struct device *dev)
272 {
273 	struct fsl_mqs *mqs_priv = dev_get_drvdata(dev);
274 	int ret;
275 
276 	ret = clk_prepare_enable(mqs_priv->ipg);
277 	if (ret) {
278 		dev_err(dev, "failed to enable ipg clock\n");
279 		return ret;
280 	}
281 
282 	ret = clk_prepare_enable(mqs_priv->mclk);
283 	if (ret) {
284 		dev_err(dev, "failed to enable mclk clock\n");
285 		clk_disable_unprepare(mqs_priv->ipg);
286 		return ret;
287 	}
288 
289 	regmap_write(mqs_priv->regmap, mqs_priv->soc->ctrl_off, mqs_priv->reg_mqs_ctrl);
290 	return 0;
291 }
292 
293 static int fsl_mqs_runtime_suspend(struct device *dev)
294 {
295 	struct fsl_mqs *mqs_priv = dev_get_drvdata(dev);
296 
297 	regmap_read(mqs_priv->regmap, mqs_priv->soc->ctrl_off, &mqs_priv->reg_mqs_ctrl);
298 
299 	clk_disable_unprepare(mqs_priv->mclk);
300 	clk_disable_unprepare(mqs_priv->ipg);
301 
302 	return 0;
303 }
304 #endif
305 
306 static const struct dev_pm_ops fsl_mqs_pm_ops = {
307 	SET_RUNTIME_PM_OPS(fsl_mqs_runtime_suspend,
308 			   fsl_mqs_runtime_resume,
309 			   NULL)
310 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
311 				pm_runtime_force_resume)
312 };
313 
314 static const struct fsl_mqs_soc_data fsl_mqs_imx8qm_data = {
315 	.use_gpr = false,
316 	.ctrl_off = REG_MQS_CTRL,
317 	.en_mask  = MQS_EN_MASK,
318 	.en_shift = MQS_EN_SHIFT,
319 	.rst_mask = MQS_SW_RST_MASK,
320 	.rst_shift = MQS_SW_RST_SHIFT,
321 	.osr_mask = MQS_OVERSAMPLE_MASK,
322 	.osr_shift = MQS_OVERSAMPLE_SHIFT,
323 	.div_mask = MQS_CLK_DIV_MASK,
324 	.div_shift = MQS_CLK_DIV_SHIFT,
325 };
326 
327 static const struct fsl_mqs_soc_data fsl_mqs_imx6sx_data = {
328 	.use_gpr = true,
329 	.ctrl_off = IOMUXC_GPR2,
330 	.en_mask  = IMX6SX_GPR2_MQS_EN_MASK,
331 	.en_shift = IMX6SX_GPR2_MQS_EN_SHIFT,
332 	.rst_mask = IMX6SX_GPR2_MQS_SW_RST_MASK,
333 	.rst_shift = IMX6SX_GPR2_MQS_SW_RST_SHIFT,
334 	.osr_mask  = IMX6SX_GPR2_MQS_OVERSAMPLE_MASK,
335 	.osr_shift = IMX6SX_GPR2_MQS_OVERSAMPLE_SHIFT,
336 	.div_mask  = IMX6SX_GPR2_MQS_CLK_DIV_MASK,
337 	.div_shift = IMX6SX_GPR2_MQS_CLK_DIV_SHIFT,
338 };
339 
340 static const struct fsl_mqs_soc_data fsl_mqs_imx93_data = {
341 	.use_gpr = true,
342 	.ctrl_off = 0x20,
343 	.en_mask  = BIT(1),
344 	.en_shift = 1,
345 	.rst_mask = BIT(2),
346 	.rst_shift = 2,
347 	.osr_mask = BIT(3),
348 	.osr_shift = 3,
349 	.div_mask = GENMASK(15, 8),
350 	.div_shift = 8,
351 };
352 
353 static const struct of_device_id fsl_mqs_dt_ids[] = {
354 	{ .compatible = "fsl,imx8qm-mqs", .data = &fsl_mqs_imx8qm_data },
355 	{ .compatible = "fsl,imx6sx-mqs", .data = &fsl_mqs_imx6sx_data },
356 	{ .compatible = "fsl,imx93-mqs", .data = &fsl_mqs_imx93_data },
357 	{}
358 };
359 MODULE_DEVICE_TABLE(of, fsl_mqs_dt_ids);
360 
361 static struct platform_driver fsl_mqs_driver = {
362 	.probe		= fsl_mqs_probe,
363 	.remove		= fsl_mqs_remove,
364 	.driver		= {
365 		.name	= "fsl-mqs",
366 		.of_match_table = fsl_mqs_dt_ids,
367 		.pm = &fsl_mqs_pm_ops,
368 	},
369 };
370 
371 module_platform_driver(fsl_mqs_driver);
372 
373 MODULE_AUTHOR("Shengjiu Wang <Shengjiu.Wang@nxp.com>");
374 MODULE_DESCRIPTION("MQS codec driver");
375 MODULE_LICENSE("GPL v2");
376 MODULE_ALIAS("platform:fsl-mqs");
377