1 // SPDX-License-Identifier: GPL-2.0-only
2 //
3 // Copyright(c) 2019 Intel Corporation. All rights reserved.
4 
5 #include <sound/pcm.h>
6 #include <sound/soc.h>
7 #include <sound/hda_codec.h>
8 #include <sound/hda_i915.h>
9 #include "../../codecs/hdac_hda.h"
10 
11 #include "hda_dsp_common.h"
12 
13 #if IS_ENABLED(CONFIG_SND_SOC_SOF_HDA_AUDIO_CODEC)
14 
15 /*
16  * Search card topology and return PCM device number
17  * matching Nth HDMI device (zero-based index).
18  */
19 static struct snd_pcm *hda_dsp_hdmi_pcm_handle(struct snd_soc_card *card,
20 					       int hdmi_idx)
21 {
22 	struct snd_soc_pcm_runtime *rtd;
23 	struct snd_pcm *spcm;
24 	int i = 0;
25 
26 	for_each_card_rtds(card, rtd) {
27 		spcm = rtd->pcm ?
28 			rtd->pcm->streams[SNDRV_PCM_STREAM_PLAYBACK].pcm : NULL;
29 		if (spcm && strstr(spcm->id, "HDMI")) {
30 			if (i == hdmi_idx)
31 				return rtd->pcm;
32 			++i;
33 		}
34 	}
35 
36 	return NULL;
37 }
38 
39 /*
40  * Search card topology and register HDMI PCM related controls
41  * to codec driver.
42  */
43 int hda_dsp_hdmi_build_controls(struct snd_soc_card *card,
44 				struct snd_soc_component *comp)
45 {
46 	struct hdac_hda_priv *hda_pvt;
47 	struct hda_codec *hcodec;
48 	struct snd_pcm *spcm;
49 	struct hda_pcm *hpcm;
50 	int err = 0, i = 0;
51 
52 	if (!comp)
53 		return -EINVAL;
54 
55 	hda_pvt = snd_soc_component_get_drvdata(comp);
56 	hcodec = &hda_pvt->codec;
57 
58 	list_for_each_entry(hpcm, &hcodec->pcm_list_head, list) {
59 		spcm = hda_dsp_hdmi_pcm_handle(card, i);
60 		if (spcm) {
61 			hpcm->pcm = spcm;
62 			hpcm->device = spcm->device;
63 			dev_dbg(card->dev,
64 				"%s: mapping HDMI converter %d to PCM %d (%p)\n",
65 				__func__, i, hpcm->device, spcm);
66 		} else {
67 			hpcm->pcm = NULL;
68 			hpcm->device = SNDRV_PCM_INVALID_DEVICE;
69 			dev_warn(card->dev,
70 				 "%s: no PCM in topology for HDMI converter %d\n\n",
71 				 __func__, i);
72 		}
73 		i++;
74 	}
75 	snd_hdac_display_power(hcodec->core.bus,
76 			       HDA_CODEC_IDX_CONTROLLER, true);
77 	err = snd_hda_codec_build_controls(hcodec);
78 	if (err < 0)
79 		dev_err(card->dev, "unable to create controls %d\n", err);
80 	snd_hdac_display_power(hcodec->core.bus,
81 			       HDA_CODEC_IDX_CONTROLLER, false);
82 
83 	return err;
84 }
85 
86 #endif
87