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