1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Machine driver for AMD ACP Audio engine using DA7219 & MAX98357 codec.
4 //
5 //Copyright 2016 Advanced Micro Devices, Inc.
6 
7 #include <sound/core.h>
8 #include <sound/soc.h>
9 #include <sound/pcm.h>
10 #include <sound/pcm_params.h>
11 #include <sound/soc-dapm.h>
12 #include <sound/jack.h>
13 #include <linux/clk.h>
14 #include <linux/gpio.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/module.h>
17 #include <linux/i2c.h>
18 #include <linux/input.h>
19 #include <linux/io.h>
20 #include <linux/acpi.h>
21 
22 #include "raven/acp3x.h"
23 #include "../codecs/rt5682.h"
24 #include "../codecs/rt1015.h"
25 
26 #define PCO_PLAT_CLK 48000000
27 #define RT5682_PLL_FREQ (48000 * 512)
28 #define DUAL_CHANNEL		2
29 
30 static struct snd_soc_jack pco_jack;
31 static struct clk *rt5682_dai_wclk;
32 static struct clk *rt5682_dai_bclk;
33 static struct gpio_desc *dmic_sel;
34 void *soc_is_rltk_max(struct device *dev);
35 
36 enum {
37 	RT5682 = 0,
38 	MAX,
39 	EC,
40 };
41 
42 static int acp3x_5682_init(struct snd_soc_pcm_runtime *rtd)
43 {
44 	int ret;
45 	struct snd_soc_card *card = rtd->card;
46 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
47 	struct snd_soc_component *component = codec_dai->component;
48 
49 	dev_info(rtd->dev, "codec dai name = %s\n", codec_dai->name);
50 
51 	/* set rt5682 dai fmt */
52 	ret =  snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S
53 			| SND_SOC_DAIFMT_NB_NF
54 			| SND_SOC_DAIFMT_CBM_CFM);
55 	if (ret < 0) {
56 		dev_err(rtd->card->dev,
57 				"Failed to set rt5682 dai fmt: %d\n", ret);
58 		return ret;
59 	}
60 
61 	/* set codec PLL */
62 	ret = snd_soc_dai_set_pll(codec_dai, RT5682_PLL2, RT5682_PLL2_S_MCLK,
63 				  PCO_PLAT_CLK, RT5682_PLL_FREQ);
64 	if (ret < 0) {
65 		dev_err(rtd->dev, "can't set rt5682 PLL: %d\n", ret);
66 		return ret;
67 	}
68 
69 	/* Set codec sysclk */
70 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5682_SCLK_S_PLL2,
71 			RT5682_PLL_FREQ, SND_SOC_CLOCK_IN);
72 	if (ret < 0) {
73 		dev_err(rtd->dev,
74 			"Failed to set rt5682 SYSCLK: %d\n", ret);
75 		return ret;
76 	}
77 
78 	/* Set tdm/i2s1 master bclk ratio */
79 	ret = snd_soc_dai_set_bclk_ratio(codec_dai, 64);
80 	if (ret < 0) {
81 		dev_err(rtd->dev,
82 			"Failed to set rt5682 tdm bclk ratio: %d\n", ret);
83 		return ret;
84 	}
85 
86 	rt5682_dai_wclk = clk_get(component->dev, "rt5682-dai-wclk");
87 	rt5682_dai_bclk = clk_get(component->dev, "rt5682-dai-bclk");
88 
89 	ret = snd_soc_card_jack_new(card, "Headset Jack",
90 				SND_JACK_HEADSET | SND_JACK_LINEOUT |
91 				SND_JACK_BTN_0 | SND_JACK_BTN_1 |
92 				SND_JACK_BTN_2 | SND_JACK_BTN_3,
93 				&pco_jack, NULL, 0);
94 	if (ret) {
95 		dev_err(card->dev, "HP jack creation failed %d\n", ret);
96 		return ret;
97 	}
98 
99 	snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
100 	snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
101 	snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
102 	snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
103 
104 	ret = snd_soc_component_set_jack(component, &pco_jack, NULL);
105 	if (ret) {
106 		dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
107 		return ret;
108 	}
109 
110 	return ret;
111 }
112 
113 static int rt5682_clk_enable(struct snd_pcm_substream *substream)
114 {
115 	int ret = 0;
116 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
117 
118 	/* RT5682 will support only 48K output with 48M mclk */
119 	clk_set_rate(rt5682_dai_wclk, 48000);
120 	clk_set_rate(rt5682_dai_bclk, 48000 * 64);
121 	ret = clk_prepare_enable(rt5682_dai_wclk);
122 	if (ret < 0) {
123 		dev_err(rtd->dev, "can't enable wclk %d\n", ret);
124 		return ret;
125 	}
126 
127 	return ret;
128 }
129 
130 static int acp3x_1015_hw_params(struct snd_pcm_substream *substream,
131 					struct snd_pcm_hw_params *params)
132 {
133 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
134 	struct snd_soc_dai *codec_dai;
135 	int srate, i, ret;
136 
137 	ret = 0;
138 	srate = params_rate(params);
139 
140 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
141 		if (strcmp(codec_dai->name, "rt1015-aif"))
142 			continue;
143 		ret = snd_soc_dai_set_bclk_ratio(codec_dai, 64);
144 		if (ret < 0)
145 			return ret;
146 		ret = snd_soc_dai_set_pll(codec_dai, 0, RT1015_PLL_S_BCLK,
147 						64 * srate, 256 * srate);
148 		if (ret < 0)
149 			return ret;
150 		ret = snd_soc_dai_set_sysclk(codec_dai, RT1015_SCLK_S_PLL,
151 					256 * srate, SND_SOC_CLOCK_IN);
152 		if (ret < 0)
153 			return ret;
154 	}
155 	return ret;
156 }
157 
158 static void rt5682_clk_disable(void)
159 {
160 	clk_disable_unprepare(rt5682_dai_wclk);
161 }
162 
163 static const unsigned int channels[] = {
164 	DUAL_CHANNEL,
165 };
166 
167 static const unsigned int rates[] = {
168 	48000,
169 };
170 
171 static const struct snd_pcm_hw_constraint_list constraints_rates = {
172 	.count = ARRAY_SIZE(rates),
173 	.list  = rates,
174 	.mask = 0,
175 };
176 
177 static const struct snd_pcm_hw_constraint_list constraints_channels = {
178 	.count = ARRAY_SIZE(channels),
179 	.list = channels,
180 	.mask = 0,
181 };
182 
183 static int acp3x_5682_startup(struct snd_pcm_substream *substream)
184 {
185 	struct snd_pcm_runtime *runtime = substream->runtime;
186 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
187 	struct snd_soc_card *card = rtd->card;
188 	struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card);
189 
190 	machine->play_i2s_instance = I2S_SP_INSTANCE;
191 	machine->cap_i2s_instance = I2S_SP_INSTANCE;
192 
193 	runtime->hw.channels_max = DUAL_CHANNEL;
194 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
195 				   &constraints_channels);
196 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
197 				   &constraints_rates);
198 	return rt5682_clk_enable(substream);
199 }
200 
201 static int acp3x_max_startup(struct snd_pcm_substream *substream)
202 {
203 	struct snd_pcm_runtime *runtime = substream->runtime;
204 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
205 	struct snd_soc_card *card = rtd->card;
206 	struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card);
207 
208 	machine->play_i2s_instance = I2S_BT_INSTANCE;
209 
210 	runtime->hw.channels_max = DUAL_CHANNEL;
211 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
212 				   &constraints_channels);
213 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
214 				   &constraints_rates);
215 	return rt5682_clk_enable(substream);
216 }
217 
218 static int acp3x_ec_dmic0_startup(struct snd_pcm_substream *substream)
219 {
220 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
221 	struct snd_soc_card *card = rtd->card;
222 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
223 	struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card);
224 
225 	machine->cap_i2s_instance = I2S_BT_INSTANCE;
226 	snd_soc_dai_set_bclk_ratio(codec_dai, 64);
227 
228 	return rt5682_clk_enable(substream);
229 }
230 
231 static int dmic_switch;
232 
233 static int dmic_get(struct snd_kcontrol *kcontrol,
234 			 struct snd_ctl_elem_value *ucontrol)
235 {
236 	ucontrol->value.integer.value[0] = dmic_switch;
237 	return 0;
238 }
239 
240 static int dmic_set(struct snd_kcontrol *kcontrol,
241 			 struct snd_ctl_elem_value *ucontrol)
242 {
243 	if (dmic_sel) {
244 		dmic_switch = ucontrol->value.integer.value[0];
245 		gpiod_set_value(dmic_sel, dmic_switch);
246 	}
247 	return 0;
248 }
249 
250 static void rt5682_shutdown(struct snd_pcm_substream *substream)
251 {
252 	rt5682_clk_disable();
253 }
254 
255 static const struct snd_soc_ops acp3x_5682_ops = {
256 	.startup = acp3x_5682_startup,
257 	.shutdown = rt5682_shutdown,
258 };
259 
260 static const struct snd_soc_ops acp3x_max_play_ops = {
261 	.startup = acp3x_max_startup,
262 	.shutdown = rt5682_shutdown,
263 	.hw_params = acp3x_1015_hw_params,
264 };
265 
266 static const struct snd_soc_ops acp3x_ec_cap0_ops = {
267 	.startup = acp3x_ec_dmic0_startup,
268 	.shutdown = rt5682_shutdown,
269 };
270 
271 SND_SOC_DAILINK_DEF(acp3x_i2s,
272 	DAILINK_COMP_ARRAY(COMP_CPU("acp3x_i2s_playcap.0")));
273 SND_SOC_DAILINK_DEF(acp3x_bt,
274 	DAILINK_COMP_ARRAY(COMP_CPU("acp3x_i2s_playcap.2")));
275 
276 SND_SOC_DAILINK_DEF(rt5682,
277 	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5682:00", "rt5682-aif1")));
278 SND_SOC_DAILINK_DEF(max,
279 	DAILINK_COMP_ARRAY(COMP_CODEC("MX98357A:00", "HiFi")));
280 SND_SOC_DAILINK_DEF(rt1015,
281 	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC1015:00", "rt1015-aif"),
282 			COMP_CODEC("i2c-10EC1015:01", "rt1015-aif")));
283 SND_SOC_DAILINK_DEF(cros_ec,
284 	DAILINK_COMP_ARRAY(COMP_CODEC("GOOG0013:00", "EC Codec I2S RX")));
285 
286 SND_SOC_DAILINK_DEF(platform,
287 	DAILINK_COMP_ARRAY(COMP_PLATFORM("acp3x_rv_i2s_dma.0")));
288 
289 static struct snd_soc_codec_conf rt1015_conf[] = {
290 	{
291 		.dlc = COMP_CODEC_CONF("i2c-10EC1015:00"),
292 		.name_prefix = "Left",
293 	},
294 	{
295 		.dlc = COMP_CODEC_CONF("i2c-10EC1015:01"),
296 		.name_prefix = "Right",
297 	},
298 };
299 
300 static struct snd_soc_dai_link acp3x_dai[] = {
301 	[RT5682] = {
302 		.name = "acp3x-5682-play",
303 		.stream_name = "Playback",
304 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
305 				| SND_SOC_DAIFMT_CBM_CFM,
306 		.init = acp3x_5682_init,
307 		.dpcm_playback = 1,
308 		.dpcm_capture = 1,
309 		.ops = &acp3x_5682_ops,
310 		SND_SOC_DAILINK_REG(acp3x_i2s, rt5682, platform),
311 	},
312 	[MAX] = {
313 		.name = "acp3x-max98357-play",
314 		.stream_name = "HiFi Playback",
315 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
316 				| SND_SOC_DAIFMT_CBS_CFS,
317 		.dpcm_playback = 1,
318 		.ops = &acp3x_max_play_ops,
319 		.cpus = acp3x_bt,
320 		.num_cpus = ARRAY_SIZE(acp3x_bt),
321 		.platforms = platform,
322 		.num_platforms = ARRAY_SIZE(platform),
323 	},
324 	[EC] = {
325 		.name = "acp3x-ec-dmic0-capture",
326 		.stream_name = "Capture DMIC0",
327 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
328 				| SND_SOC_DAIFMT_CBS_CFS,
329 		.dpcm_capture = 1,
330 		.ops = &acp3x_ec_cap0_ops,
331 		SND_SOC_DAILINK_REG(acp3x_bt, cros_ec, platform),
332 	},
333 };
334 
335 static const char * const dmic_mux_text[] = {
336 	"Front Mic",
337 	"Rear Mic",
338 };
339 
340 static SOC_ENUM_SINGLE_DECL(
341 		acp3x_dmic_enum, SND_SOC_NOPM, 0, dmic_mux_text);
342 
343 static const struct snd_kcontrol_new acp3x_dmic_mux_control =
344 	SOC_DAPM_ENUM_EXT("DMIC Select Mux", acp3x_dmic_enum,
345 			  dmic_get, dmic_set);
346 
347 static const struct snd_soc_dapm_widget acp3x_5682_widgets[] = {
348 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
349 	SND_SOC_DAPM_SPK("Spk", NULL),
350 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
351 	SND_SOC_DAPM_MUX("Dmic Mux", SND_SOC_NOPM, 0, 0,
352 			 &acp3x_dmic_mux_control),
353 };
354 
355 static const struct snd_soc_dapm_route acp3x_5682_audio_route[] = {
356 	{"Headphone Jack", NULL, "HPOL"},
357 	{"Headphone Jack", NULL, "HPOR"},
358 	{"IN1P", NULL, "Headset Mic"},
359 	{"Spk", NULL, "Speaker"},
360 	{"Dmic Mux", "Front Mic", "DMIC"},
361 	{"Dmic Mux", "Rear Mic", "DMIC"},
362 };
363 
364 static const struct snd_kcontrol_new acp3x_5682_mc_controls[] = {
365 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
366 	SOC_DAPM_PIN_SWITCH("Spk"),
367 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
368 };
369 
370 static struct snd_soc_card acp3x_5682 = {
371 	.name = "acp3xalc5682m98357",
372 	.owner = THIS_MODULE,
373 	.dai_link = acp3x_dai,
374 	.num_links = ARRAY_SIZE(acp3x_dai),
375 	.dapm_widgets = acp3x_5682_widgets,
376 	.num_dapm_widgets = ARRAY_SIZE(acp3x_5682_widgets),
377 	.dapm_routes = acp3x_5682_audio_route,
378 	.num_dapm_routes = ARRAY_SIZE(acp3x_5682_audio_route),
379 	.controls = acp3x_5682_mc_controls,
380 	.num_controls = ARRAY_SIZE(acp3x_5682_mc_controls),
381 };
382 
383 static const struct snd_soc_dapm_widget acp3x_1015_widgets[] = {
384 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
385 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
386 	SND_SOC_DAPM_MUX("Dmic Mux", SND_SOC_NOPM, 0, 0,
387 			 &acp3x_dmic_mux_control),
388 	SND_SOC_DAPM_SPK("Left Spk", NULL),
389 	SND_SOC_DAPM_SPK("Right Spk", NULL),
390 };
391 
392 static const struct snd_soc_dapm_route acp3x_1015_route[] = {
393 	{"Headphone Jack", NULL, "HPOL"},
394 	{"Headphone Jack", NULL, "HPOR"},
395 	{"IN1P", NULL, "Headset Mic"},
396 	{"Dmic Mux", "Front Mic", "DMIC"},
397 	{"Dmic Mux", "Rear Mic", "DMIC"},
398 	{"Left Spk", NULL, "Left SPO"},
399 	{"Right Spk", NULL, "Right SPO"},
400 };
401 
402 static const struct snd_kcontrol_new acp3x_mc_1015_controls[] = {
403 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
404 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
405 	SOC_DAPM_PIN_SWITCH("Left Spk"),
406 	SOC_DAPM_PIN_SWITCH("Right Spk"),
407 };
408 
409 static struct snd_soc_card acp3x_1015 = {
410 	.name = "acp3xalc56821015",
411 	.owner = THIS_MODULE,
412 	.dai_link = acp3x_dai,
413 	.num_links = ARRAY_SIZE(acp3x_dai),
414 	.dapm_widgets = acp3x_1015_widgets,
415 	.num_dapm_widgets = ARRAY_SIZE(acp3x_1015_widgets),
416 	.dapm_routes = acp3x_1015_route,
417 	.num_dapm_routes = ARRAY_SIZE(acp3x_1015_route),
418 	.codec_conf = rt1015_conf,
419 	.num_configs = ARRAY_SIZE(rt1015_conf),
420 	.controls = acp3x_mc_1015_controls,
421 	.num_controls = ARRAY_SIZE(acp3x_mc_1015_controls),
422 };
423 
424 void *soc_is_rltk_max(struct device *dev)
425 {
426 	const struct acpi_device_id *match;
427 
428 	match = acpi_match_device(dev->driver->acpi_match_table, dev);
429 	if (!match)
430 		return NULL;
431 	return (void *)match->driver_data;
432 }
433 
434 static void card_spk_dai_link_present(struct snd_soc_dai_link *links,
435 						const char *card_name)
436 {
437 	if (!strcmp(card_name, "acp3xalc56821015")) {
438 		links[1].codecs = rt1015;
439 		links[1].num_codecs = ARRAY_SIZE(rt1015);
440 	} else {
441 		links[1].codecs = max;
442 		links[1].num_codecs = ARRAY_SIZE(max);
443 	}
444 }
445 
446 static int acp3x_probe(struct platform_device *pdev)
447 {
448 	int ret;
449 	struct snd_soc_card *card;
450 	struct acp3x_platform_info *machine;
451 	struct device *dev = &pdev->dev;
452 
453 	card = (struct snd_soc_card *)soc_is_rltk_max(dev);
454 	if (!card)
455 		return -ENODEV;
456 
457 	machine = devm_kzalloc(&pdev->dev, sizeof(*machine), GFP_KERNEL);
458 	if (!machine)
459 		return -ENOMEM;
460 
461 	card_spk_dai_link_present(card->dai_link, card->name);
462 	card->dev = &pdev->dev;
463 	platform_set_drvdata(pdev, card);
464 	snd_soc_card_set_drvdata(card, machine);
465 
466 	dmic_sel = devm_gpiod_get(&pdev->dev, "dmic", GPIOD_OUT_LOW);
467 	if (IS_ERR(dmic_sel)) {
468 		dev_err(&pdev->dev, "DMIC gpio failed err=%ld\n",
469 			PTR_ERR(dmic_sel));
470 		return PTR_ERR(dmic_sel);
471 	}
472 
473 	ret = devm_snd_soc_register_card(&pdev->dev, card);
474 	if (ret) {
475 		if (ret != -EPROBE_DEFER)
476 			dev_err(&pdev->dev,
477 				"devm_snd_soc_register_card(%s) failed: %d\n",
478 				card->name, ret);
479 		else
480 			dev_dbg(&pdev->dev,
481 				"devm_snd_soc_register_card(%s) probe deferred: %d\n",
482 				card->name, ret);
483 	}
484 
485 	return ret;
486 }
487 
488 static const struct acpi_device_id acp3x_audio_acpi_match[] = {
489 	{ "AMDI5682", (unsigned long)&acp3x_5682},
490 	{ "AMDI1015", (unsigned long)&acp3x_1015},
491 	{},
492 };
493 MODULE_DEVICE_TABLE(acpi, acp3x_audio_acpi_match);
494 
495 static struct platform_driver acp3x_audio = {
496 	.driver = {
497 		.name = "acp3x-alc5682-max98357",
498 		.acpi_match_table = ACPI_PTR(acp3x_audio_acpi_match),
499 		.pm = &snd_soc_pm_ops,
500 	},
501 	.probe = acp3x_probe,
502 };
503 
504 module_platform_driver(acp3x_audio);
505 
506 MODULE_AUTHOR("akshu.agrawal@amd.com");
507 MODULE_AUTHOR("Vishnuvardhanrao.Ravulapati@amd.com");
508 MODULE_DESCRIPTION("ALC5682 ALC1015 & MAX98357 audio support");
509 MODULE_LICENSE("GPL v2");
510