Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2// Copyright(c) 2019-2020 Intel Corporation.
3
4/*
5 * Intel SOF Machine Driver with Realtek rt5682 Codec
6 * and speaker codec MAX98357A or RT1015.
7 */
8#include <linux/i2c.h>
9#include <linux/input.h>
10#include <linux/module.h>
11#include <linux/platform_device.h>
12#include <linux/clk.h>
13#include <linux/dmi.h>
14#include <sound/core.h>
15#include <sound/jack.h>
16#include <sound/pcm.h>
17#include <sound/pcm_params.h>
18#include <sound/soc.h>
19#include <sound/sof.h>
20#include <sound/rt5682.h>
21#include <sound/rt5682s.h>
22#include <sound/soc-acpi.h>
23#include "../../codecs/rt5682.h"
24#include "../../codecs/rt5682s.h"
25#include "../../codecs/rt5645.h"
26#include "../common/soc-intel-quirks.h"
27#include "sof_board_helpers.h"
28#include "sof_maxim_common.h"
29#include "sof_realtek_common.h"
30
31/* Driver-specific board quirks: from bit 0 to 7 */
32#define SOF_RT5682_MCLK_EN BIT(0)
33
34/* Default: MCLK on, MCLK 19.2M, SSP0 */
35static unsigned long sof_rt5682_quirk = SOF_RT5682_MCLK_EN |
36 SOF_SSP_PORT_CODEC(0);
37
38static int sof_rt5682_quirk_cb(const struct dmi_system_id *id)
39{
40 sof_rt5682_quirk = (unsigned long)id->driver_data;
41 return 1;
42}
43
44static const struct dmi_system_id sof_rt5682_quirk_table[] = {
45 {
46 .callback = sof_rt5682_quirk_cb,
47 .matches = {
48 DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
49 DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max"),
50 },
51 .driver_data = (void *)(SOF_SSP_PORT_CODEC(2)),
52 },
53 {
54 .callback = sof_rt5682_quirk_cb,
55 .matches = {
56 DMI_MATCH(DMI_SYS_VENDOR, "AAEON"),
57 DMI_MATCH(DMI_PRODUCT_NAME, "UP-CHT01"),
58 },
59 .driver_data = (void *)(SOF_SSP_PORT_CODEC(2)),
60 },
61 {
62 .callback = sof_rt5682_quirk_cb,
63 .matches = {
64 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
65 DMI_MATCH(DMI_PRODUCT_NAME, "WhiskeyLake Client"),
66 },
67 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
68 SOF_SSP_PORT_CODEC(1)),
69 },
70 {
71 .callback = sof_rt5682_quirk_cb,
72 .matches = {
73 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Volteer"),
74 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98373_ALC5682I_I2S_UP4"),
75 },
76 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
77 SOF_SSP_PORT_CODEC(0) |
78 SOF_SSP_PORT_AMP(2) |
79 SOF_NUM_IDISP_HDMI(4)),
80 },
81 {
82 .callback = sof_rt5682_quirk_cb,
83 .matches = {
84 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
85 DMI_MATCH(DMI_PRODUCT_NAME, "Alder Lake Client Platform"),
86 DMI_MATCH(DMI_OEM_STRING, "AUDIO-ADL_MAX98373_ALC5682I_I2S"),
87 },
88 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
89 SOF_SSP_PORT_CODEC(0) |
90 SOF_SSP_PORT_AMP(2) |
91 SOF_NUM_IDISP_HDMI(4)),
92 },
93 {
94 .callback = sof_rt5682_quirk_cb,
95 .matches = {
96 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
97 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98390_ALC5682I_I2S"),
98 },
99 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
100 SOF_SSP_PORT_CODEC(0) |
101 SOF_SSP_PORT_AMP(2) |
102 SOF_NUM_IDISP_HDMI(4)),
103 },
104 {
105 .callback = sof_rt5682_quirk_cb,
106 .matches = {
107 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
108 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98360_ALC5682I_I2S_AMP_SSP2"),
109 },
110 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
111 SOF_SSP_PORT_CODEC(0) |
112 SOF_SSP_PORT_AMP(2) |
113 SOF_NUM_IDISP_HDMI(4)),
114 },
115 {
116 .callback = sof_rt5682_quirk_cb,
117 .matches = {
118 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Rex"),
119 },
120 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
121 SOF_SSP_PORT_CODEC(2) |
122 SOF_SSP_PORT_AMP(0) |
123 SOF_SSP_PORT_BT_OFFLOAD(1) |
124 SOF_BT_OFFLOAD_PRESENT
125 ),
126 },
127 {}
128};
129
130static struct snd_soc_jack_pin jack_pins[] = {
131 {
132 .pin = "Headphone Jack",
133 .mask = SND_JACK_HEADPHONE,
134 },
135 {
136 .pin = "Headset Mic",
137 .mask = SND_JACK_MICROPHONE,
138 },
139};
140
141static int sof_rt5682_codec_init(struct snd_soc_pcm_runtime *rtd)
142{
143 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
144 struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
145 struct snd_soc_jack *jack = &ctx->headset_jack;
146 int extra_jack_data;
147 int ret, mclk_freq;
148
149 if (ctx->rt5682.mclk_en) {
150 mclk_freq = sof_dai_get_mclk(rtd);
151 if (mclk_freq <= 0) {
152 dev_err(rtd->dev, "invalid mclk freq %d\n", mclk_freq);
153 return -EINVAL;
154 }
155
156 /* need to enable ASRC function for 24MHz mclk rate */
157 if (mclk_freq == 24000000) {
158 dev_info(rtd->dev, "enable ASRC\n");
159
160 switch (ctx->codec_type) {
161 case CODEC_RT5650:
162 rt5645_sel_asrc_clk_src(component,
163 RT5645_DA_STEREO_FILTER |
164 RT5645_AD_STEREO_FILTER,
165 RT5645_CLK_SEL_I2S1_ASRC);
166 rt5645_sel_asrc_clk_src(component,
167 RT5645_DA_MONO_L_FILTER |
168 RT5645_DA_MONO_R_FILTER,
169 RT5645_CLK_SEL_I2S2_ASRC);
170 break;
171 case CODEC_RT5682:
172 rt5682_sel_asrc_clk_src(component,
173 RT5682_DA_STEREO1_FILTER |
174 RT5682_AD_STEREO1_FILTER,
175 RT5682_CLK_SEL_I2S1_ASRC);
176 break;
177 case CODEC_RT5682S:
178 rt5682s_sel_asrc_clk_src(component,
179 RT5682S_DA_STEREO1_FILTER |
180 RT5682S_AD_STEREO1_FILTER,
181 RT5682S_CLK_SEL_I2S1_ASRC);
182 break;
183 default:
184 dev_err(rtd->dev, "invalid codec type %d\n",
185 ctx->codec_type);
186 return -EINVAL;
187 }
188 }
189
190 if (ctx->rt5682.is_legacy_cpu) {
191 /*
192 * The firmware might enable the clock at
193 * boot (this information may or may not
194 * be reflected in the enable clock register).
195 * To change the rate we must disable the clock
196 * first to cover these cases. Due to common
197 * clock framework restrictions that do not allow
198 * to disable a clock that has not been enabled,
199 * we need to enable the clock first.
200 */
201 ret = clk_prepare_enable(ctx->rt5682.mclk);
202 if (!ret)
203 clk_disable_unprepare(ctx->rt5682.mclk);
204
205 ret = clk_set_rate(ctx->rt5682.mclk, 19200000);
206
207 if (ret)
208 dev_err(rtd->dev, "unable to set MCLK rate\n");
209 }
210 }
211
212 /*
213 * Headset buttons map to the google Reference headset.
214 * These can be configured by userspace.
215 */
216 ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
217 SND_JACK_HEADSET | SND_JACK_BTN_0 |
218 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
219 SND_JACK_BTN_3,
220 jack,
221 jack_pins,
222 ARRAY_SIZE(jack_pins));
223 if (ret) {
224 dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
225 return ret;
226 }
227
228 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
229 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
230 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
231 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
232
233 if (ctx->codec_type == CODEC_RT5650) {
234 extra_jack_data = SND_JACK_MICROPHONE | SND_JACK_BTN_0;
235 ret = snd_soc_component_set_jack(component, jack, &extra_jack_data);
236 } else
237 ret = snd_soc_component_set_jack(component, jack, NULL);
238
239 if (ret) {
240 dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
241 return ret;
242 }
243
244 return ret;
245};
246
247static void sof_rt5682_codec_exit(struct snd_soc_pcm_runtime *rtd)
248{
249 struct snd_soc_component *component = snd_soc_rtd_to_codec(rtd, 0)->component;
250
251 snd_soc_component_set_jack(component, NULL, NULL);
252}
253
254static int sof_rt5682_hw_params(struct snd_pcm_substream *substream,
255 struct snd_pcm_hw_params *params)
256{
257 struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
258 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
259 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
260 int pll_id, pll_source, pll_in, pll_out, clk_id, ret;
261
262 if (ctx->rt5682.mclk_en) {
263 if (ctx->rt5682.is_legacy_cpu) {
264 ret = clk_prepare_enable(ctx->rt5682.mclk);
265 if (ret < 0) {
266 dev_err(rtd->dev,
267 "could not configure MCLK state");
268 return ret;
269 }
270 }
271
272 switch (ctx->codec_type) {
273 case CODEC_RT5650:
274 pll_source = RT5645_PLL1_S_MCLK;
275 break;
276 case CODEC_RT5682:
277 pll_source = RT5682_PLL1_S_MCLK;
278 break;
279 case CODEC_RT5682S:
280 pll_source = RT5682S_PLL_S_MCLK;
281 break;
282 default:
283 dev_err(rtd->dev, "invalid codec type %d\n",
284 ctx->codec_type);
285 return -EINVAL;
286 }
287
288 /* get the tplg configured mclk. */
289 pll_in = sof_dai_get_mclk(rtd);
290 if (pll_in <= 0) {
291 dev_err(rtd->dev, "invalid mclk freq %d\n", pll_in);
292 return -EINVAL;
293 }
294 } else {
295 switch (ctx->codec_type) {
296 case CODEC_RT5650:
297 pll_source = RT5645_PLL1_S_BCLK1;
298 break;
299 case CODEC_RT5682:
300 pll_source = RT5682_PLL1_S_BCLK1;
301 break;
302 case CODEC_RT5682S:
303 pll_source = RT5682S_PLL_S_BCLK1;
304 break;
305 default:
306 dev_err(rtd->dev, "invalid codec type %d\n",
307 ctx->codec_type);
308 return -EINVAL;
309 }
310
311 /* get the tplg configured bclk. */
312 pll_in = sof_dai_get_bclk(rtd);
313 if (pll_in <= 0) {
314 dev_err(rtd->dev, "invalid bclk freq %d\n", pll_in);
315 return -EINVAL;
316 }
317 }
318
319 pll_out = params_rate(params) * 512;
320
321 /* when MCLK is 512FS, no need to set PLL configuration additionally. */
322 if (pll_in == pll_out) {
323 switch (ctx->codec_type) {
324 case CODEC_RT5650:
325 clk_id = RT5645_SCLK_S_MCLK;
326 break;
327 case CODEC_RT5682:
328 clk_id = RT5682_SCLK_S_MCLK;
329 break;
330 case CODEC_RT5682S:
331 clk_id = RT5682S_SCLK_S_MCLK;
332 break;
333 default:
334 dev_err(rtd->dev, "invalid codec type %d\n",
335 ctx->codec_type);
336 return -EINVAL;
337 }
338 } else {
339 switch (ctx->codec_type) {
340 case CODEC_RT5650:
341 pll_id = 0; /* not used in codec driver */
342 clk_id = RT5645_SCLK_S_PLL1;
343 break;
344 case CODEC_RT5682:
345 pll_id = RT5682_PLL1;
346 clk_id = RT5682_SCLK_S_PLL1;
347 break;
348 case CODEC_RT5682S:
349 /* check plla_table and pllb_table in rt5682s.c */
350 switch (pll_in) {
351 case 3072000:
352 case 24576000:
353 /*
354 * For MCLK = 24.576MHz and sample rate = 96KHz case, use PLL1 We don't test
355 * pll_out or params_rate() here since rt5682s PLL2 doesn't support 24.576MHz
356 * input, so we have no choice but to use PLL1. Besides, we will not use PLL at
357 * all if pll_in == pll_out. ex, MCLK = 24.576Mhz and sample rate = 48KHz
358 */
359 pll_id = RT5682S_PLL1;
360 clk_id = RT5682S_SCLK_S_PLL1;
361 break;
362 default:
363 pll_id = RT5682S_PLL2;
364 clk_id = RT5682S_SCLK_S_PLL2;
365 break;
366 }
367 break;
368 default:
369 dev_err(rtd->dev, "invalid codec type %d\n", ctx->codec_type);
370 return -EINVAL;
371 }
372
373 /* Configure pll for codec */
374 ret = snd_soc_dai_set_pll(codec_dai, pll_id, pll_source, pll_in,
375 pll_out);
376 if (ret < 0)
377 dev_err(rtd->dev, "snd_soc_dai_set_pll err = %d\n", ret);
378 }
379
380 /* Configure sysclk for codec */
381 ret = snd_soc_dai_set_sysclk(codec_dai, clk_id,
382 pll_out, SND_SOC_CLOCK_IN);
383 if (ret < 0)
384 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
385
386 /*
387 * slot_width should equal or large than data length, set them
388 * be the same
389 */
390 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x0, 0x0, 2,
391 params_width(params));
392 if (ret < 0) {
393 dev_err(rtd->dev, "set TDM slot err:%d\n", ret);
394 return ret;
395 }
396
397 return ret;
398}
399
400static const struct snd_soc_ops sof_rt5682_ops = {
401 .hw_params = sof_rt5682_hw_params,
402};
403
404static int sof_card_late_probe(struct snd_soc_card *card)
405{
406 struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
407 struct snd_soc_dapm_context *dapm = &card->dapm;
408 int err;
409
410 if (ctx->amp_type == CODEC_MAX98373) {
411 /* Disable Left and Right Spk pin after boot */
412 snd_soc_dapm_disable_pin(dapm, "Left Spk");
413 snd_soc_dapm_disable_pin(dapm, "Right Spk");
414 err = snd_soc_dapm_sync(dapm);
415 if (err < 0)
416 return err;
417 }
418
419 return sof_intel_board_card_late_probe(card);
420}
421
422static const struct snd_kcontrol_new sof_controls[] = {
423 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
424 SOC_DAPM_PIN_SWITCH("Headset Mic"),
425};
426
427static const struct snd_soc_dapm_widget sof_widgets[] = {
428 SND_SOC_DAPM_HP("Headphone Jack", NULL),
429 SND_SOC_DAPM_MIC("Headset Mic", NULL),
430};
431
432static const struct snd_soc_dapm_route sof_map[] = {
433 /* HP jack connectors - unknown if we have jack detection */
434 { "Headphone Jack", NULL, "HPOL" },
435 { "Headphone Jack", NULL, "HPOR" },
436
437 /* other jacks */
438 { "IN1P", NULL, "Headset Mic" },
439};
440
441static const struct snd_kcontrol_new rt5650_spk_kcontrols[] = {
442 SOC_DAPM_PIN_SWITCH("Left Spk"),
443 SOC_DAPM_PIN_SWITCH("Right Spk"),
444
445};
446
447static const struct snd_soc_dapm_widget rt5650_spk_widgets[] = {
448 SND_SOC_DAPM_SPK("Left Spk", NULL),
449 SND_SOC_DAPM_SPK("Right Spk", NULL),
450};
451
452static const struct snd_soc_dapm_route rt5650_spk_dapm_routes[] = {
453 /* speaker */
454 { "Left Spk", NULL, "SPOL" },
455 { "Right Spk", NULL, "SPOR" },
456};
457
458static int rt5650_spk_init(struct snd_soc_pcm_runtime *rtd)
459{
460 struct snd_soc_card *card = rtd->card;
461 int ret;
462
463 ret = snd_soc_dapm_new_controls(&card->dapm, rt5650_spk_widgets,
464 ARRAY_SIZE(rt5650_spk_widgets));
465 if (ret) {
466 dev_err(rtd->dev, "fail to add rt5650 spk widgets, ret %d\n",
467 ret);
468 return ret;
469 }
470
471 ret = snd_soc_add_card_controls(card, rt5650_spk_kcontrols,
472 ARRAY_SIZE(rt5650_spk_kcontrols));
473 if (ret) {
474 dev_err(rtd->dev, "fail to add rt5650 spk kcontrols, ret %d\n",
475 ret);
476 return ret;
477 }
478
479 ret = snd_soc_dapm_add_routes(&card->dapm, rt5650_spk_dapm_routes,
480 ARRAY_SIZE(rt5650_spk_dapm_routes));
481 if (ret)
482 dev_err(rtd->dev, "fail to add dapm routes, ret=%d\n", ret);
483
484 return ret;
485}
486
487/* sof audio machine driver for rt5682 codec */
488static struct snd_soc_card sof_audio_card_rt5682 = {
489 .name = "rt5682", /* the sof- prefix is added by the core */
490 .owner = THIS_MODULE,
491 .controls = sof_controls,
492 .num_controls = ARRAY_SIZE(sof_controls),
493 .dapm_widgets = sof_widgets,
494 .num_dapm_widgets = ARRAY_SIZE(sof_widgets),
495 .dapm_routes = sof_map,
496 .num_dapm_routes = ARRAY_SIZE(sof_map),
497 .fully_routed = true,
498 .late_probe = sof_card_late_probe,
499};
500
501static struct snd_soc_dai_link_component rt5682_component[] = {
502 {
503 .name = "i2c-10EC5682:00",
504 .dai_name = "rt5682-aif1",
505 }
506};
507
508static struct snd_soc_dai_link_component rt5682s_component[] = {
509 {
510 .name = "i2c-RTL5682:00",
511 .dai_name = "rt5682s-aif1",
512 }
513};
514
515static struct snd_soc_dai_link_component rt5650_components[] = {
516 {
517 .name = "i2c-10EC5650:00",
518 .dai_name = "rt5645-aif1",
519 },
520 {
521 .name = "i2c-10EC5650:00",
522 .dai_name = "rt5645-aif2",
523 }
524};
525
526static int
527sof_card_dai_links_create(struct device *dev, struct snd_soc_card *card,
528 struct sof_card_private *ctx)
529{
530 int ret;
531
532 ret = sof_intel_board_set_dai_link(dev, card, ctx);
533 if (ret)
534 return ret;
535
536 if (!ctx->codec_link) {
537 dev_err(dev, "codec link not available");
538 return -EINVAL;
539 }
540
541 /* codec-specific fields for headphone codec */
542 switch (ctx->codec_type) {
543 case CODEC_RT5650:
544 ctx->codec_link->codecs = &rt5650_components[0];
545 ctx->codec_link->num_codecs = 1;
546 break;
547 case CODEC_RT5682:
548 ctx->codec_link->codecs = rt5682_component;
549 ctx->codec_link->num_codecs = ARRAY_SIZE(rt5682_component);
550 break;
551 case CODEC_RT5682S:
552 ctx->codec_link->codecs = rt5682s_component;
553 ctx->codec_link->num_codecs = ARRAY_SIZE(rt5682s_component);
554 break;
555 default:
556 dev_err(dev, "invalid codec type %d\n", ctx->codec_type);
557 return -EINVAL;
558 }
559
560 ctx->codec_link->init = sof_rt5682_codec_init;
561 ctx->codec_link->exit = sof_rt5682_codec_exit;
562 ctx->codec_link->ops = &sof_rt5682_ops;
563
564 if (!ctx->rt5682.is_legacy_cpu) {
565 /*
566 * Currently, On SKL+ platforms MCLK will be turned off in sof
567 * runtime suspended, and it will go into runtime suspended
568 * right after playback is stop. However, rt5682 will output
569 * static noise if sysclk turns off during playback. Set
570 * ignore_pmdown_time to power down rt5682 immediately and
571 * avoid the noise.
572 * It can be removed once we can control MCLK by driver.
573 */
574 ctx->codec_link->ignore_pmdown_time = 1;
575 }
576
577 if (ctx->amp_type == CODEC_NONE)
578 return 0;
579
580 if (!ctx->amp_link) {
581 dev_err(dev, "amp link not available");
582 return -EINVAL;
583 }
584
585 /* codec-specific fields for speaker amplifier */
586 switch (ctx->amp_type) {
587 case CODEC_MAX98357A:
588 max_98357a_dai_link(ctx->amp_link);
589 break;
590 case CODEC_MAX98360A:
591 max_98360a_dai_link(ctx->amp_link);
592 break;
593 case CODEC_MAX98373:
594 max_98373_dai_link(dev, ctx->amp_link);
595 break;
596 case CODEC_MAX98390:
597 max_98390_dai_link(dev, ctx->amp_link);
598 break;
599 case CODEC_RT1011:
600 sof_rt1011_dai_link(dev, ctx->amp_link);
601 break;
602 case CODEC_RT1015:
603 sof_rt1015_dai_link(ctx->amp_link);
604 break;
605 case CODEC_RT1015P:
606 sof_rt1015p_dai_link(ctx->amp_link);
607 break;
608 case CODEC_RT1019P:
609 sof_rt1019p_dai_link(ctx->amp_link);
610 break;
611 case CODEC_RT5650:
612 /* use AIF2 to support speaker pipeline */
613 ctx->amp_link->codecs = &rt5650_components[1];
614 ctx->amp_link->num_codecs = 1;
615 ctx->amp_link->init = rt5650_spk_init;
616 ctx->amp_link->ops = &sof_rt5682_ops;
617 break;
618 default:
619 dev_err(dev, "invalid amp type %d\n", ctx->amp_type);
620 return -EINVAL;
621 }
622
623 return 0;
624}
625
626#define GLK_LINK_ORDER SOF_LINK_ORDER(SOF_LINK_AMP, \
627 SOF_LINK_CODEC, \
628 SOF_LINK_DMIC01, \
629 SOF_LINK_IDISP_HDMI, \
630 SOF_LINK_NONE, \
631 SOF_LINK_NONE, \
632 SOF_LINK_NONE)
633
634static int sof_audio_probe(struct platform_device *pdev)
635{
636 struct snd_soc_acpi_mach *mach = pdev->dev.platform_data;
637 struct sof_card_private *ctx;
638 char *card_name;
639 int ret;
640
641 if (pdev->id_entry && pdev->id_entry->driver_data)
642 sof_rt5682_quirk = (unsigned long)pdev->id_entry->driver_data;
643
644 dmi_check_system(sof_rt5682_quirk_table);
645
646 dev_dbg(&pdev->dev, "sof_rt5682_quirk = %lx\n", sof_rt5682_quirk);
647
648 /* initialize ctx with board quirk */
649 ctx = sof_intel_board_get_ctx(&pdev->dev, sof_rt5682_quirk);
650 if (!ctx)
651 return -ENOMEM;
652
653 if (ctx->codec_type == CODEC_RT5650) {
654 card_name = devm_kstrdup(&pdev->dev, "rt5650", GFP_KERNEL);
655 if (!card_name)
656 return -ENOMEM;
657
658 sof_audio_card_rt5682.name = card_name;
659
660 /* create speaker dai link also */
661 if (ctx->amp_type == CODEC_NONE)
662 ctx->amp_type = CODEC_RT5650;
663 }
664
665 if (mach->mach_params.codec_mask & IDISP_CODEC_MASK)
666 ctx->hdmi.idisp_codec = true;
667
668 if (soc_intel_is_byt() || soc_intel_is_cht()) {
669 ctx->rt5682.is_legacy_cpu = true;
670 ctx->dmic_be_num = 0;
671 /* HDMI is not supported by SOF on Baytrail/CherryTrail */
672 ctx->hdmi_num = 0;
673 } else if (soc_intel_is_glk()) {
674 /* dmic16k not support */
675 ctx->dmic_be_num = 1;
676
677 /* overwrite the DAI link order for GLK boards */
678 ctx->link_order_overwrite = GLK_LINK_ORDER;
679
680 /* backward-compatible with existing devices */
681 switch (ctx->amp_type) {
682 case CODEC_MAX98357A:
683 card_name = devm_kstrdup(&pdev->dev, "glkrt5682max",
684 GFP_KERNEL);
685 if (!card_name)
686 return -ENOMEM;
687
688 sof_audio_card_rt5682.name = card_name;
689 break;
690 default:
691 break;
692 }
693 } else if (soc_intel_is_cml()) {
694 /* backward-compatible with existing devices */
695 switch (ctx->amp_type) {
696 case CODEC_RT1011:
697 card_name = devm_kstrdup(&pdev->dev, "cml_rt1011_rt5682",
698 GFP_KERNEL);
699 if (!card_name)
700 return -ENOMEM;
701
702 sof_audio_card_rt5682.name = card_name;
703 break;
704 default:
705 break;
706 }
707 }
708
709 if (sof_rt5682_quirk & SOF_RT5682_MCLK_EN) {
710 ctx->rt5682.mclk_en = true;
711
712 /* need to get main clock from pmc */
713 if (ctx->rt5682.is_legacy_cpu) {
714 ctx->rt5682.mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
715 if (IS_ERR(ctx->rt5682.mclk)) {
716 ret = PTR_ERR(ctx->rt5682.mclk);
717
718 dev_err(&pdev->dev,
719 "Failed to get MCLK from pmc_plt_clk_3: %d\n",
720 ret);
721 return ret;
722 }
723
724 ret = clk_prepare_enable(ctx->rt5682.mclk);
725 if (ret < 0) {
726 dev_err(&pdev->dev,
727 "could not configure MCLK state");
728 return ret;
729 }
730 }
731 }
732
733 /* update dai_link */
734 ret = sof_card_dai_links_create(&pdev->dev, &sof_audio_card_rt5682, ctx);
735 if (ret)
736 return ret;
737
738 /* update codec_conf */
739 switch (ctx->amp_type) {
740 case CODEC_MAX98373:
741 max_98373_set_codec_conf(&sof_audio_card_rt5682);
742 break;
743 case CODEC_MAX98390:
744 max_98390_set_codec_conf(&pdev->dev, &sof_audio_card_rt5682);
745 break;
746 case CODEC_RT1011:
747 sof_rt1011_codec_conf(&pdev->dev, &sof_audio_card_rt5682);
748 break;
749 case CODEC_RT1015:
750 sof_rt1015_codec_conf(&sof_audio_card_rt5682);
751 break;
752 case CODEC_RT1015P:
753 sof_rt1015p_codec_conf(&sof_audio_card_rt5682);
754 break;
755 case CODEC_MAX98357A:
756 case CODEC_MAX98360A:
757 case CODEC_RT1019P:
758 case CODEC_RT5650:
759 case CODEC_NONE:
760 /* no codec conf required */
761 break;
762 default:
763 dev_err(&pdev->dev, "invalid amp type %d\n", ctx->amp_type);
764 return -EINVAL;
765 }
766
767 sof_audio_card_rt5682.dev = &pdev->dev;
768
769 /* set platform name for each dailink */
770 ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_rt5682,
771 mach->mach_params.platform);
772 if (ret)
773 return ret;
774
775 snd_soc_card_set_drvdata(&sof_audio_card_rt5682, ctx);
776
777 return devm_snd_soc_register_card(&pdev->dev,
778 &sof_audio_card_rt5682);
779}
780
781static const struct platform_device_id board_ids[] = {
782 {
783 .name = "sof_rt5682",
784 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
785 SOF_SSP_PORT_CODEC(2)),
786 },
787 {
788 .name = "glk_rt5682_def",
789 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
790 SOF_SSP_PORT_CODEC(2) |
791 SOF_SSP_PORT_AMP(1)),
792 },
793 {
794 .name = "icl_rt5682_def",
795 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
796 SOF_SSP_PORT_CODEC(0)),
797 },
798 {
799 .name = "cml_rt5682_def",
800 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
801 SOF_SSP_PORT_CODEC(0) |
802 SOF_SSP_PORT_AMP(1)),
803 },
804 {
805 .name = "jsl_rt5682_def",
806 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
807 SOF_SSP_PORT_CODEC(0) |
808 SOF_SSP_PORT_AMP(1)),
809 },
810 {
811 .name = "tgl_rt5682_def",
812 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
813 SOF_SSP_PORT_CODEC(0) |
814 SOF_SSP_PORT_AMP(1) |
815 SOF_NUM_IDISP_HDMI(4) |
816 SOF_SSP_PORT_BT_OFFLOAD(2) |
817 SOF_BT_OFFLOAD_PRESENT),
818 },
819 {
820 .name = "adl_rt5682_def",
821 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
822 SOF_SSP_PORT_CODEC(0) |
823 SOF_SSP_PORT_AMP(1) |
824 SOF_NUM_IDISP_HDMI(4) |
825 SOF_SSP_PORT_BT_OFFLOAD(2) |
826 SOF_BT_OFFLOAD_PRESENT),
827 },
828 {
829 .name = "adl_mx98357_rt5682",
830 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
831 SOF_SSP_PORT_CODEC(0) |
832 SOF_SSP_PORT_AMP(2) |
833 SOF_NUM_IDISP_HDMI(4)),
834 },
835 {
836 .name = "adl_rt5682_c1_h02",
837 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
838 SOF_SSP_PORT_CODEC(1) |
839 /* SSP 0 and SSP 2 are used for HDMI IN */
840 SOF_SSP_MASK_HDMI_CAPTURE(0x5)),
841 },
842 {
843 .name = "rpl_mx98357_rt5682",
844 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
845 SOF_SSP_PORT_CODEC(0) |
846 SOF_SSP_PORT_AMP(2) |
847 SOF_NUM_IDISP_HDMI(4)),
848 },
849 {
850 .name = "rpl_rt5682_def",
851 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
852 SOF_SSP_PORT_CODEC(0) |
853 SOF_SSP_PORT_AMP(1) |
854 SOF_NUM_IDISP_HDMI(4) |
855 SOF_SSP_PORT_BT_OFFLOAD(2) |
856 SOF_BT_OFFLOAD_PRESENT),
857 },
858 {
859 .name = "rpl_rt5682_c1_h02",
860 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
861 SOF_SSP_PORT_CODEC(1) |
862 /* SSP 0 and SSP 2 are used for HDMI IN */
863 SOF_SSP_MASK_HDMI_CAPTURE(0x5)),
864 },
865 {
866 .name = "mtl_rt5682_def",
867 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
868 SOF_SSP_PORT_CODEC(0) |
869 SOF_SSP_PORT_AMP(1) |
870 SOF_SSP_PORT_BT_OFFLOAD(2) |
871 SOF_BT_OFFLOAD_PRESENT),
872 },
873 {
874 .name = "mtl_rt5682_c1_h02",
875 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
876 SOF_SSP_PORT_CODEC(1) |
877 /* SSP 0 and SSP 2 are used for HDMI IN */
878 SOF_SSP_MASK_HDMI_CAPTURE(0x5)),
879 },
880 {
881 .name = "arl_rt5682_c1_h02",
882 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
883 SOF_SSP_PORT_CODEC(1) |
884 /* SSP 0 and SSP 2 are used for HDMI IN */
885 SOF_SSP_MASK_HDMI_CAPTURE(0x5)),
886 },
887 {
888 .name = "ptl_rt5682_def",
889 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
890 SOF_SSP_PORT_CODEC(0) |
891 SOF_SSP_PORT_AMP(1) |
892 SOF_SSP_PORT_BT_OFFLOAD(2) |
893 SOF_BT_OFFLOAD_PRESENT),
894 },
895 { }
896};
897MODULE_DEVICE_TABLE(platform, board_ids);
898
899static struct platform_driver sof_audio = {
900 .probe = sof_audio_probe,
901 .driver = {
902 .name = "sof_rt5682",
903 .pm = &snd_soc_pm_ops,
904 },
905 .id_table = board_ids,
906};
907module_platform_driver(sof_audio)
908
909/* Module information */
910MODULE_DESCRIPTION("SOF Audio Machine driver");
911MODULE_AUTHOR("Bard Liao <bard.liao@intel.com>");
912MODULE_AUTHOR("Sathya Prakash M R <sathya.prakash.m.r@intel.com>");
913MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>");
914MODULE_AUTHOR("Mac Chiang <mac.chiang@intel.com>");
915MODULE_LICENSE("GPL v2");
916MODULE_IMPORT_NS("SND_SOC_INTEL_SOF_BOARD_HELPERS");
917MODULE_IMPORT_NS("SND_SOC_INTEL_SOF_MAXIM_COMMON");
918MODULE_IMPORT_NS("SND_SOC_INTEL_SOF_REALTEK_COMMON");
1// SPDX-License-Identifier: GPL-2.0-only
2// Copyright(c) 2019-2020 Intel Corporation.
3
4/*
5 * Intel SOF Machine Driver with Realtek rt5682 Codec
6 * and speaker codec MAX98357A or RT1015.
7 */
8#include <linux/i2c.h>
9#include <linux/input.h>
10#include <linux/module.h>
11#include <linux/platform_device.h>
12#include <linux/clk.h>
13#include <linux/dmi.h>
14#include <sound/core.h>
15#include <sound/jack.h>
16#include <sound/pcm.h>
17#include <sound/pcm_params.h>
18#include <sound/soc.h>
19#include <sound/sof.h>
20#include <sound/rt5682.h>
21#include <sound/rt5682s.h>
22#include <sound/soc-acpi.h>
23#include "../../codecs/rt5682.h"
24#include "../../codecs/rt5682s.h"
25#include "../../codecs/hdac_hdmi.h"
26#include "../common/soc-intel-quirks.h"
27#include "hda_dsp_common.h"
28#include "sof_maxim_common.h"
29#include "sof_realtek_common.h"
30
31#define NAME_SIZE 32
32
33#define SOF_RT5682_SSP_CODEC(quirk) ((quirk) & GENMASK(2, 0))
34#define SOF_RT5682_SSP_CODEC_MASK (GENMASK(2, 0))
35#define SOF_RT5682_MCLK_EN BIT(3)
36#define SOF_RT5682_MCLK_24MHZ BIT(4)
37#define SOF_SPEAKER_AMP_PRESENT BIT(5)
38#define SOF_RT5682_SSP_AMP_SHIFT 6
39#define SOF_RT5682_SSP_AMP_MASK (GENMASK(8, 6))
40#define SOF_RT5682_SSP_AMP(quirk) \
41 (((quirk) << SOF_RT5682_SSP_AMP_SHIFT) & SOF_RT5682_SSP_AMP_MASK)
42#define SOF_RT5682_MCLK_BYTCHT_EN BIT(9)
43#define SOF_RT5682_NUM_HDMIDEV_SHIFT 10
44#define SOF_RT5682_NUM_HDMIDEV_MASK (GENMASK(12, 10))
45#define SOF_RT5682_NUM_HDMIDEV(quirk) \
46 ((quirk << SOF_RT5682_NUM_HDMIDEV_SHIFT) & SOF_RT5682_NUM_HDMIDEV_MASK)
47#define SOF_RT1011_SPEAKER_AMP_PRESENT BIT(13)
48#define SOF_RT1015_SPEAKER_AMP_PRESENT BIT(14)
49#define SOF_RT1015P_SPEAKER_AMP_PRESENT BIT(16)
50#define SOF_MAX98373_SPEAKER_AMP_PRESENT BIT(17)
51#define SOF_MAX98360A_SPEAKER_AMP_PRESENT BIT(18)
52
53/* BT audio offload: reserve 3 bits for future */
54#define SOF_BT_OFFLOAD_SSP_SHIFT 19
55#define SOF_BT_OFFLOAD_SSP_MASK (GENMASK(21, 19))
56#define SOF_BT_OFFLOAD_SSP(quirk) \
57 (((quirk) << SOF_BT_OFFLOAD_SSP_SHIFT) & SOF_BT_OFFLOAD_SSP_MASK)
58#define SOF_SSP_BT_OFFLOAD_PRESENT BIT(22)
59#define SOF_RT5682S_HEADPHONE_CODEC_PRESENT BIT(23)
60#define SOF_MAX98390_SPEAKER_AMP_PRESENT BIT(24)
61#define SOF_MAX98390_TWEETER_SPEAKER_PRESENT BIT(25)
62#define SOF_RT1019_SPEAKER_AMP_PRESENT BIT(26)
63
64
65/* Default: MCLK on, MCLK 19.2M, SSP0 */
66static unsigned long sof_rt5682_quirk = SOF_RT5682_MCLK_EN |
67 SOF_RT5682_SSP_CODEC(0);
68
69static int is_legacy_cpu;
70
71struct sof_hdmi_pcm {
72 struct list_head head;
73 struct snd_soc_dai *codec_dai;
74 struct snd_soc_jack hdmi_jack;
75 int device;
76};
77
78struct sof_card_private {
79 struct clk *mclk;
80 struct snd_soc_jack sof_headset;
81 struct list_head hdmi_pcm_list;
82 bool common_hdmi_codec_drv;
83 bool idisp_codec;
84};
85
86static int sof_rt5682_quirk_cb(const struct dmi_system_id *id)
87{
88 sof_rt5682_quirk = (unsigned long)id->driver_data;
89 return 1;
90}
91
92static const struct dmi_system_id sof_rt5682_quirk_table[] = {
93 {
94 .callback = sof_rt5682_quirk_cb,
95 .matches = {
96 DMI_MATCH(DMI_SYS_VENDOR, "Circuitco"),
97 DMI_MATCH(DMI_PRODUCT_NAME, "Minnowboard Max"),
98 },
99 .driver_data = (void *)(SOF_RT5682_SSP_CODEC(2)),
100 },
101 {
102 .callback = sof_rt5682_quirk_cb,
103 .matches = {
104 DMI_MATCH(DMI_SYS_VENDOR, "AAEON"),
105 DMI_MATCH(DMI_PRODUCT_NAME, "UP-CHT01"),
106 },
107 .driver_data = (void *)(SOF_RT5682_SSP_CODEC(2)),
108 },
109 {
110 .callback = sof_rt5682_quirk_cb,
111 .matches = {
112 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
113 DMI_MATCH(DMI_PRODUCT_NAME, "WhiskeyLake Client"),
114 },
115 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
116 SOF_RT5682_MCLK_24MHZ |
117 SOF_RT5682_SSP_CODEC(1)),
118 },
119 {
120 /*
121 * Dooly is hatch family but using rt1015 amp so it
122 * requires a quirk before "Google_Hatch".
123 */
124 .callback = sof_rt5682_quirk_cb,
125 .matches = {
126 DMI_MATCH(DMI_SYS_VENDOR, "HP"),
127 DMI_MATCH(DMI_PRODUCT_NAME, "Dooly"),
128 },
129 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
130 SOF_RT5682_MCLK_24MHZ |
131 SOF_RT5682_SSP_CODEC(0) |
132 SOF_SPEAKER_AMP_PRESENT |
133 SOF_RT1015_SPEAKER_AMP_PRESENT |
134 SOF_RT5682_SSP_AMP(1)),
135 },
136 {
137 .callback = sof_rt5682_quirk_cb,
138 .matches = {
139 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Hatch"),
140 },
141 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
142 SOF_RT5682_MCLK_24MHZ |
143 SOF_RT5682_SSP_CODEC(0) |
144 SOF_SPEAKER_AMP_PRESENT |
145 SOF_RT5682_SSP_AMP(1)),
146 },
147 {
148 .callback = sof_rt5682_quirk_cb,
149 .matches = {
150 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
151 DMI_MATCH(DMI_PRODUCT_NAME, "Ice Lake Client"),
152 },
153 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
154 SOF_RT5682_SSP_CODEC(0)),
155 },
156 {
157 .callback = sof_rt5682_quirk_cb,
158 .matches = {
159 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Volteer"),
160 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98373_ALC5682I_I2S_UP4"),
161 },
162 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
163 SOF_RT5682_SSP_CODEC(0) |
164 SOF_SPEAKER_AMP_PRESENT |
165 SOF_MAX98373_SPEAKER_AMP_PRESENT |
166 SOF_RT5682_SSP_AMP(2) |
167 SOF_RT5682_NUM_HDMIDEV(4)),
168 },
169 {
170 .callback = sof_rt5682_quirk_cb,
171 .matches = {
172 DMI_MATCH(DMI_SYS_VENDOR, "Intel Corporation"),
173 DMI_MATCH(DMI_PRODUCT_NAME, "Alder Lake Client Platform"),
174 DMI_MATCH(DMI_OEM_STRING, "AUDIO-ADL_MAX98373_ALC5682I_I2S"),
175 },
176 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
177 SOF_RT5682_SSP_CODEC(0) |
178 SOF_SPEAKER_AMP_PRESENT |
179 SOF_MAX98373_SPEAKER_AMP_PRESENT |
180 SOF_RT5682_SSP_AMP(2) |
181 SOF_RT5682_NUM_HDMIDEV(4)),
182 },
183 {
184 .callback = sof_rt5682_quirk_cb,
185 .matches = {
186 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
187 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98390_ALC5682I_I2S"),
188 },
189 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
190 SOF_RT5682_SSP_CODEC(0) |
191 SOF_SPEAKER_AMP_PRESENT |
192 SOF_MAX98390_SPEAKER_AMP_PRESENT |
193 SOF_RT5682_SSP_AMP(2) |
194 SOF_RT5682_NUM_HDMIDEV(4)),
195 },
196 {
197 .callback = sof_rt5682_quirk_cb,
198 .matches = {
199 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
200 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98390_ALC5682I_I2S_4SPK"),
201 },
202 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
203 SOF_RT5682_SSP_CODEC(0) |
204 SOF_SPEAKER_AMP_PRESENT |
205 SOF_MAX98390_SPEAKER_AMP_PRESENT |
206 SOF_MAX98390_TWEETER_SPEAKER_PRESENT |
207 SOF_RT5682_SSP_AMP(1) |
208 SOF_RT5682_NUM_HDMIDEV(4) |
209 SOF_BT_OFFLOAD_SSP(2) |
210 SOF_SSP_BT_OFFLOAD_PRESENT),
211
212 },
213 {
214 .callback = sof_rt5682_quirk_cb,
215 .matches = {
216 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Brya"),
217 DMI_MATCH(DMI_OEM_STRING, "AUDIO-MAX98360_ALC5682I_I2S_AMP_SSP2"),
218 },
219 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
220 SOF_RT5682_SSP_CODEC(0) |
221 SOF_SPEAKER_AMP_PRESENT |
222 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
223 SOF_RT5682_SSP_AMP(2) |
224 SOF_RT5682_NUM_HDMIDEV(4)),
225 },
226 {
227 .callback = sof_rt5682_quirk_cb,
228 .matches = {
229 DMI_MATCH(DMI_PRODUCT_FAMILY, "Google_Rex"),
230 },
231 .driver_data = (void *)(SOF_RT5682_MCLK_EN |
232 SOF_RT5682_SSP_CODEC(2) |
233 SOF_SPEAKER_AMP_PRESENT |
234 SOF_RT5682_SSP_AMP(0) |
235 SOF_RT5682_NUM_HDMIDEV(4)
236 ),
237 },
238 {}
239};
240
241static int sof_hdmi_init(struct snd_soc_pcm_runtime *rtd)
242{
243 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
244 struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
245 struct sof_hdmi_pcm *pcm;
246
247 pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
248 if (!pcm)
249 return -ENOMEM;
250
251 /* dai_link id is 1:1 mapped to the PCM device */
252 pcm->device = rtd->dai_link->id;
253 pcm->codec_dai = dai;
254
255 list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
256
257 return 0;
258}
259
260static struct snd_soc_jack_pin jack_pins[] = {
261 {
262 .pin = "Headphone Jack",
263 .mask = SND_JACK_HEADPHONE,
264 },
265 {
266 .pin = "Headset Mic",
267 .mask = SND_JACK_MICROPHONE,
268 },
269};
270
271static int sof_rt5682_codec_init(struct snd_soc_pcm_runtime *rtd)
272{
273 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
274 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
275 struct snd_soc_jack *jack;
276 int ret;
277
278 /* need to enable ASRC function for 24MHz mclk rate */
279 if ((sof_rt5682_quirk & SOF_RT5682_MCLK_EN) &&
280 (sof_rt5682_quirk & SOF_RT5682_MCLK_24MHZ)) {
281 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT)
282 rt5682s_sel_asrc_clk_src(component,
283 RT5682S_DA_STEREO1_FILTER |
284 RT5682S_AD_STEREO1_FILTER,
285 RT5682S_CLK_SEL_I2S1_ASRC);
286 else
287 rt5682_sel_asrc_clk_src(component,
288 RT5682_DA_STEREO1_FILTER |
289 RT5682_AD_STEREO1_FILTER,
290 RT5682_CLK_SEL_I2S1_ASRC);
291 }
292
293 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) {
294 /*
295 * The firmware might enable the clock at
296 * boot (this information may or may not
297 * be reflected in the enable clock register).
298 * To change the rate we must disable the clock
299 * first to cover these cases. Due to common
300 * clock framework restrictions that do not allow
301 * to disable a clock that has not been enabled,
302 * we need to enable the clock first.
303 */
304 ret = clk_prepare_enable(ctx->mclk);
305 if (!ret)
306 clk_disable_unprepare(ctx->mclk);
307
308 ret = clk_set_rate(ctx->mclk, 19200000);
309
310 if (ret)
311 dev_err(rtd->dev, "unable to set MCLK rate\n");
312 }
313
314 /*
315 * Headset buttons map to the google Reference headset.
316 * These can be configured by userspace.
317 */
318 ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
319 SND_JACK_HEADSET | SND_JACK_BTN_0 |
320 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
321 SND_JACK_BTN_3,
322 &ctx->sof_headset,
323 jack_pins,
324 ARRAY_SIZE(jack_pins));
325 if (ret) {
326 dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
327 return ret;
328 }
329
330 jack = &ctx->sof_headset;
331
332 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
333 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
334 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
335 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
336 ret = snd_soc_component_set_jack(component, jack, NULL);
337
338 if (ret) {
339 dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
340 return ret;
341 }
342
343 return ret;
344};
345
346static void sof_rt5682_codec_exit(struct snd_soc_pcm_runtime *rtd)
347{
348 struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
349
350 snd_soc_component_set_jack(component, NULL, NULL);
351}
352
353static int sof_rt5682_hw_params(struct snd_pcm_substream *substream,
354 struct snd_pcm_hw_params *params)
355{
356 struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
357 struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
358 struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
359 int pll_id, pll_source, pll_in, pll_out, clk_id, ret;
360
361 if (sof_rt5682_quirk & SOF_RT5682_MCLK_EN) {
362 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) {
363 ret = clk_prepare_enable(ctx->mclk);
364 if (ret < 0) {
365 dev_err(rtd->dev,
366 "could not configure MCLK state");
367 return ret;
368 }
369 }
370
371 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT)
372 pll_source = RT5682S_PLL_S_MCLK;
373 else
374 pll_source = RT5682_PLL1_S_MCLK;
375
376 /* get the tplg configured mclk. */
377 pll_in = sof_dai_get_mclk(rtd);
378
379 /* mclk from the quirk is the first choice */
380 if (sof_rt5682_quirk & SOF_RT5682_MCLK_24MHZ) {
381 if (pll_in != 24000000)
382 dev_warn(rtd->dev, "configure wrong mclk in tplg, please use 24MHz.\n");
383 pll_in = 24000000;
384 } else if (pll_in == 0) {
385 /* use default mclk if not specified correct in topology */
386 pll_in = 19200000;
387 } else if (pll_in < 0) {
388 return pll_in;
389 }
390 } else {
391 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT)
392 pll_source = RT5682S_PLL_S_BCLK1;
393 else
394 pll_source = RT5682_PLL1_S_BCLK1;
395
396 pll_in = params_rate(params) * 50;
397 }
398
399 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT) {
400 pll_id = RT5682S_PLL2;
401 clk_id = RT5682S_SCLK_S_PLL2;
402 } else {
403 pll_id = RT5682_PLL1;
404 clk_id = RT5682_SCLK_S_PLL1;
405 }
406
407 pll_out = params_rate(params) * 512;
408
409 /* when MCLK is 512FS, no need to set PLL configuration additionally. */
410 if (pll_in == pll_out)
411 clk_id = RT5682S_SCLK_S_MCLK;
412 else {
413 /* Configure pll for codec */
414 ret = snd_soc_dai_set_pll(codec_dai, pll_id, pll_source, pll_in,
415 pll_out);
416 if (ret < 0)
417 dev_err(rtd->dev, "snd_soc_dai_set_pll err = %d\n", ret);
418 }
419
420 /* Configure sysclk for codec */
421 ret = snd_soc_dai_set_sysclk(codec_dai, clk_id,
422 pll_out, SND_SOC_CLOCK_IN);
423 if (ret < 0)
424 dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
425
426 /*
427 * slot_width should equal or large than data length, set them
428 * be the same
429 */
430 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x0, 0x0, 2,
431 params_width(params));
432 if (ret < 0) {
433 dev_err(rtd->dev, "set TDM slot err:%d\n", ret);
434 return ret;
435 }
436
437 return ret;
438}
439
440static struct snd_soc_ops sof_rt5682_ops = {
441 .hw_params = sof_rt5682_hw_params,
442};
443
444static struct snd_soc_dai_link_component platform_component[] = {
445 {
446 /* name might be overridden during probe */
447 .name = "0000:00:1f.3"
448 }
449};
450
451static int sof_card_late_probe(struct snd_soc_card *card)
452{
453 struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
454 struct snd_soc_component *component = NULL;
455 struct snd_soc_dapm_context *dapm = &card->dapm;
456 char jack_name[NAME_SIZE];
457 struct sof_hdmi_pcm *pcm;
458 int err;
459
460 if (sof_rt5682_quirk & SOF_MAX98373_SPEAKER_AMP_PRESENT) {
461 /* Disable Left and Right Spk pin after boot */
462 snd_soc_dapm_disable_pin(dapm, "Left Spk");
463 snd_soc_dapm_disable_pin(dapm, "Right Spk");
464 err = snd_soc_dapm_sync(dapm);
465 if (err < 0)
466 return err;
467 }
468
469 /* HDMI is not supported by SOF on Baytrail/CherryTrail */
470 if (is_legacy_cpu || !ctx->idisp_codec)
471 return 0;
472
473 if (list_empty(&ctx->hdmi_pcm_list))
474 return -EINVAL;
475
476 if (ctx->common_hdmi_codec_drv) {
477 pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm,
478 head);
479 component = pcm->codec_dai->component;
480 return hda_dsp_hdmi_build_controls(card, component);
481 }
482
483 list_for_each_entry(pcm, &ctx->hdmi_pcm_list, head) {
484 component = pcm->codec_dai->component;
485 snprintf(jack_name, sizeof(jack_name),
486 "HDMI/DP, pcm=%d Jack", pcm->device);
487 err = snd_soc_card_jack_new(card, jack_name,
488 SND_JACK_AVOUT, &pcm->hdmi_jack);
489
490 if (err)
491 return err;
492
493 err = hdac_hdmi_jack_init(pcm->codec_dai, pcm->device,
494 &pcm->hdmi_jack);
495 if (err < 0)
496 return err;
497 }
498
499 return hdac_hdmi_jack_port_init(component, &card->dapm);
500}
501
502static const struct snd_kcontrol_new sof_controls[] = {
503 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
504 SOC_DAPM_PIN_SWITCH("Headset Mic"),
505 SOC_DAPM_PIN_SWITCH("Left Spk"),
506 SOC_DAPM_PIN_SWITCH("Right Spk"),
507
508};
509
510static const struct snd_soc_dapm_widget sof_widgets[] = {
511 SND_SOC_DAPM_HP("Headphone Jack", NULL),
512 SND_SOC_DAPM_MIC("Headset Mic", NULL),
513 SND_SOC_DAPM_SPK("Left Spk", NULL),
514 SND_SOC_DAPM_SPK("Right Spk", NULL),
515};
516
517static const struct snd_soc_dapm_widget dmic_widgets[] = {
518 SND_SOC_DAPM_MIC("SoC DMIC", NULL),
519};
520
521static const struct snd_soc_dapm_route sof_map[] = {
522 /* HP jack connectors - unknown if we have jack detection */
523 { "Headphone Jack", NULL, "HPOL" },
524 { "Headphone Jack", NULL, "HPOR" },
525
526 /* other jacks */
527 { "IN1P", NULL, "Headset Mic" },
528};
529
530static const struct snd_soc_dapm_route dmic_map[] = {
531 /* digital mics */
532 {"DMic", NULL, "SoC DMIC"},
533};
534
535static int dmic_init(struct snd_soc_pcm_runtime *rtd)
536{
537 struct snd_soc_card *card = rtd->card;
538 int ret;
539
540 ret = snd_soc_dapm_new_controls(&card->dapm, dmic_widgets,
541 ARRAY_SIZE(dmic_widgets));
542 if (ret) {
543 dev_err(card->dev, "DMic widget addition failed: %d\n", ret);
544 /* Don't need to add routes if widget addition failed */
545 return ret;
546 }
547
548 ret = snd_soc_dapm_add_routes(&card->dapm, dmic_map,
549 ARRAY_SIZE(dmic_map));
550
551 if (ret)
552 dev_err(card->dev, "DMic map addition failed: %d\n", ret);
553
554 return ret;
555}
556
557/* sof audio machine driver for rt5682 codec */
558static struct snd_soc_card sof_audio_card_rt5682 = {
559 .name = "rt5682", /* the sof- prefix is added by the core */
560 .owner = THIS_MODULE,
561 .controls = sof_controls,
562 .num_controls = ARRAY_SIZE(sof_controls),
563 .dapm_widgets = sof_widgets,
564 .num_dapm_widgets = ARRAY_SIZE(sof_widgets),
565 .dapm_routes = sof_map,
566 .num_dapm_routes = ARRAY_SIZE(sof_map),
567 .fully_routed = true,
568 .late_probe = sof_card_late_probe,
569};
570
571static struct snd_soc_dai_link_component rt5682_component[] = {
572 {
573 .name = "i2c-10EC5682:00",
574 .dai_name = "rt5682-aif1",
575 }
576};
577
578static struct snd_soc_dai_link_component rt5682s_component[] = {
579 {
580 .name = "i2c-RTL5682:00",
581 .dai_name = "rt5682s-aif1",
582 }
583};
584
585static struct snd_soc_dai_link_component dmic_component[] = {
586 {
587 .name = "dmic-codec",
588 .dai_name = "dmic-hifi",
589 }
590};
591
592static struct snd_soc_dai_link_component dummy_component[] = {
593 {
594 .name = "snd-soc-dummy",
595 .dai_name = "snd-soc-dummy-dai",
596 }
597};
598
599#define IDISP_CODEC_MASK 0x4
600
601static struct snd_soc_dai_link *sof_card_dai_links_create(struct device *dev,
602 int ssp_codec,
603 int ssp_amp,
604 int dmic_be_num,
605 int hdmi_num,
606 bool idisp_codec)
607{
608 struct snd_soc_dai_link_component *idisp_components;
609 struct snd_soc_dai_link_component *cpus;
610 struct snd_soc_dai_link *links;
611 int i, id = 0;
612
613 links = devm_kcalloc(dev, sof_audio_card_rt5682.num_links,
614 sizeof(struct snd_soc_dai_link), GFP_KERNEL);
615 cpus = devm_kcalloc(dev, sof_audio_card_rt5682.num_links,
616 sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);
617 if (!links || !cpus)
618 goto devm_err;
619
620 /* codec SSP */
621 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
622 "SSP%d-Codec", ssp_codec);
623 if (!links[id].name)
624 goto devm_err;
625
626 links[id].id = id;
627 if (sof_rt5682_quirk & SOF_RT5682S_HEADPHONE_CODEC_PRESENT) {
628 links[id].codecs = rt5682s_component;
629 links[id].num_codecs = ARRAY_SIZE(rt5682s_component);
630 } else {
631 links[id].codecs = rt5682_component;
632 links[id].num_codecs = ARRAY_SIZE(rt5682_component);
633 }
634 links[id].platforms = platform_component;
635 links[id].num_platforms = ARRAY_SIZE(platform_component);
636 links[id].init = sof_rt5682_codec_init;
637 links[id].exit = sof_rt5682_codec_exit;
638 links[id].ops = &sof_rt5682_ops;
639 links[id].dpcm_playback = 1;
640 links[id].dpcm_capture = 1;
641 links[id].no_pcm = 1;
642 links[id].cpus = &cpus[id];
643 links[id].num_cpus = 1;
644 if (is_legacy_cpu) {
645 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
646 "ssp%d-port",
647 ssp_codec);
648 if (!links[id].cpus->dai_name)
649 goto devm_err;
650 } else {
651 /*
652 * Currently, On SKL+ platforms MCLK will be turned off in sof
653 * runtime suspended, and it will go into runtime suspended
654 * right after playback is stop. However, rt5682 will output
655 * static noise if sysclk turns off during playback. Set
656 * ignore_pmdown_time to power down rt5682 immediately and
657 * avoid the noise.
658 * It can be removed once we can control MCLK by driver.
659 */
660 links[id].ignore_pmdown_time = 1;
661 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
662 "SSP%d Pin",
663 ssp_codec);
664 if (!links[id].cpus->dai_name)
665 goto devm_err;
666 }
667 id++;
668
669 /* dmic */
670 if (dmic_be_num > 0) {
671 /* at least we have dmic01 */
672 links[id].name = "dmic01";
673 links[id].cpus = &cpus[id];
674 links[id].cpus->dai_name = "DMIC01 Pin";
675 links[id].init = dmic_init;
676 if (dmic_be_num > 1) {
677 /* set up 2 BE links at most */
678 links[id + 1].name = "dmic16k";
679 links[id + 1].cpus = &cpus[id + 1];
680 links[id + 1].cpus->dai_name = "DMIC16k Pin";
681 dmic_be_num = 2;
682 }
683 }
684
685 for (i = 0; i < dmic_be_num; i++) {
686 links[id].id = id;
687 links[id].num_cpus = 1;
688 links[id].codecs = dmic_component;
689 links[id].num_codecs = ARRAY_SIZE(dmic_component);
690 links[id].platforms = platform_component;
691 links[id].num_platforms = ARRAY_SIZE(platform_component);
692 links[id].ignore_suspend = 1;
693 links[id].dpcm_capture = 1;
694 links[id].no_pcm = 1;
695 id++;
696 }
697
698 /* HDMI */
699 if (hdmi_num > 0) {
700 idisp_components = devm_kcalloc(dev,
701 hdmi_num,
702 sizeof(struct snd_soc_dai_link_component),
703 GFP_KERNEL);
704 if (!idisp_components)
705 goto devm_err;
706 }
707 for (i = 1; i <= hdmi_num; i++) {
708 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
709 "iDisp%d", i);
710 if (!links[id].name)
711 goto devm_err;
712
713 links[id].id = id;
714 links[id].cpus = &cpus[id];
715 links[id].num_cpus = 1;
716 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
717 "iDisp%d Pin", i);
718 if (!links[id].cpus->dai_name)
719 goto devm_err;
720
721 if (idisp_codec) {
722 idisp_components[i - 1].name = "ehdaudio0D2";
723 idisp_components[i - 1].dai_name = devm_kasprintf(dev,
724 GFP_KERNEL,
725 "intel-hdmi-hifi%d",
726 i);
727 if (!idisp_components[i - 1].dai_name)
728 goto devm_err;
729 } else {
730 idisp_components[i - 1].name = "snd-soc-dummy";
731 idisp_components[i - 1].dai_name = "snd-soc-dummy-dai";
732 }
733
734 links[id].codecs = &idisp_components[i - 1];
735 links[id].num_codecs = 1;
736 links[id].platforms = platform_component;
737 links[id].num_platforms = ARRAY_SIZE(platform_component);
738 links[id].init = sof_hdmi_init;
739 links[id].dpcm_playback = 1;
740 links[id].no_pcm = 1;
741 id++;
742 }
743
744 /* speaker amp */
745 if (sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT) {
746 links[id].name = devm_kasprintf(dev, GFP_KERNEL,
747 "SSP%d-Codec", ssp_amp);
748 if (!links[id].name)
749 goto devm_err;
750
751 links[id].id = id;
752 if (sof_rt5682_quirk & SOF_RT1015_SPEAKER_AMP_PRESENT) {
753 sof_rt1015_dai_link(&links[id]);
754 } else if (sof_rt5682_quirk & SOF_RT1015P_SPEAKER_AMP_PRESENT) {
755 sof_rt1015p_dai_link(&links[id]);
756 } else if (sof_rt5682_quirk & SOF_RT1019_SPEAKER_AMP_PRESENT) {
757 sof_rt1019p_dai_link(&links[id]);
758 } else if (sof_rt5682_quirk &
759 SOF_MAX98373_SPEAKER_AMP_PRESENT) {
760 links[id].codecs = max_98373_components;
761 links[id].num_codecs = ARRAY_SIZE(max_98373_components);
762 links[id].init = max_98373_spk_codec_init;
763 links[id].ops = &max_98373_ops;
764 } else if (sof_rt5682_quirk &
765 SOF_MAX98360A_SPEAKER_AMP_PRESENT) {
766 max_98360a_dai_link(&links[id]);
767 } else if (sof_rt5682_quirk &
768 SOF_RT1011_SPEAKER_AMP_PRESENT) {
769 sof_rt1011_dai_link(&links[id]);
770 } else if (sof_rt5682_quirk &
771 SOF_MAX98390_SPEAKER_AMP_PRESENT) {
772 if (sof_rt5682_quirk &
773 SOF_MAX98390_TWEETER_SPEAKER_PRESENT) {
774 links[id].codecs = max_98390_4spk_components;
775 links[id].num_codecs = ARRAY_SIZE(max_98390_4spk_components);
776 } else {
777 links[id].codecs = max_98390_components;
778 links[id].num_codecs = ARRAY_SIZE(max_98390_components);
779 }
780 links[id].init = max_98390_spk_codec_init;
781 links[id].ops = &max_98390_ops;
782 links[id].dpcm_capture = 1;
783
784 } else {
785 max_98357a_dai_link(&links[id]);
786 }
787 links[id].platforms = platform_component;
788 links[id].num_platforms = ARRAY_SIZE(platform_component);
789 links[id].dpcm_playback = 1;
790 /* feedback stream or firmware-generated echo reference */
791 links[id].dpcm_capture = 1;
792
793 links[id].no_pcm = 1;
794 links[id].cpus = &cpus[id];
795 links[id].num_cpus = 1;
796 if (is_legacy_cpu) {
797 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
798 "ssp%d-port",
799 ssp_amp);
800 if (!links[id].cpus->dai_name)
801 goto devm_err;
802
803 } else {
804 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
805 "SSP%d Pin",
806 ssp_amp);
807 if (!links[id].cpus->dai_name)
808 goto devm_err;
809 }
810 id++;
811 }
812
813 /* BT audio offload */
814 if (sof_rt5682_quirk & SOF_SSP_BT_OFFLOAD_PRESENT) {
815 int port = (sof_rt5682_quirk & SOF_BT_OFFLOAD_SSP_MASK) >>
816 SOF_BT_OFFLOAD_SSP_SHIFT;
817
818 links[id].id = id;
819 links[id].cpus = &cpus[id];
820 links[id].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
821 "SSP%d Pin", port);
822 if (!links[id].cpus->dai_name)
823 goto devm_err;
824 links[id].name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", port);
825 if (!links[id].name)
826 goto devm_err;
827 links[id].codecs = dummy_component;
828 links[id].num_codecs = ARRAY_SIZE(dummy_component);
829 links[id].platforms = platform_component;
830 links[id].num_platforms = ARRAY_SIZE(platform_component);
831 links[id].dpcm_playback = 1;
832 links[id].dpcm_capture = 1;
833 links[id].no_pcm = 1;
834 links[id].num_cpus = 1;
835 }
836
837 return links;
838devm_err:
839 return NULL;
840}
841
842static int sof_audio_probe(struct platform_device *pdev)
843{
844 struct snd_soc_dai_link *dai_links;
845 struct snd_soc_acpi_mach *mach;
846 struct sof_card_private *ctx;
847 int dmic_be_num, hdmi_num;
848 int ret, ssp_amp, ssp_codec;
849
850 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
851 if (!ctx)
852 return -ENOMEM;
853
854 if (pdev->id_entry && pdev->id_entry->driver_data)
855 sof_rt5682_quirk = (unsigned long)pdev->id_entry->driver_data;
856
857 dmi_check_system(sof_rt5682_quirk_table);
858
859 mach = pdev->dev.platform_data;
860
861 /* A speaker amp might not be present when the quirk claims one is.
862 * Detect this via whether the machine driver match includes quirk_data.
863 */
864 if ((sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT) && !mach->quirk_data)
865 sof_rt5682_quirk &= ~SOF_SPEAKER_AMP_PRESENT;
866
867 /* Detect the headset codec variant */
868 if (acpi_dev_present("RTL5682", NULL, -1))
869 sof_rt5682_quirk |= SOF_RT5682S_HEADPHONE_CODEC_PRESENT;
870
871 if (soc_intel_is_byt() || soc_intel_is_cht()) {
872 is_legacy_cpu = 1;
873 dmic_be_num = 0;
874 hdmi_num = 0;
875 /* default quirk for legacy cpu */
876 sof_rt5682_quirk = SOF_RT5682_MCLK_EN |
877 SOF_RT5682_MCLK_BYTCHT_EN |
878 SOF_RT5682_SSP_CODEC(2);
879 } else {
880 dmic_be_num = 2;
881 hdmi_num = (sof_rt5682_quirk & SOF_RT5682_NUM_HDMIDEV_MASK) >>
882 SOF_RT5682_NUM_HDMIDEV_SHIFT;
883 /* default number of HDMI DAI's */
884 if (!hdmi_num)
885 hdmi_num = 3;
886
887 if (mach->mach_params.codec_mask & IDISP_CODEC_MASK)
888 ctx->idisp_codec = true;
889 }
890
891 /* need to get main clock from pmc */
892 if (sof_rt5682_quirk & SOF_RT5682_MCLK_BYTCHT_EN) {
893 ctx->mclk = devm_clk_get(&pdev->dev, "pmc_plt_clk_3");
894 if (IS_ERR(ctx->mclk)) {
895 ret = PTR_ERR(ctx->mclk);
896
897 dev_err(&pdev->dev,
898 "Failed to get MCLK from pmc_plt_clk_3: %d\n",
899 ret);
900 return ret;
901 }
902
903 ret = clk_prepare_enable(ctx->mclk);
904 if (ret < 0) {
905 dev_err(&pdev->dev,
906 "could not configure MCLK state");
907 return ret;
908 }
909 }
910
911 dev_dbg(&pdev->dev, "sof_rt5682_quirk = %lx\n", sof_rt5682_quirk);
912
913 ssp_amp = (sof_rt5682_quirk & SOF_RT5682_SSP_AMP_MASK) >>
914 SOF_RT5682_SSP_AMP_SHIFT;
915
916 ssp_codec = sof_rt5682_quirk & SOF_RT5682_SSP_CODEC_MASK;
917
918 /* compute number of dai links */
919 sof_audio_card_rt5682.num_links = 1 + dmic_be_num + hdmi_num;
920
921 if (sof_rt5682_quirk & SOF_SPEAKER_AMP_PRESENT)
922 sof_audio_card_rt5682.num_links++;
923
924 if (sof_rt5682_quirk & SOF_MAX98373_SPEAKER_AMP_PRESENT)
925 max_98373_set_codec_conf(&sof_audio_card_rt5682);
926 else if (sof_rt5682_quirk & SOF_RT1011_SPEAKER_AMP_PRESENT)
927 sof_rt1011_codec_conf(&sof_audio_card_rt5682);
928 else if (sof_rt5682_quirk & SOF_RT1015P_SPEAKER_AMP_PRESENT)
929 sof_rt1015p_codec_conf(&sof_audio_card_rt5682);
930 else if (sof_rt5682_quirk & SOF_MAX98390_SPEAKER_AMP_PRESENT) {
931 if (sof_rt5682_quirk & SOF_MAX98390_TWEETER_SPEAKER_PRESENT)
932 max_98390_set_codec_conf(&sof_audio_card_rt5682,
933 ARRAY_SIZE(max_98390_4spk_components));
934 else
935 max_98390_set_codec_conf(&sof_audio_card_rt5682,
936 ARRAY_SIZE(max_98390_components));
937 }
938
939 if (sof_rt5682_quirk & SOF_SSP_BT_OFFLOAD_PRESENT)
940 sof_audio_card_rt5682.num_links++;
941
942 dai_links = sof_card_dai_links_create(&pdev->dev, ssp_codec, ssp_amp,
943 dmic_be_num, hdmi_num, ctx->idisp_codec);
944 if (!dai_links)
945 return -ENOMEM;
946
947 sof_audio_card_rt5682.dai_link = dai_links;
948
949 if (sof_rt5682_quirk & SOF_RT1015_SPEAKER_AMP_PRESENT)
950 sof_rt1015_codec_conf(&sof_audio_card_rt5682);
951
952 INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
953
954 sof_audio_card_rt5682.dev = &pdev->dev;
955
956 /* set platform name for each dailink */
957 ret = snd_soc_fixup_dai_links_platform_name(&sof_audio_card_rt5682,
958 mach->mach_params.platform);
959 if (ret)
960 return ret;
961
962 ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
963
964 snd_soc_card_set_drvdata(&sof_audio_card_rt5682, ctx);
965
966 return devm_snd_soc_register_card(&pdev->dev,
967 &sof_audio_card_rt5682);
968}
969
970static const struct platform_device_id board_ids[] = {
971 {
972 .name = "sof_rt5682",
973 },
974 {
975 .name = "tgl_mx98357_rt5682",
976 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
977 SOF_RT5682_SSP_CODEC(0) |
978 SOF_SPEAKER_AMP_PRESENT |
979 SOF_RT5682_SSP_AMP(1) |
980 SOF_RT5682_NUM_HDMIDEV(4) |
981 SOF_BT_OFFLOAD_SSP(2) |
982 SOF_SSP_BT_OFFLOAD_PRESENT),
983 },
984 {
985 .name = "jsl_rt5682_rt1015",
986 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
987 SOF_RT5682_MCLK_24MHZ |
988 SOF_RT5682_SSP_CODEC(0) |
989 SOF_SPEAKER_AMP_PRESENT |
990 SOF_RT1015_SPEAKER_AMP_PRESENT |
991 SOF_RT5682_SSP_AMP(1)),
992 },
993 {
994 .name = "tgl_mx98373_rt5682",
995 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
996 SOF_RT5682_SSP_CODEC(0) |
997 SOF_SPEAKER_AMP_PRESENT |
998 SOF_MAX98373_SPEAKER_AMP_PRESENT |
999 SOF_RT5682_SSP_AMP(1) |
1000 SOF_RT5682_NUM_HDMIDEV(4) |
1001 SOF_BT_OFFLOAD_SSP(2) |
1002 SOF_SSP_BT_OFFLOAD_PRESENT),
1003 },
1004 {
1005 .name = "jsl_rt5682_mx98360",
1006 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1007 SOF_RT5682_MCLK_24MHZ |
1008 SOF_RT5682_SSP_CODEC(0) |
1009 SOF_SPEAKER_AMP_PRESENT |
1010 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
1011 SOF_RT5682_SSP_AMP(1)),
1012 },
1013 {
1014 .name = "cml_rt1015_rt5682",
1015 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1016 SOF_RT5682_MCLK_24MHZ |
1017 SOF_RT5682_SSP_CODEC(0) |
1018 SOF_SPEAKER_AMP_PRESENT |
1019 SOF_RT1015_SPEAKER_AMP_PRESENT |
1020 SOF_RT5682_SSP_AMP(1)),
1021 },
1022 {
1023 .name = "tgl_rt1011_rt5682",
1024 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1025 SOF_RT5682_SSP_CODEC(0) |
1026 SOF_SPEAKER_AMP_PRESENT |
1027 SOF_RT1011_SPEAKER_AMP_PRESENT |
1028 SOF_RT5682_SSP_AMP(1) |
1029 SOF_RT5682_NUM_HDMIDEV(4) |
1030 SOF_BT_OFFLOAD_SSP(2) |
1031 SOF_SSP_BT_OFFLOAD_PRESENT),
1032 },
1033 {
1034 .name = "jsl_rt5682_rt1015p",
1035 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1036 SOF_RT5682_MCLK_24MHZ |
1037 SOF_RT5682_SSP_CODEC(0) |
1038 SOF_SPEAKER_AMP_PRESENT |
1039 SOF_RT1015P_SPEAKER_AMP_PRESENT |
1040 SOF_RT5682_SSP_AMP(1)),
1041 },
1042 {
1043 .name = "adl_mx98373_rt5682",
1044 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1045 SOF_RT5682_SSP_CODEC(0) |
1046 SOF_SPEAKER_AMP_PRESENT |
1047 SOF_MAX98373_SPEAKER_AMP_PRESENT |
1048 SOF_RT5682_SSP_AMP(1) |
1049 SOF_RT5682_NUM_HDMIDEV(4) |
1050 SOF_BT_OFFLOAD_SSP(2) |
1051 SOF_SSP_BT_OFFLOAD_PRESENT),
1052 },
1053 {
1054 .name = "adl_mx98357_rt5682",
1055 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1056 SOF_RT5682_SSP_CODEC(0) |
1057 SOF_SPEAKER_AMP_PRESENT |
1058 SOF_RT5682_SSP_AMP(2) |
1059 SOF_RT5682_NUM_HDMIDEV(4)),
1060 },
1061 {
1062 .name = "adl_max98390_rt5682",
1063 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1064 SOF_RT5682_SSP_CODEC(0) |
1065 SOF_SPEAKER_AMP_PRESENT |
1066 SOF_MAX98390_SPEAKER_AMP_PRESENT |
1067 SOF_RT5682_SSP_AMP(1) |
1068 SOF_RT5682_NUM_HDMIDEV(4) |
1069 SOF_BT_OFFLOAD_SSP(2) |
1070 SOF_SSP_BT_OFFLOAD_PRESENT),
1071 },
1072 {
1073 .name = "adl_mx98360_rt5682",
1074 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1075 SOF_RT5682_SSP_CODEC(0) |
1076 SOF_SPEAKER_AMP_PRESENT |
1077 SOF_MAX98360A_SPEAKER_AMP_PRESENT |
1078 SOF_RT5682_SSP_AMP(1) |
1079 SOF_RT5682_NUM_HDMIDEV(4) |
1080 SOF_BT_OFFLOAD_SSP(2) |
1081 SOF_SSP_BT_OFFLOAD_PRESENT),
1082 },
1083 {
1084 .name = "adl_rt5682",
1085 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1086 SOF_RT5682_SSP_CODEC(0) |
1087 SOF_RT5682_NUM_HDMIDEV(4) |
1088 SOF_BT_OFFLOAD_SSP(2) |
1089 SOF_SSP_BT_OFFLOAD_PRESENT),
1090 },
1091 {
1092 .name = "adl_rt1019_rt5682",
1093 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1094 SOF_RT5682_SSP_CODEC(0) |
1095 SOF_SPEAKER_AMP_PRESENT |
1096 SOF_RT1019_SPEAKER_AMP_PRESENT |
1097 SOF_RT5682_SSP_AMP(1) |
1098 SOF_RT5682_NUM_HDMIDEV(4)),
1099 },
1100 {
1101 .name = "mtl_mx98357_rt5682",
1102 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1103 SOF_RT5682_SSP_CODEC(0) |
1104 SOF_SPEAKER_AMP_PRESENT |
1105 SOF_RT5682_SSP_AMP(1) |
1106 SOF_RT5682_NUM_HDMIDEV(4)),
1107 },
1108 {
1109 .name = "jsl_rt5682",
1110 .driver_data = (kernel_ulong_t)(SOF_RT5682_MCLK_EN |
1111 SOF_RT5682_MCLK_24MHZ |
1112 SOF_RT5682_SSP_CODEC(0)),
1113 },
1114 { }
1115};
1116MODULE_DEVICE_TABLE(platform, board_ids);
1117
1118static struct platform_driver sof_audio = {
1119 .probe = sof_audio_probe,
1120 .driver = {
1121 .name = "sof_rt5682",
1122 .pm = &snd_soc_pm_ops,
1123 },
1124 .id_table = board_ids,
1125};
1126module_platform_driver(sof_audio)
1127
1128/* Module information */
1129MODULE_DESCRIPTION("SOF Audio Machine driver");
1130MODULE_AUTHOR("Bard Liao <bard.liao@intel.com>");
1131MODULE_AUTHOR("Sathya Prakash M R <sathya.prakash.m.r@intel.com>");
1132MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>");
1133MODULE_AUTHOR("Mac Chiang <mac.chiang@intel.com>");
1134MODULE_LICENSE("GPL v2");
1135MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
1136MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);
1137MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_REALTEK_COMMON);