Merge tag 'gfs2-merge-window' of git://git.kernel.org/pub/scm/linux/kernel/git/steve...
[firefly-linux-kernel-4.4.55.git] / sound / soc / codecs / tlv320aic32x4.c
index 688151ba309af2e3af8bdeea6a428850947d3773..1d9b117345a3ae6fc8182f73733603a34f6a20d7 100644 (file)
 #include <linux/delay.h>
 #include <linux/pm.h>
 #include <linux/gpio.h>
+#include <linux/of_gpio.h>
 #include <linux/i2c.h>
 #include <linux/cdev.h>
 #include <linux/slab.h>
+#include <linux/clk.h>
+#include <linux/regulator/consumer.h>
 
 #include <sound/tlv320aic32x4.h>
 #include <sound/core.h>
@@ -66,20 +69,32 @@ struct aic32x4_priv {
        u32 micpga_routing;
        bool swapdacs;
        int rstn_gpio;
+       struct clk *mclk;
+
+       struct regulator *supply_ldo;
+       struct regulator *supply_iov;
+       struct regulator *supply_dv;
+       struct regulator *supply_av;
 };
 
-/* 0dB min, 1dB steps */
-static DECLARE_TLV_DB_SCALE(tlv_step_1, 0, 100, 0);
 /* 0dB min, 0.5dB steps */
 static DECLARE_TLV_DB_SCALE(tlv_step_0_5, 0, 50, 0);
+/* -63.5dB min, 0.5dB steps */
+static DECLARE_TLV_DB_SCALE(tlv_pcm, -6350, 50, 0);
+/* -6dB min, 1dB steps */
+static DECLARE_TLV_DB_SCALE(tlv_driver_gain, -600, 100, 0);
+/* -12dB min, 0.5dB steps */
+static DECLARE_TLV_DB_SCALE(tlv_adc_vol, -1200, 50, 0);
 
 static const struct snd_kcontrol_new aic32x4_snd_controls[] = {
-       SOC_DOUBLE_R_TLV("PCM Playback Volume", AIC32X4_LDACVOL,
-                       AIC32X4_RDACVOL, 0, 0x30, 0, tlv_step_0_5),
-       SOC_DOUBLE_R_TLV("HP Driver Gain Volume", AIC32X4_HPLGAIN,
-                       AIC32X4_HPRGAIN, 0, 0x1D, 0, tlv_step_1),
-       SOC_DOUBLE_R_TLV("LO Driver Gain Volume", AIC32X4_LOLGAIN,
-                       AIC32X4_LORGAIN, 0, 0x1D, 0, tlv_step_1),
+       SOC_DOUBLE_R_S_TLV("PCM Playback Volume", AIC32X4_LDACVOL,
+                       AIC32X4_RDACVOL, 0, -0x7f, 0x30, 7, 0, tlv_pcm),
+       SOC_DOUBLE_R_S_TLV("HP Driver Gain Volume", AIC32X4_HPLGAIN,
+                       AIC32X4_HPRGAIN, 0, -0x6, 0x1d, 5, 0,
+                       tlv_driver_gain),
+       SOC_DOUBLE_R_S_TLV("LO Driver Gain Volume", AIC32X4_LOLGAIN,
+                       AIC32X4_LORGAIN, 0, -0x6, 0x1d, 5, 0,
+                       tlv_driver_gain),
        SOC_DOUBLE_R("HP DAC Playback Switch", AIC32X4_HPLGAIN,
                        AIC32X4_HPRGAIN, 6, 0x01, 1),
        SOC_DOUBLE_R("LO DAC Playback Switch", AIC32X4_LOLGAIN,
@@ -90,8 +105,8 @@ static const struct snd_kcontrol_new aic32x4_snd_controls[] = {
        SOC_SINGLE("ADCFGA Left Mute Switch", AIC32X4_ADCFGA, 7, 1, 0),
        SOC_SINGLE("ADCFGA Right Mute Switch", AIC32X4_ADCFGA, 3, 1, 0),
 
-       SOC_DOUBLE_R_TLV("ADC Level Volume", AIC32X4_LADCVOL,
-                       AIC32X4_RADCVOL, 0, 0x28, 0, tlv_step_0_5),
+       SOC_DOUBLE_R_S_TLV("ADC Level Volume", AIC32X4_LADCVOL,
+                       AIC32X4_RADCVOL, 0, -0x18, 0x28, 6, 0, tlv_adc_vol),
        SOC_DOUBLE_R_TLV("PGA Level Volume", AIC32X4_LMICPGAVOL,
                        AIC32X4_RMICPGAVOL, 0, 0x5f, 0, tlv_step_0_5),
 
@@ -480,8 +495,18 @@ static int aic32x4_mute(struct snd_soc_dai *dai, int mute)
 static int aic32x4_set_bias_level(struct snd_soc_codec *codec,
                                  enum snd_soc_bias_level level)
 {
+       struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
+       int ret;
+
        switch (level) {
        case SND_SOC_BIAS_ON:
+               /* Switch on master clock */
+               ret = clk_prepare_enable(aic32x4->mclk);
+               if (ret) {
+                       dev_err(codec->dev, "Failed to enable master clock\n");
+                       return ret;
+               }
+
                /* Switch on PLL */
                snd_soc_update_bits(codec, AIC32X4_PLLPR,
                                    AIC32X4_PLLEN, AIC32X4_PLLEN);
@@ -509,29 +534,32 @@ static int aic32x4_set_bias_level(struct snd_soc_codec *codec,
        case SND_SOC_BIAS_PREPARE:
                break;
        case SND_SOC_BIAS_STANDBY:
-               /* Switch off PLL */
-               snd_soc_update_bits(codec, AIC32X4_PLLPR,
-                                   AIC32X4_PLLEN, 0);
+               /* Switch off BCLK_N Divider */
+               snd_soc_update_bits(codec, AIC32X4_BCLKN,
+                                   AIC32X4_BCLKEN, 0);
 
-               /* Switch off NDAC Divider */
-               snd_soc_update_bits(codec, AIC32X4_NDAC,
-                                   AIC32X4_NDACEN, 0);
+               /* Switch off MADC Divider */
+               snd_soc_update_bits(codec, AIC32X4_MADC,
+                                   AIC32X4_MADCEN, 0);
+
+               /* Switch off NADC Divider */
+               snd_soc_update_bits(codec, AIC32X4_NADC,
+                                   AIC32X4_NADCEN, 0);
 
                /* Switch off MDAC Divider */
                snd_soc_update_bits(codec, AIC32X4_MDAC,
                                    AIC32X4_MDACEN, 0);
 
-               /* Switch off NADC Divider */
-               snd_soc_update_bits(codec, AIC32X4_NADC,
-                                   AIC32X4_NADCEN, 0);
+               /* Switch off NDAC Divider */
+               snd_soc_update_bits(codec, AIC32X4_NDAC,
+                                   AIC32X4_NDACEN, 0);
 
-               /* Switch off MADC Divider */
-               snd_soc_update_bits(codec, AIC32X4_MADC,
-                                   AIC32X4_MADCEN, 0);
+               /* Switch off PLL */
+               snd_soc_update_bits(codec, AIC32X4_PLLPR,
+                                   AIC32X4_PLLEN, 0);
 
-               /* Switch off BCLK_N Divider */
-               snd_soc_update_bits(codec, AIC32X4_BCLKN,
-                                   AIC32X4_BCLKEN, 0);
+               /* Switch off master clock */
+               clk_disable_unprepare(aic32x4->mclk);
                break;
        case SND_SOC_BIAS_OFF:
                break;
@@ -586,9 +614,7 @@ static int aic32x4_probe(struct snd_soc_codec *codec)
        struct aic32x4_priv *aic32x4 = snd_soc_codec_get_drvdata(codec);
        u32 tmp_reg;
 
-       snd_soc_codec_set_cache_io(codec, 8, 8, SND_SOC_REGMAP);
-
-       if (aic32x4->rstn_gpio >= 0) {
+       if (gpio_is_valid(aic32x4->rstn_gpio)) {
                ndelay(10);
                gpio_set_value(aic32x4->rstn_gpio, 1);
        }
@@ -663,11 +689,122 @@ static struct snd_soc_codec_driver soc_codec_dev_aic32x4 = {
        .num_dapm_routes = ARRAY_SIZE(aic32x4_dapm_routes),
 };
 
+static int aic32x4_parse_dt(struct aic32x4_priv *aic32x4,
+               struct device_node *np)
+{
+       aic32x4->swapdacs = false;
+       aic32x4->micpga_routing = 0;
+       aic32x4->rstn_gpio = of_get_named_gpio(np, "reset-gpios", 0);
+
+       return 0;
+}
+
+static void aic32x4_disable_regulators(struct aic32x4_priv *aic32x4)
+{
+       regulator_disable(aic32x4->supply_iov);
+
+       if (!IS_ERR(aic32x4->supply_ldo))
+               regulator_disable(aic32x4->supply_ldo);
+
+       if (!IS_ERR(aic32x4->supply_dv))
+               regulator_disable(aic32x4->supply_dv);
+
+       if (!IS_ERR(aic32x4->supply_av))
+               regulator_disable(aic32x4->supply_av);
+}
+
+static int aic32x4_setup_regulators(struct device *dev,
+               struct aic32x4_priv *aic32x4)
+{
+       int ret = 0;
+
+       aic32x4->supply_ldo = devm_regulator_get_optional(dev, "ldoin");
+       aic32x4->supply_iov = devm_regulator_get(dev, "iov");
+       aic32x4->supply_dv = devm_regulator_get_optional(dev, "dv");
+       aic32x4->supply_av = devm_regulator_get_optional(dev, "av");
+
+       /* Check if the regulator requirements are fulfilled */
+
+       if (IS_ERR(aic32x4->supply_iov)) {
+               dev_err(dev, "Missing supply 'iov'\n");
+               return PTR_ERR(aic32x4->supply_iov);
+       }
+
+       if (IS_ERR(aic32x4->supply_ldo)) {
+               if (PTR_ERR(aic32x4->supply_ldo) == -EPROBE_DEFER)
+                       return -EPROBE_DEFER;
+
+               if (IS_ERR(aic32x4->supply_dv)) {
+                       dev_err(dev, "Missing supply 'dv' or 'ldoin'\n");
+                       return PTR_ERR(aic32x4->supply_dv);
+               }
+               if (IS_ERR(aic32x4->supply_av)) {
+                       dev_err(dev, "Missing supply 'av' or 'ldoin'\n");
+                       return PTR_ERR(aic32x4->supply_av);
+               }
+       } else {
+               if (IS_ERR(aic32x4->supply_dv) &&
+                               PTR_ERR(aic32x4->supply_dv) == -EPROBE_DEFER)
+                       return -EPROBE_DEFER;
+               if (IS_ERR(aic32x4->supply_av) &&
+                               PTR_ERR(aic32x4->supply_av) == -EPROBE_DEFER)
+                       return -EPROBE_DEFER;
+       }
+
+       ret = regulator_enable(aic32x4->supply_iov);
+       if (ret) {
+               dev_err(dev, "Failed to enable regulator iov\n");
+               return ret;
+       }
+
+       if (!IS_ERR(aic32x4->supply_ldo)) {
+               ret = regulator_enable(aic32x4->supply_ldo);
+               if (ret) {
+                       dev_err(dev, "Failed to enable regulator ldo\n");
+                       goto error_ldo;
+               }
+       }
+
+       if (!IS_ERR(aic32x4->supply_dv)) {
+               ret = regulator_enable(aic32x4->supply_dv);
+               if (ret) {
+                       dev_err(dev, "Failed to enable regulator dv\n");
+                       goto error_dv;
+               }
+       }
+
+       if (!IS_ERR(aic32x4->supply_av)) {
+               ret = regulator_enable(aic32x4->supply_av);
+               if (ret) {
+                       dev_err(dev, "Failed to enable regulator av\n");
+                       goto error_av;
+               }
+       }
+
+       if (!IS_ERR(aic32x4->supply_ldo) && IS_ERR(aic32x4->supply_av))
+               aic32x4->power_cfg |= AIC32X4_PWR_AIC32X4_LDO_ENABLE;
+
+       return 0;
+
+error_av:
+       if (!IS_ERR(aic32x4->supply_dv))
+               regulator_disable(aic32x4->supply_dv);
+
+error_dv:
+       if (!IS_ERR(aic32x4->supply_ldo))
+               regulator_disable(aic32x4->supply_ldo);
+
+error_ldo:
+       regulator_disable(aic32x4->supply_iov);
+       return ret;
+}
+
 static int aic32x4_i2c_probe(struct i2c_client *i2c,
                             const struct i2c_device_id *id)
 {
        struct aic32x4_pdata *pdata = i2c->dev.platform_data;
        struct aic32x4_priv *aic32x4;
+       struct device_node *np = i2c->dev.of_node;
        int ret;
 
        aic32x4 = devm_kzalloc(&i2c->dev, sizeof(struct aic32x4_priv),
@@ -686,6 +823,12 @@ static int aic32x4_i2c_probe(struct i2c_client *i2c,
                aic32x4->swapdacs = pdata->swapdacs;
                aic32x4->micpga_routing = pdata->micpga_routing;
                aic32x4->rstn_gpio = pdata->rstn_gpio;
+       } else if (np) {
+               ret = aic32x4_parse_dt(aic32x4, np);
+               if (ret) {
+                       dev_err(&i2c->dev, "Failed to parse DT node\n");
+                       return ret;
+               }
        } else {
                aic32x4->power_cfg = 0;
                aic32x4->swapdacs = false;
@@ -693,20 +836,44 @@ static int aic32x4_i2c_probe(struct i2c_client *i2c,
                aic32x4->rstn_gpio = -1;
        }
 
-       if (aic32x4->rstn_gpio >= 0) {
+       aic32x4->mclk = devm_clk_get(&i2c->dev, "mclk");
+       if (IS_ERR(aic32x4->mclk)) {
+               dev_err(&i2c->dev, "Failed getting the mclk. The current implementation does not support the usage of this codec without mclk\n");
+               return PTR_ERR(aic32x4->mclk);
+       }
+
+       if (gpio_is_valid(aic32x4->rstn_gpio)) {
                ret = devm_gpio_request_one(&i2c->dev, aic32x4->rstn_gpio,
                                GPIOF_OUT_INIT_LOW, "tlv320aic32x4 rstn");
                if (ret != 0)
                        return ret;
        }
 
+       ret = aic32x4_setup_regulators(&i2c->dev, aic32x4);
+       if (ret) {
+               dev_err(&i2c->dev, "Failed to setup regulators\n");
+               return ret;
+       }
+
        ret = snd_soc_register_codec(&i2c->dev,
                        &soc_codec_dev_aic32x4, &aic32x4_dai, 1);
-       return ret;
+       if (ret) {
+               dev_err(&i2c->dev, "Failed to register codec\n");
+               aic32x4_disable_regulators(aic32x4);
+               return ret;
+       }
+
+       i2c_set_clientdata(i2c, aic32x4);
+
+       return 0;
 }
 
 static int aic32x4_i2c_remove(struct i2c_client *client)
 {
+       struct aic32x4_priv *aic32x4 = i2c_get_clientdata(client);
+
+       aic32x4_disable_regulators(aic32x4);
+
        snd_soc_unregister_codec(&client->dev);
        return 0;
 }
@@ -717,10 +884,17 @@ static const struct i2c_device_id aic32x4_i2c_id[] = {
 };
 MODULE_DEVICE_TABLE(i2c, aic32x4_i2c_id);
 
+static const struct of_device_id aic32x4_of_id[] = {
+       { .compatible = "ti,tlv320aic32x4", },
+       { /* senitel */ }
+};
+MODULE_DEVICE_TABLE(of, aic32x4_of_id);
+
 static struct i2c_driver aic32x4_i2c_driver = {
        .driver = {
                .name = "tlv320aic32x4",
                .owner = THIS_MODULE,
+               .of_match_table = aic32x4_of_id,
        },
        .probe =    aic32x4_i2c_probe,
        .remove =   aic32x4_i2c_remove,