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, 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 struct snd_soc_ops imx_aif_ops = {
455 .hw_params = imx_aif_hw_params,
456 .startup = imx_aif_startup,
457 };
458
459 static 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 = of_parse_phandle_with_args(cpu, "sound-dai",
555 "#sound-dai-cells", 0, &args);
556 if (ret) {
557 dev_err(card->dev, "%s: error getting cpu phandle\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_find_property(np, "fsl,mclk-equal-bclk", NULL)) {
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->cpus->of_node = args.np;
586 link->platforms->of_node = link->cpus->of_node;
587 link->id = args.args[0];
588
589 ret = snd_soc_of_get_dai_name(cpu, &link->cpus->dai_name);
590 if (ret) {
591 dev_err_probe(card->dev, ret,
592 "%s: error getting cpu dai name\n", link->name);
593 goto err;
594 }
595
596 codec = of_get_child_by_name(np, "codec");
597 if (codec) {
598 ret = snd_soc_of_get_dai_link_codecs(dev, codec, link);
599 if (ret < 0) {
600 dev_err_probe(dev, ret, "%s: codec dai not found\n",
601 link->name);
602 goto err;
603 }
604
605 plat_data->num_codecs = link->num_codecs;
606
607 /* Check the akcodec type */
608 if (!strcmp(link->codecs->dai_name, "ak4458-aif"))
609 plat_data->type = CODEC_AK4458;
610 else if (!strcmp(link->codecs->dai_name, "ak4497-aif"))
611 plat_data->type = CODEC_AK4497;
612 else if (!strcmp(link->codecs->dai_name, "ak5558-aif"))
613 plat_data->type = CODEC_AK5558;
614 else if (!strcmp(link->codecs->dai_name, "ak5552-aif"))
615 plat_data->type = CODEC_AK5552;
616
617 } else {
618 dlc = devm_kzalloc(dev, sizeof(*dlc), GFP_KERNEL);
619 if (!dlc) {
620 ret = -ENOMEM;
621 goto err;
622 }
623
624 link->codecs = dlc;
625 link->num_codecs = 1;
626
627 link->codecs->dai_name = "snd-soc-dummy-dai";
628 link->codecs->name = "snd-soc-dummy";
629 }
630
631 if (!strncmp(link->name, "HiFi-ASRC-FE", 12)) {
632 /* DPCM frontend */
633 link->dynamic = 1;
634 link->dpcm_merged_chan = 1;
635
636 ret = of_property_read_u32(args.np, "fsl,asrc-rate", &data->asrc_rate);
637 if (ret) {
638 dev_err(dev, "failed to get output rate\n");
639 ret = -EINVAL;
640 goto err;
641 }
642
643 ret = of_property_read_u32(args.np, "fsl,asrc-format", &asrc_fmt);
644 data->asrc_format = (__force snd_pcm_format_t)asrc_fmt;
645 if (ret) {
646 /* Fallback to old binding; translate to asrc_format */
647 ret = of_property_read_u32(args.np, "fsl,asrc-width", &width);
648 if (ret) {
649 dev_err(dev,
650 "failed to decide output format\n");
651 goto err;
652 }
653
654 if (width == 24)
655 data->asrc_format = SNDRV_PCM_FORMAT_S24_LE;
656 else
657 data->asrc_format = SNDRV_PCM_FORMAT_S16_LE;
658 }
659 } else if (!strncmp(link->name, "HiFi-ASRC-BE", 12)) {
660 /* DPCM backend */
661 link->no_pcm = 1;
662 link->platforms->of_node = NULL;
663 link->platforms->name = "snd-soc-dummy";
664
665 link->be_hw_params_fixup = be_hw_params_fixup;
666 link->ops = &imx_aif_ops_be;
667 } else {
668 link->ops = &imx_aif_ops;
669 }
670
671 if (link->no_pcm || link->dynamic)
672 snd_soc_dai_link_set_capabilities(link);
673
674 /* Get dai fmt */
675 ret = asoc_simple_parse_daifmt(dev, np, codec,
676 NULL, &link->dai_fmt);
677 if (ret)
678 link->dai_fmt = SND_SOC_DAIFMT_NB_NF |
679 SND_SOC_DAIFMT_CBS_CFS |
680 SND_SOC_DAIFMT_I2S;
681
682 /* Get tdm slot */
683 snd_soc_of_parse_tdm_slot(np, NULL, NULL,
684 &link_data->slots,
685 &link_data->slot_width);
686 /* default value */
687 if (!link_data->slots)
688 link_data->slots = 2;
689
690 if (!link_data->slot_width)
691 link_data->slot_width = 32;
692
693 link->ignore_pmdown_time = 1;
694 link->stream_name = link->name;
695 link++;
696 link_data++;
697
698 of_node_put(cpu);
699 of_node_put(codec);
700 of_node_put(platform);
701
702 cpu = NULL;
703 codec = NULL;
704 platform = NULL;
705 }
706
707 return 0;
708 err:
709 of_node_put(cpu);
710 of_node_put(codec);
711 of_node_put(platform);
712 err_put_np:
713 of_node_put(np);
714 return ret;
715 }
716
imx_card_probe(struct platform_device * pdev)717 static int imx_card_probe(struct platform_device *pdev)
718 {
719 struct snd_soc_dai_link *link_be = NULL, *link;
720 struct imx_card_plat_data *plat_data;
721 struct imx_card_data *data;
722 int ret, i;
723
724 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
725 if (!data)
726 return -ENOMEM;
727
728 plat_data = devm_kzalloc(&pdev->dev, sizeof(*plat_data), GFP_KERNEL);
729 if (!plat_data)
730 return -ENOMEM;
731
732 data->plat_data = plat_data;
733 data->card.dev = &pdev->dev;
734
735 dev_set_drvdata(&pdev->dev, &data->card);
736 snd_soc_card_set_drvdata(&data->card, data);
737 ret = imx_card_parse_of(data);
738 if (ret)
739 return ret;
740
741 data->num_dapm_routes = plat_data->num_codecs + 1;
742 data->dapm_routes = devm_kcalloc(&pdev->dev, data->num_dapm_routes,
743 sizeof(struct snd_soc_dapm_route),
744 GFP_KERNEL);
745 if (!data->dapm_routes)
746 return -ENOMEM;
747
748 /* configure the dapm routes */
749 switch (plat_data->type) {
750 case CODEC_AK4458:
751 case CODEC_AK4497:
752 if (plat_data->num_codecs == 1) {
753 data->dapm_routes[0].sink = "Playback";
754 data->dapm_routes[0].source = "CPU-Playback";
755 i = 1;
756 } else {
757 for (i = 0; i < plat_data->num_codecs; i++) {
758 data->dapm_routes[i].sink =
759 devm_kasprintf(&pdev->dev, GFP_KERNEL, "%d %s",
760 i + 1, "Playback");
761 data->dapm_routes[i].source = "CPU-Playback";
762 }
763 }
764 data->dapm_routes[i].sink = "CPU-Playback";
765 data->dapm_routes[i].source = "ASRC-Playback";
766 break;
767 case CODEC_AK5558:
768 case CODEC_AK5552:
769 if (plat_data->num_codecs == 1) {
770 data->dapm_routes[0].sink = "CPU-Capture";
771 data->dapm_routes[0].source = "Capture";
772 i = 1;
773 } else {
774 for (i = 0; i < plat_data->num_codecs; i++) {
775 data->dapm_routes[i].source =
776 devm_kasprintf(&pdev->dev, GFP_KERNEL, "%d %s",
777 i + 1, "Capture");
778 data->dapm_routes[i].sink = "CPU-Capture";
779 }
780 }
781 data->dapm_routes[i].sink = "ASRC-Capture";
782 data->dapm_routes[i].source = "CPU-Capture";
783 break;
784 default:
785 break;
786 }
787
788 /* default platform data for akcodecs */
789 if (codec_is_akcodec(plat_data->type)) {
790 plat_data->support_rates = akcodec_rates;
791 plat_data->num_rates = ARRAY_SIZE(akcodec_rates);
792 plat_data->support_tdm_rates = akcodec_tdm_rates;
793 plat_data->num_tdm_rates = ARRAY_SIZE(akcodec_tdm_rates);
794
795 switch (plat_data->type) {
796 case CODEC_AK4458:
797 plat_data->fs_mul = ak4458_fs_mul;
798 plat_data->num_fs_mul = ARRAY_SIZE(ak4458_fs_mul);
799 plat_data->tdm_fs_mul = ak4458_tdm_fs_mul;
800 plat_data->num_tdm_fs_mul = ARRAY_SIZE(ak4458_tdm_fs_mul);
801 plat_data->support_channels = ak4458_channels;
802 plat_data->num_channels = ARRAY_SIZE(ak4458_channels);
803 plat_data->support_tdm_channels = ak4458_tdm_channels;
804 plat_data->num_tdm_channels = ARRAY_SIZE(ak4458_tdm_channels);
805 break;
806 case CODEC_AK4497:
807 plat_data->fs_mul = ak4497_fs_mul;
808 plat_data->num_fs_mul = ARRAY_SIZE(ak4497_fs_mul);
809 plat_data->support_channels = ak4458_channels;
810 plat_data->num_channels = ARRAY_SIZE(ak4458_channels);
811 break;
812 case CODEC_AK5558:
813 case CODEC_AK5552:
814 plat_data->fs_mul = ak5558_fs_mul;
815 plat_data->num_fs_mul = ARRAY_SIZE(ak5558_fs_mul);
816 plat_data->tdm_fs_mul = ak5558_tdm_fs_mul;
817 plat_data->num_tdm_fs_mul = ARRAY_SIZE(ak5558_tdm_fs_mul);
818 plat_data->support_channels = ak5558_channels;
819 plat_data->num_channels = ARRAY_SIZE(ak5558_channels);
820 plat_data->support_tdm_channels = ak5558_tdm_channels;
821 plat_data->num_tdm_channels = ARRAY_SIZE(ak5558_tdm_channels);
822 break;
823 default:
824 break;
825 }
826 }
827
828 /* with asrc as front end */
829 if (data->card.num_links == 3) {
830 data->card.dapm_routes = data->dapm_routes;
831 data->card.num_dapm_routes = data->num_dapm_routes;
832 for_each_card_prelinks(&data->card, i, link) {
833 if (link->no_pcm == 1)
834 link_be = link;
835 }
836 for_each_card_prelinks(&data->card, i, link) {
837 if (link->dynamic == 1 && link_be) {
838 link->dpcm_playback = link_be->dpcm_playback;
839 link->dpcm_capture = link_be->dpcm_capture;
840 }
841 }
842 }
843
844 ret = devm_snd_soc_register_card(&pdev->dev, &data->card);
845 if (ret)
846 return dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
847
848 return 0;
849 }
850
851 static const struct of_device_id imx_card_dt_ids[] = {
852 { .compatible = "fsl,imx-audio-card", },
853 { },
854 };
855 MODULE_DEVICE_TABLE(of, imx_card_dt_ids);
856
857 static struct platform_driver imx_card_driver = {
858 .driver = {
859 .name = "imx-card",
860 .pm = &snd_soc_pm_ops,
861 .of_match_table = imx_card_dt_ids,
862 },
863 .probe = imx_card_probe,
864 };
865 module_platform_driver(imx_card_driver);
866
867 MODULE_DESCRIPTION("Freescale i.MX ASoC Machine Driver");
868 MODULE_LICENSE("GPL v2");
869 MODULE_ALIAS("platform:imx-card");
870