From 08fa20ae20eb378225e5519db4e07f663ce405fa Mon Sep 17 00:00:00 2001
From: Takashi Iwai <tiwai@suse.de>
Date: Fri, 31 Aug 2012 07:46:56 -0700
Subject: [PATCH] ALSA: hda - Yet another fix for D3 stop-clock refcounting
Git-commit: 08fa20ae20eb378225e5519db4e07f663ce405fa
Patch-mainline: 3.7-rc1
References: FATE#313754

The call of pm_notify callback in snd_hda_codec_free() should be with
the check of the current state whether pm_notify(false) is called or
not, instead of codec->power_on check.

For improving the code readability and fixing this inconsistency,
codec->d3_stop_clk_ok is renamed to codec->pm_down_notified, and this
flag is set only when runtime PM down is called.  The new name reflects
to a more direct purpose of the flag.

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

---
 sound/pci/hda/hda_codec.c |   41 ++++++++++++++++++++++-------------------
 sound/pci/hda/hda_codec.h |    2 +-
 2 files changed, 23 insertions(+), 20 deletions(-)

--- a/sound/pci/hda/hda_codec.c
+++ b/sound/pci/hda/hda_codec.c
@@ -1205,7 +1205,7 @@ static void snd_hda_codec_free(struct hd
 	if (codec->patch_ops.free)
 		codec->patch_ops.free(codec);
 #ifdef CONFIG_PM
-	if (codec->power_on)
+	if (!codec->pm_down_notified) /* cancel leftover refcounts */
 		hda_call_pm_notify(codec->bus, false);
 #endif
 	module_put(codec->owner);
@@ -1221,7 +1221,7 @@ static void snd_hda_codec_free(struct hd
 static bool snd_hda_codec_get_supported_ps(struct hda_codec *codec,
 				hda_nid_t fg, unsigned int power_state);
 
-static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
+static unsigned int hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
 				unsigned int power_state);
 
 /**
@@ -3571,18 +3571,14 @@ static unsigned int hda_sync_power_state
 }
 
 /*
- * set power state of the codec
+ * set power state of the codec, and return the power state
  */
-static void hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
-				unsigned int power_state)
+static unsigned int hda_set_power_state(struct hda_codec *codec, hda_nid_t fg,
+					unsigned int power_state)
 {
 	int count;
 	unsigned int state;
 
-#ifdef CONFIG_PM
-	codec->d3_stop_clk_ok = 0;
-#endif
-
 	/* this delay seems necessary to avoid click noise at power-down */
 	if (power_state == AC_PWRST_D3) {
 		/* transition time less than 10ms for power down */
@@ -3606,11 +3602,7 @@ static void hda_set_power_state(struct h
 			break;
 	}
 
-#ifdef CONFIG_PM
-	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
+	return state;
 }
 
 #ifdef CONFIG_SND_HDA_HWDEP
@@ -3627,13 +3619,16 @@ static inline void hda_exec_init_verbs(s
 #ifdef CONFIG_PM
 /*
  * call suspend and power-down; used both from PM and power-save
+ * this function returns the power state in the end
  */
-static void hda_call_codec_suspend(struct hda_codec *codec)
+static unsigned int hda_call_codec_suspend(struct hda_codec *codec)
 {
+	unsigned int state;
+
 	if (codec->patch_ops.suspend)
 		codec->patch_ops.suspend(codec);
 	hda_cleanup_all_streams(codec);
-	hda_set_power_state(codec,
+	state = hda_set_power_state(codec,
 			    codec->afg ? codec->afg : codec->mfg,
 			    AC_PWRST_D3);
 	cancel_delayed_work(&codec->power_work);
@@ -3644,6 +3639,7 @@ static void hda_call_codec_suspend(struc
 	codec->power_transition = 0;
 	codec->power_jiffies = jiffies;
 	spin_unlock(&codec->power_lock);
+	return state;
 }
 
 /*
@@ -4445,6 +4441,7 @@ static void hda_power_work(struct work_s
 	struct hda_codec *codec =
 		container_of(work, struct hda_codec, power_work.work);
 	struct hda_bus *bus = codec->bus;
+	unsigned int state;
 
 	spin_lock(&codec->power_lock);
 	if (codec->power_transition > 0) { /* during power-up sequence? */
@@ -4458,9 +4455,12 @@ static void hda_power_work(struct work_s
 	}
 	spin_unlock(&codec->power_lock);
 
-	hda_call_codec_suspend(codec);
-	if (codec->d3_stop_clk_ok)
+	state = hda_call_codec_suspend(codec);
+	codec->pm_down_notified = 0;
+	if (!bus->power_keep_link_on && (state & AC_PWRST_CLK_STOP_OK)) {
+		codec->pm_down_notified = 1;
 		hda_call_pm_notify(bus, false);
+	}
 }
 
 static void hda_keep_power_on(struct hda_codec *codec)
@@ -4517,8 +4517,11 @@ static void __snd_hda_power_up(struct hd
 	codec->power_transition = 1; /* avoid reentrance */
 	spin_unlock(&codec->power_lock);
 
-	if (codec->d3_stop_clk_ok) /* flag set at suspend */
+	if (codec->pm_down_notified) {
+		codec->pm_down_notified = 0;
 		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
@@ -868,7 +868,7 @@ struct hda_codec {
 #ifdef CONFIG_PM
 	unsigned int power_on :1;	/* current (global) power-state */
 	unsigned int d3_stop_clk:1;	/* support D3 operation without BCLK */
-	unsigned int d3_stop_clk_ok:1; /* BCLK can stop */
+	unsigned int pm_down_notified:1; /* PM notified to controller */
 	int power_transition;	/* power-state in transition */
 	int power_count;	/* current (global) power refcount */
 	struct delayed_work power_work; /* delayed task for powerdown */
