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