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 
144 		ret = snd_soc_dai_set_pll(codec_dai, 0, RT1015_PLL_S_BCLK,
145 						64 * srate, 256 * srate);
146 		if (ret < 0)
147 			return ret;
148 		ret = snd_soc_dai_set_sysclk(codec_dai, RT1015_SCLK_S_PLL,
149 					256 * srate, SND_SOC_CLOCK_IN);
150 		if (ret < 0)
151 			return ret;
152 	}
153 	return ret;
154 }
155 
156 static void rt5682_clk_disable(void)
157 {
158 	clk_disable_unprepare(rt5682_dai_wclk);
159 }
160 
161 static const unsigned int channels[] = {
162 	DUAL_CHANNEL,
163 };
164 
165 static const unsigned int rates[] = {
166 	48000,
167 };
168 
169 static const struct snd_pcm_hw_constraint_list constraints_rates = {
170 	.count = ARRAY_SIZE(rates),
171 	.list  = rates,
172 	.mask = 0,
173 };
174 
175 static const struct snd_pcm_hw_constraint_list constraints_channels = {
176 	.count = ARRAY_SIZE(channels),
177 	.list = channels,
178 	.mask = 0,
179 };
180 
181 static int acp3x_5682_startup(struct snd_pcm_substream *substream)
182 {
183 	struct snd_pcm_runtime *runtime = substream->runtime;
184 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
185 	struct snd_soc_card *card = rtd->card;
186 	struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card);
187 
188 	machine->play_i2s_instance = I2S_SP_INSTANCE;
189 	machine->cap_i2s_instance = I2S_SP_INSTANCE;
190 
191 	runtime->hw.channels_max = DUAL_CHANNEL;
192 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
193 				   &constraints_channels);
194 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
195 				   &constraints_rates);
196 	return rt5682_clk_enable(substream);
197 }
198 
199 static int acp3x_max_startup(struct snd_pcm_substream *substream)
200 {
201 	struct snd_pcm_runtime *runtime = substream->runtime;
202 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
203 	struct snd_soc_card *card = rtd->card;
204 	struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card);
205 
206 	machine->play_i2s_instance = I2S_BT_INSTANCE;
207 
208 	runtime->hw.channels_max = DUAL_CHANNEL;
209 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
210 				   &constraints_channels);
211 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
212 				   &constraints_rates);
213 	return rt5682_clk_enable(substream);
214 }
215 
216 static int acp3x_ec_dmic0_startup(struct snd_pcm_substream *substream)
217 {
218 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
219 	struct snd_soc_card *card = rtd->card;
220 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
221 	struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card);
222 
223 	machine->cap_i2s_instance = I2S_BT_INSTANCE;
224 	snd_soc_dai_set_bclk_ratio(codec_dai, 64);
225 
226 	return rt5682_clk_enable(substream);
227 }
228 
229 static int dmic_switch;
230 
231 static int dmic_get(struct snd_kcontrol *kcontrol,
232 			 struct snd_ctl_elem_value *ucontrol)
233 {
234 	ucontrol->value.integer.value[0] = dmic_switch;
235 	return 0;
236 }
237 
238 static int dmic_set(struct snd_kcontrol *kcontrol,
239 			 struct snd_ctl_elem_value *ucontrol)
240 {
241 	if (dmic_sel) {
242 		dmic_switch = ucontrol->value.integer.value[0];
243 		gpiod_set_value(dmic_sel, dmic_switch);
244 	}
245 	return 0;
246 }
247 
248 static void rt5682_shutdown(struct snd_pcm_substream *substream)
249 {
250 	rt5682_clk_disable();
251 }
252 
253 static const struct snd_soc_ops acp3x_5682_ops = {
254 	.startup = acp3x_5682_startup,
255 	.shutdown = rt5682_shutdown,
256 };
257 
258 static const struct snd_soc_ops acp3x_max_play_ops = {
259 	.startup = acp3x_max_startup,
260 	.shutdown = rt5682_shutdown,
261 	.hw_params = acp3x_1015_hw_params,
262 };
263 
264 static const struct snd_soc_ops acp3x_ec_cap0_ops = {
265 	.startup = acp3x_ec_dmic0_startup,
266 	.shutdown = rt5682_shutdown,
267 };
268 
269 SND_SOC_DAILINK_DEF(acp3x_i2s,
270 	DAILINK_COMP_ARRAY(COMP_CPU("acp3x_i2s_playcap.0")));
271 SND_SOC_DAILINK_DEF(acp3x_bt,
272 	DAILINK_COMP_ARRAY(COMP_CPU("acp3x_i2s_playcap.2")));
273 
274 SND_SOC_DAILINK_DEF(rt5682,
275 	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5682:00", "rt5682-aif1")));
276 SND_SOC_DAILINK_DEF(max,
277 	DAILINK_COMP_ARRAY(COMP_CODEC("MX98357A:00", "HiFi")));
278 SND_SOC_DAILINK_DEF(rt1015,
279 	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC1015:00", "rt1015-aif"),
280 			COMP_CODEC("i2c-10EC1015:01", "rt1015-aif")));
281 SND_SOC_DAILINK_DEF(cros_ec,
282 	DAILINK_COMP_ARRAY(COMP_CODEC("GOOG0013:00", "EC Codec I2S RX")));
283 
284 SND_SOC_DAILINK_DEF(platform,
285 	DAILINK_COMP_ARRAY(COMP_PLATFORM("acp3x_rv_i2s_dma.0")));
286 
287 static struct snd_soc_codec_conf rt1015_conf[] = {
288 	{
289 		.dlc = COMP_CODEC_CONF("i2c-10EC1015:00"),
290 		.name_prefix = "Left",
291 	},
292 	{
293 		.dlc = COMP_CODEC_CONF("i2c-10EC1015:01"),
294 		.name_prefix = "Right",
295 	},
296 };
297 
298 static struct snd_soc_dai_link acp3x_dai[] = {
299 	[RT5682] = {
300 		.name = "acp3x-5682-play",
301 		.stream_name = "Playback",
302 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
303 				| SND_SOC_DAIFMT_CBM_CFM,
304 		.init = acp3x_5682_init,
305 		.dpcm_playback = 1,
306 		.dpcm_capture = 1,
307 		.ops = &acp3x_5682_ops,
308 		SND_SOC_DAILINK_REG(acp3x_i2s, rt5682, platform),
309 	},
310 	[MAX] = {
311 		.name = "acp3x-max98357-play",
312 		.stream_name = "HiFi Playback",
313 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
314 				| SND_SOC_DAIFMT_CBS_CFS,
315 		.dpcm_playback = 1,
316 		.ops = &acp3x_max_play_ops,
317 		.cpus = acp3x_bt,
318 		.num_cpus = ARRAY_SIZE(acp3x_bt),
319 		.platforms = platform,
320 		.num_platforms = ARRAY_SIZE(platform),
321 	},
322 	[EC] = {
323 		.name = "acp3x-ec-dmic0-capture",
324 		.stream_name = "Capture DMIC0",
325 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
326 				| SND_SOC_DAIFMT_CBS_CFS,
327 		.dpcm_capture = 1,
328 		.ops = &acp3x_ec_cap0_ops,
329 		SND_SOC_DAILINK_REG(acp3x_bt, cros_ec, platform),
330 	},
331 };
332 
333 static const char * const dmic_mux_text[] = {
334 	"Front Mic",
335 	"Rear Mic",
336 };
337 
338 static SOC_ENUM_SINGLE_DECL(
339 		acp3x_dmic_enum, SND_SOC_NOPM, 0, dmic_mux_text);
340 
341 static const struct snd_kcontrol_new acp3x_dmic_mux_control =
342 	SOC_DAPM_ENUM_EXT("DMIC Select Mux", acp3x_dmic_enum,
343 			  dmic_get, dmic_set);
344 
345 static const struct snd_soc_dapm_widget acp3x_5682_widgets[] = {
346 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
347 	SND_SOC_DAPM_SPK("Spk", NULL),
348 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
349 	SND_SOC_DAPM_MUX("Dmic Mux", SND_SOC_NOPM, 0, 0,
350 			 &acp3x_dmic_mux_control),
351 };
352 
353 static const struct snd_soc_dapm_route acp3x_5682_audio_route[] = {
354 	{"Headphone Jack", NULL, "HPOL"},
355 	{"Headphone Jack", NULL, "HPOR"},
356 	{"IN1P", NULL, "Headset Mic"},
357 	{"Spk", NULL, "Speaker"},
358 	{"Dmic Mux", "Front Mic", "DMIC"},
359 	{"Dmic Mux", "Rear Mic", "DMIC"},
360 };
361 
362 static const struct snd_kcontrol_new acp3x_5682_mc_controls[] = {
363 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
364 	SOC_DAPM_PIN_SWITCH("Spk"),
365 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
366 };
367 
368 static struct snd_soc_card acp3x_5682 = {
369 	.name = "acp3xalc5682m98357",
370 	.owner = THIS_MODULE,
371 	.dai_link = acp3x_dai,
372 	.num_links = ARRAY_SIZE(acp3x_dai),
373 	.dapm_widgets = acp3x_5682_widgets,
374 	.num_dapm_widgets = ARRAY_SIZE(acp3x_5682_widgets),
375 	.dapm_routes = acp3x_5682_audio_route,
376 	.num_dapm_routes = ARRAY_SIZE(acp3x_5682_audio_route),
377 	.controls = acp3x_5682_mc_controls,
378 	.num_controls = ARRAY_SIZE(acp3x_5682_mc_controls),
379 };
380 
381 static const struct snd_soc_dapm_widget acp3x_1015_widgets[] = {
382 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
383 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
384 	SND_SOC_DAPM_MUX("Dmic Mux", SND_SOC_NOPM, 0, 0,
385 			 &acp3x_dmic_mux_control),
386 	SND_SOC_DAPM_SPK("Left Spk", NULL),
387 	SND_SOC_DAPM_SPK("Right Spk", NULL),
388 };
389 
390 static const struct snd_soc_dapm_route acp3x_1015_route[] = {
391 	{"Headphone Jack", NULL, "HPOL"},
392 	{"Headphone Jack", NULL, "HPOR"},
393 	{"IN1P", NULL, "Headset Mic"},
394 	{"Dmic Mux", "Front Mic", "DMIC"},
395 	{"Dmic Mux", "Rear Mic", "DMIC"},
396 	{"Left Spk", NULL, "Left SPO"},
397 	{"Right Spk", NULL, "Right SPO"},
398 };
399 
400 static const struct snd_kcontrol_new acp3x_mc_1015_controls[] = {
401 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
402 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
403 	SOC_DAPM_PIN_SWITCH("Left Spk"),
404 	SOC_DAPM_PIN_SWITCH("Right Spk"),
405 };
406 
407 static struct snd_soc_card acp3x_1015 = {
408 	.name = "acp3xalc56821015",
409 	.owner = THIS_MODULE,
410 	.dai_link = acp3x_dai,
411 	.num_links = ARRAY_SIZE(acp3x_dai),
412 	.dapm_widgets = acp3x_1015_widgets,
413 	.num_dapm_widgets = ARRAY_SIZE(acp3x_1015_widgets),
414 	.dapm_routes = acp3x_1015_route,
415 	.num_dapm_routes = ARRAY_SIZE(acp3x_1015_route),
416 	.codec_conf = rt1015_conf,
417 	.num_configs = ARRAY_SIZE(rt1015_conf),
418 	.controls = acp3x_mc_1015_controls,
419 	.num_controls = ARRAY_SIZE(acp3x_mc_1015_controls),
420 };
421 
422 void *soc_is_rltk_max(struct device *dev)
423 {
424 	const struct acpi_device_id *match;
425 
426 	match = acpi_match_device(dev->driver->acpi_match_table, dev);
427 	if (!match)
428 		return NULL;
429 	return (void *)match->driver_data;
430 }
431 
432 static void card_spk_dai_link_present(struct snd_soc_dai_link *links,
433 						const char *card_name)
434 {
435 	if (!strcmp(card_name, "acp3xalc56821015")) {
436 		links[1].codecs = rt1015;
437 		links[1].num_codecs = ARRAY_SIZE(rt1015);
438 	} else {
439 		links[1].codecs = max;
440 		links[1].num_codecs = ARRAY_SIZE(max);
441 	}
442 }
443 
444 static int acp3x_probe(struct platform_device *pdev)
445 {
446 	int ret;
447 	struct snd_soc_card *card;
448 	struct acp3x_platform_info *machine;
449 	struct device *dev = &pdev->dev;
450 
451 	card = (struct snd_soc_card *)soc_is_rltk_max(dev);
452 	if (!card)
453 		return -ENODEV;
454 
455 	machine = devm_kzalloc(&pdev->dev, sizeof(*machine), GFP_KERNEL);
456 	if (!machine)
457 		return -ENOMEM;
458 
459 	card_spk_dai_link_present(card->dai_link, card->name);
460 	card->dev = &pdev->dev;
461 	platform_set_drvdata(pdev, card);
462 	snd_soc_card_set_drvdata(card, machine);
463 
464 	dmic_sel = devm_gpiod_get(&pdev->dev, "dmic", GPIOD_OUT_LOW);
465 	if (IS_ERR(dmic_sel)) {
466 		dev_err(&pdev->dev, "DMIC gpio failed err=%ld\n",
467 			PTR_ERR(dmic_sel));
468 		return PTR_ERR(dmic_sel);
469 	}
470 
471 	ret = devm_snd_soc_register_card(&pdev->dev, card);
472 	if (ret) {
473 		if (ret != -EPROBE_DEFER)
474 			dev_err(&pdev->dev,
475 				"devm_snd_soc_register_card(%s) failed: %d\n",
476 				card->name, ret);
477 		else
478 			dev_dbg(&pdev->dev,
479 				"devm_snd_soc_register_card(%s) probe deferred: %d\n",
480 				card->name, ret);
481 	}
482 
483 	return ret;
484 }
485 
486 static const struct acpi_device_id acp3x_audio_acpi_match[] = {
487 	{ "AMDI5682", (unsigned long)&acp3x_5682},
488 	{ "AMDI1015", (unsigned long)&acp3x_1015},
489 	{},
490 };
491 MODULE_DEVICE_TABLE(acpi, acp3x_audio_acpi_match);
492 
493 static struct platform_driver acp3x_audio = {
494 	.driver = {
495 		.name = "acp3x-alc5682-max98357",
496 		.acpi_match_table = ACPI_PTR(acp3x_audio_acpi_match),
497 		.pm = &snd_soc_pm_ops,
498 	},
499 	.probe = acp3x_probe,
500 };
501 
502 module_platform_driver(acp3x_audio);
503 
504 MODULE_AUTHOR("akshu.agrawal@amd.com");
505 MODULE_AUTHOR("Vishnuvardhanrao.Ravulapati@amd.com");
506 MODULE_DESCRIPTION("ALC5682 ALC1015 & MAX98357 audio support");
507 MODULE_LICENSE("GPL v2");
508