Path: blob/master/sound/soc/amd/acp/acp-sdw-legacy-mach.c
54567 views
// SPDX-License-Identifier: GPL-2.0-only1// Copyright(c) 2024 Advanced Micro Devices, Inc.23/*4* acp-sdw-legacy-mach - ASoC legacy Machine driver for AMD SoundWire platforms5*/67#include <linux/bitmap.h>8#include <linux/device.h>9#include <linux/dmi.h>10#include <linux/module.h>11#include <linux/soundwire/sdw.h>12#include <linux/soundwire/sdw_type.h>13#include <sound/soc.h>14#include <sound/soc-acpi.h>15#include "soc_amd_sdw_common.h"16#include "../../codecs/rt711.h"1718static unsigned long soc_sdw_quirk = RT711_JD1;19static int quirk_override = -1;20module_param_named(quirk, quirk_override, int, 0444);21MODULE_PARM_DESC(quirk, "Board-specific quirk override");2223static void log_quirks(struct device *dev)24{25if (SOC_JACK_JDSRC(soc_sdw_quirk))26dev_dbg(dev, "quirk realtek,jack-detect-source %ld\n",27SOC_JACK_JDSRC(soc_sdw_quirk));28if (soc_sdw_quirk & ASOC_SDW_ACP_DMIC)29dev_dbg(dev, "quirk SOC_SDW_ACP_DMIC enabled\n");30if (soc_sdw_quirk & ASOC_SDW_CODEC_SPKR)31dev_dbg(dev, "quirk ASOC_SDW_CODEC_SPKR enabled\n");32}3334static int soc_sdw_quirk_cb(const struct dmi_system_id *id)35{36soc_sdw_quirk = (unsigned long)id->driver_data;37return 1;38}3940static const struct dmi_system_id soc_sdw_quirk_table[] = {41{42.callback = soc_sdw_quirk_cb,43.matches = {44DMI_MATCH(DMI_SYS_VENDOR, "AMD"),45DMI_MATCH(DMI_PRODUCT_NAME, "Birman-PHX"),46},47.driver_data = (void *)RT711_JD2,48},49{50.callback = soc_sdw_quirk_cb,51.matches = {52DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),53DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0D80"),54},55.driver_data = (void *)(ASOC_SDW_CODEC_SPKR),56},57{58.callback = soc_sdw_quirk_cb,59.matches = {60DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),61DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0D81"),62},63.driver_data = (void *)(ASOC_SDW_CODEC_SPKR),64},65{66.callback = soc_sdw_quirk_cb,67.matches = {68DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),69DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0D82"),70},71.driver_data = (void *)(ASOC_SDW_CODEC_SPKR),72},73{74.callback = soc_sdw_quirk_cb,75.matches = {76DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),77DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0D83"),78},79.driver_data = (void *)(ASOC_SDW_CODEC_SPKR),80},81{82.callback = soc_sdw_quirk_cb,83.matches = {84DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),85DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0DD3"),86},87.driver_data = (void *)(ASOC_SDW_CODEC_SPKR),88},89{90.callback = soc_sdw_quirk_cb,91.matches = {92DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc"),93DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "0DD4"),94},95.driver_data = (void *)(ASOC_SDW_CODEC_SPKR),96},97{98.callback = soc_sdw_quirk_cb,99.matches = {100DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),101DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "21YW"),102},103.driver_data = (void *)(ASOC_SDW_CODEC_SPKR),104},105{106.callback = soc_sdw_quirk_cb,107.matches = {108DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"),109DMI_EXACT_MATCH(DMI_PRODUCT_SKU, "21YX"),110},111.driver_data = (void *)(ASOC_SDW_CODEC_SPKR),112},113{}114};115116static const struct snd_soc_ops sdw_ops = {117.startup = asoc_sdw_startup,118.prepare = asoc_sdw_prepare,119.trigger = asoc_sdw_trigger,120.hw_params = asoc_sdw_hw_params,121.hw_free = asoc_sdw_hw_free,122.shutdown = asoc_sdw_shutdown,123};124125static const char * const type_strings[] = {"SimpleJack", "SmartAmp", "SmartMic"};126127static int create_sdw_dailink(struct snd_soc_card *card,128struct asoc_sdw_dailink *soc_dai,129struct snd_soc_dai_link **dai_links,130int *be_id, struct snd_soc_codec_conf **codec_conf,131struct snd_soc_dai_link_component *sdw_platform_component)132{133struct device *dev = card->dev;134struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card);135struct amd_mc_ctx *amd_ctx = (struct amd_mc_ctx *)ctx->private;136struct asoc_sdw_endpoint *soc_end;137int cpu_pin_id;138int stream;139int ret;140141list_for_each_entry(soc_end, &soc_dai->endpoints, list) {142if (soc_end->name_prefix) {143(*codec_conf)->dlc.name = soc_end->codec_name;144(*codec_conf)->name_prefix = soc_end->name_prefix;145(*codec_conf)++;146}147148if (soc_end->include_sidecar) {149ret = soc_end->codec_info->add_sidecar(card, dai_links, codec_conf);150if (ret)151return ret;152}153}154155for_each_pcm_streams(stream) {156static const char * const sdw_stream_name[] = {157"SDW%d-PIN%d-PLAYBACK",158"SDW%d-PIN%d-CAPTURE",159"SDW%d-PIN%d-PLAYBACK-%s",160"SDW%d-PIN%d-CAPTURE-%s",161};162struct snd_soc_dai_link_ch_map *codec_maps;163struct snd_soc_dai_link_component *codecs;164struct snd_soc_dai_link_component *cpus;165int num_cpus = hweight32(soc_dai->link_mask[stream]);166int num_codecs = soc_dai->num_devs[stream];167int playback, capture;168int j = 0;169char *name;170171if (!soc_dai->num_devs[stream])172continue;173174soc_end = list_first_entry(&soc_dai->endpoints,175struct asoc_sdw_endpoint, list);176177*be_id = soc_end->dai_info->dailink[stream];178if (*be_id < 0) {179dev_err(dev, "Invalid dailink id %d\n", *be_id);180return -EINVAL;181}182183switch (amd_ctx->acp_rev) {184case ACP63_PCI_REV:185ret = get_acp63_cpu_pin_id(ffs(soc_end->link_mask - 1),186*be_id, &cpu_pin_id, dev);187if (ret)188return ret;189break;190case ACP70_PCI_REV:191case ACP71_PCI_REV:192case ACP72_PCI_REV:193ret = get_acp70_cpu_pin_id(ffs(soc_end->link_mask - 1),194*be_id, &cpu_pin_id, dev);195if (ret)196return ret;197break;198default:199return -EINVAL;200}201/* create stream name according to first link id */202if (ctx->append_dai_type) {203name = devm_kasprintf(dev, GFP_KERNEL,204sdw_stream_name[stream + 2],205ffs(soc_end->link_mask) - 1,206cpu_pin_id,207type_strings[soc_end->dai_info->dai_type]);208} else {209name = devm_kasprintf(dev, GFP_KERNEL,210sdw_stream_name[stream],211ffs(soc_end->link_mask) - 1,212cpu_pin_id);213}214if (!name)215return -ENOMEM;216217cpus = devm_kcalloc(dev, num_cpus, sizeof(*cpus), GFP_KERNEL);218if (!cpus)219return -ENOMEM;220221codecs = devm_kcalloc(dev, num_codecs, sizeof(*codecs), GFP_KERNEL);222if (!codecs)223return -ENOMEM;224225codec_maps = devm_kcalloc(dev, num_codecs, sizeof(*codec_maps), GFP_KERNEL);226if (!codec_maps)227return -ENOMEM;228229list_for_each_entry(soc_end, &soc_dai->endpoints, list) {230if (!soc_end->dai_info->direction[stream])231continue;232233int link_num = ffs(soc_end->link_mask) - 1;234235cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,236"SDW%d Pin%d",237link_num, cpu_pin_id);238dev_dbg(dev, "cpu->dai_name:%s\n", cpus->dai_name);239if (!cpus->dai_name)240return -ENOMEM;241242codec_maps[j].cpu = 0;243codec_maps[j].codec = j;244245codecs[j].name = soc_end->codec_name;246codecs[j].dai_name = soc_end->dai_info->dai_name;247j++;248}249250WARN_ON(j != num_codecs);251252playback = (stream == SNDRV_PCM_STREAM_PLAYBACK);253capture = (stream == SNDRV_PCM_STREAM_CAPTURE);254255asoc_sdw_init_dai_link(dev, *dai_links, be_id, name, playback, capture,256cpus, num_cpus, sdw_platform_component,2571, codecs, num_codecs,2580, asoc_sdw_rtd_init, &sdw_ops);259/*260* SoundWire DAILINKs use 'stream' functions and Bank Switch operations261* based on wait_for_completion(), tag them as 'nonatomic'.262*/263(*dai_links)->nonatomic = true;264(*dai_links)->ch_maps = codec_maps;265266list_for_each_entry(soc_end, &soc_dai->endpoints, list) {267if (soc_end->dai_info->init)268soc_end->dai_info->init(card, *dai_links,269soc_end->codec_info,270playback);271}272273(*dai_links)++;274}275276return 0;277}278279static int create_sdw_dailinks(struct snd_soc_card *card,280struct snd_soc_dai_link **dai_links, int *be_id,281struct asoc_sdw_dailink *soc_dais,282struct snd_soc_codec_conf **codec_conf)283{284struct device *dev = card->dev;285struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card);286struct amd_mc_ctx *amd_ctx = (struct amd_mc_ctx *)ctx->private;287struct snd_soc_dai_link_component *sdw_platform_component;288int ret;289290sdw_platform_component = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component),291GFP_KERNEL);292if (!sdw_platform_component)293return -ENOMEM;294295switch (amd_ctx->acp_rev) {296case ACP63_PCI_REV:297case ACP70_PCI_REV:298case ACP71_PCI_REV:299case ACP72_PCI_REV:300sdw_platform_component->name = "amd_ps_sdw_dma.0";301break;302default:303return -EINVAL;304}305306/* generate DAI links by each sdw link */307while (soc_dais->initialised) {308int current_be_id = 0;309310ret = create_sdw_dailink(card, soc_dais, dai_links,311¤t_be_id, codec_conf, sdw_platform_component);312if (ret)313return ret;314315/* Update the be_id to match the highest ID used for SDW link */316if (*be_id < current_be_id)317*be_id = current_be_id;318319soc_dais++;320}321322return 0;323}324325static int create_dmic_dailinks(struct snd_soc_card *card,326struct snd_soc_dai_link **dai_links, int *be_id, int no_pcm)327{328struct device *dev = card->dev;329struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card);330struct amd_mc_ctx *amd_ctx = (struct amd_mc_ctx *)ctx->private;331struct snd_soc_dai_link_component *pdm_cpu;332struct snd_soc_dai_link_component *pdm_platform;333int ret;334335pdm_cpu = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);336if (!pdm_cpu)337return -ENOMEM;338339pdm_platform = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);340if (!pdm_platform)341return -ENOMEM;342343switch (amd_ctx->acp_rev) {344case ACP63_PCI_REV:345case ACP70_PCI_REV:346case ACP71_PCI_REV:347case ACP72_PCI_REV:348pdm_cpu->name = "acp_ps_pdm_dma.0";349pdm_platform->name = "acp_ps_pdm_dma.0";350break;351default:352return -EINVAL;353}354355*be_id = ACP_DMIC_BE_ID;356ret = asoc_sdw_init_simple_dai_link(dev, *dai_links, be_id, "acp-dmic-codec",3570, 1, // DMIC only supports capture358pdm_cpu->name, pdm_platform->name,359"dmic-codec.0", "dmic-hifi", no_pcm,360asoc_sdw_dmic_init, NULL);361if (ret)362return ret;363364(*dai_links)++;365366return 0;367}368369static int soc_card_dai_links_create(struct snd_soc_card *card)370{371struct device *dev = card->dev;372struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev);373int sdw_be_num = 0, dmic_num = 0;374struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card);375struct snd_soc_acpi_mach_params *mach_params = &mach->mach_params;376struct snd_soc_aux_dev *soc_aux;377struct snd_soc_codec_conf *codec_conf;378struct snd_soc_dai_link *dai_links;379int num_devs = 0;380int num_ends = 0;381int num_aux = 0;382int num_confs;383int num_links;384int be_id = 0;385int ret;386387ret = asoc_sdw_count_sdw_endpoints(card, &num_devs, &num_ends, &num_aux);388if (ret < 0) {389dev_err(dev, "failed to count devices/endpoints: %d\n", ret);390return ret;391}392393num_confs = num_ends;394395/* One per DAI link, worst case is a DAI link for every endpoint */396struct asoc_sdw_dailink *soc_dais __free(kfree) =397kcalloc(num_ends, sizeof(*soc_dais), GFP_KERNEL);398if (!soc_dais)399return -ENOMEM;400401/* One per endpoint, ie. each DAI on each codec/amp */402struct asoc_sdw_endpoint *soc_ends __free(kfree) =403kcalloc(num_ends, sizeof(*soc_ends), GFP_KERNEL);404if (!soc_ends)405return -ENOMEM;406407soc_aux = devm_kcalloc(dev, num_aux, sizeof(*soc_aux), GFP_KERNEL);408if (!soc_aux)409return -ENOMEM;410411ret = asoc_sdw_parse_sdw_endpoints(card, soc_aux, soc_dais, soc_ends, &num_confs);412if (ret < 0)413return ret;414415sdw_be_num = ret;416417/* enable dmic */418if (soc_sdw_quirk & ASOC_SDW_ACP_DMIC || mach_params->dmic_num)419dmic_num = 1;420421dev_dbg(dev, "sdw %d, dmic %d", sdw_be_num, dmic_num);422423codec_conf = devm_kcalloc(dev, num_confs, sizeof(*codec_conf), GFP_KERNEL);424if (!codec_conf)425return -ENOMEM;426427/* allocate BE dailinks */428num_links = sdw_be_num + dmic_num;429dai_links = devm_kcalloc(dev, num_links, sizeof(*dai_links), GFP_KERNEL);430if (!dai_links)431return -ENOMEM;432433card->codec_conf = codec_conf;434card->num_configs = num_confs;435card->dai_link = dai_links;436card->num_links = num_links;437card->aux_dev = soc_aux;438card->num_aux_devs = num_aux;439440/* SDW */441if (sdw_be_num) {442ret = create_sdw_dailinks(card, &dai_links, &be_id,443soc_dais, &codec_conf);444if (ret)445return ret;446}447448/* dmic */449if (dmic_num > 0) {450if (ctx->ignore_internal_dmic) {451dev_warn(dev, "Ignoring ACP DMIC\n");452} else {453ret = create_dmic_dailinks(card, &dai_links, &be_id, 0);454if (ret)455return ret;456}457}458459WARN_ON(codec_conf != card->codec_conf + card->num_configs);460WARN_ON(dai_links != card->dai_link + card->num_links);461462return ret;463}464465static int mc_probe(struct platform_device *pdev)466{467struct snd_soc_acpi_mach *mach = dev_get_platdata(&pdev->dev);468struct snd_soc_card *card;469struct amd_mc_ctx *amd_ctx;470struct asoc_sdw_mc_private *ctx;471int amp_num = 0, i;472int ret;473474amd_ctx = devm_kzalloc(&pdev->dev, sizeof(*amd_ctx), GFP_KERNEL);475if (!amd_ctx)476return -ENOMEM;477478amd_ctx->acp_rev = mach->mach_params.subsystem_rev;479amd_ctx->max_sdw_links = ACP63_SDW_MAX_LINKS;480ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);481if (!ctx)482return -ENOMEM;483ctx->codec_info_list_count = asoc_sdw_get_codec_info_list_count();484ctx->private = amd_ctx;485card = &ctx->card;486card->dev = &pdev->dev;487card->name = "amd-soundwire";488card->owner = THIS_MODULE;489card->late_probe = asoc_sdw_card_late_probe;490491snd_soc_card_set_drvdata(card, ctx);492if (mach->mach_params.subsystem_id_set)493snd_soc_card_set_pci_ssid(card,494mach->mach_params.subsystem_vendor,495mach->mach_params.subsystem_device);496497dmi_check_system(soc_sdw_quirk_table);498499if (quirk_override != -1) {500dev_info(card->dev, "Overriding quirk 0x%lx => 0x%x\n",501soc_sdw_quirk, quirk_override);502soc_sdw_quirk = quirk_override;503}504505log_quirks(card->dev);506507ctx->mc_quirk = soc_sdw_quirk;508dev_dbg(card->dev, "legacy quirk 0x%lx\n", ctx->mc_quirk);509/* reset amp_num to ensure amp_num++ starts from 0 in each probe */510for (i = 0; i < ctx->codec_info_list_count; i++)511codec_info_list[i].amp_num = 0;512513ret = soc_card_dai_links_create(card);514if (ret < 0)515return ret;516517/*518* the default amp_num is zero for each codec and519* amp_num will only be increased for active amp520* codecs on used platform521*/522for (i = 0; i < ctx->codec_info_list_count; i++)523amp_num += codec_info_list[i].amp_num;524525card->components = devm_kasprintf(card->dev, GFP_KERNEL,526" cfg-amp:%d", amp_num);527if (!card->components)528return -ENOMEM;529if (mach->mach_params.dmic_num) {530card->components = devm_kasprintf(card->dev, GFP_KERNEL,531"%s mic:dmic cfg-mics:%d",532card->components,533mach->mach_params.dmic_num);534if (!card->components)535return -ENOMEM;536}537538/* Register the card */539ret = devm_snd_soc_register_card(card->dev, card);540if (ret) {541dev_err_probe(card->dev, ret, "snd_soc_register_card failed %d\n", ret);542asoc_sdw_mc_dailink_exit_loop(card);543return ret;544}545546platform_set_drvdata(pdev, card);547548return ret;549}550551static void mc_remove(struct platform_device *pdev)552{553struct snd_soc_card *card = platform_get_drvdata(pdev);554555asoc_sdw_mc_dailink_exit_loop(card);556}557558static const struct platform_device_id mc_id_table[] = {559{ "amd_sdw", },560{}561};562MODULE_DEVICE_TABLE(platform, mc_id_table);563564static struct platform_driver soc_sdw_driver = {565.driver = {566.name = "amd_sdw",567.pm = &snd_soc_pm_ops,568},569.probe = mc_probe,570.remove = mc_remove,571.id_table = mc_id_table,572};573574module_platform_driver(soc_sdw_driver);575576MODULE_DESCRIPTION("ASoC AMD SoundWire Legacy Generic Machine driver");577MODULE_AUTHOR("Vijendar Mukunda <[email protected]>");578MODULE_LICENSE("GPL");579MODULE_IMPORT_NS("SND_SOC_SDW_UTILS");580MODULE_IMPORT_NS("SND_SOC_AMD_SDW_MACH");581582583