[PATCH 1/3] usb: phy: introduce usb_phy_set_event interface

2014-11-19 Thread Kiran Kumar Raparthy
From: Todd Poynor toddpoy...@google.com

usb: phy: introduce usb_phy_set_event interface

PHY drivers require a generic interface to handle per-PHY events.

usb_phy_set_event interface sets event to phy event.
PHY drivers call this interface for each phy event.

Cc: Felipe Balbi ba...@ti.com
Cc: Greg Kroah-Hartman gre...@linuxfoundation.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-usb@vger.kernel.org
Cc: Android Kernel Team kernel-t...@android.com
Cc: John Stultz john.stu...@linaro.org
Cc: Sumit Semwal sumit.sem...@linaro.org
Cc: Arve Hj�nnev�g a...@android.com
Cc: Benoit Goby ben...@android.com
Signed-off-by: Todd Poynor toddpoy...@google.com
[kiran: Added context to commit message, squished build fixes
from Benoit Goby and Arve Hj�nnev�g, changed wakelocks usage
to wakeupsource, merged Todd's refactoring logic and simplified
the structures and code and addressed community feedback]
Signed-off-by: Kiran Raparthy kiran.ku...@linaro.org
---
 drivers/usb/phy/phy.c   | 12 
 include/linux/usb/phy.h |  5 +
 2 files changed, 17 insertions(+)

diff --git a/drivers/usb/phy/phy.c b/drivers/usb/phy/phy.c
index 045cd30..2b1039e 100644
--- a/drivers/usb/phy/phy.c
+++ b/drivers/usb/phy/phy.c
@@ -444,3 +444,15 @@ int usb_bind_phy(const char *dev_name, u8 index,
return 0;
 }
 EXPORT_SYMBOL_GPL(usb_bind_phy);
+
+/**
+ * usb_phy_set_event - set event to phy event
+ * @x: the phy returned by usb_get_phy();
+ *
+ * This sets event to phy event
+ */
+void usb_phy_set_event(struct usb_phy *x, unsigned long event)
+{
+   x-last_event = event;
+}
+EXPORT_SYMBOL_GPL(usb_phy_set_event);
diff --git a/include/linux/usb/phy.h b/include/linux/usb/phy.h
index 353053a..3c713ff 100644
--- a/include/linux/usb/phy.h
+++ b/include/linux/usb/phy.h
@@ -210,6 +210,7 @@ extern void usb_put_phy(struct usb_phy *);
 extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
 extern int usb_bind_phy(const char *dev_name, u8 index,
const char *phy_dev_name);
+extern void usb_phy_set_event(struct usb_phy *x, unsigned long event);
 #else
 static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
 {
@@ -251,6 +252,10 @@ static inline int usb_bind_phy(const char *dev_name, u8 
index,
 {
return -EOPNOTSUPP;
 }
+
+static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event)
+{
+}
 #endif
 
 static inline int
-- 
1.8.2.1

--
To unsubscribe from this list: send the line unsubscribe linux-usb in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH 2/3] usb: phy: Handle per-PHY event for connect and disconnect events

2014-11-19 Thread Kiran Kumar Raparthy
From: Todd Poynor toddpoy...@google.com

usb: phy: Handle per-PHY event for connnect and disconnect events

When usb is connected and enumerated in device mode or when usb is
disconnected,call usb_phy_set_event from phy drivers to handle per-PHY event.

Cc: Felipe Balbi ba...@ti.com
Cc: Greg Kroah-Hartman gre...@linuxfoundation.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-usb@vger.kernel.org
Cc: Android Kernel Team kernel-t...@android.com
Cc: John Stultz john.stu...@linaro.org
Cc: Sumit Semwal sumit.sem...@linaro.org
Cc: Arve Hj�nnev�g a...@android.com
Cc: Benoit Goby ben...@android.com
Signed-off-by: Todd Poynor toddpoy...@google.com
[kiran: Added context to commit message, squished build fixes
from Benoit Goby and Arve Hj�nnev�g, changed wakelocks usage
to wakeupsource, merged Todd's refactoring logic and simplified
the structures and code and addressed community feedback]
Signed-off-by: Kiran Raparthy kiran.ku...@linaro.org
---
 drivers/usb/phy/phy-ab8500-usb.c| 15 +++
 drivers/usb/phy/phy-gpio-vbus-usb.c |  2 ++
 drivers/usb/phy/phy-mv-usb.c|  2 ++
 drivers/usb/phy/phy-tahvo.c |  2 ++
 4 files changed, 21 insertions(+)

diff --git a/drivers/usb/phy/phy-ab8500-usb.c b/drivers/usb/phy/phy-ab8500-usb.c
index 11ab2c4..d79fa3e 100644
--- a/drivers/usb/phy/phy-ab8500-usb.c
+++ b/drivers/usb/phy/phy-ab8500-usb.c
@@ -447,6 +447,7 @@ static int ab9540_usb_link_status_update(struct ab8500_usb 
*ab,
event = UX500_MUSB_NONE;
/* Fallback to default B_IDLE as nothing is connected. */
ab-phy.state = OTG_STATE_B_IDLE;
+   usb_phy_set_event(ab-phy, USB_EVENT_NONE);
break;
 
case USB_LINK_ACA_RID_C_NM_9540:
@@ -461,12 +462,14 @@ static int ab9540_usb_link_status_update(struct 
ab8500_usb *ab,
ab8500_usb_peri_phy_en(ab);
atomic_notifier_call_chain(ab-phy.notifier,
UX500_MUSB_PREPARE, ab-vbus_draw);
+   usb_phy_set_event(ab-phy, USB_EVENT_ENUMERATED);
}
if (ab-mode == USB_IDLE) {
ab-mode = USB_PERIPHERAL;
ab8500_usb_peri_phy_en(ab);
atomic_notifier_call_chain(ab-phy.notifier,
UX500_MUSB_PREPARE, ab-vbus_draw);
+   usb_phy_set_event(ab-phy, USB_EVENT_ENUMERATED);
}
if (event != UX500_MUSB_RIDC)
event = UX500_MUSB_VBUS;
@@ -502,6 +505,7 @@ static int ab9540_usb_link_status_update(struct ab8500_usb 
*ab,
event = UX500_MUSB_CHARGER;
atomic_notifier_call_chain(ab-phy.notifier,
event, ab-vbus_draw);
+   usb_phy_set_event(ab-phy, USB_EVENT_CHARGER);
break;
 
case USB_LINK_PHYEN_NO_VBUS_NO_IDGND_9540:
@@ -526,6 +530,7 @@ static int ab9540_usb_link_status_update(struct ab8500_usb 
*ab,
ab-mode = USB_IDLE;
ab-phy.otg-default_a = false;
ab-vbus_draw = 0;
+   usb_phy_set_event(ab-phy, USB_EVENT_NONE);
}
}
break;
@@ -585,6 +590,7 @@ static int ab8540_usb_link_status_update(struct ab8500_usb 
*ab,
 * is connected
 */
ab-phy.state = OTG_STATE_B_IDLE;
+   usb_phy_set_event(ab-phy, USB_EVENT_NONE);
break;
 
case USB_LINK_ACA_RID_C_NM_8540:
@@ -598,6 +604,7 @@ static int ab8540_usb_link_status_update(struct ab8500_usb 
*ab,
ab8500_usb_peri_phy_en(ab);
atomic_notifier_call_chain(ab-phy.notifier,
UX500_MUSB_PREPARE, ab-vbus_draw);
+   usb_phy_set_event(ab-phy, USB_EVENT_ENUMERATED);
}
if (event != UX500_MUSB_RIDC)
event = UX500_MUSB_VBUS;
@@ -626,6 +633,7 @@ static int ab8540_usb_link_status_update(struct ab8500_usb 
*ab,
event = UX500_MUSB_CHARGER;
atomic_notifier_call_chain(ab-phy.notifier,
event, ab-vbus_draw);
+   usb_phy_set_event(ab-phy, USB_EVENT_CHARGER);
break;
 
case USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8540:
@@ -648,6 +656,7 @@ static int ab8540_usb_link_status_update(struct ab8500_usb 
*ab,
ab-mode = USB_IDLE;
ab-phy.otg-default_a = false;
ab-vbus_draw = 0;
+   usb_phy_set_event(ab-phy, USB_EVENT_NONE);
}
break;
 
@@ -694,6 +703,7 @@ static int ab8505_usb_link_status_update(struct ab8500_usb 
*ab,
 * is connected
 */
   

[PATCH 3/3] usb: phy: hold wakeupsource when USB is enumerated in peripheral mode

2014-11-19 Thread Kiran Kumar Raparthy
From: Todd Poynor toddpoy...@google.com

usb: phy: hold wakeupsource when USB is enumerated in peripheral mode

Some systems require a mechanism to prevent system to enter into suspend
state when USB is connected and enumerated in peripheral mode.

This patch provides an interface to hold a wakeupsource to prevent suspend.
PHY drivers can use this interface when USB is connected and enumerated in
peripheral mode.

A timed wakeupsource is temporarily held on USB disconnect events, to allow
the rest of the system to react to the USB disconnection (dropping host
sessions, updating charger status, etc.) prior to re-allowing suspend.

Cc: Felipe Balbi ba...@ti.com
Cc: Greg Kroah-Hartman gre...@linuxfoundation.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-usb@vger.kernel.org
Cc: Android Kernel Team kernel-t...@android.com
Cc: John Stultz john.stu...@linaro.org
Cc: Sumit Semwal sumit.sem...@linaro.org
Cc: Arve Hj�nnev�g a...@android.com
Cc: Benoit Goby ben...@android.com
Signed-off-by: Todd Poynor toddpoy...@google.com
[kiran: Added context to commit message, squished build fixes
from Benoit Goby and Arve Hj�nnev�g, changed wakelocks usage
to wakeupsource, merged Todd's refactoring logic and simplified
the structures and code and addressed community feedback]
Signed-off-by: Kiran Raparthy kiran.ku...@linaro.org
---
 drivers/usb/phy/phy.c   | 29 +++--
 include/linux/usb/phy.h |  5 +
 2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/phy/phy.c b/drivers/usb/phy/phy.c
index 2b1039e..b8a2d56 100644
--- a/drivers/usb/phy/phy.c
+++ b/drivers/usb/phy/phy.c
@@ -329,6 +329,7 @@ int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
int ret = 0;
unsigned long   flags;
struct usb_phy  *phy;
+   char wsource_name[40];
 
if (x-type != USB_PHY_TYPE_UNDEFINED) {
dev_err(x-dev, not accepting initialized PHY %s\n, x-label);
@@ -351,6 +352,10 @@ int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
x-type = type;
list_add_tail(x-head, phy_list);
 
+   snprintf(wsource_name, sizeof(wsource_name), vbus-%s,
+   dev_name(x-dev));
+   wakeup_source_init(x-wsource, wsource_name);
+
 out:
spin_unlock_irqrestore(phy_lock, flags);
return ret;
@@ -402,6 +407,7 @@ void usb_remove_phy(struct usb_phy *x)
 
spin_lock_irqsave(phy_lock, flags);
if (x) {
+   wakeup_source_trash(x-wsource);
list_for_each_entry(phy_bind, phy_bind_list, list)
if (phy_bind-phy == x)
phy_bind-phy = NULL;
@@ -446,13 +452,32 @@ int usb_bind_phy(const char *dev_name, u8 index,
 EXPORT_SYMBOL_GPL(usb_bind_phy);
 
 /**
- * usb_phy_set_event - set event to phy event
+ * usb_phy_set_event - set event to phy event and
+ * hold/temporarily hold wakeupsource
  * @x: the phy returned by usb_get_phy();
  *
- * This sets event to phy event
+ * This holds per-PHY wakeupsource/timed wakeupsource
  */
 void usb_phy_set_event(struct usb_phy *x, unsigned long event)
 {
+
x-last_event = event;
+
+   switch (event) {
+   case USB_EVENT_ENUMERATED:
+   __pm_stay_awake(x-wsource);
+   break;
+
+   case USB_EVENT_NONE:
+   case USB_EVENT_ID:
+   case USB_EVENT_VBUS:
+   case USB_EVENT_CHARGER:
+   __pm_wakeup_event(x-wsource,
+ USB_PHY_DEFAULT_WAKEUP_SRC_TIMEOUT);
+   break;
+
+   default:
+   break;
+   }
 }
 EXPORT_SYMBOL_GPL(usb_phy_set_event);
diff --git a/include/linux/usb/phy.h b/include/linux/usb/phy.h
index 3c713ff..c593fc6 100644
--- a/include/linux/usb/phy.h
+++ b/include/linux/usb/phy.h
@@ -12,6 +12,8 @@
 #include linux/notifier.h
 #include linux/usb.h
 
+#define USB_PHY_DEFAULT_WAKEUP_SRC_TIMEOUT msecs_to_jiffies(2000)
+
 enum usb_phy_interface {
USBPHY_INTERFACE_MODE_UNKNOWN,
USBPHY_INTERFACE_MODE_UTMI,
@@ -89,6 +91,9 @@ struct usb_phy {
/* for notification of usb_phy_events */
struct atomic_notifier_head notifier;
 
+   /* wakeup source */
+   struct wakeup_sourcewsource;
+
/* to pass extra port status to the root hub */
u16 port_status;
u16 port_change;
-- 
1.8.2.1

--
To unsubscribe from this list: send the line unsubscribe linux-usb in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[RFC v6 1/3] usb: phy: introduce usb_phy_set_event interface

2014-11-11 Thread Kiran Kumar Raparthy
From: Todd Poynor toddpoy...@google.com

usb: phy: introduce usb_phy_set_event interface

PHY drivers require a generic interface to handle per-PHY events.

usb_phy_set_event interface sets event to phy event.
PHY drivers call this interface for each phy event.

Cc: Felipe Balbi ba...@ti.com
Cc: Greg Kroah-Hartman gre...@linuxfoundation.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-usb@vger.kernel.org
Cc: Android Kernel Team kernel-t...@android.com
Cc: John Stultz john.stu...@linaro.org
Cc: Sumit Semwal sumit.sem...@linaro.org
Cc: Arve Hj�nnev�g a...@android.com
Cc: Benoit Goby ben...@android.com
Signed-off-by: Todd Poynor toddpoy...@google.com
[kiran: Added context to commit message, squished build fixes
from Benoit Goby and Arve Hj�nnev�g, changed wakelocks usage
to wakeupsource, merged Todd's refactoring logic and simplified
the structures and code and addressed community feedback]
Signed-off-by: Kiran Raparthy kiran.ku...@linaro.org
---
 drivers/usb/phy/phy.c   | 12 
 include/linux/usb/phy.h |  5 +
 2 files changed, 17 insertions(+)

diff --git a/drivers/usb/phy/phy.c b/drivers/usb/phy/phy.c
index 045cd30..2b1039e 100644
--- a/drivers/usb/phy/phy.c
+++ b/drivers/usb/phy/phy.c
@@ -444,3 +444,15 @@ int usb_bind_phy(const char *dev_name, u8 index,
return 0;
 }
 EXPORT_SYMBOL_GPL(usb_bind_phy);
+
+/**
+ * usb_phy_set_event - set event to phy event
+ * @x: the phy returned by usb_get_phy();
+ *
+ * This sets event to phy event
+ */
+void usb_phy_set_event(struct usb_phy *x, unsigned long event)
+{
+   x-last_event = event;
+}
+EXPORT_SYMBOL_GPL(usb_phy_set_event);
diff --git a/include/linux/usb/phy.h b/include/linux/usb/phy.h
index 353053a..3c713ff 100644
--- a/include/linux/usb/phy.h
+++ b/include/linux/usb/phy.h
@@ -210,6 +210,7 @@ extern void usb_put_phy(struct usb_phy *);
 extern void devm_usb_put_phy(struct device *dev, struct usb_phy *x);
 extern int usb_bind_phy(const char *dev_name, u8 index,
const char *phy_dev_name);
+extern void usb_phy_set_event(struct usb_phy *x, unsigned long event);
 #else
 static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
 {
@@ -251,6 +252,10 @@ static inline int usb_bind_phy(const char *dev_name, u8 
index,
 {
return -EOPNOTSUPP;
 }
+
+static inline void usb_phy_set_event(struct usb_phy *x, unsigned long event)
+{
+}
 #endif
 
 static inline int
-- 
1.8.2.1

--
To unsubscribe from this list: send the line unsubscribe linux-usb in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[RFC v6 2/3] usb: phy: handle per-PHY connect and disconnect events

2014-11-11 Thread Kiran Kumar Raparthy
From: Todd Poynor toddpoy...@google.com

usb: phy: handle per-PHY connect and disconnect events

When usb is connected and enumerated in device mode or when usb is
disconnected,call usb_phy_set_event from phy drivers to handle per-PHY event.

Cc: Felipe Balbi ba...@ti.com
Cc: Greg Kroah-Hartman gre...@linuxfoundation.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-usb@vger.kernel.org
Cc: Android Kernel Team kernel-t...@android.com
Cc: John Stultz john.stu...@linaro.org
Cc: Sumit Semwal sumit.sem...@linaro.org
Cc: Arve Hj�nnev�g a...@android.com
Cc: Benoit Goby ben...@android.com
Signed-off-by: Todd Poynor toddpoy...@google.com
[kiran: Added context to commit message, squished build fixes
from Benoit Goby and Arve Hj�nnev�g, changed wakelocks usage
to wakeupsource, merged Todd's refactoring logic and simplified
the structures and code and addressed community feedback]
Signed-off-by: Kiran Raparthy kiran.ku...@linaro.org
---
 drivers/usb/phy/phy-ab8500-usb.c| 15 +++
 drivers/usb/phy/phy-gpio-vbus-usb.c |  2 ++
 drivers/usb/phy/phy-mv-usb.c|  2 ++
 drivers/usb/phy/phy-tahvo.c |  2 ++
 4 files changed, 21 insertions(+)

diff --git a/drivers/usb/phy/phy-ab8500-usb.c b/drivers/usb/phy/phy-ab8500-usb.c
index 11ab2c4..d79fa3e 100644
--- a/drivers/usb/phy/phy-ab8500-usb.c
+++ b/drivers/usb/phy/phy-ab8500-usb.c
@@ -447,6 +447,7 @@ static int ab9540_usb_link_status_update(struct ab8500_usb 
*ab,
event = UX500_MUSB_NONE;
/* Fallback to default B_IDLE as nothing is connected. */
ab-phy.state = OTG_STATE_B_IDLE;
+   usb_phy_set_event(ab-phy, USB_EVENT_NONE);
break;
 
case USB_LINK_ACA_RID_C_NM_9540:
@@ -461,12 +462,14 @@ static int ab9540_usb_link_status_update(struct 
ab8500_usb *ab,
ab8500_usb_peri_phy_en(ab);
atomic_notifier_call_chain(ab-phy.notifier,
UX500_MUSB_PREPARE, ab-vbus_draw);
+   usb_phy_set_event(ab-phy, USB_EVENT_ENUMERATED);
}
if (ab-mode == USB_IDLE) {
ab-mode = USB_PERIPHERAL;
ab8500_usb_peri_phy_en(ab);
atomic_notifier_call_chain(ab-phy.notifier,
UX500_MUSB_PREPARE, ab-vbus_draw);
+   usb_phy_set_event(ab-phy, USB_EVENT_ENUMERATED);
}
if (event != UX500_MUSB_RIDC)
event = UX500_MUSB_VBUS;
@@ -502,6 +505,7 @@ static int ab9540_usb_link_status_update(struct ab8500_usb 
*ab,
event = UX500_MUSB_CHARGER;
atomic_notifier_call_chain(ab-phy.notifier,
event, ab-vbus_draw);
+   usb_phy_set_event(ab-phy, USB_EVENT_CHARGER);
break;
 
case USB_LINK_PHYEN_NO_VBUS_NO_IDGND_9540:
@@ -526,6 +530,7 @@ static int ab9540_usb_link_status_update(struct ab8500_usb 
*ab,
ab-mode = USB_IDLE;
ab-phy.otg-default_a = false;
ab-vbus_draw = 0;
+   usb_phy_set_event(ab-phy, USB_EVENT_NONE);
}
}
break;
@@ -585,6 +590,7 @@ static int ab8540_usb_link_status_update(struct ab8500_usb 
*ab,
 * is connected
 */
ab-phy.state = OTG_STATE_B_IDLE;
+   usb_phy_set_event(ab-phy, USB_EVENT_NONE);
break;
 
case USB_LINK_ACA_RID_C_NM_8540:
@@ -598,6 +604,7 @@ static int ab8540_usb_link_status_update(struct ab8500_usb 
*ab,
ab8500_usb_peri_phy_en(ab);
atomic_notifier_call_chain(ab-phy.notifier,
UX500_MUSB_PREPARE, ab-vbus_draw);
+   usb_phy_set_event(ab-phy, USB_EVENT_ENUMERATED);
}
if (event != UX500_MUSB_RIDC)
event = UX500_MUSB_VBUS;
@@ -626,6 +633,7 @@ static int ab8540_usb_link_status_update(struct ab8500_usb 
*ab,
event = UX500_MUSB_CHARGER;
atomic_notifier_call_chain(ab-phy.notifier,
event, ab-vbus_draw);
+   usb_phy_set_event(ab-phy, USB_EVENT_CHARGER);
break;
 
case USB_LINK_PHYEN_NO_VBUS_NO_IDGND_8540:
@@ -648,6 +656,7 @@ static int ab8540_usb_link_status_update(struct ab8500_usb 
*ab,
ab-mode = USB_IDLE;
ab-phy.otg-default_a = false;
ab-vbus_draw = 0;
+   usb_phy_set_event(ab-phy, USB_EVENT_NONE);
}
break;
 
@@ -694,6 +703,7 @@ static int ab8505_usb_link_status_update(struct ab8500_usb 
*ab,
 * is connected
 */

[RFC v6 3/3] usb: phy: hold wakeupsource when USB is enumerated in peripheral mode

2014-11-11 Thread Kiran Kumar Raparthy
From: Todd Poynor toddpoy...@google.com

usb: phy: hold wakeupsource when USB is enumerated in peripheral mode

Some systems require a mechanism to prevent system to enter into suspend
state when USB is connected and enumerated in peripheral mode.

This patch provides an interface to hold a wakeupsource to prevent suspend.
PHY drivers can use this interface when USB is connected and enumerated in
peripheral mode.

A timed wakeupsource is temporarily held on USB disconnect events, to allow
the rest of the system to react to the USB disconnection (dropping host
sessions, updating charger status, etc.) prior to re-allowing suspend.

Cc: Felipe Balbi ba...@ti.com
Cc: Greg Kroah-Hartman gre...@linuxfoundation.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-usb@vger.kernel.org
Cc: Android Kernel Team kernel-t...@android.com
Cc: John Stultz john.stu...@linaro.org
Cc: Sumit Semwal sumit.sem...@linaro.org
Cc: Arve Hj�nnev�g a...@android.com
Cc: Benoit Goby ben...@android.com
Signed-off-by: Todd Poynor toddpoy...@google.com
[kiran: Added context to commit message, squished build fixes
from Benoit Goby and Arve Hjønnevåg, changed wakelocks usage
to wakeupsource, merged Todd's refactoring logic and simplified
the structures and code and addressed community feedback]
Signed-off-by: Kiran Raparthy kiran.ku...@linaro.org
---
 drivers/usb/phy/phy.c   | 29 +++--
 include/linux/usb/phy.h |  5 +
 2 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/phy/phy.c b/drivers/usb/phy/phy.c
index 2b1039e..b5c5fd3 100644
--- a/drivers/usb/phy/phy.c
+++ b/drivers/usb/phy/phy.c
@@ -329,6 +329,7 @@ int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
int ret = 0;
unsigned long   flags;
struct usb_phy  *phy;
+   char wsource_name[40];
 
if (x-type != USB_PHY_TYPE_UNDEFINED) {
dev_err(x-dev, not accepting initialized PHY %s\n, x-label);
@@ -351,6 +352,10 @@ int usb_add_phy(struct usb_phy *x, enum usb_phy_type type)
x-type = type;
list_add_tail(x-head, phy_list);
 
+   snprintf(wsource_name, sizeof(wsource_name), vbus-%s,
+   dev_name(x-dev));
+   wakeup_source_init(x-wsource, wsource_name);
+
 out:
spin_unlock_irqrestore(phy_lock, flags);
return ret;
@@ -402,6 +407,7 @@ void usb_remove_phy(struct usb_phy *x)
 
spin_lock_irqsave(phy_lock, flags);
if (x) {
+   wakeup_source_trash(x-wsource);
list_for_each_entry(phy_bind, phy_bind_list, list)
if (phy_bind-phy == x)
phy_bind-phy = NULL;
@@ -446,13 +452,32 @@ int usb_bind_phy(const char *dev_name, u8 index,
 EXPORT_SYMBOL_GPL(usb_bind_phy);
 
 /**
- * usb_phy_set_event - set event to phy event
+ * usb_phy_set_event - set event to phy event and
+ * hold/temporarily hold wakeupsource
  * @x: the phy returned by usb_get_phy();
  *
- * This sets event to phy event
+ * This holds per-PHY wakeupsource/timed wakeupsource
  */
 void usb_phy_set_event(struct usb_phy *x, unsigned long event)
 {
+
x-last_event = event;
+
+   switch (event) {
+   case USB_EVENT_ENUMERATED:
+   __pm_stay_awake(x-wsource);
+   break;
+
+   case USB_EVENT_NONE:
+   case USB_EVENT_ID:
+   case USB_EVENT_VBUS:
+   case USB_EVENT_CHARGER:
+   __pm_wakeup_event(x-wsource,
+   msecs_to_jiffies(TEMPORARY_HOLD_TIME));
+   break;
+
+   default:
+   break;
+   }
 }
 EXPORT_SYMBOL_GPL(usb_phy_set_event);
diff --git a/include/linux/usb/phy.h b/include/linux/usb/phy.h
index 3c713ff..bc58fb8 100644
--- a/include/linux/usb/phy.h
+++ b/include/linux/usb/phy.h
@@ -12,6 +12,8 @@
 #include linux/notifier.h
 #include linux/usb.h
 
+#define TEMPORARY_HOLD_TIME2000
+
 enum usb_phy_interface {
USBPHY_INTERFACE_MODE_UNKNOWN,
USBPHY_INTERFACE_MODE_UTMI,
@@ -89,6 +91,9 @@ struct usb_phy {
/* for notification of usb_phy_events */
struct atomic_notifier_head notifier;
 
+   /* wakeup source */
+   struct wakeup_sourcewsource;
+
/* to pass extra port status to the root hub */
u16 port_status;
u16 port_change;
-- 
1.8.2.1

--
To unsubscribe from this list: send the line unsubscribe linux-usb in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[RFC v5] usb: phy: Hold wakeupsource when USB is enumerated in peripheral mode

2014-11-04 Thread Kiran Kumar Raparthy
From: Todd Poynor toddpoy...@google.com

usb: phy: Hold wakeupsource when USB is enumerated in peripheral mode

Some systems require a mechanism to prevent system to enter into suspend
state when USB is connected and enumerated in peripheral mode.

This patch provides an interface to hold a wakeupsource to prevent suspend.
PHY drivers can use this interface when USB is connected and enumerated in
peripheral mode.

A timed wakeupsource is temporarily held on USB disconnect events, to allow
the rest of the system to react to the USB disconnection (dropping host
sessions, updating charger status, etc.) prior to re-allowing suspend.

Cc: Kishon Vijay Abraham I kis...@ti.com
Cc: Felipe Balbi ba...@ti.com
Cc: Greg Kroah-Hartman gre...@linuxfoundation.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-usb@vger.kernel.org
Cc: Android Kernel Team kernel-t...@android.com
Cc: John Stultz john.stu...@linaro.org
Cc: Sumit Semwal sumit.sem...@linaro.org
Cc: Arve Hj?nnev?g a...@android.com
Cc: Benoit Goby ben...@android.com
Signed-off-by: Todd Poynor toddpoy...@google.com
[kiran: Added context to commit message, squished build fixes
from Benoit Goby and Arve Hj?nnev?g, changed wakelocks usage
to wakeupsource, merged Todd's refactoring logic and simplified
the structures and code and addressed community feedback]
Signed-off-by: Kiran Raparthy kiran.ku...@linaro.org
---
v5:
* with the new approach,i was able to chnage only 5 phy drivers
as mentioned below to use the wakeupsource funtionality.

drivers/phy/phy-omap-control.c
drivers/usb/phy/phy-ab8500-usb.c
drivers/usb/phy/phy-tahvo.c
drivers/usb/phy/phy-mv-usb.c
drivers/usb/phy/phy-gpio-vbus-usb.c

* Notification method not used any more.
* introduced usb_phy_set_event in phy.c to hold wakeupsource
* introduced usb_phy_wsource_init/usb_phy_wsource_trash in phy.c to
initialize/relese per-PHY wakeupsource.These interfaces called from
probe/remove functions of phy drivers.
* usb_phy_set_event called from phy drivers where usb enumeration
event is handled.
* usb_phy_set_event expects usb_phy handle where as phy-omap-control.c
 provides its own type(omap_control_phy), so directly called __pm_stay_awake
and  __pm_awake_event from phy-omap-control.c instead of calling
usb_phy_set_event.

v4:
* Temporarily hold wakeupsource patch integrated into main patch.
* As per feedback,dropped enabled module parameter.
* Introduced otgws_otg_usb3_notifications function to handle event
  notifications from usb3 phy.
* Handled wakeupsource initialization,spinlock,registration of notifier block
  per-PHY.
* Updated usb_phy structure.

v3:
* As per the feedback,no global phy pointer used.
* called the one-liner wakeupsource handling calls
  directly instead of indirect functions implemented in v2.
* Removed indirect function get_phy_hook and used usb_get_phy
  to get the phy handle..

v2:
* wakeupsource handling implemeted per-PHY
* Implemented wakeupsource handling calls in phy
* included Todd's refactoring logic.

v1:
* changed to disabled by default from enable by default.
* Kconfig help text modified
* Included better commit text
* otgws_nb moved to otg_wakeupsource_init function
* Introduced get_phy_hook to handle otgws_xceiv per-PHY

Initial RFC:
* Included build fix from Benoit Goby and Arve Hj?nnev?g
* Removed lock-held field in driver as this mechanism is
  provided in wakeupsource driver.
* wakelock(wl) terminology replaced with wakeup_source(ws).

 drivers/phy/phy-omap-control.c   |  9 ++
 drivers/usb/phy/phy-ab8500-usb.c | 17 +++
 drivers/usb/phy/phy-gpio-vbus-usb.c  |  4 +++
 drivers/usb/phy/phy-mv-usb.c |  4 +++
 drivers/usb/phy/phy-tahvo.c  |  5 
 drivers/usb/phy/phy.c| 56 
 include/linux/phy/omap_control_phy.h |  4 +++
 include/linux/usb/phy.h  |  8 ++
 8 files changed, 107 insertions(+)

diff --git a/drivers/phy/phy-omap-control.c b/drivers/phy/phy-omap-control.c
index 311b4f9..efc629f 100644
--- a/drivers/phy/phy-omap-control.c
+++ b/drivers/phy/phy-omap-control.c
@@ -24,6 +24,7 @@
 #include linux/err.h
 #include linux/io.h
 #include linux/clk.h
+#include linux/usb/phy.h
 #include linux/phy/omap_control_phy.h
 
 /**
@@ -196,9 +197,12 @@ void omap_control_usb_set_mode(struct device *dev,
break;
case USB_MODE_DEVICE:
omap_control_usb_device_mode(ctrl_phy);
+   __pm_stay_awake(ctrl_phy-wsource);
break;
case USB_MODE_DISCONNECT:
omap_control_usb_set_sessionend(ctrl_phy);
+   __pm_wakeup_event(ctrl_phy-wsource,
+   msecs_to_jiffies(TEMPORARY_HOLD_TIME));
break;
default:
dev_vdbg(dev, invalid omap control usb mode\n);
@@ -246,6 +250,7 @@ static int omap_control_phy_probe(struct platform_device 
*pdev)
struct resource *res;
const struct of_device_id *of_id;
struct omap_control_phy 

[RFC v4] usb: phy: Hold wakeupsource when USB is enumerated in peripheral mode

2014-10-07 Thread Kiran Kumar Raparthy
From: Todd Poynor toddpoy...@google.com

usb: phy: Hold wakeupsource when USB is enumerated in peripheral mode

Purpose of this is to prevent the system to enter into suspend state from USB
peripheral traffic by hodling a wakeupsource when USB is connected and
enumerated in peripheral mode(say adb).

Temporarily hold a timed wakeup source on USB disconnect events, to allow
the rest of the system to react to the USB disconnection (dropping host
sessions, updating charger status, etc.) prior to re-allowing suspend.

Cc: Felipe Balbi ba...@ti.com
Cc: Greg Kroah-Hartman gre...@linuxfoundation.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-usb@vger.kernel.org
Cc: Android Kernel Team kernel-t...@android.com
Cc: John Stultz john.stu...@linaro.org
Cc: Sumit Semwal sumit.sem...@linaro.org
Cc: Arve Hj�nnev�g a...@android.com
Cc: Benoit Goby ben...@android.com
Signed-off-by: Todd Poynor toddpoy...@google.com
[kiran: Added context to commit message, squished build fixes
from Benoit Goby and Arve Hj�nnev�g, changed wakelocks usage
to wakeupsource, merged Todd's refactoring logic and simplified
the structures and code and addressed community feedback]
Signed-off-by: Kiran Raparthy kiran.ku...@linaro.org
---
v4:
* Temporarily hold wakeupsource patch integrated into main patch.
* As per feedback,dropped enabled module parameter.
* Introduced otgws_otg_usb3_notifications function to handle event
  notifications from usb3 phy.
* Handled wakeupsource initialization,spinlock,registration of notifier block
  per-PHY.
* Updated usb_phy structure.

v3:
* As per the feedback,no global phy pointer used.
* called the one-liner wakeupsource handling calls
  directly instead of indirect functions implemented in v2.
* Removed indirect function get_phy_hook and used usb_get_phy
  to get the phy handle..

v2:
* wakeupsource handling implemeted per-PHY
* Implemented wakeupsource handling calls in phy
* included Todd's refactoring logic.

v1:
* changed to disabled by default from enable by default.
* Kconfig help text modified
* Included better commit text
* otgws_nb moved to otg_wakeupsource_init function
* Introduced get_phy_hook to handle otgws_xceiv per-PHY

RFC:
* Included build fix from Benoit Goby and Arve Hj�nnev�g
* Removed lock-held field in driver as this mechanism is
  provided in wakeupsource driver.
* wakelock(wl) terminology replaced with wakeup_source(ws).

 drivers/usb/phy/Kconfig|   8 +++
 drivers/usb/phy/Makefile   |   1 +
 drivers/usb/phy/otg-wakeupsource.c | 134 +
 include/linux/usb/phy.h|   8 +++
 4 files changed, 151 insertions(+)
 create mode 100644 drivers/usb/phy/otg-wakeupsource.c

diff --git a/drivers/usb/phy/Kconfig b/drivers/usb/phy/Kconfig
index e253fa0..d9ddd85 100644
--- a/drivers/usb/phy/Kconfig
+++ b/drivers/usb/phy/Kconfig
@@ -6,6 +6,14 @@ menu USB Physical Layer drivers
 config USB_PHY
def_bool n
 
+config USB_OTG_WAKEUPSOURCE
+   bool Hold wakeupsource when USB is enumerated in peripheral mode
+   depends on PM_SLEEP
+   select USB_PHY
+   help
+ Prevent the system going into automatic suspend while
+ it is attached as a USB peripheral by holding a wakeupsource.
+
 #
 # USB Transceiver Drivers
 #
diff --git a/drivers/usb/phy/Makefile b/drivers/usb/phy/Makefile
index 24a9133..ca2fbaf 100644
--- a/drivers/usb/phy/Makefile
+++ b/drivers/usb/phy/Makefile
@@ -3,6 +3,7 @@
 #
 obj-$(CONFIG_USB_PHY)  += phy.o
 obj-$(CONFIG_OF)   += of.o
+obj-$(CONFIG_USB_OTG_WAKEUPSOURCE) += otg-wakeupsource.o
 
 # transceiver drivers, keep the list sorted
 
diff --git a/drivers/usb/phy/otg-wakeupsource.c 
b/drivers/usb/phy/otg-wakeupsource.c
new file mode 100644
index 000..00d3359
--- /dev/null
+++ b/drivers/usb/phy/otg-wakeupsource.c
@@ -0,0 +1,134 @@
+/*
+ * otg-wakeupsource.c
+ *
+ * Copyright (C) 2011 Google, Inc.
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include linux/kernel.h
+#include linux/device.h
+#include linux/module.h
+#include linux/notifier.h
+#include linux/pm_wakeup.h
+#include linux/spinlock.h
+#include linux/usb/otg.h
+
+static void otgws_handle_event(struct usb_phy *otgws_xceiv, unsigned long 
event)
+{
+   unsigned long irqflags;
+
+   spin_lock_irqsave(otgws_xceiv-otgws_slock, irqflags);
+
+   switch (event) {
+   case USB_EVENT_VBUS:
+   case USB_EVENT_ENUMERATED:
+   __pm_stay_awake(otgws_xceiv-wsource);
+   break;
+
+   case USB_EVENT_NONE:
+   case 

[RFC v3 1/2] usb: phy: Hold wakeupsource when USB is enumerated in peripheral mode

2014-09-08 Thread Kiran Kumar Raparthy
From: Todd Poynor toddpoy...@google.com

usb: phy: Hold wakeupsource when USB is enumerated in peripheral mode

Purpose of this is to prevent the system to enter into suspend state from USB
peripheral traffic by hodling a wakeupsource when USB is connected and
enumerated in peripheral mode(say adb).

Disabled by default, can enable with:
   echo Y  /sys/module/otg_wakeupsource/parameters/enabled

Cc: Felipe Balbi ba...@ti.com
Cc: Greg Kroah-Hartman gre...@linuxfoundation.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-usb@vger.kernel.org
Cc: Android Kernel Team kernel-t...@android.com
Cc: John Stultz john.stu...@linaro.org
Cc: Sumit Semwal sumit.sem...@linaro.org
Cc: Arve Hj�nnev�g a...@android.com
Cc: Benoit Goby ben...@android.com
Signed-off-by: Todd Poynor toddpoy...@google.com
[kiran: Added context to commit message, squished build fixes
from Benoit Goby and Arve Hj�nnev�g, changed wakelocks usage
to wakeupsource, merged Todd's refactoring logic and simplified
the structures and code and addressed community feedback]
Signed-off-by: Kiran Raparthy kiran.ku...@linaro.org
---
v3:
* As per the feedback,no global phy pointer used.
* called the one-liner wakeupsource handling calls
  directly instead of indirect functions implemented in v2.
* Removed indirect function get_phy_hook and used usb_get_phy
  to get the phy handle..

v2:
* wakeupsource handling implemeted per-PHY
* Implemented wakeupsource handling calls in phy
* included Todd's refactoring logic.

v1:
* changed to disabled by default from enable by default.
* Kconfig help text modified
* Included better commit text
* otgws_nb moved to otg_wakeupsource_init function
* Introduced get_phy_hook to handle otgws_xceiv per-PHY

RFC:
* Included build fix from Benoit Goby and Arve Hj�nnev�g
* Removed lock-held field in driver as this mechanism is
  provided in wakeupsource driver.
* wakelock(wl) terminology replaced with wakeup_source(ws).

 drivers/usb/phy/Kconfig|   8 +++
 drivers/usb/phy/Makefile   |   1 +
 drivers/usb/phy/otg-wakeupsource.c | 136 +
 include/linux/usb/phy.h|   4 ++
 4 files changed, 149 insertions(+)
 create mode 100644 drivers/usb/phy/otg-wakeupsource.c

diff --git a/drivers/usb/phy/Kconfig b/drivers/usb/phy/Kconfig
index e253fa0..d9ddd85 100644
--- a/drivers/usb/phy/Kconfig
+++ b/drivers/usb/phy/Kconfig
@@ -6,6 +6,14 @@ menu USB Physical Layer drivers
 config USB_PHY
def_bool n
 
+config USB_OTG_WAKEUPSOURCE
+   bool Hold wakeupsource when USB is enumerated in peripheral mode
+   depends on PM_SLEEP
+   select USB_PHY
+   help
+ Prevent the system going into automatic suspend while
+ it is attached as a USB peripheral by holding a wakeupsource.
+
 #
 # USB Transceiver Drivers
 #
diff --git a/drivers/usb/phy/Makefile b/drivers/usb/phy/Makefile
index 24a9133..ca2fbaf 100644
--- a/drivers/usb/phy/Makefile
+++ b/drivers/usb/phy/Makefile
@@ -3,6 +3,7 @@
 #
 obj-$(CONFIG_USB_PHY)  += phy.o
 obj-$(CONFIG_OF)   += of.o
+obj-$(CONFIG_USB_OTG_WAKEUPSOURCE) += otg-wakeupsource.o
 
 # transceiver drivers, keep the list sorted
 
diff --git a/drivers/usb/phy/otg-wakeupsource.c 
b/drivers/usb/phy/otg-wakeupsource.c
new file mode 100644
index 000..d9a1720
--- /dev/null
+++ b/drivers/usb/phy/otg-wakeupsource.c
@@ -0,0 +1,136 @@
+/*
+ * otg-wakeupsource.c
+ *
+ * Copyright (C) 2011 Google, Inc.
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include linux/kernel.h
+#include linux/device.h
+#include linux/module.h
+#include linux/notifier.h
+#include linux/pm_wakeup.h
+#include linux/spinlock.h
+#include linux/usb/otg.h
+
+bool enabled = false;
+
+static DEFINE_SPINLOCK(otgws_spinlock);
+
+static void otgws_handle_event(struct usb_phy *otgws_xceiv, unsigned long 
event)
+{
+   unsigned long irqflags;
+
+   spin_lock_irqsave(otgws_spinlock, irqflags);
+
+   if (!enabled) {
+   __pm_relax(otgws_xceiv-wsource);
+   spin_unlock_irqrestore(otgws_spinlock, irqflags);
+   return;
+   }
+
+   switch (event) {
+   case USB_EVENT_VBUS:
+   case USB_EVENT_ENUMERATED:
+   __pm_stay_awake(otgws_xceiv-wsource);
+   break;
+
+   case USB_EVENT_NONE:
+   case USB_EVENT_ID:
+   case USB_EVENT_CHARGER:
+   __pm_relax(otgws_xceiv-wsource);
+   break;
+
+   default:
+   break;
+   }
+
+   spin_unlock_irqrestore(otgws_spinlock, irqflags);
+}
+

[RFC v3 2/2] usb: phy: Temporarily hold timed wakeup source on charger and disconnect events

2014-09-08 Thread Kiran Kumar Raparthy
From: Todd Poynor toddpoy...@google.com

usb: phy: Temporarily hold timed wakeup source on charger and disconnect
events

It temporarily holds a timed wakeup source on USB disconnect events, to allow
the rest of the system time to react to the USB disconnection (dropping host
sessions, updating charger status, etc.) prior to re-allowing suspend

Cc: Felipe Balbi ba...@ti.com
Cc: Greg Kroah-Hartman gre...@linuxfoundation.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-usb@vger.kernel.org
Cc: Android Kernel Team kernel-t...@android.com
Cc: John Stultz john.stu...@linaro.org
Cc: Sumit Semwal sumit.sem...@linaro.org
Signed-off-by: Todd Poynor toddpoy...@google.com
[kiran: Added context to commit message and
addressed community feedback]
Signed-off-by: Kiran Raparthy kiran.ku...@linaro.org
---
v3:
* Modified the commit log.
* Remove indirect function usb_temporary_hold_wsource and call
  __pm_wakeup_even directly.

v2:
* Implement usb_temporary_hold_wsource function in phy.

v1:
* Removed the refactoring logic from this integrated into main driver code.

RFC:
* Integrated the refactoring logic and logic to hold wakeupsource
  temporarily.

 drivers/usb/phy/otg-wakeupsource.c | 3 ++-
 include/linux/usb/phy.h| 2 ++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/phy/otg-wakeupsource.c 
b/drivers/usb/phy/otg-wakeupsource.c
index d9a1720..7bc9657 100644
--- a/drivers/usb/phy/otg-wakeupsource.c
+++ b/drivers/usb/phy/otg-wakeupsource.c
@@ -47,7 +47,8 @@ static void otgws_handle_event(struct usb_phy *otgws_xceiv, 
unsigned long event)
case USB_EVENT_NONE:
case USB_EVENT_ID:
case USB_EVENT_CHARGER:
-   __pm_relax(otgws_xceiv-wsource);
+   __pm_wakeup_event(otgws_xceiv-wsource,
+   msecs_to_jiffies(TEMPORARY_HOLD_TIME));
break;
 
default:
diff --git a/include/linux/usb/phy.h b/include/linux/usb/phy.h
index c71cf15..f8f9f1f 100644
--- a/include/linux/usb/phy.h
+++ b/include/linux/usb/phy.h
@@ -12,6 +12,8 @@
 #include linux/notifier.h
 #include linux/usb.h
 
+#define TEMPORARY_HOLD_TIME2000
+
 enum usb_phy_interface {
USBPHY_INTERFACE_MODE_UNKNOWN,
USBPHY_INTERFACE_MODE_UTMI,
-- 
1.8.2.1

--
To unsubscribe from this list: send the line unsubscribe linux-usb in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[RFC v2 1/2] usb: phy: Hold wakeupsource when USB is enumerated in peripheral mode

2014-09-02 Thread Kiran Kumar Raparthy
From: Todd Poynor toddpoy...@google.com

usb: phy: Hold wakeupsource when USB is enumerated in peripheral mode

Purpose of this is to prevent the system to enter into suspend state from USB
peripheral traffic by hodling a wakeupsource when USB is connected and
enumerated in peripheral mode(say adb).

Disabled by default, can enable with:
   echo Y  /sys/module/otg_wakeupsource/parameters/enabled

Cc: Felipe Balbi ba...@ti.com
Cc: Greg Kroah-Hartman gre...@linuxfoundation.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-usb@vger.kernel.org
Cc: Android Kernel Team kernel-t...@android.com
Cc: John Stultz john.stu...@linaro.org
Cc: Arve Hj�nnev�g a...@android.com
Cc: Benoit Goby ben...@android.com
Signed-off-by: Todd Poynor toddpoy...@google.com
[kiran: Added context to commit message.
Included build fix from Benoit Goby and Arve Hj�nnev�g.
Removed lock-held field in driver as this mechanism is
provided in wakeupsource driver.
wakelock(wl) terminology replaced with wakeup_source(ws).
changed to disabled by default from enable by default.
sys entry(module param) field modified to otg_wakeupsource.
included Todd's refactoring logic.
Introduced get_phy_hook to handle otgws_xceiv per-PHY.
otgws_nb moved to otg_wakeupsource_init function.
wakeupsource handling implemeted per-PHY(in phy core).
modified Kconfig help text]
Signed-off-by: Kiran Raparthy kiran.ku...@linaro.org
---
 drivers/usb/phy/Kconfig|   8 +++
 drivers/usb/phy/Makefile   |   1 +
 drivers/usb/phy/otg-wakeupsource.c | 140 +
 drivers/usb/phy/phy.c  |  42 +++
 include/linux/usb/phy.h|   8 +++
 5 files changed, 199 insertions(+)
 create mode 100644 drivers/usb/phy/otg-wakeupsource.c

diff --git a/drivers/usb/phy/Kconfig b/drivers/usb/phy/Kconfig
index e253fa0..d9ddd85 100644
--- a/drivers/usb/phy/Kconfig
+++ b/drivers/usb/phy/Kconfig
@@ -6,6 +6,14 @@ menu USB Physical Layer drivers
 config USB_PHY
def_bool n
 
+config USB_OTG_WAKEUPSOURCE
+   bool Hold wakeupsource when USB is enumerated in peripheral mode
+   depends on PM_SLEEP
+   select USB_PHY
+   help
+ Prevent the system going into automatic suspend while
+ it is attached as a USB peripheral by holding a wakeupsource.
+
 #
 # USB Transceiver Drivers
 #
diff --git a/drivers/usb/phy/Makefile b/drivers/usb/phy/Makefile
index 24a9133..ca2fbaf 100644
--- a/drivers/usb/phy/Makefile
+++ b/drivers/usb/phy/Makefile
@@ -3,6 +3,7 @@
 #
 obj-$(CONFIG_USB_PHY)  += phy.o
 obj-$(CONFIG_OF)   += of.o
+obj-$(CONFIG_USB_OTG_WAKEUPSOURCE) += otg-wakeupsource.o
 
 # transceiver drivers, keep the list sorted
 
diff --git a/drivers/usb/phy/otg-wakeupsource.c 
b/drivers/usb/phy/otg-wakeupsource.c
new file mode 100644
index 000..fca2010
--- /dev/null
+++ b/drivers/usb/phy/otg-wakeupsource.c
@@ -0,0 +1,140 @@
+/*
+ * otg-wakeupsource.c
+ *
+ * Copyright (C) 2011 Google, Inc.
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include linux/kernel.h
+#include linux/device.h
+#include linux/module.h
+#include linux/notifier.h
+#include linux/pm_wakeup.h
+#include linux/spinlock.h
+#include linux/usb/otg.h
+
+bool enabled = false;
+
+static DEFINE_SPINLOCK(otgws_spinlock);
+static struct usb_phy *get_phy_hook(void);
+
+static void otgws_handle_event(struct usb_phy *otgws_xceiv, unsigned long 
event)
+{
+   unsigned long irqflags;
+
+   spin_lock_irqsave(otgws_spinlock, irqflags);
+
+   if (!enabled) {
+   usb_drop_wsource(otgws_xceiv);
+   spin_unlock_irqrestore(otgws_spinlock, irqflags);
+   return;
+   }
+
+   switch (event) {
+   case USB_EVENT_VBUS:
+   case USB_EVENT_ENUMERATED:
+   usb_grab_wsource(otgws_xceiv);
+   break;
+
+   case USB_EVENT_NONE:
+   case USB_EVENT_ID:
+   case USB_EVENT_CHARGER:
+   usb_drop_wsource(otgws_xceiv);
+   break;
+
+   default:
+   break;
+   }
+
+   spin_unlock_irqrestore(otgws_spinlock, irqflags);
+}
+static struct usb_phy *get_phy_hook(void)
+{
+   struct usb_phy *phy;
+
+   phy = usb_get_phy(USB_PHY_TYPE_USB2);
+
+   if (IS_ERR(phy)) {
+   pr_err(%s: No OTG transceiver found\n, __func__);
+   return NULL;
+   }
+
+   return phy;
+}
+static int otgws_otg_notifications(struct notifier_block *nb,
+

[RFC v2 2/2] usb: phy: Temporarily hold wakeupsource on charger connect and disconnect events

2014-09-02 Thread Kiran Kumar Raparthy
From: Todd Poynor toddpoy...@google.com

usb: phy: Temporarily hold wakeupsource on charger connect and disconnect
events

Allow other parts of the system to react to the charger connect/disconnect
event without allowing the system to suspend before the other parts can process
the event. This wakeup_source times out after 2 seconds; if nobody else holds a
wakeup_source by that time then the device can sleep.

Cc: Felipe Balbi ba...@ti.com
Cc: Greg Kroah-Hartman gre...@linuxfoundation.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-usb@vger.kernel.org
Cc: Android Kernel Team kernel-t...@android.com
Cc: John Stultz john.stu...@linaro.org
Signed-off-by: Todd Poynor toddpoy...@google.com
[kiran: Added context to commit message
wakeupsource handling implemented per PHY in phy core]
Signed-off-by: Kiran Raparthy kiran.ku...@linaro.org
---
 drivers/usb/phy/otg-wakeupsource.c |  2 +-
 drivers/usb/phy/phy.c  | 10 ++
 include/linux/usb/phy.h|  3 +++
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/drivers/usb/phy/otg-wakeupsource.c 
b/drivers/usb/phy/otg-wakeupsource.c
index fca2010..70fa05e 100644
--- a/drivers/usb/phy/otg-wakeupsource.c
+++ b/drivers/usb/phy/otg-wakeupsource.c
@@ -48,7 +48,7 @@ static void otgws_handle_event(struct usb_phy *otgws_xceiv, 
unsigned long event)
case USB_EVENT_NONE:
case USB_EVENT_ID:
case USB_EVENT_CHARGER:
-   usb_drop_wsource(otgws_xceiv);
+   usb_temporary_hold_wsource(otgws_xceiv);
break;
 
default:
diff --git a/drivers/usb/phy/phy.c b/drivers/usb/phy/phy.c
index 659b1e8..c488716 100644
--- a/drivers/usb/phy/phy.c
+++ b/drivers/usb/phy/phy.c
@@ -483,3 +483,13 @@ void usb_drop_wsource(struct usb_phy *x)
__pm_relax(x-wsource);
 }
 EXPORT_SYMBOL_GPL(usb_drop_wsource);
+
+/**
+ * usb_temporary_hold_wsource - temporarily hold wakeupsource
+ * @usb_phy: the phy returned by usb_get_phy()
+ */
+void usb_temporary_hold_wsource(struct usb_phy *x)
+{
+   __pm_wakeup_event(x-wsource, msecs_to_jiffies(TEMPORARY_HOLD_TIME));
+}
+EXPORT_SYMBOL_GPL(usb_temporary_hold_wsource);
diff --git a/include/linux/usb/phy.h b/include/linux/usb/phy.h
index 6c680e6..5f9b70e 100644
--- a/include/linux/usb/phy.h
+++ b/include/linux/usb/phy.h
@@ -13,6 +13,8 @@
 #include linux/usb.h
 #include linux/pm_wakeup.h
 
+#define TEMPORARY_HOLD_TIME2000
+
 enum usb_phy_interface {
USBPHY_INTERFACE_MODE_UNKNOWN,
USBPHY_INTERFACE_MODE_UTMI,
@@ -218,6 +220,7 @@ void usb_wsource_init(struct usb_phy *x, char 
*phy_dev_name);
 void usb_wsource_trash(struct usb_phy *x);
 void usb_grab_wsource(struct usb_phy *x);
 void usb_drop_wsource(struct usb_phy *x);
+void usb_temporary_hold_wsource(struct usb_phy *x);
 #else
 static inline struct usb_phy *usb_get_phy(enum usb_phy_type type)
 {
-- 
1.8.2.1

--
To unsubscribe from this list: send the line unsubscribe linux-usb in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[RFC v1 1/2] USB: OTG: Hold wakeupsource when VBUS present

2014-08-28 Thread Kiran Kumar Raparthy
From: Todd Poynor toddpoy...@google.com

USB: OTG: Hold wakeupsource when VBUS present

Purpose of this is to prevent the system to enter into
suspend state from USB peripheral traffic by hodling a
wakeupsource when USB(otg) is connected and enumerated
in peripheral mode(say adb).

Disabled by default, can enable with:
   echo Y  /sys/module/otg_wakeupsource/parameters/enabled

Cc: Felipe Balbi ba...@ti.com
Cc: Greg Kroah-Hartman gre...@linuxfoundation.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-usb@vger.kernel.org
Cc: Android Kernel Team kernel-t...@android.com
Cc: John Stultz john.stu...@linaro.org
Cc: Arve Hj�nnev�g a...@android.com
Cc: Benoit Goby ben...@android.com
Signed-off-by: Todd Poynor toddpoy...@google.com
[kiran: Added context to commit message.
Included build fix from Benoit Goby and Arve Hj�nnev�g.
Removed lock-held field in driver as this mechanism is
provided in wakeupsource driver.
wakelock(wl) terminology replaced with wakeup_source(ws).
changed to disabled by default from enable by default.
sys entry(module param) field modified to otg_wakeupsource.
included Todd's refactoring logic.
Introduced get_phy_hook to handle otgws_xceiv per-PHY.
otgws_nb moved to otg_wakeupsource_init function.
__pm_stay_awake and __pm_relax called directly from the main.
code instead of calling them via otgws_grab,otgws_drop.
modified Kconfig help text]
Signed-off-by: Kiran Raparthy kiran.ku...@linaro.org
---
 drivers/usb/phy/Kconfig|   8 +++
 drivers/usb/phy/Makefile   |   1 +
 drivers/usb/phy/otg-wakeupsource.c | 144 +
 3 files changed, 153 insertions(+)
 create mode 100644 drivers/usb/phy/otg-wakeupsource.c

diff --git a/drivers/usb/phy/Kconfig b/drivers/usb/phy/Kconfig
index e253fa0..d9ddd85 100644
--- a/drivers/usb/phy/Kconfig
+++ b/drivers/usb/phy/Kconfig
@@ -6,6 +6,14 @@ menu USB Physical Layer drivers
 config USB_PHY
def_bool n
 
+config USB_OTG_WAKEUPSOURCE
+   bool Hold wakeupsource when USB is enumerated in peripheral mode
+   depends on PM_SLEEP
+   select USB_PHY
+   help
+ Prevent the system going into automatic suspend while
+ it is attached as a USB peripheral by holding a wakeupsource.
+
 #
 # USB Transceiver Drivers
 #
diff --git a/drivers/usb/phy/Makefile b/drivers/usb/phy/Makefile
index 24a9133..ca2fbaf 100644
--- a/drivers/usb/phy/Makefile
+++ b/drivers/usb/phy/Makefile
@@ -3,6 +3,7 @@
 #
 obj-$(CONFIG_USB_PHY)  += phy.o
 obj-$(CONFIG_OF)   += of.o
+obj-$(CONFIG_USB_OTG_WAKEUPSOURCE) += otg-wakeupsource.o
 
 # transceiver drivers, keep the list sorted
 
diff --git a/drivers/usb/phy/otg-wakeupsource.c 
b/drivers/usb/phy/otg-wakeupsource.c
new file mode 100644
index 000..7c838d1
--- /dev/null
+++ b/drivers/usb/phy/otg-wakeupsource.c
@@ -0,0 +1,144 @@
+/*
+ * otg-wakeupsource.c
+ *
+ * Copyright (C) 2011 Google, Inc.
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include linux/kernel.h
+#include linux/device.h
+#include linux/module.h
+#include linux/notifier.h
+#include linux/pm_wakeup.h
+#include linux/spinlock.h
+#include linux/usb/otg.h
+
+bool enabled = false;
+
+static DEFINE_SPINLOCK(otgws_spinlock);
+static struct usb_phy *get_phy_hook(void);
+
+/* Only one lock, but since these 2 fields are associated with each other... */
+
+struct otgws_lock {
+   char name[40];
+   struct wakeup_source wsource;
+};
+
+/* VBUS present lock */
+
+static struct otgws_lock vbus_lock;
+
+static void otgws_handle_event(unsigned long event)
+{
+   unsigned long irqflags;
+
+   spin_lock_irqsave(otgws_spinlock, irqflags);
+
+   if (!enabled) {
+   __pm_relax(vbus_lock.wsource);
+   spin_unlock_irqrestore(otgws_spinlock, irqflags);
+   return;
+   }
+
+   switch (event) {
+   case USB_EVENT_VBUS:
+   case USB_EVENT_ENUMERATED:
+   __pm_stay_awake(vbus_lock.wsource);
+   break;
+
+   case USB_EVENT_NONE:
+   case USB_EVENT_ID:
+   case USB_EVENT_CHARGER:
+   __pm_relax(vbus_lock.wsource);
+   break;
+
+   default:
+   break;
+   }
+
+   spin_unlock_irqrestore(otgws_spinlock, irqflags);
+}
+static struct usb_phy *get_phy_hook(void)
+{
+   struct usb_phy *phy;
+
+   phy = usb_get_phy(USB_PHY_TYPE_USB2);
+
+   if (IS_ERR(phy)) {
+   pr_err(%s: No OTG transceiver 

[RFC v1 2/2] usb: otg: Temporarily hold wakeupsource on charger and disconnect events

2014-08-28 Thread Kiran Kumar Raparthy
From: Todd Poynor toddpoy...@google.com

usb: otg: Temporarily hold wakeupsource on charger and disconnect events

Allow other parts of the system to react to the charger connect/disconnect
event without allowing the system to suspend before the other parts can process
the event. This wakeup_source times out after 2 seconds; if nobody else holds a
wakeup_source by that time then the device can sleep.

Cc: Felipe Balbi ba...@ti.com
Cc: Greg Kroah-Hartman gre...@linuxfoundation.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-usb@vger.kernel.org
Cc: Android Kernel Team kernel-t...@android.com
Cc: John Stultz john.stu...@linaro.org
Signed-off-by: Todd Poynor toddpoy...@google.com
[kiran: Added context to commit message]
Signed-off-by: Kiran Raparthy kiran.ku...@linaro.org
---
 drivers/usb/phy/otg-wakeupsource.c | 10 +++---
 include/linux/usb/otg.h|  2 ++
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/phy/otg-wakeupsource.c 
b/drivers/usb/phy/otg-wakeupsource.c
index 7c838d1..9f3c5c1 100644
--- a/drivers/usb/phy/otg-wakeupsource.c
+++ b/drivers/usb/phy/otg-wakeupsource.c
@@ -34,8 +34,11 @@ struct otgws_lock {
struct wakeup_source wsource;
 };
 
-/* VBUS present lock */
-
+/*
+ * VBUS present lock.  Also used as a timed lock on charger
+ * connect/disconnect and USB host disconnect, to allow the system
+ * to react to the change in power.
+ */
 static struct otgws_lock vbus_lock;
 
 static void otgws_handle_event(unsigned long event)
@@ -59,7 +62,8 @@ static void otgws_handle_event(unsigned long event)
case USB_EVENT_NONE:
case USB_EVENT_ID:
case USB_EVENT_CHARGER:
-   __pm_relax(vbus_lock.wsource);
+   __pm_wakeup_event(vbus_lock.wsource,
+   msecs_to_jiffies(TEMPORARY_HOLD_TIME));
break;
 
default:
diff --git a/include/linux/usb/otg.h b/include/linux/usb/otg.h
index 154332b..4243747 100644
--- a/include/linux/usb/otg.h
+++ b/include/linux/usb/otg.h
@@ -11,6 +11,8 @@
 
 #include linux/usb/phy.h
 
+#define TEMPORARY_HOLD_TIME2000
+
 struct usb_otg {
u8  default_a;
 
-- 
1.8.2.1

--
To unsubscribe from this list: send the line unsubscribe linux-usb in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[RFC v1 2/2] usb: otg: Temporarily hold wakeupsource on charger and disconnect events

2014-08-28 Thread Kiran Kumar Raparthy
From: Todd Poynor toddpoy...@google.com

usb: otg: Temporarily hold wakeupsource on charger and disconnect events

Allow other parts of the system to react to the charger connect/disconnect
event without allowing the system to suspend before the other parts can process
the event. This wakeup_source times out after 2 seconds; if nobody else holds a
wakeup_source by that time then the device can sleep.

Cc: Felipe Balbi ba...@ti.com
Cc: Greg Kroah-Hartman gre...@linuxfoundation.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-usb@vger.kernel.org
Cc: Android Kernel Team kernel-t...@android.com
Cc: John Stultz john.stu...@linaro.org
Signed-off-by: Todd Poynor toddpoy...@google.com
[kiran: Added context to commit message]
Signed-off-by: Kiran Raparthy kiran.ku...@linaro.org
---
 drivers/usb/phy/otg-wakeupsource.c | 10 +++---
 include/linux/usb/otg.h|  2 ++
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/phy/otg-wakeupsource.c 
b/drivers/usb/phy/otg-wakeupsource.c
index 7c838d1..9f3c5c1 100644
--- a/drivers/usb/phy/otg-wakeupsource.c
+++ b/drivers/usb/phy/otg-wakeupsource.c
@@ -34,8 +34,11 @@ struct otgws_lock {
struct wakeup_source wsource;
 };
 
-/* VBUS present lock */
-
+/*
+ * VBUS present lock.  Also used as a timed lock on charger
+ * connect/disconnect and USB host disconnect, to allow the system
+ * to react to the change in power.
+ */
 static struct otgws_lock vbus_lock;
 
 static void otgws_handle_event(unsigned long event)
@@ -59,7 +62,8 @@ static void otgws_handle_event(unsigned long event)
case USB_EVENT_NONE:
case USB_EVENT_ID:
case USB_EVENT_CHARGER:
-   __pm_relax(vbus_lock.wsource);
+   __pm_wakeup_event(vbus_lock.wsource,
+   msecs_to_jiffies(TEMPORARY_HOLD_TIME));
break;
 
default:
diff --git a/include/linux/usb/otg.h b/include/linux/usb/otg.h
index 154332b..4243747 100644
--- a/include/linux/usb/otg.h
+++ b/include/linux/usb/otg.h
@@ -11,6 +11,8 @@
 
 #include linux/usb/phy.h
 
+#define TEMPORARY_HOLD_TIME2000
+
 struct usb_otg {
u8  default_a;
 
-- 
1.8.2.1

--
To unsubscribe from this list: send the line unsubscribe linux-usb in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[RFC v1 2/2] usb: otg: Temporarily hold wakeupsource on charger

2014-08-28 Thread Kiran Kumar Raparthy
From: Todd Poynor toddpoy...@google.com

usb: otg: Temporarily hold wakeupsource on charger connect and disconnect
events

Allow other parts of the system to react to the charger connect/disconnect
event without allowing the system to suspend before the other parts can process
the event. This wakeup_source times out after 2 seconds; if nobody else holds a
wakeup_source by that time then the device can sleep.

Cc: Felipe Balbi ba...@ti.com
Cc: Greg Kroah-Hartman gre...@linuxfoundation.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-usb@vger.kernel.org
Cc: Android Kernel Team kernel-t...@android.com
Cc: John Stultz john.stu...@linaro.org
Signed-off-by: Todd Poynor toddpoy...@google.com
[kiran: Added context to commit message]
Signed-off-by: Kiran Raparthy kiran.ku...@linaro.org
---
 drivers/usb/phy/otg-wakeupsource.c | 10 +++---
 include/linux/usb/otg.h|  2 ++
 2 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/phy/otg-wakeupsource.c 
b/drivers/usb/phy/otg-wakeupsource.c
index 7c838d1..9f3c5c1 100644
--- a/drivers/usb/phy/otg-wakeupsource.c
+++ b/drivers/usb/phy/otg-wakeupsource.c
@@ -34,8 +34,11 @@ struct otgws_lock {
struct wakeup_source wsource;
 };
 
-/* VBUS present lock */
-
+/*
+ * VBUS present lock.  Also used as a timed lock on charger
+ * connect/disconnect and USB host disconnect, to allow the system
+ * to react to the change in power.
+ */
 static struct otgws_lock vbus_lock;
 
 static void otgws_handle_event(unsigned long event)
@@ -59,7 +62,8 @@ static void otgws_handle_event(unsigned long event)
case USB_EVENT_NONE:
case USB_EVENT_ID:
case USB_EVENT_CHARGER:
-   __pm_relax(vbus_lock.wsource);
+   __pm_wakeup_event(vbus_lock.wsource,
+   msecs_to_jiffies(TEMPORARY_HOLD_TIME));
break;
 
default:
diff --git a/include/linux/usb/otg.h b/include/linux/usb/otg.h
index 154332b..4243747 100644
--- a/include/linux/usb/otg.h
+++ b/include/linux/usb/otg.h
@@ -11,6 +11,8 @@
 
 #include linux/usb/phy.h
 
+#define TEMPORARY_HOLD_TIME2000
+
 struct usb_otg {
u8  default_a;
 
-- 
1.8.2.1

--
To unsubscribe from this list: send the line unsubscribe linux-usb in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[RFC 1/2] USB: OTG: Hold wakeupsource when VBUS present

2014-08-22 Thread Kiran Kumar Raparthy
From: Todd Poynor toddpoy...@google.com

USB: OTG: Hold wakeupsource when VBUS present

Enabled by default, can disable with:
   echo N  /sys/module/otg_wakeupsource/parameters/enabled

This is one of the number of patches from the Android AOSP common.git tree,
which is used on almost all Android devices. so I wanted to submit it for
review to see if it should go upstream.

Cc: Felipe Balbi ba...@ti.com
Cc: Greg Kroah-Hartman gre...@linuxfoundation.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-usb@vger.kernel.org
Cc: Android Kernel Team kernel-t...@android.com
Cc: John Stultz john.stu...@linaro.org
Cc: Arve Hj�nnev�g a...@android.com
Cc: Benoit Goby ben...@android.com
Signed-off-by: Todd Poynor toddpoy...@google.com
Signed-off-by: Kiran Raparthy kiran.ku...@linaro.org
[kiran: Added context to commit message
Included build fix from Benoit Goby and Arve Arve Hj�nnev�g
Removed lock-held field in driver as this mechanism is provided in 
wakeup.c
wakelock(wl) terminology replaced with wakeup_source(ws)
sys entry(module param) field modified to otg_wakeupsource
__pm_stay_awake and __pm_relax called directly from the main
code instead of calling them via otgws_grab,otgws_drop]
---
 drivers/usb/phy/Kconfig|   8 ++
 drivers/usb/phy/Makefile   |   1 +
 drivers/usb/phy/otg-wakeupsource.c | 171 +
 3 files changed, 180 insertions(+)
 create mode 100644 drivers/usb/phy/otg-wakeupsource.c

diff --git a/drivers/usb/phy/Kconfig b/drivers/usb/phy/Kconfig
index e253fa0..9c747b2 100644
--- a/drivers/usb/phy/Kconfig
+++ b/drivers/usb/phy/Kconfig
@@ -6,6 +6,14 @@ menu USB Physical Layer drivers
 config USB_PHY
def_bool n
 
+config USB_OTG_WAKEUPSOURCE
+   bool Hold a wakeupsource when USB connected
+   depends on PM_SLEEP
+   select USB_PHY
+   help
+ Select this to automatically hold a wakeupsource when USB is
+ connected, preventing suspend.
+
 #
 # USB Transceiver Drivers
 #
diff --git a/drivers/usb/phy/Makefile b/drivers/usb/phy/Makefile
index 24a9133..ca2fbaf 100644
--- a/drivers/usb/phy/Makefile
+++ b/drivers/usb/phy/Makefile
@@ -3,6 +3,7 @@
 #
 obj-$(CONFIG_USB_PHY)  += phy.o
 obj-$(CONFIG_OF)   += of.o
+obj-$(CONFIG_USB_OTG_WAKEUPSOURCE) += otg-wakeupsource.o
 
 # transceiver drivers, keep the list sorted
 
diff --git a/drivers/usb/phy/otg-wakeupsource.c 
b/drivers/usb/phy/otg-wakeupsource.c
new file mode 100644
index 000..fa44e11
--- /dev/null
+++ b/drivers/usb/phy/otg-wakeupsource.c
@@ -0,0 +1,171 @@
+/*
+ * otg-wakeupsource.c
+ *
+ * Copyright (C) 2011 Google, Inc.
+ *
+ * This software is licensed under the terms of the GNU General Public
+ * License version 2, as published by the Free Software Foundation, and
+ * may be copied, distributed, and modified under those terms.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ */
+
+#include linux/kernel.h
+#include linux/device.h
+#include linux/module.h
+#include linux/notifier.h
+#include linux/pm_wakeup.h
+#include linux/spinlock.h
+#include linux/usb/otg.h
+
+static bool enabled = true;
+static struct usb_phy *otgws_xceiv;
+static struct notifier_block otgws_nb;
+
+
+static DEFINE_SPINLOCK(otgws_spinlock);
+
+/*
+ * Only one lock, but since these 2 fields are associated with each other...
+ */
+
+struct otgws_lock {
+   char name[40];
+   struct wakeup_source wsource;
+};
+
+/*
+ * VBUS present lock.
+ */
+
+static struct otgws_lock vbus_lock;
+
+static int otgws_otg_notifications(struct notifier_block *nb,
+  unsigned long event, void *unused)
+{
+   unsigned long irqflags;
+
+   if (!enabled)
+   return NOTIFY_OK;
+
+   spin_lock_irqsave(otgws_spinlock, irqflags);
+
+   switch (event) {
+   case USB_EVENT_VBUS:
+   case USB_EVENT_ENUMERATED:
+   __pm_stay_awake(vbus_lock.wsource);
+   break;
+
+   case USB_EVENT_NONE:
+   case USB_EVENT_ID:
+   case USB_EVENT_CHARGER:
+   __pm_relax(vbus_lock.wsource);
+   break;
+
+   default:
+   break;
+   }
+
+   spin_unlock_irqrestore(otgws_spinlock, irqflags);
+   return NOTIFY_OK;
+}
+
+static void sync_with_xceiv_state(void)
+{
+   if ((otgws_xceiv-last_event == USB_EVENT_VBUS) ||
+   (otgws_xceiv-last_event == USB_EVENT_ENUMERATED))
+   __pm_stay_awake(vbus_lock.wsource);
+   else
+   __pm_relax(vbus_lock.wsource);
+}
+
+static int init_for_xceiv(void)
+{
+   int rv;
+   struct usb_phy *phy;
+
+   if (!otgws_xceiv) {
+   phy = usb_get_phy(USB_PHY_TYPE_USB2);
+
+   if (IS_ERR(phy)) {
+   

[RFC 2/2] usb: otg: Temporarily hold wakeupsource on charger and disconnect events

2014-08-22 Thread Kiran Kumar Raparthy
From: Todd Poynor toddpoy...@google.com

usb: otg: Temporarily hold wakeupsource on charger and disconnect events

Allow other parts of the system to react to the charger connect/disconnect
event without allowing the system to suspend before the other parts can process
the event. This wakeup_source times out after 2 seconds; if nobody else holds a
wakeup_source by that time then the device can sleep.

This patch also refactoras the logic of wakeupsource_init,otg_notifications and
handle event funtions

This is one of the number of patches from the Android AOSP tegra.git tree,
which is used on Android devices. so I wanted to submit it for
review to see if it should go upstream.

Cc: Felipe Balbi ba...@ti.com
Cc: Greg Kroah-Hartman gre...@linuxfoundation.org
Cc: linux-ker...@vger.kernel.org
Cc: linux-usb@vger.kernel.org
Cc: Android Kernel Team kernel-t...@android.com
Cc: John Stultz john.stu...@linaro.org
Signed-off-by: Todd Poynor toddpoy...@google.com
Signed-off-by: Kiran Raparthy kiran.ku...@linaro.org
[kiran: Added context to commit message]
---
 drivers/usb/phy/otg-wakeupsource.c | 108 ++---
 include/linux/usb/otg.h|   2 +
 2 files changed, 41 insertions(+), 69 deletions(-)

diff --git a/drivers/usb/phy/otg-wakeupsource.c 
b/drivers/usb/phy/otg-wakeupsource.c
index fa44e11..d2c16b8 100644
--- a/drivers/usb/phy/otg-wakeupsource.c
+++ b/drivers/usb/phy/otg-wakeupsource.c
@@ -39,21 +39,25 @@ struct otgws_lock {
 };
 
 /*
- * VBUS present lock.
++ * VBUS present lock.  Also used as a timed lock on charger
++ * connect/disconnect and USB host disconnect, to allow the system
++ * to react to the change in power.
  */
 
 static struct otgws_lock vbus_lock;
 
-static int otgws_otg_notifications(struct notifier_block *nb,
-  unsigned long event, void *unused)
+static void otgws_handle_event(unsigned long event)
 {
unsigned long irqflags;
 
-   if (!enabled)
-   return NOTIFY_OK;
-
spin_lock_irqsave(otgws_spinlock, irqflags);
 
+   if (!enabled) {
+   __pm_relax(vbus_lock.wsource);
+   spin_unlock_irqrestore(otgws_spinlock, irqflags);
+   return;
+   }
+
switch (event) {
case USB_EVENT_VBUS:
case USB_EVENT_ENUMERATED:
@@ -63,7 +67,8 @@ static int otgws_otg_notifications(struct notifier_block *nb,
case USB_EVENT_NONE:
case USB_EVENT_ID:
case USB_EVENT_CHARGER:
-   __pm_relax(vbus_lock.wsource);
+   __pm_wakeup_event(vbus_lock.wsource,
+   msecs_to_jiffies(TEMPORARY_HOLD_TIME));
break;
 
default:
@@ -71,72 +76,25 @@ static int otgws_otg_notifications(struct notifier_block 
*nb,
}
 
spin_unlock_irqrestore(otgws_spinlock, irqflags);
-   return NOTIFY_OK;
-}
-
-static void sync_with_xceiv_state(void)
-{
-   if ((otgws_xceiv-last_event == USB_EVENT_VBUS) ||
-   (otgws_xceiv-last_event == USB_EVENT_ENUMERATED))
-   __pm_stay_awake(vbus_lock.wsource);
-   else
-   __pm_relax(vbus_lock.wsource);
 }
 
-static int init_for_xceiv(void)
+static int otgws_otg_notifications(struct notifier_block *nb,
+   unsigned long event, void *unused)
 {
-   int rv;
-   struct usb_phy *phy;
-
-   if (!otgws_xceiv) {
-   phy = usb_get_phy(USB_PHY_TYPE_USB2);
-
-   if (IS_ERR(phy)) {
-   pr_err(%s: No USB transceiver found\n, __func__);
-   return PTR_ERR(phy);
-   }
-   otgws_xceiv = phy;
-
-   snprintf(vbus_lock.name, sizeof(vbus_lock.name), vbus-%s,
-dev_name(otgws_xceiv-dev));
-   wakeup_source_init(vbus_lock.wsource, vbus_lock.name);
-
-   rv = usb_register_notifier(otgws_xceiv, otgws_nb);
-
-   if (rv) {
-   pr_err(%s: usb_register_notifier on transceiver %s
-   failed\n, __func__,
-  dev_name(otgws_xceiv-dev));
-   otgws_xceiv = NULL;
-   wakeup_source_trash(vbus_lock.wsource);
-   return rv;
-   }
-   }
-
-   return 0;
+   otgws_handle_event(event);
+   return NOTIFY_OK;
 }
 
 static int set_enabled(const char *val, const struct kernel_param *kp)
 {
-   unsigned long irqflags;
int rv = param_set_bool(val, kp);
 
if (rv)
return rv;
 
-   rv = init_for_xceiv();
-
-   if (rv)
-   return rv;
-
-   spin_lock_irqsave(otgws_spinlock, irqflags);
-
-   if (enabled)
-   sync_with_xceiv_state();
-   else
-   __pm_relax(vbus_lock.wsource);
+   if (otgws_xceiv)
+   otgws_handle_event(otgws_xceiv-last_event);
 
-   spin_unlock_irqrestore(otgws_spinlock, irqflags);