1 // SPDX-License-Identifier: GPL-2.0-only
3 // Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
5 // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
6 // Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <sound/hda_codec.h>
12 #include <sound/hda_i915.h>
13 #include <sound/soc.h>
14 #include <sound/soc-acpi.h>
15 #include "../../../codecs/hda.h"
17 static int avs_create_dai_links(struct device *dev, struct hda_codec *codec, int pcm_count,
18 const char *platform_name, struct snd_soc_dai_link **links)
20 struct snd_soc_dai_link_component *platform;
21 struct snd_soc_dai_link *dl;
23 const char *cname = dev_name(&codec->core.dev);
26 dl = devm_kcalloc(dev, pcm_count, sizeof(*dl), GFP_KERNEL);
27 platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
31 platform->name = platform_name;
32 pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
34 for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
35 dl[i].name = devm_kasprintf(dev, GFP_KERNEL, "%s link%d", cname, i);
42 dl[i].dpcm_playback = 1;
43 dl[i].dpcm_capture = 1;
44 dl[i].platforms = platform;
45 dl[i].num_platforms = 1;
46 dl[i].ignore_pmdown_time = 1;
48 dl[i].codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
49 dl[i].cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
50 if (!dl[i].codecs || !dl[i].cpus)
53 dl[i].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "%s-cpu%d", cname, i);
54 if (!dl[i].cpus->dai_name)
57 dl[i].codecs->name = devm_kstrdup(dev, cname, GFP_KERNEL);
58 dl[i].codecs->dai_name = pcm->name;
67 /* Should be aligned with SectionPCM's name from topology */
68 #define FEDAI_NAME_PREFIX "HDMI"
70 static struct snd_pcm *
71 avs_card_hdmi_pcm_at(struct snd_soc_card *card, int hdmi_idx)
73 struct snd_soc_pcm_runtime *rtd;
74 int dir = SNDRV_PCM_STREAM_PLAYBACK;
76 for_each_card_rtds(card, rtd) {
80 spcm = rtd->pcm ? rtd->pcm->streams[dir].pcm : NULL;
81 if (!spcm || !strstr(spcm->id, FEDAI_NAME_PREFIX))
84 ret = sscanf(spcm->id, FEDAI_NAME_PREFIX "%d", &n);
94 static int avs_card_late_probe(struct snd_soc_card *card)
96 struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev);
97 struct hda_codec *codec = mach->pdata;
99 /* Topology pcm indexing is 1-based */
102 list_for_each_entry(hpcm, &codec->pcm_list_head, list) {
103 struct snd_pcm *spcm;
105 spcm = avs_card_hdmi_pcm_at(card, i);
108 hpcm->device = spcm->device;
109 dev_info(card->dev, "%s: mapping HDMI converter %d to PCM %d (%p)\n",
110 __func__, i, hpcm->device, spcm);
113 hpcm->device = SNDRV_PCM_INVALID_DEVICE;
114 dev_warn(card->dev, "%s: no PCM in topology for HDMI converter %d\n",
120 return hda_codec_probe_complete(codec);
123 static int avs_probing_link_init(struct snd_soc_pcm_runtime *rtm)
125 struct snd_soc_acpi_mach *mach;
126 struct snd_soc_dai_link *links = NULL;
127 struct snd_soc_card *card = rtm->card;
128 struct hda_codec *codec;
130 int ret, pcm_count = 0;
132 mach = dev_get_platdata(card->dev);
135 if (list_empty(&codec->pcm_list_head))
137 list_for_each_entry(pcm, &codec->pcm_list_head, list)
140 ret = avs_create_dai_links(card->dev, codec, pcm_count, mach->mach_params.platform, &links);
142 dev_err(card->dev, "create links failed: %d\n", ret);
146 ret = snd_soc_add_pcm_runtimes(card, links, pcm_count);
148 dev_err(card->dev, "add links failed: %d\n", ret);
155 SND_SOC_DAILINK_DEF(dummy, DAILINK_COMP_ARRAY(COMP_DUMMY()));
157 static struct snd_soc_dai_link probing_link = {
158 .name = "probing-LINK",
165 .num_cpus = ARRAY_SIZE(dummy),
166 .init = avs_probing_link_init,
169 static int avs_hdaudio_probe(struct platform_device *pdev)
171 struct snd_soc_dai_link *binder;
172 struct snd_soc_acpi_mach *mach;
173 struct snd_soc_card *card;
174 struct device *dev = &pdev->dev;
175 struct hda_codec *codec;
177 mach = dev_get_platdata(dev);
180 /* codec may be unloaded before card's probe() fires */
181 if (!device_is_registered(&codec->core.dev))
184 binder = devm_kmemdup(dev, &probing_link, sizeof(probing_link), GFP_KERNEL);
188 binder->platforms = devm_kzalloc(dev, sizeof(*binder->platforms), GFP_KERNEL);
189 binder->codecs = devm_kzalloc(dev, sizeof(*binder->codecs), GFP_KERNEL);
190 if (!binder->platforms || !binder->codecs)
193 binder->codecs->name = devm_kstrdup(dev, dev_name(&codec->core.dev), GFP_KERNEL);
194 if (!binder->codecs->name)
197 binder->platforms->name = mach->mach_params.platform;
198 binder->num_platforms = 1;
199 binder->codecs->dai_name = "codec-probing-DAI";
200 binder->num_codecs = 1;
202 card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
206 card->name = binder->codecs->name;
208 card->owner = THIS_MODULE;
209 card->dai_link = binder;
211 card->fully_routed = true;
212 if (hda_codec_is_display(codec))
213 card->late_probe = avs_card_late_probe;
215 return devm_snd_soc_register_card(dev, card);
218 static struct platform_driver avs_hdaudio_driver = {
219 .probe = avs_hdaudio_probe,
221 .name = "avs_hdaudio",
222 .pm = &snd_soc_pm_ops,
226 module_platform_driver(avs_hdaudio_driver)
228 MODULE_DESCRIPTION("Intel HD-Audio machine driver");
229 MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>");
230 MODULE_LICENSE("GPL");
231 MODULE_ALIAS("platform:avs_hdaudio");