summaryrefslogtreecommitdiffstats
path: root/sound/soc/intel/boards/sof_realtek_common.c
blob: cb1c21d822f6cbba481bc4782fc5f35e5b665e5f (plain)
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
// SPDX-License-Identifier: GPL-2.0-only
//
// Copyright(c) 2020 Intel Corporation. All rights reserved.

#include <linux/device.h>
#include <linux/kernel.h>
#include <sound/pcm.h>
#include <sound/pcm_params.h>
#include <sound/soc.h>
#include <sound/soc-acpi.h>
#include <sound/soc-dai.h>
#include <sound/soc-dapm.h>
#include <sound/sof.h>
#include <uapi/sound/asound.h>
#include "../../codecs/rt1011.h"
#include "../../codecs/rt1015.h"
#include "../../codecs/rt1308.h"
#include "../common/soc-intel-quirks.h"
#include "sof_realtek_common.h"

/*
 * Common structures and functions
 */
static const struct snd_kcontrol_new realtek_2spk_kcontrols[] = {
	SOC_DAPM_PIN_SWITCH("Left Spk"),
	SOC_DAPM_PIN_SWITCH("Right Spk"),

};

static const struct snd_soc_dapm_widget realtek_2spk_widgets[] = {
	SND_SOC_DAPM_SPK("Left Spk", NULL),
	SND_SOC_DAPM_SPK("Right Spk", NULL),
};

static const struct snd_kcontrol_new realtek_4spk_kcontrols[] = {
	SOC_DAPM_PIN_SWITCH("WL Ext Spk"),
	SOC_DAPM_PIN_SWITCH("WR Ext Spk"),
	SOC_DAPM_PIN_SWITCH("TL Ext Spk"),
	SOC_DAPM_PIN_SWITCH("TR Ext Spk"),
};

static const struct snd_soc_dapm_widget realtek_4spk_widgets[] = {
	SND_SOC_DAPM_SPK("WL Ext Spk", NULL),
	SND_SOC_DAPM_SPK("WR Ext Spk", NULL),
	SND_SOC_DAPM_SPK("TL Ext Spk", NULL),
	SND_SOC_DAPM_SPK("TR Ext Spk", NULL),
};

/* helper function to get the number of specific codec */
static unsigned int get_num_codecs(const char *hid)
{
	struct acpi_device *adev;
	unsigned int dev_num = 0;

	for_each_acpi_dev_match(adev, hid, NULL, -1)
		dev_num++;

	return dev_num;
}

/*
 * Realtek ALC1011
 */
static const struct snd_soc_dapm_route speaker_map_lr[] = {
	/* speaker */
	{ "Left Spk", NULL, "Left SPO" },
	{ "Right Spk", NULL, "Right SPO" },
};

static const struct snd_soc_dapm_route rt1011_4spk_routes[] = {
	{"WL Ext Spk", NULL, "WL SPO" },
	{"WR Ext Spk", NULL, "WR SPO" },
	{"TL Ext Spk", NULL, "TL SPO" },
	{"TR Ext Spk", NULL, "TR SPO" },
};

static struct snd_soc_codec_conf rt1011_2spk_codec_confs[] = {
	{
		.dlc = COMP_CODEC_CONF(RT1011_DEV0_NAME),
		.name_prefix = "Left",
	},
	{
		.dlc = COMP_CODEC_CONF(RT1011_DEV1_NAME),
		.name_prefix = "Right",
	},
};

static struct snd_soc_codec_conf rt1011_4spk_codec_confs[] = {
	{
		.dlc = COMP_CODEC_CONF(RT1011_DEV0_NAME),
		.name_prefix = "WL",
	},
	{
		.dlc = COMP_CODEC_CONF(RT1011_DEV1_NAME),
		.name_prefix = "WR",
	},
	{
		.dlc = COMP_CODEC_CONF(RT1011_DEV2_NAME),
		.name_prefix = "TL",
	},
	{
		.dlc = COMP_CODEC_CONF(RT1011_DEV3_NAME),
		.name_prefix = "TR",
	},
};

static struct snd_soc_dai_link_component rt1011_dai_link_components[] = {
	{
		.name = RT1011_DEV0_NAME,
		.dai_name = RT1011_CODEC_DAI,
	},
	{
		.name = RT1011_DEV1_NAME,
		.dai_name = RT1011_CODEC_DAI,
	},
	{
		.name = RT1011_DEV2_NAME,
		.dai_name = RT1011_CODEC_DAI,
	},
	{
		.name = RT1011_DEV3_NAME,
		.dai_name = RT1011_CODEC_DAI,
	},
};

static const struct {
	unsigned int tx;
	unsigned int rx;
} rt1011_tdm_mask[] = {
	{.tx = 0x4, .rx = 0x1},
	{.tx = 0x8, .rx = 0x2},
	{.tx = 0x1, .rx = 0x1},
	{.tx = 0x2, .rx = 0x2},
};

static int rt1011_hw_params(struct snd_pcm_substream *substream,
			    struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
	struct snd_soc_dai *codec_dai;
	int srate, i, ret = 0;

	srate = params_rate(params);

	for_each_rtd_codec_dais(rtd, i, codec_dai) {
		/* 100 Fs to drive 24 bit data */
		ret = snd_soc_dai_set_pll(codec_dai, 0, RT1011_PLL1_S_BCLK,
					  100 * srate, 256 * srate);
		if (ret < 0) {
			dev_err(codec_dai->dev, "fail to set pll, ret %d\n",
				ret);
			return ret;
		}

		ret = snd_soc_dai_set_sysclk(codec_dai, RT1011_FS_SYS_PRE_S_PLL1,
					     256 * srate, SND_SOC_CLOCK_IN);
		if (ret < 0) {
			dev_err(codec_dai->dev, "fail to set sysclk, ret %d\n",
				ret);
			return ret;
		}

		if (i >= ARRAY_SIZE(rt1011_tdm_mask)) {
			dev_err(codec_dai->dev, "invalid codec index %d\n",
				i);
			return -ENODEV;
		}

		ret = snd_soc_dai_set_tdm_slot(codec_dai, rt1011_tdm_mask[i].tx,
					       rt1011_tdm_mask[i].rx, 4,
					       params_width(params));
		if (ret < 0) {
			dev_err(codec_dai->dev, "fail to set tdm slot, ret %d\n",
				ret);
			return ret;
		}
	}

	return 0;
}

static const struct snd_soc_ops rt1011_ops = {
	.hw_params = rt1011_hw_params,
};

static int rt1011_init(struct snd_soc_pcm_runtime *rtd)
{
	struct snd_soc_card *card = rtd->card;
	unsigned int num_codecs = get_num_codecs(RT1011_ACPI_HID);
	int ret;

	switch (num_codecs) {
	case 2:
		if (!soc_intel_is_cml()) {
			ret = snd_soc_dapm_new_controls(&card->dapm, realtek_2spk_widgets,
							ARRAY_SIZE(realtek_2spk_widgets));
			if (ret) {
				dev_err(rtd->dev, "fail to add rt1011 widgets, ret %d\n",
					ret);
				return ret;
			}

			ret = snd_soc_add_card_controls(card, realtek_2spk_kcontrols,
							ARRAY_SIZE(realtek_2spk_kcontrols));
			if (ret) {
				dev_err(rtd->dev, "fail to add rt1011 kcontrols, ret %d\n",
					ret);
				return ret;
			}

			ret = snd_soc_dapm_add_routes(&card->dapm, speaker_map_lr,
						      ARRAY_SIZE(speaker_map_lr));
			if (ret) {
				dev_err(rtd->dev, "fail to add rt1011 routes, ret %d\n",
					ret);
				return ret;
			}

			break;
		}

		/*
		 * register speaker widgets "WL Ext Spk" and "WR Ext Spk" to
		 * keep backward compatible with cml devices
		 */
		fallthrough;
	case 4:
		ret = snd_soc_dapm_new_controls(&card->dapm, realtek_4spk_widgets,
						num_codecs);
		if (ret) {
			dev_err(rtd->dev, "fail to add rt1011 widgets, ret %d\n",
				ret);
			return ret;
		}

		ret = snd_soc_add_card_controls(card, realtek_4spk_kcontrols,
						num_codecs);
		if (ret) {
			dev_err(rtd->dev, "fail to add rt1011 controls, ret %d\n",
				ret);
			return ret;
		}

		ret = snd_soc_dapm_add_routes(&card->dapm, rt1011_4spk_routes,
					      num_codecs);
		if (ret) {
			dev_err(rtd->dev, "fail to add rt1011 routes, ret %d\n",
				ret);
			return ret;
		}
		break;
	default:
		dev_err(rtd->dev, "rt1011: invalid num_codecs %d\n", num_codecs);
		return -EINVAL;
	}

	return ret;
}

void sof_rt1011_dai_link(struct device *dev, struct snd_soc_dai_link *link)
{
	unsigned int num_codecs = get_num_codecs(RT1011_ACPI_HID);

	link->codecs = rt1011_dai_link_components;

	switch (num_codecs) {
	case 2:
	case 4:
		link->num_codecs = num_codecs;
		break;
	default:
		dev_err(dev, "rt1011: invalid num_codecs %d\n", num_codecs);
		break;
	}

	link->init = rt1011_init;
	link->ops = &rt1011_ops;
}
EXPORT_SYMBOL_NS(sof_rt1011_dai_link, SND_SOC_INTEL_SOF_REALTEK_COMMON);

void sof_rt1011_codec_conf(struct device *dev, struct snd_soc_card *card)
{
	unsigned int num_codecs = get_num_codecs(RT1011_ACPI_HID);

	switch (num_codecs) {
	case 2:
		if (soc_intel_is_cml()) {
			/*
			 * use name prefix 'WL' and 'WR' for speaker widgets to
			 * keep backward compatible with cml devices
			 */
			card->codec_conf = rt1011_4spk_codec_confs;
		} else {
			card->codec_conf = rt1011_2spk_codec_confs;
		}

		card->num_configs = num_codecs;
		break;
	case 4:
		card->codec_conf = rt1011_4spk_codec_confs;
		card->num_configs = ARRAY_SIZE(rt1011_4spk_codec_confs);
		break;
	default:
		dev_err(dev, "rt1011: invalid num_codecs %d\n", num_codecs);
		break;
	}

}
EXPORT_SYMBOL_NS(sof_rt1011_codec_conf, SND_SOC_INTEL_SOF_REALTEK_COMMON);

/*
 * rt1015:  i2c mode driver for ALC1015 and ALC1015Q
 * rt1015p: auto-mode driver for ALC1015, ALC1015Q, and ALC1015Q-VB
 *
 * For stereo output, there are always two amplifiers on the board.
 * However, the ACPI implements only one device instance (UID=0) if they
 * are sharing the same enable pin. The code will detect the number of
 * device instance and use corresponding DAPM structures for
 * initialization.
 */
static const struct snd_soc_dapm_route rt1015p_1dev_dapm_routes[] = {
	/* speaker */
	{ "Left Spk", NULL, "Speaker" },
	{ "Right Spk", NULL, "Speaker" },
};

static const struct snd_soc_dapm_route rt1015p_2dev_dapm_routes[] = {
	/* speaker */
	{ "Left Spk", NULL, "Left Speaker" },
	{ "Right Spk", NULL, "Right Speaker" },
};

static struct snd_soc_codec_conf rt1015p_codec_confs[] = {
	{
		.dlc = COMP_CODEC_CONF(RT1015P_DEV0_NAME),
		.name_prefix = "Left",
	},
	{
		.dlc = COMP_CODEC_CONF(RT1015P_DEV1_NAME),
		.name_prefix = "Right",
	},
};

static struct snd_soc_dai_link_component rt1015p_dai_link_components[] = {
	{
		.name = RT1015P_DEV0_NAME,
		.dai_name = RT1015P_CODEC_DAI,
	},
	{
		.name = RT1015P_DEV1_NAME,
		.dai_name = RT1015P_CODEC_DAI,
	},
};

static int rt1015p_get_num_codecs(void)
{
	static int dev_num;

	if (dev_num)
		return dev_num;

	if (!acpi_dev_present("RTL1015", "1", -1))
		dev_num = 1;
	else
		dev_num = 2;

	return dev_num;
}

static int rt1015p_hw_params(struct snd_pcm_substream *substream,
			     struct snd_pcm_hw_params *params)
{
	/* reserved for debugging purpose */

	return 0;
}

static const struct snd_soc_ops rt1015p_ops = {
	.hw_params = rt1015p_hw_params,
};

static int rt1015p_init(struct snd_soc_pcm_runtime *rtd)
{
	struct snd_soc_card *card = rtd->card;
	int ret;

	ret = snd_soc_dapm_new_controls(&card->dapm, realtek_2spk_widgets,
					ARRAY_SIZE(realtek_2spk_widgets));
	if (ret) {
		dev_err(rtd->dev, "fail to add rt1015p widgets, ret %d\n", ret);
		return ret;
	}

	ret = snd_soc_add_card_controls(card, realtek_2spk_kcontrols,
					ARRAY_SIZE(realtek_2spk_kcontrols));
	if (ret) {
		dev_err(rtd->dev, "fail to add rt1015p kcontrols, ret %d\n", ret);
		return ret;
	}

	if (rt1015p_get_num_codecs() == 1)
		ret = snd_soc_dapm_add_routes(&card->dapm, rt1015p_1dev_dapm_routes,
					      ARRAY_SIZE(rt1015p_1dev_dapm_routes));
	else
		ret = snd_soc_dapm_add_routes(&card->dapm, rt1015p_2dev_dapm_routes,
					      ARRAY_SIZE(rt1015p_2dev_dapm_routes));
	if (ret)
		dev_err(rtd->dev, "Speaker map addition failed: %d\n", ret);
	return ret;
}

void sof_rt1015p_dai_link(struct snd_soc_dai_link *link)
{
	link->codecs = rt1015p_dai_link_components;
	link->num_codecs = rt1015p_get_num_codecs();
	link->init = rt1015p_init;
	link->ops = &rt1015p_ops;
}
EXPORT_SYMBOL_NS(sof_rt1015p_dai_link, SND_SOC_INTEL_SOF_REALTEK_COMMON);

void sof_rt1015p_codec_conf(struct snd_soc_card *card)
{
	if (rt1015p_get_num_codecs() == 1)
		return;

	card->codec_conf = rt1015p_codec_confs;
	card->num_configs = ARRAY_SIZE(rt1015p_codec_confs);
}
EXPORT_SYMBOL_NS(sof_rt1015p_codec_conf, SND_SOC_INTEL_SOF_REALTEK_COMMON);

/*
 * RT1015 audio amplifier
 */

static const struct {
	unsigned int tx;
	unsigned int rx;
} rt1015_tdm_mask[] = {
	{.tx = 0x0, .rx = 0x1},
	{.tx = 0x0, .rx = 0x2},
};

static int rt1015_hw_params(struct snd_pcm_substream *substream,
			    struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
	struct snd_soc_dai_link *dai_link = rtd->dai_link;
	struct snd_soc_dai *codec_dai;
	int i, clk_freq;
	int ret = 0;

	clk_freq = sof_dai_get_bclk(rtd);

	if (clk_freq <= 0) {
		dev_err(rtd->dev, "fail to get bclk freq, ret %d\n", clk_freq);
		return -EINVAL;
	}

	for_each_rtd_codec_dais(rtd, i, codec_dai) {
		ret = snd_soc_dai_set_pll(codec_dai, 0, RT1015_PLL_S_BCLK,
					  clk_freq,
					  params_rate(params) * 256);
		if (ret) {
			dev_err(codec_dai->dev, "fail to set pll, ret %d\n",
				ret);
			return ret;
		}

		ret = snd_soc_dai_set_sysclk(codec_dai, RT1015_SCLK_S_PLL,
					     params_rate(params) * 256,
					     SND_SOC_CLOCK_IN);
		if (ret) {
			dev_err(codec_dai->dev, "fail to set sysclk, ret %d\n",
				ret);
			return ret;
		}

		switch (dai_link->dai_fmt & SND_SOC_DAIFMT_FORMAT_MASK) {
		case SND_SOC_DAIFMT_DSP_A:
		case SND_SOC_DAIFMT_DSP_B:
			/* 4-slot TDM */
			ret = snd_soc_dai_set_tdm_slot(codec_dai,
						       rt1015_tdm_mask[i].tx,
						       rt1015_tdm_mask[i].rx,
						       4,
						       params_width(params));
			if (ret < 0) {
				dev_err(codec_dai->dev, "fail to set tdm slot, ret %d\n",
					ret);
				return ret;
			}
			break;
		default:
			dev_dbg(codec_dai->dev, "codec is in I2S mode\n");
			break;
		}
	}

	return ret;
}

static struct snd_soc_ops rt1015_ops = {
	.hw_params = rt1015_hw_params,
};

static struct snd_soc_codec_conf rt1015_amp_conf[] = {
	{
		.dlc = COMP_CODEC_CONF(RT1015_DEV0_NAME),
		.name_prefix = "Left",
	},
	{
		.dlc = COMP_CODEC_CONF(RT1015_DEV1_NAME),
		.name_prefix = "Right",
	},
};

static struct snd_soc_dai_link_component rt1015_components[] = {
	{
		.name = RT1015_DEV0_NAME,
		.dai_name = RT1015_CODEC_DAI,
	},
	{
		.name = RT1015_DEV1_NAME,
		.dai_name = RT1015_CODEC_DAI,
	},
};

static int speaker_codec_init_lr(struct snd_soc_pcm_runtime *rtd)
{
	struct snd_soc_card *card = rtd->card;
	unsigned int num_codecs = get_num_codecs(RT1015_ACPI_HID);
	int ret;

	switch (num_codecs) {
	case 2:
		ret = snd_soc_dapm_new_controls(&card->dapm, realtek_2spk_widgets,
						ARRAY_SIZE(realtek_2spk_widgets));
		if (ret) {
			dev_err(rtd->dev, "fail to add rt1015 widgets, ret %d\n",
				ret);
			return ret;
		}

		ret = snd_soc_add_card_controls(card, realtek_2spk_kcontrols,
						ARRAY_SIZE(realtek_2spk_kcontrols));
		if (ret) {
			dev_err(rtd->dev, "fail to add rt1015 kcontrols, ret %d\n",
				ret);
			return ret;
		}

		ret = snd_soc_dapm_add_routes(&rtd->card->dapm, speaker_map_lr,
					      ARRAY_SIZE(speaker_map_lr));
		if (ret) {
			dev_err(rtd->dev, "fail to add rt1015 routes, ret %d\n",
				ret);
			return ret;
		}
		break;
	default:
		dev_err(rtd->dev, "rt1015: invalid num_codecs %d\n", num_codecs);
		return -EINVAL;
	}

	return ret;
}

void sof_rt1015_codec_conf(struct snd_soc_card *card)
{
	card->codec_conf = rt1015_amp_conf;
	card->num_configs = ARRAY_SIZE(rt1015_amp_conf);
}
EXPORT_SYMBOL_NS(sof_rt1015_codec_conf, SND_SOC_INTEL_SOF_REALTEK_COMMON);

void sof_rt1015_dai_link(struct snd_soc_dai_link *link)
{
	link->codecs = rt1015_components;
	link->num_codecs = ARRAY_SIZE(rt1015_components);
	link->init = speaker_codec_init_lr;
	link->ops = &rt1015_ops;
}
EXPORT_SYMBOL_NS(sof_rt1015_dai_link, SND_SOC_INTEL_SOF_REALTEK_COMMON);

/*
 * RT1308 audio amplifier
 */
static const struct snd_kcontrol_new rt1308_kcontrols[] = {
	SOC_DAPM_PIN_SWITCH("Speakers"),
};

static const struct snd_soc_dapm_widget rt1308_dapm_widgets[] = {
	SND_SOC_DAPM_SPK("Speakers", NULL),
};

static const struct snd_soc_dapm_route rt1308_dapm_routes[] = {
	/* speaker */
	{"Speakers", NULL, "SPOL"},
	{"Speakers", NULL, "SPOR"},
};

static struct snd_soc_dai_link_component rt1308_components[] = {
	{
		.name = RT1308_DEV0_NAME,
		.dai_name = RT1308_CODEC_DAI,
	}
};

static int rt1308_init(struct snd_soc_pcm_runtime *rtd)
{
	struct snd_soc_card *card = rtd->card;
	int ret;

	ret = snd_soc_dapm_new_controls(&card->dapm, rt1308_dapm_widgets,
					ARRAY_SIZE(rt1308_dapm_widgets));
	if (ret) {
		dev_err(rtd->dev, "fail to add dapm controls, ret %d\n", ret);
		return ret;
	}

	ret = snd_soc_add_card_controls(card, rt1308_kcontrols,
					ARRAY_SIZE(rt1308_kcontrols));
	if (ret) {
		dev_err(rtd->dev, "fail to add card controls, ret %d\n", ret);
		return ret;
	}

	ret = snd_soc_dapm_add_routes(&card->dapm, rt1308_dapm_routes,
				      ARRAY_SIZE(rt1308_dapm_routes));

	if (ret)
		dev_err(rtd->dev, "fail to add dapm routes, ret %d\n", ret);

	return ret;
}

static int rt1308_hw_params(struct snd_pcm_substream *substream,
			    struct snd_pcm_hw_params *params)
{
	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
	struct snd_soc_card *card = rtd->card;
	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
	int clk_id, clk_freq, pll_out;
	int ret;

	clk_id = RT1308_PLL_S_MCLK;
	/* get the tplg configured mclk. */
	clk_freq = sof_dai_get_mclk(rtd);

	pll_out = params_rate(params) * 512;

	/* Set rt1308 pll */
	ret = snd_soc_dai_set_pll(codec_dai, 0, clk_id, clk_freq, pll_out);
	if (ret < 0) {
		dev_err(card->dev, "Failed to set RT1308 PLL: %d\n", ret);
		return ret;
	}

	/* Set rt1308 sysclk */
	ret = snd_soc_dai_set_sysclk(codec_dai, RT1308_FS_SYS_S_PLL, pll_out,
				     SND_SOC_CLOCK_IN);
	if (ret < 0)
		dev_err(card->dev, "Failed to set RT1308 SYSCLK: %d\n", ret);

	return ret;
}

static const struct snd_soc_ops rt1308_ops = {
	.hw_params = rt1308_hw_params,
};

void sof_rt1308_dai_link(struct snd_soc_dai_link *link)
{
	link->codecs = rt1308_components;
	link->num_codecs = ARRAY_SIZE(rt1308_components);
	link->init = rt1308_init;
	link->ops = &rt1308_ops;
}
EXPORT_SYMBOL_NS(sof_rt1308_dai_link, SND_SOC_INTEL_SOF_REALTEK_COMMON);

/*
 * 2-amp Configuration for RT1019
 */

static const struct snd_soc_dapm_route rt1019p_dapm_routes[] = {
	/* speaker */
	{ "Left Spk", NULL, "Speaker" },
	{ "Right Spk", NULL, "Speaker" },
};

static struct snd_soc_dai_link_component rt1019p_components[] = {
	{
		.name = RT1019P_DEV0_NAME,
		.dai_name = RT1019P_CODEC_DAI,
	},
};

static int rt1019p_init(struct snd_soc_pcm_runtime *rtd)
{
	struct snd_soc_card *card = rtd->card;
	int ret;

	ret = snd_soc_dapm_new_controls(&card->dapm, realtek_2spk_widgets,
					ARRAY_SIZE(realtek_2spk_widgets));
	if (ret) {
		dev_err(rtd->dev, "fail to add rt1019p widgets, ret %d\n", ret);
		return ret;
	}

	ret = snd_soc_add_card_controls(card, realtek_2spk_kcontrols,
					ARRAY_SIZE(realtek_2spk_kcontrols));
	if (ret) {
		dev_err(rtd->dev, "fail to add rt1019p kcontrols, ret %d\n", ret);
		return ret;
	}

	ret = snd_soc_dapm_add_routes(&card->dapm, rt1019p_dapm_routes,
				      ARRAY_SIZE(rt1019p_dapm_routes));
	if (ret) {
		dev_err(rtd->dev, "Speaker map addition failed: %d\n", ret);
		return ret;
	}
	return ret;
}

void sof_rt1019p_dai_link(struct snd_soc_dai_link *link)
{
	link->codecs = rt1019p_components;
	link->num_codecs = ARRAY_SIZE(rt1019p_components);
	link->init = rt1019p_init;
}
EXPORT_SYMBOL_NS(sof_rt1019p_dai_link, SND_SOC_INTEL_SOF_REALTEK_COMMON);

MODULE_DESCRIPTION("ASoC Intel SOF Realtek helpers");
MODULE_LICENSE("GPL");