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