1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright(c) 2019 Intel Corporation.
5 * Intel SOF Machine driver for Dialog headphone codec
8 #include <linux/input.h>
9 #include <linux/module.h>
10 #include <sound/jack.h>
11 #include <sound/pcm.h>
12 #include <sound/pcm_params.h>
13 #include <linux/platform_device.h>
14 #include <sound/soc.h>
15 #include <sound/soc-acpi.h>
16 #include <sound/sof.h>
17 #include "../../codecs/da7219.h"
18 #include "hda_dsp_common.h"
19 #include "sof_hdmi_common.h"
20 #include "sof_maxim_common.h"
21 #include "sof_ssp_common.h"
24 #define SOF_DA7219_JSL_BOARD BIT(2)
26 #define DIALOG_CODEC_DAI "da7219-hifi"
29 struct snd_soc_jack headset_jack;
30 struct sof_hdmi_private hdmi;
31 enum sof_ssp_codec codec_type;
32 enum sof_ssp_codec amp_type;
34 unsigned int pll_bypass:1;
37 static int platform_clock_control(struct snd_soc_dapm_widget *w,
38 struct snd_kcontrol *k, int event)
40 struct snd_soc_dapm_context *dapm = w->dapm;
41 struct snd_soc_card *card = dapm->card;
42 struct card_private *ctx = snd_soc_card_get_drvdata(card);
43 struct snd_soc_dai *codec_dai;
50 codec_dai = snd_soc_card_get_codec_dai(card, DIALOG_CODEC_DAI);
52 dev_err(card->dev, "Codec dai not found; Unable to set/unset codec pll\n");
56 if (SND_SOC_DAPM_EVENT_OFF(event)) {
57 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK,
60 dev_err(card->dev, "failed to stop PLL: %d\n", ret);
61 } else if (SND_SOC_DAPM_EVENT_ON(event)) {
62 dev_dbg(card->dev, "pll srm mode\n");
64 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_PLL_SRM,
65 0, DA7219_PLL_FREQ_OUT_98304);
67 dev_err(card->dev, "failed to start PLL: %d\n", ret);
73 static const struct snd_kcontrol_new controls[] = {
74 SOC_DAPM_PIN_SWITCH("Headphone Jack"),
75 SOC_DAPM_PIN_SWITCH("Headset Mic"),
76 SOC_DAPM_PIN_SWITCH("Line Out"),
77 SOC_DAPM_PIN_SWITCH("Left Spk"),
78 SOC_DAPM_PIN_SWITCH("Right Spk"),
81 static const struct snd_soc_dapm_widget widgets[] = {
82 SND_SOC_DAPM_HP("Headphone Jack", NULL),
83 SND_SOC_DAPM_MIC("Headset Mic", NULL),
84 SND_SOC_DAPM_LINE("Line Out", NULL),
86 SND_SOC_DAPM_SPK("Left Spk", NULL),
87 SND_SOC_DAPM_SPK("Right Spk", NULL),
89 SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
90 platform_clock_control, SND_SOC_DAPM_POST_PMD |
91 SND_SOC_DAPM_PRE_PMU),
93 SND_SOC_DAPM_MIC("SoC DMIC", NULL),
96 static const struct snd_soc_dapm_route audio_map[] = {
97 { "Headphone Jack", NULL, "HPL" },
98 { "Headphone Jack", NULL, "HPR" },
100 { "MIC", NULL, "Headset Mic" },
102 { "Headphone Jack", NULL, "Platform Clock" },
103 { "Headset Mic", NULL, "Platform Clock" },
104 { "Line Out", NULL, "Platform Clock" },
107 {"DMic", NULL, "SoC DMIC"},
110 static struct snd_soc_jack_pin jack_pins[] = {
112 .pin = "Headphone Jack",
113 .mask = SND_JACK_HEADPHONE,
116 .pin = "Headset Mic",
117 .mask = SND_JACK_MICROPHONE,
121 .mask = SND_JACK_LINEOUT,
125 static int da7219_codec_init(struct snd_soc_pcm_runtime *rtd)
127 struct card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
128 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
129 struct snd_soc_component *component = codec_dai->component;
130 struct snd_soc_jack *jack = &ctx->headset_jack;
133 mclk_rate = sof_dai_get_mclk(rtd);
134 if (mclk_rate <= 0) {
135 dev_err(rtd->dev, "invalid mclk freq %d\n", mclk_rate);
139 ret = snd_soc_dai_set_sysclk(codec_dai, DA7219_CLKSRC_MCLK, mclk_rate,
142 dev_err(rtd->dev, "fail to set sysclk, ret %d\n", ret);
147 * Use PLL bypass mode if MCLK is available, be sure to set the
148 * frequency of MCLK to 12.288 or 24.576MHz on topology side.
150 if (mclk_rate == 12288000 || mclk_rate == 24576000) {
151 /* PLL bypass mode */
152 dev_dbg(rtd->dev, "pll bypass mode, mclk rate %d\n", mclk_rate);
154 ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK, 0, 0);
156 dev_err(rtd->dev, "fail to set pll, ret %d\n", ret);
164 * Headset buttons map to the google Reference headset.
165 * These can be configured by userspace.
167 ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
168 SND_JACK_HEADSET | SND_JACK_BTN_0 |
169 SND_JACK_BTN_1 | SND_JACK_BTN_2 |
170 SND_JACK_BTN_3 | SND_JACK_LINEOUT,
171 jack, jack_pins, ARRAY_SIZE(jack_pins));
173 dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
177 snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
178 snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
179 snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
180 snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
182 ret = snd_soc_component_set_jack(component, jack, NULL);
184 dev_err(rtd->dev, "fail to set component jack, ret %d\n", ret);
191 static int max98373_hw_params(struct snd_pcm_substream *substream,
192 struct snd_pcm_hw_params *params)
194 struct snd_soc_pcm_runtime *runtime = snd_soc_substream_to_rtd(substream);
197 for (j = 0; j < runtime->dai_link->num_codecs; j++) {
198 struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(runtime, j);
200 if (!strcmp(codec_dai->component->name, MAX_98373_DEV0_NAME)) {
201 /* vmon_slot_no = 0 imon_slot_no = 1 for TX slots */
202 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x3, 3, 4, 16);
204 dev_err(runtime->dev, "DEV0 TDM slot err:%d\n", ret);
208 if (!strcmp(codec_dai->component->name, MAX_98373_DEV1_NAME)) {
209 /* vmon_slot_no = 2 imon_slot_no = 3 for TX slots */
210 ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xC, 3, 4, 16);
212 dev_err(runtime->dev, "DEV1 TDM slot err:%d\n", ret);
221 static const struct snd_soc_ops max98373_ops = {
222 .hw_params = max98373_hw_params,
225 static int hdmi_init(struct snd_soc_pcm_runtime *rtd)
227 struct card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
228 struct snd_soc_dai *dai = snd_soc_rtd_to_codec(rtd, 0);
230 ctx->hdmi.hdmi_comp = dai->component;
235 static int card_late_probe(struct snd_soc_card *card)
237 struct card_private *ctx = snd_soc_card_get_drvdata(card);
239 if (!ctx->hdmi.idisp_codec)
242 if (!ctx->hdmi.hdmi_comp)
245 return hda_dsp_hdmi_build_controls(card, ctx->hdmi.hdmi_comp);
248 SND_SOC_DAILINK_DEF(ssp0_pin,
249 DAILINK_COMP_ARRAY(COMP_CPU("SSP0 Pin")));
250 SND_SOC_DAILINK_DEF(ssp0_codec,
251 DAILINK_COMP_ARRAY(COMP_CODEC("i2c-DLGS7219:00", DIALOG_CODEC_DAI)));
253 SND_SOC_DAILINK_DEF(ssp1_pin,
254 DAILINK_COMP_ARRAY(COMP_CPU("SSP1 Pin")));
256 SND_SOC_DAILINK_DEF(ssp2_pin,
257 DAILINK_COMP_ARRAY(COMP_CPU("SSP2 Pin")));
258 SND_SOC_DAILINK_DEF(dummy_codec,
259 DAILINK_COMP_ARRAY(COMP_CODEC("snd-soc-dummy", "snd-soc-dummy-dai")));
261 SND_SOC_DAILINK_DEF(dmic_pin,
262 DAILINK_COMP_ARRAY(COMP_CPU("DMIC01 Pin")));
263 SND_SOC_DAILINK_DEF(dmic_codec,
264 DAILINK_COMP_ARRAY(COMP_CODEC("dmic-codec", "dmic-hifi")));
266 SND_SOC_DAILINK_DEF(dmic16k_pin,
267 DAILINK_COMP_ARRAY(COMP_CPU("DMIC16k Pin")));
269 SND_SOC_DAILINK_DEF(idisp1_pin,
270 DAILINK_COMP_ARRAY(COMP_CPU("iDisp1 Pin")));
271 SND_SOC_DAILINK_DEF(idisp1_codec,
272 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi1")));
274 SND_SOC_DAILINK_DEF(idisp2_pin,
275 DAILINK_COMP_ARRAY(COMP_CPU("iDisp2 Pin")));
276 SND_SOC_DAILINK_DEF(idisp2_codec,
277 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi2")));
279 SND_SOC_DAILINK_DEF(idisp3_pin,
280 DAILINK_COMP_ARRAY(COMP_CPU("iDisp3 Pin")));
281 SND_SOC_DAILINK_DEF(idisp3_codec,
282 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi3")));
284 SND_SOC_DAILINK_DEF(idisp4_pin,
285 DAILINK_COMP_ARRAY(COMP_CPU("iDisp4 Pin")));
286 SND_SOC_DAILINK_DEF(idisp4_codec,
287 DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi4")));
289 SND_SOC_DAILINK_DEF(platform, /* subject to be overridden during probe */
290 DAILINK_COMP_ARRAY(COMP_PLATFORM("0000:00:1f.3")));
292 static struct snd_soc_dai_link jsl_dais[] = {
293 /* Back End DAI links */
295 .name = "SSP1-Codec",
297 .ignore_pmdown_time = 1,
300 .dpcm_capture = 1, /* IV feedback */
301 SND_SOC_DAILINK_REG(ssp1_pin, max_98373_components, platform),
304 .name = "SSP0-Codec",
307 .init = da7219_codec_init,
308 .ignore_pmdown_time = 1,
311 SND_SOC_DAILINK_REG(ssp0_pin, ssp0_codec, platform),
319 SND_SOC_DAILINK_REG(dmic_pin, dmic_codec, platform),
327 SND_SOC_DAILINK_REG(idisp1_pin, idisp1_codec, platform),
335 SND_SOC_DAILINK_REG(idisp2_pin, idisp2_codec, platform),
343 SND_SOC_DAILINK_REG(idisp3_pin, idisp3_codec, platform),
351 SND_SOC_DAILINK_REG(dmic16k_pin, dmic_codec, platform),
355 static struct snd_soc_dai_link adl_dais[] = {
356 /* Back End DAI links */
358 .name = "SSP0-Codec",
361 .init = da7219_codec_init,
362 .ignore_pmdown_time = 1,
365 SND_SOC_DAILINK_REG(ssp0_pin, ssp0_codec, platform),
373 SND_SOC_DAILINK_REG(dmic_pin, dmic_codec, platform),
381 SND_SOC_DAILINK_REG(dmic16k_pin, dmic_codec, platform),
389 SND_SOC_DAILINK_REG(idisp1_pin, idisp1_codec, platform),
397 SND_SOC_DAILINK_REG(idisp2_pin, idisp2_codec, platform),
405 SND_SOC_DAILINK_REG(idisp3_pin, idisp3_codec, platform),
413 SND_SOC_DAILINK_REG(idisp4_pin, idisp4_codec, platform),
416 .name = "SSP1-Codec",
420 /* feedback stream or firmware-generated echo reference */
422 SND_SOC_DAILINK_REG(ssp1_pin, max_98373_components, platform),
430 SND_SOC_DAILINK_REG(ssp2_pin, dummy_codec, platform),
434 static struct snd_soc_card card_da7219 = {
435 .name = "da7219", /* the sof- prefix is added by the core */
436 .owner = THIS_MODULE,
437 .controls = controls,
438 .num_controls = ARRAY_SIZE(controls),
439 .dapm_widgets = widgets,
440 .num_dapm_widgets = ARRAY_SIZE(widgets),
441 .dapm_routes = audio_map,
442 .num_dapm_routes = ARRAY_SIZE(audio_map),
443 .fully_routed = true,
444 .late_probe = card_late_probe,
447 static int audio_probe(struct platform_device *pdev)
449 struct snd_soc_acpi_mach *mach = pdev->dev.platform_data;
450 struct snd_soc_dai_link *dai_links;
451 struct card_private *ctx;
452 unsigned long board_quirk = 0;
455 ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
459 if (pdev->id_entry && pdev->id_entry->driver_data)
460 board_quirk = (unsigned long)pdev->id_entry->driver_data;
462 ctx->codec_type = sof_ssp_detect_codec_type(&pdev->dev);
463 ctx->amp_type = sof_ssp_detect_amp_type(&pdev->dev);
465 if (mach->mach_params.codec_mask & IDISP_CODEC_MASK)
466 ctx->hdmi.idisp_codec = true;
468 if (board_quirk & SOF_DA7219_JSL_BOARD) {
469 /* backward-compatible with existing devices */
470 switch (ctx->amp_type) {
471 case CODEC_MAX98360A:
472 card_da7219.name = devm_kstrdup(&pdev->dev,
477 card_da7219.name = devm_kstrdup(&pdev->dev, "da7219max",
484 dai_links = jsl_dais;
487 card_da7219.num_links = ARRAY_SIZE(jsl_dais);
489 dai_links = adl_dais;
492 card_da7219.num_links = ARRAY_SIZE(adl_dais);
495 dev_dbg(&pdev->dev, "board_quirk = %lx\n", board_quirk);
498 switch (ctx->amp_type) {
499 case CODEC_MAX98360A:
500 max_98360a_dai_link(&dai_links[amp_idx]);
503 dai_links[amp_idx].codecs = max_98373_components;
504 dai_links[amp_idx].num_codecs = ARRAY_SIZE(max_98373_components);
505 dai_links[amp_idx].init = max_98373_spk_codec_init;
506 if (board_quirk & SOF_DA7219_JSL_BOARD) {
507 dai_links[amp_idx].ops = &max98373_ops; /* use local ops */
509 /* TBD: implement the amp for later platform */
510 dev_err(&pdev->dev, "max98373 not support yet\n");
514 max_98373_set_codec_conf(&card_da7219);
517 dev_err(&pdev->dev, "invalid amp type %d\n", ctx->amp_type);
521 card_da7219.dai_link = dai_links;
523 card_da7219.dev = &pdev->dev;
525 ret = snd_soc_fixup_dai_links_platform_name(&card_da7219,
526 mach->mach_params.platform);
530 snd_soc_card_set_drvdata(&card_da7219, ctx);
532 return devm_snd_soc_register_card(&pdev->dev, &card_da7219);
535 static const struct platform_device_id board_ids[] = {
537 .name = "jsl_mx98373_da7219",
538 .driver_data = (kernel_ulong_t)(SOF_DA7219_JSL_BOARD),
541 .name = "jsl_mx98360_da7219",
542 .driver_data = (kernel_ulong_t)(SOF_DA7219_JSL_BOARD),
545 .name = "adl_mx98360_da7219",
546 /* no quirk needed for this board */
550 MODULE_DEVICE_TABLE(platform, board_ids);
552 static struct platform_driver audio = {
553 .probe = audio_probe,
555 .name = "sof_da7219",
556 .pm = &snd_soc_pm_ops,
558 .id_table = board_ids,
560 module_platform_driver(audio)
562 /* Module information */
563 MODULE_DESCRIPTION("ASoC Intel(R) SOF Machine driver for Dialog codec");
564 MODULE_AUTHOR("Yong Zhi <yong.zhi@intel.com>");
565 MODULE_AUTHOR("Brent Lu <brent.lu@intel.com>");
566 MODULE_LICENSE("GPL v2");
567 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
568 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_MAXIM_COMMON);
569 MODULE_IMPORT_NS(SND_SOC_INTEL_SOF_SSP_COMMON);