From patchwork Fri Aug 22 09:50:00 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kiran Kumar Raparthy X-Patchwork-Id: 35793 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-ie0-f198.google.com (mail-ie0-f198.google.com [209.85.223.198]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 8C0302057E for ; Fri, 22 Aug 2014 09:50:30 +0000 (UTC) Received: by mail-ie0-f198.google.com with SMTP id rl12sf23461966iec.5 for ; Fri, 22 Aug 2014 02:50:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:sender:precedence:list-id:x-original-sender :x-original-authentication-results:mailing-list:list-post:list-help :list-archive:list-unsubscribe; bh=bDLHwtI6xiIUQ3RUTexvQSEQVrz6rkHr6IdKMbKP/bA=; b=UE344lPskyX0YbXiyXp1tCe+FTO/mAmkMT0Mw50Pvmn3W+sZAwaW7WTFzoEWsW2uLc 0cmdE7IIW1XkUb76phIZz71mY3yt1jUhtZ9Vs4Pdtl/qg8l2TJwaoIK9ujVIfLQ97EO0 j7m931SAdiXZcohXze5duYi95oBy/Av6Icyfi7piCNwTmHnE56RtOwQCHsBlQqNMi926 ISyA/FMeKJdPSKcmNn4y88F2VqjmSFaffQYOtRvqe29K5/iT/MummMXE6CPV80h/UcmU dPOD1ObdF9d0bIqG8Ne0ND69P4GyuP5gUshoux0QPiZhLja1bRM/Pedn2y56sgYsTZds hHFA== X-Gm-Message-State: ALoCoQk88wPkZB1iQJjXH5oDlDC93QyRp7sYWav9v0KPX/HbrHTbvzE38eaFLG/7ZbsBo8QQGi2P X-Received: by 10.42.26.12 with SMTP id d12mr6126343icc.24.1408701030120; Fri, 22 Aug 2014 02:50:30 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.140.102.202 with SMTP id w68ls1048357qge.4.gmail; Fri, 22 Aug 2014 02:50:30 -0700 (PDT) X-Received: by 10.220.182.73 with SMTP id cb9mr3310007vcb.9.1408701030021; Fri, 22 Aug 2014 02:50:30 -0700 (PDT) Received: from mail-vc0-f172.google.com (mail-vc0-f172.google.com [209.85.220.172]) by mx.google.com with ESMTPS id sr5si7250702vdc.86.2014.08.22.02.50.29 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Fri, 22 Aug 2014 02:50:29 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.172 as permitted sender) client-ip=209.85.220.172; Received: by mail-vc0-f172.google.com with SMTP id im17so12067551vcb.17 for ; Fri, 22 Aug 2014 02:50:29 -0700 (PDT) X-Received: by 10.220.114.5 with SMTP id c5mr150944vcq.28.1408701029899; Fri, 22 Aug 2014 02:50:29 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.221.45.67 with SMTP id uj3csp3425vcb; Fri, 22 Aug 2014 02:50:29 -0700 (PDT) X-Received: by 10.68.135.42 with SMTP id pp10mr5182839pbb.58.1408701029076; Fri, 22 Aug 2014 02:50:29 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 17si35654000pdg.8.2014.08.22.02.50.28 for ; Fri, 22 Aug 2014 02:50:29 -0700 (PDT) Received-SPF: none (google.com: linux-usb-owner@vger.kernel.org does not designate permitted sender hosts) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753229AbaHVJu0 (ORCPT + 2 others); Fri, 22 Aug 2014 05:50:26 -0400 Received: from mail-pd0-f174.google.com ([209.85.192.174]:34845 "EHLO mail-pd0-f174.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751846AbaHVJuZ (ORCPT ); Fri, 22 Aug 2014 05:50:25 -0400 Received: by mail-pd0-f174.google.com with SMTP id fp1so15557545pdb.5 for ; Fri, 22 Aug 2014 02:50:24 -0700 (PDT) X-Received: by 10.68.203.198 with SMTP id ks6mr5022914pbc.140.1408701024831; Fri, 22 Aug 2014 02:50:24 -0700 (PDT) Received: from c-krapar-linux.qualcomm.com ([202.46.23.54]) by mx.google.com with ESMTPSA id e2sm42777092pdk.54.2014.08.22.02.50.20 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Fri, 22 Aug 2014 02:50:24 -0700 (PDT) From: Kiran Kumar Raparthy To: linux-kernel@vger.kernel.org Cc: Todd Poynor , Felipe Balbi , Greg Kroah-Hartman , linux-usb@vger.kernel.org, Android Kernel Team , John Stultz , Kiran Raparthy Subject: [RFC 2/2] usb: otg: Temporarily hold wakeupsource on charger and disconnect events Date: Fri, 22 Aug 2014 15:20:00 +0530 Message-Id: <1408701000-8555-1-git-send-email-kiran.kumar@linaro.org> X-Mailer: git-send-email 1.8.2.1 Sender: linux-usb-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-usb@vger.kernel.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: kiran.kumar@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.220.172 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , From: Todd Poynor 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 Cc: Greg Kroah-Hartman Cc: linux-kernel@vger.kernel.org Cc: linux-usb@vger.kernel.org Cc: Android Kernel Team Cc: John Stultz Signed-off-by: Todd Poynor Signed-off-by: Kiran Raparthy [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); return 0; } @@ -150,19 +108,31 @@ MODULE_PARM_DESC(enabled, "enable wakelock when VBUS present"); static int __init otg_wakeupsource_init(void) { - unsigned long irqflags; + int ret; + struct usb_phy *phy; - otgws_nb.notifier_call = otgws_otg_notifications; + phy = usb_get_phy(USB_PHY_TYPE_USB2); - if (!init_for_xceiv()) { - spin_lock_irqsave(&otgws_spinlock, irqflags); + if (IS_ERR(phy)) { + pr_err("%s: No OTG transceiver found\n", __func__); + return PTR_ERR(phy); + } - if (enabled) - sync_with_xceiv_state(); + otgws_xceiv = phy; - spin_unlock_irqrestore(&otgws_spinlock, irqflags); - } else { - enabled = false; + snprintf(vbus_lock.name, sizeof(vbus_lock.name), "vbus-%s", + dev_name(otgws_xceiv->dev)); + wakeup_source_init(&vbus_lock.wsource, vbus_lock.name); + + otgws_nb.notifier_call = otgws_otg_notifications; + ret = usb_register_notifier(otgws_xceiv, &otgws_nb); + + if (ret) { + 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 ret; } return 0; 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 +#define TEMPORARY_HOLD_TIME 2000 + struct usb_otg { u8 default_a;