1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright(c) 2019 Intel Corporation.
3 
4 /*
5  * Intel SOF Machine driver for DA7219 + MAX98373/MAX98360A codec
6  */
7 
8 #include <linux/input.h>
9 #include <linux/module.h>
10 #include <sound/pcm.h>
11 #include <sound/pcm_params.h>
12 #include <linux/platform_device.h>
13 #include <sound/soc.h>
14 #include <sound/soc-acpi.h>
15 #include "../../codecs/da7219.h"
16 #include "../../codecs/da7219-aad.h"
17 #include "hda_dsp_common.h"
18 
19 #define DIALOG_CODEC_DAI	"da7219-hifi"
20 #define MAX98373_CODEC_DAI	"max98373-aif1"
21 #define MAXIM_DEV0_NAME		"i2c-MX98373:00"
22 #define MAXIM_DEV1_NAME		"i2c-MX98373:01"
23 
24 struct hdmi_pcm {
25 	struct list_head head;
26 	struct snd_soc_dai *codec_dai;
27 	int device;
28 };
29 
30 struct card_private {
31 	struct snd_soc_jack headset;
32 	struct list_head hdmi_pcm_list;
33 	struct snd_soc_jack hdmi[3];
34 };
35 
36 static int platform_clock_control(struct snd_soc_dapm_widget *w,
37 				  struct snd_kcontrol *k, int  event)
38 {
39 	struct snd_soc_dapm_context *dapm = w->dapm;
40 	struct snd_soc_card *card = dapm->card;
41 	struct snd_soc_dai *codec_dai;
42 	int ret = 0;
43 
44 	codec_dai = snd_soc_card_get_codec_dai(card, DIALOG_CODEC_DAI);
45 	if (!codec_dai) {
46 		dev_err(card->dev, "Codec dai not found; Unable to set/unset codec pll\n");
47 		return -EIO;
48 	}
49 
50 	if (SND_SOC_DAPM_EVENT_OFF(event)) {
51 		ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_MCLK,
52 					  0, 0);
53 		if (ret)
54 			dev_err(card->dev, "failed to stop PLL: %d\n", ret);
55 	} else if (SND_SOC_DAPM_EVENT_ON(event)) {
56 		ret = snd_soc_dai_set_pll(codec_dai, 0, DA7219_SYSCLK_PLL_SRM,
57 					  0, DA7219_PLL_FREQ_OUT_98304);
58 		if (ret)
59 			dev_err(card->dev, "failed to start PLL: %d\n", ret);
60 	}
61 
62 	return ret;
63 }
64 
65 static const struct snd_kcontrol_new controls[] = {
66 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
67 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
68 	SOC_DAPM_PIN_SWITCH("Left Spk"),
69 	SOC_DAPM_PIN_SWITCH("Right Spk"),
70 };
71 
72 static const struct snd_kcontrol_new m98360a_controls[] = {
73 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
74 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
75 	SOC_DAPM_PIN_SWITCH("Spk"),
76 };
77 
78 /* For MAX98373 amp */
79 static const struct snd_soc_dapm_widget widgets[] = {
80 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
81 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
82 
83 	SND_SOC_DAPM_SPK("Left Spk", NULL),
84 	SND_SOC_DAPM_SPK("Right Spk", NULL),
85 
86 	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
87 			    platform_clock_control, SND_SOC_DAPM_POST_PMD |
88 			    SND_SOC_DAPM_PRE_PMU),
89 };
90 
91 static const struct snd_soc_dapm_route audio_map[] = {
92 	{ "Headphone Jack", NULL, "HPL" },
93 	{ "Headphone Jack", NULL, "HPR" },
94 
95 	{ "MIC", NULL, "Headset Mic" },
96 
97 	{ "Headphone Jack", NULL, "Platform Clock" },
98 	{ "Headset Mic", NULL, "Platform Clock" },
99 
100 	{ "Left Spk", NULL, "Left BE_OUT" },
101 	{ "Right Spk", NULL, "Right BE_OUT" },
102 };
103 
104 /* For MAX98360A amp */
105 static const struct snd_soc_dapm_widget max98360a_widgets[] = {
106 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
107 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
108 
109 	SND_SOC_DAPM_SPK("Spk", NULL),
110 
111 	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
112 			    platform_clock_control, SND_SOC_DAPM_POST_PMD |
113 			    SND_SOC_DAPM_PRE_PMU),
114 };
115 
116 static const struct snd_soc_dapm_route max98360a_map[] = {
117 	{ "Headphone Jack", NULL, "HPL" },
118 	{ "Headphone Jack", NULL, "HPR" },
119 
120 	{ "MIC", NULL, "Headset Mic" },
121 
122 	{ "Headphone Jack", NULL, "Platform Clock" },
123 	{ "Headset Mic", NULL, "Platform Clock" },
124 
125 	{"Spk", NULL, "Speaker"},
126 };
127 
128 static struct snd_soc_jack headset;
129 
130 static int da7219_codec_init(struct snd_soc_pcm_runtime *rtd)
131 {
132 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
133 	struct snd_soc_component *component = codec_dai->component;
134 	struct snd_soc_jack *jack;
135 	int ret;
136 
137 	/* Configure sysclk for codec */
138 	ret = snd_soc_dai_set_sysclk(codec_dai, DA7219_CLKSRC_MCLK, 24000000,
139 				     SND_SOC_CLOCK_IN);
140 	if (ret) {
141 		dev_err(rtd->dev, "can't set codec sysclk configuration\n");
142 		return ret;
143 	}
144 
145 	/*
146 	 * Headset buttons map to the google Reference headset.
147 	 * These can be configured by userspace.
148 	 */
149 	ret = snd_soc_card_jack_new(rtd->card, "Headset Jack",
150 				    SND_JACK_HEADSET | SND_JACK_BTN_0 |
151 				    SND_JACK_BTN_1 | SND_JACK_BTN_2 |
152 				    SND_JACK_BTN_3 | SND_JACK_LINEOUT,
153 				    &headset, NULL, 0);
154 	if (ret) {
155 		dev_err(rtd->dev, "Headset Jack creation failed: %d\n", ret);
156 		return ret;
157 	}
158 
159 	jack = &headset;
160 	snd_jack_set_key(jack->jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
161 	snd_jack_set_key(jack->jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
162 	snd_jack_set_key(jack->jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
163 	snd_jack_set_key(jack->jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
164 	da7219_aad_jack_det(component, jack);
165 
166 	return ret;
167 }
168 
169 static int ssp1_hw_params(struct snd_pcm_substream *substream,
170 			      struct snd_pcm_hw_params *params)
171 {
172 	struct snd_soc_pcm_runtime *runtime = substream->private_data;
173 	int ret, j;
174 
175 	for (j = 0; j < runtime->num_codecs; j++) {
176 		struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(runtime, j);
177 
178 		if (!strcmp(codec_dai->component->name, MAXIM_DEV0_NAME)) {
179 			/* vmon_slot_no = 0 imon_slot_no = 1 for TX slots */
180 			ret = snd_soc_dai_set_tdm_slot(codec_dai, 0x3, 3, 4, 16);
181 			if (ret < 0) {
182 				dev_err(runtime->dev, "DEV0 TDM slot err:%d\n", ret);
183 				return ret;
184 			}
185 		}
186 		if (!strcmp(codec_dai->component->name, MAXIM_DEV1_NAME)) {
187 			/* vmon_slot_no = 2 imon_slot_no = 3 for TX slots */
188 			ret = snd_soc_dai_set_tdm_slot(codec_dai, 0xC, 3, 4, 16);
189 			if (ret < 0) {
190 				dev_err(runtime->dev, "DEV1 TDM slot err:%d\n", ret);
191 				return ret;
192 			}
193 		}
194 	}
195 
196 	return 0;
197 }
198 
199 static struct snd_soc_ops ssp1_ops = {
200 	.hw_params = ssp1_hw_params,
201 };
202 
203 static struct snd_soc_codec_conf max98373_codec_conf[] = {
204 	{
205 		.dlc = COMP_CODEC_CONF(MAXIM_DEV0_NAME),
206 		.name_prefix = "Right",
207 	},
208 	{
209 		.dlc = COMP_CODEC_CONF(MAXIM_DEV1_NAME),
210 		.name_prefix = "Left",
211 	},
212 };
213 
214 static int hdmi_init(struct snd_soc_pcm_runtime *rtd)
215 {
216 	struct card_private *ctx = snd_soc_card_get_drvdata(rtd->card);
217 	struct snd_soc_dai *dai = asoc_rtd_to_codec(rtd, 0);
218 	struct hdmi_pcm *pcm;
219 
220 	pcm = devm_kzalloc(rtd->card->dev, sizeof(*pcm), GFP_KERNEL);
221 	if (!pcm)
222 		return -ENOMEM;
223 
224 	pcm->device = dai->id;
225 	pcm->codec_dai = dai;
226 
227 	list_add_tail(&pcm->head, &ctx->hdmi_pcm_list);
228 
229 	return 0;
230 }
231 
232 static int card_late_probe(struct snd_soc_card *card)
233 {
234 	struct card_private *ctx = snd_soc_card_get_drvdata(card);
235 	struct snd_soc_acpi_mach *mach = (card->dev)->platform_data;
236 	struct hdmi_pcm *pcm;
237 
238 	if (mach->mach_params.common_hdmi_codec_drv) {
239 		pcm = list_first_entry(&ctx->hdmi_pcm_list, struct hdmi_pcm,
240 				       head);
241 		return hda_dsp_hdmi_build_controls(card,
242 						   pcm->codec_dai->component);
243 	}
244 
245 	return -EINVAL;
246 }
247 
248 SND_SOC_DAILINK_DEF(ssp0_pin,
249 	DAILINK_COMP_ARRAY(COMP_CPU("SSP0 Pin")));
250 SND_SOC_DAILINK_DEF(ssp0_codec,
251 	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-DLGS7219:00", DIALOG_CODEC_DAI)));
252 
253 SND_SOC_DAILINK_DEF(ssp1_pin,
254 	DAILINK_COMP_ARRAY(COMP_CPU("SSP1 Pin")));
255 SND_SOC_DAILINK_DEF(ssp1_amps,
256 	DAILINK_COMP_ARRAY(
257 	/* Left */	COMP_CODEC(MAXIM_DEV0_NAME, MAX98373_CODEC_DAI),
258 	/* Right */	COMP_CODEC(MAXIM_DEV1_NAME, MAX98373_CODEC_DAI)));
259 
260 SND_SOC_DAILINK_DEF(ssp1_m98360a,
261 	DAILINK_COMP_ARRAY(COMP_CODEC("MX98360A:00", "HiFi")));
262 
263 SND_SOC_DAILINK_DEF(dmic_pin,
264 	DAILINK_COMP_ARRAY(COMP_CPU("DMIC01 Pin")));
265 SND_SOC_DAILINK_DEF(dmic_codec,
266 	DAILINK_COMP_ARRAY(COMP_CODEC("dmic-codec", "dmic-hifi")));
267 
268 SND_SOC_DAILINK_DEF(idisp1_pin,
269 	DAILINK_COMP_ARRAY(COMP_CPU("iDisp1 Pin")));
270 SND_SOC_DAILINK_DEF(idisp1_codec,
271 	DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi1")));
272 
273 SND_SOC_DAILINK_DEF(idisp2_pin,
274 	DAILINK_COMP_ARRAY(COMP_CPU("iDisp2 Pin")));
275 SND_SOC_DAILINK_DEF(idisp2_codec,
276 	DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi2")));
277 
278 SND_SOC_DAILINK_DEF(idisp3_pin,
279 	DAILINK_COMP_ARRAY(COMP_CPU("iDisp3 Pin")));
280 SND_SOC_DAILINK_DEF(idisp3_codec,
281 	DAILINK_COMP_ARRAY(COMP_CODEC("ehdaudio0D2", "intel-hdmi-hifi3")));
282 
283 SND_SOC_DAILINK_DEF(platform, /* subject to be overridden during probe */
284 	DAILINK_COMP_ARRAY(COMP_PLATFORM("0000:00:1f.3")));
285 
286 static struct snd_soc_dai_link dais[] = {
287 	/* Back End DAI links */
288 	{
289 		.name = "SSP1-Codec",
290 		.id = 0,
291 		.ignore_pmdown_time = 1,
292 		.no_pcm = 1,
293 		.dpcm_playback = 1,
294 		.dpcm_capture = 1, /* IV feedback */
295 		.ops = &ssp1_ops,
296 		SND_SOC_DAILINK_REG(ssp1_pin, ssp1_amps, platform),
297 	},
298 	{
299 		.name = "SSP0-Codec",
300 		.id = 1,
301 		.no_pcm = 1,
302 		.init = da7219_codec_init,
303 		.ignore_pmdown_time = 1,
304 		.dpcm_playback = 1,
305 		.dpcm_capture = 1,
306 		SND_SOC_DAILINK_REG(ssp0_pin, ssp0_codec, platform),
307 	},
308 	{
309 		.name = "dmic01",
310 		.id = 2,
311 		.ignore_suspend = 1,
312 		.dpcm_capture = 1,
313 		.no_pcm = 1,
314 		SND_SOC_DAILINK_REG(dmic_pin, dmic_codec, platform),
315 	},
316 	{
317 		.name = "iDisp1",
318 		.id = 3,
319 		.init = hdmi_init,
320 		.dpcm_playback = 1,
321 		.no_pcm = 1,
322 		SND_SOC_DAILINK_REG(idisp1_pin, idisp1_codec, platform),
323 	},
324 	{
325 		.name = "iDisp2",
326 		.id = 4,
327 		.init = hdmi_init,
328 		.dpcm_playback = 1,
329 		.no_pcm = 1,
330 		SND_SOC_DAILINK_REG(idisp2_pin, idisp2_codec, platform),
331 	},
332 	{
333 		.name = "iDisp3",
334 		.id = 5,
335 		.init = hdmi_init,
336 		.dpcm_playback = 1,
337 		.no_pcm = 1,
338 		SND_SOC_DAILINK_REG(idisp3_pin, idisp3_codec, platform),
339 	},
340 };
341 
342 static struct snd_soc_card card_da7219_m98373 = {
343 	.name = "da7219max",
344 	.owner = THIS_MODULE,
345 	.dai_link = dais,
346 	.num_links = ARRAY_SIZE(dais),
347 	.controls = controls,
348 	.num_controls = ARRAY_SIZE(controls),
349 	.dapm_widgets = widgets,
350 	.num_dapm_widgets = ARRAY_SIZE(widgets),
351 	.dapm_routes = audio_map,
352 	.num_dapm_routes = ARRAY_SIZE(audio_map),
353 	.codec_conf = max98373_codec_conf,
354 	.num_configs = ARRAY_SIZE(max98373_codec_conf),
355 	.fully_routed = true,
356 	.late_probe = card_late_probe,
357 };
358 
359 static struct snd_soc_card card_da7219_m98360a = {
360 	.name = "da7219max98360a",
361 	.owner = THIS_MODULE,
362 	.dai_link = dais,
363 	.num_links = ARRAY_SIZE(dais),
364 	.controls = m98360a_controls,
365 	.num_controls = ARRAY_SIZE(m98360a_controls),
366 	.dapm_widgets = max98360a_widgets,
367 	.num_dapm_widgets = ARRAY_SIZE(max98360a_widgets),
368 	.dapm_routes = max98360a_map,
369 	.num_dapm_routes = ARRAY_SIZE(max98360a_map),
370 	.fully_routed = true,
371 	.late_probe = card_late_probe,
372 };
373 
374 static int audio_probe(struct platform_device *pdev)
375 {
376 	static struct snd_soc_card *card;
377 	struct snd_soc_acpi_mach *mach;
378 	struct card_private *ctx;
379 	int ret;
380 
381 	ctx = devm_kzalloc(&pdev->dev, sizeof(*ctx), GFP_KERNEL);
382 	if (!ctx)
383 		return -ENOMEM;
384 
385 	/* By default dais[0] is configured for max98373 */
386 	if (!strcmp(pdev->name, "sof_da7219_max98360a")) {
387 		dais[0] = (struct snd_soc_dai_link) {
388 			.name = "SSP1-Codec",
389 			.id = 0,
390 			.no_pcm = 1,
391 			.dpcm_playback = 1,
392 			.ignore_pmdown_time = 1,
393 			SND_SOC_DAILINK_REG(ssp1_pin, ssp1_m98360a, platform) };
394 	}
395 
396 	INIT_LIST_HEAD(&ctx->hdmi_pcm_list);
397 	card = (struct snd_soc_card *)pdev->id_entry->driver_data;
398 	card->dev = &pdev->dev;
399 
400 	mach = pdev->dev.platform_data;
401 	ret = snd_soc_fixup_dai_links_platform_name(card,
402 						    mach->mach_params.platform);
403 	if (ret)
404 		return ret;
405 
406 	snd_soc_card_set_drvdata(card, ctx);
407 
408 	return devm_snd_soc_register_card(&pdev->dev, card);
409 }
410 
411 static const struct platform_device_id board_ids[] = {
412 	{
413 		.name = "sof_da7219_max98373",
414 		.driver_data = (kernel_ulong_t)&card_da7219_m98373,
415 	},
416 	{
417 		.name = "sof_da7219_max98360a",
418 		.driver_data = (kernel_ulong_t)&card_da7219_m98360a,
419 	},
420 	{ }
421 };
422 
423 static struct platform_driver audio = {
424 	.probe = audio_probe,
425 	.driver = {
426 		.name = "sof_da7219_max98_360a_373",
427 		.pm = &snd_soc_pm_ops,
428 	},
429 	.id_table = board_ids,
430 };
431 module_platform_driver(audio)
432 
433 /* Module information */
434 MODULE_DESCRIPTION("ASoC Intel(R) SOF Machine driver");
435 MODULE_AUTHOR("Yong Zhi <yong.zhi@intel.com>");
436 MODULE_LICENSE("GPL v2");
437 MODULE_ALIAS("platform:sof_da7219_max98360a");
438 MODULE_ALIAS("platform:sof_da7219_max98373");
439