xref: /openbmc/linux/sound/soc/intel/avs/boards/hdaudio.c (revision 54a611b6)
1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
4 //
5 // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
6 //          Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
7 //
8 
9 #include <linux/platform_device.h>
10 #include <sound/hda_codec.h>
11 #include <sound/hda_i915.h>
12 #include <sound/soc.h>
13 #include <sound/soc-acpi.h>
14 #include "../../../codecs/hda.h"
15 
16 static int avs_create_dai_links(struct device *dev, struct hda_codec *codec, int pcm_count,
17 				const char *platform_name, struct snd_soc_dai_link **links)
18 {
19 	struct snd_soc_dai_link_component *platform;
20 	struct snd_soc_dai_link *dl;
21 	struct hda_pcm *pcm;
22 	const char *cname = dev_name(&codec->core.dev);
23 	int i;
24 
25 	dl = devm_kcalloc(dev, pcm_count, sizeof(*dl), GFP_KERNEL);
26 	platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
27 	if (!dl || !platform)
28 		return -ENOMEM;
29 
30 	platform->name = platform_name;
31 	pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
32 
33 	for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
34 		dl[i].name = devm_kasprintf(dev, GFP_KERNEL, "%s link%d", cname, i);
35 		if (!dl[i].name)
36 			return -ENOMEM;
37 
38 		dl[i].id = i;
39 		dl[i].nonatomic = 1;
40 		dl[i].no_pcm = 1;
41 		dl[i].dpcm_playback = 1;
42 		dl[i].dpcm_capture = 1;
43 		dl[i].platforms = platform;
44 		dl[i].num_platforms = 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(dev, cname, GFP_KERNEL);
56 		dl[i].codecs->dai_name = pcm->name;
57 		dl[i].num_codecs = 1;
58 		dl[i].num_cpus = 1;
59 	}
60 
61 	*links = dl;
62 	return 0;
63 }
64 
65 static int avs_create_dapm_routes(struct device *dev, struct hda_codec *codec, int pcm_count,
66 				  struct snd_soc_dapm_route **routes, int *num_routes)
67 {
68 	struct snd_soc_dapm_route *dr;
69 	struct hda_pcm *pcm;
70 	const char *cname = dev_name(&codec->core.dev);
71 	int i, n = 0;
72 
73 	/* at max twice the number of pcms */
74 	dr = devm_kcalloc(dev, pcm_count * 2, sizeof(*dr), GFP_KERNEL);
75 	if (!dr)
76 		return -ENOMEM;
77 
78 	pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
79 
80 	for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
81 		struct hda_pcm_stream *stream;
82 		int dir;
83 
84 		dir = SNDRV_PCM_STREAM_PLAYBACK;
85 		stream = &pcm->stream[dir];
86 		if (!stream->substreams)
87 			goto capture_routes;
88 
89 		dr[n].sink = devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
90 					    snd_pcm_direction_name(dir));
91 		dr[n].source = devm_kasprintf(dev, GFP_KERNEL, "%s-cpu%d Tx", cname, i);
92 		if (!dr[n].sink || !dr[n].source)
93 			return -ENOMEM;
94 		n++;
95 
96 capture_routes:
97 		dir = SNDRV_PCM_STREAM_CAPTURE;
98 		stream = &pcm->stream[dir];
99 		if (!stream->substreams)
100 			continue;
101 
102 		dr[n].sink = devm_kasprintf(dev, GFP_KERNEL, "%s-cpu%d Rx", cname, i);
103 		dr[n].source = devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
104 					      snd_pcm_direction_name(dir));
105 		if (!dr[n].sink || !dr[n].source)
106 			return -ENOMEM;
107 		n++;
108 	}
109 
110 	*routes = dr;
111 	*num_routes = n;
112 	return 0;
113 }
114 
115 /* Should be aligned with SectionPCM's name from topology */
116 #define FEDAI_NAME_PREFIX "HDMI"
117 
118 static struct snd_pcm *
119 avs_card_hdmi_pcm_at(struct snd_soc_card *card, int hdmi_idx)
120 {
121 	struct snd_soc_pcm_runtime *rtd;
122 	int dir = SNDRV_PCM_STREAM_PLAYBACK;
123 
124 	for_each_card_rtds(card, rtd) {
125 		struct snd_pcm *spcm;
126 		int ret, n;
127 
128 		spcm = rtd->pcm ? rtd->pcm->streams[dir].pcm : NULL;
129 		if (!spcm || !strstr(spcm->id, FEDAI_NAME_PREFIX))
130 			continue;
131 
132 		ret = sscanf(spcm->id, FEDAI_NAME_PREFIX "%d", &n);
133 		if (ret != 1)
134 			continue;
135 		if (n == hdmi_idx)
136 			return rtd->pcm;
137 	}
138 
139 	return NULL;
140 }
141 
142 static int avs_card_late_probe(struct snd_soc_card *card)
143 {
144 	struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev);
145 	struct hda_codec *codec = mach->pdata;
146 	struct hda_pcm *hpcm;
147 	/* Topology pcm indexing is 1-based */
148 	int i = 1;
149 
150 	list_for_each_entry(hpcm, &codec->pcm_list_head, list) {
151 		struct snd_pcm *spcm;
152 
153 		spcm = avs_card_hdmi_pcm_at(card, i);
154 		if (spcm) {
155 			hpcm->pcm = spcm;
156 			hpcm->device = spcm->device;
157 			dev_info(card->dev, "%s: mapping HDMI converter %d to PCM %d (%p)\n",
158 				 __func__, i, hpcm->device, spcm);
159 		} else {
160 			hpcm->pcm = NULL;
161 			hpcm->device = SNDRV_PCM_INVALID_DEVICE;
162 			dev_warn(card->dev, "%s: no PCM in topology for HDMI converter %d\n",
163 				 __func__, i);
164 		}
165 		i++;
166 	}
167 
168 	return hda_codec_probe_complete(codec);
169 }
170 
171 static int avs_probing_link_init(struct snd_soc_pcm_runtime *rtm)
172 {
173 	struct snd_soc_dapm_route *routes;
174 	struct snd_soc_acpi_mach *mach;
175 	struct snd_soc_dai_link *links = NULL;
176 	struct snd_soc_card *card = rtm->card;
177 	struct hda_codec *codec;
178 	struct hda_pcm *pcm;
179 	int ret, n, pcm_count = 0;
180 
181 	mach = dev_get_platdata(card->dev);
182 	codec = mach->pdata;
183 
184 	if (list_empty(&codec->pcm_list_head))
185 		return -EINVAL;
186 	list_for_each_entry(pcm, &codec->pcm_list_head, list)
187 		pcm_count++;
188 
189 	ret = avs_create_dai_links(card->dev, codec, pcm_count, mach->mach_params.platform, &links);
190 	if (ret < 0) {
191 		dev_err(card->dev, "create links failed: %d\n", ret);
192 		return ret;
193 	}
194 
195 	for (n = 0; n < pcm_count; n++) {
196 		ret = snd_soc_add_pcm_runtime(card, &links[n]);
197 		if (ret < 0) {
198 			dev_err(card->dev, "add links failed: %d\n", ret);
199 			return ret;
200 		}
201 	}
202 
203 	ret = avs_create_dapm_routes(card->dev, codec, pcm_count, &routes, &n);
204 	if (ret < 0) {
205 		dev_err(card->dev, "create routes failed: %d\n", ret);
206 		return ret;
207 	}
208 
209 	ret = snd_soc_dapm_add_routes(&card->dapm, routes, n);
210 	if (ret < 0) {
211 		dev_err(card->dev, "add routes failed: %d\n", ret);
212 		return ret;
213 	}
214 
215 	return 0;
216 }
217 
218 SND_SOC_DAILINK_DEF(dummy, DAILINK_COMP_ARRAY(COMP_DUMMY()));
219 
220 static struct snd_soc_dai_link probing_link = {
221 	.name = "probing-LINK",
222 	.id = -1,
223 	.nonatomic = 1,
224 	.no_pcm = 1,
225 	.dpcm_playback = 1,
226 	.dpcm_capture = 1,
227 	.cpus = dummy,
228 	.num_cpus = ARRAY_SIZE(dummy),
229 	.init = avs_probing_link_init,
230 };
231 
232 static int avs_hdaudio_probe(struct platform_device *pdev)
233 {
234 	struct snd_soc_dai_link *binder;
235 	struct snd_soc_acpi_mach *mach;
236 	struct snd_soc_card *card;
237 	struct device *dev = &pdev->dev;
238 	struct hda_codec *codec;
239 
240 	mach = dev_get_platdata(dev);
241 	codec = mach->pdata;
242 
243 	/* codec may be unloaded before card's probe() fires */
244 	if (!device_is_registered(&codec->core.dev))
245 		return -ENODEV;
246 
247 	binder = devm_kmemdup(dev, &probing_link, sizeof(probing_link), GFP_KERNEL);
248 	if (!binder)
249 		return -ENOMEM;
250 
251 	binder->platforms = devm_kzalloc(dev, sizeof(*binder->platforms), GFP_KERNEL);
252 	binder->codecs = devm_kzalloc(dev, sizeof(*binder->codecs), GFP_KERNEL);
253 	if (!binder->platforms || !binder->codecs)
254 		return -ENOMEM;
255 
256 	binder->codecs->name = devm_kstrdup(dev, dev_name(&codec->core.dev), GFP_KERNEL);
257 	if (!binder->codecs->name)
258 		return -ENOMEM;
259 
260 	binder->platforms->name = mach->mach_params.platform;
261 	binder->num_platforms = 1;
262 	binder->codecs->dai_name = "codec-probing-DAI";
263 	binder->num_codecs = 1;
264 
265 	card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
266 	if (!card)
267 		return -ENOMEM;
268 
269 	card->name = binder->codecs->name;
270 	card->dev = dev;
271 	card->owner = THIS_MODULE;
272 	card->dai_link = binder;
273 	card->num_links = 1;
274 	card->fully_routed = true;
275 	if (hda_codec_is_display(codec))
276 		card->late_probe = avs_card_late_probe;
277 
278 	return devm_snd_soc_register_card(dev, card);
279 }
280 
281 static struct platform_driver avs_hdaudio_driver = {
282 	.probe = avs_hdaudio_probe,
283 	.driver = {
284 		.name = "avs_hdaudio",
285 		.pm = &snd_soc_pm_ops,
286 	},
287 };
288 
289 module_platform_driver(avs_hdaudio_driver)
290 
291 MODULE_DESCRIPTION("Intel HD-Audio machine driver");
292 MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>");
293 MODULE_LICENSE("GPL");
294 MODULE_ALIAS("platform:avs_hdaudio");
295