xref: /openbmc/linux/sound/soc/fsl/fsl-asoc-card.c (revision 55b24334)
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 
31 #define DRIVER_NAME "fsl-asoc-card"
32 
33 #define CS427x_SYSCLK_MCLK 0
34 
35 #define RX 0
36 #define TX 1
37 
38 /* Default DAI format without Master and Slave flag */
39 #define DAI_FMT_BASE (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF)
40 
41 /**
42  * struct codec_priv - CODEC private data
43  * @mclk_freq: Clock rate of MCLK
44  * @free_freq: Clock rate of MCLK for hw_free()
45  * @mclk_id: MCLK (or main clock) id for set_sysclk()
46  * @fll_id: FLL (or secordary clock) id for set_sysclk()
47  * @pll_id: PLL id for set_pll()
48  */
49 struct codec_priv {
50 	unsigned long mclk_freq;
51 	unsigned long free_freq;
52 	u32 mclk_id;
53 	u32 fll_id;
54 	u32 pll_id;
55 };
56 
57 /**
58  * struct cpu_priv - CPU private data
59  * @sysclk_freq: SYSCLK rates for set_sysclk()
60  * @sysclk_dir: SYSCLK directions for set_sysclk()
61  * @sysclk_id: SYSCLK ids for set_sysclk()
62  * @slot_width: Slot width of each frame
63  *
64  * Note: [1] for tx and [0] for rx
65  */
66 struct cpu_priv {
67 	unsigned long sysclk_freq[2];
68 	u32 sysclk_dir[2];
69 	u32 sysclk_id[2];
70 	u32 slot_width;
71 };
72 
73 /**
74  * struct fsl_asoc_card_priv - Freescale Generic ASOC card private data
75  * @dai_link: DAI link structure including normal one and DPCM link
76  * @hp_jack: Headphone Jack structure
77  * @mic_jack: Microphone Jack structure
78  * @pdev: platform device pointer
79  * @codec_priv: CODEC private data
80  * @cpu_priv: CPU private data
81  * @card: ASoC card structure
82  * @streams: Mask of current active streams
83  * @sample_rate: Current sample rate
84  * @sample_format: Current sample format
85  * @asrc_rate: ASRC sample rate used by Back-Ends
86  * @asrc_format: ASRC sample format used by Back-Ends
87  * @dai_fmt: DAI format between CPU and CODEC
88  * @name: Card name
89  */
90 
91 struct fsl_asoc_card_priv {
92 	struct snd_soc_dai_link dai_link[3];
93 	struct asoc_simple_jack hp_jack;
94 	struct asoc_simple_jack mic_jack;
95 	struct platform_device *pdev;
96 	struct codec_priv codec_priv;
97 	struct cpu_priv cpu_priv;
98 	struct snd_soc_card card;
99 	u8 streams;
100 	u32 sample_rate;
101 	snd_pcm_format_t sample_format;
102 	u32 asrc_rate;
103 	snd_pcm_format_t asrc_format;
104 	u32 dai_fmt;
105 	char name[32];
106 };
107 
108 /*
109  * This dapm route map exists for DPCM link only.
110  * The other routes shall go through Device Tree.
111  *
112  * Note: keep all ASRC routes in the second half
113  *	 to drop them easily for non-ASRC cases.
114  */
115 static const struct snd_soc_dapm_route audio_map[] = {
116 	/* 1st half -- Normal DAPM routes */
117 	{"Playback",  NULL, "CPU-Playback"},
118 	{"CPU-Capture",  NULL, "Capture"},
119 	/* 2nd half -- ASRC DAPM routes */
120 	{"CPU-Playback",  NULL, "ASRC-Playback"},
121 	{"ASRC-Capture",  NULL, "CPU-Capture"},
122 };
123 
124 static const struct snd_soc_dapm_route audio_map_ac97[] = {
125 	/* 1st half -- Normal DAPM routes */
126 	{"AC97 Playback",  NULL, "CPU AC97 Playback"},
127 	{"CPU AC97 Capture",  NULL, "AC97 Capture"},
128 	/* 2nd half -- ASRC DAPM routes */
129 	{"CPU AC97 Playback",  NULL, "ASRC-Playback"},
130 	{"ASRC-Capture",  NULL, "CPU AC97 Capture"},
131 };
132 
133 static const struct snd_soc_dapm_route audio_map_tx[] = {
134 	/* 1st half -- Normal DAPM routes */
135 	{"Playback",  NULL, "CPU-Playback"},
136 	/* 2nd half -- ASRC DAPM routes */
137 	{"CPU-Playback",  NULL, "ASRC-Playback"},
138 };
139 
140 static const struct snd_soc_dapm_route audio_map_rx[] = {
141 	/* 1st half -- Normal DAPM routes */
142 	{"CPU-Capture",  NULL, "Capture"},
143 	/* 2nd half -- ASRC DAPM routes */
144 	{"ASRC-Capture",  NULL, "CPU-Capture"},
145 };
146 
147 /* Add all possible widgets into here without being redundant */
148 static const struct snd_soc_dapm_widget fsl_asoc_card_dapm_widgets[] = {
149 	SND_SOC_DAPM_LINE("Line Out Jack", NULL),
150 	SND_SOC_DAPM_LINE("Line In Jack", NULL),
151 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
152 	SND_SOC_DAPM_SPK("Ext Spk", NULL),
153 	SND_SOC_DAPM_MIC("Mic Jack", NULL),
154 	SND_SOC_DAPM_MIC("AMIC", NULL),
155 	SND_SOC_DAPM_MIC("DMIC", NULL),
156 };
157 
158 static bool fsl_asoc_card_is_ac97(struct fsl_asoc_card_priv *priv)
159 {
160 	return priv->dai_fmt == SND_SOC_DAIFMT_AC97;
161 }
162 
163 static int fsl_asoc_card_hw_params(struct snd_pcm_substream *substream,
164 				   struct snd_pcm_hw_params *params)
165 {
166 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
167 	struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
168 	bool tx = substream->stream == SNDRV_PCM_STREAM_PLAYBACK;
169 	struct codec_priv *codec_priv = &priv->codec_priv;
170 	struct cpu_priv *cpu_priv = &priv->cpu_priv;
171 	struct device *dev = rtd->card->dev;
172 	unsigned int pll_out;
173 	int ret;
174 
175 	priv->sample_rate = params_rate(params);
176 	priv->sample_format = params_format(params);
177 	priv->streams |= BIT(substream->stream);
178 
179 	if (fsl_asoc_card_is_ac97(priv))
180 		return 0;
181 
182 	/* Specific configurations of DAIs starts from here */
183 	ret = snd_soc_dai_set_sysclk(asoc_rtd_to_cpu(rtd, 0), cpu_priv->sysclk_id[tx],
184 				     cpu_priv->sysclk_freq[tx],
185 				     cpu_priv->sysclk_dir[tx]);
186 	if (ret && ret != -ENOTSUPP) {
187 		dev_err(dev, "failed to set sysclk for cpu dai\n");
188 		goto fail;
189 	}
190 
191 	if (cpu_priv->slot_width) {
192 		ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2,
193 					       cpu_priv->slot_width);
194 		if (ret && ret != -ENOTSUPP) {
195 			dev_err(dev, "failed to set TDM slot for cpu dai\n");
196 			goto fail;
197 		}
198 	}
199 
200 	/* Specific configuration for PLL */
201 	if (codec_priv->pll_id && codec_priv->fll_id) {
202 		if (priv->sample_format == SNDRV_PCM_FORMAT_S24_LE)
203 			pll_out = priv->sample_rate * 384;
204 		else
205 			pll_out = priv->sample_rate * 256;
206 
207 		ret = snd_soc_dai_set_pll(asoc_rtd_to_codec(rtd, 0),
208 					  codec_priv->pll_id,
209 					  codec_priv->mclk_id,
210 					  codec_priv->mclk_freq, pll_out);
211 		if (ret) {
212 			dev_err(dev, "failed to start FLL: %d\n", ret);
213 			goto fail;
214 		}
215 
216 		ret = snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd, 0),
217 					     codec_priv->fll_id,
218 					     pll_out, SND_SOC_CLOCK_IN);
219 
220 		if (ret && ret != -ENOTSUPP) {
221 			dev_err(dev, "failed to set SYSCLK: %d\n", ret);
222 			goto fail;
223 		}
224 	}
225 
226 	return 0;
227 
228 fail:
229 	priv->streams &= ~BIT(substream->stream);
230 	return ret;
231 }
232 
233 static int fsl_asoc_card_hw_free(struct snd_pcm_substream *substream)
234 {
235 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
236 	struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
237 	struct codec_priv *codec_priv = &priv->codec_priv;
238 	struct device *dev = rtd->card->dev;
239 	int ret;
240 
241 	priv->streams &= ~BIT(substream->stream);
242 
243 	if (!priv->streams && codec_priv->pll_id && codec_priv->fll_id) {
244 		/* Force freq to be free_freq to avoid error message in codec */
245 		ret = snd_soc_dai_set_sysclk(asoc_rtd_to_codec(rtd, 0),
246 					     codec_priv->mclk_id,
247 					     codec_priv->free_freq,
248 					     SND_SOC_CLOCK_IN);
249 		if (ret) {
250 			dev_err(dev, "failed to switch away from FLL: %d\n", ret);
251 			return ret;
252 		}
253 
254 		ret = snd_soc_dai_set_pll(asoc_rtd_to_codec(rtd, 0),
255 					  codec_priv->pll_id, 0, 0, 0);
256 		if (ret && ret != -ENOTSUPP) {
257 			dev_err(dev, "failed to stop FLL: %d\n", ret);
258 			return ret;
259 		}
260 	}
261 
262 	return 0;
263 }
264 
265 static const struct snd_soc_ops fsl_asoc_card_ops = {
266 	.hw_params = fsl_asoc_card_hw_params,
267 	.hw_free = fsl_asoc_card_hw_free,
268 };
269 
270 static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
271 			      struct snd_pcm_hw_params *params)
272 {
273 	struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(rtd->card);
274 	struct snd_interval *rate;
275 	struct snd_mask *mask;
276 
277 	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
278 	rate->max = rate->min = priv->asrc_rate;
279 
280 	mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
281 	snd_mask_none(mask);
282 	snd_mask_set_format(mask, priv->asrc_format);
283 
284 	return 0;
285 }
286 
287 SND_SOC_DAILINK_DEFS(hifi,
288 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
289 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
290 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
291 
292 SND_SOC_DAILINK_DEFS(hifi_fe,
293 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
294 	DAILINK_COMP_ARRAY(COMP_DUMMY()),
295 	DAILINK_COMP_ARRAY(COMP_EMPTY()));
296 
297 SND_SOC_DAILINK_DEFS(hifi_be,
298 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
299 	DAILINK_COMP_ARRAY(COMP_EMPTY()),
300 	DAILINK_COMP_ARRAY(COMP_DUMMY()));
301 
302 static const struct snd_soc_dai_link fsl_asoc_card_dai[] = {
303 	/* Default ASoC DAI Link*/
304 	{
305 		.name = "HiFi",
306 		.stream_name = "HiFi",
307 		.ops = &fsl_asoc_card_ops,
308 		SND_SOC_DAILINK_REG(hifi),
309 	},
310 	/* DPCM Link between Front-End and Back-End (Optional) */
311 	{
312 		.name = "HiFi-ASRC-FE",
313 		.stream_name = "HiFi-ASRC-FE",
314 		.dpcm_playback = 1,
315 		.dpcm_capture = 1,
316 		.dynamic = 1,
317 		SND_SOC_DAILINK_REG(hifi_fe),
318 	},
319 	{
320 		.name = "HiFi-ASRC-BE",
321 		.stream_name = "HiFi-ASRC-BE",
322 		.be_hw_params_fixup = be_hw_params_fixup,
323 		.ops = &fsl_asoc_card_ops,
324 		.dpcm_playback = 1,
325 		.dpcm_capture = 1,
326 		.no_pcm = 1,
327 		SND_SOC_DAILINK_REG(hifi_be),
328 	},
329 };
330 
331 static int fsl_asoc_card_audmux_init(struct device_node *np,
332 				     struct fsl_asoc_card_priv *priv)
333 {
334 	struct device *dev = &priv->pdev->dev;
335 	u32 int_ptcr = 0, ext_ptcr = 0;
336 	int int_port, ext_port;
337 	int ret;
338 
339 	ret = of_property_read_u32(np, "mux-int-port", &int_port);
340 	if (ret) {
341 		dev_err(dev, "mux-int-port missing or invalid\n");
342 		return ret;
343 	}
344 	ret = of_property_read_u32(np, "mux-ext-port", &ext_port);
345 	if (ret) {
346 		dev_err(dev, "mux-ext-port missing or invalid\n");
347 		return ret;
348 	}
349 
350 	/*
351 	 * The port numbering in the hardware manual starts at 1, while
352 	 * the AUDMUX API expects it starts at 0.
353 	 */
354 	int_port--;
355 	ext_port--;
356 
357 	/*
358 	 * Use asynchronous mode (6 wires) for all cases except AC97.
359 	 * If only 4 wires are needed, just set SSI into
360 	 * synchronous mode and enable 4 PADs in IOMUX.
361 	 */
362 	switch (priv->dai_fmt & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) {
363 	case SND_SOC_DAIFMT_CBP_CFP:
364 		int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
365 			   IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
366 			   IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
367 			   IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
368 			   IMX_AUDMUX_V2_PTCR_RFSDIR |
369 			   IMX_AUDMUX_V2_PTCR_RCLKDIR |
370 			   IMX_AUDMUX_V2_PTCR_TFSDIR |
371 			   IMX_AUDMUX_V2_PTCR_TCLKDIR;
372 		break;
373 	case SND_SOC_DAIFMT_CBP_CFC:
374 		int_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | ext_port) |
375 			   IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
376 			   IMX_AUDMUX_V2_PTCR_RCLKDIR |
377 			   IMX_AUDMUX_V2_PTCR_TCLKDIR;
378 		ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
379 			   IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
380 			   IMX_AUDMUX_V2_PTCR_RFSDIR |
381 			   IMX_AUDMUX_V2_PTCR_TFSDIR;
382 		break;
383 	case SND_SOC_DAIFMT_CBC_CFP:
384 		int_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | ext_port) |
385 			   IMX_AUDMUX_V2_PTCR_TFSEL(ext_port) |
386 			   IMX_AUDMUX_V2_PTCR_RFSDIR |
387 			   IMX_AUDMUX_V2_PTCR_TFSDIR;
388 		ext_ptcr = IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
389 			   IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
390 			   IMX_AUDMUX_V2_PTCR_RCLKDIR |
391 			   IMX_AUDMUX_V2_PTCR_TCLKDIR;
392 		break;
393 	case SND_SOC_DAIFMT_CBC_CFC:
394 		ext_ptcr = IMX_AUDMUX_V2_PTCR_RFSEL(8 | int_port) |
395 			   IMX_AUDMUX_V2_PTCR_RCSEL(8 | int_port) |
396 			   IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
397 			   IMX_AUDMUX_V2_PTCR_TCSEL(int_port) |
398 			   IMX_AUDMUX_V2_PTCR_RFSDIR |
399 			   IMX_AUDMUX_V2_PTCR_RCLKDIR |
400 			   IMX_AUDMUX_V2_PTCR_TFSDIR |
401 			   IMX_AUDMUX_V2_PTCR_TCLKDIR;
402 		break;
403 	default:
404 		if (!fsl_asoc_card_is_ac97(priv))
405 			return -EINVAL;
406 	}
407 
408 	if (fsl_asoc_card_is_ac97(priv)) {
409 		int_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
410 			   IMX_AUDMUX_V2_PTCR_TCSEL(ext_port) |
411 			   IMX_AUDMUX_V2_PTCR_TCLKDIR;
412 		ext_ptcr = IMX_AUDMUX_V2_PTCR_SYN |
413 			   IMX_AUDMUX_V2_PTCR_TFSEL(int_port) |
414 			   IMX_AUDMUX_V2_PTCR_TFSDIR;
415 	}
416 
417 	/* Asynchronous mode can not be set along with RCLKDIR */
418 	if (!fsl_asoc_card_is_ac97(priv)) {
419 		unsigned int pdcr =
420 				IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port);
421 
422 		ret = imx_audmux_v2_configure_port(int_port, 0,
423 						   pdcr);
424 		if (ret) {
425 			dev_err(dev, "audmux internal port setup failed\n");
426 			return ret;
427 		}
428 	}
429 
430 	ret = imx_audmux_v2_configure_port(int_port, int_ptcr,
431 					   IMX_AUDMUX_V2_PDCR_RXDSEL(ext_port));
432 	if (ret) {
433 		dev_err(dev, "audmux internal port setup failed\n");
434 		return ret;
435 	}
436 
437 	if (!fsl_asoc_card_is_ac97(priv)) {
438 		unsigned int pdcr =
439 				IMX_AUDMUX_V2_PDCR_RXDSEL(int_port);
440 
441 		ret = imx_audmux_v2_configure_port(ext_port, 0,
442 						   pdcr);
443 		if (ret) {
444 			dev_err(dev, "audmux external port setup failed\n");
445 			return ret;
446 		}
447 	}
448 
449 	ret = imx_audmux_v2_configure_port(ext_port, ext_ptcr,
450 					   IMX_AUDMUX_V2_PDCR_RXDSEL(int_port));
451 	if (ret) {
452 		dev_err(dev, "audmux external port setup failed\n");
453 		return ret;
454 	}
455 
456 	return 0;
457 }
458 
459 static int hp_jack_event(struct notifier_block *nb, unsigned long event,
460 			 void *data)
461 {
462 	struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
463 	struct snd_soc_dapm_context *dapm = &jack->card->dapm;
464 
465 	if (event & SND_JACK_HEADPHONE)
466 		/* Disable speaker if headphone is plugged in */
467 		return snd_soc_dapm_disable_pin(dapm, "Ext Spk");
468 	else
469 		return snd_soc_dapm_enable_pin(dapm, "Ext Spk");
470 }
471 
472 static struct notifier_block hp_jack_nb = {
473 	.notifier_call = hp_jack_event,
474 };
475 
476 static int mic_jack_event(struct notifier_block *nb, unsigned long event,
477 			  void *data)
478 {
479 	struct snd_soc_jack *jack = (struct snd_soc_jack *)data;
480 	struct snd_soc_dapm_context *dapm = &jack->card->dapm;
481 
482 	if (event & SND_JACK_MICROPHONE)
483 		/* Disable dmic if microphone is plugged in */
484 		return snd_soc_dapm_disable_pin(dapm, "DMIC");
485 	else
486 		return snd_soc_dapm_enable_pin(dapm, "DMIC");
487 }
488 
489 static struct notifier_block mic_jack_nb = {
490 	.notifier_call = mic_jack_event,
491 };
492 
493 static int fsl_asoc_card_late_probe(struct snd_soc_card *card)
494 {
495 	struct fsl_asoc_card_priv *priv = snd_soc_card_get_drvdata(card);
496 	struct snd_soc_pcm_runtime *rtd = list_first_entry(
497 			&card->rtd_list, struct snd_soc_pcm_runtime, list);
498 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
499 	struct codec_priv *codec_priv = &priv->codec_priv;
500 	struct device *dev = card->dev;
501 	int ret;
502 
503 	if (fsl_asoc_card_is_ac97(priv)) {
504 #if IS_ENABLED(CONFIG_SND_AC97_CODEC)
505 		struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
506 		struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);
507 
508 		/*
509 		 * Use slots 3/4 for S/PDIF so SSI won't try to enable
510 		 * other slots and send some samples there
511 		 * due to SLOTREQ bits for S/PDIF received from codec
512 		 */
513 		snd_ac97_update_bits(ac97, AC97_EXTENDED_STATUS,
514 				     AC97_EA_SPSA_SLOT_MASK, AC97_EA_SPSA_3_4);
515 #endif
516 
517 		return 0;
518 	}
519 
520 	ret = snd_soc_dai_set_sysclk(codec_dai, codec_priv->mclk_id,
521 				     codec_priv->mclk_freq, SND_SOC_CLOCK_IN);
522 	if (ret && ret != -ENOTSUPP) {
523 		dev_err(dev, "failed to set sysclk in %s\n", __func__);
524 		return ret;
525 	}
526 
527 	return 0;
528 }
529 
530 static int fsl_asoc_card_probe(struct platform_device *pdev)
531 {
532 	struct device_node *cpu_np, *codec_np, *asrc_np;
533 	struct device_node *np = pdev->dev.of_node;
534 	struct platform_device *asrc_pdev = NULL;
535 	struct device_node *bitclkprovider = NULL;
536 	struct device_node *frameprovider = NULL;
537 	struct platform_device *cpu_pdev;
538 	struct fsl_asoc_card_priv *priv;
539 	struct device *codec_dev = NULL;
540 	const char *codec_dai_name;
541 	const char *codec_dev_name;
542 	u32 asrc_fmt = 0;
543 	u32 width;
544 	int ret;
545 
546 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
547 	if (!priv)
548 		return -ENOMEM;
549 
550 	cpu_np = of_parse_phandle(np, "audio-cpu", 0);
551 	/* Give a chance to old DT binding */
552 	if (!cpu_np)
553 		cpu_np = of_parse_phandle(np, "ssi-controller", 0);
554 	if (!cpu_np) {
555 		dev_err(&pdev->dev, "CPU phandle missing or invalid\n");
556 		ret = -EINVAL;
557 		goto fail;
558 	}
559 
560 	cpu_pdev = of_find_device_by_node(cpu_np);
561 	if (!cpu_pdev) {
562 		dev_err(&pdev->dev, "failed to find CPU DAI device\n");
563 		ret = -EINVAL;
564 		goto fail;
565 	}
566 
567 	codec_np = of_parse_phandle(np, "audio-codec", 0);
568 	if (codec_np) {
569 		struct platform_device *codec_pdev;
570 		struct i2c_client *codec_i2c;
571 
572 		codec_i2c = of_find_i2c_device_by_node(codec_np);
573 		if (codec_i2c) {
574 			codec_dev = &codec_i2c->dev;
575 			codec_dev_name = codec_i2c->name;
576 		}
577 		if (!codec_dev) {
578 			codec_pdev = of_find_device_by_node(codec_np);
579 			if (codec_pdev) {
580 				codec_dev = &codec_pdev->dev;
581 				codec_dev_name = codec_pdev->name;
582 			}
583 		}
584 	}
585 
586 	asrc_np = of_parse_phandle(np, "audio-asrc", 0);
587 	if (asrc_np)
588 		asrc_pdev = of_find_device_by_node(asrc_np);
589 
590 	/* Get the MCLK rate only, and leave it controlled by CODEC drivers */
591 	if (codec_dev) {
592 		struct clk *codec_clk = clk_get(codec_dev, NULL);
593 
594 		if (!IS_ERR(codec_clk)) {
595 			priv->codec_priv.mclk_freq = clk_get_rate(codec_clk);
596 			clk_put(codec_clk);
597 		}
598 	}
599 
600 	/* Default sample rate and format, will be updated in hw_params() */
601 	priv->sample_rate = 44100;
602 	priv->sample_format = SNDRV_PCM_FORMAT_S16_LE;
603 
604 	/* Assign a default DAI format, and allow each card to overwrite it */
605 	priv->dai_fmt = DAI_FMT_BASE;
606 
607 	memcpy(priv->dai_link, fsl_asoc_card_dai,
608 	       sizeof(struct snd_soc_dai_link) * ARRAY_SIZE(priv->dai_link));
609 
610 	priv->card.dapm_routes = audio_map;
611 	priv->card.num_dapm_routes = ARRAY_SIZE(audio_map);
612 	priv->card.driver_name = DRIVER_NAME;
613 	/* Diversify the card configurations */
614 	if (of_device_is_compatible(np, "fsl,imx-audio-cs42888")) {
615 		codec_dai_name = "cs42888";
616 		priv->cpu_priv.sysclk_freq[TX] = priv->codec_priv.mclk_freq;
617 		priv->cpu_priv.sysclk_freq[RX] = priv->codec_priv.mclk_freq;
618 		priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
619 		priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
620 		priv->cpu_priv.slot_width = 32;
621 		priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
622 	} else if (of_device_is_compatible(np, "fsl,imx-audio-cs427x")) {
623 		codec_dai_name = "cs4271-hifi";
624 		priv->codec_priv.mclk_id = CS427x_SYSCLK_MCLK;
625 		priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
626 	} else if (of_device_is_compatible(np, "fsl,imx-audio-sgtl5000")) {
627 		codec_dai_name = "sgtl5000";
628 		priv->codec_priv.mclk_id = SGTL5000_SYSCLK;
629 		priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
630 	} else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic32x4")) {
631 		codec_dai_name = "tlv320aic32x4-hifi";
632 		priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
633 	} else if (of_device_is_compatible(np, "fsl,imx-audio-tlv320aic31xx")) {
634 		codec_dai_name = "tlv320dac31xx-hifi";
635 		priv->dai_fmt |= SND_SOC_DAIFMT_CBS_CFS;
636 		priv->dai_link[1].dpcm_capture = 0;
637 		priv->dai_link[2].dpcm_capture = 0;
638 		priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_OUT;
639 		priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_OUT;
640 		priv->card.dapm_routes = audio_map_tx;
641 		priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
642 	} else if (of_device_is_compatible(np, "fsl,imx-audio-wm8962")) {
643 		codec_dai_name = "wm8962";
644 		priv->codec_priv.mclk_id = WM8962_SYSCLK_MCLK;
645 		priv->codec_priv.fll_id = WM8962_SYSCLK_FLL;
646 		priv->codec_priv.pll_id = WM8962_FLL;
647 		priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
648 	} else if (of_device_is_compatible(np, "fsl,imx-audio-wm8960")) {
649 		codec_dai_name = "wm8960-hifi";
650 		priv->codec_priv.fll_id = WM8960_SYSCLK_AUTO;
651 		priv->codec_priv.pll_id = WM8960_SYSCLK_AUTO;
652 		priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
653 	} else if (of_device_is_compatible(np, "fsl,imx-audio-ac97")) {
654 		codec_dai_name = "ac97-hifi";
655 		priv->dai_fmt = SND_SOC_DAIFMT_AC97;
656 		priv->card.dapm_routes = audio_map_ac97;
657 		priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_ac97);
658 	} else if (of_device_is_compatible(np, "fsl,imx-audio-mqs")) {
659 		codec_dai_name = "fsl-mqs-dai";
660 		priv->dai_fmt = SND_SOC_DAIFMT_LEFT_J |
661 				SND_SOC_DAIFMT_CBC_CFC |
662 				SND_SOC_DAIFMT_NB_NF;
663 		priv->dai_link[1].dpcm_capture = 0;
664 		priv->dai_link[2].dpcm_capture = 0;
665 		priv->card.dapm_routes = audio_map_tx;
666 		priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
667 	} else if (of_device_is_compatible(np, "fsl,imx-audio-wm8524")) {
668 		codec_dai_name = "wm8524-hifi";
669 		priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
670 		priv->dai_link[1].dpcm_capture = 0;
671 		priv->dai_link[2].dpcm_capture = 0;
672 		priv->cpu_priv.slot_width = 32;
673 		priv->card.dapm_routes = audio_map_tx;
674 		priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_tx);
675 	} else if (of_device_is_compatible(np, "fsl,imx-audio-si476x")) {
676 		codec_dai_name = "si476x-codec";
677 		priv->dai_fmt |= SND_SOC_DAIFMT_CBC_CFC;
678 		priv->card.dapm_routes = audio_map_rx;
679 		priv->card.num_dapm_routes = ARRAY_SIZE(audio_map_rx);
680 	} else if (of_device_is_compatible(np, "fsl,imx-audio-wm8958")) {
681 		codec_dai_name = "wm8994-aif1";
682 		priv->dai_fmt |= SND_SOC_DAIFMT_CBP_CFP;
683 		priv->codec_priv.mclk_id = WM8994_FLL_SRC_MCLK1;
684 		priv->codec_priv.fll_id = WM8994_SYSCLK_FLL1;
685 		priv->codec_priv.pll_id = WM8994_FLL1;
686 		priv->codec_priv.free_freq = priv->codec_priv.mclk_freq;
687 		priv->card.dapm_routes = NULL;
688 		priv->card.num_dapm_routes = 0;
689 	} else {
690 		dev_err(&pdev->dev, "unknown Device Tree compatible\n");
691 		ret = -EINVAL;
692 		goto asrc_fail;
693 	}
694 
695 	/*
696 	 * Allow setting mclk-id from the device-tree node. Otherwise, the
697 	 * default value for each card configuration is used.
698 	 */
699 	of_property_read_u32(np, "mclk-id", &priv->codec_priv.mclk_id);
700 
701 	/* Format info from DT is optional. */
702 	snd_soc_daifmt_parse_clock_provider_as_phandle(np, NULL, &bitclkprovider, &frameprovider);
703 	if (bitclkprovider || frameprovider) {
704 		unsigned int daifmt = snd_soc_daifmt_parse_format(np, NULL);
705 
706 		if (codec_np == bitclkprovider)
707 			daifmt |= (codec_np == frameprovider) ?
708 				SND_SOC_DAIFMT_CBP_CFP : SND_SOC_DAIFMT_CBP_CFC;
709 		else
710 			daifmt |= (codec_np == frameprovider) ?
711 				SND_SOC_DAIFMT_CBC_CFP : SND_SOC_DAIFMT_CBC_CFC;
712 
713 		/* Override dai_fmt with value from DT */
714 		priv->dai_fmt = daifmt;
715 	}
716 
717 	/* Change direction according to format */
718 	if (priv->dai_fmt & SND_SOC_DAIFMT_CBP_CFP) {
719 		priv->cpu_priv.sysclk_dir[TX] = SND_SOC_CLOCK_IN;
720 		priv->cpu_priv.sysclk_dir[RX] = SND_SOC_CLOCK_IN;
721 	}
722 
723 	of_node_put(bitclkprovider);
724 	of_node_put(frameprovider);
725 
726 	if (!fsl_asoc_card_is_ac97(priv) && !codec_dev) {
727 		dev_dbg(&pdev->dev, "failed to find codec device\n");
728 		ret = -EPROBE_DEFER;
729 		goto asrc_fail;
730 	}
731 
732 	/* Common settings for corresponding Freescale CPU DAI driver */
733 	if (of_node_name_eq(cpu_np, "ssi")) {
734 		/* Only SSI needs to configure AUDMUX */
735 		ret = fsl_asoc_card_audmux_init(np, priv);
736 		if (ret) {
737 			dev_err(&pdev->dev, "failed to init audmux\n");
738 			goto asrc_fail;
739 		}
740 	} else if (of_node_name_eq(cpu_np, "esai")) {
741 		struct clk *esai_clk = clk_get(&cpu_pdev->dev, "extal");
742 
743 		if (!IS_ERR(esai_clk)) {
744 			priv->cpu_priv.sysclk_freq[TX] = clk_get_rate(esai_clk);
745 			priv->cpu_priv.sysclk_freq[RX] = clk_get_rate(esai_clk);
746 			clk_put(esai_clk);
747 		} else if (PTR_ERR(esai_clk) == -EPROBE_DEFER) {
748 			ret = -EPROBE_DEFER;
749 			goto asrc_fail;
750 		}
751 
752 		priv->cpu_priv.sysclk_id[1] = ESAI_HCKT_EXTAL;
753 		priv->cpu_priv.sysclk_id[0] = ESAI_HCKR_EXTAL;
754 	} else if (of_node_name_eq(cpu_np, "sai")) {
755 		priv->cpu_priv.sysclk_id[1] = FSL_SAI_CLK_MAST1;
756 		priv->cpu_priv.sysclk_id[0] = FSL_SAI_CLK_MAST1;
757 	}
758 
759 	/* Initialize sound card */
760 	priv->pdev = pdev;
761 	priv->card.dev = &pdev->dev;
762 	priv->card.owner = THIS_MODULE;
763 	ret = snd_soc_of_parse_card_name(&priv->card, "model");
764 	if (ret) {
765 		snprintf(priv->name, sizeof(priv->name), "%s-audio",
766 			 fsl_asoc_card_is_ac97(priv) ? "ac97" : codec_dev_name);
767 		priv->card.name = priv->name;
768 	}
769 	priv->card.dai_link = priv->dai_link;
770 	priv->card.late_probe = fsl_asoc_card_late_probe;
771 	priv->card.dapm_widgets = fsl_asoc_card_dapm_widgets;
772 	priv->card.num_dapm_widgets = ARRAY_SIZE(fsl_asoc_card_dapm_widgets);
773 
774 	/* Drop the second half of DAPM routes -- ASRC */
775 	if (!asrc_pdev)
776 		priv->card.num_dapm_routes /= 2;
777 
778 	if (of_property_read_bool(np, "audio-routing")) {
779 		ret = snd_soc_of_parse_audio_routing(&priv->card, "audio-routing");
780 		if (ret) {
781 			dev_err(&pdev->dev, "failed to parse audio-routing: %d\n", ret);
782 			goto asrc_fail;
783 		}
784 	}
785 
786 	/* Normal DAI Link */
787 	priv->dai_link[0].cpus->of_node = cpu_np;
788 	priv->dai_link[0].codecs->dai_name = codec_dai_name;
789 
790 	if (!fsl_asoc_card_is_ac97(priv))
791 		priv->dai_link[0].codecs->of_node = codec_np;
792 	else {
793 		u32 idx;
794 
795 		ret = of_property_read_u32(cpu_np, "cell-index", &idx);
796 		if (ret) {
797 			dev_err(&pdev->dev,
798 				"cannot get CPU index property\n");
799 			goto asrc_fail;
800 		}
801 
802 		priv->dai_link[0].codecs->name =
803 				devm_kasprintf(&pdev->dev, GFP_KERNEL,
804 					       "ac97-codec.%u",
805 					       (unsigned int)idx);
806 		if (!priv->dai_link[0].codecs->name) {
807 			ret = -ENOMEM;
808 			goto asrc_fail;
809 		}
810 	}
811 
812 	priv->dai_link[0].platforms->of_node = cpu_np;
813 	priv->dai_link[0].dai_fmt = priv->dai_fmt;
814 	priv->card.num_links = 1;
815 
816 	if (asrc_pdev) {
817 		/* DPCM DAI Links only if ASRC exists */
818 		priv->dai_link[1].cpus->of_node = asrc_np;
819 		priv->dai_link[1].platforms->of_node = asrc_np;
820 		priv->dai_link[2].codecs->dai_name = codec_dai_name;
821 		priv->dai_link[2].codecs->of_node = codec_np;
822 		priv->dai_link[2].codecs->name =
823 				priv->dai_link[0].codecs->name;
824 		priv->dai_link[2].cpus->of_node = cpu_np;
825 		priv->dai_link[2].dai_fmt = priv->dai_fmt;
826 		priv->card.num_links = 3;
827 
828 		ret = of_property_read_u32(asrc_np, "fsl,asrc-rate",
829 					   &priv->asrc_rate);
830 		if (ret) {
831 			dev_err(&pdev->dev, "failed to get output rate\n");
832 			ret = -EINVAL;
833 			goto asrc_fail;
834 		}
835 
836 		ret = of_property_read_u32(asrc_np, "fsl,asrc-format", &asrc_fmt);
837 		priv->asrc_format = (__force snd_pcm_format_t)asrc_fmt;
838 		if (ret) {
839 			/* Fallback to old binding; translate to asrc_format */
840 			ret = of_property_read_u32(asrc_np, "fsl,asrc-width",
841 						   &width);
842 			if (ret) {
843 				dev_err(&pdev->dev,
844 					"failed to decide output format\n");
845 				goto asrc_fail;
846 			}
847 
848 			if (width == 24)
849 				priv->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
850 			else
851 				priv->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
852 		}
853 	}
854 
855 	/* Finish card registering */
856 	platform_set_drvdata(pdev, priv);
857 	snd_soc_card_set_drvdata(&priv->card, priv);
858 
859 	ret = devm_snd_soc_register_card(&pdev->dev, &priv->card);
860 	if (ret) {
861 		dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
862 		goto asrc_fail;
863 	}
864 
865 	/*
866 	 * Properties "hp-det-gpio" and "mic-det-gpio" are optional, and
867 	 * asoc_simple_init_jack uses these properties for creating
868 	 * Headphone Jack and Microphone Jack.
869 	 *
870 	 * The notifier is initialized in snd_soc_card_jack_new(), then
871 	 * snd_soc_jack_notifier_register can be called.
872 	 */
873 	if (of_property_read_bool(np, "hp-det-gpio")) {
874 		ret = asoc_simple_init_jack(&priv->card, &priv->hp_jack,
875 					    1, NULL, "Headphone Jack");
876 		if (ret)
877 			goto asrc_fail;
878 
879 		snd_soc_jack_notifier_register(&priv->hp_jack.jack, &hp_jack_nb);
880 	}
881 
882 	if (of_property_read_bool(np, "mic-det-gpio")) {
883 		ret = asoc_simple_init_jack(&priv->card, &priv->mic_jack,
884 					    0, NULL, "Mic Jack");
885 		if (ret)
886 			goto asrc_fail;
887 
888 		snd_soc_jack_notifier_register(&priv->mic_jack.jack, &mic_jack_nb);
889 	}
890 
891 asrc_fail:
892 	of_node_put(asrc_np);
893 	of_node_put(codec_np);
894 	put_device(&cpu_pdev->dev);
895 fail:
896 	of_node_put(cpu_np);
897 
898 	return ret;
899 }
900 
901 static const struct of_device_id fsl_asoc_card_dt_ids[] = {
902 	{ .compatible = "fsl,imx-audio-ac97", },
903 	{ .compatible = "fsl,imx-audio-cs42888", },
904 	{ .compatible = "fsl,imx-audio-cs427x", },
905 	{ .compatible = "fsl,imx-audio-tlv320aic32x4", },
906 	{ .compatible = "fsl,imx-audio-tlv320aic31xx", },
907 	{ .compatible = "fsl,imx-audio-sgtl5000", },
908 	{ .compatible = "fsl,imx-audio-wm8962", },
909 	{ .compatible = "fsl,imx-audio-wm8960", },
910 	{ .compatible = "fsl,imx-audio-mqs", },
911 	{ .compatible = "fsl,imx-audio-wm8524", },
912 	{ .compatible = "fsl,imx-audio-si476x", },
913 	{ .compatible = "fsl,imx-audio-wm8958", },
914 	{}
915 };
916 MODULE_DEVICE_TABLE(of, fsl_asoc_card_dt_ids);
917 
918 static struct platform_driver fsl_asoc_card_driver = {
919 	.probe = fsl_asoc_card_probe,
920 	.driver = {
921 		.name = DRIVER_NAME,
922 		.pm = &snd_soc_pm_ops,
923 		.of_match_table = fsl_asoc_card_dt_ids,
924 	},
925 };
926 module_platform_driver(fsl_asoc_card_driver);
927 
928 MODULE_DESCRIPTION("Freescale Generic ASoC Sound Card driver with ASRC");
929 MODULE_AUTHOR("Nicolin Chen <nicoleotsuka@gmail.com>");
930 MODULE_ALIAS("platform:" DRIVER_NAME);
931 MODULE_LICENSE("GPL");
932