Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * ad1980.c  --  ALSA Soc AD1980 codec support
0004  *
0005  * Copyright:   Analog Devices Inc.
0006  * Author:  Roy Huang <roy.huang@analog.com>
0007  *      Cliff Cai <cliff.cai@analog.com>
0008  */
0009 
0010 /*
0011  * WARNING:
0012  *
0013  * Because Analog Devices Inc. discontinued the ad1980 sound chip since
0014  * Sep. 2009, this ad1980 driver is not maintained, tested and supported
0015  * by ADI now.
0016  */
0017 
0018 #include <linux/init.h>
0019 #include <linux/slab.h>
0020 #include <linux/module.h>
0021 #include <linux/kernel.h>
0022 #include <linux/device.h>
0023 #include <linux/regmap.h>
0024 #include <sound/core.h>
0025 #include <sound/pcm.h>
0026 #include <sound/ac97_codec.h>
0027 #include <sound/initval.h>
0028 #include <sound/soc.h>
0029 
0030 static const struct reg_default ad1980_reg_defaults[] = {
0031     { 0x02, 0x8000 },
0032     { 0x04, 0x8000 },
0033     { 0x06, 0x8000 },
0034     { 0x0c, 0x8008 },
0035     { 0x0e, 0x8008 },
0036     { 0x10, 0x8808 },
0037     { 0x12, 0x8808 },
0038     { 0x16, 0x8808 },
0039     { 0x18, 0x8808 },
0040     { 0x1a, 0x0000 },
0041     { 0x1c, 0x8000 },
0042     { 0x20, 0x0000 },
0043     { 0x28, 0x03c7 },
0044     { 0x2c, 0xbb80 },
0045     { 0x2e, 0xbb80 },
0046     { 0x30, 0xbb80 },
0047     { 0x32, 0xbb80 },
0048     { 0x36, 0x8080 },
0049     { 0x38, 0x8080 },
0050     { 0x3a, 0x2000 },
0051     { 0x60, 0x0000 },
0052     { 0x62, 0x0000 },
0053     { 0x72, 0x0000 },
0054     { 0x74, 0x1001 },
0055     { 0x76, 0x0000 },
0056 };
0057 
0058 static bool ad1980_readable_reg(struct device *dev, unsigned int reg)
0059 {
0060     switch (reg) {
0061     case AC97_RESET ... AC97_MASTER_MONO:
0062     case AC97_PHONE ... AC97_CD:
0063     case AC97_AUX ... AC97_GENERAL_PURPOSE:
0064     case AC97_POWERDOWN ... AC97_PCM_LR_ADC_RATE:
0065     case AC97_SPDIF:
0066     case AC97_CODEC_CLASS_REV:
0067     case AC97_PCI_SVID:
0068     case AC97_AD_CODEC_CFG:
0069     case AC97_AD_JACK_SPDIF:
0070     case AC97_AD_SERIAL_CFG:
0071     case AC97_VENDOR_ID1:
0072     case AC97_VENDOR_ID2:
0073         return true;
0074     default:
0075         return false;
0076     }
0077 }
0078 
0079 static bool ad1980_writeable_reg(struct device *dev, unsigned int reg)
0080 {
0081     switch (reg) {
0082     case AC97_VENDOR_ID1:
0083     case AC97_VENDOR_ID2:
0084         return false;
0085     default:
0086         return ad1980_readable_reg(dev, reg);
0087     }
0088 }
0089 
0090 static const struct regmap_config ad1980_regmap_config = {
0091     .reg_bits = 16,
0092     .reg_stride = 2,
0093     .val_bits = 16,
0094     .max_register = 0x7e,
0095     .cache_type = REGCACHE_RBTREE,
0096 
0097     .volatile_reg = regmap_ac97_default_volatile,
0098     .readable_reg = ad1980_readable_reg,
0099     .writeable_reg = ad1980_writeable_reg,
0100 
0101     .reg_defaults = ad1980_reg_defaults,
0102     .num_reg_defaults = ARRAY_SIZE(ad1980_reg_defaults),
0103 };
0104 
0105 static const char *ad1980_rec_sel[] = {"Mic", "CD", "NC", "AUX", "Line",
0106         "Stereo Mix", "Mono Mix", "Phone"};
0107 
0108 static SOC_ENUM_DOUBLE_DECL(ad1980_cap_src,
0109                 AC97_REC_SEL, 8, 0, ad1980_rec_sel);
0110 
0111 static const struct snd_kcontrol_new ad1980_snd_ac97_controls[] = {
0112 SOC_DOUBLE("Master Playback Volume", AC97_MASTER, 8, 0, 31, 1),
0113 SOC_SINGLE("Master Playback Switch", AC97_MASTER, 15, 1, 1),
0114 
0115 SOC_DOUBLE("Headphone Playback Volume", AC97_HEADPHONE, 8, 0, 31, 1),
0116 SOC_SINGLE("Headphone Playback Switch", AC97_HEADPHONE, 15, 1, 1),
0117 
0118 SOC_DOUBLE("PCM Playback Volume", AC97_PCM, 8, 0, 31, 1),
0119 SOC_SINGLE("PCM Playback Switch", AC97_PCM, 15, 1, 1),
0120 
0121 SOC_DOUBLE("PCM Capture Volume", AC97_REC_GAIN, 8, 0, 31, 0),
0122 SOC_SINGLE("PCM Capture Switch", AC97_REC_GAIN, 15, 1, 1),
0123 
0124 SOC_SINGLE("Mono Playback Volume", AC97_MASTER_MONO, 0, 31, 1),
0125 SOC_SINGLE("Mono Playback Switch", AC97_MASTER_MONO, 15, 1, 1),
0126 
0127 SOC_SINGLE("Phone Capture Volume", AC97_PHONE, 0, 31, 1),
0128 SOC_SINGLE("Phone Capture Switch", AC97_PHONE, 15, 1, 1),
0129 
0130 SOC_SINGLE("Mic Volume", AC97_MIC, 0, 31, 1),
0131 SOC_SINGLE("Mic Switch", AC97_MIC, 15, 1, 1),
0132 
0133 SOC_SINGLE("Stereo Mic Switch", AC97_AD_MISC, 6, 1, 0),
0134 SOC_DOUBLE("Line HP Swap Switch", AC97_AD_MISC, 10, 5, 1, 0),
0135 
0136 SOC_DOUBLE("Surround Playback Volume", AC97_SURROUND_MASTER, 8, 0, 31, 1),
0137 SOC_DOUBLE("Surround Playback Switch", AC97_SURROUND_MASTER, 15, 7, 1, 1),
0138 
0139 SOC_DOUBLE("Center/LFE Playback Volume", AC97_CENTER_LFE_MASTER, 8, 0, 31, 1),
0140 SOC_DOUBLE("Center/LFE Playback Switch", AC97_CENTER_LFE_MASTER, 15, 7, 1, 1),
0141 
0142 SOC_ENUM("Capture Source", ad1980_cap_src),
0143 
0144 SOC_SINGLE("Mic Boost Switch", AC97_MIC, 6, 1, 0),
0145 };
0146 
0147 static const struct snd_soc_dapm_widget ad1980_dapm_widgets[] = {
0148 SND_SOC_DAPM_INPUT("MIC1"),
0149 SND_SOC_DAPM_INPUT("MIC2"),
0150 SND_SOC_DAPM_INPUT("CD_L"),
0151 SND_SOC_DAPM_INPUT("CD_R"),
0152 SND_SOC_DAPM_INPUT("AUX_L"),
0153 SND_SOC_DAPM_INPUT("AUX_R"),
0154 SND_SOC_DAPM_INPUT("LINE_IN_L"),
0155 SND_SOC_DAPM_INPUT("LINE_IN_R"),
0156 
0157 SND_SOC_DAPM_OUTPUT("LFE_OUT"),
0158 SND_SOC_DAPM_OUTPUT("CENTER_OUT"),
0159 SND_SOC_DAPM_OUTPUT("LINE_OUT_L"),
0160 SND_SOC_DAPM_OUTPUT("LINE_OUT_R"),
0161 SND_SOC_DAPM_OUTPUT("MONO_OUT"),
0162 SND_SOC_DAPM_OUTPUT("HP_OUT_L"),
0163 SND_SOC_DAPM_OUTPUT("HP_OUT_R"),
0164 };
0165 
0166 static const struct snd_soc_dapm_route ad1980_dapm_routes[] = {
0167     { "Capture", NULL, "MIC1" },
0168     { "Capture", NULL, "MIC2" },
0169     { "Capture", NULL, "CD_L" },
0170     { "Capture", NULL, "CD_R" },
0171     { "Capture", NULL, "AUX_L" },
0172     { "Capture", NULL, "AUX_R" },
0173     { "Capture", NULL, "LINE_IN_L" },
0174     { "Capture", NULL, "LINE_IN_R" },
0175 
0176     { "LFE_OUT", NULL, "Playback" },
0177     { "CENTER_OUT", NULL, "Playback" },
0178     { "LINE_OUT_L", NULL, "Playback" },
0179     { "LINE_OUT_R", NULL, "Playback" },
0180     { "MONO_OUT", NULL, "Playback" },
0181     { "HP_OUT_L", NULL, "Playback" },
0182     { "HP_OUT_R", NULL, "Playback" },
0183 };
0184 
0185 static struct snd_soc_dai_driver ad1980_dai = {
0186     .name = "ad1980-hifi",
0187     .playback = {
0188         .stream_name = "Playback",
0189         .channels_min = 2,
0190         .channels_max = 6,
0191         .rates = SNDRV_PCM_RATE_48000,
0192         .formats = SND_SOC_STD_AC97_FMTS, },
0193     .capture = {
0194         .stream_name = "Capture",
0195         .channels_min = 2,
0196         .channels_max = 2,
0197         .rates = SNDRV_PCM_RATE_48000,
0198         .formats = SND_SOC_STD_AC97_FMTS, },
0199 };
0200 
0201 #define AD1980_VENDOR_ID 0x41445300
0202 #define AD1980_VENDOR_MASK 0xffffff00
0203 
0204 static int ad1980_reset(struct snd_soc_component *component, int try_warm)
0205 {
0206     struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);
0207     unsigned int retry_cnt = 0;
0208     int ret;
0209 
0210     do {
0211         ret = snd_ac97_reset(ac97, true, AD1980_VENDOR_ID,
0212             AD1980_VENDOR_MASK);
0213         if (ret >= 0)
0214             return 0;
0215 
0216         /*
0217          * Set bit 16slot in register 74h, then every slot will has only
0218          * 16 bits. This command is sent out in 20bit mode, in which
0219          * case the first nibble of data is eaten by the addr. (Tag is
0220          * always 16 bit)
0221          */
0222         snd_soc_component_write(component, AC97_AD_SERIAL_CFG, 0x9900);
0223 
0224     } while (retry_cnt++ < 10);
0225 
0226     dev_err(component->dev, "Failed to reset: AC97 link error\n");
0227 
0228     return -EIO;
0229 }
0230 
0231 static int ad1980_soc_probe(struct snd_soc_component *component)
0232 {
0233     struct snd_ac97 *ac97;
0234     struct regmap *regmap;
0235     int ret;
0236     u16 vendor_id2;
0237     u16 ext_status;
0238 
0239     ac97 = snd_soc_new_ac97_component(component, 0, 0);
0240     if (IS_ERR(ac97)) {
0241         ret = PTR_ERR(ac97);
0242         dev_err(component->dev, "Failed to register AC97 component: %d\n", ret);
0243         return ret;
0244     }
0245 
0246     regmap = regmap_init_ac97(ac97, &ad1980_regmap_config);
0247     if (IS_ERR(regmap)) {
0248         ret = PTR_ERR(regmap);
0249         goto err_free_ac97;
0250     }
0251 
0252     snd_soc_component_init_regmap(component, regmap);
0253     snd_soc_component_set_drvdata(component, ac97);
0254 
0255     ret = ad1980_reset(component, 0);
0256     if (ret < 0)
0257         goto reset_err;
0258 
0259     vendor_id2 = snd_soc_component_read(component, AC97_VENDOR_ID2);
0260     if (vendor_id2 == 0x5374) {
0261         dev_warn(component->dev,
0262             "Found AD1981 - only 2/2 IN/OUT Channels supported\n");
0263     }
0264 
0265     /* unmute captures and playbacks volume */
0266     snd_soc_component_write(component, AC97_MASTER, 0x0000);
0267     snd_soc_component_write(component, AC97_PCM, 0x0000);
0268     snd_soc_component_write(component, AC97_REC_GAIN, 0x0000);
0269     snd_soc_component_write(component, AC97_CENTER_LFE_MASTER, 0x0000);
0270     snd_soc_component_write(component, AC97_SURROUND_MASTER, 0x0000);
0271 
0272     /*power on LFE/CENTER/Surround DACs*/
0273     ext_status = snd_soc_component_read(component, AC97_EXTENDED_STATUS);
0274     snd_soc_component_write(component, AC97_EXTENDED_STATUS, ext_status&~0x3800);
0275 
0276     return 0;
0277 
0278 reset_err:
0279     snd_soc_component_exit_regmap(component);
0280 err_free_ac97:
0281     snd_soc_free_ac97_component(ac97);
0282     return ret;
0283 }
0284 
0285 static void ad1980_soc_remove(struct snd_soc_component *component)
0286 {
0287     struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);
0288 
0289     snd_soc_component_exit_regmap(component);
0290     snd_soc_free_ac97_component(ac97);
0291 }
0292 
0293 static const struct snd_soc_component_driver soc_component_dev_ad1980 = {
0294     .probe          = ad1980_soc_probe,
0295     .remove         = ad1980_soc_remove,
0296     .controls       = ad1980_snd_ac97_controls,
0297     .num_controls       = ARRAY_SIZE(ad1980_snd_ac97_controls),
0298     .dapm_widgets       = ad1980_dapm_widgets,
0299     .num_dapm_widgets   = ARRAY_SIZE(ad1980_dapm_widgets),
0300     .dapm_routes        = ad1980_dapm_routes,
0301     .num_dapm_routes    = ARRAY_SIZE(ad1980_dapm_routes),
0302     .idle_bias_on       = 1,
0303     .use_pmdown_time    = 1,
0304     .endianness     = 1,
0305 };
0306 
0307 static int ad1980_probe(struct platform_device *pdev)
0308 {
0309     return devm_snd_soc_register_component(&pdev->dev,
0310             &soc_component_dev_ad1980, &ad1980_dai, 1);
0311 }
0312 
0313 static struct platform_driver ad1980_codec_driver = {
0314     .driver = {
0315             .name = "ad1980",
0316     },
0317 
0318     .probe = ad1980_probe,
0319 };
0320 
0321 module_platform_driver(ad1980_codec_driver);
0322 
0323 MODULE_DESCRIPTION("ASoC ad1980 driver (Obsolete)");
0324 MODULE_AUTHOR("Roy Huang, Cliff Cai");
0325 MODULE_LICENSE("GPL");