Merge git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi-rc-fixes-2.6
[cascardo/linux.git] / sound / pci / hda / hda_codec.c
index 0435293..8cbe3bf 100644 (file)
 #include "hda_local.h"
 #include <sound/hda_hwdep.h>
 
+#ifdef CONFIG_SND_HDA_POWER_SAVE
+/* define this option here to hide as static */
+static int power_save = CONFIG_SND_HDA_POWER_SAVE_DEFAULT;
+module_param(power_save, int, 0644);
+MODULE_PARM_DESC(power_save, "Automatic power-saving timeout "
+                "(in second, 0 = disable).");
+#endif
 
 /*
  * vendor / preset table
@@ -60,6 +67,13 @@ static struct hda_vendor_id hda_vendor_ids[] = {
 #include "hda_patch.h"
 
 
+#ifdef CONFIG_SND_HDA_POWER_SAVE
+static void hda_power_work(struct work_struct *work);
+static void hda_keep_power_on(struct hda_codec *codec);
+#else
+static inline void hda_keep_power_on(struct hda_codec *codec) {}
+#endif
+
 /**
  * snd_hda_codec_read - send a command and get the response
  * @codec: the HDA codec
@@ -77,12 +91,14 @@ unsigned int snd_hda_codec_read(struct hda_codec *codec, hda_nid_t nid,
                                unsigned int verb, unsigned int parm)
 {
        unsigned int res;
+       snd_hda_power_up(codec);
        mutex_lock(&codec->bus->cmd_mutex);
        if (!codec->bus->ops.command(codec, nid, direct, verb, parm))
                res = codec->bus->ops.get_response(codec);
        else
                res = (unsigned int)-1;
        mutex_unlock(&codec->bus->cmd_mutex);
+       snd_hda_power_down(codec);
        return res;
 }
 
@@ -102,9 +118,11 @@ int snd_hda_codec_write(struct hda_codec *codec, hda_nid_t nid, int direct,
                         unsigned int verb, unsigned int parm)
 {
        int err;
+       snd_hda_power_up(codec);
        mutex_lock(&codec->bus->cmd_mutex);
        err = codec->bus->ops.command(codec, nid, direct, verb, parm);
        mutex_unlock(&codec->bus->cmd_mutex);
+       snd_hda_power_down(codec);
        return err;
 }
 
@@ -137,6 +155,8 @@ int snd_hda_get_sub_nodes(struct hda_codec *codec, hda_nid_t nid,
        unsigned int parm;
 
        parm = snd_hda_param_read(codec, nid, AC_PAR_NODE_COUNT);
+       if (parm == -1)
+               return 0;
        *start_id = (parm >> 16) & 0x7fff;
        return (int)(parm & 0x7fff);
 }
@@ -496,7 +516,7 @@ static int read_widget_caps(struct hda_codec *codec, hda_nid_t fg_node)
 
 static void init_hda_cache(struct hda_cache_rec *cache,
                           unsigned int record_size);
-static inline void free_hda_cache(struct hda_cache_rec *cache);
+static void free_hda_cache(struct hda_cache_rec *cache);
 
 /*
  * codec destructor
@@ -505,6 +525,10 @@ static void snd_hda_codec_free(struct hda_codec *codec)
 {
        if (!codec)
                return;
+#ifdef CONFIG_SND_HDA_POWER_SAVE
+       cancel_delayed_work(&codec->power_work);
+       flush_scheduled_work();
+#endif
        list_del(&codec->list);
        codec->bus->caddr_tbl[codec->addr] = NULL;
        if (codec->patch_ops.free)
@@ -551,6 +575,15 @@ int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
        init_hda_cache(&codec->amp_cache, sizeof(struct hda_amp_info));
        init_hda_cache(&codec->cmd_cache, sizeof(struct hda_cache_head));
 
+#ifdef CONFIG_SND_HDA_POWER_SAVE
+       INIT_DELAYED_WORK(&codec->power_work, hda_power_work);
+       /* snd_hda_codec_new() marks the codec as power-up, and leave it as is.
+        * the caller has to power down appropriatley after initialization
+        * phase.
+        */
+       hda_keep_power_on(codec);
+#endif
+
        list_add_tail(&codec->list, &bus->codec_list);
        bus->caddr_tbl[codec_addr] = codec;
 
@@ -593,24 +626,19 @@ int __devinit snd_hda_codec_new(struct hda_bus *bus, unsigned int codec_addr,
                snd_hda_get_codec_name(codec, bus->card->mixername,
                                       sizeof(bus->card->mixername));
 
-#ifdef CONFIG_SND_HDA_GENERIC
        if (is_generic_config(codec)) {
                err = snd_hda_parse_generic_codec(codec);
                goto patched;
        }
-#endif
        if (codec->preset && codec->preset->patch) {
                err = codec->preset->patch(codec);
                goto patched;
        }
 
        /* call the default parser */
-#ifdef CONFIG_SND_HDA_GENERIC
        err = snd_hda_parse_generic_codec(codec);
-#else
-       printk(KERN_ERR "hda-codec: No codec parser is available\n");
-       err = -ENODEV;
-#endif
+       if (err < 0)
+               printk(KERN_ERR "hda-codec: No codec parser is available\n");
 
  patched:
        if (err < 0) {
@@ -676,7 +704,7 @@ static void __devinit init_hda_cache(struct hda_cache_rec *cache,
        cache->record_size = record_size;
 }
 
-static inline void free_hda_cache(struct hda_cache_rec *cache)
+static void free_hda_cache(struct hda_cache_rec *cache)
 {
        kfree(cache->buffer);
 }
@@ -855,7 +883,7 @@ int snd_hda_codec_amp_stereo(struct hda_codec *codec, hda_nid_t nid,
        return ret;
 }
 
-#ifdef CONFIG_PM
+#ifdef SND_HDA_NEEDS_RESUME
 /* resume the all amp commands from the cache */
 void snd_hda_codec_resume_amp(struct hda_codec *codec)
 {
@@ -879,7 +907,7 @@ void snd_hda_codec_resume_amp(struct hda_codec *codec)
                }
        }
 }
-#endif /* CONFIG_PM */
+#endif /* SND_HDA_NEEDS_RESUME */
 
 /*
  * AMP control callbacks
@@ -945,6 +973,7 @@ int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
        long *valp = ucontrol->value.integer.value;
        int change = 0;
 
+       snd_hda_power_up(codec);
        if (chs & 1) {
                change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
                                                  0x7f, *valp);
@@ -953,6 +982,7 @@ int snd_hda_mixer_amp_volume_put(struct snd_kcontrol *kcontrol,
        if (chs & 2)
                change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
                                                   0x7f, *valp);
+       snd_hda_power_down(codec);
        return change;
 }
 
@@ -1025,6 +1055,7 @@ int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
        long *valp = ucontrol->value.integer.value;
        int change = 0;
 
+       snd_hda_power_up(codec);
        if (chs & 1) {
                change = snd_hda_codec_amp_update(codec, nid, 0, dir, idx,
                                                  HDA_AMP_MUTE,
@@ -1035,7 +1066,11 @@ int snd_hda_mixer_amp_switch_put(struct snd_kcontrol *kcontrol,
                change |= snd_hda_codec_amp_update(codec, nid, 1, dir, idx,
                                                   HDA_AMP_MUTE,
                                                   *valp ? 0 : HDA_AMP_MUTE);
-       
+#ifdef CONFIG_SND_HDA_POWER_SAVE
+       if (codec->patch_ops.check_power_status)
+               codec->patch_ops.check_power_status(codec, nid);
+#endif
+       snd_hda_power_down(codec);
        return change;
 }
 
@@ -1502,7 +1537,7 @@ int snd_hda_create_spdif_in_ctls(struct hda_codec *codec, hda_nid_t nid)
        return 0;
 }
 
-#ifdef CONFIG_PM
+#ifdef SND_HDA_NEEDS_RESUME
 /*
  * command cache
  */
@@ -1528,6 +1563,7 @@ int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
                              int direct, unsigned int verb, unsigned int parm)
 {
        int err;
+       snd_hda_power_up(codec);
        mutex_lock(&codec->bus->cmd_mutex);
        err = codec->bus->ops.command(codec, nid, direct, verb, parm);
        if (!err) {
@@ -1538,6 +1574,7 @@ int snd_hda_codec_write_cache(struct hda_codec *codec, hda_nid_t nid,
                        c->val = parm;
        }
        mutex_unlock(&codec->bus->cmd_mutex);
+       snd_hda_power_down(codec);
        return err;
 }
 
@@ -1572,7 +1609,7 @@ void snd_hda_sequence_write_cache(struct hda_codec *codec,
                snd_hda_codec_write_cache(codec, seq->nid, 0, seq->verb,
                                          seq->param);
 }
-#endif /* CONFIG_PM */
+#endif /* SND_HDA_NEEDS_RESUME */
 
 /*
  * set power state of the codec
@@ -1580,23 +1617,93 @@ void snd_hda_sequence_write_cache(struct hda_codec *codec,
 static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
                                unsigned int power_state)
 {
-       hda_nid_t nid, nid_start;
-       int nodes;
+       hda_nid_t nid;
+       int i;
 
        snd_hda_codec_write(codec, fg, 0, AC_VERB_SET_POWER_STATE,
                            power_state);
 
-       nodes = snd_hda_get_sub_nodes(codec, fg, &nid_start);
-       for (nid = nid_start; nid < nodes + nid_start; nid++) {
-               if (get_wcaps(codec, nid) & AC_WCAP_POWER)
+       nid = codec->start_nid;
+       for (i = 0; i < codec->num_nodes; i++, nid++) {
+               unsigned int wcaps = get_wcaps(codec, nid);
+               if (wcaps & AC_WCAP_POWER) {
+                       unsigned int wid_type = (wcaps & AC_WCAP_TYPE) >>
+                               AC_WCAP_TYPE_SHIFT;
+                       if (wid_type == AC_WID_PIN) {
+                               unsigned int pincap;
+                               /*
+                                * don't power down the widget if it controls
+                                * eapd and EAPD_BTLENABLE is set.
+                                */
+                               pincap = snd_hda_param_read(codec, nid,
+                                                           AC_PAR_PIN_CAP);
+                               if (pincap & AC_PINCAP_EAPD) {
+                                       int eapd = snd_hda_codec_read(codec,
+                                               nid, 0,
+                                               AC_VERB_GET_EAPD_BTLENABLE, 0);
+                                       eapd &= 0x02;
+                                       if (power_state == AC_PWRST_D3 && eapd)
+                                               continue;
+                               }
+                       }
                        snd_hda_codec_write(codec, nid, 0,
                                            AC_VERB_SET_POWER_STATE,
                                            power_state);
+               }
        }
 
-       if (power_state == AC_PWRST_D0)
+       if (power_state == AC_PWRST_D0) {
+               unsigned long end_time;
+               int state;
                msleep(10);
+               /* wait until the codec reachs to D0 */
+               end_time = jiffies + msecs_to_jiffies(500);
+               do {
+                       state = snd_hda_codec_read(codec, fg, 0,
+                                                  AC_VERB_GET_POWER_STATE, 0);
+                       if (state == power_state)
+                               break;
+                       msleep(1);
+               } while (time_after_eq(end_time, jiffies));
+       }
+}
+
+#ifdef SND_HDA_NEEDS_RESUME
+/*
+ * call suspend and power-down; used both from PM and power-save
+ */
+static void hda_call_codec_suspend(struct hda_codec *codec)
+{
+       if (codec->patch_ops.suspend)
+               codec->patch_ops.suspend(codec, PMSG_SUSPEND);
+       hda_set_power_state(codec,
+                           codec->afg ? codec->afg : codec->mfg,
+                           AC_PWRST_D3);
+#ifdef CONFIG_SND_HDA_POWER_SAVE
+       cancel_delayed_work(&codec->power_work);
+       codec->power_on = 0;
+       codec->power_transition = 0;
+#endif
+}
+
+/*
+ * kick up codec; used both from PM and power-save
+ */
+static void hda_call_codec_resume(struct hda_codec *codec)
+{
+       hda_set_power_state(codec,
+                           codec->afg ? codec->afg : codec->mfg,
+                           AC_PWRST_D0);
+       if (codec->patch_ops.resume)
+               codec->patch_ops.resume(codec);
+       else {
+               if (codec->patch_ops.init)
+                       codec->patch_ops.init(codec);
+               snd_hda_codec_resume_amp(codec);
+               snd_hda_codec_resume_cache(codec);
+       }
 }
+#endif /* SND_HDA_NEEDS_RESUME */
 
 
 /**
@@ -1611,28 +1718,24 @@ int __devinit snd_hda_build_controls(struct hda_bus *bus)
 {
        struct hda_codec *codec;
 
-       /* build controls */
        list_for_each_entry(codec, &bus->codec_list, list) {
-               int err;
-               if (!codec->patch_ops.build_controls)
-                       continue;
-               err = codec->patch_ops.build_controls(codec);
-               if (err < 0)
-                       return err;
-       }
-
-       /* initialize */
-       list_for_each_entry(codec, &bus->codec_list, list) {
-               int err;
+               int err = 0;
+               /* fake as if already powered-on */
+               hda_keep_power_on(codec);
+               /* then fire up */
                hda_set_power_state(codec,
                                    codec->afg ? codec->afg : codec->mfg,
                                    AC_PWRST_D0);
-               if (!codec->patch_ops.init)
-                       continue;
-               err = codec->patch_ops.init(codec);
+               /* continue to initialize... */
+               if (codec->patch_ops.init)
+                       err = codec->patch_ops.init(codec);
+               if (!err && codec->patch_ops.build_controls)
+                       err = codec->patch_ops.build_controls(codec);
+               snd_hda_power_down(codec);
                if (err < 0)
                        return err;
        }
+
        return 0;
 }
 
@@ -2078,7 +2181,7 @@ int snd_hda_check_board_config(struct hda_codec *codec,
  */
 int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
 {
-       int err;
+       int err;
 
        for (; knew->name; knew++) {
                struct snd_kcontrol *kctl;
@@ -2101,6 +2204,93 @@ int snd_hda_add_new_ctls(struct hda_codec *codec, struct snd_kcontrol_new *knew)
        return 0;
 }
 
+#ifdef CONFIG_SND_HDA_POWER_SAVE
+static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
+                               unsigned int power_state);
+
+static void hda_power_work(struct work_struct *work)
+{
+       struct hda_codec *codec =
+               container_of(work, struct hda_codec, power_work.work);
+
+       if (!codec->power_on || codec->power_count) {
+               codec->power_transition = 0;
+               return;
+       }
+
+       hda_call_codec_suspend(codec);
+       if (codec->bus->ops.pm_notify)
+               codec->bus->ops.pm_notify(codec);
+}
+
+static void hda_keep_power_on(struct hda_codec *codec)
+{
+       codec->power_count++;
+       codec->power_on = 1;
+}
+
+void snd_hda_power_up(struct hda_codec *codec)
+{
+       codec->power_count++;
+       if (codec->power_on || codec->power_transition)
+               return;
+
+       codec->power_on = 1;
+       if (codec->bus->ops.pm_notify)
+               codec->bus->ops.pm_notify(codec);
+       hda_call_codec_resume(codec);
+       cancel_delayed_work(&codec->power_work);
+       codec->power_transition = 0;
+}
+
+void snd_hda_power_down(struct hda_codec *codec)
+{
+       --codec->power_count;
+       if (!codec->power_on || codec->power_count || codec->power_transition)
+               return;
+       if (power_save) {
+               codec->power_transition = 1; /* avoid reentrance */
+               schedule_delayed_work(&codec->power_work,
+                                     msecs_to_jiffies(power_save * 1000));
+       }
+}
+
+int snd_hda_check_amp_list_power(struct hda_codec *codec,
+                                struct hda_loopback_check *check,
+                                hda_nid_t nid)
+{
+       struct hda_amp_list *p;
+       int ch, v;
+
+       if (!check->amplist)
+               return 0;
+       for (p = check->amplist; p->nid; p++) {
+               if (p->nid == nid)
+                       break;
+       }
+       if (!p->nid)
+               return 0; /* nothing changed */
+
+       for (p = check->amplist; p->nid; p++) {
+               for (ch = 0; ch < 2; ch++) {
+                       v = snd_hda_codec_amp_read(codec, p->nid, ch, p->dir,
+                                                  p->idx);
+                       if (!(v & HDA_AMP_MUTE) && v > 0) {
+                               if (!check->power_on) {
+                                       check->power_on = 1;
+                                       snd_hda_power_up(codec);
+                               }
+                               return 1;
+                       }
+               }
+       }
+       if (check->power_on) {
+               check->power_on = 0;
+               snd_hda_power_down(codec);
+       }
+       return 0;
+}
+#endif
 
 /*
  * Channel mode helper
@@ -2167,6 +2357,8 @@ int snd_hda_input_mux_info(const struct hda_input_mux *imux,
        uinfo->type = SNDRV_CTL_ELEM_TYPE_ENUMERATED;
        uinfo->count = 1;
        uinfo->value.enumerated.items = imux->num_items;
+       if (!imux->num_items)
+               return 0;
        index = uinfo->value.enumerated.item;
        if (index >= imux->num_items)
                index = imux->num_items - 1;
@@ -2182,6 +2374,8 @@ int snd_hda_input_mux_put(struct hda_codec *codec,
 {
        unsigned int idx;
 
+       if (!imux->num_items)
+               return 0;
        idx = ucontrol->value.enumerated.item[0];
        if (idx >= imux->num_items)
                idx = imux->num_items - 1;
@@ -2298,13 +2492,14 @@ int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
        /* front */
        snd_hda_codec_setup_stream(codec, nids[HDA_FRONT], stream_tag,
                                   0, format);
-       if (mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
+       if (!mout->no_share_stream &&
+           mout->hp_nid && mout->hp_nid != nids[HDA_FRONT])
                /* headphone out will just decode front left/right (stereo) */
                snd_hda_codec_setup_stream(codec, mout->hp_nid, stream_tag,
                                           0, format);
        /* extra outputs copied from front */
        for (i = 0; i < ARRAY_SIZE(mout->extra_out_nid); i++)
-               if (mout->extra_out_nid[i])
+               if (!mout->no_share_stream && mout->extra_out_nid[i])
                        snd_hda_codec_setup_stream(codec,
                                                   mout->extra_out_nid[i],
                                                   stream_tag, 0, format);
@@ -2314,7 +2509,7 @@ int snd_hda_multi_out_analog_prepare(struct hda_codec *codec,
                if (chs >= (i + 1) * 2) /* independent out */
                        snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
                                                   i * 2, format);
-               else /* copy front */
+               else if (!mout->no_share_stream) /* copy front */
                        snd_hda_codec_setup_stream(codec, nids[i], stream_tag,
                                                   0, format);
        }
@@ -2605,13 +2800,12 @@ int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
 {
        struct hda_codec *codec;
 
-       /* FIXME: should handle power widget capabilities */
        list_for_each_entry(codec, &bus->codec_list, list) {
-               if (codec->patch_ops.suspend)
-                       codec->patch_ops.suspend(codec, state);
-               hda_set_power_state(codec,
-                                   codec->afg ? codec->afg : codec->mfg,
-                                   AC_PWRST_D3);
+#ifdef CONFIG_SND_HDA_POWER_SAVE
+               if (!codec->power_on)
+                       continue;
+#endif
+               hda_call_codec_suspend(codec);
        }
        return 0;
 }
@@ -2622,24 +2816,30 @@ int snd_hda_suspend(struct hda_bus *bus, pm_message_t state)
  * @state: resume state
  *
  * Returns 0 if successful.
+ *
+ * This fucntion is defined only when POWER_SAVE isn't set.
+ * In the power-save mode, the codec is resumed dynamically.
  */
 int snd_hda_resume(struct hda_bus *bus)
 {
        struct hda_codec *codec;
 
        list_for_each_entry(codec, &bus->codec_list, list) {
-               hda_set_power_state(codec,
-                                   codec->afg ? codec->afg : codec->mfg,
-                                   AC_PWRST_D0);
-               if (codec->patch_ops.resume)
-                       codec->patch_ops.resume(codec);
-               else {
-                       codec->patch_ops.init(codec);
-                       snd_hda_codec_resume_amp(codec);
-                       snd_hda_codec_resume_cache(codec);
-               }
+               if (snd_hda_codec_needs_resume(codec))
+                       hda_call_codec_resume(codec);
        }
        return 0;
 }
+#ifdef CONFIG_SND_HDA_POWER_SAVE
+int snd_hda_codecs_inuse(struct hda_bus *bus)
+{
+       struct hda_codec *codec;
 
+       list_for_each_entry(codec, &bus->codec_list, list) {
+               if (snd_hda_codec_needs_resume(codec))
+                       return 1;
+       }
+       return 0;
+}
+#endif
 #endif