Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * stac9766.c  --  ALSA SoC STAC9766 codec support
0004  *
0005  * Copyright 2009 Jon Smirl, Digispeaker
0006  * Author: Jon Smirl <jonsmirl@gmail.com>
0007  *
0008  *  Features:-
0009  *
0010  *   o Support for AC97 Codec, S/PDIF
0011  */
0012 
0013 #include <linux/init.h>
0014 #include <linux/slab.h>
0015 #include <linux/module.h>
0016 #include <linux/device.h>
0017 #include <linux/regmap.h>
0018 #include <sound/core.h>
0019 #include <sound/pcm.h>
0020 #include <sound/ac97_codec.h>
0021 #include <sound/initval.h>
0022 #include <sound/pcm_params.h>
0023 #include <sound/soc.h>
0024 #include <sound/tlv.h>
0025 
0026 #define STAC9766_VENDOR_ID 0x83847666
0027 #define STAC9766_VENDOR_ID_MASK 0xffffffff
0028 
0029 #define AC97_STAC_DA_CONTROL 0x6A
0030 #define AC97_STAC_ANALOG_SPECIAL 0x6E
0031 #define AC97_STAC_STEREO_MIC 0x78
0032 
0033 static const struct reg_default stac9766_reg_defaults[] = {
0034     { 0x02, 0x8000 },
0035     { 0x04, 0x8000 },
0036     { 0x06, 0x8000 },
0037     { 0x0a, 0x0000 },
0038     { 0x0c, 0x8008 },
0039     { 0x0e, 0x8008 },
0040     { 0x10, 0x8808 },
0041     { 0x12, 0x8808 },
0042     { 0x14, 0x8808 },
0043     { 0x16, 0x8808 },
0044     { 0x18, 0x8808 },
0045     { 0x1a, 0x0000 },
0046     { 0x1c, 0x8000 },
0047     { 0x20, 0x0000 },
0048     { 0x22, 0x0000 },
0049     { 0x28, 0x0a05 },
0050     { 0x2c, 0xbb80 },
0051     { 0x32, 0xbb80 },
0052     { 0x3a, 0x2000 },
0053     { 0x3e, 0x0100 },
0054     { 0x4c, 0x0300 },
0055     { 0x4e, 0xffff },
0056     { 0x50, 0x0000 },
0057     { 0x52, 0x0000 },
0058     { 0x54, 0x0000 },
0059     { 0x6a, 0x0000 },
0060     { 0x6e, 0x1000 },
0061     { 0x72, 0x0000 },
0062     { 0x78, 0x0000 },
0063 };
0064 
0065 static const struct regmap_config stac9766_regmap_config = {
0066     .reg_bits = 16,
0067     .reg_stride = 2,
0068     .val_bits = 16,
0069     .max_register = 0x78,
0070     .cache_type = REGCACHE_RBTREE,
0071 
0072     .volatile_reg = regmap_ac97_default_volatile,
0073 
0074     .reg_defaults = stac9766_reg_defaults,
0075     .num_reg_defaults = ARRAY_SIZE(stac9766_reg_defaults),
0076 };
0077 
0078 static const char *stac9766_record_mux[] = {"Mic", "CD", "Video", "AUX",
0079             "Line", "Stereo Mix", "Mono Mix", "Phone"};
0080 static const char *stac9766_mono_mux[] = {"Mix", "Mic"};
0081 static const char *stac9766_mic_mux[] = {"Mic1", "Mic2"};
0082 static const char *stac9766_SPDIF_mux[] = {"PCM", "ADC Record"};
0083 static const char *stac9766_popbypass_mux[] = {"Normal", "Bypass Mixer"};
0084 static const char *stac9766_record_all_mux[] = {"All analog",
0085     "Analog plus DAC"};
0086 static const char *stac9766_boost1[] = {"0dB", "10dB"};
0087 static const char *stac9766_boost2[] = {"0dB", "20dB"};
0088 static const char *stac9766_stereo_mic[] = {"Off", "On"};
0089 
0090 static SOC_ENUM_DOUBLE_DECL(stac9766_record_enum,
0091                 AC97_REC_SEL, 8, 0, stac9766_record_mux);
0092 static SOC_ENUM_SINGLE_DECL(stac9766_mono_enum,
0093                 AC97_GENERAL_PURPOSE, 9, stac9766_mono_mux);
0094 static SOC_ENUM_SINGLE_DECL(stac9766_mic_enum,
0095                 AC97_GENERAL_PURPOSE, 8, stac9766_mic_mux);
0096 static SOC_ENUM_SINGLE_DECL(stac9766_SPDIF_enum,
0097                 AC97_STAC_DA_CONTROL, 1, stac9766_SPDIF_mux);
0098 static SOC_ENUM_SINGLE_DECL(stac9766_popbypass_enum,
0099                 AC97_GENERAL_PURPOSE, 15, stac9766_popbypass_mux);
0100 static SOC_ENUM_SINGLE_DECL(stac9766_record_all_enum,
0101                 AC97_STAC_ANALOG_SPECIAL, 12,
0102                 stac9766_record_all_mux);
0103 static SOC_ENUM_SINGLE_DECL(stac9766_boost1_enum,
0104                 AC97_MIC, 6, stac9766_boost1); /* 0/10dB */
0105 static SOC_ENUM_SINGLE_DECL(stac9766_boost2_enum,
0106                 AC97_STAC_ANALOG_SPECIAL, 2, stac9766_boost2); /* 0/20dB */
0107 static SOC_ENUM_SINGLE_DECL(stac9766_stereo_mic_enum,
0108                 AC97_STAC_STEREO_MIC, 2, stac9766_stereo_mic);
0109 
0110 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(master_tlv, -4650, 150, 0);
0111 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(record_tlv,     0, 150, 0);
0112 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(beep_tlv,   -4500, 300, 0);
0113 static const SNDRV_CTL_TLVD_DECLARE_DB_SCALE(mix_tlv,    -3450, 150, 0);
0114 
0115 static const struct snd_kcontrol_new stac9766_snd_ac97_controls[] = {
0116     SOC_DOUBLE_TLV("Speaker Volume", AC97_MASTER, 8, 0, 31, 1, master_tlv),
0117     SOC_SINGLE("Speaker Switch", AC97_MASTER, 15, 1, 1),
0118     SOC_DOUBLE_TLV("Headphone Volume", AC97_HEADPHONE, 8, 0, 31, 1,
0119                master_tlv),
0120     SOC_SINGLE("Headphone Switch", AC97_HEADPHONE, 15, 1, 1),
0121     SOC_SINGLE_TLV("Mono Out Volume", AC97_MASTER_MONO, 0, 31, 1,
0122                master_tlv),
0123     SOC_SINGLE("Mono Out Switch", AC97_MASTER_MONO, 15, 1, 1),
0124 
0125     SOC_DOUBLE_TLV("Record Volume", AC97_REC_GAIN, 8, 0, 15, 0, record_tlv),
0126     SOC_SINGLE("Record Switch", AC97_REC_GAIN, 15, 1, 1),
0127 
0128 
0129     SOC_SINGLE_TLV("Beep Volume", AC97_PC_BEEP, 1, 15, 1, beep_tlv),
0130     SOC_SINGLE("Beep Switch", AC97_PC_BEEP, 15, 1, 1),
0131     SOC_SINGLE("Beep Frequency", AC97_PC_BEEP, 5, 127, 1),
0132     SOC_SINGLE_TLV("Phone Volume", AC97_PHONE, 0, 31, 1, mix_tlv),
0133     SOC_SINGLE("Phone Switch", AC97_PHONE, 15, 1, 1),
0134 
0135     SOC_ENUM("Mic Boost1", stac9766_boost1_enum),
0136     SOC_ENUM("Mic Boost2", stac9766_boost2_enum),
0137     SOC_SINGLE_TLV("Mic Volume", AC97_MIC, 0, 31, 1, mix_tlv),
0138     SOC_SINGLE("Mic Switch", AC97_MIC, 15, 1, 1),
0139     SOC_ENUM("Stereo Mic", stac9766_stereo_mic_enum),
0140 
0141     SOC_DOUBLE_TLV("Line Volume", AC97_LINE, 8, 0, 31, 1, mix_tlv),
0142     SOC_SINGLE("Line Switch", AC97_LINE, 15, 1, 1),
0143     SOC_DOUBLE_TLV("CD Volume", AC97_CD, 8, 0, 31, 1, mix_tlv),
0144     SOC_SINGLE("CD Switch", AC97_CD, 15, 1, 1),
0145     SOC_DOUBLE_TLV("AUX Volume", AC97_AUX, 8, 0, 31, 1, mix_tlv),
0146     SOC_SINGLE("AUX Switch", AC97_AUX, 15, 1, 1),
0147     SOC_DOUBLE_TLV("Video Volume", AC97_VIDEO, 8, 0, 31, 1, mix_tlv),
0148     SOC_SINGLE("Video Switch", AC97_VIDEO, 15, 1, 1),
0149 
0150     SOC_DOUBLE_TLV("DAC Volume", AC97_PCM, 8, 0, 31, 1, mix_tlv),
0151     SOC_SINGLE("DAC Switch", AC97_PCM, 15, 1, 1),
0152     SOC_SINGLE("Loopback Test Switch", AC97_GENERAL_PURPOSE, 7, 1, 0),
0153     SOC_SINGLE("3D Volume", AC97_3D_CONTROL, 3, 2, 1),
0154     SOC_SINGLE("3D Switch", AC97_GENERAL_PURPOSE, 13, 1, 0),
0155 
0156     SOC_ENUM("SPDIF Mux", stac9766_SPDIF_enum),
0157     SOC_ENUM("Mic1/2 Mux", stac9766_mic_enum),
0158     SOC_ENUM("Record All Mux", stac9766_record_all_enum),
0159     SOC_ENUM("Record Mux", stac9766_record_enum),
0160     SOC_ENUM("Mono Mux", stac9766_mono_enum),
0161     SOC_ENUM("Pop Bypass Mux", stac9766_popbypass_enum),
0162 };
0163 
0164 static int ac97_analog_prepare(struct snd_pcm_substream *substream,
0165                    struct snd_soc_dai *dai)
0166 {
0167     struct snd_soc_component *component = dai->component;
0168     struct snd_pcm_runtime *runtime = substream->runtime;
0169     unsigned short reg;
0170 
0171     /* enable variable rate audio, disable SPDIF output */
0172     snd_soc_component_update_bits(component, AC97_EXTENDED_STATUS, 0x5, 0x1);
0173 
0174     if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK)
0175         reg = AC97_PCM_FRONT_DAC_RATE;
0176     else
0177         reg = AC97_PCM_LR_ADC_RATE;
0178 
0179     return snd_soc_component_write(component, reg, runtime->rate);
0180 }
0181 
0182 static int ac97_digital_prepare(struct snd_pcm_substream *substream,
0183                 struct snd_soc_dai *dai)
0184 {
0185     struct snd_soc_component *component = dai->component;
0186     struct snd_pcm_runtime *runtime = substream->runtime;
0187     unsigned short reg;
0188 
0189     snd_soc_component_write(component, AC97_SPDIF, 0x2002);
0190 
0191     /* Enable VRA and SPDIF out */
0192     snd_soc_component_update_bits(component, AC97_EXTENDED_STATUS, 0x5, 0x5);
0193 
0194     reg = AC97_PCM_FRONT_DAC_RATE;
0195 
0196     return snd_soc_component_write(component, reg, runtime->rate);
0197 }
0198 
0199 static int stac9766_set_bias_level(struct snd_soc_component *component,
0200                    enum snd_soc_bias_level level)
0201 {
0202     switch (level) {
0203     case SND_SOC_BIAS_ON: /* full On */
0204     case SND_SOC_BIAS_PREPARE: /* partial On */
0205     case SND_SOC_BIAS_STANDBY: /* Off, with power */
0206         snd_soc_component_write(component, AC97_POWERDOWN, 0x0000);
0207         break;
0208     case SND_SOC_BIAS_OFF: /* Off, without power */
0209         /* disable everything including AC link */
0210         snd_soc_component_write(component, AC97_POWERDOWN, 0xffff);
0211         break;
0212     }
0213     return 0;
0214 }
0215 
0216 static int stac9766_component_resume(struct snd_soc_component *component)
0217 {
0218     struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);
0219 
0220     return snd_ac97_reset(ac97, true, STAC9766_VENDOR_ID,
0221         STAC9766_VENDOR_ID_MASK);
0222 }
0223 
0224 static const struct snd_soc_dai_ops stac9766_dai_ops_analog = {
0225     .prepare = ac97_analog_prepare,
0226 };
0227 
0228 static const struct snd_soc_dai_ops stac9766_dai_ops_digital = {
0229     .prepare = ac97_digital_prepare,
0230 };
0231 
0232 static struct snd_soc_dai_driver stac9766_dai[] = {
0233 {
0234     .name = "stac9766-hifi-analog",
0235 
0236     /* stream cababilities */
0237     .playback = {
0238         .stream_name = "stac9766 analog",
0239         .channels_min = 1,
0240         .channels_max = 2,
0241         .rates = SNDRV_PCM_RATE_8000_48000,
0242         .formats = SND_SOC_STD_AC97_FMTS,
0243     },
0244     .capture = {
0245         .stream_name = "stac9766 analog",
0246         .channels_min = 1,
0247         .channels_max = 2,
0248         .rates = SNDRV_PCM_RATE_8000_48000,
0249         .formats = SND_SOC_STD_AC97_FMTS,
0250     },
0251     /* alsa ops */
0252     .ops = &stac9766_dai_ops_analog,
0253 },
0254 {
0255     .name = "stac9766-hifi-IEC958",
0256 
0257     /* stream cababilities */
0258     .playback = {
0259         .stream_name = "stac9766 IEC958",
0260         .channels_min = 1,
0261         .channels_max = 2,
0262         .rates = SNDRV_PCM_RATE_32000 | \
0263             SNDRV_PCM_RATE_44100 | SNDRV_PCM_RATE_48000,
0264         .formats = SNDRV_PCM_FMTBIT_IEC958_SUBFRAME_BE,
0265     },
0266     /* alsa ops */
0267     .ops = &stac9766_dai_ops_digital,
0268 }
0269 };
0270 
0271 static int stac9766_component_probe(struct snd_soc_component *component)
0272 {
0273     struct snd_ac97 *ac97;
0274     struct regmap *regmap;
0275     int ret;
0276 
0277     ac97 = snd_soc_new_ac97_component(component, STAC9766_VENDOR_ID,
0278             STAC9766_VENDOR_ID_MASK);
0279     if (IS_ERR(ac97))
0280         return PTR_ERR(ac97);
0281 
0282     regmap = regmap_init_ac97(ac97, &stac9766_regmap_config);
0283     if (IS_ERR(regmap)) {
0284         ret = PTR_ERR(regmap);
0285         goto err_free_ac97;
0286     }
0287 
0288     snd_soc_component_init_regmap(component, regmap);
0289     snd_soc_component_set_drvdata(component, ac97);
0290 
0291     return 0;
0292 err_free_ac97:
0293     snd_soc_free_ac97_component(ac97);
0294     return ret;
0295 }
0296 
0297 static void stac9766_component_remove(struct snd_soc_component *component)
0298 {
0299     struct snd_ac97 *ac97 = snd_soc_component_get_drvdata(component);
0300 
0301     snd_soc_component_exit_regmap(component);
0302     snd_soc_free_ac97_component(ac97);
0303 }
0304 
0305 static const struct snd_soc_component_driver soc_component_dev_stac9766 = {
0306     .controls       = stac9766_snd_ac97_controls,
0307     .num_controls       = ARRAY_SIZE(stac9766_snd_ac97_controls),
0308     .set_bias_level     = stac9766_set_bias_level,
0309     .probe          = stac9766_component_probe,
0310     .remove         = stac9766_component_remove,
0311     .resume         = stac9766_component_resume,
0312     .suspend_bias_off   = 1,
0313     .idle_bias_on       = 1,
0314     .use_pmdown_time    = 1,
0315     .endianness     = 1,
0316 };
0317 
0318 static int stac9766_probe(struct platform_device *pdev)
0319 {
0320     return devm_snd_soc_register_component(&pdev->dev,
0321             &soc_component_dev_stac9766, stac9766_dai, ARRAY_SIZE(stac9766_dai));
0322 }
0323 
0324 static struct platform_driver stac9766_codec_driver = {
0325     .driver = {
0326             .name = "stac9766-codec",
0327     },
0328 
0329     .probe = stac9766_probe,
0330 };
0331 
0332 module_platform_driver(stac9766_codec_driver);
0333 
0334 MODULE_DESCRIPTION("ASoC stac9766 driver");
0335 MODULE_AUTHOR("Jon Smirl <jonsmirl@gmail.com>");
0336 MODULE_LICENSE("GPL");