1 // SPDX-License-Identifier: GPL-2.0
3 // Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
5 // Author: Cezary Rojewski <cezary.rojewski@intel.com>
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>
16 static int hda_codec_create_dais(struct hda_codec *codec, int pcm_count,
17 struct snd_soc_dai_driver **drivers)
19 struct device *dev = &codec->core.dev;
20 struct snd_soc_dai_driver *drvs;
24 drvs = devm_kcalloc(dev, pcm_count, sizeof(*drvs), GFP_KERNEL);
28 pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
30 for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
31 struct snd_soc_pcm_stream *stream;
34 dev_info(dev, "creating for %s %d\n", pcm->name, i);
36 drvs[i].name = pcm->name;
37 drvs[i].ops = &snd_soc_hda_codec_dai_ops;
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);
47 devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
48 snd_pcm_direction_name(dir));
49 if (!stream->stream_name)
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;
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);
66 devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
67 snd_pcm_direction_name(dir));
68 if (!stream->stream_name)
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;
81 static int hda_codec_register_dais(struct hda_codec *codec, struct snd_soc_component *component)
83 struct snd_soc_dai_driver *drvs = NULL;
84 struct snd_soc_dapm_context *dapm;
86 int ret, pcm_count = 0;
88 if (list_empty(&codec->pcm_list_head))
90 list_for_each_entry(pcm, &codec->pcm_list_head, list)
93 ret = hda_codec_create_dais(codec, pcm_count, &drvs);
97 dapm = snd_soc_component_get_dapm(component);
99 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
100 struct snd_soc_dai *dai;
102 dai = snd_soc_register_dai(component, drvs, false);
104 dev_err(component->dev, "register dai for %s failed\n", pcm->name);
108 ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
110 dev_err(component->dev, "create widgets failed: %d\n", ret);
111 snd_soc_unregister_dai(dai);
115 snd_soc_dai_init_dma_data(dai, &pcm->stream[0], &pcm->stream[1]);
122 static void hda_codec_unregister_dais(struct hda_codec *codec,
123 struct snd_soc_component *component)
125 struct snd_soc_dai *dai, *save;
128 for_each_component_dais_safe(component, dai, save) {
131 list_for_each_entry(pcm, &codec->pcm_list_head, list) {
132 if (strcmp(dai->driver->name, pcm->name))
135 for_each_pcm_streams(stream)
136 snd_soc_dapm_free_widget(snd_soc_dai_get_widget(dai, stream));
138 snd_soc_unregister_dai(dai);
144 int hda_codec_probe_complete(struct hda_codec *codec)
146 struct hdac_device *hdev = &codec->core;
147 struct hdac_bus *bus = hdev->bus;
150 ret = snd_hda_codec_build_controls(codec);
152 dev_err(&hdev->dev, "unable to create controls %d\n", ret);
156 /* Bus suspended codecs as it does not manage their pm */
157 pm_runtime_set_active(&hdev->dev);
158 /* rpm was forbidden in snd_hda_codec_device_new() */
159 snd_hda_codec_set_power_save(codec, 2000);
160 snd_hda_codec_register(codec);
162 /* Complement pm_runtime_get_sync(bus) in probe */
163 pm_runtime_mark_last_busy(bus->dev);
164 pm_runtime_put_autosuspend(bus->dev);
168 EXPORT_SYMBOL_GPL(hda_codec_probe_complete);
170 /* Expects codec with usage_count=1 and status=suspended */
171 static int hda_codec_probe(struct snd_soc_component *component)
173 struct hda_codec *codec = dev_to_hda_codec(component->dev);
174 struct hdac_device *hdev = &codec->core;
175 struct hdac_bus *bus = hdev->bus;
176 struct hdac_ext_link *hlink;
177 hda_codec_patch_t patch;
181 WARN_ON(atomic_read(&hdev->dev.power.usage_count) != 1 ||
182 !pm_runtime_status_suspended(&hdev->dev));
185 hlink = snd_hdac_ext_bus_get_hlink_by_addr(bus, hdev->addr);
187 dev_err(&hdev->dev, "hdac link not found\n");
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);
196 ret = snd_hda_codec_device_new(codec->bus, component->card->snd_card, hdev->addr, codec,
199 dev_err(&hdev->dev, "create hda codec failed: %d\n", ret);
203 ret = snd_hda_codec_set_name(codec, codec->preset->name);
205 dev_err(&hdev->dev, "name failed %s\n", codec->preset->name);
209 ret = snd_hdac_regmap_init(&codec->core);
211 dev_err(&hdev->dev, "regmap init failed\n");
215 patch = (hda_codec_patch_t)codec->preset->driver_data;
217 dev_err(&hdev->dev, "no patch specified\n");
224 dev_err(&hdev->dev, "patch failed %d\n", ret);
228 ret = snd_hda_codec_parse_pcms(codec);
230 dev_err(&hdev->dev, "unable to map pcms to dai %d\n", ret);
234 ret = hda_codec_register_dais(codec, component);
236 dev_err(&hdev->dev, "update dais failed: %d\n", ret);
240 if (!hda_codec_is_display(codec)) {
241 ret = hda_codec_probe_complete(codec);
246 codec->core.lazy_cache = true;
251 hda_codec_unregister_dais(codec, component);
253 if (codec->patch_ops.free)
254 codec->patch_ops.free(codec);
256 snd_hda_codec_cleanup_for_unbind(codec);
258 if (hda_codec_is_display(codec))
259 snd_hdac_display_power(bus, hdev->addr, false);
261 snd_hdac_ext_bus_link_put(bus, hlink);
263 pm_runtime_mark_last_busy(bus->dev);
264 pm_runtime_put_autosuspend(bus->dev);
268 /* Leaves codec with usage_count=1 and status=suspended */
269 static void hda_codec_remove(struct snd_soc_component *component)
271 struct hda_codec *codec = dev_to_hda_codec(component->dev);
272 struct hdac_device *hdev = &codec->core;
273 struct hdac_bus *bus = hdev->bus;
274 struct hdac_ext_link *hlink;
275 bool was_registered = codec->core.registered;
277 /* Don't allow any more runtime suspends */
278 pm_runtime_forbid(&hdev->dev);
280 hda_codec_unregister_dais(codec, component);
282 if (codec->patch_ops.free)
283 codec->patch_ops.free(codec);
285 snd_hda_codec_cleanup_for_unbind(codec);
286 pm_runtime_put_noidle(&hdev->dev);
287 /* snd_hdac_device_exit() is only called on bus remove */
288 pm_runtime_set_suspended(&hdev->dev);
290 if (hda_codec_is_display(codec))
291 snd_hdac_display_power(bus, hdev->addr, false);
293 hlink = snd_hdac_ext_bus_get_hlink_by_addr(bus, hdev->addr);
295 snd_hdac_ext_bus_link_put(bus, hlink);
297 * HDMI card's hda_codec_probe_complete() (see late_probe()) may
298 * not be called due to early error, leaving bus uc unbalanced
300 if (!was_registered) {
301 pm_runtime_mark_last_busy(bus->dev);
302 pm_runtime_put_autosuspend(bus->dev);
306 WARN_ON(atomic_read(&hdev->dev.power.usage_count) != 1 ||
307 !pm_runtime_status_suspended(&hdev->dev));
311 static const struct snd_soc_dapm_route hda_dapm_routes[] = {
312 {"AIF1TX", NULL, "Codec Input Pin1"},
313 {"AIF2TX", NULL, "Codec Input Pin2"},
314 {"AIF3TX", NULL, "Codec Input Pin3"},
316 {"Codec Output Pin1", NULL, "AIF1RX"},
317 {"Codec Output Pin2", NULL, "AIF2RX"},
318 {"Codec Output Pin3", NULL, "AIF3RX"},
321 static const struct snd_soc_dapm_widget hda_dapm_widgets[] = {
322 /* Audio Interface */
323 SND_SOC_DAPM_AIF_IN("AIF1RX", "Analog Codec Playback", 0, SND_SOC_NOPM, 0, 0),
324 SND_SOC_DAPM_AIF_IN("AIF2RX", "Digital Codec Playback", 0, SND_SOC_NOPM, 0, 0),
325 SND_SOC_DAPM_AIF_IN("AIF3RX", "Alt Analog Codec Playback", 0, SND_SOC_NOPM, 0, 0),
326 SND_SOC_DAPM_AIF_OUT("AIF1TX", "Analog Codec Capture", 0, SND_SOC_NOPM, 0, 0),
327 SND_SOC_DAPM_AIF_OUT("AIF2TX", "Digital Codec Capture", 0, SND_SOC_NOPM, 0, 0),
328 SND_SOC_DAPM_AIF_OUT("AIF3TX", "Alt Analog Codec Capture", 0, SND_SOC_NOPM, 0, 0),
331 SND_SOC_DAPM_INPUT("Codec Input Pin1"),
332 SND_SOC_DAPM_INPUT("Codec Input Pin2"),
333 SND_SOC_DAPM_INPUT("Codec Input Pin3"),
336 SND_SOC_DAPM_OUTPUT("Codec Output Pin1"),
337 SND_SOC_DAPM_OUTPUT("Codec Output Pin2"),
338 SND_SOC_DAPM_OUTPUT("Codec Output Pin3"),
341 static struct snd_soc_dai_driver card_binder_dai = {
343 .name = "codec-probing-DAI",
346 static int hda_hdev_attach(struct hdac_device *hdev)
348 struct hda_codec *codec = dev_to_hda_codec(&hdev->dev);
349 struct snd_soc_component_driver *comp_drv;
351 comp_drv = devm_kzalloc(&hdev->dev, sizeof(*comp_drv), GFP_KERNEL);
356 * It's save to rely on dev_name() rather than a copy as component
357 * driver's lifetime is directly tied to hda codec one
359 comp_drv->name = dev_name(&hdev->dev);
360 comp_drv->probe = hda_codec_probe;
361 comp_drv->remove = hda_codec_remove;
362 comp_drv->idle_bias_on = false;
363 if (!hda_codec_is_display(codec)) {
364 comp_drv->dapm_widgets = hda_dapm_widgets;
365 comp_drv->num_dapm_widgets = ARRAY_SIZE(hda_dapm_widgets);
366 comp_drv->dapm_routes = hda_dapm_routes;
367 comp_drv->num_dapm_routes = ARRAY_SIZE(hda_dapm_routes);
370 return snd_soc_register_component(&hdev->dev, comp_drv, &card_binder_dai, 1);
373 static int hda_hdev_detach(struct hdac_device *hdev)
375 struct hda_codec *codec = dev_to_hda_codec(&hdev->dev);
377 if (codec->core.registered)
378 cancel_delayed_work_sync(&codec->jackpoll_work);
380 snd_soc_unregister_component(&hdev->dev);
385 const struct hdac_ext_bus_ops soc_hda_ext_bus_ops = {
386 .hdev_attach = hda_hdev_attach,
387 .hdev_detach = hda_hdev_detach,
389 EXPORT_SYMBOL_GPL(soc_hda_ext_bus_ops);
391 MODULE_DESCRIPTION("HD-Audio codec driver");
392 MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>");
393 MODULE_LICENSE("GPL");