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/acpi.h>
20 
21 #include "raven/acp3x.h"
22 #include "../codecs/rt5682.h"
23 
24 #define PCO_PLAT_CLK 48000000
25 #define RT5682_PLL_FREQ (48000 * 512)
26 #define DUAL_CHANNEL		2
27 
28 static struct snd_soc_jack pco_jack;
29 static struct clk *rt5682_dai_wclk;
30 static struct clk *rt5682_dai_bclk;
31 static struct gpio_desc *dmic_sel;
32 
33 static int acp3x_5682_init(struct snd_soc_pcm_runtime *rtd)
34 {
35 	int ret;
36 	struct snd_soc_card *card = rtd->card;
37 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
38 	struct snd_soc_component *component = codec_dai->component;
39 
40 	dev_info(rtd->dev, "codec dai name = %s\n", codec_dai->name);
41 
42 	/* set rt5682 dai fmt */
43 	ret =  snd_soc_dai_set_fmt(codec_dai, SND_SOC_DAIFMT_I2S
44 			| SND_SOC_DAIFMT_NB_NF
45 			| SND_SOC_DAIFMT_CBM_CFM);
46 	if (ret < 0) {
47 		dev_err(rtd->card->dev,
48 				"Failed to set rt5682 dai fmt: %d\n", ret);
49 		return ret;
50 	}
51 
52 	/* set codec PLL */
53 	ret = snd_soc_dai_set_pll(codec_dai, RT5682_PLL2, RT5682_PLL2_S_MCLK,
54 				  PCO_PLAT_CLK, RT5682_PLL_FREQ);
55 	if (ret < 0) {
56 		dev_err(rtd->dev, "can't set rt5682 PLL: %d\n", ret);
57 		return ret;
58 	}
59 
60 	/* Set codec sysclk */
61 	ret = snd_soc_dai_set_sysclk(codec_dai, RT5682_SCLK_S_PLL2,
62 			RT5682_PLL_FREQ, SND_SOC_CLOCK_IN);
63 	if (ret < 0) {
64 		dev_err(rtd->dev,
65 			"Failed to set rt5682 SYSCLK: %d\n", ret);
66 		return ret;
67 	}
68 
69 	/* Set tdm/i2s1 master bclk ratio */
70 	ret = snd_soc_dai_set_bclk_ratio(codec_dai, 64);
71 	if (ret < 0) {
72 		dev_err(rtd->dev,
73 			"Failed to set rt5682 tdm bclk ratio: %d\n", ret);
74 		return ret;
75 	}
76 
77 	rt5682_dai_wclk = clk_get(component->dev, "rt5682-dai-wclk");
78 	rt5682_dai_bclk = clk_get(component->dev, "rt5682-dai-bclk");
79 
80 	ret = snd_soc_card_jack_new(card, "Headset Jack",
81 				SND_JACK_HEADSET | SND_JACK_LINEOUT |
82 				SND_JACK_BTN_0 | SND_JACK_BTN_1 |
83 				SND_JACK_BTN_2 | SND_JACK_BTN_3,
84 				&pco_jack, NULL, 0);
85 	if (ret) {
86 		dev_err(card->dev, "HP jack creation failed %d\n", ret);
87 		return ret;
88 	}
89 
90 	snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
91 	snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_1, KEY_VOLUMEUP);
92 	snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_2, KEY_VOLUMEDOWN);
93 	snd_jack_set_key(pco_jack.jack, SND_JACK_BTN_3, KEY_VOICECOMMAND);
94 
95 	ret = snd_soc_component_set_jack(component, &pco_jack, NULL);
96 	if (ret) {
97 		dev_err(rtd->dev, "Headset Jack call-back failed: %d\n", ret);
98 		return ret;
99 	}
100 
101 	return ret;
102 }
103 
104 static int rt5682_clk_enable(struct snd_pcm_substream *substream)
105 {
106 	int ret = 0;
107 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
108 
109 	/* RT5682 will support only 48K output with 48M mclk */
110 	clk_set_rate(rt5682_dai_wclk, 48000);
111 	clk_set_rate(rt5682_dai_bclk, 48000 * 64);
112 	ret = clk_prepare_enable(rt5682_dai_wclk);
113 	if (ret < 0) {
114 		dev_err(rtd->dev, "can't enable wclk %d\n", ret);
115 		return ret;
116 	}
117 
118 	return ret;
119 }
120 
121 static void rt5682_clk_disable(void)
122 {
123 	clk_disable_unprepare(rt5682_dai_wclk);
124 }
125 
126 static const unsigned int channels[] = {
127 	DUAL_CHANNEL,
128 };
129 
130 static const unsigned int rates[] = {
131 	48000,
132 };
133 
134 static const struct snd_pcm_hw_constraint_list constraints_rates = {
135 	.count = ARRAY_SIZE(rates),
136 	.list  = rates,
137 	.mask = 0,
138 };
139 
140 static const struct snd_pcm_hw_constraint_list constraints_channels = {
141 	.count = ARRAY_SIZE(channels),
142 	.list = channels,
143 	.mask = 0,
144 };
145 
146 static int acp3x_5682_startup(struct snd_pcm_substream *substream)
147 {
148 	struct snd_pcm_runtime *runtime = substream->runtime;
149 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
150 	struct snd_soc_card *card = rtd->card;
151 	struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card);
152 
153 	machine->play_i2s_instance = I2S_SP_INSTANCE;
154 	machine->cap_i2s_instance = I2S_SP_INSTANCE;
155 
156 	runtime->hw.channels_max = DUAL_CHANNEL;
157 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
158 				   &constraints_channels);
159 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
160 				   &constraints_rates);
161 	return rt5682_clk_enable(substream);
162 }
163 
164 static int acp3x_max_startup(struct snd_pcm_substream *substream)
165 {
166 	struct snd_pcm_runtime *runtime = substream->runtime;
167 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
168 	struct snd_soc_card *card = rtd->card;
169 	struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card);
170 
171 	machine->play_i2s_instance = I2S_BT_INSTANCE;
172 
173 	runtime->hw.channels_max = DUAL_CHANNEL;
174 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
175 				   &constraints_channels);
176 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
177 				   &constraints_rates);
178 	return rt5682_clk_enable(substream);
179 }
180 
181 static int acp3x_ec_dmic0_startup(struct snd_pcm_substream *substream)
182 {
183 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
184 	struct snd_soc_card *card = rtd->card;
185 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
186 	struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card);
187 
188 	machine->cap_i2s_instance = I2S_BT_INSTANCE;
189 	snd_soc_dai_set_bclk_ratio(codec_dai, 64);
190 	if (dmic_sel)
191 		gpiod_set_value(dmic_sel, 0);
192 
193 	return rt5682_clk_enable(substream);
194 }
195 
196 static int acp3x_ec_dmic1_startup(struct snd_pcm_substream *substream)
197 {
198 	struct snd_soc_pcm_runtime *rtd = substream->private_data;
199 	struct snd_soc_card *card = rtd->card;
200 	struct snd_soc_dai *codec_dai = rtd->codec_dai;
201 	struct acp3x_platform_info *machine = snd_soc_card_get_drvdata(card);
202 
203 	machine->cap_i2s_instance = I2S_BT_INSTANCE;
204 	snd_soc_dai_set_bclk_ratio(codec_dai, 64);
205 	if (dmic_sel)
206 		gpiod_set_value(dmic_sel, 1);
207 
208 	return rt5682_clk_enable(substream);
209 }
210 
211 static void rt5682_shutdown(struct snd_pcm_substream *substream)
212 {
213 	rt5682_clk_disable();
214 }
215 
216 static const struct snd_soc_ops acp3x_5682_ops = {
217 	.startup = acp3x_5682_startup,
218 	.shutdown = rt5682_shutdown,
219 };
220 
221 static const struct snd_soc_ops acp3x_max_play_ops = {
222 	.startup = acp3x_max_startup,
223 	.shutdown = rt5682_shutdown,
224 };
225 
226 static const struct snd_soc_ops acp3x_ec_cap0_ops = {
227 	.startup = acp3x_ec_dmic0_startup,
228 	.shutdown = rt5682_shutdown,
229 };
230 
231 static const struct snd_soc_ops acp3x_ec_cap1_ops = {
232 	.startup = acp3x_ec_dmic1_startup,
233 	.shutdown = rt5682_shutdown,
234 };
235 
236 SND_SOC_DAILINK_DEF(acp3x_i2s,
237 	DAILINK_COMP_ARRAY(COMP_CPU("acp3x_i2s_playcap.0")));
238 SND_SOC_DAILINK_DEF(acp3x_bt,
239 	DAILINK_COMP_ARRAY(COMP_CPU("acp3x_i2s_playcap.2")));
240 
241 SND_SOC_DAILINK_DEF(rt5682,
242 	DAILINK_COMP_ARRAY(COMP_CODEC("i2c-10EC5682:00", "rt5682-aif1")));
243 SND_SOC_DAILINK_DEF(max,
244 	DAILINK_COMP_ARRAY(COMP_CODEC("MX98357A:00", "HiFi")));
245 SND_SOC_DAILINK_DEF(cros_ec,
246 	DAILINK_COMP_ARRAY(COMP_CODEC("GOOG0013:00", "EC Codec I2S RX")));
247 
248 SND_SOC_DAILINK_DEF(platform,
249 	DAILINK_COMP_ARRAY(COMP_PLATFORM("acp3x_rv_i2s_dma.0")));
250 
251 static struct snd_soc_dai_link acp3x_dai_5682_98357[] = {
252 	{
253 		.name = "acp3x-5682-play",
254 		.stream_name = "Playback",
255 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
256 				| SND_SOC_DAIFMT_CBM_CFM,
257 		.init = acp3x_5682_init,
258 		.dpcm_playback = 1,
259 		.dpcm_capture = 1,
260 		.ops = &acp3x_5682_ops,
261 		SND_SOC_DAILINK_REG(acp3x_i2s, rt5682, platform),
262 	},
263 	{
264 		.name = "acp3x-max98357-play",
265 		.stream_name = "HiFi Playback",
266 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
267 				| SND_SOC_DAIFMT_CBM_CFM,
268 		.dpcm_playback = 1,
269 		.ops = &acp3x_max_play_ops,
270 		SND_SOC_DAILINK_REG(acp3x_bt, max, platform),
271 	},
272 	{
273 		.name = "acp3x-ec-dmic0-capture",
274 		.stream_name = "Capture DMIC0",
275 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
276 				| SND_SOC_DAIFMT_CBS_CFS,
277 		.dpcm_capture = 1,
278 		.ops = &acp3x_ec_cap0_ops,
279 		SND_SOC_DAILINK_REG(acp3x_bt, cros_ec, platform),
280 	},
281 	{
282 		.name = "acp3x-ec-dmic1-capture",
283 		.stream_name = "Capture DMIC1",
284 		.dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
285 				| SND_SOC_DAIFMT_CBS_CFS,
286 		.dpcm_capture = 1,
287 		.ops = &acp3x_ec_cap1_ops,
288 		SND_SOC_DAILINK_REG(acp3x_bt, cros_ec, platform),
289 	},
290 };
291 
292 static const struct snd_soc_dapm_widget acp3x_widgets[] = {
293 	SND_SOC_DAPM_HP("Headphone Jack", NULL),
294 	SND_SOC_DAPM_SPK("Spk", NULL),
295 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
296 };
297 
298 static const struct snd_soc_dapm_route acp3x_audio_route[] = {
299 	{"Headphone Jack", NULL, "HPOL"},
300 	{"Headphone Jack", NULL, "HPOR"},
301 	{"IN1P", NULL, "Headset Mic"},
302 	{"Spk", NULL, "Speaker"},
303 };
304 
305 static const struct snd_kcontrol_new acp3x_mc_controls[] = {
306 	SOC_DAPM_PIN_SWITCH("Headphone Jack"),
307 	SOC_DAPM_PIN_SWITCH("Spk"),
308 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
309 };
310 
311 static struct snd_soc_card acp3x_card = {
312 	.name = "acp3xalc5682m98357",
313 	.owner = THIS_MODULE,
314 	.dai_link = acp3x_dai_5682_98357,
315 	.num_links = ARRAY_SIZE(acp3x_dai_5682_98357),
316 	.dapm_widgets = acp3x_widgets,
317 	.num_dapm_widgets = ARRAY_SIZE(acp3x_widgets),
318 	.dapm_routes = acp3x_audio_route,
319 	.num_dapm_routes = ARRAY_SIZE(acp3x_audio_route),
320 	.controls = acp3x_mc_controls,
321 	.num_controls = ARRAY_SIZE(acp3x_mc_controls),
322 };
323 
324 static int acp3x_probe(struct platform_device *pdev)
325 {
326 	int ret;
327 	struct snd_soc_card *card;
328 	struct acp3x_platform_info *machine;
329 
330 	machine = devm_kzalloc(&pdev->dev, sizeof(*machine), GFP_KERNEL);
331 	if (!machine)
332 		return -ENOMEM;
333 
334 	card = &acp3x_card;
335 	acp3x_card.dev = &pdev->dev;
336 	platform_set_drvdata(pdev, card);
337 	snd_soc_card_set_drvdata(card, machine);
338 
339 	dmic_sel = devm_gpiod_get(&pdev->dev, "dmic", GPIOD_OUT_LOW);
340 	if (IS_ERR(dmic_sel)) {
341 		dev_err(&pdev->dev, "DMIC gpio failed err=%ld\n",
342 			PTR_ERR(dmic_sel));
343 		return PTR_ERR(dmic_sel);
344 	}
345 
346 	ret = devm_snd_soc_register_card(&pdev->dev, &acp3x_card);
347 	if (ret) {
348 		dev_err(&pdev->dev,
349 				"devm_snd_soc_register_card(%s) failed: %d\n",
350 				acp3x_card.name, ret);
351 		return ret;
352 	}
353 	return 0;
354 }
355 
356 static const struct acpi_device_id acp3x_audio_acpi_match[] = {
357 	{ "AMDI5682", 0 },
358 	{},
359 };
360 MODULE_DEVICE_TABLE(acpi, acp3x_audio_acpi_match);
361 
362 static struct platform_driver acp3x_audio = {
363 	.driver = {
364 		.name = "acp3x-alc5682-max98357",
365 		.acpi_match_table = ACPI_PTR(acp3x_audio_acpi_match),
366 		.pm = &snd_soc_pm_ops,
367 	},
368 	.probe = acp3x_probe,
369 };
370 
371 module_platform_driver(acp3x_audio);
372 
373 MODULE_AUTHOR("akshu.agrawal@amd.com");
374 MODULE_DESCRIPTION("ALC5682 & MAX98357 audio support");
375 MODULE_LICENSE("GPL v2");
376