Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright 2011 Freescale Semiconductor, Inc.
0004  */
0005 
0006 #include <linux/module.h>
0007 #include <linux/device.h>
0008 #include <linux/of.h>
0009 #include <linux/of_device.h>
0010 #include <sound/core.h>
0011 #include <sound/pcm.h>
0012 #include <sound/soc.h>
0013 #include <sound/jack.h>
0014 #include <sound/soc-dapm.h>
0015 
0016 #include "../codecs/sgtl5000.h"
0017 #include "mxs-saif.h"
0018 
0019 static int mxs_sgtl5000_hw_params(struct snd_pcm_substream *substream,
0020     struct snd_pcm_hw_params *params)
0021 {
0022     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0023     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0024     struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
0025     unsigned int rate = params_rate(params);
0026     u32 mclk;
0027     int ret;
0028 
0029     /* sgtl5000 does not support 512*rate when in 96000 fs */
0030     switch (rate) {
0031     case 96000:
0032         mclk = 256 * rate;
0033         break;
0034     default:
0035         mclk = 512 * rate;
0036         break;
0037     }
0038 
0039     /* Set SGTL5000's SYSCLK (provided by SAIF MCLK) */
0040     ret = snd_soc_dai_set_sysclk(codec_dai, SGTL5000_SYSCLK, mclk, 0);
0041     if (ret) {
0042         dev_err(codec_dai->dev, "Failed to set sysclk to %u.%03uMHz\n",
0043             mclk / 1000000, mclk / 1000 % 1000);
0044         return ret;
0045     }
0046 
0047     /* The SAIF MCLK should be the same as SGTL5000_SYSCLK */
0048     ret = snd_soc_dai_set_sysclk(cpu_dai, MXS_SAIF_MCLK, mclk, 0);
0049     if (ret) {
0050         dev_err(cpu_dai->dev, "Failed to set sysclk to %u.%03uMHz\n",
0051             mclk / 1000000, mclk / 1000 % 1000);
0052         return ret;
0053     }
0054 
0055     return 0;
0056 }
0057 
0058 static const struct snd_soc_ops mxs_sgtl5000_hifi_ops = {
0059     .hw_params = mxs_sgtl5000_hw_params,
0060 };
0061 
0062 #define MXS_SGTL5000_DAI_FMT (SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF | \
0063     SND_SOC_DAIFMT_CBS_CFS)
0064 
0065 
0066 SND_SOC_DAILINK_DEFS(hifi_tx,
0067     DAILINK_COMP_ARRAY(COMP_EMPTY()),
0068     DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "sgtl5000")),
0069     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0070 
0071 SND_SOC_DAILINK_DEFS(hifi_rx,
0072     DAILINK_COMP_ARRAY(COMP_EMPTY()),
0073     DAILINK_COMP_ARRAY(COMP_CODEC(NULL, "sgtl5000")),
0074     DAILINK_COMP_ARRAY(COMP_EMPTY()));
0075 
0076 static struct snd_soc_dai_link mxs_sgtl5000_dai[] = {
0077     {
0078         .name       = "HiFi Tx",
0079         .stream_name    = "HiFi Playback",
0080         .dai_fmt    = MXS_SGTL5000_DAI_FMT,
0081         .ops        = &mxs_sgtl5000_hifi_ops,
0082         .playback_only  = true,
0083         SND_SOC_DAILINK_REG(hifi_tx),
0084     }, {
0085         .name       = "HiFi Rx",
0086         .stream_name    = "HiFi Capture",
0087         .dai_fmt    = MXS_SGTL5000_DAI_FMT,
0088         .ops        = &mxs_sgtl5000_hifi_ops,
0089         .capture_only   = true,
0090         SND_SOC_DAILINK_REG(hifi_rx),
0091     },
0092 };
0093 
0094 static const struct snd_soc_dapm_widget mxs_sgtl5000_dapm_widgets[] = {
0095     SND_SOC_DAPM_MIC("Mic Jack", NULL),
0096     SND_SOC_DAPM_LINE("Line In Jack", NULL),
0097     SND_SOC_DAPM_HP("Headphone Jack", NULL),
0098     SND_SOC_DAPM_SPK("Line Out Jack", NULL),
0099     SND_SOC_DAPM_SPK("Ext Spk", NULL),
0100 };
0101 
0102 static struct snd_soc_card mxs_sgtl5000 = {
0103     .name       = "mxs_sgtl5000",
0104     .owner      = THIS_MODULE,
0105     .dai_link   = mxs_sgtl5000_dai,
0106     .num_links  = ARRAY_SIZE(mxs_sgtl5000_dai),
0107 };
0108 
0109 static int mxs_sgtl5000_probe(struct platform_device *pdev)
0110 {
0111     struct snd_soc_card *card = &mxs_sgtl5000;
0112     int ret, i;
0113     struct device_node *np = pdev->dev.of_node;
0114     struct device_node *saif_np[2], *codec_np;
0115 
0116     saif_np[0] = of_parse_phandle(np, "saif-controllers", 0);
0117     saif_np[1] = of_parse_phandle(np, "saif-controllers", 1);
0118     codec_np = of_parse_phandle(np, "audio-codec", 0);
0119     if (!saif_np[0] || !saif_np[1] || !codec_np) {
0120         dev_err(&pdev->dev, "phandle missing or invalid\n");
0121         of_node_put(codec_np);
0122         of_node_put(saif_np[0]);
0123         of_node_put(saif_np[1]);
0124         return -EINVAL;
0125     }
0126 
0127     for (i = 0; i < 2; i++) {
0128         mxs_sgtl5000_dai[i].codecs->name = NULL;
0129         mxs_sgtl5000_dai[i].codecs->of_node = codec_np;
0130         mxs_sgtl5000_dai[i].cpus->dai_name = NULL;
0131         mxs_sgtl5000_dai[i].cpus->of_node = saif_np[i];
0132         mxs_sgtl5000_dai[i].platforms->name = NULL;
0133         mxs_sgtl5000_dai[i].platforms->of_node = saif_np[i];
0134     }
0135 
0136     of_node_put(codec_np);
0137     of_node_put(saif_np[0]);
0138     of_node_put(saif_np[1]);
0139 
0140     /*
0141      * Set an init clock(11.28Mhz) for sgtl5000 initialization(i2c r/w).
0142      * The Sgtl5000 sysclk is derived from saif0 mclk and it's range
0143      * should be >= 8MHz and <= 27M.
0144      */
0145     ret = mxs_saif_get_mclk(0, 44100 * 256, 44100);
0146     if (ret) {
0147         dev_err(&pdev->dev, "failed to get mclk\n");
0148         return ret;
0149     }
0150 
0151     card->dev = &pdev->dev;
0152 
0153     if (of_find_property(np, "audio-routing", NULL)) {
0154         card->dapm_widgets = mxs_sgtl5000_dapm_widgets;
0155         card->num_dapm_widgets = ARRAY_SIZE(mxs_sgtl5000_dapm_widgets);
0156 
0157         ret = snd_soc_of_parse_audio_routing(card, "audio-routing");
0158         if (ret) {
0159             dev_err(&pdev->dev, "failed to parse audio-routing (%d)\n",
0160                 ret);
0161             return ret;
0162         }
0163     }
0164 
0165     ret = devm_snd_soc_register_card(&pdev->dev, card);
0166     if (ret)
0167         return dev_err_probe(&pdev->dev, ret, "snd_soc_register_card failed\n");
0168 
0169     return 0;
0170 }
0171 
0172 static int mxs_sgtl5000_remove(struct platform_device *pdev)
0173 {
0174     mxs_saif_put_mclk(0);
0175 
0176     return 0;
0177 }
0178 
0179 static const struct of_device_id mxs_sgtl5000_dt_ids[] = {
0180     { .compatible = "fsl,mxs-audio-sgtl5000", },
0181     { /* sentinel */ }
0182 };
0183 MODULE_DEVICE_TABLE(of, mxs_sgtl5000_dt_ids);
0184 
0185 static struct platform_driver mxs_sgtl5000_audio_driver = {
0186     .driver = {
0187         .name = "mxs-sgtl5000",
0188         .of_match_table = mxs_sgtl5000_dt_ids,
0189     },
0190     .probe = mxs_sgtl5000_probe,
0191     .remove = mxs_sgtl5000_remove,
0192 };
0193 
0194 module_platform_driver(mxs_sgtl5000_audio_driver);
0195 
0196 MODULE_AUTHOR("Freescale Semiconductor, Inc.");
0197 MODULE_DESCRIPTION("MXS ALSA SoC Machine driver");
0198 MODULE_LICENSE("GPL");
0199 MODULE_ALIAS("platform:mxs-sgtl5000");