From patchwork Mon Oct 10 06:22:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "\(Exiting\) Baolin Wang" X-Patchwork-Id: 77425 Delivered-To: patch@linaro.org Received: by 10.140.97.247 with SMTP id m110csp1079076qge; Sun, 9 Oct 2016 23:23:30 -0700 (PDT) X-Received: by 10.66.222.230 with SMTP id qp6mr44094694pac.62.1476080610036; Sun, 09 Oct 2016 23:23:30 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id bu6si29740912pad.96.2016.10.09.23.23.29; Sun, 09 Oct 2016 23:23:30 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-pm-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@linaro.org; spf=pass (google.com: best guess record for domain of linux-pm-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-pm-owner@vger.kernel.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752139AbcJJGX0 (ORCPT + 14 others); Mon, 10 Oct 2016 02:23:26 -0400 Received: from mail-pa0-f46.google.com ([209.85.220.46]:34834 "EHLO mail-pa0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751967AbcJJGXY (ORCPT ); Mon, 10 Oct 2016 02:23:24 -0400 Received: by mail-pa0-f46.google.com with SMTP id qn10so45035812pac.2 for ; Sun, 09 Oct 2016 23:23:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :in-reply-to:references; bh=B2oP+34N6uSWh4Ril979ONGL2VCPeCX4zz7oSs5vKww=; b=G3TZhHSfUr2ZYX7im8j7ZWHGDIgtQkWE6spVptTzSmX3J1Gj4l73qF3qGvJjq7OFba aITNre9exLE5LAopkmC7OtuqpFyk3mTVmsvINqcb0Kzu3lKZZRaVadncldWh9E7MyiCC TsAqSERG7k/gL5IyXYDNhlKp2VeehRooyv970= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=B2oP+34N6uSWh4Ril979ONGL2VCPeCX4zz7oSs5vKww=; b=aWRGUd6e6a7tiXP/QgxPfF3gNskHoGQv2yLLLoKE4aUQqZbtZxQNAJ2BqHK5dkxZmo eR2RcrNmWVGvedLklougGFH9KWO7lY2x2SOOo9egafYCrRv2/P+YvPmBWV6bdvVO5T6g +jpGj9S3fzuc0z6O1DTbpN0bycIQx+HB0t3FcuMcsFU8yuv8a8NxM5+Jr+sBgdPv+1zb Vm+YP8I9V1hloVGsnnPQ6f3F0JZkB4uFkSs+D8ws2COf2Jg+XjVF0g6vhdHnovUG4dmc Szs84j9Qg65pKi+bIhuwfhNTLEmEtWloL3LcvO7zw4hI8z/nJO5oDTLAzYk/u77mtlEI 0nRw== X-Gm-Message-State: AA6/9RnGTPO6fibPDP2Q8z3jr6vjrx72KMFuG/uE/D4iCBUfwq4PuAq/W9quficvXYFMB/s2 X-Received: by 10.66.81.42 with SMTP id w10mr52203552pax.141.1476080604023; Sun, 09 Oct 2016 23:23:24 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([175.111.195.49]) by smtp.gmail.com with ESMTPSA id ad15sm51883988pac.33.2016.10.09.23.23.16 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 09 Oct 2016 23:23:23 -0700 (PDT) From: Baolin Wang To: balbi@kernel.org, gregkh@linuxfoundation.org, sre@kernel.org, dbaryshkov@gmail.com, dwmw2@infradead.org Cc: robh@kernel.org, jun.li@nxp.com, m.szyprowski@samsung.com, ruslan.bilovol@gmail.com, peter.chen@freescale.com, stern@rowland.harvard.edu, r.baldyga@samsung.com, grygorii.strashko@ti.com, yoshihiro.shimoda.uh@renesas.com, lee.jones@linaro.org, broonie@kernel.org, john.stultz@linaro.org, ckeepax@opensource.wolfsonmicro.com, patches@opensource.wolfsonmicro.com, baolin.wang@linaro.org, linux-pm@vger.kernel.org, linux-usb@vger.kernel.org, device-mainlining@lists.linuxfoundation.org, linux-kernel@vger.kernel.org Subject: [PATCH v17 3/4] usb: gadget: Integrate with the usb gadget supporting for usb charger Date: Mon, 10 Oct 2016 14:22:13 +0800 Message-Id: <700f8d4fc3c52418eeba5445f6822093cc9ff036.1476000169.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org When the usb gadget supporting for usb charger is ready, the usb charger can implement the usb_charger_plug_by_gadget() function, usb_charger_exit() function and dev_to_uchger() function by getting 'struct usb_charger' from 'struct gadget'. Signed-off-by: Baolin Wang Reviewed-by: Li Jun Tested-by: Li Jun --- drivers/usb/gadget/udc/charger.c | 97 +++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 2 deletions(-) -- 1.7.9.5 -- To unsubscribe from this list: send the line "unsubscribe linux-pm" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/usb/gadget/udc/charger.c b/drivers/usb/gadget/udc/charger.c index 16dc477..691cd75 100644 --- a/drivers/usb/gadget/udc/charger.c +++ b/drivers/usb/gadget/udc/charger.c @@ -38,7 +38,9 @@ static DEFINE_MUTEX(charger_lock); static struct usb_charger *dev_to_uchger(struct device *dev) { - return NULL; + struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev); + + return gadget->charger; } /* @@ -308,6 +310,18 @@ static int __usb_charger_set_cur_limit_by_type(struct usb_charger *uchger, int usb_charger_set_cur_limit_by_gadget(struct usb_gadget *gadget, unsigned int cur_limit) { + struct usb_charger *uchger = gadget->charger; + int ret; + + if (!uchger) + return -EINVAL; + + ret = __usb_charger_set_cur_limit_by_type(uchger, uchger->type, + cur_limit); + if (ret) + return ret; + + schedule_work(&uchger->work); return 0; } EXPORT_SYMBOL_GPL(usb_charger_set_cur_limit_by_gadget); @@ -625,11 +639,67 @@ static int usb_charger_plug_by_extcon(struct notifier_block *nb, int usb_charger_plug_by_gadget(struct usb_gadget *gadget, unsigned long state) { + struct usb_charger *uchger = gadget->charger; + enum usb_charger_state uchger_state; + + if (WARN(!uchger, "charger can not be NULL")) + return -EINVAL; + + /* + * Report event to power to setting the current limitation + * for this usb charger when one usb charger state is changed + * with detecting by usb gadget state. + */ + if (uchger->old_gadget_state != state) { + uchger->old_gadget_state = state; + + if (state >= USB_STATE_ATTACHED) { + uchger_state = USB_CHARGER_PRESENT; + } else if (state == USB_STATE_NOTATTACHED) { + mutex_lock(&uchger->lock); + + /* + * Need check the charger type to make sure the usb + * cable is removed, in case it just changes the usb + * function with configfs. + */ + uchger->type = __usb_charger_get_type(uchger); + if (uchger->type != UNKNOWN_TYPE) { + mutex_unlock(&uchger->lock); + return 0; + } + + mutex_unlock(&uchger->lock); + uchger_state = USB_CHARGER_REMOVE; + } else { + uchger_state = USB_CHARGER_DEFAULT; + } + + usb_charger_notify_state(uchger, uchger_state); + } + return 0; } EXPORT_SYMBOL_GPL(usb_charger_plug_by_gadget); /* + * usb_charger_unregister() - Unregister a usb charger. + * @uchger - the usb charger to be unregistered. + */ +static int usb_charger_unregister(struct usb_charger *uchger) +{ + ida_simple_remove(&usb_charger_ida, uchger->id); + sysfs_remove_groups(&uchger->gadget->dev.kobj, usb_charger_groups); + + mutex_lock(&charger_lock); + list_del(&uchger->list); + mutex_unlock(&charger_lock); + + kfree(uchger); + return 0; +} + +/* * usb_charger_register() - Register a new usb charger. * @uchger - the new usb charger instance. */ @@ -740,6 +810,7 @@ int usb_charger_init(struct usb_gadget *ugadget) } uchger->gadget = ugadget; + ugadget->charger = uchger; uchger->old_gadget_state = USB_STATE_NOTATTACHED; /* Register a new usb charger */ @@ -777,7 +848,29 @@ fail_extcon: int usb_charger_exit(struct usb_gadget *ugadget) { - return 0; + struct usb_charger *uchger = ugadget->charger; + + if (!uchger) + return -EINVAL; + + ugadget->charger = NULL; + extcon_unregister_notifier(uchger->extcon_dev, + EXTCON_USB, + &uchger->extcon_nb.nb); + extcon_unregister_notifier(uchger->extcon_dev, + EXTCON_CHG_USB_SDP, + &uchger->extcon_type_nb.nb); + extcon_unregister_notifier(uchger->extcon_dev, + EXTCON_CHG_USB_CDP, + &uchger->extcon_type_nb.nb); + extcon_unregister_notifier(uchger->extcon_dev, + EXTCON_CHG_USB_DCP, + &uchger->extcon_type_nb.nb); + extcon_unregister_notifier(uchger->extcon_dev, + EXTCON_CHG_USB_ACA, + &uchger->extcon_type_nb.nb); + + return usb_charger_unregister(uchger); } MODULE_AUTHOR("Baolin Wang ");