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