Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
0004 //
0005 // Author: Cezary Rojewski <cezary.rojewski@intel.com>
0006 //
0007 
0008 #include <linux/module.h>
0009 #include <linux/pm_runtime.h>
0010 #include <sound/soc.h>
0011 #include <sound/hdaudio_ext.h>
0012 #include <sound/hda_i915.h>
0013 #include <sound/hda_codec.h>
0014 #include "hda.h"
0015 
0016 static int hda_codec_create_dais(struct hda_codec *codec, int pcm_count,
0017                  struct snd_soc_dai_driver **drivers)
0018 {
0019     struct device *dev = &codec->core.dev;
0020     struct snd_soc_dai_driver *drvs;
0021     struct hda_pcm *pcm;
0022     int i;
0023 
0024     drvs = devm_kcalloc(dev, pcm_count, sizeof(*drvs), GFP_KERNEL);
0025     if (!drvs)
0026         return -ENOMEM;
0027 
0028     pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
0029 
0030     for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
0031         struct snd_soc_pcm_stream *stream;
0032         int dir;
0033 
0034         dev_info(dev, "creating for %s %d\n", pcm->name, i);
0035         drvs[i].id = i;
0036         drvs[i].name = pcm->name;
0037         drvs[i].ops = &snd_soc_hda_codec_dai_ops;
0038 
0039         dir = SNDRV_PCM_STREAM_PLAYBACK;
0040         stream = &drvs[i].playback;
0041         if (!pcm->stream[dir].substreams) {
0042             dev_info(dev, "skipping playback dai for %s\n", pcm->name);
0043             goto capture_dais;
0044         }
0045 
0046         stream->stream_name =
0047             devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
0048                        snd_pcm_direction_name(dir));
0049         if (!stream->stream_name)
0050             return -ENOMEM;
0051         stream->channels_min = pcm->stream[dir].channels_min;
0052         stream->channels_max = pcm->stream[dir].channels_max;
0053         stream->rates = pcm->stream[dir].rates;
0054         stream->formats = pcm->stream[dir].formats;
0055         stream->sig_bits = pcm->stream[dir].maxbps;
0056 
0057 capture_dais:
0058         dir = SNDRV_PCM_STREAM_CAPTURE;
0059         stream = &drvs[i].capture;
0060         if (!pcm->stream[dir].substreams) {
0061             dev_info(dev, "skipping capture dai for %s\n", pcm->name);
0062             continue;
0063         }
0064 
0065         stream->stream_name =
0066             devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
0067                        snd_pcm_direction_name(dir));
0068         if (!stream->stream_name)
0069             return -ENOMEM;
0070         stream->channels_min = pcm->stream[dir].channels_min;
0071         stream->channels_max = pcm->stream[dir].channels_max;
0072         stream->rates = pcm->stream[dir].rates;
0073         stream->formats = pcm->stream[dir].formats;
0074         stream->sig_bits = pcm->stream[dir].maxbps;
0075     }
0076 
0077     *drivers = drvs;
0078     return 0;
0079 }
0080 
0081 static int hda_codec_register_dais(struct hda_codec *codec, struct snd_soc_component *component)
0082 {
0083     struct snd_soc_dai_driver *drvs = NULL;
0084     struct snd_soc_dapm_context *dapm;
0085     struct hda_pcm *pcm;
0086     int ret, pcm_count = 0;
0087 
0088     if (list_empty(&codec->pcm_list_head))
0089         return -EINVAL;
0090     list_for_each_entry(pcm, &codec->pcm_list_head, list)
0091         pcm_count++;
0092 
0093     ret = hda_codec_create_dais(codec, pcm_count, &drvs);
0094     if (ret < 0)
0095         return ret;
0096 
0097     dapm = snd_soc_component_get_dapm(component);
0098 
0099     list_for_each_entry(pcm, &codec->pcm_list_head, list) {
0100         struct snd_soc_dai *dai;
0101 
0102         dai = snd_soc_register_dai(component, drvs, false);
0103         if (!dai) {
0104             dev_err(component->dev, "register dai for %s failed\n", pcm->name);
0105             return -EINVAL;
0106         }
0107 
0108         ret = snd_soc_dapm_new_dai_widgets(dapm, dai);
0109         if (ret < 0) {
0110             dev_err(component->dev, "create widgets failed: %d\n", ret);
0111             snd_soc_unregister_dai(dai);
0112             return ret;
0113         }
0114 
0115         snd_soc_dai_init_dma_data(dai, &pcm->stream[0], &pcm->stream[1]);
0116         drvs++;
0117     }
0118 
0119     return 0;
0120 }
0121 
0122 static void hda_codec_unregister_dais(struct hda_codec *codec,
0123                       struct snd_soc_component *component)
0124 {
0125     struct snd_soc_dai *dai, *save;
0126     struct hda_pcm *pcm;
0127 
0128     for_each_component_dais_safe(component, dai, save) {
0129         list_for_each_entry(pcm, &codec->pcm_list_head, list) {
0130             if (strcmp(dai->driver->name, pcm->name))
0131                 continue;
0132 
0133             if (dai->playback_widget)
0134                 snd_soc_dapm_free_widget(dai->playback_widget);
0135             if (dai->capture_widget)
0136                 snd_soc_dapm_free_widget(dai->capture_widget);
0137             snd_soc_unregister_dai(dai);
0138             break;
0139         }
0140     }
0141 }
0142 
0143 int hda_codec_probe_complete(struct hda_codec *codec)
0144 {
0145     struct hdac_device *hdev = &codec->core;
0146     struct hdac_bus *bus = hdev->bus;
0147     int ret;
0148 
0149     ret = snd_hda_codec_build_controls(codec);
0150     if (ret < 0) {
0151         dev_err(&hdev->dev, "unable to create controls %d\n", ret);
0152         goto out;
0153     }
0154 
0155     /* Bus suspended codecs as it does not manage their pm */
0156     pm_runtime_set_active(&hdev->dev);
0157     /* rpm was forbidden in snd_hda_codec_device_new() */
0158     snd_hda_codec_set_power_save(codec, 2000);
0159     snd_hda_codec_register(codec);
0160 out:
0161     /* Complement pm_runtime_get_sync(bus) in probe */
0162     pm_runtime_mark_last_busy(bus->dev);
0163     pm_runtime_put_autosuspend(bus->dev);
0164 
0165     return ret;
0166 }
0167 EXPORT_SYMBOL_GPL(hda_codec_probe_complete);
0168 
0169 /* Expects codec with usage_count=1 and status=suspended */
0170 static int hda_codec_probe(struct snd_soc_component *component)
0171 {
0172     struct hda_codec *codec = dev_to_hda_codec(component->dev);
0173     struct hdac_device *hdev = &codec->core;
0174     struct hdac_bus *bus = hdev->bus;
0175     struct hdac_ext_link *hlink;
0176     hda_codec_patch_t patch;
0177     int ret;
0178 
0179 #ifdef CONFIG_PM
0180     WARN_ON(atomic_read(&hdev->dev.power.usage_count) != 1 ||
0181         !pm_runtime_status_suspended(&hdev->dev));
0182 #endif
0183 
0184     hlink = snd_hdac_ext_bus_link_at(bus, hdev->addr);
0185     if (!hlink) {
0186         dev_err(&hdev->dev, "hdac link not found\n");
0187         return -EIO;
0188     }
0189 
0190     pm_runtime_get_sync(bus->dev);
0191     if (hda_codec_is_display(codec))
0192         snd_hdac_display_power(bus, hdev->addr, true);
0193     snd_hdac_ext_bus_link_get(bus, hlink);
0194 
0195     ret = snd_hda_codec_device_new(codec->bus, component->card->snd_card, hdev->addr, codec,
0196                        false);
0197     if (ret < 0) {
0198         dev_err(&hdev->dev, "create hda codec failed: %d\n", ret);
0199         goto device_new_err;
0200     }
0201 
0202     ret = snd_hda_codec_set_name(codec, codec->preset->name);
0203     if (ret < 0) {
0204         dev_err(&hdev->dev, "name failed %s\n", codec->preset->name);
0205         goto err;
0206     }
0207 
0208     ret = snd_hdac_regmap_init(&codec->core);
0209     if (ret < 0) {
0210         dev_err(&hdev->dev, "regmap init failed\n");
0211         goto err;
0212     }
0213 
0214     patch = (hda_codec_patch_t)codec->preset->driver_data;
0215     if (!patch) {
0216         dev_err(&hdev->dev, "no patch specified?\n");
0217         ret = -EINVAL;
0218         goto err;
0219     }
0220 
0221     ret = patch(codec);
0222     if (ret < 0) {
0223         dev_err(&hdev->dev, "patch failed %d\n", ret);
0224         goto err;
0225     }
0226 
0227     /* configure codec for 1:1 PCM:DAI mapping */
0228     codec->mst_no_extra_pcms = 1;
0229 
0230     ret = snd_hda_codec_parse_pcms(codec);
0231     if (ret < 0) {
0232         dev_err(&hdev->dev, "unable to map pcms to dai %d\n", ret);
0233         goto parse_pcms_err;
0234     }
0235 
0236     ret = hda_codec_register_dais(codec, component);
0237     if (ret < 0) {
0238         dev_err(&hdev->dev, "update dais failed: %d\n", ret);
0239         goto parse_pcms_err;
0240     }
0241 
0242     if (!hda_codec_is_display(codec)) {
0243         ret = hda_codec_probe_complete(codec);
0244         if (ret < 0)
0245             goto complete_err;
0246     }
0247 
0248     codec->core.lazy_cache = true;
0249 
0250     return 0;
0251 
0252 complete_err:
0253     hda_codec_unregister_dais(codec, component);
0254 parse_pcms_err:
0255     if (codec->patch_ops.free)
0256         codec->patch_ops.free(codec);
0257 err:
0258     snd_hda_codec_cleanup_for_unbind(codec);
0259 device_new_err:
0260     if (hda_codec_is_display(codec))
0261         snd_hdac_display_power(bus, hdev->addr, false);
0262 
0263     snd_hdac_ext_bus_link_put(bus, hlink);
0264 
0265     pm_runtime_mark_last_busy(bus->dev);
0266     pm_runtime_put_autosuspend(bus->dev);
0267     return ret;
0268 }
0269 
0270 /* Leaves codec with usage_count=1 and status=suspended */
0271 static void hda_codec_remove(struct snd_soc_component *component)
0272 {
0273     struct hda_codec *codec = dev_to_hda_codec(component->dev);
0274     struct hdac_device *hdev = &codec->core;
0275     struct hdac_bus *bus = hdev->bus;
0276     struct hdac_ext_link *hlink;
0277     bool was_registered = codec->core.registered;
0278 
0279     /* Don't allow any more runtime suspends */
0280     pm_runtime_forbid(&hdev->dev);
0281 
0282     hda_codec_unregister_dais(codec, component);
0283 
0284     if (codec->patch_ops.free)
0285         codec->patch_ops.free(codec);
0286 
0287     snd_hda_codec_cleanup_for_unbind(codec);
0288     pm_runtime_put_noidle(&hdev->dev);
0289     /* snd_hdac_device_exit() is only called on bus remove */
0290     pm_runtime_set_suspended(&hdev->dev);
0291 
0292     if (hda_codec_is_display(codec))
0293         snd_hdac_display_power(bus, hdev->addr, false);
0294 
0295     hlink = snd_hdac_ext_bus_link_at(bus, hdev->addr);
0296     if (hlink)
0297         snd_hdac_ext_bus_link_put(bus, hlink);
0298     /*
0299      * HDMI card's hda_codec_probe_complete() (see late_probe()) may
0300      * not be called due to early error, leaving bus uc unbalanced
0301      */
0302     if (!was_registered) {
0303         pm_runtime_mark_last_busy(bus->dev);
0304         pm_runtime_put_autosuspend(bus->dev);
0305     }
0306 
0307 #ifdef CONFIG_PM
0308     WARN_ON(atomic_read(&hdev->dev.power.usage_count) != 1 ||
0309         !pm_runtime_status_suspended(&hdev->dev));
0310 #endif
0311 }
0312 
0313 static const struct snd_soc_dapm_route hda_dapm_routes[] = {
0314     {"AIF1TX", NULL, "Codec Input Pin1"},
0315     {"AIF2TX", NULL, "Codec Input Pin2"},
0316     {"AIF3TX", NULL, "Codec Input Pin3"},
0317 
0318     {"Codec Output Pin1", NULL, "AIF1RX"},
0319     {"Codec Output Pin2", NULL, "AIF2RX"},
0320     {"Codec Output Pin3", NULL, "AIF3RX"},
0321 };
0322 
0323 static const struct snd_soc_dapm_widget hda_dapm_widgets[] = {
0324     /* Audio Interface */
0325     SND_SOC_DAPM_AIF_IN("AIF1RX", "Analog Codec Playback", 0, SND_SOC_NOPM, 0, 0),
0326     SND_SOC_DAPM_AIF_IN("AIF2RX", "Digital Codec Playback", 0, SND_SOC_NOPM, 0, 0),
0327     SND_SOC_DAPM_AIF_IN("AIF3RX", "Alt Analog Codec Playback", 0, SND_SOC_NOPM, 0, 0),
0328     SND_SOC_DAPM_AIF_OUT("AIF1TX", "Analog Codec Capture", 0, SND_SOC_NOPM, 0, 0),
0329     SND_SOC_DAPM_AIF_OUT("AIF2TX", "Digital Codec Capture", 0, SND_SOC_NOPM, 0, 0),
0330     SND_SOC_DAPM_AIF_OUT("AIF3TX", "Alt Analog Codec Capture", 0, SND_SOC_NOPM, 0, 0),
0331 
0332     /* Input Pins */
0333     SND_SOC_DAPM_INPUT("Codec Input Pin1"),
0334     SND_SOC_DAPM_INPUT("Codec Input Pin2"),
0335     SND_SOC_DAPM_INPUT("Codec Input Pin3"),
0336 
0337     /* Output Pins */
0338     SND_SOC_DAPM_OUTPUT("Codec Output Pin1"),
0339     SND_SOC_DAPM_OUTPUT("Codec Output Pin2"),
0340     SND_SOC_DAPM_OUTPUT("Codec Output Pin3"),
0341 };
0342 
0343 static struct snd_soc_dai_driver card_binder_dai = {
0344     .id = -1,
0345     .name = "codec-probing-DAI",
0346 };
0347 
0348 static int hda_hdev_attach(struct hdac_device *hdev)
0349 {
0350     struct hda_codec *codec = dev_to_hda_codec(&hdev->dev);
0351     struct snd_soc_component_driver *comp_drv;
0352 
0353     comp_drv = devm_kzalloc(&hdev->dev, sizeof(*comp_drv), GFP_KERNEL);
0354     if (!comp_drv)
0355         return -ENOMEM;
0356 
0357     /*
0358      * It's save to rely on dev_name() rather than a copy as component
0359      * driver's lifetime is directly tied to hda codec one
0360      */
0361     comp_drv->name = dev_name(&hdev->dev);
0362     comp_drv->probe = hda_codec_probe;
0363     comp_drv->remove = hda_codec_remove;
0364     comp_drv->idle_bias_on = false;
0365     if (!hda_codec_is_display(codec)) {
0366         comp_drv->dapm_widgets = hda_dapm_widgets;
0367         comp_drv->num_dapm_widgets = ARRAY_SIZE(hda_dapm_widgets);
0368         comp_drv->dapm_routes = hda_dapm_routes;
0369         comp_drv->num_dapm_routes = ARRAY_SIZE(hda_dapm_routes);
0370     }
0371 
0372     return snd_soc_register_component(&hdev->dev, comp_drv, &card_binder_dai, 1);
0373 }
0374 
0375 static int hda_hdev_detach(struct hdac_device *hdev)
0376 {
0377     struct hda_codec *codec = dev_to_hda_codec(&hdev->dev);
0378 
0379     if (codec->core.registered)
0380         cancel_delayed_work_sync(&codec->jackpoll_work);
0381 
0382     snd_soc_unregister_component(&hdev->dev);
0383 
0384     return 0;
0385 }
0386 
0387 const struct hdac_ext_bus_ops soc_hda_ext_bus_ops = {
0388     .hdev_attach = hda_hdev_attach,
0389     .hdev_detach = hda_hdev_detach,
0390 };
0391 EXPORT_SYMBOL_GPL(soc_hda_ext_bus_ops);
0392 
0393 MODULE_DESCRIPTION("HD-Audio codec driver");
0394 MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>");
0395 MODULE_LICENSE("GPL");