Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * wm9705.c  --  ALSA Soc WM9705 codec support
0004  *
0005  * Copyright 2008 Ian Molton <spyro@f2s.com>
0006  */
0007 
0008 #include <linux/init.h>
0009 #include <linux/slab.h>
0010 #include <linux/mfd/wm97xx.h>
0011 #include <linux/module.h>
0012 #include <linux/kernel.h>
0013 #include <linux/device.h>
0014 #include <linux/regmap.h>
0015 #include <sound/core.h>
0016 #include <sound/pcm.h>
0017 #include <sound/ac97_codec.h>
0018 #include <sound/ac97/codec.h>
0019 #include <sound/ac97/compat.h>
0020 #include <sound/initval.h>
0021 #include <sound/soc.h>
0022 
0023 #define WM9705_VENDOR_ID 0x574d4c05
0024 #define WM9705_VENDOR_ID_MASK 0xffffffff
0025 
0026 struct wm9705_priv {
0027     struct snd_ac97 *ac97;
0028     struct wm97xx_platform_data *mfd_pdata;
0029 };
0030 
0031 static const struct reg_default wm9705_reg_defaults[] = {
0032     { 0x02, 0x8000 },
0033     { 0x04, 0x8000 },
0034     { 0x06, 0x8000 },
0035     { 0x0a, 0x8000 },
0036     { 0x0c, 0x8008 },
0037     { 0x0e, 0x8008 },
0038     { 0x10, 0x8808 },
0039     { 0x12, 0x8808 },
0040     { 0x14, 0x8808 },
0041     { 0x16, 0x8808 },
0042     { 0x18, 0x8808 },
0043     { 0x1a, 0x0000 },
0044     { 0x1c, 0x8000 },
0045     { 0x20, 0x0000 },
0046     { 0x22, 0x0000 },
0047     { 0x26, 0x000f },
0048     { 0x28, 0x0605 },
0049     { 0x2a, 0x0000 },
0050     { 0x2c, 0xbb80 },
0051     { 0x32, 0xbb80 },
0052     { 0x34, 0x2000 },
0053     { 0x5a, 0x0000 },
0054     { 0x5c, 0x0000 },
0055     { 0x72, 0x0808 },
0056     { 0x74, 0x0000 },
0057     { 0x76, 0x0006 },
0058     { 0x78, 0x0000 },
0059     { 0x7a, 0x0000 },
0060 };
0061 
0062 static const struct regmap_config wm9705_regmap_config = {
0063     .reg_bits = 16,
0064     .reg_stride = 2,
0065     .val_bits = 16,
0066     .max_register = 0x7e,
0067     .cache_type = REGCACHE_RBTREE,
0068 
0069     .volatile_reg = regmap_ac97_default_volatile,
0070 
0071     .reg_defaults = wm9705_reg_defaults,
0072     .num_reg_defaults = ARRAY_SIZE(wm9705_reg_defaults),
0073 };
0074 
0075 static const struct snd_kcontrol_new wm9705_snd_ac97_controls[] = {
0076     SOC_DOUBLE("Master Playback Volume", AC97_MASTER, 8, 0, 31, 1),
0077     SOC_SINGLE("Master Playback Switch", AC97_MASTER, 15, 1, 1),
0078     SOC_DOUBLE("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1),
0079     SOC_SINGLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 1, 1),
0080     SOC_DOUBLE("PCM Playback Volume", AC97_PCM, 8, 0, 31, 1),
0081     SOC_SINGLE("PCM Playback Switch", AC97_PCM, 15, 1, 1),
0082     SOC_SINGLE("Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1),
0083     SOC_SINGLE("Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
0084     SOC_SINGLE("PCBeep Playback Volume", AC97_PC_BEEP, 1, 15, 1),
0085     SOC_SINGLE("Phone Playback Volume", AC97_PHONE, 0, 31, 1),
0086     SOC_DOUBLE("Line Playback Volume", AC97_LINE, 8, 0, 31, 1),
0087     SOC_DOUBLE("CD Playback Volume", AC97_CD, 8, 0, 31, 1),
0088     SOC_SINGLE("Mic Playback Volume", AC97_MIC, 0, 31, 1),
0089     SOC_SINGLE("Mic 20dB Boost Switch", AC97_MIC, 6, 1, 0),
0090     SOC_DOUBLE("Capture Volume", AC97_REC_GAIN, 8, 0, 15, 0),
0091     SOC_SINGLE("Capture Switch", AC97_REC_GAIN, 15, 1, 1),
0092 };
0093 
0094 static const char *wm9705_mic[] = {"Mic 1", "Mic 2"};
0095 static const char *wm9705_rec_sel[] = {"Mic", "CD", "NC", "NC",
0096     "Line", "Stereo Mix", "Mono Mix", "Phone"};
0097 
0098 static SOC_ENUM_SINGLE_DECL(wm9705_enum_mic,
0099                 AC97_GENERAL_PURPOSE, 8, wm9705_mic);
0100 static SOC_ENUM_SINGLE_DECL(wm9705_enum_rec_l,
0101                 AC97_REC_SEL, 8, wm9705_rec_sel);
0102 static SOC_ENUM_SINGLE_DECL(wm9705_enum_rec_r,
0103                 AC97_REC_SEL, 0, wm9705_rec_sel);
0104 
0105 /* Headphone Mixer */
0106 static const struct snd_kcontrol_new wm9705_hp_mixer_controls[] = {
0107     SOC_DAPM_SINGLE("PCBeep Playback Switch", AC97_PC_BEEP, 15, 1, 1),
0108     SOC_DAPM_SINGLE("CD Playback Switch", AC97_CD, 15, 1, 1),
0109     SOC_DAPM_SINGLE("Mic Playback Switch", AC97_MIC, 15, 1, 1),
0110     SOC_DAPM_SINGLE("Phone Playback Switch", AC97_PHONE, 15, 1, 1),
0111     SOC_DAPM_SINGLE("Line Playback Switch", AC97_LINE, 15, 1, 1),
0112 };
0113 
0114 /* Mic source */
0115 static const struct snd_kcontrol_new wm9705_mic_src_controls =
0116     SOC_DAPM_ENUM("Route", wm9705_enum_mic);
0117 
0118 /* Capture source */
0119 static const struct snd_kcontrol_new wm9705_capture_selectl_controls =
0120     SOC_DAPM_ENUM("Route", wm9705_enum_rec_l);
0121 static const struct snd_kcontrol_new wm9705_capture_selectr_controls =
0122     SOC_DAPM_ENUM("Route", wm9705_enum_rec_r);
0123 
0124 /* DAPM widgets */
0125 static const struct snd_soc_dapm_widget wm9705_dapm_widgets[] = {
0126     SND_SOC_DAPM_MUX("Mic Source", SND_SOC_NOPM, 0, 0,
0127         &wm9705_mic_src_controls),
0128     SND_SOC_DAPM_MUX("Left Capture Source", SND_SOC_NOPM, 0, 0,
0129         &wm9705_capture_selectl_controls),
0130     SND_SOC_DAPM_MUX("Right Capture Source", SND_SOC_NOPM, 0, 0,
0131         &wm9705_capture_selectr_controls),
0132     SND_SOC_DAPM_DAC("Left DAC", "Left HiFi Playback",
0133         SND_SOC_NOPM, 0, 0),
0134     SND_SOC_DAPM_DAC("Right DAC", "Right HiFi Playback",
0135         SND_SOC_NOPM, 0, 0),
0136     SND_SOC_DAPM_MIXER_NAMED_CTL("HP Mixer", SND_SOC_NOPM, 0, 0,
0137         &wm9705_hp_mixer_controls[0],
0138         ARRAY_SIZE(wm9705_hp_mixer_controls)),
0139     SND_SOC_DAPM_MIXER("Mono Mixer", SND_SOC_NOPM, 0, 0, NULL, 0),
0140     SND_SOC_DAPM_ADC("Left ADC", "Left HiFi Capture", SND_SOC_NOPM, 0, 0),
0141     SND_SOC_DAPM_ADC("Right ADC", "Right HiFi Capture", SND_SOC_NOPM, 0, 0),
0142     SND_SOC_DAPM_PGA("Headphone PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
0143     SND_SOC_DAPM_PGA("Speaker PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
0144     SND_SOC_DAPM_PGA("Line PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
0145     SND_SOC_DAPM_PGA("Line out PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
0146     SND_SOC_DAPM_PGA("Mono PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
0147     SND_SOC_DAPM_PGA("Phone PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
0148     SND_SOC_DAPM_PGA("Mic PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
0149     SND_SOC_DAPM_PGA("PCBEEP PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
0150     SND_SOC_DAPM_PGA("CD PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
0151     SND_SOC_DAPM_PGA("ADC PGA", SND_SOC_NOPM, 0, 0, NULL, 0),
0152     SND_SOC_DAPM_OUTPUT("HPOUTL"),
0153     SND_SOC_DAPM_OUTPUT("HPOUTR"),
0154     SND_SOC_DAPM_OUTPUT("LOUT"),
0155     SND_SOC_DAPM_OUTPUT("ROUT"),
0156     SND_SOC_DAPM_OUTPUT("MONOOUT"),
0157     SND_SOC_DAPM_INPUT("PHONE"),
0158     SND_SOC_DAPM_INPUT("LINEINL"),
0159     SND_SOC_DAPM_INPUT("LINEINR"),
0160     SND_SOC_DAPM_INPUT("CDINL"),
0161     SND_SOC_DAPM_INPUT("CDINR"),
0162     SND_SOC_DAPM_INPUT("PCBEEP"),
0163     SND_SOC_DAPM_INPUT("MIC1"),
0164     SND_SOC_DAPM_INPUT("MIC2"),
0165 };
0166 
0167 /* Audio map
0168  * WM9705 has no switches to disable the route from the inputs to the HP mixer
0169  * so in order to prevent active inputs from forcing the audio outputs to be
0170  * constantly enabled, we use the mutes on those inputs to simulate such
0171  * controls.
0172  */
0173 static const struct snd_soc_dapm_route wm9705_audio_map[] = {
0174     /* HP mixer */
0175     {"HP Mixer", "PCBeep Playback Switch", "PCBEEP PGA"},
0176     {"HP Mixer", "CD Playback Switch", "CD PGA"},
0177     {"HP Mixer", "Mic Playback Switch", "Mic PGA"},
0178     {"HP Mixer", "Phone Playback Switch", "Phone PGA"},
0179     {"HP Mixer", "Line Playback Switch", "Line PGA"},
0180     {"HP Mixer", NULL, "Left DAC"},
0181     {"HP Mixer", NULL, "Right DAC"},
0182 
0183     /* mono mixer */
0184     {"Mono Mixer", NULL, "HP Mixer"},
0185 
0186     /* outputs */
0187     {"Headphone PGA", NULL, "HP Mixer"},
0188     {"HPOUTL", NULL, "Headphone PGA"},
0189     {"HPOUTR", NULL, "Headphone PGA"},
0190     {"Line out PGA", NULL, "HP Mixer"},
0191     {"LOUT", NULL, "Line out PGA"},
0192     {"ROUT", NULL, "Line out PGA"},
0193     {"Mono PGA", NULL, "Mono Mixer"},
0194     {"MONOOUT", NULL, "Mono PGA"},
0195 
0196     /* inputs */
0197     {"CD PGA", NULL, "CDINL"},
0198     {"CD PGA", NULL, "CDINR"},
0199     {"Line PGA", NULL, "LINEINL"},
0200     {"Line PGA", NULL, "LINEINR"},
0201     {"Phone PGA", NULL, "PHONE"},
0202     {"Mic Source", "Mic 1", "MIC1"},
0203     {"Mic Source", "Mic 2", "MIC2"},
0204     {"Mic PGA", NULL, "Mic Source"},
0205     {"PCBEEP PGA", NULL, "PCBEEP"},
0206 
0207     /* Left capture selector */
0208     {"Left Capture Source", "Mic", "Mic Source"},
0209     {"Left Capture Source", "CD", "CDINL"},
0210     {"Left Capture Source", "Line", "LINEINL"},
0211     {"Left Capture Source", "Stereo Mix", "HP Mixer"},
0212     {"Left Capture Source", "Mono Mix", "HP Mixer"},
0213     {"Left Capture Source", "Phone", "PHONE"},
0214 
0215     /* Right capture source */
0216     {"Right Capture Source", "Mic", "Mic Source"},
0217     {"Right Capture Source", "CD", "CDINR"},
0218     {"Right Capture Source", "Line", "LINEINR"},
0219     {"Right Capture Source", "Stereo Mix", "HP Mixer"},
0220     {"Right Capture Source", "Mono Mix", "HP Mixer"},
0221     {"Right Capture Source", "Phone", "PHONE"},
0222 
0223     {"ADC PGA", NULL, "Left Capture Source"},
0224     {"ADC PGA", NULL, "Right Capture Source"},
0225 
0226     /* ADC's */
0227     {"Left ADC",  NULL, "ADC PGA"},
0228     {"Right ADC", NULL, "ADC PGA"},
0229 };
0230 
0231 static int ac97_prepare(struct snd_pcm_substream *substream,
0232             struct snd_soc_dai *dai)
0233 {
0234     struct snd_soc_component *component = dai->component;
0235     int reg;
0236 
0237     snd_soc_component_update_bits(component, AC97_EXTENDED_STATUS, 0x1, 0x1);
0238 
0239     if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
0240         reg = AC97_PCM_FRONT_DAC_RATE;
0241     else
0242         reg = AC97_PCM_LR_ADC_RATE;
0243 
0244     return snd_soc_component_write(component, reg, substream->runtime->rate);
0245 }
0246 
0247 #define WM9705_AC97_RATES (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_11025 | \
0248             SNDRV_PCM_RATE_16000 | SNDRV_PCM_RATE_22050 | \
0249             SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100 | \
0250             SNDRV_PCM_RATE_48000)
0251 
0252 static const struct snd_soc_dai_ops wm9705_dai_ops = {
0253     .prepare    = ac97_prepare,
0254 };
0255 
0256 static struct snd_soc_dai_driver wm9705_dai[] = {
0257     {
0258         .name = "wm9705-hifi",
0259         .playback = {
0260             .stream_name = "HiFi Playback",
0261             .channels_min = 1,
0262             .channels_max = 2,
0263             .rates = WM9705_AC97_RATES,
0264             .formats = SND_SOC_STD_AC97_FMTS,
0265         },
0266         .capture = {
0267             .stream_name = "HiFi Capture",
0268             .channels_min = 1,
0269             .channels_max = 2,
0270             .rates = WM9705_AC97_RATES,
0271             .formats = SND_SOC_STD_AC97_FMTS,
0272         },
0273         .ops = &wm9705_dai_ops,
0274     },
0275     {
0276         .name = "wm9705-aux",
0277         .playback = {
0278             .stream_name = "Aux Playback",
0279             .channels_min = 1,
0280             .channels_max = 1,
0281             .rates = WM9705_AC97_RATES,
0282             .formats = SNDRV_PCM_FMTBIT_S16_LE,
0283         },
0284     }
0285 };
0286 
0287 #ifdef CONFIG_PM
0288 static int wm9705_soc_suspend(struct snd_soc_component *component)
0289 {
0290     regcache_cache_bypass(component->regmap, true);
0291     snd_soc_component_write(component, AC97_POWERDOWN, 0xffff);
0292     regcache_cache_bypass(component->regmap, false);
0293 
0294     return 0;
0295 }
0296 
0297 static int wm9705_soc_resume(struct snd_soc_component *component)
0298 {
0299     struct wm9705_priv *wm9705 = snd_soc_component_get_drvdata(component);
0300     int ret;
0301 
0302     ret = snd_ac97_reset(wm9705->ac97, true, WM9705_VENDOR_ID,
0303         WM9705_VENDOR_ID_MASK);
0304     if (ret < 0)
0305         return ret;
0306 
0307     snd_soc_component_cache_sync(component);
0308 
0309     return 0;
0310 }
0311 #else
0312 #define wm9705_soc_suspend NULL
0313 #define wm9705_soc_resume NULL
0314 #endif
0315 
0316 static int wm9705_soc_probe(struct snd_soc_component *component)
0317 {
0318     struct wm9705_priv *wm9705 = snd_soc_component_get_drvdata(component);
0319     struct regmap *regmap;
0320 
0321     if (wm9705->mfd_pdata) {
0322         wm9705->ac97 = wm9705->mfd_pdata->ac97;
0323         regmap = wm9705->mfd_pdata->regmap;
0324     } else if (IS_ENABLED(CONFIG_SND_SOC_AC97_BUS)) {
0325         wm9705->ac97 = snd_soc_new_ac97_component(component, WM9705_VENDOR_ID,
0326                               WM9705_VENDOR_ID_MASK);
0327         if (IS_ERR(wm9705->ac97)) {
0328             dev_err(component->dev, "Failed to register AC97 codec\n");
0329             return PTR_ERR(wm9705->ac97);
0330         }
0331 
0332         regmap = regmap_init_ac97(wm9705->ac97, &wm9705_regmap_config);
0333         if (IS_ERR(regmap)) {
0334             snd_soc_free_ac97_component(wm9705->ac97);
0335             return PTR_ERR(regmap);
0336         }
0337     } else {
0338         return -ENXIO;
0339     }
0340 
0341     snd_soc_component_set_drvdata(component, wm9705->ac97);
0342     snd_soc_component_init_regmap(component, regmap);
0343 
0344     return 0;
0345 }
0346 
0347 static void wm9705_soc_remove(struct snd_soc_component *component)
0348 {
0349     struct wm9705_priv *wm9705 = snd_soc_component_get_drvdata(component);
0350 
0351     if (IS_ENABLED(CONFIG_SND_SOC_AC97_BUS) && !wm9705->mfd_pdata) {
0352         snd_soc_component_exit_regmap(component);
0353         snd_soc_free_ac97_component(wm9705->ac97);
0354     }
0355 }
0356 
0357 static const struct snd_soc_component_driver soc_component_dev_wm9705 = {
0358     .probe          = wm9705_soc_probe,
0359     .remove         = wm9705_soc_remove,
0360     .suspend        = wm9705_soc_suspend,
0361     .resume         = wm9705_soc_resume,
0362     .controls       = wm9705_snd_ac97_controls,
0363     .num_controls       = ARRAY_SIZE(wm9705_snd_ac97_controls),
0364     .dapm_widgets       = wm9705_dapm_widgets,
0365     .num_dapm_widgets   = ARRAY_SIZE(wm9705_dapm_widgets),
0366     .dapm_routes        = wm9705_audio_map,
0367     .num_dapm_routes    = ARRAY_SIZE(wm9705_audio_map),
0368     .idle_bias_on       = 1,
0369     .use_pmdown_time    = 1,
0370     .endianness     = 1,
0371 };
0372 
0373 static int wm9705_probe(struct platform_device *pdev)
0374 {
0375     struct wm9705_priv *wm9705;
0376 
0377     wm9705 = devm_kzalloc(&pdev->dev, sizeof(*wm9705), GFP_KERNEL);
0378     if (wm9705 == NULL)
0379         return -ENOMEM;
0380 
0381     wm9705->mfd_pdata = dev_get_platdata(&pdev->dev);
0382     platform_set_drvdata(pdev, wm9705);
0383 
0384     return devm_snd_soc_register_component(&pdev->dev,
0385             &soc_component_dev_wm9705, wm9705_dai, ARRAY_SIZE(wm9705_dai));
0386 }
0387 
0388 static struct platform_driver wm9705_codec_driver = {
0389     .driver = {
0390             .name = "wm9705-codec",
0391     },
0392 
0393     .probe = wm9705_probe,
0394 };
0395 
0396 module_platform_driver(wm9705_codec_driver);
0397 
0398 MODULE_DESCRIPTION("ASoC WM9705 driver");
0399 MODULE_AUTHOR("Ian Molton");
0400 MODULE_LICENSE("GPL v2");