Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 //
0003 // Copyright(c) 2021-2022 Intel Corporation. All rights reserved.
0004 //
0005 // Authors: Cezary Rojewski <cezary.rojewski@intel.com>
0006 //          Amadeusz Slawinski <amadeuszx.slawinski@linux.intel.com>
0007 //
0008 
0009 #include <linux/platform_device.h>
0010 #include <sound/hda_codec.h>
0011 #include <sound/hda_i915.h>
0012 #include <sound/soc.h>
0013 #include <sound/soc-acpi.h>
0014 #include "../../../codecs/hda.h"
0015 
0016 static int avs_create_dai_links(struct device *dev, struct hda_codec *codec, int pcm_count,
0017                 const char *platform_name, struct snd_soc_dai_link **links)
0018 {
0019     struct snd_soc_dai_link_component *platform;
0020     struct snd_soc_dai_link *dl;
0021     struct hda_pcm *pcm;
0022     const char *cname = dev_name(&codec->core.dev);
0023     int i;
0024 
0025     dl = devm_kcalloc(dev, pcm_count, sizeof(*dl), GFP_KERNEL);
0026     platform = devm_kzalloc(dev, sizeof(*platform), GFP_KERNEL);
0027     if (!dl || !platform)
0028         return -ENOMEM;
0029 
0030     platform->name = platform_name;
0031     pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
0032 
0033     for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
0034         dl[i].name = devm_kasprintf(dev, GFP_KERNEL, "%s link%d", cname, i);
0035         if (!dl[i].name)
0036             return -ENOMEM;
0037 
0038         dl[i].id = i;
0039         dl[i].nonatomic = 1;
0040         dl[i].no_pcm = 1;
0041         dl[i].dpcm_playback = 1;
0042         dl[i].dpcm_capture = 1;
0043         dl[i].platforms = platform;
0044         dl[i].num_platforms = 1;
0045 
0046         dl[i].codecs = devm_kzalloc(dev, sizeof(*dl->codecs), GFP_KERNEL);
0047         dl[i].cpus = devm_kzalloc(dev, sizeof(*dl->cpus), GFP_KERNEL);
0048         if (!dl[i].codecs || !dl[i].cpus)
0049             return -ENOMEM;
0050 
0051         dl[i].cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL, "%s-cpu%d", cname, i);
0052         if (!dl[i].cpus->dai_name)
0053             return -ENOMEM;
0054 
0055         dl[i].codecs->name = devm_kstrdup(dev, cname, GFP_KERNEL);
0056         dl[i].codecs->dai_name = pcm->name;
0057         dl[i].num_codecs = 1;
0058         dl[i].num_cpus = 1;
0059     }
0060 
0061     *links = dl;
0062     return 0;
0063 }
0064 
0065 static int avs_create_dapm_routes(struct device *dev, struct hda_codec *codec, int pcm_count,
0066                   struct snd_soc_dapm_route **routes, int *num_routes)
0067 {
0068     struct snd_soc_dapm_route *dr;
0069     struct hda_pcm *pcm;
0070     const char *cname = dev_name(&codec->core.dev);
0071     int i, n = 0;
0072 
0073     /* at max twice the number of pcms */
0074     dr = devm_kcalloc(dev, pcm_count * 2, sizeof(*dr), GFP_KERNEL);
0075     if (!dr)
0076         return -ENOMEM;
0077 
0078     pcm = list_first_entry(&codec->pcm_list_head, struct hda_pcm, list);
0079 
0080     for (i = 0; i < pcm_count; i++, pcm = list_next_entry(pcm, list)) {
0081         struct hda_pcm_stream *stream;
0082         int dir;
0083 
0084         dir = SNDRV_PCM_STREAM_PLAYBACK;
0085         stream = &pcm->stream[dir];
0086         if (!stream->substreams)
0087             goto capture_routes;
0088 
0089         dr[n].sink = devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
0090                         snd_pcm_direction_name(dir));
0091         dr[n].source = devm_kasprintf(dev, GFP_KERNEL, "%s-cpu%d Tx", cname, i);
0092         if (!dr[n].sink || !dr[n].source)
0093             return -ENOMEM;
0094         n++;
0095 
0096 capture_routes:
0097         dir = SNDRV_PCM_STREAM_CAPTURE;
0098         stream = &pcm->stream[dir];
0099         if (!stream->substreams)
0100             continue;
0101 
0102         dr[n].sink = devm_kasprintf(dev, GFP_KERNEL, "%s-cpu%d Rx", cname, i);
0103         dr[n].source = devm_kasprintf(dev, GFP_KERNEL, "%s %s", pcm->name,
0104                           snd_pcm_direction_name(dir));
0105         if (!dr[n].sink || !dr[n].source)
0106             return -ENOMEM;
0107         n++;
0108     }
0109 
0110     *routes = dr;
0111     *num_routes = n;
0112     return 0;
0113 }
0114 
0115 /* Should be aligned with SectionPCM's name from topology */
0116 #define FEDAI_NAME_PREFIX "HDMI"
0117 
0118 static struct snd_pcm *
0119 avs_card_hdmi_pcm_at(struct snd_soc_card *card, int hdmi_idx)
0120 {
0121     struct snd_soc_pcm_runtime *rtd;
0122     int dir = SNDRV_PCM_STREAM_PLAYBACK;
0123 
0124     for_each_card_rtds(card, rtd) {
0125         struct snd_pcm *spcm;
0126         int ret, n;
0127 
0128         spcm = rtd->pcm ? rtd->pcm->streams[dir].pcm : NULL;
0129         if (!spcm || !strstr(spcm->id, FEDAI_NAME_PREFIX))
0130             continue;
0131 
0132         ret = sscanf(spcm->id, FEDAI_NAME_PREFIX "%d", &n);
0133         if (ret != 1)
0134             continue;
0135         if (n == hdmi_idx)
0136             return rtd->pcm;
0137     }
0138 
0139     return NULL;
0140 }
0141 
0142 static int avs_card_late_probe(struct snd_soc_card *card)
0143 {
0144     struct snd_soc_acpi_mach *mach = dev_get_platdata(card->dev);
0145     struct hda_codec *codec = mach->pdata;
0146     struct hda_pcm *hpcm;
0147     /* Topology pcm indexing is 1-based */
0148     int i = 1;
0149 
0150     list_for_each_entry(hpcm, &codec->pcm_list_head, list) {
0151         struct snd_pcm *spcm;
0152 
0153         spcm = avs_card_hdmi_pcm_at(card, i);
0154         if (spcm) {
0155             hpcm->pcm = spcm;
0156             hpcm->device = spcm->device;
0157             dev_info(card->dev, "%s: mapping HDMI converter %d to PCM %d (%p)\n",
0158                  __func__, i, hpcm->device, spcm);
0159         } else {
0160             hpcm->pcm = NULL;
0161             hpcm->device = SNDRV_PCM_INVALID_DEVICE;
0162             dev_warn(card->dev, "%s: no PCM in topology for HDMI converter %d\n",
0163                  __func__, i);
0164         }
0165         i++;
0166     }
0167 
0168     return hda_codec_probe_complete(codec);
0169 }
0170 
0171 static int avs_probing_link_init(struct snd_soc_pcm_runtime *rtm)
0172 {
0173     struct snd_soc_dapm_route *routes;
0174     struct snd_soc_acpi_mach *mach;
0175     struct snd_soc_dai_link *links = NULL;
0176     struct snd_soc_card *card = rtm->card;
0177     struct hda_codec *codec;
0178     struct hda_pcm *pcm;
0179     int ret, n, pcm_count = 0;
0180 
0181     mach = dev_get_platdata(card->dev);
0182     codec = mach->pdata;
0183 
0184     if (list_empty(&codec->pcm_list_head))
0185         return -EINVAL;
0186     list_for_each_entry(pcm, &codec->pcm_list_head, list)
0187         pcm_count++;
0188 
0189     ret = avs_create_dai_links(card->dev, codec, pcm_count, mach->mach_params.platform, &links);
0190     if (ret < 0) {
0191         dev_err(card->dev, "create links failed: %d\n", ret);
0192         return ret;
0193     }
0194 
0195     for (n = 0; n < pcm_count; n++) {
0196         ret = snd_soc_add_pcm_runtime(card, &links[n]);
0197         if (ret < 0) {
0198             dev_err(card->dev, "add links failed: %d\n", ret);
0199             return ret;
0200         }
0201     }
0202 
0203     ret = avs_create_dapm_routes(card->dev, codec, pcm_count, &routes, &n);
0204     if (ret < 0) {
0205         dev_err(card->dev, "create routes failed: %d\n", ret);
0206         return ret;
0207     }
0208 
0209     ret = snd_soc_dapm_add_routes(&card->dapm, routes, n);
0210     if (ret < 0) {
0211         dev_err(card->dev, "add routes failed: %d\n", ret);
0212         return ret;
0213     }
0214 
0215     return 0;
0216 }
0217 
0218 SND_SOC_DAILINK_DEF(dummy, DAILINK_COMP_ARRAY(COMP_DUMMY()));
0219 
0220 static struct snd_soc_dai_link probing_link = {
0221     .name = "probing-LINK",
0222     .id = -1,
0223     .nonatomic = 1,
0224     .no_pcm = 1,
0225     .dpcm_playback = 1,
0226     .dpcm_capture = 1,
0227     .cpus = dummy,
0228     .num_cpus = ARRAY_SIZE(dummy),
0229     .init = avs_probing_link_init,
0230 };
0231 
0232 static int avs_hdaudio_probe(struct platform_device *pdev)
0233 {
0234     struct snd_soc_dai_link *binder;
0235     struct snd_soc_acpi_mach *mach;
0236     struct snd_soc_card *card;
0237     struct device *dev = &pdev->dev;
0238     struct hda_codec *codec;
0239 
0240     mach = dev_get_platdata(dev);
0241     codec = mach->pdata;
0242 
0243     /* codec may be unloaded before card's probe() fires */
0244     if (!device_is_registered(&codec->core.dev))
0245         return -ENODEV;
0246 
0247     binder = devm_kmemdup(dev, &probing_link, sizeof(probing_link), GFP_KERNEL);
0248     if (!binder)
0249         return -ENOMEM;
0250 
0251     binder->platforms = devm_kzalloc(dev, sizeof(*binder->platforms), GFP_KERNEL);
0252     binder->codecs = devm_kzalloc(dev, sizeof(*binder->codecs), GFP_KERNEL);
0253     if (!binder->platforms || !binder->codecs)
0254         return -ENOMEM;
0255 
0256     binder->codecs->name = devm_kstrdup(dev, dev_name(&codec->core.dev), GFP_KERNEL);
0257     if (!binder->codecs->name)
0258         return -ENOMEM;
0259 
0260     binder->platforms->name = mach->mach_params.platform;
0261     binder->num_platforms = 1;
0262     binder->codecs->dai_name = "codec-probing-DAI";
0263     binder->num_codecs = 1;
0264 
0265     card = devm_kzalloc(dev, sizeof(*card), GFP_KERNEL);
0266     if (!card)
0267         return -ENOMEM;
0268 
0269     card->name = binder->codecs->name;
0270     card->dev = dev;
0271     card->owner = THIS_MODULE;
0272     card->dai_link = binder;
0273     card->num_links = 1;
0274     card->fully_routed = true;
0275     if (hda_codec_is_display(codec))
0276         card->late_probe = avs_card_late_probe;
0277 
0278     return devm_snd_soc_register_card(dev, card);
0279 }
0280 
0281 static struct platform_driver avs_hdaudio_driver = {
0282     .probe = avs_hdaudio_probe,
0283     .driver = {
0284         .name = "avs_hdaudio",
0285         .pm = &snd_soc_pm_ops,
0286     },
0287 };
0288 
0289 module_platform_driver(avs_hdaudio_driver)
0290 
0291 MODULE_DESCRIPTION("Intel HD-Audio machine driver");
0292 MODULE_AUTHOR("Cezary Rojewski <cezary.rojewski@intel.com>");
0293 MODULE_LICENSE("GPL");
0294 MODULE_ALIAS("platform:avs_hdaudio");