1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright(c) 2018 Intel Corporation.
5 * Intel Geminilake I2S Machine Driver with MAX98357A & RT5682 Codecs
8 * Intel Apollolake I2S Machine driver
11 #include <linux/input.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.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/soc-acpi.h>
20 #include "../../codecs/rt5682.h"
21 #include "../../codecs/hdac_hdmi.h"
22 #include "hda_dsp_common.h"
24 /* The platform clock outputs 19.2Mhz clock to codec as I2S MCLK */
25 #define GLK_PLAT_CLK_FREQ 19200000
26 #define RT5682_PLL_FREQ (48000 * 512)
27 #define GLK_REALTEK_CODEC_DAI "rt5682-aif1"
28 #define GLK_MAXIM_CODEC_DAI "HiFi"
29 #define MAXIM_DEV0_NAME "MX98357A:00"
30 #define DUAL_CHANNEL 2
31 #define QUAD_CHANNEL 4
34 static struct snd_soc_jack geminilake_hdmi
[3];
37 struct list_head head
;
38 struct snd_soc_dai
*codec_dai
;
42 struct glk_card_private
{
43 struct snd_soc_jack geminilake_headset
;
44 struct list_head hdmi_pcm_list
;
45 bool common_hdmi_codec_drv
;
49 GLK_DPCM_AUDIO_PB
= 0,
52 GLK_DPCM_AUDIO_ECHO_REF_CP
,
53 GLK_DPCM_AUDIO_REF_CP
,
54 GLK_DPCM_AUDIO_DMIC_CP
,
55 GLK_DPCM_AUDIO_HDMI1_PB
,
56 GLK_DPCM_AUDIO_HDMI2_PB
,
57 GLK_DPCM_AUDIO_HDMI3_PB
,
60 static const struct snd_kcontrol_new geminilake_controls
[] = {
61 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
62 SOC_DAPM_PIN_SWITCH("Headset Mic"),
63 SOC_DAPM_PIN_SWITCH("Spk"),
66 static const struct snd_soc_dapm_widget geminilake_widgets
[] = {
67 SND_SOC_DAPM_HP("Headphone Jack", NULL
),
68 SND_SOC_DAPM_MIC("Headset Mic", NULL
),
69 SND_SOC_DAPM_SPK("Spk", NULL
),
70 SND_SOC_DAPM_MIC("SoC DMIC", NULL
),
71 SND_SOC_DAPM_SPK("HDMI1", NULL
),
72 SND_SOC_DAPM_SPK("HDMI2", NULL
),
73 SND_SOC_DAPM_SPK("HDMI3", NULL
),
76 static const struct snd_soc_dapm_route geminilake_map
[] = {
77 /* HP jack connectors - unknown if we have jack detection */
78 { "Headphone Jack", NULL
, "HPOL" },
79 { "Headphone Jack", NULL
, "HPOR" },
82 { "Spk", NULL
, "Speaker" },
85 { "IN1P", NULL
, "Headset Mic" },
88 { "DMic", NULL
, "SoC DMIC" },
90 /* CODEC BE connections */
91 { "HiFi Playback", NULL
, "ssp1 Tx" },
92 { "ssp1 Tx", NULL
, "codec0_out" },
94 { "AIF1 Playback", NULL
, "ssp2 Tx" },
95 { "ssp2 Tx", NULL
, "codec1_out" },
97 { "codec0_in", NULL
, "ssp2 Rx" },
98 { "ssp2 Rx", NULL
, "AIF1 Capture" },
100 { "HDMI1", NULL
, "hif5-0 Output" },
101 { "HDMI2", NULL
, "hif6-0 Output" },
102 { "HDMI2", NULL
, "hif7-0 Output" },
104 { "hifi3", NULL
, "iDisp3 Tx" },
105 { "iDisp3 Tx", NULL
, "iDisp3_out" },
106 { "hifi2", NULL
, "iDisp2 Tx" },
107 { "iDisp2 Tx", NULL
, "iDisp2_out" },
108 { "hifi1", NULL
, "iDisp1 Tx" },
109 { "iDisp1 Tx", NULL
, "iDisp1_out" },
112 { "dmic01_hifi", NULL
, "DMIC01 Rx" },
113 { "DMIC01 Rx", NULL
, "DMIC AIF" },
116 static int geminilake_ssp_fixup(struct snd_soc_pcm_runtime
*rtd
,
117 struct snd_pcm_hw_params
*params
)
119 struct snd_interval
*rate
= hw_param_interval(params
,
120 SNDRV_PCM_HW_PARAM_RATE
);
121 struct snd_interval
*chan
= hw_param_interval(params
,
122 SNDRV_PCM_HW_PARAM_CHANNELS
);
123 struct snd_mask
*fmt
= hw_param_mask(params
, SNDRV_PCM_HW_PARAM_FORMAT
);
125 /* The ADSP will convert the FE rate to 48k, stereo */
126 rate
->min
= rate
->max
= 48000;
127 chan
->min
= chan
->max
= DUAL_CHANNEL
;
129 /* set SSP to 24 bit */
131 snd_mask_set_format(fmt
, SNDRV_PCM_FORMAT_S24_LE
);
136 static int geminilake_rt5682_codec_init(struct snd_soc_pcm_runtime
*rtd
)
138 struct glk_card_private
*ctx
= snd_soc_card_get_drvdata(rtd
->card
);
139 struct snd_soc_component
*component
= rtd
->codec_dai
->component
;
140 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
141 struct snd_soc_jack
*jack
;
144 ret
= snd_soc_dai_set_pll(codec_dai
, 0, RT5682_PLL1_S_MCLK
,
145 GLK_PLAT_CLK_FREQ
, RT5682_PLL_FREQ
);
147 dev_err(rtd
->dev
, "can't set codec pll: %d\n", ret
);
151 /* Configure sysclk for codec */
152 ret
= snd_soc_dai_set_sysclk(codec_dai
, RT5682_SCLK_S_PLL1
,
153 RT5682_PLL_FREQ
, SND_SOC_CLOCK_IN
);
155 dev_err(rtd
->dev
, "snd_soc_dai_set_sysclk err = %d\n", ret
);
158 * Headset buttons map to the google Reference headset.
159 * These can be configured by userspace.
161 ret
= snd_soc_card_jack_new(rtd
->card
, "Headset Jack",
162 SND_JACK_HEADSET
| SND_JACK_BTN_0
| SND_JACK_BTN_1
|
163 SND_JACK_BTN_2
| SND_JACK_BTN_3
| SND_JACK_LINEOUT
,
164 &ctx
->geminilake_headset
, NULL
, 0);
166 dev_err(rtd
->dev
, "Headset Jack creation failed: %d\n", ret
);
170 jack
= &ctx
->geminilake_headset
;
172 snd_jack_set_key(jack
->jack
, SND_JACK_BTN_0
, KEY_PLAYPAUSE
);
173 snd_jack_set_key(jack
->jack
, SND_JACK_BTN_1
, KEY_VOICECOMMAND
);
174 snd_jack_set_key(jack
->jack
, SND_JACK_BTN_2
, KEY_VOLUMEUP
);
175 snd_jack_set_key(jack
->jack
, SND_JACK_BTN_3
, KEY_VOLUMEDOWN
);
177 ret
= snd_soc_component_set_jack(component
, jack
, NULL
);
180 dev_err(rtd
->dev
, "Headset Jack call-back failed: %d\n", ret
);
187 static int geminilake_rt5682_hw_params(struct snd_pcm_substream
*substream
,
188 struct snd_pcm_hw_params
*params
)
190 struct snd_soc_pcm_runtime
*rtd
= substream
->private_data
;
191 struct snd_soc_dai
*codec_dai
= rtd
->codec_dai
;
194 /* Set valid bitmask & configuration for I2S in 24 bit */
195 ret
= snd_soc_dai_set_tdm_slot(codec_dai
, 0x0, 0x0, 2, 24);
197 dev_err(rtd
->dev
, "set TDM slot err:%d\n", ret
);
204 static struct snd_soc_ops geminilake_rt5682_ops
= {
205 .hw_params
= geminilake_rt5682_hw_params
,
208 static int geminilake_hdmi_init(struct snd_soc_pcm_runtime
*rtd
)
210 struct glk_card_private
*ctx
= snd_soc_card_get_drvdata(rtd
->card
);
211 struct snd_soc_dai
*dai
= rtd
->codec_dai
;
212 struct glk_hdmi_pcm
*pcm
;
214 pcm
= devm_kzalloc(rtd
->card
->dev
, sizeof(*pcm
), GFP_KERNEL
);
218 pcm
->device
= GLK_DPCM_AUDIO_HDMI1_PB
+ dai
->id
;
219 pcm
->codec_dai
= dai
;
221 list_add_tail(&pcm
->head
, &ctx
->hdmi_pcm_list
);
226 static int geminilake_rt5682_fe_init(struct snd_soc_pcm_runtime
*rtd
)
228 struct snd_soc_component
*component
= rtd
->cpu_dai
->component
;
229 struct snd_soc_dapm_context
*dapm
;
232 dapm
= snd_soc_component_get_dapm(component
);
233 ret
= snd_soc_dapm_ignore_suspend(dapm
, "Reference Capture");
235 dev_err(rtd
->dev
, "Ref Cap ignore suspend failed %d\n", ret
);
242 static const unsigned int rates
[] = {
246 static const struct snd_pcm_hw_constraint_list constraints_rates
= {
247 .count
= ARRAY_SIZE(rates
),
252 static unsigned int channels_quad
[] = {
256 static struct snd_pcm_hw_constraint_list constraints_channels_quad
= {
257 .count
= ARRAY_SIZE(channels_quad
),
258 .list
= channels_quad
,
262 static int geminilake_dmic_fixup(struct snd_soc_pcm_runtime
*rtd
,
263 struct snd_pcm_hw_params
*params
)
265 struct snd_interval
*chan
= hw_param_interval(params
,
266 SNDRV_PCM_HW_PARAM_CHANNELS
);
269 * set BE channel constraint as user FE channels
271 chan
->min
= chan
->max
= 4;
276 static int geminilake_dmic_startup(struct snd_pcm_substream
*substream
)
278 struct snd_pcm_runtime
*runtime
= substream
->runtime
;
280 runtime
->hw
.channels_min
= runtime
->hw
.channels_max
= QUAD_CHANNEL
;
281 snd_pcm_hw_constraint_list(runtime
, 0, SNDRV_PCM_HW_PARAM_CHANNELS
,
282 &constraints_channels_quad
);
284 return snd_pcm_hw_constraint_list(substream
->runtime
, 0,
285 SNDRV_PCM_HW_PARAM_RATE
, &constraints_rates
);
288 static const struct snd_soc_ops geminilake_dmic_ops
= {
289 .startup
= geminilake_dmic_startup
,
292 static const unsigned int rates_16000
[] = {
296 static const struct snd_pcm_hw_constraint_list constraints_16000
= {
297 .count
= ARRAY_SIZE(rates_16000
),
301 static int geminilake_refcap_startup(struct snd_pcm_substream
*substream
)
303 return snd_pcm_hw_constraint_list(substream
->runtime
, 0,
304 SNDRV_PCM_HW_PARAM_RATE
,
308 static const struct snd_soc_ops geminilake_refcap_ops
= {
309 .startup
= geminilake_refcap_startup
,
312 SND_SOC_DAILINK_DEF(dummy
,
313 DAILINK_COMP_ARRAY(COMP_DUMMY()));
315 SND_SOC_DAILINK_DEF(system
,
316 DAILINK_COMP_ARRAY(COMP_CPU("System Pin")));
318 SND_SOC_DAILINK_DEF(system2
,
319 DAILINK_COMP_ARRAY(COMP_CPU("System Pin2")));
321 SND_SOC_DAILINK_DEF(echoref
,
322 DAILINK_COMP_ARRAY(COMP_CPU("Echoref Pin")));
324 SND_SOC_DAILINK_DEF(reference
,
325 DAILINK_COMP_ARRAY(COMP_CPU("Reference Pin")));
327 SND_SOC_DAILINK_DEF(dmic
,
328 DAILINK_COMP_ARRAY(COMP_CPU("DMIC Pin")));
330 SND_SOC_DAILINK_DEF(hdmi1
,
331 DAILINK_COMP_ARRAY(COMP_CPU("HDMI1 Pin")));
333 SND_SOC_DAILINK_DEF(hdmi2
,
334 DAILINK_COMP_ARRAY(COMP_CPU("HDMI2 Pin")));
336 SND_SOC_DAILINK_DEF(hdmi3
,
337 DAILINK_COMP_ARRAY(COMP_CPU("HDMI3 Pin")));
339 SND_SOC_DAILINK_DEF(ssp1_pin
,
340 DAILINK_COMP_ARRAY(COMP_CPU("SSP1 Pin")));
341 SND_SOC_DAILINK_DEF(ssp1_codec
,
342 DAILINK_COMP_ARRAY(COMP_CODEC(MAXIM_DEV0_NAME
,
343 GLK_MAXIM_CODEC_DAI
)));
345 SND_SOC_DAILINK_DEF(ssp2_pin
,
346 DAILINK_COMP_ARRAY(COMP_CPU("SSP2 Pin")));
347 SND_SOC_DAILINK_DEF(ssp2_codec
,
348 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5682:00",
349 GLK_REALTEK_CODEC_DAI
)));
351 SND_SOC_DAILINK_DEF(dmic_pin
,
352 DAILINK_COMP_ARRAY(COMP_CPU("DMIC01 Pin")));
353 SND_SOC_DAILINK_DEF(dmic_codec
,
354 DAILINK_COMP_ARRAY(COMP_CODEC("dmic-codec", "dmic-hifi")));
356 SND_SOC_DAILINK_DEF(idisp1_pin
,
357 DAILINK_COMP_ARRAY(COMP_CPU("iDisp1 Pin")));
358 SND_SOC_DAILINK_DEF(idisp1_codec
,
359 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi1")));
361 SND_SOC_DAILINK_DEF(idisp2_pin
,
362 DAILINK_COMP_ARRAY(COMP_CPU("iDisp2 Pin")));
363 SND_SOC_DAILINK_DEF(idisp2_codec
,
364 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi2")));
366 SND_SOC_DAILINK_DEF(idisp3_pin
,
367 DAILINK_COMP_ARRAY(COMP_CPU("iDisp3 Pin")));
368 SND_SOC_DAILINK_DEF(idisp3_codec
,
369 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi3")));
371 SND_SOC_DAILINK_DEF(platform
,
372 DAILINK_COMP_ARRAY(COMP_PLATFORM("0000:00:0e.0")));
374 /* geminilake digital audio interface glue - connects codec <--> CPU */
375 static struct snd_soc_dai_link geminilake_dais
[] = {
376 /* Front End DAI links */
377 [GLK_DPCM_AUDIO_PB
] = {
378 .name
= "Glk Audio Port",
379 .stream_name
= "Audio",
382 .init
= geminilake_rt5682_fe_init
,
384 SND_SOC_DPCM_TRIGGER_POST
, SND_SOC_DPCM_TRIGGER_POST
},
386 SND_SOC_DAILINK_REG(system
, dummy
, platform
),
388 [GLK_DPCM_AUDIO_CP
] = {
389 .name
= "Glk Audio Capture Port",
390 .stream_name
= "Audio Record",
394 SND_SOC_DPCM_TRIGGER_POST
, SND_SOC_DPCM_TRIGGER_POST
},
396 SND_SOC_DAILINK_REG(system
, dummy
, platform
),
398 [GLK_DPCM_AUDIO_HS_PB
] = {
399 .name
= "Glk Audio Headset Playback",
400 .stream_name
= "Headset Audio",
404 SND_SOC_DAILINK_REG(system2
, dummy
, platform
),
406 [GLK_DPCM_AUDIO_ECHO_REF_CP
] = {
407 .name
= "Glk Audio Echo Reference cap",
408 .stream_name
= "Echoreference Capture",
412 SND_SOC_DAILINK_REG(echoref
, dummy
, platform
),
414 [GLK_DPCM_AUDIO_REF_CP
] = {
415 .name
= "Glk Audio Reference cap",
416 .stream_name
= "Refcap",
421 .ops
= &geminilake_refcap_ops
,
422 SND_SOC_DAILINK_REG(reference
, dummy
, platform
),
424 [GLK_DPCM_AUDIO_DMIC_CP
] = {
425 .name
= "Glk Audio DMIC cap",
426 .stream_name
= "dmiccap",
431 .ops
= &geminilake_dmic_ops
,
432 SND_SOC_DAILINK_REG(dmic
, dummy
, platform
),
434 [GLK_DPCM_AUDIO_HDMI1_PB
] = {
435 .name
= "Glk HDMI Port1",
436 .stream_name
= "Hdmi1",
440 SND_SOC_DPCM_TRIGGER_POST
, SND_SOC_DPCM_TRIGGER_POST
},
443 SND_SOC_DAILINK_REG(hdmi1
, dummy
, platform
),
445 [GLK_DPCM_AUDIO_HDMI2_PB
] = {
446 .name
= "Glk HDMI Port2",
447 .stream_name
= "Hdmi2",
451 SND_SOC_DPCM_TRIGGER_POST
, SND_SOC_DPCM_TRIGGER_POST
},
454 SND_SOC_DAILINK_REG(hdmi2
, dummy
, platform
),
456 [GLK_DPCM_AUDIO_HDMI3_PB
] = {
457 .name
= "Glk HDMI Port3",
458 .stream_name
= "Hdmi3",
460 SND_SOC_DPCM_TRIGGER_POST
, SND_SOC_DPCM_TRIGGER_POST
},
465 SND_SOC_DAILINK_REG(hdmi3
, dummy
, platform
),
467 /* Back End DAI links */
470 .name
= "SSP1-Codec",
473 .dai_fmt
= SND_SOC_DAIFMT_I2S
|
474 SND_SOC_DAIFMT_NB_NF
|
475 SND_SOC_DAIFMT_CBS_CFS
,
476 .ignore_pmdown_time
= 1,
477 .be_hw_params_fixup
= geminilake_ssp_fixup
,
479 SND_SOC_DAILINK_REG(ssp1_pin
, ssp1_codec
, platform
),
483 .name
= "SSP2-Codec",
486 .init
= geminilake_rt5682_codec_init
,
487 .dai_fmt
= SND_SOC_DAIFMT_I2S
| SND_SOC_DAIFMT_NB_NF
|
488 SND_SOC_DAIFMT_CBS_CFS
,
489 .ignore_pmdown_time
= 1,
490 .be_hw_params_fixup
= geminilake_ssp_fixup
,
491 .ops
= &geminilake_rt5682_ops
,
494 SND_SOC_DAILINK_REG(ssp2_pin
, ssp2_codec
, platform
),
500 .be_hw_params_fixup
= geminilake_dmic_fixup
,
503 SND_SOC_DAILINK_REG(dmic_pin
, dmic_codec
, platform
),
508 .init
= geminilake_hdmi_init
,
511 SND_SOC_DAILINK_REG(idisp1_pin
, idisp1_codec
, platform
),
516 .init
= geminilake_hdmi_init
,
519 SND_SOC_DAILINK_REG(idisp2_pin
, idisp2_codec
, platform
),
524 .init
= geminilake_hdmi_init
,
527 SND_SOC_DAILINK_REG(idisp3_pin
, idisp3_codec
, platform
),
531 static int glk_card_late_probe(struct snd_soc_card
*card
)
533 struct glk_card_private
*ctx
= snd_soc_card_get_drvdata(card
);
534 struct snd_soc_component
*component
= NULL
;
535 char jack_name
[NAME_SIZE
];
536 struct glk_hdmi_pcm
*pcm
;
540 pcm
= list_first_entry(&ctx
->hdmi_pcm_list
, struct glk_hdmi_pcm
,
542 component
= pcm
->codec_dai
->component
;
544 if (ctx
->common_hdmi_codec_drv
)
545 return hda_dsp_hdmi_build_controls(card
, component
);
547 list_for_each_entry(pcm
, &ctx
->hdmi_pcm_list
, head
) {
548 component
= pcm
->codec_dai
->component
;
549 snprintf(jack_name
, sizeof(jack_name
),
550 "HDMI/DP, pcm=%d Jack", pcm
->device
);
551 err
= snd_soc_card_jack_new(card
, jack_name
,
552 SND_JACK_AVOUT
, &geminilake_hdmi
[i
],
558 err
= hdac_hdmi_jack_init(pcm
->codec_dai
, pcm
->device
,
559 &geminilake_hdmi
[i
]);
569 return hdac_hdmi_jack_port_init(component
, &card
->dapm
);
572 /* geminilake audio machine driver for SPT + RT5682 */
573 static struct snd_soc_card glk_audio_card_rt5682_m98357a
= {
574 .name
= "glkrt5682max",
575 .owner
= THIS_MODULE
,
576 .dai_link
= geminilake_dais
,
577 .num_links
= ARRAY_SIZE(geminilake_dais
),
578 .controls
= geminilake_controls
,
579 .num_controls
= ARRAY_SIZE(geminilake_controls
),
580 .dapm_widgets
= geminilake_widgets
,
581 .num_dapm_widgets
= ARRAY_SIZE(geminilake_widgets
),
582 .dapm_routes
= geminilake_map
,
583 .num_dapm_routes
= ARRAY_SIZE(geminilake_map
),
584 .fully_routed
= true,
585 .late_probe
= glk_card_late_probe
,
588 static int geminilake_audio_probe(struct platform_device
*pdev
)
590 struct glk_card_private
*ctx
;
591 struct snd_soc_acpi_mach
*mach
;
592 const char *platform_name
;
593 struct snd_soc_card
*card
;
596 ctx
= devm_kzalloc(&pdev
->dev
, sizeof(*ctx
), GFP_KERNEL
);
600 INIT_LIST_HEAD(&ctx
->hdmi_pcm_list
);
602 card
= &glk_audio_card_rt5682_m98357a
;
603 card
->dev
= &pdev
->dev
;
604 snd_soc_card_set_drvdata(card
, ctx
);
606 /* override plaform name, if required */
607 mach
= (&pdev
->dev
)->platform_data
;
608 platform_name
= mach
->mach_params
.platform
;
610 ret
= snd_soc_fixup_dai_links_platform_name(card
, platform_name
);
614 ctx
->common_hdmi_codec_drv
= mach
->mach_params
.common_hdmi_codec_drv
;
616 return devm_snd_soc_register_card(&pdev
->dev
, card
);
619 static const struct platform_device_id glk_board_ids
[] = {
621 .name
= "glk_rt5682_max98357a",
623 (kernel_ulong_t
)&glk_audio_card_rt5682_m98357a
,
628 static struct platform_driver geminilake_audio
= {
629 .probe
= geminilake_audio_probe
,
631 .name
= "glk_rt5682_max98357a",
632 .pm
= &snd_soc_pm_ops
,
634 .id_table
= glk_board_ids
,
636 module_platform_driver(geminilake_audio
)
638 /* Module information */
639 MODULE_DESCRIPTION("Geminilake Audio Machine driver-RT5682 & MAX98357A in I2S mode");
640 MODULE_AUTHOR("Naveen Manohar <naveen.m@intel.com>");
641 MODULE_AUTHOR("Harsha Priya <harshapriya.n@intel.com>");
642 MODULE_LICENSE("GPL v2");
643 MODULE_ALIAS("platform:glk_rt5682_max98357a");