2 * Freescale Generic ASoC Sound Card driver with ASRC
4 * Copyright (C) 2014 Freescale Semiconductor, Inc.
6 * Author: Nicolin Chen <nicoleotsuka@gmail.com>
8 * This file is licensed under the terms of the GNU General Public License
9 * version 2. This program is licensed "as is" without any warranty of any
10 * kind, whether express or implied.
13 #include <linux/clk.h>
14 #include <linux/i2c.h>
15 #include <linux/module.h>
16 #include <linux/of_platform.h>
17 #include <sound/pcm_params.h>
18 #include <sound/soc.h>
22 #include "imx-audmux.h"
24 #include "../codecs/sgtl5000.h"
25 #include "../codecs/wm8962.h"
30 /* Default DAI format without Master and Slave flag */
31 #define DAI_FMT_BASE (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF)
36 * @mclk_freq: Clock rate of MCLK
37 * @mclk_id: MCLK (or main clock) id for set_sysclk()
38 * @fll_id: FLL (or secordary clock) id for set_sysclk()
39 * @pll_id: PLL id for set_pll()
42 unsigned long mclk_freq;
51 * @sysclk_freq[2]: SYSCLK rates for set_sysclk()
52 * @sysclk_dir[2]: SYSCLK directions for set_sysclk()
53 * @sysclk_id[2]: SYSCLK ids for set_sysclk()
55 * Note: [1] for tx and [0] for rx
58 unsigned long sysclk_freq[2];
64 * Freescale Generic ASOC card private data
66 * @dai_link[3]: DAI link structure including normal one and DPCM link
67 * @pdev: platform device pointer
68 * @codec_priv: CODEC private data
69 * @cpu_priv: CPU private data
70 * @card: ASoC card structure
71 * @sample_rate: Current sample rate
72 * @sample_format: Current sample format
73 * @asrc_rate: ASRC sample rate used by Back-Ends
74 * @asrc_format: ASRC sample format used by Back-Ends
75 * @dai_fmt: DAI format between CPU and CODEC
79 struct fsl_asoc_card_priv {
80 struct snd_soc_dai_link dai_link[3];
81 struct platform_device *pdev;
82 struct codec_priv codec_priv;
83 struct cpu_priv cpu_priv;
84 struct snd_soc_card card;
94 * This dapm route map exsits for DPCM link only.
95 * The other routes shall go through Device Tree.
97 static const struct snd_soc_dapm_route audio_map[] = {
98 {"CPU-Playback", NULL, "ASRC-Playback"},
99 {"Playback", NULL, "CPU-Playback"},
100 {"ASRC-Capture", NULL, "CPU-Capture"},
101 {"CPU-Capture", NULL, "Capture"},
104 /* Add all possible widgets into here without being redundant */
105 static const struct snd_soc_dapm_widget fsl_asoc_card_dapm_widgets[] = {
106 SND_SOC_DAPM_LINE("Line Out Jack", NULL),
107 SND_SOC_DAPM_LINE("Line In Jack", NULL),
108 SND_SOC_DAPM_HP("Headphone Jack", NULL),
109 SND_SOC_DAPM_SPK("Ext Spk", NULL),
110 SND_SOC_DAPM_MIC("Mic Jack", NULL),
111 SND_SOC_DAPM_MIC("AMIC", NULL),
112 SND_SOC_DAPM_MIC("DMIC", NULL),
115 static int fsl_asoc_card_hw_params(struct snd_pcm_substream *substream,
116 struct snd_pcm_hw_params *params)
118 struct snd_soc_pcm_runtime *rtd = substream->private_data;
119 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
120 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
121 struct cpu_priv *cpu_priv = &priv->cpu_priv;
122 struct device *dev = rtd->card->dev;
125 priv->sample_rate = params_rate(params);
126 priv->sample_format = params_format(params);
128 if (priv->card.set_bias_level)
131 /* Specific configurations of DAIs starts from here */
132 ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, cpu_priv->sysclk_id[tx],
133 cpu_priv->sysclk_freq[tx],
134 cpu_priv->sysclk_dir[tx]);
136 dev_err(dev, "failed to set sysclk for cpu dai\n");
143 static struct snd_soc_ops fsl_asoc_card_ops = {
144 .hw_params = fsl_asoc_card_hw_params,
147 static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
148 struct snd_pcm_hw_params *params)
150 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
151 struct snd_interval *rate;
152 struct snd_mask *mask;
154 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
155 rate->max = rate->min = priv->asrc_rate;
157 mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
159 snd_mask_set(mask, priv->asrc_format);
164 static struct snd_soc_dai_link fsl_asoc_card_dai[] = {
165 /* Default ASoC DAI Link*/
168 .stream_name = "HiFi",
169 .ops = &fsl_asoc_card_ops,
171 /* DPCM Link between Front-End and Back-End (Optional) */
173 .name = "HiFi-ASRC-FE",
174 .stream_name = "HiFi-ASRC-FE",
175 .codec_name = "snd-soc-dummy",
176 .codec_dai_name = "snd-soc-dummy-dai",
182 .name = "HiFi-ASRC-BE",
183 .stream_name = "HiFi-ASRC-BE",
184 .platform_name = "snd-soc-dummy",
185 .be_hw_params_fixup = be_hw_params_fixup,
186 .ops = &fsl_asoc_card_ops,
193 static int fsl_asoc_card_set_bias_level(struct snd_soc_card *card,
194 struct snd_soc_dapm_context *dapm,
195 enum snd_soc_bias_level level)
197 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
198 struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
199 struct codec_priv *codec_priv = &priv->codec_priv;
200 struct device *dev = card->dev;
201 unsigned int pll_out;
204 if (dapm->dev != codec_dai->dev)
208 case SND_SOC_BIAS_PREPARE:
209 if (dapm->bias_level != SND_SOC_BIAS_STANDBY)
212 if (priv->sample_format == SNDRV_PCM_FORMAT_S24_LE)
213 pll_out = priv->sample_rate * 384;
215 pll_out = priv->sample_rate * 256;
217 ret = snd_soc_dai_set_pll(codec_dai, codec_priv->pll_id,
219 codec_priv->mclk_freq, pll_out);
221 dev_err(dev, "failed to start FLL: %d\n", ret);
225 ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->fll_id,
226 pll_out, SND_SOC_CLOCK_IN);
228 dev_err(dev, "failed to set SYSCLK: %d\n", ret);
233 case SND_SOC_BIAS_STANDBY:
234 if (dapm->bias_level != SND_SOC_BIAS_PREPARE)
237 ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
238 codec_priv->mclk_freq,
241 dev_err(dev, "failed to switch away from FLL: %d\n", ret);
245 ret = snd_soc_dai_set_pll(codec_dai, codec_priv->pll_id, 0, 0, 0);
247 dev_err(dev, "failed to stop FLL: %d\n", ret);
259 static int fsl_asoc_card_audmux_init(struct device_node *np,
260 struct fsl_asoc_card_priv *priv)
262 struct device *dev = &priv->pdev->dev;
263 u32 int_ptcr = 0, ext_ptcr = 0;
264 int int_port, ext_port;
267 ret = of_property_read_u32(np, "mux-int-port", &int_port);
269 dev_err(dev, "mux-int-port missing or invalid\n");
272 ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
274 dev_err(dev, "mux-ext-port missing or invalid\n");
279 * The port numbering in the hardware manual starts at 1, while
280 * the AUDMUX API expects it starts at 0.
286 * Use asynchronous mode (6 wires) for all cases.
287 * If only 4 wires are needed, just set SSI into
288 * synchronous mode and enable 4 PADs in IOMUX.
290 switch (priv->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
291 case SND_SOC_DAIFMT_CBM_CFM:
292 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
293 IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
294 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
295 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
296 IMX_AUDMUX_V2_PTCR_RFSDIR |
297 IMX_AUDMUX_V2_PTCR_RCLKDIR |
298 IMX_AUDMUX_V2_PTCR_TFSDIR |
299 IMX_AUDMUX_V2_PTCR_TCLKDIR;
301 case SND_SOC_DAIFMT_CBM_CFS:
302 int_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
303 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
304 IMX_AUDMUX_V2_PTCR_RCLKDIR |
305 IMX_AUDMUX_V2_PTCR_TCLKDIR;
306 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
307 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
308 IMX_AUDMUX_V2_PTCR_RFSDIR |
309 IMX_AUDMUX_V2_PTCR_TFSDIR;
311 case SND_SOC_DAIFMT_CBS_CFM:
312 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
313 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
314 IMX_AUDMUX_V2_PTCR_RFSDIR |
315 IMX_AUDMUX_V2_PTCR_TFSDIR;
316 ext_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
317 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
318 IMX_AUDMUX_V2_PTCR_RCLKDIR |
319 IMX_AUDMUX_V2_PTCR_TCLKDIR;
321 case SND_SOC_DAIFMT_CBS_CFS:
322 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
323 IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
324 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
325 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
326 IMX_AUDMUX_V2_PTCR_RFSDIR |
327 IMX_AUDMUX_V2_PTCR_RCLKDIR |
328 IMX_AUDMUX_V2_PTCR_TFSDIR |
329 IMX_AUDMUX_V2_PTCR_TCLKDIR;
335 /* Asynchronous mode can not be set along with RCLKDIR */
336 ret = imx_audmux_v2_configure_port(int_port, 0,
337 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
339 dev_err(dev, "audmux internal port setup failed\n");
343 ret = imx_audmux_v2_configure_port(int_port, int_ptcr,
344 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
346 dev_err(dev, "audmux internal port setup failed\n");
350 ret = imx_audmux_v2_configure_port(ext_port, 0,
351 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
353 dev_err(dev, "audmux external port setup failed\n");
357 ret = imx_audmux_v2_configure_port(ext_port, ext_ptcr,
358 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
360 dev_err(dev, "audmux external port setup failed\n");
367 static int fsl_asoc_card_late_probe(struct snd_soc_card *card)
369 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
370 struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
371 struct codec_priv *codec_priv = &priv->codec_priv;
372 struct device *dev = card->dev;
375 ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
376 codec_priv->mclk_freq, SND_SOC_CLOCK_IN);
378 dev_err(dev, "failed to set sysclk in %s\n", __func__);
385 static int fsl_asoc_card_probe(struct platform_device *pdev)
387 struct device_node *cpu_np, *codec_np, *asrc_np;
388 struct device_node *np = pdev->dev.of_node;
389 struct platform_device *asrc_pdev = NULL;
390 struct platform_device *cpu_pdev;
391 struct fsl_asoc_card_priv *priv;
392 struct i2c_client *codec_dev;
393 struct clk *codec_clk;
397 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
401 cpu_np = of_parse_phandle(np, "audio-cpu", 0);
402 /* Give a chance to old DT binding */
404 cpu_np = of_parse_phandle(np, "ssi-controller", 0);
405 codec_np = of_parse_phandle(np, "audio-codec", 0);
406 if (!cpu_np || !codec_np) {
407 dev_err(&pdev->dev, "phandle missing or invalid\n");
412 cpu_pdev = of_find_device_by_node(cpu_np);
414 dev_err(&pdev->dev, "failed to find CPU DAI device\n");
419 codec_dev = of_find_i2c_device_by_node(codec_np);
421 dev_err(&pdev->dev, "failed to find codec platform device\n");
426 asrc_np = of_parse_phandle(np, "audio-asrc", 0);
428 asrc_pdev = of_find_device_by_node(asrc_np);
430 /* Get the MCLK rate only, and leave it controlled by CODEC drivers */
431 codec_clk = clk_get(&codec_dev->dev, NULL);
432 if (!IS_ERR(codec_clk)) {
433 priv->codec_priv.mclk_freq = clk_get_rate(codec_clk);
437 /* Default sample rate and format, will be updated in hw_params() */
438 priv->sample_rate = 44100;
439 priv->sample_format = SNDRV_PCM_FORMAT_S16_LE;
441 /* Assign a default DAI format, and allow each card to overwrite it */
442 priv->dai_fmt = DAI_FMT_BASE;
444 /* Diversify the card configurations */
445 if (of_device_is_compatible(np, "fsl,imx-audio-cs42888")) {
446 priv->card.set_bias_level = NULL;
447 priv->cpu_priv.sysclk_freq[TX] = priv->codec_priv.mclk_freq;
448 priv->cpu_priv.sysclk_freq[RX] = priv->codec_priv.mclk_freq;
449 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
450 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
451 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
452 } else if (of_device_is_compatible(np, "fsl,imx-audio-sgtl5000")) {
453 priv->codec_priv.mclk_id = SGTL5000_SYSCLK;
454 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
455 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8962")) {
456 priv->card.set_bias_level = fsl_asoc_card_set_bias_level;
457 priv->codec_priv.mclk_id = WM8962_SYSCLK_MCLK;
458 priv->codec_priv.fll_id = WM8962_SYSCLK_FLL;
459 priv->codec_priv.pll_id = WM8962_FLL;
460 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
462 dev_err(&pdev->dev, "unknown Device Tree compatible\n");
466 /* Common settings for corresponding Freescale CPU DAI driver */
467 if (strstr(cpu_np->name, "ssi")) {
468 /* Only SSI needs to configure AUDMUX */
469 ret = fsl_asoc_card_audmux_init(np, priv);
471 dev_err(&pdev->dev, "failed to init audmux\n");
474 } else if (strstr(cpu_np->name, "esai")) {
475 priv->cpu_priv.sysclk_id[1] = ESAI_HCKT_EXTAL;
476 priv->cpu_priv.sysclk_id[0] = ESAI_HCKR_EXTAL;
477 } else if (strstr(cpu_np->name, "sai")) {
478 priv->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
479 priv->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
482 sprintf(priv->name, "%s-audio", codec_dev->name);
484 /* Initialize sound card */
486 priv->card.dev = &pdev->dev;
487 priv->card.name = priv->name;
488 priv->card.dai_link = priv->dai_link;
489 priv->card.dapm_routes = audio_map;
490 priv->card.late_probe = fsl_asoc_card_late_probe;
491 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map);
492 priv->card.dapm_widgets = fsl_asoc_card_dapm_widgets;
493 priv->card.num_dapm_widgets = ARRAY_SIZE(fsl_asoc_card_dapm_widgets);
495 memcpy(priv->dai_link, fsl_asoc_card_dai,
496 sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link));
498 /* Normal DAI Link */
499 priv->dai_link[0].cpu_of_node = cpu_np;
500 priv->dai_link[0].codec_of_node = codec_np;
501 priv->dai_link[0].codec_dai_name = codec_dev->name;
502 priv->dai_link[0].platform_of_node = cpu_np;
503 priv->dai_link[0].dai_fmt = priv->dai_fmt;
504 priv->card.num_links = 1;
507 /* DPCM DAI Links only if ASRC exsits */
508 priv->dai_link[1].cpu_of_node = asrc_np;
509 priv->dai_link[1].platform_of_node = asrc_np;
510 priv->dai_link[2].codec_dai_name = codec_dev->name;
511 priv->dai_link[2].codec_of_node = codec_np;
512 priv->dai_link[2].cpu_of_node = cpu_np;
513 priv->dai_link[2].dai_fmt = priv->dai_fmt;
514 priv->card.num_links = 3;
516 ret = of_property_read_u32(asrc_np, "fsl,asrc-rate",
519 dev_err(&pdev->dev, "failed to get output rate\n");
524 ret = of_property_read_u32(asrc_np, "fsl,asrc-width", &width);
526 dev_err(&pdev->dev, "failed to get output rate\n");
532 priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
534 priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
537 /* Finish card registering */
538 platform_set_drvdata(pdev, priv);
539 snd_soc_card_set_drvdata(&priv->card, priv);
541 ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
543 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
546 of_node_put(asrc_np);
548 of_node_put(codec_np);
554 static const struct of_device_id fsl_asoc_card_dt_ids[] = {
555 { .compatible = "fsl,imx-audio-cs42888", },
556 { .compatible = "fsl,imx-audio-sgtl5000", },
557 { .compatible = "fsl,imx-audio-wm8962", },
561 static struct platform_driver fsl_asoc_card_driver = {
562 .probe = fsl_asoc_card_probe,
564 .name = "fsl-asoc-card",
565 .pm = &snd_soc_pm_ops,
566 .of_match_table = fsl_asoc_card_dt_ids,
569 module_platform_driver(fsl_asoc_card_driver);
571 MODULE_DESCRIPTION("Freescale Generic ASoC Sound Card driver with ASRC");
572 MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
573 MODULE_ALIAS("platform:fsl-asoc-card");
574 MODULE_LICENSE("GPL");