xref: /linux/sound/soc/intel/boards/skl_hda_dsp_generic.c (revision a2a0312ac9ee3eb477a02f2706673cab6d375b07)
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->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 #define IDISP_CODEC_MASK	0x4
102 
103 #define HDA_CODEC_AUTOSUSPEND_DELAY_MS 1000
104 
105 static int skl_hda_fill_card_info(struct device *dev, 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 	struct snd_soc_dai_link *bt_link;
111 	u32 codec_count, codec_mask;
112 	int i, num_links, num_route;
113 
114 	codec_mask = mach_params->codec_mask;
115 	codec_count = hweight_long(codec_mask);
116 	ctx->idisp_codec = !!(codec_mask & IDISP_CODEC_MASK);
117 
118 	if (!codec_count || codec_count > 2 ||
119 	    (codec_count == 2 && !ctx->idisp_codec))
120 		return -EINVAL;
121 
122 	if (codec_mask == IDISP_CODEC_MASK) {
123 		/* topology with iDisp as the only HDA codec */
124 		num_links = IDISP_DAI_COUNT + DMIC_DAI_COUNT + BT_DAI_COUNT;
125 		num_route = IDISP_ROUTE_COUNT;
126 
127 		/*
128 		 * rearrange the dai link array and make the
129 		 * dmic dai links follow idsp dai links for only
130 		 * num_links of dai links need to be registered
131 		 * to ASoC.
132 		 */
133 		for (i = 0; i < (DMIC_DAI_COUNT + BT_DAI_COUNT); i++) {
134 			skl_hda_be_dai_links[IDISP_DAI_COUNT + i] =
135 				skl_hda_be_dai_links[IDISP_DAI_COUNT +
136 					HDAC_DAI_COUNT + i];
137 		}
138 	} else {
139 		/* topology with external and iDisp HDA codecs */
140 		num_links = ARRAY_SIZE(skl_hda_be_dai_links);
141 		num_route = ARRAY_SIZE(skl_hda_map);
142 		card->dapm_widgets = skl_hda_widgets;
143 		card->num_dapm_widgets = ARRAY_SIZE(skl_hda_widgets);
144 		if (!ctx->idisp_codec) {
145 			card->dapm_routes = &skl_hda_map[IDISP_ROUTE_COUNT];
146 			num_route -= IDISP_ROUTE_COUNT;
147 			for (i = 0; i < IDISP_DAI_COUNT; i++) {
148 				skl_hda_be_dai_links[i].codecs = &snd_soc_dummy_dlc;
149 				skl_hda_be_dai_links[i].num_codecs = 1;
150 			}
151 		}
152 	}
153 
154 	if (!ctx->bt_offload_present) {
155 		/* remove last link since bt audio offload is not supported */
156 		num_links -= BT_DAI_COUNT;
157 	} else {
158 		if (codec_mask == IDISP_CODEC_MASK)
159 			bt_link = &skl_hda_be_dai_links[IDISP_DAI_COUNT + DMIC_DAI_COUNT];
160 		else
161 			bt_link = &skl_hda_be_dai_links[IDISP_DAI_COUNT + HDAC_DAI_COUNT + DMIC_DAI_COUNT];
162 
163 		/* complete the link name and dai name with SSP port number */
164 		bt_link->name = devm_kasprintf(dev, GFP_KERNEL, "SSP%d-BT",
165 					       ctx->ssp_bt);
166 		if (!bt_link->name)
167 			return -ENOMEM;
168 
169 		bt_link->cpus->dai_name = devm_kasprintf(dev, GFP_KERNEL,
170 							 "SSP%d Pin",
171 							 ctx->ssp_bt);
172 		if (!bt_link->cpus->dai_name)
173 			return -ENOMEM;
174 	}
175 
176 	card->num_links = num_links;
177 	card->num_dapm_routes = num_route;
178 
179 	for_each_card_prelinks(card, i, dai_link)
180 		dai_link->platforms->name = mach_params->platform;
181 
182 	return 0;
183 }
184 
185 static void skl_set_hda_codec_autosuspend_delay(struct snd_soc_card *card)
186 {
187 	struct snd_soc_pcm_runtime *rtd;
188 	struct hdac_hda_priv *hda_pvt;
189 	struct snd_soc_dai *dai;
190 
191 	for_each_card_rtds(card, rtd) {
192 		if (!strstr(rtd->dai_link->codecs->name, "ehdaudio0D0"))
193 			continue;
194 		dai = snd_soc_rtd_to_codec(rtd, 0);
195 		hda_pvt = snd_soc_component_get_drvdata(dai->component);
196 		if (hda_pvt) {
197 			/*
198 			 * all codecs are on the same bus, so it's sufficient
199 			 * to look up only the first one
200 			 */
201 			snd_hda_set_power_save(hda_pvt->codec->bus,
202 					       HDA_CODEC_AUTOSUSPEND_DELAY_MS);
203 			break;
204 		}
205 	}
206 }
207 
208 static int skl_hda_audio_probe(struct platform_device *pdev)
209 {
210 	struct snd_soc_acpi_mach *mach = pdev->dev.platform_data;
211 	struct skl_hda_private *ctx;
212 	struct snd_soc_card *card;
213 	int ret;
214 
215 	dev_dbg(&pdev->dev, "entry\n");
216 
217 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
218 	if (!ctx)
219 		return -ENOMEM;
220 
221 	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
222 
223 	card = &ctx->card;
224 	card->name = "hda-dsp",
225 	card->owner = THIS_MODULE,
226 	card->dai_link = skl_hda_be_dai_links,
227 	card->dapm_widgets = skl_hda_widgets,
228 	card->dapm_routes = skl_hda_map,
229 	card->add_dai_link = skl_hda_add_dai_link,
230 	card->fully_routed = true,
231 	card->late_probe = skl_hda_card_late_probe,
232 
233 	snd_soc_card_set_drvdata(card, ctx);
234 
235 	if (hweight_long(mach->mach_params.bt_link_mask) == 1) {
236 		ctx->bt_offload_present = true;
237 		ctx->ssp_bt = fls(mach->mach_params.bt_link_mask) - 1;
238 	}
239 
240 	ret = skl_hda_fill_card_info(&pdev->dev, card, &mach->mach_params);
241 	if (ret < 0) {
242 		dev_err(&pdev->dev, "Unsupported HDAudio/iDisp configuration found\n");
243 		return ret;
244 	}
245 
246 	ctx->pcm_count = card->num_links;
247 	ctx->dai_index = 1; /* hdmi codec dai name starts from index 1 */
248 	ctx->platform_name = mach->mach_params.platform;
249 
250 	card->dev = &pdev->dev;
251 	if (!snd_soc_acpi_sof_parent(&pdev->dev))
252 		card->disable_route_checks = true;
253 
254 	if (mach->mach_params.dmic_num > 0) {
255 		card->components = devm_kasprintf(card->dev, GFP_KERNEL,
256 						  "cfg-dmics:%d",
257 						  mach->mach_params.dmic_num);
258 		if (!card->components)
259 			return -ENOMEM;
260 	}
261 
262 	ret = devm_snd_soc_register_card(&pdev->dev, card);
263 	if (!ret)
264 		skl_set_hda_codec_autosuspend_delay(card);
265 
266 	return ret;
267 }
268 
269 static struct platform_driver skl_hda_audio = {
270 	.probe = skl_hda_audio_probe,
271 	.driver = {
272 		.name = "skl_hda_dsp_generic",
273 		.pm = &snd_soc_pm_ops,
274 	},
275 };
276 
277 module_platform_driver(skl_hda_audio)
278 
279 /* Module information */
280 MODULE_DESCRIPTION("SKL/KBL/BXT/APL HDA Generic Machine driver");
281 MODULE_AUTHOR("Rakesh Ughreja <rakesh.a.ughreja@intel.com>");
282 MODULE_LICENSE("GPL v2");
283 MODULE_ALIAS("platform:skl_hda_dsp_generic");
284 MODULE_IMPORT_NS(SND_SOC_INTEL_HDA_DSP_COMMON);
285