From 68467f51c1b578ad98593bf5dee4337bd8d7798d Mon Sep 17 00:00:00 2001
From: Takashi Iwai <tiwai@suse.de>
Date: Tue, 28 Aug 2012 09:14:29 -0700
Subject: [PATCH] ALSA: hda - Fix runtime PM leftover refcounts
Git-commit: 68467f51c1b578ad98593bf5dee4337bd8d7798d
Patch-mainline: 3.7-rc1
References: FATE#313754

When the HD-audio is removed, it leaves the refcounts when codecs are
powered up (usually yes) in the destructor.  For fixing the unbalance,
and cleaning up the code mess, this patch changes the following:
- change pm_notify callback to take the explicit power on/off state,
- check of D3 stop-clock and keep_link_on flags is moved to the caller
  side,
- call pm_notify callback in snd_hda_codec_new() and snd_hda_codec_free()
  so that the refcounts are proprely updated.

Signed-off-by: Takashi Iwai <tiwai@suse.de>

---
 sound/pci/hda/hda_codec.c |   21 ++++++++++++++++-----
 sound/pci/hda/hda_codec.h |    2 +-
 sound/pci/hda/hda_intel.c |   19 +++----------------
 3 files changed, 20 insertions(+), 22 deletions(-)

--- a/sound/pci/hda/hda_codec.c
+++ b/sound/pci/hda/hda_codec.c
@@ -97,9 +97,15 @@ EXPORT_SYMBOL_HDA(snd_hda_delete_codec_p
 static void hda_power_work(struct work_struct *work);
 static void hda_keep_power_on(struct hda_codec *codec);
 #define hda_codec_is_power_on(codec)	((codec)->power_on)
+static inline void hda_call_pm_notify(struct hda_bus *bus, bool power_up)
+{
+	if (bus->ops.pm_notify)
+		bus->ops.pm_notify(bus, power_up);
+}
 #else
 static inline void hda_keep_power_on(struct hda_codec *codec) {}
 #define hda_codec_is_power_on(codec)	1
+#define hda_call_pm_notify(bus, state) {}
 #endif
 
 /**
@@ -1198,6 +1204,10 @@ static void snd_hda_codec_free(struct hd
 	codec->bus->caddr_tbl[codec->addr] = NULL;
 	if (codec->patch_ops.free)
 		codec->patch_ops.free(codec);
+#ifdef CONFIG_SND_HDA_POWER_SAVE
+	if (codec->power_on)
+		hda_call_pm_notify(codec->bus, false);
+#endif
 	module_put(codec->owner);
 	free_hda_cache(&codec->amp_cache);
 	free_hda_cache(&codec->cmd_cache);
@@ -1270,6 +1280,7 @@ int /*__devinit*/ snd_hda_codec_new(stru
 	 * phase.
 	 */
 	hda_keep_power_on(codec);
+	hda_call_pm_notify(bus, true);
 #endif
 
 	if (codec->bus->modelname) {
@@ -3583,7 +3594,7 @@ static void hda_set_power_state(struct h
 	}
 
 #ifdef CONFIG_SND_HDA_POWER_SAVE
-	if ((power_state == AC_PWRST_D3)
+	if (!codec->bus->power_keep_link_on && power_state == AC_PWRST_D3
 		&& codec->d3_stop_clk && (state & AC_PWRST_CLK_STOP_OK))
 		codec->d3_stop_clk_ok = 1;
 #endif
@@ -4437,8 +4448,8 @@ static void hda_power_work(struct work_s
 	spin_unlock(&codec->power_lock);
 
 	hda_call_codec_suspend(codec);
-	if (bus->ops.pm_notify)
-		bus->ops.pm_notify(bus, codec);
+	if (codec->d3_stop_clk_ok)
+		hda_call_pm_notify(bus, false);
 }
 
 static void hda_keep_power_on(struct hda_codec *codec)
@@ -4495,8 +4506,8 @@ static void __snd_hda_power_up(struct hd
 	codec->power_transition = 1; /* avoid reentrance */
 	spin_unlock(&codec->power_lock);
 
-	if (bus->ops.pm_notify)
-		bus->ops.pm_notify(bus, codec);
+	if (codec->d3_stop_clk_ok) /* flag set at suspend */
+		hda_call_pm_notify(bus, true);
 	hda_call_codec_resume(codec);
 
 	spin_lock(&codec->power_lock);
--- a/sound/pci/hda/hda_codec.h
+++ b/sound/pci/hda/hda_codec.h
@@ -616,7 +616,7 @@ struct hda_bus_ops {
 	void (*bus_reset)(struct hda_bus *bus);
 #ifdef CONFIG_SND_HDA_POWER_SAVE
 	/* notify power-up/down from codec to controller */
-	void (*pm_notify)(struct hda_bus *bus, struct hda_codec *codec);
+	void (*pm_notify)(struct hda_bus *bus, bool power_up);
 #endif
 };
 
--- a/sound/pci/hda/hda_intel.c
+++ b/sound/pci/hda/hda_intel.c
@@ -992,7 +992,7 @@ static unsigned int azx_get_response(str
 }
 
 #ifdef CONFIG_SND_HDA_POWER_SAVE
-static void azx_power_notify(struct hda_bus *bus, struct hda_codec *codec);
+static void azx_power_notify(struct hda_bus *bus, bool power_up);
 #endif
 
 /* reset codec link */
@@ -2360,14 +2360,11 @@ static void azx_stop_chip(struct azx *ch
 
 #ifdef CONFIG_SND_HDA_POWER_SAVE
 /* power-up/down the controller */
-static void azx_power_notify(struct hda_bus *bus, struct hda_codec *codec)
+static void azx_power_notify(struct hda_bus *bus, bool power_up)
 {
 	struct azx *chip = bus->private_data;
 
-	if (bus->power_keep_link_on || !codec->d3_stop_clk_ok)
-		return;
-
-	if (codec->power_on)
+	if (power_up)
 		pm_runtime_get_sync(&chip->pci->dev);
 	else
 		pm_runtime_put_sync(&chip->pci->dev);
@@ -3021,15 +3018,6 @@ static void power_down_all_codecs(struct
 #endif
 }
 
-static void rpm_get_all_codecs(struct azx *chip)
-{
-	struct hda_codec *codec;
-
-	list_for_each_entry(codec, &chip->bus->codec_list, list) {
-		pm_runtime_get_noresume(&chip->pci->dev);
-	}
-}
-
 static int __devinit azx_probe(struct pci_dev *pci,
 			       const struct pci_device_id *pci_id)
 {
@@ -3098,7 +3086,6 @@ static int __devinit azx_probe(struct pc
 
 	pci_set_drvdata(pci, card);
 	chip->running = 1;
-	rpm_get_all_codecs(chip); /* all codecs are active */
 	power_down_all_codecs(chip);
 	azx_notifier_register(chip);
 	azx_add_card_list(chip);
