Merge branch 'topic/pcm-subclass-fix' into for-linus
[pandora-kernel.git] / sound / soc / soc-dapm.c
index 735903a..21c6907 100644 (file)
@@ -12,7 +12,7 @@
  *  Features:
  *    o Changes power status of internal codec blocks depending on the
  *      dynamic configuration of codec internal audio paths and active
- *      DAC's/ADC's.
+ *      DACs/ADCs.
  *    o Platform power domain - can support external components i.e. amps and
  *      mic/meadphone insertion events.
  *    o Automatic Mic Bias support
 
 /* dapm power sequences - make this per codec in the future */
 static int dapm_up_seq[] = {
-       snd_soc_dapm_pre, snd_soc_dapm_micbias, snd_soc_dapm_mic,
-       snd_soc_dapm_mux, snd_soc_dapm_value_mux, snd_soc_dapm_dac,
-       snd_soc_dapm_mixer, snd_soc_dapm_mixer_named_ctl, snd_soc_dapm_pga,
-       snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk, snd_soc_dapm_post
+       snd_soc_dapm_pre, snd_soc_dapm_supply, snd_soc_dapm_micbias,
+       snd_soc_dapm_mic, snd_soc_dapm_mux, snd_soc_dapm_value_mux,
+       snd_soc_dapm_dac, snd_soc_dapm_mixer, snd_soc_dapm_mixer_named_ctl,
+       snd_soc_dapm_pga, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
+       snd_soc_dapm_post
 };
 
 static int dapm_down_seq[] = {
        snd_soc_dapm_pre, snd_soc_dapm_adc, snd_soc_dapm_hp, snd_soc_dapm_spk,
        snd_soc_dapm_pga, snd_soc_dapm_mixer_named_ctl, snd_soc_dapm_mixer,
        snd_soc_dapm_dac, snd_soc_dapm_mic, snd_soc_dapm_micbias,
-       snd_soc_dapm_mux, snd_soc_dapm_value_mux, snd_soc_dapm_post
+       snd_soc_dapm_mux, snd_soc_dapm_value_mux, snd_soc_dapm_supply,
+       snd_soc_dapm_post
 };
 
-static int dapm_status = 1;
-module_param(dapm_status, int, 0);
-MODULE_PARM_DESC(dapm_status, "enable DPM sysfs entries");
-
 static void pop_wait(u32 pop_time)
 {
        if (pop_time)
@@ -96,6 +94,48 @@ static inline struct snd_soc_dapm_widget *dapm_cnew_widget(
        return kmemdup(_widget, sizeof(*_widget), GFP_KERNEL);
 }
 
+/**
+ * snd_soc_dapm_set_bias_level - set the bias level for the system
+ * @socdev: audio device
+ * @level: level to configure
+ *
+ * Configure the bias (power) levels for the SoC audio device.
+ *
+ * Returns 0 for success else error.
+ */
+static int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
+                                      enum snd_soc_bias_level level)
+{
+       struct snd_soc_card *card = socdev->card;
+       struct snd_soc_codec *codec = socdev->card->codec;
+       int ret = 0;
+
+       switch (level) {
+       case SND_SOC_BIAS_ON:
+               dev_dbg(socdev->dev, "Setting full bias\n");
+               break;
+       case SND_SOC_BIAS_PREPARE:
+               dev_dbg(socdev->dev, "Setting bias prepare\n");
+               break;
+       case SND_SOC_BIAS_STANDBY:
+               dev_dbg(socdev->dev, "Setting standby bias\n");
+               break;
+       case SND_SOC_BIAS_OFF:
+               dev_dbg(socdev->dev, "Setting bias off\n");
+               break;
+       default:
+               dev_err(socdev->dev, "Setting invalid bias %d\n", level);
+               return -EINVAL;
+       }
+
+       if (card->set_bias_level)
+               ret = card->set_bias_level(card, level);
+       if (ret == 0 && codec->set_bias_level)
+               ret = codec->set_bias_level(codec, level);
+
+       return ret;
+}
+
 /* set up initial codec paths */
 static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
        struct snd_soc_dapm_path *p, int i)
@@ -165,6 +205,7 @@ static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
        case snd_soc_dapm_dac:
        case snd_soc_dapm_micbias:
        case snd_soc_dapm_vmid:
+       case snd_soc_dapm_supply:
                p->connect = 1;
        break;
        /* does effect routing - dynamically connected */
@@ -179,7 +220,7 @@ static void dapm_set_path_status(struct snd_soc_dapm_widget *w,
        }
 }
 
-/* connect mux widget to it's interconnecting audio paths */
+/* connect mux widget to its interconnecting audio paths */
 static int dapm_connect_mux(struct snd_soc_codec *codec,
        struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
        struct snd_soc_dapm_path *path, const char *control_name,
@@ -202,7 +243,7 @@ static int dapm_connect_mux(struct snd_soc_codec *codec,
        return -ENODEV;
 }
 
-/* connect mixer widget to it's interconnecting audio paths */
+/* connect mixer widget to its interconnecting audio paths */
 static int dapm_connect_mixer(struct snd_soc_codec *codec,
        struct snd_soc_dapm_widget *src, struct snd_soc_dapm_widget *dest,
        struct snd_soc_dapm_path *path, const char *control_name)
@@ -357,8 +398,9 @@ static int dapm_new_mixer(struct snd_soc_codec *codec,
                                path->long_name);
                        ret = snd_ctl_add(codec->card, path->kcontrol);
                        if (ret < 0) {
-                               printk(KERN_ERR "asoc: failed to add dapm kcontrol %s\n",
-                                               path->long_name);
+                               printk(KERN_ERR "asoc: failed to add dapm kcontrol %s: %d\n",
+                                      path->long_name,
+                                      ret);
                                kfree(path->long_name);
                                path->long_name = NULL;
                                return ret;
@@ -434,6 +476,9 @@ static int is_connected_output_ep(struct snd_soc_dapm_widget *widget)
        struct snd_soc_dapm_path *path;
        int con = 0;
 
+       if (widget->id == snd_soc_dapm_supply)
+               return 0;
+
        if (widget->id == snd_soc_dapm_adc && widget->active)
                return 1;
 
@@ -470,6 +515,9 @@ static int is_connected_input_ep(struct snd_soc_dapm_widget *widget)
        struct snd_soc_dapm_path *path;
        int con = 0;
 
+       if (widget->id == snd_soc_dapm_supply)
+               return 0;
+
        /* active stream ? */
        if (widget->id == snd_soc_dapm_dac && widget->active)
                return 1;
@@ -521,84 +569,12 @@ int dapm_reg_event(struct snd_soc_dapm_widget *w,
 }
 EXPORT_SYMBOL_GPL(dapm_reg_event);
 
-/*
- * Scan a single DAPM widget for a complete audio path and update the
- * power status appropriately.
+/* Standard power change method, used to apply power changes to most
+ * widgets.
  */
-static int dapm_power_widget(struct snd_soc_codec *codec, int event,
-                            struct snd_soc_dapm_widget *w)
+static int dapm_generic_apply_power(struct snd_soc_dapm_widget *w)
 {
-       int in, out, power_change, power, ret;
-
-       /* vmid - no action */
-       if (w->id == snd_soc_dapm_vmid)
-               return 0;
-
-       /* active ADC */
-       if (w->id == snd_soc_dapm_adc && w->active) {
-               in = is_connected_input_ep(w);
-               dapm_clear_walk(w->codec);
-               w->power = (in != 0) ? 1 : 0;
-               dapm_update_bits(w);
-               return 0;
-       }
-
-       /* active DAC */
-       if (w->id == snd_soc_dapm_dac && w->active) {
-               out = is_connected_output_ep(w);
-               dapm_clear_walk(w->codec);
-               w->power = (out != 0) ? 1 : 0;
-               dapm_update_bits(w);
-               return 0;
-       }
-
-       /* pre and post event widgets */
-       if (w->id == snd_soc_dapm_pre) {
-               if (!w->event)
-                       return 0;
-
-               if (event == SND_SOC_DAPM_STREAM_START) {
-                       ret = w->event(w,
-                                      NULL, SND_SOC_DAPM_PRE_PMU);
-                       if (ret < 0)
-                               return ret;
-               } else if (event == SND_SOC_DAPM_STREAM_STOP) {
-                       ret = w->event(w,
-                                      NULL, SND_SOC_DAPM_PRE_PMD);
-                       if (ret < 0)
-                               return ret;
-               }
-               return 0;
-       }
-       if (w->id == snd_soc_dapm_post) {
-               if (!w->event)
-                       return 0;
-
-               if (event == SND_SOC_DAPM_STREAM_START) {
-                       ret = w->event(w,
-                                      NULL, SND_SOC_DAPM_POST_PMU);
-                       if (ret < 0)
-                               return ret;
-               } else if (event == SND_SOC_DAPM_STREAM_STOP) {
-                       ret = w->event(w,
-                                      NULL, SND_SOC_DAPM_POST_PMD);
-                       if (ret < 0)
-                               return ret;
-               }
-               return 0;
-       }
-
-       /* all other widgets */
-       in = is_connected_input_ep(w);
-       dapm_clear_walk(w->codec);
-       out = is_connected_output_ep(w);
-       dapm_clear_walk(w->codec);
-       power = (out != 0 && in != 0) ? 1 : 0;
-       power_change = (w->power == power) ? 0 : 1;
-       w->power = power;
-
-       if (!power_change)
-               return 0;
+       int ret;
 
        /* call any power change event handlers */
        if (w->event)
@@ -607,7 +583,7 @@ static int dapm_power_widget(struct snd_soc_codec *codec, int event,
                         w->name, w->event_flags);
 
        /* power up pre event */
-       if (power && w->event &&
+       if (w->power && w->event &&
            (w->event_flags & SND_SOC_DAPM_PRE_PMU)) {
                ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMU);
                if (ret < 0)
@@ -615,7 +591,7 @@ static int dapm_power_widget(struct snd_soc_codec *codec, int event,
        }
 
        /* power down pre event */
-       if (!power && w->event &&
+       if (!w->power && w->event &&
            (w->event_flags & SND_SOC_DAPM_PRE_PMD)) {
                ret = w->event(w, NULL, SND_SOC_DAPM_PRE_PMD);
                if (ret < 0)
@@ -623,17 +599,17 @@ static int dapm_power_widget(struct snd_soc_codec *codec, int event,
        }
 
        /* Lower PGA volume to reduce pops */
-       if (w->id == snd_soc_dapm_pga && !power)
-               dapm_set_pga(w, power);
+       if (w->id == snd_soc_dapm_pga && !w->power)
+               dapm_set_pga(w, w->power);
 
        dapm_update_bits(w);
 
        /* Raise PGA volume to reduce pops */
-       if (w->id == snd_soc_dapm_pga && power)
-               dapm_set_pga(w, power);
+       if (w->id == snd_soc_dapm_pga && w->power)
+               dapm_set_pga(w, w->power);
 
        /* power up post event */
-       if (power && w->event &&
+       if (w->power && w->event &&
            (w->event_flags & SND_SOC_DAPM_POST_PMU)) {
                ret = w->event(w,
                               NULL, SND_SOC_DAPM_POST_PMU);
@@ -642,7 +618,7 @@ static int dapm_power_widget(struct snd_soc_codec *codec, int event,
        }
 
        /* power down post event */
-       if (!power && w->event &&
+       if (!w->power && w->event &&
            (w->event_flags & SND_SOC_DAPM_POST_PMD)) {
                ret = w->event(w, NULL, SND_SOC_DAPM_POST_PMD);
                if (ret < 0)
@@ -652,6 +628,116 @@ static int dapm_power_widget(struct snd_soc_codec *codec, int event,
        return 0;
 }
 
+/* Generic check to see if a widget should be powered.
+ */
+static int dapm_generic_check_power(struct snd_soc_dapm_widget *w)
+{
+       int in, out;
+
+       in = is_connected_input_ep(w);
+       dapm_clear_walk(w->codec);
+       out = is_connected_output_ep(w);
+       dapm_clear_walk(w->codec);
+       return out != 0 && in != 0;
+}
+
+/* Check to see if an ADC has power */
+static int dapm_adc_check_power(struct snd_soc_dapm_widget *w)
+{
+       int in;
+
+       if (w->active) {
+               in = is_connected_input_ep(w);
+               dapm_clear_walk(w->codec);
+               return in != 0;
+       } else {
+               return dapm_generic_check_power(w);
+       }
+}
+
+/* Check to see if a DAC has power */
+static int dapm_dac_check_power(struct snd_soc_dapm_widget *w)
+{
+       int out;
+
+       if (w->active) {
+               out = is_connected_output_ep(w);
+               dapm_clear_walk(w->codec);
+               return out != 0;
+       } else {
+               return dapm_generic_check_power(w);
+       }
+}
+
+/* Check to see if a power supply is needed */
+static int dapm_supply_check_power(struct snd_soc_dapm_widget *w)
+{
+       struct snd_soc_dapm_path *path;
+       int power = 0;
+
+       /* Check if one of our outputs is connected */
+       list_for_each_entry(path, &w->sinks, list_source) {
+               if (path->sink && path->sink->power_check &&
+                   path->sink->power_check(path->sink)) {
+                       power = 1;
+                       break;
+               }
+       }
+
+       dapm_clear_walk(w->codec);
+
+       return power;
+}
+
+/*
+ * Scan a single DAPM widget for a complete audio path and update the
+ * power status appropriately.
+ */
+static int dapm_power_widget(struct snd_soc_codec *codec, int event,
+                            struct snd_soc_dapm_widget *w)
+{
+       int ret;
+
+       switch (w->id) {
+       case snd_soc_dapm_pre:
+               if (!w->event)
+                       return 0;
+
+               if (event == SND_SOC_DAPM_STREAM_START) {
+                       ret = w->event(w,
+                                      NULL, SND_SOC_DAPM_PRE_PMU);
+                       if (ret < 0)
+                               return ret;
+               } else if (event == SND_SOC_DAPM_STREAM_STOP) {
+                       ret = w->event(w,
+                                      NULL, SND_SOC_DAPM_PRE_PMD);
+                       if (ret < 0)
+                               return ret;
+               }
+               return 0;
+
+       case snd_soc_dapm_post:
+               if (!w->event)
+                       return 0;
+
+               if (event == SND_SOC_DAPM_STREAM_START) {
+                       ret = w->event(w,
+                                      NULL, SND_SOC_DAPM_POST_PMU);
+                       if (ret < 0)
+                               return ret;
+               } else if (event == SND_SOC_DAPM_STREAM_STOP) {
+                       ret = w->event(w,
+                                      NULL, SND_SOC_DAPM_POST_PMD);
+                       if (ret < 0)
+                               return ret;
+               }
+               return 0;
+
+       default:
+               return dapm_generic_apply_power(w);
+       }
+}
+
 /*
  * Scan each dapm widget for complete audio path.
  * A complete path is a route that has valid endpoints i.e.:-
@@ -663,31 +749,102 @@ static int dapm_power_widget(struct snd_soc_codec *codec, int event,
  */
 static int dapm_power_widgets(struct snd_soc_codec *codec, int event)
 {
+       struct snd_soc_device *socdev = codec->socdev;
        struct snd_soc_dapm_widget *w;
-       int i, c = 1, *seq = NULL, ret = 0;
-
-       /* do we have a sequenced stream event */
-       if (event == SND_SOC_DAPM_STREAM_START) {
-               c = ARRAY_SIZE(dapm_up_seq);
-               seq = dapm_up_seq;
-       } else if (event == SND_SOC_DAPM_STREAM_STOP) {
-               c = ARRAY_SIZE(dapm_down_seq);
-               seq = dapm_down_seq;
+       int ret = 0;
+       int i, power;
+       int sys_power = 0;
+
+       INIT_LIST_HEAD(&codec->up_list);
+       INIT_LIST_HEAD(&codec->down_list);
+
+       /* Check which widgets we need to power and store them in
+        * lists indicating if they should be powered up or down.
+        */
+       list_for_each_entry(w, &codec->dapm_widgets, list) {
+               switch (w->id) {
+               case snd_soc_dapm_pre:
+                       list_add_tail(&codec->down_list, &w->power_list);
+                       break;
+               case snd_soc_dapm_post:
+                       list_add_tail(&codec->up_list, &w->power_list);
+                       break;
+
+               default:
+                       if (!w->power_check)
+                               continue;
+
+                       power = w->power_check(w);
+                       if (power)
+                               sys_power = 1;
+
+                       if (w->power == power)
+                               continue;
+
+                       if (power)
+                               list_add_tail(&w->power_list, &codec->up_list);
+                       else
+                               list_add_tail(&w->power_list,
+                                             &codec->down_list);
+
+                       w->power = power;
+                       break;
+               }
        }
 
-       for (i = 0; i < c; i++) {
-               list_for_each_entry(w, &codec->dapm_widgets, list) {
+       /* 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)) {
+               ret = snd_soc_dapm_set_bias_level(socdev,
+                                                 SND_SOC_BIAS_PREPARE);
+               if (ret != 0)
+                       pr_err("Failed to prepare bias: %d\n", ret);
+       }
 
+       /* Power down widgets first; try to avoid amplifying pops. */
+       for (i = 0; i < ARRAY_SIZE(dapm_down_seq); i++) {
+               list_for_each_entry(w, &codec->down_list, power_list) {
                        /* is widget in stream order */
-                       if (seq && seq[i] && w->id != seq[i])
+                       if (w->id != dapm_down_seq[i])
                                continue;
 
                        ret = dapm_power_widget(codec, event, w);
                        if (ret != 0)
-                               return ret;
+                               pr_err("Failed to power down %s: %d\n",
+                                      w->name, ret);
                }
        }
 
+       /* Now power up. */
+       for (i = 0; i < ARRAY_SIZE(dapm_up_seq); i++) {
+               list_for_each_entry(w, &codec->up_list, power_list) {
+                       /* is widget in stream order */
+                       if (w->id != dapm_up_seq[i])
+                               continue;
+
+                       ret = dapm_power_widget(codec, event, w);
+                       if (ret != 0)
+                               pr_err("Failed to power up %s: %d\n",
+                                      w->name, ret);
+               }
+       }
+
+       /* If we just powered the last thing off drop to standby bias */
+       if (codec->bias_level == SND_SOC_BIAS_PREPARE && !sys_power) {
+               ret = snd_soc_dapm_set_bias_level(socdev,
+                                                 SND_SOC_BIAS_STANDBY);
+               if (ret != 0)
+                       pr_err("Failed to apply standby 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,
+                                                 SND_SOC_BIAS_ON);
+               if (ret != 0)
+                       pr_err("Failed to apply active bias: %d\n", ret);
+       }
+
        return 0;
 }
 
@@ -723,6 +880,7 @@ static void dbg_dump_dapm(struct snd_soc_codec* codec, const char *action)
                case snd_soc_dapm_pga:
                case snd_soc_dapm_mixer:
                case snd_soc_dapm_mixer_named_ctl:
+               case snd_soc_dapm_supply:
                        if (w->name) {
                                in = is_connected_input_ep(w);
                                dapm_clear_walk(w->codec);
@@ -851,6 +1009,7 @@ static ssize_t dapm_widget_show(struct device *dev,
                case snd_soc_dapm_pga:
                case snd_soc_dapm_mixer:
                case snd_soc_dapm_mixer_named_ctl:
+               case snd_soc_dapm_supply:
                        if (w->name)
                                count += sprintf(buf + count, "%s: %s\n",
                                        w->name, w->power ? "On":"Off");
@@ -883,16 +1042,12 @@ static DEVICE_ATTR(dapm_widget, 0444, dapm_widget_show, NULL);
 
 int snd_soc_dapm_sys_add(struct device *dev)
 {
-       if (!dapm_status)
-               return 0;
        return device_create_file(dev, &dev_attr_dapm_widget);
 }
 
 static void snd_soc_dapm_sys_remove(struct device *dev)
 {
-       if (dapm_status) {
-               device_remove_file(dev, &dev_attr_dapm_widget);
-       }
+       device_remove_file(dev, &dev_attr_dapm_widget);
 }
 
 /* free all dapm widgets and resources */
@@ -1015,6 +1170,7 @@ static int snd_soc_dapm_add_route(struct snd_soc_codec *codec,
        case snd_soc_dapm_vmid:
        case snd_soc_dapm_pre:
        case snd_soc_dapm_post:
+       case snd_soc_dapm_supply:
                list_add(&path->list, &codec->dapm_paths);
                list_add(&path->list_sink, &wsink->sources);
                list_add(&path->list_source, &wsource->sinks);
@@ -1108,15 +1264,22 @@ int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
                case snd_soc_dapm_switch:
                case snd_soc_dapm_mixer:
                case snd_soc_dapm_mixer_named_ctl:
+                       w->power_check = dapm_generic_check_power;
                        dapm_new_mixer(codec, w);
                        break;
                case snd_soc_dapm_mux:
                case snd_soc_dapm_value_mux:
+                       w->power_check = dapm_generic_check_power;
                        dapm_new_mux(codec, w);
                        break;
                case snd_soc_dapm_adc:
+                       w->power_check = dapm_adc_check_power;
+                       break;
                case snd_soc_dapm_dac:
+                       w->power_check = dapm_dac_check_power;
+                       break;
                case snd_soc_dapm_pga:
+                       w->power_check = dapm_generic_check_power;
                        dapm_new_pga(codec, w);
                        break;
                case snd_soc_dapm_input:
@@ -1126,6 +1289,10 @@ int snd_soc_dapm_new_widgets(struct snd_soc_codec *codec)
                case snd_soc_dapm_hp:
                case snd_soc_dapm_mic:
                case snd_soc_dapm_line:
+                       w->power_check = dapm_generic_check_power;
+                       break;
+               case snd_soc_dapm_supply:
+                       w->power_check = dapm_supply_check_power;
                case snd_soc_dapm_vmid:
                case snd_soc_dapm_pre:
                case snd_soc_dapm_post:
@@ -1625,36 +1792,12 @@ int snd_soc_dapm_stream_event(struct snd_soc_codec *codec,
 }
 EXPORT_SYMBOL_GPL(snd_soc_dapm_stream_event);
 
-/**
- * snd_soc_dapm_set_bias_level - set the bias level for the system
- * @socdev: audio device
- * @level: level to configure
- *
- * Configure the bias (power) levels for the SoC audio device.
- *
- * Returns 0 for success else error.
- */
-int snd_soc_dapm_set_bias_level(struct snd_soc_device *socdev,
-                               enum snd_soc_bias_level level)
-{
-       struct snd_soc_card *card = socdev->card;
-       struct snd_soc_codec *codec = socdev->card->codec;
-       int ret = 0;
-
-       if (card->set_bias_level)
-               ret = card->set_bias_level(card, level);
-       if (ret == 0 && codec->set_bias_level)
-               ret = codec->set_bias_level(codec, level);
-
-       return ret;
-}
-
 /**
  * snd_soc_dapm_enable_pin - enable pin.
  * @codec: SoC codec
  * @pin: pin name
  *
- * Enables input/output pin and it's parents or children widgets iff there is
+ * Enables input/output pin and its parents or children widgets iff there is
  * a valid audio route and active audio stream.
  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  * do any widget power switching.
@@ -1670,7 +1813,7 @@ EXPORT_SYMBOL_GPL(snd_soc_dapm_enable_pin);
  * @codec: SoC codec
  * @pin: pin name
  *
- * Disables input/output pin and it's parents or children widgets.
+ * Disables input/output pin and its parents or children widgets.
  * NOTE: snd_soc_dapm_sync() needs to be called after this for DAPM to
  * do any widget power switching.
  */