Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * PCM1681 ASoC codec driver
0004  *
0005  * Copyright (c) StreamUnlimited GmbH 2013
0006  *  Marek Belisko <marek.belisko@streamunlimited.com>
0007  */
0008 
0009 #include <linux/module.h>
0010 #include <linux/slab.h>
0011 #include <linux/delay.h>
0012 #include <linux/gpio.h>
0013 #include <linux/i2c.h>
0014 #include <linux/regmap.h>
0015 #include <linux/of.h>
0016 #include <linux/of_device.h>
0017 #include <linux/of_gpio.h>
0018 #include <sound/pcm.h>
0019 #include <sound/pcm_params.h>
0020 #include <sound/soc.h>
0021 #include <sound/tlv.h>
0022 
0023 #define PCM1681_PCM_FORMATS (SNDRV_PCM_FMTBIT_S16_LE  |     \
0024                  SNDRV_PCM_FMTBIT_S24_LE)
0025 
0026 #define PCM1681_PCM_RATES   (SNDRV_PCM_RATE_8000 | SNDRV_PCM_RATE_16000 | \
0027                  SNDRV_PCM_RATE_32000 | SNDRV_PCM_RATE_44100  | \
0028                  SNDRV_PCM_RATE_48000 | SNDRV_PCM_RATE_88200  | \
0029                  SNDRV_PCM_RATE_96000 | SNDRV_PCM_RATE_192000)
0030 
0031 #define PCM1681_SOFT_MUTE_ALL       0xff
0032 #define PCM1681_DEEMPH_RATE_MASK    0x18
0033 #define PCM1681_DEEMPH_MASK     0x01
0034 
0035 #define PCM1681_ATT_CONTROL(X)  (X <= 6 ? X : X + 9) /* Attenuation level */
0036 #define PCM1681_SOFT_MUTE   0x07    /* Soft mute control register */
0037 #define PCM1681_DAC_CONTROL 0x08    /* DAC operation control */
0038 #define PCM1681_FMT_CONTROL 0x09    /* Audio interface data format */
0039 #define PCM1681_DEEMPH_CONTROL  0x0a    /* De-emphasis control */
0040 #define PCM1681_ZERO_DETECT_STATUS  0x0e    /* Zero detect status reg */
0041 
0042 static const struct reg_default pcm1681_reg_defaults[] = {
0043     { 0x01, 0xff },
0044     { 0x02, 0xff },
0045     { 0x03, 0xff },
0046     { 0x04, 0xff },
0047     { 0x05, 0xff },
0048     { 0x06, 0xff },
0049     { 0x07, 0x00 },
0050     { 0x08, 0x00 },
0051     { 0x09, 0x06 },
0052     { 0x0A, 0x00 },
0053     { 0x0B, 0xff },
0054     { 0x0C, 0x0f },
0055     { 0x0D, 0x00 },
0056     { 0x10, 0xff },
0057     { 0x11, 0xff },
0058     { 0x12, 0x00 },
0059     { 0x13, 0x00 },
0060 };
0061 
0062 static bool pcm1681_accessible_reg(struct device *dev, unsigned int reg)
0063 {
0064     return !((reg == 0x00) || (reg == 0x0f));
0065 }
0066 
0067 static bool pcm1681_writeable_reg(struct device *dev, unsigned int reg)
0068 {
0069     return pcm1681_accessible_reg(dev, reg) &&
0070         (reg != PCM1681_ZERO_DETECT_STATUS);
0071 }
0072 
0073 struct pcm1681_private {
0074     struct regmap *regmap;
0075     unsigned int format;
0076     /* Current deemphasis status */
0077     unsigned int deemph;
0078     /* Current rate for deemphasis control */
0079     unsigned int rate;
0080 };
0081 
0082 static const int pcm1681_deemph[] = { 44100, 48000, 32000 };
0083 
0084 static int pcm1681_set_deemph(struct snd_soc_component *component)
0085 {
0086     struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
0087     int i, val = -1, enable = 0;
0088 
0089     if (priv->deemph) {
0090         for (i = 0; i < ARRAY_SIZE(pcm1681_deemph); i++) {
0091             if (pcm1681_deemph[i] == priv->rate) {
0092                 val = i;
0093                 break;
0094             }
0095         }
0096     }
0097 
0098     if (val != -1) {
0099         regmap_update_bits(priv->regmap, PCM1681_DEEMPH_CONTROL,
0100                    PCM1681_DEEMPH_RATE_MASK, val << 3);
0101         enable = 1;
0102     } else {
0103         enable = 0;
0104     }
0105 
0106     /* enable/disable deemphasis functionality */
0107     return regmap_update_bits(priv->regmap, PCM1681_DEEMPH_CONTROL,
0108                     PCM1681_DEEMPH_MASK, enable);
0109 }
0110 
0111 static int pcm1681_get_deemph(struct snd_kcontrol *kcontrol,
0112                   struct snd_ctl_elem_value *ucontrol)
0113 {
0114     struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
0115     struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
0116 
0117     ucontrol->value.integer.value[0] = priv->deemph;
0118 
0119     return 0;
0120 }
0121 
0122 static int pcm1681_put_deemph(struct snd_kcontrol *kcontrol,
0123                   struct snd_ctl_elem_value *ucontrol)
0124 {
0125     struct snd_soc_component *component = snd_soc_kcontrol_component(kcontrol);
0126     struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
0127 
0128     priv->deemph = ucontrol->value.integer.value[0];
0129 
0130     return pcm1681_set_deemph(component);
0131 }
0132 
0133 static int pcm1681_set_dai_fmt(struct snd_soc_dai *codec_dai,
0134                   unsigned int format)
0135 {
0136     struct snd_soc_component *component = codec_dai->component;
0137     struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
0138 
0139     /* The PCM1681 can only be consumer to all clocks */
0140     if ((format & SND_SOC_DAIFMT_CLOCK_PROVIDER_MASK) != SND_SOC_DAIFMT_CBC_CFC) {
0141         dev_err(component->dev, "Invalid clocking mode\n");
0142         return -EINVAL;
0143     }
0144 
0145     priv->format = format;
0146 
0147     return 0;
0148 }
0149 
0150 static int pcm1681_mute(struct snd_soc_dai *dai, int mute, int direction)
0151 {
0152     struct snd_soc_component *component = dai->component;
0153     struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
0154     int val;
0155 
0156     if (mute)
0157         val = PCM1681_SOFT_MUTE_ALL;
0158     else
0159         val = 0;
0160 
0161     return regmap_write(priv->regmap, PCM1681_SOFT_MUTE, val);
0162 }
0163 
0164 static int pcm1681_hw_params(struct snd_pcm_substream *substream,
0165                  struct snd_pcm_hw_params *params,
0166                  struct snd_soc_dai *dai)
0167 {
0168     struct snd_soc_component *component = dai->component;
0169     struct pcm1681_private *priv = snd_soc_component_get_drvdata(component);
0170     int val = 0, ret;
0171 
0172     priv->rate = params_rate(params);
0173 
0174     switch (priv->format & SND_SOC_DAIFMT_FORMAT_MASK) {
0175     case SND_SOC_DAIFMT_RIGHT_J:
0176         switch (params_width(params)) {
0177         case 24:
0178             val = 0;
0179             break;
0180         case 16:
0181             val = 3;
0182             break;
0183         default:
0184             return -EINVAL;
0185         }
0186         break;
0187     case SND_SOC_DAIFMT_I2S:
0188         val = 0x04;
0189         break;
0190     case SND_SOC_DAIFMT_LEFT_J:
0191         val = 0x05;
0192         break;
0193     default:
0194         dev_err(component->dev, "Invalid DAI format\n");
0195         return -EINVAL;
0196     }
0197 
0198     ret = regmap_update_bits(priv->regmap, PCM1681_FMT_CONTROL, 0x0f, val);
0199     if (ret < 0)
0200         return ret;
0201 
0202     return pcm1681_set_deemph(component);
0203 }
0204 
0205 static const struct snd_soc_dai_ops pcm1681_dai_ops = {
0206     .set_fmt    = pcm1681_set_dai_fmt,
0207     .hw_params  = pcm1681_hw_params,
0208     .mute_stream    = pcm1681_mute,
0209     .no_capture_mute = 1,
0210 };
0211 
0212 static const struct snd_soc_dapm_widget pcm1681_dapm_widgets[] = {
0213 SND_SOC_DAPM_OUTPUT("VOUT1"),
0214 SND_SOC_DAPM_OUTPUT("VOUT2"),
0215 SND_SOC_DAPM_OUTPUT("VOUT3"),
0216 SND_SOC_DAPM_OUTPUT("VOUT4"),
0217 SND_SOC_DAPM_OUTPUT("VOUT5"),
0218 SND_SOC_DAPM_OUTPUT("VOUT6"),
0219 SND_SOC_DAPM_OUTPUT("VOUT7"),
0220 SND_SOC_DAPM_OUTPUT("VOUT8"),
0221 };
0222 
0223 static const struct snd_soc_dapm_route pcm1681_dapm_routes[] = {
0224     { "VOUT1", NULL, "Playback" },
0225     { "VOUT2", NULL, "Playback" },
0226     { "VOUT3", NULL, "Playback" },
0227     { "VOUT4", NULL, "Playback" },
0228     { "VOUT5", NULL, "Playback" },
0229     { "VOUT6", NULL, "Playback" },
0230     { "VOUT7", NULL, "Playback" },
0231     { "VOUT8", NULL, "Playback" },
0232 };
0233 
0234 static const DECLARE_TLV_DB_SCALE(pcm1681_dac_tlv, -6350, 50, 1);
0235 
0236 static const struct snd_kcontrol_new pcm1681_controls[] = {
0237     SOC_DOUBLE_R_TLV("Channel 1/2 Playback Volume",
0238             PCM1681_ATT_CONTROL(1), PCM1681_ATT_CONTROL(2), 0,
0239             0x7f, 0, pcm1681_dac_tlv),
0240     SOC_DOUBLE_R_TLV("Channel 3/4 Playback Volume",
0241             PCM1681_ATT_CONTROL(3), PCM1681_ATT_CONTROL(4), 0,
0242             0x7f, 0, pcm1681_dac_tlv),
0243     SOC_DOUBLE_R_TLV("Channel 5/6 Playback Volume",
0244             PCM1681_ATT_CONTROL(5), PCM1681_ATT_CONTROL(6), 0,
0245             0x7f, 0, pcm1681_dac_tlv),
0246     SOC_DOUBLE_R_TLV("Channel 7/8 Playback Volume",
0247             PCM1681_ATT_CONTROL(7), PCM1681_ATT_CONTROL(8), 0,
0248             0x7f, 0, pcm1681_dac_tlv),
0249     SOC_SINGLE_BOOL_EXT("De-emphasis Switch", 0,
0250                 pcm1681_get_deemph, pcm1681_put_deemph),
0251 };
0252 
0253 static struct snd_soc_dai_driver pcm1681_dai = {
0254     .name = "pcm1681-hifi",
0255     .playback = {
0256         .stream_name = "Playback",
0257         .channels_min = 2,
0258         .channels_max = 8,
0259         .rates = PCM1681_PCM_RATES,
0260         .formats = PCM1681_PCM_FORMATS,
0261     },
0262     .ops = &pcm1681_dai_ops,
0263 };
0264 
0265 #ifdef CONFIG_OF
0266 static const struct of_device_id pcm1681_dt_ids[] = {
0267     { .compatible = "ti,pcm1681", },
0268     { }
0269 };
0270 MODULE_DEVICE_TABLE(of, pcm1681_dt_ids);
0271 #endif
0272 
0273 static const struct regmap_config pcm1681_regmap = {
0274     .reg_bits       = 8,
0275     .val_bits       = 8,
0276     .max_register       = 0x13,
0277     .reg_defaults       = pcm1681_reg_defaults,
0278     .num_reg_defaults   = ARRAY_SIZE(pcm1681_reg_defaults),
0279     .writeable_reg      = pcm1681_writeable_reg,
0280     .readable_reg       = pcm1681_accessible_reg,
0281 };
0282 
0283 static const struct snd_soc_component_driver soc_component_dev_pcm1681 = {
0284     .controls       = pcm1681_controls,
0285     .num_controls       = ARRAY_SIZE(pcm1681_controls),
0286     .dapm_widgets       = pcm1681_dapm_widgets,
0287     .num_dapm_widgets   = ARRAY_SIZE(pcm1681_dapm_widgets),
0288     .dapm_routes        = pcm1681_dapm_routes,
0289     .num_dapm_routes    = ARRAY_SIZE(pcm1681_dapm_routes),
0290     .idle_bias_on       = 1,
0291     .use_pmdown_time    = 1,
0292     .endianness     = 1,
0293 };
0294 
0295 static const struct i2c_device_id pcm1681_i2c_id[] = {
0296     {"pcm1681", 0},
0297     {}
0298 };
0299 MODULE_DEVICE_TABLE(i2c, pcm1681_i2c_id);
0300 
0301 static int pcm1681_i2c_probe(struct i2c_client *client)
0302 {
0303     int ret;
0304     struct pcm1681_private *priv;
0305 
0306     priv = devm_kzalloc(&client->dev, sizeof(*priv), GFP_KERNEL);
0307     if (!priv)
0308         return -ENOMEM;
0309 
0310     priv->regmap = devm_regmap_init_i2c(client, &pcm1681_regmap);
0311     if (IS_ERR(priv->regmap)) {
0312         ret = PTR_ERR(priv->regmap);
0313         dev_err(&client->dev, "Failed to create regmap: %d\n", ret);
0314         return ret;
0315     }
0316 
0317     i2c_set_clientdata(client, priv);
0318 
0319     return devm_snd_soc_register_component(&client->dev,
0320         &soc_component_dev_pcm1681,
0321         &pcm1681_dai, 1);
0322 }
0323 
0324 static struct i2c_driver pcm1681_i2c_driver = {
0325     .driver = {
0326         .name   = "pcm1681",
0327         .of_match_table = of_match_ptr(pcm1681_dt_ids),
0328     },
0329     .id_table   = pcm1681_i2c_id,
0330     .probe_new  = pcm1681_i2c_probe,
0331 };
0332 
0333 module_i2c_driver(pcm1681_i2c_driver);
0334 
0335 MODULE_DESCRIPTION("Texas Instruments PCM1681 ALSA SoC Codec Driver");
0336 MODULE_AUTHOR("Marek Belisko <marek.belisko@streamunlimited.com>");
0337 MODULE_LICENSE("GPL");