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