xref: /linux/sound/soc/amd/vangogh/acp5x-mach.c (revision 88f5870dd360e6604dceff808bf960bf6ddf0377)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Machine driver for AMD Vangogh platform using NAU8821 & CS35L41
4  * codecs.
5  *
6  * Copyright 2021 Advanced Micro Devices, Inc.
7  */
8 
9 #include <linux/acpi.h>
10 #include <linux/dmi.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/i2c.h>
13 #include <linux/input-event-codes.h>
14 #include <linux/module.h>
15 #include <sound/jack.h>
16 #include <sound/pcm_params.h>
17 #include <sound/soc.h>
18 #include <sound/soc-dapm.h>
19 
20 #include "../../codecs/nau8821.h"
21 #include "acp5x.h"
22 
23 #define DRV_NAME			"acp5x_mach"
24 #define DUAL_CHANNEL			2
25 #define ACP5X_NUVOTON_CODEC_DAI		"nau8821-hifi"
26 #define VG_JUPITER			1
27 #define ACP5X_NUVOTON_BCLK		3072000
28 #define ACP5X_NAU8821_FREQ_OUT		12288000
29 
30 static unsigned long acp5x_machine_id;
31 static struct snd_soc_jack vg_headset;
32 
33 static struct snd_soc_jack_pin acp5x_nau8821_jack_pins[] = {
34 	{
35 		.pin	= "Headphone",
36 		.mask	= SND_JACK_HEADPHONE,
37 	},
38 	{
39 		.pin	= "Headset Mic",
40 		.mask	= SND_JACK_MICROPHONE,
41 	},
42 };
43 
44 static int acp5x_8821_init(struct snd_soc_pcm_runtime *rtd)
45 {
46 	struct snd_soc_component *component = asoc_rtd_to_codec(rtd, 0)->component;
47 	int ret;
48 
49 	/*
50 	 * Headset buttons map to the google Reference headset.
51 	 * These can be configured by userspace.
52 	 */
53 	ret = snd_soc_card_jack_new_pins(rtd->card, "Headset Jack",
54 					 SND_JACK_HEADSET | SND_JACK_BTN_0,
55 					 &vg_headset, acp5x_nau8821_jack_pins,
56 					 ARRAY_SIZE(acp5x_nau8821_jack_pins));
57 	if (ret) {
58 		dev_err(rtd->dev, "Headset Jack creation failed %d\n", ret);
59 		return ret;
60 	}
61 
62 	snd_jack_set_key(vg_headset.jack, SND_JACK_BTN_0, KEY_MEDIA);
63 	nau8821_enable_jack_detect(component, &vg_headset);
64 
65 	return ret;
66 }
67 
68 static const unsigned int rates[] = {
69 	48000,
70 };
71 
72 static const struct snd_pcm_hw_constraint_list constraints_rates = {
73 	.count = ARRAY_SIZE(rates),
74 	.list  = rates,
75 	.mask = 0,
76 };
77 
78 static const unsigned int channels[] = {
79 	2,
80 };
81 
82 static const struct snd_pcm_hw_constraint_list constraints_channels = {
83 	.count = ARRAY_SIZE(channels),
84 	.list = channels,
85 	.mask = 0,
86 };
87 
88 static const unsigned int acp5x_nau8821_format[] = {32};
89 
90 static struct snd_pcm_hw_constraint_list constraints_sample_bits = {
91 	.list = acp5x_nau8821_format,
92 	.count = ARRAY_SIZE(acp5x_nau8821_format),
93 };
94 
95 static int acp5x_8821_startup(struct snd_pcm_substream *substream)
96 {
97 	struct snd_pcm_runtime *runtime = substream->runtime;
98 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
99 	struct acp5x_platform_info *machine = snd_soc_card_get_drvdata(rtd->card);
100 
101 	machine->play_i2s_instance = I2S_SP_INSTANCE;
102 	machine->cap_i2s_instance = I2S_SP_INSTANCE;
103 
104 	runtime->hw.channels_max = DUAL_CHANNEL;
105 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
106 				   &constraints_channels);
107 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
108 				   &constraints_rates);
109 	snd_pcm_hw_constraint_list(substream->runtime, 0,
110 				   SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
111 				   &constraints_sample_bits);
112 
113 	return 0;
114 }
115 
116 static int acp5x_nau8821_hw_params(struct snd_pcm_substream *substream,
117 				   struct snd_pcm_hw_params *params)
118 {
119 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
120 	struct snd_soc_card *card = rtd->card;
121 	struct snd_soc_dai *dai = snd_soc_card_get_codec_dai(card, ACP5X_NUVOTON_CODEC_DAI);
122 	int ret;
123 
124 	ret = snd_soc_dai_set_sysclk(dai, NAU8821_CLK_FLL_BLK, 0, SND_SOC_CLOCK_IN);
125 	if (ret < 0)
126 		dev_err(card->dev, "can't set FS clock %d\n", ret);
127 	ret = snd_soc_dai_set_pll(dai, 0, 0, snd_soc_params_to_bclk(params),
128 				  params_rate(params) * 256);
129 	if (ret < 0)
130 		dev_err(card->dev, "can't set FLL: %d\n", ret);
131 
132 	return ret;
133 }
134 
135 static int acp5x_cs35l41_startup(struct snd_pcm_substream *substream)
136 {
137 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
138 	struct acp5x_platform_info *machine = snd_soc_card_get_drvdata(rtd->card);
139 	struct snd_pcm_runtime *runtime = substream->runtime;
140 
141 	machine->play_i2s_instance = I2S_HS_INSTANCE;
142 
143 	runtime->hw.channels_max = DUAL_CHANNEL;
144 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
145 				   &constraints_channels);
146 	snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
147 				   &constraints_rates);
148 
149 	return 0;
150 }
151 
152 static int acp5x_cs35l41_hw_params(struct snd_pcm_substream *substream,
153 				   struct snd_pcm_hw_params *params)
154 {
155 	struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
156 	unsigned int num_codecs = rtd->dai_link->num_codecs;
157 	struct snd_soc_card *card = rtd->card;
158 	struct snd_soc_dai *dai;
159 	unsigned int bclk_val;
160 	int ret, i;
161 
162 	ret = 0;
163 	for (i = 0; i < num_codecs; i++) {
164 		dai = asoc_rtd_to_codec(rtd, i);
165 		if (strcmp(dai->name, "cs35l41-pcm") == 0) {
166 			switch (params_rate(params)) {
167 			case 48000:
168 				bclk_val = 1536000;
169 				break;
170 			default:
171 				dev_err(card->dev, "Invalid Samplerate:0x%x\n",
172 					params_rate(params));
173 				return -EINVAL;
174 			}
175 			ret = snd_soc_component_set_sysclk(dai->component, 0, 0,
176 							   bclk_val, SND_SOC_CLOCK_IN);
177 			if (ret < 0) {
178 				dev_err(card->dev, "failed to set sysclk for CS35l41 dai\n");
179 				return ret;
180 			}
181 		}
182 	}
183 
184 	return ret;
185 }
186 
187 static const struct snd_soc_ops acp5x_8821_ops = {
188 	.startup = acp5x_8821_startup,
189 	.hw_params = acp5x_nau8821_hw_params,
190 };
191 
192 static const struct snd_soc_ops acp5x_cs35l41_play_ops = {
193 	.startup = acp5x_cs35l41_startup,
194 	.hw_params = acp5x_cs35l41_hw_params,
195 };
196 
197 static struct snd_soc_codec_conf cs35l41_conf[] = {
198 	{
199 		.dlc = COMP_CODEC_CONF("spi-VLV1776:00"),
200 		.name_prefix = "Left",
201 	},
202 	{
203 		.dlc = COMP_CODEC_CONF("spi-VLV1776:01"),
204 		.name_prefix = "Right",
205 	},
206 };
207 
208 SND_SOC_DAILINK_DEF(platform,  DAILINK_COMP_ARRAY(COMP_PLATFORM("acp5x_i2s_dma.0")));
209 SND_SOC_DAILINK_DEF(acp5x_i2s, DAILINK_COMP_ARRAY(COMP_CPU("acp5x_i2s_playcap.0")));
210 SND_SOC_DAILINK_DEF(acp5x_bt,  DAILINK_COMP_ARRAY(COMP_CPU("acp5x_i2s_playcap.1")));
211 SND_SOC_DAILINK_DEF(nau8821,   DAILINK_COMP_ARRAY(COMP_CODEC("i2c-NVTN2020:00", "nau8821-hifi")));
212 SND_SOC_DAILINK_DEF(cs35l41,   DAILINK_COMP_ARRAY(COMP_CODEC("spi-VLV1776:00", "cs35l41-pcm"),
213 						  COMP_CODEC("spi-VLV1776:01", "cs35l41-pcm")));
214 
215 static struct snd_soc_dai_link acp5x_dai[] = {
216 	{
217 		.name = "acp5x-8821-play",
218 		.stream_name = "Playback/Capture",
219 		.dai_fmt = SND_SOC_DAIFMT_I2S |
220 			   SND_SOC_DAIFMT_NB_NF |
221 			   SND_SOC_DAIFMT_CBC_CFC,
222 		.dpcm_playback = 1,
223 		.dpcm_capture = 1,
224 		.ops = &acp5x_8821_ops,
225 		.init = acp5x_8821_init,
226 		SND_SOC_DAILINK_REG(acp5x_i2s, nau8821, platform),
227 	},
228 	{
229 		.name = "acp5x-CS35L41-Stereo",
230 		.stream_name = "CS35L41 Stereo Playback",
231 		.dai_fmt = SND_SOC_DAIFMT_I2S |
232 			   SND_SOC_DAIFMT_NB_NF |
233 			   SND_SOC_DAIFMT_CBC_CFC,
234 		.dpcm_playback = 1,
235 		.playback_only = 1,
236 		.ops = &acp5x_cs35l41_play_ops,
237 		SND_SOC_DAILINK_REG(acp5x_bt, cs35l41, platform),
238 	},
239 };
240 
241 static int platform_clock_control(struct snd_soc_dapm_widget *w,
242 				  struct snd_kcontrol *k, int event)
243 {
244 	struct snd_soc_dapm_context *dapm = w->dapm;
245 	struct snd_soc_card *card = dapm->card;
246 	struct snd_soc_dai *dai;
247 	int ret = 0;
248 
249 	dai = snd_soc_card_get_codec_dai(card, ACP5X_NUVOTON_CODEC_DAI);
250 	if (!dai) {
251 		dev_err(card->dev, "Codec dai not found\n");
252 		return -EIO;
253 	}
254 
255 	if (SND_SOC_DAPM_EVENT_OFF(event)) {
256 		ret = snd_soc_dai_set_sysclk(dai, NAU8821_CLK_INTERNAL, 0, SND_SOC_CLOCK_IN);
257 		if (ret < 0) {
258 			dev_err(card->dev, "set sysclk err = %d\n", ret);
259 			return -EIO;
260 		}
261 	} else {
262 		ret = snd_soc_dai_set_sysclk(dai, NAU8821_CLK_FLL_BLK, 0, SND_SOC_CLOCK_IN);
263 		if (ret < 0)
264 			dev_err(dai->dev, "can't set BLK clock %d\n", ret);
265 		ret = snd_soc_dai_set_pll(dai, 0, 0, ACP5X_NUVOTON_BCLK, ACP5X_NAU8821_FREQ_OUT);
266 		if (ret < 0)
267 			dev_err(dai->dev, "can't set FLL: %d\n", ret);
268 	}
269 
270 	return ret;
271 }
272 
273 static const struct snd_kcontrol_new acp5x_8821_controls[] = {
274 	SOC_DAPM_PIN_SWITCH("Headphone"),
275 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
276 	SOC_DAPM_PIN_SWITCH("Int Mic"),
277 };
278 
279 static const struct snd_soc_dapm_widget acp5x_8821_widgets[] = {
280 	SND_SOC_DAPM_HP("Headphone", NULL),
281 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
282 	SND_SOC_DAPM_MIC("Int Mic", NULL),
283 	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
284 			    platform_clock_control,
285 			    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
286 };
287 
288 static const struct snd_soc_dapm_route acp5x_8821_audio_route[] = {
289 	/* HP jack connectors - unknown if we have jack detection */
290 	{ "Headphone", NULL, "HPOL" },
291 	{ "Headphone", NULL, "HPOR" },
292 	{ "MICL", NULL, "Headset Mic" },
293 	{ "MICR", NULL, "Headset Mic" },
294 	{ "DMIC", NULL, "Int Mic" },
295 
296 	{ "Headphone", NULL, "Platform Clock" },
297 	{ "Headset Mic", NULL, "Platform Clock" },
298 	{ "Int Mic", NULL, "Platform Clock" },
299 };
300 
301 static struct snd_soc_card acp5x_card = {
302 	.name = "acp5x",
303 	.owner = THIS_MODULE,
304 	.dai_link = acp5x_dai,
305 	.num_links = ARRAY_SIZE(acp5x_dai),
306 	.dapm_widgets = acp5x_8821_widgets,
307 	.num_dapm_widgets = ARRAY_SIZE(acp5x_8821_widgets),
308 	.dapm_routes = acp5x_8821_audio_route,
309 	.num_dapm_routes = ARRAY_SIZE(acp5x_8821_audio_route),
310 	.codec_conf = cs35l41_conf,
311 	.num_configs = ARRAY_SIZE(cs35l41_conf),
312 	.controls = acp5x_8821_controls,
313 	.num_controls = ARRAY_SIZE(acp5x_8821_controls),
314 };
315 
316 static int acp5x_vg_quirk_cb(const struct dmi_system_id *id)
317 {
318 	acp5x_machine_id = VG_JUPITER;
319 
320 	return 1;
321 }
322 
323 static const struct dmi_system_id acp5x_vg_quirk_table[] = {
324 	{
325 		.callback = acp5x_vg_quirk_cb,
326 		.matches = {
327 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Valve"),
328 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Jupiter"),
329 		}
330 	},
331 	{}
332 };
333 
334 static int acp5x_probe(struct platform_device *pdev)
335 {
336 	struct acp5x_platform_info *machine;
337 	struct device *dev = &pdev->dev;
338 	struct snd_soc_card *card;
339 	int ret;
340 
341 	machine = devm_kzalloc(dev, sizeof(*machine), GFP_KERNEL);
342 	if (!machine)
343 		return -ENOMEM;
344 
345 	dmi_check_system(acp5x_vg_quirk_table);
346 	switch (acp5x_machine_id) {
347 	case VG_JUPITER:
348 		card = &acp5x_card;
349 		break;
350 	default:
351 		return -ENODEV;
352 	}
353 	card->dev = dev;
354 	platform_set_drvdata(pdev, card);
355 	snd_soc_card_set_drvdata(card, machine);
356 
357 	ret = devm_snd_soc_register_card(dev, card);
358 	if (ret)
359 		return dev_err_probe(dev, ret, "Register card (%s) failed\n", card->name);
360 
361 	return 0;
362 }
363 
364 static struct platform_driver acp5x_mach_driver = {
365 	.driver = {
366 		.name = "acp5x_mach",
367 		.pm = &snd_soc_pm_ops,
368 	},
369 	.probe = acp5x_probe,
370 };
371 
372 module_platform_driver(acp5x_mach_driver);
373 
374 MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
375 MODULE_DESCRIPTION("NAU8821 & CS35L41 audio support");
376 MODULE_LICENSE("GPL v2");
377 MODULE_ALIAS("platform:" DRV_NAME);
378