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