xref: /linux/sound/soc/amd/vangogh/acp5x-mach.c (revision 6f989800639a7a29ab9a02e165b04dc144dd4f2b)
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 	ret = 0;
169 	for (i = 0; i < num_codecs; i++) {
170 		codec_dai = asoc_rtd_to_codec(rtd, i);
171 		if ((strcmp(codec_dai->name, "spi-VLV1776:00") == 0) ||
172 		    (strcmp(codec_dai->name, "spi-VLV1776:01") == 0)) {
173 			switch (params_rate(params)) {
174 			case 48000:
175 				bclk_val = 1536000;
176 				break;
177 			default:
178 				dev_err(card->dev, "Invalid Samplerate:0x%x\n",
179 					params_rate(params));
180 				return -EINVAL;
181 			}
182 			ret = snd_soc_component_set_sysclk(codec_dai->component,
183 							   0, 0, bclk_val, SND_SOC_CLOCK_IN);
184 			if (ret < 0) {
185 				dev_err(card->dev, "failed to set sysclk for CS35l41 dai\n");
186 				return ret;
187 			}
188 		}
189 	}
190 
191 	return ret;
192 }
193 
194 static const struct snd_soc_ops acp5x_8821_ops = {
195 	.startup = acp5x_8821_startup,
196 	.hw_params = acp5x_nau8821_hw_params,
197 };
198 
199 static const struct snd_soc_ops acp5x_cs35l41_play_ops = {
200 	.startup = acp5x_cs35l41_startup,
201 	.hw_params = acp5x_cs35l41_hw_params,
202 };
203 
204 static struct snd_soc_codec_conf cs35l41_conf[] = {
205 	{
206 		.dlc = COMP_CODEC_CONF("spi-VLV1776:00"),
207 		.name_prefix = "Left",
208 	},
209 	{
210 		.dlc = COMP_CODEC_CONF("spi-VLV1776:01"),
211 		.name_prefix = "Right",
212 	},
213 };
214 
215 SND_SOC_DAILINK_DEF(acp5x_i2s,
216 		    DAILINK_COMP_ARRAY(COMP_CPU("acp5x_i2s_playcap.0")));
217 
218 SND_SOC_DAILINK_DEF(acp5x_bt,
219 		    DAILINK_COMP_ARRAY(COMP_CPU("acp5x_i2s_playcap.1")));
220 
221 SND_SOC_DAILINK_DEF(nau8821,
222 		    DAILINK_COMP_ARRAY(COMP_CODEC("i2c-NVTN2020:00",
223 						  "nau8821-hifi")));
224 
225 SND_SOC_DAILINK_DEF(cs35l41,
226 		    DAILINK_COMP_ARRAY(COMP_CODEC("spi-VLV1776:00", "cs35l41-pcm"),
227 				       COMP_CODEC("spi-VLV1776:01", "cs35l41-pcm")));
228 
229 SND_SOC_DAILINK_DEF(platform,
230 		    DAILINK_COMP_ARRAY(COMP_PLATFORM("acp5x_i2s_dma.0")));
231 
232 static struct snd_soc_dai_link acp5x_dai[] = {
233 	{
234 		.name = "acp5x-8821-play",
235 		.stream_name = "Playback/Capture",
236 		.dai_fmt = SND_SOC_DAIFMT_I2S  | SND_SOC_DAIFMT_NB_NF |
237 			   SND_SOC_DAIFMT_CBC_CFC,
238 		.dpcm_playback = 1,
239 		.dpcm_capture = 1,
240 		.ops = &acp5x_8821_ops,
241 		.init = acp5x_8821_init,
242 		SND_SOC_DAILINK_REG(acp5x_i2s, nau8821, platform),
243 	},
244 	{
245 		.name = "acp5x-CS35L41-Stereo",
246 		.stream_name = "CS35L41 Stereo Playback",
247 		.dai_fmt = SND_SOC_DAIFMT_I2S  | SND_SOC_DAIFMT_NB_NF |
248 			   SND_SOC_DAIFMT_CBC_CFC,
249 		.dpcm_playback = 1,
250 		.playback_only = 1,
251 		.ops = &acp5x_cs35l41_play_ops,
252 		.init = acp5x_cs35l41_init,
253 		SND_SOC_DAILINK_REG(acp5x_bt, cs35l41, platform),
254 	},
255 };
256 
257 static int platform_clock_control(struct snd_soc_dapm_widget *w,
258 				  struct snd_kcontrol *k, int  event)
259 {
260 	struct snd_soc_dapm_context *dapm = w->dapm;
261 	struct snd_soc_card *card = dapm->card;
262 	struct snd_soc_dai *codec_dai;
263 	int ret = 0;
264 
265 	codec_dai = snd_soc_card_get_codec_dai(card, ACP5X_NUVOTON_CODEC_DAI);
266 	if (!codec_dai) {
267 		dev_err(card->dev, "Codec dai not found\n");
268 		return -EIO;
269 	}
270 
271 	if (SND_SOC_DAPM_EVENT_OFF(event)) {
272 		ret = snd_soc_dai_set_sysclk(codec_dai, NAU8821_CLK_INTERNAL,
273 					     0, SND_SOC_CLOCK_IN);
274 		if (ret < 0) {
275 			dev_err(card->dev, "set sysclk err = %d\n", ret);
276 			return -EIO;
277 		}
278 	}
279 	return ret;
280 }
281 
282 static const struct snd_kcontrol_new acp5x_8821_controls[] = {
283 	SOC_DAPM_PIN_SWITCH("Headphone"),
284 	SOC_DAPM_PIN_SWITCH("Headset Mic"),
285 	SOC_DAPM_PIN_SWITCH("Int Mic"),
286 };
287 
288 static const struct snd_soc_dapm_widget acp5x_8821_widgets[] = {
289 	SND_SOC_DAPM_HP("Headphone", NULL),
290 	SND_SOC_DAPM_MIC("Headset Mic", NULL),
291 	SND_SOC_DAPM_MIC("Int Mic", NULL),
292 	SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
293 			    platform_clock_control, SND_SOC_DAPM_POST_PMD),
294 };
295 
296 static const struct snd_soc_dapm_route acp5x_8821_audio_route[] = {
297 	/* HP jack connectors - unknown if we have jack detection */
298 	{ "Headphone", NULL, "HPOL" },
299 	{ "Headphone", NULL, "HPOR" },
300 	{ "MICL", NULL, "Headset Mic" },
301 	{ "MICR", NULL, "Headset Mic" },
302 	{ "DMIC", NULL, "Int Mic" },
303 
304 	{ "Headphone", NULL, "Platform Clock" },
305 	{ "Headset Mic", NULL, "Platform Clock" },
306 	{ "Int Mic", NULL, "Platform Clock" },
307 };
308 
309 static struct snd_soc_card acp5x_card = {
310 	.name = "acp5x",
311 	.owner = THIS_MODULE,
312 	.dai_link = acp5x_dai,
313 	.num_links = ARRAY_SIZE(acp5x_dai),
314 	.dapm_widgets = acp5x_8821_widgets,
315 	.num_dapm_widgets = ARRAY_SIZE(acp5x_8821_widgets),
316 	.dapm_routes = acp5x_8821_audio_route,
317 	.num_dapm_routes = ARRAY_SIZE(acp5x_8821_audio_route),
318 	.codec_conf = cs35l41_conf,
319 	.num_configs = ARRAY_SIZE(cs35l41_conf),
320 	.controls = acp5x_8821_controls,
321 	.num_controls = ARRAY_SIZE(acp5x_8821_controls),
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