Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 //
0003 // Copyright (c) 2020 BayLibre, SAS.
0004 // Author: Jerome Brunet <jbrunet@baylibre.com>
0005 
0006 #include <linux/clk.h>
0007 #include <linux/delay.h>
0008 #include <linux/module.h>
0009 #include <linux/regmap.h>
0010 #include <linux/regulator/consumer.h>
0011 #include <linux/reset.h>
0012 #include <sound/soc.h>
0013 #include <sound/tlv.h>
0014 
0015 #define BLOCK_EN    0x00
0016 #define  LORN_EN    0
0017 #define  LORP_EN    1
0018 #define  LOLN_EN    2
0019 #define  LOLP_EN    3
0020 #define  DACR_EN    4
0021 #define  DACL_EN    5
0022 #define  DACR_INV   20
0023 #define  DACL_INV   21
0024 #define  DACR_SRC   22
0025 #define  DACL_SRC   23
0026 #define  REFP_BUF_EN    BIT(12)
0027 #define  BIAS_CURRENT_EN BIT(13)
0028 #define  VMID_GEN_FAST  BIT(14)
0029 #define  VMID_GEN_EN    BIT(15)
0030 #define  I2S_MODE   BIT(30)
0031 #define VOL_CTRL0   0x04
0032 #define  GAIN_H     31
0033 #define  GAIN_L     23
0034 #define VOL_CTRL1   0x08
0035 #define  DAC_MONO   8
0036 #define  RAMP_RATE  10
0037 #define  VC_RAMP_MODE   12
0038 #define  MUTE_MODE  13
0039 #define  UNMUTE_MODE    14
0040 #define  DAC_SOFT_MUTE  15
0041 #define  DACR_VC    16
0042 #define  DACL_VC    24
0043 #define LINEOUT_CFG 0x0c
0044 #define  LORN_POL   0
0045 #define  LORP_POL   4
0046 #define  LOLN_POL   8
0047 #define  LOLP_POL   12
0048 #define POWER_CFG   0x10
0049 
0050 struct t9015 {
0051     struct clk *pclk;
0052     struct regulator *avdd;
0053 };
0054 
0055 static int t9015_dai_set_fmt(struct snd_soc_dai *dai, unsigned int fmt)
0056 {
0057     struct snd_soc_component *component = dai->component;
0058     unsigned int val;
0059 
0060     switch (fmt & SND_SOC_DAIFMT_MASTER_MASK) {
0061     case SND_SOC_DAIFMT_CBM_CFM:
0062         val = I2S_MODE;
0063         break;
0064 
0065     case SND_SOC_DAIFMT_CBS_CFS:
0066         val = 0;
0067         break;
0068 
0069     default:
0070         return -EINVAL;
0071     }
0072 
0073     snd_soc_component_update_bits(component, BLOCK_EN, I2S_MODE, val);
0074 
0075     if (((fmt & SND_SOC_DAIFMT_FORMAT_MASK) != SND_SOC_DAIFMT_I2S) &&
0076         ((fmt & SND_SOC_DAIFMT_FORMAT_MASK) != SND_SOC_DAIFMT_LEFT_J))
0077         return -EINVAL;
0078 
0079     return 0;
0080 }
0081 
0082 static const struct snd_soc_dai_ops t9015_dai_ops = {
0083     .set_fmt = t9015_dai_set_fmt,
0084 };
0085 
0086 static struct snd_soc_dai_driver t9015_dai = {
0087     .name = "t9015-hifi",
0088     .playback = {
0089         .stream_name = "Playback",
0090         .channels_min = 1,
0091         .channels_max = 2,
0092         .rates = SNDRV_PCM_RATE_8000_96000,
0093         .formats = (SNDRV_PCM_FMTBIT_S8 |
0094                 SNDRV_PCM_FMTBIT_S16_LE |
0095                 SNDRV_PCM_FMTBIT_S20_LE |
0096                 SNDRV_PCM_FMTBIT_S24_LE),
0097     },
0098     .ops = &t9015_dai_ops,
0099 };
0100 
0101 static const DECLARE_TLV_DB_MINMAX_MUTE(dac_vol_tlv, -9525, 0);
0102 
0103 static const char * const ramp_rate_txt[] = { "Fast", "Slow" };
0104 static SOC_ENUM_SINGLE_DECL(ramp_rate_enum, VOL_CTRL1, RAMP_RATE,
0105                 ramp_rate_txt);
0106 
0107 static const char * const dacr_in_txt[] = { "Right", "Left" };
0108 static SOC_ENUM_SINGLE_DECL(dacr_in_enum, BLOCK_EN, DACR_SRC, dacr_in_txt);
0109 
0110 static const char * const dacl_in_txt[] = { "Left", "Right" };
0111 static SOC_ENUM_SINGLE_DECL(dacl_in_enum, BLOCK_EN, DACL_SRC, dacl_in_txt);
0112 
0113 static const char * const mono_txt[] = { "Stereo", "Mono"};
0114 static SOC_ENUM_SINGLE_DECL(mono_enum, VOL_CTRL1, DAC_MONO, mono_txt);
0115 
0116 static const struct snd_kcontrol_new t9015_snd_controls[] = {
0117     /* Volume Controls */
0118     SOC_ENUM("Playback Channel Mode", mono_enum),
0119     SOC_SINGLE("Playback Switch", VOL_CTRL1, DAC_SOFT_MUTE, 1, 1),
0120     SOC_DOUBLE_TLV("Playback Volume", VOL_CTRL1, DACL_VC, DACR_VC,
0121                0xff, 0, dac_vol_tlv),
0122 
0123     /* Ramp Controls */
0124     SOC_ENUM("Ramp Rate", ramp_rate_enum),
0125     SOC_SINGLE("Volume Ramp Switch", VOL_CTRL1, VC_RAMP_MODE, 1, 0),
0126     SOC_SINGLE("Mute Ramp Switch", VOL_CTRL1, MUTE_MODE, 1, 0),
0127     SOC_SINGLE("Unmute Ramp Switch", VOL_CTRL1, UNMUTE_MODE, 1, 0),
0128 };
0129 
0130 static const struct snd_kcontrol_new t9015_right_dac_mux =
0131     SOC_DAPM_ENUM("Right DAC Source", dacr_in_enum);
0132 static const struct snd_kcontrol_new t9015_left_dac_mux =
0133     SOC_DAPM_ENUM("Left DAC Source", dacl_in_enum);
0134 
0135 static const struct snd_soc_dapm_widget t9015_dapm_widgets[] = {
0136     SND_SOC_DAPM_AIF_IN("Right IN", NULL, 0, SND_SOC_NOPM, 0, 0),
0137     SND_SOC_DAPM_AIF_IN("Left IN", NULL, 0, SND_SOC_NOPM, 0, 0),
0138     SND_SOC_DAPM_MUX("Right DAC Sel", SND_SOC_NOPM, 0, 0,
0139              &t9015_right_dac_mux),
0140     SND_SOC_DAPM_MUX("Left DAC Sel", SND_SOC_NOPM, 0, 0,
0141              &t9015_left_dac_mux),
0142     SND_SOC_DAPM_DAC("Right DAC", NULL, BLOCK_EN, DACR_EN, 0),
0143     SND_SOC_DAPM_DAC("Left DAC",  NULL, BLOCK_EN, DACL_EN, 0),
0144     SND_SOC_DAPM_OUT_DRV("Right- Driver", BLOCK_EN, LORN_EN, 0,
0145              NULL, 0),
0146     SND_SOC_DAPM_OUT_DRV("Right+ Driver", BLOCK_EN, LORP_EN, 0,
0147              NULL, 0),
0148     SND_SOC_DAPM_OUT_DRV("Left- Driver",  BLOCK_EN, LOLN_EN, 0,
0149              NULL, 0),
0150     SND_SOC_DAPM_OUT_DRV("Left+ Driver",  BLOCK_EN, LOLP_EN, 0,
0151              NULL, 0),
0152     SND_SOC_DAPM_OUTPUT("LORN"),
0153     SND_SOC_DAPM_OUTPUT("LORP"),
0154     SND_SOC_DAPM_OUTPUT("LOLN"),
0155     SND_SOC_DAPM_OUTPUT("LOLP"),
0156 };
0157 
0158 static const struct snd_soc_dapm_route t9015_dapm_routes[] = {
0159     { "Right IN", NULL, "Playback" },
0160     { "Left IN",  NULL, "Playback" },
0161     { "Right DAC Sel", "Right", "Right IN" },
0162     { "Right DAC Sel", "Left",  "Left IN" },
0163     { "Left DAC Sel",  "Right", "Right IN" },
0164     { "Left DAC Sel",  "Left",  "Left IN" },
0165     { "Right DAC", NULL, "Right DAC Sel" },
0166     { "Left DAC",  NULL, "Left DAC Sel" },
0167     { "Right- Driver", NULL, "Right DAC" },
0168     { "Right+ Driver", NULL, "Right DAC" },
0169     { "Left- Driver",  NULL, "Left DAC"  },
0170     { "Left+ Driver",  NULL, "Left DAC"  },
0171     { "LORN", NULL, "Right- Driver", },
0172     { "LORP", NULL, "Right+ Driver", },
0173     { "LOLN", NULL, "Left- Driver",  },
0174     { "LOLP", NULL, "Left+ Driver",  },
0175 };
0176 
0177 static int t9015_set_bias_level(struct snd_soc_component *component,
0178                 enum snd_soc_bias_level level)
0179 {
0180     struct t9015 *priv = snd_soc_component_get_drvdata(component);
0181     enum snd_soc_bias_level now =
0182         snd_soc_component_get_bias_level(component);
0183     int ret;
0184 
0185     switch (level) {
0186     case SND_SOC_BIAS_ON:
0187         snd_soc_component_update_bits(component, BLOCK_EN,
0188                           BIAS_CURRENT_EN,
0189                           BIAS_CURRENT_EN);
0190         break;
0191     case SND_SOC_BIAS_PREPARE:
0192         snd_soc_component_update_bits(component, BLOCK_EN,
0193                           BIAS_CURRENT_EN,
0194                           0);
0195         break;
0196     case SND_SOC_BIAS_STANDBY:
0197         ret = regulator_enable(priv->avdd);
0198         if (ret) {
0199             dev_err(component->dev, "AVDD enable failed\n");
0200             return ret;
0201         }
0202 
0203         if (now == SND_SOC_BIAS_OFF) {
0204             snd_soc_component_update_bits(component, BLOCK_EN,
0205                 VMID_GEN_EN | VMID_GEN_FAST | REFP_BUF_EN,
0206                 VMID_GEN_EN | VMID_GEN_FAST | REFP_BUF_EN);
0207 
0208             mdelay(200);
0209             snd_soc_component_update_bits(component, BLOCK_EN,
0210                               VMID_GEN_FAST,
0211                               0);
0212         }
0213 
0214         break;
0215     case SND_SOC_BIAS_OFF:
0216         snd_soc_component_update_bits(component, BLOCK_EN,
0217             VMID_GEN_EN | VMID_GEN_FAST | REFP_BUF_EN,
0218             0);
0219 
0220         regulator_disable(priv->avdd);
0221         break;
0222     }
0223 
0224     return 0;
0225 }
0226 
0227 static const struct snd_soc_component_driver t9015_codec_driver = {
0228     .set_bias_level     = t9015_set_bias_level,
0229     .controls       = t9015_snd_controls,
0230     .num_controls       = ARRAY_SIZE(t9015_snd_controls),
0231     .dapm_widgets       = t9015_dapm_widgets,
0232     .num_dapm_widgets   = ARRAY_SIZE(t9015_dapm_widgets),
0233     .dapm_routes        = t9015_dapm_routes,
0234     .num_dapm_routes    = ARRAY_SIZE(t9015_dapm_routes),
0235     .suspend_bias_off   = 1,
0236     .endianness     = 1,
0237 };
0238 
0239 static const struct regmap_config t9015_regmap_config = {
0240     .reg_bits       = 32,
0241     .reg_stride     = 4,
0242     .val_bits       = 32,
0243     .max_register       = POWER_CFG,
0244 };
0245 
0246 static int t9015_probe(struct platform_device *pdev)
0247 {
0248     struct device *dev = &pdev->dev;
0249     struct t9015 *priv;
0250     void __iomem *regs;
0251     struct regmap *regmap;
0252     int ret;
0253 
0254     priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
0255     if (!priv)
0256         return -ENOMEM;
0257     platform_set_drvdata(pdev, priv);
0258 
0259     priv->pclk = devm_clk_get(dev, "pclk");
0260     if (IS_ERR(priv->pclk))
0261         return dev_err_probe(dev, PTR_ERR(priv->pclk), "failed to get core clock\n");
0262 
0263     priv->avdd = devm_regulator_get(dev, "AVDD");
0264     if (IS_ERR(priv->avdd))
0265         return dev_err_probe(dev, PTR_ERR(priv->avdd), "failed to AVDD\n");
0266 
0267     ret = clk_prepare_enable(priv->pclk);
0268     if (ret) {
0269         dev_err(dev, "core clock enable failed\n");
0270         return ret;
0271     }
0272 
0273     ret = devm_add_action_or_reset(dev,
0274             (void(*)(void *))clk_disable_unprepare,
0275             priv->pclk);
0276     if (ret)
0277         return ret;
0278 
0279     ret = device_reset(dev);
0280     if (ret) {
0281         dev_err(dev, "reset failed\n");
0282         return ret;
0283     }
0284 
0285     regs = devm_platform_ioremap_resource(pdev, 0);
0286     if (IS_ERR(regs)) {
0287         dev_err(dev, "register map failed\n");
0288         return PTR_ERR(regs);
0289     }
0290 
0291     regmap = devm_regmap_init_mmio(dev, regs, &t9015_regmap_config);
0292     if (IS_ERR(regmap)) {
0293         dev_err(dev, "regmap init failed\n");
0294         return PTR_ERR(regmap);
0295     }
0296 
0297     /*
0298      * Initialize output polarity:
0299      * ATM the output polarity is fixed but in the future it might useful
0300      * to add DT property to set this depending on the platform needs
0301      */
0302     regmap_write(regmap, LINEOUT_CFG, 0x1111);
0303 
0304     return devm_snd_soc_register_component(dev, &t9015_codec_driver,
0305                            &t9015_dai, 1);
0306 }
0307 
0308 static const struct of_device_id t9015_ids[] __maybe_unused = {
0309     { .compatible = "amlogic,t9015", },
0310     { }
0311 };
0312 MODULE_DEVICE_TABLE(of, t9015_ids);
0313 
0314 static struct platform_driver t9015_driver = {
0315     .driver = {
0316         .name = "t9015-codec",
0317         .of_match_table = of_match_ptr(t9015_ids),
0318     },
0319     .probe = t9015_probe,
0320 };
0321 
0322 module_platform_driver(t9015_driver);
0323 
0324 MODULE_DESCRIPTION("ASoC Amlogic T9015 codec driver");
0325 MODULE_AUTHOR("Jerome Brunet <jbrunet@baylibre.com>");
0326 MODULE_LICENSE("GPL");