xref: /linux/sound/soc/intel/boards/skl_hda_dsp_generic.c (revision afeea2758b4f1210361ce2a91d8fa3e7df606ad2)
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright(c) 2015-18 Intel Corporation.
3 
4 /*
5  * Machine Driver for SKL+ platforms with DSP and iDisp, HDA Codecs
6  */
7 
8 #include <linux/module.h>
9 #include <linux/platform_device.h>
10 #include <sound/core.h>
11 #include <sound/jack.h>
12 #include <sound/pcm.h>
13 #include <sound/pcm_params.h>
14 #include <sound/soc.h>
15 #include <sound/soc-acpi.h>
16 #include "../../codecs/hdac_hdmi.h"
17 #include "skl_hda_dsp_common.h"
18 
19 static const struct snd_soc_dapm_widget skl_hda_widgets[] = {
20 	SND_SOC_DAPM_HP("Analog Out", NULL),
21 	SND_SOC_DAPM_MIC("Analog In", NULL),
22 	SND_SOC_DAPM_HP("Alt Analog Out", NULL),
23 	SND_SOC_DAPM_MIC("Alt Analog In", NULL),
24 	SND_SOC_DAPM_SPK("Digital Out", NULL),
25 	SND_SOC_DAPM_MIC("Digital In", NULL),
26 	SND_SOC_DAPM_MIC("SoC DMIC", NULL),
27 };
28 
29 static const struct snd_soc_dapm_route skl_hda_map[] = {
30 	{ "hifi3", NULL, "iDisp3 Tx"},
31 	{ "iDisp3 Tx", NULL, "iDisp3_out"},
32 	{ "hifi2", NULL, "iDisp2 Tx"},
33 	{ "iDisp2 Tx", NULL, "iDisp2_out"},
34 	{ "hifi1", NULL, "iDisp1 Tx"},
35 	{ "iDisp1 Tx", NULL, "iDisp1_out"},
36 
37 	{ "Analog Out", NULL, "Codec Output Pin1" },
38 	{ "Digital Out", NULL, "Codec Output Pin2" },
39 	{ "Alt Analog Out", NULL, "Codec Output Pin3" },
40 
41 	{ "Codec Input Pin1", NULL, "Analog In" },
42 	{ "Codec Input Pin2", NULL, "Digital In" },
43 	{ "Codec Input Pin3", NULL, "Alt Analog In" },
44 
45 	/* digital mics */
46 	{"DMic", NULL, "SoC DMIC"},
47 
48 	/* CODEC BE connections */
49 	{ "Analog Codec Playback", NULL, "Analog CPU Playback" },
50 	{ "Analog CPU Playback", NULL, "codec0_out" },
51 	{ "Digital Codec Playback", NULL, "Digital CPU Playback" },
52 	{ "Digital CPU Playback", NULL, "codec1_out" },
53 	{ "Alt Analog Codec Playback", NULL, "Alt Analog CPU Playback" },
54 	{ "Alt Analog CPU Playback", NULL, "codec2_out" },
55 
56 	{ "codec0_in", NULL, "Analog CPU Capture" },
57 	{ "Analog CPU Capture", NULL, "Analog Codec Capture" },
58 	{ "codec1_in", NULL, "Digital CPU Capture" },
59 	{ "Digital CPU Capture", NULL, "Digital Codec Capture" },
60 	{ "codec2_in", NULL, "Alt Analog CPU Capture" },
61 	{ "Alt Analog CPU Capture", NULL, "Alt Analog Codec Capture" },
62 };
63 
64 static int skl_hda_card_late_probe(struct snd_soc_card *card)
65 {
66 	return skl_hda_hdmi_jack_init(card);
67 }
68 
69 static int
70 skl_hda_add_dai_link(struct snd_soc_card *card, struct snd_soc_dai_link *link)
71 {
72 	struct skl_hda_private *ctx = snd_soc_card_get_drvdata(card);
73 	int ret = 0;
74 
75 	dev_dbg(card->dev, "dai link name - %s\n", link->name);
76 	link->platforms->name = ctx->platform_name;
77 	link->nonatomic = 1;
78 
79 	if (!ctx->idisp_codec)
80 		return 0;
81 
82 	if (strstr(link->name, "HDMI")) {
83 		ret = skl_hda_hdmi_add_pcm(card, ctx->pcm_count);
84 
85 		if (ret < 0)
86 			return ret;
87 
88 		ctx->dai_index++;
89 	}
90 
91 	ctx->pcm_count++;
92 	return ret;
93 }
94 
95 #define IDISP_DAI_COUNT		3
96 #define HDAC_DAI_COUNT		2
97 #define DMIC_DAI_COUNT		2
98 
99 /* there are two routes per iDisp output */
100 #define IDISP_ROUTE_COUNT	(IDISP_DAI_COUNT * 2)
101 #define IDISP_CODEC_MASK	0x4
102 
103 #define HDA_CODEC_AUTOSUSPEND_DELAY_MS 1000
104 
105 static int skl_hda_fill_card_info(struct snd_soc_card *card,
106 				  struct snd_soc_acpi_mach_params *mach_params)
107 {
108 	struct skl_hda_private *ctx = snd_soc_card_get_drvdata(card);
109 	struct snd_soc_dai_link *dai_link;
110 	u32 codec_count, codec_mask;
111 	int i, num_links, num_route;
112 
113 	codec_mask = mach_params->codec_mask;
114 	codec_count = hweight_long(codec_mask);
115 	ctx->idisp_codec = !!(codec_mask & IDISP_CODEC_MASK);
116 
117 	if (!codec_count || codec_count > 2 ||
118 	    (codec_count == 2 && !ctx->idisp_codec))
119 		return -EINVAL;
120 
121 	if (codec_mask == IDISP_CODEC_MASK) {
122 		/* topology with iDisp as the only HDA codec */
123 		num_links = IDISP_DAI_COUNT + DMIC_DAI_COUNT;
124 		num_route = IDISP_ROUTE_COUNT;
125 
126 		/*
127 		 * rearrange the dai link array and make the
128 		 * dmic dai links follow idsp dai links for only
129 		 * num_links of dai links need to be registered
130 		 * to ASoC.
131 		 */
132 		for (i = 0; i < DMIC_DAI_COUNT; i++) {
133 			skl_hda_be_dai_links[IDISP_DAI_COUNT + i] =
134 				skl_hda_be_dai_links[IDISP_DAI_COUNT +
135 					HDAC_DAI_COUNT + i];
136 		}
137 	} else {
138 		/* topology with external and iDisp HDA codecs */
139 		num_links = ARRAY_SIZE(skl_hda_be_dai_links);
140 		num_route = ARRAY_SIZE(skl_hda_map);
141 		card->dapm_widgets = skl_hda_widgets;
142 		card->num_dapm_widgets = ARRAY_SIZE(skl_hda_widgets);
143 		if (!ctx->idisp_codec) {
144 			card->dapm_routes = &skl_hda_map[IDISP_ROUTE_COUNT];
145 			num_route -= IDISP_ROUTE_COUNT;
146 			for (i = 0; i < IDISP_DAI_COUNT; i++) {
147 				skl_hda_be_dai_links[i].codecs = &snd_soc_dummy_dlc;
148 				skl_hda_be_dai_links[i].num_codecs = 1;
149 			}
150 		}
151 	}
152 
153 	card->num_links = num_links;
154 	card->num_dapm_routes = num_route;
155 
156 	for_each_card_prelinks(card, i, dai_link)
157 		dai_link->platforms->name = mach_params->platform;
158 
159 	return 0;
160 }
161 
162 static void skl_set_hda_codec_autosuspend_delay(struct snd_soc_card *card)
163 {
164 	struct snd_soc_pcm_runtime *rtd;
165 	struct hdac_hda_priv *hda_pvt;
166 	struct snd_soc_dai *dai;
167 
168 	for_each_card_rtds(card, rtd) {
169 		if (!strstr(rtd->dai_link->codecs->name, "ehdaudio0D0"))
170 			continue;
171 		dai = snd_soc_rtd_to_codec(rtd, 0);
172 		hda_pvt = snd_soc_component_get_drvdata(dai->component);
173 		if (hda_pvt) {
174 			/*
175 			 * all codecs are on the same bus, so it's sufficient
176 			 * to look up only the first one
177 			 */
178 			snd_hda_set_power_save(hda_pvt->codec->bus,
179 					       HDA_CODEC_AUTOSUSPEND_DELAY_MS);
180 			break;
181 		}
182 	}
183 }
184 
185 static int skl_hda_audio_probe(struct platform_device *pdev)
186 {
187 	struct snd_soc_acpi_mach *mach;
188 	struct skl_hda_private *ctx;
189 	struct snd_soc_card *card;
190 	int ret;
191 
192 	dev_dbg(&pdev->dev, "entry\n");
193 
194 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
195 	if (!ctx)
196 		return -ENOMEM;
197 
198 	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
199 
200 	mach = pdev->dev.platform_data;
201 	if (!mach)
202 		return -EINVAL;
203 
204 	card = &ctx->card;
205 	card->name = "hda-dsp",
206 	card->owner = THIS_MODULE,
207 	card->dai_link = skl_hda_be_dai_links,
208 	card->dapm_widgets = skl_hda_widgets,
209 	card->dapm_routes = skl_hda_map,
210 	card->add_dai_link = skl_hda_add_dai_link,
211 	card->fully_routed = true,
212 	card->late_probe = skl_hda_card_late_probe,
213 
214 	snd_soc_card_set_drvdata(card, ctx);
215 
216 	ret = skl_hda_fill_card_info(card, &mach->mach_params);
217 	if (ret < 0) {
218 		dev_err(&pdev->dev, "Unsupported HDAudio/iDisp configuration found\n");
219 		return ret;
220 	}
221 
222 	ctx->pcm_count = card->num_links;
223 	ctx->dai_index = 1; /* hdmi codec dai name starts from index 1 */
224 	ctx->platform_name = mach->mach_params.platform;
225 	ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
226 
227 	card->dev = &pdev->dev;
228 	if (!snd_soc_acpi_sof_parent(&pdev->dev))
229 		card->disable_route_checks = true;
230 
231 	if (mach->mach_params.dmic_num > 0) {
232 		card->components = devm_kasprintf(card->dev, GFP_KERNEL,
233 						  "cfg-dmics:%d",
234 						  mach->mach_params.dmic_num);
235 		if (!card->components)
236 			return -ENOMEM;
237 	}
238 
239 	ret = devm_snd_soc_register_card(&pdev->dev, card);
240 	if (!ret)
241 		skl_set_hda_codec_autosuspend_delay(card);
242 
243 	return ret;
244 }
245 
246 static struct platform_driver skl_hda_audio = {
247 	.probe = skl_hda_audio_probe,
248 	.driver = {
249 		.name = "skl_hda_dsp_generic",
250 		.pm = &snd_soc_pm_ops,
251 	},
252 };
253 
254 module_platform_driver(skl_hda_audio)
255 
256 /* Module information */
257 MODULE_DESCRIPTION("SKL/KBL/BXT/APL HDA Generic Machine driver");
258 MODULE_AUTHOR("Rakesh Ughreja <rakesh.a.ughreja@intel.com>");
259 MODULE_LICENSE("GPL v2");
260 MODULE_ALIAS("platform:skl_hda_dsp_generic");
261 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
262