xref: /linux/sound/soc/intel/boards/ehl_rt5660.c (revision a5210135489ae7bc1ef1cb4a8157361dd7b468cd)
1 // SPDX-License-Identifier: GPL-2.0-only
2 // Copyright (c) 2020 Intel Corporation
3 
4 /*
5  * ehl_rt5660 - ASOC Machine driver for Elkhart Lake platforms
6  * with rt5660 codec
7  */
8 
9 #include <linux/acpi.h>
10 #include <sound/core.h>
11 #include <linux/device.h>
12 #include <linux/errno.h>
13 #include <linux/gfp.h>
14 #include <sound/jack.h>
15 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 #include <sound/pcm.h>
19 #include <sound/pcm_params.h>
20 #include <sound/soc.h>
21 #include <sound/soc-acpi.h>
22 
23 #include "hda_dsp_common.h"
24 #include "../../codecs/rt5660.h"
25 
26 #define HDMI_LINK_START 3
27 #define HDMI_LINE_END 6
28 #define IDISP_CODEC_MASK	0x4
29 
30 struct sof_card_private {
31 	struct list_head hdmi_pcm_list;
32 	bool idisp_codec;
33 };
34 
35 static const struct snd_kcontrol_new rt5660_controls[] = {
36 	SOC_DAPM_PIN_SWITCH("Speaker"),
37 	/* There are two MICBIAS in rt5660, each for one MIC */
38 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
39 	SOC_DAPM_PIN_SWITCH("Headset Mic2"),
40 	SOC_DAPM_PIN_SWITCH("Line Out"),
41 };
42 
43 static const struct snd_soc_dapm_widget rt5660_widgets[] = {
44 	SND_SOC_DAPM_SPK("Speaker", NULL),
45 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
46 	SND_SOC_DAPM_MIC("Headset Mic2", NULL),
47 	SND_SOC_DAPM_MIC("SoC DMIC", NULL),
48 	SND_SOC_DAPM_LINE("Line Out", NULL),
49 };
50 
51 static const struct snd_soc_dapm_route rt5660_map[] = {
52 	{"Speaker", NULL, "SPO"},
53 
54 	{"Headset Mic", NULL, "MICBIAS1"},
55 	{"Headset Mic2", NULL, "MICBIAS2"},
56 
57 	{"IN1P", NULL, "Headset Mic"},
58 	{"IN2P", NULL, "Headset Mic2"},
59 
60 	{"Line Out", NULL, "LOUTL"},
61 	{"Line Out", NULL, "LOUTR"},
62 
63 	{"DMic", NULL, "SoC DMIC"},
64 };
65 
66 struct sof_hdmi_pcm {
67 	struct list_head head;
68 	struct snd_soc_dai *codec_dai;
69 	int device;
70 };
71 
hdmi_init(struct snd_soc_pcm_runtime * rtd)72 static int hdmi_init(struct snd_soc_pcm_runtime *rtd)
73 {
74 	struct sof_card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
75 	struct snd_soc_dai *dai = snd_soc_rtd_to_codec(rtd, 0);
76 	struct sof_hdmi_pcm *pcm;
77 
78 	pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
79 	if (!pcm)
80 		return -ENOMEM;
81 
82 	/* dai_link id is 1:1 mapped to the PCM device */
83 	pcm->device = rtd->dai_link->id;
84 	pcm->codec_dai = dai;
85 
86 	list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
87 
88 	return 0;
89 }
90 
card_late_probe(struct snd_soc_card * card)91 static int card_late_probe(struct snd_soc_card *card)
92 {
93 	struct sof_card_private *ctx = snd_soc_card_get_drvdata(card);
94 	struct sof_hdmi_pcm *pcm;
95 
96 	if (list_empty(&ctx->hdmi_pcm_list))
97 		return -ENOENT;
98 
99 	if (!ctx->idisp_codec)
100 		return 0;
101 
102 	pcm = list_first_entry(&ctx->hdmi_pcm_list, struct sof_hdmi_pcm, head);
103 
104 	return hda_dsp_hdmi_build_controls(card, pcm->codec_dai->component);
105 }
106 
rt5660_hw_params(struct snd_pcm_substream * substream,struct snd_pcm_hw_params * params)107 static int rt5660_hw_params(struct snd_pcm_substream *substream,
108 			    struct snd_pcm_hw_params *params)
109 {
110 	struct snd_soc_pcm_runtime *rtd = snd_soc_substream_to_rtd(substream);
111 	struct snd_soc_dai *codec_dai = snd_soc_rtd_to_codec(rtd, 0);
112 	int ret;
113 
114 	ret = snd_soc_dai_set_sysclk(codec_dai,
115 				     RT5660_SCLK_S_PLL1,
116 				     params_rate(params) * 512,
117 				     SND_SOC_CLOCK_IN);
118 	if (ret < 0) {
119 		dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n", ret);
120 		return ret;
121 	}
122 
123 	ret = snd_soc_dai_set_pll(codec_dai, 0,
124 				  RT5660_PLL1_S_BCLK,
125 				  params_rate(params) * 50,
126 				  params_rate(params) * 512);
127 	if (ret < 0)
128 		dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
129 
130 	return ret;
131 }
132 
133 static const struct snd_soc_ops rt5660_ops = {
134 	.hw_params = rt5660_hw_params,
135 };
136 
137 SND_SOC_DAILINK_DEF(ssp0_pin,
138 	DAILINK_COMP_ARRAY(COMP_CPU("SSP0 Pin")));
139 
140 SND_SOC_DAILINK_DEF(rt5660_codec,
141 	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5660:00", "rt5660-aif1")));
142 
143 SND_SOC_DAILINK_DEF(platform,
144 	DAILINK_COMP_ARRAY(COMP_PLATFORM("0000:00:1f.3")));
145 
146 SND_SOC_DAILINK_DEF(dmic_pin,
147 	DAILINK_COMP_ARRAY(COMP_CPU("DMIC01 Pin")));
148 SND_SOC_DAILINK_DEF(dmic_codec,
149 	DAILINK_COMP_ARRAY(COMP_CODEC("dmic-codec", "dmic-hifi")));
150 SND_SOC_DAILINK_DEF(dmic16k,
151 	DAILINK_COMP_ARRAY(COMP_CPU("DMIC16k Pin")));
152 
153 SND_SOC_DAILINK_DEF(idisp1_pin,
154 	DAILINK_COMP_ARRAY(COMP_CPU("iDisp1 Pin")));
155 SND_SOC_DAILINK_DEF(idisp1_codec,
156 	DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi1")));
157 
158 SND_SOC_DAILINK_DEF(idisp2_pin,
159 	DAILINK_COMP_ARRAY(COMP_CPU("iDisp2 Pin")));
160 SND_SOC_DAILINK_DEF(idisp2_codec,
161 	DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi2")));
162 
163 SND_SOC_DAILINK_DEF(idisp3_pin,
164 	DAILINK_COMP_ARRAY(COMP_CPU("iDisp3 Pin")));
165 SND_SOC_DAILINK_DEF(idisp3_codec,
166 	DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi3")));
167 
168 SND_SOC_DAILINK_DEF(idisp4_pin,
169 	DAILINK_COMP_ARRAY(COMP_CPU("iDisp4 Pin")));
170 SND_SOC_DAILINK_DEF(idisp4_codec,
171 	DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi4")));
172 
173 static struct snd_soc_dai_link ehl_rt5660_dailink[] = {
174 	/* back ends */
175 	{
176 		.name = "SSP0-Codec",
177 		.id = 0,
178 		.no_pcm = 1,
179 		.ops = &rt5660_ops,
180 		SND_SOC_DAILINK_REG(ssp0_pin, rt5660_codec, platform),
181 	},
182 	{
183 		.name = "dmic48k",
184 		.id = 1,
185 		.ignore_suspend = 1,
186 		.capture_only = 1,
187 		.no_pcm = 1,
188 		SND_SOC_DAILINK_REG(dmic_pin, dmic_codec, platform),
189 	},
190 	{
191 		.name = "dmic16k",
192 		.id = 2,
193 		.ignore_suspend = 1,
194 		.capture_only = 1,
195 		.no_pcm = 1,
196 		SND_SOC_DAILINK_REG(dmic16k, dmic_codec, platform),
197 	},
198 	{
199 		.name = "iDisp1",
200 		.id = 5,
201 		.init = hdmi_init,
202 		.playback_only = 1,
203 		.no_pcm = 1,
204 		SND_SOC_DAILINK_REG(idisp1_pin, idisp1_codec, platform),
205 	},
206 	{
207 		.name = "iDisp2",
208 		.id = 6,
209 		.init = hdmi_init,
210 		.playback_only = 1,
211 		.no_pcm = 1,
212 		SND_SOC_DAILINK_REG(idisp2_pin, idisp2_codec, platform),
213 	},
214 	{
215 		.name = "iDisp3",
216 		.id = 7,
217 		.init = hdmi_init,
218 		.playback_only = 1,
219 		.no_pcm = 1,
220 		SND_SOC_DAILINK_REG(idisp3_pin, idisp3_codec, platform),
221 	},
222 	{
223 		.name = "iDisp4",
224 		.id = 8,
225 		.init = hdmi_init,
226 		.playback_only = 1,
227 		.no_pcm = 1,
228 		SND_SOC_DAILINK_REG(idisp4_pin, idisp4_codec, platform),
229 	},
230 };
231 
232 /* SoC card */
233 static struct snd_soc_card snd_soc_card_ehl_rt5660 = {
234 	.name = "ehl-rt5660",
235 	.owner = THIS_MODULE,
236 	.dai_link = ehl_rt5660_dailink,
237 	.num_links = ARRAY_SIZE(ehl_rt5660_dailink),
238 	.dapm_widgets = rt5660_widgets,
239 	.num_dapm_widgets = ARRAY_SIZE(rt5660_widgets),
240 	.dapm_routes = rt5660_map,
241 	.num_dapm_routes = ARRAY_SIZE(rt5660_map),
242 	.controls = rt5660_controls,
243 	.num_controls = ARRAY_SIZE(rt5660_controls),
244 	.fully_routed = true,
245 	.late_probe = card_late_probe,
246 };
247 
248 /* If hdmi codec is not supported, switch to use dummy codec */
hdmi_link_init(struct snd_soc_card * card,struct sof_card_private * ctx,struct snd_soc_acpi_mach * mach)249 static void hdmi_link_init(struct snd_soc_card *card,
250 			   struct sof_card_private *ctx,
251 			   struct snd_soc_acpi_mach *mach)
252 {
253 	int i;
254 
255 	if (mach->mach_params.codec_mask & IDISP_CODEC_MASK) {
256 		ctx->idisp_codec = true;
257 		return;
258 	}
259 
260 	/*
261 	 * if HDMI is not enabled in kernel config, or
262 	 * hdmi codec is not supported
263 	 */
264 	for (i = HDMI_LINK_START; i <= HDMI_LINE_END; i++)
265 		card->dai_link[i].codecs[0] = snd_soc_dummy_dlc;
266 }
267 
snd_ehl_rt5660_probe(struct platform_device * pdev)268 static int snd_ehl_rt5660_probe(struct platform_device *pdev)
269 {
270 	struct snd_soc_acpi_mach *mach;
271 	struct snd_soc_card *card = &snd_soc_card_ehl_rt5660;
272 	struct sof_card_private *ctx;
273 	int ret;
274 
275 	card->dev = &pdev->dev;
276 
277 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
278 	if (!ctx)
279 		return -ENOMEM;
280 	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
281 	snd_soc_card_set_drvdata(card, ctx);
282 
283 	mach = pdev->dev.platform_data;
284 	ret = snd_soc_fixup_dai_links_platform_name(card,
285 						    mach->mach_params.platform);
286 	if (ret)
287 		return ret;
288 
289 	hdmi_link_init(card, ctx, mach);
290 
291 	return devm_snd_soc_register_card(&pdev->dev, card);
292 }
293 
294 static const struct platform_device_id ehl_board_ids[] = {
295 	{ .name = "ehl_rt5660" },
296 	{ }
297 };
298 MODULE_DEVICE_TABLE(platform, ehl_board_ids);
299 
300 static struct platform_driver snd_ehl_rt5660_driver = {
301 	.driver = {
302 		.name = "ehl_rt5660",
303 		.pm = &snd_soc_pm_ops,
304 	},
305 	.probe = snd_ehl_rt5660_probe,
306 	.id_table = ehl_board_ids,
307 };
308 
309 module_platform_driver(snd_ehl_rt5660_driver);
310 
311 MODULE_DESCRIPTION("ASoC Intel(R) Elkhartlake + rt5660 Machine driver");
312 MODULE_AUTHOR("libin.yang@intel.com");
313 MODULE_LICENSE("GPL v2");
314 MODULE_IMPORT_NS("SND_SOC_INTEL_HDA_DSP_COMMON");
315