xref: /linux/sound/soc/codecs/hda.c (revision 31fd009c28b1df5f9ecdef5283ce497e701aef80)
1 // SPDX-License-Identifier: GPL-2.0
2 //
3 // Copyright(c) 2021-2022 Intel Corporation
4 //
5 // Author: Cezary Rojewski <cezary.rojewski@intel.com>
6 //
7 
8 #include <linux/module.h>
9 #include <linux/pm_runtime.h>
10 #include <sound/soc.h>
11 #include <sound/hdaudio_ext.h>
12 #include <sound/hda_i915.h>
13 #include <sound/hda_codec.h>
14 #include "hda.h"
15 
16 static int hda_codec_create_dais(struct hda_codec *codec, int pcm_count,
17 				 struct snd_soc_dai_driver **drivers)
18 {
19 	struct device *dev = &codec->core.dev;
20 	struct snd_soc_dai_driver *drvs;
21 	struct hda_pcm *pcm;
22 	int i;
23 
24 	drvs = devm_kcalloc(dev, pcm_count, sizeof(*drvs), GFP_KERNEL);
25 	if (!drvs)
26 		return -ENOMEM;
27 
28 	pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
29 
30 	for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
31 		struct snd_soc_pcm_stream *stream;
32 		int dir;
33 
34 		dev_info(dev, "creating for %s %d\n", pcm->name, i);
35 		drvs[i].id = i;
36 		drvs[i].name = pcm->name;
37 		drvs[i].ops = &snd_soc_hda_codec_dai_ops;
38 
39 		dir = SNDRV_PCM_STREAM_PLAYBACK;
40 		stream = &drvs[i].playback;
41 		if (!pcm->stream[dir].substreams) {
42 			dev_info(dev, "skipping playback dai for %s\n", pcm->name);
43 			goto capture_dais;
44 		}
45 
46 		stream->stream_name =
47 			devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
48 				       snd_pcm_direction_name(dir));
49 		if (!stream->stream_name)
50 			return -ENOMEM;
51 		stream->channels_min = pcm->stream[dir].channels_min;
52 		stream->channels_max = pcm->stream[dir].channels_max;
53 		stream->rates = pcm->stream[dir].rates;
54 		stream->formats = pcm->stream[dir].formats;
55 		stream->subformats = pcm->stream[dir].subformats;
56 		stream->sig_bits = pcm->stream[dir].maxbps;
57 
58 capture_dais:
59 		dir = SNDRV_PCM_STREAM_CAPTURE;
60 		stream = &drvs[i].capture;
61 		if (!pcm->stream[dir].substreams) {
62 			dev_info(dev, "skipping capture dai for %s\n", pcm->name);
63 			continue;
64 		}
65 
66 		stream->stream_name =
67 			devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
68 				       snd_pcm_direction_name(dir));
69 		if (!stream->stream_name)
70 			return -ENOMEM;
71 		stream->channels_min = pcm->stream[dir].channels_min;
72 		stream->channels_max = pcm->stream[dir].channels_max;
73 		stream->rates = pcm->stream[dir].rates;
74 		stream->formats = pcm->stream[dir].formats;
75 		stream->subformats = pcm->stream[dir].subformats;
76 		stream->sig_bits = pcm->stream[dir].maxbps;
77 	}
78 
79 	*drivers = drvs;
80 	return 0;
81 }
82 
83 static int hda_codec_register_dais(struct hda_codec *codec, struct snd_soc_component *component)
84 {
85 	struct snd_soc_dai_driver *drvs = NULL;
86 	struct snd_soc_dapm_context *dapm;
87 	struct hda_pcm *pcm;
88 	int ret, pcm_count;
89 
90 	pcm_count = list_count_nodes(&codec->pcm_list_head);
91 	if (!pcm_count)
92 		return -EINVAL;
93 
94 	ret = hda_codec_create_dais(codec, pcm_count, &drvs);
95 	if (ret < 0)
96 		return ret;
97 
98 	dapm = snd_soc_component_to_dapm(component);
99 
100 	list_for_each_entry(pcm, &codec->pcm_list_head, list) {
101 		struct snd_soc_dai *dai;
102 
103 		dai = snd_soc_register_dai(component, drvs, false);
104 		if (!dai) {
105 			dev_err(component->dev, "register dai for %s failed\n", pcm->name);
106 			return -EINVAL;
107 		}
108 
109 		ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
110 		if (ret < 0) {
111 			dev_err(component->dev, "create widgets failed: %d\n", ret);
112 			snd_soc_unregister_dai(dai);
113 			return ret;
114 		}
115 
116 		snd_soc_dai_init_dma_data(dai, &pcm->stream[0], &pcm->stream[1]);
117 		drvs++;
118 	}
119 
120 	return 0;
121 }
122 
123 static void hda_codec_unregister_dais(struct hda_codec *codec,
124 				      struct snd_soc_component *component)
125 {
126 	struct snd_soc_dai *dai, *save;
127 	struct hda_pcm *pcm;
128 
129 	for_each_component_dais_safe(component, dai, save) {
130 		int stream;
131 
132 		list_for_each_entry(pcm, &codec->pcm_list_head, list) {
133 			if (strcmp(dai->driver->name, pcm->name))
134 				continue;
135 
136 			for_each_pcm_streams(stream)
137 				snd_soc_dapm_free_widget(snd_soc_dai_get_widget(dai, stream));
138 
139 			snd_soc_unregister_dai(dai);
140 			break;
141 		}
142 	}
143 }
144 
145 int hda_codec_probe_complete(struct hda_codec *codec)
146 {
147 	struct hdac_device *hdev = &codec->core;
148 	struct hdac_bus *bus = hdev->bus;
149 	int ret;
150 
151 	ret = snd_hda_codec_build_controls(codec);
152 	if (ret < 0) {
153 		dev_err(&hdev->dev, "unable to create controls %d\n", ret);
154 		return ret;
155 	}
156 
157 	/* Bus suspended codecs as it does not manage their pm */
158 	pm_runtime_set_active(&hdev->dev);
159 	/* rpm was forbidden in snd_hda_codec_device_new() */
160 	snd_hda_codec_set_power_save(codec, 2000);
161 	snd_hda_codec_register(codec);
162 
163 	/* Complement pm_runtime_get_sync(bus) in probe */
164 	pm_runtime_put_autosuspend(bus->dev);
165 
166 	return ret;
167 }
168 EXPORT_SYMBOL_GPL(hda_codec_probe_complete);
169 
170 /* Expects codec with usage_count=1 and status=suspended */
171 static int hda_codec_probe(struct snd_soc_component *component)
172 {
173 	struct hda_codec *codec = dev_to_hda_codec(component->dev);
174 	struct hda_codec_driver *driver = hda_codec_to_driver(codec);
175 	struct hdac_device *hdev = &codec->core;
176 	struct hdac_bus *bus = hdev->bus;
177 	struct hdac_ext_link *hlink;
178 	int ret;
179 
180 #ifdef CONFIG_PM
181 	WARN_ON(atomic_read(&hdev->dev.power.usage_count) != 1 ||
182 		!pm_runtime_status_suspended(&hdev->dev));
183 #endif
184 
185 	hlink = snd_hdac_ext_bus_get_hlink_by_addr(bus, hdev->addr);
186 	if (!hlink) {
187 		dev_err(&hdev->dev, "hdac link not found\n");
188 		return -EIO;
189 	}
190 
191 	pm_runtime_get_sync(bus->dev);
192 	if (hda_codec_is_display(codec))
193 		snd_hdac_display_power(bus, hdev->addr, true);
194 	snd_hdac_ext_bus_link_get(bus, hlink);
195 
196 	ret = snd_hda_codec_device_new(codec->bus, component->card->snd_card, hdev->addr, codec,
197 				       false);
198 	if (ret < 0) {
199 		dev_err(&hdev->dev, "codec create failed: %d\n", ret);
200 		goto device_new_err;
201 	}
202 
203 	ret = snd_hda_codec_set_name(codec, codec->preset->name);
204 	if (ret < 0) {
205 		dev_err(&hdev->dev, "set name: %s failed: %d\n", codec->preset->name, ret);
206 		goto err;
207 	}
208 
209 	ret = snd_hdac_regmap_init(&codec->core);
210 	if (ret < 0) {
211 		dev_err(&hdev->dev, "regmap init failed: %d\n", ret);
212 		goto err;
213 	}
214 
215 	if (WARN_ON(!(driver->ops && driver->ops->probe))) {
216 		ret = -EINVAL;
217 		goto err;
218 	}
219 
220 	/* unsol events are still blocked until registered */
221 	codec->core.unsol_disabled = false;
222 
223 	ret = driver->ops->probe(codec, codec->preset);
224 	if (ret < 0) {
225 		dev_err(&hdev->dev, "codec init failed: %d\n", ret);
226 		goto err;
227 	}
228 
229 	ret = snd_hda_codec_parse_pcms(codec);
230 	if (ret < 0) {
231 		dev_err(&hdev->dev, "unable to map pcms to dai: %d\n", ret);
232 		goto parse_pcms_err;
233 	}
234 
235 	ret = hda_codec_register_dais(codec, component);
236 	if (ret < 0) {
237 		dev_err(&hdev->dev, "update dais failed: %d\n", ret);
238 		goto parse_pcms_err;
239 	}
240 
241 	if (!hda_codec_is_display(codec)) {
242 		ret = hda_codec_probe_complete(codec);
243 		if (ret < 0)
244 			goto complete_err;
245 	}
246 
247 	codec->core.lazy_cache = true;
248 
249 	return 0;
250 
251 complete_err:
252 	hda_codec_unregister_dais(codec, component);
253 parse_pcms_err:
254 	if (driver->ops->remove)
255 		driver->ops->remove(codec);
256 err:
257 	snd_hda_codec_cleanup_for_unbind(codec);
258 device_new_err:
259 	if (hda_codec_is_display(codec))
260 		snd_hdac_display_power(bus, hdev->addr, false);
261 
262 	snd_hdac_ext_bus_link_put(bus, hlink);
263 
264 	pm_runtime_put_autosuspend(bus->dev);
265 	return ret;
266 }
267 
268 /* Leaves codec with usage_count=1 and status=suspended */
269 static void hda_codec_remove(struct snd_soc_component *component)
270 {
271 	struct hda_codec *codec = dev_to_hda_codec(component->dev);
272 	struct hda_codec_driver *driver = hda_codec_to_driver(codec);
273 	struct hdac_device *hdev = &codec->core;
274 	struct hdac_bus *bus = hdev->bus;
275 	struct hdac_ext_link *hlink;
276 	bool was_registered = codec->core.registered;
277 
278 	/* Don't allow any more runtime suspends */
279 	pm_runtime_forbid(&hdev->dev);
280 
281 	/* stop asynchronous jack handling before freeing driver resources */
282 	snd_hdac_device_disable_unsol(hdev);
283 	cancel_delayed_work_sync(&codec->jackpoll_work);
284 
285 	hda_codec_unregister_dais(codec, component);
286 
287 	if (driver->ops->remove)
288 		driver->ops->remove(codec);
289 
290 	snd_hda_codec_cleanup_for_unbind(codec);
291 	pm_runtime_put_noidle(&hdev->dev);
292 	/* snd_hdac_device_exit() is only called on bus remove */
293 	pm_runtime_set_suspended(&hdev->dev);
294 
295 	if (hda_codec_is_display(codec))
296 		snd_hdac_display_power(bus, hdev->addr, false);
297 
298 	hlink = snd_hdac_ext_bus_get_hlink_by_addr(bus, hdev->addr);
299 	if (hlink)
300 		snd_hdac_ext_bus_link_put(bus, hlink);
301 	/*
302 	 * HDMI card's hda_codec_probe_complete() (see late_probe()) may
303 	 * not be called due to early error, leaving bus uc unbalanced
304 	 */
305 	if (!was_registered) {
306 		pm_runtime_put_autosuspend(bus->dev);
307 	}
308 
309 #ifdef CONFIG_PM
310 	WARN_ON(atomic_read(&hdev->dev.power.usage_count) != 1 ||
311 		!pm_runtime_status_suspended(&hdev->dev));
312 #endif
313 }
314 
315 static const struct snd_soc_dapm_route hda_dapm_routes[] = {
316 	{"AIF1TX", NULL, "Codec Input Pin1"},
317 	{"AIF2TX", NULL, "Codec Input Pin2"},
318 	{"AIF3TX", NULL, "Codec Input Pin3"},
319 
320 	{"Codec Output Pin1", NULL, "AIF1RX"},
321 	{"Codec Output Pin2", NULL, "AIF2RX"},
322 	{"Codec Output Pin3", NULL, "AIF3RX"},
323 };
324 
325 static const struct snd_soc_dapm_widget hda_dapm_widgets[] = {
326 	/* Audio Interface */
327 	SND_SOC_DAPM_AIF_IN("AIF1RX", "Analog Codec Playback", 0, SND_SOC_NOPM, 0, 0),
328 	SND_SOC_DAPM_AIF_IN("AIF2RX", "Digital Codec Playback", 0, SND_SOC_NOPM, 0, 0),
329 	SND_SOC_DAPM_AIF_IN("AIF3RX", "Alt Analog Codec Playback", 0, SND_SOC_NOPM, 0, 0),
330 	SND_SOC_DAPM_AIF_OUT("AIF1TX", "Analog Codec Capture", 0, SND_SOC_NOPM, 0, 0),
331 	SND_SOC_DAPM_AIF_OUT("AIF2TX", "Digital Codec Capture", 0, SND_SOC_NOPM, 0, 0),
332 	SND_SOC_DAPM_AIF_OUT("AIF3TX", "Alt Analog Codec Capture", 0, SND_SOC_NOPM, 0, 0),
333 
334 	/* Input Pins */
335 	SND_SOC_DAPM_INPUT("Codec Input Pin1"),
336 	SND_SOC_DAPM_INPUT("Codec Input Pin2"),
337 	SND_SOC_DAPM_INPUT("Codec Input Pin3"),
338 
339 	/* Output Pins */
340 	SND_SOC_DAPM_OUTPUT("Codec Output Pin1"),
341 	SND_SOC_DAPM_OUTPUT("Codec Output Pin2"),
342 	SND_SOC_DAPM_OUTPUT("Codec Output Pin3"),
343 };
344 
345 static struct snd_soc_dai_driver card_binder_dai = {
346 	.id = -1,
347 	.name = "codec-probing-DAI",
348 };
349 
350 static int hda_hdev_attach(struct hdac_device *hdev)
351 {
352 	struct hda_codec *codec = dev_to_hda_codec(&hdev->dev);
353 	struct snd_soc_component_driver *comp_drv;
354 
355 	if (hda_codec_is_display(codec) && !hdev->bus->audio_component) {
356 		dev_dbg(&hdev->dev, "no i915, skip registration for 0x%08x\n", hdev->vendor_id);
357 		return -ENODEV;
358 	}
359 
360 	comp_drv = devm_kzalloc(&hdev->dev, sizeof(*comp_drv), GFP_KERNEL);
361 	if (!comp_drv)
362 		return -ENOMEM;
363 
364 	/*
365 	 * It's save to rely on dev_name() rather than a copy as component
366 	 * driver's lifetime is directly tied to hda codec one
367 	 */
368 	comp_drv->name = dev_name(&hdev->dev);
369 	comp_drv->probe = hda_codec_probe;
370 	comp_drv->remove = hda_codec_remove;
371 	comp_drv->idle_bias_on = false;
372 	if (!hda_codec_is_display(codec)) {
373 		comp_drv->dapm_widgets = hda_dapm_widgets;
374 		comp_drv->num_dapm_widgets = ARRAY_SIZE(hda_dapm_widgets);
375 		comp_drv->dapm_routes = hda_dapm_routes;
376 		comp_drv->num_dapm_routes = ARRAY_SIZE(hda_dapm_routes);
377 	}
378 
379 	return snd_soc_register_component(&hdev->dev, comp_drv, &card_binder_dai, 1);
380 }
381 
382 static int hda_hdev_detach(struct hdac_device *hdev)
383 {
384 	struct hda_codec *codec = dev_to_hda_codec(&hdev->dev);
385 
386 	if (codec->core.registered)
387 		cancel_delayed_work_sync(&codec->jackpoll_work);
388 
389 	snd_soc_unregister_component(&hdev->dev);
390 
391 	return 0;
392 }
393 
394 const struct hdac_ext_bus_ops soc_hda_ext_bus_ops = {
395 	.hdev_attach = hda_hdev_attach,
396 	.hdev_detach = hda_hdev_detach,
397 };
398 EXPORT_SYMBOL_GPL(soc_hda_ext_bus_ops);
399 
400 MODULE_DESCRIPTION("HD-Audio codec driver");
401 MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>");
402 MODULE_LICENSE("GPL");
403