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_device.h>
9 #include <linux/i2c.h>
10 #include <linux/of_gpio.h>
11 #include <linux/clk.h>
12 #include <sound/soc.h>
13 #include <sound/pcm_params.h>
14 #include <sound/pcm.h>
15 #include <sound/soc-dapm.h>
16 #include <sound/simple_card_utils.h>
17
18 #include "fsl_sai.h"
19
20 #define IMX_CARD_MCLK_22P5792MHZ 22579200
21 #define IMX_CARD_MCLK_24P576MHZ 24576000
22
23 enum codec_type {
24 CODEC_DUMMY = 0,
25 CODEC_AK5558 = 1,
26 CODEC_AK4458,
27 CODEC_AK4497,
28 CODEC_AK5552,
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 const u32 akcodec_rates[] = {
190 8000, 11025, 16000, 22050, 32000, 44100, 48000, 88200,
191 96000, 176400, 192000, 352800, 384000, 705600, 768000,
192 };
193
194 static const u32 akcodec_tdm_rates[] = {
195 8000, 16000, 32000, 48000, 96000,
196 };
197
198 static const u32 ak4458_channels[] = {
199 1, 2, 4, 6, 8, 10, 12, 14, 16,
200 };
201
202 static const u32 ak4458_tdm_channels[] = {
203 1, 2, 3, 4, 5, 6, 7, 8, 16,
204 };
205
206 static const u32 ak5558_channels[] = {
207 1, 2, 4, 6, 8,
208 };
209
210 static const u32 ak5558_tdm_channels[] = {
211 1, 2, 3, 4, 5, 6, 7, 8,
212 };
213
format_is_dsd(struct snd_pcm_hw_params * params)214 static bool format_is_dsd(struct snd_pcm_hw_params *params)
215 {
216 snd_pcm_format_t format = params_format(params);
217
218 switch (format) {
219 case SNDRV_PCM_FORMAT_DSD_U8:
220 case SNDRV_PCM_FORMAT_DSD_U16_LE:
221 case SNDRV_PCM_FORMAT_DSD_U16_BE:
222 case SNDRV_PCM_FORMAT_DSD_U32_LE:
223 case SNDRV_PCM_FORMAT_DSD_U32_BE:
224 return true;
225 default:
226 return false;
227 }
228 }
229
format_is_tdm(struct dai_link_data * link_data)230 static bool format_is_tdm(struct dai_link_data *link_data)
231 {
232 if (link_data->slots > 2)
233 return true;
234 else
235 return false;
236 }
237
codec_is_akcodec(unsigned int type)238 static bool codec_is_akcodec(unsigned int type)
239 {
240 switch (type) {
241 case CODEC_AK4458:
242 case CODEC_AK4497:
243 case CODEC_AK5558:
244 case CODEC_AK5552:
245 return true;
246 default:
247 break;
248 }
249 return false;
250 }
251
akcodec_get_mclk_rate(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params,int slots,int slot_width)252 static unsigned long akcodec_get_mclk_rate(struct snd_pcm_substream *substream,
253 struct snd_pcm_hw_params *params,
254 int slots, int slot_width)
255 {
256 struct snd_soc_pcm_runtime *rtd = substream->private_data;
257 struct imx_card_data *data = snd_soc_card_get_drvdata(rtd->card);
258 const struct imx_card_plat_data *plat_data = data->plat_data;
259 struct dai_link_data *link_data = &data->link_data[rtd->num];
260 unsigned int width = slots * slot_width;
261 unsigned int rate = params_rate(params);
262 int i;
263
264 if (format_is_tdm(link_data)) {
265 for (i = 0; i < plat_data->num_tdm_fs_mul; i++) {
266 /* min = max = slots * slots_width */
267 if (width != plat_data->tdm_fs_mul[i].min)
268 continue;
269 return rate * plat_data->tdm_fs_mul[i].mul;
270 }
271 } else {
272 for (i = 0; i < plat_data->num_fs_mul; i++) {
273 if (rate >= plat_data->fs_mul[i].rmin &&
274 rate <= plat_data->fs_mul[i].rmax) {
275 width = max(width, plat_data->fs_mul[i].wmin);
276 width = min(width, plat_data->fs_mul[i].wmax);
277
278 /* Adjust SAI bclk:mclk ratio */
279 width *= link_data->one2one_ratio ? 1 : 2;
280
281 return rate * width;
282 }
283 }
284 }
285
286 /* Let DAI manage clk frequency by default */
287 return 0;
288 }
289
imx_aif_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)290 static int imx_aif_hw_params(struct snd_pcm_substream *substream,
291 struct snd_pcm_hw_params *params)
292 {
293 struct snd_soc_pcm_runtime *rtd = substream->private_data;
294 struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
295 struct snd_soc_card *card = rtd->card;
296 struct imx_card_data *data = snd_soc_card_get_drvdata(card);
297 struct dai_link_data *link_data = &data->link_data[rtd->num];
298 struct imx_card_plat_data *plat_data = data->plat_data;
299 struct device *dev = card->dev;
300 struct snd_soc_dai *codec_dai;
301 unsigned long mclk_freq;
302 unsigned int fmt = rtd->dai_link->dai_fmt;
303 unsigned int slots, slot_width;
304 int ret, i;
305
306 slots = link_data->slots;
307 slot_width = link_data->slot_width;
308
309 if (!format_is_tdm(link_data)) {
310 if (format_is_dsd(params)) {
311 slots = 1;
312 slot_width = params_width(params);
313 fmt = (rtd->dai_link->dai_fmt & ~SND_SOC_DAIFMT_FORMAT_MASK) |
314 SND_SOC_DAIFMT_PDM;
315 } else {
316 slots = 2;
317 slot_width = params_physical_width(params);
318 fmt = (rtd->dai_link->dai_fmt & ~SND_SOC_DAIFMT_FORMAT_MASK) |
319 SND_SOC_DAIFMT_I2S;
320 }
321 }
322
323 ret = snd_soc_dai_set_fmt(cpu_dai, snd_soc_daifmt_clock_provider_flipped(fmt));
324 if (ret && ret != -ENOTSUPP) {
325 dev_err(dev, "failed to set cpu dai fmt: %d\n", ret);
326 return ret;
327 }
328 ret = snd_soc_dai_set_tdm_slot(cpu_dai,
329 BIT(slots) - 1,
330 BIT(slots) - 1,
331 slots, slot_width);
332 if (ret && ret != -ENOTSUPP) {
333 dev_err(dev, "failed to set cpu dai tdm slot: %d\n", ret);
334 return ret;
335 }
336
337 for_each_rtd_codec_dais(rtd, i, codec_dai) {
338 ret = snd_soc_dai_set_fmt(codec_dai, fmt);
339 if (ret && ret != -ENOTSUPP) {
340 dev_err(dev, "failed to set codec dai[%d] fmt: %d\n", i, ret);
341 return ret;
342 }
343
344 ret = snd_soc_dai_set_tdm_slot(codec_dai,
345 BIT(slots) - 1,
346 BIT(slots) - 1,
347 slots, slot_width);
348 if (ret && ret != -ENOTSUPP) {
349 dev_err(dev, "failed to set codec dai[%d] tdm slot: %d\n", i, ret);
350 return ret;
351 }
352 }
353
354 /* Set MCLK freq */
355 if (codec_is_akcodec(plat_data->type))
356 mclk_freq = akcodec_get_mclk_rate(substream, params, slots, slot_width);
357 else
358 mclk_freq = params_rate(params) * slots * slot_width;
359
360 if (format_is_dsd(params)) {
361 /* Use the maximum freq from DSD512 (512*44100 = 22579200) */
362 if (!(params_rate(params) % 11025))
363 mclk_freq = IMX_CARD_MCLK_22P5792MHZ;
364 else
365 mclk_freq = IMX_CARD_MCLK_24P576MHZ;
366 }
367
368 ret = snd_soc_dai_set_sysclk(cpu_dai, link_data->cpu_sysclk_id, mclk_freq,
369 SND_SOC_CLOCK_OUT);
370 if (ret && ret != -ENOTSUPP) {
371 dev_err(dev, "failed to set cpui dai mclk1 rate (%lu): %d\n", mclk_freq, ret);
372 return ret;
373 }
374
375 return 0;
376 }
377
ak5558_hw_rule_rate(struct snd_pcm_hw_params * p,struct snd_pcm_hw_rule * r)378 static int ak5558_hw_rule_rate(struct snd_pcm_hw_params *p, struct snd_pcm_hw_rule *r)
379 {
380 struct dai_link_data *link_data = r->private;
381 struct snd_interval t = { .min = 8000, .max = 8000, };
382 unsigned long mclk_freq;
383 unsigned int fs;
384 int i;
385
386 fs = hw_param_interval(p, SNDRV_PCM_HW_PARAM_SAMPLE_BITS)->min;
387 fs *= link_data->slots;
388
389 /* Identify maximum supported rate */
390 for (i = 0; i < ARRAY_SIZE(akcodec_rates); i++) {
391 mclk_freq = fs * akcodec_rates[i];
392 /* Adjust SAI bclk:mclk ratio */
393 mclk_freq *= link_data->one2one_ratio ? 1 : 2;
394
395 /* Skip rates for which MCLK is beyond supported value */
396 if (mclk_freq > 36864000)
397 continue;
398
399 if (t.max < akcodec_rates[i])
400 t.max = akcodec_rates[i];
401 }
402
403 return snd_interval_refine(hw_param_interval(p, r->var), &t);
404 }
405
imx_aif_startup(struct snd_pcm_substream * substream)406 static int imx_aif_startup(struct snd_pcm_substream *substream)
407 {
408 struct snd_pcm_runtime *runtime = substream->runtime;
409 struct snd_soc_pcm_runtime *rtd = substream->private_data;
410 struct snd_soc_card *card = rtd->card;
411 struct imx_card_data *data = snd_soc_card_get_drvdata(card);
412 struct dai_link_data *link_data = &data->link_data[rtd->num];
413 static struct snd_pcm_hw_constraint_list constraint_rates;
414 static struct snd_pcm_hw_constraint_list constraint_channels;
415 int ret = 0;
416
417 if (format_is_tdm(link_data)) {
418 constraint_channels.list = data->plat_data->support_tdm_channels;
419 constraint_channels.count = data->plat_data->num_tdm_channels;
420 constraint_rates.list = data->plat_data->support_tdm_rates;
421 constraint_rates.count = data->plat_data->num_tdm_rates;
422 } else {
423 constraint_channels.list = data->plat_data->support_channels;
424 constraint_channels.count = data->plat_data->num_channels;
425 constraint_rates.list = data->plat_data->support_rates;
426 constraint_rates.count = data->plat_data->num_rates;
427 }
428
429 if (constraint_channels.count) {
430 ret = snd_pcm_hw_constraint_list(runtime, 0,
431 SNDRV_PCM_HW_PARAM_CHANNELS,
432 &constraint_channels);
433 if (ret)
434 return ret;
435 }
436
437 if (constraint_rates.count) {
438 ret = snd_pcm_hw_constraint_list(runtime, 0,
439 SNDRV_PCM_HW_PARAM_RATE,
440 &constraint_rates);
441 if (ret)
442 return ret;
443 }
444
445 if (data->plat_data->type == CODEC_AK5558)
446 ret = snd_pcm_hw_rule_add(substream->runtime, 0,
447 SNDRV_PCM_HW_PARAM_RATE,
448 ak5558_hw_rule_rate, link_data,
449 SNDRV_PCM_HW_PARAM_SAMPLE_BITS, -1);
450
451 return ret;
452 }
453
454 static const struct snd_soc_ops imx_aif_ops = {
455 .hw_params = imx_aif_hw_params,
456 .startup = imx_aif_startup,
457 };
458
459 static const struct snd_soc_ops imx_aif_ops_be = {
460 .hw_params = imx_aif_hw_params,
461 };
462
be_hw_params_fixup(struct snd_soc_pcm_runtime * rtd,struct snd_pcm_hw_params * params)463 static int be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
464 struct snd_pcm_hw_params *params)
465 {
466 struct snd_soc_card *card = rtd->card;
467 struct imx_card_data *data = snd_soc_card_get_drvdata(card);
468 struct snd_interval *rate;
469 struct snd_mask *mask;
470
471 rate = hw_param_interval(params, SNDRV_PCM_HW_PARAM_RATE);
472 rate->max = data->asrc_rate;
473 rate->min = data->asrc_rate;
474
475 mask = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
476 snd_mask_none(mask);
477 snd_mask_set(mask, (__force unsigned int)data->asrc_format);
478
479 return 0;
480 }
481
imx_card_parse_of(struct imx_card_data * data)482 static int imx_card_parse_of(struct imx_card_data *data)
483 {
484 struct imx_card_plat_data *plat_data = data->plat_data;
485 struct snd_soc_card *card = &data->card;
486 struct snd_soc_dai_link_component *dlc;
487 struct device_node *platform = NULL;
488 struct device_node *codec = NULL;
489 struct device_node *cpu = NULL;
490 struct device_node *np;
491 struct device *dev = card->dev;
492 struct snd_soc_dai_link *link;
493 struct dai_link_data *link_data;
494 struct of_phandle_args args;
495 int ret, num_links;
496 u32 asrc_fmt = 0;
497 u32 width;
498
499 ret = snd_soc_of_parse_card_name(card, "model");
500 if (ret) {
501 dev_err(dev, "Error parsing card name: %d\n", ret);
502 return ret;
503 }
504
505 /* DAPM routes */
506 if (of_property_read_bool(dev->of_node, "audio-routing")) {
507 ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
508 if (ret)
509 return ret;
510 }
511
512 /* Populate links */
513 num_links = of_get_child_count(dev->of_node);
514
515 /* Allocate the DAI link array */
516 card->dai_link = devm_kcalloc(dev, num_links, sizeof(*link), GFP_KERNEL);
517 if (!card->dai_link)
518 return -ENOMEM;
519
520 data->link_data = devm_kcalloc(dev, num_links, sizeof(*link), GFP_KERNEL);
521 if (!data->link_data)
522 return -ENOMEM;
523
524 card->num_links = num_links;
525 link = card->dai_link;
526 link_data = data->link_data;
527
528 for_each_child_of_node(dev->of_node, np) {
529 dlc = devm_kzalloc(dev, 2 * sizeof(*dlc), GFP_KERNEL);
530 if (!dlc) {
531 ret = -ENOMEM;
532 goto err_put_np;
533 }
534
535 link->cpus = &dlc[0];
536 link->platforms = &dlc[1];
537
538 link->num_cpus = 1;
539 link->num_platforms = 1;
540
541 ret = of_property_read_string(np, "link-name", &link->name);
542 if (ret) {
543 dev_err(card->dev, "error getting codec dai_link name\n");
544 goto err_put_np;
545 }
546
547 cpu = of_get_child_by_name(np, "cpu");
548 if (!cpu) {
549 dev_err(dev, "%s: Can't find cpu DT node\n", link->name);
550 ret = -EINVAL;
551 goto err;
552 }
553
554 ret = snd_soc_of_get_dlc(cpu, &args, link->cpus, 0);
555 if (ret) {
556 dev_err_probe(card->dev, ret,
557 "%s: error getting cpu dai info\n", link->name);
558 goto err;
559 }
560
561 if (of_node_name_eq(args.np, "sai")) {
562 /* sai sysclk id */
563 link_data->cpu_sysclk_id = FSL_SAI_CLK_MAST1;
564
565 /* sai may support mclk/bclk = 1 */
566 if (of_property_read_bool(np, "fsl,mclk-equal-bclk")) {
567 link_data->one2one_ratio = true;
568 } else {
569 int i;
570
571 /*
572 * i.MX8MQ don't support one2one ratio, then
573 * with ak4497 only 16bit case is supported.
574 */
575 for (i = 0; i < ARRAY_SIZE(ak4497_fs_mul); i++) {
576 if (ak4497_fs_mul[i].rmin == 705600 &&
577 ak4497_fs_mul[i].rmax == 768000) {
578 ak4497_fs_mul[i].wmin = 32;
579 ak4497_fs_mul[i].wmax = 32;
580 }
581 }
582 }
583 }
584
585 link->platforms->of_node = link->cpus->of_node;
586 link->id = args.args[0];
587
588 codec = of_get_child_by_name(np, "codec");
589 if (codec) {
590 ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
591 if (ret < 0) {
592 dev_err_probe(dev, ret, "%s: codec dai not found\n",
593 link->name);
594 goto err;
595 }
596
597 plat_data->num_codecs = link->num_codecs;
598
599 /* Check the akcodec type */
600 if (!strcmp(link->codecs->dai_name, "ak4458-aif"))
601 plat_data->type = CODEC_AK4458;
602 else if (!strcmp(link->codecs->dai_name, "ak4497-aif"))
603 plat_data->type = CODEC_AK4497;
604 else if (!strcmp(link->codecs->dai_name, "ak5558-aif"))
605 plat_data->type = CODEC_AK5558;
606 else if (!strcmp(link->codecs->dai_name, "ak5552-aif"))
607 plat_data->type = CODEC_AK5552;
608
609 } else {
610 link->codecs = &asoc_dummy_dlc;
611 link->num_codecs = 1;
612 }
613
614 if (!strncmp(link->name, "HiFi-ASRC-FE", 12)) {
615 /* DPCM frontend */
616 link->dynamic = 1;
617 link->dpcm_merged_chan = 1;
618
619 ret = of_property_read_u32(args.np, "fsl,asrc-rate", &data->asrc_rate);
620 if (ret) {
621 dev_err(dev, "failed to get output rate\n");
622 ret = -EINVAL;
623 goto err;
624 }
625
626 ret = of_property_read_u32(args.np, "fsl,asrc-format", &asrc_fmt);
627 data->asrc_format = (__force snd_pcm_format_t)asrc_fmt;
628 if (ret) {
629 /* Fallback to old binding; translate to asrc_format */
630 ret = of_property_read_u32(args.np, "fsl,asrc-width", &width);
631 if (ret) {
632 dev_err(dev,
633 "failed to decide output format\n");
634 goto err;
635 }
636
637 if (width == 24)
638 data->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
639 else
640 data->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
641 }
642 } else if (!strncmp(link->name, "HiFi-ASRC-BE", 12)) {
643 /* DPCM backend */
644 link->no_pcm = 1;
645 link->platforms->of_node = NULL;
646 link->platforms->name = "snd-soc-dummy";
647
648 link->be_hw_params_fixup = be_hw_params_fixup;
649 link->ops = &imx_aif_ops_be;
650 } else {
651 link->ops = &imx_aif_ops;
652 }
653
654 if (link->no_pcm || link->dynamic)
655 snd_soc_dai_link_set_capabilities(link);
656
657 /* Get dai fmt */
658 ret = asoc_simple_parse_daifmt(dev, np, codec,
659 NULL, &link->dai_fmt);
660 if (ret)
661 link->dai_fmt = SND_SOC_DAIFMT_NB_NF |
662 SND_SOC_DAIFMT_CBC_CFC |
663 SND_SOC_DAIFMT_I2S;
664
665 /* Get tdm slot */
666 snd_soc_of_parse_tdm_slot(np, NULL, NULL,
667 &link_data->slots,
668 &link_data->slot_width);
669 /* default value */
670 if (!link_data->slots)
671 link_data->slots = 2;
672
673 if (!link_data->slot_width)
674 link_data->slot_width = 32;
675
676 link->ignore_pmdown_time = 1;
677 link->stream_name = link->name;
678 link++;
679 link_data++;
680
681 of_node_put(cpu);
682 of_node_put(codec);
683 of_node_put(platform);
684
685 cpu = NULL;
686 codec = NULL;
687 platform = NULL;
688 }
689
690 return 0;
691 err:
692 of_node_put(cpu);
693 of_node_put(codec);
694 of_node_put(platform);
695 err_put_np:
696 of_node_put(np);
697 return ret;
698 }
699
imx_card_probe(struct platform_device * pdev)700 static int imx_card_probe(struct platform_device *pdev)
701 {
702 struct snd_soc_dai_link *link_be = NULL, *link;
703 struct imx_card_plat_data *plat_data;
704 struct imx_card_data *data;
705 int ret, i;
706
707 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
708 if (!data)
709 return -ENOMEM;
710
711 plat_data = devm_kzalloc(&pdev->dev, sizeof(*plat_data), GFP_KERNEL);
712 if (!plat_data)
713 return -ENOMEM;
714
715 data->plat_data = plat_data;
716 data->card.dev = &pdev->dev;
717 data->card.owner = THIS_MODULE;
718
719 dev_set_drvdata(&pdev->dev, &data->card);
720 snd_soc_card_set_drvdata(&data->card, data);
721 ret = imx_card_parse_of(data);
722 if (ret)
723 return ret;
724
725 data->num_dapm_routes = plat_data->num_codecs + 1;
726 data->dapm_routes = devm_kcalloc(&pdev->dev, data->num_dapm_routes,
727 sizeof(struct snd_soc_dapm_route),
728 GFP_KERNEL);
729 if (!data->dapm_routes)
730 return -ENOMEM;
731
732 /* configure the dapm routes */
733 switch (plat_data->type) {
734 case CODEC_AK4458:
735 case CODEC_AK4497:
736 if (plat_data->num_codecs == 1) {
737 data->dapm_routes[0].sink = "Playback";
738 data->dapm_routes[0].source = "CPU-Playback";
739 i = 1;
740 } else {
741 for (i = 0; i < plat_data->num_codecs; i++) {
742 data->dapm_routes[i].sink =
743 devm_kasprintf(&pdev->dev, GFP_KERNEL, "%d %s",
744 i + 1, "Playback");
745 data->dapm_routes[i].source = "CPU-Playback";
746 }
747 }
748 data->dapm_routes[i].sink = "CPU-Playback";
749 data->dapm_routes[i].source = "ASRC-Playback";
750 break;
751 case CODEC_AK5558:
752 case CODEC_AK5552:
753 if (plat_data->num_codecs == 1) {
754 data->dapm_routes[0].sink = "CPU-Capture";
755 data->dapm_routes[0].source = "Capture";
756 i = 1;
757 } else {
758 for (i = 0; i < plat_data->num_codecs; i++) {
759 data->dapm_routes[i].source =
760 devm_kasprintf(&pdev->dev, GFP_KERNEL, "%d %s",
761 i + 1, "Capture");
762 data->dapm_routes[i].sink = "CPU-Capture";
763 }
764 }
765 data->dapm_routes[i].sink = "ASRC-Capture";
766 data->dapm_routes[i].source = "CPU-Capture";
767 break;
768 default:
769 break;
770 }
771
772 /* default platform data for akcodecs */
773 if (codec_is_akcodec(plat_data->type)) {
774 plat_data->support_rates = akcodec_rates;
775 plat_data->num_rates = ARRAY_SIZE(akcodec_rates);
776 plat_data->support_tdm_rates = akcodec_tdm_rates;
777 plat_data->num_tdm_rates = ARRAY_SIZE(akcodec_tdm_rates);
778
779 switch (plat_data->type) {
780 case CODEC_AK4458:
781 plat_data->fs_mul = ak4458_fs_mul;
782 plat_data->num_fs_mul = ARRAY_SIZE(ak4458_fs_mul);
783 plat_data->tdm_fs_mul = ak4458_tdm_fs_mul;
784 plat_data->num_tdm_fs_mul = ARRAY_SIZE(ak4458_tdm_fs_mul);
785 plat_data->support_channels = ak4458_channels;
786 plat_data->num_channels = ARRAY_SIZE(ak4458_channels);
787 plat_data->support_tdm_channels = ak4458_tdm_channels;
788 plat_data->num_tdm_channels = ARRAY_SIZE(ak4458_tdm_channels);
789 break;
790 case CODEC_AK4497:
791 plat_data->fs_mul = ak4497_fs_mul;
792 plat_data->num_fs_mul = ARRAY_SIZE(ak4497_fs_mul);
793 plat_data->support_channels = ak4458_channels;
794 plat_data->num_channels = ARRAY_SIZE(ak4458_channels);
795 break;
796 case CODEC_AK5558:
797 case CODEC_AK5552:
798 plat_data->fs_mul = ak5558_fs_mul;
799 plat_data->num_fs_mul = ARRAY_SIZE(ak5558_fs_mul);
800 plat_data->tdm_fs_mul = ak5558_tdm_fs_mul;
801 plat_data->num_tdm_fs_mul = ARRAY_SIZE(ak5558_tdm_fs_mul);
802 plat_data->support_channels = ak5558_channels;
803 plat_data->num_channels = ARRAY_SIZE(ak5558_channels);
804 plat_data->support_tdm_channels = ak5558_tdm_channels;
805 plat_data->num_tdm_channels = ARRAY_SIZE(ak5558_tdm_channels);
806 break;
807 default:
808 break;
809 }
810 }
811
812 /* with asrc as front end */
813 if (data->card.num_links == 3) {
814 data->card.dapm_routes = data->dapm_routes;
815 data->card.num_dapm_routes = data->num_dapm_routes;
816 for_each_card_prelinks(&data->card, i, link) {
817 if (link->no_pcm == 1)
818 link_be = link;
819 }
820 for_each_card_prelinks(&data->card, i, link) {
821 if (link->dynamic == 1 && link_be) {
822 link->dpcm_playback = link_be->dpcm_playback;
823 link->dpcm_capture = link_be->dpcm_capture;
824 }
825 }
826 }
827
828 ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
829 if (ret)
830 return dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
831
832 return 0;
833 }
834
835 static const struct of_device_id imx_card_dt_ids[] = {
836 { .compatible = "fsl,imx-audio-card", },
837 { },
838 };
839 MODULE_DEVICE_TABLE(of, imx_card_dt_ids);
840
841 static struct platform_driver imx_card_driver = {
842 .driver = {
843 .name = "imx-card",
844 .pm = &snd_soc_pm_ops,
845 .of_match_table = imx_card_dt_ids,
846 },
847 .probe = imx_card_probe,
848 };
849 module_platform_driver(imx_card_driver);
850
851 MODULE_DESCRIPTION("Freescale i.MX ASoC Machine Driver");
852 MODULE_LICENSE("GPL v2");
853 MODULE_ALIAS("platform:imx-card");
854