Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (c) 2015 The Linux Foundation. All rights reserved.
0004  */
0005 
0006 #include <linux/device.h>
0007 #include <linux/module.h>
0008 #include <linux/kernel.h>
0009 #include <linux/io.h>
0010 #include <linux/of.h>
0011 #include <linux/clk.h>
0012 #include <linux/platform_device.h>
0013 #include <sound/pcm.h>
0014 #include <sound/pcm_params.h>
0015 #include <sound/jack.h>
0016 #include <sound/soc.h>
0017 #include <uapi/linux/input-event-codes.h>
0018 #include <dt-bindings/sound/apq8016-lpass.h>
0019 #include "common.h"
0020 #include "qdsp6/q6afe.h"
0021 
0022 #define MI2S_COUNT  (MI2S_QUATERNARY + 1)
0023 
0024 struct apq8016_sbc_data {
0025     struct snd_soc_card card;
0026     void __iomem *mic_iomux;
0027     void __iomem *spkr_iomux;
0028     struct snd_soc_jack jack;
0029     bool jack_setup;
0030     int mi2s_clk_count[MI2S_COUNT];
0031 };
0032 
0033 #define MIC_CTRL_TER_WS_SLAVE_SEL   BIT(21)
0034 #define MIC_CTRL_QUA_WS_SLAVE_SEL_10    BIT(17)
0035 #define MIC_CTRL_TLMM_SCLK_EN       BIT(1)
0036 #define SPKR_CTL_PRI_WS_SLAVE_SEL_11    (BIT(17) | BIT(16))
0037 #define SPKR_CTL_TLMM_MCLK_EN       BIT(1)
0038 #define SPKR_CTL_TLMM_SCLK_EN       BIT(2)
0039 #define SPKR_CTL_TLMM_DATA1_EN      BIT(3)
0040 #define SPKR_CTL_TLMM_WS_OUT_SEL_MASK   GENMASK(7, 6)
0041 #define SPKR_CTL_TLMM_WS_OUT_SEL_SEC    BIT(6)
0042 #define SPKR_CTL_TLMM_WS_EN_SEL_MASK    GENMASK(19, 18)
0043 #define SPKR_CTL_TLMM_WS_EN_SEL_SEC BIT(18)
0044 #define DEFAULT_MCLK_RATE       9600000
0045 #define MI2S_BCLK_RATE          1536000
0046 
0047 static int apq8016_dai_init(struct snd_soc_pcm_runtime *rtd, int mi2s)
0048 {
0049     struct snd_soc_dai *codec_dai;
0050     struct snd_soc_component *component;
0051     struct snd_soc_card *card = rtd->card;
0052     struct apq8016_sbc_data *pdata = snd_soc_card_get_drvdata(card);
0053     int i, rval;
0054     u32 value;
0055 
0056     switch (mi2s) {
0057     case MI2S_PRIMARY:
0058         writel(readl(pdata->spkr_iomux) | SPKR_CTL_PRI_WS_SLAVE_SEL_11,
0059             pdata->spkr_iomux);
0060         break;
0061 
0062     case MI2S_QUATERNARY:
0063         /* Configure the Quat MI2S to TLMM */
0064         writel(readl(pdata->mic_iomux) | MIC_CTRL_QUA_WS_SLAVE_SEL_10 |
0065             MIC_CTRL_TLMM_SCLK_EN,
0066             pdata->mic_iomux);
0067         break;
0068     case MI2S_SECONDARY:
0069         /* Clear TLMM_WS_OUT_SEL and TLMM_WS_EN_SEL fields */
0070         value = readl(pdata->spkr_iomux) &
0071             ~(SPKR_CTL_TLMM_WS_OUT_SEL_MASK | SPKR_CTL_TLMM_WS_EN_SEL_MASK);
0072         /* Configure the Sec MI2S to TLMM */
0073         writel(value | SPKR_CTL_TLMM_MCLK_EN | SPKR_CTL_TLMM_SCLK_EN |
0074             SPKR_CTL_TLMM_DATA1_EN | SPKR_CTL_TLMM_WS_OUT_SEL_SEC |
0075             SPKR_CTL_TLMM_WS_EN_SEL_SEC, pdata->spkr_iomux);
0076         break;
0077     case MI2S_TERTIARY:
0078         writel(readl(pdata->mic_iomux) | MIC_CTRL_TER_WS_SLAVE_SEL |
0079             MIC_CTRL_TLMM_SCLK_EN,
0080             pdata->mic_iomux);
0081 
0082         break;
0083 
0084     default:
0085         dev_err(card->dev, "unsupported cpu dai configuration\n");
0086         return -EINVAL;
0087 
0088     }
0089 
0090     if (!pdata->jack_setup) {
0091         struct snd_jack *jack;
0092 
0093         rval = snd_soc_card_jack_new(card, "Headset Jack",
0094                          SND_JACK_HEADSET |
0095                          SND_JACK_HEADPHONE |
0096                          SND_JACK_BTN_0 | SND_JACK_BTN_1 |
0097                          SND_JACK_BTN_2 | SND_JACK_BTN_3 |
0098                          SND_JACK_BTN_4,
0099                          &pdata->jack);
0100 
0101         if (rval < 0) {
0102             dev_err(card->dev, "Unable to add Headphone Jack\n");
0103             return rval;
0104         }
0105 
0106         jack = pdata->jack.jack;
0107 
0108         snd_jack_set_key(jack, SND_JACK_BTN_0, KEY_PLAYPAUSE);
0109         snd_jack_set_key(jack, SND_JACK_BTN_1, KEY_VOICECOMMAND);
0110         snd_jack_set_key(jack, SND_JACK_BTN_2, KEY_VOLUMEUP);
0111         snd_jack_set_key(jack, SND_JACK_BTN_3, KEY_VOLUMEDOWN);
0112         pdata->jack_setup = true;
0113     }
0114 
0115     for_each_rtd_codec_dais(rtd, i, codec_dai) {
0116 
0117         component = codec_dai->component;
0118         /* Set default mclk for internal codec */
0119         rval = snd_soc_component_set_sysclk(component, 0, 0, DEFAULT_MCLK_RATE,
0120                        SND_SOC_CLOCK_IN);
0121         if (rval != 0 && rval != -ENOTSUPP) {
0122             dev_warn(card->dev, "Failed to set mclk: %d\n", rval);
0123             return rval;
0124         }
0125         rval = snd_soc_component_set_jack(component, &pdata->jack, NULL);
0126         if (rval != 0 && rval != -ENOTSUPP) {
0127             dev_warn(card->dev, "Failed to set jack: %d\n", rval);
0128             return rval;
0129         }
0130     }
0131 
0132     return 0;
0133 }
0134 
0135 static int apq8016_sbc_dai_init(struct snd_soc_pcm_runtime *rtd)
0136 {
0137     struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
0138 
0139     return apq8016_dai_init(rtd, cpu_dai->id);
0140 }
0141 
0142 static void apq8016_sbc_add_ops(struct snd_soc_card *card)
0143 {
0144     struct snd_soc_dai_link *link;
0145     int i;
0146 
0147     for_each_card_prelinks(card, i, link)
0148         link->init = apq8016_sbc_dai_init;
0149 }
0150 
0151 static int qdsp6_dai_get_lpass_id(struct snd_soc_dai *cpu_dai)
0152 {
0153     switch (cpu_dai->id) {
0154     case PRIMARY_MI2S_RX:
0155     case PRIMARY_MI2S_TX:
0156         return MI2S_PRIMARY;
0157     case SECONDARY_MI2S_RX:
0158     case SECONDARY_MI2S_TX:
0159         return MI2S_SECONDARY;
0160     case TERTIARY_MI2S_RX:
0161     case TERTIARY_MI2S_TX:
0162         return MI2S_TERTIARY;
0163     case QUATERNARY_MI2S_RX:
0164     case QUATERNARY_MI2S_TX:
0165         return MI2S_QUATERNARY;
0166     default:
0167         return -EINVAL;
0168     }
0169 }
0170 
0171 static int msm8916_qdsp6_dai_init(struct snd_soc_pcm_runtime *rtd)
0172 {
0173     struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
0174 
0175     snd_soc_dai_set_fmt(cpu_dai, SND_SOC_DAIFMT_BP_FP);
0176     return apq8016_dai_init(rtd, qdsp6_dai_get_lpass_id(cpu_dai));
0177 }
0178 
0179 static int msm8916_qdsp6_startup(struct snd_pcm_substream *substream)
0180 {
0181     struct snd_soc_pcm_runtime *rtd = substream->private_data;
0182     struct snd_soc_card *card = rtd->card;
0183     struct apq8016_sbc_data *data = snd_soc_card_get_drvdata(card);
0184     struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
0185     int mi2s, ret;
0186 
0187     mi2s = qdsp6_dai_get_lpass_id(cpu_dai);
0188     if (mi2s < 0)
0189         return mi2s;
0190 
0191     if (++data->mi2s_clk_count[mi2s] > 1)
0192         return 0;
0193 
0194     ret = snd_soc_dai_set_sysclk(cpu_dai, LPAIF_BIT_CLK, MI2S_BCLK_RATE, 0);
0195     if (ret)
0196         dev_err(card->dev, "Failed to enable LPAIF bit clk: %d\n", ret);
0197     return ret;
0198 }
0199 
0200 static void msm8916_qdsp6_shutdown(struct snd_pcm_substream *substream)
0201 {
0202     struct snd_soc_pcm_runtime *rtd = substream->private_data;
0203     struct snd_soc_card *card = rtd->card;
0204     struct apq8016_sbc_data *data = snd_soc_card_get_drvdata(card);
0205     struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
0206     int mi2s, ret;
0207 
0208     mi2s = qdsp6_dai_get_lpass_id(cpu_dai);
0209     if (mi2s < 0)
0210         return;
0211 
0212     if (--data->mi2s_clk_count[mi2s] > 0)
0213         return;
0214 
0215     ret = snd_soc_dai_set_sysclk(cpu_dai, LPAIF_BIT_CLK, 0, 0);
0216     if (ret)
0217         dev_err(card->dev, "Failed to disable LPAIF bit clk: %d\n", ret);
0218 }
0219 
0220 static const struct snd_soc_ops msm8916_qdsp6_be_ops = {
0221     .startup = msm8916_qdsp6_startup,
0222     .shutdown = msm8916_qdsp6_shutdown,
0223 };
0224 
0225 static int msm8916_qdsp6_be_hw_params_fixup(struct snd_soc_pcm_runtime *rtd,
0226                         struct snd_pcm_hw_params *params)
0227 {
0228     struct snd_interval *rate = hw_param_interval(params,
0229                     SNDRV_PCM_HW_PARAM_RATE);
0230     struct snd_interval *channels = hw_param_interval(params,
0231                     SNDRV_PCM_HW_PARAM_CHANNELS);
0232     struct snd_mask *fmt = hw_param_mask(params, SNDRV_PCM_HW_PARAM_FORMAT);
0233 
0234     rate->min = rate->max = 48000;
0235     channels->min = channels->max = 2;
0236     snd_mask_set_format(fmt, SNDRV_PCM_FORMAT_S16_LE);
0237 
0238     return 0;
0239 }
0240 
0241 static void msm8916_qdsp6_add_ops(struct snd_soc_card *card)
0242 {
0243     struct snd_soc_dai_link *link;
0244     int i;
0245 
0246     /* Make it obvious to userspace that QDSP6 is used */
0247     card->components = "qdsp6";
0248 
0249     for_each_card_prelinks(card, i, link) {
0250         if (link->no_pcm) {
0251             link->init = msm8916_qdsp6_dai_init;
0252             link->ops = &msm8916_qdsp6_be_ops;
0253             link->be_hw_params_fixup = msm8916_qdsp6_be_hw_params_fixup;
0254         }
0255     }
0256 }
0257 
0258 static const struct snd_soc_dapm_widget apq8016_sbc_dapm_widgets[] = {
0259 
0260     SND_SOC_DAPM_MIC("Handset Mic", NULL),
0261     SND_SOC_DAPM_MIC("Headset Mic", NULL),
0262     SND_SOC_DAPM_MIC("Secondary Mic", NULL),
0263     SND_SOC_DAPM_MIC("Digital Mic1", NULL),
0264     SND_SOC_DAPM_MIC("Digital Mic2", NULL),
0265 };
0266 
0267 static int apq8016_sbc_platform_probe(struct platform_device *pdev)
0268 {
0269     void (*add_ops)(struct snd_soc_card *card);
0270     struct device *dev = &pdev->dev;
0271     struct snd_soc_card *card;
0272     struct apq8016_sbc_data *data;
0273     int ret;
0274 
0275     add_ops = device_get_match_data(&pdev->dev);
0276     if (!add_ops)
0277         return -EINVAL;
0278 
0279     data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
0280     if (!data)
0281         return -ENOMEM;
0282 
0283     card = &data->card;
0284     card->dev = dev;
0285     card->owner = THIS_MODULE;
0286     card->dapm_widgets = apq8016_sbc_dapm_widgets;
0287     card->num_dapm_widgets = ARRAY_SIZE(apq8016_sbc_dapm_widgets);
0288 
0289     ret = qcom_snd_parse_of(card);
0290     if (ret)
0291         return ret;
0292 
0293     data->mic_iomux = devm_platform_ioremap_resource_byname(pdev, "mic-iomux");
0294     if (IS_ERR(data->mic_iomux))
0295         return PTR_ERR(data->mic_iomux);
0296 
0297     data->spkr_iomux = devm_platform_ioremap_resource_byname(pdev, "spkr-iomux");
0298     if (IS_ERR(data->spkr_iomux))
0299         return PTR_ERR(data->spkr_iomux);
0300 
0301     snd_soc_card_set_drvdata(card, data);
0302 
0303     add_ops(card);
0304     return devm_snd_soc_register_card(&pdev->dev, card);
0305 }
0306 
0307 static const struct of_device_id apq8016_sbc_device_id[] __maybe_unused = {
0308     { .compatible = "qcom,apq8016-sbc-sndcard", .data = apq8016_sbc_add_ops },
0309     { .compatible = "qcom,msm8916-qdsp6-sndcard", .data = msm8916_qdsp6_add_ops },
0310     {},
0311 };
0312 MODULE_DEVICE_TABLE(of, apq8016_sbc_device_id);
0313 
0314 static struct platform_driver apq8016_sbc_platform_driver = {
0315     .driver = {
0316         .name = "qcom-apq8016-sbc",
0317         .of_match_table = of_match_ptr(apq8016_sbc_device_id),
0318     },
0319     .probe = apq8016_sbc_platform_probe,
0320 };
0321 module_platform_driver(apq8016_sbc_platform_driver);
0322 
0323 MODULE_AUTHOR("Srinivas Kandagatla <srinivas.kandagatla@linaro.org");
0324 MODULE_DESCRIPTION("APQ8016 ASoC Machine Driver");
0325 MODULE_LICENSE("GPL v2");