blob: c63d89da51f1daccf291d406e3b6ba477283a1ad [file] [log] [blame]
Nicolin Chen708b4352014-07-30 19:27:38 +08001/*
2 * Freescale Generic ASoC Sound Card driver with ASRC
3 *
4 * Copyright (C) 2014 Freescale Semiconductor, Inc.
5 *
6 * Author: Nicolin Chen <nicoleotsuka@gmail.com>
7 *
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.
11 */
12
13#include <linux/clk.h>
14#include <linux/i2c.h>
15#include <linux/module.h>
16#include <linux/of_platform.h>
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +020017#if IS_ENABLED(CONFIG_SND_AC97_CODEC)
18#include <sound/ac97_codec.h>
19#endif
Nicolin Chen708b4352014-07-30 19:27:38 +080020#include <sound/pcm_params.h>
21#include <sound/soc.h>
22
23#include "fsl_esai.h"
24#include "fsl_sai.h"
25#include "imx-audmux.h"
26
27#include "../codecs/sgtl5000.h"
28#include "../codecs/wm8962.h"
Zidan Wang50e0ee02015-08-14 19:11:09 +080029#include "../codecs/wm8960.h"
Nicolin Chen708b4352014-07-30 19:27:38 +080030
31#define RX 0
32#define TX 1
33
34/* Default DAI format without Master and Slave flag */
35#define DAI_FMT_BASE (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF)
36
37/**
38 * CODEC private data
39 *
40 * @mclk_freq: Clock rate of MCLK
41 * @mclk_id: MCLK (or main clock) id for set_sysclk()
42 * @fll_id: FLL (or secordary clock) id for set_sysclk()
43 * @pll_id: PLL id for set_pll()
44 */
45struct codec_priv {
46 unsigned long mclk_freq;
47 u32 mclk_id;
48 u32 fll_id;
49 u32 pll_id;
50};
51
52/**
53 * CPU private data
54 *
55 * @sysclk_freq[2]: SYSCLK rates for set_sysclk()
56 * @sysclk_dir[2]: SYSCLK directions for set_sysclk()
57 * @sysclk_id[2]: SYSCLK ids for set_sysclk()
Nicolin Chencb3fc1f2014-10-24 16:48:13 -070058 * @slot_width: Slot width of each frame
Nicolin Chen708b4352014-07-30 19:27:38 +080059 *
60 * Note: [1] for tx and [0] for rx
61 */
62struct cpu_priv {
63 unsigned long sysclk_freq[2];
64 u32 sysclk_dir[2];
65 u32 sysclk_id[2];
Nicolin Chencb3fc1f2014-10-24 16:48:13 -070066 u32 slot_width;
Nicolin Chen708b4352014-07-30 19:27:38 +080067};
68
69/**
70 * Freescale Generic ASOC card private data
71 *
72 * @dai_link[3]: DAI link structure including normal one and DPCM link
73 * @pdev: platform device pointer
74 * @codec_priv: CODEC private data
75 * @cpu_priv: CPU private data
76 * @card: ASoC card structure
77 * @sample_rate: Current sample rate
78 * @sample_format: Current sample format
79 * @asrc_rate: ASRC sample rate used by Back-Ends
80 * @asrc_format: ASRC sample format used by Back-Ends
81 * @dai_fmt: DAI format between CPU and CODEC
82 * @name: Card name
83 */
84
85struct fsl_asoc_card_priv {
86 struct snd_soc_dai_link dai_link[3];
87 struct platform_device *pdev;
88 struct codec_priv codec_priv;
89 struct cpu_priv cpu_priv;
90 struct snd_soc_card card;
91 u32 sample_rate;
92 u32 sample_format;
93 u32 asrc_rate;
94 u32 asrc_format;
95 u32 dai_fmt;
96 char name[32];
97};
98
99/**
100 * This dapm route map exsits for DPCM link only.
101 * The other routes shall go through Device Tree.
102 */
103static const struct snd_soc_dapm_route audio_map[] = {
104 {"CPU-Playback", NULL, "ASRC-Playback"},
105 {"Playback", NULL, "CPU-Playback"},
106 {"ASRC-Capture", NULL, "CPU-Capture"},
107 {"CPU-Capture", NULL, "Capture"},
108};
109
110/* Add all possible widgets into here without being redundant */
111static const struct snd_soc_dapm_widget fsl_asoc_card_dapm_widgets[] = {
112 SND_SOC_DAPM_LINE("Line Out Jack", NULL),
113 SND_SOC_DAPM_LINE("Line In Jack", NULL),
114 SND_SOC_DAPM_HP("Headphone Jack", NULL),
115 SND_SOC_DAPM_SPK("Ext Spk", NULL),
116 SND_SOC_DAPM_MIC("Mic Jack", NULL),
117 SND_SOC_DAPM_MIC("AMIC", NULL),
118 SND_SOC_DAPM_MIC("DMIC", NULL),
119};
120
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200121static bool fsl_asoc_card_is_ac97(struct fsl_asoc_card_priv *priv)
122{
123 return priv->dai_fmt == SND_SOC_DAIFMT_AC97;
124}
125
Nicolin Chen708b4352014-07-30 19:27:38 +0800126static int fsl_asoc_card_hw_params(struct snd_pcm_substream *substream,
127 struct snd_pcm_hw_params *params)
128{
129 struct snd_soc_pcm_runtime *rtd = substream->private_data;
130 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
131 bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
132 struct cpu_priv *cpu_priv = &priv->cpu_priv;
133 struct device *dev = rtd->card->dev;
134 int ret;
135
136 priv->sample_rate = params_rate(params);
137 priv->sample_format = params_format(params);
138
Nicolin Chen41282922014-10-24 16:48:11 -0700139 /*
140 * If codec-dai is DAI Master and all configurations are already in the
141 * set_bias_level(), bypass the remaining settings in hw_params().
142 * Note: (dai_fmt & CBM_CFM) includes CBM_CFM and CBM_CFS.
143 */
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200144 if ((priv->card.set_bias_level &&
145 priv->dai_fmt & SND_SOC_DAIFMT_CBM_CFM) ||
146 fsl_asoc_card_is_ac97(priv))
Nicolin Chen708b4352014-07-30 19:27:38 +0800147 return 0;
148
149 /* Specific configurations of DAIs starts from here */
150 ret = snd_soc_dai_set_sysclk(rtd->cpu_dai, cpu_priv->sysclk_id[tx],
151 cpu_priv->sysclk_freq[tx],
152 cpu_priv->sysclk_dir[tx]);
153 if (ret) {
154 dev_err(dev, "failed to set sysclk for cpu dai\n");
155 return ret;
156 }
157
Nicolin Chencb3fc1f2014-10-24 16:48:13 -0700158 if (cpu_priv->slot_width) {
159 ret = snd_soc_dai_set_tdm_slot(rtd->cpu_dai, 0x3, 0x3, 2,
160 cpu_priv->slot_width);
161 if (ret) {
162 dev_err(dev, "failed to set TDM slot for cpu dai\n");
163 return ret;
164 }
165 }
166
Nicolin Chen708b4352014-07-30 19:27:38 +0800167 return 0;
168}
169
170static struct snd_soc_ops fsl_asoc_card_ops = {
171 .hw_params = fsl_asoc_card_hw_params,
172};
173
174static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
175 struct snd_pcm_hw_params *params)
176{
177 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
178 struct snd_interval *rate;
179 struct snd_mask *mask;
180
181 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
182 rate->max = rate->min = priv->asrc_rate;
183
184 mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
185 snd_mask_none(mask);
186 snd_mask_set(mask, priv->asrc_format);
187
188 return 0;
189}
190
191static struct snd_soc_dai_link fsl_asoc_card_dai[] = {
192 /* Default ASoC DAI Link*/
193 {
194 .name = "HiFi",
195 .stream_name = "HiFi",
196 .ops = &fsl_asoc_card_ops,
197 },
198 /* DPCM Link between Front-End and Back-End (Optional) */
199 {
200 .name = "HiFi-ASRC-FE",
201 .stream_name = "HiFi-ASRC-FE",
202 .codec_name = "snd-soc-dummy",
203 .codec_dai_name = "snd-soc-dummy-dai",
204 .dpcm_playback = 1,
205 .dpcm_capture = 1,
206 .dynamic = 1,
207 },
208 {
209 .name = "HiFi-ASRC-BE",
210 .stream_name = "HiFi-ASRC-BE",
211 .platform_name = "snd-soc-dummy",
212 .be_hw_params_fixup = be_hw_params_fixup,
213 .ops = &fsl_asoc_card_ops,
214 .dpcm_playback = 1,
215 .dpcm_capture = 1,
216 .no_pcm = 1,
217 },
218};
219
220static int fsl_asoc_card_set_bias_level(struct snd_soc_card *card,
221 struct snd_soc_dapm_context *dapm,
222 enum snd_soc_bias_level level)
223{
224 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
Mengdong Lin50159202015-11-18 02:34:01 -0500225 struct snd_soc_pcm_runtime *rtd;
226 struct snd_soc_dai *codec_dai;
Nicolin Chen708b4352014-07-30 19:27:38 +0800227 struct codec_priv *codec_priv = &priv->codec_priv;
228 struct device *dev = card->dev;
229 unsigned int pll_out;
230 int ret;
231
Mengdong Lin50159202015-11-18 02:34:01 -0500232 rtd = snd_soc_get_pcm_runtime(card, card->dai_link[0].name);
233 codec_dai = rtd->codec_dai;
Nicolin Chen708b4352014-07-30 19:27:38 +0800234 if (dapm->dev != codec_dai->dev)
235 return 0;
236
237 switch (level) {
238 case SND_SOC_BIAS_PREPARE:
239 if (dapm->bias_level != SND_SOC_BIAS_STANDBY)
240 break;
241
242 if (priv->sample_format == SNDRV_PCM_FORMAT_S24_LE)
243 pll_out = priv->sample_rate * 384;
244 else
245 pll_out = priv->sample_rate * 256;
246
247 ret = snd_soc_dai_set_pll(codec_dai, codec_priv->pll_id,
248 codec_priv->mclk_id,
249 codec_priv->mclk_freq, pll_out);
250 if (ret) {
251 dev_err(dev, "failed to start FLL: %d\n", ret);
252 return ret;
253 }
254
255 ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->fll_id,
256 pll_out, SND_SOC_CLOCK_IN);
257 if (ret) {
258 dev_err(dev, "failed to set SYSCLK: %d\n", ret);
259 return ret;
260 }
261 break;
262
263 case SND_SOC_BIAS_STANDBY:
264 if (dapm->bias_level != SND_SOC_BIAS_PREPARE)
265 break;
266
267 ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
268 codec_priv->mclk_freq,
269 SND_SOC_CLOCK_IN);
270 if (ret) {
271 dev_err(dev, "failed to switch away from FLL: %d\n", ret);
272 return ret;
273 }
274
275 ret = snd_soc_dai_set_pll(codec_dai, codec_priv->pll_id, 0, 0, 0);
276 if (ret) {
277 dev_err(dev, "failed to stop FLL: %d\n", ret);
278 return ret;
279 }
280 break;
281
282 default:
283 break;
284 }
285
286 return 0;
287}
288
289static int fsl_asoc_card_audmux_init(struct device_node *np,
290 struct fsl_asoc_card_priv *priv)
291{
292 struct device *dev = &priv->pdev->dev;
293 u32 int_ptcr = 0, ext_ptcr = 0;
294 int int_port, ext_port;
295 int ret;
296
297 ret = of_property_read_u32(np, "mux-int-port", &int_port);
298 if (ret) {
299 dev_err(dev, "mux-int-port missing or invalid\n");
300 return ret;
301 }
302 ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
303 if (ret) {
304 dev_err(dev, "mux-ext-port missing or invalid\n");
305 return ret;
306 }
307
308 /*
309 * The port numbering in the hardware manual starts at 1, while
310 * the AUDMUX API expects it starts at 0.
311 */
312 int_port--;
313 ext_port--;
314
315 /*
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200316 * Use asynchronous mode (6 wires) for all cases except AC97.
Nicolin Chen708b4352014-07-30 19:27:38 +0800317 * If only 4 wires are needed, just set SSI into
318 * synchronous mode and enable 4 PADs in IOMUX.
319 */
320 switch (priv->dai_fmt & SND_SOC_DAIFMT_MASTER_MASK) {
321 case SND_SOC_DAIFMT_CBM_CFM:
322 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
323 IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
324 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
325 IMX_AUDMUX_V2_PTCR_TCSEL(ext_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;
330 break;
331 case SND_SOC_DAIFMT_CBM_CFS:
332 int_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
333 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
334 IMX_AUDMUX_V2_PTCR_RCLKDIR |
335 IMX_AUDMUX_V2_PTCR_TCLKDIR;
336 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
337 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
338 IMX_AUDMUX_V2_PTCR_RFSDIR |
339 IMX_AUDMUX_V2_PTCR_TFSDIR;
340 break;
341 case SND_SOC_DAIFMT_CBS_CFM:
342 int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
343 IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
344 IMX_AUDMUX_V2_PTCR_RFSDIR |
345 IMX_AUDMUX_V2_PTCR_TFSDIR;
346 ext_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
347 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
348 IMX_AUDMUX_V2_PTCR_RCLKDIR |
349 IMX_AUDMUX_V2_PTCR_TCLKDIR;
350 break;
351 case SND_SOC_DAIFMT_CBS_CFS:
352 ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
353 IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
354 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
355 IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
356 IMX_AUDMUX_V2_PTCR_RFSDIR |
357 IMX_AUDMUX_V2_PTCR_RCLKDIR |
358 IMX_AUDMUX_V2_PTCR_TFSDIR |
359 IMX_AUDMUX_V2_PTCR_TCLKDIR;
360 break;
361 default:
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200362 if (!fsl_asoc_card_is_ac97(priv))
363 return -EINVAL;
364 }
365
366 if (fsl_asoc_card_is_ac97(priv)) {
367 int_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
368 IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
369 IMX_AUDMUX_V2_PTCR_TCLKDIR;
370 ext_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
371 IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
372 IMX_AUDMUX_V2_PTCR_TFSDIR;
Nicolin Chen708b4352014-07-30 19:27:38 +0800373 }
374
375 /* Asynchronous mode can not be set along with RCLKDIR */
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200376 if (!fsl_asoc_card_is_ac97(priv)) {
377 unsigned int pdcr =
378 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port);
379
380 ret = imx_audmux_v2_configure_port(int_port, 0,
381 pdcr);
382 if (ret) {
383 dev_err(dev, "audmux internal port setup failed\n");
384 return ret;
385 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800386 }
387
388 ret = imx_audmux_v2_configure_port(int_port, int_ptcr,
389 IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
390 if (ret) {
391 dev_err(dev, "audmux internal port setup failed\n");
392 return ret;
393 }
394
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200395 if (!fsl_asoc_card_is_ac97(priv)) {
396 unsigned int pdcr =
397 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port);
398
399 ret = imx_audmux_v2_configure_port(ext_port, 0,
400 pdcr);
401 if (ret) {
402 dev_err(dev, "audmux external port setup failed\n");
403 return ret;
404 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800405 }
406
407 ret = imx_audmux_v2_configure_port(ext_port, ext_ptcr,
408 IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
409 if (ret) {
410 dev_err(dev, "audmux external port setup failed\n");
411 return ret;
412 }
413
414 return 0;
415}
416
417static int fsl_asoc_card_late_probe(struct snd_soc_card *card)
418{
419 struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
Vinod Koulabd657b2015-11-20 22:45:20 +0530420 struct snd_soc_pcm_runtime *rtd = list_first_entry(
421 &card->rtd_list, struct snd_soc_pcm_runtime, list);
422 struct snd_soc_dai *codec_dai = rtd->codec_dai;
Nicolin Chen708b4352014-07-30 19:27:38 +0800423 struct codec_priv *codec_priv = &priv->codec_priv;
424 struct device *dev = card->dev;
425 int ret;
426
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200427 if (fsl_asoc_card_is_ac97(priv)) {
428#if IS_ENABLED(CONFIG_SND_AC97_CODEC)
Vinod Koulabd657b2015-11-20 22:45:20 +0530429 struct snd_soc_codec *codec = rtd->codec;
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200430 struct snd_ac97 *ac97 = snd_soc_codec_get_drvdata(codec);
431
432 /*
433 * Use slots 3/4 for S/PDIF so SSI won't try to enable
434 * other slots and send some samples there
435 * due to SLOTREQ bits for S/PDIF received from codec
436 */
437 snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
438 AC97_EA_SPSA_SLOT_MASK, AC97_EA_SPSA_3_4);
439#endif
440
441 return 0;
442 }
443
Nicolin Chen708b4352014-07-30 19:27:38 +0800444 ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
445 codec_priv->mclk_freq, SND_SOC_CLOCK_IN);
446 if (ret) {
447 dev_err(dev, "failed to set sysclk in %s\n", __func__);
448 return ret;
449 }
450
451 return 0;
452}
453
454static int fsl_asoc_card_probe(struct platform_device *pdev)
455{
456 struct device_node *cpu_np, *codec_np, *asrc_np;
457 struct device_node *np = pdev->dev.of_node;
458 struct platform_device *asrc_pdev = NULL;
459 struct platform_device *cpu_pdev;
460 struct fsl_asoc_card_priv *priv;
461 struct i2c_client *codec_dev;
Nicolin Chen114bb132015-08-12 13:06:12 -0700462 const char *codec_dai_name;
Nicolin Chen708b4352014-07-30 19:27:38 +0800463 u32 width;
464 int ret;
465
466 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
467 if (!priv)
468 return -ENOMEM;
469
470 cpu_np = of_parse_phandle(np, "audio-cpu", 0);
471 /* Give a chance to old DT binding */
472 if (!cpu_np)
473 cpu_np = of_parse_phandle(np, "ssi-controller", 0);
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200474 if (!cpu_np) {
475 dev_err(&pdev->dev, "CPU phandle missing or invalid\n");
Nicolin Chen708b4352014-07-30 19:27:38 +0800476 ret = -EINVAL;
477 goto fail;
478 }
479
480 cpu_pdev = of_find_device_by_node(cpu_np);
481 if (!cpu_pdev) {
482 dev_err(&pdev->dev, "failed to find CPU DAI device\n");
483 ret = -EINVAL;
484 goto fail;
485 }
486
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200487 codec_np = of_parse_phandle(np, "audio-codec", 0);
488 if (codec_np)
489 codec_dev = of_find_i2c_device_by_node(codec_np);
490 else
491 codec_dev = NULL;
Nicolin Chen708b4352014-07-30 19:27:38 +0800492
493 asrc_np = of_parse_phandle(np, "audio-asrc", 0);
494 if (asrc_np)
495 asrc_pdev = of_find_device_by_node(asrc_np);
496
497 /* Get the MCLK rate only, and leave it controlled by CODEC drivers */
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200498 if (codec_dev) {
499 struct clk *codec_clk = clk_get(&codec_dev->dev, NULL);
500
501 if (!IS_ERR(codec_clk)) {
502 priv->codec_priv.mclk_freq = clk_get_rate(codec_clk);
503 clk_put(codec_clk);
504 }
Nicolin Chen708b4352014-07-30 19:27:38 +0800505 }
506
507 /* Default sample rate and format, will be updated in hw_params() */
508 priv->sample_rate = 44100;
509 priv->sample_format = SNDRV_PCM_FORMAT_S16_LE;
510
511 /* Assign a default DAI format, and allow each card to overwrite it */
512 priv->dai_fmt = DAI_FMT_BASE;
513
514 /* Diversify the card configurations */
515 if (of_device_is_compatible(np, "fsl,imx-audio-cs42888")) {
Nicolin Chen114bb132015-08-12 13:06:12 -0700516 codec_dai_name = "cs42888";
Nicolin Chen708b4352014-07-30 19:27:38 +0800517 priv->card.set_bias_level = NULL;
518 priv->cpu_priv.sysclk_freq[TX] = priv->codec_priv.mclk_freq;
519 priv->cpu_priv.sysclk_freq[RX] = priv->codec_priv.mclk_freq;
520 priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
521 priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
Nicolin Chencb3fc1f2014-10-24 16:48:13 -0700522 priv->cpu_priv.slot_width = 32;
Nicolin Chen708b4352014-07-30 19:27:38 +0800523 priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
524 } else if (of_device_is_compatible(np, "fsl,imx-audio-sgtl5000")) {
Nicolin Chen114bb132015-08-12 13:06:12 -0700525 codec_dai_name = "sgtl5000";
Nicolin Chen708b4352014-07-30 19:27:38 +0800526 priv->codec_priv.mclk_id = SGTL5000_SYSCLK;
527 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
528 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8962")) {
Nicolin Chen114bb132015-08-12 13:06:12 -0700529 codec_dai_name = "wm8962";
Nicolin Chen708b4352014-07-30 19:27:38 +0800530 priv->card.set_bias_level = fsl_asoc_card_set_bias_level;
531 priv->codec_priv.mclk_id = WM8962_SYSCLK_MCLK;
532 priv->codec_priv.fll_id = WM8962_SYSCLK_FLL;
533 priv->codec_priv.pll_id = WM8962_FLL;
534 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Zidan Wang50e0ee02015-08-14 19:11:09 +0800535 } else if (of_device_is_compatible(np, "fsl,imx-audio-wm8960")) {
536 codec_dai_name = "wm8960-hifi";
537 priv->card.set_bias_level = fsl_asoc_card_set_bias_level;
538 priv->codec_priv.fll_id = WM8960_SYSCLK_AUTO;
539 priv->codec_priv.pll_id = WM8960_SYSCLK_AUTO;
540 priv->dai_fmt |= SND_SOC_DAIFMT_CBM_CFM;
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200541 } else if (of_device_is_compatible(np, "fsl,imx-audio-ac97")) {
542 codec_dai_name = "ac97-hifi";
543 priv->card.set_bias_level = NULL;
544 priv->dai_fmt = SND_SOC_DAIFMT_AC97;
Nicolin Chen708b4352014-07-30 19:27:38 +0800545 } else {
546 dev_err(&pdev->dev, "unknown Device Tree compatible\n");
Maciej S. Szmigiero6bd3c6f2015-08-31 17:07:12 +0200547 ret = -EINVAL;
548 goto asrc_fail;
Nicolin Chen708b4352014-07-30 19:27:38 +0800549 }
550
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200551 if (!fsl_asoc_card_is_ac97(priv) && !codec_dev) {
552 dev_err(&pdev->dev, "failed to find codec device\n");
553 ret = -EINVAL;
554 goto asrc_fail;
Nicolin Chen708b4352014-07-30 19:27:38 +0800555 }
556
557 /* Common settings for corresponding Freescale CPU DAI driver */
558 if (strstr(cpu_np->name, "ssi")) {
559 /* Only SSI needs to configure AUDMUX */
560 ret = fsl_asoc_card_audmux_init(np, priv);
561 if (ret) {
562 dev_err(&pdev->dev, "failed to init audmux\n");
Shengjiu Wang5f376712014-08-18 16:38:39 +0800563 goto asrc_fail;
Nicolin Chen708b4352014-07-30 19:27:38 +0800564 }
565 } else if (strstr(cpu_np->name, "esai")) {
566 priv->cpu_priv.sysclk_id[1] = ESAI_HCKT_EXTAL;
567 priv->cpu_priv.sysclk_id[0] = ESAI_HCKR_EXTAL;
568 } else if (strstr(cpu_np->name, "sai")) {
569 priv->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
570 priv->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
571 }
572
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200573 snprintf(priv->name, sizeof(priv->name), "%s-audio",
574 fsl_asoc_card_is_ac97(priv) ? "ac97" :
575 codec_dev->name);
Nicolin Chen708b4352014-07-30 19:27:38 +0800576
577 /* Initialize sound card */
578 priv->pdev = pdev;
579 priv->card.dev = &pdev->dev;
580 priv->card.name = priv->name;
581 priv->card.dai_link = priv->dai_link;
582 priv->card.dapm_routes = audio_map;
583 priv->card.late_probe = fsl_asoc_card_late_probe;
584 priv->card.num_dapm_routes = ARRAY_SIZE(audio_map);
585 priv->card.dapm_widgets = fsl_asoc_card_dapm_widgets;
586 priv->card.num_dapm_widgets = ARRAY_SIZE(fsl_asoc_card_dapm_widgets);
587
588 memcpy(priv->dai_link, fsl_asoc_card_dai,
589 sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link));
590
Nicolin Chen31858782015-02-14 17:22:50 -0800591 ret = snd_soc_of_parse_audio_routing(&priv->card, "audio-routing");
592 if (ret) {
593 dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
594 goto asrc_fail;
595 }
596
Nicolin Chen708b4352014-07-30 19:27:38 +0800597 /* Normal DAI Link */
598 priv->dai_link[0].cpu_of_node = cpu_np;
Nicolin Chen114bb132015-08-12 13:06:12 -0700599 priv->dai_link[0].codec_dai_name = codec_dai_name;
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200600
601 if (!fsl_asoc_card_is_ac97(priv))
602 priv->dai_link[0].codec_of_node = codec_np;
603 else {
604 u32 idx;
605
606 ret = of_property_read_u32(cpu_np, "cell-index", &idx);
607 if (ret) {
608 dev_err(&pdev->dev,
609 "cannot get CPU index property\n");
610 goto asrc_fail;
611 }
612
613 priv->dai_link[0].codec_name =
614 devm_kasprintf(&pdev->dev, GFP_KERNEL,
615 "ac97-codec.%u",
616 (unsigned int)idx);
617 }
618
Nicolin Chen708b4352014-07-30 19:27:38 +0800619 priv->dai_link[0].platform_of_node = cpu_np;
620 priv->dai_link[0].dai_fmt = priv->dai_fmt;
621 priv->card.num_links = 1;
622
623 if (asrc_pdev) {
624 /* DPCM DAI Links only if ASRC exsits */
625 priv->dai_link[1].cpu_of_node = asrc_np;
626 priv->dai_link[1].platform_of_node = asrc_np;
Nicolin Chen114bb132015-08-12 13:06:12 -0700627 priv->dai_link[2].codec_dai_name = codec_dai_name;
Nicolin Chen708b4352014-07-30 19:27:38 +0800628 priv->dai_link[2].codec_of_node = codec_np;
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200629 priv->dai_link[2].codec_name =
630 priv->dai_link[0].codec_name;
Nicolin Chen708b4352014-07-30 19:27:38 +0800631 priv->dai_link[2].cpu_of_node = cpu_np;
632 priv->dai_link[2].dai_fmt = priv->dai_fmt;
633 priv->card.num_links = 3;
634
635 ret = of_property_read_u32(asrc_np, "fsl,asrc-rate",
636 &priv->asrc_rate);
637 if (ret) {
638 dev_err(&pdev->dev, "failed to get output rate\n");
639 ret = -EINVAL;
Shengjiu Wang5f376712014-08-18 16:38:39 +0800640 goto asrc_fail;
Nicolin Chen708b4352014-07-30 19:27:38 +0800641 }
642
643 ret = of_property_read_u32(asrc_np, "fsl,asrc-width", &width);
644 if (ret) {
645 dev_err(&pdev->dev, "failed to get output rate\n");
646 ret = -EINVAL;
Shengjiu Wang5f376712014-08-18 16:38:39 +0800647 goto asrc_fail;
Nicolin Chen708b4352014-07-30 19:27:38 +0800648 }
649
650 if (width == 24)
651 priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
652 else
653 priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
654 }
655
656 /* Finish card registering */
657 platform_set_drvdata(pdev, priv);
658 snd_soc_card_set_drvdata(&priv->card, priv);
659
660 ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
661 if (ret)
662 dev_err(&pdev->dev, "snd_soc_register_card failed (%d)\n", ret);
663
Shengjiu Wang5f376712014-08-18 16:38:39 +0800664asrc_fail:
665 of_node_put(asrc_np);
Nicolin Chen708b4352014-07-30 19:27:38 +0800666 of_node_put(codec_np);
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200667fail:
Nicolin Chen708b4352014-07-30 19:27:38 +0800668 of_node_put(cpu_np);
669
670 return ret;
671}
672
673static const struct of_device_id fsl_asoc_card_dt_ids[] = {
Maciej S. Szmigiero50760ca2015-09-19 02:00:25 +0200674 { .compatible = "fsl,imx-audio-ac97", },
Nicolin Chen708b4352014-07-30 19:27:38 +0800675 { .compatible = "fsl,imx-audio-cs42888", },
676 { .compatible = "fsl,imx-audio-sgtl5000", },
677 { .compatible = "fsl,imx-audio-wm8962", },
Zidan Wang50e0ee02015-08-14 19:11:09 +0800678 { .compatible = "fsl,imx-audio-wm8960", },
Nicolin Chen708b4352014-07-30 19:27:38 +0800679 {}
680};
Luis de Bethencourt5226f232015-09-03 12:57:47 +0200681MODULE_DEVICE_TABLE(of, fsl_asoc_card_dt_ids);
Nicolin Chen708b4352014-07-30 19:27:38 +0800682
683static struct platform_driver fsl_asoc_card_driver = {
684 .probe = fsl_asoc_card_probe,
685 .driver = {
686 .name = "fsl-asoc-card",
687 .pm = &snd_soc_pm_ops,
688 .of_match_table = fsl_asoc_card_dt_ids,
689 },
690};
691module_platform_driver(fsl_asoc_card_driver);
692
693MODULE_DESCRIPTION("Freescale Generic ASoC Sound Card driver with ASRC");
694MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
695MODULE_ALIAS("platform:fsl-asoc-card");
696MODULE_LICENSE("GPL");