Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * zylonite.c  --  SoC audio for Zylonite
0004  *
0005  * Copyright 2008 Wolfson Microelectronics PLC.
0006  * Author: Mark Brown <broonie@opensource.wolfsonmicro.com>
0007  */
0008 
0009 #include <linux/module.h>
0010 #include <linux/moduleparam.h>
0011 #include <linux/device.h>
0012 #include <linux/clk.h>
0013 #include <linux/i2c.h>
0014 #include <sound/core.h>
0015 #include <sound/pcm.h>
0016 #include <sound/pcm_params.h>
0017 #include <sound/soc.h>
0018 
0019 #include "../codecs/wm9713.h"
0020 #include "pxa-ssp.h"
0021 
0022 /*
0023  * There is a physical switch SW15 on the board which changes the MCLK
0024  * for the WM9713 between the standard AC97 master clock and the
0025  * output of the CLK_POUT signal from the PXA.
0026  */
0027 static int clk_pout;
0028 module_param(clk_pout, int, 0);
0029 MODULE_PARM_DESC(clk_pout, "Use CLK_POUT as WM9713 MCLK (SW15 on board).");
0030 
0031 static struct clk *pout;
0032 
0033 static struct snd_soc_card zylonite;
0034 
0035 static const struct snd_soc_dapm_widget zylonite_dapm_widgets[] = {
0036     SND_SOC_DAPM_HP("Headphone", NULL),
0037     SND_SOC_DAPM_MIC("Headset Microphone", NULL),
0038     SND_SOC_DAPM_MIC("Handset Microphone", NULL),
0039     SND_SOC_DAPM_SPK("Multiactor", NULL),
0040     SND_SOC_DAPM_SPK("Headset Earpiece", NULL),
0041 };
0042 
0043 /* Currently supported audio map */
0044 static const struct snd_soc_dapm_route audio_map[] = {
0045 
0046     /* Headphone output connected to HPL/HPR */
0047     { "Headphone", NULL,  "HPL" },
0048     { "Headphone", NULL,  "HPR" },
0049 
0050     /* On-board earpiece */
0051     { "Headset Earpiece", NULL, "OUT3" },
0052 
0053     /* Headphone mic */
0054     { "MIC2A", NULL, "Mic Bias" },
0055     { "Mic Bias", NULL, "Headset Microphone" },
0056 
0057     /* On-board mic */
0058     { "MIC1", NULL, "Mic Bias" },
0059     { "Mic Bias", NULL, "Handset Microphone" },
0060 
0061     /* Multiactor differentially connected over SPKL/SPKR */
0062     { "Multiactor", NULL, "SPKL" },
0063     { "Multiactor", NULL, "SPKR" },
0064 };
0065 
0066 static int zylonite_wm9713_init(struct snd_soc_pcm_runtime *rtd)
0067 {
0068     if (clk_pout)
0069         snd_soc_dai_set_pll(asoc_rtd_to_codec(rtd, 0), 0, 0,
0070                     clk_get_rate(pout), 0);
0071 
0072     return 0;
0073 }
0074 
0075 static int zylonite_voice_hw_params(struct snd_pcm_substream *substream,
0076                     struct snd_pcm_hw_params *params)
0077 {
0078     struct snd_soc_pcm_runtime *rtd = asoc_substream_to_rtd(substream);
0079     struct snd_soc_dai *codec_dai = asoc_rtd_to_codec(rtd, 0);
0080     struct snd_soc_dai *cpu_dai = asoc_rtd_to_cpu(rtd, 0);
0081     unsigned int wm9713_div = 0;
0082     int ret = 0;
0083     int rate = params_rate(params);
0084 
0085     /* Only support ratios that we can generate neatly from the AC97
0086      * based master clock - in particular, this excludes 44.1kHz.
0087      * In most applications the voice DAC will be used for telephony
0088      * data so multiples of 8kHz will be the common case.
0089      */
0090     switch (rate) {
0091     case 8000:
0092         wm9713_div = 12;
0093         break;
0094     case 16000:
0095         wm9713_div = 6;
0096         break;
0097     case 48000:
0098         wm9713_div = 2;
0099         break;
0100     default:
0101         /* Don't support OSS emulation */
0102         return -EINVAL;
0103     }
0104 
0105     ret = snd_soc_dai_set_sysclk(cpu_dai, PXA_SSP_CLK_AUDIO, 0, 1);
0106     if (ret < 0)
0107         return ret;
0108 
0109     if (clk_pout)
0110         ret = snd_soc_dai_set_clkdiv(codec_dai, WM9713_PCMCLK_PLL_DIV,
0111                          WM9713_PCMDIV(wm9713_div));
0112     else
0113         ret = snd_soc_dai_set_clkdiv(codec_dai, WM9713_PCMCLK_DIV,
0114                          WM9713_PCMDIV(wm9713_div));
0115     if (ret < 0)
0116         return ret;
0117 
0118     return 0;
0119 }
0120 
0121 static const struct snd_soc_ops zylonite_voice_ops = {
0122     .hw_params = zylonite_voice_hw_params,
0123 };
0124 
0125 SND_SOC_DAILINK_DEFS(ac97,
0126     DAILINK_COMP_ARRAY(COMP_CPU("pxa2xx-ac97")),
0127     DAILINK_COMP_ARRAY(COMP_CODEC("wm9713-codec", "wm9713-hifi")),
0128     DAILINK_COMP_ARRAY(COMP_PLATFORM("pxa-pcm-audio")));
0129 
0130 SND_SOC_DAILINK_DEFS(ac97_aux,
0131     DAILINK_COMP_ARRAY(COMP_CPU("pxa2xx-ac97-aux")),
0132     DAILINK_COMP_ARRAY(COMP_CODEC("wm9713-codec", "wm9713-aux")),
0133     DAILINK_COMP_ARRAY(COMP_PLATFORM("pxa-pcm-audio")));
0134 
0135 SND_SOC_DAILINK_DEFS(voice,
0136     DAILINK_COMP_ARRAY(COMP_CPU("pxa-ssp-dai.2")),
0137     DAILINK_COMP_ARRAY(COMP_CODEC("wm9713-codec", "wm9713-voice")),
0138     DAILINK_COMP_ARRAY(COMP_PLATFORM("pxa-pcm-audio")));
0139 
0140 static struct snd_soc_dai_link zylonite_dai[] = {
0141 {
0142     .name = "AC97",
0143     .stream_name = "AC97 HiFi",
0144     .init = zylonite_wm9713_init,
0145     SND_SOC_DAILINK_REG(ac97),
0146 },
0147 {
0148     .name = "AC97 Aux",
0149     .stream_name = "AC97 Aux",
0150     SND_SOC_DAILINK_REG(ac97_aux),
0151 },
0152 {
0153     .name = "WM9713 Voice",
0154     .stream_name = "WM9713 Voice",
0155     .dai_fmt = SND_SOC_DAIFMT_I2S | SND_SOC_DAIFMT_NB_NF |
0156            SND_SOC_DAIFMT_CBS_CFS,
0157     .ops = &zylonite_voice_ops,
0158     SND_SOC_DAILINK_REG(voice),
0159 },
0160 };
0161 
0162 static int zylonite_probe(struct snd_soc_card *card)
0163 {
0164     int ret;
0165 
0166     if (clk_pout) {
0167         pout = clk_get(NULL, "CLK_POUT");
0168         if (IS_ERR(pout)) {
0169             dev_err(card->dev, "Unable to obtain CLK_POUT: %ld\n",
0170                 PTR_ERR(pout));
0171             return PTR_ERR(pout);
0172         }
0173 
0174         ret = clk_enable(pout);
0175         if (ret != 0) {
0176             dev_err(card->dev, "Unable to enable CLK_POUT: %d\n",
0177                 ret);
0178             clk_put(pout);
0179             return ret;
0180         }
0181 
0182         dev_dbg(card->dev, "MCLK enabled at %luHz\n",
0183             clk_get_rate(pout));
0184     }
0185 
0186     return 0;
0187 }
0188 
0189 static int zylonite_remove(struct snd_soc_card *card)
0190 {
0191     if (clk_pout) {
0192         clk_disable(pout);
0193         clk_put(pout);
0194     }
0195 
0196     return 0;
0197 }
0198 
0199 static int zylonite_suspend_post(struct snd_soc_card *card)
0200 {
0201     if (clk_pout)
0202         clk_disable(pout);
0203 
0204     return 0;
0205 }
0206 
0207 static int zylonite_resume_pre(struct snd_soc_card *card)
0208 {
0209     int ret = 0;
0210 
0211     if (clk_pout) {
0212         ret = clk_enable(pout);
0213         if (ret != 0)
0214             dev_err(card->dev, "Unable to enable CLK_POUT: %d\n",
0215                 ret);
0216     }
0217 
0218     return ret;
0219 }
0220 
0221 static struct snd_soc_card zylonite = {
0222     .name = "Zylonite",
0223     .owner = THIS_MODULE,
0224     .probe = &zylonite_probe,
0225     .remove = &zylonite_remove,
0226     .suspend_post = &zylonite_suspend_post,
0227     .resume_pre = &zylonite_resume_pre,
0228     .dai_link = zylonite_dai,
0229     .num_links = ARRAY_SIZE(zylonite_dai),
0230 
0231     .dapm_widgets = zylonite_dapm_widgets,
0232     .num_dapm_widgets = ARRAY_SIZE(zylonite_dapm_widgets),
0233     .dapm_routes = audio_map,
0234     .num_dapm_routes = ARRAY_SIZE(audio_map),
0235 };
0236 
0237 static struct platform_device *zylonite_snd_ac97_device;
0238 
0239 static int __init zylonite_init(void)
0240 {
0241     int ret;
0242 
0243     zylonite_snd_ac97_device = platform_device_alloc("soc-audio", -1);
0244     if (!zylonite_snd_ac97_device)
0245         return -ENOMEM;
0246 
0247     platform_set_drvdata(zylonite_snd_ac97_device, &zylonite);
0248 
0249     ret = platform_device_add(zylonite_snd_ac97_device);
0250     if (ret != 0)
0251         platform_device_put(zylonite_snd_ac97_device);
0252 
0253     return ret;
0254 }
0255 
0256 static void __exit zylonite_exit(void)
0257 {
0258     platform_device_unregister(zylonite_snd_ac97_device);
0259 }
0260 
0261 module_init(zylonite_init);
0262 module_exit(zylonite_exit);
0263 
0264 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
0265 MODULE_DESCRIPTION("ALSA SoC WM9713 Zylonite");
0266 MODULE_LICENSE("GPL");