Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  bytcr_wm5102.c - ASoc Machine driver for Intel Baytrail platforms with a
0004  *                   Wolfson Microelectronics WM5102 codec
0005  *
0006  *  Copyright (C) 2020 Hans de Goede <hdegoede@redhat.com>
0007  *  Loosely based on bytcr_rt5640.c which is:
0008  *  Copyright (C) 2014-2020 Intel Corp
0009  *  Author: Subhransu S. Prusty <subhransu.s.prusty@intel.com>
0010  */
0011 
0012 #include <linux/acpi.h>
0013 #include <linux/clk.h>
0014 #include <linux/device.h>
0015 #include <linux/init.h>
0016 #include <linux/module.h>
0017 #include <linux/moduleparam.h>
0018 #include <linux/platform_device.h>
0019 #include <linux/slab.h>
0020 #include <linux/spi/spi.h>
0021 #include <sound/jack.h>
0022 #include <sound/pcm.h>
0023 #include <sound/pcm_params.h>
0024 #include <sound/soc.h>
0025 #include <sound/soc-acpi.h>
0026 #include "../../codecs/wm5102.h"
0027 #include "../atom/sst-atom-controls.h"
0028 
0029 #define MCLK_FREQ       25000000
0030 
0031 #define WM5102_MAX_SYSCLK_4K    49152000 /* max sysclk for 4K family */
0032 #define WM5102_MAX_SYSCLK_11025 45158400 /* max sysclk for 11.025K family */
0033 
0034 struct byt_wm5102_private {
0035     struct snd_soc_jack jack;
0036     struct clk *mclk;
0037     struct gpio_desc *spkvdd_en_gpio;
0038 };
0039 
0040 static int byt_wm5102_spkvdd_power_event(struct snd_soc_dapm_widget *w,
0041     struct snd_kcontrol *kcontrol, int event)
0042 {
0043     struct snd_soc_card *card = w->dapm->card;
0044     struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card);
0045 
0046     gpiod_set_value_cansleep(priv->spkvdd_en_gpio,
0047                  !!SND_SOC_DAPM_EVENT_ON(event));
0048 
0049     return 0;
0050 }
0051 
0052 static int byt_wm5102_prepare_and_enable_pll1(struct snd_soc_dai *codec_dai, int rate)
0053 {
0054     struct snd_soc_component *codec_component = codec_dai->component;
0055     int sr_mult = ((rate % 4000) == 0) ?
0056         (WM5102_MAX_SYSCLK_4K / rate) :
0057         (WM5102_MAX_SYSCLK_11025 / rate);
0058     int ret;
0059 
0060     /* Reset FLL1 */
0061     snd_soc_dai_set_pll(codec_dai, WM5102_FLL1_REFCLK, ARIZONA_FLL_SRC_NONE, 0, 0);
0062     snd_soc_dai_set_pll(codec_dai, WM5102_FLL1, ARIZONA_FLL_SRC_NONE, 0, 0);
0063 
0064     /* Configure the FLL1 PLL before selecting it */
0065     ret = snd_soc_dai_set_pll(codec_dai, WM5102_FLL1, ARIZONA_CLK_SRC_MCLK1,
0066                   MCLK_FREQ, rate * sr_mult);
0067     if (ret) {
0068         dev_err(codec_component->dev, "Error setting PLL: %d\n", ret);
0069         return ret;
0070     }
0071 
0072     ret = snd_soc_component_set_sysclk(codec_component, ARIZONA_CLK_SYSCLK,
0073                        ARIZONA_CLK_SRC_FLL1, rate * sr_mult,
0074                        SND_SOC_CLOCK_IN);
0075     if (ret) {
0076         dev_err(codec_component->dev, "Error setting SYSCLK: %d\n", ret);
0077         return ret;
0078     }
0079 
0080     ret = snd_soc_dai_set_sysclk(codec_dai, ARIZONA_CLK_SYSCLK,
0081                      rate * 512, SND_SOC_CLOCK_IN);
0082     if (ret) {
0083         dev_err(codec_component->dev, "Error setting clock: %d\n", ret);
0084         return ret;
0085     }
0086 
0087     return 0;
0088 }
0089 
0090 static int platform_clock_control(struct snd_soc_dapm_widget *w,
0091                   struct snd_kcontrol *k, int event)
0092 {
0093     struct snd_soc_dapm_context *dapm = w->dapm;
0094     struct snd_soc_card *card = dapm->card;
0095     struct snd_soc_dai *codec_dai;
0096     struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card);
0097     int ret;
0098 
0099     codec_dai = snd_soc_card_get_codec_dai(card, "wm5102-aif1");
0100     if (!codec_dai) {
0101         dev_err(card->dev, "Error codec DAI not found\n");
0102         return -EIO;
0103     }
0104 
0105     if (SND_SOC_DAPM_EVENT_ON(event)) {
0106         ret = clk_prepare_enable(priv->mclk);
0107         if (ret) {
0108             dev_err(card->dev, "Error enabling MCLK: %d\n", ret);
0109             return ret;
0110         }
0111         ret = byt_wm5102_prepare_and_enable_pll1(codec_dai, 48000);
0112         if (ret) {
0113             dev_err(card->dev, "Error setting codec sysclk: %d\n", ret);
0114             return ret;
0115         }
0116     } else {
0117         /*
0118          * The WM5102 has a separate 32KHz clock for jack-detect
0119          * so we can disable the PLL, followed by disabling the
0120          * platform clock which is the source-clock for the PLL.
0121          */
0122         snd_soc_dai_set_pll(codec_dai, WM5102_FLL1, ARIZONA_FLL_SRC_NONE, 0, 0);
0123         clk_disable_unprepare(priv->mclk);
0124     }
0125 
0126     return 0;
0127 }
0128 
0129 static const struct snd_soc_dapm_widget byt_wm5102_widgets[] = {
0130     SND_SOC_DAPM_HP("Headphone", NULL),
0131     SND_SOC_DAPM_MIC("Headset Mic", NULL),
0132     SND_SOC_DAPM_MIC("Internal Mic", NULL),
0133     SND_SOC_DAPM_SPK("Speaker", NULL),
0134     SND_SOC_DAPM_SUPPLY("Platform Clock", SND_SOC_NOPM, 0, 0,
0135                 platform_clock_control, SND_SOC_DAPM_PRE_PMU |
0136                 SND_SOC_DAPM_POST_PMD),
0137     SND_SOC_DAPM_SUPPLY("Speaker VDD", SND_SOC_NOPM, 0, 0,
0138                 byt_wm5102_spkvdd_power_event,
0139                 SND_SOC_DAPM_PRE_PMD | SND_SOC_DAPM_POST_PMU),
0140 };
0141 
0142 static const struct snd_soc_dapm_route byt_wm5102_audio_map[] = {
0143     {"Headphone", NULL, "Platform Clock"},
0144     {"Headset Mic", NULL, "Platform Clock"},
0145     {"Internal Mic", NULL, "Platform Clock"},
0146     {"Speaker", NULL, "Platform Clock"},
0147 
0148     {"Speaker", NULL, "SPKOUTLP"},
0149     {"Speaker", NULL, "SPKOUTLN"},
0150     {"Speaker", NULL, "SPKOUTRP"},
0151     {"Speaker", NULL, "SPKOUTRN"},
0152     {"Speaker", NULL, "Speaker VDD"},
0153 
0154     {"Headphone", NULL, "HPOUT1L"},
0155     {"Headphone", NULL, "HPOUT1R"},
0156 
0157     {"Internal Mic", NULL, "MICBIAS3"},
0158     {"IN3L", NULL, "Internal Mic"},
0159 
0160     /*
0161      * The Headset Mix uses MICBIAS1 or 2 depending on if a CTIA/OMTP Headset
0162      * is connected, as the MICBIAS is applied after the CTIA/OMTP cross-switch.
0163      */
0164     {"Headset Mic", NULL, "MICBIAS1"},
0165     {"Headset Mic", NULL, "MICBIAS2"},
0166     {"IN1L", NULL, "Headset Mic"},
0167 
0168     {"AIF1 Playback", NULL, "ssp0 Tx"},
0169     {"ssp0 Tx", NULL, "modem_out"},
0170 
0171     {"modem_in", NULL, "ssp0 Rx"},
0172     {"ssp0 Rx", NULL, "AIF1 Capture"},
0173 };
0174 
0175 static const struct snd_kcontrol_new byt_wm5102_controls[] = {
0176     SOC_DAPM_PIN_SWITCH("Headphone"),
0177     SOC_DAPM_PIN_SWITCH("Headset Mic"),
0178     SOC_DAPM_PIN_SWITCH("Internal Mic"),
0179     SOC_DAPM_PIN_SWITCH("Speaker"),
0180 };
0181 
0182 static struct snd_soc_jack_pin byt_wm5102_pins[] = {
0183     {
0184         .pin    = "Headphone",
0185         .mask   = SND_JACK_HEADPHONE,
0186     },
0187     {
0188         .pin    = "Headset Mic",
0189         .mask   = SND_JACK_MICROPHONE,
0190     },
0191 };
0192 
0193 static int byt_wm5102_init(struct snd_soc_pcm_runtime *runtime)
0194 {
0195     struct snd_soc_card *card = runtime->card;
0196     struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card);
0197     struct snd_soc_component *component = asoc_rtd_to_codec(runtime, 0)->component;
0198     int ret, jack_type;
0199 
0200     card->dapm.idle_bias_off = true;
0201 
0202     ret = snd_soc_add_card_controls(card, byt_wm5102_controls,
0203                     ARRAY_SIZE(byt_wm5102_controls));
0204     if (ret) {
0205         dev_err(card->dev, "Error adding card controls: %d\n", ret);
0206         return ret;
0207     }
0208 
0209     /*
0210      * The firmware might enable the clock at boot (this information
0211      * may or may not be reflected in the enable clock register).
0212      * To change the rate we must disable the clock first to cover these
0213      * cases. Due to common clock framework restrictions that do not allow
0214      * to disable a clock that has not been enabled, we need to enable
0215      * the clock first.
0216      */
0217     ret = clk_prepare_enable(priv->mclk);
0218     if (!ret)
0219         clk_disable_unprepare(priv->mclk);
0220 
0221     ret = clk_set_rate(priv->mclk, MCLK_FREQ);
0222     if (ret) {
0223         dev_err(card->dev, "Error setting MCLK rate: %d\n", ret);
0224         return ret;
0225     }
0226 
0227     jack_type = ARIZONA_JACK_MASK | SND_JACK_BTN_0 | SND_JACK_BTN_1 |
0228             SND_JACK_BTN_2 | SND_JACK_BTN_3;
0229     ret = snd_soc_card_jack_new_pins(card, "Headset", jack_type,
0230                      &priv->jack, byt_wm5102_pins,
0231                      ARRAY_SIZE(byt_wm5102_pins));
0232     if (ret) {
0233         dev_err(card->dev, "Error creating jack: %d\n", ret);
0234         return ret;
0235     }
0236 
0237     snd_soc_component_set_jack(component, &priv->jack, NULL);
0238 
0239     return 0;
0240 }
0241 
0242 static int byt_wm5102_codec_fixup(struct snd_soc_pcm_runtime *rtd,
0243                   struct snd_pcm_hw_params *params)
0244 {
0245     struct snd_interval *rate = hw_param_interval(params,
0246                               SNDRV_PCM_HW_PARAM_RATE);
0247     struct snd_interval *channels = hw_param_interval(params,
0248                               SNDRV_PCM_HW_PARAM_CHANNELS);
0249     int ret;
0250 
0251     /* The DSP will covert the FE rate to 48k, stereo */
0252     rate->min = 48000;
0253     rate->max = 48000;
0254     channels->min = 2;
0255     channels->max = 2;
0256 
0257     /* set SSP0 to 16-bit */
0258     params_set_format(params, SNDRV_PCM_FORMAT_S16_LE);
0259 
0260     /*
0261      * Default mode for SSP configuration is TDM 4 slot, override config
0262      * with explicit setting to I2S 2ch 16-bit. The word length is set with
0263      * dai_set_tdm_slot() since there is no other API exposed
0264      */
0265     ret = snd_soc_dai_set_fmt(asoc_rtd_to_cpu(rtd, 0),
0266                   SND_SOC_DAIFMT_I2S     |
0267                   SND_SOC_DAIFMT_NB_NF   |
0268                   SND_SOC_DAIFMT_BP_FP);
0269     if (ret) {
0270         dev_err(rtd->dev, "Error setting format to I2S: %d\n", ret);
0271         return ret;
0272     }
0273 
0274     ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, 16);
0275     if (ret) {
0276         dev_err(rtd->dev, "Error setting I2S config: %d\n", ret);
0277         return ret;
0278     }
0279 
0280     return 0;
0281 }
0282 
0283 static int byt_wm5102_aif1_startup(struct snd_pcm_substream *substream)
0284 {
0285     return snd_pcm_hw_constraint_single(substream->runtime,
0286                         SNDRV_PCM_HW_PARAM_RATE, 48000);
0287 }
0288 
0289 static const struct snd_soc_ops byt_wm5102_aif1_ops = {
0290     .startup = byt_wm5102_aif1_startup,
0291 };
0292 
0293 SND_SOC_DAILINK_DEF(dummy,
0294     DAILINK_COMP_ARRAY(COMP_DUMMY()));
0295 
0296 SND_SOC_DAILINK_DEF(media,
0297     DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
0298 
0299 SND_SOC_DAILINK_DEF(deepbuffer,
0300     DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
0301 
0302 SND_SOC_DAILINK_DEF(ssp0_port,
0303     DAILINK_COMP_ARRAY(COMP_CPU("ssp0-port")));
0304 
0305 SND_SOC_DAILINK_DEF(ssp0_codec,
0306     DAILINK_COMP_ARRAY(COMP_CODEC(
0307     /*
0308      * Note there is no need to overwrite the codec-name as is done in
0309      * other bytcr machine drivers, because the codec is a MFD child-dev.
0310      */
0311     "wm5102-codec",
0312     "wm5102-aif1")));
0313 
0314 SND_SOC_DAILINK_DEF(platform,
0315     DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
0316 
0317 static struct snd_soc_dai_link byt_wm5102_dais[] = {
0318     [MERR_DPCM_AUDIO] = {
0319         .name = "Baytrail Audio Port",
0320         .stream_name = "Baytrail Audio",
0321         .nonatomic = true,
0322         .dynamic = 1,
0323         .dpcm_playback = 1,
0324         .dpcm_capture = 1,
0325         .ops = &byt_wm5102_aif1_ops,
0326         SND_SOC_DAILINK_REG(media, dummy, platform),
0327 
0328     },
0329     [MERR_DPCM_DEEP_BUFFER] = {
0330         .name = "Deep-Buffer Audio Port",
0331         .stream_name = "Deep-Buffer Audio",
0332         .nonatomic = true,
0333         .dynamic = 1,
0334         .dpcm_playback = 1,
0335         .ops = &byt_wm5102_aif1_ops,
0336         SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
0337     },
0338         /* back ends */
0339     {
0340         /*
0341          * This must be named SSP2-Codec even though this machine driver
0342          * always uses SSP0. Most machine drivers support both and dynamically
0343          * update the dailink to point to SSP0 or SSP2, while keeping the name
0344          * as "SSP2-Codec". The SOF tplg files hardcode the "SSP2-Codec" even
0345          * in the byt-foo-ssp0.tplg versions because the other machine-drivers
0346          * use "SSP2-Codec" even when SSP0 is used.
0347          */
0348         .name = "SSP2-Codec",
0349         .id = 0,
0350         .no_pcm = 1,
0351         .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
0352                         | SND_SOC_DAIFMT_CBC_CFC,
0353         .be_hw_params_fixup = byt_wm5102_codec_fixup,
0354         .dpcm_playback = 1,
0355         .dpcm_capture = 1,
0356         .init = byt_wm5102_init,
0357         SND_SOC_DAILINK_REG(ssp0_port, ssp0_codec, platform),
0358     },
0359 };
0360 
0361 /* use space before codec name to simplify card ID, and simplify driver name */
0362 #define SOF_CARD_NAME "bytcht wm5102" /* card name will be 'sof-bytcht wm5102' */
0363 #define SOF_DRIVER_NAME "SOF"
0364 
0365 #define CARD_NAME "bytcr-wm5102"
0366 #define DRIVER_NAME NULL /* card name will be used for driver name */
0367 
0368 /* SoC card */
0369 static struct snd_soc_card byt_wm5102_card = {
0370     .owner = THIS_MODULE,
0371     .dai_link = byt_wm5102_dais,
0372     .num_links = ARRAY_SIZE(byt_wm5102_dais),
0373     .dapm_widgets = byt_wm5102_widgets,
0374     .num_dapm_widgets = ARRAY_SIZE(byt_wm5102_widgets),
0375     .dapm_routes = byt_wm5102_audio_map,
0376     .num_dapm_routes = ARRAY_SIZE(byt_wm5102_audio_map),
0377     .fully_routed = true,
0378 };
0379 
0380 static int snd_byt_wm5102_mc_probe(struct platform_device *pdev)
0381 {
0382     char codec_name[SND_ACPI_I2C_ID_LEN];
0383     struct device *dev = &pdev->dev;
0384     struct byt_wm5102_private *priv;
0385     struct snd_soc_acpi_mach *mach;
0386     const char *platform_name;
0387     struct acpi_device *adev;
0388     struct device *codec_dev;
0389     bool sof_parent;
0390     int ret;
0391 
0392     priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
0393     if (!priv)
0394         return -ENOMEM;
0395 
0396     /* Get MCLK */
0397     priv->mclk = devm_clk_get(dev, "pmc_plt_clk_3");
0398     if (IS_ERR(priv->mclk))
0399         return dev_err_probe(dev, PTR_ERR(priv->mclk), "getting pmc_plt_clk_3\n");
0400 
0401     /*
0402      * Get speaker VDD enable GPIO:
0403      * 1. Get codec-device-name
0404      * 2. Get codec-device
0405      * 3. Get GPIO from codec-device
0406      */
0407     mach = dev->platform_data;
0408     adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
0409     if (!adev) {
0410         dev_err(dev, "Error cannot find acpi-dev for codec\n");
0411         return -ENOENT;
0412     }
0413     snprintf(codec_name, sizeof(codec_name), "spi-%s", acpi_dev_name(adev));
0414     put_device(&adev->dev);
0415 
0416     codec_dev = bus_find_device_by_name(&spi_bus_type, NULL, codec_name);
0417     if (!codec_dev)
0418         return -EPROBE_DEFER;
0419 
0420     /* Note no devm_ here since we call gpiod_get on codec_dev rather then dev */
0421     priv->spkvdd_en_gpio = gpiod_get(codec_dev, "wlf,spkvdd-ena", GPIOD_OUT_LOW);
0422     put_device(codec_dev);
0423 
0424     if (IS_ERR(priv->spkvdd_en_gpio)) {
0425         ret = PTR_ERR(priv->spkvdd_en_gpio);
0426         /*
0427          * The spkvdd gpio-lookup is registered by: drivers/mfd/arizona-spi.c,
0428          * so -ENOENT means that arizona-spi hasn't probed yet.
0429          */
0430         if (ret == -ENOENT)
0431             ret = -EPROBE_DEFER;
0432 
0433         return dev_err_probe(dev, ret, "getting spkvdd-GPIO\n");
0434     }
0435 
0436     /* override platform name, if required */
0437     byt_wm5102_card.dev = dev;
0438     platform_name = mach->mach_params.platform;
0439     ret = snd_soc_fixup_dai_links_platform_name(&byt_wm5102_card, platform_name);
0440     if (ret)
0441         goto out_put_gpio;
0442 
0443     /* set card and driver name and pm-ops */
0444     sof_parent = snd_soc_acpi_sof_parent(dev);
0445     if (sof_parent) {
0446         byt_wm5102_card.name = SOF_CARD_NAME;
0447         byt_wm5102_card.driver_name = SOF_DRIVER_NAME;
0448         dev->driver->pm = &snd_soc_pm_ops;
0449     } else {
0450         byt_wm5102_card.name = CARD_NAME;
0451         byt_wm5102_card.driver_name = DRIVER_NAME;
0452     }
0453 
0454     snd_soc_card_set_drvdata(&byt_wm5102_card, priv);
0455     ret = devm_snd_soc_register_card(dev, &byt_wm5102_card);
0456     if (ret) {
0457         dev_err_probe(dev, ret, "registering card\n");
0458         goto out_put_gpio;
0459     }
0460 
0461     platform_set_drvdata(pdev, &byt_wm5102_card);
0462     return 0;
0463 
0464 out_put_gpio:
0465     gpiod_put(priv->spkvdd_en_gpio);
0466     return ret;
0467 }
0468 
0469 static int snd_byt_wm5102_mc_remove(struct platform_device *pdev)
0470 {
0471     struct snd_soc_card *card = platform_get_drvdata(pdev);
0472     struct byt_wm5102_private *priv = snd_soc_card_get_drvdata(card);
0473 
0474     gpiod_put(priv->spkvdd_en_gpio);
0475     return 0;
0476 }
0477 
0478 static struct platform_driver snd_byt_wm5102_mc_driver = {
0479     .driver = {
0480         .name = "bytcr_wm5102",
0481     },
0482     .probe = snd_byt_wm5102_mc_probe,
0483     .remove = snd_byt_wm5102_mc_remove,
0484 };
0485 
0486 module_platform_driver(snd_byt_wm5102_mc_driver);
0487 
0488 MODULE_DESCRIPTION("ASoC Baytrail with WM5102 codec machine driver");
0489 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
0490 MODULE_LICENSE("GPL v2");
0491 MODULE_ALIAS("platform:bytcr_wm5102");