xref: /linux/sound/soc/amd/acp3x-rt5682-max9836.c (revision 718693352d8bcea65276615f4f8c8d531246b644)
16b8e4e7dSAkshu Agrawal // SPDX-License-Identifier: GPL-2.0+
26b8e4e7dSAkshu Agrawal //
36b8e4e7dSAkshu Agrawal // Machine driver for AMD ACP Audio engine using DA7219 & MAX98357 codec.
46b8e4e7dSAkshu Agrawal //
56b8e4e7dSAkshu Agrawal //Copyright 2016 Advanced Micro Devices, Inc.
66b8e4e7dSAkshu Agrawal 
76b8e4e7dSAkshu Agrawal #include <sound/core.h>
86b8e4e7dSAkshu Agrawal #include <sound/soc.h>
96b8e4e7dSAkshu Agrawal #include <sound/pcm.h>
106b8e4e7dSAkshu Agrawal #include <sound/pcm_params.h>
116b8e4e7dSAkshu Agrawal #include <sound/soc-dapm.h>
126b8e4e7dSAkshu Agrawal #include <sound/jack.h>
136b8e4e7dSAkshu Agrawal #include <linux/clk.h>
146b8e4e7dSAkshu Agrawal #include <linux/gpio.h>
1572c3b2b0SAkshu Agrawal #include <linux/gpio/consumer.h>
166b8e4e7dSAkshu Agrawal #include <linux/module.h>
176b8e4e7dSAkshu Agrawal #include <linux/i2c.h>
186b8e4e7dSAkshu Agrawal #include <linux/input.h>
1914beacccSYueHaibing #include <linux/io.h>
206b8e4e7dSAkshu Agrawal #include <linux/acpi.h>
216b8e4e7dSAkshu Agrawal 
226b8e4e7dSAkshu Agrawal #include "raven/acp3x.h"
236b8e4e7dSAkshu Agrawal #include "../codecs/rt5682.h"
24414e3cabSRavulapati Vishnu vardhan rao #include "../codecs/rt1015.h"
256b8e4e7dSAkshu Agrawal 
266b8e4e7dSAkshu Agrawal #define PCO_PLAT_CLK 48000000
276b8e4e7dSAkshu Agrawal #define RT5682_PLL_FREQ (48000 * 512)
286b8e4e7dSAkshu Agrawal #define DUAL_CHANNEL		2
296b8e4e7dSAkshu Agrawal 
306b8e4e7dSAkshu Agrawal static struct snd_soc_jack pco_jack;
316b8e4e7dSAkshu Agrawal static struct clk *rt5682_dai_wclk;
326b8e4e7dSAkshu Agrawal static struct clk *rt5682_dai_bclk;
3372c3b2b0SAkshu Agrawal static struct gpio_desc *dmic_sel;
340fe4b561SRavulapati Vishnu vardhan rao void *soc_is_rltk_max(struct device *dev);
356b8e4e7dSAkshu Agrawal 
36f7b2651bSRavulapati Vishnu vardhan rao enum {
37f7b2651bSRavulapati Vishnu vardhan rao 	RT5682 = 0,
38f7b2651bSRavulapati Vishnu vardhan rao 	MAX,
39f7b2651bSRavulapati Vishnu vardhan rao 	EC,
40f7b2651bSRavulapati Vishnu vardhan rao };
41f7b2651bSRavulapati Vishnu vardhan rao 
426b8e4e7dSAkshu Agrawal static int acp3x_5682_init(struct snd_soc_pcm_runtime *rtd)
436b8e4e7dSAkshu Agrawal {
446b8e4e7dSAkshu Agrawal 	int ret;
456b8e4e7dSAkshu Agrawal 	struct snd_soc_card *card = rtd->card;
46b09b22fcSKuninori Morimoto 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
476b8e4e7dSAkshu Agrawal 	struct snd_soc_component *component = codec_dai->component;
486b8e4e7dSAkshu Agrawal 
496b8e4e7dSAkshu Agrawal 	dev_info(rtd->dev, "codec dai name = %s\n", codec_dai->name);
506b8e4e7dSAkshu Agrawal 
516b8e4e7dSAkshu Agrawal 	/* set rt5682 dai fmt */
526b8e4e7dSAkshu Agrawal 	ret =  snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S
536b8e4e7dSAkshu Agrawal 			| SND_SOC_DAIFMT_NB_NF
546b8e4e7dSAkshu Agrawal 			| SND_SOC_DAIFMT_CBM_CFM);
556b8e4e7dSAkshu Agrawal 	if (ret < 0) {
566b8e4e7dSAkshu Agrawal 		dev_err(rtd->card->dev,
576b8e4e7dSAkshu Agrawal 				"Failed to set rt5682 dai fmt: %d\n", ret);
586b8e4e7dSAkshu Agrawal 		return ret;
596b8e4e7dSAkshu Agrawal 	}
606b8e4e7dSAkshu Agrawal 
616b8e4e7dSAkshu Agrawal 	/* set codec PLL */
626b8e4e7dSAkshu Agrawal 	ret = snd_soc_dai_set_pll(codec_dai, RT5682_PLL2, RT5682_PLL2_S_MCLK,
636b8e4e7dSAkshu Agrawal 				  PCO_PLAT_CLK, RT5682_PLL_FREQ);
646b8e4e7dSAkshu Agrawal 	if (ret < 0) {
656b8e4e7dSAkshu Agrawal 		dev_err(rtd->dev, "can't set rt5682 PLL: %d\n", ret);
666b8e4e7dSAkshu Agrawal 		return ret;
676b8e4e7dSAkshu Agrawal 	}
686b8e4e7dSAkshu Agrawal 
696b8e4e7dSAkshu Agrawal 	/* Set codec sysclk */
706b8e4e7dSAkshu Agrawal 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5682_SCLK_S_PLL2,
716b8e4e7dSAkshu Agrawal 			RT5682_PLL_FREQ, SND_SOC_CLOCK_IN);
726b8e4e7dSAkshu Agrawal 	if (ret < 0) {
736b8e4e7dSAkshu Agrawal 		dev_err(rtd->dev,
746b8e4e7dSAkshu Agrawal 			"Failed to set rt5682 SYSCLK: %d\n", ret);
756b8e4e7dSAkshu Agrawal 		return ret;
766b8e4e7dSAkshu Agrawal 	}
776b8e4e7dSAkshu Agrawal 
786b8e4e7dSAkshu Agrawal 	/* Set tdm/i2s1 master bclk ratio */
796b8e4e7dSAkshu Agrawal 	ret = snd_soc_dai_set_bclk_ratio(codec_dai, 64);
806b8e4e7dSAkshu Agrawal 	if (ret < 0) {
816b8e4e7dSAkshu Agrawal 		dev_err(rtd->dev,
826b8e4e7dSAkshu Agrawal 			"Failed to set rt5682 tdm bclk ratio: %d\n", ret);
836b8e4e7dSAkshu Agrawal 		return ret;
846b8e4e7dSAkshu Agrawal 	}
856b8e4e7dSAkshu Agrawal 
866b8e4e7dSAkshu Agrawal 	rt5682_dai_wclk = clk_get(component->dev, "rt5682-dai-wclk");
876b8e4e7dSAkshu Agrawal 	rt5682_dai_bclk = clk_get(component->dev, "rt5682-dai-bclk");
886b8e4e7dSAkshu Agrawal 
896b8e4e7dSAkshu Agrawal 	ret = snd_soc_card_jack_new(card, "Headset Jack",
906b8e4e7dSAkshu Agrawal 				SND_JACK_HEADSET | SND_JACK_LINEOUT |
916b8e4e7dSAkshu Agrawal 				SND_JACK_BTN_0 | SND_JACK_BTN_1 |
926b8e4e7dSAkshu Agrawal 				SND_JACK_BTN_2 | SND_JACK_BTN_3,
936b8e4e7dSAkshu Agrawal 				&pco_jack, NULL, 0);
946b8e4e7dSAkshu Agrawal 	if (ret) {
956b8e4e7dSAkshu Agrawal 		dev_err(card->dev, "HP jack creation failed %d\n", ret);
966b8e4e7dSAkshu Agrawal 		return ret;
976b8e4e7dSAkshu Agrawal 	}
986b8e4e7dSAkshu Agrawal 
996b8e4e7dSAkshu Agrawal 	snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
1008dbcfcfcSAkshu Agrawal 	snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
1018dbcfcfcSAkshu Agrawal 	snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
1028dbcfcfcSAkshu Agrawal 	snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
1036b8e4e7dSAkshu Agrawal 
1046b8e4e7dSAkshu Agrawal 	ret = snd_soc_component_set_jack(component, &pco_jack, NULL);
1056b8e4e7dSAkshu Agrawal 	if (ret) {
1066b8e4e7dSAkshu Agrawal 		dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
1076b8e4e7dSAkshu Agrawal 		return ret;
1086b8e4e7dSAkshu Agrawal 	}
1096b8e4e7dSAkshu Agrawal 
1106b8e4e7dSAkshu Agrawal 	return ret;
1116b8e4e7dSAkshu Agrawal }
1126b8e4e7dSAkshu Agrawal 
1136b8e4e7dSAkshu Agrawal static int rt5682_clk_enable(struct snd_pcm_substream *substream)
1146b8e4e7dSAkshu Agrawal {
1156b8e4e7dSAkshu Agrawal 	int ret = 0;
116ded00543SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1176b8e4e7dSAkshu Agrawal 
1186b8e4e7dSAkshu Agrawal 	/* RT5682 will support only 48K output with 48M mclk */
1196b8e4e7dSAkshu Agrawal 	clk_set_rate(rt5682_dai_wclk, 48000);
1206b8e4e7dSAkshu Agrawal 	clk_set_rate(rt5682_dai_bclk, 48000 * 64);
1216b8e4e7dSAkshu Agrawal 	ret = clk_prepare_enable(rt5682_dai_wclk);
1226b8e4e7dSAkshu Agrawal 	if (ret < 0) {
1236b8e4e7dSAkshu Agrawal 		dev_err(rtd->dev, "can't enable wclk %d\n", ret);
1246b8e4e7dSAkshu Agrawal 		return ret;
1256b8e4e7dSAkshu Agrawal 	}
1266b8e4e7dSAkshu Agrawal 
1276b8e4e7dSAkshu Agrawal 	return ret;
1286b8e4e7dSAkshu Agrawal }
1296b8e4e7dSAkshu Agrawal 
130c3936ba9SRavulapati Vishnu vardhan rao static int acp3x_1015_hw_params(struct snd_pcm_substream *substream,
131c3936ba9SRavulapati Vishnu vardhan rao 					struct snd_pcm_hw_params *params)
132c3936ba9SRavulapati Vishnu vardhan rao {
133c3936ba9SRavulapati Vishnu vardhan rao 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
134c3936ba9SRavulapati Vishnu vardhan rao 	struct snd_soc_dai *codec_dai;
135c3936ba9SRavulapati Vishnu vardhan rao 	int srate, i, ret;
136c3936ba9SRavulapati Vishnu vardhan rao 
137c3936ba9SRavulapati Vishnu vardhan rao 	ret = 0;
138c3936ba9SRavulapati Vishnu vardhan rao 	srate = params_rate(params);
139c3936ba9SRavulapati Vishnu vardhan rao 
140c3936ba9SRavulapati Vishnu vardhan rao 	for_each_rtd_codec_dais(rtd, i, codec_dai) {
141ea7dc097SRavulapati Vishnu vardhan rao 		if (strcmp(codec_dai->name, "rt1015-aif"))
142c3936ba9SRavulapati Vishnu vardhan rao 			continue;
14381a6320dSTzung-Bi Shih 
144c3936ba9SRavulapati Vishnu vardhan rao 		ret = snd_soc_dai_set_pll(codec_dai, 0, RT1015_PLL_S_BCLK,
145c3936ba9SRavulapati Vishnu vardhan rao 						64 * srate, 256 * srate);
146c3936ba9SRavulapati Vishnu vardhan rao 		if (ret < 0)
147c3936ba9SRavulapati Vishnu vardhan rao 			return ret;
148c3936ba9SRavulapati Vishnu vardhan rao 		ret = snd_soc_dai_set_sysclk(codec_dai, RT1015_SCLK_S_PLL,
149c3936ba9SRavulapati Vishnu vardhan rao 					256 * srate, SND_SOC_CLOCK_IN);
150c3936ba9SRavulapati Vishnu vardhan rao 		if (ret < 0)
151c3936ba9SRavulapati Vishnu vardhan rao 			return ret;
152c3936ba9SRavulapati Vishnu vardhan rao 	}
153c3936ba9SRavulapati Vishnu vardhan rao 	return ret;
154c3936ba9SRavulapati Vishnu vardhan rao }
155c3936ba9SRavulapati Vishnu vardhan rao 
1566b8e4e7dSAkshu Agrawal static void rt5682_clk_disable(void)
1576b8e4e7dSAkshu Agrawal {
1586b8e4e7dSAkshu Agrawal 	clk_disable_unprepare(rt5682_dai_wclk);
1596b8e4e7dSAkshu Agrawal }
1606b8e4e7dSAkshu Agrawal 
1616b8e4e7dSAkshu Agrawal static const unsigned int channels[] = {
1626b8e4e7dSAkshu Agrawal 	DUAL_CHANNEL,
1636b8e4e7dSAkshu Agrawal };
1646b8e4e7dSAkshu Agrawal 
1656b8e4e7dSAkshu Agrawal static const unsigned int rates[] = {
1666b8e4e7dSAkshu Agrawal 	48000,
1676b8e4e7dSAkshu Agrawal };
1686b8e4e7dSAkshu Agrawal 
1696b8e4e7dSAkshu Agrawal static const struct snd_pcm_hw_constraint_list constraints_rates = {
1706b8e4e7dSAkshu Agrawal 	.count = ARRAY_SIZE(rates),
1716b8e4e7dSAkshu Agrawal 	.list  = rates,
1726b8e4e7dSAkshu Agrawal 	.mask = 0,
1736b8e4e7dSAkshu Agrawal };
1746b8e4e7dSAkshu Agrawal 
1756b8e4e7dSAkshu Agrawal static const struct snd_pcm_hw_constraint_list constraints_channels = {
1766b8e4e7dSAkshu Agrawal 	.count = ARRAY_SIZE(channels),
1776b8e4e7dSAkshu Agrawal 	.list = channels,
1786b8e4e7dSAkshu Agrawal 	.mask = 0,
1796b8e4e7dSAkshu Agrawal };
1806b8e4e7dSAkshu Agrawal 
1816b8e4e7dSAkshu Agrawal static int acp3x_5682_startup(struct snd_pcm_substream *substream)
1826b8e4e7dSAkshu Agrawal {
1836b8e4e7dSAkshu Agrawal 	struct snd_pcm_runtime *runtime = substream->runtime;
184ded00543SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
1856b8e4e7dSAkshu Agrawal 	struct snd_soc_card *card = rtd->card;
1866b8e4e7dSAkshu Agrawal 	struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card);
1876b8e4e7dSAkshu Agrawal 
1886b8e4e7dSAkshu Agrawal 	machine->play_i2s_instance = I2S_SP_INSTANCE;
1896b8e4e7dSAkshu Agrawal 	machine->cap_i2s_instance = I2S_SP_INSTANCE;
1906b8e4e7dSAkshu Agrawal 
1916b8e4e7dSAkshu Agrawal 	runtime->hw.channels_max = DUAL_CHANNEL;
1926b8e4e7dSAkshu Agrawal 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
1936b8e4e7dSAkshu Agrawal 				   &constraints_channels);
1946b8e4e7dSAkshu Agrawal 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
1956b8e4e7dSAkshu Agrawal 				   &constraints_rates);
1966b8e4e7dSAkshu Agrawal 	return rt5682_clk_enable(substream);
1976b8e4e7dSAkshu Agrawal }
1986b8e4e7dSAkshu Agrawal 
1996b8e4e7dSAkshu Agrawal static int acp3x_max_startup(struct snd_pcm_substream *substream)
2006b8e4e7dSAkshu Agrawal {
2016b8e4e7dSAkshu Agrawal 	struct snd_pcm_runtime *runtime = substream->runtime;
202ded00543SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
2036b8e4e7dSAkshu Agrawal 	struct snd_soc_card *card = rtd->card;
2046b8e4e7dSAkshu Agrawal 	struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card);
2056b8e4e7dSAkshu Agrawal 
2066b8e4e7dSAkshu Agrawal 	machine->play_i2s_instance = I2S_BT_INSTANCE;
2076b8e4e7dSAkshu Agrawal 
2086b8e4e7dSAkshu Agrawal 	runtime->hw.channels_max = DUAL_CHANNEL;
2096b8e4e7dSAkshu Agrawal 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
2106b8e4e7dSAkshu Agrawal 				   &constraints_channels);
2116b8e4e7dSAkshu Agrawal 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
2126b8e4e7dSAkshu Agrawal 				   &constraints_rates);
2136b8e4e7dSAkshu Agrawal 	return rt5682_clk_enable(substream);
2146b8e4e7dSAkshu Agrawal }
2156b8e4e7dSAkshu Agrawal 
21672c3b2b0SAkshu Agrawal static int acp3x_ec_dmic0_startup(struct snd_pcm_substream *substream)
2176b8e4e7dSAkshu Agrawal {
218ded00543SKuninori Morimoto 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
2196b8e4e7dSAkshu Agrawal 	struct snd_soc_card *card = rtd->card;
220b09b22fcSKuninori Morimoto 	struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
2216b8e4e7dSAkshu Agrawal 	struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card);
2226b8e4e7dSAkshu Agrawal 
2236b8e4e7dSAkshu Agrawal 	machine->cap_i2s_instance = I2S_BT_INSTANCE;
2246b8e4e7dSAkshu Agrawal 	snd_soc_dai_set_bclk_ratio(codec_dai, 64);
22572c3b2b0SAkshu Agrawal 
22672c3b2b0SAkshu Agrawal 	return rt5682_clk_enable(substream);
22772c3b2b0SAkshu Agrawal }
22872c3b2b0SAkshu Agrawal 
229b7a742cfSAkshu Agrawal static int dmic_switch;
230b7a742cfSAkshu Agrawal 
231b7a742cfSAkshu Agrawal static int dmic_get(struct snd_kcontrol *kcontrol,
232b7a742cfSAkshu Agrawal 			 struct snd_ctl_elem_value *ucontrol)
23372c3b2b0SAkshu Agrawal {
234b7a742cfSAkshu Agrawal 	ucontrol->value.integer.value[0] = dmic_switch;
235b7a742cfSAkshu Agrawal 	return 0;
236b7a742cfSAkshu Agrawal }
23772c3b2b0SAkshu Agrawal 
238b7a742cfSAkshu Agrawal static int dmic_set(struct snd_kcontrol *kcontrol,
239b7a742cfSAkshu Agrawal 			 struct snd_ctl_elem_value *ucontrol)
240b7a742cfSAkshu Agrawal {
241b7a742cfSAkshu Agrawal 	if (dmic_sel) {
242b7a742cfSAkshu Agrawal 		dmic_switch = ucontrol->value.integer.value[0];
243b7a742cfSAkshu Agrawal 		gpiod_set_value(dmic_sel, dmic_switch);
244b7a742cfSAkshu Agrawal 	}
245b7a742cfSAkshu Agrawal 	return 0;
2466b8e4e7dSAkshu Agrawal }
2476b8e4e7dSAkshu Agrawal 
2486b8e4e7dSAkshu Agrawal static void rt5682_shutdown(struct snd_pcm_substream *substream)
2496b8e4e7dSAkshu Agrawal {
2506b8e4e7dSAkshu Agrawal 	rt5682_clk_disable();
2516b8e4e7dSAkshu Agrawal }
2526b8e4e7dSAkshu Agrawal 
2536b8e4e7dSAkshu Agrawal static const struct snd_soc_ops acp3x_5682_ops = {
2546b8e4e7dSAkshu Agrawal 	.startup = acp3x_5682_startup,
2556b8e4e7dSAkshu Agrawal 	.shutdown = rt5682_shutdown,
2566b8e4e7dSAkshu Agrawal };
2576b8e4e7dSAkshu Agrawal 
2586b8e4e7dSAkshu Agrawal static const struct snd_soc_ops acp3x_max_play_ops = {
2596b8e4e7dSAkshu Agrawal 	.startup = acp3x_max_startup,
2606b8e4e7dSAkshu Agrawal 	.shutdown = rt5682_shutdown,
261c3936ba9SRavulapati Vishnu vardhan rao 	.hw_params = acp3x_1015_hw_params,
2626b8e4e7dSAkshu Agrawal };
2636b8e4e7dSAkshu Agrawal 
26472c3b2b0SAkshu Agrawal static const struct snd_soc_ops acp3x_ec_cap0_ops = {
26572c3b2b0SAkshu Agrawal 	.startup = acp3x_ec_dmic0_startup,
26672c3b2b0SAkshu Agrawal 	.shutdown = rt5682_shutdown,
26772c3b2b0SAkshu Agrawal };
26872c3b2b0SAkshu Agrawal 
2696b8e4e7dSAkshu Agrawal SND_SOC_DAILINK_DEF(acp3x_i2s,
2706b8e4e7dSAkshu Agrawal 	DAILINK_COMP_ARRAY(COMP_CPU("acp3x_i2s_playcap.0")));
2716b8e4e7dSAkshu Agrawal SND_SOC_DAILINK_DEF(acp3x_bt,
2726b8e4e7dSAkshu Agrawal 	DAILINK_COMP_ARRAY(COMP_CPU("acp3x_i2s_playcap.2")));
2736b8e4e7dSAkshu Agrawal 
2746b8e4e7dSAkshu Agrawal SND_SOC_DAILINK_DEF(rt5682,
2756b8e4e7dSAkshu Agrawal 	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5682:00", "rt5682-aif1")));
2766b8e4e7dSAkshu Agrawal SND_SOC_DAILINK_DEF(max,
2776b8e4e7dSAkshu Agrawal 	DAILINK_COMP_ARRAY(COMP_CODEC("MX98357A:00", "HiFi")));
27826e33ed9SVijendar Mukunda SND_SOC_DAILINK_DEF(rt1015p,
27926e33ed9SVijendar Mukunda 	DAILINK_COMP_ARRAY(COMP_CODEC("RTL1015:00", "HiFi")));
280f7b2651bSRavulapati Vishnu vardhan rao SND_SOC_DAILINK_DEF(rt1015,
281f7b2651bSRavulapati Vishnu vardhan rao 	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC1015:00", "rt1015-aif"),
282f7b2651bSRavulapati Vishnu vardhan rao 			COMP_CODEC("i2c-10EC1015:01", "rt1015-aif")));
2836b8e4e7dSAkshu Agrawal SND_SOC_DAILINK_DEF(cros_ec,
2846b8e4e7dSAkshu Agrawal 	DAILINK_COMP_ARRAY(COMP_CODEC("GOOG0013:00", "EC Codec I2S RX")));
2856b8e4e7dSAkshu Agrawal 
2866b8e4e7dSAkshu Agrawal SND_SOC_DAILINK_DEF(platform,
2876b8e4e7dSAkshu Agrawal 	DAILINK_COMP_ARRAY(COMP_PLATFORM("acp3x_rv_i2s_dma.0")));
2886b8e4e7dSAkshu Agrawal 
289414e3cabSRavulapati Vishnu vardhan rao static struct snd_soc_codec_conf rt1015_conf[] = {
290414e3cabSRavulapati Vishnu vardhan rao 	{
291414e3cabSRavulapati Vishnu vardhan rao 		.dlc = COMP_CODEC_CONF("i2c-10EC1015:00"),
292414e3cabSRavulapati Vishnu vardhan rao 		.name_prefix = "Left",
293414e3cabSRavulapati Vishnu vardhan rao 	},
294414e3cabSRavulapati Vishnu vardhan rao 	{
295414e3cabSRavulapati Vishnu vardhan rao 		.dlc = COMP_CODEC_CONF("i2c-10EC1015:01"),
296414e3cabSRavulapati Vishnu vardhan rao 		.name_prefix = "Right",
297414e3cabSRavulapati Vishnu vardhan rao 	},
298414e3cabSRavulapati Vishnu vardhan rao };
299414e3cabSRavulapati Vishnu vardhan rao 
300414e3cabSRavulapati Vishnu vardhan rao static struct snd_soc_dai_link acp3x_dai[] = {
301f7b2651bSRavulapati Vishnu vardhan rao 	[RT5682] = {
3026b8e4e7dSAkshu Agrawal 		.name = "acp3x-5682-play",
3036b8e4e7dSAkshu Agrawal 		.stream_name = "Playback",
3046b8e4e7dSAkshu Agrawal 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
3056b8e4e7dSAkshu Agrawal 				| SND_SOC_DAIFMT_CBM_CFM,
3066b8e4e7dSAkshu Agrawal 		.init = acp3x_5682_init,
3076b8e4e7dSAkshu Agrawal 		.dpcm_playback = 1,
3086b8e4e7dSAkshu Agrawal 		.dpcm_capture = 1,
3096b8e4e7dSAkshu Agrawal 		.ops = &acp3x_5682_ops,
3106b8e4e7dSAkshu Agrawal 		SND_SOC_DAILINK_REG(acp3x_i2s, rt5682, platform),
3116b8e4e7dSAkshu Agrawal 	},
312f7b2651bSRavulapati Vishnu vardhan rao 	[MAX] = {
3136b8e4e7dSAkshu Agrawal 		.name = "acp3x-max98357-play",
3146b8e4e7dSAkshu Agrawal 		.stream_name = "HiFi Playback",
3156b8e4e7dSAkshu Agrawal 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
316f7b2651bSRavulapati Vishnu vardhan rao 				| SND_SOC_DAIFMT_CBS_CFS,
3176b8e4e7dSAkshu Agrawal 		.dpcm_playback = 1,
3186b8e4e7dSAkshu Agrawal 		.ops = &acp3x_max_play_ops,
319f7b2651bSRavulapati Vishnu vardhan rao 		.cpus = acp3x_bt,
320f7b2651bSRavulapati Vishnu vardhan rao 		.num_cpus = ARRAY_SIZE(acp3x_bt),
321f7b2651bSRavulapati Vishnu vardhan rao 		.platforms = platform,
322f7b2651bSRavulapati Vishnu vardhan rao 		.num_platforms = ARRAY_SIZE(platform),
3236b8e4e7dSAkshu Agrawal 	},
324f7b2651bSRavulapati Vishnu vardhan rao 	[EC] = {
32572c3b2b0SAkshu Agrawal 		.name = "acp3x-ec-dmic0-capture",
32672c3b2b0SAkshu Agrawal 		.stream_name = "Capture DMIC0",
3276b8e4e7dSAkshu Agrawal 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
3286b8e4e7dSAkshu Agrawal 				| SND_SOC_DAIFMT_CBS_CFS,
3296b8e4e7dSAkshu Agrawal 		.dpcm_capture = 1,
33072c3b2b0SAkshu Agrawal 		.ops = &acp3x_ec_cap0_ops,
33172c3b2b0SAkshu Agrawal 		SND_SOC_DAILINK_REG(acp3x_bt, cros_ec, platform),
33272c3b2b0SAkshu Agrawal 	},
3336b8e4e7dSAkshu Agrawal };
3346b8e4e7dSAkshu Agrawal 
335b7a742cfSAkshu Agrawal static const char * const dmic_mux_text[] = {
336b7a742cfSAkshu Agrawal 	"Front Mic",
337b7a742cfSAkshu Agrawal 	"Rear Mic",
338b7a742cfSAkshu Agrawal };
339b7a742cfSAkshu Agrawal 
340b7a742cfSAkshu Agrawal static SOC_ENUM_SINGLE_DECL(
341b7a742cfSAkshu Agrawal 		acp3x_dmic_enum, SND_SOC_NOPM, 0, dmic_mux_text);
342b7a742cfSAkshu Agrawal 
343b7a742cfSAkshu Agrawal static const struct snd_kcontrol_new acp3x_dmic_mux_control =
344b7a742cfSAkshu Agrawal 	SOC_DAPM_ENUM_EXT("DMIC Select Mux", acp3x_dmic_enum,
345b7a742cfSAkshu Agrawal 			  dmic_get, dmic_set);
346b7a742cfSAkshu Agrawal 
3479c04b5a4SRavulapati Vishnu vardhan rao static const struct snd_soc_dapm_widget acp3x_5682_widgets[] = {
3486b8e4e7dSAkshu Agrawal 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
3496b8e4e7dSAkshu Agrawal 	SND_SOC_DAPM_SPK("Spk", NULL),
3506b8e4e7dSAkshu Agrawal 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
351b7a742cfSAkshu Agrawal 	SND_SOC_DAPM_MUX("Dmic Mux", SND_SOC_NOPM, 0, 0,
352b7a742cfSAkshu Agrawal 			 &acp3x_dmic_mux_control),
3536b8e4e7dSAkshu Agrawal };
3546b8e4e7dSAkshu Agrawal 
3559c04b5a4SRavulapati Vishnu vardhan rao static const struct snd_soc_dapm_route acp3x_5682_audio_route[] = {
3566b8e4e7dSAkshu Agrawal 	{"Headphone Jack", NULL, "HPOL"},
3576b8e4e7dSAkshu Agrawal 	{"Headphone Jack", NULL, "HPOR"},
3586b8e4e7dSAkshu Agrawal 	{"IN1P", NULL, "Headset Mic"},
3596b8e4e7dSAkshu Agrawal 	{"Spk", NULL, "Speaker"},
360b7a742cfSAkshu Agrawal 	{"Dmic Mux", "Front Mic", "DMIC"},
361b7a742cfSAkshu Agrawal 	{"Dmic Mux", "Rear Mic", "DMIC"},
3626b8e4e7dSAkshu Agrawal };
3636b8e4e7dSAkshu Agrawal 
3649c04b5a4SRavulapati Vishnu vardhan rao static const struct snd_kcontrol_new acp3x_5682_mc_controls[] = {
3656b8e4e7dSAkshu Agrawal 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
3666b8e4e7dSAkshu Agrawal 	SOC_DAPM_PIN_SWITCH("Spk"),
3676b8e4e7dSAkshu Agrawal 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
3686b8e4e7dSAkshu Agrawal };
3696b8e4e7dSAkshu Agrawal 
3709c04b5a4SRavulapati Vishnu vardhan rao static struct snd_soc_card acp3x_5682 = {
3716b8e4e7dSAkshu Agrawal 	.name = "acp3xalc5682m98357",
3726b8e4e7dSAkshu Agrawal 	.owner = THIS_MODULE,
373414e3cabSRavulapati Vishnu vardhan rao 	.dai_link = acp3x_dai,
374414e3cabSRavulapati Vishnu vardhan rao 	.num_links = ARRAY_SIZE(acp3x_dai),
3759c04b5a4SRavulapati Vishnu vardhan rao 	.dapm_widgets = acp3x_5682_widgets,
3769c04b5a4SRavulapati Vishnu vardhan rao 	.num_dapm_widgets = ARRAY_SIZE(acp3x_5682_widgets),
3779c04b5a4SRavulapati Vishnu vardhan rao 	.dapm_routes = acp3x_5682_audio_route,
3789c04b5a4SRavulapati Vishnu vardhan rao 	.num_dapm_routes = ARRAY_SIZE(acp3x_5682_audio_route),
3799c04b5a4SRavulapati Vishnu vardhan rao 	.controls = acp3x_5682_mc_controls,
3809c04b5a4SRavulapati Vishnu vardhan rao 	.num_controls = ARRAY_SIZE(acp3x_5682_mc_controls),
3816b8e4e7dSAkshu Agrawal };
3826b8e4e7dSAkshu Agrawal 
383414e3cabSRavulapati Vishnu vardhan rao static const struct snd_soc_dapm_widget acp3x_1015_widgets[] = {
384414e3cabSRavulapati Vishnu vardhan rao 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
385414e3cabSRavulapati Vishnu vardhan rao 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
386414e3cabSRavulapati Vishnu vardhan rao 	SND_SOC_DAPM_MUX("Dmic Mux", SND_SOC_NOPM, 0, 0,
387414e3cabSRavulapati Vishnu vardhan rao 			 &acp3x_dmic_mux_control),
388414e3cabSRavulapati Vishnu vardhan rao 	SND_SOC_DAPM_SPK("Left Spk", NULL),
389414e3cabSRavulapati Vishnu vardhan rao 	SND_SOC_DAPM_SPK("Right Spk", NULL),
390414e3cabSRavulapati Vishnu vardhan rao };
391414e3cabSRavulapati Vishnu vardhan rao 
392414e3cabSRavulapati Vishnu vardhan rao static const struct snd_soc_dapm_route acp3x_1015_route[] = {
393414e3cabSRavulapati Vishnu vardhan rao 	{"Headphone Jack", NULL, "HPOL"},
394414e3cabSRavulapati Vishnu vardhan rao 	{"Headphone Jack", NULL, "HPOR"},
395414e3cabSRavulapati Vishnu vardhan rao 	{"IN1P", NULL, "Headset Mic"},
396414e3cabSRavulapati Vishnu vardhan rao 	{"Dmic Mux", "Front Mic", "DMIC"},
397414e3cabSRavulapati Vishnu vardhan rao 	{"Dmic Mux", "Rear Mic", "DMIC"},
398414e3cabSRavulapati Vishnu vardhan rao 	{"Left Spk", NULL, "Left SPO"},
399414e3cabSRavulapati Vishnu vardhan rao 	{"Right Spk", NULL, "Right SPO"},
400414e3cabSRavulapati Vishnu vardhan rao };
401414e3cabSRavulapati Vishnu vardhan rao 
402414e3cabSRavulapati Vishnu vardhan rao static const struct snd_kcontrol_new acp3x_mc_1015_controls[] = {
403414e3cabSRavulapati Vishnu vardhan rao 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
404414e3cabSRavulapati Vishnu vardhan rao 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
405414e3cabSRavulapati Vishnu vardhan rao 	SOC_DAPM_PIN_SWITCH("Left Spk"),
406414e3cabSRavulapati Vishnu vardhan rao 	SOC_DAPM_PIN_SWITCH("Right Spk"),
407414e3cabSRavulapati Vishnu vardhan rao };
408414e3cabSRavulapati Vishnu vardhan rao 
409414e3cabSRavulapati Vishnu vardhan rao static struct snd_soc_card acp3x_1015 = {
410414e3cabSRavulapati Vishnu vardhan rao 	.name = "acp3xalc56821015",
411414e3cabSRavulapati Vishnu vardhan rao 	.owner = THIS_MODULE,
412414e3cabSRavulapati Vishnu vardhan rao 	.dai_link = acp3x_dai,
413414e3cabSRavulapati Vishnu vardhan rao 	.num_links = ARRAY_SIZE(acp3x_dai),
414414e3cabSRavulapati Vishnu vardhan rao 	.dapm_widgets = acp3x_1015_widgets,
415414e3cabSRavulapati Vishnu vardhan rao 	.num_dapm_widgets = ARRAY_SIZE(acp3x_1015_widgets),
416414e3cabSRavulapati Vishnu vardhan rao 	.dapm_routes = acp3x_1015_route,
417414e3cabSRavulapati Vishnu vardhan rao 	.num_dapm_routes = ARRAY_SIZE(acp3x_1015_route),
418414e3cabSRavulapati Vishnu vardhan rao 	.codec_conf = rt1015_conf,
419414e3cabSRavulapati Vishnu vardhan rao 	.num_configs = ARRAY_SIZE(rt1015_conf),
420414e3cabSRavulapati Vishnu vardhan rao 	.controls = acp3x_mc_1015_controls,
421414e3cabSRavulapati Vishnu vardhan rao 	.num_controls = ARRAY_SIZE(acp3x_mc_1015_controls),
422414e3cabSRavulapati Vishnu vardhan rao };
423414e3cabSRavulapati Vishnu vardhan rao 
42426e33ed9SVijendar Mukunda static const struct snd_soc_dapm_widget acp3x_1015p_widgets[] = {
42526e33ed9SVijendar Mukunda 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
42626e33ed9SVijendar Mukunda 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
42726e33ed9SVijendar Mukunda 	SND_SOC_DAPM_MUX("Dmic Mux", SND_SOC_NOPM, 0, 0,
42826e33ed9SVijendar Mukunda 			 &acp3x_dmic_mux_control),
42926e33ed9SVijendar Mukunda 	SND_SOC_DAPM_SPK("Speakers", NULL),
43026e33ed9SVijendar Mukunda };
43126e33ed9SVijendar Mukunda 
43226e33ed9SVijendar Mukunda static const struct snd_soc_dapm_route acp3x_1015p_route[] = {
43326e33ed9SVijendar Mukunda 	{"Headphone Jack", NULL, "HPOL"},
43426e33ed9SVijendar Mukunda 	{"Headphone Jack", NULL, "HPOR"},
43526e33ed9SVijendar Mukunda 	{"IN1P", NULL, "Headset Mic"},
43626e33ed9SVijendar Mukunda 	{"Dmic Mux", "Front Mic", "DMIC"},
43726e33ed9SVijendar Mukunda 	{"Dmic Mux", "Rear Mic", "DMIC"},
43826e33ed9SVijendar Mukunda 	/* speaker */
43926e33ed9SVijendar Mukunda 	{ "Speakers", NULL, "Speaker" },
44026e33ed9SVijendar Mukunda };
44126e33ed9SVijendar Mukunda 
44226e33ed9SVijendar Mukunda static const struct snd_kcontrol_new acp3x_mc_1015p_controls[] = {
44326e33ed9SVijendar Mukunda 	SOC_DAPM_PIN_SWITCH("Speakers"),
44426e33ed9SVijendar Mukunda 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
44526e33ed9SVijendar Mukunda 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
44626e33ed9SVijendar Mukunda };
44726e33ed9SVijendar Mukunda 
44826e33ed9SVijendar Mukunda static struct snd_soc_card acp3x_1015p = {
44926e33ed9SVijendar Mukunda 	.name = "acp3xalc56821015p",
45026e33ed9SVijendar Mukunda 	.owner = THIS_MODULE,
45126e33ed9SVijendar Mukunda 	.dai_link = acp3x_dai,
45226e33ed9SVijendar Mukunda 	.num_links = ARRAY_SIZE(acp3x_dai),
45326e33ed9SVijendar Mukunda 	.dapm_widgets = acp3x_1015p_widgets,
45426e33ed9SVijendar Mukunda 	.num_dapm_widgets = ARRAY_SIZE(acp3x_1015p_widgets),
45526e33ed9SVijendar Mukunda 	.dapm_routes = acp3x_1015p_route,
45626e33ed9SVijendar Mukunda 	.num_dapm_routes = ARRAY_SIZE(acp3x_1015p_route),
45726e33ed9SVijendar Mukunda 	.controls = acp3x_mc_1015p_controls,
45826e33ed9SVijendar Mukunda 	.num_controls = ARRAY_SIZE(acp3x_mc_1015p_controls),
45926e33ed9SVijendar Mukunda };
46026e33ed9SVijendar Mukunda 
4610fe4b561SRavulapati Vishnu vardhan rao void *soc_is_rltk_max(struct device *dev)
4620fe4b561SRavulapati Vishnu vardhan rao {
4630fe4b561SRavulapati Vishnu vardhan rao 	const struct acpi_device_id *match;
4640fe4b561SRavulapati Vishnu vardhan rao 
4650fe4b561SRavulapati Vishnu vardhan rao 	match = acpi_match_device(dev->driver->acpi_match_table, dev);
4660fe4b561SRavulapati Vishnu vardhan rao 	if (!match)
4670fe4b561SRavulapati Vishnu vardhan rao 		return NULL;
4680fe4b561SRavulapati Vishnu vardhan rao 	return (void *)match->driver_data;
4690fe4b561SRavulapati Vishnu vardhan rao }
4700fe4b561SRavulapati Vishnu vardhan rao 
471f7b2651bSRavulapati Vishnu vardhan rao static void card_spk_dai_link_present(struct snd_soc_dai_link *links,
472f7b2651bSRavulapati Vishnu vardhan rao 						const char *card_name)
473f7b2651bSRavulapati Vishnu vardhan rao {
474f7b2651bSRavulapati Vishnu vardhan rao 	if (!strcmp(card_name, "acp3xalc56821015")) {
475f7b2651bSRavulapati Vishnu vardhan rao 		links[1].codecs = rt1015;
476f7b2651bSRavulapati Vishnu vardhan rao 		links[1].num_codecs = ARRAY_SIZE(rt1015);
47726e33ed9SVijendar Mukunda 	} else if (!strcmp(card_name, "acp3xalc56821015p")) {
47826e33ed9SVijendar Mukunda 		links[1].codecs = rt1015p;
47926e33ed9SVijendar Mukunda 		links[1].num_codecs = ARRAY_SIZE(rt1015p);
480f7b2651bSRavulapati Vishnu vardhan rao 	} else {
481f7b2651bSRavulapati Vishnu vardhan rao 		links[1].codecs = max;
482f7b2651bSRavulapati Vishnu vardhan rao 		links[1].num_codecs = ARRAY_SIZE(max);
483f7b2651bSRavulapati Vishnu vardhan rao 	}
484f7b2651bSRavulapati Vishnu vardhan rao }
485f7b2651bSRavulapati Vishnu vardhan rao 
4866b8e4e7dSAkshu Agrawal static int acp3x_probe(struct platform_device *pdev)
4876b8e4e7dSAkshu Agrawal {
4886b8e4e7dSAkshu Agrawal 	int ret;
4896b8e4e7dSAkshu Agrawal 	struct snd_soc_card *card;
4906b8e4e7dSAkshu Agrawal 	struct acp3x_platform_info *machine;
4910fe4b561SRavulapati Vishnu vardhan rao 	struct device *dev = &pdev->dev;
4920fe4b561SRavulapati Vishnu vardhan rao 
4930fe4b561SRavulapati Vishnu vardhan rao 	card = (struct snd_soc_card *)soc_is_rltk_max(dev);
4940fe4b561SRavulapati Vishnu vardhan rao 	if (!card)
4950fe4b561SRavulapati Vishnu vardhan rao 		return -ENODEV;
4966b8e4e7dSAkshu Agrawal 
4976b8e4e7dSAkshu Agrawal 	machine = devm_kzalloc(&pdev->dev, sizeof(*machine), GFP_KERNEL);
4986b8e4e7dSAkshu Agrawal 	if (!machine)
4996b8e4e7dSAkshu Agrawal 		return -ENOMEM;
5006b8e4e7dSAkshu Agrawal 
501f7b2651bSRavulapati Vishnu vardhan rao 	card_spk_dai_link_present(card->dai_link, card->name);
5020fe4b561SRavulapati Vishnu vardhan rao 	card->dev = &pdev->dev;
5036b8e4e7dSAkshu Agrawal 	platform_set_drvdata(pdev, card);
5046b8e4e7dSAkshu Agrawal 	snd_soc_card_set_drvdata(card, machine);
50572c3b2b0SAkshu Agrawal 
50672c3b2b0SAkshu Agrawal 	dmic_sel = devm_gpiod_get(&pdev->dev, "dmic", GPIOD_OUT_LOW);
50772c3b2b0SAkshu Agrawal 	if (IS_ERR(dmic_sel)) {
508d7729c40SAkshu Agrawal 		dev_err(&pdev->dev, "DMIC gpio failed err=%ld\n",
50972c3b2b0SAkshu Agrawal 			PTR_ERR(dmic_sel));
51072c3b2b0SAkshu Agrawal 		return PTR_ERR(dmic_sel);
51172c3b2b0SAkshu Agrawal 	}
51272c3b2b0SAkshu Agrawal 
5130fe4b561SRavulapati Vishnu vardhan rao 	ret = devm_snd_soc_register_card(&pdev->dev, card);
5146b8e4e7dSAkshu Agrawal 	if (ret) {
515*71869335SMario Limonciello 		return dev_err_probe(&pdev->dev, ret,
516*71869335SMario Limonciello 				"devm_snd_soc_register_card(%s) failed\n",
517*71869335SMario Limonciello 				card->name);
5186b8e4e7dSAkshu Agrawal 	}
519*71869335SMario Limonciello 	return 0;
5206b8e4e7dSAkshu Agrawal }
5216b8e4e7dSAkshu Agrawal 
5226b8e4e7dSAkshu Agrawal static const struct acpi_device_id acp3x_audio_acpi_match[] = {
5230fe4b561SRavulapati Vishnu vardhan rao 	{ "AMDI5682", (unsigned long)&acp3x_5682},
524414e3cabSRavulapati Vishnu vardhan rao 	{ "AMDI1015", (unsigned long)&acp3x_1015},
52526e33ed9SVijendar Mukunda 	{ "10021015", (unsigned long)&acp3x_1015p},
5266b8e4e7dSAkshu Agrawal 	{},
5276b8e4e7dSAkshu Agrawal };
5286b8e4e7dSAkshu Agrawal MODULE_DEVICE_TABLE(acpi, acp3x_audio_acpi_match);
5296b8e4e7dSAkshu Agrawal 
5306b8e4e7dSAkshu Agrawal static struct platform_driver acp3x_audio = {
5316b8e4e7dSAkshu Agrawal 	.driver = {
5326b8e4e7dSAkshu Agrawal 		.name = "acp3x-alc5682-max98357",
5336b8e4e7dSAkshu Agrawal 		.acpi_match_table = ACPI_PTR(acp3x_audio_acpi_match),
5346b8e4e7dSAkshu Agrawal 		.pm = &snd_soc_pm_ops,
5356b8e4e7dSAkshu Agrawal 	},
5366b8e4e7dSAkshu Agrawal 	.probe = acp3x_probe,
5376b8e4e7dSAkshu Agrawal };
5386b8e4e7dSAkshu Agrawal 
5396b8e4e7dSAkshu Agrawal module_platform_driver(acp3x_audio);
5406b8e4e7dSAkshu Agrawal 
5416b8e4e7dSAkshu Agrawal MODULE_AUTHOR("akshu.agrawal@amd.com");
542414e3cabSRavulapati Vishnu vardhan rao MODULE_AUTHOR("Vishnuvardhanrao.Ravulapati@amd.com");
54326e33ed9SVijendar Mukunda MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
54426e33ed9SVijendar Mukunda MODULE_DESCRIPTION("ALC5682 ALC1015, ALC1015P & MAX98357 audio support");
5456b8e4e7dSAkshu Agrawal MODULE_LICENSE("GPL v2");
546