From patchwork Thu Jun 30 05:35:25 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: 71221 Delivered-To: patch@linaro.org Received: by 10.140.28.4 with SMTP id 4csp212354qgy; Wed, 29 Jun 2016 22:38:00 -0700 (PDT) X-Received: by 10.98.62.15 with SMTP id l15mr18313428pfa.24.1467265076579; Wed, 29 Jun 2016 22:37:56 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a8si2563713pfj.35.2016.06.29.22.37.56; Wed, 29 Jun 2016 22:37:56 -0700 (PDT) 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 dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752014AbcF3Fht (ORCPT + 30 others); Thu, 30 Jun 2016 01:37:49 -0400 Received: from mail-pa0-f51.google.com ([209.85.220.51]:33924 "EHLO mail-pa0-f51.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751565AbcF3Fgc (ORCPT ); Thu, 30 Jun 2016 01:36:32 -0400 Received: by mail-pa0-f51.google.com with SMTP id bz2so24737465pad.1 for ; Wed, 29 Jun 2016 22:36:27 -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=HyzAZmgXd5GClyX4C2ooOB/qEJIBPhyRtViysh///Kg=; b=I2UDkJGMrP8aGMnisTKgAB8Qx/RG/5irjKDEPjvrI7D05dj6gBQyftxNiCA5VMVDFp 2OwML0Dt2b43FxJcEGkJWxOdqyjpUn498JEwOv7zx6qFTO0/I0BPzfRT0YY6XxZ4Hs5O gOvfROd45Q16DKh848furZ6qogFCu1aBFSj7U= 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=HyzAZmgXd5GClyX4C2ooOB/qEJIBPhyRtViysh///Kg=; b=FnFFA8xWdTdSecshp6KqLcEQwTo/lXXh00TRoJ/h9M0FKMvhWmXsewLi/OWfzjv2H2 lNrqG8bC6yk1foPSZ0LSJxAsrRA2s93P7W+m6l/Uo+nfAdi7AWsUrSrIgVxYIi8Ribhw 4ce9cSFQg5WchgvoZhlzej6WwTDm1BLYEZcpYqD7uYCIh2n9mRbd0a1KUAz/cTrD4F04 hF62mftWjNVDEAa4jUknvLPaZ32JN7I2gKjXVUuvnM1zkgpLALBZQ6qXk0h+LjgffUBl zt7C/oj75Q8x4JmDYt2+vqC3W2TwC0LPgi3Q4J9R48pvhbNTjI/inaIxuU3nS4CUbd5a OstQ== X-Gm-Message-State: ALyK8tJ6qNWceZi2sgS4CFbudbIjt1lWbXe8BPHutKOiOLeSrFnoVsrdRkowL1jsM1NvZcHZ X-Received: by 10.66.168.13 with SMTP id zs13mr18294476pab.68.1467264986540; Wed, 29 Jun 2016 22:36:26 -0700 (PDT) Received: from baolinwangubtpc.spreadtrum.com ([175.111.195.49]) by smtp.gmail.com with ESMTPSA id zv2sm1954150pac.43.2016.06.29.22.36.20 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 29 Jun 2016 22:36:26 -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, 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 v14 3/4] usb: gadget: Integrate with the usb gadget supporting for usb charger Date: Thu, 30 Jun 2016 13:35:25 +0800 Message-Id: <6e336398acdf4c4013f4e4760fb9dae6dd72dc58.1467264233.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 | 70 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 3 deletions(-) -- 1.7.9.5 diff --git a/drivers/usb/gadget/udc/charger.c b/drivers/usb/gadget/udc/charger.c index 7abecf3..45c0bf1b 100644 --- a/drivers/usb/gadget/udc/charger.c +++ b/drivers/usb/gadget/udc/charger.c @@ -35,7 +35,9 @@ static unsigned int __usb_charger_get_cur_limit(struct usb_charger *uchger); 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,7 +310,13 @@ 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) { - return 0; + struct usb_charger *uchger = gadget->charger; + + if (!uchger) + return 0; + + return __usb_charger_set_cur_limit_by_type(uchger, uchger->type, + cur_limit); } EXPORT_SYMBOL_GPL(usb_charger_set_cur_limit_by_gadget); @@ -562,11 +570,55 @@ 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) + uchger_state = USB_CHARGER_REMOVE; + else + uchger_state = USB_CHARGER_DEFAULT; + + usb_charger_notify_others(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) +{ + if (WARN(!uchger, "charger can not be NULL")) + return -EINVAL; + + 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. */ @@ -649,6 +701,7 @@ int usb_charger_init(struct usb_gadget *ugadget) /* register a notifier on a usb gadget device */ uchger->gadget = ugadget; + ugadget->charger = uchger; uchger->old_gadget_state = USB_STATE_NOTATTACHED; /* register a new usb charger */ @@ -671,7 +724,18 @@ fail: int usb_charger_exit(struct usb_gadget *ugadget) { - return 0; + struct usb_charger *uchger = ugadget->charger; + + if (WARN(!uchger, "charger can not be NULL")) + return -EINVAL; + + ugadget->charger = NULL; + if (uchger->extcon_dev) + extcon_unregister_notifier(uchger->extcon_dev, + EXTCON_USB, + &uchger->extcon_nb.nb); + + return usb_charger_unregister(uchger); } MODULE_AUTHOR("Baolin Wang ");