phy: twl4030-usb: make runtime pm more reliable.
authorNeilBrown <neilb@suse.de>
Thu, 16 Apr 2015 08:03:04 +0000 (18:03 +1000)
committerKishon Vijay Abraham I <kishon@ti.com>
Mon, 1 Jun 2015 10:38:24 +0000 (16:08 +0530)
A construct like:

        if (pm_runtime_suspended(twl->dev))
               pm_runtime_get_sync(twl->dev);

is against the spirit of the runtime_pm interface as it
makes the internal refcounting useless.

In this case it is also racy, particularly as 'put_autosuspend'
is used to drop a reference.
When that happens a timer is started and the device is
runtime-suspended after the timeout.
If the above code runs in this window, the device will not be
found to be suspended so no pm_runtime reference is taken.
When the timer expires the device will be suspended, which is
against the intention of the code.

So be more direct is taking and dropping references.
If twl->linkstat is VBUS_VALID or ID_GROUND, then hold a
pm_runtime reference, otherwise don't.
Define "cable_present()" to test for this condition.

Tested-by: Tony Lindgren <tony@atomide.com>
Signed-off-by: NeilBrown <neilb@suse.de>
Signed-off-by: Kishon Vijay Abraham I <kishon@ti.com>
drivers/phy/phy-twl4030-usb.c

index bc42d6a8939f4efd85fb1e10ea6d12faa1b76e97..3078f80bf520530fa500155700ed8690be6d6b9c 100644 (file)
 #define PMBR1                          0x0D
 #define GPIO_USB_4PIN_ULPI_2430C       (3 << 0)
 
+/*
+ * If VBUS is valid or ID is ground, then we know a
+ * cable is present and we need to be runtime-enabled
+ */
+static inline bool cable_present(enum omap_musb_vbus_id_status stat)
+{
+       return stat == OMAP_MUSB_VBUS_VALID ||
+               stat == OMAP_MUSB_ID_GROUND;
+}
+
 struct twl4030_usb {
        struct usb_phy          phy;
        struct device           *dev;
@@ -536,8 +546,10 @@ static irqreturn_t twl4030_usb_irq(int irq, void *_twl)
 
        mutex_lock(&twl->lock);
        if (status >= 0 && status != twl->linkstat) {
+               status_changed =
+                       cable_present(twl->linkstat) !=
+                       cable_present(status);
                twl->linkstat = status;
-               status_changed = true;
        }
        mutex_unlock(&twl->lock);
 
@@ -553,15 +565,11 @@ static irqreturn_t twl4030_usb_irq(int irq, void *_twl)
                 * USB_LINK_VBUS state.  musb_hdrc won't care until it
                 * starts to handle softconnect right.
                 */
-               if ((status == OMAP_MUSB_VBUS_VALID) ||
-                   (status == OMAP_MUSB_ID_GROUND)) {
-                       if (pm_runtime_suspended(twl->dev))
-                               pm_runtime_get_sync(twl->dev);
+               if (cable_present(status)) {
+                       pm_runtime_get_sync(twl->dev);
                } else {
-                       if (pm_runtime_active(twl->dev)) {
-                               pm_runtime_mark_last_busy(twl->dev);
-                               pm_runtime_put_autosuspend(twl->dev);
-                       }
+                       pm_runtime_mark_last_busy(twl->dev);
+                       pm_runtime_put_autosuspend(twl->dev);
                }
                omap_musb_mailbox(status);
        }
@@ -767,6 +775,9 @@ static int twl4030_usb_remove(struct platform_device *pdev)
 
        /* disable complete OTG block */
        twl4030_usb_clear_bits(twl, POWER_CTRL, POWER_CTRL_OTG_ENAB);
+
+       if (cable_present(twl->linkstat))
+               pm_runtime_put_noidle(twl->dev);
        pm_runtime_mark_last_busy(twl->dev);
        pm_runtime_put(twl->dev);