xref: /linux/sound/soc/intel/avs/boards/hdaudio.c (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2021-2022 Intel Corporation
4 //
5 // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
6 //          Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
7 //
8 
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"
16 
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)
19 {
20 	struct snd_soc_dai_link_component *platform;
21 	struct snd_soc_dai_link *dl;
22 	struct hda_pcm *pcm;
23 	const char *cname = dev_name(&codec->core.dev);
24 	int i;
25 
26 	dl = devm_kcalloc(dev, pcm_count, sizeof(*dl), GFP_KERNEL);
27 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
28 	if (!dl || !platform)
29 		return -ENOMEM;
30 
31 	platform->name = platform_name;
32 	pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
33 
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);
36 		if (!dl[i].name)
37 			return -ENOMEM;
38 
39 		dl[i].id = i;
40 		dl[i].nonatomic = 1;
41 		dl[i].no_pcm = 1;
42 		dl[i].platforms = platform;
43 		dl[i].num_platforms = 1;
44 		dl[i].ignore_pmdown_time = 1;
45 
46 		dl[i].codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
47 		dl[i].cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
48 		if (!dl[i].codecs || !dl[i].cpus)
49 			return -ENOMEM;
50 
51 		dl[i].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "%s-cpu%d", cname, i);
52 		if (!dl[i].cpus->dai_name)
53 			return -ENOMEM;
54 
55 		dl[i].codecs->name = devm_kstrdup_const(dev, cname, GFP_KERNEL);
56 		if (!dl[i].codecs->name)
57 			return -ENOMEM;
58 
59 		dl[i].codecs->dai_name = pcm->name;
60 		dl[i].num_codecs = 1;
61 		dl[i].num_cpus = 1;
62 	}
63 
64 	*links = dl;
65 	return 0;
66 }
67 
68 /* Should be aligned with SectionPCM's name from topology */
69 #define FEDAI_NAME_PREFIX "HDMI"
70 
71 static struct snd_pcm *
72 avs_card_hdmi_pcm_at(struct snd_soc_card *card, int hdmi_idx)
73 {
74 	struct snd_soc_pcm_runtime *rtd;
75 	int dir = SNDRV_PCM_STREAM_PLAYBACK;
76 
77 	for_each_card_rtds(card, rtd) {
78 		struct snd_pcm *spcm;
79 		int ret, n;
80 
81 		spcm = rtd->pcm ? rtd->pcm->streams[dir].pcm : NULL;
82 		if (!spcm || !strstr(spcm->id, FEDAI_NAME_PREFIX))
83 			continue;
84 
85 		ret = sscanf(spcm->id, FEDAI_NAME_PREFIX "%d", &n);
86 		if (ret != 1)
87 			continue;
88 		if (n == hdmi_idx)
89 			return rtd->pcm;
90 	}
91 
92 	return NULL;
93 }
94 
95 static int avs_card_late_probe(struct snd_soc_card *card)
96 {
97 	struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev);
98 	struct hda_codec *codec = mach->pdata;
99 	struct hda_pcm *hpcm;
100 	/* Topology pcm indexing is 1-based */
101 	int i = 1;
102 
103 	list_for_each_entry(hpcm, &codec->pcm_list_head, list) {
104 		struct snd_pcm *spcm;
105 
106 		spcm = avs_card_hdmi_pcm_at(card, i);
107 		if (spcm) {
108 			hpcm->pcm = spcm;
109 			hpcm->device = spcm->device;
110 			dev_info(card->dev, "%s: mapping HDMI converter %d to PCM %d (%p)\n",
111 				 __func__, i, hpcm->device, spcm);
112 		} else {
113 			hpcm->pcm = NULL;
114 			hpcm->device = SNDRV_PCM_INVALID_DEVICE;
115 			dev_warn(card->dev, "%s: no PCM in topology for HDMI converter %d\n",
116 				 __func__, i);
117 		}
118 		i++;
119 	}
120 
121 	return hda_codec_probe_complete(codec);
122 }
123 
124 static int avs_probing_link_init(struct snd_soc_pcm_runtime *rtm)
125 {
126 	struct snd_soc_acpi_mach *mach;
127 	struct snd_soc_dai_link *links = NULL;
128 	struct snd_soc_card *card = rtm->card;
129 	struct hda_codec *codec;
130 	struct hda_pcm *pcm;
131 	int ret, pcm_count = 0;
132 
133 	mach = dev_get_platdata(card->dev);
134 	codec = mach->pdata;
135 
136 	if (list_empty(&codec->pcm_list_head))
137 		return -EINVAL;
138 	list_for_each_entry(pcm, &codec->pcm_list_head, list)
139 		pcm_count++;
140 
141 	ret = avs_create_dai_links(card->dev, codec, pcm_count, mach->mach_params.platform, &links);
142 	if (ret < 0) {
143 		dev_err(card->dev, "create links failed: %d\n", ret);
144 		return ret;
145 	}
146 
147 	ret = snd_soc_add_pcm_runtimes(card, links, pcm_count);
148 	if (ret < 0) {
149 		dev_err(card->dev, "add links failed: %d\n", ret);
150 		return ret;
151 	}
152 
153 	return 0;
154 }
155 
156 static const struct snd_soc_dai_link probing_link = {
157 	.name = "probing-LINK",
158 	.id = -1,
159 	.nonatomic = 1,
160 	.no_pcm = 1,
161 	.cpus = &snd_soc_dummy_dlc,
162 	.num_cpus = 1,
163 	.init = avs_probing_link_init,
164 };
165 
166 static int avs_hdaudio_probe(struct platform_device *pdev)
167 {
168 	struct snd_soc_dai_link *binder;
169 	struct snd_soc_acpi_mach *mach;
170 	struct snd_soc_card *card;
171 	struct device *dev = &pdev->dev;
172 	struct hda_codec *codec;
173 
174 	mach = dev_get_platdata(dev);
175 	codec = mach->pdata;
176 
177 	/* codec may be unloaded before card's probe() fires */
178 	if (!device_is_registered(&codec->core.dev))
179 		return -ENODEV;
180 
181 	binder = devm_kmemdup(dev, &probing_link, sizeof(probing_link), GFP_KERNEL);
182 	if (!binder)
183 		return -ENOMEM;
184 
185 	binder->platforms = devm_kzalloc(dev, sizeof(*binder->platforms), GFP_KERNEL);
186 	binder->codecs = devm_kzalloc(dev, sizeof(*binder->codecs), GFP_KERNEL);
187 	if (!binder->platforms || !binder->codecs)
188 		return -ENOMEM;
189 
190 	binder->codecs->name = devm_kstrdup_const(dev, dev_name(&codec->core.dev), GFP_KERNEL);
191 	if (!binder->codecs->name)
192 		return -ENOMEM;
193 
194 	binder->platforms->name = mach->mach_params.platform;
195 	binder->num_platforms = 1;
196 	binder->codecs->dai_name = "codec-probing-DAI";
197 	binder->num_codecs = 1;
198 
199 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
200 	if (!card)
201 		return -ENOMEM;
202 
203 	card->name = binder->codecs->name;
204 	card->dev = dev;
205 	card->owner = THIS_MODULE;
206 	card->dai_link = binder;
207 	card->num_links = 1;
208 	card->fully_routed = true;
209 	if (hda_codec_is_display(codec))
210 		card->late_probe = avs_card_late_probe;
211 
212 	return devm_snd_soc_register_card(dev, card);
213 }
214 
215 static const struct platform_device_id avs_hdaudio_driver_ids[] = {
216 	{
217 		.name = "avs_hdaudio",
218 	},
219 	{},
220 };
221 MODULE_DEVICE_TABLE(platform, avs_hdaudio_driver_ids);
222 
223 static struct platform_driver avs_hdaudio_driver = {
224 	.probe = avs_hdaudio_probe,
225 	.driver = {
226 		.name = "avs_hdaudio",
227 		.pm = &snd_soc_pm_ops,
228 	},
229 	.id_table = avs_hdaudio_driver_ids,
230 };
231 
232 module_platform_driver(avs_hdaudio_driver)
233 
234 MODULE_DESCRIPTION("Intel HD-Audio machine driver");
235 MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>");
236 MODULE_LICENSE("GPL");
237