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