Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Machine driver for AMD Vangogh platform using NAU8821 & CS35L41
0004  * codecs.
0005  *
0006  * Copyright 2021 Advanced Micro Devices, Inc.
0007  */
0008 
0009 #include <sound/soc.h>
0010 #include <sound/soc-dapm.h>
0011 #include <linux/module.h>
0012 #include <linux/io.h>
0013 #include <sound/pcm.h>
0014 #include <sound/pcm_params.h>
0015 
0016 #include <sound/jack.h>
0017 #include <linux/clk.h>
0018 #include <linux/gpio.h>
0019 #include <linux/gpio/consumer.h>
0020 #include <linux/i2c.h>
0021 #include <linux/input.h>
0022 #include <linux/acpi.h>
0023 #include <linux/dmi.h>
0024 
0025 #include "../../codecs/nau8821.h"
0026 #include "../../codecs/cs35l41.h"
0027 
0028 #include "acp5x.h"
0029 
0030 #define DRV_NAME "acp5x_mach"
0031 #define DUAL_CHANNEL        2
0032 #define ACP5X_NUVOTON_CODEC_DAI "nau8821-hifi"
0033 #define VG_JUPITER 1
0034 #define ACP5X_NUVOTON_BCLK 3072000
0035 #define ACP5X_NAU8821_FREQ_OUT 12288000
0036 
0037 static unsigned long acp5x_machine_id;
0038 static struct snd_soc_jack vg_headset;
0039 
0040 static struct snd_soc_jack_pin acp5x_nau8821_jack_pins[] = {
0041     {
0042         .pin    = "Headphone",
0043         .mask   = SND_JACK_HEADPHONE,
0044     },
0045     {
0046         .pin    = "Headset Mic",
0047         .mask   = SND_JACK_MICROPHONE,
0048     },
0049 };
0050 
0051 static int acp5x_8821_init(struct snd_soc_pcm_runtime *rtd)
0052 {
0053     int ret;
0054     struct snd_soc_card *card = rtd->card;
0055     struct snd_soc_component *component =
0056                     asoc_rtd_to_codec(rtd, 0)->component;
0057 
0058     /*
0059      * Headset buttons map to the google Reference headset.
0060      * These can be configured by userspace.
0061      */
0062     ret = snd_soc_card_jack_new_pins(card, "Headset Jack",
0063                      SND_JACK_HEADSET | SND_JACK_BTN_0,
0064                      &vg_headset, acp5x_nau8821_jack_pins,
0065                      ARRAY_SIZE(acp5x_nau8821_jack_pins));
0066     if (ret) {
0067         dev_err(rtd->dev, "Headset Jack creation failed %d\n", ret);
0068         return ret;
0069     }
0070 
0071     snd_jack_set_key(vg_headset.jack, SND_JACK_BTN_0, KEY_MEDIA);
0072     nau8821_enable_jack_detect(component, &vg_headset);
0073     return ret;
0074 }
0075 
0076 static int acp5x_cs35l41_init(struct snd_soc_pcm_runtime *rtd)
0077 {
0078     return 0;
0079 }
0080 
0081 static const unsigned int rates[] = {
0082     48000,
0083 };
0084 
0085 static const struct snd_pcm_hw_constraint_list constraints_rates = {
0086     .count = ARRAY_SIZE(rates),
0087     .list  = rates,
0088     .mask = 0,
0089 };
0090 
0091 static const unsigned int channels[] = {
0092     2,
0093 };
0094 
0095 static const struct snd_pcm_hw_constraint_list constraints_channels = {
0096     .count = ARRAY_SIZE(channels),
0097     .list = channels,
0098     .mask = 0,
0099 };
0100 
0101 static const unsigned int acp5x_nau8821_format[] = {32};
0102 
0103 static struct snd_pcm_hw_constraint_list constraints_sample_bits = {
0104     .list = acp5x_nau8821_format,
0105     .count = ARRAY_SIZE(acp5x_nau8821_format),
0106 };
0107 
0108 static int acp5x_8821_startup(struct snd_pcm_substream *substream)
0109 {
0110     struct snd_pcm_runtime *runtime = substream->runtime;
0111     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0112     struct snd_soc_card *card = rtd->card;
0113     struct acp5x_platform_info *machine = snd_soc_card_get_drvdata(card);
0114 
0115     machine->play_i2s_instance = I2S_SP_INSTANCE;
0116     machine->cap_i2s_instance = I2S_SP_INSTANCE;
0117 
0118     runtime->hw.channels_max = DUAL_CHANNEL;
0119     snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
0120                    &constraints_channels);
0121     snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
0122                    &constraints_rates);
0123     snd_pcm_hw_constraint_list(substream->runtime, 0,
0124                    SNDRV_PCM_HW_PARAM_SAMPLE_BITS,
0125                    &constraints_sample_bits);
0126     return 0;
0127 }
0128 
0129 static int acp5x_nau8821_hw_params(struct snd_pcm_substream *substream,
0130                    struct snd_pcm_hw_params *params)
0131 {
0132     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0133     struct snd_soc_card *card = rtd->card;
0134     struct snd_soc_dai *codec_dai =
0135             snd_soc_card_get_codec_dai(card,
0136                            ACP5X_NUVOTON_CODEC_DAI);
0137     int ret;
0138 
0139     ret = snd_soc_dai_set_sysclk(codec_dai, NAU8821_CLK_FLL_BLK, 0,
0140                      SND_SOC_CLOCK_IN);
0141     if (ret < 0)
0142         dev_err(card->dev, "can't set FS clock %d\n", ret);
0143     ret = snd_soc_dai_set_pll(codec_dai, 0, 0, snd_soc_params_to_bclk(params),
0144                   params_rate(params) * 256);
0145     if (ret < 0)
0146         dev_err(card->dev, "can't set FLL: %d\n", ret);
0147 
0148     return ret;
0149 }
0150 
0151 static int acp5x_cs35l41_startup(struct snd_pcm_substream *substream)
0152 {
0153     struct snd_pcm_runtime *runtime = substream->runtime;
0154     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0155     struct snd_soc_card *card = rtd->card;
0156     struct acp5x_platform_info *machine = snd_soc_card_get_drvdata(card);
0157 
0158     machine->play_i2s_instance = I2S_HS_INSTANCE;
0159 
0160     runtime->hw.channels_max = DUAL_CHANNEL;
0161     snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_CHANNELS,
0162                    &constraints_channels);
0163     snd_pcm_hw_constraint_list(runtime, 0, SNDRV_PCM_HW_PARAM_RATE,
0164                    &constraints_rates);
0165     return 0;
0166 }
0167 
0168 static int acp5x_cs35l41_hw_params(struct snd_pcm_substream *substream,
0169                    struct snd_pcm_hw_params *params)
0170 {
0171     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0172     struct snd_soc_card *card = rtd->card;
0173     struct snd_soc_dai *codec_dai;
0174     int ret, i;
0175     unsigned int num_codecs = rtd->num_codecs;
0176     unsigned int bclk_val;
0177 
0178     ret = 0;
0179     for (i = 0; i < num_codecs; i++) {
0180         codec_dai = asoc_rtd_to_codec(rtd, i);
0181         if (strcmp(codec_dai->name, "cs35l41-pcm") == 0) {
0182             switch (params_rate(params)) {
0183             case 48000:
0184                 bclk_val = 1536000;
0185                 break;
0186             default:
0187                 dev_err(card->dev, "Invalid Samplerate:0x%x\n",
0188                     params_rate(params));
0189                 return -EINVAL;
0190             }
0191             ret = snd_soc_component_set_sysclk(codec_dai->component,
0192                                0, 0, bclk_val, SND_SOC_CLOCK_IN);
0193             if (ret < 0) {
0194                 dev_err(card->dev, "failed to set sysclk for CS35l41 dai\n");
0195                 return ret;
0196             }
0197         }
0198     }
0199 
0200     return ret;
0201 }
0202 
0203 static const struct snd_soc_ops acp5x_8821_ops = {
0204     .startup = acp5x_8821_startup,
0205     .hw_params = acp5x_nau8821_hw_params,
0206 };
0207 
0208 static const struct snd_soc_ops acp5x_cs35l41_play_ops = {
0209     .startup = acp5x_cs35l41_startup,
0210     .hw_params = acp5x_cs35l41_hw_params,
0211 };
0212 
0213 static struct snd_soc_codec_conf cs35l41_conf[] = {
0214     {
0215         .dlc = COMP_CODEC_CONF("spi-VLV1776:00"),
0216         .name_prefix = "Left",
0217     },
0218     {
0219         .dlc = COMP_CODEC_CONF("spi-VLV1776:01"),
0220         .name_prefix = "Right",
0221     },
0222 };
0223 
0224 SND_SOC_DAILINK_DEF(acp5x_i2s,
0225             DAILINK_COMP_ARRAY(COMP_CPU("acp5x_i2s_playcap.0")));
0226 
0227 SND_SOC_DAILINK_DEF(acp5x_bt,
0228             DAILINK_COMP_ARRAY(COMP_CPU("acp5x_i2s_playcap.1")));
0229 
0230 SND_SOC_DAILINK_DEF(nau8821,
0231             DAILINK_COMP_ARRAY(COMP_CODEC("i2c-NVTN2020:00",
0232                           "nau8821-hifi")));
0233 
0234 SND_SOC_DAILINK_DEF(cs35l41,
0235             DAILINK_COMP_ARRAY(COMP_CODEC("spi-VLV1776:00", "cs35l41-pcm"),
0236                        COMP_CODEC("spi-VLV1776:01", "cs35l41-pcm")));
0237 
0238 SND_SOC_DAILINK_DEF(platform,
0239             DAILINK_COMP_ARRAY(COMP_PLATFORM("acp5x_i2s_dma.0")));
0240 
0241 static struct snd_soc_dai_link acp5x_dai[] = {
0242     {
0243         .name = "acp5x-8821-play",
0244         .stream_name = "Playback/Capture",
0245         .dai_fmt = SND_SOC_DAIFMT_I2S  | SND_SOC_DAIFMT_NB_NF |
0246                SND_SOC_DAIFMT_CBC_CFC,
0247         .dpcm_playback = 1,
0248         .dpcm_capture = 1,
0249         .ops = &acp5x_8821_ops,
0250         .init = acp5x_8821_init,
0251         SND_SOC_DAILINK_REG(acp5x_i2s, nau8821, platform),
0252     },
0253     {
0254         .name = "acp5x-CS35L41-Stereo",
0255         .stream_name = "CS35L41 Stereo Playback",
0256         .dai_fmt = SND_SOC_DAIFMT_I2S  | SND_SOC_DAIFMT_NB_NF |
0257                SND_SOC_DAIFMT_CBC_CFC,
0258         .dpcm_playback = 1,
0259         .playback_only = 1,
0260         .ops = &acp5x_cs35l41_play_ops,
0261         .init = acp5x_cs35l41_init,
0262         SND_SOC_DAILINK_REG(acp5x_bt, cs35l41, platform),
0263     },
0264 };
0265 
0266 static int platform_clock_control(struct snd_soc_dapm_widget *w,
0267                   struct snd_kcontrol *k, int  event)
0268 {
0269     struct snd_soc_dapm_context *dapm = w->dapm;
0270     struct snd_soc_card *card = dapm->card;
0271     struct snd_soc_dai *codec_dai;
0272     int ret = 0;
0273 
0274     codec_dai = snd_soc_card_get_codec_dai(card, ACP5X_NUVOTON_CODEC_DAI);
0275     if (!codec_dai) {
0276         dev_err(card->dev, "Codec dai not found\n");
0277         return -EIO;
0278     }
0279 
0280     if (SND_SOC_DAPM_EVENT_OFF(event)) {
0281         ret = snd_soc_dai_set_sysclk(codec_dai, NAU8821_CLK_INTERNAL,
0282                          0, SND_SOC_CLOCK_IN);
0283         if (ret < 0) {
0284             dev_err(card->dev, "set sysclk err = %d\n", ret);
0285             return -EIO;
0286         }
0287     } else {
0288         ret = snd_soc_dai_set_sysclk(codec_dai, NAU8821_CLK_FLL_BLK, 0,
0289                          SND_SOC_CLOCK_IN);
0290         if (ret < 0)
0291             dev_err(codec_dai->dev, "can't set BLK clock %d\n", ret);
0292         ret = snd_soc_dai_set_pll(codec_dai, 0, 0, ACP5X_NUVOTON_BCLK,
0293                       ACP5X_NAU8821_FREQ_OUT);
0294         if (ret < 0)
0295             dev_err(codec_dai->dev, "can't set FLL: %d\n", ret);
0296     }
0297     return ret;
0298 }
0299 
0300 static const struct snd_kcontrol_new acp5x_8821_controls[] = {
0301     SOC_DAPM_PIN_SWITCH("Headphone"),
0302     SOC_DAPM_PIN_SWITCH("Headset Mic"),
0303     SOC_DAPM_PIN_SWITCH("Int Mic"),
0304 };
0305 
0306 static const struct snd_soc_dapm_widget acp5x_8821_widgets[] = {
0307     SND_SOC_DAPM_HP("Headphone", NULL),
0308     SND_SOC_DAPM_MIC("Headset Mic", NULL),
0309     SND_SOC_DAPM_MIC("Int Mic", NULL),
0310     SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
0311                 platform_clock_control, SND_SOC_DAPM_PRE_PMU | SND_SOC_DAPM_POST_PMD),
0312 };
0313 
0314 static const struct snd_soc_dapm_route acp5x_8821_audio_route[] = {
0315     /* HP jack connectors - unknown if we have jack detection */
0316     { "Headphone", NULL, "HPOL" },
0317     { "Headphone", NULL, "HPOR" },
0318     { "MICL", NULL, "Headset Mic" },
0319     { "MICR", NULL, "Headset Mic" },
0320     { "DMIC", NULL, "Int Mic" },
0321 
0322     { "Headphone", NULL, "Platform Clock" },
0323     { "Headset Mic", NULL, "Platform Clock" },
0324     { "Int Mic", NULL, "Platform Clock" },
0325 };
0326 
0327 static struct snd_soc_card acp5x_card = {
0328     .name = "acp5x",
0329     .owner = THIS_MODULE,
0330     .dai_link = acp5x_dai,
0331     .num_links = ARRAY_SIZE(acp5x_dai),
0332     .dapm_widgets = acp5x_8821_widgets,
0333     .num_dapm_widgets = ARRAY_SIZE(acp5x_8821_widgets),
0334     .dapm_routes = acp5x_8821_audio_route,
0335     .num_dapm_routes = ARRAY_SIZE(acp5x_8821_audio_route),
0336     .codec_conf = cs35l41_conf,
0337     .num_configs = ARRAY_SIZE(cs35l41_conf),
0338     .controls = acp5x_8821_controls,
0339     .num_controls = ARRAY_SIZE(acp5x_8821_controls),
0340 };
0341 
0342 static int acp5x_vg_quirk_cb(const struct dmi_system_id *id)
0343 {
0344     acp5x_machine_id = VG_JUPITER;
0345     return 1;
0346 }
0347 
0348 static const struct dmi_system_id acp5x_vg_quirk_table[] = {
0349     {
0350         .callback = acp5x_vg_quirk_cb,
0351         .matches = {
0352             DMI_EXACT_MATCH(DMI_BOARD_VENDOR, "Valve"),
0353             DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "Jupiter"),
0354         }
0355     },
0356     {}
0357 };
0358 
0359 static int acp5x_probe(struct platform_device *pdev)
0360 {
0361     int ret;
0362     struct acp5x_platform_info *machine;
0363     struct snd_soc_card *card;
0364 
0365     machine = devm_kzalloc(&pdev->dev, sizeof(struct acp5x_platform_info),
0366                    GFP_KERNEL);
0367     if (!machine)
0368         return -ENOMEM;
0369 
0370     dmi_check_system(acp5x_vg_quirk_table);
0371     switch (acp5x_machine_id) {
0372     case VG_JUPITER:
0373         card = &acp5x_card;
0374         acp5x_card.dev = &pdev->dev;
0375         break;
0376     default:
0377         return -ENODEV;
0378     }
0379     platform_set_drvdata(pdev, card);
0380     snd_soc_card_set_drvdata(card, machine);
0381 
0382     ret = devm_snd_soc_register_card(&pdev->dev, card);
0383     if (ret) {
0384         return dev_err_probe(&pdev->dev, ret,
0385                      "snd_soc_register_card(%s) failed\n",
0386                      acp5x_card.name);
0387     }
0388     return 0;
0389 }
0390 
0391 static struct platform_driver acp5x_mach_driver = {
0392     .driver = {
0393         .name = "acp5x_mach",
0394         .pm = &snd_soc_pm_ops,
0395     },
0396     .probe = acp5x_probe,
0397 };
0398 
0399 module_platform_driver(acp5x_mach_driver);
0400 
0401 MODULE_AUTHOR("Vijendar.Mukunda@amd.com");
0402 MODULE_DESCRIPTION("NAU8821 & CS35L41 audio support");
0403 MODULE_LICENSE("GPL v2");
0404 MODULE_ALIAS("platform:" DRV_NAME);