xref: /linux/sound/soc/fsl/fsl-asoc-card.c (revision 99b5316f08f390a5e1ea2239ecca8e2b4ef70ac6)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Freescale Generic ASoC Sound Card driver with ASRC
4 //
5 // Copyright (C) 2014 Freescale Semiconductor, Inc.
6 //
7 // Author: Nicolin Chen <nicoleotsuka@gmail.com>
8 
9 #include <linux/clk.h>
10 #include <linux/i2c.h>
11 #include <linux/module.h>
12 #include <linux/of_platform.h>
13 #if IS_ENABLED(CONFIG_SND_AC97_CODEC)
14 #include <sound/ac97_codec.h>
15 #endif
16 #include <sound/pcm_params.h>
17 #include <sound/soc.h>
18 #include <sound/jack.h>
19 #include <sound/simple_card_utils.h>
20 
21 #include "fsl_esai.h"
22 #include "fsl_sai.h"
23 #include "imx-audmux.h"
24 
25 #include "../codecs/sgtl5000.h"
26 #include "../codecs/wm8962.h"
27 #include "../codecs/wm8960.h"
28 #include "../codecs/wm8994.h"
29 #include "../codecs/tlv320aic31xx.h"
30 #include "../codecs/nau8822.h"
31 #include "../codecs/wm8904.h"
32 
33 #define DRIVER_NAME "fsl-asoc-card"
34 
35 #define CS427x_SYSCLK_MCLK 0
36 
37 #define RX 0
38 #define TX 1
39 
40 /* Default DAI format without Master and Slave flag */
41 #define DAI_FMT_BASE (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF)
42 
43 static const u32 cs42888_rates_48k[] = {
44 	48000, 96000, 192000,
45 };
46 
47 static const u32 cs42888_rates_44k[] = {
48 	44100, 88200, 176400,
49 };
50 
51 static const u32 cs42888_channels[] = {
52 	1, 2, 4, 6, 8,
53 };
54 
55 static const struct snd_pcm_hw_constraint_list cs42888_rate_48k_constraints = {
56 	.list = cs42888_rates_48k,
57 	.count = ARRAY_SIZE(cs42888_rates_48k),
58 };
59 
60 static const struct snd_pcm_hw_constraint_list cs42888_rate_44k_constraints = {
61 	.list = cs42888_rates_44k,
62 	.count = ARRAY_SIZE(cs42888_rates_44k),
63 };
64 
65 static const struct snd_pcm_hw_constraint_list cs42888_channel_constraints = {
66 	.list = cs42888_channels,
67 	.count = ARRAY_SIZE(cs42888_channels),
68 };
69 
70 /**
71  * struct codec_priv - CODEC private data
72  * @mclk: Main clock of the CODEC
73  * @mclk_freq: Clock rate of MCLK
74  * @free_freq: Clock rate of MCLK for hw_free()
75  * @mclk_id: MCLK (or main clock) id for set_sysclk()
76  * @fll_id: FLL (or secordary clock) id for set_sysclk()
77  * @pll_id: PLL id for set_pll()
78  * @pll_ratio_s24: PLL output ratio for S24_LE format (PLL_freq = sample_rate × ratio)
79  *                 Default is 384, but some codecs (e.g., WM8904) require lower values
80  *                 to stay within PLL frequency limits
81  */
82 struct codec_priv {
83 	struct clk *mclk;
84 	unsigned long mclk_freq;
85 	unsigned long free_freq;
86 	u32 mclk_id;
87 	int fll_id;
88 	int pll_id;
89 	int pll_ratio_s24;
90 };
91 
92 /**
93  * struct cpu_priv - CPU private data
94  * @sysclk_freq: SYSCLK rates for set_sysclk()
95  * @sysclk_dir: SYSCLK directions for set_sysclk()
96  * @sysclk_id: SYSCLK ids for set_sysclk()
97  * @sysclk_ratio: SYSCLK ratio on sample rate
98  * @slot_width: Slot width of each frame
99  * @slot_num: Number of slots of each frame
100  *
101  * Note: [1] for tx and [0] for rx
102  */
103 struct cpu_priv {
104 	unsigned long sysclk_freq[2];
105 	u32 sysclk_dir[2];
106 	u32 sysclk_id[2];
107 	u32 sysclk_ratio[2];
108 	u32 slot_width;
109 	u32 slot_num;
110 };
111 
112 /**
113  * struct fsl_asoc_card_priv - Freescale Generic ASOC card private data
114  * @dai_link: DAI link structure including normal one and DPCM link
115  * @hp_jack: Headphone Jack structure
116  * @mic_jack: Microphone Jack structure
117  * @pdev: platform device pointer
118  * @codec_priv: CODEC private data
119  * @cpu_priv: CPU private data
120  * @card: ASoC card structure
121  * @constraint_rates: array of supported rates
122  * @constraint_channels: array of supported channels
123  * @streams: Mask of current active streams
124  * @sample_rate: Current sample rate
125  * @sample_format: Current sample format
126  * @asrc_rate: ASRC sample rate used by Back-Ends
127  * @asrc_format: ASRC sample format used by Back-Ends
128  * @dai_fmt: DAI format between CPU and CODEC
129  * @exclude_format: excluded format;
130  * @name: Card name
131  */
132 
133 struct fsl_asoc_card_priv {
134 	struct snd_soc_dai_link dai_link[3];
135 	struct simple_util_jack hp_jack;
136 	struct simple_util_jack mic_jack;
137 	struct platform_device *pdev;
138 	struct codec_priv codec_priv[2];
139 	struct cpu_priv cpu_priv;
140 	struct snd_soc_card card;
141 	const struct snd_pcm_hw_constraint_list *constraint_rates;
142 	const struct snd_pcm_hw_constraint_list *constraint_channels;
143 	u8 streams;
144 	u32 sample_rate;
145 	snd_pcm_format_t sample_format;
146 	u32 asrc_rate;
147 	snd_pcm_format_t asrc_format;
148 	u32 dai_fmt;
149 	u64 exclude_format;
150 	char name[32];
151 };
152 
153 /*
154  * This dapm route map exists for DPCM link only.
155  * The other routes shall go through Device Tree.
156  *
157  * Note: keep all ASRC routes in the second half
158  *	 to drop them easily for non-ASRC cases.
159  */
160 static const struct snd_soc_dapm_route audio_map[] = {
161 	/* 1st half -- Normal DAPM routes */
162 	{"Playback",  NULL, "CPU-Playback"},
163 	{"CPU-Capture",  NULL, "Capture"},
164 	/* 2nd half -- ASRC DAPM routes */
165 	{"CPU-Playback",  NULL, "ASRC-Playback"},
166 	{"ASRC-Capture",  NULL, "CPU-Capture"},
167 };
168 
169 static const struct snd_soc_dapm_route audio_map_ac97[] = {
170 	/* 1st half -- Normal DAPM routes */
171 	{"AC97 Playback",  NULL, "CPU AC97 Playback"},
172 	{"CPU AC97 Capture",  NULL, "AC97 Capture"},
173 	/* 2nd half -- ASRC DAPM routes */
174 	{"CPU AC97 Playback",  NULL, "ASRC-Playback"},
175 	{"ASRC-Capture",  NULL, "CPU AC97 Capture"},
176 };
177 
178 static const struct snd_soc_dapm_route audio_map_tx[] = {
179 	/* 1st half -- Normal DAPM routes */
180 	{"Playback",  NULL, "CPU-Playback"},
181 	/* 2nd half -- ASRC DAPM routes */
182 	{"CPU-Playback",  NULL, "ASRC-Playback"},
183 };
184 
185 static const struct snd_soc_dapm_route audio_map_rx[] = {
186 	/* 1st half -- Normal DAPM routes */
187 	{"CPU-Capture",  NULL, "Capture"},
188 	/* 2nd half -- ASRC DAPM routes */
189 	{"ASRC-Capture",  NULL, "CPU-Capture"},
190 };
191 
192 /* Add all possible widgets into here without being redundant */
193 static const struct snd_soc_dapm_widget fsl_asoc_card_dapm_widgets[] = {
194 	SND_SOC_DAPM_LINE("Line Out Jack", NULL),
195 	SND_SOC_DAPM_LINE("Line In Jack", NULL),
196 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
197 	SND_SOC_DAPM_SPK("Ext Spk", NULL),
198 	SND_SOC_DAPM_MIC("Mic Jack", NULL),
199 	SND_SOC_DAPM_MIC("AMIC", NULL),
200 	SND_SOC_DAPM_MIC("DMIC", NULL),
201 };
202 
203 static bool fsl_asoc_card_is_ac97(struct fsl_asoc_card_priv *priv)
204 {
205 	return priv->dai_fmt == SND_SOC_DAIFMT_AC97;
206 }
207 
208 static int fsl_asoc_card_hw_params(struct snd_pcm_substream *substream,
209 				   struct snd_pcm_hw_params *params)
210 {
211 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
212 	struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
213 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
214 	struct codec_priv *codec_priv;
215 	struct snd_soc_dai *codec_dai;
216 	struct cpu_priv *cpu_priv = &priv->cpu_priv;
217 	struct device *dev = rtd->card->dev;
218 	unsigned int pll_out, sysclk_freq;
219 	int codec_idx;
220 	int ret;
221 
222 	priv->sample_rate = params_rate(params);
223 	priv->sample_format = params_format(params);
224 	priv->streams |= BIT(substream->stream);
225 
226 	if (fsl_asoc_card_is_ac97(priv))
227 		return 0;
228 
229 	if (!cpu_priv->sysclk_freq[tx] && cpu_priv->sysclk_ratio[tx])
230 		sysclk_freq = priv->sample_rate * cpu_priv->sysclk_ratio[tx];
231 	else
232 		sysclk_freq = cpu_priv->sysclk_freq[tx];
233 
234 	/* Specific configurations of DAIs starts from here */
235 	ret = snd_soc_dai_set_sysclk(snd_soc_rtd_to_cpu(rtd, 0), cpu_priv->sysclk_id[tx],
236 				     sysclk_freq,
237 				     cpu_priv->sysclk_dir[tx]);
238 	if (ret && ret != -ENOTSUPP) {
239 		dev_err(dev, "failed to set sysclk for cpu dai\n");
240 		goto fail;
241 	}
242 
243 	if (cpu_priv->slot_width) {
244 		if (!cpu_priv->slot_num)
245 			cpu_priv->slot_num = 2;
246 
247 		ret = snd_soc_dai_set_tdm_slot(snd_soc_rtd_to_cpu(rtd, 0), 0x3, 0x3,
248 					       cpu_priv->slot_num,
249 					       cpu_priv->slot_width);
250 		if (ret && ret != -ENOTSUPP) {
251 			dev_err(dev, "failed to set TDM slot for cpu dai\n");
252 			goto fail;
253 		}
254 	}
255 
256 	/* Specific configuration for PLL */
257 	for_each_rtd_codec_dais(rtd, codec_idx, codec_dai) {
258 		codec_priv = &priv->codec_priv[codec_idx];
259 
260 		if (codec_priv->pll_id >= 0 && codec_priv->fll_id >= 0) {
261 			if (priv->sample_format == SNDRV_PCM_FORMAT_S24_LE)
262 				pll_out = priv->sample_rate * codec_priv->pll_ratio_s24;
263 			else
264 				pll_out = priv->sample_rate * 256;
265 
266 			ret = snd_soc_dai_set_pll(codec_dai,
267 						codec_priv->pll_id,
268 						codec_priv->mclk_id,
269 						codec_priv->mclk_freq, pll_out);
270 			if (ret) {
271 				dev_err(dev, "failed to start FLL: %d\n", ret);
272 				goto fail;
273 			}
274 
275 			ret = snd_soc_dai_set_sysclk(codec_dai,
276 						codec_priv->fll_id,
277 						pll_out, SND_SOC_CLOCK_IN);
278 
279 			if (ret && ret != -ENOTSUPP) {
280 				dev_err(dev, "failed to set SYSCLK: %d\n", ret);
281 				goto fail;
282 			}
283 		}
284 	}
285 
286 	return 0;
287 
288 fail:
289 	priv->streams &= ~BIT(substream->stream);
290 	return ret;
291 }
292 
293 static int fsl_asoc_card_hw_free(struct snd_pcm_substream *substream)
294 {
295 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
296 	struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
297 	struct codec_priv *codec_priv;
298 	struct snd_soc_dai *codec_dai;
299 	struct device *dev = rtd->card->dev;
300 	int codec_idx;
301 	int ret;
302 
303 	priv->streams &= ~BIT(substream->stream);
304 
305 	for_each_rtd_codec_dais(rtd, codec_idx, codec_dai) {
306 		codec_priv = &priv->codec_priv[codec_idx];
307 
308 		if (!priv->streams && codec_priv->pll_id >= 0 && codec_priv->fll_id >= 0) {
309 			/* Force freq to be free_freq to avoid error message in codec */
310 			ret = snd_soc_dai_set_sysclk(codec_dai,
311 						codec_priv->mclk_id,
312 						codec_priv->free_freq,
313 						SND_SOC_CLOCK_IN);
314 			if (ret) {
315 				dev_err(dev, "failed to switch away from FLL: %d\n", ret);
316 				return ret;
317 			}
318 
319 			ret = snd_soc_dai_set_pll(codec_dai,
320 						codec_priv->pll_id, 0, 0, 0);
321 			if (ret && ret != -ENOTSUPP) {
322 				dev_err(dev, "failed to stop FLL: %d\n", ret);
323 				return ret;
324 			}
325 		}
326 	}
327 
328 	return 0;
329 }
330 
331 static int fsl_asoc_card_startup(struct snd_pcm_substream *substream)
332 {
333 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
334 	struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
335 	struct snd_pcm_runtime *runtime = substream->runtime;
336 	int ret;
337 
338 	if (priv->exclude_format && !rtd->dai_link->no_pcm) {
339 		ret = snd_pcm_hw_constraint_mask64(runtime,
340 						   SNDRV_PCM_HW_PARAM_FORMAT,
341 						   ~priv->exclude_format);
342 		if (ret)
343 			return ret;
344 	}
345 
346 	if (priv->constraint_channels) {
347 		ret = snd_pcm_hw_constraint_list(runtime, 0,
348 						 SNDRV_PCM_HW_PARAM_CHANNELS,
349 						 priv->constraint_channels);
350 		if (ret)
351 			return ret;
352 	}
353 
354 	/*
355 	 * Apply rate constraints only to frontend DAI links (no_pcm = 0).
356 	 * Skip DPCM backend (no_pcm = 1) as rate is fixed by be_hw_params_fixup()
357 	 * and ASRC frontend handles rate conversion.
358 	 */
359 	if (priv->constraint_rates && !rtd->dai_link->no_pcm) {
360 		ret = snd_pcm_hw_constraint_list(runtime, 0,
361 						 SNDRV_PCM_HW_PARAM_RATE,
362 						 priv->constraint_rates);
363 		if (ret)
364 			return ret;
365 	}
366 
367 	return 0;
368 }
369 
370 static const struct snd_soc_ops fsl_asoc_card_ops = {
371 	.startup = fsl_asoc_card_startup,
372 	.hw_params = fsl_asoc_card_hw_params,
373 	.hw_free = fsl_asoc_card_hw_free,
374 };
375 
376 static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
377 			      struct snd_pcm_hw_params *params)
378 {
379 	struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
380 	struct snd_interval *rate;
381 	struct snd_mask *mask;
382 
383 	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
384 	rate->max = rate->min = priv->asrc_rate;
385 
386 	mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
387 	snd_mask_none(mask);
388 	snd_mask_set_format(mask, priv->asrc_format);
389 
390 	return 0;
391 }
392 
393 static const struct snd_soc_dai_link fsl_asoc_card_dai[] = {
394 	/* Default ASoC DAI Link*/
395 	{
396 		.name = "HiFi",
397 		.stream_name = "HiFi",
398 		.ops = &fsl_asoc_card_ops,
399 	},
400 	/* DPCM Link between Front-End and Back-End (Optional) */
401 	{
402 		.name = "HiFi-ASRC-FE",
403 		.stream_name = "HiFi-ASRC-FE",
404 		.dynamic = 1,
405 	},
406 	{
407 		.name = "HiFi-ASRC-BE",
408 		.stream_name = "HiFi-ASRC-BE",
409 		.be_hw_params_fixup = be_hw_params_fixup,
410 		.ops = &fsl_asoc_card_ops,
411 		.no_pcm = 1,
412 	},
413 };
414 
415 static int fsl_asoc_card_audmux_init(struct device_node *np,
416 				     struct fsl_asoc_card_priv *priv)
417 {
418 	struct device *dev = &priv->pdev->dev;
419 	u32 int_ptcr = 0, ext_ptcr = 0;
420 	int int_port, ext_port;
421 	int ret;
422 
423 	ret = of_property_read_u32(np, "mux-int-port", &int_port);
424 	if (ret) {
425 		dev_err(dev, "mux-int-port missing or invalid\n");
426 		return ret;
427 	}
428 	ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
429 	if (ret) {
430 		dev_err(dev, "mux-ext-port missing or invalid\n");
431 		return ret;
432 	}
433 
434 	/*
435 	 * The port numbering in the hardware manual starts at 1, while
436 	 * the AUDMUX API expects it starts at 0.
437 	 */
438 	int_port--;
439 	ext_port--;
440 
441 	/*
442 	 * Use asynchronous mode (6 wires) for all cases except AC97.
443 	 * If only 4 wires are needed, just set SSI into
444 	 * synchronous mode and enable 4 PADs in IOMUX.
445 	 */
446 	switch (priv->dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
447 	case SND_SOC_DAIFMT_CBP_CFP:
448 		int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
449 			   IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
450 			   IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
451 			   IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
452 			   IMX_AUDMUX_V2_PTCR_RFSDIR |
453 			   IMX_AUDMUX_V2_PTCR_RCLKDIR |
454 			   IMX_AUDMUX_V2_PTCR_TFSDIR |
455 			   IMX_AUDMUX_V2_PTCR_TCLKDIR;
456 		break;
457 	case SND_SOC_DAIFMT_CBP_CFC:
458 		int_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
459 			   IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
460 			   IMX_AUDMUX_V2_PTCR_RCLKDIR |
461 			   IMX_AUDMUX_V2_PTCR_TCLKDIR;
462 		ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
463 			   IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
464 			   IMX_AUDMUX_V2_PTCR_RFSDIR |
465 			   IMX_AUDMUX_V2_PTCR_TFSDIR;
466 		break;
467 	case SND_SOC_DAIFMT_CBC_CFP:
468 		int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
469 			   IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
470 			   IMX_AUDMUX_V2_PTCR_RFSDIR |
471 			   IMX_AUDMUX_V2_PTCR_TFSDIR;
472 		ext_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
473 			   IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
474 			   IMX_AUDMUX_V2_PTCR_RCLKDIR |
475 			   IMX_AUDMUX_V2_PTCR_TCLKDIR;
476 		break;
477 	case SND_SOC_DAIFMT_CBC_CFC:
478 		ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
479 			   IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
480 			   IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
481 			   IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
482 			   IMX_AUDMUX_V2_PTCR_RFSDIR |
483 			   IMX_AUDMUX_V2_PTCR_RCLKDIR |
484 			   IMX_AUDMUX_V2_PTCR_TFSDIR |
485 			   IMX_AUDMUX_V2_PTCR_TCLKDIR;
486 		break;
487 	default:
488 		if (!fsl_asoc_card_is_ac97(priv))
489 			return -EINVAL;
490 	}
491 
492 	if (fsl_asoc_card_is_ac97(priv)) {
493 		int_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
494 			   IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
495 			   IMX_AUDMUX_V2_PTCR_TCLKDIR;
496 		ext_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
497 			   IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
498 			   IMX_AUDMUX_V2_PTCR_TFSDIR;
499 	}
500 
501 	/* Asynchronous mode can not be set along with RCLKDIR */
502 	if (!fsl_asoc_card_is_ac97(priv)) {
503 		unsigned int pdcr =
504 				IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port);
505 
506 		ret = imx_audmux_v2_configure_port(int_port, 0,
507 						   pdcr);
508 		if (ret) {
509 			dev_err(dev, "audmux internal port setup failed\n");
510 			return ret;
511 		}
512 	}
513 
514 	ret = imx_audmux_v2_configure_port(int_port, int_ptcr,
515 					   IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
516 	if (ret) {
517 		dev_err(dev, "audmux internal port setup failed\n");
518 		return ret;
519 	}
520 
521 	if (!fsl_asoc_card_is_ac97(priv)) {
522 		unsigned int pdcr =
523 				IMX_AUDMUX_V2_PDCR_RXDSEL(int_port);
524 
525 		ret = imx_audmux_v2_configure_port(ext_port, 0,
526 						   pdcr);
527 		if (ret) {
528 			dev_err(dev, "audmux external port setup failed\n");
529 			return ret;
530 		}
531 	}
532 
533 	ret = imx_audmux_v2_configure_port(ext_port, ext_ptcr,
534 					   IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
535 	if (ret) {
536 		dev_err(dev, "audmux external port setup failed\n");
537 		return ret;
538 	}
539 
540 	return 0;
541 }
542 
543 static int fsl_asoc_card_spdif_init(struct device_node *codec_np[],
544 				    struct device_node *cpu_np,
545 				    const char *codec_dai_name[],
546 				    struct fsl_asoc_card_priv *priv)
547 {
548 	struct device *dev = &priv->pdev->dev;
549 	struct device_node *np = dev->of_node;
550 
551 	if (!of_node_name_eq(cpu_np, "spdif")) {
552 		dev_err(dev, "CPU phandle invalid, should be an SPDIF device\n");
553 		return -EINVAL;
554 	}
555 
556 	priv->dai_link[0].playback_only = true;
557 	priv->dai_link[0].capture_only = true;
558 
559 	for (int i = 0; i < 2; i++) {
560 		if (!codec_np[i])
561 			break;
562 
563 		if (of_device_is_compatible(codec_np[i], "linux,spdif-dit")) {
564 			priv->dai_link[0].capture_only = false;
565 			codec_dai_name[i] = "dit-hifi";
566 		} else if (of_device_is_compatible(codec_np[i], "linux,spdif-dir")) {
567 			priv->dai_link[0].playback_only = false;
568 			codec_dai_name[i] = "dir-hifi";
569 		}
570 	}
571 
572 	// Old SPDIF DT binding
573 	if (!codec_np[0]) {
574 		codec_dai_name[0] = snd_soc_dummy_dlc.dai_name;
575 		if (of_property_read_bool(np, "spdif-out"))
576 			priv->dai_link[0].capture_only = false;
577 		if (of_property_read_bool(np, "spdif-in"))
578 			priv->dai_link[0].playback_only = false;
579 	}
580 
581 	if (priv->dai_link[0].playback_only && priv->dai_link[0].capture_only) {
582 		dev_err(dev, "no enabled S/PDIF DAI link\n");
583 		return -EINVAL;
584 	}
585 
586 	if (priv->dai_link[0].playback_only) {
587 		priv->dai_link[1].playback_only = true;
588 		priv->dai_link[2].playback_only = true;
589 		priv->card.dapm_routes = audio_map_tx;
590 		priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
591 	} else if (priv->dai_link[0].capture_only) {
592 		priv->dai_link[1].capture_only = true;
593 		priv->dai_link[2].capture_only = true;
594 		priv->card.dapm_routes = audio_map_rx;
595 		priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_rx);
596 	}
597 
598 	// No DAPM routes with old bindings and dummy codec
599 	if (!codec_np[0]) {
600 		priv->card.dapm_routes = NULL;
601 		priv->card.num_dapm_routes = 0;
602 	}
603 
604 	if (codec_np[0] && codec_np[1]) {
605 		priv->dai_link[0].num_codecs = 2;
606 		priv->dai_link[2].num_codecs = 2;
607 	}
608 
609 	return 0;
610 }
611 
612 static int hp_jack_event(struct notifier_block *nb, unsigned long event,
613 			 void *data)
614 {
615 	struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
616 	struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(jack->card);
617 
618 	if (event & SND_JACK_HEADPHONE)
619 		/* Disable speaker if headphone is plugged in */
620 		return snd_soc_dapm_disable_pin(dapm, "Ext Spk");
621 	else
622 		return snd_soc_dapm_enable_pin(dapm, "Ext Spk");
623 }
624 
625 static struct notifier_block hp_jack_nb = {
626 	.notifier_call = hp_jack_event,
627 };
628 
629 static int mic_jack_event(struct notifier_block *nb, unsigned long event,
630 			  void *data)
631 {
632 	struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
633 	struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(jack->card);
634 
635 	if (event & SND_JACK_MICROPHONE)
636 		/* Disable dmic if microphone is plugged in */
637 		return snd_soc_dapm_disable_pin(dapm, "DMIC");
638 	else
639 		return snd_soc_dapm_enable_pin(dapm, "DMIC");
640 }
641 
642 static struct notifier_block mic_jack_nb = {
643 	.notifier_call = mic_jack_event,
644 };
645 
646 static int fsl_asoc_card_late_probe(struct snd_soc_card *card)
647 {
648 	struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
649 	struct snd_soc_pcm_runtime *rtd = list_first_entry(
650 			&card->rtd_list, struct snd_soc_pcm_runtime, list);
651 	struct snd_soc_dai *codec_dai;
652 	struct codec_priv *codec_priv;
653 	struct device *dev = card->dev;
654 	int codec_idx;
655 	int ret;
656 
657 	if (fsl_asoc_card_is_ac97(priv)) {
658 #if IS_ENABLED(CONFIG_SND_AC97_CODEC)
659 		struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
660 		struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);
661 
662 		/*
663 		 * Use slots 3/4 for S/PDIF so SSI won't try to enable
664 		 * other slots and send some samples there
665 		 * due to SLOTREQ bits for S/PDIF received from codec
666 		 */
667 		snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
668 				     AC97_EA_SPSA_SLOT_MASK, AC97_EA_SPSA_3_4);
669 #endif
670 
671 		return 0;
672 	}
673 
674 	for_each_rtd_codec_dais(rtd, codec_idx, codec_dai) {
675 		codec_priv = &priv->codec_priv[codec_idx];
676 
677 		ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
678 					codec_priv->mclk_freq, SND_SOC_CLOCK_IN);
679 		if (ret && ret != -ENOTSUPP) {
680 			dev_err(dev, "failed to set sysclk in %s\n", __func__);
681 			return ret;
682 		}
683 
684 		if (!IS_ERR_OR_NULL(codec_priv->mclk))
685 			clk_prepare_enable(codec_priv->mclk);
686 	}
687 
688 	return 0;
689 }
690 
691 static int fsl_asoc_card_probe(struct platform_device *pdev)
692 {
693 	struct device_node *cpu_np, *asrc_np;
694 	struct snd_soc_dai_link_component *codec_comp;
695 	struct device_node *codec_np[2];
696 	struct device_node *np = pdev->dev.of_node;
697 	struct platform_device *asrc_pdev = NULL;
698 	struct device_node *bitclkprovider = NULL;
699 	struct device_node *frameprovider = NULL;
700 	struct platform_device *cpu_pdev;
701 	struct fsl_asoc_card_priv *priv;
702 	struct device *codec_dev[2] = { NULL, NULL };
703 	struct snd_soc_dai_link_component *dlc;
704 	const char *codec_dai_name[2];
705 	const char *codec_dev_name[2];
706 	u32 asrc_fmt = 0;
707 	int codec_idx;
708 	u32 width;
709 	int ret;
710 
711 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
712 	if (!priv)
713 		return -ENOMEM;
714 
715 	priv->pdev = pdev;
716 
717 	cpu_np = of_parse_phandle(np, "audio-cpu", 0);
718 	/* Give a chance to old DT bindings */
719 	if (!cpu_np)
720 		cpu_np = of_parse_phandle(np, "ssi-controller", 0);
721 	if (!cpu_np)
722 		cpu_np = of_parse_phandle(np, "spdif-controller", 0);
723 	if (!cpu_np) {
724 		dev_err(&pdev->dev, "CPU phandle missing or invalid\n");
725 		ret = -EINVAL;
726 		goto fail;
727 	}
728 
729 	cpu_pdev = of_find_device_by_node(cpu_np);
730 	if (!cpu_pdev) {
731 		dev_err(&pdev->dev, "failed to find CPU DAI device\n");
732 		ret = -EINVAL;
733 		goto fail;
734 	}
735 
736 	codec_np[0] = of_parse_phandle(np, "audio-codec", 0);
737 	codec_np[1] = of_parse_phandle(np, "audio-codec", 1);
738 
739 	for (codec_idx = 0; codec_idx < 2; codec_idx++) {
740 		if (codec_np[codec_idx]) {
741 			struct platform_device *codec_pdev;
742 			struct i2c_client *codec_i2c;
743 
744 			codec_i2c = of_find_i2c_device_by_node(codec_np[codec_idx]);
745 			if (codec_i2c) {
746 				codec_dev[codec_idx] = &codec_i2c->dev;
747 				codec_dev_name[codec_idx] = codec_i2c->name;
748 			}
749 			if (!codec_dev[codec_idx]) {
750 				codec_pdev = of_find_device_by_node(codec_np[codec_idx]);
751 				if (codec_pdev) {
752 					codec_dev[codec_idx] = &codec_pdev->dev;
753 					codec_dev_name[codec_idx] = codec_pdev->name;
754 				}
755 			}
756 		}
757 	}
758 
759 	asrc_np = of_parse_phandle(np, "audio-asrc", 0);
760 	if (asrc_np)
761 		asrc_pdev = of_find_device_by_node(asrc_np);
762 
763 	/* Get the MCLK rate only, and leave it controlled by CODEC drivers */
764 	for (codec_idx = 0; codec_idx < 2; codec_idx++) {
765 		if (codec_dev[codec_idx]) {
766 			struct clk *codec_clk = clk_get(codec_dev[codec_idx], NULL);
767 
768 			if (!IS_ERR(codec_clk)) {
769 				priv->codec_priv[codec_idx].mclk_freq = clk_get_rate(codec_clk);
770 				clk_put(codec_clk);
771 			}
772 		}
773 	}
774 
775 	/* Default sample rate and format, will be updated in hw_params() */
776 	priv->sample_rate = 44100;
777 	priv->sample_format = SNDRV_PCM_FORMAT_S16_LE;
778 
779 	/* Assign a default DAI format, and allow each card to overwrite it */
780 	priv->dai_fmt = DAI_FMT_BASE;
781 
782 	memcpy(priv->dai_link, fsl_asoc_card_dai,
783 	       sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link));
784 	/*
785 	 * "Default ASoC DAI Link": 1 cpus, 2 codecs, 1 platforms
786 	 * "DPCM Link Front-End":  1 cpus, 1 codecs (dummy), 1 platforms
787 	 * "DPCM Link Back-End": 1 cpus, 2 codecs
788 	 * totally 10 components
789 	 */
790 	dlc = devm_kcalloc(&pdev->dev, 10, sizeof(*dlc), GFP_KERNEL);
791 	if (!dlc) {
792 		ret = -ENOMEM;
793 		goto asrc_fail;
794 	}
795 
796 	priv->dai_link[0].cpus = &dlc[0];
797 	priv->dai_link[0].num_cpus = 1;
798 	priv->dai_link[0].codecs = &dlc[1];
799 	priv->dai_link[0].num_codecs = 1;
800 	priv->dai_link[0].platforms = &dlc[3];
801 	priv->dai_link[0].num_platforms = 1;
802 
803 	priv->dai_link[1].cpus = &dlc[4];
804 	priv->dai_link[1].num_cpus = 1;
805 	priv->dai_link[1].codecs = &dlc[5];
806 	priv->dai_link[1].num_codecs = 0; /* dummy */
807 	priv->dai_link[1].platforms = &dlc[6];
808 	priv->dai_link[1].num_platforms = 1;
809 
810 	priv->dai_link[2].cpus = &dlc[7];
811 	priv->dai_link[2].num_cpus = 1;
812 	priv->dai_link[2].codecs = &dlc[8];
813 	priv->dai_link[2].num_codecs = 1;
814 
815 	priv->card.dapm_routes = audio_map;
816 	priv->card.num_dapm_routes = ARRAY_SIZE(audio_map);
817 	priv->card.driver_name = DRIVER_NAME;
818 
819 	for (codec_idx = 0; codec_idx < 2; codec_idx++) {
820 		priv->codec_priv[codec_idx].fll_id = -1;
821 		priv->codec_priv[codec_idx].pll_id = -1;
822 		priv->codec_priv[codec_idx].pll_ratio_s24 = 384;
823 	}
824 
825 	/* Diversify the card configurations */
826 	if (of_device_is_compatible(np, "fsl,imx-audio-cs42888")) {
827 		codec_dai_name[0] = "cs42888";
828 		priv->cpu_priv.sysclk_freq[TX] = priv->codec_priv[0].mclk_freq;
829 		priv->cpu_priv.sysclk_freq[RX] = priv->codec_priv[0].mclk_freq;
830 		priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
831 		priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
832 		priv->cpu_priv.slot_width = 32;
833 		priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
834 		priv->constraint_channels = &cs42888_channel_constraints;
835 		if (priv->codec_priv[0].mclk_freq % 12288000 == 0)
836 			priv->constraint_rates  = &cs42888_rate_48k_constraints;
837 		else if (priv->codec_priv[0].mclk_freq % 11289600 == 0)
838 			priv->constraint_rates = &cs42888_rate_44k_constraints;
839 		else
840 			dev_warn(&pdev->dev, "Unknown MCLK frequency %lu, no rate constraints\n",
841 				 priv->codec_priv[0].mclk_freq);
842 	} else if (of_device_is_compatible(np, "fsl,imx-audio-cs427x")) {
843 		codec_dai_name[0] = "cs4271-hifi";
844 		priv->codec_priv[0].mclk_id = CS427x_SYSCLK_MCLK;
845 		priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
846 	} else if (of_device_is_compatible(np, "fsl,imx-audio-sgtl5000")) {
847 		codec_dai_name[0] = "sgtl5000";
848 		priv->codec_priv[0].mclk_id = SGTL5000_SYSCLK;
849 		priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
850 	} else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic32x4")) {
851 		codec_dai_name[0] = "tlv320aic32x4-hifi";
852 		priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
853 	} else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic31xx")) {
854 		codec_dai_name[0] = "tlv320dac31xx-hifi";
855 		priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
856 		priv->dai_link[1].playback_only = 1;
857 		priv->dai_link[2].playback_only = 1;
858 		priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
859 		priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
860 		priv->card.dapm_routes = audio_map_tx;
861 		priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
862 	} else if (of_device_is_compatible(np, "fsl,imx-audio-wm8962")) {
863 		codec_dai_name[0] = "wm8962";
864 		priv->codec_priv[0].mclk_id = WM8962_SYSCLK_MCLK;
865 		priv->codec_priv[0].fll_id = WM8962_SYSCLK_FLL;
866 		priv->codec_priv[0].pll_id = WM8962_FLL;
867 		priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
868 		/*
869 		 * WM8962 has same BCLK generation limitations as WM8960.
870 		 * See WM8960 section for detailed explanation.
871 		 */
872 		if (of_node_name_eq(cpu_np, "sai"))
873 			priv->exclude_format = SNDRV_PCM_FMTBIT_S20_3LE;
874 	} else if (of_device_is_compatible(np, "fsl,imx-audio-wm8960")) {
875 		codec_dai_name[0] = "wm8960-hifi";
876 		priv->codec_priv[0].fll_id = WM8960_SYSCLK_AUTO;
877 		priv->codec_priv[0].pll_id = WM8960_SYSCLK_AUTO;
878 		priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
879 		/*
880 		 * WM8960 in master mode cannot generate exact 1.92 MHz BCLK
881 		 * required for S20_3LE (48kHz × 2ch × 20bit). Closest available
882 		 * is 2.048 MHz (SYSCLK/6), which causes right channel corruption.
883 		 *
884 		 * In SAI master mode, SAI derive BCLK from MCLK using integer
885 		 * dividers only. S20_3LE requires non-integer divider ratios
886 		 * with standard MCLK frequencies. For example, 48kHz stereo
887 		 * needs 1.920 MHz BCLK, which requires a divider of 6.4 from
888 		 * 12.288 MHz MCLK (not an integer).
889 		 */
890 		if (of_node_name_eq(cpu_np, "sai"))
891 			priv->exclude_format = SNDRV_PCM_FMTBIT_S20_3LE;
892 	} else if (of_device_is_compatible(np, "fsl,imx-audio-ac97")) {
893 		codec_dai_name[0] = "ac97-hifi";
894 		priv->dai_fmt = SND_SOC_DAIFMT_AC97;
895 		priv->card.dapm_routes = audio_map_ac97;
896 		priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_ac97);
897 	} else if (of_device_is_compatible(np, "fsl,imx-audio-mqs")) {
898 		codec_dai_name[0] = "fsl-mqs-dai";
899 		priv->dai_fmt = SND_SOC_DAIFMT_LEFT_J |
900 				SND_SOC_DAIFMT_CBC_CFC |
901 				SND_SOC_DAIFMT_NB_NF;
902 		priv->dai_link[1].playback_only = 1;
903 		priv->dai_link[2].playback_only = 1;
904 		priv->card.dapm_routes = audio_map_tx;
905 		priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
906 	} else if (of_device_is_compatible(np, "fsl,imx-audio-wm8524")) {
907 		codec_dai_name[0] = "wm8524-hifi";
908 		priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
909 		priv->dai_link[1].playback_only = 1;
910 		priv->dai_link[2].playback_only = 1;
911 		priv->cpu_priv.slot_width = 32;
912 		priv->card.dapm_routes = audio_map_tx;
913 		priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
914 		priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
915 		priv->cpu_priv.sysclk_ratio[TX] = 256;
916 	} else if (of_device_is_compatible(np, "fsl,imx-audio-si476x")) {
917 		codec_dai_name[0] = "si476x-codec";
918 		priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
919 		priv->card.dapm_routes = audio_map_rx;
920 		priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_rx);
921 	} else if (of_device_is_compatible(np, "fsl,imx-audio-wm8958")) {
922 		codec_dai_name[0] = "wm8994-aif1";
923 		priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
924 		priv->codec_priv[0].mclk_id = WM8994_FLL_SRC_MCLK1;
925 		priv->codec_priv[0].fll_id = WM8994_SYSCLK_FLL1;
926 		priv->codec_priv[0].pll_id = WM8994_FLL1;
927 		priv->codec_priv[0].free_freq = priv->codec_priv[0].mclk_freq;
928 		priv->card.dapm_routes = NULL;
929 		priv->card.num_dapm_routes = 0;
930 	} else if (of_device_is_compatible(np, "fsl,imx-audio-nau8822")) {
931 		codec_dai_name[0] = "nau8822-hifi";
932 		priv->codec_priv[0].mclk_id = NAU8822_CLK_MCLK;
933 		priv->codec_priv[0].fll_id = NAU8822_CLK_PLL;
934 		priv->codec_priv[0].pll_id = NAU8822_CLK_PLL;
935 		priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
936 		if (codec_dev[0])
937 			priv->codec_priv[0].mclk = devm_clk_get(codec_dev[0], NULL);
938 	} else if (of_device_is_compatible(np, "fsl,imx-audio-wm8904")) {
939 		codec_dai_name[0] = "wm8904-hifi";
940 		priv->codec_priv[0].mclk_id = WM8904_FLL_MCLK;
941 		priv->codec_priv[0].fll_id = WM8904_CLK_FLL;
942 		priv->codec_priv[0].pll_id = WM8904_FLL_MCLK;
943 		priv->codec_priv[0].pll_ratio_s24 = 192;
944 		priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
945 	} else if (of_device_is_compatible(np, "fsl,imx-audio-spdif")) {
946 		ret = fsl_asoc_card_spdif_init(codec_np, cpu_np, codec_dai_name, priv);
947 		if (ret)
948 			goto asrc_fail;
949 	} else {
950 		dev_err(&pdev->dev, "unknown Device Tree compatible\n");
951 		ret = -EINVAL;
952 		goto asrc_fail;
953 	}
954 
955 	/*
956 	 * Allow setting mclk-id from the device-tree node. Otherwise, the
957 	 * default value for each card configuration is used.
958 	 */
959 	for_each_link_codecs((&(priv->dai_link[0])), codec_idx, codec_comp) {
960 		of_property_read_u32_index(np, "mclk-id", codec_idx,
961 					&priv->codec_priv[codec_idx].mclk_id);
962 	}
963 
964 	/* Format info from DT is optional. */
965 	snd_soc_daifmt_parse_clock_provider_as_phandle(np, NULL, &bitclkprovider, &frameprovider);
966 	if (bitclkprovider || frameprovider) {
967 		unsigned int daifmt = snd_soc_daifmt_parse_format(np, NULL);
968 		bool codec_bitclkprovider = false;
969 		bool codec_frameprovider = false;
970 
971 		for_each_link_codecs((&(priv->dai_link[0])), codec_idx, codec_comp) {
972 			if (bitclkprovider && codec_np[codec_idx] == bitclkprovider)
973 				codec_bitclkprovider = true;
974 			if (frameprovider && codec_np[codec_idx] == frameprovider)
975 				codec_frameprovider = true;
976 		}
977 
978 		if (codec_bitclkprovider)
979 			daifmt |= (codec_frameprovider) ?
980 				SND_SOC_DAIFMT_CBP_CFP : SND_SOC_DAIFMT_CBP_CFC;
981 		else
982 			daifmt |= (codec_frameprovider) ?
983 				SND_SOC_DAIFMT_CBC_CFP : SND_SOC_DAIFMT_CBC_CFC;
984 
985 		/* Override dai_fmt with value from DT */
986 		priv->dai_fmt = daifmt;
987 	}
988 
989 	/* Change direction according to format */
990 	if (priv->dai_fmt & SND_SOC_DAIFMT_CBP_CFP) {
991 		priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_IN;
992 		priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_IN;
993 	}
994 
995 	of_node_put(bitclkprovider);
996 	of_node_put(frameprovider);
997 
998 	if (!fsl_asoc_card_is_ac97(priv) && !codec_dev[0]
999 	    && codec_dai_name[0] != snd_soc_dummy_dlc.dai_name) {
1000 		dev_dbg(&pdev->dev, "failed to find codec device\n");
1001 		ret = -EPROBE_DEFER;
1002 		goto asrc_fail;
1003 	}
1004 
1005 	/* Common settings for corresponding Freescale CPU DAI driver */
1006 	if (of_node_name_eq(cpu_np, "ssi")) {
1007 		/* Only SSI needs to configure AUDMUX */
1008 		ret = fsl_asoc_card_audmux_init(np, priv);
1009 		if (ret) {
1010 			dev_err(&pdev->dev, "failed to init audmux\n");
1011 			goto asrc_fail;
1012 		}
1013 	} else if (of_node_name_eq(cpu_np, "esai")) {
1014 		struct clk *esai_clk = clk_get(&cpu_pdev->dev, "extal");
1015 
1016 		if (!IS_ERR(esai_clk)) {
1017 			priv->cpu_priv.sysclk_freq[TX] = clk_get_rate(esai_clk);
1018 			priv->cpu_priv.sysclk_freq[RX] = clk_get_rate(esai_clk);
1019 			clk_put(esai_clk);
1020 		} else if (PTR_ERR(esai_clk) == -EPROBE_DEFER) {
1021 			ret = -EPROBE_DEFER;
1022 			goto asrc_fail;
1023 		}
1024 
1025 		priv->cpu_priv.sysclk_id[1] = ESAI_HCKT_EXTAL;
1026 		priv->cpu_priv.sysclk_id[0] = ESAI_HCKR_EXTAL;
1027 	} else if (of_node_name_eq(cpu_np, "sai")) {
1028 		priv->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
1029 		priv->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
1030 	}
1031 
1032 	/* Initialize sound card */
1033 	priv->card.dev = &pdev->dev;
1034 	priv->card.owner = THIS_MODULE;
1035 	ret = snd_soc_of_parse_card_name(&priv->card, "model");
1036 	if (ret) {
1037 		snprintf(priv->name, sizeof(priv->name), "%s-audio",
1038 			 fsl_asoc_card_is_ac97(priv) ? "ac97" : codec_dev_name[0]);
1039 		priv->card.name = priv->name;
1040 	}
1041 	priv->card.dai_link = priv->dai_link;
1042 	priv->card.late_probe = fsl_asoc_card_late_probe;
1043 	priv->card.dapm_widgets = fsl_asoc_card_dapm_widgets;
1044 	priv->card.num_dapm_widgets = ARRAY_SIZE(fsl_asoc_card_dapm_widgets);
1045 
1046 	/* Drop the second half of DAPM routes -- ASRC */
1047 	if (!asrc_pdev)
1048 		priv->card.num_dapm_routes /= 2;
1049 
1050 	if (of_property_present(np, "audio-routing")) {
1051 		ret = snd_soc_of_parse_audio_routing(&priv->card, "audio-routing");
1052 		if (ret) {
1053 			dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
1054 			goto asrc_fail;
1055 		}
1056 	}
1057 
1058 	/* Normal DAI Link */
1059 	priv->dai_link[0].cpus->of_node = cpu_np;
1060 	for_each_link_codecs((&(priv->dai_link[0])), codec_idx, codec_comp) {
1061 		codec_comp->dai_name = codec_dai_name[codec_idx];
1062 	}
1063 
1064 	// Old SPDIF DT binding support
1065 	if (codec_dai_name[0] == snd_soc_dummy_dlc.dai_name)
1066 		priv->dai_link[0].codecs[0].name = snd_soc_dummy_dlc.name;
1067 
1068 	if (!fsl_asoc_card_is_ac97(priv)) {
1069 		for_each_link_codecs((&(priv->dai_link[0])), codec_idx, codec_comp) {
1070 			codec_comp->of_node = codec_np[codec_idx];
1071 		}
1072 	} else {
1073 		u32 idx;
1074 
1075 		ret = of_property_read_u32(cpu_np, "cell-index", &idx);
1076 		if (ret) {
1077 			dev_err(&pdev->dev,
1078 				"cannot get CPU index property\n");
1079 			goto asrc_fail;
1080 		}
1081 
1082 		priv->dai_link[0].codecs[0].name =
1083 				devm_kasprintf(&pdev->dev, GFP_KERNEL,
1084 					       "ac97-codec.%u",
1085 					       (unsigned int)idx);
1086 		if (!priv->dai_link[0].codecs[0].name) {
1087 			ret = -ENOMEM;
1088 			goto asrc_fail;
1089 		}
1090 	}
1091 
1092 	priv->dai_link[0].platforms->of_node = cpu_np;
1093 	priv->dai_link[0].dai_fmt = priv->dai_fmt;
1094 	priv->card.num_links = 1;
1095 
1096 	if (asrc_pdev) {
1097 		/* DPCM DAI Links only if ASRC exists */
1098 		priv->dai_link[1].dpcm_merged_chan = 1;
1099 		priv->dai_link[1].ignore_pmdown_time = 1;
1100 		priv->dai_link[1].cpus->of_node = asrc_np;
1101 		priv->dai_link[1].platforms->of_node = asrc_np;
1102 		for_each_link_codecs((&(priv->dai_link[2])), codec_idx, codec_comp) {
1103 			codec_comp->dai_name = priv->dai_link[0].codecs[codec_idx].dai_name;
1104 			codec_comp->of_node = priv->dai_link[0].codecs[codec_idx].of_node;
1105 			codec_comp->name = priv->dai_link[0].codecs[codec_idx].name;
1106 		}
1107 		priv->dai_link[2].cpus->of_node = cpu_np;
1108 		priv->dai_link[2].dai_fmt = priv->dai_fmt;
1109 		priv->dai_link[2].ignore_pmdown_time = 1;
1110 		priv->card.num_links = 3;
1111 
1112 		ret = of_property_read_u32(asrc_np, "fsl,asrc-rate",
1113 					   &priv->asrc_rate);
1114 		if (ret) {
1115 			dev_err(&pdev->dev, "failed to get output rate\n");
1116 			ret = -EINVAL;
1117 			goto asrc_fail;
1118 		}
1119 
1120 		ret = of_property_read_u32(asrc_np, "fsl,asrc-format", &asrc_fmt);
1121 		priv->asrc_format = (__force snd_pcm_format_t)asrc_fmt;
1122 		if (ret) {
1123 			/* Fallback to old binding; translate to asrc_format */
1124 			ret = of_property_read_u32(asrc_np, "fsl,asrc-width",
1125 						   &width);
1126 			if (ret) {
1127 				dev_err(&pdev->dev,
1128 					"failed to decide output format\n");
1129 				goto asrc_fail;
1130 			}
1131 
1132 			if (width == 24)
1133 				priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
1134 			else
1135 				priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
1136 		}
1137 	}
1138 
1139 	/* Finish card registering */
1140 	platform_set_drvdata(pdev, priv);
1141 	snd_soc_card_set_drvdata(&priv->card, priv);
1142 
1143 	ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
1144 	if (ret) {
1145 		dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
1146 		goto asrc_fail;
1147 	}
1148 
1149 	/*
1150 	 * Properties "hp-det-gpios" and "mic-det-gpios" are optional, and
1151 	 * simple_util_init_jack() uses these properties for creating
1152 	 * Headphone Jack and Microphone Jack.
1153 	 *
1154 	 * The notifier is initialized in snd_soc_card_jack_new(), then
1155 	 * snd_soc_jack_notifier_register can be called.
1156 	 */
1157 	if (of_property_present(np, "hp-det-gpios") ||
1158 	    of_property_present(np, "hp-det-gpio") /* deprecated */) {
1159 		ret = simple_util_init_jack(&priv->card, &priv->hp_jack,
1160 					    1, NULL, "Headphone Jack");
1161 		if (ret)
1162 			goto asrc_fail;
1163 
1164 		snd_soc_jack_notifier_register(&priv->hp_jack.jack, &hp_jack_nb);
1165 	}
1166 
1167 	if (of_property_present(np, "mic-det-gpios") ||
1168 	    of_property_present(np, "mic-det-gpio") /* deprecated */) {
1169 		ret = simple_util_init_jack(&priv->card, &priv->mic_jack,
1170 					    0, NULL, "Mic Jack");
1171 		if (ret)
1172 			goto asrc_fail;
1173 
1174 		snd_soc_jack_notifier_register(&priv->mic_jack.jack, &mic_jack_nb);
1175 	}
1176 
1177 asrc_fail:
1178 	of_node_put(asrc_np);
1179 	of_node_put(codec_np[0]);
1180 	of_node_put(codec_np[1]);
1181 	put_device(&cpu_pdev->dev);
1182 fail:
1183 	of_node_put(cpu_np);
1184 
1185 	return ret;
1186 }
1187 
1188 static const struct of_device_id fsl_asoc_card_dt_ids[] = {
1189 	{ .compatible = "fsl,imx-audio-ac97", },
1190 	{ .compatible = "fsl,imx-audio-cs42888", },
1191 	{ .compatible = "fsl,imx-audio-cs427x", },
1192 	{ .compatible = "fsl,imx-audio-tlv320aic32x4", },
1193 	{ .compatible = "fsl,imx-audio-tlv320aic31xx", },
1194 	{ .compatible = "fsl,imx-audio-sgtl5000", },
1195 	{ .compatible = "fsl,imx-audio-wm8962", },
1196 	{ .compatible = "fsl,imx-audio-wm8960", },
1197 	{ .compatible = "fsl,imx-audio-mqs", },
1198 	{ .compatible = "fsl,imx-audio-wm8524", },
1199 	{ .compatible = "fsl,imx-audio-si476x", },
1200 	{ .compatible = "fsl,imx-audio-wm8958", },
1201 	{ .compatible = "fsl,imx-audio-nau8822", },
1202 	{ .compatible = "fsl,imx-audio-wm8904", },
1203 	{ .compatible = "fsl,imx-audio-spdif", },
1204 	{}
1205 };
1206 MODULE_DEVICE_TABLE(of, fsl_asoc_card_dt_ids);
1207 
1208 static struct platform_driver fsl_asoc_card_driver = {
1209 	.probe = fsl_asoc_card_probe,
1210 	.driver = {
1211 		.name = DRIVER_NAME,
1212 		.pm = &snd_soc_pm_ops,
1213 		.of_match_table = fsl_asoc_card_dt_ids,
1214 	},
1215 };
1216 module_platform_driver(fsl_asoc_card_driver);
1217 
1218 MODULE_DESCRIPTION("Freescale Generic ASoC Sound Card driver with ASRC");
1219 MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
1220 MODULE_ALIAS("platform:" DRIVER_NAME);
1221 MODULE_LICENSE("GPL");
1222