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