Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 //
0003 // Copyright (c) 2020, The Linux Foundation. All rights reserved.
0004 //
0005 // sc7180.c -- ALSA SoC Machine driver for SC7180
0006 
0007 #include <dt-bindings/sound/sc7180-lpass.h>
0008 #include <linux/gpio.h>
0009 #include <linux/gpio/consumer.h>
0010 #include <linux/module.h>
0011 #include <linux/of_device.h>
0012 #include <linux/platform_device.h>
0013 #include <sound/core.h>
0014 #include <sound/jack.h>
0015 #include <sound/pcm.h>
0016 #include <sound/soc.h>
0017 #include <uapi/linux/input-event-codes.h>
0018 
0019 #include "../codecs/rt5682.h"
0020 #include "../codecs/rt5682s.h"
0021 #include "common.h"
0022 #include "lpass.h"
0023 
0024 #define DEFAULT_MCLK_RATE       19200000
0025 #define RT5682_PLL1_FREQ (48000 * 512)
0026 
0027 #define DRIVER_NAME "SC7180"
0028 
0029 struct sc7180_snd_data {
0030     struct snd_soc_card card;
0031     u32 pri_mi2s_clk_count;
0032     struct snd_soc_jack hs_jack;
0033     struct snd_soc_jack hdmi_jack;
0034     struct gpio_desc *dmic_sel;
0035     int dmic_switch;
0036 };
0037 
0038 static void sc7180_jack_free(struct snd_jack *jack)
0039 {
0040     struct snd_soc_component *component = jack->private_data;
0041 
0042     snd_soc_component_set_jack(component, NULL, NULL);
0043 }
0044 
0045 static int sc7180_headset_init(struct snd_soc_pcm_runtime *rtd)
0046 {
0047     struct snd_soc_card *card = rtd->card;
0048     struct sc7180_snd_data *pdata = snd_soc_card_get_drvdata(card);
0049     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0050     struct snd_soc_component *component = codec_dai->component;
0051     struct snd_jack *jack;
0052     int rval;
0053 
0054     rval = snd_soc_card_jack_new(
0055             card, "Headset Jack",
0056             SND_JACK_HEADSET |
0057             SND_JACK_HEADPHONE |
0058             SND_JACK_BTN_0 | SND_JACK_BTN_1 |
0059             SND_JACK_BTN_2 | SND_JACK_BTN_3,
0060             &pdata->hs_jack);
0061 
0062     if (rval < 0) {
0063         dev_err(card->dev, "Unable to add Headset Jack\n");
0064         return rval;
0065     }
0066 
0067     jack = pdata->hs_jack.jack;
0068 
0069     snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
0070     snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
0071     snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
0072     snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
0073 
0074     jack->private_data = component;
0075     jack->private_free = sc7180_jack_free;
0076 
0077     return snd_soc_component_set_jack(component, &pdata->hs_jack, NULL);
0078 }
0079 
0080 static int sc7180_hdmi_init(struct snd_soc_pcm_runtime *rtd)
0081 {
0082     struct snd_soc_card *card = rtd->card;
0083     struct sc7180_snd_data *pdata = snd_soc_card_get_drvdata(card);
0084     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0085     struct snd_soc_component *component = codec_dai->component;
0086     struct snd_jack *jack;
0087     int rval;
0088 
0089     rval = snd_soc_card_jack_new(
0090             card, "HDMI Jack",
0091             SND_JACK_LINEOUT,
0092             &pdata->hdmi_jack);
0093 
0094     if (rval < 0) {
0095         dev_err(card->dev, "Unable to add HDMI Jack\n");
0096         return rval;
0097     }
0098 
0099     jack = pdata->hdmi_jack.jack;
0100     jack->private_data = component;
0101     jack->private_free = sc7180_jack_free;
0102 
0103     return snd_soc_component_set_jack(component, &pdata->hdmi_jack, NULL);
0104 }
0105 
0106 static int sc7180_init(struct snd_soc_pcm_runtime *rtd)
0107 {
0108     struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
0109 
0110     switch (cpu_dai->id) {
0111     case MI2S_PRIMARY:
0112         return sc7180_headset_init(rtd);
0113     case MI2S_SECONDARY:
0114         return 0;
0115     case LPASS_DP_RX:
0116         return sc7180_hdmi_init(rtd);
0117     default:
0118         dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
0119             cpu_dai->id);
0120         return -EINVAL;
0121     }
0122     return 0;
0123 }
0124 
0125 static int sc7180_snd_startup(struct snd_pcm_substream *substream)
0126 {
0127     struct snd_soc_pcm_runtime *rtd = substream->private_data;
0128     struct snd_soc_card *card = rtd->card;
0129     struct sc7180_snd_data *data = snd_soc_card_get_drvdata(card);
0130     struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
0131     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0132     int pll_id, pll_source, pll_in, pll_out, clk_id, ret;
0133 
0134     if (!strcmp(codec_dai->name, "rt5682-aif1")) {
0135         pll_source = RT5682_PLL1_S_MCLK;
0136         pll_id = 0;
0137         clk_id = RT5682_SCLK_S_PLL1;
0138         pll_out = RT5682_PLL1_FREQ;
0139         pll_in = DEFAULT_MCLK_RATE;
0140     } else if (!strcmp(codec_dai->name, "rt5682s-aif1")) {
0141         pll_source = RT5682S_PLL_S_MCLK;
0142         pll_id = RT5682S_PLL2;
0143         clk_id = RT5682S_SCLK_S_PLL2;
0144         pll_out = RT5682_PLL1_FREQ;
0145         pll_in = DEFAULT_MCLK_RATE;
0146     }
0147 
0148     switch (cpu_dai->id) {
0149     case MI2S_PRIMARY:
0150         if (++data->pri_mi2s_clk_count == 1) {
0151             snd_soc_dai_set_sysclk(cpu_dai,
0152                            LPASS_MCLK0,
0153                            DEFAULT_MCLK_RATE,
0154                            SNDRV_PCM_STREAM_PLAYBACK);
0155         }
0156 
0157         snd_soc_dai_set_fmt(codec_dai,
0158                     SND_SOC_DAIFMT_BC_FC |
0159                     SND_SOC_DAIFMT_NB_NF |
0160                     SND_SOC_DAIFMT_I2S);
0161 
0162         /* Configure PLL1 for codec */
0163         ret = snd_soc_dai_set_pll(codec_dai, pll_id, pll_source,
0164                       pll_in, pll_out);
0165         if (ret) {
0166             dev_err(rtd->dev, "can't set codec pll: %d\n", ret);
0167             return ret;
0168         }
0169 
0170         /* Configure sysclk for codec */
0171         ret = snd_soc_dai_set_sysclk(codec_dai, clk_id, pll_out,
0172                          SND_SOC_CLOCK_IN);
0173         if (ret)
0174             dev_err(rtd->dev, "snd_soc_dai_set_sysclk err = %d\n",
0175                 ret);
0176 
0177         break;
0178     case MI2S_SECONDARY:
0179         break;
0180     case LPASS_DP_RX:
0181         break;
0182     default:
0183         dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
0184             cpu_dai->id);
0185         return -EINVAL;
0186     }
0187     return 0;
0188 }
0189 
0190 static int dmic_get(struct snd_kcontrol *kcontrol,
0191             struct snd_ctl_elem_value *ucontrol)
0192 {
0193     struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
0194     struct sc7180_snd_data *data = snd_soc_card_get_drvdata(dapm->card);
0195 
0196     ucontrol->value.integer.value[0] = data->dmic_switch;
0197     return 0;
0198 }
0199 
0200 static int dmic_set(struct snd_kcontrol *kcontrol,
0201             struct snd_ctl_elem_value *ucontrol)
0202 {
0203     struct snd_soc_dapm_context *dapm = snd_soc_dapm_kcontrol_dapm(kcontrol);
0204     struct sc7180_snd_data *data = snd_soc_card_get_drvdata(dapm->card);
0205 
0206     data->dmic_switch = ucontrol->value.integer.value[0];
0207     gpiod_set_value(data->dmic_sel, data->dmic_switch);
0208     return 0;
0209 }
0210 
0211 static void sc7180_snd_shutdown(struct snd_pcm_substream *substream)
0212 {
0213     struct snd_soc_pcm_runtime *rtd = substream->private_data;
0214     struct snd_soc_card *card = rtd->card;
0215     struct sc7180_snd_data *data = snd_soc_card_get_drvdata(card);
0216     struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
0217 
0218     switch (cpu_dai->id) {
0219     case MI2S_PRIMARY:
0220         if (--data->pri_mi2s_clk_count == 0) {
0221             snd_soc_dai_set_sysclk(cpu_dai,
0222                            LPASS_MCLK0,
0223                            0,
0224                            SNDRV_PCM_STREAM_PLAYBACK);
0225         }
0226         break;
0227     case MI2S_SECONDARY:
0228         break;
0229     case LPASS_DP_RX:
0230         break;
0231     default:
0232         dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
0233             cpu_dai->id);
0234         break;
0235     }
0236 }
0237 
0238 static int sc7180_adau7002_init(struct snd_soc_pcm_runtime *rtd)
0239 {
0240     struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
0241 
0242     switch (cpu_dai->id) {
0243     case MI2S_PRIMARY:
0244         return 0;
0245     case MI2S_SECONDARY:
0246         return 0;
0247     case LPASS_DP_RX:
0248         return sc7180_hdmi_init(rtd);
0249     default:
0250         dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
0251             cpu_dai->id);
0252         return -EINVAL;
0253     }
0254     return 0;
0255 }
0256 
0257 static int sc7180_adau7002_snd_startup(struct snd_pcm_substream *substream)
0258 {
0259     struct snd_soc_pcm_runtime *rtd = substream->private_data;
0260     struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
0261     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0262     struct snd_pcm_runtime *runtime = substream->runtime;
0263 
0264     switch (cpu_dai->id) {
0265     case MI2S_PRIMARY:
0266         snd_soc_dai_set_fmt(codec_dai,
0267                     SND_SOC_DAIFMT_CBS_CFS |
0268                     SND_SOC_DAIFMT_NB_NF |
0269                     SND_SOC_DAIFMT_I2S);
0270         runtime->hw.formats = SNDRV_PCM_FMTBIT_S32_LE;
0271         snd_pcm_hw_constraint_msbits(runtime, 0, 32, 32);
0272 
0273         break;
0274     case MI2S_SECONDARY:
0275         break;
0276     case LPASS_DP_RX:
0277         break;
0278     default:
0279         dev_err(rtd->dev, "%s: invalid dai id 0x%x\n", __func__,
0280             cpu_dai->id);
0281         return -EINVAL;
0282     }
0283     return 0;
0284 }
0285 
0286 static const struct snd_soc_ops sc7180_ops = {
0287     .startup = sc7180_snd_startup,
0288     .shutdown = sc7180_snd_shutdown,
0289 };
0290 
0291 static const struct snd_soc_ops sc7180_adau7002_ops = {
0292     .startup = sc7180_adau7002_snd_startup,
0293 };
0294 
0295 static const struct snd_soc_dapm_widget sc7180_snd_widgets[] = {
0296     SND_SOC_DAPM_HP("Headphone Jack", NULL),
0297     SND_SOC_DAPM_MIC("Headset Mic", NULL),
0298 };
0299 
0300 static const struct snd_soc_dapm_widget sc7180_adau7002_snd_widgets[] = {
0301     SND_SOC_DAPM_MIC("DMIC", NULL),
0302 };
0303 
0304 static const char * const dmic_mux_text[] = {
0305     "Front Mic",
0306     "Rear Mic",
0307 };
0308 
0309 static SOC_ENUM_SINGLE_DECL(sc7180_dmic_enum,
0310                 SND_SOC_NOPM, 0, dmic_mux_text);
0311 
0312 static const struct snd_kcontrol_new sc7180_dmic_mux_control =
0313     SOC_DAPM_ENUM_EXT("DMIC Select Mux", sc7180_dmic_enum,
0314               dmic_get, dmic_set);
0315 
0316 static const struct snd_soc_dapm_widget sc7180_snd_dual_mic_widgets[] = {
0317     SND_SOC_DAPM_HP("Headphone Jack", NULL),
0318     SND_SOC_DAPM_MIC("Headset Mic", NULL),
0319     SND_SOC_DAPM_MIC("DMIC", NULL),
0320     SND_SOC_DAPM_MUX("Dmic Mux", SND_SOC_NOPM, 0, 0, &sc7180_dmic_mux_control),
0321 };
0322 
0323 static const struct snd_soc_dapm_route sc7180_snd_dual_mic_audio_route[] = {
0324     {"Dmic Mux", "Front Mic", "DMIC"},
0325     {"Dmic Mux", "Rear Mic", "DMIC"},
0326 };
0327 
0328 static int sc7180_snd_platform_probe(struct platform_device *pdev)
0329 {
0330     struct snd_soc_card *card;
0331     struct sc7180_snd_data *data;
0332     struct device *dev = &pdev->dev;
0333     struct snd_soc_dai_link *link;
0334     int ret;
0335     int i;
0336     bool no_headphone = false;
0337 
0338     /* Allocate the private data */
0339     data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
0340     if (!data)
0341         return -ENOMEM;
0342 
0343     card = &data->card;
0344     snd_soc_card_set_drvdata(card, data);
0345 
0346     card->owner = THIS_MODULE;
0347     card->driver_name = DRIVER_NAME;
0348     card->dev = dev;
0349     card->dapm_widgets = sc7180_snd_widgets;
0350     card->num_dapm_widgets = ARRAY_SIZE(sc7180_snd_widgets);
0351 
0352     if (of_property_read_bool(dev->of_node, "dmic-gpios")) {
0353         card->dapm_widgets = sc7180_snd_dual_mic_widgets,
0354         card->num_dapm_widgets = ARRAY_SIZE(sc7180_snd_dual_mic_widgets),
0355         card->dapm_routes = sc7180_snd_dual_mic_audio_route,
0356         card->num_dapm_routes = ARRAY_SIZE(sc7180_snd_dual_mic_audio_route),
0357         data->dmic_sel = devm_gpiod_get(&pdev->dev, "dmic", GPIOD_OUT_LOW);
0358         if (IS_ERR(data->dmic_sel)) {
0359             dev_err(&pdev->dev, "DMIC gpio failed err=%ld\n", PTR_ERR(data->dmic_sel));
0360             return PTR_ERR(data->dmic_sel);
0361         }
0362     }
0363 
0364     if (of_device_is_compatible(dev->of_node, "google,sc7180-coachz")) {
0365         no_headphone = true;
0366         card->dapm_widgets = sc7180_adau7002_snd_widgets;
0367         card->num_dapm_widgets = ARRAY_SIZE(sc7180_adau7002_snd_widgets);
0368     }
0369 
0370     ret = qcom_snd_parse_of(card);
0371     if (ret)
0372         return ret;
0373 
0374     for_each_card_prelinks(card, i, link) {
0375         if (no_headphone) {
0376             link->ops = &sc7180_adau7002_ops;
0377             link->init = sc7180_adau7002_init;
0378         } else {
0379             link->ops = &sc7180_ops;
0380             link->init = sc7180_init;
0381         }
0382     }
0383 
0384     return devm_snd_soc_register_card(dev, card);
0385 }
0386 
0387 static const struct of_device_id sc7180_snd_device_id[]  = {
0388     {.compatible = "google,sc7180-trogdor"},
0389     {.compatible = "google,sc7180-coachz"},
0390     {},
0391 };
0392 MODULE_DEVICE_TABLE(of, sc7180_snd_device_id);
0393 
0394 static struct platform_driver sc7180_snd_driver = {
0395     .probe = sc7180_snd_platform_probe,
0396     .driver = {
0397         .name = "msm-snd-sc7180",
0398         .of_match_table = sc7180_snd_device_id,
0399         .pm = &snd_soc_pm_ops,
0400     },
0401 };
0402 module_platform_driver(sc7180_snd_driver);
0403 
0404 MODULE_DESCRIPTION("sc7180 ASoC Machine Driver");
0405 MODULE_LICENSE("GPL v2");