xref: /openbmc/linux/sound/soc/amd/vangogh/acp5x-mach.c (revision a719afe6)
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 bclk, rate = params_rate(params);
157 	struct snd_soc_component *comp;
158 	int ret, i;
159 
160 	switch (rate) {
161 	case 48000:
162 		bclk = 1536000;
163 		break;
164 	default:
165 		bclk = 0;
166 		break;
167 	}
168 
169 	for_each_rtd_components(rtd, i, comp) {
170 		if (!(strcmp(comp->name, "spi-VLV1776:00")) ||
171 		    !(strcmp(comp->name, "spi-VLV1776:01"))) {
172 			if (!bclk) {
173 				dev_err(comp->dev, "Invalid sample rate: 0x%x\n", rate);
174 				return -EINVAL;
175 			}
176 
177 			ret = snd_soc_component_set_sysclk(comp, 0, 0, bclk, SND_SOC_CLOCK_IN);
178 			if (ret) {
179 				dev_err(comp->dev, "failed to set SYSCLK: %d\n", ret);
180 				return ret;
181 			}
182 		}
183 	}
184 
185 	return 0;
186 
187 }
188 
189 static const struct snd_soc_ops acp5x_8821_ops = {
190 	.startup = acp5x_8821_startup,
191 	.hw_params = acp5x_nau8821_hw_params,
192 };
193 
194 static const struct snd_soc_ops acp5x_cs35l41_play_ops = {
195 	.startup = acp5x_cs35l41_startup,
196 	.hw_params = acp5x_cs35l41_hw_params,
197 };
198 
199 static struct snd_soc_codec_conf cs35l41_conf[] = {
200 	{
201 		.dlc = COMP_CODEC_CONF("spi-VLV1776:00"),
202 		.name_prefix = "Left",
203 	},
204 	{
205 		.dlc = COMP_CODEC_CONF("spi-VLV1776:01"),
206 		.name_prefix = "Right",
207 	},
208 };
209 
210 SND_SOC_DAILINK_DEF(platform,  DAILINK_COMP_ARRAY(COMP_PLATFORM("acp5x_i2s_dma.0")));
211 SND_SOC_DAILINK_DEF(acp5x_i2s, DAILINK_COMP_ARRAY(COMP_CPU("acp5x_i2s_playcap.0")));
212 SND_SOC_DAILINK_DEF(acp5x_bt,  DAILINK_COMP_ARRAY(COMP_CPU("acp5x_i2s_playcap.1")));
213 SND_SOC_DAILINK_DEF(nau8821,   DAILINK_COMP_ARRAY(COMP_CODEC("i2c-NVTN2020:00", "nau8821-hifi")));
214 SND_SOC_DAILINK_DEF(cs35l41,   DAILINK_COMP_ARRAY(COMP_CODEC("spi-VLV1776:00", "cs35l41-pcm"),
215 						  COMP_CODEC("spi-VLV1776:01", "cs35l41-pcm")));
216 
217 static struct snd_soc_dai_link acp5x_dai[] = {
218 	{
219 		.name = "acp5x-8821-play",
220 		.stream_name = "Playback/Capture",
221 		.dai_fmt = SND_SOC_DAIFMT_I2S |
222 			   SND_SOC_DAIFMT_NB_NF |
223 			   SND_SOC_DAIFMT_CBC_CFC,
224 		.dpcm_playback = 1,
225 		.dpcm_capture = 1,
226 		.ops = &acp5x_8821_ops,
227 		.init = acp5x_8821_init,
228 		SND_SOC_DAILINK_REG(acp5x_i2s, nau8821, platform),
229 	},
230 	{
231 		.name = "acp5x-CS35L41-Stereo",
232 		.stream_name = "CS35L41 Stereo Playback",
233 		.dai_fmt = SND_SOC_DAIFMT_I2S |
234 			   SND_SOC_DAIFMT_NB_NF |
235 			   SND_SOC_DAIFMT_CBC_CFC,
236 		.dpcm_playback = 1,
237 		.playback_only = 1,
238 		.ops = &acp5x_cs35l41_play_ops,
239 		SND_SOC_DAILINK_REG(acp5x_bt, cs35l41, platform),
240 	},
241 };
242 
243 static int platform_clock_control(struct snd_soc_dapm_widget *w,
244 				  struct snd_kcontrol *k, int event)
245 {
246 	struct snd_soc_dapm_context *dapm = w->dapm;
247 	struct snd_soc_card *card = dapm->card;
248 	struct snd_soc_dai *dai;
249 	int ret = 0;
250 
251 	dai = snd_soc_card_get_codec_dai(card, ACP5X_NUVOTON_CODEC_DAI);
252 	if (!dai) {
253 		dev_err(card->dev, "Codec dai not found\n");
254 		return -EIO;
255 	}
256 
257 	if (SND_SOC_DAPM_EVENT_OFF(event)) {
258 		ret = snd_soc_dai_set_sysclk(dai, NAU8821_CLK_INTERNAL, 0, SND_SOC_CLOCK_IN);
259 		if (ret < 0) {
260 			dev_err(card->dev, "set sysclk err = %d\n", ret);
261 			return -EIO;
262 		}
263 	} else {
264 		ret = snd_soc_dai_set_sysclk(dai, NAU8821_CLK_FLL_BLK, 0, SND_SOC_CLOCK_IN);
265 		if (ret < 0)
266 			dev_err(dai->dev, "can't set BLK clock %d\n", ret);
267 		ret = snd_soc_dai_set_pll(dai, 0, 0, ACP5X_NUVOTON_BCLK, ACP5X_NAU8821_FREQ_OUT);
268 		if (ret < 0)
269 			dev_err(dai->dev, "can't set FLL: %d\n", ret);
270 	}
271 
272 	return ret;
273 }
274 
275 static const struct snd_kcontrol_new acp5x_8821_controls[] = {
276 	SOC_DAPM_PIN_SWITCH("Headphone"),
277 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
278 	SOC_DAPM_PIN_SWITCH("Int Mic"),
279 };
280 
281 static const struct snd_soc_dapm_widget acp5x_8821_widgets[] = {
282 	SND_SOC_DAPM_HP("Headphone", NULL),
283 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
284 	SND_SOC_DAPM_MIC("Int Mic", NULL),
285 	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
286 			    platform_clock_control,
287 			    SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
288 };
289 
290 static const struct snd_soc_dapm_route acp5x_8821_audio_route[] = {
291 	/* HP jack connectors - unknown if we have jack detection */
292 	{ "Headphone", NULL, "HPOL" },
293 	{ "Headphone", NULL, "HPOR" },
294 	{ "MICL", NULL, "Headset Mic" },
295 	{ "MICR", NULL, "Headset Mic" },
296 	{ "DMIC", NULL, "Int Mic" },
297 
298 	{ "Headphone", NULL, "Platform Clock" },
299 	{ "Headset Mic", NULL, "Platform Clock" },
300 	{ "Int Mic", NULL, "Platform Clock" },
301 };
302 
303 static struct snd_soc_card acp5x_card = {
304 	.name = "acp5x",
305 	.owner = THIS_MODULE,
306 	.dai_link = acp5x_dai,
307 	.num_links = ARRAY_SIZE(acp5x_dai),
308 	.dapm_widgets = acp5x_8821_widgets,
309 	.num_dapm_widgets = ARRAY_SIZE(acp5x_8821_widgets),
310 	.dapm_routes = acp5x_8821_audio_route,
311 	.num_dapm_routes = ARRAY_SIZE(acp5x_8821_audio_route),
312 	.codec_conf = cs35l41_conf,
313 	.num_configs = ARRAY_SIZE(cs35l41_conf),
314 	.controls = acp5x_8821_controls,
315 	.num_controls = ARRAY_SIZE(acp5x_8821_controls),
316 };
317 
318 static int acp5x_vg_quirk_cb(const struct dmi_system_id *id)
319 {
320 	acp5x_machine_id = VG_JUPITER;
321 
322 	return 1;
323 }
324 
325 static const struct dmi_system_id acp5x_vg_quirk_table[] = {
326 	{
327 		.callback = acp5x_vg_quirk_cb,
328 		.matches = {
329 			DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Valve"),
330 			DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Jupiter"),
331 		}
332 	},
333 	{}
334 };
335 
336 static int acp5x_probe(struct platform_device *pdev)
337 {
338 	struct acp5x_platform_info *machine;
339 	struct device *dev = &pdev->dev;
340 	struct snd_soc_card *card;
341 	int ret;
342 
343 	machine = devm_kzalloc(dev, sizeof(*machine), GFP_KERNEL);
344 	if (!machine)
345 		return -ENOMEM;
346 
347 	dmi_check_system(acp5x_vg_quirk_table);
348 	switch (acp5x_machine_id) {
349 	case VG_JUPITER:
350 		card = &acp5x_card;
351 		break;
352 	default:
353 		return -ENODEV;
354 	}
355 	card->dev = dev;
356 	platform_set_drvdata(pdev, card);
357 	snd_soc_card_set_drvdata(card, machine);
358 
359 	ret = devm_snd_soc_register_card(dev, card);
360 	if (ret)
361 		return dev_err_probe(dev, ret, "Register card (%s) failed\n", card->name);
362 
363 	return 0;
364 }
365 
366 static struct platform_driver acp5x_mach_driver = {
367 	.driver = {
368 		.name = "acp5x_mach",
369 		.pm = &snd_soc_pm_ops,
370 	},
371 	.probe = acp5x_probe,
372 };
373 
374 module_platform_driver(acp5x_mach_driver);
375 
376 MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
377 MODULE_DESCRIPTION("NAU8821 & CS35L41 audio support");
378 MODULE_LICENSE("GPL v2");
379 MODULE_ALIAS("platform:" DRV_NAME);
380