Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[deliverable/linux.git] / sound / soc / codecs / wm8993.c
index 3c9336cd4eeb4f9c475b27c8317155b3a84ab470..bf022f68b84f114ccc44835910df1315bca3fe70 100644 (file)
@@ -16,6 +16,7 @@
 #include <linux/delay.h>
 #include <linux/pm.h>
 #include <linux/i2c.h>
+#include <linux/regulator/consumer.h>
 #include <linux/spi/spi.h>
 #include <sound/core.h>
 #include <sound/pcm.h>
 #include "wm8993.h"
 #include "wm_hubs.h"
 
+#define WM8993_NUM_SUPPLIES 6
+static const char *wm8993_supply_names[WM8993_NUM_SUPPLIES] = {
+       "DCVDD",
+       "DBVDD",
+       "AVDD1",
+       "AVDD2",
+       "CPVDD",
+       "SPKVDD",
+};
+
 static u16 wm8993_reg_defaults[WM8993_REGISTER_COUNT] = {
        0x8993,     /* R0   - Software Reset */
        0x0000,     /* R1   - Power Management (1) */
@@ -215,6 +226,7 @@ static struct {
 struct wm8993_priv {
        struct wm_hubs_data hubs_data;
        u16 reg_cache[WM8993_REGISTER_COUNT];
+       struct regulator_bulk_data supplies[WM8993_NUM_SUPPLIES];
        struct wm8993_platform_data pdata;
        struct snd_soc_codec codec;
        int master;
@@ -911,10 +923,33 @@ static const struct snd_soc_dapm_route routes[] = {
        { "Right Headphone Mux", "DAC", "DACR" },
 };
 
+static void wm8993_cache_restore(struct snd_soc_codec *codec)
+{
+       u16 *cache = codec->reg_cache;
+       int i;
+
+       if (!codec->cache_sync)
+               return;
+
+       /* Reenable hardware writes */
+       codec->cache_only = 0;
+
+       /* Restore the register settings */
+       for (i = 1; i < WM8993_MAX_REGISTER; i++) {
+               if (cache[i] == wm8993_reg_defaults[i])
+                       continue;
+               snd_soc_write(codec, i, cache[i]);
+       }
+
+       /* We're in sync again */
+       codec->cache_sync = 0;
+}
+
 static int wm8993_set_bias_level(struct snd_soc_codec *codec,
                                 enum snd_soc_bias_level level)
 {
        struct wm8993_priv *wm8993 = codec->private_data;
+       int ret;
 
        switch (level) {
        case SND_SOC_BIAS_ON:
@@ -928,6 +963,13 @@ static int wm8993_set_bias_level(struct snd_soc_codec *codec,
 
        case SND_SOC_BIAS_STANDBY:
                if (codec->bias_level == SND_SOC_BIAS_OFF) {
+                       ret = regulator_bulk_enable(ARRAY_SIZE(wm8993->supplies),
+                                                   wm8993->supplies);
+                       if (ret != 0)
+                               return ret;
+
+                       wm8993_cache_restore(codec);
+
                        /* Tune DC servo configuration */
                        snd_soc_write(codec, 0x44, 3);
                        snd_soc_write(codec, 0x56, 3);
@@ -980,6 +1022,18 @@ static int wm8993_set_bias_level(struct snd_soc_codec *codec,
                snd_soc_update_bits(codec, WM8993_POWER_MANAGEMENT_1,
                                    WM8993_VMID_SEL_MASK | WM8993_BIAS_ENA,
                                    0);
+
+#ifdef CONFIG_REGULATOR
+               /* Post 2.6.34 we will be able to get a callback when
+                * the regulators are disabled which we can use but
+               * for now just assume that the power will be cut if
+               * the regulator API is in use.
+               */
+               codec->cache_sync = 1;
+#endif
+
+               regulator_bulk_disable(ARRAY_SIZE(wm8993->supplies),
+                                      wm8993->supplies);
                break;
        }
 
@@ -1448,15 +1502,7 @@ static int wm8993_resume(struct platform_device *pdev)
        struct snd_soc_device *socdev = platform_get_drvdata(pdev);
        struct snd_soc_codec *codec = socdev->card->codec;
        struct wm8993_priv *wm8993 = codec->private_data;
-       u16 *cache = wm8993->reg_cache;
-       int i, ret;
-
-       /* Restore the register settings */
-       for (i = 1; i < WM8993_MAX_REGISTER; i++) {
-               if (cache[i] == wm8993_reg_defaults[i])
-                       continue;
-               snd_soc_write(codec, i, cache[i]);
-       }
+       int ret;
 
        wm8993_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
 
@@ -1496,6 +1542,7 @@ static int wm8993_i2c_probe(struct i2c_client *i2c,
        struct snd_soc_codec *codec;
        unsigned int val;
        int ret;
+       int i;
 
        if (wm8993_codec) {
                dev_err(&i2c->dev, "A WM8993 is already registered\n");
@@ -1543,16 +1590,35 @@ static int wm8993_i2c_probe(struct i2c_client *i2c,
 
        codec->dev = &i2c->dev;
 
+       for (i = 0; i < ARRAY_SIZE(wm8993->supplies); i++)
+               wm8993->supplies[i].supply = wm8993_supply_names[i];
+
+       ret = regulator_bulk_get(codec->dev, ARRAY_SIZE(wm8993->supplies),
+                                wm8993->supplies);
+       if (ret != 0) {
+               dev_err(codec->dev, "Failed to request supplies: %d\n", ret);
+               goto err;
+       }
+
+       ret = regulator_bulk_enable(ARRAY_SIZE(wm8993->supplies),
+                                   wm8993->supplies);
+       if (ret != 0) {
+               dev_err(codec->dev, "Failed to enable supplies: %d\n", ret);
+               goto err_get;
+       }
+
        val = snd_soc_read(codec, WM8993_SOFTWARE_RESET);
        if (val != wm8993_reg_defaults[WM8993_SOFTWARE_RESET]) {
                dev_err(codec->dev, "Invalid ID register value %x\n", val);
                ret = -EINVAL;
-               goto err;
+               goto err_enable;
        }
 
        ret = snd_soc_write(codec, WM8993_SOFTWARE_RESET, 0xffff);
        if (ret != 0)
-               goto err;
+               goto err_enable;
+
+       codec->cache_only = 1;
 
        /* By default we're using the output mixers */
        wm8993->class_w_users = 2;
@@ -1582,7 +1648,7 @@ static int wm8993_i2c_probe(struct i2c_client *i2c,
                             
        ret = wm8993_set_bias_level(codec, SND_SOC_BIAS_STANDBY);
        if (ret != 0)
-               goto err;
+               goto err_enable;
 
        wm8993_dai.dev = codec->dev;
 
@@ -1596,6 +1662,10 @@ static int wm8993_i2c_probe(struct i2c_client *i2c,
 
 err_bias:
        wm8993_set_bias_level(codec, SND_SOC_BIAS_OFF);
+err_enable:
+       regulator_bulk_disable(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
+err_get:
+       regulator_bulk_free(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
 err:
        wm8993_codec = NULL;
        kfree(wm8993);
@@ -1610,6 +1680,7 @@ static int wm8993_i2c_remove(struct i2c_client *client)
        snd_soc_unregister_dai(&wm8993_dai);
 
        wm8993_set_bias_level(&wm8993->codec, SND_SOC_BIAS_OFF);
+       regulator_bulk_free(ARRAY_SIZE(wm8993->supplies), wm8993->supplies);
        kfree(wm8993);
 
        return 0;
This page took 0.029705 seconds and 5 git commands to generate.