Path: blob/master/sound/soc/amd/acp/acp-sdw-legacy-mach.c
26481 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};8384static const struct snd_soc_ops sdw_ops = {85.startup = asoc_sdw_startup,86.prepare = asoc_sdw_prepare,87.trigger = asoc_sdw_trigger,88.hw_params = asoc_sdw_hw_params,89.hw_free = asoc_sdw_hw_free,90.shutdown = asoc_sdw_shutdown,91};9293static const char * const type_strings[] = {"SimpleJack", "SmartAmp", "SmartMic"};9495static int create_sdw_dailink(struct snd_soc_card *card,96struct asoc_sdw_dailink *soc_dai,97struct snd_soc_dai_link **dai_links,98int *be_id, struct snd_soc_codec_conf **codec_conf,99struct snd_soc_dai_link_component *sdw_platform_component)100{101struct device *dev = card->dev;102struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card);103struct amd_mc_ctx *amd_ctx = (struct amd_mc_ctx *)ctx->private;104struct asoc_sdw_endpoint *soc_end;105int cpu_pin_id;106int stream;107int ret;108109list_for_each_entry(soc_end, &soc_dai->endpoints, list) {110if (soc_end->name_prefix) {111(*codec_conf)->dlc.name = soc_end->codec_name;112(*codec_conf)->name_prefix = soc_end->name_prefix;113(*codec_conf)++;114}115116if (soc_end->include_sidecar) {117ret = soc_end->codec_info->add_sidecar(card, dai_links, codec_conf);118if (ret)119return ret;120}121}122123for_each_pcm_streams(stream) {124static const char * const sdw_stream_name[] = {125"SDW%d-PIN%d-PLAYBACK",126"SDW%d-PIN%d-CAPTURE",127"SDW%d-PIN%d-PLAYBACK-%s",128"SDW%d-PIN%d-CAPTURE-%s",129};130struct snd_soc_dai_link_ch_map *codec_maps;131struct snd_soc_dai_link_component *codecs;132struct snd_soc_dai_link_component *cpus;133int num_cpus = hweight32(soc_dai->link_mask[stream]);134int num_codecs = soc_dai->num_devs[stream];135int playback, capture;136int j = 0;137char *name;138139if (!soc_dai->num_devs[stream])140continue;141142soc_end = list_first_entry(&soc_dai->endpoints,143struct asoc_sdw_endpoint, list);144145*be_id = soc_end->dai_info->dailink[stream];146if (*be_id < 0) {147dev_err(dev, "Invalid dailink id %d\n", *be_id);148return -EINVAL;149}150151switch (amd_ctx->acp_rev) {152case ACP63_PCI_REV:153ret = get_acp63_cpu_pin_id(ffs(soc_end->link_mask - 1),154*be_id, &cpu_pin_id, dev);155if (ret)156return ret;157break;158case ACP70_PCI_REV:159case ACP71_PCI_REV:160case ACP72_PCI_REV:161ret = get_acp70_cpu_pin_id(ffs(soc_end->link_mask - 1),162*be_id, &cpu_pin_id, dev);163if (ret)164return ret;165break;166default:167return -EINVAL;168}169/* create stream name according to first link id */170if (ctx->append_dai_type) {171name = devm_kasprintf(dev, GFP_KERNEL,172sdw_stream_name[stream + 2],173ffs(soc_end->link_mask) - 1,174cpu_pin_id,175type_strings[soc_end->dai_info->dai_type]);176} else {177name = devm_kasprintf(dev, GFP_KERNEL,178sdw_stream_name[stream],179ffs(soc_end->link_mask) - 1,180cpu_pin_id);181}182if (!name)183return -ENOMEM;184185cpus = devm_kcalloc(dev, num_cpus, sizeof(*cpus), GFP_KERNEL);186if (!cpus)187return -ENOMEM;188189codecs = devm_kcalloc(dev, num_codecs, sizeof(*codecs), GFP_KERNEL);190if (!codecs)191return -ENOMEM;192193codec_maps = devm_kcalloc(dev, num_codecs, sizeof(*codec_maps), GFP_KERNEL);194if (!codec_maps)195return -ENOMEM;196197list_for_each_entry(soc_end, &soc_dai->endpoints, list) {198if (!soc_end->dai_info->direction[stream])199continue;200201int link_num = ffs(soc_end->link_mask) - 1;202203cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,204"SDW%d Pin%d",205link_num, cpu_pin_id);206dev_dbg(dev, "cpu->dai_name:%s\n", cpus->dai_name);207if (!cpus->dai_name)208return -ENOMEM;209210codec_maps[j].cpu = 0;211codec_maps[j].codec = j;212213codecs[j].name = soc_end->codec_name;214codecs[j].dai_name = soc_end->dai_info->dai_name;215j++;216}217218WARN_ON(j != num_codecs);219220playback = (stream == SNDRV_PCM_STREAM_PLAYBACK);221capture = (stream == SNDRV_PCM_STREAM_CAPTURE);222223asoc_sdw_init_dai_link(dev, *dai_links, be_id, name, playback, capture,224cpus, num_cpus, sdw_platform_component,2251, codecs, num_codecs,2260, asoc_sdw_rtd_init, &sdw_ops);227/*228* SoundWire DAILINKs use 'stream' functions and Bank Switch operations229* based on wait_for_completion(), tag them as 'nonatomic'.230*/231(*dai_links)->nonatomic = true;232(*dai_links)->ch_maps = codec_maps;233234list_for_each_entry(soc_end, &soc_dai->endpoints, list) {235if (soc_end->dai_info->init)236soc_end->dai_info->init(card, *dai_links,237soc_end->codec_info,238playback);239}240241(*dai_links)++;242}243244return 0;245}246247static int create_sdw_dailinks(struct snd_soc_card *card,248struct snd_soc_dai_link **dai_links, int *be_id,249struct asoc_sdw_dailink *soc_dais,250struct snd_soc_codec_conf **codec_conf)251{252struct device *dev = card->dev;253struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card);254struct amd_mc_ctx *amd_ctx = (struct amd_mc_ctx *)ctx->private;255struct snd_soc_dai_link_component *sdw_platform_component;256int ret;257258sdw_platform_component = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component),259GFP_KERNEL);260if (!sdw_platform_component)261return -ENOMEM;262263switch (amd_ctx->acp_rev) {264case ACP63_PCI_REV:265case ACP70_PCI_REV:266case ACP71_PCI_REV:267case ACP72_PCI_REV:268sdw_platform_component->name = "amd_ps_sdw_dma.0";269break;270default:271return -EINVAL;272}273274/* generate DAI links by each sdw link */275while (soc_dais->initialised) {276int current_be_id = 0;277278ret = create_sdw_dailink(card, soc_dais, dai_links,279¤t_be_id, codec_conf, sdw_platform_component);280if (ret)281return ret;282283/* Update the be_id to match the highest ID used for SDW link */284if (*be_id < current_be_id)285*be_id = current_be_id;286287soc_dais++;288}289290return 0;291}292293static int create_dmic_dailinks(struct snd_soc_card *card,294struct snd_soc_dai_link **dai_links, int *be_id, int no_pcm)295{296struct device *dev = card->dev;297struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card);298struct amd_mc_ctx *amd_ctx = (struct amd_mc_ctx *)ctx->private;299struct snd_soc_dai_link_component *pdm_cpu;300struct snd_soc_dai_link_component *pdm_platform;301int ret;302303pdm_cpu = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);304if (!pdm_cpu)305return -ENOMEM;306307pdm_platform = devm_kzalloc(dev, sizeof(struct snd_soc_dai_link_component), GFP_KERNEL);308if (!pdm_platform)309return -ENOMEM;310311switch (amd_ctx->acp_rev) {312case ACP63_PCI_REV:313case ACP70_PCI_REV:314case ACP71_PCI_REV:315case ACP72_PCI_REV:316pdm_cpu->name = "acp_ps_pdm_dma.0";317pdm_platform->name = "acp_ps_pdm_dma.0";318break;319default:320return -EINVAL;321}322323*be_id = ACP_DMIC_BE_ID;324ret = asoc_sdw_init_simple_dai_link(dev, *dai_links, be_id, "acp-dmic-codec",3250, 1, // DMIC only supports capture326pdm_cpu->name, pdm_platform->name,327"dmic-codec.0", "dmic-hifi", no_pcm,328asoc_sdw_dmic_init, NULL);329if (ret)330return ret;331332(*dai_links)++;333334return 0;335}336337static int soc_card_dai_links_create(struct snd_soc_card *card)338{339struct device *dev = card->dev;340struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev);341int sdw_be_num = 0, dmic_num = 0;342struct asoc_sdw_mc_private *ctx = snd_soc_card_get_drvdata(card);343struct snd_soc_acpi_mach_params *mach_params = &mach->mach_params;344struct asoc_sdw_endpoint *soc_ends __free(kfree) = NULL;345struct asoc_sdw_dailink *soc_dais __free(kfree) = NULL;346struct snd_soc_codec_conf *codec_conf;347struct snd_soc_dai_link *dai_links;348int num_devs = 0;349int num_ends = 0;350int num_links;351int be_id = 0;352int ret;353354ret = asoc_sdw_count_sdw_endpoints(card, &num_devs, &num_ends);355if (ret < 0) {356dev_err(dev, "failed to count devices/endpoints: %d\n", ret);357return ret;358}359360/* One per DAI link, worst case is a DAI link for every endpoint */361soc_dais = kcalloc(num_ends, sizeof(*soc_dais), GFP_KERNEL);362if (!soc_dais)363return -ENOMEM;364365/* One per endpoint, ie. each DAI on each codec/amp */366soc_ends = kcalloc(num_ends, sizeof(*soc_ends), GFP_KERNEL);367if (!soc_ends)368return -ENOMEM;369370ret = asoc_sdw_parse_sdw_endpoints(card, soc_dais, soc_ends, &num_devs);371if (ret < 0)372return ret;373374sdw_be_num = ret;375376/* enable dmic */377if (soc_sdw_quirk & ASOC_SDW_ACP_DMIC || mach_params->dmic_num)378dmic_num = 1;379380dev_dbg(dev, "sdw %d, dmic %d", sdw_be_num, dmic_num);381382codec_conf = devm_kcalloc(dev, num_devs, sizeof(*codec_conf), GFP_KERNEL);383if (!codec_conf)384return -ENOMEM;385386/* allocate BE dailinks */387num_links = sdw_be_num + dmic_num;388dai_links = devm_kcalloc(dev, num_links, sizeof(*dai_links), GFP_KERNEL);389if (!dai_links)390return -ENOMEM;391392card->codec_conf = codec_conf;393card->num_configs = num_devs;394card->dai_link = dai_links;395card->num_links = num_links;396397/* SDW */398if (sdw_be_num) {399ret = create_sdw_dailinks(card, &dai_links, &be_id,400soc_dais, &codec_conf);401if (ret)402return ret;403}404405/* dmic */406if (dmic_num > 0) {407if (ctx->ignore_internal_dmic) {408dev_warn(dev, "Ignoring ACP DMIC\n");409} else {410ret = create_dmic_dailinks(card, &dai_links, &be_id, 0);411if (ret)412return ret;413}414}415416WARN_ON(codec_conf != card->codec_conf + card->num_configs);417WARN_ON(dai_links != card->dai_link + card->num_links);418419return ret;420}421422static int mc_probe(struct platform_device *pdev)423{424struct snd_soc_acpi_mach *mach = dev_get_platdata(&pdev->dev);425struct snd_soc_card *card;426struct amd_mc_ctx *amd_ctx;427struct asoc_sdw_mc_private *ctx;428int amp_num = 0, i;429int ret;430431amd_ctx = devm_kzalloc(&pdev->dev, sizeof(*amd_ctx), GFP_KERNEL);432if (!amd_ctx)433return -ENOMEM;434435amd_ctx->acp_rev = mach->mach_params.subsystem_rev;436amd_ctx->max_sdw_links = ACP63_SDW_MAX_LINKS;437ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);438if (!ctx)439return -ENOMEM;440ctx->codec_info_list_count = asoc_sdw_get_codec_info_list_count();441ctx->private = amd_ctx;442card = &ctx->card;443card->dev = &pdev->dev;444card->name = "amd-soundwire";445card->owner = THIS_MODULE;446card->late_probe = asoc_sdw_card_late_probe;447448snd_soc_card_set_drvdata(card, ctx);449450dmi_check_system(soc_sdw_quirk_table);451452if (quirk_override != -1) {453dev_info(card->dev, "Overriding quirk 0x%lx => 0x%x\n",454soc_sdw_quirk, quirk_override);455soc_sdw_quirk = quirk_override;456}457458log_quirks(card->dev);459460ctx->mc_quirk = soc_sdw_quirk;461dev_dbg(card->dev, "legacy quirk 0x%lx\n", ctx->mc_quirk);462/* reset amp_num to ensure amp_num++ starts from 0 in each probe */463for (i = 0; i < ctx->codec_info_list_count; i++)464codec_info_list[i].amp_num = 0;465466ret = soc_card_dai_links_create(card);467if (ret < 0)468return ret;469470/*471* the default amp_num is zero for each codec and472* amp_num will only be increased for active amp473* codecs on used platform474*/475for (i = 0; i < ctx->codec_info_list_count; i++)476amp_num += codec_info_list[i].amp_num;477478card->components = devm_kasprintf(card->dev, GFP_KERNEL,479" cfg-amp:%d", amp_num);480if (!card->components)481return -ENOMEM;482if (mach->mach_params.dmic_num) {483card->components = devm_kasprintf(card->dev, GFP_KERNEL,484"%s mic:dmic cfg-mics:%d",485card->components,486mach->mach_params.dmic_num);487if (!card->components)488return -ENOMEM;489}490491/* Register the card */492ret = devm_snd_soc_register_card(card->dev, card);493if (ret) {494dev_err_probe(card->dev, ret, "snd_soc_register_card failed %d\n", ret);495asoc_sdw_mc_dailink_exit_loop(card);496return ret;497}498499platform_set_drvdata(pdev, card);500501return ret;502}503504static void mc_remove(struct platform_device *pdev)505{506struct snd_soc_card *card = platform_get_drvdata(pdev);507508asoc_sdw_mc_dailink_exit_loop(card);509}510511static const struct platform_device_id mc_id_table[] = {512{ "amd_sdw", },513{}514};515MODULE_DEVICE_TABLE(platform, mc_id_table);516517static struct platform_driver soc_sdw_driver = {518.driver = {519.name = "amd_sdw",520.pm = &snd_soc_pm_ops,521},522.probe = mc_probe,523.remove = mc_remove,524.id_table = mc_id_table,525};526527module_platform_driver(soc_sdw_driver);528529MODULE_DESCRIPTION("ASoC AMD SoundWire Legacy Generic Machine driver");530MODULE_AUTHOR("Vijendar Mukunda <[email protected]>");531MODULE_LICENSE("GPL");532MODULE_IMPORT_NS("SND_SOC_SDW_UTILS");533MODULE_IMPORT_NS("SND_SOC_AMD_SDW_MACH");534535536