]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - sound/soc/codecs/sgtl5000.c
Merge remote-tracking branches 'asoc/topic/twl6040', 'asoc/topic/uda134x', 'asoc...
[karo-tx-linux.git] / sound / soc / codecs / sgtl5000.c
index 10160e7a92774cb26238eadbdf28076b690453ca..29cf7ce610f4707feeaba22425daedbe50491ba4 100644 (file)
@@ -16,6 +16,7 @@
 #include <linux/pm.h>
 #include <linux/i2c.h>
 #include <linux/clk.h>
+#include <linux/log2.h>
 #include <linux/regmap.h>
 #include <linux/regulator/driver.h>
 #include <linux/regulator/machine.h>
@@ -121,6 +122,13 @@ struct ldo_regulator {
        bool enabled;
 };
 
+enum sgtl5000_micbias_resistor {
+       SGTL5000_MICBIAS_OFF = 0,
+       SGTL5000_MICBIAS_2K = 2,
+       SGTL5000_MICBIAS_4K = 4,
+       SGTL5000_MICBIAS_8K = 8,
+};
+
 /* sgtl5000 private structure in codec */
 struct sgtl5000_priv {
        int sysclk;     /* sysclk rate */
@@ -131,6 +139,8 @@ struct sgtl5000_priv {
        struct regmap *regmap;
        struct clk *mclk;
        int revision;
+       u8 micbias_resistor;
+       u8 micbias_voltage;
 };
 
 /*
@@ -145,12 +155,14 @@ struct sgtl5000_priv {
 static int mic_bias_event(struct snd_soc_dapm_widget *w,
        struct snd_kcontrol *kcontrol, int event)
 {
+       struct sgtl5000_priv *sgtl5000 = snd_soc_codec_get_drvdata(w->codec);
+
        switch (event) {
        case SND_SOC_DAPM_POST_PMU:
-               /* change mic bias resistor to 4Kohm */
+               /* change mic bias resistor */
                snd_soc_update_bits(w->codec, SGTL5000_CHIP_MIC_CTRL,
-                               SGTL5000_BIAS_R_MASK,
-                               SGTL5000_BIAS_R_4k << SGTL5000_BIAS_R_SHIFT);
+                       SGTL5000_BIAS_R_MASK,
+                       sgtl5000->micbias_resistor << SGTL5000_BIAS_R_SHIFT);
                break;
 
        case SND_SOC_DAPM_PRE_PMD:
@@ -606,7 +618,7 @@ static int sgtl5000_set_clock(struct snd_soc_codec *codec, int frame_rate)
         * factor of freq = 96 kHz can only be 256, since mclk is in the range
         * of 8 MHz - 27 MHz
         */
-       switch (sgtl5000->sysclk / sys_fs) {
+       switch (sgtl5000->sysclk / frame_rate) {
        case 256:
                clk_ctl |= SGTL5000_MCLK_FREQ_256FS <<
                        SGTL5000_MCLK_FREQ_SHIFT;
@@ -629,7 +641,7 @@ static int sgtl5000_set_clock(struct snd_soc_codec *codec, int frame_rate)
                                "PLL not supported in slave mode\n");
                        dev_err(codec->dev, "%d ratio is not supported. "
                                "SYS_MCLK needs to be 256, 384 or 512 * fs\n",
-                               sgtl5000->sysclk / sys_fs);
+                               sgtl5000->sysclk / frame_rate);
                        return -EINVAL;
                }
        }
@@ -1300,8 +1312,7 @@ static int sgtl5000_probe(struct snd_soc_codec *codec)
 
        /* enable small pop, introduce 400ms delay in turning off */
        snd_soc_update_bits(codec, SGTL5000_CHIP_REF_CTRL,
-                               SGTL5000_SMALL_POP,
-                               SGTL5000_SMALL_POP);
+                               SGTL5000_SMALL_POP, 1);
 
        /* disable short cut detector */
        snd_soc_write(codec, SGTL5000_CHIP_SHORT_CTRL, 0);
@@ -1327,8 +1338,13 @@ static int sgtl5000_probe(struct snd_soc_codec *codec)
                        SGTL5000_HP_ZCD_EN |
                        SGTL5000_ADC_ZCD_EN);
 
-       snd_soc_write(codec, SGTL5000_CHIP_MIC_CTRL, 2);
+       snd_soc_update_bits(codec, SGTL5000_CHIP_MIC_CTRL,
+                       SGTL5000_BIAS_R_MASK,
+                       sgtl5000->micbias_resistor << SGTL5000_BIAS_R_SHIFT);
 
+       snd_soc_update_bits(codec, SGTL5000_CHIP_MIC_CTRL,
+                       SGTL5000_BIAS_R_MASK,
+                       sgtl5000->micbias_voltage << SGTL5000_BIAS_R_SHIFT);
        /*
         * disable DAP
         * TODO:
@@ -1418,10 +1434,10 @@ static int sgtl5000_i2c_probe(struct i2c_client *client,
 {
        struct sgtl5000_priv *sgtl5000;
        int ret, reg, rev;
-       unsigned int mclk;
+       struct device_node *np = client->dev.of_node;
+       u32 value;
 
-       sgtl5000 = devm_kzalloc(&client->dev, sizeof(struct sgtl5000_priv),
-                                                               GFP_KERNEL);
+       sgtl5000 = devm_kzalloc(&client->dev, sizeof(*sgtl5000), GFP_KERNEL);
        if (!sgtl5000)
                return -ENOMEM;
 
@@ -1442,14 +1458,6 @@ static int sgtl5000_i2c_probe(struct i2c_client *client,
                return ret;
        }
 
-       /* SGTL5000 SYS_MCLK should be between 8 and 27 MHz */
-       mclk = clk_get_rate(sgtl5000->mclk);
-       if (mclk < 8000000 || mclk > 27000000) {
-               dev_err(&client->dev, "Invalid SYS_CLK frequency: %u.%03uMHz\n",
-                       mclk / 1000000, mclk / 1000 % 1000);
-               return -EINVAL;
-       }
-
        ret = clk_prepare_enable(sgtl5000->mclk);
        if (ret)
                return ret;
@@ -1471,6 +1479,47 @@ static int sgtl5000_i2c_probe(struct i2c_client *client,
        dev_info(&client->dev, "sgtl5000 revision 0x%x\n", rev);
        sgtl5000->revision = rev;
 
+       if (np) {
+               if (!of_property_read_u32(np,
+                       "micbias-resistor-k-ohms", &value)) {
+                       switch (value) {
+                       case SGTL5000_MICBIAS_OFF:
+                               sgtl5000->micbias_resistor = 0;
+                               break;
+                       case SGTL5000_MICBIAS_2K:
+                               sgtl5000->micbias_resistor = 1;
+                               break;
+                       case SGTL5000_MICBIAS_4K:
+                               sgtl5000->micbias_resistor = 2;
+                               break;
+                       case SGTL5000_MICBIAS_8K:
+                               sgtl5000->micbias_resistor = 3;
+                               break;
+                       default:
+                               sgtl5000->micbias_resistor = 2;
+                               dev_err(&client->dev,
+                                       "Unsuitable MicBias resistor\n");
+                       }
+               } else {
+                       /* default is 4Kohms */
+                       sgtl5000->micbias_resistor = 2;
+               }
+               if (!of_property_read_u32(np,
+                       "micbias-voltage-m-volts", &value)) {
+                       /* 1250mV => 0 */
+                       /* steps of 250mV */
+                       if ((value >= 1250) && (value <= 3000))
+                               sgtl5000->micbias_voltage = (value / 250) - 5;
+                       else {
+                               sgtl5000->micbias_voltage = 0;
+                               dev_err(&client->dev,
+                                       "Unsuitable MicBias resistor\n");
+                       }
+               } else {
+                       sgtl5000->micbias_voltage = 0;
+               }
+       }
+
        i2c_set_clientdata(client, sgtl5000);
 
        /* Ensure sgtl5000 will start with sane register values */