include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[safe/jmp/linux-2.6] / sound / soc / soc-dapm.c
index 8eaf1b6..7c28f40 100644 (file)
 #include <linux/platform_device.h>
 #include <linux/jiffies.h>
 #include <linux/debugfs.h>
+#include <linux/slab.h>
 #include <sound/core.h>
 #include <sound/pcm.h>
 #include <sound/pcm_params.h>
 #include <sound/soc-dapm.h>
 #include <sound/initval.h>
 
-/* debug */
-#ifdef DEBUG
-#define dump_dapm(codec, action) dbg_dump_dapm(codec, action)
-#else
-#define dump_dapm(codec, action)
-#endif
-
 /* dapm power sequences - make this per codec in the future */
 static int dapm_up_seq[] = {
        [snd_soc_dapm_pre] = 0,
@@ -739,6 +733,8 @@ static int dapm_seq_compare(struct snd_soc_dapm_widget *a,
                            struct snd_soc_dapm_widget *b,
                            int sort[])
 {
+       if (a->codec != b->codec)
+               return (unsigned long)a - (unsigned long)b;
        if (sort[a->id] != sort[b->id])
                return sort[a->id] - sort[b->id];
        if (a->reg != b->reg)
@@ -977,9 +973,19 @@ static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
                        if (!w->power_check)
                                continue;
 
-                       power = w->power_check(w);
-                       if (power)
-                               sys_power = 1;
+                       /* If we're suspending then pull down all the 
+                        * power. */
+                       switch (event) {
+                       case SND_SOC_DAPM_STREAM_SUSPEND:
+                               power = 0;
+                               break;
+
+                       default:
+                               power = w->power_check(w);
+                               if (power)
+                                       sys_power = 1;
+                               break;
+                       }
 
                        if (w->power == power)
                                continue;
@@ -1003,13 +1009,32 @@ static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
                case SND_SOC_DAPM_STREAM_RESUME:
                        sys_power = 1;
                        break;
+               case SND_SOC_DAPM_STREAM_SUSPEND:
+                       sys_power = 0;
+                       break;
                case SND_SOC_DAPM_STREAM_NOP:
-                       sys_power = codec->bias_level != SND_SOC_BIAS_STANDBY;
+                       switch (codec->bias_level) {
+                               case SND_SOC_BIAS_STANDBY:
+                               case SND_SOC_BIAS_OFF:
+                                       sys_power = 0;
+                                       break;
+                               default:
+                                       sys_power = 1;
+                                       break;
+                       }
+                       break;
                default:
                        break;
                }
        }
 
+       if (sys_power && codec->bias_level == SND_SOC_BIAS_OFF) {
+               ret = snd_soc_dapm_set_bias_level(socdev,
+                                                 SND_SOC_BIAS_STANDBY);
+               if (ret != 0)
+                       pr_err("Failed to turn on bias: %d\n", ret);
+       }
+
        /* If we're changing to all on or all off then prepare */
        if ((sys_power && codec->bias_level == SND_SOC_BIAS_STANDBY) ||
            (!sys_power && codec->bias_level == SND_SOC_BIAS_ON)) {
@@ -1033,6 +1058,14 @@ static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
                        pr_err("Failed to apply standby bias: %d\n", ret);
        }
 
+       /* If we're in standby and can support bias off then do that */
+       if (codec->bias_level == SND_SOC_BIAS_STANDBY &&
+           codec->idle_bias_off) {
+               ret = snd_soc_dapm_set_bias_level(socdev, SND_SOC_BIAS_OFF);
+               if (ret != 0)
+                       pr_err("Failed to turn off bias: %d\n", ret);
+       }
+
        /* If we just powered up then move to active bias */
        if (codec->bias_level == SND_SOC_BIAS_PREPARE && sys_power) {
                ret = snd_soc_dapm_set_bias_level(socdev,
@@ -1047,66 +1080,6 @@ static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
        return 0;
 }
 
-#ifdef DEBUG
-static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
-{
-       struct snd_soc_dapm_widget *w;
-       struct snd_soc_dapm_path *p = NULL;
-       int in, out;
-
-       printk("DAPM %s %s\n", codec->name, action);
-
-       list_for_each_entry(w, &codec->dapm_widgets, list) {
-
-               /* only display widgets that effect routing */
-               switch (w->id) {
-               case snd_soc_dapm_pre:
-               case snd_soc_dapm_post:
-               case snd_soc_dapm_vmid:
-                       continue;
-               case snd_soc_dapm_mux:
-               case snd_soc_dapm_value_mux:
-               case snd_soc_dapm_output:
-               case snd_soc_dapm_input:
-               case snd_soc_dapm_switch:
-               case snd_soc_dapm_hp:
-               case snd_soc_dapm_mic:
-               case snd_soc_dapm_spk:
-               case snd_soc_dapm_line:
-               case snd_soc_dapm_micbias:
-               case snd_soc_dapm_dac:
-               case snd_soc_dapm_adc:
-               case snd_soc_dapm_pga:
-               case snd_soc_dapm_mixer:
-               case snd_soc_dapm_mixer_named_ctl:
-               case snd_soc_dapm_supply:
-               case snd_soc_dapm_aif_in:
-               case snd_soc_dapm_aif_out:
-                       if (w->name) {
-                               in = is_connected_input_ep(w);
-                               dapm_clear_walk(w->codec);
-                               out = is_connected_output_ep(w);
-                               dapm_clear_walk(w->codec);
-                               printk("%s: %s  in %d out %d\n", w->name,
-                                       w->power ? "On":"Off",in, out);
-
-                               list_for_each_entry(p, &w->sources, list_sink) {
-                                       if (p->connect)
-                                               printk(" in  %s %s\n", p->name ? p->name : "static",
-                                                       p->source->name);
-                               }
-                               list_for_each_entry(p, &w->sinks, list_source) {
-                                       if (p->connect)
-                                               printk(" out %s %s\n", p->name ? p->name : "static",
-                                                       p->sink->name);
-                               }
-                       }
-               break;
-               }
-       }
-}
-#endif
-
 #ifdef CONFIG_DEBUG_FS
 static int dapm_widget_power_open_file(struct inode *inode, struct file *file)
 {
@@ -1133,9 +1106,16 @@ static ssize_t dapm_widget_power_read_file(struct file *file,
        out = is_connected_output_ep(w);
        dapm_clear_walk(w->codec);
 
-       ret = snprintf(buf, PAGE_SIZE, "%s: %s  in %d out %d\n",
+       ret = snprintf(buf, PAGE_SIZE, "%s: %s  in %d out %d",
                       w->name, w->power ? "On" : "Off", in, out);
 
+       if (w->reg >= 0)
+               ret += snprintf(buf + ret, PAGE_SIZE - ret,
+                               " - R%d(0x%x) bit %d",
+                               w->reg, w->reg, w->shift);
+
+       ret += snprintf(buf + ret, PAGE_SIZE - ret, "\n");
+
        if (w->sname)
                ret += snprintf(buf + ret, PAGE_SIZE - ret, " stream %s %s\n",
                                w->sname,
@@ -1202,8 +1182,8 @@ void snd_soc_dapm_debugfs_init(struct snd_soc_codec *codec)
 
 /* test and update the power status of a mux widget */
 static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
-                                struct snd_kcontrol *kcontrol, int mask,
-                                int mux, int val, struct soc_enum *e)
+                                struct snd_kcontrol *kcontrol, int change,
+                                int mux, struct soc_enum *e)
 {
        struct snd_soc_dapm_path *path;
        int found = 0;
@@ -1212,7 +1192,7 @@ static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
            widget->id != snd_soc_dapm_value_mux)
                return -ENODEV;
 
-       if (!snd_soc_test_bits(widget->codec, e->reg, mask, val))
+       if (!change)
                return 0;
 
        /* find dapm widget path assoc with kcontrol */
@@ -1231,18 +1211,15 @@ static int dapm_mux_update_power(struct snd_soc_dapm_widget *widget,
                        path->connect = 0; /* old connection must be powered down */
        }
 
-       if (found) {
+       if (found)
                dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
-               dump_dapm(widget->codec, "mux power update");
-       }
 
        return 0;
 }
 
 /* test and update the power status of a mixer or switch widget */
 static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
-                                  struct snd_kcontrol *kcontrol, int reg,
-                                  int val_mask, int val, int invert)
+                                  struct snd_kcontrol *kcontrol, int connect)
 {
        struct snd_soc_dapm_path *path;
        int found = 0;
@@ -1252,9 +1229,6 @@ static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
            widget->id != snd_soc_dapm_switch)
                return -ENODEV;
 
-       if (!snd_soc_test_bits(widget->codec, reg, val_mask, val))
-               return 0;
-
        /* find dapm widget path assoc with kcontrol */
        list_for_each_entry(path, &widget->codec->dapm_paths, list) {
                if (path->kcontrol != kcontrol)
@@ -1262,19 +1236,12 @@ static int dapm_mixer_update_power(struct snd_soc_dapm_widget *widget,
 
                /* found, now check type */
                found = 1;
-               if (val)
-                       /* new connection */
-                       path->connect = invert ? 0:1;
-               else
-                       /* old connection must be powered down */
-                       path->connect = invert ? 1:0;
+               path->connect = connect;
                break;
        }
 
-       if (found) {
+       if (found)
                dapm_power_widgets(widget->codec, SND_SOC_DAPM_STREAM_NOP);
-               dump_dapm(widget->codec, "mixer power update");
-       }
 
        return 0;
 }
@@ -1390,9 +1357,7 @@ static int snd_soc_dapm_set_pin(struct snd_soc_codec *codec,
  */
 int snd_soc_dapm_sync(struct snd_soc_codec *codec)
 {
-       int ret = dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
-       dump_dapm(codec, "sync");
-       return ret;
+       return dapm_power_widgets(codec, SND_SOC_DAPM_STREAM_NOP);
 }
 EXPORT_SYMBOL_GPL(snd_soc_dapm_sync);
 
@@ -1674,6 +1639,7 @@ int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
        unsigned int mask = (1 << fls(max)) - 1;
        unsigned int invert = mc->invert;
        unsigned int val, val2, val_mask;
+       int connect;
        int ret;
 
        val = (ucontrol->value.integer.value[0] & mask);
@@ -1700,7 +1666,17 @@ int snd_soc_dapm_put_volsw(struct snd_kcontrol *kcontrol,
                return 1;
        }
 
-       dapm_mixer_update_power(widget, kcontrol, reg, val_mask, val, invert);
+       if (snd_soc_test_bits(widget->codec, reg, val_mask, val)) {
+               if (val)
+                       /* new connection */
+                       connect = invert ? 0:1;
+               else
+                       /* old connection must be powered down */
+                       connect = invert ? 1:0;
+
+               dapm_mixer_update_power(widget, kcontrol, connect);
+       }
+
        if (widget->event) {
                if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
                        ret = widget->event(widget, kcontrol,
@@ -1765,7 +1741,7 @@ int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
 {
        struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
-       unsigned int val, mux;
+       unsigned int val, mux, change;
        unsigned int mask, bitmask;
        int ret = 0;
 
@@ -1785,20 +1761,21 @@ int snd_soc_dapm_put_enum_double(struct snd_kcontrol *kcontrol,
 
        mutex_lock(&widget->codec->mutex);
        widget->value = val;
-       dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
-       if (widget->event) {
-               if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
-                       ret = widget->event(widget,
-                               kcontrol, SND_SOC_DAPM_PRE_REG);
-                       if (ret < 0)
-                               goto out;
-               }
-               ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
-               if (widget->event_flags & SND_SOC_DAPM_POST_REG)
-                       ret = widget->event(widget,
-                               kcontrol, SND_SOC_DAPM_POST_REG);
-       } else
-               ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
+       change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
+       dapm_mux_update_power(widget, kcontrol, change, mux, e);
+
+       if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
+               ret = widget->event(widget,
+                                   kcontrol, SND_SOC_DAPM_PRE_REG);
+               if (ret < 0)
+                       goto out;
+       }
+
+       ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
+
+       if (widget->event_flags & SND_SOC_DAPM_POST_REG)
+               ret = widget->event(widget,
+                                   kcontrol, SND_SOC_DAPM_POST_REG);
 
 out:
        mutex_unlock(&widget->codec->mutex);
@@ -1807,6 +1784,54 @@ out:
 EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_double);
 
 /**
+ * snd_soc_dapm_get_enum_virt - Get virtual DAPM mux
+ * @kcontrol: mixer control
+ * @ucontrol: control element information
+ *
+ * Returns 0 for success.
+ */
+int snd_soc_dapm_get_enum_virt(struct snd_kcontrol *kcontrol,
+                              struct snd_ctl_elem_value *ucontrol)
+{
+       struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
+
+       ucontrol->value.enumerated.item[0] = widget->value;
+
+       return 0;
+}
+EXPORT_SYMBOL_GPL(snd_soc_dapm_get_enum_virt);
+
+/**
+ * snd_soc_dapm_put_enum_virt - Set virtual DAPM mux
+ * @kcontrol: mixer control
+ * @ucontrol: control element information
+ *
+ * Returns 0 for success.
+ */
+int snd_soc_dapm_put_enum_virt(struct snd_kcontrol *kcontrol,
+                              struct snd_ctl_elem_value *ucontrol)
+{
+       struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
+       struct soc_enum *e =
+               (struct soc_enum *)kcontrol->private_value;
+       int change;
+       int ret = 0;
+
+       if (ucontrol->value.enumerated.item[0] >= e->max)
+               return -EINVAL;
+
+       mutex_lock(&widget->codec->mutex);
+
+       change = widget->value != ucontrol->value.enumerated.item[0];
+       widget->value = ucontrol->value.enumerated.item[0];
+       dapm_mux_update_power(widget, kcontrol, change, widget->value, e);
+
+       mutex_unlock(&widget->codec->mutex);
+       return ret;
+}
+EXPORT_SYMBOL_GPL(snd_soc_dapm_put_enum_virt);
+
+/**
  * snd_soc_dapm_get_value_enum_double - dapm semi enumerated double mixer get
  *                                     callback
  * @kcontrol: mixer control
@@ -1864,7 +1889,7 @@ int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
 {
        struct snd_soc_dapm_widget *widget = snd_kcontrol_chip(kcontrol);
        struct soc_enum *e = (struct soc_enum *)kcontrol->private_value;
-       unsigned int val, mux;
+       unsigned int val, mux, change;
        unsigned int mask;
        int ret = 0;
 
@@ -1882,20 +1907,21 @@ int snd_soc_dapm_put_value_enum_double(struct snd_kcontrol *kcontrol,
 
        mutex_lock(&widget->codec->mutex);
        widget->value = val;
-       dapm_mux_update_power(widget, kcontrol, mask, mux, val, e);
-       if (widget->event) {
-               if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
-                       ret = widget->event(widget,
-                               kcontrol, SND_SOC_DAPM_PRE_REG);
-                       if (ret < 0)
-                               goto out;
-               }
-               ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
-               if (widget->event_flags & SND_SOC_DAPM_POST_REG)
-                       ret = widget->event(widget,
-                               kcontrol, SND_SOC_DAPM_POST_REG);
-       } else
-               ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
+       change = snd_soc_test_bits(widget->codec, e->reg, mask, val);
+       dapm_mux_update_power(widget, kcontrol, change, mux, e);
+
+       if (widget->event_flags & SND_SOC_DAPM_PRE_REG) {
+               ret = widget->event(widget,
+                                   kcontrol, SND_SOC_DAPM_PRE_REG);
+               if (ret < 0)
+                       goto out;
+       }
+
+       ret = snd_soc_update_bits(widget->codec, e->reg, mask, val);
+
+       if (widget->event_flags & SND_SOC_DAPM_POST_REG)
+               ret = widget->event(widget,
+                                   kcontrol, SND_SOC_DAPM_POST_REG);
 
 out:
        mutex_unlock(&widget->codec->mutex);
@@ -2085,10 +2111,9 @@ int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
                        }
                }
        }
-       mutex_unlock(&codec->mutex);
 
        dapm_power_widgets(codec, event);
-       dump_dapm(codec, __func__);
+       mutex_unlock(&codec->mutex);
        return 0;
 }
 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);