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