xref: /linux/sound/soc/fsl/imx-card.c (revision a9e6060bb2a6cae6d43a98ec0794844ad01273d3)
1 // SPDX-License-Identifier: GPL-2.0+
2 // Copyright 2017-2021 NXP
3 
4 #include <linux/module.h>
5 #include <linux/init.h>
6 #include <linux/slab.h>
7 #include <linux/gpio/consumer.h>
8 #include <linux/of.h>
9 #include <linux/i2c.h>
10 #include <linux/clk.h>
11 #include <sound/soc.h>
12 #include <sound/pcm_params.h>
13 #include <sound/pcm.h>
14 #include <sound/soc-dapm.h>
15 #include <sound/simple_card_utils.h>
16 
17 #include "fsl_sai.h"
18 
19 #define IMX_CARD_MCLK_22P5792MHZ  22579200
20 #define IMX_CARD_MCLK_24P576MHZ   24576000
21 
22 enum codec_type {
23 	CODEC_DUMMY = 0,
24 	CODEC_AK5558 = 1,
25 	CODEC_AK4458,
26 	CODEC_AK4497,
27 	CODEC_AK5552,
28 	CODEC_CS42888,
29 };
30 
31 /*
32  * Mapping LRCK fs and frame width, table 3 & 4 in datasheet
33  * @rmin: min rate
34  * @rmax: max rate
35  * @wmin: min frame ratio
36  * @wmax: max frame ratio
37  */
38 struct imx_akcodec_fs_mul {
39 	unsigned int rmin;
40 	unsigned int rmax;
41 	unsigned int wmin;
42 	unsigned int wmax;
43 };
44 
45 /*
46  * Mapping TDM mode and frame width
47  */
48 struct imx_akcodec_tdm_fs_mul {
49 	unsigned int min;
50 	unsigned int max;
51 	unsigned int mul;
52 };
53 
54 /*
55  * struct imx_card_plat_data - specific info for codecs
56  *
57  * @fs_mul: ratio of mclk/fs for normal mode
58  * @tdm_fs_mul: ratio of mclk/fs for tdm mode
59  * @support_rates: supported sample rate
60  * @support_tdm_rates: supported sample rate for tdm mode
61  * @support_channels: supported channels
62  * @support_tdm_channels: supported channels for tdm mode
63  * @num_fs_mul: ARRAY_SIZE of fs_mul
64  * @num_tdm_fs_mul: ARRAY_SIZE of tdm_fs_mul
65  * @num_rates: ARRAY_SIZE of support_rates
66  * @num_tdm_rates: ARRAY_SIZE of support_tdm_rates
67  * @num_channels: ARRAY_SIZE of support_channels
68  * @num_tdm_channels: ARRAY_SIZE of support_tdm_channels
69  * @type: codec type
70  */
71 struct imx_card_plat_data {
72 	struct imx_akcodec_fs_mul  *fs_mul;
73 	struct imx_akcodec_tdm_fs_mul  *tdm_fs_mul;
74 	const u32 *support_rates;
75 	const u32 *support_tdm_rates;
76 	const u32 *support_channels;
77 	const u32 *support_tdm_channels;
78 	unsigned int num_fs_mul;
79 	unsigned int num_tdm_fs_mul;
80 	unsigned int num_rates;
81 	unsigned int num_tdm_rates;
82 	unsigned int num_channels;
83 	unsigned int num_tdm_channels;
84 	unsigned int num_codecs;
85 	enum codec_type type;
86 };
87 
88 /*
89  * struct dai_link_data - specific info for dai link
90  *
91  * @slots: slot number
92  * @slot_width: slot width value
93  * @cpu_sysclk_id: sysclk id for cpu dai
94  * @one2one_ratio: true if mclk equal to bclk
95  */
96 struct dai_link_data {
97 	unsigned int slots;
98 	unsigned int slot_width;
99 	unsigned int cpu_sysclk_id;
100 	bool one2one_ratio;
101 };
102 
103 /*
104  * struct imx_card_data - platform device data
105  *
106  * @plat_data: pointer of imx_card_plat_data
107  * @dapm_routes: pointer of dapm_routes
108  * @link_data: private data for dai link
109  * @card: card instance
110  * @num_dapm_routes: number of dapm_routes
111  * @asrc_rate: asrc rates
112  * @asrc_format: asrc format
113  */
114 struct imx_card_data {
115 	struct imx_card_plat_data *plat_data;
116 	struct snd_soc_dapm_route *dapm_routes;
117 	struct dai_link_data *link_data;
118 	struct snd_soc_card card;
119 	int num_dapm_routes;
120 	u32 asrc_rate;
121 	snd_pcm_format_t asrc_format;
122 };
123 
124 static struct imx_akcodec_fs_mul ak4458_fs_mul[] = {
125 	/* Normal, < 32kHz */
126 	{ .rmin = 8000,   .rmax = 24000,  .wmin = 256,  .wmax = 1024, },
127 	/* Normal, 32kHz */
128 	{ .rmin = 32000,  .rmax = 32000,  .wmin = 256,  .wmax = 1024, },
129 	/* Normal */
130 	{ .rmin = 44100,  .rmax = 48000,  .wmin = 256,  .wmax = 768,  },
131 	/* Double */
132 	{ .rmin = 88200,  .rmax = 96000,  .wmin = 256,  .wmax = 512,  },
133 	/* Quad */
134 	{ .rmin = 176400, .rmax = 192000, .wmin = 128,  .wmax = 256,  },
135 	/* Oct */
136 	{ .rmin = 352800, .rmax = 384000, .wmin = 32,   .wmax = 128,  },
137 	/* Hex */
138 	{ .rmin = 705600, .rmax = 768000, .wmin = 16,   .wmax = 64,   },
139 };
140 
141 static struct imx_akcodec_tdm_fs_mul ak4458_tdm_fs_mul[] = {
142 	/*
143 	 * Table 13	- Audio Interface Format
144 	 * For TDM mode, MCLK should is set to
145 	 * obtained from 2 * slots * slot_width
146 	 */
147 	{ .min = 128,	.max = 128,	.mul = 256  }, /* TDM128 */
148 	{ .min = 256,	.max = 256,	.mul = 512  }, /* TDM256 */
149 	{ .min = 512,	.max = 512,	.mul = 1024  }, /* TDM512 */
150 };
151 
152 static struct imx_akcodec_fs_mul ak4497_fs_mul[] = {
153 	/**
154 	 * Table 7      - mapping multiplier and speed mode
155 	 * Tables 8 & 9 - mapping speed mode and LRCK fs
156 	 */
157 	{ .rmin = 8000,   .rmax = 32000,  .wmin = 256,  .wmax = 1024, }, /* Normal, <= 32kHz */
158 	{ .rmin = 44100,  .rmax = 48000,  .wmin = 256,  .wmax = 512, }, /* Normal */
159 	{ .rmin = 88200,  .rmax = 96000,  .wmin = 256,  .wmax = 256, }, /* Double */
160 	{ .rmin = 176400, .rmax = 192000, .wmin = 128,  .wmax = 128, }, /* Quad */
161 	{ .rmin = 352800, .rmax = 384000, .wmin = 128,  .wmax = 128, }, /* Oct */
162 	{ .rmin = 705600, .rmax = 768000, .wmin = 64,   .wmax = 64, }, /* Hex */
163 };
164 
165 /*
166  * Auto MCLK selection based on LRCK for Normal Mode
167  * (Table 4 from datasheet)
168  */
169 static struct imx_akcodec_fs_mul ak5558_fs_mul[] = {
170 	{ .rmin = 8000,   .rmax = 32000,  .wmin = 512,  .wmax = 1024, },
171 	{ .rmin = 44100,  .rmax = 48000,  .wmin = 512,  .wmax = 512, },
172 	{ .rmin = 88200,  .rmax = 96000,  .wmin = 256,  .wmax = 256, },
173 	{ .rmin = 176400, .rmax = 192000, .wmin = 128,  .wmax = 128, },
174 	{ .rmin = 352800, .rmax = 384000, .wmin = 64,   .wmax = 64, },
175 	{ .rmin = 705600, .rmax = 768000, .wmin = 32,   .wmax = 32, },
176 };
177 
178 /*
179  * MCLK and BCLK selection based on TDM mode
180  * because of SAI we also add the restriction: MCLK >= 2 * BCLK
181  * (Table 9 from datasheet)
182  */
183 static struct imx_akcodec_tdm_fs_mul ak5558_tdm_fs_mul[] = {
184 	{ .min = 128,	.max = 128,	.mul = 256 },
185 	{ .min = 256,	.max = 256,	.mul = 512 },
186 	{ .min = 512,	.max = 512,	.mul = 1024 },
187 };
188 
189 static struct imx_akcodec_fs_mul cs42888_fs_mul[] = {
190 	{ .rmin = 8000,   .rmax = 48000,  .wmin = 256,  .wmax = 1024, },
191 	{ .rmin = 64000,  .rmax = 96000,  .wmin = 128,  .wmax = 512, },
192 	{ .rmin = 176400, .rmax = 192000, .wmin = 64,  .wmax = 256, },
193 };
194 
195 static struct imx_akcodec_tdm_fs_mul cs42888_tdm_fs_mul[] = {
196 	{ .min = 256,	.max = 256,	.mul = 256 },
197 };
198 
199 static const u32 akcodec_rates[] = {
200 	8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
201 	96000, 176400, 192000, 352800, 384000, 705600, 768000,
202 };
203 
204 static const u32 akcodec_tdm_rates[] = {
205 	8000, 16000, 32000, 48000, 96000,
206 };
207 
208 static const u32 ak4458_channels[] = {
209 	1, 2, 4, 6, 8, 10, 12, 14, 16,
210 };
211 
212 static const u32 ak4458_tdm_channels[] = {
213 	1, 2, 3, 4, 5, 6, 7, 8, 16,
214 };
215 
216 static const u32 ak5558_channels[] = {
217 	1, 2, 4, 6, 8,
218 };
219 
220 static const u32 ak5558_tdm_channels[] = {
221 	1, 2, 3, 4, 5, 6, 7, 8,
222 };
223 
224 static const u32 cs42888_channels[] = {
225 	1, 2, 4, 6, 8,
226 };
227 
228 static const u32 cs42888_tdm_channels[] = {
229 	1, 2, 3, 4, 5, 6, 7, 8,
230 };
231 
format_is_dsd(struct snd_pcm_hw_params * params)232 static bool format_is_dsd(struct snd_pcm_hw_params *params)
233 {
234 	snd_pcm_format_t format = params_format(params);
235 
236 	switch (format) {
237 	case SNDRV_PCM_FORMAT_DSD_U8:
238 	case SNDRV_PCM_FORMAT_DSD_U16_LE:
239 	case SNDRV_PCM_FORMAT_DSD_U16_BE:
240 	case SNDRV_PCM_FORMAT_DSD_U32_LE:
241 	case SNDRV_PCM_FORMAT_DSD_U32_BE:
242 		return true;
243 	default:
244 		return false;
245 	}
246 }
247 
format_is_tdm(struct dai_link_data * link_data)248 static bool format_is_tdm(struct dai_link_data *link_data)
249 {
250 	if (link_data->slots > 2)
251 		return true;
252 	else
253 		return false;
254 }
255 
codec_is_akcodec(unsigned int type)256 static bool codec_is_akcodec(unsigned int type)
257 {
258 	switch (type) {
259 	case CODEC_AK4458:
260 	case CODEC_AK4497:
261 	case CODEC_AK5558:
262 	case CODEC_AK5552:
263 	case CODEC_CS42888:
264 		return true;
265 	default:
266 		break;
267 	}
268 	return false;
269 }
270 
akcodec_get_mclk_rate(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,int slots,int slot_width)271 static unsigned long akcodec_get_mclk_rate(struct snd_pcm_substream *substream,
272 					   struct snd_pcm_hw_params *params,
273 					   int slots, int slot_width)
274 {
275 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
276 	struct imx_card_data *data = snd_soc_card_get_drvdata(rtd->card);
277 	const struct imx_card_plat_data *plat_data = data->plat_data;
278 	struct dai_link_data *link_data = &data->link_data[rtd->id];
279 	unsigned int width = slots * slot_width;
280 	unsigned int rate = params_rate(params);
281 	int i;
282 
283 	if (format_is_tdm(link_data)) {
284 		for (i = 0; i < plat_data->num_tdm_fs_mul; i++) {
285 			/* min = max = slots * slots_width */
286 			if (width != plat_data->tdm_fs_mul[i].min)
287 				continue;
288 			return rate * plat_data->tdm_fs_mul[i].mul;
289 		}
290 	} else {
291 		for (i = 0; i < plat_data->num_fs_mul; i++) {
292 			if (rate >= plat_data->fs_mul[i].rmin &&
293 			    rate <= plat_data->fs_mul[i].rmax) {
294 				width = max(width, plat_data->fs_mul[i].wmin);
295 				width = min(width, plat_data->fs_mul[i].wmax);
296 
297 				/* Adjust SAI bclk:mclk ratio */
298 				width *= link_data->one2one_ratio ? 1 : 2;
299 
300 				return rate * width;
301 			}
302 		}
303 	}
304 
305 	/* Let DAI manage clk frequency by default */
306 	return 0;
307 }
308 
imx_aif_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)309 static int imx_aif_hw_params(struct snd_pcm_substream *substream,
310 			     struct snd_pcm_hw_params *params)
311 {
312 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
313 	struct snd_soc_dai *cpu_dai = snd_soc_rtd_to_cpu(rtd, 0);
314 	struct snd_soc_card *card = rtd->card;
315 	struct imx_card_data *data = snd_soc_card_get_drvdata(card);
316 	struct dai_link_data *link_data = &data->link_data[rtd->id];
317 	struct imx_card_plat_data *plat_data = data->plat_data;
318 	struct device *dev = card->dev;
319 	struct snd_soc_dai *codec_dai;
320 	unsigned long mclk_freq;
321 	unsigned int fmt = rtd->dai_link->dai_fmt;
322 	unsigned int slots, slot_width;
323 	int ret, i;
324 
325 	slots = link_data->slots;
326 	slot_width = link_data->slot_width;
327 
328 	if (!format_is_tdm(link_data)) {
329 		if (format_is_dsd(params)) {
330 			slots = 1;
331 			slot_width = params_width(params);
332 			fmt = (rtd->dai_link->dai_fmt & ~SND_SOC_DAIFMT_FORMAT_MASK) |
333 			      SND_SOC_DAIFMT_PDM;
334 		} else {
335 			slots = 2;
336 			slot_width = params_physical_width(params);
337 			fmt = (rtd->dai_link->dai_fmt & ~SND_SOC_DAIFMT_FORMAT_MASK) |
338 			      SND_SOC_DAIFMT_I2S;
339 		}
340 	}
341 
342 	ret = snd_soc_dai_set_fmt(cpu_dai, snd_soc_daifmt_clock_provider_flipped(fmt));
343 	if (ret && ret != -ENOTSUPP) {
344 		dev_err(dev, "failed to set cpu dai fmt: %d\n", ret);
345 		return ret;
346 	}
347 	ret = snd_soc_dai_set_tdm_slot(cpu_dai,
348 				       BIT(slots) - 1,
349 				       BIT(slots) - 1,
350 				       slots, slot_width);
351 	if (ret && ret != -ENOTSUPP) {
352 		dev_err(dev, "failed to set cpu dai tdm slot: %d\n", ret);
353 		return ret;
354 	}
355 
356 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
357 		ret = snd_soc_dai_set_fmt(codec_dai, fmt);
358 		if (ret && ret != -ENOTSUPP) {
359 			dev_err(dev, "failed to set codec dai[%d] fmt: %d\n", i, ret);
360 			return ret;
361 		}
362 
363 		if (format_is_tdm(link_data)) {
364 			ret = snd_soc_dai_set_tdm_slot(codec_dai,
365 						       BIT(slots) - 1,
366 						       BIT(slots) - 1,
367 						       slots, slot_width);
368 			if (ret && ret != -ENOTSUPP) {
369 				dev_err(dev, "failed to set codec dai[%d] tdm slot: %d\n", i, ret);
370 				return ret;
371 			}
372 		}
373 	}
374 
375 	/* Set MCLK freq */
376 	if (codec_is_akcodec(plat_data->type))
377 		mclk_freq = akcodec_get_mclk_rate(substream, params, slots, slot_width);
378 	else
379 		mclk_freq = params_rate(params) * slots * slot_width;
380 
381 	if (format_is_dsd(params)) {
382 		/* Use the maximum freq from DSD512 (512*44100 = 22579200) */
383 		if (!(params_rate(params) % 11025))
384 			mclk_freq = IMX_CARD_MCLK_22P5792MHZ;
385 		else
386 			mclk_freq = IMX_CARD_MCLK_24P576MHZ;
387 	}
388 
389 	ret = snd_soc_dai_set_sysclk(cpu_dai, link_data->cpu_sysclk_id, mclk_freq,
390 				     SND_SOC_CLOCK_OUT);
391 	if (ret && ret != -ENOTSUPP) {
392 		dev_err(dev, "failed to set cpui dai mclk1 rate (%lu): %d\n", mclk_freq, ret);
393 		return ret;
394 	}
395 	ret = snd_soc_dai_set_sysclk(codec_dai, 0, mclk_freq, SND_SOC_CLOCK_IN);
396 	if (ret && ret != -ENOTSUPP) {
397 		dev_err(dev, "failed to set codec dai mclk rate (%lu): %d\n", mclk_freq, ret);
398 		return ret;
399 	}
400 
401 	return 0;
402 }
403 
ak5558_hw_rule_rate(struct snd_pcm_hw_params * p,struct snd_pcm_hw_rule * r)404 static int ak5558_hw_rule_rate(struct snd_pcm_hw_params *p, struct snd_pcm_hw_rule *r)
405 {
406 	struct dai_link_data *link_data = r->private;
407 	struct snd_interval t = { .min = 8000, .max = 8000, };
408 	unsigned long mclk_freq;
409 	unsigned int fs;
410 	int i;
411 
412 	fs = hw_param_interval(p, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min;
413 	fs *= link_data->slots;
414 
415 	/* Identify maximum supported rate */
416 	for (i = 0; i < ARRAY_SIZE(akcodec_rates); i++) {
417 		mclk_freq = fs * akcodec_rates[i];
418 		/* Adjust SAI bclk:mclk ratio */
419 		mclk_freq *= link_data->one2one_ratio ? 1 : 2;
420 
421 		/* Skip rates for which MCLK is beyond supported value */
422 		if (mclk_freq > 36864000)
423 			continue;
424 
425 		if (t.max < akcodec_rates[i])
426 			t.max = akcodec_rates[i];
427 	}
428 
429 	return snd_interval_refine(hw_param_interval(p, r->var), &t);
430 }
431 
imx_aif_startup(struct snd_pcm_substream * substream)432 static int imx_aif_startup(struct snd_pcm_substream *substream)
433 {
434 	struct snd_pcm_runtime *runtime = substream->runtime;
435 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
436 	struct snd_soc_card *card = rtd->card;
437 	struct imx_card_data *data = snd_soc_card_get_drvdata(card);
438 	struct dai_link_data *link_data = &data->link_data[rtd->id];
439 	static struct snd_pcm_hw_constraint_list constraint_rates;
440 	static struct snd_pcm_hw_constraint_list constraint_channels;
441 	int ret = 0;
442 
443 	if (format_is_tdm(link_data)) {
444 		constraint_channels.list = data->plat_data->support_tdm_channels;
445 		constraint_channels.count = data->plat_data->num_tdm_channels;
446 		constraint_rates.list = data->plat_data->support_tdm_rates;
447 		constraint_rates.count = data->plat_data->num_tdm_rates;
448 	} else {
449 		constraint_channels.list = data->plat_data->support_channels;
450 		constraint_channels.count = data->plat_data->num_channels;
451 		constraint_rates.list = data->plat_data->support_rates;
452 		constraint_rates.count = data->plat_data->num_rates;
453 	}
454 
455 	if (constraint_channels.count) {
456 		ret = snd_pcm_hw_constraint_list(runtime, 0,
457 						 SNDRV_PCM_HW_PARAM_CHANNELS,
458 						 &constraint_channels);
459 		if (ret)
460 			return ret;
461 	}
462 
463 	if (constraint_rates.count) {
464 		ret = snd_pcm_hw_constraint_list(runtime, 0,
465 						 SNDRV_PCM_HW_PARAM_RATE,
466 						 &constraint_rates);
467 		if (ret)
468 			return ret;
469 	}
470 
471 	if (data->plat_data->type == CODEC_AK5558)
472 		ret = snd_pcm_hw_rule_add(substream->runtime, 0,
473 					  SNDRV_PCM_HW_PARAM_RATE,
474 					  ak5558_hw_rule_rate, link_data,
475 					  SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
476 
477 	return ret;
478 }
479 
480 static const struct snd_soc_ops imx_aif_ops = {
481 	.hw_params = imx_aif_hw_params,
482 	.startup = imx_aif_startup,
483 };
484 
485 static const struct snd_soc_ops imx_aif_ops_be = {
486 	.hw_params = imx_aif_hw_params,
487 };
488 
be_hw_params_fixup(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_hw_params * params)489 static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
490 			      struct snd_pcm_hw_params *params)
491 {
492 	struct snd_soc_card *card = rtd->card;
493 	struct imx_card_data *data = snd_soc_card_get_drvdata(card);
494 	struct snd_interval *rate;
495 	struct snd_mask *mask;
496 
497 	rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
498 	rate->max = data->asrc_rate;
499 	rate->min = data->asrc_rate;
500 
501 	mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
502 	snd_mask_none(mask);
503 	snd_mask_set(mask, (__force unsigned int)data->asrc_format);
504 
505 	return 0;
506 }
507 
imx_card_parse_of(struct imx_card_data * data)508 static int imx_card_parse_of(struct imx_card_data *data)
509 {
510 	struct imx_card_plat_data *plat_data = data->plat_data;
511 	struct snd_soc_card *card = &data->card;
512 	struct snd_soc_dai_link_component *dlc;
513 	struct device_node *platform = NULL;
514 	struct device_node *codec = NULL;
515 	struct device_node *cpu = NULL;
516 	struct device_node *np;
517 	struct device *dev = card->dev;
518 	struct snd_soc_dai_link *link;
519 	struct dai_link_data *link_data;
520 	struct of_phandle_args args;
521 	bool playback_only, capture_only;
522 	int ret, num_links;
523 	u32 asrc_fmt = 0;
524 	u32 width;
525 
526 	ret = snd_soc_of_parse_card_name(card, "model");
527 	if (ret) {
528 		dev_err(dev, "Error parsing card name: %d\n", ret);
529 		return ret;
530 	}
531 
532 	/* DAPM routes */
533 	if (of_property_present(dev->of_node, "audio-routing")) {
534 		ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
535 		if (ret)
536 			return ret;
537 	}
538 
539 	/* Populate links */
540 	num_links = of_get_child_count(dev->of_node);
541 
542 	/* Allocate the DAI link array */
543 	card->dai_link = devm_kcalloc(dev, num_links, sizeof(*link), GFP_KERNEL);
544 	if (!card->dai_link)
545 		return -ENOMEM;
546 
547 	data->link_data = devm_kcalloc(dev, num_links, sizeof(*link_data), GFP_KERNEL);
548 	if (!data->link_data)
549 		return -ENOMEM;
550 
551 	card->num_links = num_links;
552 	link = card->dai_link;
553 	link_data = data->link_data;
554 
555 	for_each_child_of_node(dev->of_node, np) {
556 		dlc = devm_kzalloc(dev, 2 * sizeof(*dlc), GFP_KERNEL);
557 		if (!dlc) {
558 			ret = -ENOMEM;
559 			goto err_put_np;
560 		}
561 
562 		link->cpus	= &dlc[0];
563 		link->platforms	= &dlc[1];
564 
565 		link->num_cpus		= 1;
566 		link->num_platforms	= 1;
567 
568 		ret = of_property_read_string(np, "link-name", &link->name);
569 		if (ret) {
570 			dev_err(card->dev, "error getting codec dai_link name\n");
571 			goto err_put_np;
572 		}
573 
574 		cpu = of_get_child_by_name(np, "cpu");
575 		if (!cpu) {
576 			dev_err(dev, "%s: Can't find cpu DT node\n", link->name);
577 			ret = -EINVAL;
578 			goto err;
579 		}
580 
581 		ret = snd_soc_of_get_dlc(cpu, &args, link->cpus, 0);
582 		if (ret) {
583 			dev_err_probe(card->dev, ret,
584 				      "%s: error getting cpu dai info\n", link->name);
585 			goto err;
586 		}
587 
588 		if (of_node_name_eq(args.np, "sai")) {
589 			/* sai sysclk id */
590 			link_data->cpu_sysclk_id = FSL_SAI_CLK_MAST1;
591 
592 			/* sai may support mclk/bclk = 1 */
593 			if (of_property_read_bool(np, "fsl,mclk-equal-bclk")) {
594 				link_data->one2one_ratio = true;
595 			} else {
596 				int i;
597 
598 				/*
599 				 * i.MX8MQ don't support one2one ratio, then
600 				 * with ak4497 only 16bit case is supported.
601 				 */
602 				for (i = 0; i < ARRAY_SIZE(ak4497_fs_mul); i++) {
603 					if (ak4497_fs_mul[i].rmin == 705600 &&
604 					    ak4497_fs_mul[i].rmax == 768000) {
605 						ak4497_fs_mul[i].wmin = 32;
606 						ak4497_fs_mul[i].wmax = 32;
607 					}
608 				}
609 			}
610 		}
611 
612 		link->platforms->of_node = link->cpus->of_node;
613 		link->id = args.args[0];
614 
615 		codec = of_get_child_by_name(np, "codec");
616 		if (codec) {
617 			ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
618 			if (ret < 0) {
619 				dev_err_probe(dev, ret, "%s: codec dai not found\n",
620 						link->name);
621 				goto err;
622 			}
623 
624 			plat_data->num_codecs = link->num_codecs;
625 
626 			/* Check the akcodec type */
627 			if (!strcmp(link->codecs->dai_name, "ak4458-aif"))
628 				plat_data->type = CODEC_AK4458;
629 			else if (!strcmp(link->codecs->dai_name, "ak4497-aif"))
630 				plat_data->type = CODEC_AK4497;
631 			else if (!strcmp(link->codecs->dai_name, "ak5558-aif"))
632 				plat_data->type = CODEC_AK5558;
633 			else if (!strcmp(link->codecs->dai_name, "ak5552-aif"))
634 				plat_data->type = CODEC_AK5552;
635 			else if (!strcmp(link->codecs->dai_name, "cs42888"))
636 				plat_data->type = CODEC_CS42888;
637 
638 		} else {
639 			link->codecs	 = &snd_soc_dummy_dlc;
640 			link->num_codecs = 1;
641 		}
642 
643 		if (!strncmp(link->name, "HiFi-ASRC-FE", 12)) {
644 			/* DPCM frontend */
645 			link->dynamic = 1;
646 			link->dpcm_merged_chan = 1;
647 
648 			ret = of_property_read_u32(args.np, "fsl,asrc-rate", &data->asrc_rate);
649 			if (ret) {
650 				dev_err(dev, "failed to get output rate\n");
651 				ret = -EINVAL;
652 				goto err;
653 			}
654 
655 			ret = of_property_read_u32(args.np, "fsl,asrc-format", &asrc_fmt);
656 			data->asrc_format = (__force snd_pcm_format_t)asrc_fmt;
657 			if (ret) {
658 				/* Fallback to old binding; translate to asrc_format */
659 				ret = of_property_read_u32(args.np, "fsl,asrc-width", &width);
660 				if (ret) {
661 					dev_err(dev,
662 						"failed to decide output format\n");
663 					goto err;
664 				}
665 
666 				if (width == 24)
667 					data->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
668 				else
669 					data->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
670 			}
671 		} else if (!strncmp(link->name, "HiFi-ASRC-BE", 12)) {
672 			/* DPCM backend */
673 			/*
674 			 * No need to have link->platforms. alloced dlc[1] will be just wasted,
675 			 * but it won't leak.
676 			 */
677 			link->no_pcm = 1;
678 			link->platforms = NULL;
679 
680 			link->be_hw_params_fixup = be_hw_params_fixup;
681 			link->ops = &imx_aif_ops_be;
682 		} else {
683 			link->ops = &imx_aif_ops;
684 		}
685 
686 		graph_util_parse_link_direction(np, &playback_only, &capture_only);
687 		link->playback_only = playback_only;
688 		link->capture_only = capture_only;
689 
690 		/* Get dai fmt */
691 		ret = simple_util_parse_daifmt(dev, np, codec,
692 					       NULL, &link->dai_fmt);
693 		if (ret)
694 			link->dai_fmt = SND_SOC_DAIFMT_NB_NF |
695 					SND_SOC_DAIFMT_CBC_CFC |
696 					SND_SOC_DAIFMT_I2S;
697 
698 		/* Get tdm slot */
699 		snd_soc_of_parse_tdm_slot(np, NULL, NULL,
700 					  &link_data->slots,
701 					  &link_data->slot_width);
702 		/* default value */
703 		if (!link_data->slots)
704 			link_data->slots = 2;
705 
706 		if (!link_data->slot_width)
707 			link_data->slot_width = 32;
708 
709 		link->ignore_pmdown_time = 1;
710 		link->stream_name = link->name;
711 		link++;
712 		link_data++;
713 
714 		of_node_put(cpu);
715 		of_node_put(codec);
716 		of_node_put(platform);
717 
718 		cpu = NULL;
719 		codec = NULL;
720 		platform = NULL;
721 	}
722 
723 	return 0;
724 err:
725 	of_node_put(cpu);
726 	of_node_put(codec);
727 	of_node_put(platform);
728 err_put_np:
729 	of_node_put(np);
730 	return ret;
731 }
732 
imx_card_probe(struct platform_device * pdev)733 static int imx_card_probe(struct platform_device *pdev)
734 {
735 	struct snd_soc_dai_link *link_be = NULL, *link;
736 	struct imx_card_plat_data *plat_data;
737 	struct imx_card_data *data;
738 	int ret, i;
739 
740 	data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
741 	if (!data)
742 		return -ENOMEM;
743 
744 	plat_data = devm_kzalloc(&pdev->dev, sizeof(*plat_data), GFP_KERNEL);
745 	if (!plat_data)
746 		return -ENOMEM;
747 
748 	data->plat_data = plat_data;
749 	data->card.dev = &pdev->dev;
750 	data->card.owner = THIS_MODULE;
751 
752 	dev_set_drvdata(&pdev->dev, &data->card);
753 	snd_soc_card_set_drvdata(&data->card, data);
754 	ret = imx_card_parse_of(data);
755 	if (ret)
756 		return ret;
757 
758 	data->num_dapm_routes = plat_data->num_codecs + 1;
759 	data->dapm_routes = devm_kcalloc(&pdev->dev, data->num_dapm_routes,
760 					 sizeof(struct snd_soc_dapm_route),
761 					 GFP_KERNEL);
762 	if (!data->dapm_routes)
763 		return -ENOMEM;
764 
765 	/* configure the dapm routes */
766 	switch (plat_data->type) {
767 	case CODEC_AK4458:
768 	case CODEC_AK4497:
769 		if (plat_data->num_codecs == 1) {
770 			data->dapm_routes[0].sink = "Playback";
771 			data->dapm_routes[0].source = "CPU-Playback";
772 			i = 1;
773 		} else {
774 			for (i = 0; i < plat_data->num_codecs; i++) {
775 				data->dapm_routes[i].sink =
776 					devm_kasprintf(&pdev->dev, GFP_KERNEL, "%d %s",
777 						       i + 1, "Playback");
778 				if (!data->dapm_routes[i].sink)
779 					return -ENOMEM;
780 				data->dapm_routes[i].source = "CPU-Playback";
781 			}
782 		}
783 		data->dapm_routes[i].sink = "CPU-Playback";
784 		data->dapm_routes[i].source = "ASRC-Playback";
785 		break;
786 	case CODEC_AK5558:
787 	case CODEC_AK5552:
788 		if (plat_data->num_codecs == 1) {
789 			data->dapm_routes[0].sink = "CPU-Capture";
790 			data->dapm_routes[0].source = "Capture";
791 			i = 1;
792 		} else {
793 			for (i = 0; i < plat_data->num_codecs; i++) {
794 				data->dapm_routes[i].source =
795 					devm_kasprintf(&pdev->dev, GFP_KERNEL, "%d %s",
796 						       i + 1, "Capture");
797 				if (!data->dapm_routes[i].source)
798 					return -ENOMEM;
799 				data->dapm_routes[i].sink = "CPU-Capture";
800 			}
801 		}
802 		data->dapm_routes[i].sink = "ASRC-Capture";
803 		data->dapm_routes[i].source = "CPU-Capture";
804 		break;
805 	case CODEC_CS42888:
806 		data->dapm_routes[0].sink = "Playback";
807 		data->dapm_routes[0].source = "CPU-Playback";
808 		data->dapm_routes[1].sink = "CPU-Capture";
809 		data->dapm_routes[1].source = "Capture";
810 		break;
811 	default:
812 		break;
813 	}
814 
815 	/* default platform data for akcodecs */
816 	if (codec_is_akcodec(plat_data->type)) {
817 		plat_data->support_rates = akcodec_rates;
818 		plat_data->num_rates = ARRAY_SIZE(akcodec_rates);
819 		plat_data->support_tdm_rates = akcodec_tdm_rates;
820 		plat_data->num_tdm_rates = ARRAY_SIZE(akcodec_tdm_rates);
821 
822 		switch (plat_data->type) {
823 		case CODEC_AK4458:
824 			plat_data->fs_mul = ak4458_fs_mul;
825 			plat_data->num_fs_mul = ARRAY_SIZE(ak4458_fs_mul);
826 			plat_data->tdm_fs_mul = ak4458_tdm_fs_mul;
827 			plat_data->num_tdm_fs_mul = ARRAY_SIZE(ak4458_tdm_fs_mul);
828 			plat_data->support_channels = ak4458_channels;
829 			plat_data->num_channels = ARRAY_SIZE(ak4458_channels);
830 			plat_data->support_tdm_channels = ak4458_tdm_channels;
831 			plat_data->num_tdm_channels = ARRAY_SIZE(ak4458_tdm_channels);
832 			break;
833 		case CODEC_AK4497:
834 			plat_data->fs_mul = ak4497_fs_mul;
835 			plat_data->num_fs_mul = ARRAY_SIZE(ak4497_fs_mul);
836 			plat_data->support_channels = ak4458_channels;
837 			plat_data->num_channels = ARRAY_SIZE(ak4458_channels);
838 			break;
839 		case CODEC_AK5558:
840 		case CODEC_AK5552:
841 			plat_data->fs_mul = ak5558_fs_mul;
842 			plat_data->num_fs_mul = ARRAY_SIZE(ak5558_fs_mul);
843 			plat_data->tdm_fs_mul = ak5558_tdm_fs_mul;
844 			plat_data->num_tdm_fs_mul = ARRAY_SIZE(ak5558_tdm_fs_mul);
845 			plat_data->support_channels = ak5558_channels;
846 			plat_data->num_channels = ARRAY_SIZE(ak5558_channels);
847 			plat_data->support_tdm_channels = ak5558_tdm_channels;
848 			plat_data->num_tdm_channels = ARRAY_SIZE(ak5558_tdm_channels);
849 			break;
850 		case CODEC_CS42888:
851 			plat_data->fs_mul = cs42888_fs_mul;
852 			plat_data->num_fs_mul = ARRAY_SIZE(cs42888_fs_mul);
853 			plat_data->tdm_fs_mul = cs42888_tdm_fs_mul;
854 			plat_data->num_tdm_fs_mul = ARRAY_SIZE(cs42888_tdm_fs_mul);
855 			plat_data->support_channels = cs42888_channels;
856 			plat_data->num_channels = ARRAY_SIZE(cs42888_channels);
857 			plat_data->support_tdm_channels = cs42888_tdm_channels;
858 			plat_data->num_tdm_channels = ARRAY_SIZE(cs42888_tdm_channels);
859 			break;
860 		default:
861 			break;
862 		}
863 	}
864 
865 	/* with asrc as front end */
866 	if (data->card.num_links == 3) {
867 		data->card.dapm_routes = data->dapm_routes;
868 		data->card.num_dapm_routes = data->num_dapm_routes;
869 		for_each_card_prelinks(&data->card, i, link) {
870 			if (link->no_pcm == 1)
871 				link_be = link;
872 		}
873 		for_each_card_prelinks(&data->card, i, link) {
874 			if (link->dynamic == 1 && link_be) {
875 				link->playback_only = link_be->playback_only;
876 				link->capture_only  = link_be->capture_only;
877 			}
878 		}
879 	}
880 
881 	ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
882 	if (ret)
883 		return dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
884 
885 	return 0;
886 }
887 
888 static const struct of_device_id imx_card_dt_ids[] = {
889 	{ .compatible = "fsl,imx-audio-card", },
890 	{ },
891 };
892 MODULE_DEVICE_TABLE(of, imx_card_dt_ids);
893 
894 static struct platform_driver imx_card_driver = {
895 	.driver = {
896 		.name = "imx-card",
897 		.pm = &snd_soc_pm_ops,
898 		.of_match_table = imx_card_dt_ids,
899 	},
900 	.probe = imx_card_probe,
901 };
902 module_platform_driver(imx_card_driver);
903 
904 MODULE_DESCRIPTION("Freescale i.MX ASoC Machine Driver");
905 MODULE_LICENSE("GPL v2");
906 MODULE_ALIAS("platform:imx-card");
907