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-kernel@vger.kernel.org
Cc: linux-...@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_source    wsource;
+
        /* 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-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to