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