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 SND_SOC_DAILINK_DEF(dummy_codec,
65 	DAILINK_COMP_ARRAY(COMP_CODEC("snd-soc-dummy", "snd-soc-dummy-dai")));
66 
67 static int skl_hda_card_late_probe(struct snd_soc_card *card)
68 {
69 	return skl_hda_hdmi_jack_init(card);
70 }
71 
72 static int
73 skl_hda_add_dai_link(struct snd_soc_card *card, struct snd_soc_dai_link *link)
74 {
75 	struct skl_hda_private *ctx = snd_soc_card_get_drvdata(card);
76 	int ret = 0;
77 
78 	dev_dbg(card->dev, "%s: dai link name - %s\n", __func__, link->name);
79 	link->platforms->name = ctx->platform_name;
80 	link->nonatomic = 1;
81 
82 	if (!ctx->idisp_codec)
83 		return 0;
84 
85 	if (strstr(link->name, "HDMI")) {
86 		ret = skl_hda_hdmi_add_pcm(card, ctx->pcm_count);
87 
88 		if (ret < 0)
89 			return ret;
90 
91 		ctx->dai_index++;
92 	}
93 
94 	ctx->pcm_count++;
95 	return ret;
96 }
97 
98 static struct snd_soc_card hda_soc_card = {
99 	.name = "hda-dsp",
100 	.owner = THIS_MODULE,
101 	.dai_link = skl_hda_be_dai_links,
102 	.dapm_widgets = skl_hda_widgets,
103 	.dapm_routes = skl_hda_map,
104 	.add_dai_link = skl_hda_add_dai_link,
105 	.fully_routed = true,
106 	.late_probe = skl_hda_card_late_probe,
107 };
108 
109 static char hda_soc_components[30];
110 
111 #define IDISP_DAI_COUNT		3
112 #define HDAC_DAI_COUNT		2
113 #define DMIC_DAI_COUNT		2
114 
115 /* there are two routes per iDisp output */
116 #define IDISP_ROUTE_COUNT	(IDISP_DAI_COUNT * 2)
117 #define IDISP_CODEC_MASK	0x4
118 
119 #define HDA_CODEC_AUTOSUSPEND_DELAY_MS 1000
120 
121 static int skl_hda_fill_card_info(struct snd_soc_acpi_mach_params *mach_params)
122 {
123 	struct snd_soc_card *card = &hda_soc_card;
124 	struct skl_hda_private *ctx = snd_soc_card_get_drvdata(card);
125 	struct snd_soc_dai_link *dai_link;
126 	u32 codec_count, codec_mask;
127 	int i, num_links, num_route;
128 
129 	codec_mask = mach_params->codec_mask;
130 	codec_count = hweight_long(codec_mask);
131 	ctx->idisp_codec = !!(codec_mask & IDISP_CODEC_MASK);
132 
133 	if (!codec_count || codec_count > 2 ||
134 	    (codec_count == 2 && !ctx->idisp_codec))
135 		return -EINVAL;
136 
137 	if (codec_mask == IDISP_CODEC_MASK) {
138 		/* topology with iDisp as the only HDA codec */
139 		num_links = IDISP_DAI_COUNT + DMIC_DAI_COUNT;
140 		num_route = IDISP_ROUTE_COUNT;
141 
142 		/*
143 		 * rearrange the dai link array and make the
144 		 * dmic dai links follow idsp dai links for only
145 		 * num_links of dai links need to be registered
146 		 * to ASoC.
147 		 */
148 		for (i = 0; i < DMIC_DAI_COUNT; i++) {
149 			skl_hda_be_dai_links[IDISP_DAI_COUNT + i] =
150 				skl_hda_be_dai_links[IDISP_DAI_COUNT +
151 					HDAC_DAI_COUNT + i];
152 		}
153 	} else {
154 		/* topology with external and iDisp HDA codecs */
155 		num_links = ARRAY_SIZE(skl_hda_be_dai_links);
156 		num_route = ARRAY_SIZE(skl_hda_map);
157 		card->dapm_widgets = skl_hda_widgets;
158 		card->num_dapm_widgets = ARRAY_SIZE(skl_hda_widgets);
159 		if (!ctx->idisp_codec) {
160 			for (i = 0; i < IDISP_DAI_COUNT; i++) {
161 				skl_hda_be_dai_links[i].codecs = dummy_codec;
162 				skl_hda_be_dai_links[i].num_codecs =
163 					ARRAY_SIZE(dummy_codec);
164 			}
165 		}
166 	}
167 
168 	card->num_links = num_links;
169 	card->num_dapm_routes = num_route;
170 
171 	for_each_card_prelinks(card, i, dai_link)
172 		dai_link->platforms->name = mach_params->platform;
173 
174 	return 0;
175 }
176 
177 static void skl_set_hda_codec_autosuspend_delay(struct snd_soc_card *card)
178 {
179 	struct snd_soc_pcm_runtime *rtd;
180 	struct hdac_hda_priv *hda_pvt;
181 	struct snd_soc_dai *dai;
182 
183 	for_each_card_rtds(card, rtd) {
184 		if (!strstr(rtd->dai_link->codecs->name, "ehdaudio0D0"))
185 			continue;
186 		dai = asoc_rtd_to_codec(rtd, 0);
187 		hda_pvt = snd_soc_component_get_drvdata(dai->component);
188 		if (hda_pvt) {
189 			/*
190 			 * all codecs are on the same bus, so it's sufficient
191 			 * to look up only the first one
192 			 */
193 			snd_hda_set_power_save(hda_pvt->codec.bus,
194 					       HDA_CODEC_AUTOSUSPEND_DELAY_MS);
195 			break;
196 		}
197 	}
198 }
199 
200 static int skl_hda_audio_probe(struct platform_device *pdev)
201 {
202 	struct snd_soc_acpi_mach *mach;
203 	struct skl_hda_private *ctx;
204 	int ret;
205 
206 	dev_dbg(&pdev->dev, "%s: entry\n", __func__);
207 
208 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
209 	if (!ctx)
210 		return -ENOMEM;
211 
212 	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
213 
214 	mach = pdev->dev.platform_data;
215 	if (!mach)
216 		return -EINVAL;
217 
218 	snd_soc_card_set_drvdata(&hda_soc_card, ctx);
219 
220 	ret = skl_hda_fill_card_info(&mach->mach_params);
221 	if (ret < 0) {
222 		dev_err(&pdev->dev, "Unsupported HDAudio/iDisp configuration found\n");
223 		return ret;
224 	}
225 
226 	ctx->pcm_count = hda_soc_card.num_links;
227 	ctx->dai_index = 1; /* hdmi codec dai name starts from index 1 */
228 	ctx->platform_name = mach->mach_params.platform;
229 	ctx->common_hdmi_codec_drv = mach->mach_params.common_hdmi_codec_drv;
230 
231 	hda_soc_card.dev = &pdev->dev;
232 
233 	if (mach->mach_params.dmic_num > 0) {
234 		snprintf(hda_soc_components, sizeof(hda_soc_components),
235 				"cfg-dmics:%d", mach->mach_params.dmic_num);
236 		hda_soc_card.components = hda_soc_components;
237 	}
238 
239 	ret = devm_snd_soc_register_card(&pdev->dev, &hda_soc_card);
240 	if (!ret)
241 		skl_set_hda_codec_autosuspend_delay(&hda_soc_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