aboutsummaryrefslogtreecommitdiffstats
blob: 6c5af2df8a8ddaa3dc0f18065de0ea26878475a1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
/*
 * dr7-evm.c  --  SoC audio for TI DRA7 EVM
 *
 * Author: Misael Lopez Cruz <misael.lopez@ti.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

#include <linux/platform_device.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/clk.h>
#include <linux/io.h>
#include <linux/pm_runtime.h>
#include <sound/core.h>
#include <sound/pcm.h>
#include <sound/soc.h>
#include <sound/pcm_params.h>
#include <sound/soc-dapm.h>

#define NUM_CODECS	3

struct dra7_snd_data {
	unsigned int media_mclk_freq;
	unsigned int multichannel_mclk_freq;
	unsigned int bt_bclk_freq;
	unsigned int media_slots;
	unsigned int multichannel_slots;
	unsigned int bt_rate;
	int bt_is_master;
	int always_on;
	int media_shared;
	int multichannel_shared;
	const int *rates;
	int num_rates;
};

static const int dra7_snd_rates[] = {
	 5512,  8000, 11025, 16000, 22050,
	32000, 44100, 48000, 88200, 96000,
};

static int dra7_mcasp_reparent(struct snd_soc_card *card,
			       const char *fclk_name,
			       const char *parent_name)
{
	struct clk *gfclk, *parent_clk;
	int ret;

	gfclk = clk_get(card->dev, fclk_name);
	if (IS_ERR(gfclk)) {
		dev_err(card->dev, "failed to get %s\n", fclk_name);
		return PTR_ERR(gfclk);
	}

	parent_clk = clk_get(card->dev, parent_name);
	if (IS_ERR(parent_clk)) {
		dev_err(card->dev, "failed to get new parent clock %s\n",
			parent_name);
		ret = PTR_ERR(parent_clk);
		goto err1;
	}

	ret = clk_set_parent(gfclk, parent_clk);
	if (ret) {
		dev_err(card->dev, "failed to reparent %s\n", fclk_name);
		goto err2;
	}

err2:
	clk_put(parent_clk);
err1:
	clk_put(gfclk);
	return ret;
}

static unsigned int dra7_get_bclk(struct snd_pcm_hw_params *params, int slots)
{
	int sample_size = snd_pcm_format_width(params_format(params));
	int rate = params_rate(params);

	return sample_size * slots * rate;
}

static int dra7_snd_media_hwrule_rates(struct snd_pcm_hw_params *params,
				       struct snd_pcm_hw_rule *rule)
{
	struct dra7_snd_data *card_data = rule->private;
	unsigned int slots = card_data->media_slots;
	unsigned int mclk_freq = card_data->media_mclk_freq;
	unsigned int bclk_freq;
	int width = snd_pcm_format_physical_width(params_format(params));
	int rates[10], count = 0;
	int i;

	for (i = 0; i < card_data->num_rates; i++) {
		bclk_freq = slots * card_data->rates[i] * width;
		if ((mclk_freq % bclk_freq) == 0)
			rates[count++] = card_data->rates[i];
	}

	return snd_interval_list(hw_param_interval(params, rule->var),
				 count, rates, 0);
}

static int dra7_snd_media_startup(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_card *card = rtd->card;
	struct dra7_snd_data *card_data = snd_soc_card_get_drvdata(card);

	snd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
			    dra7_snd_media_hwrule_rates, card_data,
			    SNDRV_PCM_HW_PARAM_RATE, -1);

	return 0;
}

static int dra7_snd_media_hw_params(struct snd_pcm_substream *substream,
				    struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
	struct snd_soc_dai *codec_dai = rtd->codec_dai;
	struct snd_soc_card *card = rtd->card;
	struct dra7_snd_data *card_data = snd_soc_card_get_drvdata(card);
	unsigned int fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_CBS_CFS;
	unsigned int bclk_freq;
	int ret;

	bclk_freq = dra7_get_bclk(params, card_data->media_slots);

	/* McASP driver requires inverted frame for I2S */
	ret = snd_soc_dai_set_fmt(cpu_dai, fmt | SND_SOC_DAIFMT_NB_IF);
	if (ret < 0) {
		dev_err(card->dev, "can't set CPU DAI format %d\n", ret);
		return ret;
	}

	/* Set McASP BCLK divider (clkid = 1) */
	ret = snd_soc_dai_set_clkdiv(cpu_dai, 1,
				     card_data->media_mclk_freq / bclk_freq);
	if (ret < 0) {
		dev_err(card->dev, "can't set CPU DAI clock divider %d\n", ret);
		return ret;
	}

	/* Set McASP sysclk from AHCLKX sourced from ATL */
	ret = snd_soc_dai_set_sysclk(cpu_dai, 0,
				     card_data->media_mclk_freq,
				     SND_SOC_CLOCK_IN);
	if (ret < 0) {
		dev_err(card->dev, "can't set CPU DAI sysclk %d\n", ret);
		return ret;
	}

	/* Skip settings for the dummy CODEC as it doesn't implement any DAI ops */
	if (!strcmp(codec_dai->name, "snd-soc-dummy-dai"))
		return 0;

	ret = snd_soc_dai_set_fmt(codec_dai, fmt | SND_SOC_DAIFMT_NB_NF);
	if (ret < 0) {
		dev_err(card->dev, "can't set CODEC DAI format %d\n", ret);
		return ret;
	}

	/* Set MCLK as clock source for tlv320aic3106 */
	ret = snd_soc_dai_set_sysclk(codec_dai, 0,
				     card_data->media_mclk_freq,
				     SND_SOC_CLOCK_IN);
	if (ret < 0)
		dev_err(card->dev, "can't set CODEC sysclk %d\n", ret);

	return ret;
}

static struct snd_soc_ops dra7_snd_media_ops = {
	.startup = dra7_snd_media_startup,
	.hw_params = dra7_snd_media_hw_params,
};

static int dra7_snd_multichannel_hwrule_rates(struct snd_pcm_hw_params *params,
					      struct snd_pcm_hw_rule *rule)
{
	struct dra7_snd_data *card_data = rule->private;
	unsigned int slots = card_data->multichannel_slots;
	unsigned int mclk_freq = card_data->multichannel_mclk_freq;
	unsigned int bclk_freq;
	int width = snd_pcm_format_physical_width(params_format(params));
	int rates[10], count = 0;
	int i;

	for (i = 0; i < card_data->num_rates; i++) {
		bclk_freq = slots * card_data->rates[i] * width;
		if ((mclk_freq % bclk_freq) == 0)
			rates[count++] = card_data->rates[i];
	}

	return snd_interval_list(hw_param_interval(params, rule->var),
				 count, rates, 0);
}

static int dra7_snd_multichannel_startup(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_card *card = rtd->card;
	struct dra7_snd_data *card_data = snd_soc_card_get_drvdata(card);

	/* CODEC's TDM slot mask is always 2, aligned on 2-ch boundaries */
	snd_pcm_hw_constraint_step(substream->runtime, 0,
				   SNDRV_PCM_HW_PARAM_CHANNELS, 2);

	snd_pcm_hw_rule_add(substream->runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
			    dra7_snd_multichannel_hwrule_rates, card_data,
			    SNDRV_PCM_HW_PARAM_RATE, -1);

	return 0;
}

static int dra7_snd_multichannel_hw_params(struct snd_pcm_substream *substream,
					   struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
	struct snd_soc_dai *codec_dai;
	struct snd_soc_card *card = rtd->card;
	struct dra7_snd_data *card_data = snd_soc_card_get_drvdata(card);
	unsigned int fmt = SND_SOC_DAIFMT_CBS_CFS | SND_SOC_DAIFMT_DSP_B |
			   SND_SOC_DAIFMT_IB_NF;
	unsigned int bclk_freq;
	unsigned int slot_mask = 3;
	int sample_size = snd_pcm_format_width(params_format(params));
	int i, ret;

	bclk_freq = dra7_get_bclk(params, card_data->multichannel_slots);

	ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
	if (ret < 0) {
		dev_err(card->dev, "can't set CPU DAI format %d\n", ret);
		return ret;
	}

	/* Set McASP BCLK divider (clkid = 1) */
	ret = snd_soc_dai_set_clkdiv(cpu_dai, 1,
				card_data->multichannel_mclk_freq / bclk_freq);
	if (ret < 0) {
		dev_err(card->dev, "can't set CPU DAI clock divider %d\n", ret);
		return ret;
	}

	/* Set McASP sysclk from AHCLKX sourced from ATL */
	ret = snd_soc_dai_set_sysclk(cpu_dai, 0,
				     card_data->multichannel_mclk_freq,
				     SND_SOC_CLOCK_IN);
	if (ret < 0) {
		dev_err(card->dev, "can't set CPU DAI sysclk %d\n", ret);
		return ret;
	}

	for (i = 0; i < rtd->num_codecs; i++) {
		codec_dai = rtd->codecs[i].codec_dai;

		ret = snd_soc_dai_set_fmt(codec_dai, fmt);
		if (ret < 0) {
			dev_err(card->dev, "can't set CODEC DAI format %d\n",
				ret);
			return ret;
		}

		ret = snd_soc_dai_set_tdm_slot(codec_dai, slot_mask, slot_mask,
					       card_data->multichannel_slots,
					       sample_size);
		if (ret < 0) {
			dev_err(card->dev, "can't set CODEC DAI tdm slots %d\n",
				ret);
			return ret;
		}
		slot_mask <<= 2;

		/* Set MCLK as clock source for tlv320aic3106 */
		ret = snd_soc_dai_set_sysclk(codec_dai, 0,
					     card_data->multichannel_mclk_freq,
					     SND_SOC_CLOCK_IN);
		if (ret < 0) {
			dev_err(card->dev, "can't set CODEC sysclk %d\n", ret);
			return ret;
		}
	}

	return 0;
}

static struct snd_soc_ops dra7_snd_multichannel_ops = {
	.startup = dra7_snd_multichannel_startup,
	.hw_params = dra7_snd_multichannel_hw_params,
};

static int dra7_multichannel_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
					     struct snd_pcm_hw_params *params)
{
	struct snd_interval *channels = hw_param_interval(params,
						SNDRV_PCM_HW_PARAM_CHANNELS);

	/* Each tlv320aic3x receives two channels from McASP in TDM mode */
	channels->min = 2;
	channels->max = 2;

	return 0;
}

static int dra7_snd_media_init(struct snd_soc_pcm_runtime *rtd)
{
	struct snd_soc_card *card = rtd->card;
	struct dra7_snd_data *card_data = snd_soc_card_get_drvdata(card);
	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;

	/* Minimize artifacts as much as possible if can be afforded */
	if (card_data->always_on)
		rtd->pmdown_time = INT_MAX;
	else
		rtd->pmdown_time = 0;

	/* Forbid runtime PM if the DAI is shared with radio */
	if (card_data->media_shared) {
		dev_info(card->dev, "%s is shared with radio\n", cpu_dai->name);
		pm_runtime_forbid(cpu_dai->dev);
	}

	return 0;
}

static int dra7_snd_multichannel_init(struct snd_soc_pcm_runtime *rtd)
{
	struct snd_soc_card *card = rtd->card;
	struct dra7_snd_data *card_data = snd_soc_card_get_drvdata(card);
	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;

	/* Minimize artifacts as much as possible if can be afforded */
	if (card_data->always_on)
		rtd->pmdown_time = INT_MAX;
	else
		rtd->pmdown_time = 0;

	/* Forbid runtime PM if the DAI is shared with radio */
	if (card_data->multichannel_shared) {
		dev_info(card->dev, "%s is shared with radio\n", cpu_dai->name);
		pm_runtime_forbid(cpu_dai->dev);
	}

	return 0;
}

static struct snd_soc_dai_link_codec dra7_snd_multichannel_codecs[] = {
	{
		.codec_dai_name = "tlv320aic3x-hifi",
		.hw_params_fixup = dra7_multichannel_hw_params_fixup,
	},
	{
		.codec_dai_name = "tlv320aic3x-hifi",
		.hw_params_fixup = dra7_multichannel_hw_params_fixup,
	},
	{
		.codec_dai_name = "tlv320aic3x-hifi",
		.hw_params_fixup = dra7_multichannel_hw_params_fixup,
	},
};

static int dra7_snd_bt_startup(struct snd_pcm_substream *substream)
{
	struct snd_pcm_runtime *runtime = substream->runtime;

	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_RATE,
				     8000, 8000);
	snd_pcm_hw_constraint_minmax(runtime, SNDRV_PCM_HW_PARAM_CHANNELS,
				     2, 2);
	snd_pcm_hw_constraint_mask64(runtime, SNDRV_PCM_HW_PARAM_FORMAT,
				     SNDRV_PCM_FMTBIT_S16_LE);

	return 0;
}

static int dra7_snd_bt_hw_params(struct snd_pcm_substream *substream,
				 struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_card *card = rtd->card;
	struct dra7_snd_data *card_data = snd_soc_card_get_drvdata(card);

	card_data->bt_rate = params_rate(params);

	return 0;
}

static int dra7_snd_bt_prepare(struct snd_pcm_substream *substream)
{
	struct snd_soc_pcm_runtime *rtd = substream->private_data;
	struct snd_soc_dai *cpu_dai = rtd->cpu_dai;
	struct snd_soc_card *card = rtd->card;
	struct dra7_snd_data *card_data = snd_soc_card_get_drvdata(card);
	unsigned int fmt = SND_SOC_DAIFMT_DSP_A | SND_SOC_DAIFMT_NB_IF;
	int dir;
	int ret;

	if (card_data->bt_is_master) {
		fmt |= SND_SOC_DAIFMT_CBM_CFM;
		dir = SND_SOC_CLOCK_OUT;
	} else {
		fmt |= SND_SOC_DAIFMT_CBS_CFS;
		dir = SND_SOC_CLOCK_IN;
	}

	ret = snd_soc_dai_set_fmt(cpu_dai, fmt);
	if (ret < 0) {
		dev_err(card->dev, "can't set CPU DAI format %d\n", ret);
		return ret;
	}

	ret = snd_soc_dai_set_sysclk(cpu_dai, 0, 0, dir);
	if (ret < 0) {
		dev_err(card->dev, "can't set CPU DAI sysclk %d\n", ret);
		return ret;
	}

	/*
	 * Bluetooth SCO is 8kHz, mono, 16-bits/sample but the BCLK runs
	 * at 4x the min required rate. So, the BCLK / FSYNC ratio is 64 which
	 * is not possible with McASP. Instead, the DAI link is configured
	 * in stereo (right channel carries no data) to have 32-bit samples and
	 * the BCLK / FSYNC ratio is programmed so that only 16-bits carry
	 * actual data.
	 */
	ret = snd_soc_dai_set_clkdiv(cpu_dai, 2,
				card_data->bt_bclk_freq / card_data->bt_rate);
	if (ret < 0)
		dev_err(card->dev, "can't set CPU DAI BCLK/FSYNC ratio %d\n",
			ret);

	return ret;
}

static struct snd_soc_ops dra7_snd_bt_ops = {
	.startup = dra7_snd_bt_startup,
	.hw_params = dra7_snd_bt_hw_params,
	.prepare = dra7_snd_bt_prepare,
};

static struct snd_soc_dai_link dra7_snd_dai[] = {
	{
		/* Media: McASP3 + tlv320aic3106 */
		.name = "Media",
		.codec_dai_name = "tlv320aic3x-hifi",
		.platform_name = "omap-pcm-audio",
		.ops = &dra7_snd_media_ops,
		.init = dra7_snd_media_init,
	},
	{
		/* Multichannel: McASP6 + 3 * tlv320aic3106 */
		.name = "Multichannel",
		.platform_name = "omap-pcm-audio",
		.ops = &dra7_snd_multichannel_ops,
		.codecs = dra7_snd_multichannel_codecs,
		.num_codecs = ARRAY_SIZE(dra7_snd_multichannel_codecs),
		.init = dra7_snd_multichannel_init,
	},
	{
		/* Bluetooth: McASP7 + dummy codec */
		.name = "Bluetooth",
		.platform_name = "omap-pcm-audio",
		.codec_name = "snd-soc-dummy",
		.codec_dai_name = "snd-soc-dummy-dai",
		.ops = &dra7_snd_bt_ops,
	},
	{
		/* Media: McASP5 in loopback mode */
		.name = "Loopback",
		.platform_name = "omap-pcm-audio",
		.codec_name = "snd-soc-dummy",
		.codec_dai_name = "snd-soc-dummy-dai",
		.ops = &dra7_snd_media_ops,
		.init = dra7_snd_media_init,
	},
};

static int dra7_snd_bt_get_mode(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
	struct dra7_snd_data *card_data = snd_soc_card_get_drvdata(card);

	ucontrol->value.integer.value[0] = card_data->bt_is_master;
	return 0;
}

static int dra7_snd_bt_set_mode(struct snd_kcontrol *kcontrol,
				struct snd_ctl_elem_value *ucontrol)
{
	struct snd_soc_card *card = snd_kcontrol_chip(kcontrol);
	struct dra7_snd_data *card_data = snd_soc_card_get_drvdata(card);

	card_data->bt_is_master = ucontrol->value.integer.value[0];
	return 1;
}

/*
 * The Bluetooth PCM interface (the codec side of the DAI link) can cut the
 * BCLK and FSYNC while the ALSA PCM device is still in use (typically, when
 * BT link is about to be released). Missing BCLK/FSYNC causes audio data
 * read/write to block for long time.
 * Bluetooth mode kcontrol can change the CPU DAI master or slave configuration.
 * This control is specifically meant to address the limitation describe above.
 * By switching to slave mode, the CPU DAI link has the clocks to continue
 * without stalling anymore.
 * Note that this is only in the context of McASP, no pin direction change is
 * needed.
 */
static const char * const dra7_snd_bt_texts[] = {"Slave", "Master"};

static const struct soc_enum dra7_snd_enum[] = {
	SOC_ENUM_SINGLE_EXT(2, dra7_snd_bt_texts),
};

static const struct snd_kcontrol_new dra7_snd_controls[] = {
	SOC_ENUM_EXT("Bluetooth Mode", dra7_snd_enum[0],
		     dra7_snd_bt_get_mode, dra7_snd_bt_set_mode),
};

static int dra7_snd_add_dai_link(struct snd_soc_card *card,
				 struct snd_soc_dai_link *dai_link,
				 const char *prefix)
{
	struct dra7_snd_data *card_data = snd_soc_card_get_drvdata(card);
	struct device_node *node = card->dev->of_node;
	struct device_node *dai_node;
	char prop[32];
	int ret;

	if (!node) {
		dev_err(card->dev, "card node is invalid\n");
		return -EINVAL;
	}

	snprintf(prop, sizeof(prop), "%s-mclk-freq", prefix);
	of_property_read_u32(node, prop,
			     &card_data->media_mclk_freq);

	snprintf(prop, sizeof(prop), "%s-cpu", prefix);
	dai_node = of_parse_phandle(node, prop, 0);
	if (!dai_node) {
		dev_err(card->dev, "cpu dai node is invalid\n");
		return -EINVAL;
	}

	dai_link->cpu_of_node = dai_node;

	if (!dai_link->codec_name || !dai_link->codec_dai_name) {
		snprintf(prop, sizeof(prop), "%s-codec", prefix);
		dai_node = of_parse_phandle(node, prop, 0);
		if (!dai_node) {
			dev_err(card->dev, "codec dai node is invalid\n");
			return -EINVAL;
		}

		dai_link->codec_of_node = dai_node;
	}

	snprintf(prop, sizeof(prop), "%s-slots", prefix);
	of_property_read_u32(node, prop, &card_data->media_slots);
	if ((card_data->media_slots < 1) ||
	    (card_data->media_slots > 32)) {
		dev_err(card->dev, "invalid media slot count %d\n",
			card_data->media_slots);
		return -EINVAL;
	}

	ret = snd_soc_card_new_dai_links(card, dai_link, 1);
	if (ret < 0) {
		dev_err(card->dev, "failed to add dai link %s\n",
			dai_link->name);
		return ret;
	}

	snprintf(prop, sizeof(prop), "%s-shared", prefix);
	if (of_find_property(node, prop, NULL))
		card_data->media_shared = 1;

	return 0;
}

static struct snd_soc_codec_conf dra7_codec_conf[NUM_CODECS];

static int dra7_snd_add_multichannel_dai_link(struct snd_soc_card *card,
					      struct snd_soc_dai_link *dai_link,
					      const char *prefix)
{
	struct dra7_snd_data *card_data = snd_soc_card_get_drvdata(card);
	struct device_node *node = card->dev->of_node;
	struct device_node *dai_node;
	char prop[32];
	int index = 0;
	int ret;

	if (!node) {
		dev_err(card->dev, "card node is invalid\n");
		return -EINVAL;
	}

	snprintf(prop, sizeof(prop), "%s-mclk-freq", prefix);
	of_property_read_u32(node, prop,
			     &card_data->multichannel_mclk_freq);

	snprintf(prop, sizeof(prop), "%s-cpu", prefix);
	dai_node = of_parse_phandle(node, prop, 0);
	if (!dai_node) {
		dev_err(card->dev, "cpu dai node is invalid\n");
		return -EINVAL;
	}

	dai_link->cpu_of_node = dai_node;

	snprintf(prop, sizeof(prop), "%s-codec-a", prefix);
	dai_node = of_parse_phandle(node, prop, 0);
	if (!dai_node) {
		dev_err(card->dev, "codec dai node is invalid\n");
		return -EINVAL;
	}

	dai_link->codecs[index].codec_of_node = dai_node;
	dra7_codec_conf[index].codec_of_node = dai_node;
	dra7_codec_conf[index].name_prefix = "J3A";
	index++;

	snprintf(prop, sizeof(prop), "%s-codec-b", prefix);
	dai_node = of_parse_phandle(node, prop, 0);
	if (!dai_node) {
		dev_err(card->dev, "codec dai node is invalid\n");
		return -EINVAL;
	}

	dai_link->codecs[index].codec_of_node = dai_node;
	dra7_codec_conf[index].codec_of_node = dai_node;
	dra7_codec_conf[index].name_prefix = "J3B";
	index++;

	snprintf(prop, sizeof(prop), "%s-codec-c", prefix);
	dai_node = of_parse_phandle(node, prop, 0);
	if (!dai_node) {
		dev_err(card->dev, "codec dai node is invalid\n");
		return -EINVAL;
	}

	dai_link->codecs[index].codec_of_node = dai_node;
	dra7_codec_conf[index].codec_of_node = dai_node;
	dra7_codec_conf[index].name_prefix = "J3C";

	snprintf(prop, sizeof(prop), "%s-slots", prefix);
	of_property_read_u32(node, prop, &card_data->multichannel_slots);
	if ((card_data->multichannel_slots < 1) ||
	    (card_data->multichannel_slots > 32)) {
		dev_err(card->dev, "invalid slot count %d\n",
			card_data->multichannel_slots);
		return -EINVAL;
	}

	ret = snd_soc_card_new_dai_links(card, dai_link, 1);
	if (ret < 0) {
		dev_err(card->dev, "failed to add dai link %s\n",
			dai_link->name);
		return ret;
	}

	snprintf(prop, sizeof(prop), "%s-shared", prefix);
	if (of_find_property(node, prop, NULL))
		card_data->multichannel_shared = 1;

	return 0;
}

static int dra7_snd_add_bt_dai_link(struct snd_soc_card *card,
				    struct snd_soc_dai_link *dai_link,
				    const char *prefix)
{
	struct dra7_snd_data *card_data = snd_soc_card_get_drvdata(card);
	struct device_node *node = card->dev->of_node;
	struct device_node *dai_node;
	char prop[32];
	int ret;

	card_data->bt_is_master = 1;
	card_data->bt_rate = 8000;

	if (!node) {
		dev_err(card->dev, "bluetooth node is invalid\n");
		return -EINVAL;
	}

	snprintf(prop, sizeof(prop), "%s-bclk-freq", prefix);
	of_property_read_u32(node, prop,
			     &card_data->bt_bclk_freq);

	snprintf(prop, sizeof(prop), "%s-cpu", prefix);
	dai_node = of_parse_phandle(node, prop, 0);
	if (!dai_node) {
		dev_err(card->dev, "cpu dai node is invalid\n");
		return -EINVAL;
	}

	dai_link->cpu_of_node = dai_node;

	ret = snd_soc_card_new_dai_links(card, dai_link, 1);
	if (ret < 0)
		dev_err(card->dev, "failed to add bluetooth dai link %s\n",
			dai_link->name);

	return ret;
}

/* DRA7 CPU board widgets */
static const struct snd_soc_dapm_widget dra7_snd_dapm_widgets[] = {
	/* CPU board input */
	SND_SOC_DAPM_MIC("Main Mic", NULL),
	SND_SOC_DAPM_LINE("Line In", NULL),

	/* CPU board outputs */
	SND_SOC_DAPM_HP("Headphone", NULL),
	SND_SOC_DAPM_LINE("Line Out", NULL),

	/* JAMR3 board inputs */
	SND_SOC_DAPM_LINE("JAMR3 Stereo Aux In", NULL),
	SND_SOC_DAPM_LINE("JAMR3 Mono Mic 1", NULL),
	SND_SOC_DAPM_LINE("JAMR3 Mono Mic 2", NULL),

	/* JAMR3 board outputs */
	SND_SOC_DAPM_LINE("JAMR3 Line Out 1", NULL),
	SND_SOC_DAPM_LINE("JAMR3 Line Out 2", NULL),
	SND_SOC_DAPM_LINE("JAMR3 Line Out 3", NULL),
};

/* Audio machine driver */
static struct snd_soc_card dra7_snd_card = {
	.owner = THIS_MODULE,
	.dapm_widgets = dra7_snd_dapm_widgets,
	.num_dapm_widgets = ARRAY_SIZE(dra7_snd_dapm_widgets),
	.codec_conf = dra7_codec_conf,
	.num_configs = ARRAY_SIZE(dra7_codec_conf),
};

static int dra7_snd_probe(struct platform_device *pdev)
{
	struct device_node *node = pdev->dev.of_node;
	struct snd_soc_card *card = &dra7_snd_card;
	struct dra7_snd_data *card_data;
	int gpio;
	int ret;

	card->dev = &pdev->dev;

	card_data = devm_kzalloc(&pdev->dev, sizeof(*card_data), GFP_KERNEL);
	if (card_data == NULL)
		return -ENOMEM;

	if (!node) {
		dev_err(card->dev, "missing of_node\n");
		return -ENODEV;
	}

	card_data->rates = dra7_snd_rates;
	card_data->num_rates = ARRAY_SIZE(dra7_snd_rates);

	gpio = of_get_gpio(node, 0);
	if (gpio_is_valid(gpio)) {
		ret = devm_gpio_request_one(card->dev, gpio,
					    GPIOF_OUT_INIT_LOW, "snd_gpio");
		if (ret)
			dev_warn(card->dev, "DAI sel gpio is not available\n");
	}

	ret = snd_soc_of_parse_card_name(card, "ti,model");
	if (ret) {
		dev_err(card->dev, "card name is not provided\n");
		return -ENODEV;
	}

	ret = snd_soc_of_parse_audio_routing(card,
					     "ti,audio-routing");
	if (ret) {
		dev_err(card->dev, "failed to parse DAPM routing\n");
		return ret;
	}

	snd_soc_card_set_drvdata(card, card_data);

	if (of_find_property(node, "ti,always-on", NULL))
		card_data->always_on = 1;

	ret = dra7_snd_add_dai_link(card, &dra7_snd_dai[0], "ti,media");
	if (ret) {
		dev_err(card->dev, "failed to add media dai link %d\n", ret);
		return ret;
	}

	ret = dra7_snd_add_multichannel_dai_link(card, &dra7_snd_dai[1],
						 "ti,multichannel");
	if (ret) {
		dev_err(card->dev, "failed to add multichannel dai link %d\n",
			ret);
		return ret;
	}

	ret = dra7_snd_add_bt_dai_link(card, &dra7_snd_dai[2], "ti,bt");
	if (ret)
		dev_err(card->dev, "failed to add bluetooth dai link %d\n",
			ret);

	ret = dra7_snd_add_dai_link(card, &dra7_snd_dai[3], "ti,loopback");
	if (ret)
		dev_err(card->dev, "failed to add loopback dai link %d\n", ret);

	ret = snd_soc_register_card(card);
	if (ret) {
		dev_err(card->dev, "failed to register sound card %d\n", ret);
		goto err_card;
	}

	ret = snd_soc_add_card_controls(card, dra7_snd_controls,
					ARRAY_SIZE(dra7_snd_controls));
	if (ret) {
		dev_err(card->dev, "failed to add card controls %d\n", ret);
		goto err_card;
	}

	ret = dra7_mcasp_reparent(card, "mcasp3_ahclkx_mux", "atl_clkin2_ck");
	if (ret) {
		dev_err(card->dev, "failed to reparent McASP3 %d\n", ret);
		goto err_reparent;
	}

	ret = dra7_mcasp_reparent(card, "mcasp5_ahclkx_mux", "atl_clkin2_ck");
	if (ret) {
		dev_err(card->dev, "failed to reparent McASP5 %d\n", ret);
		goto err_reparent;
	}

	ret = dra7_mcasp_reparent(card, "mcasp6_ahclkx_mux", "atl_clkin1_ck");
	if (ret) {
		dev_err(card->dev, "failed to reparent McASP6 %d\n", ret);
		goto err_reparent;
	}

	ret = dra7_mcasp_reparent(card, "mcasp7_ahclkx_mux", "abe_24m_fclk");
	if (ret) {
		dev_err(card->dev, "failed to reparent McASP7 %d\n", ret);
		goto err_reparent;
	}

	return 0;

err_reparent:
	snd_soc_unregister_card(card);
err_card:
	snd_soc_card_reset_dai_links(card);
	return ret;
}
static int dra7_snd_remove(struct platform_device *pdev)
{
	struct snd_soc_card *card = platform_get_drvdata(pdev);

	snd_soc_unregister_card(card);
	snd_soc_card_reset_dai_links(card);

	return 0;
}

static const struct of_device_id dra7_snd_of_match[] = {
	{.compatible = "ti,dra7-evm-sound", },
	{ },
};
MODULE_DEVICE_TABLE(of, dra7_snd_of_match);

static struct platform_driver dra7_snd_driver = {
	.driver = {
		.name = "dra7-evm-sound",
		.owner = THIS_MODULE,
		.pm = &snd_soc_pm_ops,
		.of_match_table = dra7_snd_of_match,
	},
	.probe = dra7_snd_probe,
	.remove = dra7_snd_remove,
};

module_platform_driver(dra7_snd_driver);

MODULE_AUTHOR("Misael Lopez Cruz <misael.lopez@ti.com>");
MODULE_DESCRIPTION("ALSA SoC for DRA7 EVM");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:dra7-evm-sound");