Path: blob/master/sound/soc/intel/boards/sof_board_helpers.c
51061 views
// SPDX-License-Identifier: GPL-2.0-only1//2// Copyright(c) 2023 Intel Corporation34#include <sound/soc.h>5#include "../common/soc-intel-quirks.h"6#include "hda_dsp_common.h"7#include "sof_board_helpers.h"89/*10* Intel HDMI DAI Link11*/12static int hdmi_init(struct snd_soc_pcm_runtime *rtd)13{14struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);15struct snd_soc_dai *dai = snd_soc_rtd_to_codec(rtd, 0);1617ctx->hdmi.hdmi_comp = dai->component;1819return 0;20}2122int sof_intel_board_card_late_probe(struct snd_soc_card *card)23{24struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);2526if (!ctx->hdmi_num)27return 0;2829if (!ctx->hdmi.idisp_codec)30return 0;3132if (!ctx->hdmi.hdmi_comp)33return -EINVAL;3435return hda_dsp_hdmi_build_controls(card, ctx->hdmi.hdmi_comp);36}37EXPORT_SYMBOL_NS(sof_intel_board_card_late_probe, "SND_SOC_INTEL_SOF_BOARD_HELPERS");3839/*40* DMIC DAI Link41*/42static const struct snd_soc_dapm_widget dmic_widgets[] = {43SND_SOC_DAPM_MIC("SoC DMIC", NULL),44};4546static const struct snd_soc_dapm_route dmic_routes[] = {47{"DMic", NULL, "SoC DMIC"},48};4950static int dmic_init(struct snd_soc_pcm_runtime *rtd)51{52struct snd_soc_card *card = rtd->card;53struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);54int ret;5556ret = snd_soc_dapm_new_controls(dapm, dmic_widgets,57ARRAY_SIZE(dmic_widgets));58if (ret) {59dev_err(rtd->dev, "fail to add dmic widgets, ret %d\n", ret);60return ret;61}6263ret = snd_soc_dapm_add_routes(dapm, dmic_routes,64ARRAY_SIZE(dmic_routes));65if (ret) {66dev_err(rtd->dev, "fail to add dmic routes, ret %d\n", ret);67return ret;68}6970return 0;71}7273/*74* HDA External Codec DAI Link75*/76static const struct snd_soc_dapm_widget hda_widgets[] = {77SND_SOC_DAPM_MIC("Analog In", NULL),78SND_SOC_DAPM_MIC("Digital In", NULL),79SND_SOC_DAPM_MIC("Alt Analog In", NULL),8081SND_SOC_DAPM_HP("Analog Out", NULL),82SND_SOC_DAPM_SPK("Digital Out", NULL),83SND_SOC_DAPM_HP("Alt Analog Out", NULL),84};8586static const struct snd_soc_dapm_route hda_routes[] = {87{ "Codec Input Pin1", NULL, "Analog In" },88{ "Codec Input Pin2", NULL, "Digital In" },89{ "Codec Input Pin3", NULL, "Alt Analog In" },9091{ "Analog Out", NULL, "Codec Output Pin1" },92{ "Digital Out", NULL, "Codec Output Pin2" },93{ "Alt Analog Out", NULL, "Codec Output Pin3" },9495/* CODEC BE connections */96{ "codec0_in", NULL, "Analog CPU Capture" },97{ "Analog CPU Capture", NULL, "Analog Codec Capture" },98{ "codec1_in", NULL, "Digital CPU Capture" },99{ "Digital CPU Capture", NULL, "Digital Codec Capture" },100{ "codec2_in", NULL, "Alt Analog CPU Capture" },101{ "Alt Analog CPU Capture", NULL, "Alt Analog Codec Capture" },102103{ "Analog Codec Playback", NULL, "Analog CPU Playback" },104{ "Analog CPU Playback", NULL, "codec0_out" },105{ "Digital Codec Playback", NULL, "Digital CPU Playback" },106{ "Digital CPU Playback", NULL, "codec1_out" },107{ "Alt Analog Codec Playback", NULL, "Alt Analog CPU Playback" },108{ "Alt Analog CPU Playback", NULL, "codec2_out" },109};110111static int hda_init(struct snd_soc_pcm_runtime *rtd)112{113struct snd_soc_card *card = rtd->card;114struct snd_soc_dapm_context *dapm = snd_soc_card_to_dapm(card);115int ret;116117ret = snd_soc_dapm_new_controls(dapm, hda_widgets,118ARRAY_SIZE(hda_widgets));119if (ret) {120dev_err(rtd->dev, "fail to add hda widgets, ret %d\n", ret);121return ret;122}123124ret = snd_soc_dapm_add_routes(dapm, hda_routes,125ARRAY_SIZE(hda_routes));126if (ret)127dev_err(rtd->dev, "fail to add hda routes, ret %d\n", ret);128129return ret;130}131132/*133* DAI Link Helpers134*/135136enum sof_dmic_be_type {137SOF_DMIC_01,138SOF_DMIC_16K,139};140141enum sof_hda_be_type {142SOF_HDA_ANALOG,143SOF_HDA_DIGITAL,144};145146/* DEFAULT_LINK_ORDER: the order used in sof_rt5682 */147#define DEFAULT_LINK_ORDER SOF_LINK_ORDER(SOF_LINK_CODEC, \148SOF_LINK_DMIC01, \149SOF_LINK_DMIC16K, \150SOF_LINK_IDISP_HDMI, \151SOF_LINK_AMP, \152SOF_LINK_BT_OFFLOAD, \153SOF_LINK_HDMI_IN)154155static struct snd_soc_dai_link_component dmic_component[] = {156{157.name = "dmic-codec",158.dai_name = "dmic-hifi",159}160};161162SND_SOC_DAILINK_DEF(hda_analog_cpus,163DAILINK_COMP_ARRAY(COMP_CPU("Analog CPU DAI")));164SND_SOC_DAILINK_DEF(hda_analog_codecs,165DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D0", "Analog Codec DAI")));166167SND_SOC_DAILINK_DEF(hda_digital_cpus,168DAILINK_COMP_ARRAY(COMP_CPU("Digital CPU DAI")));169SND_SOC_DAILINK_DEF(hda_digital_codecs,170DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D0", "Digital Codec DAI")));171172static struct snd_soc_dai_link_component platform_component[] = {173{174/* name might be overridden during probe */175.name = "0000:00:1f.3"176}177};178179static int set_ssp_codec_link(struct device *dev, struct snd_soc_dai_link *link,180int be_id, enum snd_soc_acpi_intel_codec codec_type,181int ssp_codec)182{183struct snd_soc_dai_link_component *cpus;184185dev_dbg(dev, "link %d: ssp codec %s, ssp %d\n", be_id,186snd_soc_acpi_intel_get_codec_name(codec_type), ssp_codec);187188/* link name */189link->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_codec);190if (!link->name)191return -ENOMEM;192193/* cpus */194cpus = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component),195GFP_KERNEL);196if (!cpus)197return -ENOMEM;198199if (soc_intel_is_byt() || soc_intel_is_cht()) {200/* backward-compatibility for BYT/CHT boards */201cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "ssp%d-port",202ssp_codec);203} else {204cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin",205ssp_codec);206}207if (!cpus->dai_name)208return -ENOMEM;209210link->cpus = cpus;211link->num_cpus = 1;212213/* codecs - caller to handle */214215/* platforms */216link->platforms = platform_component;217link->num_platforms = ARRAY_SIZE(platform_component);218219link->id = be_id;220link->no_pcm = 1;221222return 0;223}224225static int set_dmic_link(struct device *dev, struct snd_soc_dai_link *link,226int be_id, enum sof_dmic_be_type be_type)227{228struct snd_soc_dai_link_component *cpus;229230/* cpus */231cpus = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component),232GFP_KERNEL);233if (!cpus)234return -ENOMEM;235236switch (be_type) {237case SOF_DMIC_01:238dev_dbg(dev, "link %d: dmic01\n", be_id);239240link->name = "dmic01";241cpus->dai_name = "DMIC01 Pin";242break;243case SOF_DMIC_16K:244dev_dbg(dev, "link %d: dmic16k\n", be_id);245246link->name = "dmic16k";247cpus->dai_name = "DMIC16k Pin";248break;249default:250dev_err(dev, "invalid be type %d\n", be_type);251return -EINVAL;252}253254link->cpus = cpus;255link->num_cpus = 1;256257/* codecs */258link->codecs = dmic_component;259link->num_codecs = ARRAY_SIZE(dmic_component);260261/* platforms */262link->platforms = platform_component;263link->num_platforms = ARRAY_SIZE(platform_component);264265link->id = be_id;266if (be_type == SOF_DMIC_01)267link->init = dmic_init;268link->ignore_suspend = 1;269link->no_pcm = 1;270link->capture_only = 1;271272return 0;273}274275static int set_idisp_hdmi_link(struct device *dev, struct snd_soc_dai_link *link,276int be_id, int hdmi_id, bool idisp_codec)277{278struct snd_soc_dai_link_component *cpus, *codecs;279280dev_dbg(dev, "link %d: idisp hdmi %d, idisp codec %d\n", be_id, hdmi_id,281idisp_codec);282283/* link name */284link->name = devm_kasprintf(dev, GFP_KERNEL, "iDisp%d", hdmi_id);285if (!link->name)286return -ENOMEM;287288/* cpus */289cpus = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component),290GFP_KERNEL);291if (!cpus)292return -ENOMEM;293294cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "iDisp%d Pin", hdmi_id);295if (!cpus->dai_name)296return -ENOMEM;297298link->cpus = cpus;299link->num_cpus = 1;300301/* codecs */302if (idisp_codec) {303codecs = devm_kzalloc(dev,304sizeof(struct snd_soc_dai_link_component),305GFP_KERNEL);306if (!codecs)307return -ENOMEM;308309codecs->name = "ehdaudio0D2";310codecs->dai_name = devm_kasprintf(dev, GFP_KERNEL,311"intel-hdmi-hifi%d", hdmi_id);312if (!codecs->dai_name)313return -ENOMEM;314315link->codecs = codecs;316} else {317link->codecs = &snd_soc_dummy_dlc;318}319link->num_codecs = 1;320321/* platforms */322link->platforms = platform_component;323link->num_platforms = ARRAY_SIZE(platform_component);324325link->id = be_id;326link->init = (hdmi_id == 1) ? hdmi_init : NULL;327link->no_pcm = 1;328link->playback_only = 1;329330return 0;331}332333static int set_ssp_amp_link(struct device *dev, struct snd_soc_dai_link *link,334int be_id, enum snd_soc_acpi_intel_codec amp_type,335int ssp_amp)336{337struct snd_soc_dai_link_component *cpus;338339dev_dbg(dev, "link %d: ssp amp %s, ssp %d\n", be_id,340snd_soc_acpi_intel_get_codec_name(amp_type), ssp_amp);341342/* link name */343link->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-Codec", ssp_amp);344if (!link->name)345return -ENOMEM;346347/* cpus */348cpus = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component),349GFP_KERNEL);350if (!cpus)351return -ENOMEM;352353cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_amp);354if (!cpus->dai_name)355return -ENOMEM;356357link->cpus = cpus;358link->num_cpus = 1;359360/* codecs - caller to handle */361362/* platforms */363/* feedback stream or firmware-generated echo reference */364link->platforms = platform_component;365link->num_platforms = ARRAY_SIZE(platform_component);366367link->id = be_id;368link->no_pcm = 1;369370return 0;371}372373static int set_bt_offload_link(struct device *dev, struct snd_soc_dai_link *link,374int be_id, int ssp_bt)375{376struct snd_soc_dai_link_component *cpus;377378dev_dbg(dev, "link %d: bt offload, ssp %d\n", be_id, ssp_bt);379380/* link name */381link->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT", ssp_bt);382if (!link->name)383return -ENOMEM;384385/* cpus */386cpus = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component),387GFP_KERNEL);388if (!cpus)389return -ENOMEM;390391cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_bt);392if (!cpus->dai_name)393return -ENOMEM;394395link->cpus = cpus;396link->num_cpus = 1;397398/* codecs */399link->codecs = &snd_soc_dummy_dlc;400link->num_codecs = 1;401402/* platforms */403link->platforms = platform_component;404link->num_platforms = ARRAY_SIZE(platform_component);405406link->id = be_id;407link->no_pcm = 1;408409return 0;410}411412static int set_hdmi_in_link(struct device *dev, struct snd_soc_dai_link *link,413int be_id, int ssp_hdmi)414{415struct snd_soc_dai_link_component *cpus;416417dev_dbg(dev, "link %d: hdmi-in, ssp %d\n", be_id, ssp_hdmi);418419/* link name */420link->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-HDMI", ssp_hdmi);421if (!link->name)422return -ENOMEM;423424/* cpus */425cpus = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component),426GFP_KERNEL);427if (!cpus)428return -ENOMEM;429430cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d Pin", ssp_hdmi);431if (!cpus->dai_name)432return -ENOMEM;433434link->cpus = cpus;435link->num_cpus = 1;436437/* codecs */438link->codecs = &snd_soc_dummy_dlc;439link->num_codecs = 1;440441/* platforms */442link->platforms = platform_component;443link->num_platforms = ARRAY_SIZE(platform_component);444445link->id = be_id;446link->no_pcm = 1;447link->capture_only = 1;448449return 0;450}451452static int set_hda_codec_link(struct device *dev, struct snd_soc_dai_link *link,453int be_id, enum sof_hda_be_type be_type)454{455switch (be_type) {456case SOF_HDA_ANALOG:457dev_dbg(dev, "link %d: hda analog\n", be_id);458459link->name = "Analog Playback and Capture";460461/* cpus */462link->cpus = hda_analog_cpus;463link->num_cpus = ARRAY_SIZE(hda_analog_cpus);464465/* codecs */466link->codecs = hda_analog_codecs;467link->num_codecs = ARRAY_SIZE(hda_analog_codecs);468break;469case SOF_HDA_DIGITAL:470dev_dbg(dev, "link %d: hda digital\n", be_id);471472link->name = "Digital Playback and Capture";473474/* cpus */475link->cpus = hda_digital_cpus;476link->num_cpus = ARRAY_SIZE(hda_digital_cpus);477478/* codecs */479link->codecs = hda_digital_codecs;480link->num_codecs = ARRAY_SIZE(hda_digital_codecs);481break;482default:483dev_err(dev, "invalid be type %d\n", be_type);484return -EINVAL;485}486487/* platforms */488link->platforms = platform_component;489link->num_platforms = ARRAY_SIZE(platform_component);490491link->id = be_id;492if (be_type == SOF_HDA_ANALOG)493link->init = hda_init;494link->no_pcm = 1;495496return 0;497}498499static int calculate_num_links(struct sof_card_private *ctx)500{501int num_links = 0;502503/* headphone codec */504if (ctx->codec_type != CODEC_NONE)505num_links++;506507/* dmic01 and dmic16k */508if (ctx->dmic_be_num > 0)509num_links++;510511if (ctx->dmic_be_num > 1)512num_links++;513514/* idisp HDMI */515num_links += ctx->hdmi_num;516517/* speaker amp */518if (ctx->amp_type != CODEC_NONE)519num_links++;520521/* BT audio offload */522if (ctx->bt_offload_present)523num_links++;524525/* HDMI-In */526num_links += hweight32(ctx->ssp_mask_hdmi_in);527528/* HDA external codec */529if (ctx->hda_codec_present)530num_links += 2;531532return num_links;533}534535int sof_intel_board_set_dai_link(struct device *dev, struct snd_soc_card *card,536struct sof_card_private *ctx)537{538struct snd_soc_dai_link *links;539int num_links;540int i;541int idx = 0;542int ret;543int ssp_hdmi_in = 0;544unsigned long link_order, link;545unsigned long link_ids, be_id;546547num_links = calculate_num_links(ctx);548549links = devm_kcalloc(dev, num_links, sizeof(struct snd_soc_dai_link),550GFP_KERNEL);551if (!links)552return -ENOMEM;553554if (ctx->link_order_overwrite)555link_order = ctx->link_order_overwrite;556else557link_order = DEFAULT_LINK_ORDER;558559if (ctx->link_id_overwrite)560link_ids = ctx->link_id_overwrite;561else562link_ids = 0;563564dev_dbg(dev, "create dai links, link_order 0x%lx, id_overwrite 0x%lx\n",565link_order, link_ids);566567while (link_order) {568link = link_order & SOF_LINK_ORDER_MASK;569link_order >>= SOF_LINK_ORDER_SHIFT;570571if (ctx->link_id_overwrite) {572be_id = link_ids & SOF_LINK_IDS_MASK;573link_ids >>= SOF_LINK_IDS_SHIFT;574} else {575/* use array index as link id */576be_id = idx;577}578579switch (link) {580case SOF_LINK_CODEC:581/* headphone codec */582if (ctx->codec_type == CODEC_NONE)583continue;584585ret = set_ssp_codec_link(dev, &links[idx], be_id,586ctx->codec_type, ctx->ssp_codec);587if (ret) {588dev_err(dev, "fail to set codec link, ret %d\n",589ret);590return ret;591}592593ctx->codec_link = &links[idx];594idx++;595break;596case SOF_LINK_DMIC01:597/* dmic01 */598if (ctx->dmic_be_num == 0)599continue;600601/* at least we have dmic01 */602ret = set_dmic_link(dev, &links[idx], be_id, SOF_DMIC_01);603if (ret) {604dev_err(dev, "fail to set dmic01 link, ret %d\n",605ret);606return ret;607}608609idx++;610break;611case SOF_LINK_DMIC16K:612/* dmic16k */613if (ctx->dmic_be_num <= 1)614continue;615616/* set up 2 BE links at most */617ret = set_dmic_link(dev, &links[idx], be_id,618SOF_DMIC_16K);619if (ret) {620dev_err(dev, "fail to set dmic16k link, ret %d\n",621ret);622return ret;623}624625idx++;626break;627case SOF_LINK_IDISP_HDMI:628/* idisp HDMI */629for (i = 1; i <= ctx->hdmi_num; i++) {630ret = set_idisp_hdmi_link(dev, &links[idx],631be_id, i,632ctx->hdmi.idisp_codec);633if (ret) {634dev_err(dev, "fail to set hdmi link, ret %d\n",635ret);636return ret;637}638639idx++;640be_id++;641}642break;643case SOF_LINK_AMP:644/* speaker amp */645if (ctx->amp_type == CODEC_NONE)646continue;647648ret = set_ssp_amp_link(dev, &links[idx], be_id,649ctx->amp_type, ctx->ssp_amp);650if (ret) {651dev_err(dev, "fail to set amp link, ret %d\n",652ret);653return ret;654}655656ctx->amp_link = &links[idx];657idx++;658break;659case SOF_LINK_BT_OFFLOAD:660/* BT audio offload */661if (!ctx->bt_offload_present)662continue;663664ret = set_bt_offload_link(dev, &links[idx], be_id,665ctx->ssp_bt);666if (ret) {667dev_err(dev, "fail to set bt link, ret %d\n",668ret);669return ret;670}671672idx++;673break;674case SOF_LINK_HDMI_IN:675/* HDMI-In */676for_each_set_bit(ssp_hdmi_in, &ctx->ssp_mask_hdmi_in, 32) {677ret = set_hdmi_in_link(dev, &links[idx], be_id,678ssp_hdmi_in);679if (ret) {680dev_err(dev, "fail to set hdmi-in link, ret %d\n",681ret);682return ret;683}684685idx++;686be_id++;687}688break;689case SOF_LINK_HDA:690/* HDA external codec */691if (!ctx->hda_codec_present)692continue;693694ret = set_hda_codec_link(dev, &links[idx], be_id,695SOF_HDA_ANALOG);696if (ret) {697dev_err(dev, "fail to set hda analog link, ret %d\n",698ret);699return ret;700}701702idx++;703be_id++;704705ret = set_hda_codec_link(dev, &links[idx], be_id,706SOF_HDA_DIGITAL);707if (ret) {708dev_err(dev, "fail to set hda digital link, ret %d\n",709ret);710return ret;711}712713idx++;714break;715case SOF_LINK_NONE:716/* caught here if it's not used as terminator in macro */717fallthrough;718default:719dev_err(dev, "invalid link type %ld\n", link);720return -EINVAL;721}722}723724if (idx != num_links) {725dev_err(dev, "link number mismatch, idx %d, num_links %d\n", idx,726num_links);727return -EINVAL;728}729730card->dai_link = links;731card->num_links = num_links;732733return 0;734}735EXPORT_SYMBOL_NS(sof_intel_board_set_dai_link, "SND_SOC_INTEL_SOF_BOARD_HELPERS");736737struct sof_card_private *738sof_intel_board_get_ctx(struct device *dev, unsigned long board_quirk)739{740struct sof_card_private *ctx;741742dev_dbg(dev, "create ctx, board_quirk 0x%lx\n", board_quirk);743744ctx = devm_kzalloc(dev, sizeof(struct sof_card_private), GFP_KERNEL);745if (!ctx)746return NULL;747748ctx->codec_type = snd_soc_acpi_intel_detect_codec_type(dev);749ctx->amp_type = snd_soc_acpi_intel_detect_amp_type(dev);750751ctx->dmic_be_num = 2;752ctx->hdmi_num = (board_quirk & SOF_NUM_IDISP_HDMI_MASK) >>753SOF_NUM_IDISP_HDMI_SHIFT;754/* default number of HDMI DAI's */755if (!ctx->hdmi_num)756ctx->hdmi_num = 3;757758/* port number/mask of peripherals attached to ssp interface */759if (ctx->codec_type != CODEC_NONE)760ctx->ssp_codec = (board_quirk & SOF_SSP_PORT_CODEC_MASK) >>761SOF_SSP_PORT_CODEC_SHIFT;762763if (ctx->amp_type != CODEC_NONE)764ctx->ssp_amp = (board_quirk & SOF_SSP_PORT_AMP_MASK) >>765SOF_SSP_PORT_AMP_SHIFT;766767if (board_quirk & SOF_BT_OFFLOAD_PRESENT) {768ctx->bt_offload_present = true;769ctx->ssp_bt = (board_quirk & SOF_SSP_PORT_BT_OFFLOAD_MASK) >>770SOF_SSP_PORT_BT_OFFLOAD_SHIFT;771}772773ctx->ssp_mask_hdmi_in = (board_quirk & SOF_SSP_MASK_HDMI_CAPTURE_MASK) >>774SOF_SSP_MASK_HDMI_CAPTURE_SHIFT;775776return ctx;777}778EXPORT_SYMBOL_NS(sof_intel_board_get_ctx, "SND_SOC_INTEL_SOF_BOARD_HELPERS");779780MODULE_DESCRIPTION("ASoC Intel SOF Machine Driver Board Helpers");781MODULE_AUTHOR("Brent Lu <[email protected]>");782MODULE_LICENSE("GPL");783MODULE_IMPORT_NS("SND_SOC_INTEL_HDA_DSP_COMMON");784MODULE_IMPORT_NS("SND_SOC_ACPI_INTEL_MATCH");785786787