From patchwork Mon Feb 20 08:51:07 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: 94180 Delivered-To: patch@linaro.org Received: by 10.182.3.34 with SMTP id 2csp1241629obz; Mon, 20 Feb 2017 00:51:57 -0800 (PST) X-Received: by 10.98.207.193 with SMTP id b184mr24930730pfg.12.1487580716970; Mon, 20 Feb 2017 00:51:56 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c16si11675965plk.123.2017.02.20.00.51.56; Mon, 20 Feb 2017 00:51:56 -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 S1752349AbdBTIvz (ORCPT + 25 others); Mon, 20 Feb 2017 03:51:55 -0500 Received: from mail-pg0-f47.google.com ([74.125.83.47]:32871 "EHLO mail-pg0-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752237AbdBTIvr (ORCPT ); Mon, 20 Feb 2017 03:51:47 -0500 Received: by mail-pg0-f47.google.com with SMTP id a123so15080886pgc.0 for ; Mon, 20 Feb 2017 00:51:46 -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=mPx5bqtEjVTggMlKdwFZWk8alP4ONYZMZ5jTt5nEPY8=; b=OKfK+Jcd/Yutf0TX8AHDUBq185noGB/RqWnR6p/rG20++NlaqmDUWP/RjM8Zranbpy ydRJklJ9syRhWNU2eR2YNtwqoIVQMW7UObEHskzSPqBggYifTA3RyOCcOqw8RThOW9rM qSAvwybdJitBRdmvZ8zgPfu3Fxodd7bWUqjcw= 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=mPx5bqtEjVTggMlKdwFZWk8alP4ONYZMZ5jTt5nEPY8=; b=LoOD4gNdJZ7DIZiPuNvyVinKjMYO63Cx+wcmiHPd2sg0020WNBRvT0FEFqu8zzde09 C3z14XG+pPJ7H97QAizM5RCgub4I005mBxEEVQHGhORltoVdyP3cahBlXpdXAl48uzQ+ GUd5V8q4MC8/HZeaI5oc8/W6SMiiCjr/rzTwtvFqNXIXsDYDNcJt4I4V488gt4yAaG4x e9ispQdT28YWCFfQAlvIMwI56v0thTaooRJse8IdKfqPDopwHbRNLUztrjXZztphKSj2 eTadnps6Q7nuEBJ3yxs1gTOgp4WO7HSlaBDI5/2eGX9wNMKQ7ud5WN7m61U9fHt7vo/v Ziig== X-Gm-Message-State: AMke39m3lgeFBGQmprhnodyd7mohyXrImayS6k5kGkfIgg8ZBnrCPYie0Ilammc3FqdGJJI6 X-Received: by 10.99.94.195 with SMTP id s186mr26425340pgb.198.1487580705878; Mon, 20 Feb 2017 00:51:45 -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.38 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 20 Feb 2017 00:51:45 -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 1/4] usb: gadget: Introduce the usb charger framework Date: Mon, 20 Feb 2017 16:51:07 +0800 Message-Id: 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 This patch introduces the usb charger driver based on usb gadget that makes an enhancement to a power driver. It works well in practice but that requires a system with suitable hardware. The basic conception of the usb charger is that, when one usb charger is added or removed by reporting from the usb gadget state change or the extcon device state change, the usb charger will report to power user to set the current limitation. The usb charger will register notifiees on the usb gadget or the extcon device to get notified the usb charger state. It also supplies the notification mechanism to userspace When the usb charger state is changed. Power user will register a notifiee on the usb charger to get notified by status changes from the usb charger. It will report to power user to set the current limitation when detecting the usb charger is added or removed from extcon device state or usb gadget state. This patch doesn't yet integrate with the gadget code, so some functions which rely on the 'gadget' are not completed, that will be implemented in the following patches. Signed-off-by: Baolin Wang Reviewed-by: Li Jun Tested-by: Li Jun --- drivers/usb/gadget/Kconfig | 8 + drivers/usb/gadget/udc/Makefile | 1 + drivers/usb/gadget/udc/charger.c | 770 ++++++++++++++++++++++++++++++++++++++ include/linux/usb/charger.h | 176 +++++++++ include/uapi/linux/usb/charger.h | 31 ++ 5 files changed, 986 insertions(+) create mode 100644 drivers/usb/gadget/udc/charger.c create mode 100644 include/linux/usb/charger.h create mode 100644 include/uapi/linux/usb/charger.h -- 1.7.9.5 diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig index 8ad2032..60d2f29 100644 --- a/drivers/usb/gadget/Kconfig +++ b/drivers/usb/gadget/Kconfig @@ -134,6 +134,14 @@ config U_SERIAL_CONSOLE help It supports the serial gadget can be used as a console. +config USB_CHARGER + bool "USB charger support" + select EXTCON + help + The usb charger driver based on the usb gadget that makes an + enhancement to a power driver which can set the current limitation + when the usb charger is added or removed. + source "drivers/usb/gadget/udc/Kconfig" # diff --git a/drivers/usb/gadget/udc/Makefile b/drivers/usb/gadget/udc/Makefile index 98e74ed..ede2351 100644 --- a/drivers/usb/gadget/udc/Makefile +++ b/drivers/usb/gadget/udc/Makefile @@ -2,6 +2,7 @@ CFLAGS_trace.o := -I$(src) udc-core-y := core.o trace.o +udc-core-$(CONFIG_USB_CHARGER) += charger.o # # USB peripheral controller drivers diff --git a/drivers/usb/gadget/udc/charger.c b/drivers/usb/gadget/udc/charger.c new file mode 100644 index 0000000..6d62f54 --- /dev/null +++ b/drivers/usb/gadget/udc/charger.c @@ -0,0 +1,770 @@ +/* + * charger.c -- USB charger driver + * + * Copyright (C) 2016 Linaro Ltd. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Default current range by charger type. */ +#define DEFAULT_SDP_CUR_MIN 2 +#define DEFAULT_SDP_CUR_MAX 500 +#define DEFAULT_SDP_CUR_MIN_SS 150 +#define DEFAULT_SDP_CUR_MAX_SS 900 +#define DEFAULT_DCP_CUR_MIN 500 +#define DEFAULT_DCP_CUR_MAX 5000 +#define DEFAULT_CDP_CUR_MIN 1500 +#define DEFAULT_CDP_CUR_MAX 5000 +#define DEFAULT_ACA_CUR_MIN 1500 +#define DEFAULT_ACA_CUR_MAX 5000 + +static DEFINE_IDA(usb_charger_ida); +static LIST_HEAD(charger_list); +static DEFINE_MUTEX(charger_lock); + +static struct usb_charger *dev_to_uchger(struct device *dev) +{ + return NULL; +} + +/* + * charger_current_show() - Show the charger current. + */ +static ssize_t charger_current_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct usb_charger *uchger = dev_to_uchger(dev); + unsigned int min, max; + + usb_charger_get_current(uchger, &min, &max); + return sprintf(buf, "%u-%u\n", min, max); +} +static DEVICE_ATTR_RO(charger_current); + +/* + * charger_type_show() - Show the charger type. + * + * It can be SDP/DCP/CDP/ACA type, else for unknown type. + */ +static ssize_t charger_type_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct usb_charger *uchger = dev_to_uchger(dev); + enum usb_charger_type type = usb_charger_get_type(uchger); + int cnt; + + switch (type) { + case SDP_TYPE: + cnt = sprintf(buf, "%s\n", "SDP"); + break; + case DCP_TYPE: + cnt = sprintf(buf, "%s\n", "DCP"); + break; + case CDP_TYPE: + cnt = sprintf(buf, "%s\n", "CDP"); + break; + case ACA_TYPE: + cnt = sprintf(buf, "%s\n", "ACA"); + break; + default: + cnt = sprintf(buf, "%s\n", "UNKNOWN"); + break; + } + + return cnt; +} +static DEVICE_ATTR_RO(charger_type); + +/* + * charger_state_show() - Show the charger state. + * + * Charger state can be present or removed. + */ +static ssize_t charger_state_show(struct device *dev, + struct device_attribute *attr, + char *buf) +{ + struct usb_charger *uchger = dev_to_uchger(dev); + enum usb_charger_state state = usb_charger_get_state(uchger); + int cnt; + + switch (state) { + case USB_CHARGER_PRESENT: + cnt = sprintf(buf, "%s\n", "PRESENT"); + break; + case USB_CHARGER_ABSENT: + cnt = sprintf(buf, "%s\n", "REMOVE"); + break; + default: + cnt = sprintf(buf, "%s\n", "UNKNOWN"); + break; + } + + return cnt; +} +static DEVICE_ATTR_RO(charger_state); + +static struct attribute *usb_charger_attrs[] = { + &dev_attr_charger_current.attr, + &dev_attr_charger_type.attr, + &dev_attr_charger_state.attr, + NULL +}; + +static const struct attribute_group usb_charger_group = { + .name = "charger", + .attrs = usb_charger_attrs, +}; +__ATTRIBUTE_GROUPS(usb_charger); + +/* + * usb_charger_get_instance() - Get the first usb charger instance. + * + * Note: We assume that there is only one USB charger in the system. + */ +struct usb_charger *usb_charger_get_instance() +{ + struct usb_charger *uchger; + + mutex_lock(&charger_lock); + list_for_each_entry(uchger, &charger_list, list) { + if (uchger->name) + break; + } + mutex_unlock(&charger_lock); + + if (WARN(!uchger, "can't find usb charger")) + return ERR_PTR(-ENODEV); + + return uchger; +} +EXPORT_SYMBOL_GPL(usb_charger_get_instance); + +/* + * usb_charger_get_type() - get the usb charger type with lock protection. + * @uchger - usb charger instance. + * + * Users can get the charger type by this safe API, rather than using the + * usb_charger structure directly. + */ +enum usb_charger_type usb_charger_get_type(struct usb_charger *uchger) +{ + enum usb_charger_type type; + + mutex_lock(&uchger->lock); + type = uchger->type; + mutex_unlock(&uchger->lock); + + return type; +} +EXPORT_SYMBOL_GPL(usb_charger_get_type); + +/* + * usb_charger_get_state() - Get the charger state with lock protection. + * @uchger - the usb charger instance. + * + * Users should get the charger state by this safe API. + */ +enum usb_charger_state usb_charger_get_state(struct usb_charger *uchger) +{ + enum usb_charger_state state; + + mutex_lock(&uchger->lock); + state = uchger->state; + mutex_unlock(&uchger->lock); + + return state; +} +EXPORT_SYMBOL_GPL(usb_charger_get_state); + +/* + * usb_charger_detect_type() - detect the charger type manually. + * @uchger - usb charger instance. + * + * Note: You should ensure you need to detect the charger type manually on + * your platform. You should call it at the right gadget state to avoid + * affecting gadget enumeration. + */ +int usb_charger_detect_type(struct usb_charger *uchger) +{ + enum usb_charger_type type; + + if (!uchger->charger_detect) + return -EINVAL; + + type = uchger->charger_detect(uchger); + + mutex_lock(&uchger->lock); + uchger->type = type; + mutex_unlock(&uchger->lock); + + return 0; +} +EXPORT_SYMBOL_GPL(usb_charger_detect_type); + +/* + * usb_charger_set_default_current() - Set default current for charger. + * @uchger - usb charger instance. + */ +static void usb_charger_set_default_current(struct usb_charger *uchger) +{ + uchger->cur.sdp_min = DEFAULT_SDP_CUR_MIN; + uchger->cur.sdp_max = DEFAULT_SDP_CUR_MAX; + uchger->cur.dcp_min = DEFAULT_DCP_CUR_MIN; + uchger->cur.dcp_max = DEFAULT_DCP_CUR_MAX; + uchger->cur.cdp_min = DEFAULT_CDP_CUR_MIN; + uchger->cur.cdp_max = DEFAULT_CDP_CUR_MAX; + uchger->cur.aca_min = DEFAULT_ACA_CUR_MIN; + uchger->cur.aca_max = DEFAULT_ACA_CUR_MAX; + uchger->sdp_default_cur_change = 0; +} + +/* + * __usb_charger_get_current() - Get the charger current. + * @uchger - the usb charger instance. + * @min - return the minimum current. + * @max - return the maximum current. + * + * Callers should get the charger lock before issuing this function. + */ +static void __usb_charger_get_current(struct usb_charger *uchger, + unsigned int *min, + unsigned int *max) +{ + enum usb_charger_type type = uchger->type; + + switch (type) { + case SDP_TYPE: + /* + * For super speed gadget, the default charger maximum current + * should be 900 mA and the default minimum current should be + * 150mA. + */ + if (uchger->gadget && + uchger->gadget->speed >= USB_SPEED_SUPER) { + if (!uchger->sdp_default_cur_change) + uchger->cur.sdp_max = DEFAULT_SDP_CUR_MAX_SS; + + uchger->cur.sdp_min = DEFAULT_SDP_CUR_MIN_SS; + } + + *min = uchger->cur.sdp_min; + *max = uchger->cur.sdp_max; + break; + case DCP_TYPE: + *min = uchger->cur.dcp_min; + *max = uchger->cur.dcp_max; + break; + case CDP_TYPE: + *min = uchger->cur.cdp_min; + *max = uchger->cur.cdp_max; + break; + case ACA_TYPE: + *min = uchger->cur.aca_min; + *max = uchger->cur.aca_max; + break; + default: + *min = 0; + *max = 0; + break; + } +} + +/* + * usb_charger_get_cur_limit() - Get the maximum charger current. + * @uchger - the usb charger instance. + * + * This function should under the charger lock protection. + */ +static unsigned int usb_charger_get_cur_limit(struct usb_charger *uchger) +{ + unsigned int min_cur, max_cur; + + __usb_charger_get_current(uchger, &min_cur, &max_cur); + return max_cur; +} + +/* + * usb_charger_get_current() - Get the charger current with lock protection. + * @uchger - the usb charger instance. + * @min - return the minimum current. + * @max - return the maximum current. + * + * Users should get the charger current by this safe API. + */ +int usb_charger_get_current(struct usb_charger *uchger, + unsigned int *min, + unsigned int *max) +{ + if (!uchger) + return -EINVAL; + + mutex_lock(&uchger->lock); + __usb_charger_get_current(uchger, min, max); + mutex_unlock(&uchger->lock); + + return 0; +} +EXPORT_SYMBOL_GPL(usb_charger_get_current); + +/* + * usb_charger_notify_work() - Notify users the current has changed by work. + * @work - the work instance. + * + * Note: When users receive the charger present event, they should check the + * charger current by usb_charger_get_current() API. + */ +static void usb_charger_notify_work(struct work_struct *work) +{ + struct usb_charger *uchger = work_to_charger(work); + + mutex_lock(&uchger->lock); + if (uchger->state == USB_CHARGER_PRESENT) { + raw_notifier_call_chain(&uchger->uchger_nh, + usb_charger_get_cur_limit(uchger), + uchger); + } + mutex_unlock(&uchger->lock); +} + +/* + * __usb_charger_set_cur_limit_by_type() - Set the current limitation + * by charger type. + * @uchger - the usb charger instance. + * @type - the usb charger type. + * @cur_limit - the current limitation. + */ +static int __usb_charger_set_cur_limit_by_type(struct usb_charger *uchger, + enum usb_charger_type type, + unsigned int cur_limit) +{ + switch (type) { + case SDP_TYPE: + if (uchger->gadget && uchger->gadget->speed >= USB_SPEED_SUPER) { + uchger->cur.sdp_max = + (cur_limit > DEFAULT_SDP_CUR_MAX_SS) ? + DEFAULT_SDP_CUR_MAX_SS : cur_limit; + } else { + uchger->cur.sdp_max = + (cur_limit > DEFAULT_SDP_CUR_MAX) ? + DEFAULT_SDP_CUR_MAX : cur_limit; + } + + uchger->sdp_default_cur_change = 1; + break; + case DCP_TYPE: + uchger->cur.dcp_max = (cur_limit > DEFAULT_DCP_CUR_MAX) ? + DEFAULT_DCP_CUR_MAX : cur_limit; + break; + case CDP_TYPE: + uchger->cur.cdp_max = (cur_limit > DEFAULT_CDP_CUR_MAX) ? + DEFAULT_CDP_CUR_MAX : cur_limit; + break; + case ACA_TYPE: + uchger->cur.aca_max = (cur_limit > DEFAULT_ACA_CUR_MAX) ? + DEFAULT_ACA_CUR_MAX : cur_limit; + break; + default: + return -EINVAL; + } + + return 0; +} + +/* + * usb_charger_set_cur_limit_by_gadget() - Set the current limitation from + * gadget layer. + * @gadget - the usb gadget device. + * @cur_limit - the current limitation. + * + * Note: This function is used in atomic contexts without mutex lock. + */ +int usb_charger_set_cur_limit_by_gadget(struct usb_gadget *gadget, + unsigned int cur_limit) +{ + return 0; +} +EXPORT_SYMBOL_GPL(usb_charger_set_cur_limit_by_gadget); + +/* + * usb_charger_set_cur_limit_by_type() - Set the current limitation + * by charger type with lock protection. + * @uchger - the usb charger instance. + * @type - the usb charger type. + * @cur_limit - the current limitation. + * + * Users should set the current limitation by this lock protection API. + */ +int usb_charger_set_cur_limit_by_type(struct usb_charger *uchger, + enum usb_charger_type type, + unsigned int cur_limit) +{ + int ret; + + if (!uchger) + return -EINVAL; + + mutex_lock(&uchger->lock); + ret = __usb_charger_set_cur_limit_by_type(uchger, type, cur_limit); + mutex_unlock(&uchger->lock); + if (ret) + return ret; + + schedule_work(&uchger->work); + return ret; +} +EXPORT_SYMBOL_GPL(usb_charger_set_cur_limit_by_type); + +/* + * usb_charger_register_notify() - Register a notifiee to get notified by + * any attach status changes from the usb charger detection. + * @uchger - the usb charger instance which is monitored. + * @nb - a notifier block to be registered. + */ +int usb_charger_register_notify(struct usb_charger *uchger, + struct notifier_block *nb) +{ + int ret; + + if (!uchger || !nb) { + pr_err("Charger or nb can not be NULL.\n"); + return -EINVAL; + } + + mutex_lock(&uchger->lock); + ret = raw_notifier_chain_register(&uchger->uchger_nh, nb); + mutex_unlock(&uchger->lock); + + return ret; +} +EXPORT_SYMBOL_GPL(usb_charger_register_notify); + +/* + * usb_charger_unregister_notify() - Unregister a notifiee from the usb charger. + * @uchger - the usb charger instance which is monitored. + * @nb - a notifier block to be unregistered. + */ +int usb_charger_unregister_notify(struct usb_charger *uchger, + struct notifier_block *nb) +{ + int ret; + + if (!uchger || !nb) { + pr_err("Charger or nb can not be NULL.\n"); + return -EINVAL; + } + + mutex_lock(&uchger->lock); + ret = raw_notifier_chain_unregister(&uchger->uchger_nh, nb); + mutex_unlock(&uchger->lock); + + return ret; +} +EXPORT_SYMBOL_GPL(usb_charger_unregister_notify); + +/* + * usb_charger_notify_state() - It will notify other device registered + * on usb charger when the usb charger state is changed. + * @uchger - the usb charger instance. + * @state - the state of the usb charger. + * + * Note: When notify the charger present state to power driver, the power driver + * should get the current by usb_charger_get_current() API to set current. + */ +static void usb_charger_notify_state(struct usb_charger *uchger, + enum usb_charger_state state) +{ + char uchger_state[50] = { 0 }; + char *envp[] = { uchger_state, NULL }; + + mutex_lock(&uchger->lock); + if (uchger->state == state) { + mutex_unlock(&uchger->lock); + return; + } + + uchger->state = state; + + switch (state) { + case USB_CHARGER_PRESENT: + raw_notifier_call_chain(&uchger->uchger_nh, + usb_charger_get_cur_limit(uchger), + uchger); + snprintf(uchger_state, ARRAY_SIZE(uchger_state), + "USB_CHARGER_STATE=%s", "USB_CHARGER_PRESENT"); + break; + case USB_CHARGER_ABSENT: + uchger->type = UNKNOWN_TYPE; + usb_charger_set_default_current(uchger); + raw_notifier_call_chain(&uchger->uchger_nh, 0, uchger); + snprintf(uchger_state, ARRAY_SIZE(uchger_state), + "USB_CHARGER_STATE=%s", "USB_CHARGER_ABSENT"); + break; + default: + pr_warn("Unknown USB charger state: %d\n", state); + mutex_unlock(&uchger->lock); + return; + } + + kobject_uevent_env(&uchger->gadget->dev.kobj, KOBJ_CHANGE, envp); + mutex_unlock(&uchger->lock); +} + +/* + * usb_charger_type_by_extcon() - The notifier call function which is registered + * on the extcon device. + * @nb - the notifier block that notified by extcon device. + * @state - the extcon device state. + * @data - here specify a extcon device. + * + * return the notify flag. + */ +static int usb_charger_type_by_extcon(struct notifier_block *nb, + unsigned long state, void *data) +{ + struct usb_charger_nb *extcon_nb = + container_of(nb, struct usb_charger_nb, nb); + struct usb_charger *uchger = extcon_nb->uchger; + enum usb_charger_state uchger_state; + enum usb_charger_type type; + + if (WARN(!uchger, "charger can not be NULL")) + return NOTIFY_BAD; + + /* Determine charger type */ + if (extcon_get_cable_state_(uchger->extcon_dev, + EXTCON_CHG_USB_SDP) > 0) { + type = SDP_TYPE; + uchger_state = USB_CHARGER_PRESENT; + } else if (extcon_get_cable_state_(uchger->extcon_dev, + EXTCON_CHG_USB_CDP) > 0) { + type = CDP_TYPE; + uchger_state = USB_CHARGER_PRESENT; + } else if (extcon_get_cable_state_(uchger->extcon_dev, + EXTCON_CHG_USB_DCP) > 0) { + type = DCP_TYPE; + uchger_state = USB_CHARGER_PRESENT; + } else if (extcon_get_cable_state_(uchger->extcon_dev, + EXTCON_CHG_USB_ACA) > 0) { + type = ACA_TYPE; + uchger_state = USB_CHARGER_PRESENT; + } else { + type = UNKNOWN_TYPE; + uchger_state = USB_CHARGER_ABSENT; + } + + mutex_lock(&uchger->lock); + uchger->type = type; + mutex_unlock(&uchger->lock); + + usb_charger_notify_state(uchger, uchger_state); + + return NOTIFY_OK; +} + +/* + * usb_charger_plug_by_extcon() - The notifier call function which is registered + * on the extcon device. + * @nb - the notifier block that notified by extcon device. + * @state - the extcon device state. + * @data - here specify a extcon device. + * + * return the notify flag. + */ +static int usb_charger_plug_by_extcon(struct notifier_block *nb, + unsigned long state, void *data) +{ + struct usb_charger_nb *extcon_nb = + container_of(nb, struct usb_charger_nb, nb); + struct usb_charger *uchger = extcon_nb->uchger; + enum usb_charger_state uchger_state; + + if (WARN(!uchger, "charger can not be NULL")) + return NOTIFY_BAD; + + /* + * Report event to power users to setting the current limitation + * for this usb charger when one usb charger is added or removed + * with detecting by extcon device. + */ + if (state) + uchger_state = USB_CHARGER_PRESENT; + else + uchger_state = USB_CHARGER_ABSENT; + + usb_charger_notify_state(uchger, uchger_state); + + return NOTIFY_OK; +} + +/* + * usb_charger_plug_by_gadget() - Set the usb charger current limitation + * according to the usb gadget device state. + * @gadget - the usb gadget device. + * @state - the usb gadget state. + */ +int usb_charger_plug_by_gadget(struct usb_gadget *gadget, + unsigned long state) +{ + return 0; +} +EXPORT_SYMBOL_GPL(usb_charger_plug_by_gadget); + +/* + * usb_charger_register() - Register a new usb charger. + * @uchger - the new usb charger instance. + */ +static int usb_charger_register(struct usb_charger *uchger) +{ + int ret; + + ret = ida_simple_get(&usb_charger_ida, 0, 0, GFP_KERNEL); + if (ret < 0) { + pr_err("Failed to register usb charger: %d\n", ret); + return ret; + } + + uchger->id = ret; + scnprintf(uchger->name, CHARGER_NAME_MAX, "usb-charger.%d", uchger->id); + + ret = sysfs_create_groups(&uchger->gadget->dev.kobj, + usb_charger_groups); + if (ret) { + pr_err("Failed to create usb charger attributes: %d\n", ret); + ida_simple_remove(&usb_charger_ida, uchger->id); + uchger->id = -1; + return ret; + } + + mutex_lock(&charger_lock); + list_add_tail(&uchger->list, &charger_list); + mutex_unlock(&charger_lock); + + return 0; +} + +int usb_charger_init(struct usb_gadget *ugadget) +{ + struct usb_charger *uchger; + struct extcon_dev *edev; + int ret; + + uchger = kzalloc(sizeof(struct usb_charger), GFP_KERNEL); + if (!uchger) + return -ENOMEM; + + uchger->type = UNKNOWN_TYPE; + uchger->state = USB_CHARGER_DEFAULT; + uchger->id = -1; + usb_charger_set_default_current(uchger); + + mutex_init(&uchger->lock); + RAW_INIT_NOTIFIER_HEAD(&uchger->uchger_nh); + INIT_WORK(&uchger->work, usb_charger_notify_work); + + /* Register a notifier on a extcon device if it is exsited */ + edev = extcon_get_edev_by_phandle(ugadget->dev.parent, 0); + if (!IS_ERR_OR_NULL(edev)) { + uchger->extcon_dev = edev; + uchger->extcon_nb.nb.notifier_call = usb_charger_plug_by_extcon; + uchger->extcon_nb.uchger = uchger; + ret = extcon_register_notifier(edev, EXTCON_USB, + &uchger->extcon_nb.nb); + if (ret) { + pr_err("Failed to register extcon USB notifier.\n"); + goto fail_extcon; + } + + uchger->extcon_type_nb.nb.notifier_call = + usb_charger_type_by_extcon; + uchger->extcon_type_nb.uchger = uchger; + + ret = extcon_register_notifier(edev, EXTCON_CHG_USB_SDP, + &uchger->extcon_type_nb.nb); + if (ret) { + pr_err("Failed to register extcon USB SDP notifier.\n"); + goto fail_extcon_sdp; + } + + ret = extcon_register_notifier(edev, EXTCON_CHG_USB_CDP, + &uchger->extcon_type_nb.nb); + if (ret) { + pr_err("Failed to register extcon USB CDP notifier.\n"); + goto fail_extcon_cdp; + } + + ret = extcon_register_notifier(edev, EXTCON_CHG_USB_DCP, + &uchger->extcon_type_nb.nb); + if (ret) { + pr_err("Failed to register extcon USB DCP notifier.\n"); + goto fail_extcon_dcp; + } + + ret = extcon_register_notifier(edev, EXTCON_CHG_USB_ACA, + &uchger->extcon_type_nb.nb); + if (ret) { + pr_err("Failed to register extcon USB ACA notifier.\n"); + goto fail_extcon_aca; + } + } + + uchger->gadget = ugadget; + uchger->old_gadget_state = USB_STATE_NOTATTACHED; + + /* Register a new usb charger */ + ret = usb_charger_register(uchger); + if (ret) + goto fail_register; + + return 0; + +fail_register: + extcon_unregister_notifier(uchger->extcon_dev, + EXTCON_CHG_USB_ACA, + &uchger->extcon_type_nb.nb); +fail_extcon_aca: + extcon_unregister_notifier(uchger->extcon_dev, + EXTCON_CHG_USB_DCP, + &uchger->extcon_type_nb.nb); +fail_extcon_dcp: + extcon_unregister_notifier(uchger->extcon_dev, + EXTCON_CHG_USB_CDP, + &uchger->extcon_type_nb.nb); +fail_extcon_cdp: + extcon_unregister_notifier(uchger->extcon_dev, + EXTCON_CHG_USB_SDP, + &uchger->extcon_type_nb.nb); +fail_extcon_sdp: + extcon_unregister_notifier(uchger->extcon_dev, + EXTCON_USB, + &uchger->extcon_nb.nb); +fail_extcon: + kfree(uchger); + + return ret; +} + +int usb_charger_exit(struct usb_gadget *ugadget) +{ + return 0; +} + +MODULE_AUTHOR("Baolin Wang "); +MODULE_DESCRIPTION("USB charger driver"); +MODULE_LICENSE("GPL v2"); diff --git a/include/linux/usb/charger.h b/include/linux/usb/charger.h new file mode 100644 index 0000000..e9b1c8a --- /dev/null +++ b/include/linux/usb/charger.h @@ -0,0 +1,176 @@ +#ifndef __LINUX_USB_CHARGER_H__ +#define __LINUX_USB_CHARGER_H__ + +#include +#include + +#define CHARGER_NAME_MAX 30 +#define work_to_charger(w) (container_of((w), struct usb_charger, work)) + +/* Current range by charger type */ +struct usb_charger_current { + unsigned int sdp_min; + unsigned int sdp_max; + unsigned int dcp_min; + unsigned int dcp_max; + unsigned int cdp_min; + unsigned int cdp_max; + unsigned int aca_min; + unsigned int aca_max; +}; + +struct usb_charger_nb { + struct notifier_block nb; + struct usb_charger *uchger; +}; + +/** + * struct usb_charger - describe one usb charger + * @name: Charger name. + * @list: For linking usb charger instances into one global list. + * @uchger_nh: Notifier head for users to register. + * @lock: Protect the notifier head and charger. + * @id: The charger id. + * @type: The charger type, it can be SDP_TYPE, DCP_TYPE, CDP_TYPE or + * ACA_TYPE. + * @state: Indicate the charger state. + * @cur: Describe the current range by charger type. + * @work: Workqueue to be used for reporting users current has changed. + * @extcon_dev: For supporting extcon usb gpio device to detect usb cable + * event (plugin or not). + * @extcon_nb: Report the events to charger from extcon device. + * @extcon_type_nb: Report the charger type from extcon device. + * @gadget: One usb charger is always tied to one usb gadget. + * @old_gadget_state: Record the previous state of one usb gadget to check if + * the gadget state is changed. If gadget state is changed, then notify the + * event to charger. + * @sdp_default_cur_change: Check if it is needed to change the SDP charger + * default maximum current. + * @charger_detect: User can detect the charger type by software or hardware, + * then the charger detection method can be implemented if you need to detect + * the charger type by software. + * + * Users can set and 'charger_detect' callbacks directly according to their + * own requirements. Beyond that, users can not touch anything else directly + * in this structure. + */ +struct usb_charger { + char name[CHARGER_NAME_MAX]; + struct list_head list; + struct raw_notifier_head uchger_nh; + struct mutex lock; + int id; + enum usb_charger_type type; + enum usb_charger_state state; + struct usb_charger_current cur; + struct work_struct work; + + struct extcon_dev *extcon_dev; + struct usb_charger_nb extcon_nb; + struct usb_charger_nb extcon_type_nb; + + struct usb_gadget *gadget; + enum usb_device_state old_gadget_state; + unsigned int sdp_default_cur_change; + + enum usb_charger_type (*charger_detect)(struct usb_charger *); +}; + +#ifdef CONFIG_USB_CHARGER +struct usb_charger *usb_charger_get_instance(void); + +int usb_charger_register_notify(struct usb_charger *uchger, + struct notifier_block *nb); +int usb_charger_unregister_notify(struct usb_charger *uchger, + struct notifier_block *nb); + +int usb_charger_get_current(struct usb_charger *uchger, + unsigned int *min, unsigned int *max); + +int usb_charger_set_cur_limit_by_type(struct usb_charger *uchger, + enum usb_charger_type type, + unsigned int cur_limit); +int usb_charger_set_cur_limit_by_gadget(struct usb_gadget *gadget, + unsigned int cur_limit); + +int usb_charger_plug_by_gadget(struct usb_gadget *gadget, unsigned long state); +enum usb_charger_type usb_charger_get_type(struct usb_charger *uchger); +int usb_charger_detect_type(struct usb_charger *uchger); +enum usb_charger_state usb_charger_get_state(struct usb_charger *uchger); + +int usb_charger_init(struct usb_gadget *ugadget); +int usb_charger_exit(struct usb_gadget *ugadget); +#else +static inline struct usb_charger *usb_charger_get_instance(void) +{ + return ERR_PTR(-ENODEV); +} + +static inline int usb_charger_register_notify(struct usb_charger *uchger, + struct notifier_block *nb) +{ + return 0; +} + +static inline int usb_charger_unregister_notify(struct usb_charger *uchger, + struct notifier_block *nb) +{ + return 0; +} + +static inline int usb_charger_get_current(struct usb_charger *uchger, + unsigned int *min, unsigned int *max) +{ + return 0; +} + +static inline int +usb_charger_set_cur_limit_by_type(struct usb_charger *uchger, + enum usb_charger_type type, + unsigned int cur_limit) +{ + return 0; +} + +static inline int +usb_charger_set_cur_limit_by_gadget(struct usb_gadget *gadget, + unsigned int cur_limit) +{ + return 0; +} + +static inline enum usb_charger_type +usb_charger_get_type(struct usb_charger *uchger) +{ + return UNKNOWN_TYPE; +} + +static inline enum usb_charger_state +usb_charger_get_state(struct usb_charger *uchger) +{ + return USB_CHARGER_REMOVE; +} + +static inline int usb_charger_detect_type(struct usb_charger *uchger) +{ + return 0; +} + +static inline int usb_charger_plug_by_gadget(struct usb_gadget *gadget, + unsigned long state) +{ + return 0; +} + +static inline int usb_charger_init(struct usb_gadget *ugadget) +{ + return 0; +} + +static inline int usb_charger_exit(struct usb_gadget *ugadget) +{ + return 0; +} +#endif + +#endif /* __LINUX_USB_CHARGER_H__ */ diff --git a/include/uapi/linux/usb/charger.h b/include/uapi/linux/usb/charger.h new file mode 100644 index 0000000..5f72af3 --- /dev/null +++ b/include/uapi/linux/usb/charger.h @@ -0,0 +1,31 @@ +/* + * This file defines the USB charger type and state that are needed for + * USB device APIs. + */ + +#ifndef _UAPI__LINUX_USB_CHARGER_H +#define _UAPI__LINUX_USB_CHARGER_H + +/* + * USB charger type: + * SDP (Standard Downstream Port) + * DCP (Dedicated Charging Port) + * CDP (Charging Downstream Port) + * ACA (Accessory Charger Adapters) + */ +enum usb_charger_type { + UNKNOWN_TYPE, + SDP_TYPE, + DCP_TYPE, + CDP_TYPE, + ACA_TYPE, +}; + +/* USB charger state */ +enum usb_charger_state { + USB_CHARGER_DEFAULT, + USB_CHARGER_PRESENT, + USB_CHARGER_ABSENT, +}; + +#endif /* _UAPI__LINUX_USB_CHARGER_H */ From patchwork Mon Feb 20 08:51:08 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: 94181 Delivered-To: patch@linaro.org Received: by 10.182.3.34 with SMTP id 2csp1241731obz; Mon, 20 Feb 2017 00:52:21 -0800 (PST) X-Received: by 10.84.179.165 with SMTP id b34mr30330328plc.147.1487580741787; Mon, 20 Feb 2017 00:52:21 -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:21 -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 S1752405AbdBTIwE (ORCPT + 25 others); Mon, 20 Feb 2017 03:52:04 -0500 Received: from mail-pg0-f43.google.com ([74.125.83.43]:34002 "EHLO mail-pg0-f43.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752009AbdBTIv6 (ORCPT ); Mon, 20 Feb 2017 03:51:58 -0500 Received: by mail-pg0-f43.google.com with SMTP id z67so38031877pgb.1 for ; Mon, 20 Feb 2017 00:51:58 -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=s0vnPz+7bAK7O8lTGIDYy/yK3bIMSOWOoAvziPY8esM=; b=enr6WvoS5lseP+Bb58m/7fXaZH3En3XIkczFO0neUaJl/kWIIcee905hvhXb2jv/ZY frnPPxPeqNzUZIWkEXz+T54pcATcawEq3HxdFwiSW0eLAMNt8hV50vxXHoU/JmO8E/+o 2xTaYfvAEqokzhMR8cHS0ayb/WO+AhRz0isIs= 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=s0vnPz+7bAK7O8lTGIDYy/yK3bIMSOWOoAvziPY8esM=; b=Gt6UGqW6XbzuIAgwm8ST3ucrd3ForEMsHm71x3ll4CjfWq1h41SGl4W2b3jPYlxCgj YFewfMCnVJpQQ+pDYPQNjHRdHIOpFffq4SmiyKOjKhIoB6xN6IS0qLVHIP9ANhIhfEUK gRBhKQ43Nhb0UCMDka7go7+ThValnoTfI0xMzj5gmUwFxl2bmek1jBst0lIhF5u7NTbm aKJjtb8iHR6daWdeXCdokBV33ODBIug5Rzu8nKjJxoZuebJAPFDrOFGlLVq6SONQ1YpC vFS4jDE9VQIIfVf+KcXNkz+XFcTUovc0xAwk3VdaGfLrWANwFEt39jauBoboQ1ADGV/H 2Z9g== X-Gm-Message-State: AMke39lAwAlYd/YFfc1UyL0z2S7OFhbVAw5Hr7OrRXlZ1IaE1Jb18eKdXwLPT46VrWL0a3p8 X-Received: by 10.99.209.85 with SMTP id c21mr25791014pgj.147.1487580712419; Mon, 20 Feb 2017 00:51:52 -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.46 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 20 Feb 2017 00:51:52 -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 2/4] usb: gadget: Support for the usb charger framework Date: Mon, 20 Feb 2017 16:51:08 +0800 Message-Id: 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 For supporting the usb charger, it adds the usb_charger_init() and usb_charger_exit() functions for usb charger initialization and exit. It will report to the usb charger when the gadget state is changed, then the usb charger can do the power things. Signed-off-by: Baolin Wang Reviewed-by: Li Jun Tested-by: Li Jun --- drivers/usb/gadget/udc/core.c | 19 ++++++++++++++++++- include/linux/usb/gadget.h | 3 +++ 2 files changed, 21 insertions(+), 1 deletion(-) -- 1.7.9.5 diff --git a/drivers/usb/gadget/udc/core.c b/drivers/usb/gadget/udc/core.c index 9483489..90df022 100644 --- a/drivers/usb/gadget/udc/core.c +++ b/drivers/usb/gadget/udc/core.c @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -576,12 +577,17 @@ int usb_gadget_vbus_connect(struct usb_gadget *gadget) * reporting how much power the device may consume. For example, this * could affect how quickly batteries are recharged. * + * It will also notify the USB charger how much power the device may + * consume if there is a USB charger linking with the gadget. + * * Returns zero on success, else negative errno. */ int usb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA) { int ret = 0; + usb_charger_set_cur_limit_by_gadget(gadget, mA); + if (!gadget->ops->vbus_draw) { ret = -EOPNOTSUPP; goto out; @@ -963,6 +969,9 @@ static void usb_gadget_state_work(struct work_struct *work) struct usb_gadget *gadget = work_to_gadget(work); struct usb_udc *udc = gadget->udc; + /* when the gadget state is changed, then report to USB charger */ + usb_charger_plug_by_gadget(gadget, gadget->state); + if (udc) sysfs_notify(&udc->dev.kobj, NULL, "state"); } @@ -1132,6 +1141,10 @@ int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget, if (ret) goto err4; + ret = usb_charger_init(gadget); + if (ret) + goto err5; + usb_gadget_set_state(gadget, USB_STATE_NOTATTACHED); udc->vbus = true; @@ -1143,7 +1156,7 @@ int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget, if (ret != -EPROBE_DEFER) list_del(&driver->pending); if (ret) - goto err5; + goto err6; break; } } @@ -1152,6 +1165,9 @@ int usb_add_gadget_udc_release(struct device *parent, struct usb_gadget *gadget, return 0; +err6: + usb_charger_exit(gadget); + err5: device_del(&udc->dev); @@ -1263,6 +1279,7 @@ void usb_del_gadget_udc(struct usb_gadget *gadget) kobject_uevent(&udc->dev.kobj, KOBJ_REMOVE); flush_work(&gadget->work); device_unregister(&udc->dev); + usb_charger_exit(gadget); device_unregister(&gadget->dev); } EXPORT_SYMBOL_GPL(usb_del_gadget_udc); diff --git a/include/linux/usb/gadget.h b/include/linux/usb/gadget.h index e4516e9..b4d7be2 100644 --- a/include/linux/usb/gadget.h +++ b/include/linux/usb/gadget.h @@ -24,6 +24,7 @@ #include #include #include +#include #define UDC_TRACE_STR_MAX 512 @@ -328,6 +329,7 @@ struct usb_gadget_ops { * @in_epnum: last used in ep number * @mA: last set mA value * @otg_caps: OTG capabilities of this gadget. + * @charger: Negotiate the power with the usb charger. * @sg_supported: true if we can handle scatter-gather * @is_otg: True if the USB device port uses a Mini-AB jack, so that the * gadget driver must provide a USB OTG descriptor. @@ -387,6 +389,7 @@ struct usb_gadget { unsigned in_epnum; unsigned mA; struct usb_otg_caps *otg_caps; + struct usb_charger *charger; unsigned sg_supported:1; unsigned is_otg:1; 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; } From patchwork Mon Feb 20 08:51:10 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: 94183 Delivered-To: patch@linaro.org Received: by 10.182.3.34 with SMTP id 2csp1241739obz; Mon, 20 Feb 2017 00:52:22 -0800 (PST) X-Received: by 10.99.102.134 with SMTP id a128mr25882845pgc.215.1487580742835; 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.22; 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 S1752485AbdBTIwL (ORCPT + 25 others); Mon, 20 Feb 2017 03:52:11 -0500 Received: from mail-pg0-f44.google.com ([74.125.83.44]:36571 "EHLO mail-pg0-f44.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752444AbdBTIwH (ORCPT ); Mon, 20 Feb 2017 03:52:07 -0500 Received: by mail-pg0-f44.google.com with SMTP id s67so5487991pgb.3 for ; Mon, 20 Feb 2017 00:52:07 -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=mgliuD5EwJS3YvKgMaOoykCsMSpE4SnK2/UH/Ezf1FQ=; b=LDc5BC31x0EPYMMtSlcakhNo5PB/9Z1Rgpme3yrb97a5ahrzdsDT/fHDOMlJ5YB8lO KXmYRpTwWSM8U/cEBOM+QBN1zFL8MlAbQdbQoUoPiY/paquU5LZj+eaUmXpaKD7ST1Vi iqgkgwEgdEuXvTmhHTDKYCLDKNTBxr6AfCsb4= 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=mgliuD5EwJS3YvKgMaOoykCsMSpE4SnK2/UH/Ezf1FQ=; b=fHA2uI7PxKJjksNEz4EzqVCdNsOtMLkBOAExn9tf/GhbVtNx/cDBwY5jNBvL2+FFBH rFPG59jOiLyYPxsOVp5SunacWNOGbgtgE8UCr1RczSXiUcuyzQFAXfq1qKUCKK3Im393 9mkmiwTM2xmdZeQoDKGQfinWxjUOO462euOlTiphPDKbhKE05dk5IMRvJSWQ1jQmZdj1 SG3VFR5gyh+XR6ov41XClUHPtnnsNwxZNtQfMkX+lT2ByC7Xvlmg+1Z+YTgzjvJgMdI+ YwM+GPSI8fRLWaBDvLQsM3BkdidRM5m00qzKe14HU2KM/F3RFwvq3qcQ+3Ag4F0Uk4xz bIKg== X-Gm-Message-State: AMke39mgOuZI0vEaoY8dkLEu50T6Y7wKbvISkSgR/ESzfuevzOZE3Zr6DTv14tQGoz7RY+h0 X-Received: by 10.98.216.202 with SMTP id e193mr5712862pfg.80.1487580726624; Mon, 20 Feb 2017 00:52:06 -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.59 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 20 Feb 2017 00:52:06 -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 4/4] power: wm831x_power: Support USB charger current limit management Date: Mon, 20 Feb 2017 16:51:10 +0800 Message-Id: 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 Integrate with the newly added USB charger interface to limit the current we draw from the USB input based on the input device configuration identified by the USB stack, allowing us to charge more quickly from high current inputs without drawing more current than specified from others. Signed-off-by: Mark Brown Signed-off-by: Baolin Wang Acked-by: Lee Jones Acked-by: Charles Keepax Acked-by: Peter Chen Acked-by: Sebastian Reichel --- drivers/power/supply/wm831x_power.c | 63 +++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) -- 1.7.9.5 diff --git a/drivers/power/supply/wm831x_power.c b/drivers/power/supply/wm831x_power.c index 7082301..b5f1652 100644 --- a/drivers/power/supply/wm831x_power.c +++ b/drivers/power/supply/wm831x_power.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -31,6 +32,8 @@ struct wm831x_power { char usb_name[20]; char battery_name[20]; bool have_battery; + struct usb_charger *usb_charger; + struct notifier_block usb_notify; }; static int wm831x_power_check_online(struct wm831x *wm831x, int supply, @@ -125,6 +128,42 @@ static int wm831x_usb_get_prop(struct power_supply *psy, POWER_SUPPLY_PROP_VOLTAGE_NOW, }; +/* In milliamps */ +static const unsigned int wm831x_usb_limits[] = { + 0, + 2, + 100, + 500, + 900, + 1500, + 1800, + 550, +}; + +static int wm831x_usb_limit_change(struct notifier_block *nb, + unsigned long limit, void *data) +{ + struct wm831x_power *wm831x_power = container_of(nb, + struct wm831x_power, + usb_notify); + + /* Find the highest supported limit */ + best = 0; + for (i = 0; i < ARRAY_SIZE(wm831x_usb_limits); i++) { + if (limit >= wm831x_usb_limits[i] && + wm831x_usb_limits[best] < wm831x_usb_limits[i]) + best = i; + } + + dev_dbg(wm831x_power->wm831x->dev, + "Limiting USB current to %umA", wm831x_usb_limits[best]); + + wm831x_set_bits(wm831x_power->wm831x, WM831X_POWER_STATE, + WM831X_USB_ILIM_MASK, best); + + return 0; +} + /********************************************************************* * Battery properties *********************************************************************/ @@ -607,8 +646,26 @@ static int wm831x_power_probe(struct platform_device *pdev) } } + power->usb_charger = usb_charger_get_instance(); + if (IS_ERR(power->usb_charger)) { + ret = PTR_ERR(power->usb_charger); + dev_err(&pdev->dev, "Failed to find USB gadget: %d\n", ret); + goto err_bat_irq; + } + + power->usb_notify.notifier_call = wm831x_usb_limit_change; + + ret = usb_charger_register_notify(power->usb_charger, + &power->usb_notify); + if (ret != 0) { + dev_err(&pdev->dev, "Failed to register notifier: %d\n", ret); + goto err_usb_charger; + } + return ret; +err_usb_charger: + /* put_device on charger */ err_bat_irq: --i; for (; i >= 0; i--) { @@ -637,6 +694,12 @@ static int wm831x_power_remove(struct platform_device *pdev) struct wm831x *wm831x = wm831x_power->wm831x; int irq, i; + if (wm831x_power->usb_charger) { + usb_charger_unregister_notify(wm831x_power->usb_charger, + &wm831x_power->usb_notify); + /* Free charger */ + } + for (i = 0; i < ARRAY_SIZE(wm831x_bat_irqs); i++) { irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev,