usb: chipidea: support runtime power management for otg fsm mode

This patch adds runtime power management support for otg fsm mode, since
A-device in a_idle state cannot detect data pulse irq after suspended, here
enable wakeup by connection before suspend to make it can be resumed by DP;
and handle wakeup from that state like SRP.

Signed-off-by: Li Jun <jun.li@freescale.com>
Signed-off-by: Peter Chen <peter.chen@freescale.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
diff --git a/drivers/usb/chipidea/otg_fsm.c b/drivers/usb/chipidea/otg_fsm.c
index 562e581..e3cf5be 100644
--- a/drivers/usb/chipidea/otg_fsm.c
+++ b/drivers/usb/chipidea/otg_fsm.c
@@ -225,6 +225,9 @@
 			return;
 		}
 
+	if (list_empty(active_timers))
+		pm_runtime_get(ci->dev);
+
 	timer->count = timer->expires;
 	list_add_tail(&timer->list, active_timers);
 
@@ -241,17 +244,22 @@
 	struct ci_otg_fsm_timer *tmp_timer, *del_tmp;
 	struct ci_otg_fsm_timer *timer = ci->fsm_timer->timer_list[t];
 	struct list_head *active_timers = &ci->fsm_timer->active_timers;
+	int flag = 0;
 
 	if (t >= NUM_CI_OTG_FSM_TIMERS)
 		return;
 
 	list_for_each_entry_safe(tmp_timer, del_tmp, active_timers, list)
-		if (tmp_timer == timer)
+		if (tmp_timer == timer) {
 			list_del(&timer->list);
+			flag = 1;
+		}
 
 	/* Disable 1ms irq if there is no any active timer */
-	if (list_empty(active_timers))
+	if (list_empty(active_timers) && (flag == 1)) {
 		hw_write_otgsc(ci, OTGSC_1MSIE, 0);
+		pm_runtime_put(ci->dev);
+	}
 }
 
 /*
@@ -275,8 +283,10 @@
 	}
 
 	/* disable 1ms irq if there is no any timer active */
-	if ((expired == 1) && list_empty(active_timers))
+	if ((expired == 1) && list_empty(active_timers)) {
 		hw_write_otgsc(ci, OTGSC_1MSIE, 0);
+		pm_runtime_put(ci->dev);
+	}
 
 	return expired;
 }
@@ -585,6 +595,7 @@
 		ci->fsm.otg->state < OTG_STATE_A_IDLE)
 		return 0;
 
+	pm_runtime_get_sync(ci->dev);
 	if (otg_statemachine(&ci->fsm)) {
 		if (ci->fsm.otg->state == OTG_STATE_A_IDLE) {
 			/*
@@ -609,8 +620,13 @@
 				 */
 				ci_otg_queue_work(ci);
 			}
+		} else if (ci->fsm.otg->state == OTG_STATE_A_HOST) {
+			pm_runtime_mark_last_busy(ci->dev);
+			pm_runtime_put_autosuspend(ci->dev);
+			return 0;
 		}
 	}
+	pm_runtime_put_sync(ci->dev);
 	return 0;
 }