From patchwork Mon Feb 20 08:51:09 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "\(Exiting\) Baolin Wang" X-Patchwork-Id: 94182 Delivered-To: patch@linaro.org Received: by 10.182.3.34 with SMTP id 2csp1241734obz; Mon, 20 Feb 2017 00:52:22 -0800 (PST) X-Received: by 10.84.175.74 with SMTP id s68mr30146342plb.155.1487580742131; Mon, 20 Feb 2017 00:52:22 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id 78si5743860pgh.202.2017.02.20.00.52.21; Mon, 20 Feb 2017 00:52:22 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752420AbdBTIwI (ORCPT + 25 others); Mon, 20 Feb 2017 03:52:08 -0500 Received: from mail-pg0-f51.google.com ([74.125.83.51]:32916 "EHLO mail-pg0-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752406AbdBTIwF (ORCPT ); Mon, 20 Feb 2017 03:52:05 -0500 Received: by mail-pg0-f51.google.com with SMTP id a123so15083309pgc.0 for ; Mon, 20 Feb 2017 00:52:00 -0800 (PST) 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=CI04AiEty6ng8FZCizWI6wwcr3wXJIM0vooK9mE3DOM=; b=jw5PwsdEdEg1SpiHv0/uYPLteF11Wq5Je9X1BMUzKVx0oz9LRywbGW9woBeF9SlPZJ Hm+MXM8jRhdAjHNcwVEd04eTQChZuOSzINx9lmwu4tGTVrtrRwOzE8nzT/ugtJMgGJgx kjGt5yfiiSRNoGOFB5aYL8686XI2nxuizR9RU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:in-reply-to:references; bh=CI04AiEty6ng8FZCizWI6wwcr3wXJIM0vooK9mE3DOM=; b=USEuQEKTZsd4b/Di1krt/NGvVS1bHDyKpf/tFnWLZaq5Xc57mIA41kuCgnVuXO4uMU CbuiqKFmsiaW5Kj3QwhD5ycZXvaQUpXhklkwqwMQgHYWf4aFp5T/XlU5Q7ukHj6bmhEQ GicZvv08vpJyW/8rHES/K3byRPaaZpnJUrzn6oEytjjgzqsmuDzTQ0JGg2smQd7Ik4yq A9vtLWQvtxJij3WOZD7sO1fleFebaso12E4tug+Ptvpu1JJU0XkBzIqH1SApefY2Q84Q mpZ6WJm57LJclJ1bzVsaI80Wl8bNPV8TO4yx7dhxHgimIIG9xUxZViJKT3nKNkO9uW4g K5QQ== X-Gm-Message-State: AMke39k1HKM3O5kggf5wiU0zvzTPoc96I3IhRr1vxg+enCBRAMUVCYxfmKOlosjX00Kq21vR X-Received: by 10.84.140.36 with SMTP id 33mr29629517pls.136.1487580719742; Mon, 20 Feb 2017 00:51:59 -0800 (PST) Received: from baolinwangubtpc.spreadtrum.com ([117.18.48.82]) by smtp.gmail.com with ESMTPSA id t184sm5102967pgb.11.2017.02.20.00.51.52 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 20 Feb 2017 00:51:59 -0800 (PST) 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, grygorii.strashko@ti.com, yoshihiro.shimoda.uh@renesas.com, lee.jones@linaro.org, broonie@kernel.org, john.stultz@linaro.org, neilb@suse.com, 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 v19 3/4] usb: gadget: Integrate with the usb gadget supporting for usb charger Date: Mon, 20 Feb 2017 16:51:09 +0800 Message-Id: <084edac2d7732525d8dc437f6adbd7867377100c.1487578977.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 In-Reply-To: References: In-Reply-To: References: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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, 96 insertions(+), 1 deletion(-) -- 1.7.9.5 diff --git a/drivers/usb/gadget/udc/charger.c b/drivers/usb/gadget/udc/charger.c index 6d62f54..19eb044 100644 --- a/drivers/usb/gadget/udc/charger.c +++ b/drivers/usb/gadget/udc/charger.c @@ -38,7 +38,9 @@ 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; } /* @@ -398,6 +400,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); @@ -622,11 +636,66 @@ 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. + */ + if (uchger->type != UNKNOWN_TYPE) { + mutex_unlock(&uchger->lock); + return 0; + } + + mutex_unlock(&uchger->lock); + uchger_state = USB_CHARGER_ABSENT; + } 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); + uchger->id = -1; + + mutex_lock(&charger_lock); + list_del(&uchger->list); + mutex_unlock(&charger_lock); + + return 0; +} + +/* * usb_charger_register() - Register a new usb charger. * @uchger - the new usb charger instance. */ @@ -725,6 +794,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 */ @@ -762,6 +832,31 @@ int usb_charger_init(struct usb_gadget *ugadget) int usb_charger_exit(struct usb_gadget *ugadget) { + struct usb_charger *uchger = ugadget->charger; + + if (!uchger) + return -EINVAL; + + usb_charger_unregister(uchger); + 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); + + kfree(uchger); return 0; }