ixgbevf: Fix checksum error when using stacked vlan
[linux/fpc-iii.git] / sound / soc / fsl / fsl-asoc-card.c
blob3f6959c8e2f71b44cf054445b85553960e8c94d9
1 /*
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>
20 #include "fsl_esai.h"
21 #include "fsl_sai.h"
22 #include "imx-audmux.h"
24 #include "../codecs/sgtl5000.h"
25 #include "../codecs/wm8962.h"
27 #define RX 0
28 #define TX 1
30 /* Default DAI format without Master and Slave flag */
31 #define DAI_FMT_BASE (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF)
33 /**
34 * CODEC private data
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()
41 struct codec_priv {
42 unsigned long mclk_freq;
43 u32 mclk_id;
44 u32 fll_id;
45 u32 pll_id;
48 /**
49 * CPU private data
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()
54 * @slot_width: Slot width of each frame
56 * Note: [1] for tx and [0] for rx
58 struct cpu_priv {
59 unsigned long sysclk_freq[2];
60 u32 sysclk_dir[2];
61 u32 sysclk_id[2];
62 u32 slot_width;
65 /**
66 * Freescale Generic ASOC card private data
68 * @dai_link[3]: DAI link structure including normal one and DPCM link
69 * @pdev: platform device pointer
70 * @codec_priv: CODEC private data
71 * @cpu_priv: CPU private data
72 * @card: ASoC card structure
73 * @sample_rate: Current sample rate
74 * @sample_format: Current sample format
75 * @asrc_rate: ASRC sample rate used by Back-Ends
76 * @asrc_format: ASRC sample format used by Back-Ends
77 * @dai_fmt: DAI format between CPU and CODEC
78 * @name: Card name
81 struct fsl_asoc_card_priv {
82 struct snd_soc_dai_link dai_link[3];
83 struct platform_device *pdev;
84 struct codec_priv codec_priv;
85 struct cpu_priv cpu_priv;
86 struct snd_soc_card card;
87 u32 sample_rate;
88 u32 sample_format;
89 u32 asrc_rate;
90 u32 asrc_format;
91 u32 dai_fmt;
92 char name[32];
95 /**
96 * This dapm route map exsits for DPCM link only.
97 * The other routes shall go through Device Tree.
99 static const struct snd_soc_dapm_route audio_map[] = {
100 {"CPU-Playback", NULL, "ASRC-Playback"},
101 {"Playback", NULL, "CPU-Playback"},
102 {"ASRC-Capture", NULL, "CPU-Capture"},
103 {"CPU-Capture", NULL, "Capture"},
106 /* Add all possible widgets into here without being redundant */
107 static const struct snd_soc_dapm_widget fsl_asoc_card_dapm_widgets[] = {
108 SND_SOC_DAPM_LINE("Line Out Jack", NULL),
109 SND_SOC_DAPM_LINE("Line In Jack", NULL),
110 SND_SOC_DAPM_HP("Headphone Jack", NULL),
111 SND_SOC_DAPM_SPK("Ext Spk", NULL),
112 SND_SOC_DAPM_MIC("Mic Jack", NULL),
113 SND_SOC_DAPM_MIC("AMIC", NULL),
114 SND_SOC_DAPM_MIC("DMIC", NULL),
117 static int fsl_asoc_card_hw_params(struct snd_pcm_substream *substream,
118 struct snd_pcm_hw_params *params)
120 struct snd_soc_pcm_runtime *rtd = substream->private_data;
121 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
122 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
123 struct cpu_priv *cpu_priv = &priv->cpu_priv;
124 struct device *dev = rtd->card->dev;
125 int ret;
127 priv->sample_rate = params_rate(params);
128 priv->sample_format = params_format(params);
131 * If codec-dai is DAI Master and all configurations are already in the
132 * set_bias_level(), bypass the remaining settings in hw_params().
133 * Note: (dai_fmt & CBM_CFM) includes CBM_CFM and CBM_CFS.
135 if (priv->card.set_bias_level && priv->dai_fmt & SND_SOC_DAIFMT_CBM_CFM)
136 return 0;
138 /* Specific configurations of DAIs starts from here */
139 ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, cpu_priv->sysclk_id[tx],
140 cpu_priv->sysclk_freq[tx],
141 cpu_priv->sysclk_dir[tx]);
142 if (ret) {
143 dev_err(dev, "failed to set sysclk for cpu dai\n");
144 return ret;
147 if (cpu_priv->slot_width) {
148 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2,
149 cpu_priv->slot_width);
150 if (ret) {
151 dev_err(dev, "failed to set TDM slot for cpu dai\n");
152 return ret;
156 return 0;
159 static struct snd_soc_ops fsl_asoc_card_ops = {
160 .hw_params = fsl_asoc_card_hw_params,
163 static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
164 struct snd_pcm_hw_params *params)
166 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
167 struct snd_interval *rate;
168 struct snd_mask *mask;
170 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
171 rate->max = rate->min = priv->asrc_rate;
173 mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
174 snd_mask_none(mask);
175 snd_mask_set(mask, priv->asrc_format);
177 return 0;
180 static struct snd_soc_dai_link fsl_asoc_card_dai[] = {
181 /* Default ASoC DAI Link*/
183 .name = "HiFi",
184 .stream_name = "HiFi",
185 .ops = &fsl_asoc_card_ops,
187 /* DPCM Link between Front-End and Back-End (Optional) */
189 .name = "HiFi-ASRC-FE",
190 .stream_name = "HiFi-ASRC-FE",
191 .codec_name = "snd-soc-dummy",
192 .codec_dai_name = "snd-soc-dummy-dai",
193 .dpcm_playback = 1,
194 .dpcm_capture = 1,
195 .dynamic = 1,
198 .name = "HiFi-ASRC-BE",
199 .stream_name = "HiFi-ASRC-BE",
200 .platform_name = "snd-soc-dummy",
201 .be_hw_params_fixup = be_hw_params_fixup,
202 .ops = &fsl_asoc_card_ops,
203 .dpcm_playback = 1,
204 .dpcm_capture = 1,
205 .no_pcm = 1,
209 static int fsl_asoc_card_set_bias_level(struct snd_soc_card *card,
210 struct snd_soc_dapm_context *dapm,
211 enum snd_soc_bias_level level)
213 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
214 struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
215 struct codec_priv *codec_priv = &priv->codec_priv;
216 struct device *dev = card->dev;
217 unsigned int pll_out;
218 int ret;
220 if (dapm->dev != codec_dai->dev)
221 return 0;
223 switch (level) {
224 case SND_SOC_BIAS_PREPARE:
225 if (dapm->bias_level != SND_SOC_BIAS_STANDBY)
226 break;
228 if (priv->sample_format == SNDRV_PCM_FORMAT_S24_LE)
229 pll_out = priv->sample_rate * 384;
230 else
231 pll_out = priv->sample_rate * 256;
233 ret = snd_soc_dai_set_pll(codec_dai, codec_priv->pll_id,
234 codec_priv->mclk_id,
235 codec_priv->mclk_freq, pll_out);
236 if (ret) {
237 dev_err(dev, "failed to start FLL: %d\n", ret);
238 return ret;
241 ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->fll_id,
242 pll_out, SND_SOC_CLOCK_IN);
243 if (ret) {
244 dev_err(dev, "failed to set SYSCLK: %d\n", ret);
245 return ret;
247 break;
249 case SND_SOC_BIAS_STANDBY:
250 if (dapm->bias_level != SND_SOC_BIAS_PREPARE)
251 break;
253 ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
254 codec_priv->mclk_freq,
255 SND_SOC_CLOCK_IN);
256 if (ret) {
257 dev_err(dev, "failed to switch away from FLL: %d\n", ret);
258 return ret;
261 ret = snd_soc_dai_set_pll(codec_dai, codec_priv->pll_id, 0, 0, 0);
262 if (ret) {
263 dev_err(dev, "failed to stop FLL: %d\n", ret);
264 return ret;
266 break;
268 default:
269 break;
272 return 0;
275 static int fsl_asoc_card_audmux_init(struct device_node *np,
276 struct fsl_asoc_card_priv *priv)
278 struct device *dev = &priv->pdev->dev;
279 u32 int_ptcr = 0, ext_ptcr = 0;
280 int int_port, ext_port;
281 int ret;
283 ret = of_property_read_u32(np, "mux-int-port", &int_port);
284 if (ret) {
285 dev_err(dev, "mux-int-port missing or invalid\n");
286 return ret;
288 ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
289 if (ret) {
290 dev_err(dev, "mux-ext-port missing or invalid\n");
291 return ret;
295 * The port numbering in the hardware manual starts at 1, while
296 * the AUDMUX API expects it starts at 0.
298 int_port--;
299 ext_port--;
302 * Use asynchronous mode (6 wires) for all cases.
303 * If only 4 wires are needed, just set SSI into
304 * synchronous mode and enable 4 PADs in IOMUX.
306 switch (priv->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
307 case SND_SOC_DAIFMT_CBM_CFM:
308 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
309 IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
310 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
311 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
312 IMX_AUDMUX_V2_PTCR_RFSDIR |
313 IMX_AUDMUX_V2_PTCR_RCLKDIR |
314 IMX_AUDMUX_V2_PTCR_TFSDIR |
315 IMX_AUDMUX_V2_PTCR_TCLKDIR;
316 break;
317 case SND_SOC_DAIFMT_CBM_CFS:
318 int_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
319 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
320 IMX_AUDMUX_V2_PTCR_RCLKDIR |
321 IMX_AUDMUX_V2_PTCR_TCLKDIR;
322 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
323 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
324 IMX_AUDMUX_V2_PTCR_RFSDIR |
325 IMX_AUDMUX_V2_PTCR_TFSDIR;
326 break;
327 case SND_SOC_DAIFMT_CBS_CFM:
328 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
329 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
330 IMX_AUDMUX_V2_PTCR_RFSDIR |
331 IMX_AUDMUX_V2_PTCR_TFSDIR;
332 ext_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
333 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
334 IMX_AUDMUX_V2_PTCR_RCLKDIR |
335 IMX_AUDMUX_V2_PTCR_TCLKDIR;
336 break;
337 case SND_SOC_DAIFMT_CBS_CFS:
338 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
339 IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
340 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
341 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
342 IMX_AUDMUX_V2_PTCR_RFSDIR |
343 IMX_AUDMUX_V2_PTCR_RCLKDIR |
344 IMX_AUDMUX_V2_PTCR_TFSDIR |
345 IMX_AUDMUX_V2_PTCR_TCLKDIR;
346 break;
347 default:
348 return -EINVAL;
351 /* Asynchronous mode can not be set along with RCLKDIR */
352 ret = imx_audmux_v2_configure_port(int_port, 0,
353 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
354 if (ret) {
355 dev_err(dev, "audmux internal port setup failed\n");
356 return ret;
359 ret = imx_audmux_v2_configure_port(int_port, int_ptcr,
360 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
361 if (ret) {
362 dev_err(dev, "audmux internal port setup failed\n");
363 return ret;
366 ret = imx_audmux_v2_configure_port(ext_port, 0,
367 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
368 if (ret) {
369 dev_err(dev, "audmux external port setup failed\n");
370 return ret;
373 ret = imx_audmux_v2_configure_port(ext_port, ext_ptcr,
374 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
375 if (ret) {
376 dev_err(dev, "audmux external port setup failed\n");
377 return ret;
380 return 0;
383 static int fsl_asoc_card_late_probe(struct snd_soc_card *card)
385 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
386 struct snd_soc_dai *codec_dai = card->rtd[0].codec_dai;
387 struct codec_priv *codec_priv = &priv->codec_priv;
388 struct device *dev = card->dev;
389 int ret;
391 ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
392 codec_priv->mclk_freq, SND_SOC_CLOCK_IN);
393 if (ret) {
394 dev_err(dev, "failed to set sysclk in %s\n", __func__);
395 return ret;
398 return 0;
401 static int fsl_asoc_card_probe(struct platform_device *pdev)
403 struct device_node *cpu_np, *codec_np, *asrc_np;
404 struct device_node *np = pdev->dev.of_node;
405 struct platform_device *asrc_pdev = NULL;
406 struct platform_device *cpu_pdev;
407 struct fsl_asoc_card_priv *priv;
408 struct i2c_client *codec_dev;
409 struct clk *codec_clk;
410 u32 width;
411 int ret;
413 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
414 if (!priv)
415 return -ENOMEM;
417 cpu_np = of_parse_phandle(np, "audio-cpu", 0);
418 /* Give a chance to old DT binding */
419 if (!cpu_np)
420 cpu_np = of_parse_phandle(np, "ssi-controller", 0);
421 codec_np = of_parse_phandle(np, "audio-codec", 0);
422 if (!cpu_np || !codec_np) {
423 dev_err(&pdev->dev, "phandle missing or invalid\n");
424 ret = -EINVAL;
425 goto fail;
428 cpu_pdev = of_find_device_by_node(cpu_np);
429 if (!cpu_pdev) {
430 dev_err(&pdev->dev, "failed to find CPU DAI device\n");
431 ret = -EINVAL;
432 goto fail;
435 codec_dev = of_find_i2c_device_by_node(codec_np);
436 if (!codec_dev) {
437 dev_err(&pdev->dev, "failed to find codec platform device\n");
438 ret = -EINVAL;
439 goto fail;
442 asrc_np = of_parse_phandle(np, "audio-asrc", 0);
443 if (asrc_np)
444 asrc_pdev = of_find_device_by_node(asrc_np);
446 /* Get the MCLK rate only, and leave it controlled by CODEC drivers */
447 codec_clk = clk_get(&codec_dev->dev, NULL);
448 if (!IS_ERR(codec_clk)) {
449 priv->codec_priv.mclk_freq = clk_get_rate(codec_clk);
450 clk_put(codec_clk);
453 /* Default sample rate and format, will be updated in hw_params() */
454 priv->sample_rate = 44100;
455 priv->sample_format = SNDRV_PCM_FORMAT_S16_LE;
457 /* Assign a default DAI format, and allow each card to overwrite it */
458 priv->dai_fmt = DAI_FMT_BASE;
460 /* Diversify the card configurations */
461 if (of_device_is_compatible(np, "fsl,imx-audio-cs42888")) {
462 priv->card.set_bias_level = NULL;
463 priv->cpu_priv.sysclk_freq[TX] = priv->codec_priv.mclk_freq;
464 priv->cpu_priv.sysclk_freq[RX] = priv->codec_priv.mclk_freq;
465 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
466 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
467 priv->cpu_priv.slot_width = 32;
468 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
469 } else if (of_device_is_compatible(np, "fsl,imx-audio-sgtl5000")) {
470 priv->codec_priv.mclk_id = SGTL5000_SYSCLK;
471 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
472 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8962")) {
473 priv->card.set_bias_level = fsl_asoc_card_set_bias_level;
474 priv->codec_priv.mclk_id = WM8962_SYSCLK_MCLK;
475 priv->codec_priv.fll_id = WM8962_SYSCLK_FLL;
476 priv->codec_priv.pll_id = WM8962_FLL;
477 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
478 } else {
479 dev_err(&pdev->dev, "unknown Device Tree compatible\n");
480 return -EINVAL;
483 /* Common settings for corresponding Freescale CPU DAI driver */
484 if (strstr(cpu_np->name, "ssi")) {
485 /* Only SSI needs to configure AUDMUX */
486 ret = fsl_asoc_card_audmux_init(np, priv);
487 if (ret) {
488 dev_err(&pdev->dev, "failed to init audmux\n");
489 goto asrc_fail;
491 } else if (strstr(cpu_np->name, "esai")) {
492 priv->cpu_priv.sysclk_id[1] = ESAI_HCKT_EXTAL;
493 priv->cpu_priv.sysclk_id[0] = ESAI_HCKR_EXTAL;
494 } else if (strstr(cpu_np->name, "sai")) {
495 priv->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
496 priv->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
499 sprintf(priv->name, "%s-audio", codec_dev->name);
501 /* Initialize sound card */
502 priv->pdev = pdev;
503 priv->card.dev = &pdev->dev;
504 priv->card.name = priv->name;
505 priv->card.dai_link = priv->dai_link;
506 priv->card.dapm_routes = audio_map;
507 priv->card.late_probe = fsl_asoc_card_late_probe;
508 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map);
509 priv->card.dapm_widgets = fsl_asoc_card_dapm_widgets;
510 priv->card.num_dapm_widgets = ARRAY_SIZE(fsl_asoc_card_dapm_widgets);
512 memcpy(priv->dai_link, fsl_asoc_card_dai,
513 sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link));
515 /* Normal DAI Link */
516 priv->dai_link[0].cpu_of_node = cpu_np;
517 priv->dai_link[0].codec_of_node = codec_np;
518 priv->dai_link[0].codec_dai_name = codec_dev->name;
519 priv->dai_link[0].platform_of_node = cpu_np;
520 priv->dai_link[0].dai_fmt = priv->dai_fmt;
521 priv->card.num_links = 1;
523 if (asrc_pdev) {
524 /* DPCM DAI Links only if ASRC exsits */
525 priv->dai_link[1].cpu_of_node = asrc_np;
526 priv->dai_link[1].platform_of_node = asrc_np;
527 priv->dai_link[2].codec_dai_name = codec_dev->name;
528 priv->dai_link[2].codec_of_node = codec_np;
529 priv->dai_link[2].cpu_of_node = cpu_np;
530 priv->dai_link[2].dai_fmt = priv->dai_fmt;
531 priv->card.num_links = 3;
533 ret = of_property_read_u32(asrc_np, "fsl,asrc-rate",
534 &priv->asrc_rate);
535 if (ret) {
536 dev_err(&pdev->dev, "failed to get output rate\n");
537 ret = -EINVAL;
538 goto asrc_fail;
541 ret = of_property_read_u32(asrc_np, "fsl,asrc-width", &width);
542 if (ret) {
543 dev_err(&pdev->dev, "failed to get output rate\n");
544 ret = -EINVAL;
545 goto asrc_fail;
548 if (width == 24)
549 priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
550 else
551 priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
554 /* Finish card registering */
555 platform_set_drvdata(pdev, priv);
556 snd_soc_card_set_drvdata(&priv->card, priv);
558 ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
559 if (ret)
560 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
562 asrc_fail:
563 of_node_put(asrc_np);
564 fail:
565 of_node_put(codec_np);
566 of_node_put(cpu_np);
568 return ret;
571 static const struct of_device_id fsl_asoc_card_dt_ids[] = {
572 { .compatible = "fsl,imx-audio-cs42888", },
573 { .compatible = "fsl,imx-audio-sgtl5000", },
574 { .compatible = "fsl,imx-audio-wm8962", },
578 static struct platform_driver fsl_asoc_card_driver = {
579 .probe = fsl_asoc_card_probe,
580 .driver = {
581 .name = "fsl-asoc-card",
582 .pm = &snd_soc_pm_ops,
583 .of_match_table = fsl_asoc_card_dt_ids,
586 module_platform_driver(fsl_asoc_card_driver);
588 MODULE_DESCRIPTION("Freescale Generic ASoC Sound Card driver with ASRC");
589 MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
590 MODULE_ALIAS("platform:fsl-asoc-card");
591 MODULE_LICENSE("GPL");