1 // SPDX-License-Identifier: GPL-2.0
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 };
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 	/* CODEC BE connections */
45 	{ "Analog Codec Playback", NULL, "Analog CPU Playback" },
46 	{ "Analog CPU Playback", NULL, "codec0_out" },
47 	{ "Digital Codec Playback", NULL, "Digital CPU Playback" },
48 	{ "Digital CPU Playback", NULL, "codec1_out" },
49 	{ "Alt Analog Codec Playback", NULL, "Alt Analog CPU Playback" },
50 	{ "Alt Analog CPU Playback", NULL, "codec2_out" },
51 
52 	{ "codec0_in", NULL, "Analog CPU Capture" },
53 	{ "Analog CPU Capture", NULL, "Analog Codec Capture" },
54 	{ "codec1_in", NULL, "Digital CPU Capture" },
55 	{ "Digital CPU Capture", NULL, "Digital Codec Capture" },
56 	{ "codec2_in", NULL, "Alt Analog CPU Capture" },
57 	{ "Alt Analog CPU Capture", NULL, "Alt Analog Codec Capture" },
58 };
59 
60 static int skl_hda_card_late_probe(struct snd_soc_card *card)
61 {
62 	return skl_hda_hdmi_jack_init(card);
63 }
64 
65 static int
66 skl_hda_add_dai_link(struct snd_soc_card *card, struct snd_soc_dai_link *link)
67 {
68 	struct skl_hda_private *ctx = snd_soc_card_get_drvdata(card);
69 	int ret = 0;
70 
71 	dev_dbg(card->dev, "%s: dai link name - %s\n", __func__, link->name);
72 	link->platform_name = ctx->platform_name;
73 	link->nonatomic = 1;
74 
75 	if (strstr(link->name, "HDMI")) {
76 		ret = skl_hda_hdmi_add_pcm(card, ctx->pcm_count);
77 
78 		if (ret < 0)
79 			return ret;
80 
81 		ctx->dai_index++;
82 	}
83 
84 	ctx->pcm_count++;
85 	return ret;
86 }
87 
88 static struct snd_soc_card hda_soc_card = {
89 	.name = "skl_hda_card",
90 	.owner = THIS_MODULE,
91 	.dai_link = skl_hda_be_dai_links,
92 	.dapm_widgets = skl_hda_widgets,
93 	.dapm_routes = skl_hda_map,
94 	.add_dai_link = skl_hda_add_dai_link,
95 	.fully_routed = true,
96 	.late_probe = skl_hda_card_late_probe,
97 };
98 
99 #define IDISP_DAI_COUNT		3
100 #define HDAC_DAI_COUNT		2
101 #define DMIC_DAI_COUNT		2
102 
103 /* there are two routes per iDisp output */
104 #define IDISP_ROUTE_COUNT	(IDISP_DAI_COUNT * 2)
105 #define IDISP_CODEC_MASK	0x4
106 
107 static int skl_hda_fill_card_info(struct snd_soc_acpi_mach_params *mach_params)
108 {
109 	struct snd_soc_card *card = &hda_soc_card;
110 	struct snd_soc_dai_link *dai_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 
117 	if (codec_count == 1 && codec_mask & IDISP_CODEC_MASK) {
118 		num_links = IDISP_DAI_COUNT + DMIC_DAI_COUNT;
119 		num_route = IDISP_ROUTE_COUNT;
120 
121 		/*
122 		 * rearrange the dai link array and make the
123 		 * dmic dai links follow idsp dai links for only
124 		 * num_links of dai links need to be registered
125 		 * to ASoC.
126 		 */
127 		for (i = 0; i < DMIC_DAI_COUNT; i++) {
128 			skl_hda_be_dai_links[IDISP_DAI_COUNT + i] =
129 				skl_hda_be_dai_links[IDISP_DAI_COUNT +
130 					HDAC_DAI_COUNT + i];
131 		}
132 	} else if (codec_count == 2 && codec_mask & IDISP_CODEC_MASK) {
133 		num_links = ARRAY_SIZE(skl_hda_be_dai_links);
134 		num_route = ARRAY_SIZE(skl_hda_map);
135 		card->dapm_widgets = skl_hda_widgets;
136 		card->num_dapm_widgets = ARRAY_SIZE(skl_hda_widgets);
137 	} else {
138 		return -EINVAL;
139 	}
140 
141 	card->num_links = num_links;
142 	card->num_dapm_routes = num_route;
143 
144 	for_each_card_prelinks(card, i, dai_link)
145 		dai_link->platform_name = mach_params->platform;
146 
147 	return 0;
148 }
149 
150 static int skl_hda_audio_probe(struct platform_device *pdev)
151 {
152 	struct snd_soc_acpi_mach *mach;
153 	struct skl_hda_private *ctx;
154 	int ret;
155 
156 	dev_dbg(&pdev->dev, "%s: entry\n", __func__);
157 
158 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
159 	if (!ctx)
160 		return -ENOMEM;
161 
162 	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
163 
164 	mach = (&pdev->dev)->platform_data;
165 	if (!mach)
166 		return -EINVAL;
167 
168 	ret = skl_hda_fill_card_info(&mach->mach_params);
169 	if (ret < 0) {
170 		dev_err(&pdev->dev, "Unsupported HDAudio/iDisp configuration found\n");
171 		return ret;
172 	}
173 
174 	ctx->pcm_count = hda_soc_card.num_links;
175 	ctx->dai_index = 1; /* hdmi codec dai name starts from index 1 */
176 	ctx->platform_name = mach->mach_params.platform;
177 
178 	hda_soc_card.dev = &pdev->dev;
179 	snd_soc_card_set_drvdata(&hda_soc_card, ctx);
180 
181 	return devm_snd_soc_register_card(&pdev->dev, &hda_soc_card);
182 }
183 
184 static struct platform_driver skl_hda_audio = {
185 	.probe = skl_hda_audio_probe,
186 	.driver = {
187 		.name = "skl_hda_dsp_generic",
188 		.pm = &snd_soc_pm_ops,
189 	},
190 };
191 
192 module_platform_driver(skl_hda_audio)
193 
194 /* Module information */
195 MODULE_DESCRIPTION("SKL/KBL/BXT/APL HDA Generic Machine driver");
196 MODULE_AUTHOR("Rakesh Ughreja <rakesh.a.ughreja@intel.com>");
197 MODULE_LICENSE("GPL v2");
198 MODULE_ALIAS("platform:skl_hda_dsp_generic");
199