From patchwork Wed Dec 11 15:50:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Cercueil X-Patchwork-Id: 11285565 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D206B15AB for ; Wed, 11 Dec 2019 15:50:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id A9FEC208C3 for ; Wed, 11 Dec 2019 15:50:55 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=crapouillou.net header.i=@crapouillou.net header.b="ZlMuCBpv" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732896AbfLKPun (ORCPT ); Wed, 11 Dec 2019 10:50:43 -0500 Received: from outils.crapouillou.net ([89.234.176.41]:46474 "EHLO crapouillou.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730557AbfLKPuj (ORCPT ); Wed, 11 Dec 2019 10:50:39 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crapouillou.net; s=mail; t=1576079437; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references; bh=eyHslJzjVa1ut29atCn8FHuMjuQZsdMV5FkGrDuakgI=; b=ZlMuCBpvsUIzRzP/io42tvAT+E9mKmAIajMDbtavzOClfCHRGLdx9kK95nlS7PLiRBt39g xcNPvErDQGRe4WZejsYe6YiJeg3d9pHm86dRc0hbqBASPxumHaY/ycKPhz5I2cpaFDPFKV oMfNne0EmtF17f8LxJjA3pgDINGV+co= From: Paul Cercueil To: Sebastian Reichel , Rob Herring , Mark Rutland , Greg Kroah-Hartman Cc: od@zcrc.me, linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, Paul Cercueil Subject: [PATCH v2 1/3] dt-bindings: power/supply: Document generic USB charger Date: Wed, 11 Dec 2019 16:50:30 +0100 Message-Id: <20191211155032.167032-1-paul@crapouillou.net> MIME-Version: 1.0 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Add documentation about the devicetree bindings for the generic USB charger. Signed-off-by: Paul Cercueil --- Notes: v2: Convert to YAML .../bindings/power/supply/usb-charger.yaml | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 Documentation/devicetree/bindings/power/supply/usb-charger.yaml diff --git a/Documentation/devicetree/bindings/power/supply/usb-charger.yaml b/Documentation/devicetree/bindings/power/supply/usb-charger.yaml new file mode 100644 index 000000000000..dcd705df6e73 --- /dev/null +++ b/Documentation/devicetree/bindings/power/supply/usb-charger.yaml @@ -0,0 +1,41 @@ +# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/power/supply/usb-charger.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Generic USB charger driver bindings + +maintainers: + - Paul Cercueil + +description: | + Devicetree bindings for a generic USB charger. + + The node should be either a child of a USB node, or connect to the USB + node through the graph. The USB node must have the usb-role-switch property + set. The USB charger will report as online when the USB role is set to device, + and offline otherwise. + +properties: + compatible: + const: usb-charger + + port: true + +required: + - compatible + +additionalProperties: false + +examples: + - |+ + usb_charger: usb-charger { + compatible = "usb-charger"; + + port { + usb_charger_ep: endpoint { + remote-endpoint = <&usb_otg>; + }; + }; + }; From patchwork Wed Dec 11 15:50:31 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Cercueil X-Patchwork-Id: 11285563 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B91F915AB for ; Wed, 11 Dec 2019 15:50:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 983F224654 for ; Wed, 11 Dec 2019 15:50:53 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=crapouillou.net header.i=@crapouillou.net header.b="KF5zXw54" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731483AbfLKPus (ORCPT ); Wed, 11 Dec 2019 10:50:48 -0500 Received: from outils.crapouillou.net ([89.234.176.41]:46516 "EHLO crapouillou.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2387414AbfLKPur (ORCPT ); Wed, 11 Dec 2019 10:50:47 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crapouillou.net; s=mail; t=1576079438; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sz/wsBUGRl/3B6yGNXWjSD/WW8og/T1ubMIdBPmaXDI=; b=KF5zXw54QJn/POO8s9HQHsxBSkMEjBV1Q/eTMTSJ03M67Owaj8GZW6NxnSxxsCMXPXaR9Y Bx7/InBQCoFksg6+jpPUM+5T7k56jTU1f088Gw3JlYtbNX7jV1uqpACN+F7DzKc23wvo37 zbHfTyfz8jWKmmxVC33rl7QkhpiuaKM= From: Paul Cercueil To: Sebastian Reichel , Rob Herring , Mark Rutland , Greg Kroah-Hartman Cc: od@zcrc.me, linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, Paul Cercueil Subject: [PATCH v2 2/3] usb: roles: Add API to register notifiers Date: Wed, 11 Dec 2019 16:50:31 +0100 Message-Id: <20191211155032.167032-2-paul@crapouillou.net> In-Reply-To: <20191211155032.167032-1-paul@crapouillou.net> References: <20191211155032.167032-1-paul@crapouillou.net> MIME-Version: 1.0 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org Add usb_role_switch_notifier_register() and usb_role_switch_notifier_unregister(). The registered notifiers will be called when the USB role is changed. Signed-off-by: Paul Cercueil --- Notes: v2: New patch drivers/usb/roles/class.c | 24 +++++++++++++++++++++++- include/linux/usb/role.h | 20 ++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/drivers/usb/roles/class.c b/drivers/usb/roles/class.c index 8273126ffdf4..9b122b504b98 100644 --- a/drivers/usb/roles/class.c +++ b/drivers/usb/roles/class.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -19,6 +20,7 @@ static struct class *role_class; struct usb_role_switch { struct device dev; struct mutex lock; /* device lock*/ + struct blocking_notifier_head notifier_chain; enum usb_role role; /* From descriptor */ @@ -49,8 +51,11 @@ int usb_role_switch_set_role(struct usb_role_switch *sw, enum usb_role role) mutex_lock(&sw->lock); ret = sw->set(sw->dev.parent, role); - if (!ret) + if (!ret) { sw->role = role; + ret = blocking_notifier_call_chain(&sw->notifier_chain, + (long)role, sw); + } mutex_unlock(&sw->lock); @@ -85,6 +90,22 @@ enum usb_role usb_role_switch_get_role(struct usb_role_switch *sw) } EXPORT_SYMBOL_GPL(usb_role_switch_get_role); +int +usb_role_switch_register_notifier(struct usb_role_switch *sw, + struct notifier_block *nb) +{ + return blocking_notifier_chain_register(&sw->notifier_chain, nb); +} +EXPORT_SYMBOL_GPL(usb_role_switch_register_notifier); + +int +usb_role_switch_unregister_notifier(struct usb_role_switch *sw, + struct notifier_block *nb) +{ + return blocking_notifier_chain_unregister(&sw->notifier_chain, nb); +} +EXPORT_SYMBOL_GPL(usb_role_switch_unregister_notifier); + static void *usb_role_switch_match(struct device_connection *con, int ep, void *data) { @@ -317,6 +338,7 @@ usb_role_switch_register(struct device *parent, return ERR_PTR(-ENOMEM); mutex_init(&sw->lock); + BLOCKING_INIT_NOTIFIER_HEAD(&sw->notifier_chain); sw->allow_userspace_control = desc->allow_userspace_control; sw->usb2_port = desc->usb2_port; diff --git a/include/linux/usb/role.h b/include/linux/usb/role.h index efac3af83d6b..5f67d42cd28d 100644 --- a/include/linux/usb/role.h +++ b/include/linux/usb/role.h @@ -6,6 +6,7 @@ #include struct usb_role_switch; +struct notifier_block; enum usb_role { USB_ROLE_NONE, @@ -50,6 +51,11 @@ struct usb_role_switch *usb_role_switch_get(struct device *dev); struct usb_role_switch *fwnode_usb_role_switch_get(struct fwnode_handle *node); void usb_role_switch_put(struct usb_role_switch *sw); +int usb_role_switch_register_notifier(struct usb_role_switch *sw, + struct notifier_block *nb); +int usb_role_switch_unregister_notifier(struct usb_role_switch *sw, + struct notifier_block *nb); + struct usb_role_switch * usb_role_switch_find_by_fwnode(const struct fwnode_handle *fwnode); @@ -82,6 +88,20 @@ fwnode_usb_role_switch_get(struct fwnode_handle *node) static inline void usb_role_switch_put(struct usb_role_switch *sw) { } +static inline int +usb_role_switch_register_notifier(struct usb_role_switch *sw, + struct notifier_block *nb) +{ + return ERR_PTR(-ENODEV); +} + +static inline int +usb_role_switch_unregister_notifier(struct usb_role_switch *sw, + struct notifier_block *nb) +{ + return ERR_PTR(-ENODEV); +} + static inline struct usb_role_switch * usb_role_switch_register(struct device *parent, const struct usb_role_switch_desc *desc) From patchwork Wed Dec 11 15:50:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paul Cercueil X-Patchwork-Id: 11285569 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 45920109A for ; Wed, 11 Dec 2019 15:51:02 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1A447208C3 for ; Wed, 11 Dec 2019 15:51:02 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=crapouillou.net header.i=@crapouillou.net header.b="fcYbF9M0" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2387827AbfLKPu4 (ORCPT ); Wed, 11 Dec 2019 10:50:56 -0500 Received: from outils.crapouillou.net ([89.234.176.41]:46536 "EHLO crapouillou.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731716AbfLKPuz (ORCPT ); Wed, 11 Dec 2019 10:50:55 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=crapouillou.net; s=mail; t=1576079439; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=RAVv3nHLEk4RhtK2wNxT3ZoIYEuNj+Sa4a9OHRLEg+4=; b=fcYbF9M0WWNNHOSZVEr3yqqvshp+T3gCdnENeCtOquVxjdmey5y+VHle0wEGjpGppX80go ytchmUZhOsAk5z08V62GwKywziD1nubcqX2VJ+IfojC7sWs4dfbKsUhSpLQjIfodugtKkW WSRYcQ/Cofy4OltczOiUYW03O5aevxY= From: Paul Cercueil To: Sebastian Reichel , Rob Herring , Mark Rutland , Greg Kroah-Hartman Cc: od@zcrc.me, linux-pm@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-usb@vger.kernel.org, Paul Cercueil Subject: [PATCH v2 3/3] power/supply: Add generic USB charger driver Date: Wed, 11 Dec 2019 16:50:32 +0100 Message-Id: <20191211155032.167032-3-paul@crapouillou.net> In-Reply-To: <20191211155032.167032-1-paul@crapouillou.net> References: <20191211155032.167032-1-paul@crapouillou.net> MIME-Version: 1.0 Sender: linux-pm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org This simple charger driver uses the USB role switch framework to detect the presence of a charger. The USB charger will report as online when the USB role is set to device, and offline otherwise. Signed-off-by: Paul Cercueil --- Notes: v2: Instead of detecting charger state from USB PHY, we detect it from the USB role in use. drivers/power/supply/Kconfig | 8 ++ drivers/power/supply/Makefile | 1 + drivers/power/supply/generic-usb-charger.c | 124 +++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 drivers/power/supply/generic-usb-charger.c diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig index 27164a1d3c7c..c4221bcabee4 100644 --- a/drivers/power/supply/Kconfig +++ b/drivers/power/supply/Kconfig @@ -51,6 +51,14 @@ config GENERIC_ADC_BATTERY Say Y here to enable support for the generic battery driver which uses IIO framework to read adc. +config GENERIC_USB_CHARGER + tristate "Generic USB charger" + depends on USB_SUPPORT + select USB_ROLE_SWITCH + help + Say Y here to enable a generic USB charger driver which uses + the USB role switch framework to detect the presence of the charger. + config MAX8925_POWER tristate "MAX8925 battery charger support" depends on MFD_MAX8925 diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile index 6c7da920ea83..03f9b553bdfc 100644 --- a/drivers/power/supply/Makefile +++ b/drivers/power/supply/Makefile @@ -8,6 +8,7 @@ power_supply-$(CONFIG_LEDS_TRIGGERS) += power_supply_leds.o obj-$(CONFIG_POWER_SUPPLY) += power_supply.o obj-$(CONFIG_POWER_SUPPLY_HWMON) += power_supply_hwmon.o obj-$(CONFIG_GENERIC_ADC_BATTERY) += generic-adc-battery.o +obj-$(CONFIG_GENERIC_USB_CHARGER) += generic-usb-charger.o obj-$(CONFIG_PDA_POWER) += pda_power.o obj-$(CONFIG_APM_POWER) += apm_power.o diff --git a/drivers/power/supply/generic-usb-charger.c b/drivers/power/supply/generic-usb-charger.c new file mode 100644 index 000000000000..0493fafbd4c0 --- /dev/null +++ b/drivers/power/supply/generic-usb-charger.c @@ -0,0 +1,124 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Simple USB charger driver + * Copyright (c) 2019 Paul Cercueil + */ + +#include +#include +#include +#include +#include +#include + +struct usb_charger { + struct notifier_block nb; + struct usb_role_switch *role; + struct power_supply_desc desc; + struct power_supply *charger; +}; + +static enum power_supply_property usb_charger_properties[] = { + POWER_SUPPLY_PROP_ONLINE, +}; + +static int usb_charger_get_property(struct power_supply *psy, + enum power_supply_property psp, + union power_supply_propval *val) +{ + struct usb_charger *charger = power_supply_get_drvdata(psy); + enum usb_role role; + + switch (psp) { + case POWER_SUPPLY_PROP_ONLINE: + role = usb_role_switch_get_role(charger->role); + val->intval = role == USB_ROLE_DEVICE; + break; + default: + return -EINVAL; + } + + return 0; +} + +static int usb_charger_event(struct notifier_block *nb, + unsigned long event, void *d) +{ + struct usb_charger *charger = container_of(nb, struct usb_charger, nb); + + power_supply_changed(charger->charger); + + return 0; +} + +static void usb_charger_unregister(void *data) +{ + struct usb_charger *charger = data; + + usb_role_switch_unregister_notifier(charger->role, &charger->nb); +} + +static int usb_charger_probe(struct platform_device *pdev) +{ + struct device *dev = &pdev->dev; + struct power_supply_desc *desc; + struct usb_charger *charger; + struct power_supply_config cfg = { + .of_node = dev->of_node, + }; + int err; + + charger = devm_kzalloc(dev, sizeof(*charger), GFP_KERNEL); + if (!charger) + return -ENOMEM; + + cfg.drv_data = charger; + charger->nb.notifier_call = usb_charger_event; + + charger->role = usb_role_switch_get(dev); + if (IS_ERR(charger->role)) { + if (PTR_ERR(charger->role) != -EPROBE_DEFER) + dev_err(dev, "Unable to get USB role"); + return PTR_ERR(charger->role); + } + + desc = &charger->desc; + desc->name = "usb-charger"; + desc->properties = usb_charger_properties; + desc->num_properties = ARRAY_SIZE(usb_charger_properties); + desc->get_property = usb_charger_get_property; + desc->type = POWER_SUPPLY_TYPE_USB; + + charger->charger = devm_power_supply_register(dev, desc, &cfg); + if (IS_ERR(charger->charger)) { + dev_err(dev, "Unable to register charger"); + return PTR_ERR(charger->charger); + } + + err = usb_role_switch_register_notifier(charger->role, &charger->nb); + if (err) { + dev_err(dev, "Unable to register USB role switch notifier"); + return err; + } + + return devm_add_action_or_reset(dev, usb_charger_unregister, charger); +} + +static const struct of_device_id usb_charger_of_match[] = { + { .compatible = "usb-charger" }, + { /* sentinel */ }, +}; +MODULE_DEVICE_TABLE(of, usb_charger_of_match); + +static struct platform_driver usb_charger_driver = { + .driver = { + .name = "usb-charger", + .of_match_table = of_match_ptr(usb_charger_of_match), + }, + .probe = usb_charger_probe, +}; +module_platform_driver(usb_charger_driver); + +MODULE_DESCRIPTION("Simple USB charger driver"); +MODULE_AUTHOR("Paul Cercueil "); +MODULE_LICENSE("GPL");