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