Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  bytcht-da7213.c - ASoc Machine driver for Intel Baytrail and
0004  *             Cherrytrail-based platforms, with Dialog DA7213 codec
0005  *
0006  *  Copyright (C) 2017 Intel Corporation
0007  *  Author: Pierre-Louis Bossart <pierre-louis.bossart@linux.intel.com>
0008  *
0009  *  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0010  *
0011  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0012  */
0013 
0014 #include <linux/module.h>
0015 #include <linux/acpi.h>
0016 #include <linux/platform_device.h>
0017 #include <linux/slab.h>
0018 #include <sound/pcm.h>
0019 #include <sound/pcm_params.h>
0020 #include <sound/soc.h>
0021 #include <sound/soc-acpi.h>
0022 #include "../../codecs/da7213.h"
0023 #include "../atom/sst-atom-controls.h"
0024 
0025 static const struct snd_kcontrol_new controls[] = {
0026     SOC_DAPM_PIN_SWITCH("Headphone Jack"),
0027     SOC_DAPM_PIN_SWITCH("Headset Mic"),
0028     SOC_DAPM_PIN_SWITCH("Mic"),
0029     SOC_DAPM_PIN_SWITCH("Aux In"),
0030 };
0031 
0032 static const struct snd_soc_dapm_widget dapm_widgets[] = {
0033     SND_SOC_DAPM_HP("Headphone Jack", NULL),
0034     SND_SOC_DAPM_MIC("Headset Mic", NULL),
0035     SND_SOC_DAPM_MIC("Mic", NULL),
0036     SND_SOC_DAPM_LINE("Aux In", NULL),
0037 };
0038 
0039 static const struct snd_soc_dapm_route audio_map[] = {
0040     {"Headphone Jack", NULL, "HPL"},
0041     {"Headphone Jack", NULL, "HPR"},
0042 
0043     {"AUXL", NULL, "Aux In"},
0044     {"AUXR", NULL, "Aux In"},
0045 
0046     /* Assume Mic1 is linked to Headset and Mic2 to on-board mic */
0047     {"MIC1", NULL, "Headset Mic"},
0048     {"MIC2", NULL, "Mic"},
0049 
0050     /* SOC-codec link */
0051     {"ssp2 Tx", NULL, "codec_out0"},
0052     {"ssp2 Tx", NULL, "codec_out1"},
0053     {"codec_in0", NULL, "ssp2 Rx"},
0054     {"codec_in1", NULL, "ssp2 Rx"},
0055 
0056     {"Playback", NULL, "ssp2 Tx"},
0057     {"ssp2 Rx", NULL, "Capture"},
0058 };
0059 
0060 static int codec_fixup(struct snd_soc_pcm_runtime *rtd,
0061                struct snd_pcm_hw_params *params)
0062 {
0063     int ret;
0064     struct snd_interval *rate = hw_param_interval(params,
0065             SNDRV_PCM_HW_PARAM_RATE);
0066     struct snd_interval *channels = hw_param_interval(params,
0067                         SNDRV_PCM_HW_PARAM_CHANNELS);
0068 
0069     /* The DSP will convert the FE rate to 48k, stereo, 24bits */
0070     rate->min = rate->max = 48000;
0071     channels->min = channels->max = 2;
0072 
0073     /* set SSP2 to 24-bit */
0074     params_set_format(params, SNDRV_PCM_FORMAT_S24_LE);
0075 
0076     /*
0077      * Default mode for SSP configuration is TDM 4 slot, override config
0078      * with explicit setting to I2S 2ch 24-bit. The word length is set with
0079      * dai_set_tdm_slot() since there is no other API exposed
0080      */
0081     ret = snd_soc_dai_set_fmt(asoc_rtd_to_cpu(rtd, 0),
0082                   SND_SOC_DAIFMT_I2S     |
0083                   SND_SOC_DAIFMT_NB_NF   |
0084                   SND_SOC_DAIFMT_BP_FP);
0085     if (ret < 0) {
0086         dev_err(rtd->dev, "can't set format to I2S, err %d\n", ret);
0087         return ret;
0088     }
0089 
0090     ret = snd_soc_dai_set_tdm_slot(asoc_rtd_to_cpu(rtd, 0), 0x3, 0x3, 2, 24);
0091     if (ret < 0) {
0092         dev_err(rtd->dev, "can't set I2S config, err %d\n", ret);
0093         return ret;
0094     }
0095 
0096     return 0;
0097 }
0098 
0099 static int aif1_startup(struct snd_pcm_substream *substream)
0100 {
0101     return snd_pcm_hw_constraint_single(substream->runtime,
0102             SNDRV_PCM_HW_PARAM_RATE, 48000);
0103 }
0104 
0105 static int aif1_hw_params(struct snd_pcm_substream *substream,
0106               struct snd_pcm_hw_params *params)
0107 {
0108     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0109     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0110     int ret;
0111 
0112     ret = snd_soc_dai_set_sysclk(codec_dai, DA7213_CLKSRC_MCLK,
0113                      19200000, SND_SOC_CLOCK_IN);
0114     if (ret < 0)
0115         dev_err(codec_dai->dev, "can't set codec sysclk configuration\n");
0116 
0117     ret = snd_soc_dai_set_pll(codec_dai, 0,
0118             DA7213_SYSCLK_PLL_SRM, 0, DA7213_PLL_FREQ_OUT_98304000);
0119     if (ret < 0) {
0120         dev_err(codec_dai->dev, "failed to start PLL: %d\n", ret);
0121         return -EIO;
0122     }
0123 
0124     return ret;
0125 }
0126 
0127 static int aif1_hw_free(struct snd_pcm_substream *substream)
0128 {
0129     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0130     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0131     int ret;
0132 
0133     ret = snd_soc_dai_set_pll(codec_dai, 0,
0134                   DA7213_SYSCLK_MCLK, 0, 0);
0135     if (ret < 0) {
0136         dev_err(codec_dai->dev, "failed to stop PLL: %d\n", ret);
0137         return -EIO;
0138     }
0139 
0140     return ret;
0141 }
0142 
0143 static const struct snd_soc_ops aif1_ops = {
0144     .startup = aif1_startup,
0145 };
0146 
0147 static const struct snd_soc_ops ssp2_ops = {
0148     .hw_params = aif1_hw_params,
0149     .hw_free = aif1_hw_free,
0150 
0151 };
0152 
0153 SND_SOC_DAILINK_DEF(dummy,
0154     DAILINK_COMP_ARRAY(COMP_DUMMY()));
0155 
0156 SND_SOC_DAILINK_DEF(media,
0157     DAILINK_COMP_ARRAY(COMP_CPU("media-cpu-dai")));
0158 
0159 SND_SOC_DAILINK_DEF(deepbuffer,
0160     DAILINK_COMP_ARRAY(COMP_CPU("deepbuffer-cpu-dai")));
0161 
0162 SND_SOC_DAILINK_DEF(ssp2_port,
0163     DAILINK_COMP_ARRAY(COMP_CPU("ssp2-port")));
0164 SND_SOC_DAILINK_DEF(ssp2_codec,
0165     DAILINK_COMP_ARRAY(COMP_CODEC("i2c-DLGS7213:00",
0166                       "da7213-hifi")));
0167 
0168 SND_SOC_DAILINK_DEF(platform,
0169     DAILINK_COMP_ARRAY(COMP_PLATFORM("sst-mfld-platform")));
0170 
0171 static struct snd_soc_dai_link dailink[] = {
0172     [MERR_DPCM_AUDIO] = {
0173         .name = "Audio Port",
0174         .stream_name = "Audio",
0175         .nonatomic = true,
0176         .dynamic = 1,
0177         .dpcm_playback = 1,
0178         .dpcm_capture = 1,
0179         .ops = &aif1_ops,
0180         SND_SOC_DAILINK_REG(media, dummy, platform),
0181     },
0182     [MERR_DPCM_DEEP_BUFFER] = {
0183         .name = "Deep-Buffer Audio Port",
0184         .stream_name = "Deep-Buffer Audio",
0185         .nonatomic = true,
0186         .dynamic = 1,
0187         .dpcm_playback = 1,
0188         .ops = &aif1_ops,
0189         SND_SOC_DAILINK_REG(deepbuffer, dummy, platform),
0190     },
0191     /* CODEC<->CODEC link */
0192     /* back ends */
0193     {
0194         .name = "SSP2-Codec",
0195         .id = 0,
0196         .no_pcm = 1,
0197         .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF
0198                         | SND_SOC_DAIFMT_CBC_CFC,
0199         .be_hw_params_fixup = codec_fixup,
0200         .dpcm_playback = 1,
0201         .dpcm_capture = 1,
0202         .ops = &ssp2_ops,
0203         SND_SOC_DAILINK_REG(ssp2_port, ssp2_codec, platform),
0204     },
0205 };
0206 
0207 /* use space before codec name to simplify card ID, and simplify driver name */
0208 #define SOF_CARD_NAME "bytcht da7213" /* card name will be 'sof-bytcht da7213' */
0209 #define SOF_DRIVER_NAME "SOF"
0210 
0211 #define CARD_NAME "bytcht-da7213"
0212 #define DRIVER_NAME NULL /* card name will be used for driver name */
0213 
0214 /* SoC card */
0215 static struct snd_soc_card bytcht_da7213_card = {
0216     .name = CARD_NAME,
0217     .driver_name = DRIVER_NAME,
0218     .owner = THIS_MODULE,
0219     .dai_link = dailink,
0220     .num_links = ARRAY_SIZE(dailink),
0221     .controls = controls,
0222     .num_controls = ARRAY_SIZE(controls),
0223     .dapm_widgets = dapm_widgets,
0224     .num_dapm_widgets = ARRAY_SIZE(dapm_widgets),
0225     .dapm_routes = audio_map,
0226     .num_dapm_routes = ARRAY_SIZE(audio_map),
0227 };
0228 
0229 static char codec_name[SND_ACPI_I2C_ID_LEN];
0230 
0231 static int bytcht_da7213_probe(struct platform_device *pdev)
0232 {
0233     struct snd_soc_card *card;
0234     struct snd_soc_acpi_mach *mach;
0235     const char *platform_name;
0236     struct acpi_device *adev;
0237     bool sof_parent;
0238     int dai_index = 0;
0239     int ret_val = 0;
0240     int i;
0241 
0242     mach = pdev->dev.platform_data;
0243     card = &bytcht_da7213_card;
0244     card->dev = &pdev->dev;
0245 
0246     /* fix index of codec dai */
0247     for (i = 0; i < ARRAY_SIZE(dailink); i++) {
0248         if (!strcmp(dailink[i].codecs->name, "i2c-DLGS7213:00")) {
0249             dai_index = i;
0250             break;
0251         }
0252     }
0253 
0254     /* fixup codec name based on HID */
0255     adev = acpi_dev_get_first_match_dev(mach->id, NULL, -1);
0256     if (adev) {
0257         snprintf(codec_name, sizeof(codec_name),
0258              "i2c-%s", acpi_dev_name(adev));
0259         put_device(&adev->dev);
0260         dailink[dai_index].codecs->name = codec_name;
0261     }
0262 
0263     /* override platform name, if required */
0264     platform_name = mach->mach_params.platform;
0265 
0266     ret_val = snd_soc_fixup_dai_links_platform_name(card, platform_name);
0267     if (ret_val)
0268         return ret_val;
0269 
0270     sof_parent = snd_soc_acpi_sof_parent(&pdev->dev);
0271 
0272     /* set card and driver name */
0273     if (sof_parent) {
0274         bytcht_da7213_card.name = SOF_CARD_NAME;
0275         bytcht_da7213_card.driver_name = SOF_DRIVER_NAME;
0276     } else {
0277         bytcht_da7213_card.name = CARD_NAME;
0278         bytcht_da7213_card.driver_name = DRIVER_NAME;
0279     }
0280 
0281     /* set pm ops */
0282     if (sof_parent)
0283         pdev->dev.driver->pm = &snd_soc_pm_ops;
0284 
0285     ret_val = devm_snd_soc_register_card(&pdev->dev, card);
0286     if (ret_val) {
0287         dev_err(&pdev->dev,
0288             "snd_soc_register_card failed %d\n", ret_val);
0289         return ret_val;
0290     }
0291     platform_set_drvdata(pdev, card);
0292     return ret_val;
0293 }
0294 
0295 static struct platform_driver bytcht_da7213_driver = {
0296     .driver = {
0297         .name = "bytcht_da7213",
0298     },
0299     .probe = bytcht_da7213_probe,
0300 };
0301 module_platform_driver(bytcht_da7213_driver);
0302 
0303 MODULE_DESCRIPTION("ASoC Intel(R) Baytrail/Cherrytrail+DA7213 Machine driver");
0304 MODULE_AUTHOR("Pierre-Louis Bossart");
0305 MODULE_LICENSE("GPL v2");
0306 MODULE_ALIAS("platform:bytcht_da7213");